Skip to content

Commit 3614ffd

Browse files
U7nkU7nk
authored andcommitted
Add detached branch handling
1 parent d4fc9b4 commit 3614ffd

File tree

4 files changed

+14
-10
lines changed

4 files changed

+14
-10
lines changed

src/GitVersion.Core/Core/Abstractions/IRepositoryStore.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public interface IRepositoryStore
3333
/// </summary>
3434
BranchCommit FindCommitBranchWasBranchedFrom(IBranch? branch, Config configuration, params IBranch[] excludedBranches);
3535

36-
SemanticVersion GetCurrentCommitTaggedVersion(ICommit? commit, string? tagPrefix);
36+
SemanticVersion GetCurrentCommitTaggedVersion(ICommit? commit, string? tagPrefix, bool handleDetachedBranch);
3737
IEnumerable<SemanticVersion> GetVersionTagsOnBranch(IBranch branch, string? tagPrefixRegex);
3838
IEnumerable<(ITag Tag, SemanticVersion Semver, ICommit Commit)> GetValidVersionTags(string? tagPrefixRegex, DateTimeOffset? olderThan = null);
3939

src/GitVersion.Core/Core/GitVersionContextFactory.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public GitVersionContext Create(GitVersionOptions gitVersionOptions)
3333
currentBranch = branchForCommit ?? currentBranch;
3434
}
3535

36-
var currentCommitTaggedVersion = this.repositoryStore.GetCurrentCommitTaggedVersion(currentCommit, configuration.TagPrefix);
36+
var currentCommitTaggedVersion = this.repositoryStore.GetCurrentCommitTaggedVersion(currentCommit, configuration.TagPrefix, handleDetachedBranch: currentBranch.IsDetachedHead);
3737
var numberOfUncommittedChanges = this.repositoryStore.GetNumberOfUncommittedChanges();
3838

3939
return new GitVersionContext(currentBranch, currentCommit, configuration, currentCommitTaggedVersion, numberOfUncommittedChanges);

src/GitVersion.Core/Core/RepositoryStore.cs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -210,9 +210,9 @@ public BranchCommit FindCommitBranchWasBranchedFrom(IBranch? branch, Config conf
210210
}
211211
}
212212

213-
public SemanticVersion GetCurrentCommitTaggedVersion(ICommit? commit, string? tagPrefix)
213+
public SemanticVersion GetCurrentCommitTaggedVersion(ICommit? commit, string? tagPrefix, bool handleDetachedBranch)
214214
=> this.repository.Tags
215-
.SelectMany(t => GetCurrentCommitSemanticVersions(commit, tagPrefix, t))
215+
.SelectMany(t => GetCurrentCommitSemanticVersions(commit, tagPrefix, t, handleDetachedBranch))
216216
.Max();
217217

218218
public IEnumerable<SemanticVersion> GetVersionTagsOnBranch(IBranch branch, string? tagPrefixRegex)
@@ -281,12 +281,20 @@ public bool IsCommitOnBranch(ICommit? baseVersionSource, IBranch branch, ICommit
281281
private static bool IsReleaseBranch(INamedReference branch, IEnumerable<KeyValuePair<string, BranchConfig>> releaseBranchConfig)
282282
=> releaseBranchConfig.Any(c => c.Value?.Regex != null && Regex.IsMatch(branch.Name.Friendly, c.Value.Regex));
283283

284-
private static IEnumerable<SemanticVersion> GetCurrentCommitSemanticVersions(ICommit? commit, string? tagPrefix, ITag tag)
284+
private IEnumerable<SemanticVersion> GetCurrentCommitSemanticVersions(ICommit? commit, string? tagPrefix, ITag tag, bool handleDetachedBranch)
285285
{
286+
if (commit == null)
287+
return Array.Empty<SemanticVersion>();
288+
286289
var targetCommit = tag.PeeledTargetCommit();
290+
if (targetCommit == null)
291+
return Array.Empty<SemanticVersion>();
292+
293+
var commitToCompare = handleDetachedBranch ? FindMergeBase(commit, targetCommit) : commit;
294+
287295
var tagName = tag.Name.Friendly;
288296

289-
return targetCommit != null && Equals(targetCommit, commit) && SemanticVersion.TryParse(tagName, tagPrefix, out var version)
297+
return Equals(targetCommit, commitToCompare) && SemanticVersion.TryParse(tagName, tagPrefix, out var version)
290298
? new[] { version }
291299
: Array.Empty<SemanticVersion>();
292300
}

src/GitVersion.Core/VersionCalculation/NextVersionCalculator.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,6 @@ public NextVersion FindVersion()
4040
{
4141
this.log.Info($"Current commit is tagged with version {Context.CurrentCommitTaggedVersion}, " + "version calculation is for metadata only.");
4242
}
43-
else
44-
{
45-
EnsureHeadIsNotDetached(Context);
46-
}
4743

4844
SemanticVersion? taggedSemanticVersion = null;
4945

0 commit comments

Comments
 (0)