add-patch: update hunk splitability after editing

If, when the user edits a hunk, they change deletion lines into
context lines or vice versa, then the number of hunks that the edited
hunk can be split into may differ from the unedited hunk. This means
that so we should recalculate `hunk->splittable_into` after the hunk
has been edited. In practice users are unlikely to hit this bug as it
is doubtful that a user who has edited a hunk will split it afterwards.

Signed-off-by: Phillip Wood <phillip.wood@dunelm.org.uk>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Phillip Wood
2025-09-25 15:10:38 +00:00
committed by Junio C Hamano
parent 3b9532dab2
commit 732650e263
2 changed files with 32 additions and 1 deletions

View File

@@ -1185,19 +1185,29 @@ static ssize_t recount_edited_hunk(struct add_p_state *s, struct hunk *hunk,
{
struct hunk_header *header = &hunk->header;
size_t i;
char ch, marker = ' ';
hunk->splittable_into = 0;
header->old_count = header->new_count = 0;
for (i = hunk->start; i < hunk->end; ) {
switch(normalize_marker(&s->plain.buf[i])) {
ch = normalize_marker(&s->plain.buf[i]);
switch (ch) {
case '-':
header->old_count++;
if (marker == ' ')
hunk->splittable_into++;
marker = ch;
break;
case '+':
header->new_count++;
if (marker == ' ')
hunk->splittable_into++;
marker = ch;
break;
case ' ':
header->old_count++;
header->new_count++;
marker = ch;
break;
}