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