mirror of
https://github.com/git/git
synced 2024-09-17 23:41:33 +00:00
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>
This commit is contained in:
parent
2b8ea7f3c7
commit
b3e0fcfe42
|
@ -793,6 +793,11 @@ sub split_hunk {
|
||||||
while (++$i < @$text) {
|
while (++$i < @$text) {
|
||||||
my $line = $text->[$i];
|
my $line = $text->[$i];
|
||||||
my $display = $display->[$i];
|
my $display = $display->[$i];
|
||||||
|
if ($line =~ /^\\/) {
|
||||||
|
push @{$this->{TEXT}}, $line;
|
||||||
|
push @{$this->{DISPLAY}}, $display;
|
||||||
|
next;
|
||||||
|
}
|
||||||
if ($line =~ /^ /) {
|
if ($line =~ /^ /) {
|
||||||
if ($this->{ADDDEL} &&
|
if ($this->{ADDDEL} &&
|
||||||
!defined $next_hunk_start) {
|
!defined $next_hunk_start) {
|
||||||
|
@ -891,6 +896,9 @@ sub merge_hunk {
|
||||||
$n_cnt++;
|
$n_cnt++;
|
||||||
push @line, $line;
|
push @line, $line;
|
||||||
next;
|
next;
|
||||||
|
} elsif ($line =~ /^\\/) {
|
||||||
|
push @line, $line;
|
||||||
|
next;
|
||||||
}
|
}
|
||||||
|
|
||||||
last if ($o1_ofs <= $ofs);
|
last if ($o1_ofs <= $ofs);
|
||||||
|
@ -909,6 +917,9 @@ sub merge_hunk {
|
||||||
$n_cnt++;
|
$n_cnt++;
|
||||||
push @line, $line;
|
push @line, $line;
|
||||||
next;
|
next;
|
||||||
|
} elsif ($line =~ /^\\/) {
|
||||||
|
push @line, $line;
|
||||||
|
next;
|
||||||
}
|
}
|
||||||
$ofs++;
|
$ofs++;
|
||||||
$o_cnt++;
|
$o_cnt++;
|
||||||
|
|
|
@ -237,14 +237,15 @@ test_expect_success 'setup patch' '
|
||||||
baseline
|
baseline
|
||||||
content
|
content
|
||||||
+lastline
|
+lastline
|
||||||
|
\ No newline at end of file
|
||||||
EOF
|
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' '
|
test_expect_success 'setup expected' '
|
||||||
cat >expected <<-\EOF
|
echo diff --git a/file b/file >expected &&
|
||||||
diff --git a/file b/file
|
cat patch |sed "/^index/s/ 100644/ 100755/" >>expected &&
|
||||||
index b6f2c08..61b9053 100755
|
cat >expected-output <<-\EOF
|
||||||
--- a/file
|
--- a/file
|
||||||
+++ b/file
|
+++ b/file
|
||||||
@@ -1,2 +1,4 @@
|
@@ -1,2 +1,4 @@
|
||||||
|
@ -252,16 +253,30 @@ test_expect_success 'setup expected' '
|
||||||
baseline
|
baseline
|
||||||
content
|
content
|
||||||
+lastline
|
+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
|
EOF
|
||||||
'
|
'
|
||||||
|
|
||||||
# Test splitting the first patch, then adding both
|
# 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 commit -am "clear local changes" &&
|
||||||
git apply patch &&
|
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 >diff &&
|
git diff --cached >diff &&
|
||||||
diff_cmp expected diff
|
diff_cmp expected diff &&
|
||||||
|
test_cmp expected-output output
|
||||||
'
|
'
|
||||||
|
|
||||||
test_expect_success 'setup expected' '
|
test_expect_success 'setup expected' '
|
||||||
|
|
Loading…
Reference in a new issue