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
, void *cb
)
569 const char *slot_name
;
571 if (!strcmp(var
, "format.pretty"))
572 return git_config_string(&fmt_pretty
, var
, value
);
573 if (!strcmp(var
, "format.subjectprefix"))
574 return git_config_string(&fmt_patch_subject_prefix
, var
, value
);
575 if (!strcmp(var
, "format.filenamemaxlength")) {
576 fmt_patch_name_max
= git_config_int(var
, value
);
579 if (!strcmp(var
, "format.encodeemailheaders")) {
580 default_encode_email_headers
= git_config_bool(var
, value
);
583 if (!strcmp(var
, "log.abbrevcommit")) {
584 default_abbrev_commit
= git_config_bool(var
, value
);
587 if (!strcmp(var
, "log.date"))
588 return git_config_string(&default_date_mode
, var
, value
);
589 if (!strcmp(var
, "log.decorate")) {
590 decoration_style
= parse_decoration_style(value
);
591 if (decoration_style
< 0)
592 decoration_style
= 0; /* maybe warn? */
595 if (!strcmp(var
, "log.diffmerges"))
596 return diff_merges_config(value
);
597 if (!strcmp(var
, "log.showroot")) {
598 default_show_root
= git_config_bool(var
, value
);
601 if (!strcmp(var
, "log.follow")) {
602 default_follow
= git_config_bool(var
, value
);
605 if (skip_prefix(var
, "color.decorate.", &slot_name
))
606 return parse_decorate_color_config(var
, slot_name
, value
);
607 if (!strcmp(var
, "log.mailmap")) {
608 use_mailmap_config
= git_config_bool(var
, value
);
611 if (!strcmp(var
, "log.showsignature")) {
612 default_show_signature
= git_config_bool(var
, value
);
616 return git_diff_ui_config(var
, value
, cb
);
619 int cmd_whatchanged(int argc
, const char **argv
, const char *prefix
)
622 struct setup_revision_opt opt
;
625 git_config(git_log_config
, NULL
);
627 repo_init_revisions(the_repository
, &rev
, prefix
);
628 git_config(grep_config
, &rev
.grep_filter
);
631 rev
.simplify_history
= 0;
632 memset(&opt
, 0, sizeof(opt
));
634 opt
.revarg_opt
= REVARG_COMMITTISH
;
635 cmd_log_init(argc
, argv
, prefix
, &rev
, &opt
);
636 if (!rev
.diffopt
.output_format
)
637 rev
.diffopt
.output_format
= DIFF_FORMAT_RAW
;
638 return cmd_log_deinit(cmd_log_walk(&rev
), &rev
);
641 static void show_tagger(const char *buf
, struct rev_info
*rev
)
643 struct strbuf out
= STRBUF_INIT
;
644 struct pretty_print_context pp
= {0};
646 pp
.fmt
= rev
->commit_format
;
647 pp
.date_mode
= rev
->date_mode
;
648 pp_user_info(&pp
, "Tagger", &out
, buf
, get_log_output_encoding());
649 fprintf(rev
->diffopt
.file
, "%s", out
.buf
);
650 strbuf_release(&out
);
653 static int show_blob_object(const struct object_id
*oid
, struct rev_info
*rev
, const char *obj_name
)
655 struct object_id oidc
;
656 struct object_context obj_context
;
660 fflush(rev
->diffopt
.file
);
661 if (!rev
->diffopt
.flags
.textconv_set_via_cmdline
||
662 !rev
->diffopt
.flags
.allow_textconv
)
663 return stream_blob_to_fd(1, oid
, NULL
, 0);
665 if (get_oid_with_context(the_repository
, obj_name
,
667 &oidc
, &obj_context
))
668 die(_("not a valid object name %s"), obj_name
);
669 if (!obj_context
.path
||
670 !textconv_object(the_repository
, obj_context
.path
,
671 obj_context
.mode
, &oidc
, 1, &buf
, &size
)) {
672 free(obj_context
.path
);
673 return stream_blob_to_fd(1, oid
, NULL
, 0);
677 die(_("git show %s: bad file"), obj_name
);
679 write_or_die(1, buf
, size
);
680 free(obj_context
.path
);
684 static int show_tag_object(const struct object_id
*oid
, struct rev_info
*rev
)
687 enum object_type type
;
688 char *buf
= repo_read_object_file(the_repository
, oid
, &type
, &size
);
692 return error(_("could not read object %s"), oid_to_hex(oid
));
694 assert(type
== OBJ_TAG
);
695 while (offset
< size
&& buf
[offset
] != '\n') {
696 int new_offset
= offset
+ 1;
698 while (new_offset
< size
&& buf
[new_offset
++] != '\n')
700 if (skip_prefix(buf
+ offset
, "tagger ", &ident
))
701 show_tagger(ident
, rev
);
706 fwrite(buf
+ offset
, size
- offset
, 1, rev
->diffopt
.file
);
711 static int show_tree_object(const struct object_id
*oid UNUSED
,
712 struct strbuf
*base UNUSED
,
713 const char *pathname
, unsigned mode
,
716 FILE *file
= context
;
717 fprintf(file
, "%s%s\n", pathname
, S_ISDIR(mode
) ? "/" : "");
721 static void show_setup_revisions_tweak(struct rev_info
*rev
,
722 struct setup_revision_opt
*opt
)
724 if (rev
->first_parent_only
)
725 diff_merges_default_to_first_parent(rev
);
727 diff_merges_default_to_dense_combined(rev
);
728 if (!rev
->diffopt
.output_format
)
729 rev
->diffopt
.output_format
= DIFF_FORMAT_PATCH
;
732 int cmd_show(int argc
, const char **argv
, const char *prefix
)
736 struct setup_revision_opt opt
;
737 struct pathspec match_all
;
741 git_config(git_log_config
, NULL
);
743 if (the_repository
->gitdir
) {
744 prepare_repo_settings(the_repository
);
745 the_repository
->settings
.command_requires_full_index
= 0;
748 memset(&match_all
, 0, sizeof(match_all
));
749 repo_init_revisions(the_repository
, &rev
, prefix
);
750 git_config(grep_config
, &rev
.grep_filter
);
753 rev
.always_show_header
= 1;
755 rev
.diffopt
.stat_width
= -1; /* Scale to real terminal size */
757 memset(&opt
, 0, sizeof(opt
));
759 opt
.tweak
= show_setup_revisions_tweak
;
760 cmd_log_init(argc
, argv
, prefix
, &rev
, &opt
);
763 return cmd_log_deinit(cmd_log_walk(&rev
), &rev
);
765 rev
.diffopt
.no_free
= 1;
766 for (i
= 0; i
< rev
.pending
.nr
&& !ret
; i
++) {
767 struct object
*o
= rev
.pending
.objects
[i
].item
;
768 const char *name
= rev
.pending
.objects
[i
].name
;
771 ret
= show_blob_object(&o
->oid
, &rev
, name
);
774 struct tag
*t
= (struct tag
*)o
;
775 struct object_id
*oid
= get_tagged_oid(t
);
779 fprintf(rev
.diffopt
.file
, "%stag %s%s\n",
780 diff_get_color_opt(&rev
.diffopt
, DIFF_COMMIT
),
782 diff_get_color_opt(&rev
.diffopt
, DIFF_RESET
));
783 ret
= show_tag_object(&o
->oid
, &rev
);
787 o
= parse_object(the_repository
, oid
);
789 ret
= error(_("could not read object %s"),
791 rev
.pending
.objects
[i
].item
= o
;
798 fprintf(rev
.diffopt
.file
, "%stree %s%s\n\n",
799 diff_get_color_opt(&rev
.diffopt
, DIFF_COMMIT
),
801 diff_get_color_opt(&rev
.diffopt
, DIFF_RESET
));
802 read_tree(the_repository
, (struct tree
*)o
,
803 &match_all
, show_tree_object
,
809 struct object_array old
;
810 struct object_array blank
= OBJECT_ARRAY_INIT
;
812 memcpy(&old
, &rev
.pending
, sizeof(old
));
813 memcpy(&rev
.pending
, &blank
, sizeof(rev
.pending
));
815 add_object_array(o
, name
, &rev
.pending
);
816 ret
= cmd_log_walk_no_free(&rev
);
820 * object_array_clear(&pending). It was
821 * cleared already in prepare_revision_walk()
823 memcpy(&rev
.pending
, &old
, sizeof(rev
.pending
));
827 ret
= error(_("unknown type: %d"), o
->type
);
831 rev
.diffopt
.no_free
= 0;
832 diff_free(&rev
.diffopt
);
834 return cmd_log_deinit(ret
, &rev
);
838 * This is equivalent to "git log -g --abbrev-commit --pretty=oneline"
840 int cmd_log_reflog(int argc
, const char **argv
, const char *prefix
)
843 struct setup_revision_opt opt
;
846 git_config(git_log_config
, NULL
);
848 repo_init_revisions(the_repository
, &rev
, prefix
);
849 init_reflog_walk(&rev
.reflog_info
);
850 git_config(grep_config
, &rev
.grep_filter
);
852 rev
.verbose_header
= 1;
853 memset(&opt
, 0, sizeof(opt
));
855 cmd_log_init_defaults(&rev
);
856 rev
.abbrev_commit
= 1;
857 rev
.commit_format
= CMIT_FMT_ONELINE
;
858 rev
.use_terminator
= 1;
859 rev
.always_show_header
= 1;
860 cmd_log_init_finish(argc
, argv
, prefix
, &rev
, &opt
);
862 return cmd_log_deinit(cmd_log_walk(&rev
), &rev
);
865 static void log_setup_revisions_tweak(struct rev_info
*rev
,
866 struct setup_revision_opt
*opt
)
868 if (rev
->diffopt
.flags
.default_follow_renames
&&
869 rev
->prune_data
.nr
== 1)
870 rev
->diffopt
.flags
.follow_renames
= 1;
872 if (rev
->first_parent_only
)
873 diff_merges_default_to_first_parent(rev
);
876 int cmd_log(int argc
, const char **argv
, const char *prefix
)
879 struct setup_revision_opt opt
;
882 git_config(git_log_config
, NULL
);
884 repo_init_revisions(the_repository
, &rev
, prefix
);
885 git_config(grep_config
, &rev
.grep_filter
);
887 rev
.always_show_header
= 1;
888 memset(&opt
, 0, sizeof(opt
));
890 opt
.revarg_opt
= REVARG_COMMITTISH
;
891 opt
.tweak
= log_setup_revisions_tweak
;
892 cmd_log_init(argc
, argv
, prefix
, &rev
, &opt
);
893 return cmd_log_deinit(cmd_log_walk(&rev
), &rev
);
898 static const char *fmt_patch_suffix
= ".patch";
899 static int numbered
= 0;
900 static int auto_number
= 1;
902 static char *default_attach
= NULL
;
904 static struct string_list extra_hdr
= STRING_LIST_INIT_NODUP
;
905 static struct string_list extra_to
= STRING_LIST_INIT_NODUP
;
906 static struct string_list extra_cc
= STRING_LIST_INIT_NODUP
;
908 static void add_header(const char *value
)
910 struct string_list_item
*item
;
911 int len
= strlen(value
);
912 while (len
&& value
[len
- 1] == '\n')
915 if (!strncasecmp(value
, "to: ", 4)) {
916 item
= string_list_append(&extra_to
, value
+ 4);
918 } else if (!strncasecmp(value
, "cc: ", 4)) {
919 item
= string_list_append(&extra_cc
, value
+ 4);
922 item
= string_list_append(&extra_hdr
, value
);
925 item
->string
[len
] = '\0';
941 enum cover_from_description
{
948 enum auto_base_setting
{
954 static enum thread_level thread
;
955 static int do_signoff
;
956 static enum auto_base_setting auto_base
;
958 static const char *signature
= git_version_string
;
959 static const char *signature_file
;
960 static enum cover_setting config_cover_letter
;
961 static const char *config_output_directory
;
962 static enum cover_from_description cover_from_description_mode
= COVER_FROM_MESSAGE
;
963 static int show_notes
;
964 static struct display_notes_opt notes_opt
;
966 static enum cover_from_description
parse_cover_from_description(const char *arg
)
968 if (!arg
|| !strcmp(arg
, "default"))
969 return COVER_FROM_MESSAGE
;
970 else if (!strcmp(arg
, "none"))
971 return COVER_FROM_NONE
;
972 else if (!strcmp(arg
, "message"))
973 return COVER_FROM_MESSAGE
;
974 else if (!strcmp(arg
, "subject"))
975 return COVER_FROM_SUBJECT
;
976 else if (!strcmp(arg
, "auto"))
977 return COVER_FROM_AUTO
;
979 die(_("%s: invalid cover from description mode"), arg
);
982 static int git_format_config(const char *var
, const char *value
, void *cb
)
984 if (!strcmp(var
, "format.headers")) {
986 die(_("format.headers without value"));
990 if (!strcmp(var
, "format.suffix"))
991 return git_config_string(&fmt_patch_suffix
, var
, value
);
992 if (!strcmp(var
, "format.to")) {
994 return config_error_nonbool(var
);
995 string_list_append(&extra_to
, value
);
998 if (!strcmp(var
, "format.cc")) {
1000 return config_error_nonbool(var
);
1001 string_list_append(&extra_cc
, value
);
1004 if (!strcmp(var
, "diff.color") || !strcmp(var
, "color.diff") ||
1005 !strcmp(var
, "color.ui") || !strcmp(var
, "diff.submodule")) {
1008 if (!strcmp(var
, "format.numbered")) {
1009 if (value
&& !strcasecmp(value
, "auto")) {
1013 numbered
= git_config_bool(var
, value
);
1014 auto_number
= auto_number
&& numbered
;
1017 if (!strcmp(var
, "format.attach")) {
1018 if (value
&& *value
)
1019 default_attach
= xstrdup(value
);
1020 else if (value
&& !*value
)
1021 FREE_AND_NULL(default_attach
);
1023 default_attach
= xstrdup(git_version_string
);
1026 if (!strcmp(var
, "format.thread")) {
1027 if (value
&& !strcasecmp(value
, "deep")) {
1028 thread
= THREAD_DEEP
;
1031 if (value
&& !strcasecmp(value
, "shallow")) {
1032 thread
= THREAD_SHALLOW
;
1035 thread
= git_config_bool(var
, value
) ? THREAD_SHALLOW
: THREAD_UNSET
;
1038 if (!strcmp(var
, "format.signoff")) {
1039 do_signoff
= git_config_bool(var
, value
);
1042 if (!strcmp(var
, "format.signature"))
1043 return git_config_string(&signature
, var
, value
);
1044 if (!strcmp(var
, "format.signaturefile"))
1045 return git_config_pathname(&signature_file
, var
, value
);
1046 if (!strcmp(var
, "format.coverletter")) {
1047 if (value
&& !strcasecmp(value
, "auto")) {
1048 config_cover_letter
= COVER_AUTO
;
1051 config_cover_letter
= git_config_bool(var
, value
) ? COVER_ON
: COVER_OFF
;
1054 if (!strcmp(var
, "format.outputdirectory"))
1055 return git_config_string(&config_output_directory
, var
, value
);
1056 if (!strcmp(var
, "format.useautobase")) {
1057 if (value
&& !strcasecmp(value
, "whenAble")) {
1058 auto_base
= AUTO_BASE_WHEN_ABLE
;
1061 auto_base
= git_config_bool(var
, value
) ? AUTO_BASE_ALWAYS
: AUTO_BASE_NEVER
;
1064 if (!strcmp(var
, "format.from")) {
1065 int b
= git_parse_maybe_bool(value
);
1068 from
= xstrdup(value
);
1070 from
= xstrdup(git_committer_info(IDENT_NO_DATE
));
1075 if (!strcmp(var
, "format.forceinbodyfrom")) {
1076 force_in_body_from
= git_config_bool(var
, value
);
1079 if (!strcmp(var
, "format.notes")) {
1080 int b
= git_parse_maybe_bool(value
);
1082 enable_ref_display_notes(¬es_opt
, &show_notes
, value
);
1084 enable_default_display_notes(¬es_opt
, &show_notes
);
1086 disable_display_notes(¬es_opt
, &show_notes
);
1089 if (!strcmp(var
, "format.coverfromdescription")) {
1090 cover_from_description_mode
= parse_cover_from_description(value
);
1093 if (!strcmp(var
, "format.mboxrd")) {
1094 stdout_mboxrd
= git_config_bool(var
, value
);
1097 if (!strcmp(var
, "format.noprefix")) {
1098 format_no_prefix
= 1;
1103 * ignore some porcelain config which would otherwise be parsed by
1104 * git_diff_ui_config(), via git_log_config(); we can't just avoid
1105 * diff_ui_config completely, because we do care about some ui options
1108 if (!strcmp(var
, "diff.noprefix"))
1111 return git_log_config(var
, value
, cb
);
1114 static const char *output_directory
= NULL
;
1115 static int outdir_offset
;
1117 static int open_next_file(struct commit
*commit
, const char *subject
,
1118 struct rev_info
*rev
, int quiet
)
1120 struct strbuf filename
= STRBUF_INIT
;
1122 if (output_directory
) {
1123 strbuf_addstr(&filename
, output_directory
);
1124 strbuf_complete(&filename
, '/');
1127 if (rev
->numbered_files
)
1128 strbuf_addf(&filename
, "%d", rev
->nr
);
1130 fmt_output_commit(&filename
, commit
, rev
);
1132 fmt_output_subject(&filename
, subject
, rev
);
1135 printf("%s\n", filename
.buf
+ outdir_offset
);
1137 if (!(rev
->diffopt
.file
= fopen(filename
.buf
, "w"))) {
1138 error_errno(_("cannot open patch file %s"), filename
.buf
);
1139 strbuf_release(&filename
);
1143 strbuf_release(&filename
);
1147 static void get_patch_ids(struct rev_info
*rev
, struct patch_ids
*ids
)
1149 struct rev_info check_rev
;
1150 struct commit
*commit
, *c1
, *c2
;
1151 struct object
*o1
, *o2
;
1152 unsigned flags1
, flags2
;
1154 if (rev
->pending
.nr
!= 2)
1155 die(_("need exactly one range"));
1157 o1
= rev
->pending
.objects
[0].item
;
1158 o2
= rev
->pending
.objects
[1].item
;
1161 c1
= lookup_commit_reference(the_repository
, &o1
->oid
);
1162 c2
= lookup_commit_reference(the_repository
, &o2
->oid
);
1164 if ((flags1
& UNINTERESTING
) == (flags2
& UNINTERESTING
))
1165 die(_("not a range"));
1167 init_patch_ids(the_repository
, ids
);
1169 /* given a range a..b get all patch ids for b..a */
1170 repo_init_revisions(the_repository
, &check_rev
, rev
->prefix
);
1171 check_rev
.max_parents
= 1;
1172 o1
->flags
^= UNINTERESTING
;
1173 o2
->flags
^= UNINTERESTING
;
1174 add_pending_object(&check_rev
, o1
, "o1");
1175 add_pending_object(&check_rev
, o2
, "o2");
1176 if (prepare_revision_walk(&check_rev
))
1177 die(_("revision walk setup failed"));
1179 while ((commit
= get_revision(&check_rev
)) != NULL
) {
1180 add_commit_patch_id(commit
, ids
);
1183 /* reset for next revision walk */
1184 clear_commit_marks(c1
, SEEN
| UNINTERESTING
| SHOWN
| ADDED
);
1185 clear_commit_marks(c2
, SEEN
| UNINTERESTING
| SHOWN
| ADDED
);
1190 static void gen_message_id(struct rev_info
*info
, char *base
)
1192 struct strbuf buf
= STRBUF_INIT
;
1193 strbuf_addf(&buf
, "%s.%"PRItime
".git.%s", base
,
1194 (timestamp_t
) time(NULL
),
1195 git_committer_info(IDENT_NO_NAME
|IDENT_NO_DATE
|IDENT_STRICT
));
1196 info
->message_id
= strbuf_detach(&buf
, NULL
);
1199 static void print_signature(FILE *file
)
1201 if (!signature
|| !*signature
)
1204 fprintf(file
, "-- \n%s", signature
);
1205 if (signature
[strlen(signature
)-1] != '\n')
1210 static char *find_branch_name(struct rev_info
*rev
)
1212 int i
, positive
= -1;
1213 struct object_id branch_oid
;
1214 const struct object_id
*tip_oid
;
1215 const char *ref
, *v
;
1216 char *full_ref
, *branch
= NULL
;
1218 for (i
= 0; i
< rev
->cmdline
.nr
; i
++) {
1219 if (rev
->cmdline
.rev
[i
].flags
& UNINTERESTING
)
1228 ref
= rev
->cmdline
.rev
[positive
].name
;
1229 tip_oid
= &rev
->cmdline
.rev
[positive
].item
->oid
;
1230 if (repo_dwim_ref(the_repository
, ref
, strlen(ref
), &branch_oid
,
1232 skip_prefix(full_ref
, "refs/heads/", &v
) &&
1233 oideq(tip_oid
, &branch_oid
))
1234 branch
= xstrdup(v
);
1239 static void show_diffstat(struct rev_info
*rev
,
1240 struct commit
*origin
, struct commit
*head
)
1242 struct diff_options opts
;
1244 memcpy(&opts
, &rev
->diffopt
, sizeof(opts
));
1245 opts
.output_format
= DIFF_FORMAT_SUMMARY
| DIFF_FORMAT_DIFFSTAT
;
1246 diff_setup_done(&opts
);
1248 diff_tree_oid(get_commit_tree_oid(origin
),
1249 get_commit_tree_oid(head
),
1251 diffcore_std(&opts
);
1254 fprintf(rev
->diffopt
.file
, "\n");
1257 static void prepare_cover_text(struct pretty_print_context
*pp
,
1258 const char *branch_name
,
1260 const char *encoding
,
1263 const char *subject
= "*** SUBJECT HERE ***";
1264 const char *body
= "*** BLURB HERE ***";
1265 struct strbuf description_sb
= STRBUF_INIT
;
1266 struct strbuf subject_sb
= STRBUF_INIT
;
1268 if (cover_from_description_mode
== COVER_FROM_NONE
)
1271 if (branch_name
&& *branch_name
)
1272 read_branch_desc(&description_sb
, branch_name
);
1273 if (!description_sb
.len
)
1276 if (cover_from_description_mode
== COVER_FROM_SUBJECT
||
1277 cover_from_description_mode
== COVER_FROM_AUTO
)
1278 body
= format_subject(&subject_sb
, description_sb
.buf
, " ");
1280 if (cover_from_description_mode
== COVER_FROM_MESSAGE
||
1281 (cover_from_description_mode
== COVER_FROM_AUTO
&&
1282 subject_sb
.len
> COVER_FROM_AUTO_MAX_SUBJECT_LEN
))
1283 body
= description_sb
.buf
;
1285 subject
= subject_sb
.buf
;
1288 pp_title_line(pp
, &subject
, sb
, encoding
, need_8bit_cte
);
1289 pp_remainder(pp
, &body
, sb
, 0);
1291 strbuf_release(&description_sb
);
1292 strbuf_release(&subject_sb
);
1295 static int get_notes_refs(struct string_list_item
*item
, void *arg
)
1297 strvec_pushf(arg
, "--notes=%s", item
->string
);
1301 static void get_notes_args(struct strvec
*arg
, struct rev_info
*rev
)
1303 if (!rev
->show_notes
) {
1304 strvec_push(arg
, "--no-notes");
1305 } else if (rev
->notes_opt
.use_default_notes
> 0 ||
1306 (rev
->notes_opt
.use_default_notes
== -1 &&
1307 !rev
->notes_opt
.extra_notes_refs
.nr
)) {
1308 strvec_push(arg
, "--notes");
1310 for_each_string_list(&rev
->notes_opt
.extra_notes_refs
, get_notes_refs
, arg
);
1314 static void make_cover_letter(struct rev_info
*rev
, int use_separate_file
,
1315 struct commit
*origin
,
1316 int nr
, struct commit
**list
,
1317 const char *branch_name
,
1320 const char *committer
;
1321 struct shortlog log
;
1322 struct strbuf sb
= STRBUF_INIT
;
1324 const char *encoding
= "UTF-8";
1325 int need_8bit_cte
= 0;
1326 struct pretty_print_context pp
= {0};
1327 struct commit
*head
= list
[0];
1329 if (!cmit_fmt_is_mail(rev
->commit_format
))
1330 die(_("cover letter needs email format"));
1332 committer
= git_committer_info(0);
1334 if (use_separate_file
&&
1335 open_next_file(NULL
, rev
->numbered_files
? NULL
: "cover-letter", rev
, quiet
))
1336 die(_("failed to create cover-letter file"));
1338 log_write_email_headers(rev
, head
, &pp
.after_subject
, &need_8bit_cte
, 0);
1340 for (i
= 0; !need_8bit_cte
&& i
< nr
; i
++) {
1341 const char *buf
= repo_get_commit_buffer(the_repository
,
1343 if (has_non_ascii(buf
))
1345 repo_unuse_commit_buffer(the_repository
, list
[i
], buf
);
1349 branch_name
= find_branch_name(rev
);
1351 pp
.fmt
= CMIT_FMT_EMAIL
;
1352 pp
.date_mode
.type
= DATE_RFC2822
;
1354 pp
.print_email_subject
= 1;
1355 pp_user_info(&pp
, NULL
, &sb
, committer
, encoding
);
1356 prepare_cover_text(&pp
, branch_name
, &sb
, encoding
, need_8bit_cte
);
1357 fprintf(rev
->diffopt
.file
, "%s\n", sb
.buf
);
1359 strbuf_release(&sb
);
1361 shortlog_init(&log
);
1363 log
.wrap
= MAIL_DEFAULT_WRAP
;
1366 log
.file
= rev
->diffopt
.file
;
1367 log
.groups
= SHORTLOG_GROUP_AUTHOR
;
1368 shortlog_finish_setup(&log
);
1369 for (i
= 0; i
< nr
; i
++)
1370 shortlog_add_commit(&log
, list
[i
]);
1372 shortlog_output(&log
);
1374 /* We can only do diffstat with a unique reference point */
1376 show_diffstat(rev
, origin
, head
);
1378 if (rev
->idiff_oid1
) {
1379 fprintf_ln(rev
->diffopt
.file
, "%s", rev
->idiff_title
);
1380 show_interdiff(rev
->idiff_oid1
, rev
->idiff_oid2
, 0,
1386 * Pass minimum required diff-options to range-diff; others
1387 * can be added later if deemed desirable.
1389 struct diff_options opts
;
1390 struct strvec other_arg
= STRVEC_INIT
;
1391 struct range_diff_options range_diff_opts
= {
1392 .creation_factor
= rev
->creation_factor
,
1395 .other_arg
= &other_arg
1398 repo_diff_setup(the_repository
, &opts
);
1399 opts
.file
= rev
->diffopt
.file
;
1400 opts
.use_color
= rev
->diffopt
.use_color
;
1401 diff_setup_done(&opts
);
1402 fprintf_ln(rev
->diffopt
.file
, "%s", rev
->rdiff_title
);
1403 get_notes_args(&other_arg
, rev
);
1404 show_range_diff(rev
->rdiff1
, rev
->rdiff2
, &range_diff_opts
);
1405 strvec_clear(&other_arg
);
1409 static const char *clean_message_id(const char *msg_id
)
1412 const char *a
, *z
, *m
;
1415 while ((ch
= *m
) && (isspace(ch
) || (ch
== '<')))
1420 if (!isspace(ch
) && (ch
!= '>'))
1425 die(_("insane in-reply-to: %s"), msg_id
);
1428 return xmemdupz(a
, z
- a
);
1431 static const char *set_outdir(const char *prefix
, const char *output_directory
)
1433 if (output_directory
&& is_absolute_path(output_directory
))
1434 return output_directory
;
1436 if (!prefix
|| !*prefix
) {
1437 if (output_directory
)
1438 return output_directory
;
1439 /* The user did not explicitly ask for "./" */
1444 outdir_offset
= strlen(prefix
);
1445 if (!output_directory
)
1448 return prefix_filename(prefix
, output_directory
);
1451 static const char * const builtin_format_patch_usage
[] = {
1452 N_("git format-patch [<options>] [<since> | <revision-range>]"),
1456 static int keep_subject
= 0;
1458 static int keep_callback(const struct option
*opt
, const char *arg
, int unset
)
1460 BUG_ON_OPT_NEG(unset
);
1461 BUG_ON_OPT_ARG(arg
);
1462 ((struct rev_info
*)opt
->value
)->total
= -1;
1467 static int subject_prefix
= 0;
1469 static int subject_prefix_callback(const struct option
*opt
, const char *arg
,
1472 BUG_ON_OPT_NEG(unset
);
1474 ((struct rev_info
*)opt
->value
)->subject_prefix
= arg
;
1478 static int rfc_callback(const struct option
*opt
, const char *arg
, int unset
)
1480 BUG_ON_OPT_NEG(unset
);
1481 BUG_ON_OPT_ARG(arg
);
1482 return subject_prefix_callback(opt
, "RFC PATCH", unset
);
1485 static int numbered_cmdline_opt
= 0;
1487 static int numbered_callback(const struct option
*opt
, const char *arg
,
1490 BUG_ON_OPT_ARG(arg
);
1491 *(int *)opt
->value
= numbered_cmdline_opt
= unset
? 0 : 1;
1497 static int no_numbered_callback(const struct option
*opt
, const char *arg
,
1500 BUG_ON_OPT_NEG(unset
);
1501 return numbered_callback(opt
, arg
, 1);
1504 static int output_directory_callback(const struct option
*opt
, const char *arg
,
1507 const char **dir
= (const char **)opt
->value
;
1508 BUG_ON_OPT_NEG(unset
);
1510 die(_("two output directories?"));
1515 static int thread_callback(const struct option
*opt
, const char *arg
, int unset
)
1517 enum thread_level
*thread
= (enum thread_level
*)opt
->value
;
1519 *thread
= THREAD_UNSET
;
1520 else if (!arg
|| !strcmp(arg
, "shallow"))
1521 *thread
= THREAD_SHALLOW
;
1522 else if (!strcmp(arg
, "deep"))
1523 *thread
= THREAD_DEEP
;
1525 * Please update _git_formatpatch() in git-completion.bash
1526 * when you add new options.
1533 static int attach_callback(const struct option
*opt
, const char *arg
, int unset
)
1535 struct rev_info
*rev
= (struct rev_info
*)opt
->value
;
1537 rev
->mime_boundary
= NULL
;
1539 rev
->mime_boundary
= arg
;
1541 rev
->mime_boundary
= git_version_string
;
1542 rev
->no_inline
= unset
? 0 : 1;
1546 static int inline_callback(const struct option
*opt
, const char *arg
, int unset
)
1548 struct rev_info
*rev
= (struct rev_info
*)opt
->value
;
1550 rev
->mime_boundary
= NULL
;
1552 rev
->mime_boundary
= arg
;
1554 rev
->mime_boundary
= git_version_string
;
1559 static int header_callback(const struct option
*opt
, const char *arg
, int unset
)
1562 string_list_clear(&extra_hdr
, 0);
1563 string_list_clear(&extra_to
, 0);
1564 string_list_clear(&extra_cc
, 0);
1571 static int to_callback(const struct option
*opt
, const char *arg
, int unset
)
1574 string_list_clear(&extra_to
, 0);
1576 string_list_append(&extra_to
, arg
);
1580 static int cc_callback(const struct option
*opt
, const char *arg
, int unset
)
1583 string_list_clear(&extra_cc
, 0);
1585 string_list_append(&extra_cc
, arg
);
1589 static int from_callback(const struct option
*opt
, const char *arg
, int unset
)
1591 char **from
= opt
->value
;
1598 *from
= xstrdup(arg
);
1600 *from
= xstrdup(git_committer_info(IDENT_NO_DATE
));
1604 static int base_callback(const struct option
*opt
, const char *arg
, int unset
)
1606 const char **base_commit
= opt
->value
;
1609 auto_base
= AUTO_BASE_NEVER
;
1610 *base_commit
= NULL
;
1611 } else if (!strcmp(arg
, "auto")) {
1612 auto_base
= AUTO_BASE_ALWAYS
;
1613 *base_commit
= NULL
;
1615 auto_base
= AUTO_BASE_NEVER
;
1621 struct base_tree_info
{
1622 struct object_id base_commit
;
1623 int nr_patch_id
, alloc_patch_id
;
1624 struct object_id
*patch_id
;
1627 static struct commit
*get_base_commit(const char *base_commit
,
1628 struct commit
**list
,
1631 struct commit
*base
= NULL
;
1632 struct commit
**rev
;
1633 int i
= 0, rev_nr
= 0, auto_select
, die_on_failure
;
1635 switch (auto_base
) {
1636 case AUTO_BASE_NEVER
:
1641 /* no base information is requested */
1645 case AUTO_BASE_ALWAYS
:
1646 case AUTO_BASE_WHEN_ABLE
:
1648 BUG("requested automatic base selection but a commit was provided");
1651 die_on_failure
= auto_base
== AUTO_BASE_ALWAYS
;
1655 BUG("unexpected automatic base selection method");
1659 base
= lookup_commit_reference_by_name(base_commit
);
1661 die(_("unknown commit %s"), base_commit
);
1663 struct branch
*curr_branch
= branch_get(NULL
);
1664 const char *upstream
= branch_get_upstream(curr_branch
, NULL
);
1666 struct commit_list
*base_list
;
1667 struct commit
*commit
;
1668 struct object_id oid
;
1670 if (repo_get_oid(the_repository
, upstream
, &oid
)) {
1672 die(_("failed to resolve '%s' as a valid ref"), upstream
);
1676 commit
= lookup_commit_or_die(&oid
, "upstream base");
1677 base_list
= repo_get_merge_bases_many(the_repository
,
1680 /* There should be one and only one merge base. */
1681 if (!base_list
|| base_list
->next
) {
1682 if (die_on_failure
) {
1683 die(_("could not find exact merge base"));
1685 free_commit_list(base_list
);
1689 base
= base_list
->item
;
1690 free_commit_list(base_list
);
1693 die(_("failed to get upstream, if you want to record base commit automatically,\n"
1694 "please use git branch --set-upstream-to to track a remote branch.\n"
1695 "Or you could specify base commit by --base=<base-commit-id> manually"));
1701 ALLOC_ARRAY(rev
, total
);
1702 for (i
= 0; i
< total
; i
++)
1707 * Get merge base through pair-wise computations
1708 * and store it in rev[0].
1710 while (rev_nr
> 1) {
1711 for (i
= 0; i
< rev_nr
/ 2; i
++) {
1712 struct commit_list
*merge_base
;
1713 merge_base
= repo_get_merge_bases(the_repository
,
1716 if (!merge_base
|| merge_base
->next
) {
1717 if (die_on_failure
) {
1718 die(_("failed to find exact merge base"));
1725 rev
[i
] = merge_base
->item
;
1729 rev
[i
] = rev
[2 * i
];
1730 rev_nr
= DIV_ROUND_UP(rev_nr
, 2);
1733 if (!repo_in_merge_bases(the_repository
, base
, rev
[0])) {
1734 if (die_on_failure
) {
1735 die(_("base commit should be the ancestor of revision list"));
1742 for (i
= 0; i
< total
; i
++) {
1743 if (base
== list
[i
]) {
1744 if (die_on_failure
) {
1745 die(_("base commit shouldn't be in revision list"));
1757 define_commit_slab(commit_base
, int);
1759 static void prepare_bases(struct base_tree_info
*bases
,
1760 struct commit
*base
,
1761 struct commit
**list
,
1764 struct commit
*commit
;
1765 struct rev_info revs
;
1766 struct diff_options diffopt
;
1767 struct commit_base commit_base
;
1773 init_commit_base(&commit_base
);
1774 repo_diff_setup(the_repository
, &diffopt
);
1775 diffopt
.flags
.recursive
= 1;
1776 diff_setup_done(&diffopt
);
1778 oidcpy(&bases
->base_commit
, &base
->object
.oid
);
1780 repo_init_revisions(the_repository
, &revs
, NULL
);
1781 revs
.max_parents
= 1;
1782 revs
.topo_order
= 1;
1783 for (i
= 0; i
< total
; i
++) {
1784 list
[i
]->object
.flags
&= ~UNINTERESTING
;
1785 add_pending_object(&revs
, &list
[i
]->object
, "rev_list");
1786 *commit_base_at(&commit_base
, list
[i
]) = 1;
1788 base
->object
.flags
|= UNINTERESTING
;
1789 add_pending_object(&revs
, &base
->object
, "base");
1791 if (prepare_revision_walk(&revs
))
1792 die(_("revision walk setup failed"));
1794 * Traverse the commits list, get prerequisite patch ids
1795 * and stuff them in bases structure.
1797 while ((commit
= get_revision(&revs
)) != NULL
) {
1798 struct object_id oid
;
1799 struct object_id
*patch_id
;
1800 if (*commit_base_at(&commit_base
, commit
))
1802 if (commit_patch_id(commit
, &diffopt
, &oid
, 0))
1803 die(_("cannot get patch id"));
1804 ALLOC_GROW(bases
->patch_id
, bases
->nr_patch_id
+ 1, bases
->alloc_patch_id
);
1805 patch_id
= bases
->patch_id
+ bases
->nr_patch_id
;
1806 oidcpy(patch_id
, &oid
);
1807 bases
->nr_patch_id
++;
1809 clear_commit_base(&commit_base
);
1812 static void print_bases(struct base_tree_info
*bases
, FILE *file
)
1816 /* Only do this once, either for the cover or for the first one */
1817 if (is_null_oid(&bases
->base_commit
))
1820 /* Show the base commit */
1821 fprintf(file
, "\nbase-commit: %s\n", oid_to_hex(&bases
->base_commit
));
1823 /* Show the prerequisite patches */
1824 for (i
= bases
->nr_patch_id
- 1; i
>= 0; i
--)
1825 fprintf(file
, "prerequisite-patch-id: %s\n", oid_to_hex(&bases
->patch_id
[i
]));
1827 free(bases
->patch_id
);
1828 bases
->nr_patch_id
= 0;
1829 bases
->alloc_patch_id
= 0;
1830 oidclr(&bases
->base_commit
);
1833 static const char *diff_title(struct strbuf
*sb
,
1834 const char *reroll_count
,
1835 const char *generic
,
1836 const char *rerolled
)
1840 /* RFC may be v0, so allow -v1 to diff against v0 */
1841 if (reroll_count
&& !strtol_i(reroll_count
, 10, &v
) &&
1843 strbuf_addf(sb
, rerolled
, v
- 1);
1845 strbuf_addstr(sb
, generic
);
1849 static void infer_range_diff_ranges(struct strbuf
*r1
,
1852 struct commit
*origin
,
1853 struct commit
*head
)
1855 const char *head_oid
= oid_to_hex(&head
->object
.oid
);
1856 int prev_is_range
= is_range_diff_range(prev
);
1859 strbuf_addstr(r1
, prev
);
1861 strbuf_addf(r1
, "%s..%s", head_oid
, prev
);
1864 strbuf_addf(r2
, "%s..%s", oid_to_hex(&origin
->object
.oid
), head_oid
);
1865 else if (prev_is_range
)
1866 die(_("failed to infer range-diff origin of current series"));
1868 warning(_("using '%s' as range-diff origin of current series"), prev
);
1869 strbuf_addf(r2
, "%s..%s", prev
, head_oid
);
1873 int cmd_format_patch(int argc
, const char **argv
, const char *prefix
)
1875 struct commit
*commit
;
1876 struct commit
**list
= NULL
;
1877 struct rev_info rev
;
1878 char *to_free
= NULL
;
1879 struct setup_revision_opt s_r_opt
;
1880 int nr
= 0, total
, i
;
1882 int start_number
= -1;
1883 int just_numbers
= 0;
1884 int ignore_if_in_upstream
= 0;
1885 int cover_letter
= -1;
1886 int boundary_count
= 0;
1887 int no_binary_diff
= 0;
1888 int zero_commit
= 0;
1889 struct commit
*origin
= NULL
;
1890 const char *in_reply_to
= NULL
;
1891 struct patch_ids ids
;
1892 struct strbuf buf
= STRBUF_INIT
;
1893 int use_patch_format
= 0;
1895 const char *reroll_count
= NULL
;
1896 char *cover_from_description_arg
= NULL
;
1897 char *branch_name
= NULL
;
1898 char *base_commit
= NULL
;
1899 struct base_tree_info bases
;
1900 struct commit
*base
;
1901 int show_progress
= 0;
1902 struct progress
*progress
= NULL
;
1903 struct oid_array idiff_prev
= OID_ARRAY_INIT
;
1904 struct strbuf idiff_title
= STRBUF_INIT
;
1905 const char *rdiff_prev
= NULL
;
1906 struct strbuf rdiff1
= STRBUF_INIT
;
1907 struct strbuf rdiff2
= STRBUF_INIT
;
1908 struct strbuf rdiff_title
= STRBUF_INIT
;
1909 struct strbuf sprefix
= STRBUF_INIT
;
1910 int creation_factor
= -1;
1912 const struct option builtin_format_patch_options
[] = {
1913 OPT_CALLBACK_F('n', "numbered", &numbered
, NULL
,
1914 N_("use [PATCH n/m] even with a single patch"),
1915 PARSE_OPT_NOARG
, numbered_callback
),
1916 OPT_CALLBACK_F('N', "no-numbered", &numbered
, NULL
,
1917 N_("use [PATCH] even with multiple patches"),
1918 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
, no_numbered_callback
),
1919 OPT_BOOL('s', "signoff", &do_signoff
, N_("add a Signed-off-by trailer")),
1920 OPT_BOOL(0, "stdout", &use_stdout
,
1921 N_("print patches to standard out")),
1922 OPT_BOOL(0, "cover-letter", &cover_letter
,
1923 N_("generate a cover letter")),
1924 OPT_BOOL(0, "numbered-files", &just_numbers
,
1925 N_("use simple number sequence for output file names")),
1926 OPT_STRING(0, "suffix", &fmt_patch_suffix
, N_("sfx"),
1927 N_("use <sfx> instead of '.patch'")),
1928 OPT_INTEGER(0, "start-number", &start_number
,
1929 N_("start numbering patches at <n> instead of 1")),
1930 OPT_STRING('v', "reroll-count", &reroll_count
, N_("reroll-count"),
1931 N_("mark the series as Nth re-roll")),
1932 OPT_INTEGER(0, "filename-max-length", &fmt_patch_name_max
,
1933 N_("max length of output filename")),
1934 OPT_CALLBACK_F(0, "rfc", &rev
, NULL
,
1935 N_("use [RFC PATCH] instead of [PATCH]"),
1936 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
, rfc_callback
),
1937 OPT_STRING(0, "cover-from-description", &cover_from_description_arg
,
1938 N_("cover-from-description-mode"),
1939 N_("generate parts of a cover letter based on a branch's description")),
1940 OPT_CALLBACK_F(0, "subject-prefix", &rev
, N_("prefix"),
1941 N_("use [<prefix>] instead of [PATCH]"),
1942 PARSE_OPT_NONEG
, subject_prefix_callback
),
1943 OPT_CALLBACK_F('o', "output-directory", &output_directory
,
1944 N_("dir"), N_("store resulting files in <dir>"),
1945 PARSE_OPT_NONEG
, output_directory_callback
),
1946 OPT_CALLBACK_F('k', "keep-subject", &rev
, NULL
,
1947 N_("don't strip/add [PATCH]"),
1948 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
, keep_callback
),
1949 OPT_BOOL(0, "no-binary", &no_binary_diff
,
1950 N_("don't output binary diffs")),
1951 OPT_BOOL(0, "zero-commit", &zero_commit
,
1952 N_("output all-zero hash in From header")),
1953 OPT_BOOL(0, "ignore-if-in-upstream", &ignore_if_in_upstream
,
1954 N_("don't include a patch matching a commit upstream")),
1955 OPT_SET_INT_F('p', "no-stat", &use_patch_format
,
1956 N_("show patch format instead of default (patch + stat)"),
1957 1, PARSE_OPT_NONEG
),
1958 OPT_GROUP(N_("Messaging")),
1959 OPT_CALLBACK(0, "add-header", NULL
, N_("header"),
1960 N_("add email header"), header_callback
),
1961 OPT_CALLBACK(0, "to", NULL
, N_("email"), N_("add To: header"), to_callback
),
1962 OPT_CALLBACK(0, "cc", NULL
, N_("email"), N_("add Cc: header"), cc_callback
),
1963 OPT_CALLBACK_F(0, "from", &from
, N_("ident"),
1964 N_("set From address to <ident> (or committer ident if absent)"),
1965 PARSE_OPT_OPTARG
, from_callback
),
1966 OPT_STRING(0, "in-reply-to", &in_reply_to
, N_("message-id"),
1967 N_("make first mail a reply to <message-id>")),
1968 OPT_CALLBACK_F(0, "attach", &rev
, N_("boundary"),
1969 N_("attach the patch"), PARSE_OPT_OPTARG
,
1971 OPT_CALLBACK_F(0, "inline", &rev
, N_("boundary"),
1972 N_("inline the patch"),
1973 PARSE_OPT_OPTARG
| PARSE_OPT_NONEG
,
1975 OPT_CALLBACK_F(0, "thread", &thread
, N_("style"),
1976 N_("enable message threading, styles: shallow, deep"),
1977 PARSE_OPT_OPTARG
, thread_callback
),
1978 OPT_STRING(0, "signature", &signature
, N_("signature"),
1979 N_("add a signature")),
1980 OPT_CALLBACK_F(0, "base", &base_commit
, N_("base-commit"),
1981 N_("add prerequisite tree info to the patch series"),
1983 OPT_FILENAME(0, "signature-file", &signature_file
,
1984 N_("add a signature from a file")),
1985 OPT__QUIET(&quiet
, N_("don't print the patch filenames")),
1986 OPT_BOOL(0, "progress", &show_progress
,
1987 N_("show progress while generating patches")),
1988 OPT_CALLBACK(0, "interdiff", &idiff_prev
, N_("rev"),
1989 N_("show changes against <rev> in cover letter or single patch"),
1990 parse_opt_object_name
),
1991 OPT_STRING(0, "range-diff", &rdiff_prev
, N_("refspec"),
1992 N_("show changes against <refspec> in cover letter or single patch")),
1993 OPT_INTEGER(0, "creation-factor", &creation_factor
,
1994 N_("percentage by which creation is weighted")),
1995 OPT_BOOL(0, "force-in-body-from", &force_in_body_from
,
1996 N_("show in-body From: even if identical to the e-mail header")),
2000 extra_hdr
.strdup_strings
= 1;
2001 extra_to
.strdup_strings
= 1;
2002 extra_cc
.strdup_strings
= 1;
2004 init_log_defaults();
2005 init_display_notes(¬es_opt
);
2006 git_config(git_format_config
, NULL
);
2007 repo_init_revisions(the_repository
, &rev
, prefix
);
2008 git_config(grep_config
, &rev
.grep_filter
);
2010 rev
.show_notes
= show_notes
;
2011 memcpy(&rev
.notes_opt
, ¬es_opt
, sizeof(notes_opt
));
2012 rev
.commit_format
= CMIT_FMT_EMAIL
;
2013 rev
.encode_email_headers
= default_encode_email_headers
;
2014 rev
.expand_tabs_in_log_default
= 0;
2015 rev
.verbose_header
= 1;
2017 rev
.max_parents
= 1;
2018 rev
.diffopt
.flags
.recursive
= 1;
2019 rev
.diffopt
.no_free
= 1;
2020 rev
.subject_prefix
= fmt_patch_subject_prefix
;
2021 memset(&s_r_opt
, 0, sizeof(s_r_opt
));
2022 s_r_opt
.def
= "HEAD";
2023 s_r_opt
.revarg_opt
= REVARG_COMMITTISH
;
2025 if (format_no_prefix
)
2026 diff_set_noprefix(&rev
.diffopt
);
2028 if (default_attach
) {
2029 rev
.mime_boundary
= default_attach
;
2034 * Parse the arguments before setup_revisions(), or something
2035 * like "git format-patch -o a123 HEAD^.." may fail; a123 is
2036 * possibly a valid SHA1.
2038 argc
= parse_options(argc
, argv
, prefix
, builtin_format_patch_options
,
2039 builtin_format_patch_usage
,
2040 PARSE_OPT_KEEP_ARGV0
| PARSE_OPT_KEEP_UNKNOWN_OPT
|
2041 PARSE_OPT_KEEP_DASHDASH
);
2043 rev
.force_in_body_from
= force_in_body_from
;
2045 /* Make sure "0000-$sub.patch" gives non-negative length for $sub */
2046 if (fmt_patch_name_max
<= strlen("0000-") + strlen(fmt_patch_suffix
))
2047 fmt_patch_name_max
= strlen("0000-") + strlen(fmt_patch_suffix
);
2049 if (cover_from_description_arg
)
2050 cover_from_description_mode
= parse_cover_from_description(cover_from_description_arg
);
2053 strbuf_addf(&sprefix
, "%s v%s",
2054 rev
.subject_prefix
, reroll_count
);
2055 rev
.reroll_count
= reroll_count
;
2056 rev
.subject_prefix
= sprefix
.buf
;
2059 for (i
= 0; i
< extra_hdr
.nr
; i
++) {
2060 strbuf_addstr(&buf
, extra_hdr
.items
[i
].string
);
2061 strbuf_addch(&buf
, '\n');
2065 strbuf_addstr(&buf
, "To: ");
2066 for (i
= 0; i
< extra_to
.nr
; i
++) {
2068 strbuf_addstr(&buf
, " ");
2069 strbuf_addstr(&buf
, extra_to
.items
[i
].string
);
2070 if (i
+ 1 < extra_to
.nr
)
2071 strbuf_addch(&buf
, ',');
2072 strbuf_addch(&buf
, '\n');
2076 strbuf_addstr(&buf
, "Cc: ");
2077 for (i
= 0; i
< extra_cc
.nr
; i
++) {
2079 strbuf_addstr(&buf
, " ");
2080 strbuf_addstr(&buf
, extra_cc
.items
[i
].string
);
2081 if (i
+ 1 < extra_cc
.nr
)
2082 strbuf_addch(&buf
, ',');
2083 strbuf_addch(&buf
, '\n');
2086 rev
.extra_headers
= to_free
= strbuf_detach(&buf
, NULL
);
2089 if (split_ident_line(&rev
.from_ident
, from
, strlen(from
)))
2090 die(_("invalid ident line: %s"), from
);
2093 if (start_number
< 0)
2097 * If numbered is set solely due to format.numbered in config,
2098 * and it would conflict with --keep-subject (-k) from the
2099 * command line, reset "numbered".
2101 if (numbered
&& keep_subject
&& !numbered_cmdline_opt
)
2104 if (numbered
&& keep_subject
)
2105 die(_("options '%s' and '%s' cannot be used together"), "-n", "-k");
2106 if (keep_subject
&& subject_prefix
)
2107 die(_("options '%s' and '%s' cannot be used together"), "--subject-prefix/--rfc", "-k");
2108 rev
.preserve_subject
= keep_subject
;
2110 argc
= setup_revisions(argc
, argv
, &rev
, &s_r_opt
);
2112 die(_("unrecognized argument: %s"), argv
[1]);
2114 if (rev
.diffopt
.output_format
& DIFF_FORMAT_NAME
)
2115 die(_("--name-only does not make sense"));
2116 if (rev
.diffopt
.output_format
& DIFF_FORMAT_NAME_STATUS
)
2117 die(_("--name-status does not make sense"));
2118 if (rev
.diffopt
.output_format
& DIFF_FORMAT_CHECKDIFF
)
2119 die(_("--check does not make sense"));
2120 if (rev
.remerge_diff
)
2121 die(_("--remerge-diff does not make sense"));
2123 if (!use_patch_format
&&
2124 (!rev
.diffopt
.output_format
||
2125 rev
.diffopt
.output_format
== DIFF_FORMAT_PATCH
))
2126 rev
.diffopt
.output_format
= DIFF_FORMAT_DIFFSTAT
| DIFF_FORMAT_SUMMARY
;
2127 if (!rev
.diffopt
.stat_width
)
2128 rev
.diffopt
.stat_width
= MAIL_DEFAULT_WRAP
;
2130 /* Always generate a patch */
2131 rev
.diffopt
.output_format
|= DIFF_FORMAT_PATCH
;
2132 rev
.always_show_header
= 1;
2134 rev
.zero_commit
= zero_commit
;
2135 rev
.patch_name_max
= fmt_patch_name_max
;
2137 if (!rev
.diffopt
.flags
.text
&& !no_binary_diff
)
2138 rev
.diffopt
.flags
.binary
= 1;
2141 load_display_notes(&rev
.notes_opt
);
2143 die_for_incompatible_opt3(use_stdout
, "--stdout",
2144 rev
.diffopt
.close_file
, "--output",
2145 !!output_directory
, "--output-directory");
2147 if (use_stdout
&& stdout_mboxrd
)
2148 rev
.commit_format
= CMIT_FMT_MBOXRD
;
2152 } else if (!rev
.diffopt
.close_file
) {
2155 if (!output_directory
)
2156 output_directory
= config_output_directory
;
2157 output_directory
= set_outdir(prefix
, output_directory
);
2159 if (rev
.diffopt
.use_color
!= GIT_COLOR_ALWAYS
)
2160 rev
.diffopt
.use_color
= GIT_COLOR_NEVER
;
2162 * We consider <outdir> as 'outside of gitdir', therefore avoid
2163 * applying adjust_shared_perm in s-c-l-d.
2165 saved
= get_shared_repository();
2166 set_shared_repository(0);
2167 switch (safe_create_leading_directories_const(output_directory
)) {
2172 die(_("could not create leading directories "
2173 "of '%s'"), output_directory
);
2175 set_shared_repository(saved
);
2176 if (mkdir(output_directory
, 0777) < 0 && errno
!= EEXIST
)
2177 die_errno(_("could not create directory '%s'"),
2181 if (rev
.pending
.nr
== 1) {
2184 if (rev
.max_count
< 0 && !rev
.show_root_diff
) {
2186 * This is traditional behaviour of "git format-patch
2187 * origin" that prepares what the origin side still
2190 rev
.pending
.objects
[0].item
->flags
|= UNINTERESTING
;
2191 add_head_to_pending(&rev
);
2195 * Otherwise, it is "format-patch -22 HEAD", and/or
2196 * "format-patch --root HEAD". The user wants
2197 * get_revision() to do the usual traversal.
2200 if (!strcmp(rev
.pending
.objects
[0].name
, "HEAD"))
2204 const char *ref
, *v
;
2205 ref
= resolve_ref_unsafe("HEAD", RESOLVE_REF_READING
,
2207 if (ref
&& skip_prefix(ref
, "refs/heads/", &v
))
2208 branch_name
= xstrdup(v
);
2210 branch_name
= xstrdup(""); /* no branch */
2215 * We cannot move this anywhere earlier because we do want to
2216 * know if --root was given explicitly from the command line.
2218 rev
.show_root_diff
= 1;
2220 if (ignore_if_in_upstream
) {
2221 /* Don't say anything if head and upstream are the same. */
2222 if (rev
.pending
.nr
== 2) {
2223 struct object_array_entry
*o
= rev
.pending
.objects
;
2224 if (oideq(&o
[0].item
->oid
, &o
[1].item
->oid
))
2227 get_patch_ids(&rev
, &ids
);
2230 if (prepare_revision_walk(&rev
))
2231 die(_("revision walk setup failed"));
2233 while ((commit
= get_revision(&rev
)) != NULL
) {
2234 if (commit
->object
.flags
& BOUNDARY
) {
2236 origin
= (boundary_count
== 1) ? commit
: NULL
;
2240 if (ignore_if_in_upstream
&& has_commit_patch_id(commit
, &ids
))
2244 REALLOC_ARRAY(list
, nr
);
2245 list
[nr
- 1] = commit
;
2251 if (cover_letter
== -1) {
2252 if (config_cover_letter
== COVER_AUTO
)
2253 cover_letter
= (total
> 1);
2255 cover_letter
= (config_cover_letter
== COVER_ON
);
2257 if (!keep_subject
&& auto_number
&& (total
> 1 || cover_letter
))
2260 rev
.total
= total
+ start_number
- 1;
2262 if (idiff_prev
.nr
) {
2263 if (!cover_letter
&& total
!= 1)
2264 die(_("--interdiff requires --cover-letter or single patch"));
2265 rev
.idiff_oid1
= &idiff_prev
.oid
[idiff_prev
.nr
- 1];
2266 rev
.idiff_oid2
= get_commit_tree_oid(list
[0]);
2267 rev
.idiff_title
= diff_title(&idiff_title
, reroll_count
,
2269 _("Interdiff against v%d:"));
2272 if (creation_factor
< 0)
2273 creation_factor
= RANGE_DIFF_CREATION_FACTOR_DEFAULT
;
2274 else if (!rdiff_prev
)
2275 die(_("the option '%s' requires '%s'"), "--creation-factor", "--range-diff");
2278 if (!cover_letter
&& total
!= 1)
2279 die(_("--range-diff requires --cover-letter or single patch"));
2281 infer_range_diff_ranges(&rdiff1
, &rdiff2
, rdiff_prev
,
2283 rev
.rdiff1
= rdiff1
.buf
;
2284 rev
.rdiff2
= rdiff2
.buf
;
2285 rev
.creation_factor
= creation_factor
;
2286 rev
.rdiff_title
= diff_title(&rdiff_title
, reroll_count
,
2288 _("Range-diff against v%d:"));
2292 ; /* --no-signature inhibits all signatures */
2293 } else if (signature
&& signature
!= git_version_string
) {
2294 ; /* non-default signature already set */
2295 } else if (signature_file
) {
2296 struct strbuf buf
= STRBUF_INIT
;
2298 if (strbuf_read_file(&buf
, signature_file
, 128) < 0)
2299 die_errno(_("unable to read signature file '%s'"), signature_file
);
2300 signature
= strbuf_detach(&buf
, NULL
);
2303 memset(&bases
, 0, sizeof(bases
));
2304 base
= get_base_commit(base_commit
, list
, nr
);
2306 reset_revision_walk();
2307 clear_object_flags(UNINTERESTING
);
2308 prepare_bases(&bases
, base
, list
, nr
);
2311 if (in_reply_to
|| thread
|| cover_letter
) {
2312 rev
.ref_message_ids
= xmalloc(sizeof(*rev
.ref_message_ids
));
2313 string_list_init_nodup(rev
.ref_message_ids
);
2316 const char *msgid
= clean_message_id(in_reply_to
);
2317 string_list_append(rev
.ref_message_ids
, msgid
);
2319 rev
.numbered_files
= just_numbers
;
2320 rev
.patch_suffix
= fmt_patch_suffix
;
2323 gen_message_id(&rev
, "cover");
2324 make_cover_letter(&rev
, !!output_directory
,
2325 origin
, nr
, list
, branch_name
, quiet
);
2326 print_bases(&bases
, rev
.diffopt
.file
);
2327 print_signature(rev
.diffopt
.file
);
2330 /* interdiff/range-diff in cover-letter; omit from patches */
2331 rev
.idiff_oid1
= NULL
;
2334 rev
.add_signoff
= do_signoff
;
2337 progress
= start_delayed_progress(_("Generating patches"), total
);
2340 display_progress(progress
, total
- nr
);
2342 rev
.nr
= total
- nr
+ (start_number
- 1);
2343 /* Make the second and subsequent mails replies to the first */
2345 /* Have we already had a message ID? */
2346 if (rev
.message_id
) {
2348 * For deep threading: make every mail
2349 * a reply to the previous one, no
2350 * matter what other options are set.
2352 * For shallow threading:
2354 * Without --cover-letter and
2355 * --in-reply-to, make every mail a
2356 * reply to the one before.
2358 * With --in-reply-to but no
2359 * --cover-letter, make every mail a
2360 * reply to the <reply-to>.
2362 * With --cover-letter, make every
2363 * mail but the cover letter a reply
2364 * to the cover letter. The cover
2365 * letter is a reply to the
2366 * --in-reply-to, if specified.
2368 if (thread
== THREAD_SHALLOW
2369 && rev
.ref_message_ids
->nr
> 0
2370 && (!cover_letter
|| rev
.nr
> 1))
2371 free(rev
.message_id
);
2373 string_list_append(rev
.ref_message_ids
,
2376 gen_message_id(&rev
, oid_to_hex(&commit
->object
.oid
));
2379 if (output_directory
&&
2380 open_next_file(rev
.numbered_files
? NULL
: commit
, NULL
, &rev
, quiet
))
2381 die(_("failed to create output files"));
2382 shown
= log_tree_commit(&rev
, commit
);
2383 free_commit_buffer(the_repository
->parsed_objects
,
2386 /* We put one extra blank line between formatted
2387 * patches and this flag is used by log-tree code
2388 * to see if it needs to emit a LF before showing
2389 * the log; when using one file per patch, we do
2390 * not want the extra blank line.
2392 if (output_directory
)
2395 print_bases(&bases
, rev
.diffopt
.file
);
2396 if (rev
.mime_boundary
)
2397 fprintf(rev
.diffopt
.file
, "\n--%s%s--\n\n\n",
2398 mime_boundary_leader
,
2401 print_signature(rev
.diffopt
.file
);
2403 if (output_directory
)
2404 fclose(rev
.diffopt
.file
);
2406 stop_progress(&progress
);
2409 string_list_clear(&extra_to
, 0);
2410 string_list_clear(&extra_cc
, 0);
2411 string_list_clear(&extra_hdr
, 0);
2412 if (ignore_if_in_upstream
)
2413 free_patch_ids(&ids
);
2416 oid_array_clear(&idiff_prev
);
2417 strbuf_release(&idiff_title
);
2418 strbuf_release(&rdiff1
);
2419 strbuf_release(&rdiff2
);
2420 strbuf_release(&rdiff_title
);
2421 strbuf_release(&sprefix
);
2423 if (rev
.ref_message_ids
)
2424 string_list_clear(rev
.ref_message_ids
, 0);
2425 free(rev
.ref_message_ids
);
2426 return cmd_log_deinit(0, &rev
);
2429 static int add_pending_commit(const char *arg
, struct rev_info
*revs
, int flags
)
2431 struct object_id oid
;
2432 if (repo_get_oid(the_repository
, arg
, &oid
) == 0) {
2433 struct commit
*commit
= lookup_commit_reference(the_repository
,
2436 commit
->object
.flags
|= flags
;
2437 add_pending_object(revs
, &commit
->object
, arg
);
2444 static const char * const cherry_usage
[] = {
2445 N_("git cherry [-v] [<upstream> [<head> [<limit>]]]"),
2449 static void print_commit(char sign
, struct commit
*commit
, int verbose
,
2450 int abbrev
, FILE *file
)
2453 fprintf(file
, "%c %s\n", sign
,
2454 repo_find_unique_abbrev(the_repository
, &commit
->object
.oid
, abbrev
));
2456 struct strbuf buf
= STRBUF_INIT
;
2457 pp_commit_easy(CMIT_FMT_ONELINE
, commit
, &buf
);
2458 fprintf(file
, "%c %s %s\n", sign
,
2459 repo_find_unique_abbrev(the_repository
, &commit
->object
.oid
, abbrev
),
2461 strbuf_release(&buf
);
2465 int cmd_cherry(int argc
, const char **argv
, const char *prefix
)
2467 struct rev_info revs
;
2468 struct patch_ids ids
;
2469 struct commit
*commit
;
2470 struct commit_list
*list
= NULL
;
2471 struct branch
*current_branch
;
2472 const char *upstream
;
2473 const char *head
= "HEAD";
2474 const char *limit
= NULL
;
2475 int verbose
= 0, abbrev
= 0;
2477 struct option options
[] = {
2478 OPT__ABBREV(&abbrev
),
2479 OPT__VERBOSE(&verbose
, N_("be verbose")),
2483 argc
= parse_options(argc
, argv
, prefix
, options
, cherry_usage
, 0);
2496 current_branch
= branch_get(NULL
);
2497 upstream
= branch_get_upstream(current_branch
, NULL
);
2499 fprintf(stderr
, _("Could not find a tracked"
2500 " remote branch, please"
2501 " specify <upstream> manually.\n"));
2502 usage_with_options(cherry_usage
, options
);
2506 repo_init_revisions(the_repository
, &revs
, prefix
);
2507 revs
.max_parents
= 1;
2509 if (add_pending_commit(head
, &revs
, 0))
2510 die(_("unknown commit %s"), head
);
2511 if (add_pending_commit(upstream
, &revs
, UNINTERESTING
))
2512 die(_("unknown commit %s"), upstream
);
2514 /* Don't say anything if head and upstream are the same. */
2515 if (revs
.pending
.nr
== 2) {
2516 struct object_array_entry
*o
= revs
.pending
.objects
;
2517 if (oideq(&o
[0].item
->oid
, &o
[1].item
->oid
))
2521 get_patch_ids(&revs
, &ids
);
2523 if (limit
&& add_pending_commit(limit
, &revs
, UNINTERESTING
))
2524 die(_("unknown commit %s"), limit
);
2526 /* reverse the list of commits */
2527 if (prepare_revision_walk(&revs
))
2528 die(_("revision walk setup failed"));
2529 while ((commit
= get_revision(&revs
)) != NULL
) {
2530 commit_list_insert(commit
, &list
);
2536 commit
= list
->item
;
2537 if (has_commit_patch_id(commit
, &ids
))
2539 print_commit(sign
, commit
, verbose
, abbrev
, revs
.diffopt
.file
);
2543 free_patch_ids(&ids
);