@matheusbsbs / matheus-bagliano / commits / cee6cb7300
Fork 0

built-in add -p: implement the "worktree" patch modes

This is a straight-forward port of 2f0896ec3ad4 (restore: support --patch, 2019-04-25) which added support for `git restore -p`. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Johannes Schindelin committed Dec 21, 2019 at 21:57 UTC cee6cb7300354740ff765c74d535ba5d5f9b332e
3 files changed +53
add-interactive.h
+1
@@ -28,6 +28,7 @@ enum add_p_mode {
28 ADD_P_STASH,
29 ADD_P_RESET,
30 ADD_P_CHECKOUT,
31 + ADD_P_WORKTREE,
32 };
33
34 int run_add_p(struct repository *r, enum add_p_mode mode,
add-patch.c
+50
@@ -176,6 +176,49 @@ static struct patch_mode patch_mode_checkout_nothead = {
176 "the file\n"),
177 };
178
179 +static struct patch_mode patch_mode_worktree_head = {
180 + .diff_cmd = { "diff-index", NULL },
181 + .apply_args = { "-R", NULL },
182 + .apply_check_args = { "-R", NULL },
183 + .is_reverse = 1,
184 + .prompt_mode = {
185 + N_("Discard mode change from index and worktree [y,n,q,a,d%s,?]? "),
186 + N_("Discard deletion from index and worktree [y,n,q,a,d%s,?]? "),
187 + N_("Discard this hunk from index and worktree [y,n,q,a,d%s,?]? "),
188 + },
189 + .edit_hunk_hint = N_("If the patch applies cleanly, the edited hunk "
190 + "will immediately be marked for discarding."),
191 + .help_patch_text =
192 + N_("y - discard this hunk from worktree\n"
193 + "n - do not discard this hunk from worktree\n"
194 + "q - quit; do not discard this hunk or any of the remaining "
195 + "ones\n"
196 + "a - discard this hunk and all later hunks in the file\n"
197 + "d - do not discard this hunk or any of the later hunks in "
198 + "the file\n"),
199 +};
200 +
201 +static struct patch_mode patch_mode_worktree_nothead = {
202 + .diff_cmd = { "diff-index", "-R", NULL },
203 + .apply_args = { NULL },
204 + .apply_check_args = { NULL },
205 + .prompt_mode = {
206 + N_("Apply mode change to index and worktree [y,n,q,a,d%s,?]? "),
207 + N_("Apply deletion to index and worktree [y,n,q,a,d%s,?]? "),
208 + N_("Apply this hunk to index and worktree [y,n,q,a,d%s,?]? "),
209 + },
210 + .edit_hunk_hint = N_("If the patch applies cleanly, the edited hunk "
211 + "will immediately be marked for applying."),
212 + .help_patch_text =
213 + N_("y - apply this hunk to worktree\n"
214 + "n - do not apply this hunk to worktree\n"
215 + "q - quit; do not apply this hunk or any of the remaining "
216 + "ones\n"
217 + "a - apply this hunk and all later hunks in the file\n"
218 + "d - do not apply this hunk or any of the later hunks in "
219 + "the file\n"),
220 +};
221 +
222 struct hunk_header {
223 unsigned long old_offset, old_count, new_offset, new_count;
224 /*
@@ -1551,6 +1594,13 @@ int run_add_p(struct repository *r, enum add_p_mode mode,
1594 s.mode = &patch_mode_checkout_head;
1595 else
1596 s.mode = &patch_mode_checkout_nothead;
1597 + } else if (mode == ADD_P_WORKTREE) {
1598 + if (!revision)
1599 + s.mode = &patch_mode_checkout_index;
1600 + else if (!strcmp(revision, "HEAD"))
1601 + s.mode = &patch_mode_worktree_head;
1602 + else
1603 + s.mode = &patch_mode_worktree_nothead;
1604 } else
1605 s.mode = &patch_mode_add;
1606 s.revision = revision;
builtin/add.c
+2
@@ -208,6 +208,8 @@ int run_add_interactive(const char *revision, const char *patch_mode,
208 mode = ADD_P_RESET;
209 else if (!strcmp(patch_mode, "--patch=checkout"))
210 mode = ADD_P_CHECKOUT;
211 + else if (!strcmp(patch_mode, "--patch=worktree"))
212 + mode = ADD_P_WORKTREE;
213 else
214 die("'%s' not supported", patch_mode);
215