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"
48 #include "write-or-die.h"
50 #define MAIL_DEFAULT_WRAP 72
51 #define COVER_FROM_AUTO_MAX_SUBJECT_LEN 100
52 #define FORMAT_PATCH_NAME_MAX_DEFAULT 64
54 /* Set a default date-time format for git log ("log.date" config variable) */
55 static const char *default_date_mode
= NULL
;
57 static int default_abbrev_commit
;
58 static int default_show_root
= 1;
59 static int default_follow
;
60 static int default_show_signature
;
61 static int default_encode_email_headers
= 1;
62 static int decoration_style
;
63 static int decoration_given
;
64 static int use_mailmap_config
= 1;
65 static unsigned int force_in_body_from
;
66 static int stdout_mboxrd
;
67 static const char *fmt_patch_subject_prefix
= "PATCH";
68 static int fmt_patch_name_max
= FORMAT_PATCH_NAME_MAX_DEFAULT
;
69 static const char *fmt_pretty
;
70 static int format_no_prefix
;
72 static const char * const builtin_log_usage
[] = {
73 N_("git log [<options>] [<revision-range>] [[--] <path>...]"),
74 N_("git show [<options>] <object>..."),
78 struct line_opt_callback_data
{
81 struct string_list args
;
84 static int session_is_interactive(void)
86 return isatty(1) || pager_in_use();
89 static int auto_decoration_style(void)
91 return session_is_interactive() ? DECORATE_SHORT_REFS
: 0;
94 static int parse_decoration_style(const char *value
)
96 switch (git_parse_maybe_bool(value
)) {
98 return DECORATE_SHORT_REFS
;
104 if (!strcmp(value
, "full"))
105 return DECORATE_FULL_REFS
;
106 else if (!strcmp(value
, "short"))
107 return DECORATE_SHORT_REFS
;
108 else if (!strcmp(value
, "auto"))
109 return auto_decoration_style();
111 * Please update _git_log() in git-completion.bash when you
112 * add new decoration styles.
117 static int use_default_decoration_filter
= 1;
118 static struct string_list decorate_refs_exclude
= STRING_LIST_INIT_NODUP
;
119 static struct string_list decorate_refs_exclude_config
= STRING_LIST_INIT_NODUP
;
120 static struct string_list decorate_refs_include
= STRING_LIST_INIT_NODUP
;
122 static int clear_decorations_callback(const struct option
*opt
,
123 const char *arg
, int unset
)
125 string_list_clear(&decorate_refs_include
, 0);
126 string_list_clear(&decorate_refs_exclude
, 0);
127 use_default_decoration_filter
= 0;
131 static int decorate_callback(const struct option
*opt
, const char *arg
, int unset
)
134 decoration_style
= 0;
136 decoration_style
= parse_decoration_style(arg
);
138 decoration_style
= DECORATE_SHORT_REFS
;
140 if (decoration_style
< 0)
141 die(_("invalid --decorate option: %s"), arg
);
143 decoration_given
= 1;
148 static int log_line_range_callback(const struct option
*option
, const char *arg
, int unset
)
150 struct line_opt_callback_data
*data
= option
->value
;
152 BUG_ON_OPT_NEG(unset
);
157 data
->rev
->line_level_traverse
= 1;
158 string_list_append(&data
->args
, arg
);
163 static void init_log_defaults(void)
165 init_diff_ui_defaults();
167 decoration_style
= auto_decoration_style();
170 static void cmd_log_init_defaults(struct rev_info
*rev
)
173 get_commit_format(fmt_pretty
, rev
);
175 rev
->diffopt
.flags
.default_follow_renames
= 1;
176 rev
->verbose_header
= 1;
177 rev
->diffopt
.flags
.recursive
= 1;
178 rev
->diffopt
.stat_width
= -1; /* use full terminal width */
179 rev
->diffopt
.stat_graph_width
= -1; /* respect statGraphWidth config */
180 rev
->abbrev_commit
= default_abbrev_commit
;
181 rev
->show_root_diff
= default_show_root
;
182 rev
->subject_prefix
= fmt_patch_subject_prefix
;
183 rev
->patch_name_max
= fmt_patch_name_max
;
184 rev
->show_signature
= default_show_signature
;
185 rev
->encode_email_headers
= default_encode_email_headers
;
186 rev
->diffopt
.flags
.allow_textconv
= 1;
188 if (default_date_mode
)
189 parse_date_format(default_date_mode
, &rev
->date_mode
);
192 static void set_default_decoration_filter(struct decoration_filter
*decoration_filter
)
196 struct string_list
*include
= decoration_filter
->include_ref_pattern
;
197 const struct string_list
*config_exclude
;
199 if (!git_config_get_string_multi("log.excludeDecoration",
201 struct string_list_item
*item
;
202 for_each_string_list_item(item
, config_exclude
)
203 string_list_append(decoration_filter
->exclude_ref_config_pattern
,
208 * By default, decorate_all is disabled. Enable it if
209 * log.initialDecorationSet=all. Don't ever disable it by config,
210 * since the command-line takes precedent.
212 if (use_default_decoration_filter
&&
213 !git_config_get_string("log.initialdecorationset", &value
) &&
214 !strcmp("all", value
))
215 use_default_decoration_filter
= 0;
218 if (!use_default_decoration_filter
||
219 decoration_filter
->exclude_ref_pattern
->nr
||
220 decoration_filter
->include_ref_pattern
->nr
||
221 decoration_filter
->exclude_ref_config_pattern
->nr
)
225 * No command-line or config options were given, so
226 * populate with sensible defaults.
228 for (i
= 0; i
< ARRAY_SIZE(ref_namespace
); i
++) {
229 if (!ref_namespace
[i
].decoration
)
232 string_list_append(include
, ref_namespace
[i
].ref
);
236 static void cmd_log_init_finish(int argc
, const char **argv
, const char *prefix
,
237 struct rev_info
*rev
, struct setup_revision_opt
*opt
)
239 struct userformat_want w
;
240 int quiet
= 0, source
= 0, mailmap
;
241 static struct line_opt_callback_data line_cb
= {NULL
, NULL
, STRING_LIST_INIT_DUP
};
242 struct decoration_filter decoration_filter
= {
243 .exclude_ref_pattern
= &decorate_refs_exclude
,
244 .include_ref_pattern
= &decorate_refs_include
,
245 .exclude_ref_config_pattern
= &decorate_refs_exclude_config
,
247 static struct revision_sources revision_sources
;
249 const struct option builtin_log_options
[] = {
250 OPT__QUIET(&quiet
, N_("suppress diff output")),
251 OPT_BOOL(0, "source", &source
, N_("show source")),
252 OPT_BOOL(0, "use-mailmap", &mailmap
, N_("use mail map file")),
253 OPT_ALIAS(0, "mailmap", "use-mailmap"),
254 OPT_CALLBACK_F(0, "clear-decorations", NULL
, NULL
,
255 N_("clear all previously-defined decoration filters"),
256 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
,
257 clear_decorations_callback
),
258 OPT_STRING_LIST(0, "decorate-refs", &decorate_refs_include
,
259 N_("pattern"), N_("only decorate refs that match <pattern>")),
260 OPT_STRING_LIST(0, "decorate-refs-exclude", &decorate_refs_exclude
,
261 N_("pattern"), N_("do not decorate refs that match <pattern>")),
262 OPT_CALLBACK_F(0, "decorate", NULL
, NULL
, N_("decorate options"),
263 PARSE_OPT_OPTARG
, decorate_callback
),
264 OPT_CALLBACK('L', NULL
, &line_cb
, "range:file",
265 N_("trace the evolution of line range <start>,<end> or function :<funcname> in <file>"),
266 log_line_range_callback
),
271 line_cb
.prefix
= prefix
;
273 mailmap
= use_mailmap_config
;
274 argc
= parse_options(argc
, argv
, prefix
,
275 builtin_log_options
, builtin_log_usage
,
276 PARSE_OPT_KEEP_ARGV0
| PARSE_OPT_KEEP_UNKNOWN_OPT
|
277 PARSE_OPT_KEEP_DASHDASH
);
280 rev
->diffopt
.output_format
|= DIFF_FORMAT_NO_OUTPUT
;
281 argc
= setup_revisions(argc
, argv
, rev
, opt
);
283 /* Any arguments at this point are not recognized */
285 die(_("unrecognized argument: %s"), argv
[1]);
287 if (rev
->line_level_traverse
&& rev
->prune_data
.nr
)
288 die(_("-L<range>:<file> cannot be used with pathspec"));
290 memset(&w
, 0, sizeof(w
));
291 userformat_find_requirements(NULL
, &w
);
293 if (!rev
->show_notes_given
&& (!rev
->pretty_given
|| w
.notes
))
296 load_display_notes(&rev
->notes_opt
);
298 if ((rev
->diffopt
.pickaxe_opts
& DIFF_PICKAXE_KINDS_MASK
) ||
299 rev
->diffopt
.filter
|| rev
->diffopt
.flags
.follow_renames
)
300 rev
->always_show_header
= 0;
302 if (source
|| w
.source
) {
303 init_revision_sources(&revision_sources
);
304 rev
->sources
= &revision_sources
;
308 rev
->mailmap
= xmalloc(sizeof(struct string_list
));
309 string_list_init_nodup(rev
->mailmap
);
310 read_mailmap(rev
->mailmap
);
313 if (rev
->pretty_given
&& rev
->commit_format
== CMIT_FMT_RAW
) {
315 * "log --pretty=raw" is special; ignore UI oriented
316 * configuration variables such as decoration.
318 if (!decoration_given
)
319 decoration_style
= 0;
320 if (!rev
->abbrev_commit_given
)
321 rev
->abbrev_commit
= 0;
324 if (rev
->commit_format
== CMIT_FMT_USERFORMAT
) {
327 * Disable decoration loading if the format will not
330 decoration_style
= 0;
331 } else if (!decoration_style
) {
333 * If we are going to show them, make sure we do load
334 * them here, but taking care not to override a
335 * specific style set by config or --decorate.
337 decoration_style
= DECORATE_SHORT_REFS
;
341 if (decoration_style
|| rev
->simplify_by_decoration
) {
342 set_default_decoration_filter(&decoration_filter
);
344 if (decoration_style
)
345 rev
->show_decorations
= 1;
347 load_ref_decorations(&decoration_filter
, decoration_style
);
350 if (rev
->line_level_traverse
)
351 line_log_init(rev
, line_cb
.prefix
, &line_cb
.args
);
356 static void cmd_log_init(int argc
, const char **argv
, const char *prefix
,
357 struct rev_info
*rev
, struct setup_revision_opt
*opt
)
359 cmd_log_init_defaults(rev
);
360 cmd_log_init_finish(argc
, argv
, prefix
, rev
, opt
);
363 static int cmd_log_deinit(int ret
, struct rev_info
*rev
)
365 release_revisions(rev
);
370 * This gives a rough estimate for how many commits we
371 * will print out in the list.
373 static int estimate_commit_count(struct commit_list
*list
)
378 struct commit
*commit
= list
->item
;
379 unsigned int flags
= commit
->object
.flags
;
381 if (!(flags
& (TREESAME
| UNINTERESTING
)))
387 static void show_early_header(struct rev_info
*rev
, const char *stage
, int nr
)
389 if (rev
->shown_one
) {
391 if (rev
->commit_format
!= CMIT_FMT_ONELINE
)
392 putchar(rev
->diffopt
.line_termination
);
394 fprintf(rev
->diffopt
.file
, _("Final output: %d %s\n"), nr
, stage
);
397 static struct itimerval early_output_timer
;
399 static void log_show_early(struct rev_info
*revs
, struct commit_list
*list
)
401 int i
= revs
->early_output
;
403 int no_free
= revs
->diffopt
.no_free
;
405 revs
->diffopt
.no_free
= 0;
406 sort_in_topological_order(&list
, revs
->sort_order
);
408 struct commit
*commit
= list
->item
;
409 switch (simplify_commit(revs
, commit
)) {
412 int n
= estimate_commit_count(list
);
413 show_early_header(revs
, "incomplete", n
);
416 log_tree_commit(revs
, commit
);
422 revs
->diffopt
.no_free
= no_free
;
423 diff_free(&revs
->diffopt
);
429 /* Did we already get enough commits for the early output? */
431 revs
->diffopt
.no_free
= 0;
432 diff_free(&revs
->diffopt
);
437 * ..if no, then repeat it twice a second until we
440 * NOTE! We don't use "it_interval", because if the
441 * reader isn't listening, we want our output to be
442 * throttled by the writing, and not have the timer
443 * trigger every second even if we're blocked on a
446 early_output_timer
.it_value
.tv_sec
= 0;
447 early_output_timer
.it_value
.tv_usec
= 500000;
448 setitimer(ITIMER_REAL
, &early_output_timer
, NULL
);
451 static void early_output(int signal UNUSED
)
453 show_early_output
= log_show_early
;
456 static void setup_early_output(void)
461 * Set up the signal handler, minimally intrusively:
462 * we only set a single volatile integer word (not
463 * using sigatomic_t - trying to avoid unnecessary
464 * system dependencies and headers), and using
467 memset(&sa
, 0, sizeof(sa
));
468 sa
.sa_handler
= early_output
;
469 sigemptyset(&sa
.sa_mask
);
470 sa
.sa_flags
= SA_RESTART
;
471 sigaction(SIGALRM
, &sa
, NULL
);
474 * If we can get the whole output in less than a
475 * tenth of a second, don't even bother doing the
476 * early-output thing..
478 * This is a one-time-only trigger.
480 early_output_timer
.it_value
.tv_sec
= 0;
481 early_output_timer
.it_value
.tv_usec
= 100000;
482 setitimer(ITIMER_REAL
, &early_output_timer
, NULL
);
485 static void finish_early_output(struct rev_info
*rev
)
487 int n
= estimate_commit_count(rev
->commits
);
488 signal(SIGALRM
, SIG_IGN
);
489 show_early_header(rev
, "done", n
);
492 static int cmd_log_walk_no_free(struct rev_info
*rev
)
494 struct commit
*commit
;
498 if (rev
->remerge_diff
) {
499 rev
->remerge_objdir
= tmp_objdir_create("remerge-diff");
500 if (!rev
->remerge_objdir
)
501 die(_("unable to create temporary object directory"));
502 tmp_objdir_replace_primary_odb(rev
->remerge_objdir
, 1);
505 if (rev
->early_output
)
506 setup_early_output();
508 if (prepare_revision_walk(rev
))
509 die(_("revision walk setup failed"));
511 if (rev
->early_output
)
512 finish_early_output(rev
);
515 * For --check and --exit-code, the exit code is based on CHECK_FAILED
516 * and HAS_CHANGES being accumulated in rev->diffopt, so be careful to
517 * retain that state information if replacing rev->diffopt in this loop
519 while ((commit
= get_revision(rev
)) != NULL
) {
520 if (!log_tree_commit(rev
, commit
) && rev
->max_count
>= 0)
522 * We decremented max_count in get_revision,
523 * but we didn't actually show the commit.
526 if (!rev
->reflog_info
) {
528 * We may show a given commit multiple times when
529 * walking the reflogs.
531 free_commit_buffer(the_repository
->parsed_objects
,
533 free_commit_list(commit
->parents
);
534 commit
->parents
= NULL
;
536 if (saved_nrl
< rev
->diffopt
.needed_rename_limit
)
537 saved_nrl
= rev
->diffopt
.needed_rename_limit
;
538 if (rev
->diffopt
.degraded_cc_to_c
)
541 rev
->diffopt
.degraded_cc_to_c
= saved_dcctc
;
542 rev
->diffopt
.needed_rename_limit
= saved_nrl
;
544 if (rev
->remerge_diff
) {
545 tmp_objdir_destroy(rev
->remerge_objdir
);
546 rev
->remerge_objdir
= NULL
;
549 if (rev
->diffopt
.output_format
& DIFF_FORMAT_CHECKDIFF
&&
550 rev
->diffopt
.flags
.check_failed
) {
553 return diff_result_code(&rev
->diffopt
, 0);
556 static int cmd_log_walk(struct rev_info
*rev
)
560 rev
->diffopt
.no_free
= 1;
561 retval
= cmd_log_walk_no_free(rev
);
562 rev
->diffopt
.no_free
= 0;
563 diff_free(&rev
->diffopt
);
567 static int git_log_config(const char *var
, const char *value
,
568 const struct config_context
*ctx
, void *cb
)
570 const char *slot_name
;
572 if (!strcmp(var
, "format.pretty"))
573 return git_config_string(&fmt_pretty
, var
, value
);
574 if (!strcmp(var
, "format.subjectprefix"))
575 return git_config_string(&fmt_patch_subject_prefix
, var
, value
);
576 if (!strcmp(var
, "format.filenamemaxlength")) {
577 fmt_patch_name_max
= git_config_int(var
, value
, ctx
->kvi
);
580 if (!strcmp(var
, "format.encodeemailheaders")) {
581 default_encode_email_headers
= git_config_bool(var
, value
);
584 if (!strcmp(var
, "log.abbrevcommit")) {
585 default_abbrev_commit
= git_config_bool(var
, value
);
588 if (!strcmp(var
, "log.date"))
589 return git_config_string(&default_date_mode
, var
, value
);
590 if (!strcmp(var
, "log.decorate")) {
591 decoration_style
= parse_decoration_style(value
);
592 if (decoration_style
< 0)
593 decoration_style
= 0; /* maybe warn? */
596 if (!strcmp(var
, "log.diffmerges"))
597 return diff_merges_config(value
);
598 if (!strcmp(var
, "log.showroot")) {
599 default_show_root
= git_config_bool(var
, value
);
602 if (!strcmp(var
, "log.follow")) {
603 default_follow
= git_config_bool(var
, value
);
606 if (skip_prefix(var
, "color.decorate.", &slot_name
))
607 return parse_decorate_color_config(var
, slot_name
, value
);
608 if (!strcmp(var
, "log.mailmap")) {
609 use_mailmap_config
= git_config_bool(var
, value
);
612 if (!strcmp(var
, "log.showsignature")) {
613 default_show_signature
= git_config_bool(var
, value
);
617 return git_diff_ui_config(var
, value
, ctx
, cb
);
620 int cmd_whatchanged(int argc
, const char **argv
, const char *prefix
)
623 struct setup_revision_opt opt
;
626 git_config(git_log_config
, NULL
);
628 repo_init_revisions(the_repository
, &rev
, prefix
);
629 git_config(grep_config
, &rev
.grep_filter
);
632 rev
.simplify_history
= 0;
633 memset(&opt
, 0, sizeof(opt
));
635 opt
.revarg_opt
= REVARG_COMMITTISH
;
636 cmd_log_init(argc
, argv
, prefix
, &rev
, &opt
);
637 if (!rev
.diffopt
.output_format
)
638 rev
.diffopt
.output_format
= DIFF_FORMAT_RAW
;
639 return cmd_log_deinit(cmd_log_walk(&rev
), &rev
);
642 static void show_tagger(const char *buf
, struct rev_info
*rev
)
644 struct strbuf out
= STRBUF_INIT
;
645 struct pretty_print_context pp
= {0};
647 pp
.fmt
= rev
->commit_format
;
648 pp
.date_mode
= rev
->date_mode
;
649 pp_user_info(&pp
, "Tagger", &out
, buf
, get_log_output_encoding());
650 fprintf(rev
->diffopt
.file
, "%s", out
.buf
);
651 strbuf_release(&out
);
654 static int show_blob_object(const struct object_id
*oid
, struct rev_info
*rev
, const char *obj_name
)
656 struct object_id oidc
;
657 struct object_context obj_context
;
661 fflush(rev
->diffopt
.file
);
662 if (!rev
->diffopt
.flags
.textconv_set_via_cmdline
||
663 !rev
->diffopt
.flags
.allow_textconv
)
664 return stream_blob_to_fd(1, oid
, NULL
, 0);
666 if (get_oid_with_context(the_repository
, obj_name
,
668 &oidc
, &obj_context
))
669 die(_("not a valid object name %s"), obj_name
);
670 if (!obj_context
.path
||
671 !textconv_object(the_repository
, obj_context
.path
,
672 obj_context
.mode
, &oidc
, 1, &buf
, &size
)) {
673 free(obj_context
.path
);
674 return stream_blob_to_fd(1, oid
, NULL
, 0);
678 die(_("git show %s: bad file"), obj_name
);
680 write_or_die(1, buf
, size
);
681 free(obj_context
.path
);
685 static int show_tag_object(const struct object_id
*oid
, struct rev_info
*rev
)
688 enum object_type type
;
689 char *buf
= repo_read_object_file(the_repository
, oid
, &type
, &size
);
693 return error(_("could not read object %s"), oid_to_hex(oid
));
695 assert(type
== OBJ_TAG
);
696 while (offset
< size
&& buf
[offset
] != '\n') {
697 int new_offset
= offset
+ 1;
699 while (new_offset
< size
&& buf
[new_offset
++] != '\n')
701 if (skip_prefix(buf
+ offset
, "tagger ", &ident
))
702 show_tagger(ident
, rev
);
707 fwrite(buf
+ offset
, size
- offset
, 1, rev
->diffopt
.file
);
712 static int show_tree_object(const struct object_id
*oid UNUSED
,
713 struct strbuf
*base UNUSED
,
714 const char *pathname
, unsigned mode
,
717 FILE *file
= context
;
718 fprintf(file
, "%s%s\n", pathname
, S_ISDIR(mode
) ? "/" : "");
722 static void show_setup_revisions_tweak(struct rev_info
*rev
,
723 struct setup_revision_opt
*opt
)
725 if (rev
->first_parent_only
)
726 diff_merges_default_to_first_parent(rev
);
728 diff_merges_default_to_dense_combined(rev
);
729 if (!rev
->diffopt
.output_format
)
730 rev
->diffopt
.output_format
= DIFF_FORMAT_PATCH
;
733 int cmd_show(int argc
, const char **argv
, const char *prefix
)
737 struct setup_revision_opt opt
;
738 struct pathspec match_all
;
742 git_config(git_log_config
, NULL
);
744 if (the_repository
->gitdir
) {
745 prepare_repo_settings(the_repository
);
746 the_repository
->settings
.command_requires_full_index
= 0;
749 memset(&match_all
, 0, sizeof(match_all
));
750 repo_init_revisions(the_repository
, &rev
, prefix
);
751 git_config(grep_config
, &rev
.grep_filter
);
754 rev
.always_show_header
= 1;
756 rev
.diffopt
.stat_width
= -1; /* Scale to real terminal size */
758 memset(&opt
, 0, sizeof(opt
));
760 opt
.tweak
= show_setup_revisions_tweak
;
761 cmd_log_init(argc
, argv
, prefix
, &rev
, &opt
);
764 return cmd_log_deinit(cmd_log_walk(&rev
), &rev
);
766 rev
.diffopt
.no_free
= 1;
767 for (i
= 0; i
< rev
.pending
.nr
&& !ret
; i
++) {
768 struct object
*o
= rev
.pending
.objects
[i
].item
;
769 const char *name
= rev
.pending
.objects
[i
].name
;
772 ret
= show_blob_object(&o
->oid
, &rev
, name
);
775 struct tag
*t
= (struct tag
*)o
;
776 struct object_id
*oid
= get_tagged_oid(t
);
780 fprintf(rev
.diffopt
.file
, "%stag %s%s\n",
781 diff_get_color_opt(&rev
.diffopt
, DIFF_COMMIT
),
783 diff_get_color_opt(&rev
.diffopt
, DIFF_RESET
));
784 ret
= show_tag_object(&o
->oid
, &rev
);
788 o
= parse_object(the_repository
, oid
);
790 ret
= error(_("could not read object %s"),
792 rev
.pending
.objects
[i
].item
= o
;
799 fprintf(rev
.diffopt
.file
, "%stree %s%s\n\n",
800 diff_get_color_opt(&rev
.diffopt
, DIFF_COMMIT
),
802 diff_get_color_opt(&rev
.diffopt
, DIFF_RESET
));
803 read_tree(the_repository
, (struct tree
*)o
,
804 &match_all
, show_tree_object
,
810 struct object_array old
;
811 struct object_array blank
= OBJECT_ARRAY_INIT
;
813 memcpy(&old
, &rev
.pending
, sizeof(old
));
814 memcpy(&rev
.pending
, &blank
, sizeof(rev
.pending
));
816 add_object_array(o
, name
, &rev
.pending
);
817 ret
= cmd_log_walk_no_free(&rev
);
821 * object_array_clear(&pending). It was
822 * cleared already in prepare_revision_walk()
824 memcpy(&rev
.pending
, &old
, sizeof(rev
.pending
));
828 ret
= error(_("unknown type: %d"), o
->type
);
832 rev
.diffopt
.no_free
= 0;
833 diff_free(&rev
.diffopt
);
835 return cmd_log_deinit(ret
, &rev
);
839 * This is equivalent to "git log -g --abbrev-commit --pretty=oneline"
841 int cmd_log_reflog(int argc
, const char **argv
, const char *prefix
)
844 struct setup_revision_opt opt
;
847 git_config(git_log_config
, NULL
);
849 repo_init_revisions(the_repository
, &rev
, prefix
);
850 init_reflog_walk(&rev
.reflog_info
);
851 git_config(grep_config
, &rev
.grep_filter
);
853 rev
.verbose_header
= 1;
854 memset(&opt
, 0, sizeof(opt
));
856 cmd_log_init_defaults(&rev
);
857 rev
.abbrev_commit
= 1;
858 rev
.commit_format
= CMIT_FMT_ONELINE
;
859 rev
.use_terminator
= 1;
860 rev
.always_show_header
= 1;
861 cmd_log_init_finish(argc
, argv
, prefix
, &rev
, &opt
);
863 return cmd_log_deinit(cmd_log_walk(&rev
), &rev
);
866 static void log_setup_revisions_tweak(struct rev_info
*rev
,
867 struct setup_revision_opt
*opt
)
869 if (rev
->diffopt
.flags
.default_follow_renames
&&
870 diff_check_follow_pathspec(&rev
->prune_data
, 0))
871 rev
->diffopt
.flags
.follow_renames
= 1;
873 if (rev
->first_parent_only
)
874 diff_merges_default_to_first_parent(rev
);
877 int cmd_log(int argc
, const char **argv
, const char *prefix
)
880 struct setup_revision_opt opt
;
883 git_config(git_log_config
, NULL
);
885 repo_init_revisions(the_repository
, &rev
, prefix
);
886 git_config(grep_config
, &rev
.grep_filter
);
888 rev
.always_show_header
= 1;
889 memset(&opt
, 0, sizeof(opt
));
891 opt
.revarg_opt
= REVARG_COMMITTISH
;
892 opt
.tweak
= log_setup_revisions_tweak
;
893 cmd_log_init(argc
, argv
, prefix
, &rev
, &opt
);
894 return cmd_log_deinit(cmd_log_walk(&rev
), &rev
);
899 static const char *fmt_patch_suffix
= ".patch";
900 static int numbered
= 0;
901 static int auto_number
= 1;
903 static char *default_attach
= NULL
;
905 static struct string_list extra_hdr
= STRING_LIST_INIT_NODUP
;
906 static struct string_list extra_to
= STRING_LIST_INIT_NODUP
;
907 static struct string_list extra_cc
= STRING_LIST_INIT_NODUP
;
909 static void add_header(const char *value
)
911 struct string_list_item
*item
;
912 int len
= strlen(value
);
913 while (len
&& value
[len
- 1] == '\n')
916 if (!strncasecmp(value
, "to: ", 4)) {
917 item
= string_list_append(&extra_to
, value
+ 4);
919 } else if (!strncasecmp(value
, "cc: ", 4)) {
920 item
= string_list_append(&extra_cc
, value
+ 4);
923 item
= string_list_append(&extra_hdr
, value
);
926 item
->string
[len
] = '\0';
942 enum cover_from_description
{
949 enum auto_base_setting
{
955 static enum thread_level thread
;
956 static int do_signoff
;
957 static enum auto_base_setting auto_base
;
959 static const char *signature
= git_version_string
;
960 static const char *signature_file
;
961 static enum cover_setting config_cover_letter
;
962 static const char *config_output_directory
;
963 static enum cover_from_description cover_from_description_mode
= COVER_FROM_MESSAGE
;
964 static int show_notes
;
965 static struct display_notes_opt notes_opt
;
967 static enum cover_from_description
parse_cover_from_description(const char *arg
)
969 if (!arg
|| !strcmp(arg
, "default"))
970 return COVER_FROM_MESSAGE
;
971 else if (!strcmp(arg
, "none"))
972 return COVER_FROM_NONE
;
973 else if (!strcmp(arg
, "message"))
974 return COVER_FROM_MESSAGE
;
975 else if (!strcmp(arg
, "subject"))
976 return COVER_FROM_SUBJECT
;
977 else if (!strcmp(arg
, "auto"))
978 return COVER_FROM_AUTO
;
980 die(_("%s: invalid cover from description mode"), arg
);
983 static int git_format_config(const char *var
, const char *value
,
984 const struct config_context
*ctx
, void *cb
)
986 if (!strcmp(var
, "format.headers")) {
988 die(_("format.headers without value"));
992 if (!strcmp(var
, "format.suffix"))
993 return git_config_string(&fmt_patch_suffix
, var
, value
);
994 if (!strcmp(var
, "format.to")) {
996 return config_error_nonbool(var
);
997 string_list_append(&extra_to
, value
);
1000 if (!strcmp(var
, "format.cc")) {
1002 return config_error_nonbool(var
);
1003 string_list_append(&extra_cc
, value
);
1006 if (!strcmp(var
, "diff.color") || !strcmp(var
, "color.diff") ||
1007 !strcmp(var
, "color.ui") || !strcmp(var
, "diff.submodule")) {
1010 if (!strcmp(var
, "format.numbered")) {
1011 if (value
&& !strcasecmp(value
, "auto")) {
1015 numbered
= git_config_bool(var
, value
);
1016 auto_number
= auto_number
&& numbered
;
1019 if (!strcmp(var
, "format.attach")) {
1020 if (value
&& *value
)
1021 default_attach
= xstrdup(value
);
1022 else if (value
&& !*value
)
1023 FREE_AND_NULL(default_attach
);
1025 default_attach
= xstrdup(git_version_string
);
1028 if (!strcmp(var
, "format.thread")) {
1029 if (value
&& !strcasecmp(value
, "deep")) {
1030 thread
= THREAD_DEEP
;
1033 if (value
&& !strcasecmp(value
, "shallow")) {
1034 thread
= THREAD_SHALLOW
;
1037 thread
= git_config_bool(var
, value
) ? THREAD_SHALLOW
: THREAD_UNSET
;
1040 if (!strcmp(var
, "format.signoff")) {
1041 do_signoff
= git_config_bool(var
, value
);
1044 if (!strcmp(var
, "format.signature"))
1045 return git_config_string(&signature
, var
, value
);
1046 if (!strcmp(var
, "format.signaturefile"))
1047 return git_config_pathname(&signature_file
, var
, value
);
1048 if (!strcmp(var
, "format.coverletter")) {
1049 if (value
&& !strcasecmp(value
, "auto")) {
1050 config_cover_letter
= COVER_AUTO
;
1053 config_cover_letter
= git_config_bool(var
, value
) ? COVER_ON
: COVER_OFF
;
1056 if (!strcmp(var
, "format.outputdirectory"))
1057 return git_config_string(&config_output_directory
, var
, value
);
1058 if (!strcmp(var
, "format.useautobase")) {
1059 if (value
&& !strcasecmp(value
, "whenAble")) {
1060 auto_base
= AUTO_BASE_WHEN_ABLE
;
1063 auto_base
= git_config_bool(var
, value
) ? AUTO_BASE_ALWAYS
: AUTO_BASE_NEVER
;
1066 if (!strcmp(var
, "format.from")) {
1067 int b
= git_parse_maybe_bool(value
);
1070 from
= xstrdup(value
);
1072 from
= xstrdup(git_committer_info(IDENT_NO_DATE
));
1077 if (!strcmp(var
, "format.forceinbodyfrom")) {
1078 force_in_body_from
= git_config_bool(var
, value
);
1081 if (!strcmp(var
, "format.notes")) {
1082 int b
= git_parse_maybe_bool(value
);
1084 enable_ref_display_notes(¬es_opt
, &show_notes
, value
);
1086 enable_default_display_notes(¬es_opt
, &show_notes
);
1088 disable_display_notes(¬es_opt
, &show_notes
);
1091 if (!strcmp(var
, "format.coverfromdescription")) {
1092 cover_from_description_mode
= parse_cover_from_description(value
);
1095 if (!strcmp(var
, "format.mboxrd")) {
1096 stdout_mboxrd
= git_config_bool(var
, value
);
1099 if (!strcmp(var
, "format.noprefix")) {
1100 format_no_prefix
= 1;
1105 * ignore some porcelain config which would otherwise be parsed by
1106 * git_diff_ui_config(), via git_log_config(); we can't just avoid
1107 * diff_ui_config completely, because we do care about some ui options
1110 if (!strcmp(var
, "diff.noprefix"))
1113 return git_log_config(var
, value
, ctx
, cb
);
1116 static const char *output_directory
= NULL
;
1117 static int outdir_offset
;
1119 static int open_next_file(struct commit
*commit
, const char *subject
,
1120 struct rev_info
*rev
, int quiet
)
1122 struct strbuf filename
= STRBUF_INIT
;
1124 if (output_directory
) {
1125 strbuf_addstr(&filename
, output_directory
);
1126 strbuf_complete(&filename
, '/');
1129 if (rev
->numbered_files
)
1130 strbuf_addf(&filename
, "%d", rev
->nr
);
1132 fmt_output_commit(&filename
, commit
, rev
);
1134 fmt_output_subject(&filename
, subject
, rev
);
1137 printf("%s\n", filename
.buf
+ outdir_offset
);
1139 if (!(rev
->diffopt
.file
= fopen(filename
.buf
, "w"))) {
1140 error_errno(_("cannot open patch file %s"), filename
.buf
);
1141 strbuf_release(&filename
);
1145 strbuf_release(&filename
);
1149 static void get_patch_ids(struct rev_info
*rev
, struct patch_ids
*ids
)
1151 struct rev_info check_rev
;
1152 struct commit
*commit
, *c1
, *c2
;
1153 struct object
*o1
, *o2
;
1154 unsigned flags1
, flags2
;
1156 if (rev
->pending
.nr
!= 2)
1157 die(_("need exactly one range"));
1159 o1
= rev
->pending
.objects
[0].item
;
1160 o2
= rev
->pending
.objects
[1].item
;
1163 c1
= lookup_commit_reference(the_repository
, &o1
->oid
);
1164 c2
= lookup_commit_reference(the_repository
, &o2
->oid
);
1166 if ((flags1
& UNINTERESTING
) == (flags2
& UNINTERESTING
))
1167 die(_("not a range"));
1169 init_patch_ids(the_repository
, ids
);
1171 /* given a range a..b get all patch ids for b..a */
1172 repo_init_revisions(the_repository
, &check_rev
, rev
->prefix
);
1173 check_rev
.max_parents
= 1;
1174 o1
->flags
^= UNINTERESTING
;
1175 o2
->flags
^= UNINTERESTING
;
1176 add_pending_object(&check_rev
, o1
, "o1");
1177 add_pending_object(&check_rev
, o2
, "o2");
1178 if (prepare_revision_walk(&check_rev
))
1179 die(_("revision walk setup failed"));
1181 while ((commit
= get_revision(&check_rev
)) != NULL
) {
1182 add_commit_patch_id(commit
, ids
);
1185 /* reset for next revision walk */
1186 clear_commit_marks(c1
, SEEN
| UNINTERESTING
| SHOWN
| ADDED
);
1187 clear_commit_marks(c2
, SEEN
| UNINTERESTING
| SHOWN
| ADDED
);
1192 static void gen_message_id(struct rev_info
*info
, char *base
)
1194 struct strbuf buf
= STRBUF_INIT
;
1195 strbuf_addf(&buf
, "%s.%"PRItime
".git.%s", base
,
1196 (timestamp_t
) time(NULL
),
1197 git_committer_info(IDENT_NO_NAME
|IDENT_NO_DATE
|IDENT_STRICT
));
1198 info
->message_id
= strbuf_detach(&buf
, NULL
);
1201 static void print_signature(FILE *file
)
1203 if (!signature
|| !*signature
)
1206 fprintf(file
, "-- \n%s", signature
);
1207 if (signature
[strlen(signature
)-1] != '\n')
1212 static char *find_branch_name(struct rev_info
*rev
)
1214 int i
, positive
= -1;
1215 struct object_id branch_oid
;
1216 const struct object_id
*tip_oid
;
1217 const char *ref
, *v
;
1218 char *full_ref
, *branch
= NULL
;
1220 for (i
= 0; i
< rev
->cmdline
.nr
; i
++) {
1221 if (rev
->cmdline
.rev
[i
].flags
& UNINTERESTING
)
1230 ref
= rev
->cmdline
.rev
[positive
].name
;
1231 tip_oid
= &rev
->cmdline
.rev
[positive
].item
->oid
;
1232 if (repo_dwim_ref(the_repository
, ref
, strlen(ref
), &branch_oid
,
1234 skip_prefix(full_ref
, "refs/heads/", &v
) &&
1235 oideq(tip_oid
, &branch_oid
))
1236 branch
= xstrdup(v
);
1241 static void show_diffstat(struct rev_info
*rev
,
1242 struct commit
*origin
, struct commit
*head
)
1244 struct diff_options opts
;
1246 memcpy(&opts
, &rev
->diffopt
, sizeof(opts
));
1247 opts
.output_format
= DIFF_FORMAT_SUMMARY
| DIFF_FORMAT_DIFFSTAT
;
1248 diff_setup_done(&opts
);
1250 diff_tree_oid(get_commit_tree_oid(origin
),
1251 get_commit_tree_oid(head
),
1253 diffcore_std(&opts
);
1256 fprintf(rev
->diffopt
.file
, "\n");
1259 static void prepare_cover_text(struct pretty_print_context
*pp
,
1260 const char *branch_name
,
1262 const char *encoding
,
1265 const char *subject
= "*** SUBJECT HERE ***";
1266 const char *body
= "*** BLURB HERE ***";
1267 struct strbuf description_sb
= STRBUF_INIT
;
1268 struct strbuf subject_sb
= STRBUF_INIT
;
1270 if (cover_from_description_mode
== COVER_FROM_NONE
)
1273 if (branch_name
&& *branch_name
)
1274 read_branch_desc(&description_sb
, branch_name
);
1275 if (!description_sb
.len
)
1278 if (cover_from_description_mode
== COVER_FROM_SUBJECT
||
1279 cover_from_description_mode
== COVER_FROM_AUTO
)
1280 body
= format_subject(&subject_sb
, description_sb
.buf
, " ");
1282 if (cover_from_description_mode
== COVER_FROM_MESSAGE
||
1283 (cover_from_description_mode
== COVER_FROM_AUTO
&&
1284 subject_sb
.len
> COVER_FROM_AUTO_MAX_SUBJECT_LEN
))
1285 body
= description_sb
.buf
;
1287 subject
= subject_sb
.buf
;
1290 pp_title_line(pp
, &subject
, sb
, encoding
, need_8bit_cte
);
1291 pp_remainder(pp
, &body
, sb
, 0);
1293 strbuf_release(&description_sb
);
1294 strbuf_release(&subject_sb
);
1297 static int get_notes_refs(struct string_list_item
*item
, void *arg
)
1299 strvec_pushf(arg
, "--notes=%s", item
->string
);
1303 static void get_notes_args(struct strvec
*arg
, struct rev_info
*rev
)
1305 if (!rev
->show_notes
) {
1306 strvec_push(arg
, "--no-notes");
1307 } else if (rev
->notes_opt
.use_default_notes
> 0 ||
1308 (rev
->notes_opt
.use_default_notes
== -1 &&
1309 !rev
->notes_opt
.extra_notes_refs
.nr
)) {
1310 strvec_push(arg
, "--notes");
1312 for_each_string_list(&rev
->notes_opt
.extra_notes_refs
, get_notes_refs
, arg
);
1316 static void make_cover_letter(struct rev_info
*rev
, int use_separate_file
,
1317 struct commit
*origin
,
1318 int nr
, struct commit
**list
,
1319 const char *branch_name
,
1322 const char *committer
;
1323 struct shortlog log
;
1324 struct strbuf sb
= STRBUF_INIT
;
1326 const char *encoding
= "UTF-8";
1327 int need_8bit_cte
= 0;
1328 struct pretty_print_context pp
= {0};
1329 struct commit
*head
= list
[0];
1331 if (!cmit_fmt_is_mail(rev
->commit_format
))
1332 die(_("cover letter needs email format"));
1334 committer
= git_committer_info(0);
1336 if (use_separate_file
&&
1337 open_next_file(NULL
, rev
->numbered_files
? NULL
: "cover-letter", rev
, quiet
))
1338 die(_("failed to create cover-letter file"));
1340 log_write_email_headers(rev
, head
, &pp
.after_subject
, &need_8bit_cte
, 0);
1342 for (i
= 0; !need_8bit_cte
&& i
< nr
; i
++) {
1343 const char *buf
= repo_get_commit_buffer(the_repository
,
1345 if (has_non_ascii(buf
))
1347 repo_unuse_commit_buffer(the_repository
, list
[i
], buf
);
1351 branch_name
= find_branch_name(rev
);
1353 pp
.fmt
= CMIT_FMT_EMAIL
;
1354 pp
.date_mode
.type
= DATE_RFC2822
;
1356 pp
.print_email_subject
= 1;
1357 pp_user_info(&pp
, NULL
, &sb
, committer
, encoding
);
1358 prepare_cover_text(&pp
, branch_name
, &sb
, encoding
, need_8bit_cte
);
1359 fprintf(rev
->diffopt
.file
, "%s\n", sb
.buf
);
1361 strbuf_release(&sb
);
1363 shortlog_init(&log
);
1365 log
.wrap
= MAIL_DEFAULT_WRAP
;
1368 log
.file
= rev
->diffopt
.file
;
1369 log
.groups
= SHORTLOG_GROUP_AUTHOR
;
1370 shortlog_finish_setup(&log
);
1371 for (i
= 0; i
< nr
; i
++)
1372 shortlog_add_commit(&log
, list
[i
]);
1374 shortlog_output(&log
);
1376 /* We can only do diffstat with a unique reference point */
1378 show_diffstat(rev
, origin
, head
);
1380 if (rev
->idiff_oid1
) {
1381 fprintf_ln(rev
->diffopt
.file
, "%s", rev
->idiff_title
);
1382 show_interdiff(rev
->idiff_oid1
, rev
->idiff_oid2
, 0,
1388 * Pass minimum required diff-options to range-diff; others
1389 * can be added later if deemed desirable.
1391 struct diff_options opts
;
1392 struct strvec other_arg
= STRVEC_INIT
;
1393 struct range_diff_options range_diff_opts
= {
1394 .creation_factor
= rev
->creation_factor
,
1397 .other_arg
= &other_arg
1400 repo_diff_setup(the_repository
, &opts
);
1401 opts
.file
= rev
->diffopt
.file
;
1402 opts
.use_color
= rev
->diffopt
.use_color
;
1403 diff_setup_done(&opts
);
1404 fprintf_ln(rev
->diffopt
.file
, "%s", rev
->rdiff_title
);
1405 get_notes_args(&other_arg
, rev
);
1406 show_range_diff(rev
->rdiff1
, rev
->rdiff2
, &range_diff_opts
);
1407 strvec_clear(&other_arg
);
1411 static char *clean_message_id(const char *msg_id
)
1414 const char *a
, *z
, *m
;
1417 while ((ch
= *m
) && (isspace(ch
) || (ch
== '<')))
1422 if (!isspace(ch
) && (ch
!= '>'))
1427 die(_("insane in-reply-to: %s"), msg_id
);
1430 return xmemdupz(a
, z
- a
);
1433 static const char *set_outdir(const char *prefix
, const char *output_directory
)
1435 if (output_directory
&& is_absolute_path(output_directory
))
1436 return output_directory
;
1438 if (!prefix
|| !*prefix
) {
1439 if (output_directory
)
1440 return output_directory
;
1441 /* The user did not explicitly ask for "./" */
1446 outdir_offset
= strlen(prefix
);
1447 if (!output_directory
)
1450 return prefix_filename(prefix
, output_directory
);
1453 static const char * const builtin_format_patch_usage
[] = {
1454 N_("git format-patch [<options>] [<since> | <revision-range>]"),
1458 static int keep_subject
= 0;
1460 static int keep_callback(const struct option
*opt
, const char *arg
, int unset
)
1462 BUG_ON_OPT_NEG(unset
);
1463 BUG_ON_OPT_ARG(arg
);
1464 ((struct rev_info
*)opt
->value
)->total
= -1;
1469 static int subject_prefix
= 0;
1471 static int subject_prefix_callback(const struct option
*opt
, const char *arg
,
1474 BUG_ON_OPT_NEG(unset
);
1476 ((struct rev_info
*)opt
->value
)->subject_prefix
= arg
;
1480 static int rfc_callback(const struct option
*opt
, const char *arg
, int unset
)
1482 BUG_ON_OPT_NEG(unset
);
1483 BUG_ON_OPT_ARG(arg
);
1484 return subject_prefix_callback(opt
, "RFC PATCH", unset
);
1487 static int numbered_cmdline_opt
= 0;
1489 static int numbered_callback(const struct option
*opt
, const char *arg
,
1492 BUG_ON_OPT_ARG(arg
);
1493 *(int *)opt
->value
= numbered_cmdline_opt
= unset
? 0 : 1;
1499 static int no_numbered_callback(const struct option
*opt
, const char *arg
,
1502 BUG_ON_OPT_NEG(unset
);
1503 return numbered_callback(opt
, arg
, 1);
1506 static int output_directory_callback(const struct option
*opt
, const char *arg
,
1509 const char **dir
= (const char **)opt
->value
;
1510 BUG_ON_OPT_NEG(unset
);
1512 die(_("two output directories?"));
1517 static int thread_callback(const struct option
*opt
, const char *arg
, int unset
)
1519 enum thread_level
*thread
= (enum thread_level
*)opt
->value
;
1521 *thread
= THREAD_UNSET
;
1522 else if (!arg
|| !strcmp(arg
, "shallow"))
1523 *thread
= THREAD_SHALLOW
;
1524 else if (!strcmp(arg
, "deep"))
1525 *thread
= THREAD_DEEP
;
1527 * Please update _git_formatpatch() in git-completion.bash
1528 * when you add new options.
1535 static int attach_callback(const struct option
*opt
, const char *arg
, int unset
)
1537 struct rev_info
*rev
= (struct rev_info
*)opt
->value
;
1539 rev
->mime_boundary
= NULL
;
1541 rev
->mime_boundary
= arg
;
1543 rev
->mime_boundary
= git_version_string
;
1544 rev
->no_inline
= unset
? 0 : 1;
1548 static int inline_callback(const struct option
*opt
, const char *arg
, int unset
)
1550 struct rev_info
*rev
= (struct rev_info
*)opt
->value
;
1552 rev
->mime_boundary
= NULL
;
1554 rev
->mime_boundary
= arg
;
1556 rev
->mime_boundary
= git_version_string
;
1561 static int header_callback(const struct option
*opt
, const char *arg
, int unset
)
1564 string_list_clear(&extra_hdr
, 0);
1565 string_list_clear(&extra_to
, 0);
1566 string_list_clear(&extra_cc
, 0);
1573 static int to_callback(const struct option
*opt
, const char *arg
, int unset
)
1576 string_list_clear(&extra_to
, 0);
1578 string_list_append(&extra_to
, arg
);
1582 static int cc_callback(const struct option
*opt
, const char *arg
, int unset
)
1585 string_list_clear(&extra_cc
, 0);
1587 string_list_append(&extra_cc
, arg
);
1591 static int from_callback(const struct option
*opt
, const char *arg
, int unset
)
1593 char **from
= opt
->value
;
1600 *from
= xstrdup(arg
);
1602 *from
= xstrdup(git_committer_info(IDENT_NO_DATE
));
1606 static int base_callback(const struct option
*opt
, const char *arg
, int unset
)
1608 const char **base_commit
= opt
->value
;
1611 auto_base
= AUTO_BASE_NEVER
;
1612 *base_commit
= NULL
;
1613 } else if (!strcmp(arg
, "auto")) {
1614 auto_base
= AUTO_BASE_ALWAYS
;
1615 *base_commit
= NULL
;
1617 auto_base
= AUTO_BASE_NEVER
;
1623 struct base_tree_info
{
1624 struct object_id base_commit
;
1625 int nr_patch_id
, alloc_patch_id
;
1626 struct object_id
*patch_id
;
1629 static struct commit
*get_base_commit(const char *base_commit
,
1630 struct commit
**list
,
1633 struct commit
*base
= NULL
;
1634 struct commit
**rev
;
1635 int i
= 0, rev_nr
= 0, auto_select
, die_on_failure
;
1637 switch (auto_base
) {
1638 case AUTO_BASE_NEVER
:
1643 /* no base information is requested */
1647 case AUTO_BASE_ALWAYS
:
1648 case AUTO_BASE_WHEN_ABLE
:
1650 BUG("requested automatic base selection but a commit was provided");
1653 die_on_failure
= auto_base
== AUTO_BASE_ALWAYS
;
1657 BUG("unexpected automatic base selection method");
1661 base
= lookup_commit_reference_by_name(base_commit
);
1663 die(_("unknown commit %s"), base_commit
);
1665 struct branch
*curr_branch
= branch_get(NULL
);
1666 const char *upstream
= branch_get_upstream(curr_branch
, NULL
);
1668 struct commit_list
*base_list
;
1669 struct commit
*commit
;
1670 struct object_id oid
;
1672 if (repo_get_oid(the_repository
, upstream
, &oid
)) {
1674 die(_("failed to resolve '%s' as a valid ref"), upstream
);
1678 commit
= lookup_commit_or_die(&oid
, "upstream base");
1679 base_list
= repo_get_merge_bases_many(the_repository
,
1682 /* There should be one and only one merge base. */
1683 if (!base_list
|| base_list
->next
) {
1684 if (die_on_failure
) {
1685 die(_("could not find exact merge base"));
1687 free_commit_list(base_list
);
1691 base
= base_list
->item
;
1692 free_commit_list(base_list
);
1695 die(_("failed to get upstream, if you want to record base commit automatically,\n"
1696 "please use git branch --set-upstream-to to track a remote branch.\n"
1697 "Or you could specify base commit by --base=<base-commit-id> manually"));
1703 ALLOC_ARRAY(rev
, total
);
1704 for (i
= 0; i
< total
; i
++)
1709 * Get merge base through pair-wise computations
1710 * and store it in rev[0].
1712 while (rev_nr
> 1) {
1713 for (i
= 0; i
< rev_nr
/ 2; i
++) {
1714 struct commit_list
*merge_base
;
1715 merge_base
= repo_get_merge_bases(the_repository
,
1718 if (!merge_base
|| merge_base
->next
) {
1719 if (die_on_failure
) {
1720 die(_("failed to find exact merge base"));
1727 rev
[i
] = merge_base
->item
;
1731 rev
[i
] = rev
[2 * i
];
1732 rev_nr
= DIV_ROUND_UP(rev_nr
, 2);
1735 if (!repo_in_merge_bases(the_repository
, base
, rev
[0])) {
1736 if (die_on_failure
) {
1737 die(_("base commit should be the ancestor of revision list"));
1744 for (i
= 0; i
< total
; i
++) {
1745 if (base
== list
[i
]) {
1746 if (die_on_failure
) {
1747 die(_("base commit shouldn't be in revision list"));
1759 define_commit_slab(commit_base
, int);
1761 static void prepare_bases(struct base_tree_info
*bases
,
1762 struct commit
*base
,
1763 struct commit
**list
,
1766 struct commit
*commit
;
1767 struct rev_info revs
;
1768 struct diff_options diffopt
;
1769 struct commit_base commit_base
;
1775 init_commit_base(&commit_base
);
1776 repo_diff_setup(the_repository
, &diffopt
);
1777 diffopt
.flags
.recursive
= 1;
1778 diff_setup_done(&diffopt
);
1780 oidcpy(&bases
->base_commit
, &base
->object
.oid
);
1782 repo_init_revisions(the_repository
, &revs
, NULL
);
1783 revs
.max_parents
= 1;
1784 revs
.topo_order
= 1;
1785 for (i
= 0; i
< total
; i
++) {
1786 list
[i
]->object
.flags
&= ~UNINTERESTING
;
1787 add_pending_object(&revs
, &list
[i
]->object
, "rev_list");
1788 *commit_base_at(&commit_base
, list
[i
]) = 1;
1790 base
->object
.flags
|= UNINTERESTING
;
1791 add_pending_object(&revs
, &base
->object
, "base");
1793 if (prepare_revision_walk(&revs
))
1794 die(_("revision walk setup failed"));
1796 * Traverse the commits list, get prerequisite patch ids
1797 * and stuff them in bases structure.
1799 while ((commit
= get_revision(&revs
)) != NULL
) {
1800 struct object_id oid
;
1801 struct object_id
*patch_id
;
1802 if (*commit_base_at(&commit_base
, commit
))
1804 if (commit_patch_id(commit
, &diffopt
, &oid
, 0))
1805 die(_("cannot get patch id"));
1806 ALLOC_GROW(bases
->patch_id
, bases
->nr_patch_id
+ 1, bases
->alloc_patch_id
);
1807 patch_id
= bases
->patch_id
+ bases
->nr_patch_id
;
1808 oidcpy(patch_id
, &oid
);
1809 bases
->nr_patch_id
++;
1811 clear_commit_base(&commit_base
);
1814 static void print_bases(struct base_tree_info
*bases
, FILE *file
)
1818 /* Only do this once, either for the cover or for the first one */
1819 if (is_null_oid(&bases
->base_commit
))
1822 /* Show the base commit */
1823 fprintf(file
, "\nbase-commit: %s\n", oid_to_hex(&bases
->base_commit
));
1825 /* Show the prerequisite patches */
1826 for (i
= bases
->nr_patch_id
- 1; i
>= 0; i
--)
1827 fprintf(file
, "prerequisite-patch-id: %s\n", oid_to_hex(&bases
->patch_id
[i
]));
1829 free(bases
->patch_id
);
1830 bases
->nr_patch_id
= 0;
1831 bases
->alloc_patch_id
= 0;
1832 oidclr(&bases
->base_commit
);
1835 static const char *diff_title(struct strbuf
*sb
,
1836 const char *reroll_count
,
1837 const char *generic
,
1838 const char *rerolled
)
1842 /* RFC may be v0, so allow -v1 to diff against v0 */
1843 if (reroll_count
&& !strtol_i(reroll_count
, 10, &v
) &&
1845 strbuf_addf(sb
, rerolled
, v
- 1);
1847 strbuf_addstr(sb
, generic
);
1851 static void infer_range_diff_ranges(struct strbuf
*r1
,
1854 struct commit
*origin
,
1855 struct commit
*head
)
1857 const char *head_oid
= oid_to_hex(&head
->object
.oid
);
1858 int prev_is_range
= is_range_diff_range(prev
);
1861 strbuf_addstr(r1
, prev
);
1863 strbuf_addf(r1
, "%s..%s", head_oid
, prev
);
1866 strbuf_addf(r2
, "%s..%s", oid_to_hex(&origin
->object
.oid
), head_oid
);
1867 else if (prev_is_range
)
1868 die(_("failed to infer range-diff origin of current series"));
1870 warning(_("using '%s' as range-diff origin of current series"), prev
);
1871 strbuf_addf(r2
, "%s..%s", prev
, head_oid
);
1875 int cmd_format_patch(int argc
, const char **argv
, const char *prefix
)
1877 struct commit
*commit
;
1878 struct commit
**list
= NULL
;
1879 struct rev_info rev
;
1880 char *to_free
= NULL
;
1881 struct setup_revision_opt s_r_opt
;
1882 int nr
= 0, total
, i
;
1884 int start_number
= -1;
1885 int just_numbers
= 0;
1886 int ignore_if_in_upstream
= 0;
1887 int cover_letter
= -1;
1888 int boundary_count
= 0;
1889 int no_binary_diff
= 0;
1890 int zero_commit
= 0;
1891 struct commit
*origin
= NULL
;
1892 const char *in_reply_to
= NULL
;
1893 struct patch_ids ids
;
1894 struct strbuf buf
= STRBUF_INIT
;
1895 int use_patch_format
= 0;
1897 const char *reroll_count
= NULL
;
1898 char *cover_from_description_arg
= NULL
;
1899 char *branch_name
= NULL
;
1900 char *base_commit
= NULL
;
1901 struct base_tree_info bases
;
1902 struct commit
*base
;
1903 int show_progress
= 0;
1904 struct progress
*progress
= NULL
;
1905 struct oid_array idiff_prev
= OID_ARRAY_INIT
;
1906 struct strbuf idiff_title
= STRBUF_INIT
;
1907 const char *rdiff_prev
= NULL
;
1908 struct strbuf rdiff1
= STRBUF_INIT
;
1909 struct strbuf rdiff2
= STRBUF_INIT
;
1910 struct strbuf rdiff_title
= STRBUF_INIT
;
1911 struct strbuf sprefix
= STRBUF_INIT
;
1912 int creation_factor
= -1;
1914 const struct option builtin_format_patch_options
[] = {
1915 OPT_CALLBACK_F('n', "numbered", &numbered
, NULL
,
1916 N_("use [PATCH n/m] even with a single patch"),
1917 PARSE_OPT_NOARG
, numbered_callback
),
1918 OPT_CALLBACK_F('N', "no-numbered", &numbered
, NULL
,
1919 N_("use [PATCH] even with multiple patches"),
1920 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
, no_numbered_callback
),
1921 OPT_BOOL('s', "signoff", &do_signoff
, N_("add a Signed-off-by trailer")),
1922 OPT_BOOL(0, "stdout", &use_stdout
,
1923 N_("print patches to standard out")),
1924 OPT_BOOL(0, "cover-letter", &cover_letter
,
1925 N_("generate a cover letter")),
1926 OPT_BOOL(0, "numbered-files", &just_numbers
,
1927 N_("use simple number sequence for output file names")),
1928 OPT_STRING(0, "suffix", &fmt_patch_suffix
, N_("sfx"),
1929 N_("use <sfx> instead of '.patch'")),
1930 OPT_INTEGER(0, "start-number", &start_number
,
1931 N_("start numbering patches at <n> instead of 1")),
1932 OPT_STRING('v', "reroll-count", &reroll_count
, N_("reroll-count"),
1933 N_("mark the series as Nth re-roll")),
1934 OPT_INTEGER(0, "filename-max-length", &fmt_patch_name_max
,
1935 N_("max length of output filename")),
1936 OPT_CALLBACK_F(0, "rfc", &rev
, NULL
,
1937 N_("use [RFC PATCH] instead of [PATCH]"),
1938 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
, rfc_callback
),
1939 OPT_STRING(0, "cover-from-description", &cover_from_description_arg
,
1940 N_("cover-from-description-mode"),
1941 N_("generate parts of a cover letter based on a branch's description")),
1942 OPT_CALLBACK_F(0, "subject-prefix", &rev
, N_("prefix"),
1943 N_("use [<prefix>] instead of [PATCH]"),
1944 PARSE_OPT_NONEG
, subject_prefix_callback
),
1945 OPT_CALLBACK_F('o', "output-directory", &output_directory
,
1946 N_("dir"), N_("store resulting files in <dir>"),
1947 PARSE_OPT_NONEG
, output_directory_callback
),
1948 OPT_CALLBACK_F('k', "keep-subject", &rev
, NULL
,
1949 N_("don't strip/add [PATCH]"),
1950 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
, keep_callback
),
1951 OPT_BOOL(0, "no-binary", &no_binary_diff
,
1952 N_("don't output binary diffs")),
1953 OPT_BOOL(0, "zero-commit", &zero_commit
,
1954 N_("output all-zero hash in From header")),
1955 OPT_BOOL(0, "ignore-if-in-upstream", &ignore_if_in_upstream
,
1956 N_("don't include a patch matching a commit upstream")),
1957 OPT_SET_INT_F('p', "no-stat", &use_patch_format
,
1958 N_("show patch format instead of default (patch + stat)"),
1959 1, PARSE_OPT_NONEG
),
1960 OPT_GROUP(N_("Messaging")),
1961 OPT_CALLBACK(0, "add-header", NULL
, N_("header"),
1962 N_("add email header"), header_callback
),
1963 OPT_CALLBACK(0, "to", NULL
, N_("email"), N_("add To: header"), to_callback
),
1964 OPT_CALLBACK(0, "cc", NULL
, N_("email"), N_("add Cc: header"), cc_callback
),
1965 OPT_CALLBACK_F(0, "from", &from
, N_("ident"),
1966 N_("set From address to <ident> (or committer ident if absent)"),
1967 PARSE_OPT_OPTARG
, from_callback
),
1968 OPT_STRING(0, "in-reply-to", &in_reply_to
, N_("message-id"),
1969 N_("make first mail a reply to <message-id>")),
1970 OPT_CALLBACK_F(0, "attach", &rev
, N_("boundary"),
1971 N_("attach the patch"), PARSE_OPT_OPTARG
,
1973 OPT_CALLBACK_F(0, "inline", &rev
, N_("boundary"),
1974 N_("inline the patch"),
1975 PARSE_OPT_OPTARG
| PARSE_OPT_NONEG
,
1977 OPT_CALLBACK_F(0, "thread", &thread
, N_("style"),
1978 N_("enable message threading, styles: shallow, deep"),
1979 PARSE_OPT_OPTARG
, thread_callback
),
1980 OPT_STRING(0, "signature", &signature
, N_("signature"),
1981 N_("add a signature")),
1982 OPT_CALLBACK_F(0, "base", &base_commit
, N_("base-commit"),
1983 N_("add prerequisite tree info to the patch series"),
1985 OPT_FILENAME(0, "signature-file", &signature_file
,
1986 N_("add a signature from a file")),
1987 OPT__QUIET(&quiet
, N_("don't print the patch filenames")),
1988 OPT_BOOL(0, "progress", &show_progress
,
1989 N_("show progress while generating patches")),
1990 OPT_CALLBACK(0, "interdiff", &idiff_prev
, N_("rev"),
1991 N_("show changes against <rev> in cover letter or single patch"),
1992 parse_opt_object_name
),
1993 OPT_STRING(0, "range-diff", &rdiff_prev
, N_("refspec"),
1994 N_("show changes against <refspec> in cover letter or single patch")),
1995 OPT_INTEGER(0, "creation-factor", &creation_factor
,
1996 N_("percentage by which creation is weighted")),
1997 OPT_BOOL(0, "force-in-body-from", &force_in_body_from
,
1998 N_("show in-body From: even if identical to the e-mail header")),
2002 extra_hdr
.strdup_strings
= 1;
2003 extra_to
.strdup_strings
= 1;
2004 extra_cc
.strdup_strings
= 1;
2006 init_log_defaults();
2007 init_display_notes(¬es_opt
);
2008 git_config(git_format_config
, NULL
);
2009 repo_init_revisions(the_repository
, &rev
, prefix
);
2010 git_config(grep_config
, &rev
.grep_filter
);
2012 rev
.show_notes
= show_notes
;
2013 memcpy(&rev
.notes_opt
, ¬es_opt
, sizeof(notes_opt
));
2014 rev
.commit_format
= CMIT_FMT_EMAIL
;
2015 rev
.encode_email_headers
= default_encode_email_headers
;
2016 rev
.expand_tabs_in_log_default
= 0;
2017 rev
.verbose_header
= 1;
2019 rev
.max_parents
= 1;
2020 rev
.diffopt
.flags
.recursive
= 1;
2021 rev
.diffopt
.no_free
= 1;
2022 rev
.subject_prefix
= fmt_patch_subject_prefix
;
2023 memset(&s_r_opt
, 0, sizeof(s_r_opt
));
2024 s_r_opt
.def
= "HEAD";
2025 s_r_opt
.revarg_opt
= REVARG_COMMITTISH
;
2027 if (format_no_prefix
)
2028 diff_set_noprefix(&rev
.diffopt
);
2030 if (default_attach
) {
2031 rev
.mime_boundary
= default_attach
;
2036 * Parse the arguments before setup_revisions(), or something
2037 * like "git format-patch -o a123 HEAD^.." may fail; a123 is
2038 * possibly a valid SHA1.
2040 argc
= parse_options(argc
, argv
, prefix
, builtin_format_patch_options
,
2041 builtin_format_patch_usage
,
2042 PARSE_OPT_KEEP_ARGV0
| PARSE_OPT_KEEP_UNKNOWN_OPT
|
2043 PARSE_OPT_KEEP_DASHDASH
);
2045 rev
.force_in_body_from
= force_in_body_from
;
2047 /* Make sure "0000-$sub.patch" gives non-negative length for $sub */
2048 if (fmt_patch_name_max
<= strlen("0000-") + strlen(fmt_patch_suffix
))
2049 fmt_patch_name_max
= strlen("0000-") + strlen(fmt_patch_suffix
);
2051 if (cover_from_description_arg
)
2052 cover_from_description_mode
= parse_cover_from_description(cover_from_description_arg
);
2055 strbuf_addf(&sprefix
, "%s v%s",
2056 rev
.subject_prefix
, reroll_count
);
2057 rev
.reroll_count
= reroll_count
;
2058 rev
.subject_prefix
= sprefix
.buf
;
2061 for (i
= 0; i
< extra_hdr
.nr
; i
++) {
2062 strbuf_addstr(&buf
, extra_hdr
.items
[i
].string
);
2063 strbuf_addch(&buf
, '\n');
2067 strbuf_addstr(&buf
, "To: ");
2068 for (i
= 0; i
< extra_to
.nr
; i
++) {
2070 strbuf_addstr(&buf
, " ");
2071 strbuf_addstr(&buf
, extra_to
.items
[i
].string
);
2072 if (i
+ 1 < extra_to
.nr
)
2073 strbuf_addch(&buf
, ',');
2074 strbuf_addch(&buf
, '\n');
2078 strbuf_addstr(&buf
, "Cc: ");
2079 for (i
= 0; i
< extra_cc
.nr
; i
++) {
2081 strbuf_addstr(&buf
, " ");
2082 strbuf_addstr(&buf
, extra_cc
.items
[i
].string
);
2083 if (i
+ 1 < extra_cc
.nr
)
2084 strbuf_addch(&buf
, ',');
2085 strbuf_addch(&buf
, '\n');
2088 rev
.extra_headers
= to_free
= strbuf_detach(&buf
, NULL
);
2091 if (split_ident_line(&rev
.from_ident
, from
, strlen(from
)))
2092 die(_("invalid ident line: %s"), from
);
2095 if (start_number
< 0)
2099 * If numbered is set solely due to format.numbered in config,
2100 * and it would conflict with --keep-subject (-k) from the
2101 * command line, reset "numbered".
2103 if (numbered
&& keep_subject
&& !numbered_cmdline_opt
)
2106 if (numbered
&& keep_subject
)
2107 die(_("options '%s' and '%s' cannot be used together"), "-n", "-k");
2108 if (keep_subject
&& subject_prefix
)
2109 die(_("options '%s' and '%s' cannot be used together"), "--subject-prefix/--rfc", "-k");
2110 rev
.preserve_subject
= keep_subject
;
2112 argc
= setup_revisions(argc
, argv
, &rev
, &s_r_opt
);
2114 die(_("unrecognized argument: %s"), argv
[1]);
2116 if (rev
.diffopt
.output_format
& DIFF_FORMAT_NAME
)
2117 die(_("--name-only does not make sense"));
2118 if (rev
.diffopt
.output_format
& DIFF_FORMAT_NAME_STATUS
)
2119 die(_("--name-status does not make sense"));
2120 if (rev
.diffopt
.output_format
& DIFF_FORMAT_CHECKDIFF
)
2121 die(_("--check does not make sense"));
2122 if (rev
.remerge_diff
)
2123 die(_("--remerge-diff does not make sense"));
2125 if (!use_patch_format
&&
2126 (!rev
.diffopt
.output_format
||
2127 rev
.diffopt
.output_format
== DIFF_FORMAT_PATCH
))
2128 rev
.diffopt
.output_format
= DIFF_FORMAT_DIFFSTAT
| DIFF_FORMAT_SUMMARY
;
2129 if (!rev
.diffopt
.stat_width
)
2130 rev
.diffopt
.stat_width
= MAIL_DEFAULT_WRAP
;
2132 /* Always generate a patch */
2133 rev
.diffopt
.output_format
|= DIFF_FORMAT_PATCH
;
2134 rev
.always_show_header
= 1;
2136 rev
.zero_commit
= zero_commit
;
2137 rev
.patch_name_max
= fmt_patch_name_max
;
2139 if (!rev
.diffopt
.flags
.text
&& !no_binary_diff
)
2140 rev
.diffopt
.flags
.binary
= 1;
2143 load_display_notes(&rev
.notes_opt
);
2145 die_for_incompatible_opt3(use_stdout
, "--stdout",
2146 rev
.diffopt
.close_file
, "--output",
2147 !!output_directory
, "--output-directory");
2149 if (use_stdout
&& stdout_mboxrd
)
2150 rev
.commit_format
= CMIT_FMT_MBOXRD
;
2154 } else if (!rev
.diffopt
.close_file
) {
2157 if (!output_directory
)
2158 output_directory
= config_output_directory
;
2159 output_directory
= set_outdir(prefix
, output_directory
);
2161 if (rev
.diffopt
.use_color
!= GIT_COLOR_ALWAYS
)
2162 rev
.diffopt
.use_color
= GIT_COLOR_NEVER
;
2164 * We consider <outdir> as 'outside of gitdir', therefore avoid
2165 * applying adjust_shared_perm in s-c-l-d.
2167 saved
= get_shared_repository();
2168 set_shared_repository(0);
2169 switch (safe_create_leading_directories_const(output_directory
)) {
2174 die(_("could not create leading directories "
2175 "of '%s'"), output_directory
);
2177 set_shared_repository(saved
);
2178 if (mkdir(output_directory
, 0777) < 0 && errno
!= EEXIST
)
2179 die_errno(_("could not create directory '%s'"),
2183 if (rev
.pending
.nr
== 1) {
2186 if (rev
.max_count
< 0 && !rev
.show_root_diff
) {
2188 * This is traditional behaviour of "git format-patch
2189 * origin" that prepares what the origin side still
2192 rev
.pending
.objects
[0].item
->flags
|= UNINTERESTING
;
2193 add_head_to_pending(&rev
);
2197 * Otherwise, it is "format-patch -22 HEAD", and/or
2198 * "format-patch --root HEAD". The user wants
2199 * get_revision() to do the usual traversal.
2202 if (!strcmp(rev
.pending
.objects
[0].name
, "HEAD"))
2206 const char *ref
, *v
;
2207 ref
= resolve_ref_unsafe("HEAD", RESOLVE_REF_READING
,
2209 if (ref
&& skip_prefix(ref
, "refs/heads/", &v
))
2210 branch_name
= xstrdup(v
);
2212 branch_name
= xstrdup(""); /* no branch */
2217 * We cannot move this anywhere earlier because we do want to
2218 * know if --root was given explicitly from the command line.
2220 rev
.show_root_diff
= 1;
2222 if (ignore_if_in_upstream
) {
2223 /* Don't say anything if head and upstream are the same. */
2224 if (rev
.pending
.nr
== 2) {
2225 struct object_array_entry
*o
= rev
.pending
.objects
;
2226 if (oideq(&o
[0].item
->oid
, &o
[1].item
->oid
))
2229 get_patch_ids(&rev
, &ids
);
2232 if (prepare_revision_walk(&rev
))
2233 die(_("revision walk setup failed"));
2235 while ((commit
= get_revision(&rev
)) != NULL
) {
2236 if (commit
->object
.flags
& BOUNDARY
) {
2238 origin
= (boundary_count
== 1) ? commit
: NULL
;
2242 if (ignore_if_in_upstream
&& has_commit_patch_id(commit
, &ids
))
2246 REALLOC_ARRAY(list
, nr
);
2247 list
[nr
- 1] = commit
;
2253 if (cover_letter
== -1) {
2254 if (config_cover_letter
== COVER_AUTO
)
2255 cover_letter
= (total
> 1);
2257 cover_letter
= (config_cover_letter
== COVER_ON
);
2259 if (!keep_subject
&& auto_number
&& (total
> 1 || cover_letter
))
2262 rev
.total
= total
+ start_number
- 1;
2264 if (idiff_prev
.nr
) {
2265 if (!cover_letter
&& total
!= 1)
2266 die(_("--interdiff requires --cover-letter or single patch"));
2267 rev
.idiff_oid1
= &idiff_prev
.oid
[idiff_prev
.nr
- 1];
2268 rev
.idiff_oid2
= get_commit_tree_oid(list
[0]);
2269 rev
.idiff_title
= diff_title(&idiff_title
, reroll_count
,
2271 _("Interdiff against v%d:"));
2274 if (creation_factor
< 0)
2275 creation_factor
= RANGE_DIFF_CREATION_FACTOR_DEFAULT
;
2276 else if (!rdiff_prev
)
2277 die(_("the option '%s' requires '%s'"), "--creation-factor", "--range-diff");
2280 if (!cover_letter
&& total
!= 1)
2281 die(_("--range-diff requires --cover-letter or single patch"));
2283 infer_range_diff_ranges(&rdiff1
, &rdiff2
, rdiff_prev
,
2285 rev
.rdiff1
= rdiff1
.buf
;
2286 rev
.rdiff2
= rdiff2
.buf
;
2287 rev
.creation_factor
= creation_factor
;
2288 rev
.rdiff_title
= diff_title(&rdiff_title
, reroll_count
,
2290 _("Range-diff against v%d:"));
2294 ; /* --no-signature inhibits all signatures */
2295 } else if (signature
&& signature
!= git_version_string
) {
2296 ; /* non-default signature already set */
2297 } else if (signature_file
) {
2298 struct strbuf buf
= STRBUF_INIT
;
2300 if (strbuf_read_file(&buf
, signature_file
, 128) < 0)
2301 die_errno(_("unable to read signature file '%s'"), signature_file
);
2302 signature
= strbuf_detach(&buf
, NULL
);
2305 memset(&bases
, 0, sizeof(bases
));
2306 base
= get_base_commit(base_commit
, list
, nr
);
2308 reset_revision_walk();
2309 clear_object_flags(UNINTERESTING
);
2310 prepare_bases(&bases
, base
, list
, nr
);
2313 if (in_reply_to
|| thread
|| cover_letter
) {
2314 rev
.ref_message_ids
= xmalloc(sizeof(*rev
.ref_message_ids
));
2315 string_list_init_dup(rev
.ref_message_ids
);
2318 char *msgid
= clean_message_id(in_reply_to
);
2319 string_list_append_nodup(rev
.ref_message_ids
, msgid
);
2321 rev
.numbered_files
= just_numbers
;
2322 rev
.patch_suffix
= fmt_patch_suffix
;
2325 gen_message_id(&rev
, "cover");
2326 make_cover_letter(&rev
, !!output_directory
,
2327 origin
, nr
, list
, branch_name
, quiet
);
2328 print_bases(&bases
, rev
.diffopt
.file
);
2329 print_signature(rev
.diffopt
.file
);
2332 /* interdiff/range-diff in cover-letter; omit from patches */
2333 rev
.idiff_oid1
= NULL
;
2336 rev
.add_signoff
= do_signoff
;
2339 progress
= start_delayed_progress(_("Generating patches"), total
);
2342 display_progress(progress
, total
- nr
);
2344 rev
.nr
= total
- nr
+ (start_number
- 1);
2345 /* Make the second and subsequent mails replies to the first */
2347 /* Have we already had a message ID? */
2348 if (rev
.message_id
) {
2350 * For deep threading: make every mail
2351 * a reply to the previous one, no
2352 * matter what other options are set.
2354 * For shallow threading:
2356 * Without --cover-letter and
2357 * --in-reply-to, make every mail a
2358 * reply to the one before.
2360 * With --in-reply-to but no
2361 * --cover-letter, make every mail a
2362 * reply to the <reply-to>.
2364 * With --cover-letter, make every
2365 * mail but the cover letter a reply
2366 * to the cover letter. The cover
2367 * letter is a reply to the
2368 * --in-reply-to, if specified.
2370 if (thread
== THREAD_SHALLOW
2371 && rev
.ref_message_ids
->nr
> 0
2372 && (!cover_letter
|| rev
.nr
> 1))
2373 free(rev
.message_id
);
2375 string_list_append_nodup(rev
.ref_message_ids
,
2378 gen_message_id(&rev
, oid_to_hex(&commit
->object
.oid
));
2381 if (output_directory
&&
2382 open_next_file(rev
.numbered_files
? NULL
: commit
, NULL
, &rev
, quiet
))
2383 die(_("failed to create output files"));
2384 shown
= log_tree_commit(&rev
, commit
);
2385 free_commit_buffer(the_repository
->parsed_objects
,
2388 /* We put one extra blank line between formatted
2389 * patches and this flag is used by log-tree code
2390 * to see if it needs to emit a LF before showing
2391 * the log; when using one file per patch, we do
2392 * not want the extra blank line.
2394 if (output_directory
)
2397 print_bases(&bases
, rev
.diffopt
.file
);
2398 if (rev
.mime_boundary
)
2399 fprintf(rev
.diffopt
.file
, "\n--%s%s--\n\n\n",
2400 mime_boundary_leader
,
2403 print_signature(rev
.diffopt
.file
);
2405 if (output_directory
)
2406 fclose(rev
.diffopt
.file
);
2408 stop_progress(&progress
);
2411 string_list_clear(&extra_to
, 0);
2412 string_list_clear(&extra_cc
, 0);
2413 string_list_clear(&extra_hdr
, 0);
2414 if (ignore_if_in_upstream
)
2415 free_patch_ids(&ids
);
2418 oid_array_clear(&idiff_prev
);
2419 strbuf_release(&idiff_title
);
2420 strbuf_release(&rdiff1
);
2421 strbuf_release(&rdiff2
);
2422 strbuf_release(&rdiff_title
);
2423 strbuf_release(&sprefix
);
2425 free(rev
.message_id
);
2426 if (rev
.ref_message_ids
)
2427 string_list_clear(rev
.ref_message_ids
, 0);
2428 free(rev
.ref_message_ids
);
2429 return cmd_log_deinit(0, &rev
);
2432 static int add_pending_commit(const char *arg
, struct rev_info
*revs
, int flags
)
2434 struct object_id oid
;
2435 if (repo_get_oid(the_repository
, arg
, &oid
) == 0) {
2436 struct commit
*commit
= lookup_commit_reference(the_repository
,
2439 commit
->object
.flags
|= flags
;
2440 add_pending_object(revs
, &commit
->object
, arg
);
2447 static const char * const cherry_usage
[] = {
2448 N_("git cherry [-v] [<upstream> [<head> [<limit>]]]"),
2452 static void print_commit(char sign
, struct commit
*commit
, int verbose
,
2453 int abbrev
, FILE *file
)
2456 fprintf(file
, "%c %s\n", sign
,
2457 repo_find_unique_abbrev(the_repository
, &commit
->object
.oid
, abbrev
));
2459 struct strbuf buf
= STRBUF_INIT
;
2460 pp_commit_easy(CMIT_FMT_ONELINE
, commit
, &buf
);
2461 fprintf(file
, "%c %s %s\n", sign
,
2462 repo_find_unique_abbrev(the_repository
, &commit
->object
.oid
, abbrev
),
2464 strbuf_release(&buf
);
2468 int cmd_cherry(int argc
, const char **argv
, const char *prefix
)
2470 struct rev_info revs
;
2471 struct patch_ids ids
;
2472 struct commit
*commit
;
2473 struct commit_list
*list
= NULL
;
2474 struct branch
*current_branch
;
2475 const char *upstream
;
2476 const char *head
= "HEAD";
2477 const char *limit
= NULL
;
2478 int verbose
= 0, abbrev
= 0;
2480 struct option options
[] = {
2481 OPT__ABBREV(&abbrev
),
2482 OPT__VERBOSE(&verbose
, N_("be verbose")),
2486 argc
= parse_options(argc
, argv
, prefix
, options
, cherry_usage
, 0);
2499 current_branch
= branch_get(NULL
);
2500 upstream
= branch_get_upstream(current_branch
, NULL
);
2502 fprintf(stderr
, _("Could not find a tracked"
2503 " remote branch, please"
2504 " specify <upstream> manually.\n"));
2505 usage_with_options(cherry_usage
, options
);
2509 repo_init_revisions(the_repository
, &revs
, prefix
);
2510 revs
.max_parents
= 1;
2512 if (add_pending_commit(head
, &revs
, 0))
2513 die(_("unknown commit %s"), head
);
2514 if (add_pending_commit(upstream
, &revs
, UNINTERESTING
))
2515 die(_("unknown commit %s"), upstream
);
2517 /* Don't say anything if head and upstream are the same. */
2518 if (revs
.pending
.nr
== 2) {
2519 struct object_array_entry
*o
= revs
.pending
.objects
;
2520 if (oideq(&o
[0].item
->oid
, &o
[1].item
->oid
))
2524 get_patch_ids(&revs
, &ids
);
2526 if (limit
&& add_pending_commit(limit
, &revs
, UNINTERESTING
))
2527 die(_("unknown commit %s"), limit
);
2529 /* reverse the list of commits */
2530 if (prepare_revision_walk(&revs
))
2531 die(_("revision walk setup failed"));
2532 while ((commit
= get_revision(&revs
)) != NULL
) {
2533 commit_list_insert(commit
, &list
);
2539 commit
= list
->item
;
2540 if (has_commit_patch_id(commit
, &ids
))
2542 print_commit(sign
, commit
, verbose
, abbrev
, revs
.diffopt
.file
);
2546 free_patch_ids(&ids
);