Skip to content

Commit adf3151

Browse files
fix(server): restored worktrees find pushed thread work on a non-origin remote (pingdotgg#35769)
1 parent ad1c46b commit adf3151

3 files changed

Lines changed: 29 additions & 6 deletions

File tree

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ export class GitWorkflowService extends Context.Service<
9595
readonly remoteName: string;
9696
readonly refName: string;
9797
}) => Effect.Effect<boolean, GitCommandError>;
98+
readonly resolvePrimaryRemoteName: (cwd: string) => Effect.Effect<string, GitCommandError>;
9899
readonly resolveRemoteTrackingCommit: (input: {
99100
readonly cwd: string;
100101
readonly refName: string;
@@ -450,6 +451,13 @@ export const make = Effect.gen(function* () {
450451
Effect.andThen(git.remoteBranchExists(input)),
451452
);
452453
}),
454+
resolvePrimaryRemoteName: Effect.fn("GitWorkflowService.resolvePrimaryRemoteName")(
455+
function (cwd) {
456+
return ensureGitCommand("GitWorkflowService.resolvePrimaryRemoteName", cwd).pipe(
457+
Effect.andThen(git.resolvePrimaryRemoteName(cwd)),
458+
);
459+
},
460+
),
453461
resolveRemoteTrackingCommit: Effect.fn("GitWorkflowService.resolveRemoteTrackingCommit")(
454462
function (input) {
455463
return ensureGitCommand("GitWorkflowService.resolveRemoteTrackingCommit", input.cwd).pipe(

‎apps/server/src/orchestration-v2/ProviderTurnStartService.test.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1137,6 +1137,12 @@ effectIt.effect(
11371137
upstream: "refs/remotes/origin/feature/restore",
11381138
base: "",
11391139
},
1140+
"pushed-sole-remote": {
1141+
tip: threadTip,
1142+
file: "thread.txt",
1143+
upstream: "refs/remotes/upstream/feature/restore",
1144+
base: "",
1145+
},
11401146
detached: { tip: baseTip, file: null, upstream: null, base: "main" },
11411147
"detached-no-remote": { tip: projectTip, file: "project.txt", upstream: null, base: "" },
11421148
} as const;
@@ -1151,7 +1157,11 @@ effectIt.effect(
11511157
yield* git(workspaceRoot, ["switch", "--detach"]);
11521158
expect(yield* git(workspaceRoot, ["branch", "--show-current"])).toBe("");
11531159
}
1160+
if (scenario === "pushed-sole-remote") {
1161+
yield* git(workspaceRoot, ["remote", "rename", "origin", "upstream"]);
1162+
}
11541163
if (scenario === "detached") {
1164+
yield* git(workspaceRoot, ["remote", "rename", "upstream", "origin"]);
11551165
yield* git(workspaceRoot, ["push", "origin", "--delete", "feature/restore"]);
11561166
}
11571167
if (scenario === "detached-no-remote") {

‎apps/server/src/workspace/restoreMissingWorktree.ts‎

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ export const makeRestoreMissingWorktree = Effect.gen(function* () {
4848

4949
const resolveRecreateBase = Effect.fn("restoreMissingWorktree.resolveRecreateBase")(function* (
5050
cwd: string,
51+
remoteName: string | null,
5152
) {
5253
// Recovery can follow an external checkout, before the status cache expires.
5354
yield* gitWorkflow.invalidateLocalStatus(cwd);
@@ -56,14 +57,15 @@ export const makeRestoreMissingWorktree = Effect.gen(function* () {
5657
const refName = `refs/heads/${currentBranch}`;
5758
return { refName, baseRefName: refName };
5859
}
59-
if (yield* gitWorkflow.hasCommit({ cwd, refName: "refs/remotes/origin/HEAD" })) {
60+
const remoteHead = `refs/remotes/${remoteName}/HEAD`;
61+
if (remoteName !== null && (yield* gitWorkflow.hasCommit({ cwd, refName: remoteHead }))) {
6062
const { commitSha } = yield* gitWorkflow.resolveRemoteTrackingCommit({
6163
cwd,
6264
refName: "HEAD",
63-
fallbackRemoteName: "origin",
65+
fallbackRemoteName: remoteName,
6466
});
6567
// Start from the commit so the thread branch does not track the default branch.
66-
return { refName: commitSha, baseRefName: "refs/remotes/origin/HEAD" };
68+
return { refName: commitSha, baseRefName: remoteHead };
6769
}
6870
if (yield* gitWorkflow.hasCommit({ cwd, refName: "HEAD" })) {
6971
return { refName: "HEAD", baseRefName: undefined };
@@ -83,9 +85,12 @@ export const makeRestoreMissingWorktree = Effect.gen(function* () {
8385
yield* gitWorkflow.createWorktree({ cwd, refName: branch, path: worktreePath });
8486
return;
8587
}
88+
const remoteName = yield* gitWorkflow
89+
.resolvePrimaryRemoteName(cwd)
90+
.pipe(Effect.orElseSucceed(() => null));
8691
// Push keeps the tracking ref current, so recovery reads it without fetching.
87-
const pushedRef = `refs/remotes/origin/${branch}`;
88-
if (yield* gitWorkflow.hasCommit({ cwd, refName: pushedRef })) {
92+
const pushedRef = remoteName === null ? null : `refs/remotes/${remoteName}/${branch}`;
93+
if (pushedRef !== null && (yield* gitWorkflow.hasCommit({ cwd, refName: pushedRef }))) {
8994
yield* gitWorkflow.createWorktree({
9095
cwd,
9196
refName: pushedRef,
@@ -94,7 +99,7 @@ export const makeRestoreMissingWorktree = Effect.gen(function* () {
9499
});
95100
return;
96101
}
97-
const base = yield* resolveRecreateBase(cwd);
102+
const base = yield* resolveRecreateBase(cwd, remoteName);
98103
if (base === null) {
99104
return yield* new WorktreeRecreateNoBaseError({ worktreePath, branch });
100105
}

0 commit comments

Comments
 (0)