-1

Trying to create a new branch and push it to my remote repo using this code:

        var localBranch = repository.CreateBranch(environment);
        Commands.Checkout(repository, localBranch);
        var remote = repository.Network.Remotes.First();
        repository.Branches.Update(localBranch, b => b.Remote = remote.Name, b => b.UpstreamBranch = localBranch.CanonicalName);
        repository.Network.Push(localBranch, pushOptions);

Unfortunately I am getting the following exception:

   LibGit2Sharp.BareRepositoryException: local push doesn't (yet) support pushing to non-bare repos.
   at LibGit2Sharp.Core.Ensure.HandleError(Int32 result)
   at LibGit2Sharp.Core.Ensure.ZeroResult(Int32 result)
   at LibGit2Sharp.Core.Proxy.git_remote_push(RemoteHandle remote, IEnumerable`1 refSpecs, GitPushOptions opts)
   at LibGit2Sharp.Network.Push(Remote remote, IEnumerable`1 pushRefSpecs, PushOptions pushOptions)
   at LibGit2Sharp.Network.Push(Remote remote, String pushRefSpec, PushOptions pushOptions)
   at LibGit2Sharp.Network.Push(IEnumerable`1 branches, PushOptions pushOptions)
   at LibGit2Sharp.Network.Push(Branch branch, PushOptions pushOptions)

What am I missing? isn't it possible to push a new branch to a remote repository?

torek
  • 448,244
  • 59
  • 642
  • 775
amitc
  • 69
  • 3
  • 1
    `local push doesn't (yet) support pushing to non-bare repos.`, don't know what else you need to know, you're either using an outdated version or the library simply doesn't support pushing to a non-bare repo (yet) – MindSwipe Jan 13 '22 at 21:03
  • You have the options of fixing up libGit2Sharp to add this support, find someone else's version of libGit2Sharp that already has it, stop doing it entirely, or using command-line Git (which does support it). But that last one doesn't answer the question at all, which is why I snipped the [tag:git] tag. – torek Jan 14 '22 at 06:53
  • @MindSwipe answers is not correct, it looks like I solved my own issue by changing the code (will insert as in edit). My repository is still a non-bare repository, but now it works. – amitc Jan 14 '22 at 09:23

1 Answers1

1

So after changing my flow to the following it is now working an I am not getting the error that I got before.

    private static void CreateRemoteBranch(Repository repository, string environment, string master, PushOptions pushOptions)
    {
        Logger.Log($"Checking to see if a remote environment branch for {environment} exists");
        
        var masterBranch = repository.Branches.RemoteBranch(master);
        var remoteBranch = repository.Branches.RemoteBranch(environment);
        
        if (remoteBranch == null)
        {
            Logger.Log("Creating the new branch");
            var localBranch = repository.CreateBranch(environment, masterBranch.Tip);
            
            Logger.Log("Pushing it to remote");
            var remote = repository.Network.Remotes.First();
            repository.Branches.Update(localBranch, b => b.Remote = remote.Name, b => b.UpstreamBranch = localBranch.CanonicalName);
            repository.Network.Push(localBranch, pushOptions);
        }
        else
        {
            Logger.Log("Branch exists");
        }
    }


public static class BranchUtils
{
    public static Branch RemoteBranch(this BranchCollection branchCollection, string name)
    {
        return branchCollection[$"origin/{name}"];
    }

    public static Branch LocalBranch(this BranchCollection branchCollection, string name)
    {
        return branchCollection[name];
    }
}
amitc
  • 69
  • 3