Merge branch 'jc/am-mailinfo-direct'
authorJunio C Hamano <gitster@pobox.com>
Thu, 29 Oct 2015 20:59:23 +0000 (13:59 -0700)
committerJunio C Hamano <gitster@pobox.com>
Thu, 29 Oct 2015 20:59:23 +0000 (13:59 -0700)
"git am" used to spawn "git mailinfo" via run_command() API once
per each patch, but learned to make a direct call to mailinfo()
instead.

* jc/am-mailinfo-direct:
am: make direct call to mailinfo

1  2 
builtin/am.c
diff --combined builtin/am.c
index 4e396c832139aa3c4ae4e68e9d265b0107817aa9,1873307f9ea8460c83ac2b7b07c91709cf866beb..f1a25ab6ad881fc903a533595792b1a46b40ea91
@@@ -27,6 -27,7 +27,7 @@@
  #include "notes-utils.h"
  #include "rerere.h"
  #include "prompt.h"
+ #include "mailinfo.h"
  
  /**
   * Returns 1 if the file is empty or does not exist, 0 otherwise.
@@@ -1258,58 -1259,61 +1259,61 @@@ static void am_append_signoff(struct am
  static int parse_mail(struct am_state *state, const char *mail)
  {
        FILE *fp;
-       struct child_process cp = CHILD_PROCESS_INIT;
        struct strbuf sb = STRBUF_INIT;
        struct strbuf msg = STRBUF_INIT;
        struct strbuf author_name = STRBUF_INIT;
        struct strbuf author_date = STRBUF_INIT;
        struct strbuf author_email = STRBUF_INIT;
        int ret = 0;
+       struct mailinfo mi;
  
-       cp.git_cmd = 1;
-       cp.in = xopen(mail, O_RDONLY, 0);
-       cp.out = xopen(am_path(state, "info"), O_WRONLY | O_CREAT, 0777);
+       setup_mailinfo(&mi);
  
-       argv_array_push(&cp.args, "mailinfo");
-       argv_array_push(&cp.args, state->utf8 ? "-u" : "-n");
+       if (state->utf8)
+               mi.metainfo_charset = get_commit_output_encoding();
+       else
+               mi.metainfo_charset = NULL;
  
        switch (state->keep) {
        case KEEP_FALSE:
                break;
        case KEEP_TRUE:
-               argv_array_push(&cp.args, "-k");
+               mi.keep_subject = 1;
                break;
        case KEEP_NON_PATCH:
-               argv_array_push(&cp.args, "-b");
+               mi.keep_non_patch_brackets_in_subject = 1;
                break;
        default:
                die("BUG: invalid value for state->keep");
        }
  
        if (state->message_id)
-               argv_array_push(&cp.args, "-m");
+               mi.add_message_id = 1;
  
        switch (state->scissors) {
        case SCISSORS_UNSET:
                break;
        case SCISSORS_FALSE:
-               argv_array_push(&cp.args, "--no-scissors");
+               mi.use_scissors = 0;
                break;
        case SCISSORS_TRUE:
-               argv_array_push(&cp.args, "--scissors");
+               mi.use_scissors = 1;
                break;
        default:
                die("BUG: invalid value for state->scissors");
        }
  
-       argv_array_push(&cp.args, am_path(state, "msg"));
-       argv_array_push(&cp.args, am_path(state, "patch"));
-       if (run_command(&cp) < 0)
+       mi.input = fopen(mail, "r");
+       if (!mi.input)
+               die("could not open input");
+       mi.output = fopen(am_path(state, "info"), "w");
+       if (!mi.output)
+               die("could not open output 'info'");
+       if (mailinfo(&mi, am_path(state, "msg"), am_path(state, "patch")))
                die("could not parse patch");
  
-       close(cp.in);
-       close(cp.out);
+       fclose(mi.input);
+       fclose(mi.output);
  
        /* Extract message and author information */
        fp = xfopen(am_path(state, "info"), "r");
        }
  
        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"));
+       strbuf_addbuf(&msg, &mi.log_message);
 -      stripspace(&msg, 0);
 +      strbuf_stripspace(&msg, 0);
  
        if (state->signoff)
                am_signoff(&msg);
@@@ -1366,6 -1369,7 +1369,7 @@@ finish
        strbuf_release(&author_email);
        strbuf_release(&author_name);
        strbuf_release(&sb);
+       clear_mailinfo(&mi);
        return ret;
  }
  
@@@ -1589,38 -1593,6 +1593,38 @@@ static int build_fake_ancestor(const st
        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.
   */
@@@ -1628,6 -1600,10 +1632,6 @@@ static int fall_back_threeway(const str
  {
        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);
         * 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;
  }
  
@@@ -2225,17 -2212,6 +2229,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;
        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
        };
  
                OPT_END()
        };
  
 -      git_config(git_default_config, NULL);
 +      git_config(git_am_config, NULL);
  
        am_state_init(&state, git_path("rebase-apply"));