builtin-log.con commit Make the diff_options bitfields be an unsigned with explicit masks. (8f67f8a)
   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";
 272
 273static int git_format_config(const char *var, const char *value)
 274{
 275        if (!strcmp(var, "format.headers")) {
 276                int len;
 277
 278                if (!value)
 279                        die("format.headers without value");
 280                len = strlen(value);
 281                extra_headers_size += len + 1;
 282                extra_headers = xrealloc(extra_headers, extra_headers_size);
 283                extra_headers[extra_headers_size - len - 1] = 0;
 284                strcat(extra_headers, value);
 285                return 0;
 286        }
 287        if (!strcmp(var, "format.suffix")) {
 288                if (!value)
 289                        die("format.suffix without value");
 290                fmt_patch_suffix = xstrdup(value);
 291                return 0;
 292        }
 293        if (!strcmp(var, "diff.color") || !strcmp(var, "color.diff")) {
 294                return 0;
 295        }
 296
 297        return git_log_config(var, value);
 298}
 299
 300
 301static FILE *realstdout = NULL;
 302static const char *output_directory = NULL;
 303
 304static int reopen_stdout(struct commit *commit, int nr, int keep_subject,
 305                         int numbered_files)
 306{
 307        char filename[PATH_MAX];
 308        char *sol;
 309        int len = 0;
 310        int suffix_len = strlen(fmt_patch_suffix) + 1;
 311
 312        if (output_directory) {
 313                if (strlen(output_directory) >=
 314                    sizeof(filename) - FORMAT_PATCH_NAME_MAX - suffix_len)
 315                        return error("name of output directory is too long");
 316                strlcpy(filename, output_directory, sizeof(filename) - suffix_len);
 317                len = strlen(filename);
 318                if (filename[len - 1] != '/')
 319                        filename[len++] = '/';
 320        }
 321
 322        if (numbered_files) {
 323                sprintf(filename + len, "%d", nr);
 324                len = strlen(filename);
 325
 326        } else {
 327                sprintf(filename + len, "%04d", nr);
 328                len = strlen(filename);
 329
 330                sol = strstr(commit->buffer, "\n\n");
 331                if (sol) {
 332                        int j, space = 1;
 333
 334                        sol += 2;
 335                        /* strip [PATCH] or [PATCH blabla] */
 336                        if (!keep_subject && !prefixcmp(sol, "[PATCH")) {
 337                                char *eos = strchr(sol + 6, ']');
 338                                if (eos) {
 339                                        while (isspace(*eos))
 340                                                eos++;
 341                                        sol = eos;
 342                                }
 343                        }
 344
 345                        for (j = 0;
 346                             j < FORMAT_PATCH_NAME_MAX - suffix_len - 5 &&
 347                                     len < sizeof(filename) - suffix_len &&
 348                                     sol[j] && sol[j] != '\n';
 349                             j++) {
 350                                if (istitlechar(sol[j])) {
 351                                        if (space) {
 352                                                filename[len++] = '-';
 353                                                space = 0;
 354                                        }
 355                                        filename[len++] = sol[j];
 356                                        if (sol[j] == '.')
 357                                                while (sol[j + 1] == '.')
 358                                                        j++;
 359                                } else
 360                                        space = 1;
 361                        }
 362                        while (filename[len - 1] == '.'
 363                               || filename[len - 1] == '-')
 364                                len--;
 365                        filename[len] = 0;
 366                }
 367                if (len + suffix_len >= sizeof(filename))
 368                        return error("Patch pathname too long");
 369                strcpy(filename + len, fmt_patch_suffix);
 370        }
 371
 372        fprintf(realstdout, "%s\n", filename);
 373        if (freopen(filename, "w", stdout) == NULL)
 374                return error("Cannot open patch file %s",filename);
 375
 376        return 0;
 377}
 378
 379static void get_patch_ids(struct rev_info *rev, struct patch_ids *ids, const char *prefix)
 380{
 381        struct rev_info check_rev;
 382        struct commit *commit;
 383        struct object *o1, *o2;
 384        unsigned flags1, flags2;
 385
 386        if (rev->pending.nr != 2)
 387                die("Need exactly one range.");
 388
 389        o1 = rev->pending.objects[0].item;
 390        flags1 = o1->flags;
 391        o2 = rev->pending.objects[1].item;
 392        flags2 = o2->flags;
 393
 394        if ((flags1 & UNINTERESTING) == (flags2 & UNINTERESTING))
 395                die("Not a range.");
 396
 397        init_patch_ids(ids);
 398
 399        /* given a range a..b get all patch ids for b..a */
 400        init_revisions(&check_rev, prefix);
 401        o1->flags ^= UNINTERESTING;
 402        o2->flags ^= UNINTERESTING;
 403        add_pending_object(&check_rev, o1, "o1");
 404        add_pending_object(&check_rev, o2, "o2");
 405        prepare_revision_walk(&check_rev);
 406
 407        while ((commit = get_revision(&check_rev)) != NULL) {
 408                /* ignore merges */
 409                if (commit->parents && commit->parents->next)
 410                        continue;
 411
 412                add_commit_patch_id(commit, ids);
 413        }
 414
 415        /* reset for next revision walk */
 416        clear_commit_marks((struct commit *)o1,
 417                        SEEN | UNINTERESTING | SHOWN | ADDED);
 418        clear_commit_marks((struct commit *)o2,
 419                        SEEN | UNINTERESTING | SHOWN | ADDED);
 420        o1->flags = flags1;
 421        o2->flags = flags2;
 422}
 423
 424static void gen_message_id(char *dest, unsigned int length, char *base)
 425{
 426        const char *committer = git_committer_info(-1);
 427        const char *email_start = strrchr(committer, '<');
 428        const char *email_end = strrchr(committer, '>');
 429        if(!email_start || !email_end || email_start > email_end - 1)
 430                die("Could not extract email from committer identity.");
 431        snprintf(dest, length, "%s.%lu.git.%.*s", base,
 432                 (unsigned long) time(NULL),
 433                 (int)(email_end - email_start - 1), email_start + 1);
 434}
 435
 436static const char *clean_message_id(const char *msg_id)
 437{
 438        char ch;
 439        const char *a, *z, *m;
 440
 441        m = msg_id;
 442        while ((ch = *m) && (isspace(ch) || (ch == '<')))
 443                m++;
 444        a = m;
 445        z = NULL;
 446        while ((ch = *m)) {
 447                if (!isspace(ch) && (ch != '>'))
 448                        z = m;
 449                m++;
 450        }
 451        if (!z)
 452                die("insane in-reply-to: %s", msg_id);
 453        if (++z == m)
 454                return a;
 455        return xmemdupz(a, z - a);
 456}
 457
 458int cmd_format_patch(int argc, const char **argv, const char *prefix)
 459{
 460        struct commit *commit;
 461        struct commit **list = NULL;
 462        struct rev_info rev;
 463        int nr = 0, total, i, j;
 464        int use_stdout = 0;
 465        int numbered = 0;
 466        int start_number = -1;
 467        int keep_subject = 0;
 468        int numbered_files = 0;         /* _just_ numbers */
 469        int subject_prefix = 0;
 470        int ignore_if_in_upstream = 0;
 471        int thread = 0;
 472        const char *in_reply_to = NULL;
 473        struct patch_ids ids;
 474        char *add_signoff = NULL;
 475        char message_id[1024];
 476        char ref_message_id[1024];
 477
 478        git_config(git_format_config);
 479        init_revisions(&rev, prefix);
 480        rev.commit_format = CMIT_FMT_EMAIL;
 481        rev.verbose_header = 1;
 482        rev.diff = 1;
 483        rev.combine_merges = 0;
 484        rev.ignore_merges = 1;
 485        rev.diffopt.msg_sep = "";
 486        DIFF_OPT_SET(&rev.diffopt, RECURSIVE);
 487
 488        rev.subject_prefix = fmt_patch_subject_prefix;
 489        rev.extra_headers = extra_headers;
 490
 491        /*
 492         * Parse the arguments before setup_revisions(), or something
 493         * like "git format-patch -o a123 HEAD^.." may fail; a123 is
 494         * possibly a valid SHA1.
 495         */
 496        for (i = 1, j = 1; i < argc; i++) {
 497                if (!strcmp(argv[i], "--stdout"))
 498                        use_stdout = 1;
 499                else if (!strcmp(argv[i], "-n") ||
 500                                !strcmp(argv[i], "--numbered"))
 501                        numbered = 1;
 502                else if (!prefixcmp(argv[i], "--start-number="))
 503                        start_number = strtol(argv[i] + 15, NULL, 10);
 504                else if (!strcmp(argv[i], "--numbered-files"))
 505                        numbered_files = 1;
 506                else if (!strcmp(argv[i], "--start-number")) {
 507                        i++;
 508                        if (i == argc)
 509                                die("Need a number for --start-number");
 510                        start_number = strtol(argv[i], NULL, 10);
 511                }
 512                else if (!strcmp(argv[i], "-k") ||
 513                                !strcmp(argv[i], "--keep-subject")) {
 514                        keep_subject = 1;
 515                        rev.total = -1;
 516                }
 517                else if (!strcmp(argv[i], "--output-directory") ||
 518                         !strcmp(argv[i], "-o")) {
 519                        i++;
 520                        if (argc <= i)
 521                                die("Which directory?");
 522                        if (output_directory)
 523                                die("Two output directories?");
 524                        output_directory = argv[i];
 525                }
 526                else if (!strcmp(argv[i], "--signoff") ||
 527                         !strcmp(argv[i], "-s")) {
 528                        const char *committer;
 529                        const char *endpos;
 530                        committer = git_committer_info(1);
 531                        endpos = strchr(committer, '>');
 532                        if (!endpos)
 533                                die("bogos committer info %s\n", committer);
 534                        add_signoff = xmemdupz(committer, endpos - committer + 1);
 535                }
 536                else if (!strcmp(argv[i], "--attach")) {
 537                        rev.mime_boundary = git_version_string;
 538                        rev.no_inline = 1;
 539                }
 540                else if (!prefixcmp(argv[i], "--attach=")) {
 541                        rev.mime_boundary = argv[i] + 9;
 542                        rev.no_inline = 1;
 543                }
 544                else if (!strcmp(argv[i], "--inline")) {
 545                        rev.mime_boundary = git_version_string;
 546                        rev.no_inline = 0;
 547                }
 548                else if (!prefixcmp(argv[i], "--inline=")) {
 549                        rev.mime_boundary = argv[i] + 9;
 550                        rev.no_inline = 0;
 551                }
 552                else if (!strcmp(argv[i], "--ignore-if-in-upstream"))
 553                        ignore_if_in_upstream = 1;
 554                else if (!strcmp(argv[i], "--thread"))
 555                        thread = 1;
 556                else if (!prefixcmp(argv[i], "--in-reply-to="))
 557                        in_reply_to = argv[i] + 14;
 558                else if (!strcmp(argv[i], "--in-reply-to")) {
 559                        i++;
 560                        if (i == argc)
 561                                die("Need a Message-Id for --in-reply-to");
 562                        in_reply_to = argv[i];
 563                } else if (!prefixcmp(argv[i], "--subject-prefix=")) {
 564                        subject_prefix = 1;
 565                        rev.subject_prefix = argv[i] + 17;
 566                } else if (!prefixcmp(argv[i], "--suffix="))
 567                        fmt_patch_suffix = argv[i] + 9;
 568                else
 569                        argv[j++] = argv[i];
 570        }
 571        argc = j;
 572
 573        if (start_number < 0)
 574                start_number = 1;
 575        if (numbered && keep_subject)
 576                die ("-n and -k are mutually exclusive.");
 577        if (keep_subject && subject_prefix)
 578                die ("--subject-prefix and -k are mutually exclusive.");
 579        if (numbered_files && use_stdout)
 580                die ("--numbered-files and --stdout are mutually exclusive.");
 581
 582        argc = setup_revisions(argc, argv, &rev, "HEAD");
 583        if (argc > 1)
 584                die ("unrecognized argument: %s", argv[1]);
 585
 586        if (!rev.diffopt.output_format)
 587                rev.diffopt.output_format = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_SUMMARY | DIFF_FORMAT_PATCH;
 588
 589        if (!DIFF_OPT_TST(&rev.diffopt, TEXT))
 590                DIFF_OPT_SET(&rev.diffopt, BINARY);
 591
 592        if (!output_directory && !use_stdout)
 593                output_directory = prefix;
 594
 595        if (output_directory) {
 596                if (use_stdout)
 597                        die("standard output, or directory, which one?");
 598                if (mkdir(output_directory, 0777) < 0 && errno != EEXIST)
 599                        die("Could not create directory %s",
 600                            output_directory);
 601        }
 602
 603        if (rev.pending.nr == 1) {
 604                if (rev.max_count < 0 && !rev.show_root_diff) {
 605                        /*
 606                         * This is traditional behaviour of "git format-patch
 607                         * origin" that prepares what the origin side still
 608                         * does not have.
 609                         */
 610                        rev.pending.objects[0].item->flags |= UNINTERESTING;
 611                        add_head(&rev);
 612                }
 613                /*
 614                 * Otherwise, it is "format-patch -22 HEAD", and/or
 615                 * "format-patch --root HEAD".  The user wants
 616                 * get_revision() to do the usual traversal.
 617                 */
 618        }
 619
 620        if (ignore_if_in_upstream)
 621                get_patch_ids(&rev, &ids, prefix);
 622
 623        if (!use_stdout)
 624                realstdout = xfdopen(xdup(1), "w");
 625
 626        prepare_revision_walk(&rev);
 627        while ((commit = get_revision(&rev)) != NULL) {
 628                /* ignore merges */
 629                if (commit->parents && commit->parents->next)
 630                        continue;
 631
 632                if (ignore_if_in_upstream &&
 633                                has_commit_patch_id(commit, &ids))
 634                        continue;
 635
 636                nr++;
 637                list = xrealloc(list, nr * sizeof(list[0]));
 638                list[nr - 1] = commit;
 639        }
 640        total = nr;
 641        if (numbered)
 642                rev.total = total + start_number - 1;
 643        rev.add_signoff = add_signoff;
 644        if (in_reply_to)
 645                rev.ref_message_id = clean_message_id(in_reply_to);
 646        while (0 <= --nr) {
 647                int shown;
 648                commit = list[nr];
 649                rev.nr = total - nr + (start_number - 1);
 650                /* Make the second and subsequent mails replies to the first */
 651                if (thread) {
 652                        if (nr == (total - 2)) {
 653                                strncpy(ref_message_id, message_id,
 654                                        sizeof(ref_message_id));
 655                                ref_message_id[sizeof(ref_message_id)-1]='\0';
 656                                rev.ref_message_id = ref_message_id;
 657                        }
 658                        gen_message_id(message_id, sizeof(message_id),
 659                                       sha1_to_hex(commit->object.sha1));
 660                        rev.message_id = message_id;
 661                }
 662                if (!use_stdout)
 663                        if (reopen_stdout(commit, rev.nr, keep_subject,
 664                                          numbered_files))
 665                                die("Failed to create output files");
 666                shown = log_tree_commit(&rev, commit);
 667                free(commit->buffer);
 668                commit->buffer = NULL;
 669
 670                /* We put one extra blank line between formatted
 671                 * patches and this flag is used by log-tree code
 672                 * to see if it needs to emit a LF before showing
 673                 * the log; when using one file per patch, we do
 674                 * not want the extra blank line.
 675                 */
 676                if (!use_stdout)
 677                        rev.shown_one = 0;
 678                if (shown) {
 679                        if (rev.mime_boundary)
 680                                printf("\n--%s%s--\n\n\n",
 681                                       mime_boundary_leader,
 682                                       rev.mime_boundary);
 683                        else
 684                                printf("-- \n%s\n\n", git_version_string);
 685                }
 686                if (!use_stdout)
 687                        fclose(stdout);
 688        }
 689        free(list);
 690        if (ignore_if_in_upstream)
 691                free_patch_ids(&ids);
 692        return 0;
 693}
 694
 695static int add_pending_commit(const char *arg, struct rev_info *revs, int flags)
 696{
 697        unsigned char sha1[20];
 698        if (get_sha1(arg, sha1) == 0) {
 699                struct commit *commit = lookup_commit_reference(sha1);
 700                if (commit) {
 701                        commit->object.flags |= flags;
 702                        add_pending_object(revs, &commit->object, arg);
 703                        return 0;
 704                }
 705        }
 706        return -1;
 707}
 708
 709static const char cherry_usage[] =
 710"git-cherry [-v] <upstream> [<head>] [<limit>]";
 711int cmd_cherry(int argc, const char **argv, const char *prefix)
 712{
 713        struct rev_info revs;
 714        struct patch_ids ids;
 715        struct commit *commit;
 716        struct commit_list *list = NULL;
 717        const char *upstream;
 718        const char *head = "HEAD";
 719        const char *limit = NULL;
 720        int verbose = 0;
 721
 722        if (argc > 1 && !strcmp(argv[1], "-v")) {
 723                verbose = 1;
 724                argc--;
 725                argv++;
 726        }
 727
 728        switch (argc) {
 729        case 4:
 730                limit = argv[3];
 731                /* FALLTHROUGH */
 732        case 3:
 733                head = argv[2];
 734                /* FALLTHROUGH */
 735        case 2:
 736                upstream = argv[1];
 737                break;
 738        default:
 739                usage(cherry_usage);
 740        }
 741
 742        init_revisions(&revs, prefix);
 743        revs.diff = 1;
 744        revs.combine_merges = 0;
 745        revs.ignore_merges = 1;
 746        DIFF_OPT_SET(&revs.diffopt, RECURSIVE);
 747
 748        if (add_pending_commit(head, &revs, 0))
 749                die("Unknown commit %s", head);
 750        if (add_pending_commit(upstream, &revs, UNINTERESTING))
 751                die("Unknown commit %s", upstream);
 752
 753        /* Don't say anything if head and upstream are the same. */
 754        if (revs.pending.nr == 2) {
 755                struct object_array_entry *o = revs.pending.objects;
 756                if (hashcmp(o[0].item->sha1, o[1].item->sha1) == 0)
 757                        return 0;
 758        }
 759
 760        get_patch_ids(&revs, &ids, prefix);
 761
 762        if (limit && add_pending_commit(limit, &revs, UNINTERESTING))
 763                die("Unknown commit %s", limit);
 764
 765        /* reverse the list of commits */
 766        prepare_revision_walk(&revs);
 767        while ((commit = get_revision(&revs)) != NULL) {
 768                /* ignore merges */
 769                if (commit->parents && commit->parents->next)
 770                        continue;
 771
 772                commit_list_insert(commit, &list);
 773        }
 774
 775        while (list) {
 776                char sign = '+';
 777
 778                commit = list->item;
 779                if (has_commit_patch_id(commit, &ids))
 780                        sign = '-';
 781
 782                if (verbose) {
 783                        struct strbuf buf;
 784                        strbuf_init(&buf, 0);
 785                        pretty_print_commit(CMIT_FMT_ONELINE, commit,
 786                                            &buf, 0, NULL, NULL, 0, 0);
 787                        printf("%c %s %s\n", sign,
 788                               sha1_to_hex(commit->object.sha1), buf.buf);
 789                        strbuf_release(&buf);
 790                }
 791                else {
 792                        printf("%c %s\n", sign,
 793                               sha1_to_hex(commit->object.sha1));
 794                }
 795
 796                list = list->next;
 797        }
 798
 799        free_patch_ids(&ids);
 800        return 0;
 801}