2 * Builtin "git log" and related commands (show, whatchanged)
4 * (C) Copyright 2006 Linus Torvalds
10 #include "object-store.h"
14 #include "diff-merges.h"
19 #include "reflog-walk.h"
20 #include "patch-ids.h"
21 #include "run-command.h"
24 #include "string-list.h"
25 #include "parse-options.h"
28 #include "streaming.h"
31 #include "gpg-interface.h"
33 #include "commit-slab.h"
34 #include "repository.h"
35 #include "commit-reach.h"
36 #include "range-diff.h"
37 #include "tmp-objdir.h"
39 #define MAIL_DEFAULT_WRAP 72
40 #define COVER_FROM_AUTO_MAX_SUBJECT_LEN 100
41 #define FORMAT_PATCH_NAME_MAX_DEFAULT 64
43 /* Set a default date-time format for git log ("log.date" config variable) */
44 static const char *default_date_mode
= NULL
;
46 static int default_abbrev_commit
;
47 static int default_show_root
= 1;
48 static int default_follow
;
49 static int default_show_signature
;
50 static int default_encode_email_headers
= 1;
51 static int decoration_style
;
52 static int decoration_given
;
53 static int use_mailmap_config
= 1;
54 static unsigned int force_in_body_from
;
55 static int stdout_mboxrd
;
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
);
1081 if (!strcmp(var
, "format.mboxrd")) {
1082 stdout_mboxrd
= git_config_bool(var
, value
);
1086 return git_log_config(var
, value
, cb
);
1089 static const char *output_directory
= NULL
;
1090 static int outdir_offset
;
1092 static int open_next_file(struct commit
*commit
, const char *subject
,
1093 struct rev_info
*rev
, int quiet
)
1095 struct strbuf filename
= STRBUF_INIT
;
1097 if (output_directory
) {
1098 strbuf_addstr(&filename
, output_directory
);
1099 strbuf_complete(&filename
, '/');
1102 if (rev
->numbered_files
)
1103 strbuf_addf(&filename
, "%d", rev
->nr
);
1105 fmt_output_commit(&filename
, commit
, rev
);
1107 fmt_output_subject(&filename
, subject
, rev
);
1110 printf("%s\n", filename
.buf
+ outdir_offset
);
1112 if (!(rev
->diffopt
.file
= fopen(filename
.buf
, "w"))) {
1113 error_errno(_("cannot open patch file %s"), filename
.buf
);
1114 strbuf_release(&filename
);
1118 strbuf_release(&filename
);
1122 static void get_patch_ids(struct rev_info
*rev
, struct patch_ids
*ids
)
1124 struct rev_info check_rev
;
1125 struct commit
*commit
, *c1
, *c2
;
1126 struct object
*o1
, *o2
;
1127 unsigned flags1
, flags2
;
1129 if (rev
->pending
.nr
!= 2)
1130 die(_("need exactly one range"));
1132 o1
= rev
->pending
.objects
[0].item
;
1133 o2
= rev
->pending
.objects
[1].item
;
1136 c1
= lookup_commit_reference(the_repository
, &o1
->oid
);
1137 c2
= lookup_commit_reference(the_repository
, &o2
->oid
);
1139 if ((flags1
& UNINTERESTING
) == (flags2
& UNINTERESTING
))
1140 die(_("not a range"));
1142 init_patch_ids(the_repository
, ids
);
1144 /* given a range a..b get all patch ids for b..a */
1145 repo_init_revisions(the_repository
, &check_rev
, rev
->prefix
);
1146 check_rev
.max_parents
= 1;
1147 o1
->flags
^= UNINTERESTING
;
1148 o2
->flags
^= UNINTERESTING
;
1149 add_pending_object(&check_rev
, o1
, "o1");
1150 add_pending_object(&check_rev
, o2
, "o2");
1151 if (prepare_revision_walk(&check_rev
))
1152 die(_("revision walk setup failed"));
1154 while ((commit
= get_revision(&check_rev
)) != NULL
) {
1155 add_commit_patch_id(commit
, ids
);
1158 /* reset for next revision walk */
1159 clear_commit_marks(c1
, SEEN
| UNINTERESTING
| SHOWN
| ADDED
);
1160 clear_commit_marks(c2
, SEEN
| UNINTERESTING
| SHOWN
| ADDED
);
1165 static void gen_message_id(struct rev_info
*info
, char *base
)
1167 struct strbuf buf
= STRBUF_INIT
;
1168 strbuf_addf(&buf
, "%s.%"PRItime
".git.%s", base
,
1169 (timestamp_t
) time(NULL
),
1170 git_committer_info(IDENT_NO_NAME
|IDENT_NO_DATE
|IDENT_STRICT
));
1171 info
->message_id
= strbuf_detach(&buf
, NULL
);
1174 static void print_signature(FILE *file
)
1176 if (!signature
|| !*signature
)
1179 fprintf(file
, "-- \n%s", signature
);
1180 if (signature
[strlen(signature
)-1] != '\n')
1185 static char *find_branch_name(struct rev_info
*rev
)
1187 int i
, positive
= -1;
1188 struct object_id branch_oid
;
1189 const struct object_id
*tip_oid
;
1190 const char *ref
, *v
;
1191 char *full_ref
, *branch
= NULL
;
1193 for (i
= 0; i
< rev
->cmdline
.nr
; i
++) {
1194 if (rev
->cmdline
.rev
[i
].flags
& UNINTERESTING
)
1203 ref
= rev
->cmdline
.rev
[positive
].name
;
1204 tip_oid
= &rev
->cmdline
.rev
[positive
].item
->oid
;
1205 if (dwim_ref(ref
, strlen(ref
), &branch_oid
, &full_ref
, 0) &&
1206 skip_prefix(full_ref
, "refs/heads/", &v
) &&
1207 oideq(tip_oid
, &branch_oid
))
1208 branch
= xstrdup(v
);
1213 static void show_diffstat(struct rev_info
*rev
,
1214 struct commit
*origin
, struct commit
*head
)
1216 struct diff_options opts
;
1218 memcpy(&opts
, &rev
->diffopt
, sizeof(opts
));
1219 opts
.output_format
= DIFF_FORMAT_SUMMARY
| DIFF_FORMAT_DIFFSTAT
;
1220 diff_setup_done(&opts
);
1222 diff_tree_oid(get_commit_tree_oid(origin
),
1223 get_commit_tree_oid(head
),
1225 diffcore_std(&opts
);
1228 fprintf(rev
->diffopt
.file
, "\n");
1231 static void prepare_cover_text(struct pretty_print_context
*pp
,
1232 const char *branch_name
,
1234 const char *encoding
,
1237 const char *subject
= "*** SUBJECT HERE ***";
1238 const char *body
= "*** BLURB HERE ***";
1239 struct strbuf description_sb
= STRBUF_INIT
;
1240 struct strbuf subject_sb
= STRBUF_INIT
;
1242 if (cover_from_description_mode
== COVER_FROM_NONE
)
1245 if (branch_name
&& *branch_name
)
1246 read_branch_desc(&description_sb
, branch_name
);
1247 if (!description_sb
.len
)
1250 if (cover_from_description_mode
== COVER_FROM_SUBJECT
||
1251 cover_from_description_mode
== COVER_FROM_AUTO
)
1252 body
= format_subject(&subject_sb
, description_sb
.buf
, " ");
1254 if (cover_from_description_mode
== COVER_FROM_MESSAGE
||
1255 (cover_from_description_mode
== COVER_FROM_AUTO
&&
1256 subject_sb
.len
> COVER_FROM_AUTO_MAX_SUBJECT_LEN
))
1257 body
= description_sb
.buf
;
1259 subject
= subject_sb
.buf
;
1262 pp_title_line(pp
, &subject
, sb
, encoding
, need_8bit_cte
);
1263 pp_remainder(pp
, &body
, sb
, 0);
1265 strbuf_release(&description_sb
);
1266 strbuf_release(&subject_sb
);
1269 static int get_notes_refs(struct string_list_item
*item
, void *arg
)
1271 strvec_pushf(arg
, "--notes=%s", item
->string
);
1275 static void get_notes_args(struct strvec
*arg
, struct rev_info
*rev
)
1277 if (!rev
->show_notes
) {
1278 strvec_push(arg
, "--no-notes");
1279 } else if (rev
->notes_opt
.use_default_notes
> 0 ||
1280 (rev
->notes_opt
.use_default_notes
== -1 &&
1281 !rev
->notes_opt
.extra_notes_refs
.nr
)) {
1282 strvec_push(arg
, "--notes");
1284 for_each_string_list(&rev
->notes_opt
.extra_notes_refs
, get_notes_refs
, arg
);
1288 static void make_cover_letter(struct rev_info
*rev
, int use_separate_file
,
1289 struct commit
*origin
,
1290 int nr
, struct commit
**list
,
1291 const char *branch_name
,
1294 const char *committer
;
1295 struct shortlog log
;
1296 struct strbuf sb
= STRBUF_INIT
;
1298 const char *encoding
= "UTF-8";
1299 int need_8bit_cte
= 0;
1300 struct pretty_print_context pp
= {0};
1301 struct commit
*head
= list
[0];
1303 if (!cmit_fmt_is_mail(rev
->commit_format
))
1304 die(_("cover letter needs email format"));
1306 committer
= git_committer_info(0);
1308 if (use_separate_file
&&
1309 open_next_file(NULL
, rev
->numbered_files
? NULL
: "cover-letter", rev
, quiet
))
1310 die(_("failed to create cover-letter file"));
1312 log_write_email_headers(rev
, head
, &pp
.after_subject
, &need_8bit_cte
, 0);
1314 for (i
= 0; !need_8bit_cte
&& i
< nr
; i
++) {
1315 const char *buf
= get_commit_buffer(list
[i
], NULL
);
1316 if (has_non_ascii(buf
))
1318 unuse_commit_buffer(list
[i
], buf
);
1322 branch_name
= find_branch_name(rev
);
1324 pp
.fmt
= CMIT_FMT_EMAIL
;
1325 pp
.date_mode
.type
= DATE_RFC2822
;
1327 pp
.print_email_subject
= 1;
1328 pp_user_info(&pp
, NULL
, &sb
, committer
, encoding
);
1329 prepare_cover_text(&pp
, branch_name
, &sb
, encoding
, need_8bit_cte
);
1330 fprintf(rev
->diffopt
.file
, "%s\n", sb
.buf
);
1332 strbuf_release(&sb
);
1334 shortlog_init(&log
);
1336 log
.wrap
= MAIL_DEFAULT_WRAP
;
1339 log
.file
= rev
->diffopt
.file
;
1340 log
.groups
= SHORTLOG_GROUP_AUTHOR
;
1341 shortlog_finish_setup(&log
);
1342 for (i
= 0; i
< nr
; i
++)
1343 shortlog_add_commit(&log
, list
[i
]);
1345 shortlog_output(&log
);
1347 /* We can only do diffstat with a unique reference point */
1349 show_diffstat(rev
, origin
, head
);
1351 if (rev
->idiff_oid1
) {
1352 fprintf_ln(rev
->diffopt
.file
, "%s", rev
->idiff_title
);
1353 show_interdiff(rev
->idiff_oid1
, rev
->idiff_oid2
, 0,
1359 * Pass minimum required diff-options to range-diff; others
1360 * can be added later if deemed desirable.
1362 struct diff_options opts
;
1363 struct strvec other_arg
= STRVEC_INIT
;
1364 struct range_diff_options range_diff_opts
= {
1365 .creation_factor
= rev
->creation_factor
,
1368 .other_arg
= &other_arg
1372 opts
.file
= rev
->diffopt
.file
;
1373 opts
.use_color
= rev
->diffopt
.use_color
;
1374 diff_setup_done(&opts
);
1375 fprintf_ln(rev
->diffopt
.file
, "%s", rev
->rdiff_title
);
1376 get_notes_args(&other_arg
, rev
);
1377 show_range_diff(rev
->rdiff1
, rev
->rdiff2
, &range_diff_opts
);
1378 strvec_clear(&other_arg
);
1382 static const char *clean_message_id(const char *msg_id
)
1385 const char *a
, *z
, *m
;
1388 while ((ch
= *m
) && (isspace(ch
) || (ch
== '<')))
1393 if (!isspace(ch
) && (ch
!= '>'))
1398 die(_("insane in-reply-to: %s"), msg_id
);
1401 return xmemdupz(a
, z
- a
);
1404 static const char *set_outdir(const char *prefix
, const char *output_directory
)
1406 if (output_directory
&& is_absolute_path(output_directory
))
1407 return output_directory
;
1409 if (!prefix
|| !*prefix
) {
1410 if (output_directory
)
1411 return output_directory
;
1412 /* The user did not explicitly ask for "./" */
1417 outdir_offset
= strlen(prefix
);
1418 if (!output_directory
)
1421 return prefix_filename(prefix
, output_directory
);
1424 static const char * const builtin_format_patch_usage
[] = {
1425 N_("git format-patch [<options>] [<since> | <revision-range>]"),
1429 static int keep_subject
= 0;
1431 static int keep_callback(const struct option
*opt
, const char *arg
, int unset
)
1433 BUG_ON_OPT_NEG(unset
);
1434 BUG_ON_OPT_ARG(arg
);
1435 ((struct rev_info
*)opt
->value
)->total
= -1;
1440 static int subject_prefix
= 0;
1442 static int subject_prefix_callback(const struct option
*opt
, const char *arg
,
1445 BUG_ON_OPT_NEG(unset
);
1447 ((struct rev_info
*)opt
->value
)->subject_prefix
= arg
;
1451 static int rfc_callback(const struct option
*opt
, const char *arg
, int unset
)
1453 BUG_ON_OPT_NEG(unset
);
1454 BUG_ON_OPT_ARG(arg
);
1455 return subject_prefix_callback(opt
, "RFC PATCH", unset
);
1458 static int numbered_cmdline_opt
= 0;
1460 static int numbered_callback(const struct option
*opt
, const char *arg
,
1463 BUG_ON_OPT_ARG(arg
);
1464 *(int *)opt
->value
= numbered_cmdline_opt
= unset
? 0 : 1;
1470 static int no_numbered_callback(const struct option
*opt
, const char *arg
,
1473 BUG_ON_OPT_NEG(unset
);
1474 return numbered_callback(opt
, arg
, 1);
1477 static int output_directory_callback(const struct option
*opt
, const char *arg
,
1480 const char **dir
= (const char **)opt
->value
;
1481 BUG_ON_OPT_NEG(unset
);
1483 die(_("two output directories?"));
1488 static int thread_callback(const struct option
*opt
, const char *arg
, int unset
)
1490 enum thread_level
*thread
= (enum thread_level
*)opt
->value
;
1492 *thread
= THREAD_UNSET
;
1493 else if (!arg
|| !strcmp(arg
, "shallow"))
1494 *thread
= THREAD_SHALLOW
;
1495 else if (!strcmp(arg
, "deep"))
1496 *thread
= THREAD_DEEP
;
1498 * Please update _git_formatpatch() in git-completion.bash
1499 * when you add new options.
1506 static int attach_callback(const struct option
*opt
, const char *arg
, int unset
)
1508 struct rev_info
*rev
= (struct rev_info
*)opt
->value
;
1510 rev
->mime_boundary
= NULL
;
1512 rev
->mime_boundary
= arg
;
1514 rev
->mime_boundary
= git_version_string
;
1515 rev
->no_inline
= unset
? 0 : 1;
1519 static int inline_callback(const struct option
*opt
, const char *arg
, int unset
)
1521 struct rev_info
*rev
= (struct rev_info
*)opt
->value
;
1523 rev
->mime_boundary
= NULL
;
1525 rev
->mime_boundary
= arg
;
1527 rev
->mime_boundary
= git_version_string
;
1532 static int header_callback(const struct option
*opt
, const char *arg
, int unset
)
1535 string_list_clear(&extra_hdr
, 0);
1536 string_list_clear(&extra_to
, 0);
1537 string_list_clear(&extra_cc
, 0);
1544 static int to_callback(const struct option
*opt
, const char *arg
, int unset
)
1547 string_list_clear(&extra_to
, 0);
1549 string_list_append(&extra_to
, arg
);
1553 static int cc_callback(const struct option
*opt
, const char *arg
, int unset
)
1556 string_list_clear(&extra_cc
, 0);
1558 string_list_append(&extra_cc
, arg
);
1562 static int from_callback(const struct option
*opt
, const char *arg
, int unset
)
1564 char **from
= opt
->value
;
1571 *from
= xstrdup(arg
);
1573 *from
= xstrdup(git_committer_info(IDENT_NO_DATE
));
1577 static int base_callback(const struct option
*opt
, const char *arg
, int unset
)
1579 const char **base_commit
= opt
->value
;
1582 auto_base
= AUTO_BASE_NEVER
;
1583 *base_commit
= NULL
;
1584 } else if (!strcmp(arg
, "auto")) {
1585 auto_base
= AUTO_BASE_ALWAYS
;
1586 *base_commit
= NULL
;
1588 auto_base
= AUTO_BASE_NEVER
;
1594 struct base_tree_info
{
1595 struct object_id base_commit
;
1596 int nr_patch_id
, alloc_patch_id
;
1597 struct object_id
*patch_id
;
1600 static struct commit
*get_base_commit(const char *base_commit
,
1601 struct commit
**list
,
1604 struct commit
*base
= NULL
;
1605 struct commit
**rev
;
1606 int i
= 0, rev_nr
= 0, auto_select
, die_on_failure
;
1608 switch (auto_base
) {
1609 case AUTO_BASE_NEVER
:
1614 /* no base information is requested */
1618 case AUTO_BASE_ALWAYS
:
1619 case AUTO_BASE_WHEN_ABLE
:
1621 BUG("requested automatic base selection but a commit was provided");
1624 die_on_failure
= auto_base
== AUTO_BASE_ALWAYS
;
1628 BUG("unexpected automatic base selection method");
1632 base
= lookup_commit_reference_by_name(base_commit
);
1634 die(_("unknown commit %s"), base_commit
);
1636 struct branch
*curr_branch
= branch_get(NULL
);
1637 const char *upstream
= branch_get_upstream(curr_branch
, NULL
);
1639 struct commit_list
*base_list
;
1640 struct commit
*commit
;
1641 struct object_id oid
;
1643 if (get_oid(upstream
, &oid
)) {
1645 die(_("failed to resolve '%s' as a valid ref"), upstream
);
1649 commit
= lookup_commit_or_die(&oid
, "upstream base");
1650 base_list
= get_merge_bases_many(commit
, total
, list
);
1651 /* There should be one and only one merge base. */
1652 if (!base_list
|| base_list
->next
) {
1653 if (die_on_failure
) {
1654 die(_("could not find exact merge base"));
1656 free_commit_list(base_list
);
1660 base
= base_list
->item
;
1661 free_commit_list(base_list
);
1664 die(_("failed to get upstream, if you want to record base commit automatically,\n"
1665 "please use git branch --set-upstream-to to track a remote branch.\n"
1666 "Or you could specify base commit by --base=<base-commit-id> manually"));
1672 ALLOC_ARRAY(rev
, total
);
1673 for (i
= 0; i
< total
; i
++)
1678 * Get merge base through pair-wise computations
1679 * and store it in rev[0].
1681 while (rev_nr
> 1) {
1682 for (i
= 0; i
< rev_nr
/ 2; i
++) {
1683 struct commit_list
*merge_base
;
1684 merge_base
= get_merge_bases(rev
[2 * i
], rev
[2 * i
+ 1]);
1685 if (!merge_base
|| merge_base
->next
) {
1686 if (die_on_failure
) {
1687 die(_("failed to find exact merge base"));
1694 rev
[i
] = merge_base
->item
;
1698 rev
[i
] = rev
[2 * i
];
1699 rev_nr
= DIV_ROUND_UP(rev_nr
, 2);
1702 if (!in_merge_bases(base
, rev
[0])) {
1703 if (die_on_failure
) {
1704 die(_("base commit should be the ancestor of revision list"));
1711 for (i
= 0; i
< total
; i
++) {
1712 if (base
== list
[i
]) {
1713 if (die_on_failure
) {
1714 die(_("base commit shouldn't be in revision list"));
1726 define_commit_slab(commit_base
, int);
1728 static void prepare_bases(struct base_tree_info
*bases
,
1729 struct commit
*base
,
1730 struct commit
**list
,
1733 struct commit
*commit
;
1734 struct rev_info revs
;
1735 struct diff_options diffopt
;
1736 struct commit_base commit_base
;
1742 init_commit_base(&commit_base
);
1743 repo_diff_setup(the_repository
, &diffopt
);
1744 diffopt
.flags
.recursive
= 1;
1745 diff_setup_done(&diffopt
);
1747 oidcpy(&bases
->base_commit
, &base
->object
.oid
);
1749 repo_init_revisions(the_repository
, &revs
, NULL
);
1750 revs
.max_parents
= 1;
1751 revs
.topo_order
= 1;
1752 for (i
= 0; i
< total
; i
++) {
1753 list
[i
]->object
.flags
&= ~UNINTERESTING
;
1754 add_pending_object(&revs
, &list
[i
]->object
, "rev_list");
1755 *commit_base_at(&commit_base
, list
[i
]) = 1;
1757 base
->object
.flags
|= UNINTERESTING
;
1758 add_pending_object(&revs
, &base
->object
, "base");
1760 if (prepare_revision_walk(&revs
))
1761 die(_("revision walk setup failed"));
1763 * Traverse the commits list, get prerequisite patch ids
1764 * and stuff them in bases structure.
1766 while ((commit
= get_revision(&revs
)) != NULL
) {
1767 struct object_id oid
;
1768 struct object_id
*patch_id
;
1769 if (*commit_base_at(&commit_base
, commit
))
1771 if (commit_patch_id(commit
, &diffopt
, &oid
, 0))
1772 die(_("cannot get patch id"));
1773 ALLOC_GROW(bases
->patch_id
, bases
->nr_patch_id
+ 1, bases
->alloc_patch_id
);
1774 patch_id
= bases
->patch_id
+ bases
->nr_patch_id
;
1775 oidcpy(patch_id
, &oid
);
1776 bases
->nr_patch_id
++;
1778 clear_commit_base(&commit_base
);
1781 static void print_bases(struct base_tree_info
*bases
, FILE *file
)
1785 /* Only do this once, either for the cover or for the first one */
1786 if (is_null_oid(&bases
->base_commit
))
1789 /* Show the base commit */
1790 fprintf(file
, "\nbase-commit: %s\n", oid_to_hex(&bases
->base_commit
));
1792 /* Show the prerequisite patches */
1793 for (i
= bases
->nr_patch_id
- 1; i
>= 0; i
--)
1794 fprintf(file
, "prerequisite-patch-id: %s\n", oid_to_hex(&bases
->patch_id
[i
]));
1796 free(bases
->patch_id
);
1797 bases
->nr_patch_id
= 0;
1798 bases
->alloc_patch_id
= 0;
1799 oidclr(&bases
->base_commit
);
1802 static const char *diff_title(struct strbuf
*sb
,
1803 const char *reroll_count
,
1804 const char *generic
,
1805 const char *rerolled
)
1809 /* RFC may be v0, so allow -v1 to diff against v0 */
1810 if (reroll_count
&& !strtol_i(reroll_count
, 10, &v
) &&
1812 strbuf_addf(sb
, rerolled
, v
- 1);
1814 strbuf_addstr(sb
, generic
);
1818 static void infer_range_diff_ranges(struct strbuf
*r1
,
1821 struct commit
*origin
,
1822 struct commit
*head
)
1824 const char *head_oid
= oid_to_hex(&head
->object
.oid
);
1825 int prev_is_range
= is_range_diff_range(prev
);
1828 strbuf_addstr(r1
, prev
);
1830 strbuf_addf(r1
, "%s..%s", head_oid
, prev
);
1833 strbuf_addf(r2
, "%s..%s", oid_to_hex(&origin
->object
.oid
), head_oid
);
1834 else if (prev_is_range
)
1835 die(_("failed to infer range-diff origin of current series"));
1837 warning(_("using '%s' as range-diff origin of current series"), prev
);
1838 strbuf_addf(r2
, "%s..%s", prev
, head_oid
);
1842 int cmd_format_patch(int argc
, const char **argv
, const char *prefix
)
1844 struct commit
*commit
;
1845 struct commit
**list
= NULL
;
1846 struct rev_info rev
;
1847 char *to_free
= NULL
;
1848 struct setup_revision_opt s_r_opt
;
1849 int nr
= 0, total
, i
;
1851 int start_number
= -1;
1852 int just_numbers
= 0;
1853 int ignore_if_in_upstream
= 0;
1854 int cover_letter
= -1;
1855 int boundary_count
= 0;
1856 int no_binary_diff
= 0;
1857 int zero_commit
= 0;
1858 struct commit
*origin
= NULL
;
1859 const char *in_reply_to
= NULL
;
1860 struct patch_ids ids
;
1861 struct strbuf buf
= STRBUF_INIT
;
1862 int use_patch_format
= 0;
1864 const char *reroll_count
= NULL
;
1865 char *cover_from_description_arg
= NULL
;
1866 char *branch_name
= NULL
;
1867 char *base_commit
= NULL
;
1868 struct base_tree_info bases
;
1869 struct commit
*base
;
1870 int show_progress
= 0;
1871 struct progress
*progress
= NULL
;
1872 struct oid_array idiff_prev
= OID_ARRAY_INIT
;
1873 struct strbuf idiff_title
= STRBUF_INIT
;
1874 const char *rdiff_prev
= NULL
;
1875 struct strbuf rdiff1
= STRBUF_INIT
;
1876 struct strbuf rdiff2
= STRBUF_INIT
;
1877 struct strbuf rdiff_title
= STRBUF_INIT
;
1878 struct strbuf sprefix
= STRBUF_INIT
;
1879 int creation_factor
= -1;
1881 const struct option builtin_format_patch_options
[] = {
1882 OPT_CALLBACK_F('n', "numbered", &numbered
, NULL
,
1883 N_("use [PATCH n/m] even with a single patch"),
1884 PARSE_OPT_NOARG
, numbered_callback
),
1885 OPT_CALLBACK_F('N', "no-numbered", &numbered
, NULL
,
1886 N_("use [PATCH] even with multiple patches"),
1887 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
, no_numbered_callback
),
1888 OPT_BOOL('s', "signoff", &do_signoff
, N_("add a Signed-off-by trailer")),
1889 OPT_BOOL(0, "stdout", &use_stdout
,
1890 N_("print patches to standard out")),
1891 OPT_BOOL(0, "cover-letter", &cover_letter
,
1892 N_("generate a cover letter")),
1893 OPT_BOOL(0, "numbered-files", &just_numbers
,
1894 N_("use simple number sequence for output file names")),
1895 OPT_STRING(0, "suffix", &fmt_patch_suffix
, N_("sfx"),
1896 N_("use <sfx> instead of '.patch'")),
1897 OPT_INTEGER(0, "start-number", &start_number
,
1898 N_("start numbering patches at <n> instead of 1")),
1899 OPT_STRING('v', "reroll-count", &reroll_count
, N_("reroll-count"),
1900 N_("mark the series as Nth re-roll")),
1901 OPT_INTEGER(0, "filename-max-length", &fmt_patch_name_max
,
1902 N_("max length of output filename")),
1903 OPT_CALLBACK_F(0, "rfc", &rev
, NULL
,
1904 N_("use [RFC PATCH] instead of [PATCH]"),
1905 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
, rfc_callback
),
1906 OPT_STRING(0, "cover-from-description", &cover_from_description_arg
,
1907 N_("cover-from-description-mode"),
1908 N_("generate parts of a cover letter based on a branch's description")),
1909 OPT_CALLBACK_F(0, "subject-prefix", &rev
, N_("prefix"),
1910 N_("use [<prefix>] instead of [PATCH]"),
1911 PARSE_OPT_NONEG
, subject_prefix_callback
),
1912 OPT_CALLBACK_F('o', "output-directory", &output_directory
,
1913 N_("dir"), N_("store resulting files in <dir>"),
1914 PARSE_OPT_NONEG
, output_directory_callback
),
1915 OPT_CALLBACK_F('k', "keep-subject", &rev
, NULL
,
1916 N_("don't strip/add [PATCH]"),
1917 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
, keep_callback
),
1918 OPT_BOOL(0, "no-binary", &no_binary_diff
,
1919 N_("don't output binary diffs")),
1920 OPT_BOOL(0, "zero-commit", &zero_commit
,
1921 N_("output all-zero hash in From header")),
1922 OPT_BOOL(0, "ignore-if-in-upstream", &ignore_if_in_upstream
,
1923 N_("don't include a patch matching a commit upstream")),
1924 OPT_SET_INT_F('p', "no-stat", &use_patch_format
,
1925 N_("show patch format instead of default (patch + stat)"),
1926 1, PARSE_OPT_NONEG
),
1927 OPT_GROUP(N_("Messaging")),
1928 OPT_CALLBACK(0, "add-header", NULL
, N_("header"),
1929 N_("add email header"), header_callback
),
1930 OPT_CALLBACK(0, "to", NULL
, N_("email"), N_("add To: header"), to_callback
),
1931 OPT_CALLBACK(0, "cc", NULL
, N_("email"), N_("add Cc: header"), cc_callback
),
1932 OPT_CALLBACK_F(0, "from", &from
, N_("ident"),
1933 N_("set From address to <ident> (or committer ident if absent)"),
1934 PARSE_OPT_OPTARG
, from_callback
),
1935 OPT_STRING(0, "in-reply-to", &in_reply_to
, N_("message-id"),
1936 N_("make first mail a reply to <message-id>")),
1937 OPT_CALLBACK_F(0, "attach", &rev
, N_("boundary"),
1938 N_("attach the patch"), PARSE_OPT_OPTARG
,
1940 OPT_CALLBACK_F(0, "inline", &rev
, N_("boundary"),
1941 N_("inline the patch"),
1942 PARSE_OPT_OPTARG
| PARSE_OPT_NONEG
,
1944 OPT_CALLBACK_F(0, "thread", &thread
, N_("style"),
1945 N_("enable message threading, styles: shallow, deep"),
1946 PARSE_OPT_OPTARG
, thread_callback
),
1947 OPT_STRING(0, "signature", &signature
, N_("signature"),
1948 N_("add a signature")),
1949 OPT_CALLBACK_F(0, "base", &base_commit
, N_("base-commit"),
1950 N_("add prerequisite tree info to the patch series"),
1952 OPT_FILENAME(0, "signature-file", &signature_file
,
1953 N_("add a signature from a file")),
1954 OPT__QUIET(&quiet
, N_("don't print the patch filenames")),
1955 OPT_BOOL(0, "progress", &show_progress
,
1956 N_("show progress while generating patches")),
1957 OPT_CALLBACK(0, "interdiff", &idiff_prev
, N_("rev"),
1958 N_("show changes against <rev> in cover letter or single patch"),
1959 parse_opt_object_name
),
1960 OPT_STRING(0, "range-diff", &rdiff_prev
, N_("refspec"),
1961 N_("show changes against <refspec> in cover letter or single patch")),
1962 OPT_INTEGER(0, "creation-factor", &creation_factor
,
1963 N_("percentage by which creation is weighted")),
1964 OPT_BOOL(0, "force-in-body-from", &force_in_body_from
,
1965 N_("show in-body From: even if identical to the e-mail header")),
1969 extra_hdr
.strdup_strings
= 1;
1970 extra_to
.strdup_strings
= 1;
1971 extra_cc
.strdup_strings
= 1;
1973 init_log_defaults();
1974 init_display_notes(¬es_opt
);
1975 git_config(git_format_config
, NULL
);
1976 repo_init_revisions(the_repository
, &rev
, prefix
);
1977 git_config(grep_config
, &rev
.grep_filter
);
1979 rev
.show_notes
= show_notes
;
1980 memcpy(&rev
.notes_opt
, ¬es_opt
, sizeof(notes_opt
));
1981 rev
.commit_format
= CMIT_FMT_EMAIL
;
1982 rev
.encode_email_headers
= default_encode_email_headers
;
1983 rev
.expand_tabs_in_log_default
= 0;
1984 rev
.verbose_header
= 1;
1986 rev
.max_parents
= 1;
1987 rev
.diffopt
.flags
.recursive
= 1;
1988 rev
.diffopt
.no_free
= 1;
1989 rev
.subject_prefix
= fmt_patch_subject_prefix
;
1990 memset(&s_r_opt
, 0, sizeof(s_r_opt
));
1991 s_r_opt
.def
= "HEAD";
1992 s_r_opt
.revarg_opt
= REVARG_COMMITTISH
;
1994 if (default_attach
) {
1995 rev
.mime_boundary
= default_attach
;
2000 * Parse the arguments before setup_revisions(), or something
2001 * like "git format-patch -o a123 HEAD^.." may fail; a123 is
2002 * possibly a valid SHA1.
2004 argc
= parse_options(argc
, argv
, prefix
, builtin_format_patch_options
,
2005 builtin_format_patch_usage
,
2006 PARSE_OPT_KEEP_ARGV0
| PARSE_OPT_KEEP_UNKNOWN_OPT
|
2007 PARSE_OPT_KEEP_DASHDASH
);
2009 rev
.force_in_body_from
= force_in_body_from
;
2011 /* Make sure "0000-$sub.patch" gives non-negative length for $sub */
2012 if (fmt_patch_name_max
<= strlen("0000-") + strlen(fmt_patch_suffix
))
2013 fmt_patch_name_max
= strlen("0000-") + strlen(fmt_patch_suffix
);
2015 if (cover_from_description_arg
)
2016 cover_from_description_mode
= parse_cover_from_description(cover_from_description_arg
);
2019 strbuf_addf(&sprefix
, "%s v%s",
2020 rev
.subject_prefix
, reroll_count
);
2021 rev
.reroll_count
= reroll_count
;
2022 rev
.subject_prefix
= sprefix
.buf
;
2025 for (i
= 0; i
< extra_hdr
.nr
; i
++) {
2026 strbuf_addstr(&buf
, extra_hdr
.items
[i
].string
);
2027 strbuf_addch(&buf
, '\n');
2031 strbuf_addstr(&buf
, "To: ");
2032 for (i
= 0; i
< extra_to
.nr
; i
++) {
2034 strbuf_addstr(&buf
, " ");
2035 strbuf_addstr(&buf
, extra_to
.items
[i
].string
);
2036 if (i
+ 1 < extra_to
.nr
)
2037 strbuf_addch(&buf
, ',');
2038 strbuf_addch(&buf
, '\n');
2042 strbuf_addstr(&buf
, "Cc: ");
2043 for (i
= 0; i
< extra_cc
.nr
; i
++) {
2045 strbuf_addstr(&buf
, " ");
2046 strbuf_addstr(&buf
, extra_cc
.items
[i
].string
);
2047 if (i
+ 1 < extra_cc
.nr
)
2048 strbuf_addch(&buf
, ',');
2049 strbuf_addch(&buf
, '\n');
2052 rev
.extra_headers
= to_free
= strbuf_detach(&buf
, NULL
);
2055 if (split_ident_line(&rev
.from_ident
, from
, strlen(from
)))
2056 die(_("invalid ident line: %s"), from
);
2059 if (start_number
< 0)
2063 * If numbered is set solely due to format.numbered in config,
2064 * and it would conflict with --keep-subject (-k) from the
2065 * command line, reset "numbered".
2067 if (numbered
&& keep_subject
&& !numbered_cmdline_opt
)
2070 if (numbered
&& keep_subject
)
2071 die(_("options '%s' and '%s' cannot be used together"), "-n", "-k");
2072 if (keep_subject
&& subject_prefix
)
2073 die(_("options '%s' and '%s' cannot be used together"), "--subject-prefix/--rfc", "-k");
2074 rev
.preserve_subject
= keep_subject
;
2076 argc
= setup_revisions(argc
, argv
, &rev
, &s_r_opt
);
2078 die(_("unrecognized argument: %s"), argv
[1]);
2080 if (rev
.diffopt
.output_format
& DIFF_FORMAT_NAME
)
2081 die(_("--name-only does not make sense"));
2082 if (rev
.diffopt
.output_format
& DIFF_FORMAT_NAME_STATUS
)
2083 die(_("--name-status does not make sense"));
2084 if (rev
.diffopt
.output_format
& DIFF_FORMAT_CHECKDIFF
)
2085 die(_("--check does not make sense"));
2086 if (rev
.remerge_diff
)
2087 die(_("--remerge-diff does not make sense"));
2089 if (!use_patch_format
&&
2090 (!rev
.diffopt
.output_format
||
2091 rev
.diffopt
.output_format
== DIFF_FORMAT_PATCH
))
2092 rev
.diffopt
.output_format
= DIFF_FORMAT_DIFFSTAT
| DIFF_FORMAT_SUMMARY
;
2093 if (!rev
.diffopt
.stat_width
)
2094 rev
.diffopt
.stat_width
= MAIL_DEFAULT_WRAP
;
2096 /* Always generate a patch */
2097 rev
.diffopt
.output_format
|= DIFF_FORMAT_PATCH
;
2099 rev
.zero_commit
= zero_commit
;
2100 rev
.patch_name_max
= fmt_patch_name_max
;
2102 if (!rev
.diffopt
.flags
.text
&& !no_binary_diff
)
2103 rev
.diffopt
.flags
.binary
= 1;
2106 load_display_notes(&rev
.notes_opt
);
2108 die_for_incompatible_opt3(use_stdout
, "--stdout",
2109 rev
.diffopt
.close_file
, "--output",
2110 !!output_directory
, "--output-directory");
2112 if (use_stdout
&& stdout_mboxrd
)
2113 rev
.commit_format
= CMIT_FMT_MBOXRD
;
2117 } else if (!rev
.diffopt
.close_file
) {
2120 if (!output_directory
)
2121 output_directory
= config_output_directory
;
2122 output_directory
= set_outdir(prefix
, output_directory
);
2124 if (rev
.diffopt
.use_color
!= GIT_COLOR_ALWAYS
)
2125 rev
.diffopt
.use_color
= GIT_COLOR_NEVER
;
2127 * We consider <outdir> as 'outside of gitdir', therefore avoid
2128 * applying adjust_shared_perm in s-c-l-d.
2130 saved
= get_shared_repository();
2131 set_shared_repository(0);
2132 switch (safe_create_leading_directories_const(output_directory
)) {
2137 die(_("could not create leading directories "
2138 "of '%s'"), output_directory
);
2140 set_shared_repository(saved
);
2141 if (mkdir(output_directory
, 0777) < 0 && errno
!= EEXIST
)
2142 die_errno(_("could not create directory '%s'"),
2146 if (rev
.pending
.nr
== 1) {
2149 if (rev
.max_count
< 0 && !rev
.show_root_diff
) {
2151 * This is traditional behaviour of "git format-patch
2152 * origin" that prepares what the origin side still
2155 rev
.pending
.objects
[0].item
->flags
|= UNINTERESTING
;
2156 add_head_to_pending(&rev
);
2160 * Otherwise, it is "format-patch -22 HEAD", and/or
2161 * "format-patch --root HEAD". The user wants
2162 * get_revision() to do the usual traversal.
2165 if (!strcmp(rev
.pending
.objects
[0].name
, "HEAD"))
2169 const char *ref
, *v
;
2170 ref
= resolve_ref_unsafe("HEAD", RESOLVE_REF_READING
,
2172 if (ref
&& skip_prefix(ref
, "refs/heads/", &v
))
2173 branch_name
= xstrdup(v
);
2175 branch_name
= xstrdup(""); /* no branch */
2180 * We cannot move this anywhere earlier because we do want to
2181 * know if --root was given explicitly from the command line.
2183 rev
.show_root_diff
= 1;
2185 if (ignore_if_in_upstream
) {
2186 /* Don't say anything if head and upstream are the same. */
2187 if (rev
.pending
.nr
== 2) {
2188 struct object_array_entry
*o
= rev
.pending
.objects
;
2189 if (oideq(&o
[0].item
->oid
, &o
[1].item
->oid
))
2192 get_patch_ids(&rev
, &ids
);
2195 if (prepare_revision_walk(&rev
))
2196 die(_("revision walk setup failed"));
2198 while ((commit
= get_revision(&rev
)) != NULL
) {
2199 if (commit
->object
.flags
& BOUNDARY
) {
2201 origin
= (boundary_count
== 1) ? commit
: NULL
;
2205 if (ignore_if_in_upstream
&& has_commit_patch_id(commit
, &ids
))
2209 REALLOC_ARRAY(list
, nr
);
2210 list
[nr
- 1] = commit
;
2216 if (cover_letter
== -1) {
2217 if (config_cover_letter
== COVER_AUTO
)
2218 cover_letter
= (total
> 1);
2220 cover_letter
= (config_cover_letter
== COVER_ON
);
2222 if (!keep_subject
&& auto_number
&& (total
> 1 || cover_letter
))
2225 rev
.total
= total
+ start_number
- 1;
2227 if (idiff_prev
.nr
) {
2228 if (!cover_letter
&& total
!= 1)
2229 die(_("--interdiff requires --cover-letter or single patch"));
2230 rev
.idiff_oid1
= &idiff_prev
.oid
[idiff_prev
.nr
- 1];
2231 rev
.idiff_oid2
= get_commit_tree_oid(list
[0]);
2232 rev
.idiff_title
= diff_title(&idiff_title
, reroll_count
,
2234 _("Interdiff against v%d:"));
2237 if (creation_factor
< 0)
2238 creation_factor
= RANGE_DIFF_CREATION_FACTOR_DEFAULT
;
2239 else if (!rdiff_prev
)
2240 die(_("the option '%s' requires '%s'"), "--creation-factor", "--range-diff");
2243 if (!cover_letter
&& total
!= 1)
2244 die(_("--range-diff requires --cover-letter or single patch"));
2246 infer_range_diff_ranges(&rdiff1
, &rdiff2
, rdiff_prev
,
2248 rev
.rdiff1
= rdiff1
.buf
;
2249 rev
.rdiff2
= rdiff2
.buf
;
2250 rev
.creation_factor
= creation_factor
;
2251 rev
.rdiff_title
= diff_title(&rdiff_title
, reroll_count
,
2253 _("Range-diff against v%d:"));
2257 ; /* --no-signature inhibits all signatures */
2258 } else if (signature
&& signature
!= git_version_string
) {
2259 ; /* non-default signature already set */
2260 } else if (signature_file
) {
2261 struct strbuf buf
= STRBUF_INIT
;
2263 if (strbuf_read_file(&buf
, signature_file
, 128) < 0)
2264 die_errno(_("unable to read signature file '%s'"), signature_file
);
2265 signature
= strbuf_detach(&buf
, NULL
);
2268 memset(&bases
, 0, sizeof(bases
));
2269 base
= get_base_commit(base_commit
, list
, nr
);
2271 reset_revision_walk();
2272 clear_object_flags(UNINTERESTING
);
2273 prepare_bases(&bases
, base
, list
, nr
);
2276 if (in_reply_to
|| thread
|| cover_letter
) {
2277 rev
.ref_message_ids
= xmalloc(sizeof(*rev
.ref_message_ids
));
2278 string_list_init_nodup(rev
.ref_message_ids
);
2281 const char *msgid
= clean_message_id(in_reply_to
);
2282 string_list_append(rev
.ref_message_ids
, msgid
);
2284 rev
.numbered_files
= just_numbers
;
2285 rev
.patch_suffix
= fmt_patch_suffix
;
2288 gen_message_id(&rev
, "cover");
2289 make_cover_letter(&rev
, !!output_directory
,
2290 origin
, nr
, list
, branch_name
, quiet
);
2291 print_bases(&bases
, rev
.diffopt
.file
);
2292 print_signature(rev
.diffopt
.file
);
2295 /* interdiff/range-diff in cover-letter; omit from patches */
2296 rev
.idiff_oid1
= NULL
;
2299 rev
.add_signoff
= do_signoff
;
2302 progress
= start_delayed_progress(_("Generating patches"), total
);
2305 display_progress(progress
, total
- nr
);
2307 rev
.nr
= total
- nr
+ (start_number
- 1);
2308 /* Make the second and subsequent mails replies to the first */
2310 /* Have we already had a message ID? */
2311 if (rev
.message_id
) {
2313 * For deep threading: make every mail
2314 * a reply to the previous one, no
2315 * matter what other options are set.
2317 * For shallow threading:
2319 * Without --cover-letter and
2320 * --in-reply-to, make every mail a
2321 * reply to the one before.
2323 * With --in-reply-to but no
2324 * --cover-letter, make every mail a
2325 * reply to the <reply-to>.
2327 * With --cover-letter, make every
2328 * mail but the cover letter a reply
2329 * to the cover letter. The cover
2330 * letter is a reply to the
2331 * --in-reply-to, if specified.
2333 if (thread
== THREAD_SHALLOW
2334 && rev
.ref_message_ids
->nr
> 0
2335 && (!cover_letter
|| rev
.nr
> 1))
2336 free(rev
.message_id
);
2338 string_list_append(rev
.ref_message_ids
,
2341 gen_message_id(&rev
, oid_to_hex(&commit
->object
.oid
));
2344 if (output_directory
&&
2345 open_next_file(rev
.numbered_files
? NULL
: commit
, NULL
, &rev
, quiet
))
2346 die(_("failed to create output files"));
2347 shown
= log_tree_commit(&rev
, commit
);
2348 free_commit_buffer(the_repository
->parsed_objects
,
2351 /* We put one extra blank line between formatted
2352 * patches and this flag is used by log-tree code
2353 * to see if it needs to emit a LF before showing
2354 * the log; when using one file per patch, we do
2355 * not want the extra blank line.
2357 if (output_directory
)
2360 print_bases(&bases
, rev
.diffopt
.file
);
2361 if (rev
.mime_boundary
)
2362 fprintf(rev
.diffopt
.file
, "\n--%s%s--\n\n\n",
2363 mime_boundary_leader
,
2366 print_signature(rev
.diffopt
.file
);
2368 if (output_directory
)
2369 fclose(rev
.diffopt
.file
);
2371 stop_progress(&progress
);
2374 string_list_clear(&extra_to
, 0);
2375 string_list_clear(&extra_cc
, 0);
2376 string_list_clear(&extra_hdr
, 0);
2377 if (ignore_if_in_upstream
)
2378 free_patch_ids(&ids
);
2381 oid_array_clear(&idiff_prev
);
2382 strbuf_release(&idiff_title
);
2383 strbuf_release(&rdiff1
);
2384 strbuf_release(&rdiff2
);
2385 strbuf_release(&rdiff_title
);
2386 strbuf_release(&sprefix
);
2388 if (rev
.ref_message_ids
)
2389 string_list_clear(rev
.ref_message_ids
, 0);
2390 free(rev
.ref_message_ids
);
2391 return cmd_log_deinit(0, &rev
);
2394 static int add_pending_commit(const char *arg
, struct rev_info
*revs
, int flags
)
2396 struct object_id oid
;
2397 if (get_oid(arg
, &oid
) == 0) {
2398 struct commit
*commit
= lookup_commit_reference(the_repository
,
2401 commit
->object
.flags
|= flags
;
2402 add_pending_object(revs
, &commit
->object
, arg
);
2409 static const char * const cherry_usage
[] = {
2410 N_("git cherry [-v] [<upstream> [<head> [<limit>]]]"),
2414 static void print_commit(char sign
, struct commit
*commit
, int verbose
,
2415 int abbrev
, FILE *file
)
2418 fprintf(file
, "%c %s\n", sign
,
2419 find_unique_abbrev(&commit
->object
.oid
, abbrev
));
2421 struct strbuf buf
= STRBUF_INIT
;
2422 pp_commit_easy(CMIT_FMT_ONELINE
, commit
, &buf
);
2423 fprintf(file
, "%c %s %s\n", sign
,
2424 find_unique_abbrev(&commit
->object
.oid
, abbrev
),
2426 strbuf_release(&buf
);
2430 int cmd_cherry(int argc
, const char **argv
, const char *prefix
)
2432 struct rev_info revs
;
2433 struct patch_ids ids
;
2434 struct commit
*commit
;
2435 struct commit_list
*list
= NULL
;
2436 struct branch
*current_branch
;
2437 const char *upstream
;
2438 const char *head
= "HEAD";
2439 const char *limit
= NULL
;
2440 int verbose
= 0, abbrev
= 0;
2442 struct option options
[] = {
2443 OPT__ABBREV(&abbrev
),
2444 OPT__VERBOSE(&verbose
, N_("be verbose")),
2448 argc
= parse_options(argc
, argv
, prefix
, options
, cherry_usage
, 0);
2461 current_branch
= branch_get(NULL
);
2462 upstream
= branch_get_upstream(current_branch
, NULL
);
2464 fprintf(stderr
, _("Could not find a tracked"
2465 " remote branch, please"
2466 " specify <upstream> manually.\n"));
2467 usage_with_options(cherry_usage
, options
);
2471 repo_init_revisions(the_repository
, &revs
, prefix
);
2472 revs
.max_parents
= 1;
2474 if (add_pending_commit(head
, &revs
, 0))
2475 die(_("unknown commit %s"), head
);
2476 if (add_pending_commit(upstream
, &revs
, UNINTERESTING
))
2477 die(_("unknown commit %s"), upstream
);
2479 /* Don't say anything if head and upstream are the same. */
2480 if (revs
.pending
.nr
== 2) {
2481 struct object_array_entry
*o
= revs
.pending
.objects
;
2482 if (oideq(&o
[0].item
->oid
, &o
[1].item
->oid
))
2486 get_patch_ids(&revs
, &ids
);
2488 if (limit
&& add_pending_commit(limit
, &revs
, UNINTERESTING
))
2489 die(_("unknown commit %s"), limit
);
2491 /* reverse the list of commits */
2492 if (prepare_revision_walk(&revs
))
2493 die(_("revision walk setup failed"));
2494 while ((commit
= get_revision(&revs
)) != NULL
) {
2495 commit_list_insert(commit
, &list
);
2501 commit
= list
->item
;
2502 if (has_commit_patch_id(commit
, &ids
))
2504 print_commit(sign
, commit
, verbose
, abbrev
, revs
.diffopt
.file
);
2508 free_patch_ids(&ids
);