2 * Builtin "git log" and related commands (show, whatchanged)
4 * (C) Copyright 2006 Linus Torvalds
7 #include "git-compat-util.h"
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"
29 #include "run-command.h"
32 #include "string-list.h"
33 #include "parse-options.h"
36 #include "streaming.h"
39 #include "gpg-interface.h"
41 #include "commit-slab.h"
42 #include "repository.h"
43 #include "commit-reach.h"
44 #include "range-diff.h"
45 #include "tmp-objdir.h"
47 #include "write-or-die.h"
49 #define MAIL_DEFAULT_WRAP 72
50 #define COVER_FROM_AUTO_MAX_SUBJECT_LEN 100
51 #define FORMAT_PATCH_NAME_MAX_DEFAULT 64
53 /* Set a default date-time format for git log ("log.date" config variable) */
54 static const char *default_date_mode
= NULL
;
56 static int default_abbrev_commit
;
57 static int default_show_root
= 1;
58 static int default_follow
;
59 static int default_show_signature
;
60 static int default_encode_email_headers
= 1;
61 static int decoration_style
;
62 static int decoration_given
;
63 static int use_mailmap_config
= 1;
64 static unsigned int force_in_body_from
;
65 static int stdout_mboxrd
;
66 static const char *fmt_patch_subject_prefix
= "PATCH";
67 static int fmt_patch_name_max
= FORMAT_PATCH_NAME_MAX_DEFAULT
;
68 static const char *fmt_pretty
;
69 static int format_no_prefix
;
71 static const char * const builtin_log_usage
[] = {
72 N_("git log [<options>] [<revision-range>] [[--] <path>...]"),
73 N_("git show [<options>] <object>..."),
77 struct line_opt_callback_data
{
80 struct string_list args
;
83 static int session_is_interactive(void)
85 return isatty(1) || pager_in_use();
88 static int auto_decoration_style(void)
90 return session_is_interactive() ? DECORATE_SHORT_REFS
: 0;
93 static int parse_decoration_style(const char *value
)
95 switch (git_parse_maybe_bool(value
)) {
97 return DECORATE_SHORT_REFS
;
103 if (!strcmp(value
, "full"))
104 return DECORATE_FULL_REFS
;
105 else if (!strcmp(value
, "short"))
106 return DECORATE_SHORT_REFS
;
107 else if (!strcmp(value
, "auto"))
108 return auto_decoration_style();
110 * Please update _git_log() in git-completion.bash when you
111 * add new decoration styles.
116 static int use_default_decoration_filter
= 1;
117 static struct string_list decorate_refs_exclude
= STRING_LIST_INIT_NODUP
;
118 static struct string_list decorate_refs_exclude_config
= STRING_LIST_INIT_NODUP
;
119 static struct string_list decorate_refs_include
= STRING_LIST_INIT_NODUP
;
121 static int clear_decorations_callback(const struct option
*opt UNUSED
,
122 const char *arg
, int unset
)
124 BUG_ON_OPT_NEG(unset
);
126 string_list_clear(&decorate_refs_include
, 0);
127 string_list_clear(&decorate_refs_exclude
, 0);
128 use_default_decoration_filter
= 0;
132 static int decorate_callback(const struct option
*opt UNUSED
, const char *arg
,
136 decoration_style
= 0;
138 decoration_style
= parse_decoration_style(arg
);
140 decoration_style
= DECORATE_SHORT_REFS
;
142 if (decoration_style
< 0)
143 die(_("invalid --decorate option: %s"), arg
);
145 decoration_given
= 1;
150 static int log_line_range_callback(const struct option
*option
, const char *arg
, int unset
)
152 struct line_opt_callback_data
*data
= option
->value
;
154 BUG_ON_OPT_NEG(unset
);
159 data
->rev
->line_level_traverse
= 1;
160 string_list_append(&data
->args
, arg
);
165 static void init_log_defaults(void)
167 init_diff_ui_defaults();
169 decoration_style
= auto_decoration_style();
172 static void cmd_log_init_defaults(struct rev_info
*rev
)
175 get_commit_format(fmt_pretty
, rev
);
177 rev
->diffopt
.flags
.default_follow_renames
= 1;
178 rev
->verbose_header
= 1;
179 init_diffstat_widths(&rev
->diffopt
);
180 rev
->diffopt
.flags
.recursive
= 1;
181 rev
->diffopt
.flags
.allow_textconv
= 1;
182 rev
->abbrev_commit
= default_abbrev_commit
;
183 rev
->show_root_diff
= default_show_root
;
184 rev
->subject_prefix
= fmt_patch_subject_prefix
;
185 rev
->patch_name_max
= fmt_patch_name_max
;
186 rev
->show_signature
= default_show_signature
;
187 rev
->encode_email_headers
= default_encode_email_headers
;
189 if (default_date_mode
)
190 parse_date_format(default_date_mode
, &rev
->date_mode
);
193 static void set_default_decoration_filter(struct decoration_filter
*decoration_filter
)
197 struct string_list
*include
= decoration_filter
->include_ref_pattern
;
198 const struct string_list
*config_exclude
;
200 if (!git_config_get_string_multi("log.excludeDecoration",
202 struct string_list_item
*item
;
203 for_each_string_list_item(item
, config_exclude
)
204 string_list_append(decoration_filter
->exclude_ref_config_pattern
,
209 * By default, decorate_all is disabled. Enable it if
210 * log.initialDecorationSet=all. Don't ever disable it by config,
211 * since the command-line takes precedent.
213 if (use_default_decoration_filter
&&
214 !git_config_get_string("log.initialdecorationset", &value
) &&
215 !strcmp("all", value
))
216 use_default_decoration_filter
= 0;
219 if (!use_default_decoration_filter
||
220 decoration_filter
->exclude_ref_pattern
->nr
||
221 decoration_filter
->include_ref_pattern
->nr
||
222 decoration_filter
->exclude_ref_config_pattern
->nr
)
226 * No command-line or config options were given, so
227 * populate with sensible defaults.
229 for (i
= 0; i
< ARRAY_SIZE(ref_namespace
); i
++) {
230 if (!ref_namespace
[i
].decoration
)
233 string_list_append(include
, ref_namespace
[i
].ref
);
237 static void cmd_log_init_finish(int argc
, const char **argv
, const char *prefix
,
238 struct rev_info
*rev
, struct setup_revision_opt
*opt
)
240 struct userformat_want w
;
241 int quiet
= 0, source
= 0, mailmap
;
242 static struct line_opt_callback_data line_cb
= {NULL
, NULL
, STRING_LIST_INIT_DUP
};
243 struct decoration_filter decoration_filter
= {
244 .exclude_ref_pattern
= &decorate_refs_exclude
,
245 .include_ref_pattern
= &decorate_refs_include
,
246 .exclude_ref_config_pattern
= &decorate_refs_exclude_config
,
248 static struct revision_sources revision_sources
;
250 const struct option builtin_log_options
[] = {
251 OPT__QUIET(&quiet
, N_("suppress diff output")),
252 OPT_BOOL(0, "source", &source
, N_("show source")),
253 OPT_BOOL(0, "use-mailmap", &mailmap
, N_("use mail map file")),
254 OPT_ALIAS(0, "mailmap", "use-mailmap"),
255 OPT_CALLBACK_F(0, "clear-decorations", NULL
, NULL
,
256 N_("clear all previously-defined decoration filters"),
257 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
,
258 clear_decorations_callback
),
259 OPT_STRING_LIST(0, "decorate-refs", &decorate_refs_include
,
260 N_("pattern"), N_("only decorate refs that match <pattern>")),
261 OPT_STRING_LIST(0, "decorate-refs-exclude", &decorate_refs_exclude
,
262 N_("pattern"), N_("do not decorate refs that match <pattern>")),
263 OPT_CALLBACK_F(0, "decorate", NULL
, NULL
, N_("decorate options"),
264 PARSE_OPT_OPTARG
, decorate_callback
),
265 OPT_CALLBACK('L', NULL
, &line_cb
, "range:file",
266 N_("trace the evolution of line range <start>,<end> or function :<funcname> in <file>"),
267 log_line_range_callback
),
272 line_cb
.prefix
= prefix
;
274 mailmap
= use_mailmap_config
;
275 argc
= parse_options(argc
, argv
, prefix
,
276 builtin_log_options
, builtin_log_usage
,
277 PARSE_OPT_KEEP_ARGV0
| PARSE_OPT_KEEP_UNKNOWN_OPT
|
278 PARSE_OPT_KEEP_DASHDASH
);
281 rev
->diffopt
.output_format
|= DIFF_FORMAT_NO_OUTPUT
;
282 argc
= setup_revisions(argc
, argv
, rev
, opt
);
284 /* Any arguments at this point are not recognized */
286 die(_("unrecognized argument: %s"), argv
[1]);
288 if (rev
->line_level_traverse
&& rev
->prune_data
.nr
)
289 die(_("-L<range>:<file> cannot be used with pathspec"));
291 memset(&w
, 0, sizeof(w
));
292 userformat_find_requirements(NULL
, &w
);
294 if (!rev
->show_notes_given
&& (!rev
->pretty_given
|| w
.notes
))
297 load_display_notes(&rev
->notes_opt
);
299 if ((rev
->diffopt
.pickaxe_opts
& DIFF_PICKAXE_KINDS_MASK
) ||
300 rev
->diffopt
.filter
|| rev
->diffopt
.flags
.follow_renames
)
301 rev
->always_show_header
= 0;
303 if (source
|| w
.source
) {
304 init_revision_sources(&revision_sources
);
305 rev
->sources
= &revision_sources
;
309 rev
->mailmap
= xmalloc(sizeof(struct string_list
));
310 string_list_init_nodup(rev
->mailmap
);
311 read_mailmap(rev
->mailmap
);
314 if (rev
->pretty_given
&& rev
->commit_format
== CMIT_FMT_RAW
) {
316 * "log --pretty=raw" is special; ignore UI oriented
317 * configuration variables such as decoration.
319 if (!decoration_given
)
320 decoration_style
= 0;
321 if (!rev
->abbrev_commit_given
)
322 rev
->abbrev_commit
= 0;
325 if (rev
->commit_format
== CMIT_FMT_USERFORMAT
) {
328 * Disable decoration loading if the format will not
331 decoration_style
= 0;
332 } else if (!decoration_style
) {
334 * If we are going to show them, make sure we do load
335 * them here, but taking care not to override a
336 * specific style set by config or --decorate.
338 decoration_style
= DECORATE_SHORT_REFS
;
342 if (decoration_style
|| rev
->simplify_by_decoration
) {
343 set_default_decoration_filter(&decoration_filter
);
345 if (decoration_style
)
346 rev
->show_decorations
= 1;
348 load_ref_decorations(&decoration_filter
, decoration_style
);
351 if (rev
->line_level_traverse
)
352 line_log_init(rev
, line_cb
.prefix
, &line_cb
.args
);
357 static void cmd_log_init(int argc
, const char **argv
, const char *prefix
,
358 struct rev_info
*rev
, struct setup_revision_opt
*opt
)
360 cmd_log_init_defaults(rev
);
361 cmd_log_init_finish(argc
, argv
, prefix
, rev
, opt
);
364 static int cmd_log_deinit(int ret
, struct rev_info
*rev
)
366 release_revisions(rev
);
371 * This gives a rough estimate for how many commits we
372 * will print out in the list.
374 static int estimate_commit_count(struct commit_list
*list
)
379 struct commit
*commit
= list
->item
;
380 unsigned int flags
= commit
->object
.flags
;
382 if (!(flags
& (TREESAME
| UNINTERESTING
)))
388 static void show_early_header(struct rev_info
*rev
, const char *stage
, int nr
)
390 if (rev
->shown_one
) {
392 if (rev
->commit_format
!= CMIT_FMT_ONELINE
)
393 putchar(rev
->diffopt
.line_termination
);
395 fprintf(rev
->diffopt
.file
, _("Final output: %d %s\n"), nr
, stage
);
398 static struct itimerval early_output_timer
;
400 static void log_show_early(struct rev_info
*revs
, struct commit_list
*list
)
402 int i
= revs
->early_output
;
404 int no_free
= revs
->diffopt
.no_free
;
406 revs
->diffopt
.no_free
= 0;
407 sort_in_topological_order(&list
, revs
->sort_order
);
409 struct commit
*commit
= list
->item
;
410 switch (simplify_commit(revs
, commit
)) {
413 int n
= estimate_commit_count(list
);
414 show_early_header(revs
, "incomplete", n
);
417 log_tree_commit(revs
, commit
);
423 revs
->diffopt
.no_free
= no_free
;
424 diff_free(&revs
->diffopt
);
430 /* Did we already get enough commits for the early output? */
432 revs
->diffopt
.no_free
= 0;
433 diff_free(&revs
->diffopt
);
438 * ..if no, then repeat it twice a second until we
441 * NOTE! We don't use "it_interval", because if the
442 * reader isn't listening, we want our output to be
443 * throttled by the writing, and not have the timer
444 * trigger every second even if we're blocked on a
447 early_output_timer
.it_value
.tv_sec
= 0;
448 early_output_timer
.it_value
.tv_usec
= 500000;
449 setitimer(ITIMER_REAL
, &early_output_timer
, NULL
);
452 static void early_output(int signal UNUSED
)
454 show_early_output
= log_show_early
;
457 static void setup_early_output(void)
462 * Set up the signal handler, minimally intrusively:
463 * we only set a single volatile integer word (not
464 * using sigatomic_t - trying to avoid unnecessary
465 * system dependencies and headers), and using
468 memset(&sa
, 0, sizeof(sa
));
469 sa
.sa_handler
= early_output
;
470 sigemptyset(&sa
.sa_mask
);
471 sa
.sa_flags
= SA_RESTART
;
472 sigaction(SIGALRM
, &sa
, NULL
);
475 * If we can get the whole output in less than a
476 * tenth of a second, don't even bother doing the
477 * early-output thing..
479 * This is a one-time-only trigger.
481 early_output_timer
.it_value
.tv_sec
= 0;
482 early_output_timer
.it_value
.tv_usec
= 100000;
483 setitimer(ITIMER_REAL
, &early_output_timer
, NULL
);
486 static void finish_early_output(struct rev_info
*rev
)
488 int n
= estimate_commit_count(rev
->commits
);
489 signal(SIGALRM
, SIG_IGN
);
490 show_early_header(rev
, "done", n
);
493 static int cmd_log_walk_no_free(struct rev_info
*rev
)
495 struct commit
*commit
;
499 if (rev
->remerge_diff
) {
500 rev
->remerge_objdir
= tmp_objdir_create("remerge-diff");
501 if (!rev
->remerge_objdir
)
502 die(_("unable to create temporary object directory"));
503 tmp_objdir_replace_primary_odb(rev
->remerge_objdir
, 1);
506 if (rev
->early_output
)
507 setup_early_output();
509 if (prepare_revision_walk(rev
))
510 die(_("revision walk setup failed"));
512 if (rev
->early_output
)
513 finish_early_output(rev
);
516 * For --check and --exit-code, the exit code is based on CHECK_FAILED
517 * and HAS_CHANGES being accumulated in rev->diffopt, so be careful to
518 * retain that state information if replacing rev->diffopt in this loop
520 while ((commit
= get_revision(rev
)) != NULL
) {
521 if (!log_tree_commit(rev
, commit
) && rev
->max_count
>= 0)
523 * We decremented max_count in get_revision,
524 * but we didn't actually show the commit.
527 if (!rev
->reflog_info
) {
529 * We may show a given commit multiple times when
530 * walking the reflogs.
532 free_commit_buffer(the_repository
->parsed_objects
,
534 free_commit_list(commit
->parents
);
535 commit
->parents
= NULL
;
537 if (saved_nrl
< rev
->diffopt
.needed_rename_limit
)
538 saved_nrl
= rev
->diffopt
.needed_rename_limit
;
539 if (rev
->diffopt
.degraded_cc_to_c
)
542 rev
->diffopt
.degraded_cc_to_c
= saved_dcctc
;
543 rev
->diffopt
.needed_rename_limit
= saved_nrl
;
545 if (rev
->remerge_diff
) {
546 tmp_objdir_destroy(rev
->remerge_objdir
);
547 rev
->remerge_objdir
= NULL
;
550 if (rev
->diffopt
.output_format
& DIFF_FORMAT_CHECKDIFF
&&
551 rev
->diffopt
.flags
.check_failed
) {
554 return diff_result_code(&rev
->diffopt
);
557 static int cmd_log_walk(struct rev_info
*rev
)
561 rev
->diffopt
.no_free
= 1;
562 retval
= cmd_log_walk_no_free(rev
);
563 rev
->diffopt
.no_free
= 0;
564 diff_free(&rev
->diffopt
);
568 static int git_log_config(const char *var
, const char *value
,
569 const struct config_context
*ctx
, void *cb
)
571 const char *slot_name
;
573 if (!strcmp(var
, "format.pretty"))
574 return git_config_string(&fmt_pretty
, var
, value
);
575 if (!strcmp(var
, "format.subjectprefix"))
576 return git_config_string(&fmt_patch_subject_prefix
, var
, value
);
577 if (!strcmp(var
, "format.filenamemaxlength")) {
578 fmt_patch_name_max
= git_config_int(var
, value
, ctx
->kvi
);
581 if (!strcmp(var
, "format.encodeemailheaders")) {
582 default_encode_email_headers
= git_config_bool(var
, value
);
585 if (!strcmp(var
, "log.abbrevcommit")) {
586 default_abbrev_commit
= git_config_bool(var
, value
);
589 if (!strcmp(var
, "log.date"))
590 return git_config_string(&default_date_mode
, var
, value
);
591 if (!strcmp(var
, "log.decorate")) {
592 decoration_style
= parse_decoration_style(value
);
593 if (decoration_style
< 0)
594 decoration_style
= 0; /* maybe warn? */
597 if (!strcmp(var
, "log.diffmerges"))
598 return diff_merges_config(value
);
599 if (!strcmp(var
, "log.showroot")) {
600 default_show_root
= git_config_bool(var
, value
);
603 if (!strcmp(var
, "log.follow")) {
604 default_follow
= git_config_bool(var
, value
);
607 if (skip_prefix(var
, "color.decorate.", &slot_name
))
608 return parse_decorate_color_config(var
, slot_name
, value
);
609 if (!strcmp(var
, "log.mailmap")) {
610 use_mailmap_config
= git_config_bool(var
, value
);
613 if (!strcmp(var
, "log.showsignature")) {
614 default_show_signature
= git_config_bool(var
, value
);
618 return git_diff_ui_config(var
, value
, ctx
, cb
);
621 int cmd_whatchanged(int argc
, const char **argv
, const char *prefix
)
624 struct setup_revision_opt opt
;
627 git_config(git_log_config
, NULL
);
629 repo_init_revisions(the_repository
, &rev
, prefix
);
630 git_config(grep_config
, &rev
.grep_filter
);
633 rev
.simplify_history
= 0;
634 memset(&opt
, 0, sizeof(opt
));
636 opt
.revarg_opt
= REVARG_COMMITTISH
;
637 cmd_log_init(argc
, argv
, prefix
, &rev
, &opt
);
638 if (!rev
.diffopt
.output_format
)
639 rev
.diffopt
.output_format
= DIFF_FORMAT_RAW
;
640 return cmd_log_deinit(cmd_log_walk(&rev
), &rev
);
643 static void show_tagger(const char *buf
, struct rev_info
*rev
)
645 struct strbuf out
= STRBUF_INIT
;
646 struct pretty_print_context pp
= {0};
648 pp
.fmt
= rev
->commit_format
;
649 pp
.date_mode
= rev
->date_mode
;
650 pp_user_info(&pp
, "Tagger", &out
, buf
, get_log_output_encoding());
651 fprintf(rev
->diffopt
.file
, "%s", out
.buf
);
652 strbuf_release(&out
);
655 static int show_blob_object(const struct object_id
*oid
, struct rev_info
*rev
, const char *obj_name
)
657 struct object_id oidc
;
658 struct object_context obj_context
;
662 fflush(rev
->diffopt
.file
);
663 if (!rev
->diffopt
.flags
.textconv_set_via_cmdline
||
664 !rev
->diffopt
.flags
.allow_textconv
)
665 return stream_blob_to_fd(1, oid
, NULL
, 0);
667 if (get_oid_with_context(the_repository
, obj_name
,
669 &oidc
, &obj_context
))
670 die(_("not a valid object name %s"), obj_name
);
671 if (!obj_context
.path
||
672 !textconv_object(the_repository
, obj_context
.path
,
673 obj_context
.mode
, &oidc
, 1, &buf
, &size
)) {
674 free(obj_context
.path
);
675 return stream_blob_to_fd(1, oid
, NULL
, 0);
679 die(_("git show %s: bad file"), obj_name
);
681 write_or_die(1, buf
, size
);
682 free(obj_context
.path
);
686 static int show_tag_object(const struct object_id
*oid
, struct rev_info
*rev
)
689 enum object_type type
;
690 char *buf
= repo_read_object_file(the_repository
, oid
, &type
, &size
);
694 return error(_("could not read object %s"), oid_to_hex(oid
));
696 assert(type
== OBJ_TAG
);
697 while (offset
< size
&& buf
[offset
] != '\n') {
698 int new_offset
= offset
+ 1;
700 while (new_offset
< size
&& buf
[new_offset
++] != '\n')
702 if (skip_prefix(buf
+ offset
, "tagger ", &ident
))
703 show_tagger(ident
, rev
);
708 fwrite(buf
+ offset
, size
- offset
, 1, rev
->diffopt
.file
);
713 static int show_tree_object(const struct object_id
*oid UNUSED
,
714 struct strbuf
*base UNUSED
,
715 const char *pathname
, unsigned mode
,
718 FILE *file
= context
;
719 fprintf(file
, "%s%s\n", pathname
, S_ISDIR(mode
) ? "/" : "");
723 static void show_setup_revisions_tweak(struct rev_info
*rev
)
725 if (rev
->first_parent_only
)
726 diff_merges_default_to_first_parent(rev
);
728 diff_merges_default_to_dense_combined(rev
);
729 if (!rev
->diffopt
.output_format
)
730 rev
->diffopt
.output_format
= DIFF_FORMAT_PATCH
;
733 int cmd_show(int argc
, const char **argv
, const char *prefix
)
737 struct setup_revision_opt opt
;
738 struct pathspec match_all
;
742 git_config(git_log_config
, NULL
);
744 if (the_repository
->gitdir
) {
745 prepare_repo_settings(the_repository
);
746 the_repository
->settings
.command_requires_full_index
= 0;
749 memset(&match_all
, 0, sizeof(match_all
));
750 repo_init_revisions(the_repository
, &rev
, prefix
);
751 git_config(grep_config
, &rev
.grep_filter
);
754 rev
.always_show_header
= 1;
756 rev
.diffopt
.stat_width
= -1; /* Scale to real terminal size */
758 memset(&opt
, 0, sizeof(opt
));
760 opt
.tweak
= show_setup_revisions_tweak
;
761 cmd_log_init(argc
, argv
, prefix
, &rev
, &opt
);
764 return cmd_log_deinit(cmd_log_walk(&rev
), &rev
);
766 rev
.diffopt
.no_free
= 1;
767 for (i
= 0; i
< rev
.pending
.nr
&& !ret
; i
++) {
768 struct object
*o
= rev
.pending
.objects
[i
].item
;
769 const char *name
= rev
.pending
.objects
[i
].name
;
772 ret
= show_blob_object(&o
->oid
, &rev
, name
);
775 struct tag
*t
= (struct tag
*)o
;
776 struct object_id
*oid
= get_tagged_oid(t
);
780 fprintf(rev
.diffopt
.file
, "%stag %s%s\n",
781 diff_get_color_opt(&rev
.diffopt
, DIFF_COMMIT
),
783 diff_get_color_opt(&rev
.diffopt
, DIFF_RESET
));
784 ret
= show_tag_object(&o
->oid
, &rev
);
788 o
= parse_object(the_repository
, oid
);
790 ret
= error(_("could not read object %s"),
792 rev
.pending
.objects
[i
].item
= o
;
799 fprintf(rev
.diffopt
.file
, "%stree %s%s\n\n",
800 diff_get_color_opt(&rev
.diffopt
, DIFF_COMMIT
),
802 diff_get_color_opt(&rev
.diffopt
, DIFF_RESET
));
803 read_tree(the_repository
, (struct tree
*)o
,
804 &match_all
, show_tree_object
,
810 struct object_array old
;
811 struct object_array blank
= OBJECT_ARRAY_INIT
;
813 memcpy(&old
, &rev
.pending
, sizeof(old
));
814 memcpy(&rev
.pending
, &blank
, sizeof(rev
.pending
));
816 add_object_array(o
, name
, &rev
.pending
);
817 ret
= cmd_log_walk_no_free(&rev
);
821 * object_array_clear(&pending). It was
822 * cleared already in prepare_revision_walk()
824 memcpy(&rev
.pending
, &old
, sizeof(rev
.pending
));
828 ret
= error(_("unknown type: %d"), o
->type
);
832 rev
.diffopt
.no_free
= 0;
833 diff_free(&rev
.diffopt
);
835 return cmd_log_deinit(ret
, &rev
);
839 * This is equivalent to "git log -g --abbrev-commit --pretty=oneline"
841 int cmd_log_reflog(int argc
, const char **argv
, const char *prefix
)
844 struct setup_revision_opt opt
;
847 git_config(git_log_config
, NULL
);
849 repo_init_revisions(the_repository
, &rev
, prefix
);
850 init_reflog_walk(&rev
.reflog_info
);
851 git_config(grep_config
, &rev
.grep_filter
);
853 rev
.verbose_header
= 1;
854 memset(&opt
, 0, sizeof(opt
));
856 cmd_log_init_defaults(&rev
);
857 rev
.abbrev_commit
= 1;
858 rev
.commit_format
= CMIT_FMT_ONELINE
;
859 rev
.use_terminator
= 1;
860 rev
.always_show_header
= 1;
861 cmd_log_init_finish(argc
, argv
, prefix
, &rev
, &opt
);
863 return cmd_log_deinit(cmd_log_walk(&rev
), &rev
);
866 static void log_setup_revisions_tweak(struct rev_info
*rev
)
868 if (rev
->diffopt
.flags
.default_follow_renames
&&
869 diff_check_follow_pathspec(&rev
->prune_data
, 0))
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
,
983 const struct config_context
*ctx
, void *cb
)
985 if (!strcmp(var
, "format.headers")) {
987 die(_("format.headers without value"));
991 if (!strcmp(var
, "format.suffix"))
992 return git_config_string(&fmt_patch_suffix
, var
, value
);
993 if (!strcmp(var
, "format.to")) {
995 return config_error_nonbool(var
);
996 string_list_append(&extra_to
, value
);
999 if (!strcmp(var
, "format.cc")) {
1001 return config_error_nonbool(var
);
1002 string_list_append(&extra_cc
, value
);
1005 if (!strcmp(var
, "diff.color") || !strcmp(var
, "color.diff") ||
1006 !strcmp(var
, "color.ui") || !strcmp(var
, "diff.submodule")) {
1009 if (!strcmp(var
, "format.numbered")) {
1010 if (value
&& !strcasecmp(value
, "auto")) {
1014 numbered
= git_config_bool(var
, value
);
1015 auto_number
= auto_number
&& numbered
;
1018 if (!strcmp(var
, "format.attach")) {
1019 if (value
&& *value
)
1020 default_attach
= xstrdup(value
);
1021 else if (value
&& !*value
)
1022 FREE_AND_NULL(default_attach
);
1024 default_attach
= xstrdup(git_version_string
);
1027 if (!strcmp(var
, "format.thread")) {
1028 if (value
&& !strcasecmp(value
, "deep")) {
1029 thread
= THREAD_DEEP
;
1032 if (value
&& !strcasecmp(value
, "shallow")) {
1033 thread
= THREAD_SHALLOW
;
1036 thread
= git_config_bool(var
, value
) ? THREAD_SHALLOW
: THREAD_UNSET
;
1039 if (!strcmp(var
, "format.signoff")) {
1040 do_signoff
= git_config_bool(var
, value
);
1043 if (!strcmp(var
, "format.signature"))
1044 return git_config_string(&signature
, var
, value
);
1045 if (!strcmp(var
, "format.signaturefile"))
1046 return git_config_pathname(&signature_file
, var
, value
);
1047 if (!strcmp(var
, "format.coverletter")) {
1048 if (value
&& !strcasecmp(value
, "auto")) {
1049 config_cover_letter
= COVER_AUTO
;
1052 config_cover_letter
= git_config_bool(var
, value
) ? COVER_ON
: COVER_OFF
;
1055 if (!strcmp(var
, "format.outputdirectory"))
1056 return git_config_string(&config_output_directory
, var
, value
);
1057 if (!strcmp(var
, "format.useautobase")) {
1058 if (value
&& !strcasecmp(value
, "whenAble")) {
1059 auto_base
= AUTO_BASE_WHEN_ABLE
;
1062 auto_base
= git_config_bool(var
, value
) ? AUTO_BASE_ALWAYS
: AUTO_BASE_NEVER
;
1065 if (!strcmp(var
, "format.from")) {
1066 int b
= git_parse_maybe_bool(value
);
1069 from
= xstrdup(value
);
1071 from
= xstrdup(git_committer_info(IDENT_NO_DATE
));
1076 if (!strcmp(var
, "format.forceinbodyfrom")) {
1077 force_in_body_from
= git_config_bool(var
, value
);
1080 if (!strcmp(var
, "format.notes")) {
1081 int b
= git_parse_maybe_bool(value
);
1083 enable_ref_display_notes(¬es_opt
, &show_notes
, value
);
1085 enable_default_display_notes(¬es_opt
, &show_notes
);
1087 disable_display_notes(¬es_opt
, &show_notes
);
1090 if (!strcmp(var
, "format.coverfromdescription")) {
1091 cover_from_description_mode
= parse_cover_from_description(value
);
1094 if (!strcmp(var
, "format.mboxrd")) {
1095 stdout_mboxrd
= git_config_bool(var
, value
);
1098 if (!strcmp(var
, "format.noprefix")) {
1099 format_no_prefix
= 1;
1104 * ignore some porcelain config which would otherwise be parsed by
1105 * git_diff_ui_config(), via git_log_config(); we can't just avoid
1106 * diff_ui_config completely, because we do care about some ui options
1109 if (!strcmp(var
, "diff.noprefix"))
1112 return git_log_config(var
, value
, ctx
, cb
);
1115 static const char *output_directory
= NULL
;
1116 static int outdir_offset
;
1118 static int open_next_file(struct commit
*commit
, const char *subject
,
1119 struct rev_info
*rev
, int quiet
)
1121 struct strbuf filename
= STRBUF_INIT
;
1123 if (output_directory
) {
1124 strbuf_addstr(&filename
, output_directory
);
1125 strbuf_complete(&filename
, '/');
1128 if (rev
->numbered_files
)
1129 strbuf_addf(&filename
, "%d", rev
->nr
);
1131 fmt_output_commit(&filename
, commit
, rev
);
1133 fmt_output_subject(&filename
, subject
, rev
);
1136 printf("%s\n", filename
.buf
+ outdir_offset
);
1138 if (!(rev
->diffopt
.file
= fopen(filename
.buf
, "w"))) {
1139 error_errno(_("cannot open patch file %s"), filename
.buf
);
1140 strbuf_release(&filename
);
1144 strbuf_release(&filename
);
1148 static void get_patch_ids(struct rev_info
*rev
, struct patch_ids
*ids
)
1150 struct rev_info check_rev
;
1151 struct commit
*commit
, *c1
, *c2
;
1152 struct object
*o1
, *o2
;
1153 unsigned flags1
, flags2
;
1155 if (rev
->pending
.nr
!= 2)
1156 die(_("need exactly one range"));
1158 o1
= rev
->pending
.objects
[0].item
;
1159 o2
= rev
->pending
.objects
[1].item
;
1162 c1
= lookup_commit_reference(the_repository
, &o1
->oid
);
1163 c2
= lookup_commit_reference(the_repository
, &o2
->oid
);
1165 if ((flags1
& UNINTERESTING
) == (flags2
& UNINTERESTING
))
1166 die(_("not a range"));
1168 init_patch_ids(the_repository
, ids
);
1170 /* given a range a..b get all patch ids for b..a */
1171 repo_init_revisions(the_repository
, &check_rev
, rev
->prefix
);
1172 check_rev
.max_parents
= 1;
1173 o1
->flags
^= UNINTERESTING
;
1174 o2
->flags
^= UNINTERESTING
;
1175 add_pending_object(&check_rev
, o1
, "o1");
1176 add_pending_object(&check_rev
, o2
, "o2");
1177 if (prepare_revision_walk(&check_rev
))
1178 die(_("revision walk setup failed"));
1180 while ((commit
= get_revision(&check_rev
)) != NULL
) {
1181 add_commit_patch_id(commit
, ids
);
1184 /* reset for next revision walk */
1185 clear_commit_marks(c1
, SEEN
| UNINTERESTING
| SHOWN
| ADDED
);
1186 clear_commit_marks(c2
, SEEN
| UNINTERESTING
| SHOWN
| ADDED
);
1191 static void gen_message_id(struct rev_info
*info
, char *base
)
1193 struct strbuf buf
= STRBUF_INIT
;
1194 strbuf_addf(&buf
, "%s.%"PRItime
".git.%s", base
,
1195 (timestamp_t
) time(NULL
),
1196 git_committer_info(IDENT_NO_NAME
|IDENT_NO_DATE
|IDENT_STRICT
));
1197 info
->message_id
= strbuf_detach(&buf
, NULL
);
1200 static void print_signature(FILE *file
)
1202 if (!signature
|| !*signature
)
1205 fprintf(file
, "-- \n%s", signature
);
1206 if (signature
[strlen(signature
)-1] != '\n')
1211 static char *find_branch_name(struct rev_info
*rev
)
1213 int i
, positive
= -1;
1214 struct object_id branch_oid
;
1215 const struct object_id
*tip_oid
;
1216 const char *ref
, *v
;
1217 char *full_ref
, *branch
= NULL
;
1219 for (i
= 0; i
< rev
->cmdline
.nr
; i
++) {
1220 if (rev
->cmdline
.rev
[i
].flags
& UNINTERESTING
)
1229 ref
= rev
->cmdline
.rev
[positive
].name
;
1230 tip_oid
= &rev
->cmdline
.rev
[positive
].item
->oid
;
1231 if (repo_dwim_ref(the_repository
, ref
, strlen(ref
), &branch_oid
,
1233 skip_prefix(full_ref
, "refs/heads/", &v
) &&
1234 oideq(tip_oid
, &branch_oid
))
1235 branch
= xstrdup(v
);
1240 static void show_diffstat(struct rev_info
*rev
,
1241 struct commit
*origin
, struct commit
*head
)
1243 struct diff_options opts
;
1245 memcpy(&opts
, &rev
->diffopt
, sizeof(opts
));
1246 opts
.output_format
= DIFF_FORMAT_SUMMARY
| DIFF_FORMAT_DIFFSTAT
;
1247 diff_setup_done(&opts
);
1249 diff_tree_oid(get_commit_tree_oid(origin
),
1250 get_commit_tree_oid(head
),
1252 diffcore_std(&opts
);
1255 fprintf(rev
->diffopt
.file
, "\n");
1258 static void read_desc_file(struct strbuf
*buf
, const char *desc_file
)
1260 if (strbuf_read_file(buf
, desc_file
, 0) < 0)
1261 die_errno(_("unable to read branch description file '%s'"),
1265 static void prepare_cover_text(struct pretty_print_context
*pp
,
1266 const char *description_file
,
1267 const char *branch_name
,
1269 const char *encoding
,
1272 const char *subject
= "*** SUBJECT HERE ***";
1273 const char *body
= "*** BLURB HERE ***";
1274 struct strbuf description_sb
= STRBUF_INIT
;
1275 struct strbuf subject_sb
= STRBUF_INIT
;
1277 if (cover_from_description_mode
== COVER_FROM_NONE
)
1280 if (description_file
&& *description_file
)
1281 read_desc_file(&description_sb
, description_file
);
1282 else if (branch_name
&& *branch_name
)
1283 read_branch_desc(&description_sb
, branch_name
);
1284 if (!description_sb
.len
)
1287 if (cover_from_description_mode
== COVER_FROM_SUBJECT
||
1288 cover_from_description_mode
== COVER_FROM_AUTO
)
1289 body
= format_subject(&subject_sb
, description_sb
.buf
, " ");
1291 if (cover_from_description_mode
== COVER_FROM_MESSAGE
||
1292 (cover_from_description_mode
== COVER_FROM_AUTO
&&
1293 subject_sb
.len
> COVER_FROM_AUTO_MAX_SUBJECT_LEN
))
1294 body
= description_sb
.buf
;
1296 subject
= subject_sb
.buf
;
1299 pp_title_line(pp
, &subject
, sb
, encoding
, need_8bit_cte
);
1300 pp_remainder(pp
, &body
, sb
, 0);
1302 strbuf_release(&description_sb
);
1303 strbuf_release(&subject_sb
);
1306 static int get_notes_refs(struct string_list_item
*item
, void *arg
)
1308 strvec_pushf(arg
, "--notes=%s", item
->string
);
1312 static void get_notes_args(struct strvec
*arg
, struct rev_info
*rev
)
1314 if (!rev
->show_notes
) {
1315 strvec_push(arg
, "--no-notes");
1316 } else if (rev
->notes_opt
.use_default_notes
> 0 ||
1317 (rev
->notes_opt
.use_default_notes
== -1 &&
1318 !rev
->notes_opt
.extra_notes_refs
.nr
)) {
1319 strvec_push(arg
, "--notes");
1321 for_each_string_list(&rev
->notes_opt
.extra_notes_refs
, get_notes_refs
, arg
);
1325 static void make_cover_letter(struct rev_info
*rev
, int use_separate_file
,
1326 struct commit
*origin
,
1327 int nr
, struct commit
**list
,
1328 const char *description_file
,
1329 const char *branch_name
,
1332 const char *committer
;
1333 struct shortlog log
;
1334 struct strbuf sb
= STRBUF_INIT
;
1336 const char *encoding
= "UTF-8";
1337 int need_8bit_cte
= 0;
1338 struct pretty_print_context pp
= {0};
1339 struct commit
*head
= list
[0];
1341 if (!cmit_fmt_is_mail(rev
->commit_format
))
1342 die(_("cover letter needs email format"));
1344 committer
= git_committer_info(0);
1346 if (use_separate_file
&&
1347 open_next_file(NULL
, rev
->numbered_files
? NULL
: "cover-letter", rev
, quiet
))
1348 die(_("failed to create cover-letter file"));
1350 log_write_email_headers(rev
, head
, &pp
.after_subject
, &need_8bit_cte
, 0);
1352 for (i
= 0; !need_8bit_cte
&& i
< nr
; i
++) {
1353 const char *buf
= repo_get_commit_buffer(the_repository
,
1355 if (has_non_ascii(buf
))
1357 repo_unuse_commit_buffer(the_repository
, list
[i
], buf
);
1361 branch_name
= find_branch_name(rev
);
1363 pp
.fmt
= CMIT_FMT_EMAIL
;
1364 pp
.date_mode
.type
= DATE_RFC2822
;
1366 pp
.print_email_subject
= 1;
1367 pp_user_info(&pp
, NULL
, &sb
, committer
, encoding
);
1368 prepare_cover_text(&pp
, description_file
, branch_name
, &sb
,
1369 encoding
, need_8bit_cte
);
1370 fprintf(rev
->diffopt
.file
, "%s\n", sb
.buf
);
1372 strbuf_release(&sb
);
1374 shortlog_init(&log
);
1376 log
.wrap
= MAIL_DEFAULT_WRAP
;
1379 log
.file
= rev
->diffopt
.file
;
1380 log
.groups
= SHORTLOG_GROUP_AUTHOR
;
1381 shortlog_finish_setup(&log
);
1382 for (i
= 0; i
< nr
; i
++)
1383 shortlog_add_commit(&log
, list
[i
]);
1385 shortlog_output(&log
);
1387 /* We can only do diffstat with a unique reference point */
1389 show_diffstat(rev
, origin
, head
);
1391 if (rev
->idiff_oid1
) {
1392 fprintf_ln(rev
->diffopt
.file
, "%s", rev
->idiff_title
);
1393 show_interdiff(rev
->idiff_oid1
, rev
->idiff_oid2
, 0,
1399 * Pass minimum required diff-options to range-diff; others
1400 * can be added later if deemed desirable.
1402 struct diff_options opts
;
1403 struct strvec other_arg
= STRVEC_INIT
;
1404 struct range_diff_options range_diff_opts
= {
1405 .creation_factor
= rev
->creation_factor
,
1408 .other_arg
= &other_arg
1411 repo_diff_setup(the_repository
, &opts
);
1412 opts
.file
= rev
->diffopt
.file
;
1413 opts
.use_color
= rev
->diffopt
.use_color
;
1414 diff_setup_done(&opts
);
1415 fprintf_ln(rev
->diffopt
.file
, "%s", rev
->rdiff_title
);
1416 get_notes_args(&other_arg
, rev
);
1417 show_range_diff(rev
->rdiff1
, rev
->rdiff2
, &range_diff_opts
);
1418 strvec_clear(&other_arg
);
1422 static char *clean_message_id(const char *msg_id
)
1425 const char *a
, *z
, *m
;
1428 while ((ch
= *m
) && (isspace(ch
) || (ch
== '<')))
1433 if (!isspace(ch
) && (ch
!= '>'))
1438 die(_("insane in-reply-to: %s"), msg_id
);
1441 return xmemdupz(a
, z
- a
);
1444 static const char *set_outdir(const char *prefix
, const char *output_directory
)
1446 if (output_directory
&& is_absolute_path(output_directory
))
1447 return output_directory
;
1449 if (!prefix
|| !*prefix
) {
1450 if (output_directory
)
1451 return output_directory
;
1452 /* The user did not explicitly ask for "./" */
1457 outdir_offset
= strlen(prefix
);
1458 if (!output_directory
)
1461 return prefix_filename(prefix
, output_directory
);
1464 static const char * const builtin_format_patch_usage
[] = {
1465 N_("git format-patch [<options>] [<since> | <revision-range>]"),
1469 static int keep_subject
= 0;
1471 static int keep_callback(const struct option
*opt
, const char *arg
, int unset
)
1473 BUG_ON_OPT_NEG(unset
);
1474 BUG_ON_OPT_ARG(arg
);
1475 ((struct rev_info
*)opt
->value
)->total
= -1;
1480 static int subject_prefix
= 0;
1482 static int subject_prefix_callback(const struct option
*opt
, const char *arg
,
1485 struct strbuf
*sprefix
;
1487 BUG_ON_OPT_NEG(unset
);
1488 sprefix
= opt
->value
;
1490 strbuf_reset(sprefix
);
1491 strbuf_addstr(sprefix
, arg
);
1495 static int numbered_cmdline_opt
= 0;
1497 static int numbered_callback(const struct option
*opt
, const char *arg
,
1500 BUG_ON_OPT_ARG(arg
);
1501 *(int *)opt
->value
= numbered_cmdline_opt
= unset
? 0 : 1;
1507 static int no_numbered_callback(const struct option
*opt
, const char *arg
,
1510 BUG_ON_OPT_NEG(unset
);
1511 return numbered_callback(opt
, arg
, 1);
1514 static int output_directory_callback(const struct option
*opt
, const char *arg
,
1517 const char **dir
= (const char **)opt
->value
;
1518 BUG_ON_OPT_NEG(unset
);
1520 die(_("two output directories?"));
1525 static int thread_callback(const struct option
*opt
, const char *arg
, int unset
)
1527 enum thread_level
*thread
= (enum thread_level
*)opt
->value
;
1529 *thread
= THREAD_UNSET
;
1530 else if (!arg
|| !strcmp(arg
, "shallow"))
1531 *thread
= THREAD_SHALLOW
;
1532 else if (!strcmp(arg
, "deep"))
1533 *thread
= THREAD_DEEP
;
1535 * Please update _git_formatpatch() in git-completion.bash
1536 * when you add new options.
1543 static int attach_callback(const struct option
*opt
, const char *arg
, int unset
)
1545 struct rev_info
*rev
= (struct rev_info
*)opt
->value
;
1547 rev
->mime_boundary
= NULL
;
1549 rev
->mime_boundary
= arg
;
1551 rev
->mime_boundary
= git_version_string
;
1552 rev
->no_inline
= unset
? 0 : 1;
1556 static int inline_callback(const struct option
*opt
, const char *arg
, int unset
)
1558 struct rev_info
*rev
= (struct rev_info
*)opt
->value
;
1560 rev
->mime_boundary
= NULL
;
1562 rev
->mime_boundary
= arg
;
1564 rev
->mime_boundary
= git_version_string
;
1569 static int header_callback(const struct option
*opt UNUSED
, const char *arg
,
1573 string_list_clear(&extra_hdr
, 0);
1574 string_list_clear(&extra_to
, 0);
1575 string_list_clear(&extra_cc
, 0);
1582 static int from_callback(const struct option
*opt
, const char *arg
, int unset
)
1584 char **from
= opt
->value
;
1591 *from
= xstrdup(arg
);
1593 *from
= xstrdup(git_committer_info(IDENT_NO_DATE
));
1597 static int base_callback(const struct option
*opt
, const char *arg
, int unset
)
1599 const char **base_commit
= opt
->value
;
1602 auto_base
= AUTO_BASE_NEVER
;
1603 *base_commit
= NULL
;
1604 } else if (!strcmp(arg
, "auto")) {
1605 auto_base
= AUTO_BASE_ALWAYS
;
1606 *base_commit
= NULL
;
1608 auto_base
= AUTO_BASE_NEVER
;
1614 struct base_tree_info
{
1615 struct object_id base_commit
;
1616 int nr_patch_id
, alloc_patch_id
;
1617 struct object_id
*patch_id
;
1620 static struct commit
*get_base_commit(const char *base_commit
,
1621 struct commit
**list
,
1624 struct commit
*base
= NULL
;
1625 struct commit
**rev
;
1626 int i
= 0, rev_nr
= 0, auto_select
, die_on_failure
;
1628 switch (auto_base
) {
1629 case AUTO_BASE_NEVER
:
1634 /* no base information is requested */
1638 case AUTO_BASE_ALWAYS
:
1639 case AUTO_BASE_WHEN_ABLE
:
1641 BUG("requested automatic base selection but a commit was provided");
1644 die_on_failure
= auto_base
== AUTO_BASE_ALWAYS
;
1648 BUG("unexpected automatic base selection method");
1652 base
= lookup_commit_reference_by_name(base_commit
);
1654 die(_("unknown commit %s"), base_commit
);
1656 struct branch
*curr_branch
= branch_get(NULL
);
1657 const char *upstream
= branch_get_upstream(curr_branch
, NULL
);
1659 struct commit_list
*base_list
;
1660 struct commit
*commit
;
1661 struct object_id oid
;
1663 if (repo_get_oid(the_repository
, upstream
, &oid
)) {
1665 die(_("failed to resolve '%s' as a valid ref"), upstream
);
1669 commit
= lookup_commit_or_die(&oid
, "upstream base");
1670 base_list
= repo_get_merge_bases_many(the_repository
,
1673 /* There should be one and only one merge base. */
1674 if (!base_list
|| base_list
->next
) {
1675 if (die_on_failure
) {
1676 die(_("could not find exact merge base"));
1678 free_commit_list(base_list
);
1682 base
= base_list
->item
;
1683 free_commit_list(base_list
);
1686 die(_("failed to get upstream, if you want to record base commit automatically,\n"
1687 "please use git branch --set-upstream-to to track a remote branch.\n"
1688 "Or you could specify base commit by --base=<base-commit-id> manually"));
1694 ALLOC_ARRAY(rev
, total
);
1695 for (i
= 0; i
< total
; i
++)
1700 * Get merge base through pair-wise computations
1701 * and store it in rev[0].
1703 while (rev_nr
> 1) {
1704 for (i
= 0; i
< rev_nr
/ 2; i
++) {
1705 struct commit_list
*merge_base
;
1706 merge_base
= repo_get_merge_bases(the_repository
,
1709 if (!merge_base
|| merge_base
->next
) {
1710 if (die_on_failure
) {
1711 die(_("failed to find exact merge base"));
1718 rev
[i
] = merge_base
->item
;
1722 rev
[i
] = rev
[2 * i
];
1723 rev_nr
= DIV_ROUND_UP(rev_nr
, 2);
1726 if (!repo_in_merge_bases(the_repository
, base
, rev
[0])) {
1727 if (die_on_failure
) {
1728 die(_("base commit should be the ancestor of revision list"));
1735 for (i
= 0; i
< total
; i
++) {
1736 if (base
== list
[i
]) {
1737 if (die_on_failure
) {
1738 die(_("base commit shouldn't be in revision list"));
1750 define_commit_slab(commit_base
, int);
1752 static void prepare_bases(struct base_tree_info
*bases
,
1753 struct commit
*base
,
1754 struct commit
**list
,
1757 struct commit
*commit
;
1758 struct rev_info revs
;
1759 struct diff_options diffopt
;
1760 struct commit_base commit_base
;
1766 init_commit_base(&commit_base
);
1767 repo_diff_setup(the_repository
, &diffopt
);
1768 diffopt
.flags
.recursive
= 1;
1769 diff_setup_done(&diffopt
);
1771 oidcpy(&bases
->base_commit
, &base
->object
.oid
);
1773 repo_init_revisions(the_repository
, &revs
, NULL
);
1774 revs
.max_parents
= 1;
1775 revs
.topo_order
= 1;
1776 for (i
= 0; i
< total
; i
++) {
1777 list
[i
]->object
.flags
&= ~UNINTERESTING
;
1778 add_pending_object(&revs
, &list
[i
]->object
, "rev_list");
1779 *commit_base_at(&commit_base
, list
[i
]) = 1;
1781 base
->object
.flags
|= UNINTERESTING
;
1782 add_pending_object(&revs
, &base
->object
, "base");
1784 if (prepare_revision_walk(&revs
))
1785 die(_("revision walk setup failed"));
1787 * Traverse the commits list, get prerequisite patch ids
1788 * and stuff them in bases structure.
1790 while ((commit
= get_revision(&revs
)) != NULL
) {
1791 struct object_id oid
;
1792 struct object_id
*patch_id
;
1793 if (*commit_base_at(&commit_base
, commit
))
1795 if (commit_patch_id(commit
, &diffopt
, &oid
, 0))
1796 die(_("cannot get patch id"));
1797 ALLOC_GROW(bases
->patch_id
, bases
->nr_patch_id
+ 1, bases
->alloc_patch_id
);
1798 patch_id
= bases
->patch_id
+ bases
->nr_patch_id
;
1799 oidcpy(patch_id
, &oid
);
1800 bases
->nr_patch_id
++;
1802 clear_commit_base(&commit_base
);
1805 static void print_bases(struct base_tree_info
*bases
, FILE *file
)
1809 /* Only do this once, either for the cover or for the first one */
1810 if (is_null_oid(&bases
->base_commit
))
1813 /* Show the base commit */
1814 fprintf(file
, "\nbase-commit: %s\n", oid_to_hex(&bases
->base_commit
));
1816 /* Show the prerequisite patches */
1817 for (i
= bases
->nr_patch_id
- 1; i
>= 0; i
--)
1818 fprintf(file
, "prerequisite-patch-id: %s\n", oid_to_hex(&bases
->patch_id
[i
]));
1820 free(bases
->patch_id
);
1821 bases
->nr_patch_id
= 0;
1822 bases
->alloc_patch_id
= 0;
1823 oidclr(&bases
->base_commit
);
1826 static const char *diff_title(struct strbuf
*sb
,
1827 const char *reroll_count
,
1828 const char *generic
,
1829 const char *rerolled
)
1833 /* RFC may be v0, so allow -v1 to diff against v0 */
1834 if (reroll_count
&& !strtol_i(reroll_count
, 10, &v
) &&
1836 strbuf_addf(sb
, rerolled
, v
- 1);
1838 strbuf_addstr(sb
, generic
);
1842 static void infer_range_diff_ranges(struct strbuf
*r1
,
1845 struct commit
*origin
,
1846 struct commit
*head
)
1848 const char *head_oid
= oid_to_hex(&head
->object
.oid
);
1849 int prev_is_range
= is_range_diff_range(prev
);
1852 strbuf_addstr(r1
, prev
);
1854 strbuf_addf(r1
, "%s..%s", head_oid
, prev
);
1857 strbuf_addf(r2
, "%s..%s", oid_to_hex(&origin
->object
.oid
), head_oid
);
1858 else if (prev_is_range
)
1859 die(_("failed to infer range-diff origin of current series"));
1861 warning(_("using '%s' as range-diff origin of current series"), prev
);
1862 strbuf_addf(r2
, "%s..%s", prev
, head_oid
);
1866 int cmd_format_patch(int argc
, const char **argv
, const char *prefix
)
1868 struct commit
*commit
;
1869 struct commit
**list
= NULL
;
1870 struct rev_info rev
;
1871 char *to_free
= NULL
;
1872 struct setup_revision_opt s_r_opt
;
1873 int nr
= 0, total
, i
;
1875 int start_number
= -1;
1876 int just_numbers
= 0;
1877 int ignore_if_in_upstream
= 0;
1878 int cover_letter
= -1;
1879 int boundary_count
= 0;
1880 int no_binary_diff
= 0;
1881 int zero_commit
= 0;
1882 struct commit
*origin
= NULL
;
1883 const char *in_reply_to
= NULL
;
1884 struct patch_ids ids
;
1885 struct strbuf buf
= STRBUF_INIT
;
1886 int use_patch_format
= 0;
1888 const char *reroll_count
= NULL
;
1889 char *cover_from_description_arg
= NULL
;
1890 char *description_file
= NULL
;
1891 char *branch_name
= NULL
;
1892 char *base_commit
= NULL
;
1893 struct base_tree_info bases
;
1894 struct commit
*base
;
1895 int show_progress
= 0;
1896 struct progress
*progress
= NULL
;
1897 struct oid_array idiff_prev
= OID_ARRAY_INIT
;
1898 struct strbuf idiff_title
= STRBUF_INIT
;
1899 const char *rdiff_prev
= NULL
;
1900 struct strbuf rdiff1
= STRBUF_INIT
;
1901 struct strbuf rdiff2
= STRBUF_INIT
;
1902 struct strbuf rdiff_title
= STRBUF_INIT
;
1903 struct strbuf sprefix
= STRBUF_INIT
;
1904 int creation_factor
= -1;
1907 const struct option builtin_format_patch_options
[] = {
1908 OPT_CALLBACK_F('n', "numbered", &numbered
, NULL
,
1909 N_("use [PATCH n/m] even with a single patch"),
1910 PARSE_OPT_NOARG
, numbered_callback
),
1911 OPT_CALLBACK_F('N', "no-numbered", &numbered
, NULL
,
1912 N_("use [PATCH] even with multiple patches"),
1913 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
, no_numbered_callback
),
1914 OPT_BOOL('s', "signoff", &do_signoff
, N_("add a Signed-off-by trailer")),
1915 OPT_BOOL(0, "stdout", &use_stdout
,
1916 N_("print patches to standard out")),
1917 OPT_BOOL(0, "cover-letter", &cover_letter
,
1918 N_("generate a cover letter")),
1919 OPT_BOOL(0, "numbered-files", &just_numbers
,
1920 N_("use simple number sequence for output file names")),
1921 OPT_STRING(0, "suffix", &fmt_patch_suffix
, N_("sfx"),
1922 N_("use <sfx> instead of '.patch'")),
1923 OPT_INTEGER(0, "start-number", &start_number
,
1924 N_("start numbering patches at <n> instead of 1")),
1925 OPT_STRING('v', "reroll-count", &reroll_count
, N_("reroll-count"),
1926 N_("mark the series as Nth re-roll")),
1927 OPT_INTEGER(0, "filename-max-length", &fmt_patch_name_max
,
1928 N_("max length of output filename")),
1929 OPT_BOOL(0, "rfc", &rfc
, N_("use [RFC PATCH] instead of [PATCH]")),
1930 OPT_STRING(0, "cover-from-description", &cover_from_description_arg
,
1931 N_("cover-from-description-mode"),
1932 N_("generate parts of a cover letter based on a branch's description")),
1933 OPT_FILENAME(0, "description-file", &description_file
,
1934 N_("use branch description from file")),
1935 OPT_CALLBACK_F(0, "subject-prefix", &sprefix
, N_("prefix"),
1936 N_("use [<prefix>] instead of [PATCH]"),
1937 PARSE_OPT_NONEG
, subject_prefix_callback
),
1938 OPT_CALLBACK_F('o', "output-directory", &output_directory
,
1939 N_("dir"), N_("store resulting files in <dir>"),
1940 PARSE_OPT_NONEG
, output_directory_callback
),
1941 OPT_CALLBACK_F('k', "keep-subject", &rev
, NULL
,
1942 N_("don't strip/add [PATCH]"),
1943 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
, keep_callback
),
1944 OPT_BOOL(0, "no-binary", &no_binary_diff
,
1945 N_("don't output binary diffs")),
1946 OPT_BOOL(0, "zero-commit", &zero_commit
,
1947 N_("output all-zero hash in From header")),
1948 OPT_BOOL(0, "ignore-if-in-upstream", &ignore_if_in_upstream
,
1949 N_("don't include a patch matching a commit upstream")),
1950 OPT_SET_INT_F('p', "no-stat", &use_patch_format
,
1951 N_("show patch format instead of default (patch + stat)"),
1952 1, PARSE_OPT_NONEG
),
1953 OPT_GROUP(N_("Messaging")),
1954 OPT_CALLBACK(0, "add-header", NULL
, N_("header"),
1955 N_("add email header"), header_callback
),
1956 OPT_STRING_LIST(0, "to", &extra_to
, N_("email"), N_("add To: header")),
1957 OPT_STRING_LIST(0, "cc", &extra_cc
, N_("email"), N_("add Cc: header")),
1958 OPT_CALLBACK_F(0, "from", &from
, N_("ident"),
1959 N_("set From address to <ident> (or committer ident if absent)"),
1960 PARSE_OPT_OPTARG
, from_callback
),
1961 OPT_STRING(0, "in-reply-to", &in_reply_to
, N_("message-id"),
1962 N_("make first mail a reply to <message-id>")),
1963 OPT_CALLBACK_F(0, "attach", &rev
, N_("boundary"),
1964 N_("attach the patch"), PARSE_OPT_OPTARG
,
1966 OPT_CALLBACK_F(0, "inline", &rev
, N_("boundary"),
1967 N_("inline the patch"),
1968 PARSE_OPT_OPTARG
| PARSE_OPT_NONEG
,
1970 OPT_CALLBACK_F(0, "thread", &thread
, N_("style"),
1971 N_("enable message threading, styles: shallow, deep"),
1972 PARSE_OPT_OPTARG
, thread_callback
),
1973 OPT_STRING(0, "signature", &signature
, N_("signature"),
1974 N_("add a signature")),
1975 OPT_CALLBACK_F(0, "base", &base_commit
, N_("base-commit"),
1976 N_("add prerequisite tree info to the patch series"),
1978 OPT_FILENAME(0, "signature-file", &signature_file
,
1979 N_("add a signature from a file")),
1980 OPT__QUIET(&quiet
, N_("don't print the patch filenames")),
1981 OPT_BOOL(0, "progress", &show_progress
,
1982 N_("show progress while generating patches")),
1983 OPT_CALLBACK(0, "interdiff", &idiff_prev
, N_("rev"),
1984 N_("show changes against <rev> in cover letter or single patch"),
1985 parse_opt_object_name
),
1986 OPT_STRING(0, "range-diff", &rdiff_prev
, N_("refspec"),
1987 N_("show changes against <refspec> in cover letter or single patch")),
1988 OPT_INTEGER(0, "creation-factor", &creation_factor
,
1989 N_("percentage by which creation is weighted")),
1990 OPT_BOOL(0, "force-in-body-from", &force_in_body_from
,
1991 N_("show in-body From: even if identical to the e-mail header")),
1995 extra_hdr
.strdup_strings
= 1;
1996 extra_to
.strdup_strings
= 1;
1997 extra_cc
.strdup_strings
= 1;
1999 init_log_defaults();
2000 init_display_notes(¬es_opt
);
2001 git_config(git_format_config
, NULL
);
2002 repo_init_revisions(the_repository
, &rev
, prefix
);
2003 git_config(grep_config
, &rev
.grep_filter
);
2005 rev
.show_notes
= show_notes
;
2006 memcpy(&rev
.notes_opt
, ¬es_opt
, sizeof(notes_opt
));
2007 rev
.commit_format
= CMIT_FMT_EMAIL
;
2008 rev
.encode_email_headers
= default_encode_email_headers
;
2009 rev
.expand_tabs_in_log_default
= 0;
2010 rev
.verbose_header
= 1;
2012 rev
.max_parents
= 1;
2013 rev
.diffopt
.flags
.recursive
= 1;
2014 rev
.diffopt
.no_free
= 1;
2015 memset(&s_r_opt
, 0, sizeof(s_r_opt
));
2016 s_r_opt
.def
= "HEAD";
2017 s_r_opt
.revarg_opt
= REVARG_COMMITTISH
;
2019 strbuf_addstr(&sprefix
, fmt_patch_subject_prefix
);
2020 if (format_no_prefix
)
2021 diff_set_noprefix(&rev
.diffopt
);
2023 if (default_attach
) {
2024 rev
.mime_boundary
= default_attach
;
2029 * Parse the arguments before setup_revisions(), or something
2030 * like "git format-patch -o a123 HEAD^.." may fail; a123 is
2031 * possibly a valid SHA1.
2033 argc
= parse_options(argc
, argv
, prefix
, builtin_format_patch_options
,
2034 builtin_format_patch_usage
,
2035 PARSE_OPT_KEEP_ARGV0
| PARSE_OPT_KEEP_UNKNOWN_OPT
|
2036 PARSE_OPT_KEEP_DASHDASH
);
2038 rev
.force_in_body_from
= force_in_body_from
;
2040 /* Make sure "0000-$sub.patch" gives non-negative length for $sub */
2041 if (fmt_patch_name_max
<= strlen("0000-") + strlen(fmt_patch_suffix
))
2042 fmt_patch_name_max
= strlen("0000-") + strlen(fmt_patch_suffix
);
2044 if (cover_from_description_arg
)
2045 cover_from_description_mode
= parse_cover_from_description(cover_from_description_arg
);
2048 strbuf_insertstr(&sprefix
, 0, "RFC ");
2051 strbuf_addf(&sprefix
, " v%s", reroll_count
);
2052 rev
.reroll_count
= reroll_count
;
2055 rev
.subject_prefix
= sprefix
.buf
;
2057 for (i
= 0; i
< extra_hdr
.nr
; i
++) {
2058 strbuf_addstr(&buf
, extra_hdr
.items
[i
].string
);
2059 strbuf_addch(&buf
, '\n');
2063 strbuf_addstr(&buf
, "To: ");
2064 for (i
= 0; i
< extra_to
.nr
; i
++) {
2066 strbuf_addstr(&buf
, " ");
2067 strbuf_addstr(&buf
, extra_to
.items
[i
].string
);
2068 if (i
+ 1 < extra_to
.nr
)
2069 strbuf_addch(&buf
, ',');
2070 strbuf_addch(&buf
, '\n');
2074 strbuf_addstr(&buf
, "Cc: ");
2075 for (i
= 0; i
< extra_cc
.nr
; i
++) {
2077 strbuf_addstr(&buf
, " ");
2078 strbuf_addstr(&buf
, extra_cc
.items
[i
].string
);
2079 if (i
+ 1 < extra_cc
.nr
)
2080 strbuf_addch(&buf
, ',');
2081 strbuf_addch(&buf
, '\n');
2084 rev
.extra_headers
= to_free
= strbuf_detach(&buf
, NULL
);
2087 if (split_ident_line(&rev
.from_ident
, from
, strlen(from
)))
2088 die(_("invalid ident line: %s"), from
);
2091 if (start_number
< 0)
2095 * If numbered is set solely due to format.numbered in config,
2096 * and it would conflict with --keep-subject (-k) from the
2097 * command line, reset "numbered".
2099 if (numbered
&& keep_subject
&& !numbered_cmdline_opt
)
2102 if (numbered
&& keep_subject
)
2103 die(_("options '%s' and '%s' cannot be used together"), "-n", "-k");
2104 if (keep_subject
&& subject_prefix
)
2105 die(_("options '%s' and '%s' cannot be used together"), "--subject-prefix/--rfc", "-k");
2106 rev
.preserve_subject
= keep_subject
;
2108 argc
= setup_revisions(argc
, argv
, &rev
, &s_r_opt
);
2110 die(_("unrecognized argument: %s"), argv
[1]);
2112 if (rev
.diffopt
.output_format
& DIFF_FORMAT_NAME
)
2113 die(_("--name-only does not make sense"));
2114 if (rev
.diffopt
.output_format
& DIFF_FORMAT_NAME_STATUS
)
2115 die(_("--name-status does not make sense"));
2116 if (rev
.diffopt
.output_format
& DIFF_FORMAT_CHECKDIFF
)
2117 die(_("--check does not make sense"));
2118 if (rev
.remerge_diff
)
2119 die(_("--remerge-diff does not make sense"));
2121 if (!use_patch_format
&&
2122 (!rev
.diffopt
.output_format
||
2123 rev
.diffopt
.output_format
== DIFF_FORMAT_PATCH
))
2124 rev
.diffopt
.output_format
= DIFF_FORMAT_DIFFSTAT
| DIFF_FORMAT_SUMMARY
;
2125 if (!rev
.diffopt
.stat_width
)
2126 rev
.diffopt
.stat_width
= MAIL_DEFAULT_WRAP
;
2128 /* Always generate a patch */
2129 rev
.diffopt
.output_format
|= DIFF_FORMAT_PATCH
;
2130 rev
.always_show_header
= 1;
2132 rev
.zero_commit
= zero_commit
;
2133 rev
.patch_name_max
= fmt_patch_name_max
;
2135 if (!rev
.diffopt
.flags
.text
&& !no_binary_diff
)
2136 rev
.diffopt
.flags
.binary
= 1;
2139 load_display_notes(&rev
.notes_opt
);
2141 die_for_incompatible_opt3(use_stdout
, "--stdout",
2142 rev
.diffopt
.close_file
, "--output",
2143 !!output_directory
, "--output-directory");
2145 if (use_stdout
&& stdout_mboxrd
)
2146 rev
.commit_format
= CMIT_FMT_MBOXRD
;
2150 } else if (!rev
.diffopt
.close_file
) {
2153 if (!output_directory
)
2154 output_directory
= config_output_directory
;
2155 output_directory
= set_outdir(prefix
, output_directory
);
2157 if (rev
.diffopt
.use_color
!= GIT_COLOR_ALWAYS
)
2158 rev
.diffopt
.use_color
= GIT_COLOR_NEVER
;
2160 * We consider <outdir> as 'outside of gitdir', therefore avoid
2161 * applying adjust_shared_perm in s-c-l-d.
2163 saved
= get_shared_repository();
2164 set_shared_repository(0);
2165 switch (safe_create_leading_directories_const(output_directory
)) {
2170 die(_("could not create leading directories "
2171 "of '%s'"), output_directory
);
2173 set_shared_repository(saved
);
2174 if (mkdir(output_directory
, 0777) < 0 && errno
!= EEXIST
)
2175 die_errno(_("could not create directory '%s'"),
2179 if (rev
.pending
.nr
== 1) {
2182 if (rev
.max_count
< 0 && !rev
.show_root_diff
) {
2184 * This is traditional behaviour of "git format-patch
2185 * origin" that prepares what the origin side still
2188 rev
.pending
.objects
[0].item
->flags
|= UNINTERESTING
;
2189 add_head_to_pending(&rev
);
2193 * Otherwise, it is "format-patch -22 HEAD", and/or
2194 * "format-patch --root HEAD". The user wants
2195 * get_revision() to do the usual traversal.
2198 if (!strcmp(rev
.pending
.objects
[0].name
, "HEAD"))
2202 const char *ref
, *v
;
2203 ref
= resolve_ref_unsafe("HEAD", RESOLVE_REF_READING
,
2205 if (ref
&& skip_prefix(ref
, "refs/heads/", &v
))
2206 branch_name
= xstrdup(v
);
2208 branch_name
= xstrdup(""); /* no branch */
2213 * We cannot move this anywhere earlier because we do want to
2214 * know if --root was given explicitly from the command line.
2216 rev
.show_root_diff
= 1;
2218 if (ignore_if_in_upstream
) {
2219 /* Don't say anything if head and upstream are the same. */
2220 if (rev
.pending
.nr
== 2) {
2221 struct object_array_entry
*o
= rev
.pending
.objects
;
2222 if (oideq(&o
[0].item
->oid
, &o
[1].item
->oid
))
2225 get_patch_ids(&rev
, &ids
);
2228 if (prepare_revision_walk(&rev
))
2229 die(_("revision walk setup failed"));
2231 while ((commit
= get_revision(&rev
)) != NULL
) {
2232 if (commit
->object
.flags
& BOUNDARY
) {
2234 origin
= (boundary_count
== 1) ? commit
: NULL
;
2238 if (ignore_if_in_upstream
&& has_commit_patch_id(commit
, &ids
))
2242 REALLOC_ARRAY(list
, nr
);
2243 list
[nr
- 1] = commit
;
2249 if (cover_letter
== -1) {
2250 if (config_cover_letter
== COVER_AUTO
)
2251 cover_letter
= (total
> 1);
2253 cover_letter
= (config_cover_letter
== COVER_ON
);
2255 if (!keep_subject
&& auto_number
&& (total
> 1 || cover_letter
))
2258 rev
.total
= total
+ start_number
- 1;
2260 if (idiff_prev
.nr
) {
2261 if (!cover_letter
&& total
!= 1)
2262 die(_("--interdiff requires --cover-letter or single patch"));
2263 rev
.idiff_oid1
= &idiff_prev
.oid
[idiff_prev
.nr
- 1];
2264 rev
.idiff_oid2
= get_commit_tree_oid(list
[0]);
2265 rev
.idiff_title
= diff_title(&idiff_title
, reroll_count
,
2267 _("Interdiff against v%d:"));
2270 if (creation_factor
< 0)
2271 creation_factor
= RANGE_DIFF_CREATION_FACTOR_DEFAULT
;
2272 else if (!rdiff_prev
)
2273 die(_("the option '%s' requires '%s'"), "--creation-factor", "--range-diff");
2276 if (!cover_letter
&& total
!= 1)
2277 die(_("--range-diff requires --cover-letter or single patch"));
2279 infer_range_diff_ranges(&rdiff1
, &rdiff2
, rdiff_prev
,
2281 rev
.rdiff1
= rdiff1
.buf
;
2282 rev
.rdiff2
= rdiff2
.buf
;
2283 rev
.creation_factor
= creation_factor
;
2284 rev
.rdiff_title
= diff_title(&rdiff_title
, reroll_count
,
2286 _("Range-diff against v%d:"));
2290 ; /* --no-signature inhibits all signatures */
2291 } else if (signature
&& signature
!= git_version_string
) {
2292 ; /* non-default signature already set */
2293 } else if (signature_file
) {
2294 struct strbuf buf
= STRBUF_INIT
;
2296 if (strbuf_read_file(&buf
, signature_file
, 128) < 0)
2297 die_errno(_("unable to read signature file '%s'"), signature_file
);
2298 signature
= strbuf_detach(&buf
, NULL
);
2301 memset(&bases
, 0, sizeof(bases
));
2302 base
= get_base_commit(base_commit
, list
, nr
);
2304 reset_revision_walk();
2305 clear_object_flags(UNINTERESTING
);
2306 prepare_bases(&bases
, base
, list
, nr
);
2309 if (in_reply_to
|| thread
|| cover_letter
) {
2310 rev
.ref_message_ids
= xmalloc(sizeof(*rev
.ref_message_ids
));
2311 string_list_init_dup(rev
.ref_message_ids
);
2314 char *msgid
= clean_message_id(in_reply_to
);
2315 string_list_append_nodup(rev
.ref_message_ids
, msgid
);
2317 rev
.numbered_files
= just_numbers
;
2318 rev
.patch_suffix
= fmt_patch_suffix
;
2321 gen_message_id(&rev
, "cover");
2322 make_cover_letter(&rev
, !!output_directory
,
2323 origin
, nr
, list
, description_file
, branch_name
, quiet
);
2324 print_bases(&bases
, rev
.diffopt
.file
);
2325 print_signature(rev
.diffopt
.file
);
2328 /* interdiff/range-diff in cover-letter; omit from patches */
2329 rev
.idiff_oid1
= NULL
;
2332 rev
.add_signoff
= do_signoff
;
2335 progress
= start_delayed_progress(_("Generating patches"), total
);
2338 display_progress(progress
, total
- nr
);
2340 rev
.nr
= total
- nr
+ (start_number
- 1);
2341 /* Make the second and subsequent mails replies to the first */
2343 /* Have we already had a message ID? */
2344 if (rev
.message_id
) {
2346 * For deep threading: make every mail
2347 * a reply to the previous one, no
2348 * matter what other options are set.
2350 * For shallow threading:
2352 * Without --cover-letter and
2353 * --in-reply-to, make every mail a
2354 * reply to the one before.
2356 * With --in-reply-to but no
2357 * --cover-letter, make every mail a
2358 * reply to the <reply-to>.
2360 * With --cover-letter, make every
2361 * mail but the cover letter a reply
2362 * to the cover letter. The cover
2363 * letter is a reply to the
2364 * --in-reply-to, if specified.
2366 if (thread
== THREAD_SHALLOW
2367 && rev
.ref_message_ids
->nr
> 0
2368 && (!cover_letter
|| rev
.nr
> 1))
2369 free(rev
.message_id
);
2371 string_list_append_nodup(rev
.ref_message_ids
,
2374 gen_message_id(&rev
, oid_to_hex(&commit
->object
.oid
));
2377 if (output_directory
&&
2378 open_next_file(rev
.numbered_files
? NULL
: commit
, NULL
, &rev
, quiet
))
2379 die(_("failed to create output files"));
2380 shown
= log_tree_commit(&rev
, commit
);
2381 free_commit_buffer(the_repository
->parsed_objects
,
2384 /* We put one extra blank line between formatted
2385 * patches and this flag is used by log-tree code
2386 * to see if it needs to emit a LF before showing
2387 * the log; when using one file per patch, we do
2388 * not want the extra blank line.
2390 if (output_directory
)
2393 print_bases(&bases
, rev
.diffopt
.file
);
2394 if (rev
.mime_boundary
)
2395 fprintf(rev
.diffopt
.file
, "\n--%s%s--\n\n\n",
2396 mime_boundary_leader
,
2399 print_signature(rev
.diffopt
.file
);
2401 if (output_directory
)
2402 fclose(rev
.diffopt
.file
);
2404 stop_progress(&progress
);
2407 string_list_clear(&extra_to
, 0);
2408 string_list_clear(&extra_cc
, 0);
2409 string_list_clear(&extra_hdr
, 0);
2410 if (ignore_if_in_upstream
)
2411 free_patch_ids(&ids
);
2414 oid_array_clear(&idiff_prev
);
2415 strbuf_release(&idiff_title
);
2416 strbuf_release(&rdiff1
);
2417 strbuf_release(&rdiff2
);
2418 strbuf_release(&rdiff_title
);
2419 strbuf_release(&sprefix
);
2421 free(rev
.message_id
);
2422 if (rev
.ref_message_ids
)
2423 string_list_clear(rev
.ref_message_ids
, 0);
2424 free(rev
.ref_message_ids
);
2425 return cmd_log_deinit(0, &rev
);
2428 static int add_pending_commit(const char *arg
, struct rev_info
*revs
, int flags
)
2430 struct object_id oid
;
2431 if (repo_get_oid(the_repository
, arg
, &oid
) == 0) {
2432 struct commit
*commit
= lookup_commit_reference(the_repository
,
2435 commit
->object
.flags
|= flags
;
2436 add_pending_object(revs
, &commit
->object
, arg
);
2443 static const char * const cherry_usage
[] = {
2444 N_("git cherry [-v] [<upstream> [<head> [<limit>]]]"),
2448 static void print_commit(char sign
, struct commit
*commit
, int verbose
,
2449 int abbrev
, FILE *file
)
2452 fprintf(file
, "%c %s\n", sign
,
2453 repo_find_unique_abbrev(the_repository
, &commit
->object
.oid
, abbrev
));
2455 struct strbuf buf
= STRBUF_INIT
;
2456 pp_commit_easy(CMIT_FMT_ONELINE
, commit
, &buf
);
2457 fprintf(file
, "%c %s %s\n", sign
,
2458 repo_find_unique_abbrev(the_repository
, &commit
->object
.oid
, abbrev
),
2460 strbuf_release(&buf
);
2464 int cmd_cherry(int argc
, const char **argv
, const char *prefix
)
2466 struct rev_info revs
;
2467 struct patch_ids ids
;
2468 struct commit
*commit
;
2469 struct commit_list
*list
= NULL
;
2470 struct branch
*current_branch
;
2471 const char *upstream
;
2472 const char *head
= "HEAD";
2473 const char *limit
= NULL
;
2474 int verbose
= 0, abbrev
= 0;
2476 struct option options
[] = {
2477 OPT__ABBREV(&abbrev
),
2478 OPT__VERBOSE(&verbose
, N_("be verbose")),
2482 argc
= parse_options(argc
, argv
, prefix
, options
, cherry_usage
, 0);
2495 current_branch
= branch_get(NULL
);
2496 upstream
= branch_get_upstream(current_branch
, NULL
);
2498 fprintf(stderr
, _("Could not find a tracked"
2499 " remote branch, please"
2500 " specify <upstream> manually.\n"));
2501 usage_with_options(cherry_usage
, options
);
2505 repo_init_revisions(the_repository
, &revs
, prefix
);
2506 revs
.max_parents
= 1;
2508 if (add_pending_commit(head
, &revs
, 0))
2509 die(_("unknown commit %s"), head
);
2510 if (add_pending_commit(upstream
, &revs
, UNINTERESTING
))
2511 die(_("unknown commit %s"), upstream
);
2513 /* Don't say anything if head and upstream are the same. */
2514 if (revs
.pending
.nr
== 2) {
2515 struct object_array_entry
*o
= revs
.pending
.objects
;
2516 if (oideq(&o
[0].item
->oid
, &o
[1].item
->oid
))
2520 get_patch_ids(&revs
, &ids
);
2522 if (limit
&& add_pending_commit(limit
, &revs
, UNINTERESTING
))
2523 die(_("unknown commit %s"), limit
);
2525 /* reverse the list of commits */
2526 if (prepare_revision_walk(&revs
))
2527 die(_("revision walk setup failed"));
2528 while ((commit
= get_revision(&revs
)) != NULL
) {
2529 commit_list_insert(commit
, &list
);
2535 commit
= list
->item
;
2536 if (has_commit_patch_id(commit
, &ids
))
2538 print_commit(sign
, commit
, verbose
, abbrev
, revs
.diffopt
.file
);
2542 free_patch_ids(&ids
);