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