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