Skip to content

Commit e0b5261

Browse files
committed
add-patch: update hunk splitability after editing
When the user edits a hunk if they change deletion lines to context lines or vice versa then the number of hunks that the edited hunk can be split into may differ from the unedited hunk and so we need to update hunk->splittable_into. 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 3be7c73 commit e0b5261

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
@@ -1188,19 +1188,29 @@ static ssize_t recount_edited_hunk(struct add_p_state *s, struct hunk *hunk,
11881188
{
11891189
struct hunk_header *header = &hunk->header;
11901190
size_t i;
1191+
char ch, marker = ' ';
11911192

1193+
hunk->splittable_into = 0;
11921194
header->old_count = header->new_count = 0;
11931195
for (i = hunk->start; i < hunk->end; ) {
1194-
switch(normalize_marker(&s->plain.buf[i])) {
1196+
ch = normalize_marker(&s->plain.buf[i]);
1197+
switch (ch) {
11951198
case '-':
11961199
header->old_count++;
1200+
if (marker == ' ')
1201+
hunk->splittable_into++;
1202+
marker = ch;
11971203
break;
11981204
case '+':
11991205
header->new_count++;
1206+
if (marker == ' ')
1207+
hunk->splittable_into++;
1208+
marker = ch;
12001209
break;
12011210
case ' ':
12021211
header->old_count++;
12031212
header->new_count++;
1213+
marker = ch;
12041214
break;
12051215
}
12061216

t/t3701-add-interactive.sh

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1240,4 +1240,30 @@ test_expect_success WITH_BREAKING_CHANGES 'splitting previous hunk marks split h
12401240
test_cmp expect actual
12411241
'
12421242

1243+
test_expect_success 'splitting edited hunk' '
1244+
# Before the first hunk is edited it can be split into two
1245+
# hunks, after editing it can be split into three hunks.
1246+
1247+
write_script fake-editor.sh <<-\EOF &&
1248+
sed "s/^ c/-c/" "$1" >"$1.tmp" &&
1249+
mv "$1.tmp" "$1"
1250+
EOF
1251+
1252+
test_write_lines a b c d e f g h i j k l m n >file &&
1253+
git add file &&
1254+
test_write_lines A b c d E f g h i j k l M n >file &&
1255+
(
1256+
test_set_editor "$(pwd)/fake-editor.sh" &&
1257+
if test_have_prereq WITH_BREAKING_CHANGES
1258+
then
1259+
test_write_lines e K s j y n y q
1260+
else
1261+
test_write_lines e K s n K n y q
1262+
fi | git add -p file
1263+
) &&
1264+
git cat-file blob :file >actual &&
1265+
test_write_lines a b d e f g h i j k l M n >expect &&
1266+
test_cmp expect actual
1267+
'
1268+
12431269
test_done

0 commit comments

Comments
 (0)