t0302 & t3900: add forgotten quotes
[gitweb.git] / builtin / log.c
index 0d738d6ddc0d6335e15392dd90e4118e2cb58b88..57ce470f5094c238ea4d5390f358b5cdd9499a56 100644 (file)
@@ -33,6 +33,7 @@ static const char *default_date_mode = NULL;
 static int default_abbrev_commit;
 static int default_show_root = 1;
 static int default_follow;
+static int default_show_signature;
 static int decoration_style;
 static int decoration_given;
 static int use_mailmap_config;
@@ -51,6 +52,11 @@ struct line_opt_callback_data {
        struct string_list args;
 };
 
+static int auto_decoration_style(void)
+{
+       return (isatty(1) || pager_in_use()) ? DECORATE_SHORT_REFS : 0;
+}
+
 static int parse_decoration_style(const char *var, const char *value)
 {
        switch (git_config_maybe_bool(var, value)) {
@@ -66,7 +72,7 @@ static int parse_decoration_style(const char *var, const char *value)
        else if (!strcmp(value, "short"))
                return DECORATE_SHORT_REFS;
        else if (!strcmp(value, "auto"))
-               return (isatty(1) || pager_in_use()) ? DECORATE_SHORT_REFS : 0;
+               return auto_decoration_style();
        return -1;
 }
 
@@ -100,6 +106,14 @@ static int log_line_range_callback(const struct option *option, const char *arg,
        return 0;
 }
 
+static void init_log_defaults(void)
+{
+       init_grep_defaults();
+       init_diff_ui_defaults();
+
+       decoration_style = auto_decoration_style();
+}
+
 static void cmd_log_init_defaults(struct rev_info *rev)
 {
        if (fmt_pretty)
@@ -113,6 +127,7 @@ static void cmd_log_init_defaults(struct rev_info *rev)
        rev->abbrev_commit = default_abbrev_commit;
        rev->show_root_diff = default_show_root;
        rev->subject_prefix = fmt_patch_subject_prefix;
+       rev->show_signature = default_show_signature;
        DIFF_OPT_SET(&rev->diffopt, ALLOW_TEXTCONV);
 
        if (default_date_mode)
@@ -230,16 +245,17 @@ static void show_early_header(struct rev_info *rev, const char *stage, int nr)
                if (rev->commit_format != CMIT_FMT_ONELINE)
                        putchar(rev->diffopt.line_termination);
        }
-       printf(_("Final output: %d %s\n"), nr, stage);
+       fprintf(rev->diffopt.file, _("Final output: %d %s\n"), nr, stage);
 }
 
 static struct itimerval early_output_timer;
 
 static void log_show_early(struct rev_info *revs, struct commit_list *list)
 {
-       int i = revs->early_output;
+       int i = revs->early_output, close_file = revs->diffopt.close_file;
        int show_header = 1;
 
+       revs->diffopt.close_file = 0;
        sort_in_topological_order(&list, revs->sort_order);
        while (list && i) {
                struct commit *commit = list->item;
@@ -256,14 +272,19 @@ static void log_show_early(struct rev_info *revs, struct commit_list *list)
                case commit_ignore:
                        break;
                case commit_error:
+                       if (close_file)
+                               fclose(revs->diffopt.file);
                        return;
                }
                list = list->next;
        }
 
        /* Did we already get enough commits for the early output? */
-       if (!i)
+       if (!i) {
+               if (close_file)
+                       fclose(revs->diffopt.file);
                return;
+       }
 
        /*
         * ..if no, then repeat it twice a second until we
@@ -325,7 +346,7 @@ static int cmd_log_walk(struct rev_info *rev)
 {
        struct commit *commit;
        int saved_nrl = 0;
-       int saved_dcctc = 0;
+       int saved_dcctc = 0, close_file = rev->diffopt.close_file;
 
        if (rev->early_output)
                setup_early_output(rev);
@@ -341,6 +362,7 @@ static int cmd_log_walk(struct rev_info *rev)
         * and HAS_CHANGES being accumulated in rev->diffopt, so be careful to
         * retain that state information if replacing rev->diffopt in this loop
         */
