2 * Builtin "git log" and related commands (show, whatchanged)
4 * (C) Copyright 2006 Linus Torvalds
15 static int default_show_root = 1;
17 /* this is in builtin-diff.c */
18 void add_head(struct rev_info *revs);
20 static void cmd_log_init(int argc, const char **argv, const char *prefix,
25 rev->abbrev = DEFAULT_ABBREV;
26 rev->commit_format = CMIT_FMT_DEFAULT;
27 rev->verbose_header = 1;
28 rev->show_root_diff = default_show_root;
29 argc = setup_revisions(argc, argv, rev, "HEAD");
30 if (rev->diffopt.pickaxe || rev->diffopt.filter)
31 rev->always_show_header = 0;
32 for (i = 1; i < argc; i++) {
33 const char *arg = argv[i];
34 if (!strncmp(arg, "--encoding=", 11)) {
36 if (strcmp(arg, "none"))
37 git_log_output_encoding = strdup(arg);
39 git_log_output_encoding = "";
42 die("unrecognized argument: %s", arg);
46 static int cmd_log_walk(struct rev_info *rev)
48 struct commit *commit;
50 prepare_revision_walk(rev);
51 while ((commit = get_revision(rev)) != NULL) {
52 log_tree_commit(rev, commit);
53 if (!rev->reflog_info) {
54 /* we allow cycles in reflog ancestry */
56 commit->buffer = NULL;
58 free_commit_list(commit->parents);
59 commit->parents = NULL;
64 static int git_log_config(const char *var, const char *value)
66 if (!strcmp(var, "log.showroot")) {
67 default_show_root = git_config_bool(var, value);
70 return git_diff_ui_config(var, value);
73 int cmd_whatchanged(int argc, const char **argv, const char *prefix)
77 git_config(git_log_config);
78 init_revisions(&rev, prefix);
80 rev.diffopt.recursive = 1;
81 rev.simplify_history = 0;
82 cmd_log_init(argc, argv, prefix, &rev);
83 if (!rev.diffopt.output_format)
84 rev.diffopt.output_format = DIFF_FORMAT_RAW;
85 return cmd_log_walk(&rev);
88 static int show_object(const unsigned char *sha1, int suppress_header)
92 char *buf = read_sha1_file(sha1, type, &size);
96 return error("Could not read object %s", sha1_to_hex(sha1));
99 while (offset < size && buf[offset++] != '\n') {
100 int new_offset = offset;
101 while (new_offset < size && buf[new_offset++] != '\n')
107 fwrite(buf + offset, size - offset, 1, stdout);
112 static int show_tree_object(const unsigned char *sha1,
113 const char *base, int baselen,
114 const char *pathname, unsigned mode, int stage)
116 printf("%s%s\n", pathname, S_ISDIR(mode) ? "/" : "");
120 int cmd_show(int argc, const char **argv, const char *prefix)
123 struct object_array_entry *objects;
124 int i, count, ret = 0;
126 git_config(git_log_config);
127 init_revisions(&rev, prefix);
129 rev.diffopt.recursive = 1;
130 rev.combine_merges = 1;
131 rev.dense_combined_merges = 1;
132 rev.always_show_header = 1;
133 rev.ignore_merges = 0;
135 cmd_log_init(argc, argv, prefix, &rev);
137 count = rev.pending.nr;
138 objects = rev.pending.objects;
139 for (i = 0; i < count && !ret; i++) {
140 struct object *o = objects[i].item;
141 const char *name = objects[i].name;
144 ret = show_object(o->sha1, 0);
147 struct tag *t = (struct tag *)o;
149 printf("%stag %s%s\n\n",
150 diff_get_color(rev.diffopt.color_diff,
153 diff_get_color(rev.diffopt.color_diff,
155 ret = show_object(o->sha1, 1);
156 objects[i].item = (struct object *)t->tagged;
161 printf("%stree %s%s\n\n",
162 diff_get_color(rev.diffopt.color_diff,
165 diff_get_color(rev.diffopt.color_diff,
167 read_tree_recursive((struct tree *)o, "", 0, 0, NULL,
171 rev.pending.nr = rev.pending.alloc = 0;
172 rev.pending.objects = NULL;
173 add_object_array(o, name, &rev.pending);
174 ret = cmd_log_walk(&rev);
177 ret = error("Unknown type: %d", o->type);
184 int cmd_log(int argc, const char **argv, const char *prefix)
188 git_config(git_log_config);
189 init_revisions(&rev, prefix);
190 rev.always_show_header = 1;
191 cmd_log_init(argc, argv, prefix, &rev);
192 return cmd_log_walk(&rev);
195 static int istitlechar(char c)
197 return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
198 (c >= '0' && c <= '9') || c == '.' || c == '_';
201 static char *extra_headers = NULL;
202 static int extra_headers_size = 0;
203 static const char *fmt_patch_suffix = ".patch";
205 static int git_format_config(const char *var, const char *value)
207 if (!strcmp(var, "format.headers")) {
211 die("format.headers without value");
213 extra_headers_size += len + 1;
214 extra_headers = xrealloc(extra_headers, extra_headers_size);
215 extra_headers[extra_headers_size - len - 1] = 0;
216 strcat(extra_headers, value);
219 if (!strcmp(var, "format.suffix")) {
221 die("format.suffix without value");
222 fmt_patch_suffix = xstrdup(value);
225 if (!strcmp(var, "diff.color") || !strcmp(var, "color.diff")) {
228 return git_log_config(var, value);
232 static FILE *realstdout = NULL;
233 static const char *output_directory = NULL;
235 static void reopen_stdout(struct commit *commit, int nr, int keep_subject)
240 int suffix_len = strlen(fmt_patch_suffix) + 10; /* ., NUL and slop */
242 if (output_directory) {
243 strlcpy(filename, output_directory, 1000);
244 len = strlen(filename);
245 if (filename[len - 1] != '/')
246 filename[len++] = '/';
249 sprintf(filename + len, "%04d", nr);
250 len = strlen(filename);
252 sol = strstr(commit->buffer, "\n\n");
257 /* strip [PATCH] or [PATCH blabla] */
258 if (!keep_subject && !strncmp(sol, "[PATCH", 6)) {
259 char *eos = strchr(sol + 6, ']');
261 while (isspace(*eos))
268 len < sizeof(filename) - suffix_len &&
269 sol[j] && sol[j] != '\n';
271 if (istitlechar(sol[j])) {
273 filename[len++] = '-';
276 filename[len++] = sol[j];
278 while (sol[j + 1] == '.')
283 while (filename[len - 1] == '.' || filename[len - 1] == '-')
286 strcpy(filename + len, fmt_patch_suffix);
287 fprintf(realstdout, "%s\n", filename);
288 freopen(filename, "w", stdout);
291 static int get_patch_id(struct commit *commit, struct diff_options *options,
295 diff_tree_sha1(commit->parents->item->object.sha1,
296 commit->object.sha1, "", options);
298 diff_root_tree_sha1(commit->object.sha1, "", options);
299 diffcore_std(options);
300 return diff_flush_patch_id(options, sha1);
303 static void get_patch_ids(struct rev_info *rev, struct diff_options *options, const char *prefix)
305 struct rev_info check_rev;
306 struct commit *commit;
307 struct object *o1, *o2;
308 unsigned flags1, flags2;
309 unsigned char sha1[20];
311 if (rev->pending.nr != 2)
312 die("Need exactly one range.");
314 o1 = rev->pending.objects[0].item;
316 o2 = rev->pending.objects[1].item;
319 if ((flags1 & UNINTERESTING) == (flags2 & UNINTERESTING))
323 options->recursive = 1;
324 if (diff_setup_done(options) < 0)
325 die("diff_setup_done failed");
327 /* given a range a..b get all patch ids for b..a */
328 init_revisions(&check_rev, prefix);
329 o1->flags ^= UNINTERESTING;
330 o2->flags ^= UNINTERESTING;
331 add_pending_object(&check_rev, o1, "o1");
332 add_pending_object(&check_rev, o2, "o2");
333 prepare_revision_walk(&check_rev);
335 while ((commit = get_revision(&check_rev)) != NULL) {
337 if (commit->parents && commit->parents->next)
340 if (!get_patch_id(commit, options, sha1))
341 created_object(sha1, xcalloc(1, sizeof(struct object)));
344 /* reset for next revision walk */
345 clear_commit_marks((struct commit *)o1,
346 SEEN | UNINTERESTING | SHOWN | ADDED);
347 clear_commit_marks((struct commit *)o2,
348 SEEN | UNINTERESTING | SHOWN | ADDED);
353 static void gen_message_id(char *dest, unsigned int length, char *base)
355 const char *committer = git_committer_info(1);
356 const char *email_start = strrchr(committer, '<');
357 const char *email_end = strrchr(committer, '>');
358 if(!email_start || !email_end || email_start > email_end - 1)
359 die("Could not extract email from committer identity.");
360 snprintf(dest, length, "%s.%lu.git.%.*s", base,
361 (unsigned long) time(NULL),
362 (int)(email_end - email_start - 1), email_start + 1);
365 int cmd_format_patch(int argc, const char **argv, const char *prefix)
367 struct commit *commit;
368 struct commit **list = NULL;
370 int nr = 0, total, i, j;
373 int start_number = -1;
374 int keep_subject = 0;
375 int ignore_if_in_upstream = 0;
377 const char *in_reply_to = NULL;
378 struct diff_options patch_id_opts;
379 char *add_signoff = NULL;
380 char message_id[1024];
381 char ref_message_id[1024];
384 git_config(git_format_config);
385 init_revisions(&rev, prefix);
386 rev.commit_format = CMIT_FMT_EMAIL;
387 rev.verbose_header = 1;
389 rev.combine_merges = 0;
390 rev.ignore_merges = 1;
391 rev.diffopt.msg_sep = "";
392 rev.diffopt.recursive = 1;
394 rev.extra_headers = extra_headers;
397 * Parse the arguments before setup_revisions(), or something
398 * like "git fmt-patch -o a123 HEAD^.." may fail; a123 is
399 * possibly a valid SHA1.
401 for (i = 1, j = 1; i < argc; i++) {
402 if (!strcmp(argv[i], "--stdout"))
404 else if (!strcmp(argv[i], "-n") ||
405 !strcmp(argv[i], "--numbered"))
407 else if (!strncmp(argv[i], "--start-number=", 15))
408 start_number = strtol(argv[i] + 15, NULL, 10);
409 else if (!strcmp(argv[i], "--start-number")) {
412 die("Need a number for --start-number");
413 start_number = strtol(argv[i], NULL, 10);
415 else if (!strcmp(argv[i], "-k") ||
416 !strcmp(argv[i], "--keep-subject")) {
420 else if (!strcmp(argv[i], "--output-directory") ||
421 !strcmp(argv[i], "-o")) {
424 die("Which directory?");
425 if (output_directory)
426 die("Two output directories?");
427 output_directory = argv[i];
429 else if (!strcmp(argv[i], "--signoff") ||
430 !strcmp(argv[i], "-s")) {
431 const char *committer;
433 committer = git_committer_info(1);
434 endpos = strchr(committer, '>');
436 die("bogos committer info %s\n", committer);
437 add_signoff = xmalloc(endpos - committer + 2);
438 memcpy(add_signoff, committer, endpos - committer + 1);
439 add_signoff[endpos - committer + 1] = 0;
441 else if (!strcmp(argv[i], "--attach"))
442 rev.mime_boundary = git_version_string;
443 else if (!strncmp(argv[i], "--attach=", 9))
444 rev.mime_boundary = argv[i] + 9;
445 else if (!strcmp(argv[i], "--ignore-if-in-upstream"))
446 ignore_if_in_upstream = 1;
447 else if (!strcmp(argv[i], "--thread"))
449 else if (!strncmp(argv[i], "--in-reply-to=", 14))
450 in_reply_to = argv[i] + 14;
451 else if (!strcmp(argv[i], "--in-reply-to")) {
454 die("Need a Message-Id for --in-reply-to");
455 in_reply_to = argv[i];
457 else if (!strncmp(argv[i], "--suffix=", 9))
458 fmt_patch_suffix = argv[i] + 9;
464 if (start_number < 0)
466 if (numbered && keep_subject)
467 die ("-n and -k are mutually exclusive.");
469 argc = setup_revisions(argc, argv, &rev, "HEAD");
471 die ("unrecognized argument: %s", argv[1]);
473 if (!rev.diffopt.output_format)
474 rev.diffopt.output_format = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_SUMMARY | DIFF_FORMAT_PATCH;
476 if (!rev.diffopt.text)
477 rev.diffopt.binary = 1;
479 if (!output_directory)
480 output_directory = prefix;
482 if (output_directory) {
484 die("standard output, or directory, which one?");
485 if (mkdir(output_directory, 0777) < 0 && errno != EEXIST)
486 die("Could not create directory %s",
490 if (rev.pending.nr == 1) {
491 if (rev.max_count < 0) {
492 rev.pending.objects[0].item->flags |= UNINTERESTING;
495 /* Otherwise, it is "format-patch -22 HEAD", and
496 * get_revision() would return only the specified count.
500 if (ignore_if_in_upstream)
501 get_patch_ids(&rev, &patch_id_opts, prefix);
504 realstdout = fdopen(dup(1), "w");
506 prepare_revision_walk(&rev);
507 while ((commit = get_revision(&rev)) != NULL) {
508 unsigned char sha1[20];
511 if (commit->parents && commit->parents->next)
514 if (ignore_if_in_upstream &&
515 !get_patch_id(commit, &patch_id_opts, sha1) &&
520 list = xrealloc(list, nr * sizeof(list[0]));
521 list[nr - 1] = commit;
525 rev.total = total + start_number - 1;
526 rev.add_signoff = add_signoff;
527 rev.ref_message_id = in_reply_to;
531 rev.nr = total - nr + (start_number - 1);
532 /* Make the second and subsequent mails replies to the first */
534 if (nr == (total - 2)) {
535 strncpy(ref_message_id, message_id,
536 sizeof(ref_message_id));
537 ref_message_id[sizeof(ref_message_id)-1]='\0';
538 rev.ref_message_id = ref_message_id;
540 gen_message_id(message_id, sizeof(message_id),
541 sha1_to_hex(commit->object.sha1));
542 rev.message_id = message_id;
545 reopen_stdout(commit, rev.nr, keep_subject);
546 shown = log_tree_commit(&rev, commit);
547 free(commit->buffer);
548 commit->buffer = NULL;
550 /* We put one extra blank line between formatted
551 * patches and this flag is used by log-tree code
552 * to see if it needs to emit a LF before showing
553 * the log; when using one file per patch, we do
554 * not want the extra blank line.
559 if (rev.mime_boundary)
560 printf("\n--%s%s--\n\n\n",
561 mime_boundary_leader,
564 printf("-- \n%s\n\n", git_version_string);
573 static int add_pending_commit(const char *arg, struct rev_info *revs, int flags)
575 unsigned char sha1[20];
576 if (get_sha1(arg, sha1) == 0) {
577 struct commit *commit = lookup_commit_reference(sha1);
579 commit->object.flags |= flags;
580 add_pending_object(revs, &commit->object, arg);
587 static const char cherry_usage[] =
588 "git-cherry [-v] <upstream> [<head>] [<limit>]";
589 int cmd_cherry(int argc, const char **argv, const char *prefix)
591 struct rev_info revs;
592 struct diff_options patch_id_opts;
593 struct commit *commit;
594 struct commit_list *list = NULL;
595 const char *upstream;
596 const char *head = "HEAD";
597 const char *limit = NULL;
600 if (argc > 1 && !strcmp(argv[1], "-v")) {
620 init_revisions(&revs, prefix);
622 revs.combine_merges = 0;
623 revs.ignore_merges = 1;
624 revs.diffopt.recursive = 1;
626 if (add_pending_commit(head, &revs, 0))
627 die("Unknown commit %s", head);
628 if (add_pending_commit(upstream, &revs, UNINTERESTING))
629 die("Unknown commit %s", upstream);
631 /* Don't say anything if head and upstream are the same. */
632 if (revs.pending.nr == 2) {
633 struct object_array_entry *o = revs.pending.objects;
634 if (hashcmp(o[0].item->sha1, o[1].item->sha1) == 0)
638 get_patch_ids(&revs, &patch_id_opts, prefix);
640 if (limit && add_pending_commit(limit, &revs, UNINTERESTING))
641 die("Unknown commit %s", limit);
643 /* reverse the list of commits */
644 prepare_revision_walk(&revs);
645 while ((commit = get_revision(&revs)) != NULL) {
647 if (commit->parents && commit->parents->next)
650 commit_list_insert(commit, &list);
654 unsigned char sha1[20];
658 if (!get_patch_id(commit, &patch_id_opts, sha1) &&
663 static char buf[16384];
664 pretty_print_commit(CMIT_FMT_ONELINE, commit, ~0,
665 buf, sizeof(buf), 0, NULL, NULL, 0);
666 printf("%c %s %s\n", sign,
667 sha1_to_hex(commit->object.sha1), buf);
670 printf("%c %s\n", sign,
671 sha1_to_hex(commit->object.sha1));