Skip to content

Commit 02c8e85

Browse files
CopilotAArnott
andcommitted
Add versionHeightOffsetAppliesTo property to VersionOptions
Co-authored-by: AArnott <[email protected]>
1 parent a1960cf commit 02c8e85

File tree

5 files changed

+68
-3
lines changed

5 files changed

+68
-3
lines changed

src/NerdBank.GitVersioning/ReleaseManager.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,8 +282,9 @@ private void UpdateVersion(LibGit2Context context, SemanticVersion oldVersion, S
282282
{
283283
if (versionOptions.VersionHeightOffset != -1 && versionOptions.VersionHeightPosition.HasValue && SemanticVersion.WillVersionChangeResetVersionHeight(versionOptions.Version, newVersion, versionOptions.VersionHeightPosition.Value))
284284
{
285-
// The version will be reset by this change, so remove the version height offset property.
285+
// The version will be reset by this change, so remove the version height offset properties.
286286
versionOptions.VersionHeightOffset = null;
287+
versionOptions.VersionHeightOffsetAppliesTo = null;
287288
}
288289

289290
versionOptions.Version = newVersion;

src/NerdBank.GitVersioning/VersionOptions.cs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,12 @@ public class VersionOptions : IEquatable<VersionOptions>
7373
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
7474
private int? buildNumberOffset;
7575

76+
/// <summary>
77+
/// Backing field for the <see cref="VersionHeightOffsetAppliesTo"/> property.
78+
/// </summary>
79+
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
80+
private SemanticVersion? versionHeightOffsetAppliesTo;
81+
7682
/// <summary>
7783
/// Backing field for the <see cref="SemVer1NumericIdentifierPadding"/> property.
7884
/// </summary>
@@ -146,6 +152,7 @@ public VersionOptions(VersionOptions copyFrom)
146152
this.version = copyFrom.version;
147153
this.assemblyVersion = copyFrom.assemblyVersion is object ? new AssemblyVersionOptions(copyFrom.assemblyVersion) : null;
148154
this.buildNumberOffset = copyFrom.buildNumberOffset;
155+
this.versionHeightOffsetAppliesTo = copyFrom.versionHeightOffsetAppliesTo;
149156
this.semVer1NumericIdentifierPadding = copyFrom.semVer1NumericIdentifierPadding;
150157
this.gitCommitIdShortFixedLength = copyFrom.gitCommitIdShortFixedLength;
151158
this.gitCommitIdShortAutoMinimum = copyFrom.gitCommitIdShortAutoMinimum;
@@ -368,6 +375,25 @@ public int VersionHeightOffsetOrDefault
368375
#pragma warning restore CS0618
369376
}
370377

378+
/// <summary>
379+
/// Gets or sets the version to which the <see cref="VersionHeightOffset"/> applies.
380+
/// When the <see cref="Version"/> property changes such that the version height would be reset,
381+
/// and this property does not match the new version, the <see cref="VersionHeightOffset"/> will be ignored.
382+
/// </summary>
383+
/// <value>A semantic version, or <see langword="null"/> to indicate no constraint.</value>
384+
/// <remarks>
385+
/// This property is typically used in conjunction with <see cref="VersionHeightOffset"/> to ensure
386+
/// that the offset is only applied when the version matches the expected version. When the version
387+
/// changes such that the version height would reset, this property can be used to automatically
388+
/// stop applying the offset without needing to manually remove it from all version.json files.
389+
/// </remarks>
390+
[JsonProperty(DefaultValueHandling = DefaultValueHandling.Ignore)]
391+
public SemanticVersion? VersionHeightOffsetAppliesTo
392+
{
393+
get => this.versionHeightOffsetAppliesTo;
394+
set => this.SetIfNotReadOnly(ref this.versionHeightOffsetAppliesTo, value);
395+
}
396+
371397
/// <summary>
372398
/// Gets or sets the minimum number of digits to use for numeric identifiers in SemVer 1.
373399
/// </summary>
@@ -1667,7 +1693,8 @@ public bool Equals(VersionOptions? x, VersionOptions? y)
16671693
&& NuGetPackageVersionOptions.EqualWithDefaultsComparer.Singleton.Equals(x.NuGetPackageVersionOrDefault, y.NuGetPackageVersionOrDefault)
16681694
&& CloudBuildOptions.EqualWithDefaultsComparer.Singleton.Equals(x.CloudBuildOrDefault, y.CloudBuildOrDefault)
16691695
&& ReleaseOptions.EqualWithDefaultsComparer.Singleton.Equals(x.ReleaseOrDefault, y.ReleaseOrDefault)
1670-
&& x.VersionHeightOffset == y.VersionHeightOffset;
1696+
&& x.VersionHeightOffset == y.VersionHeightOffset
1697+
&& EqualityComparer<SemanticVersion?>.Default.Equals(x.VersionHeightOffsetAppliesTo, y.VersionHeightOffsetAppliesTo);
16711698
}
16721699

