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