【问题标题】:Get files modified/added/removed from a commit in LibGit2Sharp从 LibGit2Sharp 中的提交中获取修改/添加/删除的文件
【发布时间】:2015-07-24 17:03:29
【问题描述】:

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

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,我已经拥有了与此打印类似的所有文件。如何只获取修改/添加/删除的文件?


如何获取上一次提交并比较获取差异树?


解决方案

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

【问题讨论】:

    标签: c# git libgit2sharp


    【解决方案1】:

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

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

    【讨论】:

    • 你能帮我改变我的方法GetFiles 这样做吗?
    猜你喜欢
    • 2012-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-18
    • 1970-01-01
    相关资源
    最近更新 更多