]> git.ipfire.org Git - thirdparty/git.git/blob - builtin/log.c
config: do not leak excludes_file
[thirdparty/git.git] / builtin / log.c
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"
8 #include "config.h"
9 #include "refs.h"
10 #include "object-store.h"
11 #include "color.h"
12 #include "commit.h"
13 #include "diff.h"
14 #include "diff-merges.h"
15 #include "revision.h"
16 #include "log-tree.h"
17 #include "builtin.h"
18 #include "tag.h"
19 #include "reflog-walk.h"
20 #include "patch-ids.h"
21 #include "run-command.h"
22 #include "shortlog.h"
23 #include "remote.h"
24 #include "string-list.h"
25 #include "parse-options.h"
26 #include "line-log.h"
27 #include "branch.h"
28 #include "streaming.h"
29 #include "version.h"
30 #include "mailmap.h"
31 #include "gpg-interface.h"
32 #include "progress.h"
33 #include "commit-slab.h"
34 #include "repository.h"
35 #include "commit-reach.h"
36 #include "range-diff.h"
37 #include "tmp-objdir.h"
38
39 #define MAIL_DEFAULT_WRAP 72
40 #define COVER_FROM_AUTO_MAX_SUBJECT_LEN 100
41 #define FORMAT_PATCH_NAME_MAX_DEFAULT 64
42
43 /* Set a default date-time format for git log ("log.date" config variable) */
44 static const char *default_date_mode = NULL;
45
46 static int default_abbrev_commit;
47 static int default_show_root = 1;
48 static int default_follow;
49 static int default_show_signature;
50 static int default_encode_email_headers = 1;
51 static int decoration_style;
52 static int decoration_given;
53 static int use_mailmap_config = 1;
54 static unsigned int force_in_body_from;
55 static int stdout_mboxrd;
56 static const char *fmt_patch_subject_prefix = "PATCH";
57 static int fmt_patch_name_max = FORMAT_PATCH_NAME_MAX_DEFAULT;
58 static const char *fmt_pretty;
59
60 static const char * const builtin_log_usage[] = {
61 N_("git log [<options>] [<revision-range>] [[--] <path>...]"),
62 N_("git show [<options>] <object>..."),
63 NULL
64 };
65
66 struct line_opt_callback_data {
67 struct rev_info *rev;
68 const char *prefix;
69 struct string_list args;
70 };
71
72 static int session_is_interactive(void)
73 {
74 return isatty(1) || pager_in_use();
75 }
76
77 static int auto_decoration_style(void)
78 {
79 return session_is_interactive() ? DECORATE_SHORT_REFS : 0;
80 }
81
82 static int parse_decoration_style(const char *value)
83 {
84 switch (git_parse_maybe_bool(value)) {
85 case 1:
86 return DECORATE_SHORT_REFS;
87 case 0:
88 return 0;
89 default:
90 break;
91 }
92 if (!strcmp(value, "full"))
93 return DECORATE_FULL_REFS;
94 else if (!strcmp(value, "short"))
95 return DECORATE_SHORT_REFS;
96 else if (!strcmp(value, "auto"))
97 return auto_decoration_style();
98 /*
99 * Please update _git_log() in git-completion.bash when you
100 * add new decoration styles.
101 */
102 return -1;
103 }
104
105 static int use_default_decoration_filter = 1;
106 static struct string_list decorate_refs_exclude = STRING_LIST_INIT_NODUP;
107 static struct string_list decorate_refs_exclude_config = STRING_LIST_INIT_NODUP;
108 static struct string_list decorate_refs_include = STRING_LIST_INIT_NODUP;
109
110 static int clear_decorations_callback(const struct option *opt,
111 const char *arg, int unset)
112 {
113 string_list_clear(&decorate_refs_include, 0);
114 string_list_clear(&decorate_refs_exclude, 0);
115 use_default_decoration_filter = 0;
116 return 0;
117 }
118
119 static int decorate_callback(const struct option *opt, const char *arg, int unset)
120 {
121 if (unset)
122 decoration_style = 0;
123 else if (arg)
124 decoration_style = parse_decoration_style(arg);
125 else
126 decoration_style = DECORATE_SHORT_REFS;
127
128 if (decoration_style < 0)
129 die(_("invalid --decorate option: %s"), arg);
130
131 decoration_given = 1;
132
133 return 0;
134 }
135
136 static int log_line_range_callback(const struct option *option, const char *arg, int unset)
137 {
138 struct line_opt_callback_data *data = option->value;
139
140 BUG_ON_OPT_NEG(unset);
141
142 if (!arg)
143 return -1;
144
145 data->rev->line_level_traverse = 1;
146 string_list_append(&data->args, arg);
147
148 return 0;
149 }
150
151 static void init_log_defaults(void)
152 {
153 init_diff_ui_defaults();
154
155 decoration_style = auto_decoration_style();
156 }
157
158 static void cmd_log_init_defaults(struct rev_info *rev)
159 {
160 if (fmt_pretty)
161 get_commit_format(fmt_pretty, rev);
162 if (default_follow)
163 rev->diffopt.flags.default_follow_renames = 1;
164 rev->verbose_header = 1;
165 rev->diffopt.flags.recursive = 1;
166 rev->diffopt.stat_width = -1; /* use full terminal width */
167 rev->diffopt.stat_graph_width = -1; /* respect statGraphWidth config */
168 rev->abbrev_commit = default_abbrev_commit;
169 rev->show_root_diff = default_show_root;
170 rev->subject_prefix = fmt_patch_subject_prefix;
171 rev->patch_name_max = fmt_patch_name_max;
172 rev->show_signature = default_show_signature;
173 rev->encode_email_headers = default_encode_email_headers;
174 rev->diffopt.flags.allow_textconv = 1;
175
176 if (default_date_mode)
177 parse_date_format(default_date_mode, &rev->date_mode);
178 }
179
180 static void set_default_decoration_filter(struct decoration_filter *decoration_filter)
181 {
182 int i;
183 char *value = NULL;
184 struct string_list *include = decoration_filter->include_ref_pattern;
185 const struct string_list *config_exclude =
186 git_config_get_value_multi("log.excludeDecoration");
187
188 if (config_exclude) {
189 struct string_list_item *item;
190 for_each_string_list_item(item, config_exclude)
191 string_list_append(decoration_filter->exclude_ref_config_pattern,
192 item->string);
193 }
194
195 /*
196 * By default, decorate_all is disabled. Enable it if
197 * log.initialDecorationSet=all. Don't ever disable it by config,
198 * since the command-line takes precedent.
199 */
200 if (use_default_decoration_filter &&
201 !git_config_get_string("log.initialdecorationset", &value) &&
202 !strcmp("all", value))
203 use_default_decoration_filter = 0;
204 free(value);
205
206 if (!use_default_decoration_filter ||
207 decoration_filter->exclude_ref_pattern->nr ||
208 decoration_filter->include_ref_pattern->nr ||
209 decoration_filter->exclude_ref_config_pattern->nr)
210 return;
211
212 /*
213 * No command-line or config options were given, so
214 * populate with sensible defaults.
215 */
216 for (i = 0; i < ARRAY_SIZE(ref_namespace); i++) {
217 if (!ref_namespace[i].decoration)
218 continue;
219
220 string_list_append(include, ref_namespace[i].ref);
221 }
222 }
223
224 static void cmd_log_init_finish(int argc, const char **argv, const char *prefix,
225 struct rev_info *rev, struct setup_revision_opt *opt)
226 {
227 struct userformat_want w;
228 int quiet = 0, source = 0, mailmap;
229 static struct line_opt_callback_data line_cb = {NULL, NULL, STRING_LIST_INIT_DUP};
230 struct decoration_filter decoration_filter = {
231 .exclude_ref_pattern = &decorate_refs_exclude,
232 .include_ref_pattern = &decorate_refs_include,
233 .exclude_ref_config_pattern = &decorate_refs_exclude_config,
234 };
235 static struct revision_sources revision_sources;
236
237 const struct option builtin_log_options[] = {
238 OPT__QUIET(&quiet, N_("suppress diff output")),
239 OPT_BOOL(0, "source", &source, N_("show source")),
240 OPT_BOOL(0, "use-mailmap", &mailmap, N_("use mail map file")),
241 OPT_ALIAS(0, "mailmap", "use-mailmap"),
242 OPT_CALLBACK_F(0, "clear-decorations", NULL, NULL,
243 N_("clear all previously-defined decoration filters"),
244 PARSE_OPT_NOARG | PARSE_OPT_NONEG,
245 clear_decorations_callback),
246 OPT_STRING_LIST(0, "decorate-refs", &decorate_refs_include,
247 N_("pattern"), N_("only decorate refs that match <pattern>")),
248 OPT_STRING_LIST(0, "decorate-refs-exclude", &decorate_refs_exclude,
249 N_("pattern"), N_("do not decorate refs that match <pattern>")),
250 OPT_CALLBACK_F(0, "decorate", NULL, NULL, N_("decorate options"),
251 PARSE_OPT_OPTARG, decorate_callback),
252 OPT_CALLBACK('L', NULL, &line_cb, "range:file",
253 N_("trace the evolution of line range <start>,<end> or function :<funcname> in <file>"),
254 log_line_range_callback),
255 OPT_END()
256 };
257
258 line_cb.rev = rev;
259 line_cb.prefix = prefix;
260
261 mailmap = use_mailmap_config;
262 argc = parse_options(argc, argv, prefix,
263 builtin_log_options, builtin_log_usage,
264 PARSE_OPT_KEEP_ARGV0 | PARSE_OPT_KEEP_UNKNOWN_OPT |
265 PARSE_OPT_KEEP_DASHDASH);
266
267 if (quiet)
268 rev->diffopt.output_format |= DIFF_FORMAT_NO_OUTPUT;
269 argc = setup_revisions(argc, argv, rev, opt);
270
271 /* Any arguments at this point are not recognized */
272 if (argc > 1)
273 die(_("unrecognized argument: %s"), argv[1]);
274
275 if (rev->line_level_traverse && rev->prune_data.nr)
276 die(_("-L<range>:<file> cannot be used with pathspec"));
277
278 memset(&w, 0, sizeof(w));
279 userformat_find_requirements(NULL, &w);
280
281 if (!rev->show_notes_given && (!rev->pretty_given || w.notes))
282 rev->show_notes = 1;
283 if (rev->show_notes)
284 load_display_notes(&rev->notes_opt);
285
286 if ((rev->diffopt.pickaxe_opts & DIFF_PICKAXE_KINDS_MASK) ||
287 rev->diffopt.filter || rev->diffopt.flags.follow_renames)
288 rev->always_show_header = 0;
289
290 if (source || w.source) {
291 init_revision_sources(&revision_sources);
292 rev->sources = &revision_sources;
293 }
294
295 if (mailmap) {
296 rev->mailmap = xmalloc(sizeof(struct string_list));
297 string_list_init_nodup(rev->mailmap);
298 read_mailmap(rev->mailmap);
299 }
300
301 if (rev->pretty_given && rev->commit_format == CMIT_FMT_RAW) {
302 /*
303 * "log --pretty=raw" is special; ignore UI oriented
304 * configuration variables such as decoration.
305 */
306 if (!decoration_given)
307 decoration_style = 0;
308 if (!rev->abbrev_commit_given)
309 rev->abbrev_commit = 0;
310 }
311
312 if (rev->commit_format == CMIT_FMT_USERFORMAT) {
313 if (!w.decorate) {
314 /*
315 * Disable decoration loading if the format will not
316 * show them anyway.
317 */
318 decoration_style = 0;
319 } else if (!decoration_style) {
320 /*
321 * If we are going to show them, make sure we do load
322 * them here, but taking care not to override a
323 * specific style set by config or --decorate.
324 */
325 decoration_style = DECORATE_SHORT_REFS;
326 }
327 }
328
329 if (decoration_style || rev->simplify_by_decoration) {
330 set_default_decoration_filter(&decoration_filter);
331
332 if (decoration_style)
333 rev->show_decorations = 1;
334
335 load_ref_decorations(&decoration_filter, decoration_style);
336 }
337
338 if (rev->line_level_traverse)
339 line_log_init(rev, line_cb.prefix, &line_cb.args);
340
341 setup_pager();
342 }
343
344 static void cmd_log_init(int argc, const char **argv, const char *prefix,
345 struct rev_info *rev, struct setup_revision_opt *opt)
346 {
347 cmd_log_init_defaults(rev);
348 cmd_log_init_finish(argc, argv, prefix, rev, opt);
349 }
350
351 static int cmd_log_deinit(int ret, struct rev_info *rev)
352 {
353 release_revisions(rev);
354 return ret;
355 }
356
357 /*
358 * This gives a rough estimate for how many commits we
359 * will print out in the list.
360 */
361 static int estimate_commit_count(struct commit_list *list)
362 {
363 int n = 0;
364
365 while (list) {
366 struct commit *commit = list->item;
367 unsigned int flags = commit->object.flags;
368 list = list->next;
369 if (!(flags & (TREESAME | UNINTERESTING)))
370 n++;
371 }
372 return n;
373 }
374
375 static void show_early_header(struct rev_info *rev, const char *stage, int nr)
376 {
377 if (rev->shown_one) {
378 rev->shown_one = 0;
379 if (rev->commit_format != CMIT_FMT_ONELINE)
380 putchar(rev->diffopt.line_termination);
381 }
382 fprintf(rev->diffopt.file, _("Final output: %d %s\n"), nr, stage);
383 }
384
385 static struct itimerval early_output_timer;
386
387 static void log_show_early(struct rev_info *revs, struct commit_list *list)
388 {
389 int i = revs->early_output;
390 int show_header = 1;
391 int no_free = revs->diffopt.no_free;
392
393 revs->diffopt.no_free = 0;
394 sort_in_topological_order(&list, revs->sort_order);
395 while (list && i) {
396 struct commit *commit = list->item;
397 switch (simplify_commit(revs, commit)) {
398 case commit_show:
399 if (show_header) {
400 int n = estimate_commit_count(list);
401 show_early_header(revs, "incomplete", n);
402 show_header = 0;
403 }
404 log_tree_commit(revs, commit);
405 i--;
406 break;
407 case commit_ignore:
408 break;
409 case commit_error:
410 revs->diffopt.no_free = no_free;
411 diff_free(&revs->diffopt);
412 return;
413 }
414 list = list->next;
415 }
416
417 /* Did we already get enough commits for the early output? */
418 if (!i) {
419 revs->diffopt.no_free = 0;
420 diff_free(&revs->diffopt);
421 return;
422 }
423
424 /*
425 * ..if no, then repeat it twice a second until we
426 * do.
427 *
428 * NOTE! We don't use "it_interval", because if the
429 * reader isn't listening, we want our output to be
430 * throttled by the writing, and not have the timer
431 * trigger every second even if we're blocked on a
432 * reader!
433 */
434 early_output_timer.it_value.tv_sec = 0;
435 early_output_timer.it_value.tv_usec = 500000;
436 setitimer(ITIMER_REAL, &early_output_timer, NULL);
437 }
438
439 static void early_output(int signal)
440 {
441 show_early_output = log_show_early;
442 }
443
444 static void setup_early_output(void)
445 {
446 struct sigaction sa;
447
448 /*
449 * Set up the signal handler, minimally intrusively:
450 * we only set a single volatile integer word (not
451 * using sigatomic_t - trying to avoid unnecessary
452 * system dependencies and headers), and using
453 * SA_RESTART.
454 */
455 memset(&sa, 0, sizeof(sa));
456 sa.sa_handler = early_output;
457 sigemptyset(&sa.sa_mask);
458 sa.sa_flags = SA_RESTART;
459 sigaction(SIGALRM, &sa, NULL);
460
461 /*
462 * If we can get the whole output in less than a
463 * tenth of a second, don't even bother doing the
464 * early-output thing..
465 *
466 * This is a one-time-only trigger.
467 */
468 early_output_timer.it_value.tv_sec = 0;
469 early_output_timer.it_value.tv_usec = 100000;
470 setitimer(ITIMER_REAL, &early_output_timer, NULL);
471 }
472
473 static void finish_early_output(struct rev_info *rev)
474 {
475 int n = estimate_commit_count(rev->commits);
476 signal(SIGALRM, SIG_IGN);
477 show_early_header(rev, "done", n);
478 }
479
480 static int cmd_log_walk_no_free(struct rev_info *rev)
481 {
482 struct commit *commit;
483 int saved_nrl = 0;
484 int saved_dcctc = 0;
485
486 if (rev->remerge_diff) {
487 rev->remerge_objdir = tmp_objdir_create("remerge-diff");
488 if (!rev->remerge_objdir)
489 die(_("unable to create temporary object directory"));
490 tmp_objdir_replace_primary_odb(rev->remerge_objdir, 1);
491 }
492
493 if (rev->early_output)
494 setup_early_output();
495
496 if (prepare_revision_walk(rev))
497 die(_("revision walk setup failed"));
498
499 if (rev->early_output)
500 finish_early_output(rev);
501
502 /*
503 * For --check and --exit-code, the exit code is based on CHECK_FAILED
504 * and HAS_CHANGES being accumulated in rev->diffopt, so be careful to
505 * retain that state information if replacing rev->diffopt in this loop
506 */
507 while ((commit = get_revision(rev)) != NULL) {
508 if (!log_tree_commit(rev, commit) && rev->max_count >= 0)
509 /*
510 * We decremented max_count in get_revision,
511 * but we didn't actually show the commit.
512 */
513 rev->max_count++;
514 if (!rev->reflog_info) {
515 /*
516 * We may show a given commit multiple times when
517 * walking the reflogs.
518 */
519 free_commit_buffer(the_repository->parsed_objects,
520 commit);
521 free_commit_list(commit->parents);
522 commit->parents = NULL;
523 }
524 if (saved_nrl < rev->diffopt.needed_rename_limit)
525 saved_nrl = rev->diffopt.needed_rename_limit;
526 if (rev->diffopt.degraded_cc_to_c)
527 saved_dcctc = 1;
528 }
529 rev->diffopt.degraded_cc_to_c = saved_dcctc;
530 rev->diffopt.needed_rename_limit = saved_nrl;
531
532 if (rev->remerge_diff) {
533 tmp_objdir_destroy(rev->remerge_objdir);
534 rev->remerge_objdir = NULL;
535 }
536
537 if (rev->diffopt.output_format & DIFF_FORMAT_CHECKDIFF &&
538 rev->diffopt.flags.check_failed) {
539 return 02;
540 }
541 return diff_result_code(&rev->diffopt, 0);
542 }
543
544 static int cmd_log_walk(struct rev_info *rev)
545 {
546 int retval;
547
548 rev->diffopt.no_free = 1;
549 retval = cmd_log_walk_no_free(rev);
550 rev->diffopt.no_free = 0;
551 diff_free(&rev->diffopt);
552 return retval;
553 }
554
555 static int git_log_config(const char *var, const char *value, void *cb)
556 {
557 const char *slot_name;
558
559 if (!strcmp(var, "format.pretty"))
560 return git_config_string(&fmt_pretty, var, value);
561 if (!strcmp(var, "format.subjectprefix"))
562 return git_config_string(&fmt_patch_subject_prefix, var, value);
563 if (!strcmp(var, "format.filenamemaxlength")) {
564 fmt_patch_name_max = git_config_int(var, value);
565 return 0;
566 }
567 if (!strcmp(var, "format.encodeemailheaders")) {
568 default_encode_email_headers = git_config_bool(var, value);
569 return 0;
570 }
571 if (!strcmp(var, "log.abbrevcommit")) {
572 default_abbrev_commit = git_config_bool(var, value);
573 return 0;
574 }
575 if (!strcmp(var, "log.date"))
576 return git_config_string(&default_date_mode, var, value);
577 if (!strcmp(var, "log.decorate")) {
578 decoration_style = parse_decoration_style(value);
579 if (decoration_style < 0)
580 decoration_style = 0; /* maybe warn? */
581 return 0;
582 }
583 if (!strcmp(var, "log.diffmerges"))
584 return diff_merges_config(value);
585 if (!strcmp(var, "log.showroot")) {
586 default_show_root = git_config_bool(var, value);
587 return 0;
588 }
589 if (!strcmp(var, "log.follow")) {
590 default_follow = git_config_bool(var, value);
591 return 0;
592 }
593 if (skip_prefix(var, "color.decorate.", &slot_name))
594 return parse_decorate_color_config(var, slot_name, value);
595 if (!strcmp(var, "log.mailmap")) {
596 use_mailmap_config = git_config_bool(var, value);
597 return 0;
598 }
599 if (!strcmp(var, "log.showsignature")) {
600 default_show_signature = git_config_bool(var, value);
601 return 0;
602 }
603
604 if (git_gpg_config(var, value, cb) < 0)
605 return -1;
606 return git_diff_ui_config(var, value, cb);
607 }
608
609 int cmd_whatchanged(int argc, const char **argv, const char *prefix)
610 {
611 struct rev_info rev;
612 struct setup_revision_opt opt;
613
614 init_log_defaults();
615 git_config(git_log_config, NULL);
616
617 repo_init_revisions(the_repository, &rev, prefix);
618 git_config(grep_config, &rev.grep_filter);
619
620 rev.diff = 1;
621 rev.simplify_history = 0;
622 memset(&opt, 0, sizeof(opt));
623 opt.def = "HEAD";
624 opt.revarg_opt = REVARG_COMMITTISH;
625 cmd_log_init(argc, argv, prefix, &rev, &opt);
626 if (!rev.diffopt.output_format)
627 rev.diffopt.output_format = DIFF_FORMAT_RAW;
628 return cmd_log_deinit(cmd_log_walk(&rev), &rev);
629 }
630
631 static void show_tagger(const char *buf, struct rev_info *rev)
632 {
633 struct strbuf out = STRBUF_INIT;
634 struct pretty_print_context pp = {0};
635
636 pp.fmt = rev->commit_format;
637 pp.date_mode = rev->date_mode;
638 pp_user_info(&pp, "Tagger", &out, buf, get_log_output_encoding());
639 fprintf(rev->diffopt.file, "%s", out.buf);
640 strbuf_release(&out);
641 }
642
643 static int show_blob_object(const struct object_id *oid, struct rev_info *rev, const char *obj_name)
644 {
645 struct object_id oidc;
646 struct object_context obj_context;
647 char *buf;
648 unsigned long size;
649
650 fflush(rev->diffopt.file);
651 if (!rev->diffopt.flags.textconv_set_via_cmdline ||
652 !rev->diffopt.flags.allow_textconv)
653 return stream_blob_to_fd(1, oid, NULL, 0);
654
655 if (get_oid_with_context(the_repository, obj_name,
656 GET_OID_RECORD_PATH,
657 &oidc, &obj_context))
658 die(_("not a valid object name %s"), obj_name);
659 if (!obj_context.path ||
660 !textconv_object(the_repository, obj_context.path,
661 obj_context.mode, &oidc, 1, &buf, &size)) {
662 free(obj_context.path);
663 return stream_blob_to_fd(1, oid, NULL, 0);
664 }
665
666 if (!buf)
667 die(_("git show %s: bad file"), obj_name);
668
669 write_or_die(1, buf, size);
670 free(obj_context.path);
671 return 0;
672 }
673
674 static int show_tag_object(const struct object_id *oid, struct rev_info *rev)
675 {
676 unsigned long size;
677 enum object_type type;
678 char *buf = read_object_file(oid, &type, &size);
679 int offset = 0;
680
681 if (!buf)
682 return error(_("could not read object %s"), oid_to_hex(oid));
683
684 assert(type == OBJ_TAG);
685 while (offset < size && buf[offset] != '\n') {
686 int new_offset = offset + 1;
687 const char *ident;
688 while (new_offset < size && buf[new_offset++] != '\n')
689 ; /* do nothing */
690 if (skip_prefix(buf + offset, "tagger ", &ident))
691 show_tagger(ident, rev);
692 offset = new_offset;
693 }
694
695 if (offset < size)
696 fwrite(buf + offset, size - offset, 1, rev->diffopt.file);
697 free(buf);
698 return 0;
699 }
700
701 static int show_tree_object(const struct object_id *oid UNUSED,
702 struct strbuf *base UNUSED,
703 const char *pathname, unsigned mode,
704 void *context)
705 {
706 FILE *file = context;
707 fprintf(file, "%s%s\n", pathname, S_ISDIR(mode) ? "/" : "");
708 return 0;
709 }
710
711 static void show_setup_revisions_tweak(struct rev_info *rev,
712 struct setup_revision_opt *opt)
713 {
714 if (rev->first_parent_only)
715 diff_merges_default_to_first_parent(rev);
716 else
717 diff_merges_default_to_dense_combined(rev);
718 if (!rev->diffopt.output_format)
719 rev->diffopt.output_format = DIFF_FORMAT_PATCH;
720 }
721
722 int cmd_show(int argc, const char **argv, const char *prefix)
723 {
724 struct rev_info rev;
725 unsigned int i;
726 struct setup_revision_opt opt;
727 struct pathspec match_all;
728 int ret = 0;
729
730 init_log_defaults();
731 git_config(git_log_config, NULL);
732
733 if (the_repository->gitdir) {
734 prepare_repo_settings(the_repository);
735 the_repository->settings.command_requires_full_index = 0;
736 }
737
738 memset(&match_all, 0, sizeof(match_all));
739 repo_init_revisions(the_repository, &rev, prefix);
740 git_config(grep_config, &rev.grep_filter);
741
742 rev.diff = 1;
743 rev.always_show_header = 1;
744 rev.no_walk = 1;
745 rev.diffopt.stat_width = -1; /* Scale to real terminal size */
746
747 memset(&opt, 0, sizeof(opt));
748 opt.def = "HEAD";
749 opt.tweak = show_setup_revisions_tweak;
750 cmd_log_init(argc, argv, prefix, &rev, &opt);
751
752 if (!rev.no_walk)
753 return cmd_log_deinit(cmd_log_walk(&rev), &rev);
754
755 rev.diffopt.no_free = 1;
756 for (i = 0; i < rev.pending.nr && !ret; i++) {
757 struct object *o = rev.pending.objects[i].item;
758 const char *name = rev.pending.objects[i].name;
759 switch (o->type) {
760 case OBJ_BLOB:
761 ret = show_blob_object(&o->oid, &rev, name);
762 break;
763 case OBJ_TAG: {
764 struct tag *t = (struct tag *)o;
765 struct object_id *oid = get_tagged_oid(t);
766
767 if (rev.shown_one)
768 putchar('\n');
769 fprintf(rev.diffopt.file, "%stag %s%s\n",
770 diff_get_color_opt(&rev.diffopt, DIFF_COMMIT),
771 t->tag,
772 diff_get_color_opt(&rev.diffopt, DIFF_RESET));
773 ret = show_tag_object(&o->oid, &rev);
774 rev.shown_one = 1;
775 if (ret)
776 break;
777 o = parse_object(the_repository, oid);
778 if (!o)
779 ret = error(_("could not read object %s"),
780 oid_to_hex(oid));
781 rev.pending.objects[i].item = o;
782 i--;
783 break;
784 }
785 case OBJ_TREE:
786 if (rev.shown_one)
787 putchar('\n');
788 fprintf(rev.diffopt.file, "%stree %s%s\n\n",
789 diff_get_color_opt(&rev.diffopt, DIFF_COMMIT),
790 name,
791 diff_get_color_opt(&rev.diffopt, DIFF_RESET));
792 read_tree(the_repository, (struct tree *)o,
793 &match_all, show_tree_object,
794 rev.diffopt.file);
795 rev.shown_one = 1;
796 break;
797 case OBJ_COMMIT:
798 {
799 struct object_array old;
800 struct object_array blank = OBJECT_ARRAY_INIT;
801
802 memcpy(&old, &rev.pending, sizeof(old));
803 memcpy(&rev.pending, &blank, sizeof(rev.pending));
804
805 add_object_array(o, name, &rev.pending);
806 ret = cmd_log_walk_no_free(&rev);
807
808 /*
809 * No need for
810 * object_array_clear(&pending). It was
811 * cleared already in prepare_revision_walk()
812 */
813 memcpy(&rev.pending, &old, sizeof(rev.pending));
814 break;
815 }
816 default:
817 ret = error(_("unknown type: %d"), o->type);
818 }
819 }
820
821 rev.diffopt.no_free = 0;
822 diff_free(&rev.diffopt);
823
824 return cmd_log_deinit(ret, &rev);
825 }
826
827 /*
828 * This is equivalent to "git log -g --abbrev-commit --pretty=oneline"
829 */
830 int cmd_log_reflog(int argc, const char **argv, const char *prefix)
831 {
832 struct rev_info rev;
833 struct setup_revision_opt opt;
834
835 init_log_defaults();
836 git_config(git_log_config, NULL);
837
838 repo_init_revisions(the_repository, &rev, prefix);
839 init_reflog_walk(&rev.reflog_info);
840 git_config(grep_config, &rev.grep_filter);
841
842 rev.verbose_header = 1;
843 memset(&opt, 0, sizeof(opt));
844 opt.def = "HEAD";
845 cmd_log_init_defaults(&rev);
846 rev.abbrev_commit = 1;
847 rev.commit_format = CMIT_FMT_ONELINE;
848 rev.use_terminator = 1;
849 rev.always_show_header = 1;
850 cmd_log_init_finish(argc, argv, prefix, &rev, &opt);
851
852 return cmd_log_deinit(cmd_log_walk(&rev), &rev);
853 }
854
855 static void log_setup_revisions_tweak(struct rev_info *rev,
856 struct setup_revision_opt *opt)
857 {
858 if (rev->diffopt.flags.default_follow_renames &&
859 rev->prune_data.nr == 1)
860 rev->diffopt.flags.follow_renames = 1;
861
862 if (rev->first_parent_only)
863 diff_merges_default_to_first_parent(rev);
864 }
865
866 int cmd_log(int argc, const char **argv, const char *prefix)
867 {
868 struct rev_info rev;
869 struct setup_revision_opt opt;
870
871 init_log_defaults();
872 git_config(git_log_config, NULL);
873
874 repo_init_revisions(the_repository, &rev, prefix);
875 git_config(grep_config, &rev.grep_filter);
876
877 rev.always_show_header = 1;
878 memset(&opt, 0, sizeof(opt));
879 opt.def = "HEAD";
880 opt.revarg_opt = REVARG_COMMITTISH;
881 opt.tweak = log_setup_revisions_tweak;
882 cmd_log_init(argc, argv, prefix, &rev, &opt);
883 return cmd_log_deinit(cmd_log_walk(&rev), &rev);
884 }
885
886 /* format-patch */
887
888 static const char *fmt_patch_suffix = ".patch";
889 static int numbered = 0;
890 static int auto_number = 1;
891
892 static char *default_attach = NULL;
893
894 static struct string_list extra_hdr = STRING_LIST_INIT_NODUP;
895 static struct string_list extra_to = STRING_LIST_INIT_NODUP;
896 static struct string_list extra_cc = STRING_LIST_INIT_NODUP;
897
898 static void add_header(const char *value)
899 {
900 struct string_list_item *item;
901 int len = strlen(value);
902 while (len && value[len - 1] == '\n')
903 len--;
904
905 if (!strncasecmp(value, "to: ", 4)) {
906 item = string_list_append(&extra_to, value + 4);
907 len -= 4;
908 } else if (!strncasecmp(value, "cc: ", 4)) {
909 item = string_list_append(&extra_cc, value + 4);
910 len -= 4;
911 } else {
912 item = string_list_append(&extra_hdr, value);
913 }
914
915 item->string[len] = '\0';
916 }
917
918 enum cover_setting {
919 COVER_UNSET,
920 COVER_OFF,
921 COVER_ON,
922 COVER_AUTO
923 };
924
925 enum thread_level {
926 THREAD_UNSET,
927 THREAD_SHALLOW,
928 THREAD_DEEP
929 };
930
931 enum cover_from_description {
932 COVER_FROM_NONE,
933 COVER_FROM_MESSAGE,
934 COVER_FROM_SUBJECT,
935 COVER_FROM_AUTO
936 };
937
938 enum auto_base_setting {
939 AUTO_BASE_NEVER,
940 AUTO_BASE_ALWAYS,
941 AUTO_BASE_WHEN_ABLE
942 };
943
944 static enum thread_level thread;
945 static int do_signoff;
946 static enum auto_base_setting auto_base;
947 static char *from;
948 static const char *signature = git_version_string;
949 static const char *signature_file;
950 static enum cover_setting config_cover_letter;
951 static const char *config_output_directory;
952 static enum cover_from_description cover_from_description_mode = COVER_FROM_MESSAGE;
953 static int show_notes;
954 static struct display_notes_opt notes_opt;
955
956 static enum cover_from_description parse_cover_from_description(const char *arg)
957 {
958 if (!arg || !strcmp(arg, "default"))
959 return COVER_FROM_MESSAGE;
960 else if (!strcmp(arg, "none"))
961 return COVER_FROM_NONE;
962 else if (!strcmp(arg, "message"))
963 return COVER_FROM_MESSAGE;
964 else if (!strcmp(arg, "subject"))
965 return COVER_FROM_SUBJECT;
966 else if (!strcmp(arg, "auto"))
967 return COVER_FROM_AUTO;
968 else
969 die(_("%s: invalid cover from description mode"), arg);
970 }
971
972 static int git_format_config(const char *var, const char *value, void *cb)
973 {
974 if (!strcmp(var, "format.headers")) {
975 if (!value)
976 die(_("format.headers without value"));
977 add_header(value);
978 return 0;
979 }
980 if (!strcmp(var, "format.suffix"))
981 return git_config_string(&fmt_patch_suffix, var, value);
982 if (!strcmp(var, "format.to")) {
983 if (!value)
984 return config_error_nonbool(var);
985 string_list_append(&extra_to, value);
986 return 0;
987 }
988 if (!strcmp(var, "format.cc")) {
989 if (!value)
990 return config_error_nonbool(var);
991 string_list_append(&extra_cc, value);
992 return 0;
993 }
994 if (!strcmp(var, "diff.color") || !strcmp(var, "color.diff") ||
995 !strcmp(var, "color.ui") || !strcmp(var, "diff.submodule")) {
996 return 0;
997 }
998 if (!strcmp(var, "format.numbered")) {
999 if (value && !strcasecmp(value, "auto")) {
1000 auto_number = 1;
1001 return 0;
1002 }
1003 numbered = git_config_bool(var, value);
1004 auto_number = auto_number && numbered;
1005 return 0;
1006 }
1007 if (!strcmp(var, "format.attach")) {
1008 if (value && *value)
1009 default_attach = xstrdup(value);
1010 else if (value && !*value)
1011 FREE_AND_NULL(default_attach);
1012 else
1013 default_attach = xstrdup(git_version_string);
1014 return 0;
1015 }
1016 if (!strcmp(var, "format.thread")) {
1017 if (value && !strcasecmp(value, "deep")) {
1018 thread = THREAD_DEEP;
1019 return 0;
1020 }
1021 if (value && !strcasecmp(value, "shallow")) {
1022 thread = THREAD_SHALLOW;
1023 return 0;
1024 }
1025 thread = git_config_bool(var, value) ? THREAD_SHALLOW : THREAD_UNSET;
1026 return 0;
1027 }
1028 if (!strcmp(var, "format.signoff")) {
1029 do_signoff = git_config_bool(var, value);
1030 return 0;
1031 }
1032 if (!strcmp(var, "format.signature"))
1033 return git_config_string(&signature, var, value);
1034 if (!strcmp(var, "format.signaturefile"))
1035 return git_config_pathname(&signature_file, var, value);
1036 if (!strcmp(var, "format.coverletter")) {
1037 if (value && !strcasecmp(value, "auto")) {
1038 config_cover_letter = COVER_AUTO;
1039 return 0;
1040 }
1041 config_cover_letter = git_config_bool(var, value) ? COVER_ON : COVER_OFF;
1042 return 0;
1043 }
1044 if (!strcmp(var, "format.outputdirectory"))
1045 return git_config_string(&config_output_directory, var, value);
1046 if (!strcmp(var, "format.useautobase")) {
1047 if (value && !strcasecmp(value, "whenAble")) {
1048 auto_base = AUTO_BASE_WHEN_ABLE;
1049 return 0;
1050 }
1051 auto_base = git_config_bool(var, value) ? AUTO_BASE_ALWAYS : AUTO_BASE_NEVER;
1052 return 0;
1053 }
1054 if (!strcmp(var, "format.from")) {
1055 int b = git_parse_maybe_bool(value);
1056 free(from);
1057 if (b < 0)
1058 from = xstrdup(value);
1059 else if (b)
1060 from = xstrdup(git_committer_info(IDENT_NO_DATE));
1061 else
1062 from = NULL;
1063 return 0;
1064 }
1065 if (!strcmp(var, "format.forceinbodyfrom")) {
1066 force_in_body_from = git_config_bool(var, value);
1067 return 0;
1068 }
1069 if (!strcmp(var, "format.notes")) {
1070 int b = git_parse_maybe_bool(value);
1071 if (b < 0)
1072 enable_ref_display_notes(&notes_opt, &show_notes, value);
1073 else if (b)
1074 enable_default_display_notes(&notes_opt, &show_notes);
1075 else
1076 disable_display_notes(&notes_opt, &show_notes);
1077 return 0;
1078 }
1079 if (!strcmp(var, "format.coverfromdescription")) {
1080 cover_from_description_mode = parse_cover_from_description(value);
1081 return 0;
1082 }
1083 if (!strcmp(var, "format.mboxrd")) {
1084 stdout_mboxrd = git_config_bool(var, value);
1085 return 0;
1086 }
1087
1088 return git_log_config(var, value, cb);
1089 }
1090
1091 static const char *output_directory = NULL;
1092 static int outdir_offset;
1093
1094 static int open_next_file(struct commit *commit, const char *subject,
1095 struct rev_info *rev, int quiet)
1096 {
1097 struct strbuf filename = STRBUF_INIT;
1098
1099 if (output_directory) {
1100 strbuf_addstr(&filename, output_directory);
1101 strbuf_complete(&filename, '/');
1102 }
1103
1104 if (rev->numbered_files)
1105 strbuf_addf(&filename, "%d", rev->nr);
1106 else if (commit)
1107 fmt_output_commit(&filename, commit, rev);
1108 else
1109 fmt_output_subject(&filename, subject, rev);
1110
1111 if (!quiet)
1112 printf("%s\n", filename.buf + outdir_offset);
1113
1114 if (!(rev->diffopt.file = fopen(filename.buf, "w"))) {
1115 error_errno(_("cannot open patch file %s"), filename.buf);
1116 strbuf_release(&filename);
1117 return -1;
1118 }
1119
1120 strbuf_release(&filename);
1121 return 0;
1122 }
1123
1124 static void get_patch_ids(struct rev_info *rev, struct patch_ids *ids)
1125 {
1126 struct rev_info check_rev;
1127 struct commit *commit, *c1, *c2;
1128 struct object *o1, *o2;
1129 unsigned flags1, flags2;
1130
1131 if (rev->pending.nr != 2)
1132 die(_("need exactly one range"));
1133
1134 o1 = rev->pending.objects[0].item;
1135 o2 = rev->pending.objects[1].item;
1136 flags1 = o1->flags;
1137 flags2 = o2->flags;
1138 c1 = lookup_commit_reference(the_repository, &o1->oid);
1139 c2 = lookup_commit_reference(the_repository, &o2->oid);
1140
1141 if ((flags1 & UNINTERESTING) == (flags2 & UNINTERESTING))
1142 die(_("not a range"));
1143
1144 init_patch_ids(the_repository, ids);
1145
1146 /* given a range a..b get all patch ids for b..a */
1147 repo_init_revisions(the_repository, &check_rev, rev->prefix);
1148 check_rev.max_parents = 1;
1149 o1->flags ^= UNINTERESTING;
1150 o2->flags ^= UNINTERESTING;
1151 add_pending_object(&check_rev, o1, "o1");
1152 add_pending_object(&check_rev, o2, "o2");
1153 if (prepare_revision_walk(&check_rev))
1154 die(_("revision walk setup failed"));
1155
1156 while ((commit = get_revision(&check_rev)) != NULL) {
1157 add_commit_patch_id(commit, ids);
1158 }
1159
1160 /* reset for next revision walk */
1161 clear_commit_marks(c1, SEEN | UNINTERESTING | SHOWN | ADDED);
1162 clear_commit_marks(c2, SEEN | UNINTERESTING | SHOWN | ADDED);
1163 o1->flags = flags1;
1164 o2->flags = flags2;
1165 }
1166
1167 static void gen_message_id(struct rev_info *info, char *base)
1168 {
1169 struct strbuf buf = STRBUF_INIT;
1170 strbuf_addf(&buf, "%s.%"PRItime".git.%s", base,
1171 (timestamp_t) time(NULL),
1172 git_committer_info(IDENT_NO_NAME|IDENT_NO_DATE|IDENT_STRICT));
1173 info->message_id = strbuf_detach(&buf, NULL);
1174 }
1175
1176 static void print_signature(FILE *file)
1177 {
1178 if (!signature || !*signature)
1179 return;
1180
1181 fprintf(file, "-- \n%s", signature);
1182 if (signature[strlen(signature)-1] != '\n')
1183 putc('\n', file);
1184 putc('\n', file);
1185 }
1186
1187 static char *find_branch_name(struct rev_info *rev)
1188 {
1189 int i, positive = -1;
1190 struct object_id branch_oid;
1191 const struct object_id *tip_oid;
1192 const char *ref, *v;
1193 char *full_ref, *branch = NULL;
1194
1195 for (i = 0; i < rev->cmdline.nr; i++) {
1196 if (rev->cmdline.rev[i].flags & UNINTERESTING)
1197 continue;
1198 if (positive < 0)
1199 positive = i;
1200 else
1201 return NULL;
1202 }
1203 if (positive < 0)
1204 return NULL;
1205 ref = rev->cmdline.rev[positive].name;
1206 tip_oid = &rev->cmdline.rev[positive].item->oid;
1207 if (dwim_ref(ref, strlen(ref), &branch_oid, &full_ref, 0) &&
1208 skip_prefix(full_ref, "refs/heads/", &v) &&
1209 oideq(tip_oid, &branch_oid))
1210 branch = xstrdup(v);
1211 free(full_ref);
1212 return branch;
1213 }
1214
1215 static void show_diffstat(struct rev_info *rev,
1216 struct commit *origin, struct commit *head)
1217 {
1218 struct diff_options opts;
1219
1220 memcpy(&opts, &rev->diffopt, sizeof(opts));
1221 opts.output_format = DIFF_FORMAT_SUMMARY | DIFF_FORMAT_DIFFSTAT;
1222 diff_setup_done(&opts);
1223
1224 diff_tree_oid(get_commit_tree_oid(origin),
1225 get_commit_tree_oid(head),
1226 "", &opts);
1227 diffcore_std(&opts);
1228 diff_flush(&opts);
1229
1230 fprintf(rev->diffopt.file, "\n");
1231 }
1232
1233 static void prepare_cover_text(struct pretty_print_context *pp,
1234 const char *branch_name,
1235 struct strbuf *sb,
1236 const char *encoding,
1237 int need_8bit_cte)
1238 {
1239 const char *subject = "*** SUBJECT HERE ***";
1240 const char *body = "*** BLURB HERE ***";
1241 struct strbuf description_sb = STRBUF_INIT;
1242 struct strbuf subject_sb = STRBUF_INIT;
1243
1244 if (cover_from_description_mode == COVER_FROM_NONE)
1245 goto do_pp;
1246
1247 if (branch_name && *branch_name)
1248 read_branch_desc(&description_sb, branch_name);
1249 if (!description_sb.len)
1250 goto do_pp;
1251
1252 if (cover_from_description_mode == COVER_FROM_SUBJECT ||
1253 cover_from_description_mode == COVER_FROM_AUTO)
1254 body = format_subject(&subject_sb, description_sb.buf, " ");
1255
1256 if (cover_from_description_mode == COVER_FROM_MESSAGE ||
1257 (cover_from_description_mode == COVER_FROM_AUTO &&
1258 subject_sb.len > COVER_FROM_AUTO_MAX_SUBJECT_LEN))
1259 body = description_sb.buf;
1260 else
1261 subject = subject_sb.buf;
1262
1263 do_pp:
1264 pp_title_line(pp, &subject, sb, encoding, need_8bit_cte);
1265 pp_remainder(pp, &body, sb, 0);
1266
1267 strbuf_release(&description_sb);
1268 strbuf_release(&subject_sb);
1269 }
1270
1271 static int get_notes_refs(struct string_list_item *item, void *arg)
1272 {
1273 strvec_pushf(arg, "--notes=%s", item->string);
1274 return 0;
1275 }
1276
1277 static void get_notes_args(struct strvec *arg, struct rev_info *rev)
1278 {
1279 if (!rev->show_notes) {
1280 strvec_push(arg, "--no-notes");
1281 } else if (rev->notes_opt.use_default_notes > 0 ||
1282 (rev->notes_opt.use_default_notes == -1 &&
1283 !rev->notes_opt.extra_notes_refs.nr)) {
1284 strvec_push(arg, "--notes");
1285 } else {
1286 for_each_string_list(&rev->notes_opt.extra_notes_refs, get_notes_refs, arg);
1287 }
1288 }
1289
1290 static void make_cover_letter(struct rev_info *rev, int use_separate_file,
1291 struct commit *origin,
1292 int nr, struct commit **list,
1293 const char *branch_name,
1294 int quiet)
1295 {
1296 const char *committer;
1297 struct shortlog log;
1298 struct strbuf sb = STRBUF_INIT;
1299 int i;
1300 const char *encoding = "UTF-8";
1301 int need_8bit_cte = 0;
1302 struct pretty_print_context pp = {0};
1303 struct commit *head = list[0];
1304
1305 if (!cmit_fmt_is_mail(rev->commit_format))
1306 die(_("cover letter needs email format"));
1307
1308 committer = git_committer_info(0);
1309
1310 if (use_separate_file &&
1311 open_next_file(NULL, rev->numbered_files ? NULL : "cover-letter", rev, quiet))
1312 die(_("failed to create cover-letter file"));
1313
1314 log_write_email_headers(rev, head, &pp.after_subject, &need_8bit_cte, 0);
1315
1316 for (i = 0; !need_8bit_cte && i < nr; i++) {
1317 const char *buf = get_commit_buffer(list[i], NULL);
1318 if (has_non_ascii(buf))
1319 need_8bit_cte = 1;
1320 unuse_commit_buffer(list[i], buf);
1321 }
1322
1323 if (!branch_name)
1324 branch_name = find_branch_name(rev);
1325
1326 pp.fmt = CMIT_FMT_EMAIL;
1327 pp.date_mode.type = DATE_RFC2822;
1328 pp.rev = rev;
1329 pp.print_email_subject = 1;
1330 pp_user_info(&pp, NULL, &sb, committer, encoding);
1331 prepare_cover_text(&pp, branch_name, &sb, encoding, need_8bit_cte);
1332 fprintf(rev->diffopt.file, "%s\n", sb.buf);
1333
1334 strbuf_release(&sb);
1335
1336 shortlog_init(&log);
1337 log.wrap_lines = 1;
1338 log.wrap = MAIL_DEFAULT_WRAP;
1339 log.in1 = 2;
1340 log.in2 = 4;
1341 log.file = rev->diffopt.file;
1342 log.groups = SHORTLOG_GROUP_AUTHOR;
1343 shortlog_finish_setup(&log);
1344 for (i = 0; i < nr; i++)
1345 shortlog_add_commit(&log, list[i]);
1346
1347 shortlog_output(&log);
1348
1349 /* We can only do diffstat with a unique reference point */
1350 if (origin)
1351 show_diffstat(rev, origin, head);
1352
1353 if (rev->idiff_oid1) {
1354 fprintf_ln(rev->diffopt.file, "%s", rev->idiff_title);
1355 show_interdiff(rev->idiff_oid1, rev->idiff_oid2, 0,
1356 &rev->diffopt);
1357 }
1358
1359 if (rev->rdiff1) {
1360 /*
1361 * Pass minimum required diff-options to range-diff; others
1362 * can be added later if deemed desirable.
1363 */
1364 struct diff_options opts;
1365 struct strvec other_arg = STRVEC_INIT;
1366 struct range_diff_options range_diff_opts = {
1367 .creation_factor = rev->creation_factor,
1368 .dual_color = 1,
1369 .diffopt = &opts,
1370 .other_arg = &other_arg
1371 };
1372
1373 diff_setup(&opts);
1374 opts.file = rev->diffopt.file;
1375 opts.use_color = rev->diffopt.use_color;
1376 diff_setup_done(&opts);
1377 fprintf_ln(rev->diffopt.file, "%s", rev->rdiff_title);
1378 get_notes_args(&other_arg, rev);
1379 show_range_diff(rev->rdiff1, rev->rdiff2, &range_diff_opts);
1380 strvec_clear(&other_arg);
1381 }
1382 }
1383
1384 static const char *clean_message_id(const char *msg_id)
1385 {
1386 char ch;
1387 const char *a, *z, *m;
1388
1389 m = msg_id;
1390 while ((ch = *m) && (isspace(ch) || (ch == '<')))
1391 m++;
1392 a = m;
1393 z = NULL;
1394 while ((ch = *m)) {
1395 if (!isspace(ch) && (ch != '>'))
1396 z = m;
1397 m++;
1398 }
1399 if (!z)
1400 die(_("insane in-reply-to: %s"), msg_id);
1401 if (++z == m)
1402 return a;
1403 return xmemdupz(a, z - a);
1404 }
1405
1406 static const char *set_outdir(const char *prefix, const char *output_directory)
1407 {
1408 if (output_directory && is_absolute_path(output_directory))
1409 return output_directory;
1410
1411 if (!prefix || !*prefix) {
1412 if (output_directory)
1413 return output_directory;
1414 /* The user did not explicitly ask for "./" */
1415 outdir_offset = 2;
1416 return "./";
1417 }
1418
1419 outdir_offset = strlen(prefix);
1420 if (!output_directory)
1421 return prefix;
1422
1423 return prefix_filename(prefix, output_directory);
1424 }
1425
1426 static const char * const builtin_format_patch_usage[] = {
1427 N_("git format-patch [<options>] [<since> | <revision-range>]"),
1428 NULL
1429 };
1430
1431 static int keep_subject = 0;
1432
1433 static int keep_callback(const struct option *opt, const char *arg, int unset)
1434 {
1435 BUG_ON_OPT_NEG(unset);
1436 BUG_ON_OPT_ARG(arg);
1437 ((struct rev_info *)opt->value)->total = -1;
1438 keep_subject = 1;
1439 return 0;
1440 }
1441
1442 static int subject_prefix = 0;
1443
1444 static int subject_prefix_callback(const struct option *opt, const char *arg,
1445 int unset)
1446 {
1447 BUG_ON_OPT_NEG(unset);
1448 subject_prefix = 1;
1449 ((struct rev_info *)opt->value)->subject_prefix = arg;
1450 return 0;
1451 }
1452
1453 static int rfc_callback(const struct option *opt, const char *arg, int unset)
1454 {
1455 BUG_ON_OPT_NEG(unset);
1456 BUG_ON_OPT_ARG(arg);
1457 return subject_prefix_callback(opt, "RFC PATCH", unset);
1458 }
1459
1460 static int numbered_cmdline_opt = 0;
1461
1462 static int numbered_callback(const struct option *opt, const char *arg,
1463 int unset)
1464 {
1465 BUG_ON_OPT_ARG(arg);
1466 *(int *)opt->value = numbered_cmdline_opt = unset ? 0 : 1;
1467 if (unset)
1468 auto_number = 0;
1469 return 0;
1470 }
1471
1472 static int no_numbered_callback(const struct option *opt, const char *arg,
1473 int unset)
1474 {
1475 BUG_ON_OPT_NEG(unset);
1476 return numbered_callback(opt, arg, 1);
1477 }
1478
1479 static int output_directory_callback(const struct option *opt, const char *arg,
1480 int unset)
1481 {
1482 const char **dir = (const char **)opt->value;
1483 BUG_ON_OPT_NEG(unset);
1484 if (*dir)
1485 die(_("two output directories?"));
1486 *dir = arg;
1487 return 0;
1488 }
1489
1490 static int thread_callback(const struct option *opt, const char *arg, int unset)
1491 {
1492 enum thread_level *thread = (enum thread_level *)opt->value;
1493 if (unset)
1494 *thread = THREAD_UNSET;
1495 else if (!arg || !strcmp(arg, "shallow"))
1496 *thread = THREAD_SHALLOW;
1497 else if (!strcmp(arg, "deep"))
1498 *thread = THREAD_DEEP;
1499 /*
1500 * Please update _git_formatpatch() in git-completion.bash
1501 * when you add new options.
1502 */
1503 else
1504 return 1;
1505 return 0;
1506 }
1507
1508 static int attach_callback(const struct option *opt, const char *arg, int unset)
1509 {
1510 struct rev_info *rev = (struct rev_info *)opt->value;
1511 if (unset)
1512 rev->mime_boundary = NULL;
1513 else if (arg)
1514 rev->mime_boundary = arg;
1515 else
1516 rev->mime_boundary = git_version_string;
1517 rev->no_inline = unset ? 0 : 1;
1518 return 0;
1519 }
1520
1521 static int inline_callback(const struct option *opt, const char *arg, int unset)
1522 {
1523 struct rev_info *rev = (struct rev_info *)opt->value;
1524 if (unset)
1525 rev->mime_boundary = NULL;
1526 else if (arg)
1527 rev->mime_boundary = arg;
1528 else
1529 rev->mime_boundary = git_version_string;
1530 rev->no_inline = 0;
1531 return 0;
1532 }
1533
1534 static int header_callback(const struct option *opt, const char *arg, int unset)
1535 {
1536 if (unset) {
1537 string_list_clear(&extra_hdr, 0);
1538 string_list_clear(&extra_to, 0);
1539 string_list_clear(&extra_cc, 0);
1540 } else {
1541 add_header(arg);
1542 }
1543 return 0;
1544 }
1545
1546 static int to_callback(const struct option *opt, const char *arg, int unset)
1547 {
1548 if (unset)
1549 string_list_clear(&extra_to, 0);
1550 else
1551 string_list_append(&extra_to, arg);
1552 return 0;
1553 }
1554
1555 static int cc_callback(const struct option *opt, const char *arg, int unset)
1556 {
1557 if (unset)
1558 string_list_clear(&extra_cc, 0);
1559 else
1560 string_list_append(&extra_cc, arg);
1561 return 0;
1562 }
1563
1564 static int from_callback(const struct option *opt, const char *arg, int unset)
1565 {
1566 char **from = opt->value;
1567
1568 free(*from);
1569
1570 if (unset)
1571 *from = NULL;
1572 else if (arg)
1573 *from = xstrdup(arg);
1574 else
1575 *from = xstrdup(git_committer_info(IDENT_NO_DATE));
1576 return 0;
1577 }
1578
1579 static int base_callback(const struct option *opt, const char *arg, int unset)
1580 {
1581 const char **base_commit = opt->value;
1582
1583 if (unset) {
1584 auto_base = AUTO_BASE_NEVER;
1585 *base_commit = NULL;
1586 } else if (!strcmp(arg, "auto")) {
1587 auto_base = AUTO_BASE_ALWAYS;
1588 *base_commit = NULL;
1589 } else {
1590 auto_base = AUTO_BASE_NEVER;
1591 *base_commit = arg;
1592 }
1593 return 0;
1594 }
1595
1596 struct base_tree_info {
1597 struct object_id base_commit;
1598 int nr_patch_id, alloc_patch_id;
1599 struct object_id *patch_id;
1600 };
1601
1602 static struct commit *get_base_commit(const char *base_commit,
1603 struct commit **list,
1604 int total)
1605 {
1606 struct commit *base = NULL;
1607 struct commit **rev;
1608 int i = 0, rev_nr = 0, auto_select, die_on_failure;
1609
1610 switch (auto_base) {
1611 case AUTO_BASE_NEVER:
1612 if (base_commit) {
1613 auto_select = 0;
1614 die_on_failure = 1;
1615 } else {
1616 /* no base information is requested */
1617 return NULL;
1618 }
1619 break;
1620 case AUTO_BASE_ALWAYS:
1621 case AUTO_BASE_WHEN_ABLE:
1622 if (base_commit) {
1623 BUG("requested automatic base selection but a commit was provided");
1624 } else {
1625 auto_select = 1;
1626 die_on_failure = auto_base == AUTO_BASE_ALWAYS;
1627 }
1628 break;
1629 default:
1630 BUG("unexpected automatic base selection method");
1631 }
1632
1633 if (!auto_select) {
1634 base = lookup_commit_reference_by_name(base_commit);
1635 if (!base)
1636 die(_("unknown commit %s"), base_commit);
1637 } else {
1638 struct branch *curr_branch = branch_get(NULL);
1639 const char *upstream = branch_get_upstream(curr_branch, NULL);
1640 if (upstream) {
1641 struct commit_list *base_list;
1642 struct commit *commit;
1643 struct object_id oid;
1644
1645 if (get_oid(upstream, &oid)) {
1646 if (die_on_failure)
1647 die(_("failed to resolve '%s' as a valid ref"), upstream);
1648 else
1649 return NULL;
1650 }
1651 commit = lookup_commit_or_die(&oid, "upstream base");
1652 base_list = get_merge_bases_many(commit, total, list);
1653 /* There should be one and only one merge base. */
1654 if (!base_list || base_list->next) {
1655 if (die_on_failure) {
1656 die(_("could not find exact merge base"));
1657 } else {
1658 free_commit_list(base_list);
1659 return NULL;
1660 }
1661 }
1662 base = base_list->item;
1663 free_commit_list(base_list);
1664 } else {
1665 if (die_on_failure)
1666 die(_("failed to get upstream, if you want to record base commit automatically,\n"
1667 "please use git branch --set-upstream-to to track a remote branch.\n"
1668 "Or you could specify base commit by --base=<base-commit-id> manually"));
1669 else
1670 return NULL;
1671 }
1672 }
1673
1674 ALLOC_ARRAY(rev, total);
1675 for (i = 0; i < total; i++)
1676 rev[i] = list[i];
1677
1678 rev_nr = total;
1679 /*
1680 * Get merge base through pair-wise computations
1681 * and store it in rev[0].
1682 */
1683 while (rev_nr > 1) {
1684 for (i = 0; i < rev_nr / 2; i++) {
1685 struct commit_list *merge_base;
1686 merge_base = get_merge_bases(rev[2 * i], rev[2 * i + 1]);
1687 if (!merge_base || merge_base->next) {
1688 if (die_on_failure) {
1689 die(_("failed to find exact merge base"));
1690 } else {
1691 free(rev);
1692 return NULL;
1693 }
1694 }
1695
1696 rev[i] = merge_base->item;
1697 }
1698
1699 if (rev_nr % 2)
1700 rev[i] = rev[2 * i];
1701 rev_nr = DIV_ROUND_UP(rev_nr, 2);
1702 }
1703
1704 if (!in_merge_bases(base, rev[0])) {
1705 if (die_on_failure) {
1706 die(_("base commit should be the ancestor of revision list"));
1707 } else {
1708 free(rev);
1709 return NULL;
1710 }
1711 }
1712
1713 for (i = 0; i < total; i++) {
1714 if (base == list[i]) {
1715 if (die_on_failure) {
1716 die(_("base commit shouldn't be in revision list"));
1717 } else {
1718 free(rev);
1719 return NULL;
1720 }
1721 }
1722 }
1723
1724 free(rev);
1725 return base;
1726 }
1727
1728 define_commit_slab(commit_base, int);
1729
1730 static void prepare_bases(struct base_tree_info *bases,
1731 struct commit *base,
1732 struct commit **list,
1733 int total)
1734 {
1735 struct commit *commit;
1736 struct rev_info revs;
1737 struct diff_options diffopt;
1738 struct commit_base commit_base;
1739 int i;
1740
1741 if (!base)
1742 return;
1743
1744 init_commit_base(&commit_base);
1745 repo_diff_setup(the_repository, &diffopt);
1746 diffopt.flags.recursive = 1;
1747 diff_setup_done(&diffopt);
1748
1749 oidcpy(&bases->base_commit, &base->object.oid);
1750
1751 repo_init_revisions(the_repository, &revs, NULL);
1752 revs.max_parents = 1;
1753 revs.topo_order = 1;
1754 for (i = 0; i < total; i++) {
1755 list[i]->object.flags &= ~UNINTERESTING;
1756 add_pending_object(&revs, &list[i]->object, "rev_list");
1757 *commit_base_at(&commit_base, list[i]) = 1;
1758 }
1759 base->object.flags |= UNINTERESTING;
1760 add_pending_object(&revs, &base->object, "base");
1761
1762 if (prepare_revision_walk(&revs))
1763 die(_("revision walk setup failed"));
1764 /*
1765 * Traverse the commits list, get prerequisite patch ids
1766 * and stuff them in bases structure.
1767 */
1768 while ((commit = get_revision(&revs)) != NULL) {
1769 struct object_id oid;
1770 struct object_id *patch_id;
1771 if (*commit_base_at(&commit_base, commit))
1772 continue;
1773 if (commit_patch_id(commit, &diffopt, &oid, 0))
1774 die(_("cannot get patch id"));
1775 ALLOC_GROW(bases->patch_id, bases->nr_patch_id + 1, bases->alloc_patch_id);
1776 patch_id = bases->patch_id + bases->nr_patch_id;
1777 oidcpy(patch_id, &oid);
1778 bases->nr_patch_id++;
1779 }
1780 clear_commit_base(&commit_base);
1781 }
1782
1783 static void print_bases(struct base_tree_info *bases, FILE *file)
1784 {
1785 int i;
1786
1787 /* Only do this once, either for the cover or for the first one */
1788 if (is_null_oid(&bases->base_commit))
1789 return;
1790
1791 /* Show the base commit */
1792 fprintf(file, "\nbase-commit: %s\n", oid_to_hex(&bases->base_commit));
1793
1794 /* Show the prerequisite patches */
1795 for (i = bases->nr_patch_id - 1; i >= 0; i--)
1796 fprintf(file, "prerequisite-patch-id: %s\n", oid_to_hex(&bases->patch_id[i]));
1797
1798 free(bases->patch_id);
1799 bases->nr_patch_id = 0;
1800 bases->alloc_patch_id = 0;
1801 oidclr(&bases->base_commit);
1802 }
1803
1804 static const char *diff_title(struct strbuf *sb,
1805 const char *reroll_count,
1806 const char *generic,
1807 const char *rerolled)
1808 {
1809 int v;
1810
1811 /* RFC may be v0, so allow -v1 to diff against v0 */
1812 if (reroll_count && !strtol_i(reroll_count, 10, &v) &&
1813 v >= 1)
1814 strbuf_addf(sb, rerolled, v - 1);
1815 else
1816 strbuf_addstr(sb, generic);
1817 return sb->buf;
1818 }
1819
1820 static void infer_range_diff_ranges(struct strbuf *r1,
1821 struct strbuf *r2,
1822 const char *prev,
1823 struct commit *origin,
1824 struct commit *head)
1825 {
1826 const char *head_oid = oid_to_hex(&head->object.oid);
1827 int prev_is_range = is_range_diff_range(prev);
1828
1829 if (prev_is_range)
1830 strbuf_addstr(r1, prev);
1831 else
1832 strbuf_addf(r1, "%s..%s", head_oid, prev);
1833
1834 if (origin)
1835 strbuf_addf(r2, "%s..%s", oid_to_hex(&origin->object.oid), head_oid);
1836 else if (prev_is_range)
1837 die(_("failed to infer range-diff origin of current series"));
1838 else {
1839 warning(_("using '%s' as range-diff origin of current series"), prev);
1840 strbuf_addf(r2, "%s..%s", prev, head_oid);
1841 }
1842 }
1843
1844 int cmd_format_patch(int argc, const char **argv, const char *prefix)
1845 {
1846 struct commit *commit;
1847 struct commit **list = NULL;
1848 struct rev_info rev;
1849 char *to_free = NULL;
1850 struct setup_revision_opt s_r_opt;
1851 int nr = 0, total, i;
1852 int use_stdout = 0;
1853 int start_number = -1;
1854 int just_numbers = 0;
1855 int ignore_if_in_upstream = 0;
1856 int cover_letter = -1;
1857 int boundary_count = 0;
1858 int no_binary_diff = 0;
1859 int zero_commit = 0;
1860 struct commit *origin = NULL;
1861 const char *in_reply_to = NULL;
1862 struct patch_ids ids;
1863 struct strbuf buf = STRBUF_INIT;
1864 int use_patch_format = 0;
1865 int quiet = 0;
1866 const char *reroll_count = NULL;
1867 char *cover_from_description_arg = NULL;
1868 char *branch_name = NULL;
1869 char *base_commit = NULL;
1870 struct base_tree_info bases;
1871 struct commit *base;
1872 int show_progress = 0;
1873 struct progress *progress = NULL;
1874 struct oid_array idiff_prev = OID_ARRAY_INIT;
1875 struct strbuf idiff_title = STRBUF_INIT;
1876 const char *rdiff_prev = NULL;
1877 struct strbuf rdiff1 = STRBUF_INIT;
1878 struct strbuf rdiff2 = STRBUF_INIT;
1879 struct strbuf rdiff_title = STRBUF_INIT;
1880 struct strbuf sprefix = STRBUF_INIT;
1881 int creation_factor = -1;
1882
1883 const struct option builtin_format_patch_options[] = {
1884 OPT_CALLBACK_F('n', "numbered", &numbered, NULL,
1885 N_("use [PATCH n/m] even with a single patch"),
1886 PARSE_OPT_NOARG, numbered_callback),
1887 OPT_CALLBACK_F('N', "no-numbered", &numbered, NULL,
1888 N_("use [PATCH] even with multiple patches"),
1889 PARSE_OPT_NOARG | PARSE_OPT_NONEG, no_numbered_callback),
1890 OPT_BOOL('s', "signoff", &do_signoff, N_("add a Signed-off-by trailer")),
1891 OPT_BOOL(0, "stdout", &use_stdout,
1892 N_("print patches to standard out")),
1893 OPT_BOOL(0, "cover-letter", &cover_letter,
1894 N_("generate a cover letter")),
1895 OPT_BOOL(0, "numbered-files", &just_numbers,
1896 N_("use simple number sequence for output file names")),
1897 OPT_STRING(0, "suffix", &fmt_patch_suffix, N_("sfx"),
1898 N_("use <sfx> instead of '.patch'")),
1899 OPT_INTEGER(0, "start-number", &start_number,
1900 N_("start numbering patches at <n> instead of 1")),
1901 OPT_STRING('v', "reroll-count", &reroll_count, N_("reroll-count"),
1902 N_("mark the series as Nth re-roll")),
1903 OPT_INTEGER(0, "filename-max-length", &fmt_patch_name_max,
1904 N_("max length of output filename")),
1905 OPT_CALLBACK_F(0, "rfc", &rev, NULL,
1906 N_("use [RFC PATCH] instead of [PATCH]"),
1907 PARSE_OPT_NOARG | PARSE_OPT_NONEG, rfc_callback),
1908 OPT_STRING(0, "cover-from-description", &cover_from_description_arg,
1909 N_("cover-from-description-mode"),
1910 N_("generate parts of a cover letter based on a branch's description")),
1911 OPT_CALLBACK_F(0, "subject-prefix", &rev, N_("prefix"),
1912 N_("use [<prefix>] instead of [PATCH]"),
1913 PARSE_OPT_NONEG, subject_prefix_callback),
1914 OPT_CALLBACK_F('o', "output-directory", &output_directory,
1915 N_("dir"), N_("store resulting files in <dir>"),
1916 PARSE_OPT_NONEG, output_directory_callback),
1917 OPT_CALLBACK_F('k', "keep-subject", &rev, NULL,
1918 N_("don't strip/add [PATCH]"),
1919 PARSE_OPT_NOARG | PARSE_OPT_NONEG, keep_callback),
1920 OPT_BOOL(0, "no-binary", &no_binary_diff,
1921 N_("don't output binary diffs")),
1922 OPT_BOOL(0, "zero-commit", &zero_commit,
1923 N_("output all-zero hash in From header")),
1924 OPT_BOOL(0, "ignore-if-in-upstream", &ignore_if_in_upstream,
1925 N_("don't include a patch matching a commit upstream")),
1926 OPT_SET_INT_F('p', "no-stat", &use_patch_format,
1927 N_("show patch format instead of default (patch + stat)"),
1928 1, PARSE_OPT_NONEG),
1929 OPT_GROUP(N_("Messaging")),
1930 OPT_CALLBACK(0, "add-header", NULL, N_("header"),
1931 N_("add email header"), header_callback),
1932 OPT_CALLBACK(0, "to", NULL, N_("email"), N_("add To: header"), to_callback),
1933 OPT_CALLBACK(0, "cc", NULL, N_("email"), N_("add Cc: header"), cc_callback),
1934 OPT_CALLBACK_F(0, "from", &from, N_("ident"),
1935 N_("set From address to <ident> (or committer ident if absent)"),
1936 PARSE_OPT_OPTARG, from_callback),
1937 OPT_STRING(0, "in-reply-to", &in_reply_to, N_("message-id"),
1938 N_("make first mail a reply to <message-id>")),
1939 OPT_CALLBACK_F(0, "attach", &rev, N_("boundary"),
1940 N_("attach the patch"), PARSE_OPT_OPTARG,
1941 attach_callback),
1942 OPT_CALLBACK_F(0, "inline", &rev, N_("boundary"),
1943 N_("inline the patch"),
1944 PARSE_OPT_OPTARG | PARSE_OPT_NONEG,
1945 inline_callback),
1946 OPT_CALLBACK_F(0, "thread", &thread, N_("style"),
1947 N_("enable message threading, styles: shallow, deep"),
1948 PARSE_OPT_OPTARG, thread_callback),
1949 OPT_STRING(0, "signature", &signature, N_("signature"),
1950 N_("add a signature")),
1951 OPT_CALLBACK_F(0, "base", &base_commit, N_("base-commit"),
1952 N_("add prerequisite tree info to the patch series"),
1953 0, base_callback),
1954 OPT_FILENAME(0, "signature-file", &signature_file,
1955 N_("add a signature from a file")),
1956 OPT__QUIET(&quiet, N_("don't print the patch filenames")),
1957 OPT_BOOL(0, "progress", &show_progress,
1958 N_("show progress while generating patches")),
1959 OPT_CALLBACK(0, "interdiff", &idiff_prev, N_("rev"),
1960 N_("show changes against <rev> in cover letter or single patch"),
1961 parse_opt_object_name),
1962 OPT_STRING(0, "range-diff", &rdiff_prev, N_("refspec"),
1963 N_("show changes against <refspec> in cover letter or single patch")),
1964 OPT_INTEGER(0, "creation-factor", &creation_factor,
1965 N_("percentage by which creation is weighted")),
1966 OPT_BOOL(0, "force-in-body-from", &force_in_body_from,
1967 N_("show in-body From: even if identical to the e-mail header")),
1968 OPT_END()
1969 };
1970
1971 extra_hdr.strdup_strings = 1;
1972 extra_to.strdup_strings = 1;
1973 extra_cc.strdup_strings = 1;
1974
1975 init_log_defaults();
1976 init_display_notes(&notes_opt);
1977 git_config(git_format_config, NULL);
1978 repo_init_revisions(the_repository, &rev, prefix);
1979 git_config(grep_config, &rev.grep_filter);
1980
1981 rev.show_notes = show_notes;
1982 memcpy(&rev.notes_opt, &notes_opt, sizeof(notes_opt));
1983 rev.commit_format = CMIT_FMT_EMAIL;
1984 rev.encode_email_headers = default_encode_email_headers;
1985 rev.expand_tabs_in_log_default = 0;
1986 rev.verbose_header = 1;
1987 rev.diff = 1;
1988 rev.max_parents = 1;
1989 rev.diffopt.flags.recursive = 1;
1990 rev.diffopt.no_free = 1;
1991 rev.subject_prefix = fmt_patch_subject_prefix;
1992 memset(&s_r_opt, 0, sizeof(s_r_opt));
1993 s_r_opt.def = "HEAD";
1994 s_r_opt.revarg_opt = REVARG_COMMITTISH;
1995
1996 if (default_attach) {
1997 rev.mime_boundary = default_attach;
1998 rev.no_inline = 1;
1999 }
2000
2001 /*
2002 * Parse the arguments before setup_revisions(), or something
2003 * like "git format-patch -o a123 HEAD^.." may fail; a123 is
2004 * possibly a valid SHA1.
2005 */
2006 argc = parse_options(argc, argv, prefix, builtin_format_patch_options,
2007 builtin_format_patch_usage,
2008 PARSE_OPT_KEEP_ARGV0 | PARSE_OPT_KEEP_UNKNOWN_OPT |
2009 PARSE_OPT_KEEP_DASHDASH);
2010
2011 rev.force_in_body_from = force_in_body_from;
2012
2013 /* Make sure "0000-$sub.patch" gives non-negative length for $sub */
2014 if (fmt_patch_name_max <= strlen("0000-") + strlen(fmt_patch_suffix))
2015 fmt_patch_name_max = strlen("0000-") + strlen(fmt_patch_suffix);
2016
2017 if (cover_from_description_arg)
2018 cover_from_description_mode = parse_cover_from_description(cover_from_description_arg);
2019
2020 if (reroll_count) {
2021 strbuf_addf(&sprefix, "%s v%s",
2022 rev.subject_prefix, reroll_count);
2023 rev.reroll_count = reroll_count;
2024 rev.subject_prefix = sprefix.buf;
2025 }
2026
2027 for (i = 0; i < extra_hdr.nr; i++) {
2028 strbuf_addstr(&buf, extra_hdr.items[i].string);
2029 strbuf_addch(&buf, '\n');
2030 }
2031
2032 if (extra_to.nr)
2033 strbuf_addstr(&buf, "To: ");
2034 for (i = 0; i < extra_to.nr; i++) {
2035 if (i)
2036 strbuf_addstr(&buf, " ");
2037 strbuf_addstr(&buf, extra_to.items[i].string);
2038 if (i + 1 < extra_to.nr)
2039 strbuf_addch(&buf, ',');
2040 strbuf_addch(&buf, '\n');
2041 }
2042
2043 if (extra_cc.nr)
2044 strbuf_addstr(&buf, "Cc: ");
2045 for (i = 0; i < extra_cc.nr; i++) {
2046 if (i)
2047 strbuf_addstr(&buf, " ");
2048 strbuf_addstr(&buf, extra_cc.items[i].string);
2049 if (i + 1 < extra_cc.nr)
2050 strbuf_addch(&buf, ',');
2051 strbuf_addch(&buf, '\n');
2052 }
2053
2054 rev.extra_headers = to_free = strbuf_detach(&buf, NULL);
2055
2056 if (from) {
2057 if (split_ident_line(&rev.from_ident, from, strlen(from)))
2058 die(_("invalid ident line: %s"), from);
2059 }
2060
2061 if (start_number < 0)
2062 start_number = 1;
2063
2064 /*
2065 * If numbered is set solely due to format.numbered in config,
2066 * and it would conflict with --keep-subject (-k) from the
2067 * command line, reset "numbered".
2068 */
2069 if (numbered && keep_subject && !numbered_cmdline_opt)
2070 numbered = 0;
2071
2072 if (numbered && keep_subject)
2073 die(_("options '%s' and '%s' cannot be used together"), "-n", "-k");
2074 if (keep_subject && subject_prefix)
2075 die(_("options '%s' and '%s' cannot be used together"), "--subject-prefix/--rfc", "-k");
2076 rev.preserve_subject = keep_subject;
2077
2078 argc = setup_revisions(argc, argv, &rev, &s_r_opt);
2079 if (argc > 1)
2080 die(_("unrecognized argument: %s"), argv[1]);
2081
2082 if (rev.diffopt.output_format & DIFF_FORMAT_NAME)
2083 die(_("--name-only does not make sense"));
2084 if (rev.diffopt.output_format & DIFF_FORMAT_NAME_STATUS)
2085 die(_("--name-status does not make sense"));
2086 if (rev.diffopt.output_format & DIFF_FORMAT_CHECKDIFF)
2087 die(_("--check does not make sense"));
2088 if (rev.remerge_diff)
2089 die(_("--remerge-diff does not make sense"));
2090
2091 if (!use_patch_format &&
2092 (!rev.diffopt.output_format ||
2093 rev.diffopt.output_format == DIFF_FORMAT_PATCH))
2094 rev.diffopt.output_format = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_SUMMARY;
2095 if (!rev.diffopt.stat_width)
2096 rev.diffopt.stat_width = MAIL_DEFAULT_WRAP;
2097
2098 /* Always generate a patch */
2099 rev.diffopt.output_format |= DIFF_FORMAT_PATCH;
2100
2101 rev.zero_commit = zero_commit;
2102 rev.patch_name_max = fmt_patch_name_max;
2103
2104 if (!rev.diffopt.flags.text && !no_binary_diff)
2105 rev.diffopt.flags.binary = 1;
2106
2107 if (rev.show_notes)
2108 load_display_notes(&rev.notes_opt);
2109
2110 die_for_incompatible_opt3(use_stdout, "--stdout",
2111 rev.diffopt.close_file, "--output",
2112 !!output_directory, "--output-directory");
2113
2114 if (use_stdout && stdout_mboxrd)
2115 rev.commit_format = CMIT_FMT_MBOXRD;
2116
2117 if (use_stdout) {
2118 setup_pager();
2119 } else if (!rev.diffopt.close_file) {
2120 int saved;
2121
2122 if (!output_directory)
2123 output_directory = config_output_directory;
2124 output_directory = set_outdir(prefix, output_directory);
2125
2126 if (rev.diffopt.use_color != GIT_COLOR_ALWAYS)
2127 rev.diffopt.use_color = GIT_COLOR_NEVER;
2128 /*
2129 * We consider <outdir> as 'outside of gitdir', therefore avoid
2130 * applying adjust_shared_perm in s-c-l-d.
2131 */
2132 saved = get_shared_repository();
2133 set_shared_repository(0);
2134 switch (safe_create_leading_directories_const(output_directory)) {
2135 case SCLD_OK:
2136 case SCLD_EXISTS:
2137 break;
2138 default:
2139 die(_("could not create leading directories "
2140 "of '%s'"), output_directory);
2141 }
2142 set_shared_repository(saved);
2143 if (mkdir(output_directory, 0777) < 0 && errno != EEXIST)
2144 die_errno(_("could not create directory '%s'"),
2145 output_directory);
2146 }
2147
2148 if (rev.pending.nr == 1) {
2149 int check_head = 0;
2150
2151 if (rev.max_count < 0 && !rev.show_root_diff) {
2152 /*
2153 * This is traditional behaviour of "git format-patch
2154 * origin" that prepares what the origin side still
2155 * does not have.
2156 */
2157 rev.pending.objects[0].item->flags |= UNINTERESTING;
2158 add_head_to_pending(&rev);
2159 check_head = 1;
2160 }
2161 /*
2162 * Otherwise, it is "format-patch -22 HEAD", and/or
2163 * "format-patch --root HEAD". The user wants
2164 * get_revision() to do the usual traversal.
2165 */
2166
2167 if (!strcmp(rev.pending.objects[0].name, "HEAD"))
2168 check_head = 1;
2169
2170 if (check_head) {
2171 const char *ref, *v;
2172 ref = resolve_ref_unsafe("HEAD", RESOLVE_REF_READING,
2173 NULL, NULL);
2174 if (ref && skip_prefix(ref, "refs/heads/", &v))
2175 branch_name = xstrdup(v);
2176 else
2177 branch_name = xstrdup(""); /* no branch */
2178 }
2179 }
2180
2181 /*
2182 * We cannot move this anywhere earlier because we do want to
2183 * know if --root was given explicitly from the command line.
2184 */
2185 rev.show_root_diff = 1;
2186
2187 if (ignore_if_in_upstream) {
2188 /* Don't say anything if head and upstream are the same. */
2189 if (rev.pending.nr == 2) {
2190 struct object_array_entry *o = rev.pending.objects;
2191 if (oideq(&o[0].item->oid, &o[1].item->oid))
2192 goto done;
2193 }
2194 get_patch_ids(&rev, &ids);
2195 }
2196
2197 if (prepare_revision_walk(&rev))
2198 die(_("revision walk setup failed"));
2199 rev.boundary = 1;
2200 while ((commit = get_revision(&rev)) != NULL) {
2201 if (commit->object.flags & BOUNDARY) {
2202 boundary_count++;
2203 origin = (boundary_count == 1) ? commit : NULL;
2204 continue;
2205 }
2206
2207 if (ignore_if_in_upstream && has_commit_patch_id(commit, &ids))
2208 continue;
2209
2210 nr++;
2211 REALLOC_ARRAY(list, nr);
2212 list[nr - 1] = commit;
2213 }
2214 if (nr == 0)
2215 /* nothing to do */
2216 goto done;
2217 total = nr;
2218 if (cover_letter == -1) {
2219 if (config_cover_letter == COVER_AUTO)
2220 cover_letter = (total > 1);
2221 else
2222 cover_letter = (config_cover_letter == COVER_ON);
2223 }
2224 if (!keep_subject && auto_number && (total > 1 || cover_letter))
2225 numbered = 1;
2226 if (numbered)
2227 rev.total = total + start_number - 1;
2228
2229 if (idiff_prev.nr) {
2230 if (!cover_letter && total != 1)
2231 die(_("--interdiff requires --cover-letter or single patch"));
2232 rev.idiff_oid1 = &idiff_prev.oid[idiff_prev.nr - 1];
2233 rev.idiff_oid2 = get_commit_tree_oid(list[0]);
2234 rev.idiff_title = diff_title(&idiff_title, reroll_count,
2235 _("Interdiff:"),
2236 _("Interdiff against v%d:"));
2237 }
2238
2239 if (creation_factor < 0)
2240 creation_factor = RANGE_DIFF_CREATION_FACTOR_DEFAULT;
2241 else if (!rdiff_prev)
2242 die(_("the option '%s' requires '%s'"), "--creation-factor", "--range-diff");
2243
2244 if (rdiff_prev) {
2245 if (!cover_letter && total != 1)
2246 die(_("--range-diff requires --cover-letter or single patch"));
2247
2248 infer_range_diff_ranges(&rdiff1, &rdiff2, rdiff_prev,
2249 origin, list[0]);
2250 rev.rdiff1 = rdiff1.buf;
2251 rev.rdiff2 = rdiff2.buf;
2252 rev.creation_factor = creation_factor;
2253 rev.rdiff_title = diff_title(&rdiff_title, reroll_count,
2254 _("Range-diff:"),
2255 _("Range-diff against v%d:"));
2256 }
2257
2258 if (!signature) {
2259 ; /* --no-signature inhibits all signatures */
2260 } else if (signature && signature != git_version_string) {
2261 ; /* non-default signature already set */
2262 } else if (signature_file) {
2263 struct strbuf buf = STRBUF_INIT;
2264
2265 if (strbuf_read_file(&buf, signature_file, 128) < 0)
2266 die_errno(_("unable to read signature file '%s'"), signature_file);
2267 signature = strbuf_detach(&buf, NULL);
2268 }
2269
2270 memset(&bases, 0, sizeof(bases));
2271 base = get_base_commit(base_commit, list, nr);
2272 if (base) {
2273 reset_revision_walk();
2274 clear_object_flags(UNINTERESTING);
2275 prepare_bases(&bases, base, list, nr);
2276 }
2277
2278 if (in_reply_to || thread || cover_letter) {
2279 rev.ref_message_ids = xmalloc(sizeof(*rev.ref_message_ids));
2280 string_list_init_nodup(rev.ref_message_ids);
2281 }
2282 if (in_reply_to) {
2283 const char *msgid = clean_message_id(in_reply_to);
2284 string_list_append(rev.ref_message_ids, msgid);
2285 }
2286 rev.numbered_files = just_numbers;
2287 rev.patch_suffix = fmt_patch_suffix;
2288 if (cover_letter) {
2289 if (thread)
2290 gen_message_id(&rev, "cover");
2291 make_cover_letter(&rev, !!output_directory,
2292 origin, nr, list, branch_name, quiet);
2293 print_bases(&bases, rev.diffopt.file);
2294 print_signature(rev.diffopt.file);
2295 total++;
2296 start_number--;
2297 /* interdiff/range-diff in cover-letter; omit from patches */
2298 rev.idiff_oid1 = NULL;
2299 rev.rdiff1 = NULL;
2300 }
2301 rev.add_signoff = do_signoff;
2302
2303 if (show_progress)
2304 progress = start_delayed_progress(_("Generating patches"), total);
2305 while (0 <= --nr) {
2306 int shown;
2307 display_progress(progress, total - nr);
2308 commit = list[nr];
2309 rev.nr = total - nr + (start_number - 1);
2310 /* Make the second and subsequent mails replies to the first */
2311 if (thread) {
2312 /* Have we already had a message ID? */
2313 if (rev.message_id) {
2314 /*
2315 * For deep threading: make every mail
2316 * a reply to the previous one, no
2317 * matter what other options are set.
2318 *
2319 * For shallow threading:
2320 *
2321 * Without --cover-letter and
2322 * --in-reply-to, make every mail a
2323 * reply to the one before.
2324 *
2325 * With --in-reply-to but no
2326 * --cover-letter, make every mail a
2327 * reply to the <reply-to>.
2328 *
2329 * With --cover-letter, make every
2330 * mail but the cover letter a reply
2331 * to the cover letter. The cover
2332 * letter is a reply to the
2333 * --in-reply-to, if specified.
2334 */
2335 if (thread == THREAD_SHALLOW
2336 && rev.ref_message_ids->nr > 0
2337 && (!cover_letter || rev.nr > 1))
2338 free(rev.message_id);
2339 else
2340 string_list_append(rev.ref_message_ids,
2341 rev.message_id);
2342 }
2343 gen_message_id(&rev, oid_to_hex(&commit->object.oid));
2344 }
2345
2346 if (output_directory &&
2347 open_next_file(rev.numbered_files ? NULL : commit, NULL, &rev, quiet))
2348 die(_("failed to create output files"));
2349 shown = log_tree_commit(&rev, commit);
2350 free_commit_buffer(the_repository->parsed_objects,
2351 commit);
2352
2353 /* We put one extra blank line between formatted
2354 * patches and this flag is used by log-tree code
2355 * to see if it needs to emit a LF before showing
2356 * the log; when using one file per patch, we do
2357 * not want the extra blank line.
2358 */
2359 if (output_directory)
2360 rev.shown_one = 0;
2361 if (shown) {
2362 print_bases(&bases, rev.diffopt.file);
2363 if (rev.mime_boundary)
2364 fprintf(rev.diffopt.file, "\n--%s%s--\n\n\n",
2365 mime_boundary_leader,
2366 rev.mime_boundary);
2367 else
2368 print_signature(rev.diffopt.file);
2369 }
2370 if (output_directory)
2371 fclose(rev.diffopt.file);
2372 }
2373 stop_progress(&progress);
2374 free(list);
2375 free(branch_name);
2376 string_list_clear(&extra_to, 0);
2377 string_list_clear(&extra_cc, 0);
2378 string_list_clear(&extra_hdr, 0);
2379 if (ignore_if_in_upstream)
2380 free_patch_ids(&ids);
2381
2382 done:
2383 oid_array_clear(&idiff_prev);
2384 strbuf_release(&idiff_title);
2385 strbuf_release(&rdiff1);
2386 strbuf_release(&rdiff2);
2387 strbuf_release(&rdiff_title);
2388 strbuf_release(&sprefix);
2389 free(to_free);
2390 if (rev.ref_message_ids)
2391 string_list_clear(rev.ref_message_ids, 0);
2392 free(rev.ref_message_ids);
2393 return cmd_log_deinit(0, &rev);
2394 }
2395
2396 static int add_pending_commit(const char *arg, struct rev_info *revs, int flags)
2397 {
2398 struct object_id oid;
2399 if (get_oid(arg, &oid) == 0) {
2400 struct commit *commit = lookup_commit_reference(the_repository,
2401 &oid);
2402 if (commit) {
2403 commit->object.flags |= flags;
2404 add_pending_object(revs, &commit->object, arg);
2405 return 0;
2406 }
2407 }
2408 return -1;
2409 }
2410
2411 static const char * const cherry_usage[] = {
2412 N_("git cherry [-v] [<upstream> [<head> [<limit>]]]"),
2413 NULL
2414 };
2415
2416 static void print_commit(char sign, struct commit *commit, int verbose,
2417 int abbrev, FILE *file)
2418 {
2419 if (!verbose) {
2420 fprintf(file, "%c %s\n", sign,
2421 find_unique_abbrev(&commit->object.oid, abbrev));
2422 } else {
2423 struct strbuf buf = STRBUF_INIT;
2424 pp_commit_easy(CMIT_FMT_ONELINE, commit, &buf);
2425 fprintf(file, "%c %s %s\n", sign,
2426 find_unique_abbrev(&commit->object.oid, abbrev),
2427 buf.buf);
2428 strbuf_release(&buf);
2429 }
2430 }
2431
2432 int cmd_cherry(int argc, const char **argv, const char *prefix)
2433 {
2434 struct rev_info revs;
2435 struct patch_ids ids;
2436 struct commit *commit;
2437 struct commit_list *list = NULL;
2438 struct branch *current_branch;
2439 const char *upstream;
2440 const char *head = "HEAD";
2441 const char *limit = NULL;
2442 int verbose = 0, abbrev = 0;
2443
2444 struct option options[] = {
2445 OPT__ABBREV(&abbrev),
2446 OPT__VERBOSE(&verbose, N_("be verbose")),
2447 OPT_END()
2448 };
2449
2450 argc = parse_options(argc, argv, prefix, options, cherry_usage, 0);
2451
2452 switch (argc) {
2453 case 3:
2454 limit = argv[2];
2455 /* FALLTHROUGH */
2456 case 2:
2457 head = argv[1];
2458 /* FALLTHROUGH */
2459 case 1:
2460 upstream = argv[0];
2461 break;
2462 default:
2463 current_branch = branch_get(NULL);
2464 upstream = branch_get_upstream(current_branch, NULL);
2465 if (!upstream) {
2466 fprintf(stderr, _("Could not find a tracked"
2467 " remote branch, please"
2468 " specify <upstream> manually.\n"));
2469 usage_with_options(cherry_usage, options);
2470 }
2471 }
2472
2473 repo_init_revisions(the_repository, &revs, prefix);
2474 revs.max_parents = 1;
2475
2476 if (add_pending_commit(head, &revs, 0))
2477 die(_("unknown commit %s"), head);
2478 if (add_pending_commit(upstream, &revs, UNINTERESTING))
2479 die(_("unknown commit %s"), upstream);
2480
2481 /* Don't say anything if head and upstream are the same. */
2482 if (revs.pending.nr == 2) {
2483 struct object_array_entry *o = revs.pending.objects;
2484 if (oideq(&o[0].item->oid, &o[1].item->oid))
2485 return 0;
2486 }
2487
2488 get_patch_ids(&revs, &ids);
2489
2490 if (limit && add_pending_commit(limit, &revs, UNINTERESTING))
2491 die(_("unknown commit %s"), limit);
2492
2493 /* reverse the list of commits */
2494 if (prepare_revision_walk(&revs))
2495 die(_("revision walk setup failed"));
2496 while ((commit = get_revision(&revs)) != NULL) {
2497 commit_list_insert(commit, &list);
2498 }
2499
2500 while (list) {
2501 char sign = '+';
2502
2503 commit = list->item;
2504 if (has_commit_patch_id(commit, &ids))
2505 sign = '-';
2506 print_commit(sign, commit, verbose, abbrev, revs.diffopt.file);
2507 list = list->next;
2508 }
2509
2510 free_patch_ids(&ids);
2511 return 0;
2512 }