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