Ignore leading and trailing blank lines for comparison

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

git checkout HEAD~ bin
run test/mechanicaldiff.bats
assert_failure
assert_output --partial "-- command failed --"
```
This commit is contained in:
Sven van Heugten 2026-03-06 04:08:21 +01:00
parent 3d814b5b15
commit 0b3d0d8f46
No known key found for this signature in database
GPG key ID: D612F88666F4F660
2 changed files with 29 additions and 1 deletions

View file

@ -4,6 +4,16 @@ import re
import sys
def trim_blank_ends(lines: list[str]) -> list[str]:
start = 0
end = len(lines)
while start < end and lines[start] == "\n":
start += 1
while end > start and lines[end - 1] == "\n":
end -= 1
return lines[start:end]
def filter_change_block(
change_lines: list[str], search: str, replace: str
) -> list[str]:
@ -27,7 +37,7 @@ def filter_change_block(
transformed_removed = [
re.sub(search, replace, line) for line in removed_lines
]
if transformed_removed == added_lines:
if trim_blank_ends(transformed_removed) == trim_blank_ends(added_lines):
return change_lines
if len(removed_lines) != len(added_lines):

View file

@ -143,6 +143,24 @@ setup() {
assert_output "$(cat "$BATS_TEST_TMPDIR/diff")"
}
@test "ignores leading and trailing blank lines for comparison" {
cat >"$BATS_TEST_TMPDIR/old" <<-'EOF'
prefix foo suffix
EOF
cat >"$BATS_TEST_TMPDIR/new" <<-'EOF'
prefix bar suffix
EOF
git diff --no-index "$BATS_TEST_TMPDIR/old" "$BATS_TEST_TMPDIR/new" \
>"$BATS_TEST_TMPDIR/diff" || true
run mechanicaldiff.py foo bar <"$BATS_TEST_TMPDIR/diff"
assert_success
assert_output "$(cat "$BATS_TEST_TMPDIR/diff")"
}
@test "keeps only matching line in a consecutive change block" {
cat >"$BATS_TEST_TMPDIR/old" <<-'EOF'
foo