2 * Builtin "git log" and related commands (show, whatchanged)
4 * (C) Copyright 2006 Linus Torvalds
7 #define USE_THE_INDEX_COMPATIBILITY_MACROS
11 #include "object-store.h"
15 #include "diff-merges.h"
20 #include "reflog-walk.h"
21 #include "patch-ids.h"
22 #include "run-command.h"
25 #include "string-list.h"
26 #include "parse-options.h"
29 #include "streaming.h"
32 #include "gpg-interface.h"
34 #include "commit-slab.h"
35 #include "repository.h"
36 #include "commit-reach.h"
37 #include "range-diff.h"
38 #include "tmp-objdir.h"
40 #define MAIL_DEFAULT_WRAP 72
41 #define COVER_FROM_AUTO_MAX_SUBJECT_LEN 100
42 #define FORMAT_PATCH_NAME_MAX_DEFAULT 64
44 /* Set a default date-time format for git log ("log.date" config variable) */
45 static const char *default_date_mode
= NULL
;
47 static int default_abbrev_commit
;
48 static int default_show_root
= 1;
49 static int default_follow
;
50 static int default_show_signature
;
51 static int default_encode_email_headers
= 1;
52 static int decoration_style
;
53 static int decoration_given
;
54 static int use_mailmap_config
= 1;
55 static unsigned int force_in_body_from
;
56 static const char *fmt_patch_subject_prefix
= "PATCH";
57 static int fmt_patch_name_max
= FORMAT_PATCH_NAME_MAX_DEFAULT
;
58 static const char *fmt_pretty
;
60 static const char * const builtin_log_usage
[] = {
61 N_("git log [<options>] [<revision-range>] [[--] <path>...]"),
62 N_("git show [<options>] <object>..."),
66 struct line_opt_callback_data
{
69 struct string_list args
;
72 static int session_is_interactive(void)
74 return isatty(1) || pager_in_use();
77 static int auto_decoration_style(void)
79 return session_is_interactive() ? DECORATE_SHORT_REFS
: 0;
82 static int parse_decoration_style(const char *value
)
84 switch (git_parse_maybe_bool(value
)) {
86 return DECORATE_SHORT_REFS
;
92 if (!strcmp(value
, "full"))
93 return DECORATE_FULL_REFS
;
94 else if (!strcmp(value
, "short"))
95 return DECORATE_SHORT_REFS
;
96 else if (!strcmp(value
, "auto"))
97 return auto_decoration_style();
99 * Please update _git_log() in git-completion.bash when you
100 * add new decoration styles.
105 static int use_default_decoration_filter
= 1;
106 static struct string_list decorate_refs_exclude
= STRING_LIST_INIT_NODUP
;
107 static struct string_list decorate_refs_exclude_config
= STRING_LIST_INIT_NODUP
;
108 static struct string_list decorate_refs_include
= STRING_LIST_INIT_NODUP
;
110 static int clear_decorations_callback(const struct option
*opt
,
111 const char *arg
, int unset
)
113 string_list_clear(&decorate_refs_include
, 0);
114 string_list_clear(&decorate_refs_exclude
, 0);
115 use_default_decoration_filter
= 0;
119 static int decorate_callback(const struct option
*opt
, const char *arg
, int unset
)
122 decoration_style
= 0;
124 decoration_style
= parse_decoration_style(arg
);
126 decoration_style
= DECORATE_SHORT_REFS
;
128 if (decoration_style
< 0)
129 die(_("invalid --decorate option: %s"), arg
);
131 decoration_given
= 1;
136 static int log_line_range_callback(const struct option
*option
, const char *arg
, int unset
)
138 struct line_opt_callback_data
*data
= option
->value
;
140 BUG_ON_OPT_NEG(unset
);
145 data
->rev
->line_level_traverse
= 1;
146 string_list_append(&data
->args
, arg
);
151 static void init_log_defaults(void)
153 init_diff_ui_defaults();
155 decoration_style
= auto_decoration_style();
158 static void cmd_log_init_defaults(struct rev_info
*rev
)
161 get_commit_format(fmt_pretty
, rev
);
163 rev
->diffopt
.flags
.default_follow_renames
= 1;
164 rev
->verbose_header
= 1;
165 rev
->diffopt
.flags
.recursive
= 1;
166 rev
->diffopt
.stat_width
= -1; /* use full terminal width */
167 rev
->diffopt
.stat_graph_width
= -1; /* respect statGraphWidth config */
168 rev
->abbrev_commit
= default_abbrev_commit
;
169 rev
->show_root_diff
= default_show_root
;
170 rev
->subject_prefix
= fmt_patch_subject_prefix
;
171 rev
->patch_name_max
= fmt_patch_name_max
;
172 rev
->show_signature
= default_show_signature
;
173 rev
->encode_email_headers
= default_encode_email_headers
;
174 rev
->diffopt
.flags
.allow_textconv
= 1;
176 if (default_date_mode
)
177 parse_date_format(default_date_mode
, &rev
->date_mode
);
180 static void set_default_decoration_filter(struct decoration_filter
*decoration_filter
)
184 struct string_list
*include
= decoration_filter
->include_ref_pattern
;
185 const struct string_list
*config_exclude
=
186 git_config_get_value_multi("log.excludeDecoration");
188 if (config_exclude
) {
189 struct string_list_item
*item
;
190 for_each_string_list_item(item
, config_exclude
)
191 string_list_append(decoration_filter
->exclude_ref_config_pattern
,
196 * By default, decorate_all is disabled. Enable it if
197 * log.initialDecorationSet=all. Don't ever disable it by config,
198 * since the command-line takes precedent.
200 if (use_default_decoration_filter
&&
201 !git_config_get_string("log.initialdecorationset", &value
) &&
202 !strcmp("all", value
))
203 use_default_decoration_filter
= 0;
206 if (!use_default_decoration_filter
||
207 decoration_filter
->exclude_ref_pattern
->nr
||
208 decoration_filter
->include_ref_pattern
->nr
||
209 decoration_filter
->exclude_ref_config_pattern
->nr
)
213 * No command-line or config options were given, so
214 * populate with sensible defaults.
216 for (i
= 0; i
< ARRAY_SIZE(ref_namespace
); i
++) {
217 if (!ref_namespace
[i
].decoration
)
220 string_list_append(include
, ref_namespace
[i
].ref
);
224 static void cmd_log_init_finish(int argc
, const char **argv
, const char *prefix
,
225 struct rev_info
*rev
, struct setup_revision_opt
*opt
)
227 struct userformat_want w
;
228 int quiet
= 0, source
= 0, mailmap
;
229 static struct line_opt_callback_data line_cb
= {NULL
, NULL
, STRING_LIST_INIT_DUP
};
230 struct decoration_filter decoration_filter
= {
231 .exclude_ref_pattern
= &decorate_refs_exclude
,
232 .include_ref_pattern
= &decorate_refs_include
,
233 .exclude_ref_config_pattern
= &decorate_refs_exclude_config
,
235 static struct revision_sources revision_sources
;
237 const struct option builtin_log_options
[] = {
238 OPT__QUIET(&quiet
, N_("suppress diff output")),
239 OPT_BOOL(0, "source", &source
, N_("show source")),
240 OPT_BOOL(0, "use-mailmap", &mailmap
, N_("use mail map file")),
241 OPT_ALIAS(0, "mailmap", "use-mailmap"),
242 OPT_CALLBACK_F(0, "clear-decorations", NULL
, NULL
,
243 N_("clear all previously-defined decoration filters"),
244 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
,
245 clear_decorations_callback
),
246 OPT_STRING_LIST(0, "decorate-refs", &decorate_refs_include
,
247 N_("pattern"), N_("only decorate refs that match <pattern>")),
248 OPT_STRING_LIST(0, "decorate-refs-exclude", &decorate_refs_exclude
,
249 N_("pattern"), N_("do not decorate refs that match <pattern>")),
250 OPT_CALLBACK_F(0, "decorate", NULL
, NULL
, N_("decorate options"),
251 PARSE_OPT_OPTARG
, decorate_callback
),
252 OPT_CALLBACK('L', NULL
, &line_cb
, "range:file",
253 N_("trace the evolution of line range <start>,<end> or function :<funcname> in <file>"),
254 log_line_range_callback
),
259 line_cb
.prefix
= prefix
;
261 mailmap
= use_mailmap_config
;
262 argc
= parse_options(argc
, argv
, prefix
,
263 builtin_log_options
, builtin_log_usage
,
264 PARSE_OPT_KEEP_ARGV0
| PARSE_OPT_KEEP_UNKNOWN_OPT
|
265 PARSE_OPT_KEEP_DASHDASH
);
268 rev
->diffopt
.output_format
|= DIFF_FORMAT_NO_OUTPUT
;
269 argc
= setup_revisions(argc
, argv
, rev
, opt
);
271 /* Any arguments at this point are not recognized */
273 die(_("unrecognized argument: %s"), argv
[1]);
275 if (rev
->line_level_traverse
&& rev
->prune_data
.nr
)
276 die(_("-L<range>:<file> cannot be used with pathspec"));
278 memset(&w
, 0, sizeof(w
));
279 userformat_find_requirements(NULL
, &w
);
281 if (!rev
->show_notes_given
&& (!rev
->pretty_given
|| w
.notes
))
284 load_display_notes(&rev
->notes_opt
);
286 if ((rev
->diffopt
.pickaxe_opts
& DIFF_PICKAXE_KINDS_MASK
) ||
287 rev
->diffopt
.filter
|| rev
->diffopt
.flags
.follow_renames
)
288 rev
->always_show_header
= 0;
290 if (source
|| w
.source
) {
291 init_revision_sources(&revision_sources
);
292 rev
->sources
= &revision_sources
;
296 rev
->mailmap
= xmalloc(sizeof(struct string_list
));
297 string_list_init_nodup(rev
->mailmap
);
298 read_mailmap(rev
->mailmap
);
301 if (rev
->pretty_given
&& rev
->commit_format
== CMIT_FMT_RAW
) {
303 * "log --pretty=raw" is special; ignore UI oriented
304 * configuration variables such as decoration.
306 if (!decoration_given
)
307 decoration_style
= 0;
308 if (!rev
->abbrev_commit_given
)
309 rev
->abbrev_commit
= 0;
312 if (rev
->commit_format
== CMIT_FMT_USERFORMAT
) {
315 * Disable decoration loading if the format will not
318 decoration_style
= 0;
319 } else if (!decoration_style
) {
321 * If we are going to show them, make sure we do load
322 * them here, but taking care not to override a
323 * specific style set by config or --decorate.
325 decoration_style
= DECORATE_SHORT_REFS
;
329 if (decoration_style
|| rev
->simplify_by_decoration
) {
330 set_default_decoration_filter(&decoration_filter
);
332 if (decoration_style
)
333 rev
->show_decorations
= 1;
335 load_ref_decorations(&decoration_filter
, decoration_style
);
338 if (rev
->line_level_traverse
)
339 line_log_init(rev
, line_cb
.prefix
, &line_cb
.args
);
344 static void cmd_log_init(int argc
, const char **argv
, const char *prefix
,
345 struct rev_info
*rev
, struct setup_revision_opt
*opt
)
347 cmd_log_init_defaults(rev
);
348 cmd_log_init_finish(argc
, argv
, prefix
, rev
, opt
);
351 static int cmd_log_deinit(int ret
, struct rev_info
*rev
)
353 release_revisions(rev
);
358 * This gives a rough estimate for how many commits we
359 * will print out in the list.
361 static int estimate_commit_count(struct commit_list
*list
)
366 struct commit
*commit
= list
->item
;
367 unsigned int flags
= commit
->object
.flags
;
369 if (!(flags
& (TREESAME
| UNINTERESTING
)))
375 static void show_early_header(struct rev_info
*rev
, const char *stage
, int nr
)
377 if (rev
->shown_one
) {
379 if (rev
->commit_format
!= CMIT_FMT_ONELINE
)
380 putchar(rev
->diffopt
.line_termination
);
382 fprintf(rev
->diffopt
.file
, _("Final output: %d %s\n"), nr
, stage
);
385 static struct itimerval early_output_timer
;
387 static void log_show_early(struct rev_info
*revs
, struct commit_list
*list
)
389 int i
= revs
->early_output
;
391 int no_free
= revs
->diffopt
.no_free
;
393 revs
->diffopt
.no_free
= 0;
394 sort_in_topological_order(&list
, revs
->sort_order
);
396 struct commit
*commit
= list
->item
;
397 switch (simplify_commit(revs
, commit
)) {
400 int n
= estimate_commit_count(list
);
401 show_early_header(revs
, "incomplete", n
);
404 log_tree_commit(revs
, commit
);
410 revs
->diffopt
.no_free
= no_free
;
411 diff_free(&revs
->diffopt
);
417 /* Did we already get enough commits for the early output? */
419 revs
->diffopt
.no_free
= 0;
420 diff_free(&revs
->diffopt
);
425 * ..if no, then repeat it twice a second until we
428 * NOTE! We don't use "it_interval", because if the
429 * reader isn't listening, we want our output to be
430 * throttled by the writing, and not have the timer
431 * trigger every second even if we're blocked on a
434 early_output_timer
.it_value
.tv_sec
= 0;
435 early_output_timer
.it_value
.tv_usec
= 500000;
436 setitimer(ITIMER_REAL
, &early_output_timer
, NULL
);
439 static void early_output(int signal
)
441 show_early_output
= log_show_early
;
444 static void setup_early_output(void)
449 * Set up the signal handler, minimally intrusively:
450 * we only set a single volatile integer word (not
451 * using sigatomic_t - trying to avoid unnecessary
452 * system dependencies and headers), and using
455 memset(&sa
, 0, sizeof(sa
));
456 sa
.sa_handler
= early_output
;
457 sigemptyset(&sa
.sa_mask
);
458 sa
.sa_flags
= SA_RESTART
;
459 sigaction(SIGALRM
, &sa
, NULL
);
462 * If we can get the whole output in less than a
463 * tenth of a second, don't even bother doing the
464 * early-output thing..
466 * This is a one-time-only trigger.
468 early_output_timer
.it_value
.tv_sec
= 0;
469 early_output_timer
.it_value
.tv_usec
= 100000;
470 setitimer(ITIMER_REAL
, &early_output_timer
, NULL
);
473 static void finish_early_output(struct rev_info
*rev
)
475 int n
= estimate_commit_count(rev
->commits
);
476 signal(SIGALRM
, SIG_IGN
);
477 show_early_header(rev
, "done", n
);
480 static int cmd_log_walk_no_free(struct rev_info
*rev
)
482 struct commit
*commit
;
486 if (rev
->remerge_diff
) {
487 rev
->remerge_objdir
= tmp_objdir_create("remerge-diff");
488 if (!rev
->remerge_objdir
)
489 die(_("unable to create temporary object directory"));
490 tmp_objdir_replace_primary_odb(rev
->remerge_objdir
, 1);
493 if (rev
->early_output
)
494 setup_early_output();
496 if (prepare_revision_walk(rev
))
497 die(_("revision walk setup failed"));
499 if (rev
->early_output
)
500 finish_early_output(rev
);
503 * For --check and --exit-code, the exit code is based on CHECK_FAILED
504 * and HAS_CHANGES being accumulated in rev->diffopt, so be careful to
505 * retain that state information if replacing rev->diffopt in this loop
507 while ((commit
= get_revision(rev
)) != NULL
) {
508 if (!log_tree_commit(rev
, commit
) && rev
->max_count
>= 0)
510 * We decremented max_count in get_revision,
511 * but we didn't actually show the commit.
514 if (!rev
->reflog_info
) {
516 * We may show a given commit multiple times when
517 * walking the reflogs.
519 free_commit_buffer(the_repository
->parsed_objects
,
521 free_commit_list(commit
->parents
);
522 commit
->parents
= NULL
;
524 if (saved_nrl
< rev
->diffopt
.needed_rename_limit
)
525 saved_nrl
= rev
->diffopt
.needed_rename_limit
;
526 if (rev
->diffopt
.degraded_cc_to_c
)
529 rev
->diffopt
.degraded_cc_to_c
= saved_dcctc
;
530 rev
->diffopt
.needed_rename_limit
= saved_nrl
;
532 if (rev
->remerge_diff
) {
533 tmp_objdir_destroy(rev
->remerge_objdir
);
534 rev
->remerge_objdir
= NULL
;
537 if (rev
->diffopt
.output_format
& DIFF_FORMAT_CHECKDIFF
&&
538 rev
->diffopt
.flags
.check_failed
) {
541 return diff_result_code(&rev
->diffopt
, 0);
544 static int cmd_log_walk(struct rev_info
*rev
)
548 rev
->diffopt
.no_free
= 1;
549 retval
= cmd_log_walk_no_free(rev
);
550 rev
->diffopt
.no_free
= 0;
551 diff_free(&rev
->diffopt
);
555 static int git_log_config(const char *var
, const char *value
, void *cb
)
557 const char *slot_name
;
559 if (!strcmp(var
, "format.pretty"))
560 return git_config_string(&fmt_pretty
, var
, value
);
561 if (!strcmp(var
, "format.subjectprefix"))
562 return git_config_string(&fmt_patch_subject_prefix
, var
, value
);
563 if (!strcmp(var
, "format.filenamemaxlength")) {
564 fmt_patch_name_max
= git_config_int(var
, value
);
567 if (!strcmp(var
, "format.encodeemailheaders")) {
568 default_encode_email_headers
= git_config_bool(var
, value
);
571 if (!strcmp(var
, "log.abbrevcommit")) {
572 default_abbrev_commit
= git_config_bool(var
, value
);
575 if (!strcmp(var
, "log.date"))
576 return git_config_string(&default_date_mode
, var
, value
);
577 if (!strcmp(var
, "log.decorate")) {
578 decoration_style
= parse_decoration_style(value
);
579 if (decoration_style
< 0)
580 decoration_style
= 0; /* maybe warn? */
583 if (!strcmp(var
, "log.diffmerges"))
584 return diff_merges_config(value
);
585 if (!strcmp(var
, "log.showroot")) {
586 default_show_root
= git_config_bool(var
, value
);
589 if (!strcmp(var
, "log.follow")) {
590 default_follow
= git_config_bool(var
, value
);
593 if (skip_prefix(var
, "color.decorate.", &slot_name
))
594 return parse_decorate_color_config(var
, slot_name
, value
);
595 if (!strcmp(var
, "log.mailmap")) {
596 use_mailmap_config
= git_config_bool(var
, value
);
599 if (!strcmp(var
, "log.showsignature")) {
600 default_show_signature
= git_config_bool(var
, value
);
604 if (git_gpg_config(var
, value
, cb
) < 0)
606 return git_diff_ui_config(var
, value
, cb
);
609 int cmd_whatchanged(int argc
, const char **argv
, const char *prefix
)
612 struct setup_revision_opt opt
;
615 git_config(git_log_config
, NULL
);
617 repo_init_revisions(the_repository
, &rev
, prefix
);
618 git_config(grep_config
, &rev
.grep_filter
);
621 rev
.simplify_history
= 0;
622 memset(&opt
, 0, sizeof(opt
));
624 opt
.revarg_opt
= REVARG_COMMITTISH
;
625 cmd_log_init(argc
, argv
, prefix
, &rev
, &opt
);
626 if (!rev
.diffopt
.output_format
)
627 rev
.diffopt
.output_format
= DIFF_FORMAT_RAW
;
628 return cmd_log_deinit(cmd_log_walk(&rev
), &rev
);
631 static void show_tagger(const char *buf
, struct rev_info
*rev
)
633 struct strbuf out
= STRBUF_INIT
;
634 struct pretty_print_context pp
= {0};
636 pp
.fmt
= rev
->commit_format
;
637 pp
.date_mode
= rev
->date_mode
;
638 pp_user_info(&pp
, "Tagger", &out
, buf
, get_log_output_encoding());
639 fprintf(rev
->diffopt
.file
, "%s", out
.buf
);
640 strbuf_release(&out
);
643 static int show_blob_object(const struct object_id
*oid
, struct rev_info
*rev
, const char *obj_name
)
645 struct object_id oidc
;
646 struct object_context obj_context
;
650 fflush(rev
->diffopt
.file
);
651 if (!rev
->diffopt
.flags
.textconv_set_via_cmdline
||
652 !rev
->diffopt
.flags
.allow_textconv
)
653 return stream_blob_to_fd(1, oid
, NULL
, 0);
655 if (get_oid_with_context(the_repository
, obj_name
,
657 &oidc
, &obj_context
))
658 die(_("not a valid object name %s"), obj_name
);
659 if (!obj_context
.path
||
660 !textconv_object(the_repository
, obj_context
.path
,
661 obj_context
.mode
, &oidc
, 1, &buf
, &size
)) {
662 free(obj_context
.path
);
663 return stream_blob_to_fd(1, oid
, NULL
, 0);
667 die(_("git show %s: bad file"), obj_name
);
669 write_or_die(1, buf
, size
);
670 free(obj_context
.path
);
674 static int show_tag_object(const struct object_id
*oid
, struct rev_info
*rev
)
677 enum object_type type
;
678 char *buf
= read_object_file(oid
, &type
, &size
);
682 return error(_("could not read object %s"), oid_to_hex(oid
));
684 assert(type
== OBJ_TAG
);
685 while (offset
< size
&& buf
[offset
] != '\n') {
686 int new_offset
= offset
+ 1;
688 while (new_offset
< size
&& buf
[new_offset
++] != '\n')
690 if (skip_prefix(buf
+ offset
, "tagger ", &ident
))
691 show_tagger(ident
, rev
);
696 fwrite(buf
+ offset
, size
- offset
, 1, rev
->diffopt
.file
);
701 static int show_tree_object(const struct object_id
*oid UNUSED
,
702 struct strbuf
*base UNUSED
,
703 const char *pathname
, unsigned mode
,
706 FILE *file
= context
;
707 fprintf(file
, "%s%s\n", pathname
, S_ISDIR(mode
) ? "/" : "");
711 static void show_setup_revisions_tweak(struct rev_info
*rev
,
712 struct setup_revision_opt
*opt
)
714 if (rev
->first_parent_only
)
715 diff_merges_default_to_first_parent(rev
);
717 diff_merges_default_to_dense_combined(rev
);
718 if (!rev
->diffopt
.output_format
)
719 rev
->diffopt
.output_format
= DIFF_FORMAT_PATCH
;
722 int cmd_show(int argc
, const char **argv
, const char *prefix
)
726 struct setup_revision_opt opt
;
727 struct pathspec match_all
;
731 git_config(git_log_config
, NULL
);
733 if (the_repository
->gitdir
) {
734 prepare_repo_settings(the_repository
);
735 the_repository
->settings
.command_requires_full_index
= 0;
738 memset(&match_all
, 0, sizeof(match_all
));
739 repo_init_revisions(the_repository
, &rev
, prefix
);
740 git_config(grep_config
, &rev
.grep_filter
);
743 rev
.always_show_header
= 1;
745 rev
.diffopt
.stat_width
= -1; /* Scale to real terminal size */
747 memset(&opt
, 0, sizeof(opt
));
749 opt
.tweak
= show_setup_revisions_tweak
;
750 cmd_log_init(argc
, argv
, prefix
, &rev
, &opt
);
753 return cmd_log_deinit(cmd_log_walk(&rev
), &rev
);
755 rev
.diffopt
.no_free
= 1;
756 for (i
= 0; i
< rev
.pending
.nr
&& !ret
; i
++) {
757 struct object
*o
= rev
.pending
.objects
[i
].item
;
758 const char *name
= rev
.pending
.objects
[i
].name
;
761 ret
= show_blob_object(&o
->oid
, &rev
, name
);
764 struct tag
*t
= (struct tag
*)o
;
765 struct object_id
*oid
= get_tagged_oid(t
);
769 fprintf(rev
.diffopt
.file
, "%stag %s%s\n",
770 diff_get_color_opt(&rev
.diffopt
, DIFF_COMMIT
),
772 diff_get_color_opt(&rev
.diffopt
, DIFF_RESET
));
773 ret
= show_tag_object(&o
->oid
, &rev
);
777 o
= parse_object(the_repository
, oid
);
779 ret
= error(_("could not read object %s"),
781 rev
.pending
.objects
[i
].item
= o
;
788 fprintf(rev
.diffopt
.file
, "%stree %s%s\n\n",
789 diff_get_color_opt(&rev
.diffopt
, DIFF_COMMIT
),
791 diff_get_color_opt(&rev
.diffopt
, DIFF_RESET
));
792 read_tree(the_repository
, (struct tree
*)o
,
793 &match_all
, show_tree_object
,
799 struct object_array old
;
800 struct object_array blank
= OBJECT_ARRAY_INIT
;
802 memcpy(&old
, &rev
.pending
, sizeof(old
));
803 memcpy(&rev
.pending
, &blank
, sizeof(rev
.pending
));
805 add_object_array(o
, name
, &rev
.pending
);
806 ret
= cmd_log_walk_no_free(&rev
);
810 * object_array_clear(&pending). It was
811 * cleared already in prepare_revision_walk()
813 memcpy(&rev
.pending
, &old
, sizeof(rev
.pending
));
817 ret
= error(_("unknown type: %d"), o
->type
);
821 rev
.diffopt
.no_free
= 0;
822 diff_free(&rev
.diffopt
);
824 return cmd_log_deinit(ret
, &rev
);
828 * This is equivalent to "git log -g --abbrev-commit --pretty=oneline"
830 int cmd_log_reflog(int argc
, const char **argv
, const char *prefix
)
833 struct setup_revision_opt opt
;
836 git_config(git_log_config
, NULL
);
838 repo_init_revisions(the_repository
, &rev
, prefix
);
839 init_reflog_walk(&rev
.reflog_info
);
840 git_config(grep_config
, &rev
.grep_filter
);
842 rev
.verbose_header
= 1;
843 memset(&opt
, 0, sizeof(opt
));
845 cmd_log_init_defaults(&rev
);
846 rev
.abbrev_commit
= 1;
847 rev
.commit_format
= CMIT_FMT_ONELINE
;
848 rev
.use_terminator
= 1;
849 rev
.always_show_header
= 1;
850 cmd_log_init_finish(argc
, argv
, prefix
, &rev
, &opt
);
852 return cmd_log_deinit(cmd_log_walk(&rev
), &rev
);
855 static void log_setup_revisions_tweak(struct rev_info
*rev
,
856 struct setup_revision_opt
*opt
)
858 if (rev
->diffopt
.flags
.default_follow_renames
&&
859 rev
->prune_data
.nr
== 1)
860 rev
->diffopt
.flags
.follow_renames
= 1;
862 if (rev
->first_parent_only
)
863 diff_merges_default_to_first_parent(rev
);
866 int cmd_log(int argc
, const char **argv
, const char *prefix
)
869 struct setup_revision_opt opt
;
872 git_config(git_log_config
, NULL
);
874 repo_init_revisions(the_repository
, &rev
, prefix
);
875 git_config(grep_config
, &rev
.grep_filter
);
877 rev
.always_show_header
= 1;
878 memset(&opt
, 0, sizeof(opt
));
880 opt
.revarg_opt
= REVARG_COMMITTISH
;
881 opt
.tweak
= log_setup_revisions_tweak
;
882 cmd_log_init(argc
, argv
, prefix
, &rev
, &opt
);
883 return cmd_log_deinit(cmd_log_walk(&rev
), &rev
);
888 static const char *fmt_patch_suffix
= ".patch";
889 static int numbered
= 0;
890 static int auto_number
= 1;
892 static char *default_attach
= NULL
;
894 static struct string_list extra_hdr
= STRING_LIST_INIT_NODUP
;
895 static struct string_list extra_to
= STRING_LIST_INIT_NODUP
;
896 static struct string_list extra_cc
= STRING_LIST_INIT_NODUP
;
898 static void add_header(const char *value
)
900 struct string_list_item
*item
;
901 int len
= strlen(value
);
902 while (len
&& value
[len
- 1] == '\n')
905 if (!strncasecmp(value
, "to: ", 4)) {
906 item
= string_list_append(&extra_to
, value
+ 4);
908 } else if (!strncasecmp(value
, "cc: ", 4)) {
909 item
= string_list_append(&extra_cc
, value
+ 4);
912 item
= string_list_append(&extra_hdr
, value
);
915 item
->string
[len
] = '\0';
931 enum cover_from_description
{
938 enum auto_base_setting
{
944 static enum thread_level thread
;
945 static int do_signoff
;
946 static enum auto_base_setting auto_base
;
948 static const char *signature
= git_version_string
;
949 static const char *signature_file
;
950 static enum cover_setting config_cover_letter
;
951 static const char *config_output_directory
;
952 static enum cover_from_description cover_from_description_mode
= COVER_FROM_MESSAGE
;
953 static int show_notes
;
954 static struct display_notes_opt notes_opt
;
956 static enum cover_from_description
parse_cover_from_description(const char *arg
)
958 if (!arg
|| !strcmp(arg
, "default"))
959 return COVER_FROM_MESSAGE
;
960 else if (!strcmp(arg
, "none"))
961 return COVER_FROM_NONE
;
962 else if (!strcmp(arg
, "message"))
963 return COVER_FROM_MESSAGE
;
964 else if (!strcmp(arg
, "subject"))
965 return COVER_FROM_SUBJECT
;
966 else if (!strcmp(arg
, "auto"))
967 return COVER_FROM_AUTO
;
969 die(_("%s: invalid cover from description mode"), arg
);
972 static int git_format_config(const char *var
, const char *value
, void *cb
)
974 if (!strcmp(var
, "format.headers")) {
976 die(_("format.headers without value"));
980 if (!strcmp(var
, "format.suffix"))
981 return git_config_string(&fmt_patch_suffix
, var
, value
);
982 if (!strcmp(var
, "format.to")) {
984 return config_error_nonbool(var
);
985 string_list_append(&extra_to
, value
);
988 if (!strcmp(var
, "format.cc")) {
990 return config_error_nonbool(var
);
991 string_list_append(&extra_cc
, value
);
994 if (!strcmp(var
, "diff.color") || !strcmp(var
, "color.diff") ||
995 !strcmp(var
, "color.ui") || !strcmp(var
, "diff.submodule")) {
998 if (!strcmp(var
, "format.numbered")) {
999 if (value
&& !strcasecmp(value
, "auto")) {
1003 numbered
= git_config_bool(var
, value
);
1004 auto_number
= auto_number
&& numbered
;
1007 if (!strcmp(var
, "format.attach")) {
1008 if (value
&& *value
)
1009 default_attach
= xstrdup(value
);
1011 default_attach
= xstrdup(git_version_string
);
1014 if (!strcmp(var
, "format.thread")) {
1015 if (value
&& !strcasecmp(value
, "deep")) {
1016 thread
= THREAD_DEEP
;
1019 if (value
&& !strcasecmp(value
, "shallow")) {
1020 thread
= THREAD_SHALLOW
;
1023 thread
= git_config_bool(var
, value
) ? THREAD_SHALLOW
: THREAD_UNSET
;
1026 if (!strcmp(var
, "format.signoff")) {
1027 do_signoff
= git_config_bool(var
, value
);
1030 if (!strcmp(var
, "format.signature"))
1031 return git_config_string(&signature
, var
, value
);
1032 if (!strcmp(var
, "format.signaturefile"))
1033 return git_config_pathname(&signature_file
, var
, value
);
1034 if (!strcmp(var
, "format.coverletter")) {
1035 if (value
&& !strcasecmp(value
, "auto")) {
1036 config_cover_letter
= COVER_AUTO
;
1039 config_cover_letter
= git_config_bool(var
, value
) ? COVER_ON
: COVER_OFF
;
1042 if (!strcmp(var
, "format.outputdirectory"))
1043 return git_config_string(&config_output_directory
, var
, value
);
1044 if (!strcmp(var
, "format.useautobase")) {
1045 if (value
&& !strcasecmp(value
, "whenAble")) {
1046 auto_base
= AUTO_BASE_WHEN_ABLE
;
1049 auto_base
= git_config_bool(var
, value
) ? AUTO_BASE_ALWAYS
: AUTO_BASE_NEVER
;
1052 if (!strcmp(var
, "format.from")) {
1053 int b
= git_parse_maybe_bool(value
);
1056 from
= xstrdup(value
);
1058 from
= xstrdup(git_committer_info(IDENT_NO_DATE
));
1063 if (!strcmp(var
, "format.forceinbodyfrom")) {
1064 force_in_body_from
= git_config_bool(var
, value
);
1067 if (!strcmp(var
, "format.notes")) {
1068 int b
= git_parse_maybe_bool(value
);
1070 enable_ref_display_notes(¬es_opt
, &show_notes
, value
);
1072 enable_default_display_notes(¬es_opt
, &show_notes
);
1074 disable_display_notes(¬es_opt
, &show_notes
);
1077 if (!strcmp(var
, "format.coverfromdescription")) {
1078 cover_from_description_mode
= parse_cover_from_description(value
);
1082 return git_log_config(var
, value
, cb
);
1085 static const char *output_directory
= NULL
;
1086 static int outdir_offset
;
1088 static int open_next_file(struct commit
*commit
, const char *subject
,
1089 struct rev_info
*rev
, int quiet
)
1091 struct strbuf filename
= STRBUF_INIT
;
1093 if (output_directory
) {
1094 strbuf_addstr(&filename
, output_directory
);
1095 strbuf_complete(&filename
, '/');
1098 if (rev
->numbered_files
)
1099 strbuf_addf(&filename
, "%d", rev
->nr
);
1101 fmt_output_commit(&filename
, commit
, rev
);
1103 fmt_output_subject(&filename
, subject
, rev
);
1106 printf("%s\n", filename
.buf
+ outdir_offset
);
1108 if (!(rev
->diffopt
.file
= fopen(filename
.buf
, "w"))) {
1109 error_errno(_("cannot open patch file %s"), filename
.buf
);
1110 strbuf_release(&filename
);
1114 strbuf_release(&filename
);
1118 static void get_patch_ids(struct rev_info
*rev
, struct patch_ids
*ids
)
1120 struct rev_info check_rev
;
1121 struct commit
*commit
, *c1
, *c2
;
1122 struct object
*o1
, *o2
;
1123 unsigned flags1
, flags2
;
1125 if (rev
->pending
.nr
!= 2)
1126 die(_("need exactly one range"));
1128 o1
= rev
->pending
.objects
[0].item
;
1129 o2
= rev
->pending
.objects
[1].item
;
1132 c1
= lookup_commit_reference(the_repository
, &o1
->oid
);
1133 c2
= lookup_commit_reference(the_repository
, &o2
->oid
);
1135 if ((flags1
& UNINTERESTING
) == (flags2
& UNINTERESTING
))
1136 die(_("not a range"));
1138 init_patch_ids(the_repository
, ids
);
1140 /* given a range a..b get all patch ids for b..a */
1141 repo_init_revisions(the_repository
, &check_rev
, rev
->prefix
);
1142 check_rev
.max_parents
= 1;
1143 o1
->flags
^= UNINTERESTING
;
1144 o2
->flags
^= UNINTERESTING
;
1145 add_pending_object(&check_rev
, o1
, "o1");
1146 add_pending_object(&check_rev
, o2
, "o2");
1147 if (prepare_revision_walk(&check_rev
))
1148 die(_("revision walk setup failed"));
1150 while ((commit
= get_revision(&check_rev
)) != NULL
) {
1151 add_commit_patch_id(commit
, ids
);
1154 /* reset for next revision walk */
1155 clear_commit_marks(c1
, SEEN
| UNINTERESTING
| SHOWN
| ADDED
);
1156 clear_commit_marks(c2
, SEEN
| UNINTERESTING
| SHOWN
| ADDED
);
1161 static void gen_message_id(struct rev_info
*info
, char *base
)
1163 struct strbuf buf
= STRBUF_INIT
;
1164 strbuf_addf(&buf
, "%s.%"PRItime
".git.%s", base
,
1165 (timestamp_t
) time(NULL
),
1166 git_committer_info(IDENT_NO_NAME
|IDENT_NO_DATE
|IDENT_STRICT
));
1167 info
->message_id
= strbuf_detach(&buf
, NULL
);
1170 static void print_signature(FILE *file
)
1172 if (!signature
|| !*signature
)
1175 fprintf(file
, "-- \n%s", signature
);
1176 if (signature
[strlen(signature
)-1] != '\n')
1181 static char *find_branch_name(struct rev_info
*rev
)
1183 int i
, positive
= -1;
1184 struct object_id branch_oid
;
1185 const struct object_id
*tip_oid
;
1186 const char *ref
, *v
;
1187 char *full_ref
, *branch
= NULL
;
1189 for (i
= 0; i
< rev
->cmdline
.nr
; i
++) {
1190 if (rev
->cmdline
.rev
[i
].flags
& UNINTERESTING
)
1199 ref
= rev
->cmdline
.rev
[positive
].name
;
1200 tip_oid
= &rev
->cmdline
.rev
[positive
].item
->oid
;
1201 if (dwim_ref(ref
, strlen(ref
), &branch_oid
, &full_ref
, 0) &&
1202 skip_prefix(full_ref
, "refs/heads/", &v
) &&
1203 oideq(tip_oid
, &branch_oid
))
1204 branch
= xstrdup(v
);
1209 static void show_diffstat(struct rev_info
*rev
,
1210 struct commit
*origin
, struct commit
*head
)
1212 struct diff_options opts
;
1214 memcpy(&opts
, &rev
->diffopt
, sizeof(opts
));
1215 opts
.output_format
= DIFF_FORMAT_SUMMARY
| DIFF_FORMAT_DIFFSTAT
;
1216 diff_setup_done(&opts
);
1218 diff_tree_oid(get_commit_tree_oid(origin
),
1219 get_commit_tree_oid(head
),
1221 diffcore_std(&opts
);
1224 fprintf(rev
->diffopt
.file
, "\n");
1227 static void prepare_cover_text(struct pretty_print_context
*pp
,
1228 const char *branch_name
,
1230 const char *encoding
,
1233 const char *subject
= "*** SUBJECT HERE ***";
1234 const char *body
= "*** BLURB HERE ***";
1235 struct strbuf description_sb
= STRBUF_INIT
;
1236 struct strbuf subject_sb
= STRBUF_INIT
;
1238 if (cover_from_description_mode
== COVER_FROM_NONE
)
1241 if (branch_name
&& *branch_name
)
1242 read_branch_desc(&description_sb
, branch_name
);
1243 if (!description_sb
.len
)
1246 if (cover_from_description_mode
== COVER_FROM_SUBJECT
||
1247 cover_from_description_mode
== COVER_FROM_AUTO
)
1248 body
= format_subject(&subject_sb
, description_sb
.buf
, " ");
1250 if (cover_from_description_mode
== COVER_FROM_MESSAGE
||
1251 (cover_from_description_mode
== COVER_FROM_AUTO
&&
1252 subject_sb
.len
> COVER_FROM_AUTO_MAX_SUBJECT_LEN
))
1253 body
= description_sb
.buf
;
1255 subject
= subject_sb
.buf
;
1258 pp_title_line(pp
, &subject
, sb
, encoding
, need_8bit_cte
);
1259 pp_remainder(pp
, &body
, sb
, 0);
1261 strbuf_release(&description_sb
);
1262 strbuf_release(&subject_sb
);
1265 static int get_notes_refs(struct string_list_item
*item
, void *arg
)
1267 strvec_pushf(arg
, "--notes=%s", item
->string
);
1271 static void get_notes_args(struct strvec
*arg
, struct rev_info
*rev
)
1273 if (!rev
->show_notes
) {
1274 strvec_push(arg
, "--no-notes");
1275 } else if (rev
->notes_opt
.use_default_notes
> 0 ||
1276 (rev
->notes_opt
.use_default_notes
== -1 &&
1277 !rev
->notes_opt
.extra_notes_refs
.nr
)) {
1278 strvec_push(arg
, "--notes");
1280 for_each_string_list(&rev
->notes_opt
.extra_notes_refs
, get_notes_refs
, arg
);
1284 static void make_cover_letter(struct rev_info
*rev
, int use_separate_file
,
1285 struct commit
*origin
,
1286 int nr
, struct commit
**list
,
1287 const char *branch_name
,
1290 const char *committer
;
1291 struct shortlog log
;
1292 struct strbuf sb
= STRBUF_INIT
;
1294 const char *encoding
= "UTF-8";
1295 int need_8bit_cte
= 0;
1296 struct pretty_print_context pp
= {0};
1297 struct commit
*head
= list
[0];
1299 if (!cmit_fmt_is_mail(rev
->commit_format
))
1300 die(_("cover letter needs email format"));
1302 committer
= git_committer_info(0);
1304 if (use_separate_file
&&
1305 open_next_file(NULL
, rev
->numbered_files
? NULL
: "cover-letter", rev
, quiet
))
1306 die(_("failed to create cover-letter file"));
1308 log_write_email_headers(rev
, head
, &pp
.after_subject
, &need_8bit_cte
, 0);
1310 for (i
= 0; !need_8bit_cte
&& i
< nr
; i
++) {
1311 const char *buf
= get_commit_buffer(list
[i
], NULL
);
1312 if (has_non_ascii(buf
))
1314 unuse_commit_buffer(list
[i
], buf
);
1318 branch_name
= find_branch_name(rev
);
1320 pp
.fmt
= CMIT_FMT_EMAIL
;
1321 pp
.date_mode
.type
= DATE_RFC2822
;
1323 pp
.print_email_subject
= 1;
1324 pp_user_info(&pp
, NULL
, &sb
, committer
, encoding
);
1325 prepare_cover_text(&pp
, branch_name
, &sb
, encoding
, need_8bit_cte
);
1326 fprintf(rev
->diffopt
.file
, "%s\n", sb
.buf
);
1328 strbuf_release(&sb
);
1330 shortlog_init(&log
);
1332 log
.wrap
= MAIL_DEFAULT_WRAP
;
1335 log
.file
= rev
->diffopt
.file
;
1336 log
.groups
= SHORTLOG_GROUP_AUTHOR
;
1337 for (i
= 0; i
< nr
; i
++)
1338 shortlog_add_commit(&log
, list
[i
]);
1340 shortlog_output(&log
);
1342 /* We can only do diffstat with a unique reference point */
1344 show_diffstat(rev
, origin
, head
);
1346 if (rev
->idiff_oid1
) {
1347 fprintf_ln(rev
->diffopt
.file
, "%s", rev
->idiff_title
);
1348 show_interdiff(rev
->idiff_oid1
, rev
->idiff_oid2
, 0,
1354 * Pass minimum required diff-options to range-diff; others
1355 * can be added later if deemed desirable.
1357 struct diff_options opts
;
1358 struct strvec other_arg
= STRVEC_INIT
;
1359 struct range_diff_options range_diff_opts
= {
1360 .creation_factor
= rev
->creation_factor
,
1363 .other_arg
= &other_arg
1367 opts
.file
= rev
->diffopt
.file
;
1368 opts
.use_color
= rev
->diffopt
.use_color
;
1369 diff_setup_done(&opts
);
1370 fprintf_ln(rev
->diffopt
.file
, "%s", rev
->rdiff_title
);
1371 get_notes_args(&other_arg
, rev
);
1372 show_range_diff(rev
->rdiff1
, rev
->rdiff2
, &range_diff_opts
);
1373 strvec_clear(&other_arg
);
1377 static const char *clean_message_id(const char *msg_id
)
1380 const char *a
, *z
, *m
;
1383 while ((ch
= *m
) && (isspace(ch
) || (ch
== '<')))
1388 if (!isspace(ch
) && (ch
!= '>'))
1393 die(_("insane in-reply-to: %s"), msg_id
);
1396 return xmemdupz(a
, z
- a
);
1399 static const char *set_outdir(const char *prefix
, const char *output_directory
)
1401 if (output_directory
&& is_absolute_path(output_directory
))
1402 return output_directory
;
1404 if (!prefix
|| !*prefix
) {
1405 if (output_directory
)
1406 return output_directory
;
1407 /* The user did not explicitly ask for "./" */
1412 outdir_offset
= strlen(prefix
);
1413 if (!output_directory
)
1416 return prefix_filename(prefix
, output_directory
);
1419 static const char * const builtin_format_patch_usage
[] = {
1420 N_("git format-patch [<options>] [<since> | <revision-range>]"),
1424 static int keep_subject
= 0;
1426 static int keep_callback(const struct option
*opt
, const char *arg
, int unset
)
1428 BUG_ON_OPT_NEG(unset
);
1429 BUG_ON_OPT_ARG(arg
);
1430 ((struct rev_info
*)opt
->value
)->total
= -1;
1435 static int subject_prefix
= 0;
1437 static int subject_prefix_callback(const struct option
*opt
, const char *arg
,
1440 BUG_ON_OPT_NEG(unset
);
1442 ((struct rev_info
*)opt
->value
)->subject_prefix
= arg
;
1446 static int rfc_callback(const struct option
*opt
, const char *arg
, int unset
)
1448 BUG_ON_OPT_NEG(unset
);
1449 BUG_ON_OPT_ARG(arg
);
1450 return subject_prefix_callback(opt
, "RFC PATCH", unset
);
1453 static int numbered_cmdline_opt
= 0;
1455 static int numbered_callback(const struct option
*opt
, const char *arg
,
1458 BUG_ON_OPT_ARG(arg
);
1459 *(int *)opt
->value
= numbered_cmdline_opt
= unset
? 0 : 1;
1465 static int no_numbered_callback(const struct option
*opt
, const char *arg
,
1468 BUG_ON_OPT_NEG(unset
);
1469 return numbered_callback(opt
, arg
, 1);
1472 static int output_directory_callback(const struct option
*opt
, const char *arg
,
1475 const char **dir
= (const char **)opt
->value
;
1476 BUG_ON_OPT_NEG(unset
);
1478 die(_("two output directories?"));
1483 static int thread_callback(const struct option
*opt
, const char *arg
, int unset
)
1485 enum thread_level
*thread
= (enum thread_level
*)opt
->value
;
1487 *thread
= THREAD_UNSET
;
1488 else if (!arg
|| !strcmp(arg
, "shallow"))
1489 *thread
= THREAD_SHALLOW
;
1490 else if (!strcmp(arg
, "deep"))
1491 *thread
= THREAD_DEEP
;
1493 * Please update _git_formatpatch() in git-completion.bash
1494 * when you add new options.
1501 static int attach_callback(const struct option
*opt
, const char *arg
, int unset
)
1503 struct rev_info
*rev
= (struct rev_info
*)opt
->value
;
1505 rev
->mime_boundary
= NULL
;
1507 rev
->mime_boundary
= arg
;
1509 rev
->mime_boundary
= git_version_string
;
1510 rev
->no_inline
= unset
? 0 : 1;
1514 static int inline_callback(const struct option
*opt
, const char *arg
, int unset
)
1516 struct rev_info
*rev
= (struct rev_info
*)opt
->value
;
1518 rev
->mime_boundary
= NULL
;
1520 rev
->mime_boundary
= arg
;
1522 rev
->mime_boundary
= git_version_string
;
1527 static int header_callback(const struct option
*opt
, const char *arg
, int unset
)
1530 string_list_clear(&extra_hdr
, 0);
1531 string_list_clear(&extra_to
, 0);
1532 string_list_clear(&extra_cc
, 0);
1539 static int to_callback(const struct option
*opt
, const char *arg
, int unset
)
1542 string_list_clear(&extra_to
, 0);
1544 string_list_append(&extra_to
, arg
);
1548 static int cc_callback(const struct option
*opt
, const char *arg
, int unset
)
1551 string_list_clear(&extra_cc
, 0);
1553 string_list_append(&extra_cc
, arg
);
1557 static int from_callback(const struct option
*opt
, const char *arg
, int unset
)
1559 char **from
= opt
->value
;
1566 *from
= xstrdup(arg
);
1568 *from
= xstrdup(git_committer_info(IDENT_NO_DATE
));
1572 static int base_callback(const struct option
*opt
, const char *arg
, int unset
)
1574 const char **base_commit
= opt
->value
;
1577 auto_base
= AUTO_BASE_NEVER
;
1578 *base_commit
= NULL
;
1579 } else if (!strcmp(arg
, "auto")) {
1580 auto_base
= AUTO_BASE_ALWAYS
;
1581 *base_commit
= NULL
;
1583 auto_base
= AUTO_BASE_NEVER
;
1589 struct base_tree_info
{
1590 struct object_id base_commit
;
1591 int nr_patch_id
, alloc_patch_id
;
1592 struct object_id
*patch_id
;
1595 static struct commit
*get_base_commit(const char *base_commit
,
1596 struct commit
**list
,
1599 struct commit
*base
= NULL
;
1600 struct commit
**rev
;
1601 int i
= 0, rev_nr
= 0, auto_select
, die_on_failure
;
1603 switch (auto_base
) {
1604 case AUTO_BASE_NEVER
:
1609 /* no base information is requested */
1613 case AUTO_BASE_ALWAYS
:
1614 case AUTO_BASE_WHEN_ABLE
:
1616 BUG("requested automatic base selection but a commit was provided");
1619 die_on_failure
= auto_base
== AUTO_BASE_ALWAYS
;
1623 BUG("unexpected automatic base selection method");
1627 base
= lookup_commit_reference_by_name(base_commit
);
1629 die(_("unknown commit %s"), base_commit
);
1631 struct branch
*curr_branch
= branch_get(NULL
);
1632 const char *upstream
= branch_get_upstream(curr_branch
, NULL
);
1634 struct commit_list
*base_list
;
1635 struct commit
*commit
;
1636 struct object_id oid
;
1638 if (get_oid(upstream
, &oid
)) {
1640 die(_("failed to resolve '%s' as a valid ref"), upstream
);
1644 commit
= lookup_commit_or_die(&oid
, "upstream base");
1645 base_list
= get_merge_bases_many(commit
, total
, list
);
1646 /* There should be one and only one merge base. */
1647 if (!base_list
|| base_list
->next
) {
1648 if (die_on_failure
) {
1649 die(_("could not find exact merge base"));
1651 free_commit_list(base_list
);
1655 base
= base_list
->item
;
1656 free_commit_list(base_list
);
1659 die(_("failed to get upstream, if you want to record base commit automatically,\n"
1660 "please use git branch --set-upstream-to to track a remote branch.\n"
1661 "Or you could specify base commit by --base=<base-commit-id> manually"));
1667 ALLOC_ARRAY(rev
, total
);
1668 for (i
= 0; i
< total
; i
++)
1673 * Get merge base through pair-wise computations
1674 * and store it in rev[0].
1676 while (rev_nr
> 1) {
1677 for (i
= 0; i
< rev_nr
/ 2; i
++) {
1678 struct commit_list
*merge_base
;
1679 merge_base
= get_merge_bases(rev
[2 * i
], rev
[2 * i
+ 1]);
1680 if (!merge_base
|| merge_base
->next
) {
1681 if (die_on_failure
) {
1682 die(_("failed to find exact merge base"));
1689 rev
[i
] = merge_base
->item
;
1693 rev
[i
] = rev
[2 * i
];
1694 rev_nr
= DIV_ROUND_UP(rev_nr
, 2);
1697 if (!in_merge_bases(base
, rev
[0])) {
1698 if (die_on_failure
) {
1699 die(_("base commit should be the ancestor of revision list"));
1706 for (i
= 0; i
< total
; i
++) {
1707 if (base
== list
[i
]) {
1708 if (die_on_failure
) {
1709 die(_("base commit shouldn't be in revision list"));
1721 define_commit_slab(commit_base
, int);
1723 static void prepare_bases(struct base_tree_info
*bases
,
1724 struct commit
*base
,
1725 struct commit
**list
,
1728 struct commit
*commit
;
1729 struct rev_info revs
;
1730 struct diff_options diffopt
;
1731 struct commit_base commit_base
;
1737 init_commit_base(&commit_base
);
1738 repo_diff_setup(the_repository
, &diffopt
);
1739 diffopt
.flags
.recursive
= 1;
1740 diff_setup_done(&diffopt
);
1742 oidcpy(&bases
->base_commit
, &base
->object
.oid
);
1744 repo_init_revisions(the_repository
, &revs
, NULL
);
1745 revs
.max_parents
= 1;
1746 revs
.topo_order
= 1;
1747 for (i
= 0; i
< total
; i
++) {
1748 list
[i
]->object
.flags
&= ~UNINTERESTING
;
1749 add_pending_object(&revs
, &list
[i
]->object
, "rev_list");
1750 *commit_base_at(&commit_base
, list
[i
]) = 1;
1752 base
->object
.flags
|= UNINTERESTING
;
1753 add_pending_object(&revs
, &base
->object
, "base");
1755 if (prepare_revision_walk(&revs
))
1756 die(_("revision walk setup failed"));
1758 * Traverse the commits list, get prerequisite patch ids
1759 * and stuff them in bases structure.
1761 while ((commit
= get_revision(&revs
)) != NULL
) {
1762 struct object_id oid
;
1763 struct object_id
*patch_id
;
1764 if (*commit_base_at(&commit_base
, commit
))
1766 if (commit_patch_id(commit
, &diffopt
, &oid
, 0, 1))
1767 die(_("cannot get patch id"));
1768 ALLOC_GROW(bases
->patch_id
, bases
->nr_patch_id
+ 1, bases
->alloc_patch_id
);
1769 patch_id
= bases
->patch_id
+ bases
->nr_patch_id
;
1770 oidcpy(patch_id
, &oid
);
1771 bases
->nr_patch_id
++;
1773 clear_commit_base(&commit_base
);
1776 static void print_bases(struct base_tree_info
*bases
, FILE *file
)
1780 /* Only do this once, either for the cover or for the first one */
1781 if (is_null_oid(&bases
->base_commit
))
1784 /* Show the base commit */
1785 fprintf(file
, "\nbase-commit: %s\n", oid_to_hex(&bases
->base_commit
));
1787 /* Show the prerequisite patches */
1788 for (i
= bases
->nr_patch_id
- 1; i
>= 0; i
--)
1789 fprintf(file
, "prerequisite-patch-id: %s\n", oid_to_hex(&bases
->patch_id
[i
]));
1791 free(bases
->patch_id
);
1792 bases
->nr_patch_id
= 0;
1793 bases
->alloc_patch_id
= 0;
1794 oidclr(&bases
->base_commit
);
1797 static const char *diff_title(struct strbuf
*sb
,
1798 const char *reroll_count
,
1799 const char *generic
,
1800 const char *rerolled
)
1804 /* RFC may be v0, so allow -v1 to diff against v0 */
1805 if (reroll_count
&& !strtol_i(reroll_count
, 10, &v
) &&
1807 strbuf_addf(sb
, rerolled
, v
- 1);
1809 strbuf_addstr(sb
, generic
);
1813 static void infer_range_diff_ranges(struct strbuf
*r1
,
1816 struct commit
*origin
,
1817 struct commit
*head
)
1819 const char *head_oid
= oid_to_hex(&head
->object
.oid
);
1820 int prev_is_range
= is_range_diff_range(prev
);
1823 strbuf_addstr(r1
, prev
);
1825 strbuf_addf(r1
, "%s..%s", head_oid
, prev
);
1828 strbuf_addf(r2
, "%s..%s", oid_to_hex(&origin
->object
.oid
), head_oid
);
1829 else if (prev_is_range
)
1830 die(_("failed to infer range-diff origin of current series"));
1832 warning(_("using '%s' as range-diff origin of current series"), prev
);
1833 strbuf_addf(r2
, "%s..%s", prev
, head_oid
);
1837 int cmd_format_patch(int argc
, const char **argv
, const char *prefix
)
1839 struct commit
*commit
;
1840 struct commit
**list
= NULL
;
1841 struct rev_info rev
;
1842 char *to_free
= NULL
;
1843 struct setup_revision_opt s_r_opt
;
1844 int nr
= 0, total
, i
;
1846 int start_number
= -1;
1847 int just_numbers
= 0;
1848 int ignore_if_in_upstream
= 0;
1849 int cover_letter
= -1;
1850 int boundary_count
= 0;
1851 int no_binary_diff
= 0;
1852 int zero_commit
= 0;
1853 struct commit
*origin
= NULL
;
1854 const char *in_reply_to
= NULL
;
1855 struct patch_ids ids
;
1856 struct strbuf buf
= STRBUF_INIT
;
1857 int use_patch_format
= 0;
1859 const char *reroll_count
= NULL
;
1860 char *cover_from_description_arg
= NULL
;
1861 char *branch_name
= NULL
;
1862 char *base_commit
= NULL
;
1863 struct base_tree_info bases
;
1864 struct commit
*base
;
1865 int show_progress
= 0;
1866 struct progress
*progress
= NULL
;
1867 struct oid_array idiff_prev
= OID_ARRAY_INIT
;
1868 struct strbuf idiff_title
= STRBUF_INIT
;
1869 const char *rdiff_prev
= NULL
;
1870 struct strbuf rdiff1
= STRBUF_INIT
;
1871 struct strbuf rdiff2
= STRBUF_INIT
;
1872 struct strbuf rdiff_title
= STRBUF_INIT
;
1873 int creation_factor
= -1;
1875 const struct option builtin_format_patch_options
[] = {
1876 OPT_CALLBACK_F('n', "numbered", &numbered
, NULL
,
1877 N_("use [PATCH n/m] even with a single patch"),
1878 PARSE_OPT_NOARG
, numbered_callback
),
1879 OPT_CALLBACK_F('N', "no-numbered", &numbered
, NULL
,
1880 N_("use [PATCH] even with multiple patches"),
1881 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
, no_numbered_callback
),
1882 OPT_BOOL('s', "signoff", &do_signoff
, N_("add a Signed-off-by trailer")),
1883 OPT_BOOL(0, "stdout", &use_stdout
,
1884 N_("print patches to standard out")),
1885 OPT_BOOL(0, "cover-letter", &cover_letter
,
1886 N_("generate a cover letter")),
1887 OPT_BOOL(0, "numbered-files", &just_numbers
,
1888 N_("use simple number sequence for output file names")),
1889 OPT_STRING(0, "suffix", &fmt_patch_suffix
, N_("sfx"),
1890 N_("use <sfx> instead of '.patch'")),
1891 OPT_INTEGER(0, "start-number", &start_number
,
1892 N_("start numbering patches at <n> instead of 1")),
1893 OPT_STRING('v', "reroll-count", &reroll_count
, N_("reroll-count"),
1894 N_("mark the series as Nth re-roll")),
1895 OPT_INTEGER(0, "filename-max-length", &fmt_patch_name_max
,
1896 N_("max length of output filename")),
1897 OPT_CALLBACK_F(0, "rfc", &rev
, NULL
,
1898 N_("use [RFC PATCH] instead of [PATCH]"),
1899 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
, rfc_callback
),
1900 OPT_STRING(0, "cover-from-description", &cover_from_description_arg
,
1901 N_("cover-from-description-mode"),
1902 N_("generate parts of a cover letter based on a branch's description")),
1903 OPT_CALLBACK_F(0, "subject-prefix", &rev
, N_("prefix"),
1904 N_("use [<prefix>] instead of [PATCH]"),
1905 PARSE_OPT_NONEG
, subject_prefix_callback
),
1906 OPT_CALLBACK_F('o', "output-directory", &output_directory
,
1907 N_("dir"), N_("store resulting files in <dir>"),
1908 PARSE_OPT_NONEG
, output_directory_callback
),
1909 OPT_CALLBACK_F('k', "keep-subject", &rev
, NULL
,
1910 N_("don't strip/add [PATCH]"),
1911 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
, keep_callback
),
1912 OPT_BOOL(0, "no-binary", &no_binary_diff
,
1913 N_("don't output binary diffs")),
1914 OPT_BOOL(0, "zero-commit", &zero_commit
,
1915 N_("output all-zero hash in From header")),
1916 OPT_BOOL(0, "ignore-if-in-upstream", &ignore_if_in_upstream
,
1917 N_("don't include a patch matching a commit upstream")),
1918 OPT_SET_INT_F('p', "no-stat", &use_patch_format
,
1919 N_("show patch format instead of default (patch + stat)"),
1920 1, PARSE_OPT_NONEG
),
1921 OPT_GROUP(N_("Messaging")),
1922 OPT_CALLBACK(0, "add-header", NULL
, N_("header"),
1923 N_("add email header"), header_callback
),
1924 OPT_CALLBACK(0, "to", NULL
, N_("email"), N_("add To: header"), to_callback
),
1925 OPT_CALLBACK(0, "cc", NULL
, N_("email"), N_("add Cc: header"), cc_callback
),
1926 OPT_CALLBACK_F(0, "from", &from
, N_("ident"),
1927 N_("set From address to <ident> (or committer ident if absent)"),
1928 PARSE_OPT_OPTARG
, from_callback
),
1929 OPT_STRING(0, "in-reply-to", &in_reply_to
, N_("message-id"),
1930 N_("make first mail a reply to <message-id>")),
1931 OPT_CALLBACK_F(0, "attach", &rev
, N_("boundary"),
1932 N_("attach the patch"), PARSE_OPT_OPTARG
,
1934 OPT_CALLBACK_F(0, "inline", &rev
, N_("boundary"),
1935 N_("inline the patch"),
1936 PARSE_OPT_OPTARG
| PARSE_OPT_NONEG
,
1938 OPT_CALLBACK_F(0, "thread", &thread
, N_("style"),
1939 N_("enable message threading, styles: shallow, deep"),
1940 PARSE_OPT_OPTARG
, thread_callback
),
1941 OPT_STRING(0, "signature", &signature
, N_("signature"),
1942 N_("add a signature")),
1943 OPT_CALLBACK_F(0, "base", &base_commit
, N_("base-commit"),
1944 N_("add prerequisite tree info to the patch series"),
1946 OPT_FILENAME(0, "signature-file", &signature_file
,
1947 N_("add a signature from a file")),
1948 OPT__QUIET(&quiet
, N_("don't print the patch filenames")),
1949 OPT_BOOL(0, "progress", &show_progress
,
1950 N_("show progress while generating patches")),
1951 OPT_CALLBACK(0, "interdiff", &idiff_prev
, N_("rev"),
1952 N_("show changes against <rev> in cover letter or single patch"),
1953 parse_opt_object_name
),
1954 OPT_STRING(0, "range-diff", &rdiff_prev
, N_("refspec"),
1955 N_("show changes against <refspec> in cover letter or single patch")),
1956 OPT_INTEGER(0, "creation-factor", &creation_factor
,
1957 N_("percentage by which creation is weighted")),
1958 OPT_BOOL(0, "force-in-body-from", &force_in_body_from
,
1959 N_("show in-body From: even if identical to the e-mail header")),
1963 extra_hdr
.strdup_strings
= 1;
1964 extra_to
.strdup_strings
= 1;
1965 extra_cc
.strdup_strings
= 1;
1967 init_log_defaults();
1968 init_display_notes(¬es_opt
);
1969 git_config(git_format_config
, NULL
);
1970 repo_init_revisions(the_repository
, &rev
, prefix
);
1971 git_config(grep_config
, &rev
.grep_filter
);
1973 rev
.show_notes
= show_notes
;
1974 memcpy(&rev
.notes_opt
, ¬es_opt
, sizeof(notes_opt
));
1975 rev
.commit_format
= CMIT_FMT_EMAIL
;
1976 rev
.encode_email_headers
= default_encode_email_headers
;
1977 rev
.expand_tabs_in_log_default
= 0;
1978 rev
.verbose_header
= 1;
1980 rev
.max_parents
= 1;
1981 rev
.diffopt
.flags
.recursive
= 1;
1982 rev
.diffopt
.no_free
= 1;
1983 rev
.subject_prefix
= fmt_patch_subject_prefix
;
1984 memset(&s_r_opt
, 0, sizeof(s_r_opt
));
1985 s_r_opt
.def
= "HEAD";
1986 s_r_opt
.revarg_opt
= REVARG_COMMITTISH
;
1988 if (default_attach
) {
1989 rev
.mime_boundary
= default_attach
;
1994 * Parse the arguments before setup_revisions(), or something
1995 * like "git format-patch -o a123 HEAD^.." may fail; a123 is
1996 * possibly a valid SHA1.
1998 argc
= parse_options(argc
, argv
, prefix
, builtin_format_patch_options
,
1999 builtin_format_patch_usage
,
2000 PARSE_OPT_KEEP_ARGV0
| PARSE_OPT_KEEP_UNKNOWN_OPT
|
2001 PARSE_OPT_KEEP_DASHDASH
);
2003 rev
.force_in_body_from
= force_in_body_from
;
2005 /* Make sure "0000-$sub.patch" gives non-negative length for $sub */
2006 if (fmt_patch_name_max
<= strlen("0000-") + strlen(fmt_patch_suffix
))
2007 fmt_patch_name_max
= strlen("0000-") + strlen(fmt_patch_suffix
);
2009 if (cover_from_description_arg
)
2010 cover_from_description_mode
= parse_cover_from_description(cover_from_description_arg
);
2013 struct strbuf sprefix
= STRBUF_INIT
;
2015 strbuf_addf(&sprefix
, "%s v%s",
2016 rev
.subject_prefix
, reroll_count
);
2017 rev
.reroll_count
= reroll_count
;
2018 rev
.subject_prefix
= strbuf_detach(&sprefix
, NULL
);
2021 for (i
= 0; i
< extra_hdr
.nr
; i
++) {
2022 strbuf_addstr(&buf
, extra_hdr
.items
[i
].string
);
2023 strbuf_addch(&buf
, '\n');
2027 strbuf_addstr(&buf
, "To: ");
2028 for (i
= 0; i
< extra_to
.nr
; i
++) {
2030 strbuf_addstr(&buf
, " ");
2031 strbuf_addstr(&buf
, extra_to
.items
[i
].string
);
2032 if (i
+ 1 < extra_to
.nr
)
2033 strbuf_addch(&buf
, ',');
2034 strbuf_addch(&buf
, '\n');
2038 strbuf_addstr(&buf
, "Cc: ");
2039 for (i
= 0; i
< extra_cc
.nr
; i
++) {
2041 strbuf_addstr(&buf
, " ");
2042 strbuf_addstr(&buf
, extra_cc
.items
[i
].string
);
2043 if (i
+ 1 < extra_cc
.nr
)
2044 strbuf_addch(&buf
, ',');
2045 strbuf_addch(&buf
, '\n');
2048 rev
.extra_headers
= to_free
= strbuf_detach(&buf
, NULL
);
2051 if (split_ident_line(&rev
.from_ident
, from
, strlen(from
)))
2052 die(_("invalid ident line: %s"), from
);
2055 if (start_number
< 0)
2059 * If numbered is set solely due to format.numbered in config,
2060 * and it would conflict with --keep-subject (-k) from the
2061 * command line, reset "numbered".
2063 if (numbered
&& keep_subject
&& !numbered_cmdline_opt
)
2066 if (numbered
&& keep_subject
)
2067 die(_("options '%s' and '%s' cannot be used together"), "-n", "-k");
2068 if (keep_subject
&& subject_prefix
)
2069 die(_("options '%s' and '%s' cannot be used together"), "--subject-prefix/--rfc", "-k");
2070 rev
.preserve_subject
= keep_subject
;
2072 argc
= setup_revisions(argc
, argv
, &rev
, &s_r_opt
);
2074 die(_("unrecognized argument: %s"), argv
[1]);
2076 if (rev
.diffopt
.output_format
& DIFF_FORMAT_NAME
)
2077 die(_("--name-only does not make sense"));
2078 if (rev
.diffopt
.output_format
& DIFF_FORMAT_NAME_STATUS
)
2079 die(_("--name-status does not make sense"));
2080 if (rev
.diffopt
.output_format
& DIFF_FORMAT_CHECKDIFF
)
2081 die(_("--check does not make sense"));
2082 if (rev
.remerge_diff
)
2083 die(_("--remerge-diff does not make sense"));
2085 if (!use_patch_format
&&
2086 (!rev
.diffopt
.output_format
||
2087 rev
.diffopt
.output_format
== DIFF_FORMAT_PATCH
))
2088 rev
.diffopt
.output_format
= DIFF_FORMAT_DIFFSTAT
| DIFF_FORMAT_SUMMARY
;
2089 if (!rev
.diffopt
.stat_width
)
2090 rev
.diffopt
.stat_width
= MAIL_DEFAULT_WRAP
;
2092 /* Always generate a patch */
2093 rev
.diffopt
.output_format
|= DIFF_FORMAT_PATCH
;
2095 rev
.zero_commit
= zero_commit
;
2096 rev
.patch_name_max
= fmt_patch_name_max
;
2098 if (!rev
.diffopt
.flags
.text
&& !no_binary_diff
)
2099 rev
.diffopt
.flags
.binary
= 1;
2102 load_display_notes(&rev
.notes_opt
);
2104 die_for_incompatible_opt3(use_stdout
, "--stdout",
2105 rev
.diffopt
.close_file
, "--output",
2106 !!output_directory
, "--output-directory");
2110 } else if (!rev
.diffopt
.close_file
) {
2113 if (!output_directory
)
2114 output_directory
= config_output_directory
;
2115 output_directory
= set_outdir(prefix
, output_directory
);
2117 if (rev
.diffopt
.use_color
!= GIT_COLOR_ALWAYS
)
2118 rev
.diffopt
.use_color
= GIT_COLOR_NEVER
;
2120 * We consider <outdir> as 'outside of gitdir', therefore avoid
2121 * applying adjust_shared_perm in s-c-l-d.
2123 saved
= get_shared_repository();
2124 set_shared_repository(0);
2125 switch (safe_create_leading_directories_const(output_directory
)) {
2130 die(_("could not create leading directories "
2131 "of '%s'"), output_directory
);
2133 set_shared_repository(saved
);
2134 if (mkdir(output_directory
, 0777) < 0 && errno
!= EEXIST
)
2135 die_errno(_("could not create directory '%s'"),
2139 if (rev
.pending
.nr
== 1) {
2142 if (rev
.max_count
< 0 && !rev
.show_root_diff
) {
2144 * This is traditional behaviour of "git format-patch
2145 * origin" that prepares what the origin side still
2148 rev
.pending
.objects
[0].item
->flags
|= UNINTERESTING
;
2149 add_head_to_pending(&rev
);
2153 * Otherwise, it is "format-patch -22 HEAD", and/or
2154 * "format-patch --root HEAD". The user wants
2155 * get_revision() to do the usual traversal.
2158 if (!strcmp(rev
.pending
.objects
[0].name
, "HEAD"))
2162 const char *ref
, *v
;
2163 ref
= resolve_ref_unsafe("HEAD", RESOLVE_REF_READING
,
2165 if (ref
&& skip_prefix(ref
, "refs/heads/", &v
))
2166 branch_name
= xstrdup(v
);
2168 branch_name
= xstrdup(""); /* no branch */
2173 * We cannot move this anywhere earlier because we do want to
2174 * know if --root was given explicitly from the command line.
2176 rev
.show_root_diff
= 1;
2178 if (ignore_if_in_upstream
) {
2179 /* Don't say anything if head and upstream are the same. */
2180 if (rev
.pending
.nr
== 2) {
2181 struct object_array_entry
*o
= rev
.pending
.objects
;
2182 if (oideq(&o
[0].item
->oid
, &o
[1].item
->oid
))
2185 get_patch_ids(&rev
, &ids
);
2188 if (prepare_revision_walk(&rev
))
2189 die(_("revision walk setup failed"));
2191 while ((commit
= get_revision(&rev
)) != NULL
) {
2192 if (commit
->object
.flags
& BOUNDARY
) {
2194 origin
= (boundary_count
== 1) ? commit
: NULL
;
2198 if (ignore_if_in_upstream
&& has_commit_patch_id(commit
, &ids
))
2202 REALLOC_ARRAY(list
, nr
);
2203 list
[nr
- 1] = commit
;
2209 if (cover_letter
== -1) {
2210 if (config_cover_letter
== COVER_AUTO
)
2211 cover_letter
= (total
> 1);
2213 cover_letter
= (config_cover_letter
== COVER_ON
);
2215 if (!keep_subject
&& auto_number
&& (total
> 1 || cover_letter
))
2218 rev
.total
= total
+ start_number
- 1;
2220 if (idiff_prev
.nr
) {
2221 if (!cover_letter
&& total
!= 1)
2222 die(_("--interdiff requires --cover-letter or single patch"));
2223 rev
.idiff_oid1
= &idiff_prev
.oid
[idiff_prev
.nr
- 1];
2224 rev
.idiff_oid2
= get_commit_tree_oid(list
[0]);
2225 rev
.idiff_title
= diff_title(&idiff_title
, reroll_count
,
2227 _("Interdiff against v%d:"));
2230 if (creation_factor
< 0)
2231 creation_factor
= RANGE_DIFF_CREATION_FACTOR_DEFAULT
;
2232 else if (!rdiff_prev
)
2233 die(_("the option '%s' requires '%s'"), "--creation-factor", "--range-diff");
2236 if (!cover_letter
&& total
!= 1)
2237 die(_("--range-diff requires --cover-letter or single patch"));
2239 infer_range_diff_ranges(&rdiff1
, &rdiff2
, rdiff_prev
,
2241 rev
.rdiff1
= rdiff1
.buf
;
2242 rev
.rdiff2
= rdiff2
.buf
;
2243 rev
.creation_factor
= creation_factor
;
2244 rev
.rdiff_title
= diff_title(&rdiff_title
, reroll_count
,
2246 _("Range-diff against v%d:"));
2250 ; /* --no-signature inhibits all signatures */
2251 } else if (signature
&& signature
!= git_version_string
) {
2252 ; /* non-default signature already set */
2253 } else if (signature_file
) {
2254 struct strbuf buf
= STRBUF_INIT
;
2256 if (strbuf_read_file(&buf
, signature_file
, 128) < 0)
2257 die_errno(_("unable to read signature file '%s'"), signature_file
);
2258 signature
= strbuf_detach(&buf
, NULL
);
2261 memset(&bases
, 0, sizeof(bases
));
2262 base
= get_base_commit(base_commit
, list
, nr
);
2264 reset_revision_walk();
2265 clear_object_flags(UNINTERESTING
);
2266 prepare_bases(&bases
, base
, list
, nr
);
2269 if (in_reply_to
|| thread
|| cover_letter
) {
2270 rev
.ref_message_ids
= xmalloc(sizeof(*rev
.ref_message_ids
));
2271 string_list_init_nodup(rev
.ref_message_ids
);
2274 const char *msgid
= clean_message_id(in_reply_to
);
2275 string_list_append(rev
.ref_message_ids
, msgid
);
2277 rev
.numbered_files
= just_numbers
;
2278 rev
.patch_suffix
= fmt_patch_suffix
;
2281 gen_message_id(&rev
, "cover");
2282 make_cover_letter(&rev
, !!output_directory
,
2283 origin
, nr
, list
, branch_name
, quiet
);
2284 print_bases(&bases
, rev
.diffopt
.file
);
2285 print_signature(rev
.diffopt
.file
);
2288 /* interdiff/range-diff in cover-letter; omit from patches */
2289 rev
.idiff_oid1
= NULL
;
2292 rev
.add_signoff
= do_signoff
;
2295 progress
= start_delayed_progress(_("Generating patches"), total
);
2298 display_progress(progress
, total
- nr
);
2300 rev
.nr
= total
- nr
+ (start_number
- 1);
2301 /* Make the second and subsequent mails replies to the first */
2303 /* Have we already had a message ID? */
2304 if (rev
.message_id
) {
2306 * For deep threading: make every mail
2307 * a reply to the previous one, no
2308 * matter what other options are set.
2310 * For shallow threading:
2312 * Without --cover-letter and
2313 * --in-reply-to, make every mail a
2314 * reply to the one before.
2316 * With --in-reply-to but no
2317 * --cover-letter, make every mail a
2318 * reply to the <reply-to>.
2320 * With --cover-letter, make every
2321 * mail but the cover letter a reply
2322 * to the cover letter. The cover
2323 * letter is a reply to the
2324 * --in-reply-to, if specified.
2326 if (thread
== THREAD_SHALLOW
2327 && rev
.ref_message_ids
->nr
> 0
2328 && (!cover_letter
|| rev
.nr
> 1))
2329 free(rev
.message_id
);
2331 string_list_append(rev
.ref_message_ids
,
2334 gen_message_id(&rev
, oid_to_hex(&commit
->object
.oid
));
2337 if (output_directory
&&
2338 open_next_file(rev
.numbered_files
? NULL
: commit
, NULL
, &rev
, quiet
))
2339 die(_("failed to create output files"));
2340 shown
= log_tree_commit(&rev
, commit
);
2341 free_commit_buffer(the_repository
->parsed_objects
,
2344 /* We put one extra blank line between formatted
2345 * patches and this flag is used by log-tree code
2346 * to see if it needs to emit a LF before showing
2347 * the log; when using one file per patch, we do
2348 * not want the extra blank line.
2350 if (output_directory
)
2353 print_bases(&bases
, rev
.diffopt
.file
);
2354 if (rev
.mime_boundary
)
2355 fprintf(rev
.diffopt
.file
, "\n--%s%s--\n\n\n",
2356 mime_boundary_leader
,
2359 print_signature(rev
.diffopt
.file
);
2361 if (output_directory
)
2362 fclose(rev
.diffopt
.file
);
2364 stop_progress(&progress
);
2367 string_list_clear(&extra_to
, 0);
2368 string_list_clear(&extra_cc
, 0);
2369 string_list_clear(&extra_hdr
, 0);
2370 if (ignore_if_in_upstream
)
2371 free_patch_ids(&ids
);
2374 oid_array_clear(&idiff_prev
);
2375 strbuf_release(&idiff_title
);
2376 strbuf_release(&rdiff1
);
2377 strbuf_release(&rdiff2
);
2378 strbuf_release(&rdiff_title
);
2380 if (rev
.ref_message_ids
)
2381 string_list_clear(rev
.ref_message_ids
, 0);
2382 free(rev
.ref_message_ids
);
2383 return cmd_log_deinit(0, &rev
);
2386 static int add_pending_commit(const char *arg
, struct rev_info
*revs
, int flags
)
2388 struct object_id oid
;
2389 if (get_oid(arg
, &oid
) == 0) {
2390 struct commit
*commit
= lookup_commit_reference(the_repository
,
2393 commit
->object
.flags
|= flags
;
2394 add_pending_object(revs
, &commit
->object
, arg
);
2401 static const char * const cherry_usage
[] = {
2402 N_("git cherry [-v] [<upstream> [<head> [<limit>]]]"),
2406 static void print_commit(char sign
, struct commit
*commit
, int verbose
,
2407 int abbrev
, FILE *file
)
2410 fprintf(file
, "%c %s\n", sign
,
2411 find_unique_abbrev(&commit
->object
.oid
, abbrev
));
2413 struct strbuf buf
= STRBUF_INIT
;
2414 pp_commit_easy(CMIT_FMT_ONELINE
, commit
, &buf
);
2415 fprintf(file
, "%c %s %s\n", sign
,
2416 find_unique_abbrev(&commit
->object
.oid
, abbrev
),
2418 strbuf_release(&buf
);
2422 int cmd_cherry(int argc
, const char **argv
, const char *prefix
)
2424 struct rev_info revs
;
2425 struct patch_ids ids
;
2426 struct commit
*commit
;
2427 struct commit_list
*list
= NULL
;
2428 struct branch
*current_branch
;
2429 const char *upstream
;
2430 const char *head
= "HEAD";
2431 const char *limit
= NULL
;
2432 int verbose
= 0, abbrev
= 0;
2434 struct option options
[] = {
2435 OPT__ABBREV(&abbrev
),
2436 OPT__VERBOSE(&verbose
, N_("be verbose")),
2440 argc
= parse_options(argc
, argv
, prefix
, options
, cherry_usage
, 0);
2453 current_branch
= branch_get(NULL
);
2454 upstream
= branch_get_upstream(current_branch
, NULL
);
2456 fprintf(stderr
, _("Could not find a tracked"
2457 " remote branch, please"
2458 " specify <upstream> manually.\n"));
2459 usage_with_options(cherry_usage
, options
);
2463 repo_init_revisions(the_repository
, &revs
, prefix
);
2464 revs
.max_parents
= 1;
2466 if (add_pending_commit(head
, &revs
, 0))
2467 die(_("unknown commit %s"), head
);
2468 if (add_pending_commit(upstream
, &revs
, UNINTERESTING
))
2469 die(_("unknown commit %s"), upstream
);
2471 /* Don't say anything if head and upstream are the same. */
2472 if (revs
.pending
.nr
== 2) {
2473 struct object_array_entry
*o
= revs
.pending
.objects
;
2474 if (oideq(&o
[0].item
->oid
, &o
[1].item
->oid
))
2478 get_patch_ids(&revs
, &ids
);
2480 if (limit
&& add_pending_commit(limit
, &revs
, UNINTERESTING
))
2481 die(_("unknown commit %s"), limit
);
2483 /* reverse the list of commits */
2484 if (prepare_revision_walk(&revs
))
2485 die(_("revision walk setup failed"));
2486 while ((commit
= get_revision(&revs
)) != NULL
) {
2487 commit_list_insert(commit
, &list
);
2493 commit
= list
->item
;
2494 if (has_commit_patch_id(commit
, &ids
))
2496 print_commit(sign
, commit
, verbose
, abbrev
, revs
.diffopt
.file
);
2500 free_patch_ids(&ids
);