add-patch: allow interfile navigation when selecting hunks

After deciding on all hunks in a file, the interactive session advances automatically to the next file if there is another, or the process ends. Now using the `--no-auto-advance` flag with `--patch`, the process does not advance automatically. A user can choose to go to the next file by pressing '>' or the previous file by pressing '<', before or after deciding on all hunks in the current file. After all hunks have been decided in a file, the user can still rework with the file by applying the options available in the permit set for that hunk, and after all the decisions, the user presses 'q' to submit. After all hunks have been decided, the user can press '?' which will show the hunk selection summary in the help patch remainder text including the total hunks, number of hunks marked for use and number of hunks marked for skip. This feature is enabled by passing the `--no-auto-advance` flag to `--patch` option of the subcommands add, stash, reset, and checkout. Signed-off-by: Abraham Samuel Adekunle <abrahamadekunle50@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Abraham Samuel Adekunle committed Feb 14, 2026 at 12:06 UTC 417b181f99ce53f50dea6541430cfe1f1f359a6a
2 files changed +161 -5
add-patch.c
+61 -5
@@ -1418,7 +1418,10 @@ N_("j - go to the next undecided hunk, roll over at the bottom\n"
1418 "e - manually edit the current hunk\n"
1419 "p - print the current hunk\n"
1420 "P - print the current hunk using the pager\n"
1421 - "? - print help\n");
1421 + "> - go to the next file, roll over at the bottom\n"
1422 + "< - go to the previous file, roll over at the top\n"
1423 + "? - print help\n"
1424 + "HUNKS SUMMARY - Hunks: %d, USE: %d, SKIP: %d\n");
1425
1426 static void apply_patch(struct add_p_state *s, struct file_diff *file_diff)
1427 {
@@ -1483,6 +1486,7 @@ static size_t patch_update_file(struct add_p_state *s, size_t idx)
1486 char ch;
1487 int colored = !!s->colored.len, use_pager = 0;
1488 enum prompt_mode_type prompt_mode_type;
1489 + int all_decided = 0;
1490 struct file_diff *file_diff = s->file_diff + idx;
1491 size_t patch_update_resp = idx;
1492
@@ -1501,7 +1505,9 @@ static size_t patch_update_file(struct add_p_state *s, size_t idx)
1505 ALLOW_GOTO_NEXT_UNDECIDED_HUNK = 1 << 3,
1506 ALLOW_SEARCH_AND_GOTO = 1 << 4,
1507 ALLOW_SPLIT = 1 << 5,
1504 - ALLOW_EDIT = 1 << 6
1508 + ALLOW_EDIT = 1 << 6,
1509 + ALLOW_GOTO_PREVIOUS_FILE = 1 << 7,
1510 + ALLOW_GOTO_NEXT_FILE = 1 << 8
1511 } permitted = 0;
1512
1513 if (hunk_index >= file_diff->hunk_nr)
@@ -1533,8 +1539,12 @@ static size_t patch_update_file(struct add_p_state *s, size_t idx)
1539 /* Everything decided? */
1540 if (undecided_previous < 0 && undecided_next < 0 &&
1541 hunk->use != UNDECIDED_HUNK) {
1536 - patch_update_resp++;
1537 - break;
1542 + if (!s->s.auto_advance)
1543 + all_decided = 1;
1544 + else {
1545 + patch_update_resp++;
1546 + break;
1547 + }
1548 }
1549 strbuf_reset(&s->buf);
1550 if (file_diff->hunk_nr) {
@@ -1583,6 +1593,14 @@ static size_t patch_update_file(struct add_p_state *s, size_t idx)
1593 permitted |= ALLOW_EDIT;
1594 strbuf_addstr(&s->buf, ",e");
1595 }
1596 + if (!s->s.auto_advance && s->file_diff_nr > 1) {
1597 + permitted |= ALLOW_GOTO_NEXT_FILE;
1598 + strbuf_addstr(&s->buf, ",>");
1599 + }
1600 + if (!s->s.auto_advance && s->file_diff_nr > 1) {
1601 + permitted |= ALLOW_GOTO_PREVIOUS_FILE;
1602 + strbuf_addstr(&s->buf, ",<");
1603 + }
1604 strbuf_addstr(&s->buf, ",p,P");
1605 }
1606 if (file_diff->deleted)
@@ -1653,6 +1671,28 @@ soft_increment:
1671 } else if (ch == 'q') {
1672 patch_update_resp = s->file_diff_nr;
1673 break;
1674 + } else if (!s->s.auto_advance && s->answer.buf[0] == '>') {
1675 + if (permitted & ALLOW_GOTO_NEXT_FILE) {
1676 + if (patch_update_resp == s->file_diff_nr - 1)
1677 + patch_update_resp = 0;
1678 + else
1679 + patch_update_resp++;
1680 + break;
1681 + } else {
1682 + err(s, _("No next file"));
1683 + continue;
1684 + }
1685 + } else if (!s->s.auto_advance && s->answer.buf[0] == '<') {
1686 + if (permitted & ALLOW_GOTO_PREVIOUS_FILE) {
1687 + if (patch_update_resp == 0)
1688 + patch_update_resp = s->file_diff_nr - 1;
1689 + else
1690 + patch_update_resp--;
1691 + break;
1692 + } else {
1693 + err(s, _("No previous file"));
1694 + continue;
1695 + }
1696 } else if (s->answer.buf[0] == 'K') {
1697 if (permitted & ALLOW_GOTO_PREVIOUS_HUNK)
1698 hunk_index = dec_mod(hunk_index,
@@ -1798,6 +1838,18 @@ soft_increment:
1838 * commands shown in the prompt that are not
1839 * always available.
1840 */
1841 + if (all_decided && !strncmp(p, "HUNKS SUMMARY", 13)) {
1842 + int total = file_diff->hunk_nr, used = 0, skipped = 0;
1843 +
1844 + for (i = 0; i < file_diff->hunk_nr; i++) {
1845 + if (file_diff->hunk[i].use == USE_HUNK)
1846 + used += 1;
1847 + if (file_diff->hunk[i].use == SKIP_HUNK)
1848 + skipped += 1;
1849 + }
1850 + color_fprintf_ln(stdout, s->s.help_color, _(p),
1851 + total, used, skipped);
1852 + }
1853 if (*p != '?' && !strchr(s->buf.buf, *p))
1854 continue;
1855
@@ -1810,7 +1862,8 @@ soft_increment:
1862 }
1863 }
1864
1813 - apply_patch(s, file_diff);
1865 + if (s->s.auto_advance)
1866 + apply_patch(s, file_diff);
1867
1868 putchar('\n');
1869 return patch_update_resp;
@@ -1871,6 +1924,9 @@ int run_add_p(struct repository *r, enum add_p_mode mode,
1924 if ((i = patch_update_file(&s, i)) == s.file_diff_nr)
1925 break;
1926 }
1927 + if (!s.s.auto_advance)
1928 + for (i = 0; i < s.file_diff_nr; i++)
1929 + apply_patch(&s, s.file_diff + i);
1930
1931 if (s.file_diff_nr == 0)
1932 err(&s, _("No changes."));
t/t3701-add-interactive.sh
+100
@@ -1441,5 +1441,105 @@ test_expect_success 'EOF quits' '
1441 test_grep file out &&
1442 test_grep ! file2 out
1443 '
1444 +for cmd in add checkout reset "stash save" "stash push"
1445 +do
1446 + test_expect_success "$cmd rejects invalid --no-auto-advance options" '
1447 + test_must_fail git $cmd --no-auto-advance 2>actual &&
1448 + test_grep -E "requires .*--(interactive|patch)" actual
1449 + '
1450 +done
1451 +
1452 +test_expect_success 'manual advance (">") moves to next file with --no-auto-advance' '
1453 + git reset --hard &&
1454 + echo line1 >first-file &&
1455 + echo line2 >second-file &&
1456 + git add -A &&
1457 + git commit -m initial >/dev/null 2>&1 &&
1458 + echo change_first >>first-file &&
1459 + echo change_second >>second-file &&
1460 +
1461 + printf ">\nq\n" | git add -p --no-auto-advance >output.test 2>&1 &&
1462 + test_grep -E "(a|b)/second-file" output.test
1463 +'
1464 +
1465 +test_expect_success 'select n on a hunk, go to another file, come back and change to y stages' '
1466 + git reset --hard &&
1467 + echo one >f1 &&
1468 + echo one >f2 &&
1469 + git add -A &&
1470 + git commit -m initial >/dev/null 2>&1 &&
1471 + echo change1 >>f1 &&
1472 + echo change2 >>f2 &&
1473 +
1474 + printf "n\n>\n<\ny\nq\n" | git add -p --no-auto-advance >output.staged 2>&1 &&
1475 + git diff --cached --name-only >staged &&
1476 + test_grep -E "(a/f1)" output.staged
1477 +'
1478 +
1479 +test_expect_success 'select y on a hunk, go to another file, come back and change to n does not stage' '
1480 + git reset --hard &&
1481 + echo one >f1 &&
1482 + echo one >f2 &&
1483 + git add -A &&
1484 + git commit -m initial >/dev/null 2>&1 &&
1485 + echo change1 >>f1 &&
1486 + echo change2 >>f2 &&
1487 +
1488 + printf "y\n>\n<\nn\nq\n" | git add -p --no-auto-advance >output.unstaged 2>&1 &&
1489 + git diff --cached --name-only >staged &&
1490 + test_must_be_empty staged
1491 +'
1492 +
1493 +test_expect_success 'deciding all hunks in a file does not auto advance' '
1494 + git reset --hard &&
1495 + echo line >stay &&
1496 + echo line >other &&
1497 + git add -A &&
1498 + git commit -m initial >/dev/null 2>&1 &&
1499 + echo change >>stay &&
1500 + echo change >>other &&
1501 + test_write_lines y | git add -p --no-auto-advance >raw-output 2>&1 &&
1502 + test_grep "(1/1) Stage this hunk (was: y)" raw-output &&
1503 + test_grep ! "diff --git a/stay b/stay" raw-output
1504 +'
1505 +test_expect_success 'HUNKS SUMMARY does not show in help text when there are undecided hunks' '
1506 + git reset --hard &&
1507 + test_write_lines 1 2 3 4 5 6 7 8 9 >f &&
1508 + git add f &&
1509 + git commit -m initial >/dev/null 2>&1 &&
1510 + test_write_lines 1 X 3 4 Y 6 7 Z 9 >f &&
1511 + test_write_lines s y n | git add -p --no-auto-advance >raw-nostat 2>&1 &&
1512 + test_grep ! "HUNKS SUMMARY - Hunks: " raw-nostat
1513 +'
1514 +
1515 +test_expect_success 'help text shows HUNK SUMMARY when all hunks have been decided' '
1516 + git reset --hard &&
1517 + test_write_lines 1 2 3 4 5 6 7 8 9 >f2 &&
1518 + git add f2 &&
1519 + git commit -m initial >/dev/null 2>&1 &&
1520 + test_write_lines 1 X 3 4 Y 6 7 Z 9 >f2 &&
1521 + printf "s\ny\nn\ny\n?\n" | git add -p --no-auto-advance >raw-stat 2>&1 &&
1522 + test_grep "HUNKS SUMMARY - Hunks: 3, USE: 2, SKIP: 1" raw-stat
1523 +'
1524 +
1525 +test_expect_success 'selective staging across multiple files with --no-advance' '
1526 + git reset --hard &&
1527 + test_write_lines 1 2 3 4 5 6 7 8 9 >a.file &&
1528 + test_write_lines 1 2 3 4 5 6 7 8 9 >b.file &&
1529 + test_write_lines 1 2 3 4 5 6 7 8 9 >c.file &&
1530 + git add -A &&
1531 + git commit -m initial >/dev/null 2>&1 &&
1532 + test_write_lines 1 A2 3 4 A5 6 7 8 9 >a.file &&
1533 + test_write_lines 1 2 B3 4 5 6 7 B8 9 >b.file &&
1534 + test_write_lines C1 2 3 4 5 C6 7 8 9 >c.file &&
1535 + printf "s\ny\nn\n>\ns\nn\ny\n>\ns\ny\ny\nq\n" | git add -p --no-auto-advance >output.index 2>&1 &&
1536 + git diff --cached >staged.diff &&
1537 + test_grep "+A2" staged.diff &&
1538 + test_grep ! "+A5" staged.diff &&
1539 + test_grep "+B8" staged.diff &&
1540 + test_grep ! "+B3" staged.diff &&
1541 + test_grep "+C1" staged.diff &&
1542 + test_grep "+C6" staged.diff
1543 +'
1544
1545 test_done