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