format-patch: don't pass on the --quiet flag
[git.git] / builtin / log.c
blob691d5f0dff39378e4f3746f8a05e989dc9e4a561
1 /*
2 * Builtin "git log" and related commands (show, whatchanged)
4 * (C) Copyright 2006 Linus Torvalds
5 * 2006 Junio Hamano
6 */
7 #include "cache.h"
8 #include "color.h"
9 #include "commit.h"
10 #include "diff.h"
11 #include "revision.h"
12 #include "log-tree.h"
13 #include "builtin.h"
14 #include "tag.h"
15 #include "reflog-walk.h"
16 #include "patch-ids.h"
17 #include "run-command.h"
18 #include "shortlog.h"
19 #include "remote.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 const char *fmt_patch_subject_prefix = "PATCH";
29 static const char *fmt_pretty;
31 static const char * const builtin_log_usage =
32 "git log [<options>] [<since>..<until>] [[--] <path>...]\n"
33 " or: git show [options] <object>...";
35 static int parse_decoration_style(const char *var, const char *value)
37 switch (git_config_maybe_bool(var, value)) {
38 case 1:
39 return DECORATE_SHORT_REFS;
40 case 0:
41 return 0;
42 default:
43 break;
45 if (!strcmp(value, "full"))
46 return DECORATE_FULL_REFS;
47 else if (!strcmp(value, "short"))
48 return DECORATE_SHORT_REFS;
49 return -1;
52 static void cmd_log_init(int argc, const char **argv, const char *prefix,
53 struct rev_info *rev, struct setup_revision_opt *opt)
55 int i;
56 int decoration_given = 0;
57 struct userformat_want w;
59 rev->abbrev = DEFAULT_ABBREV;
60 rev->commit_format = CMIT_FMT_DEFAULT;
61 if (fmt_pretty)
62 get_commit_format(fmt_pretty, rev);
63 rev->verbose_header = 1;
64 DIFF_OPT_SET(&rev->diffopt, RECURSIVE);
65 rev->show_root_diff = default_show_root;
66 rev->subject_prefix = fmt_patch_subject_prefix;
67 DIFF_OPT_SET(&rev->diffopt, ALLOW_TEXTCONV);
69 if (default_date_mode)
70 rev->date_mode = parse_date_format(default_date_mode);
73 * Check for -h before setup_revisions(), or "git log -h" will
74 * fail when run without a git directory.
76 if (argc == 2 && !strcmp(argv[1], "-h"))
77 usage(builtin_log_usage);
78 argc = setup_revisions(argc, argv, rev, opt);
80 memset(&w, 0, sizeof(w));
81 userformat_find_requirements(NULL, &w);
83 if (!rev->show_notes_given && (!rev->pretty_given || w.notes))
84 rev->show_notes = 1;
85 if (rev->show_notes)
86 init_display_notes(&rev->notes_opt);
88 if (rev->diffopt.pickaxe || rev->diffopt.filter)
89 rev->always_show_header = 0;
90 if (DIFF_OPT_TST(&rev->diffopt, FOLLOW_RENAMES)) {
91 rev->always_show_header = 0;
92 if (rev->diffopt.pathspec.nr != 1)
93 usage("git logs can only follow renames on one pathname at a time");
95 for (i = 1; i < argc; i++) {
96 const char *arg = argv[i];
97 if (!strcmp(arg, "--decorate")) {
98 decoration_style = DECORATE_SHORT_REFS;
99 decoration_given = 1;
100 } else if (!prefixcmp(arg, "--decorate=")) {
101 const char *v = skip_prefix(arg, "--decorate=");
102 decoration_style = parse_decoration_style(arg, v);
103 if (decoration_style < 0)
104 die(_("invalid --decorate option: %s"), arg);
105 decoration_given = 1;
106 } else if (!strcmp(arg, "--no-decorate")) {
107 decoration_style = 0;
108 } else if (!strcmp(arg, "--source")) {
109 rev->show_source = 1;
110 } else if (!strcmp(arg, "-h")) {
111 usage(builtin_log_usage);
112 } else
113 die(_("unrecognized argument: %s"), arg);
117 * defeat log.decorate configuration interacting with --pretty=raw
118 * from the command line.
120 if (!decoration_given && rev->pretty_given
121 && rev->commit_format == CMIT_FMT_RAW)
122 decoration_style = 0;
124 if (decoration_style) {
125 rev->show_decorations = 1;
126 load_ref_decorations(decoration_style);
128 setup_pager();
132 * This gives a rough estimate for how many commits we
133 * will print out in the list.
135 static int estimate_commit_count(struct rev_info *rev, struct commit_list *list)
137 int n = 0;
139 while (list) {
140 struct commit *commit = list->item;
141 unsigned int flags = commit->object.flags;
142 list = list->next;
143 if (!(flags & (TREESAME | UNINTERESTING)))
144 n++;
146 return n;
149 static void show_early_header(struct rev_info *rev, const char *stage, int nr)
151 if (rev->shown_one) {
152 rev->shown_one = 0;
153 if (rev->commit_format != CMIT_FMT_ONELINE)
154 putchar(rev->diffopt.line_termination);
156 printf(_("Final output: %d %s\n"), nr, stage);
159 static struct itimerval early_output_timer;
161 static void log_show_early(struct rev_info *revs, struct commit_list *list)
163 int i = revs->early_output;
164 int show_header = 1;
166 sort_in_topological_order(&list, revs->lifo);
167 while (list && i) {
168 struct commit *commit = list->item;
169 switch (simplify_commit(revs, commit)) {
170 case commit_show:
171 if (show_header) {
172 int n = estimate_commit_count(revs, list);
173 show_early_header(revs, "incomplete", n);
174 show_header = 0;
176 log_tree_commit(revs, commit);
177 i--;
178 break;
179 case commit_ignore:
180 break;
181 case commit_error:
182 return;
184 list = list->next;
187 /* Did we already get enough commits for the early output? */
188 if (!i)
189 return;
192 * ..if no, then repeat it twice a second until we
193 * do.
195 * NOTE! We don't use "it_interval", because if the
196 * reader isn't listening, we want our output to be
197 * throttled by the writing, and not have the timer
198 * trigger every second even if we're blocked on a
199 * reader!
201 early_output_timer.it_value.tv_sec = 0;
202 early_output_timer.it_value.tv_usec = 500000;
203 setitimer(ITIMER_REAL, &early_output_timer, NULL);
206 static void early_output(int signal)
208 show_early_output = log_show_early;
211 static void setup_early_output(struct rev_info *rev)
213 struct sigaction sa;
216 * Set up the signal handler, minimally intrusively:
217 * we only set a single volatile integer word (not
218 * using sigatomic_t - trying to avoid unnecessary
219 * system dependencies and headers), and using
220 * SA_RESTART.
222 memset(&sa, 0, sizeof(sa));
223 sa.sa_handler = early_output;
224 sigemptyset(&sa.sa_mask);
225 sa.sa_flags = SA_RESTART;
226 sigaction(SIGALRM, &sa, NULL);
229 * If we can get the whole output in less than a
230 * tenth of a second, don't even bother doing the
231 * early-output thing..
233 * This is a one-time-only trigger.
235 early_output_timer.it_value.tv_sec = 0;
236 early_output_timer.it_value.tv_usec = 100000;
237 setitimer(ITIMER_REAL, &early_output_timer, NULL);
240 static void finish_early_output(struct rev_info *rev)
242 int n = estimate_commit_count(rev, rev->commits);
243 signal(SIGALRM, SIG_IGN);
244 show_early_header(rev, "done", n);
247 static int cmd_log_walk(struct rev_info *rev)
249 struct commit *commit;
251 if (rev->early_output)
252 setup_early_output(rev);
254 if (prepare_revision_walk(rev))
255 die(_("revision walk setup failed"));
257 if (rev->early_output)
258 finish_early_output(rev);
261 * For --check and --exit-code, the exit code is based on CHECK_FAILED
262 * and HAS_CHANGES being accumulated in rev->diffopt, so be careful to
263 * retain that state information if replacing rev->diffopt in this loop
265 while ((commit = get_revision(rev)) != NULL) {
266 if (!log_tree_commit(rev, commit) &&
267 rev->max_count >= 0)
269 * We decremented max_count in get_revision,
270 * but we didn't actually show the commit.
272 rev->max_count++;
273 if (!rev->reflog_info) {
274 /* we allow cycles in reflog ancestry */
275 free(commit->buffer);
276 commit->buffer = NULL;
278 free_commit_list(commit->parents);
279 commit->parents = NULL;
281 if (rev->diffopt.output_format & DIFF_FORMAT_CHECKDIFF &&
282 DIFF_OPT_TST(&rev->diffopt, CHECK_FAILED)) {
283 return 02;
285 return diff_result_code(&rev->diffopt, 0);
288 static int git_log_config(const char *var, const char *value, void *cb)
290 if (!strcmp(var, "format.pretty"))
291 return git_config_string(&fmt_pretty, var, value);
292 if (!strcmp(var, "format.subjectprefix"))
293 return git_config_string(&fmt_patch_subject_prefix, var, value);
294 if (!strcmp(var, "log.date"))
295 return git_config_string(&default_date_mode, var, value);
296 if (!strcmp(var, "log.decorate")) {
297 decoration_style = parse_decoration_style(var, value);
298 if (decoration_style < 0)
299 decoration_style = 0; /* maybe warn? */
300 return 0;
302 if (!strcmp(var, "log.showroot")) {
303 default_show_root = git_config_bool(var, value);
304 return 0;
306 if (!prefixcmp(var, "color.decorate."))
307 return parse_decorate_color_config(var, 15, value);
309 return git_diff_ui_config(var, value, cb);
312 int cmd_whatchanged(int argc, const char **argv, const char *prefix)
314 struct rev_info rev;
315 struct setup_revision_opt opt;
317 git_config(git_log_config, NULL);
319 if (diff_use_color_default == -1)
320 diff_use_color_default = git_use_color_default;
322 init_revisions(&rev, prefix);
323 rev.diff = 1;
324 rev.simplify_history = 0;
325 memset(&opt, 0, sizeof(opt));
326 opt.def = "HEAD";
327 cmd_log_init(argc, argv, prefix, &rev, &opt);
328 if (!rev.diffopt.output_format)
329 rev.diffopt.output_format = DIFF_FORMAT_RAW;
330 return cmd_log_walk(&rev);
333 static void show_tagger(char *buf, int len, struct rev_info *rev)
335 struct strbuf out = STRBUF_INIT;
337 pp_user_info("Tagger", rev->commit_format, &out, buf, rev->date_mode,
338 get_log_output_encoding());
339 printf("%s", out.buf);
340 strbuf_release(&out);
343 static int show_object(const unsigned char *sha1, int show_tag_object,
344 struct rev_info *rev)
346 unsigned long size;
347 enum object_type type;
348 char *buf = read_sha1_file(sha1, &type, &size);
349 int offset = 0;
351 if (!buf)
352 return error(_("Could not read object %s"), sha1_to_hex(sha1));
354 if (show_tag_object)
355 while (offset < size && buf[offset] != '\n') {
356 int new_offset = offset + 1;
357 while (new_offset < size && buf[new_offset++] != '\n')
358 ; /* do nothing */
359 if (!prefixcmp(buf + offset, "tagger "))
360 show_tagger(buf + offset + 7,
361 new_offset - offset - 7, rev);
362 offset = new_offset;
365 if (offset < size)
366 fwrite(buf + offset, size - offset, 1, stdout);
367 free(buf);
368 return 0;
371 static int show_tree_object(const unsigned char *sha1,
372 const char *base, int baselen,
373 const char *pathname, unsigned mode, int stage, void *context)
375 printf("%s%s\n", pathname, S_ISDIR(mode) ? "/" : "");
376 return 0;
379 static void show_rev_tweak_rev(struct rev_info *rev, struct setup_revision_opt *opt)
381 if (rev->ignore_merges) {
382 /* There was no "-m" on the command line */
383 rev->ignore_merges = 0;
384 if (!rev->first_parent_only && !rev->combine_merges) {
385 /* No "--first-parent", "-c", nor "--cc" */
386 rev->combine_merges = 1;
387 rev->dense_combined_merges = 1;
390 if (!rev->diffopt.output_format)
391 rev->diffopt.output_format = DIFF_FORMAT_PATCH;
394 int cmd_show(int argc, const char **argv, const char *prefix)
396 struct rev_info rev;
397 struct object_array_entry *objects;
398 struct setup_revision_opt opt;
399 int i, count, ret = 0;
401 git_config(git_log_config, NULL);
403 if (diff_use_color_default == -1)
404 diff_use_color_default = git_use_color_default;
406 init_revisions(&rev, prefix);
407 rev.diff = 1;
408 rev.always_show_header = 1;
409 rev.no_walk = 1;
410 memset(&opt, 0, sizeof(opt));
411 opt.def = "HEAD";
412 opt.tweak = show_rev_tweak_rev;
413 cmd_log_init(argc, argv, prefix, &rev, &opt);
415 count = rev.pending.nr;
416 objects = rev.pending.objects;
417 for (i = 0; i < count && !ret; i++) {
418 struct object *o = objects[i].item;
419 const char *name = objects[i].name;
420 switch (o->type) {
421 case OBJ_BLOB:
422 ret = show_object(o->sha1, 0, NULL);
423 break;
424 case OBJ_TAG: {
425 struct tag *t = (struct tag *)o;
427 if (rev.shown_one)
428 putchar('\n');
429 printf("%stag %s%s\n",
430 diff_get_color_opt(&rev.diffopt, DIFF_COMMIT),
431 t->tag,
432 diff_get_color_opt(&rev.diffopt, DIFF_RESET));
433 ret = show_object(o->sha1, 1, &rev);
434 rev.shown_one = 1;
435 if (ret)
436 break;
437 o = parse_object(t->tagged->sha1);
438 if (!o)
439 ret = error(_("Could not read object %s"),
440 sha1_to_hex(t->tagged->sha1));
441 objects[i].item = o;
442 i--;
443 break;
445 case OBJ_TREE:
446 if (rev.shown_one)
447 putchar('\n');
448 printf("%stree %s%s\n\n",
449 diff_get_color_opt(&rev.diffopt, DIFF_COMMIT),
450 name,
451 diff_get_color_opt(&rev.diffopt, DIFF_RESET));
452 read_tree_recursive((struct tree *)o, "", 0, 0, NULL,
453 show_tree_object, NULL);
454 rev.shown_one = 1;
455 break;
456 case OBJ_COMMIT:
457 rev.pending.nr = rev.pending.alloc = 0;
458 rev.pending.objects = NULL;
459 add_object_array(o, name, &rev.pending);
460 ret = cmd_log_walk(&rev);
461 break;
462 default:
463 ret = error(_("Unknown type: %d"), o->type);
466 free(objects);
467 return ret;
471 * This is equivalent to "git log -g --abbrev-commit --pretty=oneline"
473 int cmd_log_reflog(int argc, const char **argv, const char *prefix)
475 struct rev_info rev;
476 struct setup_revision_opt opt;
478 git_config(git_log_config, NULL);
480 if (diff_use_color_default == -1)
481 diff_use_color_default = git_use_color_default;
483 init_revisions(&rev, prefix);
484 init_reflog_walk(&rev.reflog_info);
485 rev.abbrev_commit = 1;
486 rev.verbose_header = 1;
487 memset(&opt, 0, sizeof(opt));
488 opt.def = "HEAD";
489 cmd_log_init(argc, argv, prefix, &rev, &opt);
492 * This means that we override whatever commit format the user gave
493 * on the cmd line. Sad, but cmd_log_init() currently doesn't
494 * allow us to set a different default.
496 rev.commit_format = CMIT_FMT_ONELINE;
497 rev.use_terminator = 1;
498 rev.always_show_header = 1;
500 return cmd_log_walk(&rev);
503 int cmd_log(int argc, const char **argv, const char *prefix)
505 struct rev_info rev;
506 struct setup_revision_opt opt;
508 git_config(git_log_config, NULL);
510 if (diff_use_color_default == -1)
511 diff_use_color_default = git_use_color_default;
513 init_revisions(&rev, prefix);
514 rev.always_show_header = 1;
515 memset(&opt, 0, sizeof(opt));
516 opt.def = "HEAD";
517 cmd_log_init(argc, argv, prefix, &rev, &opt);
518 return cmd_log_walk(&rev);
521 /* format-patch */
523 static const char *fmt_patch_suffix = ".patch";
524 static int numbered = 0;
525 static int auto_number = 1;
527 static char *default_attach = NULL;
529 static struct string_list extra_hdr;
530 static struct string_list extra_to;
531 static struct string_list extra_cc;
533 static void add_header(const char *value)
535 struct string_list_item *item;
536 int len = strlen(value);
537 while (len && value[len - 1] == '\n')
538 len--;
540 if (!strncasecmp(value, "to: ", 4)) {
541 item = string_list_append(&extra_to, value + 4);
542 len -= 4;
543 } else if (!strncasecmp(value, "cc: ", 4)) {
544 item = string_list_append(&extra_cc, value + 4);
545 len -= 4;
546 } else {
547 item = string_list_append(&extra_hdr, value);
550 item->string[len] = '\0';
553 #define THREAD_SHALLOW 1
554 #define THREAD_DEEP 2
555 static int thread;
556 static int do_signoff;
557 static const char *signature = git_version_string;
559 static int git_format_config(const char *var, const char *value, void *cb)
561 if (!strcmp(var, "format.headers")) {
562 if (!value)
563 die(_("format.headers without value"));
564 add_header(value);
565 return 0;
567 if (!strcmp(var, "format.suffix"))
568 return git_config_string(&fmt_patch_suffix, var, value);
569 if (!strcmp(var, "format.to")) {
570 if (!value)
571 return config_error_nonbool(var);
572 string_list_append(&extra_to, value);
573 return 0;
575 if (!strcmp(var, "format.cc")) {
576 if (!value)
577 return config_error_nonbool(var);
578 string_list_append(&extra_cc, value);
579 return 0;
581 if (!strcmp(var, "diff.color") || !strcmp(var, "color.diff")) {
582 return 0;
584 if (!strcmp(var, "format.numbered")) {
585 if (value && !strcasecmp(value, "auto")) {
586 auto_number = 1;
587 return 0;
589 numbered = git_config_bool(var, value);
590 auto_number = auto_number && numbered;
591 return 0;
593 if (!strcmp(var, "format.attach")) {
594 if (value && *value)
595 default_attach = xstrdup(value);
596 else
597 default_attach = xstrdup(git_version_string);
598 return 0;
600 if (!strcmp(var, "format.thread")) {
601 if (value && !strcasecmp(value, "deep")) {
602 thread = THREAD_DEEP;
603 return 0;
605 if (value && !strcasecmp(value, "shallow")) {
606 thread = THREAD_SHALLOW;
607 return 0;
609 thread = git_config_bool(var, value) && THREAD_SHALLOW;
610 return 0;
612 if (!strcmp(var, "format.signoff")) {
613 do_signoff = git_config_bool(var, value);
614 return 0;
616 if (!strcmp(var, "format.signature"))
617 return git_config_string(&signature, var, value);
619 return git_log_config(var, value, cb);
622 static FILE *realstdout = NULL;
623 static const char *output_directory = NULL;
624 static int outdir_offset;
626 static int reopen_stdout(struct commit *commit, struct rev_info *rev, int quiet)
628 struct strbuf filename = STRBUF_INIT;
629 int suffix_len = strlen(fmt_patch_suffix) + 1;
631 if (output_directory) {
632 strbuf_addstr(&filename, output_directory);
633 if (filename.len >=
634 PATH_MAX - FORMAT_PATCH_NAME_MAX - suffix_len)
635 return error(_("name of output directory is too long"));
636 if (filename.buf[filename.len - 1] != '/')
637 strbuf_addch(&filename, '/');
640 get_patch_filename(commit, rev->nr, fmt_patch_suffix, &filename);
642 if (!quiet)
643 fprintf(realstdout, "%s\n", filename.buf + outdir_offset);
645 if (freopen(filename.buf, "w", stdout) == NULL)
646 return error(_("Cannot open patch file %s"), filename.buf);
648 strbuf_release(&filename);
649 return 0;
652 static void get_patch_ids(struct rev_info *rev, struct patch_ids *ids, const char *prefix)
654 struct rev_info check_rev;
655 struct commit *commit;
656 struct object *o1, *o2;
657 unsigned flags1, flags2;
659 if (rev->pending.nr != 2)
660 die(_("Need exactly one range."));
662 o1 = rev->pending.objects[0].item;
663 flags1 = o1->flags;
664 o2 = rev->pending.objects[1].item;
665 flags2 = o2->flags;
667 if ((flags1 & UNINTERESTING) == (flags2 & UNINTERESTING))
668 die(_("Not a range."));
670 init_patch_ids(ids);
672 /* given a range a..b get all patch ids for b..a */
673 init_revisions(&check_rev, prefix);
674 o1->flags ^= UNINTERESTING;
675 o2->flags ^= UNINTERESTING;
676 add_pending_object(&check_rev, o1, "o1");
677 add_pending_object(&check_rev, o2, "o2");
678 if (prepare_revision_walk(&check_rev))
679 die(_("revision walk setup failed"));
681 while ((commit = get_revision(&check_rev)) != NULL) {
682 /* ignore merges */
683 if (commit->parents && commit->parents->next)
684 continue;
686 add_commit_patch_id(commit, ids);
689 /* reset for next revision walk */
690 clear_commit_marks((struct commit *)o1,
691 SEEN | UNINTERESTING | SHOWN | ADDED);
692 clear_commit_marks((struct commit *)o2,
693 SEEN | UNINTERESTING | SHOWN | ADDED);
694 o1->flags = flags1;
695 o2->flags = flags2;
698 static void gen_message_id(struct rev_info *info, char *base)
700 const char *committer = git_committer_info(IDENT_WARN_ON_NO_NAME);
701 const char *email_start = strrchr(committer, '<');
702 const char *email_end = strrchr(committer, '>');
703 struct strbuf buf = STRBUF_INIT;
704 if (!email_start || !email_end || email_start > email_end - 1)
705 die(_("Could not extract email from committer identity."));
706 strbuf_addf(&buf, "%s.%lu.git.%.*s", base,
707 (unsigned long) time(NULL),
708 (int)(email_end - email_start - 1), email_start + 1);
709 info->message_id = strbuf_detach(&buf, NULL);
712 static void print_signature(void)
714 if (signature && *signature)
715 printf("-- \n%s\n\n", signature);
718 static void make_cover_letter(struct rev_info *rev, int use_stdout,
719 int numbered, int numbered_files,
720 struct commit *origin,
721 int nr, struct commit **list, struct commit *head,
722 int quiet)
724 const char *committer;
725 const char *subject_start = NULL;
726 const char *body = "*** SUBJECT HERE ***\n\n*** BLURB HERE ***\n";
727 const char *msg;
728 const char *extra_headers = rev->extra_headers;
729 struct shortlog log;
730 struct strbuf sb = STRBUF_INIT;
731 int i;
732 const char *encoding = "UTF-8";
733 struct diff_options opts;
734 int need_8bit_cte = 0;
735 struct commit *commit = NULL;
737 if (rev->commit_format != CMIT_FMT_EMAIL)
738 die(_("Cover letter needs email format"));
740 committer = git_committer_info(0);
742 if (!numbered_files) {
744 * We fake a commit for the cover letter so we get the filename
745 * desired.
747 commit = xcalloc(1, sizeof(*commit));
748 commit->buffer = xmalloc(400);
749 snprintf(commit->buffer, 400,
750 "tree 0000000000000000000000000000000000000000\n"
751 "parent %s\n"
752 "author %s\n"
753 "committer %s\n\n"
754 "cover letter\n",
755 sha1_to_hex(head->object.sha1), committer, committer);
758 if (!use_stdout && reopen_stdout(commit, rev, quiet))
759 return;
761 if (commit) {
763 free(commit->buffer);
764 free(commit);
767 log_write_email_headers(rev, head, &subject_start, &extra_headers,
768 &need_8bit_cte);
770 for (i = 0; !need_8bit_cte && i < nr; i++)
771 if (has_non_ascii(list[i]->buffer))
772 need_8bit_cte = 1;
774 msg = body;
775 pp_user_info(NULL, CMIT_FMT_EMAIL, &sb, committer, DATE_RFC2822,
776 encoding);
777 pp_title_line(CMIT_FMT_EMAIL, &msg, &sb, subject_start, extra_headers,
778 encoding, need_8bit_cte);
779 pp_remainder(CMIT_FMT_EMAIL, &msg, &sb, 0);
780 printf("%s\n", sb.buf);
782 strbuf_release(&sb);
784 shortlog_init(&log);
785 log.wrap_lines = 1;
786 log.wrap = 72;
787 log.in1 = 2;
788 log.in2 = 4;
789 for (i = 0; i < nr; i++)
790 shortlog_add_commit(&log, list[i]);
792 shortlog_output(&log);
795 * We can only do diffstat with a unique reference point
797 if (!origin)
798 return;
800 memcpy(&opts, &rev->diffopt, sizeof(opts));
801 opts.output_format = DIFF_FORMAT_SUMMARY | DIFF_FORMAT_DIFFSTAT;
803 diff_setup_done(&opts);
805 diff_tree_sha1(origin->tree->object.sha1,
806 head->tree->object.sha1,
807 "", &opts);
808 diffcore_std(&opts);
809 diff_flush(&opts);
811 printf("\n");
812 print_signature();
815 static const char *clean_message_id(const char *msg_id)
817 char ch;
818 const char *a, *z, *m;
820 m = msg_id;
821 while ((ch = *m) && (isspace(ch) || (ch == '<')))
822 m++;
823 a = m;
824 z = NULL;
825 while ((ch = *m)) {
826 if (!isspace(ch) && (ch != '>'))
827 z = m;
828 m++;
830 if (!z)
831 die(_("insane in-reply-to: %s"), msg_id);
832 if (++z == m)
833 return a;
834 return xmemdupz(a, z - a);
837 static const char *set_outdir(const char *prefix, const char *output_directory)
839 if (output_directory && is_absolute_path(output_directory))
840 return output_directory;
842 if (!prefix || !*prefix) {
843 if (output_directory)
844 return output_directory;
845 /* The user did not explicitly ask for "./" */
846 outdir_offset = 2;
847 return "./";
850 outdir_offset = strlen(prefix);
851 if (!output_directory)
852 return prefix;
854 return xstrdup(prefix_filename(prefix, outdir_offset,
855 output_directory));
858 static const char * const builtin_format_patch_usage[] = {
859 "git format-patch [options] [<since> | <revision range>]",
860 NULL
863 static int keep_subject = 0;
865 static int keep_callback(const struct option *opt, const char *arg, int unset)
867 ((struct rev_info *)opt->value)->total = -1;
868 keep_subject = 1;
869 return 0;
872 static int subject_prefix = 0;
874 static int subject_prefix_callback(const struct option *opt, const char *arg,
875 int unset)
877 subject_prefix = 1;
878 ((struct rev_info *)opt->value)->subject_prefix = arg;
879 return 0;
882 static int numbered_cmdline_opt = 0;
884 static int numbered_callback(const struct option *opt, const char *arg,
885 int unset)
887 *(int *)opt->value = numbered_cmdline_opt = unset ? 0 : 1;
888 if (unset)
889 auto_number = 0;
890 return 0;
893 static int no_numbered_callback(const struct option *opt, const char *arg,
894 int unset)
896 return numbered_callback(opt, arg, 1);
899 static int output_directory_callback(const struct option *opt, const char *arg,
900 int unset)
902 const char **dir = (const char **)opt->value;
903 if (*dir)
904 die(_("Two output directories?"));
905 *dir = arg;
906 return 0;
909 static int thread_callback(const struct option *opt, const char *arg, int unset)
911 int *thread = (int *)opt->value;
912 if (unset)
913 *thread = 0;
914 else if (!arg || !strcmp(arg, "shallow"))
915 *thread = THREAD_SHALLOW;
916 else if (!strcmp(arg, "deep"))
917 *thread = THREAD_DEEP;
918 else
919 return 1;
920 return 0;
923 static int attach_callback(const struct option *opt, const char *arg, int unset)
925 struct rev_info *rev = (struct rev_info *)opt->value;
926 if (unset)
927 rev->mime_boundary = NULL;
928 else if (arg)
929 rev->mime_boundary = arg;
930 else
931 rev->mime_boundary = git_version_string;
932 rev->no_inline = unset ? 0 : 1;
933 return 0;
936 static int inline_callback(const struct option *opt, const char *arg, int unset)
938 struct rev_info *rev = (struct rev_info *)opt->value;
939 if (unset)
940 rev->mime_boundary = NULL;
941 else if (arg)
942 rev->mime_boundary = arg;
943 else
944 rev->mime_boundary = git_version_string;
945 rev->no_inline = 0;
946 return 0;
949 static int header_callback(const struct option *opt, const char *arg, int unset)
951 if (unset) {
952 string_list_clear(&extra_hdr, 0);
953 string_list_clear(&extra_to, 0);
954 string_list_clear(&extra_cc, 0);
955 } else {
956 add_header(arg);
958 return 0;
961 static int to_callback(const struct option *opt, const char *arg, int unset)
963 if (unset)
964 string_list_clear(&extra_to, 0);
965 else
966 string_list_append(&extra_to, arg);
967 return 0;
970 static int cc_callback(const struct option *opt, const char *arg, int unset)
972 if (unset)
973 string_list_clear(&extra_cc, 0);
974 else
975 string_list_append(&extra_cc, arg);
976 return 0;
979 int cmd_format_patch(int argc, const char **argv, const char *prefix)
981 struct commit *commit;
982 struct commit **list = NULL;
983 struct rev_info rev;
984 struct setup_revision_opt s_r_opt;
985 int nr = 0, total, i;
986 int use_stdout = 0;
987 int start_number = -1;
988 int numbered_files = 0; /* _just_ numbers */
989 int ignore_if_in_upstream = 0;
990 int cover_letter = 0;
991 int boundary_count = 0;
992 int no_binary_diff = 0;
993 struct commit *origin = NULL, *head = NULL;
994 const char *in_reply_to = NULL;
995 struct patch_ids ids;
996 char *add_signoff = NULL;
997 struct strbuf buf = STRBUF_INIT;
998 int use_patch_format = 0;
999 int quiet = 0;
1000 const struct option builtin_format_patch_options[] = {
1001 { OPTION_CALLBACK, 'n', "numbered", &numbered, NULL,
1002 "use [PATCH n/m] even with a single patch",
1003 PARSE_OPT_NOARG, numbered_callback },
1004 { OPTION_CALLBACK, 'N', "no-numbered", &numbered, NULL,
1005 "use [PATCH] even with multiple patches",
1006 PARSE_OPT_NOARG, no_numbered_callback },
1007 OPT_BOOLEAN('s', "signoff", &do_signoff, "add Signed-off-by:"),
1008 OPT_BOOLEAN(0, "stdout", &use_stdout,
1009 "print patches to standard out"),
1010 OPT_BOOLEAN(0, "cover-letter", &cover_letter,
1011 "generate a cover letter"),
1012 OPT_BOOLEAN(0, "numbered-files", &numbered_files,
1013 "use simple number sequence for output file names"),
1014 OPT_STRING(0, "suffix", &fmt_patch_suffix, "sfx",
1015 "use <sfx> instead of '.patch'"),
1016 OPT_INTEGER(0, "start-number", &start_number,
1017 "start numbering patches at <n> instead of 1"),
1018 { OPTION_CALLBACK, 0, "subject-prefix", &rev, "prefix",
1019 "Use [<prefix>] instead of [PATCH]",
1020 PARSE_OPT_NONEG, subject_prefix_callback },
1021 { OPTION_CALLBACK, 'o', "output-directory", &output_directory,
1022 "dir", "store resulting files in <dir>",
1023 PARSE_OPT_NONEG, output_directory_callback },
1024 { OPTION_CALLBACK, 'k', "keep-subject", &rev, NULL,
1025 "don't strip/add [PATCH]",
1026 PARSE_OPT_NOARG | PARSE_OPT_NONEG, keep_callback },
1027 OPT_BOOLEAN(0, "no-binary", &no_binary_diff,
1028 "don't output binary diffs"),
1029 OPT_BOOLEAN(0, "ignore-if-in-upstream", &ignore_if_in_upstream,
1030 "don't include a patch matching a commit upstream"),
1031 { OPTION_BOOLEAN, 'p', "no-stat", &use_patch_format, NULL,
1032 "show patch format instead of default (patch + stat)",
1033 PARSE_OPT_NONEG | PARSE_OPT_NOARG },
1034 OPT_GROUP("Messaging"),
1035 { OPTION_CALLBACK, 0, "add-header", NULL, "header",
1036 "add email header", 0, header_callback },
1037 { OPTION_CALLBACK, 0, "to", NULL, "email", "add To: header",
1038 0, to_callback },
1039 { OPTION_CALLBACK, 0, "cc", NULL, "email", "add Cc: header",
1040 0, cc_callback },
1041 OPT_STRING(0, "in-reply-to", &in_reply_to, "message-id",
1042 "make first mail a reply to <message-id>"),
1043 { OPTION_CALLBACK, 0, "attach", &rev, "boundary",
1044 "attach the patch", PARSE_OPT_OPTARG,
1045 attach_callback },
1046 { OPTION_CALLBACK, 0, "inline", &rev, "boundary",
1047 "inline the patch",
1048 PARSE_OPT_OPTARG | PARSE_OPT_NONEG,
1049 inline_callback },
1050 { OPTION_CALLBACK, 0, "thread", &thread, "style",
1051 "enable message threading, styles: shallow, deep",
1052 PARSE_OPT_OPTARG, thread_callback },
1053 OPT_STRING(0, "signature", &signature, "signature",
1054 "add a signature"),
1055 OPT_BOOLEAN(0, "quiet", &quiet,
1056 "don't print the patch filenames"),
1057 OPT_END()
1060 extra_hdr.strdup_strings = 1;
1061 extra_to.strdup_strings = 1;
1062 extra_cc.strdup_strings = 1;
1063 git_config(git_format_config, NULL);
1064 init_revisions(&rev, prefix);
1065 rev.commit_format = CMIT_FMT_EMAIL;
1066 rev.verbose_header = 1;
1067 rev.diff = 1;
1068 rev.max_parents = 1;
1069 DIFF_OPT_SET(&rev.diffopt, RECURSIVE);
1070 rev.subject_prefix = fmt_patch_subject_prefix;
1071 memset(&s_r_opt, 0, sizeof(s_r_opt));
1072 s_r_opt.def = "HEAD";
1074 if (default_attach) {
1075 rev.mime_boundary = default_attach;
1076 rev.no_inline = 1;
1080 * Parse the arguments before setup_revisions(), or something
1081 * like "git format-patch -o a123 HEAD^.." may fail; a123 is
1082 * possibly a valid SHA1.
1084 argc = parse_options(argc, argv, prefix, builtin_format_patch_options,
1085 builtin_format_patch_usage,
1086 PARSE_OPT_KEEP_ARGV0 | PARSE_OPT_KEEP_UNKNOWN |
1087 PARSE_OPT_KEEP_DASHDASH);
1089 if (do_signoff) {
1090 const char *committer;
1091 const char *endpos;
1092 committer = git_committer_info(IDENT_ERROR_ON_NO_NAME);
1093 endpos = strchr(committer, '>');
1094 if (!endpos)
1095 die(_("bogus committer info %s"), committer);
1096 add_signoff = xmemdupz(committer, endpos - committer + 1);
1099 for (i = 0; i < extra_hdr.nr; i++) {
1100 strbuf_addstr(&buf, extra_hdr.items[i].string);
1101 strbuf_addch(&buf, '\n');
1104 if (extra_to.nr)
1105 strbuf_addstr(&buf, "To: ");
1106 for (i = 0; i < extra_to.nr; i++) {
1107 if (i)
1108 strbuf_addstr(&buf, " ");
1109 strbuf_addstr(&buf, extra_to.items[i].string);
1110 if (i + 1 < extra_to.nr)
1111 strbuf_addch(&buf, ',');
1112 strbuf_addch(&buf, '\n');
1115 if (extra_cc.nr)
1116 strbuf_addstr(&buf, "Cc: ");
1117 for (i = 0; i < extra_cc.nr; i++) {
1118 if (i)
1119 strbuf_addstr(&buf, " ");
1120 strbuf_addstr(&buf, extra_cc.items[i].string);
1121 if (i + 1 < extra_cc.nr)
1122 strbuf_addch(&buf, ',');
1123 strbuf_addch(&buf, '\n');
1126 rev.extra_headers = strbuf_detach(&buf, NULL);
1128 if (start_number < 0)
1129 start_number = 1;
1132 * If numbered is set solely due to format.numbered in config,
1133 * and it would conflict with --keep-subject (-k) from the
1134 * command line, reset "numbered".
1136 if (numbered && keep_subject && !numbered_cmdline_opt)
1137 numbered = 0;
1139 if (numbered && keep_subject)
1140 die (_("-n and -k are mutually exclusive."));
1141 if (keep_subject && subject_prefix)
1142 die (_("--subject-prefix and -k are mutually exclusive."));
1144 argc = setup_revisions(argc, argv, &rev, &s_r_opt);
1145 if (argc > 1)
1146 die (_("unrecognized argument: %s"), argv[1]);
1148 if (rev.diffopt.output_format & DIFF_FORMAT_NAME)
1149 die(_("--name-only does not make sense"));
1150 if (rev.diffopt.output_format & DIFF_FORMAT_NAME_STATUS)
1151 die(_("--name-status does not make sense"));
1152 if (rev.diffopt.output_format & DIFF_FORMAT_CHECKDIFF)
1153 die(_("--check does not make sense"));
1155 if (!use_patch_format &&
1156 (!rev.diffopt.output_format ||
1157 rev.diffopt.output_format == DIFF_FORMAT_PATCH))
1158 rev.diffopt.output_format = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_SUMMARY;
1160 /* Always generate a patch */
1161 rev.diffopt.output_format |= DIFF_FORMAT_PATCH;
1163 if (!DIFF_OPT_TST(&rev.diffopt, TEXT) && !no_binary_diff)
1164 DIFF_OPT_SET(&rev.diffopt, BINARY);
1166 if (rev.show_notes)
1167 init_display_notes(&rev.notes_opt);
1169 if (!use_stdout)
1170 output_directory = set_outdir(prefix, output_directory);
1171 else
1172 setup_pager();
1174 if (output_directory) {
1175 if (use_stdout)
1176 die(_("standard output, or directory, which one?"));
1177 if (mkdir(output_directory, 0777) < 0 && errno != EEXIST)
1178 die_errno(_("Could not create directory '%s'"),
1179 output_directory);
1182 if (rev.pending.nr == 1) {
1183 if (rev.max_count < 0 && !rev.show_root_diff) {
1185 * This is traditional behaviour of "git format-patch
1186 * origin" that prepares what the origin side still
1187 * does not have.
1189 rev.pending.objects[0].item->flags |= UNINTERESTING;
1190 add_head_to_pending(&rev);
1193 * Otherwise, it is "format-patch -22 HEAD", and/or
1194 * "format-patch --root HEAD". The user wants
1195 * get_revision() to do the usual traversal.
1200 * We cannot move this anywhere earlier because we do want to
1201 * know if --root was given explicitly from the command line.
1203 rev.show_root_diff = 1;
1205 if (cover_letter) {
1206 /* remember the range */
1207 int i;
1208 for (i = 0; i < rev.pending.nr; i++) {
1209 struct object *o = rev.pending.objects[i].item;
1210 if (!(o->flags & UNINTERESTING))
1211 head = (struct commit *)o;
1213 /* We can't generate a cover letter without any patches */
1214 if (!head)
1215 return 0;
1218 if (ignore_if_in_upstream) {
1219 /* Don't say anything if head and upstream are the same. */
1220 if (rev.pending.nr == 2) {
1221 struct object_array_entry *o = rev.pending.objects;
1222 if (hashcmp(o[0].item->sha1, o[1].item->sha1) == 0)
1223 return 0;
1225 get_patch_ids(&rev, &ids, prefix);
1228 if (!use_stdout)
1229 realstdout = xfdopen(xdup(1), "w");
1231 if (prepare_revision_walk(&rev))
1232 die(_("revision walk setup failed"));
1233 rev.boundary = 1;
1234 while ((commit = get_revision(&rev)) != NULL) {
1235 if (commit->object.flags & BOUNDARY) {
1236 boundary_count++;
1237 origin = (boundary_count == 1) ? commit : NULL;
1238 continue;
1241 if (ignore_if_in_upstream &&
1242 has_commit_patch_id(commit, &ids))
1243 continue;
1245 nr++;
1246 list = xrealloc(list, nr * sizeof(list[0]));
1247 list[nr - 1] = commit;
1249 total = nr;
1250 if (!keep_subject && auto_number && total > 1)
1251 numbered = 1;
1252 if (numbered)
1253 rev.total = total + start_number - 1;
1254 if (in_reply_to || thread || cover_letter)
1255 rev.ref_message_ids = xcalloc(1, sizeof(struct string_list));
1256 if (in_reply_to) {
1257 const char *msgid = clean_message_id(in_reply_to);
1258 string_list_append(rev.ref_message_ids, msgid);
1260 rev.numbered_files = numbered_files;
1261 rev.patch_suffix = fmt_patch_suffix;
1262 if (cover_letter) {
1263 if (thread)
1264 gen_message_id(&rev, "cover");
1265 make_cover_letter(&rev, use_stdout, numbered, numbered_files,
1266 origin, nr, list, head, quiet);
1267 total++;
1268 start_number--;
1270 rev.add_signoff = add_signoff;
1271 while (0 <= --nr) {
1272 int shown;
1273 commit = list[nr];
1274 rev.nr = total - nr + (start_number - 1);
1275 /* Make the second and subsequent mails replies to the first */
1276 if (thread) {
1277 /* Have we already had a message ID? */
1278 if (rev.message_id) {
1280 * For deep threading: make every mail
1281 * a reply to the previous one, no
1282 * matter what other options are set.
1284 * For shallow threading:
1286 * Without --cover-letter and
1287 * --in-reply-to, make every mail a
1288 * reply to the one before.
1290 * With --in-reply-to but no
1291 * --cover-letter, make every mail a
1292 * reply to the <reply-to>.
1294 * With --cover-letter, make every
1295 * mail but the cover letter a reply
1296 * to the cover letter. The cover
1297 * letter is a reply to the
1298 * --in-reply-to, if specified.
1300 if (thread == THREAD_SHALLOW
1301 && rev.ref_message_ids->nr > 0
1302 && (!cover_letter || rev.nr > 1))
1303 free(rev.message_id);
1304 else
1305 string_list_append(rev.ref_message_ids,
1306 rev.message_id);
1308 gen_message_id(&rev, sha1_to_hex(commit->object.sha1));
1311 if (!use_stdout && reopen_stdout(numbered_files ? NULL : commit,
1312 &rev, quiet))
1313 die(_("Failed to create output files"));
1314 shown = log_tree_commit(&rev, commit);
1315 free(commit->buffer);
1316 commit->buffer = NULL;
1318 /* We put one extra blank line between formatted
1319 * patches and this flag is used by log-tree code
1320 * to see if it needs to emit a LF before showing
1321 * the log; when using one file per patch, we do
1322 * not want the extra blank line.
1324 if (!use_stdout)
1325 rev.shown_one = 0;
1326 if (shown) {
1327 if (rev.mime_boundary)
1328 printf("\n--%s%s--\n\n\n",
1329 mime_boundary_leader,
1330 rev.mime_boundary);
1331 else
1332 print_signature();
1334 if (!use_stdout)
1335 fclose(stdout);
1337 free(list);
1338 string_list_clear(&extra_to, 0);
1339 string_list_clear(&extra_cc, 0);
1340 string_list_clear(&extra_hdr, 0);
1341 if (ignore_if_in_upstream)
1342 free_patch_ids(&ids);
1343 return 0;
1346 static int add_pending_commit(const char *arg, struct rev_info *revs, int flags)
1348 unsigned char sha1[20];
1349 if (get_sha1(arg, sha1) == 0) {
1350 struct commit *commit = lookup_commit_reference(sha1);
1351 if (commit) {
1352 commit->object.flags |= flags;
1353 add_pending_object(revs, &commit->object, arg);
1354 return 0;
1357 return -1;
1360 static const char * const cherry_usage[] = {
1361 "git cherry [-v] [<upstream> [<head> [<limit>]]]",
1362 NULL
1365 static void print_commit(char sign, struct commit *commit, int verbose,
1366 int abbrev)
1368 if (!verbose) {
1369 printf("%c %s\n", sign,
1370 find_unique_abbrev(commit->object.sha1, abbrev));
1371 } else {
1372 struct strbuf buf = STRBUF_INIT;
1373 struct pretty_print_context ctx = {0};
1374 pretty_print_commit(CMIT_FMT_ONELINE, commit, &buf, &ctx);
1375 printf("%c %s %s\n", sign,
1376 find_unique_abbrev(commit->object.sha1, abbrev),
1377 buf.buf);
1378 strbuf_release(&buf);
1382 int cmd_cherry(int argc, const char **argv, const char *prefix)
1384 struct rev_info revs;
1385 struct patch_ids ids;
1386 struct commit *commit;
1387 struct commit_list *list = NULL;
1388 struct branch *current_branch;
1389 const char *upstream;
1390 const char *head = "HEAD";
1391 const char *limit = NULL;
1392 int verbose = 0, abbrev = 0;
1394 struct option options[] = {
1395 OPT__ABBREV(&abbrev),
1396 OPT__VERBOSE(&verbose, "be verbose"),
1397 OPT_END()
1400 argc = parse_options(argc, argv, prefix, options, cherry_usage, 0);
1402 switch (argc) {
1403 case 3:
1404 limit = argv[2];
1405 /* FALLTHROUGH */
1406 case 2:
1407 head = argv[1];
1408 /* FALLTHROUGH */
1409 case 1:
1410 upstream = argv[0];
1411 break;
1412 default:
1413 current_branch = branch_get(NULL);
1414 if (!current_branch || !current_branch->merge
1415 || !current_branch->merge[0]
1416 || !current_branch->merge[0]->dst) {
1417 fprintf(stderr, _("Could not find a tracked"
1418 " remote branch, please"
1419 " specify <upstream> manually.\n"));
1420 usage_with_options(cherry_usage, options);
1423 upstream = current_branch->merge[0]->dst;
1426 init_revisions(&revs, prefix);
1427 revs.diff = 1;
1428 revs.combine_merges = 0;
1429 revs.ignore_merges = 1;
1430 DIFF_OPT_SET(&revs.diffopt, RECURSIVE);
1432 if (add_pending_commit(head, &revs, 0))
1433 die(_("Unknown commit %s"), head);
1434 if (add_pending_commit(upstream, &revs, UNINTERESTING))
1435 die(_("Unknown commit %s"), upstream);
1437 /* Don't say anything if head and upstream are the same. */
1438 if (revs.pending.nr == 2) {
1439 struct object_array_entry *o = revs.pending.objects;
1440 if (hashcmp(o[0].item->sha1, o[1].item->sha1) == 0)
1441 return 0;
1444 get_patch_ids(&revs, &ids, prefix);
1446 if (limit && add_pending_commit(limit, &revs, UNINTERESTING))
1447 die(_("Unknown commit %s"), limit);
1449 /* reverse the list of commits */
1450 if (prepare_revision_walk(&revs))
1451 die(_("revision walk setup failed"));
1452 while ((commit = get_revision(&revs)) != NULL) {
1453 /* ignore merges */
1454 if (commit->parents && commit->parents->next)
1455 continue;
1457 commit_list_insert(commit, &list);
1460 while (list) {
1461 char sign = '+';
1463 commit = list->item;
1464 if (has_commit_patch_id(commit, &ids))
1465 sign = '-';
1466 print_commit(sign, commit, verbose, abbrev);
1467 list = list->next;
1470 free_patch_ids(&ids);
1471 return 0;