BlueGit

timfa questward

Files

questward / server / src / deploy / gitRemote.js

gitRemote.js

gitRemote.js 1.4 KiB Download Raw
1import { execFile } from "node:child_process";
2import { promisify } from "node:util";
3
4const execFileAsync = promisify(execFile);
5
6async function git (repoRoot, args)
7{
8 const { stdout } = await execFileAsync("git", args, {
9 cwd: repoRoot,
10 windowsHide: true,
11 maxBuffer: 1024 * 1024
12 });
13 return String(stdout ?? "").trim();
14}
15
16/**
17 * Non-destructive check: fetch origin and see if remote branch is ahead of HEAD.
18 * @returns {Promise<{ ok: true, ahead: boolean, local: string, remote: string }
19 * | { ok: false, error: string }>}
20 */
21export async function checkGitRemoteAhead (repoRoot, branch = "master")
22{
23 try
24 {
25 const local = await git(repoRoot, ["rev-parse", "HEAD"]);
26 await git(repoRoot, ["fetch", "--quiet", "origin", branch]);
27 const remote = await git(repoRoot, ["rev-parse", `origin/${branch}`]);
28 if (!local || !remote)
29 { return { ok: false, error: "Missing git commit hashes." }; }
30 if (local === remote)
31 { return { ok: true, ahead: false, local, remote }; }
32 const counts = await git(repoRoot, ["rev-list", "--left-right", "--count", `HEAD...origin/${branch}`]);
33 // "A\tB" = commits reachable from HEAD not remote, commits from remote not HEAD
34 const parts = counts.split(/\s+/);
35 const behind = Number(parts[1]) || 0;
36 return { ok: true, ahead: behind > 0, local, remote };
37 }
38 catch (error)
39 {
40 return { ok: false, error: error?.message ?? String(error) };
41 }
42}