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