Get files modified / added / deleted from commit in LibGit2Sharp

I have this method where I get files from my last commit:

static void GetFiles(Tree t, String dir = "") { foreach (TreeEntry treeEntry in t) { if (treeEntry.TargetType == TreeEntryTargetType.Tree) { Tree tr = repo.Lookup<Tree>(treeEntry.Target.Sha); GetFiles(tr, dir + "/" + treeEntry.Name); } else { string caminho = dir + "/" + treeEntry.Path; arquivos.Add(caminho); } } return; } 

I looked at this question , but I'm new to C # and don't understand.

I have this repository:

 c:/teste | - octocat.txt | - parentoctocat.txt | - /outros | | - octocatblue.txt | | - octored.txt 

My last commit changed these files:

 c:/teste | - /outros | | - octocatblue.txt <- This modified | | - octored.txt <- This new 

With my GetFiles method, I have all the files, as in this print. How to get only modified / added / deleted files?

Program execution


How can I get the previous commit and compare the resulting difference tree?


Decision

 static void CompareTrees() { using (repo) { Tree commitTree = repo.Head.Tip.Tree; // Main Tree Tree parentCommitTree = repo.Head.Tip.Parents.First().Tree; // Secondary Tree var patch = repo.Diff.Compare<Patch>(parentCommitTree, commitTree); // Difference foreach (var ptc in patch) { Console.WriteLine(ptc.Status +" -> "+ptc.Path); // Status -> File Path } } } 

Solution

+11
git c # libgit2sharp
source share
1 answer

Because git stores data on file systems, committing git is a snapshot of all the files contained in the commit. Then the Tree object returns you the status of all repository files.

I think you should make a difference between the tree of this commit and the tree of the previous one, and I think that this should be done using the Repository.Diff.Compare() method

+5
source share

All Articles