@@ -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+
179222struct 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 ;
0 commit comments