builtin / log.con commit Merge branch 'jc/post-c89-rules-doc' (fe9dc6b)
   1/*
   2 * Builtin "git log" and related commands (show, whatchanged)
   3 *
   4 * (C) Copyright 2006 Linus Torvalds
   5 *               2006 Junio Hamano
   6 */
   7#define USE_THE_INDEX_COMPATIBILITY_MACROS
   8#include "cache.h"
   9#include "config.h"
  10#include "refs.h"
  11#include "object-store.h"
  12#include "color.h"
  13#include "commit.h"
  14#include "diff.h"
  15#include "revision.h"
  16#include "log-tree.h"
  17#include "builtin.h"
  18#include "tag.h"
  19#include "reflog-walk.h"
  20#include "patch-ids.h"
  21#include "run-command.h"
  22#include "shortlog.h"
  23#include "remote.h"
  24#include "string-list.h"
  25#include "parse-options.h"
  26#include "line-log.h"
  27#include "branch.h"
  28#include "streaming.h"
  29#include "version.h"
  30#include "mailmap.h"
  31#include "gpg-interface.h"
  32#include "progress.h"
  33#include "commit-slab.h"
  34#include "repository.h"
  35#include "commit-reach.h"
  36#include "interdiff.h"
  37#include "range-diff.h"
  38
  39#define MAIL_DEFAULT_WRAP 72
  40
  41/* Set a default date-time format for git log ("log.date" config variable) */
  42static const char *default_date_mode = NULL;
  43
  44static int default_abbrev_commit;
  45static int default_show_root = 1;
  46static int default_follow;
  47static int default_show_signature;
  48static int decoration_style;
  49static int decoration_given;
  50static int use_mailmap_config = -1;
  51static const char *fmt_patch_subject_prefix = "PATCH";
  52static const char *fmt_pretty;
  53
  54static const char * const builtin_log_usage[] = {
  55        N_("git log [<options>] [<revision-range>] [[--] <path>...]"),
  56        N_("git show [<options>] <object>..."),
  57        NULL
  58};
  59
  60struct line_opt_callback_data {
  61        struct rev_info *rev;
  62        const char *prefix;
  63        struct string_list args;
  64};
  65
  66static int session_is_interactive(void)
  67{
  68        return isatty(1) || pager_in_use();
  69}
  70
  71static int auto_decoration_style(void)
  72{
  73        return session_is_interactive() ? DECORATE_SHORT_REFS : 0;
  74}
  75
  76static int parse_decoration_style(const char *value)
  77{
  78        switch (git_parse_maybe_bool(value)) {
  79        case 1:
  80                return DECORATE_SHORT_REFS;
  81        case 0:
  82                return 0;
  83        default:
  84                break;
  85        }
  86        if (!strcmp(value, "full"))
  87                return DECORATE_FULL_REFS;
  88        else if (!strcmp(value, "short"))
  89                return DECORATE_SHORT_REFS;
  90        else if (!strcmp(value, "auto"))
  91                return auto_decoration_style();
  92        /*
  93         * Please update _git_log() in git-completion.bash when you
  94         * add new decoration styles.
  95         */
  96        return -1;
  97}
  98
  99static int decorate_callback(const struct option *opt, const char *arg, int unset)
 100{
 101        if (unset)
 102                decoration_style = 0;
 103        else if (arg)
 104                decoration_style = parse_decoration_style(arg);
 105        else
 106                decoration_style = DECORATE_SHORT_REFS;
 107
 108        if (decoration_style < 0)
 109                die(_("invalid --decorate option: %s"), arg);
 110
 111        decoration_given = 1;
 112
 113        return 0;
 114}
 115
 116static int log_line_range_callback(const struct option *option, const char *arg, int unset)
 117{
 118        struct line_opt_callback_data *data = option->value;
 119
 120        BUG_ON_OPT_NEG(unset);
 121
 122        if (!arg)
 123                return -1;
 124
 125        data->rev->line_level_traverse = 1;
 126        string_list_append(&data->args, arg);
 127
 128        return 0;
 129}
 130
 131static void init_log_defaults(void)
 132{
 133        init_grep_defaults(the_repository);
 134        init_diff_ui_defaults();
 135
 136        decoration_style = auto_decoration_style();
 137}
 138
 139static void cmd_log_init_defaults(struct rev_info *rev)
 140{
 141        if (fmt_pretty)
 142                get_commit_format(fmt_pretty, rev);
 143        if (default_follow)
 144                rev->diffopt.flags.default_follow_renames = 1;
 145        rev->verbose_header = 1;
 146        rev->diffopt.flags.recursive = 1;
 147        rev->diffopt.stat_width = -1; /* use full terminal width */
 148        rev->diffopt.stat_graph_width = -1; /* respect statGraphWidth config */
 149        rev->abbrev_commit = default_abbrev_commit;
 150        rev->show_root_diff = default_show_root;
 151        rev->subject_prefix = fmt_patch_subject_prefix;
 152        rev->show_signature = default_show_signature;
 153        rev->diffopt.flags.allow_textconv = 1;
 154
 155        if (default_date_mode)
 156                parse_date_format(default_date_mode, &rev->date_mode);
 157}
 158
 159static char warn_unspecified_mailmap_msg[] =
 160N_("log.mailmap is not set; its implicit value will change in an\n"
 161   "upcoming release. To squelch this message and preserve current\n"
 162   "behaviour, set the log.mailmap configuration value to false.\n"
 163   "\n"
 164   "To squelch this message and adopt the new behaviour now, set the\n"
 165   "log.mailmap configuration value to true.\n"
 166   "\n"
 167   "See 'git help config' and search for 'log.mailmap' for further information.");
 168
 169static void cmd_log_init_finish(int argc, const char **argv, const char *prefix,
 170                         struct rev_info *rev, struct setup_revision_opt *opt)
 171{
 172        struct userformat_want w;
 173        int quiet = 0, source = 0, mailmap = 0;
 174        static struct line_opt_callback_data line_cb = {NULL, NULL, STRING_LIST_INIT_DUP};
 175        static struct string_list decorate_refs_exclude = STRING_LIST_INIT_NODUP;
 176        static struct string_list decorate_refs_include = STRING_LIST_INIT_NODUP;
 177        struct decoration_filter decoration_filter = {&decorate_refs_include,
 178                                                      &decorate_refs_exclude};
 179        static struct revision_sources revision_sources;
 180
 181        const struct option builtin_log_options[] = {
 182                OPT__QUIET(&quiet, N_("suppress diff output")),
 183                OPT_BOOL(0, "source", &source, N_("show source")),
 184                OPT_BOOL(0, "use-mailmap", &mailmap, N_("Use mail map file")),
 185                OPT_STRING_LIST(0, "decorate-refs", &decorate_refs_include,
 186                                N_("pattern"), N_("only decorate refs that match <pattern>")),
 187                OPT_STRING_LIST(0, "decorate-refs-exclude", &decorate_refs_exclude,
 188                                N_("pattern"), N_("do not decorate refs that match <pattern>")),
 189                { OPTION_CALLBACK, 0, "decorate", NULL, NULL, N_("decorate options"),
 190                  PARSE_OPT_OPTARG, decorate_callback},
 191                OPT_CALLBACK('L', NULL, &line_cb, "n,m:file",
 192                             N_("Process line range n,m in file, counting from 1"),
 193                             log_line_range_callback),
 194                OPT_END()
 195        };
 196
 197        line_cb.rev = rev;
 198        line_cb.prefix = prefix;
 199
 200        mailmap = use_mailmap_config;
 201        argc = parse_options(argc, argv, prefix,
 202                             builtin_log_options, builtin_log_usage,
 203                             PARSE_OPT_KEEP_ARGV0 | PARSE_OPT_KEEP_UNKNOWN |
 204                             PARSE_OPT_KEEP_DASHDASH);
 205
 206        if (quiet)
 207                rev->diffopt.output_format |= DIFF_FORMAT_NO_OUTPUT;
 208        argc = setup_revisions(argc, argv, rev, opt);
 209
 210        /* Any arguments at this point are not recognized */
 211        if (argc > 1)
 212                die(_("unrecognized argument: %s"), argv[1]);
 213
 214        memset(&w, 0, sizeof(w));
 215        userformat_find_requirements(NULL, &w);
 216
 217        if (mailmap < 0) {
 218                if (session_is_interactive() && !rev->pretty_given)
 219                        warning("%s\n", _(warn_unspecified_mailmap_msg));
 220
 221                mailmap = 0;
 222        }
 223
 224        if (!rev->show_notes_given && (!rev->pretty_given || w.notes))
 225                rev->show_notes = 1;
 226        if (rev->show_notes)
 227                init_display_notes(&rev->notes_opt);
 228
 229        if ((rev->diffopt.pickaxe_opts & DIFF_PICKAXE_KINDS_MASK) ||
 230            rev->diffopt.filter || rev->diffopt.flags.follow_renames)
 231                rev->always_show_header = 0;
 232
 233        if (source || w.source) {
 234                init_revision_sources(&revision_sources);
 235                rev->sources = &revision_sources;
 236        }
 237
 238        if (mailmap) {
 239                rev->mailmap = xcalloc(1, sizeof(struct string_list));
 240                read_mailmap(rev->mailmap, NULL);
 241        }
 242
 243        if (rev->pretty_given && rev->commit_format == CMIT_FMT_RAW) {
 244                /*
 245                 * "log --pretty=raw" is special; ignore UI oriented
 246                 * configuration variables such as decoration.
 247                 */
 248                if (!decoration_given)
 249                        decoration_style = 0;
 250                if (!rev->abbrev_commit_given)
 251                        rev->abbrev_commit = 0;
 252        }
 253
 254        if (decoration_style) {
 255                rev->show_decorations = 1;
 256                load_ref_decorations(&decoration_filter, decoration_style);
 257        }
 258
 259        if (rev->line_level_traverse)
 260                line_log_init(rev, line_cb.prefix, &line_cb.args);
 261
 262        setup_pager();
 263}
 264
 265static void cmd_log_init(int argc, const char **argv, const char *prefix,
 266                         struct rev_info *rev, struct setup_revision_opt *opt)
 267{
 268        cmd_log_init_defaults(rev);
 269        cmd_log_init_finish(argc, argv, prefix, rev, opt);
 270}
 271
 272/*
 273 * This gives a rough estimate for how many commits we
 274 * will print out in the list.
 275 */
 276static int estimate_commit_count(struct commit_list *list)
 277{
 278        int n = 0;
 279
 280        while (list) {
 281                struct commit *commit = list->item;
 282                unsigned int flags = commit->object.flags;
 283                list = list->next;
 284                if (!(flags & (TREESAME | UNINTERESTING)))
 285                        n++;
 286        }
 287        return n;
 288}
 289
 290static void show_early_header(struct rev_info *rev, const char *stage, int nr)
 291{
 292        if (rev->shown_one) {
 293                rev->shown_one = 0;
 294                if (rev->commit_format != CMIT_FMT_ONELINE)
 295                        putchar(rev->diffopt.line_termination);
 296        }
 297        fprintf(rev->diffopt.file, _("Final output: %d %s\n"), nr, stage);
 298}
 299
 300static struct itimerval early_output_timer;
 301
 302static void log_show_early(struct rev_info *revs, struct commit_list *list)
 303{
 304        int i = revs->early_output, close_file = revs->diffopt.close_file;
 305        int show_header = 1;
 306
 307        revs->diffopt.close_file = 0;
 308        sort_in_topological_order(&list, revs->sort_order);
 309        while (list && i) {
 310                struct commit *commit = list->item;
 311                switch (simplify_commit(revs, commit)) {
 312                case commit_show:
 313                        if (show_header) {
 314                                int n = estimate_commit_count(list);
 315                                show_early_header(revs, "incomplete", n);
 316                                show_header = 0;
 317                        }
 318                        log_tree_commit(revs, commit);
 319                        i--;
 320                        break;
 321                case commit_ignore:
 322                        break;
 323                case commit_error:
 324                        if (close_file)
 325                                fclose(revs->diffopt.file);
 326                        return;
 327                }
 328                list = list->next;
 329        }
 330
 331        /* Did we already get enough commits for the early output? */
 332        if (!i) {
 333                if (close_file)
 334                        fclose(revs->diffopt.file);
 335                return;
 336        }
 337
 338        /*
 339         * ..if no, then repeat it twice a second until we
 340         * do.
 341         *
 342         * NOTE! We don't use "it_interval", because if the
 343         * reader isn't listening, we want our output to be
 344         * throttled by the writing, and not have the timer
 345         * trigger every second even if we're blocked on a
 346         * reader!
 347         */
 348        early_output_timer.it_value.tv_sec = 0;
 349        early_output_timer.it_value.tv_usec = 500000;
 350        setitimer(ITIMER_REAL, &early_output_timer, NULL);
 351}
 352
 353static void early_output(int signal)
 354{
 355        show_early_output = log_show_early;
 356}
 357
 358static void setup_early_output(void)
 359{
 360        struct sigaction sa;
 361
 362        /*
 363         * Set up the signal handler, minimally intrusively:
 364         * we only set a single volatile integer word (not
 365         * using sigatomic_t - trying to avoid unnecessary
 366         * system dependencies and headers), and using
 367         * SA_RESTART.
 368         */
 369        memset(&sa, 0, sizeof(sa));
 370        sa.sa_handler = early_output;
 371        sigemptyset(&sa.sa_mask);
 372        sa.sa_flags = SA_RESTART;
 373        sigaction(SIGALRM, &sa, NULL);
 374
 375        /*
 376         * If we can get the whole output in less than a
 377         * tenth of a second, don't even bother doing the
 378         * early-output thing..
 379         *
 380         * This is a one-time-only trigger.
 381         */
 382        early_output_timer.it_value.tv_sec = 0;
 383        early_output_timer.it_value.tv_usec = 100000;
 384        setitimer(ITIMER_REAL, &early_output_timer, NULL);
 385}
 386
 387static void finish_early_output(struct rev_info *rev)
 388{
 389        int n = estimate_commit_count(rev->commits);
 390        signal(SIGALRM, SIG_IGN);
 391        show_early_header(rev, "done", n);
 392}
 393
 394static int cmd_log_walk(struct rev_info *rev)
 395{
 396        struct commit *commit;
 397        int saved_nrl = 0;
 398        int saved_dcctc = 0, close_file = rev->diffopt.close_file;
 399
 400        if (rev->early_output)
 401                setup_early_output();
 402
 403        if (prepare_revision_walk(rev))
 404                die(_("revision walk setup failed"));
 405
 406        if (rev->early_output)
 407                finish_early_output(rev);
 408
 409        /*
 410         * For --check and --exit-code, the exit code is based on CHECK_FAILED
 411         * and HAS_CHANGES being accumulated in rev->diffopt, so be careful to
 412         * retain that state information if replacing rev->diffopt in this loop
 413         */
 414        rev->diffopt.close_file = 0;
 415        while ((commit = get_revision(rev)) != NULL) {
 416                if (!log_tree_commit(rev, commit) && rev->max_count >= 0)
 417                        /*
 418                         * We decremented max_count in get_revision,
 419                         * but we didn't actually show the commit.
 420                         */
 421                        rev->max_count++;
 422                if (!rev->reflog_info) {
 423                        /*
 424                         * We may show a given commit multiple times when
 425                         * walking the reflogs.
 426                         */
 427                        free_commit_buffer(the_repository->parsed_objects,
 428                                           commit);
 429                        free_commit_list(commit->parents);
 430                        commit->parents = NULL;
 431                }
 432                if (saved_nrl < rev->diffopt.needed_rename_limit)
 433                        saved_nrl = rev->diffopt.needed_rename_limit;
 434                if (rev->diffopt.degraded_cc_to_c)
 435                        saved_dcctc = 1;
 436        }
 437        rev->diffopt.degraded_cc_to_c = saved_dcctc;
 438        rev->diffopt.needed_rename_limit = saved_nrl;
 439        if (close_file)
 440                fclose(rev->diffopt.file);
 441
 442        if (rev->diffopt.output_format & DIFF_FORMAT_CHECKDIFF &&
 443            rev->diffopt.flags.check_failed) {
 444                return 02;
 445        }
 446        return diff_result_code(&rev->diffopt, 0);
 447}
 448
 449static int git_log_config(const char *var, const char *value, void *cb)
 450{
 451        const char *slot_name;
 452
 453        if (!strcmp(var, "format.pretty"))
 454                return git_config_string(&fmt_pretty, var, value);
 455        if (!strcmp(var, "format.subjectprefix"))
 456                return git_config_string(&fmt_patch_subject_prefix, var, value);
 457        if (!strcmp(var, "log.abbrevcommit")) {
 458                default_abbrev_commit = git_config_bool(var, value);
 459                return 0;
 460        }
 461        if (!strcmp(var, "log.date"))
 462                return git_config_string(&default_date_mode, var, value);
 463        if (!strcmp(var, "log.decorate")) {
 464                decoration_style = parse_decoration_style(value);
 465                if (decoration_style < 0)
 466                        decoration_style = 0; /* maybe warn? */
 467                return 0;
 468        }
 469        if (!strcmp(var, "log.showroot")) {
 470                default_show_root = git_config_bool(var, value);
 471                return 0;
 472        }
 473        if (!strcmp(var, "log.follow")) {
 474                default_follow = git_config_bool(var, value);
 475                return 0;
 476        }
 477        if (skip_prefix(var, "color.decorate.", &slot_name))
 478                return parse_decorate_color_config(var, slot_name, value);
 479        if (!strcmp(var, "log.mailmap")) {
 480                use_mailmap_config = git_config_bool(var, value);
 481                return 0;
 482        }
 483        if (!strcmp(var, "log.showsignature")) {
 484                default_show_signature = git_config_bool(var, value);
 485                return 0;
 486        }
 487
 488        if (grep_config(var, value, cb) < 0)
 489                return -1;
 490        if (git_gpg_config(var, value, cb) < 0)
 491                return -1;
 492        return git_diff_ui_config(var, value, cb);
 493}
 494
 495int cmd_whatchanged(int argc, const char **argv, const char *prefix)
 496{
 497        struct rev_info rev;
 498        struct setup_revision_opt opt;
 499
 500        init_log_defaults();
 501        git_config(git_log_config, NULL);
 502
 503        repo_init_revisions(the_repository, &rev, prefix);
 504        rev.diff = 1;
 505        rev.simplify_history = 0;
 506        memset(&opt, 0, sizeof(opt));
 507        opt.def = "HEAD";
 508        opt.revarg_opt = REVARG_COMMITTISH;
 509        cmd_log_init(argc, argv, prefix, &rev, &opt);
 510        if (!rev.diffopt.output_format)
 511                rev.diffopt.output_format = DIFF_FORMAT_RAW;
 512        return cmd_log_walk(&rev);
 513}
 514
 515static void show_tagger(const char *buf, struct rev_info *rev)
 516{
 517        struct strbuf out = STRBUF_INIT;
 518        struct pretty_print_context pp = {0};
 519
 520        pp.fmt = rev->commit_format;
 521        pp.date_mode = rev->date_mode;
 522        pp_user_info(&pp, "Tagger", &out, buf, get_log_output_encoding());
 523        fprintf(rev->diffopt.file, "%s", out.buf);
 524        strbuf_release(&out);
 525}
 526
 527static int show_blob_object(const struct object_id *oid, struct rev_info *rev, const char *obj_name)
 528{
 529        struct object_id oidc;
 530        struct object_context obj_context;
 531        char *buf;
 532        unsigned long size;
 533
 534        fflush(rev->diffopt.file);
 535        if (!rev->diffopt.flags.textconv_set_via_cmdline ||
 536            !rev->diffopt.flags.allow_textconv)
 537                return stream_blob_to_fd(1, oid, NULL, 0);
 538
 539        if (get_oid_with_context(the_repository, obj_name,
 540                                 GET_OID_RECORD_PATH,
 541                                 &oidc, &obj_context))
 542                die(_("not a valid object name %s"), obj_name);
 543        if (!obj_context.path ||
 544            !textconv_object(the_repository, obj_context.path,
 545                             obj_context.mode, &oidc, 1, &buf, &size)) {
 546                free(obj_context.path);
 547                return stream_blob_to_fd(1, oid, NULL, 0);
 548        }
 549
 550        if (!buf)
 551                die(_("git show %s: bad file"), obj_name);
 552
 553        write_or_die(1, buf, size);
 554        free(obj_context.path);
 555        return 0;
 556}
 557
 558static int show_tag_object(const struct object_id *oid, struct rev_info *rev)
 559{
 560        unsigned long size;
 561        enum object_type type;
 562        char *buf = read_object_file(oid, &type, &size);
 563        int offset = 0;
 564
 565        if (!buf)
 566                return error(_("could not read object %s"), oid_to_hex(oid));
 567
 568        assert(type == OBJ_TAG);
 569        while (offset < size && buf[offset] != '\n') {
 570                int new_offset = offset + 1;
 571                const char *ident;
 572                while (new_offset < size && buf[new_offset++] != '\n')
 573                        ; /* do nothing */
 574                if (skip_prefix(buf + offset, "tagger ", &ident))
 575                        show_tagger(ident, rev);
 576                offset = new_offset;
 577        }
 578
 579        if (offset < size)
 580                fwrite(buf + offset, size - offset, 1, rev->diffopt.file);
 581        free(buf);
 582        return 0;
 583}
 584
 585static int show_tree_object(const struct object_id *oid,
 586                struct strbuf *base,
 587                const char *pathname, unsigned mode, int stage, void *context)
 588{
 589        FILE *file = context;
 590        fprintf(file, "%s%s\n", pathname, S_ISDIR(mode) ? "/" : "");
 591        return 0;
 592}
 593
 594static void show_setup_revisions_tweak(struct rev_info *rev,
 595                                       struct setup_revision_opt *opt)
 596{
 597        if (rev->ignore_merges) {
 598                /* There was no "-m" on the command line */
 599                rev->ignore_merges = 0;
 600                if (!rev->first_parent_only && !rev->combine_merges) {
 601                        /* No "--first-parent", "-c", or "--cc" */
 602                        rev->combine_merges = 1;
 603                        rev->dense_combined_merges = 1;
 604                }
 605        }
 606        if (!rev->diffopt.output_format)
 607                rev->diffopt.output_format = DIFF_FORMAT_PATCH;
 608}
 609
 610int cmd_show(int argc, const char **argv, const char *prefix)
 611{
 612        struct rev_info rev;
 613        struct object_array_entry *objects;
 614        struct setup_revision_opt opt;
 615        struct pathspec match_all;
 616        int i, count, ret = 0;
 617
 618        init_log_defaults();
 619        git_config(git_log_config, NULL);
 620
 621        memset(&match_all, 0, sizeof(match_all));
 622        repo_init_revisions(the_repository, &rev, prefix);
 623        rev.diff = 1;
 624        rev.always_show_header = 1;
 625        rev.no_walk = REVISION_WALK_NO_WALK_SORTED;
 626        rev.diffopt.stat_width = -1;    /* Scale to real terminal size */
 627
 628        memset(&opt, 0, sizeof(opt));
 629        opt.def = "HEAD";
 630        opt.tweak = show_setup_revisions_tweak;
 631        cmd_log_init(argc, argv, prefix, &rev, &opt);
 632
 633        if (!rev.no_walk)
 634                return cmd_log_walk(&rev);
 635
 636        count = rev.pending.nr;
 637        objects = rev.pending.objects;
 638        for (i = 0; i < count && !ret; i++) {
 639                struct object *o = objects[i].item;
 640                const char *name = objects[i].name;
 641                switch (o->type) {
 642                case OBJ_BLOB:
 643                        ret = show_blob_object(&o->oid, &rev, name);
 644                        break;
 645                case OBJ_TAG: {
 646                        struct tag *t = (struct tag *)o;
 647
 648                        if (rev.shown_one)
 649                                putchar('\n');
 650                        fprintf(rev.diffopt.file, "%stag %s%s\n",
 651                                        diff_get_color_opt(&rev.diffopt, DIFF_COMMIT),
 652                                        t->tag,
 653                                        diff_get_color_opt(&rev.diffopt, DIFF_RESET));
 654                        ret = show_tag_object(&o->oid, &rev);
 655                        rev.shown_one = 1;
 656                        if (ret)
 657                                break;
 658                        o = parse_object(the_repository, &t->tagged->oid);
 659                        if (!o)
 660                                ret = error(_("could not read object %s"),
 661                                            oid_to_hex(&t->tagged->oid));
 662                        objects[i].item = o;
 663                        i--;
 664                        break;
 665                }
 666                case OBJ_TREE:
 667                        if (rev.shown_one)
 668                                putchar('\n');
 669                        fprintf(rev.diffopt.file, "%stree %s%s\n\n",
 670                                        diff_get_color_opt(&rev.diffopt, DIFF_COMMIT),
 671                                        name,
 672                                        diff_get_color_opt(&rev.diffopt, DIFF_RESET));
 673                        read_tree_recursive(the_repository, (struct tree *)o, "",
 674                                            0, 0, &match_all, show_tree_object,
 675                                            rev.diffopt.file);
 676                        rev.shown_one = 1;
 677                        break;
 678                case OBJ_COMMIT:
 679                        rev.pending.nr = rev.pending.alloc = 0;
 680                        rev.pending.objects = NULL;
 681                        add_object_array(o, name, &rev.pending);
 682                        ret = cmd_log_walk(&rev);
 683                        break;
 684                default:
 685                        ret = error(_("unknown type: %d"), o->type);
 686                }
 687        }
 688        free(objects);
 689        return ret;
 690}
 691
 692/*
 693 * This is equivalent to "git log -g --abbrev-commit --pretty=oneline"
 694 */
 695int cmd_log_reflog(int argc, const char **argv, const char *prefix)
 696{
 697        struct rev_info rev;
 698        struct setup_revision_opt opt;
 699
 700        init_log_defaults();
 701        git_config(git_log_config, NULL);
 702
 703        repo_init_revisions(the_repository, &rev, prefix);
 704        init_reflog_walk(&rev.reflog_info);
 705        rev.verbose_header = 1;
 706        memset(&opt, 0, sizeof(opt));
 707        opt.def = "HEAD";
 708        cmd_log_init_defaults(&rev);
 709        rev.abbrev_commit = 1;
 710        rev.commit_format = CMIT_FMT_ONELINE;
 711        rev.use_terminator = 1;
 712        rev.always_show_header = 1;
 713        cmd_log_init_finish(argc, argv, prefix, &rev, &opt);
 714
 715        return cmd_log_walk(&rev);
 716}
 717
 718static void log_setup_revisions_tweak(struct rev_info *rev,
 719                                      struct setup_revision_opt *opt)
 720{
 721        if (rev->diffopt.flags.default_follow_renames &&
 722            rev->prune_data.nr == 1)
 723                rev->diffopt.flags.follow_renames = 1;
 724
 725        /* Turn --cc/-c into -p --cc/-c when -p was not given */
 726        if (!rev->diffopt.output_format && rev->combine_merges)
 727                rev->diffopt.output_format = DIFF_FORMAT_PATCH;
 728
 729        /* Turn -m on when --cc/-c was given */
 730        if (rev->combine_merges)
 731                rev->ignore_merges = 0;
 732}
 733
 734int cmd_log(int argc, const char **argv, const char *prefix)
 735{
 736        struct rev_info rev;
 737        struct setup_revision_opt opt;
 738
 739        init_log_defaults();
 740        git_config(git_log_config, NULL);
 741
 742        repo_init_revisions(the_repository, &rev, prefix);
 743        rev.always_show_header = 1;
 744        memset(&opt, 0, sizeof(opt));
 745        opt.def = "HEAD";
 746        opt.revarg_opt = REVARG_COMMITTISH;
 747        opt.tweak = log_setup_revisions_tweak;
 748        cmd_log_init(argc, argv, prefix, &rev, &opt);
 749        return cmd_log_walk(&rev);
 750}
 751
 752/* format-patch */
 753
 754static const char *fmt_patch_suffix = ".patch";
 755static int numbered = 0;
 756static int auto_number = 1;
 757
 758static char *default_attach = NULL;
 759
 760static struct string_list extra_hdr = STRING_LIST_INIT_NODUP;
 761static struct string_list extra_to = STRING_LIST_INIT_NODUP;
 762static struct string_list extra_cc = STRING_LIST_INIT_NODUP;
 763
 764static void add_header(const char *value)
 765{
 766        struct string_list_item *item;
 767        int len = strlen(value);
 768        while (len && value[len - 1] == '\n')
 769                len--;
 770
 771        if (!strncasecmp(value, "to: ", 4)) {
 772                item = string_list_append(&extra_to, value + 4);
 773                len -= 4;
 774        } else if (!strncasecmp(value, "cc: ", 4)) {
 775                item = string_list_append(&extra_cc, value + 4);
 776                len -= 4;
 777        } else {
 778                item = string_list_append(&extra_hdr, value);
 779        }
 780
 781        item->string[len] = '\0';
 782}
 783
 784#define THREAD_SHALLOW 1
 785#define THREAD_DEEP 2
 786static int thread;
 787static int do_signoff;
 788static int base_auto;
 789static char *from;
 790static const char *signature = git_version_string;
 791static const char *signature_file;
 792static int config_cover_letter;
 793static const char *config_output_directory;
 794
 795enum {
 796        COVER_UNSET,
 797        COVER_OFF,
 798        COVER_ON,
 799        COVER_AUTO
 800};
 801
 802static int git_format_config(const char *var, const char *value, void *cb)
 803{
 804        struct rev_info *rev = cb;
 805
 806        if (!strcmp(var, "format.headers")) {
 807                if (!value)
 808                        die(_("format.headers without value"));
 809                add_header(value);
 810                return 0;
 811        }
 812        if (!strcmp(var, "format.suffix"))
 813                return git_config_string(&fmt_patch_suffix, var, value);
 814        if (!strcmp(var, "format.to")) {
 815                if (!value)
 816                        return config_error_nonbool(var);
 817                string_list_append(&extra_to, value);
 818                return 0;
 819        }
 820        if (!strcmp(var, "format.cc")) {
 821                if (!value)
 822                        return config_error_nonbool(var);
 823                string_list_append(&extra_cc, value);
 824                return 0;
 825        }
 826        if (!strcmp(var, "diff.color") || !strcmp(var, "color.diff") ||
 827            !strcmp(var, "color.ui") || !strcmp(var, "diff.submodule")) {
 828                return 0;
 829        }
 830        if (!strcmp(var, "format.numbered")) {
 831                if (value && !strcasecmp(value, "auto")) {
 832                        auto_number = 1;
 833                        return 0;
 834                }
 835                numbered = git_config_bool(var, value);
 836                auto_number = auto_number && numbered;
 837                return 0;
 838        }
 839        if (!strcmp(var, "format.attach")) {
 840                if (value && *value)
 841                        default_attach = xstrdup(value);
 842                else
 843                        default_attach = xstrdup(git_version_string);
 844                return 0;
 845        }
 846        if (!strcmp(var, "format.thread")) {
 847                if (value && !strcasecmp(value, "deep")) {
 848                        thread = THREAD_DEEP;
 849                        return 0;
 850                }
 851                if (value && !strcasecmp(value, "shallow")) {
 852                        thread = THREAD_SHALLOW;
 853                        return 0;
 854                }
 855                thread = git_config_bool(var, value) && THREAD_SHALLOW;
 856                return 0;
 857        }
 858        if (!strcmp(var, "format.signoff")) {
 859                do_signoff = git_config_bool(var, value);
 860                return 0;
 861        }
 862        if (!strcmp(var, "format.signature"))
 863                return git_config_string(&signature, var, value);
 864        if (!strcmp(var, "format.signaturefile"))
 865                return git_config_pathname(&signature_file, var, value);
 866        if (!strcmp(var, "format.coverletter")) {
 867                if (value && !strcasecmp(value, "auto")) {
 868                        config_cover_letter = COVER_AUTO;
 869                        return 0;
 870                }
 871                config_cover_letter = git_config_bool(var, value) ? COVER_ON : COVER_OFF;
 872                return 0;
 873        }
 874        if (!strcmp(var, "format.outputdirectory"))
 875                return git_config_string(&config_output_directory, var, value);
 876        if (!strcmp(var, "format.useautobase")) {
 877                base_auto = git_config_bool(var, value);
 878                return 0;
 879        }
 880        if (!strcmp(var, "format.from")) {
 881                int b = git_parse_maybe_bool(value);
 882                free(from);
 883                if (b < 0)
 884                        from = xstrdup(value);
 885                else if (b)
 886                        from = xstrdup(git_committer_info(IDENT_NO_DATE));
 887                else
 888                        from = NULL;
 889                return 0;
 890        }
 891        if (!strcmp(var, "format.notes")) {
 892                struct strbuf buf = STRBUF_INIT;
 893                int b = git_parse_maybe_bool(value);
 894                if (!b)
 895                        return 0;
 896                rev->show_notes = 1;
 897                if (b < 0) {
 898                        strbuf_addstr(&buf, value);
 899                        expand_notes_ref(&buf);
 900                        string_list_append(&rev->notes_opt.extra_notes_refs,
 901                                        strbuf_detach(&buf, NULL));
 902                } else {
 903                        rev->notes_opt.use_default_notes = 1;
 904                }
 905                return 0;
 906        }
 907
 908        return git_log_config(var, value, cb);
 909}
 910
 911static const char *output_directory = NULL;
 912static int outdir_offset;
 913
 914static int open_next_file(struct commit *commit, const char *subject,
 915                         struct rev_info *rev, int quiet)
 916{
 917        struct strbuf filename = STRBUF_INIT;
 918        int suffix_len = strlen(rev->patch_suffix) + 1;
 919
 920        if (output_directory) {
 921                strbuf_addstr(&filename, output_directory);
 922                if (filename.len >=
 923                    PATH_MAX - FORMAT_PATCH_NAME_MAX - suffix_len) {
 924                        strbuf_release(&filename);
 925                        return error(_("name of output directory is too long"));
 926                }
 927                strbuf_complete(&filename, '/');
 928        }
 929
 930        if (rev->numbered_files)
 931                strbuf_addf(&filename, "%d", rev->nr);
 932        else if (commit)
 933                fmt_output_commit(&filename, commit, rev);
 934        else
 935                fmt_output_subject(&filename, subject, rev);
 936
 937        if (!quiet)
 938                printf("%s\n", filename.buf + outdir_offset);
 939
 940        if ((rev->diffopt.file = fopen(filename.buf, "w")) == NULL) {
 941                error_errno(_("cannot open patch file %s"), filename.buf);
 942                strbuf_release(&filename);
 943                return -1;
 944        }
 945
 946        strbuf_release(&filename);
 947        return 0;
 948}
 949
 950static void get_patch_ids(struct rev_info *rev, struct patch_ids *ids)
 951{
 952        struct rev_info check_rev;
 953        struct commit *commit, *c1, *c2;
 954        struct object *o1, *o2;
 955        unsigned flags1, flags2;
 956
 957        if (rev->pending.nr != 2)
 958                die(_("need exactly one range"));
 959
 960        o1 = rev->pending.objects[0].item;
 961        o2 = rev->pending.objects[1].item;
 962        flags1 = o1->flags;
 963        flags2 = o2->flags;
 964        c1 = lookup_commit_reference(the_repository, &o1->oid);
 965        c2 = lookup_commit_reference(the_repository, &o2->oid);
 966
 967        if ((flags1 & UNINTERESTING) == (flags2 & UNINTERESTING))
 968                die(_("not a range"));
 969
 970        init_patch_ids(the_repository, ids);
 971
 972        /* given a range a..b get all patch ids for b..a */
 973        repo_init_revisions(the_repository, &check_rev, rev->prefix);
 974        check_rev.max_parents = 1;
 975        o1->flags ^= UNINTERESTING;
 976        o2->flags ^= UNINTERESTING;
 977        add_pending_object(&check_rev, o1, "o1");
 978        add_pending_object(&check_rev, o2, "o2");
 979        if (prepare_revision_walk(&check_rev))
 980                die(_("revision walk setup failed"));
 981
 982        while ((commit = get_revision(&check_rev)) != NULL) {
 983                add_commit_patch_id(commit, ids);
 984        }
 985
 986        /* reset for next revision walk */
 987        clear_commit_marks(c1, SEEN | UNINTERESTING | SHOWN | ADDED);
 988        clear_commit_marks(c2, SEEN | UNINTERESTING | SHOWN | ADDED);
 989        o1->flags = flags1;
 990        o2->flags = flags2;
 991}
 992
 993static void gen_message_id(struct rev_info *info, char *base)
 994{
 995        struct strbuf buf = STRBUF_INIT;
 996        strbuf_addf(&buf, "%s.%"PRItime".git.%s", base,
 997                    (timestamp_t) time(NULL),
 998                    git_committer_info(IDENT_NO_NAME|IDENT_NO_DATE|IDENT_STRICT));
 999        info->message_id = strbuf_detach(&buf, NULL);
1000}
1001
1002static void print_signature(FILE *file)
1003{
1004        if (!signature || !*signature)
1005                return;
1006
1007        fprintf(file, "-- \n%s", signature);
1008        if (signature[strlen(signature)-1] != '\n')
1009                putc('\n', file);
1010        putc('\n', file);
1011}
1012
1013static void add_branch_description(struct strbuf *buf, const char *branch_name)
1014{
1015        struct strbuf desc = STRBUF_INIT;
1016        if (!branch_name || !*branch_name)
1017                return;
1018        read_branch_desc(&desc, branch_name);
1019        if (desc.len) {
1020                strbuf_addch(buf, '\n');
1021                strbuf_addbuf(buf, &desc);
1022                strbuf_addch(buf, '\n');
1023        }
1024        strbuf_release(&desc);
1025}
1026
1027static char *find_branch_name(struct rev_info *rev)
1028{
1029        int i, positive = -1;
1030        struct object_id branch_oid;
1031        const struct object_id *tip_oid;
1032        const char *ref, *v;
1033        char *full_ref, *branch = NULL;
1034
1035        for (i = 0; i < rev->cmdline.nr; i++) {
1036                if (rev->cmdline.rev[i].flags & UNINTERESTING)
1037                        continue;
1038                if (positive < 0)
1039                        positive = i;
1040                else
1041                        return NULL;
1042        }
1043        if (positive < 0)
1044                return NULL;
1045        ref = rev->cmdline.rev[positive].name;
1046        tip_oid = &rev->cmdline.rev[positive].item->oid;
1047        if (dwim_ref(ref, strlen(ref), &branch_oid, &full_ref) &&
1048            skip_prefix(full_ref, "refs/heads/", &v) &&
1049            oideq(tip_oid, &branch_oid))
1050                branch = xstrdup(v);
1051        free(full_ref);
1052        return branch;
1053}
1054
1055static void show_diffstat(struct rev_info *rev,
1056                          struct commit *origin, struct commit *head)
1057{
1058        struct diff_options opts;
1059
1060        memcpy(&opts, &rev->diffopt, sizeof(opts));
1061        opts.output_format = DIFF_FORMAT_SUMMARY | DIFF_FORMAT_DIFFSTAT;
1062        diff_setup_done(&opts);
1063
1064        diff_tree_oid(get_commit_tree_oid(origin),
1065                      get_commit_tree_oid(head),
1066                      "", &opts);
1067        diffcore_std(&opts);
1068        diff_flush(&opts);
1069
1070        fprintf(rev->diffopt.file, "\n");
1071}
1072
1073static void make_cover_letter(struct rev_info *rev, int use_stdout,
1074                              struct commit *origin,
1075                              int nr, struct commit **list,
1076                              const char *branch_name,
1077                              int quiet)
1078{
1079        const char *committer;
1080        const char *body = "*** SUBJECT HERE ***\n\n*** BLURB HERE ***\n";
1081        const char *msg;
1082        struct shortlog log;
1083        struct strbuf sb = STRBUF_INIT;
1084        int i;
1085        const char *encoding = "UTF-8";
1086        int need_8bit_cte = 0;
1087        struct pretty_print_context pp = {0};
1088        struct commit *head = list[0];
1089
1090        if (!cmit_fmt_is_mail(rev->commit_format))
1091                die(_("cover letter needs email format"));
1092
1093        committer = git_committer_info(0);
1094
1095        if (!use_stdout &&
1096            open_next_file(NULL, rev->numbered_files ? NULL : "cover-letter", rev, quiet))
1097                die(_("failed to create cover-letter file"));
1098
1099        log_write_email_headers(rev, head, &pp.after_subject, &need_8bit_cte, 0);
1100
1101        for (i = 0; !need_8bit_cte && i < nr; i++) {
1102                const char *buf = get_commit_buffer(list[i], NULL);
1103                if (has_non_ascii(buf))
1104                        need_8bit_cte = 1;
1105                unuse_commit_buffer(list[i], buf);
1106        }
1107
1108        if (!branch_name)
1109                branch_name = find_branch_name(rev);
1110
1111        msg = body;
1112        pp.fmt = CMIT_FMT_EMAIL;
1113        pp.date_mode.type = DATE_RFC2822;
1114        pp.rev = rev;
1115        pp.print_email_subject = 1;
1116        pp_user_info(&pp, NULL, &sb, committer, encoding);
1117        pp_title_line(&pp, &msg, &sb, encoding, need_8bit_cte);
1118        pp_remainder(&pp, &msg, &sb, 0);
1119        add_branch_description(&sb, branch_name);
1120        fprintf(rev->diffopt.file, "%s\n", sb.buf);
1121
1122        strbuf_release(&sb);
1123
1124        shortlog_init(&log);
1125        log.wrap_lines = 1;
1126        log.wrap = MAIL_DEFAULT_WRAP;
1127        log.in1 = 2;
1128        log.in2 = 4;
1129        log.file = rev->diffopt.file;
1130        for (i = 0; i < nr; i++)
1131                shortlog_add_commit(&log, list[i]);
1132
1133        shortlog_output(&log);
1134
1135        /* We can only do diffstat with a unique reference point */
1136        if (origin)
1137                show_diffstat(rev, origin, head);
1138
1139        if (rev->idiff_oid1) {
1140                fprintf_ln(rev->diffopt.file, "%s", rev->idiff_title);
1141                show_interdiff(rev, 0);
1142        }
1143
1144        if (rev->rdiff1) {
1145                /*
1146                 * Pass minimum required diff-options to range-diff; others
1147                 * can be added later if deemed desirable.
1148                 */
1149                struct diff_options opts;
1150                diff_setup(&opts);
1151                opts.file = rev->diffopt.file;
1152                opts.use_color = rev->diffopt.use_color;
1153                diff_setup_done(&opts);
1154                fprintf_ln(rev->diffopt.file, "%s", rev->rdiff_title);
1155                show_range_diff(rev->rdiff1, rev->rdiff2,
1156                                rev->creation_factor, 1, &opts);
1157        }
1158}
1159
1160static const char *clean_message_id(const char *msg_id)
1161{
1162        char ch;
1163        const char *a, *z, *m;
1164
1165        m = msg_id;
1166        while ((ch = *m) && (isspace(ch) || (ch == '<')))
1167                m++;
1168        a = m;
1169        z = NULL;
1170        while ((ch = *m)) {
1171                if (!isspace(ch) && (ch != '>'))
1172                        z = m;
1173                m++;
1174        }
1175        if (!z)
1176                die(_("insane in-reply-to: %s"), msg_id);
1177        if (++z == m)
1178                return a;
1179        return xmemdupz(a, z - a);
1180}
1181
1182static const char *set_outdir(const char *prefix, const char *output_directory)
1183{
1184        if (output_directory && is_absolute_path(output_directory))
1185                return output_directory;
1186
1187        if (!prefix || !*prefix) {
1188                if (output_directory)
1189                        return output_directory;
1190                /* The user did not explicitly ask for "./" */
1191                outdir_offset = 2;
1192                return "./";
1193        }
1194
1195        outdir_offset = strlen(prefix);
1196        if (!output_directory)
1197                return prefix;
1198
1199        return prefix_filename(prefix, output_directory);
1200}
1201
1202static const char * const builtin_format_patch_usage[] = {
1203        N_("git format-patch [<options>] [<since> | <revision-range>]"),
1204        NULL
1205};
1206
1207static int keep_subject = 0;
1208
1209static int keep_callback(const struct option *opt, const char *arg, int unset)
1210{
1211        BUG_ON_OPT_NEG(unset);
1212        BUG_ON_OPT_ARG(arg);
1213        ((struct rev_info *)opt->value)->total = -1;
1214        keep_subject = 1;
1215        return 0;
1216}
1217
1218static int subject_prefix = 0;
1219
1220static int subject_prefix_callback(const struct option *opt, const char *arg,
1221                            int unset)
1222{
1223        BUG_ON_OPT_NEG(unset);
1224        subject_prefix = 1;
1225        ((struct rev_info *)opt->value)->subject_prefix = arg;
1226        return 0;
1227}
1228
1229static int rfc_callback(const struct option *opt, const char *arg, int unset)
1230{
1231        BUG_ON_OPT_NEG(unset);
1232        BUG_ON_OPT_ARG(arg);
1233        return subject_prefix_callback(opt, "RFC PATCH", unset);
1234}
1235
1236static int numbered_cmdline_opt = 0;
1237
1238static int numbered_callback(const struct option *opt, const char *arg,
1239                             int unset)
1240{
1241        BUG_ON_OPT_ARG(arg);
1242        *(int *)opt->value = numbered_cmdline_opt = unset ? 0 : 1;
1243        if (unset)
1244                auto_number =  0;
1245        return 0;
1246}
1247
1248static int no_numbered_callback(const struct option *opt, const char *arg,
1249                                int unset)
1250{
1251        BUG_ON_OPT_NEG(unset);
1252        return numbered_callback(opt, arg, 1);
1253}
1254
1255static int output_directory_callback(const struct option *opt, const char *arg,
1256                              int unset)
1257{
1258        const char **dir = (const char **)opt->value;
1259        BUG_ON_OPT_NEG(unset);
1260        if (*dir)
1261                die(_("two output directories?"));
1262        *dir = arg;
1263        return 0;
1264}
1265
1266static int thread_callback(const struct option *opt, const char *arg, int unset)
1267{
1268        int *thread = (int *)opt->value;
1269        if (unset)
1270                *thread = 0;
1271        else if (!arg || !strcmp(arg, "shallow"))
1272                *thread = THREAD_SHALLOW;
1273        else if (!strcmp(arg, "deep"))
1274                *thread = THREAD_DEEP;
1275        /*
1276         * Please update _git_formatpatch() in git-completion.bash
1277         * when you add new options.
1278         */
1279        else
1280                return 1;
1281        return 0;
1282}
1283
1284static int attach_callback(const struct option *opt, const char *arg, int unset)
1285{
1286        struct rev_info *rev = (struct rev_info *)opt->value;
1287        if (unset)
1288                rev->mime_boundary = NULL;
1289        else if (arg)
1290                rev->mime_boundary = arg;
1291        else
1292                rev->mime_boundary = git_version_string;
1293        rev->no_inline = unset ? 0 : 1;
1294        return 0;
1295}
1296
1297static int inline_callback(const struct option *opt, const char *arg, int unset)
1298{
1299        struct rev_info *rev = (struct rev_info *)opt->value;
1300        if (unset)
1301                rev->mime_boundary = NULL;
1302        else if (arg)
1303                rev->mime_boundary = arg;
1304        else
1305                rev->mime_boundary = git_version_string;
1306        rev->no_inline = 0;
1307        return 0;
1308}
1309
1310static int header_callback(const struct option *opt, const char *arg, int unset)
1311{
1312        if (unset) {
1313                string_list_clear(&extra_hdr, 0);
1314                string_list_clear(&extra_to, 0);
1315                string_list_clear(&extra_cc, 0);
1316        } else {
1317            add_header(arg);
1318        }
1319        return 0;
1320}
1321
1322static int to_callback(const struct option *opt, const char *arg, int unset)
1323{
1324        if (unset)
1325                string_list_clear(&extra_to, 0);
1326        else
1327                string_list_append(&extra_to, arg);
1328        return 0;
1329}
1330
1331static int cc_callback(const struct option *opt, const char *arg, int unset)
1332{
1333        if (unset)
1334                string_list_clear(&extra_cc, 0);
1335        else
1336                string_list_append(&extra_cc, arg);
1337        return 0;
1338}
1339
1340static int from_callback(const struct option *opt, const char *arg, int unset)
1341{
1342        char **from = opt->value;
1343
1344        free(*from);
1345
1346        if (unset)
1347                *from = NULL;
1348        else if (arg)
1349                *from = xstrdup(arg);
1350        else
1351                *from = xstrdup(git_committer_info(IDENT_NO_DATE));
1352        return 0;
1353}
1354
1355struct base_tree_info {
1356        struct object_id base_commit;
1357        int nr_patch_id, alloc_patch_id;
1358        struct object_id *patch_id;
1359};
1360
1361static struct commit *get_base_commit(const char *base_commit,
1362                                      struct commit **list,
1363                                      int total)
1364{
1365        struct commit *base = NULL;
1366        struct commit **rev;
1367        int i = 0, rev_nr = 0;
1368
1369        if (base_commit && strcmp(base_commit, "auto")) {
1370                base = lookup_commit_reference_by_name(base_commit);
1371                if (!base)
1372                        die(_("unknown commit %s"), base_commit);
1373        } else if ((base_commit && !strcmp(base_commit, "auto")) || base_auto) {
1374                struct branch *curr_branch = branch_get(NULL);
1375                const char *upstream = branch_get_upstream(curr_branch, NULL);
1376                if (upstream) {
1377                        struct commit_list *base_list;
1378                        struct commit *commit;
1379                        struct object_id oid;
1380
1381                        if (get_oid(upstream, &oid))
1382                                die(_("failed to resolve '%s' as a valid ref"), upstream);
1383                        commit = lookup_commit_or_die(&oid, "upstream base");
1384                        base_list = get_merge_bases_many(commit, total, list);
1385                        /* There should be one and only one merge base. */
1386                        if (!base_list || base_list->next)
1387                                die(_("could not find exact merge base"));
1388                        base = base_list->item;
1389                        free_commit_list(base_list);
1390                } else {
1391                        die(_("failed to get upstream, if you want to record base commit automatically,\n"
1392                              "please use git branch --set-upstream-to to track a remote branch.\n"
1393                              "Or you could specify base commit by --base=<base-commit-id> manually"));
1394                }
1395        }
1396
1397        ALLOC_ARRAY(rev, total);
1398        for (i = 0; i < total; i++)
1399                rev[i] = list[i];
1400
1401        rev_nr = total;
1402        /*
1403         * Get merge base through pair-wise computations
1404         * and store it in rev[0].
1405         */
1406        while (rev_nr > 1) {
1407                for (i = 0; i < rev_nr / 2; i++) {
1408                        struct commit_list *merge_base;
1409                        merge_base = get_merge_bases(rev[2 * i], rev[2 * i + 1]);
1410                        if (!merge_base || merge_base->next)
1411                                die(_("failed to find exact merge base"));
1412
1413                        rev[i] = merge_base->item;
1414                }
1415
1416                if (rev_nr % 2)
1417                        rev[i] = rev[2 * i];
1418                rev_nr = DIV_ROUND_UP(rev_nr, 2);
1419        }
1420
1421        if (!in_merge_bases(base, rev[0]))
1422                die(_("base commit should be the ancestor of revision list"));
1423
1424        for (i = 0; i < total; i++) {
1425                if (base == list[i])
1426                        die(_("base commit shouldn't be in revision list"));
1427        }
1428
1429        free(rev);
1430        return base;
1431}
1432
1433define_commit_slab(commit_base, int);
1434
1435static void prepare_bases(struct base_tree_info *bases,
1436                          struct commit *base,
1437                          struct commit **list,
1438                          int total)
1439{
1440        struct commit *commit;
1441        struct rev_info revs;
1442        struct diff_options diffopt;
1443        struct commit_base commit_base;
1444        int i;
1445
1446        if (!base)
1447                return;
1448
1449        init_commit_base(&commit_base);
1450        repo_diff_setup(the_repository, &diffopt);
1451        diffopt.flags.recursive = 1;
1452        diff_setup_done(&diffopt);
1453
1454        oidcpy(&bases->base_commit, &base->object.oid);
1455
1456        repo_init_revisions(the_repository, &revs, NULL);
1457        revs.max_parents = 1;
1458        revs.topo_order = 1;
1459        for (i = 0; i < total; i++) {
1460                list[i]->object.flags &= ~UNINTERESTING;
1461                add_pending_object(&revs, &list[i]->object, "rev_list");
1462                *commit_base_at(&commit_base, list[i]) = 1;
1463        }
1464        base->object.flags |= UNINTERESTING;
1465        add_pending_object(&revs, &base->object, "base");
1466
1467        if (prepare_revision_walk(&revs))
1468                die(_("revision walk setup failed"));
1469        /*
1470         * Traverse the commits list, get prerequisite patch ids
1471         * and stuff them in bases structure.
1472         */
1473        while ((commit = get_revision(&revs)) != NULL) {
1474                struct object_id oid;
1475                struct object_id *patch_id;
1476                if (*commit_base_at(&commit_base, commit))
1477                        continue;
1478                if (commit_patch_id(commit, &diffopt, &oid, 0, 1))
1479                        die(_("cannot get patch id"));
1480                ALLOC_GROW(bases->patch_id, bases->nr_patch_id + 1, bases->alloc_patch_id);
1481                patch_id = bases->patch_id + bases->nr_patch_id;
1482                oidcpy(patch_id, &oid);
1483                bases->nr_patch_id++;
1484        }
1485        clear_commit_base(&commit_base);
1486}
1487
1488static void print_bases(struct base_tree_info *bases, FILE *file)
1489{
1490        int i;
1491
1492        /* Only do this once, either for the cover or for the first one */
1493        if (is_null_oid(&bases->base_commit))
1494                return;
1495
1496        /* Show the base commit */
1497        fprintf(file, "\nbase-commit: %s\n", oid_to_hex(&bases->base_commit));
1498
1499        /* Show the prerequisite patches */
1500        for (i = bases->nr_patch_id - 1; i >= 0; i--)
1501                fprintf(file, "prerequisite-patch-id: %s\n", oid_to_hex(&bases->patch_id[i]));
1502
1503        free(bases->patch_id);
1504        bases->nr_patch_id = 0;
1505        bases->alloc_patch_id = 0;
1506        oidclr(&bases->base_commit);
1507}
1508
1509static const char *diff_title(struct strbuf *sb, int reroll_count,
1510                       const char *generic, const char *rerolled)
1511{
1512        if (reroll_count <= 0)
1513                strbuf_addstr(sb, generic);
1514        else /* RFC may be v0, so allow -v1 to diff against v0 */
1515                strbuf_addf(sb, rerolled, reroll_count - 1);
1516        return sb->buf;
1517}
1518
1519static void infer_range_diff_ranges(struct strbuf *r1,
1520                                    struct strbuf *r2,
1521                                    const char *prev,
1522                                    struct commit *origin,
1523                                    struct commit *head)
1524{
1525        const char *head_oid = oid_to_hex(&head->object.oid);
1526
1527        if (!strstr(prev, "..")) {
1528                strbuf_addf(r1, "%s..%s", head_oid, prev);
1529                strbuf_addf(r2, "%s..%s", prev, head_oid);
1530        } else if (!origin) {
1531                die(_("failed to infer range-diff ranges"));
1532        } else {
1533                strbuf_addstr(r1, prev);
1534                strbuf_addf(r2, "%s..%s",
1535                            oid_to_hex(&origin->object.oid), head_oid);
1536        }
1537}
1538
1539int cmd_format_patch(int argc, const char **argv, const char *prefix)
1540{
1541        struct commit *commit;
1542        struct commit **list = NULL;
1543        struct rev_info rev;
1544        struct setup_revision_opt s_r_opt;
1545        int nr = 0, total, i;
1546        int use_stdout = 0;
1547        int start_number = -1;
1548        int just_numbers = 0;
1549        int ignore_if_in_upstream = 0;
1550        int cover_letter = -1;
1551        int boundary_count = 0;
1552        int no_binary_diff = 0;
1553        int zero_commit = 0;
1554        struct commit *origin = NULL;
1555        const char *in_reply_to = NULL;
1556        struct patch_ids ids;
1557        struct strbuf buf = STRBUF_INIT;
1558        int use_patch_format = 0;
1559        int quiet = 0;
1560        int reroll_count = -1;
1561        char *branch_name = NULL;
1562        char *base_commit = NULL;
1563        struct base_tree_info bases;
1564        int show_progress = 0;
1565        struct progress *progress = NULL;
1566        struct oid_array idiff_prev = OID_ARRAY_INIT;
1567        struct strbuf idiff_title = STRBUF_INIT;
1568        const char *rdiff_prev = NULL;
1569        struct strbuf rdiff1 = STRBUF_INIT;
1570        struct strbuf rdiff2 = STRBUF_INIT;
1571        struct strbuf rdiff_title = STRBUF_INIT;
1572        int creation_factor = -1;
1573
1574        const struct option builtin_format_patch_options[] = {
1575                { OPTION_CALLBACK, 'n', "numbered", &numbered, NULL,
1576                            N_("use [PATCH n/m] even with a single patch"),
1577                            PARSE_OPT_NOARG, numbered_callback },
1578                { OPTION_CALLBACK, 'N', "no-numbered", &numbered, NULL,
1579                            N_("use [PATCH] even with multiple patches"),
1580                            PARSE_OPT_NOARG | PARSE_OPT_NONEG, no_numbered_callback },
1581                OPT_BOOL('s', "signoff", &do_signoff, N_("add Signed-off-by:")),
1582                OPT_BOOL(0, "stdout", &use_stdout,
1583                            N_("print patches to standard out")),
1584                OPT_BOOL(0, "cover-letter", &cover_letter,
1585                            N_("generate a cover letter")),
1586                OPT_BOOL(0, "numbered-files", &just_numbers,
1587                            N_("use simple number sequence for output file names")),
1588                OPT_STRING(0, "suffix", &fmt_patch_suffix, N_("sfx"),
1589                            N_("use <sfx> instead of '.patch'")),
1590                OPT_INTEGER(0, "start-number", &start_number,
1591                            N_("start numbering patches at <n> instead of 1")),
1592                OPT_INTEGER('v', "reroll-count", &reroll_count,
1593                            N_("mark the series as Nth re-roll")),
1594                { OPTION_CALLBACK, 0, "rfc", &rev, NULL,
1595                            N_("Use [RFC PATCH] instead of [PATCH]"),
1596                            PARSE_OPT_NOARG | PARSE_OPT_NONEG, rfc_callback },
1597                { OPTION_CALLBACK, 0, "subject-prefix", &rev, N_("prefix"),
1598                            N_("Use [<prefix>] instead of [PATCH]"),
1599                            PARSE_OPT_NONEG, subject_prefix_callback },
1600                { OPTION_CALLBACK, 'o', "output-directory", &output_directory,
1601                            N_("dir"), N_("store resulting files in <dir>"),
1602                            PARSE_OPT_NONEG, output_directory_callback },
1603                { OPTION_CALLBACK, 'k', "keep-subject", &rev, NULL,
1604                            N_("don't strip/add [PATCH]"),
1605                            PARSE_OPT_NOARG | PARSE_OPT_NONEG, keep_callback },
1606                OPT_BOOL(0, "no-binary", &no_binary_diff,
1607                         N_("don't output binary diffs")),
1608                OPT_BOOL(0, "zero-commit", &zero_commit,
1609                         N_("output all-zero hash in From header")),
1610                OPT_BOOL(0, "ignore-if-in-upstream", &ignore_if_in_upstream,
1611                         N_("don't include a patch matching a commit upstream")),
1612                OPT_SET_INT_F('p', "no-stat", &use_patch_format,
1613                              N_("show patch format instead of default (patch + stat)"),
1614                              1, PARSE_OPT_NONEG),
1615                OPT_GROUP(N_("Messaging")),
1616                { OPTION_CALLBACK, 0, "add-header", NULL, N_("header"),
1617                            N_("add email header"), 0, header_callback },
1618                { OPTION_CALLBACK, 0, "to", NULL, N_("email"), N_("add To: header"),
1619                            0, to_callback },
1620                { OPTION_CALLBACK, 0, "cc", NULL, N_("email"), N_("add Cc: header"),
1621                            0, cc_callback },
1622                { OPTION_CALLBACK, 0, "from", &from, N_("ident"),
1623                            N_("set From address to <ident> (or committer ident if absent)"),
1624                            PARSE_OPT_OPTARG, from_callback },
1625                OPT_STRING(0, "in-reply-to", &in_reply_to, N_("message-id"),
1626                            N_("make first mail a reply to <message-id>")),
1627                { OPTION_CALLBACK, 0, "attach", &rev, N_("boundary"),
1628                            N_("attach the patch"), PARSE_OPT_OPTARG,
1629                            attach_callback },
1630                { OPTION_CALLBACK, 0, "inline", &rev, N_("boundary"),
1631                            N_("inline the patch"),
1632                            PARSE_OPT_OPTARG | PARSE_OPT_NONEG,
1633                            inline_callback },
1634                { OPTION_CALLBACK, 0, "thread", &thread, N_("style"),
1635                            N_("enable message threading, styles: shallow, deep"),
1636                            PARSE_OPT_OPTARG, thread_callback },
1637                OPT_STRING(0, "signature", &signature, N_("signature"),
1638                            N_("add a signature")),
1639                OPT_STRING(0, "base", &base_commit, N_("base-commit"),
1640                           N_("add prerequisite tree info to the patch series")),
1641                OPT_FILENAME(0, "signature-file", &signature_file,
1642                                N_("add a signature from a file")),
1643                OPT__QUIET(&quiet, N_("don't print the patch filenames")),
1644                OPT_BOOL(0, "progress", &show_progress,
1645                         N_("show progress while generating patches")),
1646                OPT_CALLBACK(0, "interdiff", &idiff_prev, N_("rev"),
1647                             N_("show changes against <rev> in cover letter or single patch"),
1648                             parse_opt_object_name),
1649                OPT_STRING(0, "range-diff", &rdiff_prev, N_("refspec"),
1650                           N_("show changes against <refspec> in cover letter or single patch")),
1651                OPT_INTEGER(0, "creation-factor", &creation_factor,
1652                            N_("percentage by which creation is weighted")),
1653                OPT_END()
1654        };
1655
1656        extra_hdr.strdup_strings = 1;
1657        extra_to.strdup_strings = 1;
1658        extra_cc.strdup_strings = 1;
1659        init_log_defaults();
1660        repo_init_revisions(the_repository, &rev, prefix);
1661        git_config(git_format_config, &rev);
1662        rev.commit_format = CMIT_FMT_EMAIL;
1663        rev.expand_tabs_in_log_default = 0;
1664        rev.verbose_header = 1;
1665        rev.diff = 1;
1666        rev.max_parents = 1;
1667        rev.diffopt.flags.recursive = 1;
1668        rev.subject_prefix = fmt_patch_subject_prefix;
1669        memset(&s_r_opt, 0, sizeof(s_r_opt));
1670        s_r_opt.def = "HEAD";
1671        s_r_opt.revarg_opt = REVARG_COMMITTISH;
1672
1673        if (default_attach) {
1674                rev.mime_boundary = default_attach;
1675                rev.no_inline = 1;
1676        }
1677
1678        /*
1679         * Parse the arguments before setup_revisions(), or something
1680         * like "git format-patch -o a123 HEAD^.." may fail; a123 is
1681         * possibly a valid SHA1.
1682         */
1683        argc = parse_options(argc, argv, prefix, builtin_format_patch_options,
1684                             builtin_format_patch_usage,
1685                             PARSE_OPT_KEEP_ARGV0 | PARSE_OPT_KEEP_UNKNOWN |
1686                             PARSE_OPT_KEEP_DASHDASH);
1687
1688        if (0 < reroll_count) {
1689                struct strbuf sprefix = STRBUF_INIT;
1690                strbuf_addf(&sprefix, "%s v%d",
1691                            rev.subject_prefix, reroll_count);
1692                rev.reroll_count = reroll_count;
1693                rev.subject_prefix = strbuf_detach(&sprefix, NULL);
1694        }
1695
1696        for (i = 0; i < extra_hdr.nr; i++) {
1697                strbuf_addstr(&buf, extra_hdr.items[i].string);
1698                strbuf_addch(&buf, '\n');
1699        }
1700
1701        if (extra_to.nr)
1702                strbuf_addstr(&buf, "To: ");
1703        for (i = 0; i < extra_to.nr; i++) {
1704                if (i)
1705                        strbuf_addstr(&buf, "    ");
1706                strbuf_addstr(&buf, extra_to.items[i].string);
1707                if (i + 1 < extra_to.nr)
1708                        strbuf_addch(&buf, ',');
1709                strbuf_addch(&buf, '\n');
1710        }
1711
1712        if (extra_cc.nr)
1713                strbuf_addstr(&buf, "Cc: ");
1714        for (i = 0; i < extra_cc.nr; i++) {
1715                if (i)
1716                        strbuf_addstr(&buf, "    ");
1717                strbuf_addstr(&buf, extra_cc.items[i].string);
1718                if (i + 1 < extra_cc.nr)
1719                        strbuf_addch(&buf, ',');
1720                strbuf_addch(&buf, '\n');
1721        }
1722
1723        rev.extra_headers = strbuf_detach(&buf, NULL);
1724
1725        if (from) {
1726                if (split_ident_line(&rev.from_ident, from, strlen(from)))
1727                        die(_("invalid ident line: %s"), from);
1728        }
1729
1730        if (start_number < 0)
1731                start_number = 1;
1732
1733        /*
1734         * If numbered is set solely due to format.numbered in config,
1735         * and it would conflict with --keep-subject (-k) from the
1736         * command line, reset "numbered".
1737         */
1738        if (numbered && keep_subject && !numbered_cmdline_opt)
1739                numbered = 0;
1740
1741        if (numbered && keep_subject)
1742                die(_("-n and -k are mutually exclusive"));
1743        if (keep_subject && subject_prefix)
1744                die(_("--subject-prefix/--rfc and -k are mutually exclusive"));
1745        rev.preserve_subject = keep_subject;
1746
1747        argc = setup_revisions(argc, argv, &rev, &s_r_opt);
1748        if (argc > 1)
1749                die(_("unrecognized argument: %s"), argv[1]);
1750
1751        if (rev.diffopt.output_format & DIFF_FORMAT_NAME)
1752                die(_("--name-only does not make sense"));
1753        if (rev.diffopt.output_format & DIFF_FORMAT_NAME_STATUS)
1754                die(_("--name-status does not make sense"));
1755        if (rev.diffopt.output_format & DIFF_FORMAT_CHECKDIFF)
1756                die(_("--check does not make sense"));
1757
1758        if (!use_patch_format &&
1759                (!rev.diffopt.output_format ||
1760                 rev.diffopt.output_format == DIFF_FORMAT_PATCH))
1761                rev.diffopt.output_format = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_SUMMARY;
1762        if (!rev.diffopt.stat_width)
1763                rev.diffopt.stat_width = MAIL_DEFAULT_WRAP;
1764
1765        /* Always generate a patch */
1766        rev.diffopt.output_format |= DIFF_FORMAT_PATCH;
1767
1768        rev.zero_commit = zero_commit;
1769
1770        if (!rev.diffopt.flags.text && !no_binary_diff)
1771                rev.diffopt.flags.binary = 1;
1772
1773        if (rev.show_notes)
1774                init_display_notes(&rev.notes_opt);
1775
1776        if (!output_directory && !use_stdout)
1777                output_directory = config_output_directory;
1778
1779        if (!use_stdout)
1780                output_directory = set_outdir(prefix, output_directory);
1781        else
1782                setup_pager();
1783
1784        if (output_directory) {
1785                if (rev.diffopt.use_color != GIT_COLOR_ALWAYS)
1786                        rev.diffopt.use_color = GIT_COLOR_NEVER;
1787                if (use_stdout)
1788                        die(_("standard output, or directory, which one?"));
1789                if (mkdir(output_directory, 0777) < 0 && errno != EEXIST)
1790                        die_errno(_("could not create directory '%s'"),
1791                                  output_directory);
1792        }
1793
1794        if (rev.pending.nr == 1) {
1795                int check_head = 0;
1796
1797                if (rev.max_count < 0 && !rev.show_root_diff) {
1798                        /*
1799                         * This is traditional behaviour of "git format-patch
1800                         * origin" that prepares what the origin side still
1801                         * does not have.
1802                         */
1803                        rev.pending.objects[0].item->flags |= UNINTERESTING;
1804                        add_head_to_pending(&rev);
1805                        check_head = 1;
1806                }
1807                /*
1808                 * Otherwise, it is "format-patch -22 HEAD", and/or
1809                 * "format-patch --root HEAD".  The user wants
1810                 * get_revision() to do the usual traversal.
1811                 */
1812
1813                if (!strcmp(rev.pending.objects[0].name, "HEAD"))
1814                        check_head = 1;
1815
1816                if (check_head) {
1817                        const char *ref, *v;
1818                        ref = resolve_ref_unsafe("HEAD", RESOLVE_REF_READING,
1819                                                 NULL, NULL);
1820                        if (ref && skip_prefix(ref, "refs/heads/", &v))
1821                                branch_name = xstrdup(v);
1822                        else
1823                                branch_name = xstrdup(""); /* no branch */
1824                }
1825        }
1826
1827        /*
1828         * We cannot move this anywhere earlier because we do want to
1829         * know if --root was given explicitly from the command line.
1830         */
1831        rev.show_root_diff = 1;
1832
1833        if (ignore_if_in_upstream) {
1834                /* Don't say anything if head and upstream are the same. */
1835                if (rev.pending.nr == 2) {
1836                        struct object_array_entry *o = rev.pending.objects;
1837                        if (oideq(&o[0].item->oid, &o[1].item->oid))
1838                                goto done;
1839                }
1840                get_patch_ids(&rev, &ids);
1841        }
1842
1843        if (prepare_revision_walk(&rev))
1844                die(_("revision walk setup failed"));
1845        rev.boundary = 1;
1846        while ((commit = get_revision(&rev)) != NULL) {
1847                if (commit->object.flags & BOUNDARY) {
1848                        boundary_count++;
1849                        origin = (boundary_count == 1) ? commit : NULL;
1850                        continue;
1851                }
1852
1853                if (ignore_if_in_upstream && has_commit_patch_id(commit, &ids))
1854                        continue;
1855
1856                nr++;
1857                REALLOC_ARRAY(list, nr);
1858                list[nr - 1] = commit;
1859        }
1860        if (nr == 0)
1861                /* nothing to do */
1862                goto done;
1863        total = nr;
1864        if (cover_letter == -1) {
1865                if (config_cover_letter == COVER_AUTO)
1866                        cover_letter = (total > 1);
1867                else
1868                        cover_letter = (config_cover_letter == COVER_ON);
1869        }
1870        if (!keep_subject && auto_number && (total > 1 || cover_letter))
1871                numbered = 1;
1872        if (numbered)
1873                rev.total = total + start_number - 1;
1874
1875        if (idiff_prev.nr) {
1876                if (!cover_letter && total != 1)
1877                        die(_("--interdiff requires --cover-letter or single patch"));
1878                rev.idiff_oid1 = &idiff_prev.oid[idiff_prev.nr - 1];
1879                rev.idiff_oid2 = get_commit_tree_oid(list[0]);
1880                rev.idiff_title = diff_title(&idiff_title, reroll_count,
1881                                             _("Interdiff:"),
1882                                             _("Interdiff against v%d:"));
1883        }
1884
1885        if (creation_factor < 0)
1886                creation_factor = RANGE_DIFF_CREATION_FACTOR_DEFAULT;
1887        else if (!rdiff_prev)
1888                die(_("--creation-factor requires --range-diff"));
1889
1890        if (rdiff_prev) {
1891                if (!cover_letter && total != 1)
1892                        die(_("--range-diff requires --cover-letter or single patch"));
1893
1894                infer_range_diff_ranges(&rdiff1, &rdiff2, rdiff_prev,
1895                                        origin, list[0]);
1896                rev.rdiff1 = rdiff1.buf;
1897                rev.rdiff2 = rdiff2.buf;
1898                rev.creation_factor = creation_factor;
1899                rev.rdiff_title = diff_title(&rdiff_title, reroll_count,
1900                                             _("Range-diff:"),
1901                                             _("Range-diff against v%d:"));
1902        }
1903
1904        if (!signature) {
1905                ; /* --no-signature inhibits all signatures */
1906        } else if (signature && signature != git_version_string) {
1907                ; /* non-default signature already set */
1908        } else if (signature_file) {
1909                struct strbuf buf = STRBUF_INIT;
1910
1911                if (strbuf_read_file(&buf, signature_file, 128) < 0)
1912                        die_errno(_("unable to read signature file '%s'"), signature_file);
1913                signature = strbuf_detach(&buf, NULL);
1914        }
1915
1916        memset(&bases, 0, sizeof(bases));
1917        if (base_commit || base_auto) {
1918                struct commit *base = get_base_commit(base_commit, list, nr);
1919                reset_revision_walk();
1920                clear_object_flags(UNINTERESTING);
1921                prepare_bases(&bases, base, list, nr);
1922        }
1923
1924        if (in_reply_to || thread || cover_letter)
1925                rev.ref_message_ids = xcalloc(1, sizeof(struct string_list));
1926        if (in_reply_to) {
1927                const char *msgid = clean_message_id(in_reply_to);
1928                string_list_append(rev.ref_message_ids, msgid);
1929        }
1930        rev.numbered_files = just_numbers;
1931        rev.patch_suffix = fmt_patch_suffix;
1932        if (cover_letter) {
1933                if (thread)
1934                        gen_message_id(&rev, "cover");
1935                make_cover_letter(&rev, use_stdout,
1936                                  origin, nr, list, branch_name, quiet);
1937                print_bases(&bases, rev.diffopt.file);
1938                print_signature(rev.diffopt.file);
1939                total++;
1940                start_number--;
1941                /* interdiff/range-diff in cover-letter; omit from patches */
1942                rev.idiff_oid1 = NULL;
1943                rev.rdiff1 = NULL;
1944        }
1945        rev.add_signoff = do_signoff;
1946
1947        if (show_progress)
1948                progress = start_delayed_progress(_("Generating patches"), total);
1949        while (0 <= --nr) {
1950                int shown;
1951                display_progress(progress, total - nr);
1952                commit = list[nr];
1953                rev.nr = total - nr + (start_number - 1);
1954                /* Make the second and subsequent mails replies to the first */
1955                if (thread) {
1956                        /* Have we already had a message ID? */
1957                        if (rev.message_id) {
1958                                /*
1959                                 * For deep threading: make every mail
1960                                 * a reply to the previous one, no
1961                                 * matter what other options are set.
1962                                 *
1963                                 * For shallow threading:
1964                                 *
1965                                 * Without --cover-letter and
1966                                 * --in-reply-to, make every mail a
1967                                 * reply to the one before.
1968                                 *
1969                                 * With --in-reply-to but no
1970                                 * --cover-letter, make every mail a
1971                                 * reply to the <reply-to>.
1972                                 *
1973                                 * With --cover-letter, make every
1974                                 * mail but the cover letter a reply
1975                                 * to the cover letter.  The cover
1976                                 * letter is a reply to the
1977                                 * --in-reply-to, if specified.
1978                                 */
1979                                if (thread == THREAD_SHALLOW
1980                                    && rev.ref_message_ids->nr > 0
1981                                    && (!cover_letter || rev.nr > 1))
1982                                        free(rev.message_id);
1983                                else
1984                                        string_list_append(rev.ref_message_ids,
1985                                                           rev.message_id);
1986                        }
1987                        gen_message_id(&rev, oid_to_hex(&commit->object.oid));
1988                }
1989
1990                if (!use_stdout &&
1991                    open_next_file(rev.numbered_files ? NULL : commit, NULL, &rev, quiet))
1992                        die(_("failed to create output files"));
1993                shown = log_tree_commit(&rev, commit);
1994                free_commit_buffer(the_repository->parsed_objects,
1995                                   commit);
1996
1997                /* We put one extra blank line between formatted
1998                 * patches and this flag is used by log-tree code
1999                 * to see if it needs to emit a LF before showing
2000                 * the log; when using one file per patch, we do
2001                 * not want the extra blank line.
2002                 */
2003                if (!use_stdout)
2004                        rev.shown_one = 0;
2005                if (shown) {
2006                        print_bases(&bases, rev.diffopt.file);
2007                        if (rev.mime_boundary)
2008                                fprintf(rev.diffopt.file, "\n--%s%s--\n\n\n",
2009                                       mime_boundary_leader,
2010                                       rev.mime_boundary);
2011                        else
2012                                print_signature(rev.diffopt.file);
2013                }
2014                if (!use_stdout)
2015                        fclose(rev.diffopt.file);
2016        }
2017        stop_progress(&progress);
2018        free(list);
2019        free(branch_name);
2020        string_list_clear(&extra_to, 0);
2021        string_list_clear(&extra_cc, 0);
2022        string_list_clear(&extra_hdr, 0);
2023        if (ignore_if_in_upstream)
2024                free_patch_ids(&ids);
2025
2026done:
2027        oid_array_clear(&idiff_prev);
2028        strbuf_release(&idiff_title);
2029        strbuf_release(&rdiff1);
2030        strbuf_release(&rdiff2);
2031        strbuf_release(&rdiff_title);
2032        return 0;
2033}
2034
2035static int add_pending_commit(const char *arg, struct rev_info *revs, int flags)
2036{
2037        struct object_id oid;
2038        if (get_oid(arg, &oid) == 0) {
2039                struct commit *commit = lookup_commit_reference(the_repository,
2040                                                                &oid);
2041                if (commit) {
2042                        commit->object.flags |= flags;
2043                        add_pending_object(revs, &commit->object, arg);
2044                        return 0;
2045                }
2046        }
2047        return -1;
2048}
2049
2050static const char * const cherry_usage[] = {
2051        N_("git cherry [-v] [<upstream> [<head> [<limit>]]]"),
2052        NULL
2053};
2054
2055static void print_commit(char sign, struct commit *commit, int verbose,
2056                         int abbrev, FILE *file)
2057{
2058        if (!verbose) {
2059                fprintf(file, "%c %s\n", sign,
2060                       find_unique_abbrev(&commit->object.oid, abbrev));
2061        } else {
2062                struct strbuf buf = STRBUF_INIT;
2063                pp_commit_easy(CMIT_FMT_ONELINE, commit, &buf);
2064                fprintf(file, "%c %s %s\n", sign,
2065                       find_unique_abbrev(&commit->object.oid, abbrev),
2066                       buf.buf);
2067                strbuf_release(&buf);
2068        }
2069}
2070
2071int cmd_cherry(int argc, const char **argv, const char *prefix)
2072{
2073        struct rev_info revs;
2074        struct patch_ids ids;
2075        struct commit *commit;
2076        struct commit_list *list = NULL;
2077        struct branch *current_branch;
2078        const char *upstream;
2079        const char *head = "HEAD";
2080        const char *limit = NULL;
2081        int verbose = 0, abbrev = 0;
2082
2083        struct option options[] = {
2084                OPT__ABBREV(&abbrev),
2085                OPT__VERBOSE(&verbose, N_("be verbose")),
2086                OPT_END()
2087        };
2088
2089        argc = parse_options(argc, argv, prefix, options, cherry_usage, 0);
2090
2091        switch (argc) {
2092        case 3:
2093                limit = argv[2];
2094                /* FALLTHROUGH */
2095        case 2:
2096                head = argv[1];
2097                /* FALLTHROUGH */
2098        case 1:
2099                upstream = argv[0];
2100                break;
2101        default:
2102                current_branch = branch_get(NULL);
2103                upstream = branch_get_upstream(current_branch, NULL);
2104                if (!upstream) {
2105                        fprintf(stderr, _("Could not find a tracked"
2106                                        " remote branch, please"
2107                                        " specify <upstream> manually.\n"));
2108                        usage_with_options(cherry_usage, options);
2109                }
2110        }
2111
2112        repo_init_revisions(the_repository, &revs, prefix);
2113        revs.max_parents = 1;
2114
2115        if (add_pending_commit(head, &revs, 0))
2116                die(_("unknown commit %s"), head);
2117        if (add_pending_commit(upstream, &revs, UNINTERESTING))
2118                die(_("unknown commit %s"), upstream);
2119
2120        /* Don't say anything if head and upstream are the same. */
2121        if (revs.pending.nr == 2) {
2122                struct object_array_entry *o = revs.pending.objects;
2123                if (oideq(&o[0].item->oid, &o[1].item->oid))
2124                        return 0;
2125        }
2126
2127        get_patch_ids(&revs, &ids);
2128
2129        if (limit && add_pending_commit(limit, &revs, UNINTERESTING))
2130                die(_("unknown commit %s"), limit);
2131
2132        /* reverse the list of commits */
2133        if (prepare_revision_walk(&revs))
2134                die(_("revision walk setup failed"));
2135        while ((commit = get_revision(&revs)) != NULL) {
2136                commit_list_insert(commit, &list);
2137        }
2138
2139        while (list) {
2140                char sign = '+';
2141
2142                commit = list->item;
2143                if (has_commit_patch_id(commit, &ids))
2144                        sign = '-';
2145                print_commit(sign, commit, verbose, abbrev, revs.diffopt.file);
2146                list = list->next;
2147        }
2148
2149        free_patch_ids(&ids);
2150        return 0;
2151}