Skip to content

Commit 824eeb7

Browse files
author
vchirikov
committed
Add GitCommitIdPrefix
1 parent 22b0f16 commit 824eeb7

File tree

5 files changed

+41
-6
lines changed

5 files changed

+41
-6
lines changed

src/NerdBank.GitVersioning.Tests/VersionFileTests.cs

Lines changed: 1 addition & 0 deletions
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/VersionOracleTests.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,26 @@ 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+
};
329+
this.WriteVersionFile(workingCopyVersion);
330+
this.InitializeSourceControl();
331+
var oracle = VersionOracle.Create(this.RepoPath);
332+
oracle.PublicRelease = false;
333+
Assert.Equal($"7.8.9-foo.25.git{this.CommitIdShort}", oracle.NuGetPackageVersion);
334+
}
335+
316336
[Fact]
317337
public void CanUseGitProjectRelativePathWithGitRepoRoot()
318338
{

src/NerdBank.GitVersioning/VersionOptions.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,14 @@ public class VersionOptions : IEquatable<VersionOptions>
6060
[JsonProperty(DefaultValueHandling = DefaultValueHandling.Ignore)]
6161
public AssemblyVersionOptions AssemblyVersion { get; set; }
6262

63+
/// <summary>
64+
/// Gets or sets the prefix for git commit id in version.
65+
/// If <c>null</c> 'g' will be used.
66+
/// </summary>
67+
/// <value>A prefix for git commit id.</value>
68+
[JsonProperty(DefaultValueHandling = DefaultValueHandling.Ignore)]
69+
public string GitCommitIdPrefix { get; set; }
70+
6371
/// <summary>
6472
/// Gets the version to use particularly for the <see cref="AssemblyVersionAttribute"/>
6573
/// instead of the default <see cref="Version"/>.

src/NerdBank.GitVersioning/VersionOracle.cs

Lines changed: 6 additions & 6 deletions
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.
@@ -418,7 +418,7 @@ public IDictionary<string, string> CloudBuildVersionVars
418418
this.PublicRelease ? string.Empty : $"-{this.GitCommitIdShort}";
419419

420420
/// <summary>
421-
/// Gets a SemVer 1.0 compliant string that represents this version, including the -gCOMMITID suffix
421+
/// Gets a SemVer 1.0 compliant string that represents this version, including the -{GitCommitIdPrefix}COMMITID suffix
422422
/// when <see cref="PublicRelease"/> is <c>false</c>.
423423
/// </summary>
424424
private string NuGetSemVer1 =>
@@ -438,13 +438,13 @@ public IDictionary<string, string> CloudBuildVersionVars
438438
private string PrereleaseVersionSemVer1 => SemanticVersionExtensions.MakePrereleaseSemVer1Compliant(this.PrereleaseVersion, this.SemVer1NumericIdentifierPadding);
439439

440440
/// <summary>
441-
/// Gets the -gc0ffee or .gc0ffee suffix for the version.
441+
/// Gets the -{<seealso cref="VersionOptions.GitCommitIdPrefix"/>}c0ffee or .<seealso cref="VersionOptions.GitCommitIdPrefix"/>c0ffee suffix for the version.
442442
/// </summary>
443443
/// <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.
444+
/// 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.
445+
/// 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.
446446
/// </remarks>
447-
private string GitCommitIdShortForNonPublicPrereleaseTag => (string.IsNullOrEmpty(this.PrereleaseVersion) ? "-" : ".") + "g" + this.GitCommitIdShort;
447+
private string GitCommitIdShortForNonPublicPrereleaseTag => (string.IsNullOrEmpty(this.PrereleaseVersion) ? "-" : ".") + (this.VersionOptions?.GitCommitIdPrefix ?? "g") + this.GitCommitIdShort;
448448

449449
private VersionOptions.CloudBuildNumberOptions CloudBuildNumberOptions { get; }
450450

src/NerdBank.GitVersioning/version.schema.json

Lines changed: 6 additions & 0 deletions
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 prerelease 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)