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