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 "range-diff.h"
38 #define MAIL_DEFAULT_WRAP 72
39 #define COVER_FROM_AUTO_MAX_SUBJECT_LEN 100
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 default_encode_email_headers
= 1;
49 static int decoration_style
;
50 static int decoration_given
;
51 static int use_mailmap_config
= 1;
52 static const char *fmt_patch_subject_prefix
= "PATCH";
53 static const char *fmt_pretty
;
55 static const char * const builtin_log_usage
[] = {
56 N_("git log [<options>] [<revision-range>] [[--] <path>...]"),
57 N_("git show [<options>] <object>..."),
61 struct line_opt_callback_data
{
64 struct string_list args
;
67 static int session_is_interactive(void)
69 return isatty(1) || pager_in_use();
72 static int auto_decoration_style(void)
74 return session_is_interactive() ? DECORATE_SHORT_REFS
: 0;
77 static int parse_decoration_style(const char *value
)
79 switch (git_parse_maybe_bool(value
)) {
81 return DECORATE_SHORT_REFS
;
87 if (!strcmp(value
, "full"))
88 return DECORATE_FULL_REFS
;
89 else if (!strcmp(value
, "short"))
90 return DECORATE_SHORT_REFS
;
91 else if (!strcmp(value
, "auto"))
92 return auto_decoration_style();
94 * Please update _git_log() in git-completion.bash when you
95 * add new decoration styles.
100 static int decorate_callback(const struct option
*opt
, const char *arg
, int unset
)
103 decoration_style
= 0;
105 decoration_style
= parse_decoration_style(arg
);
107 decoration_style
= DECORATE_SHORT_REFS
;
109 if (decoration_style
< 0)
110 die(_("invalid --decorate option: %s"), arg
);
112 decoration_given
= 1;
117 static int log_line_range_callback(const struct option
*option
, const char *arg
, int unset
)
119 struct line_opt_callback_data
*data
= option
->value
;
121 BUG_ON_OPT_NEG(unset
);
126 data
->rev
->line_level_traverse
= 1;
127 string_list_append(&data
->args
, arg
);
132 static void init_log_defaults(void)
134 init_grep_defaults(the_repository
);
135 init_diff_ui_defaults();
137 decoration_style
= auto_decoration_style();
140 static void cmd_log_init_defaults(struct rev_info
*rev
)
143 get_commit_format(fmt_pretty
, rev
);
145 rev
->diffopt
.flags
.default_follow_renames
= 1;
146 rev
->verbose_header
= 1;
147 rev
->diffopt
.flags
.recursive
= 1;
148 rev
->diffopt
.stat_width
= -1; /* use full terminal width */
149 rev
->diffopt
.stat_graph_width
= -1; /* respect statGraphWidth config */
150 rev
->abbrev_commit
= default_abbrev_commit
;
151 rev
->show_root_diff
= default_show_root
;
152 rev
->subject_prefix
= fmt_patch_subject_prefix
;
153 rev
->show_signature
= default_show_signature
;
154 rev
->encode_email_headers
= default_encode_email_headers
;
155 rev
->diffopt
.flags
.allow_textconv
= 1;
157 if (default_date_mode
)
158 parse_date_format(default_date_mode
, &rev
->date_mode
);
161 static void cmd_log_init_finish(int argc
, const char **argv
, const char *prefix
,
162 struct rev_info
*rev
, struct setup_revision_opt
*opt
)
164 struct userformat_want w
;
165 int quiet
= 0, source
= 0, mailmap
;
166 static struct line_opt_callback_data line_cb
= {NULL
, NULL
, STRING_LIST_INIT_DUP
};
167 static struct string_list decorate_refs_exclude
= STRING_LIST_INIT_NODUP
;
168 static struct string_list decorate_refs_exclude_config
= STRING_LIST_INIT_NODUP
;
169 static struct string_list decorate_refs_include
= STRING_LIST_INIT_NODUP
;
170 struct decoration_filter decoration_filter
= {&decorate_refs_include
,
171 &decorate_refs_exclude
,
172 &decorate_refs_exclude_config
};
173 static struct revision_sources revision_sources
;
175 const struct option builtin_log_options
[] = {
176 OPT__QUIET(&quiet
, N_("suppress diff output")),
177 OPT_BOOL(0, "source", &source
, N_("show source")),
178 OPT_BOOL(0, "use-mailmap", &mailmap
, N_("Use mail map file")),
179 OPT_ALIAS(0, "mailmap", "use-mailmap"),
180 OPT_STRING_LIST(0, "decorate-refs", &decorate_refs_include
,
181 N_("pattern"), N_("only decorate refs that match <pattern>")),
182 OPT_STRING_LIST(0, "decorate-refs-exclude", &decorate_refs_exclude
,
183 N_("pattern"), N_("do not decorate refs that match <pattern>")),
184 OPT_CALLBACK_F(0, "decorate", NULL
, NULL
, N_("decorate options"),
185 PARSE_OPT_OPTARG
, decorate_callback
),
186 OPT_CALLBACK('L', NULL
, &line_cb
, "n,m:file",
187 N_("Process line range n,m in file, counting from 1"),
188 log_line_range_callback
),
193 line_cb
.prefix
= prefix
;
195 mailmap
= use_mailmap_config
;
196 argc
= parse_options(argc
, argv
, prefix
,
197 builtin_log_options
, builtin_log_usage
,
198 PARSE_OPT_KEEP_ARGV0
| PARSE_OPT_KEEP_UNKNOWN
|
199 PARSE_OPT_KEEP_DASHDASH
);
202 rev
->diffopt
.output_format
|= DIFF_FORMAT_NO_OUTPUT
;
203 argc
= setup_revisions(argc
, argv
, rev
, opt
);
205 /* Any arguments at this point are not recognized */
207 die(_("unrecognized argument: %s"), argv
[1]);
209 memset(&w
, 0, sizeof(w
));
210 userformat_find_requirements(NULL
, &w
);
212 if (!rev
->show_notes_given
&& (!rev
->pretty_given
|| w
.notes
))
215 load_display_notes(&rev
->notes_opt
);
217 if ((rev
->diffopt
.pickaxe_opts
& DIFF_PICKAXE_KINDS_MASK
) ||
218 rev
->diffopt
.filter
|| rev
->diffopt
.flags
.follow_renames
)
219 rev
->always_show_header
= 0;
221 if (source
|| w
.source
) {
222 init_revision_sources(&revision_sources
);
223 rev
->sources
= &revision_sources
;
227 rev
->mailmap
= xcalloc(1, sizeof(struct string_list
));
228 read_mailmap(rev
->mailmap
, NULL
);
231 if (rev
->pretty_given
&& rev
->commit_format
== CMIT_FMT_RAW
) {
233 * "log --pretty=raw" is special; ignore UI oriented
234 * configuration variables such as decoration.
236 if (!decoration_given
)
237 decoration_style
= 0;
238 if (!rev
->abbrev_commit_given
)
239 rev
->abbrev_commit
= 0;
242 if (decoration_style
) {
243 const struct string_list
*config_exclude
=
244 repo_config_get_value_multi(the_repository
,
245 "log.excludeDecoration");
247 if (config_exclude
) {
248 struct string_list_item
*item
;
249 for_each_string_list_item(item
, config_exclude
)
250 string_list_append(&decorate_refs_exclude_config
,
254 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
, "format.encodeemailheaders")) {
458 default_encode_email_headers
= git_config_bool(var
, value
);
461 if (!strcmp(var
, "log.abbrevcommit")) {
462 default_abbrev_commit
= git_config_bool(var
, value
);
465 if (!strcmp(var
, "log.date"))
466 return git_config_string(&default_date_mode
, var
, value
);
467 if (!strcmp(var
, "log.decorate")) {
468 decoration_style
= parse_decoration_style(value
);
469 if (decoration_style
< 0)
470 decoration_style
= 0; /* maybe warn? */
473 if (!strcmp(var
, "log.showroot")) {
474 default_show_root
= git_config_bool(var
, value
);
477 if (!strcmp(var
, "log.follow")) {
478 default_follow
= git_config_bool(var
, value
);
481 if (skip_prefix(var
, "color.decorate.", &slot_name
))
482 return parse_decorate_color_config(var
, slot_name
, value
);
483 if (!strcmp(var
, "log.mailmap")) {
484 use_mailmap_config
= git_config_bool(var
, value
);
487 if (!strcmp(var
, "log.showsignature")) {
488 default_show_signature
= git_config_bool(var
, value
);
492 if (grep_config(var
, value
, cb
) < 0)
494 if (git_gpg_config(var
, value
, cb
) < 0)
496 return git_diff_ui_config(var
, value
, cb
);
499 int cmd_whatchanged(int argc
, const char **argv
, const char *prefix
)
502 struct setup_revision_opt opt
;
505 git_config(git_log_config
, NULL
);
507 repo_init_revisions(the_repository
, &rev
, prefix
);
509 rev
.simplify_history
= 0;
510 memset(&opt
, 0, sizeof(opt
));
512 opt
.revarg_opt
= REVARG_COMMITTISH
;
513 cmd_log_init(argc
, argv
, prefix
, &rev
, &opt
);
514 if (!rev
.diffopt
.output_format
)
515 rev
.diffopt
.output_format
= DIFF_FORMAT_RAW
;
516 return cmd_log_walk(&rev
);
519 static void show_tagger(const char *buf
, struct rev_info
*rev
)
521 struct strbuf out
= STRBUF_INIT
;
522 struct pretty_print_context pp
= {0};
524 pp
.fmt
= rev
->commit_format
;
525 pp
.date_mode
= rev
->date_mode
;
526 pp_user_info(&pp
, "Tagger", &out
, buf
, get_log_output_encoding());
527 fprintf(rev
->diffopt
.file
, "%s", out
.buf
);
528 strbuf_release(&out
);
531 static int show_blob_object(const struct object_id
*oid
, struct rev_info
*rev
, const char *obj_name
)
533 struct object_id oidc
;
534 struct object_context obj_context
;
538 fflush(rev
->diffopt
.file
);
539 if (!rev
->diffopt
.flags
.textconv_set_via_cmdline
||
540 !rev
->diffopt
.flags
.allow_textconv
)
541 return stream_blob_to_fd(1, oid
, NULL
, 0);
543 if (get_oid_with_context(the_repository
, obj_name
,
545 &oidc
, &obj_context
))
546 die(_("not a valid object name %s"), obj_name
);
547 if (!obj_context
.path
||
548 !textconv_object(the_repository
, obj_context
.path
,
549 obj_context
.mode
, &oidc
, 1, &buf
, &size
)) {
550 free(obj_context
.path
);
551 return stream_blob_to_fd(1, oid
, NULL
, 0);
555 die(_("git show %s: bad file"), obj_name
);
557 write_or_die(1, buf
, size
);
558 free(obj_context
.path
);
562 static int show_tag_object(const struct object_id
*oid
, struct rev_info
*rev
)
565 enum object_type type
;
566 char *buf
= read_object_file(oid
, &type
, &size
);
570 return error(_("could not read object %s"), oid_to_hex(oid
));
572 assert(type
== OBJ_TAG
);
573 while (offset
< size
&& buf
[offset
] != '\n') {
574 int new_offset
= offset
+ 1;
576 while (new_offset
< size
&& buf
[new_offset
++] != '\n')
578 if (skip_prefix(buf
+ offset
, "tagger ", &ident
))
579 show_tagger(ident
, rev
);
584 fwrite(buf
+ offset
, size
- offset
, 1, rev
->diffopt
.file
);
589 static int show_tree_object(const struct object_id
*oid
,
591 const char *pathname
, unsigned mode
, int stage
, void *context
)
593 FILE *file
= context
;
594 fprintf(file
, "%s%s\n", pathname
, S_ISDIR(mode
) ? "/" : "");
598 static void show_setup_revisions_tweak(struct rev_info
*rev
,
599 struct setup_revision_opt
*opt
)
601 if (rev
->ignore_merges
< 0) {
602 /* There was no "-m" variant on the command line */
603 rev
->ignore_merges
= 0;
604 if (!rev
->first_parent_only
&& !rev
->combine_merges
) {
605 /* No "--first-parent", "-c", or "--cc" */
606 rev
->combine_merges
= 1;
607 rev
->dense_combined_merges
= 1;
610 if (!rev
->diffopt
.output_format
)
611 rev
->diffopt
.output_format
= DIFF_FORMAT_PATCH
;
614 int cmd_show(int argc
, const char **argv
, const char *prefix
)
617 struct object_array_entry
*objects
;
618 struct setup_revision_opt opt
;
619 struct pathspec match_all
;
620 int i
, count
, ret
= 0;
623 git_config(git_log_config
, NULL
);
625 memset(&match_all
, 0, sizeof(match_all
));
626 repo_init_revisions(the_repository
, &rev
, prefix
);
628 rev
.always_show_header
= 1;
629 rev
.no_walk
= REVISION_WALK_NO_WALK_SORTED
;
630 rev
.diffopt
.stat_width
= -1; /* Scale to real terminal size */
632 memset(&opt
, 0, sizeof(opt
));
634 opt
.tweak
= show_setup_revisions_tweak
;
635 cmd_log_init(argc
, argv
, prefix
, &rev
, &opt
);
638 return cmd_log_walk(&rev
);
640 count
= rev
.pending
.nr
;
641 objects
= rev
.pending
.objects
;
642 for (i
= 0; i
< count
&& !ret
; i
++) {
643 struct object
*o
= objects
[i
].item
;
644 const char *name
= objects
[i
].name
;
647 ret
= show_blob_object(&o
->oid
, &rev
, name
);
650 struct tag
*t
= (struct tag
*)o
;
651 struct object_id
*oid
= get_tagged_oid(t
);
655 fprintf(rev
.diffopt
.file
, "%stag %s%s\n",
656 diff_get_color_opt(&rev
.diffopt
, DIFF_COMMIT
),
658 diff_get_color_opt(&rev
.diffopt
, DIFF_RESET
));
659 ret
= show_tag_object(&o
->oid
, &rev
);
663 o
= parse_object(the_repository
, oid
);
665 ret
= error(_("could not read object %s"),
674 fprintf(rev
.diffopt
.file
, "%stree %s%s\n\n",
675 diff_get_color_opt(&rev
.diffopt
, DIFF_COMMIT
),
677 diff_get_color_opt(&rev
.diffopt
, DIFF_RESET
));
678 read_tree_recursive(the_repository
, (struct tree
*)o
, "",
679 0, 0, &match_all
, show_tree_object
,
684 rev
.pending
.nr
= rev
.pending
.alloc
= 0;
685 rev
.pending
.objects
= NULL
;
686 add_object_array(o
, name
, &rev
.pending
);
687 ret
= cmd_log_walk(&rev
);
690 ret
= error(_("unknown type: %d"), o
->type
);
698 * This is equivalent to "git log -g --abbrev-commit --pretty=oneline"
700 int cmd_log_reflog(int argc
, const char **argv
, const char *prefix
)
703 struct setup_revision_opt opt
;
706 git_config(git_log_config
, NULL
);
708 repo_init_revisions(the_repository
, &rev
, prefix
);
709 init_reflog_walk(&rev
.reflog_info
);
710 rev
.verbose_header
= 1;
711 memset(&opt
, 0, sizeof(opt
));
713 cmd_log_init_defaults(&rev
);
714 rev
.abbrev_commit
= 1;
715 rev
.commit_format
= CMIT_FMT_ONELINE
;
716 rev
.use_terminator
= 1;
717 rev
.always_show_header
= 1;
718 cmd_log_init_finish(argc
, argv
, prefix
, &rev
, &opt
);
720 return cmd_log_walk(&rev
);
723 static void log_setup_revisions_tweak(struct rev_info
*rev
,
724 struct setup_revision_opt
*opt
)
726 if (rev
->diffopt
.flags
.default_follow_renames
&&
727 rev
->prune_data
.nr
== 1)
728 rev
->diffopt
.flags
.follow_renames
= 1;
730 /* Turn --cc/-c into -p --cc/-c when -p was not given */
731 if (!rev
->diffopt
.output_format
&& rev
->combine_merges
)
732 rev
->diffopt
.output_format
= DIFF_FORMAT_PATCH
;
734 if (rev
->first_parent_only
&& rev
->ignore_merges
< 0)
735 rev
->ignore_merges
= 0;
738 int cmd_log(int argc
, const char **argv
, const char *prefix
)
741 struct setup_revision_opt opt
;
744 git_config(git_log_config
, NULL
);
746 repo_init_revisions(the_repository
, &rev
, prefix
);
747 rev
.always_show_header
= 1;
748 memset(&opt
, 0, sizeof(opt
));
750 opt
.revarg_opt
= REVARG_COMMITTISH
;
751 opt
.tweak
= log_setup_revisions_tweak
;
752 cmd_log_init(argc
, argv
, prefix
, &rev
, &opt
);
753 return cmd_log_walk(&rev
);
758 static const char *fmt_patch_suffix
= ".patch";
759 static int numbered
= 0;
760 static int auto_number
= 1;
762 static char *default_attach
= NULL
;
764 static struct string_list extra_hdr
= STRING_LIST_INIT_NODUP
;
765 static struct string_list extra_to
= STRING_LIST_INIT_NODUP
;
766 static struct string_list extra_cc
= STRING_LIST_INIT_NODUP
;
768 static void add_header(const char *value
)
770 struct string_list_item
*item
;
771 int len
= strlen(value
);
772 while (len
&& value
[len
- 1] == '\n')
775 if (!strncasecmp(value
, "to: ", 4)) {
776 item
= string_list_append(&extra_to
, value
+ 4);
778 } else if (!strncasecmp(value
, "cc: ", 4)) {
779 item
= string_list_append(&extra_cc
, value
+ 4);
782 item
= string_list_append(&extra_hdr
, value
);
785 item
->string
[len
] = '\0';
801 enum cover_from_description
{
808 static enum thread_level thread
;
809 static int do_signoff
;
810 static int base_auto
;
812 static const char *signature
= git_version_string
;
813 static const char *signature_file
;
814 static enum cover_setting config_cover_letter
;
815 static const char *config_output_directory
;
816 static enum cover_from_description cover_from_description_mode
= COVER_FROM_MESSAGE
;
817 static int show_notes
;
818 static struct display_notes_opt notes_opt
;
820 static enum cover_from_description
parse_cover_from_description(const char *arg
)
822 if (!arg
|| !strcmp(arg
, "default"))
823 return COVER_FROM_MESSAGE
;
824 else if (!strcmp(arg
, "none"))
825 return COVER_FROM_NONE
;
826 else if (!strcmp(arg
, "message"))
827 return COVER_FROM_MESSAGE
;
828 else if (!strcmp(arg
, "subject"))
829 return COVER_FROM_SUBJECT
;
830 else if (!strcmp(arg
, "auto"))
831 return COVER_FROM_AUTO
;
833 die(_("%s: invalid cover from description mode"), arg
);
836 static int git_format_config(const char *var
, const char *value
, void *cb
)
838 if (!strcmp(var
, "format.headers")) {
840 die(_("format.headers without value"));
844 if (!strcmp(var
, "format.suffix"))
845 return git_config_string(&fmt_patch_suffix
, var
, value
);
846 if (!strcmp(var
, "format.to")) {
848 return config_error_nonbool(var
);
849 string_list_append(&extra_to
, value
);
852 if (!strcmp(var
, "format.cc")) {
854 return config_error_nonbool(var
);
855 string_list_append(&extra_cc
, value
);
858 if (!strcmp(var
, "diff.color") || !strcmp(var
, "color.diff") ||
859 !strcmp(var
, "color.ui") || !strcmp(var
, "diff.submodule")) {
862 if (!strcmp(var
, "format.numbered")) {
863 if (value
&& !strcasecmp(value
, "auto")) {
867 numbered
= git_config_bool(var
, value
);
868 auto_number
= auto_number
&& numbered
;
871 if (!strcmp(var
, "format.attach")) {
873 default_attach
= xstrdup(value
);
875 default_attach
= xstrdup(git_version_string
);
878 if (!strcmp(var
, "format.thread")) {
879 if (value
&& !strcasecmp(value
, "deep")) {
880 thread
= THREAD_DEEP
;
883 if (value
&& !strcasecmp(value
, "shallow")) {
884 thread
= THREAD_SHALLOW
;
887 thread
= git_config_bool(var
, value
) ? THREAD_SHALLOW
: THREAD_UNSET
;
890 if (!strcmp(var
, "format.signoff")) {
891 do_signoff
= git_config_bool(var
, value
);
894 if (!strcmp(var
, "format.signature"))
895 return git_config_string(&signature
, var
, value
);
896 if (!strcmp(var
, "format.signaturefile"))
897 return git_config_pathname(&signature_file
, var
, value
);
898 if (!strcmp(var
, "format.coverletter")) {
899 if (value
&& !strcasecmp(value
, "auto")) {
900 config_cover_letter
= COVER_AUTO
;
903 config_cover_letter
= git_config_bool(var
, value
) ? COVER_ON
: COVER_OFF
;
906 if (!strcmp(var
, "format.outputdirectory"))
907 return git_config_string(&config_output_directory
, var
, value
);
908 if (!strcmp(var
, "format.useautobase")) {
909 base_auto
= git_config_bool(var
, value
);
912 if (!strcmp(var
, "format.from")) {
913 int b
= git_parse_maybe_bool(value
);
916 from
= xstrdup(value
);
918 from
= xstrdup(git_committer_info(IDENT_NO_DATE
));
923 if (!strcmp(var
, "format.notes")) {
924 int b
= git_parse_maybe_bool(value
);
926 enable_ref_display_notes(¬es_opt
, &show_notes
, value
);
928 enable_default_display_notes(¬es_opt
, &show_notes
);
930 disable_display_notes(¬es_opt
, &show_notes
);
933 if (!strcmp(var
, "format.coverfromdescription")) {
934 cover_from_description_mode
= parse_cover_from_description(value
);
938 return git_log_config(var
, value
, cb
);
941 static const char *output_directory
= NULL
;
942 static int outdir_offset
;
944 static int open_next_file(struct commit
*commit
, const char *subject
,
945 struct rev_info
*rev
, int quiet
)
947 struct strbuf filename
= STRBUF_INIT
;
948 int suffix_len
= strlen(rev
->patch_suffix
) + 1;
950 if (output_directory
) {
951 strbuf_addstr(&filename
, output_directory
);
953 PATH_MAX
- FORMAT_PATCH_NAME_MAX
- suffix_len
) {
954 strbuf_release(&filename
);
955 return error(_("name of output directory is too long"));
957 strbuf_complete(&filename
, '/');
960 if (rev
->numbered_files
)
961 strbuf_addf(&filename
, "%d", rev
->nr
);
963 fmt_output_commit(&filename
, commit
, rev
);
965 fmt_output_subject(&filename
, subject
, rev
);
968 printf("%s\n", filename
.buf
+ outdir_offset
);
970 if ((rev
->diffopt
.file
= fopen(filename
.buf
, "w")) == NULL
) {
971 error_errno(_("cannot open patch file %s"), filename
.buf
);
972 strbuf_release(&filename
);
976 strbuf_release(&filename
);
980 static void get_patch_ids(struct rev_info
*rev
, struct patch_ids
*ids
)
982 struct rev_info check_rev
;
983 struct commit
*commit
, *c1
, *c2
;
984 struct object
*o1
, *o2
;
985 unsigned flags1
, flags2
;
987 if (rev
->pending
.nr
!= 2)
988 die(_("need exactly one range"));
990 o1
= rev
->pending
.objects
[0].item
;
991 o2
= rev
->pending
.objects
[1].item
;
994 c1
= lookup_commit_reference(the_repository
, &o1
->oid
);
995 c2
= lookup_commit_reference(the_repository
, &o2
->oid
);
997 if ((flags1
& UNINTERESTING
) == (flags2
& UNINTERESTING
))
998 die(_("not a range"));
1000 init_patch_ids(the_repository
, ids
);
1002 /* given a range a..b get all patch ids for b..a */
1003 repo_init_revisions(the_repository
, &check_rev
, rev
->prefix
);
1004 check_rev
.max_parents
= 1;
1005 o1
->flags
^= UNINTERESTING
;
1006 o2
->flags
^= UNINTERESTING
;
1007 add_pending_object(&check_rev
, o1
, "o1");
1008 add_pending_object(&check_rev
, o2
, "o2");
1009 if (prepare_revision_walk(&check_rev
))
1010 die(_("revision walk setup failed"));
1012 while ((commit
= get_revision(&check_rev
)) != NULL
) {
1013 add_commit_patch_id(commit
, ids
);
1016 /* reset for next revision walk */
1017 clear_commit_marks(c1
, SEEN
| UNINTERESTING
| SHOWN
| ADDED
);
1018 clear_commit_marks(c2
, SEEN
| UNINTERESTING
| SHOWN
| ADDED
);
1023 static void gen_message_id(struct rev_info
*info
, char *base
)
1025 struct strbuf buf
= STRBUF_INIT
;
1026 strbuf_addf(&buf
, "%s.%"PRItime
".git.%s", base
,
1027 (timestamp_t
) time(NULL
),
1028 git_committer_info(IDENT_NO_NAME
|IDENT_NO_DATE
|IDENT_STRICT
));
1029 info
->message_id
= strbuf_detach(&buf
, NULL
);
1032 static void print_signature(FILE *file
)
1034 if (!signature
|| !*signature
)
1037 fprintf(file
, "-- \n%s", signature
);
1038 if (signature
[strlen(signature
)-1] != '\n')
1043 static char *find_branch_name(struct rev_info
*rev
)
1045 int i
, positive
= -1;
1046 struct object_id branch_oid
;
1047 const struct object_id
*tip_oid
;
1048 const char *ref
, *v
;
1049 char *full_ref
, *branch
= NULL
;
1051 for (i
= 0; i
< rev
->cmdline
.nr
; i
++) {
1052 if (rev
->cmdline
.rev
[i
].flags
& UNINTERESTING
)
1061 ref
= rev
->cmdline
.rev
[positive
].name
;
1062 tip_oid
= &rev
->cmdline
.rev
[positive
].item
->oid
;
1063 if (dwim_ref(ref
, strlen(ref
), &branch_oid
, &full_ref
, 0) &&
1064 skip_prefix(full_ref
, "refs/heads/", &v
) &&
1065 oideq(tip_oid
, &branch_oid
))
1066 branch
= xstrdup(v
);
1071 static void show_diffstat(struct rev_info
*rev
,
1072 struct commit
*origin
, struct commit
*head
)
1074 struct diff_options opts
;
1076 memcpy(&opts
, &rev
->diffopt
, sizeof(opts
));
1077 opts
.output_format
= DIFF_FORMAT_SUMMARY
| DIFF_FORMAT_DIFFSTAT
;
1078 diff_setup_done(&opts
);
1080 diff_tree_oid(get_commit_tree_oid(origin
),
1081 get_commit_tree_oid(head
),
1083 diffcore_std(&opts
);
1086 fprintf(rev
->diffopt
.file
, "\n");
1089 static void prepare_cover_text(struct pretty_print_context
*pp
,
1090 const char *branch_name
,
1092 const char *encoding
,
1095 const char *subject
= "*** SUBJECT HERE ***";
1096 const char *body
= "*** BLURB HERE ***";
1097 struct strbuf description_sb
= STRBUF_INIT
;
1098 struct strbuf subject_sb
= STRBUF_INIT
;
1100 if (cover_from_description_mode
== COVER_FROM_NONE
)
1103 if (branch_name
&& *branch_name
)
1104 read_branch_desc(&description_sb
, branch_name
);
1105 if (!description_sb
.len
)
1108 if (cover_from_description_mode
== COVER_FROM_SUBJECT
||
1109 cover_from_description_mode
== COVER_FROM_AUTO
)
1110 body
= format_subject(&subject_sb
, description_sb
.buf
, " ");
1112 if (cover_from_description_mode
== COVER_FROM_MESSAGE
||
1113 (cover_from_description_mode
== COVER_FROM_AUTO
&&
1114 subject_sb
.len
> COVER_FROM_AUTO_MAX_SUBJECT_LEN
))
1115 body
= description_sb
.buf
;
1117 subject
= subject_sb
.buf
;
1120 pp_title_line(pp
, &subject
, sb
, encoding
, need_8bit_cte
);
1121 pp_remainder(pp
, &body
, sb
, 0);
1123 strbuf_release(&description_sb
);
1124 strbuf_release(&subject_sb
);
1127 static int get_notes_refs(struct string_list_item
*item
, void *arg
)
1129 strvec_pushf(arg
, "--notes=%s", item
->string
);
1133 static void get_notes_args(struct strvec
*arg
, struct rev_info
*rev
)
1135 if (!rev
->show_notes
) {
1136 strvec_push(arg
, "--no-notes");
1137 } else if (rev
->notes_opt
.use_default_notes
> 0 ||
1138 (rev
->notes_opt
.use_default_notes
== -1 &&
1139 !rev
->notes_opt
.extra_notes_refs
.nr
)) {
1140 strvec_push(arg
, "--notes");
1142 for_each_string_list(&rev
->notes_opt
.extra_notes_refs
, get_notes_refs
, arg
);
1146 static void make_cover_letter(struct rev_info
*rev
, int use_stdout
,
1147 struct commit
*origin
,
1148 int nr
, struct commit
**list
,
1149 const char *branch_name
,
1152 const char *committer
;
1153 struct shortlog log
;
1154 struct strbuf sb
= STRBUF_INIT
;
1156 const char *encoding
= "UTF-8";
1157 int need_8bit_cte
= 0;
1158 struct pretty_print_context pp
= {0};
1159 struct commit
*head
= list
[0];
1161 if (!cmit_fmt_is_mail(rev
->commit_format
))
1162 die(_("cover letter needs email format"));
1164 committer
= git_committer_info(0);
1167 open_next_file(NULL
, rev
->numbered_files
? NULL
: "cover-letter", rev
, quiet
))
1168 die(_("failed to create cover-letter file"));
1170 log_write_email_headers(rev
, head
, &pp
.after_subject
, &need_8bit_cte
, 0);
1172 for (i
= 0; !need_8bit_cte
&& i
< nr
; i
++) {
1173 const char *buf
= get_commit_buffer(list
[i
], NULL
);
1174 if (has_non_ascii(buf
))
1176 unuse_commit_buffer(list
[i
], buf
);
1180 branch_name
= find_branch_name(rev
);
1182 pp
.fmt
= CMIT_FMT_EMAIL
;
1183 pp
.date_mode
.type
= DATE_RFC2822
;
1185 pp
.print_email_subject
= 1;
1186 pp_user_info(&pp
, NULL
, &sb
, committer
, encoding
);
1187 prepare_cover_text(&pp
, branch_name
, &sb
, encoding
, need_8bit_cte
);
1188 fprintf(rev
->diffopt
.file
, "%s\n", sb
.buf
);
1190 strbuf_release(&sb
);
1192 shortlog_init(&log
);
1194 log
.wrap
= MAIL_DEFAULT_WRAP
;
1197 log
.file
= rev
->diffopt
.file
;
1198 log
.groups
= SHORTLOG_GROUP_AUTHOR
;
1199 for (i
= 0; i
< nr
; i
++)
1200 shortlog_add_commit(&log
, list
[i
]);
1202 shortlog_output(&log
);
1204 /* We can only do diffstat with a unique reference point */
1206 show_diffstat(rev
, origin
, head
);
1208 if (rev
->idiff_oid1
) {
1209 fprintf_ln(rev
->diffopt
.file
, "%s", rev
->idiff_title
);
1210 show_interdiff(rev
->idiff_oid1
, rev
->idiff_oid2
, 0,
1216 * Pass minimum required diff-options to range-diff; others
1217 * can be added later if deemed desirable.
1219 struct diff_options opts
;
1220 struct strvec other_arg
= STRVEC_INIT
;
1222 opts
.file
= rev
->diffopt
.file
;
1223 opts
.use_color
= rev
->diffopt
.use_color
;
1224 diff_setup_done(&opts
);
1225 fprintf_ln(rev
->diffopt
.file
, "%s", rev
->rdiff_title
);
1226 get_notes_args(&other_arg
, rev
);
1227 show_range_diff(rev
->rdiff1
, rev
->rdiff2
,
1228 rev
->creation_factor
, 1, &opts
, &other_arg
);
1229 strvec_clear(&other_arg
);
1233 static const char *clean_message_id(const char *msg_id
)
1236 const char *a
, *z
, *m
;
1239 while ((ch
= *m
) && (isspace(ch
) || (ch
== '<')))
1244 if (!isspace(ch
) && (ch
!= '>'))
1249 die(_("insane in-reply-to: %s"), msg_id
);
1252 return xmemdupz(a
, z
- a
);
1255 static const char *set_outdir(const char *prefix
, const char *output_directory
)
1257 if (output_directory
&& is_absolute_path(output_directory
))
1258 return output_directory
;
1260 if (!prefix
|| !*prefix
) {
1261 if (output_directory
)
1262 return output_directory
;
1263 /* The user did not explicitly ask for "./" */
1268 outdir_offset
= strlen(prefix
);
1269 if (!output_directory
)
1272 return prefix_filename(prefix
, output_directory
);
1275 static const char * const builtin_format_patch_usage
[] = {
1276 N_("git format-patch [<options>] [<since> | <revision-range>]"),
1280 static int keep_subject
= 0;
1282 static int keep_callback(const struct option
*opt
, const char *arg
, int unset
)
1284 BUG_ON_OPT_NEG(unset
);
1285 BUG_ON_OPT_ARG(arg
);
1286 ((struct rev_info
*)opt
->value
)->total
= -1;
1291 static int subject_prefix
= 0;
1293 static int subject_prefix_callback(const struct option
*opt
, const char *arg
,
1296 BUG_ON_OPT_NEG(unset
);
1298 ((struct rev_info
*)opt
->value
)->subject_prefix
= arg
;
1302 static int rfc_callback(const struct option
*opt
, const char *arg
, int unset
)
1304 BUG_ON_OPT_NEG(unset
);
1305 BUG_ON_OPT_ARG(arg
);
1306 return subject_prefix_callback(opt
, "RFC PATCH", unset
);
1309 static int numbered_cmdline_opt
= 0;
1311 static int numbered_callback(const struct option
*opt
, const char *arg
,
1314 BUG_ON_OPT_ARG(arg
);
1315 *(int *)opt
->value
= numbered_cmdline_opt
= unset
? 0 : 1;
1321 static int no_numbered_callback(const struct option
*opt
, const char *arg
,
1324 BUG_ON_OPT_NEG(unset
);
1325 return numbered_callback(opt
, arg
, 1);
1328 static int output_directory_callback(const struct option
*opt
, const char *arg
,
1331 const char **dir
= (const char **)opt
->value
;
1332 BUG_ON_OPT_NEG(unset
);
1334 die(_("two output directories?"));
1339 static int thread_callback(const struct option
*opt
, const char *arg
, int unset
)
1341 enum thread_level
*thread
= (enum thread_level
*)opt
->value
;
1343 *thread
= THREAD_UNSET
;
1344 else if (!arg
|| !strcmp(arg
, "shallow"))
1345 *thread
= THREAD_SHALLOW
;
1346 else if (!strcmp(arg
, "deep"))
1347 *thread
= THREAD_DEEP
;
1349 * Please update _git_formatpatch() in git-completion.bash
1350 * when you add new options.
1357 static int attach_callback(const struct option
*opt
, const char *arg
, int unset
)
1359 struct rev_info
*rev
= (struct rev_info
*)opt
->value
;
1361 rev
->mime_boundary
= NULL
;
1363 rev
->mime_boundary
= arg
;
1365 rev
->mime_boundary
= git_version_string
;
1366 rev
->no_inline
= unset
? 0 : 1;
1370 static int inline_callback(const struct option
*opt
, const char *arg
, int unset
)
1372 struct rev_info
*rev
= (struct rev_info
*)opt
->value
;
1374 rev
->mime_boundary
= NULL
;
1376 rev
->mime_boundary
= arg
;
1378 rev
->mime_boundary
= git_version_string
;
1383 static int header_callback(const struct option
*opt
, const char *arg
, int unset
)
1386 string_list_clear(&extra_hdr
, 0);
1387 string_list_clear(&extra_to
, 0);
1388 string_list_clear(&extra_cc
, 0);
1395 static int to_callback(const struct option
*opt
, const char *arg
, int unset
)
1398 string_list_clear(&extra_to
, 0);
1400 string_list_append(&extra_to
, arg
);
1404 static int cc_callback(const struct option
*opt
, const char *arg
, int unset
)
1407 string_list_clear(&extra_cc
, 0);
1409 string_list_append(&extra_cc
, arg
);
1413 static int from_callback(const struct option
*opt
, const char *arg
, int unset
)
1415 char **from
= opt
->value
;
1422 *from
= xstrdup(arg
);
1424 *from
= xstrdup(git_committer_info(IDENT_NO_DATE
));
1428 struct base_tree_info
{
1429 struct object_id base_commit
;
1430 int nr_patch_id
, alloc_patch_id
;
1431 struct object_id
*patch_id
;
1434 static struct commit
*get_base_commit(const char *base_commit
,
1435 struct commit
**list
,
1438 struct commit
*base
= NULL
;
1439 struct commit
**rev
;
1440 int i
= 0, rev_nr
= 0;
1442 if (base_commit
&& strcmp(base_commit
, "auto")) {
1443 base
= lookup_commit_reference_by_name(base_commit
);
1445 die(_("unknown commit %s"), base_commit
);
1446 } else if ((base_commit
&& !strcmp(base_commit
, "auto"))) {
1447 struct branch
*curr_branch
= branch_get(NULL
);
1448 const char *upstream
= branch_get_upstream(curr_branch
, NULL
);
1450 struct commit_list
*base_list
;
1451 struct commit
*commit
;
1452 struct object_id oid
;
1454 if (get_oid(upstream
, &oid
))
1455 die(_("failed to resolve '%s' as a valid ref"), upstream
);
1456 commit
= lookup_commit_or_die(&oid
, "upstream base");
1457 base_list
= get_merge_bases_many(commit
, total
, list
);
1458 /* There should be one and only one merge base. */
1459 if (!base_list
|| base_list
->next
)
1460 die(_("could not find exact merge base"));
1461 base
= base_list
->item
;
1462 free_commit_list(base_list
);
1464 die(_("failed to get upstream, if you want to record base commit automatically,\n"
1465 "please use git branch --set-upstream-to to track a remote branch.\n"
1466 "Or you could specify base commit by --base=<base-commit-id> manually"));
1470 ALLOC_ARRAY(rev
, total
);
1471 for (i
= 0; i
< total
; i
++)
1476 * Get merge base through pair-wise computations
1477 * and store it in rev[0].
1479 while (rev_nr
> 1) {
1480 for (i
= 0; i
< rev_nr
/ 2; i
++) {
1481 struct commit_list
*merge_base
;
1482 merge_base
= get_merge_bases(rev
[2 * i
], rev
[2 * i
+ 1]);
1483 if (!merge_base
|| merge_base
->next
)
1484 die(_("failed to find exact merge base"));
1486 rev
[i
] = merge_base
->item
;
1490 rev
[i
] = rev
[2 * i
];
1491 rev_nr
= DIV_ROUND_UP(rev_nr
, 2);
1494 if (!in_merge_bases(base
, rev
[0]))
1495 die(_("base commit should be the ancestor of revision list"));
1497 for (i
= 0; i
< total
; i
++) {
1498 if (base
== list
[i
])
1499 die(_("base commit shouldn't be in revision list"));
1506 define_commit_slab(commit_base
, int);
1508 static void prepare_bases(struct base_tree_info
*bases
,
1509 struct commit
*base
,
1510 struct commit
**list
,
1513 struct commit
*commit
;
1514 struct rev_info revs
;
1515 struct diff_options diffopt
;
1516 struct commit_base commit_base
;
1522 init_commit_base(&commit_base
);
1523 repo_diff_setup(the_repository
, &diffopt
);
1524 diffopt
.flags
.recursive
= 1;
1525 diff_setup_done(&diffopt
);
1527 oidcpy(&bases
->base_commit
, &base
->object
.oid
);
1529 repo_init_revisions(the_repository
, &revs
, NULL
);
1530 revs
.max_parents
= 1;
1531 revs
.topo_order
= 1;
1532 for (i
= 0; i
< total
; i
++) {
1533 list
[i
]->object
.flags
&= ~UNINTERESTING
;
1534 add_pending_object(&revs
, &list
[i
]->object
, "rev_list");
1535 *commit_base_at(&commit_base
, list
[i
]) = 1;
1537 base
->object
.flags
|= UNINTERESTING
;
1538 add_pending_object(&revs
, &base
->object
, "base");
1540 if (prepare_revision_walk(&revs
))
1541 die(_("revision walk setup failed"));
1543 * Traverse the commits list, get prerequisite patch ids
1544 * and stuff them in bases structure.
1546 while ((commit
= get_revision(&revs
)) != NULL
) {
1547 struct object_id oid
;
1548 struct object_id
*patch_id
;
1549 if (*commit_base_at(&commit_base
, commit
))
1551 if (commit_patch_id(commit
, &diffopt
, &oid
, 0, 1))
1552 die(_("cannot get patch id"));
1553 ALLOC_GROW(bases
->patch_id
, bases
->nr_patch_id
+ 1, bases
->alloc_patch_id
);
1554 patch_id
= bases
->patch_id
+ bases
->nr_patch_id
;
1555 oidcpy(patch_id
, &oid
);
1556 bases
->nr_patch_id
++;
1558 clear_commit_base(&commit_base
);
1561 static void print_bases(struct base_tree_info
*bases
, FILE *file
)
1565 /* Only do this once, either for the cover or for the first one */
1566 if (is_null_oid(&bases
->base_commit
))
1569 /* Show the base commit */
1570 fprintf(file
, "\nbase-commit: %s\n", oid_to_hex(&bases
->base_commit
));
1572 /* Show the prerequisite patches */
1573 for (i
= bases
->nr_patch_id
- 1; i
>= 0; i
--)
1574 fprintf(file
, "prerequisite-patch-id: %s\n", oid_to_hex(&bases
->patch_id
[i
]));
1576 free(bases
->patch_id
);
1577 bases
->nr_patch_id
= 0;
1578 bases
->alloc_patch_id
= 0;
1579 oidclr(&bases
->base_commit
);
1582 static const char *diff_title(struct strbuf
*sb
, int reroll_count
,
1583 const char *generic
, const char *rerolled
)
1585 if (reroll_count
<= 0)
1586 strbuf_addstr(sb
, generic
);
1587 else /* RFC may be v0, so allow -v1 to diff against v0 */
1588 strbuf_addf(sb
, rerolled
, reroll_count
- 1);
1592 static void infer_range_diff_ranges(struct strbuf
*r1
,
1595 struct commit
*origin
,
1596 struct commit
*head
)
1598 const char *head_oid
= oid_to_hex(&head
->object
.oid
);
1599 int prev_is_range
= !!strstr(prev
, "..");
1602 strbuf_addstr(r1
, prev
);
1604 strbuf_addf(r1
, "%s..%s", head_oid
, prev
);
1607 strbuf_addf(r2
, "%s..%s", oid_to_hex(&origin
->object
.oid
), head_oid
);
1608 else if (prev_is_range
)
1609 die(_("failed to infer range-diff origin of current series"));
1611 warning(_("using '%s' as range-diff origin of current series"), prev
);
1612 strbuf_addf(r2
, "%s..%s", prev
, head_oid
);
1616 int cmd_format_patch(int argc
, const char **argv
, const char *prefix
)
1618 struct commit
*commit
;
1619 struct commit
**list
= NULL
;
1620 struct rev_info rev
;
1621 struct setup_revision_opt s_r_opt
;
1622 int nr
= 0, total
, i
;
1624 int start_number
= -1;
1625 int just_numbers
= 0;
1626 int ignore_if_in_upstream
= 0;
1627 int cover_letter
= -1;
1628 int boundary_count
= 0;
1629 int no_binary_diff
= 0;
1630 int zero_commit
= 0;
1631 struct commit
*origin
= NULL
;
1632 const char *in_reply_to
= NULL
;
1633 struct patch_ids ids
;
1634 struct strbuf buf
= STRBUF_INIT
;
1635 int use_patch_format
= 0;
1637 int reroll_count
= -1;
1638 char *cover_from_description_arg
= NULL
;
1639 char *branch_name
= NULL
;
1640 char *base_commit
= NULL
;
1641 struct base_tree_info bases
;
1642 int show_progress
= 0;
1643 struct progress
*progress
= NULL
;
1644 struct oid_array idiff_prev
= OID_ARRAY_INIT
;
1645 struct strbuf idiff_title
= STRBUF_INIT
;
1646 const char *rdiff_prev
= NULL
;
1647 struct strbuf rdiff1
= STRBUF_INIT
;
1648 struct strbuf rdiff2
= STRBUF_INIT
;
1649 struct strbuf rdiff_title
= STRBUF_INIT
;
1650 int creation_factor
= -1;
1652 const struct option builtin_format_patch_options
[] = {
1653 OPT_CALLBACK_F('n', "numbered", &numbered
, NULL
,
1654 N_("use [PATCH n/m] even with a single patch"),
1655 PARSE_OPT_NOARG
, numbered_callback
),
1656 OPT_CALLBACK_F('N', "no-numbered", &numbered
, NULL
,
1657 N_("use [PATCH] even with multiple patches"),
1658 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
, no_numbered_callback
),
1659 OPT_BOOL('s', "signoff", &do_signoff
, N_("add Signed-off-by:")),
1660 OPT_BOOL(0, "stdout", &use_stdout
,
1661 N_("print patches to standard out")),
1662 OPT_BOOL(0, "cover-letter", &cover_letter
,
1663 N_("generate a cover letter")),
1664 OPT_BOOL(0, "numbered-files", &just_numbers
,
1665 N_("use simple number sequence for output file names")),
1666 OPT_STRING(0, "suffix", &fmt_patch_suffix
, N_("sfx"),
1667 N_("use <sfx> instead of '.patch'")),
1668 OPT_INTEGER(0, "start-number", &start_number
,
1669 N_("start numbering patches at <n> instead of 1")),
1670 OPT_INTEGER('v', "reroll-count", &reroll_count
,
1671 N_("mark the series as Nth re-roll")),
1672 OPT_CALLBACK_F(0, "rfc", &rev
, NULL
,
1673 N_("Use [RFC PATCH] instead of [PATCH]"),
1674 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
, rfc_callback
),
1675 OPT_STRING(0, "cover-from-description", &cover_from_description_arg
,
1676 N_("cover-from-description-mode"),
1677 N_("generate parts of a cover letter based on a branch's description")),
1678 OPT_CALLBACK_F(0, "subject-prefix", &rev
, N_("prefix"),
1679 N_("Use [<prefix>] instead of [PATCH]"),
1680 PARSE_OPT_NONEG
, subject_prefix_callback
),
1681 OPT_CALLBACK_F('o', "output-directory", &output_directory
,
1682 N_("dir"), N_("store resulting files in <dir>"),
1683 PARSE_OPT_NONEG
, output_directory_callback
),
1684 OPT_CALLBACK_F('k', "keep-subject", &rev
, NULL
,
1685 N_("don't strip/add [PATCH]"),
1686 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
, keep_callback
),
1687 OPT_BOOL(0, "no-binary", &no_binary_diff
,
1688 N_("don't output binary diffs")),
1689 OPT_BOOL(0, "zero-commit", &zero_commit
,
1690 N_("output all-zero hash in From header")),
1691 OPT_BOOL(0, "ignore-if-in-upstream", &ignore_if_in_upstream
,
1692 N_("don't include a patch matching a commit upstream")),
1693 OPT_SET_INT_F('p', "no-stat", &use_patch_format
,
1694 N_("show patch format instead of default (patch + stat)"),
1695 1, PARSE_OPT_NONEG
),
1696 OPT_GROUP(N_("Messaging")),
1697 OPT_CALLBACK(0, "add-header", NULL
, N_("header"),
1698 N_("add email header"), header_callback
),
1699 OPT_CALLBACK(0, "to", NULL
, N_("email"), N_("add To: header"), to_callback
),
1700 OPT_CALLBACK(0, "cc", NULL
, N_("email"), N_("add Cc: header"), cc_callback
),
1701 OPT_CALLBACK_F(0, "from", &from
, N_("ident"),
1702 N_("set From address to <ident> (or committer ident if absent)"),
1703 PARSE_OPT_OPTARG
, from_callback
),
1704 OPT_STRING(0, "in-reply-to", &in_reply_to
, N_("message-id"),
1705 N_("make first mail a reply to <message-id>")),
1706 OPT_CALLBACK_F(0, "attach", &rev
, N_("boundary"),
1707 N_("attach the patch"), PARSE_OPT_OPTARG
,
1709 OPT_CALLBACK_F(0, "inline", &rev
, N_("boundary"),
1710 N_("inline the patch"),
1711 PARSE_OPT_OPTARG
| PARSE_OPT_NONEG
,
1713 OPT_CALLBACK_F(0, "thread", &thread
, N_("style"),
1714 N_("enable message threading, styles: shallow, deep"),
1715 PARSE_OPT_OPTARG
, thread_callback
),
1716 OPT_STRING(0, "signature", &signature
, N_("signature"),
1717 N_("add a signature")),
1718 OPT_STRING(0, "base", &base_commit
, N_("base-commit"),
1719 N_("add prerequisite tree info to the patch series")),
1720 OPT_FILENAME(0, "signature-file", &signature_file
,
1721 N_("add a signature from a file")),
1722 OPT__QUIET(&quiet
, N_("don't print the patch filenames")),
1723 OPT_BOOL(0, "progress", &show_progress
,
1724 N_("show progress while generating patches")),
1725 OPT_CALLBACK(0, "interdiff", &idiff_prev
, N_("rev"),
1726 N_("show changes against <rev> in cover letter or single patch"),
1727 parse_opt_object_name
),
1728 OPT_STRING(0, "range-diff", &rdiff_prev
, N_("refspec"),
1729 N_("show changes against <refspec> in cover letter or single patch")),
1730 OPT_INTEGER(0, "creation-factor", &creation_factor
,
1731 N_("percentage by which creation is weighted")),
1735 extra_hdr
.strdup_strings
= 1;
1736 extra_to
.strdup_strings
= 1;
1737 extra_cc
.strdup_strings
= 1;
1738 init_log_defaults();
1739 init_display_notes(¬es_opt
);
1740 git_config(git_format_config
, NULL
);
1741 repo_init_revisions(the_repository
, &rev
, prefix
);
1742 rev
.show_notes
= show_notes
;
1743 memcpy(&rev
.notes_opt
, ¬es_opt
, sizeof(notes_opt
));
1744 rev
.commit_format
= CMIT_FMT_EMAIL
;
1745 rev
.encode_email_headers
= default_encode_email_headers
;
1746 rev
.expand_tabs_in_log_default
= 0;
1747 rev
.verbose_header
= 1;
1749 rev
.max_parents
= 1;
1750 rev
.diffopt
.flags
.recursive
= 1;
1751 rev
.subject_prefix
= fmt_patch_subject_prefix
;
1752 memset(&s_r_opt
, 0, sizeof(s_r_opt
));
1753 s_r_opt
.def
= "HEAD";
1754 s_r_opt
.revarg_opt
= REVARG_COMMITTISH
;
1757 base_commit
= "auto";
1759 if (default_attach
) {
1760 rev
.mime_boundary
= default_attach
;
1765 * Parse the arguments before setup_revisions(), or something
1766 * like "git format-patch -o a123 HEAD^.." may fail; a123 is
1767 * possibly a valid SHA1.
1769 argc
= parse_options(argc
, argv
, prefix
, builtin_format_patch_options
,
1770 builtin_format_patch_usage
,
1771 PARSE_OPT_KEEP_ARGV0
| PARSE_OPT_KEEP_UNKNOWN
|
1772 PARSE_OPT_KEEP_DASHDASH
);
1774 if (cover_from_description_arg
)
1775 cover_from_description_mode
= parse_cover_from_description(cover_from_description_arg
);
1777 if (0 < reroll_count
) {
1778 struct strbuf sprefix
= STRBUF_INIT
;
1779 strbuf_addf(&sprefix
, "%s v%d",
1780 rev
.subject_prefix
, reroll_count
);
1781 rev
.reroll_count
= reroll_count
;
1782 rev
.subject_prefix
= strbuf_detach(&sprefix
, NULL
);
1785 for (i
= 0; i
< extra_hdr
.nr
; i
++) {
1786 strbuf_addstr(&buf
, extra_hdr
.items
[i
].string
);
1787 strbuf_addch(&buf
, '\n');
1791 strbuf_addstr(&buf
, "To: ");
1792 for (i
= 0; i
< extra_to
.nr
; i
++) {
1794 strbuf_addstr(&buf
, " ");
1795 strbuf_addstr(&buf
, extra_to
.items
[i
].string
);
1796 if (i
+ 1 < extra_to
.nr
)
1797 strbuf_addch(&buf
, ',');
1798 strbuf_addch(&buf
, '\n');
1802 strbuf_addstr(&buf
, "Cc: ");
1803 for (i
= 0; i
< extra_cc
.nr
; i
++) {
1805 strbuf_addstr(&buf
, " ");
1806 strbuf_addstr(&buf
, extra_cc
.items
[i
].string
);
1807 if (i
+ 1 < extra_cc
.nr
)
1808 strbuf_addch(&buf
, ',');
1809 strbuf_addch(&buf
, '\n');
1812 rev
.extra_headers
= strbuf_detach(&buf
, NULL
);
1815 if (split_ident_line(&rev
.from_ident
, from
, strlen(from
)))
1816 die(_("invalid ident line: %s"), from
);
1819 if (start_number
< 0)
1823 * If numbered is set solely due to format.numbered in config,
1824 * and it would conflict with --keep-subject (-k) from the
1825 * command line, reset "numbered".
1827 if (numbered
&& keep_subject
&& !numbered_cmdline_opt
)
1830 if (numbered
&& keep_subject
)
1831 die(_("-n and -k are mutually exclusive"));
1832 if (keep_subject
&& subject_prefix
)
1833 die(_("--subject-prefix/--rfc and -k are mutually exclusive"));
1834 rev
.preserve_subject
= keep_subject
;
1836 argc
= setup_revisions(argc
, argv
, &rev
, &s_r_opt
);
1838 die(_("unrecognized argument: %s"), argv
[1]);
1840 if (rev
.diffopt
.output_format
& DIFF_FORMAT_NAME
)
1841 die(_("--name-only does not make sense"));
1842 if (rev
.diffopt
.output_format
& DIFF_FORMAT_NAME_STATUS
)
1843 die(_("--name-status does not make sense"));
1844 if (rev
.diffopt
.output_format
& DIFF_FORMAT_CHECKDIFF
)
1845 die(_("--check does not make sense"));
1847 if (!use_patch_format
&&
1848 (!rev
.diffopt
.output_format
||
1849 rev
.diffopt
.output_format
== DIFF_FORMAT_PATCH
))
1850 rev
.diffopt
.output_format
= DIFF_FORMAT_DIFFSTAT
| DIFF_FORMAT_SUMMARY
;
1851 if (!rev
.diffopt
.stat_width
)
1852 rev
.diffopt
.stat_width
= MAIL_DEFAULT_WRAP
;
1854 /* Always generate a patch */
1855 rev
.diffopt
.output_format
|= DIFF_FORMAT_PATCH
;
1857 rev
.zero_commit
= zero_commit
;
1859 if (!rev
.diffopt
.flags
.text
&& !no_binary_diff
)
1860 rev
.diffopt
.flags
.binary
= 1;
1863 load_display_notes(&rev
.notes_opt
);
1865 if (!output_directory
&& !use_stdout
)
1866 output_directory
= config_output_directory
;
1869 output_directory
= set_outdir(prefix
, output_directory
);
1873 if (output_directory
) {
1875 if (rev
.diffopt
.use_color
!= GIT_COLOR_ALWAYS
)
1876 rev
.diffopt
.use_color
= GIT_COLOR_NEVER
;
1878 die(_("standard output, or directory, which one?"));
1880 * We consider <outdir> as 'outside of gitdir', therefore avoid
1881 * applying adjust_shared_perm in s-c-l-d.
1883 saved
= get_shared_repository();
1884 set_shared_repository(0);
1885 switch (safe_create_leading_directories_const(output_directory
)) {
1890 die(_("could not create leading directories "
1891 "of '%s'"), output_directory
);
1893 set_shared_repository(saved
);
1894 if (mkdir(output_directory
, 0777) < 0 && errno
!= EEXIST
)
1895 die_errno(_("could not create directory '%s'"),
1899 if (rev
.pending
.nr
== 1) {
1902 if (rev
.max_count
< 0 && !rev
.show_root_diff
) {
1904 * This is traditional behaviour of "git format-patch
1905 * origin" that prepares what the origin side still
1908 rev
.pending
.objects
[0].item
->flags
|= UNINTERESTING
;
1909 add_head_to_pending(&rev
);
1913 * Otherwise, it is "format-patch -22 HEAD", and/or
1914 * "format-patch --root HEAD". The user wants
1915 * get_revision() to do the usual traversal.
1918 if (!strcmp(rev
.pending
.objects
[0].name
, "HEAD"))
1922 const char *ref
, *v
;
1923 ref
= resolve_ref_unsafe("HEAD", RESOLVE_REF_READING
,
1925 if (ref
&& skip_prefix(ref
, "refs/heads/", &v
))
1926 branch_name
= xstrdup(v
);
1928 branch_name
= xstrdup(""); /* no branch */
1933 * We cannot move this anywhere earlier because we do want to
1934 * know if --root was given explicitly from the command line.
1936 rev
.show_root_diff
= 1;
1938 if (ignore_if_in_upstream
) {
1939 /* Don't say anything if head and upstream are the same. */
1940 if (rev
.pending
.nr
== 2) {
1941 struct object_array_entry
*o
= rev
.pending
.objects
;
1942 if (oideq(&o
[0].item
->oid
, &o
[1].item
->oid
))
1945 get_patch_ids(&rev
, &ids
);
1948 if (prepare_revision_walk(&rev
))
1949 die(_("revision walk setup failed"));
1951 while ((commit
= get_revision(&rev
)) != NULL
) {
1952 if (commit
->object
.flags
& BOUNDARY
) {
1954 origin
= (boundary_count
== 1) ? commit
: NULL
;
1958 if (ignore_if_in_upstream
&& has_commit_patch_id(commit
, &ids
))
1962 REALLOC_ARRAY(list
, nr
);
1963 list
[nr
- 1] = commit
;
1969 if (cover_letter
== -1) {
1970 if (config_cover_letter
== COVER_AUTO
)
1971 cover_letter
= (total
> 1);
1973 cover_letter
= (config_cover_letter
== COVER_ON
);
1975 if (!keep_subject
&& auto_number
&& (total
> 1 || cover_letter
))
1978 rev
.total
= total
+ start_number
- 1;
1980 if (idiff_prev
.nr
) {
1981 if (!cover_letter
&& total
!= 1)
1982 die(_("--interdiff requires --cover-letter or single patch"));
1983 rev
.idiff_oid1
= &idiff_prev
.oid
[idiff_prev
.nr
- 1];
1984 rev
.idiff_oid2
= get_commit_tree_oid(list
[0]);
1985 rev
.idiff_title
= diff_title(&idiff_title
, reroll_count
,
1987 _("Interdiff against v%d:"));
1990 if (creation_factor
< 0)
1991 creation_factor
= RANGE_DIFF_CREATION_FACTOR_DEFAULT
;
1992 else if (!rdiff_prev
)
1993 die(_("--creation-factor requires --range-diff"));
1996 if (!cover_letter
&& total
!= 1)
1997 die(_("--range-diff requires --cover-letter or single patch"));
1999 infer_range_diff_ranges(&rdiff1
, &rdiff2
, rdiff_prev
,
2001 rev
.rdiff1
= rdiff1
.buf
;
2002 rev
.rdiff2
= rdiff2
.buf
;
2003 rev
.creation_factor
= creation_factor
;
2004 rev
.rdiff_title
= diff_title(&rdiff_title
, reroll_count
,
2006 _("Range-diff against v%d:"));
2010 ; /* --no-signature inhibits all signatures */
2011 } else if (signature
&& signature
!= git_version_string
) {
2012 ; /* non-default signature already set */
2013 } else if (signature_file
) {
2014 struct strbuf buf
= STRBUF_INIT
;
2016 if (strbuf_read_file(&buf
, signature_file
, 128) < 0)
2017 die_errno(_("unable to read signature file '%s'"), signature_file
);
2018 signature
= strbuf_detach(&buf
, NULL
);
2021 memset(&bases
, 0, sizeof(bases
));
2023 struct commit
*base
= get_base_commit(base_commit
, list
, nr
);
2024 reset_revision_walk();
2025 clear_object_flags(UNINTERESTING
);
2026 prepare_bases(&bases
, base
, list
, nr
);
2029 if (in_reply_to
|| thread
|| cover_letter
)
2030 rev
.ref_message_ids
= xcalloc(1, sizeof(struct string_list
));
2032 const char *msgid
= clean_message_id(in_reply_to
);
2033 string_list_append(rev
.ref_message_ids
, msgid
);
2035 rev
.numbered_files
= just_numbers
;
2036 rev
.patch_suffix
= fmt_patch_suffix
;
2039 gen_message_id(&rev
, "cover");
2040 make_cover_letter(&rev
, use_stdout
,
2041 origin
, nr
, list
, branch_name
, quiet
);
2042 print_bases(&bases
, rev
.diffopt
.file
);
2043 print_signature(rev
.diffopt
.file
);
2046 /* interdiff/range-diff in cover-letter; omit from patches */
2047 rev
.idiff_oid1
= NULL
;
2050 rev
.add_signoff
= do_signoff
;
2053 progress
= start_delayed_progress(_("Generating patches"), total
);
2056 display_progress(progress
, total
- nr
);
2058 rev
.nr
= total
- nr
+ (start_number
- 1);
2059 /* Make the second and subsequent mails replies to the first */
2061 /* Have we already had a message ID? */
2062 if (rev
.message_id
) {
2064 * For deep threading: make every mail
2065 * a reply to the previous one, no
2066 * matter what other options are set.
2068 * For shallow threading:
2070 * Without --cover-letter and
2071 * --in-reply-to, make every mail a
2072 * reply to the one before.
2074 * With --in-reply-to but no
2075 * --cover-letter, make every mail a
2076 * reply to the <reply-to>.
2078 * With --cover-letter, make every
2079 * mail but the cover letter a reply
2080 * to the cover letter. The cover
2081 * letter is a reply to the
2082 * --in-reply-to, if specified.
2084 if (thread
== THREAD_SHALLOW
2085 && rev
.ref_message_ids
->nr
> 0
2086 && (!cover_letter
|| rev
.nr
> 1))
2087 free(rev
.message_id
);
2089 string_list_append(rev
.ref_message_ids
,
2092 gen_message_id(&rev
, oid_to_hex(&commit
->object
.oid
));
2096 open_next_file(rev
.numbered_files
? NULL
: commit
, NULL
, &rev
, quiet
))
2097 die(_("failed to create output files"));
2098 shown
= log_tree_commit(&rev
, commit
);
2099 free_commit_buffer(the_repository
->parsed_objects
,
2102 /* We put one extra blank line between formatted
2103 * patches and this flag is used by log-tree code
2104 * to see if it needs to emit a LF before showing
2105 * the log; when using one file per patch, we do
2106 * not want the extra blank line.
2111 print_bases(&bases
, rev
.diffopt
.file
);
2112 if (rev
.mime_boundary
)
2113 fprintf(rev
.diffopt
.file
, "\n--%s%s--\n\n\n",
2114 mime_boundary_leader
,
2117 print_signature(rev
.diffopt
.file
);
2120 fclose(rev
.diffopt
.file
);
2122 stop_progress(&progress
);
2125 string_list_clear(&extra_to
, 0);
2126 string_list_clear(&extra_cc
, 0);
2127 string_list_clear(&extra_hdr
, 0);
2128 if (ignore_if_in_upstream
)
2129 free_patch_ids(&ids
);
2132 oid_array_clear(&idiff_prev
);
2133 strbuf_release(&idiff_title
);
2134 strbuf_release(&rdiff1
);
2135 strbuf_release(&rdiff2
);
2136 strbuf_release(&rdiff_title
);
2140 static int add_pending_commit(const char *arg
, struct rev_info
*revs
, int flags
)
2142 struct object_id oid
;
2143 if (get_oid(arg
, &oid
) == 0) {
2144 struct commit
*commit
= lookup_commit_reference(the_repository
,
2147 commit
->object
.flags
|= flags
;
2148 add_pending_object(revs
, &commit
->object
, arg
);
2155 static const char * const cherry_usage
[] = {
2156 N_("git cherry [-v] [<upstream> [<head> [<limit>]]]"),
2160 static void print_commit(char sign
, struct commit
*commit
, int verbose
,
2161 int abbrev
, FILE *file
)
2164 fprintf(file
, "%c %s\n", sign
,
2165 find_unique_abbrev(&commit
->object
.oid
, abbrev
));
2167 struct strbuf buf
= STRBUF_INIT
;
2168 pp_commit_easy(CMIT_FMT_ONELINE
, commit
, &buf
);
2169 fprintf(file
, "%c %s %s\n", sign
,
2170 find_unique_abbrev(&commit
->object
.oid
, abbrev
),
2172 strbuf_release(&buf
);
2176 int cmd_cherry(int argc
, const char **argv
, const char *prefix
)
2178 struct rev_info revs
;
2179 struct patch_ids ids
;
2180 struct commit
*commit
;
2181 struct commit_list
*list
= NULL
;
2182 struct branch
*current_branch
;
2183 const char *upstream
;
2184 const char *head
= "HEAD";
2185 const char *limit
= NULL
;
2186 int verbose
= 0, abbrev
= 0;
2188 struct option options
[] = {
2189 OPT__ABBREV(&abbrev
),
2190 OPT__VERBOSE(&verbose
, N_("be verbose")),
2194 argc
= parse_options(argc
, argv
, prefix
, options
, cherry_usage
, 0);
2207 current_branch
= branch_get(NULL
);
2208 upstream
= branch_get_upstream(current_branch
, NULL
);
2210 fprintf(stderr
, _("Could not find a tracked"
2211 " remote branch, please"
2212 " specify <upstream> manually.\n"));
2213 usage_with_options(cherry_usage
, options
);
2217 repo_init_revisions(the_repository
, &revs
, prefix
);
2218 revs
.max_parents
= 1;
2220 if (add_pending_commit(head
, &revs
, 0))
2221 die(_("unknown commit %s"), head
);
2222 if (add_pending_commit(upstream
, &revs
, UNINTERESTING
))
2223 die(_("unknown commit %s"), upstream
);
2225 /* Don't say anything if head and upstream are the same. */
2226 if (revs
.pending
.nr
== 2) {
2227 struct object_array_entry
*o
= revs
.pending
.objects
;
2228 if (oideq(&o
[0].item
->oid
, &o
[1].item
->oid
))
2232 get_patch_ids(&revs
, &ids
);
2234 if (limit
&& add_pending_commit(limit
, &revs
, UNINTERESTING
))
2235 die(_("unknown commit %s"), limit
);
2237 /* reverse the list of commits */
2238 if (prepare_revision_walk(&revs
))
2239 die(_("revision walk setup failed"));
2240 while ((commit
= get_revision(&revs
)) != NULL
) {
2241 commit_list_insert(commit
, &list
);
2247 commit
= list
->item
;
2248 if (has_commit_patch_id(commit
, &ids
))
2250 print_commit(sign
, commit
, verbose
, abbrev
, revs
.diffopt
.file
);
2254 free_patch_ids(&ids
);