add-patch: allow all-or-none application of patches
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>
Abraham Samuel Adekunle committed
Feb 14, 2026 at 12:06 UTC
d3cce5e76fcbce00d33013424bece954f11fa2f3
1 file changed
+35
-27
add-patch.c
+35
-27
@@ -1420,6 +1420,40 @@ N_("j - go to the next undecided hunk, roll over at the bottom\n"
1420
"P - print the current hunk using the pager\n"
1421
"? - print help\n");
1422
1423
+static void apply_patch(struct add_p_state *s, struct file_diff *file_diff)
1424
+{
1425
+ struct child_process cp = CHILD_PROCESS_INIT;
1426
+ size_t j;
1427
+
1428
+ /* Any hunk to be used? */
1429
+ for (j = 0; j < file_diff->hunk_nr; j++)
1430
+ if (file_diff->hunk[j].use == USE_HUNK)
1431
+ break;
1432
+
1433
+ if (j < file_diff->hunk_nr ||
1434
+ (!file_diff->hunk_nr && file_diff->head.use == USE_HUNK)) {
1435
+ /* At least one hunk selected: apply */
1436
+ strbuf_reset(&s->buf);
1437
+ reassemble_patch(s, file_diff, 0, &s->buf);
1438
+
1439
+ discard_index(s->s.r->index);
1440
+ if (s->mode->apply_for_checkout)
1441
+ apply_for_checkout(s, &s->buf,
1442
+ s->mode->is_reverse);
1443
+ else {
1444
+ setup_child_process(s, &cp, "apply", NULL);
1445
+ strvec_pushv(&cp.args, s->mode->apply_args);
1446
+ if (pipe_command(&cp, s->buf.buf, s->buf.len,
1447
+ NULL, 0, NULL, 0))
1448
+ error(_("'git apply' failed"));
1449
+ }
1450
+ if (repo_read_index(s->s.r) >= 0)
1451
+ repo_refresh_and_write_index(s->s.r, REFRESH_QUIET, 0,
1452
+ 1, NULL, NULL, NULL);
1453
+ }
1454
+
1455
+}
1456
+
1457
static size_t dec_mod(size_t a, size_t m)
1458
{
1459
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)
1481
ssize_t i, undecided_previous, undecided_next, rendered_hunk_index = -1;
1482
struct hunk *hunk;
1483
char ch;
1450
- struct child_process cp = CHILD_PROCESS_INIT;
1484
int colored = !!s->colored.len, use_pager = 0;
1485
enum prompt_mode_type prompt_mode_type;
1486
struct file_diff *file_diff = s->file_diff + idx;
@@ -1777,32 +1810,7 @@ soft_increment:
1810
}
1811
}
1812
1780
- /* Any hunk to be used? */
1781
- for (i = 0; i < file_diff->hunk_nr; i++)
1782
- if (file_diff->hunk[i].use == USE_HUNK)
1783
- break;
1784
-
1785
- if (i < file_diff->hunk_nr ||
1786
- (!file_diff->hunk_nr && file_diff->head.use == USE_HUNK)) {
1787
- /* At least one hunk selected: apply */
1788
- strbuf_reset(&s->buf);
1789
- reassemble_patch(s, file_diff, 0, &s->buf);
1790
-
1791
- discard_index(s->s.r->index);
1792
- if (s->mode->apply_for_checkout)
1793
- apply_for_checkout(s, &s->buf,
1794
- s->mode->is_reverse);
1795
- else {
1796
- setup_child_process(s, &cp, "apply", NULL);
1797
- strvec_pushv(&cp.args, s->mode->apply_args);
1798
- if (pipe_command(&cp, s->buf.buf, s->buf.len,
1799
- NULL, 0, NULL, 0))
1800
- error(_("'git apply' failed"));
1801
- }
1802
- if (repo_read_index(s->s.r) >= 0)
1803
- repo_refresh_and_write_index(s->s.r, REFRESH_QUIET, 0,
1804
- 1, NULL, NULL, NULL);
1805
- }
1813
+ apply_patch(s, file_diff);
1814
1815
putchar('\n');
1816
return patch_update_resp;