Skip to content

Commit 21683f2

Browse files
saphidclaude
andcommitted
fix(vcs): fast-forward a local pull request branch the host left behind
Without `force`, Bitbucket, Forgejo and GitCafe check out an existing pull request branch as it was, so a branch behind the pull request stayed there. After the provider checkout, the branch now moves to the provider's head commit through the same clean, fast-forward-only refresh reused worktrees use; diverged or dirty checkouts stay put and report that they are not on the head. The warning now says the checkout could not be confirmed on the latest commit rather than asserting that its code differs, since a provider that reports no head commit also lands there. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 5ba5802 commit 21683f2

4 files changed

Lines changed: 42 additions & 15 deletions

File tree

‎apps/server/src/git/GitManager.test.ts‎

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5451,6 +5451,7 @@ it.layer(layerGitManagerTest)("GitManager", (it) => {
54515451
const headShaField = (headSha: string | undefined) => (headSha === undefined ? {} : { headSha });
54525452
const setUpLocalCheckoutPullRequest = Effect.fnUntraced(function* (
54535453
reportedHead: (pullHead: string) => string | undefined = (pullHead) => pullHead,
5454+
syncCheckoutWithPullHead = true,
54545455
) {
54555456
const repoDir = yield* makeTempDir("t3code-git-manager-");
54565457
yield* initRepo(repoDir);
@@ -5478,7 +5479,7 @@ it.layer(layerGitManagerTest)("GitManager", (it) => {
54785479
state: "open",
54795480
...headShaField(reportedHead(pullHead)),
54805481
},
5481-
syncCheckoutWithPullHead: true,
5482+
syncCheckoutWithPullHead,
54825483
},
54835484
});
54845485
return { repoDir, manager, ghCalls, firstCommit, pullHead };
@@ -5566,6 +5567,29 @@ it.layer(layerGitManagerTest)("GitManager", (it) => {
55665567
}),
55675568
);
55685569

5570+
it.effect("local PR checkout fast-forwards a branch the host's checkout only switched to", () =>
5571+
Effect.gen(function* () {
5572+
// Like Bitbucket or Forgejo without `force`: the existing branch is checked out as it was.
5573+
const { repoDir, manager, firstCommit, pullHead } = yield* setUpLocalCheckoutPullRequest(
5574+
(head) => head,
5575+
false,
5576+
);
5577+
yield* runGit(repoDir, ["checkout", "main"]);
5578+
expect((yield* runGit(repoDir, ["rev-parse", "feature/pr-safe"])).stdout.trim()).toBe(
5579+
firstCommit,
5580+
);
5581+
5582+
const result = yield* preparePullRequestThread(manager, {
5583+
cwd: repoDir,
5584+
reference: "91",
5585+
mode: "local",
5586+
});
5587+
5588+
expect(result).toMatchObject({ branch: "feature/pr-safe", isOnPullRequestHead: true });
5589+
expect((yield* runGit(repoDir, ["rev-parse", "HEAD"])).stdout.trim()).toBe(pullHead);
5590+
}),
5591+
);
5592+
55695593
it.effect(
55705594
"local PR checkout keeps a branch that is ahead and reports it is not on the head",
55715595
() =>

‎apps/server/src/git/GitManager.ts‎

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2691,16 +2691,19 @@ export const make = Effect.gen(function* () {
26912691
}
26922692
});
26932693

2694-
// Whether HEAD is the head commit the provider reported. A provider that reports none, or a
2695-
// commit this checkout does not have, cannot confirm it.
2696-
const isHeadAtCommit = (cwd: string, headSha: string | undefined) =>
2694+
// Brings a local checkout onto the head commit the provider reported and says whether it got
2695+
// there. Unforced, some hosts' checkout only switches to an existing branch (Bitbucket,
2696+
// Forgejo, GitCafe), leaving it behind the pull request, so a clean fast-forward is taken here;
2697+
// nothing is ever reset. A provider that reports no head commit, or one this checkout does not
2698+
// have, cannot confirm it.
2699+
const moveToPullRequestHead = (cwd: string, headSha: string | undefined) =>
26972700
headSha === undefined || !/^[0-9a-f]{7,64}$/i.test(headSha)
26982701
? Effect.succeed(false)
2699-
: Effect.all([
2700-
gitCore.resolveCommit({ cwd, revision: "HEAD" }),
2701-
gitCore.resolveCommit({ cwd, revision: headSha }),
2702-
]).pipe(
2703-
Effect.map(([head, target]) => head.commitSha === target.commitSha),
2702+
: gitCore.resolveCommit({ cwd, revision: headSha }).pipe(
2703+
Effect.flatMap(({ commitSha }) =>
2704+
gitCore.refreshCheckedOutBranch({ cwd, targetCommit: commitSha }),
2705+
),
2706+
Effect.map((refreshed) => refreshed.onTarget),
27042707
Effect.orElseSucceed(() => false),
27052708
);
27062709

@@ -2757,7 +2760,7 @@ export const make = Effect.gen(function* () {
27572760
worktreePath: null,
27582761
// Unforced checkout leaves a branch that is ahead of, or could not be advanced to, the
27592762
// pull request where it is, so only the resulting HEAD says whether this is its head.
2760-
isOnPullRequestHead: yield* isHeadAtCommit(input.cwd, pullRequestSummary.headSha),
2763+
isOnPullRequestHead: yield* moveToPullRequestHead(input.cwd, pullRequestSummary.headSha),
27612764
};
27622765
}
27632766

‎apps/web/src/components/PullRequestThreadDialog.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ describe("pull request worktree permissions", () => {
229229

230230
expect(state.prepared).toEqual([{ branch: "feature/pr", worktreePath: null }]);
231231
expect(state.toasts).toMatchObject([
232-
{ type: "warning", title: "Checked out, but not on the pull request's commit" },
232+
{ type: "warning", title: "Checked out, but not confirmed on the latest commit" },
233233
]);
234234
});
235235

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
/**
2-
* Shown in place of success when a checkout is kept where it was rather than on the pull
3-
* request's head, by both the pull request panel and the branch picker's checkout dialog.
2+
* Shown in place of success when a checkout is not confirmed on the pull request's head, by both
3+
* the pull request panel and the branch picker's checkout dialog.
44
*/
55
export const OFF_HEAD_PULL_REQUEST_CHECKOUT_TOAST = {
66
type: "warning",
7-
title: "Checked out, but not on the pull request's commit",
7+
title: "Checked out, but not confirmed on the latest commit",
88
description:
9-
"The checkout is not on the pull request's latest commit, so the code there differs from the pull request. Uncommitted work or local commits keep it where it is.",
9+
"This checkout could not be confirmed at the pull request's latest commit, so its code may differ. Local commits or uncommitted work keep a checkout where it is.",
1010
} as const;

0 commit comments

Comments
 (0)