format-patch: use a string_list for headers
[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"
6b2f2d98 8#include "color.h"
70827b15
LT
9#include "commit.h"
10#include "diff.h"
11#include "revision.h"
12#include "log-tree.h"
91efcf60 13#include "builtin.h"
5d7eeee2 14#include "tag.h"
cf39f54e 15#include "reflog-walk.h"
5d23e133 16#include "patch-ids.h"
a5a27c79 17#include "run-command.h"
2bda2cf4 18#include "shortlog.h"
f2968022 19#include "remote.h"
b079c50e 20#include "string-list.h"
fff02ee6 21#include "parse-options.h"
70827b15 22
dd0ffd5b
HO
23/* Set a default date-time format for git log ("log.date" config variable) */
24static const char *default_date_mode = NULL;
25
0f03ca94 26static int default_show_root = 1;
d54276f2 27static const char *fmt_patch_subject_prefix = "PATCH";
94c22a5e 28static const char *fmt_pretty;
0f03ca94 29
1c370ea4
MM
30static const char * const builtin_log_usage =
31 "git log [<options>] [<since>..<until>] [[--] <path>...]\n"
32 " or: git show [options] <object>...";
33
a633fca0 34static void cmd_log_init(int argc, const char **argv, const char *prefix,
70827b15
LT
35 struct rev_info *rev)
36{
52883fbd 37 int i;
33e7018c 38 int decoration_style = 0;
52883fbd 39
70827b15
LT
40 rev->abbrev = DEFAULT_ABBREV;
41 rev->commit_format = CMIT_FMT_DEFAULT;
94c22a5e 42 if (fmt_pretty)
4da45bef 43 get_commit_format(fmt_pretty, rev);
70827b15 44 rev->verbose_header = 1;
8f67f8ae 45 DIFF_OPT_SET(&rev->diffopt, RECURSIVE);
0f03ca94 46 rev->show_root_diff = default_show_root;
d54276f2 47 rev->subject_prefix = fmt_patch_subject_prefix;
5ec11af6 48 DIFF_OPT_SET(&rev->diffopt, ALLOW_TEXTCONV);
dd0ffd5b
HO
49
50 if (default_date_mode)
51 rev->date_mode = parse_date_format(default_date_mode);
52
99caeed0
JN
53 /*
54 * Check for -h before setup_revisions(), or "git log -h" will
55 * fail when run without a git directory.
56 */
57 if (argc == 2 && !strcmp(argv[1], "-h"))
58 usage(builtin_log_usage);
70827b15 59 argc = setup_revisions(argc, argv, rev, "HEAD");
dd0ffd5b 60
66b2ed09
JH
61 if (!rev->show_notes_given && !rev->pretty_given)
62 rev->show_notes = 1;
63
9dafea26
TH
64 if (rev->diffopt.pickaxe || rev->diffopt.filter)
65 rev->always_show_header = 0;
8f67f8ae 66 if (DIFF_OPT_TST(&rev->diffopt, FOLLOW_RENAMES)) {
750f7b66
LT
67 rev->always_show_header = 0;
68 if (rev->diffopt.nr_paths != 1)
69 usage("git logs can only follow renames on one pathname at a time");
70 }
52883fbd
JH
71 for (i = 1; i < argc; i++) {
72 const char *arg = argv[i];
66688334 73 if (!strcmp(arg, "--decorate")) {
33e7018c
LH
74 decoration_style = DECORATE_SHORT_REFS;
75 } else if (!prefixcmp(arg, "--decorate=")) {
76 const char *v = skip_prefix(arg, "--decorate=");
77 if (!strcmp(v, "full"))
78 decoration_style = DECORATE_FULL_REFS;
79 else if (!strcmp(v, "short"))
80 decoration_style = DECORATE_SHORT_REFS;
81 else
82 die("invalid --decorate option: %s", arg);
0f3a290b
LT
83 } else if (!strcmp(arg, "--source")) {
84 rev->show_source = 1;
1c370ea4
MM
85 } else if (!strcmp(arg, "-h")) {
86 usage(builtin_log_usage);
ca135e7a 87 } else
52883fbd
JH
88 die("unrecognized argument: %s", arg);
89 }
33e7018c
LH
90 if (decoration_style) {
91 rev->show_decorations = 1;
92 load_ref_decorations(decoration_style);
93 }
9dafea26
TH
94}
95
252a7c02
LT
96/*
97 * This gives a rough estimate for how many commits we
98 * will print out in the list.
99 */
100static int estimate_commit_count(struct rev_info *rev, struct commit_list *list)
101{
102 int n = 0;
103
104 while (list) {
105 struct commit *commit = list->item;
106 unsigned int flags = commit->object.flags;
252a7c02 107 list = list->next;
7dc0fe3b 108 if (!(flags & (TREESAME | UNINTERESTING)))
53b2c823 109 n++;
252a7c02
LT
110 }
111 return n;
112}
113
114static void show_early_header(struct rev_info *rev, const char *stage, int nr)
115{
116 if (rev->shown_one) {
117 rev->shown_one = 0;
118 if (rev->commit_format != CMIT_FMT_ONELINE)
119 putchar(rev->diffopt.line_termination);
120 }
121 printf("Final output: %d %s\n", nr, stage);
122}
123
2af202be 124static struct itimerval early_output_timer;
252a7c02 125
cdcefbc9
LT
126static void log_show_early(struct rev_info *revs, struct commit_list *list)
127{
128 int i = revs->early_output;
252a7c02 129 int show_header = 1;
cdcefbc9
LT
130
131 sort_in_topological_order(&list, revs->lifo);
132 while (list && i) {
133 struct commit *commit = list->item;
252a7c02
LT
134 switch (simplify_commit(revs, commit)) {
135 case commit_show:
136 if (show_header) {
137 int n = estimate_commit_count(revs, list);
138 show_early_header(revs, "incomplete", n);
139 show_header = 0;
140 }
141 log_tree_commit(revs, commit);
142 i--;
143 break;
144 case commit_ignore:
145 break;
146 case commit_error:
147 return;
148 }
cdcefbc9 149 list = list->next;
cdcefbc9 150 }
252a7c02
LT
151
152 /* Did we already get enough commits for the early output? */
153 if (!i)
154 return;
155
156 /*
157 * ..if no, then repeat it twice a second until we
158 * do.
159 *
160 * NOTE! We don't use "it_interval", because if the
161 * reader isn't listening, we want our output to be
162 * throttled by the writing, and not have the timer
163 * trigger every second even if we're blocked on a
164 * reader!
165 */
166 early_output_timer.it_value.tv_sec = 0;
167 early_output_timer.it_value.tv_usec = 500000;
168 setitimer(ITIMER_REAL, &early_output_timer, NULL);
cdcefbc9
LT
169}
170
171static void early_output(int signal)
172{
173 show_early_output = log_show_early;
174}
175
176static void setup_early_output(struct rev_info *rev)
177{
178 struct sigaction sa;
cdcefbc9
LT
179
180 /*
181 * Set up the signal handler, minimally intrusively:
182 * we only set a single volatile integer word (not
183 * using sigatomic_t - trying to avoid unnecessary
184 * system dependencies and headers), and using
185 * SA_RESTART.
186 */
187 memset(&sa, 0, sizeof(sa));
188 sa.sa_handler = early_output;
189 sigemptyset(&sa.sa_mask);
190 sa.sa_flags = SA_RESTART;
191 sigaction(SIGALRM, &sa, NULL);
192
193 /*
194 * If we can get the whole output in less than a
195 * tenth of a second, don't even bother doing the
196 * early-output thing..
197 *
198 * This is a one-time-only trigger.
199 */
252a7c02
LT
200 early_output_timer.it_value.tv_sec = 0;
201 early_output_timer.it_value.tv_usec = 100000;
202 setitimer(ITIMER_REAL, &early_output_timer, NULL);
cdcefbc9
LT
203}
204
205static void finish_early_output(struct rev_info *rev)
206{
252a7c02 207 int n = estimate_commit_count(rev, rev->commits);
cdcefbc9 208 signal(SIGALRM, SIG_IGN);
252a7c02 209 show_early_header(rev, "done", n);
cdcefbc9
LT
210}
211
9dafea26
TH
212static int cmd_log_walk(struct rev_info *rev)
213{
214 struct commit *commit;
70827b15 215
cdcefbc9
LT
216 if (rev->early_output)
217 setup_early_output(rev);
218
3d51e1b5
MK
219 if (prepare_revision_walk(rev))
220 die("revision walk setup failed");
cdcefbc9
LT
221
222 if (rev->early_output)
223 finish_early_output(rev);
224
036d17fe 225 /*
84102a33
PVM
226 * For --check and --exit-code, the exit code is based on CHECK_FAILED
227 * and HAS_CHANGES being accumulated in rev->diffopt, so be careful to
228 * retain that state information if replacing rev->diffopt in this loop
036d17fe 229 */
70827b15
LT
230 while ((commit = get_revision(rev)) != NULL) {
231 log_tree_commit(rev, commit);
a6c73064
JS
232 if (!rev->reflog_info) {
233 /* we allow cycles in reflog ancestry */
234 free(commit->buffer);
235 commit->buffer = NULL;
236 }
cb115748
LT
237 free_commit_list(commit->parents);
238 commit->parents = NULL;
70827b15 239 }
036d17fe
PVM
240 if (rev->diffopt.output_format & DIFF_FORMAT_CHECKDIFF &&
241 DIFF_OPT_TST(&rev->diffopt, CHECK_FAILED)) {
242 return 02;
243 }
84102a33 244 return diff_result_code(&rev->diffopt, 0);
70827b15
LT
245}
246
ef90d6d4 247static int git_log_config(const char *var, const char *value, void *cb)
0f03ca94 248{
94c22a5e
CR
249 if (!strcmp(var, "format.pretty"))
250 return git_config_string(&fmt_pretty, var, value);
70cff3ac
BH
251 if (!strcmp(var, "format.subjectprefix"))
252 return git_config_string(&fmt_patch_subject_prefix, var, value);
dd0ffd5b
HO
253 if (!strcmp(var, "log.date"))
254 return git_config_string(&default_date_mode, var, value);
0f03ca94
PB
255 if (!strcmp(var, "log.showroot")) {
256 default_show_root = git_config_bool(var, value);
257 return 0;
258 }
ef90d6d4 259 return git_diff_ui_config(var, value, cb);
0f03ca94
PB
260}
261
a633fca0 262int cmd_whatchanged(int argc, const char **argv, const char *prefix)
70827b15
LT
263{
264 struct rev_info rev;
265
ef90d6d4 266 git_config(git_log_config, NULL);
6b2f2d98
MK
267
268 if (diff_use_color_default == -1)
269 diff_use_color_default = git_use_color_default;
270
db6296a5 271 init_revisions(&rev, prefix);
70827b15 272 rev.diff = 1;
9202434c 273 rev.simplify_history = 0;
a633fca0 274 cmd_log_init(argc, argv, prefix, &rev);
9dafea26
TH
275 if (!rev.diffopt.output_format)
276 rev.diffopt.output_format = DIFF_FORMAT_RAW;
277 return cmd_log_walk(&rev);
70827b15
LT
278}
279
56122ed8
JS
280static void show_tagger(char *buf, int len, struct rev_info *rev)
281{
ea718e65 282 struct strbuf out = STRBUF_INIT;
56122ed8 283
ea718e65
JS
284 pp_user_info("Tagger", rev->commit_format, &out, buf, rev->date_mode,
285 git_log_output_encoding ?
286 git_log_output_encoding: git_commit_encoding);
ca4ca9ed 287 printf("%s", out.buf);
ea718e65 288 strbuf_release(&out);
56122ed8
JS
289}
290
291static int show_object(const unsigned char *sha1, int show_tag_object,
292 struct rev_info *rev)
5d7eeee2
JS
293{
294 unsigned long size;
21666f1a
NP
295 enum object_type type;
296 char *buf = read_sha1_file(sha1, &type, &size);
5d7eeee2
JS
297 int offset = 0;
298
299 if (!buf)
300 return error("Could not read object %s", sha1_to_hex(sha1));
301
56122ed8
JS
302 if (show_tag_object)
303 while (offset < size && buf[offset] != '\n') {
304 int new_offset = offset + 1;
5d7eeee2
JS
305 while (new_offset < size && buf[new_offset++] != '\n')
306 ; /* do nothing */
56122ed8
JS
307 if (!prefixcmp(buf + offset, "tagger "))
308 show_tagger(buf + offset + 7,
309 new_offset - offset - 7, rev);
5d7eeee2
JS
310 offset = new_offset;
311 }
312
313 if (offset < size)
314 fwrite(buf + offset, size - offset, 1, stdout);
315 free(buf);
316 return 0;
317}
318
319static int show_tree_object(const unsigned char *sha1,
320 const char *base, int baselen,
671f0707 321 const char *pathname, unsigned mode, int stage, void *context)
5d7eeee2
JS
322{
323 printf("%s%s\n", pathname, S_ISDIR(mode) ? "/" : "");
324 return 0;
325}
326
a633fca0 327int cmd_show(int argc, const char **argv, const char *prefix)
70827b15
LT
328{
329 struct rev_info rev;
5d7eeee2
JS
330 struct object_array_entry *objects;
331 int i, count, ret = 0;
70827b15 332
ef90d6d4 333 git_config(git_log_config, NULL);
6b2f2d98
MK
334
335 if (diff_use_color_default == -1)
336 diff_use_color_default = git_use_color_default;
337
db6296a5 338 init_revisions(&rev, prefix);
70827b15 339 rev.diff = 1;
70827b15
LT
340 rev.combine_merges = 1;
341 rev.dense_combined_merges = 1;
342 rev.always_show_header = 1;
343 rev.ignore_merges = 0;
344 rev.no_walk = 1;
a633fca0 345 cmd_log_init(argc, argv, prefix, &rev);
5d7eeee2
JS
346
347 count = rev.pending.nr;
348 objects = rev.pending.objects;
349 for (i = 0; i < count && !ret; i++) {
350 struct object *o = objects[i].item;
351 const char *name = objects[i].name;
352 switch (o->type) {
353 case OBJ_BLOB:
56122ed8 354 ret = show_object(o->sha1, 0, NULL);
5d7eeee2
JS
355 break;
356 case OBJ_TAG: {
357 struct tag *t = (struct tag *)o;
358
ae03ee64
JK
359 if (rev.shown_one)
360 putchar('\n');
56122ed8 361 printf("%stag %s%s\n",
8f67f8ae 362 diff_get_color_opt(&rev.diffopt, DIFF_COMMIT),
5d7eeee2 363 t->tag,
8f67f8ae 364 diff_get_color_opt(&rev.diffopt, DIFF_RESET));
56122ed8 365 ret = show_object(o->sha1, 1, &rev);
ae03ee64 366 rev.shown_one = 1;
d2dadfe8
JH
367 if (ret)
368 break;
369 o = parse_object(t->tagged->sha1);
370 if (!o)
371 ret = error("Could not read object %s",
372 sha1_to_hex(t->tagged->sha1));
373 objects[i].item = o;
5d7eeee2
JS
374 i--;
375 break;
376 }
377 case OBJ_TREE:
ae03ee64
JK
378 if (rev.shown_one)
379 putchar('\n');
5d7eeee2 380 printf("%stree %s%s\n\n",
8f67f8ae 381 diff_get_color_opt(&rev.diffopt, DIFF_COMMIT),
5d7eeee2 382 name,
8f67f8ae 383 diff_get_color_opt(&rev.diffopt, DIFF_RESET));
5d7eeee2 384 read_tree_recursive((struct tree *)o, "", 0, 0, NULL,
671f0707 385 show_tree_object, NULL);
ae03ee64 386 rev.shown_one = 1;
5d7eeee2
JS
387 break;
388 case OBJ_COMMIT:
389 rev.pending.nr = rev.pending.alloc = 0;
390 rev.pending.objects = NULL;
391 add_object_array(o, name, &rev.pending);
392 ret = cmd_log_walk(&rev);
393 break;
394 default:
395 ret = error("Unknown type: %d", o->type);
396 }
397 }
398 free(objects);
399 return ret;
70827b15
LT
400}
401
cf39f54e
LT
402/*
403 * This is equivalent to "git log -g --abbrev-commit --pretty=oneline"
404 */
405int cmd_log_reflog(int argc, const char **argv, const char *prefix)
406{
407 struct rev_info rev;
408
ef90d6d4 409 git_config(git_log_config, NULL);
6b2f2d98
MK
410
411 if (diff_use_color_default == -1)
412 diff_use_color_default = git_use_color_default;
413
cf39f54e
LT
414 init_revisions(&rev, prefix);
415 init_reflog_walk(&rev.reflog_info);
416 rev.abbrev_commit = 1;
417 rev.verbose_header = 1;
418 cmd_log_init(argc, argv, prefix, &rev);
419
420 /*
421 * This means that we override whatever commit format the user gave
422 * on the cmd line. Sad, but cmd_log_init() currently doesn't
423 * allow us to set a different default.
424 */
425 rev.commit_format = CMIT_FMT_ONELINE;
4da45bef 426 rev.use_terminator = 1;
cf39f54e
LT
427 rev.always_show_header = 1;
428
429 /*
430 * We get called through "git reflog", so unlike the other log
431 * routines, we need to set up our pager manually..
432 */
433 setup_pager();
434
435 return cmd_log_walk(&rev);
436}
437
a633fca0 438int cmd_log(int argc, const char **argv, const char *prefix)
70827b15
LT
439{
440 struct rev_info rev;
441
ef90d6d4 442 git_config(git_log_config, NULL);
6b2f2d98
MK
443
444 if (diff_use_color_default == -1)
445 diff_use_color_default = git_use_color_default;
446
db6296a5 447 init_revisions(&rev, prefix);
70827b15 448 rev.always_show_header = 1;
a633fca0 449 cmd_log_init(argc, argv, prefix, &rev);
9dafea26 450 return cmd_log_walk(&rev);
70827b15 451}
91efcf60 452
c06d2daa 453/* format-patch */
0377db77 454
917a8f89 455static const char *fmt_patch_suffix = ".patch";
49604a4d 456static int numbered = 0;
a567fdcb 457static int auto_number = 1;
20ff0680 458
0db5260b
JW
459static char *default_attach = NULL;
460
ca9e0a1b
SB
461static struct string_list extra_hdr;
462static struct string_list extra_to;
463static struct string_list extra_cc;
3ee79d9f
DB
464
465static void add_header(const char *value)
466{
ca9e0a1b 467 struct string_list_item *item;
3ee79d9f 468 int len = strlen(value);
c8c4450e 469 while (len && value[len - 1] == '\n')
3ee79d9f 470 len--;
ca9e0a1b 471
3ee79d9f 472 if (!strncasecmp(value, "to: ", 4)) {
ca9e0a1b
SB
473 item = string_list_append(value + 4, &extra_to);
474 len -= 4;
475 } else if (!strncasecmp(value, "cc: ", 4)) {
476 item = string_list_append(value + 4, &extra_cc);
477 len -= 4;
478 } else {
479 item = string_list_append(value, &extra_hdr);
3ee79d9f 480 }
ca9e0a1b
SB
481
482 item->string[len] = '\0';
3ee79d9f
DB
483}
484
30984ed2
TR
485#define THREAD_SHALLOW 1
486#define THREAD_DEEP 2
487static int thread = 0;
1d1876e9 488static int do_signoff = 0;
30984ed2 489
ef90d6d4 490static int git_format_config(const char *var, const char *value, void *cb)
20ff0680
JS
491{
492 if (!strcmp(var, "format.headers")) {
d7fb91c6
JH
493 if (!value)
494 die("format.headers without value");
3ee79d9f 495 add_header(value);
20ff0680
JS
496 return 0;
497 }
70cff3ac
BH
498 if (!strcmp(var, "format.suffix"))
499 return git_config_string(&fmt_patch_suffix, var, value);
ae6c098f
SD
500 if (!strcmp(var, "format.to")) {
501 if (!value)
502 return config_error_nonbool(var);
ca9e0a1b 503 string_list_append(value, &extra_to);
ae6c098f
SD
504 return 0;
505 }
fe8928e6
MV
506 if (!strcmp(var, "format.cc")) {
507 if (!value)
508 return config_error_nonbool(var);
ca9e0a1b 509 string_list_append(value, &extra_cc);
fe8928e6
MV
510 return 0;
511 }
a159ca0c 512 if (!strcmp(var, "diff.color") || !strcmp(var, "color.diff")) {
f3aafa4d
RA
513 return 0;
514 }
49604a4d 515 if (!strcmp(var, "format.numbered")) {
90f5c186 516 if (value && !strcasecmp(value, "auto")) {
49604a4d
BG
517 auto_number = 1;
518 return 0;
519 }
49604a4d 520 numbered = git_config_bool(var, value);
a567fdcb 521 auto_number = auto_number && numbered;
49604a4d
BG
522 return 0;
523 }
0db5260b
JW
524 if (!strcmp(var, "format.attach")) {
525 if (value && *value)
526 default_attach = xstrdup(value);
527 else
528 default_attach = xstrdup(git_version_string);
529 return 0;
530 }
30984ed2
TR
531 if (!strcmp(var, "format.thread")) {
532 if (value && !strcasecmp(value, "deep")) {
533 thread = THREAD_DEEP;
534 return 0;
535 }
536 if (value && !strcasecmp(value, "shallow")) {
537 thread = THREAD_SHALLOW;
538 return 0;
539 }
540 thread = git_config_bool(var, value) && THREAD_SHALLOW;
541 return 0;
542 }
1d1876e9
HV
543 if (!strcmp(var, "format.signoff")) {
544 do_signoff = git_config_bool(var, value);
545 return 0;
546 }
dbd21447 547
ef90d6d4 548 return git_log_config(var, value, cb);
20ff0680
JS
549}
550
81f3a188 551static FILE *realstdout = NULL;
efd02016 552static const char *output_directory = NULL;
9800a754 553static int outdir_offset;
81f3a188 554
cd2ef591 555static int reopen_stdout(struct commit *commit, struct rev_info *rev)
0377db77 556{
cd2ef591 557 struct strbuf filename = STRBUF_INIT;
c06d2daa 558 int suffix_len = strlen(fmt_patch_suffix) + 1;
0377db77 559
2448482b 560 if (output_directory) {
cd2ef591
SB
561 strbuf_addstr(&filename, output_directory);
562 if (filename.len >=
563 PATH_MAX - FORMAT_PATCH_NAME_MAX - suffix_len)
c06d2daa 564 return error("name of output directory is too long");
cd2ef591
SB
565 if (filename.buf[filename.len - 1] != '/')
566 strbuf_addch(&filename, '/');
2448482b 567 }
0377db77 568
cd2ef591 569 get_patch_filename(commit, rev->nr, fmt_patch_suffix, &filename);
e6ff0f42 570
90b19941 571 if (!DIFF_OPT_TST(&rev->diffopt, QUICK))
cd2ef591 572 fprintf(realstdout, "%s\n", filename.buf + outdir_offset);
ec2956df 573
cd2ef591
SB
574 if (freopen(filename.buf, "w", stdout) == NULL)
575 return error("Cannot open patch file %s", filename.buf);
c06d2daa 576
cd2ef591 577 strbuf_release(&filename);
e6ff0f42 578 return 0;
0377db77
JS
579}
580
5d23e133 581static void get_patch_ids(struct rev_info *rev, struct patch_ids *ids, const char *prefix)
9c6efa36
JS
582{
583 struct rev_info check_rev;
584 struct commit *commit;
585 struct object *o1, *o2;
586 unsigned flags1, flags2;
9c6efa36
JS
587
588 if (rev->pending.nr != 2)
589 die("Need exactly one range.");
590
591 o1 = rev->pending.objects[0].item;
592 flags1 = o1->flags;
593 o2 = rev->pending.objects[1].item;
594 flags2 = o2->flags;
595
596 if ((flags1 & UNINTERESTING) == (flags2 & UNINTERESTING))
597 die("Not a range.");
598
5d23e133 599 init_patch_ids(ids);
9c6efa36
JS
600
601 /* given a range a..b get all patch ids for b..a */
db6296a5 602 init_revisions(&check_rev, prefix);
9c6efa36
JS
603 o1->flags ^= UNINTERESTING;
604 o2->flags ^= UNINTERESTING;
605 add_pending_object(&check_rev, o1, "o1");
606 add_pending_object(&check_rev, o2, "o2");
3d51e1b5
MK
607 if (prepare_revision_walk(&check_rev))
608 die("revision walk setup failed");
9c6efa36
JS
609
610 while ((commit = get_revision(&check_rev)) != NULL) {
611 /* ignore merges */
612 if (commit->parents && commit->parents->next)
613 continue;
614
5d23e133 615 add_commit_patch_id(commit, ids);
9c6efa36
JS
616 }
617
618 /* reset for next revision walk */
81db0941
JS
619 clear_commit_marks((struct commit *)o1,
620 SEEN | UNINTERESTING | SHOWN | ADDED);
621 clear_commit_marks((struct commit *)o2,
622 SEEN | UNINTERESTING | SHOWN | ADDED);
9c6efa36
JS
623 o1->flags = flags1;
624 o2->flags = flags2;
625}
626
e1a37346 627static void gen_message_id(struct rev_info *info, char *base)
d1566f78 628{
774751a8 629 const char *committer = git_committer_info(IDENT_WARN_ON_NO_NAME);
d1566f78
JT
630 const char *email_start = strrchr(committer, '<');
631 const char *email_end = strrchr(committer, '>');
f285a2d7 632 struct strbuf buf = STRBUF_INIT;
e1a37346 633 if (!email_start || !email_end || email_start > email_end - 1)
d1566f78 634 die("Could not extract email from committer identity.");
e1a37346
DB
635 strbuf_addf(&buf, "%s.%lu.git.%.*s", base,
636 (unsigned long) time(NULL),
637 (int)(email_end - email_start - 1), email_start + 1);
638 info->message_id = strbuf_detach(&buf, NULL);
d1566f78
JT
639}
640
2bda2cf4
DB
641static void make_cover_letter(struct rev_info *rev, int use_stdout,
642 int numbered, int numbered_files,
643 struct commit *origin,
644 int nr, struct commit **list, struct commit *head)
a5a27c79
DB
645{
646 const char *committer;
a5a27c79
DB
647 const char *subject_start = NULL;
648 const char *body = "*** SUBJECT HERE ***\n\n*** BLURB HERE ***\n";
649 const char *msg;
650 const char *extra_headers = rev->extra_headers;
2bda2cf4 651 struct shortlog log;
f285a2d7 652 struct strbuf sb = STRBUF_INIT;
2bda2cf4 653 int i;
330db18c 654 const char *encoding = "UTF-8";
39fe578b 655 struct diff_options opts;
267123b4 656 int need_8bit_cte = 0;
cd2ef591 657 struct commit *commit = NULL;
a5a27c79
DB
658
659 if (rev->commit_format != CMIT_FMT_EMAIL)
660 die("Cover letter needs email format");
661
cd2ef591 662 committer = git_committer_info(0);
6df514af 663
cd2ef591
SB
664 if (!numbered_files) {
665 /*
666 * We fake a commit for the cover letter so we get the filename
667 * desired.
668 */
669 commit = xcalloc(1, sizeof(*commit));
670 commit->buffer = xmalloc(400);
671 snprintf(commit->buffer, 400,
672 "tree 0000000000000000000000000000000000000000\n"
673 "parent %s\n"
674 "author %s\n"
675 "committer %s\n\n"
676 "cover letter\n",
108dab28 677 sha1_to_hex(head->object.sha1), committer, committer);
cd2ef591
SB
678 }
679
680 if (!use_stdout && reopen_stdout(commit, rev))
a5a27c79
DB
681 return;
682
cd2ef591 683 if (commit) {
a5a27c79 684
cd2ef591
SB
685 free(commit->buffer);
686 free(commit);
687 }
a5a27c79 688
108dab28 689 log_write_email_headers(rev, head, &subject_start, &extra_headers,
267123b4 690 &need_8bit_cte);
a5a27c79 691
0a7f4483
JS
692 for (i = 0; !need_8bit_cte && i < nr; i++)
693 if (has_non_ascii(list[i]->buffer))
694 need_8bit_cte = 1;
695
a5a27c79 696 msg = body;
a5a27c79
DB
697 pp_user_info(NULL, CMIT_FMT_EMAIL, &sb, committer, DATE_RFC2822,
698 encoding);
699 pp_title_line(CMIT_FMT_EMAIL, &msg, &sb, subject_start, extra_headers,
267123b4 700 encoding, need_8bit_cte);
a5a27c79
DB
701 pp_remainder(CMIT_FMT_EMAIL, &msg, &sb, 0);
702 printf("%s\n", sb.buf);
703
704 strbuf_release(&sb);
705
2bda2cf4 706 shortlog_init(&log);
859c4fbe
JS
707 log.wrap_lines = 1;
708 log.wrap = 72;
709 log.in1 = 2;
710 log.in2 = 4;
2bda2cf4
DB
711 for (i = 0; i < nr; i++)
712 shortlog_add_commit(&log, list[i]);
713
714 shortlog_output(&log);
715
a5a27c79 716 /*
2bda2cf4 717 * We can only do diffstat with a unique reference point
a5a27c79
DB
718 */
719 if (!origin)
720 return;
721
5d02294c
JS
722 memcpy(&opts, &rev->diffopt, sizeof(opts));
723 opts.output_format = DIFF_FORMAT_SUMMARY | DIFF_FORMAT_DIFFSTAT;
a5a27c79 724
39fe578b
DB
725 diff_setup_done(&opts);
726
727 diff_tree_sha1(origin->tree->object.sha1,
728 head->tree->object.sha1,
729 "", &opts);
730 diffcore_std(&opts);
731 diff_flush(&opts);
a5a27c79 732
a5a27c79 733 printf("\n");
d1566f78
JT
734}
735
8419d2ee
JH
736static const char *clean_message_id(const char *msg_id)
737{
738 char ch;
739 const char *a, *z, *m;
8419d2ee
JH
740
741 m = msg_id;
742 while ((ch = *m) && (isspace(ch) || (ch == '<')))
743 m++;
744 a = m;
745 z = NULL;
746 while ((ch = *m)) {
747 if (!isspace(ch) && (ch != '>'))
748 z = m;
749 m++;
750 }
751 if (!z)
752 die("insane in-reply-to: %s", msg_id);
753 if (++z == m)
754 return a;
182af834 755 return xmemdupz(a, z - a);
8419d2ee
JH
756}
757
9800a754
JH
758static const char *set_outdir(const char *prefix, const char *output_directory)
759{
760 if (output_directory && is_absolute_path(output_directory))
761 return output_directory;
762
763 if (!prefix || !*prefix) {
764 if (output_directory)
765 return output_directory;
766 /* The user did not explicitly ask for "./" */
767 outdir_offset = 2;
768 return "./";
769 }
770
771 outdir_offset = strlen(prefix);
772 if (!output_directory)
773 return prefix;
774
775 return xstrdup(prefix_filename(prefix, outdir_offset,
776 output_directory));
777}
778
fff02ee6
SB
779static const char * const builtin_format_patch_usage[] = {
780 "git format-patch [options] [<since> | <revision range>]",
781 NULL
782};
783
784static int keep_subject = 0;
785
786static int keep_callback(const struct option *opt, const char *arg, int unset)
787{
788 ((struct rev_info *)opt->value)->total = -1;
789 keep_subject = 1;
790 return 0;
791}
792
793static int subject_prefix = 0;
794
795static int subject_prefix_callback(const struct option *opt, const char *arg,
796 int unset)
797{
798 subject_prefix = 1;
799 ((struct rev_info *)opt->value)->subject_prefix = arg;
800 return 0;
801}
802
1af4731b
JH
803static int numbered_cmdline_opt = 0;
804
fff02ee6
SB
805static int numbered_callback(const struct option *opt, const char *arg,
806 int unset)
807{
1af4731b 808 *(int *)opt->value = numbered_cmdline_opt = unset ? 0 : 1;
fff02ee6
SB
809 if (unset)
810 auto_number = 0;
811 return 0;
812}
813
814static int no_numbered_callback(const struct option *opt, const char *arg,
815 int unset)
816{
817 return numbered_callback(opt, arg, 1);
818}
819
820static int output_directory_callback(const struct option *opt, const char *arg,
821 int unset)
822{
823 const char **dir = (const char **)opt->value;
824 if (*dir)
825 die("Two output directories?");
826 *dir = arg;
827 return 0;
828}
829
830static int thread_callback(const struct option *opt, const char *arg, int unset)
831{
832 int *thread = (int *)opt->value;
833 if (unset)
834 *thread = 0;
835 else if (!arg || !strcmp(arg, "shallow"))
836 *thread = THREAD_SHALLOW;
837 else if (!strcmp(arg, "deep"))
838 *thread = THREAD_DEEP;
839 else
840 return 1;
841 return 0;
842}
843
844static int attach_callback(const struct option *opt, const char *arg, int unset)
845{
846 struct rev_info *rev = (struct rev_info *)opt->value;
847 if (unset)
848 rev->mime_boundary = NULL;
849 else if (arg)
850 rev->mime_boundary = arg;
851 else
852 rev->mime_boundary = git_version_string;
853 rev->no_inline = unset ? 0 : 1;
854 return 0;
855}
856
857static int inline_callback(const struct option *opt, const char *arg, int unset)
858{
859 struct rev_info *rev = (struct rev_info *)opt->value;
860 if (unset)
861 rev->mime_boundary = NULL;
862 else if (arg)
863 rev->mime_boundary = arg;
864 else
865 rev->mime_boundary = git_version_string;
866 rev->no_inline = 0;
867 return 0;
868}
869
870static int header_callback(const struct option *opt, const char *arg, int unset)
871{
872 add_header(arg);
873 return 0;
874}
875
ae6c098f
SD
876static int to_callback(const struct option *opt, const char *arg, int unset)
877{
ca9e0a1b 878 string_list_append(arg, &extra_to);
ae6c098f
SD
879 return 0;
880}
881
fff02ee6
SB
882static int cc_callback(const struct option *opt, const char *arg, int unset)
883{
ca9e0a1b 884 string_list_append(arg, &extra_cc);
fff02ee6
SB
885 return 0;
886}
887
a633fca0 888int cmd_format_patch(int argc, const char **argv, const char *prefix)
91efcf60
JH
889{
890 struct commit *commit;
891 struct commit **list = NULL;
892 struct rev_info rev;
fff02ee6 893 int nr = 0, total, i;
0377db77 894 int use_stdout = 0;
fa0f02df 895 int start_number = -1;
e6ff0f42 896 int numbered_files = 0; /* _just_ numbers */
9c6efa36 897 int ignore_if_in_upstream = 0;
a5a27c79 898 int cover_letter = 0;
2bda2cf4 899 int boundary_count = 0;
37c22a4b 900 int no_binary_diff = 0;
a5a27c79 901 struct commit *origin = NULL, *head = NULL;
76af0734 902 const char *in_reply_to = NULL;
5d23e133 903 struct patch_ids ids;
cf2251b6 904 char *add_signoff = NULL;
f285a2d7 905 struct strbuf buf = STRBUF_INIT;
1d46f2ea 906 int use_patch_format = 0;
fff02ee6
SB
907 const struct option builtin_format_patch_options[] = {
908 { OPTION_CALLBACK, 'n', "numbered", &numbered, NULL,
909 "use [PATCH n/m] even with a single patch",
910 PARSE_OPT_NOARG, numbered_callback },
911 { OPTION_CALLBACK, 'N', "no-numbered", &numbered, NULL,
912 "use [PATCH] even with multiple patches",
913 PARSE_OPT_NOARG, no_numbered_callback },
914 OPT_BOOLEAN('s', "signoff", &do_signoff, "add Signed-off-by:"),
915 OPT_BOOLEAN(0, "stdout", &use_stdout,
916 "print patches to standard out"),
917 OPT_BOOLEAN(0, "cover-letter", &cover_letter,
918 "generate a cover letter"),
919 OPT_BOOLEAN(0, "numbered-files", &numbered_files,
920 "use simple number sequence for output file names"),
921 OPT_STRING(0, "suffix", &fmt_patch_suffix, "sfx",
922 "use <sfx> instead of '.patch'"),
923 OPT_INTEGER(0, "start-number", &start_number,
924 "start numbering patches at <n> instead of 1"),
925 { OPTION_CALLBACK, 0, "subject-prefix", &rev, "prefix",
926 "Use [<prefix>] instead of [PATCH]",
927 PARSE_OPT_NONEG, subject_prefix_callback },
928 { OPTION_CALLBACK, 'o', "output-directory", &output_directory,
929 "dir", "store resulting files in <dir>",
930 PARSE_OPT_NONEG, output_directory_callback },
931 { OPTION_CALLBACK, 'k', "keep-subject", &rev, NULL,
932 "don't strip/add [PATCH]",
933 PARSE_OPT_NOARG | PARSE_OPT_NONEG, keep_callback },
934 OPT_BOOLEAN(0, "no-binary", &no_binary_diff,
935 "don't output binary diffs"),
936 OPT_BOOLEAN(0, "ignore-if-in-upstream", &ignore_if_in_upstream,
937 "don't include a patch matching a commit upstream"),
2cfa8330
BG
938 { OPTION_BOOLEAN, 'p', "no-stat", &use_patch_format, NULL,
939 "show patch format instead of default (patch + stat)",
940 PARSE_OPT_NONEG | PARSE_OPT_NOARG },
fff02ee6
SB
941 OPT_GROUP("Messaging"),
942 { OPTION_CALLBACK, 0, "add-header", NULL, "header",
943 "add email header", PARSE_OPT_NONEG,
944 header_callback },
ae6c098f
SD
945 { OPTION_CALLBACK, 0, "to", NULL, "email", "add To: header",
946 PARSE_OPT_NONEG, to_callback },
fff02ee6
SB
947 { OPTION_CALLBACK, 0, "cc", NULL, "email", "add Cc: header",
948 PARSE_OPT_NONEG, cc_callback },
949 OPT_STRING(0, "in-reply-to", &in_reply_to, "message-id",
950 "make first mail a reply to <message-id>"),
951 { OPTION_CALLBACK, 0, "attach", &rev, "boundary",
952 "attach the patch", PARSE_OPT_OPTARG,
953 attach_callback },
954 { OPTION_CALLBACK, 0, "inline", &rev, "boundary",
955 "inline the patch",
956 PARSE_OPT_OPTARG | PARSE_OPT_NONEG,
957 inline_callback },
958 { OPTION_CALLBACK, 0, "thread", &thread, "style",
959 "enable message threading, styles: shallow, deep",
960 PARSE_OPT_OPTARG, thread_callback },
961 OPT_END()
962 };
91efcf60 963
ca9e0a1b
SB
964 extra_hdr.strdup_strings = 1;
965 extra_to.strdup_strings = 1;
966 extra_cc.strdup_strings = 1;
ef90d6d4 967 git_config(git_format_config, NULL);
db6296a5 968 init_revisions(&rev, prefix);
91efcf60
JH
969 rev.commit_format = CMIT_FMT_EMAIL;
970 rev.verbose_header = 1;
971 rev.diff = 1;
91efcf60
JH
972 rev.combine_merges = 0;
973 rev.ignore_merges = 1;
8f67f8ae 974 DIFF_OPT_SET(&rev.diffopt, RECURSIVE);
91efcf60 975
dbd21447 976 rev.subject_prefix = fmt_patch_subject_prefix;
20ff0680 977
0db5260b
JW
978 if (default_attach) {
979 rev.mime_boundary = default_attach;
980 rev.no_inline = 1;
981 }
982
2448482b
JS
983 /*
984 * Parse the arguments before setup_revisions(), or something
2dc189a3 985 * like "git format-patch -o a123 HEAD^.." may fail; a123 is
2448482b
JS
986 * possibly a valid SHA1.
987 */
37782920 988 argc = parse_options(argc, argv, prefix, builtin_format_patch_options,
fff02ee6 989 builtin_format_patch_usage,
382da402
FC
990 PARSE_OPT_KEEP_ARGV0 | PARSE_OPT_KEEP_UNKNOWN |
991 PARSE_OPT_KEEP_DASHDASH);
2448482b 992
1d1876e9
HV
993 if (do_signoff) {
994 const char *committer;
995 const char *endpos;
996 committer = git_committer_info(IDENT_ERROR_ON_NO_NAME);
997 endpos = strchr(committer, '>');
998 if (!endpos)
999 die("bogus committer info %s", committer);
1000 add_signoff = xmemdupz(committer, endpos - committer + 1);
1001 }
1002
ca9e0a1b
SB
1003 for (i = 0; i < extra_hdr.nr; i++) {
1004 strbuf_addstr(&buf, extra_hdr.items[i].string);
3ee79d9f
DB
1005 strbuf_addch(&buf, '\n');
1006 }
1007
ca9e0a1b 1008 if (extra_to.nr)
3ee79d9f 1009 strbuf_addstr(&buf, "To: ");
ca9e0a1b 1010 for (i = 0; i < extra_to.nr; i++) {
3ee79d9f
DB
1011 if (i)
1012 strbuf_addstr(&buf, " ");
ca9e0a1b
SB
1013 strbuf_addstr(&buf, extra_to.items[i].string);
1014 if (i + 1 < extra_to.nr)
3ee79d9f
DB
1015 strbuf_addch(&buf, ',');
1016 strbuf_addch(&buf, '\n');
1017 }
1018
ca9e0a1b 1019 if (extra_cc.nr)
3ee79d9f 1020 strbuf_addstr(&buf, "Cc: ");
ca9e0a1b 1021 for (i = 0; i < extra_cc.nr; i++) {
3ee79d9f
DB
1022 if (i)
1023 strbuf_addstr(&buf, " ");
ca9e0a1b
SB
1024 strbuf_addstr(&buf, extra_cc.items[i].string);
1025 if (i + 1 < extra_cc.nr)
3ee79d9f
DB
1026 strbuf_addch(&buf, ',');
1027 strbuf_addch(&buf, '\n');
1028 }
1029
2af202be 1030 rev.extra_headers = strbuf_detach(&buf, NULL);
3ee79d9f 1031
add5c8a5 1032 if (start_number < 0)
fa0f02df 1033 start_number = 1;
ca6b91d2
JM
1034
1035 /*
1036 * If numbered is set solely due to format.numbered in config,
1037 * and it would conflict with --keep-subject (-k) from the
1038 * command line, reset "numbered".
1039 */
1040 if (numbered && keep_subject && !numbered_cmdline_opt)
1041 numbered = 0;
1042
63b398a4 1043 if (numbered && keep_subject)
8ac80a57 1044 die ("-n and -k are mutually exclusive.");
2d9e4a47
RJ
1045 if (keep_subject && subject_prefix)
1046 die ("--subject-prefix and -k are mutually exclusive.");
8ac80a57 1047
2448482b
JS
1048 argc = setup_revisions(argc, argv, &rev, "HEAD");
1049 if (argc > 1)
1050 die ("unrecognized argument: %s", argv[1]);
0377db77 1051
02bc5b03
BG
1052 if (rev.diffopt.output_format & DIFF_FORMAT_NAME)
1053 die("--name-only does not make sense");
1054 if (rev.diffopt.output_format & DIFF_FORMAT_NAME_STATUS)
1055 die("--name-status does not make sense");
1056 if (rev.diffopt.output_format & DIFF_FORMAT_CHECKDIFF)
1057 die("--check does not make sense");
1058
1059 if (!use_patch_format &&
1060 (!rev.diffopt.output_format ||
1061 rev.diffopt.output_format == DIFF_FORMAT_PATCH))
1062 rev.diffopt.output_format = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_SUMMARY;
1063
1064 /* Always generate a patch */
1065 rev.diffopt.output_format |= DIFF_FORMAT_PATCH;
c9b5ef99 1066
37c22a4b 1067 if (!DIFF_OPT_TST(&rev.diffopt, TEXT) && !no_binary_diff)
8f67f8ae 1068 DIFF_OPT_SET(&rev.diffopt, BINARY);
e47f306d 1069
9800a754
JH
1070 if (!use_stdout)
1071 output_directory = set_outdir(prefix, output_directory);
77e565d8 1072
efd02016
JH
1073 if (output_directory) {
1074 if (use_stdout)
1075 die("standard output, or directory, which one?");
1076 if (mkdir(output_directory, 0777) < 0 && errno != EEXIST)
0721c314
TR
1077 die_errno("Could not create directory '%s'",
1078 output_directory);
efd02016
JH
1079 }
1080
1f1e895f 1081 if (rev.pending.nr == 1) {
8a1d076e
JH
1082 if (rev.max_count < 0 && !rev.show_root_diff) {
1083 /*
1084 * This is traditional behaviour of "git format-patch
1085 * origin" that prepares what the origin side still
1086 * does not have.
1087 */
7c496280 1088 rev.pending.objects[0].item->flags |= UNINTERESTING;
3384a2df 1089 add_head_to_pending(&rev);
7c496280 1090 }
8a1d076e
JH
1091 /*
1092 * Otherwise, it is "format-patch -22 HEAD", and/or
1093 * "format-patch --root HEAD". The user wants
1094 * get_revision() to do the usual traversal.
7c496280 1095 */
e686eb98 1096 }
68c2ec7f
JH
1097
1098 /*
1099 * We cannot move this anywhere earlier because we do want to
1100 * know if --root was given explicitly from the comand line.
1101 */
1102 rev.show_root_diff = 1;
1103
a5a27c79
DB
1104 if (cover_letter) {
1105 /* remember the range */
a5a27c79
DB
1106 int i;
1107 for (i = 0; i < rev.pending.nr; i++) {
1108 struct object *o = rev.pending.objects[i].item;
2bda2cf4 1109 if (!(o->flags & UNINTERESTING))
a5a27c79
DB
1110 head = (struct commit *)o;
1111 }
a5a27c79
DB
1112 /* We can't generate a cover letter without any patches */
1113 if (!head)
1114 return 0;
1115 }
e686eb98 1116
9c6efa36 1117 if (ignore_if_in_upstream)
5d23e133 1118 get_patch_ids(&rev, &ids, prefix);
9c6efa36 1119
81f3a188 1120 if (!use_stdout)
f5788250 1121 realstdout = xfdopen(xdup(1), "w");
81f3a188 1122
3d51e1b5
MK
1123 if (prepare_revision_walk(&rev))
1124 die("revision walk setup failed");
2bda2cf4 1125 rev.boundary = 1;
91efcf60 1126 while ((commit = get_revision(&rev)) != NULL) {
2bda2cf4 1127 if (commit->object.flags & BOUNDARY) {
2bda2cf4
DB
1128 boundary_count++;
1129 origin = (boundary_count == 1) ? commit : NULL;
1130 continue;
1131 }
1132
0377db77
JS
1133 /* ignore merges */
1134 if (commit->parents && commit->parents->next)
1135 continue;
9c6efa36
JS
1136
1137 if (ignore_if_in_upstream &&
5d23e133 1138 has_commit_patch_id(commit, &ids))
9c6efa36
JS
1139 continue;
1140
91efcf60 1141 nr++;
83572c1a 1142 list = xrealloc(list, nr * sizeof(list[0]));
91efcf60
JH
1143 list[nr - 1] = commit;
1144 }
0377db77 1145 total = nr;
49604a4d
BG
1146 if (!keep_subject && auto_number && total > 1)
1147 numbered = 1;
596524b3 1148 if (numbered)
fa0f02df 1149 rev.total = total + start_number - 1;
b079c50e
TR
1150 if (in_reply_to || thread || cover_letter)
1151 rev.ref_message_ids = xcalloc(1, sizeof(struct string_list));
1152 if (in_reply_to) {
1153 const char *msgid = clean_message_id(in_reply_to);
1154 string_list_append(msgid, rev.ref_message_ids);
1155 }
108dab28
SB
1156 rev.numbered_files = numbered_files;
1157 rev.patch_suffix = fmt_patch_suffix;
a5a27c79
DB
1158 if (cover_letter) {
1159 if (thread)
1160 gen_message_id(&rev, "cover");
1161 make_cover_letter(&rev, use_stdout, numbered, numbered_files,
2bda2cf4 1162 origin, nr, list, head);
a5a27c79
DB
1163 total++;
1164 start_number--;
1165 }
1166 rev.add_signoff = add_signoff;
91efcf60
JH
1167 while (0 <= --nr) {
1168 int shown;
1169 commit = list[nr];
add5c8a5 1170 rev.nr = total - nr + (start_number - 1);
d1566f78 1171 /* Make the second and subsequent mails replies to the first */
cc35de84 1172 if (thread) {
a5a27c79 1173 /* Have we already had a message ID? */
e1a37346 1174 if (rev.message_id) {
a5a27c79 1175 /*
30984ed2
TR
1176 * For deep threading: make every mail
1177 * a reply to the previous one, no
1178 * matter what other options are set.
1179 *
1180 * For shallow threading:
1181 *
2175c10d
TR
1182 * Without --cover-letter and
1183 * --in-reply-to, make every mail a
1184 * reply to the one before.
1185 *
1186 * With --in-reply-to but no
1187 * --cover-letter, make every mail a
1188 * reply to the <reply-to>.
1189 *
1190 * With --cover-letter, make every
1191 * mail but the cover letter a reply
1192 * to the cover letter. The cover
1193 * letter is a reply to the
1194 * --in-reply-to, if specified.
a5a27c79 1195 */
30984ed2
TR
1196 if (thread == THREAD_SHALLOW
1197 && rev.ref_message_ids->nr > 0
2175c10d 1198 && (!cover_letter || rev.nr > 1))
e1a37346
DB
1199 free(rev.message_id);
1200 else
b079c50e
TR
1201 string_list_append(rev.message_id,
1202 rev.ref_message_ids);
cc35de84 1203 }
e1a37346 1204 gen_message_id(&rev, sha1_to_hex(commit->object.sha1));
d1566f78 1205 }
6df514af 1206
cd2ef591
SB
1207 if (!use_stdout && reopen_stdout(numbered_files ? NULL : commit,
1208 &rev))
a5a27c79 1209 die("Failed to create output files");
91efcf60
JH
1210 shown = log_tree_commit(&rev, commit);
1211 free(commit->buffer);
1212 commit->buffer = NULL;
add5c8a5
JH
1213
1214 /* We put one extra blank line between formatted
1215 * patches and this flag is used by log-tree code
1216 * to see if it needs to emit a LF before showing
1217 * the log; when using one file per patch, we do
1218 * not want the extra blank line.
1219 */
1220 if (!use_stdout)
1221 rev.shown_one = 0;
698ce6f8
JS
1222 if (shown) {
1223 if (rev.mime_boundary)
1224 printf("\n--%s%s--\n\n\n",
1225 mime_boundary_leader,
1226 rev.mime_boundary);
1227 else
1228 printf("-- \n%s\n\n", git_version_string);
1229 }
0377db77
JS
1230 if (!use_stdout)
1231 fclose(stdout);
91efcf60
JH
1232 }
1233 free(list);
ca9e0a1b
SB
1234 string_list_clear(&extra_to, 0);
1235 string_list_clear(&extra_cc, 0);
1236 string_list_clear(&extra_hdr, 0);
5d23e133
JH
1237 if (ignore_if_in_upstream)
1238 free_patch_ids(&ids);
91efcf60
JH
1239 return 0;
1240}
1241
e827633a
RS
1242static int add_pending_commit(const char *arg, struct rev_info *revs, int flags)
1243{
1244 unsigned char sha1[20];
1245 if (get_sha1(arg, sha1) == 0) {
1246 struct commit *commit = lookup_commit_reference(sha1);
1247 if (commit) {
1248 commit->object.flags |= flags;
1249 add_pending_object(revs, &commit->object, arg);
1250 return 0;
1251 }
1252 }
1253 return -1;
1254}
1255
1256static const char cherry_usage[] =
3bc52d7a 1257"git cherry [-v] [<upstream> [<head> [<limit>]]]";
e827633a
RS
1258int cmd_cherry(int argc, const char **argv, const char *prefix)
1259{
1260 struct rev_info revs;
5d23e133 1261 struct patch_ids ids;
e827633a
RS
1262 struct commit *commit;
1263 struct commit_list *list = NULL;
f2968022 1264 struct branch *current_branch;
e827633a
RS
1265 const char *upstream;
1266 const char *head = "HEAD";
1267 const char *limit = NULL;
1268 int verbose = 0;
1269
1270 if (argc > 1 && !strcmp(argv[1], "-v")) {
1271 verbose = 1;
1272 argc--;
1273 argv++;
1274 }
1275
fef34270
JN
1276 if (argc > 1 && !strcmp(argv[1], "-h"))
1277 usage(cherry_usage);
1278
e827633a
RS
1279 switch (argc) {
1280 case 4:
1281 limit = argv[3];
1282 /* FALLTHROUGH */
1283 case 3:
1284 head = argv[2];
1285 /* FALLTHROUGH */
1286 case 2:
1287 upstream = argv[1];
1288 break;
1289 default:
f2968022
MH
1290 current_branch = branch_get(NULL);
1291 if (!current_branch || !current_branch->merge
1292 || !current_branch->merge[0]
1293 || !current_branch->merge[0]->dst) {
1294 fprintf(stderr, "Could not find a tracked"
1295 " remote branch, please"
1296 " specify <upstream> manually.\n");
1297 usage(cherry_usage);
1298 }
1299
1300 upstream = current_branch->merge[0]->dst;
e827633a
RS
1301 }
1302
1303 init_revisions(&revs, prefix);
1304 revs.diff = 1;
1305 revs.combine_merges = 0;
1306 revs.ignore_merges = 1;
8f67f8ae 1307 DIFF_OPT_SET(&revs.diffopt, RECURSIVE);
e827633a
RS
1308
1309 if (add_pending_commit(head, &revs, 0))
1310 die("Unknown commit %s", head);
1311 if (add_pending_commit(upstream, &revs, UNINTERESTING))
1312 die("Unknown commit %s", upstream);
1313
1314 /* Don't say anything if head and upstream are the same. */
1315 if (revs.pending.nr == 2) {
1316 struct object_array_entry *o = revs.pending.objects;
1317 if (hashcmp(o[0].item->sha1, o[1].item->sha1) == 0)
1318 return 0;
1319 }
1320
5d23e133 1321 get_patch_ids(&revs, &ids, prefix);
e827633a
RS
1322
1323 if (limit && add_pending_commit(limit, &revs, UNINTERESTING))
1324 die("Unknown commit %s", limit);
1325
1326 /* reverse the list of commits */
3d51e1b5
MK
1327 if (prepare_revision_walk(&revs))
1328 die("revision walk setup failed");
e827633a
RS
1329 while ((commit = get_revision(&revs)) != NULL) {
1330 /* ignore merges */
1331 if (commit->parents && commit->parents->next)
1332 continue;
1333
1334 commit_list_insert(commit, &list);
1335 }
1336
1337 while (list) {
e827633a
RS
1338 char sign = '+';
1339
1340 commit = list->item;
5d23e133 1341 if (has_commit_patch_id(commit, &ids))
e827633a
RS
1342 sign = '-';
1343
1344 if (verbose) {
f285a2d7 1345 struct strbuf buf = STRBUF_INIT;
dd2e794a 1346 struct pretty_print_context ctx = {0};
674d1727 1347 pretty_print_commit(CMIT_FMT_ONELINE, commit,
dd2e794a 1348 &buf, &ctx);
e827633a 1349 printf("%c %s %s\n", sign,
674d1727
PH
1350 sha1_to_hex(commit->object.sha1), buf.buf);
1351 strbuf_release(&buf);
e827633a
RS
1352 }
1353 else {
1354 printf("%c %s\n", sign,
1355 sha1_to_hex(commit->object.sha1));
1356 }
1357
1358 list = list->next;
1359 }
1360
5d23e133 1361 free_patch_ids(&ids);
e827633a
RS
1362 return 0;
1363}