git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v2 5/5] add-patch: reset "permitted" at loop start

From
René Scharfe <l.s.r@web.de>
Date
Oct 5, 2025, 15:55 UTC
Message-ID
<5dc0941b-2bc1-4107-b39e-8312ff7c08f9@web.de>
In-Reply-To
<17ef29a7-5214-4729-82eb-92a2af33e465@web.de>

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>
---
 add-patch.c                | 19 ++++++++++---------
 t/t3701-add-interactive.sh | 14 ++++++++++++++
 2 files changed, 24 insertions(+), 9 deletions(-)
diff --git a/add-patch.c b/add-patch.c
index 52e881d3b0..7b489d0a75 100644
--- a/add-patch.c
+++ b/add-patch.c
@@ -1428,15 +1428,6 @@ static int patch_update_file(struct add_p_state *s,
 	struct child_process cp = CHILD_PROCESS_INIT;
 	int colored = !!s->colored.len, quit = 0, use_pager = 0;
 	enum prompt_mode_type prompt_mode_type;
-	enum {
-		ALLOW_GOTO_PREVIOUS_HUNK = 1 << 0,
-		ALLOW_GOTO_PREVIOUS_UNDECIDED_HUNK = 1 << 1,
-		ALLOW_GOTO_NEXT_HUNK = 1 << 2,
-		ALLOW_GOTO_NEXT_UNDECIDED_HUNK = 1 << 3,
-		ALLOW_SEARCH_AND_GOTO = 1 << 4,
-		ALLOW_SPLIT = 1 << 5,
-		ALLOW_EDIT = 1 << 6
-	} permitted = 0;
 
 	/* Empty added files have no hunks */
 	if (!file_diff->hunk_nr && !file_diff->added)
@@ -1446,6 +1437,16 @@ static int patch_update_file(struct add_p_state *s,
 	render_diff_header(s, file_diff, colored, &s->buf);
 	fputs(s->buf.buf, stdout);
 	for (;;) {
+		enum {
+			ALLOW_GOTO_PREVIOUS_HUNK = 1 << 0,
+			ALLOW_GOTO_PREVIOUS_UNDECIDED_HUNK = 1 << 1,
+			ALLOW_GOTO_NEXT_HUNK = 1 << 2,
+			ALLOW_GOTO_NEXT_UNDECIDED_HUNK = 1 << 3,
+			ALLOW_SEARCH_AND_GOTO = 1 << 4,
+			ALLOW_SPLIT = 1 << 5,
+			ALLOW_EDIT = 1 << 6
+		} permitted = 0;
+
 		if (hunk_index >= file_diff->hunk_nr)
 			hunk_index = 0;
 		hunk = file_diff->hunk_nr
diff --git a/t/t3701-add-interactive.sh b/t/t3701-add-interactive.sh
index 385e55c783..8c24a76e59 100755
--- a/t/t3701-add-interactive.sh
+++ b/t/t3701-add-interactive.sh
@@ -1386,4 +1386,18 @@ test_expect_success 'options y, n, j, k, e roll over to next undecided (2)' '
 	test_cmp expect actual
 '
 
+test_expect_success 'invalid option s is rejected' '
+	test_write_lines a b c d e f g h i j k >file &&
+	git add file &&
+	test_write_lines X b X d e f g h i j X >file &&
+	test_write_lines j s q | git add -p >out &&
+	sed -ne "s/ @@.*//" -e "s/ \$//" -e "/^(/p" <out >actual &&
+	cat >expect <<-EOF &&
+	(1/2) Stage this hunk [y,n,q,a,d,k,K,j,J,g,/,s,e,p,?]?
+	(2/2) Stage this hunk [y,n,q,a,d,k,K,j,J,g,/,e,p,?]? Sorry, cannot split this hunk
+	(2/2) Stage this hunk [y,n,q,a,d,k,K,j,J,g,/,e,p,?]?
+	EOF
+	test_cmp expect actual
+'
+
 test_done
-- 
2.51.0
Previous: René ScharfeNext: René Scharfe
Message 25 of 37 in “Broken handling of "J" hunks for "add --interactive"?”
  1. Windl, UlrichOct 2, 2025
  2. add-patch: roll over to next undecided hunkRené Scharfe, Oct 3, 2025
  3. Phillip WoodOct 3, 2025
  4. René ScharfeOct 3, 2025
  5. Phillip WoodOct 8, 2025
  6. Junio C HamanoOct 3, 2025
  7. René ScharfeOct 3, 2025
  8. Junio C HamanoOct 3, 2025
  9. Junio C HamanoOct 3, 2025
  10. Junio C HamanoOct 3, 2025
  11. 0/5 add-patch: roll over to next undecided hunkRené Scharfe, Oct 5, 2025
  12. 1/5 add-patch: improve help for options j, J, k, and KRené Scharfe, Oct 5, 2025
  13. Junio C HamanoOct 5, 2025
  14. René ScharfeOct 6, 2025
  15. Junio C HamanoOct 6, 2025
  16. Windl, UlrichOct 31, 2025
  17. Junio C HamanoNov 1, 2025
  18. Windl, UlrichNov 3, 2025
  19. 3/5 add-patch: let options y, n, j, and e roll over to next undecidedRené Scharfe, Oct 5, 2025
  20. 2/5 add-patch: document that option J rolls overRené Scharfe, Oct 5, 2025
  21. Junio C HamanoOct 5, 2025
  22. 4/5 add-patch: let options k and K roll over like j and JRené Scharfe, Oct 5, 2025
  23. Junio C HamanoOct 5, 2025
  24. René ScharfeOct 6, 2025
  25. 5/5 add-patch: reset "permitted" at loop startRené Scharfe, Oct 5, 2025
  26. 0/6 add-patch: roll over to next undecided hunkRené Scharfe, Oct 6, 2025
  27. 1/6 add-patch: improve help for options j, J, k, and KRené Scharfe, Oct 6, 2025
  28. 2/6 add-patch: document that option J rolls overRené Scharfe, Oct 6, 2025
  29. 3/6 add-patch: let options y, n, j, and e roll over to next undecidedRené Scharfe, Oct 6, 2025
  30. 4/6 add-patch: let options k and K roll over like j and JRené Scharfe, Oct 6, 2025
  31. 5/6 add-patch: let options a and d roll over like y and nRené Scharfe, Oct 6, 2025
  32. 6/6 add-patch: reset "permitted" at loop startRené Scharfe, Oct 6, 2025
  33. Windl, UlrichOct 31, 2025
  34. Junio C HamanoOct 31, 2025
  35. Junio C HamanoOct 6, 2025
  36. René ScharfeOct 6, 2025
  37. Junio C HamanoOct 6, 2025

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.