mechanicaldiff/bin/mechanicaldiff.py

63 lines
1.5 KiB
Python
Executable file

#!/usr/bin/env python3
import sys
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__":
main()