builtin-log.con commit Make fetch-pack a builtin with an internal API (2d4177c)
   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        rev->diffopt.recursive = 1;
  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 (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(rev.diffopt.color_diff,
 189                                                DIFF_COMMIT),
 190                                        t->tag,
 191                                        diff_get_color(rev.diffopt.color_diff,
 192                                                DIFF_RESET));
 193                        ret = show_object(o->sha1, 1);
 194                        objects[i].item = (struct object *)t->tagged;
 195                        i--;
 196                        break;
 197                }
 198                case OBJ_TREE:
 199                        printf("%stree %s%s\n\n",
 200                                        diff_get_color(rev.diffopt.color_diff,
 201                                                DIFF_COMMIT),
 202                                        name,
 203                                        diff_get_color(rev.diffopt.color_diff,
 204                                                DIFF_RESET));
 205                        read_tree_recursive((struct tree *)o, "", 0, 0, NULL,
 206                                        show_tree_object);
 207                        break;
 208                case OBJ_COMMIT:
 209                        rev.pending.nr = rev.pending.alloc = 0;
 210                        rev.pending.objects = NULL;
 211                        add_object_array(o, name, &rev.pending);
 212                        ret = cmd_log_walk(&rev);
 213                        break;
 214                default:
 215                        ret = error("Unknown type: %d", o->type);
 216                }
 217        }
 218        free(objects);
 219        return ret;
 220}
 221
 222/*
 223 * This is equivalent to "git log -g --abbrev-commit --pretty=oneline"
 224 */
 225int cmd_log_reflog(int argc, const char **argv, const char *prefix)
 226{
 227        struct rev_info rev;
 228
 229        git_config(git_log_config);
 230        init_revisions(&rev, prefix);
 231        init_reflog_walk(&rev.reflog_info);
 232        rev.abbrev_commit = 1;
 233        rev.verbose_header = 1;
 234        cmd_log_init(argc, argv, prefix, &rev);
 235
 236        /*
 237         * This means that we override whatever commit format the user gave
 238         * on the cmd line.  Sad, but cmd_log_init() currently doesn't
 239         * allow us to set a different default.
 240         */
 241        rev.commit_format = CMIT_FMT_ONELINE;
 242        rev.always_show_header = 1;
 243
 244        /*
 245         * We get called through "git reflog", so unlike the other log
 246         * routines, we need to set up our pager manually..
 247         */
 248        setup_pager();
 249
 250        return cmd_log_walk(&rev);
 251}
 252
 253int cmd_log(int argc, const char **argv, const char *prefix)
 254{
 255        struct rev_info rev;
 256
 257        git_config(git_log_config);
 258        init_revisions(&rev, prefix);
 259        rev.always_show_header = 1;
 260        cmd_log_init(argc, argv, prefix, &rev);
 261        return cmd_log_walk(&rev);
 262}
 263
 264/* format-patch */
 265#define FORMAT_PATCH_NAME_MAX 64
 266
 267static int istitlechar(char c)
 268{
 269        return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
 270                (c >= '0' && c <= '9') || c == '.' || c == '_';
 271}
 272
 273static char *extra_headers = NULL;
 274static int extra_headers_size = 0;
 275static const char *fmt_patch_suffix = ".patch";
 276
 277static int git_format_config(const char *var, const char *value)
 278{
 279        if (!strcmp(var, "format.headers")) {
 280                int len;
 281
 282                if (!value)
 283                        die("format.headers without value");
 284                len = strlen(value);
 285                extra_headers_size += len + 1;
 286                extra_headers = xrealloc(extra_headers, extra_headers_size);
 287                extra_headers[extra_headers_size - len - 1] = 0;
 288                strcat(extra_headers, value);
 289                return 0;
 290        }
 291        if (!strcmp(var, "format.suffix")) {
 292                if (!value)
 293                        die("format.suffix without value");
 294                fmt_patch_suffix = xstrdup(value);
 295                return 0;
 296        }
 297        if (!strcmp(var, "diff.color") || !strcmp(var, "color.diff")) {
 298                return 0;
 299        }
 300
 301        return git_log_config(var, value);
 302}
 303
 304
 305static FILE *realstdout = NULL;
 306static const char *output_directory = NULL;
 307
 308static int reopen_stdout(struct commit *commit, int nr, int keep_subject,
 309                         int numbered_files)
 310{
 311        char filename[PATH_MAX];
 312        char *sol;
 313        int len = 0;
 314        int suffix_len = strlen(fmt_patch_suffix) + 1;
 315
 316        if (output_directory) {
 317                if (strlen(output_directory) >=
 318                    sizeof(filename) - FORMAT_PATCH_NAME_MAX - suffix_len)
 319                        return error("name of output directory is too long");
 320                strlcpy(filename, output_directory, sizeof(filename) - suffix_len);
 321                len = strlen(filename);
 322                if (filename[len - 1] != '/')
 323                        filename[len++] = '/';
 324        }
 325
 326        if (numbered_files) {
 327                sprintf(filename + len, "%d", nr);
 328                len = strlen(filename);
 329
 330        } else {
 331                sprintf(filename + len, "%04d", nr);
 332                len = strlen(filename);
 333
 334                sol = strstr(commit->buffer, "\n\n");
 335                if (sol) {
 336                        int j, space = 1;
 337
 338                        sol += 2;
 339                        /* strip [PATCH] or [PATCH blabla] */
 340                        if (!keep_subject && !prefixcmp(sol, "[PATCH")) {
 341                                char *eos = strchr(sol + 6, ']');
 342                                if (eos) {
 343                                        while (isspace(*eos))
 344                                                eos++;
 345                                        sol = eos;
 346                                }
 347                        }
 348
 349                        for (j = 0;
 350                             j < FORMAT_PATCH_NAME_MAX - suffix_len - 5 &&
 351                                     len < sizeof(filename) - suffix_len &&
 352                                     sol[j] && sol[j] != '\n';
 353                             j++) {
 354                                if (istitlechar(sol[j])) {
 355                                        if (space) {
 356                                                filename[len++] = '-';
 357                                                space = 0;
 358                                        }
 359                                        filename[len++] = sol[j];
 360                                        if (sol[j] == '.')
 361                                                while (sol[j + 1] == '.')
 362                                                        j++;
 363                                } else
 364                                        space = 1;
 365                        }
 366                        while (filename[len - 1] == '.'
 367                               || filename[len - 1] == '-')
 368                                len--;
 369                        filename[len] = 0;
 370                }
 371                if (len + suffix_len >= sizeof(filename))
 372                        return error("Patch pathname too long");
 373                strcpy(filename + len, fmt_patch_suffix);
 374        }
 375
 376        fprintf(realstdout, "%s\n", filename);
 377        if (freopen(filename, "w", stdout) == NULL)
 378                return error("Cannot open patch file %s",filename);
 379
 380        return 0;
 381}
 382
 383static void get_patch_ids(struct rev_info *rev, struct patch_ids *ids, const char *prefix)
 384{
 385        struct rev_info check_rev;
 386        struct commit *commit;
 387        struct object *o1, *o2;
 388        unsigned flags1, flags2;
 389
 390        if (rev->pending.nr != 2)
 391                die("Need exactly one range.");
 392
 393        o1 = rev->pending.objects[0].item;
 394        flags1 = o1->flags;
 395        o2 = rev->pending.objects[1].item;
 396        flags2 = o2->flags;
 397
 398        if ((flags1 & UNINTERESTING) == (flags2 & UNINTERESTING))
 399                die("Not a range.");
 400
 401        init_patch_ids(ids);
 402
 403        /* given a range a..b get all patch ids for b..a */
 404        init_revisions(&check_rev, prefix);
 405        o1->flags ^= UNINTERESTING;
 406        o2->flags ^= UNINTERESTING;
 407        add_pending_object(&check_rev, o1, "o1");
 408        add_pending_object(&check_rev, o2, "o2");
 409        prepare_revision_walk(&check_rev);
 410
 411        while ((commit = get_revision(&check_rev)) != NULL) {
 412                /* ignore merges */
 413                if (commit->parents && commit->parents->next)
 414                        continue;
 415
 416                add_commit_patch_id(commit, ids);
 417        }
 418
 419        /* reset for next revision walk */
 420        clear_commit_marks((struct commit *)o1,
 421                        SEEN | UNINTERESTING | SHOWN | ADDED);
 422        clear_commit_marks((struct commit *)o2,
 423                        SEEN | UNINTERESTING | SHOWN | ADDED);
 424        o1->flags = flags1;
 425        o2->flags = flags2;
 426}
 427
 428static void gen_message_id(char *dest, unsigned int length, char *base)
 429{
 430        const char *committer = git_committer_info(-1);
 431        const char *email_start = strrchr(committer, '<');
 432        const char *email_end = strrchr(committer, '>');
 433        if(!email_start || !email_end || email_start > email_end - 1)
 434                die("Could not extract email from committer identity.");
 435        snprintf(dest, length, "%s.%lu.git.%.*s", base,
 436                 (unsigned long) time(NULL),
 437                 (int)(email_end - email_start - 1), email_start + 1);
 438}
 439
 440static const char *clean_message_id(const char *msg_id)
 441{
 442        char ch;
 443        const char *a, *z, *m;
 444        char *n;
 445        size_t len;
 446
 447        m = msg_id;
 448        while ((ch = *m) && (isspace(ch) || (ch == '<')))
 449                m++;
 450        a = m;
 451        z = NULL;
 452        while ((ch = *m)) {
 453                if (!isspace(ch) && (ch != '>'))
 454                        z = m;
 455                m++;
 456        }
 457        if (!z)
 458                die("insane in-reply-to: %s", msg_id);
 459        if (++z == m)
 460                return a;
 461        len = z - a;
 462        n = xmalloc(len + 1);
 463        memcpy(n, a, len);
 464        n[len] = 0;
 465        return n;
 466}
 467
 468int cmd_format_patch(int argc, const char **argv, const char *prefix)
 469{
 470        struct commit *commit;
 471        struct commit **list = NULL;
 472        struct rev_info rev;
 473        int nr = 0, total, i, j;
 474        int use_stdout = 0;
 475        int numbered = 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        rev.diffopt.recursive = 1;
 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 (!prefixcmp(argv[i], "--start-number="))
 513                        start_number = strtol(argv[i] + 15, NULL, 10);
 514                else if (!strcmp(argv[i], "--numbered-files"))
 515                        numbered_files = 1;
 516                else if (!strcmp(argv[i], "--start-number")) {
 517                        i++;
 518                        if (i == argc)
 519                                die("Need a number for --start-number");
 520                        start_number = strtol(argv[i], NULL, 10);
 521                }
 522                else if (!strcmp(argv[i], "-k") ||
 523                                !strcmp(argv[i], "--keep-subject")) {
 524                        keep_subject = 1;
 525                        rev.total = -1;
 526                }
 527                else if (!strcmp(argv[i], "--output-directory") ||
 528                         !strcmp(argv[i], "-o")) {
 529                        i++;
 530                        if (argc <= i)
 531                                die("Which directory?");
 532                        if (output_directory)
 533                                die("Two output directories?");
 534                        output_directory = argv[i];
 535                }
 536                else if (!strcmp(argv[i], "--signoff") ||
 537                         !strcmp(argv[i], "-s")) {
 538                        const char *committer;
 539                        const char *endpos;
 540                        committer = git_committer_info(1);
 541                        endpos = strchr(committer, '>');
 542                        if (!endpos)
 543                                die("bogos committer info %s\n", committer);
 544                        add_signoff = xmalloc(endpos - committer + 2);
 545                        memcpy(add_signoff, committer, endpos - committer + 1);
 546                        add_signoff[endpos - committer + 1] = 0;
 547                }
 548                else if (!strcmp(argv[i], "--attach")) {
 549                        rev.mime_boundary = git_version_string;
 550                        rev.no_inline = 1;
 551                }
 552                else if (!prefixcmp(argv[i], "--attach=")) {
 553                        rev.mime_boundary = argv[i] + 9;
 554                        rev.no_inline = 1;
 555                }
 556                else if (!strcmp(argv[i], "--inline")) {
 557                        rev.mime_boundary = git_version_string;
 558                        rev.no_inline = 0;
 559                }
 560                else if (!prefixcmp(argv[i], "--inline=")) {
 561                        rev.mime_boundary = argv[i] + 9;
 562                        rev.no_inline = 0;
 563                }
 564                else if (!strcmp(argv[i], "--ignore-if-in-upstream"))
 565                        ignore_if_in_upstream = 1;
 566                else if (!strcmp(argv[i], "--thread"))
 567                        thread = 1;
 568                else if (!prefixcmp(argv[i], "--in-reply-to="))
 569                        in_reply_to = argv[i] + 14;
 570                else if (!strcmp(argv[i], "--in-reply-to")) {
 571                        i++;
 572                        if (i == argc)
 573                                die("Need a Message-Id for --in-reply-to");
 574                        in_reply_to = argv[i];
 575                } else if (!prefixcmp(argv[i], "--subject-prefix=")) {
 576                        subject_prefix = 1;
 577                        rev.subject_prefix = argv[i] + 17;
 578                } else if (!prefixcmp(argv[i], "--suffix="))
 579                        fmt_patch_suffix = argv[i] + 9;
 580                else
 581                        argv[j++] = argv[i];
 582        }
 583        argc = j;
 584
 585        if (start_number < 0)
 586                start_number = 1;
 587        if (numbered && keep_subject)
 588                die ("-n and -k are mutually exclusive.");
 589        if (keep_subject && subject_prefix)
 590                die ("--subject-prefix and -k are mutually exclusive.");
 591        if (numbered_files && use_stdout)
 592                die ("--numbered-files and --stdout are mutually exclusive.");
 593
 594        argc = setup_revisions(argc, argv, &rev, "HEAD");
 595        if (argc > 1)
 596                die ("unrecognized argument: %s", argv[1]);
 597
 598        if (!rev.diffopt.output_format)
 599                rev.diffopt.output_format = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_SUMMARY | DIFF_FORMAT_PATCH;
 600
 601        if (!rev.diffopt.text)
 602                rev.diffopt.binary = 1;
 603
 604        if (!output_directory && !use_stdout)
 605                output_directory = prefix;
 606
 607        if (output_directory) {
 608                if (use_stdout)
 609                        die("standard output, or directory, which one?");
 610                if (mkdir(output_directory, 0777) < 0 && errno != EEXIST)
 611                        die("Could not create directory %s",
 612                            output_directory);
 613        }
 614
 615        if (rev.pending.nr == 1) {
 616                if (rev.max_count < 0 && !rev.show_root_diff) {
 617                        /*
 618                         * This is traditional behaviour of "git format-patch
 619                         * origin" that prepares what the origin side still
 620                         * does not have.
 621                         */
 622                        rev.pending.objects[0].item->flags |= UNINTERESTING;
 623                        add_head(&rev);
 624                }
 625                /*
 626                 * Otherwise, it is "format-patch -22 HEAD", and/or
 627                 * "format-patch --root HEAD".  The user wants
 628                 * get_revision() to do the usual traversal.
 629                 */
 630        }
 631
 632        if (ignore_if_in_upstream)
 633                get_patch_ids(&rev, &ids, prefix);
 634
 635        if (!use_stdout)
 636                realstdout = xfdopen(xdup(1), "w");
 637
 638        prepare_revision_walk(&rev);
 639        while ((commit = get_revision(&rev)) != NULL) {
 640                /* ignore merges */
 641                if (commit->parents && commit->parents->next)
 642                        continue;
 643
 644                if (ignore_if_in_upstream &&
 645                                has_commit_patch_id(commit, &ids))
 646                        continue;
 647
 648                nr++;
 649                list = xrealloc(list, nr * sizeof(list[0]));
 650                list[nr - 1] = commit;
 651        }
 652        total = nr;
 653        if (numbered)
 654                rev.total = total + start_number - 1;
 655        rev.add_signoff = add_signoff;
 656        if (in_reply_to)
 657                rev.ref_message_id = clean_message_id(in_reply_to);
 658        while (0 <= --nr) {
 659                int shown;
 660                commit = list[nr];
 661                rev.nr = total - nr + (start_number - 1);
 662                /* Make the second and subsequent mails replies to the first */
 663                if (thread) {
 664                        if (nr == (total - 2)) {
 665                                strncpy(ref_message_id, message_id,
 666                                        sizeof(ref_message_id));
 667                                ref_message_id[sizeof(ref_message_id)-1]='\0';
 668                                rev.ref_message_id = ref_message_id;
 669                        }
 670                        gen_message_id(message_id, sizeof(message_id),
 671                                       sha1_to_hex(commit->object.sha1));
 672                        rev.message_id = message_id;
 673                }
 674                if (!use_stdout)
 675                        if (reopen_stdout(commit, rev.nr, keep_subject,
 676                                          numbered_files))
 677                                die("Failed to create output files");
 678                shown = log_tree_commit(&rev, commit);
 679                free(commit->buffer);
 680                commit->buffer = NULL;
 681
 682                /* We put one extra blank line between formatted
 683                 * patches and this flag is used by log-tree code
 684                 * to see if it needs to emit a LF before showing
 685                 * the log; when using one file per patch, we do
 686                 * not want the extra blank line.
 687                 */
 688                if (!use_stdout)
 689                        rev.shown_one = 0;
 690                if (shown) {
 691                        if (rev.mime_boundary)
 692                                printf("\n--%s%s--\n\n\n",
 693                                       mime_boundary_leader,
 694                                       rev.mime_boundary);
 695                        else
 696                                printf("-- \n%s\n\n", git_version_string);
 697                }
 698                if (!use_stdout)
 699                        fclose(stdout);
 700        }
 701        free(list);
 702        if (ignore_if_in_upstream)
 703                free_patch_ids(&ids);
 704        return 0;
 705}
 706
 707static int add_pending_commit(const char *arg, struct rev_info *revs, int flags)
 708{
 709        unsigned char sha1[20];
 710        if (get_sha1(arg, sha1) == 0) {
 711                struct commit *commit = lookup_commit_reference(sha1);
 712                if (commit) {
 713                        commit->object.flags |= flags;
 714                        add_pending_object(revs, &commit->object, arg);
 715                        return 0;
 716                }
 717        }
 718        return -1;
 719}
 720
 721static const char cherry_usage[] =
 722"git-cherry [-v] <upstream> [<head>] [<limit>]";
 723int cmd_cherry(int argc, const char **argv, const char *prefix)
 724{
 725        struct rev_info revs;
 726        struct patch_ids ids;
 727        struct commit *commit;
 728        struct commit_list *list = NULL;
 729        const char *upstream;
 730        const char *head = "HEAD";
 731        const char *limit = NULL;
 732        int verbose = 0;
 733
 734        if (argc > 1 && !strcmp(argv[1], "-v")) {
 735                verbose = 1;
 736                argc--;
 737                argv++;
 738        }
 739
 740        switch (argc) {
 741        case 4:
 742                limit = argv[3];
 743                /* FALLTHROUGH */
 744        case 3:
 745                head = argv[2];
 746                /* FALLTHROUGH */
 747        case 2:
 748                upstream = argv[1];
 749                break;
 750        default:
 751                usage(cherry_usage);
 752        }
 753
 754        init_revisions(&revs, prefix);
 755        revs.diff = 1;
 756        revs.combine_merges = 0;
 757        revs.ignore_merges = 1;
 758        revs.diffopt.recursive = 1;
 759
 760        if (add_pending_commit(head, &revs, 0))
 761                die("Unknown commit %s", head);
 762        if (add_pending_commit(upstream, &revs, UNINTERESTING))
 763                die("Unknown commit %s", upstream);
 764
 765        /* Don't say anything if head and upstream are the same. */
 766        if (revs.pending.nr == 2) {
 767                struct object_array_entry *o = revs.pending.objects;
 768                if (hashcmp(o[0].item->sha1, o[1].item->sha1) == 0)
 769                        return 0;
 770        }
 771
 772        get_patch_ids(&revs, &ids, prefix);
 773
 774        if (limit && add_pending_commit(limit, &revs, UNINTERESTING))
 775                die("Unknown commit %s", limit);
 776
 777        /* reverse the list of commits */
 778        prepare_revision_walk(&revs);
 779        while ((commit = get_revision(&revs)) != NULL) {
 780                /* ignore merges */
 781                if (commit->parents && commit->parents->next)
 782                        continue;
 783
 784                commit_list_insert(commit, &list);
 785        }
 786
 787        while (list) {
 788                char sign = '+';
 789
 790                commit = list->item;
 791                if (has_commit_patch_id(commit, &ids))
 792                        sign = '-';
 793
 794                if (verbose) {
 795                        char *buf = NULL;
 796                        unsigned long buflen = 0;
 797                        pretty_print_commit(CMIT_FMT_ONELINE, commit, ~0,
 798                                            &buf, &buflen, 0, NULL, NULL, 0);
 799                        printf("%c %s %s\n", sign,
 800                               sha1_to_hex(commit->object.sha1), buf);
 801                        free(buf);
 802                }
 803                else {
 804                        printf("%c %s\n", sign,
 805                               sha1_to_hex(commit->object.sha1));
 806                }
 807
 808                list = list->next;
 809        }
 810
 811        free_patch_ids(&ids);
 812        return 0;
 813}