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

[PATCH 1/2] add -p: mark split hunks as undecided

From
PGPhillip Wood via GitGitGadget <gitgitgadget@gmail.com>
Date
Feb 21, 2025, 14:57 UTC
Message-ID
<43a0592a462cf68bcfdc54373da2319431c3c1ca.1740149837.git.gitgitgadget@gmail.com>
In-Reply-To
<pull.1863.git.1740149837.gitgitgadget@gmail.com>
From: Phillip Wood <phillip.wood@dunelm.org.uk>

When a hunk is split each of the new hunks inherits whether it is selected or not from the original hunk. This means that if a selected hunk is split all of the new hunks are selected and the user is not asked whether or not they want to select the new hunks. This is unfortunate as the user is presumably splitting the original hunk because they only want to select some sub-set of it. Fix this by marking all the new hunks as "undecided" so that we prompt the user to decide whether to select them or not.

Signed-off-by: Phillip Wood <phillip.wood@dunelm.org.uk>
---
 add-patch.c                |  3 ++-
 t/t3701-add-interactive.sh | 10 ++++++++++
 2 files changed, 12 insertions(+), 1 deletion(-)
diff --git a/add-patch.c b/add-patch.c
index 95c67d8c80c..f44f98275cc 100644
--- a/add-patch.c
+++ b/add-patch.c
@@ -953,6 +953,7 @@ static int split_hunk(struct add_p_state *s, struct file_diff *file_diff,
 			* sizeof(*hunk));
 	hunk = file_diff->hunk + hunk_index;
 	hunk->splittable_into = 1;
+	hunk->use = UNDECIDED_HUNK;
 	memset(hunk + 1, 0, (splittable_into - 1) * sizeof(*hunk));
 
 	header = &hunk->header;
@@ -1054,7 +1055,7 @@ next_hunk_line:
 
 		hunk++;
 		hunk->splittable_into = 1;
-		hunk->use = hunk[-1].use;
+		hunk->use = UNDECIDED_HUNK;
 		header = &hunk->header;
 
 		header->old_count = header->new_count = context_line_count;
diff --git a/t/t3701-add-interactive.sh b/t/t3701-add-interactive.sh
index b8a05d95f3f..760f3d0d30f 100755
--- a/t/t3701-add-interactive.sh
+++ b/t/t3701-add-interactive.sh
@@ -1230,4 +1230,14 @@ test_expect_success 'hunk splitting works with diff.suppressBlankEmpty' '
 	test_cmp expect actual
 '
 
+test_expect_success 'splitting previous hunk marks split hunks as undecided' '
+	test_write_lines a " " b c d e f g h i j k >file &&
+	git add file &&
+	test_write_lines x " " b y d e f g h i j x >file &&
+	test_write_lines n K s n y q | git add -p file &&
+	git cat-file blob :file >actual &&
+	test_write_lines a " " b y d e f g h i j k >expect &&
+	test_cmp expect actual
+'
+
 test_done
-- 
gitgitgadget
Previous: Phillip Wood via GitGitGadgetNext: Justin Tobler
Message 2 of 27 in “add -p: a couple of hunk splitting fixes”
  1. 0/2 add -p: a couple of hunk splitting fixesPhillip Wood via GitGitGadget, Feb 21, 2025
  2. 1/2 add -p: mark split hunks as undecidedPhillip Wood via GitGitGadget, Feb 21, 2025
  3. Justin ToblerFeb 21, 2025
  4. Junio C HamanoFeb 21, 2025
  5. phillip.wood123@gmail.comFeb 26, 2025
  6. Junio C HamanoFeb 26, 2025
  7. phillip.wood123@gmail.comFeb 27, 2025
  8. Junio C HamanoFeb 27, 2025
  9. Phillip WoodFeb 28, 2025
  10. Junio C HamanoFeb 28, 2025
  11. Phillip WoodMar 4, 2025
  12. 2/2 add-patch: update hunk splitability after editingPhillip Wood via GitGitGadget, Feb 21, 2025
  13. Justin ToblerFeb 21, 2025
  14. Junio C HamanoFeb 21, 2025
  15. 0/2 add -p: a couple of hunk splitting fixesPhillip Wood via GitGitGadget, Sep 15, 2025
  16. 1/2 add -p: mark split hunks as undecidedPhillip Wood via GitGitGadget, Sep 15, 2025
  17. Junio C HamanoSep 15, 2025
  18. Phillip WoodSep 16, 2025
  19. Junio C HamanoSep 16, 2025
  20. 2/2 add-patch: update hunk splitability after editingPhillip Wood via GitGitGadget, Sep 15, 2025
  21. 0/2 add -p: a couple of hunk splitting fixesPhillip Wood via GitGitGadget, Sep 25, 2025
  22. 1/2 add -p: mark split hunks as undecidedPhillip Wood via GitGitGadget, Sep 25, 2025
  23. Junio C HamanoSep 25, 2025
  24. Phillip WoodSep 26, 2025
  25. Junio C HamanoSep 26, 2025
  26. Phillip WoodOct 8, 2025
  27. 2/2 add-patch: update hunk splitability after editingPhillip Wood via GitGitGadget, Sep 25, 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.