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 摘要。