Skip to content

Commit e7601c3

Browse files
committed
fix(server): Changes totals no longer diff against a guessed base after a branch rename
A status read that saw the temporary thread branch before the automatic rename looked up the old name's base after git branch -m had moved its config, then guessed the primary remote's default branch. With a stale fork as origin, the Changes row showed hundreds of thousands of lines. Discard the resolved base when the branch no longer exists, so that read reports working-tree totals until the next refresh.
1 parent dd14848 commit e7601c3

2 files changed

Lines changed: 70 additions & 1 deletion

File tree

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

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2173,6 +2173,70 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => {
21732173
}),
21742174
);
21752175

2176+
it.effect("does not guess a Changes base for a branch renamed during the status read", () =>
2177+
Effect.gen(function* () {
2178+
const cwd = yield* makeTmpDir();
2179+
const fork = yield* makeTmpDir();
2180+
const upstream = yield* makeTmpDir();
2181+
yield* initRepoWithCommit(cwd);
2182+
yield* git(cwd, ["branch", "-M", "main"]);
2183+
yield* git(fork, ["init", "--bare"]);
2184+
yield* git(upstream, ["init", "--bare"]);
2185+
yield* git(cwd, ["remote", "add", "origin", fork]);
2186+
yield* git(cwd, ["remote", "add", "upstream", upstream]);
2187+
yield* git(cwd, ["push", "origin", "main"]);
2188+
yield* writeTextFile(cwd, "upstream.txt", "work missing from the fork\n");
2189+
yield* git(cwd, ["add", "upstream.txt"]);
2190+
yield* git(cwd, ["commit", "-m", "upstream work"]);
2191+
yield* git(cwd, ["push", "upstream", "main"]);
2192+
yield* git(cwd, ["fetch", "origin"]);
2193+
yield* git(cwd, ["fetch", "upstream"]);
2194+
yield* git(cwd, ["checkout", "-b", "t3/temporary"]);
2195+
yield* git(cwd, ["config", "branch.t3/temporary.t3-merge-base", "main"]);
2196+
yield* git(cwd, [
2197+
"config",
2198+
"branch.t3/temporary.t3-merge-base-ref",
2199+
"refs/remotes/upstream/main",
2200+
]);
2201+
yield* writeTextFile(cwd, "feature.txt", "thread work\n");
2202+
2203+
const outer = yield* GitVcsDriver.GitVcsDriver;
2204+
const delegate = yield* ChildProcessSpawner.ChildProcessSpawner;
2205+
const renameClaimed = yield* Ref.make(false);
2206+
const renamed = yield* Deferred.make<void>();
2207+
// Rename after `git status` has read HEAD, as the thread launcher does after the first turn.
2208+
// Base config reads start only after status completes; hold every one until the rename lands.
2209+
const spawner = ChildProcessSpawner.make((command) =>
2210+
Effect.gen(function* () {
2211+
if (ChildProcess.isStandardCommand(command) && command.args[0] === "config") {
2212+
if (!(yield* Ref.getAndSet(renameClaimed, true))) {
2213+
yield* git(cwd, ["branch", "-m", "t3/temporary", "feature/named"]).pipe(
2214+
Effect.provideService(GitVcsDriver.GitVcsDriver, outer),
2215+
Effect.orDie,
2216+
);
2217+
yield* Deferred.succeed(renamed, undefined);
2218+
}
2219+
yield* Deferred.await(renamed);
2220+
}
2221+
return yield* delegate.spawn(command);
2222+
}),
2223+
);
2224+
const driver = yield* makeGitVcsDriverCore().pipe(
2225+
Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
2226+
Effect.provide(layerServerConfig),
2227+
);
2228+
2229+
const status = yield* driver.statusDetailsLocal(cwd, { includeBranchChanges: true });
2230+
2231+
assert.strictEqual(status.branch, "t3/temporary");
2232+
assert.deepStrictEqual(status.branchChanges, {
2233+
baseRef: null,
2234+
insertions: 1,
2235+
deletions: 0,
2236+
});
2237+
}),
2238+
);
2239+
21762240
it.effect("keeps local branch bases consistent when tags share their names", () =>
21772241
Effect.gen(function* () {
21782242
const cwd = yield* makeTmpDir();

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3068,12 +3068,17 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
30683068
branch: string | null,
30693069
options?: { readonly knownOriginDefaultBranch: string | null },
30703070
) {
3071-
const { baseRef, mergeBase } = yield* resolveReviewMergeBase(
3071+
const resolved = yield* resolveReviewMergeBase(
30723072
cwd,
30733073
branch,
30743074
undefined,
30753075
options?.knownOriginDefaultBranch ?? null,
30763076
);
3077+
// A rename after `git status` read HEAD moves the base config away, so the base was guessed.
3078+
const { baseRef, mergeBase } =
3079+
branch === null || (yield* branchExists(cwd, branch))
3080+
? resolved
3081+
: { baseRef: null, mergeBase: "HEAD" };
30773082
const untracked = yield* prepareUntrackedReviewIndex(cwd, []);
30783083
if (untracked === null) {
30793084
return yield* new GitCommandError({

0 commit comments

Comments
 (0)