add-patch: let options a and d roll over like y and n
Options a and d stage and unstage all undecided hunks towards the bottom of the array of hunks, respectively, and then roll over to the very first hunk. The first part is similar to y and n if the current hunk is the last one in the array, but they roll over to the next undecided hunk if there is any. That's more useful; do it for a and d as well. Signed-off-by: René Scharfe <l.s.r@web.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
René Scharfe committed
Oct 6, 2025 at 19:23 UTC
e8c744dd9a0270d616ab10aaddfa07cfc071e382
2 files changed
+21
-6
add-patch.c
+15
@@ -1418,6 +1418,17 @@ static size_t inc_mod(size_t a, size_t m)
1418
return a < m - 1 ? a + 1 : 0;
1419
}
1420
1421
+static bool get_first_undecided(const struct file_diff *file_diff, size_t *idx)
1422
+{
1423
+ for (size_t i = 0; i < file_diff->hunk_nr; i++) {
1424
+ if (file_diff->hunk[i].use == UNDECIDED_HUNK) {
1425
+ *idx = i;
1426
+ return true;
1427
+ }
1428
+ }
1429
+ return false;
1430
+}
1431
+
1432
static int patch_update_file(struct add_p_state *s,
1433
struct file_diff *file_diff)
1434
{
@@ -1572,6 +1583,8 @@ soft_increment:
1583
if (hunk->use == UNDECIDED_HUNK)
1584
hunk->use = USE_HUNK;
1585
}
1586
+ if (!get_first_undecided(file_diff, &hunk_index))
1587
+ hunk_index = 0;
1588
} else if (hunk->use == UNDECIDED_HUNK) {
1589
hunk->use = USE_HUNK;
1590
}
@@ -1582,6 +1595,8 @@ soft_increment:
1595
if (hunk->use == UNDECIDED_HUNK)
1596
hunk->use = SKIP_HUNK;
1597
}
1598
+ if (!get_first_undecided(file_diff, &hunk_index))
1599
+ hunk_index = 0;
1600
} else if (hunk->use == UNDECIDED_HUNK) {
1601
hunk->use = SKIP_HUNK;
1602
}
t/t3701-add-interactive.sh
+6
-6
@@ -1364,24 +1364,24 @@ test_expect_success 'options J, K roll over' '
1364
test_cmp expect actual
1365
'
1366
1367
-test_expect_success 'options y, n, j, k, e roll over to next undecided (1)' '
1367
+test_expect_success 'options y, n, a, d, j, k, e roll over to next undecided (1)' '
1368
test_write_lines a b c d e f g h i j k l m n o p q >file &&
1369
git add file &&
1370
test_write_lines X b c d e f g h X j k l m n o p X >file &&
1371
test_set_editor : &&
1372
- test_write_lines g3 y g3 n g3 j g3 e k q | git add -p >out &&
1373
- test_write_lines 1 3 1 3 1 3 1 3 1 2 >expect &&
1372
+ test_write_lines g3 y g3 n g3 a g3 d g3 j g3 e k q | git add -p >out &&
1373
+ test_write_lines 1 3 1 3 1 3 1 3 1 3 1 3 1 2 >expect &&
1374
sed -n -e "s-/.*--" -e "s/^(//p" <out >actual &&
1375
test_cmp expect actual
1376
'
1377
1378
-test_expect_success 'options y, n, j, k, e roll over to next undecided (2)' '
1378
+test_expect_success 'options y, n, a, d, j, k, e roll over to next undecided (2)' '
1379
test_write_lines a b c d e f g h i j k l m n o p q >file &&
1380
git add file &&
1381
test_write_lines X b c d e f g h X j k l m n o p X >file &&
1382
test_set_editor : &&
1383
- test_write_lines y g3 y g3 n g3 j g3 e g1 k q | git add -p >out &&
1384
- test_write_lines 1 2 3 2 3 2 3 2 3 2 1 2 >expect &&
1383
+ test_write_lines y g3 y g3 n g3 a g3 d g3 j g3 e g1 k q | git add -p >out &&
1384
+ test_write_lines 1 2 3 2 3 2 3 2 3 2 3 2 3 2 1 2 >expect &&
1385
sed -n -e "s-/.*--" -e "s/^(//p" <out >actual &&
1386
test_cmp expect actual
1387
'