Skip to content

Conversation

@BCSharp
Copy link

@BCSharp BCSharp commented Dec 15, 2025

Description

Implement proper handling of format specifers "D" and "B".

Related Issue

Resolves #4788

Motivation and Context

I want to be able to format Major, Minor, Patch into one integer so it can be used as Android versionCode.

Format specifier "D" does the job. Format specifier "B" is added, because, why not, it comes for free.

How Has This Been Tested?

Tests have been added to this PR by expanding existing test group GitVersion.Tests.Formatting.NumericFormatterTests.TryFormat_ValidFormats_ReturnsExpectedResult.

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation. — Format "D" is already documented, albeit for GitVersion v5.x. Format "B" is undocumented (documented only on Microsoft page)
  • I have updated the documentation accordingly.
  • I have added tests to cover my changes.
  • All new and existing tests passed. — some existing unrelated test fail

Copilot AI review requested due to automatic review settings December 15, 2025 04:30
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This pull request adds support for Binary (B) and Decimal (D) format specifiers for integer formatting in GitVersion's custom formatting system. The change allows version components to be formatted as binary or zero-padded decimal numbers, complementing the existing Hexadecimal (X) support.

Key changes:

  • Extended NumericFormatter to handle "B" and "D" format specifiers alongside the existing "X" format
  • Added test cases to verify formatting behavior with precision specifiers (b8, d8, x8)
  • Updated documentation to include the new Binary format specifier

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
src/GitVersion.Core/Formatting/NumericFormatter.cs Extended integer formatting logic to handle B (binary), D (decimal), and X (hexadecimal) format specifiers with a unified code path
src/GitVersion.Core.Tests/Formatting/NumericFormatterTests.cs Added test cases for binary, decimal, and hexadecimal formatting with precision specifiers; also removed BOM character from file
docs/input/docs/reference/custom-formatting.md Added documentation for the B (binary) format specifier

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 24 to 26
if ("BDX".Contains(char.ToUpperInvariant(format[0])) && int.TryParse(s, NumberStyles.Integer, cultureInfo, out var hex))
{
result = hex.ToString(format, cultureInfo);
Copy link

Copilot AI Dec 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The variable name 'hex' is misleading as it now handles Binary (B), Decimal (D), and Hexadecimal (X) format specifiers. Consider renaming it to something more generic like 'integer' or 'intValue' to better reflect its purpose across all three format types.

Suggested change
if ("BDX".Contains(char.ToUpperInvariant(format[0])) && int.TryParse(s, NumberStyles.Integer, cultureInfo, out var hex))
{
result = hex.ToString(format, cultureInfo);
if ("BDX".Contains(char.ToUpperInvariant(format[0])) && int.TryParse(s, NumberStyles.Integer, cultureInfo, out var intValue))
{
result = intValue.ToString(format, cultureInfo);

Copilot uses AI. Check for mistakes.
@sonarqubecloud
Copy link

// Hexadecimal formatting
if (format.StartsWith("X", StringComparison.OrdinalIgnoreCase) && int.TryParse(s, NumberStyles.Integer, cultureInfo, out var hex))
// Integer formatting with precision specifier
if ("BDX".Contains(char.ToUpperInvariant(format[0])) && int.TryParse(s, NumberStyles.Integer, cultureInfo, out var n))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you mind changing the variable from n to numericValue, and to make it consistent, please do the same for other similar variables in the method

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[ISSUE]: Formatting does not support format specifier "D"

2 participants