Files
gstack/lib/tool-path.ts
T
Garry Tanandcapy-ai[bot] abdc09dad1 Land checks a PR whose base moved in a separate clone, then with full CI, before merging (GST-1766) (#4649)
When main has moved since a PR's last CI run, `gstack-ship-queue land`
now runs its local merge checks in a throwaway clone and can then run
the full CI suite on the merged code before it merges.

Why: the old checks shared the checkout's git directory, so a PR's own
test commands could plant a hook that later ran with land's tokens, and
a PR could reach main on a base its full suite never saw.

Nothing to do: gstack's own policy turns the CI run on; other repos opt
in with `land.merged_ci`.

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>
Review-Finding: p1/red-team#1 reader=red-team
Review-Finding: p1/specialist-batched#1 reader=security
Review-Finding: p1/red-team#2 reader=red-team
Review-Finding: p1/red-team#3 reader=red-team
Review-Finding: p2/specialist-batched#1 reader=security
Review-Finding: p2/red-team#1 reader=red-team
Review-Finding: p2/red-team#2 reader=red-team
Review-Finding: p2/outside-voice#1 reader=outside-voice
Review-Finding: p2/outside-voice#2 reader=outside-voice
Review-Finding: p2/adversarial-native#1 reader=adversarial-native
Review-Finding: p2/adversarial-native#2 reader=adversarial-native
Review-Finding: p2/adversarial-native#3 reader=adversarial-native
Review-Finding: p2/adversarial-native#4 reader=adversarial-native
2026-11-22 01:27:38 +00:00

38 lines
2.1 KiB
TypeScript

/**
* The absolute git and gh a gstack process runs, resolved once per PATH value, at its first git or gh call (GST-1766).
* `gstack-ship-queue land` runs repo commands (land.merged_checks) as its own user between its own git and gh calls, and
* a check can drop a `git` or `gh` into a writable directory early on PATH; a name looked up again after the checks
* would run that file with land's environment. lib/land-merged-checks.ts records the resolved files before the checks
* and refuses when one changed, and before the checks it puts the pinned files' directories first on its own PATH, so
* every later `git` or `gh` any module starts by name runs them. gh runs `git` by name, so its PATH starts with
* the pinned git's directory.
*/
import * as path from 'node:path';
const pinned = new Map<string, string>();
/** The file `name` resolves to on PATH the first time this process asks; the bare name when PATH has none, so the spawn fails as before. */
export function toolPath(name: 'git' | 'gh', PATH = process.env.PATH ?? ''): string {
const key = `${name}\0${PATH}`;
let found = pinned.get(key);
if (found === undefined) {
found = Bun.which(name, { PATH }) ?? name;
pinned.set(key, found);
}
return found;
}
/** PATH with the pinned git's directory first, for a child such as gh that runs `git` by name. */
export function pathWithGit(PATH = process.env.PATH ?? ''): string {
const git = toolPath('git', PATH);
return path.isAbsolute(git) ? `${path.dirname(git)}${path.delimiter}${PATH}` : PATH;
}
/** PATH with the pinned git's and gh's directories first, with those files pinned for it too: land sets it on its own process before the checks, so a module that starts `git` or `gh` by name after them runs the files pinned before them. */
export function pinnedFirst(PATH = process.env.PATH ?? ''): string {
const files = (['git', 'gh'] as const).map(t => [t, toolPath(t, PATH)] as const);
const next = [...new Set(files.filter(([, f]) => path.isAbsolute(f)).map(([, f]) => path.dirname(f))), PATH].join(path.delimiter);
for (const [t, f] of files) pinned.set(`${t}\0${next}`, f);
return next;
}