Skip to content

Commit c9e3c80

Browse files
committed
Improve ExecuteCore.ExecuteInternal to handle cases where HEAD is not pointing at the desired branch
1 parent 489f9d0 commit c9e3c80

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

src/GitVersionCore/ExecuteCore.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,18 @@ VersionVariables ExecuteInternal(string targetBranch, string commitId, GitPrepar
108108

109109
return gitPreparer.WithRepository(repo =>
110110
{
111-
var gitVersionContext = new GitVersionContext(repo, configuration, commitId: commitId);
111+
// By default, we assume HEAD is pointing to the desired branch
112+
var currentBranch = repo.Head;
113+
114+
// There are some edge cases where HEAD is not pointing to the desired branch.
115+
// Therefore it's important to verify if 'currentBranch' is indeed the desired branch
116+
if (currentBranch.CanonicalName != targetBranch)
117+
{
118+
// In the case where HEAD is not the desired branch, try to find the branch with matching name
119+
currentBranch = repo.Branches.SingleOrDefault(b => b.CanonicalName == targetBranch) ?? repo.Head;
120+
}
121+
122+
var gitVersionContext = new GitVersionContext(repo, currentBranch, configuration, commitId: commitId);
112123
var semanticVersion = versionFinder.FindVersion(gitVersionContext);
113124

114125
return VariableProvider.GetVariablesFor(semanticVersion, gitVersionContext.Configuration, gitVersionContext.IsCurrentCommitTagged);

0 commit comments

Comments
 (0)