Skip to content

Commit aa683a1

Browse files
saphidclaude
andcommitted
fix(server): republishing a branch keeps its deleted base upstream as the base
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 35a947d commit aa683a1

2 files changed

Lines changed: 49 additions & 23 deletions

File tree

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5755,6 +5755,21 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => {
57555755
null,
57565756
);
57575757
assert.deepInclude(localUpstreamGone, { upstreamBranch: "origin/push" });
5758+
5759+
yield* git(cwd, ["push", "fork", "HEAD:refs/heads/release"]);
5760+
yield* git(cwd, ["branch", "-u", "fork/release"]);
5761+
yield* git(cwd, ["push", "fork", "--delete", "release"]);
5762+
yield* writeTextFile(cwd, "release.txt", "release\n");
5763+
yield* git(cwd, ["add", "-A"]);
5764+
yield* (yield* GitVcsDriver.GitVcsDriver).commit(cwd, "Add release change", "");
5765+
yield* (yield* GitVcsDriver.GitVcsDriver).pushCurrentBranch(cwd, null);
5766+
assert.equal(
5767+
yield* (yield* GitVcsDriver.GitVcsDriver).readConfigValue(
5768+
cwd,
5769+
"branch.feature/push.t3-merge-base",
5770+
),
5771+
"release",
5772+
);
57585773
}),
57595774
);
57605775

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

Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2635,6 +2635,23 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
26352635
),
26362636
);
26372637

2638+
// Publishing retargets the upstream, so preserve its comparison base first.
2639+
const preserveUpstreamBase = Effect.fn("preserveUpstreamBase")(function* (
2640+
cwd: string,
2641+
branch: string,
2642+
upstream: { readonly branchName: string; readonly upstreamRef: string },
2643+
) {
2644+
if ((yield* readBranchBase(cwd, branch)) !== null) return;
2645+
yield* writeConfigValue({ cwd, key: branchBaseConfigKey(branch), value: upstream.branchName });
2646+
if (!(yield* readConfigValue(cwd, `branch.${branch}.t3-merge-base-ref`))) {
2647+
yield* writeConfigValue({
2648+
cwd,
2649+
key: `branch.${branch}.t3-merge-base-ref`,
2650+
value: `refs/remotes/${upstream.upstreamRef}`,
2651+
});
2652+
}
2653+
});
2654+
26382655
const pushCurrentBranch: GitVcsDriver.GitVcsDriver["Service"]["pushCurrentBranch"] = Effect.fn(
26392656
"pushCurrentBranch",
26402657
)(function* (cwd, fallbackBranch, options) {
@@ -2668,21 +2685,28 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
26682685
};
26692686
}
26702687

2671-
// A deleted upstream keeps its config; republish to its remote when it was this branch's own.
2688+
// A deleted upstream keeps its config: republish to it when it was this branch's own,
2689+
// otherwise it was the base, so keep that before publishing.
26722690
let goneUpstream: { remoteName: string; branchName: string } | null = null;
2691+
let goneBaseUpstream: { branchName: string; upstreamRef: string } | null = null;
26732692
if (!details.hasUpstream) {
26742693
const mergeRef = yield* readConfigValue(cwd, `branch.${branch}.merge`);
26752694
const mergeBranch = mergeRef?.startsWith("refs/heads/")
26762695
? mergeRef.slice("refs/heads/".length)
26772696
: null;
26782697
const remoteName = yield* readConfigValue(cwd, `branch.${branch}.remote`);
2679-
if (
2680-
mergeBranch &&
2681-
remoteName &&
2682-
remoteName !== "." &&
2683-
(mergeBranch === branch || mergeBranch === (yield* resolvePublishBranchName(cwd, branch)))
2684-
) {
2685-
goneUpstream = { remoteName, branchName: mergeBranch };
2698+
if (mergeBranch && remoteName && remoteName !== ".") {
2699+
if (
2700+
mergeBranch === branch ||
2701+
mergeBranch === (yield* resolvePublishBranchName(cwd, branch))
2702+
) {
2703+
goneUpstream = { remoteName, branchName: mergeBranch };
2704+
} else {
2705+
goneBaseUpstream = {
2706+
branchName: mergeBranch,
2707+
upstreamRef: `${remoteName}/${mergeBranch}`,
2708+
};
2709+
}
26862710
}
26872711
}
26882712

@@ -2752,6 +2776,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
27522776
}
27532777
const publishBranch =
27542778
goneUpstream?.branchName ?? (yield* resolvePublishBranchName(cwd, branch));
2779+
if (goneBaseUpstream) yield* preserveUpstreamBase(cwd, branch, goneBaseUpstream);
27552780
yield* runGit(
27562781
"GitVcsDriver.pushCurrentBranch.pushWithUpstream",
27572782
cwd,
@@ -2781,21 +2806,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
27812806
);
27822807
const remoteName = publishRemoteName ?? currentUpstream.remoteName;
27832808
const publishBranch = yield* resolvePublishBranchName(cwd, branch);
2784-
// Publishing retargets the upstream, so preserve its comparison base first.
2785-
if ((yield* readBranchBase(cwd, branch)) === null) {
2786-
yield* writeConfigValue({
2787-
cwd,
2788-
key: branchBaseConfigKey(branch),
2789-
value: currentUpstream.branchName,
2790-
});
2791-
if (!(yield* readConfigValue(cwd, `branch.${branch}.t3-merge-base-ref`))) {
2792-
yield* writeConfigValue({
2793-
cwd,
2794-
key: `branch.${branch}.t3-merge-base-ref`,
2795-
value: `refs/remotes/${currentUpstream.upstreamRef}`,
2796-
});
2797-
}
2798-
}
2809+
yield* preserveUpstreamBase(cwd, branch, currentUpstream);
27992810
yield* runGit(
28002811
"GitVcsDriver.pushCurrentBranch.pushOwnBranch",
28012812
cwd,

0 commit comments

Comments
 (0)