From: Phillip Wood via GitGitGadget Date: Mon, 15 Sep 2025 15:29:03 GMT Subject: [PATCH v2 1/2] add -p: mark split hunks as undecided Message-ID: <3e2ec7b37f1935c5b091fc4b47b6209beb384d3a.1757950144.git.gitgitgadget@gmail.com> In-Reply-To: From: Phillip Wood When a hunk is split, each of the new hunks inherits whether it is selected or not from the original hunk. If a selected hunk is split all of the new hunks are marked as "selected" and the user is only prompted with the first of the split hunks. The user is not asked whether or not they wish to select the rest of the new hunks. This means that if they wish to deselect any of the new hunks apart from the first one they have to navigate back to the hunk they want to deselect before they can deselect it. This is unfortunate as the user is presumably splitting the original hunk because they only want to select some sub-set of it. Instead mark all the new hunks as "undecided" so that the user is prompted whether they wish to select each one in turn. In the case where the user only wants to change the selection of the first of the split hunks they will now have to do more work re-selecting the remaining split hunks. However, changing the selection of any of the other newly created hunks is now much simpler as the user no-longer has to navigate back to them in order to change their selected state. Due to concerns that users may be relying on the current behaviour [1] this change is guarded by WITH_BREAKING_CHANGES. [1] https://lore.kernel.org/git/xmqqjz9b6xr1.fsf@gitster.g Signed-off-by: Phillip Wood --- Documentation/BreakingChanges.adoc | 5 +++++ add-patch.c | 7 +++++++ t/t3701-add-interactive.sh | 10 ++++++++++ 3 files changed, 22 insertions(+) diff --git a/Documentation/BreakingChanges.adoc b/Documentation/BreakingChanges.adoc index f8d2eba061..f62f75898f 100644 --- a/Documentation/BreakingChanges.adoc +++ b/Documentation/BreakingChanges.adoc @@ -165,6 +165,11 @@ A prerequisite for this change is that the ecosystem is ready to support the "reftable" format. Most importantly, alternative implementations of Git like JGit, libgit2 and Gitoxide need to support it. +* The behavior of "git add -p" has been changed so that splitting a + hunk that has already been marked as selected or unselected will now + prompt the user to select each of the new hunks created by the + split instead of them inheriting their state from the original hunk. + === Removals * Support for grafting commits has long been superseded by git-replace(1). diff --git a/add-patch.c b/add-patch.c index 302e6ba7d9..32157e31ed 100644 --- a/add-patch.c +++ b/add-patch.c @@ -956,6 +956,9 @@ 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; +#ifdef WITH_BREAKING_CHANGES + hunk->use = UNDECIDED_HUNK; +#endif memset(hunk + 1, 0, (splittable_into - 1) * sizeof(*hunk)); header = &hunk->header; @@ -1057,7 +1060,11 @@ next_hunk_line: hunk++; hunk->splittable_into = 1; +#ifdef WITH_BREAKING_CHANGES + hunk->use = UNDECIDED_HUNK; +#else hunk->use = hunk[-1].use; +#endif 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 04d2a19835..43a856e0c0 100755 --- a/t/t3701-add-interactive.sh +++ b/t/t3701-add-interactive.sh @@ -1301,4 +1301,14 @@ do ' done +test_expect_success WITH_BREAKING_CHANGES '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