Skip to content

Commit 43cff42

Browse files
dschoGit for Windows Build Agent
authored and
Git for Windows Build Agent
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 <johannes.schindelin@gmx.de>
1 parent 0ff8b41 commit 43cff42

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
@@ -43,6 +43,7 @@ enum add_p_mode {
4343
ADD_P_STASH,
4444
ADD_P_RESET,
4545
ADD_P_CHECKOUT,
46+
ADD_P_WORKTREE,
4647
};
4748

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

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