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