Skip to content

Commit 99d4ff3

Browse files
committed
More concise, diff-compatible header when changing the first line
1 parent c1a1ed7 commit 99d4ff3

10 files changed

+26
-5
lines changed

src/StringDiff.php

+7-5
Original file line numberDiff line numberDiff line change
@@ -130,12 +130,14 @@ private function getUnifiedDiffHunk(
130130
invariant($old_start !== null, 'failed to find an old pos');
131131
invariant($new_start !== null, 'failed to find a new pos');
132132

133+
$format = ($start, $lines) ==> ($start === 1 && $lines === 1)
134+
? '1'
135+
: Str\format('%d,%d', $start, $lines);
136+
133137
return Str\format(
134-
"@@ -%d,%d +%d,%d @@\n",
135-
$old_start + 1,
136-
$old_lines,
137-
$new_start + 1,
138-
$new_lines,
138+
"@@ -%s +%s @@\n",
139+
$format($old_start + 1, $old_lines),
140+
$format($new_start + 1, $new_lines),
139141
).Str\join($lines, "\n")."\n";
140142
}
141143
}

tests/examples/one_line_to_two.a

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Foo

tests/examples/one_line_to_two.b

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Bar
2+
Baz
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
@@ -1 +1,2 @@
2+
-Foo
3+
+Bar
4+
+Baz

tests/examples/replace_word.a

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Framework

tests/examples/replace_word.b

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Facebook
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
@@ -1 +1 @@
2+
-Framework
3+
+Facebook

tests/examples/two_lines_to_one.a

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Foo
2+
Bar

tests/examples/two_lines_to_one.b

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Baz
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
@@ -1,2 +1 @@
2+
-Foo
3+
-Bar
4+
+Baz

0 commit comments

Comments
 (0)