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")) {
599 return config_error_nonbool(var
);
600 return diff_merges_config(value
);
602 if (!strcmp(var
, "log.showroot")) {
603 default_show_root
= git_config_bool(var
, value
);
606 if (!strcmp(var
, "log.follow")) {
607 default_follow
= git_config_bool(var
, value
);
610 if (skip_prefix(var
, "color.decorate.", &slot_name
))
611 return parse_decorate_color_config(var
, slot_name
, value
);
612 if (!strcmp(var
, "log.mailmap")) {
613 use_mailmap_config
= git_config_bool(var
, value
);
616 if (!strcmp(var
, "log.showsignature")) {
617 default_show_signature
= git_config_bool(var
, value
);
621 return git_diff_ui_config(var
, value
, ctx
, cb
);
624 int cmd_whatchanged(int argc
, const char **argv
, const char *prefix
)
627 struct setup_revision_opt opt
;
630 git_config(git_log_config
, NULL
);
632 repo_init_revisions(the_repository
, &rev
, prefix
);
633 git_config(grep_config
, &rev
.grep_filter
);
636 rev
.simplify_history
= 0;
637 memset(&opt
, 0, sizeof(opt
));
639 opt
.revarg_opt
= REVARG_COMMITTISH
;
640 cmd_log_init(argc
, argv
, prefix
, &rev
, &opt
);
641 if (!rev
.diffopt
.output_format
)
642 rev
.diffopt
.output_format
= DIFF_FORMAT_RAW
;
643 return cmd_log_deinit(cmd_log_walk(&rev
), &rev
);
646 static void show_tagger(const char *buf
, struct rev_info
*rev
)
648 struct strbuf out
= STRBUF_INIT
;
649 struct pretty_print_context pp
= {0};
651 pp
.fmt
= rev
->commit_format
;
652 pp
.date_mode
= rev
->date_mode
;
653 pp_user_info(&pp
, "Tagger", &out
, buf
, get_log_output_encoding());
654 fprintf(rev
->diffopt
.file
, "%s", out
.buf
);
655 strbuf_release(&out
);
658 static int show_blob_object(const struct object_id
*oid
, struct rev_info
*rev
, const char *obj_name
)
660 struct object_id oidc
;
661 struct object_context obj_context
;
665 fflush(rev
->diffopt
.file
);
666 if (!rev
->diffopt
.flags
.textconv_set_via_cmdline
||
667 !rev
->diffopt
.flags
.allow_textconv
)
668 return stream_blob_to_fd(1, oid
, NULL
, 0);
670 if (get_oid_with_context(the_repository
, obj_name
,
672 &oidc
, &obj_context
))
673 die(_("not a valid object name %s"), obj_name
);
674 if (!obj_context
.path
||
675 !textconv_object(the_repository
, obj_context
.path
,
676 obj_context
.mode
, &oidc
, 1, &buf
, &size
)) {
677 free(obj_context
.path
);
678 return stream_blob_to_fd(1, oid
, NULL
, 0);
682 die(_("git show %s: bad file"), obj_name
);
684 write_or_die(1, buf
, size
);
685 free(obj_context
.path
);
689 static int show_tag_object(const struct object_id
*oid
, struct rev_info
*rev
)
692 enum object_type type
;
693 char *buf
= repo_read_object_file(the_repository
, oid
, &type
, &size
);
697 return error(_("could not read object %s"), oid_to_hex(oid
));
699 assert(type
== OBJ_TAG
);
700 while (offset
< size
&& buf
[offset
] != '\n') {
701 int new_offset
= offset
+ 1;
703 while (new_offset
< size
&& buf
[new_offset
++] != '\n')
705 if (skip_prefix(buf
+ offset
, "tagger ", &ident
))
706 show_tagger(ident
, rev
);
711 fwrite(buf
+ offset
, size
- offset
, 1, rev
->diffopt
.file
);
716 static int show_tree_object(const struct object_id
*oid UNUSED
,
717 struct strbuf
*base UNUSED
,
718 const char *pathname
, unsigned mode
,
721 FILE *file
= context
;
722 fprintf(file
, "%s%s\n", pathname
, S_ISDIR(mode
) ? "/" : "");
726 static void show_setup_revisions_tweak(struct rev_info
*rev
)
728 if (rev
->first_parent_only
)
729 diff_merges_default_to_first_parent(rev
);
731 diff_merges_default_to_dense_combined(rev
);
732 if (!rev
->diffopt
.output_format
)
733 rev
->diffopt
.output_format
= DIFF_FORMAT_PATCH
;
736 int cmd_show(int argc
, const char **argv
, const char *prefix
)
740 struct setup_revision_opt opt
;
741 struct pathspec match_all
;
745 git_config(git_log_config
, NULL
);
747 if (the_repository
->gitdir
) {
748 prepare_repo_settings(the_repository
);
749 the_repository
->settings
.command_requires_full_index
= 0;
752 memset(&match_all
, 0, sizeof(match_all
));
753 repo_init_revisions(the_repository
, &rev
, prefix
);
754 git_config(grep_config
, &rev
.grep_filter
);
757 rev
.always_show_header
= 1;
759 rev
.diffopt
.stat_width
= -1; /* Scale to real terminal size */
761 memset(&opt
, 0, sizeof(opt
));
763 opt
.tweak
= show_setup_revisions_tweak
;
764 cmd_log_init(argc
, argv
, prefix
, &rev
, &opt
);
767 return cmd_log_deinit(cmd_log_walk(&rev
), &rev
);
769 rev
.diffopt
.no_free
= 1;
770 for (i
= 0; i
< rev
.pending
.nr
&& !ret
; i
++) {
771 struct object
*o
= rev
.pending
.objects
[i
].item
;
772 const char *name
= rev
.pending
.objects
[i
].name
;
775 ret
= show_blob_object(&o
->oid
, &rev
, name
);
778 struct tag
*t
= (struct tag
*)o
;
779 struct object_id
*oid
= get_tagged_oid(t
);
783 fprintf(rev
.diffopt
.file
, "%stag %s%s\n",
784 diff_get_color_opt(&rev
.diffopt
, DIFF_COMMIT
),
786 diff_get_color_opt(&rev
.diffopt
, DIFF_RESET
));
787 ret
= show_tag_object(&o
->oid
, &rev
);
791 o
= parse_object(the_repository
, oid
);
793 ret
= error(_("could not read object %s"),
795 rev
.pending
.objects
[i
].item
= o
;
802 fprintf(rev
.diffopt
.file
, "%stree %s%s\n\n",
803 diff_get_color_opt(&rev
.diffopt
, DIFF_COMMIT
),
805 diff_get_color_opt(&rev
.diffopt
, DIFF_RESET
));
806 read_tree(the_repository
, (struct tree
*)o
,
807 &match_all
, show_tree_object
,
813 struct object_array old
;
814 struct object_array blank
= OBJECT_ARRAY_INIT
;
816 memcpy(&old
, &rev
.pending
, sizeof(old
));
817 memcpy(&rev
.pending
, &blank
, sizeof(rev
.pending
));
819 add_object_array(o
, name
, &rev
.pending
);
820 ret
= cmd_log_walk_no_free(&rev
);
824 * object_array_clear(&pending). It was
825 * cleared already in prepare_revision_walk()
827 memcpy(&rev
.pending
, &old
, sizeof(rev
.pending
));
831 ret
= error(_("unknown type: %d"), o
->type
);
835 rev
.diffopt
.no_free
= 0;
836 diff_free(&rev
.diffopt
);
838 return cmd_log_deinit(ret
, &rev
);
842 * This is equivalent to "git log -g --abbrev-commit --pretty=oneline"
844 int cmd_log_reflog(int argc
, const char **argv
, const char *prefix
)
847 struct setup_revision_opt opt
;
850 git_config(git_log_config
, NULL
);
852 repo_init_revisions(the_repository
, &rev
, prefix
);
853 init_reflog_walk(&rev
.reflog_info
);
854 git_config(grep_config
, &rev
.grep_filter
);
856 rev
.verbose_header
= 1;
857 memset(&opt
, 0, sizeof(opt
));
859 cmd_log_init_defaults(&rev
);
860 rev
.abbrev_commit
= 1;
861 rev
.commit_format
= CMIT_FMT_ONELINE
;
862 rev
.use_terminator
= 1;
863 rev
.always_show_header
= 1;
864 cmd_log_init_finish(argc
, argv
, prefix
, &rev
, &opt
);
866 return cmd_log_deinit(cmd_log_walk(&rev
), &rev
);
869 static void log_setup_revisions_tweak(struct rev_info
*rev
)
871 if (rev
->diffopt
.flags
.default_follow_renames
&&
872 diff_check_follow_pathspec(&rev
->prune_data
, 0))
873 rev
->diffopt
.flags
.follow_renames
= 1;
875 if (rev
->first_parent_only
)
876 diff_merges_default_to_first_parent(rev
);
879 int cmd_log(int argc
, const char **argv
, const char *prefix
)
882 struct setup_revision_opt opt
;
885 git_config(git_log_config
, NULL
);
887 repo_init_revisions(the_repository
, &rev
, prefix
);
888 git_config(grep_config
, &rev
.grep_filter
);
890 rev
.always_show_header
= 1;
891 memset(&opt
, 0, sizeof(opt
));
893 opt
.revarg_opt
= REVARG_COMMITTISH
;
894 opt
.tweak
= log_setup_revisions_tweak
;
895 cmd_log_init(argc
, argv
, prefix
, &rev
, &opt
);
896 return cmd_log_deinit(cmd_log_walk(&rev
), &rev
);
901 static const char *fmt_patch_suffix
= ".patch";
902 static int numbered
= 0;
903 static int auto_number
= 1;
905 static char *default_attach
= NULL
;
907 static struct string_list extra_hdr
= STRING_LIST_INIT_NODUP
;
908 static struct string_list extra_to
= STRING_LIST_INIT_NODUP
;
909 static struct string_list extra_cc
= STRING_LIST_INIT_NODUP
;
911 static void add_header(const char *value
)
913 struct string_list_item
*item
;
914 int len
= strlen(value
);
915 while (len
&& value
[len
- 1] == '\n')
918 if (!strncasecmp(value
, "to: ", 4)) {
919 item
= string_list_append(&extra_to
, value
+ 4);
921 } else if (!strncasecmp(value
, "cc: ", 4)) {
922 item
= string_list_append(&extra_cc
, value
+ 4);
925 item
= string_list_append(&extra_hdr
, value
);
928 item
->string
[len
] = '\0';
944 enum cover_from_description
{
951 enum auto_base_setting
{
957 static enum thread_level thread
;
958 static int do_signoff
;
959 static enum auto_base_setting auto_base
;
961 static const char *signature
= git_version_string
;
962 static const char *signature_file
;
963 static enum cover_setting config_cover_letter
;
964 static const char *config_output_directory
;
965 static enum cover_from_description cover_from_description_mode
= COVER_FROM_MESSAGE
;
966 static int show_notes
;
967 static struct display_notes_opt notes_opt
;
969 static enum cover_from_description
parse_cover_from_description(const char *arg
)
971 if (!arg
|| !strcmp(arg
, "default"))
972 return COVER_FROM_MESSAGE
;
973 else if (!strcmp(arg
, "none"))
974 return COVER_FROM_NONE
;
975 else if (!strcmp(arg
, "message"))
976 return COVER_FROM_MESSAGE
;
977 else if (!strcmp(arg
, "subject"))
978 return COVER_FROM_SUBJECT
;
979 else if (!strcmp(arg
, "auto"))
980 return COVER_FROM_AUTO
;
982 die(_("%s: invalid cover from description mode"), arg
);
985 static int git_format_config(const char *var
, const char *value
,
986 const struct config_context
*ctx
, void *cb
)
988 if (!strcmp(var
, "format.headers")) {
990 die(_("format.headers without value"));
994 if (!strcmp(var
, "format.suffix"))
995 return git_config_string(&fmt_patch_suffix
, var
, value
);
996 if (!strcmp(var
, "format.to")) {
998 return config_error_nonbool(var
);
999 string_list_append(&extra_to
, value
);
1002 if (!strcmp(var
, "format.cc")) {
1004 return config_error_nonbool(var
);
1005 string_list_append(&extra_cc
, value
);
1008 if (!strcmp(var
, "diff.color") || !strcmp(var
, "color.diff") ||
1009 !strcmp(var
, "color.ui") || !strcmp(var
, "diff.submodule")) {
1012 if (!strcmp(var
, "format.numbered")) {
1013 if (value
&& !strcasecmp(value
, "auto")) {
1017 numbered
= git_config_bool(var
, value
);
1018 auto_number
= auto_number
&& numbered
;
1021 if (!strcmp(var
, "format.attach")) {
1022 if (value
&& *value
)
1023 default_attach
= xstrdup(value
);
1024 else if (value
&& !*value
)
1025 FREE_AND_NULL(default_attach
);
1027 default_attach
= xstrdup(git_version_string
);
1030 if (!strcmp(var
, "format.thread")) {
1031 if (value
&& !strcasecmp(value
, "deep")) {
1032 thread
= THREAD_DEEP
;
1035 if (value
&& !strcasecmp(value
, "shallow")) {
1036 thread
= THREAD_SHALLOW
;
1039 thread
= git_config_bool(var
, value
) ? THREAD_SHALLOW
: THREAD_UNSET
;
1042 if (!strcmp(var
, "format.signoff")) {
1043 do_signoff
= git_config_bool(var
, value
);
1046 if (!strcmp(var
, "format.signature"))
1047 return git_config_string(&signature
, var
, value
);
1048 if (!strcmp(var
, "format.signaturefile"))
1049 return git_config_pathname(&signature_file
, var
, value
);
1050 if (!strcmp(var
, "format.coverletter")) {
1051 if (value
&& !strcasecmp(value
, "auto")) {
1052 config_cover_letter
= COVER_AUTO
;
1055 config_cover_letter
= git_config_bool(var
, value
) ? COVER_ON
: COVER_OFF
;
1058 if (!strcmp(var
, "format.outputdirectory"))
1059 return git_config_string(&config_output_directory
, var
, value
);
1060 if (!strcmp(var
, "format.useautobase")) {
1061 if (value
&& !strcasecmp(value
, "whenAble")) {
1062 auto_base
= AUTO_BASE_WHEN_ABLE
;
1065 auto_base
= git_config_bool(var
, value
) ? AUTO_BASE_ALWAYS
: AUTO_BASE_NEVER
;
1068 if (!strcmp(var
, "format.from")) {
1069 int b
= git_parse_maybe_bool(value
);
1072 from
= xstrdup(value
);
1074 from
= xstrdup(git_committer_info(IDENT_NO_DATE
));
1079 if (!strcmp(var
, "format.forceinbodyfrom")) {
1080 force_in_body_from
= git_config_bool(var
, value
);
1083 if (!strcmp(var
, "format.notes")) {
1084 int b
= git_parse_maybe_bool(value
);
1086 enable_ref_display_notes(¬es_opt
, &show_notes
, value
);
1088 enable_default_display_notes(¬es_opt
, &show_notes
);
1090 disable_display_notes(¬es_opt
, &show_notes
);
1093 if (!strcmp(var
, "format.coverfromdescription")) {
1094 cover_from_description_mode
= parse_cover_from_description(value
);
1097 if (!strcmp(var
, "format.mboxrd")) {
1098 stdout_mboxrd
= git_config_bool(var
, value
);
1101 if (!strcmp(var
, "format.noprefix")) {
1102 format_no_prefix
= 1;
1107 * ignore some porcelain config which would otherwise be parsed by
1108 * git_diff_ui_config(), via git_log_config(); we can't just avoid
1109 * diff_ui_config completely, because we do care about some ui options
1112 if (!strcmp(var
, "diff.noprefix"))
1115 return git_log_config(var
, value
, ctx
, cb
);
1118 static const char *output_directory
= NULL
;
1119 static int outdir_offset
;
1121 static int open_next_file(struct commit
*commit
, const char *subject
,
1122 struct rev_info
*rev
, int quiet
)
1124 struct strbuf filename
= STRBUF_INIT
;
1126 if (output_directory
) {
1127 strbuf_addstr(&filename
, output_directory
);
1128 strbuf_complete(&filename
, '/');
1131 if (rev
->numbered_files
)
1132 strbuf_addf(&filename
, "%d", rev
->nr
);
1134 fmt_output_commit(&filename
, commit
, rev
);
1136 fmt_output_subject(&filename
, subject
, rev
);
1139 printf("%s\n", filename
.buf
+ outdir_offset
);
1141 if (!(rev
->diffopt
.file
= fopen(filename
.buf
, "w"))) {
1142 error_errno(_("cannot open patch file %s"), filename
.buf
);
1143 strbuf_release(&filename
);
1147 strbuf_release(&filename
);
1151 static void get_patch_ids(struct rev_info
*rev
, struct patch_ids
*ids
)
1153 struct rev_info check_rev
;
1154 struct commit
*commit
, *c1
, *c2
;
1155 struct object
*o1
, *o2
;
1156 unsigned flags1
, flags2
;
1158 if (rev
->pending
.nr
!= 2)
1159 die(_("need exactly one range"));
1161 o1
= rev
->pending
.objects
[0].item
;
1162 o2
= rev
->pending
.objects
[1].item
;
1165 c1
= lookup_commit_reference(the_repository
, &o1
->oid
);
1166 c2
= lookup_commit_reference(the_repository
, &o2
->oid
);
1168 if ((flags1
& UNINTERESTING
) == (flags2
& UNINTERESTING
))
1169 die(_("not a range"));
1171 init_patch_ids(the_repository
, ids
);
1173 /* given a range a..b get all patch ids for b..a */
1174 repo_init_revisions(the_repository
, &check_rev
, rev
->prefix
);
1175 check_rev
.max_parents
= 1;
1176 o1
->flags
^= UNINTERESTING
;
1177 o2
->flags
^= UNINTERESTING
;
1178 add_pending_object(&check_rev
, o1
, "o1");
1179 add_pending_object(&check_rev
, o2
, "o2");
1180 if (prepare_revision_walk(&check_rev
))
1181 die(_("revision walk setup failed"));
1183 while ((commit
= get_revision(&check_rev
)) != NULL
) {
1184 add_commit_patch_id(commit
, ids
);
1187 /* reset for next revision walk */
1188 clear_commit_marks(c1
, SEEN
| UNINTERESTING
| SHOWN
| ADDED
);
1189 clear_commit_marks(c2
, SEEN
| UNINTERESTING
| SHOWN
| ADDED
);
1194 static void gen_message_id(struct rev_info
*info
, char *base
)
1196 struct strbuf buf
= STRBUF_INIT
;
1197 strbuf_addf(&buf
, "%s.%"PRItime
".git.%s", base
,
1198 (timestamp_t
) time(NULL
),
1199 git_committer_info(IDENT_NO_NAME
|IDENT_NO_DATE
|IDENT_STRICT
));
1200 info
->message_id
= strbuf_detach(&buf
, NULL
);
1203 static void print_signature(FILE *file
)
1205 if (!signature
|| !*signature
)
1208 fprintf(file
, "-- \n%s", signature
);
1209 if (signature
[strlen(signature
)-1] != '\n')
1214 static char *find_branch_name(struct rev_info
*rev
)
1216 int i
, positive
= -1;
1217 struct object_id branch_oid
;
1218 const struct object_id
*tip_oid
;
1219 const char *ref
, *v
;
1220 char *full_ref
, *branch
= NULL
;
1222 for (i
= 0; i
< rev
->cmdline
.nr
; i
++) {
1223 if (rev
->cmdline
.rev
[i
].flags
& UNINTERESTING
)
1232 ref
= rev
->cmdline
.rev
[positive
].name
;
1233 tip_oid
= &rev
->cmdline
.rev
[positive
].item
->oid
;
1234 if (repo_dwim_ref(the_repository
, ref
, strlen(ref
), &branch_oid
,
1236 skip_prefix(full_ref
, "refs/heads/", &v
) &&
1237 oideq(tip_oid
, &branch_oid
))
1238 branch
= xstrdup(v
);
1243 static void show_diffstat(struct rev_info
*rev
,
1244 struct commit
*origin
, struct commit
*head
)
1246 struct diff_options opts
;
1248 memcpy(&opts
, &rev
->diffopt
, sizeof(opts
));
1249 opts
.output_format
= DIFF_FORMAT_SUMMARY
| DIFF_FORMAT_DIFFSTAT
;
1250 diff_setup_done(&opts
);
1252 diff_tree_oid(get_commit_tree_oid(origin
),
1253 get_commit_tree_oid(head
),
1255 diffcore_std(&opts
);
1258 fprintf(rev
->diffopt
.file
, "\n");
1261 static void read_desc_file(struct strbuf
*buf
, const char *desc_file
)
1263 if (strbuf_read_file(buf
, desc_file
, 0) < 0)
1264 die_errno(_("unable to read branch description file '%s'"),
1268 static void prepare_cover_text(struct pretty_print_context
*pp
,
1269 const char *description_file
,
1270 const char *branch_name
,
1272 const char *encoding
,
1275 const char *subject
= "*** SUBJECT HERE ***";
1276 const char *body
= "*** BLURB HERE ***";
1277 struct strbuf description_sb
= STRBUF_INIT
;
1278 struct strbuf subject_sb
= STRBUF_INIT
;
1280 if (cover_from_description_mode
== COVER_FROM_NONE
)
1283 if (description_file
&& *description_file
)
1284 read_desc_file(&description_sb
, description_file
);
1285 else if (branch_name
&& *branch_name
)
1286 read_branch_desc(&description_sb
, branch_name
);
1287 if (!description_sb
.len
)
1290 if (cover_from_description_mode
== COVER_FROM_SUBJECT
||
1291 cover_from_description_mode
== COVER_FROM_AUTO
)
1292 body
= format_subject(&subject_sb
, description_sb
.buf
, " ");
1294 if (cover_from_description_mode
== COVER_FROM_MESSAGE
||
1295 (cover_from_description_mode
== COVER_FROM_AUTO
&&
1296 subject_sb
.len
> COVER_FROM_AUTO_MAX_SUBJECT_LEN
))
1297 body
= description_sb
.buf
;
1299 subject
= subject_sb
.buf
;
1302 pp_title_line(pp
, &subject
, sb
, encoding
, need_8bit_cte
);
1303 pp_remainder(pp
, &body
, sb
, 0);
1305 strbuf_release(&description_sb
);
1306 strbuf_release(&subject_sb
);
1309 static int get_notes_refs(struct string_list_item
*item
, void *arg
)
1311 strvec_pushf(arg
, "--notes=%s", item
->string
);
1315 static void get_notes_args(struct strvec
*arg
, struct rev_info
*rev
)
1317 if (!rev
->show_notes
) {
1318 strvec_push(arg
, "--no-notes");
1319 } else if (rev
->notes_opt
.use_default_notes
> 0 ||
1320 (rev
->notes_opt
.use_default_notes
== -1 &&
1321 !rev
->notes_opt
.extra_notes_refs
.nr
)) {
1322 strvec_push(arg
, "--notes");
1324 for_each_string_list(&rev
->notes_opt
.extra_notes_refs
, get_notes_refs
, arg
);
1328 static void make_cover_letter(struct rev_info
*rev
, int use_separate_file
,
1329 struct commit
*origin
,
1330 int nr
, struct commit
**list
,
1331 const char *description_file
,
1332 const char *branch_name
,
1335 const char *committer
;
1336 struct shortlog log
;
1337 struct strbuf sb
= STRBUF_INIT
;
1339 const char *encoding
= "UTF-8";
1340 int need_8bit_cte
= 0;
1341 struct pretty_print_context pp
= {0};
1342 struct commit
*head
= list
[0];
1344 if (!cmit_fmt_is_mail(rev
->commit_format
))
1345 die(_("cover letter needs email format"));
1347 committer
= git_committer_info(0);
1349 if (use_separate_file
&&
1350 open_next_file(NULL
, rev
->numbered_files
? NULL
: "cover-letter", rev
, quiet
))
1351 die(_("failed to create cover-letter file"));
1353 log_write_email_headers(rev
, head
, &pp
.after_subject
, &need_8bit_cte
, 0);
1355 for (i
= 0; !need_8bit_cte
&& i
< nr
; i
++) {
1356 const char *buf
= repo_get_commit_buffer(the_repository
,
1358 if (has_non_ascii(buf
))
1360 repo_unuse_commit_buffer(the_repository
, list
[i
], buf
);
1364 branch_name
= find_branch_name(rev
);
1366 pp
.fmt
= CMIT_FMT_EMAIL
;
1367 pp
.date_mode
.type
= DATE_RFC2822
;
1369 pp
.print_email_subject
= 1;
1370 pp
.encode_email_headers
= rev
->encode_email_headers
;
1371 pp_user_info(&pp
, NULL
, &sb
, committer
, encoding
);
1372 prepare_cover_text(&pp
, description_file
, branch_name
, &sb
,
1373 encoding
, need_8bit_cte
);
1374 fprintf(rev
->diffopt
.file
, "%s\n", sb
.buf
);
1376 strbuf_release(&sb
);
1378 shortlog_init(&log
);
1380 log
.wrap
= MAIL_DEFAULT_WRAP
;
1383 log
.file
= rev
->diffopt
.file
;
1384 log
.groups
= SHORTLOG_GROUP_AUTHOR
;
1385 shortlog_finish_setup(&log
);
1386 for (i
= 0; i
< nr
; i
++)
1387 shortlog_add_commit(&log
, list
[i
]);
1389 shortlog_output(&log
);
1391 /* We can only do diffstat with a unique reference point */
1393 show_diffstat(rev
, origin
, head
);
1395 if (rev
->idiff_oid1
) {
1396 fprintf_ln(rev
->diffopt
.file
, "%s", rev
->idiff_title
);
1397 show_interdiff(rev
->idiff_oid1
, rev
->idiff_oid2
, 0,
1403 * Pass minimum required diff-options to range-diff; others
1404 * can be added later if deemed desirable.
1406 struct diff_options opts
;
1407 struct strvec other_arg
= STRVEC_INIT
;
1408 struct range_diff_options range_diff_opts
= {
1409 .creation_factor
= rev
->creation_factor
,
1412 .other_arg
= &other_arg
1415 repo_diff_setup(the_repository
, &opts
);
1416 opts
.file
= rev
->diffopt
.file
;
1417 opts
.use_color
= rev
->diffopt
.use_color
;
1418 diff_setup_done(&opts
);
1419 fprintf_ln(rev
->diffopt
.file
, "%s", rev
->rdiff_title
);
1420 get_notes_args(&other_arg
, rev
);
1421 show_range_diff(rev
->rdiff1
, rev
->rdiff2
, &range_diff_opts
);
1422 strvec_clear(&other_arg
);
1426 static char *clean_message_id(const char *msg_id
)
1429 const char *a
, *z
, *m
;
1432 while ((ch
= *m
) && (isspace(ch
) || (ch
== '<')))
1437 if (!isspace(ch
) && (ch
!= '>'))
1442 die(_("insane in-reply-to: %s"), msg_id
);
1445 return xmemdupz(a
, z
- a
);
1448 static const char *set_outdir(const char *prefix
, const char *output_directory
)
1450 if (output_directory
&& is_absolute_path(output_directory
))
1451 return output_directory
;
1453 if (!prefix
|| !*prefix
) {
1454 if (output_directory
)
1455 return output_directory
;
1456 /* The user did not explicitly ask for "./" */
1461 outdir_offset
= strlen(prefix
);
1462 if (!output_directory
)
1465 return prefix_filename(prefix
, output_directory
);
1468 static const char * const builtin_format_patch_usage
[] = {
1469 N_("git format-patch [<options>] [<since> | <revision-range>]"),
1473 static int keep_subject
= 0;
1475 static int keep_callback(const struct option
*opt
, const char *arg
, int unset
)
1477 BUG_ON_OPT_NEG(unset
);
1478 BUG_ON_OPT_ARG(arg
);
1479 ((struct rev_info
*)opt
->value
)->total
= -1;
1484 static int subject_prefix
= 0;
1486 static int subject_prefix_callback(const struct option
*opt
, const char *arg
,
1489 struct strbuf
*sprefix
;
1491 BUG_ON_OPT_NEG(unset
);
1492 sprefix
= opt
->value
;
1494 strbuf_reset(sprefix
);
1495 strbuf_addstr(sprefix
, arg
);
1499 static int numbered_cmdline_opt
= 0;
1501 static int numbered_callback(const struct option
*opt
, const char *arg
,
1504 BUG_ON_OPT_ARG(arg
);
1505 *(int *)opt
->value
= numbered_cmdline_opt
= unset
? 0 : 1;
1511 static int no_numbered_callback(const struct option
*opt
, const char *arg
,
1514 BUG_ON_OPT_NEG(unset
);
1515 return numbered_callback(opt
, arg
, 1);
1518 static int output_directory_callback(const struct option
*opt
, const char *arg
,
1521 const char **dir
= (const char **)opt
->value
;
1522 BUG_ON_OPT_NEG(unset
);
1524 die(_("two output directories?"));
1529 static int thread_callback(const struct option
*opt
, const char *arg
, int unset
)
1531 enum thread_level
*thread
= (enum thread_level
*)opt
->value
;
1533 *thread
= THREAD_UNSET
;
1534 else if (!arg
|| !strcmp(arg
, "shallow"))
1535 *thread
= THREAD_SHALLOW
;
1536 else if (!strcmp(arg
, "deep"))
1537 *thread
= THREAD_DEEP
;
1539 * Please update _git_formatpatch() in git-completion.bash
1540 * when you add new options.
1547 static int attach_callback(const struct option
*opt
, const char *arg
, int unset
)
1549 struct rev_info
*rev
= (struct rev_info
*)opt
->value
;
1551 rev
->mime_boundary
= NULL
;
1553 rev
->mime_boundary
= arg
;
1555 rev
->mime_boundary
= git_version_string
;
1556 rev
->no_inline
= unset
? 0 : 1;
1560 static int inline_callback(const struct option
*opt
, const char *arg
, int unset
)
1562 struct rev_info
*rev
= (struct rev_info
*)opt
->value
;
1564 rev
->mime_boundary
= NULL
;
1566 rev
->mime_boundary
= arg
;
1568 rev
->mime_boundary
= git_version_string
;
1573 static int header_callback(const struct option
*opt UNUSED
, const char *arg
,
1577 string_list_clear(&extra_hdr
, 0);
1578 string_list_clear(&extra_to
, 0);
1579 string_list_clear(&extra_cc
, 0);
1586 static int from_callback(const struct option
*opt
, const char *arg
, int unset
)
1588 char **from
= opt
->value
;
1595 *from
= xstrdup(arg
);
1597 *from
= xstrdup(git_committer_info(IDENT_NO_DATE
));
1601 static int base_callback(const struct option
*opt
, const char *arg
, int unset
)
1603 const char **base_commit
= opt
->value
;
1606 auto_base
= AUTO_BASE_NEVER
;
1607 *base_commit
= NULL
;
1608 } else if (!strcmp(arg
, "auto")) {
1609 auto_base
= AUTO_BASE_ALWAYS
;
1610 *base_commit
= NULL
;
1612 auto_base
= AUTO_BASE_NEVER
;
1618 struct base_tree_info
{
1619 struct object_id base_commit
;
1620 int nr_patch_id
, alloc_patch_id
;
1621 struct object_id
*patch_id
;
1624 static struct commit
*get_base_commit(const char *base_commit
,
1625 struct commit
**list
,
1628 struct commit
*base
= NULL
;
1629 struct commit
**rev
;
1630 int i
= 0, rev_nr
= 0, auto_select
, die_on_failure
;
1632 switch (auto_base
) {
1633 case AUTO_BASE_NEVER
:
1638 /* no base information is requested */
1642 case AUTO_BASE_ALWAYS
:
1643 case AUTO_BASE_WHEN_ABLE
:
1645 BUG("requested automatic base selection but a commit was provided");
1648 die_on_failure
= auto_base
== AUTO_BASE_ALWAYS
;
1652 BUG("unexpected automatic base selection method");
1656 base
= lookup_commit_reference_by_name(base_commit
);
1658 die(_("unknown commit %s"), base_commit
);
1660 struct branch
*curr_branch
= branch_get(NULL
);
1661 const char *upstream
= branch_get_upstream(curr_branch
, NULL
);
1663 struct commit_list
*base_list
;
1664 struct commit
*commit
;
1665 struct object_id oid
;
1667 if (repo_get_oid(the_repository
, upstream
, &oid
)) {
1669 die(_("failed to resolve '%s' as a valid ref"), upstream
);
1673 commit
= lookup_commit_or_die(&oid
, "upstream base");
1674 base_list
= repo_get_merge_bases_many(the_repository
,
1677 /* There should be one and only one merge base. */
1678 if (!base_list
|| base_list
->next
) {
1679 if (die_on_failure
) {
1680 die(_("could not find exact merge base"));
1682 free_commit_list(base_list
);
1686 base
= base_list
->item
;
1687 free_commit_list(base_list
);
1690 die(_("failed to get upstream, if you want to record base commit automatically,\n"
1691 "please use git branch --set-upstream-to to track a remote branch.\n"
1692 "Or you could specify base commit by --base=<base-commit-id> manually"));
1698 ALLOC_ARRAY(rev
, total
);
1699 for (i
= 0; i
< total
; i
++)
1704 * Get merge base through pair-wise computations
1705 * and store it in rev[0].
1707 while (rev_nr
> 1) {
1708 for (i
= 0; i
< rev_nr
/ 2; i
++) {
1709 struct commit_list
*merge_base
;
1710 merge_base
= repo_get_merge_bases(the_repository
,
1713 if (!merge_base
|| merge_base
->next
) {
1714 if (die_on_failure
) {
1715 die(_("failed to find exact merge base"));
1722 rev
[i
] = merge_base
->item
;
1726 rev
[i
] = rev
[2 * i
];
1727 rev_nr
= DIV_ROUND_UP(rev_nr
, 2);
1730 if (!repo_in_merge_bases(the_repository
, base
, rev
[0])) {
1731 if (die_on_failure
) {
1732 die(_("base commit should be the ancestor of revision list"));
1739 for (i
= 0; i
< total
; i
++) {
1740 if (base
== list
[i
]) {
1741 if (die_on_failure
) {
1742 die(_("base commit shouldn't be in revision list"));
1754 define_commit_slab(commit_base
, int);
1756 static void prepare_bases(struct base_tree_info
*bases
,
1757 struct commit
*base
,
1758 struct commit
**list
,
1761 struct commit
*commit
;
1762 struct rev_info revs
;
1763 struct diff_options diffopt
;
1764 struct commit_base commit_base
;
1770 init_commit_base(&commit_base
);
1771 repo_diff_setup(the_repository
, &diffopt
);
1772 diffopt
.flags
.recursive
= 1;
1773 diff_setup_done(&diffopt
);
1775 oidcpy(&bases
->base_commit
, &base
->object
.oid
);
1777 repo_init_revisions(the_repository
, &revs
, NULL
);
1778 revs
.max_parents
= 1;
1779 revs
.topo_order
= 1;
1780 for (i
= 0; i
< total
; i
++) {
1781 list
[i
]->object
.flags
&= ~UNINTERESTING
;
1782 add_pending_object(&revs
, &list
[i
]->object
, "rev_list");
1783 *commit_base_at(&commit_base
, list
[i
]) = 1;
1785 base
->object
.flags
|= UNINTERESTING
;
1786 add_pending_object(&revs
, &base
->object
, "base");
1788 if (prepare_revision_walk(&revs
))
1789 die(_("revision walk setup failed"));
1791 * Traverse the commits list, get prerequisite patch ids
1792 * and stuff them in bases structure.
1794 while ((commit
= get_revision(&revs
)) != NULL
) {
1795 struct object_id oid
;
1796 struct object_id
*patch_id
;
1797 if (*commit_base_at(&commit_base
, commit
))
1799 if (commit_patch_id(commit
, &diffopt
, &oid
, 0))
1800 die(_("cannot get patch id"));
1801 ALLOC_GROW(bases
->patch_id
, bases
->nr_patch_id
+ 1, bases
->alloc_patch_id
);
1802 patch_id
= bases
->patch_id
+ bases
->nr_patch_id
;
1803 oidcpy(patch_id
, &oid
);
1804 bases
->nr_patch_id
++;
1806 clear_commit_base(&commit_base
);
1809 static void print_bases(struct base_tree_info
*bases
, FILE *file
)
1813 /* Only do this once, either for the cover or for the first one */
1814 if (is_null_oid(&bases
->base_commit
))
1817 /* Show the base commit */
1818 fprintf(file
, "\nbase-commit: %s\n", oid_to_hex(&bases
->base_commit
));
1820 /* Show the prerequisite patches */
1821 for (i
= bases
->nr_patch_id
- 1; i
>= 0; i
--)
1822 fprintf(file
, "prerequisite-patch-id: %s\n", oid_to_hex(&bases
->patch_id
[i
]));
1824 free(bases
->patch_id
);
1825 bases
->nr_patch_id
= 0;
1826 bases
->alloc_patch_id
= 0;
1827 oidclr(&bases
->base_commit
);
1830 static const char *diff_title(struct strbuf
*sb
,
1831 const char *reroll_count
,
1832 const char *generic
,
1833 const char *rerolled
)
1837 /* RFC may be v0, so allow -v1 to diff against v0 */
1838 if (reroll_count
&& !strtol_i(reroll_count
, 10, &v
) &&
1840 strbuf_addf(sb
, rerolled
, v
- 1);
1842 strbuf_addstr(sb
, generic
);
1846 static void infer_range_diff_ranges(struct strbuf
*r1
,
1849 struct commit
*origin
,
1850 struct commit
*head
)
1852 const char *head_oid
= oid_to_hex(&head
->object
.oid
);
1853 int prev_is_range
= is_range_diff_range(prev
);
1856 strbuf_addstr(r1
, prev
);
1858 strbuf_addf(r1
, "%s..%s", head_oid
, prev
);
1861 strbuf_addf(r2
, "%s..%s", oid_to_hex(&origin
->object
.oid
), head_oid
);
1862 else if (prev_is_range
)
1863 die(_("failed to infer range-diff origin of current series"));
1865 warning(_("using '%s' as range-diff origin of current series"), prev
);
1866 strbuf_addf(r2
, "%s..%s", prev
, head_oid
);
1870 int cmd_format_patch(int argc
, const char **argv
, const char *prefix
)
1872 struct commit
*commit
;
1873 struct commit
**list
= NULL
;
1874 struct rev_info rev
;
1875 char *to_free
= NULL
;
1876 struct setup_revision_opt s_r_opt
;
1877 int nr
= 0, total
, i
;
1879 int start_number
= -1;
1880 int just_numbers
= 0;
1881 int ignore_if_in_upstream
= 0;
1882 int cover_letter
= -1;
1883 int boundary_count
= 0;
1884 int no_binary_diff
= 0;
1885 int zero_commit
= 0;
1886 struct commit
*origin
= NULL
;
1887 const char *in_reply_to
= NULL
;
1888 struct patch_ids ids
;
1889 struct strbuf buf
= STRBUF_INIT
;
1890 int use_patch_format
= 0;
1892 const char *reroll_count
= NULL
;
1893 char *cover_from_description_arg
= NULL
;
1894 char *description_file
= NULL
;
1895 char *branch_name
= NULL
;
1896 char *base_commit
= NULL
;
1897 struct base_tree_info bases
;
1898 struct commit
*base
;
1899 int show_progress
= 0;
1900 struct progress
*progress
= NULL
;
1901 struct oid_array idiff_prev
= OID_ARRAY_INIT
;
1902 struct strbuf idiff_title
= STRBUF_INIT
;
1903 const char *rdiff_prev
= NULL
;
1904 struct strbuf rdiff1
= STRBUF_INIT
;
1905 struct strbuf rdiff2
= STRBUF_INIT
;
1906 struct strbuf rdiff_title
= STRBUF_INIT
;
1907 struct strbuf sprefix
= STRBUF_INIT
;
1908 int creation_factor
= -1;
1911 const struct option builtin_format_patch_options
[] = {
1912 OPT_CALLBACK_F('n', "numbered", &numbered
, NULL
,
1913 N_("use [PATCH n/m] even with a single patch"),
1914 PARSE_OPT_NOARG
, numbered_callback
),
1915 OPT_CALLBACK_F('N', "no-numbered", &numbered
, NULL
,
1916 N_("use [PATCH] even with multiple patches"),
1917 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
, no_numbered_callback
),
1918 OPT_BOOL('s', "signoff", &do_signoff
, N_("add a Signed-off-by trailer")),
1919 OPT_BOOL(0, "stdout", &use_stdout
,
1920 N_("print patches to standard out")),
1921 OPT_BOOL(0, "cover-letter", &cover_letter
,
1922 N_("generate a cover letter")),
1923 OPT_BOOL(0, "numbered-files", &just_numbers
,
1924 N_("use simple number sequence for output file names")),
1925 OPT_STRING(0, "suffix", &fmt_patch_suffix
, N_("sfx"),
1926 N_("use <sfx> instead of '.patch'")),
1927 OPT_INTEGER(0, "start-number", &start_number
,
1928 N_("start numbering patches at <n> instead of 1")),
1929 OPT_STRING('v', "reroll-count", &reroll_count
, N_("reroll-count"),
1930 N_("mark the series as Nth re-roll")),
1931 OPT_INTEGER(0, "filename-max-length", &fmt_patch_name_max
,
1932 N_("max length of output filename")),
1933 OPT_BOOL(0, "rfc", &rfc
, N_("use [RFC PATCH] instead of [PATCH]")),
1934 OPT_STRING(0, "cover-from-description", &cover_from_description_arg
,
1935 N_("cover-from-description-mode"),
1936 N_("generate parts of a cover letter based on a branch's description")),
1937 OPT_FILENAME(0, "description-file", &description_file
,
1938 N_("use branch description from file")),
1939 OPT_CALLBACK_F(0, "subject-prefix", &sprefix
, N_("prefix"),
1940 N_("use [<prefix>] instead of [PATCH]"),
1941 PARSE_OPT_NONEG
, subject_prefix_callback
),
1942 OPT_CALLBACK_F('o', "output-directory", &output_directory
,
1943 N_("dir"), N_("store resulting files in <dir>"),
1944 PARSE_OPT_NONEG
, output_directory_callback
),
1945 OPT_CALLBACK_F('k', "keep-subject", &rev
, NULL
,
1946 N_("don't strip/add [PATCH]"),
1947 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
, keep_callback
),
1948 OPT_BOOL(0, "no-binary", &no_binary_diff
,
1949 N_("don't output binary diffs")),
1950 OPT_BOOL(0, "zero-commit", &zero_commit
,
1951 N_("output all-zero hash in From header")),
1952 OPT_BOOL(0, "ignore-if-in-upstream", &ignore_if_in_upstream
,
1953 N_("don't include a patch matching a commit upstream")),
1954 OPT_SET_INT_F('p', "no-stat", &use_patch_format
,
1955 N_("show patch format instead of default (patch + stat)"),
1956 1, PARSE_OPT_NONEG
),
1957 OPT_GROUP(N_("Messaging")),
1958 OPT_CALLBACK(0, "add-header", NULL
, N_("header"),
1959 N_("add email header"), header_callback
),
1960 OPT_STRING_LIST(0, "to", &extra_to
, N_("email"), N_("add To: header")),
1961 OPT_STRING_LIST(0, "cc", &extra_cc
, N_("email"), N_("add Cc: header")),
1962 OPT_CALLBACK_F(0, "from", &from
, N_("ident"),
1963 N_("set From address to <ident> (or committer ident if absent)"),
1964 PARSE_OPT_OPTARG
, from_callback
),
1965 OPT_STRING(0, "in-reply-to", &in_reply_to
, N_("message-id"),
1966 N_("make first mail a reply to <message-id>")),
1967 OPT_CALLBACK_F(0, "attach", &rev
, N_("boundary"),
1968 N_("attach the patch"), PARSE_OPT_OPTARG
,
1970 OPT_CALLBACK_F(0, "inline", &rev
, N_("boundary"),
1971 N_("inline the patch"),
1972 PARSE_OPT_OPTARG
| PARSE_OPT_NONEG
,
1974 OPT_CALLBACK_F(0, "thread", &thread
, N_("style"),
1975 N_("enable message threading, styles: shallow, deep"),
1976 PARSE_OPT_OPTARG
, thread_callback
),
1977 OPT_STRING(0, "signature", &signature
, N_("signature"),
1978 N_("add a signature")),
1979 OPT_CALLBACK_F(0, "base", &base_commit
, N_("base-commit"),
1980 N_("add prerequisite tree info to the patch series"),
1982 OPT_FILENAME(0, "signature-file", &signature_file
,
1983 N_("add a signature from a file")),
1984 OPT__QUIET(&quiet
, N_("don't print the patch filenames")),
1985 OPT_BOOL(0, "progress", &show_progress
,
1986 N_("show progress while generating patches")),
1987 OPT_CALLBACK(0, "interdiff", &idiff_prev
, N_("rev"),
1988 N_("show changes against <rev> in cover letter or single patch"),
1989 parse_opt_object_name
),
1990 OPT_STRING(0, "range-diff", &rdiff_prev
, N_("refspec"),
1991 N_("show changes against <refspec> in cover letter or single patch")),
1992 OPT_INTEGER(0, "creation-factor", &creation_factor
,
1993 N_("percentage by which creation is weighted")),
1994 OPT_BOOL(0, "force-in-body-from", &force_in_body_from
,
1995 N_("show in-body From: even if identical to the e-mail header")),
1999 extra_hdr
.strdup_strings
= 1;
2000 extra_to
.strdup_strings
= 1;
2001 extra_cc
.strdup_strings
= 1;
2003 init_log_defaults();
2004 init_display_notes(¬es_opt
);
2005 git_config(git_format_config
, NULL
);
2006 repo_init_revisions(the_repository
, &rev
, prefix
);
2007 git_config(grep_config
, &rev
.grep_filter
);
2009 rev
.show_notes
= show_notes
;
2010 memcpy(&rev
.notes_opt
, ¬es_opt
, sizeof(notes_opt
));
2011 rev
.commit_format
= CMIT_FMT_EMAIL
;
2012 rev
.encode_email_headers
= default_encode_email_headers
;
2013 rev
.expand_tabs_in_log_default
= 0;
2014 rev
.verbose_header
= 1;
2016 rev
.max_parents
= 1;
2017 rev
.diffopt
.flags
.recursive
= 1;
2018 rev
.diffopt
.no_free
= 1;
2019 memset(&s_r_opt
, 0, sizeof(s_r_opt
));
2020 s_r_opt
.def
= "HEAD";
2021 s_r_opt
.revarg_opt
= REVARG_COMMITTISH
;
2023 strbuf_addstr(&sprefix
, fmt_patch_subject_prefix
);
2024 if (format_no_prefix
)
2025 diff_set_noprefix(&rev
.diffopt
);
2027 if (default_attach
) {
2028 rev
.mime_boundary
= default_attach
;
2033 * Parse the arguments before setup_revisions(), or something
2034 * like "git format-patch -o a123 HEAD^.." may fail; a123 is
2035 * possibly a valid SHA1.
2037 argc
= parse_options(argc
, argv
, prefix
, builtin_format_patch_options
,
2038 builtin_format_patch_usage
,
2039 PARSE_OPT_KEEP_ARGV0
| PARSE_OPT_KEEP_UNKNOWN_OPT
|
2040 PARSE_OPT_KEEP_DASHDASH
);
2042 rev
.force_in_body_from
= force_in_body_from
;
2044 /* Make sure "0000-$sub.patch" gives non-negative length for $sub */
2045 if (fmt_patch_name_max
<= strlen("0000-") + strlen(fmt_patch_suffix
))
2046 fmt_patch_name_max
= strlen("0000-") + strlen(fmt_patch_suffix
);
2048 if (cover_from_description_arg
)
2049 cover_from_description_mode
= parse_cover_from_description(cover_from_description_arg
);
2052 strbuf_insertstr(&sprefix
, 0, "RFC ");
2055 strbuf_addf(&sprefix
, " v%s", reroll_count
);
2056 rev
.reroll_count
= reroll_count
;
2059 rev
.subject_prefix
= sprefix
.buf
;
2061 for (i
= 0; i
< extra_hdr
.nr
; i
++) {
2062 strbuf_addstr(&buf
, extra_hdr
.items
[i
].string
);
2063 strbuf_addch(&buf
, '\n');
2067 strbuf_addstr(&buf
, "To: ");
2068 for (i
= 0; i
< extra_to
.nr
; i
++) {
2070 strbuf_addstr(&buf
, " ");
2071 strbuf_addstr(&buf
, extra_to
.items
[i
].string
);
2072 if (i
+ 1 < extra_to
.nr
)
2073 strbuf_addch(&buf
, ',');
2074 strbuf_addch(&buf
, '\n');
2078 strbuf_addstr(&buf
, "Cc: ");
2079 for (i
= 0; i
< extra_cc
.nr
; i
++) {
2081 strbuf_addstr(&buf
, " ");
2082 strbuf_addstr(&buf
, extra_cc
.items
[i
].string
);
2083 if (i
+ 1 < extra_cc
.nr
)
2084 strbuf_addch(&buf
, ',');
2085 strbuf_addch(&buf
, '\n');
2088 rev
.extra_headers
= to_free
= strbuf_detach(&buf
, NULL
);
2091 if (split_ident_line(&rev
.from_ident
, from
, strlen(from
)))
2092 die(_("invalid ident line: %s"), from
);
2095 if (start_number
< 0)
2099 * If numbered is set solely due to format.numbered in config,
2100 * and it would conflict with --keep-subject (-k) from the
2101 * command line, reset "numbered".
2103 if (numbered
&& keep_subject
&& !numbered_cmdline_opt
)
2106 if (numbered
&& keep_subject
)
2107 die(_("options '%s' and '%s' cannot be used together"), "-n", "-k");
2108 if (keep_subject
&& subject_prefix
)
2109 die(_("options '%s' and '%s' cannot be used together"), "--subject-prefix/--rfc", "-k");
2110 rev
.preserve_subject
= keep_subject
;
2112 argc
= setup_revisions(argc
, argv
, &rev
, &s_r_opt
);
2114 die(_("unrecognized argument: %s"), argv
[1]);
2116 if (rev
.diffopt
.output_format
& DIFF_FORMAT_NAME
)
2117 die(_("--name-only does not make sense"));
2118 if (rev
.diffopt
.output_format
& DIFF_FORMAT_NAME_STATUS
)
2119 die(_("--name-status does not make sense"));
2120 if (rev
.diffopt
.output_format
& DIFF_FORMAT_CHECKDIFF
)
2121 die(_("--check does not make sense"));
2122 if (rev
.remerge_diff
)
2123 die(_("--remerge-diff does not make sense"));
2125 if (!use_patch_format
&&
2126 (!rev
.diffopt
.output_format
||
2127 rev
.diffopt
.output_format
== DIFF_FORMAT_PATCH
))
2128 rev
.diffopt
.output_format
= DIFF_FORMAT_DIFFSTAT
| DIFF_FORMAT_SUMMARY
;
2129 if (!rev
.diffopt
.stat_width
)
2130 rev
.diffopt
.stat_width
= MAIL_DEFAULT_WRAP
;
2132 /* Always generate a patch */
2133 rev
.diffopt
.output_format
|= DIFF_FORMAT_PATCH
;
2134 rev
.always_show_header
= 1;
2136 rev
.zero_commit
= zero_commit
;
2137 rev
.patch_name_max
= fmt_patch_name_max
;
2139 if (!rev
.diffopt
.flags
.text
&& !no_binary_diff
)
2140 rev
.diffopt
.flags
.binary
= 1;
2143 load_display_notes(&rev
.notes_opt
);
2145 die_for_incompatible_opt3(use_stdout
, "--stdout",
2146 rev
.diffopt
.close_file
, "--output",
2147 !!output_directory
, "--output-directory");
2149 if (use_stdout
&& stdout_mboxrd
)
2150 rev
.commit_format
= CMIT_FMT_MBOXRD
;
2154 } else if (!rev
.diffopt
.close_file
) {
2157 if (!output_directory
)
2158 output_directory
= config_output_directory
;
2159 output_directory
= set_outdir(prefix
, output_directory
);
2161 if (rev
.diffopt
.use_color
!= GIT_COLOR_ALWAYS
)
2162 rev
.diffopt
.use_color
= GIT_COLOR_NEVER
;
2164 * We consider <outdir> as 'outside of gitdir', therefore avoid
2165 * applying adjust_shared_perm in s-c-l-d.
2167 saved
= get_shared_repository();
2168 set_shared_repository(0);
2169 switch (safe_create_leading_directories_const(output_directory
)) {
2174 die(_("could not create leading directories "
2175 "of '%s'"), output_directory
);
2177 set_shared_repository(saved
);
2178 if (mkdir(output_directory
, 0777) < 0 && errno
!= EEXIST
)
2179 die_errno(_("could not create directory '%s'"),
2183 if (rev
.pending
.nr
== 1) {
2186 if (rev
.max_count
< 0 && !rev
.show_root_diff
) {
2188 * This is traditional behaviour of "git format-patch
2189 * origin" that prepares what the origin side still
2192 rev
.pending
.objects
[0].item
->flags
|= UNINTERESTING
;
2193 add_head_to_pending(&rev
);
2197 * Otherwise, it is "format-patch -22 HEAD", and/or
2198 * "format-patch --root HEAD". The user wants
2199 * get_revision() to do the usual traversal.
2202 if (!strcmp(rev
.pending
.objects
[0].name
, "HEAD"))
2206 const char *ref
, *v
;
2207 ref
= resolve_ref_unsafe("HEAD", RESOLVE_REF_READING
,
2209 if (ref
&& skip_prefix(ref
, "refs/heads/", &v
))
2210 branch_name
= xstrdup(v
);
2212 branch_name
= xstrdup(""); /* no branch */
2217 * We cannot move this anywhere earlier because we do want to
2218 * know if --root was given explicitly from the command line.
2220 rev
.show_root_diff
= 1;
2222 if (ignore_if_in_upstream
) {
2223 /* Don't say anything if head and upstream are the same. */
2224 if (rev
.pending
.nr
== 2) {
2225 struct object_array_entry
*o
= rev
.pending
.objects
;
2226 if (oideq(&o
[0].item
->oid
, &o
[1].item
->oid
))
2229 get_patch_ids(&rev
, &ids
);
2232 if (prepare_revision_walk(&rev
))
2233 die(_("revision walk setup failed"));
2235 while ((commit
= get_revision(&rev
)) != NULL
) {
2236 if (commit
->object
.flags
& BOUNDARY
) {
2238 origin
= (boundary_count
== 1) ? commit
: NULL
;
2242 if (ignore_if_in_upstream
&& has_commit_patch_id(commit
, &ids
))
2246 REALLOC_ARRAY(list
, nr
);
2247 list
[nr
- 1] = commit
;
2253 if (cover_letter
== -1) {
2254 if (config_cover_letter
== COVER_AUTO
)
2255 cover_letter
= (total
> 1);
2257 cover_letter
= (config_cover_letter
== COVER_ON
);
2259 if (!keep_subject
&& auto_number
&& (total
> 1 || cover_letter
))
2262 rev
.total
= total
+ start_number
- 1;
2264 if (idiff_prev
.nr
) {
2265 if (!cover_letter
&& total
!= 1)
2266 die(_("--interdiff requires --cover-letter or single patch"));
2267 rev
.idiff_oid1
= &idiff_prev
.oid
[idiff_prev
.nr
- 1];
2268 rev
.idiff_oid2
= get_commit_tree_oid(list
[0]);
2269 rev
.idiff_title
= diff_title(&idiff_title
, reroll_count
,
2271 _("Interdiff against v%d:"));
2274 if (creation_factor
< 0)
2275 creation_factor
= RANGE_DIFF_CREATION_FACTOR_DEFAULT
;
2276 else if (!rdiff_prev
)
2277 die(_("the option '%s' requires '%s'"), "--creation-factor", "--range-diff");
2280 if (!cover_letter
&& total
!= 1)
2281 die(_("--range-diff requires --cover-letter or single patch"));
2283 infer_range_diff_ranges(&rdiff1
, &rdiff2
, rdiff_prev
,
2285 rev
.rdiff1
= rdiff1
.buf
;
2286 rev
.rdiff2
= rdiff2
.buf
;
2287 rev
.creation_factor
= creation_factor
;
2288 rev
.rdiff_title
= diff_title(&rdiff_title
, reroll_count
,
2290 _("Range-diff against v%d:"));
2294 ; /* --no-signature inhibits all signatures */
2295 } else if (signature
&& signature
!= git_version_string
) {
2296 ; /* non-default signature already set */
2297 } else if (signature_file
) {
2298 struct strbuf buf
= STRBUF_INIT
;
2300 if (strbuf_read_file(&buf
, signature_file
, 128) < 0)
2301 die_errno(_("unable to read signature file '%s'"), signature_file
);
2302 signature
= strbuf_detach(&buf
, NULL
);
2305 memset(&bases
, 0, sizeof(bases
));
2306 base
= get_base_commit(base_commit
, list
, nr
);
2308 reset_revision_walk();
2309 clear_object_flags(UNINTERESTING
);
2310 prepare_bases(&bases
, base
, list
, nr
);
2313 if (in_reply_to
|| thread
|| cover_letter
) {
2314 rev
.ref_message_ids
= xmalloc(sizeof(*rev
.ref_message_ids
));
2315 string_list_init_dup(rev
.ref_message_ids
);
2318 char *msgid
= clean_message_id(in_reply_to
);
2319 string_list_append_nodup(rev
.ref_message_ids
, msgid
);
2321 rev
.numbered_files
= just_numbers
;
2322 rev
.patch_suffix
= fmt_patch_suffix
;
2325 gen_message_id(&rev
, "cover");
2326 make_cover_letter(&rev
, !!output_directory
,
2327 origin
, nr
, list
, description_file
, branch_name
, quiet
);
2328 print_bases(&bases
, rev
.diffopt
.file
);
2329 print_signature(rev
.diffopt
.file
);
2332 /* interdiff/range-diff in cover-letter; omit from patches */
2333 rev
.idiff_oid1
= NULL
;
2336 rev
.add_signoff
= do_signoff
;
2339 progress
= start_delayed_progress(_("Generating patches"), total
);
2342 display_progress(progress
, total
- nr
);
2344 rev
.nr
= total
- nr
+ (start_number
- 1);
2345 /* Make the second and subsequent mails replies to the first */
2347 /* Have we already had a message ID? */
2348 if (rev
.message_id
) {
2350 * For deep threading: make every mail
2351 * a reply to the previous one, no
2352 * matter what other options are set.
2354 * For shallow threading:
2356 * Without --cover-letter and
2357 * --in-reply-to, make every mail a
2358 * reply to the one before.
2360 * With --in-reply-to but no
2361 * --cover-letter, make every mail a
2362 * reply to the <reply-to>.
2364 * With --cover-letter, make every
2365 * mail but the cover letter a reply
2366 * to the cover letter. The cover
2367 * letter is a reply to the
2368 * --in-reply-to, if specified.
2370 if (thread
== THREAD_SHALLOW
2371 && rev
.ref_message_ids
->nr
> 0
2372 && (!cover_letter
|| rev
.nr
> 1))
2373 free(rev
.message_id
);
2375 string_list_append_nodup(rev
.ref_message_ids
,
2378 gen_message_id(&rev
, oid_to_hex(&commit
->object
.oid
));
2381 if (output_directory
&&
2382 open_next_file(rev
.numbered_files
? NULL
: commit
, NULL
, &rev
, quiet
))
2383 die(_("failed to create output files"));
2384 shown
= log_tree_commit(&rev
, commit
);
2385 free_commit_buffer(the_repository
->parsed_objects
,
2388 /* We put one extra blank line between formatted
2389 * patches and this flag is used by log-tree code
2390 * to see if it needs to emit a LF before showing
2391 * the log; when using one file per patch, we do
2392 * not want the extra blank line.
2394 if (output_directory
)
2397 print_bases(&bases
, rev
.diffopt
.file
);
2398 if (rev
.mime_boundary
)
2399 fprintf(rev
.diffopt
.file
, "\n--%s%s--\n\n\n",
2400 mime_boundary_leader
,
2403 print_signature(rev
.diffopt
.file
);
2405 if (output_directory
)
2406 fclose(rev
.diffopt
.file
);
2408 stop_progress(&progress
);
2411 string_list_clear(&extra_to
, 0);
2412 string_list_clear(&extra_cc
, 0);
2413 string_list_clear(&extra_hdr
, 0);
2414 if (ignore_if_in_upstream
)
2415 free_patch_ids(&ids
);
2418 oid_array_clear(&idiff_prev
);
2419 strbuf_release(&idiff_title
);
2420 strbuf_release(&rdiff1
);
2421 strbuf_release(&rdiff2
);
2422 strbuf_release(&rdiff_title
);
2423 strbuf_release(&sprefix
);
2425 free(rev
.message_id
);
2426 if (rev
.ref_message_ids
)
2427 string_list_clear(rev
.ref_message_ids
, 0);
2428 free(rev
.ref_message_ids
);
2429 return cmd_log_deinit(0, &rev
);
2432 static int add_pending_commit(const char *arg
, struct rev_info
*revs
, int flags
)
2434 struct object_id oid
;
2435 if (repo_get_oid(the_repository
, arg
, &oid
) == 0) {
2436 struct commit
*commit
= lookup_commit_reference(the_repository
,
2439 commit
->object
.flags
|= flags
;
2440 add_pending_object(revs
, &commit
->object
, arg
);
2447 static const char * const cherry_usage
[] = {
2448 N_("git cherry [-v] [<upstream> [<head> [<limit>]]]"),
2452 static void print_commit(char sign
, struct commit
*commit
, int verbose
,
2453 int abbrev
, FILE *file
)
2456 fprintf(file
, "%c %s\n", sign
,
2457 repo_find_unique_abbrev(the_repository
, &commit
->object
.oid
, abbrev
));
2459 struct strbuf buf
= STRBUF_INIT
;
2460 pp_commit_easy(CMIT_FMT_ONELINE
, commit
, &buf
);
2461 fprintf(file
, "%c %s %s\n", sign
,
2462 repo_find_unique_abbrev(the_repository
, &commit
->object
.oid
, abbrev
),
2464 strbuf_release(&buf
);
2468 int cmd_cherry(int argc
, const char **argv
, const char *prefix
)
2470 struct rev_info revs
;
2471 struct patch_ids ids
;
2472 struct commit
*commit
;
2473 struct commit_list
*list
= NULL
;
2474 struct branch
*current_branch
;
2475 const char *upstream
;
2476 const char *head
= "HEAD";
2477 const char *limit
= NULL
;
2478 int verbose
= 0, abbrev
= 0;
2480 struct option options
[] = {
2481 OPT__ABBREV(&abbrev
),
2482 OPT__VERBOSE(&verbose
, N_("be verbose")),
2486 argc
= parse_options(argc
, argv
, prefix
, options
, cherry_usage
, 0);
2499 current_branch
= branch_get(NULL
);
2500 upstream
= branch_get_upstream(current_branch
, NULL
);
2502 fprintf(stderr
, _("Could not find a tracked"
2503 " remote branch, please"
2504 " specify <upstream> manually.\n"));
2505 usage_with_options(cherry_usage
, options
);
2509 repo_init_revisions(the_repository
, &revs
, prefix
);
2510 revs
.max_parents
= 1;
2512 if (add_pending_commit(head
, &revs
, 0))
2513 die(_("unknown commit %s"), head
);
2514 if (add_pending_commit(upstream
, &revs
, UNINTERESTING
))
2515 die(_("unknown commit %s"), upstream
);
2517 /* Don't say anything if head and upstream are the same. */
2518 if (revs
.pending
.nr
== 2) {
2519 struct object_array_entry
*o
= revs
.pending
.objects
;
2520 if (oideq(&o
[0].item
->oid
, &o
[1].item
->oid
))
2524 get_patch_ids(&revs
, &ids
);
2526 if (limit
&& add_pending_commit(limit
, &revs
, UNINTERESTING
))
2527 die(_("unknown commit %s"), limit
);
2529 /* reverse the list of commits */
2530 if (prepare_revision_walk(&revs
))
2531 die(_("revision walk setup failed"));
2532 while ((commit
= get_revision(&revs
)) != NULL
) {
2533 commit_list_insert(commit
, &list
);
2539 commit
= list
->item
;
2540 if (has_commit_patch_id(commit
, &ids
))
2542 print_commit(sign
, commit
, verbose
, abbrev
, revs
.diffopt
.file
);
2546 free_patch_ids(&ids
);