2 * Builtin "git log" and related commands (show, whatchanged)
4 * (C) Copyright 2006 Linus Torvalds
15 #include "reflog-walk.h"
16 #include "patch-ids.h"
18 #include "run-command.h"
21 static int default_show_root = 1;
22 static const char *fmt_patch_subject_prefix = "PATCH";
24 static void add_name_decoration(const char *prefix, const char *name, struct object *obj)
26 int plen = strlen(prefix);
27 int nlen = strlen(name);
28 struct name_decoration *res = xmalloc(sizeof(struct name_decoration) + plen + nlen);
29 memcpy(res->name, prefix, plen);
30 memcpy(res->name + plen, name, nlen + 1);
31 res->next = add_decoration(&name_decoration, obj, res);
34 static int add_ref_decoration(const char *refname, const unsigned char *sha1, int flags, void *cb_data)
36 struct object *obj = parse_object(sha1);
39 add_name_decoration("", refname, obj);
40 while (obj->type == OBJ_TAG) {
41 obj = ((struct tag *)obj)->tagged;
44 add_name_decoration("tag: ", refname, obj);
49 static void cmd_log_init(int argc, const char **argv, const char *prefix,
55 rev->abbrev = DEFAULT_ABBREV;
56 rev->commit_format = CMIT_FMT_DEFAULT;
57 rev->verbose_header = 1;
58 DIFF_OPT_SET(&rev->diffopt, RECURSIVE);
59 rev->show_root_diff = default_show_root;
60 rev->subject_prefix = fmt_patch_subject_prefix;
61 argc = setup_revisions(argc, argv, rev, "HEAD");
62 if (rev->diffopt.pickaxe || rev->diffopt.filter)
63 rev->always_show_header = 0;
64 if (DIFF_OPT_TST(&rev->diffopt, FOLLOW_RENAMES)) {
65 rev->always_show_header = 0;
66 if (rev->diffopt.nr_paths != 1)
67 usage("git logs can only follow renames on one pathname at a time");
69 for (i = 1; i < argc; i++) {
70 const char *arg = argv[i];
71 if (!strcmp(arg, "--decorate")) {
73 for_each_ref(add_ref_decoration, NULL);
76 die("unrecognized argument: %s", arg);
81 * This gives a rough estimate for how many commits we
82 * will print out in the list.
84 static int estimate_commit_count(struct rev_info *rev, struct commit_list *list)
89 struct commit *commit = list->item;
90 unsigned int flags = commit->object.flags;
92 if (!(flags & (TREESAME | UNINTERESTING)))
98 static void show_early_header(struct rev_info *rev, const char *stage, int nr)
100 if (rev->shown_one) {
102 if (rev->commit_format != CMIT_FMT_ONELINE)
103 putchar(rev->diffopt.line_termination);
105 printf("Final output: %d %s\n", nr, stage);
108 struct itimerval early_output_timer;
110 static void log_show_early(struct rev_info *revs, struct commit_list *list)
112 int i = revs->early_output;
115 sort_in_topological_order(&list, revs->lifo);
117 struct commit *commit = list->item;
118 switch (simplify_commit(revs, commit)) {
121 int n = estimate_commit_count(revs, list);
122 show_early_header(revs, "incomplete", n);
125 log_tree_commit(revs, commit);
136 /* Did we already get enough commits for the early output? */
141 * ..if no, then repeat it twice a second until we
144 * NOTE! We don't use "it_interval", because if the
145 * reader isn't listening, we want our output to be
146 * throttled by the writing, and not have the timer
147 * trigger every second even if we're blocked on a
150 early_output_timer.it_value.tv_sec = 0;
151 early_output_timer.it_value.tv_usec = 500000;
152 setitimer(ITIMER_REAL, &early_output_timer, NULL);
155 static void early_output(int signal)
157 show_early_output = log_show_early;
160 static void setup_early_output(struct rev_info *rev)
165 * Set up the signal handler, minimally intrusively:
166 * we only set a single volatile integer word (not
167 * using sigatomic_t - trying to avoid unnecessary
168 * system dependencies and headers), and using
171 memset(&sa, 0, sizeof(sa));
172 sa.sa_handler = early_output;
173 sigemptyset(&sa.sa_mask);
174 sa.sa_flags = SA_RESTART;
175 sigaction(SIGALRM, &sa, NULL);
178 * If we can get the whole output in less than a
179 * tenth of a second, don't even bother doing the
180 * early-output thing..
182 * This is a one-time-only trigger.
184 early_output_timer.it_value.tv_sec = 0;
185 early_output_timer.it_value.tv_usec = 100000;
186 setitimer(ITIMER_REAL, &early_output_timer, NULL);
189 static void finish_early_output(struct rev_info *rev)
191 int n = estimate_commit_count(rev, rev->commits);
192 signal(SIGALRM, SIG_IGN);
193 show_early_header(rev, "done", n);
196 static int cmd_log_walk(struct rev_info *rev)
198 struct commit *commit;
200 if (rev->early_output)
201 setup_early_output(rev);
203 if (prepare_revision_walk(rev))
204 die("revision walk setup failed");
206 if (rev->early_output)
207 finish_early_output(rev);
209 while ((commit = get_revision(rev)) != NULL) {
210 log_tree_commit(rev, commit);
211 if (!rev->reflog_info) {
212 /* we allow cycles in reflog ancestry */
213 free(commit->buffer);
214 commit->buffer = NULL;
216 free_commit_list(commit->parents);
217 commit->parents = NULL;
222 static int git_log_config(const char *var, const char *value)
224 if (!strcmp(var, "format.subjectprefix")) {
226 config_error_nonbool(var);
227 fmt_patch_subject_prefix = xstrdup(value);
230 if (!strcmp(var, "log.showroot")) {
231 default_show_root = git_config_bool(var, value);
234 return git_diff_ui_config(var, value);
237 int cmd_whatchanged(int argc, const char **argv, const char *prefix)
241 git_config(git_log_config);
243 if (diff_use_color_default == -1)
244 diff_use_color_default = git_use_color_default;
246 init_revisions(&rev, prefix);
248 rev.simplify_history = 0;
249 cmd_log_init(argc, argv, prefix, &rev);
250 if (!rev.diffopt.output_format)
251 rev.diffopt.output_format = DIFF_FORMAT_RAW;
252 return cmd_log_walk(&rev);
255 static void show_tagger(char *buf, int len, struct rev_info *rev)
261 email_end = memchr(buf, '>', len);
267 date = strtoul(p, &p, 10);
270 tz = (int)strtol(p, NULL, 10);
271 printf("Tagger: %.*s\nDate: %s\n", (int)(email_end - buf), buf,
272 show_date(date, tz, rev->date_mode));
275 static int show_object(const unsigned char *sha1, int show_tag_object,
276 struct rev_info *rev)
279 enum object_type type;
280 char *buf = read_sha1_file(sha1, &type, &size);
284 return error("Could not read object %s", sha1_to_hex(sha1));
287 while (offset < size && buf[offset] != '\n') {
288 int new_offset = offset + 1;
289 while (new_offset < size && buf[new_offset++] != '\n')
291 if (!prefixcmp(buf + offset, "tagger "))
292 show_tagger(buf + offset + 7,
293 new_offset - offset - 7, rev);
298 fwrite(buf + offset, size - offset, 1, stdout);
303 static int show_tree_object(const unsigned char *sha1,
304 const char *base, int baselen,
305 const char *pathname, unsigned mode, int stage)
307 printf("%s%s\n", pathname, S_ISDIR(mode) ? "/" : "");
311 int cmd_show(int argc, const char **argv, const char *prefix)
314 struct object_array_entry *objects;
315 int i, count, ret = 0;
317 git_config(git_log_config);
319 if (diff_use_color_default == -1)
320 diff_use_color_default = git_use_color_default;
322 init_revisions(&rev, prefix);
324 rev.combine_merges = 1;
325 rev.dense_combined_merges = 1;
326 rev.always_show_header = 1;
327 rev.ignore_merges = 0;
329 cmd_log_init(argc, argv, prefix, &rev);
331 count = rev.pending.nr;
332 objects = rev.pending.objects;
333 for (i = 0; i < count && !ret; i++) {
334 struct object *o = objects[i].item;
335 const char *name = objects[i].name;
338 ret = show_object(o->sha1, 0, NULL);
341 struct tag *t = (struct tag *)o;
343 printf("%stag %s%s\n",
344 diff_get_color_opt(&rev.diffopt, DIFF_COMMIT),
346 diff_get_color_opt(&rev.diffopt, DIFF_RESET));
347 ret = show_object(o->sha1, 1, &rev);
348 objects[i].item = (struct object *)t->tagged;
353 printf("%stree %s%s\n\n",
354 diff_get_color_opt(&rev.diffopt, DIFF_COMMIT),
356 diff_get_color_opt(&rev.diffopt, DIFF_RESET));
357 read_tree_recursive((struct tree *)o, "", 0, 0, NULL,
361 rev.pending.nr = rev.pending.alloc = 0;
362 rev.pending.objects = NULL;
363 add_object_array(o, name, &rev.pending);
364 ret = cmd_log_walk(&rev);
367 ret = error("Unknown type: %d", o->type);
375 * This is equivalent to "git log -g --abbrev-commit --pretty=oneline"
377 int cmd_log_reflog(int argc, const char **argv, const char *prefix)
381 git_config(git_log_config);
383 if (diff_use_color_default == -1)
384 diff_use_color_default = git_use_color_default;
386 init_revisions(&rev, prefix);
387 init_reflog_walk(&rev.reflog_info);
388 rev.abbrev_commit = 1;
389 rev.verbose_header = 1;
390 cmd_log_init(argc, argv, prefix, &rev);
393 * This means that we override whatever commit format the user gave
394 * on the cmd line. Sad, but cmd_log_init() currently doesn't
395 * allow us to set a different default.
397 rev.commit_format = CMIT_FMT_ONELINE;
398 rev.always_show_header = 1;
401 * We get called through "git reflog", so unlike the other log
402 * routines, we need to set up our pager manually..
406 return cmd_log_walk(&rev);
409 int cmd_log(int argc, const char **argv, const char *prefix)
413 git_config(git_log_config);
415 if (diff_use_color_default == -1)
416 diff_use_color_default = git_use_color_default;
418 init_revisions(&rev, prefix);
419 rev.always_show_header = 1;
420 cmd_log_init(argc, argv, prefix, &rev);
421 return cmd_log_walk(&rev);
425 #define FORMAT_PATCH_NAME_MAX 64
427 static int istitlechar(char c)
429 return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
430 (c >= '0' && c <= '9') || c == '.' || c == '_';
433 static const char *fmt_patch_suffix = ".patch";
434 static int numbered = 0;
435 static int auto_number = 0;
437 static char **extra_hdr;
438 static int extra_hdr_nr;
439 static int extra_hdr_alloc;
441 static char **extra_to;
442 static int extra_to_nr;
443 static int extra_to_alloc;
445 static char **extra_cc;
446 static int extra_cc_nr;
447 static int extra_cc_alloc;
449 static void add_header(const char *value)
451 int len = strlen(value);
452 while (value[len - 1] == '\n')
454 if (!strncasecmp(value, "to: ", 4)) {
455 ALLOC_GROW(extra_to, extra_to_nr + 1, extra_to_alloc);
456 extra_to[extra_to_nr++] = xstrndup(value + 4, len - 4);
459 if (!strncasecmp(value, "cc: ", 4)) {
460 ALLOC_GROW(extra_cc, extra_cc_nr + 1, extra_cc_alloc);
461 extra_cc[extra_cc_nr++] = xstrndup(value + 4, len - 4);
464 ALLOC_GROW(extra_hdr, extra_hdr_nr + 1, extra_hdr_alloc);
465 extra_hdr[extra_hdr_nr++] = xstrndup(value, len);
468 static int git_format_config(const char *var, const char *value)
470 if (!strcmp(var, "format.headers")) {
472 die("format.headers without value");
476 if (!strcmp(var, "format.suffix")) {
478 return config_error_nonbool(var);
479 fmt_patch_suffix = xstrdup(value);
482 if (!strcmp(var, "diff.color") || !strcmp(var, "color.diff")) {
485 if (!strcmp(var, "format.numbered")) {
486 if (value && !strcasecmp(value, "auto")) {
490 numbered = git_config_bool(var, value);
494 return git_log_config(var, value);
498 static const char *get_oneline_for_filename(struct commit *commit,
501 static char filename[PATH_MAX];
504 int suffix_len = strlen(fmt_patch_suffix) + 1;
506 sol = strstr(commit->buffer, "\n\n");
513 /* strip [PATCH] or [PATCH blabla] */
514 if (!keep_subject && !prefixcmp(sol, "[PATCH")) {
515 char *eos = strchr(sol + 6, ']');
517 while (isspace(*eos))
524 j < FORMAT_PATCH_NAME_MAX - suffix_len - 5 &&
525 len < sizeof(filename) - suffix_len &&
526 sol[j] && sol[j] != '\n';
528 if (istitlechar(sol[j])) {
530 filename[len++] = '-';
533 filename[len++] = sol[j];
535 while (sol[j + 1] == '.')
540 while (filename[len - 1] == '.'
541 || filename[len - 1] == '-')
543 filename[len] = '\0';
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];
555 int suffix_len = strlen(fmt_patch_suffix) + 1;
557 if (output_directory) {
558 len = snprintf(filename, sizeof(filename), "%s",
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++] = '/';
568 len += sprintf(filename + len, "%d", nr);
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);
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;
595 o2 = rev->pending.objects[1].item;
598 if ((flags1 & UNINTERESTING) == (flags2 & UNINTERESTING))
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) {
614 if (commit->parents && commit->parents->next)
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);
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, '>');
635 if (!email_start || !email_end || email_start > email_end - 1)
636 die("Could not extract email from committer identity.");
637 strbuf_init(&buf, 0);
638 strbuf_addf(&buf, "%s.%lu.git.%.*s", base,
639 (unsigned long) time(NULL),
640 (int)(email_end - email_start - 1), email_start + 1);
641 info->message_id = strbuf_detach(&buf, NULL);
644 static void make_cover_letter(struct rev_info *rev, int use_stdout,
645 int numbered, int numbered_files,
646 struct commit *origin,
647 int nr, struct commit **list, struct commit *head)
649 const char *committer;
651 const char *subject_start = NULL;
652 const char *body = "*** SUBJECT HERE ***\n\n*** BLURB HERE ***\n";
654 const char *extra_headers = rev->extra_headers;
658 const char *encoding = "utf-8";
659 struct diff_options opts;
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))
668 head_sha1 = sha1_to_hex(head->object.sha1);
670 log_write_email_headers(rev, head_sha1, &subject_start, &extra_headers);
672 committer = git_committer_info(0);
676 pp_user_info(NULL, CMIT_FMT_EMAIL, &sb, committer, DATE_RFC2822,
678 pp_title_line(CMIT_FMT_EMAIL, &msg, &sb, subject_start, extra_headers,
680 pp_remainder(CMIT_FMT_EMAIL, &msg, &sb, 0);
681 printf("%s\n", sb.buf);
686 for (i = 0; i < nr; i++)
687 shortlog_add_commit(&log, list[i]);
689 shortlog_output(&log);
692 * We can only do diffstat with a unique reference point
698 opts.output_format |= DIFF_FORMAT_SUMMARY | DIFF_FORMAT_DIFFSTAT;
700 diff_setup_done(&opts);
702 diff_tree_sha1(origin->tree->object.sha1,
703 head->tree->object.sha1,
711 static const char *clean_message_id(const char *msg_id)
714 const char *a, *z, *m;
717 while ((ch = *m) && (isspace(ch) || (ch == '<')))
722 if (!isspace(ch) && (ch != '>'))
727 die("insane in-reply-to: %s", msg_id);
730 return xmemdupz(a, z - a);
733 int cmd_format_patch(int argc, const char **argv, const char *prefix)
735 struct commit *commit;
736 struct commit **list = NULL;
738 int nr = 0, total, i, j;
740 int start_number = -1;
741 int keep_subject = 0;
742 int numbered_files = 0; /* _just_ numbers */
743 int subject_prefix = 0;
744 int ignore_if_in_upstream = 0;
746 int cover_letter = 0;
747 int boundary_count = 0;
748 struct commit *origin = NULL, *head = NULL;
749 const char *in_reply_to = NULL;
750 struct patch_ids ids;
751 char *add_signoff = NULL;
754 git_config(git_format_config);
755 init_revisions(&rev, prefix);
756 rev.commit_format = CMIT_FMT_EMAIL;
757 rev.verbose_header = 1;
759 rev.combine_merges = 0;
760 rev.ignore_merges = 1;
761 rev.diffopt.msg_sep = "";
762 DIFF_OPT_SET(&rev.diffopt, RECURSIVE);
764 rev.subject_prefix = fmt_patch_subject_prefix;
767 * Parse the arguments before setup_revisions(), or something
768 * like "git format-patch -o a123 HEAD^.." may fail; a123 is
769 * possibly a valid SHA1.
771 for (i = 1, j = 1; i < argc; i++) {
772 if (!strcmp(argv[i], "--stdout"))
774 else if (!strcmp(argv[i], "-n") ||
775 !strcmp(argv[i], "--numbered"))
777 else if (!strcmp(argv[i], "-N") ||
778 !strcmp(argv[i], "--no-numbered")) {
782 else if (!prefixcmp(argv[i], "--start-number="))
783 start_number = strtol(argv[i] + 15, NULL, 10);
784 else if (!strcmp(argv[i], "--numbered-files"))
786 else if (!strcmp(argv[i], "--start-number")) {
789 die("Need a number for --start-number");
790 start_number = strtol(argv[i], NULL, 10);
792 else if (!prefixcmp(argv[i], "--cc=")) {
793 ALLOC_GROW(extra_cc, extra_cc_nr + 1, extra_cc_alloc);
794 extra_cc[extra_cc_nr++] = xstrdup(argv[i] + 5);
796 else if (!strcmp(argv[i], "-k") ||
797 !strcmp(argv[i], "--keep-subject")) {
801 else if (!strcmp(argv[i], "--output-directory") ||
802 !strcmp(argv[i], "-o")) {
805 die("Which directory?");
806 if (output_directory)
807 die("Two output directories?");
808 output_directory = argv[i];
810 else if (!strcmp(argv[i], "--signoff") ||
811 !strcmp(argv[i], "-s")) {
812 const char *committer;
814 committer = git_committer_info(IDENT_ERROR_ON_NO_NAME);
815 endpos = strchr(committer, '>');
817 die("bogos committer info %s\n", committer);
818 add_signoff = xmemdupz(committer, endpos - committer + 1);
820 else if (!strcmp(argv[i], "--attach")) {
821 rev.mime_boundary = git_version_string;
824 else if (!prefixcmp(argv[i], "--attach=")) {
825 rev.mime_boundary = argv[i] + 9;
828 else if (!strcmp(argv[i], "--inline")) {
829 rev.mime_boundary = git_version_string;
832 else if (!prefixcmp(argv[i], "--inline=")) {
833 rev.mime_boundary = argv[i] + 9;
836 else if (!strcmp(argv[i], "--ignore-if-in-upstream"))
837 ignore_if_in_upstream = 1;
838 else if (!strcmp(argv[i], "--thread"))
840 else if (!prefixcmp(argv[i], "--in-reply-to="))
841 in_reply_to = argv[i] + 14;
842 else if (!strcmp(argv[i], "--in-reply-to")) {
845 die("Need a Message-Id for --in-reply-to");
846 in_reply_to = argv[i];
847 } else if (!prefixcmp(argv[i], "--subject-prefix=")) {
849 rev.subject_prefix = argv[i] + 17;
850 } else if (!prefixcmp(argv[i], "--suffix="))
851 fmt_patch_suffix = argv[i] + 9;
852 else if (!strcmp(argv[i], "--cover-letter"))
859 strbuf_init(&buf, 0);
861 for (i = 0; i < extra_hdr_nr; i++) {
862 strbuf_addstr(&buf, extra_hdr[i]);
863 strbuf_addch(&buf, '\n');
867 strbuf_addstr(&buf, "To: ");
868 for (i = 0; i < extra_to_nr; i++) {
870 strbuf_addstr(&buf, " ");
871 strbuf_addstr(&buf, extra_to[i]);
872 if (i + 1 < extra_to_nr)
873 strbuf_addch(&buf, ',');
874 strbuf_addch(&buf, '\n');
878 strbuf_addstr(&buf, "Cc: ");
879 for (i = 0; i < extra_cc_nr; i++) {
881 strbuf_addstr(&buf, " ");
882 strbuf_addstr(&buf, extra_cc[i]);
883 if (i + 1 < extra_cc_nr)
884 strbuf_addch(&buf, ',');
885 strbuf_addch(&buf, '\n');
888 rev.extra_headers = strbuf_detach(&buf, 0);
890 if (start_number < 0)
892 if (numbered && keep_subject)
893 die ("-n and -k are mutually exclusive.");
894 if (keep_subject && subject_prefix)
895 die ("--subject-prefix and -k are mutually exclusive.");
896 if (numbered_files && use_stdout)
897 die ("--numbered-files and --stdout are mutually exclusive.");
899 argc = setup_revisions(argc, argv, &rev, "HEAD");
901 die ("unrecognized argument: %s", argv[1]);
903 if (!rev.diffopt.output_format)
904 rev.diffopt.output_format = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_SUMMARY | DIFF_FORMAT_PATCH;
906 if (!DIFF_OPT_TST(&rev.diffopt, TEXT))
907 DIFF_OPT_SET(&rev.diffopt, BINARY);
909 if (!output_directory && !use_stdout)
910 output_directory = prefix;
912 if (output_directory) {
914 die("standard output, or directory, which one?");
915 if (mkdir(output_directory, 0777) < 0 && errno != EEXIST)
916 die("Could not create directory %s",
920 if (rev.pending.nr == 1) {
921 if (rev.max_count < 0 && !rev.show_root_diff) {
923 * This is traditional behaviour of "git format-patch
924 * origin" that prepares what the origin side still
927 rev.pending.objects[0].item->flags |= UNINTERESTING;
928 add_head_to_pending(&rev);
931 * Otherwise, it is "format-patch -22 HEAD", and/or
932 * "format-patch --root HEAD". The user wants
933 * get_revision() to do the usual traversal.
937 /* remember the range */
939 for (i = 0; i < rev.pending.nr; i++) {
940 struct object *o = rev.pending.objects[i].item;
941 if (!(o->flags & UNINTERESTING))
942 head = (struct commit *)o;
944 /* We can't generate a cover letter without any patches */
949 if (ignore_if_in_upstream)
950 get_patch_ids(&rev, &ids, prefix);
953 realstdout = xfdopen(xdup(1), "w");
955 if (prepare_revision_walk(&rev))
956 die("revision walk setup failed");
958 while ((commit = get_revision(&rev)) != NULL) {
959 if (commit->object.flags & BOUNDARY) {
961 origin = (boundary_count == 1) ? commit : NULL;
966 if (commit->parents && commit->parents->next)
969 if (ignore_if_in_upstream &&
970 has_commit_patch_id(commit, &ids))
974 list = xrealloc(list, nr * sizeof(list[0]));
975 list[nr - 1] = commit;
978 if (!keep_subject && auto_number && total > 1)
981 rev.total = total + start_number - 1;
983 rev.ref_message_id = clean_message_id(in_reply_to);
986 gen_message_id(&rev, "cover");
987 make_cover_letter(&rev, use_stdout, numbered, numbered_files,
988 origin, nr, list, head);
992 rev.add_signoff = add_signoff;
996 rev.nr = total - nr + (start_number - 1);
997 /* Make the second and subsequent mails replies to the first */
999 /* Have we already had a message ID? */
1000 if (rev.message_id) {
1002 * If we've got the ID to be a reply
1003 * to, discard the current ID;
1004 * otherwise, make everything a reply
1007 if (rev.ref_message_id)
1008 free(rev.message_id);
1010 rev.ref_message_id = rev.message_id;
1012 gen_message_id(&rev, sha1_to_hex(commit->object.sha1));
1014 if (!use_stdout && reopen_stdout(numbered_files ? NULL :
1015 get_oneline_for_filename(commit, keep_subject),
1017 die("Failed to create output files");
1018 shown = log_tree_commit(&rev, commit);
1019 free(commit->buffer);
1020 commit->buffer = NULL;
1022 /* We put one extra blank line between formatted
1023 * patches and this flag is used by log-tree code
1024 * to see if it needs to emit a LF before showing
1025 * the log; when using one file per patch, we do
1026 * not want the extra blank line.
1031 if (rev.mime_boundary)
1032 printf("\n--%s%s--\n\n\n",
1033 mime_boundary_leader,
1036 printf("-- \n%s\n\n", git_version_string);
1042 if (ignore_if_in_upstream)
1043 free_patch_ids(&ids);
1047 static int add_pending_commit(const char *arg, struct rev_info *revs, int flags)
1049 unsigned char sha1[20];
1050 if (get_sha1(arg, sha1) == 0) {
1051 struct commit *commit = lookup_commit_reference(sha1);
1053 commit->object.flags |= flags;
1054 add_pending_object(revs, &commit->object, arg);
1061 static const char cherry_usage[] =
1062 "git-cherry [-v] <upstream> [<head>] [<limit>]";
1063 int cmd_cherry(int argc, const char **argv, const char *prefix)
1065 struct rev_info revs;
1066 struct patch_ids ids;
1067 struct commit *commit;
1068 struct commit_list *list = NULL;
1069 const char *upstream;
1070 const char *head = "HEAD";
1071 const char *limit = NULL;
1074 if (argc > 1 && !strcmp(argv[1], "-v")) {
1091 usage(cherry_usage);
1094 init_revisions(&revs, prefix);
1096 revs.combine_merges = 0;
1097 revs.ignore_merges = 1;
1098 DIFF_OPT_SET(&revs.diffopt, RECURSIVE);
1100 if (add_pending_commit(head, &revs, 0))
1101 die("Unknown commit %s", head);
1102 if (add_pending_commit(upstream, &revs, UNINTERESTING))
1103 die("Unknown commit %s", upstream);
1105 /* Don't say anything if head and upstream are the same. */
1106 if (revs.pending.nr == 2) {
1107 struct object_array_entry *o = revs.pending.objects;
1108 if (hashcmp(o[0].item->sha1, o[1].item->sha1) == 0)
1112 get_patch_ids(&revs, &ids, prefix);
1114 if (limit && add_pending_commit(limit, &revs, UNINTERESTING))
1115 die("Unknown commit %s", limit);
1117 /* reverse the list of commits */
1118 if (prepare_revision_walk(&revs))
1119 die("revision walk setup failed");
1120 while ((commit = get_revision(&revs)) != NULL) {
1122 if (commit->parents && commit->parents->next)
1125 commit_list_insert(commit, &list);
1131 commit = list->item;
1132 if (has_commit_patch_id(commit, &ids))
1137 strbuf_init(&buf, 0);
1138 pretty_print_commit(CMIT_FMT_ONELINE, commit,
1139 &buf, 0, NULL, NULL, 0, 0);
1140 printf("%c %s %s\n", sign,
1141 sha1_to_hex(commit->object.sha1), buf.buf);
1142 strbuf_release(&buf);
1145 printf("%c %s\n", sign,
1146 sha1_to_hex(commit->object.sha1));
1152 free_patch_ids(&ids);