Fix changes sometimes disappearing entirely

```git-check-assertions
run test/mechanicaldiff.bats
assert_success

git checkout HEAD~ bin
run test/mechanicaldiff.bats
assert_failure
assert_output --partial "not ok 9 keeps matching change with an extra removed line"
assert_output --partial "not ok 10 keeps matching change with an extra added line"
```
This commit is contained in:
Sven van Heugten 2026-03-06 17:01:00 +01:00
parent c55e0276a6
commit 057686a6b9
No known key found for this signature in database
GPG key ID: D612F88666F4F660
4 changed files with 74 additions and 4 deletions

View file

@ -40,11 +40,13 @@ def filter_change_block(
if trim_blank_ends(transformed_removed) == trim_blank_ends(added_lines):
return change_lines
if len(removed_lines) != len(added_lines):
return []
kept_lines = []
for removed, added in zip(removed_lines, added_lines):
max_len = max(len(removed_lines), len(added_lines))
for idx in range(max_len):
removed = removed_lines[idx] if idx < len(removed_lines) else None
added = added_lines[idx] if idx < len(added_lines) else None
if removed is None:
break
if re.sub(search, replace, removed) == added:
kept_lines.append(f"-{removed}")
kept_lines.append(f"+{added}")