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