libgit2 / libgit2/libgit2sharp

Checkout branch checks out "(no branch)"

Open
#2,040 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C#
Stars
3.5k
Forks
925
PR merge metrics
No merged PRs in 30d

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the CommitChanges and GetBranch entry points shown in the report, then reproduce the checkout on the Ubuntu pipeline environment using LibGit2Sharp 0.27.2 and compare it with Windows 10. Trace the branch returned by GetBranch and the checkout result; done means the subsequent logs and commit remain on the requested branch rather than "(no branch)".

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp, git
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.