从 LibGit2Sharp 中的提交中获取文件 modified/added/removed

Get files modified/added/removed from a commit in LibGit2Sharp

我有这个方法,我从上次提交中获取文件:

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;
}

我看过 this question,但我是 C# 的新手,不明白。

我有这个存储库:

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

我的最后一次提交修改了这个文件:

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

用我的方法 GetFiles 我得到了所有与此印刷品类似的文件。如何只获取 modified/added/removed 个文件?


如何获取之前的提交并比较差异树?


解决方案

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
        }
    }
}

由于 git 在文件系统中存储数据的方式,git 提交是提交中包含的所有文件的快照。然后 Tree 对象 return 你存储库所有文件的状态。

我认为你必须在这次提交的树和前一次提交的树之间做差异,我认为应该用方法 Repository.Diff.Compare()