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