Skip to content

Commit 744e092

Browse files
saphidclaude
andcommitted
fix(server): a republished branch goes back to the remote it tracked
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent b2d1e22 commit 744e092

2 files changed

Lines changed: 39 additions & 4 deletions

File tree

‎apps/server/src/vcs/GitVcsDriverCore.test.ts‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5725,6 +5725,24 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => {
57255725
yield* git(remote, ["--git-dir=.", "rev-parse", "refs/heads/feature/push"]),
57265726
yield* git(cwd, ["rev-parse", "HEAD"]),
57275727
);
5728+
5729+
const fork = yield* makeTmpDir("git-fork-");
5730+
yield* git(fork, ["init", "--bare"]);
5731+
yield* git(cwd, ["remote", "add", "fork", fork]);
5732+
yield* git(cwd, ["push", "-u", "fork", "feature/push"]);
5733+
yield* git(cwd, ["push", "fork", "--delete", "feature/push"]);
5734+
yield* writeTextFile(cwd, "fork.txt", "fork\n");
5735+
yield* git(cwd, ["add", "-A"]);
5736+
yield* (yield* GitVcsDriver.GitVcsDriver).commit(cwd, "Add fork change", "");
5737+
const forkRepublished = yield* (yield* GitVcsDriver.GitVcsDriver).pushCurrentBranch(
5738+
cwd,
5739+
null,
5740+
);
5741+
assert.deepInclude(forkRepublished, { upstreamBranch: "fork/feature/push" });
5742+
assert.equal(
5743+
yield* git(fork, ["--git-dir=.", "rev-parse", "refs/heads/feature/push"]),
5744+
yield* git(cwd, ["rev-parse", "HEAD"]),
5745+
);
57285746
}),
57295747
);
57305748

‎apps/server/src/vcs/GitVcsDriverCore.ts‎

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1732,6 +1732,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
17321732
const resolvePushRemoteName = Effect.fn("resolvePushRemoteName")(function* (
17331733
cwd: string,
17341734
refName: string,
1735+
fallbackRemoteName: string | null = null,
17351736
) {
17361737
const branchPushRemote = yield* runGitStdout(
17371738
"GitVcsDriver.resolvePushRemoteName.branchPushRemote",
@@ -1753,6 +1754,10 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
17531754
return pushDefaultRemote;
17541755
}
17551756

1757+
if (fallbackRemoteName) {
1758+
return fallbackRemoteName;
1759+
}
1760+
17561761
return yield* resolvePrimaryRemoteName(cwd).pipe(Effect.orElseSucceed(() => null));
17571762
});
17581763

@@ -2663,6 +2668,16 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
26632668
};
26642669
}
26652670

2671+
// A deleted upstream keeps its config; republish to its remote when it was this branch's own.
2672+
let goneUpstreamRemoteName: string | null = null;
2673+
if (!details.hasUpstream) {
2674+
const publishBranch = yield* resolvePublishBranchName(cwd, branch);
2675+
const mergeRef = yield* readConfigValue(cwd, `branch.${branch}.merge`);
2676+
if (mergeRef === `refs/heads/${publishBranch}`) {
2677+
goneUpstreamRemoteName = yield* readConfigValue(cwd, `branch.${branch}.remote`);
2678+
}
2679+
}
2680+
26662681
const hasNoLocalDelta = details.aheadCount === 0 && details.behindCount === 0;
26672682
if (hasNoLocalDelta) {
26682683
if (details.hasUpstream) {
@@ -2677,9 +2692,11 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
26772692
Effect.orElseSucceed(() => null),
26782693
);
26792694
if (comparableBaseBranch) {
2680-
const publishRemoteName = yield* resolvePushRemoteName(cwd, branch).pipe(
2681-
Effect.orElseSucceed(() => null),
2682-
);
2695+
const publishRemoteName = yield* resolvePushRemoteName(
2696+
cwd,
2697+
branch,
2698+
goneUpstreamRemoteName,
2699+
).pipe(Effect.orElseSucceed(() => null));
26832700
if (!publishRemoteName) {
26842701
return {
26852702
status: "skipped_up_to_date" as const,
@@ -2713,7 +2730,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
27132730
}
27142731

27152732
if (!details.hasUpstream) {
2716-
const publishRemoteName = yield* resolvePushRemoteName(cwd, branch);
2733+
const publishRemoteName = yield* resolvePushRemoteName(cwd, branch, goneUpstreamRemoteName);
27172734
if (!publishRemoteName) {
27182735
return yield* new GitCommandError({
27192736
...gitCommandContext({

0 commit comments

Comments
 (0)