Files
openclaw/scripts/lib/merge-head-diff-base.mjs
T
Peter Steinberger c70aee247e refactor(scripts): migrate JavaScript tools to TypeScript (#121005)
* refactor(scripts): migrate JavaScript tools to TypeScript

* fix(ci): keep changed-scope preflight zero-install

* fix(ci): preserve zero-install script owners

* fix(ci): complete script migration follow-through

* fix(release): keep stable closeout zero-install

* fix(scripts): preserve standalone execution boundaries

* fix(scripts): repair standalone loader boundaries

* fix(scripts): normalize gateway observation ids

* fix(scripts): keep Docker packager standalone

* test(scripts): preserve rebase cleanup helpers

* test(sessions): use tracked temp directory
2026-08-09 07:21:35 -07:00

129 lines
3.2 KiB
JavaScript

// Resolves the diff base for merge commits when first-parent comparison is requested.
import { execFileSync } from "node:child_process";
import { pathToFileURL } from "node:url";
const DEFAULT_GIT_OUTPUT_MAX_BUFFER = 16 * 1024 * 1024;
/**
* Resolve the git base ref to use when diffing a merge head.
* @param {{base: string, head?: string, cwd?: string, maxBuffer?: number, preferFirstParent?: boolean}} params
* @returns {string}
*/
export function resolveMergeHeadDiffBase({
base,
head = "HEAD",
cwd = process.cwd(),
maxBuffer = DEFAULT_GIT_OUTPUT_MAX_BUFFER,
preferFirstParent = false,
}) {
if (!base) {
return "";
}
if (!preferFirstParent) {
return base;
}
const parents = listCommitParents({ ref: head, cwd, maxBuffer });
if (parents.length < 2) {
return base;
}
const firstParent = resolveCommit({ ref: parents[0], cwd, maxBuffer });
const explicitBase = resolveCommit({ ref: base, cwd, maxBuffer });
if (!firstParent || firstParent === explicitBase) {
return base;
}
return firstParent;
}
/**
* @param {{ref: string, cwd: string, maxBuffer: number}} params
* @returns {string[]}
*/
function listCommitParents({ ref, cwd, maxBuffer }) {
try {
const output = execFileSync("git", ["rev-list", "--parents", "-n", "1", ref], {
cwd,
stdio: ["ignore", "pipe", "ignore"],
encoding: "utf8",
maxBuffer,
}).trim();
return output.split(/\s+/u).slice(1);
} catch {
return [];
}
}
/**
* @param {{ref: string, cwd: string, maxBuffer: number}} params
* @returns {string}
*/
function resolveCommit({ ref, cwd, maxBuffer }) {
try {
return execFileSync("git", ["rev-parse", "--verify", `${ref}^{commit}`], {
cwd,
stdio: ["ignore", "pipe", "ignore"],
encoding: "utf8",
maxBuffer,
}).trim();
} catch {
return "";
}
}
/**
* @param {readonly string[]} argv
* @param {number} index
* @param {string} optionName
* @returns {string}
*/
function readRefValue(argv, index, optionName) {
const value = argv[index + 1];
if (value === undefined || value === "" || value.startsWith("-")) {
throw new Error(`${optionName} requires a value`);
}
return value;
}
/**
* @internal Directly tested script implementation detail.
* @param {readonly string[]} argv
* @returns {{base: string, head: string, preferFirstParent: boolean}}
*/
export function parseArgs(argv) {
const args = {
base: "",
head: "HEAD",
preferFirstParent: false,
};
for (let index = 0; index < argv.length; index += 1) {
const arg = argv[index];
if (arg === "--base") {
args.base = readRefValue(argv, index, "--base");
index += 1;
continue;
}
if (arg === "--head") {
args.head = readRefValue(argv, index, "--head");
index += 1;
continue;
}
if (arg === "--prefer-first-parent") {
args.preferFirstParent = true;
}
}
return args;
}
if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) {
const args = parseArgs(process.argv.slice(2));
process.stdout.write(
`${resolveMergeHeadDiffBase({
base: args.base,
head: args.head,
preferFirstParent: args.preferFirstParent,
})}\n`,
);
}