git-format-patch -3
[git/gitweb.git] / builtin-log.c
blob9453e6d2db672d593195e199a10cc6ce1bf34db2
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"
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,
21 struct rev_info *rev)
23 int i;
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)) {
35 arg += 11;
36 if (strcmp(arg, "none"))
37 git_log_output_encoding = strdup(arg);
38 else
39 git_log_output_encoding = "";
41 else
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);
53 free(commit->buffer);
54 commit->buffer = NULL;
55 free_commit_list(commit->parents);
56 commit->parents = NULL;
58 return 0;
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);
65 return 0;
67 return git_diff_ui_config(var, value);
70 int cmd_whatchanged(int argc, const char **argv, const char *prefix)
72 struct rev_info rev;
74 git_config(git_log_config);
75 init_revisions(&rev, prefix);
76 rev.diff = 1;
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)
87 unsigned long size;
88 char type[20];
89 char *buf = read_sha1_file(sha1, type, &size);
90 int offset = 0;
92 if (!buf)
93 return error("Could not read object %s", sha1_to_hex(sha1));
95 if (suppress_header)
96 while (offset < size && buf[offset++] != '\n') {
97 int new_offset = offset;
98 while (new_offset < size && buf[new_offset++] != '\n')
99 ; /* do nothing */
100 offset = new_offset;
103 if (offset < size)
104 fwrite(buf + offset, size - offset, 1, stdout);
105 free(buf);
106 return 0;
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) ? "/" : "");
114 return 0;
117 int cmd_show(int argc, const char **argv, const char *prefix)
119 struct rev_info rev;
120 struct object_array_entry *objects;
121 int i, count, ret = 0;
123 git_config(git_log_config);
124 init_revisions(&rev, prefix);
125 rev.diff = 1;
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;
131 rev.no_walk = 1;
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;
139 switch (o->type) {
140 case OBJ_BLOB:
141 ret = show_object(o->sha1, 0);
142 break;
143 case OBJ_TAG: {
144 struct tag *t = (struct tag *)o;
146 printf("%stag %s%s\n\n",
147 diff_get_color(rev.diffopt.color_diff,
148 DIFF_COMMIT),
149 t->tag,
150 diff_get_color(rev.diffopt.color_diff,
151 DIFF_RESET));
152 ret = show_object(o->sha1, 1);
153 objects[i].item = (struct object *)t->tagged;
154 i--;
155 break;
157 case OBJ_TREE:
158 printf("%stree %s%s\n\n",
159 diff_get_color(rev.diffopt.color_diff,
160 DIFF_COMMIT),
161 name,
162 diff_get_color(rev.diffopt.color_diff,
163 DIFF_RESET));
164 read_tree_recursive((struct tree *)o, "", 0, 0, NULL,
165 show_tree_object);
166 break;
167 case OBJ_COMMIT:
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);
172 break;
173 default:
174 ret = error("Unknown type: %d", o->type);
177 free(objects);
178 return ret;
181 int cmd_log(int argc, const char **argv, const char *prefix)
183 struct rev_info rev;
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 = ".txt";
202 static int git_format_config(const char *var, const char *value)
204 if (!strcmp(var, "format.headers")) {
205 int len;
207 if (!value)
208 die("format.headers without value");
209 len = strlen(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);
214 return 0;
216 if (!strcmp(var, "format.suffix")) {
217 if (!value)
218 die("format.suffix without value");
219 fmt_patch_suffix = xstrdup(value);
220 return 0;
222 if (!strcmp(var, "diff.color") || !strcmp(var, "color.diff")) {
223 return 0;
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)
234 char filename[1024];
235 char *sol;
236 int len = 0;
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");
250 if (sol) {
251 int j, space = 1;
253 sol += 2;
254 /* strip [PATCH] or [PATCH blabla] */
255 if (!keep_subject && !strncmp(sol, "[PATCH", 6)) {
256 char *eos = strchr(sol + 6, ']');
257 if (eos) {
258 while (isspace(*eos))
259 eos++;
260 sol = eos;
264 for (j = 0;
265 len < sizeof(filename) - suffix_len &&
266 sol[j] && sol[j] != '\n';
267 j++) {
268 if (istitlechar(sol[j])) {
269 if (space) {
270 filename[len++] = '-';
271 space = 0;
273 filename[len++] = sol[j];
274 if (sol[j] == '.')
275 while (sol[j + 1] == '.')
276 j++;
277 } else
278 space = 1;
280 while (filename[len - 1] == '.' || filename[len - 1] == '-')
281 len--;
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,
289 unsigned char *sha1)
291 if (commit->parents)
292 diff_tree_sha1(commit->parents->item->object.sha1,
293 commit->object.sha1, "", options);
294 else
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;
312 flags1 = o1->flags;
313 o2 = rev->pending.objects[1].item;
314 flags2 = o2->flags;
316 if ((flags1 & UNINTERESTING) == (flags2 & UNINTERESTING))
317 die("Not a range.");
319 diff_setup(options);
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) {
333 /* ignore merges */
334 if (commit->parents && commit->parents->next)
335 continue;
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);
346 o1->flags = flags1;
347 o2->flags = flags2;
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;
366 struct rev_info rev;
367 int nr = 0, total, i, j;
368 int use_stdout = 0;
369 int numbered = 0;
370 int start_number = -1;
371 int keep_subject = 0;
372 int ignore_if_in_upstream = 0;
373 int thread = 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];
380 setup_ident();
381 git_config(git_format_config);
382 init_revisions(&rev, prefix);
383 rev.commit_format = CMIT_FMT_EMAIL;
384 rev.verbose_header = 1;
385 rev.diff = 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"))
400 use_stdout = 1;
401 else if (!strcmp(argv[i], "-n") ||
402 !strcmp(argv[i], "--numbered"))
403 numbered = 1;
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")) {
407 i++;
408 if (i == argc)
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")) {
414 keep_subject = 1;
415 rev.total = -1;
417 else if (!strcmp(argv[i], "--output-directory") ||
418 !strcmp(argv[i], "-o")) {
419 i++;
420 if (argc <= i)
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;
429 const char *endpos;
430 committer = git_committer_info(1);
431 endpos = strchr(committer, '>');
432 if (!endpos)
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"))
445 thread = 1;
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")) {
449 i++;
450 if (i == argc)
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;
456 else
457 argv[j++] = argv[i];
459 argc = j;
461 if (start_number < 0)
462 start_number = 1;
463 if (numbered && keep_subject)
464 die ("-n and -k are mutually exclusive.");
466 argc = setup_revisions(argc, argv, &rev, "HEAD");
467 if (argc > 1)
468 die ("unrecognized argument: %s", argv[1]);
470 if (!rev.diffopt.output_format)
471 rev.diffopt.output_format = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_PATCH;
473 if (!output_directory)
474 output_directory = prefix;
476 if (output_directory) {
477 if (use_stdout)
478 die("standard output, or directory, which one?");
479 if (mkdir(output_directory, 0777) < 0 && errno != EEXIST)
480 die("Could not create directory %s",
481 output_directory);
484 if (rev.pending.nr == 1) {
485 if (rev.max_count < 0) {
486 rev.pending.objects[0].item->flags |= UNINTERESTING;
487 add_head(&rev);
489 /* Otherwise, it is "format-patch -22 HEAD", and
490 * get_revision() would return only the specified count.
494 if (ignore_if_in_upstream)
495 get_patch_ids(&rev, &patch_id_opts, prefix);
497 if (!use_stdout)
498 realstdout = fdopen(dup(1), "w");
500 prepare_revision_walk(&rev);
501 while ((commit = get_revision(&rev)) != NULL) {
502 unsigned char sha1[20];
504 /* ignore merges */
505 if (commit->parents && commit->parents->next)
506 continue;
508 if (ignore_if_in_upstream &&
509 !get_patch_id(commit, &patch_id_opts, sha1) &&
510 lookup_object(sha1))
511 continue;
513 nr++;
514 list = xrealloc(list, nr * sizeof(list[0]));
515 list[nr - 1] = commit;
517 total = nr;
518 if (numbered)
519 rev.total = total + start_number - 1;
520 rev.add_signoff = add_signoff;
521 rev.ref_message_id = in_reply_to;
522 while (0 <= --nr) {
523 int shown;
524 commit = list[nr];
525 rev.nr = total - nr + (start_number - 1);
526 /* Make the second and subsequent mails replies to the first */
527 if (thread) {
528 if (nr == (total - 2)) {
529 strncpy(ref_message_id, message_id,
530 sizeof(ref_message_id));
531 ref_message_id[sizeof(ref_message_id)-1]='\0';
532 rev.ref_message_id = ref_message_id;
534 gen_message_id(message_id, sizeof(message_id),
535 sha1_to_hex(commit->object.sha1));
536 rev.message_id = message_id;
538 if (!use_stdout)
539 reopen_stdout(commit, rev.nr, keep_subject);
540 shown = log_tree_commit(&rev, commit);
541 free(commit->buffer);
542 commit->buffer = NULL;
544 /* We put one extra blank line between formatted
545 * patches and this flag is used by log-tree code
546 * to see if it needs to emit a LF before showing
547 * the log; when using one file per patch, we do
548 * not want the extra blank line.
550 if (!use_stdout)
551 rev.shown_one = 0;
552 if (shown) {
553 if (rev.mime_boundary)
554 printf("\n--%s%s--\n\n\n",
555 mime_boundary_leader,
556 rev.mime_boundary);
557 else
558 printf("-- \n%s\n\n", git_version_string);
560 if (!use_stdout)
561 fclose(stdout);
563 free(list);
564 return 0;
567 static int add_pending_commit(const char *arg, struct rev_info *revs, int flags)
569 unsigned char sha1[20];
570 if (get_sha1(arg, sha1) == 0) {
571 struct commit *commit = lookup_commit_reference(sha1);
572 if (commit) {
573 commit->object.flags |= flags;
574 add_pending_object(revs, &commit->object, arg);
575 return 0;
578 return -1;
581 static const char cherry_usage[] =
582 "git-cherry [-v] <upstream> [<head>] [<limit>]";
583 int cmd_cherry(int argc, const char **argv, const char *prefix)
585 struct rev_info revs;
586 struct diff_options patch_id_opts;
587 struct commit *commit;
588 struct commit_list *list = NULL;
589 const char *upstream;
590 const char *head = "HEAD";
591 const char *limit = NULL;
592 int verbose = 0;
594 if (argc > 1 && !strcmp(argv[1], "-v")) {
595 verbose = 1;
596 argc--;
597 argv++;
600 switch (argc) {
601 case 4:
602 limit = argv[3];
603 /* FALLTHROUGH */
604 case 3:
605 head = argv[2];
606 /* FALLTHROUGH */
607 case 2:
608 upstream = argv[1];
609 break;
610 default:
611 usage(cherry_usage);
614 init_revisions(&revs, prefix);
615 revs.diff = 1;
616 revs.combine_merges = 0;
617 revs.ignore_merges = 1;
618 revs.diffopt.recursive = 1;
620 if (add_pending_commit(head, &revs, 0))
621 die("Unknown commit %s", head);
622 if (add_pending_commit(upstream, &revs, UNINTERESTING))
623 die("Unknown commit %s", upstream);
625 /* Don't say anything if head and upstream are the same. */
626 if (revs.pending.nr == 2) {
627 struct object_array_entry *o = revs.pending.objects;
628 if (hashcmp(o[0].item->sha1, o[1].item->sha1) == 0)
629 return 0;
632 get_patch_ids(&revs, &patch_id_opts, prefix);
634 if (limit && add_pending_commit(limit, &revs, UNINTERESTING))
635 die("Unknown commit %s", limit);
637 /* reverse the list of commits */
638 prepare_revision_walk(&revs);
639 while ((commit = get_revision(&revs)) != NULL) {
640 /* ignore merges */
641 if (commit->parents && commit->parents->next)
642 continue;
644 commit_list_insert(commit, &list);
647 while (list) {
648 unsigned char sha1[20];
649 char sign = '+';
651 commit = list->item;
652 if (!get_patch_id(commit, &patch_id_opts, sha1) &&
653 lookup_object(sha1))
654 sign = '-';
656 if (verbose) {
657 static char buf[16384];
658 pretty_print_commit(CMIT_FMT_ONELINE, commit, ~0,
659 buf, sizeof(buf), 0, NULL, NULL, 0);
660 printf("%c %s %s\n", sign,
661 sha1_to_hex(commit->object.sha1), buf);
663 else {
664 printf("%c %s\n", sign,
665 sha1_to_hex(commit->object.sha1));
668 list = list->next;
671 return 0;