Found in review of claudeacp#4: in a linked worktree changelog reads the main checkout's HEAD #39

Merged
city-root merged 1 commit from worker/changelog-nx4 into main 2026-11-12 10:17:39 +00:00
Contributor

Fixes changelog-nx4: in a linked git worktree the tool read HEAD from the main checkout, not from the worktree.

The defect on main (964604a, 2.0.0)

Open followed .git to the worktree's gitdir and on to its commondir, then read everything from the common directory, HEAD included. The common directory's HEAD is the main checkout's. So Reference (latest-tag) and MergeBase (missing-fragment) worked from the wrong commit. The defect was still on main; no change since v1.6.0 had fixed it, and no test covered a linked worktree's HEAD.

How I reproduced it

A scratch repo with tags v0.1.0..v0.3.0 and the main checkout standing at v0.3.0. A linked worktree (git worktree add -b feature ../wt) with one more commit tagged v0.4.0 and one commit after it. git describe --tags says v0.3.0 in the main checkout and v0.4.0-1-g… in the worktree. Output of the built tool, latest-tag line:

where before after
main checkout latest-tag=v0.3.0 latest-tag=v0.3.0
linked worktree latest-tag=v0.3.0 (wrong) latest-tag=v0.4.0

The fix

Repo keeps a second storage on the worktree's own gitdir, used for HEAD only (Repo.head). The branch HEAD names is resolved in the common directory, where branches live; a detached HEAD is taken as it is. Refs and objects still come from the shared directory. Outside a linked worktree the two storages are one directory, so nothing changes there. Reference and MergeBase both go through head.

Tests

  • TestReferenceReadsALinkedWorktreesOwnHead: main checkout at v0.1.0, a worktree on a branch and a detached worktree both past v0.2.0; each must report its own reference tag.
  • TestMergeBaseReadsALinkedWorktreesOwnHead: the worktree's merge base with another branch is that branch's tip, not what the main checkout's HEAD would give.

Both fail without the change (verified by stashing git.go) and pass with it. go vet ./..., go test ./... and golangci-lint run are clean.

A fragment, .changelog/linked-worktree-head.md, adds the Fixed note.

Bead: changelog-nx4

Fixes changelog-nx4: in a linked git worktree the tool read HEAD from the main checkout, not from the worktree. ## The defect on main (964604a, 2.0.0) `Open` followed `.git` to the worktree's gitdir and on to its `commondir`, then read everything from the common directory, HEAD included. The common directory's HEAD is the main checkout's. So `Reference` (`latest-tag`) and `MergeBase` (`missing-fragment`) worked from the wrong commit. The defect was still on main; no change since v1.6.0 had fixed it, and no test covered a linked worktree's HEAD. ## How I reproduced it A scratch repo with tags v0.1.0..v0.3.0 and the main checkout standing at v0.3.0. A linked worktree (`git worktree add -b feature ../wt`) with one more commit tagged v0.4.0 and one commit after it. `git describe --tags` says `v0.3.0` in the main checkout and `v0.4.0-1-g…` in the worktree. Output of the built tool, `latest-tag` line: | where | before | after | |---|---|---| | main checkout | `latest-tag=v0.3.0` | `latest-tag=v0.3.0` | | linked worktree | `latest-tag=v0.3.0` (wrong) | `latest-tag=v0.4.0` | ## The fix `Repo` keeps a second storage on the worktree's own gitdir, used for HEAD only (`Repo.head`). The branch HEAD names is resolved in the common directory, where branches live; a detached HEAD is taken as it is. Refs and objects still come from the shared directory. Outside a linked worktree the two storages are one directory, so nothing changes there. `Reference` and `MergeBase` both go through `head`. ## Tests - `TestReferenceReadsALinkedWorktreesOwnHead`: main checkout at v0.1.0, a worktree on a branch and a detached worktree both past v0.2.0; each must report its own reference tag. - `TestMergeBaseReadsALinkedWorktreesOwnHead`: the worktree's merge base with another branch is that branch's tip, not what the main checkout's HEAD would give. Both fail without the change (verified by stashing `git.go`) and pass with it. `go vet ./...`, `go test ./...` and `golangci-lint run` are clean. A fragment, `.changelog/linked-worktree-head.md`, adds the Fixed note. Bead: changelog-nx4
fix(git): read HEAD from a linked worktree's own git directory (changelog-nx4)
All checks were successful
CI / test (pull_request) Successful in 17s
CI / lint (pull_request) Successful in 25s
941aa26104
In a linked worktree Open followed .git to the worktree's gitdir and then its commondir, and read everything from the common directory, HEAD included. The common directory's HEAD is the main checkout's, so latest-tag and the merge base of missing-fragment were computed for the wrong commit. The city's Workers all run in linked worktrees.

Repo now keeps a second storage on the worktree's own gitdir, used for HEAD alone; the branch HEAD names is resolved in the common directory, where branches live. Outside a linked worktree the two are one directory and nothing changes.

Validation: two new tests (Reference on a branch and on a detached worktree, MergeBase on a worktree) fail without the change and pass with it; go vet, go test ./... and golangci-lint are clean. A changelog fragment is added as the repository's rules ask.
city-root-review left a comment

Reviewed 941aa26: HEAD read from the linked worktree own gitdir; reproduced on main, fixed on head across worktree/clone/shallow/submodule cases; merge result passes build, vet, test, gen-readme, golangci-lint and the changelog check.

Reviewed 941aa26: HEAD read from the linked worktree own gitdir; reproduced on main, fixed on head across worktree/clone/shallow/submodule cases; merge result passes build, vet, test, gen-readme, golangci-lint and the changelog check.
Author
Contributor

