Skip to content

Commit b63fca6

Browse files
committed
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]>
1 parent 3c55f10 commit b63fca6

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-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: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,50 @@ static struct patch_mode patch_mode_checkout_nothead = {
174174
"the file\n"),
175175
};
176176

177+
static struct patch_mode patch_mode_worktree_head = {
178+
.diff = { "diff-index", NULL },
179+
.apply = { "-R", NULL },
180+
.apply_check = { "-R", NULL },
181+
.is_reverse = 1,
182+
.prompt_mode = {
183+
N_("Discard mode change from index and worktree [y,n,q,a,d%s,?]? "),
184+
N_("Discard deletion from index and worktree [y,n,q,a,d%s,?]? "),
185+
N_("Discard this hunk from index and worktree [y,n,q,a,d%s,?]? "),
186+
},
187+
.edit_hunk_hint = N_("If the patch applies cleanly, the edited hunk "
188+
"will immediately be marked for discarding."),
189+
.help_patch_text =
190+
N_("y - discard this hunk from worktree\n"
191+
"n - do not discard this hunk from worktree\n"
192+
"q - quit; do not discard this hunk or any of the remaining "
193+
"ones\n"
194+
"a - discard this hunk and all later hunks in the file\n"
195+
"d - do not discard this hunk or any of the later hunks in "
196+
"the file\n"),
197+
};
198+
199+
static struct patch_mode patch_mode_worktree_nothead = {
200+
.diff = { "diff-index", "-R", NULL },
201+
.apply = { NULL },
202+
.apply_check = { NULL },
203+
.is_reverse = 0,
204+
.prompt_mode = {
205+
N_("Apply mode change to index and worktree [y,n,q,a,d%s,?]? "),
206+
N_("Apply deletion to index and worktree [y,n,q,a,d%s,?]? "),
207+
N_("Apply this hunk to index and worktree [y,n,q,a,d%s,?]? "),
208+
},
209+
.edit_hunk_hint = N_("If the patch applies cleanly, the edited hunk "
210+
"will immediately be marked for applying."),
211+
.help_patch_text =
212+
N_("y - apply this hunk to worktree\n"
213+
"n - do not apply this hunk to worktree\n"
214+
"q - quit; do not apply this hunk or any of the remaining "
215+
"ones\n"
216+
"a - apply this hunk and all later hunks in the file\n"
217+
"d - do not apply this hunk or any of the later hunks in "
218+
"the file\n"),
219+
};
220+
177221
struct hunk_header {
178222
unsigned long old_offset, old_count, new_offset, new_count;
179223
/*
@@ -1549,6 +1593,13 @@ int run_add_p(struct repository *r, enum add_p_mode mode,
15491593
s.mode = &patch_mode_checkout_head;
15501594
else
15511595
s.mode = &patch_mode_checkout_nothead;
1596+
} else if (mode == ADD_P_WORKTREE) {
1597+
if (!revision)
1598+
s.mode = &patch_mode_checkout_index;
1599+
else if (!strcmp(revision, "HEAD"))
1600+
s.mode = &patch_mode_worktree_head;
1601+
else
1602+
s.mode = &patch_mode_worktree_nothead;
15521603
} else
15531604
s.mode = &patch_mode_stage;
15541605
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)