2014-07-18 59 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

相關問題