import { chmodSync, mkdirSync, writeFileSync } from "node:fs"; import path from "node:path"; // A stand-in for stagit, for the source step's tests (sourceHistory.test.ts, // source.test.ts; the real one is tested where it is installed). It writes // what stagit writes, where stagit writes it (its CWD), the way stagit.c does: // - `-c `: walks from HEAD down to the commit the file names, writes a // log line and a page for each NEW commit, appends the file's old lines, // and rewrites the file (the head, then every line); // - `-l `: a log line for the newest n and " more commits remaining, // fetch the repository"; a page for EVERY commit that has none (-l does // not cap the pages); // - neither: a line and a page for every commit; // - `-c` with `-l`: stagit's usage error; // - and every run: files.html, refs.html, the two feeds (atom.xml carries // the -u base), a per-file page, a leftover in the CWD. A commit page is // never rewritten once it exists. // Each call is appended to `log` as `cache=… limit=… base=… repo=`. // `exit` fails at once (with a stderr line); `extra` goes into every commit // page; `pad` bytes are added to the head's page. export function writeFakeStagit( file: string, log: string, o: { exit?: number; extra?: string; pad?: number } = {}, ): string { mkdirSync(path.dirname(file), { recursive: true }); writeFileSync( file, `#!/bin/sh cache=""; base=""; repo=""; limit="" while [ $# -gt 0 ]; do case "$1" in -c) cache="$2"; shift 2;; -l) limit="$2"; shift 2;; -u) base="$2"; shift 2;; *) repo="$1"; shift;; esac done echo "cache=$cache limit=$limit base=$base repo=$(basename "$repo")" >> '${log}' ${o.exit ? `echo 'stagit: something broke' >&2; exit ${o.exit}` : ""} if [ -n "$cache" ] && [ -n "$limit" ]; then echo 'usage: stagit [-c cachefile | -l commits] [-u baseurl] repodir' >&2; exit 1; fi mkdir -p commit file tip=$(git --git-dir "$repo" rev-parse HEAD) last="" if [ -n "$cache" ] && [ -f "$cache" ]; then last=$(head -n 1 "$cache"); fi : > lines.tmp n=0; rem=0 for c in $(git --git-dir "$repo" rev-list HEAD); do [ "$c" = "$last" ] && break if [ -n "$limit" ] && [ "$n" -ge "$limit" ]; then rem=$((rem + 1)) [ -f "commit/$c.html" ] && continue else n=$((n + 1)) echo "$c" >> lines.tmp fi [ -f "commit/$c.html" ] || printf '\\n\\n\\n\\nREADME ${o.extra ?? ""}\\n\\n\\n' > "commit/$c.html" done if [ -n "$cache" ]; then if [ -n "$last" ]; then tail -n +2 "$cache" >> lines.tmp; fi { echo "$tip"; cat lines.tmp; } > "$cache" fi if [ "$rem" -gt 0 ]; then echo "$rem more commits remaining, fetch the repository" >> lines.tmp; fi { printf '\\n\\n\\n\\n%s %s README \\n\\n' "$(cat "$repo/description")" "$(cat "$repo/url")" cat lines.tmp printf '
\\n\\n\\n' } > log.html rm -f lines.tmp printf '\\n\\n\\n\\nx\\n\\n\\n' > files.html printf '\\n\\n\\n\\n\\n\\n' > refs.html printf '%s file/README.md.html\\n' "$base" > atom.xml printf '\\n' > tags.xml printf 'x\\n' > file/README.md.html touch cache.XXXXleftover ${o.pad ? `head -c ${o.pad} /dev/zero >> "commit/$tip.html"` : ""} exit 0 `, ); chmodSync(file, 0o755); return file; }