2 * Builtin "git log" and related commands (show, whatchanged)
4 * (C) Copyright 2006 Linus Torvalds
17 #include "reflog-walk.h"
18 #include "patch-ids.h"
19 #include "run-command.h"
22 #include "string-list.h"
23 #include "parse-options.h"
26 #include "streaming.h"
29 #include "gpg-interface.h"
32 /* Set a default date-time format for git log ("log.date" config variable) */
33 static const char *default_date_mode
= NULL
;
35 static int default_abbrev_commit
;
36 static int default_show_root
= 1;
37 static int default_follow
;
38 static int default_show_signature
;
39 static int decoration_style
;
40 static int decoration_given
;
41 static int use_mailmap_config
;
42 static const char *fmt_patch_subject_prefix
= "PATCH";
43 static const char *fmt_pretty
;
45 static const char * const builtin_log_usage
[] = {
46 N_("git log [<options>] [<revision-range>] [[--] <path>...]"),
47 N_("git show [<options>] <object>..."),
51 struct line_opt_callback_data
{
54 struct string_list args
;
57 static int auto_decoration_style(void)
59 return (isatty(1) || pager_in_use()) ? DECORATE_SHORT_REFS
: 0;
62 static int parse_decoration_style(const char *value
)
64 switch (git_parse_maybe_bool(value
)) {
66 return DECORATE_SHORT_REFS
;
72 if (!strcmp(value
, "full"))
73 return DECORATE_FULL_REFS
;
74 else if (!strcmp(value
, "short"))
75 return DECORATE_SHORT_REFS
;
76 else if (!strcmp(value
, "auto"))
77 return auto_decoration_style();
81 static int decorate_callback(const struct option
*opt
, const char *arg
, int unset
)
86 decoration_style
= parse_decoration_style(arg
);
88 decoration_style
= DECORATE_SHORT_REFS
;
90 if (decoration_style
< 0)
91 die(_("invalid --decorate option: %s"), arg
);
98 static int log_line_range_callback(const struct option
*option
, const char *arg
, int unset
)
100 struct line_opt_callback_data
*data
= option
->value
;
105 data
->rev
->line_level_traverse
= 1;
106 string_list_append(&data
->args
, arg
);
111 static void init_log_defaults(void)
113 init_grep_defaults();
114 init_diff_ui_defaults();
116 decoration_style
= auto_decoration_style();
119 static void cmd_log_init_defaults(struct rev_info
*rev
)
122 get_commit_format(fmt_pretty
, rev
);
124 DIFF_OPT_SET(&rev
->diffopt
, DEFAULT_FOLLOW_RENAMES
);
125 rev
->verbose_header
= 1;
126 DIFF_OPT_SET(&rev
->diffopt
, RECURSIVE
);
127 rev
->diffopt
.stat_width
= -1; /* use full terminal width */
128 rev
->diffopt
.stat_graph_width
= -1; /* respect statGraphWidth config */
129 rev
->abbrev_commit
= default_abbrev_commit
;
130 rev
->show_root_diff
= default_show_root
;
131 rev
->subject_prefix
= fmt_patch_subject_prefix
;
132 rev
->show_signature
= default_show_signature
;
133 DIFF_OPT_SET(&rev
->diffopt
, ALLOW_TEXTCONV
);
135 if (default_date_mode
)
136 parse_date_format(default_date_mode
, &rev
->date_mode
);
138 memset(&rev
->diffopt
.touched_flags
, 0, sizeof(struct diff_flags
));
141 static void cmd_log_init_finish(int argc
, const char **argv
, const char *prefix
,
142 struct rev_info
*rev
, struct setup_revision_opt
*opt
)
144 struct userformat_want w
;
145 int quiet
= 0, source
= 0, mailmap
= 0;
146 static struct line_opt_callback_data line_cb
= {NULL
, NULL
, STRING_LIST_INIT_DUP
};
148 const struct option builtin_log_options
[] = {
149 OPT__QUIET(&quiet
, N_("suppress diff output")),
150 OPT_BOOL(0, "source", &source
, N_("show source")),
151 OPT_BOOL(0, "use-mailmap", &mailmap
, N_("Use mail map file")),
152 { OPTION_CALLBACK
, 0, "decorate", NULL
, NULL
, N_("decorate options"),
153 PARSE_OPT_OPTARG
, decorate_callback
},
154 OPT_CALLBACK('L', NULL
, &line_cb
, "n,m:file",
155 N_("Process line range n,m in file, counting from 1"),
156 log_line_range_callback
),
161 line_cb
.prefix
= prefix
;
163 mailmap
= use_mailmap_config
;
164 argc
= parse_options(argc
, argv
, prefix
,
165 builtin_log_options
, builtin_log_usage
,
166 PARSE_OPT_KEEP_ARGV0
| PARSE_OPT_KEEP_UNKNOWN
|
167 PARSE_OPT_KEEP_DASHDASH
);
170 rev
->diffopt
.output_format
|= DIFF_FORMAT_NO_OUTPUT
;
171 argc
= setup_revisions(argc
, argv
, rev
, opt
);
173 /* Any arguments at this point are not recognized */
175 die(_("unrecognized argument: %s"), argv
[1]);
177 memset(&w
, 0, sizeof(w
));
178 userformat_find_requirements(NULL
, &w
);
180 if (!rev
->show_notes_given
&& (!rev
->pretty_given
|| w
.notes
))
183 init_display_notes(&rev
->notes_opt
);
185 if (rev
->diffopt
.pickaxe
|| rev
->diffopt
.filter
||
186 DIFF_OPT_TST(&rev
->diffopt
, FOLLOW_RENAMES
))
187 rev
->always_show_header
= 0;
190 rev
->show_source
= 1;
193 rev
->mailmap
= xcalloc(1, sizeof(struct string_list
));
194 read_mailmap(rev
->mailmap
, NULL
);
197 if (rev
->pretty_given
&& rev
->commit_format
== CMIT_FMT_RAW
) {
199 * "log --pretty=raw" is special; ignore UI oriented
200 * configuration variables such as decoration.
202 if (!decoration_given
)
203 decoration_style
= 0;
204 if (!rev
->abbrev_commit_given
)
205 rev
->abbrev_commit
= 0;
208 if (decoration_style
) {
209 rev
->show_decorations
= 1;
210 load_ref_decorations(decoration_style
);
213 if (rev
->line_level_traverse
)
214 line_log_init(rev
, line_cb
.prefix
, &line_cb
.args
);
219 static void cmd_log_init(int argc
, const char **argv
, const char *prefix
,
220 struct rev_info
*rev
, struct setup_revision_opt
*opt
)
222 cmd_log_init_defaults(rev
);
223 cmd_log_init_finish(argc
, argv
, prefix
, rev
, opt
);
227 * This gives a rough estimate for how many commits we
228 * will print out in the list.
230 static int estimate_commit_count(struct rev_info
*rev
, struct commit_list
*list
)
235 struct commit
*commit
= list
->item
;
236 unsigned int flags
= commit
->object
.flags
;
238 if (!(flags
& (TREESAME
| UNINTERESTING
)))
244 static void show_early_header(struct rev_info
*rev
, const char *stage
, int nr
)
246 if (rev
->shown_one
) {
248 if (rev
->commit_format
!= CMIT_FMT_ONELINE
)
249 putchar(rev
->diffopt
.line_termination
);
251 fprintf(rev
->diffopt
.file
, _("Final output: %d %s\n"), nr
, stage
);
254 static struct itimerval early_output_timer
;
256 static void log_show_early(struct rev_info
*revs
, struct commit_list
*list
)
258 int i
= revs
->early_output
, close_file
= revs
->diffopt
.close_file
;
261 revs
->diffopt
.close_file
= 0;
262 sort_in_topological_order(&list
, revs
->sort_order
);
264 struct commit
*commit
= list
->item
;
265 switch (simplify_commit(revs
, commit
)) {
268 int n
= estimate_commit_count(revs
, list
);
269 show_early_header(revs
, "incomplete", n
);
272 log_tree_commit(revs
, commit
);
279 fclose(revs
->diffopt
.file
);
285 /* Did we already get enough commits for the early output? */
288 fclose(revs
->diffopt
.file
);
293 * ..if no, then repeat it twice a second until we
296 * NOTE! We don't use "it_interval", because if the
297 * reader isn't listening, we want our output to be
298 * throttled by the writing, and not have the timer
299 * trigger every second even if we're blocked on a
302 early_output_timer
.it_value
.tv_sec
= 0;
303 early_output_timer
.it_value
.tv_usec
= 500000;
304 setitimer(ITIMER_REAL
, &early_output_timer
, NULL
);
307 static void early_output(int signal
)
309 show_early_output
= log_show_early
;
312 static void setup_early_output(struct rev_info
*rev
)
317 * Set up the signal handler, minimally intrusively:
318 * we only set a single volatile integer word (not
319 * using sigatomic_t - trying to avoid unnecessary
320 * system dependencies and headers), and using
323 memset(&sa
, 0, sizeof(sa
));
324 sa
.sa_handler
= early_output
;
325 sigemptyset(&sa
.sa_mask
);
326 sa
.sa_flags
= SA_RESTART
;
327 sigaction(SIGALRM
, &sa
, NULL
);
330 * If we can get the whole output in less than a
331 * tenth of a second, don't even bother doing the
332 * early-output thing..
334 * This is a one-time-only trigger.
336 early_output_timer
.it_value
.tv_sec
= 0;
337 early_output_timer
.it_value
.tv_usec
= 100000;
338 setitimer(ITIMER_REAL
, &early_output_timer
, NULL
);
341 static void finish_early_output(struct rev_info
*rev
)
343 int n
= estimate_commit_count(rev
, rev
->commits
);
344 signal(SIGALRM
, SIG_IGN
);
345 show_early_header(rev
, "done", n
);
348 static int cmd_log_walk(struct rev_info
*rev
)
350 struct commit
*commit
;
352 int saved_dcctc
= 0, close_file
= rev
->diffopt
.close_file
;
354 if (rev
->early_output
)
355 setup_early_output(rev
);
357 if (prepare_revision_walk(rev
))
358 die(_("revision walk setup failed"));
360 if (rev
->early_output
)
361 finish_early_output(rev
);
364 * For --check and --exit-code, the exit code is based on CHECK_FAILED
365 * and HAS_CHANGES being accumulated in rev->diffopt, so be careful to
366 * retain that state information if replacing rev->diffopt in this loop
368 rev
->diffopt
.close_file
= 0;
369 while ((commit
= get_revision(rev
)) != NULL
) {
370 if (!log_tree_commit(rev
, commit
) && rev
->max_count
>= 0)
372 * We decremented max_count in get_revision,
373 * but we didn't actually show the commit.
376 if (!rev
->reflog_info
) {
378 * We may show a given commit multiple times when
379 * walking the reflogs.
381 free_commit_buffer(commit
);
382 free_commit_list(commit
->parents
);
383 commit
->parents
= NULL
;
385 if (saved_nrl
< rev
->diffopt
.needed_rename_limit
)
386 saved_nrl
= rev
->diffopt
.needed_rename_limit
;
387 if (rev
->diffopt
.degraded_cc_to_c
)
390 rev
->diffopt
.degraded_cc_to_c
= saved_dcctc
;
391 rev
->diffopt
.needed_rename_limit
= saved_nrl
;
393 fclose(rev
->diffopt
.file
);
395 if (rev
->diffopt
.output_format
& DIFF_FORMAT_CHECKDIFF
&&
396 DIFF_OPT_TST(&rev
->diffopt
, CHECK_FAILED
)) {
399 return diff_result_code(&rev
->diffopt
, 0);
402 static int git_log_config(const char *var
, const char *value
, void *cb
)
404 const char *slot_name
;
406 if (!strcmp(var
, "format.pretty"))
407 return git_config_string(&fmt_pretty
, var
, value
);
408 if (!strcmp(var
, "format.subjectprefix"))
409 return git_config_string(&fmt_patch_subject_prefix
, var
, value
);
410 if (!strcmp(var
, "log.abbrevcommit")) {
411 default_abbrev_commit
= git_config_bool(var
, value
);
414 if (!strcmp(var
, "log.date"))
415 return git_config_string(&default_date_mode
, var
, value
);
416 if (!strcmp(var
, "log.decorate")) {
417 decoration_style
= parse_decoration_style(value
);
418 if (decoration_style
< 0)
419 decoration_style
= 0; /* maybe warn? */
422 if (!strcmp(var
, "log.showroot")) {
423 default_show_root
= git_config_bool(var
, value
);
426 if (!strcmp(var
, "log.follow")) {
427 default_follow
= git_config_bool(var
, value
);
430 if (skip_prefix(var
, "color.decorate.", &slot_name
))
431 return parse_decorate_color_config(var
, slot_name
, value
);
432 if (!strcmp(var
, "log.mailmap")) {
433 use_mailmap_config
= git_config_bool(var
, value
);
436 if (!strcmp(var
, "log.showsignature")) {
437 default_show_signature
= git_config_bool(var
, value
);
441 if (grep_config(var
, value
, cb
) < 0)
443 if (git_gpg_config(var
, value
, cb
) < 0)
445 return git_diff_ui_config(var
, value
, cb
);
448 int cmd_whatchanged(int argc
, const char **argv
, const char *prefix
)
451 struct setup_revision_opt opt
;
454 git_config(git_log_config
, NULL
);
456 init_revisions(&rev
, prefix
);
458 rev
.simplify_history
= 0;
459 memset(&opt
, 0, sizeof(opt
));
461 opt
.revarg_opt
= REVARG_COMMITTISH
;
462 cmd_log_init(argc
, argv
, prefix
, &rev
, &opt
);
463 if (!rev
.diffopt
.output_format
)
464 rev
.diffopt
.output_format
= DIFF_FORMAT_RAW
;
465 return cmd_log_walk(&rev
);
468 static void show_tagger(char *buf
, int len
, struct rev_info
*rev
)
470 struct strbuf out
= STRBUF_INIT
;
471 struct pretty_print_context pp
= {0};
473 pp
.fmt
= rev
->commit_format
;
474 pp
.date_mode
= rev
->date_mode
;
475 pp_user_info(&pp
, "Tagger", &out
, buf
, get_log_output_encoding());
476 fprintf(rev
->diffopt
.file
, "%s", out
.buf
);
477 strbuf_release(&out
);
480 static int show_blob_object(const struct object_id
*oid
, struct rev_info
*rev
, const char *obj_name
)
482 struct object_id oidc
;
483 struct object_context obj_context
;
487 fflush(rev
->diffopt
.file
);
488 if (!DIFF_OPT_TOUCHED(&rev
->diffopt
, ALLOW_TEXTCONV
) ||
489 !DIFF_OPT_TST(&rev
->diffopt
, ALLOW_TEXTCONV
))
490 return stream_blob_to_fd(1, oid
, NULL
, 0);
492 if (get_oid_with_context(obj_name
, GET_OID_RECORD_PATH
,
493 &oidc
, &obj_context
))
494 die(_("Not a valid object name %s"), obj_name
);
495 if (!obj_context
.path
||
496 !textconv_object(obj_context
.path
, obj_context
.mode
, &oidc
, 1, &buf
, &size
)) {
497 free(obj_context
.path
);
498 return stream_blob_to_fd(1, oid
, NULL
, 0);
502 die(_("git show %s: bad file"), obj_name
);
504 write_or_die(1, buf
, size
);
505 free(obj_context
.path
);
509 static int show_tag_object(const struct object_id
*oid
, struct rev_info
*rev
)
512 enum object_type type
;
513 char *buf
= read_sha1_file(oid
->hash
, &type
, &size
);
517 return error(_("Could not read object %s"), oid_to_hex(oid
));
519 assert(type
== OBJ_TAG
);
520 while (offset
< size
&& buf
[offset
] != '\n') {
521 int new_offset
= offset
+ 1;
522 while (new_offset
< size
&& buf
[new_offset
++] != '\n')
524 if (starts_with(buf
+ offset
, "tagger "))
525 show_tagger(buf
+ offset
+ 7,
526 new_offset
- offset
- 7, rev
);
531 fwrite(buf
+ offset
, size
- offset
, 1, rev
->diffopt
.file
);
536 static int show_tree_object(const unsigned char *sha1
,
538 const char *pathname
, unsigned mode
, int stage
, void *context
)
540 FILE *file
= context
;
541 fprintf(file
, "%s%s\n", pathname
, S_ISDIR(mode
) ? "/" : "");
545 static void show_setup_revisions_tweak(struct rev_info
*rev
,
546 struct setup_revision_opt
*opt
)
548 if (rev
->ignore_merges
) {
549 /* There was no "-m" on the command line */
550 rev
->ignore_merges
= 0;
551 if (!rev
->first_parent_only
&& !rev
->combine_merges
) {
552 /* No "--first-parent", "-c", or "--cc" */
553 rev
->combine_merges
= 1;
554 rev
->dense_combined_merges
= 1;
557 if (!rev
->diffopt
.output_format
)
558 rev
->diffopt
.output_format
= DIFF_FORMAT_PATCH
;
561 int cmd_show(int argc
, const char **argv
, const char *prefix
)
564 struct object_array_entry
*objects
;
565 struct setup_revision_opt opt
;
566 struct pathspec match_all
;
567 int i
, count
, ret
= 0;
570 git_config(git_log_config
, NULL
);
572 memset(&match_all
, 0, sizeof(match_all
));
573 init_revisions(&rev
, prefix
);
575 rev
.always_show_header
= 1;
576 rev
.no_walk
= REVISION_WALK_NO_WALK_SORTED
;
577 rev
.diffopt
.stat_width
= -1; /* Scale to real terminal size */
579 memset(&opt
, 0, sizeof(opt
));
581 opt
.tweak
= show_setup_revisions_tweak
;
582 cmd_log_init(argc
, argv
, prefix
, &rev
, &opt
);
585 return cmd_log_walk(&rev
);
587 count
= rev
.pending
.nr
;
588 objects
= rev
.pending
.objects
;
589 for (i
= 0; i
< count
&& !ret
; i
++) {
590 struct object
*o
= objects
[i
].item
;
591 const char *name
= objects
[i
].name
;
594 ret
= show_blob_object(&o
->oid
, &rev
, name
);
597 struct tag
*t
= (struct tag
*)o
;
601 fprintf(rev
.diffopt
.file
, "%stag %s%s\n",
602 diff_get_color_opt(&rev
.diffopt
, DIFF_COMMIT
),
604 diff_get_color_opt(&rev
.diffopt
, DIFF_RESET
));
605 ret
= show_tag_object(&o
->oid
, &rev
);
609 o
= parse_object(&t
->tagged
->oid
);
611 ret
= error(_("Could not read object %s"),
612 oid_to_hex(&t
->tagged
->oid
));
620 fprintf(rev
.diffopt
.file
, "%stree %s%s\n\n",
621 diff_get_color_opt(&rev
.diffopt
, DIFF_COMMIT
),
623 diff_get_color_opt(&rev
.diffopt
, DIFF_RESET
));
624 read_tree_recursive((struct tree
*)o
, "", 0, 0, &match_all
,
625 show_tree_object
, rev
.diffopt
.file
);
629 rev
.pending
.nr
= rev
.pending
.alloc
= 0;
630 rev
.pending
.objects
= NULL
;
631 add_object_array(o
, name
, &rev
.pending
);
632 ret
= cmd_log_walk(&rev
);
635 ret
= error(_("Unknown type: %d"), o
->type
);
643 * This is equivalent to "git log -g --abbrev-commit --pretty=oneline"
645 int cmd_log_reflog(int argc
, const char **argv
, const char *prefix
)
648 struct setup_revision_opt opt
;
651 git_config(git_log_config
, NULL
);
653 init_revisions(&rev
, prefix
);
654 init_reflog_walk(&rev
.reflog_info
);
655 rev
.verbose_header
= 1;
656 memset(&opt
, 0, sizeof(opt
));
658 cmd_log_init_defaults(&rev
);
659 rev
.abbrev_commit
= 1;
660 rev
.commit_format
= CMIT_FMT_ONELINE
;
661 rev
.use_terminator
= 1;
662 rev
.always_show_header
= 1;
663 cmd_log_init_finish(argc
, argv
, prefix
, &rev
, &opt
);
665 return cmd_log_walk(&rev
);
668 static void log_setup_revisions_tweak(struct rev_info
*rev
,
669 struct setup_revision_opt
*opt
)
671 if (DIFF_OPT_TST(&rev
->diffopt
, DEFAULT_FOLLOW_RENAMES
) &&
672 rev
->prune_data
.nr
== 1)
673 DIFF_OPT_SET(&rev
->diffopt
, FOLLOW_RENAMES
);
675 /* Turn --cc/-c into -p --cc/-c when -p was not given */
676 if (!rev
->diffopt
.output_format
&& rev
->combine_merges
)
677 rev
->diffopt
.output_format
= DIFF_FORMAT_PATCH
;
679 /* Turn -m on when --cc/-c was given */
680 if (rev
->combine_merges
)
681 rev
->ignore_merges
= 0;
684 int cmd_log(int argc
, const char **argv
, const char *prefix
)
687 struct setup_revision_opt opt
;
690 git_config(git_log_config
, NULL
);
692 init_revisions(&rev
, prefix
);
693 rev
.always_show_header
= 1;
694 memset(&opt
, 0, sizeof(opt
));
696 opt
.revarg_opt
= REVARG_COMMITTISH
;
697 opt
.tweak
= log_setup_revisions_tweak
;
698 cmd_log_init(argc
, argv
, prefix
, &rev
, &opt
);
699 return cmd_log_walk(&rev
);
704 static const char *fmt_patch_suffix
= ".patch";
705 static int numbered
= 0;
706 static int auto_number
= 1;
708 static char *default_attach
= NULL
;
710 static struct string_list extra_hdr
= STRING_LIST_INIT_NODUP
;
711 static struct string_list extra_to
= STRING_LIST_INIT_NODUP
;
712 static struct string_list extra_cc
= STRING_LIST_INIT_NODUP
;
714 static void add_header(const char *value
)
716 struct string_list_item
*item
;
717 int len
= strlen(value
);
718 while (len
&& value
[len
- 1] == '\n')
721 if (!strncasecmp(value
, "to: ", 4)) {
722 item
= string_list_append(&extra_to
, value
+ 4);
724 } else if (!strncasecmp(value
, "cc: ", 4)) {
725 item
= string_list_append(&extra_cc
, value
+ 4);
728 item
= string_list_append(&extra_hdr
, value
);
731 item
->string
[len
] = '\0';
734 #define THREAD_SHALLOW 1
735 #define THREAD_DEEP 2
737 static int do_signoff
;
738 static int base_auto
;
740 static const char *signature
= git_version_string
;
741 static const char *signature_file
;
742 static int config_cover_letter
;
743 static const char *config_output_directory
;
752 static int git_format_config(const char *var
, const char *value
, void *cb
)
754 if (!strcmp(var
, "format.headers")) {
756 die(_("format.headers without value"));
760 if (!strcmp(var
, "format.suffix"))
761 return git_config_string(&fmt_patch_suffix
, var
, value
);
762 if (!strcmp(var
, "format.to")) {
764 return config_error_nonbool(var
);
765 string_list_append(&extra_to
, value
);
768 if (!strcmp(var
, "format.cc")) {
770 return config_error_nonbool(var
);
771 string_list_append(&extra_cc
, value
);
774 if (!strcmp(var
, "diff.color") || !strcmp(var
, "color.diff") ||
775 !strcmp(var
, "color.ui") || !strcmp(var
, "diff.submodule")) {
778 if (!strcmp(var
, "format.numbered")) {
779 if (value
&& !strcasecmp(value
, "auto")) {
783 numbered
= git_config_bool(var
, value
);
784 auto_number
= auto_number
&& numbered
;
787 if (!strcmp(var
, "format.attach")) {
789 default_attach
= xstrdup(value
);
791 default_attach
= xstrdup(git_version_string
);
794 if (!strcmp(var
, "format.thread")) {
795 if (value
&& !strcasecmp(value
, "deep")) {
796 thread
= THREAD_DEEP
;
799 if (value
&& !strcasecmp(value
, "shallow")) {
800 thread
= THREAD_SHALLOW
;
803 thread
= git_config_bool(var
, value
) && THREAD_SHALLOW
;
806 if (!strcmp(var
, "format.signoff")) {
807 do_signoff
= git_config_bool(var
, value
);
810 if (!strcmp(var
, "format.signature"))
811 return git_config_string(&signature
, var
, value
);
812 if (!strcmp(var
, "format.signaturefile"))
813 return git_config_pathname(&signature_file
, var
, value
);
814 if (!strcmp(var
, "format.coverletter")) {
815 if (value
&& !strcasecmp(value
, "auto")) {
816 config_cover_letter
= COVER_AUTO
;
819 config_cover_letter
= git_config_bool(var
, value
) ? COVER_ON
: COVER_OFF
;
822 if (!strcmp(var
, "format.outputdirectory"))
823 return git_config_string(&config_output_directory
, var
, value
);
824 if (!strcmp(var
, "format.useautobase")) {
825 base_auto
= git_config_bool(var
, value
);
828 if (!strcmp(var
, "format.from")) {
829 int b
= git_parse_maybe_bool(value
);
832 from
= xstrdup(value
);
834 from
= xstrdup(git_committer_info(IDENT_NO_DATE
));
840 return git_log_config(var
, value
, cb
);
843 static const char *output_directory
= NULL
;
844 static int outdir_offset
;
846 static int open_next_file(struct commit
*commit
, const char *subject
,
847 struct rev_info
*rev
, int quiet
)
849 struct strbuf filename
= STRBUF_INIT
;
850 int suffix_len
= strlen(rev
->patch_suffix
) + 1;
852 if (output_directory
) {
853 strbuf_addstr(&filename
, output_directory
);
855 PATH_MAX
- FORMAT_PATCH_NAME_MAX
- suffix_len
) {
856 strbuf_release(&filename
);
857 return error(_("name of output directory is too long"));
859 strbuf_complete(&filename
, '/');
862 if (rev
->numbered_files
)
863 strbuf_addf(&filename
, "%d", rev
->nr
);
865 fmt_output_commit(&filename
, commit
, rev
);
867 fmt_output_subject(&filename
, subject
, rev
);
870 printf("%s\n", filename
.buf
+ outdir_offset
);
872 if ((rev
->diffopt
.file
= fopen(filename
.buf
, "w")) == NULL
) {
873 error_errno(_("Cannot open patch file %s"), filename
.buf
);
874 strbuf_release(&filename
);
878 strbuf_release(&filename
);
882 static void get_patch_ids(struct rev_info
*rev
, struct patch_ids
*ids
)
884 struct rev_info check_rev
;
885 struct commit
*commit
, *c1
, *c2
;
886 struct object
*o1
, *o2
;
887 unsigned flags1
, flags2
;
889 if (rev
->pending
.nr
!= 2)
890 die(_("Need exactly one range."));
892 o1
= rev
->pending
.objects
[0].item
;
893 o2
= rev
->pending
.objects
[1].item
;
896 c1
= lookup_commit_reference(&o1
->oid
);
897 c2
= lookup_commit_reference(&o2
->oid
);
899 if ((flags1
& UNINTERESTING
) == (flags2
& UNINTERESTING
))
900 die(_("Not a range."));
904 /* given a range a..b get all patch ids for b..a */
905 init_revisions(&check_rev
, rev
->prefix
);
906 check_rev
.max_parents
= 1;
907 o1
->flags
^= UNINTERESTING
;
908 o2
->flags
^= UNINTERESTING
;
909 add_pending_object(&check_rev
, o1
, "o1");
910 add_pending_object(&check_rev
, o2
, "o2");
911 if (prepare_revision_walk(&check_rev
))
912 die(_("revision walk setup failed"));
914 while ((commit
= get_revision(&check_rev
)) != NULL
) {
915 add_commit_patch_id(commit
, ids
);
918 /* reset for next revision walk */
919 clear_commit_marks(c1
, SEEN
| UNINTERESTING
| SHOWN
| ADDED
);
920 clear_commit_marks(c2
, SEEN
| UNINTERESTING
| SHOWN
| ADDED
);
925 static void gen_message_id(struct rev_info
*info
, char *base
)
927 struct strbuf buf
= STRBUF_INIT
;
928 strbuf_addf(&buf
, "%s.%"PRItime
".git.%s", base
,
929 (timestamp_t
) time(NULL
),
930 git_committer_info(IDENT_NO_NAME
|IDENT_NO_DATE
|IDENT_STRICT
));
931 info
->message_id
= strbuf_detach(&buf
, NULL
);
934 static void print_signature(FILE *file
)
936 if (!signature
|| !*signature
)
939 fprintf(file
, "-- \n%s", signature
);
940 if (signature
[strlen(signature
)-1] != '\n')
945 static void add_branch_description(struct strbuf
*buf
, const char *branch_name
)
947 struct strbuf desc
= STRBUF_INIT
;
948 if (!branch_name
|| !*branch_name
)
950 read_branch_desc(&desc
, branch_name
);
952 strbuf_addch(buf
, '\n');
953 strbuf_addbuf(buf
, &desc
);
954 strbuf_addch(buf
, '\n');
956 strbuf_release(&desc
);
959 static char *find_branch_name(struct rev_info
*rev
)
961 int i
, positive
= -1;
962 struct object_id branch_oid
;
963 const struct object_id
*tip_oid
;
965 char *full_ref
, *branch
= NULL
;
967 for (i
= 0; i
< rev
->cmdline
.nr
; i
++) {
968 if (rev
->cmdline
.rev
[i
].flags
& UNINTERESTING
)
977 ref
= rev
->cmdline
.rev
[positive
].name
;
978 tip_oid
= &rev
->cmdline
.rev
[positive
].item
->oid
;
979 if (dwim_ref(ref
, strlen(ref
), branch_oid
.hash
, &full_ref
) &&
980 skip_prefix(full_ref
, "refs/heads/", &v
) &&
981 !oidcmp(tip_oid
, &branch_oid
))
987 static void make_cover_letter(struct rev_info
*rev
, int use_stdout
,
988 struct commit
*origin
,
989 int nr
, struct commit
**list
,
990 const char *branch_name
,
993 const char *committer
;
994 const char *body
= "*** SUBJECT HERE ***\n\n*** BLURB HERE ***\n";
997 struct strbuf sb
= STRBUF_INIT
;
999 const char *encoding
= "UTF-8";
1000 struct diff_options opts
;
1001 int need_8bit_cte
= 0;
1002 struct pretty_print_context pp
= {0};
1003 struct commit
*head
= list
[0];
1005 if (!cmit_fmt_is_mail(rev
->commit_format
))
1006 die(_("Cover letter needs email format"));
1008 committer
= git_committer_info(0);
1011 open_next_file(NULL
, rev
->numbered_files
? NULL
: "cover-letter", rev
, quiet
))
1014 log_write_email_headers(rev
, head
, &pp
.after_subject
, &need_8bit_cte
);
1016 for (i
= 0; !need_8bit_cte
&& i
< nr
; i
++) {
1017 const char *buf
= get_commit_buffer(list
[i
], NULL
);
1018 if (has_non_ascii(buf
))
1020 unuse_commit_buffer(list
[i
], buf
);
1024 branch_name
= find_branch_name(rev
);
1027 pp
.fmt
= CMIT_FMT_EMAIL
;
1028 pp
.date_mode
.type
= DATE_RFC2822
;
1030 pp
.print_email_subject
= 1;
1031 pp_user_info(&pp
, NULL
, &sb
, committer
, encoding
);
1032 pp_title_line(&pp
, &msg
, &sb
, encoding
, need_8bit_cte
);
1033 pp_remainder(&pp
, &msg
, &sb
, 0);
1034 add_branch_description(&sb
, branch_name
);
1035 fprintf(rev
->diffopt
.file
, "%s\n", sb
.buf
);
1037 strbuf_release(&sb
);
1039 shortlog_init(&log
);
1044 log
.file
= rev
->diffopt
.file
;
1045 for (i
= 0; i
< nr
; i
++)
1046 shortlog_add_commit(&log
, list
[i
]);
1048 shortlog_output(&log
);
1051 * We can only do diffstat with a unique reference point
1056 memcpy(&opts
, &rev
->diffopt
, sizeof(opts
));
1057 opts
.output_format
= DIFF_FORMAT_SUMMARY
| DIFF_FORMAT_DIFFSTAT
;
1059 diff_setup_done(&opts
);
1061 diff_tree_oid(&origin
->tree
->object
.oid
,
1062 &head
->tree
->object
.oid
,
1064 diffcore_std(&opts
);
1067 fprintf(rev
->diffopt
.file
, "\n");
1070 static const char *clean_message_id(const char *msg_id
)
1073 const char *a
, *z
, *m
;
1076 while ((ch
= *m
) && (isspace(ch
) || (ch
== '<')))
1081 if (!isspace(ch
) && (ch
!= '>'))
1086 die(_("insane in-reply-to: %s"), msg_id
);
1089 return xmemdupz(a
, z
- a
);
1092 static const char *set_outdir(const char *prefix
, const char *output_directory
)
1094 if (output_directory
&& is_absolute_path(output_directory
))
1095 return output_directory
;
1097 if (!prefix
|| !*prefix
) {
1098 if (output_directory
)
1099 return output_directory
;
1100 /* The user did not explicitly ask for "./" */
1105 outdir_offset
= strlen(prefix
);
1106 if (!output_directory
)
1109 return prefix_filename(prefix
, output_directory
);
1112 static const char * const builtin_format_patch_usage
[] = {
1113 N_("git format-patch [<options>] [<since> | <revision-range>]"),
1117 static int keep_subject
= 0;
1119 static int keep_callback(const struct option
*opt
, const char *arg
, int unset
)
1121 ((struct rev_info
*)opt
->value
)->total
= -1;
1126 static int subject_prefix
= 0;
1128 static int subject_prefix_callback(const struct option
*opt
, const char *arg
,
1132 ((struct rev_info
*)opt
->value
)->subject_prefix
= arg
;
1136 static int rfc_callback(const struct option
*opt
, const char *arg
, int unset
)
1138 return subject_prefix_callback(opt
, "RFC PATCH", unset
);
1141 static int numbered_cmdline_opt
= 0;
1143 static int numbered_callback(const struct option
*opt
, const char *arg
,
1146 *(int *)opt
->value
= numbered_cmdline_opt
= unset
? 0 : 1;
1152 static int no_numbered_callback(const struct option
*opt
, const char *arg
,
1155 return numbered_callback(opt
, arg
, 1);
1158 static int output_directory_callback(const struct option
*opt
, const char *arg
,
1161 const char **dir
= (const char **)opt
->value
;
1163 die(_("Two output directories?"));
1168 static int thread_callback(const struct option
*opt
, const char *arg
, int unset
)
1170 int *thread
= (int *)opt
->value
;
1173 else if (!arg
|| !strcmp(arg
, "shallow"))
1174 *thread
= THREAD_SHALLOW
;
1175 else if (!strcmp(arg
, "deep"))
1176 *thread
= THREAD_DEEP
;
1182 static int attach_callback(const struct option
*opt
, const char *arg
, int unset
)
1184 struct rev_info
*rev
= (struct rev_info
*)opt
->value
;
1186 rev
->mime_boundary
= NULL
;
1188 rev
->mime_boundary
= arg
;
1190 rev
->mime_boundary
= git_version_string
;
1191 rev
->no_inline
= unset
? 0 : 1;
1195 static int inline_callback(const struct option
*opt
, const char *arg
, int unset
)
1197 struct rev_info
*rev
= (struct rev_info
*)opt
->value
;
1199 rev
->mime_boundary
= NULL
;
1201 rev
->mime_boundary
= arg
;
1203 rev
->mime_boundary
= git_version_string
;
1208 static int header_callback(const struct option
*opt
, const char *arg
, int unset
)
1211 string_list_clear(&extra_hdr
, 0);
1212 string_list_clear(&extra_to
, 0);
1213 string_list_clear(&extra_cc
, 0);
1220 static int to_callback(const struct option
*opt
, const char *arg
, int unset
)
1223 string_list_clear(&extra_to
, 0);
1225 string_list_append(&extra_to
, arg
);
1229 static int cc_callback(const struct option
*opt
, const char *arg
, int unset
)
1232 string_list_clear(&extra_cc
, 0);
1234 string_list_append(&extra_cc
, arg
);
1238 static int from_callback(const struct option
*opt
, const char *arg
, int unset
)
1240 char **from
= opt
->value
;
1247 *from
= xstrdup(arg
);
1249 *from
= xstrdup(git_committer_info(IDENT_NO_DATE
));
1253 struct base_tree_info
{
1254 struct object_id base_commit
;
1255 int nr_patch_id
, alloc_patch_id
;
1256 struct object_id
*patch_id
;
1259 static struct commit
*get_base_commit(const char *base_commit
,
1260 struct commit
**list
,
1263 struct commit
*base
= NULL
;
1264 struct commit
**rev
;
1265 int i
= 0, rev_nr
= 0;
1267 if (base_commit
&& strcmp(base_commit
, "auto")) {
1268 base
= lookup_commit_reference_by_name(base_commit
);
1270 die(_("Unknown commit %s"), base_commit
);
1271 } else if ((base_commit
&& !strcmp(base_commit
, "auto")) || base_auto
) {
1272 struct branch
*curr_branch
= branch_get(NULL
);
1273 const char *upstream
= branch_get_upstream(curr_branch
, NULL
);
1275 struct commit_list
*base_list
;
1276 struct commit
*commit
;
1277 struct object_id oid
;
1279 if (get_oid(upstream
, &oid
))
1280 die(_("Failed to resolve '%s' as a valid ref."), upstream
);
1281 commit
= lookup_commit_or_die(&oid
, "upstream base");
1282 base_list
= get_merge_bases_many(commit
, total
, list
);
1283 /* There should be one and only one merge base. */
1284 if (!base_list
|| base_list
->next
)
1285 die(_("Could not find exact merge base."));
1286 base
= base_list
->item
;
1287 free_commit_list(base_list
);
1289 die(_("Failed to get upstream, if you want to record base commit automatically,\n"
1290 "please use git branch --set-upstream-to to track a remote branch.\n"
1291 "Or you could specify base commit by --base=<base-commit-id> manually."));
1295 ALLOC_ARRAY(rev
, total
);
1296 for (i
= 0; i
< total
; i
++)
1301 * Get merge base through pair-wise computations
1302 * and store it in rev[0].
1304 while (rev_nr
> 1) {
1305 for (i
= 0; i
< rev_nr
/ 2; i
++) {
1306 struct commit_list
*merge_base
;
1307 merge_base
= get_merge_bases(rev
[2 * i
], rev
[2 * i
+ 1]);
1308 if (!merge_base
|| merge_base
->next
)
1309 die(_("Failed to find exact merge base"));
1311 rev
[i
] = merge_base
->item
;
1315 rev
[i
] = rev
[2 * i
];
1316 rev_nr
= DIV_ROUND_UP(rev_nr
, 2);
1319 if (!in_merge_bases(base
, rev
[0]))
1320 die(_("base commit should be the ancestor of revision list"));
1322 for (i
= 0; i
< total
; i
++) {
1323 if (base
== list
[i
])
1324 die(_("base commit shouldn't be in revision list"));
1331 static void prepare_bases(struct base_tree_info
*bases
,
1332 struct commit
*base
,
1333 struct commit
**list
,
1336 struct commit
*commit
;
1337 struct rev_info revs
;
1338 struct diff_options diffopt
;
1344 diff_setup(&diffopt
);
1345 DIFF_OPT_SET(&diffopt
, RECURSIVE
);
1346 diff_setup_done(&diffopt
);
1348 oidcpy(&bases
->base_commit
, &base
->object
.oid
);
1350 init_revisions(&revs
, NULL
);
1351 revs
.max_parents
= 1;
1352 revs
.topo_order
= 1;
1353 for (i
= 0; i
< total
; i
++) {
1354 list
[i
]->object
.flags
&= ~UNINTERESTING
;
1355 add_pending_object(&revs
, &list
[i
]->object
, "rev_list");
1356 list
[i
]->util
= (void *)1;
1358 base
->object
.flags
|= UNINTERESTING
;
1359 add_pending_object(&revs
, &base
->object
, "base");
1361 if (prepare_revision_walk(&revs
))
1362 die(_("revision walk setup failed"));
1364 * Traverse the commits list, get prerequisite patch ids
1365 * and stuff them in bases structure.
1367 while ((commit
= get_revision(&revs
)) != NULL
) {
1368 struct object_id oid
;
1369 struct object_id
*patch_id
;
1372 if (commit_patch_id(commit
, &diffopt
, &oid
, 0))
1373 die(_("cannot get patch id"));
1374 ALLOC_GROW(bases
->patch_id
, bases
->nr_patch_id
+ 1, bases
->alloc_patch_id
);
1375 patch_id
= bases
->patch_id
+ bases
->nr_patch_id
;
1376 oidcpy(patch_id
, &oid
);
1377 bases
->nr_patch_id
++;
1381 static void print_bases(struct base_tree_info
*bases
, FILE *file
)
1385 /* Only do this once, either for the cover or for the first one */
1386 if (is_null_oid(&bases
->base_commit
))
1389 /* Show the base commit */
1390 fprintf(file
, "\nbase-commit: %s\n", oid_to_hex(&bases
->base_commit
));
1392 /* Show the prerequisite patches */
1393 for (i
= bases
->nr_patch_id
- 1; i
>= 0; i
--)
1394 fprintf(file
, "prerequisite-patch-id: %s\n", oid_to_hex(&bases
->patch_id
[i
]));
1396 free(bases
->patch_id
);
1397 bases
->nr_patch_id
= 0;
1398 bases
->alloc_patch_id
= 0;
1399 oidclr(&bases
->base_commit
);
1402 int cmd_format_patch(int argc
, const char **argv
, const char *prefix
)
1404 struct commit
*commit
;
1405 struct commit
**list
= NULL
;
1406 struct rev_info rev
;
1407 struct setup_revision_opt s_r_opt
;
1408 int nr
= 0, total
, i
;
1410 int start_number
= -1;
1411 int just_numbers
= 0;
1412 int ignore_if_in_upstream
= 0;
1413 int cover_letter
= -1;
1414 int boundary_count
= 0;
1415 int no_binary_diff
= 0;
1416 int zero_commit
= 0;
1417 struct commit
*origin
= NULL
;
1418 const char *in_reply_to
= NULL
;
1419 struct patch_ids ids
;
1420 struct strbuf buf
= STRBUF_INIT
;
1421 int use_patch_format
= 0;
1423 int reroll_count
= -1;
1424 char *branch_name
= NULL
;
1425 char *base_commit
= NULL
;
1426 struct base_tree_info bases
;
1427 int show_progress
= 0;
1428 struct progress
*progress
= NULL
;
1430 const struct option builtin_format_patch_options
[] = {
1431 { OPTION_CALLBACK
, 'n', "numbered", &numbered
, NULL
,
1432 N_("use [PATCH n/m] even with a single patch"),
1433 PARSE_OPT_NOARG
, numbered_callback
},
1434 { OPTION_CALLBACK
, 'N', "no-numbered", &numbered
, NULL
,
1435 N_("use [PATCH] even with multiple patches"),
1436 PARSE_OPT_NOARG
, no_numbered_callback
},
1437 OPT_BOOL('s', "signoff", &do_signoff
, N_("add Signed-off-by:")),
1438 OPT_BOOL(0, "stdout", &use_stdout
,
1439 N_("print patches to standard out")),
1440 OPT_BOOL(0, "cover-letter", &cover_letter
,
1441 N_("generate a cover letter")),
1442 OPT_BOOL(0, "numbered-files", &just_numbers
,
1443 N_("use simple number sequence for output file names")),
1444 OPT_STRING(0, "suffix", &fmt_patch_suffix
, N_("sfx"),
1445 N_("use <sfx> instead of '.patch'")),
1446 OPT_INTEGER(0, "start-number", &start_number
,
1447 N_("start numbering patches at <n> instead of 1")),
1448 OPT_INTEGER('v', "reroll-count", &reroll_count
,
1449 N_("mark the series as Nth re-roll")),
1450 { OPTION_CALLBACK
, 0, "rfc", &rev
, NULL
,
1451 N_("Use [RFC PATCH] instead of [PATCH]"),
1452 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
, rfc_callback
},
1453 { OPTION_CALLBACK
, 0, "subject-prefix", &rev
, N_("prefix"),
1454 N_("Use [<prefix>] instead of [PATCH]"),
1455 PARSE_OPT_NONEG
, subject_prefix_callback
},
1456 { OPTION_CALLBACK
, 'o', "output-directory", &output_directory
,
1457 N_("dir"), N_("store resulting files in <dir>"),
1458 PARSE_OPT_NONEG
, output_directory_callback
},
1459 { OPTION_CALLBACK
, 'k', "keep-subject", &rev
, NULL
,
1460 N_("don't strip/add [PATCH]"),
1461 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
, keep_callback
},
1462 OPT_BOOL(0, "no-binary", &no_binary_diff
,
1463 N_("don't output binary diffs")),
1464 OPT_BOOL(0, "zero-commit", &zero_commit
,
1465 N_("output all-zero hash in From header")),
1466 OPT_BOOL(0, "ignore-if-in-upstream", &ignore_if_in_upstream
,
1467 N_("don't include a patch matching a commit upstream")),
1468 { OPTION_SET_INT
, 'p', "no-stat", &use_patch_format
, NULL
,
1469 N_("show patch format instead of default (patch + stat)"),
1470 PARSE_OPT_NONEG
| PARSE_OPT_NOARG
, NULL
, 1},
1471 OPT_GROUP(N_("Messaging")),
1472 { OPTION_CALLBACK
, 0, "add-header", NULL
, N_("header"),
1473 N_("add email header"), 0, header_callback
},
1474 { OPTION_CALLBACK
, 0, "to", NULL
, N_("email"), N_("add To: header"),
1476 { OPTION_CALLBACK
, 0, "cc", NULL
, N_("email"), N_("add Cc: header"),
1478 { OPTION_CALLBACK
, 0, "from", &from
, N_("ident"),
1479 N_("set From address to <ident> (or committer ident if absent)"),
1480 PARSE_OPT_OPTARG
, from_callback
},
1481 OPT_STRING(0, "in-reply-to", &in_reply_to
, N_("message-id"),
1482 N_("make first mail a reply to <message-id>")),
1483 { OPTION_CALLBACK
, 0, "attach", &rev
, N_("boundary"),
1484 N_("attach the patch"), PARSE_OPT_OPTARG
,
1486 { OPTION_CALLBACK
, 0, "inline", &rev
, N_("boundary"),
1487 N_("inline the patch"),
1488 PARSE_OPT_OPTARG
| PARSE_OPT_NONEG
,
1490 { OPTION_CALLBACK
, 0, "thread", &thread
, N_("style"),
1491 N_("enable message threading, styles: shallow, deep"),
1492 PARSE_OPT_OPTARG
, thread_callback
},
1493 OPT_STRING(0, "signature", &signature
, N_("signature"),
1494 N_("add a signature")),
1495 OPT_STRING(0, "base", &base_commit
, N_("base-commit"),
1496 N_("add prerequisite tree info to the patch series")),
1497 OPT_FILENAME(0, "signature-file", &signature_file
,
1498 N_("add a signature from a file")),
1499 OPT__QUIET(&quiet
, N_("don't print the patch filenames")),
1500 OPT_BOOL(0, "progress", &show_progress
,
1501 N_("show progress while generating patches")),
1505 extra_hdr
.strdup_strings
= 1;
1506 extra_to
.strdup_strings
= 1;
1507 extra_cc
.strdup_strings
= 1;
1508 init_log_defaults();
1509 git_config(git_format_config
, NULL
);
1510 init_revisions(&rev
, prefix
);
1511 rev
.commit_format
= CMIT_FMT_EMAIL
;
1512 rev
.expand_tabs_in_log_default
= 0;
1513 rev
.verbose_header
= 1;
1515 rev
.max_parents
= 1;
1516 DIFF_OPT_SET(&rev
.diffopt
, RECURSIVE
);
1517 rev
.subject_prefix
= fmt_patch_subject_prefix
;
1518 memset(&s_r_opt
, 0, sizeof(s_r_opt
));
1519 s_r_opt
.def
= "HEAD";
1520 s_r_opt
.revarg_opt
= REVARG_COMMITTISH
;
1522 if (default_attach
) {
1523 rev
.mime_boundary
= default_attach
;
1528 * Parse the arguments before setup_revisions(), or something
1529 * like "git format-patch -o a123 HEAD^.." may fail; a123 is
1530 * possibly a valid SHA1.
1532 argc
= parse_options(argc
, argv
, prefix
, builtin_format_patch_options
,
1533 builtin_format_patch_usage
,
1534 PARSE_OPT_KEEP_ARGV0
| PARSE_OPT_KEEP_UNKNOWN
|
1535 PARSE_OPT_KEEP_DASHDASH
);
1537 if (0 < reroll_count
) {
1538 struct strbuf sprefix
= STRBUF_INIT
;
1539 strbuf_addf(&sprefix
, "%s v%d",
1540 rev
.subject_prefix
, reroll_count
);
1541 rev
.reroll_count
= reroll_count
;
1542 rev
.subject_prefix
= strbuf_detach(&sprefix
, NULL
);
1545 for (i
= 0; i
< extra_hdr
.nr
; i
++) {
1546 strbuf_addstr(&buf
, extra_hdr
.items
[i
].string
);
1547 strbuf_addch(&buf
, '\n');
1551 strbuf_addstr(&buf
, "To: ");
1552 for (i
= 0; i
< extra_to
.nr
; i
++) {
1554 strbuf_addstr(&buf
, " ");
1555 strbuf_addstr(&buf
, extra_to
.items
[i
].string
);
1556 if (i
+ 1 < extra_to
.nr
)
1557 strbuf_addch(&buf
, ',');
1558 strbuf_addch(&buf
, '\n');
1562 strbuf_addstr(&buf
, "Cc: ");
1563 for (i
= 0; i
< extra_cc
.nr
; i
++) {
1565 strbuf_addstr(&buf
, " ");
1566 strbuf_addstr(&buf
, extra_cc
.items
[i
].string
);
1567 if (i
+ 1 < extra_cc
.nr
)
1568 strbuf_addch(&buf
, ',');
1569 strbuf_addch(&buf
, '\n');
1572 rev
.extra_headers
= strbuf_detach(&buf
, NULL
);
1575 if (split_ident_line(&rev
.from_ident
, from
, strlen(from
)))
1576 die(_("invalid ident line: %s"), from
);
1579 if (start_number
< 0)
1583 * If numbered is set solely due to format.numbered in config,
1584 * and it would conflict with --keep-subject (-k) from the
1585 * command line, reset "numbered".
1587 if (numbered
&& keep_subject
&& !numbered_cmdline_opt
)
1590 if (numbered
&& keep_subject
)
1591 die (_("-n and -k are mutually exclusive."));
1592 if (keep_subject
&& subject_prefix
)
1593 die (_("--subject-prefix/--rfc and -k are mutually exclusive."));
1594 rev
.preserve_subject
= keep_subject
;
1596 argc
= setup_revisions(argc
, argv
, &rev
, &s_r_opt
);
1598 die (_("unrecognized argument: %s"), argv
[1]);
1600 if (rev
.diffopt
.output_format
& DIFF_FORMAT_NAME
)
1601 die(_("--name-only does not make sense"));
1602 if (rev
.diffopt
.output_format
& DIFF_FORMAT_NAME_STATUS
)
1603 die(_("--name-status does not make sense"));
1604 if (rev
.diffopt
.output_format
& DIFF_FORMAT_CHECKDIFF
)
1605 die(_("--check does not make sense"));
1607 if (!use_patch_format
&&
1608 (!rev
.diffopt
.output_format
||
1609 rev
.diffopt
.output_format
== DIFF_FORMAT_PATCH
))
1610 rev
.diffopt
.output_format
= DIFF_FORMAT_DIFFSTAT
| DIFF_FORMAT_SUMMARY
;
1612 /* Always generate a patch */
1613 rev
.diffopt
.output_format
|= DIFF_FORMAT_PATCH
;
1615 rev
.zero_commit
= zero_commit
;
1617 if (!DIFF_OPT_TST(&rev
.diffopt
, TEXT
) && !no_binary_diff
)
1618 DIFF_OPT_SET(&rev
.diffopt
, BINARY
);
1621 init_display_notes(&rev
.notes_opt
);
1623 if (!output_directory
&& !use_stdout
)
1624 output_directory
= config_output_directory
;
1627 output_directory
= set_outdir(prefix
, output_directory
);
1631 if (output_directory
) {
1632 if (rev
.diffopt
.use_color
!= GIT_COLOR_ALWAYS
)
1633 rev
.diffopt
.use_color
= GIT_COLOR_NEVER
;
1635 die(_("standard output, or directory, which one?"));
1636 if (mkdir(output_directory
, 0777) < 0 && errno
!= EEXIST
)
1637 die_errno(_("Could not create directory '%s'"),
1641 if (rev
.pending
.nr
== 1) {
1644 if (rev
.max_count
< 0 && !rev
.show_root_diff
) {
1646 * This is traditional behaviour of "git format-patch
1647 * origin" that prepares what the origin side still
1650 rev
.pending
.objects
[0].item
->flags
|= UNINTERESTING
;
1651 add_head_to_pending(&rev
);
1655 * Otherwise, it is "format-patch -22 HEAD", and/or
1656 * "format-patch --root HEAD". The user wants
1657 * get_revision() to do the usual traversal.
1660 if (!strcmp(rev
.pending
.objects
[0].name
, "HEAD"))
1664 const char *ref
, *v
;
1665 ref
= resolve_ref_unsafe("HEAD", RESOLVE_REF_READING
,
1667 if (ref
&& skip_prefix(ref
, "refs/heads/", &v
))
1668 branch_name
= xstrdup(v
);
1670 branch_name
= xstrdup(""); /* no branch */
1675 * We cannot move this anywhere earlier because we do want to
1676 * know if --root was given explicitly from the command line.
1678 rev
.show_root_diff
= 1;
1680 if (ignore_if_in_upstream
) {
1681 /* Don't say anything if head and upstream are the same. */
1682 if (rev
.pending
.nr
== 2) {
1683 struct object_array_entry
*o
= rev
.pending
.objects
;
1684 if (oidcmp(&o
[0].item
->oid
, &o
[1].item
->oid
) == 0)
1687 get_patch_ids(&rev
, &ids
);
1690 if (prepare_revision_walk(&rev
))
1691 die(_("revision walk setup failed"));
1693 while ((commit
= get_revision(&rev
)) != NULL
) {
1694 if (commit
->object
.flags
& BOUNDARY
) {
1696 origin
= (boundary_count
== 1) ? commit
: NULL
;
1700 if (ignore_if_in_upstream
&& has_commit_patch_id(commit
, &ids
))
1704 REALLOC_ARRAY(list
, nr
);
1705 list
[nr
- 1] = commit
;
1711 if (cover_letter
== -1) {
1712 if (config_cover_letter
== COVER_AUTO
)
1713 cover_letter
= (total
> 1);
1715 cover_letter
= (config_cover_letter
== COVER_ON
);
1717 if (!keep_subject
&& auto_number
&& (total
> 1 || cover_letter
))
1720 rev
.total
= total
+ start_number
- 1;
1723 ; /* --no-signature inhibits all signatures */
1724 } else if (signature
&& signature
!= git_version_string
) {
1725 ; /* non-default signature already set */
1726 } else if (signature_file
) {
1727 struct strbuf buf
= STRBUF_INIT
;
1729 if (strbuf_read_file(&buf
, signature_file
, 128) < 0)
1730 die_errno(_("unable to read signature file '%s'"), signature_file
);
1731 signature
= strbuf_detach(&buf
, NULL
);
1734 memset(&bases
, 0, sizeof(bases
));
1735 if (base_commit
|| base_auto
) {
1736 struct commit
*base
= get_base_commit(base_commit
, list
, nr
);
1737 reset_revision_walk();
1738 prepare_bases(&bases
, base
, list
, nr
);
1741 if (in_reply_to
|| thread
|| cover_letter
)
1742 rev
.ref_message_ids
= xcalloc(1, sizeof(struct string_list
));
1744 const char *msgid
= clean_message_id(in_reply_to
);
1745 string_list_append(rev
.ref_message_ids
, msgid
);
1747 rev
.numbered_files
= just_numbers
;
1748 rev
.patch_suffix
= fmt_patch_suffix
;
1751 gen_message_id(&rev
, "cover");
1752 make_cover_letter(&rev
, use_stdout
,
1753 origin
, nr
, list
, branch_name
, quiet
);
1754 print_bases(&bases
, rev
.diffopt
.file
);
1755 print_signature(rev
.diffopt
.file
);
1759 rev
.add_signoff
= do_signoff
;
1762 progress
= start_delayed_progress(_("Generating patches"), total
);
1765 display_progress(progress
, total
- nr
);
1767 rev
.nr
= total
- nr
+ (start_number
- 1);
1768 /* Make the second and subsequent mails replies to the first */
1770 /* Have we already had a message ID? */
1771 if (rev
.message_id
) {
1773 * For deep threading: make every mail
1774 * a reply to the previous one, no
1775 * matter what other options are set.
1777 * For shallow threading:
1779 * Without --cover-letter and
1780 * --in-reply-to, make every mail a
1781 * reply to the one before.
1783 * With --in-reply-to but no
1784 * --cover-letter, make every mail a
1785 * reply to the <reply-to>.
1787 * With --cover-letter, make every
1788 * mail but the cover letter a reply
1789 * to the cover letter. The cover
1790 * letter is a reply to the
1791 * --in-reply-to, if specified.
1793 if (thread
== THREAD_SHALLOW
1794 && rev
.ref_message_ids
->nr
> 0
1795 && (!cover_letter
|| rev
.nr
> 1))
1796 free(rev
.message_id
);
1798 string_list_append(rev
.ref_message_ids
,
1801 gen_message_id(&rev
, oid_to_hex(&commit
->object
.oid
));
1805 open_next_file(rev
.numbered_files
? NULL
: commit
, NULL
, &rev
, quiet
))
1806 die(_("Failed to create output files"));
1807 shown
= log_tree_commit(&rev
, commit
);
1808 free_commit_buffer(commit
);
1810 /* We put one extra blank line between formatted
1811 * patches and this flag is used by log-tree code
1812 * to see if it needs to emit a LF before showing
1813 * the log; when using one file per patch, we do
1814 * not want the extra blank line.
1819 print_bases(&bases
, rev
.diffopt
.file
);
1820 if (rev
.mime_boundary
)
1821 fprintf(rev
.diffopt
.file
, "\n--%s%s--\n\n\n",
1822 mime_boundary_leader
,
1825 print_signature(rev
.diffopt
.file
);
1828 fclose(rev
.diffopt
.file
);
1830 stop_progress(&progress
);
1833 string_list_clear(&extra_to
, 0);
1834 string_list_clear(&extra_cc
, 0);
1835 string_list_clear(&extra_hdr
, 0);
1836 if (ignore_if_in_upstream
)
1837 free_patch_ids(&ids
);
1841 static int add_pending_commit(const char *arg
, struct rev_info
*revs
, int flags
)
1843 struct object_id oid
;
1844 if (get_oid(arg
, &oid
) == 0) {
1845 struct commit
*commit
= lookup_commit_reference(&oid
);
1847 commit
->object
.flags
|= flags
;
1848 add_pending_object(revs
, &commit
->object
, arg
);
1855 static const char * const cherry_usage
[] = {
1856 N_("git cherry [-v] [<upstream> [<head> [<limit>]]]"),
1860 static void print_commit(char sign
, struct commit
*commit
, int verbose
,
1861 int abbrev
, FILE *file
)
1864 fprintf(file
, "%c %s\n", sign
,
1865 find_unique_abbrev(commit
->object
.oid
.hash
, abbrev
));
1867 struct strbuf buf
= STRBUF_INIT
;
1868 pp_commit_easy(CMIT_FMT_ONELINE
, commit
, &buf
);
1869 fprintf(file
, "%c %s %s\n", sign
,
1870 find_unique_abbrev(commit
->object
.oid
.hash
, abbrev
),
1872 strbuf_release(&buf
);
1876 int cmd_cherry(int argc
, const char **argv
, const char *prefix
)
1878 struct rev_info revs
;
1879 struct patch_ids ids
;
1880 struct commit
*commit
;
1881 struct commit_list
*list
= NULL
;
1882 struct branch
*current_branch
;
1883 const char *upstream
;
1884 const char *head
= "HEAD";
1885 const char *limit
= NULL
;
1886 int verbose
= 0, abbrev
= 0;
1888 struct option options
[] = {
1889 OPT__ABBREV(&abbrev
),
1890 OPT__VERBOSE(&verbose
, N_("be verbose")),
1894 argc
= parse_options(argc
, argv
, prefix
, options
, cherry_usage
, 0);
1907 current_branch
= branch_get(NULL
);
1908 upstream
= branch_get_upstream(current_branch
, NULL
);
1910 fprintf(stderr
, _("Could not find a tracked"
1911 " remote branch, please"
1912 " specify <upstream> manually.\n"));
1913 usage_with_options(cherry_usage
, options
);
1917 init_revisions(&revs
, prefix
);
1918 revs
.max_parents
= 1;
1920 if (add_pending_commit(head
, &revs
, 0))
1921 die(_("Unknown commit %s"), head
);
1922 if (add_pending_commit(upstream
, &revs
, UNINTERESTING
))
1923 die(_("Unknown commit %s"), upstream
);
1925 /* Don't say anything if head and upstream are the same. */
1926 if (revs
.pending
.nr
== 2) {
1927 struct object_array_entry
*o
= revs
.pending
.objects
;
1928 if (oidcmp(&o
[0].item
->oid
, &o
[1].item
->oid
) == 0)
1932 get_patch_ids(&revs
, &ids
);
1934 if (limit
&& add_pending_commit(limit
, &revs
, UNINTERESTING
))
1935 die(_("Unknown commit %s"), limit
);
1937 /* reverse the list of commits */
1938 if (prepare_revision_walk(&revs
))
1939 die(_("revision walk setup failed"));
1940 while ((commit
= get_revision(&revs
)) != NULL
) {
1941 commit_list_insert(commit
, &list
);
1947 commit
= list
->item
;
1948 if (has_commit_patch_id(commit
, &ids
))
1950 print_commit(sign
, commit
, verbose
, abbrev
, revs
.diffopt
.file
);
1954 free_patch_ids(&ids
);