找出一个提交属于LibGit2Sharp的分支? [英] Find out the branch a commit belongs to in LibGit2Sharp?

查看:172
本文介绍了找出一个提交属于LibGit2Sharp的分支?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在循环 LibGit2Sharp 中的提交:

  Repository repo =新版本库(Z:/ www / gg); 

foreach(LibGit2Sharp.Commit commit in repo.Commits)
{
...
}

我可以检索 Author Message 等属性,但我什么都看不到它属于哪个分支?理想情况下,我希望有一个指向分支对象的指针,但在这种情况下,即使名称也没有问题。



这是调试器显示的内容:

>



这是我正在寻找的:




TortoiseGit显示最相关分行名称的行为:





示例存储库: https://docs.google.com/open?id=0B-3-X85VysdNcmZIaGVTSDZSenVGbTJxYlI2SUlsZw

解决方案

目前没有内置方式来模仿 git branch --contains< ; commit>



Howev呃,你可以通过显式行走每个分支并比较每个弹出提交和搜索到的提交来解决这个限制。



以下测试演示了这个

  [Test] 
public void CanSearchBranchesContainingASpecificCommit()
{
using(var repo = new Repository(StandardTestRepoPath))
{
const string commitSha =5b5b025afb0b4c913b4c338a42934a3863bf3644;
IEnumerable<分支>分支= ListBranchesContaininingCommit(repo,commitSha);

branches.Count()。ShouldEqual(6);
}
}

私有IEnumerable<分支> ListBranchesContaininingCommit(Repository repo,字符串commitSha)
{
foreach(repo.Branches中的var分支)
{
var commit = repo.Commits.QueryBy(new CommitFilter {Since = branch })。其中(c => c.Sha == commitSha); $!
$ b if(!commits.Any())
{
continue;
}

产量回报分支;
}
}

注意:此代码已成功通过LibGit2Sharp的开发分支的当前技巧进行测试。



更新:



继评论中的讨论后,满足您的要求。

下面的代码将返回包含搜索提交的所有分支。如果提交恰好是至少一个分支的提示,那么这些分支将被返回。

  [Test] 
public void CanSearchBranchesContainingASpecificCommit()
{
using(var repo = new Repository(StandardTestRepoPath))
{
const string commitSha =5b5b025afb0b4c913b4c338a42934a3863bf3644;
IEnumerable<分支>分支= 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(仓库回购,字符串commitSha)
{
bool directBranchHasBeenFound = false;
foreach(repo.Branches中的var分支)
{
if(branch.Tip.Sha!= commitSha)
{
continue;
}

directBranchHasBeenFound = true;
收益回报分支;
}

if(directBranchHasBeenFound)
{
yield break;
}

foreach(repo.Branches中的var分支)
{
var commit = repo.Commits.QueryBy(new CommitFilter {Since = branch})。 (c => c.Sha == commitSha); $!
$ b if(!commits.Any())
{
continue;
}

产量回报分支;
}
}


I am looping through commits in LibGit2Sharp:

Repository repo = new Repository("Z:/www/gg");

foreach (LibGit2Sharp.Commit commit in repo.Commits)
{
    ...
}

I can retrieve properties like Author and Message, but I do not see anything about what branch it belongs to? Ideally I would like to have a pointer to the branch object, but even a name would be fine in this scenario.

This is what the debugger shows up:

This is what I am looking for:

TortoiseGit's behavior of showing the most relevant branch name:

Example repository: https://docs.google.com/open?id=0B-3-X85VysdNcmZIaGVTSDZSenVGbTJxYlI2SUlsZw

解决方案

There is currently no built-in way to mimic git branch --contains <commit>.

However, you might work around this limit by explicitly walking each branch and comparing each popped commit against the searched one.

Following test demonstrates this

[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;
    }
}

Note: This code has been successfully tested against the current tip of the development branch of LibGit2Sharp.

UPDATE:

Following the discussion in the comments, here's a little update which I hope will fulfill your request.

The code below will return all the branches containing the searched commit. If the commit happens to be the tip of at least one branch, those branches will be returned instead.

[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;
    }
}

这篇关于找出一个提交属于LibGit2Sharp的分支?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