|
| 1 | +import { createRequire } from 'node:module'; var require = createRequire(import.meta.url); |
| 2 | + |
| 3 | +// src/filter-diff/filter-diff.ts |
| 4 | +import { existsSync, readFileSync, rmSync, writeFileSync } from "fs"; |
| 5 | +function parseExcludePrefixes(excludePathsStr) { |
| 6 | + return excludePathsStr.split("\n").map((p) => p.replace(/\r/g, "").trim()).filter((p) => p.length > 0); |
| 7 | +} |
| 8 | +function filterDiff(diffContent, excludePrefixes) { |
| 9 | + const prefixes = excludePrefixes.filter((p) => p.length > 0); |
| 10 | + if (prefixes.length === 0 || diffContent === "") { |
| 11 | + const remainingCount2 = (diffContent.match(/^diff --git /gm) ?? []).length; |
| 12 | + return { filtered: diffContent, excludedFiles: [], remainingCount: remainingCount2 }; |
| 13 | + } |
| 14 | + const isExcluded = (filePath) => prefixes.some((prefix) => filePath.startsWith(prefix)); |
| 15 | + const lines = diffContent.split("\n"); |
| 16 | + const outputLines = []; |
| 17 | + let sectionLines = []; |
| 18 | + let skip = false; |
| 19 | + const excludedFiles = []; |
| 20 | + let remainingCount = 0; |
| 21 | + const flushSection = () => { |
| 22 | + if (sectionLines.length === 0) return; |
| 23 | + if (!skip) { |
| 24 | + for (const l of sectionLines) outputLines.push(l); |
| 25 | + remainingCount++; |
| 26 | + } |
| 27 | + sectionLines = []; |
| 28 | + skip = false; |
| 29 | + }; |
| 30 | + for (const line of lines) { |
| 31 | + if (line.startsWith("diff --git ")) { |
| 32 | + flushSection(); |
| 33 | + sectionLines.push(line); |
| 34 | + } else if (sectionLines.length > 0) { |
| 35 | + if (!skip) { |
| 36 | + let filePath = null; |
| 37 | + if (line.startsWith("--- a/")) { |
| 38 | + filePath = line.slice(6); |
| 39 | + } else if (line.startsWith("+++ b/")) { |
| 40 | + filePath = line.slice(6); |
| 41 | + } else if (line.startsWith("rename to ")) { |
| 42 | + filePath = line.slice(10); |
| 43 | + } |
| 44 | + if (filePath !== null && isExcluded(filePath)) { |
| 45 | + skip = true; |
| 46 | + excludedFiles.push(filePath); |
| 47 | + } |
| 48 | + } |
| 49 | + sectionLines.push(line); |
| 50 | + } else { |
| 51 | + outputLines.push(line); |
| 52 | + } |
| 53 | + } |
| 54 | + flushSection(); |
| 55 | + return { |
| 56 | + filtered: outputLines.join("\n"), |
| 57 | + excludedFiles, |
| 58 | + remainingCount |
| 59 | + }; |
| 60 | +} |
| 61 | +function applyFilter(diffPath2, excludePathsStr) { |
| 62 | + const prefixes = parseExcludePrefixes(excludePathsStr); |
| 63 | + if (prefixes.length === 0) { |
| 64 | + process.stderr.write("\u2139\uFE0F No valid prefixes in exclude-paths \u2014 skipping filter\n"); |
| 65 | + return; |
| 66 | + } |
| 67 | + process.stderr.write(`\u{1F50D} Filtering diff against ${prefixes.length} excluded prefix(es): |
| 68 | +`); |
| 69 | + for (const p of prefixes) { |
| 70 | + process.stderr.write(` - ${p} |
| 71 | +`); |
| 72 | + } |
| 73 | + const diffContent = readFileSync(diffPath2, "utf-8"); |
| 74 | + const result = filterDiff(diffContent, prefixes); |
| 75 | + for (const file of result.excludedFiles) { |
| 76 | + process.stderr.write(`\u23ED\uFE0F Excluded from review: ${file} |
| 77 | +`); |
| 78 | + } |
| 79 | + process.stderr.write( |
| 80 | + `\u2705 Filtered diff: ${result.excludedFiles.length} files excluded, ${result.remainingCount} files remaining |
| 81 | +` |
| 82 | + ); |
| 83 | + if (result.remainingCount === 0) { |
| 84 | + if (existsSync(diffPath2)) rmSync(diffPath2); |
| 85 | + process.stderr.write( |
| 86 | + "\u2139\uFE0F All files excluded \u2014 removed pr.diff so downstream diff steps are skipped\n" |
| 87 | + ); |
| 88 | + } else { |
| 89 | + writeFileSync(diffPath2, result.filtered, "utf-8"); |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +// src/filter-diff/index.ts |
| 94 | +var [, , diffPath, excludePathsArg] = process.argv; |
| 95 | +if (!diffPath) { |
| 96 | + process.stderr.write("Usage: filter-diff <diffPath> <excludePaths>\n"); |
| 97 | + process.exit(1); |
| 98 | +} |
| 99 | +try { |
| 100 | + applyFilter(diffPath, excludePathsArg ?? ""); |
| 101 | +} catch (err) { |
| 102 | + process.stderr.write(`Error: ${err instanceof Error ? err.message : String(err)} |
| 103 | +`); |
| 104 | + process.exit(1); |
| 105 | +} |
0 commit comments