Skip to content

Commit cee6cb7

Browse files
dschogitster
authored andcommitted
built-in add -p: implement the "worktree" patch modes
This is a straight-forward port of 2f0896e (restore: support --patch, 2019-04-25) which added support for `git restore -p`. Signed-off-by: Johannes Schindelin <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 52628f9 commit cee6cb7

File tree

3 files changed

+53
-0
lines changed

3 files changed

+53
-0
lines changed

add-interactive.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ enum add_p_mode {
2828
ADD_P_STASH,
2929
ADD_P_RESET,
3030
ADD_P_CHECKOUT,
31+
ADD_P_WORKTREE,
3132
};
3233

3334
int run_add_p(struct repository *r, enum add_p_mode mode,

add-patch.c

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,49 @@ static struct patch_mode patch_mode_checkout_nothead = {
176176
"the file\n"),
177177
};
178178

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+
179222
struct hunk_header {
180223
unsigned long old_offset, old_count, new_offset, new_count;
181224
/*
@@ -1551,6 +1594,13 @@ int run_add_p(struct repository *r, enum add_p_mode mode,
15511594
s.mode = &patch_mode_checkout_head;
15521595
else
15531596
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;
15541604
} else
15551605
s.mode = &patch_mode_add;
15561606
s.revision = revision;

builtin/add.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,8 @@ int run_add_interactive(const char *revision, const char *patch_mode,
208208
mode = ADD_P_RESET;
209209
else if (!strcmp(patch_mode, "--patch=checkout"))
210210
mode = ADD_P_CHECKOUT;
211+
else if (!strcmp(patch_mode, "--patch=worktree"))
212+
mode = ADD_P_WORKTREE;
211213
else
212214
die("'%s' not supported", patch_mode);
213215

0 commit comments

Comments
 (0)