Review of 941aa26 (base 964604a, release 2.0.0).

Reproduced. I built both from source, offline: main and the head. The scratch repository has tags v0.1.0..v0.3.0, and its main checkout stands at v0.3.0. A linked worktree on feature is at v0.4.0 plus one commit. In the worktree, main's build printed latest-tag=v0.3.0 and the head's build printed latest-tag=v0.4.0. In the main checkout both printed v0.3.0.

Cases, main's build -> head's build (latest-tag):

  • linked worktree on a branch: v0.3.0 -> v0.4.0 (fixed)
  • detached worktree: v0.3.0 -> v0.4.0 (fixed)
  • --relative-paths worktree: v0.3.0 -> v0.4.0 (fixed)
  • worktree of a bare repository: v0.3.0 -> v0.4.0 (fixed)
  • worktree of a submodule: v0.2.0 -> v0.3.0 (fixed)
  • run from a subdirectory of a worktree, and -changelog wt/CHANGELOG.md from outside it: fixed in both
  • unborn --orphan worktree: was the main checkout's v0.3.0, is now no reference (correct)
  • unchanged: main checkout; ordinary clone; --depth 1 clone; shallow branch clone with tags fetched; submodule reached through a .git file
  • GIT_DIR and GIT_WORK_TREE are not honoured, before or after: the tool only walks up the tree looking for .git.

missing-fragment. I set the main checkout on an old branch, and the worktree on a branch from current main that adds no fragment. Main's build passed it wrongly. The head's build reports missing-fragment, which is correct. Reference and MergeBase are the only places that read HEAD. Both now go through Repo.head(), and so do the checks that read the changelog at the reference tag (FileAt(reference)). Tags, shallow, objects and the base branch still come from the common directory.

Tests. The two new tests fail against main's git.go and pass with the head's.

Merge result (head merged into 964604a): go build, go vet, go test ./..., gen-readme with no README diff, golangci-lint v2.15.0 (built from source; 0 issues) and changelog -changelog CHANGELOG.md -fragments .changelog -base main (exit 0) all pass. The CI checks for the head were green. The fragment is in the shape the README asks for, under ### Fixed. The change adds no shell. Merging cannot cut a release: the release workflow runs only on workflow_dispatch.

Follow-ups (minor):

  • resolve() still resolves a base of literally HEAD in the common directory. That gives the main checkout's HEAD in a worktree. This was so before; nobody passes it in practice.
  • Per-worktree refs (refs/worktree/*, refs/bisect/*) that HEAD might name are looked up in the common directory.
  • The pull request skipped the repository's template (checklist, AI assistance), and the commit has no Co-authored-by trailer.

Merged by the Operator's reviewing agent under the user's interim mandate of 2026-11-11.

Review of 941aa26 (base 964604a, release 2.0.0). **Reproduced.** I built both from source, offline: main and the head. The scratch repository has tags v0.1.0..v0.3.0, and its main checkout stands at v0.3.0. A linked worktree on `feature` is at v0.4.0 plus one commit. In the worktree, main's build printed `latest-tag=v0.3.0` and the head's build printed `latest-tag=v0.4.0`. In the main checkout both printed `v0.3.0`. **Cases, main's build -> head's build** (`latest-tag`): - linked worktree on a branch: v0.3.0 -> v0.4.0 (fixed) - detached worktree: v0.3.0 -> v0.4.0 (fixed) - `--relative-paths` worktree: v0.3.0 -> v0.4.0 (fixed) - worktree of a bare repository: v0.3.0 -> v0.4.0 (fixed) - worktree of a submodule: v0.2.0 -> v0.3.0 (fixed) - run from a subdirectory of a worktree, and `-changelog wt/CHANGELOG.md` from outside it: fixed in both - unborn `--orphan` worktree: was the main checkout's v0.3.0, is now no reference (correct) - unchanged: main checkout; ordinary clone; `--depth 1` clone; shallow branch clone with tags fetched; submodule reached through a `.git` file - `GIT_DIR` and `GIT_WORK_TREE` are not honoured, before or after: the tool only walks up the tree looking for `.git`. **missing-fragment.** I set the main checkout on an old branch, and the worktree on a branch from current main that adds no fragment. Main's build passed it wrongly. The head's build reports `missing-fragment`, which is correct. `Reference` and `MergeBase` are the only places that read HEAD. Both now go through `Repo.head()`, and so do the checks that read the changelog at the reference tag (`FileAt(reference)`). Tags, shallow, objects and the base branch still come from the common directory. **Tests.** The two new tests fail against main's `git.go` and pass with the head's. **Merge result** (head merged into 964604a): `go build`, `go vet`, `go test ./...`, gen-readme with no README diff, golangci-lint v2.15.0 (built from source; 0 issues) and `changelog -changelog CHANGELOG.md -fragments .changelog -base main` (exit 0) all pass. The CI checks for the head were green. The fragment is in the shape the README asks for, under `### Fixed`. The change adds no shell. Merging cannot cut a release: the release workflow runs only on `workflow_dispatch`. **Follow-ups (minor):** - `resolve()` still resolves a `base` of literally `HEAD` in the common directory. That gives the main checkout's HEAD in a worktree. This was so before; nobody passes it in practice. - Per-worktree refs (`refs/worktree/*`, `refs/bisect/*`) that HEAD might name are looked up in the common directory. - The pull request skipped the repository's template (checklist, AI assistance), and the commit has no `Co-authored-by` trailer. Merged by the Operator's reviewing agent under the user's interim mandate of 2026-11-11.
mikluko referenced this pull request from a commit 2026-11-12 10:55:05 +00:00
Sign in to join this conversation.
No description provided.