Skip to content

Commit

Permalink
Merge pull request #3338 from U7nk/HEAD_support_for_5.x
Browse files Browse the repository at this point in the history
Add HEAD support for 5.x version, without introduction of new configuration properties.
  • Loading branch information
asbjornu authored Jan 20, 2023
2 parents b3dbbbc + 00b8458 commit 544d4d1
Show file tree
Hide file tree
Showing 9 changed files with 24 additions and 22 deletions.
5 changes: 0 additions & 5 deletions docs/input/docs/reference/requirements.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,6 @@ The repository needs to have an existing local `master` or `main` branch.
For some branch strategies (such as [Git Flow][gitflow]), a local `develop`
branch needs to exist.

### Switched branch

The repository should be [switched][git-switch] to a named, existing branch
pointing to the commit being built (i.e. no detached `HEAD`).

### Configuration

If using a `GitVersion.yml` [configuration][configuration] file, that file
Expand Down
5 changes: 5 additions & 0 deletions docs/input/docs/reference/version-increments.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,11 @@ If you create a branch with the version number in the branch name, such as
from the branch name as a source. However, GitVersion can't use the [branch
name as a version source for _other branches_][faq-branch-name-source].

### Detached HEAD
If HEAD is in detached state tag will be `-no-branch-`.

Example: `0.1.0--no-branch-.1+4`

### Tagging commit

By tagging a commit, GitVersion will use that tag for the version of that
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using NUnit.Framework;
using Shouldly;

namespace GitVersion.Core.Tests.IntegrationTests;

Expand Down Expand Up @@ -87,15 +86,14 @@ public void GivenARemoteGitRepositoryAheadOfLocalRepositoryThenChangesShouldPull
}

[Test]
public void GivenARemoteGitRepositoryWhenCheckingOutDetachedHeadUsingExistingImplementationThrowsException()
public void GivenARemoteGitRepositoryWhenCheckingOutDetachedHeadUsingExistingImplementationHandleDetachedBranch()
{
using var fixture = new RemoteRepositoryFixture();
Commands.Checkout(
fixture.LocalRepositoryFixture.Repository,
fixture.LocalRepositoryFixture.Repository.Head.Tip);

Should.Throw<WarningException>(() => fixture.AssertFullSemver("0.1.0+4", repository: fixture.LocalRepositoryFixture.Repository, onlyTrackedBranches: false),
$"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.");
fixture.AssertFullSemver("0.1.0--no-branch-.1+4", repository: fixture.LocalRepositoryFixture.Repository, onlyTrackedBranches: false);
}

[Test]
Expand Down
2 changes: 1 addition & 1 deletion src/GitVersion.Core/Core/Abstractions/IRepositoryStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public interface IRepositoryStore
/// </summary>
BranchCommit FindCommitBranchWasBranchedFrom(IBranch? branch, Config configuration, params IBranch[] excludedBranches);

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

Expand Down
2 changes: 1 addition & 1 deletion src/GitVersion.Core/Core/GitVersionContextFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public GitVersionContext Create(GitVersionOptions gitVersionOptions)
currentBranch = branchForCommit ?? currentBranch;
}

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

return new GitVersionContext(currentBranch, currentCommit, configuration, currentCommitTaggedVersion, numberOfUncommittedChanges);
Expand Down
16 changes: 12 additions & 4 deletions src/GitVersion.Core/Core/RepositoryStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -210,9 +210,9 @@ public BranchCommit FindCommitBranchWasBranchedFrom(IBranch? branch, Config conf
}
}

public SemanticVersion GetCurrentCommitTaggedVersion(ICommit? commit, string? tagPrefix)
public SemanticVersion GetCurrentCommitTaggedVersion(ICommit? commit, string? tagPrefix, bool handleDetachedBranch)
=> this.repository.Tags
.SelectMany(t => GetCurrentCommitSemanticVersions(commit, tagPrefix, t))
.SelectMany(t => GetCurrentCommitSemanticVersions(commit, tagPrefix, t, handleDetachedBranch))
.Max();

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

