2 * Builtin "git log" and related commands (show, whatchanged)
4 * (C) Copyright 2006 Linus Torvalds
7 #include "git-compat-util.h"
11 #include "environment.h"
15 #include "object-file.h"
16 #include "object-name.h"
17 #include "object-store.h"
22 #include "diff-merges.h"
26 #include "oid-array.h"
28 #include "reflog-walk.h"
29 #include "patch-ids.h"
30 #include "run-command.h"
33 #include "string-list.h"
34 #include "parse-options.h"
37 #include "streaming.h"
40 #include "gpg-interface.h"
42 #include "commit-slab.h"
43 #include "repository.h"
44 #include "commit-reach.h"
45 #include "range-diff.h"
46 #include "tmp-objdir.h"
47 #include "write-or-die.h"
49 #define MAIL_DEFAULT_WRAP 72
50 #define COVER_FROM_AUTO_MAX_SUBJECT_LEN 100
51 #define FORMAT_PATCH_NAME_MAX_DEFAULT 64
53 /* Set a default date-time format for git log ("log.date" config variable) */
54 static const char *default_date_mode
= NULL
;
56 static int default_abbrev_commit
;
57 static int default_show_root
= 1;
58 static int default_follow
;
59 static int default_show_signature
;
60 static int default_encode_email_headers
= 1;
61 static int decoration_style
;
62 static int decoration_given
;
63 static int use_mailmap_config
= 1;
64 static unsigned int force_in_body_from
;
65 static int stdout_mboxrd
;
66 static const char *fmt_patch_subject_prefix
= "PATCH";
67 static int fmt_patch_name_max
= FORMAT_PATCH_NAME_MAX_DEFAULT
;
68 static const char *fmt_pretty
;
70 static const char * const builtin_log_usage
[] = {
71 N_("git log [<options>] [<revision-range>] [[--] <path>...]"),
72 N_("git show [<options>] <object>..."),
76 struct line_opt_callback_data
{
79 struct string_list args
;
82 static int session_is_interactive(void)
84 return isatty(1) || pager_in_use();
87 static int auto_decoration_style(void)
89 return session_is_interactive() ? DECORATE_SHORT_REFS
: 0;
92 static int parse_decoration_style(const char *value
)
94 switch (git_parse_maybe_bool(value
)) {
96 return DECORATE_SHORT_REFS
;
102 if (!strcmp(value
, "full"))
103 return DECORATE_FULL_REFS
;
104 else if (!strcmp(value
, "short"))
105 return DECORATE_SHORT_REFS
;
106 else if (!strcmp(value
, "auto"))
107 return auto_decoration_style();
109 * Please update _git_log() in git-completion.bash when you
110 * add new decoration styles.
115 static int use_default_decoration_filter
= 1;
116 static struct string_list decorate_refs_exclude
= STRING_LIST_INIT_NODUP
;
117 static struct string_list decorate_refs_exclude_config
= STRING_LIST_INIT_NODUP
;
118 static struct string_list decorate_refs_include
= STRING_LIST_INIT_NODUP
;
120 static int clear_decorations_callback(const struct option
*opt
,
121 const char *arg
, int unset
)
123 string_list_clear(&decorate_refs_include
, 0);
124 string_list_clear(&decorate_refs_exclude
, 0);
125 use_default_decoration_filter
= 0;
129 static int decorate_callback(const struct option
*opt
, const char *arg
, int unset
)
132 decoration_style
= 0;
134 decoration_style
= parse_decoration_style(arg
);
136 decoration_style
= DECORATE_SHORT_REFS
;
138 if (decoration_style
< 0)
139 die(_("invalid --decorate option: %s"), arg
);
141 decoration_given
= 1;
146 static int log_line_range_callback(const struct option
*option
, const char *arg
, int unset
)
148 struct line_opt_callback_data
*data
= option
->value
;
150 BUG_ON_OPT_NEG(unset
);
155 data
->rev
->line_level_traverse
= 1;
156 string_list_append(&data
->args
, arg
);
161 static void init_log_defaults(void)
163 init_diff_ui_defaults();
165 decoration_style
= auto_decoration_style();
168 static void cmd_log_init_defaults(struct rev_info
*rev
)
171 get_commit_format(fmt_pretty
, rev
);
173 rev
->diffopt
.flags
.default_follow_renames
= 1;
174 rev
->verbose_header
= 1;
175 rev
->diffopt
.flags
.recursive
= 1;
176 rev
->diffopt
.stat_width
= -1; /* use full terminal width */
177 rev
->diffopt
.stat_graph_width
= -1; /* respect statGraphWidth config */
178 rev
->abbrev_commit
= default_abbrev_commit
;
179 rev
->show_root_diff
= default_show_root
;
180 rev
->subject_prefix
= fmt_patch_subject_prefix
;
181 rev
->patch_name_max
= fmt_patch_name_max
;
182 rev
->show_signature
= default_show_signature
;
183 rev
->encode_email_headers
= default_encode_email_headers
;
184 rev
->diffopt
.flags
.allow_textconv
= 1;
186 if (default_date_mode
)
187 parse_date_format(default_date_mode
, &rev
->date_mode
);
190 static void set_default_decoration_filter(struct decoration_filter
*decoration_filter
)
194 struct string_list
*include
= decoration_filter
->include_ref_pattern
;
195 const struct string_list
*config_exclude
=
196 git_config_get_value_multi("log.excludeDecoration");
198 if (config_exclude
) {
199 struct string_list_item
*item
;
200 for_each_string_list_item(item
, config_exclude
)
201 string_list_append(decoration_filter
->exclude_ref_config_pattern
,
206 * By default, decorate_all is disabled. Enable it if
207 * log.initialDecorationSet=all. Don't ever disable it by config,
208 * since the command-line takes precedent.
210 if (use_default_decoration_filter
&&
211 !git_config_get_string("log.initialdecorationset", &value
) &&
212 !strcmp("all", value
))
213 use_default_decoration_filter
= 0;
216 if (!use_default_decoration_filter
||
217 decoration_filter
->exclude_ref_pattern
->nr
||
218 decoration_filter
->include_ref_pattern
->nr
||
219 decoration_filter
->exclude_ref_config_pattern
->nr
)
223 * No command-line or config options were given, so
224 * populate with sensible defaults.
226 for (i
= 0; i
< ARRAY_SIZE(ref_namespace
); i
++) {
227 if (!ref_namespace
[i
].decoration
)
230 string_list_append(include
, ref_namespace
[i
].ref
);
234 static void cmd_log_init_finish(int argc
, const char **argv
, const char *prefix
,
235 struct rev_info
*rev
, struct setup_revision_opt
*opt
)
237 struct userformat_want w
;
238 int quiet
= 0, source
= 0, mailmap
;
239 static struct line_opt_callback_data line_cb
= {NULL
, NULL
, STRING_LIST_INIT_DUP
};
240 struct decoration_filter decoration_filter
= {
241 .exclude_ref_pattern
= &decorate_refs_exclude
,
242 .include_ref_pattern
= &decorate_refs_include
,
243 .exclude_ref_config_pattern
= &decorate_refs_exclude_config
,
245 static struct revision_sources revision_sources
;
247 const struct option builtin_log_options
[] = {
248 OPT__QUIET(&quiet
, N_("suppress diff output")),
249 OPT_BOOL(0, "source", &source
, N_("show source")),
250 OPT_BOOL(0, "use-mailmap", &mailmap
, N_("use mail map file")),
251 OPT_ALIAS(0, "mailmap", "use-mailmap"),
252 OPT_CALLBACK_F(0, "clear-decorations", NULL
, NULL
,
253 N_("clear all previously-defined decoration filters"),
254 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
,
255 clear_decorations_callback
),
256 OPT_STRING_LIST(0, "decorate-refs", &decorate_refs_include
,
257 N_("pattern"), N_("only decorate refs that match <pattern>")),
258 OPT_STRING_LIST(0, "decorate-refs-exclude", &decorate_refs_exclude
,
259 N_("pattern"), N_("do not decorate refs that match <pattern>")),
260 OPT_CALLBACK_F(0, "decorate", NULL
, NULL
, N_("decorate options"),
261 PARSE_OPT_OPTARG
, decorate_callback
),
262 OPT_CALLBACK('L', NULL
, &line_cb
, "range:file",
263 N_("trace the evolution of line range <start>,<end> or function :<funcname> in <file>"),
264 log_line_range_callback
),
269 line_cb
.prefix
= prefix
;
271 mailmap
= use_mailmap_config
;
272 argc
= parse_options(argc
, argv
, prefix
,
273 builtin_log_options
, builtin_log_usage
,
274 PARSE_OPT_KEEP_ARGV0
| PARSE_OPT_KEEP_UNKNOWN_OPT
|
275 PARSE_OPT_KEEP_DASHDASH
);
278 rev
->diffopt
.output_format
|= DIFF_FORMAT_NO_OUTPUT
;
279 argc
= setup_revisions(argc
, argv
, rev
, opt
);
281 /* Any arguments at this point are not recognized */
283 die(_("unrecognized argument: %s"), argv
[1]);
285 if (rev
->line_level_traverse
&& rev
->prune_data
.nr
)
286 die(_("-L<range>:<file> cannot be used with pathspec"));
288 memset(&w
, 0, sizeof(w
));
289 userformat_find_requirements(NULL
, &w
);
291 if (!rev
->show_notes_given
&& (!rev
->pretty_given
|| w
.notes
))
294 load_display_notes(&rev
->notes_opt
);
296 if ((rev
->diffopt
.pickaxe_opts
& DIFF_PICKAXE_KINDS_MASK
) ||
297 rev
->diffopt
.filter
|| rev
->diffopt
.flags
.follow_renames
)
298 rev
->always_show_header
= 0;
300 if (source
|| w
.source
) {
301 init_revision_sources(&revision_sources
);
302 rev
->sources
= &revision_sources
;
306 rev
->mailmap
= xmalloc(sizeof(struct string_list
));
307 string_list_init_nodup(rev
->mailmap
);
308 read_mailmap(rev
->mailmap
);
311 if (rev
->pretty_given
&& rev
->commit_format
== CMIT_FMT_RAW
) {
313 * "log --pretty=raw" is special; ignore UI oriented
314 * configuration variables such as decoration.
316 if (!decoration_given
)
317 decoration_style
= 0;
318 if (!rev
->abbrev_commit_given
)
319 rev
->abbrev_commit
= 0;
322 if (rev
->commit_format
== CMIT_FMT_USERFORMAT
) {
325 * Disable decoration loading if the format will not
328 decoration_style
= 0;
329 } else if (!decoration_style
) {
331 * If we are going to show them, make sure we do load
332 * them here, but taking care not to override a
333 * specific style set by config or --decorate.
335 decoration_style
= DECORATE_SHORT_REFS
;
339 if (decoration_style
|| rev
->simplify_by_decoration
) {
340 set_default_decoration_filter(&decoration_filter
);
342 if (decoration_style
)
343 rev
->show_decorations
= 1;
345 load_ref_decorations(&decoration_filter
, decoration_style
);
348 if (rev
->line_level_traverse
)
349 line_log_init(rev
, line_cb
.prefix
, &line_cb
.args
);
354 static void cmd_log_init(int argc
, const char **argv
, const char *prefix
,
355 struct rev_info
*rev
, struct setup_revision_opt
*opt
)
357 cmd_log_init_defaults(rev
);
358 cmd_log_init_finish(argc
, argv
, prefix
, rev
, opt
);
361 static int cmd_log_deinit(int ret
, struct rev_info
*rev
)
363 release_revisions(rev
);
368 * This gives a rough estimate for how many commits we
369 * will print out in the list.
371 static int estimate_commit_count(struct commit_list
*list
)
376 struct commit
*commit
= list
->item
;
377 unsigned int flags
= commit
->object
.flags
;
379 if (!(flags
& (TREESAME
| UNINTERESTING
)))
385 static void show_early_header(struct rev_info
*rev
, const char *stage
, int nr
)
387 if (rev
->shown_one
) {
389 if (rev
->commit_format
!= CMIT_FMT_ONELINE
)
390 putchar(rev
->diffopt
.line_termination
);
392 fprintf(rev
->diffopt
.file
, _("Final output: %d %s\n"), nr
, stage
);
395 static struct itimerval early_output_timer
;
397 static void log_show_early(struct rev_info
*revs
, struct commit_list
*list
)
399 int i
= revs
->early_output
;
401 int no_free
= revs
->diffopt
.no_free
;
403 revs
->diffopt
.no_free
= 0;
404 sort_in_topological_order(&list
, revs
->sort_order
);
406 struct commit
*commit
= list
->item
;
407 switch (simplify_commit(revs
, commit
)) {
410 int n
= estimate_commit_count(list
);
411 show_early_header(revs
, "incomplete", n
);
414 log_tree_commit(revs
, commit
);
420 revs
->diffopt
.no_free
= no_free
;
421 diff_free(&revs
->diffopt
);
427 /* Did we already get enough commits for the early output? */
429 revs
->diffopt
.no_free
= 0;
430 diff_free(&revs
->diffopt
);
435 * ..if no, then repeat it twice a second until we
438 * NOTE! We don't use "it_interval", because if the
439 * reader isn't listening, we want our output to be
440 * throttled by the writing, and not have the timer
441 * trigger every second even if we're blocked on a
444 early_output_timer
.it_value
.tv_sec
= 0;
445 early_output_timer
.it_value
.tv_usec
= 500000;
446 setitimer(ITIMER_REAL
, &early_output_timer
, NULL
);
449 static void early_output(int signal UNUSED
)
451 show_early_output
= log_show_early
;
454 static void setup_early_output(void)
459 * Set up the signal handler, minimally intrusively:
460 * we only set a single volatile integer word (not
461 * using sigatomic_t - trying to avoid unnecessary
462 * system dependencies and headers), and using
465 memset(&sa
, 0, sizeof(sa
));
466 sa
.sa_handler
= early_output
;
467 sigemptyset(&sa
.sa_mask
);
468 sa
.sa_flags
= SA_RESTART
;
469 sigaction(SIGALRM
, &sa
, NULL
);
472 * If we can get the whole output in less than a
473 * tenth of a second, don't even bother doing the
474 * early-output thing..
476 * This is a one-time-only trigger.
478 early_output_timer
.it_value
.tv_sec
= 0;
479 early_output_timer
.it_value
.tv_usec
= 100000;
480 setitimer(ITIMER_REAL
, &early_output_timer
, NULL
);
483 static void finish_early_output(struct rev_info
*rev
)
485 int n
= estimate_commit_count(rev
->commits
);
486 signal(SIGALRM
, SIG_IGN
);
487 show_early_header(rev
, "done", n
);
490 static int cmd_log_walk_no_free(struct rev_info
*rev
)
492 struct commit
*commit
;
496 if (rev
->remerge_diff
) {
497 rev
->remerge_objdir
= tmp_objdir_create("remerge-diff");
498 if (!rev
->remerge_objdir
)
499 die(_("unable to create temporary object directory"));
500 tmp_objdir_replace_primary_odb(rev
->remerge_objdir
, 1);
503 if (rev
->early_output
)
504 setup_early_output();
506 if (prepare_revision_walk(rev
))
507 die(_("revision walk setup failed"));
509 if (rev
->early_output
)
510 finish_early_output(rev
);
513 * For --check and --exit-code, the exit code is based on CHECK_FAILED
514 * and HAS_CHANGES being accumulated in rev->diffopt, so be careful to
515 * retain that state information if replacing rev->diffopt in this loop
517 while ((commit
= get_revision(rev
)) != NULL
) {
518 if (!log_tree_commit(rev
, commit
) && rev
->max_count
>= 0)
520 * We decremented max_count in get_revision,
521 * but we didn't actually show the commit.
524 if (!rev
->reflog_info
) {
526 * We may show a given commit multiple times when
527 * walking the reflogs.
529 free_commit_buffer(the_repository
->parsed_objects
,
531 free_commit_list(commit
->parents
);
532 commit
->parents
= NULL
;
534 if (saved_nrl
< rev
->diffopt
.needed_rename_limit
)
535 saved_nrl
= rev
->diffopt
.needed_rename_limit
;
536 if (rev
->diffopt
.degraded_cc_to_c
)
539 rev
->diffopt
.degraded_cc_to_c
= saved_dcctc
;
540 rev
->diffopt
.needed_rename_limit
= saved_nrl
;
542 if (rev
->remerge_diff
) {
543 tmp_objdir_destroy(rev
->remerge_objdir
);
544 rev
->remerge_objdir
= NULL
;
547 if (rev
->diffopt
.output_format
& DIFF_FORMAT_CHECKDIFF
&&
548 rev
->diffopt
.flags
.check_failed
) {
551 return diff_result_code(&rev
->diffopt
, 0);
554 static int cmd_log_walk(struct rev_info
*rev
)
558 rev
->diffopt
.no_free
= 1;
559 retval
= cmd_log_walk_no_free(rev
);
560 rev
->diffopt
.no_free
= 0;
561 diff_free(&rev
->diffopt
);
565 static int git_log_config(const char *var
, const char *value
, void *cb
)
567 const char *slot_name
;
569 if (!strcmp(var
, "format.pretty"))
570 return git_config_string(&fmt_pretty
, var
, value
);
571 if (!strcmp(var
, "format.subjectprefix"))
572 return git_config_string(&fmt_patch_subject_prefix
, var
, value
);
573 if (!strcmp(var
, "format.filenamemaxlength")) {
574 fmt_patch_name_max
= git_config_int(var
, value
);
577 if (!strcmp(var
, "format.encodeemailheaders")) {
578 default_encode_email_headers
= git_config_bool(var
, value
);
581 if (!strcmp(var
, "log.abbrevcommit")) {
582 default_abbrev_commit
= git_config_bool(var
, value
);
585 if (!strcmp(var
, "log.date"))
586 return git_config_string(&default_date_mode
, var
, value
);
587 if (!strcmp(var
, "log.decorate")) {
588 decoration_style
= parse_decoration_style(value
);
589 if (decoration_style
< 0)
590 decoration_style
= 0; /* maybe warn? */
593 if (!strcmp(var
, "log.diffmerges"))
594 return diff_merges_config(value
);
595 if (!strcmp(var
, "log.showroot")) {
596 default_show_root
= git_config_bool(var
, value
);
599 if (!strcmp(var
, "log.follow")) {
600 default_follow
= git_config_bool(var
, value
);
603 if (skip_prefix(var
, "color.decorate.", &slot_name
))
604 return parse_decorate_color_config(var
, slot_name
, value
);
605 if (!strcmp(var
, "log.mailmap")) {
606 use_mailmap_config
= git_config_bool(var
, value
);
609 if (!strcmp(var
, "log.showsignature")) {
610 default_show_signature
= git_config_bool(var
, value
);
614 return git_diff_ui_config(var
, value
, cb
);
617 int cmd_whatchanged(int argc
, const char **argv
, const char *prefix
)
620 struct setup_revision_opt opt
;
623 git_config(git_log_config
, NULL
);
625 repo_init_revisions(the_repository
, &rev
, prefix
);
626 git_config(grep_config
, &rev
.grep_filter
);
629 rev
.simplify_history
= 0;
630 memset(&opt
, 0, sizeof(opt
));
632 opt
.revarg_opt
= REVARG_COMMITTISH
;
633 cmd_log_init(argc
, argv
, prefix
, &rev
, &opt
);
634 if (!rev
.diffopt
.output_format
)
635 rev
.diffopt
.output_format
= DIFF_FORMAT_RAW
;
636 return cmd_log_deinit(cmd_log_walk(&rev
), &rev
);
639 static void show_tagger(const char *buf
, struct rev_info
*rev
)
641 struct strbuf out
= STRBUF_INIT
;
642 struct pretty_print_context pp
= {0};
644 pp
.fmt
= rev
->commit_format
;
645 pp
.date_mode
= rev
->date_mode
;
646 pp_user_info(&pp
, "Tagger", &out
, buf
, get_log_output_encoding());
647 fprintf(rev
->diffopt
.file
, "%s", out
.buf
);
648 strbuf_release(&out
);
651 static int show_blob_object(const struct object_id
*oid
, struct rev_info
*rev
, const char *obj_name
)
653 struct object_id oidc
;
654 struct object_context obj_context
;
658 fflush(rev
->diffopt
.file
);
659 if (!rev
->diffopt
.flags
.textconv_set_via_cmdline
||
660 !rev
->diffopt
.flags
.allow_textconv
)
661 return stream_blob_to_fd(1, oid
, NULL
, 0);
663 if (get_oid_with_context(the_repository
, obj_name
,
665 &oidc
, &obj_context
))
666 die(_("not a valid object name %s"), obj_name
);
667 if (!obj_context
.path
||
668 !textconv_object(the_repository
, obj_context
.path
,
669 obj_context
.mode
, &oidc
, 1, &buf
, &size
)) {
670 free(obj_context
.path
);
671 return stream_blob_to_fd(1, oid
, NULL
, 0);
675 die(_("git show %s: bad file"), obj_name
);
677 write_or_die(1, buf
, size
);
678 free(obj_context
.path
);
682 static int show_tag_object(const struct object_id
*oid
, struct rev_info
*rev
)
685 enum object_type type
;
686 char *buf
= repo_read_object_file(the_repository
, oid
, &type
, &size
);
690 return error(_("could not read object %s"), oid_to_hex(oid
));
692 assert(type
== OBJ_TAG
);
693 while (offset
< size
&& buf
[offset
] != '\n') {
694 int new_offset
= offset
+ 1;
696 while (new_offset
< size
&& buf
[new_offset
++] != '\n')
698 if (skip_prefix(buf
+ offset
, "tagger ", &ident
))
699 show_tagger(ident
, rev
);
704 fwrite(buf
+ offset
, size
- offset
, 1, rev
->diffopt
.file
);
709 static int show_tree_object(const struct object_id
*oid UNUSED
,
710 struct strbuf
*base UNUSED
,
711 const char *pathname
, unsigned mode
,
714 FILE *file
= context
;
715 fprintf(file
, "%s%s\n", pathname
, S_ISDIR(mode
) ? "/" : "");
719 static void show_setup_revisions_tweak(struct rev_info
*rev
,
720 struct setup_revision_opt
*opt
)
722 if (rev
->first_parent_only
)
723 diff_merges_default_to_first_parent(rev
);
725 diff_merges_default_to_dense_combined(rev
);
726 if (!rev
->diffopt
.output_format
)
727 rev
->diffopt
.output_format
= DIFF_FORMAT_PATCH
;
730 int cmd_show(int argc
, const char **argv
, const char *prefix
)
734 struct setup_revision_opt opt
;
735 struct pathspec match_all
;
739 git_config(git_log_config
, NULL
);
741 if (the_repository
->gitdir
) {
742 prepare_repo_settings(the_repository
);
743 the_repository
->settings
.command_requires_full_index
= 0;
746 memset(&match_all
, 0, sizeof(match_all
));
747 repo_init_revisions(the_repository
, &rev
, prefix
);
748 git_config(grep_config
, &rev
.grep_filter
);
751 rev
.always_show_header
= 1;
753 rev
.diffopt
.stat_width
= -1; /* Scale to real terminal size */
755 memset(&opt
, 0, sizeof(opt
));
757 opt
.tweak
= show_setup_revisions_tweak
;
758 cmd_log_init(argc
, argv
, prefix
, &rev
, &opt
);
761 return cmd_log_deinit(cmd_log_walk(&rev
), &rev
);
763 rev
.diffopt
.no_free
= 1;
764 for (i
= 0; i
< rev
.pending
.nr
&& !ret
; i
++) {
765 struct object
*o
= rev
.pending
.objects
[i
].item
;
766 const char *name
= rev
.pending
.objects
[i
].name
;
769 ret
= show_blob_object(&o
->oid
, &rev
, name
);
772 struct tag
*t
= (struct tag
*)o
;
773 struct object_id
*oid
= get_tagged_oid(t
);
777 fprintf(rev
.diffopt
.file
, "%stag %s%s\n",
778 diff_get_color_opt(&rev
.diffopt
, DIFF_COMMIT
),
780 diff_get_color_opt(&rev
.diffopt
, DIFF_RESET
));
781 ret
= show_tag_object(&o
->oid
, &rev
);
785 o
= parse_object(the_repository
, oid
);
787 ret
= error(_("could not read object %s"),
789 rev
.pending
.objects
[i
].item
= o
;
796 fprintf(rev
.diffopt
.file
, "%stree %s%s\n\n",
797 diff_get_color_opt(&rev
.diffopt
, DIFF_COMMIT
),
799 diff_get_color_opt(&rev
.diffopt
, DIFF_RESET
));
800 read_tree(the_repository
, (struct tree
*)o
,
801 &match_all
, show_tree_object
,
807 struct object_array old
;
808 struct object_array blank
= OBJECT_ARRAY_INIT
;
810 memcpy(&old
, &rev
.pending
, sizeof(old
));
811 memcpy(&rev
.pending
, &blank
, sizeof(rev
.pending
));
813 add_object_array(o
, name
, &rev
.pending
);
814 ret
= cmd_log_walk_no_free(&rev
);
818 * object_array_clear(&pending). It was
819 * cleared already in prepare_revision_walk()
821 memcpy(&rev
.pending
, &old
, sizeof(rev
.pending
));
825 ret
= error(_("unknown type: %d"), o
->type
);
829 rev
.diffopt
.no_free
= 0;
830 diff_free(&rev
.diffopt
);
832 return cmd_log_deinit(ret
, &rev
);
836 * This is equivalent to "git log -g --abbrev-commit --pretty=oneline"
838 int cmd_log_reflog(int argc
, const char **argv
, const char *prefix
)
841 struct setup_revision_opt opt
;
844 git_config(git_log_config
, NULL
);
846 repo_init_revisions(the_repository
, &rev
, prefix
);
847 init_reflog_walk(&rev
.reflog_info
);
848 git_config(grep_config
, &rev
.grep_filter
);
850 rev
.verbose_header
= 1;
851 memset(&opt
, 0, sizeof(opt
));
853 cmd_log_init_defaults(&rev
);
854 rev
.abbrev_commit
= 1;
855 rev
.commit_format
= CMIT_FMT_ONELINE
;
856 rev
.use_terminator
= 1;
857 rev
.always_show_header
= 1;
858 cmd_log_init_finish(argc
, argv
, prefix
, &rev
, &opt
);
860 return cmd_log_deinit(cmd_log_walk(&rev
), &rev
);
863 static void log_setup_revisions_tweak(struct rev_info
*rev
,
864 struct setup_revision_opt
*opt
)
866 if (rev
->diffopt
.flags
.default_follow_renames
&&
867 rev
->prune_data
.nr
== 1)
868 rev
->diffopt
.flags
.follow_renames
= 1;
870 if (rev
->first_parent_only
)
871 diff_merges_default_to_first_parent(rev
);
874 int cmd_log(int argc
, const char **argv
, const char *prefix
)
877 struct setup_revision_opt opt
;
880 git_config(git_log_config
, NULL
);
882 repo_init_revisions(the_repository
, &rev
, prefix
);
883 git_config(grep_config
, &rev
.grep_filter
);
885 rev
.always_show_header
= 1;
886 memset(&opt
, 0, sizeof(opt
));
888 opt
.revarg_opt
= REVARG_COMMITTISH
;
889 opt
.tweak
= log_setup_revisions_tweak
;
890 cmd_log_init(argc
, argv
, prefix
, &rev
, &opt
);
891 return cmd_log_deinit(cmd_log_walk(&rev
), &rev
);
896 static const char *fmt_patch_suffix
= ".patch";
897 static int numbered
= 0;
898 static int auto_number
= 1;
900 static char *default_attach
= NULL
;
902 static struct string_list extra_hdr
= STRING_LIST_INIT_NODUP
;
903 static struct string_list extra_to
= STRING_LIST_INIT_NODUP
;
904 static struct string_list extra_cc
= STRING_LIST_INIT_NODUP
;
906 static void add_header(const char *value
)
908 struct string_list_item
*item
;
909 int len
= strlen(value
);
910 while (len
&& value
[len
- 1] == '\n')
913 if (!strncasecmp(value
, "to: ", 4)) {
914 item
= string_list_append(&extra_to
, value
+ 4);
916 } else if (!strncasecmp(value
, "cc: ", 4)) {
917 item
= string_list_append(&extra_cc
, value
+ 4);
920 item
= string_list_append(&extra_hdr
, value
);
923 item
->string
[len
] = '\0';
939 enum cover_from_description
{
946 enum auto_base_setting
{
952 static enum thread_level thread
;
953 static int do_signoff
;
954 static enum auto_base_setting auto_base
;
956 static const char *signature
= git_version_string
;
957 static const char *signature_file
;
958 static enum cover_setting config_cover_letter
;
959 static const char *config_output_directory
;
960 static enum cover_from_description cover_from_description_mode
= COVER_FROM_MESSAGE
;
961 static int show_notes
;
962 static struct display_notes_opt notes_opt
;
964 static enum cover_from_description
parse_cover_from_description(const char *arg
)
966 if (!arg
|| !strcmp(arg
, "default"))
967 return COVER_FROM_MESSAGE
;
968 else if (!strcmp(arg
, "none"))
969 return COVER_FROM_NONE
;
970 else if (!strcmp(arg
, "message"))
971 return COVER_FROM_MESSAGE
;
972 else if (!strcmp(arg
, "subject"))
973 return COVER_FROM_SUBJECT
;
974 else if (!strcmp(arg
, "auto"))
975 return COVER_FROM_AUTO
;
977 die(_("%s: invalid cover from description mode"), arg
);
980 static int git_format_config(const char *var
, const char *value
, void *cb
)
982 if (!strcmp(var
, "format.headers")) {
984 die(_("format.headers without value"));
988 if (!strcmp(var
, "format.suffix"))
989 return git_config_string(&fmt_patch_suffix
, var
, value
);
990 if (!strcmp(var
, "format.to")) {
992 return config_error_nonbool(var
);
993 string_list_append(&extra_to
, value
);
996 if (!strcmp(var
, "format.cc")) {
998 return config_error_nonbool(var
);
999 string_list_append(&extra_cc
, value
);
1002 if (!strcmp(var
, "diff.color") || !strcmp(var
, "color.diff") ||
1003 !strcmp(var
, "color.ui") || !strcmp(var
, "diff.submodule")) {
1006 if (!strcmp(var
, "format.numbered")) {
1007 if (value
&& !strcasecmp(value
, "auto")) {
1011 numbered
= git_config_bool(var
, value
);
1012 auto_number
= auto_number
&& numbered
;
1015 if (!strcmp(var
, "format.attach")) {
1016 if (value
&& *value
)
1017 default_attach
= xstrdup(value
);
1018 else if (value
&& !*value
)
1019 FREE_AND_NULL(default_attach
);
1021 default_attach
= xstrdup(git_version_string
);
1024 if (!strcmp(var
, "format.thread")) {
1025 if (value
&& !strcasecmp(value
, "deep")) {
1026 thread
= THREAD_DEEP
;
1029 if (value
&& !strcasecmp(value
, "shallow")) {
1030 thread
= THREAD_SHALLOW
;
1033 thread
= git_config_bool(var
, value
) ? THREAD_SHALLOW
: THREAD_UNSET
;
1036 if (!strcmp(var
, "format.signoff")) {
1037 do_signoff
= git_config_bool(var
, value
);
1040 if (!strcmp(var
, "format.signature"))
1041 return git_config_string(&signature
, var
, value
);
1042 if (!strcmp(var
, "format.signaturefile"))
1043 return git_config_pathname(&signature_file
, var
, value
);
1044 if (!strcmp(var
, "format.coverletter")) {
1045 if (value
&& !strcasecmp(value
, "auto")) {
1046 config_cover_letter
= COVER_AUTO
;
1049 config_cover_letter
= git_config_bool(var
, value
) ? COVER_ON
: COVER_OFF
;
1052 if (!strcmp(var
, "format.outputdirectory"))
1053 return git_config_string(&config_output_directory
, var
, value
);
1054 if (!strcmp(var
, "format.useautobase")) {
1055 if (value
&& !strcasecmp(value
, "whenAble")) {
1056 auto_base
= AUTO_BASE_WHEN_ABLE
;
1059 auto_base
= git_config_bool(var
, value
) ? AUTO_BASE_ALWAYS
: AUTO_BASE_NEVER
;
1062 if (!strcmp(var
, "format.from")) {
1063 int b
= git_parse_maybe_bool(value
);
1066 from
= xstrdup(value
);
1068 from
= xstrdup(git_committer_info(IDENT_NO_DATE
));
1073 if (!strcmp(var
, "format.forceinbodyfrom")) {
1074 force_in_body_from
= git_config_bool(var
, value
);
1077 if (!strcmp(var
, "format.notes")) {
1078 int b
= git_parse_maybe_bool(value
);
1080 enable_ref_display_notes(¬es_opt
, &show_notes
, value
);
1082 enable_default_display_notes(¬es_opt
, &show_notes
);
1084 disable_display_notes(¬es_opt
, &show_notes
);
1087 if (!strcmp(var
, "format.coverfromdescription")) {
1088 cover_from_description_mode
= parse_cover_from_description(value
);
1091 if (!strcmp(var
, "format.mboxrd")) {
1092 stdout_mboxrd
= git_config_bool(var
, value
);
1096 return git_log_config(var
, value
, cb
);
1099 static const char *output_directory
= NULL
;
1100 static int outdir_offset
;
1102 static int open_next_file(struct commit
*commit
, const char *subject
,
1103 struct rev_info
*rev
, int quiet
)
1105 struct strbuf filename
= STRBUF_INIT
;
1107 if (output_directory
) {
1108 strbuf_addstr(&filename
, output_directory
);
1109 strbuf_complete(&filename
, '/');
1112 if (rev
->numbered_files
)
1113 strbuf_addf(&filename
, "%d", rev
->nr
);
1115 fmt_output_commit(&filename
, commit
, rev
);
1117 fmt_output_subject(&filename
, subject
, rev
);
1120 printf("%s\n", filename
.buf
+ outdir_offset
);
1122 if (!(rev
->diffopt
.file
= fopen(filename
.buf
, "w"))) {
1123 error_errno(_("cannot open patch file %s"), filename
.buf
);
1124 strbuf_release(&filename
);
1128 strbuf_release(&filename
);
1132 static void get_patch_ids(struct rev_info
*rev
, struct patch_ids
*ids
)
1134 struct rev_info check_rev
;
1135 struct commit
*commit
, *c1
, *c2
;
1136 struct object
*o1
, *o2
;
1137 unsigned flags1
, flags2
;
1139 if (rev
->pending
.nr
!= 2)
1140 die(_("need exactly one range"));
1142 o1
= rev
->pending
.objects
[0].item
;
1143 o2
= rev
->pending
.objects
[1].item
;
1146 c1
= lookup_commit_reference(the_repository
, &o1
->oid
);
1147 c2
= lookup_commit_reference(the_repository
, &o2
->oid
);
1149 if ((flags1
& UNINTERESTING
) == (flags2
& UNINTERESTING
))
1150 die(_("not a range"));
1152 init_patch_ids(the_repository
, ids
);
1154 /* given a range a..b get all patch ids for b..a */
1155 repo_init_revisions(the_repository
, &check_rev
, rev
->prefix
);
1156 check_rev
.max_parents
= 1;
1157 o1
->flags
^= UNINTERESTING
;
1158 o2
->flags
^= UNINTERESTING
;
1159 add_pending_object(&check_rev
, o1
, "o1");
1160 add_pending_object(&check_rev
, o2
, "o2");
1161 if (prepare_revision_walk(&check_rev
))
1162 die(_("revision walk setup failed"));
1164 while ((commit
= get_revision(&check_rev
)) != NULL
) {
1165 add_commit_patch_id(commit
, ids
);
1168 /* reset for next revision walk */
1169 clear_commit_marks(c1
, SEEN
| UNINTERESTING
| SHOWN
| ADDED
);
1170 clear_commit_marks(c2
, SEEN
| UNINTERESTING
| SHOWN
| ADDED
);
1175 static void gen_message_id(struct rev_info
*info
, char *base
)
1177 struct strbuf buf
= STRBUF_INIT
;
1178 strbuf_addf(&buf
, "%s.%"PRItime
".git.%s", base
,
1179 (timestamp_t
) time(NULL
),
1180 git_committer_info(IDENT_NO_NAME
|IDENT_NO_DATE
|IDENT_STRICT
));
1181 info
->message_id
= strbuf_detach(&buf
, NULL
);
1184 static void print_signature(FILE *file
)
1186 if (!signature
|| !*signature
)
1189 fprintf(file
, "-- \n%s", signature
);
1190 if (signature
[strlen(signature
)-1] != '\n')
1195 static char *find_branch_name(struct rev_info
*rev
)
1197 int i
, positive
= -1;
1198 struct object_id branch_oid
;
1199 const struct object_id
*tip_oid
;
1200 const char *ref
, *v
;
1201 char *full_ref
, *branch
= NULL
;
1203 for (i
= 0; i
< rev
->cmdline
.nr
; i
++) {
1204 if (rev
->cmdline
.rev
[i
].flags
& UNINTERESTING
)
1213 ref
= rev
->cmdline
.rev
[positive
].name
;
1214 tip_oid
= &rev
->cmdline
.rev
[positive
].item
->oid
;
1215 if (repo_dwim_ref(the_repository
, ref
, strlen(ref
), &branch_oid
,
1217 skip_prefix(full_ref
, "refs/heads/", &v
) &&
1218 oideq(tip_oid
, &branch_oid
))
1219 branch
= xstrdup(v
);
1224 static void show_diffstat(struct rev_info
*rev
,
1225 struct commit
*origin
, struct commit
*head
)
1227 struct diff_options opts
;
1229 memcpy(&opts
, &rev
->diffopt
, sizeof(opts
));
1230 opts
.output_format
= DIFF_FORMAT_SUMMARY
| DIFF_FORMAT_DIFFSTAT
;
1231 diff_setup_done(&opts
);
1233 diff_tree_oid(get_commit_tree_oid(origin
),
1234 get_commit_tree_oid(head
),
1236 diffcore_std(&opts
);
1239 fprintf(rev
->diffopt
.file
, "\n");
1242 static void prepare_cover_text(struct pretty_print_context
*pp
,
1243 const char *branch_name
,
1245 const char *encoding
,
1248 const char *subject
= "*** SUBJECT HERE ***";
1249 const char *body
= "*** BLURB HERE ***";
1250 struct strbuf description_sb
= STRBUF_INIT
;
1251 struct strbuf subject_sb
= STRBUF_INIT
;
1253 if (cover_from_description_mode
== COVER_FROM_NONE
)
1256 if (branch_name
&& *branch_name
)
1257 read_branch_desc(&description_sb
, branch_name
);
1258 if (!description_sb
.len
)
1261 if (cover_from_description_mode
== COVER_FROM_SUBJECT
||
1262 cover_from_description_mode
== COVER_FROM_AUTO
)
1263 body
= format_subject(&subject_sb
, description_sb
.buf
, " ");
1265 if (cover_from_description_mode
== COVER_FROM_MESSAGE
||
1266 (cover_from_description_mode
== COVER_FROM_AUTO
&&
1267 subject_sb
.len
> COVER_FROM_AUTO_MAX_SUBJECT_LEN
))
1268 body
= description_sb
.buf
;
1270 subject
= subject_sb
.buf
;
1273 pp_title_line(pp
, &subject
, sb
, encoding
, need_8bit_cte
);
1274 pp_remainder(pp
, &body
, sb
, 0);
1276 strbuf_release(&description_sb
);
1277 strbuf_release(&subject_sb
);
1280 static int get_notes_refs(struct string_list_item
*item
, void *arg
)
1282 strvec_pushf(arg
, "--notes=%s", item
->string
);
1286 static void get_notes_args(struct strvec
*arg
, struct rev_info
*rev
)
1288 if (!rev
->show_notes
) {
1289 strvec_push(arg
, "--no-notes");
1290 } else if (rev
->notes_opt
.use_default_notes
> 0 ||
1291 (rev
->notes_opt
.use_default_notes
== -1 &&
1292 !rev
->notes_opt
.extra_notes_refs
.nr
)) {
1293 strvec_push(arg
, "--notes");
1295 for_each_string_list(&rev
->notes_opt
.extra_notes_refs
, get_notes_refs
, arg
);
1299 static void make_cover_letter(struct rev_info
*rev
, int use_separate_file
,
1300 struct commit
*origin
,
1301 int nr
, struct commit
**list
,
1302 const char *branch_name
,
1305 const char *committer
;
1306 struct shortlog log
;
1307 struct strbuf sb
= STRBUF_INIT
;
1309 const char *encoding
= "UTF-8";
1310 int need_8bit_cte
= 0;
1311 struct pretty_print_context pp
= {0};
1312 struct commit
*head
= list
[0];
1314 if (!cmit_fmt_is_mail(rev
->commit_format
))
1315 die(_("cover letter needs email format"));
1317 committer
= git_committer_info(0);
1319 if (use_separate_file
&&
1320 open_next_file(NULL
, rev
->numbered_files
? NULL
: "cover-letter", rev
, quiet
))
1321 die(_("failed to create cover-letter file"));
1323 log_write_email_headers(rev
, head
, &pp
.after_subject
, &need_8bit_cte
, 0);
1325 for (i
= 0; !need_8bit_cte
&& i
< nr
; i
++) {
1326 const char *buf
= repo_get_commit_buffer(the_repository
,
1328 if (has_non_ascii(buf
))
1330 repo_unuse_commit_buffer(the_repository
, list
[i
], buf
);
1334 branch_name
= find_branch_name(rev
);
1336 pp
.fmt
= CMIT_FMT_EMAIL
;
1337 pp
.date_mode
.type
= DATE_RFC2822
;
1339 pp
.print_email_subject
= 1;
1340 pp_user_info(&pp
, NULL
, &sb
, committer
, encoding
);
1341 prepare_cover_text(&pp
, branch_name
, &sb
, encoding
, need_8bit_cte
);
1342 fprintf(rev
->diffopt
.file
, "%s\n", sb
.buf
);
1344 strbuf_release(&sb
);
1346 shortlog_init(&log
);
1348 log
.wrap
= MAIL_DEFAULT_WRAP
;
1351 log
.file
= rev
->diffopt
.file
;
1352 log
.groups
= SHORTLOG_GROUP_AUTHOR
;
1353 shortlog_finish_setup(&log
);
1354 for (i
= 0; i
< nr
; i
++)
1355 shortlog_add_commit(&log
, list
[i
]);
1357 shortlog_output(&log
);
1359 /* We can only do diffstat with a unique reference point */
1361 show_diffstat(rev
, origin
, head
);
1363 if (rev
->idiff_oid1
) {
1364 fprintf_ln(rev
->diffopt
.file
, "%s", rev
->idiff_title
);
1365 show_interdiff(rev
->idiff_oid1
, rev
->idiff_oid2
, 0,
1371 * Pass minimum required diff-options to range-diff; others
1372 * can be added later if deemed desirable.
1374 struct diff_options opts
;
1375 struct strvec other_arg
= STRVEC_INIT
;
1376 struct range_diff_options range_diff_opts
= {
1377 .creation_factor
= rev
->creation_factor
,
1380 .other_arg
= &other_arg
1383 repo_diff_setup(the_repository
, &opts
);
1384 opts
.file
= rev
->diffopt
.file
;
1385 opts
.use_color
= rev
->diffopt
.use_color
;
1386 diff_setup_done(&opts
);
1387 fprintf_ln(rev
->diffopt
.file
, "%s", rev
->rdiff_title
);
1388 get_notes_args(&other_arg
, rev
);
1389 show_range_diff(rev
->rdiff1
, rev
->rdiff2
, &range_diff_opts
);
1390 strvec_clear(&other_arg
);
1394 static const char *clean_message_id(const char *msg_id
)
1397 const char *a
, *z
, *m
;
1400 while ((ch
= *m
) && (isspace(ch
) || (ch
== '<')))
1405 if (!isspace(ch
) && (ch
!= '>'))
1410 die(_("insane in-reply-to: %s"), msg_id
);
1413 return xmemdupz(a
, z
- a
);
1416 static const char *set_outdir(const char *prefix
, const char *output_directory
)
1418 if (output_directory
&& is_absolute_path(output_directory
))
1419 return output_directory
;
1421 if (!prefix
|| !*prefix
) {
1422 if (output_directory
)
1423 return output_directory
;
1424 /* The user did not explicitly ask for "./" */
1429 outdir_offset
= strlen(prefix
);
1430 if (!output_directory
)
1433 return prefix_filename(prefix
, output_directory
);
1436 static const char * const builtin_format_patch_usage
[] = {
1437 N_("git format-patch [<options>] [<since> | <revision-range>]"),
1441 static int keep_subject
= 0;
1443 static int keep_callback(const struct option
*opt
, const char *arg
, int unset
)
1445 BUG_ON_OPT_NEG(unset
);
1446 BUG_ON_OPT_ARG(arg
);
1447 ((struct rev_info
*)opt
->value
)->total
= -1;
1452 static int subject_prefix
= 0;
1454 static int subject_prefix_callback(const struct option
*opt
, const char *arg
,
1457 BUG_ON_OPT_NEG(unset
);
1459 ((struct rev_info
*)opt
->value
)->subject_prefix
= arg
;
1463 static int rfc_callback(const struct option
*opt
, const char *arg
, int unset
)
1465 BUG_ON_OPT_NEG(unset
);
1466 BUG_ON_OPT_ARG(arg
);
1467 return subject_prefix_callback(opt
, "RFC PATCH", unset
);
1470 static int numbered_cmdline_opt
= 0;
1472 static int numbered_callback(const struct option
*opt
, const char *arg
,
1475 BUG_ON_OPT_ARG(arg
);
1476 *(int *)opt
->value
= numbered_cmdline_opt
= unset
? 0 : 1;
1482 static int no_numbered_callback(const struct option
*opt
, const char *arg
,
1485 BUG_ON_OPT_NEG(unset
);
1486 return numbered_callback(opt
, arg
, 1);
1489 static int output_directory_callback(const struct option
*opt
, const char *arg
,
1492 const char **dir
= (const char **)opt
->value
;
1493 BUG_ON_OPT_NEG(unset
);
1495 die(_("two output directories?"));
1500 static int thread_callback(const struct option
*opt
, const char *arg
, int unset
)
1502 enum thread_level
*thread
= (enum thread_level
*)opt
->value
;
1504 *thread
= THREAD_UNSET
;
1505 else if (!arg
|| !strcmp(arg
, "shallow"))
1506 *thread
= THREAD_SHALLOW
;
1507 else if (!strcmp(arg
, "deep"))
1508 *thread
= THREAD_DEEP
;
1510 * Please update _git_formatpatch() in git-completion.bash
1511 * when you add new options.
1518 static int attach_callback(const struct option
*opt
, const char *arg
, int unset
)
1520 struct rev_info
*rev
= (struct rev_info
*)opt
->value
;
1522 rev
->mime_boundary
= NULL
;
1524 rev
->mime_boundary
= arg
;
1526 rev
->mime_boundary
= git_version_string
;
1527 rev
->no_inline
= unset
? 0 : 1;
1531 static int inline_callback(const struct option
*opt
, const char *arg
, int unset
)
1533 struct rev_info
*rev
= (struct rev_info
*)opt
->value
;
1535 rev
->mime_boundary
= NULL
;
1537 rev
->mime_boundary
= arg
;
1539 rev
->mime_boundary
= git_version_string
;
1544 static int header_callback(const struct option
*opt
, const char *arg
, int unset
)
1547 string_list_clear(&extra_hdr
, 0);
1548 string_list_clear(&extra_to
, 0);
1549 string_list_clear(&extra_cc
, 0);
1556 static int to_callback(const struct option
*opt
, const char *arg
, int unset
)
1559 string_list_clear(&extra_to
, 0);
1561 string_list_append(&extra_to
, arg
);
1565 static int cc_callback(const struct option
*opt
, const char *arg
, int unset
)
1568 string_list_clear(&extra_cc
, 0);
1570 string_list_append(&extra_cc
, arg
);
1574 static int from_callback(const struct option
*opt
, const char *arg
, int unset
)
1576 char **from
= opt
->value
;
1583 *from
= xstrdup(arg
);
1585 *from
= xstrdup(git_committer_info(IDENT_NO_DATE
));
1589 static int base_callback(const struct option
*opt
, const char *arg
, int unset
)
1591 const char **base_commit
= opt
->value
;
1594 auto_base
= AUTO_BASE_NEVER
;
1595 *base_commit
= NULL
;
1596 } else if (!strcmp(arg
, "auto")) {
1597 auto_base
= AUTO_BASE_ALWAYS
;
1598 *base_commit
= NULL
;
1600 auto_base
= AUTO_BASE_NEVER
;
1606 struct base_tree_info
{
1607 struct object_id base_commit
;
1608 int nr_patch_id
, alloc_patch_id
;
1609 struct object_id
*patch_id
;
1612 static struct commit
*get_base_commit(const char *base_commit
,
1613 struct commit
**list
,
1616 struct commit
*base
= NULL
;
1617 struct commit
**rev
;
1618 int i
= 0, rev_nr
= 0, auto_select
, die_on_failure
;
1620 switch (auto_base
) {
1621 case AUTO_BASE_NEVER
:
1626 /* no base information is requested */
1630 case AUTO_BASE_ALWAYS
:
1631 case AUTO_BASE_WHEN_ABLE
:
1633 BUG("requested automatic base selection but a commit was provided");
1636 die_on_failure
= auto_base
== AUTO_BASE_ALWAYS
;
1640 BUG("unexpected automatic base selection method");
1644 base
= lookup_commit_reference_by_name(base_commit
);
1646 die(_("unknown commit %s"), base_commit
);
1648 struct branch
*curr_branch
= branch_get(NULL
);
1649 const char *upstream
= branch_get_upstream(curr_branch
, NULL
);
1651 struct commit_list
*base_list
;
1652 struct commit
*commit
;
1653 struct object_id oid
;
1655 if (repo_get_oid(the_repository
, upstream
, &oid
)) {
1657 die(_("failed to resolve '%s' as a valid ref"), upstream
);
1661 commit
= lookup_commit_or_die(&oid
, "upstream base");
1662 base_list
= repo_get_merge_bases_many(the_repository
,
1665 /* There should be one and only one merge base. */
1666 if (!base_list
|| base_list
->next
) {
1667 if (die_on_failure
) {
1668 die(_("could not find exact merge base"));
1670 free_commit_list(base_list
);
1674 base
= base_list
->item
;
1675 free_commit_list(base_list
);
1678 die(_("failed to get upstream, if you want to record base commit automatically,\n"
1679 "please use git branch --set-upstream-to to track a remote branch.\n"
1680 "Or you could specify base commit by --base=<base-commit-id> manually"));
1686 ALLOC_ARRAY(rev
, total
);
1687 for (i
= 0; i
< total
; i
++)
1692 * Get merge base through pair-wise computations
1693 * and store it in rev[0].
1695 while (rev_nr
> 1) {
1696 for (i
= 0; i
< rev_nr
/ 2; i
++) {
1697 struct commit_list
*merge_base
;
1698 merge_base
= repo_get_merge_bases(the_repository
,
1701 if (!merge_base
|| merge_base
->next
) {
1702 if (die_on_failure
) {
1703 die(_("failed to find exact merge base"));
1710 rev
[i
] = merge_base
->item
;
1714 rev
[i
] = rev
[2 * i
];
1715 rev_nr
= DIV_ROUND_UP(rev_nr
, 2);
1718 if (!repo_in_merge_bases(the_repository
, base
, rev
[0])) {
1719 if (die_on_failure
) {
1720 die(_("base commit should be the ancestor of revision list"));
1727 for (i
= 0; i
< total
; i
++) {
1728 if (base
== list
[i
]) {
1729 if (die_on_failure
) {
1730 die(_("base commit shouldn't be in revision list"));
1742 define_commit_slab(commit_base
, int);
1744 static void prepare_bases(struct base_tree_info
*bases
,
1745 struct commit
*base
,
1746 struct commit
**list
,
1749 struct commit
*commit
;
1750 struct rev_info revs
;
1751 struct diff_options diffopt
;
1752 struct commit_base commit_base
;
1758 init_commit_base(&commit_base
);
1759 repo_diff_setup(the_repository
, &diffopt
);
1760 diffopt
.flags
.recursive
= 1;
1761 diff_setup_done(&diffopt
);
1763 oidcpy(&bases
->base_commit
, &base
->object
.oid
);
1765 repo_init_revisions(the_repository
, &revs
, NULL
);
1766 revs
.max_parents
= 1;
1767 revs
.topo_order
= 1;
1768 for (i
= 0; i
< total
; i
++) {
1769 list
[i
]->object
.flags
&= ~UNINTERESTING
;
1770 add_pending_object(&revs
, &list
[i
]->object
, "rev_list");
1771 *commit_base_at(&commit_base
, list
[i
]) = 1;
1773 base
->object
.flags
|= UNINTERESTING
;
1774 add_pending_object(&revs
, &base
->object
, "base");
1776 if (prepare_revision_walk(&revs
))
1777 die(_("revision walk setup failed"));
1779 * Traverse the commits list, get prerequisite patch ids
1780 * and stuff them in bases structure.
1782 while ((commit
= get_revision(&revs
)) != NULL
) {
1783 struct object_id oid
;
1784 struct object_id
*patch_id
;
1785 if (*commit_base_at(&commit_base
, commit
))
1787 if (commit_patch_id(commit
, &diffopt
, &oid
, 0))
1788 die(_("cannot get patch id"));
1789 ALLOC_GROW(bases
->patch_id
, bases
->nr_patch_id
+ 1, bases
->alloc_patch_id
);
1790 patch_id
= bases
->patch_id
+ bases
->nr_patch_id
;
1791 oidcpy(patch_id
, &oid
);
1792 bases
->nr_patch_id
++;
1794 clear_commit_base(&commit_base
);
1797 static void print_bases(struct base_tree_info
*bases
, FILE *file
)
1801 /* Only do this once, either for the cover or for the first one */
1802 if (is_null_oid(&bases
->base_commit
))
1805 /* Show the base commit */
1806 fprintf(file
, "\nbase-commit: %s\n", oid_to_hex(&bases
->base_commit
));
1808 /* Show the prerequisite patches */
1809 for (i
= bases
->nr_patch_id
- 1; i
>= 0; i
--)
1810 fprintf(file
, "prerequisite-patch-id: %s\n", oid_to_hex(&bases
->patch_id
[i
]));
1812 free(bases
->patch_id
);
1813 bases
->nr_patch_id
= 0;
1814 bases
->alloc_patch_id
= 0;
1815 oidclr(&bases
->base_commit
);
1818 static const char *diff_title(struct strbuf
*sb
,
1819 const char *reroll_count
,
1820 const char *generic
,
1821 const char *rerolled
)
1825 /* RFC may be v0, so allow -v1 to diff against v0 */
1826 if (reroll_count
&& !strtol_i(reroll_count
, 10, &v
) &&
1828 strbuf_addf(sb
, rerolled
, v
- 1);
1830 strbuf_addstr(sb
, generic
);
1834 static void infer_range_diff_ranges(struct strbuf
*r1
,
1837 struct commit
*origin
,
1838 struct commit
*head
)
1840 const char *head_oid
= oid_to_hex(&head
->object
.oid
);
1841 int prev_is_range
= is_range_diff_range(prev
);
1844 strbuf_addstr(r1
, prev
);
1846 strbuf_addf(r1
, "%s..%s", head_oid
, prev
);
1849 strbuf_addf(r2
, "%s..%s", oid_to_hex(&origin
->object
.oid
), head_oid
);
1850 else if (prev_is_range
)
1851 die(_("failed to infer range-diff origin of current series"));
1853 warning(_("using '%s' as range-diff origin of current series"), prev
);
1854 strbuf_addf(r2
, "%s..%s", prev
, head_oid
);
1858 int cmd_format_patch(int argc
, const char **argv
, const char *prefix
)
1860 struct commit
*commit
;
1861 struct commit
**list
= NULL
;
1862 struct rev_info rev
;
1863 char *to_free
= NULL
;
1864 struct setup_revision_opt s_r_opt
;
1865 int nr
= 0, total
, i
;
1867 int start_number
= -1;
1868 int just_numbers
= 0;
1869 int ignore_if_in_upstream
= 0;
1870 int cover_letter
= -1;
1871 int boundary_count
= 0;
1872 int no_binary_diff
= 0;
1873 int zero_commit
= 0;
1874 struct commit
*origin
= NULL
;
1875 const char *in_reply_to
= NULL
;
1876 struct patch_ids ids
;
1877 struct strbuf buf
= STRBUF_INIT
;
1878 int use_patch_format
= 0;
1880 const char *reroll_count
= NULL
;
1881 char *cover_from_description_arg
= NULL
;
1882 char *branch_name
= NULL
;
1883 char *base_commit
= NULL
;
1884 struct base_tree_info bases
;
1885 struct commit
*base
;
1886 int show_progress
= 0;
1887 struct progress
*progress
= NULL
;
1888 struct oid_array idiff_prev
= OID_ARRAY_INIT
;
1889 struct strbuf idiff_title
= STRBUF_INIT
;
1890 const char *rdiff_prev
= NULL
;
1891 struct strbuf rdiff1
= STRBUF_INIT
;
1892 struct strbuf rdiff2
= STRBUF_INIT
;
1893 struct strbuf rdiff_title
= STRBUF_INIT
;
1894 struct strbuf sprefix
= STRBUF_INIT
;
1895 int creation_factor
= -1;
1897 const struct option builtin_format_patch_options
[] = {
1898 OPT_CALLBACK_F('n', "numbered", &numbered
, NULL
,
1899 N_("use [PATCH n/m] even with a single patch"),
1900 PARSE_OPT_NOARG
, numbered_callback
),
1901 OPT_CALLBACK_F('N', "no-numbered", &numbered
, NULL
,
1902 N_("use [PATCH] even with multiple patches"),
1903 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
, no_numbered_callback
),
1904 OPT_BOOL('s', "signoff", &do_signoff
, N_("add a Signed-off-by trailer")),
1905 OPT_BOOL(0, "stdout", &use_stdout
,
1906 N_("print patches to standard out")),
1907 OPT_BOOL(0, "cover-letter", &cover_letter
,
1908 N_("generate a cover letter")),
1909 OPT_BOOL(0, "numbered-files", &just_numbers
,
1910 N_("use simple number sequence for output file names")),
1911 OPT_STRING(0, "suffix", &fmt_patch_suffix
, N_("sfx"),
1912 N_("use <sfx> instead of '.patch'")),
1913 OPT_INTEGER(0, "start-number", &start_number
,
1914 N_("start numbering patches at <n> instead of 1")),
1915 OPT_STRING('v', "reroll-count", &reroll_count
, N_("reroll-count"),
1916 N_("mark the series as Nth re-roll")),
1917 OPT_INTEGER(0, "filename-max-length", &fmt_patch_name_max
,
1918 N_("max length of output filename")),
1919 OPT_CALLBACK_F(0, "rfc", &rev
, NULL
,
1920 N_("use [RFC PATCH] instead of [PATCH]"),
1921 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
, rfc_callback
),
1922 OPT_STRING(0, "cover-from-description", &cover_from_description_arg
,
1923 N_("cover-from-description-mode"),
1924 N_("generate parts of a cover letter based on a branch's description")),
1925 OPT_CALLBACK_F(0, "subject-prefix", &rev
, N_("prefix"),
1926 N_("use [<prefix>] instead of [PATCH]"),
1927 PARSE_OPT_NONEG
, subject_prefix_callback
),
1928 OPT_CALLBACK_F('o', "output-directory", &output_directory
,
1929 N_("dir"), N_("store resulting files in <dir>"),
1930 PARSE_OPT_NONEG
, output_directory_callback
),
1931 OPT_CALLBACK_F('k', "keep-subject", &rev
, NULL
,
1932 N_("don't strip/add [PATCH]"),
1933 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
, keep_callback
),
1934 OPT_BOOL(0, "no-binary", &no_binary_diff
,
1935 N_("don't output binary diffs")),
1936 OPT_BOOL(0, "zero-commit", &zero_commit
,
1937 N_("output all-zero hash in From header")),
1938 OPT_BOOL(0, "ignore-if-in-upstream", &ignore_if_in_upstream
,
1939 N_("don't include a patch matching a commit upstream")),
1940 OPT_SET_INT_F('p', "no-stat", &use_patch_format
,
1941 N_("show patch format instead of default (patch + stat)"),
1942 1, PARSE_OPT_NONEG
),
1943 OPT_GROUP(N_("Messaging")),
1944 OPT_CALLBACK(0, "add-header", NULL
, N_("header"),
1945 N_("add email header"), header_callback
),
1946 OPT_CALLBACK(0, "to", NULL
, N_("email"), N_("add To: header"), to_callback
),
1947 OPT_CALLBACK(0, "cc", NULL
, N_("email"), N_("add Cc: header"), cc_callback
),
1948 OPT_CALLBACK_F(0, "from", &from
, N_("ident"),
1949 N_("set From address to <ident> (or committer ident if absent)"),
1950 PARSE_OPT_OPTARG
, from_callback
),
1951 OPT_STRING(0, "in-reply-to", &in_reply_to
, N_("message-id"),
1952 N_("make first mail a reply to <message-id>")),
1953 OPT_CALLBACK_F(0, "attach", &rev
, N_("boundary"),
1954 N_("attach the patch"), PARSE_OPT_OPTARG
,
1956 OPT_CALLBACK_F(0, "inline", &rev
, N_("boundary"),
1957 N_("inline the patch"),
1958 PARSE_OPT_OPTARG
| PARSE_OPT_NONEG
,
1960 OPT_CALLBACK_F(0, "thread", &thread
, N_("style"),
1961 N_("enable message threading, styles: shallow, deep"),
1962 PARSE_OPT_OPTARG
, thread_callback
),
1963 OPT_STRING(0, "signature", &signature
, N_("signature"),
1964 N_("add a signature")),
1965 OPT_CALLBACK_F(0, "base", &base_commit
, N_("base-commit"),
1966 N_("add prerequisite tree info to the patch series"),
1968 OPT_FILENAME(0, "signature-file", &signature_file
,
1969 N_("add a signature from a file")),
1970 OPT__QUIET(&quiet
, N_("don't print the patch filenames")),
1971 OPT_BOOL(0, "progress", &show_progress
,
1972 N_("show progress while generating patches")),
1973 OPT_CALLBACK(0, "interdiff", &idiff_prev
, N_("rev"),
1974 N_("show changes against <rev> in cover letter or single patch"),
1975 parse_opt_object_name
),
1976 OPT_STRING(0, "range-diff", &rdiff_prev
, N_("refspec"),
1977 N_("show changes against <refspec> in cover letter or single patch")),
1978 OPT_INTEGER(0, "creation-factor", &creation_factor
,
1979 N_("percentage by which creation is weighted")),
1980 OPT_BOOL(0, "force-in-body-from", &force_in_body_from
,
1981 N_("show in-body From: even if identical to the e-mail header")),
1985 extra_hdr
.strdup_strings
= 1;
1986 extra_to
.strdup_strings
= 1;
1987 extra_cc
.strdup_strings
= 1;
1989 init_log_defaults();
1990 init_display_notes(¬es_opt
);
1991 git_config(git_format_config
, NULL
);
1992 repo_init_revisions(the_repository
, &rev
, prefix
);
1993 git_config(grep_config
, &rev
.grep_filter
);
1995 rev
.show_notes
= show_notes
;
1996 memcpy(&rev
.notes_opt
, ¬es_opt
, sizeof(notes_opt
));
1997 rev
.commit_format
= CMIT_FMT_EMAIL
;
1998 rev
.encode_email_headers
= default_encode_email_headers
;
1999 rev
.expand_tabs_in_log_default
= 0;
2000 rev
.verbose_header
= 1;
2002 rev
.max_parents
= 1;
2003 rev
.diffopt
.flags
.recursive
= 1;
2004 rev
.diffopt
.no_free
= 1;
2005 rev
.subject_prefix
= fmt_patch_subject_prefix
;
2006 memset(&s_r_opt
, 0, sizeof(s_r_opt
));
2007 s_r_opt
.def
= "HEAD";
2008 s_r_opt
.revarg_opt
= REVARG_COMMITTISH
;
2010 if (default_attach
) {
2011 rev
.mime_boundary
= default_attach
;
2016 * Parse the arguments before setup_revisions(), or something
2017 * like "git format-patch -o a123 HEAD^.." may fail; a123 is
2018 * possibly a valid SHA1.
2020 argc
= parse_options(argc
, argv
, prefix
, builtin_format_patch_options
,
2021 builtin_format_patch_usage
,
2022 PARSE_OPT_KEEP_ARGV0
| PARSE_OPT_KEEP_UNKNOWN_OPT
|
2023 PARSE_OPT_KEEP_DASHDASH
);
2025 rev
.force_in_body_from
= force_in_body_from
;
2027 /* Make sure "0000-$sub.patch" gives non-negative length for $sub */
2028 if (fmt_patch_name_max
<= strlen("0000-") + strlen(fmt_patch_suffix
))
2029 fmt_patch_name_max
= strlen("0000-") + strlen(fmt_patch_suffix
);
2031 if (cover_from_description_arg
)
2032 cover_from_description_mode
= parse_cover_from_description(cover_from_description_arg
);
2035 strbuf_addf(&sprefix
, "%s v%s",
2036 rev
.subject_prefix
, reroll_count
);
2037 rev
.reroll_count
= reroll_count
;
2038 rev
.subject_prefix
= sprefix
.buf
;
2041 for (i
= 0; i
< extra_hdr
.nr
; i
++) {
2042 strbuf_addstr(&buf
, extra_hdr
.items
[i
].string
);
2043 strbuf_addch(&buf
, '\n');
2047 strbuf_addstr(&buf
, "To: ");
2048 for (i
= 0; i
< extra_to
.nr
; i
++) {
2050 strbuf_addstr(&buf
, " ");
2051 strbuf_addstr(&buf
, extra_to
.items
[i
].string
);
2052 if (i
+ 1 < extra_to
.nr
)
2053 strbuf_addch(&buf
, ',');
2054 strbuf_addch(&buf
, '\n');
2058 strbuf_addstr(&buf
, "Cc: ");
2059 for (i
= 0; i
< extra_cc
.nr
; i
++) {
2061 strbuf_addstr(&buf
, " ");
2062 strbuf_addstr(&buf
, extra_cc
.items
[i
].string
);
2063 if (i
+ 1 < extra_cc
.nr
)
2064 strbuf_addch(&buf
, ',');
2065 strbuf_addch(&buf
, '\n');
2068 rev
.extra_headers
= to_free
= strbuf_detach(&buf
, NULL
);
2071 if (split_ident_line(&rev
.from_ident
, from
, strlen(from
)))
2072 die(_("invalid ident line: %s"), from
);
2075 if (start_number
< 0)
2079 * If numbered is set solely due to format.numbered in config,
2080 * and it would conflict with --keep-subject (-k) from the
2081 * command line, reset "numbered".
2083 if (numbered
&& keep_subject
&& !numbered_cmdline_opt
)
2086 if (numbered
&& keep_subject
)
2087 die(_("options '%s' and '%s' cannot be used together"), "-n", "-k");
2088 if (keep_subject
&& subject_prefix
)
2089 die(_("options '%s' and '%s' cannot be used together"), "--subject-prefix/--rfc", "-k");
2090 rev
.preserve_subject
= keep_subject
;
2092 argc
= setup_revisions(argc
, argv
, &rev
, &s_r_opt
);
2094 die(_("unrecognized argument: %s"), argv
[1]);
2096 if (rev
.diffopt
.output_format
& DIFF_FORMAT_NAME
)
2097 die(_("--name-only does not make sense"));
2098 if (rev
.diffopt
.output_format
& DIFF_FORMAT_NAME_STATUS
)
2099 die(_("--name-status does not make sense"));
2100 if (rev
.diffopt
.output_format
& DIFF_FORMAT_CHECKDIFF
)
2101 die(_("--check does not make sense"));
2102 if (rev
.remerge_diff
)
2103 die(_("--remerge-diff does not make sense"));
2105 if (!use_patch_format
&&
2106 (!rev
.diffopt
.output_format
||
2107 rev
.diffopt
.output_format
== DIFF_FORMAT_PATCH
))
2108 rev
.diffopt
.output_format
= DIFF_FORMAT_DIFFSTAT
| DIFF_FORMAT_SUMMARY
;
2109 if (!rev
.diffopt
.stat_width
)
2110 rev
.diffopt
.stat_width
= MAIL_DEFAULT_WRAP
;
2112 /* Always generate a patch */
2113 rev
.diffopt
.output_format
|= DIFF_FORMAT_PATCH
;
2114 rev
.always_show_header
= 1;
2116 rev
.zero_commit
= zero_commit
;
2117 rev
.patch_name_max
= fmt_patch_name_max
;
2119 if (!rev
.diffopt
.flags
.text
&& !no_binary_diff
)
2120 rev
.diffopt
.flags
.binary
= 1;
2123 load_display_notes(&rev
.notes_opt
);
2125 die_for_incompatible_opt3(use_stdout
, "--stdout",
2126 rev
.diffopt
.close_file
, "--output",
2127 !!output_directory
, "--output-directory");
2129 if (use_stdout
&& stdout_mboxrd
)
2130 rev
.commit_format
= CMIT_FMT_MBOXRD
;
2134 } else if (!rev
.diffopt
.close_file
) {
2137 if (!output_directory
)
2138 output_directory
= config_output_directory
;
2139 output_directory
= set_outdir(prefix
, output_directory
);
2141 if (rev
.diffopt
.use_color
!= GIT_COLOR_ALWAYS
)
2142 rev
.diffopt
.use_color
= GIT_COLOR_NEVER
;
2144 * We consider <outdir> as 'outside of gitdir', therefore avoid
2145 * applying adjust_shared_perm in s-c-l-d.
2147 saved
= get_shared_repository();
2148 set_shared_repository(0);
2149 switch (safe_create_leading_directories_const(output_directory
)) {
2154 die(_("could not create leading directories "
2155 "of '%s'"), output_directory
);
2157 set_shared_repository(saved
);
2158 if (mkdir(output_directory
, 0777) < 0 && errno
!= EEXIST
)
2159 die_errno(_("could not create directory '%s'"),
2163 if (rev
.pending
.nr
== 1) {
2166 if (rev
.max_count
< 0 && !rev
.show_root_diff
) {
2168 * This is traditional behaviour of "git format-patch
2169 * origin" that prepares what the origin side still
2172 rev
.pending
.objects
[0].item
->flags
|= UNINTERESTING
;
2173 add_head_to_pending(&rev
);
2177 * Otherwise, it is "format-patch -22 HEAD", and/or
2178 * "format-patch --root HEAD". The user wants
2179 * get_revision() to do the usual traversal.
2182 if (!strcmp(rev
.pending
.objects
[0].name
, "HEAD"))
2186 const char *ref
, *v
;
2187 ref
= resolve_ref_unsafe("HEAD", RESOLVE_REF_READING
,
2189 if (ref
&& skip_prefix(ref
, "refs/heads/", &v
))
2190 branch_name
= xstrdup(v
);
2192 branch_name
= xstrdup(""); /* no branch */
2197 * We cannot move this anywhere earlier because we do want to
2198 * know if --root was given explicitly from the command line.
2200 rev
.show_root_diff
= 1;
2202 if (ignore_if_in_upstream
) {
2203 /* Don't say anything if head and upstream are the same. */
2204 if (rev
.pending
.nr
== 2) {
2205 struct object_array_entry
*o
= rev
.pending
.objects
;
2206 if (oideq(&o
[0].item
->oid
, &o
[1].item
->oid
))
2209 get_patch_ids(&rev
, &ids
);
2212 if (prepare_revision_walk(&rev
))
2213 die(_("revision walk setup failed"));
2215 while ((commit
= get_revision(&rev
)) != NULL
) {
2216 if (commit
->object
.flags
& BOUNDARY
) {
2218 origin
= (boundary_count
== 1) ? commit
: NULL
;
2222 if (ignore_if_in_upstream
&& has_commit_patch_id(commit
, &ids
))
2226 REALLOC_ARRAY(list
, nr
);
2227 list
[nr
- 1] = commit
;
2233 if (cover_letter
== -1) {
2234 if (config_cover_letter
== COVER_AUTO
)
2235 cover_letter
= (total
> 1);
2237 cover_letter
= (config_cover_letter
== COVER_ON
);
2239 if (!keep_subject
&& auto_number
&& (total
> 1 || cover_letter
))
2242 rev
.total
= total
+ start_number
- 1;
2244 if (idiff_prev
.nr
) {
2245 if (!cover_letter
&& total
!= 1)
2246 die(_("--interdiff requires --cover-letter or single patch"));
2247 rev
.idiff_oid1
= &idiff_prev
.oid
[idiff_prev
.nr
- 1];
2248 rev
.idiff_oid2
= get_commit_tree_oid(list
[0]);
2249 rev
.idiff_title
= diff_title(&idiff_title
, reroll_count
,
2251 _("Interdiff against v%d:"));
2254 if (creation_factor
< 0)
2255 creation_factor
= RANGE_DIFF_CREATION_FACTOR_DEFAULT
;
2256 else if (!rdiff_prev
)
2257 die(_("the option '%s' requires '%s'"), "--creation-factor", "--range-diff");
2260 if (!cover_letter
&& total
!= 1)
2261 die(_("--range-diff requires --cover-letter or single patch"));
2263 infer_range_diff_ranges(&rdiff1
, &rdiff2
, rdiff_prev
,
2265 rev
.rdiff1
= rdiff1
.buf
;
2266 rev
.rdiff2
= rdiff2
.buf
;
2267 rev
.creation_factor
= creation_factor
;
2268 rev
.rdiff_title
= diff_title(&rdiff_title
, reroll_count
,
2270 _("Range-diff against v%d:"));
2274 ; /* --no-signature inhibits all signatures */
2275 } else if (signature
&& signature
!= git_version_string
) {
2276 ; /* non-default signature already set */
2277 } else if (signature_file
) {
2278 struct strbuf buf
= STRBUF_INIT
;
2280 if (strbuf_read_file(&buf
, signature_file
, 128) < 0)
2281 die_errno(_("unable to read signature file '%s'"), signature_file
);
2282 signature
= strbuf_detach(&buf
, NULL
);
2285 memset(&bases
, 0, sizeof(bases
));
2286 base
= get_base_commit(base_commit
, list
, nr
);
2288 reset_revision_walk();
2289 clear_object_flags(UNINTERESTING
);
2290 prepare_bases(&bases
, base
, list
, nr
);
2293 if (in_reply_to
|| thread
|| cover_letter
) {
2294 rev
.ref_message_ids
= xmalloc(sizeof(*rev
.ref_message_ids
));
2295 string_list_init_nodup(rev
.ref_message_ids
);
2298 const char *msgid
= clean_message_id(in_reply_to
);
2299 string_list_append(rev
.ref_message_ids
, msgid
);
2301 rev
.numbered_files
= just_numbers
;
2302 rev
.patch_suffix
= fmt_patch_suffix
;
2305 gen_message_id(&rev
, "cover");
2306 make_cover_letter(&rev
, !!output_directory
,
2307 origin
, nr
, list
, branch_name
, quiet
);
2308 print_bases(&bases
, rev
.diffopt
.file
);
2309 print_signature(rev
.diffopt
.file
);
2312 /* interdiff/range-diff in cover-letter; omit from patches */
2313 rev
.idiff_oid1
= NULL
;
2316 rev
.add_signoff
= do_signoff
;
2319 progress
= start_delayed_progress(_("Generating patches"), total
);
2322 display_progress(progress
, total
- nr
);
2324 rev
.nr
= total
- nr
+ (start_number
- 1);
2325 /* Make the second and subsequent mails replies to the first */
2327 /* Have we already had a message ID? */
2328 if (rev
.message_id
) {
2330 * For deep threading: make every mail
2331 * a reply to the previous one, no
2332 * matter what other options are set.
2334 * For shallow threading:
2336 * Without --cover-letter and
2337 * --in-reply-to, make every mail a
2338 * reply to the one before.
2340 * With --in-reply-to but no
2341 * --cover-letter, make every mail a
2342 * reply to the <reply-to>.
2344 * With --cover-letter, make every
2345 * mail but the cover letter a reply
2346 * to the cover letter. The cover
2347 * letter is a reply to the
2348 * --in-reply-to, if specified.
2350 if (thread
== THREAD_SHALLOW
2351 && rev
.ref_message_ids
->nr
> 0
2352 && (!cover_letter
|| rev
.nr
> 1))
2353 free(rev
.message_id
);
2355 string_list_append(rev
.ref_message_ids
,
2358 gen_message_id(&rev
, oid_to_hex(&commit
->object
.oid
));
2361 if (output_directory
&&
2362 open_next_file(rev
.numbered_files
? NULL
: commit
, NULL
, &rev
, quiet
))
2363 die(_("failed to create output files"));
2364 shown
= log_tree_commit(&rev
, commit
);
2365 free_commit_buffer(the_repository
->parsed_objects
,
2368 /* We put one extra blank line between formatted
2369 * patches and this flag is used by log-tree code
2370 * to see if it needs to emit a LF before showing
2371 * the log; when using one file per patch, we do
2372 * not want the extra blank line.
2374 if (output_directory
)
2377 print_bases(&bases
, rev
.diffopt
.file
);
2378 if (rev
.mime_boundary
)
2379 fprintf(rev
.diffopt
.file
, "\n--%s%s--\n\n\n",
2380 mime_boundary_leader
,
2383 print_signature(rev
.diffopt
.file
);
2385 if (output_directory
)
2386 fclose(rev
.diffopt
.file
);
2388 stop_progress(&progress
);
2391 string_list_clear(&extra_to
, 0);
2392 string_list_clear(&extra_cc
, 0);
2393 string_list_clear(&extra_hdr
, 0);
2394 if (ignore_if_in_upstream
)
2395 free_patch_ids(&ids
);
2398 oid_array_clear(&idiff_prev
);
2399 strbuf_release(&idiff_title
);
2400 strbuf_release(&rdiff1
);
2401 strbuf_release(&rdiff2
);
2402 strbuf_release(&rdiff_title
);
2403 strbuf_release(&sprefix
);
2405 if (rev
.ref_message_ids
)
2406 string_list_clear(rev
.ref_message_ids
, 0);
2407 free(rev
.ref_message_ids
);
2408 return cmd_log_deinit(0, &rev
);
2411 static int add_pending_commit(const char *arg
, struct rev_info
*revs
, int flags
)
2413 struct object_id oid
;
2414 if (repo_get_oid(the_repository
, arg
, &oid
) == 0) {
2415 struct commit
*commit
= lookup_commit_reference(the_repository
,
2418 commit
->object
.flags
|= flags
;
2419 add_pending_object(revs
, &commit
->object
, arg
);
2426 static const char * const cherry_usage
[] = {
2427 N_("git cherry [-v] [<upstream> [<head> [<limit>]]]"),
2431 static void print_commit(char sign
, struct commit
*commit
, int verbose
,
2432 int abbrev
, FILE *file
)
2435 fprintf(file
, "%c %s\n", sign
,
2436 repo_find_unique_abbrev(the_repository
, &commit
->object
.oid
, abbrev
));
2438 struct strbuf buf
= STRBUF_INIT
;
2439 pp_commit_easy(CMIT_FMT_ONELINE
, commit
, &buf
);
2440 fprintf(file
, "%c %s %s\n", sign
,
2441 repo_find_unique_abbrev(the_repository
, &commit
->object
.oid
, abbrev
),
2443 strbuf_release(&buf
);
2447 int cmd_cherry(int argc
, const char **argv
, const char *prefix
)
2449 struct rev_info revs
;
2450 struct patch_ids ids
;
2451 struct commit
*commit
;
2452 struct commit_list
*list
= NULL
;
2453 struct branch
*current_branch
;
2454 const char *upstream
;
2455 const char *head
= "HEAD";
2456 const char *limit
= NULL
;
2457 int verbose
= 0, abbrev
= 0;
2459 struct option options
[] = {
2460 OPT__ABBREV(&abbrev
),
2461 OPT__VERBOSE(&verbose
, N_("be verbose")),
2465 argc
= parse_options(argc
, argv
, prefix
, options
, cherry_usage
, 0);
2478 current_branch
= branch_get(NULL
);
2479 upstream
= branch_get_upstream(current_branch
, NULL
);
2481 fprintf(stderr
, _("Could not find a tracked"
2482 " remote branch, please"
2483 " specify <upstream> manually.\n"));
2484 usage_with_options(cherry_usage
, options
);
2488 repo_init_revisions(the_repository
, &revs
, prefix
);
2489 revs
.max_parents
= 1;
2491 if (add_pending_commit(head
, &revs
, 0))
2492 die(_("unknown commit %s"), head
);
2493 if (add_pending_commit(upstream
, &revs
, UNINTERESTING
))
2494 die(_("unknown commit %s"), upstream
);
2496 /* Don't say anything if head and upstream are the same. */
2497 if (revs
.pending
.nr
== 2) {
2498 struct object_array_entry
*o
= revs
.pending
.objects
;
2499 if (oideq(&o
[0].item
->oid
, &o
[1].item
->oid
))
2503 get_patch_ids(&revs
, &ids
);
2505 if (limit
&& add_pending_commit(limit
, &revs
, UNINTERESTING
))
2506 die(_("unknown commit %s"), limit
);
2508 /* reverse the list of commits */
2509 if (prepare_revision_walk(&revs
))
2510 die(_("revision walk setup failed"));
2511 while ((commit
= get_revision(&revs
)) != NULL
) {
2512 commit_list_insert(commit
, &list
);
2518 commit
= list
->item
;
2519 if (has_commit_patch_id(commit
, &ids
))
2521 print_commit(sign
, commit
, verbose
, abbrev
, revs
.diffopt
.file
);
2525 free_patch_ids(&ids
);