]> git.ipfire.org Git - thirdparty/git.git/commitdiff
add-patch: allow all-or-none application of patches
authorAbraham Samuel Adekunle <abrahamadekunle50@gmail.com>
Sat, 14 Feb 2026 11:06:06 +0000 (12:06 +0100)
committerJunio C Hamano <gitster@pobox.com>
Tue, 17 Feb 2026 18:48:37 +0000 (10:48 -0800)
When the flag `--no-auto-advance` is used with `--patch`,
if the user has decided `USE` on a hunk in a file, goes to another
file, and then returns to this file and changes the previous
decision on the hunk to `SKIP`, because the patch has already
been applied, the last decision is not registered and the now
SKIPPED hunk is still applied.

Move the logic for applying patches into a function so that we can
reuse this logic to implement the all or non application of the patches
after the user is done with the hunk selection.

Signed-off-by: Abraham Samuel Adekunle <abrahamadekunle50@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
add-patch.c

index 49eb8957453f946053faa6b389070f68b3cb62a4..d6528d4818564841019a1ef893c7bd6bc952e4ee 100644 (file)
@@ -1420,6 +1420,40 @@ N_("j - go to the next undecided hunk, roll over at the bottom\n"
    "P - print the current hunk using the pager\n"
    "? - print help\n");
 
+static void apply_patch(struct add_p_state *s, struct file_diff *file_diff)
+{
+       struct child_process cp = CHILD_PROCESS_INIT;
+       size_t j;
+
+       /* Any hunk to be used? */
+       for (j = 0; j < file_diff->hunk_nr; j++)
+               if (file_diff->hunk[j].use == USE_HUNK)
+                       break;
+
+       if (j < file_diff->hunk_nr ||
+               (!file_diff->hunk_nr && file_diff->head.use == USE_HUNK)) {
+               /* At least one hunk selected: apply */
+               strbuf_reset(&s->buf);
+               reassemble_patch(s, file_diff, 0, &s->buf);
+
+               discard_index(s->s.r->index);
+               if (s->mode->apply_for_checkout)
+                       apply_for_checkout(s, &s->buf,
+                                       s->mode->is_reverse);
+               else {
+                       setup_child_process(s, &cp, "apply", NULL);
+                       strvec_pushv(&cp.args, s->mode->apply_args);
+                       if (pipe_command(&cp, s->buf.buf, s->buf.len,
+                                       NULL, 0, NULL, 0))
+                               error(_("'git apply' failed"));
+               }
+               if (repo_read_index(s->s.r) >= 0)
+                       repo_refresh_and_write_index(s->s.r, REFRESH_QUIET, 0,
+                                                       1, NULL, NULL, NULL);
+       }
+
+}
+
 static size_t dec_mod(size_t a, size_t m)
 {
        return a > 0 ? a - 1 : m - 1;
@@ -1447,7 +1481,6 @@ static size_t patch_update_file(struct add_p_state *s, size_t idx)
        ssize_t i, undecided_previous, undecided_next, rendered_hunk_index = -1;
        struct hunk *hunk;
        char ch;
-       struct child_process cp = CHILD_PROCESS_INIT;
        int colored = !!s->colored.len, use_pager = 0;
        enum prompt_mode_type prompt_mode_type;
        struct file_diff *file_diff = s->file_diff + idx;
@@ -1777,32 +1810,7 @@ soft_increment:
                }
        }
 
-       /* Any hunk to be used? */
-       for (i = 0; i < file_diff->hunk_nr; i++)
-               if (file_diff->hunk[i].use == USE_HUNK)
-                       break;
-
-       if (i < file_diff->hunk_nr ||
-           (!file_diff->hunk_nr && file_diff->head.use == USE_HUNK)) {
-               /* At least one hunk selected: apply */
-               strbuf_reset(&s->buf);
-               reassemble_patch(s, file_diff, 0, &s->buf);
-
-               discard_index(s->s.r->index);
-               if (s->mode->apply_for_checkout)
-                       apply_for_checkout(s, &s->buf,
-                                          s->mode->is_reverse);
-               else {
-                       setup_child_process(s, &cp, "apply", NULL);
-                       strvec_pushv(&cp.args, s->mode->apply_args);
-                       if (pipe_command(&cp, s->buf.buf, s->buf.len,
-                                        NULL, 0, NULL, 0))
-                               error(_("'git apply' failed"));
-               }
-               if (repo_read_index(s->s.r) >= 0)
-                       repo_refresh_and_write_index(s->s.r, REFRESH_QUIET, 0,
-                                                    1, NULL, NULL, NULL);
-       }
+       apply_patch(s, file_diff);
 
        putchar('\n');
        return patch_update_resp;