16731700
/// <inheritdoc />

src/NerdBank.GitVersioning/VersionOptionsContractResolver.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,11 @@ protected override JsonProperty CreateProperty(MemberInfo member, MemberSerializ
8686
property.ShouldSerialize = instance => ((VersionOptions)instance).VersionHeightOffsetOrDefault != 0;
8787
}
8888

89+
if (property.DeclaringType == typeof(VersionOptions) && member.Name == nameof(VersionOptions.VersionHeightOffsetAppliesTo))
90+
{
91+
property.ShouldSerialize = instance => ((VersionOptions)instance).VersionHeightOffsetAppliesTo is not null;
92+
}
93+
8994
if (property.DeclaringType == typeof(VersionOptions) && member.Name == nameof(VersionOptions.NuGetPackageVersion))
9095
{
9196
property.ShouldSerialize = instance => !((VersionOptions)instance).NuGetPackageVersionOrDefault.IsDefault;

src/NerdBank.GitVersioning/VersionOracle.cs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,34 @@ public string PrereleaseVersion
306306
/// when calculating the integer to use as the <see cref="BuildNumber"/>
307307
/// or elsewhere that the {height} macro is used.
308308
/// </summary>
309-
public int VersionHeightOffset => this.VersionOptions?.VersionHeightOffsetOrDefault ?? 0;
309+
public int VersionHeightOffset
310+
{
311+
get
312+
{
313+
if (this.VersionOptions is null)
314+
{
315+
return 0;
316+
}
317+
318+
// Check if the offset applies to the current version
319+
if (this.VersionOptions.VersionHeightOffsetAppliesTo is object &&
320+
this.VersionOptions.Version is object &&
321+
this.VersionOptions.VersionHeightPosition.HasValue)
322+
{
323+
// If the version would be reset by a change from VersionHeightOffsetAppliesTo to Version,
324+
// then the offset does not apply.
325+
if (SemanticVersion.WillVersionChangeResetVersionHeight(
326+
this.VersionOptions.VersionHeightOffsetAppliesTo,
327+
this.VersionOptions.Version,
328+
this.VersionOptions.VersionHeightPosition.Value))
329+
{
330+
return 0;
331+
}
332+
}
333+
334+
return this.VersionOptions.VersionHeightOffsetOrDefault;
335+
}
336+
}
310337

311338
/// <summary>
312339
/// Gets or sets the ref (branch or tag) being built.

src/NerdBank.GitVersioning/version.schema.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@
6868
"description": "A number to add to the git height when calculating the version height (which typically appears as the 3rd integer in a computed version). May be negative, but not of greater magnitude than the original git height.",
6969
"default": 0
7070
},
71+
"versionHeightOffsetAppliesTo": {
72+
"type": "string",
73+
"description": "The version to which the versionHeightOffset applies. When the version property changes such that the version height would be reset, and this property does not match the new version, the versionHeightOffset will be ignored. This allows the offset to implicitly reset as intended without having to manually remove it from all version.json files.",
74+
"pattern": "^v?(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)(?:\\.(0|[1-9][0-9]*)(?:\\.(0|[1-9][0-9]*))?)?(-(?:[\\da-z\\-]+|\\{height\\})(?:\\.(?:[\\da-z\\-]+|\\{height\\}))*)?(\\+(?:[\\da-z\\-]+|\\{height\\})(?:\\.(?:[\\da-z\\-]+|\\{height\\}))*)?$"
75+
},
7176
"buildNumberOffset": {
7277
"type": "integer",
7378
"description": "OBSOLETE by v3.0. Use \"versionHeightOffset\" instead. A number to add to the git height when calculating the version height (which typically appears as the 3rd integer in a computed version). May be negative, but not of greater magnitude than the original git height.",

0 commit comments

Comments
 (0)