2012-05-10 53 views
3

如何查找具有特定父級的存儲庫中的所有提交?如何查找具有特定父級的所有提交?

例如,如果我有一個提交A,我想找到與A共享父項的所有其他提交。什麼是LibGit2Sharp中最有效的,即高性能但正確的方法?

回答

3

這是一個棘手的問題;-)

Git的對象允許檢索提交的父母。但是,沒有簡單的方法來找到提交的孩子。

下面的代碼會部分解決這個問題。這個想法是從存儲庫的所有引用(頭部,標籤,...)執行git log,並且沿途選擇帶有所請求的SHA的父代的每個提交。由於從最近的提交到祖先路徑的工作正在進行,所以如果您正在一個非常大的歷史記錄和許多分支的存儲庫中搜索非常早的提交的子代,可能需要一些時間。

[Fact] 
public void CanRetrieveChildrenOfASpecificCommit() 
{ 
    TemporaryCloneOfTestRepo path = BuildTemporaryCloneOfTestRepo(StandardTestRepoPath); 
    using (var repo = new Repository(path.RepositoryPath)) 
    { 
     const string parentSha = "5b5b025afb0b4c913b4c338a42934a3863bf3644"; 

     var filter = new Filter 
         { 
          /* Revwalk from all the refs (git log --all) ... */ 
          Since = repo.Refs, 

          /* ... and stop when the parent is reached */ 
          Until = parentSha 
         }; 

     var commits = repo.Commits.QueryBy(filter); 

     var children = from c in commits 
        from p in c.Parents 
        let pId = p.Id 
        where pId.Sha == parentSha 
        select c; 

     var expectedChildren = new[] { "c47800c7266a2be04c571c04d5a6614691ea99bd", 
             "4a202b346bb0fb0db7eff3cffeb3c70babbd2045" }; 

     Assert.Equal(expectedChildren, children.Select(c => c.Id.Sha)); 
    } 
} 

限制:

  • 這不會檢索已改寫提交(通過修訂或重訂,例如)作爲LibGit2Sharp不公開的方式來訪問引用日誌(還)
  • 無法使用此提案檢索無法訪問(懸掛)提交。

測試庫:

被查詢的資源庫的內容是展示低於

$ git log --all --graph 
* commit 4c062a6361ae6959e06292c1fa5e2822d9c96345 
| Author: gor <[email protected]> 
| Date: Thu Apr 14 18:44:16 2011 +0300 
| 
|  directory was added 
| 
* commit be3563ae3f795b2b4353bcce3a527ad0a4f7f644 
|\ Merge: 9fd738e c47800c 
| | Author: Scott Chacon <[email protected]> 
| | Date: Tue May 25 11:58:27 2010 -0700 
| | 
| |  Merge branch 'br2' 
| | 
| | * commit e90810b8df3e80c413d903f631643c716887138d 
| | | Author: Vicent Marti <[email protected]> 
| | | Date: Thu Aug 5 18:42:20 2010 +0200 
| | | 
| | |  Test commit 2 
| | | 
| | * commit 6dcf9bf7541ee10456529833502442f385010c3d 
| | Author: Vicent Marti <[email protected]> 
| | Date: Thu Aug 5 18:41:33 2010 +0200 
| | 
| |  Test commit 1 
| | 
| | * commit a4a7dce85cf63874e984719f4fdd239f5145052f 
| | |\ Merge: c47800c 9fd738e 
| |//Author: Scott Chacon <[email protected]> 
| |/ Date: Tue May 25 12:00:23 2010 -0700 
| |/ 
|/|   Merge branch 'master' into br2 
| | 
* | commit 9fd738e8f7967c078dceed8190330fc8648ee56a 
| | Author: Scott Chacon <[email protected]> 
| | Date: Mon May 24 10:19:19 2010 -0700 
| | 
| |  a fourth commit 
| | 
* | commit 4a202b346bb0fb0db7eff3cffeb3c70babbd2045 
| | Author: Scott Chacon <[email protected]> 
| | Date: Mon May 24 10:19:04 2010 -0700 
| | 
| |  a third commit 
| | 
| * commit c47800c7266a2be04c571c04d5a6614691ea99bd 
|/ Author: Scott Chacon <[email protected]> 
| Date: Tue May 25 11:58:14 2010 -0700 
| 
|  branch commit one 
| 
* commit 5b5b025afb0b4c913b4c338a42934a3863bf3644 
| Author: Scott Chacon <[email protected]> 
| Date: Tue May 11 13:38:42 2010 -0700 
| 
|  another commit 
| 
* commit 8496071c1b46c854b31185ea97743be6a8774479 
    Author: Scott Chacon <[email protected]> 
    Date: Sat May 8 16:13:06 2010 -0700 

     testing 

* commit 41bc8c69075bbdb46c5c6f0566cc8cc5b46e8bd9 
| Author: Scott Chacon <[email protected]> 
| Date: Tue May 11 13:40:41 2010 -0700 
| 
|  packed commit two 
| 
* commit 5001298e0c09ad9c34e4249bc5801c75e9754fa5 
    Author: Scott Chacon <[email protected]> 
    Date: Tue May 11 13:40:23 2010 -0700 

     packed commit one 
相關問題