Skip to content

fix(git): repoDir must be its own worktree root; never adopt an outer repository - #33

Open
nuochan wants to merge 2 commits into
PerryLink:mainfrom
nuochan:fix/repo-toplevel-isolation
Open

nuochan wants to merge 2 commits into
PerryLink:mainfrom
nuochan:fix/repo-toplevel-isolation

Conversation

@nuochan

@nuochan nuochan commented Nov 16, 2026

Copy link
Copy Markdown

Checklist

  • All gates pass locally (pnpm run typecheck && pnpm run typecheck:ci && pnpm test && pnpm run verify:self-contained && pnpm run verify:artifacts && pnpm run check:readmes && pnpm pack)
  • Tests added or updated for the behavior change
  • CHANGELOG.md updated under [Unreleased]
  • All five README language versions updated (README.md is the source)
  • Related issue linked (Fixes #… / Closes #…, if any) — none exists; searched worktree, isRepo, hijack, outer
  • No secrets, tokens, or credentials in any committed file (placeholders only)

Description

GitBackend.isRepo() decides whether ensureRepo() may skip initialization. It used git rev-parse --is-inside-work-tree, which returns true for any subdirectory of any worktree — not just the repository root. When repoDir sits inside an unrelated outer git repository (for example a sync directory placed under a project checkout), isRepo() returned true, initialization was skipped, and every subsequent write landed in the outer repository: ensureRepo() wrote core.autocrlf and the sync commit identity into its config, bootstrap() rewrote its origin remote, and subsequent sync checkpoints committed into its history. Observed live: an unrelated workspace repository had its origin and identity rewritten and gained a stray whole-tree commit, silently redirecting every session's pushes to the wrong remote.

The fix makes isRepo() mean what its callers need: repoDir is itself the root of a git worktree. It compares git rev-parse --show-toplevel against repoDir by realpath identity (both sides normalized; path.resolve fallback when a path does not exist, applied identically to both sides). A repoDir that is merely a subdirectory of an outer repository now takes the init path and gets its own dedicated repository — the outer repository's config, origin, and history stay untouched. A correctly-placed repoDir behaves exactly as before (toplevel equals repoDir).

User-visible effect: placing the sync directory inside an existing checkout no longer hijacks that checkout; the plugin initializes its own mirror repository as designed. No configuration change, no API change.

Verification

Commands run on Windows (Node v24.21.0, git 2.56.0), all at the PR commit:

  • pnpm test — 132 tests, 130 pass, 0 fail, 2 skipped (the two pre-existing Windows symlink-privilege skips). Five new real-git regressions in test/repo-isolation.test.mjs: bootstrap inside an outer worktree leaves the outer origin/identity/HEAD/commit-count untouched and creates the dedicated inner repo; isRepo is true only at a worktree root; a second bootstrap is idempotent and still leaves the outer untouched; a case-variant repoDir path at the true root is recognized (Windows); a directory-linked repoDir resolves to its target root (junction on Windows, symlink on POSIX). The two scripted git.test.mjs bootstrap tests are updated to the new rev-parse --show-toplevel call (sequences otherwise unchanged, assertions strengthened).
  • Mutation check (scratch copy): reverting isRepo() to --is-inside-work-tree fails all three isolation regressions (the hijack reproduces); dropping the realpath normalization fails the two normalization regressions; restoring the fix returns them to green, hash-verified.
  • pnpm run typecheck and pnpm run typecheck:ci — clean.
  • pnpm run lint — 4 warnings, 0 errors (all pre-existing, none in the changed code).
  • pnpm run verify:self-contained, pnpm run verify:artifacts, pnpm run check:readmes, pnpm pack — all pass.

CHANGELOG [Unreleased] and the security-boundaries section of all five READMEs document the dedicated-repository guarantee.

… repository

isRepo() used rev-parse --is-inside-work-tree, which is true for any subdirectory of any worktree. A repoDir placed inside an unrelated outer repository therefore skipped initialization: ensureRepo wrote core.autocrlf and the sync identity into the outer repo, and bootstrap rewrote its origin and committed into its history (observed live: a workspace repo's origin and identity rewritten plus a stray whole-tree commit).

isRepo() now compares rev-parse --show-toplevel against repoDir by realpath identity (path.resolve fallback for non-existent paths, both sides normalized identically). A repoDir that is merely a subdirectory of an outer repo takes the init path and gets its own dedicated repository; the outer repo's config, origin, and history stay untouched. Correctly-placed repoDirs behave exactly as before.

Tests: three real-git regressions (outer-worktree bootstrap isolation, root-only isRepo semantics, idempotency), all mutation-checked (reverting the predicate fails all three); the two scripted bootstrap tests updated to the new rev-parse call. Gates: 130 tests / 128 pass / 0 fail / 2 env-skips; typecheck and typecheck:ci clean; lint 4 pre-existing warnings; self-contained, artifacts, five-language readmes green. CHANGELOG [Unreleased] and all five READMEs updated.
…poDir)

Arm A round-1 MINOR: the raw-string-comparison mutant survived the shipped tests. Two regressions now kill it: a case-variant repoDir at the true root (Windows) and a directory-linked repoDir resolving to its target root (junction on Windows, symlink on POSIX). Suite: 132 tests / 130 pass / 0 fail / 2 env-skips; mutation verified both arms, restore hash-equal.
Copilot AI balanced review requested due to automatic review settings November 16, 2026 00:30

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

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.

2 participants