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