2 * Builtin "git log" and related commands (show, whatchanged)
4 * (C) Copyright 2006 Linus Torvalds
15 #include "reflog-walk.h"
16 #include "patch-ids.h"
17 #include "run-command.h"
20 #include "string-list.h"
21 #include "parse-options.h"
23 /* Set a default date-time format for git log ("log.date" config variable) */
24 static const char *default_date_mode = NULL;
26 static int default_show_root = 1;
27 static int decoration_style;
28 static int decoration_given;
29 static const char *fmt_patch_subject_prefix = "PATCH";
30 static const char *fmt_pretty;
32 static const char * const builtin_log_usage[] = {
33 "git log [<options>] [<since>..<until>] [[--] <path>...]\n"
34 " or: git show [options] <object>...",
38 static int parse_decoration_style(const char *var, const char *value)
40 switch (git_config_maybe_bool(var, value)) {
42 return DECORATE_SHORT_REFS;
48 if (!strcmp(value, "full"))
49 return DECORATE_FULL_REFS;
50 else if (!strcmp(value, "short"))
51 return DECORATE_SHORT_REFS;
55 static int decorate_callback(const struct option *opt, const char *arg, int unset)
60 decoration_style = parse_decoration_style("command line", arg);
62 decoration_style = DECORATE_SHORT_REFS;
64 if (decoration_style < 0)
65 die("invalid --decorate option: %s", arg);
72 static void cmd_log_init(int argc, const char **argv, const char *prefix,
73 struct rev_info *rev, struct setup_revision_opt *opt)
75 struct userformat_want w;
76 int quiet = 0, source = 0;
78 const struct option builtin_log_options[] = {
79 OPT_BOOLEAN(0, "quiet", &quiet, "supress diff output"),
80 OPT_BOOLEAN(0, "source", &source, "show source"),
81 { OPTION_CALLBACK, 0, "decorate", NULL, NULL, "decorate options",
82 PARSE_OPT_OPTARG, decorate_callback},
86 argc = parse_options(argc, argv, prefix,
87 builtin_log_options, builtin_log_usage,
88 PARSE_OPT_KEEP_ARGV0 | PARSE_OPT_KEEP_UNKNOWN |
89 PARSE_OPT_KEEP_DASHDASH);
91 rev->abbrev = DEFAULT_ABBREV;
92 rev->commit_format = CMIT_FMT_DEFAULT;
94 get_commit_format(fmt_pretty, rev);
95 rev->verbose_header = 1;
96 DIFF_OPT_SET(&rev->diffopt, RECURSIVE);
97 rev->show_root_diff = default_show_root;
98 rev->subject_prefix = fmt_patch_subject_prefix;
99 DIFF_OPT_SET(&rev->diffopt, ALLOW_TEXTCONV);
101 if (default_date_mode)
102 rev->date_mode = parse_date_format(default_date_mode);
104 argc = setup_revisions(argc, argv, rev, opt);
106 rev->diffopt.output_format |= DIFF_FORMAT_NO_OUTPUT;
108 /* Any arguments at this point are not recognized */
110 die("unrecognized argument: %s", argv[1]);
112 memset(&w, 0, sizeof(w));
113 userformat_find_requirements(NULL, &w);
115 if (!rev->show_notes_given && (!rev->pretty_given || w.notes))
118 init_display_notes(&rev->notes_opt);
120 if (rev->diffopt.pickaxe || rev->diffopt.filter)
121 rev->always_show_header = 0;
122 if (DIFF_OPT_TST(&rev->diffopt, FOLLOW_RENAMES)) {
123 rev->always_show_header = 0;
124 if (rev->diffopt.nr_paths != 1)
125 usage("git logs can only follow renames on one pathname at a time");
129 rev->show_source = 1;
132 * defeat log.decorate configuration interacting with --pretty=raw
133 * from the command line.
135 if (!decoration_given && rev->pretty_given
136 && rev->commit_format == CMIT_FMT_RAW)
137 decoration_style = 0;
139 if (decoration_style) {
140 rev->show_decorations = 1;
141 load_ref_decorations(decoration_style);
147 * This gives a rough estimate for how many commits we
148 * will print out in the list.
150 static int estimate_commit_count(struct rev_info *rev, struct commit_list *list)
155 struct commit *commit = list->item;
156 unsigned int flags = commit->object.flags;
158 if (!(flags & (TREESAME | UNINTERESTING)))
164 static void show_early_header(struct rev_info *rev, const char *stage, int nr)
166 if (rev->shown_one) {
168 if (rev->commit_format != CMIT_FMT_ONELINE)
169 putchar(rev->diffopt.line_termination);
171 printf("Final output: %d %s\n", nr, stage);
174 static struct itimerval early_output_timer;
176 static void log_show_early(struct rev_info *revs, struct commit_list *list)
178 int i = revs->early_output;
181 sort_in_topological_order(&list, revs->lifo);
183 struct commit *commit = list->item;
184 switch (simplify_commit(revs, commit)) {
187 int n = estimate_commit_count(revs, list);
188 show_early_header(revs, "incomplete", n);
191 log_tree_commit(revs, commit);
202 /* Did we already get enough commits for the early output? */
207 * ..if no, then repeat it twice a second until we
210 * NOTE! We don't use "it_interval", because if the
211 * reader isn't listening, we want our output to be
212 * throttled by the writing, and not have the timer
213 * trigger every second even if we're blocked on a
216 early_output_timer.it_value.tv_sec = 0;
217 early_output_timer.it_value.tv_usec = 500000;
218 setitimer(ITIMER_REAL, &early_output_timer, NULL);
221 static void early_output(int signal)
223 show_early_output = log_show_early;
226 static void setup_early_output(struct rev_info *rev)
231 * Set up the signal handler, minimally intrusively:
232 * we only set a single volatile integer word (not
233 * using sigatomic_t - trying to avoid unnecessary
234 * system dependencies and headers), and using
237 memset(&sa, 0, sizeof(sa));
238 sa.sa_handler = early_output;
239 sigemptyset(&sa.sa_mask);
240 sa.sa_flags = SA_RESTART;
241 sigaction(SIGALRM, &sa, NULL);
244 * If we can get the whole output in less than a
245 * tenth of a second, don't even bother doing the
246 * early-output thing..
248 * This is a one-time-only trigger.
250 early_output_timer.it_value.tv_sec = 0;
251 early_output_timer.it_value.tv_usec = 100000;
252 setitimer(ITIMER_REAL, &early_output_timer, NULL);
255 static void finish_early_output(struct rev_info *rev)
257 int n = estimate_commit_count(rev, rev->commits);
258 signal(SIGALRM, SIG_IGN);
259 show_early_header(rev, "done", n);
262 static int cmd_log_walk(struct rev_info *rev)
264 struct commit *commit;
266 if (rev->early_output)
267 setup_early_output(rev);
269 if (prepare_revision_walk(rev))
270 die("revision walk setup failed");
272 if (rev->early_output)
273 finish_early_output(rev);
276 * For --check and --exit-code, the exit code is based on CHECK_FAILED
277 * and HAS_CHANGES being accumulated in rev->diffopt, so be careful to
278 * retain that state information if replacing rev->diffopt in this loop
280 while ((commit = get_revision(rev)) != NULL) {
281 if (!log_tree_commit(rev, commit) &&
284 * We decremented max_count in get_revision,
285 * but we didn't actually show the commit.
288 if (!rev->reflog_info) {
289 /* we allow cycles in reflog ancestry */
290 free(commit->buffer);
291 commit->buffer = NULL;
293 free_commit_list(commit->parents);
294 commit->parents = NULL;
296 if (rev->diffopt.output_format & DIFF_FORMAT_CHECKDIFF &&
297 DIFF_OPT_TST(&rev->diffopt, CHECK_FAILED)) {
300 return diff_result_code(&rev->diffopt, 0);
303 static int git_log_config(const char *var, const char *value, void *cb)
305 if (!strcmp(var, "format.pretty"))
306 return git_config_string(&fmt_pretty, var, value);
307 if (!strcmp(var, "format.subjectprefix"))
308 return git_config_string(&fmt_patch_subject_prefix, var, value);
309 if (!strcmp(var, "log.date"))
310 return git_config_string(&default_date_mode, var, value);
311 if (!strcmp(var, "log.decorate")) {
312 decoration_style = parse_decoration_style(var, value);
313 if (decoration_style < 0)
314 decoration_style = 0; /* maybe warn? */
317 if (!strcmp(var, "log.showroot")) {
318 default_show_root = git_config_bool(var, value);
321 if (!prefixcmp(var, "color.decorate."))
322 return parse_decorate_color_config(var, 15, value);
324 return git_diff_ui_config(var, value, cb);
327 int cmd_whatchanged(int argc, const char **argv, const char *prefix)
330 struct setup_revision_opt opt;
332 git_config(git_log_config, NULL);
334 if (diff_use_color_default == -1)
335 diff_use_color_default = git_use_color_default;
337 init_revisions(&rev, prefix);
339 rev.simplify_history = 0;
340 memset(&opt, 0, sizeof(opt));
342 cmd_log_init(argc, argv, prefix, &rev, &opt);
343 if (!rev.diffopt.output_format)
344 rev.diffopt.output_format = DIFF_FORMAT_RAW;
345 return cmd_log_walk(&rev);
348 static void show_tagger(char *buf, int len, struct rev_info *rev)
350 struct strbuf out = STRBUF_INIT;
352 pp_user_info("Tagger", rev->commit_format, &out, buf, rev->date_mode,
353 get_log_output_encoding());
354 printf("%s", out.buf);
355 strbuf_release(&out);
358 static int show_object(const unsigned char *sha1, int show_tag_object,
359 struct rev_info *rev)
362 enum object_type type;
363 char *buf = read_sha1_file(sha1, &type, &size);
367 return error("Could not read object %s", sha1_to_hex(sha1));
370 while (offset < size && buf[offset] != '\n') {
371 int new_offset = offset + 1;
372 while (new_offset < size && buf[new_offset++] != '\n')
374 if (!prefixcmp(buf + offset, "tagger "))
375 show_tagger(buf + offset + 7,
376 new_offset - offset - 7, rev);
381 fwrite(buf + offset, size - offset, 1, stdout);
386 static int show_tree_object(const unsigned char *sha1,
387 const char *base, int baselen,
388 const char *pathname, unsigned mode, int stage, void *context)
390 printf("%s%s\n", pathname, S_ISDIR(mode) ? "/" : "");
394 static void show_rev_tweak_rev(struct rev_info *rev, struct setup_revision_opt *opt)
396 if (rev->ignore_merges) {
397 /* There was no "-m" on the command line */
398 rev->ignore_merges = 0;
399 if (!rev->first_parent_only && !rev->combine_merges) {
400 /* No "--first-parent", "-c", nor "--cc" */
401 rev->combine_merges = 1;
402 rev->dense_combined_merges = 1;
405 if (!rev->diffopt.output_format)
406 rev->diffopt.output_format = DIFF_FORMAT_PATCH;
409 int cmd_show(int argc, const char **argv, const char *prefix)
412 struct object_array_entry *objects;
413 struct setup_revision_opt opt;
414 int i, count, ret = 0;
416 git_config(git_log_config, NULL);
418 if (diff_use_color_default == -1)
419 diff_use_color_default = git_use_color_default;
421 init_revisions(&rev, prefix);
423 rev.always_show_header = 1;
425 memset(&opt, 0, sizeof(opt));
427 opt.tweak = show_rev_tweak_rev;
428 cmd_log_init(argc, argv, prefix, &rev, &opt);
430 count = rev.pending.nr;
431 objects = rev.pending.objects;
432 for (i = 0; i < count && !ret; i++) {
433 struct object *o = objects[i].item;
434 const char *name = objects[i].name;
437 ret = show_object(o->sha1, 0, NULL);
440 struct tag *t = (struct tag *)o;
444 printf("%stag %s%s\n",
445 diff_get_color_opt(&rev.diffopt, DIFF_COMMIT),
447 diff_get_color_opt(&rev.diffopt, DIFF_RESET));
448 ret = show_object(o->sha1, 1, &rev);
452 o = parse_object(t->tagged->sha1);
454 ret = error("Could not read object %s",
455 sha1_to_hex(t->tagged->sha1));
463 printf("%stree %s%s\n\n",
464 diff_get_color_opt(&rev.diffopt, DIFF_COMMIT),
466 diff_get_color_opt(&rev.diffopt, DIFF_RESET));
467 read_tree_recursive((struct tree *)o, "", 0, 0, NULL,
468 show_tree_object, NULL);
472 rev.pending.nr = rev.pending.alloc = 0;
473 rev.pending.objects = NULL;
474 add_object_array(o, name, &rev.pending);
475 ret = cmd_log_walk(&rev);
478 ret = error("Unknown type: %d", o->type);
486 * This is equivalent to "git log -g --abbrev-commit --pretty=oneline"
488 int cmd_log_reflog(int argc, const char **argv, const char *prefix)
491 struct setup_revision_opt opt;
493 git_config(git_log_config, NULL);
495 if (diff_use_color_default == -1)
496 diff_use_color_default = git_use_color_default;
498 init_revisions(&rev, prefix);
499 init_reflog_walk(&rev.reflog_info);
500 rev.abbrev_commit = 1;
501 rev.verbose_header = 1;
502 memset(&opt, 0, sizeof(opt));
504 cmd_log_init(argc, argv, prefix, &rev, &opt);
507 * This means that we override whatever commit format the user gave
508 * on the cmd line. Sad, but cmd_log_init() currently doesn't
509 * allow us to set a different default.
511 rev.commit_format = CMIT_FMT_ONELINE;
512 rev.use_terminator = 1;
513 rev.always_show_header = 1;
515 return cmd_log_walk(&rev);
518 int cmd_log(int argc, const char **argv, const char *prefix)
521 struct setup_revision_opt opt;
523 git_config(git_log_config, NULL);
525 if (diff_use_color_default == -1)
526 diff_use_color_default = git_use_color_default;
528 init_revisions(&rev, prefix);
529 rev.always_show_header = 1;
530 memset(&opt, 0, sizeof(opt));
532 cmd_log_init(argc, argv, prefix, &rev, &opt);
533 return cmd_log_walk(&rev);
538 static const char *fmt_patch_suffix = ".patch";
539 static int numbered = 0;
540 static int auto_number = 1;
542 static char *default_attach = NULL;
544 static struct string_list extra_hdr;
545 static struct string_list extra_to;
546 static struct string_list extra_cc;
548 static void add_header(const char *value)
550 struct string_list_item *item;
551 int len = strlen(value);
552 while (len && value[len - 1] == '\n')
555 if (!strncasecmp(value, "to: ", 4)) {
556 item = string_list_append(&extra_to, value + 4);
558 } else if (!strncasecmp(value, "cc: ", 4)) {
559 item = string_list_append(&extra_cc, value + 4);
562 item = string_list_append(&extra_hdr, value);
565 item->string[len] = '\0';
568 #define THREAD_SHALLOW 1
569 #define THREAD_DEEP 2
571 static int do_signoff;
572 static const char *signature = git_version_string;
574 static int git_format_config(const char *var, const char *value, void *cb)
576 if (!strcmp(var, "format.headers")) {
578 die("format.headers without value");
582 if (!strcmp(var, "format.suffix"))
583 return git_config_string(&fmt_patch_suffix, var, value);
584 if (!strcmp(var, "format.to")) {
586 return config_error_nonbool(var);
587 string_list_append(&extra_to, value);
590 if (!strcmp(var, "format.cc")) {
592 return config_error_nonbool(var);
593 string_list_append(&extra_cc, value);
596 if (!strcmp(var, "diff.color") || !strcmp(var, "color.diff")) {
599 if (!strcmp(var, "format.numbered")) {
600 if (value && !strcasecmp(value, "auto")) {
604 numbered = git_config_bool(var, value);
605 auto_number = auto_number && numbered;
608 if (!strcmp(var, "format.attach")) {
610 default_attach = xstrdup(value);
612 default_attach = xstrdup(git_version_string);
615 if (!strcmp(var, "format.thread")) {
616 if (value && !strcasecmp(value, "deep")) {
617 thread = THREAD_DEEP;
620 if (value && !strcasecmp(value, "shallow")) {
621 thread = THREAD_SHALLOW;
624 thread = git_config_bool(var, value) && THREAD_SHALLOW;
627 if (!strcmp(var, "format.signoff")) {
628 do_signoff = git_config_bool(var, value);
631 if (!strcmp(var, "format.signature"))
632 return git_config_string(&signature, var, value);
634 return git_log_config(var, value, cb);
637 static FILE *realstdout = NULL;
638 static const char *output_directory = NULL;
639 static int outdir_offset;
641 static int reopen_stdout(struct commit *commit, struct rev_info *rev)
643 struct strbuf filename = STRBUF_INIT;
644 int suffix_len = strlen(fmt_patch_suffix) + 1;
646 if (output_directory) {
647 strbuf_addstr(&filename, output_directory);
649 PATH_MAX - FORMAT_PATCH_NAME_MAX - suffix_len)
650 return error("name of output directory is too long");
651 if (filename.buf[filename.len - 1] != '/')
652 strbuf_addch(&filename, '/');
655 get_patch_filename(commit, rev->nr, fmt_patch_suffix, &filename);
657 if (!DIFF_OPT_TST(&rev->diffopt, QUICK))
658 fprintf(realstdout, "%s\n", filename.buf + outdir_offset);
660 if (freopen(filename.buf, "w", stdout) == NULL)
661 return error("Cannot open patch file %s", filename.buf);
663 strbuf_release(&filename);
667 static void get_patch_ids(struct rev_info *rev, struct patch_ids *ids, const char *prefix)
669 struct rev_info check_rev;
670 struct commit *commit;
671 struct object *o1, *o2;
672 unsigned flags1, flags2;
674 if (rev->pending.nr != 2)
675 die("Need exactly one range.");
677 o1 = rev->pending.objects[0].item;
679 o2 = rev->pending.objects[1].item;
682 if ((flags1 & UNINTERESTING) == (flags2 & UNINTERESTING))
687 /* given a range a..b get all patch ids for b..a */
688 init_revisions(&check_rev, prefix);
689 o1->flags ^= UNINTERESTING;
690 o2->flags ^= UNINTERESTING;
691 add_pending_object(&check_rev, o1, "o1");
692 add_pending_object(&check_rev, o2, "o2");
693 if (prepare_revision_walk(&check_rev))
694 die("revision walk setup failed");
696 while ((commit = get_revision(&check_rev)) != NULL) {
698 if (commit->parents && commit->parents->next)
701 add_commit_patch_id(commit, ids);
704 /* reset for next revision walk */
705 clear_commit_marks((struct commit *)o1,
706 SEEN | UNINTERESTING | SHOWN | ADDED);
707 clear_commit_marks((struct commit *)o2,
708 SEEN | UNINTERESTING | SHOWN | ADDED);
713 static void gen_message_id(struct rev_info *info, char *base)
715 const char *committer = git_committer_info(IDENT_WARN_ON_NO_NAME);
716 const char *email_start = strrchr(committer, '<');
717 const char *email_end = strrchr(committer, '>');
718 struct strbuf buf = STRBUF_INIT;
719 if (!email_start || !email_end || email_start > email_end - 1)
720 die("Could not extract email from committer identity.");
721 strbuf_addf(&buf, "%s.%lu.git.%.*s", base,
722 (unsigned long) time(NULL),
723 (int)(email_end - email_start - 1), email_start + 1);
724 info->message_id = strbuf_detach(&buf, NULL);
727 static void print_signature(void)
729 if (signature && *signature)
730 printf("-- \n%s\n\n", signature);
733 static void make_cover_letter(struct rev_info *rev, int use_stdout,
734 int numbered, int numbered_files,
735 struct commit *origin,
736 int nr, struct commit **list, struct commit *head)
738 const char *committer;
739 const char *subject_start = NULL;
740 const char *body = "*** SUBJECT HERE ***\n\n*** BLURB HERE ***\n";
742 const char *extra_headers = rev->extra_headers;
744 struct strbuf sb = STRBUF_INIT;
746 const char *encoding = "UTF-8";
747 struct diff_options opts;
748 int need_8bit_cte = 0;
749 struct commit *commit = NULL;
751 if (rev->commit_format != CMIT_FMT_EMAIL)
752 die("Cover letter needs email format");
754 committer = git_committer_info(0);
756 if (!numbered_files) {
758 * We fake a commit for the cover letter so we get the filename
761 commit = xcalloc(1, sizeof(*commit));
762 commit->buffer = xmalloc(400);
763 snprintf(commit->buffer, 400,
764 "tree 0000000000000000000000000000000000000000\n"
769 sha1_to_hex(head->object.sha1), committer, committer);
772 if (!use_stdout && reopen_stdout(commit, rev))
777 free(commit->buffer);
781 log_write_email_headers(rev, head, &subject_start, &extra_headers,
784 for (i = 0; !need_8bit_cte && i < nr; i++)
785 if (has_non_ascii(list[i]->buffer))
789 pp_user_info(NULL, CMIT_FMT_EMAIL, &sb, committer, DATE_RFC2822,
791 pp_title_line(CMIT_FMT_EMAIL, &msg, &sb, subject_start, extra_headers,
792 encoding, need_8bit_cte);
793 pp_remainder(CMIT_FMT_EMAIL, &msg, &sb, 0);
794 printf("%s\n", sb.buf);
803 for (i = 0; i < nr; i++)
804 shortlog_add_commit(&log, list[i]);
806 shortlog_output(&log);
809 * We can only do diffstat with a unique reference point
814 memcpy(&opts, &rev->diffopt, sizeof(opts));
815 opts.output_format = DIFF_FORMAT_SUMMARY | DIFF_FORMAT_DIFFSTAT;
817 diff_setup_done(&opts);
819 diff_tree_sha1(origin->tree->object.sha1,
820 head->tree->object.sha1,
829 static const char *clean_message_id(const char *msg_id)
832 const char *a, *z, *m;
835 while ((ch = *m) && (isspace(ch) || (ch == '<')))
840 if (!isspace(ch) && (ch != '>'))
845 die("insane in-reply-to: %s", msg_id);
848 return xmemdupz(a, z - a);
851 static const char *set_outdir(const char *prefix, const char *output_directory)
853 if (output_directory && is_absolute_path(output_directory))
854 return output_directory;
856 if (!prefix || !*prefix) {
857 if (output_directory)
858 return output_directory;
859 /* The user did not explicitly ask for "./" */
864 outdir_offset = strlen(prefix);
865 if (!output_directory)
868 return xstrdup(prefix_filename(prefix, outdir_offset,
872 static const char * const builtin_format_patch_usage[] = {
873 "git format-patch [options] [<since> | <revision range>]",
877 static int keep_subject = 0;
879 static int keep_callback(const struct option *opt, const char *arg, int unset)
881 ((struct rev_info *)opt->value)->total = -1;
886 static int subject_prefix = 0;
888 static int subject_prefix_callback(const struct option *opt, const char *arg,
892 ((struct rev_info *)opt->value)->subject_prefix = arg;
896 static int numbered_cmdline_opt = 0;
898 static int numbered_callback(const struct option *opt, const char *arg,
901 *(int *)opt->value = numbered_cmdline_opt = unset ? 0 : 1;
907 static int no_numbered_callback(const struct option *opt, const char *arg,
910 return numbered_callback(opt, arg, 1);
913 static int output_directory_callback(const struct option *opt, const char *arg,
916 const char **dir = (const char **)opt->value;
918 die("Two output directories?");
923 static int thread_callback(const struct option *opt, const char *arg, int unset)
925 int *thread = (int *)opt->value;
928 else if (!arg || !strcmp(arg, "shallow"))
929 *thread = THREAD_SHALLOW;
930 else if (!strcmp(arg, "deep"))
931 *thread = THREAD_DEEP;
937 static int attach_callback(const struct option *opt, const char *arg, int unset)
939 struct rev_info *rev = (struct rev_info *)opt->value;
941 rev->mime_boundary = NULL;
943 rev->mime_boundary = arg;
945 rev->mime_boundary = git_version_string;
946 rev->no_inline = unset ? 0 : 1;
950 static int inline_callback(const struct option *opt, const char *arg, int unset)
952 struct rev_info *rev = (struct rev_info *)opt->value;
954 rev->mime_boundary = NULL;
956 rev->mime_boundary = arg;
958 rev->mime_boundary = git_version_string;
963 static int header_callback(const struct option *opt, const char *arg, int unset)
966 string_list_clear(&extra_hdr, 0);
967 string_list_clear(&extra_to, 0);
968 string_list_clear(&extra_cc, 0);
975 static int to_callback(const struct option *opt, const char *arg, int unset)
978 string_list_clear(&extra_to, 0);
980 string_list_append(&extra_to, arg);
984 static int cc_callback(const struct option *opt, const char *arg, int unset)
987 string_list_clear(&extra_cc, 0);
989 string_list_append(&extra_cc, arg);
993 int cmd_format_patch(int argc, const char **argv, const char *prefix)
995 struct commit *commit;
996 struct commit **list = NULL;
998 struct setup_revision_opt s_r_opt;
999 int nr = 0, total, i;
1001 int start_number = -1;
1002 int numbered_files = 0; /* _just_ numbers */
1003 int ignore_if_in_upstream = 0;
1004 int cover_letter = 0;
1005 int boundary_count = 0;
1006 int no_binary_diff = 0;
1007 struct commit *origin = NULL, *head = NULL;
1008 const char *in_reply_to = NULL;
1009 struct patch_ids ids;
1010 char *add_signoff = NULL;
1011 struct strbuf buf = STRBUF_INIT;
1012 int use_patch_format = 0;
1013 const struct option builtin_format_patch_options[] = {
1014 { OPTION_CALLBACK, 'n', "numbered", &numbered, NULL,
1015 "use [PATCH n/m] even with a single patch",
1016 PARSE_OPT_NOARG, numbered_callback },
1017 { OPTION_CALLBACK, 'N', "no-numbered", &numbered, NULL,
1018 "use [PATCH] even with multiple patches",
1019 PARSE_OPT_NOARG, no_numbered_callback },
1020 OPT_BOOLEAN('s', "signoff", &do_signoff, "add Signed-off-by:"),
1021 OPT_BOOLEAN(0, "stdout", &use_stdout,
1022 "print patches to standard out"),
1023 OPT_BOOLEAN(0, "cover-letter", &cover_letter,
1024 "generate a cover letter"),
1025 OPT_BOOLEAN(0, "numbered-files", &numbered_files,
1026 "use simple number sequence for output file names"),
1027 OPT_STRING(0, "suffix", &fmt_patch_suffix, "sfx",
1028 "use <sfx> instead of '.patch'"),
1029 OPT_INTEGER(0, "start-number", &start_number,
1030 "start numbering patches at <n> instead of 1"),
1031 { OPTION_CALLBACK, 0, "subject-prefix", &rev, "prefix",
1032 "Use [<prefix>] instead of [PATCH]",
1033 PARSE_OPT_NONEG, subject_prefix_callback },
1034 { OPTION_CALLBACK, 'o', "output-directory", &output_directory,
1035 "dir", "store resulting files in <dir>",
1036 PARSE_OPT_NONEG, output_directory_callback },
1037 { OPTION_CALLBACK, 'k', "keep-subject", &rev, NULL,
1038 "don't strip/add [PATCH]",
1039 PARSE_OPT_NOARG | PARSE_OPT_NONEG, keep_callback },
1040 OPT_BOOLEAN(0, "no-binary", &no_binary_diff,
1041 "don't output binary diffs"),
1042 OPT_BOOLEAN(0, "ignore-if-in-upstream", &ignore_if_in_upstream,
1043 "don't include a patch matching a commit upstream"),
1044 { OPTION_BOOLEAN, 'p', "no-stat", &use_patch_format, NULL,
1045 "show patch format instead of default (patch + stat)",
1046 PARSE_OPT_NONEG | PARSE_OPT_NOARG },
1047 OPT_GROUP("Messaging"),
1048 { OPTION_CALLBACK, 0, "add-header", NULL, "header",
1049 "add email header", 0, header_callback },
1050 { OPTION_CALLBACK, 0, "to", NULL, "email", "add To: header",
1052 { OPTION_CALLBACK, 0, "cc", NULL, "email", "add Cc: header",
1054 OPT_STRING(0, "in-reply-to", &in_reply_to, "message-id",
1055 "make first mail a reply to <message-id>"),
1056 { OPTION_CALLBACK, 0, "attach", &rev, "boundary",
1057 "attach the patch", PARSE_OPT_OPTARG,
1059 { OPTION_CALLBACK, 0, "inline", &rev, "boundary",
1061 PARSE_OPT_OPTARG | PARSE_OPT_NONEG,
1063 { OPTION_CALLBACK, 0, "thread", &thread, "style",
1064 "enable message threading, styles: shallow, deep",
1065 PARSE_OPT_OPTARG, thread_callback },
1066 OPT_STRING(0, "signature", &signature, "signature",
1071 extra_hdr.strdup_strings = 1;
1072 extra_to.strdup_strings = 1;
1073 extra_cc.strdup_strings = 1;
1074 git_config(git_format_config, NULL);
1075 init_revisions(&rev, prefix);
1076 rev.commit_format = CMIT_FMT_EMAIL;
1077 rev.verbose_header = 1;
1080 DIFF_OPT_SET(&rev.diffopt, RECURSIVE);
1081 rev.subject_prefix = fmt_patch_subject_prefix;
1082 memset(&s_r_opt, 0, sizeof(s_r_opt));
1083 s_r_opt.def = "HEAD";
1085 if (default_attach) {
1086 rev.mime_boundary = default_attach;
1091 * Parse the arguments before setup_revisions(), or something
1092 * like "git format-patch -o a123 HEAD^.." may fail; a123 is
1093 * possibly a valid SHA1.
1095 argc = parse_options(argc, argv, prefix, builtin_format_patch_options,
1096 builtin_format_patch_usage,
1097 PARSE_OPT_KEEP_ARGV0 | PARSE_OPT_KEEP_UNKNOWN |
1098 PARSE_OPT_KEEP_DASHDASH);
1101 const char *committer;
1103 committer = git_committer_info(IDENT_ERROR_ON_NO_NAME);
1104 endpos = strchr(committer, '>');
1106 die("bogus committer info %s", committer);
1107 add_signoff = xmemdupz(committer, endpos - committer + 1);
1110 for (i = 0; i < extra_hdr.nr; i++) {
1111 strbuf_addstr(&buf, extra_hdr.items[i].string);
1112 strbuf_addch(&buf, '\n');
1116 strbuf_addstr(&buf, "To: ");
1117 for (i = 0; i < extra_to.nr; i++) {
1119 strbuf_addstr(&buf, " ");
1120 strbuf_addstr(&buf, extra_to.items[i].string);
1121 if (i + 1 < extra_to.nr)
1122 strbuf_addch(&buf, ',');
1123 strbuf_addch(&buf, '\n');
1127 strbuf_addstr(&buf, "Cc: ");
1128 for (i = 0; i < extra_cc.nr; i++) {
1130 strbuf_addstr(&buf, " ");
1131 strbuf_addstr(&buf, extra_cc.items[i].string);
1132 if (i + 1 < extra_cc.nr)
1133 strbuf_addch(&buf, ',');
1134 strbuf_addch(&buf, '\n');
1137 rev.extra_headers = strbuf_detach(&buf, NULL);
1139 if (start_number < 0)
1143 * If numbered is set solely due to format.numbered in config,
1144 * and it would conflict with --keep-subject (-k) from the
1145 * command line, reset "numbered".
1147 if (numbered && keep_subject && !numbered_cmdline_opt)
1150 if (numbered && keep_subject)
1151 die ("-n and -k are mutually exclusive.");
1152 if (keep_subject && subject_prefix)
1153 die ("--subject-prefix and -k are mutually exclusive.");
1155 argc = setup_revisions(argc, argv, &rev, &s_r_opt);
1157 die ("unrecognized argument: %s", argv[1]);
1159 if (rev.diffopt.output_format & DIFF_FORMAT_NAME)
1160 die("--name-only does not make sense");
1161 if (rev.diffopt.output_format & DIFF_FORMAT_NAME_STATUS)
1162 die("--name-status does not make sense");
1163 if (rev.diffopt.output_format & DIFF_FORMAT_CHECKDIFF)
1164 die("--check does not make sense");
1166 if (!use_patch_format &&
1167 (!rev.diffopt.output_format ||
1168 rev.diffopt.output_format == DIFF_FORMAT_PATCH))
1169 rev.diffopt.output_format = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_SUMMARY;
1171 /* Always generate a patch */
1172 rev.diffopt.output_format |= DIFF_FORMAT_PATCH;
1174 if (!DIFF_OPT_TST(&rev.diffopt, TEXT) && !no_binary_diff)
1175 DIFF_OPT_SET(&rev.diffopt, BINARY);
1178 init_display_notes(&rev.notes_opt);
1181 output_directory = set_outdir(prefix, output_directory);
1185 if (output_directory) {
1187 die("standard output, or directory, which one?");
1188 if (mkdir(output_directory, 0777) < 0 && errno != EEXIST)
1189 die_errno("Could not create directory '%s'",
1193 if (rev.pending.nr == 1) {
1194 if (rev.max_count < 0 && !rev.show_root_diff) {
1196 * This is traditional behaviour of "git format-patch
1197 * origin" that prepares what the origin side still
1200 rev.pending.objects[0].item->flags |= UNINTERESTING;
1201 add_head_to_pending(&rev);
1204 * Otherwise, it is "format-patch -22 HEAD", and/or
1205 * "format-patch --root HEAD". The user wants
1206 * get_revision() to do the usual traversal.
1211 * We cannot move this anywhere earlier because we do want to
1212 * know if --root was given explicitly from the command line.
1214 rev.show_root_diff = 1;
1217 /* remember the range */
1219 for (i = 0; i < rev.pending.nr; i++) {
1220 struct object *o = rev.pending.objects[i].item;
1221 if (!(o->flags & UNINTERESTING))
1222 head = (struct commit *)o;
1224 /* We can't generate a cover letter without any patches */
1229 if (ignore_if_in_upstream) {
1230 /* Don't say anything if head and upstream are the same. */
1231 if (rev.pending.nr == 2) {
1232 struct object_array_entry *o = rev.pending.objects;
1233 if (hashcmp(o[0].item->sha1, o[1].item->sha1) == 0)
1236 get_patch_ids(&rev, &ids, prefix);
1240 realstdout = xfdopen(xdup(1), "w");
1242 if (prepare_revision_walk(&rev))
1243 die("revision walk setup failed");
1245 while ((commit = get_revision(&rev)) != NULL) {
1246 if (commit->object.flags & BOUNDARY) {
1248 origin = (boundary_count == 1) ? commit : NULL;
1252 if (ignore_if_in_upstream &&
1253 has_commit_patch_id(commit, &ids))
1257 list = xrealloc(list, nr * sizeof(list[0]));
1258 list[nr - 1] = commit;
1261 if (!keep_subject && auto_number && total > 1)
1264 rev.total = total + start_number - 1;
1265 if (in_reply_to || thread || cover_letter)
1266 rev.ref_message_ids = xcalloc(1, sizeof(struct string_list));
1268 const char *msgid = clean_message_id(in_reply_to);
1269 string_list_append(rev.ref_message_ids, msgid);
1271 rev.numbered_files = numbered_files;
1272 rev.patch_suffix = fmt_patch_suffix;
1275 gen_message_id(&rev, "cover");
1276 make_cover_letter(&rev, use_stdout, numbered, numbered_files,
1277 origin, nr, list, head);
1281 rev.add_signoff = add_signoff;
1285 rev.nr = total - nr + (start_number - 1);
1286 /* Make the second and subsequent mails replies to the first */
1288 /* Have we already had a message ID? */
1289 if (rev.message_id) {
1291 * For deep threading: make every mail
1292 * a reply to the previous one, no
1293 * matter what other options are set.
1295 * For shallow threading:
1297 * Without --cover-letter and
1298 * --in-reply-to, make every mail a
1299 * reply to the one before.
1301 * With --in-reply-to but no
1302 * --cover-letter, make every mail a
1303 * reply to the <reply-to>.
1305 * With --cover-letter, make every
1306 * mail but the cover letter a reply
1307 * to the cover letter. The cover
1308 * letter is a reply to the
1309 * --in-reply-to, if specified.
1311 if (thread == THREAD_SHALLOW
1312 && rev.ref_message_ids->nr > 0
1313 && (!cover_letter || rev.nr > 1))
1314 free(rev.message_id);
1316 string_list_append(rev.ref_message_ids,
1319 gen_message_id(&rev, sha1_to_hex(commit->object.sha1));
1322 if (!use_stdout && reopen_stdout(numbered_files ? NULL : commit,
1324 die("Failed to create output files");
1325 shown = log_tree_commit(&rev, commit);
1326 free(commit->buffer);
1327 commit->buffer = NULL;
1329 /* We put one extra blank line between formatted
1330 * patches and this flag is used by log-tree code
1331 * to see if it needs to emit a LF before showing
1332 * the log; when using one file per patch, we do
1333 * not want the extra blank line.
1338 if (rev.mime_boundary)
1339 printf("\n--%s%s--\n\n\n",
1340 mime_boundary_leader,
1349 string_list_clear(&extra_to, 0);
1350 string_list_clear(&extra_cc, 0);
1351 string_list_clear(&extra_hdr, 0);
1352 if (ignore_if_in_upstream)
1353 free_patch_ids(&ids);
1357 static int add_pending_commit(const char *arg, struct rev_info *revs, int flags)
1359 unsigned char sha1[20];
1360 if (get_sha1(arg, sha1) == 0) {
1361 struct commit *commit = lookup_commit_reference(sha1);
1363 commit->object.flags |= flags;
1364 add_pending_object(revs, &commit->object, arg);
1371 static const char * const cherry_usage[] = {
1372 "git cherry [-v] [<upstream> [<head> [<limit>]]]",
1376 static void print_commit(char sign, struct commit *commit, int verbose,
1380 printf("%c %s\n", sign,
1381 find_unique_abbrev(commit->object.sha1, abbrev));
1383 struct strbuf buf = STRBUF_INIT;
1384 struct pretty_print_context ctx = {0};
1385 pretty_print_commit(CMIT_FMT_ONELINE, commit, &buf, &ctx);
1386 printf("%c %s %s\n", sign,
1387 find_unique_abbrev(commit->object.sha1, abbrev),
1389 strbuf_release(&buf);
1393 int cmd_cherry(int argc, const char **argv, const char *prefix)
1395 struct rev_info revs;
1396 struct patch_ids ids;
1397 struct commit *commit;
1398 struct commit_list *list = NULL;
1399 struct branch *current_branch;
1400 const char *upstream;
1401 const char *head = "HEAD";
1402 const char *limit = NULL;
1403 int verbose = 0, abbrev = 0;
1405 struct option options[] = {
1406 OPT__ABBREV(&abbrev),
1407 OPT__VERBOSE(&verbose, "be verbose"),
1411 argc = parse_options(argc, argv, prefix, options, cherry_usage, 0);
1424 current_branch = branch_get(NULL);
1425 if (!current_branch || !current_branch->merge
1426 || !current_branch->merge[0]
1427 || !current_branch->merge[0]->dst) {
1428 fprintf(stderr, "Could not find a tracked"
1429 " remote branch, please"
1430 " specify <upstream> manually.\n");
1431 usage_with_options(cherry_usage, options);
1434 upstream = current_branch->merge[0]->dst;
1437 init_revisions(&revs, prefix);
1439 revs.combine_merges = 0;
1440 revs.ignore_merges = 1;
1441 DIFF_OPT_SET(&revs.diffopt, RECURSIVE);
1443 if (add_pending_commit(head, &revs, 0))
1444 die("Unknown commit %s", head);
1445 if (add_pending_commit(upstream, &revs, UNINTERESTING))
1446 die("Unknown commit %s", upstream);
1448 /* Don't say anything if head and upstream are the same. */
1449 if (revs.pending.nr == 2) {
1450 struct object_array_entry *o = revs.pending.objects;
1451 if (hashcmp(o[0].item->sha1, o[1].item->sha1) == 0)
1455 get_patch_ids(&revs, &ids, prefix);
1457 if (limit && add_pending_commit(limit, &revs, UNINTERESTING))
1458 die("Unknown commit %s", limit);
1460 /* reverse the list of commits */
1461 if (prepare_revision_walk(&revs))
1462 die("revision walk setup failed");
1463 while ((commit = get_revision(&revs)) != NULL) {
1465 if (commit->parents && commit->parents->next)
1468 commit_list_insert(commit, &list);
1474 commit = list->item;
1475 if (has_commit_patch_id(commit, &ids))
1477 print_commit(sign, commit, verbose, abbrev);
1481 free_patch_ids(&ids);