fix(git): show Sync only when branch diverges from origin

This commit is contained in:
Mohamed Boudra
2026-02-18 20:36:24 +07:00
parent e8c4ae2024
commit 62d69410d3
8 changed files with 157 additions and 6 deletions
@@ -101,6 +101,43 @@ describe("checkout git utilities", () => {
}
});
it("reports ahead/behind relative to origin on the base branch", async () => {
const remoteDir = join(tempDir, "remote.git");
const cloneDir = join(tempDir, "clone");
execSync(`git init --bare -b main ${remoteDir}`);
execSync(`git remote add origin ${remoteDir}`, { cwd: repoDir });
execSync("git push -u origin main", { cwd: repoDir });
execSync(`git clone ${remoteDir} ${cloneDir}`);
execSync("git config user.email 'test@test.com'", { cwd: cloneDir });
execSync("git config user.name 'Test'", { cwd: cloneDir });
writeFileSync(join(cloneDir, "file.txt"), "remote\n");
execSync("git add file.txt", { cwd: cloneDir });
execSync("git -c commit.gpgsign=false commit -m 'remote update'", { cwd: cloneDir });
execSync("git push", { cwd: cloneDir });
execSync("git fetch origin", { cwd: repoDir });
const behindStatus = await getCheckoutStatus(repoDir);
expect(behindStatus.isGit).toBe(true);
if (!behindStatus.isGit) {
return;
}
expect(behindStatus.aheadOfOrigin).toBe(0);
expect(behindStatus.behindOfOrigin).toBe(1);
writeFileSync(join(repoDir, "local.txt"), "local\n");
execSync("git add local.txt", { cwd: repoDir });
execSync("git -c commit.gpgsign=false commit -m 'local update'", { cwd: repoDir });
const divergedStatus = await getCheckoutStatus(repoDir);
expect(divergedStatus.isGit).toBe(true);
if (!divergedStatus.isGit) {
return;
}
expect(divergedStatus.aheadOfOrigin).toBe(1);
expect(divergedStatus.behindOfOrigin).toBe(1);
});
it("commits messages with quotes safely", async () => {
const message = `He said "hello" and it's fine`;
writeFileSync(join(repoDir, "file.txt"), "quoted\n");
+25 -4
View File
@@ -441,6 +441,7 @@ export type CheckoutStatusGitNonPaseo = {
baseRef: string | null;
aheadBehind: AheadBehind | null;
aheadOfOrigin: number | null;
behindOfOrigin: number | null;
hasRemote: boolean;
remoteUrl: string | null;
isPaseoOwnedWorktree: false;
@@ -455,6 +456,7 @@ export type CheckoutStatusGitPaseo = {
baseRef: string;
aheadBehind: AheadBehind | null;
aheadOfOrigin: number | null;
behindOfOrigin: number | null;
hasRemote: boolean;
remoteUrl: string | null;
isPaseoOwnedWorktree: true;
@@ -838,6 +840,22 @@ async function getAheadOfOrigin(cwd: string, currentBranch: string): Promise<num
}
}
async function getBehindOfOrigin(cwd: string, currentBranch: string): Promise<number | null> {
if (!currentBranch) {
return null;
}
try {
const { stdout } = await execAsync(
`git rev-list --count ${currentBranch}..origin/${currentBranch}`,
{ cwd, env: READ_ONLY_GIT_ENV }
);
const count = Number.parseInt(stdout.trim(), 10);
return Number.isNaN(count) ? null : count;
} catch {
return null;
}
}
type CheckoutInspectionContext = {
worktreeRoot: string;
currentBranch: string | null;
@@ -996,10 +1014,11 @@ export async function getCheckoutStatus(
const isDirty = await isWorkingTreeDirty(cwd);
const hasRemote = remoteUrl !== null;
const baseRef = configured.baseRef ?? (await resolveBaseRef(cwd));
const aheadBehind =
baseRef && currentBranch ? await getAheadBehind(cwd, baseRef, currentBranch) : null;
const aheadOfOrigin =
hasRemote && currentBranch ? await getAheadOfOrigin(cwd, currentBranch) : null;
const [aheadBehind, aheadOfOrigin, behindOfOrigin] = await Promise.all([
baseRef && currentBranch ? getAheadBehind(cwd, baseRef, currentBranch) : Promise.resolve(null),
hasRemote && currentBranch ? getAheadOfOrigin(cwd, currentBranch) : Promise.resolve(null),
hasRemote && currentBranch ? getBehindOfOrigin(cwd, currentBranch) : Promise.resolve(null),
]);
if (configured.isPaseoOwnedWorktree) {
const mainRepoRoot = await getMainRepoRoot(cwd);
@@ -1012,6 +1031,7 @@ export async function getCheckoutStatus(
baseRef: configured.baseRef,
aheadBehind,
aheadOfOrigin,
behindOfOrigin,
hasRemote,
remoteUrl,
isPaseoOwnedWorktree: true,
@@ -1026,6 +1046,7 @@ export async function getCheckoutStatus(
baseRef,
aheadBehind,
aheadOfOrigin,
behindOfOrigin,
hasRemote,
remoteUrl,
isPaseoOwnedWorktree: false,