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