builtin-bundle: add a --basis option that specifies a basis
[git/pieter.git] / builtin-log.c
blob9db3ed5ab0553b9f3e2c13c5933f1bb3a683d89d
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, void *cb)
235 if (!strcmp(var, "format.pretty"))
236 return git_config_string(&fmt_pretty, var, value);
237 if (!strcmp(var, "format.subjectprefix"))
238 return git_config_string(&fmt_patch_subject_prefix, var, value);
239 if (!strcmp(var, "log.date"))
240 return git_config_string(&default_date_mode, var, value);
241 if (!strcmp(var, "log.showroot")) {
242 default_show_root = git_config_bool(var, value);
243 return 0;
245 return git_diff_ui_config(var, value, cb);
248 int cmd_whatchanged(int argc, const char **argv, const char *prefix)
250 struct rev_info rev;
252 git_config(git_log_config, NULL);
254 if (diff_use_color_default == -1)
255 diff_use_color_default = git_use_color_default;
257 init_revisions(&rev, prefix);
258 rev.diff = 1;
259 rev.simplify_history = 0;
260 cmd_log_init(argc, argv, prefix, &rev);
261 if (!rev.diffopt.output_format)
262 rev.diffopt.output_format = DIFF_FORMAT_RAW;
263 return cmd_log_walk(&rev);
266 static void show_tagger(char *buf, int len, struct rev_info *rev)
268 char *email_end, *p;
269 unsigned long date;
270 int tz;
272 email_end = memchr(buf, '>', len);
273 if (!email_end)
274 return;
275 p = ++email_end;
276 while (isspace(*p))
277 p++;
278 date = strtoul(p, &p, 10);
279 while (isspace(*p))
280 p++;
281 tz = (int)strtol(p, NULL, 10);
282 printf("Tagger: %.*s\nDate: %s\n", (int)(email_end - buf), buf,
283 show_date(date, tz, rev->date_mode));
286 static int show_object(const unsigned char *sha1, int show_tag_object,
287 struct rev_info *rev)
289 unsigned long size;
290 enum object_type type;
291 char *buf = read_sha1_file(sha1, &type, &size);
292 int offset = 0;
294 if (!buf)
295 return error("Could not read object %s", sha1_to_hex(sha1));
297 if (show_tag_object)
298 while (offset < size && buf[offset] != '\n') {
299 int new_offset = offset + 1;
300 while (new_offset < size && buf[new_offset++] != '\n')
301 ; /* do nothing */
302 if (!prefixcmp(buf + offset, "tagger "))
303 show_tagger(buf + offset + 7,
304 new_offset - offset - 7, rev);
305 offset = new_offset;
308 if (offset < size)
309 fwrite(buf + offset, size - offset, 1, stdout);
310 free(buf);
311 return 0;
314 static int show_tree_object(const unsigned char *sha1,
315 const char *base, int baselen,
316 const char *pathname, unsigned mode, int stage, void *context)
318 printf("%s%s\n", pathname, S_ISDIR(mode) ? "/" : "");
319 return 0;
322 int cmd_show(int argc, const char **argv, const char *prefix)
324 struct rev_info rev;
325 struct object_array_entry *objects;
326 int i, count, ret = 0;
328 git_config(git_log_config, NULL);
330 if (diff_use_color_default == -1)
331 diff_use_color_default = git_use_color_default;
333 init_revisions(&rev, prefix);
334 rev.diff = 1;
335 rev.combine_merges = 1;
336 rev.dense_combined_merges = 1;
337 rev.always_show_header = 1;
338 rev.ignore_merges = 0;
339 rev.no_walk = 1;
340 cmd_log_init(argc, argv, prefix, &rev);
342 count = rev.pending.nr;
343 objects = rev.pending.objects;
344 for (i = 0; i < count && !ret; i++) {
345 struct object *o = objects[i].item;
346 const char *name = objects[i].name;
347 switch (o->type) {
348 case OBJ_BLOB:
349 ret = show_object(o->sha1, 0, NULL);
350 break;
351 case OBJ_TAG: {
352 struct tag *t = (struct tag *)o;
354 printf("%stag %s%s\n",
355 diff_get_color_opt(&rev.diffopt, DIFF_COMMIT),
356 t->tag,
357 diff_get_color_opt(&rev.diffopt, DIFF_RESET));
358 ret = show_object(o->sha1, 1, &rev);
359 objects[i].item = parse_object(t->tagged->sha1);
360 i--;
361 break;
363 case OBJ_TREE:
364 printf("%stree %s%s\n\n",
365 diff_get_color_opt(&rev.diffopt, DIFF_COMMIT),
366 name,
367 diff_get_color_opt(&rev.diffopt, DIFF_RESET));
368 read_tree_recursive((struct tree *)o, "", 0, 0, NULL,
369 show_tree_object, NULL);
370 break;
371 case OBJ_COMMIT:
372 rev.pending.nr = rev.pending.alloc = 0;
373 rev.pending.objects = NULL;
374 add_object_array(o, name, &rev.pending);
375 ret = cmd_log_walk(&rev);
376 break;
377 default:
378 ret = error("Unknown type: %d", o->type);
381 free(objects);
382 return ret;
386 * This is equivalent to "git log -g --abbrev-commit --pretty=oneline"
388 int cmd_log_reflog(int argc, const char **argv, const char *prefix)
390 struct rev_info rev;
392 git_config(git_log_config, NULL);
394 if (diff_use_color_default == -1)
395 diff_use_color_default = git_use_color_default;
397 init_revisions(&rev, prefix);
398 init_reflog_walk(&rev.reflog_info);
399 rev.abbrev_commit = 1;
400 rev.verbose_header = 1;
401 cmd_log_init(argc, argv, prefix, &rev);
404 * This means that we override whatever commit format the user gave
405 * on the cmd line. Sad, but cmd_log_init() currently doesn't
406 * allow us to set a different default.
408 rev.commit_format = CMIT_FMT_ONELINE;
409 rev.use_terminator = 1;
410 rev.always_show_header = 1;
413 * We get called through "git reflog", so unlike the other log
414 * routines, we need to set up our pager manually..
416 setup_pager();
418 return cmd_log_walk(&rev);
421 int cmd_log(int argc, const char **argv, const char *prefix)
423 struct rev_info rev;
425 git_config(git_log_config, NULL);
427 if (diff_use_color_default == -1)
428 diff_use_color_default = git_use_color_default;
430 init_revisions(&rev, prefix);
431 rev.always_show_header = 1;
432 cmd_log_init(argc, argv, prefix, &rev);
433 return cmd_log_walk(&rev);
436 /* format-patch */
437 #define FORMAT_PATCH_NAME_MAX 64
439 static int istitlechar(char c)
441 return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
442 (c >= '0' && c <= '9') || c == '.' || c == '_';
445 static const char *fmt_patch_suffix = ".patch";
446 static int fmt_patch_signoff = 0;
447 static int numbered = 0;
448 static int auto_number = 0;
450 static char **extra_hdr;
451 static int extra_hdr_nr;
452 static int extra_hdr_alloc;
454 static char **extra_to;
455 static int extra_to_nr;
456 static int extra_to_alloc;
458 static char **extra_cc;
459 static int extra_cc_nr;
460 static int extra_cc_alloc;
462 static void add_header(const char *value)
464 int len = strlen(value);
465 while (value[len - 1] == '\n')
466 len--;
467 if (!strncasecmp(value, "to: ", 4)) {
468 ALLOC_GROW(extra_to, extra_to_nr + 1, extra_to_alloc);
469 extra_to[extra_to_nr++] = xstrndup(value + 4, len - 4);
470 return;
472 if (!strncasecmp(value, "cc: ", 4)) {
473 ALLOC_GROW(extra_cc, extra_cc_nr + 1, extra_cc_alloc);
474 extra_cc[extra_cc_nr++] = xstrndup(value + 4, len - 4);
475 return;
477 ALLOC_GROW(extra_hdr, extra_hdr_nr + 1, extra_hdr_alloc);
478 extra_hdr[extra_hdr_nr++] = xstrndup(value, len);
481 static int git_format_config(const char *var, const char *value, void *cb)
483 if (!strcmp(var, "format.headers")) {
484 if (!value)
485 die("format.headers without value");
486 add_header(value);
487 return 0;
489 if (!strcmp(var, "format.suffix"))
490 return git_config_string(&fmt_patch_suffix, var, value);
491 if (!strcmp(var, "format.cc")) {
492 if (!value)
493 return config_error_nonbool(var);
494 ALLOC_GROW(extra_cc, extra_cc_nr + 1, extra_cc_alloc);
495 extra_cc[extra_cc_nr++] = xstrdup(value);
496 return 0;
498 if (!strcmp(var, "format.sign-off")) {
499 fmt_patch_signoff = git_config_bool(var, value);
501 if (!strcmp(var, "diff.color") || !strcmp(var, "color.diff")) {
502 return 0;
504 if (!strcmp(var, "format.numbered")) {
505 if (value && !strcasecmp(value, "auto")) {
506 auto_number = 1;
507 return 0;
509 numbered = git_config_bool(var, value);
510 return 0;
513 return git_log_config(var, value, cb);
517 static const char *get_oneline_for_filename(struct commit *commit,
518 int keep_subject)
520 static char filename[PATH_MAX];
521 char *sol;
522 int len = 0;
523 int suffix_len = strlen(fmt_patch_suffix) + 1;
525 sol = strstr(commit->buffer, "\n\n");
526 if (!sol)
527 filename[0] = '\0';
528 else {
529 int j, space = 0;
531 sol += 2;
532 /* strip [PATCH] or [PATCH blabla] */
533 if (!keep_subject && !prefixcmp(sol, "[PATCH")) {
534 char *eos = strchr(sol + 6, ']');
535 if (eos) {
536 while (isspace(*eos))
537 eos++;
538 sol = eos;
542 for (j = 0;
543 j < FORMAT_PATCH_NAME_MAX - suffix_len - 5 &&
544 len < sizeof(filename) - suffix_len &&
545 sol[j] && sol[j] != '\n';
546 j++) {
547 if (istitlechar(sol[j])) {
548 if (space) {
549 filename[len++] = '-';
550 space = 0;
552 filename[len++] = sol[j];
553 if (sol[j] == '.')
554 while (sol[j + 1] == '.')
555 j++;
556 } else
557 space = 1;
559 while (filename[len - 1] == '.'
560 || filename[len - 1] == '-')
561 len--;
562 filename[len] = '\0';
564 return filename;
567 static FILE *realstdout = NULL;
568 static const char *output_directory = NULL;
570 static int reopen_stdout(const char *oneline, int nr, int total)
572 char filename[PATH_MAX];
573 int len = 0;
574 int suffix_len = strlen(fmt_patch_suffix) + 1;
576 if (output_directory) {
577 len = snprintf(filename, sizeof(filename), "%s",
578 output_directory);
579 if (len >=
580 sizeof(filename) - FORMAT_PATCH_NAME_MAX - suffix_len)
581 return error("name of output directory is too long");
582 if (filename[len - 1] != '/')
583 filename[len++] = '/';
586 if (!oneline)
587 len += sprintf(filename + len, "%d", nr);
588 else {
589 len += sprintf(filename + len, "%04d-", nr);
590 len += snprintf(filename + len, sizeof(filename) - len - 1
591 - suffix_len, "%s", oneline);
592 strcpy(filename + len, fmt_patch_suffix);
595 fprintf(realstdout, "%s\n", filename);
596 if (freopen(filename, "w", stdout) == NULL)
597 return error("Cannot open patch file %s",filename);
599 return 0;
602 static void get_patch_ids(struct rev_info *rev, struct patch_ids *ids, const char *prefix)
604 struct rev_info check_rev;
605 struct commit *commit;
606 struct object *o1, *o2;
607 unsigned flags1, flags2;
609 if (rev->pending.nr != 2)
610 die("Need exactly one range.");
612 o1 = rev->pending.objects[0].item;
613 flags1 = o1->flags;
614 o2 = rev->pending.objects[1].item;
615 flags2 = o2->flags;
617 if ((flags1 & UNINTERESTING) == (flags2 & UNINTERESTING))
618 die("Not a range.");
620 init_patch_ids(ids);
622 /* given a range a..b get all patch ids for b..a */
623 init_revisions(&check_rev, prefix);
624 o1->flags ^= UNINTERESTING;
625 o2->flags ^= UNINTERESTING;
626 add_pending_object(&check_rev, o1, "o1");
627 add_pending_object(&check_rev, o2, "o2");
628 if (prepare_revision_walk(&check_rev))
629 die("revision walk setup failed");
631 while ((commit = get_revision(&check_rev)) != NULL) {
632 /* ignore merges */
633 if (commit->parents && commit->parents->next)
634 continue;
636 add_commit_patch_id(commit, ids);
639 /* reset for next revision walk */
640 clear_commit_marks((struct commit *)o1,
641 SEEN | UNINTERESTING | SHOWN | ADDED);
642 clear_commit_marks((struct commit *)o2,
643 SEEN | UNINTERESTING | SHOWN | ADDED);
644 o1->flags = flags1;
645 o2->flags = flags2;
648 static void gen_message_id(struct rev_info *info, char *base)
650 const char *committer = git_committer_info(IDENT_WARN_ON_NO_NAME);
651 const char *email_start = strrchr(committer, '<');
652 const char *email_end = strrchr(committer, '>');
653 struct strbuf buf;
654 if (!email_start || !email_end || email_start > email_end - 1)
655 die("Could not extract email from committer identity.");
656 strbuf_init(&buf, 0);
657 strbuf_addf(&buf, "%s.%lu.git.%.*s", base,
658 (unsigned long) time(NULL),
659 (int)(email_end - email_start - 1), email_start + 1);
660 info->message_id = strbuf_detach(&buf, NULL);
663 static void make_cover_letter(struct rev_info *rev, int use_stdout,
664 int numbered, int numbered_files,
665 struct commit *origin,
666 int nr, struct commit **list, struct commit *head)
668 const char *committer;
669 char *head_sha1;
670 const char *subject_start = NULL;
671 const char *body = "*** SUBJECT HERE ***\n\n*** BLURB HERE ***\n";
672 const char *msg;
673 const char *extra_headers = rev->extra_headers;
674 struct shortlog log;
675 struct strbuf sb;
676 int i;
677 const char *encoding = "utf-8";
678 struct diff_options opts;
679 int need_8bit_cte = 0;
681 if (rev->commit_format != CMIT_FMT_EMAIL)
682 die("Cover letter needs email format");
684 if (!use_stdout && reopen_stdout(numbered_files ?
685 NULL : "cover-letter", 0, rev->total))
686 return;
688 head_sha1 = sha1_to_hex(head->object.sha1);
690 log_write_email_headers(rev, head_sha1, &subject_start, &extra_headers,
691 &need_8bit_cte);
693 committer = git_committer_info(0);
695 msg = body;
696 strbuf_init(&sb, 0);
697 pp_user_info(NULL, CMIT_FMT_EMAIL, &sb, committer, DATE_RFC2822,
698 encoding);
699 pp_title_line(CMIT_FMT_EMAIL, &msg, &sb, subject_start, extra_headers,
700 encoding, need_8bit_cte);
701 pp_remainder(CMIT_FMT_EMAIL, &msg, &sb, 0);
702 printf("%s\n", sb.buf);
704 strbuf_release(&sb);
706 shortlog_init(&log);
707 log.wrap_lines = 1;
708 log.wrap = 72;
709 log.in1 = 2;
710 log.in2 = 4;
711 for (i = 0; i < nr; i++)
712 shortlog_add_commit(&log, list[i]);
714 shortlog_output(&log);
717 * We can only do diffstat with a unique reference point
719 if (!origin)
720 return;
722 memcpy(&opts, &rev->diffopt, sizeof(opts));
723 opts.output_format = DIFF_FORMAT_SUMMARY | DIFF_FORMAT_DIFFSTAT;
725 diff_setup_done(&opts);
727 diff_tree_sha1(origin->tree->object.sha1,
728 head->tree->object.sha1,
729 "", &opts);
730 diffcore_std(&opts);
731 diff_flush(&opts);
733 printf("\n");
736 static const char *clean_message_id(const char *msg_id)
738 char ch;
739 const char *a, *z, *m;
741 m = msg_id;
742 while ((ch = *m) && (isspace(ch) || (ch == '<')))
743 m++;
744 a = m;
745 z = NULL;
746 while ((ch = *m)) {
747 if (!isspace(ch) && (ch != '>'))
748 z = m;
749 m++;
751 if (!z)
752 die("insane in-reply-to: %s", msg_id);
753 if (++z == m)
754 return a;
755 return xmemdupz(a, z - a);
758 int cmd_format_patch(int argc, const char **argv, const char *prefix)
760 struct commit *commit;
761 struct commit **list = NULL;
762 struct rev_info rev;
763 int nr = 0, total, i, j;
764 int use_stdout = 0;
765 int start_number = -1;
766 int keep_subject = 0;
767 int numbered_files = 0; /* _just_ numbers */
768 int subject_prefix = 0;
769 int ignore_if_in_upstream = 0;
770 int thread = 0;
771 int cover_letter = 0;
772 int boundary_count = 0;
773 int no_binary_diff = 0;
774 struct commit *origin = NULL, *head = NULL;
775 const char *in_reply_to = NULL;
776 struct patch_ids ids;
777 char *add_signoff = NULL;
778 struct strbuf buf;
780 git_config(git_format_config, NULL);
781 init_revisions(&rev, prefix);
782 rev.commit_format = CMIT_FMT_EMAIL;
783 rev.verbose_header = 1;
784 rev.diff = 1;
785 rev.combine_merges = 0;
786 rev.ignore_merges = 1;
787 DIFF_OPT_SET(&rev.diffopt, RECURSIVE);
789 rev.subject_prefix = fmt_patch_subject_prefix;
792 * Parse the arguments before setup_revisions(), or something
793 * like "git format-patch -o a123 HEAD^.." may fail; a123 is
794 * possibly a valid SHA1.
796 for (i = 1, j = 1; i < argc; i++) {
797 if (!strcmp(argv[i], "--stdout"))
798 use_stdout = 1;
799 else if (!strcmp(argv[i], "-n") ||
800 !strcmp(argv[i], "--numbered"))
801 numbered = 1;
802 else if (!strcmp(argv[i], "-N") ||
803 !strcmp(argv[i], "--no-numbered")) {
804 numbered = 0;
805 auto_number = 0;
807 else if (!prefixcmp(argv[i], "--start-number="))
808 start_number = strtol(argv[i] + 15, NULL, 10);
809 else if (!strcmp(argv[i], "--numbered-files"))
810 numbered_files = 1;
811 else if (!strcmp(argv[i], "--start-number")) {
812 i++;
813 if (i == argc)
814 die("Need a number for --start-number");
815 start_number = strtol(argv[i], NULL, 10);
817 else if (!prefixcmp(argv[i], "--cc=")) {
818 ALLOC_GROW(extra_cc, extra_cc_nr + 1, extra_cc_alloc);
819 extra_cc[extra_cc_nr++] = xstrdup(argv[i] + 5);
821 else if (!strcmp(argv[i], "-k") ||
822 !strcmp(argv[i], "--keep-subject")) {
823 keep_subject = 1;
824 rev.total = -1;
826 else if (!strcmp(argv[i], "--output-directory") ||
827 !strcmp(argv[i], "-o")) {
828 i++;
829 if (argc <= i)
830 die("Which directory?");
831 if (output_directory)
832 die("Two output directories?");
833 output_directory = argv[i];
835 else if (!strcmp(argv[i], "--signoff") ||
836 !strcmp(argv[i], "-s"))
837 fmt_patch_signoff = 1;
838 else if (!strcmp(argv[i], "--attach")) {
839 rev.mime_boundary = git_version_string;
840 rev.no_inline = 1;
842 else if (!prefixcmp(argv[i], "--attach=")) {
843 rev.mime_boundary = argv[i] + 9;
844 rev.no_inline = 1;
846 else if (!strcmp(argv[i], "--inline")) {
847 rev.mime_boundary = git_version_string;
848 rev.no_inline = 0;
850 else if (!prefixcmp(argv[i], "--inline=")) {
851 rev.mime_boundary = argv[i] + 9;
852 rev.no_inline = 0;
854 else if (!strcmp(argv[i], "--ignore-if-in-upstream"))
855 ignore_if_in_upstream = 1;
856 else if (!strcmp(argv[i], "--thread"))
857 thread = 1;
858 else if (!prefixcmp(argv[i], "--in-reply-to="))
859 in_reply_to = argv[i] + 14;
860 else if (!strcmp(argv[i], "--in-reply-to")) {
861 i++;
862 if (i == argc)
863 die("Need a Message-Id for --in-reply-to");
864 in_reply_to = argv[i];
865 } else if (!prefixcmp(argv[i], "--subject-prefix=")) {
866 subject_prefix = 1;
867 rev.subject_prefix = argv[i] + 17;
868 } else if (!prefixcmp(argv[i], "--suffix="))
869 fmt_patch_suffix = argv[i] + 9;
870 else if (!strcmp(argv[i], "--cover-letter"))
871 cover_letter = 1;
872 else if (!strcmp(argv[i], "--no-binary"))
873 no_binary_diff = 1;
874 else
875 argv[j++] = argv[i];
877 argc = j;
879 strbuf_init(&buf, 0);
881 for (i = 0; i < extra_hdr_nr; i++) {
882 strbuf_addstr(&buf, extra_hdr[i]);
883 strbuf_addch(&buf, '\n');
886 if (extra_to_nr)
887 strbuf_addstr(&buf, "To: ");
888 for (i = 0; i < extra_to_nr; i++) {
889 if (i)
890 strbuf_addstr(&buf, " ");
891 strbuf_addstr(&buf, extra_to[i]);
892 if (i + 1 < extra_to_nr)
893 strbuf_addch(&buf, ',');
894 strbuf_addch(&buf, '\n');
897 if (extra_cc_nr)
898 strbuf_addstr(&buf, "Cc: ");
899 for (i = 0; i < extra_cc_nr; i++) {
900 if (i)
901 strbuf_addstr(&buf, " ");
902 strbuf_addstr(&buf, extra_cc[i]);
903 if (i + 1 < extra_cc_nr)
904 strbuf_addch(&buf, ',');
905 strbuf_addch(&buf, '\n');
908 if (fmt_patch_signoff) {
909 const char *committer;
910 const char *endpos;
911 committer = git_committer_info(IDENT_ERROR_ON_NO_NAME);
912 endpos = strchr(committer, '>');
913 if (!endpos)
914 die("bogos committer info %s\n", committer);
915 add_signoff = xmemdupz(committer, endpos - committer + 1);
918 rev.extra_headers = strbuf_detach(&buf, 0);
920 if (start_number < 0)
921 start_number = 1;
922 if (numbered && keep_subject)
923 die ("-n and -k are mutually exclusive.");
924 if (keep_subject && subject_prefix)
925 die ("--subject-prefix and -k are mutually exclusive.");
926 if (numbered_files && use_stdout)
927 die ("--numbered-files and --stdout are mutually exclusive.");
929 argc = setup_revisions(argc, argv, &rev, "HEAD");
930 if (argc > 1)
931 die ("unrecognized argument: %s", argv[1]);
933 if (!rev.diffopt.output_format)
934 rev.diffopt.output_format = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_SUMMARY | DIFF_FORMAT_PATCH;
936 if (!DIFF_OPT_TST(&rev.diffopt, TEXT) && !no_binary_diff)
937 DIFF_OPT_SET(&rev.diffopt, BINARY);
939 if (!output_directory && !use_stdout)
940 output_directory = prefix;
942 if (output_directory) {
943 if (use_stdout)
944 die("standard output, or directory, which one?");
945 if (mkdir(output_directory, 0777) < 0 && errno != EEXIST)
946 die("Could not create directory %s",
947 output_directory);
950 if (rev.pending.nr == 1) {
951 if (rev.max_count < 0 && !rev.show_root_diff) {
953 * This is traditional behaviour of "git format-patch
954 * origin" that prepares what the origin side still
955 * does not have.
957 rev.pending.objects[0].item->flags |= UNINTERESTING;
958 add_head_to_pending(&rev);
961 * Otherwise, it is "format-patch -22 HEAD", and/or
962 * "format-patch --root HEAD". The user wants
963 * get_revision() to do the usual traversal.
966 if (cover_letter) {
967 /* remember the range */
968 int i;
969 for (i = 0; i < rev.pending.nr; i++) {
970 struct object *o = rev.pending.objects[i].item;
971 if (!(o->flags & UNINTERESTING))
972 head = (struct commit *)o;
974 /* We can't generate a cover letter without any patches */
975 if (!head)
976 return 0;
979 if (ignore_if_in_upstream)
980 get_patch_ids(&rev, &ids, prefix);
982 if (!use_stdout)
983 realstdout = xfdopen(xdup(1), "w");
985 if (prepare_revision_walk(&rev))
986 die("revision walk setup failed");
987 rev.boundary = 1;
988 while ((commit = get_revision(&rev)) != NULL) {
989 if (commit->object.flags & BOUNDARY) {
990 boundary_count++;
991 origin = (boundary_count == 1) ? commit : NULL;
992 continue;
995 /* ignore merges */
996 if (commit->parents && commit->parents->next)
997 continue;
999 if (ignore_if_in_upstream &&
1000 has_commit_patch_id(commit, &ids))
1001 continue;
1003 nr++;
1004 list = xrealloc(list, nr * sizeof(list[0]));
1005 list[nr - 1] = commit;
1007 total = nr;
1008 if (!keep_subject && auto_number && total > 1)
1009 numbered = 1;
1010 if (numbered)
1011 rev.total = total + start_number - 1;
1012 if (in_reply_to)
1013 rev.ref_message_id = clean_message_id(in_reply_to);
1014 if (cover_letter) {
1015 if (thread)
1016 gen_message_id(&rev, "cover");
1017 make_cover_letter(&rev, use_stdout, numbered, numbered_files,
1018 origin, nr, list, head);
1019 total++;
1020 start_number--;
1022 rev.add_signoff = add_signoff;
1023 while (0 <= --nr) {
1024 int shown;
1025 commit = list[nr];
1026 rev.nr = total - nr + (start_number - 1);
1027 /* Make the second and subsequent mails replies to the first */
1028 if (thread) {
1029 /* Have we already had a message ID? */
1030 if (rev.message_id) {
1032 * If we've got the ID to be a reply
1033 * to, discard the current ID;
1034 * otherwise, make everything a reply
1035 * to that.
1037 if (rev.ref_message_id)
1038 free(rev.message_id);
1039 else
1040 rev.ref_message_id = rev.message_id;
1042 gen_message_id(&rev, sha1_to_hex(commit->object.sha1));
1044 if (!use_stdout && reopen_stdout(numbered_files ? NULL :
1045 get_oneline_for_filename(commit, keep_subject),
1046 rev.nr, rev.total))
1047 die("Failed to create output files");
1048 shown = log_tree_commit(&rev, commit);
1049 free(commit->buffer);
1050 commit->buffer = NULL;
1052 /* We put one extra blank line between formatted
1053 * patches and this flag is used by log-tree code
1054 * to see if it needs to emit a LF before showing
1055 * the log; when using one file per patch, we do
1056 * not want the extra blank line.
1058 if (!use_stdout)
1059 rev.shown_one = 0;
1060 if (shown) {
1061 if (rev.mime_boundary)
1062 printf("\n--%s%s--\n\n\n",
1063 mime_boundary_leader,
1064 rev.mime_boundary);
1065 else
1066 printf("-- \n%s\n\n", git_version_string);
1068 if (!use_stdout)
1069 fclose(stdout);
1071 free(list);
1072 if (ignore_if_in_upstream)
1073 free_patch_ids(&ids);
1074 return 0;
1077 static int add_pending_commit(const char *arg, struct rev_info *revs, int flags)
1079 unsigned char sha1[20];
1080 if (get_sha1(arg, sha1) == 0) {
1081 struct commit *commit = lookup_commit_reference(sha1);
1082 if (commit) {
1083 commit->object.flags |= flags;
1084 add_pending_object(revs, &commit->object, arg);
1085 return 0;
1088 return -1;
1091 static const char cherry_usage[] =
1092 "git cherry [-v] <upstream> [<head>] [<limit>]";
1093 int cmd_cherry(int argc, const char **argv, const char *prefix)
1095 struct rev_info revs;
1096 struct patch_ids ids;
1097 struct commit *commit;
1098 struct commit_list *list = NULL;
1099 const char *upstream;
1100 const char *head = "HEAD";
1101 const char *limit = NULL;
1102 int verbose = 0;
1104 if (argc > 1 && !strcmp(argv[1], "-v")) {
1105 verbose = 1;
1106 argc--;
1107 argv++;
1110 switch (argc) {
1111 case 4:
1112 limit = argv[3];
1113 /* FALLTHROUGH */
1114 case 3:
1115 head = argv[2];
1116 /* FALLTHROUGH */
1117 case 2:
1118 upstream = argv[1];
1119 break;
1120 default:
1121 usage(cherry_usage);
1124 init_revisions(&revs, prefix);
1125 revs.diff = 1;
1126 revs.combine_merges = 0;
1127 revs.ignore_merges = 1;
1128 DIFF_OPT_SET(&revs.diffopt, RECURSIVE);
1130 if (add_pending_commit(head, &revs, 0))
1131 die("Unknown commit %s", head);
1132 if (add_pending_commit(upstream, &revs, UNINTERESTING))
1133 die("Unknown commit %s", upstream);
1135 /* Don't say anything if head and upstream are the same. */
1136 if (revs.pending.nr == 2) {
1137 struct object_array_entry *o = revs.pending.objects;
1138 if (hashcmp(o[0].item->sha1, o[1].item->sha1) == 0)
1139 return 0;
1142 get_patch_ids(&revs, &ids, prefix);
1144 if (limit && add_pending_commit(limit, &revs, UNINTERESTING))
1145 die("Unknown commit %s", limit);
1147 /* reverse the list of commits */
1148 if (prepare_revision_walk(&revs))
1149 die("revision walk setup failed");
1150 while ((commit = get_revision(&revs)) != NULL) {
1151 /* ignore merges */
1152 if (commit->parents && commit->parents->next)
1153 continue;
1155 commit_list_insert(commit, &list);
1158 while (list) {
1159 char sign = '+';
1161 commit = list->item;
1162 if (has_commit_patch_id(commit, &ids))
1163 sign = '-';
1165 if (verbose) {
1166 struct strbuf buf;
1167 strbuf_init(&buf, 0);
1168 pretty_print_commit(CMIT_FMT_ONELINE, commit,
1169 &buf, 0, NULL, NULL, 0, 0);
1170 printf("%c %s %s\n", sign,
1171 sha1_to_hex(commit->object.sha1), buf.buf);
1172 strbuf_release(&buf);
1174 else {
1175 printf("%c %s\n", sign,
1176 sha1_to_hex(commit->object.sha1));
1179 list = list->next;
1182 free_patch_ids(&ids);
1183 return 0;