Clean up write_in_full() users
[git/git-svn.git] / builtin-log.c
bloba59b4acef1bc45f07f15e10c0501aa23de3eed7d
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;
201 static int git_format_config(const char *var, const char *value)
203 if (!strcmp(var, "format.headers")) {
204 int len = strlen(value);
205 extra_headers_size += len + 1;
206 extra_headers = xrealloc(extra_headers, extra_headers_size);
207 extra_headers[extra_headers_size - len - 1] = 0;
208 strcat(extra_headers, value);
209 return 0;
211 if (!strcmp(var, "diff.color") || !strcmp(var, "color.diff")) {
212 return 0;
214 return git_log_config(var, value);
218 static FILE *realstdout = NULL;
219 static const char *output_directory = NULL;
221 static void reopen_stdout(struct commit *commit, int nr, int keep_subject)
223 char filename[1024];
224 char *sol;
225 int len = 0;
227 if (output_directory) {
228 strlcpy(filename, output_directory, 1010);
229 len = strlen(filename);
230 if (filename[len - 1] != '/')
231 filename[len++] = '/';
234 sprintf(filename + len, "%04d", nr);
235 len = strlen(filename);
237 sol = strstr(commit->buffer, "\n\n");
238 if (sol) {
239 int j, space = 1;
241 sol += 2;
242 /* strip [PATCH] or [PATCH blabla] */
243 if (!keep_subject && !strncmp(sol, "[PATCH", 6)) {
244 char *eos = strchr(sol + 6, ']');
245 if (eos) {
246 while (isspace(*eos))
247 eos++;
248 sol = eos;
252 for (j = 0; len < 1024 - 6 && sol[j] && sol[j] != '\n'; j++) {
253 if (istitlechar(sol[j])) {
254 if (space) {
255 filename[len++] = '-';
256 space = 0;
258 filename[len++] = sol[j];
259 if (sol[j] == '.')
260 while (sol[j + 1] == '.')
261 j++;
262 } else
263 space = 1;
265 while (filename[len - 1] == '.' || filename[len - 1] == '-')
266 len--;
268 strcpy(filename + len, ".txt");
269 fprintf(realstdout, "%s\n", filename);
270 freopen(filename, "w", stdout);
273 static int get_patch_id(struct commit *commit, struct diff_options *options,
274 unsigned char *sha1)
276 if (commit->parents)
277 diff_tree_sha1(commit->parents->item->object.sha1,
278 commit->object.sha1, "", options);
279 else
280 diff_root_tree_sha1(commit->object.sha1, "", options);
281 diffcore_std(options);
282 return diff_flush_patch_id(options, sha1);
285 static void get_patch_ids(struct rev_info *rev, struct diff_options *options, const char *prefix)
287 struct rev_info check_rev;
288 struct commit *commit;
289 struct object *o1, *o2;
290 unsigned flags1, flags2;
291 unsigned char sha1[20];
293 if (rev->pending.nr != 2)
294 die("Need exactly one range.");
296 o1 = rev->pending.objects[0].item;
297 flags1 = o1->flags;
298 o2 = rev->pending.objects[1].item;
299 flags2 = o2->flags;
301 if ((flags1 & UNINTERESTING) == (flags2 & UNINTERESTING))
302 die("Not a range.");
304 diff_setup(options);
305 options->recursive = 1;
306 if (diff_setup_done(options) < 0)
307 die("diff_setup_done failed");
309 /* given a range a..b get all patch ids for b..a */
310 init_revisions(&check_rev, prefix);
311 o1->flags ^= UNINTERESTING;
312 o2->flags ^= UNINTERESTING;
313 add_pending_object(&check_rev, o1, "o1");
314 add_pending_object(&check_rev, o2, "o2");
315 prepare_revision_walk(&check_rev);
317 while ((commit = get_revision(&check_rev)) != NULL) {
318 /* ignore merges */
319 if (commit->parents && commit->parents->next)
320 continue;
322 if (!get_patch_id(commit, options, sha1))
323 created_object(sha1, xcalloc(1, sizeof(struct object)));
326 /* reset for next revision walk */
327 clear_commit_marks((struct commit *)o1,
328 SEEN | UNINTERESTING | SHOWN | ADDED);
329 clear_commit_marks((struct commit *)o2,
330 SEEN | UNINTERESTING | SHOWN | ADDED);
331 o1->flags = flags1;
332 o2->flags = flags2;
335 static void gen_message_id(char *dest, unsigned int length, char *base)
337 const char *committer = git_committer_info(1);
338 const char *email_start = strrchr(committer, '<');
339 const char *email_end = strrchr(committer, '>');
340 if(!email_start || !email_end || email_start > email_end - 1)
341 die("Could not extract email from committer identity.");
342 snprintf(dest, length, "%s.%lu.git.%.*s", base,
343 (unsigned long) time(NULL),
344 (int)(email_end - email_start - 1), email_start + 1);
347 int cmd_format_patch(int argc, const char **argv, const char *prefix)
349 struct commit *commit;
350 struct commit **list = NULL;
351 struct rev_info rev;
352 int nr = 0, total, i, j;
353 int use_stdout = 0;
354 int numbered = 0;
355 int start_number = -1;
356 int keep_subject = 0;
357 int ignore_if_in_upstream = 0;
358 int thread = 0;
359 const char *in_reply_to = NULL;
360 struct diff_options patch_id_opts;
361 char *add_signoff = NULL;
362 char message_id[1024];
363 char ref_message_id[1024];
365 setup_ident();
366 git_config(git_format_config);
367 init_revisions(&rev, prefix);
368 rev.commit_format = CMIT_FMT_EMAIL;
369 rev.verbose_header = 1;
370 rev.diff = 1;
371 rev.combine_merges = 0;
372 rev.ignore_merges = 1;
373 rev.diffopt.msg_sep = "";
374 rev.diffopt.recursive = 1;
376 rev.extra_headers = extra_headers;
379 * Parse the arguments before setup_revisions(), or something
380 * like "git fmt-patch -o a123 HEAD^.." may fail; a123 is
381 * possibly a valid SHA1.
383 for (i = 1, j = 1; i < argc; i++) {
384 if (!strcmp(argv[i], "--stdout"))
385 use_stdout = 1;
386 else if (!strcmp(argv[i], "-n") ||
387 !strcmp(argv[i], "--numbered"))
388 numbered = 1;
389 else if (!strncmp(argv[i], "--start-number=", 15))
390 start_number = strtol(argv[i] + 15, NULL, 10);
391 else if (!strcmp(argv[i], "--start-number")) {
392 i++;
393 if (i == argc)
394 die("Need a number for --start-number");
395 start_number = strtol(argv[i], NULL, 10);
397 else if (!strcmp(argv[i], "-k") ||
398 !strcmp(argv[i], "--keep-subject")) {
399 keep_subject = 1;
400 rev.total = -1;
402 else if (!strcmp(argv[i], "--output-directory") ||
403 !strcmp(argv[i], "-o")) {
404 i++;
405 if (argc <= i)
406 die("Which directory?");
407 if (output_directory)
408 die("Two output directories?");
409 output_directory = argv[i];
411 else if (!strcmp(argv[i], "--signoff") ||
412 !strcmp(argv[i], "-s")) {
413 const char *committer;
414 const char *endpos;
415 committer = git_committer_info(1);
416 endpos = strchr(committer, '>');
417 if (!endpos)
418 die("bogos committer info %s\n", committer);
419 add_signoff = xmalloc(endpos - committer + 2);
420 memcpy(add_signoff, committer, endpos - committer + 1);
421 add_signoff[endpos - committer + 1] = 0;
423 else if (!strcmp(argv[i], "--attach"))
424 rev.mime_boundary = git_version_string;
425 else if (!strncmp(argv[i], "--attach=", 9))
426 rev.mime_boundary = argv[i] + 9;
427 else if (!strcmp(argv[i], "--ignore-if-in-upstream"))
428 ignore_if_in_upstream = 1;
429 else if (!strcmp(argv[i], "--thread"))
430 thread = 1;
431 else if (!strncmp(argv[i], "--in-reply-to=", 14))
432 in_reply_to = argv[i] + 14;
433 else if (!strcmp(argv[i], "--in-reply-to")) {
434 i++;
435 if (i == argc)
436 die("Need a Message-Id for --in-reply-to");
437 in_reply_to = argv[i];
439 else
440 argv[j++] = argv[i];
442 argc = j;
444 if (start_number < 0)
445 start_number = 1;
446 if (numbered && keep_subject)
447 die ("-n and -k are mutually exclusive.");
449 argc = setup_revisions(argc, argv, &rev, "HEAD");
450 if (argc > 1)
451 die ("unrecognized argument: %s", argv[1]);
453 if (!rev.diffopt.output_format)
454 rev.diffopt.output_format = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_PATCH;
456 if (!output_directory)
457 output_directory = prefix;
459 if (output_directory) {
460 if (use_stdout)
461 die("standard output, or directory, which one?");
462 if (mkdir(output_directory, 0777) < 0 && errno != EEXIST)
463 die("Could not create directory %s",
464 output_directory);
467 if (rev.pending.nr == 1) {
468 rev.pending.objects[0].item->flags |= UNINTERESTING;
469 add_head(&rev);
472 if (ignore_if_in_upstream)
473 get_patch_ids(&rev, &patch_id_opts, prefix);
475 if (!use_stdout)
476 realstdout = fdopen(dup(1), "w");
478 prepare_revision_walk(&rev);
479 while ((commit = get_revision(&rev)) != NULL) {
480 unsigned char sha1[20];
482 /* ignore merges */
483 if (commit->parents && commit->parents->next)
484 continue;
486 if (ignore_if_in_upstream &&
487 !get_patch_id(commit, &patch_id_opts, sha1) &&
488 lookup_object(sha1))
489 continue;
491 nr++;
492 list = xrealloc(list, nr * sizeof(list[0]));
493 list[nr - 1] = commit;
495 total = nr;
496 if (numbered)
497 rev.total = total + start_number - 1;
498 rev.add_signoff = add_signoff;
499 rev.ref_message_id = in_reply_to;
500 while (0 <= --nr) {
501 int shown;
502 commit = list[nr];
503 rev.nr = total - nr + (start_number - 1);
504 /* Make the second and subsequent mails replies to the first */
505 if (thread) {
506 if (nr == (total - 2)) {
507 strncpy(ref_message_id, message_id,
508 sizeof(ref_message_id));
509 ref_message_id[sizeof(ref_message_id)-1]='\0';
510 rev.ref_message_id = ref_message_id;
512 gen_message_id(message_id, sizeof(message_id),
513 sha1_to_hex(commit->object.sha1));
514 rev.message_id = message_id;
516 if (!use_stdout)
517 reopen_stdout(commit, rev.nr, keep_subject);
518 shown = log_tree_commit(&rev, commit);
519 free(commit->buffer);
520 commit->buffer = NULL;
522 /* We put one extra blank line between formatted
523 * patches and this flag is used by log-tree code
524 * to see if it needs to emit a LF before showing
525 * the log; when using one file per patch, we do
526 * not want the extra blank line.
528 if (!use_stdout)
529 rev.shown_one = 0;
530 if (shown) {
531 if (rev.mime_boundary)
532 printf("\n--%s%s--\n\n\n",
533 mime_boundary_leader,
534 rev.mime_boundary);
535 else
536 printf("-- \n%s\n\n", git_version_string);
538 if (!use_stdout)
539 fclose(stdout);
541 free(list);
542 return 0;
545 static int add_pending_commit(const char *arg, struct rev_info *revs, int flags)
547 unsigned char sha1[20];
548 if (get_sha1(arg, sha1) == 0) {
549 struct commit *commit = lookup_commit_reference(sha1);
550 if (commit) {
551 commit->object.flags |= flags;
552 add_pending_object(revs, &commit->object, arg);
553 return 0;
556 return -1;
559 static const char cherry_usage[] =
560 "git-cherry [-v] <upstream> [<head>] [<limit>]";
561 int cmd_cherry(int argc, const char **argv, const char *prefix)
563 struct rev_info revs;
564 struct diff_options patch_id_opts;
565 struct commit *commit;
566 struct commit_list *list = NULL;
567 const char *upstream;
568 const char *head = "HEAD";
569 const char *limit = NULL;
570 int verbose = 0;
572 if (argc > 1 && !strcmp(argv[1], "-v")) {
573 verbose = 1;
574 argc--;
575 argv++;
578 switch (argc) {
579 case 4:
580 limit = argv[3];
581 /* FALLTHROUGH */
582 case 3:
583 head = argv[2];
584 /* FALLTHROUGH */
585 case 2:
586 upstream = argv[1];
587 break;
588 default:
589 usage(cherry_usage);
592 init_revisions(&revs, prefix);
593 revs.diff = 1;
594 revs.combine_merges = 0;
595 revs.ignore_merges = 1;
596 revs.diffopt.recursive = 1;
598 if (add_pending_commit(head, &revs, 0))
599 die("Unknown commit %s", head);
600 if (add_pending_commit(upstream, &revs, UNINTERESTING))
601 die("Unknown commit %s", upstream);
603 /* Don't say anything if head and upstream are the same. */
604 if (revs.pending.nr == 2) {
605 struct object_array_entry *o = revs.pending.objects;
606 if (hashcmp(o[0].item->sha1, o[1].item->sha1) == 0)
607 return 0;
610 get_patch_ids(&revs, &patch_id_opts, prefix);
612 if (limit && add_pending_commit(limit, &revs, UNINTERESTING))
613 die("Unknown commit %s", limit);
615 /* reverse the list of commits */
616 prepare_revision_walk(&revs);
617 while ((commit = get_revision(&revs)) != NULL) {
618 /* ignore merges */
619 if (commit->parents && commit->parents->next)
620 continue;
622 commit_list_insert(commit, &list);
625 while (list) {
626 unsigned char sha1[20];
627 char sign = '+';
629 commit = list->item;
630 if (!get_patch_id(commit, &patch_id_opts, sha1) &&
631 lookup_object(sha1))
632 sign = '-';
634 if (verbose) {
635 static char buf[16384];
636 pretty_print_commit(CMIT_FMT_ONELINE, commit, ~0,
637 buf, sizeof(buf), 0, NULL, NULL, 0);
638 printf("%c %s %s\n", sign,
639 sha1_to_hex(commit->object.sha1), buf);
641 else {
642 printf("%c %s\n", sign,
643 sha1_to_hex(commit->object.sha1));
646 list = list->next;
649 return 0;