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

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

fix(server): Commit, push & PR no longer pushes the default branch into itself (#33695)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

Julius Marminge committed Nov 10, 2026 at 00:18 UTC ad91636f0b48fe41119c888ebe937acb5167f0ee
4 files changed +329 -75
apps/server/src/git/GitManager.test.ts
+187
@@ -3503,6 +3503,125 @@ it.layer(layerGitManagerTest)("GitManager", (it) => {
3503 }),
3504 );
3505
3506 + it.effect.each([
3507 + { localBranch: "develop" },
3508 + // Pushes to origin's develop under a name that differs from the base.
3509 + { localBranch: "origin/develop" },
3510 + ] as const)(
3511 + "commit_push_pr refuses to commit or push on the default branch when origin/HEAD is missing ($localBranch)",
3512 + ({ localBranch }) =>
3513 + Effect.gen(function* () {
3514 + const repoDir = yield* makeTempDir("t3code-git-manager-");
3515 + yield* initRepo(repoDir);
3516 + yield* runGit(repoDir, ["branch", "-m", "main", "develop"]);
3517 + const remoteDir = yield* createBareRemote();
3518 + yield* runGit(repoDir, ["remote", "add", "origin", remoteDir]);
3519 + yield* runGit(repoDir, ["push", "-u", "origin", "develop"]);
3520 + if (localBranch !== "develop") yield* runGit(repoDir, ["checkout", "-b", localBranch]);
3521 + const readRemoteDevelop = runGit(repoDir, [
3522 + "ls-remote",
3523 + "origin",
3524 + "refs/heads/develop",
3525 + ]).pipe(Effect.map((output) => output.stdout));
3526 + const remoteBefore = yield* readRemoteDevelop;
3527 + const localBefore = (yield* runGit(repoDir, ["rev-parse", "HEAD"])).stdout;
3528 + NodeFS.writeFileSync(NodePath.join(repoDir, "change.txt"), "change\n");
3529 +
3530 + const { manager } = yield* makeManager({
3531 + providerScenario: { defaultBranch: "develop", prListSequence: [[], []] },
3532 + });
3533 +
3534 + const error = yield* runStackedAction(manager, {
3535 + cwd: repoDir,
3536 + action: "commit_push_pr",
3537 + }).pipe(Effect.flip);
3538 +
3539 + expect(yield* readRemoteDevelop).toBe(remoteBefore);
3540 + expect((yield* runGit(repoDir, ["rev-parse", "HEAD"])).stdout).toBe(localBefore);
3541 + expect(error.message).toContain("develop is the base branch");
3542 + }),
3543 + );
3544 +
3545 + it.effect("commit_push_pr on the default branch returns its open PR into another base", () =>
3546 + Effect.gen(function* () {
3547 + const repoDir = yield* makeTempDir("t3code-git-manager-");
3548 + yield* initRepo(repoDir);
3549 + const remoteDir = yield* createBareRemote();
3550 + yield* runGit(repoDir, ["remote", "add", "origin", remoteDir]);
3551 + yield* runGit(repoDir, ["push", "-u", "origin", "main"]);
3552 + const releasePr = {
3553 + number: 43,
3554 + title: "Release",
3555 + url: "https://github.com/pingdotgg/codething-mvp/pull/43",
3556 + baseRefName: "release",
3557 + headRefName: "main",
3558 + };
3559 +
3560 + const { manager } = yield* makeManager({
3561 + providerScenario: { defaultBranch: "main", prListSequence: [[releasePr], [releasePr]] },
3562 + });
3563 + const result = yield* runStackedAction(manager, {
3564 + cwd: repoDir,
3565 + action: "commit_push_pr",
3566 + });
3567 +
3568 + expect(result.pr.status).toBe("opened_existing");
3569 + expect(result.pr.number).toBe(43);
3570 + }),
3571 + );
3572 +
3573 + it.effect(
3574 + "commit_push_pr publishes a branch that tracks the default branch under its own name",
3575 + () =>
3576 + Effect.gen(function* () {
3577 + const repoDir = yield* makeTempDir("t3code-git-manager-");
3578 + yield* initRepo(repoDir);
3579 + yield* runGit(repoDir, ["branch", "-m", "main", "develop"]);
3580 + const remoteDir = yield* createBareRemote();
3581 + yield* runGit(repoDir, ["remote", "add", "origin", remoteDir]);
3582 + yield* runGit(repoDir, ["push", "-u", "origin", "develop"]);
3583 + yield* runGit(repoDir, ["checkout", "-b", "t3code/feature", "--track", "origin/develop"]);
3584 + const readRemoteDevelop = runGit(repoDir, [
3585 + "ls-remote",
3586 + "origin",
3587 + "refs/heads/develop",
3588 + ]).pipe(Effect.map((output) => output.stdout));
3589 + const remoteBefore = yield* readRemoteDevelop;
3590 + NodeFS.writeFileSync(NodePath.join(repoDir, "change.txt"), "change\n");
3591 +
3592 + const { manager, providerCalls } = yield* makeManager({
3593 + providerScenario: {
3594 + defaultBranch: "develop",
3595 + prListSequence: [
3596 + [],
3597 + [
3598 + {
3599 + number: 506,
3600 + title: "Feature",
3601 + url: "https://github.com/pingdotgg/codething-mvp/pull/506",
3602 + baseRefName: "develop",
3603 + headRefName: "t3code/feature",
3604 + },
3605 + ],
3606 + ],
3607 + },
3608 + });
3609 +
3610 + const result = yield* runStackedAction(manager, {
3611 + cwd: repoDir,
3612 + action: "commit_push_pr",
3613 + });
3614 +
3615 + expect(result.pr.status).toBe("created");
3616 + expect(yield* readRemoteDevelop).toBe(remoteBefore);
3617 + expect(
3618 + created(providerCalls).some(
3619 + (call) => call.baseRefName === "develop" && call.headSelector === "t3code/feature",
3620 + ),
3621 + ).toBe(true);
3622 + }),
3623 + );
3624 +
3625 it.effect("create_pr reports a created PR when the lookup after creation fails", () =>
3626 Effect.gen(function* () {
3627 const repoDir = yield* makeTempDir("t3code-git-manager-");
@@ -4355,6 +4474,74 @@ it.layer(layerGitManagerTest)("GitManager", (it) => {
4474 }),
4475 );
4476
4477 + it.effect("commit_push_pr publishes the default branch to the push remote's fork", () =>
4478 + Effect.gen(function* () {
4479 + const repoDir = yield* makeTempDir("t3code-git-manager-");
4480 + yield* initRepo(repoDir);
4481 + const originDir = yield* createBareRemote();
4482 + const forkDir = yield* createBareRemote();
4483 + yield* runGit(repoDir, ["remote", "add", "origin", originDir]);
4484 + yield* runGit(repoDir, ["push", "origin", "main"]);
4485 + yield* runGit(repoDir, ["remote", "add", "fork", forkDir]);
4486 + yield* runGit(repoDir, ["push", "fork", "main"]);
4487 + yield* runGit(repoDir, ["config", "remote.pushDefault", "fork"]);
4488 + yield* configureVisibleRemoteUrlWithLocalRewrite(
4489 + repoDir,
4490 + "origin",
4491 + "git@github.com:pingdotgg/codething-mvp.git",
4492 + originDir,
4493 + );
4494 + yield* configureVisibleRemoteUrlWithLocalRewrite(
4495 + repoDir,
4496 + "fork",
4497 + "git@github.com:octocat/codething-mvp.git",
4498 + forkDir,
4499 + );
4500 + const readOriginMain = runGit(repoDir, ["ls-remote", "origin", "refs/heads/main"]).pipe(
4501 + Effect.map((output) => output.stdout),
4502 + );
4503 + const originBefore = yield* readOriginMain;
4504 + NodeFS.writeFileSync(NodePath.join(repoDir, "change.txt"), "change\n");
4505 +
4506 + const { manager, providerCalls } = yield* makeManager({
4507 + providerScenario: {
4508 + defaultBranch: "main",
4509 + prListSequenceByHeadSelector: {
4510 + "octocat:main": [
4511 + [],
4512 + [
4513 + {
4514 + number: 189,
4515 + title: "Contribute from main",
4516 + url: "https://github.com/pingdotgg/codething-mvp/pull/189",
4517 + baseRefName: "main",
4518 + headRefName: "main",
4519 + state: "open",
4520 + isCrossRepository: true,
4521 + headRepositoryNameWithOwner: "octocat/codething-mvp",
4522 + headRepositoryOwnerLogin: "octocat",
4523 + },
4524 + ],
4525 + ],
4526 + },
4527 + },
4528 + });
4529 +
4530 + const result = yield* runStackedAction(manager, {
4531 + cwd: repoDir,
4532 + action: "commit_push_pr",
4533 + });
4534 +
4535 + expect(result.pr.status).toBe("created");
4536 + expect(yield* readOriginMain).toBe(originBefore);
4537 + expect(
4538 + created(providerCalls).some(
4539 + (call) => call.baseRefName === "main" && call.headSelector === "octocat:main",
4540 + ),
4541 + ).toBe(true);
4542 + }),
4543 + );
4544 +
4545 it.effect("rejects push/pr actions from detached HEAD", () =>
4546 Effect.gen(function* () {
4547 const repoDir = yield* makeTempDir("t3code-git-manager-");
apps/server/src/git/GitManager.ts
+37
@@ -2282,6 +2282,34 @@ export const make = Effect.gen(function* () {
2282 };
2283 });
2284
2285 + const ensurePrHeadIsNotBase = Effect.fn("ensurePrHeadIsNotBase")(function* (
2286 + cwd: string,
2287 + status: { branch: string; upstreamRef: string | null; hasUpstream: boolean },
2288 + changeRequestLabel: string,
2289 + ) {
2290 + const target = yield* gitCore.resolvePushTarget(cwd, status);
2291 + if (!target) return;
2292 + const headContext = yield* resolveBranchHeadContext(cwd, {
2293 + branch: status.branch,
2294 + upstreamRef: `${target.remoteName}/${target.branchName}`,
2295 + remoteName: target.remoteName,
2296 + });
2297 + if (headContext.isCrossRepository) return;
2298 + const { baseBranch } = yield* resolvePrBase(
2299 + cwd,
2300 + status.branch,
2301 + status.upstreamRef,
2302 + headContext,
2303 + );
2304 + if (baseBranch !== target.branchName) return;
2305 + if (yield* findOpenPr(cwd, headContext)) return;
2306 + return yield* new GitManagerError({
2307 + operation: "runStackedAction",
2308 + cwd,
2309 + detail: `${baseBranch} is the base branch. Check out a feature branch to create ${changeRequestLabel}.`,
2310 + });
2311 + });
2312 +
2313 const runPrStep = Effect.fn("runPrStep")(function* (
2314 settings: SourceControlTextGenerationSettings,
2315 cwd: string,
@@ -3251,6 +3279,15 @@ export const make = Effect.gen(function* () {
3279 const currentBranch = branchStep.name ?? initialStatus.branch;
3280 const commitAction = isCommitAction(input.action) ? input.action : null;
3281 const changeRequestTerms = wantsPr ? yield* changeRequestTermsFor(input.cwd) : null;
3282 + // A stale or missing origin/HEAD hides the default branch from status, so check here
3283 + // before anything is committed or pushed to it.
3284 + if (changeRequestTerms && !input.featureBranch && initialStatus.branch) {
3285 + yield* ensurePrHeadIsNotBase(
3286 + input.cwd,
3287 + { ...initialStatus, branch: initialStatus.branch },
3288 + changeRequestTerms.shortLabelWithArticle,
3289 + );
3290 + }
3291
3292 const commit = commitAction
3293 ? yield* Ref.set(currentPhase, Option.some("commit")).pipe(
apps/server/src/vcs/GitVcsDriver.ts
+10
@@ -181,6 +181,11 @@ export interface GitPushResult {
181 setUpstream?: boolean | undefined;
182 }
183
184 +export interface GitPushTarget {
185 + remoteName: string;
186 + branchName: string;
187 +}
188 +
189 export interface GitRangeContext {
190 commitSummary: string;
191 diffSummary: string;
@@ -339,6 +344,11 @@ export class GitVcsDriver extends Context.Service<
344 fallbackBranch: string | null,
345 options?: { readonly remoteName?: string | null },
346 ) => Effect.Effect<GitPushResult, GitCommandError>;
347 + /** Where `pushCurrentBranch` would push the checked-out branch; null with no remote or when Git picks. */
348 + readonly resolvePushTarget: (
349 + cwd: string,
350 + current: { readonly branch: string; readonly hasUpstream: boolean },
351 + ) => Effect.Effect<GitPushTarget | null, GitCommandError>;
352 readonly readRangeContext: (
353 cwd: string,
354 baseRef: string,
apps/server/src/vcs/GitVcsDriverCore.ts
+95 -75
@@ -2663,6 +2663,54 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
2663 ),
2664 );
2665
2666 + // `publishes` is false only when HEAD goes to the head of its current `upstream`.
2667 + const resolvePushDestination = Effect.fn("resolvePushDestination")(function* (
2668 + cwd: string,
2669 + branch: string,
2670 + hasUpstream: boolean,
2671 + ) {
2672 + if (!hasUpstream) {
2673 + const remoteName = yield* resolvePushRemoteName(cwd, branch);
2674 + if (!remoteName) return null;
2675 + const branchName = yield* resolvePublishBranchName(cwd, branch);
2676 + return { remoteName, branchName, publishes: true, upstream: null };
2677 + }
2678 +
2679 + const upstream = yield* resolveCurrentUpstream(cwd).pipe(Effect.orElseSucceed(() => null));
2680 + if (!upstream) return null;
2681 + // Differently named upstreams are bases, not publish targets; pushing HEAD there would write features to shared branches.
2682 + const isAliasOfUpstreamHead =
2683 + branch === upstream.branchName ||
2684 + (branch.endsWith(`/${upstream.branchName}`) && upstream.upstreamRef.endsWith(`/${branch}`));
2685 + if (isAliasOfUpstreamHead) {
2686 + return {
2687 + remoteName: upstream.remoteName,
2688 + branchName: upstream.branchName,
2689 + publishes: false,
2690 + upstream,
2691 + };
2692 + }
2693 + const publishRemoteName = yield* resolvePushRemoteName(cwd, branch).pipe(
2694 + Effect.orElseSucceed(() => null),
2695 + );
2696 + const branchName = yield* resolvePublishBranchName(cwd, branch);
2697 + return {
2698 + remoteName: publishRemoteName ?? upstream.remoteName,
2699 + branchName,
2700 + publishes: true,
2701 + upstream,
2702 + };
2703 + });
2704 +
2705 + const resolvePushTarget: GitVcsDriver.GitVcsDriver["Service"]["resolvePushTarget"] = Effect.fn(
2706 + "resolvePushTarget",
2707 + )(function* (cwd, current) {
2708 + const destination = yield* resolvePushDestination(cwd, current.branch, current.hasUpstream);
2709 + return destination
2710 + ? { remoteName: destination.remoteName, branchName: destination.branchName }
2711 + : null;
2712 + });
2713 +
2714 const pushCurrentBranch: GitVcsDriver.GitVcsDriver["Service"]["pushCurrentBranch"] = Effect.fn(
2715 "pushCurrentBranch",
2716 )(function* (cwd, fallbackBranch, options) {
@@ -2697,30 +2745,27 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
2745 }
2746
2747 const hasNoLocalDelta = details.aheadCount === 0 && details.behindCount === 0;
2700 - if (hasNoLocalDelta) {
2701 - if (details.hasUpstream) {
2702 - return {
2703 - status: "skipped_up_to_date" as const,
2704 - branch,
2705 - ...(details.upstreamRef ? { upstreamBranch: details.upstreamRef } : {}),
2706 - };
2707 - }
2748 + if (hasNoLocalDelta && details.hasUpstream) {
2749 + return {
2750 + status: "skipped_up_to_date" as const,
2751 + branch,
2752 + ...(details.upstreamRef ? { upstreamBranch: details.upstreamRef } : {}),
2753 + };
2754 + }
2755
2756 + const destination = yield* resolvePushDestination(cwd, branch, details.hasUpstream);
2757 + if (hasNoLocalDelta) {
2758 const comparableBaseBranch = yield* resolveBaseBranchForNoUpstream(cwd, branch).pipe(
2759 Effect.orElseSucceed(() => null),
2760 );
2761 if (comparableBaseBranch) {
2713 - const publishRemoteName = yield* resolvePushRemoteName(cwd, branch).pipe(
2714 - Effect.orElseSucceed(() => null),
2715 - );
2716 - if (!publishRemoteName) {
2762 + if (!destination) {
2763 return {
2764 status: "skipped_up_to_date" as const,
2765 branch,
2766 };
2767 }
2768
2723 - const publishBranch = yield* resolvePublishBranchName(cwd, branch);
2769 // The configured diff base can contain HEAD while the publish branch is still behind.
2770 const publishContainsHead = yield* executeGit(
2771 "GitVcsDriver.pushCurrentBranch.publishContainsHead",
@@ -2729,7 +2774,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
2774 "merge-base",
2775 "--is-ancestor",
2776 "HEAD",
2732 - `refs/remotes/${publishRemoteName}/${publishBranch}`,
2777 + `refs/remotes/${destination.remoteName}/${destination.branchName}`,
2778 ],
2779 { allowNonZeroExit: true },
2780 ).pipe(
@@ -2745,87 +2790,61 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
2790 }
2791 }
2792
2748 - if (!details.hasUpstream) {
2749 - const publishRemoteName = yield* resolvePushRemoteName(cwd, branch);
2750 - if (!publishRemoteName) {
2751 - return yield* new GitCommandError({
2752 - ...gitCommandContext({
2753 - operation: "GitVcsDriver.pushCurrentBranch",
2754 - cwd,
2755 - args: ["push"],
2756 - }),
2757 - detail: "Cannot push because no git remote is configured for this repository.",
2793 + if (!details.hasUpstream && !destination) {
2794 + return yield* new GitCommandError({
2795 + ...gitCommandContext({
2796 + operation: "GitVcsDriver.pushCurrentBranch",
2797 + cwd,
2798 + args: ["push"],
2799 + }),
2800 + detail: "Cannot push because no git remote is configured for this repository.",
2801 + });
2802 + }
2803 +
2804 + if (destination?.publishes) {
2805 + const { remoteName, branchName: publishBranch, upstream } = destination;
2806 + // Publishing retargets the upstream, so preserve its comparison base first.
2807 + if (upstream && (yield* readBranchBase(cwd, branch)) === null) {
2808 + yield* writeConfigValue({
2809 + cwd,
2810 + key: branchBaseConfigKey(branch),
2811 + value: upstream.branchName,
2812 });
2813 + if (!(yield* readConfigValue(cwd, `branch.${branch}.t3-merge-base-ref`))) {
2814 + yield* writeConfigValue({
2815 + cwd,
2816 + key: `branch.${branch}.t3-merge-base-ref`,
2817 + value: `refs/remotes/${upstream.upstreamRef}`,
2818 + });
2819 + }
2820 }
2760 - const publishBranch = yield* resolvePublishBranchName(cwd, branch);
2821 yield* runGit(
2762 - "GitVcsDriver.pushCurrentBranch.pushWithUpstream",
2822 + upstream
2823 + ? "GitVcsDriver.pushCurrentBranch.pushOwnBranch"
2824 + : "GitVcsDriver.pushCurrentBranch.pushWithUpstream",
2825 cwd,
2764 - ["push", "--", publishRemoteName, `HEAD:refs/heads/${publishBranch}`],
2826 + ["push", "--", remoteName, `HEAD:refs/heads/${publishBranch}`],
2827 { timeoutMs: null },
2828 );
2829 return {
2830 status: "pushed" as const,
2831 branch,
2770 - upstreamBranch: `${publishRemoteName}/${publishBranch}`,
2771 - setUpstream: yield* recordPushedUpstream(cwd, branch, publishRemoteName, publishBranch),
2832 + upstreamBranch: `${remoteName}/${publishBranch}`,
2833 + setUpstream: yield* recordPushedUpstream(cwd, branch, remoteName, publishBranch),
2834 };
2835 }
2836
2775 - const currentUpstream = yield* resolveCurrentUpstream(cwd).pipe(
2776 - Effect.orElseSucceed(() => null),
2777 - );
2778 - if (currentUpstream) {
2779 - // Differently named upstreams are bases, not publish targets; pushing HEAD there would write features to shared branches.
2780 - const isAliasOfUpstreamHead =
2781 - branch === currentUpstream.branchName ||
2782 - (branch.endsWith(`/${currentUpstream.branchName}`) &&
2783 - currentUpstream.upstreamRef.endsWith(`/${branch}`));
2784 - if (!isAliasOfUpstreamHead) {
2785 - const publishRemoteName = yield* resolvePushRemoteName(cwd, branch).pipe(
2786 - Effect.orElseSucceed(() => null),
2787 - );
2788 - const remoteName = publishRemoteName ?? currentUpstream.remoteName;
2789 - const publishBranch = yield* resolvePublishBranchName(cwd, branch);
2790 - // Publishing retargets the upstream, so preserve its comparison base first.
2791 - if ((yield* readBranchBase(cwd, branch)) === null) {
2792 - yield* writeConfigValue({
2793 - cwd,
2794 - key: branchBaseConfigKey(branch),
2795 - value: currentUpstream.branchName,
2796 - });
2797 - if (!(yield* readConfigValue(cwd, `branch.${branch}.t3-merge-base-ref`))) {
2798 - yield* writeConfigValue({
2799 - cwd,
2800 - key: `branch.${branch}.t3-merge-base-ref`,
2801 - value: `refs/remotes/${currentUpstream.upstreamRef}`,
2802 - });
2803 - }
2804 - }
2805 - yield* runGit(
2806 - "GitVcsDriver.pushCurrentBranch.pushOwnBranch",
2807 - cwd,
2808 - ["push", "--", remoteName, `HEAD:refs/heads/${publishBranch}`],
2809 - { timeoutMs: null },
2810 - );
2811 - return {
2812 - status: "pushed" as const,
2813 - branch,
2814 - upstreamBranch: `${remoteName}/${publishBranch}`,
2815 - setUpstream: yield* recordPushedUpstream(cwd, branch, remoteName, publishBranch),
2816 - };
2817 - }
2818 -
2837 + if (destination?.upstream) {
2838 yield* runGit(
2839 "GitVcsDriver.pushCurrentBranch.pushUpstream",
2840 cwd,
2822 - ["push", "--", currentUpstream.remoteName, `HEAD:refs/heads/${currentUpstream.branchName}`],
2841 + ["push", "--", destination.remoteName, `HEAD:refs/heads/${destination.branchName}`],
2842 { timeoutMs: null },
2843 );
2844 return {
2845 status: "pushed" as const,
2846 branch,
2828 - upstreamBranch: currentUpstream.upstreamRef,
2847 + upstreamBranch: destination.upstream.upstreamRef,
2848 setUpstream: false,
2849 };
2850 }
@@ -5104,6 +5123,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
5123 commit: Effect.fnUntraced(function* (cwd, subject, body, options) {
5124 return yield* withListRefsInvalidation(cwd, commit(cwd, subject, body, options));
5125 }),
5126 + resolvePushTarget,
5127 pushCurrentBranch: Effect.fnUntraced(function* (cwd, fallbackBranch, options) {
5128 return yield* withListRefsInvalidation(cwd, pushCurrentBranch(cwd, fallbackBranch, options));
5129 }),