Skip to content

Commit b3e0fcf

Browse files
phillipwoodgitster
authored andcommitted
add -p: fix counting when splitting and coalescing
When a file has no trailing new line at the end diff records this by appending "\ No newline at end of file" below the last line of the file. This line should not be counted in the hunk header. Fix the splitting and coalescing code to count files without a trailing new line properly and change one of the tests to test splitting without a trailing new line. Signed-off-by: Phillip Wood <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 2b8ea7f commit b3e0fcf

File tree

2 files changed

+34
-8
lines changed

2 files changed

+34
-8
lines changed

git-add--interactive.perl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -793,6 +793,11 @@ sub split_hunk {
793793
while (++$i < @$text) {
794794
my $line = $text->[$i];
795795
my $display = $display->[$i];
796+
if ($line =~ /^\\/) {
797+
push @{$this->{TEXT}}, $line;
798+
push @{$this->{DISPLAY}}, $display;
799+
next;
800+
}
796801
if ($line =~ /^ /) {
797802
if ($this->{ADDDEL} &&
798803
!defined $next_hunk_start) {
@@ -891,6 +896,9 @@ sub merge_hunk {
891896
$n_cnt++;
892897
push @line, $line;
893898
next;
899+
} elsif ($line =~ /^\\/) {
900+
push @line, $line;
901+
next;
894902
}
895903

896904
last if ($o1_ofs <= $ofs);
@@ -909,6 +917,9 @@ sub merge_hunk {
909917
$n_cnt++;
910918
push @line, $line;
911919
next;
920+
} elsif ($line =~ /^\\/) {
921+
push @line, $line;
922+
next;
912923
}
913924
$ofs++;
914925
$o_cnt++;

t/t3701-add-interactive.sh

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -237,31 +237,46 @@ test_expect_success 'setup patch' '
237237
baseline
238238
content
239239
+lastline
240+
\ No newline at end of file
240241
EOF
241242
'
242243

243-
# Expected output, similar to the patch but w/ diff at the top
244+
# Expected output, diff is similar to the patch but w/ diff at the top
244245
test_expect_success 'setup expected' '
245-
cat >expected <<-\EOF
246-
diff --git a/file b/file
247-
index b6f2c08..61b9053 100755
246+
echo diff --git a/file b/file >expected &&
247+
cat patch |sed "/^index/s/ 100644/ 100755/" >>expected &&
248+
cat >expected-output <<-\EOF
248249
--- a/file
249250
+++ b/file
250251
@@ -1,2 +1,4 @@
251252
+firstline
252253
baseline
253254
content
254255
+lastline
256+
\ No newline at end of file
257+
@@ -1,2 +1,3 @@
258+
+firstline
259+
baseline
260+
content
261+
@@ -1,2 +2,3 @@
262+
baseline
263+
content
264+
+lastline
265+
\ No newline at end of file
255266
EOF
256267
'
257268

258269
# Test splitting the first patch, then adding both
259-
test_expect_success 'add first line works' '
270+
test_expect_success C_LOCALE_OUTPUT 'add first line works' '
260271
git commit -am "clear local changes" &&
261272
git apply patch &&
262-
(echo s; echo y; echo y) | git add -p file &&
263-
git diff --cached > diff &&
264-
diff_cmp expected diff
273+
printf "%s\n" s y y | git add -p file 2>error |
274+
sed -n -e "s/^Stage this hunk[^@]*\(@@ .*\)/\1/" \
275+
-e "/^[-+@ \\\\]"/p >output &&
276+
test_must_be_empty error &&
277+
git diff --cached >diff &&
278+
diff_cmp expected diff &&
279+
test_cmp expected-output output
265280
'
266281

267282
test_expect_success 'setup expected' '

0 commit comments

Comments
 (0)