获得文件修改/添加/从LibGit2Sharp提交移除 [英] Get files modified/added/removed from a commit in LibGit2Sharp

查看:723
本文介绍了获得文件修改/添加/从LibGit2Sharp提交移除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这种方法,在这里我从我的最后一次提交的文件获得:

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



我做的这个问题,但我新手用C#和不理解。

I did a look on this question, but I'm newbie with C# and don't understand.

我有这个资源库:

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

使用我的方法的GetFiles 我有所有的文件就像在此打印。如何只得到修改/添加/删除文件?

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

我怎样才能获得以前的承诺,并比较得到的区别树?

static void CompareTrees()
{
    using (repo)
    {
        Tree commitTree = repo.Head.Tip.Tree; // Main Tree
        Tree parentCommitTree = repo.Head.Tip.Parents.Single().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的承诺是包含在提交的所有文件的快照。那么对象回报,你的资料库中的所有文件的状态。

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.

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

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()

这篇关于获得文件修改/添加/从LibGit2Sharp提交移除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