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"
19 #include "reflog-walk.h"
20 #include "patch-ids.h"
21 #include "run-command.h"
24 #include "string-list.h"
25 #include "parse-options.h"
28 #include "streaming.h"
31 #include "gpg-interface.h"
33 #include "commit-slab.h"
34 #include "repository.h"
35 #include "commit-reach.h"
36 #include "interdiff.h"
37 #include "range-diff.h"
39 #define MAIL_DEFAULT_WRAP 72
40 #define COVER_FROM_AUTO_MAX_SUBJECT_LEN 100
42 /* Set a default date-time format for git log ("log.date" config variable) */
43 static const char *default_date_mode = NULL;
45 static int default_abbrev_commit;
46 static int default_show_root = 1;
47 static int default_follow;
48 static int default_show_signature;
49 static int decoration_style;
50 static int decoration_given;
51 static int use_mailmap_config = 1;
52 static const char *fmt_patch_subject_prefix = "PATCH";
53 static const char *fmt_pretty;
55 static const char * const builtin_log_usage[] = {
56 N_("git log [<options>] [<revision-range>] [[--] <path>...]"),
57 N_("git show [<options>] <object>..."),
61 struct line_opt_callback_data {
64 struct string_list args;
67 static int session_is_interactive(void)
69 return isatty(1) || pager_in_use();
72 static int auto_decoration_style(void)
74 return session_is_interactive() ? DECORATE_SHORT_REFS : 0;
77 static int parse_decoration_style(const char *value)
79 switch (git_parse_maybe_bool(value)) {
81 return DECORATE_SHORT_REFS;
87 if (!strcmp(value, "full"))
88 return DECORATE_FULL_REFS;
89 else if (!strcmp(value, "short"))
90 return DECORATE_SHORT_REFS;
91 else if (!strcmp(value, "auto"))
92 return auto_decoration_style();
94 * Please update _git_log() in git-completion.bash when you
95 * add new decoration styles.
100 static int decorate_callback(const struct option *opt, const char *arg, int unset)
103 decoration_style = 0;
105 decoration_style = parse_decoration_style(arg);
107 decoration_style = DECORATE_SHORT_REFS;
109 if (decoration_style < 0)
110 die(_("invalid --decorate option: %s"), arg);
112 decoration_given = 1;
117 static int log_line_range_callback(const struct option *option, const char *arg, int unset)
119 struct line_opt_callback_data *data = option->value;
121 BUG_ON_OPT_NEG(unset);
126 data->rev->line_level_traverse = 1;
127 string_list_append(&data->args, arg);
132 static void init_log_defaults(void)
134 init_grep_defaults(the_repository);
135 init_diff_ui_defaults();
137 decoration_style = auto_decoration_style();
140 static void cmd_log_init_defaults(struct rev_info *rev)
143 get_commit_format(fmt_pretty, rev);
145 rev->diffopt.flags.default_follow_renames = 1;
146 rev->verbose_header = 1;
147 rev->diffopt.flags.recursive = 1;
148 rev->diffopt.stat_width = -1; /* use full terminal width */
149 rev->diffopt.stat_graph_width = -1; /* respect statGraphWidth config */
150 rev->abbrev_commit = default_abbrev_commit;
151 rev->show_root_diff = default_show_root;
152 rev->subject_prefix = fmt_patch_subject_prefix;
153 rev->show_signature = default_show_signature;
154 rev->diffopt.flags.allow_textconv = 1;
156 if (default_date_mode)
157 parse_date_format(default_date_mode, &rev->date_mode);
160 static void cmd_log_init_finish(int argc, const char **argv, const char *prefix,
161 struct rev_info *rev, struct setup_revision_opt *opt)
163 struct userformat_want w;
164 int quiet = 0, source = 0, mailmap;
165 static struct line_opt_callback_data line_cb = {NULL, NULL, STRING_LIST_INIT_DUP};
166 static struct string_list decorate_refs_exclude = STRING_LIST_INIT_NODUP;
167 static struct string_list decorate_refs_include = STRING_LIST_INIT_NODUP;
168 struct decoration_filter decoration_filter = {&decorate_refs_include,
169 &decorate_refs_exclude};
170 static struct revision_sources revision_sources;
172 const struct option builtin_log_options[] = {
173 OPT__QUIET(&quiet, N_("suppress diff output")),
174 OPT_BOOL(0, "source", &source, N_("show source")),
175 OPT_BOOL(0, "use-mailmap", &mailmap, N_("Use mail map file")),
176 OPT_STRING_LIST(0, "decorate-refs", &decorate_refs_include,
177 N_("pattern"), N_("only decorate refs that match <pattern>")),
178 OPT_STRING_LIST(0, "decorate-refs-exclude", &decorate_refs_exclude,
179 N_("pattern"), N_("do not decorate refs that match <pattern>")),
180 { OPTION_CALLBACK, 0, "decorate", NULL, NULL, N_("decorate options"),
181 PARSE_OPT_OPTARG, decorate_callback},
182 OPT_CALLBACK('L', NULL, &line_cb, "n,m:file",
183 N_("Process line range n,m in file, counting from 1"),
184 log_line_range_callback),
189 line_cb.prefix = prefix;
191 mailmap = use_mailmap_config;
192 argc = parse_options(argc, argv, prefix,
193 builtin_log_options, builtin_log_usage,
194 PARSE_OPT_KEEP_ARGV0 | PARSE_OPT_KEEP_UNKNOWN |
195 PARSE_OPT_KEEP_DASHDASH);
198 rev->diffopt.output_format |= DIFF_FORMAT_NO_OUTPUT;
199 argc = setup_revisions(argc, argv, rev, opt);
201 /* Any arguments at this point are not recognized */
203 die(_("unrecognized argument: %s"), argv[1]);
205 memset(&w, 0, sizeof(w));
206 userformat_find_requirements(NULL, &w);
208 if (!rev->show_notes_given && (!rev->pretty_given || w.notes))
211 load_display_notes(&rev->notes_opt);
213 if ((rev->diffopt.pickaxe_opts & DIFF_PICKAXE_KINDS_MASK) ||
214 rev->diffopt.filter || rev->diffopt.flags.follow_renames)
215 rev->always_show_header = 0;
217 if (source || w.source) {
218 init_revision_sources(&revision_sources);
219 rev->sources = &revision_sources;
223 rev->mailmap = xcalloc(1, sizeof(struct string_list));
224 read_mailmap(rev->mailmap, NULL);
227 if (rev->pretty_given && rev->commit_format == CMIT_FMT_RAW) {
229 * "log --pretty=raw" is special; ignore UI oriented
230 * configuration variables such as decoration.
232 if (!decoration_given)
233 decoration_style = 0;
234 if (!rev->abbrev_commit_given)
235 rev->abbrev_commit = 0;
238 if (decoration_style) {
239 rev->show_decorations = 1;
240 load_ref_decorations(&decoration_filter, decoration_style);
243 if (rev->line_level_traverse)
244 line_log_init(rev, line_cb.prefix, &line_cb.args);
249 static void cmd_log_init(int argc, const char **argv, const char *prefix,
250 struct rev_info *rev, struct setup_revision_opt *opt)
252 cmd_log_init_defaults(rev);
253 cmd_log_init_finish(argc, argv, prefix, rev, opt);
257 * This gives a rough estimate for how many commits we
258 * will print out in the list.
260 static int estimate_commit_count(struct commit_list *list)
265 struct commit *commit = list->item;
266 unsigned int flags = commit->object.flags;
268 if (!(flags & (TREESAME | UNINTERESTING)))
274 static void show_early_header(struct rev_info *rev, const char *stage, int nr)
276 if (rev->shown_one) {
278 if (rev->commit_format != CMIT_FMT_ONELINE)
279 putchar(rev->diffopt.line_termination);
281 fprintf(rev->diffopt.file, _("Final output: %d %s\n"), nr, stage);
284 static struct itimerval early_output_timer;
286 static void log_show_early(struct rev_info *revs, struct commit_list *list)
288 int i = revs->early_output, close_file = revs->diffopt.close_file;
291 revs->diffopt.close_file = 0;
292 sort_in_topological_order(&list, revs->sort_order);
294 struct commit *commit = list->item;
295 switch (simplify_commit(revs, commit)) {
298 int n = estimate_commit_count(list);
299 show_early_header(revs, "incomplete", n);
302 log_tree_commit(revs, commit);
309 fclose(revs->diffopt.file);
315 /* Did we already get enough commits for the early output? */
318 fclose(revs->diffopt.file);
323 * ..if no, then repeat it twice a second until we
326 * NOTE! We don't use "it_interval", because if the
327 * reader isn't listening, we want our output to be
328 * throttled by the writing, and not have the timer
329 * trigger every second even if we're blocked on a
332 early_output_timer.it_value.tv_sec = 0;
333 early_output_timer.it_value.tv_usec = 500000;
334 setitimer(ITIMER_REAL, &early_output_timer, NULL);
337 static void early_output(int signal)
339 show_early_output = log_show_early;
342 static void setup_early_output(void)
347 * Set up the signal handler, minimally intrusively:
348 * we only set a single volatile integer word (not
349 * using sigatomic_t - trying to avoid unnecessary
350 * system dependencies and headers), and using
353 memset(&sa, 0, sizeof(sa));
354 sa.sa_handler = early_output;
355 sigemptyset(&sa.sa_mask);
356 sa.sa_flags = SA_RESTART;
357 sigaction(SIGALRM, &sa, NULL);
360 * If we can get the whole output in less than a
361 * tenth of a second, don't even bother doing the
362 * early-output thing..
364 * This is a one-time-only trigger.
366 early_output_timer.it_value.tv_sec = 0;
367 early_output_timer.it_value.tv_usec = 100000;
368 setitimer(ITIMER_REAL, &early_output_timer, NULL);
371 static void finish_early_output(struct rev_info *rev)
373 int n = estimate_commit_count(rev->commits);
374 signal(SIGALRM, SIG_IGN);
375 show_early_header(rev, "done", n);
378 static int cmd_log_walk(struct rev_info *rev)
380 struct commit *commit;
382 int saved_dcctc = 0, close_file = rev->diffopt.close_file;
384 if (rev->early_output)
385 setup_early_output();
387 if (prepare_revision_walk(rev))
388 die(_("revision walk setup failed"));
390 if (rev->early_output)
391 finish_early_output(rev);
394 * For --check and --exit-code, the exit code is based on CHECK_FAILED
395 * and HAS_CHANGES being accumulated in rev->diffopt, so be careful to
396 * retain that state information if replacing rev->diffopt in this loop
398 rev->diffopt.close_file = 0;
399 while ((commit = get_revision(rev)) != NULL) {
400 if (!log_tree_commit(rev, commit) && rev->max_count >= 0)
402 * We decremented max_count in get_revision,
403 * but we didn't actually show the commit.
406 if (!rev->reflog_info) {
408 * We may show a given commit multiple times when
409 * walking the reflogs.
411 free_commit_buffer(the_repository->parsed_objects,
413 free_commit_list(commit->parents);
414 commit->parents = NULL;
416 if (saved_nrl < rev->diffopt.needed_rename_limit)
417 saved_nrl = rev->diffopt.needed_rename_limit;
418 if (rev->diffopt.degraded_cc_to_c)
421 rev->diffopt.degraded_cc_to_c = saved_dcctc;
422 rev->diffopt.needed_rename_limit = saved_nrl;
424 fclose(rev->diffopt.file);
426 if (rev->diffopt.output_format & DIFF_FORMAT_CHECKDIFF &&
427 rev->diffopt.flags.check_failed) {
430 return diff_result_code(&rev->diffopt, 0);
433 static int git_log_config(const char *var, const char *value, void *cb)
435 const char *slot_name;
437 if (!strcmp(var, "format.pretty"))
438 return git_config_string(&fmt_pretty, var, value);
439 if (!strcmp(var, "format.subjectprefix"))
440 return git_config_string(&fmt_patch_subject_prefix, var, value);
441 if (!strcmp(var, "log.abbrevcommit")) {
442 default_abbrev_commit = git_config_bool(var, value);
445 if (!strcmp(var, "log.date"))
446 return git_config_string(&default_date_mode, var, value);
447 if (!strcmp(var, "log.decorate")) {
448 decoration_style = parse_decoration_style(value);
449 if (decoration_style < 0)
450 decoration_style = 0; /* maybe warn? */
453 if (!strcmp(var, "log.showroot")) {
454 default_show_root = git_config_bool(var, value);
457 if (!strcmp(var, "log.follow")) {
458 default_follow = git_config_bool(var, value);
461 if (skip_prefix(var, "color.decorate.", &slot_name))
462 return parse_decorate_color_config(var, slot_name, value);
463 if (!strcmp(var, "log.mailmap")) {
464 use_mailmap_config = git_config_bool(var, value);
467 if (!strcmp(var, "log.showsignature")) {
468 default_show_signature = git_config_bool(var, value);
472 if (grep_config(var, value, cb) < 0)
474 if (git_gpg_config(var, value, cb) < 0)
476 return git_diff_ui_config(var, value, cb);
479 int cmd_whatchanged(int argc, const char **argv, const char *prefix)
482 struct setup_revision_opt opt;
485 git_config(git_log_config, NULL);
487 repo_init_revisions(the_repository, &rev, prefix);
489 rev.simplify_history = 0;
490 memset(&opt, 0, sizeof(opt));
492 opt.revarg_opt = REVARG_COMMITTISH;
493 cmd_log_init(argc, argv, prefix, &rev, &opt);
494 if (!rev.diffopt.output_format)
495 rev.diffopt.output_format = DIFF_FORMAT_RAW;
496 return cmd_log_walk(&rev);
499 static void show_tagger(const char *buf, struct rev_info *rev)
501 struct strbuf out = STRBUF_INIT;
502 struct pretty_print_context pp = {0};
504 pp.fmt = rev->commit_format;
505 pp.date_mode = rev->date_mode;
506 pp_user_info(&pp, "Tagger", &out, buf, get_log_output_encoding());
507 fprintf(rev->diffopt.file, "%s", out.buf);
508 strbuf_release(&out);
511 static int show_blob_object(const struct object_id *oid, struct rev_info *rev, const char *obj_name)
513 struct object_id oidc;
514 struct object_context obj_context;
518 fflush(rev->diffopt.file);
519 if (!rev->diffopt.flags.textconv_set_via_cmdline ||
520 !rev->diffopt.flags.allow_textconv)
521 return stream_blob_to_fd(1, oid, NULL, 0);
523 if (get_oid_with_context(the_repository, obj_name,
525 &oidc, &obj_context))
526 die(_("not a valid object name %s"), obj_name);
527 if (!obj_context.path ||
528 !textconv_object(the_repository, obj_context.path,
529 obj_context.mode, &oidc, 1, &buf, &size)) {
530 free(obj_context.path);
531 return stream_blob_to_fd(1, oid, NULL, 0);
535 die(_("git show %s: bad file"), obj_name);
537 write_or_die(1, buf, size);
538 free(obj_context.path);
542 static int show_tag_object(const struct object_id *oid, struct rev_info *rev)
545 enum object_type type;
546 char *buf = read_object_file(oid, &type, &size);
550 return error(_("could not read object %s"), oid_to_hex(oid));
552 assert(type == OBJ_TAG);
553 while (offset < size && buf[offset] != '\n') {
554 int new_offset = offset + 1;
556 while (new_offset < size && buf[new_offset++] != '\n')
558 if (skip_prefix(buf + offset, "tagger ", &ident))
559 show_tagger(ident, rev);
564 fwrite(buf + offset, size - offset, 1, rev->diffopt.file);
569 static int show_tree_object(const struct object_id *oid,
571 const char *pathname, unsigned mode, int stage, void *context)
573 FILE *file = context;
574 fprintf(file, "%s%s\n", pathname, S_ISDIR(mode) ? "/" : "");
578 static void show_setup_revisions_tweak(struct rev_info *rev,
579 struct setup_revision_opt *opt)
581 if (rev->ignore_merges) {
582 /* There was no "-m" on the command line */
583 rev->ignore_merges = 0;
584 if (!rev->first_parent_only && !rev->combine_merges) {
585 /* No "--first-parent", "-c", or "--cc" */
586 rev->combine_merges = 1;
587 rev->dense_combined_merges = 1;
590 if (!rev->diffopt.output_format)
591 rev->diffopt.output_format = DIFF_FORMAT_PATCH;
594 int cmd_show(int argc, const char **argv, const char *prefix)
597 struct object_array_entry *objects;
598 struct setup_revision_opt opt;
599 struct pathspec match_all;
600 int i, count, ret = 0;
603 git_config(git_log_config, NULL);
605 memset(&match_all, 0, sizeof(match_all));
606 repo_init_revisions(the_repository, &rev, prefix);
608 rev.always_show_header = 1;
609 rev.no_walk = REVISION_WALK_NO_WALK_SORTED;
610 rev.diffopt.stat_width = -1; /* Scale to real terminal size */
612 memset(&opt, 0, sizeof(opt));
614 opt.tweak = show_setup_revisions_tweak;
615 cmd_log_init(argc, argv, prefix, &rev, &opt);
618 return cmd_log_walk(&rev);
620 count = rev.pending.nr;
621 objects = rev.pending.objects;
622 for (i = 0; i < count && !ret; i++) {
623 struct object *o = objects[i].item;
624 const char *name = objects[i].name;
627 ret = show_blob_object(&o->oid, &rev, name);
630 struct tag *t = (struct tag *)o;
631 struct object_id *oid = get_tagged_oid(t);
635 fprintf(rev.diffopt.file, "%stag %s%s\n",
636 diff_get_color_opt(&rev.diffopt, DIFF_COMMIT),
638 diff_get_color_opt(&rev.diffopt, DIFF_RESET));
639 ret = show_tag_object(&o->oid, &rev);
643 o = parse_object(the_repository, oid);
645 ret = error(_("could not read object %s"),
654 fprintf(rev.diffopt.file, "%stree %s%s\n\n",
655 diff_get_color_opt(&rev.diffopt, DIFF_COMMIT),
657 diff_get_color_opt(&rev.diffopt, DIFF_RESET));
658 read_tree_recursive(the_repository, (struct tree *)o, "",
659 0, 0, &match_all, show_tree_object,
664 rev.pending.nr = rev.pending.alloc = 0;
665 rev.pending.objects = NULL;
666 add_object_array(o, name, &rev.pending);
667 ret = cmd_log_walk(&rev);
670 ret = error(_("unknown type: %d"), o->type);
678 * This is equivalent to "git log -g --abbrev-commit --pretty=oneline"
680 int cmd_log_reflog(int argc, const char **argv, const char *prefix)
683 struct setup_revision_opt opt;
686 git_config(git_log_config, NULL);
688 repo_init_revisions(the_repository, &rev, prefix);
689 init_reflog_walk(&rev.reflog_info);
690 rev.verbose_header = 1;
691 memset(&opt, 0, sizeof(opt));
693 cmd_log_init_defaults(&rev);
694 rev.abbrev_commit = 1;
695 rev.commit_format = CMIT_FMT_ONELINE;
696 rev.use_terminator = 1;
697 rev.always_show_header = 1;
698 cmd_log_init_finish(argc, argv, prefix, &rev, &opt);
700 return cmd_log_walk(&rev);
703 static void log_setup_revisions_tweak(struct rev_info *rev,
704 struct setup_revision_opt *opt)
706 if (rev->diffopt.flags.default_follow_renames &&
707 rev->prune_data.nr == 1)
708 rev->diffopt.flags.follow_renames = 1;
710 /* Turn --cc/-c into -p --cc/-c when -p was not given */
711 if (!rev->diffopt.output_format && rev->combine_merges)
712 rev->diffopt.output_format = DIFF_FORMAT_PATCH;
714 /* Turn -m on when --cc/-c was given */
715 if (rev->combine_merges)
716 rev->ignore_merges = 0;
719 int cmd_log(int argc, const char **argv, const char *prefix)
722 struct setup_revision_opt opt;
725 git_config(git_log_config, NULL);
727 repo_init_revisions(the_repository, &rev, prefix);
728 rev.always_show_header = 1;
729 memset(&opt, 0, sizeof(opt));
731 opt.revarg_opt = REVARG_COMMITTISH;
732 opt.tweak = log_setup_revisions_tweak;
733 cmd_log_init(argc, argv, prefix, &rev, &opt);
734 return cmd_log_walk(&rev);
739 static const char *fmt_patch_suffix = ".patch";
740 static int numbered = 0;
741 static int auto_number = 1;
743 static char *default_attach = NULL;
745 static struct string_list extra_hdr = STRING_LIST_INIT_NODUP;
746 static struct string_list extra_to = STRING_LIST_INIT_NODUP;
747 static struct string_list extra_cc = STRING_LIST_INIT_NODUP;
749 static void add_header(const char *value)
751 struct string_list_item *item;
752 int len = strlen(value);
753 while (len && value[len - 1] == '\n')
756 if (!strncasecmp(value, "to: ", 4)) {
757 item = string_list_append(&extra_to, value + 4);
759 } else if (!strncasecmp(value, "cc: ", 4)) {
760 item = string_list_append(&extra_cc, value + 4);
763 item = string_list_append(&extra_hdr, value);
766 item->string[len] = '\0';
782 enum cover_from_description {
789 static enum thread_level thread;
790 static int do_signoff;
791 static int base_auto;
793 static const char *signature = git_version_string;
794 static const char *signature_file;
795 static enum cover_setting config_cover_letter;
796 static const char *config_output_directory;
797 static enum cover_from_description cover_from_description_mode = COVER_FROM_MESSAGE;
798 static int show_notes;
799 static struct display_notes_opt notes_opt;
801 static enum cover_from_description parse_cover_from_description(const char *arg)
803 if (!arg || !strcmp(arg, "default"))
804 return COVER_FROM_MESSAGE;
805 else if (!strcmp(arg, "none"))
806 return COVER_FROM_NONE;
807 else if (!strcmp(arg, "message"))
808 return COVER_FROM_MESSAGE;
809 else if (!strcmp(arg, "subject"))
810 return COVER_FROM_SUBJECT;
811 else if (!strcmp(arg, "auto"))
812 return COVER_FROM_AUTO;
814 die(_("%s: invalid cover from description mode"), arg);
817 static int git_format_config(const char *var, const char *value, void *cb)
819 if (!strcmp(var, "format.headers")) {
821 die(_("format.headers without value"));
825 if (!strcmp(var, "format.suffix"))
826 return git_config_string(&fmt_patch_suffix, var, value);
827 if (!strcmp(var, "format.to")) {
829 return config_error_nonbool(var);
830 string_list_append(&extra_to, value);
833 if (!strcmp(var, "format.cc")) {
835 return config_error_nonbool(var);
836 string_list_append(&extra_cc, value);
839 if (!strcmp(var, "diff.color") || !strcmp(var, "color.diff") ||
840 !strcmp(var, "color.ui") || !strcmp(var, "diff.submodule")) {
843 if (!strcmp(var, "format.numbered")) {
844 if (value && !strcasecmp(value, "auto")) {
848 numbered = git_config_bool(var, value);
849 auto_number = auto_number && numbered;
852 if (!strcmp(var, "format.attach")) {
854 default_attach = xstrdup(value);
856 default_attach = xstrdup(git_version_string);
859 if (!strcmp(var, "format.thread")) {
860 if (value && !strcasecmp(value, "deep")) {
861 thread = THREAD_DEEP;
864 if (value && !strcasecmp(value, "shallow")) {
865 thread = THREAD_SHALLOW;
868 thread = git_config_bool(var, value) ? THREAD_SHALLOW : THREAD_UNSET;
871 if (!strcmp(var, "format.signoff")) {
872 do_signoff = git_config_bool(var, value);
875 if (!strcmp(var, "format.signature"))
876 return git_config_string(&signature, var, value);
877 if (!strcmp(var, "format.signaturefile"))
878 return git_config_pathname(&signature_file, var, value);
879 if (!strcmp(var, "format.coverletter")) {
880 if (value && !strcasecmp(value, "auto")) {
881 config_cover_letter = COVER_AUTO;
884 config_cover_letter = git_config_bool(var, value) ? COVER_ON : COVER_OFF;
887 if (!strcmp(var, "format.outputdirectory"))
888 return git_config_string(&config_output_directory, var, value);
889 if (!strcmp(var, "format.useautobase")) {
890 base_auto = git_config_bool(var, value);
893 if (!strcmp(var, "format.from")) {
894 int b = git_parse_maybe_bool(value);
897 from = xstrdup(value);
899 from = xstrdup(git_committer_info(IDENT_NO_DATE));
904 if (!strcmp(var, "format.notes")) {
905 int b = git_parse_maybe_bool(value);
907 enable_ref_display_notes(¬es_opt, &show_notes, value);
909 enable_default_display_notes(¬es_opt, &show_notes);
911 disable_display_notes(¬es_opt, &show_notes);
914 if (!strcmp(var, "format.coverfromdescription")) {
915 cover_from_description_mode = parse_cover_from_description(value);
919 return git_log_config(var, value, cb);
922 static const char *output_directory = NULL;
923 static int outdir_offset;
925 static int open_next_file(struct commit *commit, const char *subject,
926 struct rev_info *rev, int quiet)
928 struct strbuf filename = STRBUF_INIT;
929 int suffix_len = strlen(rev->patch_suffix) + 1;
931 if (output_directory) {
932 strbuf_addstr(&filename, output_directory);
934 PATH_MAX - FORMAT_PATCH_NAME_MAX - suffix_len) {
935 strbuf_release(&filename);
936 return error(_("name of output directory is too long"));
938 strbuf_complete(&filename, '/');
941 if (rev->numbered_files)
942 strbuf_addf(&filename, "%d", rev->nr);
944 fmt_output_commit(&filename, commit, rev);
946 fmt_output_subject(&filename, subject, rev);
949 printf("%s\n", filename.buf + outdir_offset);
951 if ((rev->diffopt.file = fopen(filename.buf, "w")) == NULL) {
952 error_errno(_("cannot open patch file %s"), filename.buf);
953 strbuf_release(&filename);
957 strbuf_release(&filename);
961 static void get_patch_ids(struct rev_info *rev, struct patch_ids *ids)
963 struct rev_info check_rev;
964 struct commit *commit, *c1, *c2;
965 struct object *o1, *o2;
966 unsigned flags1, flags2;
968 if (rev->pending.nr != 2)
969 die(_("need exactly one range"));
971 o1 = rev->pending.objects[0].item;
972 o2 = rev->pending.objects[1].item;
975 c1 = lookup_commit_reference(the_repository, &o1->oid);
976 c2 = lookup_commit_reference(the_repository, &o2->oid);
978 if ((flags1 & UNINTERESTING) == (flags2 & UNINTERESTING))
979 die(_("not a range"));
981 init_patch_ids(the_repository, ids);
983 /* given a range a..b get all patch ids for b..a */
984 repo_init_revisions(the_repository, &check_rev, rev->prefix);
985 check_rev.max_parents = 1;
986 o1->flags ^= UNINTERESTING;
987 o2->flags ^= UNINTERESTING;
988 add_pending_object(&check_rev, o1, "o1");
989 add_pending_object(&check_rev, o2, "o2");
990 if (prepare_revision_walk(&check_rev))
991 die(_("revision walk setup failed"));
993 while ((commit = get_revision(&check_rev)) != NULL) {
994 add_commit_patch_id(commit, ids);
997 /* reset for next revision walk */
998 clear_commit_marks(c1, SEEN | UNINTERESTING | SHOWN | ADDED);
999 clear_commit_marks(c2, SEEN | UNINTERESTING | SHOWN | ADDED);
1004 static void gen_message_id(struct rev_info *info, char *base)
1006 struct strbuf buf = STRBUF_INIT;
1007 strbuf_addf(&buf, "%s.%"PRItime".git.%s", base,
1008 (timestamp_t) time(NULL),
1009 git_committer_info(IDENT_NO_NAME|IDENT_NO_DATE|IDENT_STRICT));
1010 info->message_id = strbuf_detach(&buf, NULL);
1013 static void print_signature(FILE *file)
1015 if (!signature || !*signature)
1018 fprintf(file, "-- \n%s", signature);
1019 if (signature[strlen(signature)-1] != '\n')
1024 static char *find_branch_name(struct rev_info *rev)
1026 int i, positive = -1;
1027 struct object_id branch_oid;
1028 const struct object_id *tip_oid;
1029 const char *ref, *v;
1030 char *full_ref, *branch = NULL;
1032 for (i = 0; i < rev->cmdline.nr; i++) {
1033 if (rev->cmdline.rev[i].flags & UNINTERESTING)
1042 ref = rev->cmdline.rev[positive].name;
1043 tip_oid = &rev->cmdline.rev[positive].item->oid;
1044 if (dwim_ref(ref, strlen(ref), &branch_oid, &full_ref) &&
1045 skip_prefix(full_ref, "refs/heads/", &v) &&
1046 oideq(tip_oid, &branch_oid))
1047 branch = xstrdup(v);
1052 static void show_diffstat(struct rev_info *rev,
1053 struct commit *origin, struct commit *head)
1055 struct diff_options opts;
1057 memcpy(&opts, &rev->diffopt, sizeof(opts));
1058 opts.output_format = DIFF_FORMAT_SUMMARY | DIFF_FORMAT_DIFFSTAT;
1059 diff_setup_done(&opts);
1061 diff_tree_oid(get_commit_tree_oid(origin),
1062 get_commit_tree_oid(head),
1064 diffcore_std(&opts);
1067 fprintf(rev->diffopt.file, "\n");
1070 static void prepare_cover_text(struct pretty_print_context *pp,
1071 const char *branch_name,
1073 const char *encoding,
1076 const char *subject = "*** SUBJECT HERE ***";
1077 const char *body = "*** BLURB HERE ***";
1078 struct strbuf description_sb = STRBUF_INIT;
1079 struct strbuf subject_sb = STRBUF_INIT;
1081 if (cover_from_description_mode == COVER_FROM_NONE)
1084 if (branch_name && *branch_name)
1085 read_branch_desc(&description_sb, branch_name);
1086 if (!description_sb.len)
1089 if (cover_from_description_mode == COVER_FROM_SUBJECT ||
1090 cover_from_description_mode == COVER_FROM_AUTO)
1091 body = format_subject(&subject_sb, description_sb.buf, " ");
1093 if (cover_from_description_mode == COVER_FROM_MESSAGE ||
1094 (cover_from_description_mode == COVER_FROM_AUTO &&
1095 subject_sb.len > COVER_FROM_AUTO_MAX_SUBJECT_LEN))
1096 body = description_sb.buf;
1098 subject = subject_sb.buf;
1101 pp_title_line(pp, &subject, sb, encoding, need_8bit_cte);
1102 pp_remainder(pp, &body, sb, 0);
1104 strbuf_release(&description_sb);
1105 strbuf_release(&subject_sb);
1108 static int get_notes_refs(struct string_list_item *item, void *arg)
1110 argv_array_pushf(arg, "--notes=%s", item->string);
1114 static void get_notes_args(struct argv_array *arg, struct rev_info *rev)
1116 if (!rev->show_notes) {
1117 argv_array_push(arg, "--no-notes");
1118 } else if (rev->notes_opt.use_default_notes > 0 ||
1119 (rev->notes_opt.use_default_notes == -1 &&
1120 !rev->notes_opt.extra_notes_refs.nr)) {
1121 argv_array_push(arg, "--notes");
1123 for_each_string_list(&rev->notes_opt.extra_notes_refs, get_notes_refs, arg);
1127 static void make_cover_letter(struct rev_info *rev, int use_stdout,
1128 struct commit *origin,
1129 int nr, struct commit **list,
1130 const char *branch_name,
1133 const char *committer;
1134 struct shortlog log;
1135 struct strbuf sb = STRBUF_INIT;
1137 const char *encoding = "UTF-8";
1138 int need_8bit_cte = 0;
1139 struct pretty_print_context pp = {0};
1140 struct commit *head = list[0];
1142 if (!cmit_fmt_is_mail(rev->commit_format))
1143 die(_("cover letter needs email format"));
1145 committer = git_committer_info(0);
1148 open_next_file(NULL, rev->numbered_files ? NULL : "cover-letter", rev, quiet))
1149 die(_("failed to create cover-letter file"));
1151 log_write_email_headers(rev, head, &pp.after_subject, &need_8bit_cte, 0);
1153 for (i = 0; !need_8bit_cte && i < nr; i++) {
1154 const char *buf = get_commit_buffer(list[i], NULL);
1155 if (has_non_ascii(buf))
1157 unuse_commit_buffer(list[i], buf);
1161 branch_name = find_branch_name(rev);
1163 pp.fmt = CMIT_FMT_EMAIL;
1164 pp.date_mode.type = DATE_RFC2822;
1166 pp.print_email_subject = 1;
1167 pp_user_info(&pp, NULL, &sb, committer, encoding);
1168 prepare_cover_text(&pp, branch_name, &sb, encoding, need_8bit_cte);
1169 fprintf(rev->diffopt.file, "%s\n", sb.buf);
1171 strbuf_release(&sb);
1173 shortlog_init(&log);
1175 log.wrap = MAIL_DEFAULT_WRAP;
1178 log.file = rev->diffopt.file;
1179 for (i = 0; i < nr; i++)
1180 shortlog_add_commit(&log, list[i]);
1182 shortlog_output(&log);
1184 /* We can only do diffstat with a unique reference point */
1186 show_diffstat(rev, origin, head);
1188 if (rev->idiff_oid1) {
1189 fprintf_ln(rev->diffopt.file, "%s", rev->idiff_title);
1190 show_interdiff(rev, 0);
1195 * Pass minimum required diff-options to range-diff; others
1196 * can be added later if deemed desirable.
1198 struct diff_options opts;
1199 struct argv_array other_arg = ARGV_ARRAY_INIT;
1201 opts.file = rev->diffopt.file;
1202 opts.use_color = rev->diffopt.use_color;
1203 diff_setup_done(&opts);
1204 fprintf_ln(rev->diffopt.file, "%s", rev->rdiff_title);
1205 get_notes_args(&other_arg, rev);
1206 show_range_diff(rev->rdiff1, rev->rdiff2,
1207 rev->creation_factor, 1, &opts, &other_arg);
1208 argv_array_clear(&other_arg);
1212 static const char *clean_message_id(const char *msg_id)
1215 const char *a, *z, *m;
1218 while ((ch = *m) && (isspace(ch) || (ch == '<')))
1223 if (!isspace(ch) && (ch != '>'))
1228 die(_("insane in-reply-to: %s"), msg_id);
1231 return xmemdupz(a, z - a);
1234 static const char *set_outdir(const char *prefix, const char *output_directory)
1236 if (output_directory && is_absolute_path(output_directory))
1237 return output_directory;
1239 if (!prefix || !*prefix) {
1240 if (output_directory)
1241 return output_directory;
1242 /* The user did not explicitly ask for "./" */
1247 outdir_offset = strlen(prefix);
1248 if (!output_directory)
1251 return prefix_filename(prefix, output_directory);
1254 static const char * const builtin_format_patch_usage[] = {
1255 N_("git format-patch [<options>] [<since> | <revision-range>]"),
1259 static int keep_subject = 0;
1261 static int keep_callback(const struct option *opt, const char *arg, int unset)
1263 BUG_ON_OPT_NEG(unset);
1264 BUG_ON_OPT_ARG(arg);
1265 ((struct rev_info *)opt->value)->total = -1;
1270 static int subject_prefix = 0;
1272 static int subject_prefix_callback(const struct option *opt, const char *arg,
1275 BUG_ON_OPT_NEG(unset);
1277 ((struct rev_info *)opt->value)->subject_prefix = arg;
1281 static int rfc_callback(const struct option *opt, const char *arg, int unset)
1283 BUG_ON_OPT_NEG(unset);
1284 BUG_ON_OPT_ARG(arg);
1285 return subject_prefix_callback(opt, "RFC PATCH", unset);
1288 static int numbered_cmdline_opt = 0;
1290 static int numbered_callback(const struct option *opt, const char *arg,
1293 BUG_ON_OPT_ARG(arg);
1294 *(int *)opt->value = numbered_cmdline_opt = unset ? 0 : 1;
1300 static int no_numbered_callback(const struct option *opt, const char *arg,
1303 BUG_ON_OPT_NEG(unset);
1304 return numbered_callback(opt, arg, 1);
1307 static int output_directory_callback(const struct option *opt, const char *arg,
1310 const char **dir = (const char **)opt->value;
1311 BUG_ON_OPT_NEG(unset);
1313 die(_("two output directories?"));
1318 static int thread_callback(const struct option *opt, const char *arg, int unset)
1320 enum thread_level *thread = (enum thread_level *)opt->value;
1322 *thread = THREAD_UNSET;
1323 else if (!arg || !strcmp(arg, "shallow"))
1324 *thread = THREAD_SHALLOW;
1325 else if (!strcmp(arg, "deep"))
1326 *thread = THREAD_DEEP;
1328 * Please update _git_formatpatch() in git-completion.bash
1329 * when you add new options.
1336 static int attach_callback(const struct option *opt, const char *arg, int unset)
1338 struct rev_info *rev = (struct rev_info *)opt->value;
1340 rev->mime_boundary = NULL;
1342 rev->mime_boundary = arg;
1344 rev->mime_boundary = git_version_string;
1345 rev->no_inline = unset ? 0 : 1;
1349 static int inline_callback(const struct option *opt, const char *arg, int unset)
1351 struct rev_info *rev = (struct rev_info *)opt->value;
1353 rev->mime_boundary = NULL;
1355 rev->mime_boundary = arg;
1357 rev->mime_boundary = git_version_string;
1362 static int header_callback(const struct option *opt, const char *arg, int unset)
1365 string_list_clear(&extra_hdr, 0);
1366 string_list_clear(&extra_to, 0);
1367 string_list_clear(&extra_cc, 0);
1374 static int to_callback(const struct option *opt, const char *arg, int unset)
1377 string_list_clear(&extra_to, 0);
1379 string_list_append(&extra_to, arg);
1383 static int cc_callback(const struct option *opt, const char *arg, int unset)
1386 string_list_clear(&extra_cc, 0);
1388 string_list_append(&extra_cc, arg);
1392 static int from_callback(const struct option *opt, const char *arg, int unset)
1394 char **from = opt->value;
1401 *from = xstrdup(arg);
1403 *from = xstrdup(git_committer_info(IDENT_NO_DATE));
1407 struct base_tree_info {
1408 struct object_id base_commit;
1409 int nr_patch_id, alloc_patch_id;
1410 struct object_id *patch_id;
1413 static struct commit *get_base_commit(const char *base_commit,
1414 struct commit **list,
1417 struct commit *base = NULL;
1418 struct commit **rev;
1419 int i = 0, rev_nr = 0;
1421 if (base_commit && strcmp(base_commit, "auto")) {
1422 base = lookup_commit_reference_by_name(base_commit);
1424 die(_("unknown commit %s"), base_commit);
1425 } else if ((base_commit && !strcmp(base_commit, "auto"))) {
1426 struct branch *curr_branch = branch_get(NULL);
1427 const char *upstream = branch_get_upstream(curr_branch, NULL);
1429 struct commit_list *base_list;
1430 struct commit *commit;
1431 struct object_id oid;
1433 if (get_oid(upstream, &oid))
1434 die(_("failed to resolve '%s' as a valid ref"), upstream);
1435 commit = lookup_commit_or_die(&oid, "upstream base");
1436 base_list = get_merge_bases_many(commit, total, list);
1437 /* There should be one and only one merge base. */
1438 if (!base_list || base_list->next)
1439 die(_("could not find exact merge base"));
1440 base = base_list->item;
1441 free_commit_list(base_list);
1443 die(_("failed to get upstream, if you want to record base commit automatically,\n"
1444 "please use git branch --set-upstream-to to track a remote branch.\n"
1445 "Or you could specify base commit by --base=<base-commit-id> manually"));
1449 ALLOC_ARRAY(rev, total);
1450 for (i = 0; i < total; i++)
1455 * Get merge base through pair-wise computations
1456 * and store it in rev[0].
1458 while (rev_nr > 1) {
1459 for (i = 0; i < rev_nr / 2; i++) {
1460 struct commit_list *merge_base;
1461 merge_base = get_merge_bases(rev[2 * i], rev[2 * i + 1]);
1462 if (!merge_base || merge_base->next)
1463 die(_("failed to find exact merge base"));
1465 rev[i] = merge_base->item;
1469 rev[i] = rev[2 * i];
1470 rev_nr = DIV_ROUND_UP(rev_nr, 2);
1473 if (!in_merge_bases(base, rev[0]))
1474 die(_("base commit should be the ancestor of revision list"));
1476 for (i = 0; i < total; i++) {
1477 if (base == list[i])
1478 die(_("base commit shouldn't be in revision list"));
1485 define_commit_slab(commit_base, int);
1487 static void prepare_bases(struct base_tree_info *bases,
1488 struct commit *base,
1489 struct commit **list,
1492 struct commit *commit;
1493 struct rev_info revs;
1494 struct diff_options diffopt;
1495 struct commit_base commit_base;
1501 init_commit_base(&commit_base);
1502 repo_diff_setup(the_repository, &diffopt);
1503 diffopt.flags.recursive = 1;
1504 diff_setup_done(&diffopt);
1506 oidcpy(&bases->base_commit, &base->object.oid);
1508 repo_init_revisions(the_repository, &revs, NULL);
1509 revs.max_parents = 1;
1510 revs.topo_order = 1;
1511 for (i = 0; i < total; i++) {
1512 list[i]->object.flags &= ~UNINTERESTING;
1513 add_pending_object(&revs, &list[i]->object, "rev_list");
1514 *commit_base_at(&commit_base, list[i]) = 1;
1516 base->object.flags |= UNINTERESTING;
1517 add_pending_object(&revs, &base->object, "base");
1519 if (prepare_revision_walk(&revs))
1520 die(_("revision walk setup failed"));
1522 * Traverse the commits list, get prerequisite patch ids
1523 * and stuff them in bases structure.
1525 while ((commit = get_revision(&revs)) != NULL) {
1526 struct object_id oid;
1527 struct object_id *patch_id;
1528 if (*commit_base_at(&commit_base, commit))
1530 if (commit_patch_id(commit, &diffopt, &oid, 0, 1))
1531 die(_("cannot get patch id"));
1532 ALLOC_GROW(bases->patch_id, bases->nr_patch_id + 1, bases->alloc_patch_id);
1533 patch_id = bases->patch_id + bases->nr_patch_id;
1534 oidcpy(patch_id, &oid);
1535 bases->nr_patch_id++;
1537 clear_commit_base(&commit_base);
1540 static void print_bases(struct base_tree_info *bases, FILE *file)
1544 /* Only do this once, either for the cover or for the first one */
1545 if (is_null_oid(&bases->base_commit))
1548 /* Show the base commit */
1549 fprintf(file, "\nbase-commit: %s\n", oid_to_hex(&bases->base_commit));
1551 /* Show the prerequisite patches */
1552 for (i = bases->nr_patch_id - 1; i >= 0; i--)
1553 fprintf(file, "prerequisite-patch-id: %s\n", oid_to_hex(&bases->patch_id[i]));
1555 free(bases->patch_id);
1556 bases->nr_patch_id = 0;
1557 bases->alloc_patch_id = 0;
1558 oidclr(&bases->base_commit);
1561 static const char *diff_title(struct strbuf *sb, int reroll_count,
1562 const char *generic, const char *rerolled)
1564 if (reroll_count <= 0)
1565 strbuf_addstr(sb, generic);
1566 else /* RFC may be v0, so allow -v1 to diff against v0 */
1567 strbuf_addf(sb, rerolled, reroll_count - 1);
1571 static void infer_range_diff_ranges(struct strbuf *r1,
1574 struct commit *origin,
1575 struct commit *head)
1577 const char *head_oid = oid_to_hex(&head->object.oid);
1579 if (!strstr(prev, "..")) {
1580 strbuf_addf(r1, "%s..%s", head_oid, prev);
1581 strbuf_addf(r2, "%s..%s", prev, head_oid);
1582 } else if (!origin) {
1583 die(_("failed to infer range-diff ranges"));
1585 strbuf_addstr(r1, prev);
1586 strbuf_addf(r2, "%s..%s",
1587 oid_to_hex(&origin->object.oid), head_oid);
1591 int cmd_format_patch(int argc, const char **argv, const char *prefix)
1593 struct commit *commit;
1594 struct commit **list = NULL;
1595 struct rev_info rev;
1596 struct setup_revision_opt s_r_opt;
1597 int nr = 0, total, i;
1599 int start_number = -1;
1600 int just_numbers = 0;
1601 int ignore_if_in_upstream = 0;
1602 int cover_letter = -1;
1603 int boundary_count = 0;
1604 int no_binary_diff = 0;
1605 int zero_commit = 0;
1606 struct commit *origin = NULL;
1607 const char *in_reply_to = NULL;
1608 struct patch_ids ids;
1609 struct strbuf buf = STRBUF_INIT;
1610 int use_patch_format = 0;
1612 int reroll_count = -1;
1613 char *cover_from_description_arg = NULL;
1614 char *branch_name = NULL;
1615 char *base_commit = NULL;
1616 struct base_tree_info bases;
1617 int show_progress = 0;
1618 struct progress *progress = NULL;
1619 struct oid_array idiff_prev = OID_ARRAY_INIT;
1620 struct strbuf idiff_title = STRBUF_INIT;
1621 const char *rdiff_prev = NULL;
1622 struct strbuf rdiff1 = STRBUF_INIT;
1623 struct strbuf rdiff2 = STRBUF_INIT;
1624 struct strbuf rdiff_title = STRBUF_INIT;
1625 int creation_factor = -1;
1627 const struct option builtin_format_patch_options[] = {
1628 { OPTION_CALLBACK, 'n', "numbered", &numbered, NULL,
1629 N_("use [PATCH n/m] even with a single patch"),
1630 PARSE_OPT_NOARG, numbered_callback },
1631 { OPTION_CALLBACK, 'N', "no-numbered", &numbered, NULL,
1632 N_("use [PATCH] even with multiple patches"),
1633 PARSE_OPT_NOARG | PARSE_OPT_NONEG, no_numbered_callback },
1634 OPT_BOOL('s', "signoff", &do_signoff, N_("add Signed-off-by:")),
1635 OPT_BOOL(0, "stdout", &use_stdout,
1636 N_("print patches to standard out")),
1637 OPT_BOOL(0, "cover-letter", &cover_letter,
1638 N_("generate a cover letter")),
1639 OPT_BOOL(0, "numbered-files", &just_numbers,
1640 N_("use simple number sequence for output file names")),
1641 OPT_STRING(0, "suffix", &fmt_patch_suffix, N_("sfx"),
1642 N_("use <sfx> instead of '.patch'")),
1643 OPT_INTEGER(0, "start-number", &start_number,
1644 N_("start numbering patches at <n> instead of 1")),
1645 OPT_INTEGER('v', "reroll-count", &reroll_count,
1646 N_("mark the series as Nth re-roll")),
1647 { OPTION_CALLBACK, 0, "rfc", &rev, NULL,
1648 N_("Use [RFC PATCH] instead of [PATCH]"),
1649 PARSE_OPT_NOARG | PARSE_OPT_NONEG, rfc_callback },
1650 OPT_STRING(0, "cover-from-description", &cover_from_description_arg,
1651 N_("cover-from-description-mode"),
1652 N_("generate parts of a cover letter based on a branch's description")),
1653 { OPTION_CALLBACK, 0, "subject-prefix", &rev, N_("prefix"),
1654 N_("Use [<prefix>] instead of [PATCH]"),
1655 PARSE_OPT_NONEG, subject_prefix_callback },
1656 { OPTION_CALLBACK, 'o', "output-directory", &output_directory,
1657 N_("dir"), N_("store resulting files in <dir>"),
1658 PARSE_OPT_NONEG, output_directory_callback },
1659 { OPTION_CALLBACK, 'k', "keep-subject", &rev, NULL,
1660 N_("don't strip/add [PATCH]"),
1661 PARSE_OPT_NOARG | PARSE_OPT_NONEG, keep_callback },
1662 OPT_BOOL(0, "no-binary", &no_binary_diff,
1663 N_("don't output binary diffs")),
1664 OPT_BOOL(0, "zero-commit", &zero_commit,
1665 N_("output all-zero hash in From header")),
1666 OPT_BOOL(0, "ignore-if-in-upstream", &ignore_if_in_upstream,
1667 N_("don't include a patch matching a commit upstream")),
1668 OPT_SET_INT_F('p', "no-stat", &use_patch_format,
1669 N_("show patch format instead of default (patch + stat)"),
1670 1, PARSE_OPT_NONEG),
1671 OPT_GROUP(N_("Messaging")),
1672 { OPTION_CALLBACK, 0, "add-header", NULL, N_("header"),
1673 N_("add email header"), 0, header_callback },
1674 { OPTION_CALLBACK, 0, "to", NULL, N_("email"), N_("add To: header"),
1676 { OPTION_CALLBACK, 0, "cc", NULL, N_("email"), N_("add Cc: header"),
1678 { OPTION_CALLBACK, 0, "from", &from, N_("ident"),
1679 N_("set From address to <ident> (or committer ident if absent)"),
1680 PARSE_OPT_OPTARG, from_callback },
1681 OPT_STRING(0, "in-reply-to", &in_reply_to, N_("message-id"),
1682 N_("make first mail a reply to <message-id>")),
1683 { OPTION_CALLBACK, 0, "attach", &rev, N_("boundary"),
1684 N_("attach the patch"), PARSE_OPT_OPTARG,
1686 { OPTION_CALLBACK, 0, "inline", &rev, N_("boundary"),
1687 N_("inline the patch"),
1688 PARSE_OPT_OPTARG | PARSE_OPT_NONEG,
1690 { OPTION_CALLBACK, 0, "thread", &thread, N_("style"),
1691 N_("enable message threading, styles: shallow, deep"),
1692 PARSE_OPT_OPTARG, thread_callback },
1693 OPT_STRING(0, "signature", &signature, N_("signature"),
1694 N_("add a signature")),
1695 OPT_STRING(0, "base", &base_commit, N_("base-commit"),
1696 N_("add prerequisite tree info to the patch series")),
1697 OPT_FILENAME(0, "signature-file", &signature_file,
1698 N_("add a signature from a file")),
1699 OPT__QUIET(&quiet, N_("don't print the patch filenames")),
1700 OPT_BOOL(0, "progress", &show_progress,
1701 N_("show progress while generating patches")),
1702 OPT_CALLBACK(0, "interdiff", &idiff_prev, N_("rev"),
1703 N_("show changes against <rev> in cover letter or single patch"),
1704 parse_opt_object_name),
1705 OPT_STRING(0, "range-diff", &rdiff_prev, N_("refspec"),
1706 N_("show changes against <refspec> in cover letter or single patch")),
1707 OPT_INTEGER(0, "creation-factor", &creation_factor,
1708 N_("percentage by which creation is weighted")),
1712 extra_hdr.strdup_strings = 1;
1713 extra_to.strdup_strings = 1;
1714 extra_cc.strdup_strings = 1;
1715 init_log_defaults();
1716 init_display_notes(¬es_opt);
1717 git_config(git_format_config, NULL);
1718 repo_init_revisions(the_repository, &rev, prefix);
1719 rev.show_notes = show_notes;
1720 memcpy(&rev.notes_opt, ¬es_opt, sizeof(notes_opt));
1721 rev.commit_format = CMIT_FMT_EMAIL;
1722 rev.expand_tabs_in_log_default = 0;
1723 rev.verbose_header = 1;
1725 rev.max_parents = 1;
1726 rev.diffopt.flags.recursive = 1;
1727 rev.subject_prefix = fmt_patch_subject_prefix;
1728 memset(&s_r_opt, 0, sizeof(s_r_opt));
1729 s_r_opt.def = "HEAD";
1730 s_r_opt.revarg_opt = REVARG_COMMITTISH;
1733 base_commit = "auto";
1735 if (default_attach) {
1736 rev.mime_boundary = default_attach;
1741 * Parse the arguments before setup_revisions(), or something
1742 * like "git format-patch -o a123 HEAD^.." may fail; a123 is
1743 * possibly a valid SHA1.
1745 argc = parse_options(argc, argv, prefix, builtin_format_patch_options,
1746 builtin_format_patch_usage,
1747 PARSE_OPT_KEEP_ARGV0 | PARSE_OPT_KEEP_UNKNOWN |
1748 PARSE_OPT_KEEP_DASHDASH);
1750 if (cover_from_description_arg)
1751 cover_from_description_mode = parse_cover_from_description(cover_from_description_arg);
1753 if (0 < reroll_count) {
1754 struct strbuf sprefix = STRBUF_INIT;
1755 strbuf_addf(&sprefix, "%s v%d",
1756 rev.subject_prefix, reroll_count);
1757 rev.reroll_count = reroll_count;
1758 rev.subject_prefix = strbuf_detach(&sprefix, NULL);
1761 for (i = 0; i < extra_hdr.nr; i++) {
1762 strbuf_addstr(&buf, extra_hdr.items[i].string);
1763 strbuf_addch(&buf, '\n');
1767 strbuf_addstr(&buf, "To: ");
1768 for (i = 0; i < extra_to.nr; i++) {
1770 strbuf_addstr(&buf, " ");
1771 strbuf_addstr(&buf, extra_to.items[i].string);
1772 if (i + 1 < extra_to.nr)
1773 strbuf_addch(&buf, ',');
1774 strbuf_addch(&buf, '\n');
1778 strbuf_addstr(&buf, "Cc: ");
1779 for (i = 0; i < extra_cc.nr; i++) {
1781 strbuf_addstr(&buf, " ");
1782 strbuf_addstr(&buf, extra_cc.items[i].string);
1783 if (i + 1 < extra_cc.nr)
1784 strbuf_addch(&buf, ',');
1785 strbuf_addch(&buf, '\n');
1788 rev.extra_headers = strbuf_detach(&buf, NULL);
1791 if (split_ident_line(&rev.from_ident, from, strlen(from)))
1792 die(_("invalid ident line: %s"), from);
1795 if (start_number < 0)
1799 * If numbered is set solely due to format.numbered in config,
1800 * and it would conflict with --keep-subject (-k) from the
1801 * command line, reset "numbered".
1803 if (numbered && keep_subject && !numbered_cmdline_opt)
1806 if (numbered && keep_subject)
1807 die(_("-n and -k are mutually exclusive"));
1808 if (keep_subject && subject_prefix)
1809 die(_("--subject-prefix/--rfc and -k are mutually exclusive"));
1810 rev.preserve_subject = keep_subject;
1812 argc = setup_revisions(argc, argv, &rev, &s_r_opt);
1814 die(_("unrecognized argument: %s"), argv[1]);
1816 if (rev.diffopt.output_format & DIFF_FORMAT_NAME)
1817 die(_("--name-only does not make sense"));
1818 if (rev.diffopt.output_format & DIFF_FORMAT_NAME_STATUS)
1819 die(_("--name-status does not make sense"));
1820 if (rev.diffopt.output_format & DIFF_FORMAT_CHECKDIFF)
1821 die(_("--check does not make sense"));
1823 if (!use_patch_format &&
1824 (!rev.diffopt.output_format ||
1825 rev.diffopt.output_format == DIFF_FORMAT_PATCH))
1826 rev.diffopt.output_format = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_SUMMARY;
1827 if (!rev.diffopt.stat_width)
1828 rev.diffopt.stat_width = MAIL_DEFAULT_WRAP;
1830 /* Always generate a patch */
1831 rev.diffopt.output_format |= DIFF_FORMAT_PATCH;
1833 rev.zero_commit = zero_commit;
1835 if (!rev.diffopt.flags.text && !no_binary_diff)
1836 rev.diffopt.flags.binary = 1;
1839 load_display_notes(&rev.notes_opt);
1841 if (!output_directory && !use_stdout)
1842 output_directory = config_output_directory;
1845 output_directory = set_outdir(prefix, output_directory);
1849 if (output_directory) {
1851 if (rev.diffopt.use_color != GIT_COLOR_ALWAYS)
1852 rev.diffopt.use_color = GIT_COLOR_NEVER;
1854 die(_("standard output, or directory, which one?"));
1856 * We consider <outdir> as 'outside of gitdir', therefore avoid
1857 * applying adjust_shared_perm in s-c-l-d.
1859 saved = get_shared_repository();
1860 set_shared_repository(0);
1861 switch (safe_create_leading_directories_const(output_directory)) {
1866 die(_("could not create leading directories "
1867 "of '%s'"), output_directory);
1869 set_shared_repository(saved);
1870 if (mkdir(output_directory, 0777) < 0 && errno != EEXIST)
1871 die_errno(_("could not create directory '%s'"),
1875 if (rev.pending.nr == 1) {
1878 if (rev.max_count < 0 && !rev.show_root_diff) {
1880 * This is traditional behaviour of "git format-patch
1881 * origin" that prepares what the origin side still
1884 rev.pending.objects[0].item->flags |= UNINTERESTING;
1885 add_head_to_pending(&rev);
1889 * Otherwise, it is "format-patch -22 HEAD", and/or
1890 * "format-patch --root HEAD". The user wants
1891 * get_revision() to do the usual traversal.
1894 if (!strcmp(rev.pending.objects[0].name, "HEAD"))
1898 const char *ref, *v;
1899 ref = resolve_ref_unsafe("HEAD", RESOLVE_REF_READING,
1901 if (ref && skip_prefix(ref, "refs/heads/", &v))
1902 branch_name = xstrdup(v);
1904 branch_name = xstrdup(""); /* no branch */
1909 * We cannot move this anywhere earlier because we do want to
1910 * know if --root was given explicitly from the command line.
1912 rev.show_root_diff = 1;
1914 if (ignore_if_in_upstream) {
1915 /* Don't say anything if head and upstream are the same. */
1916 if (rev.pending.nr == 2) {
1917 struct object_array_entry *o = rev.pending.objects;
1918 if (oideq(&o[0].item->oid, &o[1].item->oid))
1921 get_patch_ids(&rev, &ids);
1924 if (prepare_revision_walk(&rev))
1925 die(_("revision walk setup failed"));
1927 while ((commit = get_revision(&rev)) != NULL) {
1928 if (commit->object.flags & BOUNDARY) {
1930 origin = (boundary_count == 1) ? commit : NULL;
1934 if (ignore_if_in_upstream && has_commit_patch_id(commit, &ids))
1938 REALLOC_ARRAY(list, nr);
1939 list[nr - 1] = commit;
1945 if (cover_letter == -1) {
1946 if (config_cover_letter == COVER_AUTO)
1947 cover_letter = (total > 1);
1949 cover_letter = (config_cover_letter == COVER_ON);
1951 if (!keep_subject && auto_number && (total > 1 || cover_letter))
1954 rev.total = total + start_number - 1;
1956 if (idiff_prev.nr) {
1957 if (!cover_letter && total != 1)
1958 die(_("--interdiff requires --cover-letter or single patch"));
1959 rev.idiff_oid1 = &idiff_prev.oid[idiff_prev.nr - 1];
1960 rev.idiff_oid2 = get_commit_tree_oid(list[0]);
1961 rev.idiff_title = diff_title(&idiff_title, reroll_count,
1963 _("Interdiff against v%d:"));
1966 if (creation_factor < 0)
1967 creation_factor = RANGE_DIFF_CREATION_FACTOR_DEFAULT;
1968 else if (!rdiff_prev)
1969 die(_("--creation-factor requires --range-diff"));
1972 if (!cover_letter && total != 1)
1973 die(_("--range-diff requires --cover-letter or single patch"));
1975 infer_range_diff_ranges(&rdiff1, &rdiff2, rdiff_prev,
1977 rev.rdiff1 = rdiff1.buf;
1978 rev.rdiff2 = rdiff2.buf;
1979 rev.creation_factor = creation_factor;
1980 rev.rdiff_title = diff_title(&rdiff_title, reroll_count,
1982 _("Range-diff against v%d:"));
1986 ; /* --no-signature inhibits all signatures */
1987 } else if (signature && signature != git_version_string) {
1988 ; /* non-default signature already set */
1989 } else if (signature_file) {
1990 struct strbuf buf = STRBUF_INIT;
1992 if (strbuf_read_file(&buf, signature_file, 128) < 0)
1993 die_errno(_("unable to read signature file '%s'"), signature_file);
1994 signature = strbuf_detach(&buf, NULL);
1997 memset(&bases, 0, sizeof(bases));
1999 struct commit *base = get_base_commit(base_commit, list, nr);
2000 reset_revision_walk();
2001 clear_object_flags(UNINTERESTING);
2002 prepare_bases(&bases, base, list, nr);
2005 if (in_reply_to || thread || cover_letter)
2006 rev.ref_message_ids = xcalloc(1, sizeof(struct string_list));
2008 const char *msgid = clean_message_id(in_reply_to);
2009 string_list_append(rev.ref_message_ids, msgid);
2011 rev.numbered_files = just_numbers;
2012 rev.patch_suffix = fmt_patch_suffix;
2015 gen_message_id(&rev, "cover");
2016 make_cover_letter(&rev, use_stdout,
2017 origin, nr, list, branch_name, quiet);
2018 print_bases(&bases, rev.diffopt.file);
2019 print_signature(rev.diffopt.file);
2022 /* interdiff/range-diff in cover-letter; omit from patches */
2023 rev.idiff_oid1 = NULL;
2026 rev.add_signoff = do_signoff;
2029 progress = start_delayed_progress(_("Generating patches"), total);
2032 display_progress(progress, total - nr);
2034 rev.nr = total - nr + (start_number - 1);
2035 /* Make the second and subsequent mails replies to the first */
2037 /* Have we already had a message ID? */
2038 if (rev.message_id) {
2040 * For deep threading: make every mail
2041 * a reply to the previous one, no
2042 * matter what other options are set.
2044 * For shallow threading:
2046 * Without --cover-letter and
2047 * --in-reply-to, make every mail a
2048 * reply to the one before.
2050 * With --in-reply-to but no
2051 * --cover-letter, make every mail a
2052 * reply to the <reply-to>.
2054 * With --cover-letter, make every
2055 * mail but the cover letter a reply
2056 * to the cover letter. The cover
2057 * letter is a reply to the
2058 * --in-reply-to, if specified.
2060 if (thread == THREAD_SHALLOW
2061 && rev.ref_message_ids->nr > 0
2062 && (!cover_letter || rev.nr > 1))
2063 free(rev.message_id);
2065 string_list_append(rev.ref_message_ids,
2068 gen_message_id(&rev, oid_to_hex(&commit->object.oid));
2072 open_next_file(rev.numbered_files ? NULL : commit, NULL, &rev, quiet))
2073 die(_("failed to create output files"));
2074 shown = log_tree_commit(&rev, commit);
2075 free_commit_buffer(the_repository->parsed_objects,
2078 /* We put one extra blank line between formatted
2079 * patches and this flag is used by log-tree code
2080 * to see if it needs to emit a LF before showing
2081 * the log; when using one file per patch, we do
2082 * not want the extra blank line.
2087 print_bases(&bases, rev.diffopt.file);
2088 if (rev.mime_boundary)
2089 fprintf(rev.diffopt.file, "\n--%s%s--\n\n\n",
2090 mime_boundary_leader,
2093 print_signature(rev.diffopt.file);
2096 fclose(rev.diffopt.file);
2098 stop_progress(&progress);
2101 string_list_clear(&extra_to, 0);
2102 string_list_clear(&extra_cc, 0);
2103 string_list_clear(&extra_hdr, 0);
2104 if (ignore_if_in_upstream)
2105 free_patch_ids(&ids);
2108 oid_array_clear(&idiff_prev);
2109 strbuf_release(&idiff_title);
2110 strbuf_release(&rdiff1);
2111 strbuf_release(&rdiff2);
2112 strbuf_release(&rdiff_title);
2116 static int add_pending_commit(const char *arg, struct rev_info *revs, int flags)
2118 struct object_id oid;
2119 if (get_oid(arg, &oid) == 0) {
2120 struct commit *commit = lookup_commit_reference(the_repository,
2123 commit->object.flags |= flags;
2124 add_pending_object(revs, &commit->object, arg);
2131 static const char * const cherry_usage[] = {
2132 N_("git cherry [-v] [<upstream> [<head> [<limit>]]]"),
2136 static void print_commit(char sign, struct commit *commit, int verbose,
2137 int abbrev, FILE *file)
2140 fprintf(file, "%c %s\n", sign,
2141 find_unique_abbrev(&commit->object.oid, abbrev));
2143 struct strbuf buf = STRBUF_INIT;
2144 pp_commit_easy(CMIT_FMT_ONELINE, commit, &buf);
2145 fprintf(file, "%c %s %s\n", sign,
2146 find_unique_abbrev(&commit->object.oid, abbrev),
2148 strbuf_release(&buf);
2152 int cmd_cherry(int argc, const char **argv, const char *prefix)
2154 struct rev_info revs;
2155 struct patch_ids ids;
2156 struct commit *commit;
2157 struct commit_list *list = NULL;
2158 struct branch *current_branch;
2159 const char *upstream;
2160 const char *head = "HEAD";
2161 const char *limit = NULL;
2162 int verbose = 0, abbrev = 0;
2164 struct option options[] = {
2165 OPT__ABBREV(&abbrev),
2166 OPT__VERBOSE(&verbose, N_("be verbose")),
2170 argc = parse_options(argc, argv, prefix, options, cherry_usage, 0);
2183 current_branch = branch_get(NULL);
2184 upstream = branch_get_upstream(current_branch, NULL);
2186 fprintf(stderr, _("Could not find a tracked"
2187 " remote branch, please"
2188 " specify <upstream> manually.\n"));
2189 usage_with_options(cherry_usage, options);
2193 repo_init_revisions(the_repository, &revs, prefix);
2194 revs.max_parents = 1;
2196 if (add_pending_commit(head, &revs, 0))
2197 die(_("unknown commit %s"), head);
2198 if (add_pending_commit(upstream, &revs, UNINTERESTING))
2199 die(_("unknown commit %s"), upstream);
2201 /* Don't say anything if head and upstream are the same. */
2202 if (revs.pending.nr == 2) {
2203 struct object_array_entry *o = revs.pending.objects;
2204 if (oideq(&o[0].item->oid, &o[1].item->oid))
2208 get_patch_ids(&revs, &ids);
2210 if (limit && add_pending_commit(limit, &revs, UNINTERESTING))
2211 die(_("unknown commit %s"), limit);
2213 /* reverse the list of commits */
2214 if (prepare_revision_walk(&revs))
2215 die(_("revision walk setup failed"));
2216 while ((commit = get_revision(&revs)) != NULL) {
2217 commit_list_insert(commit, &list);
2223 commit = list->item;
2224 if (has_commit_patch_id(commit, &ids))
2226 print_commit(sign, commit, verbose, abbrev, revs.diffopt.file);
2230 free_patch_ids(&ids);