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