log: add log.mailmap configuration option
[git/jnareb-git.git] / builtin / log.c
blob16e6520a5d40f1837d850956f899bb5ef5a4c46d
1 /*
2 * Builtin "git log" and related commands (show, whatchanged)
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 #include "branch.h"
23 #include "streaming.h"
24 #include "version.h"
25 #include "mailmap.h"
27 /* Set a default date-time format for git log ("log.date" config variable) */
28 static const char *default_date_mode = NULL;
30 static int default_abbrev_commit;
31 static int default_show_root = 1;
32 static int decoration_style;
33 static int decoration_given;
34 static int use_mailmap_config;
35 static const char *fmt_patch_subject_prefix = "PATCH";
36 static const char *fmt_pretty;
38 static const char * const builtin_log_usage[] = {
39 N_("git log [<options>] [<since>..<until>] [[--] <path>...]\n")
40 N_(" or: git show [options] <object>..."),
41 NULL
44 static int parse_decoration_style(const char *var, const char *value)
46 switch (git_config_maybe_bool(var, value)) {
47 case 1:
48 return DECORATE_SHORT_REFS;
49 case 0:
50 return 0;
51 default:
52 break;
54 if (!strcmp(value, "full"))
55 return DECORATE_FULL_REFS;
56 else if (!strcmp(value, "short"))
57 return DECORATE_SHORT_REFS;
58 return -1;
61 static int decorate_callback(const struct option *opt, const char *arg, int unset)
63 if (unset)
64 decoration_style = 0;
65 else if (arg)
66 decoration_style = parse_decoration_style("command line", arg);
67 else
68 decoration_style = DECORATE_SHORT_REFS;
70 if (decoration_style < 0)
71 die("invalid --decorate option: %s", arg);
73 decoration_given = 1;
75 return 0;
78 static void cmd_log_init_defaults(struct rev_info *rev)
80 if (fmt_pretty)
81 get_commit_format(fmt_pretty, rev);
82 rev->verbose_header = 1;
83 DIFF_OPT_SET(&rev->diffopt, RECURSIVE);
84 rev->diffopt.stat_width = -1; /* use full terminal width */
85 rev->diffopt.stat_graph_width = -1; /* respect statGraphWidth config */
86 rev->abbrev_commit = default_abbrev_commit;
87 rev->show_root_diff = default_show_root;
88 rev->subject_prefix = fmt_patch_subject_prefix;
89 DIFF_OPT_SET(&rev->diffopt, ALLOW_TEXTCONV);
91 if (default_date_mode)
92 rev->date_mode = parse_date_format(default_date_mode);
95 static void cmd_log_init_finish(int argc, const char **argv, const char *prefix,
96 struct rev_info *rev, struct setup_revision_opt *opt)
98 struct userformat_want w;
99 int quiet = 0, source = 0, mailmap = 0;
101 const struct option builtin_log_options[] = {
102 OPT_BOOLEAN(0, "quiet", &quiet, N_("suppress diff output")),
103 OPT_BOOLEAN(0, "source", &source, N_("show source")),
104 OPT_BOOLEAN(0, "use-mailmap", &mailmap, N_("Use mail map file")),
105 { OPTION_CALLBACK, 0, "decorate", NULL, NULL, N_("decorate options"),
106 PARSE_OPT_OPTARG, decorate_callback},
107 OPT_END()
110 mailmap = use_mailmap_config;
111 argc = parse_options(argc, argv, prefix,
112 builtin_log_options, builtin_log_usage,
113 PARSE_OPT_KEEP_ARGV0 | PARSE_OPT_KEEP_UNKNOWN |
114 PARSE_OPT_KEEP_DASHDASH);
116 if (quiet)
117 rev->diffopt.output_format |= DIFF_FORMAT_NO_OUTPUT;
118 argc = setup_revisions(argc, argv, rev, opt);
120 /* Any arguments at this point are not recognized */
121 if (argc > 1)
122 die("unrecognized argument: %s", argv[1]);
124 memset(&w, 0, sizeof(w));
125 userformat_find_requirements(NULL, &w);
127 if (!rev->show_notes_given && (!rev->pretty_given || w.notes))
128 rev->show_notes = 1;
129 if (rev->show_notes)
130 init_display_notes(&rev->notes_opt);
132 if (rev->diffopt.pickaxe || rev->diffopt.filter)
133 rev->always_show_header = 0;
134 if (DIFF_OPT_TST(&rev->diffopt, FOLLOW_RENAMES)) {
135 rev->always_show_header = 0;
136 if (rev->diffopt.pathspec.nr != 1)
137 usage("git logs can only follow renames on one pathname at a time");
140 if (source)
141 rev->show_source = 1;
143 if (mailmap) {
144 rev->mailmap = xcalloc(1, sizeof(struct string_list));
145 read_mailmap(rev->mailmap, NULL);
148 if (rev->pretty_given && rev->commit_format == CMIT_FMT_RAW) {
150 * "log --pretty=raw" is special; ignore UI oriented
151 * configuration variables such as decoration.
153 if (!decoration_given)
154 decoration_style = 0;
155 if (!rev->abbrev_commit_given)
156 rev->abbrev_commit = 0;
159 if (decoration_style) {
160 rev->show_decorations = 1;
161 load_ref_decorations(decoration_style);
163 setup_pager();
166 static void cmd_log_init(int argc, const char **argv, const char *prefix,
167 struct rev_info *rev, struct setup_revision_opt *opt)
169 cmd_log_init_defaults(rev);
170 cmd_log_init_finish(argc, argv, prefix, rev, opt);
174 * This gives a rough estimate for how many commits we
175 * will print out in the list.
177 static int estimate_commit_count(struct rev_info *rev, struct commit_list *list)
179 int n = 0;
181 while (list) {
182 struct commit *commit = list->item;
183 unsigned int flags = commit->object.flags;
184 list = list->next;
185 if (!(flags & (TREESAME | UNINTERESTING)))
186 n++;
188 return n;
191 static void show_early_header(struct rev_info *rev, const char *stage, int nr)
193 if (rev->shown_one) {
194 rev->shown_one = 0;
195 if (rev->commit_format != CMIT_FMT_ONELINE)
196 putchar(rev->diffopt.line_termination);
198 printf(_("Final output: %d %s\n"), nr, stage);
201 static struct itimerval early_output_timer;
203 static void log_show_early(struct rev_info *revs, struct commit_list *list)
205 int i = revs->early_output;
206 int show_header = 1;
208 sort_in_topological_order(&list, revs->lifo);
209 while (list && i) {
210 struct commit *commit = list->item;
211 switch (simplify_commit(revs, commit)) {
212 case commit_show:
213 if (show_header) {
214 int n = estimate_commit_count(revs, list);
215 show_early_header(revs, "incomplete", n);
216 show_header = 0;
218 log_tree_commit(revs, commit);
219 i--;
220 break;
221 case commit_ignore:
222 break;
223 case commit_error:
224 return;
226 list = list->next;
229 /* Did we already get enough commits for the early output? */
230 if (!i)
231 return;
234 * ..if no, then repeat it twice a second until we
235 * do.
237 * NOTE! We don't use "it_interval", because if the
238 * reader isn't listening, we want our output to be
239 * throttled by the writing, and not have the timer
240 * trigger every second even if we're blocked on a
241 * reader!
243 early_output_timer.it_value.tv_sec = 0;
244 early_output_timer.it_value.tv_usec = 500000;
245 setitimer(ITIMER_REAL, &early_output_timer, NULL);
248 static void early_output(int signal)
250 show_early_output = log_show_early;
253 static void setup_early_output(struct rev_info *rev)
255 struct sigaction sa;
258 * Set up the signal handler, minimally intrusively:
259 * we only set a single volatile integer word (not
260 * using sigatomic_t - trying to avoid unnecessary
261 * system dependencies and headers), and using
262 * SA_RESTART.
264 memset(&sa, 0, sizeof(sa));
265 sa.sa_handler = early_output;
266 sigemptyset(&sa.sa_mask);
267 sa.sa_flags = SA_RESTART;
268 sigaction(SIGALRM, &sa, NULL);
271 * If we can get the whole output in less than a
272 * tenth of a second, don't even bother doing the
273 * early-output thing..
275 * This is a one-time-only trigger.
277 early_output_timer.it_value.tv_sec = 0;
278 early_output_timer.it_value.tv_usec = 100000;
279 setitimer(ITIMER_REAL, &early_output_timer, NULL);
282 static void finish_early_output(struct rev_info *rev)
284 int n = estimate_commit_count(rev, rev->commits);
285 signal(SIGALRM, SIG_IGN);
286 show_early_header(rev, "done", n);
289 static int cmd_log_walk(struct rev_info *rev)
291 struct commit *commit;
292 int saved_nrl = 0;
293 int saved_dcctc = 0;
295 if (rev->early_output)
296 setup_early_output(rev);
298 if (prepare_revision_walk(rev))
299 die(_("revision walk setup failed"));
301 if (rev->early_output)
302 finish_early_output(rev);
305 * For --check and --exit-code, the exit code is based on CHECK_FAILED
306 * and HAS_CHANGES being accumulated in rev->diffopt, so be careful to
307 * retain that state information if replacing rev->diffopt in this loop
309 while ((commit = get_revision(rev)) != NULL) {
310 if (!log_tree_commit(rev, commit) &&
311 rev->max_count >= 0)
313 * We decremented max_count in get_revision,
314 * but we didn't actually show the commit.
316 rev->max_count++;
317 if (!rev->reflog_info) {
318 /* we allow cycles in reflog ancestry */
319 free(commit->buffer);
320 commit->buffer = NULL;
322 free_commit_list(commit->parents);
323 commit->parents = NULL;
324 if (saved_nrl < rev->diffopt.needed_rename_limit)
325 saved_nrl = rev->diffopt.needed_rename_limit;
326 if (rev->diffopt.degraded_cc_to_c)
327 saved_dcctc = 1;
329 rev->diffopt.degraded_cc_to_c = saved_dcctc;
330 rev->diffopt.needed_rename_limit = saved_nrl;
332 if (rev->diffopt.output_format & DIFF_FORMAT_CHECKDIFF &&
333 DIFF_OPT_TST(&rev->diffopt, CHECK_FAILED)) {
334 return 02;
336 return diff_result_code(&rev->diffopt, 0);
339 static int git_log_config(const char *var, const char *value, void *cb)
341 if (!strcmp(var, "format.pretty"))
342 return git_config_string(&fmt_pretty, var, value);
343 if (!strcmp(var, "format.subjectprefix"))
344 return git_config_string(&fmt_patch_subject_prefix, var, value);
345 if (!strcmp(var, "log.abbrevcommit")) {
346 default_abbrev_commit = git_config_bool(var, value);
347 return 0;
349 if (!strcmp(var, "log.date"))
350 return git_config_string(&default_date_mode, var, value);
351 if (!strcmp(var, "log.decorate")) {
352 decoration_style = parse_decoration_style(var, value);
353 if (decoration_style < 0)
354 decoration_style = 0; /* maybe warn? */
355 return 0;
357 if (!strcmp(var, "log.showroot")) {
358 default_show_root = git_config_bool(var, value);
359 return 0;
361 if (!prefixcmp(var, "color.decorate."))
362 return parse_decorate_color_config(var, 15, value);
363 if (!strcmp(var, "log.mailmap")) {
364 use_mailmap_config = git_config_bool(var, value);
365 return 0;
368 if (grep_config(var, value, cb) < 0)
369 return -1;
370 return git_diff_ui_config(var, value, cb);
373 int cmd_whatchanged(int argc, const char **argv, const char *prefix)
375 struct rev_info rev;
376 struct setup_revision_opt opt;
378 init_grep_defaults();
379 git_config(git_log_config, NULL);
381 init_revisions(&rev, prefix);
382 rev.diff = 1;
383 rev.simplify_history = 0;
384 memset(&opt, 0, sizeof(opt));
385 opt.def = "HEAD";
386 opt.revarg_opt = REVARG_COMMITTISH;
387 cmd_log_init(argc, argv, prefix, &rev, &opt);
388 if (!rev.diffopt.output_format)
389 rev.diffopt.output_format = DIFF_FORMAT_RAW;
390 return cmd_log_walk(&rev);
393 static void show_tagger(char *buf, int len, struct rev_info *rev)
395 struct strbuf out = STRBUF_INIT;
396 struct pretty_print_context pp = {0};
398 pp.fmt = rev->commit_format;
399 pp.date_mode = rev->date_mode;
400 pp_user_info(&pp, "Tagger", &out, buf, get_log_output_encoding());
401 printf("%s", out.buf);
402 strbuf_release(&out);
405 static int show_blob_object(const unsigned char *sha1, struct rev_info *rev)
407 fflush(stdout);
408 return stream_blob_to_fd(1, sha1, NULL, 0);
411 static int show_tag_object(const unsigned char *sha1, struct rev_info *rev)
413 unsigned long size;
414 enum object_type type;
415 char *buf = read_sha1_file(sha1, &type, &size);
416 int offset = 0;
418 if (!buf)
419 return error(_("Could not read object %s"), sha1_to_hex(sha1));
421 assert(type == OBJ_TAG);
422 while (offset < size && buf[offset] != '\n') {
423 int new_offset = offset + 1;
424 while (new_offset < size && buf[new_offset++] != '\n')
425 ; /* do nothing */
426 if (!prefixcmp(buf + offset, "tagger "))
427 show_tagger(buf + offset + 7,
428 new_offset - offset - 7, rev);
429 offset = new_offset;
432 if (offset < size)
433 fwrite(buf + offset, size - offset, 1, stdout);
434 free(buf);
435 return 0;
438 static int show_tree_object(const unsigned char *sha1,
439 const char *base, int baselen,
440 const char *pathname, unsigned mode, int stage, void *context)
442 printf("%s%s\n", pathname, S_ISDIR(mode) ? "/" : "");
443 return 0;
446 static void show_rev_tweak_rev(struct rev_info *rev, struct setup_revision_opt *opt)
448 if (rev->ignore_merges) {
449 /* There was no "-m" on the command line */
450 rev->ignore_merges = 0;
451 if (!rev->first_parent_only && !rev->combine_merges) {
452 /* No "--first-parent", "-c", nor "--cc" */
453 rev->combine_merges = 1;
454 rev->dense_combined_merges = 1;
457 if (!rev->diffopt.output_format)
458 rev->diffopt.output_format = DIFF_FORMAT_PATCH;
461 int cmd_show(int argc, const char **argv, const char *prefix)
463 struct rev_info rev;
464 struct object_array_entry *objects;
465 struct setup_revision_opt opt;
466 struct pathspec match_all;
467 int i, count, ret = 0;
469 init_grep_defaults();
470 git_config(git_log_config, NULL);
472 init_pathspec(&match_all, NULL);
473 init_revisions(&rev, prefix);
474 rev.diff = 1;
475 rev.always_show_header = 1;
476 rev.no_walk = REVISION_WALK_NO_WALK_SORTED;
477 rev.diffopt.stat_width = -1; /* Scale to real terminal size */
479 memset(&opt, 0, sizeof(opt));
480 opt.def = "HEAD";
481 opt.tweak = show_rev_tweak_rev;
482 cmd_log_init(argc, argv, prefix, &rev, &opt);
484 if (!rev.no_walk)
485 return cmd_log_walk(&rev);
487 count = rev.pending.nr;
488 objects = rev.pending.objects;
489 for (i = 0; i < count && !ret; i++) {
490 struct object *o = objects[i].item;
491 const char *name = objects[i].name;
492 switch (o->type) {
493 case OBJ_BLOB:
494 ret = show_blob_object(o->sha1, NULL);
495 break;
496 case OBJ_TAG: {
497 struct tag *t = (struct tag *)o;
499 if (rev.shown_one)
500 putchar('\n');
501 printf("%stag %s%s\n",
502 diff_get_color_opt(&rev.diffopt, DIFF_COMMIT),
503 t->tag,
504 diff_get_color_opt(&rev.diffopt, DIFF_RESET));
505 ret = show_tag_object(o->sha1, &rev);
506 rev.shown_one = 1;
507 if (ret)
508 break;
509 o = parse_object(t->tagged->sha1);
510 if (!o)
511 ret = error(_("Could not read object %s"),
512 sha1_to_hex(t->tagged->sha1));
513 objects[i].item = o;
514 i--;
515 break;
517 case OBJ_TREE:
518 if (rev.shown_one)
519 putchar('\n');
520 printf("%stree %s%s\n\n",
521 diff_get_color_opt(&rev.diffopt, DIFF_COMMIT),
522 name,
523 diff_get_color_opt(&rev.diffopt, DIFF_RESET));
524 read_tree_recursive((struct tree *)o, "", 0, 0, &match_all,
525 show_tree_object, NULL);
526 rev.shown_one = 1;
527 break;
528 case OBJ_COMMIT:
529 rev.pending.nr = rev.pending.alloc = 0;
530 rev.pending.objects = NULL;
531 add_object_array(o, name, &rev.pending);
532 ret = cmd_log_walk(&rev);
533 break;
534 default:
535 ret = error(_("Unknown type: %d"), o->type);
538 free(objects);
539 return ret;
543 * This is equivalent to "git log -g --abbrev-commit --pretty=oneline"
545 int cmd_log_reflog(int argc, const char **argv, const char *prefix)
547 struct rev_info rev;
548 struct setup_revision_opt opt;
550 init_grep_defaults();
551 git_config(git_log_config, NULL);
553 init_revisions(&rev, prefix);
554 init_reflog_walk(&rev.reflog_info);
555 rev.verbose_header = 1;
556 memset(&opt, 0, sizeof(opt));
557 opt.def = "HEAD";
558 cmd_log_init_defaults(&rev);
559 rev.abbrev_commit = 1;
560 rev.commit_format = CMIT_FMT_ONELINE;
561 rev.use_terminator = 1;
562 rev.always_show_header = 1;
563 cmd_log_init_finish(argc, argv, prefix, &rev, &opt);
565 return cmd_log_walk(&rev);
568 int cmd_log(int argc, const char **argv, const char *prefix)
570 struct rev_info rev;
571 struct setup_revision_opt opt;
573 init_grep_defaults();
574 git_config(git_log_config, NULL);
576 init_revisions(&rev, prefix);
577 rev.always_show_header = 1;
578 memset(&opt, 0, sizeof(opt));
579 opt.def = "HEAD";
580 opt.revarg_opt = REVARG_COMMITTISH;
581 cmd_log_init(argc, argv, prefix, &rev, &opt);
582 return cmd_log_walk(&rev);
585 /* format-patch */
587 static const char *fmt_patch_suffix = ".patch";
588 static int numbered = 0;
589 static int auto_number = 1;
591 static char *default_attach = NULL;
593 static struct string_list extra_hdr;
594 static struct string_list extra_to;
595 static struct string_list extra_cc;
597 static void add_header(const char *value)
599 struct string_list_item *item;
600 int len = strlen(value);
601 while (len && value[len - 1] == '\n')
602 len--;
604 if (!strncasecmp(value, "to: ", 4)) {
605 item = string_list_append(&extra_to, value + 4);
606 len -= 4;
607 } else if (!strncasecmp(value, "cc: ", 4)) {
608 item = string_list_append(&extra_cc, value + 4);
609 len -= 4;
610 } else {
611 item = string_list_append(&extra_hdr, value);
614 item->string[len] = '\0';
617 #define THREAD_SHALLOW 1
618 #define THREAD_DEEP 2
619 static int thread;
620 static int do_signoff;
621 static const char *signature = git_version_string;
623 static int git_format_config(const char *var, const char *value, void *cb)
625 if (!strcmp(var, "format.headers")) {
626 if (!value)
627 die(_("format.headers without value"));
628 add_header(value);
629 return 0;
631 if (!strcmp(var, "format.suffix"))
632 return git_config_string(&fmt_patch_suffix, var, value);
633 if (!strcmp(var, "format.to")) {
634 if (!value)
635 return config_error_nonbool(var);
636 string_list_append(&extra_to, value);
637 return 0;
639 if (!strcmp(var, "format.cc")) {
640 if (!value)
641 return config_error_nonbool(var);
642 string_list_append(&extra_cc, value);
643 return 0;
645 if (!strcmp(var, "diff.color") || !strcmp(var, "color.diff") ||
646 !strcmp(var, "color.ui")) {
647 return 0;
649 if (!strcmp(var, "format.numbered")) {
650 if (value && !strcasecmp(value, "auto")) {
651 auto_number = 1;
652 return 0;
654 numbered = git_config_bool(var, value);
655 auto_number = auto_number && numbered;
656 return 0;
658 if (!strcmp(var, "format.attach")) {
659 if (value && *value)
660 default_attach = xstrdup(value);
661 else
662 default_attach = xstrdup(git_version_string);
663 return 0;
665 if (!strcmp(var, "format.thread")) {
666 if (value && !strcasecmp(value, "deep")) {
667 thread = THREAD_DEEP;
668 return 0;
670 if (value && !strcasecmp(value, "shallow")) {
671 thread = THREAD_SHALLOW;
672 return 0;
674 thread = git_config_bool(var, value) && THREAD_SHALLOW;
675 return 0;
677 if (!strcmp(var, "format.signoff")) {
678 do_signoff = git_config_bool(var, value);
679 return 0;
681 if (!strcmp(var, "format.signature"))
682 return git_config_string(&signature, var, value);
684 return git_log_config(var, value, cb);
687 static FILE *realstdout = NULL;
688 static const char *output_directory = NULL;
689 static int outdir_offset;
691 static int reopen_stdout(struct commit *commit, const char *subject,
692 struct rev_info *rev, int quiet)
694 struct strbuf filename = STRBUF_INIT;
695 int suffix_len = strlen(fmt_patch_suffix) + 1;
697 if (output_directory) {
698 strbuf_addstr(&filename, output_directory);
699 if (filename.len >=
700 PATH_MAX - FORMAT_PATCH_NAME_MAX - suffix_len)
701 return error(_("name of output directory is too long"));
702 if (filename.buf[filename.len - 1] != '/')
703 strbuf_addch(&filename, '/');
706 get_patch_filename(commit, subject, rev->nr, fmt_patch_suffix, &filename);
708 if (!quiet)
709 fprintf(realstdout, "%s\n", filename.buf + outdir_offset);
711 if (freopen(filename.buf, "w", stdout) == NULL)
712 return error(_("Cannot open patch file %s"), filename.buf);
714 strbuf_release(&filename);
715 return 0;
718 static void get_patch_ids(struct rev_info *rev, struct patch_ids *ids)
720 struct rev_info check_rev;
721 struct commit *commit;
722 struct object *o1, *o2;
723 unsigned flags1, flags2;
725 if (rev->pending.nr != 2)
726 die(_("Need exactly one range."));
728 o1 = rev->pending.objects[0].item;
729 flags1 = o1->flags;
730 o2 = rev->pending.objects[1].item;
731 flags2 = o2->flags;
733 if ((flags1 & UNINTERESTING) == (flags2 & UNINTERESTING))
734 die(_("Not a range."));
736 init_patch_ids(ids);
738 /* given a range a..b get all patch ids for b..a */
739 init_revisions(&check_rev, rev->prefix);
740 check_rev.max_parents = 1;
741 o1->flags ^= UNINTERESTING;
742 o2->flags ^= UNINTERESTING;
743 add_pending_object(&check_rev, o1, "o1");
744 add_pending_object(&check_rev, o2, "o2");
745 if (prepare_revision_walk(&check_rev))
746 die(_("revision walk setup failed"));
748 while ((commit = get_revision(&check_rev)) != NULL) {
749 add_commit_patch_id(commit, ids);
752 /* reset for next revision walk */
753 clear_commit_marks((struct commit *)o1,
754 SEEN | UNINTERESTING | SHOWN | ADDED);
755 clear_commit_marks((struct commit *)o2,
756 SEEN | UNINTERESTING | SHOWN | ADDED);
757 o1->flags = flags1;
758 o2->flags = flags2;
761 static void gen_message_id(struct rev_info *info, char *base)
763 struct strbuf buf = STRBUF_INIT;
764 strbuf_addf(&buf, "%s.%lu.git.%s", base,
765 (unsigned long) time(NULL),
766 git_committer_info(IDENT_NO_NAME|IDENT_NO_DATE|IDENT_STRICT));
767 info->message_id = strbuf_detach(&buf, NULL);
770 static void print_signature(void)
772 if (signature && *signature)
773 printf("-- \n%s\n\n", signature);
776 static void add_branch_description(struct strbuf *buf, const char *branch_name)
778 struct strbuf desc = STRBUF_INIT;
779 if (!branch_name || !*branch_name)
780 return;
781 read_branch_desc(&desc, branch_name);
782 if (desc.len) {
783 strbuf_addch(buf, '\n');
784 strbuf_add(buf, desc.buf, desc.len);
785 strbuf_addch(buf, '\n');
789 static void make_cover_letter(struct rev_info *rev, int use_stdout,
790 int numbered, int numbered_files,
791 struct commit *origin,
792 int nr, struct commit **list, struct commit *head,
793 const char *branch_name,
794 int quiet)
796 const char *committer;
797 const char *body = "*** SUBJECT HERE ***\n\n*** BLURB HERE ***\n";
798 const char *msg;
799 struct shortlog log;
800 struct strbuf sb = STRBUF_INIT;
801 int i;
802 const char *encoding = "UTF-8";
803 struct diff_options opts;
804 int need_8bit_cte = 0;
805 struct pretty_print_context pp = {0};
807 if (rev->commit_format != CMIT_FMT_EMAIL)
808 die(_("Cover letter needs email format"));
810 committer = git_committer_info(0);
812 if (!use_stdout &&
813 reopen_stdout(NULL, numbered_files ? NULL : "cover-letter", rev, quiet))
814 return;
816 log_write_email_headers(rev, head, &pp.subject, &pp.after_subject,
817 &need_8bit_cte);
819 for (i = 0; !need_8bit_cte && i < nr; i++)
820 if (has_non_ascii(list[i]->buffer))
821 need_8bit_cte = 1;
823 msg = body;
824 pp.fmt = CMIT_FMT_EMAIL;
825 pp.date_mode = DATE_RFC2822;
826 pp_user_info(&pp, NULL, &sb, committer, encoding);
827 pp_title_line(&pp, &msg, &sb, encoding, need_8bit_cte);
828 pp_remainder(&pp, &msg, &sb, 0);
829 add_branch_description(&sb, branch_name);
830 printf("%s\n", sb.buf);
832 strbuf_release(&sb);
834 shortlog_init(&log);
835 log.wrap_lines = 1;
836 log.wrap = 72;
837 log.in1 = 2;
838 log.in2 = 4;
839 for (i = 0; i < nr; i++)
840 shortlog_add_commit(&log, list[i]);
842 shortlog_output(&log);
845 * We can only do diffstat with a unique reference point
847 if (!origin)
848 return;
850 memcpy(&opts, &rev->diffopt, sizeof(opts));
851 opts.output_format = DIFF_FORMAT_SUMMARY | DIFF_FORMAT_DIFFSTAT;
853 diff_setup_done(&opts);
855 diff_tree_sha1(origin->tree->object.sha1,
856 head->tree->object.sha1,
857 "", &opts);
858 diffcore_std(&opts);
859 diff_flush(&opts);
861 printf("\n");
862 print_signature();
865 static const char *clean_message_id(const char *msg_id)
867 char ch;
868 const char *a, *z, *m;
870 m = msg_id;
871 while ((ch = *m) && (isspace(ch) || (ch == '<')))
872 m++;
873 a = m;
874 z = NULL;
875 while ((ch = *m)) {
876 if (!isspace(ch) && (ch != '>'))
877 z = m;
878 m++;
880 if (!z)
881 die(_("insane in-reply-to: %s"), msg_id);
882 if (++z == m)
883 return a;
884 return xmemdupz(a, z - a);
887 static const char *set_outdir(const char *prefix, const char *output_directory)
889 if (output_directory && is_absolute_path(output_directory))
890 return output_directory;
892 if (!prefix || !*prefix) {
893 if (output_directory)
894 return output_directory;
895 /* The user did not explicitly ask for "./" */
896 outdir_offset = 2;
897 return "./";
900 outdir_offset = strlen(prefix);
901 if (!output_directory)
902 return prefix;
904 return xstrdup(prefix_filename(prefix, outdir_offset,
905 output_directory));
908 static const char * const builtin_format_patch_usage[] = {
909 N_("git format-patch [options] [<since> | <revision range>]"),
910 NULL
913 static int keep_subject = 0;
915 static int keep_callback(const struct option *opt, const char *arg, int unset)
917 ((struct rev_info *)opt->value)->total = -1;
918 keep_subject = 1;
919 return 0;
922 static int subject_prefix = 0;
924 static int subject_prefix_callback(const struct option *opt, const char *arg,
925 int unset)
927 subject_prefix = 1;
928 ((struct rev_info *)opt->value)->subject_prefix = arg;
929 return 0;
932 static int numbered_cmdline_opt = 0;
934 static int numbered_callback(const struct option *opt, const char *arg,
935 int unset)
937 *(int *)opt->value = numbered_cmdline_opt = unset ? 0 : 1;
938 if (unset)
939 auto_number = 0;
940 return 0;
943 static int no_numbered_callback(const struct option *opt, const char *arg,
944 int unset)
946 return numbered_callback(opt, arg, 1);
949 static int output_directory_callback(const struct option *opt, const char *arg,
950 int unset)
952 const char **dir = (const char **)opt->value;
953 if (*dir)
954 die(_("Two output directories?"));
955 *dir = arg;
956 return 0;
959 static int thread_callback(const struct option *opt, const char *arg, int unset)
961 int *thread = (int *)opt->value;
962 if (unset)
963 *thread = 0;
964 else if (!arg || !strcmp(arg, "shallow"))
965 *thread = THREAD_SHALLOW;
966 else if (!strcmp(arg, "deep"))
967 *thread = THREAD_DEEP;
968 else
969 return 1;
970 return 0;
973 static int attach_callback(const struct option *opt, const char *arg, int unset)
975 struct rev_info *rev = (struct rev_info *)opt->value;
976 if (unset)
977 rev->mime_boundary = NULL;
978 else if (arg)
979 rev->mime_boundary = arg;
980 else
981 rev->mime_boundary = git_version_string;
982 rev->no_inline = unset ? 0 : 1;
983 return 0;
986 static int inline_callback(const struct option *opt, const char *arg, int unset)
988 struct rev_info *rev = (struct rev_info *)opt->value;
989 if (unset)
990 rev->mime_boundary = NULL;
991 else if (arg)
992 rev->mime_boundary = arg;
993 else
994 rev->mime_boundary = git_version_string;
995 rev->no_inline = 0;
996 return 0;
999 static int header_callback(const struct option *opt, const char *arg, int unset)
1001 if (unset) {
1002 string_list_clear(&extra_hdr, 0);
1003 string_list_clear(&extra_to, 0);
1004 string_list_clear(&extra_cc, 0);
1005 } else {
1006 add_header(arg);
1008 return 0;
1011 static int to_callback(const struct option *opt, const char *arg, int unset)
1013 if (unset)
1014 string_list_clear(&extra_to, 0);
1015 else
1016 string_list_append(&extra_to, arg);
1017 return 0;
1020 static int cc_callback(const struct option *opt, const char *arg, int unset)
1022 if (unset)
1023 string_list_clear(&extra_cc, 0);
1024 else
1025 string_list_append(&extra_cc, arg);
1026 return 0;
1029 static char *find_branch_name(struct rev_info *rev)
1031 int i, positive = -1;
1032 unsigned char branch_sha1[20];
1033 struct strbuf buf = STRBUF_INIT;
1034 const char *branch;
1036 for (i = 0; i < rev->cmdline.nr; i++) {
1037 if (rev->cmdline.rev[i].flags & UNINTERESTING)
1038 continue;
1039 if (positive < 0)
1040 positive = i;
1041 else
1042 return NULL;
1044 if (positive < 0)
1045 return NULL;
1046 strbuf_addf(&buf, "refs/heads/%s", rev->cmdline.rev[positive].name);
1047 branch = resolve_ref_unsafe(buf.buf, branch_sha1, 1, NULL);
1048 if (!branch ||
1049 prefixcmp(branch, "refs/heads/") ||
1050 hashcmp(rev->cmdline.rev[positive].item->sha1, branch_sha1))
1051 branch = NULL;
1052 strbuf_release(&buf);
1053 if (branch)
1054 return xstrdup(rev->cmdline.rev[positive].name);
1055 return NULL;
1058 int cmd_format_patch(int argc, const char **argv, const char *prefix)
1060 struct commit *commit;
1061 struct commit **list = NULL;
1062 struct rev_info rev;
1063 struct setup_revision_opt s_r_opt;
1064 int nr = 0, total, i;
1065 int use_stdout = 0;
1066 int start_number = -1;
1067 int numbered_files = 0; /* _just_ numbers */
1068 int ignore_if_in_upstream = 0;
1069 int cover_letter = 0;
1070 int boundary_count = 0;
1071 int no_binary_diff = 0;
1072 struct commit *origin = NULL, *head = NULL;
1073 const char *in_reply_to = NULL;
1074 struct patch_ids ids;
1075 char *add_signoff = NULL;
1076 struct strbuf buf = STRBUF_INIT;
1077 int use_patch_format = 0;
1078 int quiet = 0;
1079 char *branch_name = NULL;
1080 const struct option builtin_format_patch_options[] = {
1081 { OPTION_CALLBACK, 'n', "numbered", &numbered, NULL,
1082 N_("use [PATCH n/m] even with a single patch"),
1083 PARSE_OPT_NOARG, numbered_callback },
1084 { OPTION_CALLBACK, 'N', "no-numbered", &numbered, NULL,
1085 N_("use [PATCH] even with multiple patches"),
1086 PARSE_OPT_NOARG, no_numbered_callback },
1087 OPT_BOOLEAN('s', "signoff", &do_signoff, N_("add Signed-off-by:")),
1088 OPT_BOOLEAN(0, "stdout", &use_stdout,
1089 N_("print patches to standard out")),
1090 OPT_BOOLEAN(0, "cover-letter", &cover_letter,
1091 N_("generate a cover letter")),
1092 OPT_BOOLEAN(0, "numbered-files", &numbered_files,
1093 N_("use simple number sequence for output file names")),
1094 OPT_STRING(0, "suffix", &fmt_patch_suffix, N_("sfx"),
1095 N_("use <sfx> instead of '.patch'")),
1096 OPT_INTEGER(0, "start-number", &start_number,
1097 N_("start numbering patches at <n> instead of 1")),
1098 { OPTION_CALLBACK, 0, "subject-prefix", &rev, N_("prefix"),
1099 N_("Use [<prefix>] instead of [PATCH]"),
1100 PARSE_OPT_NONEG, subject_prefix_callback },
1101 { OPTION_CALLBACK, 'o', "output-directory", &output_directory,
1102 N_("dir"), N_("store resulting files in <dir>"),
1103 PARSE_OPT_NONEG, output_directory_callback },
1104 { OPTION_CALLBACK, 'k', "keep-subject", &rev, NULL,
1105 N_("don't strip/add [PATCH]"),
1106 PARSE_OPT_NOARG | PARSE_OPT_NONEG, keep_callback },
1107 OPT_BOOLEAN(0, "no-binary", &no_binary_diff,
1108 N_("don't output binary diffs")),
1109 OPT_BOOLEAN(0, "ignore-if-in-upstream", &ignore_if_in_upstream,
1110 N_("don't include a patch matching a commit upstream")),
1111 { OPTION_BOOLEAN, 'p', "no-stat", &use_patch_format, NULL,
1112 N_("show patch format instead of default (patch + stat)"),
1113 PARSE_OPT_NONEG | PARSE_OPT_NOARG },
1114 OPT_GROUP(N_("Messaging")),
1115 { OPTION_CALLBACK, 0, "add-header", NULL, N_("header"),
1116 N_("add email header"), 0, header_callback },
1117 { OPTION_CALLBACK, 0, "to", NULL, N_("email"), N_("add To: header"),
1118 0, to_callback },
1119 { OPTION_CALLBACK, 0, "cc", NULL, N_("email"), N_("add Cc: header"),
1120 0, cc_callback },
1121 OPT_STRING(0, "in-reply-to", &in_reply_to, N_("message-id"),
1122 N_("make first mail a reply to <message-id>")),
1123 { OPTION_CALLBACK, 0, "attach", &rev, N_("boundary"),
1124 N_("attach the patch"), PARSE_OPT_OPTARG,
1125 attach_callback },
1126 { OPTION_CALLBACK, 0, "inline", &rev, N_("boundary"),
1127 N_("inline the patch"),
1128 PARSE_OPT_OPTARG | PARSE_OPT_NONEG,
1129 inline_callback },
1130 { OPTION_CALLBACK, 0, "thread", &thread, N_("style"),
1131 N_("enable message threading, styles: shallow, deep"),
1132 PARSE_OPT_OPTARG, thread_callback },
1133 OPT_STRING(0, "signature", &signature, N_("signature"),
1134 N_("add a signature")),
1135 OPT_BOOLEAN(0, "quiet", &quiet,
1136 N_("don't print the patch filenames")),
1137 OPT_END()
1140 extra_hdr.strdup_strings = 1;
1141 extra_to.strdup_strings = 1;
1142 extra_cc.strdup_strings = 1;
1143 init_grep_defaults();
1144 git_config(git_format_config, NULL);
1145 init_revisions(&rev, prefix);
1146 rev.commit_format = CMIT_FMT_EMAIL;
1147 rev.verbose_header = 1;
1148 rev.diff = 1;
1149 rev.max_parents = 1;
1150 DIFF_OPT_SET(&rev.diffopt, RECURSIVE);
1151 rev.subject_prefix = fmt_patch_subject_prefix;
1152 memset(&s_r_opt, 0, sizeof(s_r_opt));
1153 s_r_opt.def = "HEAD";
1154 s_r_opt.revarg_opt = REVARG_COMMITTISH;
1156 if (default_attach) {
1157 rev.mime_boundary = default_attach;
1158 rev.no_inline = 1;
1162 * Parse the arguments before setup_revisions(), or something
1163 * like "git format-patch -o a123 HEAD^.." may fail; a123 is
1164 * possibly a valid SHA1.
1166 argc = parse_options(argc, argv, prefix, builtin_format_patch_options,
1167 builtin_format_patch_usage,
1168 PARSE_OPT_KEEP_ARGV0 | PARSE_OPT_KEEP_UNKNOWN |
1169 PARSE_OPT_KEEP_DASHDASH);
1171 if (do_signoff) {
1172 const char *committer;
1173 const char *endpos;
1174 committer = git_committer_info(IDENT_STRICT);
1175 endpos = strchr(committer, '>');
1176 if (!endpos)
1177 die(_("bogus committer info %s"), committer);
1178 add_signoff = xmemdupz(committer, endpos - committer + 1);
1181 for (i = 0; i < extra_hdr.nr; i++) {
1182 strbuf_addstr(&buf, extra_hdr.items[i].string);
1183 strbuf_addch(&buf, '\n');
1186 if (extra_to.nr)
1187 strbuf_addstr(&buf, "To: ");
1188 for (i = 0; i < extra_to.nr; i++) {
1189 if (i)
1190 strbuf_addstr(&buf, " ");
1191 strbuf_addstr(&buf, extra_to.items[i].string);
1192 if (i + 1 < extra_to.nr)
1193 strbuf_addch(&buf, ',');
1194 strbuf_addch(&buf, '\n');
1197 if (extra_cc.nr)
1198 strbuf_addstr(&buf, "Cc: ");
1199 for (i = 0; i < extra_cc.nr; i++) {
1200 if (i)
1201 strbuf_addstr(&buf, " ");
1202 strbuf_addstr(&buf, extra_cc.items[i].string);
1203 if (i + 1 < extra_cc.nr)
1204 strbuf_addch(&buf, ',');
1205 strbuf_addch(&buf, '\n');
1208 rev.extra_headers = strbuf_detach(&buf, NULL);
1210 if (start_number < 0)
1211 start_number = 1;
1214 * If numbered is set solely due to format.numbered in config,
1215 * and it would conflict with --keep-subject (-k) from the
1216 * command line, reset "numbered".
1218 if (numbered && keep_subject && !numbered_cmdline_opt)
1219 numbered = 0;
1221 if (numbered && keep_subject)
1222 die (_("-n and -k are mutually exclusive."));
1223 if (keep_subject && subject_prefix)
1224 die (_("--subject-prefix and -k are mutually exclusive."));
1225 rev.preserve_subject = keep_subject;
1227 argc = setup_revisions(argc, argv, &rev, &s_r_opt);
1228 if (argc > 1)
1229 die (_("unrecognized argument: %s"), argv[1]);
1231 if (rev.diffopt.output_format & DIFF_FORMAT_NAME)
1232 die(_("--name-only does not make sense"));
1233 if (rev.diffopt.output_format & DIFF_FORMAT_NAME_STATUS)
1234 die(_("--name-status does not make sense"));
1235 if (rev.diffopt.output_format & DIFF_FORMAT_CHECKDIFF)
1236 die(_("--check does not make sense"));
1238 if (!use_patch_format &&
1239 (!rev.diffopt.output_format ||
1240 rev.diffopt.output_format == DIFF_FORMAT_PATCH))
1241 rev.diffopt.output_format = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_SUMMARY;
1243 /* Always generate a patch */
1244 rev.diffopt.output_format |= DIFF_FORMAT_PATCH;
1246 if (!DIFF_OPT_TST(&rev.diffopt, TEXT) && !no_binary_diff)
1247 DIFF_OPT_SET(&rev.diffopt, BINARY);
1249 if (rev.show_notes)
1250 init_display_notes(&rev.notes_opt);
1252 if (!use_stdout)
1253 output_directory = set_outdir(prefix, output_directory);
1254 else
1255 setup_pager();
1257 if (output_directory) {
1258 if (use_stdout)
1259 die(_("standard output, or directory, which one?"));
1260 if (mkdir(output_directory, 0777) < 0 && errno != EEXIST)
1261 die_errno(_("Could not create directory '%s'"),
1262 output_directory);
1265 if (rev.pending.nr == 1) {
1266 if (rev.max_count < 0 && !rev.show_root_diff) {
1268 * This is traditional behaviour of "git format-patch
1269 * origin" that prepares what the origin side still
1270 * does not have.
1272 unsigned char sha1[20];
1273 const char *ref;
1275 rev.pending.objects[0].item->flags |= UNINTERESTING;
1276 add_head_to_pending(&rev);
1277 ref = resolve_ref_unsafe("HEAD", sha1, 1, NULL);
1278 if (ref && !prefixcmp(ref, "refs/heads/"))
1279 branch_name = xstrdup(ref + strlen("refs/heads/"));
1280 else
1281 branch_name = xstrdup(""); /* no branch */
1284 * Otherwise, it is "format-patch -22 HEAD", and/or
1285 * "format-patch --root HEAD". The user wants
1286 * get_revision() to do the usual traversal.
1291 * We cannot move this anywhere earlier because we do want to
1292 * know if --root was given explicitly from the command line.
1294 rev.show_root_diff = 1;
1296 if (cover_letter) {
1298 * NEEDSWORK:randomly pick one positive commit to show
1299 * diffstat; this is often the tip and the command
1300 * happens to do the right thing in most cases, but a
1301 * complex command like "--cover-letter a b c ^bottom"
1302 * picks "c" and shows diffstat between bottom..c
1303 * which may not match what the series represents at
1304 * all and totally broken.
1306 int i;
1307 for (i = 0; i < rev.pending.nr; i++) {
1308 struct object *o = rev.pending.objects[i].item;
1309 if (!(o->flags & UNINTERESTING))
1310 head = (struct commit *)o;
1312 /* There is nothing to show; it is not an error, though. */
1313 if (!head)
1314 return 0;
1315 if (!branch_name)
1316 branch_name = find_branch_name(&rev);
1319 if (ignore_if_in_upstream) {
1320 /* Don't say anything if head and upstream are the same. */
1321 if (rev.pending.nr == 2) {
1322 struct object_array_entry *o = rev.pending.objects;
1323 if (hashcmp(o[0].item->sha1, o[1].item->sha1) == 0)
1324 return 0;
1326 get_patch_ids(&rev, &ids);
1329 if (!use_stdout)
1330 realstdout = xfdopen(xdup(1), "w");
1332 if (prepare_revision_walk(&rev))
1333 die(_("revision walk setup failed"));
1334 rev.boundary = 1;
1335 while ((commit = get_revision(&rev)) != NULL) {
1336 if (commit->object.flags & BOUNDARY) {
1337 boundary_count++;
1338 origin = (boundary_count == 1) ? commit : NULL;
1339 continue;
1342 if (ignore_if_in_upstream &&
1343 has_commit_patch_id(commit, &ids))
1344 continue;
1346 nr++;
1347 list = xrealloc(list, nr * sizeof(list[0]));
1348 list[nr - 1] = commit;
1350 total = nr;
1351 if (!keep_subject && auto_number && total > 1)
1352 numbered = 1;
1353 if (numbered)
1354 rev.total = total + start_number - 1;
1355 if (in_reply_to || thread || cover_letter)
1356 rev.ref_message_ids = xcalloc(1, sizeof(struct string_list));
1357 if (in_reply_to) {
1358 const char *msgid = clean_message_id(in_reply_to);
1359 string_list_append(rev.ref_message_ids, msgid);
1361 rev.numbered_files = numbered_files;
1362 rev.patch_suffix = fmt_patch_suffix;
1363 if (cover_letter) {
1364 if (thread)
1365 gen_message_id(&rev, "cover");
1366 make_cover_letter(&rev, use_stdout, numbered, numbered_files,
1367 origin, nr, list, head, branch_name, quiet);
1368 total++;
1369 start_number--;
1371 rev.add_signoff = add_signoff;
1372 while (0 <= --nr) {
1373 int shown;
1374 commit = list[nr];
1375 rev.nr = total - nr + (start_number - 1);
1376 /* Make the second and subsequent mails replies to the first */
1377 if (thread) {
1378 /* Have we already had a message ID? */
1379 if (rev.message_id) {
1381 * For deep threading: make every mail
1382 * a reply to the previous one, no
1383 * matter what other options are set.
1385 * For shallow threading:
1387 * Without --cover-letter and
1388 * --in-reply-to, make every mail a
1389 * reply to the one before.
1391 * With --in-reply-to but no
1392 * --cover-letter, make every mail a
1393 * reply to the <reply-to>.
1395 * With --cover-letter, make every
1396 * mail but the cover letter a reply
1397 * to the cover letter. The cover
1398 * letter is a reply to the
1399 * --in-reply-to, if specified.
1401 if (thread == THREAD_SHALLOW
1402 && rev.ref_message_ids->nr > 0
1403 && (!cover_letter || rev.nr > 1))
1404 free(rev.message_id);
1405 else
1406 string_list_append(rev.ref_message_ids,
1407 rev.message_id);
1409 gen_message_id(&rev, sha1_to_hex(commit->object.sha1));
1412 if (!use_stdout &&
1413 reopen_stdout(numbered_files ? NULL : commit, NULL, &rev, quiet))
1414 die(_("Failed to create output files"));
1415 shown = log_tree_commit(&rev, commit);
1416 free(commit->buffer);
1417 commit->buffer = NULL;
1419 /* We put one extra blank line between formatted
1420 * patches and this flag is used by log-tree code
1421 * to see if it needs to emit a LF before showing
1422 * the log; when using one file per patch, we do
1423 * not want the extra blank line.
1425 if (!use_stdout)
1426 rev.shown_one = 0;
1427 if (shown) {
1428 if (rev.mime_boundary)
1429 printf("\n--%s%s--\n\n\n",
1430 mime_boundary_leader,
1431 rev.mime_boundary);
1432 else
1433 print_signature();
1435 if (!use_stdout)
1436 fclose(stdout);
1438 free(list);
1439 free(branch_name);
1440 string_list_clear(&extra_to, 0);
1441 string_list_clear(&extra_cc, 0);
1442 string_list_clear(&extra_hdr, 0);
1443 if (ignore_if_in_upstream)
1444 free_patch_ids(&ids);
1445 return 0;
1448 static int add_pending_commit(const char *arg, struct rev_info *revs, int flags)
1450 unsigned char sha1[20];
1451 if (get_sha1(arg, sha1) == 0) {
1452 struct commit *commit = lookup_commit_reference(sha1);
1453 if (commit) {
1454 commit->object.flags |= flags;
1455 add_pending_object(revs, &commit->object, arg);
1456 return 0;
1459 return -1;
1462 static const char * const cherry_usage[] = {
1463 N_("git cherry [-v] [<upstream> [<head> [<limit>]]]"),
1464 NULL
1467 static void print_commit(char sign, struct commit *commit, int verbose,
1468 int abbrev)
1470 if (!verbose) {
1471 printf("%c %s\n", sign,
1472 find_unique_abbrev(commit->object.sha1, abbrev));
1473 } else {
1474 struct strbuf buf = STRBUF_INIT;
1475 pp_commit_easy(CMIT_FMT_ONELINE, commit, &buf);
1476 printf("%c %s %s\n", sign,
1477 find_unique_abbrev(commit->object.sha1, abbrev),
1478 buf.buf);
1479 strbuf_release(&buf);
1483 int cmd_cherry(int argc, const char **argv, const char *prefix)
1485 struct rev_info revs;
1486 struct patch_ids ids;
1487 struct commit *commit;
1488 struct commit_list *list = NULL;
1489 struct branch *current_branch;
1490 const char *upstream;
1491 const char *head = "HEAD";
1492 const char *limit = NULL;
1493 int verbose = 0, abbrev = 0;
1495 struct option options[] = {
1496 OPT__ABBREV(&abbrev),
1497 OPT__VERBOSE(&verbose, N_("be verbose")),
1498 OPT_END()
1501 argc = parse_options(argc, argv, prefix, options, cherry_usage, 0);
1503 switch (argc) {
1504 case 3:
1505 limit = argv[2];
1506 /* FALLTHROUGH */
1507 case 2:
1508 head = argv[1];
1509 /* FALLTHROUGH */
1510 case 1:
1511 upstream = argv[0];
1512 break;
1513 default:
1514 current_branch = branch_get(NULL);
1515 if (!current_branch || !current_branch->merge
1516 || !current_branch->merge[0]
1517 || !current_branch->merge[0]->dst) {
1518 fprintf(stderr, _("Could not find a tracked"
1519 " remote branch, please"
1520 " specify <upstream> manually.\n"));
1521 usage_with_options(cherry_usage, options);
1524 upstream = current_branch->merge[0]->dst;
1527 init_revisions(&revs, prefix);
1528 revs.max_parents = 1;
1530 if (add_pending_commit(head, &revs, 0))
1531 die(_("Unknown commit %s"), head);
1532 if (add_pending_commit(upstream, &revs, UNINTERESTING))
1533 die(_("Unknown commit %s"), upstream);
1535 /* Don't say anything if head and upstream are the same. */
1536 if (revs.pending.nr == 2) {
1537 struct object_array_entry *o = revs.pending.objects;
1538 if (hashcmp(o[0].item->sha1, o[1].item->sha1) == 0)
1539 return 0;
1542 get_patch_ids(&revs, &ids);
1544 if (limit && add_pending_commit(limit, &revs, UNINTERESTING))
1545 die(_("Unknown commit %s"), limit);
1547 /* reverse the list of commits */
1548 if (prepare_revision_walk(&revs))
1549 die(_("revision walk setup failed"));
1550 while ((commit = get_revision(&revs)) != NULL) {
1551 commit_list_insert(commit, &list);
1554 while (list) {
1555 char sign = '+';
1557 commit = list->item;
1558 if (has_commit_patch_id(commit, &ids))
1559 sign = '-';
1560 print_commit(sign, commit, verbose, abbrev);
1561 list = list->next;
1564 free_patch_ids(&ids);
1565 return 0;