Skip to content

fix: resolve linked worktrees via git metadata instead of path names - #302

Merged
TheSnoozer merged 1 commit into
git-commit-id:masterfrom
delanym:fix/worktree-resolution
Dec 4, 2026
Merged

TheSnoozer merged 1 commit into
git-commit-id:masterfrom
delanym:fix/worktree-resolution

Conversation

@delanym

@delanym delanym commented Sep 6, 2026

Copy link
Copy Markdown
Contributor

resolveWorktree recognised a linked worktree by testing whether the resolved gitdir sat below a directory literally named .git, and then returned that main .git directory. Both halves are wrong:

  • Worktrees of a bare repository live in your-repository.git/worktrees/X (or .bare/worktrees/X), which the name test never matched. The administrative directory was then handed to the native git executable as a working directory and every build failed with fatal: this operation must be run in a work tree.

  • Where the test did match, resolution landed on the git directory shared by all worktrees. The native git executable therefore ran in the main checkout and stamped the build with that checkout's branch and commit rather than the ones of the worktree being built (#882).

Git records both locations explicitly in the administrative directory of every linked worktree: commondir points to the shared git directory and gitdir points back to the .git file inside the worktree. Reading those identifies a worktree without guessing from directory names, and works no matter what the repository directory is called.

resolveWorktree now returns the shared git directory from commondir, which keeps jgit on the location it resolved to before, and lookupGitDirectory treats a linked worktree like a submodule for the native git executable: it falls back to the unresolved .git file, whose parent is the working tree that is actually being built.

Since resolution now reads the files git wrote, a gitdir path that does not point at an existing worktree administrative directory is no longer rewritten -- the a/.git/worktrees/b noop cases in the test cover that. Relative gitdir values (worktree.useRelativePaths, git 2.48+) are resolved against the file that holds them, which the previous code only did for submodules.

jgit still reports the shared git directory for a worktree; that is #215 and unchanged here.

Fixes #301

Context

Contributor Checklist

  • Added relevant integration or unit tests to verify the changes
  • Update the Readme or any other documentation (including relevant Javadoc)
  • Ensured that tests pass locally: mvn clean package
  • Ensured that the code meets the current checkstyle coding style definition: mvn clean verify -Pcheckstyle -Dmaven.test.skip=true -B

`resolveWorktree` recognised a linked worktree by testing whether the
resolved gitdir sat below a directory literally named `.git`, and then
returned that main `.git` directory. Both halves are wrong:

* Worktrees of a bare repository live in `your-repository.git/worktrees/X`
  (or `.bare/worktrees/X`), which the name test never matched. The
  administrative directory was then handed to the native git executable as
  a working directory and every build failed with
  `fatal: this operation must be run in a work tree`.

* Where the test did match, resolution landed on the git directory shared
  by all worktrees. The native git executable therefore ran in the main
  checkout and stamped the build with that checkout's branch and commit
  rather than the ones of the worktree being built (#882).

Git records both locations explicitly in the administrative directory of
every linked worktree: `commondir` points to the shared git directory and
`gitdir` points back to the `.git` file inside the worktree. Reading those
identifies a worktree without guessing from directory names, and works no
matter what the repository directory is called.

`resolveWorktree` now returns the shared git directory from `commondir`,
which keeps jgit on the location it resolved to before, and
`lookupGitDirectory` treats a linked worktree like a submodule for the
native git executable: it falls back to the unresolved `.git` file, whose
parent is the working tree that is actually being built.

Since resolution now reads the files git wrote, a gitdir path that does not
point at an existing worktree administrative directory is no longer
rewritten -- the `a/.git/worktrees/b` noop cases in the test cover that.
Relative `gitdir` values (`worktree.useRelativePaths`, git 2.48+) are
resolved against the file that holds them, which the previous code only did
for submodules.

jgit still reports the shared git directory for a worktree; that is git-commit-id#215
and unchanged here.

Fixes git-commit-id#301
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@rupert-madden-abbott

Copy link
Copy Markdown

I can confirm this fixes git-commit-id/git-commit-id-maven-plugin#882 for native git. On master, a build in a linked worktree reports the main checkout's branch and commit, without failing.

Reproducer: https://github.com/rupert-madden-abbott/git-commit-id-maven-plugin-worktree-repro, run with mvn test. It creates a main checkout on main and a linked worktree on branch b, one commit ahead, then runs the revision goal inside the worktree with useNativeGit set to true, then to false.

git-commit-id-plugin-core native git JGit
6.2.1 (maven plugin 10.0.1) main and the main checkout's commit the same
this PR (d2715d5), on 10.0.1 b and the worktree's commit main, as before

Run against this PR with mvn test -Dgit-commit-id-plugin-core.version=<local build of this PR>. On the branch, GitDirLocatorTest and the checkstyle profile pass too.

@TheSnoozer

Copy link
Copy Markdown
Contributor

Apologies for not responding! Thanks @delanym for the MR and the fix!
There is a reason this project is still searching for another maintainer ;-)
Anyhow I'll take a look on the weekend and thanks @rupert-madden-abbott to be kind enough to test and verify :-)
I will add a local test to the project to reproduce the problem and then I guess we can merge & release.

TheSnoozer added a commit to git-commit-id/git-test-resources that referenced this pull request Dec 4, 2026
TheSnoozer added a commit that referenced this pull request Dec 4, 2026
@TheSnoozer
TheSnoozer merged commit df177c8 into git-commit-id:master Dec 4, 2026
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.

resolveWorktree only matches a parent named .git, so worktrees of a bare repository fail

3 participants