如何获得单个提交的每个更改文件的补丁



我试图通过挖掘git存储库来获取有关提交历史的一些信息。我正在使用libgit2sharp包。

到目前为止,我得到了提交作者、提交者、sha-value、提交日期和提交消息。我的问题是要遍历存储库树,以获得每次提交的所有更改文件的补丁。 有谁以前解决过这个问题,或者可以帮助我吗?
using (var repo = new Repository(@"pathto.git"))
            {
                var commits = repo.Commits; 
                Commit lastCommit = commits.Last();
                foreach (Commit commit in commits)
                    if (commit.Sha != lastCommit.Sha)
                    {
                        Console.WriteLine(commit.Sha);
                        Console.WriteLine(commit.Author.Name);
                        Console.WriteLine(commit.Committer.Name);
                        Console.WriteLine(commit.Author.When); //Commit-Date
                        Console.WriteLine(commit.Message);
                        Tree tree = commit.Tree;
                        Tree parentCommitTree = lastCommit.Tree; 
                        TreeChanges changes = repo.Diff.Compare<TreeChanges>(parentCommitTree, tree);
                        foreach (TreeEntryChanges treeEntryChanges in changes)
                        {
                            ObjectId oldcontenthash = treeEntryChanges.OldOid;
                            ObjectId newcontenthash = treeEntryChanges.Oid;
                        } 
                     }   
            }

另一个尝试是下面的代码。显示了根目录下的文件和文件夹,但是无法打开。

foreach(TreeEntry treeEntry in tree)
   {
    // Blob blob1 = (Blob)treeEntry.Target;
    var targettype = treeEntry.TargetType;
    if (targettype == TreeEntryTargetType.Blob)
      {
        string filename = treeEntry.Name;
        string path = treeEntry.Path;
        string sha = treeEntry.Target.Sha;
        var filemode = treeEntry.Mode;
        Console.WriteLine(filename);
        Console.WriteLine(path);
      }
      else if (targettype == TreeEntryTargetType.Tree)
        {
         Console.WriteLine("Folder: " + treeEntry.Name);
        }
   }

>(如何)获得每次提交的所有更改文件的补丁?

使用Diff.Compare<Patch>()方法,将您愿意比较的每个CommitTree传递给它。

Tree commitTree1 = repo.Lookup<Commit>("f8d44d7").Tree;
Tree commitTree2 = repo.Lookup<Commit>("7252fe2").Tree;
var patch = repo.Diff.Compare<Patch>(commitTree1, commitTree2);

可以通过查看DiffTreeToTreeFixture.cs测试套件中的测试方法CanCompareTwoVersionsOfAFileWithADiffOfTwoHunks()找到更多的使用细节。

>另一个尝试是下面的代码。

显示了根目录下的文件和文件夹,但是无法打开。

每个TreeEntry公开一个Target属性,返回指向GitObject的值。

TargetTypeTreeEntryTargetType.Tree类型时,为了检索此子Tree,您必须使用以下命令:

var subTree = (Tree)treeEntry.Target;

谢谢你的回答!

现在我收到两个提交之间的补丁。使用下面的代码,OutOfMemoryException经常被抛出。

LibGit2Sharp.Commit lastCommit = commits.First();
repository.CommitCount = commits.Count();
 foreach (LibGit2Sharp.Commit commit in commits) 
    if (commit.Sha != lastCommit.Sha)
      {
       Tree commitTree1 = repo.Lookup<LibGit2Sharp.Commit>(lastCommit.Sha).Tree;
       Tree commitTree2 = repo.Lookup<LibGit2Sharp.Commit>(commit.Sha).Tree;
       var patch = repo.Diff.Compare<Patch>(commitTree1, commitTree2);
       // some value assigments                        
        lastCommit = commit;
  }

最新更新