--walk-reflogs: actually find the right commit by date.
[git/mingw/4msysgit/wingit-dll.git] / builtin-log.c
blobf3cff13edc80afad27b9f7930a0a736ba5aa6d16
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"
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,
21 struct rev_info *rev)
23 int i;
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)) {
35 arg += 11;
36 if (strcmp(arg, "none"))
37 git_log_output_encoding = strdup(arg);
38 else
39 git_log_output_encoding = "";
41 else
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 free(commit->buffer);
54 commit->buffer = NULL;
55 free_commit_list(commit->parents);
56 commit->parents = NULL;
58 return 0;
61 static int git_log_config(const char *var, const char *value)
63 if (!strcmp(var, "log.showroot")) {
64 default_show_root = git_config_bool(var, value);
65 return 0;
67 return git_diff_ui_config(var, value);
70 int cmd_whatchanged(int argc, const char **argv, const char *prefix)
72 struct rev_info rev;
74 git_config(git_log_config);
75 init_revisions(&rev, prefix);
76 rev.diff = 1;
77 rev.diffopt.recursive = 1;
78 rev.simplify_history = 0;
79 cmd_log_init(argc, argv, prefix, &rev);
80 if (!rev.diffopt.output_format)
81 rev.diffopt.output_format = DIFF_FORMAT_RAW;
82 return cmd_log_walk(&rev);
85 static int show_object(const unsigned char *sha1, int suppress_header)
87 unsigned long size;
88 char type[20];
89 char *buf = read_sha1_file(sha1, type, &size);
90 int offset = 0;
92 if (!buf)
93 return error("Could not read object %s", sha1_to_hex(sha1));
95 if (suppress_header)
96 while (offset < size && buf[offset++] != '\n') {
97 int new_offset = offset;
98 while (new_offset < size && buf[new_offset++] != '\n')
99 ; /* do nothing */
100 offset = new_offset;
103 if (offset < size)
104 fwrite(buf + offset, size - offset, 1, stdout);
105 free(buf);
106 return 0;
109 static int show_tree_object(const unsigned char *sha1,
110 const char *base, int baselen,
111 const char *pathname, unsigned mode, int stage)
113 printf("%s%s\n", pathname, S_ISDIR(mode) ? "/" : "");
114 return 0;
117 int cmd_show(int argc, const char **argv, const char *prefix)
119 struct rev_info rev;
120 struct object_array_entry *objects;
121 int i, count, ret = 0;
123 git_config(git_log_config);
124 init_revisions(&rev, prefix);
125 rev.diff = 1;
126 rev.diffopt.recursive = 1;
127 rev.combine_merges = 1;
128 rev.dense_combined_merges = 1;
129 rev.always_show_header = 1;
130 rev.ignore_merges = 0;
131 rev.no_walk = 1;
132 cmd_log_init(argc, argv, prefix, &rev);
134 count = rev.pending.nr;
135 objects = rev.pending.objects;
136 for (i = 0; i < count && !ret; i++) {
137 struct object *o = objects[i].item;
138 const char *name = objects[i].name;
139 switch (o->type) {
140 case OBJ_BLOB:
141 ret = show_object(o->sha1, 0);
142 break;
143 case OBJ_TAG: {
144 struct tag *t = (struct tag *)o;
146 printf("%stag %s%s\n\n",
147 diff_get_color(rev.diffopt.color_diff,
148 DIFF_COMMIT),
149 t->tag,
150 diff_get_color(rev.diffopt.color_diff,
151 DIFF_RESET));
152 ret = show_object(o->sha1, 1);
153 objects[i].item = (struct object *)t->tagged;
154 i--;
155 break;
157 case OBJ_TREE:
158 printf("%stree %s%s\n\n",
159 diff_get_color(rev.diffopt.color_diff,
160 DIFF_COMMIT),
161 name,
162 diff_get_color(rev.diffopt.color_diff,
163 DIFF_RESET));
164 read_tree_recursive((struct tree *)o, "", 0, 0, NULL,
165 show_tree_object);
166 break;
167 case OBJ_COMMIT:
168 rev.pending.nr = rev.pending.alloc = 0;
169 rev.pending.objects = NULL;
170 add_object_array(o, name, &rev.pending);
171 ret = cmd_log_walk(&rev);
172 break;
173 default:
174 ret = error("Unknown type: %d", o->type);
177 free(objects);
178 return ret;
181 int cmd_log(int argc, const char **argv, const char *prefix)
183 struct rev_info rev;
185 git_config(git_log_config);
186 init_revisions(&rev, prefix);
187 rev.always_show_header = 1;
188 cmd_log_init(argc, argv, prefix, &rev);
189 return cmd_log_walk(&rev);
192 static int istitlechar(char c)
194 return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
195 (c >= '0' && c <= '9') || c == '.' || c == '_';
198 static char *extra_headers = NULL;
199 static int extra_headers_size = 0;
200 static const char *fmt_patch_suffix = ".patch";
202 static int git_format_config(const char *var, const char *value)
204 if (!strcmp(var, "format.headers")) {
205 int len;
207 if (!value)
208 die("format.headers without value");
209 len = strlen(value);
210 extra_headers_size += len + 1;
211 extra_headers = xrealloc(extra_headers, extra_headers_size);
212 extra_headers[extra_headers_size - len - 1] = 0;
213 strcat(extra_headers, value);
214 return 0;
216 if (!strcmp(var, "format.suffix")) {
217 if (!value)
218 die("format.suffix without value");
219 fmt_patch_suffix = xstrdup(value);
220 return 0;
222 if (!strcmp(var, "diff.color") || !strcmp(var, "color.diff")) {
223 return 0;
225 return git_log_config(var, value);
229 static FILE *realstdout = NULL;
230 static const char *output_directory = NULL;
232 static void reopen_stdout(struct commit *commit, int nr, int keep_subject)
234 char filename[1024];
235 char *sol;
236 int len = 0;
237 int suffix_len = strlen(fmt_patch_suffix) + 10; /* ., NUL and slop */
239 if (output_directory) {
240 strlcpy(filename, output_directory, 1000);
241 len = strlen(filename);
242 if (filename[len - 1] != '/')
243 filename[len++] = '/';
246 sprintf(filename + len, "%04d", nr);
247 len = strlen(filename);
249 sol = strstr(commit->buffer, "\n\n");
250 if (sol) {
251 int j, space = 1;
253 sol += 2;
254 /* strip [PATCH] or [PATCH blabla] */
255 if (!keep_subject && !strncmp(sol, "[PATCH", 6)) {
256 char *eos = strchr(sol + 6, ']');
257 if (eos) {
258 while (isspace(*eos))
259 eos++;
260 sol = eos;
264 for (j = 0;
265 len < sizeof(filename) - suffix_len &&
266 sol[j] && sol[j] != '\n';
267 j++) {
268 if (istitlechar(sol[j])) {
269 if (space) {
270 filename[len++] = '-';
271 space = 0;
273 filename[len++] = sol[j];
274 if (sol[j] == '.')
275 while (sol[j + 1] == '.')
276 j++;
277 } else
278 space = 1;
280 while (filename[len - 1] == '.' || filename[len - 1] == '-')
281 len--;
283 strcpy(filename + len, fmt_patch_suffix);
284 fprintf(realstdout, "%s\n", filename);
285 freopen(filename, "w", stdout);
288 static int get_patch_id(struct commit *commit, struct diff_options *options,
289 unsigned char *sha1)
291 if (commit->parents)
292 diff_tree_sha1(commit->parents->item->object.sha1,
293 commit->object.sha1, "", options);
294 else
295 diff_root_tree_sha1(commit->object.sha1, "", options);
296 diffcore_std(options);
297 return diff_flush_patch_id(options, sha1);
300 static void get_patch_ids(struct rev_info *rev, struct diff_options *options, const char *prefix)
302 struct rev_info check_rev;
303 struct commit *commit;
304 struct object *o1, *o2;
305 unsigned flags1, flags2;
306 unsigned char sha1[20];
308 if (rev->pending.nr != 2)
309 die("Need exactly one range.");
311 o1 = rev->pending.objects[0].item;
312 flags1 = o1->flags;
313 o2 = rev->pending.objects[1].item;
314 flags2 = o2->flags;
316 if ((flags1 & UNINTERESTING) == (flags2 & UNINTERESTING))
317 die("Not a range.");
319 diff_setup(options);
320 options->recursive = 1;
321 if (diff_setup_done(options) < 0)
322 die("diff_setup_done failed");
324 /* given a range a..b get all patch ids for b..a */
325 init_revisions(&check_rev, prefix);
326 o1->flags ^= UNINTERESTING;
327 o2->flags ^= UNINTERESTING;
328 add_pending_object(&check_rev, o1, "o1");
329 add_pending_object(&check_rev, o2, "o2");
330 prepare_revision_walk(&check_rev);
332 while ((commit = get_revision(&check_rev)) != NULL) {
333 /* ignore merges */
334 if (commit->parents && commit->parents->next)
335 continue;
337 if (!get_patch_id(commit, options, sha1))
338 created_object(sha1, xcalloc(1, sizeof(struct object)));
341 /* reset for next revision walk */
342 clear_commit_marks((struct commit *)o1,
343 SEEN | UNINTERESTING | SHOWN | ADDED);
344 clear_commit_marks((struct commit *)o2,
345 SEEN | UNINTERESTING | SHOWN | ADDED);
346 o1->flags = flags1;
347 o2->flags = flags2;
350 static void gen_message_id(char *dest, unsigned int length, char *base)
352 const char *committer = git_committer_info(1);
353 const char *email_start = strrchr(committer, '<');
354 const char *email_end = strrchr(committer, '>');
355 if(!email_start || !email_end || email_start > email_end - 1)
356 die("Could not extract email from committer identity.");
357 snprintf(dest, length, "%s.%lu.git.%.*s", base,
358 (unsigned long) time(NULL),
359 (int)(email_end - email_start - 1), email_start + 1);
362 int cmd_format_patch(int argc, const char **argv, const char *prefix)
364 struct commit *commit;
365 struct commit **list = NULL;
366 struct rev_info rev;
367 int nr = 0, total, i, j;
368 int use_stdout = 0;
369 int numbered = 0;
370 int start_number = -1;
371 int keep_subject = 0;
372 int ignore_if_in_upstream = 0;
373 int thread = 0;
374 const char *in_reply_to = NULL;
375 struct diff_options patch_id_opts;
376 char *add_signoff = NULL;
377 char message_id[1024];
378 char ref_message_id[1024];
380 setup_ident();
381 git_config(git_format_config);
382 init_revisions(&rev, prefix);
383 rev.commit_format = CMIT_FMT_EMAIL;
384 rev.verbose_header = 1;
385 rev.diff = 1;
386 rev.combine_merges = 0;
387 rev.ignore_merges = 1;
388 rev.diffopt.msg_sep = "";
389 rev.diffopt.recursive = 1;
391 rev.extra_headers = extra_headers;
394 * Parse the arguments before setup_revisions(), or something
395 * like "git fmt-patch -o a123 HEAD^.." may fail; a123 is
396 * possibly a valid SHA1.
398 for (i = 1, j = 1; i < argc; i++) {
399 if (!strcmp(argv[i], "--stdout"))
400 use_stdout = 1;
401 else if (!strcmp(argv[i], "-n") ||
402 !strcmp(argv[i], "--numbered"))
403 numbered = 1;
404 else if (!strncmp(argv[i], "--start-number=", 15))
405 start_number = strtol(argv[i] + 15, NULL, 10);
406 else if (!strcmp(argv[i], "--start-number")) {
407 i++;
408 if (i == argc)
409 die("Need a number for --start-number");
410 start_number = strtol(argv[i], NULL, 10);
412 else if (!strcmp(argv[i], "-k") ||
413 !strcmp(argv[i], "--keep-subject")) {
414 keep_subject = 1;
415 rev.total = -1;
417 else if (!strcmp(argv[i], "--output-directory") ||
418 !strcmp(argv[i], "-o")) {
419 i++;
420 if (argc <= i)
421 die("Which directory?");
422 if (output_directory)
423 die("Two output directories?");
424 output_directory = argv[i];
426 else if (!strcmp(argv[i], "--signoff") ||
427 !strcmp(argv[i], "-s")) {
428 const char *committer;
429 const char *endpos;
430 committer = git_committer_info(1);
431 endpos = strchr(committer, '>');
432 if (!endpos)
433 die("bogos committer info %s\n", committer);
434 add_signoff = xmalloc(endpos - committer + 2);
435 memcpy(add_signoff, committer, endpos - committer + 1);
436 add_signoff[endpos - committer + 1] = 0;
438 else if (!strcmp(argv[i], "--attach"))
439 rev.mime_boundary = git_version_string;
440 else if (!strncmp(argv[i], "--attach=", 9))
441 rev.mime_boundary = argv[i] + 9;
442 else if (!strcmp(argv[i], "--ignore-if-in-upstream"))
443 ignore_if_in_upstream = 1;
444 else if (!strcmp(argv[i], "--thread"))
445 thread = 1;
446 else if (!strncmp(argv[i], "--in-reply-to=", 14))
447 in_reply_to = argv[i] + 14;
448 else if (!strcmp(argv[i], "--in-reply-to")) {
449 i++;
450 if (i == argc)
451 die("Need a Message-Id for --in-reply-to");
452 in_reply_to = argv[i];
454 else if (!strncmp(argv[i], "--suffix=", 9))
455 fmt_patch_suffix = argv[i] + 9;
456 else
457 argv[j++] = argv[i];
459 argc = j;
461 if (start_number < 0)
462 start_number = 1;
463 if (numbered && keep_subject)
464 die ("-n and -k are mutually exclusive.");
466 argc = setup_revisions(argc, argv, &rev, "HEAD");
467 if (argc > 1)
468 die ("unrecognized argument: %s", argv[1]);
470 if (!rev.diffopt.output_format)
471 rev.diffopt.output_format = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_SUMMARY | DIFF_FORMAT_PATCH;
473 if (!rev.diffopt.text)
474 rev.diffopt.binary = 1;
476 if (!output_directory)
477 output_directory = prefix;
479 if (output_directory) {
480 if (use_stdout)
481 die("standard output, or directory, which one?");
482 if (mkdir(output_directory, 0777) < 0 && errno != EEXIST)
483 die("Could not create directory %s",
484 output_directory);
487 if (rev.pending.nr == 1) {
488 if (rev.max_count < 0) {
489 rev.pending.objects[0].item->flags |= UNINTERESTING;
490 add_head(&rev);
492 /* Otherwise, it is "format-patch -22 HEAD", and
493 * get_revision() would return only the specified count.
497 if (ignore_if_in_upstream)
498 get_patch_ids(&rev, &patch_id_opts, prefix);
500 if (!use_stdout)
501 realstdout = fdopen(dup(1), "w");
503 prepare_revision_walk(&rev);
504 while ((commit = get_revision(&rev)) != NULL) {
505 unsigned char sha1[20];
507 /* ignore merges */
508 if (commit->parents && commit->parents->next)
509 continue;
511 if (ignore_if_in_upstream &&
512 !get_patch_id(commit, &patch_id_opts, sha1) &&
513 lookup_object(sha1))
514 continue;
516 nr++;
517 list = xrealloc(list, nr * sizeof(list[0]));
518 list[nr - 1] = commit;
520 total = nr;
521 if (numbered)
522 rev.total = total + start_number - 1;
523 rev.add_signoff = add_signoff;
524 rev.ref_message_id = in_reply_to;
525 while (0 <= --nr) {
526 int shown;
527 commit = list[nr];
528 rev.nr = total - nr + (start_number - 1);
529 /* Make the second and subsequent mails replies to the first */
530 if (thread) {
531 if (nr == (total - 2)) {
532 strncpy(ref_message_id, message_id,
533 sizeof(ref_message_id));
534 ref_message_id[sizeof(ref_message_id)-1]='\0';
535 rev.ref_message_id = ref_message_id;
537 gen_message_id(message_id, sizeof(message_id),
538 sha1_to_hex(commit->object.sha1));
539 rev.message_id = message_id;
541 if (!use_stdout)
542 reopen_stdout(commit, rev.nr, keep_subject);
543 shown = log_tree_commit(&rev, commit);
544 free(commit->buffer);
545 commit->buffer = NULL;
547 /* We put one extra blank line between formatted
548 * patches and this flag is used by log-tree code
549 * to see if it needs to emit a LF before showing
550 * the log; when using one file per patch, we do
551 * not want the extra blank line.
553 if (!use_stdout)
554 rev.shown_one = 0;
555 if (shown) {
556 if (rev.mime_boundary)
557 printf("\n--%s%s--\n\n\n",
558 mime_boundary_leader,
559 rev.mime_boundary);
560 else
561 printf("-- \n%s\n\n", git_version_string);
563 if (!use_stdout)
564 fclose(stdout);
566 free(list);
567 return 0;
570 static int add_pending_commit(const char *arg, struct rev_info *revs, int flags)
572 unsigned char sha1[20];
573 if (get_sha1(arg, sha1) == 0) {
574 struct commit *commit = lookup_commit_reference(sha1);
575 if (commit) {
576 commit->object.flags |= flags;
577 add_pending_object(revs, &commit->object, arg);
578 return 0;
581 return -1;
584 static const char cherry_usage[] =
585 "git-cherry [-v] <upstream> [<head>] [<limit>]";
586 int cmd_cherry(int argc, const char **argv, const char *prefix)
588 struct rev_info revs;
589 struct diff_options patch_id_opts;
590 struct commit *commit;
591 struct commit_list *list = NULL;
592 const char *upstream;
593 const char *head = "HEAD";
594 const char *limit = NULL;
595 int verbose = 0;
597 if (argc > 1 && !strcmp(argv[1], "-v")) {
598 verbose = 1;
599 argc--;
600 argv++;
603 switch (argc) {
604 case 4:
605 limit = argv[3];
606 /* FALLTHROUGH */
607 case 3:
608 head = argv[2];
609 /* FALLTHROUGH */
610 case 2:
611 upstream = argv[1];
612 break;
613 default:
614 usage(cherry_usage);
617 init_revisions(&revs, prefix);
618 revs.diff = 1;
619 revs.combine_merges = 0;
620 revs.ignore_merges = 1;
621 revs.diffopt.recursive = 1;
623 if (add_pending_commit(head, &revs, 0))
624 die("Unknown commit %s", head);
625 if (add_pending_commit(upstream, &revs, UNINTERESTING))
626 die("Unknown commit %s", upstream);
628 /* Don't say anything if head and upstream are the same. */
629 if (revs.pending.nr == 2) {
630 struct object_array_entry *o = revs.pending.objects;
631 if (hashcmp(o[0].item->sha1, o[1].item->sha1) == 0)
632 return 0;
635 get_patch_ids(&revs, &patch_id_opts, prefix);
637 if (limit && add_pending_commit(limit, &revs, UNINTERESTING))
638 die("Unknown commit %s", limit);
640 /* reverse the list of commits */
641 prepare_revision_walk(&revs);
642 while ((commit = get_revision(&revs)) != NULL) {
643 /* ignore merges */
644 if (commit->parents && commit->parents->next)
645 continue;
647 commit_list_insert(commit, &list);
650 while (list) {
651 unsigned char sha1[20];
652 char sign = '+';
654 commit = list->item;
655 if (!get_patch_id(commit, &patch_id_opts, sha1) &&
656 lookup_object(sha1))
657 sign = '-';
659 if (verbose) {
660 static char buf[16384];
661 pretty_print_commit(CMIT_FMT_ONELINE, commit, ~0,
662 buf, sizeof(buf), 0, NULL, NULL, 0);
663 printf("%c %s %s\n", sign,
664 sha1_to_hex(commit->object.sha1), buf);
666 else {
667 printf("%c %s\n", sign,
668 sha1_to_hex(commit->object.sha1));
671 list = list->next;
674 return 0;