builtin / log.con commit Allow customizable commit decorations colors (5e11bee)
   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 "color.h"
   9#include "commit.h"
  10#include "diff.h"
  11#include "revision.h"
  12#include "log-tree.h"
  13#include "builtin.h"
  14#include "tag.h"
  15#include "reflog-walk.h"
  16#include "patch-ids.h"
  17#include "run-command.h"
  18#include "shortlog.h"
  19#include "remote.h"
  20#include "string-list.h"
  21#include "parse-options.h"
  22
  23/* Set a default date-time format for git log ("log.date" config variable) */
  24static const char *default_date_mode = NULL;
  25
  26static int default_show_root = 1;
  27static int decoration_style;
  28static const char *fmt_patch_subject_prefix = "PATCH";
  29static const char *fmt_pretty;
  30
  31static const char * const builtin_log_usage =
  32        "git log [<options>] [<since>..<until>] [[--] <path>...]\n"
  33        "   or: git show [options] <object>...";
  34
  35static int parse_decoration_style(const char *var, const char *value)
  36{
  37        switch (git_config_maybe_bool(var, value)) {
  38        case 1:
  39                return DECORATE_SHORT_REFS;
  40        case 0:
  41                return 0;
  42        default:
  43                break;
  44        }
  45        if (!strcmp(value, "full"))
  46                return DECORATE_FULL_REFS;
  47        else if (!strcmp(value, "short"))
  48                return DECORATE_SHORT_REFS;
  49        return -1;
  50}
  51
  52static void cmd_log_init(int argc, const char **argv, const char *prefix,
  53                         struct rev_info *rev, struct setup_revision_opt *opt)
  54{
  55        int i;
  56        int decoration_given = 0;
  57        struct userformat_want w;
  58
  59        rev->abbrev = DEFAULT_ABBREV;
  60        rev->commit_format = CMIT_FMT_DEFAULT;
  61        if (fmt_pretty)
  62                get_commit_format(fmt_pretty, rev);
  63        rev->verbose_header = 1;
  64        DIFF_OPT_SET(&rev->diffopt, RECURSIVE);
  65        rev->show_root_diff = default_show_root;
  66        rev->subject_prefix = fmt_patch_subject_prefix;
  67        DIFF_OPT_SET(&rev->diffopt, ALLOW_TEXTCONV);
  68
  69        if (default_date_mode)
  70                rev->date_mode = parse_date_format(default_date_mode);
  71
  72        /*
  73         * Check for -h before setup_revisions(), or "git log -h" will
  74         * fail when run without a git directory.
  75         */
  76        if (argc == 2 && !strcmp(argv[1], "-h"))
  77                usage(builtin_log_usage);
  78        argc = setup_revisions(argc, argv, rev, opt);
  79
  80        memset(&w, 0, sizeof(w));
  81        userformat_find_requirements(NULL, &w);
  82
  83        if (!rev->show_notes_given && (!rev->pretty_given || w.notes))
  84                rev->show_notes = 1;
  85        if (rev->show_notes)
  86                init_display_notes(&rev->notes_opt);
  87
  88        if (rev->diffopt.pickaxe || rev->diffopt.filter)
  89                rev->always_show_header = 0;
  90        if (DIFF_OPT_TST(&rev->diffopt, FOLLOW_RENAMES)) {
  91                rev->always_show_header = 0;
  92                if (rev->diffopt.nr_paths != 1)
  93                        usage("git logs can only follow renames on one pathname at a time");
  94        }
  95        for (i = 1; i < argc; i++) {
  96                const char *arg = argv[i];
  97                if (!strcmp(arg, "--decorate")) {
  98                        decoration_style = DECORATE_SHORT_REFS;
  99                        decoration_given = 1;
 100                } else if (!prefixcmp(arg, "--decorate=")) {
 101                        const char *v = skip_prefix(arg, "--decorate=");
 102                        decoration_style = parse_decoration_style(arg, v);
 103                        if (decoration_style < 0)
 104                                die("invalid --decorate option: %s", arg);
 105                        decoration_given = 1;
 106                } else if (!strcmp(arg, "--no-decorate")) {
 107                        decoration_style = 0;
 108                } else if (!strcmp(arg, "--source")) {
 109                        rev->show_source = 1;
 110                } else if (!strcmp(arg, "-h")) {
 111                        usage(builtin_log_usage);
 112                } else
 113                        die("unrecognized argument: %s", arg);
 114        }
 115
 116        /*
 117         * defeat log.decorate configuration interacting with --pretty=raw
 118         * from the command line.
 119         */
 120        if (!decoration_given && rev->pretty_given
 121            && rev->commit_format == CMIT_FMT_RAW)
 122                decoration_style = 0;
 123
 124        if (decoration_style) {
 125                rev->show_decorations = 1;
 126                load_ref_decorations(decoration_style);
 127        }
 128}
 129
 130/*
 131 * This gives a rough estimate for how many commits we
 132 * will print out in the list.
 133 */
 134static int estimate_commit_count(struct rev_info *rev, struct commit_list *list)
 135{
 136        int n = 0;
 137
 138        while (list) {
 139                struct commit *commit = list->item;
 140                unsigned int flags = commit->object.flags;
 141                list = list->next;
 142                if (!(flags & (TREESAME | UNINTERESTING)))
 143                        n++;
 144        }
 145        return n;
 146}
 147
 148static void show_early_header(struct rev_info *rev, const char *stage, int nr)
 149{
 150        if (rev->shown_one) {
 151                rev->shown_one = 0;
 152                if (rev->commit_format != CMIT_FMT_ONELINE)
 153                        putchar(rev->diffopt.line_termination);
 154        }
 155        printf("Final output: %d %s\n", nr, stage);
 156}
 157
 158static struct itimerval early_output_timer;
 159
 160static void log_show_early(struct rev_info *revs, struct commit_list *list)
 161{
 162        int i = revs->early_output;
 163        int show_header = 1;
 164
 165        sort_in_topological_order(&list, revs->lifo);
 166        while (list && i) {
 167                struct commit *commit = list->item;
 168                switch (simplify_commit(revs, commit)) {
 169                case commit_show:
 170                        if (show_header) {
 171                                int n = estimate_commit_count(revs, list);
 172                                show_early_header(revs, "incomplete", n);
 173                                show_header = 0;
 174                        }
 175                        log_tree_commit(revs, commit);
 176                        i--;
 177                        break;
 178                case commit_ignore:
 179                        break;
 180                case commit_error:
 181                        return;
 182                }
 183                list = list->next;
 184        }
 185
 186        /* Did we already get enough commits for the early output? */
 187        if (!i)
 188                return;
 189
 190        /*
 191         * ..if no, then repeat it twice a second until we
 192         * do.
 193         *
 194         * NOTE! We don't use "it_interval", because if the
 195         * reader isn't listening, we want our output to be
 196         * throttled by the writing, and not have the timer
 197         * trigger every second even if we're blocked on a
 198         * reader!
 199         */
 200        early_output_timer.it_value.tv_sec = 0;
 201        early_output_timer.it_value.tv_usec = 500000;
 202        setitimer(ITIMER_REAL, &early_output_timer, NULL);
 203}
 204
 205static void early_output(int signal)
 206{
 207        show_early_output = log_show_early;
 208}
 209
 210static void setup_early_output(struct rev_info *rev)
 211{
 212        struct sigaction sa;
 213
 214        /*
 215         * Set up the signal handler, minimally intrusively:
 216         * we only set a single volatile integer word (not
 217         * using sigatomic_t - trying to avoid unnecessary
 218         * system dependencies and headers), and using
 219         * SA_RESTART.
 220         */
 221        memset(&sa, 0, sizeof(sa));
 222        sa.sa_handler = early_output;
 223        sigemptyset(&sa.sa_mask);
 224        sa.sa_flags = SA_RESTART;
 225        sigaction(SIGALRM, &sa, NULL);
 226
 227        /*
 228         * If we can get the whole output in less than a
 229         * tenth of a second, don't even bother doing the
 230         * early-output thing..
 231         *
 232         * This is a one-time-only trigger.
 233         */
 234        early_output_timer.it_value.tv_sec = 0;
 235        early_output_timer.it_value.tv_usec = 100000;
 236        setitimer(ITIMER_REAL, &early_output_timer, NULL);
 237}
 238
 239static void finish_early_output(struct rev_info *rev)
 240{
 241        int n = estimate_commit_count(rev, rev->commits);
 242        signal(SIGALRM, SIG_IGN);
 243        show_early_header(rev, "done", n);
 244}
 245
 246static int cmd_log_walk(struct rev_info *rev)
 247{
 248        struct commit *commit;
 249
 250        if (rev->early_output)
 251                setup_early_output(rev);
 252
 253        if (prepare_revision_walk(rev))
 254                die("revision walk setup failed");
 255
 256        if (rev->early_output)
 257                finish_early_output(rev);
 258
 259        /*
 260         * For --check and --exit-code, the exit code is based on CHECK_FAILED
 261         * and HAS_CHANGES being accumulated in rev->diffopt, so be careful to
 262         * retain that state information if replacing rev->diffopt in this loop
 263         */
 264        while ((commit = get_revision(rev)) != NULL) {
 265                log_tree_commit(rev, commit);
 266                if (!rev->reflog_info) {
 267                        /* we allow cycles in reflog ancestry */
 268                        free(commit->buffer);
 269                        commit->buffer = NULL;
 270                }
 271                free_commit_list(commit->parents);
 272                commit->parents = NULL;
 273        }
 274        if (rev->diffopt.output_format & DIFF_FORMAT_CHECKDIFF &&
 275            DIFF_OPT_TST(&rev->diffopt, CHECK_FAILED)) {
 276                return 02;
 277        }
 278        return diff_result_code(&rev->diffopt, 0);
 279}
 280
 281static int git_log_config(const char *var, const char *value, void *cb)
 282{
 283        if (!strcmp(var, "format.pretty"))
 284                return git_config_string(&fmt_pretty, var, value);
 285        if (!strcmp(var, "format.subjectprefix"))
 286                return git_config_string(&fmt_patch_subject_prefix, var, value);
 287        if (!strcmp(var, "log.date"))
 288                return git_config_string(&default_date_mode, var, value);
 289        if (!strcmp(var, "log.decorate")) {
 290                decoration_style = parse_decoration_style(var, value);
 291                if (decoration_style < 0)
 292                        decoration_style = 0; /* maybe warn? */
 293                return 0;
 294        }
 295        if (!strcmp(var, "log.showroot")) {
 296                default_show_root = git_config_bool(var, value);
 297                return 0;
 298        }
 299        if (!prefixcmp(var, "color.decorate."))
 300                return parse_decorate_color_config(var, 15, value);
 301
 302        return git_diff_ui_config(var, value, cb);
 303}
 304
 305int cmd_whatchanged(int argc, const char **argv, const char *prefix)
 306{
 307        struct rev_info rev;
 308        struct setup_revision_opt opt;
 309
 310        git_config(git_log_config, NULL);
 311
 312        if (diff_use_color_default == -1)
 313                diff_use_color_default = git_use_color_default;
 314
 315        init_revisions(&rev, prefix);
 316        rev.diff = 1;
 317        rev.simplify_history = 0;
 318        memset(&opt, 0, sizeof(opt));
 319        opt.def = "HEAD";
 320        cmd_log_init(argc, argv, prefix, &rev, &opt);
 321        if (!rev.diffopt.output_format)
 322                rev.diffopt.output_format = DIFF_FORMAT_RAW;
 323        return cmd_log_walk(&rev);
 324}
 325
 326static void show_tagger(char *buf, int len, struct rev_info *rev)
 327{
 328        struct strbuf out = STRBUF_INIT;
 329
 330        pp_user_info("Tagger", rev->commit_format, &out, buf, rev->date_mode,
 331                git_log_output_encoding ?
 332                git_log_output_encoding: git_commit_encoding);
 333        printf("%s", out.buf);
 334        strbuf_release(&out);
 335}
 336
 337static int show_object(const unsigned char *sha1, int show_tag_object,
 338        struct rev_info *rev)
 339{
 340        unsigned long size;
 341        enum object_type type;
 342        char *buf = read_sha1_file(sha1, &type, &size);
 343        int offset = 0;
 344
 345        if (!buf)
 346                return error("Could not read object %s", sha1_to_hex(sha1));
 347
 348        if (show_tag_object)
 349                while (offset < size && buf[offset] != '\n') {
 350                        int new_offset = offset + 1;
 351                        while (new_offset < size && buf[new_offset++] != '\n')
 352                                ; /* do nothing */
 353                        if (!prefixcmp(buf + offset, "tagger "))
 354                                show_tagger(buf + offset + 7,
 355                                            new_offset - offset - 7, rev);
 356                        offset = new_offset;
 357                }
 358
 359        if (offset < size)
 360                fwrite(buf + offset, size - offset, 1, stdout);
 361        free(buf);
 362        return 0;
 363}
 364
 365static int show_tree_object(const unsigned char *sha1,
 366                const char *base, int baselen,
 367                const char *pathname, unsigned mode, int stage, void *context)
 368{
 369        printf("%s%s\n", pathname, S_ISDIR(mode) ? "/" : "");
 370        return 0;
 371}
 372
 373static void show_rev_tweak_rev(struct rev_info *rev, struct setup_revision_opt *opt)
 374{
 375        if (rev->ignore_merges) {
 376                /* There was no "-m" on the command line */
 377                rev->ignore_merges = 0;
 378                if (!rev->first_parent_only && !rev->combine_merges) {
 379                        /* No "--first-parent", "-c", nor "--cc" */
 380                        rev->combine_merges = 1;
 381                        rev->dense_combined_merges = 1;
 382                }
 383        }
 384        if (!rev->diffopt.output_format)
 385                rev->diffopt.output_format = DIFF_FORMAT_PATCH;
 386}
 387
 388int cmd_show(int argc, const char **argv, const char *prefix)
 389{
 390        struct rev_info rev;
 391        struct object_array_entry *objects;
 392        struct setup_revision_opt opt;
 393        int i, count, ret = 0;
 394
 395        git_config(git_log_config, NULL);
 396
 397        if (diff_use_color_default == -1)
 398                diff_use_color_default = git_use_color_default;
 399
 400        init_revisions(&rev, prefix);
 401        rev.diff = 1;
 402        rev.always_show_header = 1;
 403        rev.no_walk = 1;
 404        memset(&opt, 0, sizeof(opt));
 405        opt.def = "HEAD";
 406        opt.tweak = show_rev_tweak_rev;
 407        cmd_log_init(argc, argv, prefix, &rev, &opt);
 408
 409        count = rev.pending.nr;
 410        objects = rev.pending.objects;
 411        for (i = 0; i < count && !ret; i++) {
 412                struct object *o = objects[i].item;
 413                const char *name = objects[i].name;
 414                switch (o->type) {
 415                case OBJ_BLOB:
 416                        ret = show_object(o->sha1, 0, NULL);
 417                        break;
 418                case OBJ_TAG: {
 419                        struct tag *t = (struct tag *)o;
 420
 421                        if (rev.shown_one)
 422                                putchar('\n');
 423                        printf("%stag %s%s\n",
 424                                        diff_get_color_opt(&rev.diffopt, DIFF_COMMIT),
 425                                        t->tag,
 426                                        diff_get_color_opt(&rev.diffopt, DIFF_RESET));
 427                        ret = show_object(o->sha1, 1, &rev);
 428                        rev.shown_one = 1;
 429                        if (ret)
 430                                break;
 431                        o = parse_object(t->tagged->sha1);
 432                        if (!o)
 433                                ret = error("Could not read object %s",
 434                                            sha1_to_hex(t->tagged->sha1));
 435                        objects[i].item = o;
 436                        i--;
 437                        break;
 438                }
 439                case OBJ_TREE:
 440                        if (rev.shown_one)
 441                                putchar('\n');
 442                        printf("%stree %s%s\n\n",
 443                                        diff_get_color_opt(&rev.diffopt, DIFF_COMMIT),
 444                                        name,
 445                                        diff_get_color_opt(&rev.diffopt, DIFF_RESET));
 446                        read_tree_recursive((struct tree *)o, "", 0, 0, NULL,
 447                                        show_tree_object, NULL);
 448                        rev.shown_one = 1;
 449                        break;
 450                case OBJ_COMMIT:
 451                        rev.pending.nr = rev.pending.alloc = 0;
 452                        rev.pending.objects = NULL;
 453                        add_object_array(o, name, &rev.pending);
 454                        ret = cmd_log_walk(&rev);
 455                        break;
 456                default:
 457                        ret = error("Unknown type: %d", o->type);
 458                }
 459        }
 460        free(objects);
 461        return ret;
 462}
 463
 464/*
 465 * This is equivalent to "git log -g --abbrev-commit --pretty=oneline"
 466 */
 467int cmd_log_reflog(int argc, const char **argv, const char *prefix)
 468{
 469        struct rev_info rev;
 470        struct setup_revision_opt opt;
 471
 472        git_config(git_log_config, NULL);
 473
 474        if (diff_use_color_default == -1)
 475                diff_use_color_default = git_use_color_default;
 476
 477        init_revisions(&rev, prefix);
 478        init_reflog_walk(&rev.reflog_info);
 479        rev.abbrev_commit = 1;
 480        rev.verbose_header = 1;
 481        memset(&opt, 0, sizeof(opt));
 482        opt.def = "HEAD";
 483        cmd_log_init(argc, argv, prefix, &rev, &opt);
 484
 485        /*
 486         * This means that we override whatever commit format the user gave
 487         * on the cmd line.  Sad, but cmd_log_init() currently doesn't
 488         * allow us to set a different default.
 489         */
 490        rev.commit_format = CMIT_FMT_ONELINE;
 491        rev.use_terminator = 1;
 492        rev.always_show_header = 1;
 493
 494        /*
 495         * We get called through "git reflog", so unlike the other log
 496         * routines, we need to set up our pager manually..
 497         */
 498        setup_pager();
 499
 500        return cmd_log_walk(&rev);
 501}
 502
 503int cmd_log(int argc, const char **argv, const char *prefix)
 504{
 505        struct rev_info rev;
 506        struct setup_revision_opt opt;
 507
 508        git_config(git_log_config, NULL);
 509
 510        if (diff_use_color_default == -1)
 511                diff_use_color_default = git_use_color_default;
 512
 513        init_revisions(&rev, prefix);
 514        rev.always_show_header = 1;
 515        memset(&opt, 0, sizeof(opt));
 516        opt.def = "HEAD";
 517        cmd_log_init(argc, argv, prefix, &rev, &opt);
 518        return cmd_log_walk(&rev);
 519}
 520
 521/* format-patch */
 522
 523static const char *fmt_patch_suffix = ".patch";
 524static int numbered = 0;
 525static int auto_number = 1;
 526
 527static char *default_attach = NULL;
 528
 529static struct string_list extra_hdr;
 530static struct string_list extra_to;
 531static struct string_list extra_cc;
 532
 533static void add_header(const char *value)
 534{
 535        struct string_list_item *item;
 536        int len = strlen(value);
 537        while (len && value[len - 1] == '\n')
 538                len--;
 539
 540        if (!strncasecmp(value, "to: ", 4)) {
 541                item = string_list_append(value + 4, &extra_to);
 542                len -= 4;
 543        } else if (!strncasecmp(value, "cc: ", 4)) {
 544                item = string_list_append(value + 4, &extra_cc);
 545                len -= 4;
 546        } else {
 547                item = string_list_append(value, &extra_hdr);
 548        }
 549
 550        item->string[len] = '\0';
 551}
 552
 553#define THREAD_SHALLOW 1
 554#define THREAD_DEEP 2
 555static int thread = 0;
 556static int do_signoff = 0;
 557
 558static int git_format_config(const char *var, const char *value, void *cb)
 559{
 560        if (!strcmp(var, "format.headers")) {
 561                if (!value)
 562                        die("format.headers without value");
 563                add_header(value);
 564                return 0;
 565        }
 566        if (!strcmp(var, "format.suffix"))
 567                return git_config_string(&fmt_patch_suffix, var, value);
 568        if (!strcmp(var, "format.to")) {
 569                if (!value)
 570                        return config_error_nonbool(var);
 571                string_list_append(value, &extra_to);
 572                return 0;
 573        }
 574        if (!strcmp(var, "format.cc")) {
 575                if (!value)
 576                        return config_error_nonbool(var);
 577                string_list_append(value, &extra_cc);
 578                return 0;
 579        }
 580        if (!strcmp(var, "diff.color") || !strcmp(var, "color.diff")) {
 581                return 0;
 582        }
 583        if (!strcmp(var, "format.numbered")) {
 584                if (value && !strcasecmp(value, "auto")) {
 585                        auto_number = 1;
 586                        return 0;
 587                }
 588                numbered = git_config_bool(var, value);
 589                auto_number = auto_number && numbered;
 590                return 0;
 591        }
 592        if (!strcmp(var, "format.attach")) {
 593                if (value && *value)
 594                        default_attach = xstrdup(value);
 595                else
 596                        default_attach = xstrdup(git_version_string);
 597                return 0;
 598        }
 599        if (!strcmp(var, "format.thread")) {
 600                if (value && !strcasecmp(value, "deep")) {
 601                        thread = THREAD_DEEP;
 602                        return 0;
 603                }
 604                if (value && !strcasecmp(value, "shallow")) {
 605                        thread = THREAD_SHALLOW;
 606                        return 0;
 607                }
 608                thread = git_config_bool(var, value) && THREAD_SHALLOW;
 609                return 0;
 610        }
 611        if (!strcmp(var, "format.signoff")) {
 612                do_signoff = git_config_bool(var, value);
 613                return 0;
 614        }
 615
 616        return git_log_config(var, value, cb);
 617}
 618
 619static FILE *realstdout = NULL;
 620static const char *output_directory = NULL;
 621static int outdir_offset;
 622
 623static int reopen_stdout(struct commit *commit, struct rev_info *rev)
 624{
 625        struct strbuf filename = STRBUF_INIT;
 626        int suffix_len = strlen(fmt_patch_suffix) + 1;
 627
 628        if (output_directory) {
 629                strbuf_addstr(&filename, output_directory);
 630                if (filename.len >=
 631                    PATH_MAX - FORMAT_PATCH_NAME_MAX - suffix_len)
 632                        return error("name of output directory is too long");
 633                if (filename.buf[filename.len - 1] != '/')
 634                        strbuf_addch(&filename, '/');
 635        }
 636
 637        get_patch_filename(commit, rev->nr, fmt_patch_suffix, &filename);
 638
 639        if (!DIFF_OPT_TST(&rev->diffopt, QUICK))
 640                fprintf(realstdout, "%s\n", filename.buf + outdir_offset);
 641
 642        if (freopen(filename.buf, "w", stdout) == NULL)
 643                return error("Cannot open patch file %s", filename.buf);
 644
 645        strbuf_release(&filename);
 646        return 0;
 647}
 648
 649static void get_patch_ids(struct rev_info *rev, struct patch_ids *ids, const char *prefix)
 650{
 651        struct rev_info check_rev;
 652        struct commit *commit;
 653        struct object *o1, *o2;
 654        unsigned flags1, flags2;
 655
 656        if (rev->pending.nr != 2)
 657                die("Need exactly one range.");
 658
 659        o1 = rev->pending.objects[0].item;
 660        flags1 = o1->flags;
 661        o2 = rev->pending.objects[1].item;
 662        flags2 = o2->flags;
 663
 664        if ((flags1 & UNINTERESTING) == (flags2 & UNINTERESTING))
 665                die("Not a range.");
 666
 667        init_patch_ids(ids);
 668
 669        /* given a range a..b get all patch ids for b..a */
 670        init_revisions(&check_rev, prefix);
 671        o1->flags ^= UNINTERESTING;
 672        o2->flags ^= UNINTERESTING;
 673        add_pending_object(&check_rev, o1, "o1");
 674        add_pending_object(&check_rev, o2, "o2");
 675        if (prepare_revision_walk(&check_rev))
 676                die("revision walk setup failed");
 677
 678        while ((commit = get_revision(&check_rev)) != NULL) {
 679                /* ignore merges */
 680                if (commit->parents && commit->parents->next)
 681                        continue;
 682
 683                add_commit_patch_id(commit, ids);
 684        }
 685
 686        /* reset for next revision walk */
 687        clear_commit_marks((struct commit *)o1,
 688                        SEEN | UNINTERESTING | SHOWN | ADDED);
 689        clear_commit_marks((struct commit *)o2,
 690                        SEEN | UNINTERESTING | SHOWN | ADDED);
 691        o1->flags = flags1;
 692        o2->flags = flags2;
 693}
 694
 695static void gen_message_id(struct rev_info *info, char *base)
 696{
 697        const char *committer = git_committer_info(IDENT_WARN_ON_NO_NAME);
 698        const char *email_start = strrchr(committer, '<');
 699        const char *email_end = strrchr(committer, '>');
 700        struct strbuf buf = STRBUF_INIT;
 701        if (!email_start || !email_end || email_start > email_end - 1)
 702                die("Could not extract email from committer identity.");
 703        strbuf_addf(&buf, "%s.%lu.git.%.*s", base,
 704                    (unsigned long) time(NULL),
 705                    (int)(email_end - email_start - 1), email_start + 1);
 706        info->message_id = strbuf_detach(&buf, NULL);
 707}
 708
 709static void make_cover_letter(struct rev_info *rev, int use_stdout,
 710                              int numbered, int numbered_files,
 711                              struct commit *origin,
 712                              int nr, struct commit **list, struct commit *head)
 713{
 714        const char *committer;
 715        const char *subject_start = NULL;
 716        const char *body = "*** SUBJECT HERE ***\n\n*** BLURB HERE ***\n";
 717        const char *msg;
 718        const char *extra_headers = rev->extra_headers;
 719        struct shortlog log;
 720        struct strbuf sb = STRBUF_INIT;
 721        int i;
 722        const char *encoding = "UTF-8";
 723        struct diff_options opts;
 724        int need_8bit_cte = 0;
 725        struct commit *commit = NULL;
 726
 727        if (rev->commit_format != CMIT_FMT_EMAIL)
 728                die("Cover letter needs email format");
 729
 730        committer = git_committer_info(0);
 731
 732        if (!numbered_files) {
 733                /*
 734                 * We fake a commit for the cover letter so we get the filename
 735                 * desired.
 736                 */
 737                commit = xcalloc(1, sizeof(*commit));
 738                commit->buffer = xmalloc(400);
 739                snprintf(commit->buffer, 400,
 740                        "tree 0000000000000000000000000000000000000000\n"
 741                        "parent %s\n"
 742                        "author %s\n"
 743                        "committer %s\n\n"
 744                        "cover letter\n",
 745                        sha1_to_hex(head->object.sha1), committer, committer);
 746        }
 747
 748        if (!use_stdout && reopen_stdout(commit, rev))
 749                return;
 750
 751        if (commit) {
 752
 753                free(commit->buffer);
 754                free(commit);
 755        }
 756
 757        log_write_email_headers(rev, head, &subject_start, &extra_headers,
 758                                &need_8bit_cte);
 759
 760        for (i = 0; !need_8bit_cte && i < nr; i++)
 761                if (has_non_ascii(list[i]->buffer))
 762                        need_8bit_cte = 1;
 763
 764        msg = body;
 765        pp_user_info(NULL, CMIT_FMT_EMAIL, &sb, committer, DATE_RFC2822,
 766                     encoding);
 767        pp_title_line(CMIT_FMT_EMAIL, &msg, &sb, subject_start, extra_headers,
 768                      encoding, need_8bit_cte);
 769        pp_remainder(CMIT_FMT_EMAIL, &msg, &sb, 0);
 770        printf("%s\n", sb.buf);
 771
 772        strbuf_release(&sb);
 773
 774        shortlog_init(&log);
 775        log.wrap_lines = 1;
 776        log.wrap = 72;
 777        log.in1 = 2;
 778        log.in2 = 4;
 779        for (i = 0; i < nr; i++)
 780                shortlog_add_commit(&log, list[i]);
 781
 782        shortlog_output(&log);
 783
 784        /*
 785         * We can only do diffstat with a unique reference point
 786         */
 787        if (!origin)
 788                return;
 789
 790        memcpy(&opts, &rev->diffopt, sizeof(opts));
 791        opts.output_format = DIFF_FORMAT_SUMMARY | DIFF_FORMAT_DIFFSTAT;
 792
 793        diff_setup_done(&opts);
 794
 795        diff_tree_sha1(origin->tree->object.sha1,
 796                       head->tree->object.sha1,
 797                       "", &opts);
 798        diffcore_std(&opts);
 799        diff_flush(&opts);
 800
 801        printf("\n");
 802}
 803
 804static const char *clean_message_id(const char *msg_id)
 805{
 806        char ch;
 807        const char *a, *z, *m;
 808
 809        m = msg_id;
 810        while ((ch = *m) && (isspace(ch) || (ch == '<')))
 811                m++;
 812        a = m;
 813        z = NULL;
 814        while ((ch = *m)) {
 815                if (!isspace(ch) && (ch != '>'))
 816                        z = m;
 817                m++;
 818        }
 819        if (!z)
 820                die("insane in-reply-to: %s", msg_id);
 821        if (++z == m)
 822                return a;
 823        return xmemdupz(a, z - a);
 824}
 825
 826static const char *set_outdir(const char *prefix, const char *output_directory)
 827{
 828        if (output_directory && is_absolute_path(output_directory))
 829                return output_directory;
 830
 831        if (!prefix || !*prefix) {
 832                if (output_directory)
 833                        return output_directory;
 834                /* The user did not explicitly ask for "./" */
 835                outdir_offset = 2;
 836                return "./";
 837        }
 838
 839        outdir_offset = strlen(prefix);
 840        if (!output_directory)
 841                return prefix;
 842
 843        return xstrdup(prefix_filename(prefix, outdir_offset,
 844                                       output_directory));
 845}
 846
 847static const char * const builtin_format_patch_usage[] = {
 848        "git format-patch [options] [<since> | <revision range>]",
 849        NULL
 850};
 851
 852static int keep_subject = 0;
 853
 854static int keep_callback(const struct option *opt, const char *arg, int unset)
 855{
 856        ((struct rev_info *)opt->value)->total = -1;
 857        keep_subject = 1;
 858        return 0;
 859}
 860
 861static int subject_prefix = 0;
 862
 863static int subject_prefix_callback(const struct option *opt, const char *arg,
 864                            int unset)
 865{
 866        subject_prefix = 1;
 867        ((struct rev_info *)opt->value)->subject_prefix = arg;
 868        return 0;
 869}
 870
 871static int numbered_cmdline_opt = 0;
 872
 873static int numbered_callback(const struct option *opt, const char *arg,
 874                             int unset)
 875{
 876        *(int *)opt->value = numbered_cmdline_opt = unset ? 0 : 1;
 877        if (unset)
 878                auto_number =  0;
 879        return 0;
 880}
 881
 882static int no_numbered_callback(const struct option *opt, const char *arg,
 883                                int unset)
 884{
 885        return numbered_callback(opt, arg, 1);
 886}
 887
 888static int output_directory_callback(const struct option *opt, const char *arg,
 889                              int unset)
 890{
 891        const char **dir = (const char **)opt->value;
 892        if (*dir)
 893                die("Two output directories?");
 894        *dir = arg;
 895        return 0;
 896}
 897
 898static int thread_callback(const struct option *opt, const char *arg, int unset)
 899{
 900        int *thread = (int *)opt->value;
 901        if (unset)
 902                *thread = 0;
 903        else if (!arg || !strcmp(arg, "shallow"))
 904                *thread = THREAD_SHALLOW;
 905        else if (!strcmp(arg, "deep"))
 906                *thread = THREAD_DEEP;
 907        else
 908                return 1;
 909        return 0;
 910}
 911
 912static int attach_callback(const struct option *opt, const char *arg, int unset)
 913{
 914        struct rev_info *rev = (struct rev_info *)opt->value;
 915        if (unset)
 916                rev->mime_boundary = NULL;
 917        else if (arg)
 918                rev->mime_boundary = arg;
 919        else
 920                rev->mime_boundary = git_version_string;
 921        rev->no_inline = unset ? 0 : 1;
 922        return 0;
 923}
 924
 925static int inline_callback(const struct option *opt, const char *arg, int unset)
 926{
 927        struct rev_info *rev = (struct rev_info *)opt->value;
 928        if (unset)
 929                rev->mime_boundary = NULL;
 930        else if (arg)
 931                rev->mime_boundary = arg;
 932        else
 933                rev->mime_boundary = git_version_string;
 934        rev->no_inline = 0;
 935        return 0;
 936}
 937
 938static int header_callback(const struct option *opt, const char *arg, int unset)
 939{
 940        if (unset) {
 941                string_list_clear(&extra_hdr, 0);
 942                string_list_clear(&extra_to, 0);
 943                string_list_clear(&extra_cc, 0);
 944        } else {
 945            add_header(arg);
 946        }
 947        return 0;
 948}
 949
 950static int to_callback(const struct option *opt, const char *arg, int unset)
 951{
 952        if (unset)
 953                string_list_clear(&extra_to, 0);
 954        else
 955                string_list_append(arg, &extra_to);
 956        return 0;
 957}
 958
 959static int cc_callback(const struct option *opt, const char *arg, int unset)
 960{
 961        if (unset)
 962                string_list_clear(&extra_cc, 0);
 963        else
 964                string_list_append(arg, &extra_cc);
 965        return 0;
 966}
 967
 968int cmd_format_patch(int argc, const char **argv, const char *prefix)
 969{
 970        struct commit *commit;
 971        struct commit **list = NULL;
 972        struct rev_info rev;
 973        struct setup_revision_opt s_r_opt;
 974        int nr = 0, total, i;
 975        int use_stdout = 0;
 976        int start_number = -1;
 977        int numbered_files = 0;         /* _just_ numbers */
 978        int ignore_if_in_upstream = 0;
 979        int cover_letter = 0;
 980        int boundary_count = 0;
 981        int no_binary_diff = 0;
 982        struct commit *origin = NULL, *head = NULL;
 983        const char *in_reply_to = NULL;
 984        struct patch_ids ids;
 985        char *add_signoff = NULL;
 986        struct strbuf buf = STRBUF_INIT;
 987        int use_patch_format = 0;
 988        const struct option builtin_format_patch_options[] = {
 989                { OPTION_CALLBACK, 'n', "numbered", &numbered, NULL,
 990                            "use [PATCH n/m] even with a single patch",
 991                            PARSE_OPT_NOARG, numbered_callback },
 992                { OPTION_CALLBACK, 'N', "no-numbered", &numbered, NULL,
 993                            "use [PATCH] even with multiple patches",
 994                            PARSE_OPT_NOARG, no_numbered_callback },
 995                OPT_BOOLEAN('s', "signoff", &do_signoff, "add Signed-off-by:"),
 996                OPT_BOOLEAN(0, "stdout", &use_stdout,
 997                            "print patches to standard out"),
 998                OPT_BOOLEAN(0, "cover-letter", &cover_letter,
 999                            "generate a cover letter"),
1000                OPT_BOOLEAN(0, "numbered-files", &numbered_files,
1001                            "use simple number sequence for output file names"),
1002                OPT_STRING(0, "suffix", &fmt_patch_suffix, "sfx",
1003                            "use <sfx> instead of '.patch'"),
1004                OPT_INTEGER(0, "start-number", &start_number,
1005                            "start numbering patches at <n> instead of 1"),
1006                { OPTION_CALLBACK, 0, "subject-prefix", &rev, "prefix",
1007                            "Use [<prefix>] instead of [PATCH]",
1008                            PARSE_OPT_NONEG, subject_prefix_callback },
1009                { OPTION_CALLBACK, 'o', "output-directory", &output_directory,
1010                            "dir", "store resulting files in <dir>",
1011                            PARSE_OPT_NONEG, output_directory_callback },
1012                { OPTION_CALLBACK, 'k', "keep-subject", &rev, NULL,
1013                            "don't strip/add [PATCH]",
1014                            PARSE_OPT_NOARG | PARSE_OPT_NONEG, keep_callback },
1015                OPT_BOOLEAN(0, "no-binary", &no_binary_diff,
1016                            "don't output binary diffs"),
1017                OPT_BOOLEAN(0, "ignore-if-in-upstream", &ignore_if_in_upstream,
1018                            "don't include a patch matching a commit upstream"),
1019                { OPTION_BOOLEAN, 'p', "no-stat", &use_patch_format, NULL,
1020                  "show patch format instead of default (patch + stat)",
1021                  PARSE_OPT_NONEG | PARSE_OPT_NOARG },
1022                OPT_GROUP("Messaging"),
1023                { OPTION_CALLBACK, 0, "add-header", NULL, "header",
1024                            "add email header", 0, header_callback },
1025                { OPTION_CALLBACK, 0, "to", NULL, "email", "add To: header",
1026                            0, to_callback },
1027                { OPTION_CALLBACK, 0, "cc", NULL, "email", "add Cc: header",
1028                            0, cc_callback },
1029                OPT_STRING(0, "in-reply-to", &in_reply_to, "message-id",
1030                            "make first mail a reply to <message-id>"),
1031                { OPTION_CALLBACK, 0, "attach", &rev, "boundary",
1032                            "attach the patch", PARSE_OPT_OPTARG,
1033                            attach_callback },
1034                { OPTION_CALLBACK, 0, "inline", &rev, "boundary",
1035                            "inline the patch",
1036                            PARSE_OPT_OPTARG | PARSE_OPT_NONEG,
1037                            inline_callback },
1038                { OPTION_CALLBACK, 0, "thread", &thread, "style",
1039                            "enable message threading, styles: shallow, deep",
1040                            PARSE_OPT_OPTARG, thread_callback },
1041                OPT_END()
1042        };
1043
1044        extra_hdr.strdup_strings = 1;
1045        extra_to.strdup_strings = 1;
1046        extra_cc.strdup_strings = 1;
1047        git_config(git_format_config, NULL);
1048        init_revisions(&rev, prefix);
1049        rev.commit_format = CMIT_FMT_EMAIL;
1050        rev.verbose_header = 1;
1051        rev.diff = 1;
1052        rev.combine_merges = 0;
1053        rev.ignore_merges = 1;
1054        DIFF_OPT_SET(&rev.diffopt, RECURSIVE);
1055        rev.subject_prefix = fmt_patch_subject_prefix;
1056        memset(&s_r_opt, 0, sizeof(s_r_opt));
1057        s_r_opt.def = "HEAD";
1058
1059        if (default_attach) {
1060                rev.mime_boundary = default_attach;
1061                rev.no_inline = 1;
1062        }
1063
1064        /*
1065         * Parse the arguments before setup_revisions(), or something
1066         * like "git format-patch -o a123 HEAD^.." may fail; a123 is
1067         * possibly a valid SHA1.
1068         */
1069        argc = parse_options(argc, argv, prefix, builtin_format_patch_options,
1070                             builtin_format_patch_usage,
1071                             PARSE_OPT_KEEP_ARGV0 | PARSE_OPT_KEEP_UNKNOWN |
1072                             PARSE_OPT_KEEP_DASHDASH);
1073
1074        if (do_signoff) {
1075                const char *committer;
1076                const char *endpos;
1077                committer = git_committer_info(IDENT_ERROR_ON_NO_NAME);
1078                endpos = strchr(committer, '>');
1079                if (!endpos)
1080                        die("bogus committer info %s", committer);
1081                add_signoff = xmemdupz(committer, endpos - committer + 1);
1082        }
1083
1084        for (i = 0; i < extra_hdr.nr; i++) {
1085                strbuf_addstr(&buf, extra_hdr.items[i].string);
1086                strbuf_addch(&buf, '\n');
1087        }
1088
1089        if (extra_to.nr)
1090                strbuf_addstr(&buf, "To: ");
1091        for (i = 0; i < extra_to.nr; i++) {
1092                if (i)
1093                        strbuf_addstr(&buf, "    ");
1094                strbuf_addstr(&buf, extra_to.items[i].string);
1095                if (i + 1 < extra_to.nr)
1096                        strbuf_addch(&buf, ',');
1097                strbuf_addch(&buf, '\n');
1098        }
1099
1100        if (extra_cc.nr)
1101                strbuf_addstr(&buf, "Cc: ");
1102        for (i = 0; i < extra_cc.nr; i++) {
1103                if (i)
1104                        strbuf_addstr(&buf, "    ");
1105                strbuf_addstr(&buf, extra_cc.items[i].string);
1106                if (i + 1 < extra_cc.nr)
1107                        strbuf_addch(&buf, ',');
1108                strbuf_addch(&buf, '\n');
1109        }
1110
1111        rev.extra_headers = strbuf_detach(&buf, NULL);
1112
1113        if (start_number < 0)
1114                start_number = 1;
1115
1116        /*
1117         * If numbered is set solely due to format.numbered in config,
1118         * and it would conflict with --keep-subject (-k) from the
1119         * command line, reset "numbered".
1120         */
1121        if (numbered && keep_subject && !numbered_cmdline_opt)
1122                numbered = 0;
1123
1124        if (numbered && keep_subject)
1125                die ("-n and -k are mutually exclusive.");
1126        if (keep_subject && subject_prefix)
1127                die ("--subject-prefix and -k are mutually exclusive.");
1128
1129        argc = setup_revisions(argc, argv, &rev, &s_r_opt);
1130        if (argc > 1)
1131                die ("unrecognized argument: %s", argv[1]);
1132
1133        if (rev.diffopt.output_format & DIFF_FORMAT_NAME)
1134                die("--name-only does not make sense");
1135        if (rev.diffopt.output_format & DIFF_FORMAT_NAME_STATUS)
1136                die("--name-status does not make sense");
1137        if (rev.diffopt.output_format & DIFF_FORMAT_CHECKDIFF)
1138                die("--check does not make sense");
1139
1140        if (!use_patch_format &&
1141                (!rev.diffopt.output_format ||
1142                 rev.diffopt.output_format == DIFF_FORMAT_PATCH))
1143                rev.diffopt.output_format = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_SUMMARY;
1144
1145        /* Always generate a patch */
1146        rev.diffopt.output_format |= DIFF_FORMAT_PATCH;
1147
1148        if (!DIFF_OPT_TST(&rev.diffopt, TEXT) && !no_binary_diff)
1149                DIFF_OPT_SET(&rev.diffopt, BINARY);
1150
1151        if (rev.show_notes)
1152                init_display_notes(&rev.notes_opt);
1153
1154        if (!use_stdout)
1155                output_directory = set_outdir(prefix, output_directory);
1156
1157        if (output_directory) {
1158                if (use_stdout)
1159                        die("standard output, or directory, which one?");
1160                if (mkdir(output_directory, 0777) < 0 && errno != EEXIST)
1161                        die_errno("Could not create directory '%s'",
1162                                  output_directory);
1163        }
1164
1165        if (rev.pending.nr == 1) {
1166                if (rev.max_count < 0 && !rev.show_root_diff) {
1167                        /*
1168                         * This is traditional behaviour of "git format-patch
1169                         * origin" that prepares what the origin side still
1170                         * does not have.
1171                         */
1172                        rev.pending.objects[0].item->flags |= UNINTERESTING;
1173                        add_head_to_pending(&rev);
1174                }
1175                /*
1176                 * Otherwise, it is "format-patch -22 HEAD", and/or
1177                 * "format-patch --root HEAD".  The user wants
1178                 * get_revision() to do the usual traversal.
1179                 */
1180        }
1181
1182        /*
1183         * We cannot move this anywhere earlier because we do want to
1184         * know if --root was given explicitly from the command line.
1185         */
1186        rev.show_root_diff = 1;
1187
1188        if (cover_letter) {
1189                /* remember the range */
1190                int i;
1191                for (i = 0; i < rev.pending.nr; i++) {
1192                        struct object *o = rev.pending.objects[i].item;
1193                        if (!(o->flags & UNINTERESTING))
1194                                head = (struct commit *)o;
1195                }
1196                /* We can't generate a cover letter without any patches */
1197                if (!head)
1198                        return 0;
1199        }
1200
1201        if (ignore_if_in_upstream) {
1202                /* Don't say anything if head and upstream are the same. */
1203                if (rev.pending.nr == 2) {
1204                        struct object_array_entry *o = rev.pending.objects;
1205                        if (hashcmp(o[0].item->sha1, o[1].item->sha1) == 0)
1206                                return 0;
1207                }
1208                get_patch_ids(&rev, &ids, prefix);
1209        }
1210
1211        if (!use_stdout)
1212                realstdout = xfdopen(xdup(1), "w");
1213
1214        if (prepare_revision_walk(&rev))
1215                die("revision walk setup failed");
1216        rev.boundary = 1;
1217        while ((commit = get_revision(&rev)) != NULL) {
1218                if (commit->object.flags & BOUNDARY) {
1219                        boundary_count++;
1220                        origin = (boundary_count == 1) ? commit : NULL;
1221                        continue;
1222                }
1223
1224                /* ignore merges */
1225                if (commit->parents && commit->parents->next)
1226                        continue;
1227
1228                if (ignore_if_in_upstream &&
1229                                has_commit_patch_id(commit, &ids))
1230                        continue;
1231
1232                nr++;
1233                list = xrealloc(list, nr * sizeof(list[0]));
1234                list[nr - 1] = commit;
1235        }
1236        total = nr;
1237        if (!keep_subject && auto_number && total > 1)
1238                numbered = 1;
1239        if (numbered)
1240                rev.total = total + start_number - 1;
1241        if (in_reply_to || thread || cover_letter)
1242                rev.ref_message_ids = xcalloc(1, sizeof(struct string_list));
1243        if (in_reply_to) {
1244                const char *msgid = clean_message_id(in_reply_to);
1245                string_list_append(msgid, rev.ref_message_ids);
1246        }
1247        rev.numbered_files = numbered_files;
1248        rev.patch_suffix = fmt_patch_suffix;
1249        if (cover_letter) {
1250                if (thread)
1251                        gen_message_id(&rev, "cover");
1252                make_cover_letter(&rev, use_stdout, numbered, numbered_files,
1253                                  origin, nr, list, head);
1254                total++;
1255                start_number--;
1256        }
1257        rev.add_signoff = add_signoff;
1258        while (0 <= --nr) {
1259                int shown;
1260                commit = list[nr];
1261                rev.nr = total - nr + (start_number - 1);
1262                /* Make the second and subsequent mails replies to the first */
1263                if (thread) {
1264                        /* Have we already had a message ID? */
1265                        if (rev.message_id) {
1266                                /*
1267                                 * For deep threading: make every mail
1268                                 * a reply to the previous one, no
1269                                 * matter what other options are set.
1270                                 *
1271                                 * For shallow threading:
1272                                 *
1273                                 * Without --cover-letter and
1274                                 * --in-reply-to, make every mail a
1275                                 * reply to the one before.
1276                                 *
1277                                 * With --in-reply-to but no
1278                                 * --cover-letter, make every mail a
1279                                 * reply to the <reply-to>.
1280                                 *
1281                                 * With --cover-letter, make every
1282                                 * mail but the cover letter a reply
1283                                 * to the cover letter.  The cover
1284                                 * letter is a reply to the
1285                                 * --in-reply-to, if specified.
1286                                 */
1287                                if (thread == THREAD_SHALLOW
1288                                    && rev.ref_message_ids->nr > 0
1289                                    && (!cover_letter || rev.nr > 1))
1290                                        free(rev.message_id);
1291                                else
1292                                        string_list_append(rev.message_id,
1293                                                           rev.ref_message_ids);
1294                        }
1295                        gen_message_id(&rev, sha1_to_hex(commit->object.sha1));
1296                }
1297
1298                if (!use_stdout && reopen_stdout(numbered_files ? NULL : commit,
1299                                                 &rev))
1300                        die("Failed to create output files");
1301                shown = log_tree_commit(&rev, commit);
1302                free(commit->buffer);
1303                commit->buffer = NULL;
1304
1305                /* We put one extra blank line between formatted
1306                 * patches and this flag is used by log-tree code
1307                 * to see if it needs to emit a LF before showing
1308                 * the log; when using one file per patch, we do
1309                 * not want the extra blank line.
1310                 */
1311                if (!use_stdout)
1312                        rev.shown_one = 0;
1313                if (shown) {
1314                        if (rev.mime_boundary)
1315                                printf("\n--%s%s--\n\n\n",
1316                                       mime_boundary_leader,
1317                                       rev.mime_boundary);
1318                        else
1319                                printf("-- \n%s\n\n", git_version_string);
1320                }
1321                if (!use_stdout)
1322                        fclose(stdout);
1323        }
1324        free(list);
1325        string_list_clear(&extra_to, 0);
1326        string_list_clear(&extra_cc, 0);
1327        string_list_clear(&extra_hdr, 0);
1328        if (ignore_if_in_upstream)
1329                free_patch_ids(&ids);
1330        return 0;
1331}
1332
1333static int add_pending_commit(const char *arg, struct rev_info *revs, int flags)
1334{
1335        unsigned char sha1[20];
1336        if (get_sha1(arg, sha1) == 0) {
1337                struct commit *commit = lookup_commit_reference(sha1);
1338                if (commit) {
1339                        commit->object.flags |= flags;
1340                        add_pending_object(revs, &commit->object, arg);
1341                        return 0;
1342                }
1343        }
1344        return -1;
1345}
1346
1347static const char * const cherry_usage[] = {
1348        "git cherry [-v] [<upstream> [<head> [<limit>]]]",
1349        NULL
1350};
1351
1352int cmd_cherry(int argc, const char **argv, const char *prefix)
1353{
1354        struct rev_info revs;
1355        struct patch_ids ids;
1356        struct commit *commit;
1357        struct commit_list *list = NULL;
1358        struct branch *current_branch;
1359        const char *upstream;
1360        const char *head = "HEAD";
1361        const char *limit = NULL;
1362        int verbose = 0, abbrev = 0;
1363
1364        struct option options[] = {
1365                OPT__ABBREV(&abbrev),
1366                OPT__VERBOSE(&verbose),
1367                OPT_END()
1368        };
1369
1370        argc = parse_options(argc, argv, prefix, options, cherry_usage, 0);
1371
1372        switch (argc) {
1373        case 3:
1374                limit = argv[2];
1375                /* FALLTHROUGH */
1376        case 2:
1377                head = argv[1];
1378                /* FALLTHROUGH */
1379        case 1:
1380                upstream = argv[0];
1381                break;
1382        default:
1383                current_branch = branch_get(NULL);
1384                if (!current_branch || !current_branch->merge
1385                                        || !current_branch->merge[0]
1386                                        || !current_branch->merge[0]->dst) {
1387                        fprintf(stderr, "Could not find a tracked"
1388                                        " remote branch, please"
1389                                        " specify <upstream> manually.\n");
1390                        usage_with_options(cherry_usage, options);
1391                }
1392
1393                upstream = current_branch->merge[0]->dst;
1394        }
1395
1396        init_revisions(&revs, prefix);
1397        revs.diff = 1;
1398        revs.combine_merges = 0;
1399        revs.ignore_merges = 1;
1400        DIFF_OPT_SET(&revs.diffopt, RECURSIVE);
1401
1402        if (add_pending_commit(head, &revs, 0))
1403                die("Unknown commit %s", head);
1404        if (add_pending_commit(upstream, &revs, UNINTERESTING))
1405                die("Unknown commit %s", upstream);
1406
1407        /* Don't say anything if head and upstream are the same. */
1408        if (revs.pending.nr == 2) {
1409                struct object_array_entry *o = revs.pending.objects;
1410                if (hashcmp(o[0].item->sha1, o[1].item->sha1) == 0)
1411                        return 0;
1412        }
1413
1414        get_patch_ids(&revs, &ids, prefix);
1415
1416        if (limit && add_pending_commit(limit, &revs, UNINTERESTING))
1417                die("Unknown commit %s", limit);
1418
1419        /* reverse the list of commits */
1420        if (prepare_revision_walk(&revs))
1421                die("revision walk setup failed");
1422        while ((commit = get_revision(&revs)) != NULL) {
1423                /* ignore merges */
1424                if (commit->parents && commit->parents->next)
1425                        continue;
1426
1427                commit_list_insert(commit, &list);
1428        }
1429
1430        while (list) {
1431                char sign = '+';
1432
1433                commit = list->item;
1434                if (has_commit_patch_id(commit, &ids))
1435                        sign = '-';
1436
1437                if (verbose) {
1438                        struct strbuf buf = STRBUF_INIT;
1439                        struct pretty_print_context ctx = {0};
1440                        pretty_print_commit(CMIT_FMT_ONELINE, commit,
1441                                            &buf, &ctx);
1442                        printf("%c %s %s\n", sign,
1443                               find_unique_abbrev(commit->object.sha1, abbrev),
1444                               buf.buf);
1445                        strbuf_release(&buf);
1446                }
1447                else {
1448                        printf("%c %s\n", sign,
1449                               find_unique_abbrev(commit->object.sha1, abbrev));
1450                }
1451
1452                list = list->next;
1453        }
1454
1455        free_patch_ids(&ids);
1456        return 0;
1457}