git: use run_command() to execute dashed externals
[git/gitweb.git] / builtin-log.c
blob4420b4fd4f871d1284b90e80fa58fbe529b61592
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 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 if (ret)
344 break;
345 o = parse_object(t->tagged->sha1);
346 if (!o)
347 ret = error("Could not read object %s",
348 sha1_to_hex(t->tagged->sha1));
349 objects[i].item = o;
350 i--;
351 break;
353 case OBJ_TREE:
354 printf("%stree %s%s\n\n",
355 diff_get_color_opt(&rev.diffopt, DIFF_COMMIT),
356 name,
357 diff_get_color_opt(&rev.diffopt, DIFF_RESET));
358 read_tree_recursive((struct tree *)o, "", 0, 0, NULL,
359 show_tree_object, NULL);
360 break;
361 case OBJ_COMMIT:
362 rev.pending.nr = rev.pending.alloc = 0;
363 rev.pending.objects = NULL;
364 add_object_array(o, name, &rev.pending);
365 ret = cmd_log_walk(&rev);
366 break;
367 default:
368 ret = error("Unknown type: %d", o->type);
371 free(objects);
372 return ret;
376 * This is equivalent to "git log -g --abbrev-commit --pretty=oneline"
378 int cmd_log_reflog(int argc, const char **argv, const char *prefix)
380 struct rev_info rev;
382 git_config(git_log_config, NULL);
384 if (diff_use_color_default == -1)
385 diff_use_color_default = git_use_color_default;
387 init_revisions(&rev, prefix);
388 init_reflog_walk(&rev.reflog_info);
389 rev.abbrev_commit = 1;
390 rev.verbose_header = 1;
391 cmd_log_init(argc, argv, prefix, &rev);
394 * This means that we override whatever commit format the user gave
395 * on the cmd line. Sad, but cmd_log_init() currently doesn't
396 * allow us to set a different default.
398 rev.commit_format = CMIT_FMT_ONELINE;
399 rev.use_terminator = 1;
400 rev.always_show_header = 1;
403 * We get called through "git reflog", so unlike the other log
404 * routines, we need to set up our pager manually..
406 setup_pager();
408 return cmd_log_walk(&rev);
411 int cmd_log(int argc, const char **argv, const char *prefix)
413 struct rev_info rev;
415 git_config(git_log_config, NULL);
417 if (diff_use_color_default == -1)
418 diff_use_color_default = git_use_color_default;
420 init_revisions(&rev, prefix);
421 rev.always_show_header = 1;
422 cmd_log_init(argc, argv, prefix, &rev);
423 return cmd_log_walk(&rev);
426 /* format-patch */
427 #define FORMAT_PATCH_NAME_MAX 64
429 static int istitlechar(char c)
431 return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
432 (c >= '0' && c <= '9') || c == '.' || c == '_';
435 static const char *fmt_patch_suffix = ".patch";
436 static int numbered = 0;
437 static int auto_number = 1;
439 static char **extra_hdr;
440 static int extra_hdr_nr;
441 static int extra_hdr_alloc;
443 static char **extra_to;
444 static int extra_to_nr;
445 static int extra_to_alloc;
447 static char **extra_cc;
448 static int extra_cc_nr;
449 static int extra_cc_alloc;
451 static void add_header(const char *value)
453 int len = strlen(value);
454 while (len && value[len - 1] == '\n')
455 len--;
456 if (!strncasecmp(value, "to: ", 4)) {
457 ALLOC_GROW(extra_to, extra_to_nr + 1, extra_to_alloc);
458 extra_to[extra_to_nr++] = xstrndup(value + 4, len - 4);
459 return;
461 if (!strncasecmp(value, "cc: ", 4)) {
462 ALLOC_GROW(extra_cc, extra_cc_nr + 1, extra_cc_alloc);
463 extra_cc[extra_cc_nr++] = xstrndup(value + 4, len - 4);
464 return;
466 ALLOC_GROW(extra_hdr, extra_hdr_nr + 1, extra_hdr_alloc);
467 extra_hdr[extra_hdr_nr++] = xstrndup(value, len);
470 static int git_format_config(const char *var, const char *value, void *cb)
472 if (!strcmp(var, "format.headers")) {
473 if (!value)
474 die("format.headers without value");
475 add_header(value);
476 return 0;
478 if (!strcmp(var, "format.suffix"))
479 return git_config_string(&fmt_patch_suffix, var, value);
480 if (!strcmp(var, "format.cc")) {
481 if (!value)
482 return config_error_nonbool(var);
483 ALLOC_GROW(extra_cc, extra_cc_nr + 1, extra_cc_alloc);
484 extra_cc[extra_cc_nr++] = xstrdup(value);
485 return 0;
487 if (!strcmp(var, "diff.color") || !strcmp(var, "color.diff")) {
488 return 0;
490 if (!strcmp(var, "format.numbered")) {
491 if (value && !strcasecmp(value, "auto")) {
492 auto_number = 1;
493 return 0;
495 numbered = git_config_bool(var, value);
496 auto_number = auto_number && numbered;
497 return 0;
500 return git_log_config(var, value, cb);
504 static const char *get_oneline_for_filename(struct commit *commit,
505 int keep_subject)
507 static char filename[PATH_MAX];
508 char *sol;
509 int len = 0;
510 int suffix_len = strlen(fmt_patch_suffix) + 1;
512 sol = strstr(commit->buffer, "\n\n");
513 if (!sol)
514 filename[0] = '\0';
515 else {
516 int j, space = 0;
518 sol += 2;
519 /* strip [PATCH] or [PATCH blabla] */
520 if (!keep_subject && !prefixcmp(sol, "[PATCH")) {
521 char *eos = strchr(sol + 6, ']');
522 if (eos) {
523 while (isspace(*eos))
524 eos++;
525 sol = eos;
529 for (j = 0;
530 j < FORMAT_PATCH_NAME_MAX - suffix_len - 5 &&
531 len < sizeof(filename) - suffix_len &&
532 sol[j] && sol[j] != '\n';
533 j++) {
534 if (istitlechar(sol[j])) {
535 if (space) {
536 filename[len++] = '-';
537 space = 0;
539 filename[len++] = sol[j];
540 if (sol[j] == '.')
541 while (sol[j + 1] == '.')
542 j++;
543 } else
544 space = 1;
546 while (filename[len - 1] == '.'
547 || filename[len - 1] == '-')
548 len--;
549 filename[len] = '\0';
551 return filename;
554 static FILE *realstdout = NULL;
555 static const char *output_directory = NULL;
557 static int reopen_stdout(const char *oneline, int nr, int total)
559 char filename[PATH_MAX];
560 int len = 0;
561 int suffix_len = strlen(fmt_patch_suffix) + 1;
563 if (output_directory) {
564 len = snprintf(filename, sizeof(filename), "%s",
565 output_directory);
566 if (len >=
567 sizeof(filename) - FORMAT_PATCH_NAME_MAX - suffix_len)
568 return error("name of output directory is too long");
569 if (filename[len - 1] != '/')
570 filename[len++] = '/';
573 if (!oneline)
574 len += sprintf(filename + len, "%d", nr);
575 else {
576 len += sprintf(filename + len, "%04d-", nr);
577 len += snprintf(filename + len, sizeof(filename) - len - 1
578 - suffix_len, "%s", oneline);
579 strcpy(filename + len, fmt_patch_suffix);
582 fprintf(realstdout, "%s\n", filename);
583 if (freopen(filename, "w", stdout) == NULL)
584 return error("Cannot open patch file %s",filename);
586 return 0;
589 static void get_patch_ids(struct rev_info *rev, struct patch_ids *ids, const char *prefix)
591 struct rev_info check_rev;
592 struct commit *commit;
593 struct object *o1, *o2;
594 unsigned flags1, flags2;
596 if (rev->pending.nr != 2)
597 die("Need exactly one range.");
599 o1 = rev->pending.objects[0].item;
600 flags1 = o1->flags;
601 o2 = rev->pending.objects[1].item;
602 flags2 = o2->flags;
604 if ((flags1 & UNINTERESTING) == (flags2 & UNINTERESTING))
605 die("Not a range.");
607 init_patch_ids(ids);
609 /* given a range a..b get all patch ids for b..a */
610 init_revisions(&check_rev, prefix);
611 o1->flags ^= UNINTERESTING;
612 o2->flags ^= UNINTERESTING;
613 add_pending_object(&check_rev, o1, "o1");
614 add_pending_object(&check_rev, o2, "o2");
615 if (prepare_revision_walk(&check_rev))
616 die("revision walk setup failed");
618 while ((commit = get_revision(&check_rev)) != NULL) {
619 /* ignore merges */
620 if (commit->parents && commit->parents->next)
621 continue;
623 add_commit_patch_id(commit, ids);
626 /* reset for next revision walk */
627 clear_commit_marks((struct commit *)o1,
628 SEEN | UNINTERESTING | SHOWN | ADDED);
629 clear_commit_marks((struct commit *)o2,
630 SEEN | UNINTERESTING | SHOWN | ADDED);
631 o1->flags = flags1;
632 o2->flags = flags2;
635 static void gen_message_id(struct rev_info *info, char *base)
637 const char *committer = git_committer_info(IDENT_WARN_ON_NO_NAME);
638 const char *email_start = strrchr(committer, '<');
639 const char *email_end = strrchr(committer, '>');
640 struct strbuf buf = STRBUF_INIT;
641 if (!email_start || !email_end || email_start > email_end - 1)
642 die("Could not extract email from committer identity.");
643 strbuf_addf(&buf, "%s.%lu.git.%.*s", base,
644 (unsigned long) time(NULL),
645 (int)(email_end - email_start - 1), email_start + 1);
646 info->message_id = strbuf_detach(&buf, NULL);
649 static void make_cover_letter(struct rev_info *rev, int use_stdout,
650 int numbered, int numbered_files,
651 struct commit *origin,
652 int nr, struct commit **list, struct commit *head)
654 const char *committer;
655 char *head_sha1;
656 const char *subject_start = NULL;
657 const char *body = "*** SUBJECT HERE ***\n\n*** BLURB HERE ***\n";
658 const char *msg;
659 const char *extra_headers = rev->extra_headers;
660 struct shortlog log;
661 struct strbuf sb = STRBUF_INIT;
662 int i;
663 const char *encoding = "utf-8";
664 struct diff_options opts;
665 int need_8bit_cte = 0;
667 if (rev->commit_format != CMIT_FMT_EMAIL)
668 die("Cover letter needs email format");
670 if (!use_stdout && reopen_stdout(numbered_files ?
671 NULL : "cover-letter", 0, rev->total))
672 return;
674 head_sha1 = sha1_to_hex(head->object.sha1);
676 log_write_email_headers(rev, head_sha1, &subject_start, &extra_headers,
677 &need_8bit_cte);
679 committer = git_committer_info(0);
681 msg = body;
682 pp_user_info(NULL, CMIT_FMT_EMAIL, &sb, committer, DATE_RFC2822,
683 encoding);
684 pp_title_line(CMIT_FMT_EMAIL, &msg, &sb, subject_start, extra_headers,
685 encoding, need_8bit_cte);
686 pp_remainder(CMIT_FMT_EMAIL, &msg, &sb, 0);
687 printf("%s\n", sb.buf);
689 strbuf_release(&sb);
691 shortlog_init(&log);
692 log.wrap_lines = 1;
693 log.wrap = 72;
694 log.in1 = 2;
695 log.in2 = 4;
696 for (i = 0; i < nr; i++)
697 shortlog_add_commit(&log, list[i]);
699 shortlog_output(&log);
702 * We can only do diffstat with a unique reference point
704 if (!origin)
705 return;
707 memcpy(&opts, &rev->diffopt, sizeof(opts));
708 opts.output_format = DIFF_FORMAT_SUMMARY | DIFF_FORMAT_DIFFSTAT;
710 diff_setup_done(&opts);
712 diff_tree_sha1(origin->tree->object.sha1,
713 head->tree->object.sha1,
714 "", &opts);
715 diffcore_std(&opts);
716 diff_flush(&opts);
718 printf("\n");
721 static const char *clean_message_id(const char *msg_id)
723 char ch;
724 const char *a, *z, *m;
726 m = msg_id;
727 while ((ch = *m) && (isspace(ch) || (ch == '<')))
728 m++;
729 a = m;
730 z = NULL;
731 while ((ch = *m)) {
732 if (!isspace(ch) && (ch != '>'))
733 z = m;
734 m++;
736 if (!z)
737 die("insane in-reply-to: %s", msg_id);
738 if (++z == m)
739 return a;
740 return xmemdupz(a, z - a);
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 thread = 0;
756 int cover_letter = 0;
757 int boundary_count = 0;
758 int no_binary_diff = 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;
777 * Parse the arguments before setup_revisions(), or something
778 * like "git format-patch -o a123 HEAD^.." may fail; a123 is
779 * possibly a valid SHA1.
781 for (i = 1, j = 1; i < argc; i++) {
782 if (!strcmp(argv[i], "--stdout"))
783 use_stdout = 1;
784 else if (!strcmp(argv[i], "-n") ||
785 !strcmp(argv[i], "--numbered"))
786 numbered = 1;
787 else if (!strcmp(argv[i], "-N") ||
788 !strcmp(argv[i], "--no-numbered")) {
789 numbered = 0;
790 auto_number = 0;
792 else if (!prefixcmp(argv[i], "--start-number="))
793 start_number = strtol(argv[i] + 15, NULL, 10);
794 else if (!strcmp(argv[i], "--numbered-files"))
795 numbered_files = 1;
796 else if (!strcmp(argv[i], "--start-number")) {
797 i++;
798 if (i == argc)
799 die("Need a number for --start-number");
800 start_number = strtol(argv[i], NULL, 10);
802 else if (!prefixcmp(argv[i], "--cc=")) {
803 ALLOC_GROW(extra_cc, extra_cc_nr + 1, extra_cc_alloc);
804 extra_cc[extra_cc_nr++] = xstrdup(argv[i] + 5);
806 else if (!strcmp(argv[i], "-k") ||
807 !strcmp(argv[i], "--keep-subject")) {
808 keep_subject = 1;
809 rev.total = -1;
811 else if (!strcmp(argv[i], "--output-directory") ||
812 !strcmp(argv[i], "-o")) {
813 i++;
814 if (argc <= i)
815 die("Which directory?");
816 if (output_directory)
817 die("Two output directories?");
818 output_directory = argv[i];
820 else if (!strcmp(argv[i], "--signoff") ||
821 !strcmp(argv[i], "-s")) {
822 const char *committer;
823 const char *endpos;
824 committer = git_committer_info(IDENT_ERROR_ON_NO_NAME);
825 endpos = strchr(committer, '>');
826 if (!endpos)
827 die("bogus committer info %s\n", committer);
828 add_signoff = xmemdupz(committer, endpos - committer + 1);
830 else if (!strcmp(argv[i], "--attach")) {
831 rev.mime_boundary = git_version_string;
832 rev.no_inline = 1;
834 else if (!prefixcmp(argv[i], "--attach=")) {
835 rev.mime_boundary = argv[i] + 9;
836 rev.no_inline = 1;
838 else if (!strcmp(argv[i], "--inline")) {
839 rev.mime_boundary = git_version_string;
840 rev.no_inline = 0;
842 else if (!prefixcmp(argv[i], "--inline=")) {
843 rev.mime_boundary = argv[i] + 9;
844 rev.no_inline = 0;
846 else if (!strcmp(argv[i], "--ignore-if-in-upstream"))
847 ignore_if_in_upstream = 1;
848 else if (!strcmp(argv[i], "--thread"))
849 thread = 1;
850 else if (!prefixcmp(argv[i], "--in-reply-to="))
851 in_reply_to = argv[i] + 14;
852 else if (!strcmp(argv[i], "--in-reply-to")) {
853 i++;
854 if (i == argc)
855 die("Need a Message-Id for --in-reply-to");
856 in_reply_to = argv[i];
857 } else if (!prefixcmp(argv[i], "--subject-prefix=")) {
858 subject_prefix = 1;
859 rev.subject_prefix = argv[i] + 17;
860 } else if (!prefixcmp(argv[i], "--suffix="))
861 fmt_patch_suffix = argv[i] + 9;
862 else if (!strcmp(argv[i], "--cover-letter"))
863 cover_letter = 1;
864 else if (!strcmp(argv[i], "--no-binary"))
865 no_binary_diff = 1;
866 else
867 argv[j++] = argv[i];
869 argc = j;
871 for (i = 0; i < extra_hdr_nr; i++) {
872 strbuf_addstr(&buf, extra_hdr[i]);
873 strbuf_addch(&buf, '\n');
876 if (extra_to_nr)
877 strbuf_addstr(&buf, "To: ");
878 for (i = 0; i < extra_to_nr; i++) {
879 if (i)
880 strbuf_addstr(&buf, " ");
881 strbuf_addstr(&buf, extra_to[i]);
882 if (i + 1 < extra_to_nr)
883 strbuf_addch(&buf, ',');
884 strbuf_addch(&buf, '\n');
887 if (extra_cc_nr)
888 strbuf_addstr(&buf, "Cc: ");
889 for (i = 0; i < extra_cc_nr; i++) {
890 if (i)
891 strbuf_addstr(&buf, " ");
892 strbuf_addstr(&buf, extra_cc[i]);
893 if (i + 1 < extra_cc_nr)
894 strbuf_addch(&buf, ',');
895 strbuf_addch(&buf, '\n');
898 rev.extra_headers = strbuf_detach(&buf, 0);
900 if (start_number < 0)
901 start_number = 1;
902 if (numbered && keep_subject)
903 die ("-n and -k are mutually exclusive.");
904 if (keep_subject && subject_prefix)
905 die ("--subject-prefix and -k are mutually exclusive.");
906 if (numbered_files && use_stdout)
907 die ("--numbered-files and --stdout are mutually exclusive.");
909 argc = setup_revisions(argc, argv, &rev, "HEAD");
910 if (argc > 1)
911 die ("unrecognized argument: %s", argv[1]);
913 if (!rev.diffopt.output_format
914 || rev.diffopt.output_format == DIFF_FORMAT_PATCH)
915 rev.diffopt.output_format = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_SUMMARY | DIFF_FORMAT_PATCH;
917 if (!DIFF_OPT_TST(&rev.diffopt, TEXT) && !no_binary_diff)
918 DIFF_OPT_SET(&rev.diffopt, BINARY);
920 if (!output_directory && !use_stdout)
921 output_directory = prefix;
923 if (output_directory) {
924 if (use_stdout)
925 die("standard output, or directory, which one?");
926 if (mkdir(output_directory, 0777) < 0 && errno != EEXIST)
927 die("Could not create directory %s",
928 output_directory);
931 if (rev.pending.nr == 1) {
932 if (rev.max_count < 0 && !rev.show_root_diff) {
934 * This is traditional behaviour of "git format-patch
935 * origin" that prepares what the origin side still
936 * does not have.
938 rev.pending.objects[0].item->flags |= UNINTERESTING;
939 add_head_to_pending(&rev);
942 * Otherwise, it is "format-patch -22 HEAD", and/or
943 * "format-patch --root HEAD". The user wants
944 * get_revision() to do the usual traversal.
949 * We cannot move this anywhere earlier because we do want to
950 * know if --root was given explicitly from the comand line.
952 rev.show_root_diff = 1;
954 if (cover_letter) {
955 /* remember the range */
956 int i;
957 for (i = 0; i < rev.pending.nr; i++) {
958 struct object *o = rev.pending.objects[i].item;
959 if (!(o->flags & UNINTERESTING))
960 head = (struct commit *)o;
962 /* We can't generate a cover letter without any patches */
963 if (!head)
964 return 0;
967 if (ignore_if_in_upstream)
968 get_patch_ids(&rev, &ids, prefix);
970 if (!use_stdout)
971 realstdout = xfdopen(xdup(1), "w");
973 if (prepare_revision_walk(&rev))
974 die("revision walk setup failed");
975 rev.boundary = 1;
976 while ((commit = get_revision(&rev)) != NULL) {
977 if (commit->object.flags & BOUNDARY) {
978 boundary_count++;
979 origin = (boundary_count == 1) ? commit : NULL;
980 continue;
983 /* ignore merges */
984 if (commit->parents && commit->parents->next)
985 continue;
987 if (ignore_if_in_upstream &&
988 has_commit_patch_id(commit, &ids))
989 continue;
991 nr++;
992 list = xrealloc(list, nr * sizeof(list[0]));
993 list[nr - 1] = commit;
995 total = nr;
996 if (!keep_subject && auto_number && total > 1)
997 numbered = 1;
998 if (numbered)
999 rev.total = total + start_number - 1;
1000 if (in_reply_to)
1001 rev.ref_message_id = clean_message_id(in_reply_to);
1002 if (cover_letter) {
1003 if (thread)
1004 gen_message_id(&rev, "cover");
1005 make_cover_letter(&rev, use_stdout, numbered, numbered_files,
1006 origin, nr, list, head);
1007 total++;
1008 start_number--;
1010 rev.add_signoff = add_signoff;
1011 while (0 <= --nr) {
1012 int shown;
1013 commit = list[nr];
1014 rev.nr = total - nr + (start_number - 1);
1015 /* Make the second and subsequent mails replies to the first */
1016 if (thread) {
1017 /* Have we already had a message ID? */
1018 if (rev.message_id) {
1020 * If we've got the ID to be a reply
1021 * to, discard the current ID;
1022 * otherwise, make everything a reply
1023 * to that.
1025 if (rev.ref_message_id)
1026 free(rev.message_id);
1027 else
1028 rev.ref_message_id = rev.message_id;
1030 gen_message_id(&rev, sha1_to_hex(commit->object.sha1));
1032 if (!use_stdout && reopen_stdout(numbered_files ? NULL :
1033 get_oneline_for_filename(commit, keep_subject),
1034 rev.nr, rev.total))
1035 die("Failed to create output files");
1036 shown = log_tree_commit(&rev, commit);
1037 free(commit->buffer);
1038 commit->buffer = NULL;
1040 /* We put one extra blank line between formatted
1041 * patches and this flag is used by log-tree code
1042 * to see if it needs to emit a LF before showing
1043 * the log; when using one file per patch, we do
1044 * not want the extra blank line.
1046 if (!use_stdout)
1047 rev.shown_one = 0;
1048 if (shown) {
1049 if (rev.mime_boundary)
1050 printf("\n--%s%s--\n\n\n",
1051 mime_boundary_leader,
1052 rev.mime_boundary);
1053 else
1054 printf("-- \n%s\n\n", git_version_string);
1056 if (!use_stdout)
1057 fclose(stdout);
1059 free(list);
1060 if (ignore_if_in_upstream)
1061 free_patch_ids(&ids);
1062 return 0;
1065 static int add_pending_commit(const char *arg, struct rev_info *revs, int flags)
1067 unsigned char sha1[20];
1068 if (get_sha1(arg, sha1) == 0) {
1069 struct commit *commit = lookup_commit_reference(sha1);
1070 if (commit) {
1071 commit->object.flags |= flags;
1072 add_pending_object(revs, &commit->object, arg);
1073 return 0;
1076 return -1;
1079 static const char cherry_usage[] =
1080 "git cherry [-v] <upstream> [<head>] [<limit>]";
1081 int cmd_cherry(int argc, const char **argv, const char *prefix)
1083 struct rev_info revs;
1084 struct patch_ids ids;
1085 struct commit *commit;
1086 struct commit_list *list = NULL;
1087 const char *upstream;
1088 const char *head = "HEAD";
1089 const char *limit = NULL;
1090 int verbose = 0;
1092 if (argc > 1 && !strcmp(argv[1], "-v")) {
1093 verbose = 1;
1094 argc--;
1095 argv++;
1098 switch (argc) {
1099 case 4:
1100 limit = argv[3];
1101 /* FALLTHROUGH */
1102 case 3:
1103 head = argv[2];
1104 /* FALLTHROUGH */
1105 case 2:
1106 upstream = argv[1];
1107 break;
1108 default:
1109 usage(cherry_usage);
1112 init_revisions(&revs, prefix);
1113 revs.diff = 1;
1114 revs.combine_merges = 0;
1115 revs.ignore_merges = 1;
1116 DIFF_OPT_SET(&revs.diffopt, RECURSIVE);
1118 if (add_pending_commit(head, &revs, 0))
1119 die("Unknown commit %s", head);
1120 if (add_pending_commit(upstream, &revs, UNINTERESTING))
1121 die("Unknown commit %s", upstream);
1123 /* Don't say anything if head and upstream are the same. */
1124 if (revs.pending.nr == 2) {
1125 struct object_array_entry *o = revs.pending.objects;
1126 if (hashcmp(o[0].item->sha1, o[1].item->sha1) == 0)
1127 return 0;
1130 get_patch_ids(&revs, &ids, prefix);
1132 if (limit && add_pending_commit(limit, &revs, UNINTERESTING))
1133 die("Unknown commit %s", limit);
1135 /* reverse the list of commits */
1136 if (prepare_revision_walk(&revs))
1137 die("revision walk setup failed");
1138 while ((commit = get_revision(&revs)) != NULL) {
1139 /* ignore merges */
1140 if (commit->parents && commit->parents->next)
1141 continue;
1143 commit_list_insert(commit, &list);
1146 while (list) {
1147 char sign = '+';
1149 commit = list->item;
1150 if (has_commit_patch_id(commit, &ids))
1151 sign = '-';
1153 if (verbose) {
1154 struct strbuf buf = STRBUF_INIT;
1155 pretty_print_commit(CMIT_FMT_ONELINE, commit,
1156 &buf, 0, NULL, NULL, 0, 0);
1157 printf("%c %s %s\n", sign,
1158 sha1_to_hex(commit->object.sha1), buf.buf);
1159 strbuf_release(&buf);
1161 else {
1162 printf("%c %s\n", sign,
1163 sha1_to_hex(commit->object.sha1));
1166 list = list->next;
1169 free_patch_ids(&ids);
1170 return 0;