Skip to content

Fix crash when git branch config has a duplicate key - #511

Merged
dennisdoomen merged 1 commit into
mainfrom
fix-duplicate-git-config-keys
Jul 19, 2026
Merged

Fix crash when git branch config has a duplicate key#511
dennisdoomen merged 1 commit into
mainfrom
fix-duplicate-git-config-keys

Conversation

@dennisdoomen

Copy link
Copy Markdown
Collaborator

Fixes a crash in GitRepository.GetRemoteNameAndBranch when the git branch config section contains a duplicate key.

Problem

VS Code's Git extension appends a vscode-merge-base entry to [branch "..."] instead of updating it in place, sometimes leaving it duplicated. Parsing that section built a Dictionary with ToDictionary, which throws:

System.ArgumentException: An item with the same key has already been added. Key: vscode-merge-base

This breaks GitRepository.FromLocalDirectory, and therefore [GitVersion] parameter injection, for any consumer whose .git/config has this duplicate.

Fix

Group config lines by key and take the last value (matches git's own last-value-wins semantics) instead of ToDictionary. Also skip lines without = instead of crashing with IndexOutOfRangeException.

Testing

Added a regression test that reproduces the exact reported exception against a real temp git repo with a duplicated vscode-merge-base line, and confirmed it fails without the fix and passes with it. Ran the full Fallout.Build.Tests suite (only the pre-existing, unrelated FromDirectoryTest worktree limitation fails, confirmed to fail identically on main).

@dennisdoomen
dennisdoomen requested a review from a team as a code owner July 19, 2026 05:12
@dennisdoomen dennisdoomen added bug Something isn't working target/vCurrent Targets the current version labels Jul 19, 2026
@dennisdoomen
dennisdoomen marked this pull request as draft July 19, 2026 05:14
@dennisdoomen
dennisdoomen marked this pull request as ready for review July 19, 2026 05:27
@dennisdoomen
dennisdoomen force-pushed the fix-duplicate-git-config-keys branch 2 times, most recently from 2f91379 to bd04866 Compare July 19, 2026 05:31
Comment thread tests/Fallout.Build.Specs/GitRepositorySpecs.cs Outdated
Comment thread tests/Fallout.Build.Specs/GitRepositorySpecs.cs Outdated
@dennisdoomen
dennisdoomen force-pushed the fix-duplicate-git-config-keys branch from bd04866 to 403848f Compare July 19, 2026 07:10

@ChrisonSimtian ChrisonSimtian left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

lgtm, see suggestion in your test

Comment thread tests/Fallout.Build.Specs/GitRepositorySpecs.cs Outdated
@ChrisonSimtian ChrisonSimtian added the Can Merge These PRs are just waiting to be merged, nothing more to do and maintainer has approved label Jul 19, 2026
@dennisdoomen
dennisdoomen enabled auto-merge (squash) July 19, 2026 15:12
@dennisdoomen
dennisdoomen force-pushed the fix-duplicate-git-config-keys branch from 65bbe65 to f89dc7b Compare July 19, 2026 15:35
GetRemoteNameAndBranch built a Dictionary from the raw key/value lines of
a [branch "..."] config section with ToDictionary, which throws
ArgumentException on a duplicate key. VS Code's Git extension is known to
append a duplicate vscode-merge-base entry instead of updating it in
place, which crashed FromLocalDirectory (and therefore GitVersionAttribute)
for anyone using it.

Group by key and take the last value instead (matches git's own
last-value-wins semantics), and skip malformed lines without an '='
instead of throwing an IndexOutOfRange.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@dennisdoomen
dennisdoomen force-pushed the fix-duplicate-git-config-keys branch from f89dc7b to 4d19f7d Compare July 19, 2026 15:41
@dennisdoomen
dennisdoomen merged commit caeeacb into main Jul 19, 2026
4 checks passed
@dennisdoomen
dennisdoomen deleted the fix-duplicate-git-config-keys branch July 19, 2026 15:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working Can Merge These PRs are just waiting to be merged, nothing more to do and maintainer has approved target/vCurrent Targets the current version

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants