2 * Builtin "git log" and related commands (show, whatchanged)
4 * (C) Copyright 2006 Linus Torvalds
7 #include "git-compat-util.h"
10 #include "environment.h"
14 #include "object-file.h"
15 #include "object-name.h"
16 #include "object-store-ll.h"
21 #include "diff-merges.h"
25 #include "oid-array.h"
27 #include "reflog-walk.h"
28 #include "patch-ids.h"
31 #include "string-list.h"
32 #include "parse-options.h"
35 #include "streaming.h"
39 #include "commit-slab.h"
40 #include "repository.h"
41 #include "commit-reach.h"
42 #include "range-diff.h"
43 #include "tmp-objdir.h"
45 #include "write-or-die.h"
47 #define MAIL_DEFAULT_WRAP 72
48 #define COVER_FROM_AUTO_MAX_SUBJECT_LEN 100
49 #define FORMAT_PATCH_NAME_MAX_DEFAULT 64
51 /* Set a default date-time format for git log ("log.date" config variable) */
52 static const char *default_date_mode
= NULL
;
54 static int default_abbrev_commit
;
55 static int default_show_root
= 1;
56 static int default_follow
;
57 static int default_show_signature
;
58 static int default_encode_email_headers
= 1;
59 static int decoration_style
;
60 static int decoration_given
;
61 static int use_mailmap_config
= 1;
62 static unsigned int force_in_body_from
;
63 static int stdout_mboxrd
;
64 static const char *fmt_patch_subject_prefix
= "PATCH";
65 static int fmt_patch_name_max
= FORMAT_PATCH_NAME_MAX_DEFAULT
;
66 static const char *fmt_pretty
;
67 static int format_no_prefix
;
69 static const char * const builtin_log_usage
[] = {
70 N_("git log [<options>] [<revision-range>] [[--] <path>...]"),
71 N_("git show [<options>] <object>..."),
75 struct line_opt_callback_data
{
78 struct string_list args
;
81 static int session_is_interactive(void)
83 return isatty(1) || pager_in_use();
86 static int auto_decoration_style(void)
88 return session_is_interactive() ? DECORATE_SHORT_REFS
: 0;
91 static int parse_decoration_style(const char *value
)
93 switch (git_parse_maybe_bool(value
)) {
95 return DECORATE_SHORT_REFS
;
101 if (!strcmp(value
, "full"))
102 return DECORATE_FULL_REFS
;
103 else if (!strcmp(value
, "short"))
104 return DECORATE_SHORT_REFS
;
105 else if (!strcmp(value
, "auto"))
106 return auto_decoration_style();
108 * Please update _git_log() in git-completion.bash when you
109 * add new decoration styles.
114 static int use_default_decoration_filter
= 1;
115 static struct string_list decorate_refs_exclude
= STRING_LIST_INIT_NODUP
;
116 static struct string_list decorate_refs_exclude_config
= STRING_LIST_INIT_NODUP
;
117 static struct string_list decorate_refs_include
= STRING_LIST_INIT_NODUP
;
119 static int clear_decorations_callback(const struct option
*opt UNUSED
,
120 const char *arg
, int unset
)
122 BUG_ON_OPT_NEG(unset
);
124 string_list_clear(&decorate_refs_include
, 0);
125 string_list_clear(&decorate_refs_exclude
, 0);
126 use_default_decoration_filter
= 0;
130 static int decorate_callback(const struct option
*opt UNUSED
, const char *arg
,
134 decoration_style
= 0;
136 decoration_style
= parse_decoration_style(arg
);
138 decoration_style
= DECORATE_SHORT_REFS
;
140 if (decoration_style
< 0)
141 die(_("invalid --decorate option: %s"), arg
);
143 decoration_given
= 1;
148 static int log_line_range_callback(const struct option
*option
, const char *arg
, int unset
)
150 struct line_opt_callback_data
*data
= option
->value
;
152 BUG_ON_OPT_NEG(unset
);
157 data
->rev
->line_level_traverse
= 1;
158 string_list_append(&data
->args
, arg
);
163 static void init_log_defaults(void)
165 init_diff_ui_defaults();
167 decoration_style
= auto_decoration_style();
170 static void cmd_log_init_defaults(struct rev_info
*rev
)
173 get_commit_format(fmt_pretty
, rev
);
175 rev
->diffopt
.flags
.default_follow_renames
= 1;
176 rev
->verbose_header
= 1;
177 init_diffstat_widths(&rev
->diffopt
);
178 rev
->diffopt
.flags
.recursive
= 1;
179 rev
->diffopt
.flags
.allow_textconv
= 1;
180 rev
->abbrev_commit
= default_abbrev_commit
;
181 rev
->show_root_diff
= default_show_root
;
182 rev
->subject_prefix
= fmt_patch_subject_prefix
;
183 rev
->patch_name_max
= fmt_patch_name_max
;
184 rev
->show_signature
= default_show_signature
;
185 rev
->encode_email_headers
= default_encode_email_headers
;
187 if (default_date_mode
)
188 parse_date_format(default_date_mode
, &rev
->date_mode
);
191 static void set_default_decoration_filter(struct decoration_filter
*decoration_filter
)
195 struct string_list
*include
= decoration_filter
->include_ref_pattern
;
196 const struct string_list
*config_exclude
;
198 if (!git_config_get_string_multi("log.excludeDecoration",
200 struct string_list_item
*item
;
201 for_each_string_list_item(item
, config_exclude
)
202 string_list_append(decoration_filter
->exclude_ref_config_pattern
,
207 * By default, decorate_all is disabled. Enable it if
208 * log.initialDecorationSet=all. Don't ever disable it by config,
209 * since the command-line takes precedent.
211 if (use_default_decoration_filter
&&
212 !git_config_get_string("log.initialdecorationset", &value
) &&
213 !strcmp("all", value
))
214 use_default_decoration_filter
= 0;
217 if (!use_default_decoration_filter
||
218 decoration_filter
->exclude_ref_pattern
->nr
||
219 decoration_filter
->include_ref_pattern
->nr
||
220 decoration_filter
->exclude_ref_config_pattern
->nr
)
224 * No command-line or config options were given, so
225 * populate with sensible defaults.
227 for (i
= 0; i
< ARRAY_SIZE(ref_namespace
); i
++) {
228 if (!ref_namespace
[i
].decoration
)
231 string_list_append(include
, ref_namespace
[i
].ref
);
235 static void cmd_log_init_finish(int argc
, const char **argv
, const char *prefix
,
236 struct rev_info
*rev
, struct setup_revision_opt
*opt
)
238 struct userformat_want w
;
239 int quiet
= 0, source
= 0, mailmap
;
240 static struct line_opt_callback_data line_cb
= {NULL
, NULL
, STRING_LIST_INIT_DUP
};
241 struct decoration_filter decoration_filter
= {
242 .exclude_ref_pattern
= &decorate_refs_exclude
,
243 .include_ref_pattern
= &decorate_refs_include
,
244 .exclude_ref_config_pattern
= &decorate_refs_exclude_config
,
246 static struct revision_sources revision_sources
;
248 const struct option builtin_log_options
[] = {
249 OPT__QUIET(&quiet
, N_("suppress diff output")),
250 OPT_BOOL(0, "source", &source
, N_("show source")),
251 OPT_BOOL(0, "use-mailmap", &mailmap
, N_("use mail map file")),
252 OPT_ALIAS(0, "mailmap", "use-mailmap"),
253 OPT_CALLBACK_F(0, "clear-decorations", NULL
, NULL
,
254 N_("clear all previously-defined decoration filters"),
255 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
,
256 clear_decorations_callback
),
257 OPT_STRING_LIST(0, "decorate-refs", &decorate_refs_include
,
258 N_("pattern"), N_("only decorate refs that match <pattern>")),
259 OPT_STRING_LIST(0, "decorate-refs-exclude", &decorate_refs_exclude
,
260 N_("pattern"), N_("do not decorate refs that match <pattern>")),
261 OPT_CALLBACK_F(0, "decorate", NULL
, NULL
, N_("decorate options"),
262 PARSE_OPT_OPTARG
, decorate_callback
),
263 OPT_CALLBACK('L', NULL
, &line_cb
, "range:file",
264 N_("trace the evolution of line range <start>,<end> or function :<funcname> in <file>"),
265 log_line_range_callback
),
270 line_cb
.prefix
= prefix
;
272 mailmap
= use_mailmap_config
;
273 argc
= parse_options(argc
, argv
, prefix
,
274 builtin_log_options
, builtin_log_usage
,
275 PARSE_OPT_KEEP_ARGV0
| PARSE_OPT_KEEP_UNKNOWN_OPT
|
276 PARSE_OPT_KEEP_DASHDASH
);
279 rev
->diffopt
.output_format
|= DIFF_FORMAT_NO_OUTPUT
;
280 argc
= setup_revisions(argc
, argv
, rev
, opt
);
282 /* Any arguments at this point are not recognized */
284 die(_("unrecognized argument: %s"), argv
[1]);
286 if (rev
->line_level_traverse
&& rev
->prune_data
.nr
)
287 die(_("-L<range>:<file> cannot be used with pathspec"));
289 memset(&w
, 0, sizeof(w
));
290 userformat_find_requirements(NULL
, &w
);
292 if (!rev
->show_notes_given
&& (!rev
->pretty_given
|| w
.notes
))
295 load_display_notes(&rev
->notes_opt
);
297 if ((rev
->diffopt
.pickaxe_opts
& DIFF_PICKAXE_KINDS_MASK
) ||
298 rev
->diffopt
.filter
|| rev
->diffopt
.flags
.follow_renames
)
299 rev
->always_show_header
= 0;
301 if (source
|| w
.source
) {
302 init_revision_sources(&revision_sources
);
303 rev
->sources
= &revision_sources
;
307 rev
->mailmap
= xmalloc(sizeof(struct string_list
));
308 string_list_init_nodup(rev
->mailmap
);
309 read_mailmap(rev
->mailmap
);
312 if (rev
->pretty_given
&& rev
->commit_format
== CMIT_FMT_RAW
) {
314 * "log --pretty=raw" is special; ignore UI oriented
315 * configuration variables such as decoration.
317 if (!decoration_given
)
318 decoration_style
= 0;
319 if (!rev
->abbrev_commit_given
)
320 rev
->abbrev_commit
= 0;
323 if (rev
->commit_format
== CMIT_FMT_USERFORMAT
) {
326 * Disable decoration loading if the format will not
329 decoration_style
= 0;
330 } else if (!decoration_style
) {
332 * If we are going to show them, make sure we do load
333 * them here, but taking care not to override a
334 * specific style set by config or --decorate.
336 decoration_style
= DECORATE_SHORT_REFS
;
340 if (decoration_style
|| rev
->simplify_by_decoration
) {
341 set_default_decoration_filter(&decoration_filter
);
343 if (decoration_style
)
344 rev
->show_decorations
= 1;
346 load_ref_decorations(&decoration_filter
, decoration_style
);
349 if (rev
->line_level_traverse
)
350 line_log_init(rev
, line_cb
.prefix
, &line_cb
.args
);
355 static void cmd_log_init(int argc
, const char **argv
, const char *prefix
,
356 struct rev_info
*rev
, struct setup_revision_opt
*opt
)
358 cmd_log_init_defaults(rev
);
359 cmd_log_init_finish(argc
, argv
, prefix
, rev
, opt
);
362 static int cmd_log_deinit(int ret
, struct rev_info
*rev
)
364 release_revisions(rev
);
369 * This gives a rough estimate for how many commits we
370 * will print out in the list.
372 static int estimate_commit_count(struct commit_list
*list
)
377 struct commit
*commit
= list
->item
;
378 unsigned int flags
= commit
->object
.flags
;
380 if (!(flags
& (TREESAME
| UNINTERESTING
)))
386 static void show_early_header(struct rev_info
*rev
, const char *stage
, int nr
)
388 if (rev
->shown_one
) {
390 if (rev
->commit_format
!= CMIT_FMT_ONELINE
)
391 putchar(rev
->diffopt
.line_termination
);
393 fprintf(rev
->diffopt
.file
, _("Final output: %d %s\n"), nr
, stage
);
396 static struct itimerval early_output_timer
;
398 static void log_show_early(struct rev_info
*revs
, struct commit_list
*list
)
400 int i
= revs
->early_output
;
402 int no_free
= revs
->diffopt
.no_free
;
404 revs
->diffopt
.no_free
= 0;
405 sort_in_topological_order(&list
, revs
->sort_order
);
407 struct commit
*commit
= list
->item
;
408 switch (simplify_commit(revs
, commit
)) {
411 int n
= estimate_commit_count(list
);
412 show_early_header(revs
, "incomplete", n
);
415 log_tree_commit(revs
, commit
);
421 revs
->diffopt
.no_free
= no_free
;
422 diff_free(&revs
->diffopt
);
428 /* Did we already get enough commits for the early output? */
430 revs
->diffopt
.no_free
= 0;
431 diff_free(&revs
->diffopt
);
436 * ..if no, then repeat it twice a second until we
439 * NOTE! We don't use "it_interval", because if the
440 * reader isn't listening, we want our output to be
441 * throttled by the writing, and not have the timer
442 * trigger every second even if we're blocked on a
445 early_output_timer
.it_value
.tv_sec
= 0;
446 early_output_timer
.it_value
.tv_usec
= 500000;
447 setitimer(ITIMER_REAL
, &early_output_timer
, NULL
);
450 static void early_output(int signal UNUSED
)
452 show_early_output
= log_show_early
;
455 static void setup_early_output(void)
460 * Set up the signal handler, minimally intrusively:
461 * we only set a single volatile integer word (not
462 * using sigatomic_t - trying to avoid unnecessary
463 * system dependencies and headers), and using
466 memset(&sa
, 0, sizeof(sa
));
467 sa
.sa_handler
= early_output
;
468 sigemptyset(&sa
.sa_mask
);
469 sa
.sa_flags
= SA_RESTART
;
470 sigaction(SIGALRM
, &sa
, NULL
);
473 * If we can get the whole output in less than a
474 * tenth of a second, don't even bother doing the
475 * early-output thing..
477 * This is a one-time-only trigger.
479 early_output_timer
.it_value
.tv_sec
= 0;
480 early_output_timer
.it_value
.tv_usec
= 100000;
481 setitimer(ITIMER_REAL
, &early_output_timer
, NULL
);
484 static void finish_early_output(struct rev_info
*rev
)
486 int n
= estimate_commit_count(rev
->commits
);
487 signal(SIGALRM
, SIG_IGN
);
488 show_early_header(rev
, "done", n
);
491 static int cmd_log_walk_no_free(struct rev_info
*rev
)
493 struct commit
*commit
;
497 if (rev
->remerge_diff
) {
498 rev
->remerge_objdir
= tmp_objdir_create("remerge-diff");
499 if (!rev
->remerge_objdir
)
500 die(_("unable to create temporary object directory"));
501 tmp_objdir_replace_primary_odb(rev
->remerge_objdir
, 1);
504 if (rev
->early_output
)
505 setup_early_output();
507 if (prepare_revision_walk(rev
))
508 die(_("revision walk setup failed"));
510 if (rev
->early_output
)
511 finish_early_output(rev
);
514 * For --check and --exit-code, the exit code is based on CHECK_FAILED
515 * and HAS_CHANGES being accumulated in rev->diffopt, so be careful to
516 * retain that state information if replacing rev->diffopt in this loop
518 while ((commit
= get_revision(rev
)) != NULL
) {
519 if (!log_tree_commit(rev
, commit
) && rev
->max_count
>= 0)
521 * We decremented max_count in get_revision,
522 * but we didn't actually show the commit.
525 if (!rev
->reflog_info
) {
527 * We may show a given commit multiple times when
528 * walking the reflogs.
530 free_commit_buffer(the_repository
->parsed_objects
,
532 free_commit_list(commit
->parents
);
533 commit
->parents
= NULL
;
535 if (saved_nrl
< rev
->diffopt
.needed_rename_limit
)
536 saved_nrl
= rev
->diffopt
.needed_rename_limit
;
537 if (rev
->diffopt
.degraded_cc_to_c
)
540 rev
->diffopt
.degraded_cc_to_c
= saved_dcctc
;
541 rev
->diffopt
.needed_rename_limit
= saved_nrl
;
543 if (rev
->remerge_diff
) {
544 tmp_objdir_destroy(rev
->remerge_objdir
);
545 rev
->remerge_objdir
= NULL
;
548 if (rev
->diffopt
.output_format
& DIFF_FORMAT_CHECKDIFF
&&
549 rev
->diffopt
.flags
.check_failed
) {
552 return diff_result_code(&rev
->diffopt
);
555 static int cmd_log_walk(struct rev_info
*rev
)
559 rev
->diffopt
.no_free
= 1;
560 retval
= cmd_log_walk_no_free(rev
);
561 rev
->diffopt
.no_free
= 0;
562 diff_free(&rev
->diffopt
);
566 static int git_log_config(const char *var
, const char *value
,
567 const struct config_context
*ctx
, void *cb
)
569 const char *slot_name
;
571 if (!strcmp(var
, "format.pretty"))
572 return git_config_string(&fmt_pretty
, var
, value
);
573 if (!strcmp(var
, "format.subjectprefix"))
574 return git_config_string(&fmt_patch_subject_prefix
, var
, value
);
575 if (!strcmp(var
, "format.filenamemaxlength")) {
576 fmt_patch_name_max
= git_config_int(var
, value
, ctx
->kvi
);
579 if (!strcmp(var
, "format.encodeemailheaders")) {
580 default_encode_email_headers
= git_config_bool(var
, value
);
583 if (!strcmp(var
, "log.abbrevcommit")) {
584 default_abbrev_commit
= git_config_bool(var
, value
);
587 if (!strcmp(var
, "log.date"))
588 return git_config_string(&default_date_mode
, var
, value
);
589 if (!strcmp(var
, "log.decorate")) {
590 decoration_style
= parse_decoration_style(value
);
591 if (decoration_style
< 0)
592 decoration_style
= 0; /* maybe warn? */
595 if (!strcmp(var
, "log.diffmerges")) {
597 return config_error_nonbool(var
);
598 return diff_merges_config(value
);
600 if (!strcmp(var
, "log.showroot")) {
601 default_show_root
= git_config_bool(var
, value
);
604 if (!strcmp(var
, "log.follow")) {
605 default_follow
= git_config_bool(var
, value
);
608 if (skip_prefix(var
, "color.decorate.", &slot_name
))
609 return parse_decorate_color_config(var
, slot_name
, value
);
610 if (!strcmp(var
, "log.mailmap")) {
611 use_mailmap_config
= git_config_bool(var
, value
);
614 if (!strcmp(var
, "log.showsignature")) {
615 default_show_signature
= git_config_bool(var
, value
);
619 return git_diff_ui_config(var
, value
, ctx
, cb
);
622 int cmd_whatchanged(int argc
, const char **argv
, const char *prefix
)
625 struct setup_revision_opt opt
;
628 git_config(git_log_config
, NULL
);
630 repo_init_revisions(the_repository
, &rev
, prefix
);
631 git_config(grep_config
, &rev
.grep_filter
);
634 rev
.simplify_history
= 0;
635 memset(&opt
, 0, sizeof(opt
));
637 opt
.revarg_opt
= REVARG_COMMITTISH
;
638 cmd_log_init(argc
, argv
, prefix
, &rev
, &opt
);
639 if (!rev
.diffopt
.output_format
)
640 rev
.diffopt
.output_format
= DIFF_FORMAT_RAW
;
641 return cmd_log_deinit(cmd_log_walk(&rev
), &rev
);
644 static void show_tagger(const char *buf
, struct rev_info
*rev
)
646 struct strbuf out
= STRBUF_INIT
;
647 struct pretty_print_context pp
= {0};
649 pp
.fmt
= rev
->commit_format
;
650 pp
.date_mode
= rev
->date_mode
;
651 pp_user_info(&pp
, "Tagger", &out
, buf
, get_log_output_encoding());
652 fprintf(rev
->diffopt
.file
, "%s", out
.buf
);
653 strbuf_release(&out
);
656 static int show_blob_object(const struct object_id
*oid
, struct rev_info
*rev
, const char *obj_name
)
658 struct object_id oidc
;
659 struct object_context obj_context
;
663 fflush(rev
->diffopt
.file
);
664 if (!rev
->diffopt
.flags
.textconv_set_via_cmdline
||
665 !rev
->diffopt
.flags
.allow_textconv
)
666 return stream_blob_to_fd(1, oid
, NULL
, 0);
668 if (get_oid_with_context(the_repository
, obj_name
,
670 &oidc
, &obj_context
))
671 die(_("not a valid object name %s"), obj_name
);
672 if (!obj_context
.path
||
673 !textconv_object(the_repository
, obj_context
.path
,
674 obj_context
.mode
, &oidc
, 1, &buf
, &size
)) {
675 free(obj_context
.path
);
676 return stream_blob_to_fd(1, oid
, NULL
, 0);
680 die(_("git show %s: bad file"), obj_name
);
682 write_or_die(1, buf
, size
);
683 free(obj_context
.path
);
687 static int show_tag_object(const struct object_id
*oid
, struct rev_info
*rev
)
690 enum object_type type
;
691 char *buf
= repo_read_object_file(the_repository
, oid
, &type
, &size
);
695 return error(_("could not read object %s"), oid_to_hex(oid
));
697 assert(type
== OBJ_TAG
);
698 while (offset
< size
&& buf
[offset
] != '\n') {
699 int new_offset
= offset
+ 1;
701 while (new_offset
< size
&& buf
[new_offset
++] != '\n')
703 if (skip_prefix(buf
+ offset
, "tagger ", &ident
))
704 show_tagger(ident
, rev
);
709 fwrite(buf
+ offset
, size
- offset
, 1, rev
->diffopt
.file
);
714 static int show_tree_object(const struct object_id
*oid UNUSED
,
715 struct strbuf
*base UNUSED
,
716 const char *pathname
, unsigned mode
,
719 FILE *file
= context
;
720 fprintf(file
, "%s%s\n", pathname
, S_ISDIR(mode
) ? "/" : "");
724 static void show_setup_revisions_tweak(struct rev_info
*rev
)
726 if (rev
->first_parent_only
)
727 diff_merges_default_to_first_parent(rev
);
729 diff_merges_default_to_dense_combined(rev
);
730 if (!rev
->diffopt
.output_format
)
731 rev
->diffopt
.output_format
= DIFF_FORMAT_PATCH
;
734 int cmd_show(int argc
, const char **argv
, const char *prefix
)
738 struct setup_revision_opt opt
;
739 struct pathspec match_all
;
743 git_config(git_log_config
, NULL
);
745 if (the_repository
->gitdir
) {
746 prepare_repo_settings(the_repository
);
747 the_repository
->settings
.command_requires_full_index
= 0;
750 memset(&match_all
, 0, sizeof(match_all
));
751 repo_init_revisions(the_repository
, &rev
, prefix
);
752 git_config(grep_config
, &rev
.grep_filter
);
755 rev
.always_show_header
= 1;
757 rev
.diffopt
.stat_width
= -1; /* Scale to real terminal size */
759 memset(&opt
, 0, sizeof(opt
));
761 opt
.tweak
= show_setup_revisions_tweak
;
762 cmd_log_init(argc
, argv
, prefix
, &rev
, &opt
);
765 return cmd_log_deinit(cmd_log_walk(&rev
), &rev
);
767 rev
.diffopt
.no_free
= 1;
768 for (i
= 0; i
< rev
.pending
.nr
&& !ret
; i
++) {
769 struct object
*o
= rev
.pending
.objects
[i
].item
;
770 const char *name
= rev
.pending
.objects
[i
].name
;
773 ret
= show_blob_object(&o
->oid
, &rev
, name
);
776 struct tag
*t
= (struct tag
*)o
;
777 struct object_id
*oid
= get_tagged_oid(t
);
781 fprintf(rev
.diffopt
.file
, "%stag %s%s\n",
782 diff_get_color_opt(&rev
.diffopt
, DIFF_COMMIT
),
784 diff_get_color_opt(&rev
.diffopt
, DIFF_RESET
));
785 ret
= show_tag_object(&o
->oid
, &rev
);
789 o
= parse_object(the_repository
, oid
);
791 ret
= error(_("could not read object %s"),
793 rev
.pending
.objects
[i
].item
= o
;
800 fprintf(rev
.diffopt
.file
, "%stree %s%s\n\n",
801 diff_get_color_opt(&rev
.diffopt
, DIFF_COMMIT
),
803 diff_get_color_opt(&rev
.diffopt
, DIFF_RESET
));
804 read_tree(the_repository
, (struct tree
*)o
,
805 &match_all
, show_tree_object
,
811 struct object_array old
;
812 struct object_array blank
= OBJECT_ARRAY_INIT
;
814 memcpy(&old
, &rev
.pending
, sizeof(old
));
815 memcpy(&rev
.pending
, &blank
, sizeof(rev
.pending
));
817 add_object_array(o
, name
, &rev
.pending
);
818 ret
= cmd_log_walk_no_free(&rev
);
822 * object_array_clear(&pending). It was
823 * cleared already in prepare_revision_walk()
825 memcpy(&rev
.pending
, &old
, sizeof(rev
.pending
));
829 ret
= error(_("unknown type: %d"), o
->type
);
833 rev
.diffopt
.no_free
= 0;
834 diff_free(&rev
.diffopt
);
836 return cmd_log_deinit(ret
, &rev
);
840 * This is equivalent to "git log -g --abbrev-commit --pretty=oneline"
842 int cmd_log_reflog(int argc
, const char **argv
, const char *prefix
)
845 struct setup_revision_opt opt
;
848 git_config(git_log_config
, NULL
);
850 repo_init_revisions(the_repository
, &rev
, prefix
);
851 init_reflog_walk(&rev
.reflog_info
);
852 git_config(grep_config
, &rev
.grep_filter
);
854 rev
.verbose_header
= 1;
855 memset(&opt
, 0, sizeof(opt
));
857 cmd_log_init_defaults(&rev
);
858 rev
.abbrev_commit
= 1;
859 rev
.commit_format
= CMIT_FMT_ONELINE
;
860 rev
.use_terminator
= 1;
861 rev
.always_show_header
= 1;
862 cmd_log_init_finish(argc
, argv
, prefix
, &rev
, &opt
);
864 return cmd_log_deinit(cmd_log_walk(&rev
), &rev
);
867 static void log_setup_revisions_tweak(struct rev_info
*rev
)
869 if (rev
->diffopt
.flags
.default_follow_renames
&&
870 diff_check_follow_pathspec(&rev
->prune_data
, 0))
871 rev
->diffopt
.flags
.follow_renames
= 1;
873 if (rev
->first_parent_only
)
874 diff_merges_default_to_first_parent(rev
);
877 int cmd_log(int argc
, const char **argv
, const char *prefix
)
880 struct setup_revision_opt opt
;
883 git_config(git_log_config
, NULL
);
885 repo_init_revisions(the_repository
, &rev
, prefix
);
886 git_config(grep_config
, &rev
.grep_filter
);
888 rev
.always_show_header
= 1;
889 memset(&opt
, 0, sizeof(opt
));
891 opt
.revarg_opt
= REVARG_COMMITTISH
;
892 opt
.tweak
= log_setup_revisions_tweak
;
893 cmd_log_init(argc
, argv
, prefix
, &rev
, &opt
);
894 return cmd_log_deinit(cmd_log_walk(&rev
), &rev
);
899 static const char *fmt_patch_suffix
= ".patch";
900 static int numbered
= 0;
901 static int auto_number
= 1;
903 static char *default_attach
= NULL
;
905 static struct string_list extra_hdr
= STRING_LIST_INIT_NODUP
;
906 static struct string_list extra_to
= STRING_LIST_INIT_NODUP
;
907 static struct string_list extra_cc
= STRING_LIST_INIT_NODUP
;
909 static void add_header(const char *value
)
911 struct string_list_item
*item
;
912 int len
= strlen(value
);
913 while (len
&& value
[len
- 1] == '\n')
916 if (!strncasecmp(value
, "to: ", 4)) {
917 item
= string_list_append(&extra_to
, value
+ 4);
919 } else if (!strncasecmp(value
, "cc: ", 4)) {
920 item
= string_list_append(&extra_cc
, value
+ 4);
923 item
= string_list_append(&extra_hdr
, value
);
926 item
->string
[len
] = '\0';
942 enum cover_from_description
{
949 enum auto_base_setting
{
955 static enum thread_level thread
;
956 static int do_signoff
;
957 static enum auto_base_setting auto_base
;
959 static const char *signature
= git_version_string
;
960 static const char *signature_file
;
961 static enum cover_setting config_cover_letter
;
962 static const char *config_output_directory
;
963 static enum cover_from_description cover_from_description_mode
= COVER_FROM_MESSAGE
;
964 static int show_notes
;
965 static struct display_notes_opt notes_opt
;
967 static enum cover_from_description
parse_cover_from_description(const char *arg
)
969 if (!arg
|| !strcmp(arg
, "default"))
970 return COVER_FROM_MESSAGE
;
971 else if (!strcmp(arg
, "none"))
972 return COVER_FROM_NONE
;
973 else if (!strcmp(arg
, "message"))
974 return COVER_FROM_MESSAGE
;
975 else if (!strcmp(arg
, "subject"))
976 return COVER_FROM_SUBJECT
;
977 else if (!strcmp(arg
, "auto"))
978 return COVER_FROM_AUTO
;
980 die(_("%s: invalid cover from description mode"), arg
);
983 static int git_format_config(const char *var
, const char *value
,
984 const struct config_context
*ctx
, void *cb
)
986 if (!strcmp(var
, "format.headers")) {
988 die(_("format.headers without value"));
992 if (!strcmp(var
, "format.suffix"))
993 return git_config_string(&fmt_patch_suffix
, var
, value
);
994 if (!strcmp(var
, "format.to")) {
996 return config_error_nonbool(var
);
997 string_list_append(&extra_to
, value
);
1000 if (!strcmp(var
, "format.cc")) {
1002 return config_error_nonbool(var
);
1003 string_list_append(&extra_cc
, value
);
1006 if (!strcmp(var
, "diff.color") || !strcmp(var
, "color.diff") ||
1007 !strcmp(var
, "color.ui") || !strcmp(var
, "diff.submodule")) {
1010 if (!strcmp(var
, "format.numbered")) {
1011 if (value
&& !strcasecmp(value
, "auto")) {
1015 numbered
= git_config_bool(var
, value
);
1016 auto_number
= auto_number
&& numbered
;
1019 if (!strcmp(var
, "format.attach")) {
1020 if (value
&& *value
)
1021 default_attach
= xstrdup(value
);
1022 else if (value
&& !*value
)
1023 FREE_AND_NULL(default_attach
);
1025 default_attach
= xstrdup(git_version_string
);
1028 if (!strcmp(var
, "format.thread")) {
1029 if (value
&& !strcasecmp(value
, "deep")) {
1030 thread
= THREAD_DEEP
;
1033 if (value
&& !strcasecmp(value
, "shallow")) {
1034 thread
= THREAD_SHALLOW
;
1037 thread
= git_config_bool(var
, value
) ? THREAD_SHALLOW
: THREAD_UNSET
;
1040 if (!strcmp(var
, "format.signoff")) {
1041 do_signoff
= git_config_bool(var
, value
);
1044 if (!strcmp(var
, "format.signature"))
1045 return git_config_string(&signature
, var
, value
);
1046 if (!strcmp(var
, "format.signaturefile"))
1047 return git_config_pathname(&signature_file
, var
, value
);
1048 if (!strcmp(var
, "format.coverletter")) {
1049 if (value
&& !strcasecmp(value
, "auto")) {
1050 config_cover_letter
= COVER_AUTO
;
1053 config_cover_letter
= git_config_bool(var
, value
) ? COVER_ON
: COVER_OFF
;
1056 if (!strcmp(var
, "format.outputdirectory"))
1057 return git_config_string(&config_output_directory
, var
, value
);
1058 if (!strcmp(var
, "format.useautobase")) {
1059 if (value
&& !strcasecmp(value
, "whenAble")) {
1060 auto_base
= AUTO_BASE_WHEN_ABLE
;
1063 auto_base
= git_config_bool(var
, value
) ? AUTO_BASE_ALWAYS
: AUTO_BASE_NEVER
;
1066 if (!strcmp(var
, "format.from")) {
1067 int b
= git_parse_maybe_bool(value
);
1070 from
= xstrdup(value
);
1072 from
= xstrdup(git_committer_info(IDENT_NO_DATE
));
1077 if (!strcmp(var
, "format.forceinbodyfrom")) {
1078 force_in_body_from
= git_config_bool(var
, value
);
1081 if (!strcmp(var
, "format.notes")) {
1082 int b
= git_parse_maybe_bool(value
);
1084 enable_ref_display_notes(¬es_opt
, &show_notes
, value
);
1086 enable_default_display_notes(¬es_opt
, &show_notes
);
1088 disable_display_notes(¬es_opt
, &show_notes
);
1091 if (!strcmp(var
, "format.coverfromdescription")) {
1092 cover_from_description_mode
= parse_cover_from_description(value
);
1095 if (!strcmp(var
, "format.mboxrd")) {
1096 stdout_mboxrd
= git_config_bool(var
, value
);
1099 if (!strcmp(var
, "format.noprefix")) {
1100 format_no_prefix
= 1;
1105 * ignore some porcelain config which would otherwise be parsed by
1106 * git_diff_ui_config(), via git_log_config(); we can't just avoid
1107 * diff_ui_config completely, because we do care about some ui options
1110 if (!strcmp(var
, "diff.noprefix"))
1113 return git_log_config(var
, value
, ctx
, cb
);
1116 static const char *output_directory
= NULL
;
1117 static int outdir_offset
;
1119 static int open_next_file(struct commit
*commit
, const char *subject
,
1120 struct rev_info
*rev
, int quiet
)
1122 struct strbuf filename
= STRBUF_INIT
;
1124 if (output_directory
) {
1125 strbuf_addstr(&filename
, output_directory
);
1126 strbuf_complete(&filename
, '/');
1129 if (rev
->numbered_files
)
1130 strbuf_addf(&filename
, "%d", rev
->nr
);
1132 fmt_output_commit(&filename
, commit
, rev
);
1134 fmt_output_subject(&filename
, subject
, rev
);
1137 printf("%s\n", filename
.buf
+ outdir_offset
);
1139 if (!(rev
->diffopt
.file
= fopen(filename
.buf
, "w"))) {
1140 error_errno(_("cannot open patch file %s"), filename
.buf
);
1141 strbuf_release(&filename
);
1145 strbuf_release(&filename
);
1149 static void get_patch_ids(struct rev_info
*rev
, struct patch_ids
*ids
)
1151 struct rev_info check_rev
;
1152 struct commit
*commit
, *c1
, *c2
;
1153 struct object
*o1
, *o2
;
1154 unsigned flags1
, flags2
;
1156 if (rev
->pending
.nr
!= 2)
1157 die(_("need exactly one range"));
1159 o1
= rev
->pending
.objects
[0].item
;
1160 o2
= rev
->pending
.objects
[1].item
;
1163 c1
= lookup_commit_reference(the_repository
, &o1
->oid
);
1164 c2
= lookup_commit_reference(the_repository
, &o2
->oid
);
1166 if ((flags1
& UNINTERESTING
) == (flags2
& UNINTERESTING
))
1167 die(_("not a range"));
1169 init_patch_ids(the_repository
, ids
);
1171 /* given a range a..b get all patch ids for b..a */
1172 repo_init_revisions(the_repository
, &check_rev
, rev
->prefix
);
1173 check_rev
.max_parents
= 1;
1174 o1
->flags
^= UNINTERESTING
;
1175 o2
->flags
^= UNINTERESTING
;
1176 add_pending_object(&check_rev
, o1
, "o1");
1177 add_pending_object(&check_rev
, o2
, "o2");
1178 if (prepare_revision_walk(&check_rev
))
1179 die(_("revision walk setup failed"));
1181 while ((commit
= get_revision(&check_rev
)) != NULL
) {
1182 add_commit_patch_id(commit
, ids
);
1185 /* reset for next revision walk */
1186 clear_commit_marks(c1
, SEEN
| UNINTERESTING
| SHOWN
| ADDED
);
1187 clear_commit_marks(c2
, SEEN
| UNINTERESTING
| SHOWN
| ADDED
);
1192 static void gen_message_id(struct rev_info
*info
, char *base
)
1194 struct strbuf buf
= STRBUF_INIT
;
1195 strbuf_addf(&buf
, "%s.%"PRItime
".git.%s", base
,
1196 (timestamp_t
) time(NULL
),
1197 git_committer_info(IDENT_NO_NAME
|IDENT_NO_DATE
|IDENT_STRICT
));
1198 info
->message_id
= strbuf_detach(&buf
, NULL
);
1201 static void print_signature(FILE *file
)
1203 if (!signature
|| !*signature
)
1206 fprintf(file
, "-- \n%s", signature
);
1207 if (signature
[strlen(signature
)-1] != '\n')
1212 static char *find_branch_name(struct rev_info
*rev
)
1214 int i
, positive
= -1;
1215 struct object_id branch_oid
;
1216 const struct object_id
*tip_oid
;
1217 const char *ref
, *v
;
1218 char *full_ref
, *branch
= NULL
;
1220 for (i
= 0; i
< rev
->cmdline
.nr
; i
++) {
1221 if (rev
->cmdline
.rev
[i
].flags
& UNINTERESTING
)
1230 ref
= rev
->cmdline
.rev
[positive
].name
;
1231 tip_oid
= &rev
->cmdline
.rev
[positive
].item
->oid
;
1232 if (repo_dwim_ref(the_repository
, ref
, strlen(ref
), &branch_oid
,
1234 skip_prefix(full_ref
, "refs/heads/", &v
) &&
1235 oideq(tip_oid
, &branch_oid
))
1236 branch
= xstrdup(v
);
1241 static void show_diffstat(struct rev_info
*rev
,
1242 struct commit
*origin
, struct commit
*head
)
1244 struct diff_options opts
;
1246 memcpy(&opts
, &rev
->diffopt
, sizeof(opts
));
1247 opts
.output_format
= DIFF_FORMAT_SUMMARY
| DIFF_FORMAT_DIFFSTAT
;
1248 diff_setup_done(&opts
);
1250 diff_tree_oid(get_commit_tree_oid(origin
),
1251 get_commit_tree_oid(head
),
1253 diffcore_std(&opts
);
1256 fprintf(rev
->diffopt
.file
, "\n");
1259 static void read_desc_file(struct strbuf
*buf
, const char *desc_file
)
1261 if (strbuf_read_file(buf
, desc_file
, 0) < 0)
1262 die_errno(_("unable to read branch description file '%s'"),
1266 static void prepare_cover_text(struct pretty_print_context
*pp
,
1267 const char *description_file
,
1268 const char *branch_name
,
1270 const char *encoding
,
1273 const char *subject
= "*** SUBJECT HERE ***";
1274 const char *body
= "*** BLURB HERE ***";
1275 struct strbuf description_sb
= STRBUF_INIT
;
1276 struct strbuf subject_sb
= STRBUF_INIT
;
1278 if (cover_from_description_mode
== COVER_FROM_NONE
)
1281 if (description_file
&& *description_file
)
1282 read_desc_file(&description_sb
, description_file
);
1283 else if (branch_name
&& *branch_name
)
1284 read_branch_desc(&description_sb
, branch_name
);
1285 if (!description_sb
.len
)
1288 if (cover_from_description_mode
== COVER_FROM_SUBJECT
||
1289 cover_from_description_mode
== COVER_FROM_AUTO
)
1290 body
= format_subject(&subject_sb
, description_sb
.buf
, " ");
1292 if (cover_from_description_mode
== COVER_FROM_MESSAGE
||
1293 (cover_from_description_mode
== COVER_FROM_AUTO
&&
1294 subject_sb
.len
> COVER_FROM_AUTO_MAX_SUBJECT_LEN
))
1295 body
= description_sb
.buf
;
1297 subject
= subject_sb
.buf
;
1300 pp_title_line(pp
, &subject
, sb
, encoding
, need_8bit_cte
);
1301 pp_remainder(pp
, &body
, sb
, 0);
1303 strbuf_release(&description_sb
);
1304 strbuf_release(&subject_sb
);
1307 static int get_notes_refs(struct string_list_item
*item
, void *arg
)
1309 strvec_pushf(arg
, "--notes=%s", item
->string
);
1313 static void get_notes_args(struct strvec
*arg
, struct rev_info
*rev
)
1315 if (!rev
->show_notes
) {
1316 strvec_push(arg
, "--no-notes");
1317 } else if (rev
->notes_opt
.use_default_notes
> 0 ||
1318 (rev
->notes_opt
.use_default_notes
== -1 &&
1319 !rev
->notes_opt
.extra_notes_refs
.nr
)) {
1320 strvec_push(arg
, "--notes");
1322 for_each_string_list(&rev
->notes_opt
.extra_notes_refs
, get_notes_refs
, arg
);
1326 static void make_cover_letter(struct rev_info
*rev
, int use_separate_file
,
1327 struct commit
*origin
,
1328 int nr
, struct commit
**list
,
1329 const char *description_file
,
1330 const char *branch_name
,
1333 const char *committer
;
1334 struct shortlog log
;
1335 struct strbuf sb
= STRBUF_INIT
;
1337 const char *encoding
= "UTF-8";
1338 int need_8bit_cte
= 0;
1339 struct pretty_print_context pp
= {0};
1340 struct commit
*head
= list
[0];
1342 if (!cmit_fmt_is_mail(rev
->commit_format
))
1343 die(_("cover letter needs email format"));
1345 committer
= git_committer_info(0);
1347 if (use_separate_file
&&
1348 open_next_file(NULL
, rev
->numbered_files
? NULL
: "cover-letter", rev
, quiet
))
1349 die(_("failed to create cover-letter file"));
1351 log_write_email_headers(rev
, head
, &pp
.after_subject
, &need_8bit_cte
, 0);
1353 for (i
= 0; !need_8bit_cte
&& i
< nr
; i
++) {
1354 const char *buf
= repo_get_commit_buffer(the_repository
,
1356 if (has_non_ascii(buf
))
1358 repo_unuse_commit_buffer(the_repository
, list
[i
], buf
);
1362 branch_name
= find_branch_name(rev
);
1364 pp
.fmt
= CMIT_FMT_EMAIL
;
1365 pp
.date_mode
.type
= DATE_RFC2822
;
1367 pp
.print_email_subject
= 1;
1368 pp
.encode_email_headers
= rev
->encode_email_headers
;
1369 pp_user_info(&pp
, NULL
, &sb
, committer
, encoding
);
1370 prepare_cover_text(&pp
, description_file
, branch_name
, &sb
,
1371 encoding
, need_8bit_cte
);
1372 fprintf(rev
->diffopt
.file
, "%s\n", sb
.buf
);
1374 strbuf_release(&sb
);
1376 shortlog_init(&log
);
1378 log
.wrap
= MAIL_DEFAULT_WRAP
;
1381 log
.file
= rev
->diffopt
.file
;
1382 log
.groups
= SHORTLOG_GROUP_AUTHOR
;
1383 shortlog_finish_setup(&log
);
1384 for (i
= 0; i
< nr
; i
++)
1385 shortlog_add_commit(&log
, list
[i
]);
1387 shortlog_output(&log
);
1389 /* We can only do diffstat with a unique reference point */
1391 show_diffstat(rev
, origin
, head
);
1393 if (rev
->idiff_oid1
) {
1394 fprintf_ln(rev
->diffopt
.file
, "%s", rev
->idiff_title
);
1395 show_interdiff(rev
->idiff_oid1
, rev
->idiff_oid2
, 0,
1401 * Pass minimum required diff-options to range-diff; others
1402 * can be added later if deemed desirable.
1404 struct diff_options opts
;
1405 struct strvec other_arg
= STRVEC_INIT
;
1406 struct range_diff_options range_diff_opts
= {
1407 .creation_factor
= rev
->creation_factor
,
1410 .other_arg
= &other_arg
1413 repo_diff_setup(the_repository
, &opts
);
1414 opts
.file
= rev
->diffopt
.file
;
1415 opts
.use_color
= rev
->diffopt
.use_color
;
1416 diff_setup_done(&opts
);
1417 fprintf_ln(rev
->diffopt
.file
, "%s", rev
->rdiff_title
);
1418 get_notes_args(&other_arg
, rev
);
1419 show_range_diff(rev
->rdiff1
, rev
->rdiff2
, &range_diff_opts
);
1420 strvec_clear(&other_arg
);
1424 static char *clean_message_id(const char *msg_id
)
1427 const char *a
, *z
, *m
;
1430 while ((ch
= *m
) && (isspace(ch
) || (ch
== '<')))
1435 if (!isspace(ch
) && (ch
!= '>'))
1440 die(_("insane in-reply-to: %s"), msg_id
);
1443 return xmemdupz(a
, z
- a
);
1446 static const char *set_outdir(const char *prefix
, const char *output_directory
)
1448 if (output_directory
&& is_absolute_path(output_directory
))
1449 return output_directory
;
1451 if (!prefix
|| !*prefix
) {
1452 if (output_directory
)
1453 return output_directory
;
1454 /* The user did not explicitly ask for "./" */
1459 outdir_offset
= strlen(prefix
);
1460 if (!output_directory
)
1463 return prefix_filename(prefix
, output_directory
);
1466 static const char * const builtin_format_patch_usage
[] = {
1467 N_("git format-patch [<options>] [<since> | <revision-range>]"),
1471 static int keep_subject
= 0;
1473 static int keep_callback(const struct option
*opt
, const char *arg
, int unset
)
1475 BUG_ON_OPT_NEG(unset
);
1476 BUG_ON_OPT_ARG(arg
);
1477 ((struct rev_info
*)opt
->value
)->total
= -1;
1482 static int subject_prefix
= 0;
1484 static int subject_prefix_callback(const struct option
*opt
, const char *arg
,
1487 struct strbuf
*sprefix
;
1489 BUG_ON_OPT_NEG(unset
);
1490 sprefix
= opt
->value
;
1492 strbuf_reset(sprefix
);
1493 strbuf_addstr(sprefix
, arg
);
1497 static int numbered_cmdline_opt
= 0;
1499 static int numbered_callback(const struct option
*opt
, const char *arg
,
1502 BUG_ON_OPT_ARG(arg
);
1503 *(int *)opt
->value
= numbered_cmdline_opt
= unset
? 0 : 1;
1509 static int no_numbered_callback(const struct option
*opt
, const char *arg
,
1512 BUG_ON_OPT_NEG(unset
);
1513 return numbered_callback(opt
, arg
, 1);
1516 static int output_directory_callback(const struct option
*opt
, const char *arg
,
1519 const char **dir
= (const char **)opt
->value
;
1520 BUG_ON_OPT_NEG(unset
);
1522 die(_("two output directories?"));
1527 static int thread_callback(const struct option
*opt
, const char *arg
, int unset
)
1529 enum thread_level
*thread
= (enum thread_level
*)opt
->value
;
1531 *thread
= THREAD_UNSET
;
1532 else if (!arg
|| !strcmp(arg
, "shallow"))
1533 *thread
= THREAD_SHALLOW
;
1534 else if (!strcmp(arg
, "deep"))
1535 *thread
= THREAD_DEEP
;
1537 * Please update _git_formatpatch() in git-completion.bash
1538 * when you add new options.
1545 static int attach_callback(const struct option
*opt
, const char *arg
, int unset
)
1547 struct rev_info
*rev
= (struct rev_info
*)opt
->value
;
1549 rev
->mime_boundary
= NULL
;
1551 rev
->mime_boundary
= arg
;
1553 rev
->mime_boundary
= git_version_string
;
1554 rev
->no_inline
= unset
? 0 : 1;
1558 static int inline_callback(const struct option
*opt
, const char *arg
, int unset
)
1560 struct rev_info
*rev
= (struct rev_info
*)opt
->value
;
1562 rev
->mime_boundary
= NULL
;
1564 rev
->mime_boundary
= arg
;
1566 rev
->mime_boundary
= git_version_string
;
1571 static int header_callback(const struct option
*opt UNUSED
, const char *arg
,
1575 string_list_clear(&extra_hdr
, 0);
1576 string_list_clear(&extra_to
, 0);
1577 string_list_clear(&extra_cc
, 0);
1584 static int from_callback(const struct option
*opt
, const char *arg
, int unset
)
1586 char **from
= opt
->value
;
1593 *from
= xstrdup(arg
);
1595 *from
= xstrdup(git_committer_info(IDENT_NO_DATE
));
1599 static int base_callback(const struct option
*opt
, const char *arg
, int unset
)
1601 const char **base_commit
= opt
->value
;
1604 auto_base
= AUTO_BASE_NEVER
;
1605 *base_commit
= NULL
;
1606 } else if (!strcmp(arg
, "auto")) {
1607 auto_base
= AUTO_BASE_ALWAYS
;
1608 *base_commit
= NULL
;
1610 auto_base
= AUTO_BASE_NEVER
;
1616 struct base_tree_info
{
1617 struct object_id base_commit
;
1618 int nr_patch_id
, alloc_patch_id
;
1619 struct object_id
*patch_id
;
1622 static struct commit
*get_base_commit(const char *base_commit
,
1623 struct commit
**list
,
1626 struct commit
*base
= NULL
;
1627 struct commit
**rev
;
1628 int i
= 0, rev_nr
= 0, auto_select
, die_on_failure
;
1630 switch (auto_base
) {
1631 case AUTO_BASE_NEVER
:
1636 /* no base information is requested */
1640 case AUTO_BASE_ALWAYS
:
1641 case AUTO_BASE_WHEN_ABLE
:
1643 BUG("requested automatic base selection but a commit was provided");
1646 die_on_failure
= auto_base
== AUTO_BASE_ALWAYS
;
1650 BUG("unexpected automatic base selection method");
1654 base
= lookup_commit_reference_by_name(base_commit
);
1656 die(_("unknown commit %s"), base_commit
);
1658 struct branch
*curr_branch
= branch_get(NULL
);
1659 const char *upstream
= branch_get_upstream(curr_branch
, NULL
);
1661 struct commit_list
*base_list
;
1662 struct commit
*commit
;
1663 struct object_id oid
;
1665 if (repo_get_oid(the_repository
, upstream
, &oid
)) {
1667 die(_("failed to resolve '%s' as a valid ref"), upstream
);
1671 commit
= lookup_commit_or_die(&oid
, "upstream base");
1672 base_list
= repo_get_merge_bases_many(the_repository
,
1675 /* There should be one and only one merge base. */
1676 if (!base_list
|| base_list
->next
) {
1677 if (die_on_failure
) {
1678 die(_("could not find exact merge base"));
1680 free_commit_list(base_list
);
1684 base
= base_list
->item
;
1685 free_commit_list(base_list
);
1688 die(_("failed to get upstream, if you want to record base commit automatically,\n"
1689 "please use git branch --set-upstream-to to track a remote branch.\n"
1690 "Or you could specify base commit by --base=<base-commit-id> manually"));
1696 ALLOC_ARRAY(rev
, total
);
1697 for (i
= 0; i
< total
; i
++)
1702 * Get merge base through pair-wise computations
1703 * and store it in rev[0].
1705 while (rev_nr
> 1) {
1706 for (i
= 0; i
< rev_nr
/ 2; i
++) {
1707 struct commit_list
*merge_base
;
1708 merge_base
= repo_get_merge_bases(the_repository
,
1711 if (!merge_base
|| merge_base
->next
) {
1712 if (die_on_failure
) {
1713 die(_("failed to find exact merge base"));
1720 rev
[i
] = merge_base
->item
;
1724 rev
[i
] = rev
[2 * i
];
1725 rev_nr
= DIV_ROUND_UP(rev_nr
, 2);
1728 if (!repo_in_merge_bases(the_repository
, base
, rev
[0])) {
1729 if (die_on_failure
) {
1730 die(_("base commit should be the ancestor of revision list"));
1737 for (i
= 0; i
< total
; i
++) {
1738 if (base
== list
[i
]) {
1739 if (die_on_failure
) {
1740 die(_("base commit shouldn't be in revision list"));
1752 define_commit_slab(commit_base
, int);
1754 static void prepare_bases(struct base_tree_info
*bases
,
1755 struct commit
*base
,
1756 struct commit
**list
,
1759 struct commit
*commit
;
1760 struct rev_info revs
;
1761 struct diff_options diffopt
;
1762 struct commit_base commit_base
;
1768 init_commit_base(&commit_base
);
1769 repo_diff_setup(the_repository
, &diffopt
);
1770 diffopt
.flags
.recursive
= 1;
1771 diff_setup_done(&diffopt
);
1773 oidcpy(&bases
->base_commit
, &base
->object
.oid
);
1775 repo_init_revisions(the_repository
, &revs
, NULL
);
1776 revs
.max_parents
= 1;
1777 revs
.topo_order
= 1;
1778 for (i
= 0; i
< total
; i
++) {
1779 list
[i
]->object
.flags
&= ~UNINTERESTING
;
1780 add_pending_object(&revs
, &list
[i
]->object
, "rev_list");
1781 *commit_base_at(&commit_base
, list
[i
]) = 1;
1783 base
->object
.flags
|= UNINTERESTING
;
1784 add_pending_object(&revs
, &base
->object
, "base");
1786 if (prepare_revision_walk(&revs
))
1787 die(_("revision walk setup failed"));
1789 * Traverse the commits list, get prerequisite patch ids
1790 * and stuff them in bases structure.
1792 while ((commit
= get_revision(&revs
)) != NULL
) {
1793 struct object_id oid
;
1794 struct object_id
*patch_id
;
1795 if (*commit_base_at(&commit_base
, commit
))
1797 if (commit_patch_id(commit
, &diffopt
, &oid
, 0))
1798 die(_("cannot get patch id"));
1799 ALLOC_GROW(bases
->patch_id
, bases
->nr_patch_id
+ 1, bases
->alloc_patch_id
);
1800 patch_id
= bases
->patch_id
+ bases
->nr_patch_id
;
1801 oidcpy(patch_id
, &oid
);
1802 bases
->nr_patch_id
++;
1804 clear_commit_base(&commit_base
);
1807 static void print_bases(struct base_tree_info
*bases
, FILE *file
)
1811 /* Only do this once, either for the cover or for the first one */
1812 if (is_null_oid(&bases
->base_commit
))
1815 /* Show the base commit */
1816 fprintf(file
, "\nbase-commit: %s\n", oid_to_hex(&bases
->base_commit
));
1818 /* Show the prerequisite patches */
1819 for (i
= bases
->nr_patch_id
- 1; i
>= 0; i
--)
1820 fprintf(file
, "prerequisite-patch-id: %s\n", oid_to_hex(&bases
->patch_id
[i
]));
1822 free(bases
->patch_id
);
1823 bases
->nr_patch_id
= 0;
1824 bases
->alloc_patch_id
= 0;
1825 oidclr(&bases
->base_commit
);
1828 static const char *diff_title(struct strbuf
*sb
,
1829 const char *reroll_count
,
1830 const char *generic
,
1831 const char *rerolled
)
1835 /* RFC may be v0, so allow -v1 to diff against v0 */
1836 if (reroll_count
&& !strtol_i(reroll_count
, 10, &v
) &&
1838 strbuf_addf(sb
, rerolled
, v
- 1);
1840 strbuf_addstr(sb
, generic
);
1844 static void infer_range_diff_ranges(struct strbuf
*r1
,
1847 struct commit
*origin
,
1848 struct commit
*head
)
1850 const char *head_oid
= oid_to_hex(&head
->object
.oid
);
1851 int prev_is_range
= is_range_diff_range(prev
);
1854 strbuf_addstr(r1
, prev
);
1856 strbuf_addf(r1
, "%s..%s", head_oid
, prev
);
1859 strbuf_addf(r2
, "%s..%s", oid_to_hex(&origin
->object
.oid
), head_oid
);
1860 else if (prev_is_range
)
1861 die(_("failed to infer range-diff origin of current series"));
1863 warning(_("using '%s' as range-diff origin of current series"), prev
);
1864 strbuf_addf(r2
, "%s..%s", prev
, head_oid
);
1868 int cmd_format_patch(int argc
, const char **argv
, const char *prefix
)
1870 struct commit
*commit
;
1871 struct commit
**list
= NULL
;
1872 struct rev_info rev
;
1873 char *to_free
= NULL
;
1874 struct setup_revision_opt s_r_opt
;
1875 int nr
= 0, total
, i
;
1877 int start_number
= -1;
1878 int just_numbers
= 0;
1879 int ignore_if_in_upstream
= 0;
1880 int cover_letter
= -1;
1881 int boundary_count
= 0;
1882 int no_binary_diff
= 0;
1883 int zero_commit
= 0;
1884 struct commit
*origin
= NULL
;
1885 const char *in_reply_to
= NULL
;
1886 struct patch_ids ids
;
1887 struct strbuf buf
= STRBUF_INIT
;
1888 int use_patch_format
= 0;
1890 const char *reroll_count
= NULL
;
1891 char *cover_from_description_arg
= NULL
;
1892 char *description_file
= NULL
;
1893 char *branch_name
= NULL
;
1894 char *base_commit
= NULL
;
1895 struct base_tree_info bases
;
1896 struct commit
*base
;
1897 int show_progress
= 0;
1898 struct progress
*progress
= NULL
;
1899 struct oid_array idiff_prev
= OID_ARRAY_INIT
;
1900 struct strbuf idiff_title
= STRBUF_INIT
;
1901 const char *rdiff_prev
= NULL
;
1902 struct strbuf rdiff1
= STRBUF_INIT
;
1903 struct strbuf rdiff2
= STRBUF_INIT
;
1904 struct strbuf rdiff_title
= STRBUF_INIT
;
1905 struct strbuf sprefix
= STRBUF_INIT
;
1906 int creation_factor
= -1;
1909 const struct option builtin_format_patch_options
[] = {
1910 OPT_CALLBACK_F('n', "numbered", &numbered
, NULL
,
1911 N_("use [PATCH n/m] even with a single patch"),
1912 PARSE_OPT_NOARG
, numbered_callback
),
1913 OPT_CALLBACK_F('N', "no-numbered", &numbered
, NULL
,
1914 N_("use [PATCH] even with multiple patches"),
1915 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
, no_numbered_callback
),
1916 OPT_BOOL('s', "signoff", &do_signoff
, N_("add a Signed-off-by trailer")),
1917 OPT_BOOL(0, "stdout", &use_stdout
,
1918 N_("print patches to standard out")),
1919 OPT_BOOL(0, "cover-letter", &cover_letter
,
1920 N_("generate a cover letter")),
1921 OPT_BOOL(0, "numbered-files", &just_numbers
,
1922 N_("use simple number sequence for output file names")),
1923 OPT_STRING(0, "suffix", &fmt_patch_suffix
, N_("sfx"),
1924 N_("use <sfx> instead of '.patch'")),
1925 OPT_INTEGER(0, "start-number", &start_number
,
1926 N_("start numbering patches at <n> instead of 1")),
1927 OPT_STRING('v', "reroll-count", &reroll_count
, N_("reroll-count"),
1928 N_("mark the series as Nth re-roll")),
1929 OPT_INTEGER(0, "filename-max-length", &fmt_patch_name_max
,
1930 N_("max length of output filename")),
1931 OPT_BOOL(0, "rfc", &rfc
, N_("use [RFC PATCH] instead of [PATCH]")),
1932 OPT_STRING(0, "cover-from-description", &cover_from_description_arg
,
1933 N_("cover-from-description-mode"),
1934 N_("generate parts of a cover letter based on a branch's description")),
1935 OPT_FILENAME(0, "description-file", &description_file
,
1936 N_("use branch description from file")),
1937 OPT_CALLBACK_F(0, "subject-prefix", &sprefix
, N_("prefix"),
1938 N_("use [<prefix>] instead of [PATCH]"),
1939 PARSE_OPT_NONEG
, subject_prefix_callback
),
1940 OPT_CALLBACK_F('o', "output-directory", &output_directory
,
1941 N_("dir"), N_("store resulting files in <dir>"),
1942 PARSE_OPT_NONEG
, output_directory_callback
),
1943 OPT_CALLBACK_F('k', "keep-subject", &rev
, NULL
,
1944 N_("don't strip/add [PATCH]"),
1945 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
, keep_callback
),
1946 OPT_BOOL(0, "no-binary", &no_binary_diff
,
1947 N_("don't output binary diffs")),
1948 OPT_BOOL(0, "zero-commit", &zero_commit
,
1949 N_("output all-zero hash in From header")),
1950 OPT_BOOL(0, "ignore-if-in-upstream", &ignore_if_in_upstream
,
1951 N_("don't include a patch matching a commit upstream")),
1952 OPT_SET_INT_F('p', "no-stat", &use_patch_format
,
1953 N_("show patch format instead of default (patch + stat)"),
1954 1, PARSE_OPT_NONEG
),
1955 OPT_GROUP(N_("Messaging")),
1956 OPT_CALLBACK(0, "add-header", NULL
, N_("header"),
1957 N_("add email header"), header_callback
),
1958 OPT_STRING_LIST(0, "to", &extra_to
, N_("email"), N_("add To: header")),
1959 OPT_STRING_LIST(0, "cc", &extra_cc
, N_("email"), N_("add Cc: header")),
1960 OPT_CALLBACK_F(0, "from", &from
, N_("ident"),
1961 N_("set From address to <ident> (or committer ident if absent)"),
1962 PARSE_OPT_OPTARG
, from_callback
),
1963 OPT_STRING(0, "in-reply-to", &in_reply_to
, N_("message-id"),
1964 N_("make first mail a reply to <message-id>")),
1965 OPT_CALLBACK_F(0, "attach", &rev
, N_("boundary"),
1966 N_("attach the patch"), PARSE_OPT_OPTARG
,
1968 OPT_CALLBACK_F(0, "inline", &rev
, N_("boundary"),
1969 N_("inline the patch"),
1970 PARSE_OPT_OPTARG
| PARSE_OPT_NONEG
,
1972 OPT_CALLBACK_F(0, "thread", &thread
, N_("style"),
1973 N_("enable message threading, styles: shallow, deep"),
1974 PARSE_OPT_OPTARG
, thread_callback
),
1975 OPT_STRING(0, "signature", &signature
, N_("signature"),
1976 N_("add a signature")),
1977 OPT_CALLBACK_F(0, "base", &base_commit
, N_("base-commit"),
1978 N_("add prerequisite tree info to the patch series"),
1980 OPT_FILENAME(0, "signature-file", &signature_file
,
1981 N_("add a signature from a file")),
1982 OPT__QUIET(&quiet
, N_("don't print the patch filenames")),
1983 OPT_BOOL(0, "progress", &show_progress
,
1984 N_("show progress while generating patches")),
1985 OPT_CALLBACK(0, "interdiff", &idiff_prev
, N_("rev"),
1986 N_("show changes against <rev> in cover letter or single patch"),
1987 parse_opt_object_name
),
1988 OPT_STRING(0, "range-diff", &rdiff_prev
, N_("refspec"),
1989 N_("show changes against <refspec> in cover letter or single patch")),
1990 OPT_INTEGER(0, "creation-factor", &creation_factor
,
1991 N_("percentage by which creation is weighted")),
1992 OPT_BOOL(0, "force-in-body-from", &force_in_body_from
,
1993 N_("show in-body From: even if identical to the e-mail header")),
1997 extra_hdr
.strdup_strings
= 1;
1998 extra_to
.strdup_strings
= 1;
1999 extra_cc
.strdup_strings
= 1;
2001 init_log_defaults();
2002 init_display_notes(¬es_opt
);
2003 git_config(git_format_config
, NULL
);
2004 repo_init_revisions(the_repository
, &rev
, prefix
);
2005 git_config(grep_config
, &rev
.grep_filter
);
2007 rev
.show_notes
= show_notes
;
2008 memcpy(&rev
.notes_opt
, ¬es_opt
, sizeof(notes_opt
));
2009 rev
.commit_format
= CMIT_FMT_EMAIL
;
2010 rev
.encode_email_headers
= default_encode_email_headers
;
2011 rev
.expand_tabs_in_log_default
= 0;
2012 rev
.verbose_header
= 1;
2014 rev
.max_parents
= 1;
2015 rev
.diffopt
.flags
.recursive
= 1;
2016 rev
.diffopt
.no_free
= 1;
2017 memset(&s_r_opt
, 0, sizeof(s_r_opt
));
2018 s_r_opt
.def
= "HEAD";
2019 s_r_opt
.revarg_opt
= REVARG_COMMITTISH
;
2021 strbuf_addstr(&sprefix
, fmt_patch_subject_prefix
);
2022 if (format_no_prefix
)
2023 diff_set_noprefix(&rev
.diffopt
);
2025 if (default_attach
) {
2026 rev
.mime_boundary
= default_attach
;
2031 * Parse the arguments before setup_revisions(), or something
2032 * like "git format-patch -o a123 HEAD^.." may fail; a123 is
2033 * possibly a valid SHA1.
2035 argc
= parse_options(argc
, argv
, prefix
, builtin_format_patch_options
,
2036 builtin_format_patch_usage
,
2037 PARSE_OPT_KEEP_ARGV0
| PARSE_OPT_KEEP_UNKNOWN_OPT
|
2038 PARSE_OPT_KEEP_DASHDASH
);
2040 rev
.force_in_body_from
= force_in_body_from
;
2042 /* Make sure "0000-$sub.patch" gives non-negative length for $sub */
2043 if (fmt_patch_name_max
<= strlen("0000-") + strlen(fmt_patch_suffix
))
2044 fmt_patch_name_max
= strlen("0000-") + strlen(fmt_patch_suffix
);
2046 if (cover_from_description_arg
)
2047 cover_from_description_mode
= parse_cover_from_description(cover_from_description_arg
);
2050 strbuf_insertstr(&sprefix
, 0, "RFC ");
2053 strbuf_addf(&sprefix
, " v%s", reroll_count
);
2054 rev
.reroll_count
= reroll_count
;
2057 rev
.subject_prefix
= sprefix
.buf
;
2059 for (i
= 0; i
< extra_hdr
.nr
; i
++) {
2060 strbuf_addstr(&buf
, extra_hdr
.items
[i
].string
);
2061 strbuf_addch(&buf
, '\n');
2065 strbuf_addstr(&buf
, "To: ");
2066 for (i
= 0; i
< extra_to
.nr
; i
++) {
2068 strbuf_addstr(&buf
, " ");
2069 strbuf_addstr(&buf
, extra_to
.items
[i
].string
);
2070 if (i
+ 1 < extra_to
.nr
)
2071 strbuf_addch(&buf
, ',');
2072 strbuf_addch(&buf
, '\n');
2076 strbuf_addstr(&buf
, "Cc: ");
2077 for (i
= 0; i
< extra_cc
.nr
; i
++) {
2079 strbuf_addstr(&buf
, " ");
2080 strbuf_addstr(&buf
, extra_cc
.items
[i
].string
);
2081 if (i
+ 1 < extra_cc
.nr
)
2082 strbuf_addch(&buf
, ',');
2083 strbuf_addch(&buf
, '\n');
2086 rev
.extra_headers
= to_free
= strbuf_detach(&buf
, NULL
);
2089 if (split_ident_line(&rev
.from_ident
, from
, strlen(from
)))
2090 die(_("invalid ident line: %s"), from
);
2093 if (start_number
< 0)
2097 * If numbered is set solely due to format.numbered in config,
2098 * and it would conflict with --keep-subject (-k) from the
2099 * command line, reset "numbered".
2101 if (numbered
&& keep_subject
&& !numbered_cmdline_opt
)
2104 if (numbered
&& keep_subject
)
2105 die(_("options '%s' and '%s' cannot be used together"), "-n", "-k");
2106 if (keep_subject
&& subject_prefix
)
2107 die(_("options '%s' and '%s' cannot be used together"), "--subject-prefix/--rfc", "-k");
2108 rev
.preserve_subject
= keep_subject
;
2110 argc
= setup_revisions(argc
, argv
, &rev
, &s_r_opt
);
2112 die(_("unrecognized argument: %s"), argv
[1]);
2114 if (rev
.diffopt
.output_format
& DIFF_FORMAT_NAME
)
2115 die(_("--name-only does not make sense"));
2116 if (rev
.diffopt
.output_format
& DIFF_FORMAT_NAME_STATUS
)
2117 die(_("--name-status does not make sense"));
2118 if (rev
.diffopt
.output_format
& DIFF_FORMAT_CHECKDIFF
)
2119 die(_("--check does not make sense"));
2120 if (rev
.remerge_diff
)
2121 die(_("--remerge-diff does not make sense"));
2123 if (!use_patch_format
&&
2124 (!rev
.diffopt
.output_format
||
2125 rev
.diffopt
.output_format
== DIFF_FORMAT_PATCH
))
2126 rev
.diffopt
.output_format
= DIFF_FORMAT_DIFFSTAT
| DIFF_FORMAT_SUMMARY
;
2127 if (!rev
.diffopt
.stat_width
)
2128 rev
.diffopt
.stat_width
= MAIL_DEFAULT_WRAP
;
2130 /* Always generate a patch */
2131 rev
.diffopt
.output_format
|= DIFF_FORMAT_PATCH
;
2132 rev
.always_show_header
= 1;
2134 rev
.zero_commit
= zero_commit
;
2135 rev
.patch_name_max
= fmt_patch_name_max
;
2137 if (!rev
.diffopt
.flags
.text
&& !no_binary_diff
)
2138 rev
.diffopt
.flags
.binary
= 1;
2141 load_display_notes(&rev
.notes_opt
);
2143 die_for_incompatible_opt3(use_stdout
, "--stdout",
2144 rev
.diffopt
.close_file
, "--output",
2145 !!output_directory
, "--output-directory");
2147 if (use_stdout
&& stdout_mboxrd
)
2148 rev
.commit_format
= CMIT_FMT_MBOXRD
;
2152 } else if (!rev
.diffopt
.close_file
) {
2155 if (!output_directory
)
2156 output_directory
= config_output_directory
;
2157 output_directory
= set_outdir(prefix
, output_directory
);
2159 if (rev
.diffopt
.use_color
!= GIT_COLOR_ALWAYS
)
2160 rev
.diffopt
.use_color
= GIT_COLOR_NEVER
;
2162 * We consider <outdir> as 'outside of gitdir', therefore avoid
2163 * applying adjust_shared_perm in s-c-l-d.
2165 saved
= get_shared_repository();
2166 set_shared_repository(0);
2167 switch (safe_create_leading_directories_const(output_directory
)) {
2172 die(_("could not create leading directories "
2173 "of '%s'"), output_directory
);
2175 set_shared_repository(saved
);
2176 if (mkdir(output_directory
, 0777) < 0 && errno
!= EEXIST
)
2177 die_errno(_("could not create directory '%s'"),
2181 if (rev
.pending
.nr
== 1) {
2184 if (rev
.max_count
< 0 && !rev
.show_root_diff
) {
2186 * This is traditional behaviour of "git format-patch
2187 * origin" that prepares what the origin side still
2190 rev
.pending
.objects
[0].item
->flags
|= UNINTERESTING
;
2191 add_head_to_pending(&rev
);
2195 * Otherwise, it is "format-patch -22 HEAD", and/or
2196 * "format-patch --root HEAD". The user wants
2197 * get_revision() to do the usual traversal.
2200 if (!strcmp(rev
.pending
.objects
[0].name
, "HEAD"))
2204 const char *ref
, *v
;
2205 ref
= resolve_ref_unsafe("HEAD", RESOLVE_REF_READING
,
2207 if (ref
&& skip_prefix(ref
, "refs/heads/", &v
))
2208 branch_name
= xstrdup(v
);
2210 branch_name
= xstrdup(""); /* no branch */
2215 * We cannot move this anywhere earlier because we do want to
2216 * know if --root was given explicitly from the command line.
2218 rev
.show_root_diff
= 1;
2220 if (ignore_if_in_upstream
) {
2221 /* Don't say anything if head and upstream are the same. */
2222 if (rev
.pending
.nr
== 2) {
2223 struct object_array_entry
*o
= rev
.pending
.objects
;
2224 if (oideq(&o
[0].item
->oid
, &o
[1].item
->oid
))
2227 get_patch_ids(&rev
, &ids
);
2230 if (prepare_revision_walk(&rev
))
2231 die(_("revision walk setup failed"));
2233 while ((commit
= get_revision(&rev
)) != NULL
) {
2234 if (commit
->object
.flags
& BOUNDARY
) {
2236 origin
= (boundary_count
== 1) ? commit
: NULL
;
2240 if (ignore_if_in_upstream
&& has_commit_patch_id(commit
, &ids
))
2244 REALLOC_ARRAY(list
, nr
);
2245 list
[nr
- 1] = commit
;
2251 if (cover_letter
== -1) {
2252 if (config_cover_letter
== COVER_AUTO
)
2253 cover_letter
= (total
> 1);
2255 cover_letter
= (config_cover_letter
== COVER_ON
);
2257 if (!keep_subject
&& auto_number
&& (total
> 1 || cover_letter
))
2260 rev
.total
= total
+ start_number
- 1;
2262 if (idiff_prev
.nr
) {
2263 if (!cover_letter
&& total
!= 1)
2264 die(_("--interdiff requires --cover-letter or single patch"));
2265 rev
.idiff_oid1
= &idiff_prev
.oid
[idiff_prev
.nr
- 1];
2266 rev
.idiff_oid2
= get_commit_tree_oid(list
[0]);
2267 rev
.idiff_title
= diff_title(&idiff_title
, reroll_count
,
2269 _("Interdiff against v%d:"));
2272 if (creation_factor
< 0)
2273 creation_factor
= RANGE_DIFF_CREATION_FACTOR_DEFAULT
;
2274 else if (!rdiff_prev
)
2275 die(_("the option '%s' requires '%s'"), "--creation-factor", "--range-diff");
2278 if (!cover_letter
&& total
!= 1)
2279 die(_("--range-diff requires --cover-letter or single patch"));
2281 infer_range_diff_ranges(&rdiff1
, &rdiff2
, rdiff_prev
,
2283 rev
.rdiff1
= rdiff1
.buf
;
2284 rev
.rdiff2
= rdiff2
.buf
;
2285 rev
.creation_factor
= creation_factor
;
2286 rev
.rdiff_title
= diff_title(&rdiff_title
, reroll_count
,
2288 _("Range-diff against v%d:"));
2292 ; /* --no-signature inhibits all signatures */
2293 } else if (signature
&& signature
!= git_version_string
) {
2294 ; /* non-default signature already set */
2295 } else if (signature_file
) {
2296 struct strbuf buf
= STRBUF_INIT
;
2298 if (strbuf_read_file(&buf
, signature_file
, 128) < 0)
2299 die_errno(_("unable to read signature file '%s'"), signature_file
);
2300 signature
= strbuf_detach(&buf
, NULL
);
2303 memset(&bases
, 0, sizeof(bases
));
2304 base
= get_base_commit(base_commit
, list
, nr
);
2306 reset_revision_walk();
2307 clear_object_flags(UNINTERESTING
);
2308 prepare_bases(&bases
, base
, list
, nr
);
2311 if (in_reply_to
|| thread
|| cover_letter
) {
2312 rev
.ref_message_ids
= xmalloc(sizeof(*rev
.ref_message_ids
));
2313 string_list_init_dup(rev
.ref_message_ids
);
2316 char *msgid
= clean_message_id(in_reply_to
);
2317 string_list_append_nodup(rev
.ref_message_ids
, msgid
);
2319 rev
.numbered_files
= just_numbers
;
2320 rev
.patch_suffix
= fmt_patch_suffix
;
2323 gen_message_id(&rev
, "cover");
2324 make_cover_letter(&rev
, !!output_directory
,
2325 origin
, nr
, list
, description_file
, branch_name
, quiet
);
2326 print_bases(&bases
, rev
.diffopt
.file
);
2327 print_signature(rev
.diffopt
.file
);
2330 /* interdiff/range-diff in cover-letter; omit from patches */
2331 rev
.idiff_oid1
= NULL
;
2334 rev
.add_signoff
= do_signoff
;
2337 progress
= start_delayed_progress(_("Generating patches"), total
);
2340 display_progress(progress
, total
- nr
);
2342 rev
.nr
= total
- nr
+ (start_number
- 1);
2343 /* Make the second and subsequent mails replies to the first */
2345 /* Have we already had a message ID? */
2346 if (rev
.message_id
) {
2348 * For deep threading: make every mail
2349 * a reply to the previous one, no
2350 * matter what other options are set.
2352 * For shallow threading:
2354 * Without --cover-letter and
2355 * --in-reply-to, make every mail a
2356 * reply to the one before.
2358 * With --in-reply-to but no
2359 * --cover-letter, make every mail a
2360 * reply to the <reply-to>.
2362 * With --cover-letter, make every
2363 * mail but the cover letter a reply
2364 * to the cover letter. The cover
2365 * letter is a reply to the
2366 * --in-reply-to, if specified.
2368 if (thread
== THREAD_SHALLOW
2369 && rev
.ref_message_ids
->nr
> 0
2370 && (!cover_letter
|| rev
.nr
> 1))
2371 free(rev
.message_id
);
2373 string_list_append_nodup(rev
.ref_message_ids
,
2376 gen_message_id(&rev
, oid_to_hex(&commit
->object
.oid
));
2379 if (output_directory
&&
2380 open_next_file(rev
.numbered_files
? NULL
: commit
, NULL
, &rev
, quiet
))
2381 die(_("failed to create output files"));
2382 shown
= log_tree_commit(&rev
, commit
);
2383 free_commit_buffer(the_repository
->parsed_objects
,
2386 /* We put one extra blank line between formatted
2387 * patches and this flag is used by log-tree code
2388 * to see if it needs to emit a LF before showing
2389 * the log; when using one file per patch, we do
2390 * not want the extra blank line.
2392 if (output_directory
)
2395 print_bases(&bases
, rev
.diffopt
.file
);
2396 if (rev
.mime_boundary
)
2397 fprintf(rev
.diffopt
.file
, "\n--%s%s--\n\n\n",
2398 mime_boundary_leader
,
2401 print_signature(rev
.diffopt
.file
);
2403 if (output_directory
)
2404 fclose(rev
.diffopt
.file
);
2406 stop_progress(&progress
);
2409 string_list_clear(&extra_to
, 0);
2410 string_list_clear(&extra_cc
, 0);
2411 string_list_clear(&extra_hdr
, 0);
2412 if (ignore_if_in_upstream
)
2413 free_patch_ids(&ids
);
2416 oid_array_clear(&idiff_prev
);
2417 strbuf_release(&idiff_title
);
2418 strbuf_release(&rdiff1
);
2419 strbuf_release(&rdiff2
);
2420 strbuf_release(&rdiff_title
);
2421 strbuf_release(&sprefix
);
2423 free(rev
.message_id
);
2424 if (rev
.ref_message_ids
)
2425 string_list_clear(rev
.ref_message_ids
, 0);
2426 free(rev
.ref_message_ids
);
2427 return cmd_log_deinit(0, &rev
);
2430 static int add_pending_commit(const char *arg
, struct rev_info
*revs
, int flags
)
2432 struct object_id oid
;
2433 if (repo_get_oid(the_repository
, arg
, &oid
) == 0) {
2434 struct commit
*commit
= lookup_commit_reference(the_repository
,
2437 commit
->object
.flags
|= flags
;
2438 add_pending_object(revs
, &commit
->object
, arg
);
2445 static const char * const cherry_usage
[] = {
2446 N_("git cherry [-v] [<upstream> [<head> [<limit>]]]"),
2450 static void print_commit(char sign
, struct commit
*commit
, int verbose
,
2451 int abbrev
, FILE *file
)
2454 fprintf(file
, "%c %s\n", sign
,
2455 repo_find_unique_abbrev(the_repository
, &commit
->object
.oid
, abbrev
));
2457 struct strbuf buf
= STRBUF_INIT
;
2458 pp_commit_easy(CMIT_FMT_ONELINE
, commit
, &buf
);
2459 fprintf(file
, "%c %s %s\n", sign
,
2460 repo_find_unique_abbrev(the_repository
, &commit
->object
.oid
, abbrev
),
2462 strbuf_release(&buf
);
2466 int cmd_cherry(int argc
, const char **argv
, const char *prefix
)
2468 struct rev_info revs
;
2469 struct patch_ids ids
;
2470 struct commit
*commit
;
2471 struct commit_list
*list
= NULL
;
2472 struct branch
*current_branch
;
2473 const char *upstream
;
2474 const char *head
= "HEAD";
2475 const char *limit
= NULL
;
2476 int verbose
= 0, abbrev
= 0;
2478 struct option options
[] = {
2479 OPT__ABBREV(&abbrev
),
2480 OPT__VERBOSE(&verbose
, N_("be verbose")),
2484 argc
= parse_options(argc
, argv
, prefix
, options
, cherry_usage
, 0);
2497 current_branch
= branch_get(NULL
);
2498 upstream
= branch_get_upstream(current_branch
, NULL
);
2500 fprintf(stderr
, _("Could not find a tracked"
2501 " remote branch, please"
2502 " specify <upstream> manually.\n"));
2503 usage_with_options(cherry_usage
, options
);
2507 repo_init_revisions(the_repository
, &revs
, prefix
);
2508 revs
.max_parents
= 1;
2510 if (add_pending_commit(head
, &revs
, 0))
2511 die(_("unknown commit %s"), head
);
2512 if (add_pending_commit(upstream
, &revs
, UNINTERESTING
))
2513 die(_("unknown commit %s"), upstream
);
2515 /* Don't say anything if head and upstream are the same. */
2516 if (revs
.pending
.nr
== 2) {
2517 struct object_array_entry
*o
= revs
.pending
.objects
;
2518 if (oideq(&o
[0].item
->oid
, &o
[1].item
->oid
))
2522 get_patch_ids(&revs
, &ids
);
2524 if (limit
&& add_pending_commit(limit
, &revs
, UNINTERESTING
))
2525 die(_("unknown commit %s"), limit
);
2527 /* reverse the list of commits */
2528 if (prepare_revision_walk(&revs
))
2529 die(_("revision walk setup failed"));
2530 while ((commit
= get_revision(&revs
)) != NULL
) {
2531 commit_list_insert(commit
, &list
);
2537 commit
= list
->item
;
2538 if (has_commit_patch_id(commit
, &ids
))
2540 print_commit(sign
, commit
, verbose
, abbrev
, revs
.diffopt
.file
);
2544 free_patch_ids(&ids
);