2 * Builtin "git log" and related commands (show, whatchanged)
4 * (C) Copyright 2006 Linus Torvalds
10 #include "environment.h"
14 #include "object-file.h"
15 #include "object-name.h"
16 #include "object-store-ll.h"
21 #include "diff-merges.h"
25 #include "oid-array.h"
27 #include "reflog-walk.h"
28 #include "patch-ids.h"
31 #include "string-list.h"
32 #include "parse-options.h"
35 #include "streaming.h"
39 #include "commit-slab.h"
40 #include "repository.h"
41 #include "commit-reach.h"
42 #include "range-diff.h"
43 #include "tmp-objdir.h"
45 #include "write-or-die.h"
47 #define MAIL_DEFAULT_WRAP 72
48 #define COVER_FROM_AUTO_MAX_SUBJECT_LEN 100
49 #define FORMAT_PATCH_NAME_MAX_DEFAULT 64
51 static unsigned int force_in_body_from
;
52 static int stdout_mboxrd
;
53 static int format_no_prefix
;
55 static const char * const builtin_log_usage
[] = {
56 N_("git log [<options>] [<revision-range>] [[--] <path>...]"),
57 N_("git show [<options>] <object>..."),
61 struct line_opt_callback_data
{
64 struct string_list args
;
67 static int session_is_interactive(void)
69 return isatty(1) || pager_in_use();
72 static int auto_decoration_style(void)
74 return session_is_interactive() ? DECORATE_SHORT_REFS
: 0;
77 static int parse_decoration_style(const char *value
)
79 switch (git_parse_maybe_bool(value
)) {
81 return DECORATE_SHORT_REFS
;
87 if (!strcmp(value
, "full"))
88 return DECORATE_FULL_REFS
;
89 else if (!strcmp(value
, "short"))
90 return DECORATE_SHORT_REFS
;
91 else if (!strcmp(value
, "auto"))
92 return auto_decoration_style();
94 * Please update _git_log() in git-completion.bash when you
95 * add new decoration styles.
101 int default_abbrev_commit
;
102 int default_show_root
;
104 int default_show_signature
;
105 int default_encode_email_headers
;
106 int decoration_style
;
107 int decoration_given
;
108 int use_mailmap_config
;
109 char *fmt_patch_subject_prefix
;
110 int fmt_patch_name_max
;
112 char *default_date_mode
;
115 static void log_config_init(struct log_config
*cfg
)
117 memset(cfg
, 0, sizeof(*cfg
));
118 cfg
->default_show_root
= 1;
119 cfg
->default_encode_email_headers
= 1;
120 cfg
->use_mailmap_config
= 1;
121 cfg
->fmt_patch_subject_prefix
= xstrdup("PATCH");
122 cfg
->fmt_patch_name_max
= FORMAT_PATCH_NAME_MAX_DEFAULT
;
123 cfg
->decoration_style
= auto_decoration_style();
126 static void log_config_release(struct log_config
*cfg
)
128 free(cfg
->default_date_mode
);
129 free(cfg
->fmt_pretty
);
130 free(cfg
->fmt_patch_subject_prefix
);
133 static int use_default_decoration_filter
= 1;
134 static struct string_list decorate_refs_exclude
= STRING_LIST_INIT_NODUP
;
135 static struct string_list decorate_refs_exclude_config
= STRING_LIST_INIT_NODUP
;
136 static struct string_list decorate_refs_include
= STRING_LIST_INIT_NODUP
;
138 static int clear_decorations_callback(const struct option
*opt UNUSED
,
139 const char *arg
, int unset
)
141 BUG_ON_OPT_NEG(unset
);
143 string_list_clear(&decorate_refs_include
, 0);
144 string_list_clear(&decorate_refs_exclude
, 0);
145 use_default_decoration_filter
= 0;
149 static int decorate_callback(const struct option
*opt
, const char *arg
,
152 struct log_config
*cfg
= opt
->value
;
155 cfg
->decoration_style
= 0;
157 cfg
->decoration_style
= parse_decoration_style(arg
);
159 cfg
->decoration_style
= DECORATE_SHORT_REFS
;
161 if (cfg
->decoration_style
< 0)
162 die(_("invalid --decorate option: %s"), arg
);
164 cfg
->decoration_given
= 1;
169 static int log_line_range_callback(const struct option
*option
, const char *arg
, int unset
)
171 struct line_opt_callback_data
*data
= option
->value
;
173 BUG_ON_OPT_NEG(unset
);
178 data
->rev
->line_level_traverse
= 1;
179 string_list_append(&data
->args
, arg
);
184 static void cmd_log_init_defaults(struct rev_info
*rev
,
185 struct log_config
*cfg
)
188 get_commit_format(cfg
->fmt_pretty
, rev
);
189 if (cfg
->default_follow
)
190 rev
->diffopt
.flags
.default_follow_renames
= 1;
191 rev
->verbose_header
= 1;
192 init_diffstat_widths(&rev
->diffopt
);
193 rev
->diffopt
.flags
.recursive
= 1;
194 rev
->diffopt
.flags
.allow_textconv
= 1;
195 rev
->abbrev_commit
= cfg
->default_abbrev_commit
;
196 rev
->show_root_diff
= cfg
->default_show_root
;
197 rev
->subject_prefix
= cfg
->fmt_patch_subject_prefix
;
198 rev
->patch_name_max
= cfg
->fmt_patch_name_max
;
199 rev
->show_signature
= cfg
->default_show_signature
;
200 rev
->encode_email_headers
= cfg
->default_encode_email_headers
;
202 if (cfg
->default_date_mode
)
203 parse_date_format(cfg
->default_date_mode
, &rev
->date_mode
);
206 static void set_default_decoration_filter(struct decoration_filter
*decoration_filter
)
210 struct string_list
*include
= decoration_filter
->include_ref_pattern
;
211 const struct string_list
*config_exclude
;
213 if (!git_config_get_string_multi("log.excludeDecoration",
215 struct string_list_item
*item
;
216 for_each_string_list_item(item
, config_exclude
)
217 string_list_append(decoration_filter
->exclude_ref_config_pattern
,
222 * By default, decorate_all is disabled. Enable it if
223 * log.initialDecorationSet=all. Don't ever disable it by config,
224 * since the command-line takes precedent.
226 if (use_default_decoration_filter
&&
227 !git_config_get_string("log.initialdecorationset", &value
) &&
228 !strcmp("all", value
))
229 use_default_decoration_filter
= 0;
232 if (!use_default_decoration_filter
||
233 decoration_filter
->exclude_ref_pattern
->nr
||
234 decoration_filter
->include_ref_pattern
->nr
||
235 decoration_filter
->exclude_ref_config_pattern
->nr
)
239 * No command-line or config options were given, so
240 * populate with sensible defaults.
242 for (i
= 0; i
< ARRAY_SIZE(ref_namespace
); i
++) {
243 if (!ref_namespace
[i
].decoration
)
246 string_list_append(include
, ref_namespace
[i
].ref
);
250 static void cmd_log_init_finish(int argc
, const char **argv
, const char *prefix
,
251 struct rev_info
*rev
, struct setup_revision_opt
*opt
,
252 struct log_config
*cfg
)
254 struct userformat_want w
;
255 int quiet
= 0, source
= 0, mailmap
;
256 static struct line_opt_callback_data line_cb
= {NULL
, NULL
, STRING_LIST_INIT_DUP
};
257 struct decoration_filter decoration_filter
= {
258 .exclude_ref_pattern
= &decorate_refs_exclude
,
259 .include_ref_pattern
= &decorate_refs_include
,
260 .exclude_ref_config_pattern
= &decorate_refs_exclude_config
,
262 static struct revision_sources revision_sources
;
264 const struct option builtin_log_options
[] = {
265 OPT__QUIET(&quiet
, N_("suppress diff output")),
266 OPT_BOOL(0, "source", &source
, N_("show source")),
267 OPT_BOOL(0, "use-mailmap", &mailmap
, N_("use mail map file")),
268 OPT_ALIAS(0, "mailmap", "use-mailmap"),
269 OPT_CALLBACK_F(0, "clear-decorations", NULL
, NULL
,
270 N_("clear all previously-defined decoration filters"),
271 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
,
272 clear_decorations_callback
),
273 OPT_STRING_LIST(0, "decorate-refs", &decorate_refs_include
,
274 N_("pattern"), N_("only decorate refs that match <pattern>")),
275 OPT_STRING_LIST(0, "decorate-refs-exclude", &decorate_refs_exclude
,
276 N_("pattern"), N_("do not decorate refs that match <pattern>")),
277 OPT_CALLBACK_F(0, "decorate", cfg
, NULL
, N_("decorate options"),
278 PARSE_OPT_OPTARG
, decorate_callback
),
279 OPT_CALLBACK('L', NULL
, &line_cb
, "range:file",
280 N_("trace the evolution of line range <start>,<end> or function :<funcname> in <file>"),
281 log_line_range_callback
),
286 line_cb
.prefix
= prefix
;
288 mailmap
= cfg
->use_mailmap_config
;
289 argc
= parse_options(argc
, argv
, prefix
,
290 builtin_log_options
, builtin_log_usage
,
291 PARSE_OPT_KEEP_ARGV0
| PARSE_OPT_KEEP_UNKNOWN_OPT
|
292 PARSE_OPT_KEEP_DASHDASH
);
295 rev
->diffopt
.output_format
|= DIFF_FORMAT_NO_OUTPUT
;
296 argc
= setup_revisions(argc
, argv
, rev
, opt
);
298 /* Any arguments at this point are not recognized */
300 die(_("unrecognized argument: %s"), argv
[1]);
302 if (rev
->line_level_traverse
&& rev
->prune_data
.nr
)
303 die(_("-L<range>:<file> cannot be used with pathspec"));
305 memset(&w
, 0, sizeof(w
));
306 userformat_find_requirements(NULL
, &w
);
308 if (!rev
->show_notes_given
&& (!rev
->pretty_given
|| w
.notes
))
311 load_display_notes(&rev
->notes_opt
);
313 if ((rev
->diffopt
.pickaxe_opts
& DIFF_PICKAXE_KINDS_MASK
) ||
314 rev
->diffopt
.filter
|| rev
->diffopt
.flags
.follow_renames
)
315 rev
->always_show_header
= 0;
317 if (source
|| w
.source
) {
318 init_revision_sources(&revision_sources
);
319 rev
->sources
= &revision_sources
;
323 rev
->mailmap
= xmalloc(sizeof(struct string_list
));
324 string_list_init_nodup(rev
->mailmap
);
325 read_mailmap(rev
->mailmap
);
328 if (rev
->pretty_given
&& rev
->commit_format
== CMIT_FMT_RAW
) {
330 * "log --pretty=raw" is special; ignore UI oriented
331 * configuration variables such as decoration.
333 if (!cfg
->decoration_given
)
334 cfg
->decoration_style
= 0;
335 if (!rev
->abbrev_commit_given
)
336 rev
->abbrev_commit
= 0;
339 if (rev
->commit_format
== CMIT_FMT_USERFORMAT
) {
342 * Disable decoration loading if the format will not
345 cfg
->decoration_style
= 0;
346 } else if (!cfg
->decoration_style
) {
348 * If we are going to show them, make sure we do load
349 * them here, but taking care not to override a
350 * specific style set by config or --decorate.
352 cfg
->decoration_style
= DECORATE_SHORT_REFS
;
356 if (cfg
->decoration_style
|| rev
->simplify_by_decoration
) {
357 set_default_decoration_filter(&decoration_filter
);
359 if (cfg
->decoration_style
)
360 rev
->show_decorations
= 1;
362 load_ref_decorations(&decoration_filter
, cfg
->decoration_style
);
365 if (rev
->line_level_traverse
)
366 line_log_init(rev
, line_cb
.prefix
, &line_cb
.args
);
371 static void cmd_log_init(int argc
, const char **argv
, const char *prefix
,
372 struct rev_info
*rev
, struct setup_revision_opt
*opt
,
373 struct log_config
*cfg
)
375 cmd_log_init_defaults(rev
, cfg
);
376 cmd_log_init_finish(argc
, argv
, prefix
, rev
, opt
, cfg
);
380 * This gives a rough estimate for how many commits we
381 * will print out in the list.
383 static int estimate_commit_count(struct commit_list
*list
)
388 struct commit
*commit
= list
->item
;
389 unsigned int flags
= commit
->object
.flags
;
391 if (!(flags
& (TREESAME
| UNINTERESTING
)))
397 static void show_early_header(struct rev_info
*rev
, const char *stage
, int nr
)
399 if (rev
->shown_one
) {
401 if (rev
->commit_format
!= CMIT_FMT_ONELINE
)
402 putchar(rev
->diffopt
.line_termination
);
404 fprintf(rev
->diffopt
.file
, _("Final output: %d %s\n"), nr
, stage
);
407 static struct itimerval early_output_timer
;
409 static void log_show_early(struct rev_info
*revs
, struct commit_list
*list
)
411 int i
= revs
->early_output
;
413 int no_free
= revs
->diffopt
.no_free
;
415 revs
->diffopt
.no_free
= 0;
416 sort_in_topological_order(&list
, revs
->sort_order
);
418 struct commit
*commit
= list
->item
;
419 switch (simplify_commit(revs
, commit
)) {
422 int n
= estimate_commit_count(list
);
423 show_early_header(revs
, "incomplete", n
);
426 log_tree_commit(revs
, commit
);
432 revs
->diffopt
.no_free
= no_free
;
433 diff_free(&revs
->diffopt
);
439 /* Did we already get enough commits for the early output? */
441 revs
->diffopt
.no_free
= 0;
442 diff_free(&revs
->diffopt
);
447 * ..if no, then repeat it twice a second until we
450 * NOTE! We don't use "it_interval", because if the
451 * reader isn't listening, we want our output to be
452 * throttled by the writing, and not have the timer
453 * trigger every second even if we're blocked on a
456 early_output_timer
.it_value
.tv_sec
= 0;
457 early_output_timer
.it_value
.tv_usec
= 500000;
458 setitimer(ITIMER_REAL
, &early_output_timer
, NULL
);
461 static void early_output(int signal UNUSED
)
463 show_early_output
= log_show_early
;
466 static void setup_early_output(void)
471 * Set up the signal handler, minimally intrusively:
472 * we only set a single volatile integer word (not
473 * using sigatomic_t - trying to avoid unnecessary
474 * system dependencies and headers), and using
477 memset(&sa
, 0, sizeof(sa
));
478 sa
.sa_handler
= early_output
;
479 sigemptyset(&sa
.sa_mask
);
480 sa
.sa_flags
= SA_RESTART
;
481 sigaction(SIGALRM
, &sa
, NULL
);
484 * If we can get the whole output in less than a
485 * tenth of a second, don't even bother doing the
486 * early-output thing..
488 * This is a one-time-only trigger.
490 early_output_timer
.it_value
.tv_sec
= 0;
491 early_output_timer
.it_value
.tv_usec
= 100000;
492 setitimer(ITIMER_REAL
, &early_output_timer
, NULL
);
495 static void finish_early_output(struct rev_info
*rev
)
497 int n
= estimate_commit_count(rev
->commits
);
498 signal(SIGALRM
, SIG_IGN
);
499 show_early_header(rev
, "done", n
);
502 static int cmd_log_walk_no_free(struct rev_info
*rev
)
504 struct commit
*commit
;
508 if (rev
->remerge_diff
) {
509 rev
->remerge_objdir
= tmp_objdir_create("remerge-diff");
510 if (!rev
->remerge_objdir
)
511 die(_("unable to create temporary object directory"));
512 tmp_objdir_replace_primary_odb(rev
->remerge_objdir
, 1);
515 if (rev
->early_output
)
516 setup_early_output();
518 if (prepare_revision_walk(rev
))
519 die(_("revision walk setup failed"));
521 if (rev
->early_output
)
522 finish_early_output(rev
);
525 * For --check and --exit-code, the exit code is based on CHECK_FAILED
526 * and HAS_CHANGES being accumulated in rev->diffopt, so be careful to
527 * retain that state information if replacing rev->diffopt in this loop
529 while ((commit
= get_revision(rev
)) != NULL
) {
530 if (!log_tree_commit(rev
, commit
) && rev
->max_count
>= 0)
532 * We decremented max_count in get_revision,
533 * but we didn't actually show the commit.
536 if (!rev
->reflog_info
) {
538 * We may show a given commit multiple times when
539 * walking the reflogs.
541 free_commit_buffer(the_repository
->parsed_objects
,
543 free_commit_list(commit
->parents
);
544 commit
->parents
= NULL
;
546 if (saved_nrl
< rev
->diffopt
.needed_rename_limit
)
547 saved_nrl
= rev
->diffopt
.needed_rename_limit
;
548 if (rev
->diffopt
.degraded_cc_to_c
)
551 rev
->diffopt
.degraded_cc_to_c
= saved_dcctc
;
552 rev
->diffopt
.needed_rename_limit
= saved_nrl
;
554 if (rev
->remerge_diff
) {
555 tmp_objdir_destroy(rev
->remerge_objdir
);
556 rev
->remerge_objdir
= NULL
;
559 if (rev
->diffopt
.output_format
& DIFF_FORMAT_CHECKDIFF
&&
560 rev
->diffopt
.flags
.check_failed
) {
563 return diff_result_code(&rev
->diffopt
);
566 static int cmd_log_walk(struct rev_info
*rev
)
570 rev
->diffopt
.no_free
= 1;
571 retval
= cmd_log_walk_no_free(rev
);
572 rev
->diffopt
.no_free
= 0;
573 diff_free(&rev
->diffopt
);
577 static int git_log_config(const char *var
, const char *value
,
578 const struct config_context
*ctx
, void *cb
)
580 struct log_config
*cfg
= cb
;
581 const char *slot_name
;
583 if (!strcmp(var
, "format.pretty")) {
584 FREE_AND_NULL(cfg
->fmt_pretty
);
585 return git_config_string(&cfg
->fmt_pretty
, var
, value
);
587 if (!strcmp(var
, "format.subjectprefix")) {
588 FREE_AND_NULL(cfg
->fmt_patch_subject_prefix
);
589 return git_config_string(&cfg
->fmt_patch_subject_prefix
, var
, value
);
591 if (!strcmp(var
, "format.filenamemaxlength")) {
592 cfg
->fmt_patch_name_max
= git_config_int(var
, value
, ctx
->kvi
);
595 if (!strcmp(var
, "format.encodeemailheaders")) {
596 cfg
->default_encode_email_headers
= git_config_bool(var
, value
);
599 if (!strcmp(var
, "log.abbrevcommit")) {
600 cfg
->default_abbrev_commit
= git_config_bool(var
, value
);
603 if (!strcmp(var
, "log.date")) {
604 FREE_AND_NULL(cfg
->default_date_mode
);
605 return git_config_string(&cfg
->default_date_mode
, var
, value
);
607 if (!strcmp(var
, "log.decorate")) {
608 cfg
->decoration_style
= parse_decoration_style(value
);
609 if (cfg
->decoration_style
< 0)
610 cfg
->decoration_style
= 0; /* maybe warn? */
613 if (!strcmp(var
, "log.diffmerges")) {
615 return config_error_nonbool(var
);
616 return diff_merges_config(value
);
618 if (!strcmp(var
, "log.showroot")) {
619 cfg
->default_show_root
= git_config_bool(var
, value
);
622 if (!strcmp(var
, "log.follow")) {
623 cfg
->default_follow
= git_config_bool(var
, value
);
626 if (skip_prefix(var
, "color.decorate.", &slot_name
))
627 return parse_decorate_color_config(var
, slot_name
, value
);
628 if (!strcmp(var
, "log.mailmap")) {
629 cfg
->use_mailmap_config
= git_config_bool(var
, value
);
632 if (!strcmp(var
, "log.showsignature")) {
633 cfg
->default_show_signature
= git_config_bool(var
, value
);
637 return git_diff_ui_config(var
, value
, ctx
, cb
);
640 int cmd_whatchanged(int argc
, const char **argv
, const char *prefix
)
642 struct log_config cfg
;
644 struct setup_revision_opt opt
;
647 log_config_init(&cfg
);
648 init_diff_ui_defaults();
649 git_config(git_log_config
, &cfg
);
651 repo_init_revisions(the_repository
, &rev
, prefix
);
652 git_config(grep_config
, &rev
.grep_filter
);
655 rev
.simplify_history
= 0;
656 memset(&opt
, 0, sizeof(opt
));
658 opt
.revarg_opt
= REVARG_COMMITTISH
;
659 cmd_log_init(argc
, argv
, prefix
, &rev
, &opt
, &cfg
);
660 if (!rev
.diffopt
.output_format
)
661 rev
.diffopt
.output_format
= DIFF_FORMAT_RAW
;
663 ret
= cmd_log_walk(&rev
);
665 release_revisions(&rev
);
666 log_config_release(&cfg
);
670 static void show_tagger(const char *buf
, struct rev_info
*rev
)
672 struct strbuf out
= STRBUF_INIT
;
673 struct pretty_print_context pp
= {0};
675 pp
.fmt
= rev
->commit_format
;
676 pp
.date_mode
= rev
->date_mode
;
677 pp_user_info(&pp
, "Tagger", &out
, buf
, get_log_output_encoding());
678 fprintf(rev
->diffopt
.file
, "%s", out
.buf
);
679 strbuf_release(&out
);
682 static int show_blob_object(const struct object_id
*oid
, struct rev_info
*rev
, const char *obj_name
)
684 struct object_id oidc
;
685 struct object_context obj_context
= {0};
689 fflush(rev
->diffopt
.file
);
690 if (!rev
->diffopt
.flags
.textconv_set_via_cmdline
||
691 !rev
->diffopt
.flags
.allow_textconv
)
692 return stream_blob_to_fd(1, oid
, NULL
, 0);
694 if (get_oid_with_context(the_repository
, obj_name
,
696 &oidc
, &obj_context
))
697 die(_("not a valid object name %s"), obj_name
);
698 if (!obj_context
.path
||
699 !textconv_object(the_repository
, obj_context
.path
,
700 obj_context
.mode
, &oidc
, 1, &buf
, &size
)) {
701 object_context_release(&obj_context
);
702 return stream_blob_to_fd(1, oid
, NULL
, 0);
706 die(_("git show %s: bad file"), obj_name
);
708 write_or_die(1, buf
, size
);
709 object_context_release(&obj_context
);
713 static int show_tag_object(const struct object_id
*oid
, struct rev_info
*rev
)
716 enum object_type type
;
717 char *buf
= repo_read_object_file(the_repository
, oid
, &type
, &size
);
721 return error(_("could not read object %s"), oid_to_hex(oid
));
723 assert(type
== OBJ_TAG
);
724 while (offset
< size
&& buf
[offset
] != '\n') {
725 int new_offset
= offset
+ 1;
727 while (new_offset
< size
&& buf
[new_offset
++] != '\n')
729 if (skip_prefix(buf
+ offset
, "tagger ", &ident
))
730 show_tagger(ident
, rev
);
735 fwrite(buf
+ offset
, size
- offset
, 1, rev
->diffopt
.file
);
740 static int show_tree_object(const struct object_id
*oid UNUSED
,
741 struct strbuf
*base UNUSED
,
742 const char *pathname
, unsigned mode
,
745 FILE *file
= context
;
746 fprintf(file
, "%s%s\n", pathname
, S_ISDIR(mode
) ? "/" : "");
750 static void show_setup_revisions_tweak(struct rev_info
*rev
)
752 if (rev
->first_parent_only
)
753 diff_merges_default_to_first_parent(rev
);
755 diff_merges_default_to_dense_combined(rev
);
756 if (!rev
->diffopt
.output_format
)
757 rev
->diffopt
.output_format
= DIFF_FORMAT_PATCH
;
760 int cmd_show(int argc
, const char **argv
, const char *prefix
)
762 struct log_config cfg
;
765 struct setup_revision_opt opt
;
766 struct pathspec match_all
;
769 log_config_init(&cfg
);
770 init_diff_ui_defaults();
771 git_config(git_log_config
, &cfg
);
773 if (the_repository
->gitdir
) {
774 prepare_repo_settings(the_repository
);
775 the_repository
->settings
.command_requires_full_index
= 0;
778 memset(&match_all
, 0, sizeof(match_all
));
779 repo_init_revisions(the_repository
, &rev
, prefix
);
780 git_config(grep_config
, &rev
.grep_filter
);
783 rev
.always_show_header
= 1;
785 rev
.diffopt
.stat_width
= -1; /* Scale to real terminal size */
787 memset(&opt
, 0, sizeof(opt
));
789 opt
.tweak
= show_setup_revisions_tweak
;
790 cmd_log_init(argc
, argv
, prefix
, &rev
, &opt
, &cfg
);
793 ret
= cmd_log_walk(&rev
);
794 release_revisions(&rev
);
795 log_config_release(&cfg
);
799 rev
.diffopt
.no_free
= 1;
800 for (i
= 0; i
< rev
.pending
.nr
&& !ret
; i
++) {
801 struct object
*o
= rev
.pending
.objects
[i
].item
;
802 const char *name
= rev
.pending
.objects
[i
].name
;
805 ret
= show_blob_object(&o
->oid
, &rev
, name
);
808 struct tag
*t
= (struct tag
*)o
;
809 struct object_id
*oid
= get_tagged_oid(t
);
813 fprintf(rev
.diffopt
.file
, "%stag %s%s\n",
814 diff_get_color_opt(&rev
.diffopt
, DIFF_COMMIT
),
816 diff_get_color_opt(&rev
.diffopt
, DIFF_RESET
));
817 ret
= show_tag_object(&o
->oid
, &rev
);
821 o
= parse_object(the_repository
, oid
);
823 ret
= error(_("could not read object %s"),
825 rev
.pending
.objects
[i
].item
= o
;
832 fprintf(rev
.diffopt
.file
, "%stree %s%s\n\n",
833 diff_get_color_opt(&rev
.diffopt
, DIFF_COMMIT
),
835 diff_get_color_opt(&rev
.diffopt
, DIFF_RESET
));
836 read_tree(the_repository
, (struct tree
*)o
,
837 &match_all
, show_tree_object
,
843 struct object_array old
;
844 struct object_array blank
= OBJECT_ARRAY_INIT
;
846 memcpy(&old
, &rev
.pending
, sizeof(old
));
847 memcpy(&rev
.pending
, &blank
, sizeof(rev
.pending
));
849 add_object_array(o
, name
, &rev
.pending
);
850 ret
= cmd_log_walk_no_free(&rev
);
854 * object_array_clear(&pending). It was
855 * cleared already in prepare_revision_walk()
857 memcpy(&rev
.pending
, &old
, sizeof(rev
.pending
));
861 ret
= error(_("unknown type: %d"), o
->type
);
865 rev
.diffopt
.no_free
= 0;
866 diff_free(&rev
.diffopt
);
867 release_revisions(&rev
);
868 log_config_release(&cfg
);
874 * This is equivalent to "git log -g --abbrev-commit --pretty=oneline"
876 int cmd_log_reflog(int argc
, const char **argv
, const char *prefix
)
878 struct log_config cfg
;
880 struct setup_revision_opt opt
;
883 log_config_init(&cfg
);
884 init_diff_ui_defaults();
885 git_config(git_log_config
, &cfg
);
887 repo_init_revisions(the_repository
, &rev
, prefix
);
888 init_reflog_walk(&rev
.reflog_info
);
889 git_config(grep_config
, &rev
.grep_filter
);
891 rev
.verbose_header
= 1;
892 memset(&opt
, 0, sizeof(opt
));
894 cmd_log_init_defaults(&rev
, &cfg
);
895 rev
.abbrev_commit
= 1;
896 rev
.commit_format
= CMIT_FMT_ONELINE
;
897 rev
.use_terminator
= 1;
898 rev
.always_show_header
= 1;
899 cmd_log_init_finish(argc
, argv
, prefix
, &rev
, &opt
, &cfg
);
901 ret
= cmd_log_walk(&rev
);
903 release_revisions(&rev
);
904 log_config_release(&cfg
);
908 static void log_setup_revisions_tweak(struct rev_info
*rev
)
910 if (rev
->diffopt
.flags
.default_follow_renames
&&
911 diff_check_follow_pathspec(&rev
->prune_data
, 0))
912 rev
->diffopt
.flags
.follow_renames
= 1;
914 if (rev
->first_parent_only
)
915 diff_merges_default_to_first_parent(rev
);
918 int cmd_log(int argc
, const char **argv
, const char *prefix
)
920 struct log_config cfg
;
922 struct setup_revision_opt opt
;
925 log_config_init(&cfg
);
926 init_diff_ui_defaults();
927 git_config(git_log_config
, &cfg
);
929 repo_init_revisions(the_repository
, &rev
, prefix
);
930 git_config(grep_config
, &rev
.grep_filter
);
932 rev
.always_show_header
= 1;
933 memset(&opt
, 0, sizeof(opt
));
935 opt
.revarg_opt
= REVARG_COMMITTISH
;
936 opt
.tweak
= log_setup_revisions_tweak
;
937 cmd_log_init(argc
, argv
, prefix
, &rev
, &opt
, &cfg
);
939 ret
= cmd_log_walk(&rev
);
941 release_revisions(&rev
);
942 log_config_release(&cfg
);
961 enum cover_from_description
{
968 enum auto_base_setting
{
974 struct format_config
{
975 struct log_config log
;
976 enum thread_level thread
;
978 enum auto_base_setting auto_base
;
982 char *signature_file
;
983 enum cover_setting config_cover_letter
;
984 char *config_output_directory
;
985 enum cover_from_description cover_from_description_mode
;
987 struct display_notes_opt notes_opt
;
988 int numbered_cmdline_opt
;
991 char *default_attach
;
992 struct string_list extra_hdr
;
993 struct string_list extra_to
;
994 struct string_list extra_cc
;
997 struct strbuf sprefix
;
998 char *fmt_patch_suffix
;
1001 static void format_config_init(struct format_config
*cfg
)
1003 memset(cfg
, 0, sizeof(*cfg
));
1004 log_config_init(&cfg
->log
);
1005 cfg
->cover_from_description_mode
= COVER_FROM_MESSAGE
;
1006 cfg
->auto_number
= 1;
1007 string_list_init_dup(&cfg
->extra_hdr
);
1008 string_list_init_dup(&cfg
->extra_to
);
1009 string_list_init_dup(&cfg
->extra_cc
);
1010 strbuf_init(&cfg
->sprefix
, 0);
1011 cfg
->fmt_patch_suffix
= xstrdup(".patch");
1014 static void format_config_release(struct format_config
*cfg
)
1016 log_config_release(&cfg
->log
);
1017 free(cfg
->base_commit
);
1019 free(cfg
->signature
);
1020 free(cfg
->signature_file
);
1021 free(cfg
->config_output_directory
);
1022 free(cfg
->default_attach
);
1023 string_list_clear(&cfg
->extra_hdr
, 0);
1024 string_list_clear(&cfg
->extra_to
, 0);
1025 string_list_clear(&cfg
->extra_cc
, 0);
1026 strbuf_release(&cfg
->sprefix
);
1027 free(cfg
->fmt_patch_suffix
);
1030 static enum cover_from_description
parse_cover_from_description(const char *arg
)
1032 if (!arg
|| !strcmp(arg
, "default"))
1033 return COVER_FROM_MESSAGE
;
1034 else if (!strcmp(arg
, "none"))
1035 return COVER_FROM_NONE
;
1036 else if (!strcmp(arg
, "message"))
1037 return COVER_FROM_MESSAGE
;
1038 else if (!strcmp(arg
, "subject"))
1039 return COVER_FROM_SUBJECT
;
1040 else if (!strcmp(arg
, "auto"))
1041 return COVER_FROM_AUTO
;
1043 die(_("%s: invalid cover from description mode"), arg
);
1046 static void add_header(struct format_config
*cfg
, const char *value
)
1048 struct string_list_item
*item
;
1049 int len
= strlen(value
);
1050 while (len
&& value
[len
- 1] == '\n')
1053 if (!strncasecmp(value
, "to: ", 4)) {
1054 item
= string_list_append(&cfg
->extra_to
, value
+ 4);
1056 } else if (!strncasecmp(value
, "cc: ", 4)) {
1057 item
= string_list_append(&cfg
->extra_cc
, value
+ 4);
1060 item
= string_list_append(&cfg
->extra_hdr
, value
);
1063 item
->string
[len
] = '\0';
1066 static int git_format_config(const char *var
, const char *value
,
1067 const struct config_context
*ctx
, void *cb
)
1069 struct format_config
*cfg
= cb
;
1071 if (!strcmp(var
, "format.headers")) {
1073 die(_("format.headers without value"));
1074 add_header(cfg
, value
);
1077 if (!strcmp(var
, "format.suffix")) {
1078 FREE_AND_NULL(cfg
->fmt_patch_suffix
);
1079 return git_config_string(&cfg
->fmt_patch_suffix
, var
, value
);
1081 if (!strcmp(var
, "format.to")) {
1083 return config_error_nonbool(var
);
1084 string_list_append(&cfg
->extra_to
, value
);
1087 if (!strcmp(var
, "format.cc")) {
1089 return config_error_nonbool(var
);
1090 string_list_append(&cfg
->extra_cc
, value
);
1093 if (!strcmp(var
, "diff.color") || !strcmp(var
, "color.diff") ||
1094 !strcmp(var
, "color.ui") || !strcmp(var
, "diff.submodule")) {
1097 if (!strcmp(var
, "format.numbered")) {
1098 if (value
&& !strcasecmp(value
, "auto")) {
1099 cfg
->auto_number
= 1;
1102 cfg
->numbered
= git_config_bool(var
, value
);
1103 cfg
->auto_number
= cfg
->auto_number
&& cfg
->numbered
;
1106 if (!strcmp(var
, "format.attach")) {
1107 if (value
&& *value
) {
1108 FREE_AND_NULL(cfg
->default_attach
);
1109 cfg
->default_attach
= xstrdup(value
);
1110 } else if (value
&& !*value
) {
1111 FREE_AND_NULL(cfg
->default_attach
);
1113 FREE_AND_NULL(cfg
->default_attach
);
1114 cfg
->default_attach
= xstrdup(git_version_string
);
1118 if (!strcmp(var
, "format.thread")) {
1119 if (value
&& !strcasecmp(value
, "deep")) {
1120 cfg
->thread
= THREAD_DEEP
;
1123 if (value
&& !strcasecmp(value
, "shallow")) {
1124 cfg
->thread
= THREAD_SHALLOW
;
1127 cfg
->thread
= git_config_bool(var
, value
) ? THREAD_SHALLOW
: THREAD_UNSET
;
1130 if (!strcmp(var
, "format.signoff")) {
1131 cfg
->do_signoff
= git_config_bool(var
, value
);
1134 if (!strcmp(var
, "format.signature")) {
1135 FREE_AND_NULL(cfg
->signature
);
1136 return git_config_string(&cfg
->signature
, var
, value
);
1138 if (!strcmp(var
, "format.signaturefile")) {
1139 FREE_AND_NULL(cfg
->signature_file
);
1140 return git_config_pathname(&cfg
->signature_file
, var
, value
);
1142 if (!strcmp(var
, "format.coverletter")) {
1143 if (value
&& !strcasecmp(value
, "auto")) {
1144 cfg
->config_cover_letter
= COVER_AUTO
;
1147 cfg
->config_cover_letter
= git_config_bool(var
, value
) ? COVER_ON
: COVER_OFF
;
1150 if (!strcmp(var
, "format.outputdirectory")) {
1151 FREE_AND_NULL(cfg
->config_output_directory
);
1152 return git_config_string(&cfg
->config_output_directory
, var
, value
);
1154 if (!strcmp(var
, "format.useautobase")) {
1155 if (value
&& !strcasecmp(value
, "whenAble")) {
1156 cfg
->auto_base
= AUTO_BASE_WHEN_ABLE
;
1159 cfg
->auto_base
= git_config_bool(var
, value
) ? AUTO_BASE_ALWAYS
: AUTO_BASE_NEVER
;
1162 if (!strcmp(var
, "format.from")) {
1163 int b
= git_parse_maybe_bool(value
);
1164 FREE_AND_NULL(cfg
->from
);
1166 cfg
->from
= xstrdup(value
);
1168 cfg
->from
= xstrdup(git_committer_info(IDENT_NO_DATE
));
1171 if (!strcmp(var
, "format.forceinbodyfrom")) {
1172 force_in_body_from
= git_config_bool(var
, value
);
1175 if (!strcmp(var
, "format.notes")) {
1176 int b
= git_parse_maybe_bool(value
);
1178 enable_ref_display_notes(&cfg
->notes_opt
, &cfg
->show_notes
, value
);
1180 enable_default_display_notes(&cfg
->notes_opt
, &cfg
->show_notes
);
1182 disable_display_notes(&cfg
->notes_opt
, &cfg
->show_notes
);
1185 if (!strcmp(var
, "format.coverfromdescription")) {
1186 cfg
->cover_from_description_mode
= parse_cover_from_description(value
);
1189 if (!strcmp(var
, "format.mboxrd")) {
1190 stdout_mboxrd
= git_config_bool(var
, value
);
1193 if (!strcmp(var
, "format.noprefix")) {
1194 format_no_prefix
= 1;
1199 * ignore some porcelain config which would otherwise be parsed by
1200 * git_diff_ui_config(), via git_log_config(); we can't just avoid
1201 * diff_ui_config completely, because we do care about some ui options
1204 if (!strcmp(var
, "diff.noprefix"))
1207 return git_log_config(var
, value
, ctx
, &cfg
->log
);
1210 static const char *output_directory
= NULL
;
1211 static int outdir_offset
;
1213 static int open_next_file(struct commit
*commit
, const char *subject
,
1214 struct rev_info
*rev
, int quiet
)
1216 struct strbuf filename
= STRBUF_INIT
;
1218 if (output_directory
) {
1219 strbuf_addstr(&filename
, output_directory
);
1220 strbuf_complete(&filename
, '/');
1223 if (rev
->numbered_files
)
1224 strbuf_addf(&filename
, "%d", rev
->nr
);
1226 fmt_output_commit(&filename
, commit
, rev
);
1228 fmt_output_subject(&filename
, subject
, rev
);
1231 printf("%s\n", filename
.buf
+ outdir_offset
);
1233 if (!(rev
->diffopt
.file
= fopen(filename
.buf
, "w"))) {
1234 error_errno(_("cannot open patch file %s"), filename
.buf
);
1235 strbuf_release(&filename
);
1239 strbuf_release(&filename
);
1243 static void get_patch_ids(struct rev_info
*rev
, struct patch_ids
*ids
)
1245 struct rev_info check_rev
;
1246 struct commit
*commit
, *c1
, *c2
;
1247 struct object
*o1
, *o2
;
1248 unsigned flags1
, flags2
;
1250 if (rev
->pending
.nr
!= 2)
1251 die(_("need exactly one range"));
1253 o1
= rev
->pending
.objects
[0].item
;
1254 o2
= rev
->pending
.objects
[1].item
;
1257 c1
= lookup_commit_reference(the_repository
, &o1
->oid
);
1258 c2
= lookup_commit_reference(the_repository
, &o2
->oid
);
1260 if ((flags1
& UNINTERESTING
) == (flags2
& UNINTERESTING
))
1261 die(_("not a range"));
1263 init_patch_ids(the_repository
, ids
);
1265 /* given a range a..b get all patch ids for b..a */
1266 repo_init_revisions(the_repository
, &check_rev
, rev
->prefix
);
1267 check_rev
.max_parents
= 1;
1268 o1
->flags
^= UNINTERESTING
;
1269 o2
->flags
^= UNINTERESTING
;
1270 add_pending_object(&check_rev
, o1
, "o1");
1271 add_pending_object(&check_rev
, o2
, "o2");
1272 if (prepare_revision_walk(&check_rev
))
1273 die(_("revision walk setup failed"));
1275 while ((commit
= get_revision(&check_rev
)) != NULL
) {
1276 add_commit_patch_id(commit
, ids
);
1279 /* reset for next revision walk */
1280 clear_commit_marks(c1
, SEEN
| UNINTERESTING
| SHOWN
| ADDED
);
1281 clear_commit_marks(c2
, SEEN
| UNINTERESTING
| SHOWN
| ADDED
);
1286 static void gen_message_id(struct rev_info
*info
, const char *base
)
1288 struct strbuf buf
= STRBUF_INIT
;
1289 strbuf_addf(&buf
, "%s.%"PRItime
".git.%s", base
,
1290 (timestamp_t
) time(NULL
),
1291 git_committer_info(IDENT_NO_NAME
|IDENT_NO_DATE
|IDENT_STRICT
));
1292 info
->message_id
= strbuf_detach(&buf
, NULL
);
1295 static void print_signature(const char *signature
, FILE *file
)
1297 if (!signature
|| !*signature
)
1300 fprintf(file
, "-- \n%s", signature
);
1301 if (signature
[strlen(signature
)-1] != '\n')
1306 static char *find_branch_name(struct rev_info
*rev
)
1308 int i
, positive
= -1;
1309 struct object_id branch_oid
;
1310 const struct object_id
*tip_oid
;
1311 const char *ref
, *v
;
1312 char *full_ref
, *branch
= NULL
;
1314 for (i
= 0; i
< rev
->cmdline
.nr
; i
++) {
1315 if (rev
->cmdline
.rev
[i
].flags
& UNINTERESTING
)
1324 ref
= rev
->cmdline
.rev
[positive
].name
;
1325 tip_oid
= &rev
->cmdline
.rev
[positive
].item
->oid
;
1326 if (repo_dwim_ref(the_repository
, ref
, strlen(ref
), &branch_oid
,
1328 skip_prefix(full_ref
, "refs/heads/", &v
) &&
1329 oideq(tip_oid
, &branch_oid
))
1330 branch
= xstrdup(v
);
1335 static void show_diffstat(struct rev_info
*rev
,
1336 struct commit
*origin
, struct commit
*head
)
1338 struct diff_options opts
;
1340 memcpy(&opts
, &rev
->diffopt
, sizeof(opts
));
1341 opts
.output_format
= DIFF_FORMAT_SUMMARY
| DIFF_FORMAT_DIFFSTAT
;
1342 diff_setup_done(&opts
);
1344 diff_tree_oid(get_commit_tree_oid(origin
),
1345 get_commit_tree_oid(head
),
1347 diffcore_std(&opts
);
1350 fprintf(rev
->diffopt
.file
, "\n");
1353 static void read_desc_file(struct strbuf
*buf
, const char *desc_file
)
1355 if (strbuf_read_file(buf
, desc_file
, 0) < 0)
1356 die_errno(_("unable to read branch description file '%s'"),
1360 static void prepare_cover_text(struct pretty_print_context
*pp
,
1361 const char *description_file
,
1362 const char *branch_name
,
1364 const char *encoding
,
1366 const struct format_config
*cfg
)
1368 const char *subject
= "*** SUBJECT HERE ***";
1369 const char *body
= "*** BLURB HERE ***";
1370 struct strbuf description_sb
= STRBUF_INIT
;
1371 struct strbuf subject_sb
= STRBUF_INIT
;
1373 if (cfg
->cover_from_description_mode
== COVER_FROM_NONE
)
1376 if (description_file
&& *description_file
)
1377 read_desc_file(&description_sb
, description_file
);
1378 else if (branch_name
&& *branch_name
)
1379 read_branch_desc(&description_sb
, branch_name
);
1380 if (!description_sb
.len
)
1383 if (cfg
->cover_from_description_mode
== COVER_FROM_SUBJECT
||
1384 cfg
->cover_from_description_mode
== COVER_FROM_AUTO
)
1385 body
= format_subject(&subject_sb
, description_sb
.buf
, " ");
1387 if (cfg
->cover_from_description_mode
== COVER_FROM_MESSAGE
||
1388 (cfg
->cover_from_description_mode
== COVER_FROM_AUTO
&&
1389 subject_sb
.len
> COVER_FROM_AUTO_MAX_SUBJECT_LEN
))
1390 body
= description_sb
.buf
;
1392 subject
= subject_sb
.buf
;
1395 pp_email_subject(pp
, &subject
, sb
, encoding
, need_8bit_cte
);
1396 pp_remainder(pp
, &body
, sb
, 0);
1398 strbuf_release(&description_sb
);
1399 strbuf_release(&subject_sb
);
1402 static int get_notes_refs(struct string_list_item
*item
, void *arg
)
1404 strvec_pushf(arg
, "--notes=%s", item
->string
);
1408 static void get_notes_args(struct strvec
*arg
, struct rev_info
*rev
)
1410 if (!rev
->show_notes
) {
1411 strvec_push(arg
, "--no-notes");
1412 } else if (rev
->notes_opt
.use_default_notes
> 0 ||
1413 (rev
->notes_opt
.use_default_notes
== -1 &&
1414 !rev
->notes_opt
.extra_notes_refs
.nr
)) {
1415 strvec_push(arg
, "--notes");
1417 for_each_string_list(&rev
->notes_opt
.extra_notes_refs
, get_notes_refs
, arg
);
1421 static void make_cover_letter(struct rev_info
*rev
, int use_separate_file
,
1422 struct commit
*origin
,
1423 int nr
, struct commit
**list
,
1424 const char *description_file
,
1425 const char *branch_name
,
1427 const struct format_config
*cfg
)
1429 const char *committer
;
1430 struct shortlog log
;
1431 struct strbuf sb
= STRBUF_INIT
;
1433 const char *encoding
= "UTF-8";
1434 int need_8bit_cte
= 0;
1435 struct pretty_print_context pp
= {0};
1436 struct commit
*head
= list
[0];
1437 char *to_free
= NULL
;
1439 if (!cmit_fmt_is_mail(rev
->commit_format
))
1440 die(_("cover letter needs email format"));
1442 committer
= git_committer_info(0);
1444 if (use_separate_file
&&
1445 open_next_file(NULL
, rev
->numbered_files
? NULL
: "cover-letter", rev
, quiet
))
1446 die(_("failed to create cover-letter file"));
1448 log_write_email_headers(rev
, head
, &pp
.after_subject
, &need_8bit_cte
, 0);
1450 for (i
= 0; !need_8bit_cte
&& i
< nr
; i
++) {
1451 const char *buf
= repo_get_commit_buffer(the_repository
,
1453 if (has_non_ascii(buf
))
1455 repo_unuse_commit_buffer(the_repository
, list
[i
], buf
);
1459 branch_name
= to_free
= find_branch_name(rev
);
1461 pp
.fmt
= CMIT_FMT_EMAIL
;
1462 pp
.date_mode
.type
= DATE_RFC2822
;
1464 pp
.encode_email_headers
= rev
->encode_email_headers
;
1465 pp_user_info(&pp
, NULL
, &sb
, committer
, encoding
);
1466 prepare_cover_text(&pp
, description_file
, branch_name
, &sb
,
1467 encoding
, need_8bit_cte
, cfg
);
1468 fprintf(rev
->diffopt
.file
, "%s\n", sb
.buf
);
1471 free(pp
.after_subject
);
1472 strbuf_release(&sb
);
1474 shortlog_init(&log
);
1476 log
.wrap
= MAIL_DEFAULT_WRAP
;
1479 log
.file
= rev
->diffopt
.file
;
1480 log
.groups
= SHORTLOG_GROUP_AUTHOR
;
1481 shortlog_finish_setup(&log
);
1482 for (i
= 0; i
< nr
; i
++)
1483 shortlog_add_commit(&log
, list
[i
]);
1485 shortlog_output(&log
);
1487 /* We can only do diffstat with a unique reference point */
1489 show_diffstat(rev
, origin
, head
);
1491 if (rev
->idiff_oid1
) {
1492 fprintf_ln(rev
->diffopt
.file
, "%s", rev
->idiff_title
);
1493 show_interdiff(rev
->idiff_oid1
, rev
->idiff_oid2
, 0,
1499 * Pass minimum required diff-options to range-diff; others
1500 * can be added later if deemed desirable.
1502 struct diff_options opts
;
1503 struct strvec other_arg
= STRVEC_INIT
;
1504 struct range_diff_options range_diff_opts
= {
1505 .creation_factor
= rev
->creation_factor
,
1508 .other_arg
= &other_arg
1511 repo_diff_setup(the_repository
, &opts
);
1512 opts
.file
= rev
->diffopt
.file
;
1513 opts
.use_color
= rev
->diffopt
.use_color
;
1514 diff_setup_done(&opts
);
1515 fprintf_ln(rev
->diffopt
.file
, "%s", rev
->rdiff_title
);
1516 get_notes_args(&other_arg
, rev
);
1517 show_range_diff(rev
->rdiff1
, rev
->rdiff2
, &range_diff_opts
);
1518 strvec_clear(&other_arg
);
1522 static char *clean_message_id(const char *msg_id
)
1525 const char *a
, *z
, *m
;
1528 while ((ch
= *m
) && (isspace(ch
) || (ch
== '<')))
1533 if (!isspace(ch
) && (ch
!= '>'))
1538 die(_("insane in-reply-to: %s"), msg_id
);
1541 return xmemdupz(a
, z
- a
);
1544 static const char *set_outdir(const char *prefix
, const char *output_directory
)
1546 if (output_directory
&& is_absolute_path(output_directory
))
1547 return output_directory
;
1549 if (!prefix
|| !*prefix
) {
1550 if (output_directory
)
1551 return output_directory
;
1552 /* The user did not explicitly ask for "./" */
1557 outdir_offset
= strlen(prefix
);
1558 if (!output_directory
)
1561 return prefix_filename(prefix
, output_directory
);
1564 static const char * const builtin_format_patch_usage
[] = {
1565 N_("git format-patch [<options>] [<since> | <revision-range>]"),
1569 struct keep_callback_data
{
1570 struct format_config
*cfg
;
1571 struct rev_info
*revs
;
1574 static int keep_callback(const struct option
*opt
, const char *arg
, int unset
)
1576 struct keep_callback_data
*data
= opt
->value
;
1577 BUG_ON_OPT_NEG(unset
);
1578 BUG_ON_OPT_ARG(arg
);
1579 data
->revs
->total
= -1;
1580 data
->cfg
->keep_subject
= 1;
1584 static int subject_prefix_callback(const struct option
*opt
, const char *arg
,
1587 struct format_config
*cfg
= opt
->value
;
1589 BUG_ON_OPT_NEG(unset
);
1590 cfg
->subject_prefix
= 1;
1591 strbuf_reset(&cfg
->sprefix
);
1592 strbuf_addstr(&cfg
->sprefix
, arg
);
1596 static int rfc_callback(const struct option
*opt
, const char *arg
,
1599 const char **rfc
= opt
->value
;
1605 *rfc
= arg
? arg
: "RFC";
1609 static int numbered_callback(const struct option
*opt
, const char *arg
,
1612 struct format_config
*cfg
= opt
->value
;
1613 BUG_ON_OPT_ARG(arg
);
1614 cfg
->numbered
= cfg
->numbered_cmdline_opt
= unset
? 0 : 1;
1616 cfg
->auto_number
= 0;
1620 static int no_numbered_callback(const struct option
*opt
, const char *arg
,
1623 BUG_ON_OPT_NEG(unset
);
1624 return numbered_callback(opt
, arg
, 1);
1627 static int output_directory_callback(const struct option
*opt
, const char *arg
,
1630 const char **dir
= (const char **)opt
->value
;
1631 BUG_ON_OPT_NEG(unset
);
1633 die(_("two output directories?"));
1638 static int thread_callback(const struct option
*opt
, const char *arg
, int unset
)
1640 struct format_config
*cfg
= opt
->value
;
1643 cfg
->thread
= THREAD_UNSET
;
1644 else if (!arg
|| !strcmp(arg
, "shallow"))
1645 cfg
->thread
= THREAD_SHALLOW
;
1646 else if (!strcmp(arg
, "deep"))
1647 cfg
->thread
= THREAD_DEEP
;
1649 * Please update _git_formatpatch() in git-completion.bash
1650 * when you add new options.
1657 static int attach_callback(const struct option
*opt
, const char *arg
, int unset
)
1659 struct rev_info
*rev
= (struct rev_info
*)opt
->value
;
1661 rev
->mime_boundary
= NULL
;
1663 rev
->mime_boundary
= arg
;
1665 rev
->mime_boundary
= git_version_string
;
1666 rev
->no_inline
= unset
? 0 : 1;
1670 static int inline_callback(const struct option
*opt
, const char *arg
, int unset
)
1672 struct rev_info
*rev
= (struct rev_info
*)opt
->value
;
1674 rev
->mime_boundary
= NULL
;
1676 rev
->mime_boundary
= arg
;
1678 rev
->mime_boundary
= git_version_string
;
1683 static int header_callback(const struct option
*opt
, const char *arg
,
1686 struct format_config
*cfg
= opt
->value
;
1689 string_list_clear(&cfg
->extra_hdr
, 0);
1690 string_list_clear(&cfg
->extra_to
, 0);
1691 string_list_clear(&cfg
->extra_cc
, 0);
1693 add_header(cfg
, arg
);
1698 static int from_callback(const struct option
*opt
, const char *arg
, int unset
)
1700 char **from
= opt
->value
;
1707 *from
= xstrdup(arg
);
1709 *from
= xstrdup(git_committer_info(IDENT_NO_DATE
));
1713 static int base_callback(const struct option
*opt
, const char *arg
, int unset
)
1715 struct format_config
*cfg
= opt
->value
;
1718 cfg
->auto_base
= AUTO_BASE_NEVER
;
1719 FREE_AND_NULL(cfg
->base_commit
);
1720 } else if (!strcmp(arg
, "auto")) {
1721 cfg
->auto_base
= AUTO_BASE_ALWAYS
;
1722 FREE_AND_NULL(cfg
->base_commit
);
1724 cfg
->auto_base
= AUTO_BASE_NEVER
;
1725 cfg
->base_commit
= xstrdup(arg
);
1730 struct base_tree_info
{
1731 struct object_id base_commit
;
1732 int nr_patch_id
, alloc_patch_id
;
1733 struct object_id
*patch_id
;
1736 static struct commit
*get_base_commit(const struct format_config
*cfg
,
1737 struct commit
**list
,
1740 struct commit
*base
= NULL
;
1741 struct commit
**rev
;
1742 int i
= 0, rev_nr
= 0, auto_select
, die_on_failure
, ret
;
1744 switch (cfg
->auto_base
) {
1745 case AUTO_BASE_NEVER
:
1746 if (cfg
->base_commit
) {
1750 /* no base information is requested */
1754 case AUTO_BASE_ALWAYS
:
1755 case AUTO_BASE_WHEN_ABLE
:
1756 if (cfg
->base_commit
) {
1757 BUG("requested automatic base selection but a commit was provided");
1760 die_on_failure
= cfg
->auto_base
== AUTO_BASE_ALWAYS
;
1764 BUG("unexpected automatic base selection method");
1768 base
= lookup_commit_reference_by_name(cfg
->base_commit
);
1770 die(_("unknown commit %s"), cfg
->base_commit
);
1772 struct branch
*curr_branch
= branch_get(NULL
);
1773 const char *upstream
= branch_get_upstream(curr_branch
, NULL
);
1775 struct commit_list
*base_list
= NULL
;
1776 struct commit
*commit
;
1777 struct object_id oid
;
1779 if (repo_get_oid(the_repository
, upstream
, &oid
)) {
1781 die(_("failed to resolve '%s' as a valid ref"), upstream
);
1785 commit
= lookup_commit_or_die(&oid
, "upstream base");
1786 if (repo_get_merge_bases_many(the_repository
,
1790 /* There should be one and only one merge base. */
1791 !base_list
|| base_list
->next
) {
1792 if (die_on_failure
) {
1793 die(_("could not find exact merge base"));
1795 free_commit_list(base_list
);
1799 base
= base_list
->item
;
1800 free_commit_list(base_list
);
1803 die(_("failed to get upstream, if you want to record base commit automatically,\n"
1804 "please use git branch --set-upstream-to to track a remote branch.\n"
1805 "Or you could specify base commit by --base=<base-commit-id> manually"));
1811 ALLOC_ARRAY(rev
, total
);
1812 for (i
= 0; i
< total
; i
++)
1817 * Get merge base through pair-wise computations
1818 * and store it in rev[0].
1820 while (rev_nr
> 1) {
1821 for (i
= 0; i
< rev_nr
/ 2; i
++) {
1822 struct commit_list
*merge_base
= NULL
;
1823 if (repo_get_merge_bases(the_repository
,
1825 rev
[2 * i
+ 1], &merge_base
) < 0 ||
1826 !merge_base
|| merge_base
->next
) {
1827 if (die_on_failure
) {
1828 die(_("failed to find exact merge base"));
1835 rev
[i
] = merge_base
->item
;
1839 rev
[i
] = rev
[2 * i
];
1840 rev_nr
= DIV_ROUND_UP(rev_nr
, 2);
1843 ret
= repo_in_merge_bases(the_repository
, base
, rev
[0]);
1847 if (die_on_failure
) {
1848 die(_("base commit should be the ancestor of revision list"));
1855 for (i
= 0; i
< total
; i
++) {
1856 if (base
== list
[i
]) {
1857 if (die_on_failure
) {
1858 die(_("base commit shouldn't be in revision list"));
1870 define_commit_slab(commit_base
, int);
1872 static void prepare_bases(struct base_tree_info
*bases
,
1873 struct commit
*base
,
1874 struct commit
**list
,
1877 struct commit
*commit
;
1878 struct rev_info revs
;
1879 struct diff_options diffopt
;
1880 struct commit_base commit_base
;
1886 init_commit_base(&commit_base
);
1887 repo_diff_setup(the_repository
, &diffopt
);
1888 diffopt
.flags
.recursive
= 1;
1889 diff_setup_done(&diffopt
);
1891 oidcpy(&bases
->base_commit
, &base
->object
.oid
);
1893 repo_init_revisions(the_repository
, &revs
, NULL
);
1894 revs
.max_parents
= 1;
1895 revs
.topo_order
= 1;
1896 for (i
= 0; i
< total
; i
++) {
1897 list
[i
]->object
.flags
&= ~UNINTERESTING
;
1898 add_pending_object(&revs
, &list
[i
]->object
, "rev_list");
1899 *commit_base_at(&commit_base
, list
[i
]) = 1;
1901 base
->object
.flags
|= UNINTERESTING
;
1902 add_pending_object(&revs
, &base
->object
, "base");
1904 if (prepare_revision_walk(&revs
))
1905 die(_("revision walk setup failed"));
1907 * Traverse the commits list, get prerequisite patch ids
1908 * and stuff them in bases structure.
1910 while ((commit
= get_revision(&revs
)) != NULL
) {
1911 struct object_id oid
;
1912 struct object_id
*patch_id
;
1913 if (*commit_base_at(&commit_base
, commit
))
1915 if (commit_patch_id(commit
, &diffopt
, &oid
, 0))
1916 die(_("cannot get patch id"));
1917 ALLOC_GROW(bases
->patch_id
, bases
->nr_patch_id
+ 1, bases
->alloc_patch_id
);
1918 patch_id
= bases
->patch_id
+ bases
->nr_patch_id
;
1919 oidcpy(patch_id
, &oid
);
1920 bases
->nr_patch_id
++;
1922 clear_commit_base(&commit_base
);
1925 static void print_bases(struct base_tree_info
*bases
, FILE *file
)
1929 /* Only do this once, either for the cover or for the first one */
1930 if (is_null_oid(&bases
->base_commit
))
1933 /* Show the base commit */
1934 fprintf(file
, "\nbase-commit: %s\n", oid_to_hex(&bases
->base_commit
));
1936 /* Show the prerequisite patches */
1937 for (i
= bases
->nr_patch_id
- 1; i
>= 0; i
--)
1938 fprintf(file
, "prerequisite-patch-id: %s\n", oid_to_hex(&bases
->patch_id
[i
]));
1940 free(bases
->patch_id
);
1941 bases
->nr_patch_id
= 0;
1942 bases
->alloc_patch_id
= 0;
1943 oidclr(&bases
->base_commit
, the_repository
->hash_algo
);
1946 static const char *diff_title(struct strbuf
*sb
,
1947 const char *reroll_count
,
1948 const char *generic
,
1949 const char *rerolled
)
1953 /* RFC may be v0, so allow -v1 to diff against v0 */
1954 if (reroll_count
&& !strtol_i(reroll_count
, 10, &v
) &&
1956 strbuf_addf(sb
, rerolled
, v
- 1);
1958 strbuf_addstr(sb
, generic
);
1962 static void infer_range_diff_ranges(struct strbuf
*r1
,
1965 struct commit
*origin
,
1966 struct commit
*head
)
1968 const char *head_oid
= oid_to_hex(&head
->object
.oid
);
1969 int prev_is_range
= is_range_diff_range(prev
);
1972 strbuf_addstr(r1
, prev
);
1974 strbuf_addf(r1
, "%s..%s", head_oid
, prev
);
1977 strbuf_addf(r2
, "%s..%s", oid_to_hex(&origin
->object
.oid
), head_oid
);
1978 else if (prev_is_range
)
1979 die(_("failed to infer range-diff origin of current series"));
1981 warning(_("using '%s' as range-diff origin of current series"), prev
);
1982 strbuf_addf(r2
, "%s..%s", prev
, head_oid
);
1986 int cmd_format_patch(int argc
, const char **argv
, const char *prefix
)
1988 struct format_config cfg
;
1989 struct commit
*commit
;
1990 struct commit
**list
= NULL
;
1991 struct rev_info rev
;
1992 char *to_free
= NULL
;
1993 struct setup_revision_opt s_r_opt
;
1994 int nr
= 0, total
, i
;
1996 int start_number
= -1;
1997 int just_numbers
= 0;
1998 int ignore_if_in_upstream
= 0;
1999 int cover_letter
= -1;
2000 int boundary_count
= 0;
2001 int no_binary_diff
= 0;
2002 int zero_commit
= 0;
2003 struct commit
*origin
= NULL
;
2004 const char *in_reply_to
= NULL
;
2005 struct patch_ids ids
;
2006 struct strbuf buf
= STRBUF_INIT
;
2007 int use_patch_format
= 0;
2009 const char *reroll_count
= NULL
;
2010 char *cover_from_description_arg
= NULL
;
2011 char *description_file
= NULL
;
2012 char *branch_name
= NULL
;
2013 struct base_tree_info bases
;
2014 struct commit
*base
;
2015 int show_progress
= 0;
2016 struct progress
*progress
= NULL
;
2017 struct oid_array idiff_prev
= OID_ARRAY_INIT
;
2018 struct strbuf idiff_title
= STRBUF_INIT
;
2019 const char *rdiff_prev
= NULL
;
2020 struct strbuf rdiff1
= STRBUF_INIT
;
2021 struct strbuf rdiff2
= STRBUF_INIT
;
2022 struct strbuf rdiff_title
= STRBUF_INIT
;
2023 const char *rfc
= NULL
;
2024 int creation_factor
= -1;
2025 const char *signature
= git_version_string
;
2026 char *signature_file_arg
= NULL
;
2027 struct keep_callback_data keep_callback_data
= {
2031 const char *fmt_patch_suffix
= NULL
;
2033 const struct option builtin_format_patch_options
[] = {
2034 OPT_CALLBACK_F('n', "numbered", &cfg
, NULL
,
2035 N_("use [PATCH n/m] even with a single patch"),
2036 PARSE_OPT_NOARG
, numbered_callback
),
2037 OPT_CALLBACK_F('N', "no-numbered", &cfg
, NULL
,
2038 N_("use [PATCH] even with multiple patches"),
2039 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
, no_numbered_callback
),
2040 OPT_BOOL('s', "signoff", &cfg
.do_signoff
, N_("add a Signed-off-by trailer")),
2041 OPT_BOOL(0, "stdout", &use_stdout
,
2042 N_("print patches to standard out")),
2043 OPT_BOOL(0, "cover-letter", &cover_letter
,
2044 N_("generate a cover letter")),
2045 OPT_BOOL(0, "numbered-files", &just_numbers
,
2046 N_("use simple number sequence for output file names")),
2047 OPT_STRING(0, "suffix", &fmt_patch_suffix
, N_("sfx"),
2048 N_("use <sfx> instead of '.patch'")),
2049 OPT_INTEGER(0, "start-number", &start_number
,
2050 N_("start numbering patches at <n> instead of 1")),
2051 OPT_STRING('v', "reroll-count", &reroll_count
, N_("reroll-count"),
2052 N_("mark the series as Nth re-roll")),
2053 OPT_INTEGER(0, "filename-max-length", &cfg
.log
.fmt_patch_name_max
,
2054 N_("max length of output filename")),
2055 OPT_CALLBACK_F(0, "rfc", &rfc
, N_("rfc"),
2056 N_("add <rfc> (default 'RFC') before 'PATCH'"),
2057 PARSE_OPT_OPTARG
, rfc_callback
),
2058 OPT_STRING(0, "cover-from-description", &cover_from_description_arg
,
2059 N_("cover-from-description-mode"),
2060 N_("generate parts of a cover letter based on a branch's description")),
2061 OPT_FILENAME(0, "description-file", &description_file
,
2062 N_("use branch description from file")),
2063 OPT_CALLBACK_F(0, "subject-prefix", &cfg
, N_("prefix"),
2064 N_("use [<prefix>] instead of [PATCH]"),
2065 PARSE_OPT_NONEG
, subject_prefix_callback
),
2066 OPT_CALLBACK_F('o', "output-directory", &output_directory
,
2067 N_("dir"), N_("store resulting files in <dir>"),
2068 PARSE_OPT_NONEG
, output_directory_callback
),
2069 OPT_CALLBACK_F('k', "keep-subject", &keep_callback_data
, NULL
,
2070 N_("don't strip/add [PATCH]"),
2071 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
, keep_callback
),
2072 OPT_BOOL(0, "no-binary", &no_binary_diff
,
2073 N_("don't output binary diffs")),
2074 OPT_BOOL(0, "zero-commit", &zero_commit
,
2075 N_("output all-zero hash in From header")),
2076 OPT_BOOL(0, "ignore-if-in-upstream", &ignore_if_in_upstream
,
2077 N_("don't include a patch matching a commit upstream")),
2078 OPT_SET_INT_F('p', "no-stat", &use_patch_format
,
2079 N_("show patch format instead of default (patch + stat)"),
2080 1, PARSE_OPT_NONEG
),
2081 OPT_GROUP(N_("Messaging")),
2082 OPT_CALLBACK(0, "add-header", &cfg
, N_("header"),
2083 N_("add email header"), header_callback
),
2084 OPT_STRING_LIST(0, "to", &cfg
.extra_to
, N_("email"), N_("add To: header")),
2085 OPT_STRING_LIST(0, "cc", &cfg
.extra_cc
, N_("email"), N_("add Cc: header")),
2086 OPT_CALLBACK_F(0, "from", &cfg
.from
, N_("ident"),
2087 N_("set From address to <ident> (or committer ident if absent)"),
2088 PARSE_OPT_OPTARG
, from_callback
),
2089 OPT_STRING(0, "in-reply-to", &in_reply_to
, N_("message-id"),
2090 N_("make first mail a reply to <message-id>")),
2091 OPT_CALLBACK_F(0, "attach", &rev
, N_("boundary"),
2092 N_("attach the patch"), PARSE_OPT_OPTARG
,
2094 OPT_CALLBACK_F(0, "inline", &rev
, N_("boundary"),
2095 N_("inline the patch"),
2096 PARSE_OPT_OPTARG
| PARSE_OPT_NONEG
,
2098 OPT_CALLBACK_F(0, "thread", &cfg
, N_("style"),
2099 N_("enable message threading, styles: shallow, deep"),
2100 PARSE_OPT_OPTARG
, thread_callback
),
2101 OPT_STRING(0, "signature", &signature
, N_("signature"),
2102 N_("add a signature")),
2103 OPT_CALLBACK_F(0, "base", &cfg
, N_("base-commit"),
2104 N_("add prerequisite tree info to the patch series"),
2106 OPT_FILENAME(0, "signature-file", &signature_file_arg
,
2107 N_("add a signature from a file")),
2108 OPT__QUIET(&quiet
, N_("don't print the patch filenames")),
2109 OPT_BOOL(0, "progress", &show_progress
,
2110 N_("show progress while generating patches")),
2111 OPT_CALLBACK(0, "interdiff", &idiff_prev
, N_("rev"),
2112 N_("show changes against <rev> in cover letter or single patch"),
2113 parse_opt_object_name
),
2114 OPT_STRING(0, "range-diff", &rdiff_prev
, N_("refspec"),
2115 N_("show changes against <refspec> in cover letter or single patch")),
2116 OPT_INTEGER(0, "creation-factor", &creation_factor
,
2117 N_("percentage by which creation is weighted")),
2118 OPT_BOOL(0, "force-in-body-from", &force_in_body_from
,
2119 N_("show in-body From: even if identical to the e-mail header")),
2123 format_config_init(&cfg
);
2124 init_diff_ui_defaults();
2125 init_display_notes(&cfg
.notes_opt
);
2126 git_config(git_format_config
, &cfg
);
2127 repo_init_revisions(the_repository
, &rev
, prefix
);
2128 git_config(grep_config
, &rev
.grep_filter
);
2130 rev
.show_notes
= cfg
.show_notes
;
2131 memcpy(&rev
.notes_opt
, &cfg
.notes_opt
, sizeof(cfg
.notes_opt
));
2132 rev
.commit_format
= CMIT_FMT_EMAIL
;
2133 rev
.encode_email_headers
= cfg
.log
.default_encode_email_headers
;
2134 rev
.expand_tabs_in_log_default
= 0;
2135 rev
.verbose_header
= 1;
2137 rev
.max_parents
= 1;
2138 rev
.diffopt
.flags
.recursive
= 1;
2139 rev
.diffopt
.no_free
= 1;
2140 memset(&s_r_opt
, 0, sizeof(s_r_opt
));
2141 s_r_opt
.def
= "HEAD";
2142 s_r_opt
.revarg_opt
= REVARG_COMMITTISH
;
2144 strbuf_addstr(&cfg
.sprefix
, cfg
.log
.fmt_patch_subject_prefix
);
2145 if (format_no_prefix
)
2146 diff_set_noprefix(&rev
.diffopt
);
2148 if (cfg
.default_attach
) {
2149 rev
.mime_boundary
= cfg
.default_attach
;
2154 * Parse the arguments before setup_revisions(), or something
2155 * like "git format-patch -o a123 HEAD^.." may fail; a123 is
2156 * possibly a valid SHA1.
2158 argc
= parse_options(argc
, argv
, prefix
, builtin_format_patch_options
,
2159 builtin_format_patch_usage
,
2160 PARSE_OPT_KEEP_ARGV0
| PARSE_OPT_KEEP_UNKNOWN_OPT
|
2161 PARSE_OPT_KEEP_DASHDASH
);
2163 rev
.force_in_body_from
= force_in_body_from
;
2165 if (!fmt_patch_suffix
)
2166 fmt_patch_suffix
= cfg
.fmt_patch_suffix
;
2168 /* Make sure "0000-$sub.patch" gives non-negative length for $sub */
2169 if (cfg
.log
.fmt_patch_name_max
<= strlen("0000-") + strlen(fmt_patch_suffix
))
2170 cfg
.log
.fmt_patch_name_max
= strlen("0000-") + strlen(fmt_patch_suffix
);
2172 if (cover_from_description_arg
)
2173 cfg
.cover_from_description_mode
= parse_cover_from_description(cover_from_description_arg
);
2175 if (rfc
&& rfc
[0]) {
2176 cfg
.subject_prefix
= 1;
2178 strbuf_addf(&cfg
.sprefix
, " %s", rfc
+ 1);
2180 strbuf_insertf(&cfg
.sprefix
, 0, "%s ", rfc
);
2184 strbuf_addf(&cfg
.sprefix
, " v%s", reroll_count
);
2185 rev
.reroll_count
= reroll_count
;
2188 rev
.subject_prefix
= cfg
.sprefix
.buf
;
2190 for (i
= 0; i
< cfg
.extra_hdr
.nr
; i
++) {
2191 strbuf_addstr(&buf
, cfg
.extra_hdr
.items
[i
].string
);
2192 strbuf_addch(&buf
, '\n');
2195 if (cfg
.extra_to
.nr
)
2196 strbuf_addstr(&buf
, "To: ");
2197 for (i
= 0; i
< cfg
.extra_to
.nr
; i
++) {
2199 strbuf_addstr(&buf
, " ");
2200 strbuf_addstr(&buf
, cfg
.extra_to
.items
[i
].string
);
2201 if (i
+ 1 < cfg
.extra_to
.nr
)
2202 strbuf_addch(&buf
, ',');
2203 strbuf_addch(&buf
, '\n');
2206 if (cfg
.extra_cc
.nr
)
2207 strbuf_addstr(&buf
, "Cc: ");
2208 for (i
= 0; i
< cfg
.extra_cc
.nr
; i
++) {
2210 strbuf_addstr(&buf
, " ");
2211 strbuf_addstr(&buf
, cfg
.extra_cc
.items
[i
].string
);
2212 if (i
+ 1 < cfg
.extra_cc
.nr
)
2213 strbuf_addch(&buf
, ',');
2214 strbuf_addch(&buf
, '\n');
2217 rev
.extra_headers
= to_free
= strbuf_detach(&buf
, NULL
);
2220 if (split_ident_line(&rev
.from_ident
, cfg
.from
, strlen(cfg
.from
)))
2221 die(_("invalid ident line: %s"), cfg
.from
);
2224 if (start_number
< 0)
2228 * If numbered is set solely due to format.numbered in config,
2229 * and it would conflict with --keep-subject (-k) from the
2230 * command line, reset "numbered".
2232 if (cfg
.numbered
&& cfg
.keep_subject
&& !cfg
.numbered_cmdline_opt
)
2235 if (cfg
.numbered
&& cfg
.keep_subject
)
2236 die(_("options '%s' and '%s' cannot be used together"), "-n", "-k");
2237 if (cfg
.keep_subject
&& cfg
.subject_prefix
)
2238 die(_("options '%s' and '%s' cannot be used together"), "--subject-prefix/--rfc", "-k");
2239 rev
.preserve_subject
= cfg
.keep_subject
;
2241 argc
= setup_revisions(argc
, argv
, &rev
, &s_r_opt
);
2243 die(_("unrecognized argument: %s"), argv
[1]);
2245 if (rev
.diffopt
.output_format
& DIFF_FORMAT_NAME
)
2246 die(_("--name-only does not make sense"));
2247 if (rev
.diffopt
.output_format
& DIFF_FORMAT_NAME_STATUS
)
2248 die(_("--name-status does not make sense"));
2249 if (rev
.diffopt
.output_format
& DIFF_FORMAT_CHECKDIFF
)
2250 die(_("--check does not make sense"));
2251 if (rev
.remerge_diff
)
2252 die(_("--remerge-diff does not make sense"));
2254 if (!use_patch_format
&&
2255 (!rev
.diffopt
.output_format
||
2256 rev
.diffopt
.output_format
== DIFF_FORMAT_PATCH
))
2257 rev
.diffopt
.output_format
= DIFF_FORMAT_DIFFSTAT
| DIFF_FORMAT_SUMMARY
;
2258 if (!rev
.diffopt
.stat_width
)
2259 rev
.diffopt
.stat_width
= MAIL_DEFAULT_WRAP
;
2261 /* Always generate a patch */
2262 rev
.diffopt
.output_format
|= DIFF_FORMAT_PATCH
;
2263 rev
.always_show_header
= 1;
2265 rev
.zero_commit
= zero_commit
;
2266 rev
.patch_name_max
= cfg
.log
.fmt_patch_name_max
;
2268 if (!rev
.diffopt
.flags
.text
&& !no_binary_diff
)
2269 rev
.diffopt
.flags
.binary
= 1;
2272 load_display_notes(&rev
.notes_opt
);
2274 die_for_incompatible_opt3(use_stdout
, "--stdout",
2275 rev
.diffopt
.close_file
, "--output",
2276 !!output_directory
, "--output-directory");
2278 if (use_stdout
&& stdout_mboxrd
)
2279 rev
.commit_format
= CMIT_FMT_MBOXRD
;
2283 } else if (!rev
.diffopt
.close_file
) {
2286 if (!output_directory
)
2287 output_directory
= cfg
.config_output_directory
;
2288 output_directory
= set_outdir(prefix
, output_directory
);
2290 if (rev
.diffopt
.use_color
!= GIT_COLOR_ALWAYS
)
2291 rev
.diffopt
.use_color
= GIT_COLOR_NEVER
;
2293 * We consider <outdir> as 'outside of gitdir', therefore avoid
2294 * applying adjust_shared_perm in s-c-l-d.
2296 saved
= get_shared_repository();
2297 set_shared_repository(0);
2298 switch (safe_create_leading_directories_const(output_directory
)) {
2303 die(_("could not create leading directories "
2304 "of '%s'"), output_directory
);
2306 set_shared_repository(saved
);
2307 if (mkdir(output_directory
, 0777) < 0 && errno
!= EEXIST
)
2308 die_errno(_("could not create directory '%s'"),
2312 if (rev
.pending
.nr
== 1) {
2315 if (rev
.max_count
< 0 && !rev
.show_root_diff
) {
2317 * This is traditional behaviour of "git format-patch
2318 * origin" that prepares what the origin side still
2321 rev
.pending
.objects
[0].item
->flags
|= UNINTERESTING
;
2322 add_head_to_pending(&rev
);
2326 * Otherwise, it is "format-patch -22 HEAD", and/or
2327 * "format-patch --root HEAD". The user wants
2328 * get_revision() to do the usual traversal.
2331 if (!strcmp(rev
.pending
.objects
[0].name
, "HEAD"))
2335 const char *ref
, *v
;
2336 ref
= refs_resolve_ref_unsafe(get_main_ref_store(the_repository
),
2338 RESOLVE_REF_READING
,
2340 if (ref
&& skip_prefix(ref
, "refs/heads/", &v
))
2341 branch_name
= xstrdup(v
);
2343 branch_name
= xstrdup(""); /* no branch */
2348 * We cannot move this anywhere earlier because we do want to
2349 * know if --root was given explicitly from the command line.
2351 rev
.show_root_diff
= 1;
2353 if (ignore_if_in_upstream
) {
2354 /* Don't say anything if head and upstream are the same. */
2355 if (rev
.pending
.nr
== 2) {
2356 struct object_array_entry
*o
= rev
.pending
.objects
;
2357 if (oideq(&o
[0].item
->oid
, &o
[1].item
->oid
))
2360 get_patch_ids(&rev
, &ids
);
2363 if (prepare_revision_walk(&rev
))
2364 die(_("revision walk setup failed"));
2366 while ((commit
= get_revision(&rev
)) != NULL
) {
2367 if (commit
->object
.flags
& BOUNDARY
) {
2369 origin
= (boundary_count
== 1) ? commit
: NULL
;
2373 if (ignore_if_in_upstream
&& has_commit_patch_id(commit
, &ids
))
2377 REALLOC_ARRAY(list
, nr
);
2378 list
[nr
- 1] = commit
;
2384 if (cover_letter
== -1) {
2385 if (cfg
.config_cover_letter
== COVER_AUTO
)
2386 cover_letter
= (total
> 1);
2387 else if ((idiff_prev
.nr
|| rdiff_prev
) && (total
> 1))
2388 cover_letter
= (cfg
.config_cover_letter
!= COVER_OFF
);
2390 cover_letter
= (cfg
.config_cover_letter
== COVER_ON
);
2392 if (!cfg
.keep_subject
&& cfg
.auto_number
&& (total
> 1 || cover_letter
))
2395 rev
.total
= total
+ start_number
- 1;
2397 if (idiff_prev
.nr
) {
2398 if (!cover_letter
&& total
!= 1)
2399 die(_("--interdiff requires --cover-letter or single patch"));
2400 rev
.idiff_oid1
= &idiff_prev
.oid
[idiff_prev
.nr
- 1];
2401 rev
.idiff_oid2
= get_commit_tree_oid(list
[0]);
2402 rev
.idiff_title
= diff_title(&idiff_title
, reroll_count
,
2404 _("Interdiff against v%d:"));
2407 if (creation_factor
< 0)
2408 creation_factor
= CREATION_FACTOR_FOR_THE_SAME_SERIES
;
2409 else if (!rdiff_prev
)
2410 die(_("the option '%s' requires '%s'"), "--creation-factor", "--range-diff");
2413 if (!cover_letter
&& total
!= 1)
2414 die(_("--range-diff requires --cover-letter or single patch"));
2416 infer_range_diff_ranges(&rdiff1
, &rdiff2
, rdiff_prev
,
2418 rev
.rdiff1
= rdiff1
.buf
;
2419 rev
.rdiff2
= rdiff2
.buf
;
2420 rev
.creation_factor
= creation_factor
;
2421 rev
.rdiff_title
= diff_title(&rdiff_title
, reroll_count
,
2423 _("Range-diff against v%d:"));
2427 * The order of precedence is:
2429 * 1. The `--signature` and `--no-signature` options.
2430 * 2. The `--signature-file` option.
2431 * 3. The `format.signature` config.
2432 * 4. The `format.signatureFile` config.
2433 * 5. Default `git_version_string`.
2436 ; /* --no-signature inhibits all signatures */
2437 } else if (signature
&& signature
!= git_version_string
) {
2438 ; /* non-default signature already set */
2439 } else if (signature_file_arg
|| (cfg
.signature_file
&& !cfg
.signature
)) {
2440 struct strbuf buf
= STRBUF_INIT
;
2441 const char *signature_file
= signature_file_arg
?
2442 signature_file_arg
: cfg
.signature_file
;
2444 if (strbuf_read_file(&buf
, signature_file
, 128) < 0)
2445 die_errno(_("unable to read signature file '%s'"), signature_file
);
2446 signature
= strbuf_detach(&buf
, NULL
);
2447 } else if (cfg
.signature
) {
2448 signature
= cfg
.signature
;
2451 memset(&bases
, 0, sizeof(bases
));
2452 base
= get_base_commit(&cfg
, list
, nr
);
2454 reset_revision_walk();
2455 clear_object_flags(UNINTERESTING
);
2456 prepare_bases(&bases
, base
, list
, nr
);
2459 if (in_reply_to
|| cfg
.thread
|| cover_letter
) {
2460 rev
.ref_message_ids
= xmalloc(sizeof(*rev
.ref_message_ids
));
2461 string_list_init_dup(rev
.ref_message_ids
);
2464 char *msgid
= clean_message_id(in_reply_to
);
2465 string_list_append_nodup(rev
.ref_message_ids
, msgid
);
2467 rev
.numbered_files
= just_numbers
;
2468 rev
.patch_suffix
= fmt_patch_suffix
;
2471 gen_message_id(&rev
, "cover");
2472 make_cover_letter(&rev
, !!output_directory
,
2473 origin
, nr
, list
, description_file
, branch_name
, quiet
, &cfg
);
2474 print_bases(&bases
, rev
.diffopt
.file
);
2475 print_signature(signature
, rev
.diffopt
.file
);
2478 /* interdiff/range-diff in cover-letter; omit from patches */
2479 rev
.idiff_oid1
= NULL
;
2482 rev
.add_signoff
= cfg
.do_signoff
;
2485 progress
= start_delayed_progress(_("Generating patches"), total
);
2488 display_progress(progress
, total
- nr
);
2490 rev
.nr
= total
- nr
+ (start_number
- 1);
2491 /* Make the second and subsequent mails replies to the first */
2493 /* Have we already had a message ID? */
2494 if (rev
.message_id
) {
2496 * For deep threading: make every mail
2497 * a reply to the previous one, no
2498 * matter what other options are set.
2500 * For shallow threading:
2502 * Without --cover-letter and
2503 * --in-reply-to, make every mail a
2504 * reply to the one before.
2506 * With --in-reply-to but no
2507 * --cover-letter, make every mail a
2508 * reply to the <reply-to>.
2510 * With --cover-letter, make every
2511 * mail but the cover letter a reply
2512 * to the cover letter. The cover
2513 * letter is a reply to the
2514 * --in-reply-to, if specified.
2516 if (cfg
.thread
== THREAD_SHALLOW
2517 && rev
.ref_message_ids
->nr
> 0
2518 && (!cover_letter
|| rev
.nr
> 1))
2519 free(rev
.message_id
);
2521 string_list_append_nodup(rev
.ref_message_ids
,
2524 gen_message_id(&rev
, oid_to_hex(&commit
->object
.oid
));
2527 if (output_directory
&&
2528 open_next_file(rev
.numbered_files
? NULL
: commit
, NULL
, &rev
, quiet
))
2529 die(_("failed to create output files"));
2530 shown
= log_tree_commit(&rev
, commit
);
2531 free_commit_buffer(the_repository
->parsed_objects
,
2534 /* We put one extra blank line between formatted
2535 * patches and this flag is used by log-tree code
2536 * to see if it needs to emit a LF before showing
2537 * the log; when using one file per patch, we do
2538 * not want the extra blank line.
2540 if (output_directory
)
2543 print_bases(&bases
, rev
.diffopt
.file
);
2544 if (rev
.mime_boundary
)
2545 fprintf(rev
.diffopt
.file
, "\n--%s%s--\n\n\n",
2546 mime_boundary_leader
,
2549 print_signature(signature
, rev
.diffopt
.file
);
2551 if (output_directory
)
2552 fclose(rev
.diffopt
.file
);
2554 stop_progress(&progress
);
2557 if (ignore_if_in_upstream
)
2558 free_patch_ids(&ids
);
2561 oid_array_clear(&idiff_prev
);
2562 strbuf_release(&idiff_title
);
2563 strbuf_release(&rdiff1
);
2564 strbuf_release(&rdiff2
);
2565 strbuf_release(&rdiff_title
);
2566 free(description_file
);
2567 free(signature_file_arg
);
2569 free(rev
.message_id
);
2570 if (rev
.ref_message_ids
)
2571 string_list_clear(rev
.ref_message_ids
, 0);
2572 free(rev
.ref_message_ids
);
2573 release_revisions(&rev
);
2574 format_config_release(&cfg
);
2578 static int add_pending_commit(const char *arg
, struct rev_info
*revs
, int flags
)
2580 struct object_id oid
;
2581 if (repo_get_oid(the_repository
, arg
, &oid
) == 0) {
2582 struct commit
*commit
= lookup_commit_reference(the_repository
,
2585 commit
->object
.flags
|= flags
;
2586 add_pending_object(revs
, &commit
->object
, arg
);
2593 static const char * const cherry_usage
[] = {
2594 N_("git cherry [-v] [<upstream> [<head> [<limit>]]]"),
2598 static void print_commit(char sign
, struct commit
*commit
, int verbose
,
2599 int abbrev
, FILE *file
)
2602 fprintf(file
, "%c %s\n", sign
,
2603 repo_find_unique_abbrev(the_repository
, &commit
->object
.oid
, abbrev
));
2605 struct strbuf buf
= STRBUF_INIT
;
2606 pp_commit_easy(CMIT_FMT_ONELINE
, commit
, &buf
);
2607 fprintf(file
, "%c %s %s\n", sign
,
2608 repo_find_unique_abbrev(the_repository
, &commit
->object
.oid
, abbrev
),
2610 strbuf_release(&buf
);
2614 int cmd_cherry(int argc
, const char **argv
, const char *prefix
)
2616 struct rev_info revs
;
2617 struct patch_ids ids
;
2618 struct commit
*commit
;
2619 struct commit_list
*list
= NULL
;
2620 struct branch
*current_branch
;
2621 const char *upstream
;
2622 const char *head
= "HEAD";
2623 const char *limit
= NULL
;
2624 int verbose
= 0, abbrev
= 0;
2626 struct option options
[] = {
2627 OPT__ABBREV(&abbrev
),
2628 OPT__VERBOSE(&verbose
, N_("be verbose")),
2632 argc
= parse_options(argc
, argv
, prefix
, options
, cherry_usage
, 0);
2645 current_branch
= branch_get(NULL
);
2646 upstream
= branch_get_upstream(current_branch
, NULL
);
2648 fprintf(stderr
, _("Could not find a tracked"
2649 " remote branch, please"
2650 " specify <upstream> manually.\n"));
2651 usage_with_options(cherry_usage
, options
);
2655 repo_init_revisions(the_repository
, &revs
, prefix
);
2656 revs
.max_parents
= 1;
2658 if (add_pending_commit(head
, &revs
, 0))
2659 die(_("unknown commit %s"), head
);
2660 if (add_pending_commit(upstream
, &revs
, UNINTERESTING
))
2661 die(_("unknown commit %s"), upstream
);
2663 /* Don't say anything if head and upstream are the same. */
2664 if (revs
.pending
.nr
== 2) {
2665 struct object_array_entry
*o
= revs
.pending
.objects
;
2666 if (oideq(&o
[0].item
->oid
, &o
[1].item
->oid
))
2670 get_patch_ids(&revs
, &ids
);
2672 if (limit
&& add_pending_commit(limit
, &revs
, UNINTERESTING
))
2673 die(_("unknown commit %s"), limit
);
2675 /* reverse the list of commits */
2676 if (prepare_revision_walk(&revs
))
2677 die(_("revision walk setup failed"));
2678 while ((commit
= get_revision(&revs
)) != NULL
) {
2679 commit_list_insert(commit
, &list
);
2682 for (struct commit_list
*l
= list
; l
; l
= l
->next
) {
2686 if (has_commit_patch_id(commit
, &ids
))
2688 print_commit(sign
, commit
, verbose
, abbrev
, revs
.diffopt
.file
);
2691 free_commit_list(list
);
2692 free_patch_ids(&ids
);