Skip to content

Commit ab61962

Browse files
ctaggartAArnott
authored andcommitted
get commit short id from git repo (#304)
1 parent 6fbb081 commit ab61962

File tree

6 files changed

+74
-6
lines changed

6 files changed

+74
-6
lines changed

doc/versionJson.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ The content of the version.json file is a JSON serialized object with these prop
2828
"assemblyVersion": "x.y", // optional. Use when x.y for AssemblyVersionAttribute differs from the default version property.
2929
"buildNumberOffset": "zOffset", // optional. Use when you need to add/subtract a fixed value from the computed build number.
3030
"semVer1NumericIdentifierPadding": 4, // optional. Use when your -prerelease includes numeric identifiers and need semver1 support.
31+
"gitCommitIdShortFixedLength": 10, // optional. Set the commit ID abbreviation length.
32+
"gitCommitIdShortAutoMinimum": 0, // optional. Set to use the short commit ID abbreviation provided by the git repository.
3133
"nugetPackageVersion": {
3234
"semVer": 1 // optional. Set to either 1 or 2 to control how the NuGet package version string is generated. Default is 1.
3335
},

src/NerdBank.GitVersioning.Tests/BuildIntegrationTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public BuildIntegrationTests(ITestOutputHelper logger)
7272
}
7373
}
7474

75-
private string CommitIdShort => this.Repo.Head.Commits.First().Id.Sha.Substring(0, 10);
75+
private string CommitIdShort => this.Repo.Head.Commits.First().Id.Sha.Substring(0, VersionOptions.DefaultGitCommitIdShortFixedLength);
7676

7777
protected override void Dispose(bool disposing)
7878
{
@@ -176,7 +176,7 @@ public async Task GetBuildVersion_In_Git_But_Head_Lacks_VersionFile()
176176
Assumes.True(repo.Index[VersionFile.JsonFileName] == null);
177177
var buildResult = await this.BuildAsync();
178178
Assert.Equal("3.4.0." + repo.Head.Commits.First().GetIdAsVersion().Revision, buildResult.BuildVersion);
179-
Assert.Equal("3.4.0+" + repo.Head.Commits.First().Id.Sha.Substring(0, 10), buildResult.AssemblyInformationalVersion);
179+
Assert.Equal("3.4.0+" + repo.Head.Commits.First().Id.Sha.Substring(0, VersionOptions.DefaultGitCommitIdShortFixedLength), buildResult.AssemblyInformationalVersion);
180180
}
181181

182182
[Fact]
@@ -201,7 +201,7 @@ public async Task GetBuildVersion_In_Git_No_VersionFile_At_All()
201201
repo.Commit("empty", this.Signer, this.Signer, new CommitOptions { AllowEmptyCommit = true });
202202
var buildResult = await this.BuildAsync();
203203
Assert.Equal("0.0.1." + repo.Head.Commits.First().GetIdAsVersion().Revision, buildResult.BuildVersion);
204-
Assert.Equal("0.0.1+" + repo.Head.Commits.First().Id.Sha.Substring(0, 10), buildResult.AssemblyInformationalVersion);
204+
Assert.Equal("0.0.1+" + repo.Head.Commits.First().Id.Sha.Substring(0, VersionOptions.DefaultGitCommitIdShortFixedLength), buildResult.AssemblyInformationalVersion);
205205
}
206206

207207
[Fact]

src/NerdBank.GitVersioning.Tests/VersionOracleTests.cs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public VersionOracleTests(ITestOutputHelper logger)
1515
{
1616
}
1717

18-
private string CommitIdShort => this.Repo.Head.Commits.First().Id.Sha.Substring(0, 10);
18+
private string CommitIdShort => this.Repo.Head.Commits.First().Id.Sha.Substring(0, VersionOptions.DefaultGitCommitIdShortFixedLength);
1919

2020
[Fact]
2121
public void NotRepo()
@@ -180,6 +180,24 @@ public void SemVerStableNonPublicVersion()
180180
Assert.Matches(@"^2.3.1-g[a-f0-9]{10}$", oracle.ChocolateyPackageVersion);
181181
}
182182

