Skip to content

Commit 3a831b1

Browse files
committed
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]>
1 parent 3e2ec7b commit 3a831b1

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

add-patch.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1191,19 +1191,29 @@ static ssize_t recount_edited_hunk(struct add_p_state *s, struct hunk *hunk,
11911191
{
11921192
struct hunk_header *header = &hunk->header;
11931193
size_t i;
1194+
char ch, marker = ' ';
11941195

1196+
hunk->splittable_into = 0;
11951197
header->old_count = header->new_count = 0;
11961198
for (i = hunk->start; i < hunk->end; ) {
1197-
switch(normalize_marker(&s->plain.buf[i])) {
1199+
ch = normalize_marker(&s->plain.buf[i]);
1200+
switch (ch) {
11981201
case '-':
11991202
header->old_count++;
1203+
if (marker == ' ')
1204+
hunk->splittable_into++;
1205+
marker = ch;
12001206
break;
12011207
case '+':
12021208
header->new_count++;
1209+
if (marker == ' ')
1210+
hunk->splittable_into++;
1211+
marker = ch;
12031212
break;
12041213
case ' ':
12051214
header->old_count++;
12061215
header->new_count++;
1216+
marker = ch;
12071217
break;
12081218
}
12091219

t/t3701-add-interactive.sh

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1311,4 +1311,30 @@ test_expect_success WITH_BREAKING_CHANGES 'splitting previous hunk marks split h
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+
if test_have_prereq WITH_BREAKING_CHANGES
1329+
then
1330+
test_write_lines e K s j y n y q
1331+
else
1332+
test_write_lines e K s n K n y q
1333+
fi | git add -p file
1334+
) &&
1335+
git cat-file blob :file >actual &&
1336+
test_write_lines a b d e f g h i j k l M n >expect &&
1337+
test_cmp expect actual
1338+
'
1339+
13141340
test_done

0 commit comments

Comments
 (0)