From faa0d93cb8dc1016161f9d149a5d278b80c44976 Mon Sep 17 00:00:00 2001 From: Mohamed Boudra Date: Thu, 14 May 2026 19:10:14 +0800 Subject: [PATCH] Fix ahead count when upstream branch is gone (#1023) --- packages/server/src/utils/checkout-git.test.ts | 16 ++++++++++++++++ packages/server/src/utils/checkout-git.ts | 17 ++++++++++++++++- 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/packages/server/src/utils/checkout-git.test.ts b/packages/server/src/utils/checkout-git.test.ts index 82dc3d199..7cda4a9c6 100644 --- a/packages/server/src/utils/checkout-git.test.ts +++ b/packages/server/src/utils/checkout-git.test.ts @@ -378,6 +378,22 @@ const x = 1; expect(divergedStatus.behindOfOrigin).toBe(1); }); + it("does not report the full branch history as ahead when the current branch remote is gone", async () => { + setupRemoteTrackingMain(repoDir, tempDir); + execFileSync("git", ["checkout", "-b", "feature"], { cwd: repoDir }); + commitFile(repoDir, "feature.txt", "feature\n", "feature commit"); + execFileSync("git", ["push", "-u", "origin", "feature"], { cwd: repoDir }); + execFileSync("git", ["push", "origin", "--delete", "feature"], { cwd: repoDir }); + execFileSync("git", ["fetch", "--prune", "origin"], { cwd: repoDir }); + + const status = await getCheckoutStatus(repoDir); + expect(status.isGit).toBe(true); + if (!status.isGit) { + return; + } + expect(status.aheadOfOrigin).toBeNull(); + }); + it("does not report incoming additions when the base branch is behind its remote", async () => { const { cloneDir } = setupRemoteTrackingMain(repoDir, tempDir); commitFile(cloneDir, "file.txt", "remote one\nremote two\n", "remote update"); diff --git a/packages/server/src/utils/checkout-git.ts b/packages/server/src/utils/checkout-git.ts index 684e4ea4a..8a1ca0f1c 100644 --- a/packages/server/src/utils/checkout-git.ts +++ b/packages/server/src/utils/checkout-git.ts @@ -1241,14 +1241,19 @@ async function getAheadOfOrigin(cwd: string, currentBranch: string): Promise { + const remoteName = await getGitConfigValue(cwd, `branch.${currentBranch}.remote`); + if (remoteName !== "origin") { + return null; + } + + const mergeRef = await getGitConfigValue(cwd, `branch.${currentBranch}.merge`); + return parseBranchMergeHeadRef(mergeRef); +} + async function getBehindOfOrigin(cwd: string, currentBranch: string): Promise { if (!currentBranch) { return null;