Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changing the hardcoded g prefix #445

Merged
merged 3 commits into from Mar 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions src/NerdBank.GitVersioning.Tests/VersionFileTests.cs
Expand Up @@ -146,6 +146,7 @@ public void SetVersion_WritesSimplestFile(string version, string assemblyVersion
[InlineData(@"{""release"":{""increment"":""minor""}}", @"{}")]
[InlineData(@"{""release"":{""branchName"":""v{version}""}}", @"{}")]
[InlineData(@"{""release"":{""firstUnstableTag"":""alpha""}}", @"{}")]
[InlineData(@"{""release"":{""gitCommitIdPrefix"":""g""}}", @"{}")]
[InlineData(@"{""release"":{""firstUnstableTag"":""tag""}}", @"{""release"":{""firstUnstableTag"":""tag""}}")]
[InlineData(@"{""release"":{""branchName"":""v{version}"",""versionIncrement"":""minor"",""firstUnstableTag"":""alpha""}}", @"{}")]
[InlineData(@"{""release"":{""versionIncrement"":""major""}}", @"{""release"":{""versionIncrement"":""major""}}")]
Expand Down
2 changes: 1 addition & 1 deletion src/NerdBank.GitVersioning.Tests/VersionOptionsTests.cs
Expand Up @@ -202,7 +202,7 @@ public void ReleaseOptions_Equality()
var ro5 = new VersionOptions.ReleaseOptions()
{
BranchName = "branchName",
VersionIncrement = VersionOptions.ReleaseVersionIncrement.Minor,
VersionIncrement = VersionOptions.ReleaseVersionIncrement.Minor,
};
var ro6 = new VersionOptions.ReleaseOptions()
{
Expand Down
19 changes: 19 additions & 0 deletions src/NerdBank.GitVersioning.Tests/VersionOracleTests.cs
Expand Up @@ -313,6 +313,25 @@ public void CanSetSemVer2ForNuGetPackageVersionNonPublicRelease()
Assert.Equal($"7.8.9-foo.25.g{this.CommitIdShort}", oracle.NuGetPackageVersion);
}

[Fact]
public void CanSetGitCommitIdPrefixNonPublicRelease()
{
VersionOptions workingCopyVersion = new VersionOptions
{
Version = SemanticVersion.Parse("7.8.9-foo.25"),
NuGetPackageVersion = new VersionOptions.NuGetPackageVersionOptions
{
SemVer = 2,
},
GitCommitIdPrefix = "git",
};
this.WriteVersionFile(workingCopyVersion);
this.InitializeSourceControl();
var oracle = VersionOracle.Create(this.RepoPath);
oracle.PublicRelease = false;
Assert.Equal($"7.8.9-foo.25.git{this.CommitIdShort}", oracle.NuGetPackageVersion);
}

[Fact]
public void CanUseGitProjectRelativePathWithGitRepoRoot()
{
Expand Down
29 changes: 29 additions & 0 deletions src/NerdBank.GitVersioning/VersionOptions.cs
Expand Up @@ -15,6 +15,9 @@
[DebuggerDisplay("{DebuggerDisplay,nq}")]
public class VersionOptions : IEquatable<VersionOptions>
{
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private string gitCommitIdPrefix;

/// <summary>
/// Default value for <see cref="VersionPrecision"/>.
/// </summary>
Expand Down Expand Up @@ -60,6 +63,32 @@ public class VersionOptions : IEquatable<VersionOptions>
[JsonProperty(DefaultValueHandling = DefaultValueHandling.Ignore)]
public AssemblyVersionOptions AssemblyVersion { get; set; }


/// <summary>
/// Gets or sets the prefix for git commit id in version.
/// Because of semver rules the prefix must lead with a [A-z_] character (not a number) and it cannot be the empty string.
/// If <c>null</c> 'g' will be used.
/// </summary>
/// <value>A prefix for git commit id.</value>
[JsonProperty(DefaultValueHandling = DefaultValueHandling.Ignore)]
public string GitCommitIdPrefix
{
get => gitCommitIdPrefix;
set
{
if (string.IsNullOrWhiteSpace(value))
{
throw new ArgumentNullException(nameof(value), $"{nameof(this.GitCommitIdPrefix)} can't be empty");
}
char first = value[0];
if (first < 'A' || (first > 'Z' && first < 'a' && first != '_') || first > 'z')
{
throw new ArgumentException(nameof(value), $"{nameof(this.GitCommitIdPrefix)} must lead with a [A-z_] character (not a number)");
}
this.gitCommitIdPrefix = value;
}
}

/// <summary>
/// Gets the version to use particularly for the <see cref="AssemblyVersionAttribute"/>
/// instead of the default <see cref="Version"/>.
Expand Down
9 changes: 5 additions & 4 deletions src/NerdBank.GitVersioning/VersionOracle.cs
Expand Up @@ -409,7 +409,7 @@ public IEnumerable<string> BuildMetadataWithCommitId
/// See <see href="https://github.com/dotnet/Nerdbank.GitVersioning/issues/260#issuecomment-445511898">this discussion</see>.
/// </remarks>
private string NuGetSemVer1BuildMetadata =>
this.PublicRelease ? string.Empty : $"-g{this.GitCommitIdShort}";
this.PublicRelease ? string.Empty : $"-{this.VersionOptions?.GitCommitIdPrefix ?? "g"}{this.GitCommitIdShort}";

/// <summary>
/// Gets the build metadata, compliant to SemVer 1.0.
Expand Down Expand Up @@ -439,12 +439,13 @@ public IEnumerable<string> BuildMetadataWithCommitId

/// <summary>
/// Gets the -gc0ffee or .gc0ffee suffix for the version.
/// The g in the prefix might be changed if <see cref="VersionOptions.GitCommitIdPrefix"/> is set.
/// </summary>
/// <remarks>
/// The `g` prefix to the commit ID is to remain SemVer2 compliant particularly when the partial commit ID we use is made up entirely of numerals.
/// SemVer2 forbids numerals to begin with leading zeros, but a git commit just might, so we begin with `g` always to avoid failures when the commit ID happens to be problematic.
/// The prefix to the commit ID is to remain SemVer2 compliant particularly when the partial commit ID we use is made up entirely of numerals.
/// SemVer2 forbids numerals to begin with leading zeros, but a git commit just might, so we begin with prefix always to avoid failures when the commit ID happens to be problematic.
/// </remarks>
private string GitCommitIdShortForNonPublicPrereleaseTag => (string.IsNullOrEmpty(this.PrereleaseVersion) ? "-" : ".") + "g" + this.GitCommitIdShort;
private string GitCommitIdShortForNonPublicPrereleaseTag => (string.IsNullOrEmpty(this.PrereleaseVersion) ? "-" : ".") + (this.VersionOptions?.GitCommitIdPrefix ?? "g") + this.GitCommitIdShort;

private VersionOptions.CloudBuildNumberOptions CloudBuildNumberOptions { get; }

Expand Down
6 changes: 6 additions & 0 deletions src/NerdBank.GitVersioning/version.schema.json
Expand Up @@ -79,6 +79,12 @@
"default": 10,
"maximum": 40
},
"gitCommitIdPrefix": {
"type": "string",
"description": "The git commit prefix (e.g. 'g') in non-public release versions.",
"pattern": "^[^0-9][\\da-z\\-_\\.]*$",
"default": "g"
},
"gitCommitIdShortAutoMinimum": {
"type": "integer",
"description": "When greater than 0, the length of the commit ID will be either this value or the shortest unambiguous git-abbreviated commit ID possible, whichever is greater. When 0, the gitCommitIdShortFixedLength property is used instead.",
Expand Down