2013-10-09 75 views
2

如何在libgit2sharp中創建孤兒分支?libgit2sharp中的孤兒分支

我能找到的所有方法都是創建一個指向提交的分支。
我正在尋找類似命令的效果:

git checkout --orphan BRANCH_NAME 

回答

3

git checkout --orphan BRANCH_NAME實際上移動HEAD未出生的分支BRANCH_NAME不改變工作目錄,也沒有索引。

通過使用repo.Refs.UpdateTarget()方法更新HEAD參考的目標,您可以使用LibGit2Sharp執行類似的操作。

下面的測試演示了這種

[Fact] 
public void CanCreateAnUnbornBranch() 
{ 
    string path = CloneStandardTestRepo(); 
    using (var repo = new Repository(path)) 
    { 
     // No branch named orphan 
     Assert.Null(repo.Branches["orphan"]); 

     // HEAD doesn't point to an unborn branch 
     Assert.False(repo.Info.IsHeadUnborn); 

     // Let's move the HEAD to this branch to be created 
     repo.Refs.UpdateTarget("HEAD", "refs/heads/orphan"); 
     Assert.True(repo.Info.IsHeadUnborn); 

     // The branch still doesn't exist 
     Assert.Null(repo.Branches["orphan"]); 

     // Create a commit against HEAD 
     var signature = new Signature("Me", "[email protected]", DateTimeOffset.Now); 
     Commit c = repo.Commit("New initial root commit", signature, signature); 

     // Ensure this commit has no parent 
     Assert.Equal(0, c.Parents.Count()); 

     // The branch now exists... 
     Branch orphan = repo.Branches["orphan"]; 
     Assert.NotNull(orphan); 

     // ...and points to that newly created commit 
     Assert.Equal(c, orphan.Tip); 
    } 
}