Merge branch 'jk/ident-loosen-getpwuid' into maint
[gitweb.git] / builtin / am.c
index 27165a6730ef04da2b4c22ee03eccadfa2e517b6..4e396c832139aa3c4ae4e68e9d265b0107817aa9 100644 (file)
@@ -1207,6 +1207,33 @@ static void NORETURN die_user_resolve(const struct am_state *state)
        exit(128);
 }
 
+static void am_signoff(struct strbuf *sb)
+{
+       char *cp;
+       struct strbuf mine = STRBUF_INIT;
+
+       /* Does it end with our own sign-off? */
+       strbuf_addf(&mine, "\n%s%s\n",
+                   sign_off_header,
+                   fmt_name(getenv("GIT_COMMITTER_NAME"),
+                            getenv("GIT_COMMITTER_EMAIL")));
+       if (mine.len < sb->len &&
+           !strcmp(mine.buf, sb->buf + sb->len - mine.len))
+               goto exit; /* no need to duplicate */
+
+       /* Does it have any Signed-off-by: in the text */
+       for (cp = sb->buf;
+            cp && *cp && (cp = strstr(cp, sign_off_header)) != NULL;
+            cp = strchr(cp, '\n')) {
+               if (sb->buf == cp || cp[-1] == '\n')
+                       break;
+       }
+
+       strbuf_addstr(sb, mine.buf + !!cp);
+exit:
+       strbuf_release(&mine);
+}
+
 /**
  * Appends signoff to the "msg" field of the am_state.
  */
@@ -1215,7 +1242,7 @@ static void am_append_signoff(struct am_state *state)
        struct strbuf sb = STRBUF_INIT;
 
        strbuf_attach(&sb, state->msg, state->msg_len, state->msg_len);
-       append_signoff(&sb, 0, 0);
+       am_signoff(&sb);
        state->msg = strbuf_detach(&sb, &state->msg_len);
 }
 
@@ -1316,10 +1343,10 @@ static int parse_mail(struct am_state *state, const char *mail)
        strbuf_addstr(&msg, "\n\n");
        if (strbuf_read_file(&msg, am_path(state, "msg"), 0) < 0)
                die_errno(_("could not read '%s'"), am_path(state, "msg"));
-       stripspace(&msg, 0);
+       strbuf_stripspace(&msg, 0);
 
        if (state->signoff)
-               append_signoff(&msg, 0, 0);
+               am_signoff(&msg);
 
        assert(!state->author_name);
        state->author_name = strbuf_detach(&author_name, NULL);
@@ -1562,6 +1589,38 @@ static int build_fake_ancestor(const struct am_state *state, const char *index_f
        return 0;
 }
 
+/**
+ * Do the three-way merge using fake ancestor, his tree constructed
+ * from the fake ancestor and the postimage of the patch, and our
+ * state.
+ */
+static int run_fallback_merge_recursive(const struct am_state *state,
+                                       unsigned char *orig_tree,
+                                       unsigned char *our_tree,
+                                       unsigned char *his_tree)
+{
+       struct child_process cp = CHILD_PROCESS_INIT;
+       int status;
+
+       cp.git_cmd = 1;
+
+       argv_array_pushf(&cp.env_array, "GITHEAD_%s=%.*s",
+                        sha1_to_hex(his_tree), linelen(state->msg), state->msg);
+       if (state->quiet)
+               argv_array_push(&cp.env_array, "GIT_MERGE_VERBOSITY=0");
+
+       argv_array_push(&cp.args, "merge-recursive");
+       argv_array_push(&cp.args, sha1_to_hex(orig_tree));
+       argv_array_push(&cp.args, "--");
+       argv_array_push(&cp.args, sha1_to_hex(our_tree));
+       argv_array_push(&cp.args, sha1_to_hex(his_tree));
+
+       status = run_command(&cp) ? (-1) : 0;
+       discard_cache();
+       read_cache();
+       return status;
+}
+
 /**
  * Attempt a threeway merge, using index_path as the temporary index.
  */
@@ -1569,10 +1628,6 @@ static int fall_back_threeway(const struct am_state *state, const char *index_pa
 {
        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);
@@ -1624,22 +1679,11 @@ static int fall_back_threeway(const struct am_state *state, const char *index_pa
         * 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)) {
+       if (run_fallback_merge_recursive(state, orig_tree, our_tree, his_tree)) {
                rerere(state->allow_rerere_autoupdate);
-               free(his_tree_name);
                return error(_("Failed to merge in the changes."));
        }
 
-       free(his_tree_name);
        return 0;
 }
 
@@ -2057,11 +2101,6 @@ static int clean_index(const unsigned char *head, const unsigned char *remote)
 static void am_rerere_clear(void)
 {
        struct string_list merge_rr = STRING_LIST_INIT_DUP;
-       int fd = setup_rerere(&merge_rr, 0);
-
-       if (fd < 0)
-               return;
-
        rerere_clear(&merge_rr);
        string_list_clear(&merge_rr, 1);
 }
@@ -2186,6 +2225,17 @@ enum resume_mode {
        RESUME_ABORT
 };
 
+static int git_am_config(const char *k, const char *v, void *cb)
+{
+       int status;
+
+       status = git_gpg_config(k, v, NULL);
+       if (status)
+               return status;
+
+       return git_default_config(k, v, NULL);
+}
+
 int cmd_am(int argc, const char **argv, const char *prefix)
 {
        struct am_state state;
@@ -2196,8 +2246,8 @@ int cmd_am(int argc, const char **argv, const char *prefix)
        int in_progress;
 
        const char * const usage[] = {
-               N_("git am [options] [(<mbox>|<Maildir>)...]"),
-               N_("git am [options] (--continue | --skip | --abort)"),
+               N_("git am [<options>] [(<mbox>|<Maildir>)...]"),
+               N_("git am [<options>] (--continue | --skip | --abort)"),
                NULL
        };
 
@@ -2286,7 +2336,7 @@ int cmd_am(int argc, const char **argv, const char *prefix)
                OPT_END()
        };
 
-       git_config(git_default_config, NULL);
+       git_config(git_am_config, NULL);
 
        am_state_init(&state, git_path("rebase-apply"));