add-patch: let options y, n, j, and e roll over to next undecided

The options y, n, and e mark the current hunk as decided. If there's another undecided hunk towards the bottom of the hunk array they go there. If there isn't, but there is another undecided hunk towards the top then they go to the very first hunk, no matter if it has already been decided on. The option j does basically the same move. Technically it is not allowed if there's no undecided hunk towards the bottom, but the variable "permitted" is never reset, so this permission is retained from the very first hunk. That may a bug, but this behavior is at least consistent with y, n, and e and arguably more useful than refusing to move. Improve the roll-over behavior of these four options by moving to the first undecided hunk instead of hunk 1, consistent with what they do when not rolling over. Also adjust the error message for j, as it will only be shown if there's no other undecided hunk in either direction. Reported-by: Windl, Ulrich <u.windl@ukr.de> 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:21 UTC 171c1688ccbe5e6d709444a65a5ca2e0a9175b16
3 files changed +33 -4
Documentation/git-add.adoc
+1 -1
@@ -342,7 +342,7 @@ patch::
342 d - do not stage this hunk or any of the later hunks in the file
343 g - select a hunk to go to
344 / - search for a hunk matching the given regex
345 - j - go to the next undecided hunk
345 + j - go to the next undecided hunk, roll over at the bottom
346 J - go to the next hunk, roll over at the bottom
347 k - go to the previous undecided hunk
348 K - go to the previous hunk
add-patch.c
+10 -3
@@ -1397,7 +1397,7 @@ static size_t display_hunks(struct add_p_state *s,
1397 }
1398
1399 static const char help_patch_remainder[] =
1400 -N_("j - go to the next undecided hunk\n"
1400 +N_("j - go to the next undecided hunk, roll over at the bottom\n"
1401 "J - go to the next hunk, roll over at the bottom\n"
1402 "k - go to the previous undecided hunk\n"
1403 "K - go to the previous hunk\n"
@@ -1408,6 +1408,11 @@ N_("j - go to the next undecided hunk\n"
1408 "p - print the current hunk, 'P' to use the pager\n"
1409 "? - print help\n");
1410
1411 +static size_t inc_mod(size_t a, size_t m)
1412 +{
1413 + return a < m - 1 ? a + 1 : 0;
1414 +}
1415 +
1416 static int patch_update_file(struct add_p_state *s,
1417 struct file_diff *file_diff)
1418 {
@@ -1451,7 +1456,9 @@ static int patch_update_file(struct add_p_state *s,
1456 break;
1457 }
1458
1454 - for (i = hunk_index + 1; i < file_diff->hunk_nr; i++)
1459 + for (i = inc_mod(hunk_index, file_diff->hunk_nr);
1460 + i != hunk_index;
1461 + i = inc_mod(i, file_diff->hunk_nr))
1462 if (file_diff->hunk[i].use == UNDECIDED_HUNK) {
1463 undecided_next = i;
1464 break;
@@ -1594,7 +1601,7 @@ soft_increment:
1601 if (permitted & ALLOW_GOTO_NEXT_UNDECIDED_HUNK)
1602 hunk_index = undecided_next;
1603 else
1597 - err(s, _("No next hunk"));
1604 + err(s, _("No other undecided hunk"));
1605 } else if (s->answer.buf[0] == 'g') {
1606 char *pend;
1607 unsigned long response;
t/t3701-add-interactive.sh
+22
@@ -1364,4 +1364,26 @@ test_expect_success 'option J rolls over' '
1364 test_cmp expect actual
1365 '
1366
1367 +test_expect_success 'options y, n, j, 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 q | git add -p >out &&
1373 + test_write_lines 1 3 1 3 1 3 1 3 1 >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, 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 q | git add -p >out &&
1384 + test_write_lines 1 2 3 2 3 2 3 2 3 2 >expect &&
1385 + sed -n -e "s-/.*--" -e "s/^(//p" <out >actual &&
1386 + test_cmp expect actual
1387 +'
1388 +
1389 test_done