Reset the signal being handled
[git/kirr.git] / builtin-log.c
blob543855b7ad11bd0355d88bc1502030509875e0c7
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 "refs.h"
18 #include "run-command.h"
19 #include "shortlog.h"
21 /* Set a default date-time format for git log ("log.date" config variable) */
22 static const char *default_date_mode = NULL;
24 static int default_show_root = 1;
25 static const char *fmt_patch_subject_prefix = "PATCH";
26 static const char *fmt_pretty;
28 static void add_name_decoration(const char *prefix, const char *name, struct object *obj)
30 int plen = strlen(prefix);
31 int nlen = strlen(name);
32 struct name_decoration *res = xmalloc(sizeof(struct name_decoration) + plen + nlen);
33 memcpy(res->name, prefix, plen);
34 memcpy(res->name + plen, name, nlen + 1);
35 res->next = add_decoration(&name_decoration, obj, res);
38 static int add_ref_decoration(const char *refname, const unsigned char *sha1, int flags, void *cb_data)
40 struct object *obj = parse_object(sha1);
41 if (!obj)
42 return 0;
43 add_name_decoration("", refname, obj);
44 while (obj->type == OBJ_TAG) {
45 obj = ((struct tag *)obj)->tagged;
46 if (!obj)
47 break;
48 add_name_decoration("tag: ", refname, obj);
50 return 0;
53 static void cmd_log_init(int argc, const char **argv, const char *prefix,
54 struct rev_info *rev)
56 int i;
57 int decorate = 0;
59 rev->abbrev = DEFAULT_ABBREV;
60 rev->commit_format = CMIT_FMT_DEFAULT;
61 if (fmt_pretty)
62 get_commit_format(fmt_pretty, rev);
63 rev->verbose_header = 1;
64 DIFF_OPT_SET(&rev->diffopt, RECURSIVE);
65 rev->show_root_diff = default_show_root;
66 rev->subject_prefix = fmt_patch_subject_prefix;
68 if (default_date_mode)
69 rev->date_mode = parse_date_format(default_date_mode);
71 argc = setup_revisions(argc, argv, rev, "HEAD");
73 if (rev->diffopt.pickaxe || rev->diffopt.filter)
74 rev->always_show_header = 0;
75 if (DIFF_OPT_TST(&rev->diffopt, FOLLOW_RENAMES)) {
76 rev->always_show_header = 0;
77 if (rev->diffopt.nr_paths != 1)
78 usage("git logs can only follow renames on one pathname at a time");
80 for (i = 1; i < argc; i++) {
81 const char *arg = argv[i];
82 if (!strcmp(arg, "--decorate")) {
83 if (!decorate)
84 for_each_ref(add_ref_decoration, NULL);
85 decorate = 1;
86 } else
87 die("unrecognized argument: %s", arg);
92 * This gives a rough estimate for how many commits we
93 * will print out in the list.
95 static int estimate_commit_count(struct rev_info *rev, struct commit_list *list)
97 int n = 0;
99 while (list) {
100 struct commit *commit = list->item;
101 unsigned int flags = commit->object.flags;
102 list = list->next;
103 if (!(flags & (TREESAME | UNINTERESTING)))
104 n++;
106 return n;
109 static void show_early_header(struct rev_info *rev, const char *stage, int nr)
111 if (rev->shown_one) {
112 rev->shown_one = 0;
113 if (rev->commit_format != CMIT_FMT_ONELINE)
114 putchar(rev->diffopt.line_termination);
116 printf("Final output: %d %s\n", nr, stage);
119 struct itimerval early_output_timer;
121 static void log_show_early(struct rev_info *revs, struct commit_list *list)
123 int i = revs->early_output;
124 int show_header = 1;
126 sort_in_topological_order(&list, revs->lifo);
127 while (list && i) {
128 struct commit *commit = list->item;
129 switch (simplify_commit(revs, commit)) {
130 case commit_show:
131 if (show_header) {
132 int n = estimate_commit_count(revs, list);
133 show_early_header(revs, "incomplete", n);
134 show_header = 0;
136 log_tree_commit(revs, commit);
137 i--;
138 break;
139 case commit_ignore:
140 break;
141 case commit_error:
142 return;
144 list = list->next;
147 /* Did we already get enough commits for the early output? */
148 if (!i)
149 return;
152 * ..if no, then repeat it twice a second until we
153 * do.
155 * NOTE! We don't use "it_interval", because if the
156 * reader isn't listening, we want our output to be
157 * throttled by the writing, and not have the timer
158 * trigger every second even if we're blocked on a
159 * reader!
161 early_output_timer.it_value.tv_sec = 0;
162 early_output_timer.it_value.tv_usec = 500000;
163 setitimer(ITIMER_REAL, &early_output_timer, NULL);
166 static void early_output(int signal)
168 show_early_output = log_show_early;
171 static void setup_early_output(struct rev_info *rev)
173 struct sigaction sa;
176 * Set up the signal handler, minimally intrusively:
177 * we only set a single volatile integer word (not
178 * using sigatomic_t - trying to avoid unnecessary
179 * system dependencies and headers), and using
180 * SA_RESTART.
182 memset(&sa, 0, sizeof(sa));
183 sa.sa_handler = early_output;
184 sigemptyset(&sa.sa_mask);
185 sa.sa_flags = SA_RESTART;
186 sigaction(SIGALRM, &sa, NULL);
189 * If we can get the whole output in less than a
190 * tenth of a second, don't even bother doing the
191 * early-output thing..
193 * This is a one-time-only trigger.
195 early_output_timer.it_value.tv_sec = 0;
196 early_output_timer.it_value.tv_usec = 100000;
197 setitimer(ITIMER_REAL, &early_output_timer, NULL);
200 static void finish_early_output(struct rev_info *rev)
202 int n = estimate_commit_count(rev, rev->commits);
203 signal(SIGALRM, SIG_IGN);
204 show_early_header(rev, "done", n);
207 static int cmd_log_walk(struct rev_info *rev)
209 struct commit *commit;
211 if (rev->early_output)
212 setup_early_output(rev);
214 if (prepare_revision_walk(rev))
215 die("revision walk setup failed");
217 if (rev->early_output)
218 finish_early_output(rev);
220 while ((commit = get_revision(rev)) != NULL) {
221 log_tree_commit(rev, commit);
222 if (!rev->reflog_info) {
223 /* we allow cycles in reflog ancestry */
224 free(commit->buffer);
225 commit->buffer = NULL;
227 free_commit_list(commit->parents);
228 commit->parents = NULL;
230 return 0;
233 static int git_log_config(const char *var, const char *value)
235 if (!strcmp(var, "format.pretty"))
236 return git_config_string(&fmt_pretty, var, value);
237 if (!strcmp(var, "format.subjectprefix")) {
238 if (!value)
239 config_error_nonbool(var);
240 fmt_patch_subject_prefix = xstrdup(value);
241 return 0;
243 if (!strcmp(var, "log.date"))
244 return git_config_string(&default_date_mode, var, value);
245 if (!strcmp(var, "log.showroot")) {
246 default_show_root = git_config_bool(var, value);
247 return 0;
249 return git_diff_ui_config(var, value);
252 int cmd_whatchanged(int argc, const char **argv, const char *prefix)
254 struct rev_info rev;
256 git_config(git_log_config);
258 if (diff_use_color_default == -1)
259 diff_use_color_default = git_use_color_default;
261 init_revisions(&rev, prefix);
262 rev.diff = 1;
263 rev.simplify_history = 0;
264 cmd_log_init(argc, argv, prefix, &rev);
265 if (!rev.diffopt.output_format)
266 rev.diffopt.output_format = DIFF_FORMAT_RAW;
267 return cmd_log_walk(&rev);
270 static void show_tagger(char *buf, int len, struct rev_info *rev)
272 char *email_end, *p;
273 unsigned long date;
274 int tz;
276 email_end = memchr(buf, '>', len);
277 if (!email_end)
278 return;
279 p = ++email_end;
280 while (isspace(*p))
281 p++;
282 date = strtoul(p, &p, 10);
283 while (isspace(*p))
284 p++;
285 tz = (int)strtol(p, NULL, 10);
286 printf("Tagger: %.*s\nDate: %s\n", (int)(email_end - buf), buf,
287 show_date(date, tz, rev->date_mode));
290 static int show_object(const unsigned char *sha1, int show_tag_object,
291 struct rev_info *rev)
293 unsigned long size;
294 enum object_type type;
295 char *buf = read_sha1_file(sha1, &type, &size);
296 int offset = 0;
298 if (!buf)
299 return error("Could not read object %s", sha1_to_hex(sha1));
301 if (show_tag_object)
302 while (offset < size && buf[offset] != '\n') {
303 int new_offset = offset + 1;
304 while (new_offset < size && buf[new_offset++] != '\n')
305 ; /* do nothing */
306 if (!prefixcmp(buf + offset, "tagger "))
307 show_tagger(buf + offset + 7,
308 new_offset - offset - 7, rev);
309 offset = new_offset;
312 if (offset < size)
313 fwrite(buf + offset, size - offset, 1, stdout);
314 free(buf);
315 return 0;
318 static int show_tree_object(const unsigned char *sha1,
319 const char *base, int baselen,
320 const char *pathname, unsigned mode, int stage)
322 printf("%s%s\n", pathname, S_ISDIR(mode) ? "/" : "");
323 return 0;
326 int cmd_show(int argc, const char **argv, const char *prefix)
328 struct rev_info rev;
329 struct object_array_entry *objects;
330 int i, count, ret = 0;
332 git_config(git_log_config);
334 if (diff_use_color_default == -1)
335 diff_use_color_default = git_use_color_default;
337 init_revisions(&rev, prefix);
338 rev.diff = 1;
339 rev.combine_merges = 1;
340 rev.dense_combined_merges = 1;
341 rev.always_show_header = 1;
342 rev.ignore_merges = 0;
343 rev.no_walk = 1;
344 cmd_log_init(argc, argv, prefix, &rev);
346 count = rev.pending.nr;
347 objects = rev.pending.objects;
348 for (i = 0; i < count && !ret; i++) {
349 struct object *o = objects[i].item;
350 const char *name = objects[i].name;
351 switch (o->type) {
352 case OBJ_BLOB:
353 ret = show_object(o->sha1, 0, NULL);
354 break;
355 case OBJ_TAG: {
356 struct tag *t = (struct tag *)o;
358 printf("%stag %s%s\n",
359 diff_get_color_opt(&rev.diffopt, DIFF_COMMIT),
360 t->tag,
361 diff_get_color_opt(&rev.diffopt, DIFF_RESET));
362 ret = show_object(o->sha1, 1, &rev);
363 objects[i].item = (struct object *)t->tagged;
364 i--;
365 break;
367 case OBJ_TREE:
368 printf("%stree %s%s\n\n",
369 diff_get_color_opt(&rev.diffopt, DIFF_COMMIT),
370 name,
371 diff_get_color_opt(&rev.diffopt, DIFF_RESET));
372 read_tree_recursive((struct tree *)o, "", 0, 0, NULL,
373 show_tree_object);
374 break;
375 case OBJ_COMMIT:
376 rev.pending.nr = rev.pending.alloc = 0;
377 rev.pending.objects = NULL;
378 add_object_array(o, name, &rev.pending);
379 ret = cmd_log_walk(&rev);
380 break;
381 default:
382 ret = error("Unknown type: %d", o->type);
385 free(objects);
386 return ret;
390 * This is equivalent to "git log -g --abbrev-commit --pretty=oneline"
392 int cmd_log_reflog(int argc, const char **argv, const char *prefix)
394 struct rev_info rev;
396 git_config(git_log_config);
398 if (diff_use_color_default == -1)
399 diff_use_color_default = git_use_color_default;
401 init_revisions(&rev, prefix);
402 init_reflog_walk(&rev.reflog_info);
403 rev.abbrev_commit = 1;
404 rev.verbose_header = 1;
405 cmd_log_init(argc, argv, prefix, &rev);
408 * This means that we override whatever commit format the user gave
409 * on the cmd line. Sad, but cmd_log_init() currently doesn't
410 * allow us to set a different default.
412 rev.commit_format = CMIT_FMT_ONELINE;
413 rev.use_terminator = 1;
414 rev.always_show_header = 1;
417 * We get called through "git reflog", so unlike the other log
418 * routines, we need to set up our pager manually..
420 setup_pager();
422 return cmd_log_walk(&rev);
425 int cmd_log(int argc, const char **argv, const char *prefix)
427 struct rev_info rev;
429 git_config(git_log_config);
431 if (diff_use_color_default == -1)
432 diff_use_color_default = git_use_color_default;
434 init_revisions(&rev, prefix);
435 rev.always_show_header = 1;
436 cmd_log_init(argc, argv, prefix, &rev);
437 return cmd_log_walk(&rev);
440 /* format-patch */
441 #define FORMAT_PATCH_NAME_MAX 64
443 static int istitlechar(char c)
445 return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
446 (c >= '0' && c <= '9') || c == '.' || c == '_';
449 static const char *fmt_patch_suffix = ".patch";
450 static int numbered = 0;
451 static int auto_number = 0;
453 static char **extra_hdr;
454 static int extra_hdr_nr;
455 static int extra_hdr_alloc;
457 static char **extra_to;
458 static int extra_to_nr;
459 static int extra_to_alloc;
461 static char **extra_cc;
462 static int extra_cc_nr;
463 static int extra_cc_alloc;
465 static void add_header(const char *value)
467 int len = strlen(value);
468 while (value[len - 1] == '\n')
469 len--;
470 if (!strncasecmp(value, "to: ", 4)) {
471 ALLOC_GROW(extra_to, extra_to_nr + 1, extra_to_alloc);
472 extra_to[extra_to_nr++] = xstrndup(value + 4, len - 4);
473 return;
475 if (!strncasecmp(value, "cc: ", 4)) {
476 ALLOC_GROW(extra_cc, extra_cc_nr + 1, extra_cc_alloc);
477 extra_cc[extra_cc_nr++] = xstrndup(value + 4, len - 4);
478 return;
480 ALLOC_GROW(extra_hdr, extra_hdr_nr + 1, extra_hdr_alloc);
481 extra_hdr[extra_hdr_nr++] = xstrndup(value, len);
484 static int git_format_config(const char *var, const char *value)
486 if (!strcmp(var, "format.headers")) {
487 if (!value)
488 die("format.headers without value");
489 add_header(value);
490 return 0;
492 if (!strcmp(var, "format.suffix")) {
493 if (!value)
494 return config_error_nonbool(var);
495 fmt_patch_suffix = xstrdup(value);
496 return 0;
498 if (!strcmp(var, "format.cc")) {
499 if (!value)
500 return config_error_nonbool(var);
501 ALLOC_GROW(extra_cc, extra_cc_nr + 1, extra_cc_alloc);
502 extra_cc[extra_cc_nr++] = xstrdup(value);
503 return 0;
505 if (!strcmp(var, "diff.color") || !strcmp(var, "color.diff")) {
506 return 0;
508 if (!strcmp(var, "format.numbered")) {
509 if (value && !strcasecmp(value, "auto")) {
510 auto_number = 1;
511 return 0;
513 numbered = git_config_bool(var, value);
514 return 0;
517 return git_log_config(var, value);
521 static const char *get_oneline_for_filename(struct commit *commit,
522 int keep_subject)
524 static char filename[PATH_MAX];
525 char *sol;
526 int len = 0;
527 int suffix_len = strlen(fmt_patch_suffix) + 1;
529 sol = strstr(commit->buffer, "\n\n");
530 if (!sol)
531 filename[0] = '\0';
532 else {
533 int j, space = 0;
535 sol += 2;
536 /* strip [PATCH] or [PATCH blabla] */
537 if (!keep_subject && !prefixcmp(sol, "[PATCH")) {
538 char *eos = strchr(sol + 6, ']');
539 if (eos) {
540 while (isspace(*eos))
541 eos++;
542 sol = eos;
546 for (j = 0;
547 j < FORMAT_PATCH_NAME_MAX - suffix_len - 5 &&
548 len < sizeof(filename) - suffix_len &&
549 sol[j] && sol[j] != '\n';
550 j++) {
551 if (istitlechar(sol[j])) {
552 if (space) {
553 filename[len++] = '-';
554 space = 0;
556 filename[len++] = sol[j];
557 if (sol[j] == '.')
558 while (sol[j + 1] == '.')
559 j++;
560 } else
561 space = 1;
563 while (filename[len - 1] == '.'
564 || filename[len - 1] == '-')
565 len--;
566 filename[len] = '\0';
568 return filename;
571 static FILE *realstdout = NULL;
572 static const char *output_directory = NULL;
574 static int reopen_stdout(const char *oneline, int nr, int total)
576 char filename[PATH_MAX];
577 int len = 0;
578 int suffix_len = strlen(fmt_patch_suffix) + 1;
580 if (output_directory) {
581 len = snprintf(filename, sizeof(filename), "%s",
582 output_directory);
583 if (len >=
584 sizeof(filename) - FORMAT_PATCH_NAME_MAX - suffix_len)
585 return error("name of output directory is too long");
586 if (filename[len - 1] != '/')
587 filename[len++] = '/';
590 if (!oneline)
591 len += sprintf(filename + len, "%d", nr);
592 else {
593 len += sprintf(filename + len, "%04d-", nr);
594 len += snprintf(filename + len, sizeof(filename) - len - 1
595 - suffix_len, "%s", oneline);
596 strcpy(filename + len, fmt_patch_suffix);
599 fprintf(realstdout, "%s\n", filename);
600 if (freopen(filename, "w", stdout) == NULL)
601 return error("Cannot open patch file %s",filename);
603 return 0;
606 static void get_patch_ids(struct rev_info *rev, struct patch_ids *ids, const char *prefix)
608 struct rev_info check_rev;
609 struct commit *commit;
610 struct object *o1, *o2;
611 unsigned flags1, flags2;
613 if (rev->pending.nr != 2)
614 die("Need exactly one range.");
616 o1 = rev->pending.objects[0].item;
617 flags1 = o1->flags;
618 o2 = rev->pending.objects[1].item;
619 flags2 = o2->flags;
621 if ((flags1 & UNINTERESTING) == (flags2 & UNINTERESTING))
622 die("Not a range.");
624 init_patch_ids(ids);
626 /* given a range a..b get all patch ids for b..a */
627 init_revisions(&check_rev, prefix);
628 o1->flags ^= UNINTERESTING;
629 o2->flags ^= UNINTERESTING;
630 add_pending_object(&check_rev, o1, "o1");
631 add_pending_object(&check_rev, o2, "o2");
632 if (prepare_revision_walk(&check_rev))
633 die("revision walk setup failed");
635 while ((commit = get_revision(&check_rev)) != NULL) {
636 /* ignore merges */
637 if (commit->parents && commit->parents->next)
638 continue;
640 add_commit_patch_id(commit, ids);
643 /* reset for next revision walk */
644 clear_commit_marks((struct commit *)o1,
645 SEEN | UNINTERESTING | SHOWN | ADDED);
646 clear_commit_marks((struct commit *)o2,
647 SEEN | UNINTERESTING | SHOWN | ADDED);
648 o1->flags = flags1;
649 o2->flags = flags2;
652 static void gen_message_id(struct rev_info *info, char *base)
654 const char *committer = git_committer_info(IDENT_WARN_ON_NO_NAME);
655 const char *email_start = strrchr(committer, '<');
656 const char *email_end = strrchr(committer, '>');
657 struct strbuf buf;
658 if (!email_start || !email_end || email_start > email_end - 1)
659 die("Could not extract email from committer identity.");
660 strbuf_init(&buf, 0);
661 strbuf_addf(&buf, "%s.%lu.git.%.*s", base,
662 (unsigned long) time(NULL),
663 (int)(email_end - email_start - 1), email_start + 1);
664 info->message_id = strbuf_detach(&buf, NULL);
667 static void make_cover_letter(struct rev_info *rev, int use_stdout,
668 int numbered, int numbered_files,
669 struct commit *origin,
670 int nr, struct commit **list, struct commit *head)
672 const char *committer;
673 char *head_sha1;
674 const char *subject_start = NULL;
675 const char *body = "*** SUBJECT HERE ***\n\n*** BLURB HERE ***\n";
676 const char *msg;
677 const char *extra_headers = rev->extra_headers;
678 struct shortlog log;
679 struct strbuf sb;
680 int i;
681 const char *encoding = "utf-8";
682 struct diff_options opts;
683 int need_8bit_cte = 0;
685 if (rev->commit_format != CMIT_FMT_EMAIL)
686 die("Cover letter needs email format");
688 if (!use_stdout && reopen_stdout(numbered_files ?
689 NULL : "cover-letter", 0, rev->total))
690 return;
692 head_sha1 = sha1_to_hex(head->object.sha1);
694 log_write_email_headers(rev, head_sha1, &subject_start, &extra_headers,
695 &need_8bit_cte);
697 committer = git_committer_info(0);
699 msg = body;
700 strbuf_init(&sb, 0);
701 pp_user_info(NULL, CMIT_FMT_EMAIL, &sb, committer, DATE_RFC2822,
702 encoding);
703 pp_title_line(CMIT_FMT_EMAIL, &msg, &sb, subject_start, extra_headers,
704 encoding, need_8bit_cte);
705 pp_remainder(CMIT_FMT_EMAIL, &msg, &sb, 0);
706 printf("%s\n", sb.buf);
708 strbuf_release(&sb);
710 shortlog_init(&log);
711 log.wrap_lines = 1;
712 log.wrap = 72;
713 log.in1 = 2;
714 log.in2 = 4;
715 for (i = 0; i < nr; i++)
716 shortlog_add_commit(&log, list[i]);
718 shortlog_output(&log);
721 * We can only do diffstat with a unique reference point
723 if (!origin)
724 return;
726 memcpy(&opts, &rev->diffopt, sizeof(opts));
727 opts.output_format = DIFF_FORMAT_SUMMARY | DIFF_FORMAT_DIFFSTAT;
729 diff_setup_done(&opts);
731 diff_tree_sha1(origin->tree->object.sha1,
732 head->tree->object.sha1,
733 "", &opts);
734 diffcore_std(&opts);
735 diff_flush(&opts);
737 printf("\n");
740 static const char *clean_message_id(const char *msg_id)
742 char ch;
743 const char *a, *z, *m;
745 m = msg_id;
746 while ((ch = *m) && (isspace(ch) || (ch == '<')))
747 m++;
748 a = m;
749 z = NULL;
750 while ((ch = *m)) {
751 if (!isspace(ch) && (ch != '>'))
752 z = m;
753 m++;
755 if (!z)
756 die("insane in-reply-to: %s", msg_id);
757 if (++z == m)
758 return a;
759 return xmemdupz(a, z - a);
762 int cmd_format_patch(int argc, const char **argv, const char *prefix)
764 struct commit *commit;
765 struct commit **list = NULL;
766 struct rev_info rev;
767 int nr = 0, total, i, j;
768 int use_stdout = 0;
769 int start_number = -1;
770 int keep_subject = 0;
771 int numbered_files = 0; /* _just_ numbers */
772 int subject_prefix = 0;
773 int ignore_if_in_upstream = 0;
774 int thread = 0;
775 int cover_letter = 0;
776 int boundary_count = 0;
777 int no_binary_diff = 0;
778 struct commit *origin = NULL, *head = NULL;
779 const char *in_reply_to = NULL;
780 struct patch_ids ids;
781 char *add_signoff = NULL;
782 struct strbuf buf;
784 git_config(git_format_config);
785 init_revisions(&rev, prefix);
786 rev.commit_format = CMIT_FMT_EMAIL;
787 rev.verbose_header = 1;
788 rev.diff = 1;
789 rev.combine_merges = 0;
790 rev.ignore_merges = 1;
791 DIFF_OPT_SET(&rev.diffopt, RECURSIVE);
793 rev.subject_prefix = fmt_patch_subject_prefix;
796 * Parse the arguments before setup_revisions(), or something
797 * like "git format-patch -o a123 HEAD^.." may fail; a123 is
798 * possibly a valid SHA1.
800 for (i = 1, j = 1; i < argc; i++) {
801 if (!strcmp(argv[i], "--stdout"))
802 use_stdout = 1;
803 else if (!strcmp(argv[i], "-n") ||
804 !strcmp(argv[i], "--numbered"))
805 numbered = 1;
806 else if (!strcmp(argv[i], "-N") ||
807 !strcmp(argv[i], "--no-numbered")) {
808 numbered = 0;
809 auto_number = 0;
811 else if (!prefixcmp(argv[i], "--start-number="))
812 start_number = strtol(argv[i] + 15, NULL, 10);
813 else if (!strcmp(argv[i], "--numbered-files"))
814 numbered_files = 1;
815 else if (!strcmp(argv[i], "--start-number")) {
816 i++;
817 if (i == argc)
818 die("Need a number for --start-number");
819 start_number = strtol(argv[i], NULL, 10);
821 else if (!prefixcmp(argv[i], "--cc=")) {
822 ALLOC_GROW(extra_cc, extra_cc_nr + 1, extra_cc_alloc);
823 extra_cc[extra_cc_nr++] = xstrdup(argv[i] + 5);
825 else if (!strcmp(argv[i], "-k") ||
826 !strcmp(argv[i], "--keep-subject")) {
827 keep_subject = 1;
828 rev.total = -1;
830 else if (!strcmp(argv[i], "--output-directory") ||
831 !strcmp(argv[i], "-o")) {
832 i++;
833 if (argc <= i)
834 die("Which directory?");
835 if (output_directory)
836 die("Two output directories?");
837 output_directory = argv[i];
839 else if (!strcmp(argv[i], "--signoff") ||
840 !strcmp(argv[i], "-s")) {
841 const char *committer;
842 const char *endpos;
843 committer = git_committer_info(IDENT_ERROR_ON_NO_NAME);
844 endpos = strchr(committer, '>');
845 if (!endpos)
846 die("bogos committer info %s\n", committer);
847 add_signoff = xmemdupz(committer, endpos - committer + 1);
849 else if (!strcmp(argv[i], "--attach")) {
850 rev.mime_boundary = git_version_string;
851 rev.no_inline = 1;
853 else if (!prefixcmp(argv[i], "--attach=")) {
854 rev.mime_boundary = argv[i] + 9;
855 rev.no_inline = 1;
857 else if (!strcmp(argv[i], "--inline")) {
858 rev.mime_boundary = git_version_string;
859 rev.no_inline = 0;
861 else if (!prefixcmp(argv[i], "--inline=")) {
862 rev.mime_boundary = argv[i] + 9;
863 rev.no_inline = 0;
865 else if (!strcmp(argv[i], "--ignore-if-in-upstream"))
866 ignore_if_in_upstream = 1;
867 else if (!strcmp(argv[i], "--thread"))
868 thread = 1;
869 else if (!prefixcmp(argv[i], "--in-reply-to="))
870 in_reply_to = argv[i] + 14;
871 else if (!strcmp(argv[i], "--in-reply-to")) {
872 i++;
873 if (i == argc)
874 die("Need a Message-Id for --in-reply-to");
875 in_reply_to = argv[i];
876 } else if (!prefixcmp(argv[i], "--subject-prefix=")) {
877 subject_prefix = 1;
878 rev.subject_prefix = argv[i] + 17;
879 } else if (!prefixcmp(argv[i], "--suffix="))
880 fmt_patch_suffix = argv[i] + 9;
881 else if (!strcmp(argv[i], "--cover-letter"))
882 cover_letter = 1;
883 else if (!strcmp(argv[i], "--no-binary"))
884 no_binary_diff = 1;
885 else
886 argv[j++] = argv[i];
888 argc = j;
890 strbuf_init(&buf, 0);
892 for (i = 0; i < extra_hdr_nr; i++) {
893 strbuf_addstr(&buf, extra_hdr[i]);
894 strbuf_addch(&buf, '\n');
897 if (extra_to_nr)
898 strbuf_addstr(&buf, "To: ");
899 for (i = 0; i < extra_to_nr; i++) {
900 if (i)
901 strbuf_addstr(&buf, " ");
902 strbuf_addstr(&buf, extra_to[i]);
903 if (i + 1 < extra_to_nr)
904 strbuf_addch(&buf, ',');
905 strbuf_addch(&buf, '\n');
908 if (extra_cc_nr)
909 strbuf_addstr(&buf, "Cc: ");
910 for (i = 0; i < extra_cc_nr; i++) {
911 if (i)
912 strbuf_addstr(&buf, " ");
913 strbuf_addstr(&buf, extra_cc[i]);
914 if (i + 1 < extra_cc_nr)
915 strbuf_addch(&buf, ',');
916 strbuf_addch(&buf, '\n');
919 rev.extra_headers = strbuf_detach(&buf, 0);
921 if (start_number < 0)
922 start_number = 1;
923 if (numbered && keep_subject)
924 die ("-n and -k are mutually exclusive.");
925 if (keep_subject && subject_prefix)
926 die ("--subject-prefix and -k are mutually exclusive.");
927 if (numbered_files && use_stdout)
928 die ("--numbered-files and --stdout are mutually exclusive.");
930 argc = setup_revisions(argc, argv, &rev, "HEAD");
931 if (argc > 1)
932 die ("unrecognized argument: %s", argv[1]);
934 if (!rev.diffopt.output_format)
935 rev.diffopt.output_format = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_SUMMARY | DIFF_FORMAT_PATCH;
937 if (!DIFF_OPT_TST(&rev.diffopt, TEXT) && !no_binary_diff)
938 DIFF_OPT_SET(&rev.diffopt, BINARY);
940 if (!output_directory && !use_stdout)
941 output_directory = prefix;
943 if (output_directory) {
944 if (use_stdout)
945 die("standard output, or directory, which one?");
946 if (mkdir(output_directory, 0777) < 0 && errno != EEXIST)
947 die("Could not create directory %s",
948 output_directory);
951 if (rev.pending.nr == 1) {
952 if (rev.max_count < 0 && !rev.show_root_diff) {
954 * This is traditional behaviour of "git format-patch
955 * origin" that prepares what the origin side still
956 * does not have.
958 rev.pending.objects[0].item->flags |= UNINTERESTING;
959 add_head_to_pending(&rev);
962 * Otherwise, it is "format-patch -22 HEAD", and/or
963 * "format-patch --root HEAD". The user wants
964 * get_revision() to do the usual traversal.
967 if (cover_letter) {
968 /* remember the range */
969 int i;
970 for (i = 0; i < rev.pending.nr; i++) {
971 struct object *o = rev.pending.objects[i].item;
972 if (!(o->flags & UNINTERESTING))
973 head = (struct commit *)o;
975 /* We can't generate a cover letter without any patches */
976 if (!head)
977 return 0;
980 if (ignore_if_in_upstream)
981 get_patch_ids(&rev, &ids, prefix);
983 if (!use_stdout)
984 realstdout = xfdopen(xdup(1), "w");
986 if (prepare_revision_walk(&rev))
987 die("revision walk setup failed");
988 rev.boundary = 1;
989 while ((commit = get_revision(&rev)) != NULL) {
990 if (commit->object.flags & BOUNDARY) {
991 boundary_count++;
992 origin = (boundary_count == 1) ? commit : NULL;
993 continue;
996 /* ignore merges */
997 if (commit->parents && commit->parents->next)
998 continue;
1000 if (ignore_if_in_upstream &&
1001 has_commit_patch_id(commit, &ids))
1002 continue;
1004 nr++;
1005 list = xrealloc(list, nr * sizeof(list[0]));
1006 list[nr - 1] = commit;
1008 total = nr;
1009 if (!keep_subject && auto_number && total > 1)
1010 numbered = 1;
1011 if (numbered)
1012 rev.total = total + start_number - 1;
1013 if (in_reply_to)
1014 rev.ref_message_id = clean_message_id(in_reply_to);
1015 if (cover_letter) {
1016 if (thread)
1017 gen_message_id(&rev, "cover");
1018 make_cover_letter(&rev, use_stdout, numbered, numbered_files,
1019 origin, nr, list, head);
1020 total++;
1021 start_number--;
1023 rev.add_signoff = add_signoff;
1024 while (0 <= --nr) {
1025 int shown;
1026 commit = list[nr];
1027 rev.nr = total - nr + (start_number - 1);
1028 /* Make the second and subsequent mails replies to the first */
1029 if (thread) {
1030 /* Have we already had a message ID? */
1031 if (rev.message_id) {
1033 * If we've got the ID to be a reply
1034 * to, discard the current ID;
1035 * otherwise, make everything a reply
1036 * to that.
1038 if (rev.ref_message_id)
1039 free(rev.message_id);
1040 else
1041 rev.ref_message_id = rev.message_id;
1043 gen_message_id(&rev, sha1_to_hex(commit->object.sha1));
1045 if (!use_stdout && reopen_stdout(numbered_files ? NULL :
1046 get_oneline_for_filename(commit, keep_subject),
1047 rev.nr, rev.total))
1048 die("Failed to create output files");
1049 shown = log_tree_commit(&rev, commit);
1050 free(commit->buffer);
1051 commit->buffer = NULL;
1053 /* We put one extra blank line between formatted
1054 * patches and this flag is used by log-tree code
1055 * to see if it needs to emit a LF before showing
1056 * the log; when using one file per patch, we do
1057 * not want the extra blank line.
1059 if (!use_stdout)
1060 rev.shown_one = 0;
1061 if (shown) {
1062 if (rev.mime_boundary)
1063 printf("\n--%s%s--\n\n\n",
1064 mime_boundary_leader,
1065 rev.mime_boundary);
1066 else
1067 printf("-- \n%s\n\n", git_version_string);
1069 if (!use_stdout)
1070 fclose(stdout);
1072 free(list);
1073 if (ignore_if_in_upstream)
1074 free_patch_ids(&ids);
1075 return 0;
1078 static int add_pending_commit(const char *arg, struct rev_info *revs, int flags)
1080 unsigned char sha1[20];
1081 if (get_sha1(arg, sha1) == 0) {
1082 struct commit *commit = lookup_commit_reference(sha1);
1083 if (commit) {
1084 commit->object.flags |= flags;
1085 add_pending_object(revs, &commit->object, arg);
1086 return 0;
1089 return -1;
1092 static const char cherry_usage[] =
1093 "git-cherry [-v] <upstream> [<head>] [<limit>]";
1094 int cmd_cherry(int argc, const char **argv, const char *prefix)
1096 struct rev_info revs;
1097 struct patch_ids ids;
1098 struct commit *commit;
1099 struct commit_list *list = NULL;
1100 const char *upstream;
1101 const char *head = "HEAD";
1102 const char *limit = NULL;
1103 int verbose = 0;
1105 if (argc > 1 && !strcmp(argv[1], "-v")) {
1106 verbose = 1;
1107 argc--;
1108 argv++;
1111 switch (argc) {
1112 case 4:
1113 limit = argv[3];
1114 /* FALLTHROUGH */
1115 case 3:
1116 head = argv[2];
1117 /* FALLTHROUGH */
1118 case 2:
1119 upstream = argv[1];
1120 break;
1121 default:
1122 usage(cherry_usage);
1125 init_revisions(&revs, prefix);
1126 revs.diff = 1;
1127 revs.combine_merges = 0;
1128 revs.ignore_merges = 1;
1129 DIFF_OPT_SET(&revs.diffopt, RECURSIVE);
1131 if (add_pending_commit(head, &revs, 0))
1132 die("Unknown commit %s", head);
1133 if (add_pending_commit(upstream, &revs, UNINTERESTING))
1134 die("Unknown commit %s", upstream);
1136 /* Don't say anything if head and upstream are the same. */
1137 if (revs.pending.nr == 2) {
1138 struct object_array_entry *o = revs.pending.objects;
1139 if (hashcmp(o[0].item->sha1, o[1].item->sha1) == 0)
1140 return 0;
1143 get_patch_ids(&revs, &ids, prefix);
1145 if (limit && add_pending_commit(limit, &revs, UNINTERESTING))
1146 die("Unknown commit %s", limit);
1148 /* reverse the list of commits */
1149 if (prepare_revision_walk(&revs))
1150 die("revision walk setup failed");
1151 while ((commit = get_revision(&revs)) != NULL) {
1152 /* ignore merges */
1153 if (commit->parents && commit->parents->next)
1154 continue;
1156 commit_list_insert(commit, &list);
1159 while (list) {
1160 char sign = '+';
1162 commit = list->item;
1163 if (has_commit_patch_id(commit, &ids))
1164 sign = '-';
1166 if (verbose) {
1167 struct strbuf buf;
1168 strbuf_init(&buf, 0);
1169 pretty_print_commit(CMIT_FMT_ONELINE, commit,
1170 &buf, 0, NULL, NULL, 0, 0);
1171 printf("%c %s %s\n", sign,
1172 sha1_to_hex(commit->object.sha1), buf.buf);
1173 strbuf_release(&buf);
1175 else {
1176 printf("%c %s\n", sign,
1177 sha1_to_hex(commit->object.sha1));
1180 list = list->next;
1183 free_patch_ids(&ids);
1184 return 0;