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