log: flip the --mailmap default unconditionally
[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 */
f8adbec9 7#define USE_THE_INDEX_COMPATIBILITY_MACROS
70827b15 8#include "cache.h"
b2141fc1 9#include "config.h"
fb58c8d5 10#include "refs.h"
cbd53a21 11#include "object-store.h"
6b2f2d98 12#include "color.h"
70827b15
LT
13#include "commit.h"
14#include "diff.h"
15#include "revision.h"
16#include "log-tree.h"
91efcf60 17#include "builtin.h"
5d7eeee2 18#include "tag.h"
cf39f54e 19#include "reflog-walk.h"
5d23e133 20#include "patch-ids.h"
a5a27c79 21#include "run-command.h"
2bda2cf4 22#include "shortlog.h"
f2968022 23#include "remote.h"
b079c50e 24#include "string-list.h"
fff02ee6 25#include "parse-options.h"
12da1d1f 26#include "line-log.h"
739453a3 27#include "branch.h"
74775a09 28#include "streaming.h"
816fb46b 29#include "version.h"
ea57bc0d 30#include "mailmap.h"
6005dbb9 31#include "gpg-interface.h"
738e88a2 32#include "progress.h"
7b680d32 33#include "commit-slab.h"
109cd76d 34#include "repository.h"
64043556 35#include "commit-reach.h"
126facf8 36#include "interdiff.h"
31e2617a 37#include "range-diff.h"
70827b15 38
43662b23
NTND
39#define MAIL_DEFAULT_WRAP 72
40
dd0ffd5b
HO
41/* Set a default date-time format for git log ("log.date" config variable) */
42static const char *default_date_mode = NULL;
43
0c47695a 44static int default_abbrev_commit;
0f03ca94 45static int default_show_root = 1;
076c9837 46static int default_follow;
fce04c3c 47static int default_show_signature;
8a3d203b 48static int decoration_style;
1c40c36b 49static int decoration_given;
f0596ecc 50static int use_mailmap_config = -1;
d54276f2 51static const char *fmt_patch_subject_prefix = "PATCH";
94c22a5e 52static const char *fmt_pretty;
0f03ca94 53
1c40c36b 54static const char * const builtin_log_usage[] = {
ce41720c 55 N_("git log [<options>] [<revision-range>] [[--] <path>...]"),
bb831db6 56 N_("git show [<options>] <object>..."),
1c40c36b
CMN
57 NULL
58};
1c370ea4 59
12da1d1f
TR
60struct line_opt_callback_data {
61 struct rev_info *rev;
62 const char *prefix;
63 struct string_list args;
64};
65
f0596ecc
AC
66static int session_is_interactive(void)
67{
68 return isatty(1) || pager_in_use();
69}
70
940a911f
AH
71static int auto_decoration_style(void)
72{
f0596ecc 73 return session_is_interactive() ? DECORATE_SHORT_REFS : 0;
940a911f
AH
74}
75
f094b89a 76static int parse_decoration_style(const char *value)
8a3d203b 77{
89576613 78 switch (git_parse_maybe_bool(value)) {
8a3d203b
JH
79 case 1:
80 return DECORATE_SHORT_REFS;
81 case 0:
82 return 0;
83 default:
84 break;
85 }
86 if (!strcmp(value, "full"))
87 return DECORATE_FULL_REFS;
88 else if (!strcmp(value, "short"))
89 return DECORATE_SHORT_REFS;
15715866 90 else if (!strcmp(value, "auto"))
940a911f 91 return auto_decoration_style();
5a59a230
NTND
92 /*
93 * Please update _git_log() in git-completion.bash when you
94 * add new decoration styles.
95 */
8a3d203b
JH
96 return -1;
97}
98
1c40c36b
CMN
99static int decorate_callback(const struct option *opt, const char *arg, int unset)
100{
101 if (unset)
102 decoration_style = 0;
103 else if (arg)
f094b89a 104 decoration_style = parse_decoration_style(arg);
1c40c36b
CMN
105 else
106 decoration_style = DECORATE_SHORT_REFS;
107
108 if (decoration_style < 0)
e4a590ef 109 die(_("invalid --decorate option: %s"), arg);
1c40c36b
CMN
110
111 decoration_given = 1;
112
113 return 0;
114}
115
12da1d1f
TR
116static int log_line_range_callback(const struct option *option, const char *arg, int unset)
117{
118 struct line_opt_callback_data *data = option->value;
119
517fe807
JK
120 BUG_ON_OPT_NEG(unset);
121
12da1d1f
TR
122 if (!arg)
123 return -1;
124
125 data->rev->line_level_traverse = 1;
126 string_list_append(&data->args, arg);
127
128 return 0;
129}
130
9501d191
MM
131static void init_log_defaults(void)
132{
38bbc2ea 133 init_grep_defaults(the_repository);
5404c116 134 init_diff_ui_defaults();
c74271aa 135
136 decoration_style = auto_decoration_style();
9501d191
MM
137}
138
ef803fd4 139static void cmd_log_init_defaults(struct rev_info *rev)
70827b15 140{
94c22a5e 141 if (fmt_pretty)
4da45bef 142 get_commit_format(fmt_pretty, rev);
076c9837 143 if (default_follow)
0d1e0e78 144 rev->diffopt.flags.default_follow_renames = 1;
70827b15 145 rev->verbose_header = 1;
0d1e0e78 146 rev->diffopt.flags.recursive = 1;
5e0ec15e 147 rev->diffopt.stat_width = -1; /* use full terminal width */
df44483a 148 rev->diffopt.stat_graph_width = -1; /* respect statGraphWidth config */
0c47695a 149 rev->abbrev_commit = default_abbrev_commit;
0f03ca94 150 rev->show_root_diff = default_show_root;
d54276f2 151 rev->subject_prefix = fmt_patch_subject_prefix;
fce04c3c 152 rev->show_signature = default_show_signature;
0d1e0e78 153 rev->diffopt.flags.allow_textconv = 1;
dd0ffd5b
HO
154
155 if (default_date_mode)
a5481a6c 156 parse_date_format(default_date_mode, &rev->date_mode);
ef803fd4 157}
dd0ffd5b 158
ef803fd4
MG
159static void cmd_log_init_finish(int argc, const char **argv, const char *prefix,
160 struct rev_info *rev, struct setup_revision_opt *opt)
161{
ef803fd4 162 struct userformat_want w;
ea57bc0d 163 int quiet = 0, source = 0, mailmap = 0;
12da1d1f 164 static struct line_opt_callback_data line_cb = {NULL, NULL, STRING_LIST_INIT_DUP};
65516f58
RA
165 static struct string_list decorate_refs_exclude = STRING_LIST_INIT_NODUP;
166 static struct string_list decorate_refs_include = STRING_LIST_INIT_NODUP;
167 struct decoration_filter decoration_filter = {&decorate_refs_include,
168 &decorate_refs_exclude};
87be2523 169 static struct revision_sources revision_sources;
1c40c36b
CMN
170
171 const struct option builtin_log_options[] = {
b7df098c 172 OPT__QUIET(&quiet, N_("suppress diff output")),
aa089cd9
FC
173 OPT_BOOL(0, "source", &source, N_("show source")),
174 OPT_BOOL(0, "use-mailmap", &mailmap, N_("Use mail map file")),
65516f58
RA
175 OPT_STRING_LIST(0, "decorate-refs", &decorate_refs_include,
176 N_("pattern"), N_("only decorate refs that match <pattern>")),
177 OPT_STRING_LIST(0, "decorate-refs-exclude", &decorate_refs_exclude,
178 N_("pattern"), N_("do not decorate refs that match <pattern>")),
e62cd35a 179 { OPTION_CALLBACK, 0, "decorate", NULL, NULL, N_("decorate options"),
1c40c36b 180 PARSE_OPT_OPTARG, decorate_callback},
12da1d1f 181 OPT_CALLBACK('L', NULL, &line_cb, "n,m:file",
e4a590ef 182 N_("Process line range n,m in file, counting from 1"),
12da1d1f 183 log_line_range_callback),
1c40c36b
CMN
184 OPT_END()
185 };
186
12da1d1f
TR
187 line_cb.rev = rev;
188 line_cb.prefix = prefix;
189
e6bb5f78 190 mailmap = use_mailmap_config;
1c40c36b
CMN
191 argc = parse_options(argc, argv, prefix,
192 builtin_log_options, builtin_log_usage,
193 PARSE_OPT_KEEP_ARGV0 | PARSE_OPT_KEEP_UNKNOWN |
194 PARSE_OPT_KEEP_DASHDASH);
52883fbd 195
01771a8e
JH
196 if (quiet)
197 rev->diffopt.output_format |= DIFF_FORMAT_NO_OUTPUT;
f9c75d85 198 argc = setup_revisions(argc, argv, rev, opt);
dd0ffd5b 199
1c40c36b
CMN
200 /* Any arguments at this point are not recognized */
201 if (argc > 1)
e4a590ef 202 die(_("unrecognized argument: %s"), argv[1]);
1c40c36b 203
5b163603
JG
204 memset(&w, 0, sizeof(w));
205 userformat_find_requirements(NULL, &w);
206
7ed20f59 207 if (mailmap < 0)
f0596ecc 208 mailmap = 0;
f0596ecc 209
5b163603 210 if (!rev->show_notes_given && (!rev->pretty_given || w.notes))
66b2ed09 211 rev->show_notes = 1;
894a9d33
TR
212 if (rev->show_notes)
213 init_display_notes(&rev->notes_opt);
66b2ed09 214
cf63051a
SB
215 if ((rev->diffopt.pickaxe_opts & DIFF_PICKAXE_KINDS_MASK) ||
216 rev->diffopt.filter || rev->diffopt.flags.follow_renames)
9dafea26 217 rev->always_show_header = 0;
1c40c36b 218
ad6f028f 219 if (source || w.source) {
87be2523
NTND
220 init_revision_sources(&revision_sources);
221 rev->sources = &revision_sources;
222 }
635530a2 223
ea57bc0d
AP
224 if (mailmap) {
225 rev->mailmap = xcalloc(1, sizeof(struct string_list));
226 read_mailmap(rev->mailmap, NULL);
227 }
228
0c47695a
JS
229 if (rev->pretty_given && rev->commit_format == CMIT_FMT_RAW) {
230 /*
231 * "log --pretty=raw" is special; ignore UI oriented
232 * configuration variables such as decoration.
233 */
234 if (!decoration_given)
235 decoration_style = 0;
236 if (!rev->abbrev_commit_given)
237 rev->abbrev_commit = 0;
238 }
635530a2 239
33e7018c
LH
240 if (decoration_style) {
241 rev->show_decorations = 1;
65516f58 242 load_ref_decorations(&decoration_filter, decoration_style);
33e7018c 243 }
12da1d1f
TR
244
245 if (rev->line_level_traverse)
246 line_log_init(rev, line_cb.prefix, &line_cb.args);
247
1fda91b5 248 setup_pager();
9dafea26
TH
249}
250
ef803fd4
MG
251static void cmd_log_init(int argc, const char **argv, const char *prefix,
252 struct rev_info *rev, struct setup_revision_opt *opt)
253{
254 cmd_log_init_defaults(rev);
255 cmd_log_init_finish(argc, argv, prefix, rev, opt);
256}
257
252a7c02
LT
258/*
259 * This gives a rough estimate for how many commits we
260 * will print out in the list.
261 */
7954d365 262static int estimate_commit_count(struct commit_list *list)
252a7c02
LT
263{
264 int n = 0;
265
266 while (list) {
267 struct commit *commit = list->item;
268 unsigned int flags = commit->object.flags;
252a7c02 269 list = list->next;
7dc0fe3b 270 if (!(flags & (TREESAME | UNINTERESTING)))
53b2c823 271 n++;
252a7c02
LT
272 }
273 return n;
274}
275
276static void show_early_header(struct rev_info *rev, const char *stage, int nr)
277{
278 if (rev->shown_one) {
279 rev->shown_one = 0;
280 if (rev->commit_format != CMIT_FMT_ONELINE)
281 putchar(rev->diffopt.line_termination);
282 }
95235f5b 283 fprintf(rev->diffopt.file, _("Final output: %d %s\n"), nr, stage);
252a7c02
LT
284}
285
2af202be 286static struct itimerval early_output_timer;
252a7c02 287
cdcefbc9
LT
288static void log_show_early(struct rev_info *revs, struct commit_list *list)
289{
6ea57703 290 int i = revs->early_output, close_file = revs->diffopt.close_file;
252a7c02 291 int show_header = 1;
cdcefbc9 292
6ea57703 293 revs->diffopt.close_file = 0;
08f704f2 294 sort_in_topological_order(&list, revs->sort_order);
cdcefbc9
LT
295 while (list && i) {
296 struct commit *commit = list->item;
252a7c02
LT
297 switch (simplify_commit(revs, commit)) {
298 case commit_show:
299 if (show_header) {
7954d365 300 int n = estimate_commit_count(list);
252a7c02
LT
301 show_early_header(revs, "incomplete", n);
302 show_header = 0;
303 }
304 log_tree_commit(revs, commit);
305 i--;
306 break;
307 case commit_ignore:
308 break;
309 case commit_error:
6ea57703
JS
310 if (close_file)
311 fclose(revs->diffopt.file);
252a7c02
LT
312 return;
313 }
cdcefbc9 314 list = list->next;
cdcefbc9 315 }
252a7c02
LT
316
317 /* Did we already get enough commits for the early output? */
6ea57703
JS
318 if (!i) {
319 if (close_file)
320 fclose(revs->diffopt.file);
252a7c02 321 return;
6ea57703 322 }
252a7c02
LT
323
324 /*
325 * ..if no, then repeat it twice a second until we
326 * do.
327 *
328 * NOTE! We don't use "it_interval", because if the
329 * reader isn't listening, we want our output to be
330 * throttled by the writing, and not have the timer
331 * trigger every second even if we're blocked on a
332 * reader!
333 */
334 early_output_timer.it_value.tv_sec = 0;
335 early_output_timer.it_value.tv_usec = 500000;
336 setitimer(ITIMER_REAL, &early_output_timer, NULL);
cdcefbc9
LT
337}
338
339static void early_output(int signal)
340{
341 show_early_output = log_show_early;
342}
343
7954d365 344static void setup_early_output(void)
cdcefbc9
LT
345{
346 struct sigaction sa;
cdcefbc9
LT
347
348 /*
349 * Set up the signal handler, minimally intrusively:
350 * we only set a single volatile integer word (not
351 * using sigatomic_t - trying to avoid unnecessary
352 * system dependencies and headers), and using
353 * SA_RESTART.
354 */
355 memset(&sa, 0, sizeof(sa));
356 sa.sa_handler = early_output;
357 sigemptyset(&sa.sa_mask);
358 sa.sa_flags = SA_RESTART;
359 sigaction(SIGALRM, &sa, NULL);
360
361 /*
362 * If we can get the whole output in less than a
363 * tenth of a second, don't even bother doing the
364 * early-output thing..
365 *
366 * This is a one-time-only trigger.
367 */
252a7c02
LT
368 early_output_timer.it_value.tv_sec = 0;
369 early_output_timer.it_value.tv_usec = 100000;
370 setitimer(ITIMER_REAL, &early_output_timer, NULL);
cdcefbc9
LT
371}
372
373static void finish_early_output(struct rev_info *rev)
374{
7954d365 375 int n = estimate_commit_count(rev->commits);
cdcefbc9 376 signal(SIGALRM, SIG_IGN);
252a7c02 377 show_early_header(rev, "done", n);
cdcefbc9
LT
378}
379
9dafea26
TH
380static int cmd_log_walk(struct rev_info *rev)
381{
382 struct commit *commit;
f31027c9 383 int saved_nrl = 0;
6ea57703 384 int saved_dcctc = 0, close_file = rev->diffopt.close_file;
70827b15 385
cdcefbc9 386 if (rev->early_output)
7954d365 387 setup_early_output();
cdcefbc9 388
3d51e1b5 389 if (prepare_revision_walk(rev))
5c5f1d7c 390 die(_("revision walk setup failed"));
cdcefbc9
LT
391
392 if (rev->early_output)
393 finish_early_output(rev);
394
036d17fe 395 /*
84102a33
PVM
396 * For --check and --exit-code, the exit code is based on CHECK_FAILED
397 * and HAS_CHANGES being accumulated in rev->diffopt, so be careful to
398 * retain that state information if replacing rev->diffopt in this loop
036d17fe 399 */
6ea57703 400 rev->diffopt.close_file = 0;
70827b15 401 while ((commit = get_revision(rev)) != NULL) {
3acf8dd8 402 if (!log_tree_commit(rev, commit) && rev->max_count >= 0)
251df09b
MM
403 /*
404 * We decremented max_count in get_revision,
405 * but we didn't actually show the commit.
406 */
407 rev->max_count++;
a6c73064 408 if (!rev->reflog_info) {
822601e8
JK
409 /*
410 * We may show a given commit multiple times when
411 * walking the reflogs.
412 */
6a7895fd
SB
413 free_commit_buffer(the_repository->parsed_objects,
414 commit);
f35650df
JK
415 free_commit_list(commit->parents);
416 commit->parents = NULL;
a6c73064 417 }
f31027c9
JH
418 if (saved_nrl < rev->diffopt.needed_rename_limit)
419 saved_nrl = rev->diffopt.needed_rename_limit;
420 if (rev->diffopt.degraded_cc_to_c)
421 saved_dcctc = 1;
70827b15 422 }
f31027c9
JH
423 rev->diffopt.degraded_cc_to_c = saved_dcctc;
424 rev->diffopt.needed_rename_limit = saved_nrl;
6ea57703
JS
425 if (close_file)
426 fclose(rev->diffopt.file);
f31027c9 427
036d17fe 428 if (rev->diffopt.output_format & DIFF_FORMAT_CHECKDIFF &&
0d1e0e78 429 rev->diffopt.flags.check_failed) {
036d17fe
PVM
430 return 02;
431 }
84102a33 432 return diff_result_code(&rev->diffopt, 0);
70827b15
LT
433}
434
ef90d6d4 435static int git_log_config(const char *var, const char *value, void *cb)
0f03ca94 436{
e3f1da98
RS
437 const char *slot_name;
438
94c22a5e
CR
439 if (!strcmp(var, "format.pretty"))
440 return git_config_string(&fmt_pretty, var, value);
70cff3ac
BH
441 if (!strcmp(var, "format.subjectprefix"))
442 return git_config_string(&fmt_patch_subject_prefix, var, value);
0c47695a
JS
443 if (!strcmp(var, "log.abbrevcommit")) {
444 default_abbrev_commit = git_config_bool(var, value);
445 return 0;
446 }
dd0ffd5b
HO
447 if (!strcmp(var, "log.date"))
448 return git_config_string(&default_date_mode, var, value);
eb734454 449 if (!strcmp(var, "log.decorate")) {
f094b89a 450 decoration_style = parse_decoration_style(value);
8a3d203b
JH
451 if (decoration_style < 0)
452 decoration_style = 0; /* maybe warn? */
eb734454
SD
453 return 0;
454 }
0f03ca94
PB
455 if (!strcmp(var, "log.showroot")) {
456 default_show_root = git_config_bool(var, value);
457 return 0;
458 }
076c9837
DT
459 if (!strcmp(var, "log.follow")) {
460 default_follow = git_config_bool(var, value);
461 return 0;
462 }
e3f1da98 463 if (skip_prefix(var, "color.decorate.", &slot_name))
b9465768 464 return parse_decorate_color_config(var, slot_name, value);
e6bb5f78
AP
465 if (!strcmp(var, "log.mailmap")) {
466 use_mailmap_config = git_config_bool(var, value);
467 return 0;
468 }
fce04c3c
MJ
469 if (!strcmp(var, "log.showsignature")) {
470 default_show_signature = git_config_bool(var, value);
471 return 0;
472 }
e6bb5f78 473
0657bcbf
JH
474 if (grep_config(var, value, cb) < 0)
475 return -1;
6005dbb9 476 if (git_gpg_config(var, value, cb) < 0)
0657bcbf 477 return -1;
ef90d6d4 478 return git_diff_ui_config(var, value, cb);
0f03ca94
PB
479}
480
a633fca0 481int cmd_whatchanged(int argc, const char **argv, const char *prefix)
70827b15
LT
482{
483 struct rev_info rev;
32962c9b 484 struct setup_revision_opt opt;
70827b15 485
9501d191 486 init_log_defaults();
ef90d6d4 487 git_config(git_log_config, NULL);
6b2f2d98 488
2abf3503 489 repo_init_revisions(the_repository, &rev, prefix);
70827b15 490 rev.diff = 1;
9202434c 491 rev.simplify_history = 0;
32962c9b
JH
492 memset(&opt, 0, sizeof(opt));
493 opt.def = "HEAD";
d5f6b1d7 494 opt.revarg_opt = REVARG_COMMITTISH;
32962c9b 495 cmd_log_init(argc, argv, prefix, &rev, &opt);
9dafea26
TH
496 if (!rev.diffopt.output_format)
497 rev.diffopt.output_format = DIFF_FORMAT_RAW;
498 return cmd_log_walk(&rev);
70827b15
LT
499}
500
af117077 501static void show_tagger(const char *buf, struct rev_info *rev)
56122ed8 502{
ea718e65 503 struct strbuf out = STRBUF_INIT;
6bf13944 504 struct pretty_print_context pp = {0};
56122ed8 505
6bf13944
JK
506 pp.fmt = rev->commit_format;
507 pp.date_mode = rev->date_mode;
508 pp_user_info(&pp, "Tagger", &out, buf, get_log_output_encoding());
95235f5b 509 fprintf(rev->diffopt.file, "%s", out.buf);
ea718e65 510 strbuf_release(&out);
56122ed8
JS
511}
512
d801627b 513static int show_blob_object(const struct object_id *oid, struct rev_info *rev, const char *obj_name)
74775a09 514{
d801627b 515 struct object_id oidc;
083b9931
MG
516 struct object_context obj_context;
517 char *buf;
518 unsigned long size;
519
95235f5b 520 fflush(rev->diffopt.file);
0d1e0e78
BW
521 if (!rev->diffopt.flags.textconv_set_via_cmdline ||
522 !rev->diffopt.flags.allow_textconv)
7eda0e4f 523 return stream_blob_to_fd(1, oid, NULL, 0);
083b9931 524
3a7a698e
NTND
525 if (get_oid_with_context(the_repository, obj_name,
526 GET_OID_RECORD_PATH,
e82caf38 527 &oidc, &obj_context))
bc208ae3 528 die(_("not a valid object name %s"), obj_name);
dc944b65 529 if (!obj_context.path ||
6afaf807
NTND
530 !textconv_object(the_repository, obj_context.path,
531 obj_context.mode, &oidc, 1, &buf, &size)) {
dc944b65 532 free(obj_context.path);
7eda0e4f 533 return stream_blob_to_fd(1, oid, NULL, 0);
dc944b65 534 }
083b9931
MG
535
536 if (!buf)
e4a590ef 537 die(_("git show %s: bad file"), obj_name);
083b9931
MG
538
539 write_or_die(1, buf, size);
dc944b65 540 free(obj_context.path);
083b9931 541 return 0;
74775a09
NTND
542}
543
d801627b 544static int show_tag_object(const struct object_id *oid, struct rev_info *rev)
5d7eeee2
JS
545{
546 unsigned long size;
21666f1a 547 enum object_type type;
b4f5aca4 548 char *buf = read_object_file(oid, &type, &size);
5d7eeee2
JS
549 int offset = 0;
550
551 if (!buf)
bc208ae3 552 return error(_("could not read object %s"), oid_to_hex(oid));
5d7eeee2 553
74775a09
NTND
554 assert(type == OBJ_TAG);
555 while (offset < size && buf[offset] != '\n') {
556 int new_offset = offset + 1;
af117077 557 const char *ident;
74775a09
NTND
558 while (new_offset < size && buf[new_offset++] != '\n')
559 ; /* do nothing */
af117077
JK
560 if (skip_prefix(buf + offset, "tagger ", &ident))
561 show_tagger(ident, rev);
74775a09
NTND
562 offset = new_offset;
563 }
5d7eeee2
JS
564
565 if (offset < size)
95235f5b 566 fwrite(buf + offset, size - offset, 1, rev->diffopt.file);
5d7eeee2
JS
567 free(buf);
568 return 0;
569}
570
df46d77e 571static int show_tree_object(const struct object_id *oid,
6a0b0b6d 572 struct strbuf *base,
671f0707 573 const char *pathname, unsigned mode, int stage, void *context)
5d7eeee2 574{
95235f5b
JS
575 FILE *file = context;
576 fprintf(file, "%s%s\n", pathname, S_ISDIR(mode) ? "/" : "");
5d7eeee2
JS
577 return 0;
578}
579
b130c706
JH
580static void show_setup_revisions_tweak(struct rev_info *rev,
581 struct setup_revision_opt *opt)
b4490059 582{
2bf65873
JH
583 if (rev->ignore_merges) {
584 /* There was no "-m" on the command line */
585 rev->ignore_merges = 0;
586 if (!rev->first_parent_only && !rev->combine_merges) {
01689909 587 /* No "--first-parent", "-c", or "--cc" */
2bf65873
JH
588 rev->combine_merges = 1;
589 rev->dense_combined_merges = 1;
590 }
591 }
b4490059
JH
592 if (!rev->diffopt.output_format)
593 rev->diffopt.output_format = DIFF_FORMAT_PATCH;
594}
595
a633fca0 596int cmd_show(int argc, const char **argv, const char *prefix)
70827b15
LT
597{
598 struct rev_info rev;
5d7eeee2 599 struct object_array_entry *objects;
32962c9b 600 struct setup_revision_opt opt;
f0096c06 601 struct pathspec match_all;
5d7eeee2 602 int i, count, ret = 0;
70827b15 603
9501d191 604 init_log_defaults();
ef90d6d4 605 git_config(git_log_config, NULL);
6b2f2d98 606
9a087274 607 memset(&match_all, 0, sizeof(match_all));
2abf3503 608 repo_init_revisions(the_repository, &rev, prefix);
70827b15 609 rev.diff = 1;
70827b15 610 rev.always_show_header = 1;
ca92e59e 611 rev.no_walk = REVISION_WALK_NO_WALK_SORTED;
666c92a2
ZJS
612 rev.diffopt.stat_width = -1; /* Scale to real terminal size */
613
32962c9b
JH
614 memset(&opt, 0, sizeof(opt));
615 opt.def = "HEAD";
b130c706 616 opt.tweak = show_setup_revisions_tweak;
32962c9b 617 cmd_log_init(argc, argv, prefix, &rev, &opt);
5d7eeee2 618
c5941f1a
JH
619 if (!rev.no_walk)
620 return cmd_log_walk(&rev);
621
5d7eeee2
JS
622 count = rev.pending.nr;
623 objects = rev.pending.objects;
624 for (i = 0; i < count && !ret; i++) {
625 struct object *o = objects[i].item;
626 const char *name = objects[i].name;
627 switch (o->type) {
628 case OBJ_BLOB:
d801627b 629 ret = show_blob_object(&o->oid, &rev, name);
5d7eeee2
JS
630 break;
631 case OBJ_TAG: {
632 struct tag *t = (struct tag *)o;
633
ae03ee64
JK
634 if (rev.shown_one)
635 putchar('\n');
95235f5b 636 fprintf(rev.diffopt.file, "%stag %s%s\n",
8f67f8ae 637 diff_get_color_opt(&rev.diffopt, DIFF_COMMIT),
5d7eeee2 638 t->tag,
8f67f8ae 639 diff_get_color_opt(&rev.diffopt, DIFF_RESET));
d801627b 640 ret = show_tag_object(&o->oid, &rev);
ae03ee64 641 rev.shown_one = 1;
d2dadfe8
JH
642 if (ret)
643 break;
109cd76d 644 o = parse_object(the_repository, &t->tagged->oid);
d2dadfe8 645 if (!o)
bc208ae3 646 ret = error(_("could not read object %s"),
f2fd0760 647 oid_to_hex(&t->tagged->oid));
d2dadfe8 648 objects[i].item = o;
5d7eeee2
JS
649 i--;
650 break;
651 }
652 case OBJ_TREE:
ae03ee64
JK
653 if (rev.shown_one)
654 putchar('\n');
95235f5b 655 fprintf(rev.diffopt.file, "%stree %s%s\n\n",
8f67f8ae 656 diff_get_color_opt(&rev.diffopt, DIFF_COMMIT),
5d7eeee2 657 name,
8f67f8ae 658 diff_get_color_opt(&rev.diffopt, DIFF_RESET));
e092073d
NTND
659 read_tree_recursive(the_repository, (struct tree *)o, "",
660 0, 0, &match_all, show_tree_object,
661 rev.diffopt.file);
ae03ee64 662 rev.shown_one = 1;
5d7eeee2
JS
663 break;
664 case OBJ_COMMIT:
665 rev.pending.nr = rev.pending.alloc = 0;
666 rev.pending.objects = NULL;
667 add_object_array(o, name, &rev.pending);
668 ret = cmd_log_walk(&rev);
669 break;
670 default:
bc208ae3 671 ret = error(_("unknown type: %d"), o->type);
5d7eeee2
JS
672 }
673 }
674 free(objects);
675 return ret;
70827b15
LT
676}
677
cf39f54e
LT
678/*
679 * This is equivalent to "git log -g --abbrev-commit --pretty=oneline"
680 */
681int cmd_log_reflog(int argc, const char **argv, const char *prefix)
682{
683 struct rev_info rev;
32962c9b 684 struct setup_revision_opt opt;
cf39f54e 685
9501d191 686 init_log_defaults();
ef90d6d4 687 git_config(git_log_config, NULL);
6b2f2d98 688
2abf3503 689 repo_init_revisions(the_repository, &rev, prefix);
cf39f54e 690 init_reflog_walk(&rev.reflog_info);
cf39f54e 691 rev.verbose_header = 1;
32962c9b
JH
692 memset(&opt, 0, sizeof(opt));
693 opt.def = "HEAD";
4b56cf58 694 cmd_log_init_defaults(&rev);
0c47695a 695 rev.abbrev_commit = 1;
cf39f54e 696 rev.commit_format = CMIT_FMT_ONELINE;
4da45bef 697 rev.use_terminator = 1;
cf39f54e 698 rev.always_show_header = 1;
4b56cf58 699 cmd_log_init_finish(argc, argv, prefix, &rev, &opt);
cf39f54e 700
cf39f54e
LT
701 return cmd_log_walk(&rev);
702}
703
b130c706
JH
704static void log_setup_revisions_tweak(struct rev_info *rev,
705 struct setup_revision_opt *opt)
076c9837 706{
0d1e0e78 707 if (rev->diffopt.flags.default_follow_renames &&
076c9837 708 rev->prune_data.nr == 1)
0d1e0e78 709 rev->diffopt.flags.follow_renames = 1;
c7eaf8b4
JH
710
711 /* Turn --cc/-c into -p --cc/-c when -p was not given */
712 if (!rev->diffopt.output_format && rev->combine_merges)
713 rev->diffopt.output_format = DIFF_FORMAT_PATCH;
82dee416
JH
714
715 /* Turn -m on when --cc/-c was given */
716 if (rev->combine_merges)
717 rev->ignore_merges = 0;
076c9837
DT
718}
719
a633fca0 720int cmd_log(int argc, const char **argv, const char *prefix)
70827b15
LT
721{
722 struct rev_info rev;
32962c9b 723 struct setup_revision_opt opt;
70827b15 724
9501d191 725 init_log_defaults();
ef90d6d4 726 git_config(git_log_config, NULL);
6b2f2d98 727
2abf3503 728 repo_init_revisions(the_repository, &rev, prefix);
70827b15 729 rev.always_show_header = 1;
32962c9b
JH
730 memset(&opt, 0, sizeof(opt));
731 opt.def = "HEAD";
d5f6b1d7 732 opt.revarg_opt = REVARG_COMMITTISH;
b130c706 733 opt.tweak = log_setup_revisions_tweak;
32962c9b 734 cmd_log_init(argc, argv, prefix, &rev, &opt);
9dafea26 735 return cmd_log_walk(&rev);
70827b15 736}
91efcf60 737
c06d2daa 738/* format-patch */
0377db77 739
917a8f89 740static const char *fmt_patch_suffix = ".patch";
49604a4d 741static int numbered = 0;
a567fdcb 742static int auto_number = 1;
20ff0680 743
0db5260b
JW
744static char *default_attach = NULL;
745
2721ce21
JK
746static struct string_list extra_hdr = STRING_LIST_INIT_NODUP;
747static struct string_list extra_to = STRING_LIST_INIT_NODUP;
748static struct string_list extra_cc = STRING_LIST_INIT_NODUP;
3ee79d9f
DB
749
750static void add_header(const char *value)
751{
ca9e0a1b 752 struct string_list_item *item;
3ee79d9f 753 int len = strlen(value);
c8c4450e 754 while (len && value[len - 1] == '\n')
3ee79d9f 755 len--;
ca9e0a1b 756
3ee79d9f 757 if (!strncasecmp(value, "to: ", 4)) {
1d2f80fa 758 item = string_list_append(&extra_to, value + 4);
ca9e0a1b
SB
759 len -= 4;
760 } else if (!strncasecmp(value, "cc: ", 4)) {
1d2f80fa 761 item = string_list_append(&extra_cc, value + 4);
ca9e0a1b
SB
762 len -= 4;
763 } else {
1d2f80fa 764 item = string_list_append(&extra_hdr, value);
3ee79d9f 765 }
ca9e0a1b
SB
766
767 item->string[len] = '\0';
3ee79d9f
DB
768}
769
30984ed2
TR
770#define THREAD_SHALLOW 1
771#define THREAD_DEEP 2
6622d9c7
SB
772static int thread;
773static int do_signoff;
bb52995f 774static int base_auto;
6bc6b6c0 775static char *from;
6622d9c7 776static const char *signature = git_version_string;
7022650f 777static const char *signature_file;
2a4c2607 778static int config_cover_letter;
bc6bf2d7 779static const char *config_output_directory;
2a4c2607
FC
780
781enum {
782 COVER_UNSET,
783 COVER_OFF,
784 COVER_ON,
785 COVER_AUTO
786};
30984ed2 787
ef90d6d4 788static int git_format_config(const char *var, const char *value, void *cb)
20ff0680 789{
13cdf780
DL
790 struct rev_info *rev = cb;
791
20ff0680 792 if (!strcmp(var, "format.headers")) {
d7fb91c6 793 if (!value)
5c5f1d7c 794 die(_("format.headers without value"));
3ee79d9f 795 add_header(value);
20ff0680
JS
796 return 0;
797 }
70cff3ac
BH
798 if (!strcmp(var, "format.suffix"))
799 return git_config_string(&fmt_patch_suffix, var, value);
ae6c098f
SD
800 if (!strcmp(var, "format.to")) {
801 if (!value)
802 return config_error_nonbool(var);
1d2f80fa 803 string_list_append(&extra_to, value);
ae6c098f
SD
804 return 0;
805 }
fe8928e6
MV
806 if (!strcmp(var, "format.cc")) {
807 if (!value)
808 return config_error_nonbool(var);
1d2f80fa 809 string_list_append(&extra_cc, value);
fe8928e6
MV
810 return 0;
811 }
787570c7 812 if (!strcmp(var, "diff.color") || !strcmp(var, "color.diff") ||
339de508 813 !strcmp(var, "color.ui") || !strcmp(var, "diff.submodule")) {
f3aafa4d
RA
814 return 0;
815 }
49604a4d 816 if (!strcmp(var, "format.numbered")) {
90f5c186 817 if (value && !strcasecmp(value, "auto")) {
49604a4d
BG
818 auto_number = 1;
819 return 0;
820 }
49604a4d 821 numbered = git_config_bool(var, value);
a567fdcb 822 auto_number = auto_number && numbered;
49604a4d
BG
823 return 0;
824 }
0db5260b
JW
825 if (!strcmp(var, "format.attach")) {
826 if (value && *value)
827 default_attach = xstrdup(value);
828 else
829 default_attach = xstrdup(git_version_string);
830 return 0;
831 }
30984ed2
TR
832 if (!strcmp(var, "format.thread")) {
833 if (value && !strcasecmp(value, "deep")) {
834 thread = THREAD_DEEP;
835 return 0;
836 }
837 if (value && !strcasecmp(value, "shallow")) {
838 thread = THREAD_SHALLOW;
839 return 0;
840 }
841 thread = git_config_bool(var, value) && THREAD_SHALLOW;
842 return 0;
843 }
1d1876e9
HV
844 if (!strcmp(var, "format.signoff")) {
845 do_signoff = git_config_bool(var, value);
846 return 0;
847 }
6622d9c7
SB
848 if (!strcmp(var, "format.signature"))
849 return git_config_string(&signature, var, value);
7022650f
JM
850 if (!strcmp(var, "format.signaturefile"))
851 return git_config_pathname(&signature_file, var, value);
2a4c2607
FC
852 if (!strcmp(var, "format.coverletter")) {
853 if (value && !strcasecmp(value, "auto")) {
854 config_cover_letter = COVER_AUTO;
855 return 0;
856 }
857 config_cover_letter = git_config_bool(var, value) ? COVER_ON : COVER_OFF;
858 return 0;
859 }
bc6bf2d7
AK
860 if (!strcmp(var, "format.outputdirectory"))
861 return git_config_string(&config_output_directory, var, value);
bb52995f
XY
862 if (!strcmp(var, "format.useautobase")) {
863 base_auto = git_config_bool(var, value);
864 return 0;
865 }
6bc6b6c0 866 if (!strcmp(var, "format.from")) {
89576613 867 int b = git_parse_maybe_bool(value);
6bc6b6c0
JT
868 free(from);
869 if (b < 0)
870 from = xstrdup(value);
871 else if (b)
872 from = xstrdup(git_committer_info(IDENT_NO_DATE));
873 else
874 from = NULL;
875 return 0;
876 }
13cdf780
DL
877 if (!strcmp(var, "format.notes")) {
878 struct strbuf buf = STRBUF_INIT;
879 int b = git_parse_maybe_bool(value);
880 if (!b)
881 return 0;
882 rev->show_notes = 1;
883 if (b < 0) {
884 strbuf_addstr(&buf, value);
885 expand_notes_ref(&buf);
886 string_list_append(&rev->notes_opt.extra_notes_refs,
887 strbuf_detach(&buf, NULL));
888 } else {
889 rev->notes_opt.use_default_notes = 1;
890 }
891 return 0;
892 }
dbd21447 893
ef90d6d4 894 return git_log_config(var, value, cb);
20ff0680
JS
895}
896
efd02016 897static const char *output_directory = NULL;
9800a754 898static int outdir_offset;
81f3a188 899
95235f5b 900static int open_next_file(struct commit *commit, const char *subject,
a21c2f94 901 struct rev_info *rev, int quiet)
0377db77 902{
cd2ef591 903 struct strbuf filename = STRBUF_INIT;
68cb7b6f 904 int suffix_len = strlen(rev->patch_suffix) + 1;
0377db77 905
2448482b 906 if (output_directory) {
cd2ef591
SB
907 strbuf_addstr(&filename, output_directory);
908 if (filename.len >=
15d980a7
NTND
909 PATH_MAX - FORMAT_PATCH_NAME_MAX - suffix_len) {
910 strbuf_release(&filename);
5c5f1d7c 911 return error(_("name of output directory is too long"));
15d980a7 912 }
00b6c178 913 strbuf_complete(&filename, '/');
2448482b 914 }
0377db77 915
38ec23ac
JH
916 if (rev->numbered_files)
917 strbuf_addf(&filename, "%d", rev->nr);
d28b5d47
JH
918 else if (commit)
919 fmt_output_commit(&filename, commit, rev);
38ec23ac 920 else
d28b5d47 921 fmt_output_subject(&filename, subject, rev);
e6ff0f42 922
250087f2 923 if (!quiet)
36a4d905 924 printf("%s\n", filename.buf + outdir_offset);
ec2956df 925
15d980a7 926 if ((rev->diffopt.file = fopen(filename.buf, "w")) == NULL) {
bc208ae3 927 error_errno(_("cannot open patch file %s"), filename.buf);
15d980a7
NTND
928 strbuf_release(&filename);
929 return -1;
930 }
c06d2daa 931
cd2ef591 932 strbuf_release(&filename);
e6ff0f42 933 return 0;
0377db77
JS
934}
935
811929f1 936static void get_patch_ids(struct rev_info *rev, struct patch_ids *ids)
9c6efa36
JS
937{
938 struct rev_info check_rev;
9b7a61d7 939 struct commit *commit, *c1, *c2;
9c6efa36
JS
940 struct object *o1, *o2;
941 unsigned flags1, flags2;
9c6efa36
JS
942
943 if (rev->pending.nr != 2)
bc208ae3 944 die(_("need exactly one range"));
9c6efa36
JS
945
946 o1 = rev->pending.objects[0].item;
9c6efa36 947 o2 = rev->pending.objects[1].item;
9b7a61d7 948 flags1 = o1->flags;
9c6efa36 949 flags2 = o2->flags;
2122f675
SB
950 c1 = lookup_commit_reference(the_repository, &o1->oid);
951 c2 = lookup_commit_reference(the_repository, &o2->oid);
9c6efa36
JS
952
953 if ((flags1 & UNINTERESTING) == (flags2 & UNINTERESTING))
bc208ae3 954 die(_("not a range"));
9c6efa36 955
a7edadda 956 init_patch_ids(the_repository, ids);
9c6efa36
JS
957
958 /* given a range a..b get all patch ids for b..a */
2abf3503 959 repo_init_revisions(the_repository, &check_rev, rev->prefix);
51f4de3a 960 check_rev.max_parents = 1;
9c6efa36
JS
961 o1->flags ^= UNINTERESTING;
962 o2->flags ^= UNINTERESTING;
963 add_pending_object(&check_rev, o1, "o1");
964 add_pending_object(&check_rev, o2, "o2");
3d51e1b5 965 if (prepare_revision_walk(&check_rev))
5c5f1d7c 966 die(_("revision walk setup failed"));
9c6efa36
JS
967
968 while ((commit = get_revision(&check_rev)) != NULL) {
5d23e133 969 add_commit_patch_id(commit, ids);
9c6efa36
JS
970 }
971
972 /* reset for next revision walk */
9b7a61d7
JH
973 clear_commit_marks(c1, SEEN | UNINTERESTING | SHOWN | ADDED);
974 clear_commit_marks(c2, SEEN | UNINTERESTING | SHOWN | ADDED);
9c6efa36
JS
975 o1->flags = flags1;
976 o2->flags = flags2;
977}
978
e1a37346 979static void gen_message_id(struct rev_info *info, char *base)
d1566f78 980{
f285a2d7 981 struct strbuf buf = STRBUF_INIT;
cb71f8bd 982 strbuf_addf(&buf, "%s.%"PRItime".git.%s", base,
dddbad72 983 (timestamp_t) time(NULL),
59f9b8a9 984 git_committer_info(IDENT_NO_NAME|IDENT_NO_DATE|IDENT_STRICT));
e1a37346 985 info->message_id = strbuf_detach(&buf, NULL);
d1566f78
JT
986}
987
95235f5b 988static void print_signature(FILE *file)
6622d9c7 989{
c6076e2b
JK
990 if (!signature || !*signature)
991 return;
992
95235f5b 993 fprintf(file, "-- \n%s", signature);
c6076e2b 994 if (signature[strlen(signature)-1] != '\n')
95235f5b
JS
995 putc('\n', file);
996 putc('\n', file);
6622d9c7
SB
997}
998
739453a3
JH
999static void add_branch_description(struct strbuf *buf, const char *branch_name)
1000{
1001 struct strbuf desc = STRBUF_INIT;
1002 if (!branch_name || !*branch_name)
1003 return;
1004 read_branch_desc(&desc, branch_name);
1005 if (desc.len) {
1006 strbuf_addch(buf, '\n');
e992d1eb 1007 strbuf_addbuf(buf, &desc);
739453a3
JH
1008 strbuf_addch(buf, '\n');
1009 }
94204bf5 1010 strbuf_release(&desc);
739453a3
JH
1011}
1012
d7ddad01
FC
1013static char *find_branch_name(struct rev_info *rev)
1014{
1015 int i, positive = -1;
f2fd0760 1016 struct object_id branch_oid;
1017 const struct object_id *tip_oid;
95b567c7 1018 const char *ref, *v;
d7ddad01
FC
1019 char *full_ref, *branch = NULL;
1020
1021 for (i = 0; i < rev->cmdline.nr; i++) {
1022 if (rev->cmdline.rev[i].flags & UNINTERESTING)
1023 continue;
1024 if (positive < 0)
1025 positive = i;
1026 else
1027 return NULL;
1028 }
1029 if (positive < 0)
1030 return NULL;
1031 ref = rev->cmdline.rev[positive].name;
f2fd0760 1032 tip_oid = &rev->cmdline.rev[positive].item->oid;
cca5fa64 1033 if (dwim_ref(ref, strlen(ref), &branch_oid, &full_ref) &&
95b567c7 1034 skip_prefix(full_ref, "refs/heads/", &v) &&
4a7e27e9 1035 oideq(tip_oid, &branch_oid))
95b567c7 1036 branch = xstrdup(v);
d7ddad01
FC
1037 free(full_ref);
1038 return branch;
1039}
1040
fa5b7ea6
ES
1041static void show_diffstat(struct rev_info *rev,
1042 struct commit *origin, struct commit *head)
1043{
1044 struct diff_options opts;
1045
1046 memcpy(&opts, &rev->diffopt, sizeof(opts));
1047 opts.output_format = DIFF_FORMAT_SUMMARY | DIFF_FORMAT_DIFFSTAT;
fa5b7ea6
ES
1048 diff_setup_done(&opts);
1049
1050 diff_tree_oid(get_commit_tree_oid(origin),
1051 get_commit_tree_oid(head),
1052 "", &opts);
1053 diffcore_std(&opts);
1054 diff_flush(&opts);
1055
1056 fprintf(rev->diffopt.file, "\n");
1057}
1058
2bda2cf4 1059static void make_cover_letter(struct rev_info *rev, int use_stdout,
2bda2cf4 1060 struct commit *origin,
d7ddad01 1061 int nr, struct commit **list,
739453a3 1062 const char *branch_name,
250087f2 1063 int quiet)
a5a27c79
DB
1064{
1065 const char *committer;
a5a27c79
DB
1066 const char *body = "*** SUBJECT HERE ***\n\n*** BLURB HERE ***\n";
1067 const char *msg;
2bda2cf4 1068 struct shortlog log;
f285a2d7 1069 struct strbuf sb = STRBUF_INIT;
2bda2cf4 1070 int i;
330db18c 1071 const char *encoding = "UTF-8";
267123b4 1072 int need_8bit_cte = 0;
6bf13944 1073 struct pretty_print_context pp = {0};
d7ddad01 1074 struct commit *head = list[0];
a5a27c79 1075
9f23e040 1076 if (!cmit_fmt_is_mail(rev->commit_format))
bc208ae3 1077 die(_("cover letter needs email format"));
a5a27c79 1078
cd2ef591 1079 committer = git_committer_info(0);
6df514af 1080
a21c2f94 1081 if (!use_stdout &&
95235f5b 1082 open_next_file(NULL, rev->numbered_files ? NULL : "cover-letter", rev, quiet))
2fe95f49 1083 die(_("failed to create cover-letter file"));
a5a27c79 1084
50cd54ef 1085 log_write_email_headers(rev, head, &pp.after_subject, &need_8bit_cte, 0);
a5a27c79 1086
bc6b8fc1 1087 for (i = 0; !need_8bit_cte && i < nr; i++) {
8597ea3a 1088 const char *buf = get_commit_buffer(list[i], NULL);
bc6b8fc1 1089 if (has_non_ascii(buf))
0a7f4483 1090 need_8bit_cte = 1;
bc6b8fc1
JK
1091 unuse_commit_buffer(list[i], buf);
1092 }
0a7f4483 1093
d7ddad01
FC
1094 if (!branch_name)
1095 branch_name = find_branch_name(rev);
1096
a5a27c79 1097 msg = body;
6bf13944 1098 pp.fmt = CMIT_FMT_EMAIL;
a5481a6c 1099 pp.date_mode.type = DATE_RFC2822;
6d167fd7
RS
1100 pp.rev = rev;
1101 pp.print_email_subject = 1;
6bf13944
JK
1102 pp_user_info(&pp, NULL, &sb, committer, encoding);
1103 pp_title_line(&pp, &msg, &sb, encoding, need_8bit_cte);
1104 pp_remainder(&pp, &msg, &sb, 0);
739453a3 1105 add_branch_description(&sb, branch_name);
95235f5b 1106 fprintf(rev->diffopt.file, "%s\n", sb.buf);
a5a27c79
DB
1107
1108 strbuf_release(&sb);
1109
2bda2cf4 1110 shortlog_init(&log);
859c4fbe 1111 log.wrap_lines = 1;
43662b23 1112 log.wrap = MAIL_DEFAULT_WRAP;
859c4fbe
JS
1113 log.in1 = 2;
1114 log.in2 = 4;
95235f5b 1115 log.file = rev->diffopt.file;
2bda2cf4
DB
1116 for (i = 0; i < nr; i++)
1117 shortlog_add_commit(&log, list[i]);
1118
1119 shortlog_output(&log);
1120
fa5b7ea6
ES
1121 /* We can only do diffstat with a unique reference point */
1122 if (origin)
1123 show_diffstat(rev, origin, head);
a5a27c79 1124
126facf8 1125 if (rev->idiff_oid1) {
5ac290f9 1126 fprintf_ln(rev->diffopt.file, "%s", rev->idiff_title);
3b026417 1127 show_interdiff(rev, 0);
126facf8 1128 }
31e2617a
ES
1129
1130 if (rev->rdiff1) {
ac0edf1f
1131 /*
1132 * Pass minimum required diff-options to range-diff; others
1133 * can be added later if deemed desirable.
1134 */
1135 struct diff_options opts;
1136 diff_setup(&opts);
1137 opts.file = rev->diffopt.file;
1138 opts.use_color = rev->diffopt.use_color;
1139 diff_setup_done(&opts);
4ee99689 1140 fprintf_ln(rev->diffopt.file, "%s", rev->rdiff_title);
31e2617a 1141 show_range_diff(rev->rdiff1, rev->rdiff2,
ac0edf1f 1142 rev->creation_factor, 1, &opts);
31e2617a 1143 }
d1566f78
JT
1144}
1145
8419d2ee
JH
1146static const char *clean_message_id(const char *msg_id)
1147{
1148 char ch;
1149 const char *a, *z, *m;
8419d2ee
JH
1150
1151 m = msg_id;
1152 while ((ch = *m) && (isspace(ch) || (ch == '<')))
1153 m++;
1154 a = m;
1155 z = NULL;
1156 while ((ch = *m)) {
1157 if (!isspace(ch) && (ch != '>'))
1158 z = m;
1159 m++;
1160 }
1161 if (!z)
5c5f1d7c 1162 die(_("insane in-reply-to: %s"), msg_id);
8419d2ee
JH
1163 if (++z == m)
1164 return a;
182af834 1165 return xmemdupz(a, z - a);
8419d2ee
JH
1166}
1167
9800a754
JH
1168static const char *set_outdir(const char *prefix, const char *output_directory)
1169{
1170 if (output_directory && is_absolute_path(output_directory))
1171 return output_directory;
1172
1173 if (!prefix || !*prefix) {
1174 if (output_directory)
1175 return output_directory;
1176 /* The user did not explicitly ask for "./" */
1177 outdir_offset = 2;
1178 return "./";
1179 }
1180
1181 outdir_offset = strlen(prefix);
1182 if (!output_directory)
1183 return prefix;
1184
e4da43b1 1185 return prefix_filename(prefix, output_directory);
9800a754
JH
1186}
1187
fff02ee6 1188static const char * const builtin_format_patch_usage[] = {
9c9b4f2f 1189 N_("git format-patch [<options>] [<since> | <revision-range>]"),
fff02ee6
SB
1190 NULL
1191};
1192
1193static int keep_subject = 0;
1194
1195static int keep_callback(const struct option *opt, const char *arg, int unset)
1196{
517fe807
JK
1197 BUG_ON_OPT_NEG(unset);
1198 BUG_ON_OPT_ARG(arg);
fff02ee6
SB
1199 ((struct rev_info *)opt->value)->total = -1;
1200 keep_subject = 1;
1201 return 0;
1202}
1203
1204static int subject_prefix = 0;
1205
1206static int subject_prefix_callback(const struct option *opt, const char *arg,
1207 int unset)
1208{
517fe807 1209 BUG_ON_OPT_NEG(unset);
fff02ee6
SB
1210 subject_prefix = 1;
1211 ((struct rev_info *)opt->value)->subject_prefix = arg;
1212 return 0;
1213}
1214
68e83a5b
JT
1215static int rfc_callback(const struct option *opt, const char *arg, int unset)
1216{
517fe807
JK
1217 BUG_ON_OPT_NEG(unset);
1218 BUG_ON_OPT_ARG(arg);
68e83a5b
JT
1219 return subject_prefix_callback(opt, "RFC PATCH", unset);
1220}
1221
1af4731b
JH
1222static int numbered_cmdline_opt = 0;
1223
fff02ee6
SB
1224static int numbered_callback(const struct option *opt, const char *arg,
1225 int unset)
1226{
517fe807 1227 BUG_ON_OPT_ARG(arg);
1af4731b 1228 *(int *)opt->value = numbered_cmdline_opt = unset ? 0 : 1;
fff02ee6
SB
1229 if (unset)
1230 auto_number = 0;
1231 return 0;
1232}
1233
1234static int no_numbered_callback(const struct option *opt, const char *arg,
1235 int unset)
1236{
517fe807 1237 BUG_ON_OPT_NEG(unset);
fff02ee6
SB
1238 return numbered_callback(opt, arg, 1);
1239}
1240
1241static int output_directory_callback(const struct option *opt, const char *arg,
1242 int unset)
1243{
1244 const char **dir = (const char **)opt->value;
517fe807 1245 BUG_ON_OPT_NEG(unset);
fff02ee6 1246 if (*dir)
bc208ae3 1247 die(_("two output directories?"));
fff02ee6
SB
1248 *dir = arg;
1249 return 0;
1250}
1251
1252static int thread_callback(const struct option *opt, const char *arg, int unset)
1253{
1254 int *thread = (int *)opt->value;
1255 if (unset)
1256 *thread = 0;
1257 else if (!arg || !strcmp(arg, "shallow"))
1258 *thread = THREAD_SHALLOW;
1259 else if (!strcmp(arg, "deep"))
1260 *thread = THREAD_DEEP;
5a59a230
NTND
1261 /*
1262 * Please update _git_formatpatch() in git-completion.bash
1263 * when you add new options.
1264 */
fff02ee6
SB
1265 else
1266 return 1;
1267 return 0;
1268}
1269
1270static int attach_callback(const struct option *opt, const char *arg, int unset)
1271{
1272 struct rev_info *rev = (struct rev_info *)opt->value;
1273 if (unset)
1274 rev->mime_boundary = NULL;
1275 else if (arg)
1276 rev->mime_boundary = arg;
1277 else
1278 rev->mime_boundary = git_version_string;
1279 rev->no_inline = unset ? 0 : 1;
1280 return 0;
1281}
1282
1283static int inline_callback(const struct option *opt, const char *arg, int unset)
1284{
1285 struct rev_info *rev = (struct rev_info *)opt->value;
1286 if (unset)
1287 rev->mime_boundary = NULL;
1288 else if (arg)
1289 rev->mime_boundary = arg;
1290 else
1291 rev->mime_boundary = git_version_string;
1292 rev->no_inline = 0;
1293 return 0;
1294}
1295
1296static int header_callback(const struct option *opt, const char *arg, int unset)
1297{
c4260034
SB
1298 if (unset) {
1299 string_list_clear(&extra_hdr, 0);
1300 string_list_clear(&extra_to, 0);
1301 string_list_clear(&extra_cc, 0);
1302 } else {
1303 add_header(arg);
1304 }
fff02ee6
SB
1305 return 0;
1306}
1307
ae6c098f
SD
1308static int to_callback(const struct option *opt, const char *arg, int unset)
1309{
c4260034
SB
1310 if (unset)
1311 string_list_clear(&extra_to, 0);
1312 else
1d2f80fa 1313 string_list_append(&extra_to, arg);
fff02ee6
SB
1314 return 0;
1315}
1316
1317static int cc_callback(const struct option *opt, const char *arg, int unset)
1318{
c4260034
SB
1319 if (unset)
1320 string_list_clear(&extra_cc, 0);
1321 else
1d2f80fa 1322 string_list_append(&extra_cc, arg);
fff02ee6
SB
1323 return 0;
1324}
1325
a9080475
JK
1326static int from_callback(const struct option *opt, const char *arg, int unset)
1327{
1328 char **from = opt->value;
1329
1330 free(*from);
1331
1332 if (unset)
1333 *from = NULL;
1334 else if (arg)
1335 *from = xstrdup(arg);
1336 else
1337 *from = xstrdup(git_committer_info(IDENT_NO_DATE));
1338 return 0;
1339}
1340
fa2ab86d
XY
1341struct base_tree_info {
1342 struct object_id base_commit;
1343 int nr_patch_id, alloc_patch_id;
1344 struct object_id *patch_id;
1345};
1346
1347static struct commit *get_base_commit(const char *base_commit,
1348 struct commit **list,
1349 int total)
1350{
1351 struct commit *base = NULL;
1352 struct commit **rev;
1353 int i = 0, rev_nr = 0;
1354
bb52995f
XY
1355 if (base_commit && strcmp(base_commit, "auto")) {
1356 base = lookup_commit_reference_by_name(base_commit);
1357 if (!base)
bc208ae3 1358 die(_("unknown commit %s"), base_commit);
bb52995f 1359 } else if ((base_commit && !strcmp(base_commit, "auto")) || base_auto) {
3de66517
XY
1360 struct branch *curr_branch = branch_get(NULL);
1361 const char *upstream = branch_get_upstream(curr_branch, NULL);
1362 if (upstream) {
1363 struct commit_list *base_list;
1364 struct commit *commit;
d801627b 1365 struct object_id oid;
3de66517 1366
d801627b 1367 if (get_oid(upstream, &oid))
bc208ae3 1368 die(_("failed to resolve '%s' as a valid ref"), upstream);
bc83266a 1369 commit = lookup_commit_or_die(&oid, "upstream base");
3de66517
XY
1370 base_list = get_merge_bases_many(commit, total, list);
1371 /* There should be one and only one merge base. */
1372 if (!base_list || base_list->next)
bc208ae3 1373 die(_("could not find exact merge base"));
3de66517
XY
1374 base = base_list->item;
1375 free_commit_list(base_list);
1376 } else {
bc208ae3 1377 die(_("failed to get upstream, if you want to record base commit automatically,\n"
3de66517 1378 "please use git branch --set-upstream-to to track a remote branch.\n"
bc208ae3 1379 "Or you could specify base commit by --base=<base-commit-id> manually"));
3de66517 1380 }
3de66517 1381 }
fa2ab86d
XY
1382
1383 ALLOC_ARRAY(rev, total);
1384 for (i = 0; i < total; i++)
1385 rev[i] = list[i];
1386
1387 rev_nr = total;
1388 /*
1389 * Get merge base through pair-wise computations
1390 * and store it in rev[0].
1391 */
1392 while (rev_nr > 1) {
1393 for (i = 0; i < rev_nr / 2; i++) {
1394 struct commit_list *merge_base;
1395 merge_base = get_merge_bases(rev[2 * i], rev[2 * i + 1]);
1396 if (!merge_base || merge_base->next)
bc208ae3 1397 die(_("failed to find exact merge base"));
fa2ab86d
XY
1398
1399 rev[i] = merge_base->item;
1400 }
1401
1402 if (rev_nr % 2)
1403 rev[i] = rev[2 * i];
42c78a21 1404 rev_nr = DIV_ROUND_UP(rev_nr, 2);
fa2ab86d
XY
1405 }
1406
1407 if (!in_merge_bases(base, rev[0]))
1408 die(_("base commit should be the ancestor of revision list"));
1409
1410 for (i = 0; i < total; i++) {
1411 if (base == list[i])
1412 die(_("base commit shouldn't be in revision list"));
1413 }
1414
1415 free(rev);
1416 return base;
1417}
1418
7b680d32
NTND
1419define_commit_slab(commit_base, int);
1420
fa2ab86d
XY
1421static void prepare_bases(struct base_tree_info *bases,
1422 struct commit *base,
1423 struct commit **list,
1424 int total)
1425{
1426 struct commit *commit;
1427 struct rev_info revs;
1428 struct diff_options diffopt;
7b680d32 1429 struct commit_base commit_base;
fa2ab86d
XY
1430 int i;
1431
1432 if (!base)
1433 return;
1434
7b680d32 1435 init_commit_base(&commit_base);
e6757652 1436 repo_diff_setup(the_repository, &diffopt);
0d1e0e78 1437 diffopt.flags.recursive = 1;
fa2ab86d
XY
1438 diff_setup_done(&diffopt);
1439
1440 oidcpy(&bases->base_commit, &base->object.oid);
1441
2abf3503 1442 repo_init_revisions(the_repository, &revs, NULL);
fa2ab86d
XY
1443 revs.max_parents = 1;
1444 revs.topo_order = 1;
1445 for (i = 0; i < total; i++) {
1446 list[i]->object.flags &= ~UNINTERESTING;
1447 add_pending_object(&revs, &list[i]->object, "rev_list");
7b680d32 1448 *commit_base_at(&commit_base, list[i]) = 1;
fa2ab86d
XY
1449 }
1450 base->object.flags |= UNINTERESTING;
1451 add_pending_object(&revs, &base->object, "base");
1452
1453 if (prepare_revision_walk(&revs))
1454 die(_("revision walk setup failed"));
1455 /*
1456 * Traverse the commits list, get prerequisite patch ids
1457 * and stuff them in bases structure.
1458 */
1459 while ((commit = get_revision(&revs)) != NULL) {
d801627b 1460 struct object_id oid;
fa2ab86d 1461 struct object_id *patch_id;
7b680d32 1462 if (*commit_base_at(&commit_base, commit))
fa2ab86d 1463 continue;
a8f6855f 1464 if (commit_patch_id(commit, &diffopt, &oid, 0, 1))
fa2ab86d
XY
1465 die(_("cannot get patch id"));
1466 ALLOC_GROW(bases->patch_id, bases->nr_patch_id + 1, bases->alloc_patch_id);
1467 patch_id = bases->patch_id + bases->nr_patch_id;
d801627b 1468 oidcpy(patch_id, &oid);
fa2ab86d
XY
1469 bases->nr_patch_id++;
1470 }
7b680d32 1471 clear_commit_base(&commit_base);
fa2ab86d
XY
1472}
1473
95235f5b 1474static void print_bases(struct base_tree_info *bases, FILE *file)
fa2ab86d
XY
1475{
1476 int i;
1477
1478 /* Only do this once, either for the cover or for the first one */
1479 if (is_null_oid(&bases->base_commit))
1480 return;
1481
1482 /* Show the base commit */
480871e0 1483 fprintf(file, "\nbase-commit: %s\n", oid_to_hex(&bases->base_commit));
fa2ab86d
XY
1484
1485 /* Show the prerequisite patches */
1486 for (i = bases->nr_patch_id - 1; i >= 0; i--)
95235f5b 1487 fprintf(file, "prerequisite-patch-id: %s\n", oid_to_hex(&bases->patch_id[i]));
fa2ab86d
XY
1488
1489 free(bases->patch_id);
1490 bases->nr_patch_id = 0;
1491 bases->alloc_patch_id = 0;
1492 oidclr(&bases->base_commit);
1493}
1494
5ac290f9
ES
1495static const char *diff_title(struct strbuf *sb, int reroll_count,
1496 const char *generic, const char *rerolled)
1497{
1498 if (reroll_count <= 0)
1499 strbuf_addstr(sb, generic);
1500 else /* RFC may be v0, so allow -v1 to diff against v0 */
1501 strbuf_addf(sb, rerolled, reroll_count - 1);
1502 return sb->buf;
1503}
1504
31e2617a
ES
1505static void infer_range_diff_ranges(struct strbuf *r1,
1506 struct strbuf *r2,
1507 const char *prev,
2e6fd71a 1508 struct commit *origin,
31e2617a
ES
1509 struct commit *head)
1510{
1511 const char *head_oid = oid_to_hex(&head->object.oid);
1512
2e6fd71a
ES
1513 if (!strstr(prev, "..")) {
1514 strbuf_addf(r1, "%s..%s", head_oid, prev);
1515 strbuf_addf(r2, "%s..%s", prev, head_oid);
1516 } else if (!origin) {
1517 die(_("failed to infer range-diff ranges"));
1518 } else {
1519 strbuf_addstr(r1, prev);
1520 strbuf_addf(r2, "%s..%s",
1521 oid_to_hex(&origin->object.oid), head_oid);
1522 }
31e2617a
ES
1523}
1524
a633fca0 1525int cmd_format_patch(int argc, const char **argv, const char *prefix)
91efcf60
JH
1526{
1527 struct commit *commit;
1528 struct commit **list = NULL;
1529 struct rev_info rev;
32962c9b 1530 struct setup_revision_opt s_r_opt;
fff02ee6 1531 int nr = 0, total, i;
0377db77 1532 int use_stdout = 0;
fa0f02df 1533 int start_number = -1;
ce37596c 1534 int just_numbers = 0;
9c6efa36 1535 int ignore_if_in_upstream = 0;
2a4c2607 1536 int cover_letter = -1;
2bda2cf4 1537 int boundary_count = 0;
37c22a4b 1538 int no_binary_diff = 0;
3a30aa17 1539 int zero_commit = 0;
d7ddad01 1540 struct commit *origin = NULL;
76af0734 1541 const char *in_reply_to = NULL;
5d23e133 1542 struct patch_ids ids;
f285a2d7 1543 struct strbuf buf = STRBUF_INIT;
1d46f2ea 1544 int use_patch_format = 0;
250087f2 1545 int quiet = 0;
5fe10fe8 1546 int reroll_count = -1;
739453a3 1547 char *branch_name = NULL;
fa2ab86d
XY
1548 char *base_commit = NULL;
1549 struct base_tree_info bases;
738e88a2
KW
1550 int show_progress = 0;
1551 struct progress *progress = NULL;
126facf8 1552 struct oid_array idiff_prev = OID_ARRAY_INIT;
5ac290f9 1553 struct strbuf idiff_title = STRBUF_INIT;
31e2617a
ES
1554 const char *rdiff_prev = NULL;
1555 struct strbuf rdiff1 = STRBUF_INIT;
1556 struct strbuf rdiff2 = STRBUF_INIT;
4ee99689 1557 struct strbuf rdiff_title = STRBUF_INIT;
8631bf1c 1558 int creation_factor = -1;
fa2ab86d 1559
fff02ee6
SB
1560 const struct option builtin_format_patch_options[] = {
1561 { OPTION_CALLBACK, 'n', "numbered", &numbered, NULL,
61007a58 1562 N_("use [PATCH n/m] even with a single patch"),
fff02ee6
SB
1563 PARSE_OPT_NOARG, numbered_callback },
1564 { OPTION_CALLBACK, 'N', "no-numbered", &numbered, NULL,
61007a58 1565 N_("use [PATCH] even with multiple patches"),
964fd83b 1566 PARSE_OPT_NOARG | PARSE_OPT_NONEG, no_numbered_callback },
aa089cd9
FC
1567 OPT_BOOL('s', "signoff", &do_signoff, N_("add Signed-off-by:")),
1568 OPT_BOOL(0, "stdout", &use_stdout,
61007a58 1569 N_("print patches to standard out")),
aa089cd9 1570 OPT_BOOL(0, "cover-letter", &cover_letter,
61007a58 1571 N_("generate a cover letter")),
aa089cd9 1572 OPT_BOOL(0, "numbered-files", &just_numbers,
61007a58
NTND
1573 N_("use simple number sequence for output file names")),
1574 OPT_STRING(0, "suffix", &fmt_patch_suffix, N_("sfx"),
1575 N_("use <sfx> instead of '.patch'")),
fff02ee6 1576 OPT_INTEGER(0, "start-number", &start_number,
61007a58 1577 N_("start numbering patches at <n> instead of 1")),
7952ea66 1578 OPT_INTEGER('v', "reroll-count", &reroll_count,
5fe10fe8 1579 N_("mark the series as Nth re-roll")),
68e83a5b
JT
1580 { OPTION_CALLBACK, 0, "rfc", &rev, NULL,
1581 N_("Use [RFC PATCH] instead of [PATCH]"),
1582 PARSE_OPT_NOARG | PARSE_OPT_NONEG, rfc_callback },
61007a58
NTND
1583 { OPTION_CALLBACK, 0, "subject-prefix", &rev, N_("prefix"),
1584 N_("Use [<prefix>] instead of [PATCH]"),
fff02ee6
SB
1585 PARSE_OPT_NONEG, subject_prefix_callback },
1586 { OPTION_CALLBACK, 'o', "output-directory", &output_directory,
61007a58 1587 N_("dir"), N_("store resulting files in <dir>"),
fff02ee6
SB
1588 PARSE_OPT_NONEG, output_directory_callback },
1589 { OPTION_CALLBACK, 'k', "keep-subject", &rev, NULL,
61007a58 1590 N_("don't strip/add [PATCH]"),
fff02ee6 1591 PARSE_OPT_NOARG | PARSE_OPT_NONEG, keep_callback },
d5d09d47
SB
1592 OPT_BOOL(0, "no-binary", &no_binary_diff,
1593 N_("don't output binary diffs")),
3a30aa17 1594 OPT_BOOL(0, "zero-commit", &zero_commit,
1595 N_("output all-zero hash in From header")),
d5d09d47
SB
1596 OPT_BOOL(0, "ignore-if-in-upstream", &ignore_if_in_upstream,
1597 N_("don't include a patch matching a commit upstream")),
3e4a67b4
NTND
1598 OPT_SET_INT_F('p', "no-stat", &use_patch_format,
1599 N_("show patch format instead of default (patch + stat)"),
1600 1, PARSE_OPT_NONEG),
61007a58
NTND
1601 OPT_GROUP(N_("Messaging")),
1602 { OPTION_CALLBACK, 0, "add-header", NULL, N_("header"),
1603 N_("add email header"), 0, header_callback },
1604 { OPTION_CALLBACK, 0, "to", NULL, N_("email"), N_("add To: header"),
c4260034 1605 0, to_callback },
61007a58 1606 { OPTION_CALLBACK, 0, "cc", NULL, N_("email"), N_("add Cc: header"),
c4260034 1607 0, cc_callback },
a9080475
JK
1608 { OPTION_CALLBACK, 0, "from", &from, N_("ident"),
1609 N_("set From address to <ident> (or committer ident if absent)"),
1610 PARSE_OPT_OPTARG, from_callback },
61007a58
NTND
1611 OPT_STRING(0, "in-reply-to", &in_reply_to, N_("message-id"),
1612 N_("make first mail a reply to <message-id>")),
1613 { OPTION_CALLBACK, 0, "attach", &rev, N_("boundary"),
1614 N_("attach the patch"), PARSE_OPT_OPTARG,
fff02ee6 1615 attach_callback },
61007a58
NTND
1616 { OPTION_CALLBACK, 0, "inline", &rev, N_("boundary"),
1617 N_("inline the patch"),
fff02ee6
SB
1618 PARSE_OPT_OPTARG | PARSE_OPT_NONEG,
1619 inline_callback },
61007a58
NTND
1620 { OPTION_CALLBACK, 0, "thread", &thread, N_("style"),
1621 N_("enable message threading, styles: shallow, deep"),
fff02ee6 1622 PARSE_OPT_OPTARG, thread_callback },
61007a58
NTND
1623 OPT_STRING(0, "signature", &signature, N_("signature"),
1624 N_("add a signature")),
fa2ab86d
XY
1625 OPT_STRING(0, "base", &base_commit, N_("base-commit"),
1626 N_("add prerequisite tree info to the patch series")),
7022650f
JM
1627 OPT_FILENAME(0, "signature-file", &signature_file,
1628 N_("add a signature from a file")),
b7df098c 1629 OPT__QUIET(&quiet, N_("don't print the patch filenames")),
738e88a2
KW
1630 OPT_BOOL(0, "progress", &show_progress,
1631 N_("show progress while generating patches")),
126facf8 1632 OPT_CALLBACK(0, "interdiff", &idiff_prev, N_("rev"),
ee6cbf71 1633 N_("show changes against <rev> in cover letter or single patch"),
126facf8 1634 parse_opt_object_name),
31e2617a 1635 OPT_STRING(0, "range-diff", &rdiff_prev, N_("refspec"),
40ce4160 1636 N_("show changes against <refspec> in cover letter or single patch")),
8631bf1c
ES
1637 OPT_INTEGER(0, "creation-factor", &creation_factor,
1638 N_("percentage by which creation is weighted")),
fff02ee6
SB
1639 OPT_END()
1640 };
91efcf60 1641
ca9e0a1b
SB
1642 extra_hdr.strdup_strings = 1;
1643 extra_to.strdup_strings = 1;
1644 extra_cc.strdup_strings = 1;
9501d191 1645 init_log_defaults();
2abf3503 1646 repo_init_revisions(the_repository, &rev, prefix);
13cdf780 1647 git_config(git_format_config, &rev);
91efcf60 1648 rev.commit_format = CMIT_FMT_EMAIL;
0893eec8 1649 rev.expand_tabs_in_log_default = 0;
91efcf60
JH
1650 rev.verbose_header = 1;
1651 rev.diff = 1;
ad5aeede 1652 rev.max_parents = 1;
0d1e0e78 1653 rev.diffopt.flags.recursive = 1;
dbd21447 1654 rev.subject_prefix = fmt_patch_subject_prefix;
32962c9b
JH
1655 memset(&s_r_opt, 0, sizeof(s_r_opt));
1656 s_r_opt.def = "HEAD";
d5f6b1d7 1657 s_r_opt.revarg_opt = REVARG_COMMITTISH;
20ff0680 1658
0db5260b
JW
1659 if (default_attach) {
1660 rev.mime_boundary = default_attach;
1661 rev.no_inline = 1;
1662 }
1663
2448482b
JS
1664 /*
1665 * Parse the arguments before setup_revisions(), or something
2dc189a3 1666 * like "git format-patch -o a123 HEAD^.." may fail; a123 is
2448482b
JS
1667 * possibly a valid SHA1.
1668 */
37782920 1669 argc = parse_options(argc, argv, prefix, builtin_format_patch_options,
fff02ee6 1670 builtin_format_patch_usage,
382da402
FC
1671 PARSE_OPT_KEEP_ARGV0 | PARSE_OPT_KEEP_UNKNOWN |
1672 PARSE_OPT_KEEP_DASHDASH);
2448482b 1673
5fe10fe8
JH
1674 if (0 < reroll_count) {
1675 struct strbuf sprefix = STRBUF_INIT;
1676 strbuf_addf(&sprefix, "%s v%d",
1677 rev.subject_prefix, reroll_count);
1678 rev.reroll_count = reroll_count;
1679 rev.subject_prefix = strbuf_detach(&sprefix, NULL);
1680 }
1681
ca9e0a1b
SB
1682 for (i = 0; i < extra_hdr.nr; i++) {
1683 strbuf_addstr(&buf, extra_hdr.items[i].string);
3ee79d9f
DB
1684 strbuf_addch(&buf, '\n');
1685 }
1686
ca9e0a1b 1687 if (extra_to.nr)
3ee79d9f 1688 strbuf_addstr(&buf, "To: ");
ca9e0a1b 1689 for (i = 0; i < extra_to.nr; i++) {
3ee79d9f
DB
1690 if (i)
1691 strbuf_addstr(&buf, " ");
ca9e0a1b
SB
1692 strbuf_addstr(&buf, extra_to.items[i].string);
1693 if (i + 1 < extra_to.nr)
3ee79d9f
DB
1694 strbuf_addch(&buf, ',');
1695 strbuf_addch(&buf, '\n');
1696 }
1697
ca9e0a1b 1698 if (extra_cc.nr)
3ee79d9f 1699 strbuf_addstr(&buf, "Cc: ");
ca9e0a1b 1700 for (i = 0; i < extra_cc.nr; i++) {
3ee79d9f
DB
1701 if (i)
1702 strbuf_addstr(&buf, " ");
ca9e0a1b
SB
1703 strbuf_addstr(&buf, extra_cc.items[i].string);
1704 if (i + 1 < extra_cc.nr)
3ee79d9f
DB
1705 strbuf_addch(&buf, ',');
1706 strbuf_addch(&buf, '\n');
1707 }
1708
2af202be 1709 rev.extra_headers = strbuf_detach(&buf, NULL);
3ee79d9f 1710
a9080475
JK
1711 if (from) {
1712 if (split_ident_line(&rev.from_ident, from, strlen(from)))
1713 die(_("invalid ident line: %s"), from);
1714 }
1715
add5c8a5 1716 if (start_number < 0)
fa0f02df 1717 start_number = 1;
ca6b91d2
JM
1718
1719 /*
1720 * If numbered is set solely due to format.numbered in config,
1721 * and it would conflict with --keep-subject (-k) from the
1722 * command line, reset "numbered".
1723 */
1724 if (numbered && keep_subject && !numbered_cmdline_opt)
1725 numbered = 0;
1726
63b398a4 1727 if (numbered && keep_subject)
1a07e59c 1728 die(_("-n and -k are mutually exclusive"));
2d9e4a47 1729 if (keep_subject && subject_prefix)
1a07e59c 1730 die(_("--subject-prefix/--rfc and -k are mutually exclusive"));
9553d2b2 1731 rev.preserve_subject = keep_subject;
8ac80a57 1732
32962c9b 1733 argc = setup_revisions(argc, argv, &rev, &s_r_opt);
2448482b 1734 if (argc > 1)
1a07e59c 1735 die(_("unrecognized argument: %s"), argv[1]);
0377db77 1736
02bc5b03 1737 if (rev.diffopt.output_format & DIFF_FORMAT_NAME)
f338cb83 1738 die(_("--name-only does not make sense"));
02bc5b03 1739 if (rev.diffopt.output_format & DIFF_FORMAT_NAME_STATUS)
f338cb83 1740 die(_("--name-status does not make sense"));
02bc5b03 1741 if (rev.diffopt.output_format & DIFF_FORMAT_CHECKDIFF)
f338cb83 1742 die(_("--check does not make sense"));
02bc5b03
BG
1743
1744 if (!use_patch_format &&
1745 (!rev.diffopt.output_format ||
1746 rev.diffopt.output_format == DIFF_FORMAT_PATCH))
1747 rev.diffopt.output_format = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_SUMMARY;
071dd0ba
NTND
1748 if (!rev.diffopt.stat_width)
1749 rev.diffopt.stat_width = MAIL_DEFAULT_WRAP;
02bc5b03
BG
1750
1751 /* Always generate a patch */
1752 rev.diffopt.output_format |= DIFF_FORMAT_PATCH;
c9b5ef99 1753
3a30aa17 1754 rev.zero_commit = zero_commit;
1755
0d1e0e78
BW
1756 if (!rev.diffopt.flags.text && !no_binary_diff)
1757 rev.diffopt.flags.binary = 1;
e47f306d 1758
894a9d33
TR
1759 if (rev.show_notes)
1760 init_display_notes(&rev.notes_opt);
1761
bc6bf2d7
AK
1762 if (!output_directory && !use_stdout)
1763 output_directory = config_output_directory;
1764
9800a754
JH
1765 if (!use_stdout)
1766 output_directory = set_outdir(prefix, output_directory);
38a94bb6
TRC
1767 else
1768 setup_pager();
77e565d8 1769
efd02016 1770 if (output_directory) {
11f4eb19
JS
1771 if (rev.diffopt.use_color != GIT_COLOR_ALWAYS)
1772 rev.diffopt.use_color = GIT_COLOR_NEVER;
efd02016 1773 if (use_stdout)
5c5f1d7c 1774 die(_("standard output, or directory, which one?"));
efd02016 1775 if (mkdir(output_directory, 0777) < 0 && errno != EEXIST)
bc208ae3 1776 die_errno(_("could not create directory '%s'"),
0721c314 1777 output_directory);
efd02016
JH
1778 }
1779
1f1e895f 1780 if (rev.pending.nr == 1) {
427a8ec5
FC
1781 int check_head = 0;
1782
8a1d076e
JH
1783 if (rev.max_count < 0 && !rev.show_root_diff) {
1784 /*
1785 * This is traditional behaviour of "git format-patch
1786 * origin" that prepares what the origin side still
1787 * does not have.
1788 */
7c496280 1789 rev.pending.objects[0].item->flags |= UNINTERESTING;
3384a2df 1790 add_head_to_pending(&rev);
427a8ec5 1791 check_head = 1;
7c496280 1792 }
8a1d076e
JH
1793 /*
1794 * Otherwise, it is "format-patch -22 HEAD", and/or
1795 * "format-patch --root HEAD". The user wants
1796 * get_revision() to do the usual traversal.
7c496280 1797 */
427a8ec5
FC
1798
1799 if (!strcmp(rev.pending.objects[0].name, "HEAD"))
1800 check_head = 1;
1801
1802 if (check_head) {
95b567c7 1803 const char *ref, *v;
7695d118 1804 ref = resolve_ref_unsafe("HEAD", RESOLVE_REF_READING,
744c040b 1805 NULL, NULL);
95b567c7
JK
1806 if (ref && skip_prefix(ref, "refs/heads/", &v))
1807 branch_name = xstrdup(v);
427a8ec5
FC
1808 else
1809 branch_name = xstrdup(""); /* no branch */
1810 }
e686eb98 1811 }
68c2ec7f
JH
1812
1813 /*
1814 * We cannot move this anywhere earlier because we do want to
9517e6b8 1815 * know if --root was given explicitly from the command line.
68c2ec7f
JH
1816 */
1817 rev.show_root_diff = 1;
1818
657ab61e
KB
1819 if (ignore_if_in_upstream) {
1820 /* Don't say anything if head and upstream are the same. */
1821 if (rev.pending.nr == 2) {
1822 struct object_array_entry *o = rev.pending.objects;
4a7e27e9 1823 if (oideq(&o[0].item->oid, &o[1].item->oid))
126facf8 1824 goto done;
657ab61e 1825 }
811929f1 1826 get_patch_ids(&rev, &ids);
657ab61e 1827 }
9c6efa36 1828
3d51e1b5 1829 if (prepare_revision_walk(&rev))
5c5f1d7c 1830 die(_("revision walk setup failed"));
2bda2cf4 1831 rev.boundary = 1;
91efcf60 1832 while ((commit = get_revision(&rev)) != NULL) {
2bda2cf4 1833 if (commit->object.flags & BOUNDARY) {
2bda2cf4
DB
1834 boundary_count++;
1835 origin = (boundary_count == 1) ? commit : NULL;
1836 continue;
1837 }
1838
3acf8dd8 1839 if (ignore_if_in_upstream && has_commit_patch_id(commit, &ids))
9c6efa36
JS
1840 continue;
1841
91efcf60 1842 nr++;
2756ca43 1843 REALLOC_ARRAY(list, nr);
91efcf60
JH
1844 list[nr - 1] = commit;
1845 }
80d35ca0
FC
1846 if (nr == 0)
1847 /* nothing to do */
126facf8 1848 goto done;
0377db77 1849 total = nr;
2a4c2607
FC
1850 if (cover_letter == -1) {
1851 if (config_cover_letter == COVER_AUTO)
1852 cover_letter = (total > 1);
1853 else
1854 cover_letter = (config_cover_letter == COVER_ON);
1855 }
957ed3a5
JK
1856 if (!keep_subject && auto_number && (total > 1 || cover_letter))
1857 numbered = 1;
1858 if (numbered)
1859 rev.total = total + start_number - 1;
2a4c2607 1860
126facf8 1861 if (idiff_prev.nr) {
ee6cbf71
ES
1862 if (!cover_letter && total != 1)
1863 die(_("--interdiff requires --cover-letter or single patch"));
126facf8
ES
1864 rev.idiff_oid1 = &idiff_prev.oid[idiff_prev.nr - 1];
1865 rev.idiff_oid2 = get_commit_tree_oid(list[0]);
5ac290f9
ES
1866 rev.idiff_title = diff_title(&idiff_title, reroll_count,
1867 _("Interdiff:"),
1868 _("Interdiff against v%d:"));
126facf8
ES
1869 }
1870
8631bf1c
ES
1871 if (creation_factor < 0)
1872 creation_factor = RANGE_DIFF_CREATION_FACTOR_DEFAULT;
1873 else if (!rdiff_prev)
1874 die(_("--creation-factor requires --range-diff"));
1875
31e2617a 1876 if (rdiff_prev) {
40ce4160
ES
1877 if (!cover_letter && total != 1)
1878 die(_("--range-diff requires --cover-letter or single patch"));
31e2617a 1879
2e6fd71a
ES
1880 infer_range_diff_ranges(&rdiff1, &rdiff2, rdiff_prev,
1881 origin, list[0]);
31e2617a
ES
1882 rev.rdiff1 = rdiff1.buf;
1883 rev.rdiff2 = rdiff2.buf;
8631bf1c 1884 rev.creation_factor = creation_factor;
4ee99689
ES
1885 rev.rdiff_title = diff_title(&rdiff_title, reroll_count,
1886 _("Range-diff:"),
1887 _("Range-diff against v%d:"));
31e2617a
ES
1888 }
1889
7022650f
JM
1890 if (!signature) {
1891 ; /* --no-signature inhibits all signatures */
1892 } else if (signature && signature != git_version_string) {
1893 ; /* non-default signature already set */
1894 } else if (signature_file) {
1895 struct strbuf buf = STRBUF_INIT;
1896
1897 if (strbuf_read_file(&buf, signature_file, 128) < 0)
1898 die_errno(_("unable to read signature file '%s'"), signature_file);
1899 signature = strbuf_detach(&buf, NULL);
1900 }
1901
fa2ab86d 1902 memset(&bases, 0, sizeof(bases));
bb52995f 1903 if (base_commit || base_auto) {
fa2ab86d
XY
1904 struct commit *base = get_base_commit(base_commit, list, nr);
1905 reset_revision_walk();
15b76c1f 1906 clear_object_flags(UNINTERESTING);
fa2ab86d
XY
1907 prepare_bases(&bases, base, list, nr);
1908 }
1909
b079c50e
TR
1910 if (in_reply_to || thread || cover_letter)
1911 rev.ref_message_ids = xcalloc(1, sizeof(struct string_list));
1912 if (in_reply_to) {
1913 const char *msgid = clean_message_id(in_reply_to);
1d2f80fa 1914 string_list_append(rev.ref_message_ids, msgid);
b079c50e 1915 }
ce37596c 1916 rev.numbered_files = just_numbers;
108dab28 1917 rev.patch_suffix = fmt_patch_suffix;
a5a27c79
DB
1918 if (cover_letter) {
1919 if (thread)
1920 gen_message_id(&rev, "cover");
ce37596c 1921 make_cover_letter(&rev, use_stdout,
d7ddad01 1922 origin, nr, list, branch_name, quiet);
95235f5b 1923 print_bases(&bases, rev.diffopt.file);
480871e0 1924 print_signature(rev.diffopt.file);
a5a27c79
DB
1925 total++;
1926 start_number--;
40ce4160 1927 /* interdiff/range-diff in cover-letter; omit from patches */
ee6cbf71 1928 rev.idiff_oid1 = NULL;
40ce4160 1929 rev.rdiff1 = NULL;
a5a27c79 1930 }
5289c56a 1931 rev.add_signoff = do_signoff;
738e88a2
KW
1932
1933 if (show_progress)
d33a4332 1934 progress = start_delayed_progress(_("Generating patches"), total);
91efcf60
JH
1935 while (0 <= --nr) {
1936 int shown;
738e88a2 1937 display_progress(progress, total - nr);
91efcf60 1938 commit = list[nr];
add5c8a5 1939 rev.nr = total - nr + (start_number - 1);
d1566f78 1940 /* Make the second and subsequent mails replies to the first */
cc35de84 1941 if (thread) {
a5a27c79 1942 /* Have we already had a message ID? */
e1a37346 1943 if (rev.message_id) {
a5a27c79 1944 /*
30984ed2
TR
1945 * For deep threading: make every mail
1946 * a reply to the previous one, no
1947 * matter what other options are set.
1948 *
1949 * For shallow threading:
1950 *
2175c10d
TR
1951 * Without --cover-letter and
1952 * --in-reply-to, make every mail a
1953 * reply to the one before.
1954 *
1955 * With --in-reply-to but no
1956 * --cover-letter, make every mail a
1957 * reply to the <reply-to>.
1958 *
1959 * With --cover-letter, make every
1960 * mail but the cover letter a reply
1961 * to the cover letter. The cover
1962 * letter is a reply to the
1963 * --in-reply-to, if specified.
a5a27c79 1964 */
30984ed2
TR
1965 if (thread == THREAD_SHALLOW
1966 && rev.ref_message_ids->nr > 0
2175c10d 1967 && (!cover_letter || rev.nr > 1))
e1a37346
DB
1968 free(rev.message_id);
1969 else
1d2f80fa
JP
1970 string_list_append(rev.ref_message_ids,
1971 rev.message_id);
cc35de84 1972 }
f2fd0760 1973 gen_message_id(&rev, oid_to_hex(&commit->object.oid));
d1566f78 1974 }
6df514af 1975
a21c2f94 1976 if (!use_stdout &&
95235f5b 1977 open_next_file(rev.numbered_files ? NULL : commit, NULL, &rev, quiet))
bc208ae3 1978 die(_("failed to create output files"));
91efcf60 1979 shown = log_tree_commit(&rev, commit);
6a7895fd
SB
1980 free_commit_buffer(the_repository->parsed_objects,
1981 commit);
add5c8a5
JH
1982
1983 /* We put one extra blank line between formatted
1984 * patches and this flag is used by log-tree code
1985 * to see if it needs to emit a LF before showing
1986 * the log; when using one file per patch, we do
1987 * not want the extra blank line.
1988 */
1989 if (!use_stdout)
1990 rev.shown_one = 0;
698ce6f8 1991 if (shown) {
480871e0 1992 print_bases(&bases, rev.diffopt.file);
698ce6f8 1993 if (rev.mime_boundary)
95235f5b 1994 fprintf(rev.diffopt.file, "\n--%s%s--\n\n\n",
698ce6f8
JS
1995 mime_boundary_leader,
1996 rev.mime_boundary);
1997 else
95235f5b 1998 print_signature(rev.diffopt.file);
698ce6f8 1999 }
0377db77 2000 if (!use_stdout)
95235f5b 2001 fclose(rev.diffopt.file);
91efcf60 2002 }
738e88a2 2003 stop_progress(&progress);
91efcf60 2004 free(list);
739453a3 2005 free(branch_name);
ca9e0a1b
SB
2006 string_list_clear(&extra_to, 0);
2007 string_list_clear(&extra_cc, 0);
2008 string_list_clear(&extra_hdr, 0);
5d23e133
JH
2009 if (ignore_if_in_upstream)
2010 free_patch_ids(&ids);
126facf8
ES
2011
2012done:
2013 oid_array_clear(&idiff_prev);
5ac290f9 2014 strbuf_release(&idiff_title);
31e2617a
ES
2015 strbuf_release(&rdiff1);
2016 strbuf_release(&rdiff2);
4ee99689 2017 strbuf_release(&rdiff_title);
91efcf60
JH
2018 return 0;
2019}
2020
e827633a
RS
2021static int add_pending_commit(const char *arg, struct rev_info *revs, int flags)
2022{
d801627b 2023 struct object_id oid;
2024 if (get_oid(arg, &oid) == 0) {
2122f675
SB
2025 struct commit *commit = lookup_commit_reference(the_repository,
2026 &oid);
e827633a
RS
2027 if (commit) {
2028 commit->object.flags |= flags;
2029 add_pending_object(revs, &commit->object, arg);
2030 return 0;
2031 }
2032 }
2033 return -1;
2034}
2035
28a53178 2036static const char * const cherry_usage[] = {
986d1bb8 2037 N_("git cherry [-v] [<upstream> [<head> [<limit>]]]"),
28a53178
EFL
2038 NULL
2039};
2040
a3a32e7f 2041static void print_commit(char sign, struct commit *commit, int verbose,
95235f5b 2042 int abbrev, FILE *file)
a3a32e7f
JN
2043{
2044 if (!verbose) {
95235f5b 2045 fprintf(file, "%c %s\n", sign,
aab9583f 2046 find_unique_abbrev(&commit->object.oid, abbrev));
a3a32e7f
JN
2047 } else {
2048 struct strbuf buf = STRBUF_INIT;
f67d2e82 2049 pp_commit_easy(CMIT_FMT_ONELINE, commit, &buf);
95235f5b 2050 fprintf(file, "%c %s %s\n", sign,
aab9583f 2051 find_unique_abbrev(&commit->object.oid, abbrev),
a3a32e7f
JN
2052 buf.buf);
2053 strbuf_release(&buf);
2054 }
2055}
2056
e827633a
RS
2057int cmd_cherry(int argc, const char **argv, const char *prefix)
2058{
2059 struct rev_info revs;
5d23e133 2060 struct patch_ids ids;
e827633a
RS
2061 struct commit *commit;
2062 struct commit_list *list = NULL;
f2968022 2063 struct branch *current_branch;
e827633a
RS
2064 const char *upstream;
2065 const char *head = "HEAD";
2066 const char *limit = NULL;
28a53178 2067 int verbose = 0, abbrev = 0;
e827633a 2068
28a53178
EFL
2069 struct option options[] = {
2070 OPT__ABBREV(&abbrev),
986d1bb8 2071 OPT__VERBOSE(&verbose, N_("be verbose")),
28a53178
EFL
2072 OPT_END()
2073 };
e827633a 2074
28a53178 2075 argc = parse_options(argc, argv, prefix, options, cherry_usage, 0);
fef34270 2076
e827633a 2077 switch (argc) {
e827633a 2078 case 3:
28a53178 2079 limit = argv[2];
e827633a
RS
2080 /* FALLTHROUGH */
2081 case 2:
28a53178
EFL
2082 head = argv[1];
2083 /* FALLTHROUGH */
2084 case 1:
2085 upstream = argv[0];
e827633a
RS
2086 break;
2087 default:
f2968022 2088 current_branch = branch_get(NULL);
3a429d0a 2089 upstream = branch_get_upstream(current_branch, NULL);
a9f9f8cc 2090 if (!upstream) {
5c5f1d7c 2091 fprintf(stderr, _("Could not find a tracked"
f2968022 2092 " remote branch, please"
5c5f1d7c 2093 " specify <upstream> manually.\n"));
28a53178 2094 usage_with_options(cherry_usage, options);
f2968022 2095 }
e827633a
RS
2096 }
2097
2abf3503 2098 repo_init_revisions(the_repository, &revs, prefix);
51f4de3a 2099 revs.max_parents = 1;
e827633a
RS
2100
2101 if (add_pending_commit(head, &revs, 0))
bc208ae3 2102 die(_("unknown commit %s"), head);
e827633a 2103 if (add_pending_commit(upstream, &revs, UNINTERESTING))
bc208ae3 2104 die(_("unknown commit %s"), upstream);
e827633a
RS
2105
2106 /* Don't say anything if head and upstream are the same. */
2107 if (revs.pending.nr == 2) {
2108 struct object_array_entry *o = revs.pending.objects;
4a7e27e9 2109 if (oideq(&o[0].item->oid, &o[1].item->oid))
e827633a
RS
2110 return 0;
2111 }
2112
811929f1 2113 get_patch_ids(&revs, &ids);
e827633a
RS
2114
2115 if (limit && add_pending_commit(limit, &revs, UNINTERESTING))
bc208ae3 2116 die(_("unknown commit %s"), limit);
e827633a
RS
2117
2118 /* reverse the list of commits */
3d51e1b5 2119 if (prepare_revision_walk(&revs))
5c5f1d7c 2120 die(_("revision walk setup failed"));
e827633a 2121 while ((commit = get_revision(&revs)) != NULL) {
e827633a
RS
2122 commit_list_insert(commit, &list);
2123 }
2124
2125 while (list) {
e827633a
RS
2126 char sign = '+';
2127
2128 commit = list->item;
5d23e133 2129 if (has_commit_patch_id(commit, &ids))
e827633a 2130 sign = '-';
95235f5b 2131 print_commit(sign, commit, verbose, abbrev, revs.diffopt.file);
e827633a
RS
2132 list = list->next;
2133 }
2134
5d23e133 2135 free_patch_ids(&ids);
e827633a
RS
2136 return 0;
2137}