gitweb: use new Git::Repo API, and add optional caching
[git/gitweb-caching.git] / builtin-log.c
blob1d3c5cbf580f3989d9605ed68f80beb050e19d39
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);
221 * For --check and --exit-code, the exit code is based on CHECK_FAILED
222 * and HAS_CHANGES being accumulated in rev->diffopt, so be careful to
223 * retain that state information if replacing rev->diffopt in this loop
225 while ((commit = get_revision(rev)) != NULL) {
226 log_tree_commit(rev, commit);
227 if (!rev->reflog_info) {
228 /* we allow cycles in reflog ancestry */
229 free(commit->buffer);
230 commit->buffer = NULL;
232 free_commit_list(commit->parents);
233 commit->parents = NULL;
235 if (rev->diffopt.output_format & DIFF_FORMAT_CHECKDIFF &&
236 DIFF_OPT_TST(&rev->diffopt, CHECK_FAILED)) {
237 return 02;
239 return diff_result_code(&rev->diffopt, 0);
242 static int git_log_config(const char *var, const char *value, void *cb)
244 if (!strcmp(var, "format.pretty"))
245 return git_config_string(&fmt_pretty, var, value);
246 if (!strcmp(var, "format.subjectprefix"))
247 return git_config_string(&fmt_patch_subject_prefix, var, value);
248 if (!strcmp(var, "log.date"))
249 return git_config_string(&default_date_mode, var, value);
250 if (!strcmp(var, "log.showroot")) {
251 default_show_root = git_config_bool(var, value);
252 return 0;
254 return git_diff_ui_config(var, value, cb);
257 int cmd_whatchanged(int argc, const char **argv, const char *prefix)
259 struct rev_info rev;
261 git_config(git_log_config, NULL);
263 if (diff_use_color_default == -1)
264 diff_use_color_default = git_use_color_default;
266 init_revisions(&rev, prefix);
267 rev.diff = 1;
268 rev.simplify_history = 0;
269 cmd_log_init(argc, argv, prefix, &rev);
270 if (!rev.diffopt.output_format)
271 rev.diffopt.output_format = DIFF_FORMAT_RAW;
272 return cmd_log_walk(&rev);
275 static void show_tagger(char *buf, int len, struct rev_info *rev)
277 char *email_end, *p;
278 unsigned long date;
279 int tz;
281 email_end = memchr(buf, '>', len);
282 if (!email_end)
283 return;
284 p = ++email_end;
285 while (isspace(*p))
286 p++;
287 date = strtoul(p, &p, 10);
288 while (isspace(*p))
289 p++;
290 tz = (int)strtol(p, NULL, 10);
291 printf("Tagger: %.*s\nDate: %s\n", (int)(email_end - buf), buf,
292 show_date(date, tz, rev->date_mode));
295 static int show_object(const unsigned char *sha1, int show_tag_object,
296 struct rev_info *rev)
298 unsigned long size;
299 enum object_type type;
300 char *buf = read_sha1_file(sha1, &type, &size);
301 int offset = 0;
303 if (!buf)
304 return error("Could not read object %s", sha1_to_hex(sha1));
306 if (show_tag_object)
307 while (offset < size && buf[offset] != '\n') {
308 int new_offset = offset + 1;
309 while (new_offset < size && buf[new_offset++] != '\n')
310 ; /* do nothing */
311 if (!prefixcmp(buf + offset, "tagger "))
312 show_tagger(buf + offset + 7,
313 new_offset - offset - 7, rev);
314 offset = new_offset;
317 if (offset < size)
318 fwrite(buf + offset, size - offset, 1, stdout);
319 free(buf);
320 return 0;
323 static int show_tree_object(const unsigned char *sha1,
324 const char *base, int baselen,
325 const char *pathname, unsigned mode, int stage, void *context)
327 printf("%s%s\n", pathname, S_ISDIR(mode) ? "/" : "");
328 return 0;
331 int cmd_show(int argc, const char **argv, const char *prefix)
333 struct rev_info rev;
334 struct object_array_entry *objects;
335 int i, count, ret = 0;
337 git_config(git_log_config, NULL);
339 if (diff_use_color_default == -1)
340 diff_use_color_default = git_use_color_default;
342 init_revisions(&rev, prefix);
343 rev.diff = 1;
344 rev.combine_merges = 1;
345 rev.dense_combined_merges = 1;
346 rev.always_show_header = 1;
347 rev.ignore_merges = 0;
348 rev.no_walk = 1;
349 cmd_log_init(argc, argv, prefix, &rev);
351 count = rev.pending.nr;
352 objects = rev.pending.objects;
353 for (i = 0; i < count && !ret; i++) {
354 struct object *o = objects[i].item;
355 const char *name = objects[i].name;
356 switch (o->type) {
357 case OBJ_BLOB:
358 ret = show_object(o->sha1, 0, NULL);
359 break;
360 case OBJ_TAG: {
361 struct tag *t = (struct tag *)o;
363 printf("%stag %s%s\n",
364 diff_get_color_opt(&rev.diffopt, DIFF_COMMIT),
365 t->tag,
366 diff_get_color_opt(&rev.diffopt, DIFF_RESET));
367 ret = show_object(o->sha1, 1, &rev);
368 objects[i].item = parse_object(t->tagged->sha1);
369 i--;
370 break;
372 case OBJ_TREE:
373 printf("%stree %s%s\n\n",
374 diff_get_color_opt(&rev.diffopt, DIFF_COMMIT),
375 name,
376 diff_get_color_opt(&rev.diffopt, DIFF_RESET));
377 read_tree_recursive((struct tree *)o, "", 0, 0, NULL,
378 show_tree_object, NULL);
379 break;
380 case OBJ_COMMIT:
381 rev.pending.nr = rev.pending.alloc = 0;
382 rev.pending.objects = NULL;
383 add_object_array(o, name, &rev.pending);
384 ret = cmd_log_walk(&rev);
385 break;
386 default:
387 ret = error("Unknown type: %d", o->type);
390 free(objects);
391 return ret;
395 * This is equivalent to "git log -g --abbrev-commit --pretty=oneline"
397 int cmd_log_reflog(int argc, const char **argv, const char *prefix)
399 struct rev_info rev;
401 git_config(git_log_config, NULL);
403 if (diff_use_color_default == -1)
404 diff_use_color_default = git_use_color_default;
406 init_revisions(&rev, prefix);
407 init_reflog_walk(&rev.reflog_info);
408 rev.abbrev_commit = 1;
409 rev.verbose_header = 1;
410 cmd_log_init(argc, argv, prefix, &rev);
413 * This means that we override whatever commit format the user gave
414 * on the cmd line. Sad, but cmd_log_init() currently doesn't
415 * allow us to set a different default.
417 rev.commit_format = CMIT_FMT_ONELINE;
418 rev.use_terminator = 1;
419 rev.always_show_header = 1;
422 * We get called through "git reflog", so unlike the other log
423 * routines, we need to set up our pager manually..
425 setup_pager();
427 return cmd_log_walk(&rev);
430 int cmd_log(int argc, const char **argv, const char *prefix)
432 struct rev_info rev;
434 git_config(git_log_config, NULL);
436 if (diff_use_color_default == -1)
437 diff_use_color_default = git_use_color_default;
439 init_revisions(&rev, prefix);
440 rev.always_show_header = 1;
441 cmd_log_init(argc, argv, prefix, &rev);
442 return cmd_log_walk(&rev);
445 /* format-patch */
446 #define FORMAT_PATCH_NAME_MAX 64
448 static int istitlechar(char c)
450 return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
451 (c >= '0' && c <= '9') || c == '.' || c == '_';
454 static const char *fmt_patch_suffix = ".patch";
455 static int numbered = 0;
456 static int auto_number = 0;
458 static char **extra_hdr;
459 static int extra_hdr_nr;
460 static int extra_hdr_alloc;
462 static char **extra_to;
463 static int extra_to_nr;
464 static int extra_to_alloc;
466 static char **extra_cc;
467 static int extra_cc_nr;
468 static int extra_cc_alloc;
470 static void add_header(const char *value)
472 int len = strlen(value);
473 while (len && value[len - 1] == '\n')
474 len--;
475 if (!strncasecmp(value, "to: ", 4)) {
476 ALLOC_GROW(extra_to, extra_to_nr + 1, extra_to_alloc);
477 extra_to[extra_to_nr++] = xstrndup(value + 4, len - 4);
478 return;
480 if (!strncasecmp(value, "cc: ", 4)) {
481 ALLOC_GROW(extra_cc, extra_cc_nr + 1, extra_cc_alloc);
482 extra_cc[extra_cc_nr++] = xstrndup(value + 4, len - 4);
483 return;
485 ALLOC_GROW(extra_hdr, extra_hdr_nr + 1, extra_hdr_alloc);
486 extra_hdr[extra_hdr_nr++] = xstrndup(value, len);
489 static int git_format_config(const char *var, const char *value, void *cb)
491 if (!strcmp(var, "format.headers")) {
492 if (!value)
493 die("format.headers without value");
494 add_header(value);
495 return 0;
497 if (!strcmp(var, "format.suffix"))
498 return git_config_string(&fmt_patch_suffix, var, value);
499 if (!strcmp(var, "format.cc")) {
500 if (!value)
501 return config_error_nonbool(var);
502 ALLOC_GROW(extra_cc, extra_cc_nr + 1, extra_cc_alloc);
503 extra_cc[extra_cc_nr++] = xstrdup(value);
504 return 0;
506 if (!strcmp(var, "diff.color") || !strcmp(var, "color.diff")) {
507 return 0;
509 if (!strcmp(var, "format.numbered")) {
510 if (value && !strcasecmp(value, "auto")) {
511 auto_number = 1;
512 return 0;
514 numbered = git_config_bool(var, value);
515 return 0;
518 return git_log_config(var, value, cb);
522 static const char *get_oneline_for_filename(struct commit *commit,
523 int keep_subject)
525 static char filename[PATH_MAX];
526 char *sol;
527 int len = 0;
528 int suffix_len = strlen(fmt_patch_suffix) + 1;
530 sol = strstr(commit->buffer, "\n\n");
531 if (!sol)
532 filename[0] = '\0';
533 else {
534 int j, space = 0;
536 sol += 2;
537 /* strip [PATCH] or [PATCH blabla] */
538 if (!keep_subject && !prefixcmp(sol, "[PATCH")) {
539 char *eos = strchr(sol + 6, ']');
540 if (eos) {
541 while (isspace(*eos))
542 eos++;
543 sol = eos;
547 for (j = 0;
548 j < FORMAT_PATCH_NAME_MAX - suffix_len - 5 &&
549 len < sizeof(filename) - suffix_len &&
550 sol[j] && sol[j] != '\n';
551 j++) {
552 if (istitlechar(sol[j])) {
553 if (space) {
554 filename[len++] = '-';
555 space = 0;
557 filename[len++] = sol[j];
558 if (sol[j] == '.')
559 while (sol[j + 1] == '.')
560 j++;
561 } else
562 space = 1;
564 while (filename[len - 1] == '.'
565 || filename[len - 1] == '-')
566 len--;
567 filename[len] = '\0';
569 return filename;
572 static FILE *realstdout = NULL;
573 static const char *output_directory = NULL;
575 static int reopen_stdout(const char *oneline, int nr, int total)
577 char filename[PATH_MAX];
578 int len = 0;
579 int suffix_len = strlen(fmt_patch_suffix) + 1;
581 if (output_directory) {
582 len = snprintf(filename, sizeof(filename), "%s",
583 output_directory);
584 if (len >=
585 sizeof(filename) - FORMAT_PATCH_NAME_MAX - suffix_len)
586 return error("name of output directory is too long");
587 if (filename[len - 1] != '/')
588 filename[len++] = '/';
591 if (!oneline)
592 len += sprintf(filename + len, "%d", nr);
593 else {
594 len += sprintf(filename + len, "%04d-", nr);
595 len += snprintf(filename + len, sizeof(filename) - len - 1
596 - suffix_len, "%s", oneline);
597 strcpy(filename + len, fmt_patch_suffix);
600 fprintf(realstdout, "%s\n", filename);
601 if (freopen(filename, "w", stdout) == NULL)
602 return error("Cannot open patch file %s",filename);
604 return 0;
607 static void get_patch_ids(struct rev_info *rev, struct patch_ids *ids, const char *prefix)
609 struct rev_info check_rev;
610 struct commit *commit;
611 struct object *o1, *o2;
612 unsigned flags1, flags2;
614 if (rev->pending.nr != 2)
615 die("Need exactly one range.");
617 o1 = rev->pending.objects[0].item;
618 flags1 = o1->flags;
619 o2 = rev->pending.objects[1].item;
620 flags2 = o2->flags;
622 if ((flags1 & UNINTERESTING) == (flags2 & UNINTERESTING))
623 die("Not a range.");
625 init_patch_ids(ids);
627 /* given a range a..b get all patch ids for b..a */
628 init_revisions(&check_rev, prefix);
629 o1->flags ^= UNINTERESTING;
630 o2->flags ^= UNINTERESTING;
631 add_pending_object(&check_rev, o1, "o1");
632 add_pending_object(&check_rev, o2, "o2");
633 if (prepare_revision_walk(&check_rev))
634 die("revision walk setup failed");
636 while ((commit = get_revision(&check_rev)) != NULL) {
637 /* ignore merges */
638 if (commit->parents && commit->parents->next)
639 continue;
641 add_commit_patch_id(commit, ids);
644 /* reset for next revision walk */
645 clear_commit_marks((struct commit *)o1,
646 SEEN | UNINTERESTING | SHOWN | ADDED);
647 clear_commit_marks((struct commit *)o2,
648 SEEN | UNINTERESTING | SHOWN | ADDED);
649 o1->flags = flags1;
650 o2->flags = flags2;
653 static void gen_message_id(struct rev_info *info, char *base)
655 const char *committer = git_committer_info(IDENT_WARN_ON_NO_NAME);
656 const char *email_start = strrchr(committer, '<');
657 const char *email_end = strrchr(committer, '>');
658 struct strbuf buf;
659 if (!email_start || !email_end || email_start > email_end - 1)
660 die("Could not extract email from committer identity.");
661 strbuf_init(&buf, 0);
662 strbuf_addf(&buf, "%s.%lu.git.%.*s", base,
663 (unsigned long) time(NULL),
664 (int)(email_end - email_start - 1), email_start + 1);
665 info->message_id = strbuf_detach(&buf, NULL);
668 static void make_cover_letter(struct rev_info *rev, int use_stdout,
669 int numbered, int numbered_files,
670 struct commit *origin,
671 int nr, struct commit **list, struct commit *head)
673 const char *committer;
674 char *head_sha1;
675 const char *subject_start = NULL;
676 const char *body = "*** SUBJECT HERE ***\n\n*** BLURB HERE ***\n";
677 const char *msg;
678 const char *extra_headers = rev->extra_headers;
679 struct shortlog log;
680 struct strbuf sb;
681 int i;
682 const char *encoding = "utf-8";
683 struct diff_options opts;
684 int need_8bit_cte = 0;
686 if (rev->commit_format != CMIT_FMT_EMAIL)
687 die("Cover letter needs email format");
689 if (!use_stdout && reopen_stdout(numbered_files ?
690 NULL : "cover-letter", 0, rev->total))
691 return;
693 head_sha1 = sha1_to_hex(head->object.sha1);
695 log_write_email_headers(rev, head_sha1, &subject_start, &extra_headers,
696 &need_8bit_cte);
698 committer = git_committer_info(0);
700 msg = body;
701 strbuf_init(&sb, 0);
702 pp_user_info(NULL, CMIT_FMT_EMAIL, &sb, committer, DATE_RFC2822,
703 encoding);
704 pp_title_line(CMIT_FMT_EMAIL, &msg, &sb, subject_start, extra_headers,
705 encoding, need_8bit_cte);
706 pp_remainder(CMIT_FMT_EMAIL, &msg, &sb, 0);
707 printf("%s\n", sb.buf);
709 strbuf_release(&sb);
711 shortlog_init(&log);
712 log.wrap_lines = 1;
713 log.wrap = 72;
714 log.in1 = 2;
715 log.in2 = 4;
716 for (i = 0; i < nr; i++)
717 shortlog_add_commit(&log, list[i]);
719 shortlog_output(&log);
722 * We can only do diffstat with a unique reference point
724 if (!origin)
725 return;
727 memcpy(&opts, &rev->diffopt, sizeof(opts));
728 opts.output_format = DIFF_FORMAT_SUMMARY | DIFF_FORMAT_DIFFSTAT;
730 diff_setup_done(&opts);
732 diff_tree_sha1(origin->tree->object.sha1,
733 head->tree->object.sha1,
734 "", &opts);
735 diffcore_std(&opts);
736 diff_flush(&opts);
738 printf("\n");
741 static const char *clean_message_id(const char *msg_id)
743 char ch;
744 const char *a, *z, *m;
746 m = msg_id;
747 while ((ch = *m) && (isspace(ch) || (ch == '<')))
748 m++;
749 a = m;
750 z = NULL;
751 while ((ch = *m)) {
752 if (!isspace(ch) && (ch != '>'))
753 z = m;
754 m++;
756 if (!z)
757 die("insane in-reply-to: %s", msg_id);
758 if (++z == m)
759 return a;
760 return xmemdupz(a, z - a);
763 int cmd_format_patch(int argc, const char **argv, const char *prefix)
765 struct commit *commit;
766 struct commit **list = NULL;
767 struct rev_info rev;
768 int nr = 0, total, i, j;
769 int use_stdout = 0;
770 int start_number = -1;
771 int keep_subject = 0;
772 int numbered_files = 0; /* _just_ numbers */
773 int subject_prefix = 0;
774 int ignore_if_in_upstream = 0;
775 int thread = 0;
776 int cover_letter = 0;
777 int boundary_count = 0;
778 int no_binary_diff = 0;
779 struct commit *origin = NULL, *head = NULL;
780 const char *in_reply_to = NULL;
781 struct patch_ids ids;
782 char *add_signoff = NULL;
783 struct strbuf buf;
785 git_config(git_format_config, NULL);
786 init_revisions(&rev, prefix);
787 rev.commit_format = CMIT_FMT_EMAIL;
788 rev.verbose_header = 1;
789 rev.diff = 1;
790 rev.combine_merges = 0;
791 rev.ignore_merges = 1;
792 DIFF_OPT_SET(&rev.diffopt, RECURSIVE);
794 rev.subject_prefix = fmt_patch_subject_prefix;
797 * Parse the arguments before setup_revisions(), or something
798 * like "git format-patch -o a123 HEAD^.." may fail; a123 is
799 * possibly a valid SHA1.
801 for (i = 1, j = 1; i < argc; i++) {
802 if (!strcmp(argv[i], "--stdout"))
803 use_stdout = 1;
804 else if (!strcmp(argv[i], "-n") ||
805 !strcmp(argv[i], "--numbered"))
806 numbered = 1;
807 else if (!strcmp(argv[i], "-N") ||
808 !strcmp(argv[i], "--no-numbered")) {
809 numbered = 0;
810 auto_number = 0;
812 else if (!prefixcmp(argv[i], "--start-number="))
813 start_number = strtol(argv[i] + 15, NULL, 10);
814 else if (!strcmp(argv[i], "--numbered-files"))
815 numbered_files = 1;
816 else if (!strcmp(argv[i], "--start-number")) {
817 i++;
818 if (i == argc)
819 die("Need a number for --start-number");
820 start_number = strtol(argv[i], NULL, 10);
822 else if (!prefixcmp(argv[i], "--cc=")) {
823 ALLOC_GROW(extra_cc, extra_cc_nr + 1, extra_cc_alloc);
824 extra_cc[extra_cc_nr++] = xstrdup(argv[i] + 5);
826 else if (!strcmp(argv[i], "-k") ||
827 !strcmp(argv[i], "--keep-subject")) {
828 keep_subject = 1;
829 rev.total = -1;
831 else if (!strcmp(argv[i], "--output-directory") ||
832 !strcmp(argv[i], "-o")) {
833 i++;
834 if (argc <= i)
835 die("Which directory?");
836 if (output_directory)
837 die("Two output directories?");
838 output_directory = argv[i];
840 else if (!strcmp(argv[i], "--signoff") ||
841 !strcmp(argv[i], "-s")) {
842 const char *committer;
843 const char *endpos;
844 committer = git_committer_info(IDENT_ERROR_ON_NO_NAME);
845 endpos = strchr(committer, '>');
846 if (!endpos)
847 die("bogos committer info %s\n", committer);
848 add_signoff = xmemdupz(committer, endpos - committer + 1);
850 else if (!strcmp(argv[i], "--attach")) {
851 rev.mime_boundary = git_version_string;
852 rev.no_inline = 1;
854 else if (!prefixcmp(argv[i], "--attach=")) {
855 rev.mime_boundary = argv[i] + 9;
856 rev.no_inline = 1;
858 else if (!strcmp(argv[i], "--inline")) {
859 rev.mime_boundary = git_version_string;
860 rev.no_inline = 0;
862 else if (!prefixcmp(argv[i], "--inline=")) {
863 rev.mime_boundary = argv[i] + 9;
864 rev.no_inline = 0;
866 else if (!strcmp(argv[i], "--ignore-if-in-upstream"))
867 ignore_if_in_upstream = 1;
868 else if (!strcmp(argv[i], "--thread"))
869 thread = 1;
870 else if (!prefixcmp(argv[i], "--in-reply-to="))
871 in_reply_to = argv[i] + 14;
872 else if (!strcmp(argv[i], "--in-reply-to")) {
873 i++;
874 if (i == argc)
875 die("Need a Message-Id for --in-reply-to");
876 in_reply_to = argv[i];
877 } else if (!prefixcmp(argv[i], "--subject-prefix=")) {
878 subject_prefix = 1;
879 rev.subject_prefix = argv[i] + 17;
880 } else if (!prefixcmp(argv[i], "--suffix="))
881 fmt_patch_suffix = argv[i] + 9;
882 else if (!strcmp(argv[i], "--cover-letter"))
883 cover_letter = 1;
884 else if (!strcmp(argv[i], "--no-binary"))
885 no_binary_diff = 1;
886 else
887 argv[j++] = argv[i];
889 argc = j;
891 strbuf_init(&buf, 0);
893 for (i = 0; i < extra_hdr_nr; i++) {
894 strbuf_addstr(&buf, extra_hdr[i]);
895 strbuf_addch(&buf, '\n');
898 if (extra_to_nr)
899 strbuf_addstr(&buf, "To: ");
900 for (i = 0; i < extra_to_nr; i++) {
901 if (i)
902 strbuf_addstr(&buf, " ");
903 strbuf_addstr(&buf, extra_to[i]);
904 if (i + 1 < extra_to_nr)
905 strbuf_addch(&buf, ',');
906 strbuf_addch(&buf, '\n');
909 if (extra_cc_nr)
910 strbuf_addstr(&buf, "Cc: ");
911 for (i = 0; i < extra_cc_nr; i++) {
912 if (i)
913 strbuf_addstr(&buf, " ");
914 strbuf_addstr(&buf, extra_cc[i]);
915 if (i + 1 < extra_cc_nr)
916 strbuf_addch(&buf, ',');
917 strbuf_addch(&buf, '\n');
920 rev.extra_headers = strbuf_detach(&buf, 0);
922 if (start_number < 0)
923 start_number = 1;
924 if (numbered && keep_subject)
925 die ("-n and -k are mutually exclusive.");
926 if (keep_subject && subject_prefix)
927 die ("--subject-prefix and -k are mutually exclusive.");
928 if (numbered_files && use_stdout)
929 die ("--numbered-files and --stdout are mutually exclusive.");
931 argc = setup_revisions(argc, argv, &rev, "HEAD");
932 if (argc > 1)
933 die ("unrecognized argument: %s", argv[1]);
935 if (!rev.diffopt.output_format
936 || rev.diffopt.output_format == DIFF_FORMAT_PATCH)
937 rev.diffopt.output_format = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_SUMMARY | DIFF_FORMAT_PATCH;
939 if (!DIFF_OPT_TST(&rev.diffopt, TEXT) && !no_binary_diff)
940 DIFF_OPT_SET(&rev.diffopt, BINARY);
942 if (!output_directory && !use_stdout)
943 output_directory = prefix;
945 if (output_directory) {
946 if (use_stdout)
947 die("standard output, or directory, which one?");
948 if (mkdir(output_directory, 0777) < 0 && errno != EEXIST)
949 die("Could not create directory %s",
950 output_directory);
953 if (rev.pending.nr == 1) {
954 if (rev.max_count < 0 && !rev.show_root_diff) {
956 * This is traditional behaviour of "git format-patch
957 * origin" that prepares what the origin side still
958 * does not have.
960 rev.pending.objects[0].item->flags |= UNINTERESTING;
961 add_head_to_pending(&rev);
964 * Otherwise, it is "format-patch -22 HEAD", and/or
965 * "format-patch --root HEAD". The user wants
966 * get_revision() to do the usual traversal.
969 if (cover_letter) {
970 /* remember the range */
971 int i;
972 for (i = 0; i < rev.pending.nr; i++) {
973 struct object *o = rev.pending.objects[i].item;
974 if (!(o->flags & UNINTERESTING))
975 head = (struct commit *)o;
977 /* We can't generate a cover letter without any patches */
978 if (!head)
979 return 0;
982 if (ignore_if_in_upstream)
983 get_patch_ids(&rev, &ids, prefix);
985 if (!use_stdout)
986 realstdout = xfdopen(xdup(1), "w");
988 if (prepare_revision_walk(&rev))
989 die("revision walk setup failed");
990 rev.boundary = 1;
991 while ((commit = get_revision(&rev)) != NULL) {
992 if (commit->object.flags & BOUNDARY) {
993 boundary_count++;
994 origin = (boundary_count == 1) ? commit : NULL;
995 continue;
998 /* ignore merges */
999 if (commit->parents && commit->parents->next)
1000 continue;
1002 if (ignore_if_in_upstream &&
1003 has_commit_patch_id(commit, &ids))
1004 continue;
1006 nr++;
1007 list = xrealloc(list, nr * sizeof(list[0]));
1008 list[nr - 1] = commit;
1010 total = nr;
1011 if (!keep_subject && auto_number && total > 1)
1012 numbered = 1;
1013 if (numbered)
1014 rev.total = total + start_number - 1;
1015 if (in_reply_to)
1016 rev.ref_message_id = clean_message_id(in_reply_to);
1017 if (cover_letter) {
1018 if (thread)
1019 gen_message_id(&rev, "cover");
1020 make_cover_letter(&rev, use_stdout, numbered, numbered_files,
1021 origin, nr, list, head);
1022 total++;
1023 start_number--;
1025 rev.add_signoff = add_signoff;
1026 while (0 <= --nr) {
1027 int shown;
1028 commit = list[nr];
1029 rev.nr = total - nr + (start_number - 1);
1030 /* Make the second and subsequent mails replies to the first */
1031 if (thread) {
1032 /* Have we already had a message ID? */
1033 if (rev.message_id) {
1035 * If we've got the ID to be a reply
1036 * to, discard the current ID;
1037 * otherwise, make everything a reply
1038 * to that.
1040 if (rev.ref_message_id)
1041 free(rev.message_id);
1042 else
1043 rev.ref_message_id = rev.message_id;
1045 gen_message_id(&rev, sha1_to_hex(commit->object.sha1));
1047 if (!use_stdout && reopen_stdout(numbered_files ? NULL :
1048 get_oneline_for_filename(commit, keep_subject),
1049 rev.nr, rev.total))
1050 die("Failed to create output files");
1051 shown = log_tree_commit(&rev, commit);
1052 free(commit->buffer);
1053 commit->buffer = NULL;
1055 /* We put one extra blank line between formatted
1056 * patches and this flag is used by log-tree code
1057 * to see if it needs to emit a LF before showing
1058 * the log; when using one file per patch, we do
1059 * not want the extra blank line.
1061 if (!use_stdout)
1062 rev.shown_one = 0;
1063 if (shown) {
1064 if (rev.mime_boundary)
1065 printf("\n--%s%s--\n\n\n",
1066 mime_boundary_leader,
1067 rev.mime_boundary);
1068 else
1069 printf("-- \n%s\n\n", git_version_string);
1071 if (!use_stdout)
1072 fclose(stdout);
1074 free(list);
1075 if (ignore_if_in_upstream)
1076 free_patch_ids(&ids);
1077 return 0;
1080 static int add_pending_commit(const char *arg, struct rev_info *revs, int flags)
1082 unsigned char sha1[20];
1083 if (get_sha1(arg, sha1) == 0) {
1084 struct commit *commit = lookup_commit_reference(sha1);
1085 if (commit) {
1086 commit->object.flags |= flags;
1087 add_pending_object(revs, &commit->object, arg);
1088 return 0;
1091 return -1;
1094 static const char cherry_usage[] =
1095 "git cherry [-v] <upstream> [<head>] [<limit>]";
1096 int cmd_cherry(int argc, const char **argv, const char *prefix)
1098 struct rev_info revs;
1099 struct patch_ids ids;
1100 struct commit *commit;
1101 struct commit_list *list = NULL;
1102 const char *upstream;
1103 const char *head = "HEAD";
1104 const char *limit = NULL;
1105 int verbose = 0;
1107 if (argc > 1 && !strcmp(argv[1], "-v")) {
1108 verbose = 1;
1109 argc--;
1110 argv++;
1113 switch (argc) {
1114 case 4:
1115 limit = argv[3];
1116 /* FALLTHROUGH */
1117 case 3:
1118 head = argv[2];
1119 /* FALLTHROUGH */
1120 case 2:
1121 upstream = argv[1];
1122 break;
1123 default:
1124 usage(cherry_usage);
1127 init_revisions(&revs, prefix);
1128 revs.diff = 1;
1129 revs.combine_merges = 0;
1130 revs.ignore_merges = 1;
1131 DIFF_OPT_SET(&revs.diffopt, RECURSIVE);
1133 if (add_pending_commit(head, &revs, 0))
1134 die("Unknown commit %s", head);
1135 if (add_pending_commit(upstream, &revs, UNINTERESTING))
1136 die("Unknown commit %s", upstream);
1138 /* Don't say anything if head and upstream are the same. */
1139 if (revs.pending.nr == 2) {
1140 struct object_array_entry *o = revs.pending.objects;
1141 if (hashcmp(o[0].item->sha1, o[1].item->sha1) == 0)
1142 return 0;
1145 get_patch_ids(&revs, &ids, prefix);
1147 if (limit && add_pending_commit(limit, &revs, UNINTERESTING))
1148 die("Unknown commit %s", limit);
1150 /* reverse the list of commits */
1151 if (prepare_revision_walk(&revs))
1152 die("revision walk setup failed");
1153 while ((commit = get_revision(&revs)) != NULL) {
1154 /* ignore merges */
1155 if (commit->parents && commit->parents->next)
1156 continue;
1158 commit_list_insert(commit, &list);
1161 while (list) {
1162 char sign = '+';
1164 commit = list->item;
1165 if (has_commit_patch_id(commit, &ids))
1166 sign = '-';
1168 if (verbose) {
1169 struct strbuf buf;
1170 strbuf_init(&buf, 0);
1171 pretty_print_commit(CMIT_FMT_ONELINE, commit,
1172 &buf, 0, NULL, NULL, 0, 0);
1173 printf("%c %s %s\n", sign,
1174 sha1_to_hex(commit->object.sha1), buf.buf);
1175 strbuf_release(&buf);
1177 else {
1178 printf("%c %s\n", sign,
1179 sha1_to_hex(commit->object.sha1));
1182 list = list->next;
1185 free_patch_ids(&ids);
1186 return 0;