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