builtin-checkout.c: check error return from read_cache()
[git/mingw.git] / builtin-log.c
blobb164717379627b7bfc303bd3fdf360ecd2db152d
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;
41 if (default_date_mode)
42 rev->date_mode = parse_date_format(default_date_mode);
44 argc = setup_revisions(argc, argv, rev, "HEAD");
46 if (rev->diffopt.pickaxe || rev->diffopt.filter)
47 rev->always_show_header = 0;
48 if (DIFF_OPT_TST(&rev->diffopt, FOLLOW_RENAMES)) {
49 rev->always_show_header = 0;
50 if (rev->diffopt.nr_paths != 1)
51 usage("git logs can only follow renames on one pathname at a time");
53 for (i = 1; i < argc; i++) {
54 const char *arg = argv[i];
55 if (!strcmp(arg, "--decorate")) {
56 load_ref_decorations();
57 rev->show_decorations = 1;
58 } else if (!strcmp(arg, "--source")) {
59 rev->show_source = 1;
60 } else
61 die("unrecognized argument: %s", arg);
63 DIFF_OPT_SET(&rev->diffopt, ALLOW_TEXTCONV);
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 char *email_end, *p;
253 unsigned long date;
254 int tz;
256 email_end = memchr(buf, '>', len);
257 if (!email_end)
258 return;
259 p = ++email_end;
260 while (isspace(*p))
261 p++;
262 date = strtoul(p, &p, 10);
263 while (isspace(*p))
264 p++;
265 tz = (int)strtol(p, NULL, 10);
266 printf("Tagger: %.*s\nDate: %s\n", (int)(email_end - buf), buf,
267 show_date(date, tz, rev->date_mode));
270 static int show_object(const unsigned char *sha1, int show_tag_object,
271 struct rev_info *rev)
273 unsigned long size;
274 enum object_type type;
275 char *buf = read_sha1_file(sha1, &type, &size);
276 int offset = 0;
278 if (!buf)
279 return error("Could not read object %s", sha1_to_hex(sha1));
281 if (show_tag_object)
282 while (offset < size && buf[offset] != '\n') {
283 int new_offset = offset + 1;
284 while (new_offset < size && buf[new_offset++] != '\n')
285 ; /* do nothing */
286 if (!prefixcmp(buf + offset, "tagger "))
287 show_tagger(buf + offset + 7,
288 new_offset - offset - 7, rev);
289 offset = new_offset;
292 if (offset < size)
293 fwrite(buf + offset, size - offset, 1, stdout);
294 free(buf);
295 return 0;
298 static int show_tree_object(const unsigned char *sha1,
299 const char *base, int baselen,
300 const char *pathname, unsigned mode, int stage, void *context)
302 printf("%s%s\n", pathname, S_ISDIR(mode) ? "/" : "");
303 return 0;
306 int cmd_show(int argc, const char **argv, const char *prefix)
308 struct rev_info rev;
309 struct object_array_entry *objects;
310 int i, count, ret = 0;
312 git_config(git_log_config, NULL);
314 if (diff_use_color_default == -1)
315 diff_use_color_default = git_use_color_default;
317 init_revisions(&rev, prefix);
318 rev.diff = 1;
319 rev.combine_merges = 1;
320 rev.dense_combined_merges = 1;
321 rev.always_show_header = 1;
322 rev.ignore_merges = 0;
323 rev.no_walk = 1;
324 cmd_log_init(argc, argv, prefix, &rev);
326 count = rev.pending.nr;
327 objects = rev.pending.objects;
328 for (i = 0; i < count && !ret; i++) {
329 struct object *o = objects[i].item;
330 const char *name = objects[i].name;
331 switch (o->type) {
332 case OBJ_BLOB:
333 ret = show_object(o->sha1, 0, NULL);
334 break;
335 case OBJ_TAG: {
336 struct tag *t = (struct tag *)o;
338 printf("%stag %s%s\n",
339 diff_get_color_opt(&rev.diffopt, DIFF_COMMIT),
340 t->tag,
341 diff_get_color_opt(&rev.diffopt, DIFF_RESET));
342 ret = show_object(o->sha1, 1, &rev);
343 objects[i].item = parse_object(t->tagged->sha1);
344 i--;
345 break;
347 case OBJ_TREE:
348 printf("%stree %s%s\n\n",
349 diff_get_color_opt(&rev.diffopt, DIFF_COMMIT),
350 name,
351 diff_get_color_opt(&rev.diffopt, DIFF_RESET));
352 read_tree_recursive((struct tree *)o, "", 0, 0, NULL,
353 show_tree_object, NULL);
354 break;
355 case OBJ_COMMIT:
356 rev.pending.nr = rev.pending.alloc = 0;
357 rev.pending.objects = NULL;
358 add_object_array(o, name, &rev.pending);
359 ret = cmd_log_walk(&rev);
360 break;
361 default:
362 ret = error("Unknown type: %d", o->type);
365 free(objects);
366 return ret;
370 * This is equivalent to "git log -g --abbrev-commit --pretty=oneline"
372 int cmd_log_reflog(int argc, const char **argv, const char *prefix)
374 struct rev_info rev;
376 git_config(git_log_config, NULL);
378 if (diff_use_color_default == -1)
379 diff_use_color_default = git_use_color_default;
381 init_revisions(&rev, prefix);
382 init_reflog_walk(&rev.reflog_info);
383 rev.abbrev_commit = 1;
384 rev.verbose_header = 1;
385 cmd_log_init(argc, argv, prefix, &rev);
388 * This means that we override whatever commit format the user gave
389 * on the cmd line. Sad, but cmd_log_init() currently doesn't
390 * allow us to set a different default.
392 rev.commit_format = CMIT_FMT_ONELINE;
393 rev.use_terminator = 1;
394 rev.always_show_header = 1;
397 * We get called through "git reflog", so unlike the other log
398 * routines, we need to set up our pager manually..
400 setup_pager();
402 return cmd_log_walk(&rev);
405 int cmd_log(int argc, const char **argv, const char *prefix)
407 struct rev_info rev;
409 git_config(git_log_config, NULL);
411 if (diff_use_color_default == -1)
412 diff_use_color_default = git_use_color_default;
414 init_revisions(&rev, prefix);
415 rev.always_show_header = 1;
416 cmd_log_init(argc, argv, prefix, &rev);
417 return cmd_log_walk(&rev);
420 /* format-patch */
421 #define FORMAT_PATCH_NAME_MAX 64
423 static int istitlechar(char c)
425 return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
426 (c >= '0' && c <= '9') || c == '.' || c == '_';
429 static const char *fmt_patch_suffix = ".patch";
430 static int numbered = 0;
431 static int auto_number = 1;
433 static char **extra_hdr;
434 static int extra_hdr_nr;
435 static int extra_hdr_alloc;
437 static char **extra_to;
438 static int extra_to_nr;
439 static int extra_to_alloc;
441 static char **extra_cc;
442 static int extra_cc_nr;
443 static int extra_cc_alloc;
445 static void add_header(const char *value)
447 int len = strlen(value);
448 while (len && value[len - 1] == '\n')
449 len--;
450 if (!strncasecmp(value, "to: ", 4)) {
451 ALLOC_GROW(extra_to, extra_to_nr + 1, extra_to_alloc);
452 extra_to[extra_to_nr++] = xstrndup(value + 4, len - 4);
453 return;
455 if (!strncasecmp(value, "cc: ", 4)) {
456 ALLOC_GROW(extra_cc, extra_cc_nr + 1, extra_cc_alloc);
457 extra_cc[extra_cc_nr++] = xstrndup(value + 4, len - 4);
458 return;
460 ALLOC_GROW(extra_hdr, extra_hdr_nr + 1, extra_hdr_alloc);
461 extra_hdr[extra_hdr_nr++] = xstrndup(value, len);
464 static int git_format_config(const char *var, const char *value, void *cb)
466 if (!strcmp(var, "format.headers")) {
467 if (!value)
468 die("format.headers without value");
469 add_header(value);
470 return 0;
472 if (!strcmp(var, "format.suffix"))
473 return git_config_string(&fmt_patch_suffix, var, value);
474 if (!strcmp(var, "format.cc")) {
475 if (!value)
476 return config_error_nonbool(var);
477 ALLOC_GROW(extra_cc, extra_cc_nr + 1, extra_cc_alloc);
478 extra_cc[extra_cc_nr++] = xstrdup(value);
479 return 0;
481 if (!strcmp(var, "diff.color") || !strcmp(var, "color.diff")) {
482 return 0;
484 if (!strcmp(var, "format.numbered")) {
485 if (value && !strcasecmp(value, "auto")) {
486 auto_number = 1;
487 return 0;
489 numbered = git_config_bool(var, value);
490 auto_number = auto_number && numbered;
491 return 0;
494 return git_log_config(var, value, cb);
498 static const char *get_oneline_for_filename(struct commit *commit,
499 int keep_subject)
501 static char filename[PATH_MAX];
502 char *sol;
503 int len = 0;
504 int suffix_len = strlen(fmt_patch_suffix) + 1;
506 sol = strstr(commit->buffer, "\n\n");
507 if (!sol)
508 filename[0] = '\0';
509 else {
510 int j, space = 0;
512 sol += 2;
513 /* strip [PATCH] or [PATCH blabla] */
514 if (!keep_subject && !prefixcmp(sol, "[PATCH")) {
515 char *eos = strchr(sol + 6, ']');
516 if (eos) {
517 while (isspace(*eos))
518 eos++;
519 sol = eos;
523 for (j = 0;
524 j < FORMAT_PATCH_NAME_MAX - suffix_len - 5 &&
525 len < sizeof(filename) - suffix_len &&
526 sol[j] && sol[j] != '\n';
527 j++) {
528 if (istitlechar(sol[j])) {
529 if (space) {
530 filename[len++] = '-';
531 space = 0;
533 filename[len++] = sol[j];
534 if (sol[j] == '.')
535 while (sol[j + 1] == '.')
536 j++;
537 } else
538 space = 1;
540 while (filename[len - 1] == '.'
541 || filename[len - 1] == '-')
542 len--;
543 filename[len] = '\0';
545 return filename;
548 static FILE *realstdout = NULL;
549 static const char *output_directory = NULL;
551 static int reopen_stdout(const char *oneline, int nr, int total)
553 char filename[PATH_MAX];
554 int len = 0;
555 int suffix_len = strlen(fmt_patch_suffix) + 1;
557 if (output_directory) {
558 len = snprintf(filename, sizeof(filename), "%s",
559 output_directory);
560 if (len >=
561 sizeof(filename) - FORMAT_PATCH_NAME_MAX - suffix_len)
562 return error("name of output directory is too long");
563 if (filename[len - 1] != '/')
564 filename[len++] = '/';
567 if (!oneline)
568 len += sprintf(filename + len, "%d", nr);
569 else {
570 len += sprintf(filename + len, "%04d-", nr);
571 len += snprintf(filename + len, sizeof(filename) - len - 1
572 - suffix_len, "%s", oneline);
573 strcpy(filename + len, fmt_patch_suffix);
576 fprintf(realstdout, "%s\n", filename);
577 if (freopen(filename, "w", stdout) == NULL)
578 return error("Cannot open patch file %s",filename);
580 return 0;
583 static void get_patch_ids(struct rev_info *rev, struct patch_ids *ids, const char *prefix)
585 struct rev_info check_rev;
586 struct commit *commit;
587 struct object *o1, *o2;
588 unsigned flags1, flags2;
590 if (rev->pending.nr != 2)
591 die("Need exactly one range.");
593 o1 = rev->pending.objects[0].item;
594 flags1 = o1->flags;
595 o2 = rev->pending.objects[1].item;
596 flags2 = o2->flags;
598 if ((flags1 & UNINTERESTING) == (flags2 & UNINTERESTING))
599 die("Not a range.");
601 init_patch_ids(ids);
603 /* given a range a..b get all patch ids for b..a */
604 init_revisions(&check_rev, prefix);
605 o1->flags ^= UNINTERESTING;
606 o2->flags ^= UNINTERESTING;
607 add_pending_object(&check_rev, o1, "o1");
608 add_pending_object(&check_rev, o2, "o2");
609 if (prepare_revision_walk(&check_rev))
610 die("revision walk setup failed");
612 while ((commit = get_revision(&check_rev)) != NULL) {
613 /* ignore merges */
614 if (commit->parents && commit->parents->next)
615 continue;
617 add_commit_patch_id(commit, ids);
620 /* reset for next revision walk */
621 clear_commit_marks((struct commit *)o1,
622 SEEN | UNINTERESTING | SHOWN | ADDED);
623 clear_commit_marks((struct commit *)o2,
624 SEEN | UNINTERESTING | SHOWN | ADDED);
625 o1->flags = flags1;
626 o2->flags = flags2;
629 static void gen_message_id(struct rev_info *info, char *base)
631 const char *committer = git_committer_info(IDENT_WARN_ON_NO_NAME);
632 const char *email_start = strrchr(committer, '<');
633 const char *email_end = strrchr(committer, '>');
634 struct strbuf buf = STRBUF_INIT;
635 if (!email_start || !email_end || email_start > email_end - 1)
636 die("Could not extract email from committer identity.");
637 strbuf_addf(&buf, "%s.%lu.git.%.*s", base,
638 (unsigned long) time(NULL),
639 (int)(email_end - email_start - 1), email_start + 1);
640 info->message_id = strbuf_detach(&buf, NULL);
643 static void make_cover_letter(struct rev_info *rev, int use_stdout,
644 int numbered, int numbered_files,
645 struct commit *origin,
646 int nr, struct commit **list, struct commit *head)
648 const char *committer;
649 char *head_sha1;
650 const char *subject_start = NULL;
651 const char *body = "*** SUBJECT HERE ***\n\n*** BLURB HERE ***\n";
652 const char *msg;
653 const char *extra_headers = rev->extra_headers;
654 struct shortlog log;
655 struct strbuf sb = STRBUF_INIT;
656 int i;
657 const char *encoding = "utf-8";
658 struct diff_options opts;
659 int need_8bit_cte = 0;
661 if (rev->commit_format != CMIT_FMT_EMAIL)
662 die("Cover letter needs email format");
664 if (!use_stdout && reopen_stdout(numbered_files ?
665 NULL : "cover-letter", 0, rev->total))
666 return;
668 head_sha1 = sha1_to_hex(head->object.sha1);
670 log_write_email_headers(rev, head_sha1, &subject_start, &extra_headers,
671 &need_8bit_cte);
673 committer = git_committer_info(0);
675 msg = body;
676 pp_user_info(NULL, CMIT_FMT_EMAIL, &sb, committer, DATE_RFC2822,
677 encoding);
678 pp_title_line(CMIT_FMT_EMAIL, &msg, &sb, subject_start, extra_headers,
679 encoding, need_8bit_cte);
680 pp_remainder(CMIT_FMT_EMAIL, &msg, &sb, 0);
681 printf("%s\n", sb.buf);
683 strbuf_release(&sb);
685 shortlog_init(&log);
686 log.wrap_lines = 1;
687 log.wrap = 72;
688 log.in1 = 2;
689 log.in2 = 4;
690 for (i = 0; i < nr; i++)
691 shortlog_add_commit(&log, list[i]);
693 shortlog_output(&log);
696 * We can only do diffstat with a unique reference point
698 if (!origin)
699 return;
701 memcpy(&opts, &rev->diffopt, sizeof(opts));
702 opts.output_format = DIFF_FORMAT_SUMMARY | DIFF_FORMAT_DIFFSTAT;
704 diff_setup_done(&opts);
706 diff_tree_sha1(origin->tree->object.sha1,
707 head->tree->object.sha1,
708 "", &opts);
709 diffcore_std(&opts);
710 diff_flush(&opts);
712 printf("\n");
715 static const char *clean_message_id(const char *msg_id)
717 char ch;
718 const char *a, *z, *m;
720 m = msg_id;
721 while ((ch = *m) && (isspace(ch) || (ch == '<')))
722 m++;
723 a = m;
724 z = NULL;
725 while ((ch = *m)) {
726 if (!isspace(ch) && (ch != '>'))
727 z = m;
728 m++;
730 if (!z)
731 die("insane in-reply-to: %s", msg_id);
732 if (++z == m)
733 return a;
734 return xmemdupz(a, z - a);
737 int cmd_format_patch(int argc, const char **argv, const char *prefix)
739 struct commit *commit;
740 struct commit **list = NULL;
741 struct rev_info rev;
742 int nr = 0, total, i, j;
743 int use_stdout = 0;
744 int start_number = -1;
745 int keep_subject = 0;
746 int numbered_files = 0; /* _just_ numbers */
747 int subject_prefix = 0;
748 int ignore_if_in_upstream = 0;
749 int thread = 0;
750 int cover_letter = 0;
751 int boundary_count = 0;
752 int no_binary_diff = 0;
753 struct commit *origin = NULL, *head = NULL;
754 const char *in_reply_to = NULL;
755 struct patch_ids ids;
756 char *add_signoff = NULL;
757 struct strbuf buf = STRBUF_INIT;
759 git_config(git_format_config, NULL);
760 init_revisions(&rev, prefix);
761 rev.commit_format = CMIT_FMT_EMAIL;
762 rev.verbose_header = 1;
763 rev.diff = 1;
764 rev.combine_merges = 0;
765 rev.ignore_merges = 1;
766 DIFF_OPT_SET(&rev.diffopt, RECURSIVE);
768 rev.subject_prefix = fmt_patch_subject_prefix;
771 * Parse the arguments before setup_revisions(), or something
772 * like "git format-patch -o a123 HEAD^.." may fail; a123 is
773 * possibly a valid SHA1.
775 for (i = 1, j = 1; i < argc; i++) {
776 if (!strcmp(argv[i], "--stdout"))
777 use_stdout = 1;
778 else if (!strcmp(argv[i], "-n") ||
779 !strcmp(argv[i], "--numbered"))
780 numbered = 1;
781 else if (!strcmp(argv[i], "-N") ||
782 !strcmp(argv[i], "--no-numbered")) {
783 numbered = 0;
784 auto_number = 0;
786 else if (!prefixcmp(argv[i], "--start-number="))
787 start_number = strtol(argv[i] + 15, NULL, 10);
788 else if (!strcmp(argv[i], "--numbered-files"))
789 numbered_files = 1;
790 else if (!strcmp(argv[i], "--start-number")) {
791 i++;
792 if (i == argc)
793 die("Need a number for --start-number");
794 start_number = strtol(argv[i], NULL, 10);
796 else if (!prefixcmp(argv[i], "--cc=")) {
797 ALLOC_GROW(extra_cc, extra_cc_nr + 1, extra_cc_alloc);
798 extra_cc[extra_cc_nr++] = xstrdup(argv[i] + 5);
800 else if (!strcmp(argv[i], "-k") ||
801 !strcmp(argv[i], "--keep-subject")) {
802 keep_subject = 1;
803 rev.total = -1;
805 else if (!strcmp(argv[i], "--output-directory") ||
806 !strcmp(argv[i], "-o")) {
807 i++;
808 if (argc <= i)
809 die("Which directory?");
810 if (output_directory)
811 die("Two output directories?");
812 output_directory = argv[i];
814 else if (!strcmp(argv[i], "--signoff") ||
815 !strcmp(argv[i], "-s")) {
816 const char *committer;
817 const char *endpos;
818 committer = git_committer_info(IDENT_ERROR_ON_NO_NAME);
819 endpos = strchr(committer, '>');
820 if (!endpos)
821 die("bogus committer info %s\n", committer);
822 add_signoff = xmemdupz(committer, endpos - committer + 1);
824 else if (!strcmp(argv[i], "--attach")) {
825 rev.mime_boundary = git_version_string;
826 rev.no_inline = 1;
828 else if (!prefixcmp(argv[i], "--attach=")) {
829 rev.mime_boundary = argv[i] + 9;
830 rev.no_inline = 1;
832 else if (!strcmp(argv[i], "--inline")) {
833 rev.mime_boundary = git_version_string;
834 rev.no_inline = 0;
836 else if (!prefixcmp(argv[i], "--inline=")) {
837 rev.mime_boundary = argv[i] + 9;
838 rev.no_inline = 0;
840 else if (!strcmp(argv[i], "--ignore-if-in-upstream"))
841 ignore_if_in_upstream = 1;
842 else if (!strcmp(argv[i], "--thread"))
843 thread = 1;
844 else if (!prefixcmp(argv[i], "--in-reply-to="))
845 in_reply_to = argv[i] + 14;
846 else if (!strcmp(argv[i], "--in-reply-to")) {
847 i++;
848 if (i == argc)
849 die("Need a Message-Id for --in-reply-to");
850 in_reply_to = argv[i];
851 } else if (!prefixcmp(argv[i], "--subject-prefix=")) {
852 subject_prefix = 1;
853 rev.subject_prefix = argv[i] + 17;
854 } else if (!prefixcmp(argv[i], "--suffix="))
855 fmt_patch_suffix = argv[i] + 9;
856 else if (!strcmp(argv[i], "--cover-letter"))
857 cover_letter = 1;
858 else if (!strcmp(argv[i], "--no-binary"))
859 no_binary_diff = 1;
860 else
861 argv[j++] = argv[i];
863 argc = j;
865 for (i = 0; i < extra_hdr_nr; i++) {
866 strbuf_addstr(&buf, extra_hdr[i]);
867 strbuf_addch(&buf, '\n');
870 if (extra_to_nr)
871 strbuf_addstr(&buf, "To: ");
872 for (i = 0; i < extra_to_nr; i++) {
873 if (i)
874 strbuf_addstr(&buf, " ");
875 strbuf_addstr(&buf, extra_to[i]);
876 if (i + 1 < extra_to_nr)
877 strbuf_addch(&buf, ',');
878 strbuf_addch(&buf, '\n');
881 if (extra_cc_nr)
882 strbuf_addstr(&buf, "Cc: ");
883 for (i = 0; i < extra_cc_nr; i++) {
884 if (i)
885 strbuf_addstr(&buf, " ");
886 strbuf_addstr(&buf, extra_cc[i]);
887 if (i + 1 < extra_cc_nr)
888 strbuf_addch(&buf, ',');
889 strbuf_addch(&buf, '\n');
892 rev.extra_headers = strbuf_detach(&buf, 0);
894 if (start_number < 0)
895 start_number = 1;
896 if (numbered && keep_subject)
897 die ("-n and -k are mutually exclusive.");
898 if (keep_subject && subject_prefix)
899 die ("--subject-prefix and -k are mutually exclusive.");
900 if (numbered_files && use_stdout)
901 die ("--numbered-files and --stdout are mutually exclusive.");
903 argc = setup_revisions(argc, argv, &rev, "HEAD");
904 if (argc > 1)
905 die ("unrecognized argument: %s", argv[1]);
907 if (!rev.diffopt.output_format
908 || rev.diffopt.output_format == DIFF_FORMAT_PATCH)
909 rev.diffopt.output_format = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_SUMMARY | DIFF_FORMAT_PATCH;
911 if (!DIFF_OPT_TST(&rev.diffopt, TEXT) && !no_binary_diff)
912 DIFF_OPT_SET(&rev.diffopt, BINARY);
914 if (!output_directory && !use_stdout)
915 output_directory = prefix;
917 if (output_directory) {
918 if (use_stdout)
919 die("standard output, or directory, which one?");
920 if (mkdir(output_directory, 0777) < 0 && errno != EEXIST)
921 die("Could not create directory %s",
922 output_directory);
925 if (rev.pending.nr == 1) {
926 if (rev.max_count < 0 && !rev.show_root_diff) {
928 * This is traditional behaviour of "git format-patch
929 * origin" that prepares what the origin side still
930 * does not have.
932 rev.pending.objects[0].item->flags |= UNINTERESTING;
933 add_head_to_pending(&rev);
936 * Otherwise, it is "format-patch -22 HEAD", and/or
937 * "format-patch --root HEAD". The user wants
938 * get_revision() to do the usual traversal.
941 if (cover_letter) {
942 /* remember the range */
943 int i;
944 for (i = 0; i < rev.pending.nr; i++) {
945 struct object *o = rev.pending.objects[i].item;
946 if (!(o->flags & UNINTERESTING))
947 head = (struct commit *)o;
949 /* We can't generate a cover letter without any patches */
950 if (!head)
951 return 0;
954 if (ignore_if_in_upstream)
955 get_patch_ids(&rev, &ids, prefix);
957 if (!use_stdout)
958 realstdout = xfdopen(xdup(1), "w");
960 if (prepare_revision_walk(&rev))
961 die("revision walk setup failed");
962 rev.boundary = 1;
963 while ((commit = get_revision(&rev)) != NULL) {
964 if (commit->object.flags & BOUNDARY) {
965 boundary_count++;
966 origin = (boundary_count == 1) ? commit : NULL;
967 continue;
970 /* ignore merges */
971 if (commit->parents && commit->parents->next)
972 continue;
974 if (ignore_if_in_upstream &&
975 has_commit_patch_id(commit, &ids))
976 continue;
978 nr++;
979 list = xrealloc(list, nr * sizeof(list[0]));
980 list[nr - 1] = commit;
982 total = nr;
983 if (!keep_subject && auto_number && total > 1)
984 numbered = 1;
985 if (numbered)
986 rev.total = total + start_number - 1;
987 if (in_reply_to)
988 rev.ref_message_id = clean_message_id(in_reply_to);
989 if (cover_letter) {
990 if (thread)
991 gen_message_id(&rev, "cover");
992 make_cover_letter(&rev, use_stdout, numbered, numbered_files,
993 origin, nr, list, head);
994 total++;
995 start_number--;
997 rev.add_signoff = add_signoff;
998 while (0 <= --nr) {
999 int shown;
1000 commit = list[nr];
1001 rev.nr = total - nr + (start_number - 1);
1002 /* Make the second and subsequent mails replies to the first */
1003 if (thread) {
1004 /* Have we already had a message ID? */
1005 if (rev.message_id) {
1007 * If we've got the ID to be a reply
1008 * to, discard the current ID;
1009 * otherwise, make everything a reply
1010 * to that.
1012 if (rev.ref_message_id)
1013 free(rev.message_id);
1014 else
1015 rev.ref_message_id = rev.message_id;
1017 gen_message_id(&rev, sha1_to_hex(commit->object.sha1));
1019 if (!use_stdout && reopen_stdout(numbered_files ? NULL :
1020 get_oneline_for_filename(commit, keep_subject),
1021 rev.nr, rev.total))
1022 die("Failed to create output files");
1023 shown = log_tree_commit(&rev, commit);
1024 free(commit->buffer);
1025 commit->buffer = NULL;
1027 /* We put one extra blank line between formatted
1028 * patches and this flag is used by log-tree code
1029 * to see if it needs to emit a LF before showing
1030 * the log; when using one file per patch, we do
1031 * not want the extra blank line.
1033 if (!use_stdout)
1034 rev.shown_one = 0;
1035 if (shown) {
1036 if (rev.mime_boundary)
1037 printf("\n--%s%s--\n\n\n",
1038 mime_boundary_leader,
1039 rev.mime_boundary);
1040 else
1041 printf("-- \n%s\n\n", git_version_string);
1043 if (!use_stdout)
1044 fclose(stdout);
1046 free(list);
1047 if (ignore_if_in_upstream)
1048 free_patch_ids(&ids);
1049 return 0;
1052 static int add_pending_commit(const char *arg, struct rev_info *revs, int flags)
1054 unsigned char sha1[20];
1055 if (get_sha1(arg, sha1) == 0) {
1056 struct commit *commit = lookup_commit_reference(sha1);
1057 if (commit) {
1058 commit->object.flags |= flags;
1059 add_pending_object(revs, &commit->object, arg);
1060 return 0;
1063 return -1;
1066 static const char cherry_usage[] =
1067 "git cherry [-v] <upstream> [<head>] [<limit>]";
1068 int cmd_cherry(int argc, const char **argv, const char *prefix)
1070 struct rev_info revs;
1071 struct patch_ids ids;
1072 struct commit *commit;
1073 struct commit_list *list = NULL;
1074 const char *upstream;
1075 const char *head = "HEAD";
1076 const char *limit = NULL;
1077 int verbose = 0;
1079 if (argc > 1 && !strcmp(argv[1], "-v")) {
1080 verbose = 1;
1081 argc--;
1082 argv++;
1085 switch (argc) {
1086 case 4:
1087 limit = argv[3];
1088 /* FALLTHROUGH */
1089 case 3:
1090 head = argv[2];
1091 /* FALLTHROUGH */
1092 case 2:
1093 upstream = argv[1];
1094 break;
1095 default:
1096 usage(cherry_usage);
1099 init_revisions(&revs, prefix);
1100 revs.diff = 1;
1101 revs.combine_merges = 0;
1102 revs.ignore_merges = 1;
1103 DIFF_OPT_SET(&revs.diffopt, RECURSIVE);
1105 if (add_pending_commit(head, &revs, 0))
1106 die("Unknown commit %s", head);
1107 if (add_pending_commit(upstream, &revs, UNINTERESTING))
1108 die("Unknown commit %s", upstream);
1110 /* Don't say anything if head and upstream are the same. */
1111 if (revs.pending.nr == 2) {
1112 struct object_array_entry *o = revs.pending.objects;
1113 if (hashcmp(o[0].item->sha1, o[1].item->sha1) == 0)
1114 return 0;
1117 get_patch_ids(&revs, &ids, prefix);
1119 if (limit && add_pending_commit(limit, &revs, UNINTERESTING))
1120 die("Unknown commit %s", limit);
1122 /* reverse the list of commits */
1123 if (prepare_revision_walk(&revs))
1124 die("revision walk setup failed");
1125 while ((commit = get_revision(&revs)) != NULL) {
1126 /* ignore merges */
1127 if (commit->parents && commit->parents->next)
1128 continue;
1130 commit_list_insert(commit, &list);
1133 while (list) {
1134 char sign = '+';
1136 commit = list->item;
1137 if (has_commit_patch_id(commit, &ids))
1138 sign = '-';
1140 if (verbose) {
1141 struct strbuf buf = STRBUF_INIT;
1142 pretty_print_commit(CMIT_FMT_ONELINE, commit,
1143 &buf, 0, NULL, NULL, 0, 0);
1144 printf("%c %s %s\n", sign,
1145 sha1_to_hex(commit->object.sha1), buf.buf);
1146 strbuf_release(&buf);
1148 else {
1149 printf("%c %s\n", sign,
1150 sha1_to_hex(commit->object.sha1));
1153 list = list->next;
1156 free_patch_ids(&ids);
1157 return 0;