refactor: simplify pass (trekexecute)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Kjell Tore Guttormsen 2026-10-26 20:39:40 +01:00
commit 52c382c198
Signed by: ktg
SSH key fingerprint: SHA256:JakMjO6FTBBzN0Bhfj9saOoEjaFxlSdYuZQQpM/lF9Q
8 changed files with 45 additions and 52 deletions

View file

@ -86,12 +86,20 @@ function gitFailed(what, r) {
const short = (c) => c.slice(0, 12);
/** Where the managed block's start marker (`s`) and end marker (`e`) are; null when either is missing. */
function blockBounds(text) {
const s = text.indexOf(MARK_START);
if (s === -1) return null;
const e = text.indexOf(MARK_END, s + MARK_START.length);
if (e === -1) return null;
return { s, e };
}
/** The lessons text with its managed block (start marker to end marker, plus its newline) taken out. */
function outsideBlock(text) {
const s = text.indexOf(MARK_START);
if (s === -1) return text;
const e = text.indexOf(MARK_END, s + MARK_START.length);
if (e === -1) return text;
const b = blockBounds(text);
if (!b) return text;
const { s, e } = b;
let end = e + MARK_END.length;
if (text.startsWith('\r\n', end)) end += 2;
else if (text[end] === '\n') end += 1;
@ -100,11 +108,9 @@ function outsideBlock(text) {
/** The non-blank lines inside the managed block (the same block `outsideBlock` takes out); none without one. */
function blockLines(text) {
const s = text.indexOf(MARK_START);
if (s === -1) return [];
const e = text.indexOf(MARK_END, s + MARK_START.length);
if (e === -1) return [];
return text.slice(s + MARK_START.length, e).split(/\r?\n/).filter((l) => l.trim() !== '');
const b = blockBounds(text);
if (!b) return [];
return text.slice(b.s + MARK_START.length, b.e).split(/\r?\n/).filter((l) => l.trim() !== '');
}
/**

View file

@ -98,8 +98,10 @@ function headIsUnborn(root) {
if (sym.error || sym.status !== 0) return false;
const ref = String(sym.stdout ?? '').trim();
if (!/^refs\/heads\/./.test(ref)) return false;
if (lexists(resolve(root, git(root, ['rev-parse', '--git-path', ref]).trim()))) return false;
const packed = resolve(root, git(root, ['rev-parse', '--git-path', 'packed-refs']).trim());
// One call, one output line per --git-path, in argument order.
const [loosePath = '', packedPath = ''] = git(root, ['rev-parse', '--git-path', ref, '--git-path', 'packed-refs']).split('\n');
if (lexists(resolve(root, loosePath.trim()))) return false;
const packed = resolve(root, packedPath.trim());
if (lexists(packed)) {
const lines = readFileSync(packed, 'utf8').split('\n');
if (lines.some((l) => l.endsWith(` ${ref}`))) return false;

View file

@ -36,7 +36,7 @@ import { FULL_SHA_RE } from '../review/reviewed-commit.mjs';
*/
export function doneLineRe(step, commit) {
const sha = commit === undefined || commit === null || commit === ''
? '(?:[0-9a-f]{40}|[0-9a-f]{64})'
? FULL_SHA_RE.source.slice(1, -1) // the same 40-or-64 alternation, without its ^ and $
: FULL_SHA_RE.test(String(commit)) ? String(commit).toLowerCase() : '(?!)';
return new RegExp(`^DONE step ${step}: \`.+\` exit 0 at ${sha} \\d{4}-\\d{2}-\\d{2}T\\S+$`, 'm');
}