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