From 2c5a34ae562b7d61b60394c66983edc3f02a2282 Mon Sep 17 00:00:00 2001 From: Azim Sonawalla Date: Sun, 18 Oct 2026 01:10:11 +0000 Subject: [PATCH] fix(server): Changes counts only a fork branch's own work The thread panel's Changes row showed +2,381 -1,165 for a branch whose diff was +58 -63. The base name ("main") was only ever looked up on the primary remote or as a local branch, so a branch cut from fork/main was diffed against origin/main and counted every commit the fork has that upstream lacks. Even gh-merge-base=fork/main resolved to origin/main. The base name is chosen as before. When the remote the local base branch tracks also has it, as with a local main that follows a fork, Changes uses whichever of the two copies the branch was cut from: the one that leaves the fewest of its commits outside. Remotes fetched only for someone else's pull request are never compared, and the local branch still wins when the primary remote lacks the name. Co-Authored-By: Claude Opus 5.5 --- apps/server/src/vcs/GitVcsDriverCore.test.ts | 89 +++++++++++++++++++ apps/server/src/vcs/GitVcsDriverCore.ts | 91 ++++++++++++++++---- 2 files changed, 163 insertions(+), 17 deletions(-) diff --git a/apps/server/src/vcs/GitVcsDriverCore.test.ts b/apps/server/src/vcs/GitVcsDriverCore.test.ts index d98da188562f9..d9962b1490137 100644 --- a/apps/server/src/vcs/GitVcsDriverCore.test.ts +++ b/apps/server/src/vcs/GitVcsDriverCore.test.ts @@ -2414,6 +2414,95 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => { }), ); + it.effect("counts Changes from the copy of main a branch was cut from", () => + Effect.gen(function* () { + const upstream = yield* makeTmpDir(); + yield* initRepoWithCommit(upstream); + yield* git(upstream, ["branch", "-M", "main"]); + const fork = yield* makeTmpDir(); + yield* git(fork, ["clone", upstream, "."]); + yield* git(fork, ["config", "user.email", "test@test.com"]); + yield* git(fork, ["config", "user.name", "Test"]); + yield* writeTextFile(fork, "fork.txt", "one\ntwo\nthree\n"); + yield* git(fork, ["add", "fork.txt"]); + yield* git(fork, ["commit", "-m", "fork-only work"]); + yield* writeTextFile(upstream, "upstream.txt", "upstream\n"); + yield* git(upstream, ["add", "upstream.txt"]); + yield* git(upstream, ["commit", "-m", "upstream-only work"]); + const cwd = yield* makeTmpDir(); + yield* git(cwd, ["clone", upstream, "."]); + yield* git(cwd, ["config", "user.email", "test@test.com"]); + yield* git(cwd, ["config", "user.name", "Test"]); + yield* git(cwd, ["remote", "add", "fork", fork]); + yield* git(cwd, ["fetch", "fork"]); + // Local main follows the fork, so both copies of main can be the base. + yield* git(cwd, ["branch", "--set-upstream-to", "fork/main", "main"]); + const driver = yield* GitVcsDriver.GitVcsDriver; + const changes = Effect.map( + driver.statusDetailsLocal(cwd, { includeBranchChanges: true }), + (status) => status.branchChanges, + ); + + yield* git(cwd, ["switch", "-c", "from-fork", "fork/main"]); + yield* writeTextFile(cwd, "from-fork.txt", "fork feature\n"); + yield* git(cwd, ["add", "from-fork.txt"]); + yield* git(cwd, ["commit", "-m", "work on the fork"]); + assert.deepStrictEqual(yield* changes, { + baseRef: "fork/main", + insertions: 1, + deletions: 0, + }); + + yield* git(cwd, ["switch", "-c", "from-origin", "origin/main"]); + yield* writeTextFile(cwd, "from-origin.txt", "origin\nfeature\n"); + yield* git(cwd, ["add", "from-origin.txt"]); + yield* git(cwd, ["commit", "-m", "work on upstream"]); + assert.deepStrictEqual(yield* changes, { + baseRef: "origin/main", + insertions: 2, + deletions: 0, + }); + + yield* git(cwd, ["switch", "main"]); + yield* git(cwd, ["reset", "--hard", "fork/main"]); + yield* writeTextFile(cwd, "local.txt", "local\n"); + assert.deepStrictEqual(yield* changes, { + baseRef: "fork/main", + insertions: 1, + deletions: 0, + }); + }), + ); + + it.effect("never counts Changes from a pull request author's copy of main", () => + Effect.gen(function* () { + const upstream = yield* makeTmpDir(); + yield* initRepoWithCommit(upstream); + yield* git(upstream, ["branch", "-M", "main"]); + const author = yield* makeTmpDir(); + yield* git(author, ["clone", upstream, "."]); + yield* git(author, ["config", "user.email", "test@test.com"]); + yield* git(author, ["config", "user.name", "Test"]); + yield* writeTextFile(author, "pr.txt", "pull\nrequest\n"); + yield* git(author, ["add", "pr.txt"]); + yield* git(author, ["commit", "-m", "pull request work"]); + const cwd = yield* makeTmpDir(); + yield* git(cwd, ["clone", upstream, "."]); + // How a checkout of a pull request opened from the author's main looks. + yield* git(cwd, ["remote", "add", "author", author]); + yield* git(cwd, ["fetch", "author", "+refs/heads/main:refs/remotes/author/main"]); + yield* git(cwd, ["switch", "-c", "t3code/pr-42/main", "author/main"]); + const driver = yield* GitVcsDriver.GitVcsDriver; + + const status = yield* driver.statusDetailsLocal(cwd, { includeBranchChanges: true }); + assert.deepStrictEqual(status.branchChanges, { + baseRef: "origin/main", + insertions: 2, + deletions: 0, + }); + }), + ); + it.effect("reports non-repository directories without failing", () => Effect.gen(function* () { const cwd = yield* makeTmpDir(); diff --git a/apps/server/src/vcs/GitVcsDriverCore.ts b/apps/server/src/vcs/GitVcsDriverCore.ts index 439c6a55ac783..2d0e83cb5d1ac 100644 --- a/apps/server/src/vcs/GitVcsDriverCore.ts +++ b/apps/server/src/vcs/GitVcsDriverCore.ts @@ -1677,6 +1677,55 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* return remoteName; }); + // The remote the local `branchName` tracks, such as the fork a local main follows. + // A remote fetched for someone else's pull request is never it. + const readTrackedRemoteName = Effect.fn("readTrackedRemoteName")(function* ( + cwd: string, + branchName: string, + ) { + const ref = `refs/heads/${branchName}`; + const stdout = yield* runGitStdout( + "GitVcsDriver.readTrackedRemoteName", + cwd, + ["for-each-ref", "--format=%(refname) %(upstream:remotename)", ref], + true, + ); + const remoteName = stdout + .split("\n") + .map((line) => line.trim().split(" ")) + .find(([refName]) => refName === ref)?.[1]; + return remoteName && remoteName !== "." ? remoteName : null; + }); + + // The copy that leaves the fewest of `refName`'s commits outside it is the one + // the branch was cut from. Ties keep the earlier copy. + const closestCopy = Effect.fn("closestCopy")(function* ( + cwd: string, + refName: string, + copies: readonly [string, ...string[]], + ) { + let closest = copies[0]; + let fewestOutside = Number.POSITIVE_INFINITY; + for (const copy of copies) { + const stdout = yield* runGitStdout( + "GitVcsDriver.closestCopy", + cwd, + ["rev-list", "--count", `${copy}..refs/heads/${refName}`], + true, + ); + const outside = Number.parseInt(stdout.trim(), 10); + if (outside < fewestOutside) { + closest = copy; + fewestOutside = outside; + } + } + return closest; + }); + + // Picks the base branch name from gh-merge-base, the default branch, then main or + // master, and returns the primary remote's copy, else the local branch. When the local + // base tracks another remote, as with a fork beside its upstream, the copy the branch + // was cut from wins, so Changes holds only the branch's commits. // `allowRemoteOfCurrent` lets the review diff compare the default branch with its remote copy. const resolveBaseBranchForNoUpstream = Effect.fn("resolveBaseBranchForNoUpstream")(function* ( cwd: string, @@ -1716,34 +1765,42 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* if (normalizedCandidate.length === 0) { continue; } - if (normalizedCandidate === refName) { - if ( - options?.allowRemoteOfCurrent && - primaryRemoteName && - (yield* remoteBranchExists({ - cwd, - remoteName: primaryRemoteName, - refName: normalizedCandidate, - })) - ) { - return `${primaryRemoteName}/${normalizedCandidate}`; - } + const isCurrent = normalizedCandidate === refName; + if (isCurrent && !options?.allowRemoteOfCurrent) { continue; } - if ( + const primaryCopy = primaryRemoteName && (yield* remoteBranchExists({ cwd, remoteName: primaryRemoteName, refName: normalizedCandidate, })) - ) { - return `${primaryRemoteName}/${normalizedCandidate}`; + ? `${primaryRemoteName}/${normalizedCandidate}` + : null; + if (primaryCopy === null && !isCurrent && (yield* branchExists(cwd, normalizedCandidate))) { + return normalizedCandidate; } - if (yield* branchExists(cwd, normalizedCandidate)) { - return normalizedCandidate; + const copies = primaryCopy === null ? [] : [primaryCopy]; + const trackedRemoteName = yield* readTrackedRemoteName(cwd, normalizedCandidate); + if ( + trackedRemoteName !== null && + trackedRemoteName !== primaryRemoteName && + (yield* remoteBranchExists({ + cwd, + remoteName: trackedRemoteName, + refName: normalizedCandidate, + })) + ) { + copies.push(`${trackedRemoteName}/${normalizedCandidate}`); + } + const [firstCopy, ...otherCopies] = copies; + if (firstCopy !== undefined) { + return otherCopies.length === 0 + ? firstCopy + : yield* closestCopy(cwd, refName, [firstCopy, ...otherCopies]); } }