Makefile: Add cache-tree.h to the headers list
[git/git-p4.git] / builtin-log.c
blobfa81c259207d24a506b671f081f7b218ceb557f4
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 "commit.h"
9 #include "diff.h"
10 #include "revision.h"
11 #include "log-tree.h"
12 #include "builtin.h"
13 #include "tag.h"
14 #include "reflog-walk.h"
15 #include "patch-ids.h"
16 #include "refs.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);
37 if (!obj)
38 return 0;
39 add_name_decoration("", refname, obj);
40 while (obj->type == OBJ_TAG) {
41 obj = ((struct tag *)obj)->tagged;
42 if (!obj)
43 break;
44 add_name_decoration("tag: ", refname, obj);
46 return 0;
49 static void cmd_log_init(int argc, const char **argv, const char *prefix,
50 struct rev_info *rev)
52 int i;
53 int decorate = 0;
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")) {
72 if (!decorate)
73 for_each_ref(add_ref_decoration, NULL);
74 decorate = 1;
75 } else
76 die("unrecognized argument: %s", arg);
80 static int cmd_log_walk(struct rev_info *rev)
82 struct commit *commit;
84 prepare_revision_walk(rev);
85 while ((commit = get_revision(rev)) != NULL) {
86 log_tree_commit(rev, commit);
87 if (!rev->reflog_info) {
88 /* we allow cycles in reflog ancestry */
89 free(commit->buffer);
90 commit->buffer = NULL;
92 free_commit_list(commit->parents);
93 commit->parents = NULL;
95 return 0;
98 static int git_log_config(const char *var, const char *value)
100 if (!strcmp(var, "format.subjectprefix")) {
101 if (!value)
102 die("format.subjectprefix without value");
103 fmt_patch_subject_prefix = xstrdup(value);
104 return 0;
106 if (!strcmp(var, "log.showroot")) {
107 default_show_root = git_config_bool(var, value);
108 return 0;
110 return git_diff_ui_config(var, value);
113 int cmd_whatchanged(int argc, const char **argv, const char *prefix)
115 struct rev_info rev;
117 git_config(git_log_config);
118 init_revisions(&rev, prefix);
119 rev.diff = 1;
120 rev.simplify_history = 0;
121 cmd_log_init(argc, argv, prefix, &rev);
122 if (!rev.diffopt.output_format)
123 rev.diffopt.output_format = DIFF_FORMAT_RAW;
124 return cmd_log_walk(&rev);
127 static int show_object(const unsigned char *sha1, int suppress_header)
129 unsigned long size;
130 enum object_type type;
131 char *buf = read_sha1_file(sha1, &type, &size);
132 int offset = 0;
134 if (!buf)
135 return error("Could not read object %s", sha1_to_hex(sha1));
137 if (suppress_header)
138 while (offset < size && buf[offset++] != '\n') {
139 int new_offset = offset;
140 while (new_offset < size && buf[new_offset++] != '\n')
141 ; /* do nothing */
142 offset = new_offset;
145 if (offset < size)
146 fwrite(buf + offset, size - offset, 1, stdout);
147 free(buf);
148 return 0;
151 static int show_tree_object(const unsigned char *sha1,
152 const char *base, int baselen,
153 const char *pathname, unsigned mode, int stage)
155 printf("%s%s\n", pathname, S_ISDIR(mode) ? "/" : "");
156 return 0;
159 int cmd_show(int argc, const char **argv, const char *prefix)
161 struct rev_info rev;
162 struct object_array_entry *objects;
163 int i, count, ret = 0;
165 git_config(git_log_config);
166 init_revisions(&rev, prefix);
167 rev.diff = 1;
168 rev.combine_merges = 1;
169 rev.dense_combined_merges = 1;
170 rev.always_show_header = 1;
171 rev.ignore_merges = 0;
172 rev.no_walk = 1;
173 cmd_log_init(argc, argv, prefix, &rev);
175 count = rev.pending.nr;
176 objects = rev.pending.objects;
177 for (i = 0; i < count && !ret; i++) {
178 struct object *o = objects[i].item;
179 const char *name = objects[i].name;
180 switch (o->type) {
181 case OBJ_BLOB:
182 ret = show_object(o->sha1, 0);
183 break;
184 case OBJ_TAG: {
185 struct tag *t = (struct tag *)o;
187 printf("%stag %s%s\n\n",
188 diff_get_color(rev.diffopt.color_diff,
189 DIFF_COMMIT),
190 t->tag,
191 diff_get_color(rev.diffopt.color_diff,
192 DIFF_RESET));
193 ret = show_object(o->sha1, 1);
194 objects[i].item = (struct object *)t->tagged;
195 i--;
196 break;
198 case OBJ_TREE:
199 printf("%stree %s%s\n\n",
200 diff_get_color(rev.diffopt.color_diff,
201 DIFF_COMMIT),
202 name,
203 diff_get_color(rev.diffopt.color_diff,
204 DIFF_RESET));
205 read_tree_recursive((struct tree *)o, "", 0, 0, NULL,
206 show_tree_object);
207 break;
208 case OBJ_COMMIT:
209 rev.pending.nr = rev.pending.alloc = 0;
210 rev.pending.objects = NULL;
211 add_object_array(o, name, &rev.pending);
212 ret = cmd_log_walk(&rev);
213 break;
214 default:
215 ret = error("Unknown type: %d", o->type);
218 free(objects);
219 return ret;
223 * This is equivalent to "git log -g --abbrev-commit --pretty=oneline"
225 int cmd_log_reflog(int argc, const char **argv, const char *prefix)
227 struct rev_info rev;
229 git_config(git_log_config);
230 init_revisions(&rev, prefix);
231 init_reflog_walk(&rev.reflog_info);
232 rev.abbrev_commit = 1;
233 rev.verbose_header = 1;
234 cmd_log_init(argc, argv, prefix, &rev);
237 * This means that we override whatever commit format the user gave
238 * on the cmd line. Sad, but cmd_log_init() currently doesn't
239 * allow us to set a different default.
241 rev.commit_format = CMIT_FMT_ONELINE;
242 rev.always_show_header = 1;
245 * We get called through "git reflog", so unlike the other log
246 * routines, we need to set up our pager manually..
248 setup_pager();
250 return cmd_log_walk(&rev);
253 int cmd_log(int argc, const char **argv, const char *prefix)
255 struct rev_info rev;
257 git_config(git_log_config);
258 init_revisions(&rev, prefix);
259 rev.always_show_header = 1;
260 cmd_log_init(argc, argv, prefix, &rev);
261 return cmd_log_walk(&rev);
264 /* format-patch */
265 #define FORMAT_PATCH_NAME_MAX 64
267 static int istitlechar(char c)
269 return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
270 (c >= '0' && c <= '9') || c == '.' || c == '_';
273 static char *extra_headers = NULL;
274 static int extra_headers_size = 0;
275 static const char *fmt_patch_suffix = ".patch";
277 static int git_format_config(const char *var, const char *value)
279 if (!strcmp(var, "format.headers")) {
280 int len;
282 if (!value)
283 die("format.headers without value");
284 len = strlen(value);
285 extra_headers_size += len + 1;
286 extra_headers = xrealloc(extra_headers, extra_headers_size);
287 extra_headers[extra_headers_size - len - 1] = 0;
288 strcat(extra_headers, value);
289 return 0;
291 if (!strcmp(var, "format.suffix")) {
292 if (!value)
293 die("format.suffix without value");
294 fmt_patch_suffix = xstrdup(value);
295 return 0;
297 if (!strcmp(var, "diff.color") || !strcmp(var, "color.diff")) {
298 return 0;
301 return git_log_config(var, value);
305 static FILE *realstdout = NULL;
306 static const char *output_directory = NULL;
308 static int reopen_stdout(struct commit *commit, int nr, int keep_subject,
309 int numbered_files)
311 char filename[PATH_MAX];
312 char *sol;
313 int len = 0;
314 int suffix_len = strlen(fmt_patch_suffix) + 1;
316 if (output_directory) {
317 if (strlen(output_directory) >=
318 sizeof(filename) - FORMAT_PATCH_NAME_MAX - suffix_len)
319 return error("name of output directory is too long");
320 strlcpy(filename, output_directory, sizeof(filename) - suffix_len);
321 len = strlen(filename);
322 if (filename[len - 1] != '/')
323 filename[len++] = '/';
326 if (numbered_files) {
327 sprintf(filename + len, "%d", nr);
328 len = strlen(filename);
330 } else {
331 sprintf(filename + len, "%04d", nr);
332 len = strlen(filename);
334 sol = strstr(commit->buffer, "\n\n");
335 if (sol) {
336 int j, space = 1;
338 sol += 2;
339 /* strip [PATCH] or [PATCH blabla] */
340 if (!keep_subject && !prefixcmp(sol, "[PATCH")) {
341 char *eos = strchr(sol + 6, ']');
342 if (eos) {
343 while (isspace(*eos))
344 eos++;
345 sol = eos;
349 for (j = 0;
350 j < FORMAT_PATCH_NAME_MAX - suffix_len - 5 &&
351 len < sizeof(filename) - suffix_len &&
352 sol[j] && sol[j] != '\n';
353 j++) {
354 if (istitlechar(sol[j])) {
355 if (space) {
356 filename[len++] = '-';
357 space = 0;
359 filename[len++] = sol[j];
360 if (sol[j] == '.')
361 while (sol[j + 1] == '.')
362 j++;
363 } else
364 space = 1;
366 while (filename[len - 1] == '.'
367 || filename[len - 1] == '-')
368 len--;
369 filename[len] = 0;
371 if (len + suffix_len >= sizeof(filename))
372 return error("Patch pathname too long");
373 strcpy(filename + len, fmt_patch_suffix);
376 fprintf(realstdout, "%s\n", filename);
377 if (freopen(filename, "w", stdout) == NULL)
378 return error("Cannot open patch file %s",filename);
380 return 0;
383 static void get_patch_ids(struct rev_info *rev, struct patch_ids *ids, const char *prefix)
385 struct rev_info check_rev;
386 struct commit *commit;
387 struct object *o1, *o2;
388 unsigned flags1, flags2;
390 if (rev->pending.nr != 2)
391 die("Need exactly one range.");
393 o1 = rev->pending.objects[0].item;
394 flags1 = o1->flags;
395 o2 = rev->pending.objects[1].item;
396 flags2 = o2->flags;
398 if ((flags1 & UNINTERESTING) == (flags2 & UNINTERESTING))
399 die("Not a range.");
401 init_patch_ids(ids);
403 /* given a range a..b get all patch ids for b..a */
404 init_revisions(&check_rev, prefix);
405 o1->flags ^= UNINTERESTING;
406 o2->flags ^= UNINTERESTING;
407 add_pending_object(&check_rev, o1, "o1");
408 add_pending_object(&check_rev, o2, "o2");
409 prepare_revision_walk(&check_rev);
411 while ((commit = get_revision(&check_rev)) != NULL) {
412 /* ignore merges */
413 if (commit->parents && commit->parents->next)
414 continue;
416 add_commit_patch_id(commit, ids);
419 /* reset for next revision walk */
420 clear_commit_marks((struct commit *)o1,
421 SEEN | UNINTERESTING | SHOWN | ADDED);
422 clear_commit_marks((struct commit *)o2,
423 SEEN | UNINTERESTING | SHOWN | ADDED);
424 o1->flags = flags1;
425 o2->flags = flags2;
428 static void gen_message_id(char *dest, unsigned int length, char *base)
430 const char *committer = git_committer_info(-1);
431 const char *email_start = strrchr(committer, '<');
432 const char *email_end = strrchr(committer, '>');
433 if(!email_start || !email_end || email_start > email_end - 1)
434 die("Could not extract email from committer identity.");
435 snprintf(dest, length, "%s.%lu.git.%.*s", base,
436 (unsigned long) time(NULL),
437 (int)(email_end - email_start - 1), email_start + 1);
440 int cmd_format_patch(int argc, const char **argv, const char *prefix)
442 struct commit *commit;
443 struct commit **list = NULL;
444 struct rev_info rev;
445 int nr = 0, total, i, j;
446 int use_stdout = 0;
447 int numbered = 0;
448 int start_number = -1;
449 int keep_subject = 0;
450 int numbered_files = 0; /* _just_ numbers */
451 int subject_prefix = 0;
452 int ignore_if_in_upstream = 0;
453 int thread = 0;
454 const char *in_reply_to = NULL;
455 struct patch_ids ids;
456 char *add_signoff = NULL;
457 char message_id[1024];
458 char ref_message_id[1024];
460 git_config(git_format_config);
461 init_revisions(&rev, prefix);
462 rev.commit_format = CMIT_FMT_EMAIL;
463 rev.verbose_header = 1;
464 rev.diff = 1;
465 rev.combine_merges = 0;
466 rev.ignore_merges = 1;
467 rev.diffopt.msg_sep = "";
468 rev.diffopt.recursive = 1;
470 rev.subject_prefix = fmt_patch_subject_prefix;
471 rev.extra_headers = extra_headers;
474 * Parse the arguments before setup_revisions(), or something
475 * like "git format-patch -o a123 HEAD^.." may fail; a123 is
476 * possibly a valid SHA1.
478 for (i = 1, j = 1; i < argc; i++) {
479 if (!strcmp(argv[i], "--stdout"))
480 use_stdout = 1;
481 else if (!strcmp(argv[i], "-n") ||
482 !strcmp(argv[i], "--numbered"))
483 numbered = 1;
484 else if (!prefixcmp(argv[i], "--start-number="))
485 start_number = strtol(argv[i] + 15, NULL, 10);
486 else if (!strcmp(argv[i], "--numbered-files"))
487 numbered_files = 1;
488 else if (!strcmp(argv[i], "--start-number")) {
489 i++;
490 if (i == argc)
491 die("Need a number for --start-number");
492 start_number = strtol(argv[i], NULL, 10);
494 else if (!strcmp(argv[i], "-k") ||
495 !strcmp(argv[i], "--keep-subject")) {
496 keep_subject = 1;
497 rev.total = -1;
499 else if (!strcmp(argv[i], "--output-directory") ||
500 !strcmp(argv[i], "-o")) {
501 i++;
502 if (argc <= i)
503 die("Which directory?");
504 if (output_directory)
505 die("Two output directories?");
506 output_directory = argv[i];
508 else if (!strcmp(argv[i], "--signoff") ||
509 !strcmp(argv[i], "-s")) {
510 const char *committer;
511 const char *endpos;
512 committer = git_committer_info(1);
513 endpos = strchr(committer, '>');
514 if (!endpos)
515 die("bogos committer info %s\n", committer);
516 add_signoff = xmalloc(endpos - committer + 2);
517 memcpy(add_signoff, committer, endpos - committer + 1);
518 add_signoff[endpos - committer + 1] = 0;
520 else if (!strcmp(argv[i], "--attach")) {
521 rev.mime_boundary = git_version_string;
522 rev.no_inline = 1;
524 else if (!prefixcmp(argv[i], "--attach=")) {
525 rev.mime_boundary = argv[i] + 9;
526 rev.no_inline = 1;
528 else if (!strcmp(argv[i], "--inline")) {
529 rev.mime_boundary = git_version_string;
530 rev.no_inline = 0;
532 else if (!prefixcmp(argv[i], "--inline=")) {
533 rev.mime_boundary = argv[i] + 9;
534 rev.no_inline = 0;
536 else if (!strcmp(argv[i], "--ignore-if-in-upstream"))
537 ignore_if_in_upstream = 1;
538 else if (!strcmp(argv[i], "--thread"))
539 thread = 1;
540 else if (!prefixcmp(argv[i], "--in-reply-to="))
541 in_reply_to = argv[i] + 14;
542 else if (!strcmp(argv[i], "--in-reply-to")) {
543 i++;
544 if (i == argc)
545 die("Need a Message-Id for --in-reply-to");
546 in_reply_to = argv[i];
547 } else if (!prefixcmp(argv[i], "--subject-prefix=")) {
548 subject_prefix = 1;
549 rev.subject_prefix = argv[i] + 17;
550 } else if (!prefixcmp(argv[i], "--suffix="))
551 fmt_patch_suffix = argv[i] + 9;
552 else
553 argv[j++] = argv[i];
555 argc = j;
557 if (start_number < 0)
558 start_number = 1;
559 if (numbered && keep_subject)
560 die ("-n and -k are mutually exclusive.");
561 if (keep_subject && subject_prefix)
562 die ("--subject-prefix and -k are mutually exclusive.");
563 if (numbered_files && use_stdout)
564 die ("--numbered-files and --stdout are mutually exclusive.");
566 argc = setup_revisions(argc, argv, &rev, "HEAD");
567 if (argc > 1)
568 die ("unrecognized argument: %s", argv[1]);
570 if (!rev.diffopt.output_format)
571 rev.diffopt.output_format = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_SUMMARY | DIFF_FORMAT_PATCH;
573 if (!rev.diffopt.text)
574 rev.diffopt.binary = 1;
576 if (!output_directory && !use_stdout)
577 output_directory = prefix;
579 if (output_directory) {
580 if (use_stdout)
581 die("standard output, or directory, which one?");
582 if (mkdir(output_directory, 0777) < 0 && errno != EEXIST)
583 die("Could not create directory %s",
584 output_directory);
587 if (rev.pending.nr == 1) {
588 if (rev.max_count < 0 && !rev.show_root_diff) {
590 * This is traditional behaviour of "git format-patch
591 * origin" that prepares what the origin side still
592 * does not have.
594 rev.pending.objects[0].item->flags |= UNINTERESTING;
595 add_head(&rev);
598 * Otherwise, it is "format-patch -22 HEAD", and/or
599 * "format-patch --root HEAD". The user wants
600 * get_revision() to do the usual traversal.
604 if (ignore_if_in_upstream)
605 get_patch_ids(&rev, &ids, prefix);
607 if (!use_stdout)
608 realstdout = xfdopen(xdup(1), "w");
610 prepare_revision_walk(&rev);
611 while ((commit = get_revision(&rev)) != NULL) {
612 /* ignore merges */
613 if (commit->parents && commit->parents->next)
614 continue;
616 if (ignore_if_in_upstream &&
617 has_commit_patch_id(commit, &ids))
618 continue;
620 nr++;
621 list = xrealloc(list, nr * sizeof(list[0]));
622 list[nr - 1] = commit;
624 total = nr;
625 if (numbered)
626 rev.total = total + start_number - 1;
627 rev.add_signoff = add_signoff;
628 rev.ref_message_id = in_reply_to;
629 while (0 <= --nr) {
630 int shown;
631 commit = list[nr];
632 rev.nr = total - nr + (start_number - 1);
633 /* Make the second and subsequent mails replies to the first */
634 if (thread) {
635 if (nr == (total - 2)) {
636 strncpy(ref_message_id, message_id,
637 sizeof(ref_message_id));
638 ref_message_id[sizeof(ref_message_id)-1]='\0';
639 rev.ref_message_id = ref_message_id;
641 gen_message_id(message_id, sizeof(message_id),
642 sha1_to_hex(commit->object.sha1));
643 rev.message_id = message_id;
645 if (!use_stdout)
646 if (reopen_stdout(commit, rev.nr, keep_subject,
647 numbered_files))
648 die("Failed to create output files");
649 shown = log_tree_commit(&rev, commit);
650 free(commit->buffer);
651 commit->buffer = NULL;
653 /* We put one extra blank line between formatted
654 * patches and this flag is used by log-tree code
655 * to see if it needs to emit a LF before showing
656 * the log; when using one file per patch, we do
657 * not want the extra blank line.
659 if (!use_stdout)
660 rev.shown_one = 0;
661 if (shown) {
662 if (rev.mime_boundary)
663 printf("\n--%s%s--\n\n\n",
664 mime_boundary_leader,
665 rev.mime_boundary);
666 else
667 printf("-- \n%s\n\n", git_version_string);
669 if (!use_stdout)
670 fclose(stdout);
672 free(list);
673 if (ignore_if_in_upstream)
674 free_patch_ids(&ids);
675 return 0;
678 static int add_pending_commit(const char *arg, struct rev_info *revs, int flags)
680 unsigned char sha1[20];
681 if (get_sha1(arg, sha1) == 0) {
682 struct commit *commit = lookup_commit_reference(sha1);
683 if (commit) {
684 commit->object.flags |= flags;
685 add_pending_object(revs, &commit->object, arg);
686 return 0;
689 return -1;
692 static const char cherry_usage[] =
693 "git-cherry [-v] <upstream> [<head>] [<limit>]";
694 int cmd_cherry(int argc, const char **argv, const char *prefix)
696 struct rev_info revs;
697 struct patch_ids ids;
698 struct commit *commit;
699 struct commit_list *list = NULL;
700 const char *upstream;
701 const char *head = "HEAD";
702 const char *limit = NULL;
703 int verbose = 0;
705 if (argc > 1 && !strcmp(argv[1], "-v")) {
706 verbose = 1;
707 argc--;
708 argv++;
711 switch (argc) {
712 case 4:
713 limit = argv[3];
714 /* FALLTHROUGH */
715 case 3:
716 head = argv[2];
717 /* FALLTHROUGH */
718 case 2:
719 upstream = argv[1];
720 break;
721 default:
722 usage(cherry_usage);
725 init_revisions(&revs, prefix);
726 revs.diff = 1;
727 revs.combine_merges = 0;
728 revs.ignore_merges = 1;
729 revs.diffopt.recursive = 1;
731 if (add_pending_commit(head, &revs, 0))
732 die("Unknown commit %s", head);
733 if (add_pending_commit(upstream, &revs, UNINTERESTING))
734 die("Unknown commit %s", upstream);
736 /* Don't say anything if head and upstream are the same. */
737 if (revs.pending.nr == 2) {
738 struct object_array_entry *o = revs.pending.objects;
739 if (hashcmp(o[0].item->sha1, o[1].item->sha1) == 0)
740 return 0;
743 get_patch_ids(&revs, &ids, prefix);
745 if (limit && add_pending_commit(limit, &revs, UNINTERESTING))
746 die("Unknown commit %s", limit);
748 /* reverse the list of commits */
749 prepare_revision_walk(&revs);
750 while ((commit = get_revision(&revs)) != NULL) {
751 /* ignore merges */
752 if (commit->parents && commit->parents->next)
753 continue;
755 commit_list_insert(commit, &list);
758 while (list) {
759 char sign = '+';
761 commit = list->item;
762 if (has_commit_patch_id(commit, &ids))
763 sign = '-';
765 if (verbose) {
766 char *buf = NULL;
767 unsigned long buflen = 0;
768 pretty_print_commit(CMIT_FMT_ONELINE, commit, ~0,
769 &buf, &buflen, 0, NULL, NULL, 0);
770 printf("%c %s %s\n", sign,
771 sha1_to_hex(commit->object.sha1), buf);
772 free(buf);
774 else {
775 printf("%c %s\n", sign,
776 sha1_to_hex(commit->object.sha1));
779 list = list->next;
782 free_patch_ids(&ids);
783 return 0;