everyday: replace a few 'prune' and 'repack' with 'gc'
[git/dscho.git] / builtin-log.c
blob8df3c1394a0b70548708137bfe20126fbea01709
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 rev->abbrev = DEFAULT_ABBREV;
24 rev->commit_format = CMIT_FMT_DEFAULT;
25 rev->verbose_header = 1;
26 rev->show_root_diff = default_show_root;
27 argc = setup_revisions(argc, argv, rev, "HEAD");
28 if (rev->diffopt.pickaxe || rev->diffopt.filter)
29 rev->always_show_header = 0;
30 if (argc > 1)
31 die("unrecognized argument: %s", argv[1]);
34 static int cmd_log_walk(struct rev_info *rev)
36 struct commit *commit;
38 prepare_revision_walk(rev);
39 while ((commit = get_revision(rev)) != NULL) {
40 log_tree_commit(rev, commit);
41 free(commit->buffer);
42 commit->buffer = NULL;
43 free_commit_list(commit->parents);
44 commit->parents = NULL;
46 return 0;
49 static int git_log_config(const char *var, const char *value)
51 if (!strcmp(var, "log.showroot")) {
52 default_show_root = git_config_bool(var, value);
53 return 0;
55 return git_diff_ui_config(var, value);
58 int cmd_whatchanged(int argc, const char **argv, const char *prefix)
60 struct rev_info rev;
62 git_config(git_log_config);
63 init_revisions(&rev, prefix);
64 rev.diff = 1;
65 rev.diffopt.recursive = 1;
66 rev.simplify_history = 0;
67 cmd_log_init(argc, argv, prefix, &rev);
68 if (!rev.diffopt.output_format)
69 rev.diffopt.output_format = DIFF_FORMAT_RAW;
70 return cmd_log_walk(&rev);
73 static int show_object(const unsigned char *sha1, int suppress_header)
75 unsigned long size;
76 char type[20];
77 char *buf = read_sha1_file(sha1, type, &size);
78 int offset = 0;
80 if (!buf)
81 return error("Could not read object %s", sha1_to_hex(sha1));
83 if (suppress_header)
84 while (offset < size && buf[offset++] != '\n') {
85 int new_offset = offset;
86 while (new_offset < size && buf[new_offset++] != '\n')
87 ; /* do nothing */
88 offset = new_offset;
91 if (offset < size)
92 fwrite(buf + offset, size - offset, 1, stdout);
93 free(buf);
94 return 0;
97 static int show_tree_object(const unsigned char *sha1,
98 const char *base, int baselen,
99 const char *pathname, unsigned mode, int stage)
101 printf("%s%s\n", pathname, S_ISDIR(mode) ? "/" : "");
102 return 0;
105 int cmd_show(int argc, const char **argv, const char *prefix)
107 struct rev_info rev;
108 struct object_array_entry *objects;
109 int i, count, ret = 0;
111 git_config(git_log_config);
112 init_revisions(&rev, prefix);
113 rev.diff = 1;
114 rev.diffopt.recursive = 1;
115 rev.combine_merges = 1;
116 rev.dense_combined_merges = 1;
117 rev.always_show_header = 1;
118 rev.ignore_merges = 0;
119 rev.no_walk = 1;
120 cmd_log_init(argc, argv, prefix, &rev);
122 count = rev.pending.nr;
123 objects = rev.pending.objects;
124 for (i = 0; i < count && !ret; i++) {
125 struct object *o = objects[i].item;
126 const char *name = objects[i].name;
127 switch (o->type) {
128 case OBJ_BLOB:
129 ret = show_object(o->sha1, 0);
130 break;
131 case OBJ_TAG: {
132 struct tag *t = (struct tag *)o;
134 printf("%stag %s%s\n\n",
135 diff_get_color(rev.diffopt.color_diff,
136 DIFF_COMMIT),
137 t->tag,
138 diff_get_color(rev.diffopt.color_diff,
139 DIFF_RESET));
140 ret = show_object(o->sha1, 1);
141 objects[i].item = (struct object *)t->tagged;
142 i--;
143 break;
145 case OBJ_TREE:
146 printf("%stree %s%s\n\n",
147 diff_get_color(rev.diffopt.color_diff,
148 DIFF_COMMIT),
149 name,
150 diff_get_color(rev.diffopt.color_diff,
151 DIFF_RESET));
152 read_tree_recursive((struct tree *)o, "", 0, 0, NULL,
153 show_tree_object);
154 break;
155 case OBJ_COMMIT:
156 rev.pending.nr = rev.pending.alloc = 0;
157 rev.pending.objects = NULL;
158 add_object_array(o, name, &rev.pending);
159 ret = cmd_log_walk(&rev);
160 break;
161 default:
162 ret = error("Unknown type: %d", o->type);
165 free(objects);
166 return ret;
169 int cmd_log(int argc, const char **argv, const char *prefix)
171 struct rev_info rev;
173 git_config(git_log_config);
174 init_revisions(&rev, prefix);
175 rev.always_show_header = 1;
176 cmd_log_init(argc, argv, prefix, &rev);
177 return cmd_log_walk(&rev);
180 static int istitlechar(char c)
182 return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
183 (c >= '0' && c <= '9') || c == '.' || c == '_';
186 static char *extra_headers = NULL;
187 static int extra_headers_size = 0;
189 static int git_format_config(const char *var, const char *value)
191 if (!strcmp(var, "format.headers")) {
192 int len = strlen(value);
193 extra_headers_size += len + 1;
194 extra_headers = xrealloc(extra_headers, extra_headers_size);
195 extra_headers[extra_headers_size - len - 1] = 0;
196 strcat(extra_headers, value);
197 return 0;
199 if (!strcmp(var, "diff.color") || !strcmp(var, "color.diff")) {
200 return 0;
202 return git_log_config(var, value);
206 static FILE *realstdout = NULL;
207 static const char *output_directory = NULL;
209 static void reopen_stdout(struct commit *commit, int nr, int keep_subject)
211 char filename[1024];
212 char *sol;
213 int len = 0;
215 if (output_directory) {
216 strlcpy(filename, output_directory, 1010);
217 len = strlen(filename);
218 if (filename[len - 1] != '/')
219 filename[len++] = '/';
222 sprintf(filename + len, "%04d", nr);
223 len = strlen(filename);
225 sol = strstr(commit->buffer, "\n\n");
226 if (sol) {
227 int j, space = 1;
229 sol += 2;
230 /* strip [PATCH] or [PATCH blabla] */
231 if (!keep_subject && !strncmp(sol, "[PATCH", 6)) {
232 char *eos = strchr(sol + 6, ']');
233 if (eos) {
234 while (isspace(*eos))
235 eos++;
236 sol = eos;
240 for (j = 0; len < 1024 - 6 && sol[j] && sol[j] != '\n'; j++) {
241 if (istitlechar(sol[j])) {
242 if (space) {
243 filename[len++] = '-';
244 space = 0;
246 filename[len++] = sol[j];
247 if (sol[j] == '.')
248 while (sol[j + 1] == '.')
249 j++;
250 } else
251 space = 1;
253 while (filename[len - 1] == '.' || filename[len - 1] == '-')
254 len--;
256 strcpy(filename + len, ".txt");
257 fprintf(realstdout, "%s\n", filename);
258 freopen(filename, "w", stdout);
261 static int get_patch_id(struct commit *commit, struct diff_options *options,
262 unsigned char *sha1)
264 if (commit->parents)
265 diff_tree_sha1(commit->parents->item->object.sha1,
266 commit->object.sha1, "", options);
267 else
268 diff_root_tree_sha1(commit->object.sha1, "", options);
269 diffcore_std(options);
270 return diff_flush_patch_id(options, sha1);
273 static void get_patch_ids(struct rev_info *rev, struct diff_options *options, const char *prefix)
275 struct rev_info check_rev;
276 struct commit *commit;
277 struct object *o1, *o2;
278 unsigned flags1, flags2;
279 unsigned char sha1[20];
281 if (rev->pending.nr != 2)
282 die("Need exactly one range.");
284 o1 = rev->pending.objects[0].item;
285 flags1 = o1->flags;
286 o2 = rev->pending.objects[1].item;
287 flags2 = o2->flags;
289 if ((flags1 & UNINTERESTING) == (flags2 & UNINTERESTING))
290 die("Not a range.");
292 diff_setup(options);
293 options->recursive = 1;
294 if (diff_setup_done(options) < 0)
295 die("diff_setup_done failed");
297 /* given a range a..b get all patch ids for b..a */
298 init_revisions(&check_rev, prefix);
299 o1->flags ^= UNINTERESTING;
300 o2->flags ^= UNINTERESTING;
301 add_pending_object(&check_rev, o1, "o1");
302 add_pending_object(&check_rev, o2, "o2");
303 prepare_revision_walk(&check_rev);
305 while ((commit = get_revision(&check_rev)) != NULL) {
306 /* ignore merges */
307 if (commit->parents && commit->parents->next)
308 continue;
310 if (!get_patch_id(commit, options, sha1))
311 created_object(sha1, xcalloc(1, sizeof(struct object)));
314 /* reset for next revision walk */
315 clear_commit_marks((struct commit *)o1,
316 SEEN | UNINTERESTING | SHOWN | ADDED);
317 clear_commit_marks((struct commit *)o2,
318 SEEN | UNINTERESTING | SHOWN | ADDED);
319 o1->flags = flags1;
320 o2->flags = flags2;
323 static void gen_message_id(char *dest, unsigned int length, char *base)
325 const char *committer = git_committer_info(1);
326 const char *email_start = strrchr(committer, '<');
327 const char *email_end = strrchr(committer, '>');
328 if(!email_start || !email_end || email_start > email_end - 1)
329 die("Could not extract email from committer identity.");
330 snprintf(dest, length, "%s.%lu.git.%.*s", base,
331 (unsigned long) time(NULL),
332 (int)(email_end - email_start - 1), email_start + 1);
335 int cmd_format_patch(int argc, const char **argv, const char *prefix)
337 struct commit *commit;
338 struct commit **list = NULL;
339 struct rev_info rev;
340 int nr = 0, total, i, j;
341 int use_stdout = 0;
342 int numbered = 0;
343 int start_number = -1;
344 int keep_subject = 0;
345 int ignore_if_in_upstream = 0;
346 int thread = 0;
347 const char *in_reply_to = NULL;
348 struct diff_options patch_id_opts;
349 char *add_signoff = NULL;
350 char message_id[1024];
351 char ref_message_id[1024];
353 setup_ident();
354 git_config(git_format_config);
355 init_revisions(&rev, prefix);
356 rev.commit_format = CMIT_FMT_EMAIL;
357 rev.verbose_header = 1;
358 rev.diff = 1;
359 rev.combine_merges = 0;
360 rev.ignore_merges = 1;
361 rev.diffopt.msg_sep = "";
362 rev.diffopt.recursive = 1;
364 rev.extra_headers = extra_headers;
367 * Parse the arguments before setup_revisions(), or something
368 * like "git fmt-patch -o a123 HEAD^.." may fail; a123 is
369 * possibly a valid SHA1.
371 for (i = 1, j = 1; i < argc; i++) {
372 if (!strcmp(argv[i], "--stdout"))
373 use_stdout = 1;
374 else if (!strcmp(argv[i], "-n") ||
375 !strcmp(argv[i], "--numbered"))
376 numbered = 1;
377 else if (!strncmp(argv[i], "--start-number=", 15))
378 start_number = strtol(argv[i] + 15, NULL, 10);
379 else if (!strcmp(argv[i], "--start-number")) {
380 i++;
381 if (i == argc)
382 die("Need a number for --start-number");
383 start_number = strtol(argv[i], NULL, 10);
385 else if (!strcmp(argv[i], "-k") ||
386 !strcmp(argv[i], "--keep-subject")) {
387 keep_subject = 1;
388 rev.total = -1;
390 else if (!strcmp(argv[i], "--output-directory") ||
391 !strcmp(argv[i], "-o")) {
392 i++;
393 if (argc <= i)
394 die("Which directory?");
395 if (output_directory)
396 die("Two output directories?");
397 output_directory = argv[i];
399 else if (!strcmp(argv[i], "--signoff") ||
400 !strcmp(argv[i], "-s")) {
401 const char *committer;
402 const char *endpos;
403 committer = git_committer_info(1);
404 endpos = strchr(committer, '>');
405 if (!endpos)
406 die("bogos committer info %s\n", committer);
407 add_signoff = xmalloc(endpos - committer + 2);
408 memcpy(add_signoff, committer, endpos - committer + 1);
409 add_signoff[endpos - committer + 1] = 0;
411 else if (!strcmp(argv[i], "--attach"))
412 rev.mime_boundary = git_version_string;
413 else if (!strncmp(argv[i], "--attach=", 9))
414 rev.mime_boundary = argv[i] + 9;
415 else if (!strcmp(argv[i], "--ignore-if-in-upstream"))
416 ignore_if_in_upstream = 1;
417 else if (!strcmp(argv[i], "--thread"))
418 thread = 1;
419 else if (!strncmp(argv[i], "--in-reply-to=", 14))
420 in_reply_to = argv[i] + 14;
421 else if (!strcmp(argv[i], "--in-reply-to")) {
422 i++;
423 if (i == argc)
424 die("Need a Message-Id for --in-reply-to");
425 in_reply_to = argv[i];
427 else
428 argv[j++] = argv[i];
430 argc = j;
432 if (start_number < 0)
433 start_number = 1;
434 if (numbered && keep_subject)
435 die ("-n and -k are mutually exclusive.");
437 argc = setup_revisions(argc, argv, &rev, "HEAD");
438 if (argc > 1)
439 die ("unrecognized argument: %s", argv[1]);
441 if (!rev.diffopt.output_format)
442 rev.diffopt.output_format = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_PATCH;
444 if (!output_directory)
445 output_directory = prefix;
447 if (output_directory) {
448 if (use_stdout)
449 die("standard output, or directory, which one?");
450 if (mkdir(output_directory, 0777) < 0 && errno != EEXIST)
451 die("Could not create directory %s",
452 output_directory);
455 if (rev.pending.nr == 1) {
456 rev.pending.objects[0].item->flags |= UNINTERESTING;
457 add_head(&rev);
460 if (ignore_if_in_upstream)
461 get_patch_ids(&rev, &patch_id_opts, prefix);
463 if (!use_stdout)
464 realstdout = fdopen(dup(1), "w");
466 prepare_revision_walk(&rev);
467 while ((commit = get_revision(&rev)) != NULL) {
468 unsigned char sha1[20];
470 /* ignore merges */
471 if (commit->parents && commit->parents->next)
472 continue;
474 if (ignore_if_in_upstream &&
475 !get_patch_id(commit, &patch_id_opts, sha1) &&
476 lookup_object(sha1))
477 continue;
479 nr++;
480 list = xrealloc(list, nr * sizeof(list[0]));
481 list[nr - 1] = commit;
483 total = nr;
484 if (numbered)
485 rev.total = total + start_number - 1;
486 rev.add_signoff = add_signoff;
487 rev.ref_message_id = in_reply_to;
488 while (0 <= --nr) {
489 int shown;
490 commit = list[nr];
491 rev.nr = total - nr + (start_number - 1);
492 /* Make the second and subsequent mails replies to the first */
493 if (thread) {
494 if (nr == (total - 2)) {
495 strncpy(ref_message_id, message_id,
496 sizeof(ref_message_id));
497 ref_message_id[sizeof(ref_message_id)-1]='\0';
498 rev.ref_message_id = ref_message_id;
500 gen_message_id(message_id, sizeof(message_id),
501 sha1_to_hex(commit->object.sha1));
502 rev.message_id = message_id;
504 if (!use_stdout)
505 reopen_stdout(commit, rev.nr, keep_subject);
506 shown = log_tree_commit(&rev, commit);
507 free(commit->buffer);
508 commit->buffer = NULL;
510 /* We put one extra blank line between formatted
511 * patches and this flag is used by log-tree code
512 * to see if it needs to emit a LF before showing
513 * the log; when using one file per patch, we do
514 * not want the extra blank line.
516 if (!use_stdout)
517 rev.shown_one = 0;
518 if (shown) {
519 if (rev.mime_boundary)
520 printf("\n--%s%s--\n\n\n",
521 mime_boundary_leader,
522 rev.mime_boundary);
523 else
524 printf("-- \n%s\n\n", git_version_string);
526 if (!use_stdout)
527 fclose(stdout);
529 free(list);
530 return 0;
533 static int add_pending_commit(const char *arg, struct rev_info *revs, int flags)
535 unsigned char sha1[20];
536 if (get_sha1(arg, sha1) == 0) {
537 struct commit *commit = lookup_commit_reference(sha1);
538 if (commit) {
539 commit->object.flags |= flags;
540 add_pending_object(revs, &commit->object, arg);
541 return 0;
544 return -1;
547 static const char cherry_usage[] =
548 "git-cherry [-v] <upstream> [<head>] [<limit>]";
549 int cmd_cherry(int argc, const char **argv, const char *prefix)
551 struct rev_info revs;
552 struct diff_options patch_id_opts;
553 struct commit *commit;
554 struct commit_list *list = NULL;
555 const char *upstream;
556 const char *head = "HEAD";
557 const char *limit = NULL;
558 int verbose = 0;
560 if (argc > 1 && !strcmp(argv[1], "-v")) {
561 verbose = 1;
562 argc--;
563 argv++;
566 switch (argc) {
567 case 4:
568 limit = argv[3];
569 /* FALLTHROUGH */
570 case 3:
571 head = argv[2];
572 /* FALLTHROUGH */
573 case 2:
574 upstream = argv[1];
575 break;
576 default:
577 usage(cherry_usage);
580 init_revisions(&revs, prefix);
581 revs.diff = 1;
582 revs.combine_merges = 0;
583 revs.ignore_merges = 1;
584 revs.diffopt.recursive = 1;
586 if (add_pending_commit(head, &revs, 0))
587 die("Unknown commit %s", head);
588 if (add_pending_commit(upstream, &revs, UNINTERESTING))
589 die("Unknown commit %s", upstream);
591 /* Don't say anything if head and upstream are the same. */
592 if (revs.pending.nr == 2) {
593 struct object_array_entry *o = revs.pending.objects;
594 if (hashcmp(o[0].item->sha1, o[1].item->sha1) == 0)
595 return 0;
598 get_patch_ids(&revs, &patch_id_opts, prefix);
600 if (limit && add_pending_commit(limit, &revs, UNINTERESTING))
601 die("Unknown commit %s", limit);
603 /* reverse the list of commits */
604 prepare_revision_walk(&revs);
605 while ((commit = get_revision(&revs)) != NULL) {
606 /* ignore merges */
607 if (commit->parents && commit->parents->next)
608 continue;
610 commit_list_insert(commit, &list);
613 while (list) {
614 unsigned char sha1[20];
615 char sign = '+';
617 commit = list->item;
618 if (!get_patch_id(commit, &patch_id_opts, sha1) &&
619 lookup_object(sha1))
620 sign = '-';
622 if (verbose) {
623 static char buf[16384];
624 pretty_print_commit(CMIT_FMT_ONELINE, commit, ~0,
625 buf, sizeof(buf), 0, NULL, NULL, 0);
626 printf("%c %s %s\n", sign,
627 sha1_to_hex(commit->object.sha1), buf);
629 else {
630 printf("%c %s\n", sign,
631 sha1_to_hex(commit->object.sha1));
634 list = list->next;
637 return 0;