completion: improve handling of -c/-C and -b/-B in switch/checkout
[alt-git.git] / builtin / log.c
bloba5c3ace9a0f68912c63f57dc4152d2f9c4f819e8
1 /*
2 * Builtin "git log" and related commands (show, whatchanged)
4 * (C) Copyright 2006 Linus Torvalds
5 * 2006 Junio Hamano
6 */
7 #define USE_THE_INDEX_COMPATIBILITY_MACROS
8 #include "cache.h"
9 #include "config.h"
10 #include "refs.h"
11 #include "object-store.h"
12 #include "color.h"
13 #include "commit.h"
14 #include "diff.h"
15 #include "revision.h"
16 #include "log-tree.h"
17 #include "builtin.h"
18 #include "tag.h"
19 #include "reflog-walk.h"
20 #include "patch-ids.h"
21 #include "run-command.h"
22 #include "shortlog.h"
23 #include "remote.h"
24 #include "string-list.h"
25 #include "parse-options.h"
26 #include "line-log.h"
27 #include "branch.h"
28 #include "streaming.h"
29 #include "version.h"
30 #include "mailmap.h"
31 #include "gpg-interface.h"
32 #include "progress.h"
33 #include "commit-slab.h"
34 #include "repository.h"
35 #include "commit-reach.h"
36 #include "interdiff.h"
37 #include "range-diff.h"
39 #define MAIL_DEFAULT_WRAP 72
40 #define COVER_FROM_AUTO_MAX_SUBJECT_LEN 100
42 /* Set a default date-time format for git log ("log.date" config variable) */
43 static const char *default_date_mode = NULL;
45 static int default_abbrev_commit;
46 static int default_show_root = 1;
47 static int default_follow;
48 static int default_show_signature;
49 static int default_encode_email_headers = 1;
50 static int decoration_style;
51 static int decoration_given;
52 static int use_mailmap_config = 1;
53 static const char *fmt_patch_subject_prefix = "PATCH";
54 static const char *fmt_pretty;
56 static const char * const builtin_log_usage[] = {
57 N_("git log [<options>] [<revision-range>] [[--] <path>...]"),
58 N_("git show [<options>] <object>..."),
59 NULL
62 struct line_opt_callback_data {
63 struct rev_info *rev;
64 const char *prefix;
65 struct string_list args;
68 static int session_is_interactive(void)
70 return isatty(1) || pager_in_use();
73 static int auto_decoration_style(void)
75 return session_is_interactive() ? DECORATE_SHORT_REFS : 0;
78 static int parse_decoration_style(const char *value)
80 switch (git_parse_maybe_bool(value)) {
81 case 1:
82 return DECORATE_SHORT_REFS;
83 case 0:
84 return 0;
85 default:
86 break;
88 if (!strcmp(value, "full"))
89 return DECORATE_FULL_REFS;
90 else if (!strcmp(value, "short"))
91 return DECORATE_SHORT_REFS;
92 else if (!strcmp(value, "auto"))
93 return auto_decoration_style();
95 * Please update _git_log() in git-completion.bash when you
96 * add new decoration styles.
98 return -1;
101 static int decorate_callback(const struct option *opt, const char *arg, int unset)
103 if (unset)
104 decoration_style = 0;
105 else if (arg)
106 decoration_style = parse_decoration_style(arg);
107 else
108 decoration_style = DECORATE_SHORT_REFS;
110 if (decoration_style < 0)
111 die(_("invalid --decorate option: %s"), arg);
113 decoration_given = 1;
115 return 0;
118 static int log_line_range_callback(const struct option *option, const char *arg, int unset)
120 struct line_opt_callback_data *data = option->value;
122 BUG_ON_OPT_NEG(unset);
124 if (!arg)
125 return -1;
127 data->rev->line_level_traverse = 1;
128 string_list_append(&data->args, arg);
130 return 0;
133 static void init_log_defaults(void)
135 init_grep_defaults(the_repository);
136 init_diff_ui_defaults();
138 decoration_style = auto_decoration_style();
141 static void cmd_log_init_defaults(struct rev_info *rev)
143 if (fmt_pretty)
144 get_commit_format(fmt_pretty, rev);
145 if (default_follow)
146 rev->diffopt.flags.default_follow_renames = 1;
147 rev->verbose_header = 1;
148 rev->diffopt.flags.recursive = 1;
149 rev->diffopt.stat_width = -1; /* use full terminal width */
150 rev->diffopt.stat_graph_width = -1; /* respect statGraphWidth config */
151 rev->abbrev_commit = default_abbrev_commit;
152 rev->show_root_diff = default_show_root;
153 rev->subject_prefix = fmt_patch_subject_prefix;
154 rev->show_signature = default_show_signature;
155 rev->encode_email_headers = default_encode_email_headers;
156 rev->diffopt.flags.allow_textconv = 1;
158 if (default_date_mode)
159 parse_date_format(default_date_mode, &rev->date_mode);
162 static void cmd_log_init_finish(int argc, const char **argv, const char *prefix,
163 struct rev_info *rev, struct setup_revision_opt *opt)
165 struct userformat_want w;
166 int quiet = 0, source = 0, mailmap;
167 static struct line_opt_callback_data line_cb = {NULL, NULL, STRING_LIST_INIT_DUP};
168 static struct string_list decorate_refs_exclude = STRING_LIST_INIT_NODUP;
169 static struct string_list decorate_refs_include = STRING_LIST_INIT_NODUP;
170 struct decoration_filter decoration_filter = {&decorate_refs_include,
171 &decorate_refs_exclude};
172 static struct revision_sources revision_sources;
174 const struct option builtin_log_options[] = {
175 OPT__QUIET(&quiet, N_("suppress diff output")),
176 OPT_BOOL(0, "source", &source, N_("show source")),
177 OPT_BOOL(0, "use-mailmap", &mailmap, N_("Use mail map file")),
178 OPT_STRING_LIST(0, "decorate-refs", &decorate_refs_include,
179 N_("pattern"), N_("only decorate refs that match <pattern>")),
180 OPT_STRING_LIST(0, "decorate-refs-exclude", &decorate_refs_exclude,
181 N_("pattern"), N_("do not decorate refs that match <pattern>")),
182 { OPTION_CALLBACK, 0, "decorate", NULL, NULL, N_("decorate options"),
183 PARSE_OPT_OPTARG, decorate_callback},
184 OPT_CALLBACK('L', NULL, &line_cb, "n,m:file",
185 N_("Process line range n,m in file, counting from 1"),
186 log_line_range_callback),
187 OPT_END()
190 line_cb.rev = rev;
191 line_cb.prefix = prefix;
193 mailmap = use_mailmap_config;
194 argc = parse_options(argc, argv, prefix,
195 builtin_log_options, builtin_log_usage,
196 PARSE_OPT_KEEP_ARGV0 | PARSE_OPT_KEEP_UNKNOWN |
197 PARSE_OPT_KEEP_DASHDASH);
199 if (quiet)
200 rev->diffopt.output_format |= DIFF_FORMAT_NO_OUTPUT;
201 argc = setup_revisions(argc, argv, rev, opt);
203 /* Any arguments at this point are not recognized */
204 if (argc > 1)
205 die(_("unrecognized argument: %s"), argv[1]);
207 memset(&w, 0, sizeof(w));
208 userformat_find_requirements(NULL, &w);
210 if (!rev->show_notes_given && (!rev->pretty_given || w.notes))
211 rev->show_notes = 1;
212 if (rev->show_notes)
213 load_display_notes(&rev->notes_opt);
215 if ((rev->diffopt.pickaxe_opts & DIFF_PICKAXE_KINDS_MASK) ||
216 rev->diffopt.filter || rev->diffopt.flags.follow_renames)
217 rev->always_show_header = 0;
219 if (source || w.source) {
220 init_revision_sources(&revision_sources);
221 rev->sources = &revision_sources;
224 if (mailmap) {
225 rev->mailmap = xcalloc(1, sizeof(struct string_list));
226 read_mailmap(rev->mailmap, NULL);
229 if (rev->pretty_given && rev->commit_format == CMIT_FMT_RAW) {
231 * "log --pretty=raw" is special; ignore UI oriented
232 * configuration variables such as decoration.
234 if (!decoration_given)
235 decoration_style = 0;
236 if (!rev->abbrev_commit_given)
237 rev->abbrev_commit = 0;
240 if (decoration_style) {
241 rev->show_decorations = 1;
242 load_ref_decorations(&decoration_filter, decoration_style);
245 if (rev->line_level_traverse)
246 line_log_init(rev, line_cb.prefix, &line_cb.args);
248 setup_pager();
251 static void cmd_log_init(int argc, const char **argv, const char *prefix,
252 struct rev_info *rev, struct setup_revision_opt *opt)
254 cmd_log_init_defaults(rev);
255 cmd_log_init_finish(argc, argv, prefix, rev, opt);
259 * This gives a rough estimate for how many commits we
260 * will print out in the list.
262 static int estimate_commit_count(struct commit_list *list)
264 int n = 0;
266 while (list) {
267 struct commit *commit = list->item;
268 unsigned int flags = commit->object.flags;
269 list = list->next;
270 if (!(flags & (TREESAME | UNINTERESTING)))
271 n++;
273 return n;
276 static void show_early_header(struct rev_info *rev, const char *stage, int nr)
278 if (rev->shown_one) {
279 rev->shown_one = 0;
280 if (rev->commit_format != CMIT_FMT_ONELINE)
281 putchar(rev->diffopt.line_termination);
283 fprintf(rev->diffopt.file, _("Final output: %d %s\n"), nr, stage);
286 static struct itimerval early_output_timer;
288 static void log_show_early(struct rev_info *revs, struct commit_list *list)
290 int i = revs->early_output, close_file = revs->diffopt.close_file;
291 int show_header = 1;
293 revs->diffopt.close_file = 0;
294 sort_in_topological_order(&list, revs->sort_order);
295 while (list && i) {
296 struct commit *commit = list->item;
297 switch (simplify_commit(revs, commit)) {
298 case commit_show:
299 if (show_header) {
300 int n = estimate_commit_count(list);
301 show_early_header(revs, "incomplete", n);
302 show_header = 0;
304 log_tree_commit(revs, commit);
305 i--;
306 break;
307 case commit_ignore:
308 break;
309 case commit_error:
310 if (close_file)
311 fclose(revs->diffopt.file);
312 return;
314 list = list->next;
317 /* Did we already get enough commits for the early output? */
318 if (!i) {
319 if (close_file)
320 fclose(revs->diffopt.file);
321 return;
325 * ..if no, then repeat it twice a second until we
326 * do.
328 * NOTE! We don't use "it_interval", because if the
329 * reader isn't listening, we want our output to be
330 * throttled by the writing, and not have the timer
331 * trigger every second even if we're blocked on a
332 * reader!
334 early_output_timer.it_value.tv_sec = 0;
335 early_output_timer.it_value.tv_usec = 500000;
336 setitimer(ITIMER_REAL, &early_output_timer, NULL);
339 static void early_output(int signal)
341 show_early_output = log_show_early;
344 static void setup_early_output(void)
346 struct sigaction sa;
349 * Set up the signal handler, minimally intrusively:
350 * we only set a single volatile integer word (not
351 * using sigatomic_t - trying to avoid unnecessary
352 * system dependencies and headers), and using
353 * SA_RESTART.
355 memset(&sa, 0, sizeof(sa));
356 sa.sa_handler = early_output;
357 sigemptyset(&sa.sa_mask);
358 sa.sa_flags = SA_RESTART;
359 sigaction(SIGALRM, &sa, NULL);
362 * If we can get the whole output in less than a
363 * tenth of a second, don't even bother doing the
364 * early-output thing..
366 * This is a one-time-only trigger.
368 early_output_timer.it_value.tv_sec = 0;
369 early_output_timer.it_value.tv_usec = 100000;
370 setitimer(ITIMER_REAL, &early_output_timer, NULL);
373 static void finish_early_output(struct rev_info *rev)
375 int n = estimate_commit_count(rev->commits);
376 signal(SIGALRM, SIG_IGN);
377 show_early_header(rev, "done", n);
380 static int cmd_log_walk(struct rev_info *rev)
382 struct commit *commit;
383 int saved_nrl = 0;
384 int saved_dcctc = 0, close_file = rev->diffopt.close_file;
386 if (rev->early_output)
387 setup_early_output();
389 if (prepare_revision_walk(rev))
390 die(_("revision walk setup failed"));
392 if (rev->early_output)
393 finish_early_output(rev);
396 * For --check and --exit-code, the exit code is based on CHECK_FAILED
397 * and HAS_CHANGES being accumulated in rev->diffopt, so be careful to
398 * retain that state information if replacing rev->diffopt in this loop
400 rev->diffopt.close_file = 0;
401 while ((commit = get_revision(rev)) != NULL) {
402 if (!log_tree_commit(rev, commit) && rev->max_count >= 0)
404 * We decremented max_count in get_revision,
405 * but we didn't actually show the commit.
407 rev->max_count++;
408 if (!rev->reflog_info) {
410 * We may show a given commit multiple times when
411 * walking the reflogs.
413 free_commit_buffer(the_repository->parsed_objects,
414 commit);
415 free_commit_list(commit->parents);
416 commit->parents = NULL;
418 if (saved_nrl < rev->diffopt.needed_rename_limit)
419 saved_nrl = rev->diffopt.needed_rename_limit;
420 if (rev->diffopt.degraded_cc_to_c)
421 saved_dcctc = 1;
423 rev->diffopt.degraded_cc_to_c = saved_dcctc;
424 rev->diffopt.needed_rename_limit = saved_nrl;
425 if (close_file)
426 fclose(rev->diffopt.file);
428 if (rev->diffopt.output_format & DIFF_FORMAT_CHECKDIFF &&
429 rev->diffopt.flags.check_failed) {
430 return 02;
432 return diff_result_code(&rev->diffopt, 0);
435 static int git_log_config(const char *var, const char *value, void *cb)
437 const char *slot_name;
439 if (!strcmp(var, "format.pretty"))
440 return git_config_string(&fmt_pretty, var, value);
441 if (!strcmp(var, "format.subjectprefix"))
442 return git_config_string(&fmt_patch_subject_prefix, var, value);
443 if (!strcmp(var, "format.encodeemailheaders")) {
444 default_encode_email_headers = git_config_bool(var, value);
445 return 0;
447 if (!strcmp(var, "log.abbrevcommit")) {
448 default_abbrev_commit = git_config_bool(var, value);
449 return 0;
451 if (!strcmp(var, "log.date"))
452 return git_config_string(&default_date_mode, var, value);
453 if (!strcmp(var, "log.decorate")) {
454 decoration_style = parse_decoration_style(value);
455 if (decoration_style < 0)
456 decoration_style = 0; /* maybe warn? */
457 return 0;
459 if (!strcmp(var, "log.showroot")) {
460 default_show_root = git_config_bool(var, value);
461 return 0;
463 if (!strcmp(var, "log.follow")) {
464 default_follow = git_config_bool(var, value);
465 return 0;
467 if (skip_prefix(var, "color.decorate.", &slot_name))
468 return parse_decorate_color_config(var, slot_name, value);
469 if (!strcmp(var, "log.mailmap")) {
470 use_mailmap_config = git_config_bool(var, value);
471 return 0;
473 if (!strcmp(var, "log.showsignature")) {
474 default_show_signature = git_config_bool(var, value);
475 return 0;
478 if (grep_config(var, value, cb) < 0)
479 return -1;
480 if (git_gpg_config(var, value, cb) < 0)
481 return -1;
482 return git_diff_ui_config(var, value, cb);
485 int cmd_whatchanged(int argc, const char **argv, const char *prefix)
487 struct rev_info rev;
488 struct setup_revision_opt opt;
490 init_log_defaults();
491 git_config(git_log_config, NULL);
493 repo_init_revisions(the_repository, &rev, prefix);
494 rev.diff = 1;
495 rev.simplify_history = 0;
496 memset(&opt, 0, sizeof(opt));
497 opt.def = "HEAD";
498 opt.revarg_opt = REVARG_COMMITTISH;
499 cmd_log_init(argc, argv, prefix, &rev, &opt);
500 if (!rev.diffopt.output_format)
501 rev.diffopt.output_format = DIFF_FORMAT_RAW;
502 return cmd_log_walk(&rev);
505 static void show_tagger(const char *buf, struct rev_info *rev)
507 struct strbuf out = STRBUF_INIT;
508 struct pretty_print_context pp = {0};
510 pp.fmt = rev->commit_format;
511 pp.date_mode = rev->date_mode;
512 pp_user_info(&pp, "Tagger", &out, buf, get_log_output_encoding());
513 fprintf(rev->diffopt.file, "%s", out.buf);
514 strbuf_release(&out);
517 static int show_blob_object(const struct object_id *oid, struct rev_info *rev, const char *obj_name)
519 struct object_id oidc;
520 struct object_context obj_context;
521 char *buf;
522 unsigned long size;
524 fflush(rev->diffopt.file);
525 if (!rev->diffopt.flags.textconv_set_via_cmdline ||
526 !rev->diffopt.flags.allow_textconv)
527 return stream_blob_to_fd(1, oid, NULL, 0);
529 if (get_oid_with_context(the_repository, obj_name,
530 GET_OID_RECORD_PATH,
531 &oidc, &obj_context))
532 die(_("not a valid object name %s"), obj_name);
533 if (!obj_context.path ||
534 !textconv_object(the_repository, obj_context.path,
535 obj_context.mode, &oidc, 1, &buf, &size)) {
536 free(obj_context.path);
537 return stream_blob_to_fd(1, oid, NULL, 0);
540 if (!buf)
541 die(_("git show %s: bad file"), obj_name);
543 write_or_die(1, buf, size);
544 free(obj_context.path);
545 return 0;
548 static int show_tag_object(const struct object_id *oid, struct rev_info *rev)
550 unsigned long size;
551 enum object_type type;
552 char *buf = read_object_file(oid, &type, &size);
553 int offset = 0;
555 if (!buf)
556 return error(_("could not read object %s"), oid_to_hex(oid));
558 assert(type == OBJ_TAG);
559 while (offset < size && buf[offset] != '\n') {
560 int new_offset = offset + 1;
561 const char *ident;
562 while (new_offset < size && buf[new_offset++] != '\n')
563 ; /* do nothing */
564 if (skip_prefix(buf + offset, "tagger ", &ident))
565 show_tagger(ident, rev);
566 offset = new_offset;
569 if (offset < size)
570 fwrite(buf + offset, size - offset, 1, rev->diffopt.file);
571 free(buf);
572 return 0;
575 static int show_tree_object(const struct object_id *oid,
576 struct strbuf *base,
577 const char *pathname, unsigned mode, int stage, void *context)
579 FILE *file = context;
580 fprintf(file, "%s%s\n", pathname, S_ISDIR(mode) ? "/" : "");
581 return 0;
584 static void show_setup_revisions_tweak(struct rev_info *rev,
585 struct setup_revision_opt *opt)
587 if (rev->ignore_merges) {
588 /* There was no "-m" on the command line */
589 rev->ignore_merges = 0;
590 if (!rev->first_parent_only && !rev->combine_merges) {
591 /* No "--first-parent", "-c", or "--cc" */
592 rev->combine_merges = 1;
593 rev->dense_combined_merges = 1;
596 if (!rev->diffopt.output_format)
597 rev->diffopt.output_format = DIFF_FORMAT_PATCH;
600 int cmd_show(int argc, const char **argv, const char *prefix)
602 struct rev_info rev;
603 struct object_array_entry *objects;
604 struct setup_revision_opt opt;
605 struct pathspec match_all;
606 int i, count, ret = 0;
608 init_log_defaults();
609 git_config(git_log_config, NULL);
611 memset(&match_all, 0, sizeof(match_all));
612 repo_init_revisions(the_repository, &rev, prefix);
613 rev.diff = 1;
614 rev.always_show_header = 1;
615 rev.no_walk = REVISION_WALK_NO_WALK_SORTED;
616 rev.diffopt.stat_width = -1; /* Scale to real terminal size */
618 memset(&opt, 0, sizeof(opt));
619 opt.def = "HEAD";
620 opt.tweak = show_setup_revisions_tweak;
621 cmd_log_init(argc, argv, prefix, &rev, &opt);
623 if (!rev.no_walk)
624 return cmd_log_walk(&rev);
626 count = rev.pending.nr;
627 objects = rev.pending.objects;
628 for (i = 0; i < count && !ret; i++) {
629 struct object *o = objects[i].item;
630 const char *name = objects[i].name;
631 switch (o->type) {
632 case OBJ_BLOB:
633 ret = show_blob_object(&o->oid, &rev, name);
634 break;
635 case OBJ_TAG: {
636 struct tag *t = (struct tag *)o;
637 struct object_id *oid = get_tagged_oid(t);
639 if (rev.shown_one)
640 putchar('\n');
641 fprintf(rev.diffopt.file, "%stag %s%s\n",
642 diff_get_color_opt(&rev.diffopt, DIFF_COMMIT),
643 t->tag,
644 diff_get_color_opt(&rev.diffopt, DIFF_RESET));
645 ret = show_tag_object(&o->oid, &rev);
646 rev.shown_one = 1;
647 if (ret)
648 break;
649 o = parse_object(the_repository, oid);
650 if (!o)
651 ret = error(_("could not read object %s"),
652 oid_to_hex(oid));
653 objects[i].item = o;
654 i--;
655 break;
657 case OBJ_TREE:
658 if (rev.shown_one)
659 putchar('\n');
660 fprintf(rev.diffopt.file, "%stree %s%s\n\n",
661 diff_get_color_opt(&rev.diffopt, DIFF_COMMIT),
662 name,
663 diff_get_color_opt(&rev.diffopt, DIFF_RESET));
664 read_tree_recursive(the_repository, (struct tree *)o, "",
665 0, 0, &match_all, show_tree_object,
666 rev.diffopt.file);
667 rev.shown_one = 1;
668 break;
669 case OBJ_COMMIT:
670 rev.pending.nr = rev.pending.alloc = 0;
671 rev.pending.objects = NULL;
672 add_object_array(o, name, &rev.pending);
673 ret = cmd_log_walk(&rev);
674 break;
675 default:
676 ret = error(_("unknown type: %d"), o->type);
679 free(objects);
680 return ret;
684 * This is equivalent to "git log -g --abbrev-commit --pretty=oneline"
686 int cmd_log_reflog(int argc, const char **argv, const char *prefix)
688 struct rev_info rev;
689 struct setup_revision_opt opt;
691 init_log_defaults();
692 git_config(git_log_config, NULL);
694 repo_init_revisions(the_repository, &rev, prefix);
695 init_reflog_walk(&rev.reflog_info);
696 rev.verbose_header = 1;
697 memset(&opt, 0, sizeof(opt));
698 opt.def = "HEAD";
699 cmd_log_init_defaults(&rev);
700 rev.abbrev_commit = 1;
701 rev.commit_format = CMIT_FMT_ONELINE;
702 rev.use_terminator = 1;
703 rev.always_show_header = 1;
704 cmd_log_init_finish(argc, argv, prefix, &rev, &opt);
706 return cmd_log_walk(&rev);
709 static void log_setup_revisions_tweak(struct rev_info *rev,
710 struct setup_revision_opt *opt)
712 if (rev->diffopt.flags.default_follow_renames &&
713 rev->prune_data.nr == 1)
714 rev->diffopt.flags.follow_renames = 1;
716 /* Turn --cc/-c into -p --cc/-c when -p was not given */
717 if (!rev->diffopt.output_format && rev->combine_merges)
718 rev->diffopt.output_format = DIFF_FORMAT_PATCH;
720 /* Turn -m on when --cc/-c was given */
721 if (rev->combine_merges)
722 rev->ignore_merges = 0;
725 int cmd_log(int argc, const char **argv, const char *prefix)
727 struct rev_info rev;
728 struct setup_revision_opt opt;
730 init_log_defaults();
731 git_config(git_log_config, NULL);
733 repo_init_revisions(the_repository, &rev, prefix);
734 rev.always_show_header = 1;
735 memset(&opt, 0, sizeof(opt));
736 opt.def = "HEAD";
737 opt.revarg_opt = REVARG_COMMITTISH;
738 opt.tweak = log_setup_revisions_tweak;
739 cmd_log_init(argc, argv, prefix, &rev, &opt);
740 return cmd_log_walk(&rev);
743 /* format-patch */
745 static const char *fmt_patch_suffix = ".patch";
746 static int numbered = 0;
747 static int auto_number = 1;
749 static char *default_attach = NULL;
751 static struct string_list extra_hdr = STRING_LIST_INIT_NODUP;
752 static struct string_list extra_to = STRING_LIST_INIT_NODUP;
753 static struct string_list extra_cc = STRING_LIST_INIT_NODUP;
755 static void add_header(const char *value)
757 struct string_list_item *item;
758 int len = strlen(value);
759 while (len && value[len - 1] == '\n')
760 len--;
762 if (!strncasecmp(value, "to: ", 4)) {
763 item = string_list_append(&extra_to, value + 4);
764 len -= 4;
765 } else if (!strncasecmp(value, "cc: ", 4)) {
766 item = string_list_append(&extra_cc, value + 4);
767 len -= 4;
768 } else {
769 item = string_list_append(&extra_hdr, value);
772 item->string[len] = '\0';
775 enum cover_setting {
776 COVER_UNSET,
777 COVER_OFF,
778 COVER_ON,
779 COVER_AUTO
782 enum thread_level {
783 THREAD_UNSET,
784 THREAD_SHALLOW,
785 THREAD_DEEP
788 enum cover_from_description {
789 COVER_FROM_NONE,
790 COVER_FROM_MESSAGE,
791 COVER_FROM_SUBJECT,
792 COVER_FROM_AUTO
795 static enum thread_level thread;
796 static int do_signoff;
797 static int base_auto;
798 static char *from;
799 static const char *signature = git_version_string;
800 static const char *signature_file;
801 static enum cover_setting config_cover_letter;
802 static const char *config_output_directory;
803 static enum cover_from_description cover_from_description_mode = COVER_FROM_MESSAGE;
804 static int show_notes;
805 static struct display_notes_opt notes_opt;
807 static enum cover_from_description parse_cover_from_description(const char *arg)
809 if (!arg || !strcmp(arg, "default"))
810 return COVER_FROM_MESSAGE;
811 else if (!strcmp(arg, "none"))
812 return COVER_FROM_NONE;
813 else if (!strcmp(arg, "message"))
814 return COVER_FROM_MESSAGE;
815 else if (!strcmp(arg, "subject"))
816 return COVER_FROM_SUBJECT;
817 else if (!strcmp(arg, "auto"))
818 return COVER_FROM_AUTO;
819 else
820 die(_("%s: invalid cover from description mode"), arg);
823 static int git_format_config(const char *var, const char *value, void *cb)
825 if (!strcmp(var, "format.headers")) {
826 if (!value)
827 die(_("format.headers without value"));
828 add_header(value);
829 return 0;
831 if (!strcmp(var, "format.suffix"))
832 return git_config_string(&fmt_patch_suffix, var, value);
833 if (!strcmp(var, "format.to")) {
834 if (!value)
835 return config_error_nonbool(var);
836 string_list_append(&extra_to, value);
837 return 0;
839 if (!strcmp(var, "format.cc")) {
840 if (!value)
841 return config_error_nonbool(var);
842 string_list_append(&extra_cc, value);
843 return 0;
845 if (!strcmp(var, "diff.color") || !strcmp(var, "color.diff") ||
846 !strcmp(var, "color.ui") || !strcmp(var, "diff.submodule")) {
847 return 0;
849 if (!strcmp(var, "format.numbered")) {
850 if (value && !strcasecmp(value, "auto")) {
851 auto_number = 1;
852 return 0;
854 numbered = git_config_bool(var, value);
855 auto_number = auto_number && numbered;
856 return 0;
858 if (!strcmp(var, "format.attach")) {
859 if (value && *value)
860 default_attach = xstrdup(value);
861 else
862 default_attach = xstrdup(git_version_string);
863 return 0;
865 if (!strcmp(var, "format.thread")) {
866 if (value && !strcasecmp(value, "deep")) {
867 thread = THREAD_DEEP;
868 return 0;
870 if (value && !strcasecmp(value, "shallow")) {
871 thread = THREAD_SHALLOW;
872 return 0;
874 thread = git_config_bool(var, value) ? THREAD_SHALLOW : THREAD_UNSET;
875 return 0;
877 if (!strcmp(var, "format.signoff")) {
878 do_signoff = git_config_bool(var, value);
879 return 0;
881 if (!strcmp(var, "format.signature"))
882 return git_config_string(&signature, var, value);
883 if (!strcmp(var, "format.signaturefile"))
884 return git_config_pathname(&signature_file, var, value);
885 if (!strcmp(var, "format.coverletter")) {
886 if (value && !strcasecmp(value, "auto")) {
887 config_cover_letter = COVER_AUTO;
888 return 0;
890 config_cover_letter = git_config_bool(var, value) ? COVER_ON : COVER_OFF;
891 return 0;
893 if (!strcmp(var, "format.outputdirectory"))
894 return git_config_string(&config_output_directory, var, value);
895 if (!strcmp(var, "format.useautobase")) {
896 base_auto = git_config_bool(var, value);
897 return 0;
899 if (!strcmp(var, "format.from")) {
900 int b = git_parse_maybe_bool(value);
901 free(from);
902 if (b < 0)
903 from = xstrdup(value);
904 else if (b)
905 from = xstrdup(git_committer_info(IDENT_NO_DATE));
906 else
907 from = NULL;
908 return 0;
910 if (!strcmp(var, "format.notes")) {
911 int b = git_parse_maybe_bool(value);
912 if (b < 0)
913 enable_ref_display_notes(&notes_opt, &show_notes, value);
914 else if (b)
915 enable_default_display_notes(&notes_opt, &show_notes);
916 else
917 disable_display_notes(&notes_opt, &show_notes);
918 return 0;
920 if (!strcmp(var, "format.coverfromdescription")) {
921 cover_from_description_mode = parse_cover_from_description(value);
922 return 0;
925 return git_log_config(var, value, cb);
928 static const char *output_directory = NULL;
929 static int outdir_offset;
931 static int open_next_file(struct commit *commit, const char *subject,
932 struct rev_info *rev, int quiet)
934 struct strbuf filename = STRBUF_INIT;
935 int suffix_len = strlen(rev->patch_suffix) + 1;
937 if (output_directory) {
938 strbuf_addstr(&filename, output_directory);
939 if (filename.len >=
940 PATH_MAX - FORMAT_PATCH_NAME_MAX - suffix_len) {
941 strbuf_release(&filename);
942 return error(_("name of output directory is too long"));
944 strbuf_complete(&filename, '/');
947 if (rev->numbered_files)
948 strbuf_addf(&filename, "%d", rev->nr);
949 else if (commit)
950 fmt_output_commit(&filename, commit, rev);
951 else
952 fmt_output_subject(&filename, subject, rev);
954 if (!quiet)
955 printf("%s\n", filename.buf + outdir_offset);
957 if ((rev->diffopt.file = fopen(filename.buf, "w")) == NULL) {
958 error_errno(_("cannot open patch file %s"), filename.buf);
959 strbuf_release(&filename);
960 return -1;
963 strbuf_release(&filename);
964 return 0;
967 static void get_patch_ids(struct rev_info *rev, struct patch_ids *ids)
969 struct rev_info check_rev;
970 struct commit *commit, *c1, *c2;
971 struct object *o1, *o2;
972 unsigned flags1, flags2;
974 if (rev->pending.nr != 2)
975 die(_("need exactly one range"));
977 o1 = rev->pending.objects[0].item;
978 o2 = rev->pending.objects[1].item;
979 flags1 = o1->flags;
980 flags2 = o2->flags;
981 c1 = lookup_commit_reference(the_repository, &o1->oid);
982 c2 = lookup_commit_reference(the_repository, &o2->oid);
984 if ((flags1 & UNINTERESTING) == (flags2 & UNINTERESTING))
985 die(_("not a range"));
987 init_patch_ids(the_repository, ids);
989 /* given a range a..b get all patch ids for b..a */
990 repo_init_revisions(the_repository, &check_rev, rev->prefix);
991 check_rev.max_parents = 1;
992 o1->flags ^= UNINTERESTING;
993 o2->flags ^= UNINTERESTING;
994 add_pending_object(&check_rev, o1, "o1");
995 add_pending_object(&check_rev, o2, "o2");
996 if (prepare_revision_walk(&check_rev))
997 die(_("revision walk setup failed"));
999 while ((commit = get_revision(&check_rev)) != NULL) {
1000 add_commit_patch_id(commit, ids);
1003 /* reset for next revision walk */
1004 clear_commit_marks(c1, SEEN | UNINTERESTING | SHOWN | ADDED);
1005 clear_commit_marks(c2, SEEN | UNINTERESTING | SHOWN | ADDED);
1006 o1->flags = flags1;
1007 o2->flags = flags2;
1010 static void gen_message_id(struct rev_info *info, char *base)
1012 struct strbuf buf = STRBUF_INIT;
1013 strbuf_addf(&buf, "%s.%"PRItime".git.%s", base,
1014 (timestamp_t) time(NULL),
1015 git_committer_info(IDENT_NO_NAME|IDENT_NO_DATE|IDENT_STRICT));
1016 info->message_id = strbuf_detach(&buf, NULL);
1019 static void print_signature(FILE *file)
1021 if (!signature || !*signature)
1022 return;
1024 fprintf(file, "-- \n%s", signature);
1025 if (signature[strlen(signature)-1] != '\n')
1026 putc('\n', file);
1027 putc('\n', file);
1030 static char *find_branch_name(struct rev_info *rev)
1032 int i, positive = -1;
1033 struct object_id branch_oid;
1034 const struct object_id *tip_oid;
1035 const char *ref, *v;
1036 char *full_ref, *branch = NULL;
1038 for (i = 0; i < rev->cmdline.nr; i++) {
1039 if (rev->cmdline.rev[i].flags & UNINTERESTING)
1040 continue;
1041 if (positive < 0)
1042 positive = i;
1043 else
1044 return NULL;
1046 if (positive < 0)
1047 return NULL;
1048 ref = rev->cmdline.rev[positive].name;
1049 tip_oid = &rev->cmdline.rev[positive].item->oid;
1050 if (dwim_ref(ref, strlen(ref), &branch_oid, &full_ref) &&
1051 skip_prefix(full_ref, "refs/heads/", &v) &&
1052 oideq(tip_oid, &branch_oid))
1053 branch = xstrdup(v);
1054 free(full_ref);
1055 return branch;
1058 static void show_diffstat(struct rev_info *rev,
1059 struct commit *origin, struct commit *head)
1061 struct diff_options opts;
1063 memcpy(&opts, &rev->diffopt, sizeof(opts));
1064 opts.output_format = DIFF_FORMAT_SUMMARY | DIFF_FORMAT_DIFFSTAT;
1065 diff_setup_done(&opts);
1067 diff_tree_oid(get_commit_tree_oid(origin),
1068 get_commit_tree_oid(head),
1069 "", &opts);
1070 diffcore_std(&opts);
1071 diff_flush(&opts);
1073 fprintf(rev->diffopt.file, "\n");
1076 static void prepare_cover_text(struct pretty_print_context *pp,
1077 const char *branch_name,
1078 struct strbuf *sb,
1079 const char *encoding,
1080 int need_8bit_cte)
1082 const char *subject = "*** SUBJECT HERE ***";
1083 const char *body = "*** BLURB HERE ***";
1084 struct strbuf description_sb = STRBUF_INIT;
1085 struct strbuf subject_sb = STRBUF_INIT;
1087 if (cover_from_description_mode == COVER_FROM_NONE)
1088 goto do_pp;
1090 if (branch_name && *branch_name)
1091 read_branch_desc(&description_sb, branch_name);
1092 if (!description_sb.len)
1093 goto do_pp;
1095 if (cover_from_description_mode == COVER_FROM_SUBJECT ||
1096 cover_from_description_mode == COVER_FROM_AUTO)
1097 body = format_subject(&subject_sb, description_sb.buf, " ");
1099 if (cover_from_description_mode == COVER_FROM_MESSAGE ||
1100 (cover_from_description_mode == COVER_FROM_AUTO &&
1101 subject_sb.len > COVER_FROM_AUTO_MAX_SUBJECT_LEN))
1102 body = description_sb.buf;
1103 else
1104 subject = subject_sb.buf;
1106 do_pp:
1107 pp_title_line(pp, &subject, sb, encoding, need_8bit_cte);
1108 pp_remainder(pp, &body, sb, 0);
1110 strbuf_release(&description_sb);
1111 strbuf_release(&subject_sb);
1114 static int get_notes_refs(struct string_list_item *item, void *arg)
1116 argv_array_pushf(arg, "--notes=%s", item->string);
1117 return 0;
1120 static void get_notes_args(struct argv_array *arg, struct rev_info *rev)
1122 if (!rev->show_notes) {
1123 argv_array_push(arg, "--no-notes");
1124 } else if (rev->notes_opt.use_default_notes > 0 ||
1125 (rev->notes_opt.use_default_notes == -1 &&
1126 !rev->notes_opt.extra_notes_refs.nr)) {
1127 argv_array_push(arg, "--notes");
1128 } else {
1129 for_each_string_list(&rev->notes_opt.extra_notes_refs, get_notes_refs, arg);
1133 static void make_cover_letter(struct rev_info *rev, int use_stdout,
1134 struct commit *origin,
1135 int nr, struct commit **list,
1136 const char *branch_name,
1137 int quiet)
1139 const char *committer;
1140 struct shortlog log;
1141 struct strbuf sb = STRBUF_INIT;
1142 int i;
1143 const char *encoding = "UTF-8";
1144 int need_8bit_cte = 0;
1145 struct pretty_print_context pp = {0};
1146 struct commit *head = list[0];
1148 if (!cmit_fmt_is_mail(rev->commit_format))
1149 die(_("cover letter needs email format"));
1151 committer = git_committer_info(0);
1153 if (!use_stdout &&
1154 open_next_file(NULL, rev->numbered_files ? NULL : "cover-letter", rev, quiet))
1155 die(_("failed to create cover-letter file"));
1157 log_write_email_headers(rev, head, &pp.after_subject, &need_8bit_cte, 0);
1159 for (i = 0; !need_8bit_cte && i < nr; i++) {
1160 const char *buf = get_commit_buffer(list[i], NULL);
1161 if (has_non_ascii(buf))
1162 need_8bit_cte = 1;
1163 unuse_commit_buffer(list[i], buf);
1166 if (!branch_name)
1167 branch_name = find_branch_name(rev);
1169 pp.fmt = CMIT_FMT_EMAIL;
1170 pp.date_mode.type = DATE_RFC2822;
1171 pp.rev = rev;
1172 pp.print_email_subject = 1;
1173 pp_user_info(&pp, NULL, &sb, committer, encoding);
1174 prepare_cover_text(&pp, branch_name, &sb, encoding, need_8bit_cte);
1175 fprintf(rev->diffopt.file, "%s\n", sb.buf);
1177 strbuf_release(&sb);
1179 shortlog_init(&log);
1180 log.wrap_lines = 1;
1181 log.wrap = MAIL_DEFAULT_WRAP;
1182 log.in1 = 2;
1183 log.in2 = 4;
1184 log.file = rev->diffopt.file;
1185 for (i = 0; i < nr; i++)
1186 shortlog_add_commit(&log, list[i]);
1188 shortlog_output(&log);
1190 /* We can only do diffstat with a unique reference point */
1191 if (origin)
1192 show_diffstat(rev, origin, head);
1194 if (rev->idiff_oid1) {
1195 fprintf_ln(rev->diffopt.file, "%s", rev->idiff_title);
1196 show_interdiff(rev, 0);
1199 if (rev->rdiff1) {
1201 * Pass minimum required diff-options to range-diff; others
1202 * can be added later if deemed desirable.
1204 struct diff_options opts;
1205 struct argv_array other_arg = ARGV_ARRAY_INIT;
1206 diff_setup(&opts);
1207 opts.file = rev->diffopt.file;
1208 opts.use_color = rev->diffopt.use_color;
1209 diff_setup_done(&opts);
1210 fprintf_ln(rev->diffopt.file, "%s", rev->rdiff_title);
1211 get_notes_args(&other_arg, rev);
1212 show_range_diff(rev->rdiff1, rev->rdiff2,
1213 rev->creation_factor, 1, &opts, &other_arg);
1214 argv_array_clear(&other_arg);
1218 static const char *clean_message_id(const char *msg_id)
1220 char ch;
1221 const char *a, *z, *m;
1223 m = msg_id;
1224 while ((ch = *m) && (isspace(ch) || (ch == '<')))
1225 m++;
1226 a = m;
1227 z = NULL;
1228 while ((ch = *m)) {
1229 if (!isspace(ch) && (ch != '>'))
1230 z = m;
1231 m++;
1233 if (!z)
1234 die(_("insane in-reply-to: %s"), msg_id);
1235 if (++z == m)
1236 return a;
1237 return xmemdupz(a, z - a);
1240 static const char *set_outdir(const char *prefix, const char *output_directory)
1242 if (output_directory && is_absolute_path(output_directory))
1243 return output_directory;
1245 if (!prefix || !*prefix) {
1246 if (output_directory)
1247 return output_directory;
1248 /* The user did not explicitly ask for "./" */
1249 outdir_offset = 2;
1250 return "./";
1253 outdir_offset = strlen(prefix);
1254 if (!output_directory)
1255 return prefix;
1257 return prefix_filename(prefix, output_directory);
1260 static const char * const builtin_format_patch_usage[] = {
1261 N_("git format-patch [<options>] [<since> | <revision-range>]"),
1262 NULL
1265 static int keep_subject = 0;
1267 static int keep_callback(const struct option *opt, const char *arg, int unset)
1269 BUG_ON_OPT_NEG(unset);
1270 BUG_ON_OPT_ARG(arg);
1271 ((struct rev_info *)opt->value)->total = -1;
1272 keep_subject = 1;
1273 return 0;
1276 static int subject_prefix = 0;
1278 static int subject_prefix_callback(const struct option *opt, const char *arg,
1279 int unset)
1281 BUG_ON_OPT_NEG(unset);
1282 subject_prefix = 1;
1283 ((struct rev_info *)opt->value)->subject_prefix = arg;
1284 return 0;
1287 static int rfc_callback(const struct option *opt, const char *arg, int unset)
1289 BUG_ON_OPT_NEG(unset);
1290 BUG_ON_OPT_ARG(arg);
1291 return subject_prefix_callback(opt, "RFC PATCH", unset);
1294 static int numbered_cmdline_opt = 0;
1296 static int numbered_callback(const struct option *opt, const char *arg,
1297 int unset)
1299 BUG_ON_OPT_ARG(arg);
1300 *(int *)opt->value = numbered_cmdline_opt = unset ? 0 : 1;
1301 if (unset)
1302 auto_number = 0;
1303 return 0;
1306 static int no_numbered_callback(const struct option *opt, const char *arg,
1307 int unset)
1309 BUG_ON_OPT_NEG(unset);
1310 return numbered_callback(opt, arg, 1);
1313 static int output_directory_callback(const struct option *opt, const char *arg,
1314 int unset)
1316 const char **dir = (const char **)opt->value;
1317 BUG_ON_OPT_NEG(unset);
1318 if (*dir)
1319 die(_("two output directories?"));
1320 *dir = arg;
1321 return 0;
1324 static int thread_callback(const struct option *opt, const char *arg, int unset)
1326 enum thread_level *thread = (enum thread_level *)opt->value;
1327 if (unset)
1328 *thread = THREAD_UNSET;
1329 else if (!arg || !strcmp(arg, "shallow"))
1330 *thread = THREAD_SHALLOW;
1331 else if (!strcmp(arg, "deep"))
1332 *thread = THREAD_DEEP;
1334 * Please update _git_formatpatch() in git-completion.bash
1335 * when you add new options.
1337 else
1338 return 1;
1339 return 0;
1342 static int attach_callback(const struct option *opt, const char *arg, int unset)
1344 struct rev_info *rev = (struct rev_info *)opt->value;
1345 if (unset)
1346 rev->mime_boundary = NULL;
1347 else if (arg)
1348 rev->mime_boundary = arg;
1349 else
1350 rev->mime_boundary = git_version_string;
1351 rev->no_inline = unset ? 0 : 1;
1352 return 0;
1355 static int inline_callback(const struct option *opt, const char *arg, int unset)
1357 struct rev_info *rev = (struct rev_info *)opt->value;
1358 if (unset)
1359 rev->mime_boundary = NULL;
1360 else if (arg)
1361 rev->mime_boundary = arg;
1362 else
1363 rev->mime_boundary = git_version_string;
1364 rev->no_inline = 0;
1365 return 0;
1368 static int header_callback(const struct option *opt, const char *arg, int unset)
1370 if (unset) {
1371 string_list_clear(&extra_hdr, 0);
1372 string_list_clear(&extra_to, 0);
1373 string_list_clear(&extra_cc, 0);
1374 } else {
1375 add_header(arg);
1377 return 0;
1380 static int to_callback(const struct option *opt, const char *arg, int unset)
1382 if (unset)
1383 string_list_clear(&extra_to, 0);
1384 else
1385 string_list_append(&extra_to, arg);
1386 return 0;
1389 static int cc_callback(const struct option *opt, const char *arg, int unset)
1391 if (unset)
1392 string_list_clear(&extra_cc, 0);
1393 else
1394 string_list_append(&extra_cc, arg);
1395 return 0;
1398 static int from_callback(const struct option *opt, const char *arg, int unset)
1400 char **from = opt->value;
1402 free(*from);
1404 if (unset)
1405 *from = NULL;
1406 else if (arg)
1407 *from = xstrdup(arg);
1408 else
1409 *from = xstrdup(git_committer_info(IDENT_NO_DATE));
1410 return 0;
1413 struct base_tree_info {
1414 struct object_id base_commit;
1415 int nr_patch_id, alloc_patch_id;
1416 struct object_id *patch_id;
1419 static struct commit *get_base_commit(const char *base_commit,
1420 struct commit **list,
1421 int total)
1423 struct commit *base = NULL;
1424 struct commit **rev;
1425 int i = 0, rev_nr = 0;
1427 if (base_commit && strcmp(base_commit, "auto")) {
1428 base = lookup_commit_reference_by_name(base_commit);
1429 if (!base)
1430 die(_("unknown commit %s"), base_commit);
1431 } else if ((base_commit && !strcmp(base_commit, "auto"))) {
1432 struct branch *curr_branch = branch_get(NULL);
1433 const char *upstream = branch_get_upstream(curr_branch, NULL);
1434 if (upstream) {
1435 struct commit_list *base_list;
1436 struct commit *commit;
1437 struct object_id oid;
1439 if (get_oid(upstream, &oid))
1440 die(_("failed to resolve '%s' as a valid ref"), upstream);
1441 commit = lookup_commit_or_die(&oid, "upstream base");
1442 base_list = get_merge_bases_many(commit, total, list);
1443 /* There should be one and only one merge base. */
1444 if (!base_list || base_list->next)
1445 die(_("could not find exact merge base"));
1446 base = base_list->item;
1447 free_commit_list(base_list);
1448 } else {
1449 die(_("failed to get upstream, if you want to record base commit automatically,\n"
1450 "please use git branch --set-upstream-to to track a remote branch.\n"
1451 "Or you could specify base commit by --base=<base-commit-id> manually"));
1455 ALLOC_ARRAY(rev, total);
1456 for (i = 0; i < total; i++)
1457 rev[i] = list[i];
1459 rev_nr = total;
1461 * Get merge base through pair-wise computations
1462 * and store it in rev[0].
1464 while (rev_nr > 1) {
1465 for (i = 0; i < rev_nr / 2; i++) {
1466 struct commit_list *merge_base;
1467 merge_base = get_merge_bases(rev[2 * i], rev[2 * i + 1]);
1468 if (!merge_base || merge_base->next)
1469 die(_("failed to find exact merge base"));
1471 rev[i] = merge_base->item;
1474 if (rev_nr % 2)
1475 rev[i] = rev[2 * i];
1476 rev_nr = DIV_ROUND_UP(rev_nr, 2);
1479 if (!in_merge_bases(base, rev[0]))
1480 die(_("base commit should be the ancestor of revision list"));
1482 for (i = 0; i < total; i++) {
1483 if (base == list[i])
1484 die(_("base commit shouldn't be in revision list"));
1487 free(rev);
1488 return base;
1491 define_commit_slab(commit_base, int);
1493 static void prepare_bases(struct base_tree_info *bases,
1494 struct commit *base,
1495 struct commit **list,
1496 int total)
1498 struct commit *commit;
1499 struct rev_info revs;
1500 struct diff_options diffopt;
1501 struct commit_base commit_base;
1502 int i;
1504 if (!base)
1505 return;
1507 init_commit_base(&commit_base);
1508 repo_diff_setup(the_repository, &diffopt);
1509 diffopt.flags.recursive = 1;
1510 diff_setup_done(&diffopt);
1512 oidcpy(&bases->base_commit, &base->object.oid);
1514 repo_init_revisions(the_repository, &revs, NULL);
1515 revs.max_parents = 1;
1516 revs.topo_order = 1;
1517 for (i = 0; i < total; i++) {
1518 list[i]->object.flags &= ~UNINTERESTING;
1519 add_pending_object(&revs, &list[i]->object, "rev_list");
1520 *commit_base_at(&commit_base, list[i]) = 1;
1522 base->object.flags |= UNINTERESTING;
1523 add_pending_object(&revs, &base->object, "base");
1525 if (prepare_revision_walk(&revs))
1526 die(_("revision walk setup failed"));
1528 * Traverse the commits list, get prerequisite patch ids
1529 * and stuff them in bases structure.
1531 while ((commit = get_revision(&revs)) != NULL) {
1532 struct object_id oid;
1533 struct object_id *patch_id;
1534 if (*commit_base_at(&commit_base, commit))
1535 continue;
1536 if (commit_patch_id(commit, &diffopt, &oid, 0, 1))
1537 die(_("cannot get patch id"));
1538 ALLOC_GROW(bases->patch_id, bases->nr_patch_id + 1, bases->alloc_patch_id);
1539 patch_id = bases->patch_id + bases->nr_patch_id;
1540 oidcpy(patch_id, &oid);
1541 bases->nr_patch_id++;
1543 clear_commit_base(&commit_base);
1546 static void print_bases(struct base_tree_info *bases, FILE *file)
1548 int i;
1550 /* Only do this once, either for the cover or for the first one */
1551 if (is_null_oid(&bases->base_commit))
1552 return;
1554 /* Show the base commit */
1555 fprintf(file, "\nbase-commit: %s\n", oid_to_hex(&bases->base_commit));
1557 /* Show the prerequisite patches */
1558 for (i = bases->nr_patch_id - 1; i >= 0; i--)
1559 fprintf(file, "prerequisite-patch-id: %s\n", oid_to_hex(&bases->patch_id[i]));
1561 free(bases->patch_id);
1562 bases->nr_patch_id = 0;
1563 bases->alloc_patch_id = 0;
1564 oidclr(&bases->base_commit);
1567 static const char *diff_title(struct strbuf *sb, int reroll_count,
1568 const char *generic, const char *rerolled)
1570 if (reroll_count <= 0)
1571 strbuf_addstr(sb, generic);
1572 else /* RFC may be v0, so allow -v1 to diff against v0 */
1573 strbuf_addf(sb, rerolled, reroll_count - 1);
1574 return sb->buf;
1577 static void infer_range_diff_ranges(struct strbuf *r1,
1578 struct strbuf *r2,
1579 const char *prev,
1580 struct commit *origin,
1581 struct commit *head)
1583 const char *head_oid = oid_to_hex(&head->object.oid);
1585 if (!strstr(prev, "..")) {
1586 strbuf_addf(r1, "%s..%s", head_oid, prev);
1587 strbuf_addf(r2, "%s..%s", prev, head_oid);
1588 } else if (!origin) {
1589 die(_("failed to infer range-diff ranges"));
1590 } else {
1591 strbuf_addstr(r1, prev);
1592 strbuf_addf(r2, "%s..%s",
1593 oid_to_hex(&origin->object.oid), head_oid);
1597 int cmd_format_patch(int argc, const char **argv, const char *prefix)
1599 struct commit *commit;
1600 struct commit **list = NULL;
1601 struct rev_info rev;
1602 struct setup_revision_opt s_r_opt;
1603 int nr = 0, total, i;
1604 int use_stdout = 0;
1605 int start_number = -1;
1606 int just_numbers = 0;
1607 int ignore_if_in_upstream = 0;
1608 int cover_letter = -1;
1609 int boundary_count = 0;
1610 int no_binary_diff = 0;
1611 int zero_commit = 0;
1612 struct commit *origin = NULL;
1613 const char *in_reply_to = NULL;
1614 struct patch_ids ids;
1615 struct strbuf buf = STRBUF_INIT;
1616 int use_patch_format = 0;
1617 int quiet = 0;
1618 int reroll_count = -1;
1619 char *cover_from_description_arg = NULL;
1620 char *branch_name = NULL;
1621 char *base_commit = NULL;
1622 struct base_tree_info bases;
1623 int show_progress = 0;
1624 struct progress *progress = NULL;
1625 struct oid_array idiff_prev = OID_ARRAY_INIT;
1626 struct strbuf idiff_title = STRBUF_INIT;
1627 const char *rdiff_prev = NULL;
1628 struct strbuf rdiff1 = STRBUF_INIT;
1629 struct strbuf rdiff2 = STRBUF_INIT;
1630 struct strbuf rdiff_title = STRBUF_INIT;
1631 int creation_factor = -1;
1633 const struct option builtin_format_patch_options[] = {
1634 { OPTION_CALLBACK, 'n', "numbered", &numbered, NULL,
1635 N_("use [PATCH n/m] even with a single patch"),
1636 PARSE_OPT_NOARG, numbered_callback },
1637 { OPTION_CALLBACK, 'N', "no-numbered", &numbered, NULL,
1638 N_("use [PATCH] even with multiple patches"),
1639 PARSE_OPT_NOARG | PARSE_OPT_NONEG, no_numbered_callback },
1640 OPT_BOOL('s', "signoff", &do_signoff, N_("add Signed-off-by:")),
1641 OPT_BOOL(0, "stdout", &use_stdout,
1642 N_("print patches to standard out")),
1643 OPT_BOOL(0, "cover-letter", &cover_letter,
1644 N_("generate a cover letter")),
1645 OPT_BOOL(0, "numbered-files", &just_numbers,
1646 N_("use simple number sequence for output file names")),
1647 OPT_STRING(0, "suffix", &fmt_patch_suffix, N_("sfx"),
1648 N_("use <sfx> instead of '.patch'")),
1649 OPT_INTEGER(0, "start-number", &start_number,
1650 N_("start numbering patches at <n> instead of 1")),
1651 OPT_INTEGER('v', "reroll-count", &reroll_count,
1652 N_("mark the series as Nth re-roll")),
1653 { OPTION_CALLBACK, 0, "rfc", &rev, NULL,
1654 N_("Use [RFC PATCH] instead of [PATCH]"),
1655 PARSE_OPT_NOARG | PARSE_OPT_NONEG, rfc_callback },
1656 OPT_STRING(0, "cover-from-description", &cover_from_description_arg,
1657 N_("cover-from-description-mode"),
1658 N_("generate parts of a cover letter based on a branch's description")),
1659 { OPTION_CALLBACK, 0, "subject-prefix", &rev, N_("prefix"),
1660 N_("Use [<prefix>] instead of [PATCH]"),
1661 PARSE_OPT_NONEG, subject_prefix_callback },
1662 { OPTION_CALLBACK, 'o', "output-directory", &output_directory,
1663 N_("dir"), N_("store resulting files in <dir>"),
1664 PARSE_OPT_NONEG, output_directory_callback },
1665 { OPTION_CALLBACK, 'k', "keep-subject", &rev, NULL,
1666 N_("don't strip/add [PATCH]"),
1667 PARSE_OPT_NOARG | PARSE_OPT_NONEG, keep_callback },
1668 OPT_BOOL(0, "no-binary", &no_binary_diff,
1669 N_("don't output binary diffs")),
1670 OPT_BOOL(0, "zero-commit", &zero_commit,
1671 N_("output all-zero hash in From header")),
1672 OPT_BOOL(0, "ignore-if-in-upstream", &ignore_if_in_upstream,
1673 N_("don't include a patch matching a commit upstream")),
1674 OPT_SET_INT_F('p', "no-stat", &use_patch_format,
1675 N_("show patch format instead of default (patch + stat)"),
1676 1, PARSE_OPT_NONEG),
1677 OPT_GROUP(N_("Messaging")),
1678 { OPTION_CALLBACK, 0, "add-header", NULL, N_("header"),
1679 N_("add email header"), 0, header_callback },
1680 { OPTION_CALLBACK, 0, "to", NULL, N_("email"), N_("add To: header"),
1681 0, to_callback },
1682 { OPTION_CALLBACK, 0, "cc", NULL, N_("email"), N_("add Cc: header"),
1683 0, cc_callback },
1684 { OPTION_CALLBACK, 0, "from", &from, N_("ident"),
1685 N_("set From address to <ident> (or committer ident if absent)"),
1686 PARSE_OPT_OPTARG, from_callback },
1687 OPT_STRING(0, "in-reply-to", &in_reply_to, N_("message-id"),
1688 N_("make first mail a reply to <message-id>")),
1689 { OPTION_CALLBACK, 0, "attach", &rev, N_("boundary"),
1690 N_("attach the patch"), PARSE_OPT_OPTARG,
1691 attach_callback },
1692 { OPTION_CALLBACK, 0, "inline", &rev, N_("boundary"),
1693 N_("inline the patch"),
1694 PARSE_OPT_OPTARG | PARSE_OPT_NONEG,
1695 inline_callback },
1696 { OPTION_CALLBACK, 0, "thread", &thread, N_("style"),
1697 N_("enable message threading, styles: shallow, deep"),
1698 PARSE_OPT_OPTARG, thread_callback },
1699 OPT_STRING(0, "signature", &signature, N_("signature"),
1700 N_("add a signature")),
1701 OPT_STRING(0, "base", &base_commit, N_("base-commit"),
1702 N_("add prerequisite tree info to the patch series")),
1703 OPT_FILENAME(0, "signature-file", &signature_file,
1704 N_("add a signature from a file")),
1705 OPT__QUIET(&quiet, N_("don't print the patch filenames")),
1706 OPT_BOOL(0, "progress", &show_progress,
1707 N_("show progress while generating patches")),
1708 OPT_CALLBACK(0, "interdiff", &idiff_prev, N_("rev"),
1709 N_("show changes against <rev> in cover letter or single patch"),
1710 parse_opt_object_name),
1711 OPT_STRING(0, "range-diff", &rdiff_prev, N_("refspec"),
1712 N_("show changes against <refspec> in cover letter or single patch")),
1713 OPT_INTEGER(0, "creation-factor", &creation_factor,
1714 N_("percentage by which creation is weighted")),
1715 OPT_END()
1718 extra_hdr.strdup_strings = 1;
1719 extra_to.strdup_strings = 1;
1720 extra_cc.strdup_strings = 1;
1721 init_log_defaults();
1722 init_display_notes(&notes_opt);
1723 git_config(git_format_config, NULL);
1724 repo_init_revisions(the_repository, &rev, prefix);
1725 rev.show_notes = show_notes;
1726 memcpy(&rev.notes_opt, &notes_opt, sizeof(notes_opt));
1727 rev.commit_format = CMIT_FMT_EMAIL;
1728 rev.encode_email_headers = default_encode_email_headers;
1729 rev.expand_tabs_in_log_default = 0;
1730 rev.verbose_header = 1;
1731 rev.diff = 1;
1732 rev.max_parents = 1;
1733 rev.diffopt.flags.recursive = 1;
1734 rev.subject_prefix = fmt_patch_subject_prefix;
1735 memset(&s_r_opt, 0, sizeof(s_r_opt));
1736 s_r_opt.def = "HEAD";
1737 s_r_opt.revarg_opt = REVARG_COMMITTISH;
1739 if (base_auto)
1740 base_commit = "auto";
1742 if (default_attach) {
1743 rev.mime_boundary = default_attach;
1744 rev.no_inline = 1;
1748 * Parse the arguments before setup_revisions(), or something
1749 * like "git format-patch -o a123 HEAD^.." may fail; a123 is
1750 * possibly a valid SHA1.
1752 argc = parse_options(argc, argv, prefix, builtin_format_patch_options,
1753 builtin_format_patch_usage,
1754 PARSE_OPT_KEEP_ARGV0 | PARSE_OPT_KEEP_UNKNOWN |
1755 PARSE_OPT_KEEP_DASHDASH);
1757 if (cover_from_description_arg)
1758 cover_from_description_mode = parse_cover_from_description(cover_from_description_arg);
1760 if (0 < reroll_count) {
1761 struct strbuf sprefix = STRBUF_INIT;
1762 strbuf_addf(&sprefix, "%s v%d",
1763 rev.subject_prefix, reroll_count);
1764 rev.reroll_count = reroll_count;
1765 rev.subject_prefix = strbuf_detach(&sprefix, NULL);
1768 for (i = 0; i < extra_hdr.nr; i++) {
1769 strbuf_addstr(&buf, extra_hdr.items[i].string);
1770 strbuf_addch(&buf, '\n');
1773 if (extra_to.nr)
1774 strbuf_addstr(&buf, "To: ");
1775 for (i = 0; i < extra_to.nr; i++) {
1776 if (i)
1777 strbuf_addstr(&buf, " ");
1778 strbuf_addstr(&buf, extra_to.items[i].string);
1779 if (i + 1 < extra_to.nr)
1780 strbuf_addch(&buf, ',');
1781 strbuf_addch(&buf, '\n');
1784 if (extra_cc.nr)
1785 strbuf_addstr(&buf, "Cc: ");
1786 for (i = 0; i < extra_cc.nr; i++) {
1787 if (i)
1788 strbuf_addstr(&buf, " ");
1789 strbuf_addstr(&buf, extra_cc.items[i].string);
1790 if (i + 1 < extra_cc.nr)
1791 strbuf_addch(&buf, ',');
1792 strbuf_addch(&buf, '\n');
1795 rev.extra_headers = strbuf_detach(&buf, NULL);
1797 if (from) {
1798 if (split_ident_line(&rev.from_ident, from, strlen(from)))
1799 die(_("invalid ident line: %s"), from);
1802 if (start_number < 0)
1803 start_number = 1;
1806 * If numbered is set solely due to format.numbered in config,
1807 * and it would conflict with --keep-subject (-k) from the
1808 * command line, reset "numbered".
1810 if (numbered && keep_subject && !numbered_cmdline_opt)
1811 numbered = 0;
1813 if (numbered && keep_subject)
1814 die(_("-n and -k are mutually exclusive"));
1815 if (keep_subject && subject_prefix)
1816 die(_("--subject-prefix/--rfc and -k are mutually exclusive"));
1817 rev.preserve_subject = keep_subject;
1819 argc = setup_revisions(argc, argv, &rev, &s_r_opt);
1820 if (argc > 1)
1821 die(_("unrecognized argument: %s"), argv[1]);
1823 if (rev.diffopt.output_format & DIFF_FORMAT_NAME)
1824 die(_("--name-only does not make sense"));
1825 if (rev.diffopt.output_format & DIFF_FORMAT_NAME_STATUS)
1826 die(_("--name-status does not make sense"));
1827 if (rev.diffopt.output_format & DIFF_FORMAT_CHECKDIFF)
1828 die(_("--check does not make sense"));
1830 if (!use_patch_format &&
1831 (!rev.diffopt.output_format ||
1832 rev.diffopt.output_format == DIFF_FORMAT_PATCH))
1833 rev.diffopt.output_format = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_SUMMARY;
1834 if (!rev.diffopt.stat_width)
1835 rev.diffopt.stat_width = MAIL_DEFAULT_WRAP;
1837 /* Always generate a patch */
1838 rev.diffopt.output_format |= DIFF_FORMAT_PATCH;
1840 rev.zero_commit = zero_commit;
1842 if (!rev.diffopt.flags.text && !no_binary_diff)
1843 rev.diffopt.flags.binary = 1;
1845 if (rev.show_notes)
1846 load_display_notes(&rev.notes_opt);
1848 if (!output_directory && !use_stdout)
1849 output_directory = config_output_directory;
1851 if (!use_stdout)
1852 output_directory = set_outdir(prefix, output_directory);
1853 else
1854 setup_pager();
1856 if (output_directory) {
1857 int saved;
1858 if (rev.diffopt.use_color != GIT_COLOR_ALWAYS)
1859 rev.diffopt.use_color = GIT_COLOR_NEVER;
1860 if (use_stdout)
1861 die(_("standard output, or directory, which one?"));
1863 * We consider <outdir> as 'outside of gitdir', therefore avoid
1864 * applying adjust_shared_perm in s-c-l-d.
1866 saved = get_shared_repository();
1867 set_shared_repository(0);
1868 switch (safe_create_leading_directories_const(output_directory)) {
1869 case SCLD_OK:
1870 case SCLD_EXISTS:
1871 break;
1872 default:
1873 die(_("could not create leading directories "
1874 "of '%s'"), output_directory);
1876 set_shared_repository(saved);
1877 if (mkdir(output_directory, 0777) < 0 && errno != EEXIST)
1878 die_errno(_("could not create directory '%s'"),
1879 output_directory);
1882 if (rev.pending.nr == 1) {
1883 int check_head = 0;
1885 if (rev.max_count < 0 && !rev.show_root_diff) {
1887 * This is traditional behaviour of "git format-patch
1888 * origin" that prepares what the origin side still
1889 * does not have.
1891 rev.pending.objects[0].item->flags |= UNINTERESTING;
1892 add_head_to_pending(&rev);
1893 check_head = 1;
1896 * Otherwise, it is "format-patch -22 HEAD", and/or
1897 * "format-patch --root HEAD". The user wants
1898 * get_revision() to do the usual traversal.
1901 if (!strcmp(rev.pending.objects[0].name, "HEAD"))
1902 check_head = 1;
1904 if (check_head) {
1905 const char *ref, *v;
1906 ref = resolve_ref_unsafe("HEAD", RESOLVE_REF_READING,
1907 NULL, NULL);
1908 if (ref && skip_prefix(ref, "refs/heads/", &v))
1909 branch_name = xstrdup(v);
1910 else
1911 branch_name = xstrdup(""); /* no branch */
1916 * We cannot move this anywhere earlier because we do want to
1917 * know if --root was given explicitly from the command line.
1919 rev.show_root_diff = 1;
1921 if (ignore_if_in_upstream) {
1922 /* Don't say anything if head and upstream are the same. */
1923 if (rev.pending.nr == 2) {
1924 struct object_array_entry *o = rev.pending.objects;
1925 if (oideq(&o[0].item->oid, &o[1].item->oid))
1926 goto done;
1928 get_patch_ids(&rev, &ids);
1931 if (prepare_revision_walk(&rev))
1932 die(_("revision walk setup failed"));
1933 rev.boundary = 1;
1934 while ((commit = get_revision(&rev)) != NULL) {
1935 if (commit->object.flags & BOUNDARY) {
1936 boundary_count++;
1937 origin = (boundary_count == 1) ? commit : NULL;
1938 continue;
1941 if (ignore_if_in_upstream && has_commit_patch_id(commit, &ids))
1942 continue;
1944 nr++;
1945 REALLOC_ARRAY(list, nr);
1946 list[nr - 1] = commit;
1948 if (nr == 0)
1949 /* nothing to do */
1950 goto done;
1951 total = nr;
1952 if (cover_letter == -1) {
1953 if (config_cover_letter == COVER_AUTO)
1954 cover_letter = (total > 1);
1955 else
1956 cover_letter = (config_cover_letter == COVER_ON);
1958 if (!keep_subject && auto_number && (total > 1 || cover_letter))
1959 numbered = 1;
1960 if (numbered)
1961 rev.total = total + start_number - 1;
1963 if (idiff_prev.nr) {
1964 if (!cover_letter && total != 1)
1965 die(_("--interdiff requires --cover-letter or single patch"));
1966 rev.idiff_oid1 = &idiff_prev.oid[idiff_prev.nr - 1];
1967 rev.idiff_oid2 = get_commit_tree_oid(list[0]);
1968 rev.idiff_title = diff_title(&idiff_title, reroll_count,
1969 _("Interdiff:"),
1970 _("Interdiff against v%d:"));
1973 if (creation_factor < 0)
1974 creation_factor = RANGE_DIFF_CREATION_FACTOR_DEFAULT;
1975 else if (!rdiff_prev)
1976 die(_("--creation-factor requires --range-diff"));
1978 if (rdiff_prev) {
1979 if (!cover_letter && total != 1)
1980 die(_("--range-diff requires --cover-letter or single patch"));
1982 infer_range_diff_ranges(&rdiff1, &rdiff2, rdiff_prev,
1983 origin, list[0]);
1984 rev.rdiff1 = rdiff1.buf;
1985 rev.rdiff2 = rdiff2.buf;
1986 rev.creation_factor = creation_factor;
1987 rev.rdiff_title = diff_title(&rdiff_title, reroll_count,
1988 _("Range-diff:"),
1989 _("Range-diff against v%d:"));
1992 if (!signature) {
1993 ; /* --no-signature inhibits all signatures */
1994 } else if (signature && signature != git_version_string) {
1995 ; /* non-default signature already set */
1996 } else if (signature_file) {
1997 struct strbuf buf = STRBUF_INIT;
1999 if (strbuf_read_file(&buf, signature_file, 128) < 0)
2000 die_errno(_("unable to read signature file '%s'"), signature_file);
2001 signature = strbuf_detach(&buf, NULL);
2004 memset(&bases, 0, sizeof(bases));
2005 if (base_commit) {
2006 struct commit *base = get_base_commit(base_commit, list, nr);
2007 reset_revision_walk();
2008 clear_object_flags(UNINTERESTING);
2009 prepare_bases(&bases, base, list, nr);
2012 if (in_reply_to || thread || cover_letter)
2013 rev.ref_message_ids = xcalloc(1, sizeof(struct string_list));
2014 if (in_reply_to) {
2015 const char *msgid = clean_message_id(in_reply_to);
2016 string_list_append(rev.ref_message_ids, msgid);
2018 rev.numbered_files = just_numbers;
2019 rev.patch_suffix = fmt_patch_suffix;
2020 if (cover_letter) {
2021 if (thread)
2022 gen_message_id(&rev, "cover");
2023 make_cover_letter(&rev, use_stdout,
2024 origin, nr, list, branch_name, quiet);
2025 print_bases(&bases, rev.diffopt.file);
2026 print_signature(rev.diffopt.file);
2027 total++;
2028 start_number--;
2029 /* interdiff/range-diff in cover-letter; omit from patches */
2030 rev.idiff_oid1 = NULL;
2031 rev.rdiff1 = NULL;
2033 rev.add_signoff = do_signoff;
2035 if (show_progress)
2036 progress = start_delayed_progress(_("Generating patches"), total);
2037 while (0 <= --nr) {
2038 int shown;
2039 display_progress(progress, total - nr);
2040 commit = list[nr];
2041 rev.nr = total - nr + (start_number - 1);
2042 /* Make the second and subsequent mails replies to the first */
2043 if (thread) {
2044 /* Have we already had a message ID? */
2045 if (rev.message_id) {
2047 * For deep threading: make every mail
2048 * a reply to the previous one, no
2049 * matter what other options are set.
2051 * For shallow threading:
2053 * Without --cover-letter and
2054 * --in-reply-to, make every mail a
2055 * reply to the one before.
2057 * With --in-reply-to but no
2058 * --cover-letter, make every mail a
2059 * reply to the <reply-to>.
2061 * With --cover-letter, make every
2062 * mail but the cover letter a reply
2063 * to the cover letter. The cover
2064 * letter is a reply to the
2065 * --in-reply-to, if specified.
2067 if (thread == THREAD_SHALLOW
2068 && rev.ref_message_ids->nr > 0
2069 && (!cover_letter || rev.nr > 1))
2070 free(rev.message_id);
2071 else
2072 string_list_append(rev.ref_message_ids,
2073 rev.message_id);
2075 gen_message_id(&rev, oid_to_hex(&commit->object.oid));
2078 if (!use_stdout &&
2079 open_next_file(rev.numbered_files ? NULL : commit, NULL, &rev, quiet))
2080 die(_("failed to create output files"));
2081 shown = log_tree_commit(&rev, commit);
2082 free_commit_buffer(the_repository->parsed_objects,
2083 commit);
2085 /* We put one extra blank line between formatted
2086 * patches and this flag is used by log-tree code
2087 * to see if it needs to emit a LF before showing
2088 * the log; when using one file per patch, we do
2089 * not want the extra blank line.
2091 if (!use_stdout)
2092 rev.shown_one = 0;
2093 if (shown) {
2094 print_bases(&bases, rev.diffopt.file);
2095 if (rev.mime_boundary)
2096 fprintf(rev.diffopt.file, "\n--%s%s--\n\n\n",
2097 mime_boundary_leader,
2098 rev.mime_boundary);
2099 else
2100 print_signature(rev.diffopt.file);
2102 if (!use_stdout)
2103 fclose(rev.diffopt.file);
2105 stop_progress(&progress);
2106 free(list);
2107 free(branch_name);
2108 string_list_clear(&extra_to, 0);
2109 string_list_clear(&extra_cc, 0);
2110 string_list_clear(&extra_hdr, 0);
2111 if (ignore_if_in_upstream)
2112 free_patch_ids(&ids);
2114 done:
2115 oid_array_clear(&idiff_prev);
2116 strbuf_release(&idiff_title);
2117 strbuf_release(&rdiff1);
2118 strbuf_release(&rdiff2);
2119 strbuf_release(&rdiff_title);
2120 return 0;
2123 static int add_pending_commit(const char *arg, struct rev_info *revs, int flags)
2125 struct object_id oid;
2126 if (get_oid(arg, &oid) == 0) {
2127 struct commit *commit = lookup_commit_reference(the_repository,
2128 &oid);
2129 if (commit) {
2130 commit->object.flags |= flags;
2131 add_pending_object(revs, &commit->object, arg);
2132 return 0;
2135 return -1;
2138 static const char * const cherry_usage[] = {
2139 N_("git cherry [-v] [<upstream> [<head> [<limit>]]]"),
2140 NULL
2143 static void print_commit(char sign, struct commit *commit, int verbose,
2144 int abbrev, FILE *file)
2146 if (!verbose) {
2147 fprintf(file, "%c %s\n", sign,
2148 find_unique_abbrev(&commit->object.oid, abbrev));
2149 } else {
2150 struct strbuf buf = STRBUF_INIT;
2151 pp_commit_easy(CMIT_FMT_ONELINE, commit, &buf);
2152 fprintf(file, "%c %s %s\n", sign,
2153 find_unique_abbrev(&commit->object.oid, abbrev),
2154 buf.buf);
2155 strbuf_release(&buf);
2159 int cmd_cherry(int argc, const char **argv, const char *prefix)
2161 struct rev_info revs;
2162 struct patch_ids ids;
2163 struct commit *commit;
2164 struct commit_list *list = NULL;
2165 struct branch *current_branch;
2166 const char *upstream;
2167 const char *head = "HEAD";
2168 const char *limit = NULL;
2169 int verbose = 0, abbrev = 0;
2171 struct option options[] = {
2172 OPT__ABBREV(&abbrev),
2173 OPT__VERBOSE(&verbose, N_("be verbose")),
2174 OPT_END()
2177 argc = parse_options(argc, argv, prefix, options, cherry_usage, 0);
2179 switch (argc) {
2180 case 3:
2181 limit = argv[2];
2182 /* FALLTHROUGH */
2183 case 2:
2184 head = argv[1];
2185 /* FALLTHROUGH */
2186 case 1:
2187 upstream = argv[0];
2188 break;
2189 default:
2190 current_branch = branch_get(NULL);
2191 upstream = branch_get_upstream(current_branch, NULL);
2192 if (!upstream) {
2193 fprintf(stderr, _("Could not find a tracked"
2194 " remote branch, please"
2195 " specify <upstream> manually.\n"));
2196 usage_with_options(cherry_usage, options);
2200 repo_init_revisions(the_repository, &revs, prefix);
2201 revs.max_parents = 1;
2203 if (add_pending_commit(head, &revs, 0))
2204 die(_("unknown commit %s"), head);
2205 if (add_pending_commit(upstream, &revs, UNINTERESTING))
2206 die(_("unknown commit %s"), upstream);
2208 /* Don't say anything if head and upstream are the same. */
2209 if (revs.pending.nr == 2) {
2210 struct object_array_entry *o = revs.pending.objects;
2211 if (oideq(&o[0].item->oid, &o[1].item->oid))
2212 return 0;
2215 get_patch_ids(&revs, &ids);
2217 if (limit && add_pending_commit(limit, &revs, UNINTERESTING))
2218 die(_("unknown commit %s"), limit);
2220 /* reverse the list of commits */
2221 if (prepare_revision_walk(&revs))
2222 die(_("revision walk setup failed"));
2223 while ((commit = get_revision(&revs)) != NULL) {
2224 commit_list_insert(commit, &list);
2227 while (list) {
2228 char sign = '+';
2230 commit = list->item;
2231 if (has_commit_patch_id(commit, &ids))
2232 sign = '-';
2233 print_commit(sign, commit, verbose, abbrev, revs.diffopt.file);
2234 list = list->next;
2237 free_patch_ids(&ids);
2238 return 0;