$EMAIL is a last resort fallback, as it's system-wide.
[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"
cf39f54e 14#include "reflog-walk.h"
5d23e133 15#include "patch-ids.h"
ca135e7a 16#include "refs.h"
70827b15 17
0f03ca94
PB
18static int default_show_root = 1;
19
e686eb98
JS
20/* this is in builtin-diff.c */
21void add_head(struct rev_info *revs);
22
ca135e7a
LT
23static void add_name_decoration(const char *prefix, const char *name, struct object *obj)
24{
25 int plen = strlen(prefix);
26 int nlen = strlen(name);
27 struct name_decoration *res = xmalloc(sizeof(struct name_decoration) + plen + nlen);
28 memcpy(res->name, prefix, plen);
29 memcpy(res->name + plen, name, nlen + 1);
30 res->next = add_decoration(&name_decoration, obj, res);
31}
32
33static int add_ref_decoration(const char *refname, const unsigned char *sha1, int flags, void *cb_data)
34{
35 struct object *obj = parse_object(sha1);
36 if (!obj)
37 return 0;
38 add_name_decoration("", refname, obj);
39 while (obj->type == OBJ_TAG) {
40 obj = ((struct tag *)obj)->tagged;
41 if (!obj)
42 break;
43 add_name_decoration("tag: ", refname, obj);
44 }
45 return 0;
46}
47
a633fca0 48static void cmd_log_init(int argc, const char **argv, const char *prefix,
70827b15
LT
49 struct rev_info *rev)
50{
52883fbd 51 int i;
ca135e7a 52 int decorate = 0;
52883fbd 53
70827b15
LT
54 rev->abbrev = DEFAULT_ABBREV;
55 rev->commit_format = CMIT_FMT_DEFAULT;
56 rev->verbose_header = 1;
0f03ca94 57 rev->show_root_diff = default_show_root;
70827b15 58 argc = setup_revisions(argc, argv, rev, "HEAD");
9dafea26
TH
59 if (rev->diffopt.pickaxe || rev->diffopt.filter)
60 rev->always_show_header = 0;
52883fbd
JH
61 for (i = 1; i < argc; i++) {
62 const char *arg = argv[i];
cc44c765 63 if (!prefixcmp(arg, "--encoding=")) {
52883fbd 64 arg += 11;
52883fbd 65 if (strcmp(arg, "none"))
567fb65e 66 git_log_output_encoding = xstrdup(arg);
52883fbd 67 else
d2c11a38 68 git_log_output_encoding = "";
ca135e7a
LT
69 } else if (!strcmp(arg, "--decorate")) {
70 if (!decorate)
71 for_each_ref(add_ref_decoration, NULL);
72 decorate = 1;
73 } else
52883fbd
JH
74 die("unrecognized argument: %s", arg);
75 }
9dafea26
TH
76}
77
78static int cmd_log_walk(struct rev_info *rev)
79{
80 struct commit *commit;
70827b15
LT
81
82 prepare_revision_walk(rev);
70827b15
LT
83 while ((commit = get_revision(rev)) != NULL) {
84 log_tree_commit(rev, commit);
a6c73064
JS
85 if (!rev->reflog_info) {
86 /* we allow cycles in reflog ancestry */
87 free(commit->buffer);
88 commit->buffer = NULL;
89 }
cb115748
LT
90 free_commit_list(commit->parents);
91 commit->parents = NULL;
70827b15
LT
92 }
93 return 0;
94}
95
0f03ca94
PB
96static int git_log_config(const char *var, const char *value)
97{
98 if (!strcmp(var, "log.showroot")) {
99 default_show_root = git_config_bool(var, value);
100 return 0;
101 }
102 return git_diff_ui_config(var, value);
103}
104
a633fca0 105int cmd_whatchanged(int argc, const char **argv, const char *prefix)
70827b15
LT
106{
107 struct rev_info rev;
108
0f03ca94 109 git_config(git_log_config);
db6296a5 110 init_revisions(&rev, prefix);
70827b15
LT
111 rev.diff = 1;
112 rev.diffopt.recursive = 1;
9202434c 113 rev.simplify_history = 0;
a633fca0 114 cmd_log_init(argc, argv, prefix, &rev);
9dafea26
TH
115 if (!rev.diffopt.output_format)
116 rev.diffopt.output_format = DIFF_FORMAT_RAW;
117 return cmd_log_walk(&rev);
70827b15
LT
118}
119
5d7eeee2
JS
120static int show_object(const unsigned char *sha1, int suppress_header)
121{
122 unsigned long size;
21666f1a
NP
123 enum object_type type;
124 char *buf = read_sha1_file(sha1, &type, &size);
5d7eeee2
JS
125 int offset = 0;
126
127 if (!buf)
128 return error("Could not read object %s", sha1_to_hex(sha1));
129
130 if (suppress_header)
131 while (offset < size && buf[offset++] != '\n') {
132 int new_offset = offset;
133 while (new_offset < size && buf[new_offset++] != '\n')
134 ; /* do nothing */
135 offset = new_offset;
136 }
137
138 if (offset < size)
139 fwrite(buf + offset, size - offset, 1, stdout);
140 free(buf);
141 return 0;
142}
143
144static int show_tree_object(const unsigned char *sha1,
145 const char *base, int baselen,
146 const char *pathname, unsigned mode, int stage)
147{
148 printf("%s%s\n", pathname, S_ISDIR(mode) ? "/" : "");
149 return 0;
150}
151
a633fca0 152int cmd_show(int argc, const char **argv, const char *prefix)
70827b15
LT
153{
154 struct rev_info rev;
5d7eeee2
JS
155 struct object_array_entry *objects;
156 int i, count, ret = 0;
70827b15 157
0f03ca94 158 git_config(git_log_config);
db6296a5 159 init_revisions(&rev, prefix);
70827b15
LT
160 rev.diff = 1;
161 rev.diffopt.recursive = 1;
162 rev.combine_merges = 1;
163 rev.dense_combined_merges = 1;
164 rev.always_show_header = 1;
165 rev.ignore_merges = 0;
166 rev.no_walk = 1;
a633fca0 167 cmd_log_init(argc, argv, prefix, &rev);
5d7eeee2
JS
168
169 count = rev.pending.nr;
170 objects = rev.pending.objects;
171 for (i = 0; i < count && !ret; i++) {
172 struct object *o = objects[i].item;
173 const char *name = objects[i].name;
174 switch (o->type) {
175 case OBJ_BLOB:
176 ret = show_object(o->sha1, 0);
177 break;
178 case OBJ_TAG: {
179 struct tag *t = (struct tag *)o;
180
181 printf("%stag %s%s\n\n",
182 diff_get_color(rev.diffopt.color_diff,
183 DIFF_COMMIT),
184 t->tag,
185 diff_get_color(rev.diffopt.color_diff,
186 DIFF_RESET));
187 ret = show_object(o->sha1, 1);
188 objects[i].item = (struct object *)t->tagged;
189 i--;
190 break;
191 }
192 case OBJ_TREE:
193 printf("%stree %s%s\n\n",
194 diff_get_color(rev.diffopt.color_diff,
195 DIFF_COMMIT),
196 name,
197 diff_get_color(rev.diffopt.color_diff,
198 DIFF_RESET));
199 read_tree_recursive((struct tree *)o, "", 0, 0, NULL,
200 show_tree_object);
201 break;
202 case OBJ_COMMIT:
203 rev.pending.nr = rev.pending.alloc = 0;
204 rev.pending.objects = NULL;
205 add_object_array(o, name, &rev.pending);
206 ret = cmd_log_walk(&rev);
207 break;
208 default:
209 ret = error("Unknown type: %d", o->type);
210 }
211 }
212 free(objects);
213 return ret;
70827b15
LT
214}
215
cf39f54e
LT
216/*
217 * This is equivalent to "git log -g --abbrev-commit --pretty=oneline"
218 */
219int cmd_log_reflog(int argc, const char **argv, const char *prefix)
220{
221 struct rev_info rev;
222
223 git_config(git_log_config);
224 init_revisions(&rev, prefix);
225 init_reflog_walk(&rev.reflog_info);
226 rev.abbrev_commit = 1;
227 rev.verbose_header = 1;
228 cmd_log_init(argc, argv, prefix, &rev);
229
230 /*
231 * This means that we override whatever commit format the user gave
232 * on the cmd line. Sad, but cmd_log_init() currently doesn't
233 * allow us to set a different default.
234 */
235 rev.commit_format = CMIT_FMT_ONELINE;
236 rev.always_show_header = 1;
237
238 /*
239 * We get called through "git reflog", so unlike the other log
240 * routines, we need to set up our pager manually..
241 */
242 setup_pager();
243
244 return cmd_log_walk(&rev);
245}
246
a633fca0 247int cmd_log(int argc, const char **argv, const char *prefix)
70827b15
LT
248{
249 struct rev_info rev;
250
0f03ca94 251 git_config(git_log_config);
db6296a5 252 init_revisions(&rev, prefix);
70827b15 253 rev.always_show_header = 1;
a633fca0 254 cmd_log_init(argc, argv, prefix, &rev);
9dafea26 255 return cmd_log_walk(&rev);
70827b15 256}
91efcf60 257
c06d2daa
RR
258/* format-patch */
259#define FORMAT_PATCH_NAME_MAX 64
260
0377db77
JS
261static int istitlechar(char c)
262{
263 return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
264 (c >= '0' && c <= '9') || c == '.' || c == '_';
265}
266
20ff0680
JS
267static char *extra_headers = NULL;
268static int extra_headers_size = 0;
917a8f89 269static const char *fmt_patch_suffix = ".patch";
20ff0680
JS
270
271static int git_format_config(const char *var, const char *value)
272{
273 if (!strcmp(var, "format.headers")) {
d7fb91c6
JH
274 int len;
275
276 if (!value)
277 die("format.headers without value");
278 len = strlen(value);
20ff0680 279 extra_headers_size += len + 1;
83572c1a 280 extra_headers = xrealloc(extra_headers, extra_headers_size);
20ff0680
JS
281 extra_headers[extra_headers_size - len - 1] = 0;
282 strcat(extra_headers, value);
283 return 0;
284 }
03eeaeae
JH
285 if (!strcmp(var, "format.suffix")) {
286 if (!value)
287 die("format.suffix without value");
288 fmt_patch_suffix = xstrdup(value);
289 return 0;
290 }
a159ca0c 291 if (!strcmp(var, "diff.color") || !strcmp(var, "color.diff")) {
f3aafa4d
RA
292 return 0;
293 }
0f03ca94 294 return git_log_config(var, value);
20ff0680
JS
295}
296
297
81f3a188 298static FILE *realstdout = NULL;
efd02016 299static const char *output_directory = NULL;
81f3a188 300
c06d2daa 301static int reopen_stdout(struct commit *commit, int nr, int keep_subject)
0377db77 302{
c06d2daa 303 char filename[PATH_MAX];
0377db77 304 char *sol;
2448482b 305 int len = 0;
c06d2daa 306 int suffix_len = strlen(fmt_patch_suffix) + 1;
0377db77 307
2448482b 308 if (output_directory) {
c06d2daa
RR
309 if (strlen(output_directory) >=
310 sizeof(filename) - FORMAT_PATCH_NAME_MAX - suffix_len)
311 return error("name of output directory is too long");
312 strlcpy(filename, output_directory, sizeof(filename) - suffix_len);
2448482b
JS
313 len = strlen(filename);
314 if (filename[len - 1] != '/')
315 filename[len++] = '/';
316 }
0377db77 317
2448482b 318 sprintf(filename + len, "%04d", nr);
0377db77
JS
319 len = strlen(filename);
320
321 sol = strstr(commit->buffer, "\n\n");
322 if (sol) {
323 int j, space = 1;
324
325 sol += 2;
326 /* strip [PATCH] or [PATCH blabla] */
cc44c765 327 if (!keep_subject && !prefixcmp(sol, "[PATCH")) {
0377db77
JS
328 char *eos = strchr(sol + 6, ']');
329 if (eos) {
330 while (isspace(*eos))
331 eos++;
332 sol = eos;
333 }
334 }
335
03eeaeae 336 for (j = 0;
c06d2daa
RR
337 j < FORMAT_PATCH_NAME_MAX - suffix_len - 5 &&
338 len < sizeof(filename) - suffix_len &&
03eeaeae
JH
339 sol[j] && sol[j] != '\n';
340 j++) {
0377db77
JS
341 if (istitlechar(sol[j])) {
342 if (space) {
343 filename[len++] = '-';
344 space = 0;
345 }
346 filename[len++] = sol[j];
347 if (sol[j] == '.')
348 while (sol[j + 1] == '.')
349 j++;
350 } else
351 space = 1;
352 }
353 while (filename[len - 1] == '.' || filename[len - 1] == '-')
354 len--;
c06d2daa 355 filename[len] = 0;
0377db77 356 }
c06d2daa
RR
357 if (len + suffix_len >= sizeof(filename))
358 return error("Patch pathname too long");
03eeaeae 359 strcpy(filename + len, fmt_patch_suffix);
81f3a188 360 fprintf(realstdout, "%s\n", filename);
c06d2daa
RR
361 if (freopen(filename, "w", stdout) == NULL)
362 return error("Cannot open patch file %s",filename);
363 return 0;
364
0377db77
JS
365}
366
5d23e133 367static void get_patch_ids(struct rev_info *rev, struct patch_ids *ids, const char *prefix)
9c6efa36
JS
368{
369 struct rev_info check_rev;
370 struct commit *commit;
371 struct object *o1, *o2;
372 unsigned flags1, flags2;
9c6efa36
JS
373
374 if (rev->pending.nr != 2)
375 die("Need exactly one range.");
376
377 o1 = rev->pending.objects[0].item;
378 flags1 = o1->flags;
379 o2 = rev->pending.objects[1].item;
380 flags2 = o2->flags;
381
382 if ((flags1 & UNINTERESTING) == (flags2 & UNINTERESTING))
383 die("Not a range.");
384
5d23e133 385 init_patch_ids(ids);
9c6efa36
JS
386
387 /* given a range a..b get all patch ids for b..a */
db6296a5 388 init_revisions(&check_rev, prefix);
9c6efa36
JS
389 o1->flags ^= UNINTERESTING;
390 o2->flags ^= UNINTERESTING;
391 add_pending_object(&check_rev, o1, "o1");
392 add_pending_object(&check_rev, o2, "o2");
393 prepare_revision_walk(&check_rev);
394
395 while ((commit = get_revision(&check_rev)) != NULL) {
396 /* ignore merges */
397 if (commit->parents && commit->parents->next)
398 continue;
399
5d23e133 400 add_commit_patch_id(commit, ids);
9c6efa36
JS
401 }
402
403 /* reset for next revision walk */
81db0941
JS
404 clear_commit_marks((struct commit *)o1,
405 SEEN | UNINTERESTING | SHOWN | ADDED);
406 clear_commit_marks((struct commit *)o2,
407 SEEN | UNINTERESTING | SHOWN | ADDED);
9c6efa36
JS
408 o1->flags = flags1;
409 o2->flags = flags2;
410}
411
d1566f78
JT
412static void gen_message_id(char *dest, unsigned int length, char *base)
413{
cb280e10 414 const char *committer = git_committer_info(-1);
d1566f78
JT
415 const char *email_start = strrchr(committer, '<');
416 const char *email_end = strrchr(committer, '>');
417 if(!email_start || !email_end || email_start > email_end - 1)
418 die("Could not extract email from committer identity.");
76af0734
JH
419 snprintf(dest, length, "%s.%lu.git.%.*s", base,
420 (unsigned long) time(NULL),
421 (int)(email_end - email_start - 1), email_start + 1);
d1566f78
JT
422}
423
a633fca0 424int cmd_format_patch(int argc, const char **argv, const char *prefix)
91efcf60
JH
425{
426 struct commit *commit;
427 struct commit **list = NULL;
428 struct rev_info rev;
2448482b 429 int nr = 0, total, i, j;
0377db77 430 int use_stdout = 0;
596524b3 431 int numbered = 0;
fa0f02df 432 int start_number = -1;
8ac80a57 433 int keep_subject = 0;
2d9e4a47 434 int subject_prefix = 0;
9c6efa36 435 int ignore_if_in_upstream = 0;
cc35de84 436 int thread = 0;
76af0734 437 const char *in_reply_to = NULL;
5d23e133 438 struct patch_ids ids;
cf2251b6 439 char *add_signoff = NULL;
d1566f78
JT
440 char message_id[1024];
441 char ref_message_id[1024];
91efcf60 442
97beb812 443 git_config(git_format_config);
db6296a5 444 init_revisions(&rev, prefix);
91efcf60
JH
445 rev.commit_format = CMIT_FMT_EMAIL;
446 rev.verbose_header = 1;
447 rev.diff = 1;
91efcf60
JH
448 rev.combine_merges = 0;
449 rev.ignore_merges = 1;
27e1b127
JH
450 rev.diffopt.msg_sep = "";
451 rev.diffopt.recursive = 1;
91efcf60 452
20ff0680
JS
453 rev.extra_headers = extra_headers;
454
2448482b
JS
455 /*
456 * Parse the arguments before setup_revisions(), or something
2dc189a3 457 * like "git format-patch -o a123 HEAD^.." may fail; a123 is
2448482b
JS
458 * possibly a valid SHA1.
459 */
460 for (i = 1, j = 1; i < argc; i++) {
461 if (!strcmp(argv[i], "--stdout"))
0377db77 462 use_stdout = 1;
596524b3
JS
463 else if (!strcmp(argv[i], "-n") ||
464 !strcmp(argv[i], "--numbered"))
465 numbered = 1;
cc44c765 466 else if (!prefixcmp(argv[i], "--start-number="))
fa0f02df
JS
467 start_number = strtol(argv[i] + 15, NULL, 10);
468 else if (!strcmp(argv[i], "--start-number")) {
469 i++;
470 if (i == argc)
471 die("Need a number for --start-number");
472 start_number = strtol(argv[i], NULL, 10);
cf2251b6
JH
473 }
474 else if (!strcmp(argv[i], "-k") ||
8ac80a57
JS
475 !strcmp(argv[i], "--keep-subject")) {
476 keep_subject = 1;
477 rev.total = -1;
cf2251b6 478 }
efd02016
JH
479 else if (!strcmp(argv[i], "--output-directory") ||
480 !strcmp(argv[i], "-o")) {
2448482b 481 i++;
efd02016
JH
482 if (argc <= i)
483 die("Which directory?");
484 if (output_directory)
485 die("Two output directories?");
486 output_directory = argv[i];
698ce6f8 487 }
cf2251b6
JH
488 else if (!strcmp(argv[i], "--signoff") ||
489 !strcmp(argv[i], "-s")) {
6c4cca1c
EB
490 const char *committer;
491 const char *endpos;
6c4cca1c
EB
492 committer = git_committer_info(1);
493 endpos = strchr(committer, '>');
cf2251b6
JH
494 if (!endpos)
495 die("bogos committer info %s\n", committer);
496 add_signoff = xmalloc(endpos - committer + 2);
497 memcpy(add_signoff, committer, endpos - committer + 1);
498 add_signoff[endpos - committer + 1] = 0;
499 }
c112f689 500 else if (!strcmp(argv[i], "--attach")) {
698ce6f8 501 rev.mime_boundary = git_version_string;
c112f689
JS
502 rev.no_inline = 1;
503 }
504 else if (!prefixcmp(argv[i], "--attach=")) {
505 rev.mime_boundary = argv[i] + 9;
506 rev.no_inline = 1;
507 }
508 else if (!strcmp(argv[i], "--inline")) {
509 rev.mime_boundary = git_version_string;
510 rev.no_inline = 0;
511 }
512 else if (!prefixcmp(argv[i], "--inline=")) {
698ce6f8 513 rev.mime_boundary = argv[i] + 9;
c112f689
JS
514 rev.no_inline = 0;
515 }
9c6efa36
JS
516 else if (!strcmp(argv[i], "--ignore-if-in-upstream"))
517 ignore_if_in_upstream = 1;
cc35de84
JT
518 else if (!strcmp(argv[i], "--thread"))
519 thread = 1;
cc44c765 520 else if (!prefixcmp(argv[i], "--in-reply-to="))
da56645d
JT
521 in_reply_to = argv[i] + 14;
522 else if (!strcmp(argv[i], "--in-reply-to")) {
523 i++;
524 if (i == argc)
525 die("Need a Message-Id for --in-reply-to");
526 in_reply_to = argv[i];
2d9e4a47
RJ
527 } else if (!prefixcmp(argv[i], "--subject-prefix=")) {
528 subject_prefix = 1;
529 rev.subject_prefix = argv[i] + 17;
530 } else if (!prefixcmp(argv[i], "--suffix="))
03eeaeae 531 fmt_patch_suffix = argv[i] + 9;
698ce6f8 532 else
2448482b 533 argv[j++] = argv[i];
0377db77 534 }
2448482b
JS
535 argc = j;
536
add5c8a5 537 if (start_number < 0)
fa0f02df 538 start_number = 1;
63b398a4 539 if (numbered && keep_subject)
8ac80a57 540 die ("-n and -k are mutually exclusive.");
2d9e4a47
RJ
541 if (keep_subject && subject_prefix)
542 die ("--subject-prefix and -k are mutually exclusive.");
8ac80a57 543
2448482b
JS
544 argc = setup_revisions(argc, argv, &rev, "HEAD");
545 if (argc > 1)
546 die ("unrecognized argument: %s", argv[1]);
0377db77 547
c9b5ef99 548 if (!rev.diffopt.output_format)
c261e438 549 rev.diffopt.output_format = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_SUMMARY | DIFF_FORMAT_PATCH;
c9b5ef99 550
e47f306d
JH
551 if (!rev.diffopt.text)
552 rev.diffopt.binary = 1;
553
90f70a91 554 if (!output_directory && !use_stdout)
77e565d8
ML
555 output_directory = prefix;
556
efd02016
JH
557 if (output_directory) {
558 if (use_stdout)
559 die("standard output, or directory, which one?");
560 if (mkdir(output_directory, 0777) < 0 && errno != EEXIST)
561 die("Could not create directory %s",
562 output_directory);
563 }
564
1f1e895f 565 if (rev.pending.nr == 1) {
7c496280
JH
566 if (rev.max_count < 0) {
567 rev.pending.objects[0].item->flags |= UNINTERESTING;
568 add_head(&rev);
569 }
570 /* Otherwise, it is "format-patch -22 HEAD", and
571 * get_revision() would return only the specified count.
572 */
e686eb98
JS
573 }
574
9c6efa36 575 if (ignore_if_in_upstream)
5d23e133 576 get_patch_ids(&rev, &ids, prefix);
9c6efa36 577
81f3a188
JS
578 if (!use_stdout)
579 realstdout = fdopen(dup(1), "w");
580
91efcf60
JH
581 prepare_revision_walk(&rev);
582 while ((commit = get_revision(&rev)) != NULL) {
0377db77
JS
583 /* ignore merges */
584 if (commit->parents && commit->parents->next)
585 continue;
9c6efa36
JS
586
587 if (ignore_if_in_upstream &&
5d23e133 588 has_commit_patch_id(commit, &ids))
9c6efa36
JS
589 continue;
590
91efcf60 591 nr++;
83572c1a 592 list = xrealloc(list, nr * sizeof(list[0]));
91efcf60
JH
593 list[nr - 1] = commit;
594 }
0377db77 595 total = nr;
596524b3 596 if (numbered)
fa0f02df 597 rev.total = total + start_number - 1;
cf2251b6 598 rev.add_signoff = add_signoff;
da56645d 599 rev.ref_message_id = in_reply_to;
91efcf60
JH
600 while (0 <= --nr) {
601 int shown;
602 commit = list[nr];
add5c8a5 603 rev.nr = total - nr + (start_number - 1);
d1566f78 604 /* Make the second and subsequent mails replies to the first */
cc35de84
JT
605 if (thread) {
606 if (nr == (total - 2)) {
607 strncpy(ref_message_id, message_id,
608 sizeof(ref_message_id));
609 ref_message_id[sizeof(ref_message_id)-1]='\0';
610 rev.ref_message_id = ref_message_id;
611 }
612 gen_message_id(message_id, sizeof(message_id),
613 sha1_to_hex(commit->object.sha1));
614 rev.message_id = message_id;
d1566f78 615 }
0377db77 616 if (!use_stdout)
c06d2daa
RR
617 if (reopen_stdout(commit, rev.nr, keep_subject))
618 die("Failed to create output files");
91efcf60
JH
619 shown = log_tree_commit(&rev, commit);
620 free(commit->buffer);
621 commit->buffer = NULL;
add5c8a5
JH
622
623 /* We put one extra blank line between formatted
624 * patches and this flag is used by log-tree code
625 * to see if it needs to emit a LF before showing
626 * the log; when using one file per patch, we do
627 * not want the extra blank line.
628 */
629 if (!use_stdout)
630 rev.shown_one = 0;
698ce6f8
JS
631 if (shown) {
632 if (rev.mime_boundary)
633 printf("\n--%s%s--\n\n\n",
634 mime_boundary_leader,
635 rev.mime_boundary);
636 else
637 printf("-- \n%s\n\n", git_version_string);
638 }
0377db77
JS
639 if (!use_stdout)
640 fclose(stdout);
91efcf60
JH
641 }
642 free(list);
5d23e133
JH
643 if (ignore_if_in_upstream)
644 free_patch_ids(&ids);
91efcf60
JH
645 return 0;
646}
647
e827633a
RS
648static int add_pending_commit(const char *arg, struct rev_info *revs, int flags)
649{
650 unsigned char sha1[20];
651 if (get_sha1(arg, sha1) == 0) {
652 struct commit *commit = lookup_commit_reference(sha1);
653 if (commit) {
654 commit->object.flags |= flags;
655 add_pending_object(revs, &commit->object, arg);
656 return 0;
657 }
658 }
659 return -1;
660}
661
662static const char cherry_usage[] =
663"git-cherry [-v] <upstream> [<head>] [<limit>]";
664int cmd_cherry(int argc, const char **argv, const char *prefix)
665{
666 struct rev_info revs;
5d23e133 667 struct patch_ids ids;
e827633a
RS
668 struct commit *commit;
669 struct commit_list *list = NULL;
670 const char *upstream;
671 const char *head = "HEAD";
672 const char *limit = NULL;
673 int verbose = 0;
674
675 if (argc > 1 && !strcmp(argv[1], "-v")) {
676 verbose = 1;
677 argc--;
678 argv++;
679 }
680
681 switch (argc) {
682 case 4:
683 limit = argv[3];
684 /* FALLTHROUGH */
685 case 3:
686 head = argv[2];
687 /* FALLTHROUGH */
688 case 2:
689 upstream = argv[1];
690 break;
691 default:
692 usage(cherry_usage);
693 }
694
695 init_revisions(&revs, prefix);
696 revs.diff = 1;
697 revs.combine_merges = 0;
698 revs.ignore_merges = 1;
699 revs.diffopt.recursive = 1;
700
701 if (add_pending_commit(head, &revs, 0))
702 die("Unknown commit %s", head);
703 if (add_pending_commit(upstream, &revs, UNINTERESTING))
704 die("Unknown commit %s", upstream);
705
706 /* Don't say anything if head and upstream are the same. */
707 if (revs.pending.nr == 2) {
708 struct object_array_entry *o = revs.pending.objects;
709 if (hashcmp(o[0].item->sha1, o[1].item->sha1) == 0)
710 return 0;
711 }
712
5d23e133 713 get_patch_ids(&revs, &ids, prefix);
e827633a
RS
714
715 if (limit && add_pending_commit(limit, &revs, UNINTERESTING))
716 die("Unknown commit %s", limit);
717
718 /* reverse the list of commits */
719 prepare_revision_walk(&revs);
720 while ((commit = get_revision(&revs)) != NULL) {
721 /* ignore merges */
722 if (commit->parents && commit->parents->next)
723 continue;
724
725 commit_list_insert(commit, &list);
726 }
727
728 while (list) {
e827633a
RS
729 char sign = '+';
730
731 commit = list->item;
5d23e133 732 if (has_commit_patch_id(commit, &ids))
e827633a
RS
733 sign = '-';
734
735 if (verbose) {
736 static char buf[16384];
737 pretty_print_commit(CMIT_FMT_ONELINE, commit, ~0,
738 buf, sizeof(buf), 0, NULL, NULL, 0);
739 printf("%c %s %s\n", sign,
740 sha1_to_hex(commit->object.sha1), buf);
741 }
742 else {
743 printf("%c %s\n", sign,
744 sha1_to_hex(commit->object.sha1));
745 }
746
747 list = list->next;
748 }
749
5d23e133 750 free_patch_ids(&ids);
e827633a
RS
751 return 0;
752}