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