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