repositories / pi-ext
pi-ext
bugabingas pi extensions
owned by bugabinga
extensions/diff-review/args.ts
RawFile outline
import type { ReviewSource } from "./types.js";
export type DiffReviewArgs = {
staged: boolean;
base?: string;
files?: string[];
};
export function parseArgs(input: string): DiffReviewArgs {
const tokens = tokenize(input);
const args: DiffReviewArgs = { staged: false };
for (let i = 0; i < tokens.length; i++) {
const token = tokens[i];
if (token === "--staged") {
args.staged = true;
continue;
}
if (token === "--base") {
const value = tokens[++i];
if (!value) throw new Error("--base requires a ref");
args.base = parseBaseRef(value);
continue;
}
if (token.startsWith("--base=")) {
args.base = parseBaseRef(token.slice("--base=".length));
continue;
}
if (token === "--files") {
const value = tokens[++i];
if (!value) throw new Error("--files requires a comma-separated list");
args.files = parseFiles(value);
continue;
}
if (token.startsWith("--files=")) {
args.files = parseFiles(token.slice("--files=".length));
continue;
}
throw new Error(`Unknown argument: ${token}`);
}
if (args.staged && args.base)
throw new Error("--staged and --base are mutually exclusive");
return args;
}
export function sourceFromArgs(args: DiffReviewArgs): ReviewSource {
if (args.base)
return {
kind: "base",
base: args.base,
...(args.files && { files: args.files }),
};
if (args.staged)
return { kind: "staged", ...(args.files && { files: args.files }) };
return { kind: "working-tree", ...(args.files && { files: args.files }) };
}
function parseBaseRef(value: string): string {
if (!value) throw new Error("--base requires a ref");
if (value.startsWith("-"))
throw new Error("--base must be a git revision, not an option");
if (value.includes("\0")) throw new Error("--base cannot contain NUL");
return value;
}
function parseFiles(value: string): string[] {
const files = value
.split(",")
.map((file) => file.trim())
.filter(Boolean);
if (files.length === 0) throw new Error("--files requires at least one path");
for (const file of files) validatePathFilter(file);
return files;
}
function validatePathFilter(path: string) {
if (path.startsWith("/") || /^[a-zA-Z]:[\\/]/.test(path))
throw new Error(`Path must be relative: ${path}`);
if (path.split(/[\\/]+/).includes(".."))
throw new Error(`Path cannot contain '..': ${path}`);
if (path.includes("\0")) throw new Error("Path cannot contain NUL");
}
function tokenize(input: string): string[] {
const tokens: string[] = [];
let current = "";
let quote: '"' | "'" | undefined;
let escaping = false;
for (const ch of input.trim()) {
if (escaping) {
current += ch;
escaping = false;
continue;
}
if (ch === "\\" && quote !== "'") {
escaping = true;
continue;
}
if ((ch === '"' || ch === "'") && !quote) {
quote = ch;
continue;
}
if (ch === quote) {
quote = undefined;
continue;
}
if (/\s/.test(ch) && !quote) {
if (current) tokens.push(current);
current = "";
continue;
}
current += ch;
}
if (escaping) current += "\\";
if (quote) throw new Error("Unclosed quote in arguments");
if (current) tokens.push(current);
return tokens;
}