libgit2 / libgit2/libgit2sharp

Checkout branch checks out "(no branch)"

未关闭
#2,040 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

主要语言
C#
星标
3.5k
派生
925
PR 合并指标
30 天内没有已合并 PR

描述

I encountered a Bug where Checkout would not checkout the provided branch, but rather nothing (?). This somehow does not occur on my local dev-machine, but only on our pipeline agent.

Reproduction steps

This is my code to commit my changes

public Commit? CommitChanges(SemVersion newVersion)
{
    using var repo = new Repository(_executionSettings.Git.CloneTarget);
    _logger.LogInformation("{head}", repo.Head.FriendlyName);
    var status = repo.RetrieveStatus();
    if (!status.IsDirty)
    {
        _logger.LogInformation("No changes in repository, skipping commit");
        return null;
    }

    var targetBranch = LibGit2Sharp.Commands.Checkout(repo, GetBranch(repo, _executionSettings.Git.TargetBranch));
    _logger.LogInformation("{head}", repo.Head.FriendlyName);
    LibGit2Sharp.Commands.Stage(repo, new[]
    {
        _executionSettings.FileNames.Changelog,
        _executionSettings.FileNames.Version
    });

    var author = new Signature("Version Agent", "versionagent@example.com", DateTimeOffset.Now);
    var commitMessage = $"chore(release): Released {newVersion} to production";
    _logger.LogInformation("{head}", repo.Head.FriendlyName);
    var commit = repo.Commit(commitMessage, author, author);
    _logger.LogInformation("{head}", repo.Head.FriendlyName);
    
    _logger.LogInformation("Commited new changes");
    return commit;
}

And this loads the provided branch.

private Branch GetBranch(Repository repo, string branchName)
{
    var branch = repo.Branches.SingleOrDefault(branch => branch.FriendlyName == branchName);

    if (branch == null)
    {
        throw new ArgumentException($"Could not find branch '{branchName}'. Did you mean to use 'origin/{branchName}' instead?");
    }

    _logger.LogInformation("Found branch '{branchName}' with head at {headSha}", branch.CanonicalName, branch.Commits.First().Sha);
    return branch;
}
Expected behavior

I would except, that every logging statement would print "develop"

Actual behavior

Only the first logging prints "develop". The following only print "(no branch)"
image

Version of LibGit2Sharp (release number or SHA1)

0.27.2

Operating system(s) tested; .NET runtime tested

Local dev-machine: Windows 10
Pipeline: ubuntu

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

从报告中显示的 CommitChanges 和 GetBranch 入口点开始,然后使用 LibGit2Sharp 0.27.2 在 Ubuntu pipeline 环境中复现 checkout,并将其与 Windows 10 进行比较。跟踪 GetBranch 返回的分支和 checkout 结果;完成的标准是后续日志和 commit 仍位于请求的分支上,而不是 "(no branch)"。

由索引模型根据 Issue 内容生成。

评估

技术栈
csharp, git
领域
devtools
Issue 类型
缺陷
难度
4/5
预计耗时
3-5 天
活跃度
停滞
描述清晰度
基本清楚
新手友好度
35/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。