Skip to content

Commit e3fefcc

Browse files
CopilotAArnott
andcommitted
Fix versionHeightOffset logic to use EffectiveVersionHeightOffset property
Co-authored-by: AArnott <[email protected]>
1 parent 3f33d80 commit e3fefcc

File tree

4 files changed

+36
-31
lines changed

4 files changed

+36
-31
lines changed

src/NerdBank.GitVersioning/LibGit2/LibGit2GitExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ internal static Version GetIdAsVersionHelper(this Commit? commit, VersionOptions
231231
// and forbids 0xffff as a value.
232232
if (versionHeightPosition.HasValue)
233233
{
234-
int adjustedVersionHeight = versionHeight == 0 ? 0 : versionHeight + (versionOptions?.VersionHeightOffset ?? 0);
234+
int adjustedVersionHeight = versionHeight == 0 ? 0 : versionHeight + (versionOptions?.EffectiveVersionHeightOffset ?? 0);
235235
Verify.Operation(adjustedVersionHeight <= MaximumBuildNumberOrRevisionComponent, "Git height is {0}, which is greater than the maximum allowed {0}.", adjustedVersionHeight, MaximumBuildNumberOrRevisionComponent);
236236
switch (versionHeightPosition.Value)
237237
{
@@ -344,7 +344,7 @@ private static bool IsVersionHeightMismatch(Version version, VersionOptions vers
344344
{
345345
int expectedVersionHeight = SemanticVersion.ReadVersionPosition(version, position.Value);
346346

347-
int actualVersionOffset = versionOptions.VersionHeightOffsetOrDefault;
347+
int actualVersionOffset = versionOptions.EffectiveVersionHeightOffset;
348348
int actualVersionHeight = GetCommitHeight(commit, tracker, c => CommitMatchesVersion(c, version, position.Value - 1, tracker));
349349
return expectedVersionHeight != actualVersionHeight + actualVersionOffset;
350350
}

src/NerdBank.GitVersioning/Managed/ManagedGitContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ private Version GetIdAsVersionHelper(VersionOptions? versionOptions, int version
176176
// and forbids 0xffff as a value.
177177
if (versionHeightPosition.HasValue)
178178
{
179-
int adjustedVersionHeight = versionHeight == 0 ? 0 : versionHeight + (versionOptions?.VersionHeightOffset ?? 0);
179+
int adjustedVersionHeight = versionHeight == 0 ? 0 : versionHeight + (versionOptions?.EffectiveVersionHeightOffset ?? 0);
180180
Verify.Operation(adjustedVersionHeight <= MaximumBuildNumberOrRevisionComponent, "Git height is {0}, which is greater than the maximum allowed {0}.", adjustedVersionHeight, MaximumBuildNumberOrRevisionComponent);
181181
switch (versionHeightPosition.Value)
182182
{

src/NerdBank.GitVersioning/VersionOptions.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,38 @@ public int VersionHeightOffsetOrDefault
375375
#pragma warning restore CS0618
376376
}
377377

378+
/// <summary>
379+
/// Gets the effective version height offset, taking into account the <see cref="VersionHeightOffsetAppliesTo"/> property.
380+
/// </summary>
381+
/// <returns>
382+
/// The version height offset if it applies to the current version, or 0 if the version has changed
383+
/// such that the offset should no longer be applied.
384+
/// </returns>
385+
[JsonIgnore]
386+
public int EffectiveVersionHeightOffset
387+
{
388+
get
389+
{
390+
// Check if the offset applies to the current version
391+
if (this.VersionHeightOffsetAppliesTo is object &&
392+
this.Version is object &&
393+
this.VersionHeightPosition.HasValue)
394+
{
395+
// If the version would be reset by a change from VersionHeightOffsetAppliesTo to Version,
396+
// then the offset does not apply.
397+
if (SemanticVersion.WillVersionChangeResetVersionHeight(
398+
this.VersionHeightOffsetAppliesTo,
399+
this.Version,
400+
this.VersionHeightPosition.Value))
401+
{
402+
return 0;
403+
}
404+
}
405+
406+
return this.VersionHeightOffsetOrDefault;
407+
}
408+
}
409+
378410
/// <summary>
379411
/// Gets or sets the version to which the <see cref="VersionHeightOffset"/> applies.
380412
/// When the <see cref="Version"/> property changes such that the version height would be reset,

src/NerdBank.GitVersioning/VersionOracle.cs

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -306,34 +306,7 @@ 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
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-
}
309+
public int VersionHeightOffset => this.VersionOptions?.EffectiveVersionHeightOffset ?? 0;
337310

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

0 commit comments

Comments
 (0)