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