2 * Builtin "git log" and related commands (show, whatchanged)
4 * (C) Copyright 2006 Linus Torvalds
10 #include "object-store.h"
14 #include "diff-merges.h"
19 #include "reflog-walk.h"
20 #include "patch-ids.h"
21 #include "run-command.h"
24 #include "string-list.h"
25 #include "parse-options.h"
28 #include "streaming.h"
31 #include "gpg-interface.h"
33 #include "commit-slab.h"
34 #include "repository.h"
35 #include "commit-reach.h"
36 #include "range-diff.h"
37 #include "tmp-objdir.h"
39 #define MAIL_DEFAULT_WRAP 72
40 #define COVER_FROM_AUTO_MAX_SUBJECT_LEN 100
41 #define FORMAT_PATCH_NAME_MAX_DEFAULT 64
43 /* Set a default date-time format for git log ("log.date" config variable) */
44 static const char *default_date_mode
= NULL
;
46 static int default_abbrev_commit
;
47 static int default_show_root
= 1;
48 static int default_follow
;
49 static int default_show_signature
;
50 static int default_encode_email_headers
= 1;
51 static int decoration_style
;
52 static int decoration_given
;
53 static int use_mailmap_config
= 1;
54 static unsigned int force_in_body_from
;
55 static const char *fmt_patch_subject_prefix
= "PATCH";
56 static int fmt_patch_name_max
= FORMAT_PATCH_NAME_MAX_DEFAULT
;
57 static const char *fmt_pretty
;
59 static const char * const builtin_log_usage
[] = {
60 N_("git log [<options>] [<revision-range>] [[--] <path>...]"),
61 N_("git show [<options>] <object>..."),
65 struct line_opt_callback_data
{
68 struct string_list args
;
71 static int session_is_interactive(void)
73 return isatty(1) || pager_in_use();
76 static int auto_decoration_style(void)
78 return session_is_interactive() ? DECORATE_SHORT_REFS
: 0;
81 static int parse_decoration_style(const char *value
)
83 switch (git_parse_maybe_bool(value
)) {
85 return DECORATE_SHORT_REFS
;
91 if (!strcmp(value
, "full"))
92 return DECORATE_FULL_REFS
;
93 else if (!strcmp(value
, "short"))
94 return DECORATE_SHORT_REFS
;
95 else if (!strcmp(value
, "auto"))
96 return auto_decoration_style();
98 * Please update _git_log() in git-completion.bash when you
99 * add new decoration styles.
104 static int use_default_decoration_filter
= 1;
105 static struct string_list decorate_refs_exclude
= STRING_LIST_INIT_NODUP
;
106 static struct string_list decorate_refs_exclude_config
= STRING_LIST_INIT_NODUP
;
107 static struct string_list decorate_refs_include
= STRING_LIST_INIT_NODUP
;
109 static int clear_decorations_callback(const struct option
*opt
,
110 const char *arg
, int unset
)
112 string_list_clear(&decorate_refs_include
, 0);
113 string_list_clear(&decorate_refs_exclude
, 0);
114 use_default_decoration_filter
= 0;
118 static int decorate_callback(const struct option
*opt
, const char *arg
, int unset
)
121 decoration_style
= 0;
123 decoration_style
= parse_decoration_style(arg
);
125 decoration_style
= DECORATE_SHORT_REFS
;
127 if (decoration_style
< 0)
128 die(_("invalid --decorate option: %s"), arg
);
130 decoration_given
= 1;
135 static int log_line_range_callback(const struct option
*option
, const char *arg
, int unset
)
137 struct line_opt_callback_data
*data
= option
->value
;
139 BUG_ON_OPT_NEG(unset
);
144 data
->rev
->line_level_traverse
= 1;
145 string_list_append(&data
->args
, arg
);
150 static void init_log_defaults(void)
152 init_diff_ui_defaults();
154 decoration_style
= auto_decoration_style();
157 static void cmd_log_init_defaults(struct rev_info
*rev
)
160 get_commit_format(fmt_pretty
, rev
);
162 rev
->diffopt
.flags
.default_follow_renames
= 1;
163 rev
->verbose_header
= 1;
164 rev
->diffopt
.flags
.recursive
= 1;
165 rev
->diffopt
.stat_width
= -1; /* use full terminal width */
166 rev
->diffopt
.stat_graph_width
= -1; /* respect statGraphWidth config */
167 rev
->abbrev_commit
= default_abbrev_commit
;
168 rev
->show_root_diff
= default_show_root
;
169 rev
->subject_prefix
= fmt_patch_subject_prefix
;
170 rev
->patch_name_max
= fmt_patch_name_max
;
171 rev
->show_signature
= default_show_signature
;
172 rev
->encode_email_headers
= default_encode_email_headers
;
173 rev
->diffopt
.flags
.allow_textconv
= 1;
175 if (default_date_mode
)
176 parse_date_format(default_date_mode
, &rev
->date_mode
);
179 static void set_default_decoration_filter(struct decoration_filter
*decoration_filter
)
183 struct string_list
*include
= decoration_filter
->include_ref_pattern
;
184 const struct string_list
*config_exclude
=
185 git_config_get_value_multi("log.excludeDecoration");
187 if (config_exclude
) {
188 struct string_list_item
*item
;
189 for_each_string_list_item(item
, config_exclude
)
190 string_list_append(decoration_filter
->exclude_ref_config_pattern
,
195 * By default, decorate_all is disabled. Enable it if
196 * log.initialDecorationSet=all. Don't ever disable it by config,
197 * since the command-line takes precedent.
199 if (use_default_decoration_filter
&&
200 !git_config_get_string("log.initialdecorationset", &value
) &&
201 !strcmp("all", value
))
202 use_default_decoration_filter
= 0;
205 if (!use_default_decoration_filter
||
206 decoration_filter
->exclude_ref_pattern
->nr
||
207 decoration_filter
->include_ref_pattern
->nr
||
208 decoration_filter
->exclude_ref_config_pattern
->nr
)
212 * No command-line or config options were given, so
213 * populate with sensible defaults.
215 for (i
= 0; i
< ARRAY_SIZE(ref_namespace
); i
++) {
216 if (!ref_namespace
[i
].decoration
)
219 string_list_append(include
, ref_namespace
[i
].ref
);
223 static void cmd_log_init_finish(int argc
, const char **argv
, const char *prefix
,
224 struct rev_info
*rev
, struct setup_revision_opt
*opt
)
226 struct userformat_want w
;
227 int quiet
= 0, source
= 0, mailmap
;
228 static struct line_opt_callback_data line_cb
= {NULL
, NULL
, STRING_LIST_INIT_DUP
};
229 struct decoration_filter decoration_filter
= {
230 .exclude_ref_pattern
= &decorate_refs_exclude
,
231 .include_ref_pattern
= &decorate_refs_include
,
232 .exclude_ref_config_pattern
= &decorate_refs_exclude_config
,
234 static struct revision_sources revision_sources
;
236 const struct option builtin_log_options
[] = {
237 OPT__QUIET(&quiet
, N_("suppress diff output")),
238 OPT_BOOL(0, "source", &source
, N_("show source")),
239 OPT_BOOL(0, "use-mailmap", &mailmap
, N_("use mail map file")),
240 OPT_ALIAS(0, "mailmap", "use-mailmap"),
241 OPT_CALLBACK_F(0, "clear-decorations", NULL
, NULL
,
242 N_("clear all previously-defined decoration filters"),
243 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
,
244 clear_decorations_callback
),
245 OPT_STRING_LIST(0, "decorate-refs", &decorate_refs_include
,
246 N_("pattern"), N_("only decorate refs that match <pattern>")),
247 OPT_STRING_LIST(0, "decorate-refs-exclude", &decorate_refs_exclude
,
248 N_("pattern"), N_("do not decorate refs that match <pattern>")),
249 OPT_CALLBACK_F(0, "decorate", NULL
, NULL
, N_("decorate options"),
250 PARSE_OPT_OPTARG
, decorate_callback
),
251 OPT_CALLBACK('L', NULL
, &line_cb
, "range:file",
252 N_("trace the evolution of line range <start>,<end> or function :<funcname> in <file>"),
253 log_line_range_callback
),
258 line_cb
.prefix
= prefix
;
260 mailmap
= use_mailmap_config
;
261 argc
= parse_options(argc
, argv
, prefix
,
262 builtin_log_options
, builtin_log_usage
,
263 PARSE_OPT_KEEP_ARGV0
| PARSE_OPT_KEEP_UNKNOWN_OPT
|
264 PARSE_OPT_KEEP_DASHDASH
);
267 rev
->diffopt
.output_format
|= DIFF_FORMAT_NO_OUTPUT
;
268 argc
= setup_revisions(argc
, argv
, rev
, opt
);
270 /* Any arguments at this point are not recognized */
272 die(_("unrecognized argument: %s"), argv
[1]);
274 if (rev
->line_level_traverse
&& rev
->prune_data
.nr
)
275 die(_("-L<range>:<file> cannot be used with pathspec"));
277 memset(&w
, 0, sizeof(w
));
278 userformat_find_requirements(NULL
, &w
);
280 if (!rev
->show_notes_given
&& (!rev
->pretty_given
|| w
.notes
))
283 load_display_notes(&rev
->notes_opt
);
285 if ((rev
->diffopt
.pickaxe_opts
& DIFF_PICKAXE_KINDS_MASK
) ||
286 rev
->diffopt
.filter
|| rev
->diffopt
.flags
.follow_renames
)
287 rev
->always_show_header
= 0;
289 if (source
|| w
.source
) {
290 init_revision_sources(&revision_sources
);
291 rev
->sources
= &revision_sources
;
295 rev
->mailmap
= xmalloc(sizeof(struct string_list
));
296 string_list_init_nodup(rev
->mailmap
);
297 read_mailmap(rev
->mailmap
);
300 if (rev
->pretty_given
&& rev
->commit_format
== CMIT_FMT_RAW
) {
302 * "log --pretty=raw" is special; ignore UI oriented
303 * configuration variables such as decoration.
305 if (!decoration_given
)
306 decoration_style
= 0;
307 if (!rev
->abbrev_commit_given
)
308 rev
->abbrev_commit
= 0;
311 if (rev
->commit_format
== CMIT_FMT_USERFORMAT
) {
314 * Disable decoration loading if the format will not
317 decoration_style
= 0;
318 } else if (!decoration_style
) {
320 * If we are going to show them, make sure we do load
321 * them here, but taking care not to override a
322 * specific style set by config or --decorate.
324 decoration_style
= DECORATE_SHORT_REFS
;
328 if (decoration_style
|| rev
->simplify_by_decoration
) {
329 set_default_decoration_filter(&decoration_filter
);
331 if (decoration_style
)
332 rev
->show_decorations
= 1;
334 load_ref_decorations(&decoration_filter
, decoration_style
);
337 if (rev
->line_level_traverse
)
338 line_log_init(rev
, line_cb
.prefix
, &line_cb
.args
);
343 static void cmd_log_init(int argc
, const char **argv
, const char *prefix
,
344 struct rev_info
*rev
, struct setup_revision_opt
*opt
)
346 cmd_log_init_defaults(rev
);
347 cmd_log_init_finish(argc
, argv
, prefix
, rev
, opt
);
350 static int cmd_log_deinit(int ret
, struct rev_info
*rev
)
352 release_revisions(rev
);
357 * This gives a rough estimate for how many commits we
358 * will print out in the list.
360 static int estimate_commit_count(struct commit_list
*list
)
365 struct commit
*commit
= list
->item
;
366 unsigned int flags
= commit
->object
.flags
;
368 if (!(flags
& (TREESAME
| UNINTERESTING
)))
374 static void show_early_header(struct rev_info
*rev
, const char *stage
, int nr
)
376 if (rev
->shown_one
) {
378 if (rev
->commit_format
!= CMIT_FMT_ONELINE
)
379 putchar(rev
->diffopt
.line_termination
);
381 fprintf(rev
->diffopt
.file
, _("Final output: %d %s\n"), nr
, stage
);
384 static struct itimerval early_output_timer
;
386 static void log_show_early(struct rev_info
*revs
, struct commit_list
*list
)
388 int i
= revs
->early_output
;
390 int no_free
= revs
->diffopt
.no_free
;
392 revs
->diffopt
.no_free
= 0;
393 sort_in_topological_order(&list
, revs
->sort_order
);
395 struct commit
*commit
= list
->item
;
396 switch (simplify_commit(revs
, commit
)) {
399 int n
= estimate_commit_count(list
);
400 show_early_header(revs
, "incomplete", n
);
403 log_tree_commit(revs
, commit
);
409 revs
->diffopt
.no_free
= no_free
;
410 diff_free(&revs
->diffopt
);
416 /* Did we already get enough commits for the early output? */
418 revs
->diffopt
.no_free
= 0;
419 diff_free(&revs
->diffopt
);
424 * ..if no, then repeat it twice a second until we
427 * NOTE! We don't use "it_interval", because if the
428 * reader isn't listening, we want our output to be
429 * throttled by the writing, and not have the timer
430 * trigger every second even if we're blocked on a
433 early_output_timer
.it_value
.tv_sec
= 0;
434 early_output_timer
.it_value
.tv_usec
= 500000;
435 setitimer(ITIMER_REAL
, &early_output_timer
, NULL
);
438 static void early_output(int signal
)
440 show_early_output
= log_show_early
;
443 static void setup_early_output(void)
448 * Set up the signal handler, minimally intrusively:
449 * we only set a single volatile integer word (not
450 * using sigatomic_t - trying to avoid unnecessary
451 * system dependencies and headers), and using
454 memset(&sa
, 0, sizeof(sa
));
455 sa
.sa_handler
= early_output
;
456 sigemptyset(&sa
.sa_mask
);
457 sa
.sa_flags
= SA_RESTART
;
458 sigaction(SIGALRM
, &sa
, NULL
);
461 * If we can get the whole output in less than a
462 * tenth of a second, don't even bother doing the
463 * early-output thing..
465 * This is a one-time-only trigger.
467 early_output_timer
.it_value
.tv_sec
= 0;
468 early_output_timer
.it_value
.tv_usec
= 100000;
469 setitimer(ITIMER_REAL
, &early_output_timer
, NULL
);
472 static void finish_early_output(struct rev_info
*rev
)
474 int n
= estimate_commit_count(rev
->commits
);
475 signal(SIGALRM
, SIG_IGN
);
476 show_early_header(rev
, "done", n
);
479 static int cmd_log_walk_no_free(struct rev_info
*rev
)
481 struct commit
*commit
;
485 if (rev
->remerge_diff
) {
486 rev
->remerge_objdir
= tmp_objdir_create("remerge-diff");
487 if (!rev
->remerge_objdir
)
488 die(_("unable to create temporary object directory"));
489 tmp_objdir_replace_primary_odb(rev
->remerge_objdir
, 1);
492 if (rev
->early_output
)
493 setup_early_output();
495 if (prepare_revision_walk(rev
))
496 die(_("revision walk setup failed"));
498 if (rev
->early_output
)
499 finish_early_output(rev
);
502 * For --check and --exit-code, the exit code is based on CHECK_FAILED
503 * and HAS_CHANGES being accumulated in rev->diffopt, so be careful to
504 * retain that state information if replacing rev->diffopt in this loop
506 while ((commit
= get_revision(rev
)) != NULL
) {
507 if (!log_tree_commit(rev
, commit
) && rev
->max_count
>= 0)
509 * We decremented max_count in get_revision,
510 * but we didn't actually show the commit.
513 if (!rev
->reflog_info
) {
515 * We may show a given commit multiple times when
516 * walking the reflogs.
518 free_commit_buffer(the_repository
->parsed_objects
,
520 free_commit_list(commit
->parents
);
521 commit
->parents
= NULL
;
523 if (saved_nrl
< rev
->diffopt
.needed_rename_limit
)
524 saved_nrl
= rev
->diffopt
.needed_rename_limit
;
525 if (rev
->diffopt
.degraded_cc_to_c
)
528 rev
->diffopt
.degraded_cc_to_c
= saved_dcctc
;
529 rev
->diffopt
.needed_rename_limit
= saved_nrl
;
531 if (rev
->remerge_diff
) {
532 tmp_objdir_destroy(rev
->remerge_objdir
);
533 rev
->remerge_objdir
= NULL
;
536 if (rev
->diffopt
.output_format
& DIFF_FORMAT_CHECKDIFF
&&
537 rev
->diffopt
.flags
.check_failed
) {
540 return diff_result_code(&rev
->diffopt
, 0);
543 static int cmd_log_walk(struct rev_info
*rev
)
547 rev
->diffopt
.no_free
= 1;
548 retval
= cmd_log_walk_no_free(rev
);
549 rev
->diffopt
.no_free
= 0;
550 diff_free(&rev
->diffopt
);
554 static int git_log_config(const char *var
, const char *value
, void *cb
)
556 const char *slot_name
;
558 if (!strcmp(var
, "format.pretty"))
559 return git_config_string(&fmt_pretty
, var
, value
);
560 if (!strcmp(var
, "format.subjectprefix"))
561 return git_config_string(&fmt_patch_subject_prefix
, var
, value
);
562 if (!strcmp(var
, "format.filenamemaxlength")) {
563 fmt_patch_name_max
= git_config_int(var
, value
);
566 if (!strcmp(var
, "format.encodeemailheaders")) {
567 default_encode_email_headers
= git_config_bool(var
, value
);
570 if (!strcmp(var
, "log.abbrevcommit")) {
571 default_abbrev_commit
= git_config_bool(var
, value
);
574 if (!strcmp(var
, "log.date"))
575 return git_config_string(&default_date_mode
, var
, value
);
576 if (!strcmp(var
, "log.decorate")) {
577 decoration_style
= parse_decoration_style(value
);
578 if (decoration_style
< 0)
579 decoration_style
= 0; /* maybe warn? */
582 if (!strcmp(var
, "log.diffmerges"))
583 return diff_merges_config(value
);
584 if (!strcmp(var
, "log.showroot")) {
585 default_show_root
= git_config_bool(var
, value
);
588 if (!strcmp(var
, "log.follow")) {
589 default_follow
= git_config_bool(var
, value
);
592 if (skip_prefix(var
, "color.decorate.", &slot_name
))
593 return parse_decorate_color_config(var
, slot_name
, value
);
594 if (!strcmp(var
, "log.mailmap")) {
595 use_mailmap_config
= git_config_bool(var
, value
);
598 if (!strcmp(var
, "log.showsignature")) {
599 default_show_signature
= git_config_bool(var
, value
);
603 if (git_gpg_config(var
, value
, cb
) < 0)
605 return git_diff_ui_config(var
, value
, cb
);
608 int cmd_whatchanged(int argc
, const char **argv
, const char *prefix
)
611 struct setup_revision_opt opt
;
614 git_config(git_log_config
, NULL
);
616 repo_init_revisions(the_repository
, &rev
, prefix
);
617 git_config(grep_config
, &rev
.grep_filter
);
620 rev
.simplify_history
= 0;
621 memset(&opt
, 0, sizeof(opt
));
623 opt
.revarg_opt
= REVARG_COMMITTISH
;
624 cmd_log_init(argc
, argv
, prefix
, &rev
, &opt
);
625 if (!rev
.diffopt
.output_format
)
626 rev
.diffopt
.output_format
= DIFF_FORMAT_RAW
;
627 return cmd_log_deinit(cmd_log_walk(&rev
), &rev
);
630 static void show_tagger(const char *buf
, struct rev_info
*rev
)
632 struct strbuf out
= STRBUF_INIT
;
633 struct pretty_print_context pp
= {0};
635 pp
.fmt
= rev
->commit_format
;
636 pp
.date_mode
= rev
->date_mode
;
637 pp_user_info(&pp
, "Tagger", &out
, buf
, get_log_output_encoding());
638 fprintf(rev
->diffopt
.file
, "%s", out
.buf
);
639 strbuf_release(&out
);
642 static int show_blob_object(const struct object_id
*oid
, struct rev_info
*rev
, const char *obj_name
)
644 struct object_id oidc
;
645 struct object_context obj_context
;
649 fflush(rev
->diffopt
.file
);
650 if (!rev
->diffopt
.flags
.textconv_set_via_cmdline
||
651 !rev
->diffopt
.flags
.allow_textconv
)
652 return stream_blob_to_fd(1, oid
, NULL
, 0);
654 if (get_oid_with_context(the_repository
, obj_name
,
656 &oidc
, &obj_context
))
657 die(_("not a valid object name %s"), obj_name
);
658 if (!obj_context
.path
||
659 !textconv_object(the_repository
, obj_context
.path
,
660 obj_context
.mode
, &oidc
, 1, &buf
, &size
)) {
661 free(obj_context
.path
);
662 return stream_blob_to_fd(1, oid
, NULL
, 0);
666 die(_("git show %s: bad file"), obj_name
);
668 write_or_die(1, buf
, size
);
669 free(obj_context
.path
);
673 static int show_tag_object(const struct object_id
*oid
, struct rev_info
*rev
)
676 enum object_type type
;
677 char *buf
= read_object_file(oid
, &type
, &size
);
681 return error(_("could not read object %s"), oid_to_hex(oid
));
683 assert(type
== OBJ_TAG
);
684 while (offset
< size
&& buf
[offset
] != '\n') {
685 int new_offset
= offset
+ 1;
687 while (new_offset
< size
&& buf
[new_offset
++] != '\n')
689 if (skip_prefix(buf
+ offset
, "tagger ", &ident
))
690 show_tagger(ident
, rev
);
695 fwrite(buf
+ offset
, size
- offset
, 1, rev
->diffopt
.file
);
700 static int show_tree_object(const struct object_id
*oid UNUSED
,
701 struct strbuf
*base UNUSED
,
702 const char *pathname
, unsigned mode
,
705 FILE *file
= context
;
706 fprintf(file
, "%s%s\n", pathname
, S_ISDIR(mode
) ? "/" : "");
710 static void show_setup_revisions_tweak(struct rev_info
*rev
,
711 struct setup_revision_opt
*opt
)
713 if (rev
->first_parent_only
)
714 diff_merges_default_to_first_parent(rev
);
716 diff_merges_default_to_dense_combined(rev
);
717 if (!rev
->diffopt
.output_format
)
718 rev
->diffopt
.output_format
= DIFF_FORMAT_PATCH
;
721 int cmd_show(int argc
, const char **argv
, const char *prefix
)
725 struct setup_revision_opt opt
;
726 struct pathspec match_all
;
730 git_config(git_log_config
, NULL
);
732 if (the_repository
->gitdir
) {
733 prepare_repo_settings(the_repository
);
734 the_repository
->settings
.command_requires_full_index
= 0;
737 memset(&match_all
, 0, sizeof(match_all
));
738 repo_init_revisions(the_repository
, &rev
, prefix
);
739 git_config(grep_config
, &rev
.grep_filter
);
742 rev
.always_show_header
= 1;
744 rev
.diffopt
.stat_width
= -1; /* Scale to real terminal size */
746 memset(&opt
, 0, sizeof(opt
));
748 opt
.tweak
= show_setup_revisions_tweak
;
749 cmd_log_init(argc
, argv
, prefix
, &rev
, &opt
);
752 return cmd_log_deinit(cmd_log_walk(&rev
), &rev
);
754 rev
.diffopt
.no_free
= 1;
755 for (i
= 0; i
< rev
.pending
.nr
&& !ret
; i
++) {
756 struct object
*o
= rev
.pending
.objects
[i
].item
;
757 const char *name
= rev
.pending
.objects
[i
].name
;
760 ret
= show_blob_object(&o
->oid
, &rev
, name
);
763 struct tag
*t
= (struct tag
*)o
;
764 struct object_id
*oid
= get_tagged_oid(t
);
768 fprintf(rev
.diffopt
.file
, "%stag %s%s\n",
769 diff_get_color_opt(&rev
.diffopt
, DIFF_COMMIT
),
771 diff_get_color_opt(&rev
.diffopt
, DIFF_RESET
));
772 ret
= show_tag_object(&o
->oid
, &rev
);
776 o
= parse_object(the_repository
, oid
);
778 ret
= error(_("could not read object %s"),
780 rev
.pending
.objects
[i
].item
= o
;
787 fprintf(rev
.diffopt
.file
, "%stree %s%s\n\n",
788 diff_get_color_opt(&rev
.diffopt
, DIFF_COMMIT
),
790 diff_get_color_opt(&rev
.diffopt
, DIFF_RESET
));
791 read_tree(the_repository
, (struct tree
*)o
,
792 &match_all
, show_tree_object
,
798 struct object_array old
;
799 struct object_array blank
= OBJECT_ARRAY_INIT
;
801 memcpy(&old
, &rev
.pending
, sizeof(old
));
802 memcpy(&rev
.pending
, &blank
, sizeof(rev
.pending
));
804 add_object_array(o
, name
, &rev
.pending
);
805 ret
= cmd_log_walk_no_free(&rev
);
809 * object_array_clear(&pending). It was
810 * cleared already in prepare_revision_walk()
812 memcpy(&rev
.pending
, &old
, sizeof(rev
.pending
));
816 ret
= error(_("unknown type: %d"), o
->type
);
820 rev
.diffopt
.no_free
= 0;
821 diff_free(&rev
.diffopt
);
823 return cmd_log_deinit(ret
, &rev
);
827 * This is equivalent to "git log -g --abbrev-commit --pretty=oneline"
829 int cmd_log_reflog(int argc
, const char **argv
, const char *prefix
)
832 struct setup_revision_opt opt
;
835 git_config(git_log_config
, NULL
);
837 repo_init_revisions(the_repository
, &rev
, prefix
);
838 init_reflog_walk(&rev
.reflog_info
);
839 git_config(grep_config
, &rev
.grep_filter
);
841 rev
.verbose_header
= 1;
842 memset(&opt
, 0, sizeof(opt
));
844 cmd_log_init_defaults(&rev
);
845 rev
.abbrev_commit
= 1;
846 rev
.commit_format
= CMIT_FMT_ONELINE
;
847 rev
.use_terminator
= 1;
848 rev
.always_show_header
= 1;
849 cmd_log_init_finish(argc
, argv
, prefix
, &rev
, &opt
);
851 return cmd_log_deinit(cmd_log_walk(&rev
), &rev
);
854 static void log_setup_revisions_tweak(struct rev_info
*rev
,
855 struct setup_revision_opt
*opt
)
857 if (rev
->diffopt
.flags
.default_follow_renames
&&
858 rev
->prune_data
.nr
== 1)
859 rev
->diffopt
.flags
.follow_renames
= 1;
861 if (rev
->first_parent_only
)
862 diff_merges_default_to_first_parent(rev
);
865 int cmd_log(int argc
, const char **argv
, const char *prefix
)
868 struct setup_revision_opt opt
;
871 git_config(git_log_config
, NULL
);
873 repo_init_revisions(the_repository
, &rev
, prefix
);
874 git_config(grep_config
, &rev
.grep_filter
);
876 rev
.always_show_header
= 1;
877 memset(&opt
, 0, sizeof(opt
));
879 opt
.revarg_opt
= REVARG_COMMITTISH
;
880 opt
.tweak
= log_setup_revisions_tweak
;
881 cmd_log_init(argc
, argv
, prefix
, &rev
, &opt
);
882 return cmd_log_deinit(cmd_log_walk(&rev
), &rev
);
887 static const char *fmt_patch_suffix
= ".patch";
888 static int numbered
= 0;
889 static int auto_number
= 1;
891 static char *default_attach
= NULL
;
893 static struct string_list extra_hdr
= STRING_LIST_INIT_NODUP
;
894 static struct string_list extra_to
= STRING_LIST_INIT_NODUP
;
895 static struct string_list extra_cc
= STRING_LIST_INIT_NODUP
;
897 static void add_header(const char *value
)
899 struct string_list_item
*item
;
900 int len
= strlen(value
);
901 while (len
&& value
[len
- 1] == '\n')
904 if (!strncasecmp(value
, "to: ", 4)) {
905 item
= string_list_append(&extra_to
, value
+ 4);
907 } else if (!strncasecmp(value
, "cc: ", 4)) {
908 item
= string_list_append(&extra_cc
, value
+ 4);
911 item
= string_list_append(&extra_hdr
, value
);
914 item
->string
[len
] = '\0';
930 enum cover_from_description
{
937 enum auto_base_setting
{
943 static enum thread_level thread
;
944 static int do_signoff
;
945 static enum auto_base_setting auto_base
;
947 static const char *signature
= git_version_string
;
948 static const char *signature_file
;
949 static enum cover_setting config_cover_letter
;
950 static const char *config_output_directory
;
951 static enum cover_from_description cover_from_description_mode
= COVER_FROM_MESSAGE
;
952 static int show_notes
;
953 static struct display_notes_opt notes_opt
;
955 static enum cover_from_description
parse_cover_from_description(const char *arg
)
957 if (!arg
|| !strcmp(arg
, "default"))
958 return COVER_FROM_MESSAGE
;
959 else if (!strcmp(arg
, "none"))
960 return COVER_FROM_NONE
;
961 else if (!strcmp(arg
, "message"))
962 return COVER_FROM_MESSAGE
;
963 else if (!strcmp(arg
, "subject"))
964 return COVER_FROM_SUBJECT
;
965 else if (!strcmp(arg
, "auto"))
966 return COVER_FROM_AUTO
;
968 die(_("%s: invalid cover from description mode"), arg
);
971 static int git_format_config(const char *var
, const char *value
, void *cb
)
973 if (!strcmp(var
, "format.headers")) {
975 die(_("format.headers without value"));
979 if (!strcmp(var
, "format.suffix"))
980 return git_config_string(&fmt_patch_suffix
, var
, value
);
981 if (!strcmp(var
, "format.to")) {
983 return config_error_nonbool(var
);
984 string_list_append(&extra_to
, value
);
987 if (!strcmp(var
, "format.cc")) {
989 return config_error_nonbool(var
);
990 string_list_append(&extra_cc
, value
);
993 if (!strcmp(var
, "diff.color") || !strcmp(var
, "color.diff") ||
994 !strcmp(var
, "color.ui") || !strcmp(var
, "diff.submodule")) {
997 if (!strcmp(var
, "format.numbered")) {
998 if (value
&& !strcasecmp(value
, "auto")) {
1002 numbered
= git_config_bool(var
, value
);
1003 auto_number
= auto_number
&& numbered
;
1006 if (!strcmp(var
, "format.attach")) {
1007 if (value
&& *value
)
1008 default_attach
= xstrdup(value
);
1010 default_attach
= xstrdup(git_version_string
);
1013 if (!strcmp(var
, "format.thread")) {
1014 if (value
&& !strcasecmp(value
, "deep")) {
1015 thread
= THREAD_DEEP
;
1018 if (value
&& !strcasecmp(value
, "shallow")) {
1019 thread
= THREAD_SHALLOW
;
1022 thread
= git_config_bool(var
, value
) ? THREAD_SHALLOW
: THREAD_UNSET
;
1025 if (!strcmp(var
, "format.signoff")) {
1026 do_signoff
= git_config_bool(var
, value
);
1029 if (!strcmp(var
, "format.signature"))
1030 return git_config_string(&signature
, var
, value
);
1031 if (!strcmp(var
, "format.signaturefile"))
1032 return git_config_pathname(&signature_file
, var
, value
);
1033 if (!strcmp(var
, "format.coverletter")) {
1034 if (value
&& !strcasecmp(value
, "auto")) {
1035 config_cover_letter
= COVER_AUTO
;
1038 config_cover_letter
= git_config_bool(var
, value
) ? COVER_ON
: COVER_OFF
;
1041 if (!strcmp(var
, "format.outputdirectory"))
1042 return git_config_string(&config_output_directory
, var
, value
);
1043 if (!strcmp(var
, "format.useautobase")) {
1044 if (value
&& !strcasecmp(value
, "whenAble")) {
1045 auto_base
= AUTO_BASE_WHEN_ABLE
;
1048 auto_base
= git_config_bool(var
, value
) ? AUTO_BASE_ALWAYS
: AUTO_BASE_NEVER
;
1051 if (!strcmp(var
, "format.from")) {
1052 int b
= git_parse_maybe_bool(value
);
1055 from
= xstrdup(value
);
1057 from
= xstrdup(git_committer_info(IDENT_NO_DATE
));
1062 if (!strcmp(var
, "format.forceinbodyfrom")) {
1063 force_in_body_from
= git_config_bool(var
, value
);
1066 if (!strcmp(var
, "format.notes")) {
1067 int b
= git_parse_maybe_bool(value
);
1069 enable_ref_display_notes(¬es_opt
, &show_notes
, value
);
1071 enable_default_display_notes(¬es_opt
, &show_notes
);
1073 disable_display_notes(¬es_opt
, &show_notes
);
1076 if (!strcmp(var
, "format.coverfromdescription")) {
1077 cover_from_description_mode
= parse_cover_from_description(value
);
1081 return git_log_config(var
, value
, cb
);
1084 static const char *output_directory
= NULL
;
1085 static int outdir_offset
;
1087 static int open_next_file(struct commit
*commit
, const char *subject
,
1088 struct rev_info
*rev
, int quiet
)
1090 struct strbuf filename
= STRBUF_INIT
;
1092 if (output_directory
) {
1093 strbuf_addstr(&filename
, output_directory
);
1094 strbuf_complete(&filename
, '/');
1097 if (rev
->numbered_files
)
1098 strbuf_addf(&filename
, "%d", rev
->nr
);
1100 fmt_output_commit(&filename
, commit
, rev
);
1102 fmt_output_subject(&filename
, subject
, rev
);
1105 printf("%s\n", filename
.buf
+ outdir_offset
);
1107 if (!(rev
->diffopt
.file
= fopen(filename
.buf
, "w"))) {
1108 error_errno(_("cannot open patch file %s"), filename
.buf
);
1109 strbuf_release(&filename
);
1113 strbuf_release(&filename
);
1117 static void get_patch_ids(struct rev_info
*rev
, struct patch_ids
*ids
)
1119 struct rev_info check_rev
;
1120 struct commit
*commit
, *c1
, *c2
;
1121 struct object
*o1
, *o2
;
1122 unsigned flags1
, flags2
;
1124 if (rev
->pending
.nr
!= 2)
1125 die(_("need exactly one range"));
1127 o1
= rev
->pending
.objects
[0].item
;
1128 o2
= rev
->pending
.objects
[1].item
;
1131 c1
= lookup_commit_reference(the_repository
, &o1
->oid
);
1132 c2
= lookup_commit_reference(the_repository
, &o2
->oid
);
1134 if ((flags1
& UNINTERESTING
) == (flags2
& UNINTERESTING
))
1135 die(_("not a range"));
1137 init_patch_ids(the_repository
, ids
);
1139 /* given a range a..b get all patch ids for b..a */
1140 repo_init_revisions(the_repository
, &check_rev
, rev
->prefix
);
1141 check_rev
.max_parents
= 1;
1142 o1
->flags
^= UNINTERESTING
;
1143 o2
->flags
^= UNINTERESTING
;
1144 add_pending_object(&check_rev
, o1
, "o1");
1145 add_pending_object(&check_rev
, o2
, "o2");
1146 if (prepare_revision_walk(&check_rev
))
1147 die(_("revision walk setup failed"));
1149 while ((commit
= get_revision(&check_rev
)) != NULL
) {
1150 add_commit_patch_id(commit
, ids
);
1153 /* reset for next revision walk */
1154 clear_commit_marks(c1
, SEEN
| UNINTERESTING
| SHOWN
| ADDED
);
1155 clear_commit_marks(c2
, SEEN
| UNINTERESTING
| SHOWN
| ADDED
);
1160 static void gen_message_id(struct rev_info
*info
, char *base
)
1162 struct strbuf buf
= STRBUF_INIT
;
1163 strbuf_addf(&buf
, "%s.%"PRItime
".git.%s", base
,
1164 (timestamp_t
) time(NULL
),
1165 git_committer_info(IDENT_NO_NAME
|IDENT_NO_DATE
|IDENT_STRICT
));
1166 info
->message_id
= strbuf_detach(&buf
, NULL
);
1169 static void print_signature(FILE *file
)
1171 if (!signature
|| !*signature
)
1174 fprintf(file
, "-- \n%s", signature
);
1175 if (signature
[strlen(signature
)-1] != '\n')
1180 static char *find_branch_name(struct rev_info
*rev
)
1182 int i
, positive
= -1;
1183 struct object_id branch_oid
;
1184 const struct object_id
*tip_oid
;
1185 const char *ref
, *v
;
1186 char *full_ref
, *branch
= NULL
;
1188 for (i
= 0; i
< rev
->cmdline
.nr
; i
++) {
1189 if (rev
->cmdline
.rev
[i
].flags
& UNINTERESTING
)
1198 ref
= rev
->cmdline
.rev
[positive
].name
;
1199 tip_oid
= &rev
->cmdline
.rev
[positive
].item
->oid
;
1200 if (dwim_ref(ref
, strlen(ref
), &branch_oid
, &full_ref
, 0) &&
1201 skip_prefix(full_ref
, "refs/heads/", &v
) &&
1202 oideq(tip_oid
, &branch_oid
))
1203 branch
= xstrdup(v
);
1208 static void show_diffstat(struct rev_info
*rev
,
1209 struct commit
*origin
, struct commit
*head
)
1211 struct diff_options opts
;
1213 memcpy(&opts
, &rev
->diffopt
, sizeof(opts
));
1214 opts
.output_format
= DIFF_FORMAT_SUMMARY
| DIFF_FORMAT_DIFFSTAT
;
1215 diff_setup_done(&opts
);
1217 diff_tree_oid(get_commit_tree_oid(origin
),
1218 get_commit_tree_oid(head
),
1220 diffcore_std(&opts
);
1223 fprintf(rev
->diffopt
.file
, "\n");
1226 static void prepare_cover_text(struct pretty_print_context
*pp
,
1227 const char *branch_name
,
1229 const char *encoding
,
1232 const char *subject
= "*** SUBJECT HERE ***";
1233 const char *body
= "*** BLURB HERE ***";
1234 struct strbuf description_sb
= STRBUF_INIT
;
1235 struct strbuf subject_sb
= STRBUF_INIT
;
1237 if (cover_from_description_mode
== COVER_FROM_NONE
)
1240 if (branch_name
&& *branch_name
)
1241 read_branch_desc(&description_sb
, branch_name
);
1242 if (!description_sb
.len
)
1245 if (cover_from_description_mode
== COVER_FROM_SUBJECT
||
1246 cover_from_description_mode
== COVER_FROM_AUTO
)
1247 body
= format_subject(&subject_sb
, description_sb
.buf
, " ");
1249 if (cover_from_description_mode
== COVER_FROM_MESSAGE
||
1250 (cover_from_description_mode
== COVER_FROM_AUTO
&&
1251 subject_sb
.len
> COVER_FROM_AUTO_MAX_SUBJECT_LEN
))
1252 body
= description_sb
.buf
;
1254 subject
= subject_sb
.buf
;
1257 pp_title_line(pp
, &subject
, sb
, encoding
, need_8bit_cte
);
1258 pp_remainder(pp
, &body
, sb
, 0);
1260 strbuf_release(&description_sb
);
1261 strbuf_release(&subject_sb
);
1264 static int get_notes_refs(struct string_list_item
*item
, void *arg
)
1266 strvec_pushf(arg
, "--notes=%s", item
->string
);
1270 static void get_notes_args(struct strvec
*arg
, struct rev_info
*rev
)
1272 if (!rev
->show_notes
) {
1273 strvec_push(arg
, "--no-notes");
1274 } else if (rev
->notes_opt
.use_default_notes
> 0 ||
1275 (rev
->notes_opt
.use_default_notes
== -1 &&
1276 !rev
->notes_opt
.extra_notes_refs
.nr
)) {
1277 strvec_push(arg
, "--notes");
1279 for_each_string_list(&rev
->notes_opt
.extra_notes_refs
, get_notes_refs
, arg
);
1283 static void make_cover_letter(struct rev_info
*rev
, int use_separate_file
,
1284 struct commit
*origin
,
1285 int nr
, struct commit
**list
,
1286 const char *branch_name
,
1289 const char *committer
;
1290 struct shortlog log
;
1291 struct strbuf sb
= STRBUF_INIT
;
1293 const char *encoding
= "UTF-8";
1294 int need_8bit_cte
= 0;
1295 struct pretty_print_context pp
= {0};
1296 struct commit
*head
= list
[0];
1298 if (!cmit_fmt_is_mail(rev
->commit_format
))
1299 die(_("cover letter needs email format"));
1301 committer
= git_committer_info(0);
1303 if (use_separate_file
&&
1304 open_next_file(NULL
, rev
->numbered_files
? NULL
: "cover-letter", rev
, quiet
))
1305 die(_("failed to create cover-letter file"));
1307 log_write_email_headers(rev
, head
, &pp
.after_subject
, &need_8bit_cte
, 0);
1309 for (i
= 0; !need_8bit_cte
&& i
< nr
; i
++) {
1310 const char *buf
= get_commit_buffer(list
[i
], NULL
);
1311 if (has_non_ascii(buf
))
1313 unuse_commit_buffer(list
[i
], buf
);
1317 branch_name
= find_branch_name(rev
);
1319 pp
.fmt
= CMIT_FMT_EMAIL
;
1320 pp
.date_mode
.type
= DATE_RFC2822
;
1322 pp
.print_email_subject
= 1;
1323 pp_user_info(&pp
, NULL
, &sb
, committer
, encoding
);
1324 prepare_cover_text(&pp
, branch_name
, &sb
, encoding
, need_8bit_cte
);
1325 fprintf(rev
->diffopt
.file
, "%s\n", sb
.buf
);
1327 strbuf_release(&sb
);
1329 shortlog_init(&log
);
1331 log
.wrap
= MAIL_DEFAULT_WRAP
;
1334 log
.file
= rev
->diffopt
.file
;
1335 log
.groups
= SHORTLOG_GROUP_AUTHOR
;
1336 shortlog_finish_setup(&log
);
1337 for (i
= 0; i
< nr
; i
++)
1338 shortlog_add_commit(&log
, list
[i
]);
1340 shortlog_output(&log
);
1342 /* We can only do diffstat with a unique reference point */
1344 show_diffstat(rev
, origin
, head
);
1346 if (rev
->idiff_oid1
) {
1347 fprintf_ln(rev
->diffopt
.file
, "%s", rev
->idiff_title
);
1348 show_interdiff(rev
->idiff_oid1
, rev
->idiff_oid2
, 0,
1354 * Pass minimum required diff-options to range-diff; others
1355 * can be added later if deemed desirable.
1357 struct diff_options opts
;
1358 struct strvec other_arg
= STRVEC_INIT
;
1359 struct range_diff_options range_diff_opts
= {
1360 .creation_factor
= rev
->creation_factor
,
1363 .other_arg
= &other_arg
1367 opts
.file
= rev
->diffopt
.file
;
1368 opts
.use_color
= rev
->diffopt
.use_color
;
1369 diff_setup_done(&opts
);
1370 fprintf_ln(rev
->diffopt
.file
, "%s", rev
->rdiff_title
);
1371 get_notes_args(&other_arg
, rev
);
1372 show_range_diff(rev
->rdiff1
, rev
->rdiff2
, &range_diff_opts
);
1373 strvec_clear(&other_arg
);
1377 static const char *clean_message_id(const char *msg_id
)
1380 const char *a
, *z
, *m
;
1383 while ((ch
= *m
) && (isspace(ch
) || (ch
== '<')))
1388 if (!isspace(ch
) && (ch
!= '>'))
1393 die(_("insane in-reply-to: %s"), msg_id
);
1396 return xmemdupz(a
, z
- a
);
1399 static const char *set_outdir(const char *prefix
, const char *output_directory
)
1401 if (output_directory
&& is_absolute_path(output_directory
))
1402 return output_directory
;
1404 if (!prefix
|| !*prefix
) {
1405 if (output_directory
)
1406 return output_directory
;
1407 /* The user did not explicitly ask for "./" */
1412 outdir_offset
= strlen(prefix
);
1413 if (!output_directory
)
1416 return prefix_filename(prefix
, output_directory
);
1419 static const char * const builtin_format_patch_usage
[] = {
1420 N_("git format-patch [<options>] [<since> | <revision-range>]"),
1424 static int keep_subject
= 0;
1426 static int keep_callback(const struct option
*opt
, const char *arg
, int unset
)
1428 BUG_ON_OPT_NEG(unset
);
1429 BUG_ON_OPT_ARG(arg
);
1430 ((struct rev_info
*)opt
->value
)->total
= -1;
1435 static int subject_prefix
= 0;
1437 static int subject_prefix_callback(const struct option
*opt
, const char *arg
,
1440 BUG_ON_OPT_NEG(unset
);
1442 ((struct rev_info
*)opt
->value
)->subject_prefix
= arg
;
1446 static int rfc_callback(const struct option
*opt
, const char *arg
, int unset
)
1448 BUG_ON_OPT_NEG(unset
);
1449 BUG_ON_OPT_ARG(arg
);
1450 return subject_prefix_callback(opt
, "RFC PATCH", unset
);
1453 static int numbered_cmdline_opt
= 0;
1455 static int numbered_callback(const struct option
*opt
, const char *arg
,
1458 BUG_ON_OPT_ARG(arg
);
1459 *(int *)opt
->value
= numbered_cmdline_opt
= unset
? 0 : 1;
1465 static int no_numbered_callback(const struct option
*opt
, const char *arg
,
1468 BUG_ON_OPT_NEG(unset
);
1469 return numbered_callback(opt
, arg
, 1);
1472 static int output_directory_callback(const struct option
*opt
, const char *arg
,
1475 const char **dir
= (const char **)opt
->value
;
1476 BUG_ON_OPT_NEG(unset
);
1478 die(_("two output directories?"));
1483 static int thread_callback(const struct option
*opt
, const char *arg
, int unset
)
1485 enum thread_level
*thread
= (enum thread_level
*)opt
->value
;
1487 *thread
= THREAD_UNSET
;
1488 else if (!arg
|| !strcmp(arg
, "shallow"))
1489 *thread
= THREAD_SHALLOW
;
1490 else if (!strcmp(arg
, "deep"))
1491 *thread
= THREAD_DEEP
;
1493 * Please update _git_formatpatch() in git-completion.bash
1494 * when you add new options.
1501 static int attach_callback(const struct option
*opt
, const char *arg
, int unset
)
1503 struct rev_info
*rev
= (struct rev_info
*)opt
->value
;
1505 rev
->mime_boundary
= NULL
;
1507 rev
->mime_boundary
= arg
;
1509 rev
->mime_boundary
= git_version_string
;
1510 rev
->no_inline
= unset
? 0 : 1;
1514 static int inline_callback(const struct option
*opt
, const char *arg
, int unset
)
1516 struct rev_info
*rev
= (struct rev_info
*)opt
->value
;
1518 rev
->mime_boundary
= NULL
;
1520 rev
->mime_boundary
= arg
;
1522 rev
->mime_boundary
= git_version_string
;
1527 static int header_callback(const struct option
*opt
, const char *arg
, int unset
)
1530 string_list_clear(&extra_hdr
, 0);
1531 string_list_clear(&extra_to
, 0);
1532 string_list_clear(&extra_cc
, 0);
1539 static int to_callback(const struct option
*opt
, const char *arg
, int unset
)
1542 string_list_clear(&extra_to
, 0);
1544 string_list_append(&extra_to
, arg
);
1548 static int cc_callback(const struct option
*opt
, const char *arg
, int unset
)
1551 string_list_clear(&extra_cc
, 0);
1553 string_list_append(&extra_cc
, arg
);
1557 static int from_callback(const struct option
*opt
, const char *arg
, int unset
)
1559 char **from
= opt
->value
;
1566 *from
= xstrdup(arg
);
1568 *from
= xstrdup(git_committer_info(IDENT_NO_DATE
));
1572 static int base_callback(const struct option
*opt
, const char *arg
, int unset
)
1574 const char **base_commit
= opt
->value
;
1577 auto_base
= AUTO_BASE_NEVER
;
1578 *base_commit
= NULL
;
1579 } else if (!strcmp(arg
, "auto")) {
1580 auto_base
= AUTO_BASE_ALWAYS
;
1581 *base_commit
= NULL
;
1583 auto_base
= AUTO_BASE_NEVER
;
1589 struct base_tree_info
{
1590 struct object_id base_commit
;
1591 int nr_patch_id
, alloc_patch_id
;
1592 struct object_id
*patch_id
;
1595 static struct commit
*get_base_commit(const char *base_commit
,
1596 struct commit
**list
,
1599 struct commit
*base
= NULL
;
1600 struct commit
**rev
;
1601 int i
= 0, rev_nr
= 0, auto_select
, die_on_failure
;
1603 switch (auto_base
) {
1604 case AUTO_BASE_NEVER
:
1609 /* no base information is requested */
1613 case AUTO_BASE_ALWAYS
:
1614 case AUTO_BASE_WHEN_ABLE
:
1616 BUG("requested automatic base selection but a commit was provided");
1619 die_on_failure
= auto_base
== AUTO_BASE_ALWAYS
;
1623 BUG("unexpected automatic base selection method");
1627 base
= lookup_commit_reference_by_name(base_commit
);
1629 die(_("unknown commit %s"), base_commit
);
1631 struct branch
*curr_branch
= branch_get(NULL
);
1632 const char *upstream
= branch_get_upstream(curr_branch
, NULL
);
1634 struct commit_list
*base_list
;
1635 struct commit
*commit
;
1636 struct object_id oid
;
1638 if (get_oid(upstream
, &oid
)) {
1640 die(_("failed to resolve '%s' as a valid ref"), upstream
);
1644 commit
= lookup_commit_or_die(&oid
, "upstream base");
1645 base_list
= get_merge_bases_many(commit
, total
, list
);
1646 /* There should be one and only one merge base. */
1647 if (!base_list
|| base_list
->next
) {
1648 if (die_on_failure
) {
1649 die(_("could not find exact merge base"));
1651 free_commit_list(base_list
);
1655 base
= base_list
->item
;
1656 free_commit_list(base_list
);
1659 die(_("failed to get upstream, if you want to record base commit automatically,\n"
1660 "please use git branch --set-upstream-to to track a remote branch.\n"
1661 "Or you could specify base commit by --base=<base-commit-id> manually"));
1667 ALLOC_ARRAY(rev
, total
);
1668 for (i
= 0; i
< total
; i
++)
1673 * Get merge base through pair-wise computations
1674 * and store it in rev[0].
1676 while (rev_nr
> 1) {
1677 for (i
= 0; i
< rev_nr
/ 2; i
++) {
1678 struct commit_list
*merge_base
;
1679 merge_base
= get_merge_bases(rev
[2 * i
], rev
[2 * i
+ 1]);
1680 if (!merge_base
|| merge_base
->next
) {
1681 if (die_on_failure
) {
1682 die(_("failed to find exact merge base"));
1689 rev
[i
] = merge_base
->item
;
1693 rev
[i
] = rev
[2 * i
];
1694 rev_nr
= DIV_ROUND_UP(rev_nr
, 2);
1697 if (!in_merge_bases(base
, rev
[0])) {
1698 if (die_on_failure
) {
1699 die(_("base commit should be the ancestor of revision list"));
1706 for (i
= 0; i
< total
; i
++) {
1707 if (base
== list
[i
]) {
1708 if (die_on_failure
) {
1709 die(_("base commit shouldn't be in revision list"));
1721 define_commit_slab(commit_base
, int);
1723 static void prepare_bases(struct base_tree_info
*bases
,
1724 struct commit
*base
,
1725 struct commit
**list
,
1728 struct commit
*commit
;
1729 struct rev_info revs
;
1730 struct diff_options diffopt
;
1731 struct commit_base commit_base
;
1737 init_commit_base(&commit_base
);
1738 repo_diff_setup(the_repository
, &diffopt
);
1739 diffopt
.flags
.recursive
= 1;
1740 diff_setup_done(&diffopt
);
1742 oidcpy(&bases
->base_commit
, &base
->object
.oid
);
1744 repo_init_revisions(the_repository
, &revs
, NULL
);
1745 revs
.max_parents
= 1;
1746 revs
.topo_order
= 1;
1747 for (i
= 0; i
< total
; i
++) {
1748 list
[i
]->object
.flags
&= ~UNINTERESTING
;
1749 add_pending_object(&revs
, &list
[i
]->object
, "rev_list");
1750 *commit_base_at(&commit_base
, list
[i
]) = 1;
1752 base
->object
.flags
|= UNINTERESTING
;
1753 add_pending_object(&revs
, &base
->object
, "base");
1755 if (prepare_revision_walk(&revs
))
1756 die(_("revision walk setup failed"));
1758 * Traverse the commits list, get prerequisite patch ids
1759 * and stuff them in bases structure.
1761 while ((commit
= get_revision(&revs
)) != NULL
) {
1762 struct object_id oid
;
1763 struct object_id
*patch_id
;
1764 if (*commit_base_at(&commit_base
, commit
))
1766 if (commit_patch_id(commit
, &diffopt
, &oid
, 0))
1767 die(_("cannot get patch id"));
1768 ALLOC_GROW(bases
->patch_id
, bases
->nr_patch_id
+ 1, bases
->alloc_patch_id
);
1769 patch_id
= bases
->patch_id
+ bases
->nr_patch_id
;
1770 oidcpy(patch_id
, &oid
);
1771 bases
->nr_patch_id
++;
1773 clear_commit_base(&commit_base
);
1776 static void print_bases(struct base_tree_info
*bases
, FILE *file
)
1780 /* Only do this once, either for the cover or for the first one */
1781 if (is_null_oid(&bases
->base_commit
))
1784 /* Show the base commit */
1785 fprintf(file
, "\nbase-commit: %s\n", oid_to_hex(&bases
->base_commit
));
1787 /* Show the prerequisite patches */
1788 for (i
= bases
->nr_patch_id
- 1; i
>= 0; i
--)
1789 fprintf(file
, "prerequisite-patch-id: %s\n", oid_to_hex(&bases
->patch_id
[i
]));
1791 free(bases
->patch_id
);
1792 bases
->nr_patch_id
= 0;
1793 bases
->alloc_patch_id
= 0;
1794 oidclr(&bases
->base_commit
);
1797 static const char *diff_title(struct strbuf
*sb
,
1798 const char *reroll_count
,
1799 const char *generic
,
1800 const char *rerolled
)
1804 /* RFC may be v0, so allow -v1 to diff against v0 */
1805 if (reroll_count
&& !strtol_i(reroll_count
, 10, &v
) &&
1807 strbuf_addf(sb
, rerolled
, v
- 1);
1809 strbuf_addstr(sb
, generic
);
1813 static void infer_range_diff_ranges(struct strbuf
*r1
,
1816 struct commit
*origin
,
1817 struct commit
*head
)
1819 const char *head_oid
= oid_to_hex(&head
->object
.oid
);
1820 int prev_is_range
= is_range_diff_range(prev
);
1823 strbuf_addstr(r1
, prev
);
1825 strbuf_addf(r1
, "%s..%s", head_oid
, prev
);
1828 strbuf_addf(r2
, "%s..%s", oid_to_hex(&origin
->object
.oid
), head_oid
);
1829 else if (prev_is_range
)
1830 die(_("failed to infer range-diff origin of current series"));
1832 warning(_("using '%s' as range-diff origin of current series"), prev
);
1833 strbuf_addf(r2
, "%s..%s", prev
, head_oid
);
1837 int cmd_format_patch(int argc
, const char **argv
, const char *prefix
)
1839 struct commit
*commit
;
1840 struct commit
**list
= NULL
;
1841 struct rev_info rev
;
1842 char *to_free
= NULL
;
1843 struct setup_revision_opt s_r_opt
;
1844 int nr
= 0, total
, i
;
1846 int start_number
= -1;
1847 int just_numbers
= 0;
1848 int ignore_if_in_upstream
= 0;
1849 int cover_letter
= -1;
1850 int boundary_count
= 0;
1851 int no_binary_diff
= 0;
1852 int zero_commit
= 0;
1853 struct commit
*origin
= NULL
;
1854 const char *in_reply_to
= NULL
;
1855 struct patch_ids ids
;
1856 struct strbuf buf
= STRBUF_INIT
;
1857 int use_patch_format
= 0;
1859 const char *reroll_count
= NULL
;
1860 char *cover_from_description_arg
= NULL
;
1861 char *branch_name
= NULL
;
1862 char *base_commit
= NULL
;
1863 struct base_tree_info bases
;
1864 struct commit
*base
;
1865 int show_progress
= 0;
1866 struct progress
*progress
= NULL
;
1867 struct oid_array idiff_prev
= OID_ARRAY_INIT
;
1868 struct strbuf idiff_title
= STRBUF_INIT
;
1869 const char *rdiff_prev
= NULL
;
1870 struct strbuf rdiff1
= STRBUF_INIT
;
1871 struct strbuf rdiff2
= STRBUF_INIT
;
1872 struct strbuf rdiff_title
= STRBUF_INIT
;
1873 int creation_factor
= -1;
1875 const struct option builtin_format_patch_options
[] = {
1876 OPT_CALLBACK_F('n', "numbered", &numbered
, NULL
,
1877 N_("use [PATCH n/m] even with a single patch"),
1878 PARSE_OPT_NOARG
, numbered_callback
),
1879 OPT_CALLBACK_F('N', "no-numbered", &numbered
, NULL
,
1880 N_("use [PATCH] even with multiple patches"),
1881 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
, no_numbered_callback
),
1882 OPT_BOOL('s', "signoff", &do_signoff
, N_("add a Signed-off-by trailer")),
1883 OPT_BOOL(0, "stdout", &use_stdout
,
1884 N_("print patches to standard out")),
1885 OPT_BOOL(0, "cover-letter", &cover_letter
,
1886 N_("generate a cover letter")),
1887 OPT_BOOL(0, "numbered-files", &just_numbers
,
1888 N_("use simple number sequence for output file names")),
1889 OPT_STRING(0, "suffix", &fmt_patch_suffix
, N_("sfx"),
1890 N_("use <sfx> instead of '.patch'")),
1891 OPT_INTEGER(0, "start-number", &start_number
,
1892 N_("start numbering patches at <n> instead of 1")),
1893 OPT_STRING('v', "reroll-count", &reroll_count
, N_("reroll-count"),
1894 N_("mark the series as Nth re-roll")),
1895 OPT_INTEGER(0, "filename-max-length", &fmt_patch_name_max
,
1896 N_("max length of output filename")),
1897 OPT_CALLBACK_F(0, "rfc", &rev
, NULL
,
1898 N_("use [RFC PATCH] instead of [PATCH]"),
1899 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
, rfc_callback
),
1900 OPT_STRING(0, "cover-from-description", &cover_from_description_arg
,
1901 N_("cover-from-description-mode"),
1902 N_("generate parts of a cover letter based on a branch's description")),
1903 OPT_CALLBACK_F(0, "subject-prefix", &rev
, N_("prefix"),
1904 N_("use [<prefix>] instead of [PATCH]"),
1905 PARSE_OPT_NONEG
, subject_prefix_callback
),
1906 OPT_CALLBACK_F('o', "output-directory", &output_directory
,
1907 N_("dir"), N_("store resulting files in <dir>"),
1908 PARSE_OPT_NONEG
, output_directory_callback
),
1909 OPT_CALLBACK_F('k', "keep-subject", &rev
, NULL
,
1910 N_("don't strip/add [PATCH]"),
1911 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
, keep_callback
),
1912 OPT_BOOL(0, "no-binary", &no_binary_diff
,
1913 N_("don't output binary diffs")),
1914 OPT_BOOL(0, "zero-commit", &zero_commit
,
1915 N_("output all-zero hash in From header")),
1916 OPT_BOOL(0, "ignore-if-in-upstream", &ignore_if_in_upstream
,
1917 N_("don't include a patch matching a commit upstream")),
1918 OPT_SET_INT_F('p', "no-stat", &use_patch_format
,
1919 N_("show patch format instead of default (patch + stat)"),
1920 1, PARSE_OPT_NONEG
),
1921 OPT_GROUP(N_("Messaging")),
1922 OPT_CALLBACK(0, "add-header", NULL
, N_("header"),
1923 N_("add email header"), header_callback
),
1924 OPT_CALLBACK(0, "to", NULL
, N_("email"), N_("add To: header"), to_callback
),
1925 OPT_CALLBACK(0, "cc", NULL
, N_("email"), N_("add Cc: header"), cc_callback
),
1926 OPT_CALLBACK_F(0, "from", &from
, N_("ident"),
1927 N_("set From address to <ident> (or committer ident if absent)"),
1928 PARSE_OPT_OPTARG
, from_callback
),
1929 OPT_STRING(0, "in-reply-to", &in_reply_to
, N_("message-id"),
1930 N_("make first mail a reply to <message-id>")),
1931 OPT_CALLBACK_F(0, "attach", &rev
, N_("boundary"),
1932 N_("attach the patch"), PARSE_OPT_OPTARG
,
1934 OPT_CALLBACK_F(0, "inline", &rev
, N_("boundary"),
1935 N_("inline the patch"),
1936 PARSE_OPT_OPTARG
| PARSE_OPT_NONEG
,
1938 OPT_CALLBACK_F(0, "thread", &thread
, N_("style"),
1939 N_("enable message threading, styles: shallow, deep"),
1940 PARSE_OPT_OPTARG
, thread_callback
),
1941 OPT_STRING(0, "signature", &signature
, N_("signature"),
1942 N_("add a signature")),
1943 OPT_CALLBACK_F(0, "base", &base_commit
, N_("base-commit"),
1944 N_("add prerequisite tree info to the patch series"),
1946 OPT_FILENAME(0, "signature-file", &signature_file
,
1947 N_("add a signature from a file")),
1948 OPT__QUIET(&quiet
, N_("don't print the patch filenames")),
1949 OPT_BOOL(0, "progress", &show_progress
,
1950 N_("show progress while generating patches")),
1951 OPT_CALLBACK(0, "interdiff", &idiff_prev
, N_("rev"),
1952 N_("show changes against <rev> in cover letter or single patch"),
1953 parse_opt_object_name
),
1954 OPT_STRING(0, "range-diff", &rdiff_prev
, N_("refspec"),
1955 N_("show changes against <refspec> in cover letter or single patch")),
1956 OPT_INTEGER(0, "creation-factor", &creation_factor
,
1957 N_("percentage by which creation is weighted")),
1958 OPT_BOOL(0, "force-in-body-from", &force_in_body_from
,
1959 N_("show in-body From: even if identical to the e-mail header")),
1963 extra_hdr
.strdup_strings
= 1;
1964 extra_to
.strdup_strings
= 1;
1965 extra_cc
.strdup_strings
= 1;
1967 init_log_defaults();
1968 init_display_notes(¬es_opt
);
1969 git_config(git_format_config
, NULL
);
1970 repo_init_revisions(the_repository
, &rev
, prefix
);
1971 git_config(grep_config
, &rev
.grep_filter
);
1973 rev
.show_notes
= show_notes
;
1974 memcpy(&rev
.notes_opt
, ¬es_opt
, sizeof(notes_opt
));
1975 rev
.commit_format
= CMIT_FMT_EMAIL
;
1976 rev
.encode_email_headers
= default_encode_email_headers
;
1977 rev
.expand_tabs_in_log_default
= 0;
1978 rev
.verbose_header
= 1;
1980 rev
.max_parents
= 1;
1981 rev
.diffopt
.flags
.recursive
= 1;
1982 rev
.diffopt
.no_free
= 1;
1983 rev
.subject_prefix
= fmt_patch_subject_prefix
;
1984 memset(&s_r_opt
, 0, sizeof(s_r_opt
));
1985 s_r_opt
.def
= "HEAD";
1986 s_r_opt
.revarg_opt
= REVARG_COMMITTISH
;
1988 if (default_attach
) {
1989 rev
.mime_boundary
= default_attach
;
1994 * Parse the arguments before setup_revisions(), or something
1995 * like "git format-patch -o a123 HEAD^.." may fail; a123 is
1996 * possibly a valid SHA1.
1998 argc
= parse_options(argc
, argv
, prefix
, builtin_format_patch_options
,
1999 builtin_format_patch_usage
,
2000 PARSE_OPT_KEEP_ARGV0
| PARSE_OPT_KEEP_UNKNOWN_OPT
|
2001 PARSE_OPT_KEEP_DASHDASH
);
2003 rev
.force_in_body_from
= force_in_body_from
;
2005 /* Make sure "0000-$sub.patch" gives non-negative length for $sub */
2006 if (fmt_patch_name_max
<= strlen("0000-") + strlen(fmt_patch_suffix
))
2007 fmt_patch_name_max
= strlen("0000-") + strlen(fmt_patch_suffix
);
2009 if (cover_from_description_arg
)
2010 cover_from_description_mode
= parse_cover_from_description(cover_from_description_arg
);
2013 struct strbuf sprefix
= STRBUF_INIT
;
2015 strbuf_addf(&sprefix
, "%s v%s",
2016 rev
.subject_prefix
, reroll_count
);
2017 rev
.reroll_count
= reroll_count
;
2018 rev
.subject_prefix
= strbuf_detach(&sprefix
, NULL
);
2021 for (i
= 0; i
< extra_hdr
.nr
; i
++) {
2022 strbuf_addstr(&buf
, extra_hdr
.items
[i
].string
);
2023 strbuf_addch(&buf
, '\n');
2027 strbuf_addstr(&buf
, "To: ");
2028 for (i
= 0; i
< extra_to
.nr
; i
++) {
2030 strbuf_addstr(&buf
, " ");
2031 strbuf_addstr(&buf
, extra_to
.items
[i
].string
);
2032 if (i
+ 1 < extra_to
.nr
)
2033 strbuf_addch(&buf
, ',');
2034 strbuf_addch(&buf
, '\n');
2038 strbuf_addstr(&buf
, "Cc: ");
2039 for (i
= 0; i
< extra_cc
.nr
; i
++) {
2041 strbuf_addstr(&buf
, " ");
2042 strbuf_addstr(&buf
, extra_cc
.items
[i
].string
);
2043 if (i
+ 1 < extra_cc
.nr
)
2044 strbuf_addch(&buf
, ',');
2045 strbuf_addch(&buf
, '\n');
2048 rev
.extra_headers
= to_free
= strbuf_detach(&buf
, NULL
);
2051 if (split_ident_line(&rev
.from_ident
, from
, strlen(from
)))
2052 die(_("invalid ident line: %s"), from
);
2055 if (start_number
< 0)
2059 * If numbered is set solely due to format.numbered in config,
2060 * and it would conflict with --keep-subject (-k) from the
2061 * command line, reset "numbered".
2063 if (numbered
&& keep_subject
&& !numbered_cmdline_opt
)
2066 if (numbered
&& keep_subject
)
2067 die(_("options '%s' and '%s' cannot be used together"), "-n", "-k");
2068 if (keep_subject
&& subject_prefix
)
2069 die(_("options '%s' and '%s' cannot be used together"), "--subject-prefix/--rfc", "-k");
2070 rev
.preserve_subject
= keep_subject
;
2072 argc
= setup_revisions(argc
, argv
, &rev
, &s_r_opt
);
2074 die(_("unrecognized argument: %s"), argv
[1]);
2076 if (rev
.diffopt
.output_format
& DIFF_FORMAT_NAME
)
2077 die(_("--name-only does not make sense"));
2078 if (rev
.diffopt
.output_format
& DIFF_FORMAT_NAME_STATUS
)
2079 die(_("--name-status does not make sense"));
2080 if (rev
.diffopt
.output_format
& DIFF_FORMAT_CHECKDIFF
)
2081 die(_("--check does not make sense"));
2082 if (rev
.remerge_diff
)
2083 die(_("--remerge-diff does not make sense"));
2085 if (!use_patch_format
&&
2086 (!rev
.diffopt
.output_format
||
2087 rev
.diffopt
.output_format
== DIFF_FORMAT_PATCH
))
2088 rev
.diffopt
.output_format
= DIFF_FORMAT_DIFFSTAT
| DIFF_FORMAT_SUMMARY
;
2089 if (!rev
.diffopt
.stat_width
)
2090 rev
.diffopt
.stat_width
= MAIL_DEFAULT_WRAP
;
2092 /* Always generate a patch */
2093 rev
.diffopt
.output_format
|= DIFF_FORMAT_PATCH
;
2095 rev
.zero_commit
= zero_commit
;
2096 rev
.patch_name_max
= fmt_patch_name_max
;
2098 if (!rev
.diffopt
.flags
.text
&& !no_binary_diff
)
2099 rev
.diffopt
.flags
.binary
= 1;
2102 load_display_notes(&rev
.notes_opt
);
2104 die_for_incompatible_opt3(use_stdout
, "--stdout",
2105 rev
.diffopt
.close_file
, "--output",
2106 !!output_directory
, "--output-directory");
2110 } else if (!rev
.diffopt
.close_file
) {
2113 if (!output_directory
)
2114 output_directory
= config_output_directory
;
2115 output_directory
= set_outdir(prefix
, output_directory
);
2117 if (rev
.diffopt
.use_color
!= GIT_COLOR_ALWAYS
)
2118 rev
.diffopt
.use_color
= GIT_COLOR_NEVER
;
2120 * We consider <outdir> as 'outside of gitdir', therefore avoid
2121 * applying adjust_shared_perm in s-c-l-d.
2123 saved
= get_shared_repository();
2124 set_shared_repository(0);
2125 switch (safe_create_leading_directories_const(output_directory
)) {
2130 die(_("could not create leading directories "
2131 "of '%s'"), output_directory
);
2133 set_shared_repository(saved
);
2134 if (mkdir(output_directory
, 0777) < 0 && errno
!= EEXIST
)
2135 die_errno(_("could not create directory '%s'"),
2139 if (rev
.pending
.nr
== 1) {
2142 if (rev
.max_count
< 0 && !rev
.show_root_diff
) {
2144 * This is traditional behaviour of "git format-patch
2145 * origin" that prepares what the origin side still
2148 rev
.pending
.objects
[0].item
->flags
|= UNINTERESTING
;
2149 add_head_to_pending(&rev
);
2153 * Otherwise, it is "format-patch -22 HEAD", and/or
2154 * "format-patch --root HEAD". The user wants
2155 * get_revision() to do the usual traversal.
2158 if (!strcmp(rev
.pending
.objects
[0].name
, "HEAD"))
2162 const char *ref
, *v
;
2163 ref
= resolve_ref_unsafe("HEAD", RESOLVE_REF_READING
,
2165 if (ref
&& skip_prefix(ref
, "refs/heads/", &v
))
2166 branch_name
= xstrdup(v
);
2168 branch_name
= xstrdup(""); /* no branch */
2173 * We cannot move this anywhere earlier because we do want to
2174 * know if --root was given explicitly from the command line.
2176 rev
.show_root_diff
= 1;
2178 if (ignore_if_in_upstream
) {
2179 /* Don't say anything if head and upstream are the same. */
2180 if (rev
.pending
.nr
== 2) {
2181 struct object_array_entry
*o
= rev
.pending
.objects
;
2182 if (oideq(&o
[0].item
->oid
, &o
[1].item
->oid
))
2185 get_patch_ids(&rev
, &ids
);
2188 if (prepare_revision_walk(&rev
))
2189 die(_("revision walk setup failed"));
2191 while ((commit
= get_revision(&rev
)) != NULL
) {
2192 if (commit
->object
.flags
& BOUNDARY
) {
2194 origin
= (boundary_count
== 1) ? commit
: NULL
;
2198 if (ignore_if_in_upstream
&& has_commit_patch_id(commit
, &ids
))
2202 REALLOC_ARRAY(list
, nr
);
2203 list
[nr
- 1] = commit
;
2209 if (cover_letter
== -1) {
2210 if (config_cover_letter
== COVER_AUTO
)
2211 cover_letter
= (total
> 1);
2213 cover_letter
= (config_cover_letter
== COVER_ON
);
2215 if (!keep_subject
&& auto_number
&& (total
> 1 || cover_letter
))
2218 rev
.total
= total
+ start_number
- 1;
2220 if (idiff_prev
.nr
) {
2221 if (!cover_letter
&& total
!= 1)
2222 die(_("--interdiff requires --cover-letter or single patch"));
2223 rev
.idiff_oid1
= &idiff_prev
.oid
[idiff_prev
.nr
- 1];
2224 rev
.idiff_oid2
= get_commit_tree_oid(list
[0]);
2225 rev
.idiff_title
= diff_title(&idiff_title
, reroll_count
,
2227 _("Interdiff against v%d:"));
2230 if (creation_factor
< 0)
2231 creation_factor
= RANGE_DIFF_CREATION_FACTOR_DEFAULT
;
2232 else if (!rdiff_prev
)
2233 die(_("the option '%s' requires '%s'"), "--creation-factor", "--range-diff");
2236 if (!cover_letter
&& total
!= 1)
2237 die(_("--range-diff requires --cover-letter or single patch"));
2239 infer_range_diff_ranges(&rdiff1
, &rdiff2
, rdiff_prev
,
2241 rev
.rdiff1
= rdiff1
.buf
;
2242 rev
.rdiff2
= rdiff2
.buf
;
2243 rev
.creation_factor
= creation_factor
;
2244 rev
.rdiff_title
= diff_title(&rdiff_title
, reroll_count
,
2246 _("Range-diff against v%d:"));
2250 ; /* --no-signature inhibits all signatures */
2251 } else if (signature
&& signature
!= git_version_string
) {
2252 ; /* non-default signature already set */
2253 } else if (signature_file
) {
2254 struct strbuf buf
= STRBUF_INIT
;
2256 if (strbuf_read_file(&buf
, signature_file
, 128) < 0)
2257 die_errno(_("unable to read signature file '%s'"), signature_file
);
2258 signature
= strbuf_detach(&buf
, NULL
);
2261 memset(&bases
, 0, sizeof(bases
));
2262 base
= get_base_commit(base_commit
, list
, nr
);
2264 reset_revision_walk();
2265 clear_object_flags(UNINTERESTING
);
2266 prepare_bases(&bases
, base
, list
, nr
);
2269 if (in_reply_to
|| thread
|| cover_letter
) {
2270 rev
.ref_message_ids
= xmalloc(sizeof(*rev
.ref_message_ids
));
2271 string_list_init_nodup(rev
.ref_message_ids
);
2274 const char *msgid
= clean_message_id(in_reply_to
);
2275 string_list_append(rev
.ref_message_ids
, msgid
);
2277 rev
.numbered_files
= just_numbers
;
2278 rev
.patch_suffix
= fmt_patch_suffix
;
2281 gen_message_id(&rev
, "cover");
2282 make_cover_letter(&rev
, !!output_directory
,
2283 origin
, nr
, list
, branch_name
, quiet
);
2284 print_bases(&bases
, rev
.diffopt
.file
);
2285 print_signature(rev
.diffopt
.file
);
2288 /* interdiff/range-diff in cover-letter; omit from patches */
2289 rev
.idiff_oid1
= NULL
;
2292 rev
.add_signoff
= do_signoff
;
2295 progress
= start_delayed_progress(_("Generating patches"), total
);
2298 display_progress(progress
, total
- nr
);
2300 rev
.nr
= total
- nr
+ (start_number
- 1);
2301 /* Make the second and subsequent mails replies to the first */
2303 /* Have we already had a message ID? */
2304 if (rev
.message_id
) {
2306 * For deep threading: make every mail
2307 * a reply to the previous one, no
2308 * matter what other options are set.
2310 * For shallow threading:
2312 * Without --cover-letter and
2313 * --in-reply-to, make every mail a
2314 * reply to the one before.
2316 * With --in-reply-to but no
2317 * --cover-letter, make every mail a
2318 * reply to the <reply-to>.
2320 * With --cover-letter, make every
2321 * mail but the cover letter a reply
2322 * to the cover letter. The cover
2323 * letter is a reply to the
2324 * --in-reply-to, if specified.
2326 if (thread
== THREAD_SHALLOW
2327 && rev
.ref_message_ids
->nr
> 0
2328 && (!cover_letter
|| rev
.nr
> 1))
2329 free(rev
.message_id
);
2331 string_list_append(rev
.ref_message_ids
,
2334 gen_message_id(&rev
, oid_to_hex(&commit
->object
.oid
));
2337 if (output_directory
&&
2338 open_next_file(rev
.numbered_files
? NULL
: commit
, NULL
, &rev
, quiet
))
2339 die(_("failed to create output files"));
2340 shown
= log_tree_commit(&rev
, commit
);
2341 free_commit_buffer(the_repository
->parsed_objects
,
2344 /* We put one extra blank line between formatted
2345 * patches and this flag is used by log-tree code
2346 * to see if it needs to emit a LF before showing
2347 * the log; when using one file per patch, we do
2348 * not want the extra blank line.
2350 if (output_directory
)
2353 print_bases(&bases
, rev
.diffopt
.file
);
2354 if (rev
.mime_boundary
)
2355 fprintf(rev
.diffopt
.file
, "\n--%s%s--\n\n\n",
2356 mime_boundary_leader
,
2359 print_signature(rev
.diffopt
.file
);
2361 if (output_directory
)
2362 fclose(rev
.diffopt
.file
);
2364 stop_progress(&progress
);
2367 string_list_clear(&extra_to
, 0);
2368 string_list_clear(&extra_cc
, 0);
2369 string_list_clear(&extra_hdr
, 0);
2370 if (ignore_if_in_upstream
)
2371 free_patch_ids(&ids
);
2374 oid_array_clear(&idiff_prev
);
2375 strbuf_release(&idiff_title
);
2376 strbuf_release(&rdiff1
);
2377 strbuf_release(&rdiff2
);
2378 strbuf_release(&rdiff_title
);
2380 if (rev
.ref_message_ids
)
2381 string_list_clear(rev
.ref_message_ids
, 0);
2382 free(rev
.ref_message_ids
);
2383 return cmd_log_deinit(0, &rev
);
2386 static int add_pending_commit(const char *arg
, struct rev_info
*revs
, int flags
)
2388 struct object_id oid
;
2389 if (get_oid(arg
, &oid
) == 0) {
2390 struct commit
*commit
= lookup_commit_reference(the_repository
,
2393 commit
->object
.flags
|= flags
;
2394 add_pending_object(revs
, &commit
->object
, arg
);
2401 static const char * const cherry_usage
[] = {
2402 N_("git cherry [-v] [<upstream> [<head> [<limit>]]]"),
2406 static void print_commit(char sign
, struct commit
*commit
, int verbose
,
2407 int abbrev
, FILE *file
)
2410 fprintf(file
, "%c %s\n", sign
,
2411 find_unique_abbrev(&commit
->object
.oid
, abbrev
));
2413 struct strbuf buf
= STRBUF_INIT
;
2414 pp_commit_easy(CMIT_FMT_ONELINE
, commit
, &buf
);
2415 fprintf(file
, "%c %s %s\n", sign
,
2416 find_unique_abbrev(&commit
->object
.oid
, abbrev
),
2418 strbuf_release(&buf
);
2422 int cmd_cherry(int argc
, const char **argv
, const char *prefix
)
2424 struct rev_info revs
;
2425 struct patch_ids ids
;
2426 struct commit
*commit
;
2427 struct commit_list
*list
= NULL
;
2428 struct branch
*current_branch
;
2429 const char *upstream
;
2430 const char *head
= "HEAD";
2431 const char *limit
= NULL
;
2432 int verbose
= 0, abbrev
= 0;
2434 struct option options
[] = {
2435 OPT__ABBREV(&abbrev
),
2436 OPT__VERBOSE(&verbose
, N_("be verbose")),
2440 argc
= parse_options(argc
, argv
, prefix
, options
, cherry_usage
, 0);
2453 current_branch
= branch_get(NULL
);
2454 upstream
= branch_get_upstream(current_branch
, NULL
);
2456 fprintf(stderr
, _("Could not find a tracked"
2457 " remote branch, please"
2458 " specify <upstream> manually.\n"));
2459 usage_with_options(cherry_usage
, options
);
2463 repo_init_revisions(the_repository
, &revs
, prefix
);
2464 revs
.max_parents
= 1;
2466 if (add_pending_commit(head
, &revs
, 0))
2467 die(_("unknown commit %s"), head
);
2468 if (add_pending_commit(upstream
, &revs
, UNINTERESTING
))
2469 die(_("unknown commit %s"), upstream
);
2471 /* Don't say anything if head and upstream are the same. */
2472 if (revs
.pending
.nr
== 2) {
2473 struct object_array_entry
*o
= revs
.pending
.objects
;
2474 if (oideq(&o
[0].item
->oid
, &o
[1].item
->oid
))
2478 get_patch_ids(&revs
, &ids
);
2480 if (limit
&& add_pending_commit(limit
, &revs
, UNINTERESTING
))
2481 die(_("unknown commit %s"), limit
);
2483 /* reverse the list of commits */
2484 if (prepare_revision_walk(&revs
))
2485 die(_("revision walk setup failed"));
2486 while ((commit
= get_revision(&revs
)) != NULL
) {
2487 commit_list_insert(commit
, &list
);
2493 commit
= list
->item
;
2494 if (has_commit_patch_id(commit
, &ids
))
2496 print_commit(sign
, commit
, verbose
, abbrev
, revs
.diffopt
.file
);
2500 free_patch_ids(&ids
);