Merge branch 'jc/am-i-v-fix'
[gitweb.git] / builtin / am.c
index 56cf26e8b7dd794c34c95524caaf5c86aa3252ba..d003939bc5c65947451de4a6e1817eb0f184211f 100644 (file)
@@ -10,6 +10,7 @@
 #include "dir.h"
 #include "run-command.h"
 #include "quote.h"
+#include "tempfile.h"
 #include "lockfile.h"
 #include "cache-tree.h"
 #include "refs.h"
@@ -26,6 +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.
@@ -43,21 +45,6 @@ static int is_empty_file(const char *filename)
        return !st.st_size;
 }
 
-/**
- * Like strbuf_getline(), but treats both '\n' and "\r\n" as line terminators.
- */
-static int strbuf_getline_crlf(struct strbuf *sb, FILE *fp)
-{
-       if (strbuf_getwholeline(sb, fp, '\n'))
-               return EOF;
-       if (sb->buf[sb->len - 1] == '\n') {
-               strbuf_setlen(sb, sb->len - 1);
-               if (sb->len > 0 && sb->buf[sb->len - 1] == '\r')
-                       strbuf_setlen(sb, sb->len - 1);
-       }
-       return 0;
-}
-
 /**
  * Returns the length of the first line of msg.
  */
@@ -98,6 +85,12 @@ enum scissors_type {
        SCISSORS_TRUE        /* pass --scissors to git-mailinfo */
 };
 
