Skip to content

Commit 5bb27a1

Browse files
authored
Merge pull request #445 from vchirikov/master
Changing the hardcoded `g` prefix #2
2 parents 68f1376 + 3cbd500 commit 5bb27a1

File tree

6 files changed

+61
-5
lines changed

6 files changed

+61
-5
lines changed

src/NerdBank.GitVersioning.Tests/VersionFileTests.cs

+1
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ public void SetVersion_WritesSimplestFile(string version, string assemblyVersion
146146
[InlineData(@"{""release"":{""increment"":""minor""}}", @"{}")]
147147
[InlineData(@"{""release"":{""branchName"":""v{version}""}}", @"{}")]
148148
[InlineData(@"{""release"":{""firstUnstableTag"":""alpha""}}", @"{}")]
149+
[InlineData(@"{""release"":{""gitCommitIdPrefix"":""g""}}", @"{}")]
149150
[InlineData(@"{""release"":{""firstUnstableTag"":""tag""}}", @"{""release"":{""firstUnstableTag"":""tag""}}")]
150151
[InlineData(@"{""release"":{""branchName"":""v{version}"",""versionIncrement"":""minor"",""firstUnstableTag"":""alpha""}}", @"{}")]
151152
[InlineData(@"{""release"":{""versionIncrement"":""major""}}", @"{""release"":{""versionIncrement"":""major""}}")]

src/NerdBank.GitVersioning.Tests/VersionOptionsTests.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ public void ReleaseOptions_Equality()
202202
var ro5 = new VersionOptions.ReleaseOptions()
203203
{
204204
BranchName = "branchName",
205-
VersionIncrement = VersionOptions.ReleaseVersionIncrement.Minor,
205+
VersionIncrement = VersionOptions.ReleaseVersionIncrement.Minor,
206206
};
207207
var ro6 = new VersionOptions.ReleaseOptions()
208208
{

src/NerdBank.GitVersioning.Tests/VersionOracleTests.cs

+19
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,25 @@ public void CanSetSemVer2ForNuGetPackageVersionNonPublicRelease()
313313
Assert.Equal($"7.8.9-foo.25.g{this.CommitIdShort}", oracle.NuGetPackageVersion);
314314
}
315315

316+
[Fact]
317+
public void CanSetGitCommitIdPrefixNonPublicRelease()
318+
{
319+
VersionOptions workingCopyVersion = new VersionOptions
320+
{
321+
Version = SemanticVersion.Parse("7.8.9-foo.25"),
322+
NuGetPackageVersion = new VersionOptions.NuGetPackageVersionOptions
323+
{
324+
SemVer = 2,
325+
},
326+
GitCommitIdPrefix = "git",
327+
};
328+
this.WriteVersionFile(workingCopyVersion);
329+
this.InitializeSourceControl();
330+
var oracle = VersionOracle.Create(this.RepoPath);
331+
oracle.PublicRelease = false;
332+
Assert.Equal($"7.8.9-foo.25.git{this.CommitIdShort}", oracle.NuGetPackageVersion);
333+
}
334+
316335
[Fact]
317336
public void CanUseGitProjectRelativePathWithGitRepoRoot()
318337
{

src/NerdBank.GitVersioning/VersionOptions.cs

+29
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
[DebuggerDisplay("{DebuggerDisplay,nq}")]
1616
public class VersionOptions : IEquatable<VersionOptions>
1717
{
18+
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
19+
private string gitCommitIdPrefix;
20+
1821
/// <summary>
1922
/// Default value for <see cref="VersionPrecision"/>.
2023
/// </summary>
@@ -60,6 +63,32 @@ public class VersionOptions : IEquatable<VersionOptions>
6063
[JsonProperty(DefaultValueHandling = DefaultValueHandling.Ignore)]
6164
public AssemblyVersionOptions AssemblyVersion { get; set; }
6265

66+
67+
/// <summary>
68+
/// Gets or sets the prefix for git commit id in version.
69+
/// Because of semver rules the prefix must lead with a [A-z_] character (not a number) and it cannot be the empty string.
70+
/// If <c>null</c> 'g' will be used.
71+
/// </summary>
72+
/// <value>A prefix for git commit id.</value>
73+
[JsonProperty(DefaultValueHandling = DefaultValueHandling.Ignore)]
74+
public string GitCommitIdPrefix
75+
{
76+
get => gitCommitIdPrefix;
77+
set
78+
{
79+
if (string.IsNullOrWhiteSpace(value))
80+
{
81+
throw new ArgumentNullException(nameof(value), $"{nameof(this.GitCommitIdPrefix)} can't be empty");
82+
}
83+
char first = value[0];
84+
if (first < 'A' || (first > 'Z' && first < 'a' && first != '_') || first > 'z')
85+
{
86+
throw new ArgumentException(nameof(value), $"{nameof(this.GitCommitIdPrefix)} must lead with a [A-z_] character (not a number)");
87+
}
88+
this.gitCommitIdPrefix = value;
89+
}
90+
}
91+
6392
/// <summary>
6493
/// Gets the version to use particularly for the <see cref="AssemblyVersionAttribute"/>
6594
/// instead of the default <see cref="Version"/>.

src/NerdBank.GitVersioning/VersionOracle.cs

+5-4
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,7 @@ public IDictionary<string, string> CloudBuildVersionVars
409409
/// See <see href="https://github.com/dotnet/Nerdbank.GitVersioning/issues/260#issuecomment-445511898">this discussion</see>.
410410
/// </remarks>
411411
private string NuGetSemVer1BuildMetadata =>
412-
this.PublicRelease ? string.Empty : $"-g{this.GitCommitIdShort}";
412+
this.PublicRelease ? string.Empty : $"-{this.VersionOptions?.GitCommitIdPrefix ?? "g"}{this.GitCommitIdShort}";
413413

414414
/// <summary>
415415
/// Gets the build metadata, compliant to SemVer 1.0.
@@ -439,12 +439,13 @@ public IDictionary<string, string> CloudBuildVersionVars
439439

440440
/// <summary>
441441
/// Gets the -gc0ffee or .gc0ffee suffix for the version.
442+
/// The g in the prefix might be changed if <see cref="VersionOptions.GitCommitIdPrefix"/> is set.
442443
/// </summary>
443444
/// <remarks>
444-
/// 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.
445-
/// 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.
445+
/// 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.
446+
/// 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.
446447
/// </remarks>
447-
private string GitCommitIdShortForNonPublicPrereleaseTag => (string.IsNullOrEmpty(this.PrereleaseVersion) ? "-" : ".") + "g" + this.GitCommitIdShort;
448+
private string GitCommitIdShortForNonPublicPrereleaseTag => (string.IsNullOrEmpty(this.PrereleaseVersion) ? "-" : ".") + (this.VersionOptions?.GitCommitIdPrefix ?? "g") + this.GitCommitIdShort;
448449

449450
private VersionOptions.CloudBuildNumberOptions CloudBuildNumberOptions { get; }
450451

src/NerdBank.GitVersioning/version.schema.json

+6
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,12 @@
7979
"default": 10,
8080
"maximum": 40
8181
},
82+
"gitCommitIdPrefix": {
83+
"type": "string",
84+
"description": "The git commit prefix (e.g. 'g') in non-public release versions.",
85+
"pattern": "^[^0-9][\\da-z\\-_\\.]*$",
86+
"default": "g"
87+
},
8288
"gitCommitIdShortAutoMinimum": {
8389
"type": "integer",
8490
"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.",

0 commit comments

Comments
 (0)