Skip to content

Commit 544d4d1

Browse files
authored
Merge pull request #3338 from U7nk/HEAD_support_for_5.x
Add HEAD support for 5.x version, without introduction of new configuration properties.
2 parents b3dbbbc + 00b8458 commit 544d4d1

File tree

9 files changed

+24
-22
lines changed

9 files changed

+24
-22
lines changed

docs/input/docs/reference/requirements.md

-5
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,6 @@ The repository needs to have an existing local `master` or `main` branch.
2828
For some branch strategies (such as [Git Flow][gitflow]), a local `develop`
2929
branch needs to exist.
3030

31-
### Switched branch
32-
33-
The repository should be [switched][git-switch] to a named, existing branch
34-
pointing to the commit being built (i.e. no detached `HEAD`).
35-
3631
### Configuration
3732

3833
If using a `GitVersion.yml` [configuration][configuration] file, that file

docs/input/docs/reference/version-increments.md

+5
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,11 @@ If you create a branch with the version number in the branch name, such as
115115
from the branch name as a source. However, GitVersion can't use the [branch
116116
name as a version source for _other branches_][faq-branch-name-source].
117117

118+
### Detached HEAD
119+
If HEAD is in detached state tag will be `-no-branch-`.
120+
121+
Example: `0.1.0--no-branch-.1+4`
122+
118123
### Tagging commit
119124

120125
By tagging a commit, GitVersion will use that tag for the version of that

src/GitVersion.Core.Tests/IntegrationTests/RemoteRepositoryScenarios.cs

+2-4
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
using Microsoft.Extensions.DependencyInjection;
66
using Microsoft.Extensions.Options;
77
using NUnit.Framework;
8-
using Shouldly;
98

109
namespace GitVersion.Core.Tests.IntegrationTests;
1110

@@ -87,15 +86,14 @@ public void GivenARemoteGitRepositoryAheadOfLocalRepositoryThenChangesShouldPull
8786
}
8887

8988
[Test]
90-
public void GivenARemoteGitRepositoryWhenCheckingOutDetachedHeadUsingExistingImplementationThrowsException()
89+
public void GivenARemoteGitRepositoryWhenCheckingOutDetachedHeadUsingExistingImplementationHandleDetachedBranch()
9190
{
9291
using var fixture = new RemoteRepositoryFixture();
9392
Commands.Checkout(
9493
fixture.LocalRepositoryFixture.Repository,
9594
fixture.LocalRepositoryFixture.Repository.Head.Tip);
9695

97-
Should.Throw<WarningException>(() => fixture.AssertFullSemver("0.1.0+4", repository: fixture.LocalRepositoryFixture.Repository, onlyTrackedBranches: false),
98-
$"It looks like the branch being examined is a detached Head pointing to commit '{fixture.LocalRepositoryFixture.Repository.Head.Tip.Id.ToString(7)}'. Without a proper branch name GitVersion cannot determine the build version.");
96+
fixture.AssertFullSemver("0.1.0--no-branch-.1+4", repository: fixture.LocalRepositoryFixture.Repository, onlyTrackedBranches: false);
9997
}
10098

10199
[Test]

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

+1-1
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

+1-1
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

+12-4
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/PublicAPI.Shipped.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ GitVersion.Common.IRepositoryStore.GetBranchesForCommit(GitVersion.ICommit! comm
157157
GitVersion.Common.IRepositoryStore.GetChosenBranch(GitVersion.Model.Configuration.Config! configuration) -> GitVersion.IBranch?
158158
GitVersion.Common.IRepositoryStore.GetCommitLog(GitVersion.ICommit? baseVersionSource, GitVersion.ICommit? currentCommit) -> System.Collections.Generic.IEnumerable<GitVersion.ICommit!>!
159159
GitVersion.Common.IRepositoryStore.GetCurrentCommit(GitVersion.IBranch! currentBranch, string? commitId) -> GitVersion.ICommit?
160-
GitVersion.Common.IRepositoryStore.GetCurrentCommitTaggedVersion(GitVersion.ICommit? commit, string? tagPrefix) -> GitVersion.SemanticVersion!
160+
GitVersion.Common.IRepositoryStore.GetCurrentCommitTaggedVersion(GitVersion.ICommit? commit, string? tagPrefix, bool handleDetachedBranch) -> GitVersion.SemanticVersion!
161161
GitVersion.Common.IRepositoryStore.GetExcludedInheritBranches(GitVersion.Model.Configuration.Config! configuration) -> System.Collections.Generic.IEnumerable<GitVersion.IBranch!>!
162162
GitVersion.Common.IRepositoryStore.GetMainlineBranches(GitVersion.ICommit! commit, GitVersion.Model.Configuration.Config! configuration, System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string!, GitVersion.Model.Configuration.BranchConfig!>>? mainlineBranchConfigs) -> System.Collections.Generic.IDictionary<string!, System.Collections.Generic.List<GitVersion.IBranch!>!>!
163163
GitVersion.Common.IRepositoryStore.GetMainlineCommitLog(GitVersion.ICommit? baseVersionSource, GitVersion.ICommit? mainlineTip) -> System.Collections.Generic.IEnumerable<GitVersion.ICommit!>!
@@ -859,7 +859,7 @@ GitVersion.RepositoryStore.GetBranchesForCommit(GitVersion.ICommit! commit) -> S
859859
GitVersion.RepositoryStore.GetChosenBranch(GitVersion.Model.Configuration.Config! configuration) -> GitVersion.IBranch?
860860
GitVersion.RepositoryStore.GetCommitLog(GitVersion.ICommit? baseVersionSource, GitVersion.ICommit? currentCommit) -> System.Collections.Generic.IEnumerable<GitVersion.ICommit!>!
861861
GitVersion.RepositoryStore.GetCurrentCommit(GitVersion.IBranch! currentBranch, string? commitId) -> GitVersion.ICommit?
862-
GitVersion.RepositoryStore.GetCurrentCommitTaggedVersion(GitVersion.ICommit? commit, string? tagPrefix) -> GitVersion.SemanticVersion!
862+
GitVersion.RepositoryStore.GetCurrentCommitTaggedVersion(GitVersion.ICommit? commit, string? tagPrefix, bool handleDetachedBranch) -> GitVersion.SemanticVersion!
863863
GitVersion.RepositoryStore.GetExcludedInheritBranches(GitVersion.Model.Configuration.Config! configuration) -> System.Collections.Generic.IEnumerable<GitVersion.IBranch!>!
864864
GitVersion.RepositoryStore.GetMainlineBranches(GitVersion.ICommit! commit, GitVersion.Model.Configuration.Config! configuration, System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string!, GitVersion.Model.Configuration.BranchConfig!>>? mainlineBranchConfigs) -> System.Collections.Generic.IDictionary<string!, System.Collections.Generic.List<GitVersion.IBranch!>!>!
865865
GitVersion.RepositoryStore.GetMainlineCommitLog(GitVersion.ICommit? baseVersionSource, GitVersion.ICommit? mainlineTip) -> System.Collections.Generic.IEnumerable<GitVersion.ICommit!>!

src/GitVersion.Core/VersionCalculation/IncrementStrategyFinder.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public VersionField DetermineIncrementedField(GitVersionContext context, BaseVer
5353
return defaultIncrement;
5454
}
5555

56-
return commitMessageIncrement ?? VersionField.None;
56+
return commitMessageIncrement.Value;
5757
}
5858

5959
public VersionField? GetIncrementForCommits(Config configuration, IEnumerable<ICommit> commits)

src/GitVersion.Core/VersionCalculation/NextVersionCalculator.cs

-4
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)