+enum signoff_type {
+       SIGNOFF_FALSE = 0,
+       SIGNOFF_TRUE = 1,
+       SIGNOFF_EXPLICIT /* --signoff was set on the command-line */
+};
+
 struct am_state {
        /* state directory path */
        char *dir;
@@ -123,7 +116,7 @@ struct am_state {
        int interactive;
        int threeway;
        int quiet;
-       int signoff;
+       int signoff; /* enum signoff_type */
        int utf8;
        int keep; /* enum keep_type */
        int message_id;
@@ -187,6 +180,27 @@ static inline const char *am_path(const struct am_state *state, const char *path
        return mkpath("%s/%s", state->dir, path);
 }
 
+/**
+ * For convenience to call write_file()
+ */
+static int write_state_text(const struct am_state *state,
+                           const char *name, const char *string)
+{
+       return write_file(am_path(state, name), "%s", string);
+}
+
+static int write_state_count(const struct am_state *state,
+                            const char *name, int value)
+{
+       return write_file(am_path(state, name), "%d", value);
+}
+
+static int write_state_bool(const struct am_state *state,
+                           const char *name, int value)
+{
+       return write_state_text(state, name, value ? "t" : "f");
+}
+
 /**
  * If state->quiet is false, calls fprintf(fp, fmt, ...), and appends a newline
  * at the end.
@@ -255,7 +269,7 @@ static char *read_shell_var(FILE *fp, const char *key)
        struct strbuf sb = STRBUF_INIT;
        const char *str;
 
-       if (strbuf_getline(&sb, fp, '\n'))
+       if (strbuf_getline_lf(&sb, fp))
                goto fail;
 
        if (!skip_prefix(sb.buf, key, &str))
@@ -356,7 +370,7 @@ static void write_author_script(const struct am_state *state)
        sq_quote_buf(&sb, state->author_date);
        strbuf_addch(&sb, '\n');
 
-       write_file(am_path(state, "author-script"), 1, "%s", sb.buf);
+       write_state_text(state, "author-script", sb.buf);
 
        strbuf_release(&sb);
 }
@@ -544,7 +558,7 @@ static int copy_notes_for_rebase(const struct am_state *state)
 
        fp = xfopen(am_path(state, "rewritten"), "r");
 
-       while (!strbuf_getline(&sb, fp, '\n')) {
+       while (!strbuf_getline_lf(&sb, fp)) {
                unsigned char from_obj[GIT_SHA1_RAWSZ], to_obj[GIT_SHA1_RAWSZ];
 
                if (sb.len != GIT_SHA1_HEXSZ * 2 + 1) {
@@ -599,7 +613,7 @@ static int is_mail(FILE *fp)
        if (regcomp(&regex, header_regex, REG_NOSUB | REG_EXTENDED))
                die("invalid pattern: %s", header_regex);
 
-       while (!strbuf_getline_crlf(&sb, fp)) {
+       while (!strbuf_getline(&sb, fp)) {
                if (!sb.len)
                        break; /* End of header */
 
@@ -646,7 +660,7 @@ static int detect_patch_format(const char **paths)
 
        fp = xfopen(*paths, "r");
 
-       while (!strbuf_getline_crlf(&l1, fp)) {
+       while (!strbuf_getline(&l1, fp)) {
                if (l1.len)
                        break;
        }
@@ -667,9 +681,9 @@ static int detect_patch_format(const char **paths)
        }
 
        strbuf_reset(&l2);
-       strbuf_getline_crlf(&l2, fp);
+       strbuf_getline(&l2, fp);
        strbuf_reset(&l3);
-       strbuf_getline_crlf(&l3, fp);
+       strbuf_getline(&l3, fp);
 
        /*
         * If the second line is empty and the third is a From, Author or Date
@@ -788,7 +802,7 @@ static int stgit_patch_to_mail(FILE *out, FILE *in, int keep_cr)
        struct strbuf sb = STRBUF_INIT;
        int subject_printed = 0;
 
-       while (!strbuf_getline(&sb, in, '\n')) {
+       while (!strbuf_getline_lf(&sb, in)) {
                const char *str;
 
                if (str_isspace(sb.buf))
@@ -846,7 +860,7 @@ static int split_mail_stgit_series(struct am_state *state, const char **paths,
                return error(_("could not open '%s' for reading: %s"), *paths,
                                strerror(errno));
 
-       while (!strbuf_getline(&sb, fp, '\n')) {
+       while (!strbuf_getline_lf(&sb, fp)) {
                if (*sb.buf == '#')
                        continue; /* skip comment lines */
 
@@ -871,7 +885,7 @@ static int hg_patch_to_mail(FILE *out, FILE *in, int keep_cr)
 {
        struct strbuf sb = STRBUF_INIT;
 
-       while (!strbuf_getline(&sb, in, '\n')) {
+       while (!strbuf_getline_lf(&sb, in)) {
                const char *str;
 
                if (skip_prefix(sb.buf, "# User ", &str))
@@ -994,13 +1008,10 @@ static void am_setup(struct am_state *state, enum patch_format patch_format,
        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");
+       write_state_bool(state, "threeway", state->threeway);
+       write_state_bool(state, "quiet", state->quiet);
+       write_state_bool(state, "sign", state->signoff);
+       write_state_bool(state, "utf8", state->utf8);
 
        switch (state->keep) {
        case KEEP_FALSE:
@@ -1016,9 +1027,8 @@ static void am_setup(struct am_state *state, enum patch_format patch_format,
                die("BUG: invalid value for state->keep");
        }
 
-       write_file(am_path(state, "keep"), 1, "%s", str);
-
-       write_file(am_path(state, "messageid"), 1, state->message_id ? "t" : "f");
+       write_state_text(state, "keep", str);
+       write_state_bool(state, "messageid", state->message_id);
 
        switch (state->scissors) {
        case SCISSORS_UNSET:
@@ -1033,24 +1043,23 @@ static void am_setup(struct am_state *state, enum patch_format patch_format,
        default:
                die("BUG: invalid value for state->scissors");
        }
-
-       write_file(am_path(state, "scissors"), 1, "%s", str);
+       write_state_text(state, "scissors", str);
 
        sq_quote_argv(&sb, state->git_apply_opts.argv, 0);
-       write_file(am_path(state, "apply-opt"), 1, "%s", sb.buf);
+       write_state_text(state, "apply-opt", sb.buf);
 
        if (state->rebasing)
-               write_file(am_path(state, "rebasing"), 1, "%s", "");
+               write_state_text(state, "rebasing", "");
        else
-               write_file(am_path(state, "applying"), 1, "%s", "");
+               write_state_text(state, "applying", "");
 
        if (!get_sha1("HEAD", curr_head)) {
-               write_file(am_path(state, "abort-safety"), 1, "%s", sha1_to_hex(curr_head));
+               write_state_text(state, "abort-safety", sha1_to_hex(curr_head));
                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", "");
+               write_state_text(state, "abort-safety", "");
                if (!state->rebasing)
                        delete_ref("ORIG_HEAD", NULL, 0);
        }
@@ -1060,9 +1069,8 @@ static void am_setup(struct am_state *state, enum patch_format patch_format,
         * session is in progress, they should be written last.
         */
 
-       write_file(am_path(state, "next"), 1, "%d", state->cur);
-
-       write_file(am_path(state, "last"), 1, "%d", state->last);
+       write_state_count(state, "next", state->cur);
+       write_state_count(state, "last", state->last);
 
        strbuf_release(&sb);
 }
@@ -1095,12 +1103,12 @@ static void am_next(struct am_state *state)
        unlink(am_path(state, "original-commit"));
 
        if (!get_sha1("HEAD", head))
-               write_file(am_path(state, "abort-safety"), 1, "%s", sha1_to_hex(head));
+               write_state_text(state, "abort-safety", sha1_to_hex(head));
        else
-               write_file(am_path(state, "abort-safety"), 1, "%s", "");
+               write_state_text(state, "abort-safety", "");
 
        state->cur++;
-       write_file(am_path(state, "next"), 1, "%d", state->cur);
+       write_state_count(state, "next", state->cur);
 }
 
 /**
@@ -1185,6 +1193,45 @@ 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.
+ */
+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);
+       am_signoff(&sb);
+       state->msg = strbuf_detach(&sb, &state->msg_len);
+}
+
 /**
  * Parses `mail` using git-mailinfo, extracting its patch and authorship info.
  * state->msg will be set to the patch message. state->author_name,
@@ -1197,62 +1244,65 @@ static void NORETURN die_user_resolve(const struct am_state *state)
 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");
-       while (!strbuf_getline(&sb, fp, '\n')) {
+       while (!strbuf_getline_lf(&sb, fp)) {
                const char *x;
 
                if (skip_prefix(sb.buf, "Subject: ", &x)) {
@@ -1280,12 +1330,11 @@ 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_addbuf(&msg, &mi.log_message);
+       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);
@@ -1305,6 +1354,7 @@ static int parse_mail(struct am_state *state, const char *mail)
        strbuf_release(&author_email);
        strbuf_release(&author_name);
        strbuf_release(&sb);
+       clear_mailinfo(&mi);
        return ret;
 }
 
@@ -1318,7 +1368,7 @@ static int get_mail_commit_sha1(unsigned char *commit_id, const char *mail)
        FILE *fp = xfopen(mail, "r");
        const char *x;
 
-       if (strbuf_getline(&sb, fp, '\n'))
+       if (strbuf_getline_lf(&sb, fp))
                return -1;
 
        if (!skip_prefix(sb.buf, "From ", &x))
@@ -1376,7 +1426,7 @@ static void get_commit_info(struct am_state *state, struct commit *commit)
        assert(!state->msg);
        msg = strstr(buffer, "\n\n");
        if (!msg)
-               die(_("unable to parse commit %s"), sha1_to_hex(commit->object.sha1));
+               die(_("unable to parse commit %s"), oid_to_hex(&commit->object.oid));
        state->msg = xstrdup(msg + 2);
        state->msg_len = strlen(state->msg);
 }
@@ -1461,8 +1511,7 @@ static int parse_mail_rebase(struct am_state *state, const char *mail)
        write_commit_patch(state, commit);
 
        hashcpy(state->orig_commit, commit_sha1);
-       write_file(am_path(state, "original-commit"), 1, "%s",
-                       sha1_to_hex(commit_sha1));
+       write_state_text(state, "original-commit", sha1_to_hex(commit_sha1));
 
        return 0;
 }
@@ -1529,6 +1578,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.
  */
@@ -1536,10 +1617,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);
@@ -1565,7 +1642,7 @@ static int fall_back_threeway(const struct am_state *state, const char *index_pa
 
                init_revisions(&rev_info, NULL);
                rev_info.diffopt.output_format = DIFF_FORMAT_NAME_STATUS;
-               diff_opt_parse(&rev_info.diffopt, &diff_filter_str, 1);
+               diff_opt_parse(&rev_info.diffopt, &diff_filter_str, 1, rev_info.prefix);
                add_pending_sha1(&rev_info, "HEAD", our_tree, 0);
                diff_setup_done(&rev_info.diffopt);
                run_diff_index(&rev_info, 1);
@@ -1591,22 +1668,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;
 }
 
@@ -1764,7 +1830,7 @@ static void am_run(struct am_state *state, int resume)
        refresh_and_write_cache();
 
        if (index_has_changes(&sb)) {
-               write_file(am_path(state, "dirtyindex"), 1, "t");
+               write_state_bool(state, "dirtyindex", 1);
                die(_("Dirty index: cannot apply patches (dirty: %s)"), sb.buf);
        }
 
@@ -1779,7 +1845,6 @@ static void am_run(struct am_state *state, int resume)
 
                if (resume) {
                        validate_resume_state(state);
-                       resume = 0;
                } else {
                        int skip;
 
@@ -1841,6 +1906,10 @@ static void am_run(struct am_state *state, int resume)
 
 next:
                am_next(state);
+
+               if (resume)
+                       am_load(state);
+               resume = 0;
        }
 
        if (!is_empty_file(am_path(state, "rewritten"))) {
@@ -1855,6 +1924,7 @@ static void am_run(struct am_state *state, int resume)
         */
        if (!state->rebasing) {
                am_destroy(state);
+               close_all_packs();
                run_command_v_opt(argv_gc_auto, RUN_GIT_CMD);
        }
 }
@@ -1895,6 +1965,7 @@ static void am_resolve(struct am_state *state)
 
 next:
        am_next(state);
+       am_load(state);
        am_run(state, 0);
 }
 
@@ -1939,16 +2010,49 @@ static int fast_forward_to(struct tree *head, struct tree *remote, int reset)
        return 0;
 }
 
+/**
+ * Merges a tree into the index. The index's stat info will take precedence
+ * over the merged tree's. Returns 0 on success, -1 on failure.
+ */
+static int merge_tree(struct tree *tree)
+{
+       struct lock_file *lock_file;
+       struct unpack_trees_options opts;
+       struct tree_desc t[1];
+
+       if (parse_tree(tree))
+               return -1;
+
+       lock_file = xcalloc(1, sizeof(struct lock_file));
+       hold_locked_index(lock_file, 1);
+
+       memset(&opts, 0, sizeof(opts));
+       opts.head_idx = 1;
+       opts.src_index = &the_index;
+       opts.dst_index = &the_index;
+       opts.merge = 1;
+       opts.fn = oneway_merge;
+       init_tree_desc(&t[0], tree->buffer, tree->size);
+
+       if (unpack_trees(1, t, &opts)) {
+               rollback_lock_file(lock_file);
+               return -1;
+       }
+
+       if (write_locked_index(&the_index, lock_file, COMMIT_LOCK))
+               die(_("unable to write new index file"));
+
+       return 0;
+}
+
 /**
  * Clean the index without touching entries that are not modified between
  * `head` and `remote`.
  */
 static int clean_index(const unsigned char *head, const unsigned char *remote)
 {
-       struct lock_file *lock_file;
        struct tree *head_tree, *remote_tree, *index_tree;
        unsigned char index[GIT_SHA1_RAWSZ];
-       struct pathspec pathspec;
 
        head_tree = parse_tree_indirect(head);
        if (!head_tree)
@@ -1973,18 +2077,8 @@ static int clean_index(const unsigned char *head, const unsigned char *remote)
        if (fast_forward_to(index_tree, remote_tree, 0))
                return -1;
 
-       memset(&pathspec, 0, sizeof(pathspec));
-
-       lock_file = xcalloc(1, sizeof(struct lock_file));
-       hold_locked_index(lock_file, 1);
-
-       if (read_tree(remote_tree, 0, &pathspec)) {
-               rollback_lock_file(lock_file);
+       if (merge_tree(remote_tree))
                return -1;
-       }
-
-       if (write_locked_index(&the_index, lock_file, COMMIT_LOCK))
-               die(_("unable to write new index file"));
 
        remove_branch_state();
 
@@ -1997,11 +2091,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);
 }
@@ -2022,6 +2111,7 @@ static void am_skip(struct am_state *state)
                die(_("failed to clean index"));
 
        am_next(state);
+       am_load(state);
        am_run(state, 0);
 }
 
