add-patch: reset "permitted" at loop start

Don't accumulate allowed options from any visited hunks, start fresh at the top of the loop instead and only record the allowed options for the current hunk. Reported-by: Junio C Hamano <gitster@pobox.com> 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:24 UTC 208e23ea47ad71c20246ff234efa3abc8080513f
2 files changed +24 -9
add-patch.c
+10 -9
@@ -1439,15 +1439,6 @@ static int patch_update_file(struct add_p_state *s,
1439 struct child_process cp = CHILD_PROCESS_INIT;
1440 int colored = !!s->colored.len, quit = 0, use_pager = 0;
1441 enum prompt_mode_type prompt_mode_type;
1442 - enum {
1443 - ALLOW_GOTO_PREVIOUS_HUNK = 1 << 0,
1444 - ALLOW_GOTO_PREVIOUS_UNDECIDED_HUNK = 1 << 1,
1445 - ALLOW_GOTO_NEXT_HUNK = 1 << 2,
1446 - ALLOW_GOTO_NEXT_UNDECIDED_HUNK = 1 << 3,
1447 - ALLOW_SEARCH_AND_GOTO = 1 << 4,
1448 - ALLOW_SPLIT = 1 << 5,
1449 - ALLOW_EDIT = 1 << 6
1450 - } permitted = 0;
1442
1443 /* Empty added files have no hunks */
1444 if (!file_diff->hunk_nr && !file_diff->added)
@@ -1457,6 +1448,16 @@ static int patch_update_file(struct add_p_state *s,
1448 render_diff_header(s, file_diff, colored, &s->buf);
1449 fputs(s->buf.buf, stdout);
1450 for (;;) {
1451 + enum {
1452 + ALLOW_GOTO_PREVIOUS_HUNK = 1 << 0,
1453 + ALLOW_GOTO_PREVIOUS_UNDECIDED_HUNK = 1 << 1,
1454 + ALLOW_GOTO_NEXT_HUNK = 1 << 2,
1455 + ALLOW_GOTO_NEXT_UNDECIDED_HUNK = 1 << 3,
1456 + ALLOW_SEARCH_AND_GOTO = 1 << 4,
1457 + ALLOW_SPLIT = 1 << 5,
1458 + ALLOW_EDIT = 1 << 6
1459 + } permitted = 0;
1460 +
1461 if (hunk_index >= file_diff->hunk_nr)
1462 hunk_index = 0;
1463 hunk = file_diff->hunk_nr
t/t3701-add-interactive.sh
+14
@@ -1386,4 +1386,18 @@ test_expect_success 'options y, n, a, d, j, k, e roll over to next undecided (2)
1386 test_cmp expect actual
1387 '
1388
1389 +test_expect_success 'invalid option s is rejected' '
1390 + test_write_lines a b c d e f g h i j k >file &&
1391 + git add file &&
1392 + test_write_lines X b X d e f g h i j X >file &&
1393 + test_write_lines j s q | git add -p >out &&
1394 + sed -ne "s/ @@.*//" -e "s/ \$//" -e "/^(/p" <out >actual &&
1395 + cat >expect <<-EOF &&
1396 + (1/2) Stage this hunk [y,n,q,a,d,k,K,j,J,g,/,s,e,p,?]?
1397 + (2/2) Stage this hunk [y,n,q,a,d,k,K,j,J,g,/,e,p,?]? Sorry, cannot split this hunk
1398 + (2/2) Stage this hunk [y,n,q,a,d,k,K,j,J,g,/,e,p,?]?
1399 + EOF
1400 + test_cmp expect actual
1401 +'
1402 +
1403 test_done