filter-branch: add git_commit_non_empty_tree and --prune-empty.
[4msysgit-hv.git] / builtin-log.c
blob4a02ee987ac421f3a89a53873f00d50760cd0044
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"
20 /* Set a default date-time format for git log ("log.date" config variable) */
21 static const char *default_date_mode = NULL;
23 static int default_show_root = 1;
24 static const char *fmt_patch_subject_prefix = "PATCH";
25 static const char *fmt_pretty;
27 static void cmd_log_init(int argc, const char **argv, const char *prefix,
28 struct rev_info *rev)
30 int i;
32 rev->abbrev = DEFAULT_ABBREV;
33 rev->commit_format = CMIT_FMT_DEFAULT;
34 if (fmt_pretty)
35 get_commit_format(fmt_pretty, rev);
36 rev->verbose_header = 1;
37 DIFF_OPT_SET(&rev->diffopt, RECURSIVE);
38 rev->show_root_diff = default_show_root;
39 rev->subject_prefix = fmt_patch_subject_prefix;
40 DIFF_OPT_SET(&rev->diffopt, ALLOW_TEXTCONV);
42 if (default_date_mode)
43 rev->date_mode = parse_date_format(default_date_mode);
45 argc = setup_revisions(argc, argv, rev, "HEAD");
47 if (rev->diffopt.pickaxe || rev->diffopt.filter)
48 rev->always_show_header = 0;
49 if (DIFF_OPT_TST(&rev->diffopt, FOLLOW_RENAMES)) {
50 rev->always_show_header = 0;
51 if (rev->diffopt.nr_paths != 1)
52 usage("git logs can only follow renames on one pathname at a time");
54 for (i = 1; i < argc; i++) {
55 const char *arg = argv[i];
56 if (!strcmp(arg, "--decorate")) {
57 load_ref_decorations();
58 rev->show_decorations = 1;
59 } else if (!strcmp(arg, "--source")) {
60 rev->show_source = 1;
61 } else
62 die("unrecognized argument: %s", arg);
67 * This gives a rough estimate for how many commits we
68 * will print out in the list.
70 static int estimate_commit_count(struct rev_info *rev, struct commit_list *list)
72 int n = 0;
74 while (list) {
75 struct commit *commit = list->item;
76 unsigned int flags = commit->object.flags;
77 list = list->next;
78 if (!(flags & (TREESAME | UNINTERESTING)))
79 n++;
81 return n;
84 static void show_early_header(struct rev_info *rev, const char *stage, int nr)
86 if (rev->shown_one) {
87 rev->shown_one = 0;
88 if (rev->commit_format != CMIT_FMT_ONELINE)
89 putchar(rev->diffopt.line_termination);
91 printf("Final output: %d %s\n", nr, stage);
94 struct itimerval early_output_timer;
96 static void log_show_early(struct rev_info *revs, struct commit_list *list)
98 int i = revs->early_output;
99 int show_header = 1;
101 sort_in_topological_order(&list, revs->lifo);
102 while (list && i) {
103 struct commit *commit = list->item;
104 switch (simplify_commit(revs, commit)) {
105 case commit_show:
106 if (show_header) {
107 int n = estimate_commit_count(revs, list);
108 show_early_header(revs, "incomplete", n);
109 show_header = 0;
111 log_tree_commit(revs, commit);
112 i--;
113 break;
114 case commit_ignore:
115 break;
116 case commit_error:
117 return;
119 list = list->next;
122 /* Did we already get enough commits for the early output? */
123 if (!i)
124 return;
127 * ..if no, then repeat it twice a second until we
128 * do.
130 * NOTE! We don't use "it_interval", because if the
131 * reader isn't listening, we want our output to be
132 * throttled by the writing, and not have the timer
133 * trigger every second even if we're blocked on a
134 * reader!
136 early_output_timer.it_value.tv_sec = 0;
137 early_output_timer.it_value.tv_usec = 500000;
138 setitimer(ITIMER_REAL, &early_output_timer, NULL);
141 static void early_output(int signal)
143 show_early_output = log_show_early;
146 static void setup_early_output(struct rev_info *rev)
148 struct sigaction sa;
151 * Set up the signal handler, minimally intrusively:
152 * we only set a single volatile integer word (not
153 * using sigatomic_t - trying to avoid unnecessary
154 * system dependencies and headers), and using
155 * SA_RESTART.
157 memset(&sa, 0, sizeof(sa));
158 sa.sa_handler = early_output;
159 sigemptyset(&sa.sa_mask);
160 sa.sa_flags = SA_RESTART;
161 sigaction(SIGALRM, &sa, NULL);
164 * If we can get the whole output in less than a
165 * tenth of a second, don't even bother doing the
166 * early-output thing..
168 * This is a one-time-only trigger.
170 early_output_timer.it_value.tv_sec = 0;
171 early_output_timer.it_value.tv_usec = 100000;
172 setitimer(ITIMER_REAL, &early_output_timer, NULL);
175 static void finish_early_output(struct rev_info *rev)
177 int n = estimate_commit_count(rev, rev->commits);
178 signal(SIGALRM, SIG_IGN);
179 show_early_header(rev, "done", n);
182 static int cmd_log_walk(struct rev_info *rev)
184 struct commit *commit;
186 if (rev->early_output)
187 setup_early_output(rev);
189 if (prepare_revision_walk(rev))
190 die("revision walk setup failed");
192 if (rev->early_output)
193 finish_early_output(rev);
196 * For --check and --exit-code, the exit code is based on CHECK_FAILED
197 * and HAS_CHANGES being accumulated in rev->diffopt, so be careful to
198 * retain that state information if replacing rev->diffopt in this loop
200 while ((commit = get_revision(rev)) != NULL) {
201 log_tree_commit(rev, commit);
202 if (!rev->reflog_info) {
203 /* we allow cycles in reflog ancestry */
204 free(commit->buffer);
205 commit->buffer = NULL;
207 free_commit_list(commit->parents);
208 commit->parents = NULL;
210 if (rev->diffopt.output_format & DIFF_FORMAT_CHECKDIFF &&
211 DIFF_OPT_TST(&rev->diffopt, CHECK_FAILED)) {
212 return 02;
214 return diff_result_code(&rev->diffopt, 0);
217 static int git_log_config(const char *var, const char *value, void *cb)
219 if (!strcmp(var, "format.pretty"))
220 return git_config_string(&fmt_pretty, var, value);
221 if (!strcmp(var, "format.subjectprefix"))
222 return git_config_string(&fmt_patch_subject_prefix, var, value);
223 if (!strcmp(var, "log.date"))
224 return git_config_string(&default_date_mode, var, value);
225 if (!strcmp(var, "log.showroot")) {
226 default_show_root = git_config_bool(var, value);
227 return 0;
229 return git_diff_ui_config(var, value, cb);
232 int cmd_whatchanged(int argc, const char **argv, const char *prefix)
234 struct rev_info rev;
236 git_config(git_log_config, NULL);
238 if (diff_use_color_default == -1)
239 diff_use_color_default = git_use_color_default;
241 init_revisions(&rev, prefix);
242 rev.diff = 1;
243 rev.simplify_history = 0;
244 cmd_log_init(argc, argv, prefix, &rev);
245 if (!rev.diffopt.output_format)
246 rev.diffopt.output_format = DIFF_FORMAT_RAW;
247 return cmd_log_walk(&rev);
250 static void show_tagger(char *buf, int len, struct rev_info *rev)
252 struct strbuf out = STRBUF_INIT;
254 pp_user_info("Tagger", rev->commit_format, &out, buf, rev->date_mode,
255 git_log_output_encoding ?
256 git_log_output_encoding: git_commit_encoding);
257 printf("%s\n", out.buf);
258 strbuf_release(&out);
261 static int show_object(const unsigned char *sha1, int show_tag_object,
262 struct rev_info *rev)
264 unsigned long size;
265 enum object_type type;
266 char *buf = read_sha1_file(sha1, &type, &size);
267 int offset = 0;
269 if (!buf)
270 return error("Could not read object %s", sha1_to_hex(sha1));
272 if (show_tag_object)
273 while (offset < size && buf[offset] != '\n') {
274 int new_offset = offset + 1;
275 while (new_offset < size && buf[new_offset++] != '\n')
276 ; /* do nothing */
277 if (!prefixcmp(buf + offset, "tagger "))
278 show_tagger(buf + offset + 7,
279 new_offset - offset - 7, rev);
280 offset = new_offset;
283 if (offset < size)
284 fwrite(buf + offset, size - offset, 1, stdout);
285 free(buf);
286 return 0;
289 static int show_tree_object(const unsigned char *sha1,
290 const char *base, int baselen,
291 const char *pathname, unsigned mode, int stage, void *context)
293 printf("%s%s\n", pathname, S_ISDIR(mode) ? "/" : "");
294 return 0;
297 int cmd_show(int argc, const char **argv, const char *prefix)
299 struct rev_info rev;
300 struct object_array_entry *objects;
301 int i, count, ret = 0;
303 git_config(git_log_config, NULL);
305 if (diff_use_color_default == -1)
306 diff_use_color_default = git_use_color_default;
308 init_revisions(&rev, prefix);
309 rev.diff = 1;
310 rev.combine_merges = 1;
311 rev.dense_combined_merges = 1;
312 rev.always_show_header = 1;
313 rev.ignore_merges = 0;
314 rev.no_walk = 1;
315 cmd_log_init(argc, argv, prefix, &rev);
317 count = rev.pending.nr;
318 objects = rev.pending.objects;
319 for (i = 0; i < count && !ret; i++) {
320 struct object *o = objects[i].item;
321 const char *name = objects[i].name;
322 switch (o->type) {
323 case OBJ_BLOB:
324 ret = show_object(o->sha1, 0, NULL);
325 break;
326 case OBJ_TAG: {
327 struct tag *t = (struct tag *)o;
329 printf("%stag %s%s\n",
330 diff_get_color_opt(&rev.diffopt, DIFF_COMMIT),
331 t->tag,
332 diff_get_color_opt(&rev.diffopt, DIFF_RESET));
333 ret = show_object(o->sha1, 1, &rev);
334 if (ret)
335 break;
336 o = parse_object(t->tagged->sha1);
337 if (!o)
338 ret = error("Could not read object %s",
339 sha1_to_hex(t->tagged->sha1));
340 objects[i].item = o;
341 i--;
342 break;
344 case OBJ_TREE:
345 printf("%stree %s%s\n\n",
346 diff_get_color_opt(&rev.diffopt, DIFF_COMMIT),
347 name,
348 diff_get_color_opt(&rev.diffopt, DIFF_RESET));
349 read_tree_recursive((struct tree *)o, "", 0, 0, NULL,
350 show_tree_object, NULL);
351 break;
352 case OBJ_COMMIT:
353 rev.pending.nr = rev.pending.alloc = 0;
354 rev.pending.objects = NULL;
355 add_object_array(o, name, &rev.pending);
356 ret = cmd_log_walk(&rev);
357 break;
358 default:
359 ret = error("Unknown type: %d", o->type);
362 free(objects);
363 return ret;
367 * This is equivalent to "git log -g --abbrev-commit --pretty=oneline"
369 int cmd_log_reflog(int argc, const char **argv, const char *prefix)
371 struct rev_info rev;
373 git_config(git_log_config, NULL);
375 if (diff_use_color_default == -1)
376 diff_use_color_default = git_use_color_default;
378 init_revisions(&rev, prefix);
379 init_reflog_walk(&rev.reflog_info);
380 rev.abbrev_commit = 1;
381 rev.verbose_header = 1;
382 cmd_log_init(argc, argv, prefix, &rev);
385 * This means that we override whatever commit format the user gave
386 * on the cmd line. Sad, but cmd_log_init() currently doesn't
387 * allow us to set a different default.
389 rev.commit_format = CMIT_FMT_ONELINE;
390 rev.use_terminator = 1;
391 rev.always_show_header = 1;
394 * We get called through "git reflog", so unlike the other log
395 * routines, we need to set up our pager manually..
397 setup_pager();
399 return cmd_log_walk(&rev);
402 int cmd_log(int argc, const char **argv, const char *prefix)
404 struct rev_info rev;
406 git_config(git_log_config, NULL);
408 if (diff_use_color_default == -1)
409 diff_use_color_default = git_use_color_default;
411 init_revisions(&rev, prefix);
412 rev.always_show_header = 1;
413 cmd_log_init(argc, argv, prefix, &rev);
414 return cmd_log_walk(&rev);
417 /* format-patch */
418 #define FORMAT_PATCH_NAME_MAX 64
420 static int istitlechar(char c)
422 return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
423 (c >= '0' && c <= '9') || c == '.' || c == '_';
426 static const char *fmt_patch_suffix = ".patch";
427 static int numbered = 0;
428 static int auto_number = 1;
430 static char **extra_hdr;
431 static int extra_hdr_nr;
432 static int extra_hdr_alloc;
434 static char **extra_to;
435 static int extra_to_nr;
436 static int extra_to_alloc;
438 static char **extra_cc;
439 static int extra_cc_nr;
440 static int extra_cc_alloc;
442 static void add_header(const char *value)
444 int len = strlen(value);
445 while (len && value[len - 1] == '\n')
446 len--;
447 if (!strncasecmp(value, "to: ", 4)) {
448 ALLOC_GROW(extra_to, extra_to_nr + 1, extra_to_alloc);
449 extra_to[extra_to_nr++] = xstrndup(value + 4, len - 4);
450 return;
452 if (!strncasecmp(value, "cc: ", 4)) {
453 ALLOC_GROW(extra_cc, extra_cc_nr + 1, extra_cc_alloc);
454 extra_cc[extra_cc_nr++] = xstrndup(value + 4, len - 4);
455 return;
457 ALLOC_GROW(extra_hdr, extra_hdr_nr + 1, extra_hdr_alloc);
458 extra_hdr[extra_hdr_nr++] = xstrndup(value, len);
461 static int git_format_config(const char *var, const char *value, void *cb)
463 if (!strcmp(var, "format.headers")) {
464 if (!value)
465 die("format.headers without value");
466 add_header(value);
467 return 0;
469 if (!strcmp(var, "format.suffix"))
470 return git_config_string(&fmt_patch_suffix, var, value);
471 if (!strcmp(var, "format.cc")) {
472 if (!value)
473 return config_error_nonbool(var);
474 ALLOC_GROW(extra_cc, extra_cc_nr + 1, extra_cc_alloc);
475 extra_cc[extra_cc_nr++] = xstrdup(value);
476 return 0;
478 if (!strcmp(var, "diff.color") || !strcmp(var, "color.diff")) {
479 return 0;
481 if (!strcmp(var, "format.numbered")) {
482 if (value && !strcasecmp(value, "auto")) {
483 auto_number = 1;
484 return 0;
486 numbered = git_config_bool(var, value);
487 auto_number = auto_number && numbered;
488 return 0;
491 return git_log_config(var, value, cb);
495 static const char *get_oneline_for_filename(struct commit *commit,
496 int keep_subject)
498 static char filename[PATH_MAX];
499 char *sol;
500 int len = 0;
501 int suffix_len = strlen(fmt_patch_suffix) + 1;
503 sol = strstr(commit->buffer, "\n\n");
504 if (!sol)
505 filename[0] = '\0';
506 else {
507 int j, space = 0;
509 sol += 2;
510 /* strip [PATCH] or [PATCH blabla] */
511 if (!keep_subject && !prefixcmp(sol, "[PATCH")) {
512 char *eos = strchr(sol + 6, ']');
513 if (eos) {
514 while (isspace(*eos))
515 eos++;
516 sol = eos;
520 for (j = 0;
521 j < FORMAT_PATCH_NAME_MAX - suffix_len - 5 &&
522 len < sizeof(filename) - suffix_len &&
523 sol[j] && sol[j] != '\n';
524 j++) {
525 if (istitlechar(sol[j])) {
526 if (space) {
527 filename[len++] = '-';
528 space = 0;
530 filename[len++] = sol[j];
531 if (sol[j] == '.')
532 while (sol[j + 1] == '.')
533 j++;
534 } else
535 space = 1;
537 while (filename[len - 1] == '.'
538 || filename[len - 1] == '-')
539 len--;
540 filename[len] = '\0';
542 return filename;
545 static FILE *realstdout = NULL;
546 static const char *output_directory = NULL;
548 static int reopen_stdout(const char *oneline, int nr, int total)
550 char filename[PATH_MAX];
551 int len = 0;
552 int suffix_len = strlen(fmt_patch_suffix) + 1;
554 if (output_directory) {
555 len = snprintf(filename, sizeof(filename), "%s",
556 output_directory);
557 if (len >=
558 sizeof(filename) - FORMAT_PATCH_NAME_MAX - suffix_len)
559 return error("name of output directory is too long");
560 if (filename[len - 1] != '/')
561 filename[len++] = '/';
564 if (!oneline)
565 len += sprintf(filename + len, "%d", nr);
566 else {
567 len += sprintf(filename + len, "%04d-", nr);
568 len += snprintf(filename + len, sizeof(filename) - len - 1
569 - suffix_len, "%s", oneline);
570 strcpy(filename + len, fmt_patch_suffix);
573 fprintf(realstdout, "%s\n", filename);
574 if (freopen(filename, "w", stdout) == NULL)
575 return error("Cannot open patch file %s",filename);
577 return 0;
580 static void get_patch_ids(struct rev_info *rev, struct patch_ids *ids, const char *prefix)
582 struct rev_info check_rev;
583 struct commit *commit;
584 struct object *o1, *o2;
585 unsigned flags1, flags2;
587 if (rev->pending.nr != 2)
588 die("Need exactly one range.");
590 o1 = rev->pending.objects[0].item;
591 flags1 = o1->flags;
592 o2 = rev->pending.objects[1].item;
593 flags2 = o2->flags;
595 if ((flags1 & UNINTERESTING) == (flags2 & UNINTERESTING))
596 die("Not a range.");
598 init_patch_ids(ids);
600 /* given a range a..b get all patch ids for b..a */
601 init_revisions(&check_rev, prefix);
602 o1->flags ^= UNINTERESTING;
603 o2->flags ^= UNINTERESTING;
604 add_pending_object(&check_rev, o1, "o1");
605 add_pending_object(&check_rev, o2, "o2");
606 if (prepare_revision_walk(&check_rev))
607 die("revision walk setup failed");
609 while ((commit = get_revision(&check_rev)) != NULL) {
610 /* ignore merges */
611 if (commit->parents && commit->parents->next)
612 continue;
614 add_commit_patch_id(commit, ids);
617 /* reset for next revision walk */
618 clear_commit_marks((struct commit *)o1,
619 SEEN | UNINTERESTING | SHOWN | ADDED);
620 clear_commit_marks((struct commit *)o2,
621 SEEN | UNINTERESTING | SHOWN | ADDED);
622 o1->flags = flags1;
623 o2->flags = flags2;
626 static void gen_message_id(struct rev_info *info, char *base)
628 const char *committer = git_committer_info(IDENT_WARN_ON_NO_NAME);
629 const char *email_start = strrchr(committer, '<');
630 const char *email_end = strrchr(committer, '>');
631 struct strbuf buf = STRBUF_INIT;
632 if (!email_start || !email_end || email_start > email_end - 1)
633 die("Could not extract email from committer identity.");
634 strbuf_addf(&buf, "%s.%lu.git.%.*s", base,
635 (unsigned long) time(NULL),
636 (int)(email_end - email_start - 1), email_start + 1);
637 info->message_id = strbuf_detach(&buf, NULL);
640 static void make_cover_letter(struct rev_info *rev, int use_stdout,
641 int numbered, int numbered_files,
642 struct commit *origin,
643 int nr, struct commit **list, struct commit *head)
645 const char *committer;
646 char *head_sha1;
647 const char *subject_start = NULL;
648 const char *body = "*** SUBJECT HERE ***\n\n*** BLURB HERE ***\n";
649 const char *msg;
650 const char *extra_headers = rev->extra_headers;
651 struct shortlog log;
652 struct strbuf sb = STRBUF_INIT;
653 int i;
654 const char *encoding = "utf-8";
655 struct diff_options opts;
656 int need_8bit_cte = 0;
658 if (rev->commit_format != CMIT_FMT_EMAIL)
659 die("Cover letter needs email format");
661 if (!use_stdout && reopen_stdout(numbered_files ?
662 NULL : "cover-letter", 0, rev->total))
663 return;
665 head_sha1 = sha1_to_hex(head->object.sha1);
667 log_write_email_headers(rev, head_sha1, &subject_start, &extra_headers,
668 &need_8bit_cte);
670 committer = git_committer_info(0);
672 msg = body;
673 pp_user_info(NULL, CMIT_FMT_EMAIL, &sb, committer, DATE_RFC2822,
674 encoding);
675 pp_title_line(CMIT_FMT_EMAIL, &msg, &sb, subject_start, extra_headers,
676 encoding, need_8bit_cte);
677 pp_remainder(CMIT_FMT_EMAIL, &msg, &sb, 0);
678 printf("%s\n", sb.buf);
680 strbuf_release(&sb);
682 shortlog_init(&log);
683 log.wrap_lines = 1;
684 log.wrap = 72;
685 log.in1 = 2;
686 log.in2 = 4;
687 for (i = 0; i < nr; i++)
688 shortlog_add_commit(&log, list[i]);
690 shortlog_output(&log);
693 * We can only do diffstat with a unique reference point
695 if (!origin)
696 return;
698 memcpy(&opts, &rev->diffopt, sizeof(opts));
699 opts.output_format = DIFF_FORMAT_SUMMARY | DIFF_FORMAT_DIFFSTAT;
701 diff_setup_done(&opts);
703 diff_tree_sha1(origin->tree->object.sha1,
704 head->tree->object.sha1,
705 "", &opts);
706 diffcore_std(&opts);
707 diff_flush(&opts);
709 printf("\n");
712 static const char *clean_message_id(const char *msg_id)
714 char ch;
715 const char *a, *z, *m;
717 m = msg_id;
718 while ((ch = *m) && (isspace(ch) || (ch == '<')))
719 m++;
720 a = m;
721 z = NULL;
722 while ((ch = *m)) {
723 if (!isspace(ch) && (ch != '>'))
724 z = m;
725 m++;
727 if (!z)
728 die("insane in-reply-to: %s", msg_id);
729 if (++z == m)
730 return a;
731 return xmemdupz(a, z - a);
734 int cmd_format_patch(int argc, const char **argv, const char *prefix)
736 struct commit *commit;
737 struct commit **list = NULL;
738 struct rev_info rev;
739 int nr = 0, total, i, j;
740 int use_stdout = 0;
741 int start_number = -1;
742 int keep_subject = 0;
743 int numbered_files = 0; /* _just_ numbers */
744 int subject_prefix = 0;
745 int ignore_if_in_upstream = 0;
746 int thread = 0;
747 int cover_letter = 0;
748 int boundary_count = 0;
749 int no_binary_diff = 0;
750 struct commit *origin = NULL, *head = NULL;
751 const char *in_reply_to = NULL;
752 struct patch_ids ids;
753 char *add_signoff = NULL;
754 struct strbuf buf = STRBUF_INIT;
756 git_config(git_format_config, NULL);
757 init_revisions(&rev, prefix);
758 rev.commit_format = CMIT_FMT_EMAIL;
759 rev.verbose_header = 1;
760 rev.diff = 1;
761 rev.combine_merges = 0;
762 rev.ignore_merges = 1;
763 DIFF_OPT_SET(&rev.diffopt, RECURSIVE);
765 rev.subject_prefix = fmt_patch_subject_prefix;
768 * Parse the arguments before setup_revisions(), or something
769 * like "git format-patch -o a123 HEAD^.." may fail; a123 is
770 * possibly a valid SHA1.
772 for (i = 1, j = 1; i < argc; i++) {
773 if (!strcmp(argv[i], "--stdout"))
774 use_stdout = 1;
775 else if (!strcmp(argv[i], "-n") ||
776 !strcmp(argv[i], "--numbered"))
777 numbered = 1;
778 else if (!strcmp(argv[i], "-N") ||
779 !strcmp(argv[i], "--no-numbered")) {
780 numbered = 0;
781 auto_number = 0;
783 else if (!prefixcmp(argv[i], "--start-number="))
784 start_number = strtol(argv[i] + 15, NULL, 10);
785 else if (!strcmp(argv[i], "--numbered-files"))
786 numbered_files = 1;
787 else if (!strcmp(argv[i], "--start-number")) {
788 i++;
789 if (i == argc)
790 die("Need a number for --start-number");
791 start_number = strtol(argv[i], NULL, 10);
793 else if (!prefixcmp(argv[i], "--cc=")) {
794 ALLOC_GROW(extra_cc, extra_cc_nr + 1, extra_cc_alloc);
795 extra_cc[extra_cc_nr++] = xstrdup(argv[i] + 5);
797 else if (!strcmp(argv[i], "-k") ||
798 !strcmp(argv[i], "--keep-subject")) {
799 keep_subject = 1;
800 rev.total = -1;
802 else if (!strcmp(argv[i], "--output-directory") ||
803 !strcmp(argv[i], "-o")) {
804 i++;
805 if (argc <= i)
806 die("Which directory?");
807 if (output_directory)
808 die("Two output directories?");
809 output_directory = argv[i];
811 else if (!strcmp(argv[i], "--signoff") ||
812 !strcmp(argv[i], "-s")) {
813 const char *committer;
814 const char *endpos;
815 committer = git_committer_info(IDENT_ERROR_ON_NO_NAME);
816 endpos = strchr(committer, '>');
817 if (!endpos)
818 die("bogus committer info %s", committer);
819 add_signoff = xmemdupz(committer, endpos - committer + 1);
821 else if (!strcmp(argv[i], "--attach")) {
822 rev.mime_boundary = git_version_string;
823 rev.no_inline = 1;
825 else if (!prefixcmp(argv[i], "--attach=")) {
826 rev.mime_boundary = argv[i] + 9;
827 rev.no_inline = 1;
829 else if (!strcmp(argv[i], "--inline")) {
830 rev.mime_boundary = git_version_string;
831 rev.no_inline = 0;
833 else if (!prefixcmp(argv[i], "--inline=")) {
834 rev.mime_boundary = argv[i] + 9;
835 rev.no_inline = 0;
837 else if (!strcmp(argv[i], "--ignore-if-in-upstream"))
838 ignore_if_in_upstream = 1;
839 else if (!strcmp(argv[i], "--thread"))
840 thread = 1;
841 else if (!prefixcmp(argv[i], "--in-reply-to="))
842 in_reply_to = argv[i] + 14;
843 else if (!strcmp(argv[i], "--in-reply-to")) {
844 i++;
845 if (i == argc)
846 die("Need a Message-Id for --in-reply-to");
847 in_reply_to = argv[i];
848 } else if (!prefixcmp(argv[i], "--subject-prefix=")) {
849 subject_prefix = 1;
850 rev.subject_prefix = argv[i] + 17;
851 } else if (!prefixcmp(argv[i], "--suffix="))
852 fmt_patch_suffix = argv[i] + 9;
853 else if (!strcmp(argv[i], "--cover-letter"))
854 cover_letter = 1;
855 else if (!strcmp(argv[i], "--no-binary"))
856 no_binary_diff = 1;
857 else
858 argv[j++] = argv[i];
860 argc = j;
862 for (i = 0; i < extra_hdr_nr; i++) {
863 strbuf_addstr(&buf, extra_hdr[i]);
864 strbuf_addch(&buf, '\n');
867 if (extra_to_nr)
868 strbuf_addstr(&buf, "To: ");
869 for (i = 0; i < extra_to_nr; i++) {
870 if (i)
871 strbuf_addstr(&buf, " ");
872 strbuf_addstr(&buf, extra_to[i]);
873 if (i + 1 < extra_to_nr)
874 strbuf_addch(&buf, ',');
875 strbuf_addch(&buf, '\n');
878 if (extra_cc_nr)
879 strbuf_addstr(&buf, "Cc: ");
880 for (i = 0; i < extra_cc_nr; i++) {
881 if (i)
882 strbuf_addstr(&buf, " ");
883 strbuf_addstr(&buf, extra_cc[i]);
884 if (i + 1 < extra_cc_nr)
885 strbuf_addch(&buf, ',');
886 strbuf_addch(&buf, '\n');
889 rev.extra_headers = strbuf_detach(&buf, 0);
891 if (start_number < 0)
892 start_number = 1;
893 if (numbered && keep_subject)
894 die ("-n and -k are mutually exclusive.");
895 if (keep_subject && subject_prefix)
896 die ("--subject-prefix and -k are mutually exclusive.");
897 if (numbered_files && use_stdout)
898 die ("--numbered-files and --stdout are mutually exclusive.");
900 argc = setup_revisions(argc, argv, &rev, "HEAD");
901 if (argc > 1)
902 die ("unrecognized argument: %s", argv[1]);
904 if (!rev.diffopt.output_format
905 || rev.diffopt.output_format == DIFF_FORMAT_PATCH)
906 rev.diffopt.output_format = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_SUMMARY | DIFF_FORMAT_PATCH;
908 if (!DIFF_OPT_TST(&rev.diffopt, TEXT) && !no_binary_diff)
909 DIFF_OPT_SET(&rev.diffopt, BINARY);
911 if (!output_directory && !use_stdout)
912 output_directory = prefix;
914 if (output_directory) {
915 if (use_stdout)
916 die("standard output, or directory, which one?");
917 if (mkdir(output_directory, 0777) < 0 && errno != EEXIST)
918 die("Could not create directory %s",
919 output_directory);
922 if (rev.pending.nr == 1) {
923 if (rev.max_count < 0 && !rev.show_root_diff) {
925 * This is traditional behaviour of "git format-patch
926 * origin" that prepares what the origin side still
927 * does not have.
929 rev.pending.objects[0].item->flags |= UNINTERESTING;
930 add_head_to_pending(&rev);
933 * Otherwise, it is "format-patch -22 HEAD", and/or
934 * "format-patch --root HEAD". The user wants
935 * get_revision() to do the usual traversal.
938 if (cover_letter) {
939 /* remember the range */
940 int i;
941 for (i = 0; i < rev.pending.nr; i++) {
942 struct object *o = rev.pending.objects[i].item;
943 if (!(o->flags & UNINTERESTING))
944 head = (struct commit *)o;
946 /* We can't generate a cover letter without any patches */
947 if (!head)
948 return 0;
951 if (ignore_if_in_upstream)
952 get_patch_ids(&rev, &ids, prefix);
954 if (!use_stdout)
955 realstdout = xfdopen(xdup(1), "w");
957 if (prepare_revision_walk(&rev))
958 die("revision walk setup failed");
959 rev.boundary = 1;
960 while ((commit = get_revision(&rev)) != NULL) {
961 if (commit->object.flags & BOUNDARY) {
962 boundary_count++;
963 origin = (boundary_count == 1) ? commit : NULL;
964 continue;
967 /* ignore merges */
968 if (commit->parents && commit->parents->next)
969 continue;
971 if (ignore_if_in_upstream &&
972 has_commit_patch_id(commit, &ids))
973 continue;
975 nr++;
976 list = xrealloc(list, nr * sizeof(list[0]));
977 list[nr - 1] = commit;
979 total = nr;
980 if (!keep_subject && auto_number && total > 1)
981 numbered = 1;
982 if (numbered)
983 rev.total = total + start_number - 1;
984 if (in_reply_to)
985 rev.ref_message_id = clean_message_id(in_reply_to);
986 if (cover_letter) {
987 if (thread)
988 gen_message_id(&rev, "cover");
989 make_cover_letter(&rev, use_stdout, numbered, numbered_files,
990 origin, nr, list, head);
991 total++;
992 start_number--;
994 rev.add_signoff = add_signoff;
995 while (0 <= --nr) {
996 int shown;
997 commit = list[nr];
998 rev.nr = total - nr + (start_number - 1);
999 /* Make the second and subsequent mails replies to the first */
1000 if (thread) {
1001 /* Have we already had a message ID? */
1002 if (rev.message_id) {
1004 * If we've got the ID to be a reply
1005 * to, discard the current ID;
1006 * otherwise, make everything a reply
1007 * to that.
1009 if (rev.ref_message_id)
1010 free(rev.message_id);
1011 else
1012 rev.ref_message_id = rev.message_id;
1014 gen_message_id(&rev, sha1_to_hex(commit->object.sha1));
1016 if (!use_stdout && reopen_stdout(numbered_files ? NULL :
1017 get_oneline_for_filename(commit, keep_subject),
1018 rev.nr, rev.total))
1019 die("Failed to create output files");
1020 shown = log_tree_commit(&rev, commit);
1021 free(commit->buffer);
1022 commit->buffer = NULL;
1024 /* We put one extra blank line between formatted
1025 * patches and this flag is used by log-tree code
1026 * to see if it needs to emit a LF before showing
1027 * the log; when using one file per patch, we do
1028 * not want the extra blank line.
1030 if (!use_stdout)
1031 rev.shown_one = 0;
1032 if (shown) {
1033 if (rev.mime_boundary)
1034 printf("\n--%s%s--\n\n\n",
1035 mime_boundary_leader,
1036 rev.mime_boundary);
1037 else
1038 printf("-- \n%s\n\n", git_version_string);
1040 if (!use_stdout)
1041 fclose(stdout);
1043 free(list);
1044 if (ignore_if_in_upstream)
1045 free_patch_ids(&ids);
1046 return 0;
1049 static int add_pending_commit(const char *arg, struct rev_info *revs, int flags)
1051 unsigned char sha1[20];
1052 if (get_sha1(arg, sha1) == 0) {
1053 struct commit *commit = lookup_commit_reference(sha1);
1054 if (commit) {
1055 commit->object.flags |= flags;
1056 add_pending_object(revs, &commit->object, arg);
1057 return 0;
1060 return -1;
1063 static const char cherry_usage[] =
1064 "git cherry [-v] <upstream> [<head>] [<limit>]";
1065 int cmd_cherry(int argc, const char **argv, const char *prefix)
1067 struct rev_info revs;
1068 struct patch_ids ids;
1069 struct commit *commit;
1070 struct commit_list *list = NULL;
1071 const char *upstream;
1072 const char *head = "HEAD";
1073 const char *limit = NULL;
1074 int verbose = 0;
1076 if (argc > 1 && !strcmp(argv[1], "-v")) {
1077 verbose = 1;
1078 argc--;
1079 argv++;
1082 switch (argc) {
1083 case 4:
1084 limit = argv[3];
1085 /* FALLTHROUGH */
1086 case 3:
1087 head = argv[2];
1088 /* FALLTHROUGH */
1089 case 2:
1090 upstream = argv[1];
1091 break;
1092 default:
1093 usage(cherry_usage);
1096 init_revisions(&revs, prefix);
1097 revs.diff = 1;
1098 revs.combine_merges = 0;
1099 revs.ignore_merges = 1;
1100 DIFF_OPT_SET(&revs.diffopt, RECURSIVE);
1102 if (add_pending_commit(head, &revs, 0))
1103 die("Unknown commit %s", head);
1104 if (add_pending_commit(upstream, &revs, UNINTERESTING))
1105 die("Unknown commit %s", upstream);
1107 /* Don't say anything if head and upstream are the same. */
1108 if (revs.pending.nr == 2) {
1109 struct object_array_entry *o = revs.pending.objects;
1110 if (hashcmp(o[0].item->sha1, o[1].item->sha1) == 0)
1111 return 0;
1114 get_patch_ids(&revs, &ids, prefix);
1116 if (limit && add_pending_commit(limit, &revs, UNINTERESTING))
1117 die("Unknown commit %s", limit);
1119 /* reverse the list of commits */
1120 if (prepare_revision_walk(&revs))
1121 die("revision walk setup failed");
1122 while ((commit = get_revision(&revs)) != NULL) {
1123 /* ignore merges */
1124 if (commit->parents && commit->parents->next)
1125 continue;
1127 commit_list_insert(commit, &list);
1130 while (list) {
1131 char sign = '+';
1133 commit = list->item;
1134 if (has_commit_patch_id(commit, &ids))
1135 sign = '-';
1137 if (verbose) {
1138 struct strbuf buf = STRBUF_INIT;
1139 pretty_print_commit(CMIT_FMT_ONELINE, commit,
1140 &buf, 0, NULL, NULL, 0, 0);
1141 printf("%c %s %s\n", sign,
1142 sha1_to_hex(commit->object.sha1), buf.buf);
1143 strbuf_release(&buf);
1145 else {
1146 printf("%c %s\n", sign,
1147 sha1_to_hex(commit->object.sha1));
1150 list = list->next;
1153 free_patch_ids(&ids);
1154 return 0;