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