From 8b45ac3d873ef27351bf07a7e617b608eac80a5d Mon Sep 17 00:00:00 2001 From: Sven van Heugten Date: Thu, 5 Mar 2026 17:27:58 +0100 Subject: [PATCH] Add main implementation with a placeholder for should_include_hunk --- bin/mechanicaldiff.py | 57 ++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 54 insertions(+), 3 deletions(-) diff --git a/bin/mechanicaldiff.py b/bin/mechanicaldiff.py index 749b25a..6a6166c 100755 --- a/bin/mechanicaldiff.py +++ b/bin/mechanicaldiff.py @@ -3,9 +3,60 @@ import sys -def main(): - data = sys.stdin.read() - sys.stdout.write(data) +def should_include_hunk(hunk_text: str) -> bool: + return True + + +def main() -> None: + lines = sys.stdin.read().splitlines(keepends=True) + preamble_lines = [] + sections = [] + current = None + + for line in lines: + if line.startswith("diff --git "): + if current is not None: + sections.append(current) + current = {"header": [line], "hunks": []} + continue + + if current is None: + preamble_lines.append(line) + continue + + if line.startswith("@@ "): + current["hunks"].append([line]) + else: + if current["hunks"]: + current["hunks"][-1].append(line) + else: + current["header"].append(line) + + if current is not None: + sections.append(current) + + output_lines = [] + output_lines.extend(preamble_lines) + + for section in sections: + if not section["hunks"]: + output_lines.extend(section["header"]) + continue + + kept_hunks = [] + for hunk_lines in section["hunks"]: + hunk_text = "".join(hunk_lines) + if should_include_hunk(hunk_text): + kept_hunks.append(hunk_lines) + + if not kept_hunks: + continue + + output_lines.extend(section["header"]) + for hunk_lines in kept_hunks: + output_lines.extend(hunk_lines) + + sys.stdout.write("".join(output_lines)) if __name__ == "__main__":