目前模仿git branch --contains <commit>
没有内置的方式。
但是,您可以通过明确地遍历每个分支并将每个弹出的提交与搜索的提交进行比较来解决此限制。
下面的测试演示了这种
[Test]
public void CanSearchBranchesContainingASpecificCommit()
{
using (var repo = new Repository(StandardTestRepoPath))
{
const string commitSha = "5b5b025afb0b4c913b4c338a42934a3863bf3644";
IEnumerable<Branch> branches = ListBranchesContaininingCommit(repo, commitSha);
branches.Count().ShouldEqual(6);
}
}
private IEnumerable<Branch> ListBranchesContaininingCommit(Repository repo, string commitSha)
{
foreach (var branch in repo.Branches)
{
var commits = repo.Commits.QueryBy(new CommitFilter { Since = branch }).Where(c => c.Sha == commitSha);
if (!commits.Any())
{
continue;
}
yield return branch;
}
}
注:该代码兑LibGit2Sharp的the current tip of the development branch试验成功。
UPDATE:
继在评论中讨论,这里有一个小更新,我希望将满足您的要求。
下面的代码将返回包含搜索提交的所有分支。如果提交恰好是至少一个分支的提示,那么这些分支将被返回。
[Test]
public void CanSearchBranchesContainingASpecificCommit()
{
using (var repo = new Repository(StandardTestRepoPath))
{
const string commitSha = "5b5b025afb0b4c913b4c338a42934a3863bf3644";
IEnumerable<Branch> branches = ListBranchesContaininingCommit(repo, commitSha);
branches.Count().ShouldEqual(6);
const string otherCommitSha = "4a202b346bb0fb0db7eff3cffeb3c70babbd2045";
branches = ListBranchesContaininingCommit(repo, otherCommitSha);
branches.Count().ShouldEqual(1); // origin/packed-test
}
}
private IEnumerable<Branch> ListBranchesContaininingCommit(Repository repo, string commitSha)
{
bool directBranchHasBeenFound = false;
foreach (var branch in repo.Branches)
{
if (branch.Tip.Sha != commitSha)
{
continue;
}
directBranchHasBeenFound = true;
yield return branch;
}
if (directBranchHasBeenFound)
{
yield break;
}
foreach (var branch in repo.Branches)
{
var commits = repo.Commits.QueryBy(new CommitFilter { Since = branch }).Where(c => c.Sha == commitSha);
if (!commits.Any())
{
continue;
}
yield return branch;
}
}
这工作正常,但还有一件事:我可以不知何故知道这些多个分支中的哪一个是它所属的“最近的一个”?即在我的情况下,如果它是一年前属于某个分支的,如果它在一周前刚刚合并为“master”,那就没什么意义了。我怀疑我只能依靠它成为第一个结果集... – Tower 2012-02-26 11:10:45
你能给我一些例子或等效的git命令吗?我很乐意回答你,但我不确定要清楚地了解“最新”分支是什么。 – nulltoken 2012-02-26 11:40:27
如果您从'master'分支并创建分支'foo'。如果你现在提交'foo',提交是2个分支的一部分('foo'和'master')。我想要的只是'foo',因为那是“最近的”。如果'foo'是一个孤儿分支('git checkout -b foo --orphan'),那么你的解决方案只会返回'foo',我可以使用它,但在很多情况下,提交是一个或多个分支的一部分,只对最近连接的那个感兴趣。 – Tower 2012-02-26 12:40:17