Skip to content

Commit a89f3c4

Browse files
dschoGit for Windows Build Agent
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]>
1 parent 6452226 commit a89f3c4

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
/*
@@ -1492,6 +1536,13 @@ int run_add_p(struct repository *r, enum add_p_mode mode,
14921536
s.mode = &patch_mode_checkout_head;
14931537
else
14941538
s.mode = &patch_mode_checkout_nothead;
1539+
} else if (mode == ADD_P_WORKTREE) {
1540+
if (!revision)
1541+
s.mode = &patch_mode_checkout_index;
1542+
else if (!strcmp(revision, "HEAD"))
1543+
s.mode = &patch_mode_worktree_head;
1544+
else
1545+
s.mode = &patch_mode_worktree_nothead;
14951546
} else
14961547
s.mode = &patch_mode_stage;
14971548
s.revision = revision;

builtin/add.c

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

0 commit comments

Comments
 (0)