Skip to content

Commit 3be7c73

Browse files
committed
add -p: mark split hunks as undecided
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 selected and the user is only prompted with the first of the split hunks, they are not asked whether or not they want to select the rest of the new hunks. If they want to deselect one of the other new hunks they have to navigate back to it to do that. 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 we prompt the user to decide whether to select them or not. 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 before being able 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/[email protected] Signed-off-by: Phillip Wood <[email protected]>
1 parent 038143d commit 3be7c73

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

Documentation/BreakingChanges.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,11 @@ Cf. <[email protected]>,
118118
119119
<CA+EOSBncr=4a4d8n9xS4FNehyebpmX8JiUwCsXD47EQDE+DiUQ@mail.gmail.com>.
120120

121+
* The behavior of "git add -p" has been changed so that splitting a
122+
hunk that has already been marked as selected or unselected will now
123+
prompt the user to select each of the new hunks created by the
124+
split instead of them inheriting their state from the original hunk.
125+
121126
=== Removals
122127

123128
* Support for grafting commits has long been superseded by git-replace(1).

add-patch.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -953,6 +953,9 @@ static int split_hunk(struct add_p_state *s, struct file_diff *file_diff,
953953
* sizeof(*hunk));
954954
hunk = file_diff->hunk + hunk_index;
955955
hunk->splittable_into = 1;
956+
#ifdef WITH_BREAKING_CHANGES
957+
hunk->use = UNDECIDED_HUNK;
958+
#endif
956959
memset(hunk + 1, 0, (splittable_into - 1) * sizeof(*hunk));
957960

958961
header = &hunk->header;
@@ -1054,7 +1057,11 @@ static int split_hunk(struct add_p_state *s, struct file_diff *file_diff,
10541057

10551058
hunk++;
10561059
hunk->splittable_into = 1;
1060+
#ifdef WITH_BREAKING_CHANGES
1061+
hunk->use = UNDECIDED_HUNK;
1062+
#else
10571063
hunk->use = hunk[-1].use;
1064+
#endif
10581065
header = &hunk->header;
10591066

10601067
header->old_count = header->new_count = context_line_count;

t/t3701-add-interactive.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1230,4 +1230,14 @@ test_expect_success 'hunk splitting works with diff.suppressBlankEmpty' '
12301230
test_cmp expect actual
12311231
'
12321232

1233+
test_expect_success WITH_BREAKING_CHANGES 'splitting previous hunk marks split hunks as undecided' '
1234+
test_write_lines a " " b c d e f g h i j k >file &&
1235+
git add file &&
1236+
test_write_lines x " " b y d e f g h i j x >file &&
1237+
test_write_lines n K s n y q | git add -p file &&
1238+
git cat-file blob :file >actual &&
1239+
test_write_lines a " " b y d e f g h i j k >expect &&
1240+
test_cmp expect actual
1241+
'
1242+
12331243
test_done

0 commit comments

Comments
 (0)