Skip to content

Commit 3fdb4a8

Browse files
committed
Merge branch 'pw/add-p-hunk-splitting-fix' into next
Marking a hunk 'selected' in "git add -p" and then splitting made all the split pieces 'selected'; this has been changed to make them all 'undecided', which gives better end-user experience. * pw/add-p-hunk-splitting-fix: add-patch: update hunk splitability after editing add -p: mark split hunks as undecided
2 parents cf0fc3e + 732650e commit 3fdb4a8

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed

add-patch.c

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -956,6 +956,7 @@ static int split_hunk(struct add_p_state *s, struct file_diff *file_diff,
956956
* sizeof(*hunk));
957957
hunk = file_diff->hunk + hunk_index;
958958
hunk->splittable_into = 1;
959+
hunk->use = UNDECIDED_HUNK;
959960
memset(hunk + 1, 0, (splittable_into - 1) * sizeof(*hunk));
960961

961962
header = &hunk->header;
@@ -1057,7 +1058,7 @@ static int split_hunk(struct add_p_state *s, struct file_diff *file_diff,
10571058

10581059
hunk++;
10591060
hunk->splittable_into = 1;
1060-
hunk->use = hunk[-1].use;
1061+
hunk->use = UNDECIDED_HUNK;
10611062
header = &hunk->header;
10621063

10631064
header->old_count = header->new_count = context_line_count;
@@ -1184,19 +1185,29 @@ static ssize_t recount_edited_hunk(struct add_p_state *s, struct hunk *hunk,
11841185
{
11851186
struct hunk_header *header = &hunk->header;
11861187
size_t i;
1188+
char ch, marker = ' ';
11871189

1190+
hunk->splittable_into = 0;
11881191
header->old_count = header->new_count = 0;
11891192
for (i = hunk->start; i < hunk->end; ) {
1190-
switch(normalize_marker(&s->plain.buf[i])) {
1193+
ch = normalize_marker(&s->plain.buf[i]);
1194+
switch (ch) {
11911195
case '-':
11921196
header->old_count++;
1197+
if (marker == ' ')
1198+
hunk->splittable_into++;
1199+
marker = ch;
11931200
break;
11941201
case '+':
11951202
header->new_count++;
1203+
if (marker == ' ')
1204+
hunk->splittable_into++;
1205+
marker = ch;
11961206
break;
11971207
case ' ':
11981208
header->old_count++;
11991209
header->new_count++;
1210+
marker = ch;
12001211
break;
12011212
}
12021213

t/t3701-add-interactive.sh

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1354,4 +1354,35 @@ do
13541354
'
13551355
done
13561356

1357+
test_expect_success 'splitting previous hunk marks split hunks as undecided' '
1358+
test_write_lines a " " b c d e f g h i j k >file &&
1359+
git add file &&
1360+
test_write_lines x " " b y d e f g h i j x >file &&
1361+
test_write_lines n K s n y q | git add -p file &&
1362+
git cat-file blob :file >actual &&
1363+
test_write_lines a " " b y d e f g h i j k >expect &&
1364+
test_cmp expect actual
1365+
'
1366+
1367+
test_expect_success 'splitting edited hunk' '
1368+
# Before the first hunk is edited it can be split into two
1369+
# hunks, after editing it can be split into three hunks.
1370+
1371+
write_script fake-editor.sh <<-\EOF &&
1372+
sed "s/^ c/-c/" "$1" >"$1.tmp" &&
1373+
mv "$1.tmp" "$1"
1374+
EOF
1375+
1376+
test_write_lines a b c d e f g h i j k l m n >file &&
1377+
git add file &&
1378+
test_write_lines A b c d E f g h i j k l M n >file &&
1379+
(
1380+
test_set_editor "$(pwd)/fake-editor.sh" &&
1381+
test_write_lines e K s j y n y q | git add -p file
1382+
) &&
1383+
git cat-file blob :file >actual &&
1384+
test_write_lines a b d e f g h i j k l M n >expect &&
1385+
test_cmp expect actual
1386+
'
1387+
13571388
test_done

0 commit comments

Comments
 (0)