[ISSUE]: Mainline: version on main falls back to 0.0.x when a branch named in a merge message still exists

Aperta
#5,405 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

@arturcic ci sta già lavorando.

Dal 9/10/2026.

  • #5295 di @arturcic — aperta

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
35/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Ferma
Stack tecnologico
csharp, git
Ambito
backend, devtools

Direzione di ricerca

Start by reviewing the Mainline strategy log and the branch source and merge-base lookup for main and part1; the issue includes a bash reproduction script that creates the problematic history. Run it with GitVersion.Tool 6.8.2, then trace how the existing part1 branch affects version selection. Done means the same version is calculated on main whether part1 exists or has been deleted.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

bug
Prerequisites
  • I have written a descriptive issue title
  • I have searched issues to ensure it has not already been reported
GitVersion package

GitVersion.Tool, GitVersion.MsBuild

GitVersion version

6.8.2

Operating system

Windows

What are you seeing?

With the Mainline strategy, the version calculated on main depends on
whether a branch that is only named in a merge message on main still
exists. If it exists, the tag on main is ignored and the version is counted
from 0.0.0. If the branch is deleted, the same commit gets the expected
version.

In the minimal repro below, the same commit on main gets:

  • 1.0.0 while branch part1 exists. The base version is 0.0.2-1, and the
    result looks like it is then raised to the tag.
  • 1.0.7 after git branch -D part1.

The history is the usual "update a stacked feature branch, then complete the
pull request" flow:

  1. part2 is branched off part1.
  2. main is merged into part1.
  3. part1 is merged into part2 ("Merge branch 'part1' into part2").
  4. part2 is merged into main.

The merge message names part1. The log shows that GitVersion then looks for
the source of part1 and takes the merge base with main to be Part 1, not
the tip of part1. It ends up with CommitOnTrunk: Force version increment '0.0.2-1'.

In our real repository (Azure DevOps, PR merges with "Merged PR n: …"
messages, completed branches kept), every build on master produced
11.1.0 instead of 11.1.3xx after upgrading from GitVersion 5.12 to 6.8.2.
That was caused by one such kept branch. There is no warning and the build
succeeds, so it publishes packages with a wrong, lower version. On CI this
is easy to hit, because the checkout fetches all remote branches and
GitVersion creates local branches for them.

What is expected?

The version on main should not depend on whether a branch named in a
merge message still exists. The tag 1.0.0 on main should always be the
version source, so the result is the same with and without part1.

Workaround: setting track-merge-message: false on the main branch gives
the same version (1.0.9 in the repro) whether or not part1 exists.

Steps to Reproduce

Run this script with GitVersion.Tool 6.8.2 (dotnet tool install -g GitVersion.Tool --version 6.8.2). It works in bash on Linux and Git Bash on
Windows; set GV to the path of dotnet-gitversion if it is not on PATH.

#!/usr/bin/env bash
# GitVersion 6.8.2, Mainline strategy: the version on main changes when a
# branch named in a merge message on main still exists.
set -e
GV=${GV:-dotnet-gitversion}
rm -rf repro && mkdir repro && cd repro
git init -q -b main
git config user.name test; git config user.email [email protected]
cat > GitVersion.yml <<'YML'
workflow: GitFlow/v1
strategies:
- Mainline
branches:
  main:
    mode: ContinuousDeployment
    increment: Patch
YML
git add GitVersion.yml; git commit -q -m "Initial commit"
git tag 1.0.0
pr() { # a short-lived branch with one commit, merged into main with --no-ff
  git checkout -q -b "pr$1" main; git commit -q --allow-empty -m "Change $1"
  git checkout -q main; git merge -q --no-ff "pr$1" -m "Merged PR $1"; git branch -q -D "pr$1"; }

