Skip to content

Commit f4d35a6

Browse files
phillipwoodgitster
authored andcommitted
add -p: fix counting empty context lines in edited patches
recount_edited_hunk() introduced in commit 2b8ea7f ("add -p: calculate offset delta for edited patches", 2018-03-05) required all context lines to start with a space, empty lines are not counted. This was intended to avoid any recounting problems if the user had introduced empty lines at the end when editing the patch. However this introduced a regression into 'git add -p' as it seems it is common for editors to strip the trailing whitespace from empty context lines when patches are edited thereby introducing empty lines that should be counted. 'git apply' knows how to deal with such empty lines and POSIX states that whether or not there is an space on an empty context line is implementation defined [1]. Fix the regression by counting lines that consist solely of a newline as well as lines starting with a space as context lines and add a test to prevent future regressions. [1] http://pubs.opengroup.org/onlinepubs/9699919799/utilities/diff.html Reported-by: Mahmoud Al-Qudsi <[email protected]> Reported-by: Oliver Joseph Ash <[email protected]> Reported-by: Jeff Felchner <[email protected]> Signed-off-by: Phillip Wood <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 3a8522f commit f4d35a6

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

git-add--interactive.perl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1047,7 +1047,7 @@ sub recount_edited_hunk {
10471047
$o_cnt++;
10481048
} elsif ($mode eq '+') {
10491049
$n_cnt++;
1050-
} elsif ($mode eq ' ') {
1050+
} elsif ($mode eq ' ' or $mode eq "\n") {
10511051
$o_cnt++;
10521052
$n_cnt++;
10531053
}

t/t3701-add-interactive.sh

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,49 @@ test_expect_success 'real edit works' '
175175
diff_cmp expected output
176176
'
177177

178+
test_expect_success 'setup file' '
179+
test_write_lines a "" b "" c >file &&
180+
git add file &&
181+
test_write_lines a "" d "" c >file
182+
'
183+
184+
test_expect_success 'setup patch' '
185+
SP=" " &&
186+
NULL="" &&
187+
cat >patch <<-EOF
188+
@@ -1,4 +1,4 @@
189+
a
190+
$NULL
191+
-b
192+
+f
193+
$SP
194+
c
195+
EOF
196+
'
197+
198+
test_expect_success 'setup expected' '
199+
cat >expected <<-EOF
200+
diff --git a/file b/file
201+
index b5dd6c9..f910ae9 100644
202+
--- a/file
203+
+++ b/file
204+
@@ -1,5 +1,5 @@
205+
a
206+
$SP
207+
-f
208+
+d
209+
$SP
210+
c
211+
EOF
212+
'
213+
214+
test_expect_success 'edit can strip spaces from empty context lines' '
215+
test_write_lines e n q | git add -p 2>error &&
216+
test_must_be_empty error &&
217+
git diff >output &&
218+
diff_cmp expected output
219+
'
220+
178221
test_expect_success 'skip files similarly as commit -a' '
179222
git reset &&
180223
echo file >.gitignore &&

0 commit comments

Comments
 (0)