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 const char *fmt_patch_subject_prefix = "PATCH";
28 static const char *fmt_pretty;
30 static const char * const builtin_log_usage =
31 "git log [<options>] [<since>..<until>] [[--] <path>...]\n"
32 " or: git show [options] <object>...";
34 static void cmd_log_init(int argc, const char **argv, const char *prefix,
38 int decoration_style = 0;
40 rev->abbrev = DEFAULT_ABBREV;
41 rev->commit_format = CMIT_FMT_DEFAULT;
43 get_commit_format(fmt_pretty, rev);
44 rev->verbose_header = 1;
45 DIFF_OPT_SET(&rev->diffopt, RECURSIVE);
46 rev->show_root_diff = default_show_root;
47 rev->subject_prefix = fmt_patch_subject_prefix;
48 DIFF_OPT_SET(&rev->diffopt, ALLOW_TEXTCONV);
50 if (default_date_mode)
51 rev->date_mode = parse_date_format(default_date_mode);
54 * Check for -h before setup_revisions(), or "git log -h" will
55 * fail when run without a git directory.
57 if (argc == 2 && !strcmp(argv[1], "-h"))
58 usage(builtin_log_usage);
59 argc = setup_revisions(argc, argv, rev, "HEAD");
61 if (!rev->show_notes_given && !rev->pretty_given)
64 if (rev->diffopt.pickaxe || rev->diffopt.filter)
65 rev->always_show_header = 0;
66 if (DIFF_OPT_TST(&rev->diffopt, FOLLOW_RENAMES)) {
67 rev->always_show_header = 0;
68 if (rev->diffopt.nr_paths != 1)
69 usage("git logs can only follow renames on one pathname at a time");
71 for (i = 1; i < argc; i++) {
72 const char *arg = argv[i];
73 if (!strcmp(arg, "--decorate")) {
74 decoration_style = DECORATE_SHORT_REFS;
75 } else if (!prefixcmp(arg, "--decorate=")) {
76 const char *v = skip_prefix(arg, "--decorate=");
77 if (!strcmp(v, "full"))
78 decoration_style = DECORATE_FULL_REFS;
79 else if (!strcmp(v, "short"))
80 decoration_style = DECORATE_SHORT_REFS;
82 die("invalid --decorate option: %s", arg);
83 } else if (!strcmp(arg, "--source")) {
85 } else if (!strcmp(arg, "-h")) {
86 usage(builtin_log_usage);
88 die("unrecognized argument: %s", arg);
90 if (decoration_style) {
91 rev->show_decorations = 1;
92 load_ref_decorations(decoration_style);
97 * This gives a rough estimate for how many commits we
98 * will print out in the list.
100 static int estimate_commit_count(struct rev_info *rev, struct commit_list *list)
105 struct commit *commit = list->item;
106 unsigned int flags = commit->object.flags;
108 if (!(flags & (TREESAME | UNINTERESTING)))
114 static void show_early_header(struct rev_info *rev, const char *stage, int nr)
116 if (rev->shown_one) {
118 if (rev->commit_format != CMIT_FMT_ONELINE)
119 putchar(rev->diffopt.line_termination);
121 printf("Final output: %d %s\n", nr, stage);
124 static struct itimerval early_output_timer;
126 static void log_show_early(struct rev_info *revs, struct commit_list *list)
128 int i = revs->early_output;
131 sort_in_topological_order(&list, revs->lifo);
133 struct commit *commit = list->item;
134 switch (simplify_commit(revs, commit)) {
137 int n = estimate_commit_count(revs, list);
138 show_early_header(revs, "incomplete", n);
141 log_tree_commit(revs, commit);
152 /* Did we already get enough commits for the early output? */
157 * ..if no, then repeat it twice a second until we
160 * NOTE! We don't use "it_interval", because if the
161 * reader isn't listening, we want our output to be
162 * throttled by the writing, and not have the timer
163 * trigger every second even if we're blocked on a
166 early_output_timer.it_value.tv_sec = 0;
167 early_output_timer.it_value.tv_usec = 500000;
168 setitimer(ITIMER_REAL, &early_output_timer, NULL);
171 static void early_output(int signal)
173 show_early_output = log_show_early;
176 static void setup_early_output(struct rev_info *rev)
181 * Set up the signal handler, minimally intrusively:
182 * we only set a single volatile integer word (not
183 * using sigatomic_t - trying to avoid unnecessary
184 * system dependencies and headers), and using
187 memset(&sa, 0, sizeof(sa));
188 sa.sa_handler = early_output;
189 sigemptyset(&sa.sa_mask);
190 sa.sa_flags = SA_RESTART;
191 sigaction(SIGALRM, &sa, NULL);
194 * If we can get the whole output in less than a
195 * tenth of a second, don't even bother doing the
196 * early-output thing..
198 * This is a one-time-only trigger.
200 early_output_timer.it_value.tv_sec = 0;
201 early_output_timer.it_value.tv_usec = 100000;
202 setitimer(ITIMER_REAL, &early_output_timer, NULL);
205 static void finish_early_output(struct rev_info *rev)
207 int n = estimate_commit_count(rev, rev->commits);
208 signal(SIGALRM, SIG_IGN);
209 show_early_header(rev, "done", n);
212 static int cmd_log_walk(struct rev_info *rev)
214 struct commit *commit;
216 if (rev->early_output)
217 setup_early_output(rev);
219 if (prepare_revision_walk(rev))
220 die("revision walk setup failed");
222 if (rev->early_output)
223 finish_early_output(rev);
226 * For --check and --exit-code, the exit code is based on CHECK_FAILED
227 * and HAS_CHANGES being accumulated in rev->diffopt, so be careful to
228 * retain that state information if replacing rev->diffopt in this loop
230 while ((commit = get_revision(rev)) != NULL) {
231 log_tree_commit(rev, commit);
232 if (!rev->reflog_info) {
233 /* we allow cycles in reflog ancestry */
234 free(commit->buffer);
235 commit->buffer = NULL;
237 free_commit_list(commit->parents);
238 commit->parents = NULL;
240 if (rev->diffopt.output_format & DIFF_FORMAT_CHECKDIFF &&
241 DIFF_OPT_TST(&rev->diffopt, CHECK_FAILED)) {
244 return diff_result_code(&rev->diffopt, 0);
247 static int git_log_config(const char *var, const char *value, void *cb)
249 if (!strcmp(var, "format.pretty"))
250 return git_config_string(&fmt_pretty, var, value);
251 if (!strcmp(var, "format.subjectprefix"))
252 return git_config_string(&fmt_patch_subject_prefix, var, value);
253 if (!strcmp(var, "log.date"))
254 return git_config_string(&default_date_mode, var, value);
255 if (!strcmp(var, "log.showroot")) {
256 default_show_root = git_config_bool(var, value);
259 return git_diff_ui_config(var, value, cb);
262 int cmd_whatchanged(int argc, const char **argv, const char *prefix)
266 git_config(git_log_config, NULL);
268 if (diff_use_color_default == -1)
269 diff_use_color_default = git_use_color_default;
271 init_revisions(&rev, prefix);
273 rev.simplify_history = 0;
274 cmd_log_init(argc, argv, prefix, &rev);
275 if (!rev.diffopt.output_format)
276 rev.diffopt.output_format = DIFF_FORMAT_RAW;
277 return cmd_log_walk(&rev);
280 static void show_tagger(char *buf, int len, struct rev_info *rev)
282 struct strbuf out = STRBUF_INIT;
284 pp_user_info("Tagger", rev->commit_format, &out, buf, rev->date_mode,
285 git_log_output_encoding ?
286 git_log_output_encoding: git_commit_encoding);
287 printf("%s", out.buf);
288 strbuf_release(&out);
291 static int show_object(const unsigned char *sha1, int show_tag_object,
292 struct rev_info *rev)
295 enum object_type type;
296 char *buf = read_sha1_file(sha1, &type, &size);
300 return error("Could not read object %s", sha1_to_hex(sha1));
303 while (offset < size && buf[offset] != '\n') {
304 int new_offset = offset + 1;
305 while (new_offset < size && buf[new_offset++] != '\n')
307 if (!prefixcmp(buf + offset, "tagger "))
308 show_tagger(buf + offset + 7,
309 new_offset - offset - 7, rev);
314 fwrite(buf + offset, size - offset, 1, stdout);
319 static int show_tree_object(const unsigned char *sha1,
320 const char *base, int baselen,
321 const char *pathname, unsigned mode, int stage, void *context)
323 printf("%s%s\n", pathname, S_ISDIR(mode) ? "/" : "");
327 int cmd_show(int argc, const char **argv, const char *prefix)
330 struct object_array_entry *objects;
331 int i, count, ret = 0;
333 git_config(git_log_config, NULL);
335 if (diff_use_color_default == -1)
336 diff_use_color_default = git_use_color_default;
338 init_revisions(&rev, prefix);
340 rev.combine_merges = 1;
341 rev.dense_combined_merges = 1;
342 rev.always_show_header = 1;
343 rev.ignore_merges = 0;
345 cmd_log_init(argc, argv, prefix, &rev);
347 count = rev.pending.nr;
348 objects = rev.pending.objects;
349 for (i = 0; i < count && !ret; i++) {
350 struct object *o = objects[i].item;
351 const char *name = objects[i].name;
354 ret = show_object(o->sha1, 0, NULL);
357 struct tag *t = (struct tag *)o;
361 printf("%stag %s%s\n",
362 diff_get_color_opt(&rev.diffopt, DIFF_COMMIT),
364 diff_get_color_opt(&rev.diffopt, DIFF_RESET));
365 ret = show_object(o->sha1, 1, &rev);
369 o = parse_object(t->tagged->sha1);
371 ret = error("Could not read object %s",
372 sha1_to_hex(t->tagged->sha1));
380 printf("%stree %s%s\n\n",
381 diff_get_color_opt(&rev.diffopt, DIFF_COMMIT),
383 diff_get_color_opt(&rev.diffopt, DIFF_RESET));
384 read_tree_recursive((struct tree *)o, "", 0, 0, NULL,
385 show_tree_object, NULL);
389 rev.pending.nr = rev.pending.alloc = 0;
390 rev.pending.objects = NULL;
391 add_object_array(o, name, &rev.pending);
392 ret = cmd_log_walk(&rev);
395 ret = error("Unknown type: %d", o->type);
403 * This is equivalent to "git log -g --abbrev-commit --pretty=oneline"
405 int cmd_log_reflog(int argc, const char **argv, const char *prefix)
409 git_config(git_log_config, NULL);
411 if (diff_use_color_default == -1)
412 diff_use_color_default = git_use_color_default;
414 init_revisions(&rev, prefix);
415 init_reflog_walk(&rev.reflog_info);
416 rev.abbrev_commit = 1;
417 rev.verbose_header = 1;
418 cmd_log_init(argc, argv, prefix, &rev);
421 * This means that we override whatever commit format the user gave
422 * on the cmd line. Sad, but cmd_log_init() currently doesn't
423 * allow us to set a different default.
425 rev.commit_format = CMIT_FMT_ONELINE;
426 rev.use_terminator = 1;
427 rev.always_show_header = 1;
430 * We get called through "git reflog", so unlike the other log
431 * routines, we need to set up our pager manually..
435 return cmd_log_walk(&rev);
438 int cmd_log(int argc, const char **argv, const char *prefix)
442 git_config(git_log_config, NULL);
444 if (diff_use_color_default == -1)
445 diff_use_color_default = git_use_color_default;
447 init_revisions(&rev, prefix);
448 rev.always_show_header = 1;
449 cmd_log_init(argc, argv, prefix, &rev);
450 return cmd_log_walk(&rev);
455 static const char *fmt_patch_suffix = ".patch";
456 static int numbered = 0;
457 static int auto_number = 1;
459 static char *default_attach = NULL;
461 static struct string_list extra_hdr;
462 static struct string_list extra_to;
463 static struct string_list extra_cc;
465 static void add_header(const char *value)
467 struct string_list_item *item;
468 int len = strlen(value);
469 while (len && value[len - 1] == '\n')
472 if (!strncasecmp(value, "to: ", 4)) {
473 item = string_list_append(value + 4, &extra_to);
475 } else if (!strncasecmp(value, "cc: ", 4)) {
476 item = string_list_append(value + 4, &extra_cc);
479 item = string_list_append(value, &extra_hdr);
482 item->string[len] = '\0';
485 #define THREAD_SHALLOW 1
486 #define THREAD_DEEP 2
487 static int thread = 0;
488 static int do_signoff = 0;
490 static int git_format_config(const char *var, const char *value, void *cb)
492 if (!strcmp(var, "format.headers")) {
494 die("format.headers without value");
498 if (!strcmp(var, "format.suffix"))
499 return git_config_string(&fmt_patch_suffix, var, value);
500 if (!strcmp(var, "format.to")) {
502 return config_error_nonbool(var);
503 string_list_append(value, &extra_to);
506 if (!strcmp(var, "format.cc")) {
508 return config_error_nonbool(var);
509 string_list_append(value, &extra_cc);
512 if (!strcmp(var, "diff.color") || !strcmp(var, "color.diff")) {
515 if (!strcmp(var, "format.numbered")) {
516 if (value && !strcasecmp(value, "auto")) {
520 numbered = git_config_bool(var, value);
521 auto_number = auto_number && numbered;
524 if (!strcmp(var, "format.attach")) {
526 default_attach = xstrdup(value);
528 default_attach = xstrdup(git_version_string);
531 if (!strcmp(var, "format.thread")) {
532 if (value && !strcasecmp(value, "deep")) {
533 thread = THREAD_DEEP;
536 if (value && !strcasecmp(value, "shallow")) {
537 thread = THREAD_SHALLOW;
540 thread = git_config_bool(var, value) && THREAD_SHALLOW;
543 if (!strcmp(var, "format.signoff")) {
544 do_signoff = git_config_bool(var, value);
548 return git_log_config(var, value, cb);
551 static FILE *realstdout = NULL;
552 static const char *output_directory = NULL;
553 static int outdir_offset;
555 static int reopen_stdout(struct commit *commit, struct rev_info *rev)
557 struct strbuf filename = STRBUF_INIT;
558 int suffix_len = strlen(fmt_patch_suffix) + 1;
560 if (output_directory) {
561 strbuf_addstr(&filename, output_directory);
563 PATH_MAX - FORMAT_PATCH_NAME_MAX - suffix_len)
564 return error("name of output directory is too long");
565 if (filename.buf[filename.len - 1] != '/')
566 strbuf_addch(&filename, '/');
569 get_patch_filename(commit, rev->nr, fmt_patch_suffix, &filename);
571 if (!DIFF_OPT_TST(&rev->diffopt, QUICK))
572 fprintf(realstdout, "%s\n", filename.buf + outdir_offset);
574 if (freopen(filename.buf, "w", stdout) == NULL)
575 return error("Cannot open patch file %s", filename.buf);
577 strbuf_release(&filename);
581 static void get_patch_ids(struct rev_info *rev, struct patch_ids *ids, const char *prefix)
583 struct rev_info check_rev;
584 struct commit *commit;
585 struct object *o1, *o2;
586 unsigned flags1, flags2;
588 if (rev->pending.nr != 2)
589 die("Need exactly one range.");
591 o1 = rev->pending.objects[0].item;
593 o2 = rev->pending.objects[1].item;
596 if ((flags1 & UNINTERESTING) == (flags2 & UNINTERESTING))
601 /* given a range a..b get all patch ids for b..a */
602 init_revisions(&check_rev, prefix);
603 o1->flags ^= UNINTERESTING;
604 o2->flags ^= UNINTERESTING;
605 add_pending_object(&check_rev, o1, "o1");
606 add_pending_object(&check_rev, o2, "o2");
607 if (prepare_revision_walk(&check_rev))
608 die("revision walk setup failed");
610 while ((commit = get_revision(&check_rev)) != NULL) {
612 if (commit->parents && commit->parents->next)
615 add_commit_patch_id(commit, ids);
618 /* reset for next revision walk */
619 clear_commit_marks((struct commit *)o1,
620 SEEN | UNINTERESTING | SHOWN | ADDED);
621 clear_commit_marks((struct commit *)o2,
622 SEEN | UNINTERESTING | SHOWN | ADDED);
627 static void gen_message_id(struct rev_info *info, char *base)
629 const char *committer = git_committer_info(IDENT_WARN_ON_NO_NAME);
630 const char *email_start = strrchr(committer, '<');
631 const char *email_end = strrchr(committer, '>');
632 struct strbuf buf = STRBUF_INIT;
633 if (!email_start || !email_end || email_start > email_end - 1)
634 die("Could not extract email from committer identity.");
635 strbuf_addf(&buf, "%s.%lu.git.%.*s", base,
636 (unsigned long) time(NULL),
637 (int)(email_end - email_start - 1), email_start + 1);
638 info->message_id = strbuf_detach(&buf, NULL);
641 static void make_cover_letter(struct rev_info *rev, int use_stdout,
642 int numbered, int numbered_files,
643 struct commit *origin,
644 int nr, struct commit **list, struct commit *head)
646 const char *committer;
647 const char *subject_start = NULL;
648 const char *body = "*** SUBJECT HERE ***\n\n*** BLURB HERE ***\n";
650 const char *extra_headers = rev->extra_headers;
652 struct strbuf sb = STRBUF_INIT;
654 const char *encoding = "UTF-8";
655 struct diff_options opts;
656 int need_8bit_cte = 0;
657 struct commit *commit = NULL;
659 if (rev->commit_format != CMIT_FMT_EMAIL)
660 die("Cover letter needs email format");
662 committer = git_committer_info(0);
664 if (!numbered_files) {
666 * We fake a commit for the cover letter so we get the filename
669 commit = xcalloc(1, sizeof(*commit));
670 commit->buffer = xmalloc(400);
671 snprintf(commit->buffer, 400,
672 "tree 0000000000000000000000000000000000000000\n"
677 sha1_to_hex(head->object.sha1), committer, committer);
680 if (!use_stdout && reopen_stdout(commit, rev))
685 free(commit->buffer);
689 log_write_email_headers(rev, head, &subject_start, &extra_headers,
692 for (i = 0; !need_8bit_cte && i < nr; i++)
693 if (has_non_ascii(list[i]->buffer))
697 pp_user_info(NULL, CMIT_FMT_EMAIL, &sb, committer, DATE_RFC2822,
699 pp_title_line(CMIT_FMT_EMAIL, &msg, &sb, subject_start, extra_headers,
700 encoding, need_8bit_cte);
701 pp_remainder(CMIT_FMT_EMAIL, &msg, &sb, 0);
702 printf("%s\n", sb.buf);
711 for (i = 0; i < nr; i++)
712 shortlog_add_commit(&log, list[i]);
714 shortlog_output(&log);
717 * We can only do diffstat with a unique reference point
722 memcpy(&opts, &rev->diffopt, sizeof(opts));
723 opts.output_format = DIFF_FORMAT_SUMMARY | DIFF_FORMAT_DIFFSTAT;
725 diff_setup_done(&opts);
727 diff_tree_sha1(origin->tree->object.sha1,
728 head->tree->object.sha1,
736 static const char *clean_message_id(const char *msg_id)
739 const char *a, *z, *m;
742 while ((ch = *m) && (isspace(ch) || (ch == '<')))
747 if (!isspace(ch) && (ch != '>'))
752 die("insane in-reply-to: %s", msg_id);
755 return xmemdupz(a, z - a);
758 static const char *set_outdir(const char *prefix, const char *output_directory)
760 if (output_directory && is_absolute_path(output_directory))
761 return output_directory;
763 if (!prefix || !*prefix) {
764 if (output_directory)
765 return output_directory;
766 /* The user did not explicitly ask for "./" */
771 outdir_offset = strlen(prefix);
772 if (!output_directory)
775 return xstrdup(prefix_filename(prefix, outdir_offset,
779 static const char * const builtin_format_patch_usage[] = {
780 "git format-patch [options] [<since> | <revision range>]",
784 static int keep_subject = 0;
786 static int keep_callback(const struct option *opt, const char *arg, int unset)
788 ((struct rev_info *)opt->value)->total = -1;
793 static int subject_prefix = 0;
795 static int subject_prefix_callback(const struct option *opt, const char *arg,
799 ((struct rev_info *)opt->value)->subject_prefix = arg;
803 static int numbered_cmdline_opt = 0;
805 static int numbered_callback(const struct option *opt, const char *arg,
808 *(int *)opt->value = numbered_cmdline_opt = unset ? 0 : 1;
814 static int no_numbered_callback(const struct option *opt, const char *arg,
817 return numbered_callback(opt, arg, 1);
820 static int output_directory_callback(const struct option *opt, const char *arg,
823 const char **dir = (const char **)opt->value;
825 die("Two output directories?");
830 static int thread_callback(const struct option *opt, const char *arg, int unset)
832 int *thread = (int *)opt->value;
835 else if (!arg || !strcmp(arg, "shallow"))
836 *thread = THREAD_SHALLOW;
837 else if (!strcmp(arg, "deep"))
838 *thread = THREAD_DEEP;
844 static int attach_callback(const struct option *opt, const char *arg, int unset)
846 struct rev_info *rev = (struct rev_info *)opt->value;
848 rev->mime_boundary = NULL;
850 rev->mime_boundary = arg;
852 rev->mime_boundary = git_version_string;
853 rev->no_inline = unset ? 0 : 1;
857 static int inline_callback(const struct option *opt, const char *arg, int unset)
859 struct rev_info *rev = (struct rev_info *)opt->value;
861 rev->mime_boundary = NULL;
863 rev->mime_boundary = arg;
865 rev->mime_boundary = git_version_string;
870 static int header_callback(const struct option *opt, const char *arg, int unset)
873 string_list_clear(&extra_hdr, 0);
874 string_list_clear(&extra_to, 0);
875 string_list_clear(&extra_cc, 0);
882 static int to_callback(const struct option *opt, const char *arg, int unset)
885 string_list_clear(&extra_to, 0);
887 string_list_append(arg, &extra_to);
891 static int cc_callback(const struct option *opt, const char *arg, int unset)
894 string_list_clear(&extra_cc, 0);
896 string_list_append(arg, &extra_cc);
900 int cmd_format_patch(int argc, const char **argv, const char *prefix)
902 struct commit *commit;
903 struct commit **list = NULL;
905 int nr = 0, total, i;
907 int start_number = -1;
908 int numbered_files = 0; /* _just_ numbers */
909 int ignore_if_in_upstream = 0;
910 int cover_letter = 0;
911 int boundary_count = 0;
912 int no_binary_diff = 0;
913 struct commit *origin = NULL, *head = NULL;
914 const char *in_reply_to = NULL;
915 struct patch_ids ids;
916 char *add_signoff = NULL;
917 struct strbuf buf = STRBUF_INIT;
918 int use_patch_format = 0;
919 const struct option builtin_format_patch_options[] = {
920 { OPTION_CALLBACK, 'n', "numbered", &numbered, NULL,
921 "use [PATCH n/m] even with a single patch",
922 PARSE_OPT_NOARG, numbered_callback },
923 { OPTION_CALLBACK, 'N', "no-numbered", &numbered, NULL,
924 "use [PATCH] even with multiple patches",
925 PARSE_OPT_NOARG, no_numbered_callback },
926 OPT_BOOLEAN('s', "signoff", &do_signoff, "add Signed-off-by:"),
927 OPT_BOOLEAN(0, "stdout", &use_stdout,
928 "print patches to standard out"),
929 OPT_BOOLEAN(0, "cover-letter", &cover_letter,
930 "generate a cover letter"),
931 OPT_BOOLEAN(0, "numbered-files", &numbered_files,
932 "use simple number sequence for output file names"),
933 OPT_STRING(0, "suffix", &fmt_patch_suffix, "sfx",
934 "use <sfx> instead of '.patch'"),
935 OPT_INTEGER(0, "start-number", &start_number,
936 "start numbering patches at <n> instead of 1"),
937 { OPTION_CALLBACK, 0, "subject-prefix", &rev, "prefix",
938 "Use [<prefix>] instead of [PATCH]",
939 PARSE_OPT_NONEG, subject_prefix_callback },
940 { OPTION_CALLBACK, 'o', "output-directory", &output_directory,
941 "dir", "store resulting files in <dir>",
942 PARSE_OPT_NONEG, output_directory_callback },
943 { OPTION_CALLBACK, 'k', "keep-subject", &rev, NULL,
944 "don't strip/add [PATCH]",
945 PARSE_OPT_NOARG | PARSE_OPT_NONEG, keep_callback },
946 OPT_BOOLEAN(0, "no-binary", &no_binary_diff,
947 "don't output binary diffs"),
948 OPT_BOOLEAN(0, "ignore-if-in-upstream", &ignore_if_in_upstream,
949 "don't include a patch matching a commit upstream"),
950 { OPTION_BOOLEAN, 'p', "no-stat", &use_patch_format, NULL,
951 "show patch format instead of default (patch + stat)",
952 PARSE_OPT_NONEG | PARSE_OPT_NOARG },
953 OPT_GROUP("Messaging"),
954 { OPTION_CALLBACK, 0, "add-header", NULL, "header",
955 "add email header", 0, header_callback },
956 { OPTION_CALLBACK, 0, "to", NULL, "email", "add To: header",
958 { OPTION_CALLBACK, 0, "cc", NULL, "email", "add Cc: header",
960 OPT_STRING(0, "in-reply-to", &in_reply_to, "message-id",
961 "make first mail a reply to <message-id>"),
962 { OPTION_CALLBACK, 0, "attach", &rev, "boundary",
963 "attach the patch", PARSE_OPT_OPTARG,
965 { OPTION_CALLBACK, 0, "inline", &rev, "boundary",
967 PARSE_OPT_OPTARG | PARSE_OPT_NONEG,
969 { OPTION_CALLBACK, 0, "thread", &thread, "style",
970 "enable message threading, styles: shallow, deep",
971 PARSE_OPT_OPTARG, thread_callback },
975 extra_hdr.strdup_strings = 1;
976 extra_to.strdup_strings = 1;
977 extra_cc.strdup_strings = 1;
978 git_config(git_format_config, NULL);
979 init_revisions(&rev, prefix);
980 rev.commit_format = CMIT_FMT_EMAIL;
981 rev.verbose_header = 1;
983 rev.combine_merges = 0;
984 rev.ignore_merges = 1;
985 DIFF_OPT_SET(&rev.diffopt, RECURSIVE);
987 rev.subject_prefix = fmt_patch_subject_prefix;
989 if (default_attach) {
990 rev.mime_boundary = default_attach;
995 * Parse the arguments before setup_revisions(), or something
996 * like "git format-patch -o a123 HEAD^.." may fail; a123 is
997 * possibly a valid SHA1.
999 argc = parse_options(argc, argv, prefix, builtin_format_patch_options,
1000 builtin_format_patch_usage,
1001 PARSE_OPT_KEEP_ARGV0 | PARSE_OPT_KEEP_UNKNOWN |
1002 PARSE_OPT_KEEP_DASHDASH);
1005 const char *committer;
1007 committer = git_committer_info(IDENT_ERROR_ON_NO_NAME);
1008 endpos = strchr(committer, '>');
1010 die("bogus committer info %s", committer);
1011 add_signoff = xmemdupz(committer, endpos - committer + 1);
1014 for (i = 0; i < extra_hdr.nr; i++) {
1015 strbuf_addstr(&buf, extra_hdr.items[i].string);
1016 strbuf_addch(&buf, '\n');
1020 strbuf_addstr(&buf, "To: ");
1021 for (i = 0; i < extra_to.nr; i++) {
1023 strbuf_addstr(&buf, " ");
1024 strbuf_addstr(&buf, extra_to.items[i].string);
1025 if (i + 1 < extra_to.nr)
1026 strbuf_addch(&buf, ',');
1027 strbuf_addch(&buf, '\n');
1031 strbuf_addstr(&buf, "Cc: ");
1032 for (i = 0; i < extra_cc.nr; i++) {
1034 strbuf_addstr(&buf, " ");
1035 strbuf_addstr(&buf, extra_cc.items[i].string);
1036 if (i + 1 < extra_cc.nr)
1037 strbuf_addch(&buf, ',');
1038 strbuf_addch(&buf, '\n');
1041 rev.extra_headers = strbuf_detach(&buf, NULL);
1043 if (start_number < 0)
1047 * If numbered is set solely due to format.numbered in config,
1048 * and it would conflict with --keep-subject (-k) from the
1049 * command line, reset "numbered".
1051 if (numbered && keep_subject && !numbered_cmdline_opt)
1054 if (numbered && keep_subject)
1055 die ("-n and -k are mutually exclusive.");
1056 if (keep_subject && subject_prefix)
1057 die ("--subject-prefix and -k are mutually exclusive.");
1059 argc = setup_revisions(argc, argv, &rev, "HEAD");
1061 die ("unrecognized argument: %s", argv[1]);
1063 if (rev.diffopt.output_format & DIFF_FORMAT_NAME)
1064 die("--name-only does not make sense");
1065 if (rev.diffopt.output_format & DIFF_FORMAT_NAME_STATUS)
1066 die("--name-status does not make sense");
1067 if (rev.diffopt.output_format & DIFF_FORMAT_CHECKDIFF)
1068 die("--check does not make sense");
1070 if (!use_patch_format &&
1071 (!rev.diffopt.output_format ||
1072 rev.diffopt.output_format == DIFF_FORMAT_PATCH))
1073 rev.diffopt.output_format = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_SUMMARY;
1075 /* Always generate a patch */
1076 rev.diffopt.output_format |= DIFF_FORMAT_PATCH;
1078 if (!DIFF_OPT_TST(&rev.diffopt, TEXT) && !no_binary_diff)
1079 DIFF_OPT_SET(&rev.diffopt, BINARY);
1082 output_directory = set_outdir(prefix, output_directory);
1084 if (output_directory) {
1086 die("standard output, or directory, which one?");
1087 if (mkdir(output_directory, 0777) < 0 && errno != EEXIST)
1088 die_errno("Could not create directory '%s'",
1092 if (rev.pending.nr == 1) {
1093 if (rev.max_count < 0 && !rev.show_root_diff) {
1095 * This is traditional behaviour of "git format-patch
1096 * origin" that prepares what the origin side still
1099 rev.pending.objects[0].item->flags |= UNINTERESTING;
1100 add_head_to_pending(&rev);
1103 * Otherwise, it is "format-patch -22 HEAD", and/or
1104 * "format-patch --root HEAD". The user wants
1105 * get_revision() to do the usual traversal.
1110 * We cannot move this anywhere earlier because we do want to
1111 * know if --root was given explicitly from the command line.
1113 rev.show_root_diff = 1;
1116 /* remember the range */
1118 for (i = 0; i < rev.pending.nr; i++) {
1119 struct object *o = rev.pending.objects[i].item;
1120 if (!(o->flags & UNINTERESTING))
1121 head = (struct commit *)o;
1123 /* We can't generate a cover letter without any patches */
1128 if (ignore_if_in_upstream)
1129 get_patch_ids(&rev, &ids, prefix);
1132 realstdout = xfdopen(xdup(1), "w");
1134 if (prepare_revision_walk(&rev))
1135 die("revision walk setup failed");
1137 while ((commit = get_revision(&rev)) != NULL) {
1138 if (commit->object.flags & BOUNDARY) {
1140 origin = (boundary_count == 1) ? commit : NULL;
1145 if (commit->parents && commit->parents->next)
1148 if (ignore_if_in_upstream &&
1149 has_commit_patch_id(commit, &ids))
1153 list = xrealloc(list, nr * sizeof(list[0]));
1154 list[nr - 1] = commit;
1157 if (!keep_subject && auto_number && total > 1)
1160 rev.total = total + start_number - 1;
1161 if (in_reply_to || thread || cover_letter)
1162 rev.ref_message_ids = xcalloc(1, sizeof(struct string_list));
1164 const char *msgid = clean_message_id(in_reply_to);
1165 string_list_append(msgid, rev.ref_message_ids);
1167 rev.numbered_files = numbered_files;
1168 rev.patch_suffix = fmt_patch_suffix;
1171 gen_message_id(&rev, "cover");
1172 make_cover_letter(&rev, use_stdout, numbered, numbered_files,
1173 origin, nr, list, head);
1177 rev.add_signoff = add_signoff;
1181 rev.nr = total - nr + (start_number - 1);
1182 /* Make the second and subsequent mails replies to the first */
1184 /* Have we already had a message ID? */
1185 if (rev.message_id) {
1187 * For deep threading: make every mail
1188 * a reply to the previous one, no
1189 * matter what other options are set.
1191 * For shallow threading:
1193 * Without --cover-letter and
1194 * --in-reply-to, make every mail a
1195 * reply to the one before.
1197 * With --in-reply-to but no
1198 * --cover-letter, make every mail a
1199 * reply to the <reply-to>.
1201 * With --cover-letter, make every
1202 * mail but the cover letter a reply
1203 * to the cover letter. The cover
1204 * letter is a reply to the
1205 * --in-reply-to, if specified.
1207 if (thread == THREAD_SHALLOW
1208 && rev.ref_message_ids->nr > 0
1209 && (!cover_letter || rev.nr > 1))
1210 free(rev.message_id);
1212 string_list_append(rev.message_id,
1213 rev.ref_message_ids);
1215 gen_message_id(&rev, sha1_to_hex(commit->object.sha1));
1218 if (!use_stdout && reopen_stdout(numbered_files ? NULL : commit,
1220 die("Failed to create output files");
1221 shown = log_tree_commit(&rev, commit);
1222 free(commit->buffer);
1223 commit->buffer = NULL;
1225 /* We put one extra blank line between formatted
1226 * patches and this flag is used by log-tree code
1227 * to see if it needs to emit a LF before showing
1228 * the log; when using one file per patch, we do
1229 * not want the extra blank line.
1234 if (rev.mime_boundary)
1235 printf("\n--%s%s--\n\n\n",
1236 mime_boundary_leader,
1239 printf("-- \n%s\n\n", git_version_string);
1245 string_list_clear(&extra_to, 0);
1246 string_list_clear(&extra_cc, 0);
1247 string_list_clear(&extra_hdr, 0);
1248 if (ignore_if_in_upstream)
1249 free_patch_ids(&ids);
1253 static int add_pending_commit(const char *arg, struct rev_info *revs, int flags)
1255 unsigned char sha1[20];
1256 if (get_sha1(arg, sha1) == 0) {
1257 struct commit *commit = lookup_commit_reference(sha1);
1259 commit->object.flags |= flags;
1260 add_pending_object(revs, &commit->object, arg);
1267 static const char cherry_usage[] =
1268 "git cherry [-v] [<upstream> [<head> [<limit>]]]";
1269 int cmd_cherry(int argc, const char **argv, const char *prefix)
1271 struct rev_info revs;
1272 struct patch_ids ids;
1273 struct commit *commit;
1274 struct commit_list *list = NULL;
1275 struct branch *current_branch;
1276 const char *upstream;
1277 const char *head = "HEAD";
1278 const char *limit = NULL;
1281 if (argc > 1 && !strcmp(argv[1], "-v")) {
1287 if (argc > 1 && !strcmp(argv[1], "-h"))
1288 usage(cherry_usage);
1301 current_branch = branch_get(NULL);
1302 if (!current_branch || !current_branch->merge
1303 || !current_branch->merge[0]
1304 || !current_branch->merge[0]->dst) {
1305 fprintf(stderr, "Could not find a tracked"
1306 " remote branch, please"
1307 " specify <upstream> manually.\n");
1308 usage(cherry_usage);
1311 upstream = current_branch->merge[0]->dst;
1314 init_revisions(&revs, prefix);
1316 revs.combine_merges = 0;
1317 revs.ignore_merges = 1;
1318 DIFF_OPT_SET(&revs.diffopt, RECURSIVE);
1320 if (add_pending_commit(head, &revs, 0))
1321 die("Unknown commit %s", head);
1322 if (add_pending_commit(upstream, &revs, UNINTERESTING))
1323 die("Unknown commit %s", upstream);
1325 /* Don't say anything if head and upstream are the same. */
1326 if (revs.pending.nr == 2) {
1327 struct object_array_entry *o = revs.pending.objects;
1328 if (hashcmp(o[0].item->sha1, o[1].item->sha1) == 0)
1332 get_patch_ids(&revs, &ids, prefix);
1334 if (limit && add_pending_commit(limit, &revs, UNINTERESTING))
1335 die("Unknown commit %s", limit);
1337 /* reverse the list of commits */
1338 if (prepare_revision_walk(&revs))
1339 die("revision walk setup failed");
1340 while ((commit = get_revision(&revs)) != NULL) {
1342 if (commit->parents && commit->parents->next)
1345 commit_list_insert(commit, &list);
1351 commit = list->item;
1352 if (has_commit_patch_id(commit, &ids))
1356 struct strbuf buf = STRBUF_INIT;
1357 struct pretty_print_context ctx = {0};
1358 pretty_print_commit(CMIT_FMT_ONELINE, commit,
1360 printf("%c %s %s\n", sign,
1361 sha1_to_hex(commit->object.sha1), buf.buf);
1362 strbuf_release(&buf);
1365 printf("%c %s\n", sign,
1366 sha1_to_hex(commit->object.sha1));
1372 free_patch_ids(&ids);