Skip to content

Commit 1878421

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 <[email protected]>
1 parent 0394451 commit 1878421

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

add-patch.c

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

1186+
hunk->splittable_into = 0;
11851187
header->old_count = header->new_count = 0;
11861188
for (i = hunk->start; i < hunk->end; ) {
1187-
switch(normalize_marker(&s->plain.buf[i])) {
1189+
ch = normalize_marker(&s->plain.buf[i]);
1190+
switch (ch) {
11881191
case '-':
11891192
header->old_count++;
1193+
if (marker == ' ')
1194+
hunk->splittable_into++;
1195+
marker = ch;
11901196
break;
11911197
case '+':
11921198
header->new_count++;
1199+
if (marker == ' ')
1200+
hunk->splittable_into++;
1201+
marker = ch;
11931202
break;
11941203
case ' ':
11951204
header->old_count++;
11961205
header->new_count++;
1206+
marker = ch;
11971207
break;
11981208
}
11991209

t/t3701-add-interactive.sh

+21
Original file line numberDiff line numberDiff line change
@@ -1230,4 +1230,25 @@ test_expect_success 'hunk splitting works with diff.suppressBlankEmpty' '
12301230
test_cmp expect actual
12311231
'
12321232

1233+
test_expect_success 'splitting edited hunk' '
1234+
# Before the first hunk is edited it can be split into two
1235+
# hunks, after editing it can be split into three hunks.
1236+
1237+
write_script fake-editor.sh <<-\EOF &&
1238+
sed "s/^ c/-c/" "$1" >"$1.tmp" &&
1239+
mv "$1.tmp" "$1"
1240+
EOF
1241+
1242+
test_write_lines a b c d e f g h i j k l m n >file &&
1243+
git add file &&
1244+
test_write_lines A b c d E f g h i j k l M n >file &&
1245+
(
1246+
test_set_editor "$(pwd)/fake-editor.sh" &&
1247+
test_write_lines e K s n K n y q | git add -p file
1248+
) &&
1249+
git cat-file blob :file >actual &&
1250+
test_write_lines a b d e f g h i j k l M n >expect &&
1251+
test_cmp expect actual
1252+
'
1253+
12331254
test_done

0 commit comments

Comments
 (0)