diff --git a/docs/articles/results.md b/docs/articles/results.md
index 17cbf2d..9194d39 100644
--- a/docs/articles/results.md
+++ b/docs/articles/results.md
@@ -16,6 +16,7 @@ The following is an example from invoking the command line tool:
"Height": 18,
"HeightPadded": "0018",
"Sha": "ebc8f22ae83bfa3c1e36d6bf70c2a383ae30c9dd",
+ "Sha7": "ebc8f22",
"CanonicalBranchName": "refs/heads/preview/test",
"BranchName": "preview/test",
"Formats": {
@@ -38,6 +39,7 @@ Properties
| Height | int | The calculated height |
| HeightPadded | int | The calculated height padded to four digits |
| Sha | string | The sha of the current commit at the time of invocation |
+| Sha7 | string | The sha of the current commit at the time of invocation, shortened to 7 characters |
| BranchName | string | The checked out branch at the time of invocation |
| CanonicalBranchName | string | The full canonical name of the checked out branch at the time of invocation |
diff --git a/src/SimpleVersion.Abstractions/Model/VersionResult.cs b/src/SimpleVersion.Abstractions/Model/VersionResult.cs
index e617304..1d541b9 100644
--- a/src/SimpleVersion.Abstractions/Model/VersionResult.cs
+++ b/src/SimpleVersion.Abstractions/Model/VersionResult.cs
@@ -52,6 +52,11 @@ public class VersionResult
///
public string Sha { get; set; }
+ ///
+ /// Gets or sets the shortened (7 char) sha of the current commit.
+ ///
+ public string Sha7 { get; set; }
+
///
/// Gets or sets the friendly branch name of the current branch.
///
diff --git a/src/SimpleVersion.Core/Pipeline/VersionContext.cs b/src/SimpleVersion.Core/Pipeline/VersionContext.cs
index 715efb4..0a2e40f 100644
--- a/src/SimpleVersion.Core/Pipeline/VersionContext.cs
+++ b/src/SimpleVersion.Core/Pipeline/VersionContext.cs
@@ -36,11 +36,13 @@ public VersionContext(Git.IRepository repository)
private VersionResult SetIntialResult()
{
+ var sha = Repository.Head.Tip?.Sha;
return new VersionResult
{
BranchName = Repository.Head.FriendlyName,
CanonicalBranchName = Repository.Head.CanonicalName,
- Sha = Repository.Head.Tip?.Sha
+ Sha = sha,
+ Sha7 = (string.IsNullOrEmpty(sha) || sha.Length < 7) ? null : sha.Substring(0, 7)
};
}
}
diff --git a/src/SimpleVersion.Core/Rules/ShortShaTokenRule.cs b/src/SimpleVersion.Core/Rules/ShortShaTokenRule.cs
index 2ec17ac..8e4b28c 100644
--- a/src/SimpleVersion.Core/Rules/ShortShaTokenRule.cs
+++ b/src/SimpleVersion.Core/Rules/ShortShaTokenRule.cs
@@ -27,8 +27,7 @@ public class ShortShaTokenRule : ITokenRule
///
public string Resolve(IVersionContext context, string value)
{
- var shortSha = context.Result.Sha.Substring(0, 7);
- return Regex.Replace(value, Regex.Escape(Token), $"c{shortSha}", RegexOptions.IgnoreCase);
+ return Regex.Replace(value, Regex.Escape(Token), $"c{context.Result.Sha7}", RegexOptions.IgnoreCase);
}
///
diff --git a/test/SimpleVersion.Core.Tests/Pipeline/Formatting/Semver1FormatProcessFixture.cs b/test/SimpleVersion.Core.Tests/Pipeline/Formatting/Semver1FormatProcessFixture.cs
index 7f48e37..bd2dc8c 100644
--- a/test/SimpleVersion.Core.Tests/Pipeline/Formatting/Semver1FormatProcessFixture.cs
+++ b/test/SimpleVersion.Core.Tests/Pipeline/Formatting/Semver1FormatProcessFixture.cs
@@ -49,8 +49,7 @@ public void Apply_LabelParts_NonRelease_Is_Formatted(
};
context.Result.Version = context.Configuration.Version;
- var shaSub = context.Result.Sha.Substring(0, 7);
- var fullExpected = $"{expectedPart}-c{shaSub}";
+ var fullExpected = $"{expectedPart}-c{context.Result.Sha7}";
// Act
_sut.Apply(context);
diff --git a/test/SimpleVersion.Core.Tests/Pipeline/Formatting/Semver2FormatProcessFixture.cs b/test/SimpleVersion.Core.Tests/Pipeline/Formatting/Semver2FormatProcessFixture.cs
index 687fecc..9591e28 100644
--- a/test/SimpleVersion.Core.Tests/Pipeline/Formatting/Semver2FormatProcessFixture.cs
+++ b/test/SimpleVersion.Core.Tests/Pipeline/Formatting/Semver2FormatProcessFixture.cs
@@ -50,8 +50,7 @@ public void Apply_LabelParts_NonRelease_Is_Formatted(
context.Result.Version = context.Configuration.Version;
var divider = parts.Length > 0 ? '.' : '-';
- var shaSub = context.Result.Sha.Substring(0, 7);
- var fullExpected = $"{expectedPart}{divider}c{shaSub}";
+ var fullExpected = $"{expectedPart}{divider}c{context.Result.Sha7}";
// Act
_sut.Apply(context);
@@ -112,13 +111,12 @@ public void Apply_MetadataParts_NonRelease_Is_Formatted(
Result = Utils.GetVersionResult(height, false)
};
context.Result.Version = context.Configuration.Version;
- var shaSub = context.Result.Sha.Substring(0, 7);
string expected;
if (parts.Length > 0)
- expected = $"{version}-c{shaSub}+{string.Join(".", parts)}";
+ expected = $"{version}-c{context.Result.Sha7}+{string.Join(".", parts)}";
else
- expected = $"{version}-c{shaSub}";
+ expected = $"{version}-c{context.Result.Sha7}";
// Act
_sut.Apply(context);
diff --git a/test/SimpleVersion.Core.Tests/Pipeline/VersionContextFixture.cs b/test/SimpleVersion.Core.Tests/Pipeline/VersionContextFixture.cs
index 892690f..705ad8e 100644
--- a/test/SimpleVersion.Core.Tests/Pipeline/VersionContextFixture.cs
+++ b/test/SimpleVersion.Core.Tests/Pipeline/VersionContextFixture.cs
@@ -35,6 +35,7 @@ public void Ctor_WithEmptyRepo_SetsProperties()
sut.Result.BranchName.Should().Be(fixture.Repository.Head.FriendlyName);
sut.Result.CanonicalBranchName.Should().Be(fixture.Repository.Head.CanonicalName);
sut.Result.Sha.Should().BeNull();
+ sut.Result.Sha7.Should().BeNull();
}
}
@@ -56,6 +57,7 @@ public void Ctor_WithCommittedRepo_SetsProperties()
sut.Result.BranchName.Should().Be(fixture.Repository.Head.FriendlyName);
sut.Result.CanonicalBranchName.Should().Be(fixture.Repository.Head.CanonicalName);
sut.Result.Sha.Should().Be(fixture.Repository.Head.Tip.Sha);
+ sut.Result.Sha7.Should().Be(fixture.Repository.Head.Tip.Sha.Substring(0, 7));
}
}
}
diff --git a/test/SimpleVersion.Core.Tests/Rules/ShortShaTokenRuleFixture.cs b/test/SimpleVersion.Core.Tests/Rules/ShortShaTokenRuleFixture.cs
index f551bb1..2492dbb 100644
--- a/test/SimpleVersion.Core.Tests/Rules/ShortShaTokenRuleFixture.cs
+++ b/test/SimpleVersion.Core.Tests/Rules/ShortShaTokenRuleFixture.cs
@@ -44,6 +44,26 @@ public void Resolve_ReplacesToken_IfNeeded(string input, string expected)
// Act
var result = _sut.Resolve(context, input);
+ // Assert
+ result.Should().Be(expected);
+ }
+ }
+
+ [Theory]
+ [InlineData("this-{shortsha}", "this-c4ca82d2")]
+ public void Resolve_ReplacesToken2_IfNeeded(string input, string expected)
+ {
+ // Arrange
+ using (var fixture = new EmptyRepositoryFixture())
+ {
+ var context = new VersionContext(fixture.Repository)
+ {
+ Result = Utils.GetVersionResult(10)
+ };
+
+ // Act
+ var result = _sut.Resolve(context, input);
+
// Assert
result.Should().Be(expected);
}
diff --git a/test/SimpleVersion.Core.Tests/Utils.cs b/test/SimpleVersion.Core.Tests/Utils.cs
index 7cca9c7..3738150 100644
--- a/test/SimpleVersion.Core.Tests/Utils.cs
+++ b/test/SimpleVersion.Core.Tests/Utils.cs
@@ -44,6 +44,7 @@ public static VersionResult GetVersionResult(int height, bool release = true)
BranchName = branchName,
CanonicalBranchName = "refs/heads/" + branchName,
Sha = "4ca82d2c58f48007bf16d69ebf036fc4ebfdd059",
+ Sha7 = "4ca82d2",
Height = height
};
}