gitk: Second try to work around the command line limit on Windows
[git/dscho.git] / builtin / log.c
blob046f3e4adaf7e34633851e6c2e5a45c38753a306
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;
58 rev->abbrev = DEFAULT_ABBREV;
59 rev->commit_format = CMIT_FMT_DEFAULT;
60 if (fmt_pretty)
61 get_commit_format(fmt_pretty, rev);
62 rev->verbose_header = 1;
63 DIFF_OPT_SET(&rev->diffopt, RECURSIVE);
64 rev->show_root_diff = default_show_root;
65 rev->subject_prefix = fmt_patch_subject_prefix;
66 DIFF_OPT_SET(&rev->diffopt, ALLOW_TEXTCONV);
68 if (default_date_mode)
69 rev->date_mode = parse_date_format(default_date_mode);
72 * Check for -h before setup_revisions(), or "git log -h" will
73 * fail when run without a git directory.
75 if (argc == 2 && !strcmp(argv[1], "-h"))
76 usage(builtin_log_usage);
77 argc = setup_revisions(argc, argv, rev, opt);
79 if (!rev->show_notes_given && !rev->pretty_given)
80 rev->show_notes = 1;
81 if (rev->show_notes)
82 init_display_notes(&rev->notes_opt);
84 if (rev->diffopt.pickaxe || rev->diffopt.filter)
85 rev->always_show_header = 0;
86 if (DIFF_OPT_TST(&rev->diffopt, FOLLOW_RENAMES)) {
87 rev->always_show_header = 0;
88 if (rev->diffopt.nr_paths != 1)
89 usage("git logs can only follow renames on one pathname at a time");
91 for (i = 1; i < argc; i++) {
92 const char *arg = argv[i];
93 if (!strcmp(arg, "--decorate")) {
94 decoration_style = DECORATE_SHORT_REFS;
95 decoration_given = 1;
96 } else if (!prefixcmp(arg, "--decorate=")) {
97 const char *v = skip_prefix(arg, "--decorate=");
98 decoration_style = parse_decoration_style(arg, v);
99 if (decoration_style < 0)
100 die("invalid --decorate option: %s", arg);
101 decoration_given = 1;
102 } else if (!strcmp(arg, "--no-decorate")) {
103 decoration_style = 0;
104 } else if (!strcmp(arg, "--source")) {
105 rev->show_source = 1;
106 } else if (!strcmp(arg, "-h")) {
107 usage(builtin_log_usage);
108 } else
109 die("unrecognized argument: %s", arg);
113 * defeat log.decorate configuration interacting with --pretty
114 * from the command line.
116 if (!decoration_given && rev->pretty_given)
117 decoration_style = 0;
119 if (decoration_style) {
120 rev->show_decorations = 1;
121 load_ref_decorations(decoration_style);
126 * This gives a rough estimate for how many commits we
127 * will print out in the list.
129 static int estimate_commit_count(struct rev_info *rev, struct commit_list *list)
131 int n = 0;
133 while (list) {
134 struct commit *commit = list->item;
135 unsigned int flags = commit->object.flags;
136 list = list->next;
137 if (!(flags & (TREESAME | UNINTERESTING)))
138 n++;
140 return n;
143 static void show_early_header(struct rev_info *rev, const char *stage, int nr)
145 if (rev->shown_one) {
146 rev->shown_one = 0;
147 if (rev->commit_format != CMIT_FMT_ONELINE)
148 putchar(rev->diffopt.line_termination);
150 printf("Final output: %d %s\n", nr, stage);
153 static struct itimerval early_output_timer;
155 static void log_show_early(struct rev_info *revs, struct commit_list *list)
157 int i = revs->early_output;
158 int show_header = 1;
160 sort_in_topological_order(&list, revs->lifo);
161 while (list && i) {
162 struct commit *commit = list->item;
163 switch (simplify_commit(revs, commit)) {
164 case commit_show:
165 if (show_header) {
166 int n = estimate_commit_count(revs, list);
167 show_early_header(revs, "incomplete", n);
168 show_header = 0;
170 log_tree_commit(revs, commit);
171 i--;
172 break;
173 case commit_ignore:
174 break;
175 case commit_error:
176 return;
178 list = list->next;
181 /* Did we already get enough commits for the early output? */
182 if (!i)
183 return;
186 * ..if no, then repeat it twice a second until we
187 * do.
189 * NOTE! We don't use "it_interval", because if the
190 * reader isn't listening, we want our output to be
191 * throttled by the writing, and not have the timer
192 * trigger every second even if we're blocked on a
193 * reader!
195 early_output_timer.it_value.tv_sec = 0;
196 early_output_timer.it_value.tv_usec = 500000;
197 setitimer(ITIMER_REAL, &early_output_timer, NULL);
200 static void early_output(int signal)
202 show_early_output = log_show_early;
205 static void setup_early_output(struct rev_info *rev)
207 struct sigaction sa;
210 * Set up the signal handler, minimally intrusively:
211 * we only set a single volatile integer word (not
212 * using sigatomic_t - trying to avoid unnecessary
213 * system dependencies and headers), and using
214 * SA_RESTART.
216 memset(&sa, 0, sizeof(sa));
217 sa.sa_handler = early_output;
218 sigemptyset(&sa.sa_mask);
219 sa.sa_flags = SA_RESTART;
220 sigaction(SIGALRM, &sa, NULL);
223 * If we can get the whole output in less than a
224 * tenth of a second, don't even bother doing the
225 * early-output thing..
227 * This is a one-time-only trigger.
229 early_output_timer.it_value.tv_sec = 0;
230 early_output_timer.it_value.tv_usec = 100000;
231 setitimer(ITIMER_REAL, &early_output_timer, NULL);
234 static void finish_early_output(struct rev_info *rev)
236 int n = estimate_commit_count(rev, rev->commits);
237 signal(SIGALRM, SIG_IGN);
238 show_early_header(rev, "done", n);
241 static int cmd_log_walk(struct rev_info *rev)
243 struct commit *commit;
245 if (rev->early_output)
246 setup_early_output(rev);
248 if (prepare_revision_walk(rev))
249 die("revision walk setup failed");
251 if (rev->early_output)
252 finish_early_output(rev);
255 * For --check and --exit-code, the exit code is based on CHECK_FAILED
256 * and HAS_CHANGES being accumulated in rev->diffopt, so be careful to
257 * retain that state information if replacing rev->diffopt in this loop
259 while ((commit = get_revision(rev)) != NULL) {
260 log_tree_commit(rev, commit);
261 if (!rev->reflog_info) {
262 /* we allow cycles in reflog ancestry */
263 free(commit->buffer);
264 commit->buffer = NULL;
266 free_commit_list(commit->parents);
267 commit->parents = NULL;
269 if (rev->diffopt.output_format & DIFF_FORMAT_CHECKDIFF &&
270 DIFF_OPT_TST(&rev->diffopt, CHECK_FAILED)) {
271 return 02;
273 return diff_result_code(&rev->diffopt, 0);
276 static int git_log_config(const char *var, const char *value, void *cb)
278 if (!strcmp(var, "format.pretty"))
279 return git_config_string(&fmt_pretty, var, value);
280 if (!strcmp(var, "format.subjectprefix"))
281 return git_config_string(&fmt_patch_subject_prefix, var, value);
282 if (!strcmp(var, "log.date"))
283 return git_config_string(&default_date_mode, var, value);
284 if (!strcmp(var, "log.decorate")) {
285 decoration_style = parse_decoration_style(var, value);
286 if (decoration_style < 0)
287 decoration_style = 0; /* maybe warn? */
288 return 0;
290 if (!strcmp(var, "log.showroot")) {
291 default_show_root = git_config_bool(var, value);
292 return 0;
294 return git_diff_ui_config(var, value, cb);
297 int cmd_whatchanged(int argc, const char **argv, const char *prefix)
299 struct rev_info rev;
300 struct setup_revision_opt opt;
302 git_config(git_log_config, NULL);
304 if (diff_use_color_default == -1)
305 diff_use_color_default = git_use_color_default;
307 init_revisions(&rev, prefix);
308 rev.diff = 1;
309 rev.simplify_history = 0;
310 memset(&opt, 0, sizeof(opt));
311 opt.def = "HEAD";
312 cmd_log_init(argc, argv, prefix, &rev, &opt);
313 if (!rev.diffopt.output_format)
314 rev.diffopt.output_format = DIFF_FORMAT_RAW;
315 return cmd_log_walk(&rev);
318 static void show_tagger(char *buf, int len, struct rev_info *rev)
320 struct strbuf out = STRBUF_INIT;
322 pp_user_info("Tagger", rev->commit_format, &out, buf, rev->date_mode,
323 git_log_output_encoding ?
324 git_log_output_encoding: git_commit_encoding);
325 printf("%s", out.buf);
326 strbuf_release(&out);
329 static int show_object(const unsigned char *sha1, int show_tag_object,
330 struct rev_info *rev)
332 unsigned long size;
333 enum object_type type;
334 char *buf = read_sha1_file(sha1, &type, &size);
335 int offset = 0;
337 if (!buf)
338 return error("Could not read object %s", sha1_to_hex(sha1));
340 if (show_tag_object)
341 while (offset < size && buf[offset] != '\n') {
342 int new_offset = offset + 1;
343 while (new_offset < size && buf[new_offset++] != '\n')
344 ; /* do nothing */
345 if (!prefixcmp(buf + offset, "tagger "))
346 show_tagger(buf + offset + 7,
347 new_offset - offset - 7, rev);
348 offset = new_offset;
351 if (offset < size)
352 fwrite(buf + offset, size - offset, 1, stdout);
353 free(buf);
354 return 0;
357 static int show_tree_object(const unsigned char *sha1,
358 const char *base, int baselen,
359 const char *pathname, unsigned mode, int stage, void *context)
361 printf("%s%s\n", pathname, S_ISDIR(mode) ? "/" : "");
362 return 0;
365 static void show_rev_tweak_rev(struct rev_info *rev, struct setup_revision_opt *opt)
367 if (rev->ignore_merges) {
368 /* There was no "-m" on the command line */
369 rev->ignore_merges = 0;
370 if (!rev->first_parent_only && !rev->combine_merges) {
371 /* No "--first-parent", "-c", nor "--cc" */
372 rev->combine_merges = 1;
373 rev->dense_combined_merges = 1;
376 if (!rev->diffopt.output_format)
377 rev->diffopt.output_format = DIFF_FORMAT_PATCH;
380 int cmd_show(int argc, const char **argv, const char *prefix)
382 struct rev_info rev;
383 struct object_array_entry *objects;
384 struct setup_revision_opt opt;
385 int i, count, ret = 0;
387 git_config(git_log_config, NULL);
389 if (diff_use_color_default == -1)
390 diff_use_color_default = git_use_color_default;
392 init_revisions(&rev, prefix);
393 rev.diff = 1;
394 rev.always_show_header = 1;
395 rev.no_walk = 1;
396 memset(&opt, 0, sizeof(opt));
397 opt.def = "HEAD";
398 opt.tweak = show_rev_tweak_rev;
399 cmd_log_init(argc, argv, prefix, &rev, &opt);
401 count = rev.pending.nr;
402 objects = rev.pending.objects;
403 for (i = 0; i < count && !ret; i++) {
404 struct object *o = objects[i].item;
405 const char *name = objects[i].name;
406 switch (o->type) {
407 case OBJ_BLOB:
408 ret = show_object(o->sha1, 0, NULL);
409 break;
410 case OBJ_TAG: {
411 struct tag *t = (struct tag *)o;
413 if (rev.shown_one)
414 putchar('\n');
415 printf("%stag %s%s\n",
416 diff_get_color_opt(&rev.diffopt, DIFF_COMMIT),
417 t->tag,
418 diff_get_color_opt(&rev.diffopt, DIFF_RESET));
419 ret = show_object(o->sha1, 1, &rev);
420 rev.shown_one = 1;
421 if (ret)
422 break;
423 o = parse_object(t->tagged->sha1);
424 if (!o)
425 ret = error("Could not read object %s",
426 sha1_to_hex(t->tagged->sha1));
427 objects[i].item = o;
428 i--;
429 break;
431 case OBJ_TREE:
432 if (rev.shown_one)
433 putchar('\n');
434 printf("%stree %s%s\n\n",
435 diff_get_color_opt(&rev.diffopt, DIFF_COMMIT),
436 name,
437 diff_get_color_opt(&rev.diffopt, DIFF_RESET));
438 read_tree_recursive((struct tree *)o, "", 0, 0, NULL,
439 show_tree_object, NULL);
440 rev.shown_one = 1;
441 break;
442 case OBJ_COMMIT:
443 rev.pending.nr = rev.pending.alloc = 0;
444 rev.pending.objects = NULL;
445 add_object_array(o, name, &rev.pending);
446 ret = cmd_log_walk(&rev);
447 break;
448 default:
449 ret = error("Unknown type: %d", o->type);
452 free(objects);
453 return ret;
457 * This is equivalent to "git log -g --abbrev-commit --pretty=oneline"
459 int cmd_log_reflog(int argc, const char **argv, const char *prefix)
461 struct rev_info rev;
462 struct setup_revision_opt opt;
464 git_config(git_log_config, NULL);
466 if (diff_use_color_default == -1)
467 diff_use_color_default = git_use_color_default;
469 init_revisions(&rev, prefix);
470 init_reflog_walk(&rev.reflog_info);
471 rev.abbrev_commit = 1;
472 rev.verbose_header = 1;
473 memset(&opt, 0, sizeof(opt));
474 opt.def = "HEAD";
475 cmd_log_init(argc, argv, prefix, &rev, &opt);
478 * This means that we override whatever commit format the user gave
479 * on the cmd line. Sad, but cmd_log_init() currently doesn't
480 * allow us to set a different default.
482 rev.commit_format = CMIT_FMT_ONELINE;
483 rev.use_terminator = 1;
484 rev.always_show_header = 1;
487 * We get called through "git reflog", so unlike the other log
488 * routines, we need to set up our pager manually..
490 setup_pager();
492 return cmd_log_walk(&rev);
495 int cmd_log(int argc, const char **argv, const char *prefix)
497 struct rev_info rev;
498 struct setup_revision_opt opt;
500 git_config(git_log_config, NULL);
502 if (diff_use_color_default == -1)
503 diff_use_color_default = git_use_color_default;
505 init_revisions(&rev, prefix);
506 rev.always_show_header = 1;
507 memset(&opt, 0, sizeof(opt));
508 opt.def = "HEAD";
509 cmd_log_init(argc, argv, prefix, &rev, &opt);
510 return cmd_log_walk(&rev);
513 /* format-patch */
515 static const char *fmt_patch_suffix = ".patch";
516 static int numbered = 0;
517 static int auto_number = 1;
519 static char *default_attach = NULL;
521 static struct string_list extra_hdr;
522 static struct string_list extra_to;
523 static struct string_list extra_cc;
525 static void add_header(const char *value)
527 struct string_list_item *item;
528 int len = strlen(value);
529 while (len && value[len - 1] == '\n')
530 len--;
532 if (!strncasecmp(value, "to: ", 4)) {
533 item = string_list_append(value + 4, &extra_to);
534 len -= 4;
535 } else if (!strncasecmp(value, "cc: ", 4)) {
536 item = string_list_append(value + 4, &extra_cc);
537 len -= 4;
538 } else {
539 item = string_list_append(value, &extra_hdr);
542 item->string[len] = '\0';
545 #define THREAD_SHALLOW 1
546 #define THREAD_DEEP 2
547 static int thread = 0;
548 static int do_signoff = 0;
550 static int git_format_config(const char *var, const char *value, void *cb)
552 if (!strcmp(var, "format.headers")) {
553 if (!value)
554 die("format.headers without value");
555 add_header(value);
556 return 0;
558 if (!strcmp(var, "format.suffix"))
559 return git_config_string(&fmt_patch_suffix, var, value);
560 if (!strcmp(var, "format.to")) {
561 if (!value)
562 return config_error_nonbool(var);
563 string_list_append(value, &extra_to);
564 return 0;
566 if (!strcmp(var, "format.cc")) {
567 if (!value)
568 return config_error_nonbool(var);
569 string_list_append(value, &extra_cc);
570 return 0;
572 if (!strcmp(var, "diff.color") || !strcmp(var, "color.diff")) {
573 return 0;
575 if (!strcmp(var, "format.numbered")) {
576 if (value && !strcasecmp(value, "auto")) {
577 auto_number = 1;
578 return 0;
580 numbered = git_config_bool(var, value);
581 auto_number = auto_number && numbered;
582 return 0;
584 if (!strcmp(var, "format.attach")) {
585 if (value && *value)
586 default_attach = xstrdup(value);
587 else
588 default_attach = xstrdup(git_version_string);
589 return 0;
591 if (!strcmp(var, "format.thread")) {
592 if (value && !strcasecmp(value, "deep")) {
593 thread = THREAD_DEEP;
594 return 0;
596 if (value && !strcasecmp(value, "shallow")) {
597 thread = THREAD_SHALLOW;
598 return 0;
600 thread = git_config_bool(var, value) && THREAD_SHALLOW;
601 return 0;
603 if (!strcmp(var, "format.signoff")) {
604 do_signoff = git_config_bool(var, value);
605 return 0;
608 return git_log_config(var, value, cb);
611 static FILE *realstdout = NULL;
612 static const char *output_directory = NULL;
613 static int outdir_offset;
615 static int reopen_stdout(struct commit *commit, struct rev_info *rev)
617 struct strbuf filename = STRBUF_INIT;
618 int suffix_len = strlen(fmt_patch_suffix) + 1;
620 if (output_directory) {
621 strbuf_addstr(&filename, output_directory);
622 if (filename.len >=
623 PATH_MAX - FORMAT_PATCH_NAME_MAX - suffix_len)
624 return error("name of output directory is too long");
625 if (filename.buf[filename.len - 1] != '/')
626 strbuf_addch(&filename, '/');
629 get_patch_filename(commit, rev->nr, fmt_patch_suffix, &filename);
631 if (!DIFF_OPT_TST(&rev->diffopt, QUICK))
632 fprintf(realstdout, "%s\n", filename.buf + outdir_offset);
634 if (freopen(filename.buf, "w", stdout) == NULL)
635 return error("Cannot open patch file %s", filename.buf);
637 strbuf_release(&filename);
638 return 0;
641 static void get_patch_ids(struct rev_info *rev, struct patch_ids *ids, const char *prefix)
643 struct rev_info check_rev;
644 struct commit *commit;
645 struct object *o1, *o2;
646 unsigned flags1, flags2;
648 if (rev->pending.nr != 2)
649 die("Need exactly one range.");
651 o1 = rev->pending.objects[0].item;
652 flags1 = o1->flags;
653 o2 = rev->pending.objects[1].item;
654 flags2 = o2->flags;
656 if ((flags1 & UNINTERESTING) == (flags2 & UNINTERESTING))
657 die("Not a range.");
659 init_patch_ids(ids);
661 /* given a range a..b get all patch ids for b..a */
662 init_revisions(&check_rev, prefix);
663 o1->flags ^= UNINTERESTING;
664 o2->flags ^= UNINTERESTING;
665 add_pending_object(&check_rev, o1, "o1");
666 add_pending_object(&check_rev, o2, "o2");
667 if (prepare_revision_walk(&check_rev))
668 die("revision walk setup failed");
670 while ((commit = get_revision(&check_rev)) != NULL) {
671 /* ignore merges */
672 if (commit->parents && commit->parents->next)
673 continue;
675 add_commit_patch_id(commit, ids);
678 /* reset for next revision walk */
679 clear_commit_marks((struct commit *)o1,
680 SEEN | UNINTERESTING | SHOWN | ADDED);
681 clear_commit_marks((struct commit *)o2,
682 SEEN | UNINTERESTING | SHOWN | ADDED);
683 o1->flags = flags1;
684 o2->flags = flags2;
687 static void gen_message_id(struct rev_info *info, char *base)
689 const char *committer = git_committer_info(IDENT_WARN_ON_NO_NAME);
690 const char *email_start = strrchr(committer, '<');
691 const char *email_end = strrchr(committer, '>');
692 struct strbuf buf = STRBUF_INIT;
693 if (!email_start || !email_end || email_start > email_end - 1)
694 die("Could not extract email from committer identity.");
695 strbuf_addf(&buf, "%s.%lu.git.%.*s", base,
696 (unsigned long) time(NULL),
697 (int)(email_end - email_start - 1), email_start + 1);
698 info->message_id = strbuf_detach(&buf, NULL);
701 static void make_cover_letter(struct rev_info *rev, int use_stdout,
702 int numbered, int numbered_files,
703 struct commit *origin,
704 int nr, struct commit **list, struct commit *head)
706 const char *committer;
707 const char *subject_start = NULL;
708 const char *body = "*** SUBJECT HERE ***\n\n*** BLURB HERE ***\n";
709 const char *msg;
710 const char *extra_headers = rev->extra_headers;
711 struct shortlog log;
712 struct strbuf sb = STRBUF_INIT;
713 int i;
714 const char *encoding = "UTF-8";
715 struct diff_options opts;
716 int need_8bit_cte = 0;
717 struct commit *commit = NULL;
719 if (rev->commit_format != CMIT_FMT_EMAIL)
720 die("Cover letter needs email format");
722 committer = git_committer_info(0);
724 if (!numbered_files) {
726 * We fake a commit for the cover letter so we get the filename
727 * desired.
729 commit = xcalloc(1, sizeof(*commit));
730 commit->buffer = xmalloc(400);
731 snprintf(commit->buffer, 400,
732 "tree 0000000000000000000000000000000000000000\n"
733 "parent %s\n"
734 "author %s\n"
735 "committer %s\n\n"
736 "cover letter\n",
737 sha1_to_hex(head->object.sha1), committer, committer);
740 if (!use_stdout && reopen_stdout(commit, rev))
741 return;
743 if (commit) {
745 free(commit->buffer);
746 free(commit);
749 log_write_email_headers(rev, head, &subject_start, &extra_headers,
750 &need_8bit_cte);
752 for (i = 0; !need_8bit_cte && i < nr; i++)
753 if (has_non_ascii(list[i]->buffer))
754 need_8bit_cte = 1;
756 msg = body;
757 pp_user_info(NULL, CMIT_FMT_EMAIL, &sb, committer, DATE_RFC2822,
758 encoding);
759 pp_title_line(CMIT_FMT_EMAIL, &msg, &sb, subject_start, extra_headers,
760 encoding, need_8bit_cte);
761 pp_remainder(CMIT_FMT_EMAIL, &msg, &sb, 0);
762 printf("%s\n", sb.buf);
764 strbuf_release(&sb);
766 shortlog_init(&log);
767 log.wrap_lines = 1;
768 log.wrap = 72;
769 log.in1 = 2;
770 log.in2 = 4;
771 for (i = 0; i < nr; i++)
772 shortlog_add_commit(&log, list[i]);
774 shortlog_output(&log);
777 * We can only do diffstat with a unique reference point
779 if (!origin)
780 return;
782 memcpy(&opts, &rev->diffopt, sizeof(opts));
783 opts.output_format = DIFF_FORMAT_SUMMARY | DIFF_FORMAT_DIFFSTAT;
785 diff_setup_done(&opts);
787 diff_tree_sha1(origin->tree->object.sha1,
788 head->tree->object.sha1,
789 "", &opts);
790 diffcore_std(&opts);
791 diff_flush(&opts);
793 printf("\n");
796 static const char *clean_message_id(const char *msg_id)
798 char ch;
799 const char *a, *z, *m;
801 m = msg_id;
802 while ((ch = *m) && (isspace(ch) || (ch == '<')))
803 m++;
804 a = m;
805 z = NULL;
806 while ((ch = *m)) {
807 if (!isspace(ch) && (ch != '>'))
808 z = m;
809 m++;
811 if (!z)
812 die("insane in-reply-to: %s", msg_id);
813 if (++z == m)
814 return a;
815 return xmemdupz(a, z - a);
818 static const char *set_outdir(const char *prefix, const char *output_directory)
820 if (output_directory && is_absolute_path(output_directory))
821 return output_directory;
823 if (!prefix || !*prefix) {
824 if (output_directory)
825 return output_directory;
826 /* The user did not explicitly ask for "./" */
827 outdir_offset = 2;
828 return "./";
831 outdir_offset = strlen(prefix);
832 if (!output_directory)
833 return prefix;
835 return xstrdup(prefix_filename(prefix, outdir_offset,
836 output_directory));
839 static const char * const builtin_format_patch_usage[] = {
840 "git format-patch [options] [<since> | <revision range>]",
841 NULL
844 static int keep_subject = 0;
846 static int keep_callback(const struct option *opt, const char *arg, int unset)
848 ((struct rev_info *)opt->value)->total = -1;
849 keep_subject = 1;
850 return 0;
853 static int subject_prefix = 0;
855 static int subject_prefix_callback(const struct option *opt, const char *arg,
856 int unset)
858 subject_prefix = 1;
859 ((struct rev_info *)opt->value)->subject_prefix = arg;
860 return 0;
863 static int numbered_cmdline_opt = 0;
865 static int numbered_callback(const struct option *opt, const char *arg,
866 int unset)
868 *(int *)opt->value = numbered_cmdline_opt = unset ? 0 : 1;
869 if (unset)
870 auto_number = 0;
871 return 0;
874 static int no_numbered_callback(const struct option *opt, const char *arg,
875 int unset)
877 return numbered_callback(opt, arg, 1);
880 static int output_directory_callback(const struct option *opt, const char *arg,
881 int unset)
883 const char **dir = (const char **)opt->value;
884 if (*dir)
885 die("Two output directories?");
886 *dir = arg;
887 return 0;
890 static int thread_callback(const struct option *opt, const char *arg, int unset)
892 int *thread = (int *)opt->value;
893 if (unset)
894 *thread = 0;
895 else if (!arg || !strcmp(arg, "shallow"))
896 *thread = THREAD_SHALLOW;
897 else if (!strcmp(arg, "deep"))
898 *thread = THREAD_DEEP;
899 else
900 return 1;
901 return 0;
904 static int attach_callback(const struct option *opt, const char *arg, int unset)
906 struct rev_info *rev = (struct rev_info *)opt->value;
907 if (unset)
908 rev->mime_boundary = NULL;
909 else if (arg)
910 rev->mime_boundary = arg;
911 else
912 rev->mime_boundary = git_version_string;
913 rev->no_inline = unset ? 0 : 1;
914 return 0;
917 static int inline_callback(const struct option *opt, const char *arg, int unset)
919 struct rev_info *rev = (struct rev_info *)opt->value;
920 if (unset)
921 rev->mime_boundary = NULL;
922 else if (arg)
923 rev->mime_boundary = arg;
924 else
925 rev->mime_boundary = git_version_string;
926 rev->no_inline = 0;
927 return 0;
930 static int header_callback(const struct option *opt, const char *arg, int unset)
932 if (unset) {
933 string_list_clear(&extra_hdr, 0);
934 string_list_clear(&extra_to, 0);
935 string_list_clear(&extra_cc, 0);
936 } else {
937 add_header(arg);
939 return 0;
942 static int to_callback(const struct option *opt, const char *arg, int unset)
944 if (unset)
945 string_list_clear(&extra_to, 0);
946 else
947 string_list_append(arg, &extra_to);
948 return 0;
951 static int cc_callback(const struct option *opt, const char *arg, int unset)
953 if (unset)
954 string_list_clear(&extra_cc, 0);
955 else
956 string_list_append(arg, &extra_cc);
957 return 0;
960 int cmd_format_patch(int argc, const char **argv, const char *prefix)
962 struct commit *commit;
963 struct commit **list = NULL;
964 struct rev_info rev;
965 struct setup_revision_opt s_r_opt;
966 int nr = 0, total, i;
967 int use_stdout = 0;
968 int start_number = -1;
969 int numbered_files = 0; /* _just_ numbers */
970 int ignore_if_in_upstream = 0;
971 int cover_letter = 0;
972 int boundary_count = 0;
973 int no_binary_diff = 0;
974 struct commit *origin = NULL, *head = NULL;
975 const char *in_reply_to = NULL;
976 struct patch_ids ids;
977 char *add_signoff = NULL;
978 struct strbuf buf = STRBUF_INIT;
979 int use_patch_format = 0;
980 const struct option builtin_format_patch_options[] = {
981 { OPTION_CALLBACK, 'n', "numbered", &numbered, NULL,
982 "use [PATCH n/m] even with a single patch",
983 PARSE_OPT_NOARG, numbered_callback },
984 { OPTION_CALLBACK, 'N', "no-numbered", &numbered, NULL,
985 "use [PATCH] even with multiple patches",
986 PARSE_OPT_NOARG, no_numbered_callback },
987 OPT_BOOLEAN('s', "signoff", &do_signoff, "add Signed-off-by:"),
988 OPT_BOOLEAN(0, "stdout", &use_stdout,
989 "print patches to standard out"),
990 OPT_BOOLEAN(0, "cover-letter", &cover_letter,
991 "generate a cover letter"),
992 OPT_BOOLEAN(0, "numbered-files", &numbered_files,
993 "use simple number sequence for output file names"),
994 OPT_STRING(0, "suffix", &fmt_patch_suffix, "sfx",
995 "use <sfx> instead of '.patch'"),
996 OPT_INTEGER(0, "start-number", &start_number,
997 "start numbering patches at <n> instead of 1"),
998 { OPTION_CALLBACK, 0, "subject-prefix", &rev, "prefix",
999 "Use [<prefix>] instead of [PATCH]",
1000 PARSE_OPT_NONEG, subject_prefix_callback },
1001 { OPTION_CALLBACK, 'o', "output-directory", &output_directory,
1002 "dir", "store resulting files in <dir>",
1003 PARSE_OPT_NONEG, output_directory_callback },
1004 { OPTION_CALLBACK, 'k', "keep-subject", &rev, NULL,
1005 "don't strip/add [PATCH]",
1006 PARSE_OPT_NOARG | PARSE_OPT_NONEG, keep_callback },
1007 OPT_BOOLEAN(0, "no-binary", &no_binary_diff,
1008 "don't output binary diffs"),
1009 OPT_BOOLEAN(0, "ignore-if-in-upstream", &ignore_if_in_upstream,
1010 "don't include a patch matching a commit upstream"),
1011 { OPTION_BOOLEAN, 'p', "no-stat", &use_patch_format, NULL,
1012 "show patch format instead of default (patch + stat)",
1013 PARSE_OPT_NONEG | PARSE_OPT_NOARG },
1014 OPT_GROUP("Messaging"),
1015 { OPTION_CALLBACK, 0, "add-header", NULL, "header",
1016 "add email header", 0, header_callback },
1017 { OPTION_CALLBACK, 0, "to", NULL, "email", "add To: header",
1018 0, to_callback },
1019 { OPTION_CALLBACK, 0, "cc", NULL, "email", "add Cc: header",
1020 0, cc_callback },
1021 OPT_STRING(0, "in-reply-to", &in_reply_to, "message-id",
1022 "make first mail a reply to <message-id>"),
1023 { OPTION_CALLBACK, 0, "attach", &rev, "boundary",
1024 "attach the patch", PARSE_OPT_OPTARG,
1025 attach_callback },
1026 { OPTION_CALLBACK, 0, "inline", &rev, "boundary",
1027 "inline the patch",
1028 PARSE_OPT_OPTARG | PARSE_OPT_NONEG,
1029 inline_callback },
1030 { OPTION_CALLBACK, 0, "thread", &thread, "style",
1031 "enable message threading, styles: shallow, deep",
1032 PARSE_OPT_OPTARG, thread_callback },
1033 OPT_END()
1036 extra_hdr.strdup_strings = 1;
1037 extra_to.strdup_strings = 1;
1038 extra_cc.strdup_strings = 1;
1039 git_config(git_format_config, NULL);
1040 init_revisions(&rev, prefix);
1041 rev.commit_format = CMIT_FMT_EMAIL;
1042 rev.verbose_header = 1;
1043 rev.diff = 1;
1044 rev.combine_merges = 0;
1045 rev.ignore_merges = 1;
1046 DIFF_OPT_SET(&rev.diffopt, RECURSIVE);
1047 rev.subject_prefix = fmt_patch_subject_prefix;
1048 memset(&s_r_opt, 0, sizeof(s_r_opt));
1049 s_r_opt.def = "HEAD";
1051 if (default_attach) {
1052 rev.mime_boundary = default_attach;
1053 rev.no_inline = 1;
1057 * Parse the arguments before setup_revisions(), or something
1058 * like "git format-patch -o a123 HEAD^.." may fail; a123 is
1059 * possibly a valid SHA1.
1061 argc = parse_options(argc, argv, prefix, builtin_format_patch_options,
1062 builtin_format_patch_usage,
1063 PARSE_OPT_KEEP_ARGV0 | PARSE_OPT_KEEP_UNKNOWN |
1064 PARSE_OPT_KEEP_DASHDASH);
1066 if (do_signoff) {
1067 const char *committer;
1068 const char *endpos;
1069 committer = git_committer_info(IDENT_ERROR_ON_NO_NAME);
1070 endpos = strchr(committer, '>');
1071 if (!endpos)
1072 die("bogus committer info %s", committer);
1073 add_signoff = xmemdupz(committer, endpos - committer + 1);
1076 for (i = 0; i < extra_hdr.nr; i++) {
1077 strbuf_addstr(&buf, extra_hdr.items[i].string);
1078 strbuf_addch(&buf, '\n');
1081 if (extra_to.nr)
1082 strbuf_addstr(&buf, "To: ");
1083 for (i = 0; i < extra_to.nr; i++) {
1084 if (i)
1085 strbuf_addstr(&buf, " ");
1086 strbuf_addstr(&buf, extra_to.items[i].string);
1087 if (i + 1 < extra_to.nr)
1088 strbuf_addch(&buf, ',');
1089 strbuf_addch(&buf, '\n');
1092 if (extra_cc.nr)
1093 strbuf_addstr(&buf, "Cc: ");
1094 for (i = 0; i < extra_cc.nr; i++) {
1095 if (i)
1096 strbuf_addstr(&buf, " ");
1097 strbuf_addstr(&buf, extra_cc.items[i].string);
1098 if (i + 1 < extra_cc.nr)
1099 strbuf_addch(&buf, ',');
1100 strbuf_addch(&buf, '\n');
1103 rev.extra_headers = strbuf_detach(&buf, NULL);
1105 if (start_number < 0)
1106 start_number = 1;
1109 * If numbered is set solely due to format.numbered in config,
1110 * and it would conflict with --keep-subject (-k) from the
1111 * command line, reset "numbered".
1113 if (numbered && keep_subject && !numbered_cmdline_opt)
1114 numbered = 0;
1116 if (numbered && keep_subject)
1117 die ("-n and -k are mutually exclusive.");
1118 if (keep_subject && subject_prefix)
1119 die ("--subject-prefix and -k are mutually exclusive.");
1121 argc = setup_revisions(argc, argv, &rev, &s_r_opt);
1122 if (argc > 1)
1123 die ("unrecognized argument: %s", argv[1]);
1125 if (rev.diffopt.output_format & DIFF_FORMAT_NAME)
1126 die("--name-only does not make sense");
1127 if (rev.diffopt.output_format & DIFF_FORMAT_NAME_STATUS)
1128 die("--name-status does not make sense");
1129 if (rev.diffopt.output_format & DIFF_FORMAT_CHECKDIFF)
1130 die("--check does not make sense");
1132 if (!use_patch_format &&
1133 (!rev.diffopt.output_format ||
1134 rev.diffopt.output_format == DIFF_FORMAT_PATCH))
1135 rev.diffopt.output_format = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_SUMMARY;
1137 /* Always generate a patch */
1138 rev.diffopt.output_format |= DIFF_FORMAT_PATCH;
1140 if (!DIFF_OPT_TST(&rev.diffopt, TEXT) && !no_binary_diff)
1141 DIFF_OPT_SET(&rev.diffopt, BINARY);
1143 if (rev.show_notes)
1144 init_display_notes(&rev.notes_opt);
1146 if (!use_stdout)
1147 output_directory = set_outdir(prefix, output_directory);
1149 if (output_directory) {
1150 if (use_stdout)
1151 die("standard output, or directory, which one?");
1152 if (mkdir(output_directory, 0777) < 0 && errno != EEXIST)
1153 die_errno("Could not create directory '%s'",
1154 output_directory);
1157 if (rev.pending.nr == 1) {
1158 if (rev.max_count < 0 && !rev.show_root_diff) {
1160 * This is traditional behaviour of "git format-patch
1161 * origin" that prepares what the origin side still
1162 * does not have.
1164 rev.pending.objects[0].item->flags |= UNINTERESTING;
1165 add_head_to_pending(&rev);
1168 * Otherwise, it is "format-patch -22 HEAD", and/or
1169 * "format-patch --root HEAD". The user wants
1170 * get_revision() to do the usual traversal.
1175 * We cannot move this anywhere earlier because we do want to
1176 * know if --root was given explicitly from the command line.
1178 rev.show_root_diff = 1;
1180 if (cover_letter) {
1181 /* remember the range */
1182 int i;
1183 for (i = 0; i < rev.pending.nr; i++) {
1184 struct object *o = rev.pending.objects[i].item;
1185 if (!(o->flags & UNINTERESTING))
1186 head = (struct commit *)o;
1188 /* We can't generate a cover letter without any patches */
1189 if (!head)
1190 return 0;
1193 if (ignore_if_in_upstream) {
1194 /* Don't say anything if head and upstream are the same. */
1195 if (rev.pending.nr == 2) {
1196 struct object_array_entry *o = rev.pending.objects;
1197 if (hashcmp(o[0].item->sha1, o[1].item->sha1) == 0)
1198 return 0;
1200 get_patch_ids(&rev, &ids, prefix);
1203 if (!use_stdout)
1204 realstdout = xfdopen(xdup(1), "w");
1206 if (prepare_revision_walk(&rev))
1207 die("revision walk setup failed");
1208 rev.boundary = 1;
1209 while ((commit = get_revision(&rev)) != NULL) {
1210 if (commit->object.flags & BOUNDARY) {
1211 boundary_count++;
1212 origin = (boundary_count == 1) ? commit : NULL;
1213 continue;
1216 /* ignore merges */
1217 if (commit->parents && commit->parents->next)
1218 continue;
1220 if (ignore_if_in_upstream &&
1221 has_commit_patch_id(commit, &ids))
1222 continue;
1224 nr++;
1225 list = xrealloc(list, nr * sizeof(list[0]));
1226 list[nr - 1] = commit;
1228 total = nr;
1229 if (!keep_subject && auto_number && total > 1)
1230 numbered = 1;
1231 if (numbered)
1232 rev.total = total + start_number - 1;
1233 if (in_reply_to || thread || cover_letter)
1234 rev.ref_message_ids = xcalloc(1, sizeof(struct string_list));
1235 if (in_reply_to) {
1236 const char *msgid = clean_message_id(in_reply_to);
1237 string_list_append(msgid, rev.ref_message_ids);
1239 rev.numbered_files = numbered_files;
1240 rev.patch_suffix = fmt_patch_suffix;
1241 if (cover_letter) {
1242 if (thread)
1243 gen_message_id(&rev, "cover");
1244 make_cover_letter(&rev, use_stdout, numbered, numbered_files,
1245 origin, nr, list, head);
1246 total++;
1247 start_number--;
1249 rev.add_signoff = add_signoff;
1250 while (0 <= --nr) {
1251 int shown;
1252 commit = list[nr];
1253 rev.nr = total - nr + (start_number - 1);
1254 /* Make the second and subsequent mails replies to the first */
1255 if (thread) {
1256 /* Have we already had a message ID? */
1257 if (rev.message_id) {
1259 * For deep threading: make every mail
1260 * a reply to the previous one, no
1261 * matter what other options are set.
1263 * For shallow threading:
1265 * Without --cover-letter and
1266 * --in-reply-to, make every mail a
1267 * reply to the one before.
1269 * With --in-reply-to but no
1270 * --cover-letter, make every mail a
1271 * reply to the <reply-to>.
1273 * With --cover-letter, make every
1274 * mail but the cover letter a reply
1275 * to the cover letter. The cover
1276 * letter is a reply to the
1277 * --in-reply-to, if specified.
1279 if (thread == THREAD_SHALLOW
1280 && rev.ref_message_ids->nr > 0
1281 && (!cover_letter || rev.nr > 1))
1282 free(rev.message_id);
1283 else
1284 string_list_append(rev.message_id,
1285 rev.ref_message_ids);
1287 gen_message_id(&rev, sha1_to_hex(commit->object.sha1));
1290 if (!use_stdout && reopen_stdout(numbered_files ? NULL : commit,
1291 &rev))
1292 die("Failed to create output files");
1293 shown = log_tree_commit(&rev, commit);
1294 free(commit->buffer);
1295 commit->buffer = NULL;
1297 /* We put one extra blank line between formatted
1298 * patches and this flag is used by log-tree code
1299 * to see if it needs to emit a LF before showing
1300 * the log; when using one file per patch, we do
1301 * not want the extra blank line.
1303 if (!use_stdout)
1304 rev.shown_one = 0;
1305 if (shown) {
1306 if (rev.mime_boundary)
1307 printf("\n--%s%s--\n\n\n",
1308 mime_boundary_leader,
1309 rev.mime_boundary);
1310 else
1311 printf("-- \n%s\n\n", git_version_string);
1313 if (!use_stdout)
1314 fclose(stdout);
1316 free(list);
1317 string_list_clear(&extra_to, 0);
1318 string_list_clear(&extra_cc, 0);
1319 string_list_clear(&extra_hdr, 0);
1320 if (ignore_if_in_upstream)
1321 free_patch_ids(&ids);
1322 return 0;
1325 static int add_pending_commit(const char *arg, struct rev_info *revs, int flags)
1327 unsigned char sha1[20];
1328 if (get_sha1(arg, sha1) == 0) {
1329 struct commit *commit = lookup_commit_reference(sha1);
1330 if (commit) {
1331 commit->object.flags |= flags;
1332 add_pending_object(revs, &commit->object, arg);
1333 return 0;
1336 return -1;
1339 static const char * const cherry_usage[] = {
1340 "git cherry [-v] [<upstream> [<head> [<limit>]]]",
1341 NULL
1344 int cmd_cherry(int argc, const char **argv, const char *prefix)
1346 struct rev_info revs;
1347 struct patch_ids ids;
1348 struct commit *commit;
1349 struct commit_list *list = NULL;
1350 struct branch *current_branch;
1351 const char *upstream;
1352 const char *head = "HEAD";
1353 const char *limit = NULL;
1354 int verbose = 0, abbrev = 0;
1356 struct option options[] = {
1357 OPT__ABBREV(&abbrev),
1358 OPT__VERBOSE(&verbose),
1359 OPT_END()
1362 argc = parse_options(argc, argv, prefix, options, cherry_usage, 0);
1364 switch (argc) {
1365 case 3:
1366 limit = argv[2];
1367 /* FALLTHROUGH */
1368 case 2:
1369 head = argv[1];
1370 /* FALLTHROUGH */
1371 case 1:
1372 upstream = argv[0];
1373 break;
1374 default:
1375 current_branch = branch_get(NULL);
1376 if (!current_branch || !current_branch->merge
1377 || !current_branch->merge[0]
1378 || !current_branch->merge[0]->dst) {
1379 fprintf(stderr, "Could not find a tracked"
1380 " remote branch, please"
1381 " specify <upstream> manually.\n");
1382 usage_with_options(cherry_usage, options);
1385 upstream = current_branch->merge[0]->dst;
1388 init_revisions(&revs, prefix);
1389 revs.diff = 1;
1390 revs.combine_merges = 0;
1391 revs.ignore_merges = 1;
1392 DIFF_OPT_SET(&revs.diffopt, RECURSIVE);
1394 if (add_pending_commit(head, &revs, 0))
1395 die("Unknown commit %s", head);
1396 if (add_pending_commit(upstream, &revs, UNINTERESTING))
1397 die("Unknown commit %s", upstream);
1399 /* Don't say anything if head and upstream are the same. */
1400 if (revs.pending.nr == 2) {
1401 struct object_array_entry *o = revs.pending.objects;
1402 if (hashcmp(o[0].item->sha1, o[1].item->sha1) == 0)
1403 return 0;
1406 get_patch_ids(&revs, &ids, prefix);
1408 if (limit && add_pending_commit(limit, &revs, UNINTERESTING))
1409 die("Unknown commit %s", limit);
1411 /* reverse the list of commits */
1412 if (prepare_revision_walk(&revs))
1413 die("revision walk setup failed");
1414 while ((commit = get_revision(&revs)) != NULL) {
1415 /* ignore merges */
1416 if (commit->parents && commit->parents->next)
1417 continue;
1419 commit_list_insert(commit, &list);
1422 while (list) {
1423 char sign = '+';
1425 commit = list->item;
1426 if (has_commit_patch_id(commit, &ids))
1427 sign = '-';
1429 if (verbose) {
1430 struct strbuf buf = STRBUF_INIT;
1431 struct pretty_print_context ctx = {0};
1432 pretty_print_commit(CMIT_FMT_ONELINE, commit,
1433 &buf, &ctx);
1434 printf("%c %s %s\n", sign,
1435 find_unique_abbrev(commit->object.sha1, abbrev),
1436 buf.buf);
1437 strbuf_release(&buf);
1439 else {
1440 printf("%c %s\n", sign,
1441 find_unique_abbrev(commit->object.sha1, abbrev));
1444 list = list->next;
1447 free_patch_ids(&ids);
1448 return 0;