rebase--interactive: remote stray closing parenthesis
[git.git] / builtin-log.c
blob073a2a16a3fafd66d13b1ed106a0016617ec63ad
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;
20 /* this is in builtin-diff.c */
21 void add_head(struct rev_info *revs);
23 static void add_name_decoration(const char *prefix, const char *name, struct object *obj)
25 int plen = strlen(prefix);
26 int nlen = strlen(name);
27 struct name_decoration *res = xmalloc(sizeof(struct name_decoration) + plen + nlen);
28 memcpy(res->name, prefix, plen);
29 memcpy(res->name + plen, name, nlen + 1);
30 res->next = add_decoration(&name_decoration, obj, res);
33 static int add_ref_decoration(const char *refname, const unsigned char *sha1, int flags, void *cb_data)
35 struct object *obj = parse_object(sha1);
36 if (!obj)
37 return 0;
38 add_name_decoration("", refname, obj);
39 while (obj->type == OBJ_TAG) {
40 obj = ((struct tag *)obj)->tagged;
41 if (!obj)
42 break;
43 add_name_decoration("tag: ", refname, obj);
45 return 0;
48 static void cmd_log_init(int argc, const char **argv, const char *prefix,
49 struct rev_info *rev)
51 int i;
52 int decorate = 0;
54 rev->abbrev = DEFAULT_ABBREV;
55 rev->commit_format = CMIT_FMT_DEFAULT;
56 rev->verbose_header = 1;
57 rev->show_root_diff = default_show_root;
58 argc = setup_revisions(argc, argv, rev, "HEAD");
59 if (rev->diffopt.pickaxe || rev->diffopt.filter)
60 rev->always_show_header = 0;
61 if (rev->diffopt.follow_renames) {
62 rev->always_show_header = 0;
63 if (rev->diffopt.nr_paths != 1)
64 usage("git logs can only follow renames on one pathname at a time");
66 for (i = 1; i < argc; i++) {
67 const char *arg = argv[i];
68 if (!strcmp(arg, "--decorate")) {
69 if (!decorate)
70 for_each_ref(add_ref_decoration, NULL);
71 decorate = 1;
72 } else
73 die("unrecognized argument: %s", arg);
77 static int cmd_log_walk(struct rev_info *rev)
79 struct commit *commit;
81 prepare_revision_walk(rev);
82 while ((commit = get_revision(rev)) != NULL) {
83 log_tree_commit(rev, commit);
84 if (!rev->reflog_info) {
85 /* we allow cycles in reflog ancestry */
86 free(commit->buffer);
87 commit->buffer = NULL;
89 free_commit_list(commit->parents);
90 commit->parents = NULL;
92 return 0;
95 static int git_log_config(const char *var, const char *value)
97 if (!strcmp(var, "log.showroot")) {
98 default_show_root = git_config_bool(var, value);
99 return 0;
101 return git_diff_ui_config(var, value);
104 int cmd_whatchanged(int argc, const char **argv, const char *prefix)
106 struct rev_info rev;
108 git_config(git_log_config);
109 init_revisions(&rev, prefix);
110 rev.diff = 1;
111 rev.diffopt.recursive = 1;
112 rev.simplify_history = 0;
113 cmd_log_init(argc, argv, prefix, &rev);
114 if (!rev.diffopt.output_format)
115 rev.diffopt.output_format = DIFF_FORMAT_RAW;
116 return cmd_log_walk(&rev);
119 static int show_object(const unsigned char *sha1, int suppress_header)
121 unsigned long size;
122 enum object_type type;
123 char *buf = read_sha1_file(sha1, &type, &size);
124 int offset = 0;
126 if (!buf)
127 return error("Could not read object %s", sha1_to_hex(sha1));
129 if (suppress_header)
130 while (offset < size && buf[offset++] != '\n') {
131 int new_offset = offset;
132 while (new_offset < size && buf[new_offset++] != '\n')
133 ; /* do nothing */
134 offset = new_offset;
137 if (offset < size)
138 fwrite(buf + offset, size - offset, 1, stdout);
139 free(buf);
140 return 0;
143 static int show_tree_object(const unsigned char *sha1,
144 const char *base, int baselen,
145 const char *pathname, unsigned mode, int stage)
147 printf("%s%s\n", pathname, S_ISDIR(mode) ? "/" : "");
148 return 0;
151 int cmd_show(int argc, const char **argv, const char *prefix)
153 struct rev_info rev;
154 struct object_array_entry *objects;
155 int i, count, ret = 0;
157 git_config(git_log_config);
158 init_revisions(&rev, prefix);
159 rev.diff = 1;
160 rev.diffopt.recursive = 1;
161 rev.combine_merges = 1;
162 rev.dense_combined_merges = 1;
163 rev.always_show_header = 1;
164 rev.ignore_merges = 0;
165 rev.no_walk = 1;
166 cmd_log_init(argc, argv, prefix, &rev);
168 count = rev.pending.nr;
169 objects = rev.pending.objects;
170 for (i = 0; i < count && !ret; i++) {
171 struct object *o = objects[i].item;
172 const char *name = objects[i].name;
173 switch (o->type) {
174 case OBJ_BLOB:
175 ret = show_object(o->sha1, 0);
176 break;
177 case OBJ_TAG: {
178 struct tag *t = (struct tag *)o;
180 printf("%stag %s%s\n\n",
181 diff_get_color(rev.diffopt.color_diff,
182 DIFF_COMMIT),
183 t->tag,
184 diff_get_color(rev.diffopt.color_diff,
185 DIFF_RESET));
186 ret = show_object(o->sha1, 1);
187 objects[i].item = (struct object *)t->tagged;
188 i--;
189 break;
191 case OBJ_TREE:
192 printf("%stree %s%s\n\n",
193 diff_get_color(rev.diffopt.color_diff,
194 DIFF_COMMIT),
195 name,
196 diff_get_color(rev.diffopt.color_diff,
197 DIFF_RESET));
198 read_tree_recursive((struct tree *)o, "", 0, 0, NULL,
199 show_tree_object);
200 break;
201 case OBJ_COMMIT:
202 rev.pending.nr = rev.pending.alloc = 0;
203 rev.pending.objects = NULL;
204 add_object_array(o, name, &rev.pending);
205 ret = cmd_log_walk(&rev);
206 break;
207 default:
208 ret = error("Unknown type: %d", o->type);
211 free(objects);
212 return ret;
216 * This is equivalent to "git log -g --abbrev-commit --pretty=oneline"
218 int cmd_log_reflog(int argc, const char **argv, const char *prefix)
220 struct rev_info rev;
222 git_config(git_log_config);
223 init_revisions(&rev, prefix);
224 init_reflog_walk(&rev.reflog_info);
225 rev.abbrev_commit = 1;
226 rev.verbose_header = 1;
227 cmd_log_init(argc, argv, prefix, &rev);
230 * This means that we override whatever commit format the user gave
231 * on the cmd line. Sad, but cmd_log_init() currently doesn't
232 * allow us to set a different default.
234 rev.commit_format = CMIT_FMT_ONELINE;
235 rev.always_show_header = 1;
238 * We get called through "git reflog", so unlike the other log
239 * routines, we need to set up our pager manually..
241 setup_pager();
243 return cmd_log_walk(&rev);
246 int cmd_log(int argc, const char **argv, const char *prefix)
248 struct rev_info rev;
250 git_config(git_log_config);
251 init_revisions(&rev, prefix);
252 rev.always_show_header = 1;
253 cmd_log_init(argc, argv, prefix, &rev);
254 return cmd_log_walk(&rev);
257 /* format-patch */
258 #define FORMAT_PATCH_NAME_MAX 64
260 static int istitlechar(char c)
262 return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
263 (c >= '0' && c <= '9') || c == '.' || c == '_';
266 static char *extra_headers = NULL;
267 static int extra_headers_size = 0;
268 static const char *fmt_patch_suffix = ".patch";
270 static int git_format_config(const char *var, const char *value)
272 if (!strcmp(var, "format.headers")) {
273 int len;
275 if (!value)
276 die("format.headers without value");
277 len = strlen(value);
278 extra_headers_size += len + 1;
279 extra_headers = xrealloc(extra_headers, extra_headers_size);
280 extra_headers[extra_headers_size - len - 1] = 0;
281 strcat(extra_headers, value);
282 return 0;
284 if (!strcmp(var, "format.suffix")) {
285 if (!value)
286 die("format.suffix without value");
287 fmt_patch_suffix = xstrdup(value);
288 return 0;
290 if (!strcmp(var, "diff.color") || !strcmp(var, "color.diff")) {
291 return 0;
293 return git_log_config(var, value);
297 static FILE *realstdout = NULL;
298 static const char *output_directory = NULL;
300 static int reopen_stdout(struct commit *commit, int nr, int keep_subject,
301 int numbered_files)
303 char filename[PATH_MAX];
304 char *sol;
305 int len = 0;
306 int suffix_len = strlen(fmt_patch_suffix) + 1;
308 if (output_directory) {
309 if (strlen(output_directory) >=
310 sizeof(filename) - FORMAT_PATCH_NAME_MAX - suffix_len)
311 return error("name of output directory is too long");
312 strlcpy(filename, output_directory, sizeof(filename) - suffix_len);
313 len = strlen(filename);
314 if (filename[len - 1] != '/')
315 filename[len++] = '/';
318 if (numbered_files) {
319 sprintf(filename + len, "%d", nr);
320 len = strlen(filename);
322 } else {
323 sprintf(filename + len, "%04d", nr);
324 len = strlen(filename);
326 sol = strstr(commit->buffer, "\n\n");
327 if (sol) {
328 int j, space = 1;
330 sol += 2;
331 /* strip [PATCH] or [PATCH blabla] */
332 if (!keep_subject && !prefixcmp(sol, "[PATCH")) {
333 char *eos = strchr(sol + 6, ']');
334 if (eos) {
335 while (isspace(*eos))
336 eos++;
337 sol = eos;
341 for (j = 0;
342 j < FORMAT_PATCH_NAME_MAX - suffix_len - 5 &&
343 len < sizeof(filename) - suffix_len &&
344 sol[j] && sol[j] != '\n';
345 j++) {
346 if (istitlechar(sol[j])) {
347 if (space) {
348 filename[len++] = '-';
349 space = 0;
351 filename[len++] = sol[j];
352 if (sol[j] == '.')
353 while (sol[j + 1] == '.')
354 j++;
355 } else
356 space = 1;
358 while (filename[len - 1] == '.'
359 || filename[len - 1] == '-')
360 len--;
361 filename[len] = 0;
363 if (len + suffix_len >= sizeof(filename))
364 return error("Patch pathname too long");
365 strcpy(filename + len, fmt_patch_suffix);
368 fprintf(realstdout, "%s\n", filename);
369 if (freopen(filename, "w", stdout) == NULL)
370 return error("Cannot open patch file %s",filename);
372 return 0;
375 static void get_patch_ids(struct rev_info *rev, struct patch_ids *ids, const char *prefix)
377 struct rev_info check_rev;
378 struct commit *commit;
379 struct object *o1, *o2;
380 unsigned flags1, flags2;
382 if (rev->pending.nr != 2)
383 die("Need exactly one range.");
385 o1 = rev->pending.objects[0].item;
386 flags1 = o1->flags;
387 o2 = rev->pending.objects[1].item;
388 flags2 = o2->flags;
390 if ((flags1 & UNINTERESTING) == (flags2 & UNINTERESTING))
391 die("Not a range.");
393 init_patch_ids(ids);
395 /* given a range a..b get all patch ids for b..a */
396 init_revisions(&check_rev, prefix);
397 o1->flags ^= UNINTERESTING;
398 o2->flags ^= UNINTERESTING;
399 add_pending_object(&check_rev, o1, "o1");
400 add_pending_object(&check_rev, o2, "o2");
401 prepare_revision_walk(&check_rev);
403 while ((commit = get_revision(&check_rev)) != NULL) {
404 /* ignore merges */
405 if (commit->parents && commit->parents->next)
406 continue;
408 add_commit_patch_id(commit, ids);
411 /* reset for next revision walk */
412 clear_commit_marks((struct commit *)o1,
413 SEEN | UNINTERESTING | SHOWN | ADDED);
414 clear_commit_marks((struct commit *)o2,
415 SEEN | UNINTERESTING | SHOWN | ADDED);
416 o1->flags = flags1;
417 o2->flags = flags2;
420 static void gen_message_id(char *dest, unsigned int length, char *base)
422 const char *committer = git_committer_info(-1);
423 const char *email_start = strrchr(committer, '<');
424 const char *email_end = strrchr(committer, '>');
425 if(!email_start || !email_end || email_start > email_end - 1)
426 die("Could not extract email from committer identity.");
427 snprintf(dest, length, "%s.%lu.git.%.*s", base,
428 (unsigned long) time(NULL),
429 (int)(email_end - email_start - 1), email_start + 1);
432 int cmd_format_patch(int argc, const char **argv, const char *prefix)
434 struct commit *commit;
435 struct commit **list = NULL;
436 struct rev_info rev;
437 int nr = 0, total, i, j;
438 int use_stdout = 0;
439 int numbered = 0;
440 int start_number = -1;
441 int keep_subject = 0;
442 int numbered_files = 0; /* _just_ numbers */
443 int subject_prefix = 0;
444 int ignore_if_in_upstream = 0;
445 int thread = 0;
446 const char *in_reply_to = NULL;
447 struct patch_ids ids;
448 char *add_signoff = NULL;
449 char message_id[1024];
450 char ref_message_id[1024];
452 git_config(git_format_config);
453 init_revisions(&rev, prefix);
454 rev.commit_format = CMIT_FMT_EMAIL;
455 rev.verbose_header = 1;
456 rev.diff = 1;
457 rev.combine_merges = 0;
458 rev.ignore_merges = 1;
459 rev.diffopt.msg_sep = "";
460 rev.diffopt.recursive = 1;
462 rev.extra_headers = extra_headers;
465 * Parse the arguments before setup_revisions(), or something
466 * like "git format-patch -o a123 HEAD^.." may fail; a123 is
467 * possibly a valid SHA1.
469 for (i = 1, j = 1; i < argc; i++) {
470 if (!strcmp(argv[i], "--stdout"))
471 use_stdout = 1;
472 else if (!strcmp(argv[i], "-n") ||
473 !strcmp(argv[i], "--numbered"))
474 numbered = 1;
475 else if (!prefixcmp(argv[i], "--start-number="))
476 start_number = strtol(argv[i] + 15, NULL, 10);
477 else if (!strcmp(argv[i], "--numbered-files"))
478 numbered_files = 1;
479 else if (!strcmp(argv[i], "--start-number")) {
480 i++;
481 if (i == argc)
482 die("Need a number for --start-number");
483 start_number = strtol(argv[i], NULL, 10);
485 else if (!strcmp(argv[i], "-k") ||
486 !strcmp(argv[i], "--keep-subject")) {
487 keep_subject = 1;
488 rev.total = -1;
490 else if (!strcmp(argv[i], "--output-directory") ||
491 !strcmp(argv[i], "-o")) {
492 i++;
493 if (argc <= i)
494 die("Which directory?");
495 if (output_directory)
496 die("Two output directories?");
497 output_directory = argv[i];
499 else if (!strcmp(argv[i], "--signoff") ||
500 !strcmp(argv[i], "-s")) {
501 const char *committer;
502 const char *endpos;
503 committer = git_committer_info(1);
504 endpos = strchr(committer, '>');
505 if (!endpos)
506 die("bogos committer info %s\n", committer);
507 add_signoff = xmalloc(endpos - committer + 2);
508 memcpy(add_signoff, committer, endpos - committer + 1);
509 add_signoff[endpos - committer + 1] = 0;
511 else if (!strcmp(argv[i], "--attach")) {
512 rev.mime_boundary = git_version_string;
513 rev.no_inline = 1;
515 else if (!prefixcmp(argv[i], "--attach=")) {
516 rev.mime_boundary = argv[i] + 9;
517 rev.no_inline = 1;
519 else if (!strcmp(argv[i], "--inline")) {
520 rev.mime_boundary = git_version_string;
521 rev.no_inline = 0;
523 else if (!prefixcmp(argv[i], "--inline=")) {
524 rev.mime_boundary = argv[i] + 9;
525 rev.no_inline = 0;
527 else if (!strcmp(argv[i], "--ignore-if-in-upstream"))
528 ignore_if_in_upstream = 1;
529 else if (!strcmp(argv[i], "--thread"))
530 thread = 1;
531 else if (!prefixcmp(argv[i], "--in-reply-to="))
532 in_reply_to = argv[i] + 14;
533 else if (!strcmp(argv[i], "--in-reply-to")) {
534 i++;
535 if (i == argc)
536 die("Need a Message-Id for --in-reply-to");
537 in_reply_to = argv[i];
538 } else if (!prefixcmp(argv[i], "--subject-prefix=")) {
539 subject_prefix = 1;
540 rev.subject_prefix = argv[i] + 17;
541 } else if (!prefixcmp(argv[i], "--suffix="))
542 fmt_patch_suffix = argv[i] + 9;
543 else
544 argv[j++] = argv[i];
546 argc = j;
548 if (start_number < 0)
549 start_number = 1;
550 if (numbered && keep_subject)
551 die ("-n and -k are mutually exclusive.");
552 if (keep_subject && subject_prefix)
553 die ("--subject-prefix and -k are mutually exclusive.");
554 if (numbered_files && use_stdout)
555 die ("--numbered-files and --stdout are mutually exclusive.");
557 argc = setup_revisions(argc, argv, &rev, "HEAD");
558 if (argc > 1)
559 die ("unrecognized argument: %s", argv[1]);
561 if (!rev.diffopt.output_format)
562 rev.diffopt.output_format = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_SUMMARY | DIFF_FORMAT_PATCH;
564 if (!rev.diffopt.text)
565 rev.diffopt.binary = 1;
567 if (!output_directory && !use_stdout)
568 output_directory = prefix;
570 if (output_directory) {
571 if (use_stdout)
572 die("standard output, or directory, which one?");
573 if (mkdir(output_directory, 0777) < 0 && errno != EEXIST)
574 die("Could not create directory %s",
575 output_directory);
578 if (rev.pending.nr == 1) {
579 if (rev.max_count < 0) {
580 rev.pending.objects[0].item->flags |= UNINTERESTING;
581 add_head(&rev);
583 /* Otherwise, it is "format-patch -22 HEAD", and
584 * get_revision() would return only the specified count.
588 if (ignore_if_in_upstream)
589 get_patch_ids(&rev, &ids, prefix);
591 if (!use_stdout)
592 realstdout = fdopen(dup(1), "w");
594 prepare_revision_walk(&rev);
595 while ((commit = get_revision(&rev)) != NULL) {
596 /* ignore merges */
597 if (commit->parents && commit->parents->next)
598 continue;
600 if (ignore_if_in_upstream &&
601 has_commit_patch_id(commit, &ids))
602 continue;
604 nr++;
605 list = xrealloc(list, nr * sizeof(list[0]));
606 list[nr - 1] = commit;
608 total = nr;
609 if (numbered)
610 rev.total = total + start_number - 1;
611 rev.add_signoff = add_signoff;
612 rev.ref_message_id = in_reply_to;
613 while (0 <= --nr) {
614 int shown;
615 commit = list[nr];
616 rev.nr = total - nr + (start_number - 1);
617 /* Make the second and subsequent mails replies to the first */
618 if (thread) {
619 if (nr == (total - 2)) {
620 strncpy(ref_message_id, message_id,
621 sizeof(ref_message_id));
622 ref_message_id[sizeof(ref_message_id)-1]='\0';
623 rev.ref_message_id = ref_message_id;
625 gen_message_id(message_id, sizeof(message_id),
626 sha1_to_hex(commit->object.sha1));
627 rev.message_id = message_id;
629 if (!use_stdout)
630 if (reopen_stdout(commit, rev.nr, keep_subject,
631 numbered_files))
632 die("Failed to create output files");
633 shown = log_tree_commit(&rev, commit);
634 free(commit->buffer);
635 commit->buffer = NULL;
637 /* We put one extra blank line between formatted
638 * patches and this flag is used by log-tree code
639 * to see if it needs to emit a LF before showing
640 * the log; when using one file per patch, we do
641 * not want the extra blank line.
643 if (!use_stdout)
644 rev.shown_one = 0;
645 if (shown) {
646 if (rev.mime_boundary)
647 printf("\n--%s%s--\n\n\n",
648 mime_boundary_leader,
649 rev.mime_boundary);
650 else
651 printf("-- \n%s\n\n", git_version_string);
653 if (!use_stdout)
654 fclose(stdout);
656 free(list);
657 if (ignore_if_in_upstream)
658 free_patch_ids(&ids);
659 return 0;
662 static int add_pending_commit(const char *arg, struct rev_info *revs, int flags)
664 unsigned char sha1[20];
665 if (get_sha1(arg, sha1) == 0) {
666 struct commit *commit = lookup_commit_reference(sha1);
667 if (commit) {
668 commit->object.flags |= flags;
669 add_pending_object(revs, &commit->object, arg);
670 return 0;
673 return -1;
676 static const char cherry_usage[] =
677 "git-cherry [-v] <upstream> [<head>] [<limit>]";
678 int cmd_cherry(int argc, const char **argv, const char *prefix)
680 struct rev_info revs;
681 struct patch_ids ids;
682 struct commit *commit;
683 struct commit_list *list = NULL;
684 const char *upstream;
685 const char *head = "HEAD";
686 const char *limit = NULL;
687 int verbose = 0;
689 if (argc > 1 && !strcmp(argv[1], "-v")) {
690 verbose = 1;
691 argc--;
692 argv++;
695 switch (argc) {
696 case 4:
697 limit = argv[3];
698 /* FALLTHROUGH */
699 case 3:
700 head = argv[2];
701 /* FALLTHROUGH */
702 case 2:
703 upstream = argv[1];
704 break;
705 default:
706 usage(cherry_usage);
709 init_revisions(&revs, prefix);
710 revs.diff = 1;
711 revs.combine_merges = 0;
712 revs.ignore_merges = 1;
713 revs.diffopt.recursive = 1;
715 if (add_pending_commit(head, &revs, 0))
716 die("Unknown commit %s", head);
717 if (add_pending_commit(upstream, &revs, UNINTERESTING))
718 die("Unknown commit %s", upstream);
720 /* Don't say anything if head and upstream are the same. */
721 if (revs.pending.nr == 2) {
722 struct object_array_entry *o = revs.pending.objects;
723 if (hashcmp(o[0].item->sha1, o[1].item->sha1) == 0)
724 return 0;
727 get_patch_ids(&revs, &ids, prefix);
729 if (limit && add_pending_commit(limit, &revs, UNINTERESTING))
730 die("Unknown commit %s", limit);
732 /* reverse the list of commits */
733 prepare_revision_walk(&revs);
734 while ((commit = get_revision(&revs)) != NULL) {
735 /* ignore merges */
736 if (commit->parents && commit->parents->next)
737 continue;
739 commit_list_insert(commit, &list);
742 while (list) {
743 char sign = '+';
745 commit = list->item;
746 if (has_commit_patch_id(commit, &ids))
747 sign = '-';
749 if (verbose) {
750 char *buf = NULL;
751 unsigned long buflen = 0;
752 pretty_print_commit(CMIT_FMT_ONELINE, commit, ~0,
753 &buf, &buflen, 0, NULL, NULL, 0);
754 printf("%c %s %s\n", sign,
755 sha1_to_hex(commit->object.sha1), buf);
756 free(buf);
758 else {
759 printf("%c %s\n", sign,
760 sha1_to_hex(commit->object.sha1));
763 list = list->next;
766 free_patch_ids(&ids);
767 return 0;