pr 1
git checkout -q -b part1 main;  git commit -q --allow-empty -m "Part 1"
git checkout -q -b part2 part1; git commit -q --allow-empty -m "Part 2"   # part2 builds on part1
git checkout -q main; pr 2
git checkout -q part1; git merge -q --no-ff main  -m "Merge branch 'main' into part1"
git checkout -q part2; git merge -q --no-ff part1 -m "Merge branch 'part1' into part2"
git checkout -q main;  git merge -q --no-ff part2 -m "Merged PR 3"; git branch -q -D part2
pr 4

ver() { "$GV" "$(pwd -W 2>/dev/null || pwd)" /nocache | grep '"FullSemVer"' | tr -d ' ",'; }
echo "branch part1 exists:  $(ver)"
git branch -q -D part1
echo "branch part1 deleted: $(ver)"

Output:

branch part1 exists:  FullSemVer:1.0.0
branch part1 deleted: FullSemVer:1.0.7

Resulting history:

*   Merged PR 4 (HEAD -> main)
|\
| * Change 4
|/
*   Merged PR 3
|\
| *   Merge branch 'part1' into part2
| |\
| | *   Merge branch 'main' into part1 (part1)
| | |\
| |_|/
|/| |
* | |   Merged PR 2
|\ \ \
| * | | Change 2
|/ / /
| * / Part 2
| |/
| * Part 1
|/
*   Merged PR 1
|\
| * Change 1
|/
* Initial commit (tag: 1.0.0)

Each step is needed. The bug does not show when:

  • the message of the part1 → part2 merge does not name part1,
  • main is not merged into part1 first, or
  • the branch named in the message is deleted.
RepositoryFixture Test

No response

Output log or link to your CI build (if appropriate).
Mainline part of the log with branch `part1` present (`/verbosity Diagnostic`,
DEBUG lines removed):


INFO Running against branch: main ('0e121dd' - Merged PR 4)
INFO -< Begin: Fetching the base versions for version calculation... >-
  INFO -< Begin: Calculating base versions for 'main' >-
    INFO -< Begin: [Using 'MainlineVersionStrategy' strategy] >-
      INFO -< Begin: Getting tagged semantic versions on branch 'refs/heads/main'. TagPrefix: [vV]? and Format: Strict >-
      INFO -< End: Getting tagged semantic versions on branch 'refs/heads/main'. TagPrefix: [vV]? and Format: Strict (Took: 8.39ms) >-
      INFO -< Begin: Finding branches source of 'main' >-
      INFO -< End: Finding branches source of 'main' (Took: 17.55ms) >-
      INFO -< Begin: Finding branches source of 'part1' >-
        INFO -< Begin: Finding merge base between 'part1' and 'main'. >-
          INFO Found merge base of ''12eaa6b' - Merge branch 'main' into part1'
          INFO next merge base --> '13c2268' - Part 1
          INFO Merge base of 'part1' and 'main' is ''13c2268' - Part 1'
        INFO -< End: Finding merge base between 'part1' and 'main'. (Took: 18.85ms) >-
      INFO -< End: Finding branches source of 'part1' (Took: 26.90ms) >-
      INFO CommitOnTrunk: Force version increment '0.0.2-1' +semver 'Patch' with label '' based on commit '723c890'.
    INFO -< End: [Using 'MainlineVersionStrategy' strategy] (Took: 317.22ms) >-
  INFO -< End: Calculating base versions for 'main' (Took: 336.23ms) >-
INFO -< End: Fetching the base versions for version calculation... (Took: 344.75ms) >-
INFO -------------------------------------------------------
INFO Base version used: CommitOnTrunk: Take '0.0.2-1' based on commit '723c890'.


Possibly related: thomhurst/TUnit#7215 reports an `InvalidOperationException`
in the Mainline strategy (GitVersion 6.7.0) when a merge message names
another branch that exists on origin.
Lingua principale
C#
Stelle
3.1k
Fork
661
Merge medio
7h 4m
PR unite (30g)
77

Preparare l'ambiente

Apri in Codespaces

Avvia il container di sviluppo del progetto nel browser, con il tuo account GitHub.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di GitTools/GitVersion

Tutte le issue di GitTools/GitVersion

Issue simili

Altre issue su C#

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.