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