Skip to content

Commit a0870ab

Browse files
committed
fixup! built-in add -p: implement the hunk splitting feature
When counting how many lines a hunk can be split into, we need to be careful to take line comments in the diff into account, i.e. lines starting with a backslash. Example: [...] -} \ No newline at end of file +} Logically, these line comments belong to the line preceding them, therefore we need to pretend that they have the same diff marker (space, minus or plus character). This fixes #2364. Signed-off-by: Johannes Schindelin <[email protected]>
1 parent 504b50d commit a0870ab

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

add-patch.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -511,10 +511,9 @@ static int parse_diff(struct add_p_state *s, const struct pathspec *ps)
511511
(int)(eol - (plain->buf + file_diff->head.start)),
512512
plain->buf + file_diff->head.start);
513513

514-
if ((marker == '-' || marker == '+') &&
515-
(*p == ' ' || *p == '\\'))
514+
if ((marker == '-' || marker == '+') && *p == ' ')
516515
hunk->splittable_into++;
517-
if (marker)
516+
if (marker && *p != '\\')
518517
marker = *p;
519518

520519
p = eol == pend ? pend : eol + 1;

t/t3701-add-interactive.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,18 @@ test_expect_failure 'split hunk "add -p (no, yes, edit)"' '
472472
! grep "^+31" actual
473473
'
474474

475+
test_expect_success 'split hunk with incomplete line at end' '
476+
git reset --hard &&
477+
printf "missing LF" >>test &&
478+
git add test &&
479+
test_write_lines before 10 20 30 40 50 60 70 >test &&
480+
git grep --cached missing &&
481+
test_write_lines s n y q | git add -p &&
482+
test_must_fail git grep --cached missing &&
483+
git grep before &&
484+
test_must_fail git grep --cached before
485+
'
486+
475487
test_expect_failure 'edit, adding lines to the first hunk' '
476488
test_write_lines 10 11 20 30 40 50 51 60 >test &&
477489
git reset &&

0 commit comments

Comments
 (0)