builtin-log.con commit Merge branch 'ph/diffopts' (e6cb314)
   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 "commit.h"
   9#include "diff.h"
  10#include "revision.h"
  11#include "log-tree.h"
  12#include "builtin.h"
  13#include "tag.h"
  14#include "reflog-walk.h"
  15#include "patch-ids.h"
  16#include "refs.h"
  17
  18static int default_show_root = 1;
  19static const char *fmt_patch_subject_prefix = "PATCH";
  20
  21/* this is in builtin-diff.c */
  22void add_head(struct rev_info *revs);
  23
  24static void add_name_decoration(const char *prefix, const char *name, struct object *obj)
  25{
  26        int plen = strlen(prefix);
  27        int nlen = strlen(name);
  28        struct name_decoration *res = xmalloc(sizeof(struct name_decoration) + plen + nlen);
  29        memcpy(res->name, prefix, plen);
  30        memcpy(res->name + plen, name, nlen + 1);
  31        res->next = add_decoration(&name_decoration, obj, res);
  32}
  33
  34static int add_ref_decoration(const char *refname, const unsigned char *sha1, int flags, void *cb_data)
  35{
  36        struct object *obj = parse_object(sha1);
  37        if (!obj)
  38                return 0;
  39        add_name_decoration("", refname, obj);
  40        while (obj->type == OBJ_TAG) {
  41                obj = ((struct tag *)obj)->tagged;
  42                if (!obj)
  43                        break;
  44                add_name_decoration("tag: ", refname, obj);
  45        }
  46        return 0;
  47}
  48
  49static void cmd_log_init(int argc, const char **argv, const char *prefix,
  50                      struct rev_info *rev)
  51{
  52        int i;
  53        int decorate = 0;
  54
  55        rev->abbrev = DEFAULT_ABBREV;
  56        rev->commit_format = CMIT_FMT_DEFAULT;
  57        rev->verbose_header = 1;
  58        DIFF_OPT_SET(&rev->diffopt, RECURSIVE);
  59        rev->show_root_diff = default_show_root;
  60        rev->subject_prefix = fmt_patch_subject_prefix;
  61        argc = setup_revisions(argc, argv, rev, "HEAD");
  62        if (rev->diffopt.pickaxe || rev->diffopt.filter)
  63                rev->always_show_header = 0;
  64        if (DIFF_OPT_TST(&rev->diffopt, FOLLOW_RENAMES)) {
  65                rev->always_show_header = 0;
  66                if (rev->diffopt.nr_paths != 1)
  67                        usage("git logs can only follow renames on one pathname at a time");
  68        }
  69        for (i = 1; i < argc; i++) {
  70                const char *arg = argv[i];
  71                if (!strcmp(arg, "--decorate")) {
  72                        if (!decorate)
  73                                for_each_ref(add_ref_decoration, NULL);
  74                        decorate = 1;
  75                } else
  76                        die("unrecognized argument: %s", arg);
  77        }
  78}
  79
  80static int cmd_log_walk(struct rev_info *rev)
  81{
  82        struct commit *commit;
  83
  84        prepare_revision_walk(rev);
  85        while ((commit = get_revision(rev)) != NULL) {
  86                log_tree_commit(rev, commit);
  87                if (!rev->reflog_info) {
  88                        /* we allow cycles in reflog ancestry */
  89                        free(commit->buffer);
  90                        commit->buffer = NULL;
  91                }
  92                free_commit_list(commit->parents);
  93                commit->parents = NULL;
  94        }
  95        return 0;
  96}
  97
  98static int git_log_config(const char *var, const char *value)
  99{
 100        if (!strcmp(var, "format.subjectprefix")) {
 101                if (!value)
 102                        die("format.subjectprefix without value");
 103                fmt_patch_subject_prefix = xstrdup(value);
 104                return 0;
 105        }
 106        if (!strcmp(var, "log.showroot")) {
 107                default_show_root = git_config_bool(var, value);
 108                return 0;
 109        }
 110        return git_diff_ui_config(var, value);
 111}
 112
 113int cmd_whatchanged(int argc, const char **argv, const char *prefix)
 114{
 115        struct rev_info rev;
 116
 117        git_config(git_log_config);
 118        init_revisions(&rev, prefix);
 119        rev.diff = 1;
 120        rev.simplify_history = 0;
 121        cmd_log_init(argc, argv, prefix, &rev);
 122        if (!rev.diffopt.output_format)
 123                rev.diffopt.output_format = DIFF_FORMAT_RAW;
 124        return cmd_log_walk(&rev);
 125}
 126
 127static int show_object(const unsigned char *sha1, int suppress_header)
 128{
 129        unsigned long size;
 130        enum object_type type;
 131        char *buf = read_sha1_file(sha1, &type, &size);
 132        int offset = 0;
 133
 134        if (!buf)
 135                return error("Could not read object %s", sha1_to_hex(sha1));
 136
 137        if (suppress_header)
 138                while (offset < size && buf[offset++] != '\n') {
 139                        int new_offset = offset;
 140                        while (new_offset < size && buf[new_offset++] != '\n')
 141                                ; /* do nothing */
 142                        offset = new_offset;
 143                }
 144
 145        if (offset < size)
 146                fwrite(buf + offset, size - offset, 1, stdout);
 147        free(buf);
 148        return 0;
 149}
 150
 151static int show_tree_object(const unsigned char *sha1,
 152                const char *base, int baselen,
 153                const char *pathname, unsigned mode, int stage)
 154{
 155        printf("%s%s\n", pathname, S_ISDIR(mode) ? "/" : "");
 156        return 0;
 157}
 158
 159int cmd_show(int argc, const char **argv, const char *prefix)
 160{
 161        struct rev_info rev;
 162        struct object_array_entry *objects;
 163        int i, count, ret = 0;
 164
 165        git_config(git_log_config);
 166        init_revisions(&rev, prefix);
 167        rev.diff = 1;
 168        rev.combine_merges = 1;
 169        rev.dense_combined_merges = 1;
 170        rev.always_show_header = 1;
 171        rev.ignore_merges = 0;
 172        rev.no_walk = 1;
 173        cmd_log_init(argc, argv, prefix, &rev);
 174
 175        count = rev.pending.nr;
 176        objects = rev.pending.objects;
 177        for (i = 0; i < count && !ret; i++) {
 178                struct object *o = objects[i].item;
 179                const char *name = objects[i].name;
 180                switch (o->type) {
 181                case OBJ_BLOB:
 182                        ret = show_object(o->sha1, 0);
 183                        break;
 184                case OBJ_TAG: {
 185                        struct tag *t = (struct tag *)o;
 186
 187                        printf("%stag %s%s\n\n",
 188                                        diff_get_color_opt(&rev.diffopt, DIFF_COMMIT),
 189                                        t->tag,
 190                                        diff_get_color_opt(&rev.diffopt, DIFF_RESET));
 191                        ret = show_object(o->sha1, 1);
 192                        objects[i].item = (struct object *)t->tagged;
 193                        i--;
 194                        break;
 195                }
 196                case OBJ_TREE:
 197                        printf("%stree %s%s\n\n",
 198                                        diff_get_color_opt(&rev.diffopt, DIFF_COMMIT),
 199                                        name,
 200                                        diff_get_color_opt(&rev.diffopt, DIFF_RESET));
 201                        read_tree_recursive((struct tree *)o, "", 0, 0, NULL,
 202                                        show_tree_object);
 203                        break;
 204                case OBJ_COMMIT:
 205                        rev.pending.nr = rev.pending.alloc = 0;
 206                        rev.pending.objects = NULL;
 207                        add_object_array(o, name, &rev.pending);
 208                        ret = cmd_log_walk(&rev);
 209                        break;
 210                default:
 211                        ret = error("Unknown type: %d", o->type);
 212                }
 213        }
 214        free(objects);
 215        return ret;
 216}
 217
 218/*
 219 * This is equivalent to "git log -g --abbrev-commit --pretty=oneline"
 220 */
 221int cmd_log_reflog(int argc, const char **argv, const char *prefix)
 222{
 223        struct rev_info rev;
 224
 225        git_config(git_log_config);
 226        init_revisions(&rev, prefix);
 227        init_reflog_walk(&rev.reflog_info);
 228        rev.abbrev_commit = 1;
 229        rev.verbose_header = 1;
 230        cmd_log_init(argc, argv, prefix, &rev);
 231
 232        /*
 233         * This means that we override whatever commit format the user gave
 234         * on the cmd line.  Sad, but cmd_log_init() currently doesn't
 235         * allow us to set a different default.
 236         */
 237        rev.commit_format = CMIT_FMT_ONELINE;
 238        rev.always_show_header = 1;
 239
 240        /*
 241         * We get called through "git reflog", so unlike the other log
 242         * routines, we need to set up our pager manually..
 243         */
 244        setup_pager();
 245
 246        return cmd_log_walk(&rev);
 247}
 248
 249int cmd_log(int argc, const char **argv, const char *prefix)
 250{
 251        struct rev_info rev;
 252
 253        git_config(git_log_config);
 254        init_revisions(&rev, prefix);
 255        rev.always_show_header = 1;
 256        cmd_log_init(argc, argv, prefix, &rev);
 257        return cmd_log_walk(&rev);
 258}
 259
 260/* format-patch */
 261#define FORMAT_PATCH_NAME_MAX 64
 262
 263static int istitlechar(char c)
 264{
 265        return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
 266                (c >= '0' && c <= '9') || c == '.' || c == '_';
 267}
 268
 269static char *extra_headers = NULL;
 270static int extra_headers_size = 0;
 271static const char *fmt_patch_suffix = ".patch";
 272static int numbered = 0;
 273static int auto_number = 0;
 274
 275static int git_format_config(const char *var, const char *value)
 276{
 277        if (!strcmp(var, "format.headers")) {
 278                int len;
 279
 280                if (!value)
 281                        die("format.headers without value");
 282                len = strlen(value);
 283                extra_headers_size += len + 1;
 284                extra_headers = xrealloc(extra_headers, extra_headers_size);
 285                extra_headers[extra_headers_size - len - 1] = 0;
 286                strcat(extra_headers, value);
 287                return 0;
 288        }
 289        if (!strcmp(var, "format.suffix")) {
 290                if (!value)
 291                        die("format.suffix without value");
 292                fmt_patch_suffix = xstrdup(value);
 293                return 0;
 294        }
 295        if (!strcmp(var, "diff.color") || !strcmp(var, "color.diff")) {
 296                return 0;
 297        }
 298        if (!strcmp(var, "format.numbered")) {
 299                if (!strcasecmp(value, "auto")) {
 300                        auto_number = 1;
 301                        return 0;
 302                }
 303
 304                numbered = git_config_bool(var, value);
 305                return 0;
 306        }
 307
 308        return git_log_config(var, value);
 309}
 310
 311
 312static FILE *realstdout = NULL;
 313static const char *output_directory = NULL;
 314
 315static int reopen_stdout(struct commit *commit, int nr, int keep_subject,
 316                         int numbered_files)
 317{
 318        char filename[PATH_MAX];
 319        char *sol;
 320        int len = 0;
 321        int suffix_len = strlen(fmt_patch_suffix) + 1;
 322
 323        if (output_directory) {
 324                if (strlen(output_directory) >=
 325                    sizeof(filename) - FORMAT_PATCH_NAME_MAX - suffix_len)
 326                        return error("name of output directory is too long");
 327                strlcpy(filename, output_directory, sizeof(filename) - suffix_len);
 328                len = strlen(filename);
 329                if (filename[len - 1] != '/')
 330                        filename[len++] = '/';
 331        }
 332
 333        if (numbered_files) {
 334                sprintf(filename + len, "%d", nr);
 335                len = strlen(filename);
 336
 337        } else {
 338                sprintf(filename + len, "%04d", nr);
 339                len = strlen(filename);
 340
 341                sol = strstr(commit->buffer, "\n\n");
 342                if (sol) {
 343                        int j, space = 1;
 344
 345                        sol += 2;
 346                        /* strip [PATCH] or [PATCH blabla] */
 347                        if (!keep_subject && !prefixcmp(sol, "[PATCH")) {
 348                                char *eos = strchr(sol + 6, ']');
 349                                if (eos) {
 350                                        while (isspace(*eos))
 351                                                eos++;
 352                                        sol = eos;
 353                                }
 354                        }
 355
 356                        for (j = 0;
 357                             j < FORMAT_PATCH_NAME_MAX - suffix_len - 5 &&
 358                                     len < sizeof(filename) - suffix_len &&
 359                                     sol[j] && sol[j] != '\n';
 360                             j++) {
 361                                if (istitlechar(sol[j])) {
 362                                        if (space) {
 363                                                filename[len++] = '-';
 364                                                space = 0;
 365                                        }
 366                                        filename[len++] = sol[j];
 367                                        if (sol[j] == '.')
 368                                                while (sol[j + 1] == '.')
 369                                                        j++;
 370                                } else
 371                                        space = 1;
 372                        }
 373                        while (filename[len - 1] == '.'
 374                               || filename[len - 1] == '-')
 375                                len--;
 376                        filename[len] = 0;
 377                }
 378                if (len + suffix_len >= sizeof(filename))
 379                        return error("Patch pathname too long");
 380                strcpy(filename + len, fmt_patch_suffix);
 381        }
 382
 383        fprintf(realstdout, "%s\n", filename);
 384        if (freopen(filename, "w", stdout) == NULL)
 385                return error("Cannot open patch file %s",filename);
 386
 387        return 0;
 388}
 389
 390static void get_patch_ids(struct rev_info *rev, struct patch_ids *ids, const char *prefix)
 391{
 392        struct rev_info check_rev;
 393        struct commit *commit;
 394        struct object *o1, *o2;
 395        unsigned flags1, flags2;
 396
 397        if (rev->pending.nr != 2)
 398                die("Need exactly one range.");
 399
 400        o1 = rev->pending.objects[0].item;
 401        flags1 = o1->flags;
 402        o2 = rev->pending.objects[1].item;
 403        flags2 = o2->flags;
 404
 405        if ((flags1 & UNINTERESTING) == (flags2 & UNINTERESTING))
 406                die("Not a range.");
 407
 408        init_patch_ids(ids);
 409
 410        /* given a range a..b get all patch ids for b..a */
 411        init_revisions(&check_rev, prefix);
 412        o1->flags ^= UNINTERESTING;
 413        o2->flags ^= UNINTERESTING;
 414        add_pending_object(&check_rev, o1, "o1");
 415        add_pending_object(&check_rev, o2, "o2");
 416        prepare_revision_walk(&check_rev);
 417
 418        while ((commit = get_revision(&check_rev)) != NULL) {
 419                /* ignore merges */
 420                if (commit->parents && commit->parents->next)
 421                        continue;
 422
 423                add_commit_patch_id(commit, ids);
 424        }
 425
 426        /* reset for next revision walk */
 427        clear_commit_marks((struct commit *)o1,
 428                        SEEN | UNINTERESTING | SHOWN | ADDED);
 429        clear_commit_marks((struct commit *)o2,
 430                        SEEN | UNINTERESTING | SHOWN | ADDED);
 431        o1->flags = flags1;
 432        o2->flags = flags2;
 433}
 434
 435static void gen_message_id(char *dest, unsigned int length, char *base)
 436{
 437        const char *committer = git_committer_info(-1);
 438        const char *email_start = strrchr(committer, '<');
 439        const char *email_end = strrchr(committer, '>');
 440        if(!email_start || !email_end || email_start > email_end - 1)
 441                die("Could not extract email from committer identity.");
 442        snprintf(dest, length, "%s.%lu.git.%.*s", base,
 443                 (unsigned long) time(NULL),
 444                 (int)(email_end - email_start - 1), email_start + 1);
 445}
 446
 447static const char *clean_message_id(const char *msg_id)
 448{
 449        char ch;
 450        const char *a, *z, *m;
 451
 452        m = msg_id;
 453        while ((ch = *m) && (isspace(ch) || (ch == '<')))
 454                m++;
 455        a = m;
 456        z = NULL;
 457        while ((ch = *m)) {
 458                if (!isspace(ch) && (ch != '>'))
 459                        z = m;
 460                m++;
 461        }
 462        if (!z)
 463                die("insane in-reply-to: %s", msg_id);
 464        if (++z == m)
 465                return a;
 466        return xmemdupz(a, z - a);
 467}
 468
 469int cmd_format_patch(int argc, const char **argv, const char *prefix)
 470{
 471        struct commit *commit;
 472        struct commit **list = NULL;
 473        struct rev_info rev;
 474        int nr = 0, total, i, j;
 475        int use_stdout = 0;
 476        int start_number = -1;
 477        int keep_subject = 0;
 478        int numbered_files = 0;         /* _just_ numbers */
 479        int subject_prefix = 0;
 480        int ignore_if_in_upstream = 0;
 481        int thread = 0;
 482        const char *in_reply_to = NULL;
 483        struct patch_ids ids;
 484        char *add_signoff = NULL;
 485        char message_id[1024];
 486        char ref_message_id[1024];
 487
 488        git_config(git_format_config);
 489        init_revisions(&rev, prefix);
 490        rev.commit_format = CMIT_FMT_EMAIL;
 491        rev.verbose_header = 1;
 492        rev.diff = 1;
 493        rev.combine_merges = 0;
 494        rev.ignore_merges = 1;
 495        rev.diffopt.msg_sep = "";
 496        DIFF_OPT_SET(&rev.diffopt, RECURSIVE);
 497
 498        rev.subject_prefix = fmt_patch_subject_prefix;
 499        rev.extra_headers = extra_headers;
 500
 501        /*
 502         * Parse the arguments before setup_revisions(), or something
 503         * like "git format-patch -o a123 HEAD^.." may fail; a123 is
 504         * possibly a valid SHA1.
 505         */
 506        for (i = 1, j = 1; i < argc; i++) {
 507                if (!strcmp(argv[i], "--stdout"))
 508                        use_stdout = 1;
 509                else if (!strcmp(argv[i], "-n") ||
 510                                !strcmp(argv[i], "--numbered"))
 511                        numbered = 1;
 512                else if (!strcmp(argv[i], "-N") ||
 513                                !strcmp(argv[i], "--no-numbered")) {
 514                        numbered = 0;
 515                        auto_number = 0;
 516                }
 517                else if (!prefixcmp(argv[i], "--start-number="))
 518                        start_number = strtol(argv[i] + 15, NULL, 10);
 519                else if (!strcmp(argv[i], "--numbered-files"))
 520                        numbered_files = 1;
 521                else if (!strcmp(argv[i], "--start-number")) {
 522                        i++;
 523                        if (i == argc)
 524                                die("Need a number for --start-number");
 525                        start_number = strtol(argv[i], NULL, 10);
 526                }
 527                else if (!strcmp(argv[i], "-k") ||
 528                                !strcmp(argv[i], "--keep-subject")) {
 529                        keep_subject = 1;
 530                        rev.total = -1;
 531                }
 532                else if (!strcmp(argv[i], "--output-directory") ||
 533                         !strcmp(argv[i], "-o")) {
 534                        i++;
 535                        if (argc <= i)
 536                                die("Which directory?");
 537                        if (output_directory)
 538                                die("Two output directories?");
 539                        output_directory = argv[i];
 540                }
 541                else if (!strcmp(argv[i], "--signoff") ||
 542                         !strcmp(argv[i], "-s")) {
 543                        const char *committer;
 544                        const char *endpos;
 545                        committer = git_committer_info(1);
 546                        endpos = strchr(committer, '>');
 547                        if (!endpos)
 548                                die("bogos committer info %s\n", committer);
 549                        add_signoff = xmemdupz(committer, endpos - committer + 1);
 550                }
 551                else if (!strcmp(argv[i], "--attach")) {
 552                        rev.mime_boundary = git_version_string;
 553                        rev.no_inline = 1;
 554                }
 555                else if (!prefixcmp(argv[i], "--attach=")) {
 556                        rev.mime_boundary = argv[i] + 9;
 557                        rev.no_inline = 1;
 558                }
 559                else if (!strcmp(argv[i], "--inline")) {
 560                        rev.mime_boundary = git_version_string;
 561                        rev.no_inline = 0;
 562                }
 563                else if (!prefixcmp(argv[i], "--inline=")) {
 564                        rev.mime_boundary = argv[i] + 9;
 565                        rev.no_inline = 0;
 566                }
 567                else if (!strcmp(argv[i], "--ignore-if-in-upstream"))
 568                        ignore_if_in_upstream = 1;
 569                else if (!strcmp(argv[i], "--thread"))
 570                        thread = 1;
 571                else if (!prefixcmp(argv[i], "--in-reply-to="))
 572                        in_reply_to = argv[i] + 14;
 573                else if (!strcmp(argv[i], "--in-reply-to")) {
 574                        i++;
 575                        if (i == argc)
 576                                die("Need a Message-Id for --in-reply-to");
 577                        in_reply_to = argv[i];
 578                } else if (!prefixcmp(argv[i], "--subject-prefix=")) {
 579                        subject_prefix = 1;
 580                        rev.subject_prefix = argv[i] + 17;
 581                } else if (!prefixcmp(argv[i], "--suffix="))
 582                        fmt_patch_suffix = argv[i] + 9;
 583                else
 584                        argv[j++] = argv[i];
 585        }
 586        argc = j;
 587
 588        if (start_number < 0)
 589                start_number = 1;
 590        if (numbered && keep_subject)
 591                die ("-n and -k are mutually exclusive.");
 592        if (keep_subject && subject_prefix)
 593                die ("--subject-prefix and -k are mutually exclusive.");
 594        if (numbered_files && use_stdout)
 595                die ("--numbered-files and --stdout are mutually exclusive.");
 596
 597        argc = setup_revisions(argc, argv, &rev, "HEAD");
 598        if (argc > 1)
 599                die ("unrecognized argument: %s", argv[1]);
 600
 601        if (!rev.diffopt.output_format)
 602                rev.diffopt.output_format = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_SUMMARY | DIFF_FORMAT_PATCH;
 603
 604        if (!DIFF_OPT_TST(&rev.diffopt, TEXT))
 605                DIFF_OPT_SET(&rev.diffopt, BINARY);
 606
 607        if (!output_directory && !use_stdout)
 608                output_directory = prefix;
 609
 610        if (output_directory) {
 611                if (use_stdout)
 612                        die("standard output, or directory, which one?");
 613                if (mkdir(output_directory, 0777) < 0 && errno != EEXIST)
 614                        die("Could not create directory %s",
 615                            output_directory);
 616        }
 617
 618        if (rev.pending.nr == 1) {
 619                if (rev.max_count < 0 && !rev.show_root_diff) {
 620                        /*
 621                         * This is traditional behaviour of "git format-patch
 622                         * origin" that prepares what the origin side still
 623                         * does not have.
 624                         */
 625                        rev.pending.objects[0].item->flags |= UNINTERESTING;
 626                        add_head(&rev);
 627                }
 628                /*
 629                 * Otherwise, it is "format-patch -22 HEAD", and/or
 630                 * "format-patch --root HEAD".  The user wants
 631                 * get_revision() to do the usual traversal.
 632                 */
 633        }
 634
 635        if (ignore_if_in_upstream)
 636                get_patch_ids(&rev, &ids, prefix);
 637
 638        if (!use_stdout)
 639                realstdout = xfdopen(xdup(1), "w");
 640
 641        prepare_revision_walk(&rev);
 642        while ((commit = get_revision(&rev)) != NULL) {
 643                /* ignore merges */
 644                if (commit->parents && commit->parents->next)
 645                        continue;
 646
 647                if (ignore_if_in_upstream &&
 648                                has_commit_patch_id(commit, &ids))
 649                        continue;
 650
 651                nr++;
 652                list = xrealloc(list, nr * sizeof(list[0]));
 653                list[nr - 1] = commit;
 654        }
 655        total = nr;
 656        if (!keep_subject && auto_number && total > 1)
 657                numbered = 1;
 658        if (numbered)
 659                rev.total = total + start_number - 1;
 660        rev.add_signoff = add_signoff;
 661        if (in_reply_to)
 662                rev.ref_message_id = clean_message_id(in_reply_to);
 663        while (0 <= --nr) {
 664                int shown;
 665                commit = list[nr];
 666                rev.nr = total - nr + (start_number - 1);
 667                /* Make the second and subsequent mails replies to the first */
 668                if (thread) {
 669                        if (nr == (total - 2)) {
 670                                strncpy(ref_message_id, message_id,
 671                                        sizeof(ref_message_id));
 672                                ref_message_id[sizeof(ref_message_id)-1]='\0';
 673                                rev.ref_message_id = ref_message_id;
 674                        }
 675                        gen_message_id(message_id, sizeof(message_id),
 676                                       sha1_to_hex(commit->object.sha1));
 677                        rev.message_id = message_id;
 678                }
 679                if (!use_stdout)
 680                        if (reopen_stdout(commit, rev.nr, keep_subject,
 681                                          numbered_files))
 682                                die("Failed to create output files");
 683                shown = log_tree_commit(&rev, commit);
 684                free(commit->buffer);
 685                commit->buffer = NULL;
 686
 687                /* We put one extra blank line between formatted
 688                 * patches and this flag is used by log-tree code
 689                 * to see if it needs to emit a LF before showing
 690                 * the log; when using one file per patch, we do
 691                 * not want the extra blank line.
 692                 */
 693                if (!use_stdout)
 694                        rev.shown_one = 0;
 695                if (shown) {
 696                        if (rev.mime_boundary)
 697                                printf("\n--%s%s--\n\n\n",
 698                                       mime_boundary_leader,
 699                                       rev.mime_boundary);
 700                        else
 701                                printf("-- \n%s\n\n", git_version_string);
 702                }
 703                if (!use_stdout)
 704                        fclose(stdout);
 705        }
 706        free(list);
 707        if (ignore_if_in_upstream)
 708                free_patch_ids(&ids);
 709        return 0;
 710}
 711
 712static int add_pending_commit(const char *arg, struct rev_info *revs, int flags)
 713{
 714        unsigned char sha1[20];
 715        if (get_sha1(arg, sha1) == 0) {
 716                struct commit *commit = lookup_commit_reference(sha1);
 717                if (commit) {
 718                        commit->object.flags |= flags;
 719                        add_pending_object(revs, &commit->object, arg);
 720                        return 0;
 721                }
 722        }
 723        return -1;
 724}
 725
 726static const char cherry_usage[] =
 727"git-cherry [-v] <upstream> [<head>] [<limit>]";
 728int cmd_cherry(int argc, const char **argv, const char *prefix)
 729{
 730        struct rev_info revs;
 731        struct patch_ids ids;
 732        struct commit *commit;
 733        struct commit_list *list = NULL;
 734        const char *upstream;
 735        const char *head = "HEAD";
 736        const char *limit = NULL;
 737        int verbose = 0;
 738
 739        if (argc > 1 && !strcmp(argv[1], "-v")) {
 740                verbose = 1;
 741                argc--;
 742                argv++;
 743        }
 744
 745        switch (argc) {
 746        case 4:
 747                limit = argv[3];
 748                /* FALLTHROUGH */
 749        case 3:
 750                head = argv[2];
 751                /* FALLTHROUGH */
 752        case 2:
 753                upstream = argv[1];
 754                break;
 755        default:
 756                usage(cherry_usage);
 757        }
 758
 759        init_revisions(&revs, prefix);
 760        revs.diff = 1;
 761        revs.combine_merges = 0;
 762        revs.ignore_merges = 1;
 763        DIFF_OPT_SET(&revs.diffopt, RECURSIVE);
 764
 765        if (add_pending_commit(head, &revs, 0))
 766                die("Unknown commit %s", head);
 767        if (add_pending_commit(upstream, &revs, UNINTERESTING))
 768                die("Unknown commit %s", upstream);
 769
 770        /* Don't say anything if head and upstream are the same. */
 771        if (revs.pending.nr == 2) {
 772                struct object_array_entry *o = revs.pending.objects;
 773                if (hashcmp(o[0].item->sha1, o[1].item->sha1) == 0)
 774                        return 0;
 775        }
 776
 777        get_patch_ids(&revs, &ids, prefix);
 778
 779        if (limit && add_pending_commit(limit, &revs, UNINTERESTING))
 780                die("Unknown commit %s", limit);
 781
 782        /* reverse the list of commits */
 783        prepare_revision_walk(&revs);
 784        while ((commit = get_revision(&revs)) != NULL) {
 785                /* ignore merges */
 786                if (commit->parents && commit->parents->next)
 787                        continue;
 788
 789                commit_list_insert(commit, &list);
 790        }
 791
 792        while (list) {
 793                char sign = '+';
 794
 795                commit = list->item;
 796                if (has_commit_patch_id(commit, &ids))
 797                        sign = '-';
 798
 799                if (verbose) {
 800                        struct strbuf buf;
 801                        strbuf_init(&buf, 0);
 802                        pretty_print_commit(CMIT_FMT_ONELINE, commit,
 803                                            &buf, 0, NULL, NULL, 0, 0);
 804                        printf("%c %s %s\n", sign,
 805                               sha1_to_hex(commit->object.sha1), buf.buf);
 806                        strbuf_release(&buf);
 807                }
 808                else {
 809                        printf("%c %s\n", sign,
 810                               sha1_to_hex(commit->object.sha1));
 811                }
 812
 813                list = list->next;
 814        }
 815
 816        free_patch_ids(&ids);
 817        return 0;
 818}