parse_diff.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. __copyright__ = """
  2. Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  3. Exceptions. See /LICENSE for license information.
  4. SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  5. """
  6. import sys
  7. from collections import defaultdict
  8. from typing import TextIO, Dict, List
  9. def parse_diff(stream: TextIO) -> None:
  10. current_file: str = ""
  11. file_changes: Dict[str, Dict[str, List[str]]] = defaultdict(
  12. lambda: {"input": [], "stderr": [], "stdout": []}
  13. )
  14. for line in stream:
  15. if line.startswith("diff --git"):
  16. parts = line.split()
  17. if len(parts) >= 4:
  18. current_file = (
  19. parts[3][2:] if parts[3].startswith("b/") else parts[3]
  20. )
  21. elif line.startswith("+") or line.startswith("-"):
  22. if not line.startswith("+++") and not line.startswith("---"):
  23. stripped = line[1:].strip()
  24. if stripped.startswith("// CHECK:STDERR"):
  25. file_changes[current_file]["stderr"].append(
  26. line.rstrip("\n")
  27. )
  28. elif stripped.startswith("// CHECK:STDOUT"):
  29. file_changes[current_file]["stdout"].append(
  30. line.rstrip("\n")
  31. )
  32. elif stripped.startswith("// CHECK"):
  33. file_changes[current_file]["stdout"].append(
  34. line.rstrip("\n")
  35. )
  36. else:
  37. file_changes[current_file]["input"].append(
  38. line.rstrip("\n")
  39. )
  40. for f, c in file_changes.items():
  41. if not c["input"] and not c["stderr"] and not c["stdout"]:
  42. continue
  43. print(f"File: {f}")
  44. if c["input"]:
  45. print(" --- Input Changes ---")
  46. for change in c["input"]:
  47. print(f" {change}")
  48. if c["stderr"]:
  49. print(" --- STDERR Changes ---")
  50. for change in c["stderr"]:
  51. print(f" {change}")
  52. if c["stdout"]:
  53. print(" --- STDOUT Changes ---")
  54. for change in c["stdout"]:
  55. print(f" {change}")
  56. print("-" * 40)
  57. if __name__ == "__main__":
  58. parse_diff(sys.stdin)