Implement line-history search (git log -L)
[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"
6b2f2d98 8#include "color.h"
70827b15
LT
9#include "commit.h"
10#include "diff.h"
11#include "revision.h"
12#include "log-tree.h"
91efcf60 13#include "builtin.h"
5d7eeee2 14#include "tag.h"
cf39f54e 15#include "reflog-walk.h"
5d23e133 16#include "patch-ids.h"
a5a27c79 17#include "run-command.h"
2bda2cf4 18#include "shortlog.h"
f2968022 19#include "remote.h"
b079c50e 20#include "string-list.h"
fff02ee6 21#include "parse-options.h"
12da1d1f 22#include "line-log.h"
739453a3 23#include "branch.h"
74775a09 24#include "streaming.h"
816fb46b 25#include "version.h"
ea57bc0d 26#include "mailmap.h"
70827b15 27
dd0ffd5b
HO
28/* Set a default date-time format for git log ("log.date" config variable) */
29static const char *default_date_mode = NULL;
30
0c47695a 31static int default_abbrev_commit;
0f03ca94 32static int default_show_root = 1;
8a3d203b 33static int decoration_style;
1c40c36b 34static int decoration_given;
e6bb5f78 35static int use_mailmap_config;
d54276f2 36static const char *fmt_patch_subject_prefix = "PATCH";
94c22a5e 37static const char *fmt_pretty;
0f03ca94 38
1c40c36b 39static const char * const builtin_log_usage[] = {
e62cd35a
NTND
40 N_("git log [<options>] [<since>..<until>] [[--] <path>...]\n")
41 N_(" or: git show [options] <object>..."),
1c40c36b
CMN
42 NULL
43};
1c370ea4 44
12da1d1f
TR
45struct line_opt_callback_data {
46 struct rev_info *rev;
47 const char *prefix;
48 struct string_list args;
49};
50
8a3d203b
JH
51static int parse_decoration_style(const char *var, const char *value)
52{
53 switch (git_config_maybe_bool(var, value)) {
54 case 1:
55 return DECORATE_SHORT_REFS;
56 case 0:
57 return 0;
58 default:
59 break;
60 }
61 if (!strcmp(value, "full"))
62 return DECORATE_FULL_REFS;
63 else if (!strcmp(value, "short"))
64 return DECORATE_SHORT_REFS;
65 return -1;
66}
67
1c40c36b
CMN
68static int decorate_callback(const struct option *opt, const char *arg, int unset)
69{
70 if (unset)
71 decoration_style = 0;
72 else if (arg)
73 decoration_style = parse_decoration_style("command line", arg);
74 else
75 decoration_style = DECORATE_SHORT_REFS;
76
77 if (decoration_style < 0)
78 die("invalid --decorate option: %s", arg);
79
80 decoration_given = 1;
81
82 return 0;
83}
84
12da1d1f
TR
85static int log_line_range_callback(const struct option *option, const char *arg, int unset)
86{
87 struct line_opt_callback_data *data = option->value;
88
89 if (!arg)
90 return -1;
91
92 data->rev->line_level_traverse = 1;
93 string_list_append(&data->args, arg);
94
95 return 0;
96}
97
ef803fd4 98static void cmd_log_init_defaults(struct rev_info *rev)
70827b15 99{
94c22a5e 100 if (fmt_pretty)
4da45bef 101 get_commit_format(fmt_pretty, rev);
70827b15 102 rev->verbose_header = 1;
8f67f8ae 103 DIFF_OPT_SET(&rev->diffopt, RECURSIVE);
5e0ec15e 104 rev->diffopt.stat_width = -1; /* use full terminal width */
df44483a 105 rev->diffopt.stat_graph_width = -1; /* respect statGraphWidth config */
0c47695a 106 rev->abbrev_commit = default_abbrev_commit;
0f03ca94 107 rev->show_root_diff = default_show_root;
d54276f2 108 rev->subject_prefix = fmt_patch_subject_prefix;
5ec11af6 109 DIFF_OPT_SET(&rev->diffopt, ALLOW_TEXTCONV);
dd0ffd5b
HO
110
111 if (default_date_mode)
112 rev->date_mode = parse_date_format(default_date_mode);
ef803fd4 113}
dd0ffd5b 114
ef803fd4
MG
115static void cmd_log_init_finish(int argc, const char **argv, const char *prefix,
116 struct rev_info *rev, struct setup_revision_opt *opt)
117{
ef803fd4 118 struct userformat_want w;
ea57bc0d 119 int quiet = 0, source = 0, mailmap = 0;
12da1d1f 120 static struct line_opt_callback_data line_cb = {NULL, NULL, STRING_LIST_INIT_DUP};
1c40c36b
CMN
121
122 const struct option builtin_log_options[] = {
e62cd35a
NTND
123 OPT_BOOLEAN(0, "quiet", &quiet, N_("suppress diff output")),
124 OPT_BOOLEAN(0, "source", &source, N_("show source")),
ea57bc0d 125 OPT_BOOLEAN(0, "use-mailmap", &mailmap, N_("Use mail map file")),
e62cd35a 126 { OPTION_CALLBACK, 0, "decorate", NULL, NULL, N_("decorate options"),
1c40c36b 127 PARSE_OPT_OPTARG, decorate_callback},
12da1d1f
TR
128 OPT_CALLBACK('L', NULL, &line_cb, "n,m:file",
129 "Process line range n,m in file, counting from 1",
130 log_line_range_callback),
1c40c36b
CMN
131 OPT_END()
132 };
133
12da1d1f
TR
134 line_cb.rev = rev;
135 line_cb.prefix = prefix;
136
e6bb5f78 137 mailmap = use_mailmap_config;
1c40c36b
CMN
138 argc = parse_options(argc, argv, prefix,
139 builtin_log_options, builtin_log_usage,
140 PARSE_OPT_KEEP_ARGV0 | PARSE_OPT_KEEP_UNKNOWN |
141 PARSE_OPT_KEEP_DASHDASH);
52883fbd 142
01771a8e
JH
143 if (quiet)
144 rev->diffopt.output_format |= DIFF_FORMAT_NO_OUTPUT;
f9c75d85 145 argc = setup_revisions(argc, argv, rev, opt);
dd0ffd5b 146
1c40c36b
CMN
147 /* Any arguments at this point are not recognized */
148 if (argc > 1)
149 die("unrecognized argument: %s", argv[1]);
150
5b163603
JG
151 memset(&w, 0, sizeof(w));
152 userformat_find_requirements(NULL, &w);
153
154 if (!rev->show_notes_given && (!rev->pretty_given || w.notes))
66b2ed09 155 rev->show_notes = 1;
894a9d33
TR
156 if (rev->show_notes)
157 init_display_notes(&rev->notes_opt);
66b2ed09 158
9dafea26
TH
159 if (rev->diffopt.pickaxe || rev->diffopt.filter)
160 rev->always_show_header = 0;
8f67f8ae 161 if (DIFF_OPT_TST(&rev->diffopt, FOLLOW_RENAMES)) {
750f7b66 162 rev->always_show_header = 0;
66f13625 163 if (rev->diffopt.pathspec.nr != 1)
750f7b66
LT
164 usage("git logs can only follow renames on one pathname at a time");
165 }
1c40c36b
CMN
166
167 if (source)
168 rev->show_source = 1;
635530a2 169
ea57bc0d
AP
170 if (mailmap) {
171 rev->mailmap = xcalloc(1, sizeof(struct string_list));
172 read_mailmap(rev->mailmap, NULL);
173 }
174
0c47695a
JS
175 if (rev->pretty_given && rev->commit_format == CMIT_FMT_RAW) {
176 /*
177 * "log --pretty=raw" is special; ignore UI oriented
178 * configuration variables such as decoration.
179 */
180 if (!decoration_given)
181 decoration_style = 0;
182 if (!rev->abbrev_commit_given)
183 rev->abbrev_commit = 0;
184 }
635530a2 185
33e7018c
LH
186 if (decoration_style) {
187 rev->show_decorations = 1;
188 load_ref_decorations(decoration_style);
189 }
12da1d1f
TR
190
191 if (rev->line_level_traverse)
192 line_log_init(rev, line_cb.prefix, &line_cb.args);
193
1fda91b5 194 setup_pager();
9dafea26
TH
195}
196
ef803fd4
MG
197static void cmd_log_init(int argc, const char **argv, const char *prefix,
198 struct rev_info *rev, struct setup_revision_opt *opt)
199{
200 cmd_log_init_defaults(rev);
201 cmd_log_init_finish(argc, argv, prefix, rev, opt);
202}
203
252a7c02
LT
204/*
205 * This gives a rough estimate for how many commits we
206 * will print out in the list.
207 */
208static int estimate_commit_count(struct rev_info *rev, struct commit_list *list)
209{
210 int n = 0;
211
212 while (list) {
213 struct commit *commit = list->item;
214 unsigned int flags = commit->object.flags;
252a7c02 215 list = list->next;
7dc0fe3b 216 if (!(flags & (TREESAME | UNINTERESTING)))
53b2c823 217 n++;
252a7c02
LT
218 }
219 return n;
220}
221
222static void show_early_header(struct rev_info *rev, const char *stage, int nr)
223{
224 if (rev->shown_one) {
225 rev->shown_one = 0;
226 if (rev->commit_format != CMIT_FMT_ONELINE)
227 putchar(rev->diffopt.line_termination);
228 }
5c5f1d7c 229 printf(_("Final output: %d %s\n"), nr, stage);
252a7c02
LT
230}
231
2af202be 232static struct itimerval early_output_timer;
252a7c02 233
cdcefbc9
LT
234static void log_show_early(struct rev_info *revs, struct commit_list *list)
235{
236 int i = revs->early_output;
252a7c02 237 int show_header = 1;
cdcefbc9
LT
238
239 sort_in_topological_order(&list, revs->lifo);
240 while (list && i) {
241 struct commit *commit = list->item;
252a7c02
LT
242 switch (simplify_commit(revs, commit)) {
243 case commit_show:
244 if (show_header) {
245 int n = estimate_commit_count(revs, list);
246 show_early_header(revs, "incomplete", n);
247 show_header = 0;
248 }
249 log_tree_commit(revs, commit);
250 i--;
251 break;
252 case commit_ignore:
253 break;
254 case commit_error:
255 return;
256 }
cdcefbc9 257 list = list->next;
cdcefbc9 258 }
252a7c02
LT
259
260 /* Did we already get enough commits for the early output? */
261 if (!i)
262 return;
263
264 /*
265 * ..if no, then repeat it twice a second until we
266 * do.
267 *
268 * NOTE! We don't use "it_interval", because if the
269 * reader isn't listening, we want our output to be
270 * throttled by the writing, and not have the timer
271 * trigger every second even if we're blocked on a
272 * reader!
273 */
274 early_output_timer.it_value.tv_sec = 0;
275 early_output_timer.it_value.tv_usec = 500000;
276 setitimer(ITIMER_REAL, &early_output_timer, NULL);
cdcefbc9
LT
277}
278
279static void early_output(int signal)
280{
281 show_early_output = log_show_early;
282}
283
284static void setup_early_output(struct rev_info *rev)
285{
286 struct sigaction sa;
cdcefbc9
LT
287
288 /*
289 * Set up the signal handler, minimally intrusively:
290 * we only set a single volatile integer word (not
291 * using sigatomic_t - trying to avoid unnecessary
292 * system dependencies and headers), and using
293 * SA_RESTART.
294 */
295 memset(&sa, 0, sizeof(sa));
296 sa.sa_handler = early_output;
297 sigemptyset(&sa.sa_mask);
298 sa.sa_flags = SA_RESTART;
299 sigaction(SIGALRM, &sa, NULL);
300
301 /*
302 * If we can get the whole output in less than a
303 * tenth of a second, don't even bother doing the
304 * early-output thing..
305 *
306 * This is a one-time-only trigger.
307 */
252a7c02
LT
308 early_output_timer.it_value.tv_sec = 0;
309 early_output_timer.it_value.tv_usec = 100000;
310 setitimer(ITIMER_REAL, &early_output_timer, NULL);
cdcefbc9
LT
311}
312
313static void finish_early_output(struct rev_info *rev)
314{
252a7c02 315 int n = estimate_commit_count(rev, rev->commits);
cdcefbc9 316 signal(SIGALRM, SIG_IGN);
252a7c02 317 show_early_header(rev, "done", n);
cdcefbc9
LT
318}
319
9dafea26
TH
320static int cmd_log_walk(struct rev_info *rev)
321{
322 struct commit *commit;
f31027c9
JH
323 int saved_nrl = 0;
324 int saved_dcctc = 0;
70827b15 325
cdcefbc9
LT
326 if (rev->early_output)
327 setup_early_output(rev);
328
3d51e1b5 329 if (prepare_revision_walk(rev))
5c5f1d7c 330 die(_("revision walk setup failed"));
cdcefbc9
LT
331
332 if (rev->early_output)
333 finish_early_output(rev);
334
036d17fe 335 /*
84102a33
PVM
336 * For --check and --exit-code, the exit code is based on CHECK_FAILED
337 * and HAS_CHANGES being accumulated in rev->diffopt, so be careful to
338 * retain that state information if replacing rev->diffopt in this loop
036d17fe 339 */
70827b15 340 while ((commit = get_revision(rev)) != NULL) {
251df09b
MM
341 if (!log_tree_commit(rev, commit) &&
342 rev->max_count >= 0)
343 /*
344 * We decremented max_count in get_revision,
345 * but we didn't actually show the commit.
346 */
347 rev->max_count++;
a6c73064
JS
348 if (!rev->reflog_info) {
349 /* we allow cycles in reflog ancestry */
350 free(commit->buffer);
351 commit->buffer = NULL;
352 }
cb115748
LT
353 free_commit_list(commit->parents);
354 commit->parents = NULL;
f31027c9
JH
355 if (saved_nrl < rev->diffopt.needed_rename_limit)
356 saved_nrl = rev->diffopt.needed_rename_limit;
357 if (rev->diffopt.degraded_cc_to_c)
358 saved_dcctc = 1;
70827b15 359 }
f31027c9
JH
360 rev->diffopt.degraded_cc_to_c = saved_dcctc;
361 rev->diffopt.needed_rename_limit = saved_nrl;
362
036d17fe
PVM
363 if (rev->diffopt.output_format & DIFF_FORMAT_CHECKDIFF &&
364 DIFF_OPT_TST(&rev->diffopt, CHECK_FAILED)) {
365 return 02;
366 }
84102a33 367 return diff_result_code(&rev->diffopt, 0);
70827b15
LT
368}
369
ef90d6d4 370static int git_log_config(const char *var, const char *value, void *cb)
0f03ca94 371{
94c22a5e
CR
372 if (!strcmp(var, "format.pretty"))
373 return git_config_string(&fmt_pretty, var, value);
70cff3ac
BH
374 if (!strcmp(var, "format.subjectprefix"))
375 return git_config_string(&fmt_patch_subject_prefix, var, value);
0c47695a
JS
376 if (!strcmp(var, "log.abbrevcommit")) {
377 default_abbrev_commit = git_config_bool(var, value);
378 return 0;
379 }
dd0ffd5b
HO
380 if (!strcmp(var, "log.date"))
381 return git_config_string(&default_date_mode, var, value);
eb734454 382 if (!strcmp(var, "log.decorate")) {
8a3d203b
JH
383 decoration_style = parse_decoration_style(var, value);
384 if (decoration_style < 0)
385 decoration_style = 0; /* maybe warn? */
eb734454
SD
386 return 0;
387 }
0f03ca94
PB
388 if (!strcmp(var, "log.showroot")) {
389 default_show_root = git_config_bool(var, value);
390 return 0;
391 }
5e11bee6
NR
392 if (!prefixcmp(var, "color.decorate."))
393 return parse_decorate_color_config(var, 15, value);
e6bb5f78
AP
394 if (!strcmp(var, "log.mailmap")) {
395 use_mailmap_config = git_config_bool(var, value);
396 return 0;
397 }
398
0657bcbf
JH
399 if (grep_config(var, value, cb) < 0)
400 return -1;
ef90d6d4 401 return git_diff_ui_config(var, value, cb);
0f03ca94
PB
402}
403
a633fca0 404int cmd_whatchanged(int argc, const char **argv, const char *prefix)
70827b15
LT
405{
406 struct rev_info rev;
32962c9b 407 struct setup_revision_opt opt;
70827b15 408
0657bcbf 409 init_grep_defaults();
ef90d6d4 410 git_config(git_log_config, NULL);
6b2f2d98 411
db6296a5 412 init_revisions(&rev, prefix);
70827b15 413 rev.diff = 1;
9202434c 414 rev.simplify_history = 0;
32962c9b
JH
415 memset(&opt, 0, sizeof(opt));
416 opt.def = "HEAD";
d5f6b1d7 417 opt.revarg_opt = REVARG_COMMITTISH;
32962c9b 418 cmd_log_init(argc, argv, prefix, &rev, &opt);
9dafea26
TH
419 if (!rev.diffopt.output_format)
420 rev.diffopt.output_format = DIFF_FORMAT_RAW;
421 return cmd_log_walk(&rev);
70827b15
LT
422}
423
56122ed8
JS
424static void show_tagger(char *buf, int len, struct rev_info *rev)
425{
ea718e65 426 struct strbuf out = STRBUF_INIT;
6bf13944 427 struct pretty_print_context pp = {0};
56122ed8 428
6bf13944
JK
429 pp.fmt = rev->commit_format;
430 pp.date_mode = rev->date_mode;
431 pp_user_info(&pp, "Tagger", &out, buf, get_log_output_encoding());
ca4ca9ed 432 printf("%s", out.buf);
ea718e65 433 strbuf_release(&out);
56122ed8
JS
434}
435
74775a09
NTND
436static int show_blob_object(const unsigned char *sha1, struct rev_info *rev)
437{
438 fflush(stdout);
439 return stream_blob_to_fd(1, sha1, NULL, 0);
440}
441
442static int show_tag_object(const unsigned char *sha1, struct rev_info *rev)
5d7eeee2
JS
443{
444 unsigned long size;
21666f1a
NP
445 enum object_type type;
446 char *buf = read_sha1_file(sha1, &type, &size);
5d7eeee2
JS
447 int offset = 0;
448
449 if (!buf)
5c5f1d7c 450 return error(_("Could not read object %s"), sha1_to_hex(sha1));
5d7eeee2 451
74775a09
NTND
452 assert(type == OBJ_TAG);
453 while (offset < size && buf[offset] != '\n') {
454 int new_offset = offset + 1;
455 while (new_offset < size && buf[new_offset++] != '\n')
456 ; /* do nothing */
457 if (!prefixcmp(buf + offset, "tagger "))
458 show_tagger(buf + offset + 7,
459 new_offset - offset - 7, rev);
460 offset = new_offset;
461 }
5d7eeee2
JS
462
463 if (offset < size)
464 fwrite(buf + offset, size - offset, 1, stdout);
465 free(buf);
466 return 0;
467}
468
469static int show_tree_object(const unsigned char *sha1,
470 const char *base, int baselen,
671f0707 471 const char *pathname, unsigned mode, int stage, void *context)
5d7eeee2
JS
472{
473 printf("%s%s\n", pathname, S_ISDIR(mode) ? "/" : "");
474 return 0;
475}
476
b4490059
JH
477static void show_rev_tweak_rev(struct rev_info *rev, struct setup_revision_opt *opt)
478{
2bf65873
JH
479 if (rev->ignore_merges) {
480 /* There was no "-m" on the command line */
481 rev->ignore_merges = 0;
482 if (!rev->first_parent_only && !rev->combine_merges) {
483 /* No "--first-parent", "-c", nor "--cc" */
484 rev->combine_merges = 1;
485 rev->dense_combined_merges = 1;
486 }
487 }
b4490059
JH
488 if (!rev->diffopt.output_format)
489 rev->diffopt.output_format = DIFF_FORMAT_PATCH;
490}
491
a633fca0 492int cmd_show(int argc, const char **argv, const char *prefix)
70827b15
LT
493{
494 struct rev_info rev;
5d7eeee2 495 struct object_array_entry *objects;
32962c9b 496 struct setup_revision_opt opt;
f0096c06 497 struct pathspec match_all;
5d7eeee2 498 int i, count, ret = 0;
70827b15 499
0657bcbf 500 init_grep_defaults();
ef90d6d4 501 git_config(git_log_config, NULL);
6b2f2d98 502
f0096c06 503 init_pathspec(&match_all, NULL);
db6296a5 504 init_revisions(&rev, prefix);
70827b15 505 rev.diff = 1;
70827b15 506 rev.always_show_header = 1;
ca92e59e 507 rev.no_walk = REVISION_WALK_NO_WALK_SORTED;
666c92a2
ZJS
508 rev.diffopt.stat_width = -1; /* Scale to real terminal size */
509
32962c9b
JH
510 memset(&opt, 0, sizeof(opt));
511 opt.def = "HEAD";
b4490059 512 opt.tweak = show_rev_tweak_rev;
32962c9b 513 cmd_log_init(argc, argv, prefix, &rev, &opt);
5d7eeee2 514
c5941f1a
JH
515 if (!rev.no_walk)
516 return cmd_log_walk(&rev);
517
5d7eeee2
JS
518 count = rev.pending.nr;
519 objects = rev.pending.objects;
520 for (i = 0; i < count && !ret; i++) {
521 struct object *o = objects[i].item;
522 const char *name = objects[i].name;
523 switch (o->type) {
524 case OBJ_BLOB:
74775a09 525 ret = show_blob_object(o->sha1, NULL);
5d7eeee2
JS
526 break;
527 case OBJ_TAG: {
528 struct tag *t = (struct tag *)o;
529
ae03ee64
JK
530 if (rev.shown_one)
531 putchar('\n');
56122ed8 532 printf("%stag %s%s\n",
8f67f8ae 533 diff_get_color_opt(&rev.diffopt, DIFF_COMMIT),
5d7eeee2 534 t->tag,
8f67f8ae 535 diff_get_color_opt(&rev.diffopt, DIFF_RESET));
74775a09 536 ret = show_tag_object(o->sha1, &rev);
ae03ee64 537 rev.shown_one = 1;
d2dadfe8
JH
538 if (ret)
539 break;
540 o = parse_object(t->tagged->sha1);
541 if (!o)
5c5f1d7c 542 ret = error(_("Could not read object %s"),
d2dadfe8
JH
543 sha1_to_hex(t->tagged->sha1));
544 objects[i].item = o;
5d7eeee2
JS
545 i--;
546 break;
547 }
548 case OBJ_TREE:
ae03ee64
JK
549 if (rev.shown_one)
550 putchar('\n');
5d7eeee2 551 printf("%stree %s%s\n\n",
8f67f8ae 552 diff_get_color_opt(&rev.diffopt, DIFF_COMMIT),
5d7eeee2 553 name,
8f67f8ae 554 diff_get_color_opt(&rev.diffopt, DIFF_RESET));
f0096c06 555 read_tree_recursive((struct tree *)o, "", 0, 0, &match_all,
671f0707 556 show_tree_object, NULL);
ae03ee64 557 rev.shown_one = 1;
5d7eeee2
JS
558 break;
559 case OBJ_COMMIT:
560 rev.pending.nr = rev.pending.alloc = 0;
561 rev.pending.objects = NULL;
562 add_object_array(o, name, &rev.pending);
563 ret = cmd_log_walk(&rev);
564 break;
565 default:
5c5f1d7c 566 ret = error(_("Unknown type: %d"), o->type);
5d7eeee2
JS
567 }
568 }
569 free(objects);
570 return ret;
70827b15
LT
571}
572
cf39f54e
LT
573/*
574 * This is equivalent to "git log -g --abbrev-commit --pretty=oneline"
575 */
576int cmd_log_reflog(int argc, const char **argv, const char *prefix)
577{
578 struct rev_info rev;
32962c9b 579 struct setup_revision_opt opt;
cf39f54e 580
0657bcbf 581 init_grep_defaults();
ef90d6d4 582 git_config(git_log_config, NULL);
6b2f2d98 583
cf39f54e
LT
584 init_revisions(&rev, prefix);
585 init_reflog_walk(&rev.reflog_info);
cf39f54e 586 rev.verbose_header = 1;
32962c9b
JH
587 memset(&opt, 0, sizeof(opt));
588 opt.def = "HEAD";
4b56cf58 589 cmd_log_init_defaults(&rev);
0c47695a 590 rev.abbrev_commit = 1;
cf39f54e 591 rev.commit_format = CMIT_FMT_ONELINE;
4da45bef 592 rev.use_terminator = 1;
cf39f54e 593 rev.always_show_header = 1;
4b56cf58 594 cmd_log_init_finish(argc, argv, prefix, &rev, &opt);
cf39f54e 595
cf39f54e
LT
596 return cmd_log_walk(&rev);
597}
598
a633fca0 599int cmd_log(int argc, const char **argv, const char *prefix)
70827b15
LT
600{
601 struct rev_info rev;
32962c9b 602 struct setup_revision_opt opt;
70827b15 603
0657bcbf 604 init_grep_defaults();
ef90d6d4 605 git_config(git_log_config, NULL);
6b2f2d98 606
db6296a5 607 init_revisions(&rev, prefix);
70827b15 608 rev.always_show_header = 1;
32962c9b
JH
609 memset(&opt, 0, sizeof(opt));
610 opt.def = "HEAD";
d5f6b1d7 611 opt.revarg_opt = REVARG_COMMITTISH;
32962c9b 612 cmd_log_init(argc, argv, prefix, &rev, &opt);
9dafea26 613 return cmd_log_walk(&rev);
70827b15 614}
91efcf60 615
c06d2daa 616/* format-patch */
0377db77 617
917a8f89 618static const char *fmt_patch_suffix = ".patch";
49604a4d 619static int numbered = 0;
a567fdcb 620static int auto_number = 1;
20ff0680 621
0db5260b
JW
622static char *default_attach = NULL;
623
ca9e0a1b
SB
624static struct string_list extra_hdr;
625static struct string_list extra_to;
626static struct string_list extra_cc;
3ee79d9f
DB
627
628static void add_header(const char *value)
629{
ca9e0a1b 630 struct string_list_item *item;
3ee79d9f 631 int len = strlen(value);
c8c4450e 632 while (len && value[len - 1] == '\n')
3ee79d9f 633 len--;
ca9e0a1b 634
3ee79d9f 635 if (!strncasecmp(value, "to: ", 4)) {
1d2f80fa 636 item = string_list_append(&extra_to, value + 4);
ca9e0a1b
SB
637 len -= 4;
638 } else if (!strncasecmp(value, "cc: ", 4)) {
1d2f80fa 639 item = string_list_append(&extra_cc, value + 4);
ca9e0a1b
SB
640 len -= 4;
641 } else {
1d2f80fa 642 item = string_list_append(&extra_hdr, value);
3ee79d9f 643 }
ca9e0a1b
SB
644
645 item->string[len] = '\0';
3ee79d9f
DB
646}
647
30984ed2
TR
648#define THREAD_SHALLOW 1
649#define THREAD_DEEP 2
6622d9c7
SB
650static int thread;
651static int do_signoff;
652static const char *signature = git_version_string;
30984ed2 653
ef90d6d4 654static int git_format_config(const char *var, const char *value, void *cb)
20ff0680
JS
655{
656 if (!strcmp(var, "format.headers")) {
d7fb91c6 657 if (!value)
5c5f1d7c 658 die(_("format.headers without value"));
3ee79d9f 659 add_header(value);
20ff0680
JS
660 return 0;
661 }
70cff3ac
BH
662 if (!strcmp(var, "format.suffix"))
663 return git_config_string(&fmt_patch_suffix, var, value);
ae6c098f
SD
664 if (!strcmp(var, "format.to")) {
665 if (!value)
666 return config_error_nonbool(var);
1d2f80fa 667 string_list_append(&extra_to, value);
ae6c098f
SD
668 return 0;
669 }
fe8928e6
MV
670 if (!strcmp(var, "format.cc")) {
671 if (!value)
672 return config_error_nonbool(var);
1d2f80fa 673 string_list_append(&extra_cc, value);
fe8928e6
MV
674 return 0;
675 }
787570c7
PYH
676 if (!strcmp(var, "diff.color") || !strcmp(var, "color.diff") ||
677 !strcmp(var, "color.ui")) {
f3aafa4d
RA
678 return 0;
679 }
49604a4d 680 if (!strcmp(var, "format.numbered")) {
90f5c186 681 if (value && !strcasecmp(value, "auto")) {
49604a4d
BG
682 auto_number = 1;
683 return 0;
684 }
49604a4d 685 numbered = git_config_bool(var, value);
a567fdcb 686 auto_number = auto_number && numbered;
49604a4d
BG
687 return 0;
688 }
0db5260b
JW
689 if (!strcmp(var, "format.attach")) {
690 if (value && *value)
691 default_attach = xstrdup(value);
692 else
693 default_attach = xstrdup(git_version_string);
694 return 0;
695 }
30984ed2
TR
696 if (!strcmp(var, "format.thread")) {
697 if (value && !strcasecmp(value, "deep")) {
698 thread = THREAD_DEEP;
699 return 0;
700 }
701 if (value && !strcasecmp(value, "shallow")) {
702 thread = THREAD_SHALLOW;
703 return 0;
704 }
705 thread = git_config_bool(var, value) && THREAD_SHALLOW;
706 return 0;
707 }
1d1876e9
HV
708 if (!strcmp(var, "format.signoff")) {
709 do_signoff = git_config_bool(var, value);
710 return 0;
711 }
6622d9c7
SB
712 if (!strcmp(var, "format.signature"))
713 return git_config_string(&signature, var, value);
dbd21447 714
ef90d6d4 715 return git_log_config(var, value, cb);
20ff0680
JS
716}
717
81f3a188 718static FILE *realstdout = NULL;
efd02016 719static const char *output_directory = NULL;
9800a754 720static int outdir_offset;
81f3a188 721
a21c2f94
JK
722static int reopen_stdout(struct commit *commit, const char *subject,
723 struct rev_info *rev, int quiet)
0377db77 724{
cd2ef591 725 struct strbuf filename = STRBUF_INIT;
68cb7b6f 726 int suffix_len = strlen(rev->patch_suffix) + 1;
0377db77 727
2448482b 728 if (output_directory) {
cd2ef591
SB
729 strbuf_addstr(&filename, output_directory);
730 if (filename.len >=
731 PATH_MAX - FORMAT_PATCH_NAME_MAX - suffix_len)
5c5f1d7c 732 return error(_("name of output directory is too long"));
cd2ef591
SB
733 if (filename.buf[filename.len - 1] != '/')
734 strbuf_addch(&filename, '/');
2448482b 735 }
0377db77 736
38ec23ac
JH
737 if (rev->numbered_files)
738 strbuf_addf(&filename, "%d", rev->nr);
d28b5d47
JH
739 else if (commit)
740 fmt_output_commit(&filename, commit, rev);
38ec23ac 741 else
d28b5d47 742 fmt_output_subject(&filename, subject, rev);
e6ff0f42 743
250087f2 744 if (!quiet)
cd2ef591 745 fprintf(realstdout, "%s\n", filename.buf + outdir_offset);
ec2956df 746
cd2ef591 747 if (freopen(filename.buf, "w", stdout) == NULL)
5c5f1d7c 748 return error(_("Cannot open patch file %s"), filename.buf);
c06d2daa 749
cd2ef591 750 strbuf_release(&filename);
e6ff0f42 751 return 0;
0377db77
JS
752}
753
811929f1 754static void get_patch_ids(struct rev_info *rev, struct patch_ids *ids)
9c6efa36
JS
755{
756 struct rev_info check_rev;
757 struct commit *commit;
758 struct object *o1, *o2;
759 unsigned flags1, flags2;
9c6efa36
JS
760
761 if (rev->pending.nr != 2)
5c5f1d7c 762 die(_("Need exactly one range."));
9c6efa36
JS
763
764 o1 = rev->pending.objects[0].item;
765 flags1 = o1->flags;
766 o2 = rev->pending.objects[1].item;
767 flags2 = o2->flags;
768
769 if ((flags1 & UNINTERESTING) == (flags2 & UNINTERESTING))
5c5f1d7c 770 die(_("Not a range."));
9c6efa36 771
5d23e133 772 init_patch_ids(ids);
9c6efa36
JS
773
774 /* given a range a..b get all patch ids for b..a */
811929f1 775 init_revisions(&check_rev, rev->prefix);
51f4de3a 776 check_rev.max_parents = 1;
9c6efa36
JS
777 o1->flags ^= UNINTERESTING;
778 o2->flags ^= UNINTERESTING;
779 add_pending_object(&check_rev, o1, "o1");
780 add_pending_object(&check_rev, o2, "o2");
3d51e1b5 781 if (prepare_revision_walk(&check_rev))
5c5f1d7c 782 die(_("revision walk setup failed"));
9c6efa36
JS
783
784 while ((commit = get_revision(&check_rev)) != NULL) {
5d23e133 785 add_commit_patch_id(commit, ids);
9c6efa36
JS
786 }
787
788 /* reset for next revision walk */
81db0941
JS
789 clear_commit_marks((struct commit *)o1,
790 SEEN | UNINTERESTING | SHOWN | ADDED);
791 clear_commit_marks((struct commit *)o2,
792 SEEN | UNINTERESTING | SHOWN | ADDED);
9c6efa36
JS
793 o1->flags = flags1;
794 o2->flags = flags2;
795}
796
e1a37346 797static void gen_message_id(struct rev_info *info, char *base)
d1566f78 798{
f285a2d7 799 struct strbuf buf = STRBUF_INIT;
43ae9f47 800 strbuf_addf(&buf, "%s.%lu.git.%s", base,
c73f384f 801 (unsigned long) time(NULL),
59f9b8a9 802 git_committer_info(IDENT_NO_NAME|IDENT_NO_DATE|IDENT_STRICT));
e1a37346 803 info->message_id = strbuf_detach(&buf, NULL);
d1566f78
JT
804}
805
6622d9c7
SB
806static void print_signature(void)
807{
808 if (signature && *signature)
809 printf("-- \n%s\n\n", signature);
810}
811
739453a3
JH
812static void add_branch_description(struct strbuf *buf, const char *branch_name)
813{
814 struct strbuf desc = STRBUF_INIT;
815 if (!branch_name || !*branch_name)
816 return;
817 read_branch_desc(&desc, branch_name);
818 if (desc.len) {
819 strbuf_addch(buf, '\n');
820 strbuf_add(buf, desc.buf, desc.len);
821 strbuf_addch(buf, '\n');
822 }
823}
824
2bda2cf4 825static void make_cover_letter(struct rev_info *rev, int use_stdout,
2bda2cf4 826 struct commit *origin,
250087f2 827 int nr, struct commit **list, struct commit *head,
739453a3 828 const char *branch_name,
250087f2 829 int quiet)
a5a27c79
DB
830{
831 const char *committer;
a5a27c79
DB
832 const char *body = "*** SUBJECT HERE ***\n\n*** BLURB HERE ***\n";
833 const char *msg;
2bda2cf4 834 struct shortlog log;
f285a2d7 835 struct strbuf sb = STRBUF_INIT;
2bda2cf4 836 int i;
330db18c 837 const char *encoding = "UTF-8";
39fe578b 838 struct diff_options opts;
267123b4 839 int need_8bit_cte = 0;
6bf13944 840 struct pretty_print_context pp = {0};
a5a27c79
DB
841
842 if (rev->commit_format != CMIT_FMT_EMAIL)
5c5f1d7c 843 die(_("Cover letter needs email format"));
a5a27c79 844
cd2ef591 845 committer = git_committer_info(0);
6df514af 846
a21c2f94 847 if (!use_stdout &&
ce37596c 848 reopen_stdout(NULL, rev->numbered_files ? NULL : "cover-letter", rev, quiet))
a5a27c79
DB
849 return;
850
6bf13944 851 log_write_email_headers(rev, head, &pp.subject, &pp.after_subject,
267123b4 852 &need_8bit_cte);
a5a27c79 853
0a7f4483
JS
854 for (i = 0; !need_8bit_cte && i < nr; i++)
855 if (has_non_ascii(list[i]->buffer))
856 need_8bit_cte = 1;
857
a5a27c79 858 msg = body;
6bf13944
JK
859 pp.fmt = CMIT_FMT_EMAIL;
860 pp.date_mode = DATE_RFC2822;
861 pp_user_info(&pp, NULL, &sb, committer, encoding);
862 pp_title_line(&pp, &msg, &sb, encoding, need_8bit_cte);
863 pp_remainder(&pp, &msg, &sb, 0);
739453a3 864 add_branch_description(&sb, branch_name);
a5a27c79
DB
865 printf("%s\n", sb.buf);
866
867 strbuf_release(&sb);
868
2bda2cf4 869 shortlog_init(&log);
859c4fbe
JS
870 log.wrap_lines = 1;
871 log.wrap = 72;
872 log.in1 = 2;
873 log.in2 = 4;
2bda2cf4
DB
874 for (i = 0; i < nr; i++)
875 shortlog_add_commit(&log, list[i]);
876
877 shortlog_output(&log);
878
a5a27c79 879 /*
2bda2cf4 880 * We can only do diffstat with a unique reference point
a5a27c79
DB
881 */
882 if (!origin)
883 return;
884
5d02294c
JS
885 memcpy(&opts, &rev->diffopt, sizeof(opts));
886 opts.output_format = DIFF_FORMAT_SUMMARY | DIFF_FORMAT_DIFFSTAT;
a5a27c79 887
39fe578b
DB
888 diff_setup_done(&opts);
889
890 diff_tree_sha1(origin->tree->object.sha1,
891 head->tree->object.sha1,
892 "", &opts);
893 diffcore_std(&opts);
894 diff_flush(&opts);
a5a27c79 895
a5a27c79 896 printf("\n");
6622d9c7 897 print_signature();
d1566f78
JT
898}
899
8419d2ee
JH
900static const char *clean_message_id(const char *msg_id)
901{
902 char ch;
903 const char *a, *z, *m;
8419d2ee
JH
904
905 m = msg_id;
906 while ((ch = *m) && (isspace(ch) || (ch == '<')))
907 m++;
908 a = m;
909 z = NULL;
910 while ((ch = *m)) {
911 if (!isspace(ch) && (ch != '>'))
912 z = m;
913 m++;
914 }
915 if (!z)
5c5f1d7c 916 die(_("insane in-reply-to: %s"), msg_id);
8419d2ee
JH
917 if (++z == m)
918 return a;
182af834 919 return xmemdupz(a, z - a);
8419d2ee
JH
920}
921
9800a754
JH
922static const char *set_outdir(const char *prefix, const char *output_directory)
923{
924 if (output_directory && is_absolute_path(output_directory))
925 return output_directory;
926
927 if (!prefix || !*prefix) {
928 if (output_directory)
929 return output_directory;
930 /* The user did not explicitly ask for "./" */
931 outdir_offset = 2;
932 return "./";
933 }
934
935 outdir_offset = strlen(prefix);
936 if (!output_directory)
937 return prefix;
938
939 return xstrdup(prefix_filename(prefix, outdir_offset,
940 output_directory));
941}
942
fff02ee6 943static const char * const builtin_format_patch_usage[] = {
61007a58 944 N_("git format-patch [options] [<since> | <revision range>]"),
fff02ee6
SB
945 NULL
946};
947
948static int keep_subject = 0;
949
950static int keep_callback(const struct option *opt, const char *arg, int unset)
951{
952 ((struct rev_info *)opt->value)->total = -1;
953 keep_subject = 1;
954 return 0;
955}
956
957static int subject_prefix = 0;
958
959static int subject_prefix_callback(const struct option *opt, const char *arg,
960 int unset)
961{
962 subject_prefix = 1;
963 ((struct rev_info *)opt->value)->subject_prefix = arg;
964 return 0;
965}
966
1af4731b
JH
967static int numbered_cmdline_opt = 0;
968
fff02ee6
SB
969static int numbered_callback(const struct option *opt, const char *arg,
970 int unset)
971{
1af4731b 972 *(int *)opt->value = numbered_cmdline_opt = unset ? 0 : 1;
fff02ee6
SB
973 if (unset)
974 auto_number = 0;
975 return 0;
976}
977
978static int no_numbered_callback(const struct option *opt, const char *arg,
979 int unset)
980{
981 return numbered_callback(opt, arg, 1);
982}
983
984static int output_directory_callback(const struct option *opt, const char *arg,
985 int unset)
986{
987 const char **dir = (const char **)opt->value;
988 if (*dir)
5c5f1d7c 989 die(_("Two output directories?"));
fff02ee6
SB
990 *dir = arg;
991 return 0;
992}
993
994static int thread_callback(const struct option *opt, const char *arg, int unset)
995{
996 int *thread = (int *)opt->value;
997 if (unset)
998 *thread = 0;
999 else if (!arg || !strcmp(arg, "shallow"))
1000 *thread = THREAD_SHALLOW;
1001 else if (!strcmp(arg, "deep"))
1002 *thread = THREAD_DEEP;
1003 else
1004 return 1;
1005 return 0;
1006}
1007
1008static int attach_callback(const struct option *opt, const char *arg, int unset)
1009{
1010 struct rev_info *rev = (struct rev_info *)opt->value;
1011 if (unset)
1012 rev->mime_boundary = NULL;
1013 else if (arg)
1014 rev->mime_boundary = arg;
1015 else
1016 rev->mime_boundary = git_version_string;
1017 rev->no_inline = unset ? 0 : 1;
1018 return 0;
1019}
1020
1021static int inline_callback(const struct option *opt, const char *arg, int unset)
1022{
1023 struct rev_info *rev = (struct rev_info *)opt->value;
1024 if (unset)
1025 rev->mime_boundary = NULL;
1026 else if (arg)
1027 rev->mime_boundary = arg;
1028 else
1029 rev->mime_boundary = git_version_string;
1030 rev->no_inline = 0;
1031 return 0;
1032}
1033
1034static int header_callback(const struct option *opt, const char *arg, int unset)
1035{
c4260034
SB
1036 if (unset) {
1037 string_list_clear(&extra_hdr, 0);
1038 string_list_clear(&extra_to, 0);
1039 string_list_clear(&extra_cc, 0);
1040 } else {
1041 add_header(arg);
1042 }
fff02ee6
SB
1043 return 0;
1044}
1045
ae6c098f
SD
1046static int to_callback(const struct option *opt, const char *arg, int unset)
1047{
c4260034
SB
1048 if (unset)
1049 string_list_clear(&extra_to, 0);
1050 else
1d2f80fa 1051 string_list_append(&extra_to, arg);
fff02ee6
SB
1052 return 0;
1053}
1054
1055static int cc_callback(const struct option *opt, const char *arg, int unset)
1056{
c4260034
SB
1057 if (unset)
1058 string_list_clear(&extra_cc, 0);
1059 else
1d2f80fa 1060 string_list_append(&extra_cc, arg);
fff02ee6
SB
1061 return 0;
1062}
1063
739453a3
JH
1064static char *find_branch_name(struct rev_info *rev)
1065{
1066 int i, positive = -1;
1067 unsigned char branch_sha1[20];
5ee29aef 1068 const unsigned char *tip_sha1;
20b630aa
NTND
1069 const char *ref;
1070 char *full_ref, *branch = NULL;
739453a3
JH
1071
1072 for (i = 0; i < rev->cmdline.nr; i++) {
1073 if (rev->cmdline.rev[i].flags & UNINTERESTING)
1074 continue;
1075 if (positive < 0)
1076 positive = i;
1077 else
1078 return NULL;
1079 }
5ee29aef
NTND
1080 if (0 <= positive) {
1081 ref = rev->cmdline.rev[positive].name;
1082 tip_sha1 = rev->cmdline.rev[positive].item->sha1;
1083 } else if (!rev->cmdline.nr && rev->pending.nr == 1 &&
1084 !strcmp(rev->pending.objects[0].name, "HEAD")) {
1085 /*
1086 * No actual ref from command line, but "HEAD" from
1087 * rev->def was added in setup_revisions()
1088 * e.g. format-patch --cover-letter -12
1089 */
1090 ref = "HEAD";
1091 tip_sha1 = rev->pending.objects[0].item->sha1;
1092 } else {
739453a3 1093 return NULL;
5ee29aef 1094 }
20b630aa
NTND
1095 if (dwim_ref(ref, strlen(ref), branch_sha1, &full_ref) &&
1096 !prefixcmp(full_ref, "refs/heads/") &&
5ee29aef 1097 !hashcmp(tip_sha1, branch_sha1))
20b630aa
NTND
1098 branch = xstrdup(full_ref + strlen("refs/heads/"));
1099 free(full_ref);
1100 return branch;
739453a3
JH
1101}
1102
a633fca0 1103int cmd_format_patch(int argc, const char **argv, const char *prefix)
91efcf60
JH
1104{
1105 struct commit *commit;
1106 struct commit **list = NULL;
1107 struct rev_info rev;
32962c9b 1108 struct setup_revision_opt s_r_opt;
fff02ee6 1109 int nr = 0, total, i;
0377db77 1110 int use_stdout = 0;
fa0f02df 1111 int start_number = -1;
ce37596c 1112 int just_numbers = 0;
9c6efa36 1113 int ignore_if_in_upstream = 0;
a5a27c79 1114 int cover_letter = 0;
2bda2cf4 1115 int boundary_count = 0;
37c22a4b 1116 int no_binary_diff = 0;
a5a27c79 1117 struct commit *origin = NULL, *head = NULL;
76af0734 1118 const char *in_reply_to = NULL;
5d23e133 1119 struct patch_ids ids;
cf2251b6 1120 char *add_signoff = NULL;
f285a2d7 1121 struct strbuf buf = STRBUF_INIT;
1d46f2ea 1122 int use_patch_format = 0;
250087f2 1123 int quiet = 0;
5fe10fe8 1124 int reroll_count = -1;
739453a3 1125 char *branch_name = NULL;
fff02ee6
SB
1126 const struct option builtin_format_patch_options[] = {
1127 { OPTION_CALLBACK, 'n', "numbered", &numbered, NULL,
61007a58 1128 N_("use [PATCH n/m] even with a single patch"),
fff02ee6
SB
1129 PARSE_OPT_NOARG, numbered_callback },
1130 { OPTION_CALLBACK, 'N', "no-numbered", &numbered, NULL,
61007a58 1131 N_("use [PATCH] even with multiple patches"),
fff02ee6 1132 PARSE_OPT_NOARG, no_numbered_callback },
61007a58 1133 OPT_BOOLEAN('s', "signoff", &do_signoff, N_("add Signed-off-by:")),
fff02ee6 1134 OPT_BOOLEAN(0, "stdout", &use_stdout,
61007a58 1135 N_("print patches to standard out")),
fff02ee6 1136 OPT_BOOLEAN(0, "cover-letter", &cover_letter,
61007a58 1137 N_("generate a cover letter")),
ce37596c 1138 OPT_BOOLEAN(0, "numbered-files", &just_numbers,
61007a58
NTND
1139 N_("use simple number sequence for output file names")),
1140 OPT_STRING(0, "suffix", &fmt_patch_suffix, N_("sfx"),
1141 N_("use <sfx> instead of '.patch'")),
fff02ee6 1142 OPT_INTEGER(0, "start-number", &start_number,
61007a58 1143 N_("start numbering patches at <n> instead of 1")),
7952ea66 1144 OPT_INTEGER('v', "reroll-count", &reroll_count,
5fe10fe8 1145 N_("mark the series as Nth re-roll")),
61007a58
NTND
1146 { OPTION_CALLBACK, 0, "subject-prefix", &rev, N_("prefix"),
1147 N_("Use [<prefix>] instead of [PATCH]"),
fff02ee6
SB
1148 PARSE_OPT_NONEG, subject_prefix_callback },
1149 { OPTION_CALLBACK, 'o', "output-directory", &output_directory,
61007a58 1150 N_("dir"), N_("store resulting files in <dir>"),
fff02ee6
SB
1151 PARSE_OPT_NONEG, output_directory_callback },
1152 { OPTION_CALLBACK, 'k', "keep-subject", &rev, NULL,
61007a58 1153 N_("don't strip/add [PATCH]"),
fff02ee6
SB
1154 PARSE_OPT_NOARG | PARSE_OPT_NONEG, keep_callback },
1155 OPT_BOOLEAN(0, "no-binary", &no_binary_diff,
61007a58 1156 N_("don't output binary diffs")),
fff02ee6 1157 OPT_BOOLEAN(0, "ignore-if-in-upstream", &ignore_if_in_upstream,
61007a58 1158 N_("don't include a patch matching a commit upstream")),
2cfa8330 1159 { OPTION_BOOLEAN, 'p', "no-stat", &use_patch_format, NULL,
61007a58 1160 N_("show patch format instead of default (patch + stat)"),
2cfa8330 1161 PARSE_OPT_NONEG | PARSE_OPT_NOARG },
61007a58
NTND
1162 OPT_GROUP(N_("Messaging")),
1163 { OPTION_CALLBACK, 0, "add-header", NULL, N_("header"),
1164 N_("add email header"), 0, header_callback },
1165 { OPTION_CALLBACK, 0, "to", NULL, N_("email"), N_("add To: header"),
c4260034 1166 0, to_callback },
61007a58 1167 { OPTION_CALLBACK, 0, "cc", NULL, N_("email"), N_("add Cc: header"),
c4260034 1168 0, cc_callback },
61007a58
NTND
1169 OPT_STRING(0, "in-reply-to", &in_reply_to, N_("message-id"),
1170 N_("make first mail a reply to <message-id>")),
1171 { OPTION_CALLBACK, 0, "attach", &rev, N_("boundary"),
1172 N_("attach the patch"), PARSE_OPT_OPTARG,
fff02ee6 1173 attach_callback },
61007a58
NTND
1174 { OPTION_CALLBACK, 0, "inline", &rev, N_("boundary"),
1175 N_("inline the patch"),
fff02ee6
SB
1176 PARSE_OPT_OPTARG | PARSE_OPT_NONEG,
1177 inline_callback },
61007a58
NTND
1178 { OPTION_CALLBACK, 0, "thread", &thread, N_("style"),
1179 N_("enable message threading, styles: shallow, deep"),
fff02ee6 1180 PARSE_OPT_OPTARG, thread_callback },
61007a58
NTND
1181 OPT_STRING(0, "signature", &signature, N_("signature"),
1182 N_("add a signature")),
250087f2 1183 OPT_BOOLEAN(0, "quiet", &quiet,
61007a58 1184 N_("don't print the patch filenames")),
fff02ee6
SB
1185 OPT_END()
1186 };
91efcf60 1187
ca9e0a1b
SB
1188 extra_hdr.strdup_strings = 1;
1189 extra_to.strdup_strings = 1;
1190 extra_cc.strdup_strings = 1;
0657bcbf 1191 init_grep_defaults();
ef90d6d4 1192 git_config(git_format_config, NULL);
db6296a5 1193 init_revisions(&rev, prefix);
91efcf60
JH
1194 rev.commit_format = CMIT_FMT_EMAIL;
1195 rev.verbose_header = 1;
1196 rev.diff = 1;
ad5aeede 1197 rev.max_parents = 1;
8f67f8ae 1198 DIFF_OPT_SET(&rev.diffopt, RECURSIVE);
dbd21447 1199 rev.subject_prefix = fmt_patch_subject_prefix;
32962c9b
JH
1200 memset(&s_r_opt, 0, sizeof(s_r_opt));
1201 s_r_opt.def = "HEAD";
d5f6b1d7 1202 s_r_opt.revarg_opt = REVARG_COMMITTISH;
20ff0680 1203
0db5260b
JW
1204 if (default_attach) {
1205 rev.mime_boundary = default_attach;
1206 rev.no_inline = 1;
1207 }
1208
2448482b
JS
1209 /*
1210 * Parse the arguments before setup_revisions(), or something
2dc189a3 1211 * like "git format-patch -o a123 HEAD^.." may fail; a123 is
2448482b
JS
1212 * possibly a valid SHA1.
1213 */
37782920 1214 argc = parse_options(argc, argv, prefix, builtin_format_patch_options,
fff02ee6 1215 builtin_format_patch_usage,
382da402
FC
1216 PARSE_OPT_KEEP_ARGV0 | PARSE_OPT_KEEP_UNKNOWN |
1217 PARSE_OPT_KEEP_DASHDASH);
2448482b 1218
5fe10fe8
JH
1219 if (0 < reroll_count) {
1220 struct strbuf sprefix = STRBUF_INIT;
1221 strbuf_addf(&sprefix, "%s v%d",
1222 rev.subject_prefix, reroll_count);
1223 rev.reroll_count = reroll_count;
1224 rev.subject_prefix = strbuf_detach(&sprefix, NULL);
1225 }
1226
1d1876e9
HV
1227 if (do_signoff) {
1228 const char *committer;
1229 const char *endpos;
f9bc573f 1230 committer = git_committer_info(IDENT_STRICT);
1d1876e9
HV
1231 endpos = strchr(committer, '>');
1232 if (!endpos)
5c5f1d7c 1233 die(_("bogus committer info %s"), committer);
1d1876e9
HV
1234 add_signoff = xmemdupz(committer, endpos - committer + 1);
1235 }
1236
ca9e0a1b
SB
1237 for (i = 0; i < extra_hdr.nr; i++) {
1238 strbuf_addstr(&buf, extra_hdr.items[i].string);
3ee79d9f
DB
1239 strbuf_addch(&buf, '\n');
1240 }
1241
ca9e0a1b 1242 if (extra_to.nr)
3ee79d9f 1243 strbuf_addstr(&buf, "To: ");
ca9e0a1b 1244 for (i = 0; i < extra_to.nr; i++) {
3ee79d9f
DB
1245 if (i)
1246 strbuf_addstr(&buf, " ");
ca9e0a1b
SB
1247 strbuf_addstr(&buf, extra_to.items[i].string);
1248 if (i + 1 < extra_to.nr)
3ee79d9f
DB
1249 strbuf_addch(&buf, ',');
1250 strbuf_addch(&buf, '\n');
1251 }
1252
ca9e0a1b 1253 if (extra_cc.nr)
3ee79d9f 1254 strbuf_addstr(&buf, "Cc: ");
ca9e0a1b 1255 for (i = 0; i < extra_cc.nr; i++) {
3ee79d9f
DB
1256 if (i)
1257 strbuf_addstr(&buf, " ");
ca9e0a1b
SB
1258 strbuf_addstr(&buf, extra_cc.items[i].string);
1259 if (i + 1 < extra_cc.nr)
3ee79d9f
DB
1260 strbuf_addch(&buf, ',');
1261 strbuf_addch(&buf, '\n');
1262 }
1263
2af202be 1264 rev.extra_headers = strbuf_detach(&buf, NULL);
3ee79d9f 1265
add5c8a5 1266 if (start_number < 0)
fa0f02df 1267 start_number = 1;
ca6b91d2
JM
1268
1269 /*
1270 * If numbered is set solely due to format.numbered in config,
1271 * and it would conflict with --keep-subject (-k) from the
1272 * command line, reset "numbered".
1273 */
1274 if (numbered && keep_subject && !numbered_cmdline_opt)
1275 numbered = 0;
1276
63b398a4 1277 if (numbered && keep_subject)
5c5f1d7c 1278 die (_("-n and -k are mutually exclusive."));
2d9e4a47 1279 if (keep_subject && subject_prefix)
5c5f1d7c 1280 die (_("--subject-prefix and -k are mutually exclusive."));
9553d2b2 1281 rev.preserve_subject = keep_subject;
8ac80a57 1282
32962c9b 1283 argc = setup_revisions(argc, argv, &rev, &s_r_opt);
2448482b 1284 if (argc > 1)
5c5f1d7c 1285 die (_("unrecognized argument: %s"), argv[1]);
0377db77 1286
02bc5b03 1287 if (rev.diffopt.output_format & DIFF_FORMAT_NAME)
f338cb83 1288 die(_("--name-only does not make sense"));
02bc5b03 1289 if (rev.diffopt.output_format & DIFF_FORMAT_NAME_STATUS)
f338cb83 1290 die(_("--name-status does not make sense"));
02bc5b03 1291 if (rev.diffopt.output_format & DIFF_FORMAT_CHECKDIFF)
f338cb83 1292 die(_("--check does not make sense"));
02bc5b03
BG
1293
1294 if (!use_patch_format &&
1295 (!rev.diffopt.output_format ||
1296 rev.diffopt.output_format == DIFF_FORMAT_PATCH))
1297 rev.diffopt.output_format = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_SUMMARY;
1298
1299 /* Always generate a patch */
1300 rev.diffopt.output_format |= DIFF_FORMAT_PATCH;
c9b5ef99 1301
37c22a4b 1302 if (!DIFF_OPT_TST(&rev.diffopt, TEXT) && !no_binary_diff)
8f67f8ae 1303 DIFF_OPT_SET(&rev.diffopt, BINARY);
e47f306d 1304
894a9d33
TR
1305 if (rev.show_notes)
1306 init_display_notes(&rev.notes_opt);
1307
9800a754
JH
1308 if (!use_stdout)
1309 output_directory = set_outdir(prefix, output_directory);
38a94bb6
TRC
1310 else
1311 setup_pager();
77e565d8 1312
efd02016
JH
1313 if (output_directory) {
1314 if (use_stdout)
5c5f1d7c 1315 die(_("standard output, or directory, which one?"));
efd02016 1316 if (mkdir(output_directory, 0777) < 0 && errno != EEXIST)
5c5f1d7c 1317 die_errno(_("Could not create directory '%s'"),
0721c314 1318 output_directory);
efd02016
JH
1319 }
1320
1f1e895f 1321 if (rev.pending.nr == 1) {
8a1d076e
JH
1322 if (rev.max_count < 0 && !rev.show_root_diff) {
1323 /*
1324 * This is traditional behaviour of "git format-patch
1325 * origin" that prepares what the origin side still
1326 * does not have.
1327 */
739453a3
JH
1328 unsigned char sha1[20];
1329 const char *ref;
1330
7c496280 1331 rev.pending.objects[0].item->flags |= UNINTERESTING;
3384a2df 1332 add_head_to_pending(&rev);
8cad4744 1333 ref = resolve_ref_unsafe("HEAD", sha1, 1, NULL);
739453a3
JH
1334 if (ref && !prefixcmp(ref, "refs/heads/"))
1335 branch_name = xstrdup(ref + strlen("refs/heads/"));
1336 else
1337 branch_name = xstrdup(""); /* no branch */
7c496280 1338 }
8a1d076e
JH
1339 /*
1340 * Otherwise, it is "format-patch -22 HEAD", and/or
1341 * "format-patch --root HEAD". The user wants
1342 * get_revision() to do the usual traversal.
7c496280 1343 */
e686eb98 1344 }
68c2ec7f
JH
1345
1346 /*
1347 * We cannot move this anywhere earlier because we do want to
9517e6b8 1348 * know if --root was given explicitly from the command line.
68c2ec7f
JH
1349 */
1350 rev.show_root_diff = 1;
1351
a5a27c79 1352 if (cover_letter) {
739453a3
JH
1353 /*
1354 * NEEDSWORK:randomly pick one positive commit to show
1355 * diffstat; this is often the tip and the command
1356 * happens to do the right thing in most cases, but a
1357 * complex command like "--cover-letter a b c ^bottom"
1358 * picks "c" and shows diffstat between bottom..c
1359 * which may not match what the series represents at
1360 * all and totally broken.
1361 */
a5a27c79
DB
1362 int i;
1363 for (i = 0; i < rev.pending.nr; i++) {
1364 struct object *o = rev.pending.objects[i].item;
2bda2cf4 1365 if (!(o->flags & UNINTERESTING))
a5a27c79
DB
1366 head = (struct commit *)o;
1367 }
739453a3 1368 /* There is nothing to show; it is not an error, though. */
a5a27c79
DB
1369 if (!head)
1370 return 0;
739453a3
JH
1371 if (!branch_name)
1372 branch_name = find_branch_name(&rev);
a5a27c79 1373 }
e686eb98 1374
657ab61e
KB
1375 if (ignore_if_in_upstream) {
1376 /* Don't say anything if head and upstream are the same. */
1377 if (rev.pending.nr == 2) {
1378 struct object_array_entry *o = rev.pending.objects;
1379 if (hashcmp(o[0].item->sha1, o[1].item->sha1) == 0)
1380 return 0;
1381 }
811929f1 1382 get_patch_ids(&rev, &ids);
657ab61e 1383 }
9c6efa36 1384
81f3a188 1385 if (!use_stdout)
f5788250 1386 realstdout = xfdopen(xdup(1), "w");
81f3a188 1387
3d51e1b5 1388 if (prepare_revision_walk(&rev))
5c5f1d7c 1389 die(_("revision walk setup failed"));
2bda2cf4 1390 rev.boundary = 1;
91efcf60 1391 while ((commit = get_revision(&rev)) != NULL) {
2bda2cf4 1392 if (commit->object.flags & BOUNDARY) {
2bda2cf4
DB
1393 boundary_count++;
1394 origin = (boundary_count == 1) ? commit : NULL;
1395 continue;
1396 }
1397
9c6efa36 1398 if (ignore_if_in_upstream &&
5d23e133 1399 has_commit_patch_id(commit, &ids))
9c6efa36
JS
1400 continue;
1401
91efcf60 1402 nr++;
83572c1a 1403 list = xrealloc(list, nr * sizeof(list[0]));
91efcf60
JH
1404 list[nr - 1] = commit;
1405 }
0377db77 1406 total = nr;
49604a4d
BG
1407 if (!keep_subject && auto_number && total > 1)
1408 numbered = 1;
596524b3 1409 if (numbered)
fa0f02df 1410 rev.total = total + start_number - 1;
b079c50e
TR
1411 if (in_reply_to || thread || cover_letter)
1412 rev.ref_message_ids = xcalloc(1, sizeof(struct string_list));
1413 if (in_reply_to) {
1414 const char *msgid = clean_message_id(in_reply_to);
1d2f80fa 1415 string_list_append(rev.ref_message_ids, msgid);
b079c50e 1416 }
ce37596c 1417 rev.numbered_files = just_numbers;
108dab28 1418 rev.patch_suffix = fmt_patch_suffix;
a5a27c79
DB
1419 if (cover_letter) {
1420 if (thread)
1421 gen_message_id(&rev, "cover");
ce37596c 1422 make_cover_letter(&rev, use_stdout,
739453a3 1423 origin, nr, list, head, branch_name, quiet);
a5a27c79
DB
1424 total++;
1425 start_number--;
1426 }
1427 rev.add_signoff = add_signoff;
91efcf60
JH
1428 while (0 <= --nr) {
1429 int shown;
1430 commit = list[nr];
add5c8a5 1431 rev.nr = total - nr + (start_number - 1);
d1566f78 1432 /* Make the second and subsequent mails replies to the first */
cc35de84 1433 if (thread) {
a5a27c79 1434 /* Have we already had a message ID? */
e1a37346 1435 if (rev.message_id) {
a5a27c79 1436 /*
30984ed2
TR
1437 * For deep threading: make every mail
1438 * a reply to the previous one, no
1439 * matter what other options are set.
1440 *
1441 * For shallow threading:
1442 *
2175c10d
TR
1443 * Without --cover-letter and
1444 * --in-reply-to, make every mail a
1445 * reply to the one before.
1446 *
1447 * With --in-reply-to but no
1448 * --cover-letter, make every mail a
1449 * reply to the <reply-to>.
1450 *
1451 * With --cover-letter, make every
1452 * mail but the cover letter a reply
1453 * to the cover letter. The cover
1454 * letter is a reply to the
1455 * --in-reply-to, if specified.
a5a27c79 1456 */
30984ed2
TR
1457 if (thread == THREAD_SHALLOW
1458 && rev.ref_message_ids->nr > 0
2175c10d 1459 && (!cover_letter || rev.nr > 1))
e1a37346
DB
1460 free(rev.message_id);
1461 else
1d2f80fa
JP
1462 string_list_append(rev.ref_message_ids,
1463 rev.message_id);
cc35de84 1464 }
e1a37346 1465 gen_message_id(&rev, sha1_to_hex(commit->object.sha1));
d1566f78 1466 }
6df514af 1467
a21c2f94 1468 if (!use_stdout &&
ce37596c 1469 reopen_stdout(rev.numbered_files ? NULL : commit, NULL, &rev, quiet))
5c5f1d7c 1470 die(_("Failed to create output files"));
91efcf60
JH
1471 shown = log_tree_commit(&rev, commit);
1472 free(commit->buffer);
1473 commit->buffer = NULL;
add5c8a5
JH
1474
1475 /* We put one extra blank line between formatted
1476 * patches and this flag is used by log-tree code
1477 * to see if it needs to emit a LF before showing
1478 * the log; when using one file per patch, we do
1479 * not want the extra blank line.
1480 */
1481 if (!use_stdout)
1482 rev.shown_one = 0;
698ce6f8
JS
1483 if (shown) {
1484 if (rev.mime_boundary)
1485 printf("\n--%s%s--\n\n\n",
1486 mime_boundary_leader,
1487 rev.mime_boundary);
1488 else
6622d9c7 1489 print_signature();
698ce6f8 1490 }
0377db77
JS
1491 if (!use_stdout)
1492 fclose(stdout);
91efcf60
JH
1493 }
1494 free(list);
739453a3 1495 free(branch_name);
ca9e0a1b
SB
1496 string_list_clear(&extra_to, 0);
1497 string_list_clear(&extra_cc, 0);
1498 string_list_clear(&extra_hdr, 0);
5d23e133
JH
1499 if (ignore_if_in_upstream)
1500 free_patch_ids(&ids);
91efcf60
JH
1501 return 0;
1502}
1503
e827633a
RS
1504static int add_pending_commit(const char *arg, struct rev_info *revs, int flags)
1505{
1506 unsigned char sha1[20];
1507 if (get_sha1(arg, sha1) == 0) {
1508 struct commit *commit = lookup_commit_reference(sha1);
1509 if (commit) {
1510 commit->object.flags |= flags;
1511 add_pending_object(revs, &commit->object, arg);
1512 return 0;
1513 }
1514 }
1515 return -1;
1516}
1517
28a53178 1518static const char * const cherry_usage[] = {
986d1bb8 1519 N_("git cherry [-v] [<upstream> [<head> [<limit>]]]"),
28a53178
EFL
1520 NULL
1521};
1522
a3a32e7f
JN
1523static void print_commit(char sign, struct commit *commit, int verbose,
1524 int abbrev)
1525{
1526 if (!verbose) {
1527 printf("%c %s\n", sign,
1528 find_unique_abbrev(commit->object.sha1, abbrev));
1529 } else {
1530 struct strbuf buf = STRBUF_INIT;
f67d2e82 1531 pp_commit_easy(CMIT_FMT_ONELINE, commit, &buf);
a3a32e7f
JN
1532 printf("%c %s %s\n", sign,
1533 find_unique_abbrev(commit->object.sha1, abbrev),
1534 buf.buf);
1535 strbuf_release(&buf);
1536 }
1537}
1538
e827633a
RS
1539int cmd_cherry(int argc, const char **argv, const char *prefix)
1540{
1541 struct rev_info revs;
5d23e133 1542 struct patch_ids ids;
e827633a
RS
1543 struct commit *commit;
1544 struct commit_list *list = NULL;
f2968022 1545 struct branch *current_branch;
e827633a
RS
1546 const char *upstream;
1547 const char *head = "HEAD";
1548 const char *limit = NULL;
28a53178 1549 int verbose = 0, abbrev = 0;
e827633a 1550
28a53178
EFL
1551 struct option options[] = {
1552 OPT__ABBREV(&abbrev),
986d1bb8 1553 OPT__VERBOSE(&verbose, N_("be verbose")),
28a53178
EFL
1554 OPT_END()
1555 };
e827633a 1556
28a53178 1557 argc = parse_options(argc, argv, prefix, options, cherry_usage, 0);
fef34270 1558
e827633a 1559 switch (argc) {
e827633a 1560 case 3:
28a53178 1561 limit = argv[2];
e827633a
RS
1562 /* FALLTHROUGH */
1563 case 2:
28a53178
EFL
1564 head = argv[1];
1565 /* FALLTHROUGH */
1566 case 1:
1567 upstream = argv[0];
e827633a
RS
1568 break;
1569 default:
f2968022
MH
1570 current_branch = branch_get(NULL);
1571 if (!current_branch || !current_branch->merge
1572 || !current_branch->merge[0]
1573 || !current_branch->merge[0]->dst) {
5c5f1d7c 1574 fprintf(stderr, _("Could not find a tracked"
f2968022 1575 " remote branch, please"
5c5f1d7c 1576 " specify <upstream> manually.\n"));
28a53178 1577 usage_with_options(cherry_usage, options);
f2968022
MH
1578 }
1579
1580 upstream = current_branch->merge[0]->dst;
e827633a
RS
1581 }
1582
1583 init_revisions(&revs, prefix);
51f4de3a 1584 revs.max_parents = 1;
e827633a
RS
1585
1586 if (add_pending_commit(head, &revs, 0))
5c5f1d7c 1587 die(_("Unknown commit %s"), head);
e827633a 1588 if (add_pending_commit(upstream, &revs, UNINTERESTING))
5c5f1d7c 1589 die(_("Unknown commit %s"), upstream);
e827633a
RS
1590
1591 /* Don't say anything if head and upstream are the same. */
1592 if (revs.pending.nr == 2) {
1593 struct object_array_entry *o = revs.pending.objects;
1594 if (hashcmp(o[0].item->sha1, o[1].item->sha1) == 0)
1595 return 0;
1596 }
1597
811929f1 1598 get_patch_ids(&revs, &ids);
e827633a
RS
1599
1600 if (limit && add_pending_commit(limit, &revs, UNINTERESTING))
5c5f1d7c 1601 die(_("Unknown commit %s"), limit);
e827633a
RS
1602
1603 /* reverse the list of commits */
3d51e1b5 1604 if (prepare_revision_walk(&revs))
5c5f1d7c 1605 die(_("revision walk setup failed"));
e827633a 1606 while ((commit = get_revision(&revs)) != NULL) {
e827633a
RS
1607 commit_list_insert(commit, &list);
1608 }
1609
1610 while (list) {
e827633a
RS
1611 char sign = '+';
1612
1613 commit = list->item;
5d23e133 1614 if (has_commit_patch_id(commit, &ids))
e827633a 1615 sign = '-';
a3a32e7f 1616 print_commit(sign, commit, verbose, abbrev);
e827633a
RS
1617 list = list->next;
1618 }
1619
5d23e133 1620 free_patch_ids(&ids);
e827633a
RS
1621 return 0;
1622}