2 * Builtin "git log" and related commands (show, whatchanged)
4 * (C) Copyright 2006 Linus Torvalds
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,
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)) {
36 if (strcmp(arg, "none"))
37 git_log_output_encoding = strdup(arg);
39 git_log_output_encoding = "";
42 die("unrecognized argument: %s", arg);
46 static int cmd_log_walk(struct rev_info *rev)
48 struct commit *commit;
50 prepare_revision_walk(rev);
51 while ((commit = get_revision(rev)) != NULL) {
52 log_tree_commit(rev, commit);
54 commit->buffer = NULL;
55 free_commit_list(commit->parents);
56 commit->parents = NULL;
61 static int git_log_config(const char *var, const char *value)
63 if (!strcmp(var, "log.showroot")) {
64 default_show_root = git_config_bool(var, value);
67 return git_diff_ui_config(var, value);
70 int cmd_whatchanged(int argc, const char **argv, const char *prefix)
74 git_config(git_log_config);
75 init_revisions(&rev, prefix);
77 rev.diffopt.recursive = 1;
78 rev.simplify_history = 0;
79 cmd_log_init(argc, argv, prefix, &rev);
80 if (!rev.diffopt.output_format)
81 rev.diffopt.output_format = DIFF_FORMAT_RAW;
82 return cmd_log_walk(&rev);
85 static int show_object(const unsigned char *sha1, int suppress_header)
89 char *buf = read_sha1_file(sha1, type, &size);
93 return error("Could not read object %s", sha1_to_hex(sha1));
96 while (offset < size && buf[offset++] != '\n') {
97 int new_offset = offset;
98 while (new_offset < size && buf[new_offset++] != '\n')
104 fwrite(buf + offset, size - offset, 1, stdout);
109 static int show_tree_object(const unsigned char *sha1,
110 const char *base, int baselen,
111 const char *pathname, unsigned mode, int stage)
113 printf("%s%s\n", pathname, S_ISDIR(mode) ? "/" : "");
117 int cmd_show(int argc, const char **argv, const char *prefix)
120 struct object_array_entry *objects;
121 int i, count, ret = 0;
123 git_config(git_log_config);
124 init_revisions(&rev, prefix);
126 rev.diffopt.recursive = 1;
127 rev.combine_merges = 1;
128 rev.dense_combined_merges = 1;
129 rev.always_show_header = 1;
130 rev.ignore_merges = 0;
132 cmd_log_init(argc, argv, prefix, &rev);
134 count = rev.pending.nr;
135 objects = rev.pending.objects;
136 for (i = 0; i < count && !ret; i++) {
137 struct object *o = objects[i].item;
138 const char *name = objects[i].name;
141 ret = show_object(o->sha1, 0);
144 struct tag *t = (struct tag *)o;
146 printf("%stag %s%s\n\n",
147 diff_get_color(rev.diffopt.color_diff,
150 diff_get_color(rev.diffopt.color_diff,
152 ret = show_object(o->sha1, 1);
153 objects[i].item = (struct object *)t->tagged;
158 printf("%stree %s%s\n\n",
159 diff_get_color(rev.diffopt.color_diff,
162 diff_get_color(rev.diffopt.color_diff,
164 read_tree_recursive((struct tree *)o, "", 0, 0, NULL,
168 rev.pending.nr = rev.pending.alloc = 0;
169 rev.pending.objects = NULL;
170 add_object_array(o, name, &rev.pending);
171 ret = cmd_log_walk(&rev);
174 ret = error("Unknown type: %d", o->type);
181 int cmd_log(int argc, const char **argv, const char *prefix)
185 git_config(git_log_config);
186 init_revisions(&rev, prefix);
187 rev.always_show_header = 1;
188 cmd_log_init(argc, argv, prefix, &rev);
189 return cmd_log_walk(&rev);
192 static int istitlechar(char c)
194 return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
195 (c >= '0' && c <= '9') || c == '.' || c == '_';
198 static char *extra_headers = NULL;
199 static int extra_headers_size = 0;
200 static const char *fmt_patch_suffix = ".patch";
202 static int git_format_config(const char *var, const char *value)
204 if (!strcmp(var, "format.headers")) {
208 die("format.headers without value");
210 extra_headers_size += len + 1;
211 extra_headers = xrealloc(extra_headers, extra_headers_size);
212 extra_headers[extra_headers_size - len - 1] = 0;
213 strcat(extra_headers, value);
216 if (!strcmp(var, "format.suffix")) {
218 die("format.suffix without value");
219 fmt_patch_suffix = xstrdup(value);
222 if (!strcmp(var, "diff.color") || !strcmp(var, "color.diff")) {
225 return git_log_config(var, value);
229 static FILE *realstdout = NULL;
230 static const char *output_directory = NULL;
232 static void reopen_stdout(struct commit *commit, int nr, int keep_subject)
237 int suffix_len = strlen(fmt_patch_suffix) + 10; /* ., NUL and slop */
239 if (output_directory) {
240 strlcpy(filename, output_directory, 1000);
241 len = strlen(filename);
242 if (filename[len - 1] != '/')
243 filename[len++] = '/';
246 sprintf(filename + len, "%04d", nr);
247 len = strlen(filename);
249 sol = strstr(commit->buffer, "\n\n");
254 /* strip [PATCH] or [PATCH blabla] */
255 if (!keep_subject && !strncmp(sol, "[PATCH", 6)) {
256 char *eos = strchr(sol + 6, ']');
258 while (isspace(*eos))
265 len < sizeof(filename) - suffix_len &&
266 sol[j] && sol[j] != '\n';
268 if (istitlechar(sol[j])) {
270 filename[len++] = '-';
273 filename[len++] = sol[j];
275 while (sol[j + 1] == '.')
280 while (filename[len - 1] == '.' || filename[len - 1] == '-')
283 strcpy(filename + len, fmt_patch_suffix);
284 fprintf(realstdout, "%s\n", filename);
285 freopen(filename, "w", stdout);
288 static int get_patch_id(struct commit *commit, struct diff_options *options,
292 diff_tree_sha1(commit->parents->item->object.sha1,
293 commit->object.sha1, "", options);
295 diff_root_tree_sha1(commit->object.sha1, "", options);
296 diffcore_std(options);
297 return diff_flush_patch_id(options, sha1);
300 static void get_patch_ids(struct rev_info *rev, struct diff_options *options, const char *prefix)
302 struct rev_info check_rev;
303 struct commit *commit;
304 struct object *o1, *o2;
305 unsigned flags1, flags2;
306 unsigned char sha1[20];
308 if (rev->pending.nr != 2)
309 die("Need exactly one range.");
311 o1 = rev->pending.objects[0].item;
313 o2 = rev->pending.objects[1].item;
316 if ((flags1 & UNINTERESTING) == (flags2 & UNINTERESTING))
320 options->recursive = 1;
321 if (diff_setup_done(options) < 0)
322 die("diff_setup_done failed");
324 /* given a range a..b get all patch ids for b..a */
325 init_revisions(&check_rev, prefix);
326 o1->flags ^= UNINTERESTING;
327 o2->flags ^= UNINTERESTING;
328 add_pending_object(&check_rev, o1, "o1");
329 add_pending_object(&check_rev, o2, "o2");
330 prepare_revision_walk(&check_rev);
332 while ((commit = get_revision(&check_rev)) != NULL) {
334 if (commit->parents && commit->parents->next)
337 if (!get_patch_id(commit, options, sha1))
338 created_object(sha1, xcalloc(1, sizeof(struct object)));
341 /* reset for next revision walk */
342 clear_commit_marks((struct commit *)o1,
343 SEEN | UNINTERESTING | SHOWN | ADDED);
344 clear_commit_marks((struct commit *)o2,
345 SEEN | UNINTERESTING | SHOWN | ADDED);
350 static void gen_message_id(char *dest, unsigned int length, char *base)
352 const char *committer = git_committer_info(1);
353 const char *email_start = strrchr(committer, '<');
354 const char *email_end = strrchr(committer, '>');
355 if(!email_start || !email_end || email_start > email_end - 1)
356 die("Could not extract email from committer identity.");
357 snprintf(dest, length, "%s.%lu.git.%.*s", base,
358 (unsigned long) time(NULL),
359 (int)(email_end - email_start - 1), email_start + 1);
362 int cmd_format_patch(int argc, const char **argv, const char *prefix)
364 struct commit *commit;
365 struct commit **list = NULL;
367 int nr = 0, total, i, j;
370 int start_number = -1;
371 int keep_subject = 0;
372 int ignore_if_in_upstream = 0;
374 const char *in_reply_to = NULL;
375 struct diff_options patch_id_opts;
376 char *add_signoff = NULL;
377 char message_id[1024];
378 char ref_message_id[1024];
381 git_config(git_format_config);
382 init_revisions(&rev, prefix);
383 rev.commit_format = CMIT_FMT_EMAIL;
384 rev.verbose_header = 1;
386 rev.combine_merges = 0;
387 rev.ignore_merges = 1;
388 rev.diffopt.msg_sep = "";
389 rev.diffopt.recursive = 1;
391 rev.extra_headers = extra_headers;
394 * Parse the arguments before setup_revisions(), or something
395 * like "git fmt-patch -o a123 HEAD^.." may fail; a123 is
396 * possibly a valid SHA1.
398 for (i = 1, j = 1; i < argc; i++) {
399 if (!strcmp(argv[i], "--stdout"))
401 else if (!strcmp(argv[i], "-n") ||
402 !strcmp(argv[i], "--numbered"))
404 else if (!strncmp(argv[i], "--start-number=", 15))
405 start_number = strtol(argv[i] + 15, NULL, 10);
406 else if (!strcmp(argv[i], "--start-number")) {
409 die("Need a number for --start-number");
410 start_number = strtol(argv[i], NULL, 10);
412 else if (!strcmp(argv[i], "-k") ||
413 !strcmp(argv[i], "--keep-subject")) {
417 else if (!strcmp(argv[i], "--output-directory") ||
418 !strcmp(argv[i], "-o")) {
421 die("Which directory?");
422 if (output_directory)
423 die("Two output directories?");
424 output_directory = argv[i];
426 else if (!strcmp(argv[i], "--signoff") ||
427 !strcmp(argv[i], "-s")) {
428 const char *committer;
430 committer = git_committer_info(1);
431 endpos = strchr(committer, '>');
433 die("bogos committer info %s\n", committer);
434 add_signoff = xmalloc(endpos - committer + 2);
435 memcpy(add_signoff, committer, endpos - committer + 1);
436 add_signoff[endpos - committer + 1] = 0;
438 else if (!strcmp(argv[i], "--attach"))
439 rev.mime_boundary = git_version_string;
440 else if (!strncmp(argv[i], "--attach=", 9))
441 rev.mime_boundary = argv[i] + 9;
442 else if (!strcmp(argv[i], "--ignore-if-in-upstream"))
443 ignore_if_in_upstream = 1;
444 else if (!strcmp(argv[i], "--thread"))
446 else if (!strncmp(argv[i], "--in-reply-to=", 14))
447 in_reply_to = argv[i] + 14;
448 else if (!strcmp(argv[i], "--in-reply-to")) {
451 die("Need a Message-Id for --in-reply-to");
452 in_reply_to = argv[i];
454 else if (!strncmp(argv[i], "--suffix=", 9))
455 fmt_patch_suffix = argv[i] + 9;
461 if (start_number < 0)
463 if (numbered && keep_subject)
464 die ("-n and -k are mutually exclusive.");
466 argc = setup_revisions(argc, argv, &rev, "HEAD");
468 die ("unrecognized argument: %s", argv[1]);
470 if (!rev.diffopt.output_format)
471 rev.diffopt.output_format = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_SUMMARY | DIFF_FORMAT_PATCH;
473 if (!rev.diffopt.text)
474 rev.diffopt.binary = 1;
476 if (!output_directory)
477 output_directory = prefix;
479 if (output_directory) {
481 die("standard output, or directory, which one?");
482 if (mkdir(output_directory, 0777) < 0 && errno != EEXIST)
483 die("Could not create directory %s",
487 if (rev.pending.nr == 1) {
488 if (rev.max_count < 0) {
489 rev.pending.objects[0].item->flags |= UNINTERESTING;
492 /* Otherwise, it is "format-patch -22 HEAD", and
493 * get_revision() would return only the specified count.
497 if (ignore_if_in_upstream)
498 get_patch_ids(&rev, &patch_id_opts, prefix);
501 realstdout = fdopen(dup(1), "w");
503 prepare_revision_walk(&rev);
504 while ((commit = get_revision(&rev)) != NULL) {
505 unsigned char sha1[20];
508 if (commit->parents && commit->parents->next)
511 if (ignore_if_in_upstream &&
512 !get_patch_id(commit, &patch_id_opts, sha1) &&
517 list = xrealloc(list, nr * sizeof(list[0]));
518 list[nr - 1] = commit;
522 rev.total = total + start_number - 1;
523 rev.add_signoff = add_signoff;
524 rev.ref_message_id = in_reply_to;
528 rev.nr = total - nr + (start_number - 1);
529 /* Make the second and subsequent mails replies to the first */
531 if (nr == (total - 2)) {
532 strncpy(ref_message_id, message_id,
533 sizeof(ref_message_id));
534 ref_message_id[sizeof(ref_message_id)-1]='\0';
535 rev.ref_message_id = ref_message_id;
537 gen_message_id(message_id, sizeof(message_id),
538 sha1_to_hex(commit->object.sha1));
539 rev.message_id = message_id;
542 reopen_stdout(commit, rev.nr, keep_subject);
543 shown = log_tree_commit(&rev, commit);
544 free(commit->buffer);
545 commit->buffer = NULL;
547 /* We put one extra blank line between formatted
548 * patches and this flag is used by log-tree code
549 * to see if it needs to emit a LF before showing
550 * the log; when using one file per patch, we do
551 * not want the extra blank line.
556 if (rev.mime_boundary)
557 printf("\n--%s%s--\n\n\n",
558 mime_boundary_leader,
561 printf("-- \n%s\n\n", git_version_string);
570 static int add_pending_commit(const char *arg, struct rev_info *revs, int flags)
572 unsigned char sha1[20];
573 if (get_sha1(arg, sha1) == 0) {
574 struct commit *commit = lookup_commit_reference(sha1);
576 commit->object.flags |= flags;
577 add_pending_object(revs, &commit->object, arg);
584 static const char cherry_usage[] =
585 "git-cherry [-v] <upstream> [<head>] [<limit>]";
586 int cmd_cherry(int argc, const char **argv, const char *prefix)
588 struct rev_info revs;
589 struct diff_options patch_id_opts;
590 struct commit *commit;
591 struct commit_list *list = NULL;
592 const char *upstream;
593 const char *head = "HEAD";
594 const char *limit = NULL;
597 if (argc > 1 && !strcmp(argv[1], "-v")) {
617 init_revisions(&revs, prefix);
619 revs.combine_merges = 0;
620 revs.ignore_merges = 1;
621 revs.diffopt.recursive = 1;
623 if (add_pending_commit(head, &revs, 0))
624 die("Unknown commit %s", head);
625 if (add_pending_commit(upstream, &revs, UNINTERESTING))
626 die("Unknown commit %s", upstream);
628 /* Don't say anything if head and upstream are the same. */
629 if (revs.pending.nr == 2) {
630 struct object_array_entry *o = revs.pending.objects;
631 if (hashcmp(o[0].item->sha1, o[1].item->sha1) == 0)
635 get_patch_ids(&revs, &patch_id_opts, prefix);
637 if (limit && add_pending_commit(limit, &revs, UNINTERESTING))
638 die("Unknown commit %s", limit);
640 /* reverse the list of commits */
641 prepare_revision_walk(&revs);
642 while ((commit = get_revision(&revs)) != NULL) {
644 if (commit->parents && commit->parents->next)
647 commit_list_insert(commit, &list);
651 unsigned char sha1[20];
655 if (!get_patch_id(commit, &patch_id_opts, sha1) &&
660 static char buf[16384];
661 pretty_print_commit(CMIT_FMT_ONELINE, commit, ~0,
662 buf, sizeof(buf), 0, NULL, NULL, 0);
663 printf("%c %s %s\n", sign,
664 sha1_to_hex(commit->object.sha1), buf);
667 printf("%c %s\n", sign,
668 sha1_to_hex(commit->object.sha1));