allow custom diff header labels

This commit is contained in:
ewin 2025-09-04 23:05:54 -04:00
parent 436acf7299
commit 4f17754f4c
Signed by: erin
SSH key fingerprint: SHA256:swjoHhREbZPbWe+gyJNi24d4NAxJSyUIm3fpZj4z3wc

View file

@ -13,16 +13,25 @@ const toBase64 = str => Buffer.from(str, 'utf-8').toString('base64');
* terrible terrible terrible string diff helper for debugging
* @param {string} a
* @param {string} b
* @param {object} page Page object from the API, used to generate diff labels
*/
export function diff (a, b) {
export function diff (a, b, page) {
let labels = null;
if (page) { // generate diff labels based on
labels = [
`${page.title} (${page.revid ? `revision ${page.revid}` : 'current revision'}${page.timestamp ? ` from ${page.timestamp}` : ''})`,
`${page.title} (pending edit)`,
];
}
// base64 input strings before passing to shell to avoid escaping issues
// https://stackoverflow.com/a/60221847
// use tail to cut off useless file info lines
execSync(String.raw`bash <<- EOF
// use tail to cut off file info lines and re-add with fake filenames/dates
// this function is so extra now holy shit
execSync(String.raw`bash <<- 'EOF'
diff --color=always -u \
<(echo "${toBase64(a)}" | base64 -d) \
<(echo "${toBase64(b)}" | base64 -d) \
| tail -n +3
${labels ? `--label="$(echo '${toBase64(labels[0])}' | base64 -d)" ` : ''}<(echo '${toBase64(a)}' | base64 -d) \
${labels ? `--label="$(echo '${toBase64(labels[1])}' | base64 -d)" ` : ''}<(echo '${toBase64(b)}' | base64 -d) \
${labels ? `|| true` : `| tail -n +3` /* cut off header if no labels */}
EOF`, {
// display result directly in terminal
stdio: 'inherit',