Libgit2Sharp:获取与初始(第一次)提交相关联的文件

时间:2017-01-11 21:15:35

标签: c# git libgit2 libgit2sharp

如何检索属于存储库第一次(初始)提交的文件?

我目前正在使用以下内容来查找属于提交的文件(并且它可以正常工作)。但是,由于该方法需要两个参数,我应该传递什么来获取属于第一次提交的文件?或者我需要使用另一种方法吗?

    repo.Diff.Compare<TreeChanges>(repo.Commits.ElementAt(i).Tree, repo.Commits.ElementAt(i + 1).Tree)

谢谢!

2 个答案:

答案 0 :(得分:2)

您可以轻松地在初始树和空树之间进行差异以捕获文件:

foreach (TreeEntryChanges change in repo.Diff.Compare<TreeChanges>(null, commit.Tree))
{
     Console.WriteLine("\t{0} :\t{1}", change.Status, change.Path);
}

答案 1 :(得分:0)

我能够使用以下内容实现我的要求:

                    //The tree object corresponding to the first commit in the repo
                    Tree firstCommit = repo.Lookup<Tree>(repo.Commits.ElementAt(i).Tree.Sha);
                    //The tree object corresponding to the last commit in the repo
                    Tree lastCommit = repo.Lookup<Tree>(repo.Commits.ElementAt(0).Tree.Sha);


                    var changes = repo.Diff.Compare<TreeChanges>(lastCommit, firstCommit);
                    foreach (var item in changes)
                    {
                        if (item.Status != ChangeKind.Deleted)
                        {
                            //...This object (i.e. item) corresponds to a file that was part of the first (initial) commit...
                        }
                    }

如果有更好的方法,请告诉我......

相关问题