log: add load_ref_decorations()
[git/jnareb-git.git] / builtin-log.c
blob0f164620b9b7b9d0df088f101b725cee632272a4
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 void load_ref_decorations(void)
55 static int loaded;
56 if (!loaded) {
57 loaded = 1;
58 for_each_ref(add_ref_decoration, NULL);
62 static void cmd_log_init(int argc, const char **argv, const char *prefix,
63 struct rev_info *rev)
65 int i;
66 int decorate = 0;
68 rev->abbrev = DEFAULT_ABBREV;
69 rev->commit_format = CMIT_FMT_DEFAULT;
70 if (fmt_pretty)
71 get_commit_format(fmt_pretty, rev);
72 rev->verbose_header = 1;
73 DIFF_OPT_SET(&rev->diffopt, RECURSIVE);
74 rev->show_root_diff = default_show_root;
75 rev->subject_prefix = fmt_patch_subject_prefix;
77 if (default_date_mode)
78 rev->date_mode = parse_date_format(default_date_mode);
80 argc = setup_revisions(argc, argv, rev, "HEAD");
82 if (rev->diffopt.pickaxe || rev->diffopt.filter)
83 rev->always_show_header = 0;
84 if (DIFF_OPT_TST(&rev->diffopt, FOLLOW_RENAMES)) {
85 rev->always_show_header = 0;
86 if (rev->diffopt.nr_paths != 1)
87 usage("git logs can only follow renames on one pathname at a time");
89 for (i = 1; i < argc; i++) {
90 const char *arg = argv[i];
91 if (!strcmp(arg, "--decorate")) {
92 load_ref_decorations();
93 decorate = 1;
94 } else
95 die("unrecognized argument: %s", arg);
100 * This gives a rough estimate for how many commits we
101 * will print out in the list.
103 static int estimate_commit_count(struct rev_info *rev, struct commit_list *list)
105 int n = 0;
107 while (list) {
108 struct commit *commit = list->item;
109 unsigned int flags = commit->object.flags;
110 list = list->next;
111 if (!(flags & (TREESAME | UNINTERESTING)))
112 n++;
114 return n;
117 static void show_early_header(struct rev_info *rev, const char *stage, int nr)
119 if (rev->shown_one) {
120 rev->shown_one = 0;
121 if (rev->commit_format != CMIT_FMT_ONELINE)
122 putchar(rev->diffopt.line_termination);
124 printf("Final output: %d %s\n", nr, stage);
127 struct itimerval early_output_timer;
129 static void log_show_early(struct rev_info *revs, struct commit_list *list)
131 int i = revs->early_output;
132 int show_header = 1;
134 sort_in_topological_order(&list, revs->lifo);
135 while (list && i) {
136 struct commit *commit = list->item;
137 switch (simplify_commit(revs, commit)) {
138 case commit_show:
139 if (show_header) {
140 int n = estimate_commit_count(revs, list);
141 show_early_header(revs, "incomplete", n);
142 show_header = 0;
144 log_tree_commit(revs, commit);
145 i--;
146 break;
147 case commit_ignore:
148 break;
149 case commit_error:
150 return;
152 list = list->next;
155 /* Did we already get enough commits for the early output? */
156 if (!i)
157 return;
160 * ..if no, then repeat it twice a second until we
161 * do.
163 * NOTE! We don't use "it_interval", because if the
164 * reader isn't listening, we want our output to be
165 * throttled by the writing, and not have the timer
166 * trigger every second even if we're blocked on a
167 * reader!
169 early_output_timer.it_value.tv_sec = 0;
170 early_output_timer.it_value.tv_usec = 500000;
171 setitimer(ITIMER_REAL, &early_output_timer, NULL);
174 static void early_output(int signal)
176 show_early_output = log_show_early;
179 static void setup_early_output(struct rev_info *rev)
181 struct sigaction sa;
184 * Set up the signal handler, minimally intrusively:
185 * we only set a single volatile integer word (not
186 * using sigatomic_t - trying to avoid unnecessary
187 * system dependencies and headers), and using
188 * SA_RESTART.
190 memset(&sa, 0, sizeof(sa));
191 sa.sa_handler = early_output;
192 sigemptyset(&sa.sa_mask);
193 sa.sa_flags = SA_RESTART;
194 sigaction(SIGALRM, &sa, NULL);
197 * If we can get the whole output in less than a
198 * tenth of a second, don't even bother doing the
199 * early-output thing..
201 * This is a one-time-only trigger.
203 early_output_timer.it_value.tv_sec = 0;
204 early_output_timer.it_value.tv_usec = 100000;
205 setitimer(ITIMER_REAL, &early_output_timer, NULL);
208 static void finish_early_output(struct rev_info *rev)
210 int n = estimate_commit_count(rev, rev->commits);
211 signal(SIGALRM, SIG_IGN);
212 show_early_header(rev, "done", n);
215 static int cmd_log_walk(struct rev_info *rev)
217 struct commit *commit;
219 if (rev->early_output)
220 setup_early_output(rev);
222 if (prepare_revision_walk(rev))
223 die("revision walk setup failed");
225 if (rev->early_output)
226 finish_early_output(rev);
229 * For --check and --exit-code, the exit code is based on CHECK_FAILED
230 * and HAS_CHANGES being accumulated in rev->diffopt, so be careful to
231 * retain that state information if replacing rev->diffopt in this loop
233 while ((commit = get_revision(rev)) != NULL) {
234 log_tree_commit(rev, commit);
235 if (!rev->reflog_info) {
236 /* we allow cycles in reflog ancestry */
237 free(commit->buffer);
238 commit->buffer = NULL;
240 free_commit_list(commit->parents);
241 commit->parents = NULL;
243 if (rev->diffopt.output_format & DIFF_FORMAT_CHECKDIFF &&
244 DIFF_OPT_TST(&rev->diffopt, CHECK_FAILED)) {
245 return 02;
247 return diff_result_code(&rev->diffopt, 0);
250 static int git_log_config(const char *var, const char *value, void *cb)
252 if (!strcmp(var, "format.pretty"))
253 return git_config_string(&fmt_pretty, var, value);
254 if (!strcmp(var, "format.subjectprefix"))
255 return git_config_string(&fmt_patch_subject_prefix, var, value);
256 if (!strcmp(var, "log.date"))
257 return git_config_string(&default_date_mode, var, value);
258 if (!strcmp(var, "log.showroot")) {
259 default_show_root = git_config_bool(var, value);
260 return 0;
262 return git_diff_ui_config(var, value, cb);
265 int cmd_whatchanged(int argc, const char **argv, const char *prefix)
267 struct rev_info rev;
269 git_config(git_log_config, NULL);
271 if (diff_use_color_default == -1)
272 diff_use_color_default = git_use_color_default;
274 init_revisions(&rev, prefix);
275 rev.diff = 1;
276 rev.simplify_history = 0;
277 cmd_log_init(argc, argv, prefix, &rev);
278 if (!rev.diffopt.output_format)
279 rev.diffopt.output_format = DIFF_FORMAT_RAW;
280 return cmd_log_walk(&rev);
283 static void show_tagger(char *buf, int len, struct rev_info *rev)
285 char *email_end, *p;
286 unsigned long date;
287 int tz;
289 email_end = memchr(buf, '>', len);
290 if (!email_end)
291 return;
292 p = ++email_end;
293 while (isspace(*p))
294 p++;
295 date = strtoul(p, &p, 10);
296 while (isspace(*p))
297 p++;
298 tz = (int)strtol(p, NULL, 10);
299 printf("Tagger: %.*s\nDate: %s\n", (int)(email_end - buf), buf,
300 show_date(date, tz, rev->date_mode));
303 static int show_object(const unsigned char *sha1, int show_tag_object,
304 struct rev_info *rev)
306 unsigned long size;
307 enum object_type type;
308 char *buf = read_sha1_file(sha1, &type, &size);
309 int offset = 0;
311 if (!buf)
312 return error("Could not read object %s", sha1_to_hex(sha1));
314 if (show_tag_object)
315 while (offset < size && buf[offset] != '\n') {
316 int new_offset = offset + 1;
317 while (new_offset < size && buf[new_offset++] != '\n')
318 ; /* do nothing */
319 if (!prefixcmp(buf + offset, "tagger "))
320 show_tagger(buf + offset + 7,
321 new_offset - offset - 7, rev);
322 offset = new_offset;
325 if (offset < size)
326 fwrite(buf + offset, size - offset, 1, stdout);
327 free(buf);
328 return 0;
331 static int show_tree_object(const unsigned char *sha1,
332 const char *base, int baselen,
333 const char *pathname, unsigned mode, int stage, void *context)
335 printf("%s%s\n", pathname, S_ISDIR(mode) ? "/" : "");
336 return 0;
339 int cmd_show(int argc, const char **argv, const char *prefix)
341 struct rev_info rev;
342 struct object_array_entry *objects;
343 int i, count, ret = 0;
345 git_config(git_log_config, NULL);
347 if (diff_use_color_default == -1)
348 diff_use_color_default = git_use_color_default;
350 init_revisions(&rev, prefix);
351 rev.diff = 1;
352 rev.combine_merges = 1;
353 rev.dense_combined_merges = 1;
354 rev.always_show_header = 1;
355 rev.ignore_merges = 0;
356 rev.no_walk = 1;
357 cmd_log_init(argc, argv, prefix, &rev);
359 count = rev.pending.nr;
360 objects = rev.pending.objects;
361 for (i = 0; i < count && !ret; i++) {
362 struct object *o = objects[i].item;
363 const char *name = objects[i].name;
364 switch (o->type) {
365 case OBJ_BLOB:
366 ret = show_object(o->sha1, 0, NULL);
367 break;
368 case OBJ_TAG: {
369 struct tag *t = (struct tag *)o;
371 printf("%stag %s%s\n",
372 diff_get_color_opt(&rev.diffopt, DIFF_COMMIT),
373 t->tag,
374 diff_get_color_opt(&rev.diffopt, DIFF_RESET));
375 ret = show_object(o->sha1, 1, &rev);
376 objects[i].item = parse_object(t->tagged->sha1);
377 i--;
378 break;
380 case OBJ_TREE:
381 printf("%stree %s%s\n\n",
382 diff_get_color_opt(&rev.diffopt, DIFF_COMMIT),
383 name,
384 diff_get_color_opt(&rev.diffopt, DIFF_RESET));
385 read_tree_recursive((struct tree *)o, "", 0, 0, NULL,
386 show_tree_object, NULL);
387 break;
388 case OBJ_COMMIT:
389 rev.pending.nr = rev.pending.alloc = 0;
390 rev.pending.objects = NULL;
391 add_object_array(o, name, &rev.pending);
392 ret = cmd_log_walk(&rev);
393 break;
394 default:
395 ret = error("Unknown type: %d", o->type);
398 free(objects);
399 return ret;
403 * This is equivalent to "git log -g --abbrev-commit --pretty=oneline"
405 int cmd_log_reflog(int argc, const char **argv, const char *prefix)
407 struct rev_info rev;
409 git_config(git_log_config, NULL);
411 if (diff_use_color_default == -1)
412 diff_use_color_default = git_use_color_default;
414 init_revisions(&rev, prefix);
415 init_reflog_walk(&rev.reflog_info);
416 rev.abbrev_commit = 1;
417 rev.verbose_header = 1;
418 cmd_log_init(argc, argv, prefix, &rev);
421 * This means that we override whatever commit format the user gave
422 * on the cmd line. Sad, but cmd_log_init() currently doesn't
423 * allow us to set a different default.
425 rev.commit_format = CMIT_FMT_ONELINE;
426 rev.use_terminator = 1;
427 rev.always_show_header = 1;
430 * We get called through "git reflog", so unlike the other log
431 * routines, we need to set up our pager manually..
433 setup_pager();
435 return cmd_log_walk(&rev);
438 int cmd_log(int argc, const char **argv, const char *prefix)
440 struct rev_info rev;
442 git_config(git_log_config, NULL);
444 if (diff_use_color_default == -1)
445 diff_use_color_default = git_use_color_default;
447 init_revisions(&rev, prefix);
448 rev.always_show_header = 1;
449 cmd_log_init(argc, argv, prefix, &rev);
450 return cmd_log_walk(&rev);
453 /* format-patch */
454 #define FORMAT_PATCH_NAME_MAX 64
456 static int istitlechar(char c)
458 return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
459 (c >= '0' && c <= '9') || c == '.' || c == '_';
462 static const char *fmt_patch_suffix = ".patch";
463 static int numbered = 0;
464 static int auto_number = 0;
466 static char **extra_hdr;
467 static int extra_hdr_nr;
468 static int extra_hdr_alloc;
470 static char **extra_to;
471 static int extra_to_nr;
472 static int extra_to_alloc;
474 static char **extra_cc;
475 static int extra_cc_nr;
476 static int extra_cc_alloc;
478 static void add_header(const char *value)
480 int len = strlen(value);
481 while (len && value[len - 1] == '\n')
482 len--;
483 if (!strncasecmp(value, "to: ", 4)) {
484 ALLOC_GROW(extra_to, extra_to_nr + 1, extra_to_alloc);
485 extra_to[extra_to_nr++] = xstrndup(value + 4, len - 4);
486 return;
488 if (!strncasecmp(value, "cc: ", 4)) {
489 ALLOC_GROW(extra_cc, extra_cc_nr + 1, extra_cc_alloc);
490 extra_cc[extra_cc_nr++] = xstrndup(value + 4, len - 4);
491 return;
493 ALLOC_GROW(extra_hdr, extra_hdr_nr + 1, extra_hdr_alloc);
494 extra_hdr[extra_hdr_nr++] = xstrndup(value, len);
497 static int git_format_config(const char *var, const char *value, void *cb)
499 if (!strcmp(var, "format.headers")) {
500 if (!value)
501 die("format.headers without value");
502 add_header(value);
503 return 0;
505 if (!strcmp(var, "format.suffix"))
506 return git_config_string(&fmt_patch_suffix, var, value);
507 if (!strcmp(var, "format.cc")) {
508 if (!value)
509 return config_error_nonbool(var);
510 ALLOC_GROW(extra_cc, extra_cc_nr + 1, extra_cc_alloc);
511 extra_cc[extra_cc_nr++] = xstrdup(value);
512 return 0;
514 if (!strcmp(var, "diff.color") || !strcmp(var, "color.diff")) {
515 return 0;
517 if (!strcmp(var, "format.numbered")) {
518 if (value && !strcasecmp(value, "auto")) {
519 auto_number = 1;
520 return 0;
522 numbered = git_config_bool(var, value);
523 return 0;
526 return git_log_config(var, value, cb);
530 static const char *get_oneline_for_filename(struct commit *commit,
531 int keep_subject)
533 static char filename[PATH_MAX];
534 char *sol;
535 int len = 0;
536 int suffix_len = strlen(fmt_patch_suffix) + 1;
538 sol = strstr(commit->buffer, "\n\n");
539 if (!sol)
540 filename[0] = '\0';
541 else {
542 int j, space = 0;
544 sol += 2;
545 /* strip [PATCH] or [PATCH blabla] */
546 if (!keep_subject && !prefixcmp(sol, "[PATCH")) {
547 char *eos = strchr(sol + 6, ']');
548 if (eos) {
549 while (isspace(*eos))
550 eos++;
551 sol = eos;
555 for (j = 0;
556 j < FORMAT_PATCH_NAME_MAX - suffix_len - 5 &&
557 len < sizeof(filename) - suffix_len &&
558 sol[j] && sol[j] != '\n';
559 j++) {
560 if (istitlechar(sol[j])) {
561 if (space) {
562 filename[len++] = '-';
563 space = 0;
565 filename[len++] = sol[j];
566 if (sol[j] == '.')
567 while (sol[j + 1] == '.')
568 j++;
569 } else
570 space = 1;
572 while (filename[len - 1] == '.'
573 || filename[len - 1] == '-')
574 len--;
575 filename[len] = '\0';
577 return filename;
580 static FILE *realstdout = NULL;
581 static const char *output_directory = NULL;
583 static int reopen_stdout(const char *oneline, int nr, int total)
585 char filename[PATH_MAX];
586 int len = 0;
587 int suffix_len = strlen(fmt_patch_suffix) + 1;
589 if (output_directory) {
590 len = snprintf(filename, sizeof(filename), "%s",
591 output_directory);
592 if (len >=
593 sizeof(filename) - FORMAT_PATCH_NAME_MAX - suffix_len)
594 return error("name of output directory is too long");
595 if (filename[len - 1] != '/')
596 filename[len++] = '/';
599 if (!oneline)
600 len += sprintf(filename + len, "%d", nr);
601 else {
602 len += sprintf(filename + len, "%04d-", nr);
603 len += snprintf(filename + len, sizeof(filename) - len - 1
604 - suffix_len, "%s", oneline);
605 strcpy(filename + len, fmt_patch_suffix);
608 fprintf(realstdout, "%s\n", filename);
609 if (freopen(filename, "w", stdout) == NULL)
610 return error("Cannot open patch file %s",filename);
612 return 0;
615 static void get_patch_ids(struct rev_info *rev, struct patch_ids *ids, const char *prefix)
617 struct rev_info check_rev;
618 struct commit *commit;
619 struct object *o1, *o2;
620 unsigned flags1, flags2;
622 if (rev->pending.nr != 2)
623 die("Need exactly one range.");
625 o1 = rev->pending.objects[0].item;
626 flags1 = o1->flags;
627 o2 = rev->pending.objects[1].item;
628 flags2 = o2->flags;
630 if ((flags1 & UNINTERESTING) == (flags2 & UNINTERESTING))
631 die("Not a range.");
633 init_patch_ids(ids);
635 /* given a range a..b get all patch ids for b..a */
636 init_revisions(&check_rev, prefix);
637 o1->flags ^= UNINTERESTING;
638 o2->flags ^= UNINTERESTING;
639 add_pending_object(&check_rev, o1, "o1");
640 add_pending_object(&check_rev, o2, "o2");
641 if (prepare_revision_walk(&check_rev))
642 die("revision walk setup failed");
644 while ((commit = get_revision(&check_rev)) != NULL) {
645 /* ignore merges */
646 if (commit->parents && commit->parents->next)
647 continue;
649 add_commit_patch_id(commit, ids);
652 /* reset for next revision walk */
653 clear_commit_marks((struct commit *)o1,
654 SEEN | UNINTERESTING | SHOWN | ADDED);
655 clear_commit_marks((struct commit *)o2,
656 SEEN | UNINTERESTING | SHOWN | ADDED);
657 o1->flags = flags1;
658 o2->flags = flags2;
661 static void gen_message_id(struct rev_info *info, char *base)
663 const char *committer = git_committer_info(IDENT_WARN_ON_NO_NAME);
664 const char *email_start = strrchr(committer, '<');
665 const char *email_end = strrchr(committer, '>');
666 struct strbuf buf;
667 if (!email_start || !email_end || email_start > email_end - 1)
668 die("Could not extract email from committer identity.");
669 strbuf_init(&buf, 0);
670 strbuf_addf(&buf, "%s.%lu.git.%.*s", base,
671 (unsigned long) time(NULL),
672 (int)(email_end - email_start - 1), email_start + 1);
673 info->message_id = strbuf_detach(&buf, NULL);
676 static void make_cover_letter(struct rev_info *rev, int use_stdout,
677 int numbered, int numbered_files,
678 struct commit *origin,
679 int nr, struct commit **list, struct commit *head)
681 const char *committer;
682 char *head_sha1;
683 const char *subject_start = NULL;
684 const char *body = "*** SUBJECT HERE ***\n\n*** BLURB HERE ***\n";
685 const char *msg;
686 const char *extra_headers = rev->extra_headers;
687 struct shortlog log;
688 struct strbuf sb;
689 int i;
690 const char *encoding = "utf-8";
691 struct diff_options opts;
692 int need_8bit_cte = 0;
694 if (rev->commit_format != CMIT_FMT_EMAIL)
695 die("Cover letter needs email format");
697 if (!use_stdout && reopen_stdout(numbered_files ?
698 NULL : "cover-letter", 0, rev->total))
699 return;
701 head_sha1 = sha1_to_hex(head->object.sha1);
703 log_write_email_headers(rev, head_sha1, &subject_start, &extra_headers,
704 &need_8bit_cte);
706 committer = git_committer_info(0);
708 msg = body;
709 strbuf_init(&sb, 0);
710 pp_user_info(NULL, CMIT_FMT_EMAIL, &sb, committer, DATE_RFC2822,
711 encoding);
712 pp_title_line(CMIT_FMT_EMAIL, &msg, &sb, subject_start, extra_headers,
713 encoding, need_8bit_cte);
714 pp_remainder(CMIT_FMT_EMAIL, &msg, &sb, 0);
715 printf("%s\n", sb.buf);
717 strbuf_release(&sb);
719 shortlog_init(&log);
720 log.wrap_lines = 1;
721 log.wrap = 72;
722 log.in1 = 2;
723 log.in2 = 4;
724 for (i = 0; i < nr; i++)
725 shortlog_add_commit(&log, list[i]);
727 shortlog_output(&log);
730 * We can only do diffstat with a unique reference point
732 if (!origin)
733 return;
735 memcpy(&opts, &rev->diffopt, sizeof(opts));
736 opts.output_format = DIFF_FORMAT_SUMMARY | DIFF_FORMAT_DIFFSTAT;
738 diff_setup_done(&opts);
740 diff_tree_sha1(origin->tree->object.sha1,
741 head->tree->object.sha1,
742 "", &opts);
743 diffcore_std(&opts);
744 diff_flush(&opts);
746 printf("\n");
749 static const char *clean_message_id(const char *msg_id)
751 char ch;
752 const char *a, *z, *m;
754 m = msg_id;
755 while ((ch = *m) && (isspace(ch) || (ch == '<')))
756 m++;
757 a = m;
758 z = NULL;
759 while ((ch = *m)) {
760 if (!isspace(ch) && (ch != '>'))
761 z = m;
762 m++;
764 if (!z)
765 die("insane in-reply-to: %s", msg_id);
766 if (++z == m)
767 return a;
768 return xmemdupz(a, z - a);
771 int cmd_format_patch(int argc, const char **argv, const char *prefix)
773 struct commit *commit;
774 struct commit **list = NULL;
775 struct rev_info rev;
776 int nr = 0, total, i, j;
777 int use_stdout = 0;
778 int start_number = -1;
779 int keep_subject = 0;
780 int numbered_files = 0; /* _just_ numbers */
781 int subject_prefix = 0;
782 int ignore_if_in_upstream = 0;
783 int thread = 0;
784 int cover_letter = 0;
785 int boundary_count = 0;
786 int no_binary_diff = 0;
787 struct commit *origin = NULL, *head = NULL;
788 const char *in_reply_to = NULL;
789 struct patch_ids ids;
790 char *add_signoff = NULL;
791 struct strbuf buf;
793 git_config(git_format_config, NULL);
794 init_revisions(&rev, prefix);
795 rev.commit_format = CMIT_FMT_EMAIL;
796 rev.verbose_header = 1;
797 rev.diff = 1;
798 rev.combine_merges = 0;
799 rev.ignore_merges = 1;
800 DIFF_OPT_SET(&rev.diffopt, RECURSIVE);
802 rev.subject_prefix = fmt_patch_subject_prefix;
805 * Parse the arguments before setup_revisions(), or something
806 * like "git format-patch -o a123 HEAD^.." may fail; a123 is
807 * possibly a valid SHA1.
809 for (i = 1, j = 1; i < argc; i++) {
810 if (!strcmp(argv[i], "--stdout"))
811 use_stdout = 1;
812 else if (!strcmp(argv[i], "-n") ||
813 !strcmp(argv[i], "--numbered"))
814 numbered = 1;
815 else if (!strcmp(argv[i], "-N") ||
816 !strcmp(argv[i], "--no-numbered")) {
817 numbered = 0;
818 auto_number = 0;
820 else if (!prefixcmp(argv[i], "--start-number="))
821 start_number = strtol(argv[i] + 15, NULL, 10);
822 else if (!strcmp(argv[i], "--numbered-files"))
823 numbered_files = 1;
824 else if (!strcmp(argv[i], "--start-number")) {
825 i++;
826 if (i == argc)
827 die("Need a number for --start-number");
828 start_number = strtol(argv[i], NULL, 10);
830 else if (!prefixcmp(argv[i], "--cc=")) {
831 ALLOC_GROW(extra_cc, extra_cc_nr + 1, extra_cc_alloc);
832 extra_cc[extra_cc_nr++] = xstrdup(argv[i] + 5);
834 else if (!strcmp(argv[i], "-k") ||
835 !strcmp(argv[i], "--keep-subject")) {
836 keep_subject = 1;
837 rev.total = -1;
839 else if (!strcmp(argv[i], "--output-directory") ||
840 !strcmp(argv[i], "-o")) {
841 i++;
842 if (argc <= i)
843 die("Which directory?");
844 if (output_directory)
845 die("Two output directories?");
846 output_directory = argv[i];
848 else if (!strcmp(argv[i], "--signoff") ||
849 !strcmp(argv[i], "-s")) {
850 const char *committer;
851 const char *endpos;
852 committer = git_committer_info(IDENT_ERROR_ON_NO_NAME);
853 endpos = strchr(committer, '>');
854 if (!endpos)
855 die("bogos committer info %s\n", committer);
856 add_signoff = xmemdupz(committer, endpos - committer + 1);
858 else if (!strcmp(argv[i], "--attach")) {
859 rev.mime_boundary = git_version_string;
860 rev.no_inline = 1;
862 else if (!prefixcmp(argv[i], "--attach=")) {
863 rev.mime_boundary = argv[i] + 9;
864 rev.no_inline = 1;
866 else if (!strcmp(argv[i], "--inline")) {
867 rev.mime_boundary = git_version_string;
868 rev.no_inline = 0;
870 else if (!prefixcmp(argv[i], "--inline=")) {
871 rev.mime_boundary = argv[i] + 9;
872 rev.no_inline = 0;
874 else if (!strcmp(argv[i], "--ignore-if-in-upstream"))
875 ignore_if_in_upstream = 1;
876 else if (!strcmp(argv[i], "--thread"))
877 thread = 1;
878 else if (!prefixcmp(argv[i], "--in-reply-to="))
879 in_reply_to = argv[i] + 14;
880 else if (!strcmp(argv[i], "--in-reply-to")) {
881 i++;
882 if (i == argc)
883 die("Need a Message-Id for --in-reply-to");
884 in_reply_to = argv[i];
885 } else if (!prefixcmp(argv[i], "--subject-prefix=")) {
886 subject_prefix = 1;
887 rev.subject_prefix = argv[i] + 17;
888 } else if (!prefixcmp(argv[i], "--suffix="))
889 fmt_patch_suffix = argv[i] + 9;
890 else if (!strcmp(argv[i], "--cover-letter"))
891 cover_letter = 1;
892 else if (!strcmp(argv[i], "--no-binary"))
893 no_binary_diff = 1;
894 else
895 argv[j++] = argv[i];
897 argc = j;
899 strbuf_init(&buf, 0);
901 for (i = 0; i < extra_hdr_nr; i++) {
902 strbuf_addstr(&buf, extra_hdr[i]);
903 strbuf_addch(&buf, '\n');
906 if (extra_to_nr)
907 strbuf_addstr(&buf, "To: ");
908 for (i = 0; i < extra_to_nr; i++) {
909 if (i)
910 strbuf_addstr(&buf, " ");
911 strbuf_addstr(&buf, extra_to[i]);
912 if (i + 1 < extra_to_nr)
913 strbuf_addch(&buf, ',');
914 strbuf_addch(&buf, '\n');
917 if (extra_cc_nr)
918 strbuf_addstr(&buf, "Cc: ");
919 for (i = 0; i < extra_cc_nr; i++) {
920 if (i)
921 strbuf_addstr(&buf, " ");
922 strbuf_addstr(&buf, extra_cc[i]);
923 if (i + 1 < extra_cc_nr)
924 strbuf_addch(&buf, ',');
925 strbuf_addch(&buf, '\n');
928 rev.extra_headers = strbuf_detach(&buf, 0);
930 if (start_number < 0)
931 start_number = 1;
932 if (numbered && keep_subject)
933 die ("-n and -k are mutually exclusive.");
934 if (keep_subject && subject_prefix)
935 die ("--subject-prefix and -k are mutually exclusive.");
936 if (numbered_files && use_stdout)
937 die ("--numbered-files and --stdout are mutually exclusive.");
939 argc = setup_revisions(argc, argv, &rev, "HEAD");
940 if (argc > 1)
941 die ("unrecognized argument: %s", argv[1]);
943 if (!rev.diffopt.output_format
944 || rev.diffopt.output_format == DIFF_FORMAT_PATCH)
945 rev.diffopt.output_format = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_SUMMARY | DIFF_FORMAT_PATCH;
947 if (!DIFF_OPT_TST(&rev.diffopt, TEXT) && !no_binary_diff)
948 DIFF_OPT_SET(&rev.diffopt, BINARY);
950 if (!output_directory && !use_stdout)
951 output_directory = prefix;
953 if (output_directory) {
954 if (use_stdout)
955 die("standard output, or directory, which one?");
956 if (mkdir(output_directory, 0777) < 0 && errno != EEXIST)
957 die("Could not create directory %s",
958 output_directory);
961 if (rev.pending.nr == 1) {
962 if (rev.max_count < 0 && !rev.show_root_diff) {
964 * This is traditional behaviour of "git format-patch
965 * origin" that prepares what the origin side still
966 * does not have.
968 rev.pending.objects[0].item->flags |= UNINTERESTING;
969 add_head_to_pending(&rev);
972 * Otherwise, it is "format-patch -22 HEAD", and/or
973 * "format-patch --root HEAD". The user wants
974 * get_revision() to do the usual traversal.
977 if (cover_letter) {
978 /* remember the range */
979 int i;
980 for (i = 0; i < rev.pending.nr; i++) {
981 struct object *o = rev.pending.objects[i].item;
982 if (!(o->flags & UNINTERESTING))
983 head = (struct commit *)o;
985 /* We can't generate a cover letter without any patches */
986 if (!head)
987 return 0;
990 if (ignore_if_in_upstream)
991 get_patch_ids(&rev, &ids, prefix);
993 if (!use_stdout)
994 realstdout = xfdopen(xdup(1), "w");
996 if (prepare_revision_walk(&rev))
997 die("revision walk setup failed");
998 rev.boundary = 1;
999 while ((commit = get_revision(&rev)) != NULL) {
1000 if (commit->object.flags & BOUNDARY) {
1001 boundary_count++;
1002 origin = (boundary_count == 1) ? commit : NULL;
1003 continue;
1006 /* ignore merges */
1007 if (commit->parents && commit->parents->next)
1008 continue;
1010 if (ignore_if_in_upstream &&
1011 has_commit_patch_id(commit, &ids))
1012 continue;
1014 nr++;
1015 list = xrealloc(list, nr * sizeof(list[0]));
1016 list[nr - 1] = commit;
1018 total = nr;
1019 if (!keep_subject && auto_number && total > 1)
1020 numbered = 1;
1021 if (numbered)
1022 rev.total = total + start_number - 1;
1023 if (in_reply_to)
1024 rev.ref_message_id = clean_message_id(in_reply_to);
1025 if (cover_letter) {
1026 if (thread)
1027 gen_message_id(&rev, "cover");
1028 make_cover_letter(&rev, use_stdout, numbered, numbered_files,
1029 origin, nr, list, head);
1030 total++;
1031 start_number--;
1033 rev.add_signoff = add_signoff;
1034 while (0 <= --nr) {
1035 int shown;
1036 commit = list[nr];
1037 rev.nr = total - nr + (start_number - 1);
1038 /* Make the second and subsequent mails replies to the first */
1039 if (thread) {
1040 /* Have we already had a message ID? */
1041 if (rev.message_id) {
1043 * If we've got the ID to be a reply
1044 * to, discard the current ID;
1045 * otherwise, make everything a reply
1046 * to that.
1048 if (rev.ref_message_id)
1049 free(rev.message_id);
1050 else
1051 rev.ref_message_id = rev.message_id;
1053 gen_message_id(&rev, sha1_to_hex(commit->object.sha1));
1055 if (!use_stdout && reopen_stdout(numbered_files ? NULL :
1056 get_oneline_for_filename(commit, keep_subject),
1057 rev.nr, rev.total))
1058 die("Failed to create output files");
1059 shown = log_tree_commit(&rev, commit);
1060 free(commit->buffer);
1061 commit->buffer = NULL;
1063 /* We put one extra blank line between formatted
1064 * patches and this flag is used by log-tree code
1065 * to see if it needs to emit a LF before showing
1066 * the log; when using one file per patch, we do
1067 * not want the extra blank line.
1069 if (!use_stdout)
1070 rev.shown_one = 0;
1071 if (shown) {
1072 if (rev.mime_boundary)
1073 printf("\n--%s%s--\n\n\n",
1074 mime_boundary_leader,
1075 rev.mime_boundary);
1076 else
1077 printf("-- \n%s\n\n", git_version_string);
1079 if (!use_stdout)
1080 fclose(stdout);
1082 free(list);
1083 if (ignore_if_in_upstream)
1084 free_patch_ids(&ids);
1085 return 0;
1088 static int add_pending_commit(const char *arg, struct rev_info *revs, int flags)
1090 unsigned char sha1[20];
1091 if (get_sha1(arg, sha1) == 0) {
1092 struct commit *commit = lookup_commit_reference(sha1);
1093 if (commit) {
1094 commit->object.flags |= flags;
1095 add_pending_object(revs, &commit->object, arg);
1096 return 0;
1099 return -1;
1102 static const char cherry_usage[] =
1103 "git cherry [-v] <upstream> [<head>] [<limit>]";
1104 int cmd_cherry(int argc, const char **argv, const char *prefix)
1106 struct rev_info revs;
1107 struct patch_ids ids;
1108 struct commit *commit;
1109 struct commit_list *list = NULL;
1110 const char *upstream;
1111 const char *head = "HEAD";
1112 const char *limit = NULL;
1113 int verbose = 0;
1115 if (argc > 1 && !strcmp(argv[1], "-v")) {
1116 verbose = 1;
1117 argc--;
1118 argv++;
1121 switch (argc) {
1122 case 4:
1123 limit = argv[3];
1124 /* FALLTHROUGH */
1125 case 3:
1126 head = argv[2];
1127 /* FALLTHROUGH */
1128 case 2:
1129 upstream = argv[1];
1130 break;
1131 default:
1132 usage(cherry_usage);
1135 init_revisions(&revs, prefix);
1136 revs.diff = 1;
1137 revs.combine_merges = 0;
1138 revs.ignore_merges = 1;
1139 DIFF_OPT_SET(&revs.diffopt, RECURSIVE);
1141 if (add_pending_commit(head, &revs, 0))
1142 die("Unknown commit %s", head);
1143 if (add_pending_commit(upstream, &revs, UNINTERESTING))
1144 die("Unknown commit %s", upstream);
1146 /* Don't say anything if head and upstream are the same. */
1147 if (revs.pending.nr == 2) {
1148 struct object_array_entry *o = revs.pending.objects;
1149 if (hashcmp(o[0].item->sha1, o[1].item->sha1) == 0)
1150 return 0;
1153 get_patch_ids(&revs, &ids, prefix);
1155 if (limit && add_pending_commit(limit, &revs, UNINTERESTING))
1156 die("Unknown commit %s", limit);
1158 /* reverse the list of commits */
1159 if (prepare_revision_walk(&revs))
1160 die("revision walk setup failed");
1161 while ((commit = get_revision(&revs)) != NULL) {
1162 /* ignore merges */
1163 if (commit->parents && commit->parents->next)
1164 continue;
1166 commit_list_insert(commit, &list);
1169 while (list) {
1170 char sign = '+';
1172 commit = list->item;
1173 if (has_commit_patch_id(commit, &ids))
1174 sign = '-';
1176 if (verbose) {
1177 struct strbuf buf;
1178 strbuf_init(&buf, 0);
1179 pretty_print_commit(CMIT_FMT_ONELINE, commit,
1180 &buf, 0, NULL, NULL, 0, 0);
1181 printf("%c %s %s\n", sign,
1182 sha1_to_hex(commit->object.sha1), buf.buf);
1183 strbuf_release(&buf);
1185 else {
1186 printf("%c %s\n", sign,
1187 sha1_to_hex(commit->object.sha1));
1190 list = list->next;
1193 free_patch_ids(&ids);
1194 return 0;