]> git.ipfire.org Git - thirdparty/git.git/commitdiff
add-patch: update hunk splitability after editing
authorPhillip Wood <phillip.wood@dunelm.org.uk>
Thu, 25 Sep 2025 15:10:38 +0000 (15:10 +0000)
committerJunio C Hamano <gitster@pobox.com>
Thu, 25 Sep 2025 17:13:23 +0000 (10:13 -0700)
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>
add-patch.c
t/t3701-add-interactive.sh

index 61f42de9ea94e6e8d1cc7305993628312846ba6f..bcc2d7666ff87221b35997a0613bc7c34c413d73 100644 (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;
                }
 
index a6829fd0859d1ca0e4c14a29ea9eda7ed0928b03..13739a45820241708ecb1c9c13a8ba3287d4e792 100755 (executable)
@@ -1311,4 +1311,25 @@ test_expect_success 'splitting previous hunk marks split hunks as undecided' '
        test_cmp expect actual
 '
 
+test_expect_success 'splitting edited hunk' '
+       # Before the first hunk is edited it can be split into two
+       # hunks, after editing it can be split into three hunks.
+
+       write_script fake-editor.sh <<-\EOF &&
+       sed "s/^ c/-c/" "$1" >"$1.tmp" &&
+       mv "$1.tmp" "$1"
+       EOF
+
+       test_write_lines a b c d e f g h i j k l m n >file &&
+       git add file &&
+       test_write_lines A b c d E f g h i j k l M n >file &&
+       (
+               test_set_editor "$(pwd)/fake-editor.sh" &&
+               test_write_lines e K s j y n y q | git add -p file
+       ) &&
+       git cat-file blob :file >actual &&
+       test_write_lines a b d e f g h i j k l M n >expect &&
+       test_cmp expect actual
+'
+
 test_done