Skip to content

Commit 732650e

Browse files
phillipwoodgitster
authored andcommitted
add-patch: update hunk splitability after editing
If, when the user edits a hunk, they change deletion lines into context lines or vice versa, then the number of hunks that the edited hunk can be split into may differ from the unedited hunk. This means that so we should recalculate `hunk->splittable_into` after the hunk has been edited. In practice users are unlikely to hit this bug as it is doubtful that a user who has edited a hunk will split it afterwards. Signed-off-by: Phillip Wood <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 3b9532d commit 732650e

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

add-patch.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1185,19 +1185,29 @@ static ssize_t recount_edited_hunk(struct add_p_state *s, struct hunk *hunk,
11851185
{
11861186
struct hunk_header *header = &hunk->header;
11871187
size_t i;
1188+
char ch, marker = ' ';
11881189

1190+
hunk->splittable_into = 0;
11891191
header->old_count = header->new_count = 0;
11901192
for (i = hunk->start; i < hunk->end; ) {
1191-
switch(normalize_marker(&s->plain.buf[i])) {
1193+
ch = normalize_marker(&s->plain.buf[i]);
1194+
switch (ch) {
11921195
case '-':
11931196
header->old_count++;
1197+
if (marker == ' ')
1198+
hunk->splittable_into++;
1199+
marker = ch;
11941200
break;
11951201
case '+':
11961202
header->new_count++;
1203+
if (marker == ' ')
1204+
hunk->splittable_into++;
1205+
marker = ch;
11971206
break;
11981207
case ' ':
11991208
header->old_count++;
12001209
header->new_count++;
1210+
marker = ch;
12011211
break;
12021212
}
12031213

t/t3701-add-interactive.sh

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1311,4 +1311,25 @@ test_expect_success 'splitting previous hunk marks split hunks as undecided' '
13111311
test_cmp expect actual
13121312
'
13131313

1314+
test_expect_success 'splitting edited hunk' '
1315+
# Before the first hunk is edited it can be split into two
1316+
# hunks, after editing it can be split into three hunks.
1317+
1318+
write_script fake-editor.sh <<-\EOF &&
1319+
sed "s/^ c/-c/" "$1" >"$1.tmp" &&
1320+
mv "$1.tmp" "$1"
1321+
EOF
1322+
1323+
test_write_lines a b c d e f g h i j k l m n >file &&
1324+
git add file &&
1325+
test_write_lines A b c d E f g h i j k l M n >file &&
1326+
(
1327+
test_set_editor "$(pwd)/fake-editor.sh" &&
1328+
test_write_lines e K s j y n y q | git add -p file
1329+
) &&
1330+
git cat-file blob :file >actual &&
1331+
test_write_lines a b d e f g h i j k l M n >expect &&
1332+
test_cmp expect actual
1333+
'
1334+
13141335
test_done

0 commit comments

Comments
 (0)