2 * Builtin "git log" and related commands (show, whatchanged)
4 * (C) Copyright 2006 Linus Torvalds
7 #define USE_THE_INDEX_COMPATIBILITY_MACROS
11 #include "object-store.h"
15 #include "diff-merges.h"
20 #include "reflog-walk.h"
21 #include "patch-ids.h"
22 #include "run-command.h"
25 #include "string-list.h"
26 #include "parse-options.h"
29 #include "streaming.h"
32 #include "gpg-interface.h"
34 #include "commit-slab.h"
35 #include "repository.h"
36 #include "commit-reach.h"
37 #include "range-diff.h"
38 #include "tmp-objdir.h"
40 #define MAIL_DEFAULT_WRAP 72
41 #define COVER_FROM_AUTO_MAX_SUBJECT_LEN 100
42 #define FORMAT_PATCH_NAME_MAX_DEFAULT 64
44 /* Set a default date-time format for git log ("log.date" config variable) */
45 static const char *default_date_mode
= NULL
;
47 static int default_abbrev_commit
;
48 static int default_show_root
= 1;
49 static int default_follow
;
50 static int default_show_signature
;
51 static int default_encode_email_headers
= 1;
52 static int decoration_style
;
53 static int decoration_given
;
54 static int use_mailmap_config
= 1;
55 static const char *fmt_patch_subject_prefix
= "PATCH";
56 static int fmt_patch_name_max
= FORMAT_PATCH_NAME_MAX_DEFAULT
;
57 static const char *fmt_pretty
;
59 static const char * const builtin_log_usage
[] = {
60 N_("git log [<options>] [<revision-range>] [[--] <path>...]"),
61 N_("git show [<options>] <object>..."),
65 struct line_opt_callback_data
{
68 struct string_list args
;
71 static int session_is_interactive(void)
73 return isatty(1) || pager_in_use();
76 static int auto_decoration_style(void)
78 return session_is_interactive() ? DECORATE_SHORT_REFS
: 0;
81 static int parse_decoration_style(const char *value
)
83 switch (git_parse_maybe_bool(value
)) {
85 return DECORATE_SHORT_REFS
;
91 if (!strcmp(value
, "full"))
92 return DECORATE_FULL_REFS
;
93 else if (!strcmp(value
, "short"))
94 return DECORATE_SHORT_REFS
;
95 else if (!strcmp(value
, "auto"))
96 return auto_decoration_style();
98 * Please update _git_log() in git-completion.bash when you
99 * add new decoration styles.
104 static int decorate_callback(const struct option
*opt
, const char *arg
, int unset
)
107 decoration_style
= 0;
109 decoration_style
= parse_decoration_style(arg
);
111 decoration_style
= DECORATE_SHORT_REFS
;
113 if (decoration_style
< 0)
114 die(_("invalid --decorate option: %s"), arg
);
116 decoration_given
= 1;
121 static int log_line_range_callback(const struct option
*option
, const char *arg
, int unset
)
123 struct line_opt_callback_data
*data
= option
->value
;
125 BUG_ON_OPT_NEG(unset
);
130 data
->rev
->line_level_traverse
= 1;
131 string_list_append(&data
->args
, arg
);
136 static void init_log_defaults(void)
138 init_diff_ui_defaults();
140 decoration_style
= auto_decoration_style();
143 static void cmd_log_init_defaults(struct rev_info
*rev
)
146 get_commit_format(fmt_pretty
, rev
);
148 rev
->diffopt
.flags
.default_follow_renames
= 1;
149 rev
->verbose_header
= 1;
150 rev
->diffopt
.flags
.recursive
= 1;
151 rev
->diffopt
.stat_width
= -1; /* use full terminal width */
152 rev
->diffopt
.stat_graph_width
= -1; /* respect statGraphWidth config */
153 rev
->abbrev_commit
= default_abbrev_commit
;
154 rev
->show_root_diff
= default_show_root
;
155 rev
->subject_prefix
= fmt_patch_subject_prefix
;
156 rev
->patch_name_max
= fmt_patch_name_max
;
157 rev
->show_signature
= default_show_signature
;
158 rev
->encode_email_headers
= default_encode_email_headers
;
159 rev
->diffopt
.flags
.allow_textconv
= 1;
161 if (default_date_mode
)
162 parse_date_format(default_date_mode
, &rev
->date_mode
);
165 static void cmd_log_init_finish(int argc
, const char **argv
, const char *prefix
,
166 struct rev_info
*rev
, struct setup_revision_opt
*opt
)
168 struct userformat_want w
;
169 int quiet
= 0, source
= 0, mailmap
;
170 static struct line_opt_callback_data line_cb
= {NULL
, NULL
, STRING_LIST_INIT_DUP
};
171 static struct string_list decorate_refs_exclude
= STRING_LIST_INIT_NODUP
;
172 static struct string_list decorate_refs_exclude_config
= STRING_LIST_INIT_NODUP
;
173 static struct string_list decorate_refs_include
= STRING_LIST_INIT_NODUP
;
174 struct decoration_filter decoration_filter
= {&decorate_refs_include
,
175 &decorate_refs_exclude
,
176 &decorate_refs_exclude_config
};
177 static struct revision_sources revision_sources
;
179 const struct option builtin_log_options
[] = {
180 OPT__QUIET(&quiet
, N_("suppress diff output")),
181 OPT_BOOL(0, "source", &source
, N_("show source")),
182 OPT_BOOL(0, "use-mailmap", &mailmap
, N_("use mail map file")),
183 OPT_ALIAS(0, "mailmap", "use-mailmap"),
184 OPT_STRING_LIST(0, "decorate-refs", &decorate_refs_include
,
185 N_("pattern"), N_("only decorate refs that match <pattern>")),
186 OPT_STRING_LIST(0, "decorate-refs-exclude", &decorate_refs_exclude
,
187 N_("pattern"), N_("do not decorate refs that match <pattern>")),
188 OPT_CALLBACK_F(0, "decorate", NULL
, NULL
, N_("decorate options"),
189 PARSE_OPT_OPTARG
, decorate_callback
),
190 OPT_CALLBACK('L', NULL
, &line_cb
, "range:file",
191 N_("trace the evolution of line range <start>,<end> or function :<funcname> in <file>"),
192 log_line_range_callback
),
197 line_cb
.prefix
= prefix
;
199 mailmap
= use_mailmap_config
;
200 argc
= parse_options(argc
, argv
, prefix
,
201 builtin_log_options
, builtin_log_usage
,
202 PARSE_OPT_KEEP_ARGV0
| PARSE_OPT_KEEP_UNKNOWN
|
203 PARSE_OPT_KEEP_DASHDASH
);
206 rev
->diffopt
.output_format
|= DIFF_FORMAT_NO_OUTPUT
;
207 argc
= setup_revisions(argc
, argv
, rev
, opt
);
209 /* Any arguments at this point are not recognized */
211 die(_("unrecognized argument: %s"), argv
[1]);
213 if (rev
->line_level_traverse
&& rev
->prune_data
.nr
)
214 die(_("-L<range>:<file> cannot be used with pathspec"));
216 memset(&w
, 0, sizeof(w
));
217 userformat_find_requirements(NULL
, &w
);
219 if (!rev
->show_notes_given
&& (!rev
->pretty_given
|| w
.notes
))
222 load_display_notes(&rev
->notes_opt
);
224 if ((rev
->diffopt
.pickaxe_opts
& DIFF_PICKAXE_KINDS_MASK
) ||
225 rev
->diffopt
.filter
|| rev
->diffopt
.flags
.follow_renames
)
226 rev
->always_show_header
= 0;
228 if (source
|| w
.source
) {
229 init_revision_sources(&revision_sources
);
230 rev
->sources
= &revision_sources
;
234 rev
->mailmap
= xmalloc(sizeof(struct string_list
));
235 string_list_init_nodup(rev
->mailmap
);
236 read_mailmap(rev
->mailmap
);
239 if (rev
->pretty_given
&& rev
->commit_format
== CMIT_FMT_RAW
) {
241 * "log --pretty=raw" is special; ignore UI oriented
242 * configuration variables such as decoration.
244 if (!decoration_given
)
245 decoration_style
= 0;
246 if (!rev
->abbrev_commit_given
)
247 rev
->abbrev_commit
= 0;
250 if (rev
->commit_format
== CMIT_FMT_USERFORMAT
) {
253 * Disable decoration loading if the format will not
256 decoration_style
= 0;
257 } else if (!decoration_style
) {
259 * If we are going to show them, make sure we do load
260 * them here, but taking care not to override a
261 * specific style set by config or --decorate.
263 decoration_style
= DECORATE_SHORT_REFS
;
267 if (decoration_style
|| rev
->simplify_by_decoration
) {
268 const struct string_list
*config_exclude
=
269 repo_config_get_value_multi(the_repository
,
270 "log.excludeDecoration");
272 if (config_exclude
) {
273 struct string_list_item
*item
;
274 for_each_string_list_item(item
, config_exclude
)
275 string_list_append(&decorate_refs_exclude_config
,
279 if (decoration_style
)
280 rev
->show_decorations
= 1;
282 load_ref_decorations(&decoration_filter
, decoration_style
);
285 if (rev
->line_level_traverse
)
286 line_log_init(rev
, line_cb
.prefix
, &line_cb
.args
);
291 static void cmd_log_init(int argc
, const char **argv
, const char *prefix
,
292 struct rev_info
*rev
, struct setup_revision_opt
*opt
)
294 cmd_log_init_defaults(rev
);
295 cmd_log_init_finish(argc
, argv
, prefix
, rev
, opt
);
298 static int cmd_log_deinit(int ret
, struct rev_info
*rev
)
300 release_revisions(rev
);
305 * This gives a rough estimate for how many commits we
306 * will print out in the list.
308 static int estimate_commit_count(struct commit_list
*list
)
313 struct commit
*commit
= list
->item
;
314 unsigned int flags
= commit
->object
.flags
;
316 if (!(flags
& (TREESAME
| UNINTERESTING
)))
322 static void show_early_header(struct rev_info
*rev
, const char *stage
, int nr
)
324 if (rev
->shown_one
) {
326 if (rev
->commit_format
!= CMIT_FMT_ONELINE
)
327 putchar(rev
->diffopt
.line_termination
);
329 fprintf(rev
->diffopt
.file
, _("Final output: %d %s\n"), nr
, stage
);
332 static struct itimerval early_output_timer
;
334 static void log_show_early(struct rev_info
*revs
, struct commit_list
*list
)
336 int i
= revs
->early_output
;
338 int no_free
= revs
->diffopt
.no_free
;
340 revs
->diffopt
.no_free
= 0;
341 sort_in_topological_order(&list
, revs
->sort_order
);
343 struct commit
*commit
= list
->item
;
344 switch (simplify_commit(revs
, commit
)) {
347 int n
= estimate_commit_count(list
);
348 show_early_header(revs
, "incomplete", n
);
351 log_tree_commit(revs
, commit
);
357 revs
->diffopt
.no_free
= no_free
;
358 diff_free(&revs
->diffopt
);
364 /* Did we already get enough commits for the early output? */
366 revs
->diffopt
.no_free
= 0;
367 diff_free(&revs
->diffopt
);
372 * ..if no, then repeat it twice a second until we
375 * NOTE! We don't use "it_interval", because if the
376 * reader isn't listening, we want our output to be
377 * throttled by the writing, and not have the timer
378 * trigger every second even if we're blocked on a
381 early_output_timer
.it_value
.tv_sec
= 0;
382 early_output_timer
.it_value
.tv_usec
= 500000;
383 setitimer(ITIMER_REAL
, &early_output_timer
, NULL
);
386 static void early_output(int signal
)
388 show_early_output
= log_show_early
;
391 static void setup_early_output(void)
396 * Set up the signal handler, minimally intrusively:
397 * we only set a single volatile integer word (not
398 * using sigatomic_t - trying to avoid unnecessary
399 * system dependencies and headers), and using
402 memset(&sa
, 0, sizeof(sa
));
403 sa
.sa_handler
= early_output
;
404 sigemptyset(&sa
.sa_mask
);
405 sa
.sa_flags
= SA_RESTART
;
406 sigaction(SIGALRM
, &sa
, NULL
);
409 * If we can get the whole output in less than a
410 * tenth of a second, don't even bother doing the
411 * early-output thing..
413 * This is a one-time-only trigger.
415 early_output_timer
.it_value
.tv_sec
= 0;
416 early_output_timer
.it_value
.tv_usec
= 100000;
417 setitimer(ITIMER_REAL
, &early_output_timer
, NULL
);
420 static void finish_early_output(struct rev_info
*rev
)
422 int n
= estimate_commit_count(rev
->commits
);
423 signal(SIGALRM
, SIG_IGN
);
424 show_early_header(rev
, "done", n
);
427 static int cmd_log_walk_no_free(struct rev_info
*rev
)
429 struct commit
*commit
;
433 if (rev
->remerge_diff
) {
434 rev
->remerge_objdir
= tmp_objdir_create("remerge-diff");
435 if (!rev
->remerge_objdir
)
436 die(_("unable to create temporary object directory"));
437 tmp_objdir_replace_primary_odb(rev
->remerge_objdir
, 1);
440 if (rev
->early_output
)
441 setup_early_output();
443 if (prepare_revision_walk(rev
))
444 die(_("revision walk setup failed"));
446 if (rev
->early_output
)
447 finish_early_output(rev
);
450 * For --check and --exit-code, the exit code is based on CHECK_FAILED
451 * and HAS_CHANGES being accumulated in rev->diffopt, so be careful to
452 * retain that state information if replacing rev->diffopt in this loop
454 while ((commit
= get_revision(rev
)) != NULL
) {
455 if (!log_tree_commit(rev
, commit
) && rev
->max_count
>= 0)
457 * We decremented max_count in get_revision,
458 * but we didn't actually show the commit.
461 if (!rev
->reflog_info
) {
463 * We may show a given commit multiple times when
464 * walking the reflogs.
466 free_commit_buffer(the_repository
->parsed_objects
,
468 free_commit_list(commit
->parents
);
469 commit
->parents
= NULL
;
471 if (saved_nrl
< rev
->diffopt
.needed_rename_limit
)
472 saved_nrl
= rev
->diffopt
.needed_rename_limit
;
473 if (rev
->diffopt
.degraded_cc_to_c
)
476 rev
->diffopt
.degraded_cc_to_c
= saved_dcctc
;
477 rev
->diffopt
.needed_rename_limit
= saved_nrl
;
479 if (rev
->remerge_diff
) {
480 tmp_objdir_destroy(rev
->remerge_objdir
);
481 rev
->remerge_objdir
= NULL
;
484 if (rev
->diffopt
.output_format
& DIFF_FORMAT_CHECKDIFF
&&
485 rev
->diffopt
.flags
.check_failed
) {
488 return diff_result_code(&rev
->diffopt
, 0);
491 static int cmd_log_walk(struct rev_info
*rev
)
495 rev
->diffopt
.no_free
= 1;
496 retval
= cmd_log_walk_no_free(rev
);
497 rev
->diffopt
.no_free
= 0;
498 diff_free(&rev
->diffopt
);
502 static int git_log_config(const char *var
, const char *value
, void *cb
)
504 const char *slot_name
;
506 if (!strcmp(var
, "format.pretty"))
507 return git_config_string(&fmt_pretty
, var
, value
);
508 if (!strcmp(var
, "format.subjectprefix"))
509 return git_config_string(&fmt_patch_subject_prefix
, var
, value
);
510 if (!strcmp(var
, "format.filenamemaxlength")) {
511 fmt_patch_name_max
= git_config_int(var
, value
);
514 if (!strcmp(var
, "format.encodeemailheaders")) {
515 default_encode_email_headers
= git_config_bool(var
, value
);
518 if (!strcmp(var
, "log.abbrevcommit")) {
519 default_abbrev_commit
= git_config_bool(var
, value
);
522 if (!strcmp(var
, "log.date"))
523 return git_config_string(&default_date_mode
, var
, value
);
524 if (!strcmp(var
, "log.decorate")) {
525 decoration_style
= parse_decoration_style(value
);
526 if (decoration_style
< 0)
527 decoration_style
= 0; /* maybe warn? */
530 if (!strcmp(var
, "log.diffmerges"))
531 return diff_merges_config(value
);
532 if (!strcmp(var
, "log.showroot")) {
533 default_show_root
= git_config_bool(var
, value
);
536 if (!strcmp(var
, "log.follow")) {
537 default_follow
= git_config_bool(var
, value
);
540 if (skip_prefix(var
, "color.decorate.", &slot_name
))
541 return parse_decorate_color_config(var
, slot_name
, value
);
542 if (!strcmp(var
, "log.mailmap")) {
543 use_mailmap_config
= git_config_bool(var
, value
);
546 if (!strcmp(var
, "log.showsignature")) {
547 default_show_signature
= git_config_bool(var
, value
);
551 if (git_gpg_config(var
, value
, cb
) < 0)
553 return git_diff_ui_config(var
, value
, cb
);
556 int cmd_whatchanged(int argc
, const char **argv
, const char *prefix
)
559 struct setup_revision_opt opt
;
562 git_config(git_log_config
, NULL
);
564 repo_init_revisions(the_repository
, &rev
, prefix
);
565 git_config(grep_config
, &rev
.grep_filter
);
568 rev
.simplify_history
= 0;
569 memset(&opt
, 0, sizeof(opt
));
571 opt
.revarg_opt
= REVARG_COMMITTISH
;
572 cmd_log_init(argc
, argv
, prefix
, &rev
, &opt
);
573 if (!rev
.diffopt
.output_format
)
574 rev
.diffopt
.output_format
= DIFF_FORMAT_RAW
;
575 return cmd_log_deinit(cmd_log_walk(&rev
), &rev
);
578 static void show_tagger(const char *buf
, struct rev_info
*rev
)
580 struct strbuf out
= STRBUF_INIT
;
581 struct pretty_print_context pp
= {0};
583 pp
.fmt
= rev
->commit_format
;
584 pp
.date_mode
= rev
->date_mode
;
585 pp_user_info(&pp
, "Tagger", &out
, buf
, get_log_output_encoding());
586 fprintf(rev
->diffopt
.file
, "%s", out
.buf
);
587 strbuf_release(&out
);
590 static int show_blob_object(const struct object_id
*oid
, struct rev_info
*rev
, const char *obj_name
)
592 struct object_id oidc
;
593 struct object_context obj_context
;
597 fflush(rev
->diffopt
.file
);
598 if (!rev
->diffopt
.flags
.textconv_set_via_cmdline
||
599 !rev
->diffopt
.flags
.allow_textconv
)
600 return stream_blob_to_fd(1, oid
, NULL
, 0);
602 if (get_oid_with_context(the_repository
, obj_name
,
604 &oidc
, &obj_context
))
605 die(_("not a valid object name %s"), obj_name
);
606 if (!obj_context
.path
||
607 !textconv_object(the_repository
, obj_context
.path
,
608 obj_context
.mode
, &oidc
, 1, &buf
, &size
)) {
609 free(obj_context
.path
);
610 return stream_blob_to_fd(1, oid
, NULL
, 0);
614 die(_("git show %s: bad file"), obj_name
);
616 write_or_die(1, buf
, size
);
617 free(obj_context
.path
);
621 static int show_tag_object(const struct object_id
*oid
, struct rev_info
*rev
)
624 enum object_type type
;
625 char *buf
= read_object_file(oid
, &type
, &size
);
629 return error(_("could not read object %s"), oid_to_hex(oid
));
631 assert(type
== OBJ_TAG
);
632 while (offset
< size
&& buf
[offset
] != '\n') {
633 int new_offset
= offset
+ 1;
635 while (new_offset
< size
&& buf
[new_offset
++] != '\n')
637 if (skip_prefix(buf
+ offset
, "tagger ", &ident
))
638 show_tagger(ident
, rev
);
643 fwrite(buf
+ offset
, size
- offset
, 1, rev
->diffopt
.file
);
648 static int show_tree_object(const struct object_id
*oid
,
650 const char *pathname
, unsigned mode
, void *context
)
652 FILE *file
= context
;
653 fprintf(file
, "%s%s\n", pathname
, S_ISDIR(mode
) ? "/" : "");
657 static void show_setup_revisions_tweak(struct rev_info
*rev
,
658 struct setup_revision_opt
*opt
)
660 if (rev
->first_parent_only
)
661 diff_merges_default_to_first_parent(rev
);
663 diff_merges_default_to_dense_combined(rev
);
664 if (!rev
->diffopt
.output_format
)
665 rev
->diffopt
.output_format
= DIFF_FORMAT_PATCH
;
668 int cmd_show(int argc
, const char **argv
, const char *prefix
)
671 struct object_array_entry
*objects
;
672 struct setup_revision_opt opt
;
673 struct pathspec match_all
;
674 int i
, count
, ret
= 0;
677 git_config(git_log_config
, NULL
);
679 if (the_repository
->gitdir
) {
680 prepare_repo_settings(the_repository
);
681 the_repository
->settings
.command_requires_full_index
= 0;
684 memset(&match_all
, 0, sizeof(match_all
));
685 repo_init_revisions(the_repository
, &rev
, prefix
);
686 git_config(grep_config
, &rev
.grep_filter
);
689 rev
.always_show_header
= 1;
691 rev
.diffopt
.stat_width
= -1; /* Scale to real terminal size */
693 memset(&opt
, 0, sizeof(opt
));
695 opt
.tweak
= show_setup_revisions_tweak
;
696 cmd_log_init(argc
, argv
, prefix
, &rev
, &opt
);
699 return cmd_log_deinit(cmd_log_walk(&rev
), &rev
);
701 count
= rev
.pending
.nr
;
702 objects
= rev
.pending
.objects
;
703 rev
.diffopt
.no_free
= 1;
704 for (i
= 0; i
< count
&& !ret
; i
++) {
705 struct object
*o
= objects
[i
].item
;
706 const char *name
= objects
[i
].name
;
709 ret
= show_blob_object(&o
->oid
, &rev
, name
);
712 struct tag
*t
= (struct tag
*)o
;
713 struct object_id
*oid
= get_tagged_oid(t
);
717 fprintf(rev
.diffopt
.file
, "%stag %s%s\n",
718 diff_get_color_opt(&rev
.diffopt
, DIFF_COMMIT
),
720 diff_get_color_opt(&rev
.diffopt
, DIFF_RESET
));
721 ret
= show_tag_object(&o
->oid
, &rev
);
725 o
= parse_object(the_repository
, oid
);
727 ret
= error(_("could not read object %s"),
736 fprintf(rev
.diffopt
.file
, "%stree %s%s\n\n",
737 diff_get_color_opt(&rev
.diffopt
, DIFF_COMMIT
),
739 diff_get_color_opt(&rev
.diffopt
, DIFF_RESET
));
740 read_tree(the_repository
, (struct tree
*)o
,
741 &match_all
, show_tree_object
,
746 rev
.pending
.nr
= rev
.pending
.alloc
= 0;
747 rev
.pending
.objects
= NULL
;
748 add_object_array(o
, name
, &rev
.pending
);
749 ret
= cmd_log_walk_no_free(&rev
);
752 ret
= error(_("unknown type: %d"), o
->type
);
756 rev
.diffopt
.no_free
= 0;
757 diff_free(&rev
.diffopt
);
759 return cmd_log_deinit(ret
, &rev
);
763 * This is equivalent to "git log -g --abbrev-commit --pretty=oneline"
765 int cmd_log_reflog(int argc
, const char **argv
, const char *prefix
)
768 struct setup_revision_opt opt
;
771 git_config(git_log_config
, NULL
);
773 repo_init_revisions(the_repository
, &rev
, prefix
);
774 init_reflog_walk(&rev
.reflog_info
);
775 git_config(grep_config
, &rev
.grep_filter
);
777 rev
.verbose_header
= 1;
778 memset(&opt
, 0, sizeof(opt
));
780 cmd_log_init_defaults(&rev
);
781 rev
.abbrev_commit
= 1;
782 rev
.commit_format
= CMIT_FMT_ONELINE
;
783 rev
.use_terminator
= 1;
784 rev
.always_show_header
= 1;
785 cmd_log_init_finish(argc
, argv
, prefix
, &rev
, &opt
);
787 return cmd_log_deinit(cmd_log_walk(&rev
), &rev
);
790 static void log_setup_revisions_tweak(struct rev_info
*rev
,
791 struct setup_revision_opt
*opt
)
793 if (rev
->diffopt
.flags
.default_follow_renames
&&
794 rev
->prune_data
.nr
== 1)
795 rev
->diffopt
.flags
.follow_renames
= 1;
797 if (rev
->first_parent_only
)
798 diff_merges_default_to_first_parent(rev
);
801 int cmd_log(int argc
, const char **argv
, const char *prefix
)
804 struct setup_revision_opt opt
;
807 git_config(git_log_config
, NULL
);
809 repo_init_revisions(the_repository
, &rev
, prefix
);
810 git_config(grep_config
, &rev
.grep_filter
);
812 rev
.always_show_header
= 1;
813 memset(&opt
, 0, sizeof(opt
));
815 opt
.revarg_opt
= REVARG_COMMITTISH
;
816 opt
.tweak
= log_setup_revisions_tweak
;
817 cmd_log_init(argc
, argv
, prefix
, &rev
, &opt
);
818 return cmd_log_deinit(cmd_log_walk(&rev
), &rev
);
823 static const char *fmt_patch_suffix
= ".patch";
824 static int numbered
= 0;
825 static int auto_number
= 1;
827 static char *default_attach
= NULL
;
829 static struct string_list extra_hdr
= STRING_LIST_INIT_NODUP
;
830 static struct string_list extra_to
= STRING_LIST_INIT_NODUP
;
831 static struct string_list extra_cc
= STRING_LIST_INIT_NODUP
;
833 static void add_header(const char *value
)
835 struct string_list_item
*item
;
836 int len
= strlen(value
);
837 while (len
&& value
[len
- 1] == '\n')
840 if (!strncasecmp(value
, "to: ", 4)) {
841 item
= string_list_append(&extra_to
, value
+ 4);
843 } else if (!strncasecmp(value
, "cc: ", 4)) {
844 item
= string_list_append(&extra_cc
, value
+ 4);
847 item
= string_list_append(&extra_hdr
, value
);
850 item
->string
[len
] = '\0';
866 enum cover_from_description
{
873 enum auto_base_setting
{
879 static enum thread_level thread
;
880 static int do_signoff
;
881 static enum auto_base_setting auto_base
;
883 static const char *signature
= git_version_string
;
884 static const char *signature_file
;
885 static enum cover_setting config_cover_letter
;
886 static const char *config_output_directory
;
887 static enum cover_from_description cover_from_description_mode
= COVER_FROM_MESSAGE
;
888 static int show_notes
;
889 static struct display_notes_opt notes_opt
;
891 static enum cover_from_description
parse_cover_from_description(const char *arg
)
893 if (!arg
|| !strcmp(arg
, "default"))
894 return COVER_FROM_MESSAGE
;
895 else if (!strcmp(arg
, "none"))
896 return COVER_FROM_NONE
;
897 else if (!strcmp(arg
, "message"))
898 return COVER_FROM_MESSAGE
;
899 else if (!strcmp(arg
, "subject"))
900 return COVER_FROM_SUBJECT
;
901 else if (!strcmp(arg
, "auto"))
902 return COVER_FROM_AUTO
;
904 die(_("%s: invalid cover from description mode"), arg
);
907 static int git_format_config(const char *var
, const char *value
, void *cb
)
909 if (!strcmp(var
, "format.headers")) {
911 die(_("format.headers without value"));
915 if (!strcmp(var
, "format.suffix"))
916 return git_config_string(&fmt_patch_suffix
, var
, value
);
917 if (!strcmp(var
, "format.to")) {
919 return config_error_nonbool(var
);
920 string_list_append(&extra_to
, value
);
923 if (!strcmp(var
, "format.cc")) {
925 return config_error_nonbool(var
);
926 string_list_append(&extra_cc
, value
);
929 if (!strcmp(var
, "diff.color") || !strcmp(var
, "color.diff") ||
930 !strcmp(var
, "color.ui") || !strcmp(var
, "diff.submodule")) {
933 if (!strcmp(var
, "format.numbered")) {
934 if (value
&& !strcasecmp(value
, "auto")) {
938 numbered
= git_config_bool(var
, value
);
939 auto_number
= auto_number
&& numbered
;
942 if (!strcmp(var
, "format.attach")) {
944 default_attach
= xstrdup(value
);
946 default_attach
= xstrdup(git_version_string
);
949 if (!strcmp(var
, "format.thread")) {
950 if (value
&& !strcasecmp(value
, "deep")) {
951 thread
= THREAD_DEEP
;
954 if (value
&& !strcasecmp(value
, "shallow")) {
955 thread
= THREAD_SHALLOW
;
958 thread
= git_config_bool(var
, value
) ? THREAD_SHALLOW
: THREAD_UNSET
;
961 if (!strcmp(var
, "format.signoff")) {
962 do_signoff
= git_config_bool(var
, value
);
965 if (!strcmp(var
, "format.signature"))
966 return git_config_string(&signature
, var
, value
);
967 if (!strcmp(var
, "format.signaturefile"))
968 return git_config_pathname(&signature_file
, var
, value
);
969 if (!strcmp(var
, "format.coverletter")) {
970 if (value
&& !strcasecmp(value
, "auto")) {
971 config_cover_letter
= COVER_AUTO
;
974 config_cover_letter
= git_config_bool(var
, value
) ? COVER_ON
: COVER_OFF
;
977 if (!strcmp(var
, "format.outputdirectory"))
978 return git_config_string(&config_output_directory
, var
, value
);
979 if (!strcmp(var
, "format.useautobase")) {
980 if (value
&& !strcasecmp(value
, "whenAble")) {
981 auto_base
= AUTO_BASE_WHEN_ABLE
;
984 auto_base
= git_config_bool(var
, value
) ? AUTO_BASE_ALWAYS
: AUTO_BASE_NEVER
;
987 if (!strcmp(var
, "format.from")) {
988 int b
= git_parse_maybe_bool(value
);
991 from
= xstrdup(value
);
993 from
= xstrdup(git_committer_info(IDENT_NO_DATE
));
998 if (!strcmp(var
, "format.notes")) {
999 int b
= git_parse_maybe_bool(value
);
1001 enable_ref_display_notes(¬es_opt
, &show_notes
, value
);
1003 enable_default_display_notes(¬es_opt
, &show_notes
);
1005 disable_display_notes(¬es_opt
, &show_notes
);
1008 if (!strcmp(var
, "format.coverfromdescription")) {
1009 cover_from_description_mode
= parse_cover_from_description(value
);
1013 return git_log_config(var
, value
, cb
);
1016 static const char *output_directory
= NULL
;
1017 static int outdir_offset
;
1019 static int open_next_file(struct commit
*commit
, const char *subject
,
1020 struct rev_info
*rev
, int quiet
)
1022 struct strbuf filename
= STRBUF_INIT
;
1024 if (output_directory
) {
1025 strbuf_addstr(&filename
, output_directory
);
1026 strbuf_complete(&filename
, '/');
1029 if (rev
->numbered_files
)
1030 strbuf_addf(&filename
, "%d", rev
->nr
);
1032 fmt_output_commit(&filename
, commit
, rev
);
1034 fmt_output_subject(&filename
, subject
, rev
);
1037 printf("%s\n", filename
.buf
+ outdir_offset
);
1039 if (!(rev
->diffopt
.file
= fopen(filename
.buf
, "w"))) {
1040 error_errno(_("cannot open patch file %s"), filename
.buf
);
1041 strbuf_release(&filename
);
1045 strbuf_release(&filename
);
1049 static void get_patch_ids(struct rev_info
*rev
, struct patch_ids
*ids
)
1051 struct rev_info check_rev
;
1052 struct commit
*commit
, *c1
, *c2
;
1053 struct object
*o1
, *o2
;
1054 unsigned flags1
, flags2
;
1056 if (rev
->pending
.nr
!= 2)
1057 die(_("need exactly one range"));
1059 o1
= rev
->pending
.objects
[0].item
;
1060 o2
= rev
->pending
.objects
[1].item
;
1063 c1
= lookup_commit_reference(the_repository
, &o1
->oid
);
1064 c2
= lookup_commit_reference(the_repository
, &o2
->oid
);
1066 if ((flags1
& UNINTERESTING
) == (flags2
& UNINTERESTING
))
1067 die(_("not a range"));
1069 init_patch_ids(the_repository
, ids
);
1071 /* given a range a..b get all patch ids for b..a */
1072 repo_init_revisions(the_repository
, &check_rev
, rev
->prefix
);
1073 check_rev
.max_parents
= 1;
1074 o1
->flags
^= UNINTERESTING
;
1075 o2
->flags
^= UNINTERESTING
;
1076 add_pending_object(&check_rev
, o1
, "o1");
1077 add_pending_object(&check_rev
, o2
, "o2");
1078 if (prepare_revision_walk(&check_rev
))
1079 die(_("revision walk setup failed"));
1081 while ((commit
= get_revision(&check_rev
)) != NULL
) {
1082 add_commit_patch_id(commit
, ids
);
1085 /* reset for next revision walk */
1086 clear_commit_marks(c1
, SEEN
| UNINTERESTING
| SHOWN
| ADDED
);
1087 clear_commit_marks(c2
, SEEN
| UNINTERESTING
| SHOWN
| ADDED
);
1092 static void gen_message_id(struct rev_info
*info
, char *base
)
1094 struct strbuf buf
= STRBUF_INIT
;
1095 strbuf_addf(&buf
, "%s.%"PRItime
".git.%s", base
,
1096 (timestamp_t
) time(NULL
),
1097 git_committer_info(IDENT_NO_NAME
|IDENT_NO_DATE
|IDENT_STRICT
));
1098 info
->message_id
= strbuf_detach(&buf
, NULL
);
1101 static void print_signature(FILE *file
)
1103 if (!signature
|| !*signature
)
1106 fprintf(file
, "-- \n%s", signature
);
1107 if (signature
[strlen(signature
)-1] != '\n')
1112 static char *find_branch_name(struct rev_info
*rev
)
1114 int i
, positive
= -1;
1115 struct object_id branch_oid
;
1116 const struct object_id
*tip_oid
;
1117 const char *ref
, *v
;
1118 char *full_ref
, *branch
= NULL
;
1120 for (i
= 0; i
< rev
->cmdline
.nr
; i
++) {
1121 if (rev
->cmdline
.rev
[i
].flags
& UNINTERESTING
)
1130 ref
= rev
->cmdline
.rev
[positive
].name
;
1131 tip_oid
= &rev
->cmdline
.rev
[positive
].item
->oid
;
1132 if (dwim_ref(ref
, strlen(ref
), &branch_oid
, &full_ref
, 0) &&
1133 skip_prefix(full_ref
, "refs/heads/", &v
) &&
1134 oideq(tip_oid
, &branch_oid
))
1135 branch
= xstrdup(v
);
1140 static void show_diffstat(struct rev_info
*rev
,
1141 struct commit
*origin
, struct commit
*head
)
1143 struct diff_options opts
;
1145 memcpy(&opts
, &rev
->diffopt
, sizeof(opts
));
1146 opts
.output_format
= DIFF_FORMAT_SUMMARY
| DIFF_FORMAT_DIFFSTAT
;
1147 diff_setup_done(&opts
);
1149 diff_tree_oid(get_commit_tree_oid(origin
),
1150 get_commit_tree_oid(head
),
1152 diffcore_std(&opts
);
1155 fprintf(rev
->diffopt
.file
, "\n");
1158 static void prepare_cover_text(struct pretty_print_context
*pp
,
1159 const char *branch_name
,
1161 const char *encoding
,
1164 const char *subject
= "*** SUBJECT HERE ***";
1165 const char *body
= "*** BLURB HERE ***";
1166 struct strbuf description_sb
= STRBUF_INIT
;
1167 struct strbuf subject_sb
= STRBUF_INIT
;
1169 if (cover_from_description_mode
== COVER_FROM_NONE
)
1172 if (branch_name
&& *branch_name
)
1173 read_branch_desc(&description_sb
, branch_name
);
1174 if (!description_sb
.len
)
1177 if (cover_from_description_mode
== COVER_FROM_SUBJECT
||
1178 cover_from_description_mode
== COVER_FROM_AUTO
)
1179 body
= format_subject(&subject_sb
, description_sb
.buf
, " ");
1181 if (cover_from_description_mode
== COVER_FROM_MESSAGE
||
1182 (cover_from_description_mode
== COVER_FROM_AUTO
&&
1183 subject_sb
.len
> COVER_FROM_AUTO_MAX_SUBJECT_LEN
))
1184 body
= description_sb
.buf
;
1186 subject
= subject_sb
.buf
;
1189 pp_title_line(pp
, &subject
, sb
, encoding
, need_8bit_cte
);
1190 pp_remainder(pp
, &body
, sb
, 0);
1192 strbuf_release(&description_sb
);
1193 strbuf_release(&subject_sb
);
1196 static int get_notes_refs(struct string_list_item
*item
, void *arg
)
1198 strvec_pushf(arg
, "--notes=%s", item
->string
);
1202 static void get_notes_args(struct strvec
*arg
, struct rev_info
*rev
)
1204 if (!rev
->show_notes
) {
1205 strvec_push(arg
, "--no-notes");
1206 } else if (rev
->notes_opt
.use_default_notes
> 0 ||
1207 (rev
->notes_opt
.use_default_notes
== -1 &&
1208 !rev
->notes_opt
.extra_notes_refs
.nr
)) {
1209 strvec_push(arg
, "--notes");
1211 for_each_string_list(&rev
->notes_opt
.extra_notes_refs
, get_notes_refs
, arg
);
1215 static void make_cover_letter(struct rev_info
*rev
, int use_separate_file
,
1216 struct commit
*origin
,
1217 int nr
, struct commit
**list
,
1218 const char *branch_name
,
1221 const char *committer
;
1222 struct shortlog log
;
1223 struct strbuf sb
= STRBUF_INIT
;
1225 const char *encoding
= "UTF-8";
1226 int need_8bit_cte
= 0;
1227 struct pretty_print_context pp
= {0};
1228 struct commit
*head
= list
[0];
1230 if (!cmit_fmt_is_mail(rev
->commit_format
))
1231 die(_("cover letter needs email format"));
1233 committer
= git_committer_info(0);
1235 if (use_separate_file
&&
1236 open_next_file(NULL
, rev
->numbered_files
? NULL
: "cover-letter", rev
, quiet
))
1237 die(_("failed to create cover-letter file"));
1239 log_write_email_headers(rev
, head
, &pp
.after_subject
, &need_8bit_cte
, 0);
1241 for (i
= 0; !need_8bit_cte
&& i
< nr
; i
++) {
1242 const char *buf
= get_commit_buffer(list
[i
], NULL
);
1243 if (has_non_ascii(buf
))
1245 unuse_commit_buffer(list
[i
], buf
);
1249 branch_name
= find_branch_name(rev
);
1251 pp
.fmt
= CMIT_FMT_EMAIL
;
1252 pp
.date_mode
.type
= DATE_RFC2822
;
1254 pp
.print_email_subject
= 1;
1255 pp_user_info(&pp
, NULL
, &sb
, committer
, encoding
);
1256 prepare_cover_text(&pp
, branch_name
, &sb
, encoding
, need_8bit_cte
);
1257 fprintf(rev
->diffopt
.file
, "%s\n", sb
.buf
);
1259 strbuf_release(&sb
);
1261 shortlog_init(&log
);
1263 log
.wrap
= MAIL_DEFAULT_WRAP
;
1266 log
.file
= rev
->diffopt
.file
;
1267 log
.groups
= SHORTLOG_GROUP_AUTHOR
;
1268 for (i
= 0; i
< nr
; i
++)
1269 shortlog_add_commit(&log
, list
[i
]);
1271 shortlog_output(&log
);
1273 /* We can only do diffstat with a unique reference point */
1275 show_diffstat(rev
, origin
, head
);
1277 if (rev
->idiff_oid1
) {
1278 fprintf_ln(rev
->diffopt
.file
, "%s", rev
->idiff_title
);
1279 show_interdiff(rev
->idiff_oid1
, rev
->idiff_oid2
, 0,
1285 * Pass minimum required diff-options to range-diff; others
1286 * can be added later if deemed desirable.
1288 struct diff_options opts
;
1289 struct strvec other_arg
= STRVEC_INIT
;
1290 struct range_diff_options range_diff_opts
= {
1291 .creation_factor
= rev
->creation_factor
,
1294 .other_arg
= &other_arg
1298 opts
.file
= rev
->diffopt
.file
;
1299 opts
.use_color
= rev
->diffopt
.use_color
;
1300 diff_setup_done(&opts
);
1301 fprintf_ln(rev
->diffopt
.file
, "%s", rev
->rdiff_title
);
1302 get_notes_args(&other_arg
, rev
);
1303 show_range_diff(rev
->rdiff1
, rev
->rdiff2
, &range_diff_opts
);
1304 strvec_clear(&other_arg
);
1308 static const char *clean_message_id(const char *msg_id
)
1311 const char *a
, *z
, *m
;
1314 while ((ch
= *m
) && (isspace(ch
) || (ch
== '<')))
1319 if (!isspace(ch
) && (ch
!= '>'))
1324 die(_("insane in-reply-to: %s"), msg_id
);
1327 return xmemdupz(a
, z
- a
);
1330 static const char *set_outdir(const char *prefix
, const char *output_directory
)
1332 if (output_directory
&& is_absolute_path(output_directory
))
1333 return output_directory
;
1335 if (!prefix
|| !*prefix
) {
1336 if (output_directory
)
1337 return output_directory
;
1338 /* The user did not explicitly ask for "./" */
1343 outdir_offset
= strlen(prefix
);
1344 if (!output_directory
)
1347 return prefix_filename(prefix
, output_directory
);
1350 static const char * const builtin_format_patch_usage
[] = {
1351 N_("git format-patch [<options>] [<since> | <revision-range>]"),
1355 static int keep_subject
= 0;
1357 static int keep_callback(const struct option
*opt
, const char *arg
, int unset
)
1359 BUG_ON_OPT_NEG(unset
);
1360 BUG_ON_OPT_ARG(arg
);
1361 ((struct rev_info
*)opt
->value
)->total
= -1;
1366 static int subject_prefix
= 0;
1368 static int subject_prefix_callback(const struct option
*opt
, const char *arg
,
1371 BUG_ON_OPT_NEG(unset
);
1373 ((struct rev_info
*)opt
->value
)->subject_prefix
= arg
;
1377 static int rfc_callback(const struct option
*opt
, const char *arg
, int unset
)
1379 BUG_ON_OPT_NEG(unset
);
1380 BUG_ON_OPT_ARG(arg
);
1381 return subject_prefix_callback(opt
, "RFC PATCH", unset
);
1384 static int numbered_cmdline_opt
= 0;
1386 static int numbered_callback(const struct option
*opt
, const char *arg
,
1389 BUG_ON_OPT_ARG(arg
);
1390 *(int *)opt
->value
= numbered_cmdline_opt
= unset
? 0 : 1;
1396 static int no_numbered_callback(const struct option
*opt
, const char *arg
,
1399 BUG_ON_OPT_NEG(unset
);
1400 return numbered_callback(opt
, arg
, 1);
1403 static int output_directory_callback(const struct option
*opt
, const char *arg
,
1406 const char **dir
= (const char **)opt
->value
;
1407 BUG_ON_OPT_NEG(unset
);
1409 die(_("two output directories?"));
1414 static int thread_callback(const struct option
*opt
, const char *arg
, int unset
)
1416 enum thread_level
*thread
= (enum thread_level
*)opt
->value
;
1418 *thread
= THREAD_UNSET
;
1419 else if (!arg
|| !strcmp(arg
, "shallow"))
1420 *thread
= THREAD_SHALLOW
;
1421 else if (!strcmp(arg
, "deep"))
1422 *thread
= THREAD_DEEP
;
1424 * Please update _git_formatpatch() in git-completion.bash
1425 * when you add new options.
1432 static int attach_callback(const struct option
*opt
, const char *arg
, int unset
)
1434 struct rev_info
*rev
= (struct rev_info
*)opt
->value
;
1436 rev
->mime_boundary
= NULL
;
1438 rev
->mime_boundary
= arg
;
1440 rev
->mime_boundary
= git_version_string
;
1441 rev
->no_inline
= unset
? 0 : 1;
1445 static int inline_callback(const struct option
*opt
, const char *arg
, int unset
)
1447 struct rev_info
*rev
= (struct rev_info
*)opt
->value
;
1449 rev
->mime_boundary
= NULL
;
1451 rev
->mime_boundary
= arg
;
1453 rev
->mime_boundary
= git_version_string
;
1458 static int header_callback(const struct option
*opt
, const char *arg
, int unset
)
1461 string_list_clear(&extra_hdr
, 0);
1462 string_list_clear(&extra_to
, 0);
1463 string_list_clear(&extra_cc
, 0);
1470 static int to_callback(const struct option
*opt
, const char *arg
, int unset
)
1473 string_list_clear(&extra_to
, 0);
1475 string_list_append(&extra_to
, arg
);
1479 static int cc_callback(const struct option
*opt
, const char *arg
, int unset
)
1482 string_list_clear(&extra_cc
, 0);
1484 string_list_append(&extra_cc
, arg
);
1488 static int from_callback(const struct option
*opt
, const char *arg
, int unset
)
1490 char **from
= opt
->value
;
1497 *from
= xstrdup(arg
);
1499 *from
= xstrdup(git_committer_info(IDENT_NO_DATE
));
1503 static int base_callback(const struct option
*opt
, const char *arg
, int unset
)
1505 const char **base_commit
= opt
->value
;
1508 auto_base
= AUTO_BASE_NEVER
;
1509 *base_commit
= NULL
;
1510 } else if (!strcmp(arg
, "auto")) {
1511 auto_base
= AUTO_BASE_ALWAYS
;
1512 *base_commit
= NULL
;
1514 auto_base
= AUTO_BASE_NEVER
;
1520 struct base_tree_info
{
1521 struct object_id base_commit
;
1522 int nr_patch_id
, alloc_patch_id
;
1523 struct object_id
*patch_id
;
1526 static struct commit
*get_base_commit(const char *base_commit
,
1527 struct commit
**list
,
1530 struct commit
*base
= NULL
;
1531 struct commit
**rev
;
1532 int i
= 0, rev_nr
= 0, auto_select
, die_on_failure
;
1534 switch (auto_base
) {
1535 case AUTO_BASE_NEVER
:
1540 /* no base information is requested */
1544 case AUTO_BASE_ALWAYS
:
1545 case AUTO_BASE_WHEN_ABLE
:
1547 BUG("requested automatic base selection but a commit was provided");
1550 die_on_failure
= auto_base
== AUTO_BASE_ALWAYS
;
1554 BUG("unexpected automatic base selection method");
1558 base
= lookup_commit_reference_by_name(base_commit
);
1560 die(_("unknown commit %s"), base_commit
);
1562 struct branch
*curr_branch
= branch_get(NULL
);
1563 const char *upstream
= branch_get_upstream(curr_branch
, NULL
);
1565 struct commit_list
*base_list
;
1566 struct commit
*commit
;
1567 struct object_id oid
;
1569 if (get_oid(upstream
, &oid
)) {
1571 die(_("failed to resolve '%s' as a valid ref"), upstream
);
1575 commit
= lookup_commit_or_die(&oid
, "upstream base");
1576 base_list
= get_merge_bases_many(commit
, total
, list
);
1577 /* There should be one and only one merge base. */
1578 if (!base_list
|| base_list
->next
) {
1579 if (die_on_failure
) {
1580 die(_("could not find exact merge base"));
1582 free_commit_list(base_list
);
1586 base
= base_list
->item
;
1587 free_commit_list(base_list
);
1590 die(_("failed to get upstream, if you want to record base commit automatically,\n"
1591 "please use git branch --set-upstream-to to track a remote branch.\n"
1592 "Or you could specify base commit by --base=<base-commit-id> manually"));
1598 ALLOC_ARRAY(rev
, total
);
1599 for (i
= 0; i
< total
; i
++)
1604 * Get merge base through pair-wise computations
1605 * and store it in rev[0].
1607 while (rev_nr
> 1) {
1608 for (i
= 0; i
< rev_nr
/ 2; i
++) {
1609 struct commit_list
*merge_base
;
1610 merge_base
= get_merge_bases(rev
[2 * i
], rev
[2 * i
+ 1]);
1611 if (!merge_base
|| merge_base
->next
) {
1612 if (die_on_failure
) {
1613 die(_("failed to find exact merge base"));
1620 rev
[i
] = merge_base
->item
;
1624 rev
[i
] = rev
[2 * i
];
1625 rev_nr
= DIV_ROUND_UP(rev_nr
, 2);
1628 if (!in_merge_bases(base
, rev
[0])) {
1629 if (die_on_failure
) {
1630 die(_("base commit should be the ancestor of revision list"));
1637 for (i
= 0; i
< total
; i
++) {
1638 if (base
== list
[i
]) {
1639 if (die_on_failure
) {
1640 die(_("base commit shouldn't be in revision list"));
1652 define_commit_slab(commit_base
, int);
1654 static void prepare_bases(struct base_tree_info
*bases
,
1655 struct commit
*base
,
1656 struct commit
**list
,
1659 struct commit
*commit
;
1660 struct rev_info revs
;
1661 struct diff_options diffopt
;
1662 struct commit_base commit_base
;
1668 init_commit_base(&commit_base
);
1669 repo_diff_setup(the_repository
, &diffopt
);
1670 diffopt
.flags
.recursive
= 1;
1671 diff_setup_done(&diffopt
);
1673 oidcpy(&bases
->base_commit
, &base
->object
.oid
);
1675 repo_init_revisions(the_repository
, &revs
, NULL
);
1676 revs
.max_parents
= 1;
1677 revs
.topo_order
= 1;
1678 for (i
= 0; i
< total
; i
++) {
1679 list
[i
]->object
.flags
&= ~UNINTERESTING
;
1680 add_pending_object(&revs
, &list
[i
]->object
, "rev_list");
1681 *commit_base_at(&commit_base
, list
[i
]) = 1;
1683 base
->object
.flags
|= UNINTERESTING
;
1684 add_pending_object(&revs
, &base
->object
, "base");
1686 if (prepare_revision_walk(&revs
))
1687 die(_("revision walk setup failed"));
1689 * Traverse the commits list, get prerequisite patch ids
1690 * and stuff them in bases structure.
1692 while ((commit
= get_revision(&revs
)) != NULL
) {
1693 struct object_id oid
;
1694 struct object_id
*patch_id
;
1695 if (*commit_base_at(&commit_base
, commit
))
1697 if (commit_patch_id(commit
, &diffopt
, &oid
, 0, 1))
1698 die(_("cannot get patch id"));
1699 ALLOC_GROW(bases
->patch_id
, bases
->nr_patch_id
+ 1, bases
->alloc_patch_id
);
1700 patch_id
= bases
->patch_id
+ bases
->nr_patch_id
;
1701 oidcpy(patch_id
, &oid
);
1702 bases
->nr_patch_id
++;
1704 clear_commit_base(&commit_base
);
1707 static void print_bases(struct base_tree_info
*bases
, FILE *file
)
1711 /* Only do this once, either for the cover or for the first one */
1712 if (is_null_oid(&bases
->base_commit
))
1715 /* Show the base commit */
1716 fprintf(file
, "\nbase-commit: %s\n", oid_to_hex(&bases
->base_commit
));
1718 /* Show the prerequisite patches */
1719 for (i
= bases
->nr_patch_id
- 1; i
>= 0; i
--)
1720 fprintf(file
, "prerequisite-patch-id: %s\n", oid_to_hex(&bases
->patch_id
[i
]));
1722 free(bases
->patch_id
);
1723 bases
->nr_patch_id
= 0;
1724 bases
->alloc_patch_id
= 0;
1725 oidclr(&bases
->base_commit
);
1728 static const char *diff_title(struct strbuf
*sb
,
1729 const char *reroll_count
,
1730 const char *generic
,
1731 const char *rerolled
)
1735 /* RFC may be v0, so allow -v1 to diff against v0 */
1736 if (reroll_count
&& !strtol_i(reroll_count
, 10, &v
) &&
1738 strbuf_addf(sb
, rerolled
, v
- 1);
1740 strbuf_addstr(sb
, generic
);
1744 static void infer_range_diff_ranges(struct strbuf
*r1
,
1747 struct commit
*origin
,
1748 struct commit
*head
)
1750 const char *head_oid
= oid_to_hex(&head
->object
.oid
);
1751 int prev_is_range
= is_range_diff_range(prev
);
1754 strbuf_addstr(r1
, prev
);
1756 strbuf_addf(r1
, "%s..%s", head_oid
, prev
);
1759 strbuf_addf(r2
, "%s..%s", oid_to_hex(&origin
->object
.oid
), head_oid
);
1760 else if (prev_is_range
)
1761 die(_("failed to infer range-diff origin of current series"));
1763 warning(_("using '%s' as range-diff origin of current series"), prev
);
1764 strbuf_addf(r2
, "%s..%s", prev
, head_oid
);
1768 int cmd_format_patch(int argc
, const char **argv
, const char *prefix
)
1770 struct commit
*commit
;
1771 struct commit
**list
= NULL
;
1772 struct rev_info rev
;
1773 char *to_free
= NULL
;
1774 struct setup_revision_opt s_r_opt
;
1775 int nr
= 0, total
, i
;
1777 int start_number
= -1;
1778 int just_numbers
= 0;
1779 int ignore_if_in_upstream
= 0;
1780 int cover_letter
= -1;
1781 int boundary_count
= 0;
1782 int no_binary_diff
= 0;
1783 int zero_commit
= 0;
1784 struct commit
*origin
= NULL
;
1785 const char *in_reply_to
= NULL
;
1786 struct patch_ids ids
;
1787 struct strbuf buf
= STRBUF_INIT
;
1788 int use_patch_format
= 0;
1790 const char *reroll_count
= NULL
;
1791 char *cover_from_description_arg
= NULL
;
1792 char *branch_name
= NULL
;
1793 char *base_commit
= NULL
;
1794 struct base_tree_info bases
;
1795 struct commit
*base
;
1796 int show_progress
= 0;
1797 struct progress
*progress
= NULL
;
1798 struct oid_array idiff_prev
= OID_ARRAY_INIT
;
1799 struct strbuf idiff_title
= STRBUF_INIT
;
1800 const char *rdiff_prev
= NULL
;
1801 struct strbuf rdiff1
= STRBUF_INIT
;
1802 struct strbuf rdiff2
= STRBUF_INIT
;
1803 struct strbuf rdiff_title
= STRBUF_INIT
;
1804 int creation_factor
= -1;
1806 const struct option builtin_format_patch_options
[] = {
1807 OPT_CALLBACK_F('n', "numbered", &numbered
, NULL
,
1808 N_("use [PATCH n/m] even with a single patch"),
1809 PARSE_OPT_NOARG
, numbered_callback
),
1810 OPT_CALLBACK_F('N', "no-numbered", &numbered
, NULL
,
1811 N_("use [PATCH] even with multiple patches"),
1812 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
, no_numbered_callback
),
1813 OPT_BOOL('s', "signoff", &do_signoff
, N_("add a Signed-off-by trailer")),
1814 OPT_BOOL(0, "stdout", &use_stdout
,
1815 N_("print patches to standard out")),
1816 OPT_BOOL(0, "cover-letter", &cover_letter
,
1817 N_("generate a cover letter")),
1818 OPT_BOOL(0, "numbered-files", &just_numbers
,
1819 N_("use simple number sequence for output file names")),
1820 OPT_STRING(0, "suffix", &fmt_patch_suffix
, N_("sfx"),
1821 N_("use <sfx> instead of '.patch'")),
1822 OPT_INTEGER(0, "start-number", &start_number
,
1823 N_("start numbering patches at <n> instead of 1")),
1824 OPT_STRING('v', "reroll-count", &reroll_count
, N_("reroll-count"),
1825 N_("mark the series as Nth re-roll")),
1826 OPT_INTEGER(0, "filename-max-length", &fmt_patch_name_max
,
1827 N_("max length of output filename")),
1828 OPT_CALLBACK_F(0, "rfc", &rev
, NULL
,
1829 N_("use [RFC PATCH] instead of [PATCH]"),
1830 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
, rfc_callback
),
1831 OPT_STRING(0, "cover-from-description", &cover_from_description_arg
,
1832 N_("cover-from-description-mode"),
1833 N_("generate parts of a cover letter based on a branch's description")),
1834 OPT_CALLBACK_F(0, "subject-prefix", &rev
, N_("prefix"),
1835 N_("use [<prefix>] instead of [PATCH]"),
1836 PARSE_OPT_NONEG
, subject_prefix_callback
),
1837 OPT_CALLBACK_F('o', "output-directory", &output_directory
,
1838 N_("dir"), N_("store resulting files in <dir>"),
1839 PARSE_OPT_NONEG
, output_directory_callback
),
1840 OPT_CALLBACK_F('k', "keep-subject", &rev
, NULL
,
1841 N_("don't strip/add [PATCH]"),
1842 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
, keep_callback
),
1843 OPT_BOOL(0, "no-binary", &no_binary_diff
,
1844 N_("don't output binary diffs")),
1845 OPT_BOOL(0, "zero-commit", &zero_commit
,
1846 N_("output all-zero hash in From header")),
1847 OPT_BOOL(0, "ignore-if-in-upstream", &ignore_if_in_upstream
,
1848 N_("don't include a patch matching a commit upstream")),
1849 OPT_SET_INT_F('p', "no-stat", &use_patch_format
,
1850 N_("show patch format instead of default (patch + stat)"),
1851 1, PARSE_OPT_NONEG
),
1852 OPT_GROUP(N_("Messaging")),
1853 OPT_CALLBACK(0, "add-header", NULL
, N_("header"),
1854 N_("add email header"), header_callback
),
1855 OPT_CALLBACK(0, "to", NULL
, N_("email"), N_("add To: header"), to_callback
),
1856 OPT_CALLBACK(0, "cc", NULL
, N_("email"), N_("add Cc: header"), cc_callback
),
1857 OPT_CALLBACK_F(0, "from", &from
, N_("ident"),
1858 N_("set From address to <ident> (or committer ident if absent)"),
1859 PARSE_OPT_OPTARG
, from_callback
),
1860 OPT_STRING(0, "in-reply-to", &in_reply_to
, N_("message-id"),
1861 N_("make first mail a reply to <message-id>")),
1862 OPT_CALLBACK_F(0, "attach", &rev
, N_("boundary"),
1863 N_("attach the patch"), PARSE_OPT_OPTARG
,
1865 OPT_CALLBACK_F(0, "inline", &rev
, N_("boundary"),
1866 N_("inline the patch"),
1867 PARSE_OPT_OPTARG
| PARSE_OPT_NONEG
,
1869 OPT_CALLBACK_F(0, "thread", &thread
, N_("style"),
1870 N_("enable message threading, styles: shallow, deep"),
1871 PARSE_OPT_OPTARG
, thread_callback
),
1872 OPT_STRING(0, "signature", &signature
, N_("signature"),
1873 N_("add a signature")),
1874 OPT_CALLBACK_F(0, "base", &base_commit
, N_("base-commit"),
1875 N_("add prerequisite tree info to the patch series"),
1877 OPT_FILENAME(0, "signature-file", &signature_file
,
1878 N_("add a signature from a file")),
1879 OPT__QUIET(&quiet
, N_("don't print the patch filenames")),
1880 OPT_BOOL(0, "progress", &show_progress
,
1881 N_("show progress while generating patches")),
1882 OPT_CALLBACK(0, "interdiff", &idiff_prev
, N_("rev"),
1883 N_("show changes against <rev> in cover letter or single patch"),
1884 parse_opt_object_name
),
1885 OPT_STRING(0, "range-diff", &rdiff_prev
, N_("refspec"),
1886 N_("show changes against <refspec> in cover letter or single patch")),
1887 OPT_INTEGER(0, "creation-factor", &creation_factor
,
1888 N_("percentage by which creation is weighted")),
1892 extra_hdr
.strdup_strings
= 1;
1893 extra_to
.strdup_strings
= 1;
1894 extra_cc
.strdup_strings
= 1;
1896 init_log_defaults();
1897 init_display_notes(¬es_opt
);
1898 git_config(git_format_config
, NULL
);
1899 repo_init_revisions(the_repository
, &rev
, prefix
);
1900 git_config(grep_config
, &rev
.grep_filter
);
1902 rev
.show_notes
= show_notes
;
1903 memcpy(&rev
.notes_opt
, ¬es_opt
, sizeof(notes_opt
));
1904 rev
.commit_format
= CMIT_FMT_EMAIL
;
1905 rev
.encode_email_headers
= default_encode_email_headers
;
1906 rev
.expand_tabs_in_log_default
= 0;
1907 rev
.verbose_header
= 1;
1909 rev
.max_parents
= 1;
1910 rev
.diffopt
.flags
.recursive
= 1;
1911 rev
.diffopt
.no_free
= 1;
1912 rev
.subject_prefix
= fmt_patch_subject_prefix
;
1913 memset(&s_r_opt
, 0, sizeof(s_r_opt
));
1914 s_r_opt
.def
= "HEAD";
1915 s_r_opt
.revarg_opt
= REVARG_COMMITTISH
;
1917 if (default_attach
) {
1918 rev
.mime_boundary
= default_attach
;
1923 * Parse the arguments before setup_revisions(), or something
1924 * like "git format-patch -o a123 HEAD^.." may fail; a123 is
1925 * possibly a valid SHA1.
1927 argc
= parse_options(argc
, argv
, prefix
, builtin_format_patch_options
,
1928 builtin_format_patch_usage
,
1929 PARSE_OPT_KEEP_ARGV0
| PARSE_OPT_KEEP_UNKNOWN
|
1930 PARSE_OPT_KEEP_DASHDASH
);
1932 /* Make sure "0000-$sub.patch" gives non-negative length for $sub */
1933 if (fmt_patch_name_max
<= strlen("0000-") + strlen(fmt_patch_suffix
))
1934 fmt_patch_name_max
= strlen("0000-") + strlen(fmt_patch_suffix
);
1936 if (cover_from_description_arg
)
1937 cover_from_description_mode
= parse_cover_from_description(cover_from_description_arg
);
1940 struct strbuf sprefix
= STRBUF_INIT
;
1942 strbuf_addf(&sprefix
, "%s v%s",
1943 rev
.subject_prefix
, reroll_count
);
1944 rev
.reroll_count
= reroll_count
;
1945 rev
.subject_prefix
= strbuf_detach(&sprefix
, NULL
);
1948 for (i
= 0; i
< extra_hdr
.nr
; i
++) {
1949 strbuf_addstr(&buf
, extra_hdr
.items
[i
].string
);
1950 strbuf_addch(&buf
, '\n');
1954 strbuf_addstr(&buf
, "To: ");
1955 for (i
= 0; i
< extra_to
.nr
; i
++) {
1957 strbuf_addstr(&buf
, " ");
1958 strbuf_addstr(&buf
, extra_to
.items
[i
].string
);
1959 if (i
+ 1 < extra_to
.nr
)
1960 strbuf_addch(&buf
, ',');
1961 strbuf_addch(&buf
, '\n');
1965 strbuf_addstr(&buf
, "Cc: ");
1966 for (i
= 0; i
< extra_cc
.nr
; i
++) {
1968 strbuf_addstr(&buf
, " ");
1969 strbuf_addstr(&buf
, extra_cc
.items
[i
].string
);
1970 if (i
+ 1 < extra_cc
.nr
)
1971 strbuf_addch(&buf
, ',');
1972 strbuf_addch(&buf
, '\n');
1975 rev
.extra_headers
= to_free
= strbuf_detach(&buf
, NULL
);
1978 if (split_ident_line(&rev
.from_ident
, from
, strlen(from
)))
1979 die(_("invalid ident line: %s"), from
);
1982 if (start_number
< 0)
1986 * If numbered is set solely due to format.numbered in config,
1987 * and it would conflict with --keep-subject (-k) from the
1988 * command line, reset "numbered".
1990 if (numbered
&& keep_subject
&& !numbered_cmdline_opt
)
1993 if (numbered
&& keep_subject
)
1994 die(_("options '%s' and '%s' cannot be used together"), "-n", "-k");
1995 if (keep_subject
&& subject_prefix
)
1996 die(_("options '%s' and '%s' cannot be used together"), "--subject-prefix/--rfc", "-k");
1997 rev
.preserve_subject
= keep_subject
;
1999 argc
= setup_revisions(argc
, argv
, &rev
, &s_r_opt
);
2001 die(_("unrecognized argument: %s"), argv
[1]);
2003 if (rev
.diffopt
.output_format
& DIFF_FORMAT_NAME
)
2004 die(_("--name-only does not make sense"));
2005 if (rev
.diffopt
.output_format
& DIFF_FORMAT_NAME_STATUS
)
2006 die(_("--name-status does not make sense"));
2007 if (rev
.diffopt
.output_format
& DIFF_FORMAT_CHECKDIFF
)
2008 die(_("--check does not make sense"));
2009 if (rev
.remerge_diff
)
2010 die(_("--remerge-diff does not make sense"));
2012 if (!use_patch_format
&&
2013 (!rev
.diffopt
.output_format
||
2014 rev
.diffopt
.output_format
== DIFF_FORMAT_PATCH
))
2015 rev
.diffopt
.output_format
= DIFF_FORMAT_DIFFSTAT
| DIFF_FORMAT_SUMMARY
;
2016 if (!rev
.diffopt
.stat_width
)
2017 rev
.diffopt
.stat_width
= MAIL_DEFAULT_WRAP
;
2019 /* Always generate a patch */
2020 rev
.diffopt
.output_format
|= DIFF_FORMAT_PATCH
;
2022 rev
.zero_commit
= zero_commit
;
2023 rev
.patch_name_max
= fmt_patch_name_max
;
2025 if (!rev
.diffopt
.flags
.text
&& !no_binary_diff
)
2026 rev
.diffopt
.flags
.binary
= 1;
2029 load_display_notes(&rev
.notes_opt
);
2031 die_for_incompatible_opt3(use_stdout
, "--stdout",
2032 rev
.diffopt
.close_file
, "--output",
2033 !!output_directory
, "--output-directory");
2037 } else if (!rev
.diffopt
.close_file
) {
2040 if (!output_directory
)
2041 output_directory
= config_output_directory
;
2042 output_directory
= set_outdir(prefix
, output_directory
);
2044 if (rev
.diffopt
.use_color
!= GIT_COLOR_ALWAYS
)
2045 rev
.diffopt
.use_color
= GIT_COLOR_NEVER
;
2047 * We consider <outdir> as 'outside of gitdir', therefore avoid
2048 * applying adjust_shared_perm in s-c-l-d.
2050 saved
= get_shared_repository();
2051 set_shared_repository(0);
2052 switch (safe_create_leading_directories_const(output_directory
)) {
2057 die(_("could not create leading directories "
2058 "of '%s'"), output_directory
);
2060 set_shared_repository(saved
);
2061 if (mkdir(output_directory
, 0777) < 0 && errno
!= EEXIST
)
2062 die_errno(_("could not create directory '%s'"),
2066 if (rev
.pending
.nr
== 1) {
2069 if (rev
.max_count
< 0 && !rev
.show_root_diff
) {
2071 * This is traditional behaviour of "git format-patch
2072 * origin" that prepares what the origin side still
2075 rev
.pending
.objects
[0].item
->flags
|= UNINTERESTING
;
2076 add_head_to_pending(&rev
);
2080 * Otherwise, it is "format-patch -22 HEAD", and/or
2081 * "format-patch --root HEAD". The user wants
2082 * get_revision() to do the usual traversal.
2085 if (!strcmp(rev
.pending
.objects
[0].name
, "HEAD"))
2089 const char *ref
, *v
;
2090 ref
= resolve_ref_unsafe("HEAD", RESOLVE_REF_READING
,
2092 if (ref
&& skip_prefix(ref
, "refs/heads/", &v
))
2093 branch_name
= xstrdup(v
);
2095 branch_name
= xstrdup(""); /* no branch */
2100 * We cannot move this anywhere earlier because we do want to
2101 * know if --root was given explicitly from the command line.
2103 rev
.show_root_diff
= 1;
2105 if (ignore_if_in_upstream
) {
2106 /* Don't say anything if head and upstream are the same. */
2107 if (rev
.pending
.nr
== 2) {
2108 struct object_array_entry
*o
= rev
.pending
.objects
;
2109 if (oideq(&o
[0].item
->oid
, &o
[1].item
->oid
))
2112 get_patch_ids(&rev
, &ids
);
2115 if (prepare_revision_walk(&rev
))
2116 die(_("revision walk setup failed"));
2118 while ((commit
= get_revision(&rev
)) != NULL
) {
2119 if (commit
->object
.flags
& BOUNDARY
) {
2121 origin
= (boundary_count
== 1) ? commit
: NULL
;
2125 if (ignore_if_in_upstream
&& has_commit_patch_id(commit
, &ids
))
2129 REALLOC_ARRAY(list
, nr
);
2130 list
[nr
- 1] = commit
;
2136 if (cover_letter
== -1) {
2137 if (config_cover_letter
== COVER_AUTO
)
2138 cover_letter
= (total
> 1);
2140 cover_letter
= (config_cover_letter
== COVER_ON
);
2142 if (!keep_subject
&& auto_number
&& (total
> 1 || cover_letter
))
2145 rev
.total
= total
+ start_number
- 1;
2147 if (idiff_prev
.nr
) {
2148 if (!cover_letter
&& total
!= 1)
2149 die(_("--interdiff requires --cover-letter or single patch"));
2150 rev
.idiff_oid1
= &idiff_prev
.oid
[idiff_prev
.nr
- 1];
2151 rev
.idiff_oid2
= get_commit_tree_oid(list
[0]);
2152 rev
.idiff_title
= diff_title(&idiff_title
, reroll_count
,
2154 _("Interdiff against v%d:"));
2157 if (creation_factor
< 0)
2158 creation_factor
= RANGE_DIFF_CREATION_FACTOR_DEFAULT
;
2159 else if (!rdiff_prev
)
2160 die(_("the option '%s' requires '%s'"), "--creation-factor", "--range-diff");
2163 if (!cover_letter
&& total
!= 1)
2164 die(_("--range-diff requires --cover-letter or single patch"));
2166 infer_range_diff_ranges(&rdiff1
, &rdiff2
, rdiff_prev
,
2168 rev
.rdiff1
= rdiff1
.buf
;
2169 rev
.rdiff2
= rdiff2
.buf
;
2170 rev
.creation_factor
= creation_factor
;
2171 rev
.rdiff_title
= diff_title(&rdiff_title
, reroll_count
,
2173 _("Range-diff against v%d:"));
2177 ; /* --no-signature inhibits all signatures */
2178 } else if (signature
&& signature
!= git_version_string
) {
2179 ; /* non-default signature already set */
2180 } else if (signature_file
) {
2181 struct strbuf buf
= STRBUF_INIT
;
2183 if (strbuf_read_file(&buf
, signature_file
, 128) < 0)
2184 die_errno(_("unable to read signature file '%s'"), signature_file
);
2185 signature
= strbuf_detach(&buf
, NULL
);
2188 memset(&bases
, 0, sizeof(bases
));
2189 base
= get_base_commit(base_commit
, list
, nr
);
2191 reset_revision_walk();
2192 clear_object_flags(UNINTERESTING
);
2193 prepare_bases(&bases
, base
, list
, nr
);
2196 if (in_reply_to
|| thread
|| cover_letter
) {
2197 rev
.ref_message_ids
= xmalloc(sizeof(*rev
.ref_message_ids
));
2198 string_list_init_nodup(rev
.ref_message_ids
);
2201 const char *msgid
= clean_message_id(in_reply_to
);
2202 string_list_append(rev
.ref_message_ids
, msgid
);
2204 rev
.numbered_files
= just_numbers
;
2205 rev
.patch_suffix
= fmt_patch_suffix
;
2208 gen_message_id(&rev
, "cover");
2209 make_cover_letter(&rev
, !!output_directory
,
2210 origin
, nr
, list
, branch_name
, quiet
);
2211 print_bases(&bases
, rev
.diffopt
.file
);
2212 print_signature(rev
.diffopt
.file
);
2215 /* interdiff/range-diff in cover-letter; omit from patches */
2216 rev
.idiff_oid1
= NULL
;
2219 rev
.add_signoff
= do_signoff
;
2222 progress
= start_delayed_progress(_("Generating patches"), total
);
2225 display_progress(progress
, total
- nr
);
2227 rev
.nr
= total
- nr
+ (start_number
- 1);
2228 /* Make the second and subsequent mails replies to the first */
2230 /* Have we already had a message ID? */
2231 if (rev
.message_id
) {
2233 * For deep threading: make every mail
2234 * a reply to the previous one, no
2235 * matter what other options are set.
2237 * For shallow threading:
2239 * Without --cover-letter and
2240 * --in-reply-to, make every mail a
2241 * reply to the one before.
2243 * With --in-reply-to but no
2244 * --cover-letter, make every mail a
2245 * reply to the <reply-to>.
2247 * With --cover-letter, make every
2248 * mail but the cover letter a reply
2249 * to the cover letter. The cover
2250 * letter is a reply to the
2251 * --in-reply-to, if specified.
2253 if (thread
== THREAD_SHALLOW
2254 && rev
.ref_message_ids
->nr
> 0
2255 && (!cover_letter
|| rev
.nr
> 1))
2256 free(rev
.message_id
);
2258 string_list_append(rev
.ref_message_ids
,
2261 gen_message_id(&rev
, oid_to_hex(&commit
->object
.oid
));
2264 if (output_directory
&&
2265 open_next_file(rev
.numbered_files
? NULL
: commit
, NULL
, &rev
, quiet
))
2266 die(_("failed to create output files"));
2267 shown
= log_tree_commit(&rev
, commit
);
2268 free_commit_buffer(the_repository
->parsed_objects
,
2271 /* We put one extra blank line between formatted
2272 * patches and this flag is used by log-tree code
2273 * to see if it needs to emit a LF before showing
2274 * the log; when using one file per patch, we do
2275 * not want the extra blank line.
2277 if (output_directory
)
2280 print_bases(&bases
, rev
.diffopt
.file
);
2281 if (rev
.mime_boundary
)
2282 fprintf(rev
.diffopt
.file
, "\n--%s%s--\n\n\n",
2283 mime_boundary_leader
,
2286 print_signature(rev
.diffopt
.file
);
2288 if (output_directory
)
2289 fclose(rev
.diffopt
.file
);
2291 stop_progress(&progress
);
2294 string_list_clear(&extra_to
, 0);
2295 string_list_clear(&extra_cc
, 0);
2296 string_list_clear(&extra_hdr
, 0);
2297 if (ignore_if_in_upstream
)
2298 free_patch_ids(&ids
);
2301 oid_array_clear(&idiff_prev
);
2302 strbuf_release(&idiff_title
);
2303 strbuf_release(&rdiff1
);
2304 strbuf_release(&rdiff2
);
2305 strbuf_release(&rdiff_title
);
2307 if (rev
.ref_message_ids
)
2308 string_list_clear(rev
.ref_message_ids
, 0);
2309 free(rev
.ref_message_ids
);
2310 return cmd_log_deinit(0, &rev
);
2313 static int add_pending_commit(const char *arg
, struct rev_info
*revs
, int flags
)
2315 struct object_id oid
;
2316 if (get_oid(arg
, &oid
) == 0) {
2317 struct commit
*commit
= lookup_commit_reference(the_repository
,
2320 commit
->object
.flags
|= flags
;
2321 add_pending_object(revs
, &commit
->object
, arg
);
2328 static const char * const cherry_usage
[] = {
2329 N_("git cherry [-v] [<upstream> [<head> [<limit>]]]"),
2333 static void print_commit(char sign
, struct commit
*commit
, int verbose
,
2334 int abbrev
, FILE *file
)
2337 fprintf(file
, "%c %s\n", sign
,
2338 find_unique_abbrev(&commit
->object
.oid
, abbrev
));
2340 struct strbuf buf
= STRBUF_INIT
;
2341 pp_commit_easy(CMIT_FMT_ONELINE
, commit
, &buf
);
2342 fprintf(file
, "%c %s %s\n", sign
,
2343 find_unique_abbrev(&commit
->object
.oid
, abbrev
),
2345 strbuf_release(&buf
);
2349 int cmd_cherry(int argc
, const char **argv
, const char *prefix
)
2351 struct rev_info revs
;
2352 struct patch_ids ids
;
2353 struct commit
*commit
;
2354 struct commit_list
*list
= NULL
;
2355 struct branch
*current_branch
;
2356 const char *upstream
;
2357 const char *head
= "HEAD";
2358 const char *limit
= NULL
;
2359 int verbose
= 0, abbrev
= 0;
2361 struct option options
[] = {
2362 OPT__ABBREV(&abbrev
),
2363 OPT__VERBOSE(&verbose
, N_("be verbose")),
2367 argc
= parse_options(argc
, argv
, prefix
, options
, cherry_usage
, 0);
2380 current_branch
= branch_get(NULL
);
2381 upstream
= branch_get_upstream(current_branch
, NULL
);
2383 fprintf(stderr
, _("Could not find a tracked"
2384 " remote branch, please"
2385 " specify <upstream> manually.\n"));
2386 usage_with_options(cherry_usage
, options
);
2390 repo_init_revisions(the_repository
, &revs
, prefix
);
2391 revs
.max_parents
= 1;
2393 if (add_pending_commit(head
, &revs
, 0))
2394 die(_("unknown commit %s"), head
);
2395 if (add_pending_commit(upstream
, &revs
, UNINTERESTING
))
2396 die(_("unknown commit %s"), upstream
);
2398 /* Don't say anything if head and upstream are the same. */
2399 if (revs
.pending
.nr
== 2) {
2400 struct object_array_entry
*o
= revs
.pending
.objects
;
2401 if (oideq(&o
[0].item
->oid
, &o
[1].item
->oid
))
2405 get_patch_ids(&revs
, &ids
);
2407 if (limit
&& add_pending_commit(limit
, &revs
, UNINTERESTING
))
2408 die(_("unknown commit %s"), limit
);
2410 /* reverse the list of commits */
2411 if (prepare_revision_walk(&revs
))
2412 die(_("revision walk setup failed"));
2413 while ((commit
= get_revision(&revs
)) != NULL
) {
2414 commit_list_insert(commit
, &list
);
2420 commit
= list
->item
;
2421 if (has_commit_patch_id(commit
, &ids
))
2423 print_commit(sign
, commit
, verbose
, abbrev
, revs
.diffopt
.file
);
2427 free_patch_ids(&ids
);