]> git.ipfire.org Git - thirdparty/git.git/commitdiff
built-in add -p: implement the "worktree" patch modes
authorJohannes Schindelin <johannes.schindelin@gmx.de>
Sat, 21 Dec 2019 21:57:15 +0000 (21:57 +0000)
committerJunio C Hamano <gitster@pobox.com>
Sun, 22 Dec 2019 00:06:22 +0000 (16:06 -0800)
This is a straight-forward port of 2f0896ec3ad4 (restore: support
--patch, 2019-04-25) which added support for `git restore -p`.

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
add-interactive.h
add-patch.c
builtin/add.c

index 77907f6e212dcfec4b5391da41b8399da1ea98db..b2f23479c5e64c35f7715bfa3b6509a0452f8034 100644 (file)
@@ -28,6 +28,7 @@ enum add_p_mode {
        ADD_P_STASH,
        ADD_P_RESET,
        ADD_P_CHECKOUT,
+       ADD_P_WORKTREE,
 };
 
 int run_add_p(struct repository *r, enum add_p_mode mode,
index ec5116c187006e914514f12dd0848bdac09c4aa2..46c6c183d5bfd16a8b176fc17723ce75223e00e0 100644 (file)
@@ -176,6 +176,49 @@ static struct patch_mode patch_mode_checkout_nothead = {
                        "the file\n"),
 };
 
+static struct patch_mode patch_mode_worktree_head = {
+       .diff_cmd = { "diff-index", NULL },
+       .apply_args = { "-R", NULL },
+       .apply_check_args = { "-R", NULL },
+       .is_reverse = 1,
+       .prompt_mode = {
+               N_("Discard mode change from index and worktree [y,n,q,a,d%s,?]? "),
+               N_("Discard deletion from index and worktree [y,n,q,a,d%s,?]? "),
+               N_("Discard this hunk from index and worktree [y,n,q,a,d%s,?]? "),
+       },
+       .edit_hunk_hint = N_("If the patch applies cleanly, the edited hunk "
+                            "will immediately be marked for discarding."),
+       .help_patch_text =
+               N_("y - discard this hunk from worktree\n"
+                  "n - do not discard this hunk from worktree\n"
+                  "q - quit; do not discard this hunk or any of the remaining "
+                       "ones\n"
+                  "a - discard this hunk and all later hunks in the file\n"
+                  "d - do not discard this hunk or any of the later hunks in "
+                       "the file\n"),
+};
+
+static struct patch_mode patch_mode_worktree_nothead = {
+       .diff_cmd = { "diff-index", "-R", NULL },
+       .apply_args = { NULL },
+       .apply_check_args = { NULL },
+       .prompt_mode = {
+               N_("Apply mode change to index and worktree [y,n,q,a,d%s,?]? "),
+               N_("Apply deletion to index and worktree [y,n,q,a,d%s,?]? "),
+               N_("Apply this hunk to index and worktree [y,n,q,a,d%s,?]? "),
+       },
+       .edit_hunk_hint = N_("If the patch applies cleanly, the edited hunk "
+                            "will immediately be marked for applying."),
+       .help_patch_text =
+               N_("y - apply this hunk to worktree\n"
+                  "n - do not apply this hunk to worktree\n"
+                  "q - quit; do not apply this hunk or any of the remaining "
+                       "ones\n"
+                  "a - apply this hunk and all later hunks in the file\n"
+                  "d - do not apply this hunk or any of the later hunks in "
+                       "the file\n"),
+};
+
 struct hunk_header {
        unsigned long old_offset, old_count, new_offset, new_count;
        /*
@@ -1551,6 +1594,13 @@ int run_add_p(struct repository *r, enum add_p_mode mode,
                        s.mode = &patch_mode_checkout_head;
                else
                        s.mode = &patch_mode_checkout_nothead;
+       } else if (mode == ADD_P_WORKTREE) {
+               if (!revision)
+                       s.mode = &patch_mode_checkout_index;
+               else if (!strcmp(revision, "HEAD"))
+                       s.mode = &patch_mode_worktree_head;
+               else
+                       s.mode = &patch_mode_worktree_nothead;
        } else
                s.mode = &patch_mode_add;
        s.revision = revision;
index 191856b036cce6e6725b1cf6e9700a75ce9d8623..b5927105aaae691246103dc6caeab7d63377db38 100644 (file)
@@ -208,6 +208,8 @@ int run_add_interactive(const char *revision, const char *patch_mode,
                        mode = ADD_P_RESET;
                else if (!strcmp(patch_mode, "--patch=checkout"))
                        mode = ADD_P_CHECKOUT;
+               else if (!strcmp(patch_mode, "--patch=worktree"))
+                       mode = ADD_P_WORKTREE;
                else
                        die("'%s' not supported", patch_mode);