Move computation of absolute paths from Makefile to runtime (in preparation for RUNTI...
[alt-git.git] / builtin-log.c
blobc7aa48e74821b9b8aea2f99041cf323c9057c8da
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 "run-command.h"
18 #include "shortlog.h"
19 #include "remote.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 cmd_log_init(int argc, const char **argv, const char *prefix,
29 struct rev_info *rev)
31 int i;
33 rev->abbrev = DEFAULT_ABBREV;
34 rev->commit_format = CMIT_FMT_DEFAULT;
35 if (fmt_pretty)
36 get_commit_format(fmt_pretty, rev);
37 rev->verbose_header = 1;
38 DIFF_OPT_SET(&rev->diffopt, RECURSIVE);
39 rev->show_root_diff = default_show_root;
40 rev->subject_prefix = fmt_patch_subject_prefix;
41 DIFF_OPT_SET(&rev->diffopt, ALLOW_TEXTCONV);
43 if (default_date_mode)
44 rev->date_mode = parse_date_format(default_date_mode);
46 argc = setup_revisions(argc, argv, rev, "HEAD");
48 if (rev->diffopt.pickaxe || rev->diffopt.filter)
49 rev->always_show_header = 0;
50 if (DIFF_OPT_TST(&rev->diffopt, FOLLOW_RENAMES)) {
51 rev->always_show_header = 0;
52 if (rev->diffopt.nr_paths != 1)
53 usage("git logs can only follow renames on one pathname at a time");
55 for (i = 1; i < argc; i++) {
56 const char *arg = argv[i];
57 if (!strcmp(arg, "--decorate")) {
58 load_ref_decorations();
59 rev->show_decorations = 1;
60 } else if (!strcmp(arg, "--source")) {
61 rev->show_source = 1;
62 } else
63 die("unrecognized argument: %s", arg);
68 * This gives a rough estimate for how many commits we
69 * will print out in the list.
71 static int estimate_commit_count(struct rev_info *rev, struct commit_list *list)
73 int n = 0;
75 while (list) {
76 struct commit *commit = list->item;
77 unsigned int flags = commit->object.flags;
78 list = list->next;
79 if (!(flags & (TREESAME | UNINTERESTING)))
80 n++;
82 return n;
85 static void show_early_header(struct rev_info *rev, const char *stage, int nr)
87 if (rev->shown_one) {
88 rev->shown_one = 0;
89 if (rev->commit_format != CMIT_FMT_ONELINE)
90 putchar(rev->diffopt.line_termination);
92 printf("Final output: %d %s\n", nr, stage);
95 struct itimerval early_output_timer;
97 static void log_show_early(struct rev_info *revs, struct commit_list *list)
99 int i = revs->early_output;
100 int show_header = 1;
102 sort_in_topological_order(&list, revs->lifo);
103 while (list && i) {
104 struct commit *commit = list->item;
105 switch (simplify_commit(revs, commit)) {
106 case commit_show:
107 if (show_header) {
108 int n = estimate_commit_count(revs, list);
109 show_early_header(revs, "incomplete", n);
110 show_header = 0;
112 log_tree_commit(revs, commit);
113 i--;
114 break;
115 case commit_ignore:
116 break;
117 case commit_error:
118 return;
120 list = list->next;
123 /* Did we already get enough commits for the early output? */
124 if (!i)
125 return;
128 * ..if no, then repeat it twice a second until we
129 * do.
131 * NOTE! We don't use "it_interval", because if the
132 * reader isn't listening, we want our output to be
133 * throttled by the writing, and not have the timer
134 * trigger every second even if we're blocked on a
135 * reader!
137 early_output_timer.it_value.tv_sec = 0;
138 early_output_timer.it_value.tv_usec = 500000;
139 setitimer(ITIMER_REAL, &early_output_timer, NULL);
142 static void early_output(int signal)
144 show_early_output = log_show_early;
147 static void setup_early_output(struct rev_info *rev)
149 struct sigaction sa;
152 * Set up the signal handler, minimally intrusively:
153 * we only set a single volatile integer word (not
154 * using sigatomic_t - trying to avoid unnecessary
155 * system dependencies and headers), and using
156 * SA_RESTART.
158 memset(&sa, 0, sizeof(sa));
159 sa.sa_handler = early_output;
160 sigemptyset(&sa.sa_mask);
161 sa.sa_flags = SA_RESTART;
162 sigaction(SIGALRM, &sa, NULL);
165 * If we can get the whole output in less than a
166 * tenth of a second, don't even bother doing the
167 * early-output thing..
169 * This is a one-time-only trigger.
171 early_output_timer.it_value.tv_sec = 0;
172 early_output_timer.it_value.tv_usec = 100000;
173 setitimer(ITIMER_REAL, &early_output_timer, NULL);
176 static void finish_early_output(struct rev_info *rev)
178 int n = estimate_commit_count(rev, rev->commits);
179 signal(SIGALRM, SIG_IGN);
180 show_early_header(rev, "done", n);
183 static int cmd_log_walk(struct rev_info *rev)
185 struct commit *commit;
187 if (rev->early_output)
188 setup_early_output(rev);
190 if (prepare_revision_walk(rev))
191 die("revision walk setup failed");
193 if (rev->early_output)
194 finish_early_output(rev);
197 * For --check and --exit-code, the exit code is based on CHECK_FAILED
198 * and HAS_CHANGES being accumulated in rev->diffopt, so be careful to
199 * retain that state information if replacing rev->diffopt in this loop
201 while ((commit = get_revision(rev)) != NULL) {
202 log_tree_commit(rev, commit);
203 if (!rev->reflog_info) {
204 /* we allow cycles in reflog ancestry */
205 free(commit->buffer);
206 commit->buffer = NULL;
208 free_commit_list(commit->parents);
209 commit->parents = NULL;
211 if (rev->diffopt.output_format & DIFF_FORMAT_CHECKDIFF &&
212 DIFF_OPT_TST(&rev->diffopt, CHECK_FAILED)) {
213 return 02;
215 return diff_result_code(&rev->diffopt, 0);
218 static int git_log_config(const char *var, const char *value, void *cb)
220 if (!strcmp(var, "format.pretty"))
221 return git_config_string(&fmt_pretty, var, value);
222 if (!strcmp(var, "format.subjectprefix"))
223 return git_config_string(&fmt_patch_subject_prefix, var, value);
224 if (!strcmp(var, "log.date"))
225 return git_config_string(&default_date_mode, var, value);
226 if (!strcmp(var, "log.showroot")) {
227 default_show_root = git_config_bool(var, value);
228 return 0;
230 return git_diff_ui_config(var, value, cb);
233 int cmd_whatchanged(int argc, const char **argv, const char *prefix)
235 struct rev_info rev;
237 git_config(git_log_config, NULL);
239 if (diff_use_color_default == -1)
240 diff_use_color_default = git_use_color_default;
242 init_revisions(&rev, prefix);
243 rev.diff = 1;
244 rev.simplify_history = 0;
245 cmd_log_init(argc, argv, prefix, &rev);
246 if (!rev.diffopt.output_format)
247 rev.diffopt.output_format = DIFF_FORMAT_RAW;
248 return cmd_log_walk(&rev);
251 static void show_tagger(char *buf, int len, struct rev_info *rev)
253 struct strbuf out = STRBUF_INIT;
255 pp_user_info("Tagger", rev->commit_format, &out, buf, rev->date_mode,
256 git_log_output_encoding ?
257 git_log_output_encoding: git_commit_encoding);
258 printf("%s\n", out.buf);
259 strbuf_release(&out);
262 static int show_object(const unsigned char *sha1, int show_tag_object,
263 struct rev_info *rev)
265 unsigned long size;
266 enum object_type type;
267 char *buf = read_sha1_file(sha1, &type, &size);
268 int offset = 0;
270 if (!buf)
271 return error("Could not read object %s", sha1_to_hex(sha1));
273 if (show_tag_object)
274 while (offset < size && buf[offset] != '\n') {
275 int new_offset = offset + 1;
276 while (new_offset < size && buf[new_offset++] != '\n')
277 ; /* do nothing */
278 if (!prefixcmp(buf + offset, "tagger "))
279 show_tagger(buf + offset + 7,
280 new_offset - offset - 7, rev);
281 offset = new_offset;
284 if (offset < size)
285 fwrite(buf + offset, size - offset, 1, stdout);
286 free(buf);
287 return 0;
290 static int show_tree_object(const unsigned char *sha1,
291 const char *base, int baselen,
292 const char *pathname, unsigned mode, int stage, void *context)
294 printf("%s%s\n", pathname, S_ISDIR(mode) ? "/" : "");
295 return 0;
298 int cmd_show(int argc, const char **argv, const char *prefix)
300 struct rev_info rev;
301 struct object_array_entry *objects;
302 int i, count, ret = 0;
304 git_config(git_log_config, NULL);
306 if (diff_use_color_default == -1)
307 diff_use_color_default = git_use_color_default;
309 init_revisions(&rev, prefix);
310 rev.diff = 1;
311 rev.combine_merges = 1;
312 rev.dense_combined_merges = 1;
313 rev.always_show_header = 1;
314 rev.ignore_merges = 0;
315 rev.no_walk = 1;
316 cmd_log_init(argc, argv, prefix, &rev);
318 count = rev.pending.nr;
319 objects = rev.pending.objects;
320 for (i = 0; i < count && !ret; i++) {
321 struct object *o = objects[i].item;
322 const char *name = objects[i].name;
323 switch (o->type) {
324 case OBJ_BLOB:
325 ret = show_object(o->sha1, 0, NULL);
326 break;
327 case OBJ_TAG: {
328 struct tag *t = (struct tag *)o;
330 printf("%stag %s%s\n",
331 diff_get_color_opt(&rev.diffopt, DIFF_COMMIT),
332 t->tag,
333 diff_get_color_opt(&rev.diffopt, DIFF_RESET));
334 ret = show_object(o->sha1, 1, &rev);
335 if (ret)
336 break;
337 o = parse_object(t->tagged->sha1);
338 if (!o)
339 ret = error("Could not read object %s",
340 sha1_to_hex(t->tagged->sha1));
341 objects[i].item = o;
342 i--;
343 break;
345 case OBJ_TREE:
346 printf("%stree %s%s\n\n",
347 diff_get_color_opt(&rev.diffopt, DIFF_COMMIT),
348 name,
349 diff_get_color_opt(&rev.diffopt, DIFF_RESET));
350 read_tree_recursive((struct tree *)o, "", 0, 0, NULL,
351 show_tree_object, NULL);
352 break;
353 case OBJ_COMMIT:
354 rev.pending.nr = rev.pending.alloc = 0;
355 rev.pending.objects = NULL;
356 add_object_array(o, name, &rev.pending);
357 ret = cmd_log_walk(&rev);
358 break;
359 default:
360 ret = error("Unknown type: %d", o->type);
363 free(objects);
364 return ret;
368 * This is equivalent to "git log -g --abbrev-commit --pretty=oneline"
370 int cmd_log_reflog(int argc, const char **argv, const char *prefix)
372 struct rev_info rev;
374 git_config(git_log_config, NULL);
376 if (diff_use_color_default == -1)
377 diff_use_color_default = git_use_color_default;
379 init_revisions(&rev, prefix);
380 init_reflog_walk(&rev.reflog_info);
381 rev.abbrev_commit = 1;
382 rev.verbose_header = 1;
383 cmd_log_init(argc, argv, prefix, &rev);
386 * This means that we override whatever commit format the user gave
387 * on the cmd line. Sad, but cmd_log_init() currently doesn't
388 * allow us to set a different default.
390 rev.commit_format = CMIT_FMT_ONELINE;
391 rev.use_terminator = 1;
392 rev.always_show_header = 1;
395 * We get called through "git reflog", so unlike the other log
396 * routines, we need to set up our pager manually..
398 setup_pager();
400 return cmd_log_walk(&rev);
403 int cmd_log(int argc, const char **argv, const char *prefix)
405 struct rev_info rev;
407 git_config(git_log_config, NULL);
409 if (diff_use_color_default == -1)
410 diff_use_color_default = git_use_color_default;
412 init_revisions(&rev, prefix);
413 rev.always_show_header = 1;
414 cmd_log_init(argc, argv, prefix, &rev);
415 return cmd_log_walk(&rev);
418 /* format-patch */
419 #define FORMAT_PATCH_NAME_MAX 64
421 static int istitlechar(char c)
423 return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
424 (c >= '0' && c <= '9') || c == '.' || c == '_';
427 static const char *fmt_patch_suffix = ".patch";
428 static int numbered = 0;
429 static int auto_number = 1;
431 static char **extra_hdr;
432 static int extra_hdr_nr;
433 static int extra_hdr_alloc;
435 static char **extra_to;
436 static int extra_to_nr;
437 static int extra_to_alloc;
439 static char **extra_cc;
440 static int extra_cc_nr;
441 static int extra_cc_alloc;
443 static void add_header(const char *value)
445 int len = strlen(value);
446 while (len && value[len - 1] == '\n')
447 len--;
448 if (!strncasecmp(value, "to: ", 4)) {
449 ALLOC_GROW(extra_to, extra_to_nr + 1, extra_to_alloc);
450 extra_to[extra_to_nr++] = xstrndup(value + 4, len - 4);
451 return;
453 if (!strncasecmp(value, "cc: ", 4)) {
454 ALLOC_GROW(extra_cc, extra_cc_nr + 1, extra_cc_alloc);
455 extra_cc[extra_cc_nr++] = xstrndup(value + 4, len - 4);
456 return;
458 ALLOC_GROW(extra_hdr, extra_hdr_nr + 1, extra_hdr_alloc);
459 extra_hdr[extra_hdr_nr++] = xstrndup(value, len);
462 static int git_format_config(const char *var, const char *value, void *cb)
464 if (!strcmp(var, "format.headers")) {
465 if (!value)
466 die("format.headers without value");
467 add_header(value);
468 return 0;
470 if (!strcmp(var, "format.suffix"))
471 return git_config_string(&fmt_patch_suffix, var, value);
472 if (!strcmp(var, "format.cc")) {
473 if (!value)
474 return config_error_nonbool(var);
475 ALLOC_GROW(extra_cc, extra_cc_nr + 1, extra_cc_alloc);
476 extra_cc[extra_cc_nr++] = xstrdup(value);
477 return 0;
479 if (!strcmp(var, "diff.color") || !strcmp(var, "color.diff")) {
480 return 0;
482 if (!strcmp(var, "format.numbered")) {
483 if (value && !strcasecmp(value, "auto")) {
484 auto_number = 1;
485 return 0;
487 numbered = git_config_bool(var, value);
488 auto_number = auto_number && numbered;
489 return 0;
492 return git_log_config(var, value, cb);
496 static const char *get_oneline_for_filename(struct commit *commit,
497 int keep_subject)
499 static char filename[PATH_MAX];
500 char *sol;
501 int len = 0;
502 int suffix_len = strlen(fmt_patch_suffix) + 1;
504 sol = strstr(commit->buffer, "\n\n");
505 if (!sol)
506 filename[0] = '\0';
507 else {
508 int j, space = 0;
510 sol += 2;
511 /* strip [PATCH] or [PATCH blabla] */
512 if (!keep_subject && !prefixcmp(sol, "[PATCH")) {
513 char *eos = strchr(sol + 6, ']');
514 if (eos) {
515 while (isspace(*eos))
516 eos++;
517 sol = eos;
521 for (j = 0;
522 j < FORMAT_PATCH_NAME_MAX - suffix_len - 5 &&
523 len < sizeof(filename) - suffix_len &&
524 sol[j] && sol[j] != '\n';
525 j++) {
526 if (istitlechar(sol[j])) {
527 if (space) {
528 filename[len++] = '-';
529 space = 0;
531 filename[len++] = sol[j];
532 if (sol[j] == '.')
533 while (sol[j + 1] == '.')
534 j++;
535 } else
536 space = 1;
538 while (filename[len - 1] == '.'
539 || filename[len - 1] == '-')
540 len--;
541 filename[len] = '\0';
543 return filename;
546 static FILE *realstdout = NULL;
547 static const char *output_directory = NULL;
549 static int reopen_stdout(const char *oneline, int nr, int total)
551 char filename[PATH_MAX];
552 int len = 0;
553 int suffix_len = strlen(fmt_patch_suffix) + 1;
555 if (output_directory) {
556 len = snprintf(filename, sizeof(filename), "%s",
557 output_directory);
558 if (len >=
559 sizeof(filename) - FORMAT_PATCH_NAME_MAX - suffix_len)
560 return error("name of output directory is too long");
561 if (filename[len - 1] != '/')
562 filename[len++] = '/';
565 if (!oneline)
566 len += sprintf(filename + len, "%d", nr);
567 else {
568 len += sprintf(filename + len, "%04d-", nr);
569 len += snprintf(filename + len, sizeof(filename) - len - 1
570 - suffix_len, "%s", oneline);
571 strcpy(filename + len, fmt_patch_suffix);
574 fprintf(realstdout, "%s\n", filename);
575 if (freopen(filename, "w", stdout) == NULL)
576 return error("Cannot open patch file %s",filename);
578 return 0;
581 static void get_patch_ids(struct rev_info *rev, struct patch_ids *ids, const char *prefix)
583 struct rev_info check_rev;
584 struct commit *commit;
585 struct object *o1, *o2;
586 unsigned flags1, flags2;
588 if (rev->pending.nr != 2)
589 die("Need exactly one range.");
591 o1 = rev->pending.objects[0].item;
592 flags1 = o1->flags;
593 o2 = rev->pending.objects[1].item;
594 flags2 = o2->flags;
596 if ((flags1 & UNINTERESTING) == (flags2 & UNINTERESTING))
597 die("Not a range.");
599 init_patch_ids(ids);
601 /* given a range a..b get all patch ids for b..a */
602 init_revisions(&check_rev, prefix);
603 o1->flags ^= UNINTERESTING;
604 o2->flags ^= UNINTERESTING;
605 add_pending_object(&check_rev, o1, "o1");
606 add_pending_object(&check_rev, o2, "o2");
607 if (prepare_revision_walk(&check_rev))
608 die("revision walk setup failed");
610 while ((commit = get_revision(&check_rev)) != NULL) {
611 /* ignore merges */
612 if (commit->parents && commit->parents->next)
613 continue;
615 add_commit_patch_id(commit, ids);
618 /* reset for next revision walk */
619 clear_commit_marks((struct commit *)o1,
620 SEEN | UNINTERESTING | SHOWN | ADDED);
621 clear_commit_marks((struct commit *)o2,
622 SEEN | UNINTERESTING | SHOWN | ADDED);
623 o1->flags = flags1;
624 o2->flags = flags2;
627 static void gen_message_id(struct rev_info *info, char *base)
629 const char *committer = git_committer_info(IDENT_WARN_ON_NO_NAME);
630 const char *email_start = strrchr(committer, '<');
631 const char *email_end = strrchr(committer, '>');
632 struct strbuf buf = STRBUF_INIT;
633 if (!email_start || !email_end || email_start > email_end - 1)
634 die("Could not extract email from committer identity.");
635 strbuf_addf(&buf, "%s.%lu.git.%.*s", base,
636 (unsigned long) time(NULL),
637 (int)(email_end - email_start - 1), email_start + 1);
638 info->message_id = strbuf_detach(&buf, NULL);
641 static void make_cover_letter(struct rev_info *rev, int use_stdout,
642 int numbered, int numbered_files,
643 struct commit *origin,
644 int nr, struct commit **list, struct commit *head)
646 const char *committer;
647 char *head_sha1;
648 const char *subject_start = NULL;
649 const char *body = "*** SUBJECT HERE ***\n\n*** BLURB HERE ***\n";
650 const char *msg;
651 const char *extra_headers = rev->extra_headers;
652 struct shortlog log;
653 struct strbuf sb = STRBUF_INIT;
654 int i;
655 const char *encoding = "utf-8";
656 struct diff_options opts;
657 int need_8bit_cte = 0;
659 if (rev->commit_format != CMIT_FMT_EMAIL)
660 die("Cover letter needs email format");
662 if (!use_stdout && reopen_stdout(numbered_files ?
663 NULL : "cover-letter", 0, rev->total))
664 return;
666 head_sha1 = sha1_to_hex(head->object.sha1);
668 log_write_email_headers(rev, head_sha1, &subject_start, &extra_headers,
669 &need_8bit_cte);
671 committer = git_committer_info(0);
673 msg = body;
674 pp_user_info(NULL, CMIT_FMT_EMAIL, &sb, committer, DATE_RFC2822,
675 encoding);
676 pp_title_line(CMIT_FMT_EMAIL, &msg, &sb, subject_start, extra_headers,
677 encoding, need_8bit_cte);
678 pp_remainder(CMIT_FMT_EMAIL, &msg, &sb, 0);
679 printf("%s\n", sb.buf);
681 strbuf_release(&sb);
683 shortlog_init(&log);
684 log.wrap_lines = 1;
685 log.wrap = 72;
686 log.in1 = 2;
687 log.in2 = 4;
688 for (i = 0; i < nr; i++)
689 shortlog_add_commit(&log, list[i]);
691 shortlog_output(&log);
694 * We can only do diffstat with a unique reference point
696 if (!origin)
697 return;
699 memcpy(&opts, &rev->diffopt, sizeof(opts));
700 opts.output_format = DIFF_FORMAT_SUMMARY | DIFF_FORMAT_DIFFSTAT;
702 diff_setup_done(&opts);
704 diff_tree_sha1(origin->tree->object.sha1,
705 head->tree->object.sha1,
706 "", &opts);
707 diffcore_std(&opts);
708 diff_flush(&opts);
710 printf("\n");
713 static const char *clean_message_id(const char *msg_id)
715 char ch;
716 const char *a, *z, *m;
718 m = msg_id;
719 while ((ch = *m) && (isspace(ch) || (ch == '<')))
720 m++;
721 a = m;
722 z = NULL;
723 while ((ch = *m)) {
724 if (!isspace(ch) && (ch != '>'))
725 z = m;
726 m++;
728 if (!z)
729 die("insane in-reply-to: %s", msg_id);
730 if (++z == m)
731 return a;
732 return xmemdupz(a, z - a);
735 int cmd_format_patch(int argc, const char **argv, const char *prefix)
737 struct commit *commit;
738 struct commit **list = NULL;
739 struct rev_info rev;
740 int nr = 0, total, i, j;
741 int use_stdout = 0;
742 int start_number = -1;
743 int keep_subject = 0;
744 int numbered_files = 0; /* _just_ numbers */
745 int subject_prefix = 0;
746 int ignore_if_in_upstream = 0;
747 int thread = 0;
748 int cover_letter = 0;
749 int boundary_count = 0;
750 int no_binary_diff = 0;
751 struct commit *origin = NULL, *head = NULL;
752 const char *in_reply_to = NULL;
753 struct patch_ids ids;
754 char *add_signoff = NULL;
755 struct strbuf buf = STRBUF_INIT;
757 git_config(git_format_config, NULL);
758 init_revisions(&rev, prefix);
759 rev.commit_format = CMIT_FMT_EMAIL;
760 rev.verbose_header = 1;
761 rev.diff = 1;
762 rev.combine_merges = 0;
763 rev.ignore_merges = 1;
764 DIFF_OPT_SET(&rev.diffopt, RECURSIVE);
766 rev.subject_prefix = fmt_patch_subject_prefix;
769 * Parse the arguments before setup_revisions(), or something
770 * like "git format-patch -o a123 HEAD^.." may fail; a123 is
771 * possibly a valid SHA1.
773 for (i = 1, j = 1; i < argc; i++) {
774 if (!strcmp(argv[i], "--stdout"))
775 use_stdout = 1;
776 else if (!strcmp(argv[i], "-n") ||
777 !strcmp(argv[i], "--numbered"))
778 numbered = 1;
779 else if (!strcmp(argv[i], "-N") ||
780 !strcmp(argv[i], "--no-numbered")) {
781 numbered = 0;
782 auto_number = 0;
784 else if (!prefixcmp(argv[i], "--start-number="))
785 start_number = strtol(argv[i] + 15, NULL, 10);
786 else if (!strcmp(argv[i], "--numbered-files"))
787 numbered_files = 1;
788 else if (!strcmp(argv[i], "--start-number")) {
789 i++;
790 if (i == argc)
791 die("Need a number for --start-number");
792 start_number = strtol(argv[i], NULL, 10);
794 else if (!prefixcmp(argv[i], "--cc=")) {
795 ALLOC_GROW(extra_cc, extra_cc_nr + 1, extra_cc_alloc);
796 extra_cc[extra_cc_nr++] = xstrdup(argv[i] + 5);
798 else if (!strcmp(argv[i], "-k") ||
799 !strcmp(argv[i], "--keep-subject")) {
800 keep_subject = 1;
801 rev.total = -1;
803 else if (!strcmp(argv[i], "--output-directory") ||
804 !strcmp(argv[i], "-o")) {
805 i++;
806 if (argc <= i)
807 die("Which directory?");
808 if (output_directory)
809 die("Two output directories?");
810 output_directory = argv[i];
812 else if (!strcmp(argv[i], "--signoff") ||
813 !strcmp(argv[i], "-s")) {
814 const char *committer;
815 const char *endpos;
816 committer = git_committer_info(IDENT_ERROR_ON_NO_NAME);
817 endpos = strchr(committer, '>');
818 if (!endpos)
819 die("bogus committer info %s", committer);
820 add_signoff = xmemdupz(committer, endpos - committer + 1);
822 else if (!strcmp(argv[i], "--attach")) {
823 rev.mime_boundary = git_version_string;
824 rev.no_inline = 1;
826 else if (!prefixcmp(argv[i], "--attach=")) {
827 rev.mime_boundary = argv[i] + 9;
828 rev.no_inline = 1;
830 else if (!strcmp(argv[i], "--inline")) {
831 rev.mime_boundary = git_version_string;
832 rev.no_inline = 0;
834 else if (!prefixcmp(argv[i], "--inline=")) {
835 rev.mime_boundary = argv[i] + 9;
836 rev.no_inline = 0;
838 else if (!strcmp(argv[i], "--ignore-if-in-upstream"))
839 ignore_if_in_upstream = 1;
840 else if (!strcmp(argv[i], "--thread"))
841 thread = 1;
842 else if (!prefixcmp(argv[i], "--in-reply-to="))
843 in_reply_to = argv[i] + 14;
844 else if (!strcmp(argv[i], "--in-reply-to")) {
845 i++;
846 if (i == argc)
847 die("Need a Message-Id for --in-reply-to");
848 in_reply_to = argv[i];
849 } else if (!prefixcmp(argv[i], "--subject-prefix=")) {
850 subject_prefix = 1;
851 rev.subject_prefix = argv[i] + 17;
852 } else if (!prefixcmp(argv[i], "--suffix="))
853 fmt_patch_suffix = argv[i] + 9;
854 else if (!strcmp(argv[i], "--cover-letter"))
855 cover_letter = 1;
856 else if (!strcmp(argv[i], "--no-binary"))
857 no_binary_diff = 1;
858 else
859 argv[j++] = argv[i];
861 argc = j;
863 for (i = 0; i < extra_hdr_nr; i++) {
864 strbuf_addstr(&buf, extra_hdr[i]);
865 strbuf_addch(&buf, '\n');
868 if (extra_to_nr)
869 strbuf_addstr(&buf, "To: ");
870 for (i = 0; i < extra_to_nr; i++) {
871 if (i)
872 strbuf_addstr(&buf, " ");
873 strbuf_addstr(&buf, extra_to[i]);
874 if (i + 1 < extra_to_nr)
875 strbuf_addch(&buf, ',');
876 strbuf_addch(&buf, '\n');
879 if (extra_cc_nr)
880 strbuf_addstr(&buf, "Cc: ");
881 for (i = 0; i < extra_cc_nr; i++) {
882 if (i)
883 strbuf_addstr(&buf, " ");
884 strbuf_addstr(&buf, extra_cc[i]);
885 if (i + 1 < extra_cc_nr)
886 strbuf_addch(&buf, ',');
887 strbuf_addch(&buf, '\n');
890 rev.extra_headers = strbuf_detach(&buf, 0);
892 if (start_number < 0)
893 start_number = 1;
894 if (numbered && keep_subject)
895 die ("-n and -k are mutually exclusive.");
896 if (keep_subject && subject_prefix)
897 die ("--subject-prefix and -k are mutually exclusive.");
898 if (numbered_files && use_stdout)
899 die ("--numbered-files and --stdout are mutually exclusive.");
901 argc = setup_revisions(argc, argv, &rev, "HEAD");
902 if (argc > 1)
903 die ("unrecognized argument: %s", argv[1]);
905 if (!rev.diffopt.output_format
906 || rev.diffopt.output_format == DIFF_FORMAT_PATCH)
907 rev.diffopt.output_format = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_SUMMARY | DIFF_FORMAT_PATCH;
909 if (!DIFF_OPT_TST(&rev.diffopt, TEXT) && !no_binary_diff)
910 DIFF_OPT_SET(&rev.diffopt, BINARY);
912 if (!output_directory && !use_stdout)
913 output_directory = prefix;
915 if (output_directory) {
916 if (use_stdout)
917 die("standard output, or directory, which one?");
918 if (mkdir(output_directory, 0777) < 0 && errno != EEXIST)
919 die("Could not create directory %s",
920 output_directory);
923 if (rev.pending.nr == 1) {
924 if (rev.max_count < 0 && !rev.show_root_diff) {
926 * This is traditional behaviour of "git format-patch
927 * origin" that prepares what the origin side still
928 * does not have.
930 rev.pending.objects[0].item->flags |= UNINTERESTING;
931 add_head_to_pending(&rev);
934 * Otherwise, it is "format-patch -22 HEAD", and/or
935 * "format-patch --root HEAD". The user wants
936 * get_revision() to do the usual traversal.
941 * We cannot move this anywhere earlier because we do want to
942 * know if --root was given explicitly from the comand line.
944 rev.show_root_diff = 1;
946 if (cover_letter) {
947 /* remember the range */
948 int i;
949 for (i = 0; i < rev.pending.nr; i++) {
950 struct object *o = rev.pending.objects[i].item;
951 if (!(o->flags & UNINTERESTING))
952 head = (struct commit *)o;
954 /* We can't generate a cover letter without any patches */
955 if (!head)
956 return 0;
959 if (ignore_if_in_upstream)
960 get_patch_ids(&rev, &ids, prefix);
962 if (!use_stdout)
963 realstdout = xfdopen(xdup(1), "w");
965 if (prepare_revision_walk(&rev))
966 die("revision walk setup failed");
967 rev.boundary = 1;
968 while ((commit = get_revision(&rev)) != NULL) {
969 if (commit->object.flags & BOUNDARY) {
970 boundary_count++;
971 origin = (boundary_count == 1) ? commit : NULL;
972 continue;
975 /* ignore merges */
976 if (commit->parents && commit->parents->next)
977 continue;
979 if (ignore_if_in_upstream &&
980 has_commit_patch_id(commit, &ids))
981 continue;
983 nr++;
984 list = xrealloc(list, nr * sizeof(list[0]));
985 list[nr - 1] = commit;
987 total = nr;
988 if (!keep_subject && auto_number && total > 1)
989 numbered = 1;
990 if (numbered)
991 rev.total = total + start_number - 1;
992 if (in_reply_to)
993 rev.ref_message_id = clean_message_id(in_reply_to);
994 if (cover_letter) {
995 if (thread)
996 gen_message_id(&rev, "cover");
997 make_cover_letter(&rev, use_stdout, numbered, numbered_files,
998 origin, nr, list, head);
999 total++;
1000 start_number--;
1002 rev.add_signoff = add_signoff;
1003 while (0 <= --nr) {
1004 int shown;
1005 commit = list[nr];
1006 rev.nr = total - nr + (start_number - 1);
1007 /* Make the second and subsequent mails replies to the first */
1008 if (thread) {
1009 /* Have we already had a message ID? */
1010 if (rev.message_id) {
1012 * If we've got the ID to be a reply
1013 * to, discard the current ID;
1014 * otherwise, make everything a reply
1015 * to that.
1017 if (rev.ref_message_id)
1018 free(rev.message_id);
1019 else
1020 rev.ref_message_id = rev.message_id;
1022 gen_message_id(&rev, sha1_to_hex(commit->object.sha1));
1024 if (!use_stdout && reopen_stdout(numbered_files ? NULL :
1025 get_oneline_for_filename(commit, keep_subject),
1026 rev.nr, rev.total))
1027 die("Failed to create output files");
1028 shown = log_tree_commit(&rev, commit);
1029 free(commit->buffer);
1030 commit->buffer = NULL;
1032 /* We put one extra blank line between formatted
1033 * patches and this flag is used by log-tree code
1034 * to see if it needs to emit a LF before showing
1035 * the log; when using one file per patch, we do
1036 * not want the extra blank line.
1038 if (!use_stdout)
1039 rev.shown_one = 0;
1040 if (shown) {
1041 if (rev.mime_boundary)
1042 printf("\n--%s%s--\n\n\n",
1043 mime_boundary_leader,
1044 rev.mime_boundary);
1045 else
1046 printf("-- \n%s\n\n", git_version_string);
1048 if (!use_stdout)
1049 fclose(stdout);
1051 free(list);
1052 if (ignore_if_in_upstream)
1053 free_patch_ids(&ids);
1054 return 0;
1057 static int add_pending_commit(const char *arg, struct rev_info *revs, int flags)
1059 unsigned char sha1[20];
1060 if (get_sha1(arg, sha1) == 0) {
1061 struct commit *commit = lookup_commit_reference(sha1);
1062 if (commit) {
1063 commit->object.flags |= flags;
1064 add_pending_object(revs, &commit->object, arg);
1065 return 0;
1068 return -1;
1071 static const char cherry_usage[] =
1072 "git cherry [-v] [<upstream> [<head> [<limit>]]]";
1073 int cmd_cherry(int argc, const char **argv, const char *prefix)
1075 struct rev_info revs;
1076 struct patch_ids ids;
1077 struct commit *commit;
1078 struct commit_list *list = NULL;
1079 struct branch *current_branch;
1080 const char *upstream;
1081 const char *head = "HEAD";
1082 const char *limit = NULL;
1083 int verbose = 0;
1085 if (argc > 1 && !strcmp(argv[1], "-v")) {
1086 verbose = 1;
1087 argc--;
1088 argv++;
1091 switch (argc) {
1092 case 4:
1093 limit = argv[3];
1094 /* FALLTHROUGH */
1095 case 3:
1096 head = argv[2];
1097 /* FALLTHROUGH */
1098 case 2:
1099 upstream = argv[1];
1100 break;
1101 default:
1102 current_branch = branch_get(NULL);
1103 if (!current_branch || !current_branch->merge
1104 || !current_branch->merge[0]
1105 || !current_branch->merge[0]->dst) {
1106 fprintf(stderr, "Could not find a tracked"
1107 " remote branch, please"
1108 " specify <upstream> manually.\n");
1109 usage(cherry_usage);
1112 upstream = current_branch->merge[0]->dst;
1115 init_revisions(&revs, prefix);
1116 revs.diff = 1;
1117 revs.combine_merges = 0;
1118 revs.ignore_merges = 1;
1119 DIFF_OPT_SET(&revs.diffopt, RECURSIVE);
1121 if (add_pending_commit(head, &revs, 0))
1122 die("Unknown commit %s", head);
1123 if (add_pending_commit(upstream, &revs, UNINTERESTING))
1124 die("Unknown commit %s", upstream);
1126 /* Don't say anything if head and upstream are the same. */
1127 if (revs.pending.nr == 2) {
1128 struct object_array_entry *o = revs.pending.objects;
1129 if (hashcmp(o[0].item->sha1, o[1].item->sha1) == 0)
1130 return 0;
1133 get_patch_ids(&revs, &ids, prefix);
1135 if (limit && add_pending_commit(limit, &revs, UNINTERESTING))
1136 die("Unknown commit %s", limit);
1138 /* reverse the list of commits */
1139 if (prepare_revision_walk(&revs))
1140 die("revision walk setup failed");
1141 while ((commit = get_revision(&revs)) != NULL) {
1142 /* ignore merges */
1143 if (commit->parents && commit->parents->next)
1144 continue;
1146 commit_list_insert(commit, &list);
1149 while (list) {
1150 char sign = '+';
1152 commit = list->item;
1153 if (has_commit_patch_id(commit, &ids))
1154 sign = '-';
1156 if (verbose) {
1157 struct strbuf buf = STRBUF_INIT;
1158 pretty_print_commit(CMIT_FMT_ONELINE, commit,
1159 &buf, 0, NULL, NULL, 0, 0);
1160 printf("%c %s %s\n", sign,
1161 sha1_to_hex(commit->object.sha1), buf.buf);
1162 strbuf_release(&buf);
1164 else {
1165 printf("%c %s\n", sign,
1166 sha1_to_hex(commit->object.sha1));
1169 list = list->next;
1172 free_patch_ids(&ids);
1173 return 0;