pack-objects: Add runtime detection of online CPU's
[git/gitweb.git] / builtin-log.c
blobc67d63cb1cf588877fa10a7546757f2cd249c5c6
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"
19 static int default_show_root = 1;
20 static const char *fmt_patch_subject_prefix = "PATCH";
22 static void add_name_decoration(const char *prefix, const char *name, struct object *obj)
24 int plen = strlen(prefix);
25 int nlen = strlen(name);
26 struct name_decoration *res = xmalloc(sizeof(struct name_decoration) + plen + nlen);
27 memcpy(res->name, prefix, plen);
28 memcpy(res->name + plen, name, nlen + 1);
29 res->next = add_decoration(&name_decoration, obj, res);
32 static int add_ref_decoration(const char *refname, const unsigned char *sha1, int flags, void *cb_data)
34 struct object *obj = parse_object(sha1);
35 if (!obj)
36 return 0;
37 add_name_decoration("", refname, obj);
38 while (obj->type == OBJ_TAG) {
39 obj = ((struct tag *)obj)->tagged;
40 if (!obj)
41 break;
42 add_name_decoration("tag: ", refname, obj);
44 return 0;
47 static void cmd_log_init(int argc, const char **argv, const char *prefix,
48 struct rev_info *rev)
50 int i;
51 int decorate = 0;
53 rev->abbrev = DEFAULT_ABBREV;
54 rev->commit_format = CMIT_FMT_DEFAULT;
55 rev->verbose_header = 1;
56 DIFF_OPT_SET(&rev->diffopt, RECURSIVE);
57 rev->show_root_diff = default_show_root;
58 rev->subject_prefix = fmt_patch_subject_prefix;
59 argc = setup_revisions(argc, argv, rev, "HEAD");
60 if (rev->diffopt.pickaxe || rev->diffopt.filter)
61 rev->always_show_header = 0;
62 if (DIFF_OPT_TST(&rev->diffopt, FOLLOW_RENAMES)) {
63 rev->always_show_header = 0;
64 if (rev->diffopt.nr_paths != 1)
65 usage("git logs can only follow renames on one pathname at a time");
67 for (i = 1; i < argc; i++) {
68 const char *arg = argv[i];
69 if (!strcmp(arg, "--decorate")) {
70 if (!decorate)
71 for_each_ref(add_ref_decoration, NULL);
72 decorate = 1;
73 } else
74 die("unrecognized argument: %s", arg);
79 * This gives a rough estimate for how many commits we
80 * will print out in the list.
82 static int estimate_commit_count(struct rev_info *rev, struct commit_list *list)
84 int n = 0;
86 while (list) {
87 struct commit *commit = list->item;
88 unsigned int flags = commit->object.flags;
89 list = list->next;
90 if (!(flags & (TREESAME | UNINTERESTING)))
91 n++;
93 return n;
96 static void show_early_header(struct rev_info *rev, const char *stage, int nr)
98 if (rev->shown_one) {
99 rev->shown_one = 0;
100 if (rev->commit_format != CMIT_FMT_ONELINE)
101 putchar(rev->diffopt.line_termination);
103 printf("Final output: %d %s\n", nr, stage);
106 struct itimerval early_output_timer;
108 static void log_show_early(struct rev_info *revs, struct commit_list *list)
110 int i = revs->early_output;
111 int show_header = 1;
113 sort_in_topological_order(&list, revs->lifo);
114 while (list && i) {
115 struct commit *commit = list->item;
116 switch (simplify_commit(revs, commit)) {
117 case commit_show:
118 if (show_header) {
119 int n = estimate_commit_count(revs, list);
120 show_early_header(revs, "incomplete", n);
121 show_header = 0;
123 log_tree_commit(revs, commit);
124 i--;
125 break;
126 case commit_ignore:
127 break;
128 case commit_error:
129 return;
131 list = list->next;
134 /* Did we already get enough commits for the early output? */
135 if (!i)
136 return;
139 * ..if no, then repeat it twice a second until we
140 * do.
142 * NOTE! We don't use "it_interval", because if the
143 * reader isn't listening, we want our output to be
144 * throttled by the writing, and not have the timer
145 * trigger every second even if we're blocked on a
146 * reader!
148 early_output_timer.it_value.tv_sec = 0;
149 early_output_timer.it_value.tv_usec = 500000;
150 setitimer(ITIMER_REAL, &early_output_timer, NULL);
153 static void early_output(int signal)
155 show_early_output = log_show_early;
158 static void setup_early_output(struct rev_info *rev)
160 struct sigaction sa;
163 * Set up the signal handler, minimally intrusively:
164 * we only set a single volatile integer word (not
165 * using sigatomic_t - trying to avoid unnecessary
166 * system dependencies and headers), and using
167 * SA_RESTART.
169 memset(&sa, 0, sizeof(sa));
170 sa.sa_handler = early_output;
171 sigemptyset(&sa.sa_mask);
172 sa.sa_flags = SA_RESTART;
173 sigaction(SIGALRM, &sa, NULL);
176 * If we can get the whole output in less than a
177 * tenth of a second, don't even bother doing the
178 * early-output thing..
180 * This is a one-time-only trigger.
182 early_output_timer.it_value.tv_sec = 0;
183 early_output_timer.it_value.tv_usec = 100000;
184 setitimer(ITIMER_REAL, &early_output_timer, NULL);
187 static void finish_early_output(struct rev_info *rev)
189 int n = estimate_commit_count(rev, rev->commits);
190 signal(SIGALRM, SIG_IGN);
191 show_early_header(rev, "done", n);
194 static int cmd_log_walk(struct rev_info *rev)
196 struct commit *commit;
198 if (rev->early_output)
199 setup_early_output(rev);
201 if (prepare_revision_walk(rev))
202 die("revision walk setup failed");
204 if (rev->early_output)
205 finish_early_output(rev);
207 while ((commit = get_revision(rev)) != NULL) {
208 log_tree_commit(rev, commit);
209 if (!rev->reflog_info) {
210 /* we allow cycles in reflog ancestry */
211 free(commit->buffer);
212 commit->buffer = NULL;
214 free_commit_list(commit->parents);
215 commit->parents = NULL;
217 return 0;
220 static int git_log_config(const char *var, const char *value)
222 if (!strcmp(var, "format.subjectprefix")) {
223 if (!value)
224 config_error_nonbool(var);
225 fmt_patch_subject_prefix = xstrdup(value);
226 return 0;
228 if (!strcmp(var, "log.showroot")) {
229 default_show_root = git_config_bool(var, value);
230 return 0;
232 return git_diff_ui_config(var, value);
235 int cmd_whatchanged(int argc, const char **argv, const char *prefix)
237 struct rev_info rev;
239 git_config(git_log_config);
241 if (diff_use_color_default == -1)
242 diff_use_color_default = git_use_color_default;
244 init_revisions(&rev, prefix);
245 rev.diff = 1;
246 rev.simplify_history = 0;
247 cmd_log_init(argc, argv, prefix, &rev);
248 if (!rev.diffopt.output_format)
249 rev.diffopt.output_format = DIFF_FORMAT_RAW;
250 return cmd_log_walk(&rev);
253 static void show_tagger(char *buf, int len, struct rev_info *rev)
255 char *email_end, *p;
256 unsigned long date;
257 int tz;
259 email_end = memchr(buf, '>', len);
260 if (!email_end)
261 return;
262 p = ++email_end;
263 while (isspace(*p))
264 p++;
265 date = strtoul(p, &p, 10);
266 while (isspace(*p))
267 p++;
268 tz = (int)strtol(p, NULL, 10);
269 printf("Tagger: %.*s\nDate: %s\n", (int)(email_end - buf), buf,
270 show_date(date, tz, rev->date_mode));
273 static int show_object(const unsigned char *sha1, int show_tag_object,
274 struct rev_info *rev)
276 unsigned long size;
277 enum object_type type;
278 char *buf = read_sha1_file(sha1, &type, &size);
279 int offset = 0;
281 if (!buf)
282 return error("Could not read object %s", sha1_to_hex(sha1));
284 if (show_tag_object)
285 while (offset < size && buf[offset] != '\n') {
286 int new_offset = offset + 1;
287 while (new_offset < size && buf[new_offset++] != '\n')
288 ; /* do nothing */
289 if (!prefixcmp(buf + offset, "tagger "))
290 show_tagger(buf + offset + 7,
291 new_offset - offset - 7, rev);
292 offset = new_offset;
295 if (offset < size)
296 fwrite(buf + offset, size - offset, 1, stdout);
297 free(buf);
298 return 0;
301 static int show_tree_object(const unsigned char *sha1,
302 const char *base, int baselen,
303 const char *pathname, unsigned mode, int stage)
305 printf("%s%s\n", pathname, S_ISDIR(mode) ? "/" : "");
306 return 0;
309 int cmd_show(int argc, const char **argv, const char *prefix)
311 struct rev_info rev;
312 struct object_array_entry *objects;
313 int i, count, ret = 0;
315 git_config(git_log_config);
317 if (diff_use_color_default == -1)
318 diff_use_color_default = git_use_color_default;
320 init_revisions(&rev, prefix);
321 rev.diff = 1;
322 rev.combine_merges = 1;
323 rev.dense_combined_merges = 1;
324 rev.always_show_header = 1;
325 rev.ignore_merges = 0;
326 rev.no_walk = 1;
327 cmd_log_init(argc, argv, prefix, &rev);
329 count = rev.pending.nr;
330 objects = rev.pending.objects;
331 for (i = 0; i < count && !ret; i++) {
332 struct object *o = objects[i].item;
333 const char *name = objects[i].name;
334 switch (o->type) {
335 case OBJ_BLOB:
336 ret = show_object(o->sha1, 0, NULL);
337 break;
338 case OBJ_TAG: {
339 struct tag *t = (struct tag *)o;
341 printf("%stag %s%s\n",
342 diff_get_color_opt(&rev.diffopt, DIFF_COMMIT),
343 t->tag,
344 diff_get_color_opt(&rev.diffopt, DIFF_RESET));
345 ret = show_object(o->sha1, 1, &rev);
346 objects[i].item = (struct object *)t->tagged;
347 i--;
348 break;
350 case OBJ_TREE:
351 printf("%stree %s%s\n\n",
352 diff_get_color_opt(&rev.diffopt, DIFF_COMMIT),
353 name,
354 diff_get_color_opt(&rev.diffopt, DIFF_RESET));
355 read_tree_recursive((struct tree *)o, "", 0, 0, NULL,
356 show_tree_object);
357 break;
358 case OBJ_COMMIT:
359 rev.pending.nr = rev.pending.alloc = 0;
360 rev.pending.objects = NULL;
361 add_object_array(o, name, &rev.pending);
362 ret = cmd_log_walk(&rev);
363 break;
364 default:
365 ret = error("Unknown type: %d", o->type);
368 free(objects);
369 return ret;
373 * This is equivalent to "git log -g --abbrev-commit --pretty=oneline"
375 int cmd_log_reflog(int argc, const char **argv, const char *prefix)
377 struct rev_info rev;
379 git_config(git_log_config);
381 if (diff_use_color_default == -1)
382 diff_use_color_default = git_use_color_default;
384 init_revisions(&rev, prefix);
385 init_reflog_walk(&rev.reflog_info);
386 rev.abbrev_commit = 1;
387 rev.verbose_header = 1;
388 cmd_log_init(argc, argv, prefix, &rev);
391 * This means that we override whatever commit format the user gave
392 * on the cmd line. Sad, but cmd_log_init() currently doesn't
393 * allow us to set a different default.
395 rev.commit_format = CMIT_FMT_ONELINE;
396 rev.always_show_header = 1;
399 * We get called through "git reflog", so unlike the other log
400 * routines, we need to set up our pager manually..
402 setup_pager();
404 return cmd_log_walk(&rev);
407 int cmd_log(int argc, const char **argv, const char *prefix)
409 struct rev_info rev;
411 git_config(git_log_config);
413 if (diff_use_color_default == -1)
414 diff_use_color_default = git_use_color_default;
416 init_revisions(&rev, prefix);
417 rev.always_show_header = 1;
418 cmd_log_init(argc, argv, prefix, &rev);
419 return cmd_log_walk(&rev);
422 /* format-patch */
423 #define FORMAT_PATCH_NAME_MAX 64
425 static int istitlechar(char c)
427 return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
428 (c >= '0' && c <= '9') || c == '.' || c == '_';
431 static char *extra_headers = NULL;
432 static int extra_headers_size = 0;
433 static const char *fmt_patch_suffix = ".patch";
434 static int numbered = 0;
435 static int auto_number = 0;
437 static int git_format_config(const char *var, const char *value)
439 if (!strcmp(var, "format.headers")) {
440 int len;
442 if (!value)
443 die("format.headers without value");
444 len = strlen(value);
445 extra_headers_size += len + 1;
446 extra_headers = xrealloc(extra_headers, extra_headers_size);
447 extra_headers[extra_headers_size - len - 1] = 0;
448 strcat(extra_headers, value);
449 return 0;
451 if (!strcmp(var, "format.suffix")) {
452 if (!value)
453 return config_error_nonbool(var);
454 fmt_patch_suffix = xstrdup(value);
455 return 0;
457 if (!strcmp(var, "diff.color") || !strcmp(var, "color.diff")) {
458 return 0;
460 if (!strcmp(var, "format.numbered")) {
461 if (value && !strcasecmp(value, "auto")) {
462 auto_number = 1;
463 return 0;
465 numbered = git_config_bool(var, value);
466 return 0;
469 return git_log_config(var, value);
473 static FILE *realstdout = NULL;
474 static const char *output_directory = NULL;
476 static int reopen_stdout(struct commit *commit, int nr, int keep_subject,
477 int numbered_files)
479 char filename[PATH_MAX];
480 char *sol;
481 int len = 0;
482 int suffix_len = strlen(fmt_patch_suffix) + 1;
484 if (output_directory) {
485 if (strlen(output_directory) >=
486 sizeof(filename) - FORMAT_PATCH_NAME_MAX - suffix_len)
487 return error("name of output directory is too long");
488 strlcpy(filename, output_directory, sizeof(filename) - suffix_len);
489 len = strlen(filename);
490 if (filename[len - 1] != '/')
491 filename[len++] = '/';
494 if (numbered_files) {
495 sprintf(filename + len, "%d", nr);
496 len = strlen(filename);
498 } else {
499 sprintf(filename + len, "%04d", nr);
500 len = strlen(filename);
502 sol = strstr(commit->buffer, "\n\n");
503 if (sol) {
504 int j, space = 1;
506 sol += 2;
507 /* strip [PATCH] or [PATCH blabla] */
508 if (!keep_subject && !prefixcmp(sol, "[PATCH")) {
509 char *eos = strchr(sol + 6, ']');
510 if (eos) {
511 while (isspace(*eos))
512 eos++;
513 sol = eos;
517 for (j = 0;
518 j < FORMAT_PATCH_NAME_MAX - suffix_len - 5 &&
519 len < sizeof(filename) - suffix_len &&
520 sol[j] && sol[j] != '\n';
521 j++) {
522 if (istitlechar(sol[j])) {
523 if (space) {
524 filename[len++] = '-';
525 space = 0;
527 filename[len++] = sol[j];
528 if (sol[j] == '.')
529 while (sol[j + 1] == '.')
530 j++;
531 } else
532 space = 1;
534 while (filename[len - 1] == '.'
535 || filename[len - 1] == '-')
536 len--;
537 filename[len] = 0;
539 if (len + suffix_len >= sizeof(filename))
540 return error("Patch pathname too long");
541 strcpy(filename + len, fmt_patch_suffix);
544 fprintf(realstdout, "%s\n", filename);
545 if (freopen(filename, "w", stdout) == NULL)
546 return error("Cannot open patch file %s",filename);
548 return 0;
551 static void get_patch_ids(struct rev_info *rev, struct patch_ids *ids, const char *prefix)
553 struct rev_info check_rev;
554 struct commit *commit;
555 struct object *o1, *o2;
556 unsigned flags1, flags2;
558 if (rev->pending.nr != 2)
559 die("Need exactly one range.");
561 o1 = rev->pending.objects[0].item;
562 flags1 = o1->flags;
563 o2 = rev->pending.objects[1].item;
564 flags2 = o2->flags;
566 if ((flags1 & UNINTERESTING) == (flags2 & UNINTERESTING))
567 die("Not a range.");
569 init_patch_ids(ids);
571 /* given a range a..b get all patch ids for b..a */
572 init_revisions(&check_rev, prefix);
573 o1->flags ^= UNINTERESTING;
574 o2->flags ^= UNINTERESTING;
575 add_pending_object(&check_rev, o1, "o1");
576 add_pending_object(&check_rev, o2, "o2");
577 if (prepare_revision_walk(&check_rev))
578 die("revision walk setup failed");
580 while ((commit = get_revision(&check_rev)) != NULL) {
581 /* ignore merges */
582 if (commit->parents && commit->parents->next)
583 continue;
585 add_commit_patch_id(commit, ids);
588 /* reset for next revision walk */
589 clear_commit_marks((struct commit *)o1,
590 SEEN | UNINTERESTING | SHOWN | ADDED);
591 clear_commit_marks((struct commit *)o2,
592 SEEN | UNINTERESTING | SHOWN | ADDED);
593 o1->flags = flags1;
594 o2->flags = flags2;
597 static void gen_message_id(char *dest, unsigned int length, char *base)
599 const char *committer = git_committer_info(IDENT_WARN_ON_NO_NAME);
600 const char *email_start = strrchr(committer, '<');
601 const char *email_end = strrchr(committer, '>');
602 if(!email_start || !email_end || email_start > email_end - 1)
603 die("Could not extract email from committer identity.");
604 snprintf(dest, length, "%s.%lu.git.%.*s", base,
605 (unsigned long) time(NULL),
606 (int)(email_end - email_start - 1), email_start + 1);
609 static const char *clean_message_id(const char *msg_id)
611 char ch;
612 const char *a, *z, *m;
614 m = msg_id;
615 while ((ch = *m) && (isspace(ch) || (ch == '<')))
616 m++;
617 a = m;
618 z = NULL;
619 while ((ch = *m)) {
620 if (!isspace(ch) && (ch != '>'))
621 z = m;
622 m++;
624 if (!z)
625 die("insane in-reply-to: %s", msg_id);
626 if (++z == m)
627 return a;
628 return xmemdupz(a, z - a);
631 int cmd_format_patch(int argc, const char **argv, const char *prefix)
633 struct commit *commit;
634 struct commit **list = NULL;
635 struct rev_info rev;
636 int nr = 0, total, i, j;
637 int use_stdout = 0;
638 int start_number = -1;
639 int keep_subject = 0;
640 int numbered_files = 0; /* _just_ numbers */
641 int subject_prefix = 0;
642 int ignore_if_in_upstream = 0;
643 int thread = 0;
644 const char *in_reply_to = NULL;
645 struct patch_ids ids;
646 char *add_signoff = NULL;
647 char message_id[1024];
648 char ref_message_id[1024];
650 git_config(git_format_config);
651 init_revisions(&rev, prefix);
652 rev.commit_format = CMIT_FMT_EMAIL;
653 rev.verbose_header = 1;
654 rev.diff = 1;
655 rev.combine_merges = 0;
656 rev.ignore_merges = 1;
657 rev.diffopt.msg_sep = "";
658 DIFF_OPT_SET(&rev.diffopt, RECURSIVE);
660 rev.subject_prefix = fmt_patch_subject_prefix;
661 rev.extra_headers = extra_headers;
664 * Parse the arguments before setup_revisions(), or something
665 * like "git format-patch -o a123 HEAD^.." may fail; a123 is
666 * possibly a valid SHA1.
668 for (i = 1, j = 1; i < argc; i++) {
669 if (!strcmp(argv[i], "--stdout"))
670 use_stdout = 1;
671 else if (!strcmp(argv[i], "-n") ||
672 !strcmp(argv[i], "--numbered"))
673 numbered = 1;
674 else if (!strcmp(argv[i], "-N") ||
675 !strcmp(argv[i], "--no-numbered")) {
676 numbered = 0;
677 auto_number = 0;
679 else if (!prefixcmp(argv[i], "--start-number="))
680 start_number = strtol(argv[i] + 15, NULL, 10);
681 else if (!strcmp(argv[i], "--numbered-files"))
682 numbered_files = 1;
683 else if (!strcmp(argv[i], "--start-number")) {
684 i++;
685 if (i == argc)
686 die("Need a number for --start-number");
687 start_number = strtol(argv[i], NULL, 10);
689 else if (!strcmp(argv[i], "-k") ||
690 !strcmp(argv[i], "--keep-subject")) {
691 keep_subject = 1;
692 rev.total = -1;
694 else if (!strcmp(argv[i], "--output-directory") ||
695 !strcmp(argv[i], "-o")) {
696 i++;
697 if (argc <= i)
698 die("Which directory?");
699 if (output_directory)
700 die("Two output directories?");
701 output_directory = argv[i];
703 else if (!strcmp(argv[i], "--signoff") ||
704 !strcmp(argv[i], "-s")) {
705 const char *committer;
706 const char *endpos;
707 committer = git_committer_info(IDENT_ERROR_ON_NO_NAME);
708 endpos = strchr(committer, '>');
709 if (!endpos)
710 die("bogos committer info %s\n", committer);
711 add_signoff = xmemdupz(committer, endpos - committer + 1);
713 else if (!strcmp(argv[i], "--attach")) {
714 rev.mime_boundary = git_version_string;
715 rev.no_inline = 1;
717 else if (!prefixcmp(argv[i], "--attach=")) {
718 rev.mime_boundary = argv[i] + 9;
719 rev.no_inline = 1;
721 else if (!strcmp(argv[i], "--inline")) {
722 rev.mime_boundary = git_version_string;
723 rev.no_inline = 0;
725 else if (!prefixcmp(argv[i], "--inline=")) {
726 rev.mime_boundary = argv[i] + 9;
727 rev.no_inline = 0;
729 else if (!strcmp(argv[i], "--ignore-if-in-upstream"))
730 ignore_if_in_upstream = 1;
731 else if (!strcmp(argv[i], "--thread"))
732 thread = 1;
733 else if (!prefixcmp(argv[i], "--in-reply-to="))
734 in_reply_to = argv[i] + 14;
735 else if (!strcmp(argv[i], "--in-reply-to")) {
736 i++;
737 if (i == argc)
738 die("Need a Message-Id for --in-reply-to");
739 in_reply_to = argv[i];
740 } else if (!prefixcmp(argv[i], "--subject-prefix=")) {
741 subject_prefix = 1;
742 rev.subject_prefix = argv[i] + 17;
743 } else if (!prefixcmp(argv[i], "--suffix="))
744 fmt_patch_suffix = argv[i] + 9;
745 else
746 argv[j++] = argv[i];
748 argc = j;
750 if (start_number < 0)
751 start_number = 1;
752 if (numbered && keep_subject)
753 die ("-n and -k are mutually exclusive.");
754 if (keep_subject && subject_prefix)
755 die ("--subject-prefix and -k are mutually exclusive.");
756 if (numbered_files && use_stdout)
757 die ("--numbered-files and --stdout are mutually exclusive.");
759 argc = setup_revisions(argc, argv, &rev, "HEAD");
760 if (argc > 1)
761 die ("unrecognized argument: %s", argv[1]);
763 if (!rev.diffopt.output_format)
764 rev.diffopt.output_format = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_SUMMARY | DIFF_FORMAT_PATCH;
766 if (!DIFF_OPT_TST(&rev.diffopt, TEXT))
767 DIFF_OPT_SET(&rev.diffopt, BINARY);
769 if (!output_directory && !use_stdout)
770 output_directory = prefix;
772 if (output_directory) {
773 if (use_stdout)
774 die("standard output, or directory, which one?");
775 if (mkdir(output_directory, 0777) < 0 && errno != EEXIST)
776 die("Could not create directory %s",
777 output_directory);
780 if (rev.pending.nr == 1) {
781 if (rev.max_count < 0 && !rev.show_root_diff) {
783 * This is traditional behaviour of "git format-patch
784 * origin" that prepares what the origin side still
785 * does not have.
787 rev.pending.objects[0].item->flags |= UNINTERESTING;
788 add_head_to_pending(&rev);
791 * Otherwise, it is "format-patch -22 HEAD", and/or
792 * "format-patch --root HEAD". The user wants
793 * get_revision() to do the usual traversal.
797 if (ignore_if_in_upstream)
798 get_patch_ids(&rev, &ids, prefix);
800 if (!use_stdout)
801 realstdout = xfdopen(xdup(1), "w");
803 if (prepare_revision_walk(&rev))
804 die("revision walk setup failed");
805 while ((commit = get_revision(&rev)) != NULL) {
806 /* ignore merges */
807 if (commit->parents && commit->parents->next)
808 continue;
810 if (ignore_if_in_upstream &&
811 has_commit_patch_id(commit, &ids))
812 continue;
814 nr++;
815 list = xrealloc(list, nr * sizeof(list[0]));
816 list[nr - 1] = commit;
818 total = nr;
819 if (!keep_subject && auto_number && total > 1)
820 numbered = 1;
821 if (numbered)
822 rev.total = total + start_number - 1;
823 rev.add_signoff = add_signoff;
824 if (in_reply_to)
825 rev.ref_message_id = clean_message_id(in_reply_to);
826 while (0 <= --nr) {
827 int shown;
828 commit = list[nr];
829 rev.nr = total - nr + (start_number - 1);
830 /* Make the second and subsequent mails replies to the first */
831 if (thread) {
832 if (nr == (total - 2)) {
833 strncpy(ref_message_id, message_id,
834 sizeof(ref_message_id));
835 ref_message_id[sizeof(ref_message_id)-1]='\0';
836 rev.ref_message_id = ref_message_id;
838 gen_message_id(message_id, sizeof(message_id),
839 sha1_to_hex(commit->object.sha1));
840 rev.message_id = message_id;
842 if (!use_stdout)
843 if (reopen_stdout(commit, rev.nr, keep_subject,
844 numbered_files))
845 die("Failed to create output files");
846 shown = log_tree_commit(&rev, commit);
847 free(commit->buffer);
848 commit->buffer = NULL;
850 /* We put one extra blank line between formatted
851 * patches and this flag is used by log-tree code
852 * to see if it needs to emit a LF before showing
853 * the log; when using one file per patch, we do
854 * not want the extra blank line.
856 if (!use_stdout)
857 rev.shown_one = 0;
858 if (shown) {
859 if (rev.mime_boundary)
860 printf("\n--%s%s--\n\n\n",
861 mime_boundary_leader,
862 rev.mime_boundary);
863 else
864 printf("-- \n%s\n\n", git_version_string);
866 if (!use_stdout)
867 fclose(stdout);
869 free(list);
870 if (ignore_if_in_upstream)
871 free_patch_ids(&ids);
872 return 0;
875 static int add_pending_commit(const char *arg, struct rev_info *revs, int flags)
877 unsigned char sha1[20];
878 if (get_sha1(arg, sha1) == 0) {
879 struct commit *commit = lookup_commit_reference(sha1);
880 if (commit) {
881 commit->object.flags |= flags;
882 add_pending_object(revs, &commit->object, arg);
883 return 0;
886 return -1;
889 static const char cherry_usage[] =
890 "git-cherry [-v] <upstream> [<head>] [<limit>]";
891 int cmd_cherry(int argc, const char **argv, const char *prefix)
893 struct rev_info revs;
894 struct patch_ids ids;
895 struct commit *commit;
896 struct commit_list *list = NULL;
897 const char *upstream;
898 const char *head = "HEAD";
899 const char *limit = NULL;
900 int verbose = 0;
902 if (argc > 1 && !strcmp(argv[1], "-v")) {
903 verbose = 1;
904 argc--;
905 argv++;
908 switch (argc) {
909 case 4:
910 limit = argv[3];
911 /* FALLTHROUGH */
912 case 3:
913 head = argv[2];
914 /* FALLTHROUGH */
915 case 2:
916 upstream = argv[1];
917 break;
918 default:
919 usage(cherry_usage);
922 init_revisions(&revs, prefix);
923 revs.diff = 1;
924 revs.combine_merges = 0;
925 revs.ignore_merges = 1;
926 DIFF_OPT_SET(&revs.diffopt, RECURSIVE);
928 if (add_pending_commit(head, &revs, 0))
929 die("Unknown commit %s", head);
930 if (add_pending_commit(upstream, &revs, UNINTERESTING))
931 die("Unknown commit %s", upstream);
933 /* Don't say anything if head and upstream are the same. */
934 if (revs.pending.nr == 2) {
935 struct object_array_entry *o = revs.pending.objects;
936 if (hashcmp(o[0].item->sha1, o[1].item->sha1) == 0)
937 return 0;
940 get_patch_ids(&revs, &ids, prefix);
942 if (limit && add_pending_commit(limit, &revs, UNINTERESTING))
943 die("Unknown commit %s", limit);
945 /* reverse the list of commits */
946 if (prepare_revision_walk(&revs))
947 die("revision walk setup failed");
948 while ((commit = get_revision(&revs)) != NULL) {
949 /* ignore merges */
950 if (commit->parents && commit->parents->next)
951 continue;
953 commit_list_insert(commit, &list);
956 while (list) {
957 char sign = '+';
959 commit = list->item;
960 if (has_commit_patch_id(commit, &ids))
961 sign = '-';
963 if (verbose) {
964 struct strbuf buf;
965 strbuf_init(&buf, 0);
966 pretty_print_commit(CMIT_FMT_ONELINE, commit,
967 &buf, 0, NULL, NULL, 0, 0);
968 printf("%c %s %s\n", sign,
969 sha1_to_hex(commit->object.sha1), buf.buf);
970 strbuf_release(&buf);
972 else {
973 printf("%c %s\n", sign,
974 sha1_to_hex(commit->object.sha1));
977 list = list->next;
980 free_patch_ids(&ids);
981 return 0;