Skip to content

Commit 35ef0ee

Browse files
committed
add-patch: update hunk splitability after editing
When the users 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 <phillip.wood@dunelm.org.uk>
1 parent 43a0592 commit 35ef0ee

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

Diff for: add-patch.c

+11-1
Original file line numberDiff line numberDiff line change
@@ -1182,19 +1182,29 @@ static ssize_t recount_edited_hunk(struct add_p_state *s, struct hunk *hunk,
11821182
{
11831183
struct hunk_header *header = &hunk->header;
11841184
size_t i;
1185+
char ch, marker = ' ';
11851186

1187+
hunk->splittable_into = 0;
11861188
header->old_count = header->new_count = 0;
11871189
for (i = hunk->start; i < hunk->end; ) {
1188-
switch(normalize_marker(&s->plain.buf[i])) {
1190+
ch = normalize_marker(&s->plain.buf[i]);
1191+
switch (ch) {
11891192
case '-':
11901193
header->old_count++;
1194+
if (marker == ' ')
1195+
hunk->splittable_into++;
1196+
marker = ch;
11911197
break;
11921198
case '+':
11931199
header->new_count++;
1200+
if (marker == ' ')
1201+
hunk->splittable_into++;
1202+
marker = ch;
11941203
break;
11951204
case ' ':
11961205
header->old_count++;
11971206
header->new_count++;
1207+
marker = ch;
11981208
break;
11991209
}
12001210

Diff for: t/t3701-add-interactive.sh

+21
Original file line numberDiff line numberDiff line change
@@ -1240,4 +1240,25 @@ test_expect_success 'splitting previous hunk marks split hunks as undecided' '
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+
test_write_lines e K s j y n y q | git add -p file
1258+
) &&
1259+
git cat-file blob :file >actual &&
1260+
test_write_lines a b d e f g h i j k l M n >expect &&
1261+
test_cmp expect actual
1262+
'
1263+
12431264
test_done

0 commit comments

Comments
 (0)