Last active
December 4, 2025 21:31
-
-
Save melver/f486222c8f191bbbb8c64a5492bb041b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python | |
| # SPDX-License-Identifier: Apache-2.0 | |
| # Copyright 2025 Google LLC. | |
| # Author: Marco Elver | |
| import sys | |
| from collections import Counter | |
| def main(): | |
| """ | |
| Reads a diff from stdin and checks if lines were simply moved around. | |
| Ignores blank lines and leading/trailing whitespace. | |
| """ | |
| removed = Counter() | |
| added = Counter() | |
| for line in sys.stdin: | |
| line = line.rstrip('\n') | |
| # Skip git diff header lines | |
| if line.startswith('diff --git'): continue | |
| if line.startswith('index '): continue | |
| if line.startswith('old mode '): continue | |
| if line.startswith('new mode '): continue | |
| if line.startswith('new file mode'): continue | |
| if line.startswith('deleted file mode'): continue | |
| if line.startswith('similarity index'): continue | |
| if line.startswith('copy from'): continue | |
| if line.startswith('copy to'): continue | |
| if line.startswith('rename from'): continue | |
| if line.startswith('rename to'): continue | |
| if line.startswith('--- '): continue | |
| if line.startswith('+++ '): continue | |
| if line.startswith('@@ '): continue | |
| if line.startswith(r'\ No newline'): continue | |
| # Check for added/removed lines | |
| if line.startswith('-'): | |
| content = line[1:].strip() | |
| if content: # Ignore blank lines | |
| removed[content] += 1 | |
| elif line.startswith('+'): | |
| content = line[1:].strip() | |
| if content: # Ignore blank lines | |
| added[content] += 1 | |
| # Calculate differences | |
| lost_lines = removed - added | |
| extra_lines = added - removed | |
| if not lost_lines and not extra_lines: | |
| print("OK: No lines lost or added (ignoring whitespace).") | |
| sys.exit(0) | |
| found_issues = False | |
| if lost_lines: | |
| found_issues = True | |
| print("--- LOST LINES (Removed but not re-added) ---") | |
| for line, count in lost_lines.items(): | |
| prefix = f"{count} copies" if count > 1 else "1 copy" | |
| print(f"[{prefix}] {line}") | |
| print() | |
| if extra_lines: | |
| found_issues = True | |
| print("--- ADDED LINES (Added but not present originally) ---") | |
| for line, count in extra_lines.items(): | |
| prefix = f"{count} copies" if count > 1 else "1 copy" | |
| print(f"[{prefix}] {line}") | |
| if found_issues: | |
| sys.exit(1) | |
| if __name__ == "__main__": | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment