Skip to content

fix(git-node): support git worktrees in release --promote - #1270

Open
ychampion wants to merge 1 commit into
nodejs:mainfrom
ychampion:fix/promote-release-worktree
Open

ychampion wants to merge 1 commit into
nodejs:mainfrom
ychampion:fix/promote-release-worktree

Conversation

@ychampion

Copy link
Copy Markdown

Summary

Fixes: #906

When running git node release --promote from a release-line worktree (such as v23.x) while the repository's default branch (main) is already checked out in another worktree, ReleasePromotion.prototype.checkoutDefaultBranch() failed with:

fatal: 'main' is already used by worktree at '...'
Error: git checkout,main failed: 128

Changes

  • lib/promote_release.js:
    • Parse git worktree list --porcelain via parseWorktreeList() to detect whether refs/heads/<defaultBranch> is already checked out in another non-bare, non-prunable worktree (comparing normalized realpaths against git rev-parse --show-toplevel).
    • When the default branch is checked out in another worktree, prompt the user before switching into that worktree (and if that worktree has uncommitted changes per git status --porcelain, warn and prompt with defaultAnswer: false to stash them before proceeding, aborting cleanly if declined).
    • Track the switched worktree and restore both any temporary target-worktree stash and the original working directory (process.chdir + this.dir) in a finally block inside promote() (and on early errors in checkoutDefaultBranch()), so wrapStash() in components/git/release.js always pops the starting worktree's stash in the original directory.
    • Keep existing git checkout <defaultBranch> behavior unchanged when the default branch is not checked out in another worktree or is already checked out in the current worktree.
    • Replace hardcoded existsSync('.git/CHERRY_PICK_HEAD') in cherryPickReleaseCommit() with this.cherryPickInProgress() so linked worktrees (where .git is a file) work properly.
  • lib/session.js:
    • Resolve gitdir: indirection in Session.prototype.gitDir when .git is a linked-worktree file (falling back to <dir>/.git and process.cwd() when this.dir is unset), ensuring cherryPickInProgress(), rebaseInProgress(), and amInProgress() work inside linked worktrees.
  • test/unit/promote_release.test.js:
    • Add unit tests for parseWorktreeList() and end-to-end worktree tests using temporary git init + git worktree add repositories covering switching/cherry-picking/restoring cwd, declining to switch, dirty target worktree decline and stash/restore-on-error, normal checkout when branch is free, no-op when already on default-branch worktree, and gitDir / cherryPickInProgress() in a linked worktree.

Verification

  • node --test test/unit/promote_release.test.js (17/17 passing)
  • node --test test/unit/prepare_release.test.js test/unit/cherry_pick.test.js test/unit/amend.test.js test/unit/run.test.js test/unit/cli.test.js (all passing)
  • npm run lint (eslint . --cache: 0 errors, 0 warnings)

When running `git node release --promote` from a release-line worktree
while the default branch is already checked out in another worktree,
`git checkout <defaultBranch>` fails with exit code 128.

Inspect `git worktree list --porcelain` in `checkoutDefaultBranch()` to
detect when the default branch is checked out in another worktree, ask
the user before switching to it, guard dirty target worktrees with an
explicit stash confirmation, and restore any target stash and original
working directory in `finally`. Also resolve linked-worktree `.git`
files in `Session.prototype.gitDir` and use `cherryPickInProgress()`.

Fixes: nodejs#906
Signed-off-by: ychampion <68075205+ychampion@users.noreply.github.com>
@aduh95

aduh95 commented Dec 5, 2026

Copy link
Copy Markdown
Contributor

What's your use-case for running git node release --promote?

This branch has not been deployed

No deployments
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.

git node release --promote doesn't support worktrees

2 participants