Fix ahead count when upstream branch is gone (#1023)

This commit is contained in:
Mohamed Boudra
2026-05-14 19:10:14 +08:00
committed by GitHub
parent 2dd77aae04
commit faa0d93cb8
2 changed files with 32 additions and 1 deletions
+16 -1
View File
@@ -1241,14 +1241,19 @@ async function getAheadOfOrigin(cwd: string, currentBranch: string): Promise<num
if (!currentBranch) {
return null;
}
const trackedOriginBranch = await getTrackedOriginBranch(cwd, currentBranch);
const originBranch = trackedOriginBranch ?? currentBranch;
try {
const { stdout } = await runGitCommand(
["rev-list", "--count", `origin/${currentBranch}..${currentBranch}`],
["rev-list", "--count", `origin/${originBranch}..${currentBranch}`],
{ cwd, envOverlay: READ_ONLY_GIT_ENV },
);
const count = Number.parseInt(stdout.trim(), 10);
return Number.isNaN(count) ? null : count;
} catch {
if (trackedOriginBranch) {
return null;
}
try {
const { stdout } = await runGitCommand(["rev-list", "--count", currentBranch], {
cwd,
@@ -1262,6 +1267,16 @@ async function getAheadOfOrigin(cwd: string, currentBranch: string): Promise<num
}
}
async function getTrackedOriginBranch(cwd: string, currentBranch: string): Promise<string | null> {
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<number | null> {
if (!currentBranch) {
return null;