@devries-cameron20 / t3code Mirror · read-only / commits / d0a494fc0b
Fork 0

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

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 16:00 UTC

fix(server): honor the configured push remote on tracked branches (#24047)

Co-authored-by: maria-rcks <maria@kuuro.net> Co-authored-by: Julius Marminge <julius0216@outlook.com>

Victor committed Nov 14, 2026 at 02:57 UTC d0a494fc0bfc61ef1b9a2d38bb715450b1679a96
5 files changed +303 -36
apps/server/src/git/GitManager.test.ts
+51
@@ -4759,6 +4759,57 @@ it.layer(layerGitManagerTest)("GitManager", (it) => {
4759 }),
4760 );
4761
4762 + it.effect(
4763 + "create_pr publishes to the push remote when the tracked upstream already has HEAD",
4764 + () =>
4765 + Effect.gen(function* () {
4766 + const repoDir = yield* makeTempDir("t3code-git-manager-");
4767 + yield* initRepo(repoDir);
4768 + const originDir = yield* createBareRemote();
4769 + const forkDir = yield* createBareRemote();
4770 + yield* runGit(repoDir, ["remote", "add", "origin", originDir]);
4771 + yield* runGit(repoDir, ["remote", "add", "fork", forkDir]);
4772 + yield* runGit(repoDir, ["checkout", "-b", "topic"]);
4773 + NodeFS.writeFileSync(NodePath.join(repoDir, "changes.txt"), "change\n");
4774 + yield* runGit(repoDir, ["add", "changes.txt"]);
4775 + yield* runGit(repoDir, ["commit", "-m", "Topic change"]);
4776 + yield* runGit(repoDir, ["push", "-u", "origin", "topic"]);
4777 + yield* configureVisibleRemoteUrlWithLocalRewrite(
4778 + repoDir,
4779 + "origin",
4780 + "git@github.com:pingdotgg/codething-mvp.git",
4781 + originDir,
4782 + );
4783 + yield* configureVisibleRemoteUrlWithLocalRewrite(
4784 + repoDir,
4785 + "fork",
4786 + "git@github.com:octocat/codething-mvp.git",
4787 + forkDir,
4788 + );
4789 + yield* runGit(repoDir, ["config", "remote.pushDefault", "fork"]);
4790 +
4791 + const { manager, providerCalls } = yield* makeManager();
4792 + const result = yield* runStackedAction(manager, {
4793 + cwd: repoDir,
4794 + action: "create_pr",
4795 + });
4796 +
4797 + expect(result.push).toMatchObject({ status: "pushed", upstreamBranch: "fork/topic" });
4798 + expect(result.pr.status).toBe("created");
4799 + expect(
4800 + yield* runGit(forkDir, ["--git-dir=.", "log", "-1", "--pretty=%s", "topic"]),
4801 + ).toMatchObject({
4802 + stdout: "Topic change\n",
4803 + });
4804 + expect(created(providerCalls)).toEqual([
4805 + { baseRefName: "main", headSelector: "octocat:topic" },
4806 + ]);
4807 + expect(yield* runGit(repoDir, ["rev-parse", "--abbrev-ref", "@{upstream}"])).toMatchObject({
4808 + stdout: "origin/topic\n",
4809 + });
4810 + }),
4811 + );
4812 +
4813 it.effect("rejects push/pr actions from detached HEAD", () =>
4814 Effect.gen(function* () {
4815 const repoDir = yield* makeTempDir("t3code-git-manager-");
apps/server/src/git/GitManager.ts
+25 -5
@@ -2376,6 +2376,7 @@ export const make = Effect.gen(function* () {
2376 fallbackBranch: string | null,
2377 threadId: ThreadId | undefined,
2378 emit: GitActionProgressEmitter,
2379 + pushRemoteRef?: { readonly remoteName: string; readonly upstreamRef: string },
2380 ) {
2381 const provider = yield* sourceControlProvider(cwd);
2382 const terms = sourceControlDefinitions.get(provider.kind).changeRequest;
@@ -2396,10 +2397,12 @@ export const make = Effect.gen(function* () {
2397 });
2398 }
2399
2399 - const headContext = yield* resolveBranchHeadContext(cwd, {
2400 - branch,
2401 - upstreamRef: details.upstreamRef,
2402 - });
2400 + // A configured push remote receives the commits while the branch keeps
2401 + // tracking its upstream, so the PR head is the push remote's branch.
2402 + const headContext = yield* resolveBranchHeadContext(
2403 + cwd,
2404 + pushRemoteRef ? { branch, ...pushRemoteRef } : { branch, upstreamRef: details.upstreamRef },
2405 + );
2406
2407 const existing = yield* findOpenPr(cwd, headContext);
2408 if (existing) {
@@ -3412,12 +3415,26 @@ export const make = Effect.gen(function* () {
3415 > {
3416 const initialStatus = yield* gitCore.statusDetails(input.cwd);
3417 const wantsCommit = isCommitAction(input.action);
3418 + const upToDateBranch =
3419 + input.action === "create_pr" &&
3420 + initialStatus.hasUpstream &&
3421 + initialStatus.aheadCount === 0
3422 + ? initialStatus.branch
3423 + : null;
3424 + // A current upstream says nothing about a separate push remote, which may lack HEAD.
3425 + const pushTarget = upToDateBranch
3426 + ? yield* gitCore
3427 + .resolvePushTarget(input.cwd, { branch: upToDateBranch, hasUpstream: true })
3428 + .pipe(Effect.orElseSucceed(() => null))
3429 + : null;
3430 const wantsPush =
3431 input.action === "push" ||
3432 input.action === "commit_push" ||
3433 input.action === "commit_push_pr" ||
3434 (input.action === "create_pr" &&
3420 - (!initialStatus.hasUpstream || initialStatus.aheadCount > 0));
3435 + (!initialStatus.hasUpstream ||
3436 + initialStatus.aheadCount > 0 ||
3437 + pushTarget?.viaPushRemote === true));
3438 const wantsPr = input.action === "create_pr" || input.action === "commit_push_pr";
3439
3440 if (input.featureBranch && !wantsCommit) {
@@ -3570,6 +3587,9 @@ export const make = Effect.gen(function* () {
3587 currentBranch,
3588 input.threadId,
3589 progress.emit,
3590 + "pushRemote" in push && push.pushRemote && push.upstreamBranch
3591 + ? { remoteName: push.pushRemote, upstreamRef: push.upstreamBranch }
3592 + : undefined,
3593 ),
3594 ),
3595 )
apps/server/src/vcs/GitVcsDriver.ts
+4
@@ -179,12 +179,16 @@ export interface GitPushResult {
179 status: "pushed" | "skipped_up_to_date";
180 branch: string;
181 upstreamBranch?: string | undefined;
182 + /** Set when `upstreamBranch` is on a configured push remote instead of the tracked upstream. */
183 + pushRemote?: string | undefined;
184 setUpstream?: boolean | undefined;
185 }
186
187 export interface GitPushTarget {
188 remoteName: string;
189 branchName: string;
190 + /** The tracked upstream is current, yet pushes go to this configured push remote. */
191 + viaPushRemote: boolean;
192 }
193
194 export interface GitRangeContext {
apps/server/src/vcs/GitVcsDriverCore.test.ts
+146
@@ -7998,6 +7998,152 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => {
7998 assert.notEqual(originMain.exitCode, 0);
7999 }),
8000 );
8001 +
8002 + it.effect.each(["branch.topic.pushRemote", "remote.pushDefault"] as const)(
8003 + "pushes a branch tracking its upstream head to the %s remote",
8004 + (configKey) =>
8005 + Effect.gen(function* () {
8006 + const cwd = yield* makeTmpDir();
8007 + const upstreamRemote = yield* makeTmpDir("git-upstream-remote-");
8008 + const forkRemote = yield* makeTmpDir("git-fork-remote-");
8009 + yield* initRepoWithCommit(cwd);
8010 + const driver = yield* GitVcsDriver.GitVcsDriver;
8011 + yield* git(upstreamRemote, ["init", "--bare"]);
8012 + yield* git(forkRemote, ["init", "--bare"]);
8013 + yield* git(cwd, ["remote", "add", "upstream", upstreamRemote]);
8014 + yield* git(cwd, ["remote", "add", "fork", forkRemote]);
8015 + yield* git(cwd, ["checkout", "-b", "topic"]);
8016 + yield* git(cwd, ["push", "upstream", "topic"]);
8017 + yield* git(cwd, ["push", "fork", "topic"]);
8018 + yield* git(cwd, ["branch", "--set-upstream-to=upstream/topic"]);
8019 + yield* git(cwd, ["config", configKey, "fork"]);
8020 + const upstreamTopicSha = yield* git(cwd, ["rev-parse", "HEAD"]);
8021 + yield* writeTextFile(cwd, "fork.txt", "fork\n");
8022 + yield* git(cwd, ["add", "-A"]);
8023 + yield* driver.commit(cwd, "Add fork update", "");
8024 +
8025 + const pushed = yield* driver.pushCurrentBranch(cwd, null);
8026 +
8027 + assert.deepInclude(pushed, {
8028 + status: "pushed",
8029 + branch: "topic",
8030 + upstreamBranch: "fork/topic",
8031 + pushRemote: "fork",
8032 + setUpstream: false,
8033 + });
8034 + assert.equal(
8035 + yield* git(forkRemote, ["--git-dir=.", "log", "-1", "--pretty=%s", "topic"]),
8036 + "Add fork update",
8037 + );
8038 + assert.equal(
8039 + yield* git(upstreamRemote, ["--git-dir=.", "rev-parse", "topic"]),
8040 + upstreamTopicSha,
8041 + );
8042 + assert.equal(
8043 + yield* git(cwd, ["rev-parse", "--abbrev-ref", "@{upstream}"]),
8044 + "upstream/topic",
8045 + );
8046 + }),
8047 + );
8048 +
8049 + it.effect.each(["deleted", "rewound"] as const)(
8050 + "pushes an up-to-date tracked branch to the push remote when its branch there is %s",
8051 + (forkState) =>
8052 + Effect.gen(function* () {
8053 + const cwd = yield* makeTmpDir();
8054 + const upstreamRemote = yield* makeTmpDir("git-upstream-remote-");
8055 + const forkRemote = yield* makeTmpDir("git-fork-remote-");
8056 + yield* initRepoWithCommit(cwd);
8057 + const driver = yield* GitVcsDriver.GitVcsDriver;
8058 + yield* git(upstreamRemote, ["init", "--bare"]);
8059 + yield* git(forkRemote, ["init", "--bare"]);
8060 + yield* git(cwd, ["remote", "add", "upstream", upstreamRemote]);
8061 + yield* git(cwd, ["remote", "add", "fork", forkRemote]);
8062 + yield* git(cwd, ["checkout", "-b", "topic"]);
8063 + yield* writeTextFile(cwd, "upstream.txt", "upstream\n");
8064 + yield* git(cwd, ["add", "-A"]);
8065 + yield* driver.commit(cwd, "Add upstream update", "");
8066 + yield* git(cwd, ["push", "-u", "upstream", "topic"]);
8067 + yield* git(cwd, ["config", "remote.pushDefault", "fork"]);
8068 + // Another checkout changes the fork after this one last fetched it,
8069 + // leaving a local fork/topic that still contains HEAD.
8070 + yield* git(cwd, ["push", "fork", "topic"]);
8071 + yield* git(
8072 + forkRemote,
8073 + forkState === "deleted"
8074 + ? ["--git-dir=.", "update-ref", "-d", "refs/heads/topic"]
8075 + : ["--git-dir=.", "update-ref", "refs/heads/topic", "topic~1"],
8076 + );
8077 +
8078 + const pushed = yield* driver.pushCurrentBranch(cwd, null);
8079 +
8080 + assert.deepInclude(pushed, {
8081 + status: "pushed",
8082 + upstreamBranch: "fork/topic",
8083 + pushRemote: "fork",
8084 + });
8085 + assert.equal(
8086 + yield* git(forkRemote, ["--git-dir=.", "log", "-1", "--pretty=%s", "topic"]),
8087 + "Add upstream update",
8088 + );
8089 + assert.equal(
8090 + yield* git(cwd, ["rev-parse", "--abbrev-ref", "@{upstream}"]),
8091 + "upstream/topic",
8092 + );
8093 + }),
8094 + );
8095 +
8096 + it.effect("skips an up-to-date tracked branch the push remote already has", () =>
8097 + Effect.gen(function* () {
8098 + const cwd = yield* makeTmpDir();
8099 + const upstreamRemote = yield* makeTmpDir("git-upstream-remote-");
8100 + const forkRemote = yield* makeTmpDir("git-fork-remote-");
8101 + yield* initRepoWithCommit(cwd);
8102 + const driver = yield* GitVcsDriver.GitVcsDriver;
8103 + yield* git(upstreamRemote, ["init", "--bare"]);
8104 + yield* git(forkRemote, ["init", "--bare"]);
8105 + yield* git(cwd, ["remote", "add", "upstream", upstreamRemote]);
8106 + yield* git(cwd, ["remote", "add", "fork", forkRemote]);
8107 + yield* git(cwd, ["checkout", "-b", "topic"]);
8108 + yield* git(cwd, ["push", "-u", "upstream", "topic"]);
8109 + yield* git(cwd, ["push", "fork", "topic"]);
8110 + yield* git(cwd, ["config", "remote.pushDefault", "fork"]);
8111 +
8112 + const pushed = yield* driver.pushCurrentBranch(cwd, null);
8113 +
8114 + assert.deepInclude(pushed, {
8115 + status: "skipped_up_to_date",
8116 + upstreamBranch: "fork/topic",
8117 + pushRemote: "fork",
8118 + });
8119 + }),
8120 + );
8121 +
8122 + it.effect("skips an up-to-date branch whose differently named upstream is its base", () =>
8123 + Effect.gen(function* () {
8124 + const cwd = yield* makeTmpDir();
8125 + const upstreamRemote = yield* makeTmpDir("git-upstream-remote-");
8126 + const forkRemote = yield* makeTmpDir("git-fork-remote-");
8127 + yield* initRepoWithCommit(cwd);
8128 + const driver = yield* GitVcsDriver.GitVcsDriver;
8129 + yield* git(upstreamRemote, ["init", "--bare"]);
8130 + yield* git(forkRemote, ["init", "--bare"]);
8131 + yield* git(cwd, ["remote", "add", "upstream", upstreamRemote]);
8132 + yield* git(cwd, ["remote", "add", "fork", forkRemote]);
8133 + yield* git(cwd, ["push", "upstream", "HEAD:refs/heads/release"]);
8134 + yield* git(cwd, ["fetch", "upstream"]);
8135 + yield* git(cwd, ["checkout", "-b", "topic", "--track", "upstream/release"]);
8136 + yield* git(cwd, ["config", "remote.pushDefault", "fork"]);
8137 +
8138 + const pushed = yield* driver.pushCurrentBranch(cwd, null);
8139 +
8140 + assert.deepInclude(pushed, {
8141 + status: "skipped_up_to_date",
8142 + upstreamBranch: "upstream/release",
8143 + });
8144 + assert.equal(yield* git(forkRemote, ["--git-dir=.", "branch", "--list", "topic"]), "");
8145 + }),
8146 + );
8147 });
8148 });
8149
apps/server/src/vcs/GitVcsDriverCore.ts
+77 -31
@@ -1819,7 +1819,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
1819 ),
1820 );
1821
1822 - const resolvePushRemoteName = Effect.fn("resolvePushRemoteName")(function* (
1822 + const resolveConfiguredPushRemoteName = Effect.fn("resolveConfiguredPushRemoteName")(function* (
1823 cwd: string,
1824 refName: string,
1825 ) {
@@ -1839,13 +1839,32 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
1839 ["config", "--get", "remote.pushDefault"],
1840 true,
1841 ).pipe(Effect.map((stdout) => stdout.trim()));
1842 - if (pushDefaultRemote.length > 0) {
1843 - return pushDefaultRemote;
1842 + return pushDefaultRemote.length > 0 ? pushDefaultRemote : null;
1843 + });
1844 +
1845 + const resolvePushRemoteName = Effect.fn("resolvePushRemoteName")(function* (
1846 + cwd: string,
1847 + refName: string,
1848 + ) {
1849 + const configuredRemote = yield* resolveConfiguredPushRemoteName(cwd, refName);
1850 + if (configuredRemote) {
1851 + return configuredRemote;
1852 }
1853
1854 return yield* resolvePrimaryRemoteName(cwd).pipe(Effect.orElseSucceed(() => null));
1855 });
1856
1857 + const remoteRefContainsHead = (cwd: string, remoteRef: string) =>
1858 + executeGit(
1859 + "GitVcsDriver.pushCurrentBranch.publishContainsHead",
1860 + cwd,
1861 + ["merge-base", "--is-ancestor", "HEAD", `refs/remotes/${remoteRef}`],
1862 + { allowNonZeroExit: true },
1863 + ).pipe(
1864 + Effect.map((result) => result.exitCode === 0),
1865 + Effect.orElseSucceed(() => false),
1866 + );
1867 +
1868 const ensureRemote: GitVcsDriver.GitVcsDriver["Service"]["ensureRemote"] = Effect.fn(
1869 "ensureRemote",
1870 )(function* (input) {
@@ -2846,7 +2865,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
2865 const remoteName = yield* resolvePushRemoteName(cwd, branch);
2866 if (!remoteName) return null;
2867 const branchName = yield* resolvePublishBranchName(cwd, branch);
2849 - return { remoteName, branchName, publishes: true, upstream: null };
2868 + return { remoteName, branchName, publishes: true, upstream: null, pushRemote: null };
2869 }
2870
2871 if ((yield* readConfigValue(cwd, `branch.${branch}.remote`)) === ".") {
@@ -2864,6 +2883,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
2883 branchName: branch,
2884 publishes: true,
2885 upstream: { remoteName: ".", branchName: localUpstream, upstreamRef: localUpstream },
2886 + pushRemote: null,
2887 };
2888 }
2889
@@ -2874,11 +2894,20 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
2894 branch === upstream.branchName ||
2895 (branch.endsWith(`/${upstream.branchName}`) && upstream.upstreamRef.endsWith(`/${branch}`));
2896 if (isAliasOfUpstreamHead) {
2897 + // A fork workflow pushes to the configured push remote and keeps the upstream for pulls, as plain `git push` does.
2898 + const configuredPushRemote = yield* resolveConfiguredPushRemoteName(cwd, branch).pipe(
2899 + Effect.orElseSucceed(() => null),
2900 + );
2901 + const pushRemote =
2902 + configuredPushRemote && configuredPushRemote !== upstream.remoteName
2903 + ? configuredPushRemote
2904 + : null;
2905 return {
2878 - remoteName: upstream.remoteName,
2906 + remoteName: pushRemote ?? upstream.remoteName,
2907 branchName: upstream.branchName,
2908 publishes: false,
2909 upstream,
2910 + pushRemote,
2911 };
2912 }
2913 const publishRemoteName = yield* resolvePushRemoteName(cwd, branch).pipe(
@@ -2890,6 +2919,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
2919 branchName,
2920 publishes: true,
2921 upstream,
2922 + pushRemote: null,
2923 };
2924 });
2925
@@ -2898,7 +2928,11 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
2928 )(function* (cwd, current) {
2929 const destination = yield* resolvePushDestination(cwd, current.branch, current.hasUpstream);
2930 return destination
2901 - ? { remoteName: destination.remoteName, branchName: destination.branchName }
2931 + ? {
2932 + remoteName: destination.remoteName,
2933 + branchName: destination.branchName,
2934 + viaPushRemote: destination.pushRemote !== null,
2935 + }
2936 : null;
2937 });
2938
@@ -2936,16 +2970,35 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
2970 }
2971
2972 const hasNoLocalDelta = details.aheadCount === 0 && details.behindCount === 0;
2939 - if (hasNoLocalDelta && details.hasUpstream) {
2940 - return {
2941 - status: "skipped_up_to_date" as const,
2942 - branch,
2943 - ...(details.upstreamRef ? { upstreamBranch: details.upstreamRef } : {}),
2944 - };
2945 - }
2946 -
2973 const destination = yield* resolvePushDestination(cwd, branch, details.hasUpstream);
2948 - if (hasNoLocalDelta) {
2974 + if (hasNoLocalDelta && details.hasUpstream) {
2975 + if (!destination?.pushRemote) {
2976 + return {
2977 + status: "skipped_up_to_date" as const,
2978 + branch,
2979 + ...(details.upstreamRef ? { upstreamBranch: details.upstreamRef } : {}),
2980 + };
2981 + }
2982 + const { pushRemote, branchName } = destination;
2983 + const pushRemoteRef = `${pushRemote}/${branchName}`;
2984 + // The local ref can be stale when the branch was deleted or rewound elsewhere.
2985 + const pushRemoteHasHead = yield* fetchRemoteTrackingBranch({
2986 + cwd,
2987 + remoteName: pushRemote,
2988 + remoteBranch: branchName,
2989 + }).pipe(
2990 + Effect.andThen(remoteRefContainsHead(cwd, pushRemoteRef)),
2991 + Effect.orElseSucceed(() => false),
2992 + );
2993 + if (pushRemoteHasHead) {
2994 + return {
2995 + status: "skipped_up_to_date" as const,
2996 + branch,
2997 + upstreamBranch: pushRemoteRef,
2998 + pushRemote,
2999 + };
3000 + }
3001 + } else if (hasNoLocalDelta) {
3002 const comparableBaseBranch = yield* resolveBaseBranchForNoUpstream(cwd, branch).pipe(
3003 Effect.orElseSucceed(() => null),
3004 );
@@ -2958,21 +3011,9 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
3011 }
3012
3013 // The configured diff base can contain HEAD while the publish branch is still behind.
2961 - const publishContainsHead = yield* executeGit(
2962 - "GitVcsDriver.pushCurrentBranch.publishContainsHead",
2963 - cwd,
2964 - [
2965 - "merge-base",
2966 - "--is-ancestor",
2967 - "HEAD",
2968 - `refs/remotes/${destination.remoteName}/${destination.branchName}`,
2969 - ],
2970 - { allowNonZeroExit: true },
2971 - ).pipe(
2972 - Effect.map((result) => result.exitCode === 0),
2973 - Effect.orElseSucceed(() => false),
2974 - );
2975 - if (publishContainsHead) {
3014 + if (
3015 + yield* remoteRefContainsHead(cwd, `${destination.remoteName}/${destination.branchName}`)
3016 + ) {
3017 return {
3018 status: "skipped_up_to_date" as const,
3019 branch,
@@ -3041,7 +3082,12 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
3082 return {
3083 status: "pushed" as const,
3084 branch,
3044 - upstreamBranch: destination.upstream.upstreamRef,
3085 + ...(destination.pushRemote
3086 + ? {
3087 + upstreamBranch: `${destination.remoteName}/${destination.branchName}`,
3088 + pushRemote: destination.pushRemote,
3089 + }
3090 + : { upstreamBranch: destination.upstream.upstreamRef }),
3091 setUpstream: false,
3092 };
3093 }