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

[PATCH v2 3/5] add-patch: let options y, n, j, and e roll over to next undecided

From
René Scharfe <l.s.r@web.de>
Date
Oct 5, 2025, 15:55 UTC
Message-ID
<3e1f51b4-b654-4fec-9774-8a76ee6f6cc3@web.de>
In-Reply-To
<17ef29a7-5214-4729-82eb-92a2af33e465@web.de>

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.

Reported-by: Windl, Ulrich <u.windl@ukr.de>
Signed-off-by: René Scharfe <l.s.r@web.de>
---
 Documentation/git-add.adoc |  2 +-
 add-patch.c                | 11 +++++++++--
 t/t3701-add-interactive.sh | 22 ++++++++++++++++++++++
 3 files changed, 32 insertions(+), 3 deletions(-)
diff --git a/Documentation/git-add.adoc b/Documentation/git-add.adoc
index 5c05a3a7f9..596cdeff93 100644
--- a/Documentation/git-add.adoc
+++ b/Documentation/git-add.adoc
@@ -342,7 +342,7 @@ patch::
        d - do not stage this hunk or any of the later hunks in the file
        g - select a hunk to go to
        / - search for a hunk matching the given regex
-       j - go to the next undecided hunk
+       j - go to the next undecided hunk, roll over at the bottom
        J - go to the next hunk, roll over at the bottom
        k - go to the previous undecided hunk
        K - go to the previous hunk
diff --git a/add-patch.c b/add-patch.c
index bef2ba7a25..da75618dcb 100644
--- a/add-patch.c
+++ b/add-patch.c
@@ -1397,7 +1397,7 @@ static size_t display_hunks(struct add_p_state *s,
 }
 
 static const char help_patch_remainder[] =
-N_("j - go to the next undecided hunk\n"
+N_("j - go to the next undecided hunk, roll over at the bottom\n"
    "J - go to the next hunk, roll over at the bottom\n"
    "k - go to the previous undecided hunk\n"
    "K - go to the previous hunk\n"
@@ -1408,6 +1408,11 @@ N_("j - go to the next undecided hunk\n"
    "p - print the current hunk, 'P' to use the pager\n"
    "? - print help\n");
 
+static size_t inc_mod(size_t a, size_t m)
+{
+	return a < m - 1 ? a + 1 : 0;
+}
+
 static int patch_update_file(struct add_p_state *s,
 			     struct file_diff *file_diff)
 {
@@ -1451,7 +1456,9 @@ static int patch_update_file(struct add_p_state *s,
 					break;
 				}
 
-			for (i = hunk_index + 1; i < file_diff->hunk_nr; i++)
+			for (i = inc_mod(hunk_index, file_diff->hunk_nr);
+			     i != hunk_index;
+			     i = inc_mod(i, file_diff->hunk_nr))
 				if (file_diff->hunk[i].use == UNDECIDED_HUNK) {
 					undecided_next = i;
 					break;
diff --git a/t/t3701-add-interactive.sh b/t/t3701-add-interactive.sh
index d5d2e120ab..8086d3da71 100755
--- a/t/t3701-add-interactive.sh
+++ b/t/t3701-add-interactive.sh
@@ -1364,4 +1364,26 @@ test_expect_success 'option J rolls over' '
 	test_cmp expect actual
 '
 
+test_expect_success 'options y, n, j, e roll over to next undecided (1)' '
+	test_write_lines a b c d e f g h i j k l m n o p q >file &&
+	git add file &&
+	test_write_lines X b c d e f g h X j k l m n o p X >file &&
+	test_set_editor : &&
+	test_write_lines g3 y g3 n g3 j g3 e q | git add -p >out &&
+	test_write_lines 1  3 1  3 1  3 1  3 1 >expect &&
+	sed -n -e "s-/.*--" -e "s/^(//p" <out >actual &&
+	test_cmp expect actual
+'
+
+test_expect_success 'options y, n, j, e roll over to next undecided (2)' '
+	test_write_lines a b c d e f g h i j k l m n o p q >file &&
+	git add file &&
+	test_write_lines X b c d e f g h X j k l m n o p X >file &&
+	test_set_editor : &&
+	test_write_lines y g3 y g3 n g3 j g3 e q | git add -p >out &&
+	test_write_lines 1 2  3 2  3 2  3 2  3 2 >expect &&
+	sed -n -e "s-/.*--" -e "s/^(//p" <out >actual &&
+	test_cmp expect actual
+'
+
 test_done
-- 
2.51.0
Previous: Windl, UlrichNext: René Scharfe
Message 19 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.