Count changed lines between two text files using difflib.
(file_a: pathlib.Path, file_b: pathlib.Path)
| 1082 | |
| 1083 | |
| 1084 | def _count_file_diff(file_a: pathlib.Path, file_b: pathlib.Path) -> int: |
| 1085 | """Count changed lines between two text files using difflib.""" |
| 1086 | a_content = safe_read_text(file_a) |
| 1087 | b_content = safe_read_text(file_b) |
| 1088 | if a_content is None or b_content is None: |
| 1089 | return 0 |
| 1090 | if a_content == b_content: |
| 1091 | return 0 |
| 1092 | a_lines = a_content.splitlines() |
| 1093 | b_lines = b_content.splitlines() |
| 1094 | count = 0 |
| 1095 | for line in difflib.unified_diff(a_lines, b_lines, lineterm=""): |
| 1096 | if (line.startswith("+") and not line.startswith("+++")) or ( |
| 1097 | line.startswith("-") and not line.startswith("---") |
| 1098 | ): |
| 1099 | count += 1 |
| 1100 | return count |
| 1101 | |
| 1102 | |
| 1103 | def _count_path_diff(path_a: pathlib.Path, path_b: pathlib.Path) -> int: |
no test coverage detected