config.txt: document add.ignore-errors
[git/gitweb.git] / builtin-log.c
blobf10cfebdbbabac67dea41639289e7ad968d5bbe3
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 cmd_log_init(argc, argv, prefix, &rev);
247 if (!rev.diffopt.output_format)
248 rev.diffopt.output_format = DIFF_FORMAT_RAW;
249 return cmd_log_walk(&rev);
252 static void show_tagger(char *buf, int len, struct rev_info *rev)
254 struct strbuf out = STRBUF_INIT;
256 pp_user_info("Tagger", rev->commit_format, &out, buf, rev->date_mode,
257 git_log_output_encoding ?
258 git_log_output_encoding: git_commit_encoding);
259 printf("%s\n", out.buf);
260 strbuf_release(&out);
263 static int show_object(const unsigned char *sha1, int show_tag_object,
264 struct rev_info *rev)
266 unsigned long size;
267 enum object_type type;
268 char *buf = read_sha1_file(sha1, &type, &size);
269 int offset = 0;
271 if (!buf)
272 return error("Could not read object %s", sha1_to_hex(sha1));
274 if (show_tag_object)
275 while (offset < size && buf[offset] != '\n') {
276 int new_offset = offset + 1;
277 while (new_offset < size && buf[new_offset++] != '\n')
278 ; /* do nothing */
279 if (!prefixcmp(buf + offset, "tagger "))
280 show_tagger(buf + offset + 7,
281 new_offset - offset - 7, rev);
282 offset = new_offset;
285 if (offset < size)
286 fwrite(buf + offset, size - offset, 1, stdout);
287 free(buf);
288 return 0;
291 static int show_tree_object(const unsigned char *sha1,
292 const char *base, int baselen,
293 const char *pathname, unsigned mode, int stage, void *context)
295 printf("%s%s\n", pathname, S_ISDIR(mode) ? "/" : "");
296 return 0;
299 int cmd_show(int argc, const char **argv, const char *prefix)
301 struct rev_info rev;
302 struct object_array_entry *objects;
303 int i, count, ret = 0;
305 git_config(git_log_config, NULL);
307 if (diff_use_color_default == -1)
308 diff_use_color_default = git_use_color_default;
310 init_revisions(&rev, prefix);
311 rev.diff = 1;
312 rev.combine_merges = 1;
313 rev.dense_combined_merges = 1;
314 rev.always_show_header = 1;
315 rev.ignore_merges = 0;
316 rev.no_walk = 1;
317 cmd_log_init(argc, argv, prefix, &rev);
319 count = rev.pending.nr;
320 objects = rev.pending.objects;
321 for (i = 0; i < count && !ret; i++) {
322 struct object *o = objects[i].item;
323 const char *name = objects[i].name;
324 switch (o->type) {
325 case OBJ_BLOB:
326 ret = show_object(o->sha1, 0, NULL);
327 break;
328 case OBJ_TAG: {
329 struct tag *t = (struct tag *)o;
331 printf("%stag %s%s\n",
332 diff_get_color_opt(&rev.diffopt, DIFF_COMMIT),
333 t->tag,
334 diff_get_color_opt(&rev.diffopt, DIFF_RESET));
335 ret = show_object(o->sha1, 1, &rev);
336 if (ret)
337 break;
338 o = parse_object(t->tagged->sha1);
339 if (!o)
340 ret = error("Could not read object %s",
341 sha1_to_hex(t->tagged->sha1));
342 objects[i].item = o;
343 i--;
344 break;
346 case OBJ_TREE:
347 printf("%stree %s%s\n\n",
348 diff_get_color_opt(&rev.diffopt, DIFF_COMMIT),
349 name,
350 diff_get_color_opt(&rev.diffopt, DIFF_RESET));
351 read_tree_recursive((struct tree *)o, "", 0, 0, NULL,
352 show_tree_object, NULL);
353 break;
354 case OBJ_COMMIT:
355 rev.pending.nr = rev.pending.alloc = 0;
356 rev.pending.objects = NULL;
357 add_object_array(o, name, &rev.pending);
358 ret = cmd_log_walk(&rev);
359 break;
360 default:
361 ret = error("Unknown type: %d", o->type);
364 free(objects);
365 return ret;
369 * This is equivalent to "git log -g --abbrev-commit --pretty=oneline"
371 int cmd_log_reflog(int argc, const char **argv, const char *prefix)
373 struct rev_info rev;
375 git_config(git_log_config, NULL);
377 if (diff_use_color_default == -1)
378 diff_use_color_default = git_use_color_default;
380 init_revisions(&rev, prefix);
381 init_reflog_walk(&rev.reflog_info);
382 rev.abbrev_commit = 1;
383 rev.verbose_header = 1;
384 cmd_log_init(argc, argv, prefix, &rev);
387 * This means that we override whatever commit format the user gave
388 * on the cmd line. Sad, but cmd_log_init() currently doesn't
389 * allow us to set a different default.
391 rev.commit_format = CMIT_FMT_ONELINE;
392 rev.use_terminator = 1;
393 rev.always_show_header = 1;
396 * We get called through "git reflog", so unlike the other log
397 * routines, we need to set up our pager manually..
399 setup_pager();
401 return cmd_log_walk(&rev);
404 int cmd_log(int argc, const char **argv, const char *prefix)
406 struct rev_info rev;
408 git_config(git_log_config, NULL);
410 if (diff_use_color_default == -1)
411 diff_use_color_default = git_use_color_default;
413 init_revisions(&rev, prefix);
414 rev.always_show_header = 1;
415 cmd_log_init(argc, argv, prefix, &rev);
416 return cmd_log_walk(&rev);
419 /* format-patch */
421 static const char *fmt_patch_suffix = ".patch";
422 static int numbered = 0;
423 static int auto_number = 1;
425 static char *default_attach = NULL;
427 static char **extra_hdr;
428 static int extra_hdr_nr;
429 static int extra_hdr_alloc;
431 static char **extra_to;
432 static int extra_to_nr;
433 static int extra_to_alloc;
435 static char **extra_cc;
436 static int extra_cc_nr;
437 static int extra_cc_alloc;
439 static void add_header(const char *value)
441 int len = strlen(value);
442 while (len && value[len - 1] == '\n')
443 len--;
444 if (!strncasecmp(value, "to: ", 4)) {
445 ALLOC_GROW(extra_to, extra_to_nr + 1, extra_to_alloc);
446 extra_to[extra_to_nr++] = xstrndup(value + 4, len - 4);
447 return;
449 if (!strncasecmp(value, "cc: ", 4)) {
450 ALLOC_GROW(extra_cc, extra_cc_nr + 1, extra_cc_alloc);
451 extra_cc[extra_cc_nr++] = xstrndup(value + 4, len - 4);
452 return;
454 ALLOC_GROW(extra_hdr, extra_hdr_nr + 1, extra_hdr_alloc);
455 extra_hdr[extra_hdr_nr++] = xstrndup(value, len);
458 #define THREAD_SHALLOW 1
459 #define THREAD_DEEP 2
460 static int thread = 0;
461 static int do_signoff = 0;
463 static int git_format_config(const char *var, const char *value, void *cb)
465 if (!strcmp(var, "format.headers")) {
466 if (!value)
467 die("format.headers without value");
468 add_header(value);
469 return 0;
471 if (!strcmp(var, "format.suffix"))
472 return git_config_string(&fmt_patch_suffix, var, value);
473 if (!strcmp(var, "format.cc")) {
474 if (!value)
475 return config_error_nonbool(var);
476 ALLOC_GROW(extra_cc, extra_cc_nr + 1, extra_cc_alloc);
477 extra_cc[extra_cc_nr++] = xstrdup(value);
478 return 0;
480 if (!strcmp(var, "diff.color") || !strcmp(var, "color.diff")) {
481 return 0;
483 if (!strcmp(var, "format.numbered")) {
484 if (value && !strcasecmp(value, "auto")) {
485 auto_number = 1;
486 return 0;
488 numbered = git_config_bool(var, value);
489 auto_number = auto_number && numbered;
490 return 0;
492 if (!strcmp(var, "format.attach")) {
493 if (value && *value)
494 default_attach = xstrdup(value);
495 else
496 default_attach = xstrdup(git_version_string);
497 return 0;
499 if (!strcmp(var, "format.thread")) {
500 if (value && !strcasecmp(value, "deep")) {
501 thread = THREAD_DEEP;
502 return 0;
504 if (value && !strcasecmp(value, "shallow")) {
505 thread = THREAD_SHALLOW;
506 return 0;
508 thread = git_config_bool(var, value) && THREAD_SHALLOW;
509 return 0;
511 if (!strcmp(var, "format.signoff")) {
512 do_signoff = git_config_bool(var, value);
513 return 0;
516 return git_log_config(var, value, cb);
519 static FILE *realstdout = NULL;
520 static const char *output_directory = NULL;
521 static int outdir_offset;
523 static int reopen_stdout(struct commit *commit, struct rev_info *rev)
525 struct strbuf filename = STRBUF_INIT;
526 int suffix_len = strlen(fmt_patch_suffix) + 1;
528 if (output_directory) {
529 strbuf_addstr(&filename, output_directory);
530 if (filename.len >=
531 PATH_MAX - FORMAT_PATCH_NAME_MAX - suffix_len)
532 return error("name of output directory is too long");
533 if (filename.buf[filename.len - 1] != '/')
534 strbuf_addch(&filename, '/');
537 get_patch_filename(commit, rev->nr, fmt_patch_suffix, &filename);
539 if (!DIFF_OPT_TST(&rev->diffopt, QUIET))
540 fprintf(realstdout, "%s\n", filename.buf + outdir_offset);
542 if (freopen(filename.buf, "w", stdout) == NULL)
543 return error("Cannot open patch file %s", filename.buf);
545 strbuf_release(&filename);
546 return 0;
549 static void get_patch_ids(struct rev_info *rev, struct patch_ids *ids, const char *prefix)
551 struct rev_info check_rev;
552 struct commit *commit;
553 struct object *o1, *o2;
554 unsigned flags1, flags2;
556 if (rev->pending.nr != 2)
557 die("Need exactly one range.");
559 o1 = rev->pending.objects[0].item;
560 flags1 = o1->flags;
561 o2 = rev->pending.objects[1].item;
562 flags2 = o2->flags;
564 if ((flags1 & UNINTERESTING) == (flags2 & UNINTERESTING))
565 die("Not a range.");
567 init_patch_ids(ids);
569 /* given a range a..b get all patch ids for b..a */
570 init_revisions(&check_rev, prefix);
571 o1->flags ^= UNINTERESTING;
572 o2->flags ^= UNINTERESTING;
573 add_pending_object(&check_rev, o1, "o1");
574 add_pending_object(&check_rev, o2, "o2");
575 if (prepare_revision_walk(&check_rev))
576 die("revision walk setup failed");
578 while ((commit = get_revision(&check_rev)) != NULL) {
579 /* ignore merges */
580 if (commit->parents && commit->parents->next)
581 continue;
583 add_commit_patch_id(commit, ids);
586 /* reset for next revision walk */
587 clear_commit_marks((struct commit *)o1,
588 SEEN | UNINTERESTING | SHOWN | ADDED);
589 clear_commit_marks((struct commit *)o2,
590 SEEN | UNINTERESTING | SHOWN | ADDED);
591 o1->flags = flags1;
592 o2->flags = flags2;
595 static void gen_message_id(struct rev_info *info, char *base)
597 const char *committer = git_committer_info(IDENT_WARN_ON_NO_NAME);
598 const char *email_start = strrchr(committer, '<');
599 const char *email_end = strrchr(committer, '>');
600 struct strbuf buf = STRBUF_INIT;
601 if (!email_start || !email_end || email_start > email_end - 1)
602 die("Could not extract email from committer identity.");
603 strbuf_addf(&buf, "%s.%lu.git.%.*s", base,
604 (unsigned long) time(NULL),
605 (int)(email_end - email_start - 1), email_start + 1);
606 info->message_id = strbuf_detach(&buf, NULL);
609 static void make_cover_letter(struct rev_info *rev, int use_stdout,
610 int numbered, int numbered_files,
611 struct commit *origin,
612 int nr, struct commit **list, struct commit *head)
614 const char *committer;
615 const char *subject_start = NULL;
616 const char *body = "*** SUBJECT HERE ***\n\n*** BLURB HERE ***\n";
617 const char *msg;
618 const char *extra_headers = rev->extra_headers;
619 struct shortlog log;
620 struct strbuf sb = STRBUF_INIT;
621 int i;
622 const char *encoding = "utf-8";
623 struct diff_options opts;
624 int need_8bit_cte = 0;
625 struct commit *commit = NULL;
627 if (rev->commit_format != CMIT_FMT_EMAIL)
628 die("Cover letter needs email format");
630 committer = git_committer_info(0);
632 if (!numbered_files) {
634 * We fake a commit for the cover letter so we get the filename
635 * desired.
637 commit = xcalloc(1, sizeof(*commit));
638 commit->buffer = xmalloc(400);
639 snprintf(commit->buffer, 400,
640 "tree 0000000000000000000000000000000000000000\n"
641 "parent %s\n"
642 "author %s\n"
643 "committer %s\n\n"
644 "cover letter\n",
645 sha1_to_hex(head->object.sha1), committer, committer);
648 if (!use_stdout && reopen_stdout(commit, rev))
649 return;
651 if (commit) {
653 free(commit->buffer);
654 free(commit);
657 log_write_email_headers(rev, head, &subject_start, &extra_headers,
658 &need_8bit_cte);
660 msg = body;
661 pp_user_info(NULL, CMIT_FMT_EMAIL, &sb, committer, DATE_RFC2822,
662 encoding);
663 pp_title_line(CMIT_FMT_EMAIL, &msg, &sb, subject_start, extra_headers,
664 encoding, need_8bit_cte);
665 pp_remainder(CMIT_FMT_EMAIL, &msg, &sb, 0);
666 printf("%s\n", sb.buf);
668 strbuf_release(&sb);
670 shortlog_init(&log);
671 log.wrap_lines = 1;
672 log.wrap = 72;
673 log.in1 = 2;
674 log.in2 = 4;
675 for (i = 0; i < nr; i++)
676 shortlog_add_commit(&log, list[i]);
678 shortlog_output(&log);
681 * We can only do diffstat with a unique reference point
683 if (!origin)
684 return;
686 memcpy(&opts, &rev->diffopt, sizeof(opts));
687 opts.output_format = DIFF_FORMAT_SUMMARY | DIFF_FORMAT_DIFFSTAT;
689 diff_setup_done(&opts);
691 diff_tree_sha1(origin->tree->object.sha1,
692 head->tree->object.sha1,
693 "", &opts);
694 diffcore_std(&opts);
695 diff_flush(&opts);
697 printf("\n");
700 static const char *clean_message_id(const char *msg_id)
702 char ch;
703 const char *a, *z, *m;
705 m = msg_id;
706 while ((ch = *m) && (isspace(ch) || (ch == '<')))
707 m++;
708 a = m;
709 z = NULL;
710 while ((ch = *m)) {
711 if (!isspace(ch) && (ch != '>'))
712 z = m;
713 m++;
715 if (!z)
716 die("insane in-reply-to: %s", msg_id);
717 if (++z == m)
718 return a;
719 return xmemdupz(a, z - a);
722 static const char *set_outdir(const char *prefix, const char *output_directory)
724 if (output_directory && is_absolute_path(output_directory))
725 return output_directory;
727 if (!prefix || !*prefix) {
728 if (output_directory)
729 return output_directory;
730 /* The user did not explicitly ask for "./" */
731 outdir_offset = 2;
732 return "./";
735 outdir_offset = strlen(prefix);
736 if (!output_directory)
737 return prefix;
739 return xstrdup(prefix_filename(prefix, outdir_offset,
740 output_directory));
743 int cmd_format_patch(int argc, const char **argv, const char *prefix)
745 struct commit *commit;
746 struct commit **list = NULL;
747 struct rev_info rev;
748 int nr = 0, total, i, j;
749 int use_stdout = 0;
750 int start_number = -1;
751 int keep_subject = 0;
752 int numbered_files = 0; /* _just_ numbers */
753 int subject_prefix = 0;
754 int ignore_if_in_upstream = 0;
755 int cover_letter = 0;
756 int boundary_count = 0;
757 int no_binary_diff = 0;
758 int numbered_cmdline_opt = 0;
759 struct commit *origin = NULL, *head = NULL;
760 const char *in_reply_to = NULL;
761 struct patch_ids ids;
762 char *add_signoff = NULL;
763 struct strbuf buf = STRBUF_INIT;
765 git_config(git_format_config, NULL);
766 init_revisions(&rev, prefix);
767 rev.commit_format = CMIT_FMT_EMAIL;
768 rev.verbose_header = 1;
769 rev.diff = 1;
770 rev.combine_merges = 0;
771 rev.ignore_merges = 1;
772 DIFF_OPT_SET(&rev.diffopt, RECURSIVE);
774 rev.subject_prefix = fmt_patch_subject_prefix;
776 if (default_attach) {
777 rev.mime_boundary = default_attach;
778 rev.no_inline = 1;
782 * Parse the arguments before setup_revisions(), or something
783 * like "git format-patch -o a123 HEAD^.." may fail; a123 is
784 * possibly a valid SHA1.
786 for (i = 1, j = 1; i < argc; i++) {
787 if (!strcmp(argv[i], "--stdout"))
788 use_stdout = 1;
789 else if (!strcmp(argv[i], "-n") ||
790 !strcmp(argv[i], "--numbered")) {
791 numbered = 1;
792 numbered_cmdline_opt = 1;
794 else if (!strcmp(argv[i], "-N") ||
795 !strcmp(argv[i], "--no-numbered")) {
796 numbered = 0;
797 auto_number = 0;
799 else if (!prefixcmp(argv[i], "--start-number="))
800 start_number = strtol(argv[i] + 15, NULL, 10);
801 else if (!strcmp(argv[i], "--numbered-files"))
802 numbered_files = 1;
803 else if (!strcmp(argv[i], "--start-number")) {
804 i++;
805 if (i == argc)
806 die("Need a number for --start-number");
807 start_number = strtol(argv[i], NULL, 10);
809 else if (!prefixcmp(argv[i], "--cc=")) {
810 ALLOC_GROW(extra_cc, extra_cc_nr + 1, extra_cc_alloc);
811 extra_cc[extra_cc_nr++] = xstrdup(argv[i] + 5);
813 else if (!strcmp(argv[i], "-k") ||
814 !strcmp(argv[i], "--keep-subject")) {
815 keep_subject = 1;
816 rev.total = -1;
818 else if (!strcmp(argv[i], "--output-directory") ||
819 !strcmp(argv[i], "-o")) {
820 i++;
821 if (argc <= i)
822 die("Which directory?");
823 if (output_directory)
824 die("Two output directories?");
825 output_directory = argv[i];
827 else if (!strcmp(argv[i], "--signoff") ||
828 !strcmp(argv[i], "-s")) {
829 do_signoff = 1;
831 else if (!strcmp(argv[i], "--attach")) {
832 rev.mime_boundary = git_version_string;
833 rev.no_inline = 1;
835 else if (!prefixcmp(argv[i], "--attach=")) {
836 rev.mime_boundary = argv[i] + 9;
837 rev.no_inline = 1;
839 else if (!strcmp(argv[i], "--no-attach")) {
840 rev.mime_boundary = NULL;
841 rev.no_inline = 0;
843 else if (!strcmp(argv[i], "--inline")) {
844 rev.mime_boundary = git_version_string;
845 rev.no_inline = 0;
847 else if (!prefixcmp(argv[i], "--inline=")) {
848 rev.mime_boundary = argv[i] + 9;
849 rev.no_inline = 0;
851 else if (!strcmp(argv[i], "--ignore-if-in-upstream"))
852 ignore_if_in_upstream = 1;
853 else if (!strcmp(argv[i], "--thread")
854 || !strcmp(argv[i], "--thread=shallow"))
855 thread = THREAD_SHALLOW;
856 else if (!strcmp(argv[i], "--thread=deep"))
857 thread = THREAD_DEEP;
858 else if (!strcmp(argv[i], "--no-thread"))
859 thread = 0;
860 else if (!prefixcmp(argv[i], "--in-reply-to="))
861 in_reply_to = argv[i] + 14;
862 else if (!strcmp(argv[i], "--in-reply-to")) {
863 i++;
864 if (i == argc)
865 die("Need a Message-Id for --in-reply-to");
866 in_reply_to = argv[i];
867 } else if (!prefixcmp(argv[i], "--subject-prefix=")) {
868 subject_prefix = 1;
869 rev.subject_prefix = argv[i] + 17;
870 } else if (!prefixcmp(argv[i], "--suffix="))
871 fmt_patch_suffix = argv[i] + 9;
872 else if (!strcmp(argv[i], "--cover-letter"))
873 cover_letter = 1;
874 else if (!strcmp(argv[i], "--no-binary"))
875 no_binary_diff = 1;
876 else if (!prefixcmp(argv[i], "--add-header="))
877 add_header(argv[i] + 13);
878 else
879 argv[j++] = argv[i];
881 argc = j;
883 if (do_signoff) {
884 const char *committer;
885 const char *endpos;
886 committer = git_committer_info(IDENT_ERROR_ON_NO_NAME);
887 endpos = strchr(committer, '>');
888 if (!endpos)
889 die("bogus committer info %s", committer);
890 add_signoff = xmemdupz(committer, endpos - committer + 1);
893 for (i = 0; i < extra_hdr_nr; i++) {
894 strbuf_addstr(&buf, extra_hdr[i]);
895 strbuf_addch(&buf, '\n');
898 if (extra_to_nr)
899 strbuf_addstr(&buf, "To: ");
900 for (i = 0; i < extra_to_nr; i++) {
901 if (i)
902 strbuf_addstr(&buf, " ");
903 strbuf_addstr(&buf, extra_to[i]);
904 if (i + 1 < extra_to_nr)
905 strbuf_addch(&buf, ',');
906 strbuf_addch(&buf, '\n');
909 if (extra_cc_nr)
910 strbuf_addstr(&buf, "Cc: ");
911 for (i = 0; i < extra_cc_nr; i++) {
912 if (i)
913 strbuf_addstr(&buf, " ");
914 strbuf_addstr(&buf, extra_cc[i]);
915 if (i + 1 < extra_cc_nr)
916 strbuf_addch(&buf, ',');
917 strbuf_addch(&buf, '\n');
920 rev.extra_headers = strbuf_detach(&buf, 0);
922 if (start_number < 0)
923 start_number = 1;
926 * If numbered is set solely due to format.numbered in config,
927 * and it would conflict with --keep-subject (-k) from the
928 * command line, reset "numbered".
930 if (numbered && keep_subject && !numbered_cmdline_opt)
931 numbered = 0;
933 if (numbered && keep_subject)
934 die ("-n and -k are mutually exclusive.");
935 if (keep_subject && subject_prefix)
936 die ("--subject-prefix and -k are mutually exclusive.");
938 argc = setup_revisions(argc, argv, &rev, "HEAD");
939 if (argc > 1)
940 die ("unrecognized argument: %s", argv[1]);
942 if (!rev.diffopt.output_format
943 || rev.diffopt.output_format == DIFF_FORMAT_PATCH)
944 rev.diffopt.output_format = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_SUMMARY | DIFF_FORMAT_PATCH;
946 if (!DIFF_OPT_TST(&rev.diffopt, TEXT) && !no_binary_diff)
947 DIFF_OPT_SET(&rev.diffopt, BINARY);
949 if (!use_stdout)
950 output_directory = set_outdir(prefix, output_directory);
952 if (output_directory) {
953 if (use_stdout)
954 die("standard output, or directory, which one?");
955 if (mkdir(output_directory, 0777) < 0 && errno != EEXIST)
956 die("Could not create directory %s",
957 output_directory);
960 if (rev.pending.nr == 1) {
961 if (rev.max_count < 0 && !rev.show_root_diff) {
963 * This is traditional behaviour of "git format-patch
964 * origin" that prepares what the origin side still
965 * does not have.
967 rev.pending.objects[0].item->flags |= UNINTERESTING;
968 add_head_to_pending(&rev);
971 * Otherwise, it is "format-patch -22 HEAD", and/or
972 * "format-patch --root HEAD". The user wants
973 * get_revision() to do the usual traversal.
978 * We cannot move this anywhere earlier because we do want to
979 * know if --root was given explicitly from the comand line.
981 rev.show_root_diff = 1;
983 if (cover_letter) {
984 /* remember the range */
985 int i;
986 for (i = 0; i < rev.pending.nr; i++) {
987 struct object *o = rev.pending.objects[i].item;
988 if (!(o->flags & UNINTERESTING))
989 head = (struct commit *)o;
991 /* We can't generate a cover letter without any patches */
992 if (!head)
993 return 0;
996 if (ignore_if_in_upstream)
997 get_patch_ids(&rev, &ids, prefix);
999 if (!use_stdout)
1000 realstdout = xfdopen(xdup(1), "w");
1002 if (prepare_revision_walk(&rev))
1003 die("revision walk setup failed");
1004 rev.boundary = 1;
1005 while ((commit = get_revision(&rev)) != NULL) {
1006 if (commit->object.flags & BOUNDARY) {
1007 boundary_count++;
1008 origin = (boundary_count == 1) ? commit : NULL;
1009 continue;
1012 /* ignore merges */
1013 if (commit->parents && commit->parents->next)
1014 continue;
1016 if (ignore_if_in_upstream &&
1017 has_commit_patch_id(commit, &ids))
1018 continue;
1020 nr++;
1021 list = xrealloc(list, nr * sizeof(list[0]));
1022 list[nr - 1] = commit;
1024 total = nr;
1025 if (!keep_subject && auto_number && total > 1)
1026 numbered = 1;
1027 if (numbered)
1028 rev.total = total + start_number - 1;
1029 if (in_reply_to || thread || cover_letter)
1030 rev.ref_message_ids = xcalloc(1, sizeof(struct string_list));
1031 if (in_reply_to) {
1032 const char *msgid = clean_message_id(in_reply_to);
1033 string_list_append(msgid, rev.ref_message_ids);
1035 rev.numbered_files = numbered_files;
1036 rev.patch_suffix = fmt_patch_suffix;
1037 if (cover_letter) {
1038 if (thread)
1039 gen_message_id(&rev, "cover");
1040 make_cover_letter(&rev, use_stdout, numbered, numbered_files,
1041 origin, nr, list, head);
1042 total++;
1043 start_number--;
1045 rev.add_signoff = add_signoff;
1046 while (0 <= --nr) {
1047 int shown;
1048 commit = list[nr];
1049 rev.nr = total - nr + (start_number - 1);
1050 /* Make the second and subsequent mails replies to the first */
1051 if (thread) {
1052 /* Have we already had a message ID? */
1053 if (rev.message_id) {
1055 * For deep threading: make every mail
1056 * a reply to the previous one, no
1057 * matter what other options are set.
1059 * For shallow threading:
1061 * Without --cover-letter and
1062 * --in-reply-to, make every mail a
1063 * reply to the one before.
1065 * With --in-reply-to but no
1066 * --cover-letter, make every mail a
1067 * reply to the <reply-to>.
1069 * With --cover-letter, make every
1070 * mail but the cover letter a reply
1071 * to the cover letter. The cover
1072 * letter is a reply to the
1073 * --in-reply-to, if specified.
1075 if (thread == THREAD_SHALLOW
1076 && rev.ref_message_ids->nr > 0
1077 && (!cover_letter || rev.nr > 1))
1078 free(rev.message_id);
1079 else
1080 string_list_append(rev.message_id,
1081 rev.ref_message_ids);
1083 gen_message_id(&rev, sha1_to_hex(commit->object.sha1));
1086 if (!use_stdout && reopen_stdout(numbered_files ? NULL : commit,
1087 &rev))
1088 die("Failed to create output files");
1089 shown = log_tree_commit(&rev, commit);
1090 free(commit->buffer);
1091 commit->buffer = NULL;
1093 /* We put one extra blank line between formatted
1094 * patches and this flag is used by log-tree code
1095 * to see if it needs to emit a LF before showing
1096 * the log; when using one file per patch, we do
1097 * not want the extra blank line.
1099 if (!use_stdout)
1100 rev.shown_one = 0;
1101 if (shown) {
1102 if (rev.mime_boundary)
1103 printf("\n--%s%s--\n\n\n",
1104 mime_boundary_leader,
1105 rev.mime_boundary);
1106 else
1107 printf("-- \n%s\n\n", git_version_string);
1109 if (!use_stdout)
1110 fclose(stdout);
1112 free(list);
1113 if (ignore_if_in_upstream)
1114 free_patch_ids(&ids);
1115 return 0;
1118 static int add_pending_commit(const char *arg, struct rev_info *revs, int flags)
1120 unsigned char sha1[20];
1121 if (get_sha1(arg, sha1) == 0) {
1122 struct commit *commit = lookup_commit_reference(sha1);
1123 if (commit) {
1124 commit->object.flags |= flags;
1125 add_pending_object(revs, &commit->object, arg);
1126 return 0;
1129 return -1;
1132 static const char cherry_usage[] =
1133 "git cherry [-v] [<upstream> [<head> [<limit>]]]";
1134 int cmd_cherry(int argc, const char **argv, const char *prefix)
1136 struct rev_info revs;
1137 struct patch_ids ids;
1138 struct commit *commit;
1139 struct commit_list *list = NULL;
1140 struct branch *current_branch;
1141 const char *upstream;
1142 const char *head = "HEAD";
1143 const char *limit = NULL;
1144 int verbose = 0;
1146 if (argc > 1 && !strcmp(argv[1], "-v")) {
1147 verbose = 1;
1148 argc--;
1149 argv++;
1152 switch (argc) {
1153 case 4:
1154 limit = argv[3];
1155 /* FALLTHROUGH */
1156 case 3:
1157 head = argv[2];
1158 /* FALLTHROUGH */
1159 case 2:
1160 upstream = argv[1];
1161 break;
1162 default:
1163 current_branch = branch_get(NULL);
1164 if (!current_branch || !current_branch->merge
1165 || !current_branch->merge[0]
1166 || !current_branch->merge[0]->dst) {
1167 fprintf(stderr, "Could not find a tracked"
1168 " remote branch, please"
1169 " specify <upstream> manually.\n");
1170 usage(cherry_usage);
1173 upstream = current_branch->merge[0]->dst;
1176 init_revisions(&revs, prefix);
1177 revs.diff = 1;
1178 revs.combine_merges = 0;
1179 revs.ignore_merges = 1;
1180 DIFF_OPT_SET(&revs.diffopt, RECURSIVE);
1182 if (add_pending_commit(head, &revs, 0))
1183 die("Unknown commit %s", head);
1184 if (add_pending_commit(upstream, &revs, UNINTERESTING))
1185 die("Unknown commit %s", upstream);
1187 /* Don't say anything if head and upstream are the same. */
1188 if (revs.pending.nr == 2) {
1189 struct object_array_entry *o = revs.pending.objects;
1190 if (hashcmp(o[0].item->sha1, o[1].item->sha1) == 0)
1191 return 0;
1194 get_patch_ids(&revs, &ids, prefix);
1196 if (limit && add_pending_commit(limit, &revs, UNINTERESTING))
1197 die("Unknown commit %s", limit);
1199 /* reverse the list of commits */
1200 if (prepare_revision_walk(&revs))
1201 die("revision walk setup failed");
1202 while ((commit = get_revision(&revs)) != NULL) {
1203 /* ignore merges */
1204 if (commit->parents && commit->parents->next)
1205 continue;
1207 commit_list_insert(commit, &list);
1210 while (list) {
1211 char sign = '+';
1213 commit = list->item;
1214 if (has_commit_patch_id(commit, &ids))
1215 sign = '-';
1217 if (verbose) {
1218 struct strbuf buf = STRBUF_INIT;
1219 pretty_print_commit(CMIT_FMT_ONELINE, commit,
1220 &buf, 0, NULL, NULL, 0, 0);
1221 printf("%c %s %s\n", sign,
1222 sha1_to_hex(commit->object.sha1), buf.buf);
1223 strbuf_release(&buf);
1225 else {
1226 printf("%c %s\n", sign,
1227 sha1_to_hex(commit->object.sha1));
1230 list = list->next;
1233 free_patch_ids(&ids);
1234 return 0;