fix(server): refreshing a pull request worktree leaves a branch switched in during the fetch alone #47502
MergedStiensWout(GitHub profile, opens in a new tab)merged 7 commits into
main fromrefs/pull/47502/headmerged by juliusmarminge View on GitHubtanstack-source added labelsbug
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:L
- @@ -7197,2 +7225,20 @@7225+ ["update-ref", "refs/t3code/pre-refresh", headCommit],7226+ { fallbackErrorDetail: "git failed to record the previous checkout commit" },7227+ );7228+ }719772297198- return { headCommit: input.targetCommit, moved: true, onTarget: true };7230+ const lockedIndex = { GIT_INDEX_FILE: lockPath };7231+ const checkout = (from: string, to: string) =>7232+ executeGit(7233+ "GitVcsDriver.refreshCheckedOutBranch.move",7234+ input.cwd,7235+ // A two-way merge refuses to overwrite edits racing our cleanliness check.7236+ ["read-tree", "-m", "-u", from, to],7237+ {7238+ env: lockedIndex,7239+ // Killing a slow checkout partway through strands a half-updated working tree.7240+ timeoutMs: null,7241+ fallbackErrorDetail: "git failed to move the checkout onto the pull request head",7242+ },7243+ );7244+ yield* checkout(headCommit, input.targetCommit);
- @@ -7177,0 +7174,21 @@7174+ const lockFailed = (cause: unknown) =>7175+ new GitCommandError({7176+ operation: "GitVcsDriver.refreshCheckedOutBranch.lockIndex",7177+ command: "lock index",7178+ cwd: input.cwd,7179+ detail: "Git index is locked by another command.",7180+ cause,7181+ });7182+ // Git's relative paths use the physical cwd, even when the project uses a symlink.7183+ const indexPath = path.resolve(7184+ yield* fileSystem.realPath(input.cwd).pipe(Effect.mapError(lockFailed)),7185+ yield* runGitStdout("GitVcsDriver.refreshCheckedOutBranch.indexPath", input.cwd, [7186+ "rev-parse",7187+ "--git-path",7188+ "index",7189+ ]).pipe(Effect.map((stdout) => stdout.trim())),7190+ );7191+ const lockPath = `${indexPath}.lock`;7192+ let lockHeld = false;7193+ // Holding index.lock keeps git switch, merge and reset out, so HEAD names the same branch7194+ // from the check below until the index is committed; the move edits that locked index.@@ -7177,0 +7178,21 @@7178+ cwd: input.cwd,7179+ detail: "Git index is locked by another command.",7180+ cause,7181+ });7182+ // Git's relative paths use the physical cwd, even when the project uses a symlink.7183+ const indexPath = path.resolve(7184+ yield* fileSystem.realPath(input.cwd).pipe(Effect.mapError(lockFailed)),7185+ yield* runGitStdout("GitVcsDriver.refreshCheckedOutBranch.indexPath", input.cwd, [7186+ "rev-parse",7187+ "--git-path",7188+ "index",7189+ ]).pipe(Effect.map((stdout) => stdout.trim())),7190+ );7191+ const lockPath = `${indexPath}.lock`;7192+ let lockHeld = false;7193+ // Holding index.lock keeps git switch, merge and reset out, so HEAD names the same branch7194+ // from the check below until the index is committed; the move edits that locked index.7195+ return yield* Effect.gen(function* () {7196+ yield* Effect.try({7197+ try: () => {7198+ NodeFS.copyFileSync(indexPath, lockPath, NodeFS.constants.COPYFILE_EXCL);@@ -7197,2 +7225,20 @@7225+ ["update-ref", "refs/t3code/pre-refresh", headCommit],7226+ { fallbackErrorDetail: "git failed to record the previous checkout commit" },7227+ );7228+ }719772297198- return { headCommit: input.targetCommit, moved: true, onTarget: true };7230+ const lockedIndex = { GIT_INDEX_FILE: lockPath };7231+ const checkout = (from: string, to: string) =>7232+ executeGit(7233+ "GitVcsDriver.refreshCheckedOutBranch.move",7234+ input.cwd,7235+ // A two-way merge refuses to overwrite edits racing our cleanliness check.7236+ ["read-tree", "-m", "-u", from, to],7237+ {7238+ env: lockedIndex,7239+ // Killing a slow checkout partway through strands a half-updated working tree.7240+ timeoutMs: null,7241+ fallbackErrorDetail: "git failed to move the checkout onto the pull request head",7242+ },7243+ );7244+ yield* checkout(headCommit, input.targetCommit);@@ -7198,0 +7244,21 @@7244+ yield* checkout(headCommit, input.targetCommit);7245+ // Moves only the branch HEAD named under the lock, and only from the commit just checked.7246+ yield* executeGit(7247+ "GitVcsDriver.refreshCheckedOutBranch.moveRef",7248+ input.cwd,7249+ [7250+ "update-ref",7251+ "-m",7252+ `refresh: moving to ${input.targetCommit}`,7253+ ...(headRef === null ? ["--no-deref", "HEAD"] : [headRef]),7254+ input.targetCommit,7255+ headCommit,7256+ ],7257+ { fallbackErrorDetail: "git failed to move the branch onto the pull request head" },7258+ ).pipe(Effect.tapError(() => checkout(input.targetCommit, headCommit).pipe(Effect.ignore)));7259+ yield* Effect.try({7260+ try: () => {7261+ NodeFS.renameSync(lockPath, indexPath);7262+ lockHeld = false;7263+ },7264+ catch: lockFailed,
juliusmarminge added labelstakeover:in-progress
Pusheda0d0209
- @@ -4112,3 +4112,4 @@41124112 Effect.flatMap((target) =>41134113 gitCore.refreshCheckedOutBranch({41144114 cwd: worktreePath,4115+ branch: localPullRequestBranch,
Sign in to join this conversation.







macroscopeapp[bot](GitHub profile, opens in a new tab) reviewed