private static IEnumerable<SemanticVersion> GetCurrentCommitSemanticVersions(ICommit? commit, string? tagPrefix, ITag tag)
private IEnumerable<SemanticVersion> GetCurrentCommitSemanticVersions(ICommit? commit, string? tagPrefix, ITag tag, bool handleDetachedBranch)
{
if (commit == null)
return Array.Empty<SemanticVersion>();

var targetCommit = tag.PeeledTargetCommit();
if (targetCommit == null)
return Array.Empty<SemanticVersion>();

var commitToCompare = handleDetachedBranch ? FindMergeBase(commit, targetCommit) : commit;

var tagName = tag.Name.Friendly;

return targetCommit != null && Equals(targetCommit, commit) && SemanticVersion.TryParse(tagName, tagPrefix, out var version)
return Equals(targetCommit, commitToCompare) && SemanticVersion.TryParse(tagName, tagPrefix, out var version)
? new[] { version }
: Array.Empty<SemanticVersion>();
}
Expand Down
4 changes: 2 additions & 2 deletions src/GitVersion.Core/PublicAPI.Shipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ GitVersion.Common.IRepositoryStore.GetBranchesForCommit(GitVersion.ICommit! comm
GitVersion.Common.IRepositoryStore.GetChosenBranch(GitVersion.Model.Configuration.Config! configuration) -> GitVersion.IBranch?
GitVersion.Common.IRepositoryStore.GetCommitLog(GitVersion.ICommit? baseVersionSource, GitVersion.ICommit? currentCommit) -> System.Collections.Generic.IEnumerable<GitVersion.ICommit!>!
GitVersion.Common.IRepositoryStore.GetCurrentCommit(GitVersion.IBranch! currentBranch, string? commitId) -> GitVersion.ICommit?
GitVersion.Common.IRepositoryStore.GetCurrentCommitTaggedVersion(GitVersion.ICommit? commit, string? tagPrefix) -> GitVersion.SemanticVersion!
GitVersion.Common.IRepositoryStore.GetCurrentCommitTaggedVersion(GitVersion.ICommit? commit, string? tagPrefix, bool handleDetachedBranch) -> GitVersion.SemanticVersion!
GitVersion.Common.IRepositoryStore.GetExcludedInheritBranches(GitVersion.Model.Configuration.Config! configuration) -> System.Collections.Generic.IEnumerable<GitVersion.IBranch!>!
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!>!>!
GitVersion.Common.IRepositoryStore.GetMainlineCommitLog(GitVersion.ICommit? baseVersionSource, GitVersion.ICommit? mainlineTip) -> System.Collections.Generic.IEnumerable<GitVersion.ICommit!>!
Expand Down Expand Up @@ -859,7 +859,7 @@ GitVersion.RepositoryStore.GetBranchesForCommit(GitVersion.ICommit! commit) -> S
GitVersion.RepositoryStore.GetChosenBranch(GitVersion.Model.Configuration.Config! configuration) -> GitVersion.IBranch?
GitVersion.RepositoryStore.GetCommitLog(GitVersion.ICommit? baseVersionSource, GitVersion.ICommit? currentCommit) -> System.Collections.Generic.IEnumerable<GitVersion.ICommit!>!
GitVersion.RepositoryStore.GetCurrentCommit(GitVersion.IBranch! currentBranch, string? commitId) -> GitVersion.ICommit?
GitVersion.RepositoryStore.GetCurrentCommitTaggedVersion(GitVersion.ICommit? commit, string? tagPrefix) -> GitVersion.SemanticVersion!
GitVersion.RepositoryStore.GetCurrentCommitTaggedVersion(GitVersion.ICommit? commit, string? tagPrefix, bool handleDetachedBranch) -> GitVersion.SemanticVersion!
GitVersion.RepositoryStore.GetExcludedInheritBranches(GitVersion.Model.Configuration.Config! configuration) -> System.Collections.Generic.IEnumerable<GitVersion.IBranch!>!
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!>!>!
GitVersion.RepositoryStore.GetMainlineCommitLog(GitVersion.ICommit? baseVersionSource, GitVersion.ICommit? mainlineTip) -> System.Collections.Generic.IEnumerable<GitVersion.ICommit!>!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public VersionField DetermineIncrementedField(GitVersionContext context, BaseVer
return defaultIncrement;
}

return commitMessageIncrement ?? VersionField.None;
return commitMessageIncrement.Value;
}

public VersionField? GetIncrementForCommits(Config configuration, IEnumerable<ICommit> commits)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,6 @@ public NextVersion FindVersion()
{
this.log.Info($"Current commit is tagged with version {Context.CurrentCommitTaggedVersion}, " + "version calculation is for metadata only.");
}
else
{
EnsureHeadIsNotDetached(Context);
}

SemanticVersion? taggedSemanticVersion = null;

Expand Down

0 comments on commit 544d4d1

Please sign in to comment.