无法从libgit2sharp

时间:2018-08-29 15:37:31

标签: libgit2sharp

我想出了以下代码来识别最新提交和上一次提交之间的更改文件。此逻辑显示已更改文件的名称。但是,它不会记录“已添加”文件的名称。我正在使用GitLab存储库进行测试。请提出解决方法的建议。

    static void Main(string[] args)
    {            
        using (var repo = new Repository(@"C:\xxxx\source\repos\SampleApp"))
        {
            //Get the brach
            var branch = repo.Branches.Where(b => !b.IsRemote && b.IsCurrentRepositoryHead).FirstOrDefault();

            //Get the latest commit
            var latestCommit = branch.Commits.ElementAt(0);
            Console.WriteLine(string.Format("Latest Commit: {0}-{1}", latestCommit.MessageShort, latestCommit.Committer.When));

            //Get the previous comit
            var previousCommit = branch.Commits.ElementAt(1);
            Console.WriteLine(string.Format("Previous Commit: {0}-{1}", previousCommit.MessageShort, previousCommit.Committer.When));

            //Get the change set
            var changeSet = repo.Diff.Compare<TreeChanges>(latestCommit.Tree, previousCommit.Tree);

            var modifiedFiles = changeSet.Modified;                
            var addedFiles = changeSet.Added;

            //Print names of modified files
            foreach (var m in modifiedFiles)
            {
                Console.WriteLine("Modified: " + m.Path);
            }

            //Print names of added files
            foreach (var m in addedFiles)
            {
                Console.WriteLine("Added: " + m.Path);
            }
            Console.Read();     
        }
    }
}

谢谢。

更新: 问题出在下面一行获取更改集的地方。我以其他方式使用旧树和新树参数。我已经交换了这些,现在工作正常。

//Get the change set
var changeSet = repo.Diff.Compare<TreeChanges>(previousCommit.Tree, latestCommit.Tree);

1 个答案:

答案 0 :(得分:0)

下面是完整的功能代码。

static void Main(string[] args)
        {            
            using (var repo = new Repository(@"C:\xxxx\source\repos\SampleApp"))
            {
                //Get the brach
                var branch = repo.Branches.Where(b => !b.IsRemote && b.IsCurrentRepositoryHead).FirstOrDefault();

                //Get the latest commit
                var latestCommit = branch.Commits.ElementAt(0);
                Console.WriteLine(string.Format("Latest Commit: {0}-{1}", latestCommit.MessageShort, latestCommit.Committer.When));

                //Get the previous comit
                var previousCommit = branch.Commits.ElementAt(1);
                Console.WriteLine(string.Format("Previous Commit: {0}-{1}", previousCommit.MessageShort, previousCommit.Committer.When));

                //Get the change set
                var changeSet = repo.Diff.Compare<TreeChanges>(previousCommit.Tree, latestCommit.Tree);

                var modifiedFiles = changeSet.Modified;                
                var addedFiles = changeSet.Added;

                //Print names of modified files
                foreach (var m in modifiedFiles)
                {
                    Console.WriteLine("Modified: " + m.Path);
                }

                //Print names of added files
                foreach (var m in addedFiles)
                {
                    Console.WriteLine("Added: " + m.Path);
                }
                Console.Read();     
            }
        }