@devries-cameron20 / t3code-1 / commits / c0ee5a1f5f
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

fix(server): pushing a branch whose remote branch was deleted publishes it again (#29619)

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Julius Marminge <julius0216@outlook.com>

Alex Southwell committed Nov 15, 2026 at 01:51 UTC c0ee5a1f5f2e74d36be41d80d86b416a47fafdea
2 files changed +224 -36
apps/server/src/vcs/GitVcsDriverCore.test.ts
+164 -23
@@ -4659,7 +4659,7 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => {
4659 }),
4660 );
4661
4662 - it.effect("reports remote status on unborn HEAD without failing", () =>
4662 + it.effect("reports status on unborn HEAD without failing or dropping its upstream", () =>
4663 Effect.gen(function* () {
4664 const cwd = yield* makeTmpDir();
4665 const driver = yield* GitVcsDriver.GitVcsDriver;
@@ -4673,6 +4673,17 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => {
4673 assert.equal(status.hasUpstream, false);
4674 assert.equal(status.aheadCount, 0);
4675 assert.equal(status.behindCount, 0);
4676 +
4677 + const seed = yield* makeTmpDir();
4678 + const remote = yield* makeTmpDir("git-remote-");
4679 + yield* initRepoWithCommit(seed);
4680 + yield* git(remote, ["init", "--bare"]);
4681 + yield* git(seed, ["push", remote, `HEAD:refs/heads/${initialBranch}`]);
4682 + yield* git(cwd, ["remote", "add", "origin", remote]);
4683 + yield* git(cwd, ["fetch", "origin"]);
4684 + yield* git(cwd, ["config", `branch.${initialBranch}.remote`, "origin"]);
4685 + yield* git(cwd, ["config", `branch.${initialBranch}.merge`, `refs/heads/${initialBranch}`]);
4686 + assert.equal((yield* driver.statusDetailsLocal(cwd)).hasUpstream, true);
4687 }),
4688 );
4689
@@ -8035,6 +8046,92 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => {
8046 }),
8047 );
8048
8049 + it.effect("republishes a branch whose upstream was deleted", () =>
8050 + Effect.gen(function* () {
8051 + const cwd = yield* makeTmpDir();
8052 + const remote = yield* makeTmpDir("git-remote-");
8053 + yield* initRepoWithCommit(cwd);
8054 + yield* git(remote, ["init", "--bare"]);
8055 + yield* git(cwd, ["remote", "add", "origin", remote]);
8056 + yield* (yield* GitVcsDriver.GitVcsDriver).createRef({
8057 + cwd,
8058 + refName: "feature/push",
8059 + });
8060 + yield* (yield* GitVcsDriver.GitVcsDriver).switchRef({
8061 + cwd,
8062 + refName: "feature/push",
8063 + });
8064 + yield* writeTextFile(cwd, "feature.txt", "feature\n");
8065 + yield* git(cwd, ["add", "-A"]);
8066 + yield* (yield* GitVcsDriver.GitVcsDriver).commit(cwd, "Add feature", "");
8067 +
8068 + yield* (yield* GitVcsDriver.GitVcsDriver).pushCurrentBranch(cwd, null);
8069 +
8070 + yield* git(cwd, ["push", "origin", "--delete", "feature/push"]);
8071 + yield* writeTextFile(cwd, "followup.txt", "followup\n");
8072 + yield* git(cwd, ["add", "-A"]);
8073 + yield* (yield* GitVcsDriver.GitVcsDriver).commit(cwd, "Add followup", "");
8074 + const republished = yield* (yield* GitVcsDriver.GitVcsDriver).pushCurrentBranch(cwd, null);
8075 + assert.deepInclude(republished, { status: "pushed", branch: "feature/push" });
8076 + assert.equal(
8077 + yield* git(remote, ["--git-dir=.", "rev-parse", "refs/heads/feature/push"]),
8078 + yield* git(cwd, ["rev-parse", "HEAD"]),
8079 + );
8080 +
8081 + const fork = yield* makeTmpDir("git-fork-");
8082 + yield* git(fork, ["init", "--bare"]);
8083 + yield* git(cwd, ["remote", "add", "fork", fork]);
8084 + yield* git(cwd, ["remote", "add", "feature", fork]);
8085 + yield* git(cwd, ["push", "-u", "fork", "feature/push"]);
8086 + yield* git(cwd, ["push", "fork", "--delete", "feature/push"]);
8087 + yield* writeTextFile(cwd, "fork.txt", "fork\n");
8088 + yield* git(cwd, ["add", "-A"]);
8089 + yield* (yield* GitVcsDriver.GitVcsDriver).commit(cwd, "Add fork change", "");
8090 + assert.deepInclude(
8091 + yield* (yield* GitVcsDriver.GitVcsDriver).resolvePushTarget(cwd, {
8092 + branch: "feature/push",
8093 + hasUpstream: false,
8094 + }),
8095 + { remoteName: "fork", branchName: "feature/push" },
8096 + );
8097 + const forkRepublished = yield* (yield* GitVcsDriver.GitVcsDriver).pushCurrentBranch(
8098 + cwd,
8099 + null,
8100 + );
8101 + assert.deepInclude(forkRepublished, { upstreamBranch: "fork/feature/push" });
8102 + assert.equal(
8103 + yield* git(fork, ["--git-dir=.", "rev-parse", "refs/heads/feature/push"]),
8104 + yield* git(cwd, ["rev-parse", "HEAD"]),
8105 + );
8106 +
8107 + yield* git(cwd, ["config", "branch.feature/push.remote", "."]);
8108 + yield* git(cwd, ["config", "branch.feature/push.merge", "refs/heads/push"]);
8109 + yield* writeTextFile(cwd, "local.txt", "local\n");
8110 + yield* git(cwd, ["add", "-A"]);
8111 + yield* (yield* GitVcsDriver.GitVcsDriver).commit(cwd, "Add local change", "");
8112 + const localUpstreamGone = yield* (yield* GitVcsDriver.GitVcsDriver).pushCurrentBranch(
8113 + cwd,
8114 + null,
8115 + );
8116 + assert.deepInclude(localUpstreamGone, { upstreamBranch: "origin/feature/push" });
8117 +
8118 + yield* git(cwd, ["push", "fork", "HEAD:refs/heads/release"]);
8119 + yield* git(cwd, ["branch", "-u", "fork/release"]);
8120 + yield* git(cwd, ["push", "fork", "--delete", "release"]);
8121 + yield* writeTextFile(cwd, "release.txt", "release\n");
8122 + yield* git(cwd, ["add", "-A"]);
8123 + yield* (yield* GitVcsDriver.GitVcsDriver).commit(cwd, "Add release change", "");
8124 + yield* (yield* GitVcsDriver.GitVcsDriver).pushCurrentBranch(cwd, null);
8125 + assert.equal(
8126 + yield* (yield* GitVcsDriver.GitVcsDriver).readConfigValue(
8127 + cwd,
8128 + "branch.feature/push.t3-merge-base",
8129 + ),
8130 + "release",
8131 + );
8132 + }),
8133 + );
8134 +
8135 it.effect("rebases a pushed branch's pulls when branch.autoSetupRebase asks for it", () =>
8136 Effect.gen(function* () {
8137 const cwd = yield* makeTmpDir();
@@ -8191,14 +8288,15 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => {
8288 );
8289
8290 it.effect.each([
8194 - { baseBranch: "dev", local: false },
8195 - { baseBranch: "dev", local: true },
8196 - { baseBranch: "feature/base", local: true },
8197 - { baseBranch: "upstream", local: true },
8198 - { baseBranch: "upstream/feature/x", local: true },
8291 + { baseBranch: "dev", local: false, branch: "feature/x", deleted: false },
8292 + { baseBranch: "dev", local: true, branch: "feature/x", deleted: false },
8293 + { baseBranch: "feature/base", local: true, branch: "feature/x", deleted: false },
8294 + { baseBranch: "upstream", local: true, branch: "feature/x", deleted: false },
8295 + { baseBranch: "upstream/feature/x", local: true, branch: "feature/x", deleted: false },
8296 + { baseBranch: "dev", local: false, branch: "origin/dev", deleted: true },
8297 ])(
8200 - "publishes a branch tracking $baseBranch (local=$local) under its own name, not the base",
8201 - ({ baseBranch, local }) =>
8298 + "publishes $branch tracking $baseBranch (local=$local, deleted=$deleted) without updating the base",
8299 + ({ baseBranch, local, branch, deleted }) =>
8300 Effect.gen(function* () {
8301 const cwd = yield* makeTmpDir();
8302 const remote = yield* makeTmpDir("git-remote-");
@@ -8223,51 +8321,68 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => {
8321 "checkout",
8322 "--track",
8323 "-b",
8226 - "feature/x",
8324 + branch,
8325 local ? `refs/heads/${baseBranch}` : `refs/remotes/upstream/${baseBranch}`,
8326 ]);
8327 assert.equal(
8230 - yield* driver.readConfigValue(cwd, "branch.feature/x.remote"),
8328 + yield* driver.readConfigValue(cwd, `branch.${branch}.remote`),
8329 local ? "." : "upstream",
8330 );
8331 yield* writeTextFile(cwd, "feature.txt", "feature\n");
8332 yield* git(cwd, ["add", "-A"]);
8333 yield* driver.commit(cwd, "Add feature", "");
8334 + if (deleted) {
8335 + yield* git(cwd, ["push", "upstream", "--delete", baseBranch]);
8336 + }
8337 + const publishBranch = branch === "origin/dev" ? "dev" : branch;
8338 + assert.deepInclude(
8339 + yield* driver.resolvePushTarget(cwd, { branch, hasUpstream: !deleted }),
8340 + { remoteName: "origin", branchName: publishBranch },
8341 + );
8342
8343 const pushed = yield* driver.pushCurrentBranch(cwd, null);
8344
8345 assert.deepInclude(pushed, {
8346 status: "pushed",
8241 - branch: "feature/x",
8242 - upstreamBranch: "origin/feature/x",
8347 + branch,
8348 + upstreamBranch: `origin/${publishBranch}`,
8349 setUpstream: true,
8350 });
8351 assert.equal(
8246 - yield* git(remote, ["--git-dir=.", "log", "-1", "--pretty=%s", "feature/x"]),
8352 + yield* git(remote, ["--git-dir=.", "log", "-1", "--pretty=%s", publishBranch]),
8353 "Add feature",
8354 );
8355 + if (publishBranch !== baseBranch) {
8356 + assert.equal(
8357 + yield* git(remote, ["--git-dir=.", "rev-parse", `refs/heads/${baseBranch}`]),
8358 + devSha,
8359 + );
8360 + }
8361 assert.equal(
8250 - yield* git(remote, ["--git-dir=.", "rev-parse", `refs/heads/${baseBranch}`]),
8251 - devSha,
8252 - );
8253 - assert.equal(
8254 - yield* git(upstream, ["--git-dir=.", "rev-parse", `refs/heads/${baseBranch}`]),
8255 - upstreamDevSha,
8362 + yield* git(upstream, [
8363 + "--git-dir=.",
8364 + "for-each-ref",
8365 + "--format=%(objectname)",
8366 + `refs/heads/${baseBranch}`,
8367 + ]),
8368 + deleted ? "" : upstreamDevSha,
8369 );
8370 assert.equal(
8258 - yield* git(cwd, ["rev-parse", "--abbrev-ref", "@{upstream}"]),
8259 - "origin/feature/x",
8371 + yield* git(cwd, ["rev-parse", "--symbolic-full-name", "@{upstream}"]),
8372 + `refs/remotes/origin/${publishBranch}`,
8373 );
8374 assert.equal(
8262 - yield* driver.readConfigValue(cwd, "branch.feature/x.t3-merge-base"),
8375 + yield* driver.readConfigValue(cwd, `branch.${branch}.t3-merge-base`),
8376 baseBranch,
8377 );
8378 assert.equal(
8266 - yield* driver.readConfigValue(cwd, "branch.feature/x.t3-merge-base-ref"),
8379 + yield* driver.readConfigValue(cwd, `branch.${branch}.t3-merge-base-ref`),
8380 local ? `refs/heads/${baseBranch}` : `refs/remotes/upstream/${baseBranch}`,
8381 );
8382 assert.equal(yield* git(cwd, ["rev-parse", `refs/heads/${baseBranch}`]), upstreamDevSha);
8383
8384 + if (deleted) return;
8385 +
8386 const preview = yield* driver.getReviewDiffPreview({ cwd });
8387 const changes = preview.sources.find((source) => source.kind === "branch-range")!;
8388 assert.deepStrictEqual(changes.files, [
@@ -8416,6 +8531,32 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => {
8531 yield* git(remote, ["--git-dir=.", "log", "-1", "--pretty=%s", "effect-atom"]),
8532 "Add alias update",
8533 );
8534 +
8535 + yield* git(cwd, ["push", "my-org/upstream", "--delete", "effect-atom"]);
8536 + assert.deepInclude(
8537 + yield* driver.resolvePushTarget(cwd, {
8538 + branch: "upstream/effect-atom",
8539 + hasUpstream: false,
8540 + }),
8541 + { remoteName: "my-org/upstream", branchName: "effect-atom" },
8542 + );
8543 + assert.deepInclude(yield* driver.pushCurrentBranch(cwd, null), {
8544 + upstreamBranch: "my-org/upstream/effect-atom",
8545 + setUpstream: true,
8546 + });
8547 + assert.equal(
8548 + yield* git(remote, ["--git-dir=.", "rev-parse", "refs/heads/effect-atom"]),
8549 + yield* git(cwd, ["rev-parse", "HEAD"]),
8550 + );
8551 + assert.equal(
8552 + yield* git(remote, [
8553 + "--git-dir=.",
8554 + "for-each-ref",
8555 + "--format=%(refname)",
8556 + "refs/heads/upstream/",
8557 + ]),
8558 + "",
8559 + );
8560 }),
8561 );
8562
apps/server/src/vcs/GitVcsDriverCore.ts
+60 -13
@@ -69,6 +69,10 @@ const branchBaseConfigKey = (branch: string) => `branch.${branch}.t3-merge-base`
69 * CLI's key, which branches cut before then still carry instead.
70 */
71 const legacyBranchBaseConfigKey = (branch: string) => `branch.${branch}.gh-merge-base`;
72 +// Differently named upstreams are bases, not publish targets; pushing HEAD there would write features to shared branches.
73 +const isAliasOfUpstreamHead = (branch: string, upstreamBranch: string, upstreamRef: string) =>
74 + branch === upstreamBranch ||
75 + (branch.endsWith(`/${upstreamBranch}`) && upstreamRef.endsWith(`/${branch}`));
76 const gitProcesses = Semaphore.makeUnsafe(8);
77 const backgroundGitProcesses = Semaphore.makeUnsafe(2);
78 // The fetch before `git worktree add` downloads everything the remote gained since
@@ -1857,12 +1861,17 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
1861 const resolvePushRemoteName = Effect.fn("resolvePushRemoteName")(function* (
1862 cwd: string,
1863 refName: string,
1864 + fallbackRemoteName: string | null = null,
1865 ) {
1866 const configuredRemote = yield* resolveConfiguredPushRemoteName(cwd, refName);
1867 if (configuredRemote) {
1868 return configuredRemote;
1869 }
1870
1871 + if (fallbackRemoteName) {
1872 + return fallbackRemoteName;
1873 + }
1874 +
1875 return yield* resolvePrimaryRemoteName(cwd).pipe(Effect.orElseSucceed(() => null));
1876 });
1877
@@ -2362,6 +2371,10 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
2371
2372 let refName: string | null = null;
2373 let upstreamRef: string | null = null;
2374 + // Git omits branch.ab when the upstream ref is gone, e.g. deleted after its PR merged,
2375 + // and also for an unborn HEAD, which can still track a fetched upstream.
2376 + let upstreamRefExists = false;
2377 + let isUnbornHead = false;
2378 let aheadCount = 0;
2379 let behindCount = 0;
2380 let aheadOfDefaultCount = 0;
@@ -2372,6 +2385,10 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
2385 const statusRecords = statusStdout.split("\0");
2386 for (let index = 0; index < statusRecords.length; index++) {
2387 const line = statusRecords[index]!;
2388 + if (line === "# branch.oid (initial)") {
2389 + isUnbornHead = true;
2390 + continue;
2391 + }
2392 if (line.startsWith("# branch.head ")) {
2393 const value = line.slice("# branch.head ".length).trim();
2394 refName = value === "(detached)" ? null : value;
@@ -2382,11 +2399,13 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
2399 upstreamRef = value.length > 0 ? value : null;
2400 continue;
2401 }
2385 - if (includeDivergence && line.startsWith("# branch.ab ")) {
2386 - const value = line.slice("# branch.ab ".length).trim();
2387 - const parsed = parseBranchAb(value);
2388 - aheadCount = parsed.ahead;
2389 - behindCount = parsed.behind;
2402 + if (line.startsWith("# branch.ab ")) {
2403 + upstreamRefExists = true;
2404 + if (includeDivergence) {
2405 + const parsed = parseBranchAb(line.slice("# branch.ab ".length).trim());
2406 + aheadCount = parsed.ahead;
2407 + behindCount = parsed.behind;
2408 + }
2409 continue;
2410 }
2411 if (line.length > 0 && !line.startsWith("#")) {
@@ -2405,6 +2424,8 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
2424 }
2425 }
2426
2427 + if (!upstreamRefExists && !isUnbornHead) upstreamRef = null;
2428 +
2429 const isDefaultBranch =
2430 refName !== null &&
2431 (refName === defaultBranch ||
@@ -2928,10 +2949,40 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
2949 hasUpstream: boolean,
2950 ) {
2951 if (!hasUpstream) {
2931 - const remoteName = yield* resolvePushRemoteName(cwd, branch);
2952 + // A deleted upstream keeps its config: republish to it when it was this branch's own,
2953 + // otherwise it was the base, so keep that before publishing.
2954 + let goneUpstream: { remoteName: string; branchName: string } | null = null;
2955 + let goneBaseUpstream: { remoteName: string; branchName: string; upstreamRef: string } | null =
2956 + null;
2957 + const mergeRef = yield* readConfigValue(cwd, `branch.${branch}.merge`);
2958 + const mergeBranch = mergeRef?.startsWith("refs/heads/")
2959 + ? mergeRef.slice("refs/heads/".length)
2960 + : null;
2961 + const configuredRemoteName = yield* readConfigValue(cwd, `branch.${branch}.remote`);
2962 + if (mergeBranch && configuredRemoteName && configuredRemoteName !== ".") {
2963 + if (isAliasOfUpstreamHead(branch, mergeBranch, `${configuredRemoteName}/${mergeBranch}`)) {
2964 + goneUpstream = { remoteName: configuredRemoteName, branchName: mergeBranch };
2965 + } else {
2966 + goneBaseUpstream = {
2967 + remoteName: configuredRemoteName,
2968 + branchName: mergeBranch,
2969 + upstreamRef: `${configuredRemoteName}/${mergeBranch}`,
2970 + };
2971 + }
2972 + }
2973 + const remoteName = yield* resolvePushRemoteName(cwd, branch, goneUpstream?.remoteName);
2974 if (!remoteName) return null;
2933 - const branchName = yield* resolvePublishBranchName(cwd, branch);
2934 - return { remoteName, branchName, publishes: true, upstream: null, pushRemote: null };
2975 + // A gone local upstream still gets the full local name, as below.
2976 + const branchName =
2977 + goneUpstream?.branchName ??
2978 + (configuredRemoteName === "." ? branch : yield* resolvePublishBranchName(cwd, branch));
2979 + return {
2980 + remoteName,
2981 + branchName,
2982 + publishes: true,
2983 + upstream: goneBaseUpstream,
2984 + pushRemote: null,
2985 + };
2986 }
2987
2988 if ((yield* readConfigValue(cwd, `branch.${branch}.remote`)) === ".") {
@@ -2955,11 +3006,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
3006
3007 const upstream = yield* resolveCurrentUpstream(cwd).pipe(Effect.orElseSucceed(() => null));
3008 if (!upstream) return null;
2958 - // Differently named upstreams are bases, not publish targets; pushing HEAD there would write features to shared branches.
2959 - const isAliasOfUpstreamHead =
2960 - branch === upstream.branchName ||
2961 - (branch.endsWith(`/${upstream.branchName}`) && upstream.upstreamRef.endsWith(`/${branch}`));
2962 - if (isAliasOfUpstreamHead) {
3009 + if (isAliasOfUpstreamHead(branch, upstream.branchName, upstream.upstreamRef)) {
3010 // A fork workflow pushes to the configured push remote and keeps the upstream for pulls, as plain `git push` does.
3011 const configuredPushRemote = yield* resolveConfiguredPushRemoteName(cwd, branch).pipe(
3012 Effect.orElseSucceed(() => null),