Skip to content

Commit

Permalink
add -p: mark split hunks as undecided
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
phillipwood committed Feb 18, 2025
1 parent 0394451 commit 43a0592
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
3 changes: 2 additions & 1 deletion add-patch.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -1054,7 +1055,7 @@ static int split_hunk(struct add_p_state *s, struct file_diff *file_diff,

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;
Expand Down
10 changes: 10 additions & 0 deletions t/t3701-add-interactive.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 43a0592

Please sign in to comment.