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