2 * Builtin "git log" and related commands (show, whatchanged)
4 * (C) Copyright 2006 Linus Torvalds
7 #define USE_THE_INDEX_COMPATIBILITY_MACROS
11 #include "object-store.h"
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 "interdiff.h"
37 #include "range-diff.h"
39 #define MAIL_DEFAULT_WRAP 72
41 /* Set a default date-time format for git log ("log.date" config variable) */
42 static const char *default_date_mode
= NULL
;
44 static int default_abbrev_commit
;
45 static int default_show_root
= 1;
46 static int default_follow
;
47 static int default_show_signature
;
48 static int decoration_style
;
49 static int decoration_given
;
50 static int use_mailmap_config
= -1;
51 static const char *fmt_patch_subject_prefix
= "PATCH";
52 static const char *fmt_pretty
;
54 static const char * const builtin_log_usage
[] = {
55 N_("git log [<options>] [<revision-range>] [[--] <path>...]"),
56 N_("git show [<options>] <object>..."),
60 struct line_opt_callback_data
{
63 struct string_list args
;
66 static int session_is_interactive(void)
68 return isatty(1) || pager_in_use();
71 static int auto_decoration_style(void)
73 return session_is_interactive() ? DECORATE_SHORT_REFS
: 0;
76 static int parse_decoration_style(const char *value
)
78 switch (git_parse_maybe_bool(value
)) {
80 return DECORATE_SHORT_REFS
;
86 if (!strcmp(value
, "full"))
87 return DECORATE_FULL_REFS
;
88 else if (!strcmp(value
, "short"))
89 return DECORATE_SHORT_REFS
;
90 else if (!strcmp(value
, "auto"))
91 return auto_decoration_style();
93 * Please update _git_log() in git-completion.bash when you
94 * add new decoration styles.
99 static int decorate_callback(const struct option
*opt
, const char *arg
, int unset
)
102 decoration_style
= 0;
104 decoration_style
= parse_decoration_style(arg
);
106 decoration_style
= DECORATE_SHORT_REFS
;
108 if (decoration_style
< 0)
109 die(_("invalid --decorate option: %s"), arg
);
111 decoration_given
= 1;
116 static int log_line_range_callback(const struct option
*option
, const char *arg
, int unset
)
118 struct line_opt_callback_data
*data
= option
->value
;
120 BUG_ON_OPT_NEG(unset
);
125 data
->rev
->line_level_traverse
= 1;
126 string_list_append(&data
->args
, arg
);
131 static void init_log_defaults(void)
133 init_grep_defaults(the_repository
);
134 init_diff_ui_defaults();
136 decoration_style
= auto_decoration_style();
139 static void cmd_log_init_defaults(struct rev_info
*rev
)
142 get_commit_format(fmt_pretty
, rev
);
144 rev
->diffopt
.flags
.default_follow_renames
= 1;
145 rev
->verbose_header
= 1;
146 rev
->diffopt
.flags
.recursive
= 1;
147 rev
->diffopt
.stat_width
= -1; /* use full terminal width */
148 rev
->diffopt
.stat_graph_width
= -1; /* respect statGraphWidth config */
149 rev
->abbrev_commit
= default_abbrev_commit
;
150 rev
->show_root_diff
= default_show_root
;
151 rev
->subject_prefix
= fmt_patch_subject_prefix
;
152 rev
->show_signature
= default_show_signature
;
153 rev
->diffopt
.flags
.allow_textconv
= 1;
155 if (default_date_mode
)
156 parse_date_format(default_date_mode
, &rev
->date_mode
);
159 static char warn_unspecified_mailmap_msg
[] =
160 N_("log.mailmap is not set; its implicit value will change in an\n"
161 "upcoming release. To squelch this message and preserve current\n"
162 "behaviour, set the log.mailmap configuration value to false.\n"
164 "To squelch this message and adopt the new behaviour now, set the\n"
165 "log.mailmap configuration value to true.\n"
167 "See 'git help config' and search for 'log.mailmap' for further information.");
169 static void cmd_log_init_finish(int argc
, const char **argv
, const char *prefix
,
170 struct rev_info
*rev
, struct setup_revision_opt
*opt
)
172 struct userformat_want w
;
173 int quiet
= 0, source
= 0, mailmap
= 0;
174 static struct line_opt_callback_data line_cb
= {NULL
, NULL
, STRING_LIST_INIT_DUP
};
175 static struct string_list decorate_refs_exclude
= STRING_LIST_INIT_NODUP
;
176 static struct string_list decorate_refs_include
= STRING_LIST_INIT_NODUP
;
177 struct decoration_filter decoration_filter
= {&decorate_refs_include
,
178 &decorate_refs_exclude
};
179 static struct revision_sources revision_sources
;
181 const struct option builtin_log_options
[] = {
182 OPT__QUIET(&quiet
, N_("suppress diff output")),
183 OPT_BOOL(0, "source", &source
, N_("show source")),
184 OPT_BOOL(0, "use-mailmap", &mailmap
, N_("Use mail map file")),
185 OPT_STRING_LIST(0, "decorate-refs", &decorate_refs_include
,
186 N_("pattern"), N_("only decorate refs that match <pattern>")),
187 OPT_STRING_LIST(0, "decorate-refs-exclude", &decorate_refs_exclude
,
188 N_("pattern"), N_("do not decorate refs that match <pattern>")),
189 { OPTION_CALLBACK
, 0, "decorate", NULL
, NULL
, N_("decorate options"),
190 PARSE_OPT_OPTARG
, decorate_callback
},
191 OPT_CALLBACK('L', NULL
, &line_cb
, "n,m:file",
192 N_("Process line range n,m in file, counting from 1"),
193 log_line_range_callback
),
198 line_cb
.prefix
= prefix
;
200 mailmap
= use_mailmap_config
;
201 argc
= parse_options(argc
, argv
, prefix
,
202 builtin_log_options
, builtin_log_usage
,
203 PARSE_OPT_KEEP_ARGV0
| PARSE_OPT_KEEP_UNKNOWN
|
204 PARSE_OPT_KEEP_DASHDASH
);
207 rev
->diffopt
.output_format
|= DIFF_FORMAT_NO_OUTPUT
;
208 argc
= setup_revisions(argc
, argv
, rev
, opt
);
210 /* Any arguments at this point are not recognized */
212 die(_("unrecognized argument: %s"), argv
[1]);
214 memset(&w
, 0, sizeof(w
));
215 userformat_find_requirements(NULL
, &w
);
218 if (session_is_interactive() && !rev
->pretty_given
)
219 warning("%s\n", _(warn_unspecified_mailmap_msg
));
224 if (!rev
->show_notes_given
&& (!rev
->pretty_given
|| w
.notes
))
227 init_display_notes(&rev
->notes_opt
);
229 if ((rev
->diffopt
.pickaxe_opts
& DIFF_PICKAXE_KINDS_MASK
) ||
230 rev
->diffopt
.filter
|| rev
->diffopt
.flags
.follow_renames
)
231 rev
->always_show_header
= 0;
233 if (source
|| w
.source
) {
234 init_revision_sources(&revision_sources
);
235 rev
->sources
= &revision_sources
;
239 rev
->mailmap
= xcalloc(1, sizeof(struct string_list
));
240 read_mailmap(rev
->mailmap
, NULL
);
243 if (rev
->pretty_given
&& rev
->commit_format
== CMIT_FMT_RAW
) {
245 * "log --pretty=raw" is special; ignore UI oriented
246 * configuration variables such as decoration.
248 if (!decoration_given
)
249 decoration_style
= 0;
250 if (!rev
->abbrev_commit_given
)
251 rev
->abbrev_commit
= 0;
254 if (decoration_style
) {
255 rev
->show_decorations
= 1;
256 load_ref_decorations(&decoration_filter
, decoration_style
);
259 if (rev
->line_level_traverse
)
260 line_log_init(rev
, line_cb
.prefix
, &line_cb
.args
);
265 static void cmd_log_init(int argc
, const char **argv
, const char *prefix
,
266 struct rev_info
*rev
, struct setup_revision_opt
*opt
)
268 cmd_log_init_defaults(rev
);
269 cmd_log_init_finish(argc
, argv
, prefix
, rev
, opt
);
273 * This gives a rough estimate for how many commits we
274 * will print out in the list.
276 static int estimate_commit_count(struct commit_list
*list
)
281 struct commit
*commit
= list
->item
;
282 unsigned int flags
= commit
->object
.flags
;
284 if (!(flags
& (TREESAME
| UNINTERESTING
)))
290 static void show_early_header(struct rev_info
*rev
, const char *stage
, int nr
)
292 if (rev
->shown_one
) {
294 if (rev
->commit_format
!= CMIT_FMT_ONELINE
)
295 putchar(rev
->diffopt
.line_termination
);
297 fprintf(rev
->diffopt
.file
, _("Final output: %d %s\n"), nr
, stage
);
300 static struct itimerval early_output_timer
;
302 static void log_show_early(struct rev_info
*revs
, struct commit_list
*list
)
304 int i
= revs
->early_output
, close_file
= revs
->diffopt
.close_file
;
307 revs
->diffopt
.close_file
= 0;
308 sort_in_topological_order(&list
, revs
->sort_order
);
310 struct commit
*commit
= list
->item
;
311 switch (simplify_commit(revs
, commit
)) {
314 int n
= estimate_commit_count(list
);
315 show_early_header(revs
, "incomplete", n
);
318 log_tree_commit(revs
, commit
);
325 fclose(revs
->diffopt
.file
);
331 /* Did we already get enough commits for the early output? */
334 fclose(revs
->diffopt
.file
);
339 * ..if no, then repeat it twice a second until we
342 * NOTE! We don't use "it_interval", because if the
343 * reader isn't listening, we want our output to be
344 * throttled by the writing, and not have the timer
345 * trigger every second even if we're blocked on a
348 early_output_timer
.it_value
.tv_sec
= 0;
349 early_output_timer
.it_value
.tv_usec
= 500000;
350 setitimer(ITIMER_REAL
, &early_output_timer
, NULL
);
353 static void early_output(int signal
)
355 show_early_output
= log_show_early
;
358 static void setup_early_output(void)
363 * Set up the signal handler, minimally intrusively:
364 * we only set a single volatile integer word (not
365 * using sigatomic_t - trying to avoid unnecessary
366 * system dependencies and headers), and using
369 memset(&sa
, 0, sizeof(sa
));
370 sa
.sa_handler
= early_output
;
371 sigemptyset(&sa
.sa_mask
);
372 sa
.sa_flags
= SA_RESTART
;
373 sigaction(SIGALRM
, &sa
, NULL
);
376 * If we can get the whole output in less than a
377 * tenth of a second, don't even bother doing the
378 * early-output thing..
380 * This is a one-time-only trigger.
382 early_output_timer
.it_value
.tv_sec
= 0;
383 early_output_timer
.it_value
.tv_usec
= 100000;
384 setitimer(ITIMER_REAL
, &early_output_timer
, NULL
);
387 static void finish_early_output(struct rev_info
*rev
)
389 int n
= estimate_commit_count(rev
->commits
);
390 signal(SIGALRM
, SIG_IGN
);
391 show_early_header(rev
, "done", n
);
394 static int cmd_log_walk(struct rev_info
*rev
)
396 struct commit
*commit
;
398 int saved_dcctc
= 0, close_file
= rev
->diffopt
.close_file
;
400 if (rev
->early_output
)
401 setup_early_output();
403 if (prepare_revision_walk(rev
))
404 die(_("revision walk setup failed"));
406 if (rev
->early_output
)
407 finish_early_output(rev
);
410 * For --check and --exit-code, the exit code is based on CHECK_FAILED
411 * and HAS_CHANGES being accumulated in rev->diffopt, so be careful to
412 * retain that state information if replacing rev->diffopt in this loop
414 rev
->diffopt
.close_file
= 0;
415 while ((commit
= get_revision(rev
)) != NULL
) {
416 if (!log_tree_commit(rev
, commit
) && rev
->max_count
>= 0)
418 * We decremented max_count in get_revision,
419 * but we didn't actually show the commit.
422 if (!rev
->reflog_info
) {
424 * We may show a given commit multiple times when
425 * walking the reflogs.
427 free_commit_buffer(the_repository
->parsed_objects
,
429 free_commit_list(commit
->parents
);
430 commit
->parents
= NULL
;
432 if (saved_nrl
< rev
->diffopt
.needed_rename_limit
)
433 saved_nrl
= rev
->diffopt
.needed_rename_limit
;
434 if (rev
->diffopt
.degraded_cc_to_c
)
437 rev
->diffopt
.degraded_cc_to_c
= saved_dcctc
;
438 rev
->diffopt
.needed_rename_limit
= saved_nrl
;
440 fclose(rev
->diffopt
.file
);
442 if (rev
->diffopt
.output_format
& DIFF_FORMAT_CHECKDIFF
&&
443 rev
->diffopt
.flags
.check_failed
) {
446 return diff_result_code(&rev
->diffopt
, 0);
449 static int git_log_config(const char *var
, const char *value
, void *cb
)
451 const char *slot_name
;
453 if (!strcmp(var
, "format.pretty"))
454 return git_config_string(&fmt_pretty
, var
, value
);
455 if (!strcmp(var
, "format.subjectprefix"))
456 return git_config_string(&fmt_patch_subject_prefix
, var
, value
);
457 if (!strcmp(var
, "log.abbrevcommit")) {
458 default_abbrev_commit
= git_config_bool(var
, value
);
461 if (!strcmp(var
, "log.date"))
462 return git_config_string(&default_date_mode
, var
, value
);
463 if (!strcmp(var
, "log.decorate")) {
464 decoration_style
= parse_decoration_style(value
);
465 if (decoration_style
< 0)
466 decoration_style
= 0; /* maybe warn? */
469 if (!strcmp(var
, "log.showroot")) {
470 default_show_root
= git_config_bool(var
, value
);
473 if (!strcmp(var
, "log.follow")) {
474 default_follow
= git_config_bool(var
, value
);
477 if (skip_prefix(var
, "color.decorate.", &slot_name
))
478 return parse_decorate_color_config(var
, slot_name
, value
);
479 if (!strcmp(var
, "log.mailmap")) {
480 use_mailmap_config
= git_config_bool(var
, value
);
483 if (!strcmp(var
, "log.showsignature")) {
484 default_show_signature
= git_config_bool(var
, value
);
488 if (grep_config(var
, value
, cb
) < 0)
490 if (git_gpg_config(var
, value
, cb
) < 0)
492 return git_diff_ui_config(var
, value
, cb
);
495 int cmd_whatchanged(int argc
, const char **argv
, const char *prefix
)
498 struct setup_revision_opt opt
;
501 git_config(git_log_config
, NULL
);
503 repo_init_revisions(the_repository
, &rev
, prefix
);
505 rev
.simplify_history
= 0;
506 memset(&opt
, 0, sizeof(opt
));
508 opt
.revarg_opt
= REVARG_COMMITTISH
;
509 cmd_log_init(argc
, argv
, prefix
, &rev
, &opt
);
510 if (!rev
.diffopt
.output_format
)
511 rev
.diffopt
.output_format
= DIFF_FORMAT_RAW
;
512 return cmd_log_walk(&rev
);
515 static void show_tagger(const char *buf
, struct rev_info
*rev
)
517 struct strbuf out
= STRBUF_INIT
;
518 struct pretty_print_context pp
= {0};
520 pp
.fmt
= rev
->commit_format
;
521 pp
.date_mode
= rev
->date_mode
;
522 pp_user_info(&pp
, "Tagger", &out
, buf
, get_log_output_encoding());
523 fprintf(rev
->diffopt
.file
, "%s", out
.buf
);
524 strbuf_release(&out
);
527 static int show_blob_object(const struct object_id
*oid
, struct rev_info
*rev
, const char *obj_name
)
529 struct object_id oidc
;
530 struct object_context obj_context
;
534 fflush(rev
->diffopt
.file
);
535 if (!rev
->diffopt
.flags
.textconv_set_via_cmdline
||
536 !rev
->diffopt
.flags
.allow_textconv
)
537 return stream_blob_to_fd(1, oid
, NULL
, 0);
539 if (get_oid_with_context(the_repository
, obj_name
,
541 &oidc
, &obj_context
))
542 die(_("not a valid object name %s"), obj_name
);
543 if (!obj_context
.path
||
544 !textconv_object(the_repository
, obj_context
.path
,
545 obj_context
.mode
, &oidc
, 1, &buf
, &size
)) {
546 free(obj_context
.path
);
547 return stream_blob_to_fd(1, oid
, NULL
, 0);
551 die(_("git show %s: bad file"), obj_name
);
553 write_or_die(1, buf
, size
);
554 free(obj_context
.path
);
558 static int show_tag_object(const struct object_id
*oid
, struct rev_info
*rev
)
561 enum object_type type
;
562 char *buf
= read_object_file(oid
, &type
, &size
);
566 return error(_("could not read object %s"), oid_to_hex(oid
));
568 assert(type
== OBJ_TAG
);
569 while (offset
< size
&& buf
[offset
] != '\n') {
570 int new_offset
= offset
+ 1;
572 while (new_offset
< size
&& buf
[new_offset
++] != '\n')
574 if (skip_prefix(buf
+ offset
, "tagger ", &ident
))
575 show_tagger(ident
, rev
);
580 fwrite(buf
+ offset
, size
- offset
, 1, rev
->diffopt
.file
);
585 static int show_tree_object(const struct object_id
*oid
,
587 const char *pathname
, unsigned mode
, int stage
, void *context
)
589 FILE *file
= context
;
590 fprintf(file
, "%s%s\n", pathname
, S_ISDIR(mode
) ? "/" : "");
594 static void show_setup_revisions_tweak(struct rev_info
*rev
,
595 struct setup_revision_opt
*opt
)
597 if (rev
->ignore_merges
) {
598 /* There was no "-m" on the command line */
599 rev
->ignore_merges
= 0;
600 if (!rev
->first_parent_only
&& !rev
->combine_merges
) {
601 /* No "--first-parent", "-c", or "--cc" */
602 rev
->combine_merges
= 1;
603 rev
->dense_combined_merges
= 1;
606 if (!rev
->diffopt
.output_format
)
607 rev
->diffopt
.output_format
= DIFF_FORMAT_PATCH
;
610 int cmd_show(int argc
, const char **argv
, const char *prefix
)
613 struct object_array_entry
*objects
;
614 struct setup_revision_opt opt
;
615 struct pathspec match_all
;
616 int i
, count
, ret
= 0;
619 git_config(git_log_config
, NULL
);
621 memset(&match_all
, 0, sizeof(match_all
));
622 repo_init_revisions(the_repository
, &rev
, prefix
);
624 rev
.always_show_header
= 1;
625 rev
.no_walk
= REVISION_WALK_NO_WALK_SORTED
;
626 rev
.diffopt
.stat_width
= -1; /* Scale to real terminal size */
628 memset(&opt
, 0, sizeof(opt
));
630 opt
.tweak
= show_setup_revisions_tweak
;
631 cmd_log_init(argc
, argv
, prefix
, &rev
, &opt
);
634 return cmd_log_walk(&rev
);
636 count
= rev
.pending
.nr
;
637 objects
= rev
.pending
.objects
;
638 for (i
= 0; i
< count
&& !ret
; i
++) {
639 struct object
*o
= objects
[i
].item
;
640 const char *name
= objects
[i
].name
;
643 ret
= show_blob_object(&o
->oid
, &rev
, name
);
646 struct tag
*t
= (struct tag
*)o
;
650 fprintf(rev
.diffopt
.file
, "%stag %s%s\n",
651 diff_get_color_opt(&rev
.diffopt
, DIFF_COMMIT
),
653 diff_get_color_opt(&rev
.diffopt
, DIFF_RESET
));
654 ret
= show_tag_object(&o
->oid
, &rev
);
658 o
= parse_object(the_repository
, &t
->tagged
->oid
);
660 ret
= error(_("could not read object %s"),
661 oid_to_hex(&t
->tagged
->oid
));
669 fprintf(rev
.diffopt
.file
, "%stree %s%s\n\n",
670 diff_get_color_opt(&rev
.diffopt
, DIFF_COMMIT
),
672 diff_get_color_opt(&rev
.diffopt
, DIFF_RESET
));
673 read_tree_recursive(the_repository
, (struct tree
*)o
, "",
674 0, 0, &match_all
, show_tree_object
,
679 rev
.pending
.nr
= rev
.pending
.alloc
= 0;
680 rev
.pending
.objects
= NULL
;
681 add_object_array(o
, name
, &rev
.pending
);
682 ret
= cmd_log_walk(&rev
);
685 ret
= error(_("unknown type: %d"), o
->type
);
693 * This is equivalent to "git log -g --abbrev-commit --pretty=oneline"
695 int cmd_log_reflog(int argc
, const char **argv
, const char *prefix
)
698 struct setup_revision_opt opt
;
701 git_config(git_log_config
, NULL
);
703 repo_init_revisions(the_repository
, &rev
, prefix
);
704 init_reflog_walk(&rev
.reflog_info
);
705 rev
.verbose_header
= 1;
706 memset(&opt
, 0, sizeof(opt
));
708 cmd_log_init_defaults(&rev
);
709 rev
.abbrev_commit
= 1;
710 rev
.commit_format
= CMIT_FMT_ONELINE
;
711 rev
.use_terminator
= 1;
712 rev
.always_show_header
= 1;
713 cmd_log_init_finish(argc
, argv
, prefix
, &rev
, &opt
);
715 return cmd_log_walk(&rev
);
718 static void log_setup_revisions_tweak(struct rev_info
*rev
,
719 struct setup_revision_opt
*opt
)
721 if (rev
->diffopt
.flags
.default_follow_renames
&&
722 rev
->prune_data
.nr
== 1)
723 rev
->diffopt
.flags
.follow_renames
= 1;
725 /* Turn --cc/-c into -p --cc/-c when -p was not given */
726 if (!rev
->diffopt
.output_format
&& rev
->combine_merges
)
727 rev
->diffopt
.output_format
= DIFF_FORMAT_PATCH
;
729 /* Turn -m on when --cc/-c was given */
730 if (rev
->combine_merges
)
731 rev
->ignore_merges
= 0;
734 int cmd_log(int argc
, const char **argv
, const char *prefix
)
737 struct setup_revision_opt opt
;
740 git_config(git_log_config
, NULL
);
742 repo_init_revisions(the_repository
, &rev
, prefix
);
743 rev
.always_show_header
= 1;
744 memset(&opt
, 0, sizeof(opt
));
746 opt
.revarg_opt
= REVARG_COMMITTISH
;
747 opt
.tweak
= log_setup_revisions_tweak
;
748 cmd_log_init(argc
, argv
, prefix
, &rev
, &opt
);
749 return cmd_log_walk(&rev
);
754 static const char *fmt_patch_suffix
= ".patch";
755 static int numbered
= 0;
756 static int auto_number
= 1;
758 static char *default_attach
= NULL
;
760 static struct string_list extra_hdr
= STRING_LIST_INIT_NODUP
;
761 static struct string_list extra_to
= STRING_LIST_INIT_NODUP
;
762 static struct string_list extra_cc
= STRING_LIST_INIT_NODUP
;
764 static void add_header(const char *value
)
766 struct string_list_item
*item
;
767 int len
= strlen(value
);
768 while (len
&& value
[len
- 1] == '\n')
771 if (!strncasecmp(value
, "to: ", 4)) {
772 item
= string_list_append(&extra_to
, value
+ 4);
774 } else if (!strncasecmp(value
, "cc: ", 4)) {
775 item
= string_list_append(&extra_cc
, value
+ 4);
778 item
= string_list_append(&extra_hdr
, value
);
781 item
->string
[len
] = '\0';
784 #define THREAD_SHALLOW 1
785 #define THREAD_DEEP 2
787 static int do_signoff
;
788 static int base_auto
;
790 static const char *signature
= git_version_string
;
791 static const char *signature_file
;
792 static int config_cover_letter
;
793 static const char *config_output_directory
;
802 static int git_format_config(const char *var
, const char *value
, void *cb
)
804 struct rev_info
*rev
= cb
;
806 if (!strcmp(var
, "format.headers")) {
808 die(_("format.headers without value"));
812 if (!strcmp(var
, "format.suffix"))
813 return git_config_string(&fmt_patch_suffix
, var
, value
);
814 if (!strcmp(var
, "format.to")) {
816 return config_error_nonbool(var
);
817 string_list_append(&extra_to
, value
);
820 if (!strcmp(var
, "format.cc")) {
822 return config_error_nonbool(var
);
823 string_list_append(&extra_cc
, value
);
826 if (!strcmp(var
, "diff.color") || !strcmp(var
, "color.diff") ||
827 !strcmp(var
, "color.ui") || !strcmp(var
, "diff.submodule")) {
830 if (!strcmp(var
, "format.numbered")) {
831 if (value
&& !strcasecmp(value
, "auto")) {
835 numbered
= git_config_bool(var
, value
);
836 auto_number
= auto_number
&& numbered
;
839 if (!strcmp(var
, "format.attach")) {
841 default_attach
= xstrdup(value
);
843 default_attach
= xstrdup(git_version_string
);
846 if (!strcmp(var
, "format.thread")) {
847 if (value
&& !strcasecmp(value
, "deep")) {
848 thread
= THREAD_DEEP
;
851 if (value
&& !strcasecmp(value
, "shallow")) {
852 thread
= THREAD_SHALLOW
;
855 thread
= git_config_bool(var
, value
) && THREAD_SHALLOW
;
858 if (!strcmp(var
, "format.signoff")) {
859 do_signoff
= git_config_bool(var
, value
);
862 if (!strcmp(var
, "format.signature"))
863 return git_config_string(&signature
, var
, value
);
864 if (!strcmp(var
, "format.signaturefile"))
865 return git_config_pathname(&signature_file
, var
, value
);
866 if (!strcmp(var
, "format.coverletter")) {
867 if (value
&& !strcasecmp(value
, "auto")) {
868 config_cover_letter
= COVER_AUTO
;
871 config_cover_letter
= git_config_bool(var
, value
) ? COVER_ON
: COVER_OFF
;
874 if (!strcmp(var
, "format.outputdirectory"))
875 return git_config_string(&config_output_directory
, var
, value
);
876 if (!strcmp(var
, "format.useautobase")) {
877 base_auto
= git_config_bool(var
, value
);
880 if (!strcmp(var
, "format.from")) {
881 int b
= git_parse_maybe_bool(value
);
884 from
= xstrdup(value
);
886 from
= xstrdup(git_committer_info(IDENT_NO_DATE
));
891 if (!strcmp(var
, "format.notes")) {
892 struct strbuf buf
= STRBUF_INIT
;
893 int b
= git_parse_maybe_bool(value
);
898 strbuf_addstr(&buf
, value
);
899 expand_notes_ref(&buf
);
900 string_list_append(&rev
->notes_opt
.extra_notes_refs
,
901 strbuf_detach(&buf
, NULL
));
903 rev
->notes_opt
.use_default_notes
= 1;
908 return git_log_config(var
, value
, cb
);
911 static const char *output_directory
= NULL
;
912 static int outdir_offset
;
914 static int open_next_file(struct commit
*commit
, const char *subject
,
915 struct rev_info
*rev
, int quiet
)
917 struct strbuf filename
= STRBUF_INIT
;
918 int suffix_len
= strlen(rev
->patch_suffix
) + 1;
920 if (output_directory
) {
921 strbuf_addstr(&filename
, output_directory
);
923 PATH_MAX
- FORMAT_PATCH_NAME_MAX
- suffix_len
) {
924 strbuf_release(&filename
);
925 return error(_("name of output directory is too long"));
927 strbuf_complete(&filename
, '/');
930 if (rev
->numbered_files
)
931 strbuf_addf(&filename
, "%d", rev
->nr
);
933 fmt_output_commit(&filename
, commit
, rev
);
935 fmt_output_subject(&filename
, subject
, rev
);
938 printf("%s\n", filename
.buf
+ outdir_offset
);
940 if ((rev
->diffopt
.file
= fopen(filename
.buf
, "w")) == NULL
) {
941 error_errno(_("cannot open patch file %s"), filename
.buf
);
942 strbuf_release(&filename
);
946 strbuf_release(&filename
);
950 static void get_patch_ids(struct rev_info
*rev
, struct patch_ids
*ids
)
952 struct rev_info check_rev
;
953 struct commit
*commit
, *c1
, *c2
;
954 struct object
*o1
, *o2
;
955 unsigned flags1
, flags2
;
957 if (rev
->pending
.nr
!= 2)
958 die(_("need exactly one range"));
960 o1
= rev
->pending
.objects
[0].item
;
961 o2
= rev
->pending
.objects
[1].item
;
964 c1
= lookup_commit_reference(the_repository
, &o1
->oid
);
965 c2
= lookup_commit_reference(the_repository
, &o2
->oid
);
967 if ((flags1
& UNINTERESTING
) == (flags2
& UNINTERESTING
))
968 die(_("not a range"));
970 init_patch_ids(the_repository
, ids
);
972 /* given a range a..b get all patch ids for b..a */
973 repo_init_revisions(the_repository
, &check_rev
, rev
->prefix
);
974 check_rev
.max_parents
= 1;
975 o1
->flags
^= UNINTERESTING
;
976 o2
->flags
^= UNINTERESTING
;
977 add_pending_object(&check_rev
, o1
, "o1");
978 add_pending_object(&check_rev
, o2
, "o2");
979 if (prepare_revision_walk(&check_rev
))
980 die(_("revision walk setup failed"));
982 while ((commit
= get_revision(&check_rev
)) != NULL
) {
983 add_commit_patch_id(commit
, ids
);
986 /* reset for next revision walk */
987 clear_commit_marks(c1
, SEEN
| UNINTERESTING
| SHOWN
| ADDED
);
988 clear_commit_marks(c2
, SEEN
| UNINTERESTING
| SHOWN
| ADDED
);
993 static void gen_message_id(struct rev_info
*info
, char *base
)
995 struct strbuf buf
= STRBUF_INIT
;
996 strbuf_addf(&buf
, "%s.%"PRItime
".git.%s", base
,
997 (timestamp_t
) time(NULL
),
998 git_committer_info(IDENT_NO_NAME
|IDENT_NO_DATE
|IDENT_STRICT
));
999 info
->message_id
= strbuf_detach(&buf
, NULL
);
1002 static void print_signature(FILE *file
)
1004 if (!signature
|| !*signature
)
1007 fprintf(file
, "-- \n%s", signature
);
1008 if (signature
[strlen(signature
)-1] != '\n')
1013 static void add_branch_description(struct strbuf
*buf
, const char *branch_name
)
1015 struct strbuf desc
= STRBUF_INIT
;
1016 if (!branch_name
|| !*branch_name
)
1018 read_branch_desc(&desc
, branch_name
);
1020 strbuf_addch(buf
, '\n');
1021 strbuf_addbuf(buf
, &desc
);
1022 strbuf_addch(buf
, '\n');
1024 strbuf_release(&desc
);
1027 static char *find_branch_name(struct rev_info
*rev
)
1029 int i
, positive
= -1;
1030 struct object_id branch_oid
;
1031 const struct object_id
*tip_oid
;
1032 const char *ref
, *v
;
1033 char *full_ref
, *branch
= NULL
;
1035 for (i
= 0; i
< rev
->cmdline
.nr
; i
++) {
1036 if (rev
->cmdline
.rev
[i
].flags
& UNINTERESTING
)
1045 ref
= rev
->cmdline
.rev
[positive
].name
;
1046 tip_oid
= &rev
->cmdline
.rev
[positive
].item
->oid
;
1047 if (dwim_ref(ref
, strlen(ref
), &branch_oid
, &full_ref
) &&
1048 skip_prefix(full_ref
, "refs/heads/", &v
) &&
1049 oideq(tip_oid
, &branch_oid
))
1050 branch
= xstrdup(v
);
1055 static void show_diffstat(struct rev_info
*rev
,
1056 struct commit
*origin
, struct commit
*head
)
1058 struct diff_options opts
;
1060 memcpy(&opts
, &rev
->diffopt
, sizeof(opts
));
1061 opts
.output_format
= DIFF_FORMAT_SUMMARY
| DIFF_FORMAT_DIFFSTAT
;
1062 diff_setup_done(&opts
);
1064 diff_tree_oid(get_commit_tree_oid(origin
),
1065 get_commit_tree_oid(head
),
1067 diffcore_std(&opts
);
1070 fprintf(rev
->diffopt
.file
, "\n");
1073 static void make_cover_letter(struct rev_info
*rev
, int use_stdout
,
1074 struct commit
*origin
,
1075 int nr
, struct commit
**list
,
1076 const char *branch_name
,
1079 const char *committer
;
1080 const char *body
= "*** SUBJECT HERE ***\n\n*** BLURB HERE ***\n";
1082 struct shortlog log
;
1083 struct strbuf sb
= STRBUF_INIT
;
1085 const char *encoding
= "UTF-8";
1086 int need_8bit_cte
= 0;
1087 struct pretty_print_context pp
= {0};
1088 struct commit
*head
= list
[0];
1090 if (!cmit_fmt_is_mail(rev
->commit_format
))
1091 die(_("cover letter needs email format"));
1093 committer
= git_committer_info(0);
1096 open_next_file(NULL
, rev
->numbered_files
? NULL
: "cover-letter", rev
, quiet
))
1097 die(_("failed to create cover-letter file"));
1099 log_write_email_headers(rev
, head
, &pp
.after_subject
, &need_8bit_cte
, 0);
1101 for (i
= 0; !need_8bit_cte
&& i
< nr
; i
++) {
1102 const char *buf
= get_commit_buffer(list
[i
], NULL
);
1103 if (has_non_ascii(buf
))
1105 unuse_commit_buffer(list
[i
], buf
);
1109 branch_name
= find_branch_name(rev
);
1112 pp
.fmt
= CMIT_FMT_EMAIL
;
1113 pp
.date_mode
.type
= DATE_RFC2822
;
1115 pp
.print_email_subject
= 1;
1116 pp_user_info(&pp
, NULL
, &sb
, committer
, encoding
);
1117 pp_title_line(&pp
, &msg
, &sb
, encoding
, need_8bit_cte
);
1118 pp_remainder(&pp
, &msg
, &sb
, 0);
1119 add_branch_description(&sb
, branch_name
);
1120 fprintf(rev
->diffopt
.file
, "%s\n", sb
.buf
);
1122 strbuf_release(&sb
);
1124 shortlog_init(&log
);
1126 log
.wrap
= MAIL_DEFAULT_WRAP
;
1129 log
.file
= rev
->diffopt
.file
;
1130 for (i
= 0; i
< nr
; i
++)
1131 shortlog_add_commit(&log
, list
[i
]);
1133 shortlog_output(&log
);
1135 /* We can only do diffstat with a unique reference point */
1137 show_diffstat(rev
, origin
, head
);
1139 if (rev
->idiff_oid1
) {
1140 fprintf_ln(rev
->diffopt
.file
, "%s", rev
->idiff_title
);
1141 show_interdiff(rev
, 0);
1146 * Pass minimum required diff-options to range-diff; others
1147 * can be added later if deemed desirable.
1149 struct diff_options opts
;
1151 opts
.file
= rev
->diffopt
.file
;
1152 opts
.use_color
= rev
->diffopt
.use_color
;
1153 diff_setup_done(&opts
);
1154 fprintf_ln(rev
->diffopt
.file
, "%s", rev
->rdiff_title
);
1155 show_range_diff(rev
->rdiff1
, rev
->rdiff2
,
1156 rev
->creation_factor
, 1, &opts
);
1160 static const char *clean_message_id(const char *msg_id
)
1163 const char *a
, *z
, *m
;
1166 while ((ch
= *m
) && (isspace(ch
) || (ch
== '<')))
1171 if (!isspace(ch
) && (ch
!= '>'))
1176 die(_("insane in-reply-to: %s"), msg_id
);
1179 return xmemdupz(a
, z
- a
);
1182 static const char *set_outdir(const char *prefix
, const char *output_directory
)
1184 if (output_directory
&& is_absolute_path(output_directory
))
1185 return output_directory
;
1187 if (!prefix
|| !*prefix
) {
1188 if (output_directory
)
1189 return output_directory
;
1190 /* The user did not explicitly ask for "./" */
1195 outdir_offset
= strlen(prefix
);
1196 if (!output_directory
)
1199 return prefix_filename(prefix
, output_directory
);
1202 static const char * const builtin_format_patch_usage
[] = {
1203 N_("git format-patch [<options>] [<since> | <revision-range>]"),
1207 static int keep_subject
= 0;
1209 static int keep_callback(const struct option
*opt
, const char *arg
, int unset
)
1211 BUG_ON_OPT_NEG(unset
);
1212 BUG_ON_OPT_ARG(arg
);
1213 ((struct rev_info
*)opt
->value
)->total
= -1;
1218 static int subject_prefix
= 0;
1220 static int subject_prefix_callback(const struct option
*opt
, const char *arg
,
1223 BUG_ON_OPT_NEG(unset
);
1225 ((struct rev_info
*)opt
->value
)->subject_prefix
= arg
;
1229 static int rfc_callback(const struct option
*opt
, const char *arg
, int unset
)
1231 BUG_ON_OPT_NEG(unset
);
1232 BUG_ON_OPT_ARG(arg
);
1233 return subject_prefix_callback(opt
, "RFC PATCH", unset
);
1236 static int numbered_cmdline_opt
= 0;
1238 static int numbered_callback(const struct option
*opt
, const char *arg
,
1241 BUG_ON_OPT_ARG(arg
);
1242 *(int *)opt
->value
= numbered_cmdline_opt
= unset
? 0 : 1;
1248 static int no_numbered_callback(const struct option
*opt
, const char *arg
,
1251 BUG_ON_OPT_NEG(unset
);
1252 return numbered_callback(opt
, arg
, 1);
1255 static int output_directory_callback(const struct option
*opt
, const char *arg
,
1258 const char **dir
= (const char **)opt
->value
;
1259 BUG_ON_OPT_NEG(unset
);
1261 die(_("two output directories?"));
1266 static int thread_callback(const struct option
*opt
, const char *arg
, int unset
)
1268 int *thread
= (int *)opt
->value
;
1271 else if (!arg
|| !strcmp(arg
, "shallow"))
1272 *thread
= THREAD_SHALLOW
;
1273 else if (!strcmp(arg
, "deep"))
1274 *thread
= THREAD_DEEP
;
1276 * Please update _git_formatpatch() in git-completion.bash
1277 * when you add new options.
1284 static int attach_callback(const struct option
*opt
, const char *arg
, int unset
)
1286 struct rev_info
*rev
= (struct rev_info
*)opt
->value
;
1288 rev
->mime_boundary
= NULL
;
1290 rev
->mime_boundary
= arg
;
1292 rev
->mime_boundary
= git_version_string
;
1293 rev
->no_inline
= unset
? 0 : 1;
1297 static int inline_callback(const struct option
*opt
, const char *arg
, int unset
)
1299 struct rev_info
*rev
= (struct rev_info
*)opt
->value
;
1301 rev
->mime_boundary
= NULL
;
1303 rev
->mime_boundary
= arg
;
1305 rev
->mime_boundary
= git_version_string
;
1310 static int header_callback(const struct option
*opt
, const char *arg
, int unset
)
1313 string_list_clear(&extra_hdr
, 0);
1314 string_list_clear(&extra_to
, 0);
1315 string_list_clear(&extra_cc
, 0);
1322 static int to_callback(const struct option
*opt
, const char *arg
, int unset
)
1325 string_list_clear(&extra_to
, 0);
1327 string_list_append(&extra_to
, arg
);
1331 static int cc_callback(const struct option
*opt
, const char *arg
, int unset
)
1334 string_list_clear(&extra_cc
, 0);
1336 string_list_append(&extra_cc
, arg
);
1340 static int from_callback(const struct option
*opt
, const char *arg
, int unset
)
1342 char **from
= opt
->value
;
1349 *from
= xstrdup(arg
);
1351 *from
= xstrdup(git_committer_info(IDENT_NO_DATE
));
1355 struct base_tree_info
{
1356 struct object_id base_commit
;
1357 int nr_patch_id
, alloc_patch_id
;
1358 struct object_id
*patch_id
;
1361 static struct commit
*get_base_commit(const char *base_commit
,
1362 struct commit
**list
,
1365 struct commit
*base
= NULL
;
1366 struct commit
**rev
;
1367 int i
= 0, rev_nr
= 0;
1369 if (base_commit
&& strcmp(base_commit
, "auto")) {
1370 base
= lookup_commit_reference_by_name(base_commit
);
1372 die(_("unknown commit %s"), base_commit
);
1373 } else if ((base_commit
&& !strcmp(base_commit
, "auto")) || base_auto
) {
1374 struct branch
*curr_branch
= branch_get(NULL
);
1375 const char *upstream
= branch_get_upstream(curr_branch
, NULL
);
1377 struct commit_list
*base_list
;
1378 struct commit
*commit
;
1379 struct object_id oid
;
1381 if (get_oid(upstream
, &oid
))
1382 die(_("failed to resolve '%s' as a valid ref"), upstream
);
1383 commit
= lookup_commit_or_die(&oid
, "upstream base");
1384 base_list
= get_merge_bases_many(commit
, total
, list
);
1385 /* There should be one and only one merge base. */
1386 if (!base_list
|| base_list
->next
)
1387 die(_("could not find exact merge base"));
1388 base
= base_list
->item
;
1389 free_commit_list(base_list
);
1391 die(_("failed to get upstream, if you want to record base commit automatically,\n"
1392 "please use git branch --set-upstream-to to track a remote branch.\n"
1393 "Or you could specify base commit by --base=<base-commit-id> manually"));
1397 ALLOC_ARRAY(rev
, total
);
1398 for (i
= 0; i
< total
; i
++)
1403 * Get merge base through pair-wise computations
1404 * and store it in rev[0].
1406 while (rev_nr
> 1) {
1407 for (i
= 0; i
< rev_nr
/ 2; i
++) {
1408 struct commit_list
*merge_base
;
1409 merge_base
= get_merge_bases(rev
[2 * i
], rev
[2 * i
+ 1]);
1410 if (!merge_base
|| merge_base
->next
)
1411 die(_("failed to find exact merge base"));
1413 rev
[i
] = merge_base
->item
;
1417 rev
[i
] = rev
[2 * i
];
1418 rev_nr
= DIV_ROUND_UP(rev_nr
, 2);
1421 if (!in_merge_bases(base
, rev
[0]))
1422 die(_("base commit should be the ancestor of revision list"));
1424 for (i
= 0; i
< total
; i
++) {
1425 if (base
== list
[i
])
1426 die(_("base commit shouldn't be in revision list"));
1433 define_commit_slab(commit_base
, int);
1435 static void prepare_bases(struct base_tree_info
*bases
,
1436 struct commit
*base
,
1437 struct commit
**list
,
1440 struct commit
*commit
;
1441 struct rev_info revs
;
1442 struct diff_options diffopt
;
1443 struct commit_base commit_base
;
1449 init_commit_base(&commit_base
);
1450 repo_diff_setup(the_repository
, &diffopt
);
1451 diffopt
.flags
.recursive
= 1;
1452 diff_setup_done(&diffopt
);
1454 oidcpy(&bases
->base_commit
, &base
->object
.oid
);
1456 repo_init_revisions(the_repository
, &revs
, NULL
);
1457 revs
.max_parents
= 1;
1458 revs
.topo_order
= 1;
1459 for (i
= 0; i
< total
; i
++) {
1460 list
[i
]->object
.flags
&= ~UNINTERESTING
;
1461 add_pending_object(&revs
, &list
[i
]->object
, "rev_list");
1462 *commit_base_at(&commit_base
, list
[i
]) = 1;
1464 base
->object
.flags
|= UNINTERESTING
;
1465 add_pending_object(&revs
, &base
->object
, "base");
1467 if (prepare_revision_walk(&revs
))
1468 die(_("revision walk setup failed"));
1470 * Traverse the commits list, get prerequisite patch ids
1471 * and stuff them in bases structure.
1473 while ((commit
= get_revision(&revs
)) != NULL
) {
1474 struct object_id oid
;
1475 struct object_id
*patch_id
;
1476 if (*commit_base_at(&commit_base
, commit
))
1478 if (commit_patch_id(commit
, &diffopt
, &oid
, 0, 1))
1479 die(_("cannot get patch id"));
1480 ALLOC_GROW(bases
->patch_id
, bases
->nr_patch_id
+ 1, bases
->alloc_patch_id
);
1481 patch_id
= bases
->patch_id
+ bases
->nr_patch_id
;
1482 oidcpy(patch_id
, &oid
);
1483 bases
->nr_patch_id
++;
1485 clear_commit_base(&commit_base
);
1488 static void print_bases(struct base_tree_info
*bases
, FILE *file
)
1492 /* Only do this once, either for the cover or for the first one */
1493 if (is_null_oid(&bases
->base_commit
))
1496 /* Show the base commit */
1497 fprintf(file
, "\nbase-commit: %s\n", oid_to_hex(&bases
->base_commit
));
1499 /* Show the prerequisite patches */
1500 for (i
= bases
->nr_patch_id
- 1; i
>= 0; i
--)
1501 fprintf(file
, "prerequisite-patch-id: %s\n", oid_to_hex(&bases
->patch_id
[i
]));
1503 free(bases
->patch_id
);
1504 bases
->nr_patch_id
= 0;
1505 bases
->alloc_patch_id
= 0;
1506 oidclr(&bases
->base_commit
);
1509 static const char *diff_title(struct strbuf
*sb
, int reroll_count
,
1510 const char *generic
, const char *rerolled
)
1512 if (reroll_count
<= 0)
1513 strbuf_addstr(sb
, generic
);
1514 else /* RFC may be v0, so allow -v1 to diff against v0 */
1515 strbuf_addf(sb
, rerolled
, reroll_count
- 1);
1519 static void infer_range_diff_ranges(struct strbuf
*r1
,
1522 struct commit
*origin
,
1523 struct commit
*head
)
1525 const char *head_oid
= oid_to_hex(&head
->object
.oid
);
1527 if (!strstr(prev
, "..")) {
1528 strbuf_addf(r1
, "%s..%s", head_oid
, prev
);
1529 strbuf_addf(r2
, "%s..%s", prev
, head_oid
);
1530 } else if (!origin
) {
1531 die(_("failed to infer range-diff ranges"));
1533 strbuf_addstr(r1
, prev
);
1534 strbuf_addf(r2
, "%s..%s",
1535 oid_to_hex(&origin
->object
.oid
), head_oid
);
1539 int cmd_format_patch(int argc
, const char **argv
, const char *prefix
)
1541 struct commit
*commit
;
1542 struct commit
**list
= NULL
;
1543 struct rev_info rev
;
1544 struct setup_revision_opt s_r_opt
;
1545 int nr
= 0, total
, i
;
1547 int start_number
= -1;
1548 int just_numbers
= 0;
1549 int ignore_if_in_upstream
= 0;
1550 int cover_letter
= -1;
1551 int boundary_count
= 0;
1552 int no_binary_diff
= 0;
1553 int zero_commit
= 0;
1554 struct commit
*origin
= NULL
;
1555 const char *in_reply_to
= NULL
;
1556 struct patch_ids ids
;
1557 struct strbuf buf
= STRBUF_INIT
;
1558 int use_patch_format
= 0;
1560 int reroll_count
= -1;
1561 char *branch_name
= NULL
;
1562 char *base_commit
= NULL
;
1563 struct base_tree_info bases
;
1564 int show_progress
= 0;
1565 struct progress
*progress
= NULL
;
1566 struct oid_array idiff_prev
= OID_ARRAY_INIT
;
1567 struct strbuf idiff_title
= STRBUF_INIT
;
1568 const char *rdiff_prev
= NULL
;
1569 struct strbuf rdiff1
= STRBUF_INIT
;
1570 struct strbuf rdiff2
= STRBUF_INIT
;
1571 struct strbuf rdiff_title
= STRBUF_INIT
;
1572 int creation_factor
= -1;
1574 const struct option builtin_format_patch_options
[] = {
1575 { OPTION_CALLBACK
, 'n', "numbered", &numbered
, NULL
,
1576 N_("use [PATCH n/m] even with a single patch"),
1577 PARSE_OPT_NOARG
, numbered_callback
},
1578 { OPTION_CALLBACK
, 'N', "no-numbered", &numbered
, NULL
,
1579 N_("use [PATCH] even with multiple patches"),
1580 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
, no_numbered_callback
},
1581 OPT_BOOL('s', "signoff", &do_signoff
, N_("add Signed-off-by:")),
1582 OPT_BOOL(0, "stdout", &use_stdout
,
1583 N_("print patches to standard out")),
1584 OPT_BOOL(0, "cover-letter", &cover_letter
,
1585 N_("generate a cover letter")),
1586 OPT_BOOL(0, "numbered-files", &just_numbers
,
1587 N_("use simple number sequence for output file names")),
1588 OPT_STRING(0, "suffix", &fmt_patch_suffix
, N_("sfx"),
1589 N_("use <sfx> instead of '.patch'")),
1590 OPT_INTEGER(0, "start-number", &start_number
,
1591 N_("start numbering patches at <n> instead of 1")),
1592 OPT_INTEGER('v', "reroll-count", &reroll_count
,
1593 N_("mark the series as Nth re-roll")),
1594 { OPTION_CALLBACK
, 0, "rfc", &rev
, NULL
,
1595 N_("Use [RFC PATCH] instead of [PATCH]"),
1596 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
, rfc_callback
},
1597 { OPTION_CALLBACK
, 0, "subject-prefix", &rev
, N_("prefix"),
1598 N_("Use [<prefix>] instead of [PATCH]"),
1599 PARSE_OPT_NONEG
, subject_prefix_callback
},
1600 { OPTION_CALLBACK
, 'o', "output-directory", &output_directory
,
1601 N_("dir"), N_("store resulting files in <dir>"),
1602 PARSE_OPT_NONEG
, output_directory_callback
},
1603 { OPTION_CALLBACK
, 'k', "keep-subject", &rev
, NULL
,
1604 N_("don't strip/add [PATCH]"),
1605 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
, keep_callback
},
1606 OPT_BOOL(0, "no-binary", &no_binary_diff
,
1607 N_("don't output binary diffs")),
1608 OPT_BOOL(0, "zero-commit", &zero_commit
,
1609 N_("output all-zero hash in From header")),
1610 OPT_BOOL(0, "ignore-if-in-upstream", &ignore_if_in_upstream
,
1611 N_("don't include a patch matching a commit upstream")),
1612 OPT_SET_INT_F('p', "no-stat", &use_patch_format
,
1613 N_("show patch format instead of default (patch + stat)"),
1614 1, PARSE_OPT_NONEG
),
1615 OPT_GROUP(N_("Messaging")),
1616 { OPTION_CALLBACK
, 0, "add-header", NULL
, N_("header"),
1617 N_("add email header"), 0, header_callback
},
1618 { OPTION_CALLBACK
, 0, "to", NULL
, N_("email"), N_("add To: header"),
1620 { OPTION_CALLBACK
, 0, "cc", NULL
, N_("email"), N_("add Cc: header"),
1622 { OPTION_CALLBACK
, 0, "from", &from
, N_("ident"),
1623 N_("set From address to <ident> (or committer ident if absent)"),
1624 PARSE_OPT_OPTARG
, from_callback
},
1625 OPT_STRING(0, "in-reply-to", &in_reply_to
, N_("message-id"),
1626 N_("make first mail a reply to <message-id>")),
1627 { OPTION_CALLBACK
, 0, "attach", &rev
, N_("boundary"),
1628 N_("attach the patch"), PARSE_OPT_OPTARG
,
1630 { OPTION_CALLBACK
, 0, "inline", &rev
, N_("boundary"),
1631 N_("inline the patch"),
1632 PARSE_OPT_OPTARG
| PARSE_OPT_NONEG
,
1634 { OPTION_CALLBACK
, 0, "thread", &thread
, N_("style"),
1635 N_("enable message threading, styles: shallow, deep"),
1636 PARSE_OPT_OPTARG
, thread_callback
},
1637 OPT_STRING(0, "signature", &signature
, N_("signature"),
1638 N_("add a signature")),
1639 OPT_STRING(0, "base", &base_commit
, N_("base-commit"),
1640 N_("add prerequisite tree info to the patch series")),
1641 OPT_FILENAME(0, "signature-file", &signature_file
,
1642 N_("add a signature from a file")),
1643 OPT__QUIET(&quiet
, N_("don't print the patch filenames")),
1644 OPT_BOOL(0, "progress", &show_progress
,
1645 N_("show progress while generating patches")),
1646 OPT_CALLBACK(0, "interdiff", &idiff_prev
, N_("rev"),
1647 N_("show changes against <rev> in cover letter or single patch"),
1648 parse_opt_object_name
),
1649 OPT_STRING(0, "range-diff", &rdiff_prev
, N_("refspec"),
1650 N_("show changes against <refspec> in cover letter or single patch")),
1651 OPT_INTEGER(0, "creation-factor", &creation_factor
,
1652 N_("percentage by which creation is weighted")),
1656 extra_hdr
.strdup_strings
= 1;
1657 extra_to
.strdup_strings
= 1;
1658 extra_cc
.strdup_strings
= 1;
1659 init_log_defaults();
1660 repo_init_revisions(the_repository
, &rev
, prefix
);
1661 git_config(git_format_config
, &rev
);
1662 rev
.commit_format
= CMIT_FMT_EMAIL
;
1663 rev
.expand_tabs_in_log_default
= 0;
1664 rev
.verbose_header
= 1;
1666 rev
.max_parents
= 1;
1667 rev
.diffopt
.flags
.recursive
= 1;
1668 rev
.subject_prefix
= fmt_patch_subject_prefix
;
1669 memset(&s_r_opt
, 0, sizeof(s_r_opt
));
1670 s_r_opt
.def
= "HEAD";
1671 s_r_opt
.revarg_opt
= REVARG_COMMITTISH
;
1673 if (default_attach
) {
1674 rev
.mime_boundary
= default_attach
;
1679 * Parse the arguments before setup_revisions(), or something
1680 * like "git format-patch -o a123 HEAD^.." may fail; a123 is
1681 * possibly a valid SHA1.
1683 argc
= parse_options(argc
, argv
, prefix
, builtin_format_patch_options
,
1684 builtin_format_patch_usage
,
1685 PARSE_OPT_KEEP_ARGV0
| PARSE_OPT_KEEP_UNKNOWN
|
1686 PARSE_OPT_KEEP_DASHDASH
);
1688 if (0 < reroll_count
) {
1689 struct strbuf sprefix
= STRBUF_INIT
;
1690 strbuf_addf(&sprefix
, "%s v%d",
1691 rev
.subject_prefix
, reroll_count
);
1692 rev
.reroll_count
= reroll_count
;
1693 rev
.subject_prefix
= strbuf_detach(&sprefix
, NULL
);
1696 for (i
= 0; i
< extra_hdr
.nr
; i
++) {
1697 strbuf_addstr(&buf
, extra_hdr
.items
[i
].string
);
1698 strbuf_addch(&buf
, '\n');
1702 strbuf_addstr(&buf
, "To: ");
1703 for (i
= 0; i
< extra_to
.nr
; i
++) {
1705 strbuf_addstr(&buf
, " ");
1706 strbuf_addstr(&buf
, extra_to
.items
[i
].string
);
1707 if (i
+ 1 < extra_to
.nr
)
1708 strbuf_addch(&buf
, ',');
1709 strbuf_addch(&buf
, '\n');
1713 strbuf_addstr(&buf
, "Cc: ");
1714 for (i
= 0; i
< extra_cc
.nr
; i
++) {
1716 strbuf_addstr(&buf
, " ");
1717 strbuf_addstr(&buf
, extra_cc
.items
[i
].string
);
1718 if (i
+ 1 < extra_cc
.nr
)
1719 strbuf_addch(&buf
, ',');
1720 strbuf_addch(&buf
, '\n');
1723 rev
.extra_headers
= strbuf_detach(&buf
, NULL
);
1726 if (split_ident_line(&rev
.from_ident
, from
, strlen(from
)))
1727 die(_("invalid ident line: %s"), from
);
1730 if (start_number
< 0)
1734 * If numbered is set solely due to format.numbered in config,
1735 * and it would conflict with --keep-subject (-k) from the
1736 * command line, reset "numbered".
1738 if (numbered
&& keep_subject
&& !numbered_cmdline_opt
)
1741 if (numbered
&& keep_subject
)
1742 die(_("-n and -k are mutually exclusive"));
1743 if (keep_subject
&& subject_prefix
)
1744 die(_("--subject-prefix/--rfc and -k are mutually exclusive"));
1745 rev
.preserve_subject
= keep_subject
;
1747 argc
= setup_revisions(argc
, argv
, &rev
, &s_r_opt
);
1749 die(_("unrecognized argument: %s"), argv
[1]);
1751 if (rev
.diffopt
.output_format
& DIFF_FORMAT_NAME
)
1752 die(_("--name-only does not make sense"));
1753 if (rev
.diffopt
.output_format
& DIFF_FORMAT_NAME_STATUS
)
1754 die(_("--name-status does not make sense"));
1755 if (rev
.diffopt
.output_format
& DIFF_FORMAT_CHECKDIFF
)
1756 die(_("--check does not make sense"));
1758 if (!use_patch_format
&&
1759 (!rev
.diffopt
.output_format
||
1760 rev
.diffopt
.output_format
== DIFF_FORMAT_PATCH
))
1761 rev
.diffopt
.output_format
= DIFF_FORMAT_DIFFSTAT
| DIFF_FORMAT_SUMMARY
;
1762 if (!rev
.diffopt
.stat_width
)
1763 rev
.diffopt
.stat_width
= MAIL_DEFAULT_WRAP
;
1765 /* Always generate a patch */
1766 rev
.diffopt
.output_format
|= DIFF_FORMAT_PATCH
;
1768 rev
.zero_commit
= zero_commit
;
1770 if (!rev
.diffopt
.flags
.text
&& !no_binary_diff
)
1771 rev
.diffopt
.flags
.binary
= 1;
1774 init_display_notes(&rev
.notes_opt
);
1776 if (!output_directory
&& !use_stdout
)
1777 output_directory
= config_output_directory
;
1780 output_directory
= set_outdir(prefix
, output_directory
);
1784 if (output_directory
) {
1785 if (rev
.diffopt
.use_color
!= GIT_COLOR_ALWAYS
)
1786 rev
.diffopt
.use_color
= GIT_COLOR_NEVER
;
1788 die(_("standard output, or directory, which one?"));
1789 if (mkdir(output_directory
, 0777) < 0 && errno
!= EEXIST
)
1790 die_errno(_("could not create directory '%s'"),
1794 if (rev
.pending
.nr
== 1) {
1797 if (rev
.max_count
< 0 && !rev
.show_root_diff
) {
1799 * This is traditional behaviour of "git format-patch
1800 * origin" that prepares what the origin side still
1803 rev
.pending
.objects
[0].item
->flags
|= UNINTERESTING
;
1804 add_head_to_pending(&rev
);
1808 * Otherwise, it is "format-patch -22 HEAD", and/or
1809 * "format-patch --root HEAD". The user wants
1810 * get_revision() to do the usual traversal.
1813 if (!strcmp(rev
.pending
.objects
[0].name
, "HEAD"))
1817 const char *ref
, *v
;
1818 ref
= resolve_ref_unsafe("HEAD", RESOLVE_REF_READING
,
1820 if (ref
&& skip_prefix(ref
, "refs/heads/", &v
))
1821 branch_name
= xstrdup(v
);
1823 branch_name
= xstrdup(""); /* no branch */
1828 * We cannot move this anywhere earlier because we do want to
1829 * know if --root was given explicitly from the command line.
1831 rev
.show_root_diff
= 1;
1833 if (ignore_if_in_upstream
) {
1834 /* Don't say anything if head and upstream are the same. */
1835 if (rev
.pending
.nr
== 2) {
1836 struct object_array_entry
*o
= rev
.pending
.objects
;
1837 if (oideq(&o
[0].item
->oid
, &o
[1].item
->oid
))
1840 get_patch_ids(&rev
, &ids
);
1843 if (prepare_revision_walk(&rev
))
1844 die(_("revision walk setup failed"));
1846 while ((commit
= get_revision(&rev
)) != NULL
) {
1847 if (commit
->object
.flags
& BOUNDARY
) {
1849 origin
= (boundary_count
== 1) ? commit
: NULL
;
1853 if (ignore_if_in_upstream
&& has_commit_patch_id(commit
, &ids
))
1857 REALLOC_ARRAY(list
, nr
);
1858 list
[nr
- 1] = commit
;
1864 if (cover_letter
== -1) {
1865 if (config_cover_letter
== COVER_AUTO
)
1866 cover_letter
= (total
> 1);
1868 cover_letter
= (config_cover_letter
== COVER_ON
);
1870 if (!keep_subject
&& auto_number
&& (total
> 1 || cover_letter
))
1873 rev
.total
= total
+ start_number
- 1;
1875 if (idiff_prev
.nr
) {
1876 if (!cover_letter
&& total
!= 1)
1877 die(_("--interdiff requires --cover-letter or single patch"));
1878 rev
.idiff_oid1
= &idiff_prev
.oid
[idiff_prev
.nr
- 1];
1879 rev
.idiff_oid2
= get_commit_tree_oid(list
[0]);
1880 rev
.idiff_title
= diff_title(&idiff_title
, reroll_count
,
1882 _("Interdiff against v%d:"));
1885 if (creation_factor
< 0)
1886 creation_factor
= RANGE_DIFF_CREATION_FACTOR_DEFAULT
;
1887 else if (!rdiff_prev
)
1888 die(_("--creation-factor requires --range-diff"));
1891 if (!cover_letter
&& total
!= 1)
1892 die(_("--range-diff requires --cover-letter or single patch"));
1894 infer_range_diff_ranges(&rdiff1
, &rdiff2
, rdiff_prev
,
1896 rev
.rdiff1
= rdiff1
.buf
;
1897 rev
.rdiff2
= rdiff2
.buf
;
1898 rev
.creation_factor
= creation_factor
;
1899 rev
.rdiff_title
= diff_title(&rdiff_title
, reroll_count
,
1901 _("Range-diff against v%d:"));
1905 ; /* --no-signature inhibits all signatures */
1906 } else if (signature
&& signature
!= git_version_string
) {
1907 ; /* non-default signature already set */
1908 } else if (signature_file
) {
1909 struct strbuf buf
= STRBUF_INIT
;
1911 if (strbuf_read_file(&buf
, signature_file
, 128) < 0)
1912 die_errno(_("unable to read signature file '%s'"), signature_file
);
1913 signature
= strbuf_detach(&buf
, NULL
);
1916 memset(&bases
, 0, sizeof(bases
));
1917 if (base_commit
|| base_auto
) {
1918 struct commit
*base
= get_base_commit(base_commit
, list
, nr
);
1919 reset_revision_walk();
1920 clear_object_flags(UNINTERESTING
);
1921 prepare_bases(&bases
, base
, list
, nr
);
1924 if (in_reply_to
|| thread
|| cover_letter
)
1925 rev
.ref_message_ids
= xcalloc(1, sizeof(struct string_list
));
1927 const char *msgid
= clean_message_id(in_reply_to
);
1928 string_list_append(rev
.ref_message_ids
, msgid
);
1930 rev
.numbered_files
= just_numbers
;
1931 rev
.patch_suffix
= fmt_patch_suffix
;
1934 gen_message_id(&rev
, "cover");
1935 make_cover_letter(&rev
, use_stdout
,
1936 origin
, nr
, list
, branch_name
, quiet
);
1937 print_bases(&bases
, rev
.diffopt
.file
);
1938 print_signature(rev
.diffopt
.file
);
1941 /* interdiff/range-diff in cover-letter; omit from patches */
1942 rev
.idiff_oid1
= NULL
;
1945 rev
.add_signoff
= do_signoff
;
1948 progress
= start_delayed_progress(_("Generating patches"), total
);
1951 display_progress(progress
, total
- nr
);
1953 rev
.nr
= total
- nr
+ (start_number
- 1);
1954 /* Make the second and subsequent mails replies to the first */
1956 /* Have we already had a message ID? */
1957 if (rev
.message_id
) {
1959 * For deep threading: make every mail
1960 * a reply to the previous one, no
1961 * matter what other options are set.
1963 * For shallow threading:
1965 * Without --cover-letter and
1966 * --in-reply-to, make every mail a
1967 * reply to the one before.
1969 * With --in-reply-to but no
1970 * --cover-letter, make every mail a
1971 * reply to the <reply-to>.
1973 * With --cover-letter, make every
1974 * mail but the cover letter a reply
1975 * to the cover letter. The cover
1976 * letter is a reply to the
1977 * --in-reply-to, if specified.
1979 if (thread
== THREAD_SHALLOW
1980 && rev
.ref_message_ids
->nr
> 0
1981 && (!cover_letter
|| rev
.nr
> 1))
1982 free(rev
.message_id
);
1984 string_list_append(rev
.ref_message_ids
,
1987 gen_message_id(&rev
, oid_to_hex(&commit
->object
.oid
));
1991 open_next_file(rev
.numbered_files
? NULL
: commit
, NULL
, &rev
, quiet
))
1992 die(_("failed to create output files"));
1993 shown
= log_tree_commit(&rev
, commit
);
1994 free_commit_buffer(the_repository
->parsed_objects
,
1997 /* We put one extra blank line between formatted
1998 * patches and this flag is used by log-tree code
1999 * to see if it needs to emit a LF before showing
2000 * the log; when using one file per patch, we do
2001 * not want the extra blank line.
2006 print_bases(&bases
, rev
.diffopt
.file
);
2007 if (rev
.mime_boundary
)
2008 fprintf(rev
.diffopt
.file
, "\n--%s%s--\n\n\n",
2009 mime_boundary_leader
,
2012 print_signature(rev
.diffopt
.file
);
2015 fclose(rev
.diffopt
.file
);
2017 stop_progress(&progress
);
2020 string_list_clear(&extra_to
, 0);
2021 string_list_clear(&extra_cc
, 0);
2022 string_list_clear(&extra_hdr
, 0);
2023 if (ignore_if_in_upstream
)
2024 free_patch_ids(&ids
);
2027 oid_array_clear(&idiff_prev
);
2028 strbuf_release(&idiff_title
);
2029 strbuf_release(&rdiff1
);
2030 strbuf_release(&rdiff2
);
2031 strbuf_release(&rdiff_title
);
2035 static int add_pending_commit(const char *arg
, struct rev_info
*revs
, int flags
)
2037 struct object_id oid
;
2038 if (get_oid(arg
, &oid
) == 0) {
2039 struct commit
*commit
= lookup_commit_reference(the_repository
,
2042 commit
->object
.flags
|= flags
;
2043 add_pending_object(revs
, &commit
->object
, arg
);
2050 static const char * const cherry_usage
[] = {
2051 N_("git cherry [-v] [<upstream> [<head> [<limit>]]]"),
2055 static void print_commit(char sign
, struct commit
*commit
, int verbose
,
2056 int abbrev
, FILE *file
)
2059 fprintf(file
, "%c %s\n", sign
,
2060 find_unique_abbrev(&commit
->object
.oid
, abbrev
));
2062 struct strbuf buf
= STRBUF_INIT
;
2063 pp_commit_easy(CMIT_FMT_ONELINE
, commit
, &buf
);
2064 fprintf(file
, "%c %s %s\n", sign
,
2065 find_unique_abbrev(&commit
->object
.oid
, abbrev
),
2067 strbuf_release(&buf
);
2071 int cmd_cherry(int argc
, const char **argv
, const char *prefix
)
2073 struct rev_info revs
;
2074 struct patch_ids ids
;
2075 struct commit
*commit
;
2076 struct commit_list
*list
= NULL
;
2077 struct branch
*current_branch
;
2078 const char *upstream
;
2079 const char *head
= "HEAD";
2080 const char *limit
= NULL
;
2081 int verbose
= 0, abbrev
= 0;
2083 struct option options
[] = {
2084 OPT__ABBREV(&abbrev
),
2085 OPT__VERBOSE(&verbose
, N_("be verbose")),
2089 argc
= parse_options(argc
, argv
, prefix
, options
, cherry_usage
, 0);
2102 current_branch
= branch_get(NULL
);
2103 upstream
= branch_get_upstream(current_branch
, NULL
);
2105 fprintf(stderr
, _("Could not find a tracked"
2106 " remote branch, please"
2107 " specify <upstream> manually.\n"));
2108 usage_with_options(cherry_usage
, options
);
2112 repo_init_revisions(the_repository
, &revs
, prefix
);
2113 revs
.max_parents
= 1;
2115 if (add_pending_commit(head
, &revs
, 0))
2116 die(_("unknown commit %s"), head
);
2117 if (add_pending_commit(upstream
, &revs
, UNINTERESTING
))
2118 die(_("unknown commit %s"), upstream
);
2120 /* Don't say anything if head and upstream are the same. */
2121 if (revs
.pending
.nr
== 2) {
2122 struct object_array_entry
*o
= revs
.pending
.objects
;
2123 if (oideq(&o
[0].item
->oid
, &o
[1].item
->oid
))
2127 get_patch_ids(&revs
, &ids
);
2129 if (limit
&& add_pending_commit(limit
, &revs
, UNINTERESTING
))
2130 die(_("unknown commit %s"), limit
);
2132 /* reverse the list of commits */
2133 if (prepare_revision_walk(&revs
))
2134 die(_("revision walk setup failed"));
2135 while ((commit
= get_revision(&revs
)) != NULL
) {
2136 commit_list_insert(commit
, &list
);
2142 commit
= list
->item
;
2143 if (has_commit_patch_id(commit
, &ids
))
2145 print_commit(sign
, commit
, verbose
, abbrev
, revs
.diffopt
.file
);
2149 free_patch_ids(&ids
);