Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Checkout branch checks out "(no branch)" #2040

Open
starteNCS opened this issue Jun 1, 2023 · 1 comment
Open

Checkout branch checks out "(no branch)" #2040

starteNCS opened this issue Jun 1, 2023 · 1 comment

Comments

@starteNCS
Copy link

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

@MarcelKonnegen
Copy link

MarcelKonnegen commented Aug 14, 2023

Hi,
we are having the exact same problem in our company. Is this a confirmed bug or an error in using the library?

UPDATE: This is how we solved the problem:

var existingRemoteBranch = Commands.Checkout(repository, existingRemoteBranchReference); var localBranch = repository.CreateBranch(localBranchName, existingRemoteBranch.Tip); repository.Branches.Update(localBranch, b => { b.Remote = "origin"; b.UpstreamBranch = localBranch.CanonicalName; } var branchToWorkWith = Commands.Checkout(repository, localBranch);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants