GitVersion icon indicating copy to clipboard operation
GitVersion copied to clipboard

[ISSUE]: branch regex is matched against {EscapedBranchName} not {BranchName}

Open Jaykul opened this issue 1 year ago • 3 comments

Prerequisites

  • [X] I have written a descriptive issue title
  • [X] I have searched issues to ensure it has not already been reported

GitVersion package

GitVersion.Tool

GitVersion version

6.0.1

Operating system

Windows

What are you seeing?

It took me a lot of experimentation to understand why I was getting the text "{BranchName}" in my GitVersion output ... and more confusingly: "--BranchName-" in my informational version numbers ...

It turns out that even though all the default configuration uses [/-] in the regex patterns, the actual value that the regex configuration is being matched against is the {EscapedBranchName} not the {BranchName} (so the / will never match).

What is expected?

I expected the regex to be matched against the branch name, with slashes in it (so I can use dashes WITHIN the path segments without breaking the regex matching). That is, we need to be able to use a branch name like feature/jaykul/new-terminal and have the {BranchName} come out as "new-terminal" not "terminal"

Additionally, I think the documentation needs to call out when variable output is being set by the regex patterns. This was not clear to me until I got the wrong output and started investigating -- the old behavior (5.x) allowed me to override the regex without needing to re-define the capture group.

Steps to Reproduce

Create branches config:

branches:
  feature:
    regex: .*/(?<BranchName>[^/]+)$
    source-branches: ["main", "feature", "release"]

Create a branch named: /feature/yourname/dash-separated-words

Run gitversion, and see {BranchName} everywhere.

RepositoryFixture Test

No response

Output log or link to your CI build (if appropriate).

No response

Jaykul avatar Aug 11 '24 18:08 Jaykul

Yes I agree this behavior is not intentionally. I think the related source code is located in the ConfigurationExtensions class (see Attachment[1]). May I ask you to change it and create a pull-request?

Thank you!

Attachment[1]: image

HHobeck avatar Aug 12 '24 06:08 HHobeck

Has as PR been made for this? I wasn't able to find one and I am running into a similar issue where my regex is:

feature:
     regex: ^feature\/

and I am winding up with the output containing {BranchName} as well:

"BranchName": "feature/net8"
...
"PreReleaseTag": "{BranchName}.1"

Oninaig avatar Oct 02 '24 22:10 Oninaig

@Oninaig - I have a variant of the following. Does this work for you? I do not think a PR has been made yet.

feature:
  regex: ^features?[/-](?<BranchName>.+)$

Hope that helps (or that you already arrived at something similar).

connorjs avatar Oct 14 '24 16:10 connorjs

:tada: This issue has been resolved in version 6.2.0 :tada: The release is available on:

Your GitReleaseManager bot :package::rocket:

gittools-bot avatar Apr 01 '25 18:04 gittools-bot

Has this issue regressed in 6.4.0?

I cannot seem to get GitVersion to substitute the branch name using either {EscapedBranchName} or {BranchName}:

GitVersion.yml:

mode: ContinuousDeployment
increment: Patch
commit-message-incrementing: Enabled
tag-prefix: '[vV]?'
tag-pre-release-weight: 60000
label: '{BranchName}'
branches:
  main:
    regex: ^main$
    increment: None
    prevent-increment:
      of-merged-branch: true
    track-merge-target: false
    track-merge-message: true
    is-main-branch: true
    label: ''
    pre-release-weight: 55000
  develop:
    regex: ^develop$
    increment: Patch
    track-merge-target: false
    track-merge-message: true
    label: 'alpha'
    source-branches:
    - main
    pre-release-weight: 30000
  feature:
    regex: ^features?[/-]
    increment: Patch
    track-merge-message: true
    track-merge-target: false
    label: '{BranchName}'
    source-branches:
    - develop
    - main
    pre-release-weight: 30000
  hotfix:
    regex: ^hotfix(es)?[/-]
    increment: Patch
    track-merge-message: true
    track-merge-target: false
    label: '{BranchName}'
    source-branches:
    - develop
    - main
    pre-release-weight: 30000
ignore:
  sha: []
  paths: []

Example output for branch feature/icons:

{
  "AssemblySemFileVer": "4.0.1.0",
  "AssemblySemVer": "4.0.1.0",
  "BranchName": "feature/icons",
  "BuildMetaData": 4,
  "CommitDate": "2025-10-29",
  "CommitsSinceVersionSource": 4,
  "EscapedBranchName": "feature-icons",
  "FullBuildMetaData": "4.Branch.feature-icons.Sha.dfc7108472e4a0c405156f7f222613f5c4c7ba19",
  "FullSemVer": "4.0.1-{BranchName}.1+4",
  "InformationalVersion": "4.0.1-{BranchName}.1+4.Branch.feature-icons.Sha.dfc7108472e4a0c405156f7f222613f5c4c7ba19",
  "Major": 4,
  "MajorMinorPatch": "4.0.1",
  "Minor": 0,
  "Patch": 1,
  "PreReleaseLabel": "{BranchName}",
  "PreReleaseLabelWithDash": "-{BranchName}",
  "PreReleaseNumber": 1,
  "PreReleaseTag": "{BranchName}.1",
  "PreReleaseTagWithDash": "-{BranchName}.1",
  "SemVer": "4.0.1-{BranchName}.1",
  "Sha": "dfc7108472e4a0c405156f7f222613f5c4c7ba19",
  "ShortSha": "dfc7108",
  "UncommittedChanges": 1,
  "VersionSourceSha": "e0bd69aa5c9e06f06346f479c22f699dc6051841",
  "WeightedPreReleaseNumber": 30001
}

BarnabyDove-Bedrock avatar Oct 29 '25 11:10 BarnabyDove-Bedrock