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->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")) {
72 for_each_ref(add_ref_decoration, NULL);
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 */
89 commit->buffer = NULL;
91 free_commit_list(commit->parents);
92 commit->parents = NULL;
97 static int git_log_config(const char *var, const char *value)
99 if (!strcmp(var, "format.subjectprefix")) {
101 die("format.subjectprefix without value");
102 fmt_patch_subject_prefix = xstrdup(value);
105 if (!strcmp(var, "log.showroot")) {
106 default_show_root = git_config_bool(var, value);
109 return git_diff_ui_config(var, value);
112 int cmd_whatchanged(int argc, const char **argv, const char *prefix)
116 git_config(git_log_config);
117 init_revisions(&rev, prefix);
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)
130 enum object_type type;
131 char *buf = read_sha1_file(sha1, &type, &size);
135 return error("Could not read object %s", sha1_to_hex(sha1));
138 while (offset < size && buf[offset++] != '\n') {
139 int new_offset = offset;
140 while (new_offset < size && buf[new_offset++] != '\n')
146 fwrite(buf + offset, size - offset, 1, stdout);
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) ? "/" : "");
159 int cmd_show(int argc, const char **argv, const char *prefix)
162 struct object_array_entry *objects;
163 int i, count, ret = 0;
165 git_config(git_log_config);
166 init_revisions(&rev, prefix);
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;
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;
183 ret = show_object(o->sha1, 0);
186 struct tag *t = (struct tag *)o;
188 printf("%stag %s%s\n\n",
189 diff_get_color(rev.diffopt.color_diff,
192 diff_get_color(rev.diffopt.color_diff,
194 ret = show_object(o->sha1, 1);
195 objects[i].item = (struct object *)t->tagged;
200 printf("%stree %s%s\n\n",
201 diff_get_color(rev.diffopt.color_diff,
204 diff_get_color(rev.diffopt.color_diff,
206 read_tree_recursive((struct tree *)o, "", 0, 0, NULL,
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);
216 ret = error("Unknown type: %d", o->type);
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)
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..
251 return cmd_log_walk(&rev);
254 int cmd_log(int argc, const char **argv, const char *prefix)
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);
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")) {
284 die("format.headers without 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);
292 if (!strcmp(var, "format.suffix")) {
294 die("format.suffix without value");
295 fmt_patch_suffix = xstrdup(value);
298 if (!strcmp(var, "diff.color") || !strcmp(var, "color.diff")) {
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,
312 char filename[PATH_MAX];
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);
332 sprintf(filename + len, "%04d", nr);
333 len = strlen(filename);
335 sol = strstr(commit->buffer, "\n\n");
340 /* strip [PATCH] or [PATCH blabla] */
341 if (!keep_subject && !prefixcmp(sol, "[PATCH")) {
342 char *eos = strchr(sol + 6, ']');
344 while (isspace(*eos))
351 j < FORMAT_PATCH_NAME_MAX - suffix_len - 5 &&
352 len < sizeof(filename) - suffix_len &&
353 sol[j] && sol[j] != '\n';
355 if (istitlechar(sol[j])) {
357 filename[len++] = '-';
360 filename[len++] = sol[j];
362 while (sol[j + 1] == '.')
367 while (filename[len - 1] == '.'
368 || filename[len - 1] == '-')
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);
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;
396 o2 = rev->pending.objects[1].item;
399 if ((flags1 & UNINTERESTING) == (flags2 & UNINTERESTING))
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) {
414 if (commit->parents && commit->parents->next)
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);
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;
446 int nr = 0, total, i, j;
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;
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;
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"))
482 else if (!strcmp(argv[i], "-n") ||
483 !strcmp(argv[i], "--numbered"))
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"))
489 else if (!strcmp(argv[i], "--start-number")) {
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")) {
500 else if (!strcmp(argv[i], "--output-directory") ||
501 !strcmp(argv[i], "-o")) {
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;
513 committer = git_committer_info(1);
514 endpos = strchr(committer, '>');
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;
525 else if (!prefixcmp(argv[i], "--attach=")) {
526 rev.mime_boundary = argv[i] + 9;
529 else if (!strcmp(argv[i], "--inline")) {
530 rev.mime_boundary = git_version_string;
533 else if (!prefixcmp(argv[i], "--inline=")) {
534 rev.mime_boundary = argv[i] + 9;
537 else if (!strcmp(argv[i], "--ignore-if-in-upstream"))
538 ignore_if_in_upstream = 1;
539 else if (!strcmp(argv[i], "--thread"))
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")) {
546 die("Need a Message-Id for --in-reply-to");
547 in_reply_to = argv[i];
548 } else if (!prefixcmp(argv[i], "--subject-prefix=")) {
550 rev.subject_prefix = argv[i] + 17;
551 } else if (!prefixcmp(argv[i], "--suffix="))
552 fmt_patch_suffix = argv[i] + 9;
558 if (start_number < 0)
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");
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) {
582 die("standard output, or directory, which one?");
583 if (mkdir(output_directory, 0777) < 0 && errno != EEXIST)
584 die("Could not create directory %s",
588 if (rev.pending.nr == 1) {
589 if (rev.max_count < 0) {
590 rev.pending.objects[0].item->flags |= UNINTERESTING;
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);
602 realstdout = xfdopen(xdup(1), "w");
604 prepare_revision_walk(&rev);
605 while ((commit = get_revision(&rev)) != NULL) {
607 if (commit->parents && commit->parents->next)
610 if (ignore_if_in_upstream &&
611 has_commit_patch_id(commit, &ids))
615 list = xrealloc(list, nr * sizeof(list[0]));
616 list[nr - 1] = commit;
620 rev.total = total + start_number - 1;
621 rev.add_signoff = add_signoff;
622 rev.ref_message_id = in_reply_to;
626 rev.nr = total - nr + (start_number - 1);
627 /* Make the second and subsequent mails replies to the first */
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;
640 if (reopen_stdout(commit, rev.nr, keep_subject,
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.
656 if (rev.mime_boundary)
657 printf("\n--%s%s--\n\n\n",
658 mime_boundary_leader,
661 printf("-- \n%s\n\n", git_version_string);
667 if (ignore_if_in_upstream)
668 free_patch_ids(&ids);
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);
678 commit->object.flags |= flags;
679 add_pending_object(revs, &commit->object, arg);
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;
699 if (argc > 1 && !strcmp(argv[1], "-v")) {
719 init_revisions(&revs, prefix);
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)
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) {
746 if (commit->parents && commit->parents->next)
749 commit_list_insert(commit, &list);
756 if (has_commit_patch_id(commit, &ids))
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);
769 printf("%c %s\n", sign,
770 sha1_to_hex(commit->object.sha1));
776 free_patch_ids(&ids);