Skip to content

Report AlreadyExisted for a submodule, worktree or separate-git-dir target [patch] - #182

Merged
matt-edmondson merged 2 commits into
mainfrom
fix/138-init-probe-gitfile
Oct 6, 2026
Merged

matt-edmondson merged 2 commits into
mainfrom
fix/138-init-probe-gitfile

Conversation

@matt-edmondson

Copy link
Copy Markdown
Contributor

Fixes #138

What changed

GitInitBuilder.ProbeAsync decided that a repository already existed only when rev-parse --git-dir printed .git or .. It treated an absolute path as meaning "the repository is in an ancestor directory". git also prints an absolute path for a repository rooted at the target whose .git is a file: a submodule, a linked worktree, or a --separate-git-dir repository. git re-initializes those repositories and ignores --initial-branch, but AlreadyExisted came back false.

The probe now runs rev-parse --is-bare-repository --git-dir --show-cdup, and the new IsRepositoryRoot reads its output:

  • --git-dir is .: the target is a git directory itself (a bare repository or a .git directory). The answer is yes, as before.
  • Otherwise, if the repository is bare: the target is a subdirectory of the bare repository. The answer is no.
  • Otherwise, the answer is yes only when --show-cdup is empty, meaning the target is the root of the working tree. A subdirectory prints ../ and so on.

This approach compares no paths, so symlinked temp directories and case differences on Windows/macOS can't cause a mismatch. A note in CLAUDE.md now describes the new probe.

Tests

  • Unit tests: the scripted probe outputs now match the new command. A new test covers a .git file at the target, and a table test runs IsRepositoryRoot against every layout.
  • Integration tests against real git: AlreadyExisted is true for a --separate-git-dir repository and for a git worktree add worktree, and still false for a plain subdirectory of a repository.

Results with git 2.43:

  • With the old .git/. rule restored, the 4 new positive tests fail. With the fix, they pass.
  • Full suite: 744 of 744 passed.

🤖 Generated with Claude Code

https://claude.ai/code/session_01W83XxSnDX5Fu4sVPDKjXem


Generated by Claude Code

…arget [patch]

Init's probe treated any absolute --git-dir answer as "a repository above
the target", but git prints one too when the target's .git is a file
pointing elsewhere. git re-initializes those repositories and ignores
--initial-branch, while AlreadyExisted said a fresh one was created. Ask
rev-parse for --is-bare-repository and --show-cdup as well, and treat an
empty cdup (the working-tree root) as the repository being at the target.

Fixes #138

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01W83XxSnDX5Fu4sVPDKjXem
Comment thread GitIntegration.Test/Integration/GitRoundTripTests.cs Fixed
Comment thread GitIntegration.Test/Integration/GitRoundTripTests.cs Fixed
Comment thread GitIntegration.Test/Integration/GitRoundTripTests.cs Fixed
Comment thread GitIntegration.Test/Integration/GitRoundTripTests.cs Fixed
Path.Combine drops earlier arguments when a later one is rooted; Path.Join
never does.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01W83XxSnDX5Fu4sVPDKjXem
@sonarqubecloud

sonarqubecloud Bot commented Oct 6, 2026

Copy link
Copy Markdown

@matt-edmondson
matt-edmondson merged commit dd0e667 into main Oct 6, 2026
16 checks passed
@matt-edmondson
matt-edmondson deleted the fix/138-init-probe-gitfile branch October 6, 2026 22:40
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.

Init() reports AlreadyExisted = false when re-initializing a submodule, worktree or --separate-git-dir repository, hiding an ignored --initial-branch

2 participants