2 * Builtin "git log" and related commands (show, whatchanged)
4 * (C) Copyright 2006 Linus Torvalds
14 #include "reflog-walk.h"
15 #include "patch-ids.h"
18 static int default_show_root = 1;
19 static const char *fmt_patch_subject_prefix = "PATCH";
21 /* this is in builtin-diff.c */
22 void add_head(struct rev_info *revs);
24 static void add_name_decoration(const char *prefix, const char *name, struct object *obj)
26 int plen = strlen(prefix);
27 int nlen = strlen(name);
28 struct name_decoration *res = xmalloc(sizeof(struct name_decoration) + plen + nlen);
29 memcpy(res->name, prefix, plen);
30 memcpy(res->name + plen, name, nlen + 1);
31 res->next = add_decoration(&name_decoration, obj, res);
34 static int add_ref_decoration(const char *refname, const unsigned char *sha1, int flags, void *cb_data)
36 struct object *obj = parse_object(sha1);
39 add_name_decoration("", refname, obj);
40 while (obj->type == OBJ_TAG) {
41 obj = ((struct tag *)obj)->tagged;
44 add_name_decoration("tag: ", refname, obj);
49 static void cmd_log_init(int argc, const char **argv, const char *prefix,
55 rev->abbrev = DEFAULT_ABBREV;
56 rev->commit_format = CMIT_FMT_DEFAULT;
57 rev->verbose_header = 1;
58 rev->diffopt.recursive = 1;
59 rev->show_root_diff = default_show_root;
60 rev->subject_prefix = fmt_patch_subject_prefix;
61 argc = setup_revisions(argc, argv, rev, "HEAD");
62 if (rev->diffopt.pickaxe || rev->diffopt.filter)
63 rev->always_show_header = 0;
64 if (rev->diffopt.follow_renames) {
65 rev->always_show_header = 0;
66 if (rev->diffopt.nr_paths != 1)
67 usage("git logs can only follow renames on one pathname at a time");
69 for (i = 1; i < argc; i++) {
70 const char *arg = argv[i];
71 if (!strcmp(arg, "--decorate")) {
73 for_each_ref(add_ref_decoration, NULL);
76 die("unrecognized argument: %s", arg);
80 static void log_show_early(struct rev_info *revs, struct commit_list *list)
82 int i = revs->early_output;
84 sort_in_topological_order(&list, revs->lifo);
86 struct commit *commit = list->item;
87 log_tree_commit(revs, commit);
93 static void early_output(int signal)
95 show_early_output = log_show_early;
98 static void setup_early_output(struct rev_info *rev)
104 * Set up the signal handler, minimally intrusively:
105 * we only set a single volatile integer word (not
106 * using sigatomic_t - trying to avoid unnecessary
107 * system dependencies and headers), and using
110 memset(&sa, 0, sizeof(sa));
111 sa.sa_handler = early_output;
112 sigemptyset(&sa.sa_mask);
113 sa.sa_flags = SA_RESTART;
114 sigaction(SIGALRM, &sa, NULL);
117 * If we can get the whole output in less than a
118 * tenth of a second, don't even bother doing the
119 * early-output thing..
121 * This is a one-time-only trigger.
123 memset(&v, 0, sizeof(v));
124 v.it_value.tv_sec = 0;
125 v.it_value.tv_usec = 100000;
126 setitimer(ITIMER_REAL, &v, NULL);
129 static void finish_early_output(struct rev_info *rev)
131 signal(SIGALRM, SIG_IGN);
132 if (rev->shown_one) {
134 if (rev->commit_format != CMIT_FMT_ONELINE)
135 putchar(rev->diffopt.line_termination);
137 printf("Final output:\n");
140 static int cmd_log_walk(struct rev_info *rev)
142 struct commit *commit;
144 if (rev->early_output)
145 setup_early_output(rev);
147 prepare_revision_walk(rev);
149 if (rev->early_output)
150 finish_early_output(rev);
152 while ((commit = get_revision(rev)) != NULL) {
153 log_tree_commit(rev, commit);
154 if (!rev->reflog_info) {
155 /* we allow cycles in reflog ancestry */
156 free(commit->buffer);
157 commit->buffer = NULL;
159 free_commit_list(commit->parents);
160 commit->parents = NULL;
165 static int git_log_config(const char *var, const char *value)
167 if (!strcmp(var, "format.subjectprefix")) {
169 die("format.subjectprefix without value");
170 fmt_patch_subject_prefix = xstrdup(value);
173 if (!strcmp(var, "log.showroot")) {
174 default_show_root = git_config_bool(var, value);
177 return git_diff_ui_config(var, value);
180 int cmd_whatchanged(int argc, const char **argv, const char *prefix)
184 git_config(git_log_config);
185 init_revisions(&rev, prefix);
187 rev.simplify_history = 0;
188 cmd_log_init(argc, argv, prefix, &rev);
189 if (!rev.diffopt.output_format)
190 rev.diffopt.output_format = DIFF_FORMAT_RAW;
191 return cmd_log_walk(&rev);
194 static int show_object(const unsigned char *sha1, int suppress_header)
197 enum object_type type;
198 char *buf = read_sha1_file(sha1, &type, &size);
202 return error("Could not read object %s", sha1_to_hex(sha1));
205 while (offset < size && buf[offset++] != '\n') {
206 int new_offset = offset;
207 while (new_offset < size && buf[new_offset++] != '\n')
213 fwrite(buf + offset, size - offset, 1, stdout);
218 static int show_tree_object(const unsigned char *sha1,
219 const char *base, int baselen,
220 const char *pathname, unsigned mode, int stage)
222 printf("%s%s\n", pathname, S_ISDIR(mode) ? "/" : "");
226 int cmd_show(int argc, const char **argv, const char *prefix)
229 struct object_array_entry *objects;
230 int i, count, ret = 0;
232 git_config(git_log_config);
233 init_revisions(&rev, prefix);
235 rev.combine_merges = 1;
236 rev.dense_combined_merges = 1;
237 rev.always_show_header = 1;
238 rev.ignore_merges = 0;
240 cmd_log_init(argc, argv, prefix, &rev);
242 count = rev.pending.nr;
243 objects = rev.pending.objects;
244 for (i = 0; i < count && !ret; i++) {
245 struct object *o = objects[i].item;
246 const char *name = objects[i].name;
249 ret = show_object(o->sha1, 0);
252 struct tag *t = (struct tag *)o;
254 printf("%stag %s%s\n\n",
255 diff_get_color(rev.diffopt.color_diff,
258 diff_get_color(rev.diffopt.color_diff,
260 ret = show_object(o->sha1, 1);
261 objects[i].item = (struct object *)t->tagged;
266 printf("%stree %s%s\n\n",
267 diff_get_color(rev.diffopt.color_diff,
270 diff_get_color(rev.diffopt.color_diff,
272 read_tree_recursive((struct tree *)o, "", 0, 0, NULL,
276 rev.pending.nr = rev.pending.alloc = 0;
277 rev.pending.objects = NULL;
278 add_object_array(o, name, &rev.pending);
279 ret = cmd_log_walk(&rev);
282 ret = error("Unknown type: %d", o->type);
290 * This is equivalent to "git log -g --abbrev-commit --pretty=oneline"
292 int cmd_log_reflog(int argc, const char **argv, const char *prefix)
296 git_config(git_log_config);
297 init_revisions(&rev, prefix);
298 init_reflog_walk(&rev.reflog_info);
299 rev.abbrev_commit = 1;
300 rev.verbose_header = 1;
301 cmd_log_init(argc, argv, prefix, &rev);
304 * This means that we override whatever commit format the user gave
305 * on the cmd line. Sad, but cmd_log_init() currently doesn't
306 * allow us to set a different default.
308 rev.commit_format = CMIT_FMT_ONELINE;
309 rev.always_show_header = 1;
312 * We get called through "git reflog", so unlike the other log
313 * routines, we need to set up our pager manually..
317 return cmd_log_walk(&rev);
320 int cmd_log(int argc, const char **argv, const char *prefix)
324 git_config(git_log_config);
325 init_revisions(&rev, prefix);
326 rev.always_show_header = 1;
327 cmd_log_init(argc, argv, prefix, &rev);
328 return cmd_log_walk(&rev);
332 #define FORMAT_PATCH_NAME_MAX 64
334 static int istitlechar(char c)
336 return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
337 (c >= '0' && c <= '9') || c == '.' || c == '_';
340 static char *extra_headers = NULL;
341 static int extra_headers_size = 0;
342 static const char *fmt_patch_suffix = ".patch";
344 static int git_format_config(const char *var, const char *value)
346 if (!strcmp(var, "format.headers")) {
350 die("format.headers without value");
352 extra_headers_size += len + 1;
353 extra_headers = xrealloc(extra_headers, extra_headers_size);
354 extra_headers[extra_headers_size - len - 1] = 0;
355 strcat(extra_headers, value);
358 if (!strcmp(var, "format.suffix")) {
360 die("format.suffix without value");
361 fmt_patch_suffix = xstrdup(value);
364 if (!strcmp(var, "diff.color") || !strcmp(var, "color.diff")) {
368 return git_log_config(var, value);
372 static FILE *realstdout = NULL;
373 static const char *output_directory = NULL;
375 static int reopen_stdout(struct commit *commit, int nr, int keep_subject,
378 char filename[PATH_MAX];
381 int suffix_len = strlen(fmt_patch_suffix) + 1;
383 if (output_directory) {
384 if (strlen(output_directory) >=
385 sizeof(filename) - FORMAT_PATCH_NAME_MAX - suffix_len)
386 return error("name of output directory is too long");
387 strlcpy(filename, output_directory, sizeof(filename) - suffix_len);
388 len = strlen(filename);
389 if (filename[len - 1] != '/')
390 filename[len++] = '/';
393 if (numbered_files) {
394 sprintf(filename + len, "%d", nr);
395 len = strlen(filename);
398 sprintf(filename + len, "%04d", nr);
399 len = strlen(filename);
401 sol = strstr(commit->buffer, "\n\n");
406 /* strip [PATCH] or [PATCH blabla] */
407 if (!keep_subject && !prefixcmp(sol, "[PATCH")) {
408 char *eos = strchr(sol + 6, ']');
410 while (isspace(*eos))
417 j < FORMAT_PATCH_NAME_MAX - suffix_len - 5 &&
418 len < sizeof(filename) - suffix_len &&
419 sol[j] && sol[j] != '\n';
421 if (istitlechar(sol[j])) {
423 filename[len++] = '-';
426 filename[len++] = sol[j];
428 while (sol[j + 1] == '.')
433 while (filename[len - 1] == '.'
434 || filename[len - 1] == '-')
438 if (len + suffix_len >= sizeof(filename))
439 return error("Patch pathname too long");
440 strcpy(filename + len, fmt_patch_suffix);
443 fprintf(realstdout, "%s\n", filename);
444 if (freopen(filename, "w", stdout) == NULL)
445 return error("Cannot open patch file %s",filename);
450 static void get_patch_ids(struct rev_info *rev, struct patch_ids *ids, const char *prefix)
452 struct rev_info check_rev;
453 struct commit *commit;
454 struct object *o1, *o2;
455 unsigned flags1, flags2;
457 if (rev->pending.nr != 2)
458 die("Need exactly one range.");
460 o1 = rev->pending.objects[0].item;
462 o2 = rev->pending.objects[1].item;
465 if ((flags1 & UNINTERESTING) == (flags2 & UNINTERESTING))
470 /* given a range a..b get all patch ids for b..a */
471 init_revisions(&check_rev, prefix);
472 o1->flags ^= UNINTERESTING;
473 o2->flags ^= UNINTERESTING;
474 add_pending_object(&check_rev, o1, "o1");
475 add_pending_object(&check_rev, o2, "o2");
476 prepare_revision_walk(&check_rev);
478 while ((commit = get_revision(&check_rev)) != NULL) {
480 if (commit->parents && commit->parents->next)
483 add_commit_patch_id(commit, ids);
486 /* reset for next revision walk */
487 clear_commit_marks((struct commit *)o1,
488 SEEN | UNINTERESTING | SHOWN | ADDED);
489 clear_commit_marks((struct commit *)o2,
490 SEEN | UNINTERESTING | SHOWN | ADDED);
495 static void gen_message_id(char *dest, unsigned int length, char *base)
497 const char *committer = git_committer_info(-1);
498 const char *email_start = strrchr(committer, '<');
499 const char *email_end = strrchr(committer, '>');
500 if(!email_start || !email_end || email_start > email_end - 1)
501 die("Could not extract email from committer identity.");
502 snprintf(dest, length, "%s.%lu.git.%.*s", base,
503 (unsigned long) time(NULL),
504 (int)(email_end - email_start - 1), email_start + 1);
507 static const char *clean_message_id(const char *msg_id)
510 const char *a, *z, *m;
513 while ((ch = *m) && (isspace(ch) || (ch == '<')))
518 if (!isspace(ch) && (ch != '>'))
523 die("insane in-reply-to: %s", msg_id);
526 return xmemdupz(a, z - a);
529 int cmd_format_patch(int argc, const char **argv, const char *prefix)
531 struct commit *commit;
532 struct commit **list = NULL;
534 int nr = 0, total, i, j;
537 int start_number = -1;
538 int keep_subject = 0;
539 int numbered_files = 0; /* _just_ numbers */
540 int subject_prefix = 0;
541 int ignore_if_in_upstream = 0;
543 const char *in_reply_to = NULL;
544 struct patch_ids ids;
545 char *add_signoff = NULL;
546 char message_id[1024];
547 char ref_message_id[1024];
549 git_config(git_format_config);
550 init_revisions(&rev, prefix);
551 rev.commit_format = CMIT_FMT_EMAIL;
552 rev.verbose_header = 1;
554 rev.combine_merges = 0;
555 rev.ignore_merges = 1;
556 rev.diffopt.msg_sep = "";
557 rev.diffopt.recursive = 1;
559 rev.subject_prefix = fmt_patch_subject_prefix;
560 rev.extra_headers = extra_headers;
563 * Parse the arguments before setup_revisions(), or something
564 * like "git format-patch -o a123 HEAD^.." may fail; a123 is
565 * possibly a valid SHA1.
567 for (i = 1, j = 1; i < argc; i++) {
568 if (!strcmp(argv[i], "--stdout"))
570 else if (!strcmp(argv[i], "-n") ||
571 !strcmp(argv[i], "--numbered"))
573 else if (!prefixcmp(argv[i], "--start-number="))
574 start_number = strtol(argv[i] + 15, NULL, 10);
575 else if (!strcmp(argv[i], "--numbered-files"))
577 else if (!strcmp(argv[i], "--start-number")) {
580 die("Need a number for --start-number");
581 start_number = strtol(argv[i], NULL, 10);
583 else if (!strcmp(argv[i], "-k") ||
584 !strcmp(argv[i], "--keep-subject")) {
588 else if (!strcmp(argv[i], "--output-directory") ||
589 !strcmp(argv[i], "-o")) {
592 die("Which directory?");
593 if (output_directory)
594 die("Two output directories?");
595 output_directory = argv[i];
597 else if (!strcmp(argv[i], "--signoff") ||
598 !strcmp(argv[i], "-s")) {
599 const char *committer;
601 committer = git_committer_info(1);
602 endpos = strchr(committer, '>');
604 die("bogos committer info %s\n", committer);
605 add_signoff = xmemdupz(committer, endpos - committer + 1);
607 else if (!strcmp(argv[i], "--attach")) {
608 rev.mime_boundary = git_version_string;
611 else if (!prefixcmp(argv[i], "--attach=")) {
612 rev.mime_boundary = argv[i] + 9;
615 else if (!strcmp(argv[i], "--inline")) {
616 rev.mime_boundary = git_version_string;
619 else if (!prefixcmp(argv[i], "--inline=")) {
620 rev.mime_boundary = argv[i] + 9;
623 else if (!strcmp(argv[i], "--ignore-if-in-upstream"))
624 ignore_if_in_upstream = 1;
625 else if (!strcmp(argv[i], "--thread"))
627 else if (!prefixcmp(argv[i], "--in-reply-to="))
628 in_reply_to = argv[i] + 14;
629 else if (!strcmp(argv[i], "--in-reply-to")) {
632 die("Need a Message-Id for --in-reply-to");
633 in_reply_to = argv[i];
634 } else if (!prefixcmp(argv[i], "--subject-prefix=")) {
636 rev.subject_prefix = argv[i] + 17;
637 } else if (!prefixcmp(argv[i], "--suffix="))
638 fmt_patch_suffix = argv[i] + 9;
644 if (start_number < 0)
646 if (numbered && keep_subject)
647 die ("-n and -k are mutually exclusive.");
648 if (keep_subject && subject_prefix)
649 die ("--subject-prefix and -k are mutually exclusive.");
650 if (numbered_files && use_stdout)
651 die ("--numbered-files and --stdout are mutually exclusive.");
653 argc = setup_revisions(argc, argv, &rev, "HEAD");
655 die ("unrecognized argument: %s", argv[1]);
657 if (!rev.diffopt.output_format)
658 rev.diffopt.output_format = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_SUMMARY | DIFF_FORMAT_PATCH;
660 if (!rev.diffopt.text)
661 rev.diffopt.binary = 1;
663 if (!output_directory && !use_stdout)
664 output_directory = prefix;
666 if (output_directory) {
668 die("standard output, or directory, which one?");
669 if (mkdir(output_directory, 0777) < 0 && errno != EEXIST)
670 die("Could not create directory %s",
674 if (rev.pending.nr == 1) {
675 if (rev.max_count < 0 && !rev.show_root_diff) {
677 * This is traditional behaviour of "git format-patch
678 * origin" that prepares what the origin side still
681 rev.pending.objects[0].item->flags |= UNINTERESTING;
685 * Otherwise, it is "format-patch -22 HEAD", and/or
686 * "format-patch --root HEAD". The user wants
687 * get_revision() to do the usual traversal.
691 if (ignore_if_in_upstream)
692 get_patch_ids(&rev, &ids, prefix);
695 realstdout = xfdopen(xdup(1), "w");
697 prepare_revision_walk(&rev);
698 while ((commit = get_revision(&rev)) != NULL) {
700 if (commit->parents && commit->parents->next)
703 if (ignore_if_in_upstream &&
704 has_commit_patch_id(commit, &ids))
708 list = xrealloc(list, nr * sizeof(list[0]));
709 list[nr - 1] = commit;
713 rev.total = total + start_number - 1;
714 rev.add_signoff = add_signoff;
716 rev.ref_message_id = clean_message_id(in_reply_to);
720 rev.nr = total - nr + (start_number - 1);
721 /* Make the second and subsequent mails replies to the first */
723 if (nr == (total - 2)) {
724 strncpy(ref_message_id, message_id,
725 sizeof(ref_message_id));
726 ref_message_id[sizeof(ref_message_id)-1]='\0';
727 rev.ref_message_id = ref_message_id;
729 gen_message_id(message_id, sizeof(message_id),
730 sha1_to_hex(commit->object.sha1));
731 rev.message_id = message_id;
734 if (reopen_stdout(commit, rev.nr, keep_subject,
736 die("Failed to create output files");
737 shown = log_tree_commit(&rev, commit);
738 free(commit->buffer);
739 commit->buffer = NULL;
741 /* We put one extra blank line between formatted
742 * patches and this flag is used by log-tree code
743 * to see if it needs to emit a LF before showing
744 * the log; when using one file per patch, we do
745 * not want the extra blank line.
750 if (rev.mime_boundary)
751 printf("\n--%s%s--\n\n\n",
752 mime_boundary_leader,
755 printf("-- \n%s\n\n", git_version_string);
761 if (ignore_if_in_upstream)
762 free_patch_ids(&ids);
766 static int add_pending_commit(const char *arg, struct rev_info *revs, int flags)
768 unsigned char sha1[20];
769 if (get_sha1(arg, sha1) == 0) {
770 struct commit *commit = lookup_commit_reference(sha1);
772 commit->object.flags |= flags;
773 add_pending_object(revs, &commit->object, arg);
780 static const char cherry_usage[] =
781 "git-cherry [-v] <upstream> [<head>] [<limit>]";
782 int cmd_cherry(int argc, const char **argv, const char *prefix)
784 struct rev_info revs;
785 struct patch_ids ids;
786 struct commit *commit;
787 struct commit_list *list = NULL;
788 const char *upstream;
789 const char *head = "HEAD";
790 const char *limit = NULL;
793 if (argc > 1 && !strcmp(argv[1], "-v")) {
813 init_revisions(&revs, prefix);
815 revs.combine_merges = 0;
816 revs.ignore_merges = 1;
817 revs.diffopt.recursive = 1;
819 if (add_pending_commit(head, &revs, 0))
820 die("Unknown commit %s", head);
821 if (add_pending_commit(upstream, &revs, UNINTERESTING))
822 die("Unknown commit %s", upstream);
824 /* Don't say anything if head and upstream are the same. */
825 if (revs.pending.nr == 2) {
826 struct object_array_entry *o = revs.pending.objects;
827 if (hashcmp(o[0].item->sha1, o[1].item->sha1) == 0)
831 get_patch_ids(&revs, &ids, prefix);
833 if (limit && add_pending_commit(limit, &revs, UNINTERESTING))
834 die("Unknown commit %s", limit);
836 /* reverse the list of commits */
837 prepare_revision_walk(&revs);
838 while ((commit = get_revision(&revs)) != NULL) {
840 if (commit->parents && commit->parents->next)
843 commit_list_insert(commit, &list);
850 if (has_commit_patch_id(commit, &ids))
855 strbuf_init(&buf, 0);
856 pretty_print_commit(CMIT_FMT_ONELINE, commit,
857 &buf, 0, NULL, NULL, 0, 0);
858 printf("%c %s %s\n", sign,
859 sha1_to_hex(commit->object.sha1), buf.buf);
860 strbuf_release(&buf);
863 printf("%c %s\n", sign,
864 sha1_to_hex(commit->object.sha1));
870 free_patch_ids(&ids);