@@ -2125,6 +2215,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;
@@ -2132,10 +2233,11 @@ int cmd_am(int argc, const char **argv, const char *prefix)
        int keep_cr = -1;
        int patch_format = PATCH_FORMAT_UNKNOWN;
        enum resume_mode resume = RESUME_FALSE;
+       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
        };
 
@@ -2143,12 +2245,13 @@ int cmd_am(int argc, const char **argv, const char *prefix)
                OPT_BOOL('i', "interactive", &state.interactive,
                        N_("run interactively")),
                OPT_HIDDEN_BOOL('b', "binary", &binary,
-                       N_("(historical option -- no-op")),
+                       N_("historical option -- no-op")),
                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_SET_INT('s', "signoff", &state.signoff,
+                       N_("add a Signed-off-by line to the commit message"),
+                       SIGNOFF_EXPLICIT),
                OPT_BOOL('u', "utf8", &state.utf8,
                        N_("recode into utf8 (default)")),
                OPT_SET_INT('k', "keep", &state.keep,
@@ -2223,10 +2326,14 @@ 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"));
 
+       in_progress = am_in_progress(&state);
+       if (in_progress)
+               am_load(&state);
+
        argc = parse_options(argc, argv, prefix, options, usage, 0);
 
        if (binary >= 0)
@@ -2239,7 +2346,7 @@ int cmd_am(int argc, const char **argv, const char *prefix)
        if (read_index_preload(&the_index, NULL) < 0)
                die(_("failed to read the index"));
 
-       if (am_in_progress(&state)) {
+       if (in_progress) {
                /*
                 * Catch user error to feed us patches when there is a session
                 * in progress:
@@ -2258,7 +2365,8 @@ int cmd_am(int argc, const char **argv, const char *prefix)
                if (resume == RESUME_FALSE)
                        resume = RESUME_APPLY;
 
-               am_load(&state);
+               if (state.signoff == SIGNOFF_EXPLICIT)
+                       am_append_signoff(&state);
        } else {
                struct argv_array paths = ARGV_ARRAY_INIT;
                int i;