从LibGit2Sharp中的提交中获取已修改/添加/删除的文件

时间:2023-01-26 15:10:53

I've 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 did a look on this question, but I'm newbie with C# and don't understand.

我看了一下这个问题,但我是C#的新手并且不明白。

I have this repository:

我有这个存储库:

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

My last commit have this files modified:

我上次提交修改了这些文件:

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

With my method GetFiles I've all files like in this print. How get only the modified/added/removed files?

使用我的GetFiles方法,我所有的文件都在这个打印中。如何只获得修改/添加/删除的文件?

从LibGit2Sharp中的提交中获取已修改/添加/删除的文件


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

如何获得先前的提交并比较获得差异树?


Solution

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

从LibGit2Sharp中的提交中获取已修改/添加/删除的文件

1 个解决方案

#1


Because of how git store data in the file systems, a git commit is a snapshot of all the files contained in the commit. Then the Tree object return you the state of all the files of the repository.

由于git如何在文件系统中存储数据,git commit是提交中包含的所有文件的快照。然后Tree对象返回存储库的所有文件的状态。

I think that you must do the diff between the tree of this commit and the tree of the previous one and I think it should be done with the method Repository.Diff.Compare()

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

#1


Because of how git store data in the file systems, a git commit is a snapshot of all the files contained in the commit. Then the Tree object return you the state of all the files of the repository.

由于git如何在文件系统中存储数据,git commit是提交中包含的所有文件的快照。然后Tree对象返回存储库的所有文件的状态。

I think that you must do the diff between the tree of this commit and the tree of the previous one and I think it should be done with the method Repository.Diff.Compare()

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