2014-07-18 61 views
1

我试图通过挖掘一个混帐存储库,以获取有关提交历史的一些信息。我正在使用包libgit2sharp。如何获取单个提交的每个更改文件的修补程序?

到目前为止,我提交的作者,提交者,SHA-值,提交时间和提交消息。我的问题是移动存储库树,以获取每个提交的所有更改文件的修补程序。

有谁之前解决这个问题,或者它可以帮助我吗?

using (var repo = new Repository(@"path\to\.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); 
     } 
    } 

回答

3

>(如何)获得每一个的所有修改文件的补丁提交?

使用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); 

人们可以通过考虑看看测试找到更多的使用细节metthod在DiffTreeToTreeFixture.cs CanCompareTwoVersionsOfAFileWithADiffOfTwoHunks()测试套件。

>另一个艰难的是下面的代码。它显示了根级别的文件和文件夹,但我无法打开文件夹。

每个TreeEntry公开一个Target属性返回指向GitObject

TargetTypeTreeEntryTargetType.Tree型的,为了找回这个孩子Tree,你必须使用以下命令:

var subTree = (Tree)treeEntry.Target; 
1

感谢您的回答!

现在我收到两次提交的补丁。使用以下代码,通常会抛出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; 
    } 
+0

例外是不应该发生的。您能否向** [问题跟踪器](https://github.com/libgit2/libgit2sharp/issues/new)**提交完整的repro案件? – nulltoken

+0

这是完整的代码。它发生在ca. 7.000提交 – Thomas

+0

请在跟踪器中提交一个专用问题以及您正在使用的公共存储库的URL – nulltoken

相关问题