builtin-am: implement -k/--keep, --keep-non-patch
[gitweb.git] / builtin / am.c
index 8b8f2da7f44f539bc676a3b3f0b7a0d628596403..68dca2e8d3d2e7402d264dbc5e1b7d74ac685349 100644 (file)
 #include "diffcore.h"
 #include "unpack-trees.h"
 #include "branch.h"
+#include "sequencer.h"
+#include "revision.h"
+#include "merge-recursive.h"
+#include "revision.h"
+#include "log-tree.h"
 
 /**
  * Returns 1 if the file is empty or does not exist, 0 otherwise.
@@ -63,6 +68,12 @@ enum patch_format {
        PATCH_FORMAT_MBOX
 };
 
+enum keep_type {
+       KEEP_FALSE = 0,
+       KEEP_TRUE,      /* pass -k flag to git-mailinfo */
+       KEEP_NON_PATCH  /* pass -b flag to git-mailinfo */
+};
+
 struct am_state {
        /* state directory path */
        char *dir;
@@ -82,8 +93,13 @@ struct am_state {
        int prec;
 
        /* various operating modes and command line options */
+       int threeway;
        int quiet;
+       int signoff;
+       int utf8;
+       int keep; /* enum keep_type */
        const char *resolvemsg;
+       int rebasing;
 };
 
 /**
@@ -98,6 +114,8 @@ static void am_state_init(struct am_state *state, const char *dir)
        state->dir = xstrdup(dir);
 
        state->prec = 4;
+
+       state->utf8 = 1;
 }
 
 /**
@@ -350,9 +368,28 @@ static void am_load(struct am_state *state)
 
        read_commit_msg(state);
 
+       read_state_file(&sb, state, "threeway", 1);
+       state->threeway = !strcmp(sb.buf, "t");
+
        read_state_file(&sb, state, "quiet", 1);
        state->quiet = !strcmp(sb.buf, "t");
 
+       read_state_file(&sb, state, "sign", 1);
+       state->signoff = !strcmp(sb.buf, "t");
+
+       read_state_file(&sb, state, "utf8", 1);
+       state->utf8 = !strcmp(sb.buf, "t");
+
+       read_state_file(&sb, state, "keep", 1);
+       if (!strcmp(sb.buf, "t"))
+               state->keep = KEEP_TRUE;
+       else if (!strcmp(sb.buf, "b"))
+               state->keep = KEEP_NON_PATCH;
+       else
+               state->keep = KEEP_FALSE;
+
+       state->rebasing = !!file_exists(am_path(state, "rebasing"));
+
        strbuf_release(&sb);
 }
 
@@ -514,6 +551,7 @@ static void am_setup(struct am_state *state, enum patch_format patch_format,
                        const char **paths)
 {
        unsigned char curr_head[GIT_SHA1_RAWSZ];
+       const char *str;
 
        if (!patch_format)
                patch_format = detect_patch_format(paths);
@@ -531,14 +569,47 @@ static void am_setup(struct am_state *state, enum patch_format patch_format,
                die(_("Failed to split patches."));
        }
 
+       if (state->rebasing)
+               state->threeway = 1;
+
+       write_file(am_path(state, "threeway"), 1, state->threeway ? "t" : "f");
+
        write_file(am_path(state, "quiet"), 1, state->quiet ? "t" : "f");
 
+       write_file(am_path(state, "sign"), 1, state->signoff ? "t" : "f");
+
+       write_file(am_path(state, "utf8"), 1, state->utf8 ? "t" : "f");
+
+       switch (state->keep) {
+       case KEEP_FALSE:
+               str = "f";
+               break;
+       case KEEP_TRUE:
+               str = "t";
+               break;
+       case KEEP_NON_PATCH:
+               str = "b";
+               break;
+       default:
+               die("BUG: invalid value for state->keep");
+       }
+
+       write_file(am_path(state, "keep"), 1, "%s", str);
+
+       if (state->rebasing)
+               write_file(am_path(state, "rebasing"), 1, "%s", "");
+       else
+               write_file(am_path(state, "applying"), 1, "%s", "");
+
        if (!get_sha1("HEAD", curr_head)) {
                write_file(am_path(state, "abort-safety"), 1, "%s", sha1_to_hex(curr_head));
-               update_ref("am", "ORIG_HEAD", curr_head, NULL, 0, UPDATE_REFS_DIE_ON_ERR);
+               if (!state->rebasing)
+                       update_ref("am", "ORIG_HEAD", curr_head, NULL, 0,
+                                       UPDATE_REFS_DIE_ON_ERR);
        } else {
                write_file(am_path(state, "abort-safety"), 1, "%s", "");
-               delete_ref("ORIG_HEAD", NULL, 0);
+               if (!state->rebasing)
+                       delete_ref("ORIG_HEAD", NULL, 0);
        }
 
        /*
@@ -691,6 +762,21 @@ static int parse_mail(struct am_state *state, const char *mail)
        cp.out = xopen(am_path(state, "info"), O_WRONLY | O_CREAT, 0777);
 
        argv_array_push(&cp.args, "mailinfo");
+       argv_array_push(&cp.args, state->utf8 ? "-u" : "-n");
+
+       switch (state->keep) {
+       case KEEP_FALSE:
+               break;
+       case KEEP_TRUE:
+               argv_array_push(&cp.args, "-k");
+               break;
+       case KEEP_NON_PATCH:
+               argv_array_push(&cp.args, "-b");
+               break;
+       default:
+               die("BUG: invalid value for state->keep");
+       }
+
        argv_array_push(&cp.args, am_path(state, "msg"));
        argv_array_push(&cp.args, am_path(state, "patch"));
 
@@ -734,6 +820,9 @@ static int parse_mail(struct am_state *state, const char *mail)
                die_errno(_("could not read '%s'"), am_path(state, "msg"));
        stripspace(&msg, 0);
 
+       if (state->signoff)
+               append_signoff(&msg, 0, 0);
+
        assert(!state->author_name);
        state->author_name = strbuf_detach(&author_name, NULL);
 
@@ -756,25 +845,264 @@ static int parse_mail(struct am_state *state, const char *mail)
 }
 
 /**
- * Applies current patch with git-apply. Returns 0 on success, -1 otherwise.
+ * Sets commit_id to the commit hash where the mail was generated from.
+ * Returns 0 on success, -1 on failure.
+ */
+static int get_mail_commit_sha1(unsigned char *commit_id, const char *mail)
+{
+       struct strbuf sb = STRBUF_INIT;
+       FILE *fp = xfopen(mail, "r");
+       const char *x;
+
+       if (strbuf_getline(&sb, fp, '\n'))
+               return -1;
+
+       if (!skip_prefix(sb.buf, "From ", &x))
+               return -1;
+
+       if (get_sha1_hex(x, commit_id) < 0)
+               return -1;
+
+       strbuf_release(&sb);
+       fclose(fp);
+       return 0;
+}
+
+/**
+ * Sets state->msg, state->author_name, state->author_email, state->author_date
+ * to the commit's respective info.
+ */
+static void get_commit_info(struct am_state *state, struct commit *commit)
+{
+       const char *buffer, *ident_line, *author_date, *msg;
+       size_t ident_len;
+       struct ident_split ident_split;
+       struct strbuf sb = STRBUF_INIT;
+
+       buffer = logmsg_reencode(commit, NULL, get_commit_output_encoding());
+
+       ident_line = find_commit_header(buffer, "author", &ident_len);
+
+       if (split_ident_line(&ident_split, ident_line, ident_len) < 0) {
+               strbuf_add(&sb, ident_line, ident_len);
+               die(_("invalid ident line: %s"), sb.buf);
+       }
+
+       assert(!state->author_name);
+       if (ident_split.name_begin) {
+               strbuf_add(&sb, ident_split.name_begin,
+                       ident_split.name_end - ident_split.name_begin);
+               state->author_name = strbuf_detach(&sb, NULL);
+       } else
+               state->author_name = xstrdup("");
+
+       assert(!state->author_email);
+       if (ident_split.mail_begin) {
+               strbuf_add(&sb, ident_split.mail_begin,
+                       ident_split.mail_end - ident_split.mail_begin);
+               state->author_email = strbuf_detach(&sb, NULL);
+       } else
+               state->author_email = xstrdup("");
+
+       author_date = show_ident_date(&ident_split, DATE_MODE(NORMAL));
+       strbuf_addstr(&sb, author_date);
+       assert(!state->author_date);
+       state->author_date = strbuf_detach(&sb, NULL);
+
+       assert(!state->msg);
+       msg = strstr(buffer, "\n\n");
+       if (!msg)
+               die(_("unable to parse commit %s"), sha1_to_hex(commit->object.sha1));
+       state->msg = xstrdup(msg + 2);
+       state->msg_len = strlen(state->msg);
+}
+
+/**
+ * Writes `commit` as a patch to the state directory's "patch" file.
+ */
+static void write_commit_patch(const struct am_state *state, struct commit *commit)
+{
+       struct rev_info rev_info;
+       FILE *fp;
+
+       fp = xfopen(am_path(state, "patch"), "w");
+       init_revisions(&rev_info, NULL);
+       rev_info.diff = 1;
+       rev_info.abbrev = 0;
+       rev_info.disable_stdin = 1;
+       rev_info.show_root_diff = 1;
+       rev_info.diffopt.output_format = DIFF_FORMAT_PATCH;
+       rev_info.no_commit_id = 1;
+       DIFF_OPT_SET(&rev_info.diffopt, BINARY);
+       DIFF_OPT_SET(&rev_info.diffopt, FULL_INDEX);
+       rev_info.diffopt.use_color = 0;
+       rev_info.diffopt.file = fp;
+       rev_info.diffopt.close_file = 1;
+       add_pending_object(&rev_info, &commit->object, "");
+       diff_setup_done(&rev_info.diffopt);
+       log_tree_commit(&rev_info, commit);
+}
+
+/**
+ * Like parse_mail(), but parses the mail by looking up its commit ID
+ * directly. This is used in --rebasing mode to bypass git-mailinfo's munging
+ * of patches.
+ *
+ * Will always return 0 as the patch should never be skipped.
+ */
+static int parse_mail_rebase(struct am_state *state, const char *mail)
+{
+       struct commit *commit;
+       unsigned char commit_sha1[GIT_SHA1_RAWSZ];
+
+       if (get_mail_commit_sha1(commit_sha1, mail) < 0)
+               die(_("could not parse %s"), mail);
+
+       commit = lookup_commit_or_die(commit_sha1, mail);
+
+       get_commit_info(state, commit);
+
+       write_commit_patch(state, commit);
+
+       return 0;
+}
+
+/**
+ * Applies current patch with git-apply. Returns 0 on success, -1 otherwise. If
+ * `index_file` is not NULL, the patch will be applied to that index.
  */
-static int run_apply(const struct am_state *state)
+static int run_apply(const struct am_state *state, const char *index_file)
 {
        struct child_process cp = CHILD_PROCESS_INIT;
 
        cp.git_cmd = 1;
 
+       if (index_file)
+               argv_array_pushf(&cp.env_array, "GIT_INDEX_FILE=%s", index_file);
+
+       /*
+        * If we are allowed to fall back on 3-way merge, don't give false
+        * errors during the initial attempt.
+        */
+       if (state->threeway && !index_file) {
+               cp.no_stdout = 1;
+               cp.no_stderr = 1;
+       }
+
        argv_array_push(&cp.args, "apply");
-       argv_array_push(&cp.args, "--index");
+
+       if (index_file)
+               argv_array_push(&cp.args, "--cached");
+       else
+               argv_array_push(&cp.args, "--index");
+
        argv_array_push(&cp.args, am_path(state, "patch"));
 
        if (run_command(&cp))
                return -1;
 
        /* Reload index as git-apply will have modified it. */
+       discard_cache();
+       read_cache_from(index_file ? index_file : get_index_file());
+
+       return 0;
+}
+
+/**
+ * Builds an index that contains just the blobs needed for a 3way merge.
+ */
+static int build_fake_ancestor(const struct am_state *state, const char *index_file)
+{
+       struct child_process cp = CHILD_PROCESS_INIT;
+
+       cp.git_cmd = 1;
+       argv_array_push(&cp.args, "apply");
+       argv_array_pushf(&cp.args, "--build-fake-ancestor=%s", index_file);
+       argv_array_push(&cp.args, am_path(state, "patch"));
+
+       if (run_command(&cp))
+               return -1;
+
+       return 0;
+}
+
+/**
+ * Attempt a threeway merge, using index_path as the temporary index.
+ */
+static int fall_back_threeway(const struct am_state *state, const char *index_path)
+{
+       unsigned char orig_tree[GIT_SHA1_RAWSZ], his_tree[GIT_SHA1_RAWSZ],
+                     our_tree[GIT_SHA1_RAWSZ];
+       const unsigned char *bases[1] = {orig_tree};
+       struct merge_options o;
+       struct commit *result;
+       char *his_tree_name;
+
+       if (get_sha1("HEAD", our_tree) < 0)
+               hashcpy(our_tree, EMPTY_TREE_SHA1_BIN);
+
+       if (build_fake_ancestor(state, index_path))
+               return error("could not build fake ancestor");
+
+       discard_cache();
+       read_cache_from(index_path);
+
+       if (write_index_as_tree(orig_tree, &the_index, index_path, 0, NULL))
+               return error(_("Repository lacks necessary blobs to fall back on 3-way merge."));
+
+       say(state, stdout, _("Using index info to reconstruct a base tree..."));
+
+       if (!state->quiet) {
+               /*
+                * List paths that needed 3-way fallback, so that the user can
+                * review them with extra care to spot mismerges.
+                */
+               struct rev_info rev_info;
+               const char *diff_filter_str = "--diff-filter=AM";
+
+               init_revisions(&rev_info, NULL);
+               rev_info.diffopt.output_format = DIFF_FORMAT_NAME_STATUS;
+               diff_opt_parse(&rev_info.diffopt, &diff_filter_str, 1);
+               add_pending_sha1(&rev_info, "HEAD", our_tree, 0);
+               diff_setup_done(&rev_info.diffopt);
+               run_diff_index(&rev_info, 1);
+       }
+
+       if (run_apply(state, index_path))
+               return error(_("Did you hand edit your patch?\n"
+                               "It does not apply to blobs recorded in its index."));
+
+       if (write_index_as_tree(his_tree, &the_index, index_path, 0, NULL))
+               return error("could not write tree");
+
+       say(state, stdout, _("Falling back to patching base and 3-way merge..."));
+
        discard_cache();
        read_cache();
 
+       /*
+        * This is not so wrong. Depending on which base we picked, orig_tree
+        * may be wildly different from ours, but his_tree has the same set of
+        * wildly different changes in parts the patch did not touch, so
+        * recursive ends up canceling them, saying that we reverted all those
+        * changes.
+        */
+
+       init_merge_options(&o);
+
+       o.branch1 = "HEAD";
+       his_tree_name = xstrfmt("%.*s", linelen(state->msg), state->msg);
+       o.branch2 = his_tree_name;
+
+       if (state->quiet)
+               o.verbosity = 0;
+
+       if (merge_recursive_generic(&o, our_tree, his_tree, 1, bases, &result)) {
+               free(his_tree_name);
+               return error(_("Failed to merge in the changes."));
+       }
+
+       free(his_tree_name);
        return 0;
 }
 
@@ -862,6 +1190,7 @@ static void am_run(struct am_state *state, int resume)
 
        while (state->cur <= state->last) {
                const char *mail = am_path(state, msgnum(state));
+               int apply_status;
 
                if (!file_exists(mail))
                        goto next;
@@ -870,7 +1199,14 @@ static void am_run(struct am_state *state, int resume)
                        validate_resume_state(state);
                        resume = 0;
                } else {
-                       if (parse_mail(state, mail))
+                       int skip;
+
+                       if (state->rebasing)
+                               skip = parse_mail_rebase(state, mail);
+                       else
+                               skip = parse_mail(state, mail);
+
+                       if (skip)
                                goto next; /* mail should be skipped */
 
                        write_author_script(state);
@@ -879,7 +1215,26 @@ static void am_run(struct am_state *state, int resume)
 
                say(state, stdout, _("Applying: %.*s"), linelen(state->msg), state->msg);
 
-               if (run_apply(state) < 0) {
+               apply_status = run_apply(state, NULL);
+
+               if (apply_status && state->threeway) {
+                       struct strbuf sb = STRBUF_INIT;
+
+                       strbuf_addstr(&sb, am_path(state, "patch-merge-index"));
+                       apply_status = fall_back_threeway(state, sb.buf);
+                       strbuf_release(&sb);
+
+                       /*
+                        * Applying the patch to an earlier tree and merging
+                        * the result may have produced the same tree as ours.
+                        */
+                       if (!apply_status && !index_has_changes(NULL)) {
+                               say(state, stdout, _("No changes -- Patch already applied."));
+                               goto next;
+                       }
+               }
+
+               if (apply_status) {
                        int advice_amworkdir = 1;
 
                        printf_ln(_("Patch failed at %s %.*s"), msgnum(state),
@@ -900,8 +1255,14 @@ static void am_run(struct am_state *state, int resume)
                am_next(state);
        }
 
-       am_destroy(state);
-       run_command_v_opt(argv_gc_auto, RUN_GIT_CMD);
+       /*
+        * In rebasing mode, it's up to the caller to take care of
+        * housekeeping.
+        */
+       if (!state->rebasing) {
+               am_destroy(state);
+               run_command_v_opt(argv_gc_auto, RUN_GIT_CMD);
+       }
 }
 
 /**
@@ -1149,7 +1510,17 @@ int cmd_am(int argc, const char **argv, const char *prefix)
        };
 
        struct option options[] = {
+               OPT_BOOL('3', "3way", &state.threeway,
+                       N_("allow fall back on 3way merging if needed")),
                OPT__QUIET(&state.quiet, N_("be quiet")),
+               OPT_BOOL('s', "signoff", &state.signoff,
+                       N_("add a Signed-off-by line to the commit message")),
+               OPT_BOOL('u', "utf8", &state.utf8,
+                       N_("recode into utf8 (default)")),
+               OPT_SET_INT('k', "keep", &state.keep,
+                       N_("pass -k flag to git-mailinfo"), KEEP_TRUE),
+               OPT_SET_INT(0, "keep-non-patch", &state.keep,
+                       N_("pass -b flag to git-mailinfo"), KEEP_NON_PATCH),
                OPT_CALLBACK(0, "patch-format", &patch_format, N_("format"),
                        N_("format the patch(es) are in"),
                        parse_opt_patchformat),
@@ -1167,6 +1538,8 @@ int cmd_am(int argc, const char **argv, const char *prefix)
                OPT_CMDMODE(0, "abort", &resume,
                        N_("restore the original branch and abort the patching operation."),
                        RESUME_ABORT),
+               OPT_HIDDEN_BOOL(0, "rebasing", &state.rebasing,
+                       N_("(internal use for git-rebase)")),
                OPT_END()
        };
 
@@ -1218,6 +1591,23 @@ int cmd_am(int argc, const char **argv, const char *prefix)
                struct argv_array paths = ARGV_ARRAY_INIT;
                int i;
 
+               /*
+                * Handle stray state directory in the independent-run case. In
+                * the --rebasing case, it is up to the caller to take care of
+                * stray directories.
+                */
+               if (file_exists(state.dir) && !state.rebasing) {
+                       if (resume == RESUME_ABORT) {
+                               am_destroy(&state);
+                               am_state_release(&state);
+                               return 0;
+                       }
+
+                       die(_("Stray %s directory found.\n"
+                               "Use \"git am --abort\" to remove it."),
+                               state.dir);
+               }
+
                if (resume)
                        die(_("Resolve operation not in progress, we are not resuming."));