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