+       rev->diffopt.close_file = 0;
        while ((commit = get_revision(rev)) != NULL) {
                if (!log_tree_commit(rev, commit) && rev->max_count >= 0)
                        /*
@@ -361,6 +383,8 @@ static int cmd_log_walk(struct rev_info *rev)
        }
        rev->diffopt.degraded_cc_to_c = saved_dcctc;
        rev->diffopt.needed_rename_limit = saved_nrl;
+       if (close_file)
+               fclose(rev->diffopt.file);
 
        if (rev->diffopt.output_format & DIFF_FORMAT_CHECKDIFF &&
            DIFF_OPT_TST(&rev->diffopt, CHECK_FAILED)) {
@@ -403,6 +427,10 @@ static int git_log_config(const char *var, const char *value, void *cb)
                use_mailmap_config = git_config_bool(var, value);
                return 0;
        }
+       if (!strcmp(var, "log.showsignature")) {
+               default_show_signature = git_config_bool(var, value);
+               return 0;
+       }
 
        if (grep_config(var, value, cb) < 0)
                return -1;
@@ -416,7 +444,7 @@ int cmd_whatchanged(int argc, const char **argv, const char *prefix)
        struct rev_info rev;
        struct setup_revision_opt opt;
 
-       init_grep_defaults();
+       init_log_defaults();
        git_config(git_log_config, NULL);
 
        init_revisions(&rev, prefix);
@@ -439,44 +467,48 @@ static void show_tagger(char *buf, int len, struct rev_info *rev)
        pp.fmt = rev->commit_format;
        pp.date_mode = rev->date_mode;
        pp_user_info(&pp, "Tagger", &out, buf, get_log_output_encoding());
-       printf("%s", out.buf);
+       fprintf(rev->diffopt.file, "%s", out.buf);
        strbuf_release(&out);
 }
 
-static int show_blob_object(const unsigned char *sha1, struct rev_info *rev, const char *obj_name)
+static int show_blob_object(const struct object_id *oid, struct rev_info *rev, const char *obj_name)
 {
-       unsigned char sha1c[20];
+       struct object_id oidc;
        struct object_context obj_context;
        char *buf;
        unsigned long size;
 
-       fflush(stdout);
+       fflush(rev->diffopt.file);
        if (!DIFF_OPT_TOUCHED(&rev->diffopt, ALLOW_TEXTCONV) ||
            !DIFF_OPT_TST(&rev->diffopt, ALLOW_TEXTCONV))
-               return stream_blob_to_fd(1, sha1, NULL, 0);
+               return stream_blob_to_fd(1, oid, NULL, 0);
 
-       if (get_sha1_with_context(obj_name, 0, sha1c, &obj_context))
+       if (get_sha1_with_context(obj_name, GET_SHA1_RECORD_PATH,
+                                 oidc.hash, &obj_context))
                die(_("Not a valid object name %s"), obj_name);
-       if (!obj_context.path[0] ||
-           !textconv_object(obj_context.path, obj_context.mode, sha1c, 1, &buf, &size))
-               return stream_blob_to_fd(1, sha1, NULL, 0);
+       if (!obj_context.path ||
+           !textconv_object(obj_context.path, obj_context.mode, &oidc, 1, &buf, &size)) {
+               free(obj_context.path);
+               return stream_blob_to_fd(1, oid, NULL, 0);
+       }
 
        if (!buf)
                die(_("git show %s: bad file"), obj_name);
 
        write_or_die(1, buf, size);
+       free(obj_context.path);
        return 0;
 }
 
-static int show_tag_object(const unsigned char *sha1, struct rev_info *rev)
+static int show_tag_object(const struct object_id *oid, struct rev_info *rev)
 {
        unsigned long size;
        enum object_type type;
-       char *buf = read_sha1_file(sha1, &type, &size);
+       char *buf = read_sha1_file(oid->hash, &type, &size);
        int offset = 0;
 
        if (!buf)
-               return error(_("Could not read object %s"), sha1_to_hex(sha1));
+               return error(_("Could not read object %s"), oid_to_hex(oid));
 
        assert(type == OBJ_TAG);
        while (offset < size && buf[offset] != '\n') {
@@ -490,7 +522,7 @@ static int show_tag_object(const unsigned char *sha1, struct rev_info *rev)
        }
 
        if (offset < size)
-               fwrite(buf + offset, size - offset, 1, stdout);
+               fwrite(buf + offset, size - offset, 1, rev->diffopt.file);
        free(buf);
        return 0;
 }
@@ -499,7 +531,8 @@ static int show_tree_object(const unsigned char *sha1,
                struct strbuf *base,
                const char *pathname, unsigned mode, int stage, void *context)
 {
-       printf("%s%s\n", pathname, S_ISDIR(mode) ? "/" : "");
+       FILE *file = context;
+       fprintf(file, "%s%s\n", pathname, S_ISDIR(mode) ? "/" : "");
        return 0;
 }
 
@@ -527,7 +560,7 @@ int cmd_show(int argc, const char **argv, const char *prefix)
        struct pathspec match_all;
        int i, count, ret = 0;
 
-       init_grep_defaults();
+       init_log_defaults();
        git_config(git_log_config, NULL);
 
        memset(&match_all, 0, sizeof(match_all));
@@ -552,18 +585,18 @@ int cmd_show(int argc, const char **argv, const char *prefix)
                const char *name = objects[i].name;
                switch (o->type) {
                case OBJ_BLOB:
-                       ret = show_blob_object(o->oid.hash, &rev, name);
+                       ret = show_blob_object(&o->oid, &rev, name);
                        break;
                case OBJ_TAG: {
                        struct tag *t = (struct tag *)o;
 
                        if (rev.shown_one)
                                putchar('\n');
-                       printf("%stag %s%s\n",
+                       fprintf(rev.diffopt.file, "%stag %s%s\n",
                                        diff_get_color_opt(&rev.diffopt, DIFF_COMMIT),
                                        t->tag,
                                        diff_get_color_opt(&rev.diffopt, DIFF_RESET));
-                       ret = show_tag_object(o->oid.hash, &rev);
+                       ret = show_tag_object(&o->oid, &rev);
                        rev.shown_one = 1;
                        if (ret)
                                break;
@@ -578,12 +611,12 @@ int cmd_show(int argc, const char **argv, const char *prefix)
                case OBJ_TREE:
                        if (rev.shown_one)
                                putchar('\n');
-                       printf("%stree %s%s\n\n",
+                       fprintf(rev.diffopt.file, "%stree %s%s\n\n",
                                        diff_get_color_opt(&rev.diffopt, DIFF_COMMIT),
                                        name,
                                        diff_get_color_opt(&rev.diffopt, DIFF_RESET));
                        read_tree_recursive((struct tree *)o, "", 0, 0, &match_all,
-                                       show_tree_object, NULL);
+                                       show_tree_object, rev.diffopt.file);
                        rev.shown_one = 1;
                        break;
                case OBJ_COMMIT:
@@ -608,7 +641,7 @@ int cmd_log_reflog(int argc, const char **argv, const char *prefix)
        struct rev_info rev;
        struct setup_revision_opt opt;
 
-       init_grep_defaults();
+       init_log_defaults();
        git_config(git_log_config, NULL);
 
        init_revisions(&rev, prefix);
@@ -647,7 +680,7 @@ int cmd_log(int argc, const char **argv, const char *prefix)
        struct rev_info rev;
        struct setup_revision_opt opt;
 
-       init_grep_defaults();
+       init_log_defaults();
        git_config(git_log_config, NULL);
 
        init_revisions(&rev, prefix);
@@ -668,9 +701,9 @@ static int auto_number = 1;
 
 static char *default_attach = NULL;
 
-static struct string_list extra_hdr;
-static struct string_list extra_to;
-static struct string_list extra_cc;
+static struct string_list extra_hdr = STRING_LIST_INIT_NODUP;
+static struct string_list extra_to = STRING_LIST_INIT_NODUP;
+static struct string_list extra_cc = STRING_LIST_INIT_NODUP;
 
 static void add_header(const char *value)
 {
@@ -696,6 +729,8 @@ static void add_header(const char *value)
 #define THREAD_DEEP 2
 static int thread;
 static int do_signoff;
+static int base_auto;
+static char *from;
 static const char *signature = git_version_string;
 static const char *signature_file;
 static int config_cover_letter;
@@ -780,15 +815,29 @@ static int git_format_config(const char *var, const char *value, void *cb)
        }
        if (!strcmp(var, "format.outputdirectory"))
                return git_config_string(&config_output_directory, var, value);
+       if (!strcmp(var, "format.useautobase")) {
+               base_auto = git_config_bool(var, value);
+               return 0;
+       }
+       if (!strcmp(var, "format.from")) {
+               int b = git_config_maybe_bool(var, value);
+               free(from);
+               if (b < 0)
+                       from = xstrdup(value);
+               else if (b)
+                       from = xstrdup(git_committer_info(IDENT_NO_DATE));
+               else
+                       from = NULL;
+               return 0;
+       }
 
        return git_log_config(var, value, cb);
 }
 
-static FILE *realstdout = NULL;
 static const char *output_directory = NULL;
 static int outdir_offset;
 
-static int reopen_stdout(struct commit *commit, const char *subject,
+static int open_next_file(struct commit *commit, const char *subject,
                         struct rev_info *rev, int quiet)
 {
        struct strbuf filename = STRBUF_INIT;
@@ -810,9 +859,9 @@ static int reopen_stdout(struct commit *commit, const char *subject,
                fmt_output_subject(&filename, subject, rev);
 
        if (!quiet)
-               fprintf(realstdout, "%s\n", filename.buf + outdir_offset);
+               printf("%s\n", filename.buf + outdir_offset);
 
-       if (freopen(filename.buf, "w", stdout) == NULL)
+       if ((rev->diffopt.file = fopen(filename.buf, "w")) == NULL)
                return error(_("Cannot open patch file %s"), filename.buf);
 
        strbuf_release(&filename);
@@ -871,15 +920,15 @@ static void gen_message_id(struct rev_info *info, char *base)
        info->message_id = strbuf_detach(&buf, NULL);
 }
 
-static void print_signature(void)
+static void print_signature(FILE *file)
 {
        if (!signature || !*signature)
                return;
 
-       printf("-- \n%s", signature);
+       fprintf(file, "-- \n%s", signature);
        if (signature[strlen(signature)-1] != '\n')
-               putchar('\n');
-       putchar('\n');
+               putc('\n', file);
+       putc('\n', file);
 }
 
 static void add_branch_description(struct strbuf *buf, const char *branch_name)
@@ -942,17 +991,16 @@ static void make_cover_letter(struct rev_info *rev, int use_stdout,
        struct pretty_print_context pp = {0};
        struct commit *head = list[0];
 
-       if (rev->commit_format != CMIT_FMT_EMAIL)
+       if (!cmit_fmt_is_mail(rev->commit_format))
                die(_("Cover letter needs email format"));
 
        committer = git_committer_info(0);
 
        if (!use_stdout &&
-           reopen_stdout(NULL, rev->numbered_files ? NULL : "cover-letter", rev, quiet))
+           open_next_file(NULL, rev->numbered_files ? NULL : "cover-letter", rev, quiet))
                return;
 
-       log_write_email_headers(rev, head, &pp.subject, &pp.after_subject,
-                               &need_8bit_cte);
+       log_write_email_headers(rev, head, &pp.after_subject, &need_8bit_cte);
 
        for (i = 0; !need_8bit_cte && i < nr; i++) {
                const char *buf = get_commit_buffer(list[i], NULL);
@@ -967,11 +1015,13 @@ static void make_cover_letter(struct rev_info *rev, int use_stdout,
        msg = body;
        pp.fmt = CMIT_FMT_EMAIL;
        pp.date_mode.type = DATE_RFC2822;
+       pp.rev = rev;
+       pp.print_email_subject = 1;
        pp_user_info(&pp, NULL, &sb, committer, encoding);
        pp_title_line(&pp, &msg, &sb, encoding, need_8bit_cte);
        pp_remainder(&pp, &msg, &sb, 0);
        add_branch_description(&sb, branch_name);
-       printf("%s\n", sb.buf);
+       fprintf(rev->diffopt.file, "%s\n", sb.buf);
 
        strbuf_release(&sb);
 
@@ -980,6 +1030,7 @@ static void make_cover_letter(struct rev_info *rev, int use_stdout,
        log.wrap = 72;
        log.in1 = 2;
        log.in2 = 4;
+       log.file = rev->diffopt.file;
        for (i = 0; i < nr; i++)
                shortlog_add_commit(&log, list[i]);
 
@@ -1002,8 +1053,7 @@ static void make_cover_letter(struct rev_info *rev, int use_stdout,
        diffcore_std(&opts);
        diff_flush(&opts);
 
-       printf("\n");
-       print_signature();
+       fprintf(rev->diffopt.file, "\n");
 }
 
 static const char *clean_message_id(const char *msg_id)
@@ -1045,8 +1095,7 @@ static const char *set_outdir(const char *prefix, const char *output_directory)
        if (!output_directory)
                return prefix;
 
-       return xstrdup(prefix_filename(prefix, outdir_offset,
-                                      output_directory));
+       return prefix_filename(prefix, output_directory);
 }
 
 static const char * const builtin_format_patch_usage[] = {
@@ -1073,6 +1122,11 @@ static int subject_prefix_callback(const struct option *opt, const char *arg,
        return 0;
 }
 
+static int rfc_callback(const struct option *opt, const char *arg, int unset)
+{
+       return subject_prefix_callback(opt, "RFC PATCH", unset);
+}
+
 static int numbered_cmdline_opt = 0;
 
 static int numbered_callback(const struct option *opt, const char *arg,
@@ -1185,6 +1239,155 @@ static int from_callback(const struct option *opt, const char *arg, int unset)
        return 0;
 }
 
+struct base_tree_info {
+       struct object_id base_commit;
+       int nr_patch_id, alloc_patch_id;
+       struct object_id *patch_id;
+};
+
+static struct commit *get_base_commit(const char *base_commit,
+                                     struct commit **list,
+                                     int total)
+{
+       struct commit *base = NULL;
+       struct commit **rev;
+       int i = 0, rev_nr = 0;
+
+       if (base_commit && strcmp(base_commit, "auto")) {
+               base = lookup_commit_reference_by_name(base_commit);
+               if (!base)
+                       die(_("Unknown commit %s"), base_commit);
+       } else if ((base_commit && !strcmp(base_commit, "auto")) || base_auto) {
+               struct branch *curr_branch = branch_get(NULL);
+               const char *upstream = branch_get_upstream(curr_branch, NULL);
+               if (upstream) {
+                       struct commit_list *base_list;
+                       struct commit *commit;
+                       struct object_id oid;
+
+                       if (get_oid(upstream, &oid))
+                               die(_("Failed to resolve '%s' as a valid ref."), upstream);
+                       commit = lookup_commit_or_die(oid.hash, "upstream base");
+                       base_list = get_merge_bases_many(commit, total, list);
+                       /* There should be one and only one merge base. */
+                       if (!base_list || base_list->next)
+                               die(_("Could not find exact merge base."));
+                       base = base_list->item;
+                       free_commit_list(base_list);
+               } else {
+                       die(_("Failed to get upstream, if you want to record base commit automatically,\n"
+                             "please use git branch --set-upstream-to to track a remote branch.\n"
+                             "Or you could specify base commit by --base=<base-commit-id> manually."));
+               }
+       }
+
+       ALLOC_ARRAY(rev, total);
+       for (i = 0; i < total; i++)
+               rev[i] = list[i];
+
+       rev_nr = total;
+       /*
+        * Get merge base through pair-wise computations
+        * and store it in rev[0].
+        */
+       while (rev_nr > 1) {
+               for (i = 0; i < rev_nr / 2; i++) {
+                       struct commit_list *merge_base;
+                       merge_base = get_merge_bases(rev[2 * i], rev[2 * i + 1]);
+                       if (!merge_base || merge_base->next)
+                               die(_("Failed to find exact merge base"));
+
+                       rev[i] = merge_base->item;
+               }
+
+               if (rev_nr % 2)
+                       rev[i] = rev[2 * i];
+               rev_nr = (rev_nr + 1) / 2;
+       }
+
+       if (!in_merge_bases(base, rev[0]))
+               die(_("base commit should be the ancestor of revision list"));
+
+       for (i = 0; i < total; i++) {
+               if (base == list[i])
+                       die(_("base commit shouldn't be in revision list"));
+       }
+
+       free(rev);
+       return base;
+}
+
+static void prepare_bases(struct base_tree_info *bases,
+                         struct commit *base,
+                         struct commit **list,
+                         int total)
+{
+       struct commit *commit;
+       struct rev_info revs;
+       struct diff_options diffopt;
+       int i;
+
+       if (!base)
+               return;
+
+       diff_setup(&diffopt);
+       DIFF_OPT_SET(&diffopt, RECURSIVE);
+       diff_setup_done(&diffopt);
+
+       oidcpy(&bases->base_commit, &base->object.oid);
+
+       init_revisions(&revs, NULL);
+       revs.max_parents = 1;
+       revs.topo_order = 1;
+       for (i = 0; i < total; i++) {
+               list[i]->object.flags &= ~UNINTERESTING;
+               add_pending_object(&revs, &list[i]->object, "rev_list");
+               list[i]->util = (void *)1;
+       }
+       base->object.flags |= UNINTERESTING;
+       add_pending_object(&revs, &base->object, "base");
+
+       if (prepare_revision_walk(&revs))
+               die(_("revision walk setup failed"));
+       /*
+        * Traverse the commits list, get prerequisite patch ids
+        * and stuff them in bases structure.
+        */
+       while ((commit = get_revision(&revs)) != NULL) {
+               struct object_id oid;
+               struct object_id *patch_id;
+               if (commit->util)
+                       continue;
+               if (commit_patch_id(commit, &diffopt, oid.hash, 0))
+                       die(_("cannot get patch id"));
+               ALLOC_GROW(bases->patch_id, bases->nr_patch_id + 1, bases->alloc_patch_id);
+               patch_id = bases->patch_id + bases->nr_patch_id;
+               oidcpy(patch_id, &oid);
+               bases->nr_patch_id++;
+       }
+}
+
+static void print_bases(struct base_tree_info *bases, FILE *file)
+{
+       int i;
+
+       /* Only do this once, either for the cover or for the first one */
+       if (is_null_oid(&bases->base_commit))
+               return;
+
+       /* Show the base commit */
+       fprintf(file, "\nbase-commit: %s\n", oid_to_hex(&bases->base_commit));
+
+       /* Show the prerequisite patches */
+       for (i = bases->nr_patch_id - 1; i >= 0; i--)
+               fprintf(file, "prerequisite-patch-id: %s\n", oid_to_hex(&bases->patch_id[i]));
+
+       free(bases->patch_id);
+       bases->nr_patch_id = 0;
+       bases->alloc_patch_id = 0;
+       oidclr(&bases->base_commit);
+}
+
 int cmd_format_patch(int argc, const char **argv, const char *prefix)
 {
        struct commit *commit;
@@ -1208,7 +1411,9 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
        int quiet = 0;
        int reroll_count = -1;
        char *branch_name = NULL;
-       char *from = NULL;
+       char *base_commit = NULL;
+       struct base_tree_info bases;
+
        const struct option builtin_format_patch_options[] = {
                { OPTION_CALLBACK, 'n', "numbered", &numbered, NULL,
                            N_("use [PATCH n/m] even with a single patch"),
@@ -1229,6 +1434,9 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
                            N_("start numbering patches at <n> instead of 1")),
                OPT_INTEGER('v', "reroll-count", &reroll_count,
                            N_("mark the series as Nth re-roll")),
+               { OPTION_CALLBACK, 0, "rfc", &rev, NULL,
+                           N_("Use [RFC PATCH] instead of [PATCH]"),
+                           PARSE_OPT_NOARG | PARSE_OPT_NONEG, rfc_callback },
                { OPTION_CALLBACK, 0, "subject-prefix", &rev, N_("prefix"),
                            N_("Use [<prefix>] instead of [PATCH]"),
                            PARSE_OPT_NONEG, subject_prefix_callback },
@@ -1271,6 +1479,8 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
                            PARSE_OPT_OPTARG, thread_callback },
                OPT_STRING(0, "signature", &signature, N_("signature"),
                            N_("add a signature")),
+               OPT_STRING(0, "base", &base_commit, N_("base-commit"),
+                          N_("add prerequisite tree info to the patch series")),
                OPT_FILENAME(0, "signature-file", &signature_file,
                                N_("add a signature from a file")),
                OPT__QUIET(&quiet, N_("don't print the patch filenames")),
@@ -1280,10 +1490,11 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
        extra_hdr.strdup_strings = 1;
        extra_to.strdup_strings = 1;
        extra_cc.strdup_strings = 1;
-       init_grep_defaults();
+       init_log_defaults();
        git_config(git_format_config, NULL);
        init_revisions(&rev, prefix);
        rev.commit_format = CMIT_FMT_EMAIL;
+       rev.expand_tabs_in_log_default = 0;
        rev.verbose_header = 1;
        rev.diff = 1;
        rev.max_parents = 1;
@@ -1364,7 +1575,7 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
        if (numbered && keep_subject)
                die (_("-n and -k are mutually exclusive."));
        if (keep_subject && subject_prefix)
-               die (_("--subject-prefix and -k are mutually exclusive."));
+               die (_("--subject-prefix/--rfc and -k are mutually exclusive."));
        rev.preserve_subject = keep_subject;
 
        argc = setup_revisions(argc, argv, &rev, &s_r_opt);
@@ -1403,6 +1614,8 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
                setup_pager();
 
        if (output_directory) {
+               if (rev.diffopt.use_color != GIT_COLOR_ALWAYS)
+                       rev.diffopt.use_color = GIT_COLOR_NEVER;
                if (use_stdout)
                        die(_("standard output, or directory, which one?"));
                if (mkdir(output_directory, 0777) < 0 && errno != EEXIST)
@@ -1433,10 +1646,10 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
                        check_head = 1;
 
                if (check_head) {
-                       unsigned char sha1[20];
+                       struct object_id oid;
                        const char *ref, *v;
                        ref = resolve_ref_unsafe("HEAD", RESOLVE_REF_READING,
-                                                sha1, NULL);
+                                                oid.hash, NULL);
                        if (ref && skip_prefix(ref, "refs/heads/", &v))
                                branch_name = xstrdup(v);
                        else
@@ -1460,9 +1673,6 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
                get_patch_ids(&rev, &ids);
        }
 
-       if (!use_stdout)
-               realstdout = xfdopen(xdup(1), "w");
-
        if (prepare_revision_walk(&rev))
                die(_("revision walk setup failed"));
        rev.boundary = 1;
@@ -1484,16 +1694,16 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
                /* nothing to do */
                return 0;
        total = nr;
-       if (!keep_subject && auto_number && total > 1)
-               numbered = 1;
-       if (numbered)
-               rev.total = total + start_number - 1;
        if (cover_letter == -1) {
                if (config_cover_letter == COVER_AUTO)
                        cover_letter = (total > 1);
                else
                        cover_letter = (config_cover_letter == COVER_ON);
        }
+       if (!keep_subject && auto_number && (total > 1 || cover_letter))
+               numbered = 1;
+       if (numbered)
+               rev.total = total + start_number - 1;
 
        if (!signature) {
                ; /* --no-signature inhibits all signatures */
@@ -1507,6 +1717,13 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
                signature = strbuf_detach(&buf, NULL);
        }
 
+       memset(&bases, 0, sizeof(bases));
+       if (base_commit || base_auto) {
+               struct commit *base = get_base_commit(base_commit, list, nr);
+               reset_revision_walk();
+               prepare_bases(&bases, base, list, nr);
+       }
+
        if (in_reply_to || thread || cover_letter)
                rev.ref_message_ids = xcalloc(1, sizeof(struct string_list));
        if (in_reply_to) {
@@ -1520,6 +1737,8 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
                        gen_message_id(&rev, "cover");
                make_cover_letter(&rev, use_stdout,
                                  origin, nr, list, branch_name, quiet);
+               print_bases(&bases, rev.diffopt.file);
+               print_signature(rev.diffopt.file);
                total++;
                start_number--;
        }
@@ -1565,7 +1784,7 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
                }
 
                if (!use_stdout &&
-                   reopen_stdout(rev.numbered_files ? NULL : commit, NULL, &rev, quiet))
+                   open_next_file(rev.numbered_files ? NULL : commit, NULL, &rev, quiet))
                        die(_("Failed to create output files"));
                shown = log_tree_commit(&rev, commit);
                free_commit_buffer(commit);
