From: Phillip Wood <phillip.w...@dunelm.org.uk>

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.w...@dunelm.org.uk>
---
 git-add--interactive.perl  | 13 +++++++++----
 t/t3701-add-interactive.sh | 28 ++++++++++++++++++++++------
 2 files changed, 31 insertions(+), 10 deletions(-)

diff --git a/git-add--interactive.perl b/git-add--interactive.perl
index 
0df0c2aa065af88e159f8e9a2febe12f4ef8ee75..3226c2c4f02d5f8679d77b8eede984fc727b422d
 100755
--- a/git-add--interactive.perl
+++ b/git-add--interactive.perl
@@ -793,6 +793,11 @@ sub split_hunk {
                while (++$i < @$text) {
                        my $line = $text->[$i];
                        my $display = $display->[$i];
+                       if ($line =~ /^\\/) {
+                               push @{$this->{TEXT}}, $line;
+                               push @{$this->{DISPLAY}}, $display;
+                               next;
+                       }
                        if ($line =~ /^ /) {
                                if ($this->{ADDDEL} &&
                                    !defined $next_hunk_start) {
@@ -887,8 +892,8 @@ sub merge_hunk {
        $o_cnt = $n_cnt = 0;
        for ($i = 1; $i < @{$prev->{TEXT}}; $i++) {
                my $line = $prev->{TEXT}[$i];
-               if ($line =~ /^\+/) {
-                       $n_cnt++;
+               if ($line =~ /^[+\\]/) {
+                       $n_cnt++ if ($line =~ /^\+/);
                        push @line, $line;
                        next;
                }
@@ -905,8 +910,8 @@ sub merge_hunk {
 
        for ($i = 1; $i < @{$this->{TEXT}}; $i++) {
                my $line = $this->{TEXT}[$i];
-               if ($line =~ /^\+/) {
-                       $n_cnt++;
+               if ($line =~ /^[+\\]/) {
+                       $n_cnt++ if ($line =~ /^\+/);
                        push @line, $line;
                        next;
                }
diff --git a/t/t3701-add-interactive.sh b/t/t3701-add-interactive.sh
index 
bbda771ba7e516aa37a204beffba7eeb0c85a2f4..0fb9c0e0f140e21ef7ad467c40b9211d29f53db6
 100755
--- a/t/t3701-add-interactive.sh
+++ b/t/t3701-add-interactive.sh
@@ -221,30 +221,46 @@ test_expect_success 'setup patch' '
         baseline
         content
        +lastline
+       \ No newline at end of file
        EOF
 '
 
-# Expected output, similar to the patch but w/ diff at the top
+# Expected output, diff is similar to the patch but w/ diff at the top
 test_expect_success 'setup expected' '
-       cat >expected <<-EOF
-       diff --git a/file b/file
+       echo diff --git a/file b/file >expected &&
+       cat patch >>expected &&
+       cat >expected-output <<-EOF
        --- a/file
        +++ b/file
        @@ -1,2 +1,4 @@
        +firstline
         baseline
         content
        +lastline
+       \ No newline at end of file
+       @@ -1,2 +1,3 @@
+       +firstline
+        baseline
+        content
+       @@ -1,2 +2,3 @@
+        baseline
+        content
+       +lastline
+       \ No newline at end of file
        EOF
 '
 
 # Test splitting the first patch, then adding both
-test_expect_success 'add first line works' '
+test_expect_success C_LOCALE_OUTPUT 'add first line works' '
        git commit -am "clear local changes" &&
        git apply patch &&
-       (echo s; echo y; echo y) | git add -p file &&
+       printf "%s\n" s y y | git add -p file 2>error |
+               sed -n -e "s/^Stage this hunk[^@]*\(@@ .*\)/\1/" \
+                      -e "/^[-+@ \\\\]"/p  >output &&
+       test_must_be_empty error &&
        git diff --cached | sed /^index/d >diff &&
-       test_cmp expected diff
+       test_cmp expected diff &&
+       test_cmp expected-output output
 '
 
 test_expect_success 'setup expected' '
-- 
2.16.1

Reply via email to