]> git.ipfire.org Git - thirdparty/git.git/blobdiff - add-patch.c
Merge branch 'jk/escaped-wildcard-dwim'
[thirdparty/git.git] / add-patch.c
index ec5116c187006e914514f12dd0848bdac09c4aa2..d8dafa8168dc8389468d8c2c3cd6221220826605 100644 (file)
@@ -6,6 +6,7 @@
 #include "pathspec.h"
 #include "color.h"
 #include "diff.h"
+#include "compat/terminal.h"
 
 enum prompt_mode_type {
        PROMPT_MODE_CHANGE = 0, PROMPT_DELETION, PROMPT_HUNK,
@@ -176,6 +177,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;
        /*
@@ -317,6 +361,7 @@ static int is_octal(const char *p, size_t len)
 static int parse_diff(struct add_p_state *s, const struct pathspec *ps)
 {
        struct argv_array args = ARGV_ARRAY_INIT;
+       const char *diff_algorithm = s->s.interactive_diff_algorithm;
        struct strbuf *plain = &s->plain, *colored = NULL;
        struct child_process cp = CHILD_PROCESS_INIT;
        char *p, *pend, *colored_p = NULL, *colored_pend = NULL, marker = '\0';
@@ -326,6 +371,8 @@ static int parse_diff(struct add_p_state *s, const struct pathspec *ps)
        int res;
 
        argv_array_pushv(&args, s->mode->diff_cmd);
+       if (diff_algorithm)
+               argv_array_pushf(&args, "--diff-algorithm=%s", diff_algorithm);
        if (s->revision) {
                struct object_id oid;
                argv_array_push(&args,
@@ -355,6 +402,7 @@ static int parse_diff(struct add_p_state *s, const struct pathspec *ps)
 
        if (want_color_fd(1, -1)) {
                struct child_process colored_cp = CHILD_PROCESS_INIT;
+               const char *diff_filter = s->s.interactive_diff_filter;
 
                setup_child_process(s, &colored_cp, NULL);
                xsnprintf((char *)args.argv[color_arg_index], 8, "--color");
@@ -364,6 +412,24 @@ static int parse_diff(struct add_p_state *s, const struct pathspec *ps)
                argv_array_clear(&args);
                if (res)
                        return error(_("could not parse colored diff"));
+
+               if (diff_filter) {
+                       struct child_process filter_cp = CHILD_PROCESS_INIT;
+
+                       setup_child_process(s, &filter_cp,
+                                           diff_filter, NULL);
+                       filter_cp.git_cmd = 0;
+                       filter_cp.use_shell = 1;
+                       strbuf_reset(&s->buf);
+                       if (pipe_command(&filter_cp,
+                                        colored->buf, colored->len,
+                                        &s->buf, colored->len,
+                                        NULL, 0) < 0)
+                               return error(_("failed to run '%s'"),
+                                            diff_filter);
+                       strbuf_swap(colored, &s->buf);
+               }
+
                strbuf_complete_line(colored);
                colored_p = colored->buf;
                colored_pend = colored_p + colored->len;
@@ -488,6 +554,9 @@ static int parse_diff(struct add_p_state *s, const struct pathspec *ps)
                                                   colored_pend - colored_p);
                        if (colored_eol)
                                colored_p = colored_eol + 1;
+                       else if (p != pend)
+                               /* colored shorter than non-colored? */
+                               goto mismatched_output;
                        else
                                colored_p = colored_pend;
 
@@ -512,6 +581,15 @@ static int parse_diff(struct add_p_state *s, const struct pathspec *ps)
                 */
                hunk->splittable_into++;
 
+       /* non-colored shorter than colored? */
+       if (colored_p != colored_pend) {
+mismatched_output:
+               error(_("mismatched output from interactive.diffFilter"));
+               advise(_("Your filter must maintain a one-to-one correspondence\n"
+                        "between its input and output lines."));
+               return -1;
+       }
+
        return 0;
 }
 
@@ -1072,14 +1150,27 @@ static int run_apply_check(struct add_p_state *s,
        return 0;
 }
 
+static int read_single_character(struct add_p_state *s)
+{
+       if (s->s.use_single_key) {
+               int res = read_key_without_echo(&s->answer);
+               printf("%s\n", res == EOF ? "" : s->answer.buf);
+               return res;
+       }
+
+       if (strbuf_getline(&s->answer, stdin) == EOF)
+               return EOF;
+       strbuf_trim_trailing_newline(&s->answer);
+       return 0;
+}
+
 static int prompt_yesno(struct add_p_state *s, const char *prompt)
 {
        for (;;) {
                color_fprintf(stdout, s->s.prompt_color, "%s", _(prompt));
                fflush(stdout);
-               if (strbuf_getline(&s->answer, stdin) == EOF)
+               if (read_single_character(s) == EOF)
                        return -1;
-               strbuf_trim_trailing_newline(&s->answer);
                switch (tolower(s->answer.buf[0])) {
                case 'n': return 0;
                case 'y': return 1;
@@ -1319,9 +1410,8 @@ static int patch_update_file(struct add_p_state *s,
                              _(s->mode->prompt_mode[prompt_mode_type]),
                              s->buf.buf);
                fflush(stdout);
-               if (strbuf_getline(&s->answer, stdin) == EOF)
+               if (read_single_character(s) == EOF)
                        break;
-               strbuf_trim_trailing_newline(&s->answer);
 
                if (!s->answer.len)
                        continue;
@@ -1551,6 +1641,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;
@@ -1562,6 +1659,7 @@ int run_add_p(struct repository *r, enum add_p_mode mode,
            parse_diff(&s, ps) < 0) {
                strbuf_release(&s.plain);
                strbuf_release(&s.colored);
+               clear_add_i_state(&s.s);
                return -1;
        }
 
@@ -1580,5 +1678,6 @@ int run_add_p(struct repository *r, enum add_p_mode mode,
        strbuf_release(&s.buf);
        strbuf_release(&s.plain);
        strbuf_release(&s.colored);
+       clear_add_i_state(&s.s);
        return 0;
 }