@@ -1579,15 +1798,16 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
                if (!use_stdout)
                        rev.shown_one = 0;
                if (shown) {
+                       print_bases(&bases, rev.diffopt.file);
                        if (rev.mime_boundary)
-                               printf("\n--%s%s--\n\n\n",
+                               fprintf(rev.diffopt.file, "\n--%s%s--\n\n\n",
                                       mime_boundary_leader,
                                       rev.mime_boundary);
                        else
-                               print_signature();
+                               print_signature(rev.diffopt.file);
                }
                if (!use_stdout)
-                       fclose(stdout);
+                       fclose(rev.diffopt.file);
        }
        free(list);
        free(branch_name);
@@ -1601,9 +1821,9 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
 
 static int add_pending_commit(const char *arg, struct rev_info *revs, int flags)
 {
-       unsigned char sha1[20];
-       if (get_sha1(arg, sha1) == 0) {
-               struct commit *commit = lookup_commit_reference(sha1);
+       struct object_id oid;
+       if (get_oid(arg, &oid) == 0) {
+               struct commit *commit = lookup_commit_reference(oid.hash);
                if (commit) {
                        commit->object.flags |= flags;
                        add_pending_object(revs, &commit->object, arg);
@@ -1619,15 +1839,15 @@ static const char * const cherry_usage[] = {
 };
 
 static void print_commit(char sign, struct commit *commit, int verbose,
-                        int abbrev)
+                        int abbrev, FILE *file)
 {
        if (!verbose) {
-               printf("%c %s\n", sign,
+               fprintf(file, "%c %s\n", sign,
                       find_unique_abbrev(commit->object.oid.hash, abbrev));
        } else {
                struct strbuf buf = STRBUF_INIT;
                pp_commit_easy(CMIT_FMT_ONELINE, commit, &buf);
-               printf("%c %s %s\n", sign,
+               fprintf(file, "%c %s %s\n", sign,
                       find_unique_abbrev(commit->object.oid.hash, abbrev),
                       buf.buf);
                strbuf_release(&buf);
@@ -1708,7 +1928,7 @@ int cmd_cherry(int argc, const char **argv, const char *prefix)
                commit = list->item;
                if (has_commit_patch_id(commit, &ids))
                        sign = '-';
-               print_commit(sign, commit, verbose, abbrev);
+               print_commit(sign, commit, verbose, abbrev, revs.diffopt.file);
                list = list->next;
        }