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