Skip to content

Commit

Permalink
Insert option to considered the remote name when generating a semanti…
Browse files Browse the repository at this point in the history
…c version on a remote branch.
  • Loading branch information
HHobeck committed Feb 24, 2023
1 parent e7f8903 commit 3beab1e
Show file tree
Hide file tree
Showing 13 changed files with 70 additions and 14 deletions.
9 changes: 8 additions & 1 deletion docs/input/docs/reference/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ created. Modify this to suit your needs.
The global configuration looks like this:

```yaml
remote-name-in-git: origin
assembly-versioning-scheme: MajorMinorPatch
assembly-file-versioning-scheme: MajorMinorPatch
label-prefix: '[vV]?'
Expand Down Expand Up @@ -160,7 +161,7 @@ ignore:
sha: []
mode: ContinuousDelivery
label: '{BranchName}'
increment: Inherit
increment: Patch
prevent-increment-of-merged-branch-version: false
track-merge-target: false
track-merge-message: true
Expand All @@ -173,6 +174,12 @@ is-mainline: false
The details of the available options are as follows:
### remote-name-in-git
The user can define the name of the remote (in moste cases it's `origin`) which
will be considered when generating a semantic version on a remote branch. If the
actual remote name doesn't match this pattern an error will be thrown.

### next-version

Allows you to bump the next version explicitly. Useful for bumping `main` or a
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
remote-name-in-git: origin
assembly-versioning-scheme: MajorMinorPatch
assembly-file-versioning-scheme: MajorMinorPatch
label-prefix: '[vV]?'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ namespace GitVersion.Core.Tests.Helpers;
public class TestEffectiveConfiguration : EffectiveConfiguration
{
public TestEffectiveConfiguration(
string remoteNameInGit = GitVersionConfiguration.DefaultRemoteNameInGit,
AssemblyVersioningScheme assemblyVersioningScheme = AssemblyVersioningScheme.MajorMinorPatch,
AssemblyFileVersioningScheme assemblyFileVersioningScheme = AssemblyFileVersioningScheme.MajorMinorPatch,
string? assemblyVersioningFormat = null,
Expand All @@ -31,7 +32,8 @@ public TestEffectiveConfiguration(
bool isMainline = false,
string commitDateFormat = "yyyy-MM-dd",
bool updateBuildNumber = false) :
base(assemblyVersioningScheme,
base(remoteNameInGit,
assemblyVersioningScheme,
assemblyFileVersioningScheme,
assemblyInformationalFormat,
assemblyVersioningFormat,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,26 @@ public void TakesVersionFromNameOfBranchThatIsReleaseByConfig()
fixture.AssertFullSemver("2.0.0+1", configuration);
}

[Test]
public void TakesVersionFromNameOfRemoteReleaseBranchInOrigin()
[TestCase("origin")]
[TestCase("upstream")]
public void TakesVersionFromNameOfRemoteReleaseBranchInOrigin(string remoteNameInGit)
{
var configuration = GitFlowConfigurationBuilder.New
.WithRemoteNameInGit(remoteNameInGit)
.Build();

using var fixture = new RemoteRepositoryFixture();
if (remoteNameInGit != "origin")
{
fixture.LocalRepositoryFixture.Repository.Network.Remotes.Rename("origin", remoteNameInGit);
}
fixture.BranchTo("release/2.0.0");
fixture.MakeACommit();
Commands.Fetch((Repository)fixture.LocalRepositoryFixture.Repository, fixture.LocalRepositoryFixture.Repository.Network.Remotes.First().Name, Array.Empty<string>(), new FetchOptions(), null);

fixture.LocalRepositoryFixture.Checkout("origin/release/2.0.0");
fixture.LocalRepositoryFixture.Checkout($"{remoteNameInGit}/release/2.0.0");

fixture.LocalRepositoryFixture.AssertFullSemver("2.0.0-beta.1+1");
fixture.LocalRepositoryFixture.AssertFullSemver("2.0.0-beta.1+1", configuration);
}

[Test]
Expand All @@ -61,7 +70,8 @@ public void DoesNotTakeVersionFromNameOfRemoteReleaseBranchInCustomRemote()
Commands.Fetch((Repository)fixture.LocalRepositoryFixture.Repository, fixture.LocalRepositoryFixture.Repository.Network.Remotes.First().Name, Array.Empty<string>(), new FetchOptions(), null);

fixture.LocalRepositoryFixture.Checkout("upstream/release/2.0.0");
Action action = () => fixture.LocalRepositoryFixture.GetVersion();

fixture.LocalRepositoryFixture.AssertFullSemver("0.0.0-beta.1+6"); // This test fails with 2.0.0-beta.1+1
action.ShouldThrow<InvalidOperationException>();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ private void VerifyAssemblyInfoFile(
this.fileSystem.ReadAllText(fileName).Returns(assemblyFileContent);
});

var configuration = new TestEffectiveConfiguration(versioningScheme);
var configuration = new TestEffectiveConfiguration(assemblyVersioningScheme: versioningScheme);
var variables = this.variableProvider.GetVariablesFor(version, configuration, false);

verify?.Invoke(this.fileSystem, variables);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ private void VerifyAssemblyInfoFile(
this.fileSystem.ReadAllText(fileName).Returns(projectFileContent);
});

var configuration = new TestEffectiveConfiguration(versioningScheme);
var configuration = new TestEffectiveConfiguration(assemblyVersioningScheme: versioningScheme);
var variables = this.variableProvider.GetVariablesFor(version, configuration, false);

verify?.Invoke(this.fileSystem, variables);
Expand Down
10 changes: 10 additions & 0 deletions src/GitVersion.Core/Configuration/ConfigurationBuilderBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ namespace GitVersion.Configuration;
internal abstract class ConfigurationBuilderBase<TConfigurationBuilder>
where TConfigurationBuilder : ConfigurationBuilderBase<TConfigurationBuilder>
{
private string? remoteNameInGit;
private AssemblyVersioningScheme? assemblyVersioningScheme;
private AssemblyFileVersioningScheme? assemblyFileVersioningScheme;
private string? assemblyInformationalFormat;
Expand Down Expand Up @@ -49,6 +50,13 @@ protected ConfigurationBuilderBase()
}
}

public virtual TConfigurationBuilder WithRemoteNameInGit(string? value)
{
this.remoteNameInGit = value;
return (TConfigurationBuilder)this;
}


public virtual TConfigurationBuilder WithAssemblyVersioningScheme(AssemblyVersioningScheme? value)
{
this.assemblyVersioningScheme = value;
Expand Down Expand Up @@ -276,6 +284,7 @@ public virtual TConfigurationBuilder WithPreReleaseWeight(int? value)

public virtual TConfigurationBuilder WithConfiguration(GitVersionConfiguration value)
{
WithRemoteNameInGit(value.RemoteNameInGit);
WithAssemblyVersioningScheme(value.AssemblyVersioningScheme);
WithAssemblyFileVersioningScheme(value.AssemblyFileVersioningScheme);
WithAssemblyInformationalFormat(value.AssemblyInformationalFormat);
Expand Down Expand Up @@ -328,6 +337,7 @@ public virtual GitVersionConfiguration Build()
{
GitVersionConfiguration configuration = new()
{
RemoteNameInGit = this.remoteNameInGit,
AssemblyVersioningScheme = this.assemblyVersioningScheme,
AssemblyFileVersioningScheme = this.assemblyFileVersioningScheme,
AssemblyInformationalFormat = this.assemblyInformationalFormat,
Expand Down
6 changes: 5 additions & 1 deletion src/GitVersion.Core/Configuration/EffectiveConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public EffectiveConfiguration(GitVersionConfiguration configuration, BranchConfi
if (!configuration.LabelPreReleaseWeight.HasValue)
throw new Exception("Configuration value for 'LabelPreReleaseWeight' has no value. (this should not happen, please report an issue)");

RemoteNameInGit = configuration.RemoteNameInGit;
AssemblyVersioningScheme = configuration.AssemblyVersioningScheme.Value;
AssemblyFileVersioningScheme = configuration.AssemblyFileVersioningScheme.Value;
AssemblyInformationalFormat = configuration.AssemblyInformationalFormat;
Expand Down Expand Up @@ -68,7 +69,8 @@ public EffectiveConfiguration(GitVersionConfiguration configuration, BranchConfi
LabelPreReleaseWeight = configuration.LabelPreReleaseWeight.Value;
}

protected EffectiveConfiguration(AssemblyVersioningScheme assemblyVersioningScheme,
protected EffectiveConfiguration(string remoteNameInGit,
AssemblyVersioningScheme assemblyVersioningScheme,
AssemblyFileVersioningScheme assemblyFileVersioningScheme,
string? assemblyInformationalFormat,
string? assemblyVersioningFormat,
Expand Down Expand Up @@ -97,6 +99,7 @@ protected EffectiveConfiguration(AssemblyVersioningScheme assemblyVersioningSche
int preReleaseWeight,
int labelPreReleaseWeight)
{
RemoteNameInGit = remoteNameInGit;
AssemblyVersioningScheme = assemblyVersioningScheme;
AssemblyFileVersioningScheme = assemblyFileVersioningScheme;
AssemblyInformationalFormat = assemblyInformationalFormat;
Expand Down Expand Up @@ -127,6 +130,7 @@ protected EffectiveConfiguration(AssemblyVersioningScheme assemblyVersioningSche
LabelPreReleaseWeight = labelPreReleaseWeight;
}

public string? RemoteNameInGit { get; }
public bool TracksReleaseBranches { get; }
public bool IsReleaseBranch { get; }
public bool IsMainline { get; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ private GitFlowConfigurationBuilder()
{
WithConfiguration(new()
{
RemoteNameInGit = GitVersionConfiguration.DefaultRemoteNameInGit,
AssemblyFileVersioningScheme = AssemblyFileVersioningScheme.MajorMinorPatch,
AssemblyVersioningScheme = AssemblyVersioningScheme.MajorMinorPatch,
CommitDateFormat = "yyyy-MM-dd",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ private GitHubFlowConfigurationBuilder()
{
WithConfiguration(new()
{
RemoteNameInGit = GitVersionConfiguration.DefaultRemoteNameInGit,
AssemblyFileVersioningScheme = AssemblyFileVersioningScheme.MajorMinorPatch,
AssemblyVersioningScheme = AssemblyVersioningScheme.MajorMinorPatch,
CommitDateFormat = "yyyy-MM-dd",
Expand Down
4 changes: 4 additions & 0 deletions src/GitVersion.Core/Configuration/GitVersionConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ public GitVersionConfiguration()
Ignore = new IgnoreConfiguration();
}

[YamlMember(Alias = "remote-name-in-git")]
public string? RemoteNameInGit { get; set; }

[YamlMember(Alias = "assembly-versioning-scheme")]
public AssemblyVersioningScheme? AssemblyVersioningScheme { get; set; }

Expand Down Expand Up @@ -84,6 +87,7 @@ public override string ToString()
return stringBuilder.ToString();
}

public const string DefaultRemoteNameInGit = "origin";
public const string DefaultLabelPrefix = "[vV]?";
public const string ReleaseBranchRegex = "^releases?[/-]";
public const string FeatureBranchRegex = "^features?[/-]";
Expand Down
16 changes: 14 additions & 2 deletions src/GitVersion.Core/Core/GitVersionContextFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ public GitVersionContextFactory(IConfigurationProvider configurationProvider, IR
public GitVersionContext Create(GitVersionOptions gitVersionOptions)
{
var currentBranch = this.repositoryStore.GetTargetBranch(gitVersionOptions.RepositoryInfo.TargetBranch);
if (currentBranch == null)
throw new InvalidOperationException("Need a branch to operate on");
if (currentBranch == null) throw new InvalidOperationException("Need a branch to operate on");

string blablub = currentBranch.Name.Canonical;

var currentCommit = this.repositoryStore.GetCurrentCommit(currentBranch, gitVersionOptions.RepositoryInfo.CommitId);

Expand All @@ -34,6 +35,17 @@ public GitVersionContext Create(GitVersionOptions gitVersionOptions)
currentBranch = branchForCommit ?? currentBranch;
}

if (currentBranch.IsRemote)
{
var remoteNameInGit = configuration.RemoteNameInGit;
if (string.IsNullOrEmpty(remoteNameInGit) || !currentBranch.Name.Friendly.StartsWith(remoteNameInGit))
{
throw new InvalidOperationException(
$"The remote branch name '{currentBranch.Name.Friendly}' is not valid. Please use another branch or change the configuration."
);
}
}

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

Expand Down
8 changes: 6 additions & 2 deletions src/GitVersion.Core/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#nullable enable
const GitVersion.Configuration.GitVersionConfiguration.DefaultRemoteNameInGit = "origin" -> string!
GitVersion.AssemblyInfoData
GitVersion.AssemblyInfoData.AssemblyInfoData() -> void
GitVersion.AssemblyInfoData.EnsureAssemblyInfo -> bool
Expand Down Expand Up @@ -153,7 +154,7 @@ GitVersion.Configuration.EffectiveConfiguration.BranchPrefixToTrim.get -> string
GitVersion.Configuration.EffectiveConfiguration.CommitDateFormat.get -> string?
GitVersion.Configuration.EffectiveConfiguration.CommitMessageIncrementing.get -> GitVersion.VersionCalculation.CommitMessageIncrementMode
GitVersion.Configuration.EffectiveConfiguration.EffectiveConfiguration(GitVersion.Configuration.GitVersionConfiguration! configuration, GitVersion.Configuration.BranchConfiguration! branchConfiguration) -> void
GitVersion.Configuration.EffectiveConfiguration.EffectiveConfiguration(GitVersion.Extensions.AssemblyVersioningScheme assemblyVersioningScheme, GitVersion.Extensions.AssemblyFileVersioningScheme assemblyFileVersioningScheme, string? assemblyInformationalFormat, string? assemblyVersioningFormat, string? assemblyFileVersioningFormat, GitVersion.VersionCalculation.VersioningMode versioningMode, string? labelPrefix, string! label, string? nextVersion, GitVersion.IncrementStrategy increment, string? branchPrefixToTrim, bool preventIncrementOfMergedBranchVersion, string? labelNumberPattern, bool trackMergeTarget, string? majorVersionBumpMessage, string? minorVersionBumpMessage, string? patchVersionBumpMessage, string? noBumpMessage, GitVersion.VersionCalculation.CommitMessageIncrementMode commitMessageIncrementing, System.Collections.Generic.IEnumerable<GitVersion.VersionCalculation.IVersionFilter!>! versionFilters, bool tracksReleaseBranches, bool isReleaseBranch, bool isMainline, string? commitDateFormat, bool updateBuildNumber, GitVersion.SemanticVersionFormat semanticVersionFormat, int preReleaseWeight, int labelPreReleaseWeight) -> void
GitVersion.Configuration.EffectiveConfiguration.EffectiveConfiguration(string! remoteNameInGit, GitVersion.Extensions.AssemblyVersioningScheme assemblyVersioningScheme, GitVersion.Extensions.AssemblyFileVersioningScheme assemblyFileVersioningScheme, string? assemblyInformationalFormat, string? assemblyVersioningFormat, string? assemblyFileVersioningFormat, GitVersion.VersionCalculation.VersioningMode versioningMode, string? labelPrefix, string! label, string? nextVersion, GitVersion.IncrementStrategy increment, string? branchPrefixToTrim, bool preventIncrementOfMergedBranchVersion, string? labelNumberPattern, bool trackMergeTarget, string? majorVersionBumpMessage, string? minorVersionBumpMessage, string? patchVersionBumpMessage, string? noBumpMessage, GitVersion.VersionCalculation.CommitMessageIncrementMode commitMessageIncrementing, System.Collections.Generic.IEnumerable<GitVersion.VersionCalculation.IVersionFilter!>! versionFilters, bool tracksReleaseBranches, bool isReleaseBranch, bool isMainline, string? commitDateFormat, bool updateBuildNumber, GitVersion.SemanticVersionFormat semanticVersionFormat, int preReleaseWeight, int labelPreReleaseWeight) -> void
GitVersion.Configuration.EffectiveConfiguration.Increment.get -> GitVersion.IncrementStrategy
GitVersion.Configuration.EffectiveConfiguration.IsMainline.get -> bool
GitVersion.Configuration.EffectiveConfiguration.IsReleaseBranch.get -> bool
Expand All @@ -168,6 +169,7 @@ GitVersion.Configuration.EffectiveConfiguration.NoBumpMessage.get -> string?
GitVersion.Configuration.EffectiveConfiguration.PatchVersionBumpMessage.get -> string?
GitVersion.Configuration.EffectiveConfiguration.PreReleaseWeight.get -> int
GitVersion.Configuration.EffectiveConfiguration.PreventIncrementOfMergedBranchVersion.get -> bool
GitVersion.Configuration.EffectiveConfiguration.RemoteNameInGit.get -> string?
GitVersion.Configuration.EffectiveConfiguration.SemanticVersionFormat.get -> GitVersion.SemanticVersionFormat
GitVersion.Configuration.EffectiveConfiguration.SemanticVersionFormat.set -> void
GitVersion.Configuration.EffectiveConfiguration.TrackMergeMessage.get -> bool
Expand All @@ -194,9 +196,9 @@ GitVersion.Configuration.GitVersionConfiguration.CommitDateFormat.set -> void
GitVersion.Configuration.GitVersionConfiguration.GitVersionConfiguration() -> void
GitVersion.Configuration.GitVersionConfiguration.Ignore.get -> GitVersion.Configuration.IgnoreConfiguration!
GitVersion.Configuration.GitVersionConfiguration.Ignore.set -> void
GitVersion.Configuration.GitVersionConfiguration.LabelPrefix.get -> string?
GitVersion.Configuration.GitVersionConfiguration.LabelPreReleaseWeight.get -> int?
GitVersion.Configuration.GitVersionConfiguration.LabelPreReleaseWeight.set -> void
GitVersion.Configuration.GitVersionConfiguration.LabelPrefix.get -> string?
GitVersion.Configuration.GitVersionConfiguration.LabelPrefix.set -> void
GitVersion.Configuration.GitVersionConfiguration.MajorVersionBumpMessage.get -> string?
GitVersion.Configuration.GitVersionConfiguration.MajorVersionBumpMessage.set -> void
Expand All @@ -210,6 +212,8 @@ GitVersion.Configuration.GitVersionConfiguration.NoBumpMessage.get -> string?
GitVersion.Configuration.GitVersionConfiguration.NoBumpMessage.set -> void
GitVersion.Configuration.GitVersionConfiguration.PatchVersionBumpMessage.get -> string?
GitVersion.Configuration.GitVersionConfiguration.PatchVersionBumpMessage.set -> void
GitVersion.Configuration.GitVersionConfiguration.RemoteNameInGit.get -> string?
GitVersion.Configuration.GitVersionConfiguration.RemoteNameInGit.set -> void
GitVersion.Configuration.GitVersionConfiguration.SemanticVersionFormat.get -> GitVersion.SemanticVersionFormat
GitVersion.Configuration.GitVersionConfiguration.SemanticVersionFormat.set -> void
GitVersion.Configuration.GitVersionConfiguration.UpdateBuildNumber.get -> bool
Expand Down

0 comments on commit 3beab1e

Please sign in to comment.