@devries-cameron20 / t3code-1 / commits / 8b0fbc9a7d
Fork 0

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

fix(server): honor configured remote diff bases (#16453)

Nikita Maximilian Grimm committed Oct 18, 2026 at 21:51 UTC 8b0fbc9a7d9fd9b4fa062aa907ab5868b4a69bdf
2 files changed +284 -20
apps/server/src/vcs/GitVcsDriverCore.test.ts
+220
@@ -2288,6 +2288,159 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => {
2288 }),
2289 );
2290
2291 + it.effect("honors configured remote-qualified bases for Changes and ahead counts", () =>
2292 + Effect.gen(function* () {
2293 + const cwd = yield* makeTmpDir();
2294 + const fork = yield* makeTmpDir();
2295 + const upstream = yield* makeTmpDir();
2296 + yield* initRepoWithCommit(cwd);
2297 + const driver = yield* GitVcsDriver.GitVcsDriver;
2298 + yield* git(cwd, ["branch", "-M", "main"]);
2299 + yield* git(fork, ["init", "--bare"]);
2300 + yield* git(upstream, ["init", "--bare"]);
2301 + yield* git(cwd, ["remote", "add", "origin", fork]);
2302 + yield* git(cwd, ["remote", "add", "upstream", upstream]);
2303 + yield* git(cwd, ["remote", "add", "canonical/team", upstream]);
2304 + yield* git(cwd, ["push", "origin", "main"]);
2305 + yield* writeTextFile(cwd, "upstream.txt", "unrelated upstream work\n");
2306 + yield* git(cwd, ["add", "upstream.txt"]);
2307 + yield* git(cwd, ["commit", "-m", "upstream work"]);
2308 + yield* git(cwd, ["push", "upstream", "main"]);
2309 + yield* git(cwd, ["fetch", "canonical/team"]);
2310 + yield* git(cwd, ["branch", "release/v2"]);
2311 + yield* git(cwd, ["checkout", "-b", "feature/configured-base"]);
2312 + yield* writeTextFile(cwd, "feature.txt", "intended branch change\n");
2313 + yield* git(cwd, ["add", "feature.txt"]);
2314 + yield* git(cwd, ["commit", "-m", "feature work"]);
2315 +
2316 + for (const baseRef of ["upstream/main", "canonical/team/main", "release/v2"]) {
2317 + yield* git(cwd, ["config", "branch.feature/configured-base.gh-merge-base", baseRef]);
2318 + const status = yield* driver.statusDetailsLocal(cwd, { includeBranchChanges: true });
2319 + assert.deepStrictEqual(status.branchChanges, { baseRef, insertions: 1, deletions: 0 });
2320 + assert.strictEqual(status.aheadCount, 1);
2321 + const preview = yield* driver.getReviewDiffPreview({ cwd });
2322 + const changes = preview.sources.find((source) => source.kind === "branch-range")!;
2323 + assert.strictEqual(changes.baseRef, baseRef);
2324 + assert.deepStrictEqual(changes.files, [
2325 + { path: "feature.txt", previousPath: null, additions: 1, deletions: 0 },
2326 + ]);
2327 + }
2328 +
2329 + for (const baseRef of ["origin/main", "upstream/missing", "main"]) {
2330 + yield* git(cwd, ["config", "branch.feature/configured-base.gh-merge-base", baseRef]);
2331 + const status = yield* driver.statusDetailsLocal(cwd, { includeBranchChanges: true });
2332 + assert.deepStrictEqual(status.branchChanges, {
2333 + baseRef: "origin/main",
2334 + insertions: 2,
2335 + deletions: 0,
2336 + });
2337 + assert.strictEqual(status.aheadCount, 2);
2338 + }
2339 +
2340 + yield* git(cwd, [
2341 + "config",
2342 + "branch.feature/configured-base.gh-merge-base",
2343 + "upstream/main",
2344 + ]);
2345 + yield* writeTextFile(cwd, "upstream.txt", "feature edits upstream file\n");
2346 + for (const shadowRef of ["refs/heads/upstream/main", "refs/tags/upstream/main"]) {
2347 + yield* git(cwd, ["update-ref", shadowRef, "HEAD~2"]);
2348 + const status = yield* driver.statusDetailsLocal(cwd, { includeBranchChanges: true });
2349 + assert.deepStrictEqual(status.branchChanges, {
2350 + baseRef: "upstream/main",
2351 + insertions: 2,
2352 + deletions: 1,
2353 + });
2354 + assert.strictEqual(status.aheadCount, 1);
2355 + const preview = yield* driver.getReviewDiffPreview({ cwd });
2356 + const changes = preview.sources.find((source) => source.kind === "branch-range")!;
2357 + assert.strictEqual(changes.baseRef, "upstream/main");
2358 + assert.deepStrictEqual(changes.files, [
2359 + { path: "feature.txt", previousPath: null, additions: 1, deletions: 0 },
2360 + { path: "upstream.txt", previousPath: null, additions: 1, deletions: 1 },
2361 + ]);
2362 + const contents = yield* driver.getReviewDiffFileContents(
2363 + makeReviewDiffFileContentsInput(cwd, {
2364 + sourceKind: "branch-range",
2365 + baseRef: changes.baseRef,
2366 + headRef: "feature/configured-base",
2367 + oldPath: "upstream.txt",
2368 + newPath: "upstream.txt",
2369 + }),
2370 + );
2371 + assert.deepStrictEqual(contents, {
2372 + oldContents: "unrelated upstream work\n",
2373 + newContents: "feature edits upstream file\n",
2374 + });
2375 + yield* git(cwd, ["update-ref", "-d", shadowRef]);
2376 + }
2377 + }),
2378 + );
2379 +
2380 + it.effect("keeps local branch bases consistent when tags share their names", () =>
2381 + Effect.gen(function* () {
2382 + const cwd = yield* makeTmpDir();
2383 + yield* initRepoWithCommit(cwd);
2384 + const driver = yield* GitVcsDriver.GitVcsDriver;
2385 + yield* git(cwd, ["branch", "-M", "main"]);
2386 + const initialCommit = yield* git(cwd, ["rev-parse", "HEAD"]);
2387 + yield* git(cwd, ["update-ref", "refs/tags/main", initialCommit]);
2388 + yield* git(cwd, ["update-ref", "refs/tags/release/v2", initialCommit]);
2389 + yield* writeTextFile(cwd, "README.md", "# test\nbase work\n");
2390 + yield* git(cwd, ["commit", "-am", "base work"]);
2391 + yield* git(cwd, ["branch", "release/v2", "HEAD"]);
2392 + yield* git(cwd, ["checkout", "-b", "feature/local-base", "HEAD"]);
2393 + yield* writeTextFile(cwd, "README.md", "# test\nbase work\nfeature work\n");
2394 + yield* git(cwd, ["commit", "-am", "feature work"]);
2395 +
2396 + for (const configuredBase of [undefined, "main", "release/v2"]) {
2397 + if (configuredBase !== undefined) {
2398 + yield* git(cwd, ["config", "branch.feature/local-base.gh-merge-base", configuredBase]);
2399 + }
2400 + const baseRef = configuredBase ?? "main";
2401 + const status = yield* driver.statusDetailsLocal(cwd, { includeBranchChanges: true });
2402 + assert.deepStrictEqual(status.branchChanges, { baseRef, insertions: 1, deletions: 0 });
2403 + assert.strictEqual(status.aheadCount, 1);
2404 + const preview = yield* driver.getReviewDiffPreview({ cwd });
2405 + const changes = preview.sources.find((source) => source.kind === "branch-range")!;
2406 + assert.strictEqual(changes.baseRef, baseRef);
2407 + assert.deepStrictEqual(changes.files, [
2408 + { path: "README.md", previousPath: null, additions: 1, deletions: 0 },
2409 + ]);
2410 + const scoped = yield* driver.getReviewDiffPreview({
2411 + cwd,
2412 + baseRef,
2413 + file: { path: "README.md", previousPath: null, sourceKind: "branch-range" },
2414 + });
2415 + const scopedChanges = scoped.sources.find((source) => source.kind === "branch-range")!;
2416 + assert.deepStrictEqual(scopedChanges.files, changes.files);
2417 + assert.strictEqual(scopedChanges.diff, changes.diff);
2418 + const contents = yield* driver.getReviewDiffFileContents(
2419 + makeReviewDiffFileContentsInput(cwd, {
2420 + sourceKind: "branch-range",
2421 + baseRef: changes.baseRef,
2422 + headRef: "feature/local-base",
2423 + }),
2424 + );
2425 + assert.deepStrictEqual(contents, {
2426 + oldContents: "# test\nbase work\n",
2427 + newContents: "# test\nbase work\nfeature work\n",
2428 + });
2429 + }
2430 +
2431 + for (const baseRef of ["refs/tags/main", initialCommit, "HEAD~2"]) {
2432 + const contents = yield* driver.getReviewDiffFileContents(
2433 + makeReviewDiffFileContentsInput(cwd, {
2434 + sourceKind: "branch-range",
2435 + baseRef,
2436 + headRef: "feature/local-base",
2437 + }),
2438 + );
2439 + assert.strictEqual(contents.oldContents, "# test\n");
2440 + }
2441 + }),
2442 + );
2443 +
2444 it.effect("Changes accepts an explicit base on a detached HEAD and rejects a bad one", () =>
2445 Effect.gen(function* () {
2446 const cwd = yield* makeTmpDir();
@@ -4155,6 +4308,73 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => {
4308 }),
4309 );
4310
4311 + it.effect.each(["feature/push", "origin/feature/push"])(
4312 + "publishes %s when the configured diff base already contains HEAD",
4313 + (branch) =>
4314 + Effect.gen(function* () {
4315 + const cwd = yield* makeTmpDir();
4316 + const originRemote = yield* makeTmpDir("git-origin-remote-");
4317 + const upstreamRemote = yield* makeTmpDir("git-upstream-remote-");
4318 + yield* initRepoWithCommit(cwd);
4319 + const driver = yield* GitVcsDriver.GitVcsDriver;
4320 + yield* git(cwd, ["branch", "-M", "main"]);
4321 + yield* git(originRemote, ["init", "--bare"]);
4322 + yield* git(upstreamRemote, ["init", "--bare"]);
4323 + yield* git(cwd, ["remote", "add", "origin", originRemote]);
4324 + yield* git(cwd, ["remote", "add", "upstream", upstreamRemote]);
4325 + yield* git(cwd, ["push", "origin", "main", "main:refs/heads/feature/push"]);
4326 + yield* git(cwd, ["checkout", "--no-track", "-b", branch]);
4327 + yield* writeTextFile(cwd, "feature.txt", "feature\n");
4328 + yield* git(cwd, ["add", "-A"]);
4329 + yield* driver.commit(cwd, "Add feature", "");
4330 + yield* git(cwd, ["push", "upstream", "HEAD:refs/heads/main"]);
4331 + yield* git(cwd, ["config", `branch.${branch}.gh-merge-base`, "upstream/main"]);
4332 +
4333 + const status = yield* driver.statusDetails(cwd);
4334 + assert.equal(status.hasUpstream, false);
4335 + assert.equal(status.aheadCount, 0);
4336 + assert.equal(status.behindCount, 0);
4337 + const head = yield* git(cwd, ["rev-parse", "HEAD"]);
4338 + assert.notEqual(
4339 + yield* git(originRemote, ["--git-dir=.", "rev-parse", "refs/heads/feature/push"]),
4340 + head,
4341 + );
4342 +
4343 + const pushed = yield* driver.pushCurrentBranch(cwd, null);
4344 + assert.deepInclude(pushed, {
4345 + status: "pushed",
4346 + branch,
4347 + upstreamBranch: "origin/feature/push",
4348 + setUpstream: true,
4349 + });
4350 + assert.equal(
4351 + yield* git(originRemote, ["--git-dir=.", "rev-parse", "refs/heads/feature/push"]),
4352 + head,
4353 + );
4354 +
4355 + yield* git(cwd, ["branch", "--unset-upstream"]);
4356 + const skipped = yield* driver.pushCurrentBranch(cwd, null);
4357 + assert.deepInclude(skipped, { status: "skipped_up_to_date", branch });
4358 +
4359 + yield* writeTextFile(cwd, "remote.txt", "remote update\n");
4360 + yield* git(cwd, ["add", "-A"]);
4361 + yield* driver.commit(cwd, "Advance publish branch", "");
4362 + yield* git(cwd, ["push", "origin", "HEAD:refs/heads/feature/push"]);
4363 + const publishedHead = yield* git(originRemote, [
4364 + "--git-dir=.",
4365 + "rev-parse",
4366 + "refs/heads/feature/push",
4367 + ]);
4368 + yield* git(cwd, ["reset", "--hard", head]);
4369 + const skippedBehindRemote = yield* driver.pushCurrentBranch(cwd, null);
4370 + assert.deepInclude(skippedBehindRemote, { status: "skipped_up_to_date", branch });
4371 + assert.equal(
4372 + yield* git(originRemote, ["--git-dir=.", "rev-parse", "refs/heads/feature/push"]),
4373 + publishedHead,
4374 + );
4375 + }),
4376 + );
4377 +
4378 it.effect("allows pushes to run longer than the default command timeout", () =>
4379 Effect.gen(function* () {
4380 const delegate = yield* ChildProcessSpawner.ChildProcessSpawner;
apps/server/src/vcs/GitVcsDriverCore.ts
+64 -20
@@ -1667,6 +1667,21 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
1667 return remoteName;
1668 });
1669
1670 + const resolveRemoteBaseRef = Effect.fn("resolveRemoteBaseRef")(function* (
1671 + cwd: string,
1672 + baseRef: string,
1673 + ) {
1674 + if (!baseRef.includes("/") || baseRef.startsWith("refs/")) return null;
1675 + const comparisonRef = `refs/remotes/${baseRef}`;
1676 + const remoteBase = yield* executeGit(
1677 + "GitVcsDriver.resolveRemoteBaseRef",
1678 + cwd,
1679 + ["show-ref", "--verify", "--quiet", comparisonRef],
1680 + { allowNonZeroExit: true },
1681 + );
1682 + return remoteBase.exitCode === 0 ? { baseRef, comparisonRef } : null;
1683 + });
1684 +
1685 const resolveBaseBranchForNoUpstream = Effect.fn("resolveBaseBranchForNoUpstream")(function* (
1686 cwd: string,
1687 refName: string,
@@ -1679,6 +1694,10 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
1694 true,
1695 ).pipe(Effect.map((stdout) => stdout.trim()));
1696
1697 + // Keep the configured remote and compare its exact ref even when a local ref shadows it.
1698 + const configuredRemoteBase = yield* resolveRemoteBaseRef(cwd, configuredBaseBranch);
1699 + if (configuredRemoteBase !== null) return configuredRemoteBase;
1700 +
1701 const primaryRemoteName = yield* resolvePrimaryRemoteName(cwd).pipe(
1702 Effect.orElseSucceed(() => null),
1703 );
@@ -1715,7 +1734,10 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
1734 refName: normalizedCandidate,
1735 }))
1736 ) {
1718 - return `${primaryRemoteName}/${normalizedCandidate}`;
1737 + return {
1738 + baseRef: `${primaryRemoteName}/${normalizedCandidate}`,
1739 + comparisonRef: `refs/remotes/${primaryRemoteName}/${normalizedCandidate}`,
1740 + };
1741 }
1742 continue;
1743 }
@@ -1728,11 +1750,14 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
1750 refName: normalizedCandidate,
1751 }))
1752 ) {
1731 - return `${primaryRemoteName}/${normalizedCandidate}`;
1753 + return {
1754 + baseRef: `${primaryRemoteName}/${normalizedCandidate}`,
1755 + comparisonRef: `refs/remotes/${primaryRemoteName}/${normalizedCandidate}`,
1756 + };
1757 }
1758
1759 if (yield* branchExists(cwd, normalizedCandidate)) {
1735 - return normalizedCandidate;
1760 + return { baseRef: normalizedCandidate, comparisonRef: `refs/heads/${normalizedCandidate}` };
1761 }
1762 }
1763
@@ -1743,15 +1768,15 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
1768 cwd: string,
1769 refName: string,
1770 ) {
1746 - const baseRef = yield* resolveBaseBranchForNoUpstream(cwd, refName);
1747 - if (!baseRef) {
1771 + const base = yield* resolveBaseBranchForNoUpstream(cwd, refName);
1772 + if (!base) {
1773 return 0;
1774 }
1775
1776 const result = yield* executeGit(
1777 "GitVcsDriver.computeAheadCountAgainstBase",
1778 cwd,
1754 - ["rev-list", "--count", "--end-of-options", `${baseRef}..HEAD`],
1779 + ["rev-list", "--count", "--end-of-options", `${base.comparisonRef}..HEAD`],
1780 { allowNonZeroExit: true },
1781 );
1782 if (result.exitCode !== 0) {
@@ -2386,12 +2411,23 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
2411 };
2412 }
2413
2389 - const hasRemoteBranch = yield* remoteBranchExists({
2414 + const publishBranch = yield* resolvePublishBranchName(cwd, branch);
2415 + // The configured diff base can contain HEAD while the publish branch is still behind.
2416 + const publishContainsHead = yield* executeGit(
2417 + "GitVcsDriver.pushCurrentBranch.publishContainsHead",
2418 cwd,
2391 - remoteName: publishRemoteName,
2392 - refName: branch,
2393 - }).pipe(Effect.orElseSucceed(() => false));
2394 - if (hasRemoteBranch) {
2419 + [
2420 + "merge-base",
2421 + "--is-ancestor",
2422 + "HEAD",
2423 + `refs/remotes/${publishRemoteName}/${publishBranch}`,
2424 + ],
2425 + { allowNonZeroExit: true },
2426 + ).pipe(
2427 + Effect.map((result) => result.exitCode === 0),
2428 + Effect.orElseSucceed(() => false),
2429 + );
2430 + if (publishContainsHead) {
2431 return {
2432 status: "skipped_up_to_date" as const,
2433 branch,
@@ -2725,15 +2761,23 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
2761 branch: string | null,
2762 explicitBaseRef?: string,
2763 ) {
2728 - const baseRef =
2729 - explicitBaseRef ??
2730 - (branch
2731 - ? yield* resolveBaseBranchForNoUpstream(cwd, branch, { allowRemoteOfCurrent: true }).pipe(
2732 - Effect.orElseSucceed(() => null),
2733 - )
2734 - : null);
2735 - if (baseRef === null) return { baseRef, mergeBase: "HEAD" };
2736 - const args = ["merge-base", "--end-of-options", baseRef, "HEAD"];
2764 + const base =
2765 + explicitBaseRef !== undefined
2766 + ? ((yield* resolveRemoteBaseRef(cwd, explicitBaseRef)) ?? {
2767 + baseRef: explicitBaseRef,
2768 + comparisonRef:
2769 + !explicitBaseRef.startsWith("refs/") && (yield* branchExists(cwd, explicitBaseRef))
2770 + ? `refs/heads/${explicitBaseRef}`
2771 + : explicitBaseRef,
2772 + })
2773 + : branch
2774 + ? yield* resolveBaseBranchForNoUpstream(cwd, branch, { allowRemoteOfCurrent: true }).pipe(
2775 + Effect.orElseSucceed(() => null),
2776 + )
2777 + : null;
2778 + if (base === null) return { baseRef: null, mergeBase: "HEAD" };
2779 + const { baseRef, comparisonRef } = base;
2780 + const args = ["merge-base", "--end-of-options", comparisonRef, "HEAD"];
2781 const result = yield* executeGit("GitVcsDriver.resolveReviewMergeBase", cwd, args, {
2782 allowNonZeroExit: true,
2783 });