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