Skip to content

Commit f1a5a2f

Browse files
committed
fix(server): a refused checkout is handed back only when the branch is off the head
1 parent 0a86822 commit f1a5a2f

2 files changed

Lines changed: 61 additions & 12 deletions

File tree

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

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5033,20 +5033,27 @@ it.layer(GitManagerTestLayer)("GitManager", (it) => {
50335033
);
50345034

50355035
it.effect(
5036-
"hands back a clean PR head after provider refusal when fetching its head succeeds",
5036+
"passes up provider refusal on the PR branch when its head is already checked out",
50375037
() =>
50385038
Effect.gen(function* () {
5039+
const providerFailure = new Error("provider refused checkout");
50395040
const { repoDir, headBranch, c1, checkout } = yield* makeLocalPullRequestFixture({
50405041
prCheckout: () => {
5041-
throw new Error("provider refused checkout");
5042+
throw providerFailure;
50425043
},
50435044
});
50445045
yield* runGit(repoDir, ["branch", "--unset-upstream"]);
50455046

5046-
const result = yield* checkout();
5047+
const error = yield* checkout().pipe(Effect.flip);
50475048

5048-
expect(result.branch).toBe(headBranch);
5049-
expect(result.isOnPullRequestHead).toBe(true);
5049+
expect(error).toMatchObject({
5050+
_tag: "SourceControlProviderError",
5051+
operation: "checkoutChangeRequest",
5052+
cause: { _tag: "GitHubCliCommandError", cause: providerFailure },
5053+
});
5054+
expect((yield* runGit(repoDir, ["branch", "--show-current"])).stdout.trim()).toBe(
5055+
headBranch,
5056+
);
50505057
expect((yield* runGit(repoDir, ["rev-parse", "HEAD"])).stdout.trim()).toBe(c1);
50515058
expect(
50525059
(yield* runGit(repoDir, ["rev-parse", "--abbrev-ref", "@{upstream}"])).stdout.trim(),
@@ -5055,6 +5062,33 @@ it.layer(GitManagerTestLayer)("GitManager", (it) => {
50555062
}),
50565063
);
50575064

5065+
it.effect(
5066+
"hands back a clean PR branch behind its head after provider refusal without moving it",
5067+
() =>
5068+
Effect.gen(function* () {
5069+
const { repoDir, headBranch, c1, checkout } = yield* makeLocalPullRequestFixture({
5070+
prCheckout: (cwd) => {
5071+
runGitSyncForFakeGh(cwd, ["checkout", "feature/pr-local-keeps-work"]);
5072+
throw new Error("provider refused checkout");
5073+
},
5074+
});
5075+
const c2 = yield* advanceLocalPullRequestRemote(repoDir);
5076+
yield* runGit(repoDir, ["checkout", "main"]);
5077+
5078+
const result = yield* checkout();
5079+
5080+
expect(result.branch).toBe(headBranch);
5081+
expect(result.worktreePath).toBeNull();
5082+
expect(result.isOnPullRequestHead).toBe(false);
5083+
expect((yield* runGit(repoDir, ["branch", "--show-current"])).stdout.trim()).toBe(
5084+
headBranch,
5085+
);
5086+
expect((yield* runGit(repoDir, ["rev-parse", "HEAD"])).stdout.trim()).toBe(c1);
5087+
expect((yield* runGit(repoDir, ["rev-parse", "@{upstream}"])).stdout.trim()).toBe(c2);
5088+
expect((yield* runGit(repoDir, ["status", "--porcelain"])).stdout).toBe("");
5089+
}),
5090+
);
5091+
50585092
it.effect("preserves an ignored file that a local PR fast-forward would overwrite", () =>
50595093
Effect.gen(function* () {
50605094
const repoDir = yield* makeTempDir("t3code-git-manager-");

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

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2392,12 +2392,9 @@ export const make = Effect.gen(function* () {
23922392
.pipe(
23932393
Effect.as(null),
23942394
// A refused fast-forward (local commits, or edits in the way) can fail the checkout
2395-
// after it already switched to the branch. That checkout is the pull request's, just
2396-
// not at its head, so it is handed back and the refresh reports it — provided the head
2397-
// can be fetched below, which tells a refusal from a network or authentication
2398-
// failure. Only for a same-repository head: a fork's head branch may share its name
2399-
// with the user's own branch (a fork PR from `main`), which must keep its tracking
2400-
// config untouched.
2395+
// after it already switched to the branch; it is classified below. Only for a
2396+
// same-repository head: a fork's head branch may share its name with the user's own
2397+
// branch (a fork PR from `main`), which must keep its tracking config untouched.
24012398
Effect.catch((error) =>
24022399
Effect.gen(function* () {
24032400
const details = yield* gitCore.statusDetails(input.cwd);
@@ -2421,13 +2418,31 @@ export const make = Effect.gen(function* () {
24212418
details.branch ?? pullRequest.headBranch,
24222419
);
24232420
if (refusedCheckout !== null) {
2424-
if (head === null) {
2421+
// Classified by state, and nothing is moved: a refusal leaves the branch off the
2422+
// fetched head. On the head, or with no head fetched (network, authentication), the
2423+
// provider had no such reason to fail, so its error stands.
2424+
const offHead =
2425+
head !== null &&
2426+
(yield* Effect.all([
2427+
gitCore.resolveCommit({ cwd: input.cwd, revision: head.ref }),
2428+
gitCore.resolveCommit({ cwd: input.cwd, revision: "HEAD" }),
2429+
]).pipe(
2430+
Effect.map(([target, current]) => target.commitSha !== current.commitSha),
2431+
Effect.orElseSucceed(() => false),
2432+
));
2433+
if (!offHead) {
24252434
return yield* refusedCheckout;
24262435
}
24272436
yield* Effect.logWarning(
24282437
"GitManager.preparePullRequestThread checkout stopped on the pull request branch",
24292438
{ cwd: input.cwd, headBranch: pullRequest.headBranch, cause: refusedCheckout },
24302439
);
2440+
return {
2441+
pullRequest,
2442+
branch: details.branch ?? pullRequest.headBranch,
2443+
worktreePath: null,
2444+
isOnPullRequestHead: false,
2445+
};
24312446
}
24322447
// The head comes only from the branch just fetched for the pull request, never from
24332448
// `refs/pull/<n>/head` on the primary remote: when `origin` is the user's fork, its pull

0 commit comments

Comments
 (0)