2 * Builtin "git log" and related commands (show, whatchanged)
4 * (C) Copyright 2006 Linus Torvalds
7 #define USE_THE_INDEX_COMPATIBILITY_MACROS
11 #include "object-store.h"
15 #include "diff-merges.h"
20 #include "reflog-walk.h"
21 #include "patch-ids.h"
22 #include "run-command.h"
25 #include "string-list.h"
26 #include "parse-options.h"
29 #include "streaming.h"
32 #include "gpg-interface.h"
34 #include "commit-slab.h"
35 #include "repository.h"
36 #include "commit-reach.h"
37 #include "range-diff.h"
39 #define MAIL_DEFAULT_WRAP 72
40 #define COVER_FROM_AUTO_MAX_SUBJECT_LEN 100
41 #define FORMAT_PATCH_NAME_MAX_DEFAULT 64
43 /* Set a default date-time format for git log ("log.date" config variable) */
44 static const char *default_date_mode = NULL;
46 static int default_abbrev_commit;
47 static int default_show_root = 1;
48 static int default_follow;
49 static int default_show_signature;
50 static int default_encode_email_headers = 1;
51 static int decoration_style;
52 static int decoration_given;
53 static int use_mailmap_config = 1;
54 static const char *fmt_patch_subject_prefix = "PATCH";
55 static int fmt_patch_name_max = FORMAT_PATCH_NAME_MAX_DEFAULT;
56 static const char *fmt_pretty;
58 static const char * const builtin_log_usage[] = {
59 N_("git log [<options>] [<revision-range>] [[--] <path>...]"),
60 N_("git show [<options>] <object>..."),
64 struct line_opt_callback_data {
67 struct string_list args;
70 static int session_is_interactive(void)
72 return isatty(1) || pager_in_use();
75 static int auto_decoration_style(void)
77 return session_is_interactive() ? DECORATE_SHORT_REFS : 0;
80 static int parse_decoration_style(const char *value)
82 switch (git_parse_maybe_bool(value)) {
84 return DECORATE_SHORT_REFS;
90 if (!strcmp(value, "full"))
91 return DECORATE_FULL_REFS;
92 else if (!strcmp(value, "short"))
93 return DECORATE_SHORT_REFS;
94 else if (!strcmp(value, "auto"))
95 return auto_decoration_style();
97 * Please update _git_log() in git-completion.bash when you
98 * add new decoration styles.
103 static int decorate_callback(const struct option *opt, const char *arg, int unset)
106 decoration_style = 0;
108 decoration_style = parse_decoration_style(arg);
110 decoration_style = DECORATE_SHORT_REFS;
112 if (decoration_style < 0)
113 die(_("invalid --decorate option: %s"), arg);
115 decoration_given = 1;
120 static int log_line_range_callback(const struct option *option, const char *arg, int unset)
122 struct line_opt_callback_data *data = option->value;
124 BUG_ON_OPT_NEG(unset);
129 data->rev->line_level_traverse = 1;
130 string_list_append(&data->args, arg);
135 static void init_log_defaults(void)
137 init_diff_ui_defaults();
139 decoration_style = auto_decoration_style();
142 static void cmd_log_init_defaults(struct rev_info *rev)
145 get_commit_format(fmt_pretty, rev);
147 rev->diffopt.flags.default_follow_renames = 1;
148 rev->verbose_header = 1;
149 rev->diffopt.flags.recursive = 1;
150 rev->diffopt.stat_width = -1; /* use full terminal width */
151 rev->diffopt.stat_graph_width = -1; /* respect statGraphWidth config */
152 rev->abbrev_commit = default_abbrev_commit;
153 rev->show_root_diff = default_show_root;
154 rev->subject_prefix = fmt_patch_subject_prefix;
155 rev->patch_name_max = fmt_patch_name_max;
156 rev->show_signature = default_show_signature;
157 rev->encode_email_headers = default_encode_email_headers;
158 rev->diffopt.flags.allow_textconv = 1;
160 if (default_date_mode)
161 parse_date_format(default_date_mode, &rev->date_mode);
164 static void cmd_log_init_finish(int argc, const char **argv, const char *prefix,
165 struct rev_info *rev, struct setup_revision_opt *opt)
167 struct userformat_want w;
168 int quiet = 0, source = 0, mailmap;
169 static struct line_opt_callback_data line_cb = {NULL, NULL, STRING_LIST_INIT_DUP};
170 static struct string_list decorate_refs_exclude = STRING_LIST_INIT_NODUP;
171 static struct string_list decorate_refs_exclude_config = STRING_LIST_INIT_NODUP;
172 static struct string_list decorate_refs_include = STRING_LIST_INIT_NODUP;
173 struct decoration_filter decoration_filter = {&decorate_refs_include,
174 &decorate_refs_exclude,
175 &decorate_refs_exclude_config};
176 static struct revision_sources revision_sources;
178 const struct option builtin_log_options[] = {
179 OPT__QUIET(&quiet, N_("suppress diff output")),
180 OPT_BOOL(0, "source", &source, N_("show source")),
181 OPT_BOOL(0, "use-mailmap", &mailmap, N_("use mail map file")),
182 OPT_ALIAS(0, "mailmap", "use-mailmap"),
183 OPT_STRING_LIST(0, "decorate-refs", &decorate_refs_include,
184 N_("pattern"), N_("only decorate refs that match <pattern>")),
185 OPT_STRING_LIST(0, "decorate-refs-exclude", &decorate_refs_exclude,
186 N_("pattern"), N_("do not decorate refs that match <pattern>")),
187 OPT_CALLBACK_F(0, "decorate", NULL, NULL, N_("decorate options"),
188 PARSE_OPT_OPTARG, decorate_callback),
189 OPT_CALLBACK('L', NULL, &line_cb, "range:file",
190 N_("trace the evolution of line range <start>,<end> or function :<funcname> in <file>"),
191 log_line_range_callback),
196 line_cb.prefix = prefix;
198 mailmap = use_mailmap_config;
199 argc = parse_options(argc, argv, prefix,
200 builtin_log_options, builtin_log_usage,
201 PARSE_OPT_KEEP_ARGV0 | PARSE_OPT_KEEP_UNKNOWN |
202 PARSE_OPT_KEEP_DASHDASH);
205 rev->diffopt.output_format |= DIFF_FORMAT_NO_OUTPUT;
206 argc = setup_revisions(argc, argv, rev, opt);
208 /* Any arguments at this point are not recognized */
210 die(_("unrecognized argument: %s"), argv[1]);
212 if (rev->line_level_traverse && rev->prune_data.nr)
213 die(_("-L<range>:<file> cannot be used with pathspec"));
215 memset(&w, 0, sizeof(w));
216 userformat_find_requirements(NULL, &w);
218 if (!rev->show_notes_given && (!rev->pretty_given || w.notes))
221 load_display_notes(&rev->notes_opt);
223 if ((rev->diffopt.pickaxe_opts & DIFF_PICKAXE_KINDS_MASK) ||
224 rev->diffopt.filter || rev->diffopt.flags.follow_renames)
225 rev->always_show_header = 0;
227 if (source || w.source) {
228 init_revision_sources(&revision_sources);
229 rev->sources = &revision_sources;
233 rev->mailmap = xcalloc(1, sizeof(struct string_list));
234 read_mailmap(rev->mailmap);
237 if (rev->pretty_given && rev->commit_format == CMIT_FMT_RAW) {
239 * "log --pretty=raw" is special; ignore UI oriented
240 * configuration variables such as decoration.
242 if (!decoration_given)
243 decoration_style = 0;
244 if (!rev->abbrev_commit_given)
245 rev->abbrev_commit = 0;
248 if (decoration_style) {
249 const struct string_list *config_exclude =
250 repo_config_get_value_multi(the_repository,
251 "log.excludeDecoration");
253 if (config_exclude) {
254 struct string_list_item *item;
255 for_each_string_list_item(item, config_exclude)
256 string_list_append(&decorate_refs_exclude_config,
260 rev->show_decorations = 1;
262 load_ref_decorations(&decoration_filter, decoration_style);
265 if (rev->line_level_traverse)
266 line_log_init(rev, line_cb.prefix, &line_cb.args);
271 static void cmd_log_init(int argc, const char **argv, const char *prefix,
272 struct rev_info *rev, struct setup_revision_opt *opt)
274 cmd_log_init_defaults(rev);
275 cmd_log_init_finish(argc, argv, prefix, rev, opt);
279 * This gives a rough estimate for how many commits we
280 * will print out in the list.
282 static int estimate_commit_count(struct commit_list *list)
287 struct commit *commit = list->item;
288 unsigned int flags = commit->object.flags;
290 if (!(flags & (TREESAME | UNINTERESTING)))
296 static void show_early_header(struct rev_info *rev, const char *stage, int nr)
298 if (rev->shown_one) {
300 if (rev->commit_format != CMIT_FMT_ONELINE)
301 putchar(rev->diffopt.line_termination);
303 fprintf(rev->diffopt.file, _("Final output: %d %s\n"), nr, stage);
306 static struct itimerval early_output_timer;
308 static void log_show_early(struct rev_info *revs, struct commit_list *list)
310 int i = revs->early_output;
312 int no_free = revs->diffopt.no_free;
314 revs->diffopt.no_free = 0;
315 sort_in_topological_order(&list, revs->sort_order);
317 struct commit *commit = list->item;
318 switch (simplify_commit(revs, commit)) {
321 int n = estimate_commit_count(list);
322 show_early_header(revs, "incomplete", n);
325 log_tree_commit(revs, commit);
331 revs->diffopt.no_free = no_free;
332 diff_free(&revs->diffopt);
338 /* Did we already get enough commits for the early output? */
340 revs->diffopt.no_free = 0;
341 diff_free(&revs->diffopt);
346 * ..if no, then repeat it twice a second until we
349 * NOTE! We don't use "it_interval", because if the
350 * reader isn't listening, we want our output to be
351 * throttled by the writing, and not have the timer
352 * trigger every second even if we're blocked on a
355 early_output_timer.it_value.tv_sec = 0;
356 early_output_timer.it_value.tv_usec = 500000;
357 setitimer(ITIMER_REAL, &early_output_timer, NULL);
360 static void early_output(int signal)
362 show_early_output = log_show_early;
365 static void setup_early_output(void)
370 * Set up the signal handler, minimally intrusively:
371 * we only set a single volatile integer word (not
372 * using sigatomic_t - trying to avoid unnecessary
373 * system dependencies and headers), and using
376 memset(&sa, 0, sizeof(sa));
377 sa.sa_handler = early_output;
378 sigemptyset(&sa.sa_mask);
379 sa.sa_flags = SA_RESTART;
380 sigaction(SIGALRM, &sa, NULL);
383 * If we can get the whole output in less than a
384 * tenth of a second, don't even bother doing the
385 * early-output thing..
387 * This is a one-time-only trigger.
389 early_output_timer.it_value.tv_sec = 0;
390 early_output_timer.it_value.tv_usec = 100000;
391 setitimer(ITIMER_REAL, &early_output_timer, NULL);
394 static void finish_early_output(struct rev_info *rev)
396 int n = estimate_commit_count(rev->commits);
397 signal(SIGALRM, SIG_IGN);
398 show_early_header(rev, "done", n);
401 static int cmd_log_walk(struct rev_info *rev)
403 struct commit *commit;
407 if (rev->early_output)
408 setup_early_output();
410 if (prepare_revision_walk(rev))
411 die(_("revision walk setup failed"));
413 if (rev->early_output)
414 finish_early_output(rev);
417 * For --check and --exit-code, the exit code is based on CHECK_FAILED
418 * and HAS_CHANGES being accumulated in rev->diffopt, so be careful to
419 * retain that state information if replacing rev->diffopt in this loop
421 rev->diffopt.no_free = 1;
422 while ((commit = get_revision(rev)) != NULL) {
423 if (!log_tree_commit(rev, commit) && rev->max_count >= 0)
425 * We decremented max_count in get_revision,
426 * but we didn't actually show the commit.
429 if (!rev->reflog_info) {
431 * We may show a given commit multiple times when
432 * walking the reflogs.
434 free_commit_buffer(the_repository->parsed_objects,
436 free_commit_list(commit->parents);
437 commit->parents = NULL;
439 if (saved_nrl < rev->diffopt.needed_rename_limit)
440 saved_nrl = rev->diffopt.needed_rename_limit;
441 if (rev->diffopt.degraded_cc_to_c)
444 rev->diffopt.degraded_cc_to_c = saved_dcctc;
445 rev->diffopt.needed_rename_limit = saved_nrl;
446 rev->diffopt.no_free = 0;
447 diff_free(&rev->diffopt);
449 if (rev->diffopt.output_format & DIFF_FORMAT_CHECKDIFF &&
450 rev->diffopt.flags.check_failed) {
453 return diff_result_code(&rev->diffopt, 0);
456 static int git_log_config(const char *var, const char *value, void *cb)
458 const char *slot_name;
460 if (!strcmp(var, "format.pretty"))
461 return git_config_string(&fmt_pretty, var, value);
462 if (!strcmp(var, "format.subjectprefix"))
463 return git_config_string(&fmt_patch_subject_prefix, var, value);
464 if (!strcmp(var, "format.filenamemaxlength")) {
465 fmt_patch_name_max = git_config_int(var, value);
468 if (!strcmp(var, "format.encodeemailheaders")) {
469 default_encode_email_headers = git_config_bool(var, value);
472 if (!strcmp(var, "log.abbrevcommit")) {
473 default_abbrev_commit = git_config_bool(var, value);
476 if (!strcmp(var, "log.date"))
477 return git_config_string(&default_date_mode, var, value);
478 if (!strcmp(var, "log.decorate")) {
479 decoration_style = parse_decoration_style(value);
480 if (decoration_style < 0)
481 decoration_style = 0; /* maybe warn? */
484 if (!strcmp(var, "log.showroot")) {
485 default_show_root = git_config_bool(var, value);
488 if (!strcmp(var, "log.follow")) {
489 default_follow = git_config_bool(var, value);
492 if (skip_prefix(var, "color.decorate.", &slot_name))
493 return parse_decorate_color_config(var, slot_name, value);
494 if (!strcmp(var, "log.mailmap")) {
495 use_mailmap_config = git_config_bool(var, value);
498 if (!strcmp(var, "log.showsignature")) {
499 default_show_signature = git_config_bool(var, value);
503 if (grep_config(var, value, cb) < 0)
505 if (git_gpg_config(var, value, cb) < 0)
507 return git_diff_ui_config(var, value, cb);
510 int cmd_whatchanged(int argc, const char **argv, const char *prefix)
513 struct setup_revision_opt opt;
516 git_config(git_log_config, NULL);
518 repo_init_revisions(the_repository, &rev, prefix);
520 rev.simplify_history = 0;
521 memset(&opt, 0, sizeof(opt));
523 opt.revarg_opt = REVARG_COMMITTISH;
524 cmd_log_init(argc, argv, prefix, &rev, &opt);
525 if (!rev.diffopt.output_format)
526 rev.diffopt.output_format = DIFF_FORMAT_RAW;
527 return cmd_log_walk(&rev);
530 static void show_tagger(const char *buf, struct rev_info *rev)
532 struct strbuf out = STRBUF_INIT;
533 struct pretty_print_context pp = {0};
535 pp.fmt = rev->commit_format;
536 pp.date_mode = rev->date_mode;
537 pp_user_info(&pp, "Tagger", &out, buf, get_log_output_encoding());
538 fprintf(rev->diffopt.file, "%s", out.buf);
539 strbuf_release(&out);
542 static int show_blob_object(const struct object_id *oid, struct rev_info *rev, const char *obj_name)
544 struct object_id oidc;
545 struct object_context obj_context;
549 fflush(rev->diffopt.file);
550 if (!rev->diffopt.flags.textconv_set_via_cmdline ||
551 !rev->diffopt.flags.allow_textconv)
552 return stream_blob_to_fd(1, oid, NULL, 0);
554 if (get_oid_with_context(the_repository, obj_name,
556 &oidc, &obj_context))
557 die(_("not a valid object name %s"), obj_name);
558 if (!obj_context.path ||
559 !textconv_object(the_repository, obj_context.path,
560 obj_context.mode, &oidc, 1, &buf, &size)) {
561 free(obj_context.path);
562 return stream_blob_to_fd(1, oid, NULL, 0);
566 die(_("git show %s: bad file"), obj_name);
568 write_or_die(1, buf, size);
569 free(obj_context.path);
573 static int show_tag_object(const struct object_id *oid, struct rev_info *rev)
576 enum object_type type;
577 char *buf = read_object_file(oid, &type, &size);
581 return error(_("could not read object %s"), oid_to_hex(oid));
583 assert(type == OBJ_TAG);
584 while (offset < size && buf[offset] != '\n') {
585 int new_offset = offset + 1;
587 while (new_offset < size && buf[new_offset++] != '\n')
589 if (skip_prefix(buf + offset, "tagger ", &ident))
590 show_tagger(ident, rev);
595 fwrite(buf + offset, size - offset, 1, rev->diffopt.file);
600 static int show_tree_object(const struct object_id *oid,
602 const char *pathname, unsigned mode, void *context)
604 FILE *file = context;
605 fprintf(file, "%s%s\n", pathname, S_ISDIR(mode) ? "/" : "");
609 static void show_setup_revisions_tweak(struct rev_info *rev,
610 struct setup_revision_opt *opt)
612 if (rev->first_parent_only)
613 diff_merges_default_to_first_parent(rev);
615 diff_merges_default_to_dense_combined(rev);
616 if (!rev->diffopt.output_format)
617 rev->diffopt.output_format = DIFF_FORMAT_PATCH;
620 int cmd_show(int argc, const char **argv, const char *prefix)
623 struct object_array_entry *objects;
624 struct setup_revision_opt opt;
625 struct pathspec match_all;
626 int i, count, ret = 0;
629 git_config(git_log_config, NULL);
631 memset(&match_all, 0, sizeof(match_all));
632 repo_init_revisions(the_repository, &rev, prefix);
634 rev.always_show_header = 1;
635 rev.no_walk = REVISION_WALK_NO_WALK_SORTED;
636 rev.diffopt.stat_width = -1; /* Scale to real terminal size */
638 memset(&opt, 0, sizeof(opt));
640 opt.tweak = show_setup_revisions_tweak;
641 cmd_log_init(argc, argv, prefix, &rev, &opt);
644 return cmd_log_walk(&rev);
646 count = rev.pending.nr;
647 objects = rev.pending.objects;
648 for (i = 0; i < count && !ret; i++) {
649 struct object *o = objects[i].item;
650 const char *name = objects[i].name;
653 ret = show_blob_object(&o->oid, &rev, name);
656 struct tag *t = (struct tag *)o;
657 struct object_id *oid = get_tagged_oid(t);
661 fprintf(rev.diffopt.file, "%stag %s%s\n",
662 diff_get_color_opt(&rev.diffopt, DIFF_COMMIT),
664 diff_get_color_opt(&rev.diffopt, DIFF_RESET));
665 ret = show_tag_object(&o->oid, &rev);
669 o = parse_object(the_repository, oid);
671 ret = error(_("could not read object %s"),
680 fprintf(rev.diffopt.file, "%stree %s%s\n\n",
681 diff_get_color_opt(&rev.diffopt, DIFF_COMMIT),
683 diff_get_color_opt(&rev.diffopt, DIFF_RESET));
684 read_tree(the_repository, (struct tree *)o,
685 &match_all, show_tree_object,
690 rev.pending.nr = rev.pending.alloc = 0;
691 rev.pending.objects = NULL;
692 add_object_array(o, name, &rev.pending);
693 ret = cmd_log_walk(&rev);
696 ret = error(_("unknown type: %d"), o->type);
704 * This is equivalent to "git log -g --abbrev-commit --pretty=oneline"
706 int cmd_log_reflog(int argc, const char **argv, const char *prefix)
709 struct setup_revision_opt opt;
712 git_config(git_log_config, NULL);
714 repo_init_revisions(the_repository, &rev, prefix);
715 init_reflog_walk(&rev.reflog_info);
716 rev.verbose_header = 1;
717 memset(&opt, 0, sizeof(opt));
719 cmd_log_init_defaults(&rev);
720 rev.abbrev_commit = 1;
721 rev.commit_format = CMIT_FMT_ONELINE;
722 rev.use_terminator = 1;
723 rev.always_show_header = 1;
724 cmd_log_init_finish(argc, argv, prefix, &rev, &opt);
726 return cmd_log_walk(&rev);
729 static void log_setup_revisions_tweak(struct rev_info *rev,
730 struct setup_revision_opt *opt)
732 if (rev->diffopt.flags.default_follow_renames &&
733 rev->prune_data.nr == 1)
734 rev->diffopt.flags.follow_renames = 1;
736 if (rev->first_parent_only)
737 diff_merges_default_to_first_parent(rev);
740 int cmd_log(int argc, const char **argv, const char *prefix)
743 struct setup_revision_opt opt;
746 git_config(git_log_config, NULL);
748 repo_init_revisions(the_repository, &rev, prefix);
749 rev.always_show_header = 1;
750 memset(&opt, 0, sizeof(opt));
752 opt.revarg_opt = REVARG_COMMITTISH;
753 opt.tweak = log_setup_revisions_tweak;
754 cmd_log_init(argc, argv, prefix, &rev, &opt);
755 return cmd_log_walk(&rev);
760 static const char *fmt_patch_suffix = ".patch";
761 static int numbered = 0;
762 static int auto_number = 1;
764 static char *default_attach = NULL;
766 static struct string_list extra_hdr = STRING_LIST_INIT_NODUP;
767 static struct string_list extra_to = STRING_LIST_INIT_NODUP;
768 static struct string_list extra_cc = STRING_LIST_INIT_NODUP;
770 static void add_header(const char *value)
772 struct string_list_item *item;
773 int len = strlen(value);
774 while (len && value[len - 1] == '\n')
777 if (!strncasecmp(value, "to: ", 4)) {
778 item = string_list_append(&extra_to, value + 4);
780 } else if (!strncasecmp(value, "cc: ", 4)) {
781 item = string_list_append(&extra_cc, value + 4);
784 item = string_list_append(&extra_hdr, value);
787 item->string[len] = '\0';
803 enum cover_from_description {
810 enum auto_base_setting {
816 static enum thread_level thread;
817 static int do_signoff;
818 static enum auto_base_setting auto_base;
820 static const char *signature = git_version_string;
821 static const char *signature_file;
822 static enum cover_setting config_cover_letter;
823 static const char *config_output_directory;
824 static enum cover_from_description cover_from_description_mode = COVER_FROM_MESSAGE;
825 static int show_notes;
826 static struct display_notes_opt notes_opt;
828 static enum cover_from_description parse_cover_from_description(const char *arg)
830 if (!arg || !strcmp(arg, "default"))
831 return COVER_FROM_MESSAGE;
832 else if (!strcmp(arg, "none"))
833 return COVER_FROM_NONE;
834 else if (!strcmp(arg, "message"))
835 return COVER_FROM_MESSAGE;
836 else if (!strcmp(arg, "subject"))
837 return COVER_FROM_SUBJECT;
838 else if (!strcmp(arg, "auto"))
839 return COVER_FROM_AUTO;
841 die(_("%s: invalid cover from description mode"), arg);
844 static int git_format_config(const char *var, const char *value, void *cb)
846 if (!strcmp(var, "format.headers")) {
848 die(_("format.headers without value"));
852 if (!strcmp(var, "format.suffix"))
853 return git_config_string(&fmt_patch_suffix, var, value);
854 if (!strcmp(var, "format.to")) {
856 return config_error_nonbool(var);
857 string_list_append(&extra_to, value);
860 if (!strcmp(var, "format.cc")) {
862 return config_error_nonbool(var);
863 string_list_append(&extra_cc, value);
866 if (!strcmp(var, "diff.color") || !strcmp(var, "color.diff") ||
867 !strcmp(var, "color.ui") || !strcmp(var, "diff.submodule")) {
870 if (!strcmp(var, "format.numbered")) {
871 if (value && !strcasecmp(value, "auto")) {
875 numbered = git_config_bool(var, value);
876 auto_number = auto_number && numbered;
879 if (!strcmp(var, "format.attach")) {
881 default_attach = xstrdup(value);
883 default_attach = xstrdup(git_version_string);
886 if (!strcmp(var, "format.thread")) {
887 if (value && !strcasecmp(value, "deep")) {
888 thread = THREAD_DEEP;
891 if (value && !strcasecmp(value, "shallow")) {
892 thread = THREAD_SHALLOW;
895 thread = git_config_bool(var, value) ? THREAD_SHALLOW : THREAD_UNSET;
898 if (!strcmp(var, "format.signoff")) {
899 do_signoff = git_config_bool(var, value);
902 if (!strcmp(var, "format.signature"))
903 return git_config_string(&signature, var, value);
904 if (!strcmp(var, "format.signaturefile"))
905 return git_config_pathname(&signature_file, var, value);
906 if (!strcmp(var, "format.coverletter")) {
907 if (value && !strcasecmp(value, "auto")) {
908 config_cover_letter = COVER_AUTO;
911 config_cover_letter = git_config_bool(var, value) ? COVER_ON : COVER_OFF;
914 if (!strcmp(var, "format.outputdirectory"))
915 return git_config_string(&config_output_directory, var, value);
916 if (!strcmp(var, "format.useautobase")) {
917 if (value && !strcasecmp(value, "whenAble")) {
918 auto_base = AUTO_BASE_WHEN_ABLE;
921 auto_base = git_config_bool(var, value) ? AUTO_BASE_ALWAYS : AUTO_BASE_NEVER;
924 if (!strcmp(var, "format.from")) {
925 int b = git_parse_maybe_bool(value);
928 from = xstrdup(value);
930 from = xstrdup(git_committer_info(IDENT_NO_DATE));
935 if (!strcmp(var, "format.notes")) {
936 int b = git_parse_maybe_bool(value);
938 enable_ref_display_notes(¬es_opt, &show_notes, value);
940 enable_default_display_notes(¬es_opt, &show_notes);
942 disable_display_notes(¬es_opt, &show_notes);
945 if (!strcmp(var, "format.coverfromdescription")) {
946 cover_from_description_mode = parse_cover_from_description(value);
950 return git_log_config(var, value, cb);
953 static const char *output_directory = NULL;
954 static int outdir_offset;
956 static int open_next_file(struct commit *commit, const char *subject,
957 struct rev_info *rev, int quiet)
959 struct strbuf filename = STRBUF_INIT;
961 if (output_directory) {
962 strbuf_addstr(&filename, output_directory);
963 strbuf_complete(&filename, '/');
966 if (rev->numbered_files)
967 strbuf_addf(&filename, "%d", rev->nr);
969 fmt_output_commit(&filename, commit, rev);
971 fmt_output_subject(&filename, subject, rev);
974 printf("%s\n", filename.buf + outdir_offset);
976 if ((rev->diffopt.file = fopen(filename.buf, "w")) == NULL) {
977 error_errno(_("cannot open patch file %s"), filename.buf);
978 strbuf_release(&filename);
982 strbuf_release(&filename);
986 static void get_patch_ids(struct rev_info *rev, struct patch_ids *ids)
988 struct rev_info check_rev;
989 struct commit *commit, *c1, *c2;
990 struct object *o1, *o2;
991 unsigned flags1, flags2;
993 if (rev->pending.nr != 2)
994 die(_("need exactly one range"));
996 o1 = rev->pending.objects[0].item;
997 o2 = rev->pending.objects[1].item;
1000 c1 = lookup_commit_reference(the_repository, &o1->oid);
1001 c2 = lookup_commit_reference(the_repository, &o2->oid);
1003 if ((flags1 & UNINTERESTING) == (flags2 & UNINTERESTING))
1004 die(_("not a range"));
1006 init_patch_ids(the_repository, ids);
1008 /* given a range a..b get all patch ids for b..a */
1009 repo_init_revisions(the_repository, &check_rev, rev->prefix);
1010 check_rev.max_parents = 1;
1011 o1->flags ^= UNINTERESTING;
1012 o2->flags ^= UNINTERESTING;
1013 add_pending_object(&check_rev, o1, "o1");
1014 add_pending_object(&check_rev, o2, "o2");
1015 if (prepare_revision_walk(&check_rev))
1016 die(_("revision walk setup failed"));
1018 while ((commit = get_revision(&check_rev)) != NULL) {
1019 add_commit_patch_id(commit, ids);
1022 /* reset for next revision walk */
1023 clear_commit_marks(c1, SEEN | UNINTERESTING | SHOWN | ADDED);
1024 clear_commit_marks(c2, SEEN | UNINTERESTING | SHOWN | ADDED);
1029 static void gen_message_id(struct rev_info *info, char *base)
1031 struct strbuf buf = STRBUF_INIT;
1032 strbuf_addf(&buf, "%s.%"PRItime".git.%s", base,
1033 (timestamp_t) time(NULL),
1034 git_committer_info(IDENT_NO_NAME|IDENT_NO_DATE|IDENT_STRICT));
1035 info->message_id = strbuf_detach(&buf, NULL);
1038 static void print_signature(FILE *file)
1040 if (!signature || !*signature)
1043 fprintf(file, "-- \n%s", signature);
1044 if (signature[strlen(signature)-1] != '\n')
1049 static char *find_branch_name(struct rev_info *rev)
1051 int i, positive = -1;
1052 struct object_id branch_oid;
1053 const struct object_id *tip_oid;
1054 const char *ref, *v;
1055 char *full_ref, *branch = NULL;
1057 for (i = 0; i < rev->cmdline.nr; i++) {
1058 if (rev->cmdline.rev[i].flags & UNINTERESTING)
1067 ref = rev->cmdline.rev[positive].name;
1068 tip_oid = &rev->cmdline.rev[positive].item->oid;
1069 if (dwim_ref(ref, strlen(ref), &branch_oid, &full_ref, 0) &&
1070 skip_prefix(full_ref, "refs/heads/", &v) &&
1071 oideq(tip_oid, &branch_oid))
1072 branch = xstrdup(v);
1077 static void show_diffstat(struct rev_info *rev,
1078 struct commit *origin, struct commit *head)
1080 struct diff_options opts;
1082 memcpy(&opts, &rev->diffopt, sizeof(opts));
1083 opts.output_format = DIFF_FORMAT_SUMMARY | DIFF_FORMAT_DIFFSTAT;
1084 diff_setup_done(&opts);
1086 diff_tree_oid(get_commit_tree_oid(origin),
1087 get_commit_tree_oid(head),
1089 diffcore_std(&opts);
1092 fprintf(rev->diffopt.file, "\n");
1095 static void prepare_cover_text(struct pretty_print_context *pp,
1096 const char *branch_name,
1098 const char *encoding,
1101 const char *subject = "*** SUBJECT HERE ***";
1102 const char *body = "*** BLURB HERE ***";
1103 struct strbuf description_sb = STRBUF_INIT;
1104 struct strbuf subject_sb = STRBUF_INIT;
1106 if (cover_from_description_mode == COVER_FROM_NONE)
1109 if (branch_name && *branch_name)
1110 read_branch_desc(&description_sb, branch_name);
1111 if (!description_sb.len)
1114 if (cover_from_description_mode == COVER_FROM_SUBJECT ||
1115 cover_from_description_mode == COVER_FROM_AUTO)
1116 body = format_subject(&subject_sb, description_sb.buf, " ");
1118 if (cover_from_description_mode == COVER_FROM_MESSAGE ||
1119 (cover_from_description_mode == COVER_FROM_AUTO &&
1120 subject_sb.len > COVER_FROM_AUTO_MAX_SUBJECT_LEN))
1121 body = description_sb.buf;
1123 subject = subject_sb.buf;
1126 pp_title_line(pp, &subject, sb, encoding, need_8bit_cte);
1127 pp_remainder(pp, &body, sb, 0);
1129 strbuf_release(&description_sb);
1130 strbuf_release(&subject_sb);
1133 static int get_notes_refs(struct string_list_item *item, void *arg)
1135 strvec_pushf(arg, "--notes=%s", item->string);
1139 static void get_notes_args(struct strvec *arg, struct rev_info *rev)
1141 if (!rev->show_notes) {
1142 strvec_push(arg, "--no-notes");
1143 } else if (rev->notes_opt.use_default_notes > 0 ||
1144 (rev->notes_opt.use_default_notes == -1 &&
1145 !rev->notes_opt.extra_notes_refs.nr)) {
1146 strvec_push(arg, "--notes");
1148 for_each_string_list(&rev->notes_opt.extra_notes_refs, get_notes_refs, arg);
1152 static void make_cover_letter(struct rev_info *rev, int use_separate_file,
1153 struct commit *origin,
1154 int nr, struct commit **list,
1155 const char *branch_name,
1158 const char *committer;
1159 struct shortlog log;
1160 struct strbuf sb = STRBUF_INIT;
1162 const char *encoding = "UTF-8";
1163 int need_8bit_cte = 0;
1164 struct pretty_print_context pp = {0};
1165 struct commit *head = list[0];
1167 if (!cmit_fmt_is_mail(rev->commit_format))
1168 die(_("cover letter needs email format"));
1170 committer = git_committer_info(0);
1172 if (use_separate_file &&
1173 open_next_file(NULL, rev->numbered_files ? NULL : "cover-letter", rev, quiet))
1174 die(_("failed to create cover-letter file"));
1176 log_write_email_headers(rev, head, &pp.after_subject, &need_8bit_cte, 0);
1178 for (i = 0; !need_8bit_cte && i < nr; i++) {
1179 const char *buf = get_commit_buffer(list[i], NULL);
1180 if (has_non_ascii(buf))
1182 unuse_commit_buffer(list[i], buf);
1186 branch_name = find_branch_name(rev);
1188 pp.fmt = CMIT_FMT_EMAIL;
1189 pp.date_mode.type = DATE_RFC2822;
1191 pp.print_email_subject = 1;
1192 pp_user_info(&pp, NULL, &sb, committer, encoding);
1193 prepare_cover_text(&pp, branch_name, &sb, encoding, need_8bit_cte);
1194 fprintf(rev->diffopt.file, "%s\n", sb.buf);
1196 strbuf_release(&sb);
1198 shortlog_init(&log);
1200 log.wrap = MAIL_DEFAULT_WRAP;
1203 log.file = rev->diffopt.file;
1204 log.groups = SHORTLOG_GROUP_AUTHOR;
1205 for (i = 0; i < nr; i++)
1206 shortlog_add_commit(&log, list[i]);
1208 shortlog_output(&log);
1210 /* We can only do diffstat with a unique reference point */
1212 show_diffstat(rev, origin, head);
1214 if (rev->idiff_oid1) {
1215 fprintf_ln(rev->diffopt.file, "%s", rev->idiff_title);
1216 show_interdiff(rev->idiff_oid1, rev->idiff_oid2, 0,
1222 * Pass minimum required diff-options to range-diff; others
1223 * can be added later if deemed desirable.
1225 struct diff_options opts;
1226 struct strvec other_arg = STRVEC_INIT;
1227 struct range_diff_options range_diff_opts = {
1228 .creation_factor = rev->creation_factor,
1231 .other_arg = &other_arg
1235 opts.file = rev->diffopt.file;
1236 opts.use_color = rev->diffopt.use_color;
1237 diff_setup_done(&opts);
1238 fprintf_ln(rev->diffopt.file, "%s", rev->rdiff_title);
1239 get_notes_args(&other_arg, rev);
1240 show_range_diff(rev->rdiff1, rev->rdiff2, &range_diff_opts);
1241 strvec_clear(&other_arg);
1245 static const char *clean_message_id(const char *msg_id)
1248 const char *a, *z, *m;
1251 while ((ch = *m) && (isspace(ch) || (ch == '<')))
1256 if (!isspace(ch) && (ch != '>'))
1261 die(_("insane in-reply-to: %s"), msg_id);
1264 return xmemdupz(a, z - a);
1267 static const char *set_outdir(const char *prefix, const char *output_directory)
1269 if (output_directory && is_absolute_path(output_directory))
1270 return output_directory;
1272 if (!prefix || !*prefix) {
1273 if (output_directory)
1274 return output_directory;
1275 /* The user did not explicitly ask for "./" */
1280 outdir_offset = strlen(prefix);
1281 if (!output_directory)
1284 return prefix_filename(prefix, output_directory);
1287 static const char * const builtin_format_patch_usage[] = {
1288 N_("git format-patch [<options>] [<since> | <revision-range>]"),
1292 static int keep_subject = 0;
1294 static int keep_callback(const struct option *opt, const char *arg, int unset)
1296 BUG_ON_OPT_NEG(unset);
1297 BUG_ON_OPT_ARG(arg);
1298 ((struct rev_info *)opt->value)->total = -1;
1303 static int subject_prefix = 0;
1305 static int subject_prefix_callback(const struct option *opt, const char *arg,
1308 BUG_ON_OPT_NEG(unset);
1310 ((struct rev_info *)opt->value)->subject_prefix = arg;
1314 static int rfc_callback(const struct option *opt, const char *arg, int unset)
1316 BUG_ON_OPT_NEG(unset);
1317 BUG_ON_OPT_ARG(arg);
1318 return subject_prefix_callback(opt, "RFC PATCH", unset);
1321 static int numbered_cmdline_opt = 0;
1323 static int numbered_callback(const struct option *opt, const char *arg,
1326 BUG_ON_OPT_ARG(arg);
1327 *(int *)opt->value = numbered_cmdline_opt = unset ? 0 : 1;
1333 static int no_numbered_callback(const struct option *opt, const char *arg,
1336 BUG_ON_OPT_NEG(unset);
1337 return numbered_callback(opt, arg, 1);
1340 static int output_directory_callback(const struct option *opt, const char *arg,
1343 const char **dir = (const char **)opt->value;
1344 BUG_ON_OPT_NEG(unset);
1346 die(_("two output directories?"));
1351 static int thread_callback(const struct option *opt, const char *arg, int unset)
1353 enum thread_level *thread = (enum thread_level *)opt->value;
1355 *thread = THREAD_UNSET;
1356 else if (!arg || !strcmp(arg, "shallow"))
1357 *thread = THREAD_SHALLOW;
1358 else if (!strcmp(arg, "deep"))
1359 *thread = THREAD_DEEP;
1361 * Please update _git_formatpatch() in git-completion.bash
1362 * when you add new options.
1369 static int attach_callback(const struct option *opt, const char *arg, int unset)
1371 struct rev_info *rev = (struct rev_info *)opt->value;
1373 rev->mime_boundary = NULL;
1375 rev->mime_boundary = arg;
1377 rev->mime_boundary = git_version_string;
1378 rev->no_inline = unset ? 0 : 1;
1382 static int inline_callback(const struct option *opt, const char *arg, int unset)
1384 struct rev_info *rev = (struct rev_info *)opt->value;
1386 rev->mime_boundary = NULL;
1388 rev->mime_boundary = arg;
1390 rev->mime_boundary = git_version_string;
1395 static int header_callback(const struct option *opt, const char *arg, int unset)
1398 string_list_clear(&extra_hdr, 0);
1399 string_list_clear(&extra_to, 0);
1400 string_list_clear(&extra_cc, 0);
1407 static int to_callback(const struct option *opt, const char *arg, int unset)
1410 string_list_clear(&extra_to, 0);
1412 string_list_append(&extra_to, arg);
1416 static int cc_callback(const struct option *opt, const char *arg, int unset)
1419 string_list_clear(&extra_cc, 0);
1421 string_list_append(&extra_cc, arg);
1425 static int from_callback(const struct option *opt, const char *arg, int unset)
1427 char **from = opt->value;
1434 *from = xstrdup(arg);
1436 *from = xstrdup(git_committer_info(IDENT_NO_DATE));
1440 static int base_callback(const struct option *opt, const char *arg, int unset)
1442 const char **base_commit = opt->value;
1445 auto_base = AUTO_BASE_NEVER;
1446 *base_commit = NULL;
1447 } else if (!strcmp(arg, "auto")) {
1448 auto_base = AUTO_BASE_ALWAYS;
1449 *base_commit = NULL;
1451 auto_base = AUTO_BASE_NEVER;
1457 struct base_tree_info {
1458 struct object_id base_commit;
1459 int nr_patch_id, alloc_patch_id;
1460 struct object_id *patch_id;
1463 static struct commit *get_base_commit(const char *base_commit,
1464 struct commit **list,
1467 struct commit *base = NULL;
1468 struct commit **rev;
1469 int i = 0, rev_nr = 0, auto_select, die_on_failure;
1471 switch (auto_base) {
1472 case AUTO_BASE_NEVER:
1477 /* no base information is requested */
1481 case AUTO_BASE_ALWAYS:
1482 case AUTO_BASE_WHEN_ABLE:
1484 BUG("requested automatic base selection but a commit was provided");
1487 die_on_failure = auto_base == AUTO_BASE_ALWAYS;
1491 BUG("unexpected automatic base selection method");
1495 base = lookup_commit_reference_by_name(base_commit);
1497 die(_("unknown commit %s"), base_commit);
1499 struct branch *curr_branch = branch_get(NULL);
1500 const char *upstream = branch_get_upstream(curr_branch, NULL);
1502 struct commit_list *base_list;
1503 struct commit *commit;
1504 struct object_id oid;
1506 if (get_oid(upstream, &oid)) {
1508 die(_("failed to resolve '%s' as a valid ref"), upstream);
1512 commit = lookup_commit_or_die(&oid, "upstream base");
1513 base_list = get_merge_bases_many(commit, total, list);
1514 /* There should be one and only one merge base. */
1515 if (!base_list || base_list->next) {
1516 if (die_on_failure) {
1517 die(_("could not find exact merge base"));
1519 free_commit_list(base_list);
1523 base = base_list->item;
1524 free_commit_list(base_list);
1527 die(_("failed to get upstream, if you want to record base commit automatically,\n"
1528 "please use git branch --set-upstream-to to track a remote branch.\n"
1529 "Or you could specify base commit by --base=<base-commit-id> manually"));
1535 ALLOC_ARRAY(rev, total);
1536 for (i = 0; i < total; i++)
1541 * Get merge base through pair-wise computations
1542 * and store it in rev[0].
1544 while (rev_nr > 1) {
1545 for (i = 0; i < rev_nr / 2; i++) {
1546 struct commit_list *merge_base;
1547 merge_base = get_merge_bases(rev[2 * i], rev[2 * i + 1]);
1548 if (!merge_base || merge_base->next) {
1549 if (die_on_failure) {
1550 die(_("failed to find exact merge base"));
1557 rev[i] = merge_base->item;
1561 rev[i] = rev[2 * i];
1562 rev_nr = DIV_ROUND_UP(rev_nr, 2);
1565 if (!in_merge_bases(base, rev[0])) {
1566 if (die_on_failure) {
1567 die(_("base commit should be the ancestor of revision list"));
1574 for (i = 0; i < total; i++) {
1575 if (base == list[i]) {
1576 if (die_on_failure) {
1577 die(_("base commit shouldn't be in revision list"));
1589 define_commit_slab(commit_base, int);
1591 static void prepare_bases(struct base_tree_info *bases,
1592 struct commit *base,
1593 struct commit **list,
1596 struct commit *commit;
1597 struct rev_info revs;
1598 struct diff_options diffopt;
1599 struct commit_base commit_base;
1605 init_commit_base(&commit_base);
1606 repo_diff_setup(the_repository, &diffopt);
1607 diffopt.flags.recursive = 1;
1608 diff_setup_done(&diffopt);
1610 oidcpy(&bases->base_commit, &base->object.oid);
1612 repo_init_revisions(the_repository, &revs, NULL);
1613 revs.max_parents = 1;
1614 revs.topo_order = 1;
1615 for (i = 0; i < total; i++) {
1616 list[i]->object.flags &= ~UNINTERESTING;
1617 add_pending_object(&revs, &list[i]->object, "rev_list");
1618 *commit_base_at(&commit_base, list[i]) = 1;
1620 base->object.flags |= UNINTERESTING;
1621 add_pending_object(&revs, &base->object, "base");
1623 if (prepare_revision_walk(&revs))
1624 die(_("revision walk setup failed"));
1626 * Traverse the commits list, get prerequisite patch ids
1627 * and stuff them in bases structure.
1629 while ((commit = get_revision(&revs)) != NULL) {
1630 struct object_id oid;
1631 struct object_id *patch_id;
1632 if (*commit_base_at(&commit_base, commit))
1634 if (commit_patch_id(commit, &diffopt, &oid, 0, 1))
1635 die(_("cannot get patch id"));
1636 ALLOC_GROW(bases->patch_id, bases->nr_patch_id + 1, bases->alloc_patch_id);
1637 patch_id = bases->patch_id + bases->nr_patch_id;
1638 oidcpy(patch_id, &oid);
1639 bases->nr_patch_id++;
1641 clear_commit_base(&commit_base);
1644 static void print_bases(struct base_tree_info *bases, FILE *file)
1648 /* Only do this once, either for the cover or for the first one */
1649 if (is_null_oid(&bases->base_commit))
1652 /* Show the base commit */
1653 fprintf(file, "\nbase-commit: %s\n", oid_to_hex(&bases->base_commit));
1655 /* Show the prerequisite patches */
1656 for (i = bases->nr_patch_id - 1; i >= 0; i--)
1657 fprintf(file, "prerequisite-patch-id: %s\n", oid_to_hex(&bases->patch_id[i]));
1659 free(bases->patch_id);
1660 bases->nr_patch_id = 0;
1661 bases->alloc_patch_id = 0;
1662 oidclr(&bases->base_commit);
1665 static const char *diff_title(struct strbuf *sb,
1666 const char *reroll_count,
1667 const char *generic,
1668 const char *rerolled)
1672 /* RFC may be v0, so allow -v1 to diff against v0 */
1673 if (reroll_count && !strtol_i(reroll_count, 10, &v) &&
1675 strbuf_addf(sb, rerolled, v - 1);
1677 strbuf_addstr(sb, generic);
1681 static void infer_range_diff_ranges(struct strbuf *r1,
1684 struct commit *origin,
1685 struct commit *head)
1687 const char *head_oid = oid_to_hex(&head->object.oid);
1688 int prev_is_range = is_range_diff_range(prev);
1691 strbuf_addstr(r1, prev);
1693 strbuf_addf(r1, "%s..%s", head_oid, prev);
1696 strbuf_addf(r2, "%s..%s", oid_to_hex(&origin->object.oid), head_oid);
1697 else if (prev_is_range)
1698 die(_("failed to infer range-diff origin of current series"));
1700 warning(_("using '%s' as range-diff origin of current series"), prev);
1701 strbuf_addf(r2, "%s..%s", prev, head_oid);
1705 int cmd_format_patch(int argc, const char **argv, const char *prefix)
1707 struct commit *commit;
1708 struct commit **list = NULL;
1709 struct rev_info rev;
1710 struct setup_revision_opt s_r_opt;
1711 int nr = 0, total, i;
1713 int start_number = -1;
1714 int just_numbers = 0;
1715 int ignore_if_in_upstream = 0;
1716 int cover_letter = -1;
1717 int boundary_count = 0;
1718 int no_binary_diff = 0;
1719 int zero_commit = 0;
1720 struct commit *origin = NULL;
1721 const char *in_reply_to = NULL;
1722 struct patch_ids ids;
1723 struct strbuf buf = STRBUF_INIT;
1724 int use_patch_format = 0;
1726 const char *reroll_count = NULL;
1727 char *cover_from_description_arg = NULL;
1728 char *branch_name = NULL;
1729 char *base_commit = NULL;
1730 struct base_tree_info bases;
1731 struct commit *base;
1732 int show_progress = 0;
1733 struct progress *progress = NULL;
1734 struct oid_array idiff_prev = OID_ARRAY_INIT;
1735 struct strbuf idiff_title = STRBUF_INIT;
1736 const char *rdiff_prev = NULL;
1737 struct strbuf rdiff1 = STRBUF_INIT;
1738 struct strbuf rdiff2 = STRBUF_INIT;
1739 struct strbuf rdiff_title = STRBUF_INIT;
1740 int creation_factor = -1;
1742 const struct option builtin_format_patch_options[] = {
1743 OPT_CALLBACK_F('n', "numbered", &numbered, NULL,
1744 N_("use [PATCH n/m] even with a single patch"),
1745 PARSE_OPT_NOARG, numbered_callback),
1746 OPT_CALLBACK_F('N', "no-numbered", &numbered, NULL,
1747 N_("use [PATCH] even with multiple patches"),
1748 PARSE_OPT_NOARG | PARSE_OPT_NONEG, no_numbered_callback),
1749 OPT_BOOL('s', "signoff", &do_signoff, N_("add a Signed-off-by trailer")),
1750 OPT_BOOL(0, "stdout", &use_stdout,
1751 N_("print patches to standard out")),
1752 OPT_BOOL(0, "cover-letter", &cover_letter,
1753 N_("generate a cover letter")),
1754 OPT_BOOL(0, "numbered-files", &just_numbers,
1755 N_("use simple number sequence for output file names")),
1756 OPT_STRING(0, "suffix", &fmt_patch_suffix, N_("sfx"),
1757 N_("use <sfx> instead of '.patch'")),
1758 OPT_INTEGER(0, "start-number", &start_number,
1759 N_("start numbering patches at <n> instead of 1")),
1760 OPT_STRING('v', "reroll-count", &reroll_count, N_("reroll-count"),
1761 N_("mark the series as Nth re-roll")),
1762 OPT_INTEGER(0, "filename-max-length", &fmt_patch_name_max,
1763 N_("max length of output filename")),
1764 OPT_CALLBACK_F(0, "rfc", &rev, NULL,
1765 N_("use [RFC PATCH] instead of [PATCH]"),
1766 PARSE_OPT_NOARG | PARSE_OPT_NONEG, rfc_callback),
1767 OPT_STRING(0, "cover-from-description", &cover_from_description_arg,
1768 N_("cover-from-description-mode"),
1769 N_("generate parts of a cover letter based on a branch's description")),
1770 OPT_CALLBACK_F(0, "subject-prefix", &rev, N_("prefix"),
1771 N_("use [<prefix>] instead of [PATCH]"),
1772 PARSE_OPT_NONEG, subject_prefix_callback),
1773 OPT_CALLBACK_F('o', "output-directory", &output_directory,
1774 N_("dir"), N_("store resulting files in <dir>"),
1775 PARSE_OPT_NONEG, output_directory_callback),
1776 OPT_CALLBACK_F('k', "keep-subject", &rev, NULL,
1777 N_("don't strip/add [PATCH]"),
1778 PARSE_OPT_NOARG | PARSE_OPT_NONEG, keep_callback),
1779 OPT_BOOL(0, "no-binary", &no_binary_diff,
1780 N_("don't output binary diffs")),
1781 OPT_BOOL(0, "zero-commit", &zero_commit,
1782 N_("output all-zero hash in From header")),
1783 OPT_BOOL(0, "ignore-if-in-upstream", &ignore_if_in_upstream,
1784 N_("don't include a patch matching a commit upstream")),
1785 OPT_SET_INT_F('p', "no-stat", &use_patch_format,
1786 N_("show patch format instead of default (patch + stat)"),
1787 1, PARSE_OPT_NONEG),
1788 OPT_GROUP(N_("Messaging")),
1789 OPT_CALLBACK(0, "add-header", NULL, N_("header"),
1790 N_("add email header"), header_callback),
1791 OPT_CALLBACK(0, "to", NULL, N_("email"), N_("add To: header"), to_callback),
1792 OPT_CALLBACK(0, "cc", NULL, N_("email"), N_("add Cc: header"), cc_callback),
1793 OPT_CALLBACK_F(0, "from", &from, N_("ident"),
1794 N_("set From address to <ident> (or committer ident if absent)"),
1795 PARSE_OPT_OPTARG, from_callback),
1796 OPT_STRING(0, "in-reply-to", &in_reply_to, N_("message-id"),
1797 N_("make first mail a reply to <message-id>")),
1798 OPT_CALLBACK_F(0, "attach", &rev, N_("boundary"),
1799 N_("attach the patch"), PARSE_OPT_OPTARG,
1801 OPT_CALLBACK_F(0, "inline", &rev, N_("boundary"),
1802 N_("inline the patch"),
1803 PARSE_OPT_OPTARG | PARSE_OPT_NONEG,
1805 OPT_CALLBACK_F(0, "thread", &thread, N_("style"),
1806 N_("enable message threading, styles: shallow, deep"),
1807 PARSE_OPT_OPTARG, thread_callback),
1808 OPT_STRING(0, "signature", &signature, N_("signature"),
1809 N_("add a signature")),
1810 OPT_CALLBACK_F(0, "base", &base_commit, N_("base-commit"),
1811 N_("add prerequisite tree info to the patch series"),
1813 OPT_FILENAME(0, "signature-file", &signature_file,
1814 N_("add a signature from a file")),
1815 OPT__QUIET(&quiet, N_("don't print the patch filenames")),
1816 OPT_BOOL(0, "progress", &show_progress,
1817 N_("show progress while generating patches")),
1818 OPT_CALLBACK(0, "interdiff", &idiff_prev, N_("rev"),
1819 N_("show changes against <rev> in cover letter or single patch"),
1820 parse_opt_object_name),
1821 OPT_STRING(0, "range-diff", &rdiff_prev, N_("refspec"),
1822 N_("show changes against <refspec> in cover letter or single patch")),
1823 OPT_INTEGER(0, "creation-factor", &creation_factor,
1824 N_("percentage by which creation is weighted")),
1828 extra_hdr.strdup_strings = 1;
1829 extra_to.strdup_strings = 1;
1830 extra_cc.strdup_strings = 1;
1831 init_log_defaults();
1832 init_display_notes(¬es_opt);
1833 git_config(git_format_config, NULL);
1834 repo_init_revisions(the_repository, &rev, prefix);
1835 rev.show_notes = show_notes;
1836 memcpy(&rev.notes_opt, ¬es_opt, sizeof(notes_opt));
1837 rev.commit_format = CMIT_FMT_EMAIL;
1838 rev.encode_email_headers = default_encode_email_headers;
1839 rev.expand_tabs_in_log_default = 0;
1840 rev.verbose_header = 1;
1842 rev.max_parents = 1;
1843 rev.diffopt.flags.recursive = 1;
1844 rev.subject_prefix = fmt_patch_subject_prefix;
1845 memset(&s_r_opt, 0, sizeof(s_r_opt));
1846 s_r_opt.def = "HEAD";
1847 s_r_opt.revarg_opt = REVARG_COMMITTISH;
1849 if (default_attach) {
1850 rev.mime_boundary = default_attach;
1855 * Parse the arguments before setup_revisions(), or something
1856 * like "git format-patch -o a123 HEAD^.." may fail; a123 is
1857 * possibly a valid SHA1.
1859 argc = parse_options(argc, argv, prefix, builtin_format_patch_options,
1860 builtin_format_patch_usage,
1861 PARSE_OPT_KEEP_ARGV0 | PARSE_OPT_KEEP_UNKNOWN |
1862 PARSE_OPT_KEEP_DASHDASH);
1864 /* Make sure "0000-$sub.patch" gives non-negative length for $sub */
1865 if (fmt_patch_name_max <= strlen("0000-") + strlen(fmt_patch_suffix))
1866 fmt_patch_name_max = strlen("0000-") + strlen(fmt_patch_suffix);
1868 if (cover_from_description_arg)
1869 cover_from_description_mode = parse_cover_from_description(cover_from_description_arg);
1872 struct strbuf sprefix = STRBUF_INIT;
1874 strbuf_addf(&sprefix, "%s v%s",
1875 rev.subject_prefix, reroll_count);
1876 rev.reroll_count = reroll_count;
1877 rev.subject_prefix = strbuf_detach(&sprefix, NULL);
1880 for (i = 0; i < extra_hdr.nr; i++) {
1881 strbuf_addstr(&buf, extra_hdr.items[i].string);
1882 strbuf_addch(&buf, '\n');
1886 strbuf_addstr(&buf, "To: ");
1887 for (i = 0; i < extra_to.nr; i++) {
1889 strbuf_addstr(&buf, " ");
1890 strbuf_addstr(&buf, extra_to.items[i].string);
1891 if (i + 1 < extra_to.nr)
1892 strbuf_addch(&buf, ',');
1893 strbuf_addch(&buf, '\n');
1897 strbuf_addstr(&buf, "Cc: ");
1898 for (i = 0; i < extra_cc.nr; i++) {
1900 strbuf_addstr(&buf, " ");
1901 strbuf_addstr(&buf, extra_cc.items[i].string);
1902 if (i + 1 < extra_cc.nr)
1903 strbuf_addch(&buf, ',');
1904 strbuf_addch(&buf, '\n');
1907 rev.extra_headers = strbuf_detach(&buf, NULL);
1910 if (split_ident_line(&rev.from_ident, from, strlen(from)))
1911 die(_("invalid ident line: %s"), from);
1914 if (start_number < 0)
1918 * If numbered is set solely due to format.numbered in config,
1919 * and it would conflict with --keep-subject (-k) from the
1920 * command line, reset "numbered".
1922 if (numbered && keep_subject && !numbered_cmdline_opt)
1925 if (numbered && keep_subject)
1926 die(_("-n and -k are mutually exclusive"));
1927 if (keep_subject && subject_prefix)
1928 die(_("--subject-prefix/--rfc and -k are mutually exclusive"));
1929 rev.preserve_subject = keep_subject;
1931 argc = setup_revisions(argc, argv, &rev, &s_r_opt);
1933 die(_("unrecognized argument: %s"), argv[1]);
1935 if (rev.diffopt.output_format & DIFF_FORMAT_NAME)
1936 die(_("--name-only does not make sense"));
1937 if (rev.diffopt.output_format & DIFF_FORMAT_NAME_STATUS)
1938 die(_("--name-status does not make sense"));
1939 if (rev.diffopt.output_format & DIFF_FORMAT_CHECKDIFF)
1940 die(_("--check does not make sense"));
1942 if (!use_patch_format &&
1943 (!rev.diffopt.output_format ||
1944 rev.diffopt.output_format == DIFF_FORMAT_PATCH))
1945 rev.diffopt.output_format = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_SUMMARY;
1946 if (!rev.diffopt.stat_width)
1947 rev.diffopt.stat_width = MAIL_DEFAULT_WRAP;
1949 /* Always generate a patch */
1950 rev.diffopt.output_format |= DIFF_FORMAT_PATCH;
1952 rev.zero_commit = zero_commit;
1953 rev.patch_name_max = fmt_patch_name_max;
1955 if (!rev.diffopt.flags.text && !no_binary_diff)
1956 rev.diffopt.flags.binary = 1;
1959 load_display_notes(&rev.notes_opt);
1961 if (use_stdout + rev.diffopt.close_file + !!output_directory > 1)
1962 die(_("--stdout, --output, and --output-directory are mutually exclusive"));
1966 } else if (rev.diffopt.close_file) {
1968 * The diff code parsed --output; it has already opened the
1969 * file, but but we must instruct it not to close after each
1972 rev.diffopt.no_free = 1;
1976 if (!output_directory)
1977 output_directory = config_output_directory;
1978 output_directory = set_outdir(prefix, output_directory);
1980 if (rev.diffopt.use_color != GIT_COLOR_ALWAYS)
1981 rev.diffopt.use_color = GIT_COLOR_NEVER;
1983 * We consider <outdir> as 'outside of gitdir', therefore avoid
1984 * applying adjust_shared_perm in s-c-l-d.
1986 saved = get_shared_repository();
1987 set_shared_repository(0);
1988 switch (safe_create_leading_directories_const(output_directory)) {
1993 die(_("could not create leading directories "
1994 "of '%s'"), output_directory);
1996 set_shared_repository(saved);
1997 if (mkdir(output_directory, 0777) < 0 && errno != EEXIST)
1998 die_errno(_("could not create directory '%s'"),
2002 if (rev.pending.nr == 1) {
2005 if (rev.max_count < 0 && !rev.show_root_diff) {
2007 * This is traditional behaviour of "git format-patch
2008 * origin" that prepares what the origin side still
2011 rev.pending.objects[0].item->flags |= UNINTERESTING;
2012 add_head_to_pending(&rev);
2016 * Otherwise, it is "format-patch -22 HEAD", and/or
2017 * "format-patch --root HEAD". The user wants
2018 * get_revision() to do the usual traversal.
2021 if (!strcmp(rev.pending.objects[0].name, "HEAD"))
2025 const char *ref, *v;
2026 ref = resolve_ref_unsafe("HEAD", RESOLVE_REF_READING,
2028 if (ref && skip_prefix(ref, "refs/heads/", &v))
2029 branch_name = xstrdup(v);
2031 branch_name = xstrdup(""); /* no branch */
2036 * We cannot move this anywhere earlier because we do want to
2037 * know if --root was given explicitly from the command line.
2039 rev.show_root_diff = 1;
2041 if (ignore_if_in_upstream) {
2042 /* Don't say anything if head and upstream are the same. */
2043 if (rev.pending.nr == 2) {
2044 struct object_array_entry *o = rev.pending.objects;
2045 if (oideq(&o[0].item->oid, &o[1].item->oid))
2048 get_patch_ids(&rev, &ids);
2051 if (prepare_revision_walk(&rev))
2052 die(_("revision walk setup failed"));
2054 while ((commit = get_revision(&rev)) != NULL) {
2055 if (commit->object.flags & BOUNDARY) {
2057 origin = (boundary_count == 1) ? commit : NULL;
2061 if (ignore_if_in_upstream && has_commit_patch_id(commit, &ids))
2065 REALLOC_ARRAY(list, nr);
2066 list[nr - 1] = commit;
2072 if (cover_letter == -1) {
2073 if (config_cover_letter == COVER_AUTO)
2074 cover_letter = (total > 1);
2076 cover_letter = (config_cover_letter == COVER_ON);
2078 if (!keep_subject && auto_number && (total > 1 || cover_letter))
2081 rev.total = total + start_number - 1;
2083 if (idiff_prev.nr) {
2084 if (!cover_letter && total != 1)
2085 die(_("--interdiff requires --cover-letter or single patch"));
2086 rev.idiff_oid1 = &idiff_prev.oid[idiff_prev.nr - 1];
2087 rev.idiff_oid2 = get_commit_tree_oid(list[0]);
2088 rev.idiff_title = diff_title(&idiff_title, reroll_count,
2090 _("Interdiff against v%d:"));
2093 if (creation_factor < 0)
2094 creation_factor = RANGE_DIFF_CREATION_FACTOR_DEFAULT;
2095 else if (!rdiff_prev)
2096 die(_("--creation-factor requires --range-diff"));
2099 if (!cover_letter && total != 1)
2100 die(_("--range-diff requires --cover-letter or single patch"));
2102 infer_range_diff_ranges(&rdiff1, &rdiff2, rdiff_prev,
2104 rev.rdiff1 = rdiff1.buf;
2105 rev.rdiff2 = rdiff2.buf;
2106 rev.creation_factor = creation_factor;
2107 rev.rdiff_title = diff_title(&rdiff_title, reroll_count,
2109 _("Range-diff against v%d:"));
2113 ; /* --no-signature inhibits all signatures */
2114 } else if (signature && signature != git_version_string) {
2115 ; /* non-default signature already set */
2116 } else if (signature_file) {
2117 struct strbuf buf = STRBUF_INIT;
2119 if (strbuf_read_file(&buf, signature_file, 128) < 0)
2120 die_errno(_("unable to read signature file '%s'"), signature_file);
2121 signature = strbuf_detach(&buf, NULL);
2124 memset(&bases, 0, sizeof(bases));
2125 base = get_base_commit(base_commit, list, nr);
2127 reset_revision_walk();
2128 clear_object_flags(UNINTERESTING);
2129 prepare_bases(&bases, base, list, nr);
2132 if (in_reply_to || thread || cover_letter)
2133 rev.ref_message_ids = xcalloc(1, sizeof(struct string_list));
2135 const char *msgid = clean_message_id(in_reply_to);
2136 string_list_append(rev.ref_message_ids, msgid);
2138 rev.numbered_files = just_numbers;
2139 rev.patch_suffix = fmt_patch_suffix;
2142 gen_message_id(&rev, "cover");
2143 make_cover_letter(&rev, !!output_directory,
2144 origin, nr, list, branch_name, quiet);
2145 print_bases(&bases, rev.diffopt.file);
2146 print_signature(rev.diffopt.file);
2149 /* interdiff/range-diff in cover-letter; omit from patches */
2150 rev.idiff_oid1 = NULL;
2153 rev.add_signoff = do_signoff;
2156 progress = start_delayed_progress(_("Generating patches"), total);
2159 display_progress(progress, total - nr);
2161 rev.nr = total - nr + (start_number - 1);
2162 /* Make the second and subsequent mails replies to the first */
2164 /* Have we already had a message ID? */
2165 if (rev.message_id) {
2167 * For deep threading: make every mail
2168 * a reply to the previous one, no
2169 * matter what other options are set.
2171 * For shallow threading:
2173 * Without --cover-letter and
2174 * --in-reply-to, make every mail a
2175 * reply to the one before.
2177 * With --in-reply-to but no
2178 * --cover-letter, make every mail a
2179 * reply to the <reply-to>.
2181 * With --cover-letter, make every
2182 * mail but the cover letter a reply
2183 * to the cover letter. The cover
2184 * letter is a reply to the
2185 * --in-reply-to, if specified.
2187 if (thread == THREAD_SHALLOW
2188 && rev.ref_message_ids->nr > 0
2189 && (!cover_letter || rev.nr > 1))
2190 free(rev.message_id);
2192 string_list_append(rev.ref_message_ids,
2195 gen_message_id(&rev, oid_to_hex(&commit->object.oid));
2198 if (output_directory &&
2199 open_next_file(rev.numbered_files ? NULL : commit, NULL, &rev, quiet))
2200 die(_("failed to create output files"));
2201 shown = log_tree_commit(&rev, commit);
2202 free_commit_buffer(the_repository->parsed_objects,
2205 /* We put one extra blank line between formatted
2206 * patches and this flag is used by log-tree code
2207 * to see if it needs to emit a LF before showing
2208 * the log; when using one file per patch, we do
2209 * not want the extra blank line.
2211 if (output_directory)
2214 print_bases(&bases, rev.diffopt.file);
2215 if (rev.mime_boundary)
2216 fprintf(rev.diffopt.file, "\n--%s%s--\n\n\n",
2217 mime_boundary_leader,
2220 print_signature(rev.diffopt.file);
2222 if (output_directory)
2223 fclose(rev.diffopt.file);
2225 stop_progress(&progress);
2228 string_list_clear(&extra_to, 0);
2229 string_list_clear(&extra_cc, 0);
2230 string_list_clear(&extra_hdr, 0);
2231 if (ignore_if_in_upstream)
2232 free_patch_ids(&ids);
2235 oid_array_clear(&idiff_prev);
2236 strbuf_release(&idiff_title);
2237 strbuf_release(&rdiff1);
2238 strbuf_release(&rdiff2);
2239 strbuf_release(&rdiff_title);
2243 static int add_pending_commit(const char *arg, struct rev_info *revs, int flags)
2245 struct object_id oid;
2246 if (get_oid(arg, &oid) == 0) {
2247 struct commit *commit = lookup_commit_reference(the_repository,
2250 commit->object.flags |= flags;
2251 add_pending_object(revs, &commit->object, arg);
2258 static const char * const cherry_usage[] = {
2259 N_("git cherry [-v] [<upstream> [<head> [<limit>]]]"),
2263 static void print_commit(char sign, struct commit *commit, int verbose,
2264 int abbrev, FILE *file)
2267 fprintf(file, "%c %s\n", sign,
2268 find_unique_abbrev(&commit->object.oid, abbrev));
2270 struct strbuf buf = STRBUF_INIT;
2271 pp_commit_easy(CMIT_FMT_ONELINE, commit, &buf);
2272 fprintf(file, "%c %s %s\n", sign,
2273 find_unique_abbrev(&commit->object.oid, abbrev),
2275 strbuf_release(&buf);
2279 int cmd_cherry(int argc, const char **argv, const char *prefix)
2281 struct rev_info revs;
2282 struct patch_ids ids;
2283 struct commit *commit;
2284 struct commit_list *list = NULL;
2285 struct branch *current_branch;
2286 const char *upstream;
2287 const char *head = "HEAD";
2288 const char *limit = NULL;
2289 int verbose = 0, abbrev = 0;
2291 struct option options[] = {
2292 OPT__ABBREV(&abbrev),
2293 OPT__VERBOSE(&verbose, N_("be verbose")),
2297 argc = parse_options(argc, argv, prefix, options, cherry_usage, 0);
2310 current_branch = branch_get(NULL);
2311 upstream = branch_get_upstream(current_branch, NULL);
2313 fprintf(stderr, _("Could not find a tracked"
2314 " remote branch, please"
2315 " specify <upstream> manually.\n"));
2316 usage_with_options(cherry_usage, options);
2320 repo_init_revisions(the_repository, &revs, prefix);
2321 revs.max_parents = 1;
2323 if (add_pending_commit(head, &revs, 0))
2324 die(_("unknown commit %s"), head);
2325 if (add_pending_commit(upstream, &revs, UNINTERESTING))
2326 die(_("unknown commit %s"), upstream);
2328 /* Don't say anything if head and upstream are the same. */
2329 if (revs.pending.nr == 2) {
2330 struct object_array_entry *o = revs.pending.objects;
2331 if (oideq(&o[0].item->oid, &o[1].item->oid))
2335 get_patch_ids(&revs, &ids);
2337 if (limit && add_pending_commit(limit, &revs, UNINTERESTING))
2338 die(_("unknown commit %s"), limit);
2340 /* reverse the list of commits */
2341 if (prepare_revision_walk(&revs))
2342 die(_("revision walk setup failed"));
2343 while ((commit = get_revision(&revs)) != NULL) {
2344 commit_list_insert(commit, &list);
2350 commit = list->item;
2351 if (has_commit_patch_id(commit, &ids))
2353 print_commit(sign, commit, verbose, abbrev, revs.diffopt.file);
2357 free_patch_ids(&ids);