Teach log family --encoding
[git/dkf.git] / builtin-log.c
blobb7e47cb5fe980374fa0d328830a524c6f6d65b66
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 (MAX_ENCODING_LENGTH <= strlen(arg))
37 die(" Value of output encoding '%s' too long",
38 arg);
39 if (strcmp(arg, "none"))
40 strcpy(git_commit_encoding, arg);
41 else
42 git_commit_encoding[0] = 0;
44 else
45 die("unrecognized argument: %s", arg);
49 static int cmd_log_walk(struct rev_info *rev)
51 struct commit *commit;
53 prepare_revision_walk(rev);
54 while ((commit = get_revision(rev)) != NULL) {
55 log_tree_commit(rev, commit);
56 free(commit->buffer);
57 commit->buffer = NULL;
58 free_commit_list(commit->parents);
59 commit->parents = NULL;
61 return 0;
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);
68 return 0;
70 return git_diff_ui_config(var, value);
73 int cmd_whatchanged(int argc, const char **argv, const char *prefix)
75 struct rev_info rev;
77 git_config(git_log_config);
78 init_revisions(&rev, prefix);
79 rev.diff = 1;
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)
90 unsigned long size;
91 char type[20];
92 char *buf = read_sha1_file(sha1, type, &size);
93 int offset = 0;
95 if (!buf)
96 return error("Could not read object %s", sha1_to_hex(sha1));
98 if (suppress_header)
99 while (offset < size && buf[offset++] != '\n') {
100 int new_offset = offset;
101 while (new_offset < size && buf[new_offset++] != '\n')
102 ; /* do nothing */
103 offset = new_offset;
106 if (offset < size)
107 fwrite(buf + offset, size - offset, 1, stdout);
108 free(buf);
109 return 0;
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) ? "/" : "");
117 return 0;
120 int cmd_show(int argc, const char **argv, const char *prefix)
122 struct rev_info rev;
123 struct object_array_entry *objects;
124 int i, count, ret = 0;
126 git_config(git_log_config);
127 init_revisions(&rev, prefix);
128 rev.diff = 1;
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;
134 rev.no_walk = 1;
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;
142 switch (o->type) {
143 case OBJ_BLOB:
144 ret = show_object(o->sha1, 0);
145 break;
146 case OBJ_TAG: {
147 struct tag *t = (struct tag *)o;
149 printf("%stag %s%s\n\n",
150 diff_get_color(rev.diffopt.color_diff,
151 DIFF_COMMIT),
152 t->tag,
153 diff_get_color(rev.diffopt.color_diff,
154 DIFF_RESET));
155 ret = show_object(o->sha1, 1);
156 objects[i].item = (struct object *)t->tagged;
157 i--;
158 break;
160 case OBJ_TREE:
161 printf("%stree %s%s\n\n",
162 diff_get_color(rev.diffopt.color_diff,
163 DIFF_COMMIT),
164 name,
165 diff_get_color(rev.diffopt.color_diff,
166 DIFF_RESET));
167 read_tree_recursive((struct tree *)o, "", 0, 0, NULL,
168 show_tree_object);
169 break;
170 case OBJ_COMMIT:
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);
175 break;
176 default:
177 ret = error("Unknown type: %d", o->type);
180 free(objects);
181 return ret;
184 int cmd_log(int argc, const char **argv, const char *prefix)
186 struct rev_info rev;
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;
204 static int git_format_config(const char *var, const char *value)
206 if (!strcmp(var, "format.headers")) {
207 int len = strlen(value);
208 extra_headers_size += len + 1;
209 extra_headers = xrealloc(extra_headers, extra_headers_size);
210 extra_headers[extra_headers_size - len - 1] = 0;
211 strcat(extra_headers, value);
212 return 0;
214 if (!strcmp(var, "diff.color") || !strcmp(var, "color.diff")) {
215 return 0;
217 return git_log_config(var, value);
221 static FILE *realstdout = NULL;
222 static const char *output_directory = NULL;
224 static void reopen_stdout(struct commit *commit, int nr, int keep_subject)
226 char filename[1024];
227 char *sol;
228 int len = 0;
230 if (output_directory) {
231 strlcpy(filename, output_directory, 1010);
232 len = strlen(filename);
233 if (filename[len - 1] != '/')
234 filename[len++] = '/';
237 sprintf(filename + len, "%04d", nr);
238 len = strlen(filename);
240 sol = strstr(commit->buffer, "\n\n");
241 if (sol) {
242 int j, space = 1;
244 sol += 2;
245 /* strip [PATCH] or [PATCH blabla] */
246 if (!keep_subject && !strncmp(sol, "[PATCH", 6)) {
247 char *eos = strchr(sol + 6, ']');
248 if (eos) {
249 while (isspace(*eos))
250 eos++;
251 sol = eos;
255 for (j = 0; len < 1024 - 6 && sol[j] && sol[j] != '\n'; j++) {
256 if (istitlechar(sol[j])) {
257 if (space) {
258 filename[len++] = '-';
259 space = 0;
261 filename[len++] = sol[j];
262 if (sol[j] == '.')
263 while (sol[j + 1] == '.')
264 j++;
265 } else
266 space = 1;
268 while (filename[len - 1] == '.' || filename[len - 1] == '-')
269 len--;
271 strcpy(filename + len, ".txt");
272 fprintf(realstdout, "%s\n", filename);
273 freopen(filename, "w", stdout);
276 static int get_patch_id(struct commit *commit, struct diff_options *options,
277 unsigned char *sha1)
279 if (commit->parents)
280 diff_tree_sha1(commit->parents->item->object.sha1,
281 commit->object.sha1, "", options);
282 else
283 diff_root_tree_sha1(commit->object.sha1, "", options);
284 diffcore_std(options);
285 return diff_flush_patch_id(options, sha1);
288 static void get_patch_ids(struct rev_info *rev, struct diff_options *options, const char *prefix)
290 struct rev_info check_rev;
291 struct commit *commit;
292 struct object *o1, *o2;
293 unsigned flags1, flags2;
294 unsigned char sha1[20];
296 if (rev->pending.nr != 2)
297 die("Need exactly one range.");
299 o1 = rev->pending.objects[0].item;
300 flags1 = o1->flags;
301 o2 = rev->pending.objects[1].item;
302 flags2 = o2->flags;
304 if ((flags1 & UNINTERESTING) == (flags2 & UNINTERESTING))
305 die("Not a range.");
307 diff_setup(options);
308 options->recursive = 1;
309 if (diff_setup_done(options) < 0)
310 die("diff_setup_done failed");
312 /* given a range a..b get all patch ids for b..a */
313 init_revisions(&check_rev, prefix);
314 o1->flags ^= UNINTERESTING;
315 o2->flags ^= UNINTERESTING;
316 add_pending_object(&check_rev, o1, "o1");
317 add_pending_object(&check_rev, o2, "o2");
318 prepare_revision_walk(&check_rev);
320 while ((commit = get_revision(&check_rev)) != NULL) {
321 /* ignore merges */
322 if (commit->parents && commit->parents->next)
323 continue;
325 if (!get_patch_id(commit, options, sha1))
326 created_object(sha1, xcalloc(1, sizeof(struct object)));
329 /* reset for next revision walk */
330 clear_commit_marks((struct commit *)o1,
331 SEEN | UNINTERESTING | SHOWN | ADDED);
332 clear_commit_marks((struct commit *)o2,
333 SEEN | UNINTERESTING | SHOWN | ADDED);
334 o1->flags = flags1;
335 o2->flags = flags2;
338 static void gen_message_id(char *dest, unsigned int length, char *base)
340 const char *committer = git_committer_info(1);
341 const char *email_start = strrchr(committer, '<');
342 const char *email_end = strrchr(committer, '>');
343 if(!email_start || !email_end || email_start > email_end - 1)
344 die("Could not extract email from committer identity.");
345 snprintf(dest, length, "%s.%lu.git.%.*s", base,
346 (unsigned long) time(NULL),
347 (int)(email_end - email_start - 1), email_start + 1);
350 int cmd_format_patch(int argc, const char **argv, const char *prefix)
352 struct commit *commit;
353 struct commit **list = NULL;
354 struct rev_info rev;
355 int nr = 0, total, i, j;
356 int use_stdout = 0;
357 int numbered = 0;
358 int start_number = -1;
359 int keep_subject = 0;
360 int ignore_if_in_upstream = 0;
361 int thread = 0;
362 const char *in_reply_to = NULL;
363 struct diff_options patch_id_opts;
364 char *add_signoff = NULL;
365 char message_id[1024];
366 char ref_message_id[1024];
368 setup_ident();
369 git_config(git_format_config);
370 init_revisions(&rev, prefix);
371 rev.commit_format = CMIT_FMT_EMAIL;
372 rev.verbose_header = 1;
373 rev.diff = 1;
374 rev.combine_merges = 0;
375 rev.ignore_merges = 1;
376 rev.diffopt.msg_sep = "";
377 rev.diffopt.recursive = 1;
379 rev.extra_headers = extra_headers;
382 * Parse the arguments before setup_revisions(), or something
383 * like "git fmt-patch -o a123 HEAD^.." may fail; a123 is
384 * possibly a valid SHA1.
386 for (i = 1, j = 1; i < argc; i++) {
387 if (!strcmp(argv[i], "--stdout"))
388 use_stdout = 1;
389 else if (!strcmp(argv[i], "-n") ||
390 !strcmp(argv[i], "--numbered"))
391 numbered = 1;
392 else if (!strncmp(argv[i], "--start-number=", 15))
393 start_number = strtol(argv[i] + 15, NULL, 10);
394 else if (!strcmp(argv[i], "--start-number")) {
395 i++;
396 if (i == argc)
397 die("Need a number for --start-number");
398 start_number = strtol(argv[i], NULL, 10);
400 else if (!strcmp(argv[i], "-k") ||
401 !strcmp(argv[i], "--keep-subject")) {
402 keep_subject = 1;
403 rev.total = -1;
405 else if (!strcmp(argv[i], "--output-directory") ||
406 !strcmp(argv[i], "-o")) {
407 i++;
408 if (argc <= i)
409 die("Which directory?");
410 if (output_directory)
411 die("Two output directories?");
412 output_directory = argv[i];
414 else if (!strcmp(argv[i], "--signoff") ||
415 !strcmp(argv[i], "-s")) {
416 const char *committer;
417 const char *endpos;
418 committer = git_committer_info(1);
419 endpos = strchr(committer, '>');
420 if (!endpos)
421 die("bogos committer info %s\n", committer);
422 add_signoff = xmalloc(endpos - committer + 2);
423 memcpy(add_signoff, committer, endpos - committer + 1);
424 add_signoff[endpos - committer + 1] = 0;
426 else if (!strcmp(argv[i], "--attach"))
427 rev.mime_boundary = git_version_string;
428 else if (!strncmp(argv[i], "--attach=", 9))
429 rev.mime_boundary = argv[i] + 9;
430 else if (!strcmp(argv[i], "--ignore-if-in-upstream"))
431 ignore_if_in_upstream = 1;
432 else if (!strcmp(argv[i], "--thread"))
433 thread = 1;
434 else if (!strncmp(argv[i], "--in-reply-to=", 14))
435 in_reply_to = argv[i] + 14;
436 else if (!strcmp(argv[i], "--in-reply-to")) {
437 i++;
438 if (i == argc)
439 die("Need a Message-Id for --in-reply-to");
440 in_reply_to = argv[i];
442 else
443 argv[j++] = argv[i];
445 argc = j;
447 if (start_number < 0)
448 start_number = 1;
449 if (numbered && keep_subject)
450 die ("-n and -k are mutually exclusive.");
452 argc = setup_revisions(argc, argv, &rev, "HEAD");
453 if (argc > 1)
454 die ("unrecognized argument: %s", argv[1]);
456 if (!rev.diffopt.output_format)
457 rev.diffopt.output_format = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_PATCH;
459 if (!output_directory)
460 output_directory = prefix;
462 if (output_directory) {
463 if (use_stdout)
464 die("standard output, or directory, which one?");
465 if (mkdir(output_directory, 0777) < 0 && errno != EEXIST)
466 die("Could not create directory %s",
467 output_directory);
470 if (rev.pending.nr == 1) {
471 rev.pending.objects[0].item->flags |= UNINTERESTING;
472 add_head(&rev);
475 if (ignore_if_in_upstream)
476 get_patch_ids(&rev, &patch_id_opts, prefix);
478 if (!use_stdout)
479 realstdout = fdopen(dup(1), "w");
481 prepare_revision_walk(&rev);
482 while ((commit = get_revision(&rev)) != NULL) {
483 unsigned char sha1[20];
485 /* ignore merges */
486 if (commit->parents && commit->parents->next)
487 continue;
489 if (ignore_if_in_upstream &&
490 !get_patch_id(commit, &patch_id_opts, sha1) &&
491 lookup_object(sha1))
492 continue;
494 nr++;
495 list = xrealloc(list, nr * sizeof(list[0]));
496 list[nr - 1] = commit;
498 total = nr;
499 if (numbered)
500 rev.total = total + start_number - 1;
501 rev.add_signoff = add_signoff;
502 rev.ref_message_id = in_reply_to;
503 while (0 <= --nr) {
504 int shown;
505 commit = list[nr];
506 rev.nr = total - nr + (start_number - 1);
507 /* Make the second and subsequent mails replies to the first */
508 if (thread) {
509 if (nr == (total - 2)) {
510 strncpy(ref_message_id, message_id,
511 sizeof(ref_message_id));
512 ref_message_id[sizeof(ref_message_id)-1]='\0';
513 rev.ref_message_id = ref_message_id;
515 gen_message_id(message_id, sizeof(message_id),
516 sha1_to_hex(commit->object.sha1));
517 rev.message_id = message_id;
519 if (!use_stdout)
520 reopen_stdout(commit, rev.nr, keep_subject);
521 shown = log_tree_commit(&rev, commit);
522 free(commit->buffer);
523 commit->buffer = NULL;
525 /* We put one extra blank line between formatted
526 * patches and this flag is used by log-tree code
527 * to see if it needs to emit a LF before showing
528 * the log; when using one file per patch, we do
529 * not want the extra blank line.
531 if (!use_stdout)
532 rev.shown_one = 0;
533 if (shown) {
534 if (rev.mime_boundary)
535 printf("\n--%s%s--\n\n\n",
536 mime_boundary_leader,
537 rev.mime_boundary);
538 else
539 printf("-- \n%s\n\n", git_version_string);
541 if (!use_stdout)
542 fclose(stdout);
544 free(list);
545 return 0;
548 static int add_pending_commit(const char *arg, struct rev_info *revs, int flags)
550 unsigned char sha1[20];
551 if (get_sha1(arg, sha1) == 0) {
552 struct commit *commit = lookup_commit_reference(sha1);
553 if (commit) {
554 commit->object.flags |= flags;
555 add_pending_object(revs, &commit->object, arg);
556 return 0;
559 return -1;
562 static const char cherry_usage[] =
563 "git-cherry [-v] <upstream> [<head>] [<limit>]";
564 int cmd_cherry(int argc, const char **argv, const char *prefix)
566 struct rev_info revs;
567 struct diff_options patch_id_opts;
568 struct commit *commit;
569 struct commit_list *list = NULL;
570 const char *upstream;
571 const char *head = "HEAD";
572 const char *limit = NULL;
573 int verbose = 0;
575 if (argc > 1 && !strcmp(argv[1], "-v")) {
576 verbose = 1;
577 argc--;
578 argv++;
581 switch (argc) {
582 case 4:
583 limit = argv[3];
584 /* FALLTHROUGH */
585 case 3:
586 head = argv[2];
587 /* FALLTHROUGH */
588 case 2:
589 upstream = argv[1];
590 break;
591 default:
592 usage(cherry_usage);
595 init_revisions(&revs, prefix);
596 revs.diff = 1;
597 revs.combine_merges = 0;
598 revs.ignore_merges = 1;
599 revs.diffopt.recursive = 1;
601 if (add_pending_commit(head, &revs, 0))
602 die("Unknown commit %s", head);
603 if (add_pending_commit(upstream, &revs, UNINTERESTING))
604 die("Unknown commit %s", upstream);
606 /* Don't say anything if head and upstream are the same. */
607 if (revs.pending.nr == 2) {
608 struct object_array_entry *o = revs.pending.objects;
609 if (hashcmp(o[0].item->sha1, o[1].item->sha1) == 0)
610 return 0;
613 get_patch_ids(&revs, &patch_id_opts, prefix);
615 if (limit && add_pending_commit(limit, &revs, UNINTERESTING))
616 die("Unknown commit %s", limit);
618 /* reverse the list of commits */
619 prepare_revision_walk(&revs);
620 while ((commit = get_revision(&revs)) != NULL) {
621 /* ignore merges */
622 if (commit->parents && commit->parents->next)
623 continue;
625 commit_list_insert(commit, &list);
628 while (list) {
629 unsigned char sha1[20];
630 char sign = '+';
632 commit = list->item;
633 if (!get_patch_id(commit, &patch_id_opts, sha1) &&
634 lookup_object(sha1))
635 sign = '-';
637 if (verbose) {
638 static char buf[16384];
639 pretty_print_commit(CMIT_FMT_ONELINE, commit, ~0,
640 buf, sizeof(buf), 0, NULL, NULL, 0);
641 printf("%c %s %s\n", sign,
642 sha1_to_hex(commit->object.sha1), buf);
644 else {
645 printf("%c %s\n", sign,
646 sha1_to_hex(commit->object.sha1));
649 list = list->next;
652 return 0;