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

LibGit2Sharp.NameConflictException: Failed to write reference 'refs/heads/feature/Morpheus_v12.2': a reference with that name already exists. #864

Closed
jwhite opened this issue May 17, 2016 · 32 comments · Fixed by #2261
Labels
Milestone

Comments

@jwhite
Copy link

jwhite commented May 17, 2016

Sorry, I'm just starting to look at this but I have an exception that occurs only on 1 branch that we have. The rest are working.

This is in version 3.5.3 building on a TFS vNext agent.

`ERROR [05/18/16 11:44:42:43] An unexpected error occurred:

2016-05-17T23:44:42.4352442Z LibGit2Sharp.NameConflictException: Failed to write reference 'refs/heads/feature/Morpheus_v12.2': a reference with that name already exists.
 2016-05-17T23:44:42.4352442Z    at LibGit2Sharp.Core.Ensure.HandleError(Int32 result)
 2016-05-17T23:44:42.4352442Z    at LibGit2Sharp.Core.Ensure.ZeroResult(Int32 result)
 2016-05-17T23:44:42.4352442Z    at LibGit2Sharp.Core.Proxy.git_reference_create(RepositoryHandle repo, String name, ObjectId targetId, Boolean allowOverwrite, String logMessage)
2016-05-17T23:44:42.4352442Z    at LibGit2Sharp.ReferenceCollection.Add(String name, ObjectId targetId, String logMessage, Boolean allowOverwrite)
2016-05-17T23:44:42.4352442Z    at LibGit2Sharp.ReferenceCollection.Add(String name, ObjectId targetId)
2016-05-17T23:44:42.4352442Z    at GitTools.Git.GitRepositoryHelper.EnsureLocalBranchExistsForCurrentBranch(Repository repo, String currentBranch)
2016-05-17T23:44:42.4352442Z    at GitTools.Git.GitRepositoryHelper.NormalizeGitDirectory(String gitDirectory, AuthenticationInfo authentication, Boolean noFetch, String currentBranch)
2016-05-17T23:44:42.4352442Z    at GitVersion.GitPreparer.Initialise(Boolean normaliseGitDirectory, String currentBranch)
2016-05-17T23:44:42.4352442Z    at GitVersion.ExecuteCore.ExecuteInternal(String targetBranch, String commitId, IRepository repo, GitPreparer gitPreparer, String projectRoot, IBuildServer buildServer, Config overrideConfig)
2016-05-17T23:44:42.4352442Z    at GitVersion.ExecuteCore.ExecuteGitVersion(String targetUrl, String dynamicRepositoryLocation, Authentication authentication, String targetBranch, Boolean noFetch, String workingDirectory, String commitId, Config overrideConfig)
2016-05-17T23:44:42.4352442Z    at GitVersion.SpecifiedArgumentRunner.Run(Arguments arguments, IFileSystem fileSystem)
2016-05-17T23:44:42.4352442Z    at GitVersion.Program.VerifyArgumentsAndRun()
2016-05-17T23:44:42.4352442Z INFO [05/18/16 11:44:42:43] 
2016-05-17T23:44:42.4352442Z INFO [05/18/16 11:44:42:43] Here is the current git graph (please include in issue): 
2016-05-17T23:44:42.4352442Z INFO [05/18/16 11:44:42:43] Showing max of 100 commits
2016-05-17T23:44:43.3258635Z INFO [05/18/16 11:44:43:32] *   e1e8217 47 minutes ago  (HEAD, origin/Feature/Morpheus_v12.2)
2016-05-17T23:44:43.3258635Z |\  
2016-05-17T23:44:43.3258635Z | | * 497957a 58 minutes ago  (origin/pull/US751)
2016-05-17T23:44:43.3414884Z | | *   daf77dc 2 hours ago 
2016-05-17T23:44:43.3414884Z | | |\  
2016-05-17T23:44:43.3414884Z | |_|/  
2016-05-17T23:44:43.3414884Z |/| |   
2016-05-17T23:44:43.3414884Z * | |   4d340a3 2 hours ago 
2016-05-17T23:44:43.3414884Z |\ \ \  
2016-05-17T23:44:43.3414884Z | * \ \   f5d110e 21 hours ago  (Feature/Morpheus_v12.2)
2016-05-17T23:44:43.3414884Z | |\ \ \  
2016-05-17T23:44:43.3414884Z | | * \ \   7c614e2 2 days ago  (origin/pull/US968, pull/US968)
2016-05-17T23:44:43.3414884Z | | |\ \ \  
2016-05-17T23:44:43.3414884Z | | * | | | 4b6cb49 2 days ago 
2016-05-17T23:44:43.3414884Z * | | | | | 8858504 2 hours ago 
2016-05-17T23:44:43.3414884Z |/ / / / /  
2016-05-17T23:44:43.3414884Z | | | | * 853b994 2 hours ago 
2016-05-17T23:44:43.3414884Z | | | | * 5245501 21 hours ago 
2016-05-17T23:44:43.3414884Z | |_|_|/  
2016-05-17T23:44:43.3414884Z |/| | |   
2016-05-17T23:44:43.3414884Z * | | |   0ca06fd 22 hours ago 
2016-05-17T23:44:43.3414884Z |\ \ \ \  
2016-05-17T23:44:43.3414884Z | |_|/ /  
2016-05-17T23:44:43.3414884Z |/| | |   
2016-05-17T23:44:43.3414884Z | | | | *   32923d9 3 hours ago  (origin/Feature/VRecalls)
2016-05-17T23:44:43.3414884Z | | | | |\  
2016-05-17T23:44:43.3414884Z | | | | |/  
2016-05-17T23:44:43.3571133Z | | | |/|   
2016-05-17T23:44:43.3571133Z | | | * |   7aa63d9 3 hours ago  (origin/master, master)
2016-05-17T23:44:43.3571133Z | | | |\ \  
2016-05-17T23:44:43.3571133Z | | | | * | 027a9e5 3 hours ago 
2016-05-17T23:44:43.3571133Z | | | |/ /  
2016-05-17T23:44:43.3571133Z | | | * |   9625f39 4 hours ago 
2016-05-17T23:44:43.3571133Z | | | |\ \  
2016-05-17T23:44:43.3571133Z | | | | * | 2da01c4 4 hours ago 
2016-05-17T23:44:43.3571133Z | | | * | |   3e808dc 19 hours ago 
2016-05-17T23:44:43.3571133Z | | | |\ \ \  
2016-05-17T23:44:43.3571133Z | | | | * | | a87dd32 20 hours ago 
2016-05-17T23:44:43.3571133Z | | | | * | |   279f6cf 20 hours ago 
2016-05-17T23:44:43.3571133Z | | | | |\ \ \  
2016-05-17T23:44:43.3571133Z | | | | | * | | 1eb4bd6 20 hours ago 
2016-05-17T23:44:43.3571133Z | | |_|_|/ / /  
2016-05-17T23:44:43.3571133Z | |/| | | | |   
2016-05-17T23:44:43.3571133Z | | | | * | | 4b846e7 20 hours ago 
2016-05-17T23:44:43.3571133Z | | |_|/ / /  
2016-05-17T23:44:43.3571133Z | |/| | | |   
2016-05-17T23:44:43.3571133Z | | | * | |   36918e5 20 hours ago 
2016-05-17T23:44:43.3571133Z | | | |\ \ \  
2016-05-17T23:44:43.3571133Z | | |/ / / /  
2016-05-17T23:44:43.3571133Z | |/| | | |   
2016-05-17T23:44:43.3571133Z | | | * | | 01e3c52 21 hours ago 
2016-05-17T23:44:43.3571133Z | | |/ / /  
2016-05-17T23:44:43.3571133Z | |/| | |   
2016-05-17T23:44:43.3571133Z | | | | *   9cc356b 21 hours ago  (Feature/VRecalls)
2016-05-17T23:44:43.3571133Z | | | | |\  
2016-05-17T23:44:43.3571133Z | | | | | * de88a2a 21 hours ago 
2016-05-17T23:44:43.3571133Z | | | | | *   4ad26d2 21 hours ago 
2016-05-17T23:44:43.3571133Z | | | | | |\  
2016-05-17T23:44:43.3571133Z | | | | | | *   dfb8e2a 22 hours ago 
2016-05-17T23:44:43.3571133Z | | | | | | |\  
2016-05-17T23:44:43.3571133Z | | |_|_|_|_|/  
2016-05-17T23:44:43.3571133Z | |/| | | | |   
2016-05-17T23:44:43.3571133Z | * | | | | |   726e6cb 24 hours ago 
2016-05-17T23:44:43.3571133Z | |\ \ \ \ \ \  
2016-05-17T23:44:43.3571133Z | | * | | | | | 5856f76 25 hours ago  (origin/pull/M49638, pull/M49638)
2016-05-17T23:44:43.3727382Z | | * | | | | | c9f186c 2 days ago 
2016-05-17T23:44:43.3727382Z | * | | | | | |   53a095b 24 hours ago 
2016-05-17T23:44:43.3727382Z | |\ \ \ \ \ \ \  
2016-05-17T23:44:43.3727382Z | | * | | | | | | a71dff4 2 days ago  (origin/pull/lightinstaller, pull/lightinstaller)
2016-05-17T23:44:43.3727382Z | | * | | | | | | 14b9c79 2 days ago 
2016-05-17T23:44:43.3727382Z | | * | | | | | | 11e2e91 2 days ago 
2016-05-17T23:44:43.3727382Z | | * | | | | | | 78c956a 2 days ago 
2016-05-17T23:44:43.3727382Z | | * | | | | | | cae8e9d 2 days ago 
2016-05-17T23:44:43.3727382Z | | * | | | | | | 7f08e37 2 days ago 
2016-05-17T23:44:43.3727382Z | | * | | | | | | 3c3d76a 2 days ago 
2016-05-17T23:44:43.3727382Z | | * | | | | | | a4ffa79 2 days ago 
2016-05-17T23:44:43.3727382Z | | * | | | | | | d6d5d4b 2 days ago 
2016-05-17T23:44:43.3727382Z | | * | | | | | | be6aef8 2 days ago 
2016-05-17T23:44:43.3727382Z | | * | | | | | | 3c16f81 5 days ago 
2016-05-17T23:44:43.3727382Z | | * | | | | | | c4a47f8 5 days ago 
2016-05-17T23:44:43.3727382Z | | * | | | | | | 0d1e995 5 days ago 
2016-05-17T23:44:43.3727382Z | | * | | | | | | bbcc78c 5 days ago 
2016-05-17T23:44:43.3727382Z | | * | | | | | | 53680e0 5 days ago 
2016-05-17T23:44:43.3727382Z | | * | | | | | |   e5cdff6 6 days ago 
2016-05-17T23:44:43.3727382Z | | |\ \ \ \ \ \ \  
2016-05-17T23:44:43.3727382Z | | | | |/ / / / /  
2016-05-17T23:44:43.3727382Z | | | |/| | | | |   
2016-05-17T23:44:43.3727382Z | | * | | | | | | 47c468d 6 days ago 
2016-05-17T23:44:43.3727382Z | * | | | | | | |   e5850f1 25 hours ago 
2016-05-17T23:44:43.3727382Z | |\ \ \ \ \ \ \ \  
2016-05-17T23:44:43.3727382Z | | |_|_|_|/ / / /  
2016-05-17T23:44:43.3727382Z | |/| | | | | | |   
2016-05-17T23:44:43.3727382Z | * | | | | | | |   c2eb9ac 26 hours ago 
2016-05-17T23:44:43.3727382Z | |\ \ \ \ \ \ \ \  
2016-05-17T23:44:43.3727382Z | | * \ \ \ \ \ \ \   628b35b 2 days ago  (origin/pull/M49556, pull/M49556)
2016-05-17T23:44:43.3727382Z | | |\ \ \ \ \ \ \ \  
2016-05-17T23:44:43.3727382Z | | |/ / / / / / / /  
2016-05-17T23:44:43.3727382Z | |/| | | | | | | |   
2016-05-17T23:44:43.3727382Z | | * | | | | | | | df15870 6 weeks ago  (origin/M49556, M49556)
2016-05-17T23:44:43.3727382Z | | | | | | | | * |   892a7c0 23 hours ago 
2016-05-17T23:44:43.3727382Z | | | | | | | | |\ \  
2016-05-17T23:44:43.3727382Z | | | | | | | | | |/  
2016-05-17T23:44:43.3727382Z | | | | | | | | * |   a119604 25 hours ago 
2016-05-17T23:44:43.3883631Z | | | | | | | | |\ \  
2016-05-17T23:44:43.3883631Z | | | | | | | | * | | 392e639 25 hours ago 
2016-05-17T23:44:43.3883631Z | | | | | | | * | | | 602ab82 21 hours ago 
2016-05-17T23:44:43.3883631Z | | | | | | | | |_|/  
2016-05-17T23:44:43.3883631Z | | | | | | | |/| |   
2016-05-17T23:44:43.3883631Z | | | | | | | * | |   d31cda9 24 hours ago 
2016-05-17T23:44:43.3883631Z | | | | | | | |\ \ \  
2016-05-17T23:44:43.3883631Z | | | | | | | | * \ \   6565e63 24 hours ago 
2016-05-17T23:44:43.3883631Z | | | | | | | | |\ \ \  
2016-05-17T23:44:43.3883631Z | | | | | | | | | * \ \   b6e60fc 2 days ago 
2016-05-17T23:44:43.3883631Z | | | | | | | | | |\ \ \  
2016-05-17T23:44:43.3883631Z | | |_|_|_|_|_|_|_|/ / /  
2016-05-17T23:44:43.3883631Z | |/| | | | | | | | | |   
2016-05-17T23:44:43.3883631Z | * | | | | | | | | | |   04518f5 2 days ago  (origin/Feature/MedicalHistoryFix, origin/Feature/M47228-DAFAlerts, Feature/MedicalHistoryFix, Feature/M47228-DAFAlerts)
2016-05-17T23:44:43.3883631Z | |\ \ \ \ \ \ \ \ \ \ \  
2016-05-17T23:44:43.3883631Z | * \ \ \ \ \ \ \ \ \ \ \   24a1fc5 2 days ago 
2016-05-17T23:44:43.3883631Z | |\ \ \ \ \ \ \ \ \ \ \ \  
2016-05-17T23:44:43.3883631Z | | |_|_|_|_|_|/ / / / / /  
2016-05-17T23:44:43.3883631Z | |/| | | | | | | | | | |   
2016-05-17T23:44:43.3883631Z | | | | | | | | | | * | | 1d0ce6c 2 days ago 
2016-05-17T23:44:43.3883631Z | | |_|_|_|_|_|_|_|/ / /  
2016-05-17T23:44:43.3883631Z | |/| | | | | | | | | |   
2016-05-17T23:44:43.3883631Z | | | | | | | | * | | | 9cdedf7 24 hours ago 
2016-05-17T23:44:43.3883631Z | | | | | | | | |/ / /  
2016-05-17T23:44:43.3883631Z | | | | | | | | * | | 9a185e6 24 hours ago 
2016-05-17T23:44:43.3883631Z | | | | | | | | | |/  
2016-05-17T23:44:43.3883631Z | | | | | | | | |/|   
2016-05-17T23:44:43.3883631Z | | | | | | | | * | 874d120 25 hours ago 
2016-05-17T23:44:43.3883631Z | | | | | | | | * | 4d72ce0 2 days ago 
2016-05-17T23:44:43.3883631Z | | | | | | | | |/  
2016-05-17T23:44:43.3883631Z | | | | | | | | * 4f445a3 2 days ago 
2016-05-17T23:44:43.3883631Z | | |_|_|_|_|_|/  
2016-05-17T23:44:43.3883631Z | |/| | | | | |   
2016-05-17T23:44:43.3883631Z | | | | | | | | * 2289d18 20 hours ago  (origin/pull/TFS-949, pull/TFS-949)
2016-05-17T23:44:43.3883631Z | |_|_|_|_|_|_|/  
2016-05-17T23:44:43.3883631Z |/| | | | | | |   
2016-05-17T23:44:43.3883631Z * | | | | | | |   94f3b17 2 days ago 
2016-05-17T23:44:43.4039880Z |\ \ \ \ \ \ \ \  
2016-05-17T23:44:43.4039880Z | * | | | | | | | 2412edc 5 days ago  (origin/pull/US962, pull/US962)
2016-05-17T23:44:43.4039880Z | | |_|_|_|_|_|/  
2016-05-17T23:44:43.4039880Z | |/| | | | | |   
2016-05-17T23:44:43.4039880Z * | | | | | | |   48ae8c6 2 days ago 
2016-05-17T23:44:43.4039880Z |\ \ \ \ \ \ \ \  
2016-05-17T23:44:43.4039880Z | |/ / / / / / /  
2016-05-17T23:44:43.4039880Z |/| | | | | | |   

2016-05-17T23:44:43.4039880Z | * | | | | | | fdc04f5 2 days ago  (origin/pull/US966, pull/US966)
 <snip>
@jwhite
Copy link
Author

jwhite commented May 18, 2016

This occurs sporadically on build agents using build vNext. Logging into to the agent locally and running c:\tasks\GitVersion\3.5.3\GitVersion.exe /output buildserver /nofetch (the same command as reported in the log) has some strange behavior.

It takes a very long time and then eventually succeeds.

Could this have anything to do with the %tmp% file?

@JakeGinnivan
Copy link
Contributor

From the looks of it its a case sensitivity issue.

refs/heads/feature/Morpheus_v12.2 vs (Feature/Morpheus_v12.2)

That would be my first guess. Possibly https://github.com/GitTools/GitTools.Core/blob/master/src/GitTools.Core/GitTools.Core.Shared/Git/Helpers/GitRepositoryHelper.cs#L120 needs to be a case insensitive check.

@GitHubSachin
Copy link

What is the resolution here? I am seeing same error on one of my branch

@drub0y
Copy link

drub0y commented Jun 5, 2017

So we just ran into this as well and it's because we had some history where we had someone use a capital F for a feature branch (i.e. Feature/whatever) in the past so when it goes through history and the first Feature/... it comes across is upper, but newer branches are lowercase (i.e. feature/...) it has already established the Feature/ virtual folder (if you will) as upper case. Obviously some of the code is treating things as case-insensitive, while other code is treating it as case-sensitive (i.e. _Equals_ vs _Equals(..., StringComparisonOptions.OridinalIgnoreCase)) and so you end up with this disagreement in the two different parts of the code.

Judging by the stack trace, it looks to me like this is a bug in Lib2GitSharp though, not GitVersion, right?

@rsc-bot
Copy link

rsc-bot commented Jun 5, 2017

I am receiving this error with TeamCity now.

@JackGrinningCat
Copy link

I got the same.
This was the first google hit and the comments are fitting with my history.

I made a big Story/... instead of story/...
Deleted the old Story/... and recreated the branch as story/...

The error occurred after an PullRequest in TFS. The build failed at git version. My error looks the same as above, without the git log, that's failing too, because the team don't have git in the path 😅 .

I'll recheck on that with our dev-team. I requested a Build again, which successfully handled this case.
May be there is an old build folder from my first story trial, that is related to the first story.

@drub0y Maybe it is a problem from Lib2GitSharp. But maybe it is the use or a missing parameters of it.

@FrankPeach
Copy link

I'm having the problem too. Any solution yet?

@Ivan-L
Copy link

Ivan-L commented Jan 23, 2018

Ditto, we're also hitting the same issue - someone created a Feature/ branch in our repo and now subsequent builds from feature/ branches are failing. Is there any way around this?

@jrestall
Copy link

jrestall commented Jun 12, 2018

+1

As mentioned above the issue seems to be with the code at
https://github.com/GitTools/GitTools.Core/blob/master/src/GitTools.Core/Git/Helpers/GitRepositoryHelper.cs#L155

2018-06-08T01:23:04.6228083Z ERROR [06/08/18 13:23:04:62] An unexpected error occurred:
2018-06-08T01:23:04.6228083Z LibGit2Sharp.NameConflictException: Failed to write reference 'refs/heads/feature/Config/Add_Config': a reference with that name already exists.
2018-06-08T01:23:04.6228083Z at LibGit2Sharp.Core.Ensure.HandleError(Int32 result)
2018-06-08T01:23:04.6228083Z at LibGit2Sharp.Core.Ensure.ZeroResult(Int32 result)
2018-06-08T01:23:04.6228083Z at LibGit2Sharp.Core.Proxy.git_reference_create(RepositoryHandle repo, String name, ObjectId targetId, Boolean allowOverwrite, String logMessage)
2018-06-08T01:23:04.6228083Z at LibGit2Sharp.ReferenceCollection.Add(String name, ObjectId targetId, String logMessage, Boolean allowOverwrite)
2018-06-08T01:23:04.6228083Z at LibGit2Sharp.ReferenceCollection.Add(String name, ObjectId targetId)
2018-06-08T01:23:04.6228083Z at GitTools.Git.GitRepositoryHelper.EnsureLocalBranchExistsForCurrentBranch(Repository repo, String currentBranch)
2018-06-08T01:23:04.6228083Z at GitTools.Git.GitRepositoryHelper.NormalizeGitDirectory(String gitDirectory, AuthenticationInfo authentication, Boolean noFetch, String currentBranch)
2018-06-08T01:23:04.6228083Z at GitVersion.GitPreparer.Initialise(Boolean normaliseGitDirectory, String currentBranch)
2018-06-08T01:23:04.6228083Z at GitVersion.ExecuteCore.ExecuteInternal(String targetBranch, String commitId, IRepository repo, GitPreparer gitPreparer, String projectRoot, IBuildServer buildServer, Config overrideConfig)
2018-06-08T01:23:04.6228083Z at GitVersion.ExecuteCore.ExecuteGitVersion(String targetUrl, String dynamicRepositoryLocation, Authentication authentication, String targetBranch, Boolean noFetch, String workingDirectory, String commitId, Config overrideConfig)
2018-06-08T01:23:04.6228083Z at GitVersion.SpecifiedArgumentRunner.Run(Arguments arguments, IFileSystem fileSystem)
2018-06-08T01:23:04.6228083Z at GitVersion.Program.VerifyArgumentsAndRun()

@asbjornu
Copy link
Member

As GitVersion stopped using GitTools.Core in #1581 and after upgrading to LibGit2Sharp 0.26 in #1713, I'm not sure this problem still exists. Can you please try the latest build of GitVersion and report back whether it fixes this problem or not? Please reopen this issue if the problem persists.

@zapr-16
Copy link

zapr-16 commented Oct 1, 2019

Hi @asbjornu , it's still happening with the latest version 5.0.1. Should this issue be reopened or should I open another issue?

In my case I have two branches that cause the conflict:

  • hotfix/BRANCH1
  • reflix/BRANCH1 (notice this one is a typo for relfix)

Error below:

An unexpected error occurred:
LibGit2Sharp.NameConflictException: Failed to write reference 'refs/heads/hotfix/DEVOPS-819': a reference with that name already exists.
at LibGit2Sharp.Core.Ensure.HandleError(Int32 result)
at LibGit2Sharp.Core.Proxy.git_reference_create(RepositoryHandle repo, String name, ObjectId targetId, Boolean allowOverwrite, String logMessage)
at LibGit2Sharp.ReferenceCollection.Add(String name, ObjectId targetId, String logMessage, Boolean allowOverwrite)
at LibGit2Sharp.ReferenceCollection.Add(String name, ObjectId targetId)
at GitTools.Git.GitRepositoryHelper.EnsureLocalBranchExistsForCurrentBranch(Repository repo, String currentBranch)
at GitTools.Git.GitRepositoryHelper.NormalizeGitDirectory(String gitDirectory, AuthenticationInfo authentication, Boolean noFetch, String currentBranch)
at GitVersion.GitPreparer.Initialise(Boolean normaliseGitDirectory, String currentBranch)
at GitVersion.ExecuteCore.ExecuteGitVersion(String targetUrl, String dynamicRepositoryLocation, Authentication authentication, String targetBranch, Boolean noFetch, String workingDirectory, String commitId, Config overrideConfig)
at GitVersion.SpecifiedArgumentRunner.Run(Arguments arguments, IFileSystem fileSystem)
at GitVersion.Program.VerifyArgumentsAndRun()

@asbjornu
Copy link
Member

asbjornu commented Oct 1, 2019

@zapr-16, are you able to reproduce this problem in a RepositoryFixture test akin to the following?

public void CanTakeVersionFromHotfixesBranch()
{
using (var fixture = new BaseGitFlowRepositoryFixture(r =>
{
r.MakeATaggedCommit("1.0.0");
r.MakeATaggedCommit("1.1.0");
r.MakeATaggedCommit("2.0.0");
}))
{
// Merge hotfix branch to support
Commands.Checkout(fixture.Repository, "master");
Commands.Checkout(fixture.Repository, fixture.Repository.CreateBranch("support-1.1", (Commit)fixture.Repository.Tags.Single(t => t.FriendlyName == "1.1.0").Target));
fixture.AssertFullSemver("1.1.0");
// create hotfix branch
Commands.Checkout(fixture.Repository, fixture.Repository.CreateBranch("hotfixes/1.1.1"));
fixture.AssertFullSemver("1.1.0"); // We are still on a tagged commit
fixture.Repository.MakeACommit();
fixture.AssertFullSemver("1.1.1-beta.1+1");
fixture.Repository.MakeACommit();
fixture.AssertFullSemver("1.1.1-beta.1+2");
}
}

@thoemmi
Copy link
Contributor

thoemmi commented Jan 31, 2020

It's still happening with 5.1.2 😞

@asbjornu
Copy link
Member

@thoemmi, are you able to reproduce the problem in a RepositoryFixture as per #864 (comment)?

@thoemmi
Copy link
Contributor

thoemmi commented Jan 31, 2020

Unfortunately, it does not occur locally but on the Azure DevOps build server.

@thoemmi
Copy link
Contributor

thoemmi commented Jan 31, 2020

Actually, it happens in DevOps Pull Requests only. If I queue an build of the branch explicitly, it succeeds. I guess it's how Azure DevOps handles the implicit PR branches.

@thoemmi
Copy link
Contributor

thoemmi commented Feb 25, 2020

This issue still occurs every now and then in our Pull Request builds on Azure DevOps. Unfortunately, I am not able to reproduce it locally. (BTW, GitVersionTask is 5.1.2)

@asbjornu Since this issue is closed already, should I open a new issue, or can you re-open this one?

@asbjornu asbjornu added the bug label Feb 25, 2020
@asbjornu asbjornu reopened this Feb 25, 2020
@eriksteinebach
Copy link

eriksteinebach commented Apr 13, 2020

I am having this on our master branch in one of our projects. We have 5 other Azure DevOps pipeline builds running fine with the "GitVersionTask" referenced in multiple projects.

But this 6th build is giving an error:
##[error]C:\Users\VssAdministrator\.nuget\packages\gitversiontask\5.2.4\build\GitVersionTask.targets(10,9): Error : NameConflictException: failed to write reference 'refs/heads/master': a reference with that name already exists.

This branch obviously always has been called "master", so it is not a casing issue in our case.
We have PR triggered builds turned off. We trigger builds on every checkin. So in our case it is not on PR builds.
I also cannot reproduce it locally

Our VS solution has 3 projects with "GitVersionTask" installed. When I remove the "GitVersionTask" from 1 project (an leave it in the other 2). The build succeeds. So the task works fine in the other 2 projects. If I then add the "GitVersionTask" back to the 3th project, the build still succeeds. But when I then make any other change/checkin, the build fails again with the same error message.

Does this make any sense to anyone?

@eriksteinebach
Copy link

I see here that some more information is being logged:
https://github.com/GitTools/GitTools.Core/blob/master/src/GitTools.Core/Git/Helpers/GitRepositoryHelper.cs#L155

Is there a way to turn this logging on, on the GitVersionTask?

@asbjornu
Copy link
Member

asbjornu commented May 7, 2020

LibGit2Sharp was upgraded to version 0.27.0-preview-0034 in 438d450 and released as a part of version 5.3.2 of GitVersion. Can you please perform an upgrade and see whether the problem still exists there?

Regarding logging in the task, @eriksteinebach, I believe that is something you need to adjust with MSBuild on the command line when you build the solution/project.

@martinmine
Copy link

Still failing with version 5.3.2:

[Step 1/13] LibGit2Sharp.NameConflictException: failed to write reference 'refs/heads/feature/ESF-875-branch-cleanup': a reference with that name already exists.
[Step 1/13]    at LibGit2Sharp.Core.Ensure.HandleError(Int32 result)
[Step 1/13]    at LibGit2Sharp.Core.Ensure.ZeroResult(Int32 result)
[Step 1/13]    at LibGit2Sharp.Core.Proxy.git_reference_create(RepositoryHandle repo, String name, ObjectId targetId, Boolean allowOverwrite, String logMessage)
[Step 1/13]    at LibGit2Sharp.ReferenceCollection.Add(String name, ObjectId targetId, String logMessage, Boolean allowOverwrite)[09:25:23]
[Step 1/13]    at LibGit2Sharp.ReferenceCollection.Add(String name, ObjectId targetId)
[Step 1/13]    at GitVersion.Extensions.RepositoryExtensions.EnsureLocalBranchExistsForCurrentBranch(IRepository repo, ILog log, Remote remote, String currentBranch)
[Step 1/13]    at GitVersion.GitPreparer.NormalizeGitDirectory(String gitDirectory, Boolean noFetch, String currentBranch, Boolean isDynamicRepository)
[Step 1/13]    at GitVersion.GitPreparer.NormalizeGitDirectory(String targetBranch, String gitDirectory, Boolean isDynamicRepository)
[Step 1/13]    at GitVersion.GitPreparer.PrepareInternal(Boolean normalizeGitDirectory, String currentBranch, Boolean shouldCleanUpRemotes)
[Step 1/13]    at GitVersion.GitPreparer.Prepare()
[Step 1/13]    at GitVersion.GitVersionTool.CalculateVersionVariables()
[Step 1/13]    at GitVersion.GitVersionExecutor.RunGitVersionTool(GitVersionOptions gitVersionOptions)

@asbjornu
Copy link
Member

asbjornu commented May 7, 2020

Ok, so these are most likely the problematic lines:

if (repo.Branches.All(b => b.CanonicalName != localCanonicalName))
{
log.Info(isBranch ? $"Creating local branch {localCanonicalName}"
: $"Creating local branch {localCanonicalName} pointing at {repoTipId}");
repo.Refs.Add(localCanonicalName, repoTipId);
}

So I suppose CanonicalName and localCanonicalName must differ, somehow. If someone could dig up the logs for when this is failing, it would be interesting to see what the log.Info() statement on line 59 writes to the log.

@martinmine
Copy link

After hours of debugging and scratching my head, I managed to solve this problem for my case. I noticed that the exception was thrown on one build node, and others had no problems running GitVersion. Running it manually also worked fine. After excluding my problematic build node and having merged a change to master, I got a new error:

[Updating sources] Failed to perform checkout on agent: '"C:\Program Files\Git\bin\git.exe" update-ref --stdin -z' command failed.
exit code: 128
stderr: fatal: cannot lock ref 'refs/heads/featurE/feat-test': Unable to create '<path to build node area>/system/git/git-<somehash>.git/refs/heads/featurE/feat-test.lock': File exists.

This made me realize I had a typo in the name of my feature branch (note the capitalized E). After some testing, it seems I am able to reproduce this bug with the following steps (it is important that everything is built on the same build node):

  1. Create a branch branch with name featurE/feat-1, add a commit, push, and let the build server build this, this will go fine.
  2. Make the build server build my master branch. This will also go fine.
  3. Create a new feature branch with name feature/feat-2, add a commit, and push it. GitVersion will then crash with the NameConflictException error above.

I managed to get out of this problem by removing all the feature branches remotely and deleting the git data cache (my case this was in <path to build node area>\system\git\git-<somehash>.git). My CI server would then be able to build feature branches again. Since I am running on Windows, I get the feeling this problem can be related to how Windows is non-case-sensitive and some parts of git is not handling this well. It could also be related to how my CI server interacts with git, since the featurE typo somehow spread onto the name of other feature branches that it pulled remotely.

@asbjornu
Copy link
Member

asbjornu commented May 7, 2020

Great discovery, @martinmine. Case sensitivity explains the problem quite well. We need to fix this in GitVersion so branches are always compared case insensitively.

@eriksteinebach
Copy link

Interesting thing is that my problem went away by switching from using the GitVersionTask nuget package in the projects to using GitVersionTask (installed through the marketplace) as a step in my build and using the generated version number during the build of the project.

Not sure why, maybe the problematic code is limited to the nuget package or maybe I started using a new build agent. But the task has worked very stable for me where I had multiple problems with the nuget package. And the task gives me better insight in the version being used, so I really like the Task approach better (for anyone else running into this issue).

asbjornu added a commit to asbjornu/GitVersion that referenced this issue May 11, 2020
asbjornu added a commit to asbjornu/GitVersion that referenced this issue May 11, 2020
@asbjornu asbjornu added this to the 5.3.x milestone May 11, 2020
@arturcic arturcic modified the milestones: 5.3.x, 5.3.3 May 12, 2020
@github-actions
Copy link

🎉 This issue has been resolved in version 5.3.3 🎉
The release is available on:

Your GitReleaseManager bot 📦🚀

@martinmine
Copy link

I can confirm that the 5.3.3 solved the problem for my case ❤️

@asbjornu
Copy link
Member

Thanks for the confirmation, @martinmine! 🙏

@mcascone
Copy link

I am sorry to report that I am seeing this same issue in 5.6.11+Branch.main.Sha.1680a89918cb1b3b15cd37ccf78079e1fc3bfdff

ERROR [06/28/22 21:48:26:15] An unexpected error occurred:
 LibGit2Sharp.NameConflictException: failed to write reference 'refs/heads/Develop': a reference with that name already exists.
    at LibGit2Sharp.Core.Ensure.HandleError(Int32 result)
    at LibGit2Sharp.Core.Ensure.ZeroResult(Int32 result)
    at LibGit2Sharp.Core.Proxy.git_reference_create(RepositoryHandle repo, String name, ObjectId targetId, Boolean allowOverwrite, String logMessage)
    at LibGit2Sharp.ReferenceCollection.Add(String name, ObjectId targetId, String logMessage, Boolean allowOverwrite)
    at LibGit2Sharp.ReferenceCollection.Add(String name, String canonicalRefNameOrObjectish, String logMessage, Boolean allowOverwrite)
    at LibGit2Sharp.ReferenceCollection.Add(String name, String canonicalRefNameOrObjectish, Boolean allowOverwrite)
    at GitVersion.ReferenceCollection.Add(String name, String canonicalRefNameOrObjectish, Boolean allowOverwrite)
    at GitVersion.GitPreparer.EnsureLocalBranchExistsForCurrentBranch(IRemote remote, String currentBranch)
    at GitVersion.GitPreparer.NormalizeGitDirectory(Boolean noFetch, String currentBranchName, Boolean isDynamicRepository)
    at GitVersion.GitPreparer.NormalizeGitDirectory(String targetBranch, Boolean isDynamicRepository)
    at GitVersion.GitPreparer.PrepareInternal(Boolean normalizeGitDirectory, String currentBranch, Boolean shouldCleanUpRemotes)
    at GitVersion.GitPreparer.Prepare()
    at GitVersion.GitVersionCalculateTool.CalculateVersionVariables()
    at GitVersion.GitVersionExecutor.RunGitVersionTool(GitVersionOptions gitVersionOptions)

After upgrading to the latest as of right now:

gitversion /version
5.10.3+Branch.support-5.x.Sha.bc9c9d003e655385e3dd1ba3bd013e04062d2f9b

I get the same failure, with more detailed stacktrace:

INFO [06/28/22 22:03:02:82] End: Normalizing git directory for branch 'Develop' (Took: 448.25ms)
   ERROR [06/28/22 22:03:02:84] An unexpected error occurred:
 LibGit2Sharp.NameConflictException: failed to write reference 'refs/heads/Develop': a reference with that name already exists.
    at LibGit2Sharp.Core.Ensure.HandleError(Int32 result) in /_/LibGit2Sharp/Core/Ensure.cs:line 154
    at LibGit2Sharp.Core.Ensure.ZeroResult(Int32 result) in /_/LibGit2Sharp/Core/Ensure.cs:line 172
    at LibGit2Sharp.Core.Proxy.git_reference_create(RepositoryHandle repo, String name, ObjectId targetId, Boolean allowOverwrite, String logMessage) in /_/LibGit2Sharp/Core/Proxy.cs:line 1889
    at LibGit2Sharp.ReferenceCollection.Add(String name, ObjectId targetId, String logMessage, Boolean allowOverwrite) in /_/LibGit2Sharp/ReferenceCollection.cs:line 194
    at LibGit2Sharp.ReferenceCollection.Add(String name, String canonicalRefNameOrObjectish, String logMessage, Boolean allowOverwrite) in /_/LibGit2Sharp/ReferenceCollection.cs:line 143
    at LibGit2Sharp.ReferenceCollection.Add(String name, String canonicalRefNameOrObjectish, Boolean allowOverwrite) in /_/LibGit2Sharp/ReferenceCollection.cs:line 167
    at GitVersion.ReferenceCollection.Add(String name, String canonicalRefNameOrObject, Boolean allowOverwrite) in D:\a\GitVersion\GitVersion\src\GitVersion.LibGit2Sharp\Git\ReferenceCollection.cs:line 14
    at GitVersion.GitPreparer.EnsureLocalBranchExistsForCurrentBranch(IRemote remote, String currentBranch) in D:\a\GitVersion\GitVersion\src\GitVersion.Core\Core\GitPreparer.cs:line 391
    at GitVersion.GitPreparer.NormalizeGitDirectory(Boolean noFetch, String currentBranchName, Boolean isDynamicRepository) in D:\a\GitVersion\GitVersion\src\GitVersion.Core\Core\GitPreparer.cs:line 164
    at GitVersion.GitPreparer.NormalizeGitDirectory(String targetBranch, Boolean isDynamicRepository) in D:\a\GitVersion\GitVersion\src\GitVersion.Core\Core\GitPreparer.cs:line 134
    at GitVersion.GitPreparer.PrepareInternal(GitVersionOptions gitVersionOptions) in D:\a\GitVersion\GitVersion\src\GitVersion.Core\Core\GitPreparer.cs:line 69
    at GitVersion.GitPreparer.Prepare() in D:\a\GitVersion\GitVersion\src\GitVersion.Core\Core\GitPreparer.cs:line 48
    at GitVersion.GitVersionCalculateTool.CalculateVersionVariables() in D:\a\GitVersion\GitVersion\src\GitVersion.Core\Core\GitVersionCalculateTool.cs:line 43
    at GitVersion.GitVersionExecutor.RunGitVersionTool(GitVersionOptions gitVersionOptions) in D:\a\GitVersion\GitVersion\src\GitVersion.App\GitVersionExecutor.cs:line 66
   INFO [06/28/22 22:03:02:84] Attempting to show the current git graph (please include in issue): 
   INFO [06/28/22 22:03:02:84] Showing max of 100 commits
   INFO [06/28/22 22:03:02:92] *   3c7d465 5 hours ago  (HEAD, origin/Develop, master, develop)

and you can see right there, the case difference of develop and Develop.

@asbjornu
Copy link
Member

GitVersion can't help with filesystem case sensitivity, @mcascone. You need to ensure that you name your branches consistently, everywhere. Preferably in all-lowercase.

@mujdatdinc
Copy link

FYI. Same problem here. No capital letters in branch name but I still see the error

GitVersion.Tool 5.6.10 x64

LibGit2Sharp.NameConflictException: failed to write reference 'refs/heads/pbi/scheduler': a reference with that name already exists. at LibGit2Sharp.Core.Ensure.HandleError(Int32 result) at LibGit2Sharp.Core.Proxy.git_reference_create(RepositoryHandle repo, String name, ObjectId targetId, Boolean allowOverwrite, String logMessage) at LibGit2Sharp.ReferenceCollection.Add(String name, ObjectId targetId, String logMessage, Boolean allowOverwrite) at LibGit2Sharp.ReferenceCollection.Add(String name, String canonicalRefNameOrObjectish, String logMessage, Boolean allowOverwrite) at LibGit2Sharp.ReferenceCollection.Add(String name, String canonicalRefNameOrObjectish, Boolean allowOverwrite) at GitVersion.ReferenceCollection.Add(String name, String canonicalRefNameOrObjectish, Boolean allowOverwrite) in D:\a\GitVersion\GitVersion\src\GitVersion.LibGit2Sharp\Git\ReferenceCollection.cs:line 20 at GitVersion.GitPreparer.EnsureLocalBranchExistsForCurrentBranch(IRemote remote, String currentBranch) in D:\a\GitVersion\GitVersion\src\GitVersion.Core\Core\GitPreparer.cs:line 382 at GitVersion.GitPreparer.NormalizeGitDirectory(Boolean noFetch, String currentBranchName, Boolean isDynamicRepository) in D:\a\GitVersion\GitVersion\src\GitVersion.Core\Core\GitPreparer.cs:line 189 at GitVersion.GitPreparer.NormalizeGitDirectory(String targetBranch, Boolean isDynamicRepository) in D:\a\GitVersion\GitVersion\src\GitVersion.Core\Core\GitPreparer.cs:line 147 at GitVersion.GitPreparer.PrepareInternal(Boolean normalizeGitDirectory, String currentBranch, Boolean shouldCleanUpRemotes) in D:\a\GitVersion\GitVersion\src\GitVersion.Core\Core\GitPreparer.cs:line 79 at GitVersion.GitPreparer.Prepare() in D:\a\GitVersion\GitVersion\src\GitVersion.Core\Core\GitPreparer.cs:line 60 at GitVersion.GitVersionCalculateTool.CalculateVersionVariables() in D:\a\GitVersion\GitVersion\src\GitVersion.Core\Core\GitVersionCalculateTool.cs:line 45 at GitVersion.GitVersionExecutor.RunGitVersionTool(GitVersionOptions gitVersionOptions) in D:\a\GitVersion\GitVersion\src\GitVersion.App\GitVersionExecutor.cs:line 70 at GitVersion.GitPreparer.EnsureLocalBranchExistsForCurrentBranch(IRemote remote, String currentBranch) in D:\a\GitVersion\GitVersion\src\GitVersion.Core\Core\GitPreparer.cs:line 382 at GitVersion.GitPreparer.NormalizeGitDirectory(Boolean noFetch, String currentBranchName, Boolean isDynamicRepository) in D:\a\GitVersion\GitVersion\src\GitVersion.Core\Core\GitPreparer.cs:line 189 at GitVersion.GitPreparer.NormalizeGitDirectory(String targetBranch, Boolean isDynamicRepository) in D:\a\GitVersion\GitVersion\src\GitVersion.Core\Core\GitPreparer.cs:line 147 at GitVersion.GitPreparer.PrepareInternal(Boolean normalizeGitDirectory, String currentBranch, Boolean shouldCleanUpRemotes) in D:\a\GitVersion\GitVersion\src\GitVersion.Core\Core\GitPreparer.cs:line 79 at GitVersion.GitPreparer.Prepare() in D:\a\GitVersion\GitVersion\src\GitVersion.Core\Core\GitPreparer.cs:line 60 at GitVersion.GitVersionCalculateTool.CalculateVersionVariables() in D:\a\GitVersion\GitVersion\src\GitVersion.Core\Core\GitVersionCalculateTool.cs:line 45 at GitVersion.GitVersionExecutor.RunGitVersionTool(GitVersionOptions gitVersionOptions) in D:\a\GitVersion\GitVersion\src\GitVersion.App\GitVersionExecutor.cs:line 70

@asbjornu
Copy link
Member

Are you able to reproduce this in a public repository, @mujdatdinc?

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

Successfully merging a pull request may close this issue.