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 <phillip.wood@dunelm.org.uk> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Phillip Wood committed Mar 5, 2018 at 10:56 UTC b3e0fcfe429204ccdf1f6977c55127171d65a8e5
2 files changed +34 -8
git-add--interactive.perl
+11
@@ -793,6 +793,11 @@ sub split_hunk {
793 while (++$i < @$text) {
794 my $line = $text->[$i];
795 my $display = $display->[$i];
796 + if ($line =~ /^\\/) {
797 + push @{$this->{TEXT}}, $line;
798 + push @{$this->{DISPLAY}}, $display;
799 + next;
800 + }
801 if ($line =~ /^ /) {
802 if ($this->{ADDDEL} &&
803 !defined $next_hunk_start) {
@@ -891,6 +896,9 @@ sub merge_hunk {
896 $n_cnt++;
897 push @line, $line;
898 next;
899 + } elsif ($line =~ /^\\/) {
900 + push @line, $line;
901 + next;
902 }
903
904 last if ($o1_ofs <= $ofs);
@@ -909,6 +917,9 @@ sub merge_hunk {
917 $n_cnt++;
918 push @line, $line;
919 next;
920 + } elsif ($line =~ /^\\/) {
921 + push @line, $line;
922 + next;
923 }
924 $ofs++;
925 $o_cnt++;
t/t3701-add-interactive.sh
+23 -8
@@ -237,14 +237,15 @@ test_expect_success 'setup patch' '
237 baseline
238 content
239 +lastline
240 + \ No newline at end of file
241 EOF
242 '
243
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
245 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
249 --- a/file
250 +++ b/file
251 @@ -1,2 +1,4 @@
@@ -252,16 +253,30 @@ test_expect_success 'setup expected' '
253 baseline
254 content
255 +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
266 EOF
267 '
268
269 # 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' '
271 git commit -am "clear local changes" &&
272 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
280 '
281
282 test_expect_success 'setup expected' '