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