git-log --cherry-pick A...B
[git/jrn.git] / builtin-log.c
blobeea96901b9dc339aa2b60ba35c8ce6a5ee369125
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"
17 static int default_show_root = 1;
19 /* this is in builtin-diff.c */
20 void add_head(struct rev_info *revs);
22 static void cmd_log_init(int argc, const char **argv, const char *prefix,
23 struct rev_info *rev)
25 int i;
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 = "";
43 else
44 die("unrecognized argument: %s", arg);
48 static int cmd_log_walk(struct rev_info *rev)
50 struct commit *commit;
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;
60 free_commit_list(commit->parents);
61 commit->parents = NULL;
63 return 0;
66 static int git_log_config(const char *var, const char *value)
68 if (!strcmp(var, "log.showroot")) {
69 default_show_root = git_config_bool(var, value);
70 return 0;
72 return git_diff_ui_config(var, value);
75 int cmd_whatchanged(int argc, const char **argv, const char *prefix)
77 struct rev_info rev;
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);
90 static int show_object(const unsigned char *sha1, int suppress_header)
92 unsigned long size;
93 enum object_type type;
94 char *buf = read_sha1_file(sha1, &type, &size);
95 int offset = 0;
97 if (!buf)
98 return error("Could not read object %s", sha1_to_hex(sha1));
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;
108 if (offset < size)
109 fwrite(buf + offset, size - offset, 1, stdout);
110 free(buf);
111 return 0;
114 static int show_tree_object(const unsigned char *sha1,
115 const char *base, int baselen,
116 const char *pathname, unsigned mode, int stage)
118 printf("%s%s\n", pathname, S_ISDIR(mode) ? "/" : "");
119 return 0;
122 int cmd_show(int argc, const char **argv, const char *prefix)
124 struct rev_info rev;
125 struct object_array_entry *objects;
126 int i, count, ret = 0;
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);
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;
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;
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);
182 free(objects);
183 return ret;
187 * This is equivalent to "git log -g --abbrev-commit --pretty=oneline"
189 int cmd_log_reflog(int argc, const char **argv, const char *prefix)
191 struct rev_info rev;
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);
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.
205 rev.commit_format = CMIT_FMT_ONELINE;
206 rev.always_show_header = 1;
209 * We get called through "git reflog", so unlike the other log
210 * routines, we need to set up our pager manually..
212 setup_pager();
214 return cmd_log_walk(&rev);
217 int cmd_log(int argc, const char **argv, const char *prefix)
219 struct rev_info rev;
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);
228 /* format-patch */
229 #define FORMAT_PATCH_NAME_MAX 64
231 static int istitlechar(char c)
233 return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
234 (c >= '0' && c <= '9') || c == '.' || c == '_';
237 static char *extra_headers = NULL;
238 static int extra_headers_size = 0;
239 static const char *fmt_patch_suffix = ".patch";
241 static int git_format_config(const char *var, const char *value)
243 if (!strcmp(var, "format.headers")) {
244 int len;
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;
255 if (!strcmp(var, "format.suffix")) {
256 if (!value)
257 die("format.suffix without value");
258 fmt_patch_suffix = xstrdup(value);
259 return 0;
261 if (!strcmp(var, "diff.color") || !strcmp(var, "color.diff")) {
262 return 0;
264 return git_log_config(var, value);
268 static FILE *realstdout = NULL;
269 static const char *output_directory = NULL;
271 static int reopen_stdout(struct commit *commit, int nr, int keep_subject)
273 char filename[PATH_MAX];
274 char *sol;
275 int len = 0;
276 int suffix_len = strlen(fmt_patch_suffix) + 1;
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++] = '/';
288 sprintf(filename + len, "%04d", nr);
289 len = strlen(filename);
291 sol = strstr(commit->buffer, "\n\n");
292 if (sol) {
293 int j, space = 1;
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;
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;
316 filename[len++] = sol[j];
317 if (sol[j] == '.')
318 while (sol[j + 1] == '.')
319 j++;
320 } else
321 space = 1;
323 while (filename[len - 1] == '.' || filename[len - 1] == '-')
324 len--;
325 filename[len] = 0;
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;
337 static void get_patch_ids(struct rev_info *rev, struct patch_ids *ids, const char *prefix)
339 struct rev_info check_rev;
340 struct commit *commit;
341 struct object *o1, *o2;
342 unsigned flags1, flags2;
344 if (rev->pending.nr != 2)
345 die("Need exactly one range.");
347 o1 = rev->pending.objects[0].item;
348 flags1 = o1->flags;
349 o2 = rev->pending.objects[1].item;
350 flags2 = o2->flags;
352 if ((flags1 & UNINTERESTING) == (flags2 & UNINTERESTING))
353 die("Not a range.");
355 init_patch_ids(ids);
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);
365 while ((commit = get_revision(&check_rev)) != NULL) {
366 /* ignore merges */
367 if (commit->parents && commit->parents->next)
368 continue;
370 add_commit_patch_id(commit, ids);
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;
382 static void gen_message_id(char *dest, unsigned int length, char *base)
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);
394 int cmd_format_patch(int argc, const char **argv, const char *prefix)
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];
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 rev.subject_prefix = "PATCH";
424 rev.extra_headers = extra_headers;
427 * Parse the arguments before setup_revisions(), or something
428 * like "git fmt-patch -o a123 HEAD^.." may fail; a123 is
429 * possibly a valid SHA1.
431 for (i = 1, j = 1; i < argc; i++) {
432 if (!strcmp(argv[i], "--stdout"))
433 use_stdout = 1;
434 else if (!strcmp(argv[i], "-n") ||
435 !strcmp(argv[i], "--numbered"))
436 numbered = 1;
437 else if (!prefixcmp(argv[i], "--start-number="))
438 start_number = strtol(argv[i] + 15, NULL, 10);
439 else if (!strcmp(argv[i], "--start-number")) {
440 i++;
441 if (i == argc)
442 die("Need a number for --start-number");
443 start_number = strtol(argv[i], NULL, 10);
445 else if (!strcmp(argv[i], "-k") ||
446 !strcmp(argv[i], "--keep-subject")) {
447 keep_subject = 1;
448 rev.total = -1;
450 else if (!strcmp(argv[i], "--output-directory") ||
451 !strcmp(argv[i], "-o")) {
452 i++;
453 if (argc <= i)
454 die("Which directory?");
455 if (output_directory)
456 die("Two output directories?");
457 output_directory = argv[i];
459 else if (!strcmp(argv[i], "--signoff") ||
460 !strcmp(argv[i], "-s")) {
461 const char *committer;
462 const char *endpos;
463 committer = git_committer_info(1);
464 endpos = strchr(committer, '>');
465 if (!endpos)
466 die("bogos committer info %s\n", committer);
467 add_signoff = xmalloc(endpos - committer + 2);
468 memcpy(add_signoff, committer, endpos - committer + 1);
469 add_signoff[endpos - committer + 1] = 0;
471 else if (!strcmp(argv[i], "--attach")) {
472 rev.mime_boundary = git_version_string;
473 rev.no_inline = 1;
475 else if (!prefixcmp(argv[i], "--attach=")) {
476 rev.mime_boundary = argv[i] + 9;
477 rev.no_inline = 1;
479 else if (!strcmp(argv[i], "--inline")) {
480 rev.mime_boundary = git_version_string;
481 rev.no_inline = 0;
483 else if (!prefixcmp(argv[i], "--inline=")) {
484 rev.mime_boundary = argv[i] + 9;
485 rev.no_inline = 0;
487 else if (!strcmp(argv[i], "--ignore-if-in-upstream"))
488 ignore_if_in_upstream = 1;
489 else if (!strcmp(argv[i], "--thread"))
490 thread = 1;
491 else if (!prefixcmp(argv[i], "--in-reply-to="))
492 in_reply_to = argv[i] + 14;
493 else if (!strcmp(argv[i], "--in-reply-to")) {
494 i++;
495 if (i == argc)
496 die("Need a Message-Id for --in-reply-to");
497 in_reply_to = argv[i];
498 } else if (!prefixcmp(argv[i], "--subject-prefix=")) {
499 subject_prefix = 1;
500 rev.subject_prefix = argv[i] + 17;
501 } else if (!prefixcmp(argv[i], "--suffix="))
502 fmt_patch_suffix = argv[i] + 9;
503 else
504 argv[j++] = argv[i];
506 argc = j;
508 if (start_number < 0)
509 start_number = 1;
510 if (numbered && keep_subject)
511 die ("-n and -k are mutually exclusive.");
512 if (keep_subject && subject_prefix)
513 die ("--subject-prefix and -k are mutually exclusive.");
515 argc = setup_revisions(argc, argv, &rev, "HEAD");
516 if (argc > 1)
517 die ("unrecognized argument: %s", argv[1]);
519 if (!rev.diffopt.output_format)
520 rev.diffopt.output_format = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_SUMMARY | DIFF_FORMAT_PATCH;
522 if (!rev.diffopt.text)
523 rev.diffopt.binary = 1;
525 if (!output_directory && !use_stdout)
526 output_directory = prefix;
528 if (output_directory) {
529 if (use_stdout)
530 die("standard output, or directory, which one?");
531 if (mkdir(output_directory, 0777) < 0 && errno != EEXIST)
532 die("Could not create directory %s",
533 output_directory);
536 if (rev.pending.nr == 1) {
537 if (rev.max_count < 0) {
538 rev.pending.objects[0].item->flags |= UNINTERESTING;
539 add_head(&rev);
541 /* Otherwise, it is "format-patch -22 HEAD", and
542 * get_revision() would return only the specified count.
546 if (ignore_if_in_upstream)
547 get_patch_ids(&rev, &ids, prefix);
549 if (!use_stdout)
550 realstdout = fdopen(dup(1), "w");
552 prepare_revision_walk(&rev);
553 while ((commit = get_revision(&rev)) != NULL) {
554 /* ignore merges */
555 if (commit->parents && commit->parents->next)
556 continue;
558 if (ignore_if_in_upstream &&
559 has_commit_patch_id(commit, &ids))
560 continue;
562 nr++;
563 list = xrealloc(list, nr * sizeof(list[0]));
564 list[nr - 1] = commit;
566 total = nr;
567 if (numbered)
568 rev.total = total + start_number - 1;
569 rev.add_signoff = add_signoff;
570 rev.ref_message_id = in_reply_to;
571 while (0 <= --nr) {
572 int shown;
573 commit = list[nr];
574 rev.nr = total - nr + (start_number - 1);
575 /* Make the second and subsequent mails replies to the first */
576 if (thread) {
577 if (nr == (total - 2)) {
578 strncpy(ref_message_id, message_id,
579 sizeof(ref_message_id));
580 ref_message_id[sizeof(ref_message_id)-1]='\0';
581 rev.ref_message_id = ref_message_id;
583 gen_message_id(message_id, sizeof(message_id),
584 sha1_to_hex(commit->object.sha1));
585 rev.message_id = message_id;
587 if (!use_stdout)
588 if (reopen_stdout(commit, rev.nr, keep_subject))
589 die("Failed to create output files");
590 shown = log_tree_commit(&rev, commit);
591 free(commit->buffer);
592 commit->buffer = NULL;
594 /* We put one extra blank line between formatted
595 * patches and this flag is used by log-tree code
596 * to see if it needs to emit a LF before showing
597 * the log; when using one file per patch, we do
598 * not want the extra blank line.
600 if (!use_stdout)
601 rev.shown_one = 0;
602 if (shown) {
603 if (rev.mime_boundary)
604 printf("\n--%s%s--\n\n\n",
605 mime_boundary_leader,
606 rev.mime_boundary);
607 else
608 printf("-- \n%s\n\n", git_version_string);
610 if (!use_stdout)
611 fclose(stdout);
613 free(list);
614 if (ignore_if_in_upstream)
615 free_patch_ids(&ids);
616 return 0;
619 static int add_pending_commit(const char *arg, struct rev_info *revs, int flags)
621 unsigned char sha1[20];
622 if (get_sha1(arg, sha1) == 0) {
623 struct commit *commit = lookup_commit_reference(sha1);
624 if (commit) {
625 commit->object.flags |= flags;
626 add_pending_object(revs, &commit->object, arg);
627 return 0;
630 return -1;
633 static const char cherry_usage[] =
634 "git-cherry [-v] <upstream> [<head>] [<limit>]";
635 int cmd_cherry(int argc, const char **argv, const char *prefix)
637 struct rev_info revs;
638 struct patch_ids ids;
639 struct commit *commit;
640 struct commit_list *list = NULL;
641 const char *upstream;
642 const char *head = "HEAD";
643 const char *limit = NULL;
644 int verbose = 0;
646 if (argc > 1 && !strcmp(argv[1], "-v")) {
647 verbose = 1;
648 argc--;
649 argv++;
652 switch (argc) {
653 case 4:
654 limit = argv[3];
655 /* FALLTHROUGH */
656 case 3:
657 head = argv[2];
658 /* FALLTHROUGH */
659 case 2:
660 upstream = argv[1];
661 break;
662 default:
663 usage(cherry_usage);
666 init_revisions(&revs, prefix);
667 revs.diff = 1;
668 revs.combine_merges = 0;
669 revs.ignore_merges = 1;
670 revs.diffopt.recursive = 1;
672 if (add_pending_commit(head, &revs, 0))
673 die("Unknown commit %s", head);
674 if (add_pending_commit(upstream, &revs, UNINTERESTING))
675 die("Unknown commit %s", upstream);
677 /* Don't say anything if head and upstream are the same. */
678 if (revs.pending.nr == 2) {
679 struct object_array_entry *o = revs.pending.objects;
680 if (hashcmp(o[0].item->sha1, o[1].item->sha1) == 0)
681 return 0;
684 get_patch_ids(&revs, &ids, prefix);
686 if (limit && add_pending_commit(limit, &revs, UNINTERESTING))
687 die("Unknown commit %s", limit);
689 /* reverse the list of commits */
690 prepare_revision_walk(&revs);
691 while ((commit = get_revision(&revs)) != NULL) {
692 /* ignore merges */
693 if (commit->parents && commit->parents->next)
694 continue;
696 commit_list_insert(commit, &list);
699 while (list) {
700 char sign = '+';
702 commit = list->item;
703 if (has_commit_patch_id(commit, &ids))
704 sign = '-';
706 if (verbose) {
707 static char buf[16384];
708 pretty_print_commit(CMIT_FMT_ONELINE, commit, ~0,
709 buf, sizeof(buf), 0, NULL, NULL, 0);
710 printf("%c %s %s\n", sign,
711 sha1_to_hex(commit->object.sha1), buf);
713 else {
714 printf("%c %s\n", sign,
715 sha1_to_hex(commit->object.sha1));
718 list = list->next;
721 free_patch_ids(&ids);
722 return 0;