Skip to content

Commit 192d538

Browse files
devdekunlegitster
authored andcommitted
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>
1 parent ec69170 commit 192d538

File tree

1 file changed

+35
-27
lines changed

1 file changed

+35
-27
lines changed

add-patch.c

Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1420,6 +1420,40 @@ N_("j - go to the next undecided hunk, roll over at the bottom\n"
14201420
"P - print the current hunk using the pager\n"
14211421
"? - print help\n");
14221422

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+
14231457
static size_t dec_mod(size_t a, size_t m)
14241458
{
14251459
return a > 0 ? a - 1 : m - 1;
@@ -1447,7 +1481,6 @@ static ssize_t patch_update_file(struct add_p_state *s, size_t idx)
14471481
ssize_t i, undecided_previous, undecided_next, rendered_hunk_index = -1;
14481482
struct hunk *hunk;
14491483
char ch;
1450-
struct child_process cp = CHILD_PROCESS_INIT;
14511484
int colored = !!s->colored.len, use_pager = 0;
14521485
enum prompt_mode_type prompt_mode_type;
14531486
struct file_diff *file_diff = s->file_diff + idx;
@@ -1777,32 +1810,7 @@ static ssize_t patch_update_file(struct add_p_state *s, size_t idx)
17771810
}
17781811
}
17791812

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);
18061814

18071815
putchar('\n');
18081816
return patch_update_resp;

0 commit comments

Comments
 (0)