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