cvsserver: always initialize state in argsplit()
[git.git] / builtin-log.c
blob13bae3110e05b804db22149e9eb366669a8147c7
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->show_root_diff = default_show_root;
59 rev->subject_prefix = fmt_patch_subject_prefix;
60 argc = setup_revisions(argc, argv, rev, "HEAD");
61 if (rev->diffopt.pickaxe || rev->diffopt.filter)
62 rev->always_show_header = 0;
63 if (rev->diffopt.follow_renames) {
64 rev->always_show_header = 0;
65 if (rev->diffopt.nr_paths != 1)
66 usage("git logs can only follow renames on one pathname at a time");
68 for (i = 1; i < argc; i++) {
69 const char *arg = argv[i];
70 if (!strcmp(arg, "--decorate")) {
71 if (!decorate)
72 for_each_ref(add_ref_decoration, NULL);
73 decorate = 1;
74 } else
75 die("unrecognized argument: %s", arg);
79 static int cmd_log_walk(struct rev_info *rev)
81 struct commit *commit;
83 prepare_revision_walk(rev);
84 while ((commit = get_revision(rev)) != NULL) {
85 log_tree_commit(rev, commit);
86 if (!rev->reflog_info) {
87 /* we allow cycles in reflog ancestry */
88 free(commit->buffer);
89 commit->buffer = NULL;
91 free_commit_list(commit->parents);
92 commit->parents = NULL;
94 return 0;
97 static int git_log_config(const char *var, const char *value)
99 if (!strcmp(var, "format.subjectprefix")) {
100 if (!value)
101 die("format.subjectprefix without value");
102 fmt_patch_subject_prefix = xstrdup(value);
103 return 0;
105 if (!strcmp(var, "log.showroot")) {
106 default_show_root = git_config_bool(var, value);
107 return 0;
109 return git_diff_ui_config(var, value);
112 int cmd_whatchanged(int argc, const char **argv, const char *prefix)
114 struct rev_info rev;
116 git_config(git_log_config);
117 init_revisions(&rev, prefix);
118 rev.diff = 1;
119 rev.diffopt.recursive = 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.diffopt.recursive = 1;
169 rev.combine_merges = 1;
170 rev.dense_combined_merges = 1;
171 rev.always_show_header = 1;
172 rev.ignore_merges = 0;
173 rev.no_walk = 1;
174 cmd_log_init(argc, argv, prefix, &rev);
176 count = rev.pending.nr;
177 objects = rev.pending.objects;
178 for (i = 0; i < count && !ret; i++) {
179 struct object *o = objects[i].item;
180 const char *name = objects[i].name;
181 switch (o->type) {
182 case OBJ_BLOB:
183 ret = show_object(o->sha1, 0);
184 break;
185 case OBJ_TAG: {
186 struct tag *t = (struct tag *)o;
188 printf("%stag %s%s\n\n",
189 diff_get_color(rev.diffopt.color_diff,
190 DIFF_COMMIT),
191 t->tag,
192 diff_get_color(rev.diffopt.color_diff,
193 DIFF_RESET));
194 ret = show_object(o->sha1, 1);
195 objects[i].item = (struct object *)t->tagged;
196 i--;
197 break;
199 case OBJ_TREE:
200 printf("%stree %s%s\n\n",
201 diff_get_color(rev.diffopt.color_diff,
202 DIFF_COMMIT),
203 name,
204 diff_get_color(rev.diffopt.color_diff,
205 DIFF_RESET));
206 read_tree_recursive((struct tree *)o, "", 0, 0, NULL,
207 show_tree_object);
208 break;
209 case OBJ_COMMIT:
210 rev.pending.nr = rev.pending.alloc = 0;
211 rev.pending.objects = NULL;
212 add_object_array(o, name, &rev.pending);
213 ret = cmd_log_walk(&rev);
214 break;
215 default:
216 ret = error("Unknown type: %d", o->type);
219 free(objects);
220 return ret;
224 * This is equivalent to "git log -g --abbrev-commit --pretty=oneline"
226 int cmd_log_reflog(int argc, const char **argv, const char *prefix)
228 struct rev_info rev;
230 git_config(git_log_config);
231 init_revisions(&rev, prefix);
232 init_reflog_walk(&rev.reflog_info);
233 rev.abbrev_commit = 1;
234 rev.verbose_header = 1;
235 cmd_log_init(argc, argv, prefix, &rev);
238 * This means that we override whatever commit format the user gave
239 * on the cmd line. Sad, but cmd_log_init() currently doesn't
240 * allow us to set a different default.
242 rev.commit_format = CMIT_FMT_ONELINE;
243 rev.always_show_header = 1;
246 * We get called through "git reflog", so unlike the other log
247 * routines, we need to set up our pager manually..
249 setup_pager();
251 return cmd_log_walk(&rev);
254 int cmd_log(int argc, const char **argv, const char *prefix)
256 struct rev_info rev;
258 git_config(git_log_config);
259 init_revisions(&rev, prefix);
260 rev.always_show_header = 1;
261 cmd_log_init(argc, argv, prefix, &rev);
262 return cmd_log_walk(&rev);
265 /* format-patch */
266 #define FORMAT_PATCH_NAME_MAX 64
268 static int istitlechar(char c)
270 return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
271 (c >= '0' && c <= '9') || c == '.' || c == '_';
274 static char *extra_headers = NULL;
275 static int extra_headers_size = 0;
276 static const char *fmt_patch_suffix = ".patch";
278 static int git_format_config(const char *var, const char *value)
280 if (!strcmp(var, "format.headers")) {
281 int len;
283 if (!value)
284 die("format.headers without value");
285 len = strlen(value);
286 extra_headers_size += len + 1;
287 extra_headers = xrealloc(extra_headers, extra_headers_size);
288 extra_headers[extra_headers_size - len - 1] = 0;
289 strcat(extra_headers, value);
290 return 0;
292 if (!strcmp(var, "format.suffix")) {
293 if (!value)
294 die("format.suffix without value");
295 fmt_patch_suffix = xstrdup(value);
296 return 0;
298 if (!strcmp(var, "diff.color") || !strcmp(var, "color.diff")) {
299 return 0;
302 return git_log_config(var, value);
306 static FILE *realstdout = NULL;
307 static const char *output_directory = NULL;
309 static int reopen_stdout(struct commit *commit, int nr, int keep_subject,
310 int numbered_files)
312 char filename[PATH_MAX];
313 char *sol;
314 int len = 0;
315 int suffix_len = strlen(fmt_patch_suffix) + 1;
317 if (output_directory) {
318 if (strlen(output_directory) >=
319 sizeof(filename) - FORMAT_PATCH_NAME_MAX - suffix_len)
320 return error("name of output directory is too long");
321 strlcpy(filename, output_directory, sizeof(filename) - suffix_len);
322 len = strlen(filename);
323 if (filename[len - 1] != '/')
324 filename[len++] = '/';
327 if (numbered_files) {
328 sprintf(filename + len, "%d", nr);
329 len = strlen(filename);
331 } else {
332 sprintf(filename + len, "%04d", nr);
333 len = strlen(filename);
335 sol = strstr(commit->buffer, "\n\n");
336 if (sol) {
337 int j, space = 1;
339 sol += 2;
340 /* strip [PATCH] or [PATCH blabla] */
341 if (!keep_subject && !prefixcmp(sol, "[PATCH")) {
342 char *eos = strchr(sol + 6, ']');
343 if (eos) {
344 while (isspace(*eos))
345 eos++;
346 sol = eos;
350 for (j = 0;
351 j < FORMAT_PATCH_NAME_MAX - suffix_len - 5 &&
352 len < sizeof(filename) - suffix_len &&
353 sol[j] && sol[j] != '\n';
354 j++) {
355 if (istitlechar(sol[j])) {
356 if (space) {
357 filename[len++] = '-';
358 space = 0;
360 filename[len++] = sol[j];
361 if (sol[j] == '.')
362 while (sol[j + 1] == '.')
363 j++;
364 } else
365 space = 1;
367 while (filename[len - 1] == '.'
368 || filename[len - 1] == '-')
369 len--;
370 filename[len] = 0;
372 if (len + suffix_len >= sizeof(filename))
373 return error("Patch pathname too long");
374 strcpy(filename + len, fmt_patch_suffix);
377 fprintf(realstdout, "%s\n", filename);
378 if (freopen(filename, "w", stdout) == NULL)
379 return error("Cannot open patch file %s",filename);
381 return 0;
384 static void get_patch_ids(struct rev_info *rev, struct patch_ids *ids, const char *prefix)
386 struct rev_info check_rev;
387 struct commit *commit;
388 struct object *o1, *o2;
389 unsigned flags1, flags2;
391 if (rev->pending.nr != 2)
392 die("Need exactly one range.");
394 o1 = rev->pending.objects[0].item;
395 flags1 = o1->flags;
396 o2 = rev->pending.objects[1].item;
397 flags2 = o2->flags;
399 if ((flags1 & UNINTERESTING) == (flags2 & UNINTERESTING))
400 die("Not a range.");
402 init_patch_ids(ids);
404 /* given a range a..b get all patch ids for b..a */
405 init_revisions(&check_rev, prefix);
406 o1->flags ^= UNINTERESTING;
407 o2->flags ^= UNINTERESTING;
408 add_pending_object(&check_rev, o1, "o1");
409 add_pending_object(&check_rev, o2, "o2");
410 prepare_revision_walk(&check_rev);
412 while ((commit = get_revision(&check_rev)) != NULL) {
413 /* ignore merges */
414 if (commit->parents && commit->parents->next)
415 continue;
417 add_commit_patch_id(commit, ids);
420 /* reset for next revision walk */
421 clear_commit_marks((struct commit *)o1,
422 SEEN | UNINTERESTING | SHOWN | ADDED);
423 clear_commit_marks((struct commit *)o2,
424 SEEN | UNINTERESTING | SHOWN | ADDED);
425 o1->flags = flags1;
426 o2->flags = flags2;
429 static void gen_message_id(char *dest, unsigned int length, char *base)
431 const char *committer = git_committer_info(-1);
432 const char *email_start = strrchr(committer, '<');
433 const char *email_end = strrchr(committer, '>');
434 if(!email_start || !email_end || email_start > email_end - 1)
435 die("Could not extract email from committer identity.");
436 snprintf(dest, length, "%s.%lu.git.%.*s", base,
437 (unsigned long) time(NULL),
438 (int)(email_end - email_start - 1), email_start + 1);
441 int cmd_format_patch(int argc, const char **argv, const char *prefix)
443 struct commit *commit;
444 struct commit **list = NULL;
445 struct rev_info rev;
446 int nr = 0, total, i, j;
447 int use_stdout = 0;
448 int numbered = 0;
449 int start_number = -1;
450 int keep_subject = 0;
451 int numbered_files = 0; /* _just_ numbers */
452 int subject_prefix = 0;
453 int ignore_if_in_upstream = 0;
454 int thread = 0;
455 const char *in_reply_to = NULL;
456 struct patch_ids ids;
457 char *add_signoff = NULL;
458 char message_id[1024];
459 char ref_message_id[1024];
461 git_config(git_format_config);
462 init_revisions(&rev, prefix);
463 rev.commit_format = CMIT_FMT_EMAIL;
464 rev.verbose_header = 1;
465 rev.diff = 1;
466 rev.combine_merges = 0;
467 rev.ignore_merges = 1;
468 rev.diffopt.msg_sep = "";
469 rev.diffopt.recursive = 1;
471 rev.subject_prefix = fmt_patch_subject_prefix;
472 rev.extra_headers = extra_headers;
475 * Parse the arguments before setup_revisions(), or something
476 * like "git format-patch -o a123 HEAD^.." may fail; a123 is
477 * possibly a valid SHA1.
479 for (i = 1, j = 1; i < argc; i++) {
480 if (!strcmp(argv[i], "--stdout"))
481 use_stdout = 1;
482 else if (!strcmp(argv[i], "-n") ||
483 !strcmp(argv[i], "--numbered"))
484 numbered = 1;
485 else if (!prefixcmp(argv[i], "--start-number="))
486 start_number = strtol(argv[i] + 15, NULL, 10);
487 else if (!strcmp(argv[i], "--numbered-files"))
488 numbered_files = 1;
489 else if (!strcmp(argv[i], "--start-number")) {
490 i++;
491 if (i == argc)
492 die("Need a number for --start-number");
493 start_number = strtol(argv[i], NULL, 10);
495 else if (!strcmp(argv[i], "-k") ||
496 !strcmp(argv[i], "--keep-subject")) {
497 keep_subject = 1;
498 rev.total = -1;
500 else if (!strcmp(argv[i], "--output-directory") ||
501 !strcmp(argv[i], "-o")) {
502 i++;
503 if (argc <= i)
504 die("Which directory?");
505 if (output_directory)
506 die("Two output directories?");
507 output_directory = argv[i];
509 else if (!strcmp(argv[i], "--signoff") ||
510 !strcmp(argv[i], "-s")) {
511 const char *committer;
512 const char *endpos;
513 committer = git_committer_info(1);
514 endpos = strchr(committer, '>');
515 if (!endpos)
516 die("bogos committer info %s\n", committer);
517 add_signoff = xmalloc(endpos - committer + 2);
518 memcpy(add_signoff, committer, endpos - committer + 1);
519 add_signoff[endpos - committer + 1] = 0;
521 else if (!strcmp(argv[i], "--attach")) {
522 rev.mime_boundary = git_version_string;
523 rev.no_inline = 1;
525 else if (!prefixcmp(argv[i], "--attach=")) {
526 rev.mime_boundary = argv[i] + 9;
527 rev.no_inline = 1;
529 else if (!strcmp(argv[i], "--inline")) {
530 rev.mime_boundary = git_version_string;
531 rev.no_inline = 0;
533 else if (!prefixcmp(argv[i], "--inline=")) {
534 rev.mime_boundary = argv[i] + 9;
535 rev.no_inline = 0;
537 else if (!strcmp(argv[i], "--ignore-if-in-upstream"))
538 ignore_if_in_upstream = 1;
539 else if (!strcmp(argv[i], "--thread"))
540 thread = 1;
541 else if (!prefixcmp(argv[i], "--in-reply-to="))
542 in_reply_to = argv[i] + 14;
543 else if (!strcmp(argv[i], "--in-reply-to")) {
544 i++;
545 if (i == argc)
546 die("Need a Message-Id for --in-reply-to");
547 in_reply_to = argv[i];
548 } else if (!prefixcmp(argv[i], "--subject-prefix=")) {
549 subject_prefix = 1;
550 rev.subject_prefix = argv[i] + 17;
551 } else if (!prefixcmp(argv[i], "--suffix="))
552 fmt_patch_suffix = argv[i] + 9;
553 else
554 argv[j++] = argv[i];
556 argc = j;
558 if (start_number < 0)
559 start_number = 1;
560 if (numbered && keep_subject)
561 die ("-n and -k are mutually exclusive.");
562 if (keep_subject && subject_prefix)
563 die ("--subject-prefix and -k are mutually exclusive.");
564 if (numbered_files && use_stdout)
565 die ("--numbered-files and --stdout are mutually exclusive.");
567 argc = setup_revisions(argc, argv, &rev, "HEAD");
568 if (argc > 1)
569 die ("unrecognized argument: %s", argv[1]);
571 if (!rev.diffopt.output_format)
572 rev.diffopt.output_format = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_SUMMARY | DIFF_FORMAT_PATCH;
574 if (!rev.diffopt.text)
575 rev.diffopt.binary = 1;
577 if (!output_directory && !use_stdout)
578 output_directory = prefix;
580 if (output_directory) {
581 if (use_stdout)
582 die("standard output, or directory, which one?");
583 if (mkdir(output_directory, 0777) < 0 && errno != EEXIST)
584 die("Could not create directory %s",
585 output_directory);
588 if (rev.pending.nr == 1) {
589 if (rev.max_count < 0) {
590 rev.pending.objects[0].item->flags |= UNINTERESTING;
591 add_head(&rev);
593 /* Otherwise, it is "format-patch -22 HEAD", and
594 * get_revision() would return only the specified count.
598 if (ignore_if_in_upstream)
599 get_patch_ids(&rev, &ids, prefix);
601 if (!use_stdout)
602 realstdout = xfdopen(xdup(1), "w");
604 prepare_revision_walk(&rev);
605 while ((commit = get_revision(&rev)) != NULL) {
606 /* ignore merges */
607 if (commit->parents && commit->parents->next)
608 continue;
610 if (ignore_if_in_upstream &&
611 has_commit_patch_id(commit, &ids))
612 continue;
614 nr++;
615 list = xrealloc(list, nr * sizeof(list[0]));
616 list[nr - 1] = commit;
618 total = nr;
619 if (numbered)
620 rev.total = total + start_number - 1;
621 rev.add_signoff = add_signoff;
622 rev.ref_message_id = in_reply_to;
623 while (0 <= --nr) {
624 int shown;
625 commit = list[nr];
626 rev.nr = total - nr + (start_number - 1);
627 /* Make the second and subsequent mails replies to the first */
628 if (thread) {
629 if (nr == (total - 2)) {
630 strncpy(ref_message_id, message_id,
631 sizeof(ref_message_id));
632 ref_message_id[sizeof(ref_message_id)-1]='\0';
633 rev.ref_message_id = ref_message_id;
635 gen_message_id(message_id, sizeof(message_id),
636 sha1_to_hex(commit->object.sha1));
637 rev.message_id = message_id;
639 if (!use_stdout)
640 if (reopen_stdout(commit, rev.nr, keep_subject,
641 numbered_files))
642 die("Failed to create output files");
643 shown = log_tree_commit(&rev, commit);
644 free(commit->buffer);
645 commit->buffer = NULL;
647 /* We put one extra blank line between formatted
648 * patches and this flag is used by log-tree code
649 * to see if it needs to emit a LF before showing
650 * the log; when using one file per patch, we do
651 * not want the extra blank line.
653 if (!use_stdout)
654 rev.shown_one = 0;
655 if (shown) {
656 if (rev.mime_boundary)
657 printf("\n--%s%s--\n\n\n",
658 mime_boundary_leader,
659 rev.mime_boundary);
660 else
661 printf("-- \n%s\n\n", git_version_string);
663 if (!use_stdout)
664 fclose(stdout);
666 free(list);
667 if (ignore_if_in_upstream)
668 free_patch_ids(&ids);
669 return 0;
672 static int add_pending_commit(const char *arg, struct rev_info *revs, int flags)
674 unsigned char sha1[20];
675 if (get_sha1(arg, sha1) == 0) {
676 struct commit *commit = lookup_commit_reference(sha1);
677 if (commit) {
678 commit->object.flags |= flags;
679 add_pending_object(revs, &commit->object, arg);
680 return 0;
683 return -1;
686 static const char cherry_usage[] =
687 "git-cherry [-v] <upstream> [<head>] [<limit>]";
688 int cmd_cherry(int argc, const char **argv, const char *prefix)
690 struct rev_info revs;
691 struct patch_ids ids;
692 struct commit *commit;
693 struct commit_list *list = NULL;
694 const char *upstream;
695 const char *head = "HEAD";
696 const char *limit = NULL;
697 int verbose = 0;
699 if (argc > 1 && !strcmp(argv[1], "-v")) {
700 verbose = 1;
701 argc--;
702 argv++;
705 switch (argc) {
706 case 4:
707 limit = argv[3];
708 /* FALLTHROUGH */
709 case 3:
710 head = argv[2];
711 /* FALLTHROUGH */
712 case 2:
713 upstream = argv[1];
714 break;
715 default:
716 usage(cherry_usage);
719 init_revisions(&revs, prefix);
720 revs.diff = 1;
721 revs.combine_merges = 0;
722 revs.ignore_merges = 1;
723 revs.diffopt.recursive = 1;
725 if (add_pending_commit(head, &revs, 0))
726 die("Unknown commit %s", head);
727 if (add_pending_commit(upstream, &revs, UNINTERESTING))
728 die("Unknown commit %s", upstream);
730 /* Don't say anything if head and upstream are the same. */
731 if (revs.pending.nr == 2) {
732 struct object_array_entry *o = revs.pending.objects;
733 if (hashcmp(o[0].item->sha1, o[1].item->sha1) == 0)
734 return 0;
737 get_patch_ids(&revs, &ids, prefix);
739 if (limit && add_pending_commit(limit, &revs, UNINTERESTING))
740 die("Unknown commit %s", limit);
742 /* reverse the list of commits */
743 prepare_revision_walk(&revs);
744 while ((commit = get_revision(&revs)) != NULL) {
745 /* ignore merges */
746 if (commit->parents && commit->parents->next)
747 continue;
749 commit_list_insert(commit, &list);
752 while (list) {
753 char sign = '+';
755 commit = list->item;
756 if (has_commit_patch_id(commit, &ids))
757 sign = '-';
759 if (verbose) {
760 char *buf = NULL;
761 unsigned long buflen = 0;
762 pretty_print_commit(CMIT_FMT_ONELINE, commit, ~0,
763 &buf, &buflen, 0, NULL, NULL, 0);
764 printf("%c %s %s\n", sign,
765 sha1_to_hex(commit->object.sha1), buf);
766 free(buf);
768 else {
769 printf("%c %s\n", sign,
770 sha1_to_hex(commit->object.sha1));
773 list = list->next;
776 free_patch_ids(&ids);
777 return 0;