cmd_log_init: remove parsing of --encoding command line parameter
[alt-git.git] / builtin-log.c
blob0aede7683986aff131f9278a968c84622c01181b
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"
14 #include "reflog-walk.h"
15 #include "patch-ids.h"
16 #include "refs.h"
18 static int default_show_root = 1;
20 /* this is in builtin-diff.c */
21 void add_head(struct rev_info *revs);
23 static void add_name_decoration(const char *prefix, const char *name, struct object *obj)
25 int plen = strlen(prefix);
26 int nlen = strlen(name);
27 struct name_decoration *res = xmalloc(sizeof(struct name_decoration) + plen + nlen);
28 memcpy(res->name, prefix, plen);
29 memcpy(res->name + plen, name, nlen + 1);
30 res->next = add_decoration(&name_decoration, obj, res);
33 static int add_ref_decoration(const char *refname, const unsigned char *sha1, int flags, void *cb_data)
35 struct object *obj = parse_object(sha1);
36 if (!obj)
37 return 0;
38 add_name_decoration("", refname, obj);
39 while (obj->type == OBJ_TAG) {
40 obj = ((struct tag *)obj)->tagged;
41 if (!obj)
42 break;
43 add_name_decoration("tag: ", refname, obj);
45 return 0;
48 static void cmd_log_init(int argc, const char **argv, const char *prefix,
49 struct rev_info *rev)
51 int i;
52 int decorate = 0;
54 rev->abbrev = DEFAULT_ABBREV;
55 rev->commit_format = CMIT_FMT_DEFAULT;
56 rev->verbose_header = 1;
57 rev->show_root_diff = default_show_root;
58 argc = setup_revisions(argc, argv, rev, "HEAD");
59 if (rev->diffopt.pickaxe || rev->diffopt.filter)
60 rev->always_show_header = 0;
61 for (i = 1; i < argc; i++) {
62 const char *arg = argv[i];
63 if (!strcmp(arg, "--decorate")) {
64 if (!decorate)
65 for_each_ref(add_ref_decoration, NULL);
66 decorate = 1;
67 } else
68 die("unrecognized argument: %s", arg);
72 static int cmd_log_walk(struct rev_info *rev)
74 struct commit *commit;
76 prepare_revision_walk(rev);
77 while ((commit = get_revision(rev)) != NULL) {
78 log_tree_commit(rev, commit);
79 if (!rev->reflog_info) {
80 /* we allow cycles in reflog ancestry */
81 free(commit->buffer);
82 commit->buffer = NULL;
84 free_commit_list(commit->parents);
85 commit->parents = NULL;
87 return 0;
90 static int git_log_config(const char *var, const char *value)
92 if (!strcmp(var, "log.showroot")) {
93 default_show_root = git_config_bool(var, value);
94 return 0;
96 return git_diff_ui_config(var, value);
99 int cmd_whatchanged(int argc, const char **argv, const char *prefix)
101 struct rev_info rev;
103 git_config(git_log_config);
104 init_revisions(&rev, prefix);
105 rev.diff = 1;
106 rev.diffopt.recursive = 1;
107 rev.simplify_history = 0;
108 cmd_log_init(argc, argv, prefix, &rev);
109 if (!rev.diffopt.output_format)
110 rev.diffopt.output_format = DIFF_FORMAT_RAW;
111 return cmd_log_walk(&rev);
114 static int show_object(const unsigned char *sha1, int suppress_header)
116 unsigned long size;
117 enum object_type type;
118 char *buf = read_sha1_file(sha1, &type, &size);
119 int offset = 0;
121 if (!buf)
122 return error("Could not read object %s", sha1_to_hex(sha1));
124 if (suppress_header)
125 while (offset < size && buf[offset++] != '\n') {
126 int new_offset = offset;
127 while (new_offset < size && buf[new_offset++] != '\n')
128 ; /* do nothing */
129 offset = new_offset;
132 if (offset < size)
133 fwrite(buf + offset, size - offset, 1, stdout);
134 free(buf);
135 return 0;
138 static int show_tree_object(const unsigned char *sha1,
139 const char *base, int baselen,
140 const char *pathname, unsigned mode, int stage)
142 printf("%s%s\n", pathname, S_ISDIR(mode) ? "/" : "");
143 return 0;
146 int cmd_show(int argc, const char **argv, const char *prefix)
148 struct rev_info rev;
149 struct object_array_entry *objects;
150 int i, count, ret = 0;
152 git_config(git_log_config);
153 init_revisions(&rev, prefix);
154 rev.diff = 1;
155 rev.diffopt.recursive = 1;
156 rev.combine_merges = 1;
157 rev.dense_combined_merges = 1;
158 rev.always_show_header = 1;
159 rev.ignore_merges = 0;
160 rev.no_walk = 1;
161 cmd_log_init(argc, argv, prefix, &rev);
163 count = rev.pending.nr;
164 objects = rev.pending.objects;
165 for (i = 0; i < count && !ret; i++) {
166 struct object *o = objects[i].item;
167 const char *name = objects[i].name;
168 switch (o->type) {
169 case OBJ_BLOB:
170 ret = show_object(o->sha1, 0);
171 break;
172 case OBJ_TAG: {
173 struct tag *t = (struct tag *)o;
175 printf("%stag %s%s\n\n",
176 diff_get_color(rev.diffopt.color_diff,
177 DIFF_COMMIT),
178 t->tag,
179 diff_get_color(rev.diffopt.color_diff,
180 DIFF_RESET));
181 ret = show_object(o->sha1, 1);
182 objects[i].item = (struct object *)t->tagged;
183 i--;
184 break;
186 case OBJ_TREE:
187 printf("%stree %s%s\n\n",
188 diff_get_color(rev.diffopt.color_diff,
189 DIFF_COMMIT),
190 name,
191 diff_get_color(rev.diffopt.color_diff,
192 DIFF_RESET));
193 read_tree_recursive((struct tree *)o, "", 0, 0, NULL,
194 show_tree_object);
195 break;
196 case OBJ_COMMIT:
197 rev.pending.nr = rev.pending.alloc = 0;
198 rev.pending.objects = NULL;
199 add_object_array(o, name, &rev.pending);
200 ret = cmd_log_walk(&rev);
201 break;
202 default:
203 ret = error("Unknown type: %d", o->type);
206 free(objects);
207 return ret;
211 * This is equivalent to "git log -g --abbrev-commit --pretty=oneline"
213 int cmd_log_reflog(int argc, const char **argv, const char *prefix)
215 struct rev_info rev;
217 git_config(git_log_config);
218 init_revisions(&rev, prefix);
219 init_reflog_walk(&rev.reflog_info);
220 rev.abbrev_commit = 1;
221 rev.verbose_header = 1;
222 cmd_log_init(argc, argv, prefix, &rev);
225 * This means that we override whatever commit format the user gave
226 * on the cmd line. Sad, but cmd_log_init() currently doesn't
227 * allow us to set a different default.
229 rev.commit_format = CMIT_FMT_ONELINE;
230 rev.always_show_header = 1;
233 * We get called through "git reflog", so unlike the other log
234 * routines, we need to set up our pager manually..
236 setup_pager();
238 return cmd_log_walk(&rev);
241 int cmd_log(int argc, const char **argv, const char *prefix)
243 struct rev_info rev;
245 git_config(git_log_config);
246 init_revisions(&rev, prefix);
247 rev.always_show_header = 1;
248 cmd_log_init(argc, argv, prefix, &rev);
249 return cmd_log_walk(&rev);
252 /* format-patch */
253 #define FORMAT_PATCH_NAME_MAX 64
255 static int istitlechar(char c)
257 return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
258 (c >= '0' && c <= '9') || c == '.' || c == '_';
261 static char *extra_headers = NULL;
262 static int extra_headers_size = 0;
263 static const char *fmt_patch_suffix = ".patch";
265 static int git_format_config(const char *var, const char *value)
267 if (!strcmp(var, "format.headers")) {
268 int len;
270 if (!value)
271 die("format.headers without value");
272 len = strlen(value);
273 extra_headers_size += len + 1;
274 extra_headers = xrealloc(extra_headers, extra_headers_size);
275 extra_headers[extra_headers_size - len - 1] = 0;
276 strcat(extra_headers, value);
277 return 0;
279 if (!strcmp(var, "format.suffix")) {
280 if (!value)
281 die("format.suffix without value");
282 fmt_patch_suffix = xstrdup(value);
283 return 0;
285 if (!strcmp(var, "diff.color") || !strcmp(var, "color.diff")) {
286 return 0;
288 return git_log_config(var, value);
292 static FILE *realstdout = NULL;
293 static const char *output_directory = NULL;
295 static int reopen_stdout(struct commit *commit, int nr, int keep_subject,
296 int numbered_files)
298 char filename[PATH_MAX];
299 char *sol;
300 int len = 0;
301 int suffix_len = strlen(fmt_patch_suffix) + 1;
303 if (output_directory) {
304 if (strlen(output_directory) >=
305 sizeof(filename) - FORMAT_PATCH_NAME_MAX - suffix_len)
306 return error("name of output directory is too long");
307 strlcpy(filename, output_directory, sizeof(filename) - suffix_len);
308 len = strlen(filename);
309 if (filename[len - 1] != '/')
310 filename[len++] = '/';
313 if (numbered_files) {
314 sprintf(filename + len, "%d", nr);
315 len = strlen(filename);
317 } else {
318 sprintf(filename + len, "%04d", nr);
319 len = strlen(filename);
321 sol = strstr(commit->buffer, "\n\n");
322 if (sol) {
323 int j, space = 1;
325 sol += 2;
326 /* strip [PATCH] or [PATCH blabla] */
327 if (!keep_subject && !prefixcmp(sol, "[PATCH")) {
328 char *eos = strchr(sol + 6, ']');
329 if (eos) {
330 while (isspace(*eos))
331 eos++;
332 sol = eos;
336 for (j = 0;
337 j < FORMAT_PATCH_NAME_MAX - suffix_len - 5 &&
338 len < sizeof(filename) - suffix_len &&
339 sol[j] && sol[j] != '\n';
340 j++) {
341 if (istitlechar(sol[j])) {
342 if (space) {
343 filename[len++] = '-';
344 space = 0;
346 filename[len++] = sol[j];
347 if (sol[j] == '.')
348 while (sol[j + 1] == '.')
349 j++;
350 } else
351 space = 1;
353 while (filename[len - 1] == '.'
354 || filename[len - 1] == '-')
355 len--;
356 filename[len] = 0;
358 if (len + suffix_len >= sizeof(filename))
359 return error("Patch pathname too long");
360 strcpy(filename + len, fmt_patch_suffix);
363 fprintf(realstdout, "%s\n", filename);
364 if (freopen(filename, "w", stdout) == NULL)
365 return error("Cannot open patch file %s",filename);
367 return 0;
370 static void get_patch_ids(struct rev_info *rev, struct patch_ids *ids, const char *prefix)
372 struct rev_info check_rev;
373 struct commit *commit;
374 struct object *o1, *o2;
375 unsigned flags1, flags2;
377 if (rev->pending.nr != 2)
378 die("Need exactly one range.");
380 o1 = rev->pending.objects[0].item;
381 flags1 = o1->flags;
382 o2 = rev->pending.objects[1].item;
383 flags2 = o2->flags;
385 if ((flags1 & UNINTERESTING) == (flags2 & UNINTERESTING))
386 die("Not a range.");
388 init_patch_ids(ids);
390 /* given a range a..b get all patch ids for b..a */
391 init_revisions(&check_rev, prefix);
392 o1->flags ^= UNINTERESTING;
393 o2->flags ^= UNINTERESTING;
394 add_pending_object(&check_rev, o1, "o1");
395 add_pending_object(&check_rev, o2, "o2");
396 prepare_revision_walk(&check_rev);
398 while ((commit = get_revision(&check_rev)) != NULL) {
399 /* ignore merges */
400 if (commit->parents && commit->parents->next)
401 continue;
403 add_commit_patch_id(commit, ids);
406 /* reset for next revision walk */
407 clear_commit_marks((struct commit *)o1,
408 SEEN | UNINTERESTING | SHOWN | ADDED);
409 clear_commit_marks((struct commit *)o2,
410 SEEN | UNINTERESTING | SHOWN | ADDED);
411 o1->flags = flags1;
412 o2->flags = flags2;
415 static void gen_message_id(char *dest, unsigned int length, char *base)
417 const char *committer = git_committer_info(-1);
418 const char *email_start = strrchr(committer, '<');
419 const char *email_end = strrchr(committer, '>');
420 if(!email_start || !email_end || email_start > email_end - 1)
421 die("Could not extract email from committer identity.");
422 snprintf(dest, length, "%s.%lu.git.%.*s", base,
423 (unsigned long) time(NULL),
424 (int)(email_end - email_start - 1), email_start + 1);
427 int cmd_format_patch(int argc, const char **argv, const char *prefix)
429 struct commit *commit;
430 struct commit **list = NULL;
431 struct rev_info rev;
432 int nr = 0, total, i, j;
433 int use_stdout = 0;
434 int numbered = 0;
435 int start_number = -1;
436 int keep_subject = 0;
437 int numbered_files = 0; /* _just_ numbers */
438 int subject_prefix = 0;
439 int ignore_if_in_upstream = 0;
440 int thread = 0;
441 const char *in_reply_to = NULL;
442 struct patch_ids ids;
443 char *add_signoff = NULL;
444 char message_id[1024];
445 char ref_message_id[1024];
447 git_config(git_format_config);
448 init_revisions(&rev, prefix);
449 rev.commit_format = CMIT_FMT_EMAIL;
450 rev.verbose_header = 1;
451 rev.diff = 1;
452 rev.combine_merges = 0;
453 rev.ignore_merges = 1;
454 rev.diffopt.msg_sep = "";
455 rev.diffopt.recursive = 1;
457 rev.extra_headers = extra_headers;
460 * Parse the arguments before setup_revisions(), or something
461 * like "git format-patch -o a123 HEAD^.." may fail; a123 is
462 * possibly a valid SHA1.
464 for (i = 1, j = 1; i < argc; i++) {
465 if (!strcmp(argv[i], "--stdout"))
466 use_stdout = 1;
467 else if (!strcmp(argv[i], "-n") ||
468 !strcmp(argv[i], "--numbered"))
469 numbered = 1;
470 else if (!prefixcmp(argv[i], "--start-number="))
471 start_number = strtol(argv[i] + 15, NULL, 10);
472 else if (!strcmp(argv[i], "--numbered-files"))
473 numbered_files = 1;
474 else if (!strcmp(argv[i], "--start-number")) {
475 i++;
476 if (i == argc)
477 die("Need a number for --start-number");
478 start_number = strtol(argv[i], NULL, 10);
480 else if (!strcmp(argv[i], "-k") ||
481 !strcmp(argv[i], "--keep-subject")) {
482 keep_subject = 1;
483 rev.total = -1;
485 else if (!strcmp(argv[i], "--output-directory") ||
486 !strcmp(argv[i], "-o")) {
487 i++;
488 if (argc <= i)
489 die("Which directory?");
490 if (output_directory)
491 die("Two output directories?");
492 output_directory = argv[i];
494 else if (!strcmp(argv[i], "--signoff") ||
495 !strcmp(argv[i], "-s")) {
496 const char *committer;
497 const char *endpos;
498 committer = git_committer_info(1);
499 endpos = strchr(committer, '>');
500 if (!endpos)
501 die("bogos committer info %s\n", committer);
502 add_signoff = xmalloc(endpos - committer + 2);
503 memcpy(add_signoff, committer, endpos - committer + 1);
504 add_signoff[endpos - committer + 1] = 0;
506 else if (!strcmp(argv[i], "--attach")) {
507 rev.mime_boundary = git_version_string;
508 rev.no_inline = 1;
510 else if (!prefixcmp(argv[i], "--attach=")) {
511 rev.mime_boundary = argv[i] + 9;
512 rev.no_inline = 1;
514 else if (!strcmp(argv[i], "--inline")) {
515 rev.mime_boundary = git_version_string;
516 rev.no_inline = 0;
518 else if (!prefixcmp(argv[i], "--inline=")) {
519 rev.mime_boundary = argv[i] + 9;
520 rev.no_inline = 0;
522 else if (!strcmp(argv[i], "--ignore-if-in-upstream"))
523 ignore_if_in_upstream = 1;
524 else if (!strcmp(argv[i], "--thread"))
525 thread = 1;
526 else if (!prefixcmp(argv[i], "--in-reply-to="))
527 in_reply_to = argv[i] + 14;
528 else if (!strcmp(argv[i], "--in-reply-to")) {
529 i++;
530 if (i == argc)
531 die("Need a Message-Id for --in-reply-to");
532 in_reply_to = argv[i];
533 } else if (!prefixcmp(argv[i], "--subject-prefix=")) {
534 subject_prefix = 1;
535 rev.subject_prefix = argv[i] + 17;
536 } else if (!prefixcmp(argv[i], "--suffix="))
537 fmt_patch_suffix = argv[i] + 9;
538 else
539 argv[j++] = argv[i];
541 argc = j;
543 if (start_number < 0)
544 start_number = 1;
545 if (numbered && keep_subject)
546 die ("-n and -k are mutually exclusive.");
547 if (keep_subject && subject_prefix)
548 die ("--subject-prefix and -k are mutually exclusive.");
549 if (numbered_files && use_stdout)
550 die ("--numbered-files and --stdout are mutually exclusive.");
552 argc = setup_revisions(argc, argv, &rev, "HEAD");
553 if (argc > 1)
554 die ("unrecognized argument: %s", argv[1]);
556 if (!rev.diffopt.output_format)
557 rev.diffopt.output_format = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_SUMMARY | DIFF_FORMAT_PATCH;
559 if (!rev.diffopt.text)
560 rev.diffopt.binary = 1;
562 if (!output_directory && !use_stdout)
563 output_directory = prefix;
565 if (output_directory) {
566 if (use_stdout)
567 die("standard output, or directory, which one?");
568 if (mkdir(output_directory, 0777) < 0 && errno != EEXIST)
569 die("Could not create directory %s",
570 output_directory);
573 if (rev.pending.nr == 1) {
574 if (rev.max_count < 0) {
575 rev.pending.objects[0].item->flags |= UNINTERESTING;
576 add_head(&rev);
578 /* Otherwise, it is "format-patch -22 HEAD", and
579 * get_revision() would return only the specified count.
583 if (ignore_if_in_upstream)
584 get_patch_ids(&rev, &ids, prefix);
586 if (!use_stdout)
587 realstdout = fdopen(dup(1), "w");
589 prepare_revision_walk(&rev);
590 while ((commit = get_revision(&rev)) != NULL) {
591 /* ignore merges */
592 if (commit->parents && commit->parents->next)
593 continue;
595 if (ignore_if_in_upstream &&
596 has_commit_patch_id(commit, &ids))
597 continue;
599 nr++;
600 list = xrealloc(list, nr * sizeof(list[0]));
601 list[nr - 1] = commit;
603 total = nr;
604 if (numbered)
605 rev.total = total + start_number - 1;
606 rev.add_signoff = add_signoff;
607 rev.ref_message_id = in_reply_to;
608 while (0 <= --nr) {
609 int shown;
610 commit = list[nr];
611 rev.nr = total - nr + (start_number - 1);
612 /* Make the second and subsequent mails replies to the first */
613 if (thread) {
614 if (nr == (total - 2)) {
615 strncpy(ref_message_id, message_id,
616 sizeof(ref_message_id));
617 ref_message_id[sizeof(ref_message_id)-1]='\0';
618 rev.ref_message_id = ref_message_id;
620 gen_message_id(message_id, sizeof(message_id),
621 sha1_to_hex(commit->object.sha1));
622 rev.message_id = message_id;
624 if (!use_stdout)
625 if (reopen_stdout(commit, rev.nr, keep_subject,
626 numbered_files))
627 die("Failed to create output files");
628 shown = log_tree_commit(&rev, commit);
629 free(commit->buffer);
630 commit->buffer = NULL;
632 /* We put one extra blank line between formatted
633 * patches and this flag is used by log-tree code
634 * to see if it needs to emit a LF before showing
635 * the log; when using one file per patch, we do
636 * not want the extra blank line.
638 if (!use_stdout)
639 rev.shown_one = 0;
640 if (shown) {
641 if (rev.mime_boundary)
642 printf("\n--%s%s--\n\n\n",
643 mime_boundary_leader,
644 rev.mime_boundary);
645 else
646 printf("-- \n%s\n\n", git_version_string);
648 if (!use_stdout)
649 fclose(stdout);
651 free(list);
652 if (ignore_if_in_upstream)
653 free_patch_ids(&ids);
654 return 0;
657 static int add_pending_commit(const char *arg, struct rev_info *revs, int flags)
659 unsigned char sha1[20];
660 if (get_sha1(arg, sha1) == 0) {
661 struct commit *commit = lookup_commit_reference(sha1);
662 if (commit) {
663 commit->object.flags |= flags;
664 add_pending_object(revs, &commit->object, arg);
665 return 0;
668 return -1;
671 static const char cherry_usage[] =
672 "git-cherry [-v] <upstream> [<head>] [<limit>]";
673 int cmd_cherry(int argc, const char **argv, const char *prefix)
675 struct rev_info revs;
676 struct patch_ids ids;
677 struct commit *commit;
678 struct commit_list *list = NULL;
679 const char *upstream;
680 const char *head = "HEAD";
681 const char *limit = NULL;
682 int verbose = 0;
684 if (argc > 1 && !strcmp(argv[1], "-v")) {
685 verbose = 1;
686 argc--;
687 argv++;
690 switch (argc) {
691 case 4:
692 limit = argv[3];
693 /* FALLTHROUGH */
694 case 3:
695 head = argv[2];
696 /* FALLTHROUGH */
697 case 2:
698 upstream = argv[1];
699 break;
700 default:
701 usage(cherry_usage);
704 init_revisions(&revs, prefix);
705 revs.diff = 1;
706 revs.combine_merges = 0;
707 revs.ignore_merges = 1;
708 revs.diffopt.recursive = 1;
710 if (add_pending_commit(head, &revs, 0))
711 die("Unknown commit %s", head);
712 if (add_pending_commit(upstream, &revs, UNINTERESTING))
713 die("Unknown commit %s", upstream);
715 /* Don't say anything if head and upstream are the same. */
716 if (revs.pending.nr == 2) {
717 struct object_array_entry *o = revs.pending.objects;
718 if (hashcmp(o[0].item->sha1, o[1].item->sha1) == 0)
719 return 0;
722 get_patch_ids(&revs, &ids, prefix);
724 if (limit && add_pending_commit(limit, &revs, UNINTERESTING))
725 die("Unknown commit %s", limit);
727 /* reverse the list of commits */
728 prepare_revision_walk(&revs);
729 while ((commit = get_revision(&revs)) != NULL) {
730 /* ignore merges */
731 if (commit->parents && commit->parents->next)
732 continue;
734 commit_list_insert(commit, &list);
737 while (list) {
738 char sign = '+';
740 commit = list->item;
741 if (has_commit_patch_id(commit, &ids))
742 sign = '-';
744 if (verbose) {
745 static char buf[16384];
746 pretty_print_commit(CMIT_FMT_ONELINE, commit, ~0,
747 buf, sizeof(buf), 0, NULL, NULL, 0);
748 printf("%c %s %s\n", sign,
749 sha1_to_hex(commit->object.sha1), buf);
751 else {
752 printf("%c %s\n", sign,
753 sha1_to_hex(commit->object.sha1));
756 list = list->next;
759 free_patch_ids(&ids);
760 return 0;