Add the diff option --no-defaults
[git/dscho.git] / builtin-log.c
blob2f365375de0a77d56ff34c6b9360eebc4377e554
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"
22 /* Set a default date-time format for git log ("log.date" config variable) */
23 static const char *default_date_mode = NULL;
25 static int default_show_root = 1;
26 static const char *fmt_patch_subject_prefix = "PATCH";
27 static const char *fmt_pretty;
29 static void cmd_log_init(int argc, const char **argv, const char *prefix,
30 struct rev_info *rev)
32 int i;
34 rev->abbrev = DEFAULT_ABBREV;
35 rev->commit_format = CMIT_FMT_DEFAULT;
36 if (fmt_pretty)
37 get_commit_format(fmt_pretty, rev);
38 rev->verbose_header = 1;
39 DIFF_OPT_SET(&rev->diffopt, RECURSIVE);
40 rev->show_root_diff = default_show_root;
41 rev->subject_prefix = fmt_patch_subject_prefix;
42 DIFF_OPT_SET(&rev->diffopt, ALLOW_TEXTCONV);
44 if (default_date_mode)
45 rev->date_mode = parse_date_format(default_date_mode);
47 argc = setup_revisions(argc, argv, rev, "HEAD");
49 if (rev->diffopt.pickaxe || rev->diffopt.filter)
50 rev->always_show_header = 0;
51 if (DIFF_OPT_TST(&rev->diffopt, FOLLOW_RENAMES)) {
52 rev->always_show_header = 0;
53 if (rev->diffopt.nr_paths != 1)
54 usage("git logs can only follow renames on one pathname at a time");
56 for (i = 1; i < argc; i++) {
57 const char *arg = argv[i];
58 if (!strcmp(arg, "--decorate")) {
59 load_ref_decorations();
60 rev->show_decorations = 1;
61 } else if (!strcmp(arg, "--source")) {
62 rev->show_source = 1;
63 } else
64 die("unrecognized argument: %s", arg);
69 * This gives a rough estimate for how many commits we
70 * will print out in the list.
72 static int estimate_commit_count(struct rev_info *rev, struct commit_list *list)
74 int n = 0;
76 while (list) {
77 struct commit *commit = list->item;
78 unsigned int flags = commit->object.flags;
79 list = list->next;
80 if (!(flags & (TREESAME | UNINTERESTING)))
81 n++;
83 return n;
86 static void show_early_header(struct rev_info *rev, const char *stage, int nr)
88 if (rev->shown_one) {
89 rev->shown_one = 0;
90 if (rev->commit_format != CMIT_FMT_ONELINE)
91 putchar(rev->diffopt.line_termination);
93 printf("Final output: %d %s\n", nr, stage);
96 struct itimerval early_output_timer;
98 static void log_show_early(struct rev_info *revs, struct commit_list *list)
100 int i = revs->early_output;
101 int show_header = 1;
103 sort_in_topological_order(&list, revs->lifo);
104 while (list && i) {
105 struct commit *commit = list->item;
106 switch (simplify_commit(revs, commit)) {
107 case commit_show:
108 if (show_header) {
109 int n = estimate_commit_count(revs, list);
110 show_early_header(revs, "incomplete", n);
111 show_header = 0;
113 log_tree_commit(revs, commit);
114 i--;
115 break;
116 case commit_ignore:
117 break;
118 case commit_error:
119 return;
121 list = list->next;
124 /* Did we already get enough commits for the early output? */
125 if (!i)
126 return;
129 * ..if no, then repeat it twice a second until we
130 * do.
132 * NOTE! We don't use "it_interval", because if the
133 * reader isn't listening, we want our output to be
134 * throttled by the writing, and not have the timer
135 * trigger every second even if we're blocked on a
136 * reader!
138 early_output_timer.it_value.tv_sec = 0;
139 early_output_timer.it_value.tv_usec = 500000;
140 setitimer(ITIMER_REAL, &early_output_timer, NULL);
143 static void early_output(int signal)
145 show_early_output = log_show_early;
148 static void setup_early_output(struct rev_info *rev)
150 struct sigaction sa;
153 * Set up the signal handler, minimally intrusively:
154 * we only set a single volatile integer word (not
155 * using sigatomic_t - trying to avoid unnecessary
156 * system dependencies and headers), and using
157 * SA_RESTART.
159 memset(&sa, 0, sizeof(sa));
160 sa.sa_handler = early_output;
161 sigemptyset(&sa.sa_mask);
162 sa.sa_flags = SA_RESTART;
163 sigaction(SIGALRM, &sa, NULL);
166 * If we can get the whole output in less than a
167 * tenth of a second, don't even bother doing the
168 * early-output thing..
170 * This is a one-time-only trigger.
172 early_output_timer.it_value.tv_sec = 0;
173 early_output_timer.it_value.tv_usec = 100000;
174 setitimer(ITIMER_REAL, &early_output_timer, NULL);
177 static void finish_early_output(struct rev_info *rev)
179 int n = estimate_commit_count(rev, rev->commits);
180 signal(SIGALRM, SIG_IGN);
181 show_early_header(rev, "done", n);
184 static int cmd_log_walk(struct rev_info *rev)
186 struct commit *commit;
188 if (rev->early_output)
189 setup_early_output(rev);
191 if (prepare_revision_walk(rev))
192 die("revision walk setup failed");
194 if (rev->early_output)
195 finish_early_output(rev);
198 * For --check and --exit-code, the exit code is based on CHECK_FAILED
199 * and HAS_CHANGES being accumulated in rev->diffopt, so be careful to
200 * retain that state information if replacing rev->diffopt in this loop
202 while ((commit = get_revision(rev)) != NULL) {
203 log_tree_commit(rev, commit);
204 if (!rev->reflog_info) {
205 /* we allow cycles in reflog ancestry */
206 free(commit->buffer);
207 commit->buffer = NULL;
209 free_commit_list(commit->parents);
210 commit->parents = NULL;
212 if (rev->diffopt.output_format & DIFF_FORMAT_CHECKDIFF &&
213 DIFF_OPT_TST(&rev->diffopt, CHECK_FAILED)) {
214 return 02;
216 return diff_result_code(&rev->diffopt, 0);
219 static int git_log_config(const char *var, const char *value, void *cb)
221 if (!strcmp(var, "format.pretty"))
222 return git_config_string(&fmt_pretty, var, value);
223 if (!strcmp(var, "format.subjectprefix"))
224 return git_config_string(&fmt_patch_subject_prefix, var, value);
225 if (!strcmp(var, "log.date"))
226 return git_config_string(&default_date_mode, var, value);
227 if (!strcmp(var, "log.showroot")) {
228 default_show_root = git_config_bool(var, value);
229 return 0;
231 return git_diff_ui_config(var, value, cb);
234 int cmd_whatchanged(int argc, const char **argv, const char *prefix)
236 struct rev_info rev;
238 git_config(git_log_config, NULL);
240 if (diff_use_color_default == -1)
241 diff_use_color_default = git_use_color_default;
243 init_revisions(&rev, prefix);
244 rev.diff = 1;
245 rev.simplify_history = 0;
246 argc = parse_default_diff_options(argc, argv, &rev.diffopt);
247 cmd_log_init(argc, argv, prefix, &rev);
248 if (!rev.diffopt.output_format)
249 rev.diffopt.output_format = DIFF_FORMAT_RAW;
250 return cmd_log_walk(&rev);
253 static void show_tagger(char *buf, int len, struct rev_info *rev)
255 struct strbuf out = STRBUF_INIT;
257 pp_user_info("Tagger", rev->commit_format, &out, buf, rev->date_mode,
258 git_log_output_encoding ?
259 git_log_output_encoding: git_commit_encoding);
260 printf("%s\n", out.buf);
261 strbuf_release(&out);
264 static int show_object(const unsigned char *sha1, int show_tag_object,
265 struct rev_info *rev)
267 unsigned long size;
268 enum object_type type;
269 char *buf = read_sha1_file(sha1, &type, &size);
270 int offset = 0;
272 if (!buf)
273 return error("Could not read object %s", sha1_to_hex(sha1));
275 if (show_tag_object)
276 while (offset < size && buf[offset] != '\n') {
277 int new_offset = offset + 1;
278 while (new_offset < size && buf[new_offset++] != '\n')
279 ; /* do nothing */
280 if (!prefixcmp(buf + offset, "tagger "))
281 show_tagger(buf + offset + 7,
282 new_offset - offset - 7, rev);
283 offset = new_offset;
286 if (offset < size)
287 fwrite(buf + offset, size - offset, 1, stdout);
288 free(buf);
289 return 0;
292 static int show_tree_object(const unsigned char *sha1,
293 const char *base, int baselen,
294 const char *pathname, unsigned mode, int stage, void *context)
296 printf("%s%s\n", pathname, S_ISDIR(mode) ? "/" : "");
297 return 0;
300 int cmd_show(int argc, const char **argv, const char *prefix)
302 struct rev_info rev;
303 struct object_array_entry *objects;
304 int i, count, ret = 0;
306 git_config(git_log_config, NULL);
308 if (diff_use_color_default == -1)
309 diff_use_color_default = git_use_color_default;
311 init_revisions(&rev, prefix);
312 rev.diff = 1;
313 rev.combine_merges = 1;
314 rev.dense_combined_merges = 1;
315 rev.always_show_header = 1;
316 rev.ignore_merges = 0;
317 rev.no_walk = 1;
318 argc = parse_default_diff_options(argc, argv, &rev.diffopt);
319 cmd_log_init(argc, argv, prefix, &rev);
321 count = rev.pending.nr;
322 objects = rev.pending.objects;
323 for (i = 0; i < count && !ret; i++) {
324 struct object *o = objects[i].item;
325 const char *name = objects[i].name;
326 switch (o->type) {
327 case OBJ_BLOB:
328 ret = show_object(o->sha1, 0, NULL);
329 break;
330 case OBJ_TAG: {
331 struct tag *t = (struct tag *)o;
333 printf("%stag %s%s\n",
334 diff_get_color_opt(&rev.diffopt, DIFF_COMMIT),
335 t->tag,
336 diff_get_color_opt(&rev.diffopt, DIFF_RESET));
337 ret = show_object(o->sha1, 1, &rev);
338 if (ret)
339 break;
340 o = parse_object(t->tagged->sha1);
341 if (!o)
342 ret = error("Could not read object %s",
343 sha1_to_hex(t->tagged->sha1));
344 objects[i].item = o;
345 i--;
346 break;
348 case OBJ_TREE:
349 printf("%stree %s%s\n\n",
350 diff_get_color_opt(&rev.diffopt, DIFF_COMMIT),
351 name,
352 diff_get_color_opt(&rev.diffopt, DIFF_RESET));
353 read_tree_recursive((struct tree *)o, "", 0, 0, NULL,
354 show_tree_object, NULL);
355 break;
356 case OBJ_COMMIT:
357 rev.pending.nr = rev.pending.alloc = 0;
358 rev.pending.objects = NULL;
359 add_object_array(o, name, &rev.pending);
360 ret = cmd_log_walk(&rev);
361 break;
362 default:
363 ret = error("Unknown type: %d", o->type);
366 free(objects);
367 return ret;
371 * This is equivalent to "git log -g --abbrev-commit --pretty=oneline"
373 int cmd_log_reflog(int argc, const char **argv, const char *prefix)
375 struct rev_info rev;
377 git_config(git_log_config, NULL);
379 if (diff_use_color_default == -1)
380 diff_use_color_default = git_use_color_default;
382 init_revisions(&rev, prefix);
383 init_reflog_walk(&rev.reflog_info);
384 rev.abbrev_commit = 1;
385 rev.verbose_header = 1;
386 argc = parse_default_diff_options(argc, argv, &rev.diffopt);
387 cmd_log_init(argc, argv, prefix, &rev);
390 * This means that we override whatever commit format the user gave
391 * on the cmd line. Sad, but cmd_log_init() currently doesn't
392 * allow us to set a different default.
394 rev.commit_format = CMIT_FMT_ONELINE;
395 rev.use_terminator = 1;
396 rev.always_show_header = 1;
399 * We get called through "git reflog", so unlike the other log
400 * routines, we need to set up our pager manually..
402 setup_pager();
404 return cmd_log_walk(&rev);
407 int cmd_log(int argc, const char **argv, const char *prefix)
409 struct rev_info rev;
411 git_config(git_log_config, NULL);
413 if (diff_use_color_default == -1)
414 diff_use_color_default = git_use_color_default;
416 init_revisions(&rev, prefix);
417 rev.always_show_header = 1;
418 argc = parse_default_diff_options(argc, argv, &rev.diffopt);
419 cmd_log_init(argc, argv, prefix, &rev);
420 return cmd_log_walk(&rev);
423 /* format-patch */
425 static const char *fmt_patch_suffix = ".patch";
426 static int numbered = 0;
427 static int auto_number = 1;
429 static char *default_attach = NULL;
431 static char **extra_hdr;
432 static int extra_hdr_nr;
433 static int extra_hdr_alloc;
435 static char **extra_to;
436 static int extra_to_nr;
437 static int extra_to_alloc;
439 static char **extra_cc;
440 static int extra_cc_nr;
441 static int extra_cc_alloc;
443 static void add_header(const char *value)
445 int len = strlen(value);
446 while (len && value[len - 1] == '\n')
447 len--;
448 if (!strncasecmp(value, "to: ", 4)) {
449 ALLOC_GROW(extra_to, extra_to_nr + 1, extra_to_alloc);
450 extra_to[extra_to_nr++] = xstrndup(value + 4, len - 4);
451 return;
453 if (!strncasecmp(value, "cc: ", 4)) {
454 ALLOC_GROW(extra_cc, extra_cc_nr + 1, extra_cc_alloc);
455 extra_cc[extra_cc_nr++] = xstrndup(value + 4, len - 4);
456 return;
458 ALLOC_GROW(extra_hdr, extra_hdr_nr + 1, extra_hdr_alloc);
459 extra_hdr[extra_hdr_nr++] = xstrndup(value, len);
462 #define THREAD_SHALLOW 1
463 #define THREAD_DEEP 2
464 static int thread = 0;
465 static int do_signoff = 0;
467 static int git_format_config(const char *var, const char *value, void *cb)
469 if (!strcmp(var, "format.headers")) {
470 if (!value)
471 die("format.headers without value");
472 add_header(value);
473 return 0;
475 if (!strcmp(var, "format.suffix"))
476 return git_config_string(&fmt_patch_suffix, var, value);
477 if (!strcmp(var, "format.cc")) {
478 if (!value)
479 return config_error_nonbool(var);
480 ALLOC_GROW(extra_cc, extra_cc_nr + 1, extra_cc_alloc);
481 extra_cc[extra_cc_nr++] = xstrdup(value);
482 return 0;
484 if (!strcmp(var, "diff.color") || !strcmp(var, "color.diff")) {
485 return 0;
487 if (!strcmp(var, "format.numbered")) {
488 if (value && !strcasecmp(value, "auto")) {
489 auto_number = 1;
490 return 0;
492 numbered = git_config_bool(var, value);
493 auto_number = auto_number && numbered;
494 return 0;
496 if (!strcmp(var, "format.attach")) {
497 if (value && *value)
498 default_attach = xstrdup(value);
499 else
500 default_attach = xstrdup(git_version_string);
501 return 0;
503 if (!strcmp(var, "format.thread")) {
504 if (value && !strcasecmp(value, "deep")) {
505 thread = THREAD_DEEP;
506 return 0;
508 if (value && !strcasecmp(value, "shallow")) {
509 thread = THREAD_SHALLOW;
510 return 0;
512 thread = git_config_bool(var, value) && THREAD_SHALLOW;
513 return 0;
515 if (!strcmp(var, "format.signoff")) {
516 do_signoff = git_config_bool(var, value);
517 return 0;
520 return git_log_config(var, value, cb);
523 static FILE *realstdout = NULL;
524 static const char *output_directory = NULL;
525 static int outdir_offset;
527 static int reopen_stdout(struct commit *commit, struct rev_info *rev)
529 struct strbuf filename = STRBUF_INIT;
530 int suffix_len = strlen(fmt_patch_suffix) + 1;
532 if (output_directory) {
533 strbuf_addstr(&filename, output_directory);
534 if (filename.len >=
535 PATH_MAX - FORMAT_PATCH_NAME_MAX - suffix_len)
536 return error("name of output directory is too long");
537 if (filename.buf[filename.len - 1] != '/')
538 strbuf_addch(&filename, '/');
541 get_patch_filename(commit, rev->nr, fmt_patch_suffix, &filename);
543 if (!DIFF_OPT_TST(&rev->diffopt, QUIET))
544 fprintf(realstdout, "%s\n", filename.buf + outdir_offset);
546 if (freopen(filename.buf, "w", stdout) == NULL)
547 return error("Cannot open patch file %s", filename.buf);
549 strbuf_release(&filename);
550 return 0;
553 static void get_patch_ids(struct rev_info *rev, struct patch_ids *ids, const char *prefix)
555 struct rev_info check_rev;
556 struct commit *commit;
557 struct object *o1, *o2;
558 unsigned flags1, flags2;
560 if (rev->pending.nr != 2)
561 die("Need exactly one range.");
563 o1 = rev->pending.objects[0].item;
564 flags1 = o1->flags;
565 o2 = rev->pending.objects[1].item;
566 flags2 = o2->flags;
568 if ((flags1 & UNINTERESTING) == (flags2 & UNINTERESTING))
569 die("Not a range.");
571 init_patch_ids(ids);
573 /* given a range a..b get all patch ids for b..a */
574 init_revisions(&check_rev, prefix);
575 o1->flags ^= UNINTERESTING;
576 o2->flags ^= UNINTERESTING;
577 add_pending_object(&check_rev, o1, "o1");
578 add_pending_object(&check_rev, o2, "o2");
579 if (prepare_revision_walk(&check_rev))
580 die("revision walk setup failed");
582 while ((commit = get_revision(&check_rev)) != NULL) {
583 /* ignore merges */
584 if (commit->parents && commit->parents->next)
585 continue;
587 add_commit_patch_id(commit, ids);
590 /* reset for next revision walk */
591 clear_commit_marks((struct commit *)o1,
592 SEEN | UNINTERESTING | SHOWN | ADDED);
593 clear_commit_marks((struct commit *)o2,
594 SEEN | UNINTERESTING | SHOWN | ADDED);
595 o1->flags = flags1;
596 o2->flags = flags2;
599 static void gen_message_id(struct rev_info *info, char *base)
601 const char *committer = git_committer_info(IDENT_WARN_ON_NO_NAME);
602 const char *email_start = strrchr(committer, '<');
603 const char *email_end = strrchr(committer, '>');
604 struct strbuf buf = STRBUF_INIT;
605 if (!email_start || !email_end || email_start > email_end - 1)
606 die("Could not extract email from committer identity.");
607 strbuf_addf(&buf, "%s.%lu.git.%.*s", base,
608 (unsigned long) time(NULL),
609 (int)(email_end - email_start - 1), email_start + 1);
610 info->message_id = strbuf_detach(&buf, NULL);
613 static void make_cover_letter(struct rev_info *rev, int use_stdout,
614 int numbered, int numbered_files,
615 struct commit *origin,
616 int nr, struct commit **list, struct commit *head)
618 const char *committer;
619 const char *subject_start = NULL;
620 const char *body = "*** SUBJECT HERE ***\n\n*** BLURB HERE ***\n";
621 const char *msg;
622 const char *extra_headers = rev->extra_headers;
623 struct shortlog log;
624 struct strbuf sb = STRBUF_INIT;
625 int i;
626 const char *encoding = "utf-8";
627 struct diff_options opts;
628 int need_8bit_cte = 0;
629 struct commit *commit = NULL;
631 if (rev->commit_format != CMIT_FMT_EMAIL)
632 die("Cover letter needs email format");
634 committer = git_committer_info(0);
636 if (!numbered_files) {
638 * We fake a commit for the cover letter so we get the filename
639 * desired.
641 commit = xcalloc(1, sizeof(*commit));
642 commit->buffer = xmalloc(400);
643 snprintf(commit->buffer, 400,
644 "tree 0000000000000000000000000000000000000000\n"
645 "parent %s\n"
646 "author %s\n"
647 "committer %s\n\n"
648 "cover letter\n",
649 sha1_to_hex(head->object.sha1), committer, committer);
652 if (!use_stdout && reopen_stdout(commit, rev))
653 return;
655 if (commit) {
657 free(commit->buffer);
658 free(commit);
661 log_write_email_headers(rev, head, &subject_start, &extra_headers,
662 &need_8bit_cte);
664 msg = body;
665 pp_user_info(NULL, CMIT_FMT_EMAIL, &sb, committer, DATE_RFC2822,
666 encoding);
667 pp_title_line(CMIT_FMT_EMAIL, &msg, &sb, subject_start, extra_headers,
668 encoding, need_8bit_cte);
669 pp_remainder(CMIT_FMT_EMAIL, &msg, &sb, 0);
670 printf("%s\n", sb.buf);
672 strbuf_release(&sb);
674 shortlog_init(&log);
675 log.wrap_lines = 1;
676 log.wrap = 72;
677 log.in1 = 2;
678 log.in2 = 4;
679 for (i = 0; i < nr; i++)
680 shortlog_add_commit(&log, list[i]);
682 shortlog_output(&log);
685 * We can only do diffstat with a unique reference point
687 if (!origin)
688 return;
690 memcpy(&opts, &rev->diffopt, sizeof(opts));
691 opts.output_format = DIFF_FORMAT_SUMMARY | DIFF_FORMAT_DIFFSTAT;
693 diff_setup_done(&opts);
695 diff_tree_sha1(origin->tree->object.sha1,
696 head->tree->object.sha1,
697 "", &opts);
698 diffcore_std(&opts);
699 diff_flush(&opts);
701 printf("\n");
704 static const char *clean_message_id(const char *msg_id)
706 char ch;
707 const char *a, *z, *m;
709 m = msg_id;
710 while ((ch = *m) && (isspace(ch) || (ch == '<')))
711 m++;
712 a = m;
713 z = NULL;
714 while ((ch = *m)) {
715 if (!isspace(ch) && (ch != '>'))
716 z = m;
717 m++;
719 if (!z)
720 die("insane in-reply-to: %s", msg_id);
721 if (++z == m)
722 return a;
723 return xmemdupz(a, z - a);
726 static const char *set_outdir(const char *prefix, const char *output_directory)
728 if (output_directory && is_absolute_path(output_directory))
729 return output_directory;
731 if (!prefix || !*prefix) {
732 if (output_directory)
733 return output_directory;
734 /* The user did not explicitly ask for "./" */
735 outdir_offset = 2;
736 return "./";
739 outdir_offset = strlen(prefix);
740 if (!output_directory)
741 return prefix;
743 return xstrdup(prefix_filename(prefix, outdir_offset,
744 output_directory));
747 int cmd_format_patch(int argc, const char **argv, const char *prefix)
749 struct commit *commit;
750 struct commit **list = NULL;
751 struct rev_info rev;
752 int nr = 0, total, i, j;
753 int use_stdout = 0;
754 int start_number = -1;
755 int keep_subject = 0;
756 int numbered_files = 0; /* _just_ numbers */
757 int subject_prefix = 0;
758 int ignore_if_in_upstream = 0;
759 int cover_letter = 0;
760 int boundary_count = 0;
761 int no_binary_diff = 0;
762 struct commit *origin = NULL, *head = NULL;
763 const char *in_reply_to = NULL;
764 struct patch_ids ids;
765 char *add_signoff = NULL;
766 struct strbuf buf = STRBUF_INIT;
768 git_config(git_format_config, NULL);
769 init_revisions(&rev, prefix);
770 rev.commit_format = CMIT_FMT_EMAIL;
771 rev.verbose_header = 1;
772 rev.diff = 1;
773 rev.combine_merges = 0;
774 rev.ignore_merges = 1;
775 DIFF_OPT_SET(&rev.diffopt, RECURSIVE);
777 rev.subject_prefix = fmt_patch_subject_prefix;
779 if (default_attach) {
780 rev.mime_boundary = default_attach;
781 rev.no_inline = 1;
785 * Parse the arguments before setup_revisions(), or something
786 * like "git format-patch -o a123 HEAD^.." may fail; a123 is
787 * possibly a valid SHA1.
789 for (i = 1, j = 1; i < argc; i++) {
790 if (!strcmp(argv[i], "--stdout"))
791 use_stdout = 1;
792 else if (!strcmp(argv[i], "-n") ||
793 !strcmp(argv[i], "--numbered"))
794 numbered = 1;
795 else if (!strcmp(argv[i], "-N") ||
796 !strcmp(argv[i], "--no-numbered")) {
797 numbered = 0;
798 auto_number = 0;
800 else if (!prefixcmp(argv[i], "--start-number="))
801 start_number = strtol(argv[i] + 15, NULL, 10);
802 else if (!strcmp(argv[i], "--numbered-files"))
803 numbered_files = 1;
804 else if (!strcmp(argv[i], "--start-number")) {
805 i++;
806 if (i == argc)
807 die("Need a number for --start-number");
808 start_number = strtol(argv[i], NULL, 10);
810 else if (!prefixcmp(argv[i], "--cc=")) {
811 ALLOC_GROW(extra_cc, extra_cc_nr + 1, extra_cc_alloc);
812 extra_cc[extra_cc_nr++] = xstrdup(argv[i] + 5);
814 else if (!strcmp(argv[i], "-k") ||
815 !strcmp(argv[i], "--keep-subject")) {
816 keep_subject = 1;
817 rev.total = -1;
819 else if (!strcmp(argv[i], "--output-directory") ||
820 !strcmp(argv[i], "-o")) {
821 i++;
822 if (argc <= i)
823 die("Which directory?");
824 if (output_directory)
825 die("Two output directories?");
826 output_directory = argv[i];
828 else if (!strcmp(argv[i], "--signoff") ||
829 !strcmp(argv[i], "-s")) {
830 do_signoff = 1;
832 else if (!strcmp(argv[i], "--attach")) {
833 rev.mime_boundary = git_version_string;
834 rev.no_inline = 1;
836 else if (!prefixcmp(argv[i], "--attach=")) {
837 rev.mime_boundary = argv[i] + 9;
838 rev.no_inline = 1;
840 else if (!strcmp(argv[i], "--no-attach")) {
841 rev.mime_boundary = NULL;
842 rev.no_inline = 0;
844 else if (!strcmp(argv[i], "--inline")) {
845 rev.mime_boundary = git_version_string;
846 rev.no_inline = 0;
848 else if (!prefixcmp(argv[i], "--inline=")) {
849 rev.mime_boundary = argv[i] + 9;
850 rev.no_inline = 0;
852 else if (!strcmp(argv[i], "--ignore-if-in-upstream"))
853 ignore_if_in_upstream = 1;
854 else if (!strcmp(argv[i], "--thread")
855 || !strcmp(argv[i], "--thread=shallow"))
856 thread = THREAD_SHALLOW;
857 else if (!strcmp(argv[i], "--thread=deep"))
858 thread = THREAD_DEEP;
859 else if (!strcmp(argv[i], "--no-thread"))
860 thread = 0;
861 else if (!prefixcmp(argv[i], "--in-reply-to="))
862 in_reply_to = argv[i] + 14;
863 else if (!strcmp(argv[i], "--in-reply-to")) {
864 i++;
865 if (i == argc)
866 die("Need a Message-Id for --in-reply-to");
867 in_reply_to = argv[i];
868 } else if (!prefixcmp(argv[i], "--subject-prefix=")) {
869 subject_prefix = 1;
870 rev.subject_prefix = argv[i] + 17;
871 } else if (!prefixcmp(argv[i], "--suffix="))
872 fmt_patch_suffix = argv[i] + 9;
873 else if (!strcmp(argv[i], "--cover-letter"))
874 cover_letter = 1;
875 else if (!strcmp(argv[i], "--no-binary"))
876 no_binary_diff = 1;
877 else if (!prefixcmp(argv[i], "--add-header="))
878 add_header(argv[i] + 13);
879 else
880 argv[j++] = argv[i];
882 argc = j;
884 if (do_signoff) {
885 const char *committer;
886 const char *endpos;
887 committer = git_committer_info(IDENT_ERROR_ON_NO_NAME);
888 endpos = strchr(committer, '>');
889 if (!endpos)
890 die("bogus committer info %s", committer);
891 add_signoff = xmemdupz(committer, endpos - committer + 1);
894 for (i = 0; i < extra_hdr_nr; i++) {
895 strbuf_addstr(&buf, extra_hdr[i]);
896 strbuf_addch(&buf, '\n');
899 if (extra_to_nr)
900 strbuf_addstr(&buf, "To: ");
901 for (i = 0; i < extra_to_nr; i++) {
902 if (i)
903 strbuf_addstr(&buf, " ");
904 strbuf_addstr(&buf, extra_to[i]);
905 if (i + 1 < extra_to_nr)
906 strbuf_addch(&buf, ',');
907 strbuf_addch(&buf, '\n');
910 if (extra_cc_nr)
911 strbuf_addstr(&buf, "Cc: ");
912 for (i = 0; i < extra_cc_nr; i++) {
913 if (i)
914 strbuf_addstr(&buf, " ");
915 strbuf_addstr(&buf, extra_cc[i]);
916 if (i + 1 < extra_cc_nr)
917 strbuf_addch(&buf, ',');
918 strbuf_addch(&buf, '\n');
921 rev.extra_headers = strbuf_detach(&buf, 0);
923 if (start_number < 0)
924 start_number = 1;
925 if (numbered && keep_subject)
926 die ("-n and -k are mutually exclusive.");
927 if (keep_subject && subject_prefix)
928 die ("--subject-prefix and -k are mutually exclusive.");
930 argc = setup_revisions(argc, argv, &rev, "HEAD");
931 if (argc > 1)
932 die ("unrecognized argument: %s", argv[1]);
934 if (!rev.diffopt.output_format
935 || rev.diffopt.output_format == DIFF_FORMAT_PATCH)
936 rev.diffopt.output_format = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_SUMMARY | DIFF_FORMAT_PATCH;
938 if (!DIFF_OPT_TST(&rev.diffopt, TEXT) && !no_binary_diff)
939 DIFF_OPT_SET(&rev.diffopt, BINARY);
941 if (!use_stdout)
942 output_directory = set_outdir(prefix, output_directory);
944 if (output_directory) {
945 if (use_stdout)
946 die("standard output, or directory, which one?");
947 if (mkdir(output_directory, 0777) < 0 && errno != EEXIST)
948 die("Could not create directory %s",
949 output_directory);
952 if (rev.pending.nr == 1) {
953 if (rev.max_count < 0 && !rev.show_root_diff) {
955 * This is traditional behaviour of "git format-patch
956 * origin" that prepares what the origin side still
957 * does not have.
959 rev.pending.objects[0].item->flags |= UNINTERESTING;
960 add_head_to_pending(&rev);
963 * Otherwise, it is "format-patch -22 HEAD", and/or
964 * "format-patch --root HEAD". The user wants
965 * get_revision() to do the usual traversal.
970 * We cannot move this anywhere earlier because we do want to
971 * know if --root was given explicitly from the comand line.
973 rev.show_root_diff = 1;
975 if (cover_letter) {
976 /* remember the range */
977 int i;
978 for (i = 0; i < rev.pending.nr; i++) {
979 struct object *o = rev.pending.objects[i].item;
980 if (!(o->flags & UNINTERESTING))
981 head = (struct commit *)o;
983 /* We can't generate a cover letter without any patches */
984 if (!head)
985 return 0;
988 if (ignore_if_in_upstream)
989 get_patch_ids(&rev, &ids, prefix);
991 if (!use_stdout)
992 realstdout = xfdopen(xdup(1), "w");
994 if (prepare_revision_walk(&rev))
995 die("revision walk setup failed");
996 rev.boundary = 1;
997 while ((commit = get_revision(&rev)) != NULL) {
998 if (commit->object.flags & BOUNDARY) {
999 boundary_count++;
1000 origin = (boundary_count == 1) ? commit : NULL;
1001 continue;
1004 /* ignore merges */
1005 if (commit->parents && commit->parents->next)
1006 continue;
1008 if (ignore_if_in_upstream &&
1009 has_commit_patch_id(commit, &ids))
1010 continue;
1012 nr++;
1013 list = xrealloc(list, nr * sizeof(list[0]));
1014 list[nr - 1] = commit;
1016 total = nr;
1017 if (!keep_subject && auto_number && total > 1)
1018 numbered = 1;
1019 if (numbered)
1020 rev.total = total + start_number - 1;
1021 if (in_reply_to || thread || cover_letter)
1022 rev.ref_message_ids = xcalloc(1, sizeof(struct string_list));
1023 if (in_reply_to) {
1024 const char *msgid = clean_message_id(in_reply_to);
1025 string_list_append(msgid, rev.ref_message_ids);
1027 rev.numbered_files = numbered_files;
1028 rev.patch_suffix = fmt_patch_suffix;
1029 if (cover_letter) {
1030 if (thread)
1031 gen_message_id(&rev, "cover");
1032 make_cover_letter(&rev, use_stdout, numbered, numbered_files,
1033 origin, nr, list, head);
1034 total++;
1035 start_number--;
1037 rev.add_signoff = add_signoff;
1038 while (0 <= --nr) {
1039 int shown;
1040 commit = list[nr];
1041 rev.nr = total - nr + (start_number - 1);
1042 /* Make the second and subsequent mails replies to the first */
1043 if (thread) {
1044 /* Have we already had a message ID? */
1045 if (rev.message_id) {
1047 * For deep threading: make every mail
1048 * a reply to the previous one, no
1049 * matter what other options are set.
1051 * For shallow threading:
1053 * Without --cover-letter and
1054 * --in-reply-to, make every mail a
1055 * reply to the one before.
1057 * With --in-reply-to but no
1058 * --cover-letter, make every mail a
1059 * reply to the <reply-to>.
1061 * With --cover-letter, make every
1062 * mail but the cover letter a reply
1063 * to the cover letter. The cover
1064 * letter is a reply to the
1065 * --in-reply-to, if specified.
1067 if (thread == THREAD_SHALLOW
1068 && rev.ref_message_ids->nr > 0
1069 && (!cover_letter || rev.nr > 1))
1070 free(rev.message_id);
1071 else
1072 string_list_append(rev.message_id,
1073 rev.ref_message_ids);
1075 gen_message_id(&rev, sha1_to_hex(commit->object.sha1));
1078 if (!use_stdout && reopen_stdout(numbered_files ? NULL : commit,
1079 &rev))
1080 die("Failed to create output files");
1081 shown = log_tree_commit(&rev, commit);
1082 free(commit->buffer);
1083 commit->buffer = NULL;
1085 /* We put one extra blank line between formatted
1086 * patches and this flag is used by log-tree code
1087 * to see if it needs to emit a LF before showing
1088 * the log; when using one file per patch, we do
1089 * not want the extra blank line.
1091 if (!use_stdout)
1092 rev.shown_one = 0;
1093 if (shown) {
1094 if (rev.mime_boundary)
1095 printf("\n--%s%s--\n\n\n",
1096 mime_boundary_leader,
1097 rev.mime_boundary);
1098 else
1099 printf("-- \n%s\n\n", git_version_string);
1101 if (!use_stdout)
1102 fclose(stdout);
1104 free(list);
1105 if (ignore_if_in_upstream)
1106 free_patch_ids(&ids);
1107 return 0;
1110 static int add_pending_commit(const char *arg, struct rev_info *revs, int flags)
1112 unsigned char sha1[20];
1113 if (get_sha1(arg, sha1) == 0) {
1114 struct commit *commit = lookup_commit_reference(sha1);
1115 if (commit) {
1116 commit->object.flags |= flags;
1117 add_pending_object(revs, &commit->object, arg);
1118 return 0;
1121 return -1;
1124 static const char cherry_usage[] =
1125 "git cherry [-v] [<upstream> [<head> [<limit>]]]";
1126 int cmd_cherry(int argc, const char **argv, const char *prefix)
1128 struct rev_info revs;
1129 struct patch_ids ids;
1130 struct commit *commit;
1131 struct commit_list *list = NULL;
1132 struct branch *current_branch;
1133 const char *upstream;
1134 const char *head = "HEAD";
1135 const char *limit = NULL;
1136 int verbose = 0;
1138 if (argc > 1 && !strcmp(argv[1], "-v")) {
1139 verbose = 1;
1140 argc--;
1141 argv++;
1144 switch (argc) {
1145 case 4:
1146 limit = argv[3];
1147 /* FALLTHROUGH */
1148 case 3:
1149 head = argv[2];
1150 /* FALLTHROUGH */
1151 case 2:
1152 upstream = argv[1];
1153 break;
1154 default:
1155 current_branch = branch_get(NULL);
1156 if (!current_branch || !current_branch->merge
1157 || !current_branch->merge[0]
1158 || !current_branch->merge[0]->dst) {
1159 fprintf(stderr, "Could not find a tracked"
1160 " remote branch, please"
1161 " specify <upstream> manually.\n");
1162 usage(cherry_usage);
1165 upstream = current_branch->merge[0]->dst;
1168 init_revisions(&revs, prefix);
1169 revs.diff = 1;
1170 revs.combine_merges = 0;
1171 revs.ignore_merges = 1;
1172 DIFF_OPT_SET(&revs.diffopt, RECURSIVE);
1174 if (add_pending_commit(head, &revs, 0))
1175 die("Unknown commit %s", head);
1176 if (add_pending_commit(upstream, &revs, UNINTERESTING))
1177 die("Unknown commit %s", upstream);
1179 /* Don't say anything if head and upstream are the same. */
1180 if (revs.pending.nr == 2) {
1181 struct object_array_entry *o = revs.pending.objects;
1182 if (hashcmp(o[0].item->sha1, o[1].item->sha1) == 0)
1183 return 0;
1186 get_patch_ids(&revs, &ids, prefix);
1188 if (limit && add_pending_commit(limit, &revs, UNINTERESTING))
1189 die("Unknown commit %s", limit);
1191 /* reverse the list of commits */
1192 if (prepare_revision_walk(&revs))
1193 die("revision walk setup failed");
1194 while ((commit = get_revision(&revs)) != NULL) {
1195 /* ignore merges */
1196 if (commit->parents && commit->parents->next)
1197 continue;
1199 commit_list_insert(commit, &list);
1202 while (list) {
1203 char sign = '+';
1205 commit = list->item;
1206 if (has_commit_patch_id(commit, &ids))
1207 sign = '-';
1209 if (verbose) {
1210 struct strbuf buf = STRBUF_INIT;
1211 pretty_print_commit(CMIT_FMT_ONELINE, commit,
1212 &buf, 0, NULL, NULL, 0, 0);
1213 printf("%c %s %s\n", sign,
1214 sha1_to_hex(commit->object.sha1), buf.buf);
1215 strbuf_release(&buf);
1217 else {
1218 printf("%c %s\n", sign,
1219 sha1_to_hex(commit->object.sha1));
1222 list = list->next;
1225 free_patch_ids(&ids);
1226 return 0;