2 * Builtin "git log" and related commands (show, whatchanged)
4 * (C) Copyright 2006 Linus Torvalds
10 #include "object-store.h"
18 #include "reflog-walk.h"
19 #include "patch-ids.h"
20 #include "run-command.h"
23 #include "string-list.h"
24 #include "parse-options.h"
27 #include "streaming.h"
30 #include "gpg-interface.h"
32 #include "commit-slab.h"
33 #include "repository.h"
34 #include "commit-reach.h"
35 #include "interdiff.h"
36 #include "range-diff.h"
38 #define MAIL_DEFAULT_WRAP 72
40 /* Set a default date-time format for git log ("log.date" config variable) */
41 static const char *default_date_mode = NULL;
43 static int default_abbrev_commit;
44 static int default_show_root = 1;
45 static int default_follow;
46 static int default_show_signature;
47 static int decoration_style;
48 static int decoration_given;
49 static int use_mailmap_config;
50 static const char *fmt_patch_subject_prefix = "PATCH";
51 static const char *fmt_pretty;
53 static const char * const builtin_log_usage[] = {
54 N_("git log [<options>] [<revision-range>] [[--] <path>...]"),
55 N_("git show [<options>] <object>..."),
59 struct line_opt_callback_data {
62 struct string_list args;
65 static int auto_decoration_style(void)
67 return (isatty(1) || pager_in_use()) ? DECORATE_SHORT_REFS : 0;
70 static int parse_decoration_style(const char *value)
72 switch (git_parse_maybe_bool(value)) {
74 return DECORATE_SHORT_REFS;
80 if (!strcmp(value, "full"))
81 return DECORATE_FULL_REFS;
82 else if (!strcmp(value, "short"))
83 return DECORATE_SHORT_REFS;
84 else if (!strcmp(value, "auto"))
85 return auto_decoration_style();
89 static int decorate_callback(const struct option *opt, const char *arg, int unset)
94 decoration_style = parse_decoration_style(arg);
96 decoration_style = DECORATE_SHORT_REFS;
98 if (decoration_style < 0)
99 die(_("invalid --decorate option: %s"), arg);
101 decoration_given = 1;
106 static int log_line_range_callback(const struct option *option, const char *arg, int unset)
108 struct line_opt_callback_data *data = option->value;
110 BUG_ON_OPT_NEG(unset);
115 data->rev->line_level_traverse = 1;
116 string_list_append(&data->args, arg);
121 static void init_log_defaults(void)
123 init_grep_defaults(the_repository);
124 init_diff_ui_defaults();
126 decoration_style = auto_decoration_style();
129 static void cmd_log_init_defaults(struct rev_info *rev)
132 get_commit_format(fmt_pretty, rev);
134 rev->diffopt.flags.default_follow_renames = 1;
135 rev->verbose_header = 1;
136 rev->diffopt.flags.recursive = 1;
137 rev->diffopt.stat_width = -1; /* use full terminal width */
138 rev->diffopt.stat_graph_width = -1; /* respect statGraphWidth config */
139 rev->abbrev_commit = default_abbrev_commit;
140 rev->show_root_diff = default_show_root;
141 rev->subject_prefix = fmt_patch_subject_prefix;
142 rev->show_signature = default_show_signature;
143 rev->diffopt.flags.allow_textconv = 1;
145 if (default_date_mode)
146 parse_date_format(default_date_mode, &rev->date_mode);
149 static void cmd_log_init_finish(int argc, const char **argv, const char *prefix,
150 struct rev_info *rev, struct setup_revision_opt *opt)
152 struct userformat_want w;
153 int quiet = 0, source = 0, mailmap = 0;
154 static struct line_opt_callback_data line_cb = {NULL, NULL, STRING_LIST_INIT_DUP};
155 static struct string_list decorate_refs_exclude = STRING_LIST_INIT_NODUP;
156 static struct string_list decorate_refs_include = STRING_LIST_INIT_NODUP;
157 struct decoration_filter decoration_filter = {&decorate_refs_include,
158 &decorate_refs_exclude};
159 static struct revision_sources revision_sources;
161 const struct option builtin_log_options[] = {
162 OPT__QUIET(&quiet, N_("suppress diff output")),
163 OPT_BOOL(0, "source", &source, N_("show source")),
164 OPT_BOOL(0, "use-mailmap", &mailmap, N_("Use mail map file")),
165 OPT_STRING_LIST(0, "decorate-refs", &decorate_refs_include,
166 N_("pattern"), N_("only decorate refs that match <pattern>")),
167 OPT_STRING_LIST(0, "decorate-refs-exclude", &decorate_refs_exclude,
168 N_("pattern"), N_("do not decorate refs that match <pattern>")),
169 { OPTION_CALLBACK, 0, "decorate", NULL, NULL, N_("decorate options"),
170 PARSE_OPT_OPTARG, decorate_callback},
171 OPT_CALLBACK('L', NULL, &line_cb, "n,m:file",
172 N_("Process line range n,m in file, counting from 1"),
173 log_line_range_callback),
178 line_cb.prefix = prefix;
180 mailmap = use_mailmap_config;
181 argc = parse_options(argc, argv, prefix,
182 builtin_log_options, builtin_log_usage,
183 PARSE_OPT_KEEP_ARGV0 | PARSE_OPT_KEEP_UNKNOWN |
184 PARSE_OPT_KEEP_DASHDASH);
187 rev->diffopt.output_format |= DIFF_FORMAT_NO_OUTPUT;
188 argc = setup_revisions(argc, argv, rev, opt);
190 /* Any arguments at this point are not recognized */
192 die(_("unrecognized argument: %s"), argv[1]);
194 memset(&w, 0, sizeof(w));
195 userformat_find_requirements(NULL, &w);
197 if (!rev->show_notes_given && (!rev->pretty_given || w.notes))
200 init_display_notes(&rev->notes_opt);
202 if ((rev->diffopt.pickaxe_opts & DIFF_PICKAXE_KINDS_MASK) ||
203 rev->diffopt.filter || rev->diffopt.flags.follow_renames)
204 rev->always_show_header = 0;
207 init_revision_sources(&revision_sources);
208 rev->sources = &revision_sources;
212 rev->mailmap = xcalloc(1, sizeof(struct string_list));
213 read_mailmap(rev->mailmap, NULL);
216 if (rev->pretty_given && rev->commit_format == CMIT_FMT_RAW) {
218 * "log --pretty=raw" is special; ignore UI oriented
219 * configuration variables such as decoration.
221 if (!decoration_given)
222 decoration_style = 0;
223 if (!rev->abbrev_commit_given)
224 rev->abbrev_commit = 0;
227 if (decoration_style) {
228 rev->show_decorations = 1;
229 load_ref_decorations(&decoration_filter, decoration_style);
232 if (rev->line_level_traverse)
233 line_log_init(rev, line_cb.prefix, &line_cb.args);
238 static void cmd_log_init(int argc, const char **argv, const char *prefix,
239 struct rev_info *rev, struct setup_revision_opt *opt)
241 cmd_log_init_defaults(rev);
242 cmd_log_init_finish(argc, argv, prefix, rev, opt);
246 * This gives a rough estimate for how many commits we
247 * will print out in the list.
249 static int estimate_commit_count(struct rev_info *rev, struct commit_list *list)
254 struct commit *commit = list->item;
255 unsigned int flags = commit->object.flags;
257 if (!(flags & (TREESAME | UNINTERESTING)))
263 static void show_early_header(struct rev_info *rev, const char *stage, int nr)
265 if (rev->shown_one) {
267 if (rev->commit_format != CMIT_FMT_ONELINE)
268 putchar(rev->diffopt.line_termination);
270 fprintf(rev->diffopt.file, _("Final output: %d %s\n"), nr, stage);
273 static struct itimerval early_output_timer;
275 static void log_show_early(struct rev_info *revs, struct commit_list *list)
277 int i = revs->early_output, close_file = revs->diffopt.close_file;
280 revs->diffopt.close_file = 0;
281 sort_in_topological_order(&list, revs->sort_order);
283 struct commit *commit = list->item;
284 switch (simplify_commit(revs, commit)) {
287 int n = estimate_commit_count(revs, list);
288 show_early_header(revs, "incomplete", n);
291 log_tree_commit(revs, commit);
298 fclose(revs->diffopt.file);
304 /* Did we already get enough commits for the early output? */
307 fclose(revs->diffopt.file);
312 * ..if no, then repeat it twice a second until we
315 * NOTE! We don't use "it_interval", because if the
316 * reader isn't listening, we want our output to be
317 * throttled by the writing, and not have the timer
318 * trigger every second even if we're blocked on a
321 early_output_timer.it_value.tv_sec = 0;
322 early_output_timer.it_value.tv_usec = 500000;
323 setitimer(ITIMER_REAL, &early_output_timer, NULL);
326 static void early_output(int signal)
328 show_early_output = log_show_early;
331 static void setup_early_output(struct rev_info *rev)
336 * Set up the signal handler, minimally intrusively:
337 * we only set a single volatile integer word (not
338 * using sigatomic_t - trying to avoid unnecessary
339 * system dependencies and headers), and using
342 memset(&sa, 0, sizeof(sa));
343 sa.sa_handler = early_output;
344 sigemptyset(&sa.sa_mask);
345 sa.sa_flags = SA_RESTART;
346 sigaction(SIGALRM, &sa, NULL);
349 * If we can get the whole output in less than a
350 * tenth of a second, don't even bother doing the
351 * early-output thing..
353 * This is a one-time-only trigger.
355 early_output_timer.it_value.tv_sec = 0;
356 early_output_timer.it_value.tv_usec = 100000;
357 setitimer(ITIMER_REAL, &early_output_timer, NULL);
360 static void finish_early_output(struct rev_info *rev)
362 int n = estimate_commit_count(rev, rev->commits);
363 signal(SIGALRM, SIG_IGN);
364 show_early_header(rev, "done", n);
367 static int cmd_log_walk(struct rev_info *rev)
369 struct commit *commit;
371 int saved_dcctc = 0, close_file = rev->diffopt.close_file;
373 if (rev->early_output)
374 setup_early_output(rev);
376 if (prepare_revision_walk(rev))
377 die(_("revision walk setup failed"));
379 if (rev->early_output)
380 finish_early_output(rev);
383 * For --check and --exit-code, the exit code is based on CHECK_FAILED
384 * and HAS_CHANGES being accumulated in rev->diffopt, so be careful to
385 * retain that state information if replacing rev->diffopt in this loop
387 rev->diffopt.close_file = 0;
388 while ((commit = get_revision(rev)) != NULL) {
389 if (!log_tree_commit(rev, commit) && rev->max_count >= 0)
391 * We decremented max_count in get_revision,
392 * but we didn't actually show the commit.
395 if (!rev->reflog_info) {
397 * We may show a given commit multiple times when
398 * walking the reflogs.
400 free_commit_buffer(commit);
401 free_commit_list(commit->parents);
402 commit->parents = NULL;
404 if (saved_nrl < rev->diffopt.needed_rename_limit)
405 saved_nrl = rev->diffopt.needed_rename_limit;
406 if (rev->diffopt.degraded_cc_to_c)
409 rev->diffopt.degraded_cc_to_c = saved_dcctc;
410 rev->diffopt.needed_rename_limit = saved_nrl;
412 fclose(rev->diffopt.file);
414 if (rev->diffopt.output_format & DIFF_FORMAT_CHECKDIFF &&
415 rev->diffopt.flags.check_failed) {
418 return diff_result_code(&rev->diffopt, 0);
421 static int git_log_config(const char *var, const char *value, void *cb)
423 const char *slot_name;
425 if (!strcmp(var, "format.pretty"))
426 return git_config_string(&fmt_pretty, var, value);
427 if (!strcmp(var, "format.subjectprefix"))
428 return git_config_string(&fmt_patch_subject_prefix, var, value);
429 if (!strcmp(var, "log.abbrevcommit")) {
430 default_abbrev_commit = git_config_bool(var, value);
433 if (!strcmp(var, "log.date"))
434 return git_config_string(&default_date_mode, var, value);
435 if (!strcmp(var, "log.decorate")) {
436 decoration_style = parse_decoration_style(value);
437 if (decoration_style < 0)
438 decoration_style = 0; /* maybe warn? */
441 if (!strcmp(var, "log.showroot")) {
442 default_show_root = git_config_bool(var, value);
445 if (!strcmp(var, "log.follow")) {
446 default_follow = git_config_bool(var, value);
449 if (skip_prefix(var, "color.decorate.", &slot_name))
450 return parse_decorate_color_config(var, slot_name, value);
451 if (!strcmp(var, "log.mailmap")) {
452 use_mailmap_config = git_config_bool(var, value);
455 if (!strcmp(var, "log.showsignature")) {
456 default_show_signature = git_config_bool(var, value);
460 if (grep_config(var, value, cb) < 0)
462 if (git_gpg_config(var, value, cb) < 0)
464 return git_diff_ui_config(var, value, cb);
467 int cmd_whatchanged(int argc, const char **argv, const char *prefix)
470 struct setup_revision_opt opt;
473 git_config(git_log_config, NULL);
475 repo_init_revisions(the_repository, &rev, prefix);
477 rev.simplify_history = 0;
478 memset(&opt, 0, sizeof(opt));
480 opt.revarg_opt = REVARG_COMMITTISH;
481 cmd_log_init(argc, argv, prefix, &rev, &opt);
482 if (!rev.diffopt.output_format)
483 rev.diffopt.output_format = DIFF_FORMAT_RAW;
484 return cmd_log_walk(&rev);
487 static void show_tagger(char *buf, int len, struct rev_info *rev)
489 struct strbuf out = STRBUF_INIT;
490 struct pretty_print_context pp = {0};
492 pp.fmt = rev->commit_format;
493 pp.date_mode = rev->date_mode;
494 pp_user_info(&pp, "Tagger", &out, buf, get_log_output_encoding());
495 fprintf(rev->diffopt.file, "%s", out.buf);
496 strbuf_release(&out);
499 static int show_blob_object(const struct object_id *oid, struct rev_info *rev, const char *obj_name)
501 struct object_id oidc;
502 struct object_context obj_context;
506 fflush(rev->diffopt.file);
507 if (!rev->diffopt.flags.textconv_set_via_cmdline ||
508 !rev->diffopt.flags.allow_textconv)
509 return stream_blob_to_fd(1, oid, NULL, 0);
511 if (get_oid_with_context(obj_name, GET_OID_RECORD_PATH,
512 &oidc, &obj_context))
513 die(_("Not a valid object name %s"), obj_name);
514 if (!obj_context.path ||
515 !textconv_object(the_repository, obj_context.path,
516 obj_context.mode, &oidc, 1, &buf, &size)) {
517 free(obj_context.path);
518 return stream_blob_to_fd(1, oid, NULL, 0);
522 die(_("git show %s: bad file"), obj_name);
524 write_or_die(1, buf, size);
525 free(obj_context.path);
529 static int show_tag_object(const struct object_id *oid, struct rev_info *rev)
532 enum object_type type;
533 char *buf = read_object_file(oid, &type, &size);
537 return error(_("Could not read object %s"), oid_to_hex(oid));
539 assert(type == OBJ_TAG);
540 while (offset < size && buf[offset] != '\n') {
541 int new_offset = offset + 1;
542 while (new_offset < size && buf[new_offset++] != '\n')
544 if (starts_with(buf + offset, "tagger "))
545 show_tagger(buf + offset + 7,
546 new_offset - offset - 7, rev);
551 fwrite(buf + offset, size - offset, 1, rev->diffopt.file);
556 static int show_tree_object(const struct object_id *oid,
558 const char *pathname, unsigned mode, int stage, void *context)
560 FILE *file = context;
561 fprintf(file, "%s%s\n", pathname, S_ISDIR(mode) ? "/" : "");
565 static void show_setup_revisions_tweak(struct rev_info *rev,
566 struct setup_revision_opt *opt)
568 if (rev->ignore_merges) {
569 /* There was no "-m" on the command line */
570 rev->ignore_merges = 0;
571 if (!rev->first_parent_only && !rev->combine_merges) {
572 /* No "--first-parent", "-c", or "--cc" */
573 rev->combine_merges = 1;
574 rev->dense_combined_merges = 1;
577 if (!rev->diffopt.output_format)
578 rev->diffopt.output_format = DIFF_FORMAT_PATCH;
581 int cmd_show(int argc, const char **argv, const char *prefix)
584 struct object_array_entry *objects;
585 struct setup_revision_opt opt;
586 struct pathspec match_all;
587 int i, count, ret = 0;
590 git_config(git_log_config, NULL);
592 memset(&match_all, 0, sizeof(match_all));
593 repo_init_revisions(the_repository, &rev, prefix);
595 rev.always_show_header = 1;
596 rev.no_walk = REVISION_WALK_NO_WALK_SORTED;
597 rev.diffopt.stat_width = -1; /* Scale to real terminal size */
599 memset(&opt, 0, sizeof(opt));
601 opt.tweak = show_setup_revisions_tweak;
602 cmd_log_init(argc, argv, prefix, &rev, &opt);
605 return cmd_log_walk(&rev);
607 count = rev.pending.nr;
608 objects = rev.pending.objects;
609 for (i = 0; i < count && !ret; i++) {
610 struct object *o = objects[i].item;
611 const char *name = objects[i].name;
614 ret = show_blob_object(&o->oid, &rev, name);
617 struct tag *t = (struct tag *)o;
621 fprintf(rev.diffopt.file, "%stag %s%s\n",
622 diff_get_color_opt(&rev.diffopt, DIFF_COMMIT),
624 diff_get_color_opt(&rev.diffopt, DIFF_RESET));
625 ret = show_tag_object(&o->oid, &rev);
629 o = parse_object(the_repository, &t->tagged->oid);
631 ret = error(_("Could not read object %s"),
632 oid_to_hex(&t->tagged->oid));
640 fprintf(rev.diffopt.file, "%stree %s%s\n\n",
641 diff_get_color_opt(&rev.diffopt, DIFF_COMMIT),
643 diff_get_color_opt(&rev.diffopt, DIFF_RESET));
644 read_tree_recursive((struct tree *)o, "", 0, 0, &match_all,
645 show_tree_object, rev.diffopt.file);
649 rev.pending.nr = rev.pending.alloc = 0;
650 rev.pending.objects = NULL;
651 add_object_array(o, name, &rev.pending);
652 ret = cmd_log_walk(&rev);
655 ret = error(_("Unknown type: %d"), o->type);
663 * This is equivalent to "git log -g --abbrev-commit --pretty=oneline"
665 int cmd_log_reflog(int argc, const char **argv, const char *prefix)
668 struct setup_revision_opt opt;
671 git_config(git_log_config, NULL);
673 repo_init_revisions(the_repository, &rev, prefix);
674 init_reflog_walk(&rev.reflog_info);
675 rev.verbose_header = 1;
676 memset(&opt, 0, sizeof(opt));
678 cmd_log_init_defaults(&rev);
679 rev.abbrev_commit = 1;
680 rev.commit_format = CMIT_FMT_ONELINE;
681 rev.use_terminator = 1;
682 rev.always_show_header = 1;
683 cmd_log_init_finish(argc, argv, prefix, &rev, &opt);
685 return cmd_log_walk(&rev);
688 static void log_setup_revisions_tweak(struct rev_info *rev,
689 struct setup_revision_opt *opt)
691 if (rev->diffopt.flags.default_follow_renames &&
692 rev->prune_data.nr == 1)
693 rev->diffopt.flags.follow_renames = 1;
695 /* Turn --cc/-c into -p --cc/-c when -p was not given */
696 if (!rev->diffopt.output_format && rev->combine_merges)
697 rev->diffopt.output_format = DIFF_FORMAT_PATCH;
699 /* Turn -m on when --cc/-c was given */
700 if (rev->combine_merges)
701 rev->ignore_merges = 0;
704 int cmd_log(int argc, const char **argv, const char *prefix)
707 struct setup_revision_opt opt;
710 git_config(git_log_config, NULL);
712 repo_init_revisions(the_repository, &rev, prefix);
713 rev.always_show_header = 1;
714 memset(&opt, 0, sizeof(opt));
716 opt.revarg_opt = REVARG_COMMITTISH;
717 opt.tweak = log_setup_revisions_tweak;
718 cmd_log_init(argc, argv, prefix, &rev, &opt);
719 return cmd_log_walk(&rev);
724 static const char *fmt_patch_suffix = ".patch";
725 static int numbered = 0;
726 static int auto_number = 1;
728 static char *default_attach = NULL;
730 static struct string_list extra_hdr = STRING_LIST_INIT_NODUP;
731 static struct string_list extra_to = STRING_LIST_INIT_NODUP;
732 static struct string_list extra_cc = STRING_LIST_INIT_NODUP;
734 static void add_header(const char *value)
736 struct string_list_item *item;
737 int len = strlen(value);
738 while (len && value[len - 1] == '\n')
741 if (!strncasecmp(value, "to: ", 4)) {
742 item = string_list_append(&extra_to, value + 4);
744 } else if (!strncasecmp(value, "cc: ", 4)) {
745 item = string_list_append(&extra_cc, value + 4);
748 item = string_list_append(&extra_hdr, value);
751 item->string[len] = '\0';
754 #define THREAD_SHALLOW 1
755 #define THREAD_DEEP 2
757 static int do_signoff;
758 static int base_auto;
760 static const char *signature = git_version_string;
761 static const char *signature_file;
762 static int config_cover_letter;
763 static const char *config_output_directory;
772 static int git_format_config(const char *var, const char *value, void *cb)
774 if (!strcmp(var, "format.headers")) {
776 die(_("format.headers without value"));
780 if (!strcmp(var, "format.suffix"))
781 return git_config_string(&fmt_patch_suffix, var, value);
782 if (!strcmp(var, "format.to")) {
784 return config_error_nonbool(var);
785 string_list_append(&extra_to, value);
788 if (!strcmp(var, "format.cc")) {
790 return config_error_nonbool(var);
791 string_list_append(&extra_cc, value);
794 if (!strcmp(var, "diff.color") || !strcmp(var, "color.diff") ||
795 !strcmp(var, "color.ui") || !strcmp(var, "diff.submodule")) {
798 if (!strcmp(var, "format.numbered")) {
799 if (value && !strcasecmp(value, "auto")) {
803 numbered = git_config_bool(var, value);
804 auto_number = auto_number && numbered;
807 if (!strcmp(var, "format.attach")) {
809 default_attach = xstrdup(value);
811 default_attach = xstrdup(git_version_string);
814 if (!strcmp(var, "format.thread")) {
815 if (value && !strcasecmp(value, "deep")) {
816 thread = THREAD_DEEP;
819 if (value && !strcasecmp(value, "shallow")) {
820 thread = THREAD_SHALLOW;
823 thread = git_config_bool(var, value) && THREAD_SHALLOW;
826 if (!strcmp(var, "format.signoff")) {
827 do_signoff = git_config_bool(var, value);
830 if (!strcmp(var, "format.signature"))
831 return git_config_string(&signature, var, value);
832 if (!strcmp(var, "format.signaturefile"))
833 return git_config_pathname(&signature_file, var, value);
834 if (!strcmp(var, "format.coverletter")) {
835 if (value && !strcasecmp(value, "auto")) {
836 config_cover_letter = COVER_AUTO;
839 config_cover_letter = git_config_bool(var, value) ? COVER_ON : COVER_OFF;
842 if (!strcmp(var, "format.outputdirectory"))
843 return git_config_string(&config_output_directory, var, value);
844 if (!strcmp(var, "format.useautobase")) {
845 base_auto = git_config_bool(var, value);
848 if (!strcmp(var, "format.from")) {
849 int b = git_parse_maybe_bool(value);
852 from = xstrdup(value);
854 from = xstrdup(git_committer_info(IDENT_NO_DATE));
860 return git_log_config(var, value, cb);
863 static const char *output_directory = NULL;
864 static int outdir_offset;
866 static int open_next_file(struct commit *commit, const char *subject,
867 struct rev_info *rev, int quiet)
869 struct strbuf filename = STRBUF_INIT;
870 int suffix_len = strlen(rev->patch_suffix) + 1;
872 if (output_directory) {
873 strbuf_addstr(&filename, output_directory);
875 PATH_MAX - FORMAT_PATCH_NAME_MAX - suffix_len) {
876 strbuf_release(&filename);
877 return error(_("name of output directory is too long"));
879 strbuf_complete(&filename, '/');
882 if (rev->numbered_files)
883 strbuf_addf(&filename, "%d", rev->nr);
885 fmt_output_commit(&filename, commit, rev);
887 fmt_output_subject(&filename, subject, rev);
890 printf("%s\n", filename.buf + outdir_offset);
892 if ((rev->diffopt.file = fopen(filename.buf, "w")) == NULL) {
893 error_errno(_("Cannot open patch file %s"), filename.buf);
894 strbuf_release(&filename);
898 strbuf_release(&filename);
902 static void get_patch_ids(struct rev_info *rev, struct patch_ids *ids)
904 struct rev_info check_rev;
905 struct commit *commit, *c1, *c2;
906 struct object *o1, *o2;
907 unsigned flags1, flags2;
909 if (rev->pending.nr != 2)
910 die(_("Need exactly one range."));
912 o1 = rev->pending.objects[0].item;
913 o2 = rev->pending.objects[1].item;
916 c1 = lookup_commit_reference(the_repository, &o1->oid);
917 c2 = lookup_commit_reference(the_repository, &o2->oid);
919 if ((flags1 & UNINTERESTING) == (flags2 & UNINTERESTING))
920 die(_("Not a range."));
922 init_patch_ids(the_repository, ids);
924 /* given a range a..b get all patch ids for b..a */
925 repo_init_revisions(the_repository, &check_rev, rev->prefix);
926 check_rev.max_parents = 1;
927 o1->flags ^= UNINTERESTING;
928 o2->flags ^= UNINTERESTING;
929 add_pending_object(&check_rev, o1, "o1");
930 add_pending_object(&check_rev, o2, "o2");
931 if (prepare_revision_walk(&check_rev))
932 die(_("revision walk setup failed"));
934 while ((commit = get_revision(&check_rev)) != NULL) {
935 add_commit_patch_id(commit, ids);
938 /* reset for next revision walk */
939 clear_commit_marks(c1, SEEN | UNINTERESTING | SHOWN | ADDED);
940 clear_commit_marks(c2, SEEN | UNINTERESTING | SHOWN | ADDED);
945 static void gen_message_id(struct rev_info *info, char *base)
947 struct strbuf buf = STRBUF_INIT;
948 strbuf_addf(&buf, "%s.%"PRItime".git.%s", base,
949 (timestamp_t) time(NULL),
950 git_committer_info(IDENT_NO_NAME|IDENT_NO_DATE|IDENT_STRICT));
951 info->message_id = strbuf_detach(&buf, NULL);
954 static void print_signature(FILE *file)
956 if (!signature || !*signature)
959 fprintf(file, "-- \n%s", signature);
960 if (signature[strlen(signature)-1] != '\n')
965 static void add_branch_description(struct strbuf *buf, const char *branch_name)
967 struct strbuf desc = STRBUF_INIT;
968 if (!branch_name || !*branch_name)
970 read_branch_desc(&desc, branch_name);
972 strbuf_addch(buf, '\n');
973 strbuf_addbuf(buf, &desc);
974 strbuf_addch(buf, '\n');
976 strbuf_release(&desc);
979 static char *find_branch_name(struct rev_info *rev)
981 int i, positive = -1;
982 struct object_id branch_oid;
983 const struct object_id *tip_oid;
985 char *full_ref, *branch = NULL;
987 for (i = 0; i < rev->cmdline.nr; i++) {
988 if (rev->cmdline.rev[i].flags & UNINTERESTING)
997 ref = rev->cmdline.rev[positive].name;
998 tip_oid = &rev->cmdline.rev[positive].item->oid;
999 if (dwim_ref(ref, strlen(ref), &branch_oid, &full_ref) &&
1000 skip_prefix(full_ref, "refs/heads/", &v) &&
1001 oideq(tip_oid, &branch_oid))
1002 branch = xstrdup(v);
1007 static void show_diffstat(struct rev_info *rev,
1008 struct commit *origin, struct commit *head)
1010 struct diff_options opts;
1012 memcpy(&opts, &rev->diffopt, sizeof(opts));
1013 opts.output_format = DIFF_FORMAT_SUMMARY | DIFF_FORMAT_DIFFSTAT;
1014 opts.stat_width = MAIL_DEFAULT_WRAP;
1016 diff_setup_done(&opts);
1018 diff_tree_oid(get_commit_tree_oid(origin),
1019 get_commit_tree_oid(head),
1021 diffcore_std(&opts);
1024 fprintf(rev->diffopt.file, "\n");
1027 static void make_cover_letter(struct rev_info *rev, int use_stdout,
1028 struct commit *origin,
1029 int nr, struct commit **list,
1030 const char *branch_name,
1033 const char *committer;
1034 const char *body = "*** SUBJECT HERE ***\n\n*** BLURB HERE ***\n";
1036 struct shortlog log;
1037 struct strbuf sb = STRBUF_INIT;
1039 const char *encoding = "UTF-8";
1040 int need_8bit_cte = 0;
1041 struct pretty_print_context pp = {0};
1042 struct commit *head = list[0];
1044 if (!cmit_fmt_is_mail(rev->commit_format))
1045 die(_("Cover letter needs email format"));
1047 committer = git_committer_info(0);
1050 open_next_file(NULL, rev->numbered_files ? NULL : "cover-letter", rev, quiet))
1053 log_write_email_headers(rev, head, &pp.after_subject, &need_8bit_cte, 0);
1055 for (i = 0; !need_8bit_cte && i < nr; i++) {
1056 const char *buf = get_commit_buffer(list[i], NULL);
1057 if (has_non_ascii(buf))
1059 unuse_commit_buffer(list[i], buf);
1063 branch_name = find_branch_name(rev);
1066 pp.fmt = CMIT_FMT_EMAIL;
1067 pp.date_mode.type = DATE_RFC2822;
1069 pp.print_email_subject = 1;
1070 pp_user_info(&pp, NULL, &sb, committer, encoding);
1071 pp_title_line(&pp, &msg, &sb, encoding, need_8bit_cte);
1072 pp_remainder(&pp, &msg, &sb, 0);
1073 add_branch_description(&sb, branch_name);
1074 fprintf(rev->diffopt.file, "%s\n", sb.buf);
1076 strbuf_release(&sb);
1078 shortlog_init(&log);
1080 log.wrap = MAIL_DEFAULT_WRAP;
1083 log.file = rev->diffopt.file;
1084 for (i = 0; i < nr; i++)
1085 shortlog_add_commit(&log, list[i]);
1087 shortlog_output(&log);
1089 /* We can only do diffstat with a unique reference point */
1091 show_diffstat(rev, origin, head);
1093 if (rev->idiff_oid1) {
1094 fprintf_ln(rev->diffopt.file, "%s", rev->idiff_title);
1095 show_interdiff(rev, 0);
1099 fprintf_ln(rev->diffopt.file, "%s", rev->rdiff_title);
1100 show_range_diff(rev->rdiff1, rev->rdiff2,
1101 rev->creation_factor, 1, &rev->diffopt);
1105 static const char *clean_message_id(const char *msg_id)
1108 const char *a, *z, *m;
1111 while ((ch = *m) && (isspace(ch) || (ch == '<')))
1116 if (!isspace(ch) && (ch != '>'))
1121 die(_("insane in-reply-to: %s"), msg_id);
1124 return xmemdupz(a, z - a);
1127 static const char *set_outdir(const char *prefix, const char *output_directory)
1129 if (output_directory && is_absolute_path(output_directory))
1130 return output_directory;
1132 if (!prefix || !*prefix) {
1133 if (output_directory)
1134 return output_directory;
1135 /* The user did not explicitly ask for "./" */
1140 outdir_offset = strlen(prefix);
1141 if (!output_directory)
1144 return prefix_filename(prefix, output_directory);
1147 static const char * const builtin_format_patch_usage[] = {
1148 N_("git format-patch [<options>] [<since> | <revision-range>]"),
1152 static int keep_subject = 0;
1154 static int keep_callback(const struct option *opt, const char *arg, int unset)
1156 BUG_ON_OPT_NEG(unset);
1157 BUG_ON_OPT_ARG(arg);
1158 ((struct rev_info *)opt->value)->total = -1;
1163 static int subject_prefix = 0;
1165 static int subject_prefix_callback(const struct option *opt, const char *arg,
1168 BUG_ON_OPT_NEG(unset);
1170 ((struct rev_info *)opt->value)->subject_prefix = arg;
1174 static int rfc_callback(const struct option *opt, const char *arg, int unset)
1176 BUG_ON_OPT_NEG(unset);
1177 BUG_ON_OPT_ARG(arg);
1178 return subject_prefix_callback(opt, "RFC PATCH", unset);
1181 static int numbered_cmdline_opt = 0;
1183 static int numbered_callback(const struct option *opt, const char *arg,
1186 BUG_ON_OPT_ARG(arg);
1187 *(int *)opt->value = numbered_cmdline_opt = unset ? 0 : 1;
1193 static int no_numbered_callback(const struct option *opt, const char *arg,
1196 BUG_ON_OPT_NEG(unset);
1197 return numbered_callback(opt, arg, 1);
1200 static int output_directory_callback(const struct option *opt, const char *arg,
1203 const char **dir = (const char **)opt->value;
1204 BUG_ON_OPT_NEG(unset);
1206 die(_("Two output directories?"));
1211 static int thread_callback(const struct option *opt, const char *arg, int unset)
1213 int *thread = (int *)opt->value;
1216 else if (!arg || !strcmp(arg, "shallow"))
1217 *thread = THREAD_SHALLOW;
1218 else if (!strcmp(arg, "deep"))
1219 *thread = THREAD_DEEP;
1225 static int attach_callback(const struct option *opt, const char *arg, int unset)
1227 struct rev_info *rev = (struct rev_info *)opt->value;
1229 rev->mime_boundary = NULL;
1231 rev->mime_boundary = arg;
1233 rev->mime_boundary = git_version_string;
1234 rev->no_inline = unset ? 0 : 1;
1238 static int inline_callback(const struct option *opt, const char *arg, int unset)
1240 struct rev_info *rev = (struct rev_info *)opt->value;
1242 rev->mime_boundary = NULL;
1244 rev->mime_boundary = arg;
1246 rev->mime_boundary = git_version_string;
1251 static int header_callback(const struct option *opt, const char *arg, int unset)
1254 string_list_clear(&extra_hdr, 0);
1255 string_list_clear(&extra_to, 0);
1256 string_list_clear(&extra_cc, 0);
1263 static int to_callback(const struct option *opt, const char *arg, int unset)
1266 string_list_clear(&extra_to, 0);
1268 string_list_append(&extra_to, arg);
1272 static int cc_callback(const struct option *opt, const char *arg, int unset)
1275 string_list_clear(&extra_cc, 0);
1277 string_list_append(&extra_cc, arg);
1281 static int from_callback(const struct option *opt, const char *arg, int unset)
1283 char **from = opt->value;
1290 *from = xstrdup(arg);
1292 *from = xstrdup(git_committer_info(IDENT_NO_DATE));
1296 struct base_tree_info {
1297 struct object_id base_commit;
1298 int nr_patch_id, alloc_patch_id;
1299 struct object_id *patch_id;
1302 static struct commit *get_base_commit(const char *base_commit,
1303 struct commit **list,
1306 struct commit *base = NULL;
1307 struct commit **rev;
1308 int i = 0, rev_nr = 0;
1310 if (base_commit && strcmp(base_commit, "auto")) {
1311 base = lookup_commit_reference_by_name(base_commit);
1313 die(_("Unknown commit %s"), base_commit);
1314 } else if ((base_commit && !strcmp(base_commit, "auto")) || base_auto) {
1315 struct branch *curr_branch = branch_get(NULL);
1316 const char *upstream = branch_get_upstream(curr_branch, NULL);
1318 struct commit_list *base_list;
1319 struct commit *commit;
1320 struct object_id oid;
1322 if (get_oid(upstream, &oid))
1323 die(_("Failed to resolve '%s' as a valid ref."), upstream);
1324 commit = lookup_commit_or_die(&oid, "upstream base");
1325 base_list = get_merge_bases_many(commit, total, list);
1326 /* There should be one and only one merge base. */
1327 if (!base_list || base_list->next)
1328 die(_("Could not find exact merge base."));
1329 base = base_list->item;
1330 free_commit_list(base_list);
1332 die(_("Failed to get upstream, if you want to record base commit automatically,\n"
1333 "please use git branch --set-upstream-to to track a remote branch.\n"
1334 "Or you could specify base commit by --base=<base-commit-id> manually."));
1338 ALLOC_ARRAY(rev, total);
1339 for (i = 0; i < total; i++)
1344 * Get merge base through pair-wise computations
1345 * and store it in rev[0].
1347 while (rev_nr > 1) {
1348 for (i = 0; i < rev_nr / 2; i++) {
1349 struct commit_list *merge_base;
1350 merge_base = get_merge_bases(rev[2 * i], rev[2 * i + 1]);
1351 if (!merge_base || merge_base->next)
1352 die(_("Failed to find exact merge base"));
1354 rev[i] = merge_base->item;
1358 rev[i] = rev[2 * i];
1359 rev_nr = DIV_ROUND_UP(rev_nr, 2);
1362 if (!in_merge_bases(base, rev[0]))
1363 die(_("base commit should be the ancestor of revision list"));
1365 for (i = 0; i < total; i++) {
1366 if (base == list[i])
1367 die(_("base commit shouldn't be in revision list"));
1374 define_commit_slab(commit_base, int);
1376 static void prepare_bases(struct base_tree_info *bases,
1377 struct commit *base,
1378 struct commit **list,
1381 struct commit *commit;
1382 struct rev_info revs;
1383 struct diff_options diffopt;
1384 struct commit_base commit_base;
1390 init_commit_base(&commit_base);
1391 repo_diff_setup(the_repository, &diffopt);
1392 diffopt.flags.recursive = 1;
1393 diff_setup_done(&diffopt);
1395 oidcpy(&bases->base_commit, &base->object.oid);
1397 repo_init_revisions(the_repository, &revs, NULL);
1398 revs.max_parents = 1;
1399 revs.topo_order = 1;
1400 for (i = 0; i < total; i++) {
1401 list[i]->object.flags &= ~UNINTERESTING;
1402 add_pending_object(&revs, &list[i]->object, "rev_list");
1403 *commit_base_at(&commit_base, list[i]) = 1;
1405 base->object.flags |= UNINTERESTING;
1406 add_pending_object(&revs, &base->object, "base");
1408 if (prepare_revision_walk(&revs))
1409 die(_("revision walk setup failed"));
1411 * Traverse the commits list, get prerequisite patch ids
1412 * and stuff them in bases structure.
1414 while ((commit = get_revision(&revs)) != NULL) {
1415 struct object_id oid;
1416 struct object_id *patch_id;
1417 if (*commit_base_at(&commit_base, commit))
1419 if (commit_patch_id(commit, &diffopt, &oid, 0))
1420 die(_("cannot get patch id"));
1421 ALLOC_GROW(bases->patch_id, bases->nr_patch_id + 1, bases->alloc_patch_id);
1422 patch_id = bases->patch_id + bases->nr_patch_id;
1423 oidcpy(patch_id, &oid);
1424 bases->nr_patch_id++;
1426 clear_commit_base(&commit_base);
1429 static void print_bases(struct base_tree_info *bases, FILE *file)
1433 /* Only do this once, either for the cover or for the first one */
1434 if (is_null_oid(&bases->base_commit))
1437 /* Show the base commit */
1438 fprintf(file, "\nbase-commit: %s\n", oid_to_hex(&bases->base_commit));
1440 /* Show the prerequisite patches */
1441 for (i = bases->nr_patch_id - 1; i >= 0; i--)
1442 fprintf(file, "prerequisite-patch-id: %s\n", oid_to_hex(&bases->patch_id[i]));
1444 free(bases->patch_id);
1445 bases->nr_patch_id = 0;
1446 bases->alloc_patch_id = 0;
1447 oidclr(&bases->base_commit);
1450 static const char *diff_title(struct strbuf *sb, int reroll_count,
1451 const char *generic, const char *rerolled)
1453 if (reroll_count <= 0)
1454 strbuf_addstr(sb, generic);
1455 else /* RFC may be v0, so allow -v1 to diff against v0 */
1456 strbuf_addf(sb, rerolled, reroll_count - 1);
1460 static void infer_range_diff_ranges(struct strbuf *r1,
1463 struct commit *origin,
1464 struct commit *head)
1466 const char *head_oid = oid_to_hex(&head->object.oid);
1468 if (!strstr(prev, "..")) {
1469 strbuf_addf(r1, "%s..%s", head_oid, prev);
1470 strbuf_addf(r2, "%s..%s", prev, head_oid);
1471 } else if (!origin) {
1472 die(_("failed to infer range-diff ranges"));
1474 strbuf_addstr(r1, prev);
1475 strbuf_addf(r2, "%s..%s",
1476 oid_to_hex(&origin->object.oid), head_oid);
1480 int cmd_format_patch(int argc, const char **argv, const char *prefix)
1482 struct commit *commit;
1483 struct commit **list = NULL;
1484 struct rev_info rev;
1485 struct setup_revision_opt s_r_opt;
1486 int nr = 0, total, i;
1488 int start_number = -1;
1489 int just_numbers = 0;
1490 int ignore_if_in_upstream = 0;
1491 int cover_letter = -1;
1492 int boundary_count = 0;
1493 int no_binary_diff = 0;
1494 int zero_commit = 0;
1495 struct commit *origin = NULL;
1496 const char *in_reply_to = NULL;
1497 struct patch_ids ids;
1498 struct strbuf buf = STRBUF_INIT;
1499 int use_patch_format = 0;
1501 int reroll_count = -1;
1502 char *branch_name = NULL;
1503 char *base_commit = NULL;
1504 struct base_tree_info bases;
1505 int show_progress = 0;
1506 struct progress *progress = NULL;
1507 struct oid_array idiff_prev = OID_ARRAY_INIT;
1508 struct strbuf idiff_title = STRBUF_INIT;
1509 const char *rdiff_prev = NULL;
1510 struct strbuf rdiff1 = STRBUF_INIT;
1511 struct strbuf rdiff2 = STRBUF_INIT;
1512 struct strbuf rdiff_title = STRBUF_INIT;
1513 int creation_factor = -1;
1515 const struct option builtin_format_patch_options[] = {
1516 { OPTION_CALLBACK, 'n', "numbered", &numbered, NULL,
1517 N_("use [PATCH n/m] even with a single patch"),
1518 PARSE_OPT_NOARG, numbered_callback },
1519 { OPTION_CALLBACK, 'N', "no-numbered", &numbered, NULL,
1520 N_("use [PATCH] even with multiple patches"),
1521 PARSE_OPT_NOARG | PARSE_OPT_NONEG, no_numbered_callback },
1522 OPT_BOOL('s', "signoff", &do_signoff, N_("add Signed-off-by:")),
1523 OPT_BOOL(0, "stdout", &use_stdout,
1524 N_("print patches to standard out")),
1525 OPT_BOOL(0, "cover-letter", &cover_letter,
1526 N_("generate a cover letter")),
1527 OPT_BOOL(0, "numbered-files", &just_numbers,
1528 N_("use simple number sequence for output file names")),
1529 OPT_STRING(0, "suffix", &fmt_patch_suffix, N_("sfx"),
1530 N_("use <sfx> instead of '.patch'")),
1531 OPT_INTEGER(0, "start-number", &start_number,
1532 N_("start numbering patches at <n> instead of 1")),
1533 OPT_INTEGER('v', "reroll-count", &reroll_count,
1534 N_("mark the series as Nth re-roll")),
1535 { OPTION_CALLBACK, 0, "rfc", &rev, NULL,
1536 N_("Use [RFC PATCH] instead of [PATCH]"),
1537 PARSE_OPT_NOARG | PARSE_OPT_NONEG, rfc_callback },
1538 { OPTION_CALLBACK, 0, "subject-prefix", &rev, N_("prefix"),
1539 N_("Use [<prefix>] instead of [PATCH]"),
1540 PARSE_OPT_NONEG, subject_prefix_callback },
1541 { OPTION_CALLBACK, 'o', "output-directory", &output_directory,
1542 N_("dir"), N_("store resulting files in <dir>"),
1543 PARSE_OPT_NONEG, output_directory_callback },
1544 { OPTION_CALLBACK, 'k', "keep-subject", &rev, NULL,
1545 N_("don't strip/add [PATCH]"),
1546 PARSE_OPT_NOARG | PARSE_OPT_NONEG, keep_callback },
1547 OPT_BOOL(0, "no-binary", &no_binary_diff,
1548 N_("don't output binary diffs")),
1549 OPT_BOOL(0, "zero-commit", &zero_commit,
1550 N_("output all-zero hash in From header")),
1551 OPT_BOOL(0, "ignore-if-in-upstream", &ignore_if_in_upstream,
1552 N_("don't include a patch matching a commit upstream")),
1553 OPT_SET_INT_F('p', "no-stat", &use_patch_format,
1554 N_("show patch format instead of default (patch + stat)"),
1555 1, PARSE_OPT_NONEG),
1556 OPT_GROUP(N_("Messaging")),
1557 { OPTION_CALLBACK, 0, "add-header", NULL, N_("header"),
1558 N_("add email header"), 0, header_callback },
1559 { OPTION_CALLBACK, 0, "to", NULL, N_("email"), N_("add To: header"),
1561 { OPTION_CALLBACK, 0, "cc", NULL, N_("email"), N_("add Cc: header"),
1563 { OPTION_CALLBACK, 0, "from", &from, N_("ident"),
1564 N_("set From address to <ident> (or committer ident if absent)"),
1565 PARSE_OPT_OPTARG, from_callback },
1566 OPT_STRING(0, "in-reply-to", &in_reply_to, N_("message-id"),
1567 N_("make first mail a reply to <message-id>")),
1568 { OPTION_CALLBACK, 0, "attach", &rev, N_("boundary"),
1569 N_("attach the patch"), PARSE_OPT_OPTARG,
1571 { OPTION_CALLBACK, 0, "inline", &rev, N_("boundary"),
1572 N_("inline the patch"),
1573 PARSE_OPT_OPTARG | PARSE_OPT_NONEG,
1575 { OPTION_CALLBACK, 0, "thread", &thread, N_("style"),
1576 N_("enable message threading, styles: shallow, deep"),
1577 PARSE_OPT_OPTARG, thread_callback },
1578 OPT_STRING(0, "signature", &signature, N_("signature"),
1579 N_("add a signature")),
1580 OPT_STRING(0, "base", &base_commit, N_("base-commit"),
1581 N_("add prerequisite tree info to the patch series")),
1582 OPT_FILENAME(0, "signature-file", &signature_file,
1583 N_("add a signature from a file")),
1584 OPT__QUIET(&quiet, N_("don't print the patch filenames")),
1585 OPT_BOOL(0, "progress", &show_progress,
1586 N_("show progress while generating patches")),
1587 OPT_CALLBACK(0, "interdiff", &idiff_prev, N_("rev"),
1588 N_("show changes against <rev> in cover letter or single patch"),
1589 parse_opt_object_name),
1590 OPT_STRING(0, "range-diff", &rdiff_prev, N_("refspec"),
1591 N_("show changes against <refspec> in cover letter or single patch")),
1592 OPT_INTEGER(0, "creation-factor", &creation_factor,
1593 N_("percentage by which creation is weighted")),
1597 extra_hdr.strdup_strings = 1;
1598 extra_to.strdup_strings = 1;
1599 extra_cc.strdup_strings = 1;
1600 init_log_defaults();
1601 git_config(git_format_config, NULL);
1602 repo_init_revisions(the_repository, &rev, prefix);
1603 rev.commit_format = CMIT_FMT_EMAIL;
1604 rev.expand_tabs_in_log_default = 0;
1605 rev.verbose_header = 1;
1607 rev.max_parents = 1;
1608 rev.diffopt.flags.recursive = 1;
1609 rev.subject_prefix = fmt_patch_subject_prefix;
1610 memset(&s_r_opt, 0, sizeof(s_r_opt));
1611 s_r_opt.def = "HEAD";
1612 s_r_opt.revarg_opt = REVARG_COMMITTISH;
1614 if (default_attach) {
1615 rev.mime_boundary = default_attach;
1620 * Parse the arguments before setup_revisions(), or something
1621 * like "git format-patch -o a123 HEAD^.." may fail; a123 is
1622 * possibly a valid SHA1.
1624 argc = parse_options(argc, argv, prefix, builtin_format_patch_options,
1625 builtin_format_patch_usage,
1626 PARSE_OPT_KEEP_ARGV0 | PARSE_OPT_KEEP_UNKNOWN |
1627 PARSE_OPT_KEEP_DASHDASH);
1629 if (0 < reroll_count) {
1630 struct strbuf sprefix = STRBUF_INIT;
1631 strbuf_addf(&sprefix, "%s v%d",
1632 rev.subject_prefix, reroll_count);
1633 rev.reroll_count = reroll_count;
1634 rev.subject_prefix = strbuf_detach(&sprefix, NULL);
1637 for (i = 0; i < extra_hdr.nr; i++) {
1638 strbuf_addstr(&buf, extra_hdr.items[i].string);
1639 strbuf_addch(&buf, '\n');
1643 strbuf_addstr(&buf, "To: ");
1644 for (i = 0; i < extra_to.nr; i++) {
1646 strbuf_addstr(&buf, " ");
1647 strbuf_addstr(&buf, extra_to.items[i].string);
1648 if (i + 1 < extra_to.nr)
1649 strbuf_addch(&buf, ',');
1650 strbuf_addch(&buf, '\n');
1654 strbuf_addstr(&buf, "Cc: ");
1655 for (i = 0; i < extra_cc.nr; i++) {
1657 strbuf_addstr(&buf, " ");
1658 strbuf_addstr(&buf, extra_cc.items[i].string);
1659 if (i + 1 < extra_cc.nr)
1660 strbuf_addch(&buf, ',');
1661 strbuf_addch(&buf, '\n');
1664 rev.extra_headers = strbuf_detach(&buf, NULL);
1667 if (split_ident_line(&rev.from_ident, from, strlen(from)))
1668 die(_("invalid ident line: %s"), from);
1671 if (start_number < 0)
1675 * If numbered is set solely due to format.numbered in config,
1676 * and it would conflict with --keep-subject (-k) from the
1677 * command line, reset "numbered".
1679 if (numbered && keep_subject && !numbered_cmdline_opt)
1682 if (numbered && keep_subject)
1683 die(_("-n and -k are mutually exclusive"));
1684 if (keep_subject && subject_prefix)
1685 die(_("--subject-prefix/--rfc and -k are mutually exclusive"));
1686 rev.preserve_subject = keep_subject;
1688 argc = setup_revisions(argc, argv, &rev, &s_r_opt);
1690 die(_("unrecognized argument: %s"), argv[1]);
1692 if (rev.diffopt.output_format & DIFF_FORMAT_NAME)
1693 die(_("--name-only does not make sense"));
1694 if (rev.diffopt.output_format & DIFF_FORMAT_NAME_STATUS)
1695 die(_("--name-status does not make sense"));
1696 if (rev.diffopt.output_format & DIFF_FORMAT_CHECKDIFF)
1697 die(_("--check does not make sense"));
1699 if (!use_patch_format &&
1700 (!rev.diffopt.output_format ||
1701 rev.diffopt.output_format == DIFF_FORMAT_PATCH))
1702 rev.diffopt.output_format = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_SUMMARY;
1703 if (!rev.diffopt.stat_width)
1704 rev.diffopt.stat_width = MAIL_DEFAULT_WRAP;
1706 /* Always generate a patch */
1707 rev.diffopt.output_format |= DIFF_FORMAT_PATCH;
1709 rev.zero_commit = zero_commit;
1711 if (!rev.diffopt.flags.text && !no_binary_diff)
1712 rev.diffopt.flags.binary = 1;
1715 init_display_notes(&rev.notes_opt);
1717 if (!output_directory && !use_stdout)
1718 output_directory = config_output_directory;
1721 output_directory = set_outdir(prefix, output_directory);
1725 if (output_directory) {
1726 if (rev.diffopt.use_color != GIT_COLOR_ALWAYS)
1727 rev.diffopt.use_color = GIT_COLOR_NEVER;
1729 die(_("standard output, or directory, which one?"));
1730 if (mkdir(output_directory, 0777) < 0 && errno != EEXIST)
1731 die_errno(_("Could not create directory '%s'"),
1735 if (rev.pending.nr == 1) {
1738 if (rev.max_count < 0 && !rev.show_root_diff) {
1740 * This is traditional behaviour of "git format-patch
1741 * origin" that prepares what the origin side still
1744 rev.pending.objects[0].item->flags |= UNINTERESTING;
1745 add_head_to_pending(&rev);
1749 * Otherwise, it is "format-patch -22 HEAD", and/or
1750 * "format-patch --root HEAD". The user wants
1751 * get_revision() to do the usual traversal.
1754 if (!strcmp(rev.pending.objects[0].name, "HEAD"))
1758 const char *ref, *v;
1759 ref = resolve_ref_unsafe("HEAD", RESOLVE_REF_READING,
1761 if (ref && skip_prefix(ref, "refs/heads/", &v))
1762 branch_name = xstrdup(v);
1764 branch_name = xstrdup(""); /* no branch */
1769 * We cannot move this anywhere earlier because we do want to
1770 * know if --root was given explicitly from the command line.
1772 rev.show_root_diff = 1;
1774 if (ignore_if_in_upstream) {
1775 /* Don't say anything if head and upstream are the same. */
1776 if (rev.pending.nr == 2) {
1777 struct object_array_entry *o = rev.pending.objects;
1778 if (oideq(&o[0].item->oid, &o[1].item->oid))
1781 get_patch_ids(&rev, &ids);
1784 if (prepare_revision_walk(&rev))
1785 die(_("revision walk setup failed"));
1787 while ((commit = get_revision(&rev)) != NULL) {
1788 if (commit->object.flags & BOUNDARY) {
1790 origin = (boundary_count == 1) ? commit : NULL;
1794 if (ignore_if_in_upstream && has_commit_patch_id(commit, &ids))
1798 REALLOC_ARRAY(list, nr);
1799 list[nr - 1] = commit;
1805 if (cover_letter == -1) {
1806 if (config_cover_letter == COVER_AUTO)
1807 cover_letter = (total > 1);
1809 cover_letter = (config_cover_letter == COVER_ON);
1811 if (!keep_subject && auto_number && (total > 1 || cover_letter))
1814 rev.total = total + start_number - 1;
1816 if (idiff_prev.nr) {
1817 if (!cover_letter && total != 1)
1818 die(_("--interdiff requires --cover-letter or single patch"));
1819 rev.idiff_oid1 = &idiff_prev.oid[idiff_prev.nr - 1];
1820 rev.idiff_oid2 = get_commit_tree_oid(list[0]);
1821 rev.idiff_title = diff_title(&idiff_title, reroll_count,
1823 _("Interdiff against v%d:"));
1826 if (creation_factor < 0)
1827 creation_factor = RANGE_DIFF_CREATION_FACTOR_DEFAULT;
1828 else if (!rdiff_prev)
1829 die(_("--creation-factor requires --range-diff"));
1832 if (!cover_letter && total != 1)
1833 die(_("--range-diff requires --cover-letter or single patch"));
1835 infer_range_diff_ranges(&rdiff1, &rdiff2, rdiff_prev,
1837 rev.rdiff1 = rdiff1.buf;
1838 rev.rdiff2 = rdiff2.buf;
1839 rev.creation_factor = creation_factor;
1840 rev.rdiff_title = diff_title(&rdiff_title, reroll_count,
1842 _("Range-diff against v%d:"));
1846 ; /* --no-signature inhibits all signatures */
1847 } else if (signature && signature != git_version_string) {
1848 ; /* non-default signature already set */
1849 } else if (signature_file) {
1850 struct strbuf buf = STRBUF_INIT;
1852 if (strbuf_read_file(&buf, signature_file, 128) < 0)
1853 die_errno(_("unable to read signature file '%s'"), signature_file);
1854 signature = strbuf_detach(&buf, NULL);
1857 memset(&bases, 0, sizeof(bases));
1858 if (base_commit || base_auto) {
1859 struct commit *base = get_base_commit(base_commit, list, nr);
1860 reset_revision_walk();
1861 clear_object_flags(UNINTERESTING);
1862 prepare_bases(&bases, base, list, nr);
1865 if (in_reply_to || thread || cover_letter)
1866 rev.ref_message_ids = xcalloc(1, sizeof(struct string_list));
1868 const char *msgid = clean_message_id(in_reply_to);
1869 string_list_append(rev.ref_message_ids, msgid);
1871 rev.numbered_files = just_numbers;
1872 rev.patch_suffix = fmt_patch_suffix;
1875 gen_message_id(&rev, "cover");
1876 make_cover_letter(&rev, use_stdout,
1877 origin, nr, list, branch_name, quiet);
1878 print_bases(&bases, rev.diffopt.file);
1879 print_signature(rev.diffopt.file);
1882 /* interdiff/range-diff in cover-letter; omit from patches */
1883 rev.idiff_oid1 = NULL;
1886 rev.add_signoff = do_signoff;
1889 progress = start_delayed_progress(_("Generating patches"), total);
1892 display_progress(progress, total - nr);
1894 rev.nr = total - nr + (start_number - 1);
1895 /* Make the second and subsequent mails replies to the first */
1897 /* Have we already had a message ID? */
1898 if (rev.message_id) {
1900 * For deep threading: make every mail
1901 * a reply to the previous one, no
1902 * matter what other options are set.
1904 * For shallow threading:
1906 * Without --cover-letter and
1907 * --in-reply-to, make every mail a
1908 * reply to the one before.
1910 * With --in-reply-to but no
1911 * --cover-letter, make every mail a
1912 * reply to the <reply-to>.
1914 * With --cover-letter, make every
1915 * mail but the cover letter a reply
1916 * to the cover letter. The cover
1917 * letter is a reply to the
1918 * --in-reply-to, if specified.
1920 if (thread == THREAD_SHALLOW
1921 && rev.ref_message_ids->nr > 0
1922 && (!cover_letter || rev.nr > 1))
1923 free(rev.message_id);
1925 string_list_append(rev.ref_message_ids,
1928 gen_message_id(&rev, oid_to_hex(&commit->object.oid));
1932 open_next_file(rev.numbered_files ? NULL : commit, NULL, &rev, quiet))
1933 die(_("Failed to create output files"));
1934 shown = log_tree_commit(&rev, commit);
1935 free_commit_buffer(commit);
1937 /* We put one extra blank line between formatted
1938 * patches and this flag is used by log-tree code
1939 * to see if it needs to emit a LF before showing
1940 * the log; when using one file per patch, we do
1941 * not want the extra blank line.
1946 print_bases(&bases, rev.diffopt.file);
1947 if (rev.mime_boundary)
1948 fprintf(rev.diffopt.file, "\n--%s%s--\n\n\n",
1949 mime_boundary_leader,
1952 print_signature(rev.diffopt.file);
1955 fclose(rev.diffopt.file);
1957 stop_progress(&progress);
1960 string_list_clear(&extra_to, 0);
1961 string_list_clear(&extra_cc, 0);
1962 string_list_clear(&extra_hdr, 0);
1963 if (ignore_if_in_upstream)
1964 free_patch_ids(&ids);
1967 oid_array_clear(&idiff_prev);
1968 strbuf_release(&idiff_title);
1969 strbuf_release(&rdiff1);
1970 strbuf_release(&rdiff2);
1971 strbuf_release(&rdiff_title);
1975 static int add_pending_commit(const char *arg, struct rev_info *revs, int flags)
1977 struct object_id oid;
1978 if (get_oid(arg, &oid) == 0) {
1979 struct commit *commit = lookup_commit_reference(the_repository,
1982 commit->object.flags |= flags;
1983 add_pending_object(revs, &commit->object, arg);
1990 static const char * const cherry_usage[] = {
1991 N_("git cherry [-v] [<upstream> [<head> [<limit>]]]"),
1995 static void print_commit(char sign, struct commit *commit, int verbose,
1996 int abbrev, FILE *file)
1999 fprintf(file, "%c %s\n", sign,
2000 find_unique_abbrev(&commit->object.oid, abbrev));
2002 struct strbuf buf = STRBUF_INIT;
2003 pp_commit_easy(CMIT_FMT_ONELINE, commit, &buf);
2004 fprintf(file, "%c %s %s\n", sign,
2005 find_unique_abbrev(&commit->object.oid, abbrev),
2007 strbuf_release(&buf);
2011 int cmd_cherry(int argc, const char **argv, const char *prefix)
2013 struct rev_info revs;
2014 struct patch_ids ids;
2015 struct commit *commit;
2016 struct commit_list *list = NULL;
2017 struct branch *current_branch;
2018 const char *upstream;
2019 const char *head = "HEAD";
2020 const char *limit = NULL;
2021 int verbose = 0, abbrev = 0;
2023 struct option options[] = {
2024 OPT__ABBREV(&abbrev),
2025 OPT__VERBOSE(&verbose, N_("be verbose")),
2029 argc = parse_options(argc, argv, prefix, options, cherry_usage, 0);
2042 current_branch = branch_get(NULL);
2043 upstream = branch_get_upstream(current_branch, NULL);
2045 fprintf(stderr, _("Could not find a tracked"
2046 " remote branch, please"
2047 " specify <upstream> manually.\n"));
2048 usage_with_options(cherry_usage, options);
2052 repo_init_revisions(the_repository, &revs, prefix);
2053 revs.max_parents = 1;
2055 if (add_pending_commit(head, &revs, 0))
2056 die(_("Unknown commit %s"), head);
2057 if (add_pending_commit(upstream, &revs, UNINTERESTING))
2058 die(_("Unknown commit %s"), upstream);
2060 /* Don't say anything if head and upstream are the same. */
2061 if (revs.pending.nr == 2) {
2062 struct object_array_entry *o = revs.pending.objects;
2063 if (oideq(&o[0].item->oid, &o[1].item->oid))
2067 get_patch_ids(&revs, &ids);
2069 if (limit && add_pending_commit(limit, &revs, UNINTERESTING))
2070 die(_("Unknown commit %s"), limit);
2072 /* reverse the list of commits */
2073 if (prepare_revision_walk(&revs))
2074 die(_("revision walk setup failed"));
2075 while ((commit = get_revision(&revs)) != NULL) {
2076 commit_list_insert(commit, &list);
2082 commit = list->item;
2083 if (has_commit_patch_id(commit, &ids))
2085 print_commit(sign, commit, verbose, abbrev, revs.diffopt.file);
2089 free_patch_ids(&ids);