More missing static
[git/kirr.git] / builtin-log.c
blob212cdfc769b58403a99155e321ea36f018061ea4
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 for (i = 1; i < argc; i++) {
62 const char *arg = argv[i];
63 if (!prefixcmp(arg, "--encoding=")) {
64 arg += 11;
65 if (strcmp(arg, "none"))
66 git_log_output_encoding = xstrdup(arg);
67 else
68 git_log_output_encoding = "";
69 } else if (!strcmp(arg, "--decorate")) {
70 if (!decorate)
71 for_each_ref(add_ref_decoration, NULL);
72 decorate = 1;
73 } else
74 die("unrecognized argument: %s", arg);
78 static int cmd_log_walk(struct rev_info *rev)
80 struct commit *commit;
82 prepare_revision_walk(rev);
83 while ((commit = get_revision(rev)) != NULL) {
84 log_tree_commit(rev, commit);
85 if (!rev->reflog_info) {
86 /* we allow cycles in reflog ancestry */
87 free(commit->buffer);
88 commit->buffer = NULL;
90 free_commit_list(commit->parents);
91 commit->parents = NULL;
93 return 0;
96 static int git_log_config(const char *var, const char *value)
98 if (!strcmp(var, "log.showroot")) {
99 default_show_root = git_config_bool(var, value);
100 return 0;
102 return git_diff_ui_config(var, value);
105 int cmd_whatchanged(int argc, const char **argv, const char *prefix)
107 struct rev_info rev;
109 git_config(git_log_config);
110 init_revisions(&rev, prefix);
111 rev.diff = 1;
112 rev.diffopt.recursive = 1;
113 rev.simplify_history = 0;
114 cmd_log_init(argc, argv, prefix, &rev);
115 if (!rev.diffopt.output_format)
116 rev.diffopt.output_format = DIFF_FORMAT_RAW;
117 return cmd_log_walk(&rev);
120 static int show_object(const unsigned char *sha1, int suppress_header)
122 unsigned long size;
123 enum object_type type;
124 char *buf = read_sha1_file(sha1, &type, &size);
125 int offset = 0;
127 if (!buf)
128 return error("Could not read object %s", sha1_to_hex(sha1));
130 if (suppress_header)
131 while (offset < size && buf[offset++] != '\n') {
132 int new_offset = offset;
133 while (new_offset < size && buf[new_offset++] != '\n')
134 ; /* do nothing */
135 offset = new_offset;
138 if (offset < size)
139 fwrite(buf + offset, size - offset, 1, stdout);
140 free(buf);
141 return 0;
144 static int show_tree_object(const unsigned char *sha1,
145 const char *base, int baselen,
146 const char *pathname, unsigned mode, int stage)
148 printf("%s%s\n", pathname, S_ISDIR(mode) ? "/" : "");
149 return 0;
152 int cmd_show(int argc, const char **argv, const char *prefix)
154 struct rev_info rev;
155 struct object_array_entry *objects;
156 int i, count, ret = 0;
158 git_config(git_log_config);
159 init_revisions(&rev, prefix);
160 rev.diff = 1;
161 rev.diffopt.recursive = 1;
162 rev.combine_merges = 1;
163 rev.dense_combined_merges = 1;
164 rev.always_show_header = 1;
165 rev.ignore_merges = 0;
166 rev.no_walk = 1;
167 cmd_log_init(argc, argv, prefix, &rev);
169 count = rev.pending.nr;
170 objects = rev.pending.objects;
171 for (i = 0; i < count && !ret; i++) {
172 struct object *o = objects[i].item;
173 const char *name = objects[i].name;
174 switch (o->type) {
175 case OBJ_BLOB:
176 ret = show_object(o->sha1, 0);
177 break;
178 case OBJ_TAG: {
179 struct tag *t = (struct tag *)o;
181 printf("%stag %s%s\n\n",
182 diff_get_color(rev.diffopt.color_diff,
183 DIFF_COMMIT),
184 t->tag,
185 diff_get_color(rev.diffopt.color_diff,
186 DIFF_RESET));
187 ret = show_object(o->sha1, 1);
188 objects[i].item = (struct object *)t->tagged;
189 i--;
190 break;
192 case OBJ_TREE:
193 printf("%stree %s%s\n\n",
194 diff_get_color(rev.diffopt.color_diff,
195 DIFF_COMMIT),
196 name,
197 diff_get_color(rev.diffopt.color_diff,
198 DIFF_RESET));
199 read_tree_recursive((struct tree *)o, "", 0, 0, NULL,
200 show_tree_object);
201 break;
202 case OBJ_COMMIT:
203 rev.pending.nr = rev.pending.alloc = 0;
204 rev.pending.objects = NULL;
205 add_object_array(o, name, &rev.pending);
206 ret = cmd_log_walk(&rev);
207 break;
208 default:
209 ret = error("Unknown type: %d", o->type);
212 free(objects);
213 return ret;
217 * This is equivalent to "git log -g --abbrev-commit --pretty=oneline"
219 int cmd_log_reflog(int argc, const char **argv, const char *prefix)
221 struct rev_info rev;
223 git_config(git_log_config);
224 init_revisions(&rev, prefix);
225 init_reflog_walk(&rev.reflog_info);
226 rev.abbrev_commit = 1;
227 rev.verbose_header = 1;
228 cmd_log_init(argc, argv, prefix, &rev);
231 * This means that we override whatever commit format the user gave
232 * on the cmd line. Sad, but cmd_log_init() currently doesn't
233 * allow us to set a different default.
235 rev.commit_format = CMIT_FMT_ONELINE;
236 rev.always_show_header = 1;
239 * We get called through "git reflog", so unlike the other log
240 * routines, we need to set up our pager manually..
242 setup_pager();
244 return cmd_log_walk(&rev);
247 int cmd_log(int argc, const char **argv, const char *prefix)
249 struct rev_info rev;
251 git_config(git_log_config);
252 init_revisions(&rev, prefix);
253 rev.always_show_header = 1;
254 cmd_log_init(argc, argv, prefix, &rev);
255 return cmd_log_walk(&rev);
258 /* format-patch */
259 #define FORMAT_PATCH_NAME_MAX 64
261 static int istitlechar(char c)
263 return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
264 (c >= '0' && c <= '9') || c == '.' || c == '_';
267 static char *extra_headers = NULL;
268 static int extra_headers_size = 0;
269 static const char *fmt_patch_suffix = ".patch";
271 static int git_format_config(const char *var, const char *value)
273 if (!strcmp(var, "format.headers")) {
274 int len;
276 if (!value)
277 die("format.headers without value");
278 len = strlen(value);
279 extra_headers_size += len + 1;
280 extra_headers = xrealloc(extra_headers, extra_headers_size);
281 extra_headers[extra_headers_size - len - 1] = 0;
282 strcat(extra_headers, value);
283 return 0;
285 if (!strcmp(var, "format.suffix")) {
286 if (!value)
287 die("format.suffix without value");
288 fmt_patch_suffix = xstrdup(value);
289 return 0;
291 if (!strcmp(var, "diff.color") || !strcmp(var, "color.diff")) {
292 return 0;
294 return git_log_config(var, value);
298 static FILE *realstdout = NULL;
299 static const char *output_directory = NULL;
301 static int reopen_stdout(struct commit *commit, int nr, int keep_subject,
302 int numbered_files)
304 char filename[PATH_MAX];
305 char *sol;
306 int len = 0;
307 int suffix_len = strlen(fmt_patch_suffix) + 1;
309 if (output_directory) {
310 if (strlen(output_directory) >=
311 sizeof(filename) - FORMAT_PATCH_NAME_MAX - suffix_len)
312 return error("name of output directory is too long");
313 strlcpy(filename, output_directory, sizeof(filename) - suffix_len);
314 len = strlen(filename);
315 if (filename[len - 1] != '/')
316 filename[len++] = '/';
319 if (numbered_files) {
320 sprintf(filename + len, "%d", nr);
321 len = strlen(filename);
323 } else {
324 sprintf(filename + len, "%04d", nr);
325 len = strlen(filename);
327 sol = strstr(commit->buffer, "\n\n");
328 if (sol) {
329 int j, space = 1;
331 sol += 2;
332 /* strip [PATCH] or [PATCH blabla] */
333 if (!keep_subject && !prefixcmp(sol, "[PATCH")) {
334 char *eos = strchr(sol + 6, ']');
335 if (eos) {
336 while (isspace(*eos))
337 eos++;
338 sol = eos;
342 for (j = 0;
343 j < FORMAT_PATCH_NAME_MAX - suffix_len - 5 &&
344 len < sizeof(filename) - suffix_len &&
345 sol[j] && sol[j] != '\n';
346 j++) {
347 if (istitlechar(sol[j])) {
348 if (space) {
349 filename[len++] = '-';
350 space = 0;
352 filename[len++] = sol[j];
353 if (sol[j] == '.')
354 while (sol[j + 1] == '.')
355 j++;
356 } else
357 space = 1;
359 while (filename[len - 1] == '.'
360 || filename[len - 1] == '-')
361 len--;
362 filename[len] = 0;
364 if (len + suffix_len >= sizeof(filename))
365 return error("Patch pathname too long");
366 strcpy(filename + len, fmt_patch_suffix);
369 fprintf(realstdout, "%s\n", filename);
370 if (freopen(filename, "w", stdout) == NULL)
371 return error("Cannot open patch file %s",filename);
373 return 0;
376 static void get_patch_ids(struct rev_info *rev, struct patch_ids *ids, const char *prefix)
378 struct rev_info check_rev;
379 struct commit *commit;
380 struct object *o1, *o2;
381 unsigned flags1, flags2;
383 if (rev->pending.nr != 2)
384 die("Need exactly one range.");
386 o1 = rev->pending.objects[0].item;
387 flags1 = o1->flags;
388 o2 = rev->pending.objects[1].item;
389 flags2 = o2->flags;
391 if ((flags1 & UNINTERESTING) == (flags2 & UNINTERESTING))
392 die("Not a range.");
394 init_patch_ids(ids);
396 /* given a range a..b get all patch ids for b..a */
397 init_revisions(&check_rev, prefix);
398 o1->flags ^= UNINTERESTING;
399 o2->flags ^= UNINTERESTING;
400 add_pending_object(&check_rev, o1, "o1");
401 add_pending_object(&check_rev, o2, "o2");
402 prepare_revision_walk(&check_rev);
404 while ((commit = get_revision(&check_rev)) != NULL) {
405 /* ignore merges */
406 if (commit->parents && commit->parents->next)
407 continue;
409 add_commit_patch_id(commit, ids);
412 /* reset for next revision walk */
413 clear_commit_marks((struct commit *)o1,
414 SEEN | UNINTERESTING | SHOWN | ADDED);
415 clear_commit_marks((struct commit *)o2,
416 SEEN | UNINTERESTING | SHOWN | ADDED);
417 o1->flags = flags1;
418 o2->flags = flags2;
421 static void gen_message_id(char *dest, unsigned int length, char *base)
423 const char *committer = git_committer_info(-1);
424 const char *email_start = strrchr(committer, '<');
425 const char *email_end = strrchr(committer, '>');
426 if(!email_start || !email_end || email_start > email_end - 1)
427 die("Could not extract email from committer identity.");
428 snprintf(dest, length, "%s.%lu.git.%.*s", base,
429 (unsigned long) time(NULL),
430 (int)(email_end - email_start - 1), email_start + 1);
433 int cmd_format_patch(int argc, const char **argv, const char *prefix)
435 struct commit *commit;
436 struct commit **list = NULL;
437 struct rev_info rev;
438 int nr = 0, total, i, j;
439 int use_stdout = 0;
440 int numbered = 0;
441 int start_number = -1;
442 int keep_subject = 0;
443 int numbered_files = 0; /* _just_ numbers */
444 int subject_prefix = 0;
445 int ignore_if_in_upstream = 0;
446 int thread = 0;
447 const char *in_reply_to = NULL;
448 struct patch_ids ids;
449 char *add_signoff = NULL;
450 char message_id[1024];
451 char ref_message_id[1024];
453 git_config(git_format_config);
454 init_revisions(&rev, prefix);
455 rev.commit_format = CMIT_FMT_EMAIL;
456 rev.verbose_header = 1;
457 rev.diff = 1;
458 rev.combine_merges = 0;
459 rev.ignore_merges = 1;
460 rev.diffopt.msg_sep = "";
461 rev.diffopt.recursive = 1;
463 rev.extra_headers = extra_headers;
466 * Parse the arguments before setup_revisions(), or something
467 * like "git format-patch -o a123 HEAD^.." may fail; a123 is
468 * possibly a valid SHA1.
470 for (i = 1, j = 1; i < argc; i++) {
471 if (!strcmp(argv[i], "--stdout"))
472 use_stdout = 1;
473 else if (!strcmp(argv[i], "-n") ||
474 !strcmp(argv[i], "--numbered"))
475 numbered = 1;
476 else if (!prefixcmp(argv[i], "--start-number="))
477 start_number = strtol(argv[i] + 15, NULL, 10);
478 else if (!strcmp(argv[i], "--numbered-files"))
479 numbered_files = 1;
480 else if (!strcmp(argv[i], "--start-number")) {
481 i++;
482 if (i == argc)
483 die("Need a number for --start-number");
484 start_number = strtol(argv[i], NULL, 10);
486 else if (!strcmp(argv[i], "-k") ||
487 !strcmp(argv[i], "--keep-subject")) {
488 keep_subject = 1;
489 rev.total = -1;
491 else if (!strcmp(argv[i], "--output-directory") ||
492 !strcmp(argv[i], "-o")) {
493 i++;
494 if (argc <= i)
495 die("Which directory?");
496 if (output_directory)
497 die("Two output directories?");
498 output_directory = argv[i];
500 else if (!strcmp(argv[i], "--signoff") ||
501 !strcmp(argv[i], "-s")) {
502 const char *committer;
503 const char *endpos;
504 committer = git_committer_info(1);
505 endpos = strchr(committer, '>');
506 if (!endpos)
507 die("bogos committer info %s\n", committer);
508 add_signoff = xmalloc(endpos - committer + 2);
509 memcpy(add_signoff, committer, endpos - committer + 1);
510 add_signoff[endpos - committer + 1] = 0;
512 else if (!strcmp(argv[i], "--attach")) {
513 rev.mime_boundary = git_version_string;
514 rev.no_inline = 1;
516 else if (!prefixcmp(argv[i], "--attach=")) {
517 rev.mime_boundary = argv[i] + 9;
518 rev.no_inline = 1;
520 else if (!strcmp(argv[i], "--inline")) {
521 rev.mime_boundary = git_version_string;
522 rev.no_inline = 0;
524 else if (!prefixcmp(argv[i], "--inline=")) {
525 rev.mime_boundary = argv[i] + 9;
526 rev.no_inline = 0;
528 else if (!strcmp(argv[i], "--ignore-if-in-upstream"))
529 ignore_if_in_upstream = 1;
530 else if (!strcmp(argv[i], "--thread"))
531 thread = 1;
532 else if (!prefixcmp(argv[i], "--in-reply-to="))
533 in_reply_to = argv[i] + 14;
534 else if (!strcmp(argv[i], "--in-reply-to")) {
535 i++;
536 if (i == argc)
537 die("Need a Message-Id for --in-reply-to");
538 in_reply_to = argv[i];
539 } else if (!prefixcmp(argv[i], "--subject-prefix=")) {
540 subject_prefix = 1;
541 rev.subject_prefix = argv[i] + 17;
542 } else if (!prefixcmp(argv[i], "--suffix="))
543 fmt_patch_suffix = argv[i] + 9;
544 else
545 argv[j++] = argv[i];
547 argc = j;
549 if (start_number < 0)
550 start_number = 1;
551 if (numbered && keep_subject)
552 die ("-n and -k are mutually exclusive.");
553 if (keep_subject && subject_prefix)
554 die ("--subject-prefix and -k are mutually exclusive.");
555 if (numbered_files && use_stdout)
556 die ("--numbered-files and --stdout are mutually exclusive.");
558 argc = setup_revisions(argc, argv, &rev, "HEAD");
559 if (argc > 1)
560 die ("unrecognized argument: %s", argv[1]);
562 if (!rev.diffopt.output_format)
563 rev.diffopt.output_format = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_SUMMARY | DIFF_FORMAT_PATCH;
565 if (!rev.diffopt.text)
566 rev.diffopt.binary = 1;
568 if (!output_directory && !use_stdout)
569 output_directory = prefix;
571 if (output_directory) {
572 if (use_stdout)
573 die("standard output, or directory, which one?");
574 if (mkdir(output_directory, 0777) < 0 && errno != EEXIST)
575 die("Could not create directory %s",
576 output_directory);
579 if (rev.pending.nr == 1) {
580 if (rev.max_count < 0) {
581 rev.pending.objects[0].item->flags |= UNINTERESTING;
582 add_head(&rev);
584 /* Otherwise, it is "format-patch -22 HEAD", and
585 * get_revision() would return only the specified count.
589 if (ignore_if_in_upstream)
590 get_patch_ids(&rev, &ids, prefix);
592 if (!use_stdout)
593 realstdout = fdopen(dup(1), "w");
595 prepare_revision_walk(&rev);
596 while ((commit = get_revision(&rev)) != NULL) {
597 /* ignore merges */
598 if (commit->parents && commit->parents->next)
599 continue;
601 if (ignore_if_in_upstream &&
602 has_commit_patch_id(commit, &ids))
603 continue;
605 nr++;
606 list = xrealloc(list, nr * sizeof(list[0]));
607 list[nr - 1] = commit;
609 total = nr;
610 if (numbered)
611 rev.total = total + start_number - 1;
612 rev.add_signoff = add_signoff;
613 rev.ref_message_id = in_reply_to;
614 while (0 <= --nr) {
615 int shown;
616 commit = list[nr];
617 rev.nr = total - nr + (start_number - 1);
618 /* Make the second and subsequent mails replies to the first */
619 if (thread) {
620 if (nr == (total - 2)) {
621 strncpy(ref_message_id, message_id,
622 sizeof(ref_message_id));
623 ref_message_id[sizeof(ref_message_id)-1]='\0';
624 rev.ref_message_id = ref_message_id;
626 gen_message_id(message_id, sizeof(message_id),
627 sha1_to_hex(commit->object.sha1));
628 rev.message_id = message_id;
630 if (!use_stdout)
631 if (reopen_stdout(commit, rev.nr, keep_subject,
632 numbered_files))
633 die("Failed to create output files");
634 shown = log_tree_commit(&rev, commit);
635 free(commit->buffer);
636 commit->buffer = NULL;
638 /* We put one extra blank line between formatted
639 * patches and this flag is used by log-tree code
640 * to see if it needs to emit a LF before showing
641 * the log; when using one file per patch, we do
642 * not want the extra blank line.
644 if (!use_stdout)
645 rev.shown_one = 0;
646 if (shown) {
647 if (rev.mime_boundary)
648 printf("\n--%s%s--\n\n\n",
649 mime_boundary_leader,
650 rev.mime_boundary);
651 else
652 printf("-- \n%s\n\n", git_version_string);
654 if (!use_stdout)
655 fclose(stdout);
657 free(list);
658 if (ignore_if_in_upstream)
659 free_patch_ids(&ids);
660 return 0;
663 static int add_pending_commit(const char *arg, struct rev_info *revs, int flags)
665 unsigned char sha1[20];
666 if (get_sha1(arg, sha1) == 0) {
667 struct commit *commit = lookup_commit_reference(sha1);
668 if (commit) {
669 commit->object.flags |= flags;
670 add_pending_object(revs, &commit->object, arg);
671 return 0;
674 return -1;
677 static const char cherry_usage[] =
678 "git-cherry [-v] <upstream> [<head>] [<limit>]";
679 int cmd_cherry(int argc, const char **argv, const char *prefix)
681 struct rev_info revs;
682 struct patch_ids ids;
683 struct commit *commit;
684 struct commit_list *list = NULL;
685 const char *upstream;
686 const char *head = "HEAD";
687 const char *limit = NULL;
688 int verbose = 0;
690 if (argc > 1 && !strcmp(argv[1], "-v")) {
691 verbose = 1;
692 argc--;
693 argv++;
696 switch (argc) {
697 case 4:
698 limit = argv[3];
699 /* FALLTHROUGH */
700 case 3:
701 head = argv[2];
702 /* FALLTHROUGH */
703 case 2:
704 upstream = argv[1];
705 break;
706 default:
707 usage(cherry_usage);
710 init_revisions(&revs, prefix);
711 revs.diff = 1;
712 revs.combine_merges = 0;
713 revs.ignore_merges = 1;
714 revs.diffopt.recursive = 1;
716 if (add_pending_commit(head, &revs, 0))
717 die("Unknown commit %s", head);
718 if (add_pending_commit(upstream, &revs, UNINTERESTING))
719 die("Unknown commit %s", upstream);
721 /* Don't say anything if head and upstream are the same. */
722 if (revs.pending.nr == 2) {
723 struct object_array_entry *o = revs.pending.objects;
724 if (hashcmp(o[0].item->sha1, o[1].item->sha1) == 0)
725 return 0;
728 get_patch_ids(&revs, &ids, prefix);
730 if (limit && add_pending_commit(limit, &revs, UNINTERESTING))
731 die("Unknown commit %s", limit);
733 /* reverse the list of commits */
734 prepare_revision_walk(&revs);
735 while ((commit = get_revision(&revs)) != NULL) {
736 /* ignore merges */
737 if (commit->parents && commit->parents->next)
738 continue;
740 commit_list_insert(commit, &list);
743 while (list) {
744 char sign = '+';
746 commit = list->item;
747 if (has_commit_patch_id(commit, &ids))
748 sign = '-';
750 if (verbose) {
751 static char buf[16384];
752 pretty_print_commit(CMIT_FMT_ONELINE, commit, ~0,
753 buf, sizeof(buf), 0, NULL, NULL, 0);
754 printf("%c %s %s\n", sign,
755 sha1_to_hex(commit->object.sha1), buf);
757 else {
758 printf("%c %s\n", sign,
759 sha1_to_hex(commit->object.sha1));
762 list = list->next;
765 free_patch_ids(&ids);
766 return 0;