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