183+
[Fact]
184+
public void SemVerStableNonPublicVersionShortened()
185+
{
186+
var workingCopyVersion = new VersionOptions
187+
{
188+
Version = SemanticVersion.Parse("2.3"),
189+
GitCommitIdShortFixedLength = 7
190+
};
191+
this.WriteVersionFile(workingCopyVersion);
192+
this.InitializeSourceControl();
193+
var oracle = VersionOracle.Create(this.RepoPath);
194+
oracle.PublicRelease = false;
195+
Assert.Matches(@"^2.3.1-[^g]{7}$", oracle.SemVer1);
196+
Assert.Matches(@"^2.3.1-[^g]{7}$", oracle.SemVer2);
197+
Assert.Matches(@"^2.3.1-g[a-f0-9]{7}$", oracle.NuGetPackageVersion);
198+
Assert.Matches(@"^2.3.1-g[a-f0-9]{7}$", oracle.ChocolateyPackageVersion);
199+
}
200+
183201
[Fact]
184202
public void DefaultNuGetPackageVersionIsSemVer1PublicRelease()
185203
{

src/NerdBank.GitVersioning/VersionOptions.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ public class VersionOptions : IEquatable<VersionOptions>
2626
/// </summary>
2727
private const int DefaultSemVer1NumericIdentifierPadding = 4;
2828

29+
/// <summary>
30+
/// The default value for the <see cref="GitCommitIdShortFixedLength"/> property.
31+
/// </summary>
32+
public const int DefaultGitCommitIdShortFixedLength = 10;
33+
2934
/// <summary>
3035
/// The $schema field that should be serialized when writing
3136
/// </summary>
@@ -88,6 +93,21 @@ public class VersionOptions : IEquatable<VersionOptions>
8893
[JsonIgnore]
8994
public int SemVer1NumericIdentifierPaddingOrDefault => this.SemVer1NumericIdentifierPadding ?? DefaultSemVer1NumericIdentifierPadding;
9095

96+
/// <summary>
97+
/// Gets or sets the abbreviated git commit hash length.
98+
/// </summary>
99+
[JsonProperty(DefaultValueHandling = DefaultValueHandling.Ignore)]
100+
public int? GitCommitIdShortFixedLength { get; set; }
101+
102+
/// <summary>
103+
/// Gets or sets the abbreviated git commit hash length minimum value.
104+
/// The git repository provides the value.
105+
/// If set to 0 or a git repository is not available, <see cref="GitCommitIdShortFixedLength"/> is used.
106+
/// The value is 0 by default.
107+
/// </summary>
108+
[JsonProperty(DefaultValueHandling = DefaultValueHandling.Ignore)]
109+
public int? GitCommitIdShortAutoMinimum { get; set; }
110+
91111
/// <summary>
92112
/// Gets or sets the options around NuGet version strings
93113
/// </summary>

src/NerdBank.GitVersioning/VersionOracle.cs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,22 @@ public VersionOracle(string projectDirectory, LibGit2Sharp.Repository repo, LibG
9696
this.Version = this.VersionOptions?.Version.Version ?? Version0;
9797
}
9898

99+
// get the commit id abbreviation only if the commit id is set
100+
if (!string.IsNullOrEmpty(this.GitCommitId))
101+
{
102+
var gitCommitIdShortFixedLength = this.VersionOptions?.GitCommitIdShortFixedLength ?? VersionOptions.DefaultGitCommitIdShortFixedLength;
103+
var gitCommitIdShortAutoMinimum = this.VersionOptions?.GitCommitIdShortAutoMinimum ?? 0;
104+
// get it from the git repository if there is a repository present and it is enabled
105+
if (repo != null && gitCommitIdShortAutoMinimum > 0)
106+
{
107+
this.GitCommitIdShort = repo.ObjectDatabase.ShortenObjectId(commit, gitCommitIdShortAutoMinimum);
108+
}
109+
else
110+
{
111+
this.GitCommitIdShort = this.GitCommitId.Substring(0, gitCommitIdShortFixedLength);
112+
}
113+
}
114+
99115
this.VersionHeightOffset = this.VersionOptions?.BuildNumberOffsetOrDefault ?? 0;
100116

101117
this.PrereleaseVersion = this.ReplaceMacros(this.VersionOptions?.Version?.Prerelease ?? string.Empty);
@@ -143,7 +159,7 @@ public IEnumerable<string> BuildMetadataWithCommitId
143159
{
144160
if (!string.IsNullOrEmpty(this.GitCommitId))
145161
{
146-
yield return this.GitCommitId.Substring(0, 10);
162+
yield return this.GitCommitIdShort;
147163
}
148164

149165
foreach (string identifier in this.BuildMetadata)
@@ -238,7 +254,7 @@ public IEnumerable<string> BuildMetadataWithCommitId
238254
/// <summary>
239255
/// Gets the first several characters of the Git revision control commit id for HEAD (the current source code version).
240256
/// </summary>
241-
public string GitCommitIdShort => string.IsNullOrEmpty(this.GitCommitId) ? null : this.GitCommitId.Substring(0, 10);
257+
public string GitCommitIdShort { get; }
242258

243259
/// <summary>
244260
/// Gets the number of commits in the longest single path between

src/NerdBank.GitVersioning/version.schema.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,18 @@
6868
"minimum": 1,
6969
"maximum": 6
7070
},
71+
"gitCommitIdShortFixedLength": {
72+
"type": "integer",
73+
"description": "The fixed number of characters to use for the commit ID abbreviation length. This property is ignored if the gitCommitIdShortAutoMinimum property is greater than 0.",
74+
"default": 10,
75+
"maximum": 40
76+
},
77+
"gitCommitIdShortAutoMinimum": {
78+
"type": "integer",
79+
"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.",
80+
"default": 0,
81+
"maximum": 40
82+
},
7183
"nugetPackageVersion": {
7284
"type": "object",
7385
"description": "Details for how and what the generated version for NuGet packages will be.",

0 commit comments

Comments
 (0)