fix(betterer 🐛): don't reinitialize an existing git repo (corrupts shared config in worktrees)#1251
Open
mattmorganpdx wants to merge 2 commits into
Open
Conversation
…ared config in worktrees) `BettererGitΩ._init()` ran `git init` on every run, even though `_findGitRoot()` has already established that we're inside a git repository. Re-initialising is redundant, and in a linked-worktree setup (where `<worktree>/.git` is a gitfile pointing into a shared common git dir) it reinitialises against the *common* git dir and can rewrite shared config — flipping `core.bare` and breaking every worktree on the repo (`fatal: this operation must be run in a work tree`). Guard the init behind `git.checkIsRepo()` so an already-initialised repo is never reinitialised. Also fix the retry loop, whose `i >= retries` guard could never be true and so silently swallowed all `git init` errors. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
| // linked worktree (where `<worktree>/.git` is a gitfile pointing into a shared common git dir) | ||
| // it reinitialises against the *common* git dir and can rewrite shared config, flipping | ||
| // `core.bare` and breaking every worktree on the repo: | ||
| if (await git.checkIsRepo()) { |
Owner
There was a problem hiding this comment.
I think the code is self explanatory here, would you mind removing the comments?
| return; | ||
| } catch (error) { | ||
| if (i >= retries) { | ||
| if (i === retries - 1) { |
| const { paths, cleanup } = await createFixture('worktree', {}); | ||
|
|
||
| const root = paths.cwd; | ||
| const sourcePath = path.posix.join(root, 'source'); |
Owner
There was a problem hiding this comment.
the fixture should have utilities for making paths and writing files etc, which the cleanup will then handle.
| ].join('\n') | ||
| ); | ||
| await fs.writeFile(path.posix.join(sourcePath, 'src', 'index.ts'), `// HACK:`); | ||
| await git(sourcePath, 'init -q'); |
Owner
There was a problem hiding this comment.
Not a huge fan of the direct git calls, is it possible to do this with simple-git which should be available? I understand it's more complex git behaviour so it might just be necessary.
| it('should not reinitialise the repository when run inside a git worktree', async () => { | ||
| const { betterer } = await import('@betterer/betterer'); | ||
|
|
||
| // The fixture directory is our scratch space. Inside it we build the popular "bare repo + |
Owner
There was a problem hiding this comment.
Don't think the comments here are necessary either.
Owner
|
The E2E tests failing is "normal" ftr |
…ls and simple-git Remove the explanatory comments from `_init` and the worktree test, write the test fixtures via `createFixture` utilities (cleanup handles teardown), and drive the git setup through `simple-git` instead of direct `exec` calls. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Author
|
Thanks for the quick review! Addressed in 2269888:
Re-verified it fails against the original |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
BettererGitΩ._init()rangit initon every Betterer run. This PR skips it when we're already inside a git repository, and fixes the retry loop so init errors are actually surfaced.Why
init()calls_findGitRoot()first, which throws unless a.gitalready exists — so by the time_init()runs, the repository is guaranteed to be initialized and thegit initis redundant.It's also harmful in git worktree setups. When Betterer runs inside a linked worktree (
<worktree>/.gitis a gitfile pointing into a shared common git dir), the redundantgit initreinitializes against the common git dir and rewrites shared config — flippingcore.bareand breaking every worktree on the repo.This is reliably reproducible with the popular "bare clone + worktree" layout:
Separately, the retry loop could never rethrow:
so any
git initfailure (e.g. parallel workers colliding on the config lock —error: could not lock config file …/config: File exists) was silently swallowed.Changes
packages/betterer/src/fs/git.ts: guardgit.init()behindgit.checkIsRepo()so an already-initialized repo is never reinitialized; fix the retry condition (i === retries - 1) and return on success.test/worktree.spec.ts: new regression test that builds a bare-repo + linked-worktree layout, runs Betterer inside the worktree, and asserts the shared common-dircore.bareis untouched. It fails against the current code (core.bareflipstrue → false) and passes with this change.Behavior impact
git initis no longer invoked — previously a no-op, now skipped entirely. No functional change to Betterer's results.core.barecan't be flipped.init()still runs (and now correctly throws after exhausting retries instead of swallowing the error). Note that_findGitRoot()already throws before reaching here unless a.gitexists, so this path is effectively unreachable today; the guard is defensive and self-documenting.Note on
v6This targets the
master(5.x) line, where the bug lives. Thev6/nxrewrite already removed this code path —BettererGitΩno longer callsgit initand version control is only created in precommit mode — so there's nothing to port forward. This is purely a 5.x fix.Possibly related: #1129 (same
_findGitRoot/init area).