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