Skip to content

Commit 250fed7

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 4bf3bfb commit 250fed7

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
@@ -37,6 +37,7 @@ enum add_p_mode {
3737
ADD_P_STASH,
3838
ADD_P_RESET,
3939
ADD_P_CHECKOUT,
40+
ADD_P_WORKTREE,
4041
};
4142

4243
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
@@ -172,6 +172,50 @@ static struct patch_mode patch_mode_checkout_nothead = {
172172
"the file\n"),
173173
};
174174

175+
static struct patch_mode patch_mode_worktree_head = {
176+
.diff = { "diff-index", NULL },
177+
.apply = { "-R", NULL },
178+
.apply_check = { "-R", NULL },
179+
.is_reverse = 1,
180+
.prompt_mode = {
181+
N_("Discard mode change from index and worktree [y,n,q,a,d%s,?]? "),
182+
N_("Discard deletion from index and worktree [y,n,q,a,d%s,?]? "),
183+
N_("Discard this hunk from index and worktree [y,n,q,a,d%s,?]? "),
184+
},
185+
.edit_hunk_hint = N_("If the patch applies cleanly, the edited hunk "
186+
"will immediately be marked for discarding."),
187+
.help_patch_text =
188+
N_("y - discard this hunk from worktree\n"
189+
"n - do not discard this hunk from worktree\n"
190+
"q - quit; do not discard this hunk or any of the remaining "
191+
"ones\n"
192+
"a - discard this hunk and all later hunks in the file\n"
193+
"d - do not discard this hunk or any of the later hunks in "
194+
"the file\n"),
195+
};
196+
197+
static struct patch_mode patch_mode_worktree_nothead = {
198+
.diff = { "diff-index", "-R", NULL },
199+
.apply = { NULL },
200+
.apply_check = { NULL },
201+
.is_reverse = 0,
202+
.prompt_mode = {
203+
N_("Apply mode change to index and worktree [y,n,q,a,d%s,?]? "),
204+
N_("Apply deletion to index and worktree [y,n,q,a,d%s,?]? "),
205+
N_("Apply this hunk to index and worktree [y,n,q,a,d%s,?]? "),
206+
},
207+
.edit_hunk_hint = N_("If the patch applies cleanly, the edited hunk "
208+
"will immediately be marked for applying."),
209+
.help_patch_text =
210+
N_("y - apply this hunk to worktree\n"
211+
"n - do not apply this hunk to worktree\n"
212+
"q - quit; do not apply this hunk or any of the remaining "
213+
"ones\n"
214+
"a - apply this hunk and all later hunks in the file\n"
215+
"d - do not apply this hunk or any of the later hunks in "
216+
"the file\n"),
217+
};
218+
175219
struct hunk_header {
176220
unsigned long old_offset, old_count, new_offset, new_count;
177221
/*
@@ -1510,6 +1554,13 @@ int run_add_p(struct repository *r, enum add_p_mode mode,
15101554
s.mode = &patch_mode_checkout_head;
15111555
else
15121556
s.mode = &patch_mode_checkout_nothead;
1557+
} else if (mode == ADD_P_WORKTREE) {
1558+
if (!revision)
1559+
s.mode = &patch_mode_checkout_index;
1560+
else if (!strcmp(revision, "HEAD"))
1561+
s.mode = &patch_mode_worktree_head;
1562+
else
1563+
s.mode = &patch_mode_worktree_nothead;
15131564
} else
15141565
s.mode = &patch_mode_stage;
15151566
s.revision = revision;

builtin/add.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,8 @@ int run_add_interactive(const char *revision, const char *patch_mode,
206206
mode = ADD_P_RESET;
207207
else if (!strcmp(patch_mode, "--patch=checkout"))
208208
mode = ADD_P_CHECKOUT;
209+
else if (!strcmp(patch_mode, "--patch=worktree"))
210+
mode = ADD_P_WORKTREE;
209211
else
210212
die("'%s' not supported", patch_mode);
211213

0 commit comments

Comments
 (0)