Document pack .idx file format upgrade strategy.
[git/gitweb.git] / builtin-log.c
blob1cd9d3f76834b40eb217cbb36784b85d5d076e3a
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 = ".txt";
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_PATCH;
473 if (!output_directory)
474 output_directory = prefix;
476 if (output_directory) {
477 if (use_stdout)
478 die("standard output, or directory, which one?");
479 if (mkdir(output_directory, 0777) < 0 && errno != EEXIST)
480 die("Could not create directory %s",
481 output_directory);
484 if (rev.pending.nr == 1) {
485 rev.pending.objects[0].item->flags |= UNINTERESTING;
486 add_head(&rev);
489 if (ignore_if_in_upstream)
490 get_patch_ids(&rev, &patch_id_opts, prefix);
492 if (!use_stdout)
493 realstdout = fdopen(dup(1), "w");
495 prepare_revision_walk(&rev);
496 while ((commit = get_revision(&rev)) != NULL) {
497 unsigned char sha1[20];
499 /* ignore merges */
500 if (commit->parents && commit->parents->next)
501 continue;
503 if (ignore_if_in_upstream &&
504 !get_patch_id(commit, &patch_id_opts, sha1) &&
505 lookup_object(sha1))
506 continue;
508 nr++;
509 list = xrealloc(list, nr * sizeof(list[0]));
510 list[nr - 1] = commit;
512 total = nr;
513 if (numbered)
514 rev.total = total + start_number - 1;
515 rev.add_signoff = add_signoff;
516 rev.ref_message_id = in_reply_to;
517 while (0 <= --nr) {
518 int shown;
519 commit = list[nr];
520 rev.nr = total - nr + (start_number - 1);
521 /* Make the second and subsequent mails replies to the first */
522 if (thread) {
523 if (nr == (total - 2)) {
524 strncpy(ref_message_id, message_id,
525 sizeof(ref_message_id));
526 ref_message_id[sizeof(ref_message_id)-1]='\0';
527 rev.ref_message_id = ref_message_id;
529 gen_message_id(message_id, sizeof(message_id),
530 sha1_to_hex(commit->object.sha1));
531 rev.message_id = message_id;
533 if (!use_stdout)
534 reopen_stdout(commit, rev.nr, keep_subject);
535 shown = log_tree_commit(&rev, commit);
536 free(commit->buffer);
537 commit->buffer = NULL;
539 /* We put one extra blank line between formatted
540 * patches and this flag is used by log-tree code
541 * to see if it needs to emit a LF before showing
542 * the log; when using one file per patch, we do
543 * not want the extra blank line.
545 if (!use_stdout)
546 rev.shown_one = 0;
547 if (shown) {
548 if (rev.mime_boundary)
549 printf("\n--%s%s--\n\n\n",
550 mime_boundary_leader,
551 rev.mime_boundary);
552 else
553 printf("-- \n%s\n\n", git_version_string);
555 if (!use_stdout)
556 fclose(stdout);
558 free(list);
559 return 0;
562 static int add_pending_commit(const char *arg, struct rev_info *revs, int flags)
564 unsigned char sha1[20];
565 if (get_sha1(arg, sha1) == 0) {
566 struct commit *commit = lookup_commit_reference(sha1);
567 if (commit) {
568 commit->object.flags |= flags;
569 add_pending_object(revs, &commit->object, arg);
570 return 0;
573 return -1;
576 static const char cherry_usage[] =
577 "git-cherry [-v] <upstream> [<head>] [<limit>]";
578 int cmd_cherry(int argc, const char **argv, const char *prefix)
580 struct rev_info revs;
581 struct diff_options patch_id_opts;
582 struct commit *commit;
583 struct commit_list *list = NULL;
584 const char *upstream;
585 const char *head = "HEAD";
586 const char *limit = NULL;
587 int verbose = 0;
589 if (argc > 1 && !strcmp(argv[1], "-v")) {
590 verbose = 1;
591 argc--;
592 argv++;
595 switch (argc) {
596 case 4:
597 limit = argv[3];
598 /* FALLTHROUGH */
599 case 3:
600 head = argv[2];
601 /* FALLTHROUGH */
602 case 2:
603 upstream = argv[1];
604 break;
605 default:
606 usage(cherry_usage);
609 init_revisions(&revs, prefix);
610 revs.diff = 1;
611 revs.combine_merges = 0;
612 revs.ignore_merges = 1;
613 revs.diffopt.recursive = 1;
615 if (add_pending_commit(head, &revs, 0))
616 die("Unknown commit %s", head);
617 if (add_pending_commit(upstream, &revs, UNINTERESTING))
618 die("Unknown commit %s", upstream);
620 /* Don't say anything if head and upstream are the same. */
621 if (revs.pending.nr == 2) {
622 struct object_array_entry *o = revs.pending.objects;
623 if (hashcmp(o[0].item->sha1, o[1].item->sha1) == 0)
624 return 0;
627 get_patch_ids(&revs, &patch_id_opts, prefix);
629 if (limit && add_pending_commit(limit, &revs, UNINTERESTING))
630 die("Unknown commit %s", limit);
632 /* reverse the list of commits */
633 prepare_revision_walk(&revs);
634 while ((commit = get_revision(&revs)) != NULL) {
635 /* ignore merges */
636 if (commit->parents && commit->parents->next)
637 continue;
639 commit_list_insert(commit, &list);
642 while (list) {
643 unsigned char sha1[20];
644 char sign = '+';
646 commit = list->item;
647 if (!get_patch_id(commit, &patch_id_opts, sha1) &&
648 lookup_object(sha1))
649 sign = '-';
651 if (verbose) {
652 static char buf[16384];
653 pretty_print_commit(CMIT_FMT_ONELINE, commit, ~0,
654 buf, sizeof(buf), 0, NULL, NULL, 0);
655 printf("%c %s %s\n", sign,
656 sha1_to_hex(commit->object.sha1), buf);
658 else {
659 printf("%c %s\n", sign,
660 sha1_to_hex(commit->object.sha1));
663 list = list->next;
666 return 0;