Skip to content

Commit 31567bb

Browse files
fix(server): restored worktrees find pushed thread work on a non-origin remote
Worktree recovery looked for the pushed thread tip only at refs/remotes/origin/<branch>, and its detached fallback only at refs/remotes/origin/HEAD. With a sole remote under another name, it silently recreated the branch from the project branch or HEAD, dropping the pushed commits. Recovery now uses the repository's primary remote (origin when present, else the first remote), as other git flows do. Fixes #35751 Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 7d2d9ab commit 31567bb

2 files changed

Lines changed: 19 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/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)