Add tests for extra headers in format-patch
[git/dscho.git] / builtin-log.c
blob3dc765011ca148d6735d83d3b2342e907eaf7bc3
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 "commit.h"
9 #include "diff.h"
10 #include "revision.h"
11 #include "log-tree.h"
12 #include "builtin.h"
13 #include "tag.h"
14 #include "reflog-walk.h"
15 #include "patch-ids.h"
16 #include "refs.h"
17 #include "run-command.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 prepare_revision_walk(rev);
203 if (rev->early_output)
204 finish_early_output(rev);
206 while ((commit = get_revision(rev)) != NULL) {
207 log_tree_commit(rev, commit);
208 if (!rev->reflog_info) {
209 /* we allow cycles in reflog ancestry */
210 free(commit->buffer);
211 commit->buffer = NULL;
213 free_commit_list(commit->parents);
214 commit->parents = NULL;
216 return 0;
219 static int git_log_config(const char *var, const char *value)
221 if (!strcmp(var, "format.subjectprefix")) {
222 if (!value)
223 config_error_nonbool(var);
224 fmt_patch_subject_prefix = xstrdup(value);
225 return 0;
227 if (!strcmp(var, "log.showroot")) {
228 default_show_root = git_config_bool(var, value);
229 return 0;
231 return git_diff_ui_config(var, value);
234 int cmd_whatchanged(int argc, const char **argv, const char *prefix)
236 struct rev_info rev;
238 git_config(git_log_config);
239 init_revisions(&rev, prefix);
240 rev.diff = 1;
241 rev.simplify_history = 0;
242 cmd_log_init(argc, argv, prefix, &rev);
243 if (!rev.diffopt.output_format)
244 rev.diffopt.output_format = DIFF_FORMAT_RAW;
245 return cmd_log_walk(&rev);
248 static void show_tagger(char *buf, int len, struct rev_info *rev)
250 char *email_end, *p;
251 unsigned long date;
252 int tz;
254 email_end = memchr(buf, '>', len);
255 if (!email_end)
256 return;
257 p = ++email_end;
258 while (isspace(*p))
259 p++;
260 date = strtoul(p, &p, 10);
261 while (isspace(*p))
262 p++;
263 tz = (int)strtol(p, NULL, 10);
264 printf("Tagger: %.*s\nDate: %s\n", (int)(email_end - buf), buf,
265 show_date(date, tz, rev->date_mode));
268 static int show_object(const unsigned char *sha1, int show_tag_object,
269 struct rev_info *rev)
271 unsigned long size;
272 enum object_type type;
273 char *buf = read_sha1_file(sha1, &type, &size);
274 int offset = 0;
276 if (!buf)
277 return error("Could not read object %s", sha1_to_hex(sha1));
279 if (show_tag_object)
280 while (offset < size && buf[offset] != '\n') {
281 int new_offset = offset + 1;
282 while (new_offset < size && buf[new_offset++] != '\n')
283 ; /* do nothing */
284 if (!prefixcmp(buf + offset, "tagger "))
285 show_tagger(buf + offset + 7,
286 new_offset - offset - 7, rev);
287 offset = new_offset;
290 if (offset < size)
291 fwrite(buf + offset, size - offset, 1, stdout);
292 free(buf);
293 return 0;
296 static int show_tree_object(const unsigned char *sha1,
297 const char *base, int baselen,
298 const char *pathname, unsigned mode, int stage)
300 printf("%s%s\n", pathname, S_ISDIR(mode) ? "/" : "");
301 return 0;
304 int cmd_show(int argc, const char **argv, const char *prefix)
306 struct rev_info rev;
307 struct object_array_entry *objects;
308 int i, count, ret = 0;
310 git_config(git_log_config);
311 init_revisions(&rev, prefix);
312 rev.diff = 1;
313 rev.combine_merges = 1;
314 rev.dense_combined_merges = 1;
315 rev.always_show_header = 1;
316 rev.ignore_merges = 0;
317 rev.no_walk = 1;
318 cmd_log_init(argc, argv, prefix, &rev);
320 count = rev.pending.nr;
321 objects = rev.pending.objects;
322 for (i = 0; i < count && !ret; i++) {
323 struct object *o = objects[i].item;
324 const char *name = objects[i].name;
325 switch (o->type) {
326 case OBJ_BLOB:
327 ret = show_object(o->sha1, 0, NULL);
328 break;
329 case OBJ_TAG: {
330 struct tag *t = (struct tag *)o;
332 printf("%stag %s%s\n",
333 diff_get_color_opt(&rev.diffopt, DIFF_COMMIT),
334 t->tag,
335 diff_get_color_opt(&rev.diffopt, DIFF_RESET));
336 ret = show_object(o->sha1, 1, &rev);
337 objects[i].item = (struct object *)t->tagged;
338 i--;
339 break;
341 case OBJ_TREE:
342 printf("%stree %s%s\n\n",
343 diff_get_color_opt(&rev.diffopt, DIFF_COMMIT),
344 name,
345 diff_get_color_opt(&rev.diffopt, DIFF_RESET));
346 read_tree_recursive((struct tree *)o, "", 0, 0, NULL,
347 show_tree_object);
348 break;
349 case OBJ_COMMIT:
350 rev.pending.nr = rev.pending.alloc = 0;
351 rev.pending.objects = NULL;
352 add_object_array(o, name, &rev.pending);
353 ret = cmd_log_walk(&rev);
354 break;
355 default:
356 ret = error("Unknown type: %d", o->type);
359 free(objects);
360 return ret;
364 * This is equivalent to "git log -g --abbrev-commit --pretty=oneline"
366 int cmd_log_reflog(int argc, const char **argv, const char *prefix)
368 struct rev_info rev;
370 git_config(git_log_config);
371 init_revisions(&rev, prefix);
372 init_reflog_walk(&rev.reflog_info);
373 rev.abbrev_commit = 1;
374 rev.verbose_header = 1;
375 cmd_log_init(argc, argv, prefix, &rev);
378 * This means that we override whatever commit format the user gave
379 * on the cmd line. Sad, but cmd_log_init() currently doesn't
380 * allow us to set a different default.
382 rev.commit_format = CMIT_FMT_ONELINE;
383 rev.always_show_header = 1;
386 * We get called through "git reflog", so unlike the other log
387 * routines, we need to set up our pager manually..
389 setup_pager();
391 return cmd_log_walk(&rev);
394 int cmd_log(int argc, const char **argv, const char *prefix)
396 struct rev_info rev;
398 git_config(git_log_config);
399 init_revisions(&rev, prefix);
400 rev.always_show_header = 1;
401 cmd_log_init(argc, argv, prefix, &rev);
402 return cmd_log_walk(&rev);
405 /* format-patch */
406 #define FORMAT_PATCH_NAME_MAX 64
408 static int istitlechar(char c)
410 return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
411 (c >= '0' && c <= '9') || c == '.' || c == '_';
414 static char *extra_headers = NULL;
415 static int extra_headers_size = 0;
416 static const char *fmt_patch_suffix = ".patch";
417 static int numbered = 0;
418 static int auto_number = 0;
420 static int git_format_config(const char *var, const char *value)
422 if (!strcmp(var, "format.headers")) {
423 int len;
425 if (!value)
426 die("format.headers without value");
427 len = strlen(value);
428 extra_headers_size += len + 1;
429 extra_headers = xrealloc(extra_headers, extra_headers_size);
430 extra_headers[extra_headers_size - len - 1] = 0;
431 strcat(extra_headers, value);
432 return 0;
434 if (!strcmp(var, "format.suffix")) {
435 if (!value)
436 return config_error_nonbool(var);
437 fmt_patch_suffix = xstrdup(value);
438 return 0;
440 if (!strcmp(var, "diff.color") || !strcmp(var, "color.diff")) {
441 return 0;
443 if (!strcmp(var, "format.numbered")) {
444 if (value && !strcasecmp(value, "auto")) {
445 auto_number = 1;
446 return 0;
448 numbered = git_config_bool(var, value);
449 return 0;
452 return git_log_config(var, value);
456 static const char *get_oneline_for_filename(struct commit *commit,
457 int keep_subject)
459 static char filename[PATH_MAX];
460 char *sol;
461 int len = 0;
462 int suffix_len = strlen(fmt_patch_suffix) + 1;
464 sol = strstr(commit->buffer, "\n\n");
465 if (!sol)
466 filename[0] = '\0';
467 else {
468 int j, space = 0;
470 sol += 2;
471 /* strip [PATCH] or [PATCH blabla] */
472 if (!keep_subject && !prefixcmp(sol, "[PATCH")) {
473 char *eos = strchr(sol + 6, ']');
474 if (eos) {
475 while (isspace(*eos))
476 eos++;
477 sol = eos;
481 for (j = 0;
482 j < FORMAT_PATCH_NAME_MAX - suffix_len - 5 &&
483 len < sizeof(filename) - suffix_len &&
484 sol[j] && sol[j] != '\n';
485 j++) {
486 if (istitlechar(sol[j])) {
487 if (space) {
488 filename[len++] = '-';
489 space = 0;
491 filename[len++] = sol[j];
492 if (sol[j] == '.')
493 while (sol[j + 1] == '.')
494 j++;
495 } else
496 space = 1;
498 while (filename[len - 1] == '.'
499 || filename[len - 1] == '-')
500 len--;
501 filename[len] = '\0';
503 return filename;
506 static FILE *realstdout = NULL;
507 static const char *output_directory = NULL;
509 static int reopen_stdout(const char *oneline, int nr, int total)
511 char filename[PATH_MAX];
512 int len = 0;
513 int suffix_len = strlen(fmt_patch_suffix) + 1;
515 if (output_directory) {
516 len = snprintf(filename, sizeof(filename), "%s",
517 output_directory);
518 if (len >=
519 sizeof(filename) - FORMAT_PATCH_NAME_MAX - suffix_len)
520 return error("name of output directory is too long");
521 if (filename[len - 1] != '/')
522 filename[len++] = '/';
525 if (!oneline)
526 len += sprintf(filename + len, "%d", nr);
527 else {
528 len += sprintf(filename + len, "%04d-", nr);
529 len += snprintf(filename + len, sizeof(filename) - len - 1
530 - suffix_len, "%s", oneline);
531 strcpy(filename + len, fmt_patch_suffix);
534 fprintf(realstdout, "%s\n", filename);
535 if (freopen(filename, "w", stdout) == NULL)
536 return error("Cannot open patch file %s",filename);
538 return 0;
541 static void get_patch_ids(struct rev_info *rev, struct patch_ids *ids, const char *prefix)
543 struct rev_info check_rev;
544 struct commit *commit;
545 struct object *o1, *o2;
546 unsigned flags1, flags2;
548 if (rev->pending.nr != 2)
549 die("Need exactly one range.");
551 o1 = rev->pending.objects[0].item;
552 flags1 = o1->flags;
553 o2 = rev->pending.objects[1].item;
554 flags2 = o2->flags;
556 if ((flags1 & UNINTERESTING) == (flags2 & UNINTERESTING))
557 die("Not a range.");
559 init_patch_ids(ids);
561 /* given a range a..b get all patch ids for b..a */
562 init_revisions(&check_rev, prefix);
563 o1->flags ^= UNINTERESTING;
564 o2->flags ^= UNINTERESTING;
565 add_pending_object(&check_rev, o1, "o1");
566 add_pending_object(&check_rev, o2, "o2");
567 prepare_revision_walk(&check_rev);
569 while ((commit = get_revision(&check_rev)) != NULL) {
570 /* ignore merges */
571 if (commit->parents && commit->parents->next)
572 continue;
574 add_commit_patch_id(commit, ids);
577 /* reset for next revision walk */
578 clear_commit_marks((struct commit *)o1,
579 SEEN | UNINTERESTING | SHOWN | ADDED);
580 clear_commit_marks((struct commit *)o2,
581 SEEN | UNINTERESTING | SHOWN | ADDED);
582 o1->flags = flags1;
583 o2->flags = flags2;
586 static void gen_message_id(struct rev_info *info, char *base)
588 const char *committer = git_committer_info(IDENT_WARN_ON_NO_NAME);
589 const char *email_start = strrchr(committer, '<');
590 const char *email_end = strrchr(committer, '>');
591 struct strbuf buf;
592 if (!email_start || !email_end || email_start > email_end - 1)
593 die("Could not extract email from committer identity.");
594 strbuf_init(&buf, 0);
595 strbuf_addf(&buf, "%s.%lu.git.%.*s", base,
596 (unsigned long) time(NULL),
597 (int)(email_end - email_start - 1), email_start + 1);
598 info->message_id = strbuf_detach(&buf, NULL);
601 static void make_cover_letter(struct rev_info *rev,
602 int use_stdout, int numbered, int numbered_files,
603 struct commit *origin, struct commit *head)
605 const char *committer;
606 const char *origin_sha1, *head_sha1;
607 const char *argv[7];
608 const char *subject_start = NULL;
609 const char *body = "*** SUBJECT HERE ***\n\n*** BLURB HERE ***\n";
610 const char *msg;
611 const char *extra_headers = rev->extra_headers;
612 struct strbuf sb;
613 const char *encoding = "utf-8";
615 if (rev->commit_format != CMIT_FMT_EMAIL)
616 die("Cover letter needs email format");
618 if (!use_stdout && reopen_stdout(numbered_files ?
619 NULL : "cover-letter", 0, rev->total))
620 return;
622 origin_sha1 = sha1_to_hex(origin ? origin->object.sha1 : null_sha1);
623 head_sha1 = sha1_to_hex(head->object.sha1);
625 log_write_email_headers(rev, head_sha1, &subject_start, &extra_headers);
627 committer = git_committer_info(0);
629 msg = body;
630 strbuf_init(&sb, 0);
631 pp_user_info(NULL, CMIT_FMT_EMAIL, &sb, committer, DATE_RFC2822,
632 encoding);
633 pp_title_line(CMIT_FMT_EMAIL, &msg, &sb, subject_start, extra_headers,
634 encoding, 0);
635 pp_remainder(CMIT_FMT_EMAIL, &msg, &sb, 0);
636 printf("%s\n", sb.buf);
638 strbuf_release(&sb);
641 * We can only do diffstat with a unique reference point, and
642 * log is a bit tricky, so just skip it.
644 if (!origin)
645 return;
647 argv[0] = "shortlog";
648 argv[1] = head_sha1;
649 argv[2] = "--not";
650 argv[3] = origin_sha1;
651 argv[4] = "--";
652 argv[5] = NULL;
653 fflush(stdout);
654 run_command_v_opt(argv, RUN_GIT_CMD);
656 argv[0] = "diff";
657 argv[1] = "--stat";
658 argv[2] = "--summary";
659 argv[3] = head_sha1;
660 argv[4] = "--not";
661 argv[5] = origin_sha1;
662 argv[6] = "--";
663 argv[7] = NULL;
664 fflush(stdout);
665 run_command_v_opt(argv, RUN_GIT_CMD);
667 fflush(stdout);
668 printf("\n");
671 static const char *clean_message_id(const char *msg_id)
673 char ch;
674 const char *a, *z, *m;
676 m = msg_id;
677 while ((ch = *m) && (isspace(ch) || (ch == '<')))
678 m++;
679 a = m;
680 z = NULL;
681 while ((ch = *m)) {
682 if (!isspace(ch) && (ch != '>'))
683 z = m;
684 m++;
686 if (!z)
687 die("insane in-reply-to: %s", msg_id);
688 if (++z == m)
689 return a;
690 return xmemdupz(a, z - a);
693 int cmd_format_patch(int argc, const char **argv, const char *prefix)
695 struct commit *commit;
696 struct commit **list = NULL;
697 struct rev_info rev;
698 int nr = 0, total, i, j;
699 int use_stdout = 0;
700 int start_number = -1;
701 int keep_subject = 0;
702 int numbered_files = 0; /* _just_ numbers */
703 int subject_prefix = 0;
704 int ignore_if_in_upstream = 0;
705 int thread = 0;
706 int cover_letter = 0;
707 struct commit *origin = NULL, *head = NULL;
708 const char *in_reply_to = NULL;
709 struct patch_ids ids;
710 char *add_signoff = NULL;
712 git_config(git_format_config);
713 init_revisions(&rev, prefix);
714 rev.commit_format = CMIT_FMT_EMAIL;
715 rev.verbose_header = 1;
716 rev.diff = 1;
717 rev.combine_merges = 0;
718 rev.ignore_merges = 1;
719 rev.diffopt.msg_sep = "";
720 DIFF_OPT_SET(&rev.diffopt, RECURSIVE);
722 rev.subject_prefix = fmt_patch_subject_prefix;
723 rev.extra_headers = extra_headers;
726 * Parse the arguments before setup_revisions(), or something
727 * like "git format-patch -o a123 HEAD^.." may fail; a123 is
728 * possibly a valid SHA1.
730 for (i = 1, j = 1; i < argc; i++) {
731 if (!strcmp(argv[i], "--stdout"))
732 use_stdout = 1;
733 else if (!strcmp(argv[i], "-n") ||
734 !strcmp(argv[i], "--numbered"))
735 numbered = 1;
736 else if (!strcmp(argv[i], "-N") ||
737 !strcmp(argv[i], "--no-numbered")) {
738 numbered = 0;
739 auto_number = 0;
741 else if (!prefixcmp(argv[i], "--start-number="))
742 start_number = strtol(argv[i] + 15, NULL, 10);
743 else if (!strcmp(argv[i], "--numbered-files"))
744 numbered_files = 1;
745 else if (!strcmp(argv[i], "--start-number")) {
746 i++;
747 if (i == argc)
748 die("Need a number for --start-number");
749 start_number = strtol(argv[i], NULL, 10);
751 else if (!strcmp(argv[i], "-k") ||
752 !strcmp(argv[i], "--keep-subject")) {
753 keep_subject = 1;
754 rev.total = -1;
756 else if (!strcmp(argv[i], "--output-directory") ||
757 !strcmp(argv[i], "-o")) {
758 i++;
759 if (argc <= i)
760 die("Which directory?");
761 if (output_directory)
762 die("Two output directories?");
763 output_directory = argv[i];
765 else if (!strcmp(argv[i], "--signoff") ||
766 !strcmp(argv[i], "-s")) {
767 const char *committer;
768 const char *endpos;
769 committer = git_committer_info(IDENT_ERROR_ON_NO_NAME);
770 endpos = strchr(committer, '>');
771 if (!endpos)
772 die("bogos committer info %s\n", committer);
773 add_signoff = xmemdupz(committer, endpos - committer + 1);
775 else if (!strcmp(argv[i], "--attach")) {
776 rev.mime_boundary = git_version_string;
777 rev.no_inline = 1;
779 else if (!prefixcmp(argv[i], "--attach=")) {
780 rev.mime_boundary = argv[i] + 9;
781 rev.no_inline = 1;
783 else if (!strcmp(argv[i], "--inline")) {
784 rev.mime_boundary = git_version_string;
785 rev.no_inline = 0;
787 else if (!prefixcmp(argv[i], "--inline=")) {
788 rev.mime_boundary = argv[i] + 9;
789 rev.no_inline = 0;
791 else if (!strcmp(argv[i], "--ignore-if-in-upstream"))
792 ignore_if_in_upstream = 1;
793 else if (!strcmp(argv[i], "--thread"))
794 thread = 1;
795 else if (!prefixcmp(argv[i], "--in-reply-to="))
796 in_reply_to = argv[i] + 14;
797 else if (!strcmp(argv[i], "--in-reply-to")) {
798 i++;
799 if (i == argc)
800 die("Need a Message-Id for --in-reply-to");
801 in_reply_to = argv[i];
802 } else if (!prefixcmp(argv[i], "--subject-prefix=")) {
803 subject_prefix = 1;
804 rev.subject_prefix = argv[i] + 17;
805 } else if (!prefixcmp(argv[i], "--suffix="))
806 fmt_patch_suffix = argv[i] + 9;
807 else if (!strcmp(argv[i], "--cover-letter"))
808 cover_letter = 1;
809 else
810 argv[j++] = argv[i];
812 argc = j;
814 if (start_number < 0)
815 start_number = 1;
816 if (numbered && keep_subject)
817 die ("-n and -k are mutually exclusive.");
818 if (keep_subject && subject_prefix)
819 die ("--subject-prefix and -k are mutually exclusive.");
820 if (numbered_files && use_stdout)
821 die ("--numbered-files and --stdout are mutually exclusive.");
823 argc = setup_revisions(argc, argv, &rev, "HEAD");
824 if (argc > 1)
825 die ("unrecognized argument: %s", argv[1]);
827 if (!rev.diffopt.output_format)
828 rev.diffopt.output_format = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_SUMMARY | DIFF_FORMAT_PATCH;
830 if (!DIFF_OPT_TST(&rev.diffopt, TEXT))
831 DIFF_OPT_SET(&rev.diffopt, BINARY);
833 if (!output_directory && !use_stdout)
834 output_directory = prefix;
836 if (output_directory) {
837 if (use_stdout)
838 die("standard output, or directory, which one?");
839 if (mkdir(output_directory, 0777) < 0 && errno != EEXIST)
840 die("Could not create directory %s",
841 output_directory);
844 if (rev.pending.nr == 1) {
845 if (rev.max_count < 0 && !rev.show_root_diff) {
847 * This is traditional behaviour of "git format-patch
848 * origin" that prepares what the origin side still
849 * does not have.
851 rev.pending.objects[0].item->flags |= UNINTERESTING;
852 add_head_to_pending(&rev);
855 * Otherwise, it is "format-patch -22 HEAD", and/or
856 * "format-patch --root HEAD". The user wants
857 * get_revision() to do the usual traversal.
860 if (cover_letter) {
861 /* remember the range */
862 int negative_count = 0;
863 int i;
864 for (i = 0; i < rev.pending.nr; i++) {
865 struct object *o = rev.pending.objects[i].item;
866 if (o->flags & UNINTERESTING) {
867 origin = (struct commit *)o;
868 negative_count++;
869 } else
870 head = (struct commit *)o;
872 /* Multiple origins don't work for diffstat. */
873 if (negative_count > 1)
874 origin = NULL;
875 /* We can't generate a cover letter without any patches */
876 if (!head)
877 return 0;
880 if (ignore_if_in_upstream)
881 get_patch_ids(&rev, &ids, prefix);
883 if (!use_stdout)
884 realstdout = xfdopen(xdup(1), "w");
886 prepare_revision_walk(&rev);
887 while ((commit = get_revision(&rev)) != NULL) {
888 /* ignore merges */
889 if (commit->parents && commit->parents->next)
890 continue;
892 if (ignore_if_in_upstream &&
893 has_commit_patch_id(commit, &ids))
894 continue;
896 nr++;
897 list = xrealloc(list, nr * sizeof(list[0]));
898 list[nr - 1] = commit;
900 total = nr;
901 if (!keep_subject && auto_number && total > 1)
902 numbered = 1;
903 if (numbered)
904 rev.total = total + start_number - 1;
905 if (in_reply_to)
906 rev.ref_message_id = clean_message_id(in_reply_to);
907 if (cover_letter) {
908 if (thread)
909 gen_message_id(&rev, "cover");
910 make_cover_letter(&rev, use_stdout, numbered, numbered_files,
911 origin, head);
912 total++;
913 start_number--;
915 rev.add_signoff = add_signoff;
916 while (0 <= --nr) {
917 int shown;
918 commit = list[nr];
919 rev.nr = total - nr + (start_number - 1);
920 /* Make the second and subsequent mails replies to the first */
921 if (thread) {
922 /* Have we already had a message ID? */
923 if (rev.message_id) {
925 * If we've got the ID to be a reply
926 * to, discard the current ID;
927 * otherwise, make everything a reply
928 * to that.
930 if (rev.ref_message_id)
931 free(rev.message_id);
932 else
933 rev.ref_message_id = rev.message_id;
935 gen_message_id(&rev, sha1_to_hex(commit->object.sha1));
937 if (!use_stdout && reopen_stdout(numbered_files ? NULL :
938 get_oneline_for_filename(commit, keep_subject),
939 rev.nr, rev.total))
940 die("Failed to create output files");
941 shown = log_tree_commit(&rev, commit);
942 free(commit->buffer);
943 commit->buffer = NULL;
945 /* We put one extra blank line between formatted
946 * patches and this flag is used by log-tree code
947 * to see if it needs to emit a LF before showing
948 * the log; when using one file per patch, we do
949 * not want the extra blank line.
951 if (!use_stdout)
952 rev.shown_one = 0;
953 if (shown) {
954 if (rev.mime_boundary)
955 printf("\n--%s%s--\n\n\n",
956 mime_boundary_leader,
957 rev.mime_boundary);
958 else
959 printf("-- \n%s\n\n", git_version_string);
961 if (!use_stdout)
962 fclose(stdout);
964 free(list);
965 if (ignore_if_in_upstream)
966 free_patch_ids(&ids);
967 return 0;
970 static int add_pending_commit(const char *arg, struct rev_info *revs, int flags)
972 unsigned char sha1[20];
973 if (get_sha1(arg, sha1) == 0) {
974 struct commit *commit = lookup_commit_reference(sha1);
975 if (commit) {
976 commit->object.flags |= flags;
977 add_pending_object(revs, &commit->object, arg);
978 return 0;
981 return -1;
984 static const char cherry_usage[] =
985 "git-cherry [-v] <upstream> [<head>] [<limit>]";
986 int cmd_cherry(int argc, const char **argv, const char *prefix)
988 struct rev_info revs;
989 struct patch_ids ids;
990 struct commit *commit;
991 struct commit_list *list = NULL;
992 const char *upstream;
993 const char *head = "HEAD";
994 const char *limit = NULL;
995 int verbose = 0;
997 if (argc > 1 && !strcmp(argv[1], "-v")) {
998 verbose = 1;
999 argc--;
1000 argv++;
1003 switch (argc) {
1004 case 4:
1005 limit = argv[3];
1006 /* FALLTHROUGH */
1007 case 3:
1008 head = argv[2];
1009 /* FALLTHROUGH */
1010 case 2:
1011 upstream = argv[1];
1012 break;
1013 default:
1014 usage(cherry_usage);
1017 init_revisions(&revs, prefix);
1018 revs.diff = 1;
1019 revs.combine_merges = 0;
1020 revs.ignore_merges = 1;
1021 DIFF_OPT_SET(&revs.diffopt, RECURSIVE);
1023 if (add_pending_commit(head, &revs, 0))
1024 die("Unknown commit %s", head);
1025 if (add_pending_commit(upstream, &revs, UNINTERESTING))
1026 die("Unknown commit %s", upstream);
1028 /* Don't say anything if head and upstream are the same. */
1029 if (revs.pending.nr == 2) {
1030 struct object_array_entry *o = revs.pending.objects;
1031 if (hashcmp(o[0].item->sha1, o[1].item->sha1) == 0)
1032 return 0;
1035 get_patch_ids(&revs, &ids, prefix);
1037 if (limit && add_pending_commit(limit, &revs, UNINTERESTING))
1038 die("Unknown commit %s", limit);
1040 /* reverse the list of commits */
1041 prepare_revision_walk(&revs);
1042 while ((commit = get_revision(&revs)) != NULL) {
1043 /* ignore merges */
1044 if (commit->parents && commit->parents->next)
1045 continue;
1047 commit_list_insert(commit, &list);
1050 while (list) {
1051 char sign = '+';
1053 commit = list->item;
1054 if (has_commit_patch_id(commit, &ids))
1055 sign = '-';
1057 if (verbose) {
1058 struct strbuf buf;
1059 strbuf_init(&buf, 0);
1060 pretty_print_commit(CMIT_FMT_ONELINE, commit,
1061 &buf, 0, NULL, NULL, 0, 0);
1062 printf("%c %s %s\n", sign,
1063 sha1_to_hex(commit->object.sha1), buf.buf);
1064 strbuf_release(&buf);
1066 else {
1067 printf("%c %s\n", sign,
1068 sha1_to_hex(commit->object.sha1));
1071 list = list->next;
1074 free_patch_ids(&ids);
1075 return 0;