Skip to content

Commit 5eb8da8

Browse files
committed
Merge branch 'pw/add-p-recount'
When user edits the patch in "git add -p" and the user's editor is set to strip trailing whitespaces indiscriminately, an empty line that is unchanged in the patch would become completely empty (instead of a line with a sole SP on it). The code introduced in Git 2.17 timeframe failed to parse such a patch, but now it learned to notice the situation and cope with it. * pw/add-p-recount: add -p: fix counting empty context lines in edited patches
2 parents 0079732 + f4d35a6 commit 5eb8da8

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
@@ -1062,7 +1062,7 @@ sub recount_edited_hunk {
10621062
$o_cnt++;
10631063
} elsif ($mode eq '+') {
10641064
$n_cnt++;
1065-
} elsif ($mode eq ' ') {
1065+
} elsif ($mode eq ' ' or $mode eq "\n") {
10661066
$o_cnt++;
10671067
$n_cnt++;
10681068
}

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)