2 * Builtin "git log" and related commands (show, whatchanged)
4 * (C) Copyright 2006 Linus Torvalds
16 #include "reflog-walk.h"
17 #include "patch-ids.h"
18 #include "run-command.h"
21 #include "string-list.h"
22 #include "parse-options.h"
25 #include "streaming.h"
28 #include "gpg-interface.h"
30 /* Set a default date-time format for git log ("log.date" config variable) */
31 static const char *default_date_mode
= NULL
;
33 static int default_abbrev_commit
;
34 static int default_show_root
= 1;
35 static int default_follow
;
36 static int default_show_signature
;
37 static int decoration_style
;
38 static int decoration_given
;
39 static int use_mailmap_config
;
40 static const char *fmt_patch_subject_prefix
= "PATCH";
41 static const char *fmt_pretty
;
43 static const char * const builtin_log_usage
[] = {
44 N_("git log [<options>] [<revision-range>] [[--] <path>...]"),
45 N_("git show [<options>] <object>..."),
49 struct line_opt_callback_data
{
52 struct string_list args
;
55 static int parse_decoration_style(const char *var
, const char *value
)
57 switch (git_config_maybe_bool(var
, value
)) {
59 return DECORATE_SHORT_REFS
;
65 if (!strcmp(value
, "full"))
66 return DECORATE_FULL_REFS
;
67 else if (!strcmp(value
, "short"))
68 return DECORATE_SHORT_REFS
;
69 else if (!strcmp(value
, "auto"))
70 return (isatty(1) || pager_in_use()) ? DECORATE_SHORT_REFS
: 0;
74 static int decorate_callback(const struct option
*opt
, const char *arg
, int unset
)
79 decoration_style
= parse_decoration_style("command line", arg
);
81 decoration_style
= DECORATE_SHORT_REFS
;
83 if (decoration_style
< 0)
84 die(_("invalid --decorate option: %s"), arg
);
91 static int log_line_range_callback(const struct option
*option
, const char *arg
, int unset
)
93 struct line_opt_callback_data
*data
= option
->value
;
98 data
->rev
->line_level_traverse
= 1;
99 string_list_append(&data
->args
, arg
);
104 static void init_log_defaults(void)
106 init_grep_defaults();
107 init_diff_ui_defaults();
110 static void cmd_log_init_defaults(struct rev_info
*rev
)
113 get_commit_format(fmt_pretty
, rev
);
115 DIFF_OPT_SET(&rev
->diffopt
, DEFAULT_FOLLOW_RENAMES
);
116 rev
->verbose_header
= 1;
117 DIFF_OPT_SET(&rev
->diffopt
, RECURSIVE
);
118 rev
->diffopt
.stat_width
= -1; /* use full terminal width */
119 rev
->diffopt
.stat_graph_width
= -1; /* respect statGraphWidth config */
120 rev
->abbrev_commit
= default_abbrev_commit
;
121 rev
->show_root_diff
= default_show_root
;
122 rev
->subject_prefix
= fmt_patch_subject_prefix
;
123 rev
->show_signature
= default_show_signature
;
124 DIFF_OPT_SET(&rev
->diffopt
, ALLOW_TEXTCONV
);
126 if (default_date_mode
)
127 parse_date_format(default_date_mode
, &rev
->date_mode
);
128 rev
->diffopt
.touched_flags
= 0;
131 static void cmd_log_init_finish(int argc
, const char **argv
, const char *prefix
,
132 struct rev_info
*rev
, struct setup_revision_opt
*opt
)
134 struct userformat_want w
;
135 int quiet
= 0, source
= 0, mailmap
= 0;
136 static struct line_opt_callback_data line_cb
= {NULL
, NULL
, STRING_LIST_INIT_DUP
};
138 const struct option builtin_log_options
[] = {
139 OPT__QUIET(&quiet
, N_("suppress diff output")),
140 OPT_BOOL(0, "source", &source
, N_("show source")),
141 OPT_BOOL(0, "use-mailmap", &mailmap
, N_("Use mail map file")),
142 { OPTION_CALLBACK
, 0, "decorate", NULL
, NULL
, N_("decorate options"),
143 PARSE_OPT_OPTARG
, decorate_callback
},
144 OPT_CALLBACK('L', NULL
, &line_cb
, "n,m:file",
145 N_("Process line range n,m in file, counting from 1"),
146 log_line_range_callback
),
151 line_cb
.prefix
= prefix
;
153 mailmap
= use_mailmap_config
;
154 argc
= parse_options(argc
, argv
, prefix
,
155 builtin_log_options
, builtin_log_usage
,
156 PARSE_OPT_KEEP_ARGV0
| PARSE_OPT_KEEP_UNKNOWN
|
157 PARSE_OPT_KEEP_DASHDASH
);
160 rev
->diffopt
.output_format
|= DIFF_FORMAT_NO_OUTPUT
;
161 argc
= setup_revisions(argc
, argv
, rev
, opt
);
163 /* Any arguments at this point are not recognized */
165 die(_("unrecognized argument: %s"), argv
[1]);
167 memset(&w
, 0, sizeof(w
));
168 userformat_find_requirements(NULL
, &w
);
170 if (!rev
->show_notes_given
&& (!rev
->pretty_given
|| w
.notes
))
173 init_display_notes(&rev
->notes_opt
);
175 if (rev
->diffopt
.pickaxe
|| rev
->diffopt
.filter
||
176 DIFF_OPT_TST(&rev
->diffopt
, FOLLOW_RENAMES
))
177 rev
->always_show_header
= 0;
180 rev
->show_source
= 1;
183 rev
->mailmap
= xcalloc(1, sizeof(struct string_list
));
184 read_mailmap(rev
->mailmap
, NULL
);
187 if (rev
->pretty_given
&& rev
->commit_format
== CMIT_FMT_RAW
) {
189 * "log --pretty=raw" is special; ignore UI oriented
190 * configuration variables such as decoration.
192 if (!decoration_given
)
193 decoration_style
= 0;
194 if (!rev
->abbrev_commit_given
)
195 rev
->abbrev_commit
= 0;
198 if (decoration_style
) {
199 rev
->show_decorations
= 1;
200 load_ref_decorations(decoration_style
);
203 if (rev
->line_level_traverse
)
204 line_log_init(rev
, line_cb
.prefix
, &line_cb
.args
);
209 static void cmd_log_init(int argc
, const char **argv
, const char *prefix
,
210 struct rev_info
*rev
, struct setup_revision_opt
*opt
)
212 cmd_log_init_defaults(rev
);
213 cmd_log_init_finish(argc
, argv
, prefix
, rev
, opt
);
217 * This gives a rough estimate for how many commits we
218 * will print out in the list.
220 static int estimate_commit_count(struct rev_info
*rev
, struct commit_list
*list
)
225 struct commit
*commit
= list
->item
;
226 unsigned int flags
= commit
->object
.flags
;
228 if (!(flags
& (TREESAME
| UNINTERESTING
)))
234 static void show_early_header(struct rev_info
*rev
, const char *stage
, int nr
)
236 if (rev
->shown_one
) {
238 if (rev
->commit_format
!= CMIT_FMT_ONELINE
)
239 putchar(rev
->diffopt
.line_termination
);
241 fprintf(rev
->diffopt
.file
, _("Final output: %d %s\n"), nr
, stage
);
244 static struct itimerval early_output_timer
;
246 static void log_show_early(struct rev_info
*revs
, struct commit_list
*list
)
248 int i
= revs
->early_output
, close_file
= revs
->diffopt
.close_file
;
251 revs
->diffopt
.close_file
= 0;
252 sort_in_topological_order(&list
, revs
->sort_order
);
254 struct commit
*commit
= list
->item
;
255 switch (simplify_commit(revs
, commit
)) {
258 int n
= estimate_commit_count(revs
, list
);
259 show_early_header(revs
, "incomplete", n
);
262 log_tree_commit(revs
, commit
);
269 fclose(revs
->diffopt
.file
);
275 /* Did we already get enough commits for the early output? */
278 fclose(revs
->diffopt
.file
);
283 * ..if no, then repeat it twice a second until we
286 * NOTE! We don't use "it_interval", because if the
287 * reader isn't listening, we want our output to be
288 * throttled by the writing, and not have the timer
289 * trigger every second even if we're blocked on a
292 early_output_timer
.it_value
.tv_sec
= 0;
293 early_output_timer
.it_value
.tv_usec
= 500000;
294 setitimer(ITIMER_REAL
, &early_output_timer
, NULL
);
297 static void early_output(int signal
)
299 show_early_output
= log_show_early
;
302 static void setup_early_output(struct rev_info
*rev
)
307 * Set up the signal handler, minimally intrusively:
308 * we only set a single volatile integer word (not
309 * using sigatomic_t - trying to avoid unnecessary
310 * system dependencies and headers), and using
313 memset(&sa
, 0, sizeof(sa
));
314 sa
.sa_handler
= early_output
;
315 sigemptyset(&sa
.sa_mask
);
316 sa
.sa_flags
= SA_RESTART
;
317 sigaction(SIGALRM
, &sa
, NULL
);
320 * If we can get the whole output in less than a
321 * tenth of a second, don't even bother doing the
322 * early-output thing..
324 * This is a one-time-only trigger.
326 early_output_timer
.it_value
.tv_sec
= 0;
327 early_output_timer
.it_value
.tv_usec
= 100000;
328 setitimer(ITIMER_REAL
, &early_output_timer
, NULL
);
331 static void finish_early_output(struct rev_info
*rev
)
333 int n
= estimate_commit_count(rev
, rev
->commits
);
334 signal(SIGALRM
, SIG_IGN
);
335 show_early_header(rev
, "done", n
);
338 static int cmd_log_walk(struct rev_info
*rev
)
340 struct commit
*commit
;
342 int saved_dcctc
= 0, close_file
= rev
->diffopt
.close_file
;
344 if (rev
->early_output
)
345 setup_early_output(rev
);
347 if (prepare_revision_walk(rev
))
348 die(_("revision walk setup failed"));
350 if (rev
->early_output
)
351 finish_early_output(rev
);
354 * For --check and --exit-code, the exit code is based on CHECK_FAILED
355 * and HAS_CHANGES being accumulated in rev->diffopt, so be careful to
356 * retain that state information if replacing rev->diffopt in this loop
358 rev
->diffopt
.close_file
= 0;
359 while ((commit
= get_revision(rev
)) != NULL
) {
360 if (!log_tree_commit(rev
, commit
) && rev
->max_count
>= 0)
362 * We decremented max_count in get_revision,
363 * but we didn't actually show the commit.
366 if (!rev
->reflog_info
) {
367 /* we allow cycles in reflog ancestry */
368 free_commit_buffer(commit
);
370 free_commit_list(commit
->parents
);
371 commit
->parents
= NULL
;
372 if (saved_nrl
< rev
->diffopt
.needed_rename_limit
)
373 saved_nrl
= rev
->diffopt
.needed_rename_limit
;
374 if (rev
->diffopt
.degraded_cc_to_c
)
377 rev
->diffopt
.degraded_cc_to_c
= saved_dcctc
;
378 rev
->diffopt
.needed_rename_limit
= saved_nrl
;
380 fclose(rev
->diffopt
.file
);
382 if (rev
->diffopt
.output_format
& DIFF_FORMAT_CHECKDIFF
&&
383 DIFF_OPT_TST(&rev
->diffopt
, CHECK_FAILED
)) {
386 return diff_result_code(&rev
->diffopt
, 0);
389 static int git_log_config(const char *var
, const char *value
, void *cb
)
391 const char *slot_name
;
393 if (!strcmp(var
, "format.pretty"))
394 return git_config_string(&fmt_pretty
, var
, value
);
395 if (!strcmp(var
, "format.subjectprefix"))
396 return git_config_string(&fmt_patch_subject_prefix
, var
, value
);
397 if (!strcmp(var
, "log.abbrevcommit")) {
398 default_abbrev_commit
= git_config_bool(var
, value
);
401 if (!strcmp(var
, "log.date"))
402 return git_config_string(&default_date_mode
, var
, value
);
403 if (!strcmp(var
, "log.decorate")) {
404 decoration_style
= parse_decoration_style(var
, value
);
405 if (decoration_style
< 0)
406 decoration_style
= 0; /* maybe warn? */
409 if (!strcmp(var
, "log.showroot")) {
410 default_show_root
= git_config_bool(var
, value
);
413 if (!strcmp(var
, "log.follow")) {
414 default_follow
= git_config_bool(var
, value
);
417 if (skip_prefix(var
, "color.decorate.", &slot_name
))
418 return parse_decorate_color_config(var
, slot_name
, value
);
419 if (!strcmp(var
, "log.mailmap")) {
420 use_mailmap_config
= git_config_bool(var
, value
);
423 if (!strcmp(var
, "log.showsignature")) {
424 default_show_signature
= git_config_bool(var
, value
);
428 if (grep_config(var
, value
, cb
) < 0)
430 if (git_gpg_config(var
, value
, cb
) < 0)
432 return git_diff_ui_config(var
, value
, cb
);
435 int cmd_whatchanged(int argc
, const char **argv
, const char *prefix
)
438 struct setup_revision_opt opt
;
441 git_config(git_log_config
, NULL
);
443 init_revisions(&rev
, prefix
);
445 rev
.simplify_history
= 0;
446 memset(&opt
, 0, sizeof(opt
));
448 opt
.revarg_opt
= REVARG_COMMITTISH
;
449 cmd_log_init(argc
, argv
, prefix
, &rev
, &opt
);
450 if (!rev
.diffopt
.output_format
)
451 rev
.diffopt
.output_format
= DIFF_FORMAT_RAW
;
452 return cmd_log_walk(&rev
);
455 static void show_tagger(char *buf
, int len
, struct rev_info
*rev
)
457 struct strbuf out
= STRBUF_INIT
;
458 struct pretty_print_context pp
= {0};
460 pp
.fmt
= rev
->commit_format
;
461 pp
.date_mode
= rev
->date_mode
;
462 pp_user_info(&pp
, "Tagger", &out
, buf
, get_log_output_encoding());
463 fprintf(rev
->diffopt
.file
, "%s", out
.buf
);
464 strbuf_release(&out
);
467 static int show_blob_object(const struct object_id
*oid
, struct rev_info
*rev
, const char *obj_name
)
469 struct object_id oidc
;
470 struct object_context obj_context
;
474 fflush(rev
->diffopt
.file
);
475 if (!DIFF_OPT_TOUCHED(&rev
->diffopt
, ALLOW_TEXTCONV
) ||
476 !DIFF_OPT_TST(&rev
->diffopt
, ALLOW_TEXTCONV
))
477 return stream_blob_to_fd(1, oid
, NULL
, 0);
479 if (get_sha1_with_context(obj_name
, 0, oidc
.hash
, &obj_context
))
480 die(_("Not a valid object name %s"), obj_name
);
481 if (!obj_context
.path
[0] ||
482 !textconv_object(obj_context
.path
, obj_context
.mode
, &oidc
, 1, &buf
, &size
))
483 return stream_blob_to_fd(1, oid
, NULL
, 0);
486 die(_("git show %s: bad file"), obj_name
);
488 write_or_die(1, buf
, size
);
492 static int show_tag_object(const struct object_id
*oid
, struct rev_info
*rev
)
495 enum object_type type
;
496 char *buf
= read_sha1_file(oid
->hash
, &type
, &size
);
500 return error(_("Could not read object %s"), oid_to_hex(oid
));
502 assert(type
== OBJ_TAG
);
503 while (offset
< size
&& buf
[offset
] != '\n') {
504 int new_offset
= offset
+ 1;
505 while (new_offset
< size
&& buf
[new_offset
++] != '\n')
507 if (starts_with(buf
+ offset
, "tagger "))
508 show_tagger(buf
+ offset
+ 7,
509 new_offset
- offset
- 7, rev
);
514 fwrite(buf
+ offset
, size
- offset
, 1, rev
->diffopt
.file
);
519 static int show_tree_object(const unsigned char *sha1
,
521 const char *pathname
, unsigned mode
, int stage
, void *context
)
523 FILE *file
= context
;
524 fprintf(file
, "%s%s\n", pathname
, S_ISDIR(mode
) ? "/" : "");
528 static void show_setup_revisions_tweak(struct rev_info
*rev
,
529 struct setup_revision_opt
*opt
)
531 if (rev
->ignore_merges
) {
532 /* There was no "-m" on the command line */
533 rev
->ignore_merges
= 0;
534 if (!rev
->first_parent_only
&& !rev
->combine_merges
) {
535 /* No "--first-parent", "-c", or "--cc" */
536 rev
->combine_merges
= 1;
537 rev
->dense_combined_merges
= 1;
540 if (!rev
->diffopt
.output_format
)
541 rev
->diffopt
.output_format
= DIFF_FORMAT_PATCH
;
544 int cmd_show(int argc
, const char **argv
, const char *prefix
)
547 struct object_array_entry
*objects
;
548 struct setup_revision_opt opt
;
549 struct pathspec match_all
;
550 int i
, count
, ret
= 0;
553 git_config(git_log_config
, NULL
);
555 memset(&match_all
, 0, sizeof(match_all
));
556 init_revisions(&rev
, prefix
);
558 rev
.always_show_header
= 1;
559 rev
.no_walk
= REVISION_WALK_NO_WALK_SORTED
;
560 rev
.diffopt
.stat_width
= -1; /* Scale to real terminal size */
562 memset(&opt
, 0, sizeof(opt
));
564 opt
.tweak
= show_setup_revisions_tweak
;
565 cmd_log_init(argc
, argv
, prefix
, &rev
, &opt
);
568 return cmd_log_walk(&rev
);
570 count
= rev
.pending
.nr
;
571 objects
= rev
.pending
.objects
;
572 for (i
= 0; i
< count
&& !ret
; i
++) {
573 struct object
*o
= objects
[i
].item
;
574 const char *name
= objects
[i
].name
;
577 ret
= show_blob_object(&o
->oid
, &rev
, name
);
580 struct tag
*t
= (struct tag
*)o
;
584 fprintf(rev
.diffopt
.file
, "%stag %s%s\n",
585 diff_get_color_opt(&rev
.diffopt
, DIFF_COMMIT
),
587 diff_get_color_opt(&rev
.diffopt
, DIFF_RESET
));
588 ret
= show_tag_object(&o
->oid
, &rev
);
592 o
= parse_object(t
->tagged
->oid
.hash
);
594 ret
= error(_("Could not read object %s"),
595 oid_to_hex(&t
->tagged
->oid
));
603 fprintf(rev
.diffopt
.file
, "%stree %s%s\n\n",
604 diff_get_color_opt(&rev
.diffopt
, DIFF_COMMIT
),
606 diff_get_color_opt(&rev
.diffopt
, DIFF_RESET
));
607 read_tree_recursive((struct tree
*)o
, "", 0, 0, &match_all
,
608 show_tree_object
, rev
.diffopt
.file
);
612 rev
.pending
.nr
= rev
.pending
.alloc
= 0;
613 rev
.pending
.objects
= NULL
;
614 add_object_array(o
, name
, &rev
.pending
);
615 ret
= cmd_log_walk(&rev
);
618 ret
= error(_("Unknown type: %d"), o
->type
);
626 * This is equivalent to "git log -g --abbrev-commit --pretty=oneline"
628 int cmd_log_reflog(int argc
, const char **argv
, const char *prefix
)
631 struct setup_revision_opt opt
;
634 git_config(git_log_config
, NULL
);
636 init_revisions(&rev
, prefix
);
637 init_reflog_walk(&rev
.reflog_info
);
638 rev
.verbose_header
= 1;
639 memset(&opt
, 0, sizeof(opt
));
641 cmd_log_init_defaults(&rev
);
642 rev
.abbrev_commit
= 1;
643 rev
.commit_format
= CMIT_FMT_ONELINE
;
644 rev
.use_terminator
= 1;
645 rev
.always_show_header
= 1;
646 cmd_log_init_finish(argc
, argv
, prefix
, &rev
, &opt
);
648 return cmd_log_walk(&rev
);
651 static void log_setup_revisions_tweak(struct rev_info
*rev
,
652 struct setup_revision_opt
*opt
)
654 if (DIFF_OPT_TST(&rev
->diffopt
, DEFAULT_FOLLOW_RENAMES
) &&
655 rev
->prune_data
.nr
== 1)
656 DIFF_OPT_SET(&rev
->diffopt
, FOLLOW_RENAMES
);
658 /* Turn --cc/-c into -p --cc/-c when -p was not given */
659 if (!rev
->diffopt
.output_format
&& rev
->combine_merges
)
660 rev
->diffopt
.output_format
= DIFF_FORMAT_PATCH
;
662 /* Turn -m on when --cc/-c was given */
663 if (rev
->combine_merges
)
664 rev
->ignore_merges
= 0;
667 int cmd_log(int argc
, const char **argv
, const char *prefix
)
670 struct setup_revision_opt opt
;
673 git_config(git_log_config
, NULL
);
675 init_revisions(&rev
, prefix
);
676 rev
.always_show_header
= 1;
677 memset(&opt
, 0, sizeof(opt
));
679 opt
.revarg_opt
= REVARG_COMMITTISH
;
680 opt
.tweak
= log_setup_revisions_tweak
;
681 cmd_log_init(argc
, argv
, prefix
, &rev
, &opt
);
682 return cmd_log_walk(&rev
);
687 static const char *fmt_patch_suffix
= ".patch";
688 static int numbered
= 0;
689 static int auto_number
= 1;
691 static char *default_attach
= NULL
;
693 static struct string_list extra_hdr
= STRING_LIST_INIT_NODUP
;
694 static struct string_list extra_to
= STRING_LIST_INIT_NODUP
;
695 static struct string_list extra_cc
= STRING_LIST_INIT_NODUP
;
697 static void add_header(const char *value
)
699 struct string_list_item
*item
;
700 int len
= strlen(value
);
701 while (len
&& value
[len
- 1] == '\n')
704 if (!strncasecmp(value
, "to: ", 4)) {
705 item
= string_list_append(&extra_to
, value
+ 4);
707 } else if (!strncasecmp(value
, "cc: ", 4)) {
708 item
= string_list_append(&extra_cc
, value
+ 4);
711 item
= string_list_append(&extra_hdr
, value
);
714 item
->string
[len
] = '\0';
717 #define THREAD_SHALLOW 1
718 #define THREAD_DEEP 2
720 static int do_signoff
;
721 static int base_auto
;
723 static const char *signature
= git_version_string
;
724 static const char *signature_file
;
725 static int config_cover_letter
;
726 static const char *config_output_directory
;
735 static int git_format_config(const char *var
, const char *value
, void *cb
)
737 if (!strcmp(var
, "format.headers")) {
739 die(_("format.headers without value"));
743 if (!strcmp(var
, "format.suffix"))
744 return git_config_string(&fmt_patch_suffix
, var
, value
);
745 if (!strcmp(var
, "format.to")) {
747 return config_error_nonbool(var
);
748 string_list_append(&extra_to
, value
);
751 if (!strcmp(var
, "format.cc")) {
753 return config_error_nonbool(var
);
754 string_list_append(&extra_cc
, value
);
757 if (!strcmp(var
, "diff.color") || !strcmp(var
, "color.diff") ||
758 !strcmp(var
, "color.ui") || !strcmp(var
, "diff.submodule")) {
761 if (!strcmp(var
, "format.numbered")) {
762 if (value
&& !strcasecmp(value
, "auto")) {
766 numbered
= git_config_bool(var
, value
);
767 auto_number
= auto_number
&& numbered
;
770 if (!strcmp(var
, "format.attach")) {
772 default_attach
= xstrdup(value
);
774 default_attach
= xstrdup(git_version_string
);
777 if (!strcmp(var
, "format.thread")) {
778 if (value
&& !strcasecmp(value
, "deep")) {
779 thread
= THREAD_DEEP
;
782 if (value
&& !strcasecmp(value
, "shallow")) {
783 thread
= THREAD_SHALLOW
;
786 thread
= git_config_bool(var
, value
) && THREAD_SHALLOW
;
789 if (!strcmp(var
, "format.signoff")) {
790 do_signoff
= git_config_bool(var
, value
);
793 if (!strcmp(var
, "format.signature"))
794 return git_config_string(&signature
, var
, value
);
795 if (!strcmp(var
, "format.signaturefile"))
796 return git_config_pathname(&signature_file
, var
, value
);
797 if (!strcmp(var
, "format.coverletter")) {
798 if (value
&& !strcasecmp(value
, "auto")) {
799 config_cover_letter
= COVER_AUTO
;
802 config_cover_letter
= git_config_bool(var
, value
) ? COVER_ON
: COVER_OFF
;
805 if (!strcmp(var
, "format.outputdirectory"))
806 return git_config_string(&config_output_directory
, var
, value
);
807 if (!strcmp(var
, "format.useautobase")) {
808 base_auto
= git_config_bool(var
, value
);
811 if (!strcmp(var
, "format.from")) {
812 int b
= git_config_maybe_bool(var
, value
);
815 from
= xstrdup(value
);
817 from
= xstrdup(git_committer_info(IDENT_NO_DATE
));
823 return git_log_config(var
, value
, cb
);
826 static const char *output_directory
= NULL
;
827 static int outdir_offset
;
829 static int open_next_file(struct commit
*commit
, const char *subject
,
830 struct rev_info
*rev
, int quiet
)
832 struct strbuf filename
= STRBUF_INIT
;
833 int suffix_len
= strlen(rev
->patch_suffix
) + 1;
835 if (output_directory
) {
836 strbuf_addstr(&filename
, output_directory
);
838 PATH_MAX
- FORMAT_PATCH_NAME_MAX
- suffix_len
)
839 return error(_("name of output directory is too long"));
840 strbuf_complete(&filename
, '/');
843 if (rev
->numbered_files
)
844 strbuf_addf(&filename
, "%d", rev
->nr
);
846 fmt_output_commit(&filename
, commit
, rev
);
848 fmt_output_subject(&filename
, subject
, rev
);
851 printf("%s\n", filename
.buf
+ outdir_offset
);
853 if ((rev
->diffopt
.file
= fopen(filename
.buf
, "w")) == NULL
)
854 return error(_("Cannot open patch file %s"), filename
.buf
);
856 strbuf_release(&filename
);
860 static void get_patch_ids(struct rev_info
*rev
, struct patch_ids
*ids
)
862 struct rev_info check_rev
;
863 struct commit
*commit
, *c1
, *c2
;
864 struct object
*o1
, *o2
;
865 unsigned flags1
, flags2
;
867 if (rev
->pending
.nr
!= 2)
868 die(_("Need exactly one range."));
870 o1
= rev
->pending
.objects
[0].item
;
871 o2
= rev
->pending
.objects
[1].item
;
874 c1
= lookup_commit_reference(o1
->oid
.hash
);
875 c2
= lookup_commit_reference(o2
->oid
.hash
);
877 if ((flags1
& UNINTERESTING
) == (flags2
& UNINTERESTING
))
878 die(_("Not a range."));
882 /* given a range a..b get all patch ids for b..a */
883 init_revisions(&check_rev
, rev
->prefix
);
884 check_rev
.max_parents
= 1;
885 o1
->flags
^= UNINTERESTING
;
886 o2
->flags
^= UNINTERESTING
;
887 add_pending_object(&check_rev
, o1
, "o1");
888 add_pending_object(&check_rev
, o2
, "o2");
889 if (prepare_revision_walk(&check_rev
))
890 die(_("revision walk setup failed"));
892 while ((commit
= get_revision(&check_rev
)) != NULL
) {
893 add_commit_patch_id(commit
, ids
);
896 /* reset for next revision walk */
897 clear_commit_marks(c1
, SEEN
| UNINTERESTING
| SHOWN
| ADDED
);
898 clear_commit_marks(c2
, SEEN
| UNINTERESTING
| SHOWN
| ADDED
);
903 static void gen_message_id(struct rev_info
*info
, char *base
)
905 struct strbuf buf
= STRBUF_INIT
;
906 strbuf_addf(&buf
, "%s.%lu.git.%s", base
,
907 (unsigned long) time(NULL
),
908 git_committer_info(IDENT_NO_NAME
|IDENT_NO_DATE
|IDENT_STRICT
));
909 info
->message_id
= strbuf_detach(&buf
, NULL
);
912 static void print_signature(FILE *file
)
914 if (!signature
|| !*signature
)
917 fprintf(file
, "-- \n%s", signature
);
918 if (signature
[strlen(signature
)-1] != '\n')
923 static void add_branch_description(struct strbuf
*buf
, const char *branch_name
)
925 struct strbuf desc
= STRBUF_INIT
;
926 if (!branch_name
|| !*branch_name
)
928 read_branch_desc(&desc
, branch_name
);
930 strbuf_addch(buf
, '\n');
931 strbuf_addbuf(buf
, &desc
);
932 strbuf_addch(buf
, '\n');
934 strbuf_release(&desc
);
937 static char *find_branch_name(struct rev_info
*rev
)
939 int i
, positive
= -1;
940 struct object_id branch_oid
;
941 const struct object_id
*tip_oid
;
943 char *full_ref
, *branch
= NULL
;
945 for (i
= 0; i
< rev
->cmdline
.nr
; i
++) {
946 if (rev
->cmdline
.rev
[i
].flags
& UNINTERESTING
)
955 ref
= rev
->cmdline
.rev
[positive
].name
;
956 tip_oid
= &rev
->cmdline
.rev
[positive
].item
->oid
;
957 if (dwim_ref(ref
, strlen(ref
), branch_oid
.hash
, &full_ref
) &&
958 skip_prefix(full_ref
, "refs/heads/", &v
) &&
959 !oidcmp(tip_oid
, &branch_oid
))
965 static void make_cover_letter(struct rev_info
*rev
, int use_stdout
,
966 struct commit
*origin
,
967 int nr
, struct commit
**list
,
968 const char *branch_name
,
971 const char *committer
;
972 const char *body
= "*** SUBJECT HERE ***\n\n*** BLURB HERE ***\n";
975 struct strbuf sb
= STRBUF_INIT
;
977 const char *encoding
= "UTF-8";
978 struct diff_options opts
;
979 int need_8bit_cte
= 0;
980 struct pretty_print_context pp
= {0};
981 struct commit
*head
= list
[0];
983 if (!cmit_fmt_is_mail(rev
->commit_format
))
984 die(_("Cover letter needs email format"));
986 committer
= git_committer_info(0);
989 open_next_file(NULL
, rev
->numbered_files
? NULL
: "cover-letter", rev
, quiet
))
992 log_write_email_headers(rev
, head
, &pp
.subject
, &pp
.after_subject
,
995 for (i
= 0; !need_8bit_cte
&& i
< nr
; i
++) {
996 const char *buf
= get_commit_buffer(list
[i
], NULL
);
997 if (has_non_ascii(buf
))
999 unuse_commit_buffer(list
[i
], buf
);
1003 branch_name
= find_branch_name(rev
);
1006 pp
.fmt
= CMIT_FMT_EMAIL
;
1007 pp
.date_mode
.type
= DATE_RFC2822
;
1008 pp_user_info(&pp
, NULL
, &sb
, committer
, encoding
);
1009 pp_title_line(&pp
, &msg
, &sb
, encoding
, need_8bit_cte
);
1010 pp_remainder(&pp
, &msg
, &sb
, 0);
1011 add_branch_description(&sb
, branch_name
);
1012 fprintf(rev
->diffopt
.file
, "%s\n", sb
.buf
);
1014 strbuf_release(&sb
);
1016 shortlog_init(&log
);
1021 log
.file
= rev
->diffopt
.file
;
1022 for (i
= 0; i
< nr
; i
++)
1023 shortlog_add_commit(&log
, list
[i
]);
1025 shortlog_output(&log
);
1028 * We can only do diffstat with a unique reference point
1033 memcpy(&opts
, &rev
->diffopt
, sizeof(opts
));
1034 opts
.output_format
= DIFF_FORMAT_SUMMARY
| DIFF_FORMAT_DIFFSTAT
;
1036 diff_setup_done(&opts
);
1038 diff_tree_sha1(origin
->tree
->object
.oid
.hash
,
1039 head
->tree
->object
.oid
.hash
,
1041 diffcore_std(&opts
);
1044 fprintf(rev
->diffopt
.file
, "\n");
1047 static const char *clean_message_id(const char *msg_id
)
1050 const char *a
, *z
, *m
;
1053 while ((ch
= *m
) && (isspace(ch
) || (ch
== '<')))
1058 if (!isspace(ch
) && (ch
!= '>'))
1063 die(_("insane in-reply-to: %s"), msg_id
);
1066 return xmemdupz(a
, z
- a
);
1069 static const char *set_outdir(const char *prefix
, const char *output_directory
)
1071 if (output_directory
&& is_absolute_path(output_directory
))
1072 return output_directory
;
1074 if (!prefix
|| !*prefix
) {
1075 if (output_directory
)
1076 return output_directory
;
1077 /* The user did not explicitly ask for "./" */
1082 outdir_offset
= strlen(prefix
);
1083 if (!output_directory
)
1086 return xstrdup(prefix_filename(prefix
, outdir_offset
,
1090 static const char * const builtin_format_patch_usage
[] = {
1091 N_("git format-patch [<options>] [<since> | <revision-range>]"),
1095 static int keep_subject
= 0;
1097 static int keep_callback(const struct option
*opt
, const char *arg
, int unset
)
1099 ((struct rev_info
*)opt
->value
)->total
= -1;
1104 static int subject_prefix
= 0;
1106 static int subject_prefix_callback(const struct option
*opt
, const char *arg
,
1110 ((struct rev_info
*)opt
->value
)->subject_prefix
= arg
;
1114 static int numbered_cmdline_opt
= 0;
1116 static int numbered_callback(const struct option
*opt
, const char *arg
,
1119 *(int *)opt
->value
= numbered_cmdline_opt
= unset
? 0 : 1;
1125 static int no_numbered_callback(const struct option
*opt
, const char *arg
,
1128 return numbered_callback(opt
, arg
, 1);
1131 static int output_directory_callback(const struct option
*opt
, const char *arg
,
1134 const char **dir
= (const char **)opt
->value
;
1136 die(_("Two output directories?"));
1141 static int thread_callback(const struct option
*opt
, const char *arg
, int unset
)
1143 int *thread
= (int *)opt
->value
;
1146 else if (!arg
|| !strcmp(arg
, "shallow"))
1147 *thread
= THREAD_SHALLOW
;
1148 else if (!strcmp(arg
, "deep"))
1149 *thread
= THREAD_DEEP
;
1155 static int attach_callback(const struct option
*opt
, const char *arg
, int unset
)
1157 struct rev_info
*rev
= (struct rev_info
*)opt
->value
;
1159 rev
->mime_boundary
= NULL
;
1161 rev
->mime_boundary
= arg
;
1163 rev
->mime_boundary
= git_version_string
;
1164 rev
->no_inline
= unset
? 0 : 1;
1168 static int inline_callback(const struct option
*opt
, const char *arg
, int unset
)
1170 struct rev_info
*rev
= (struct rev_info
*)opt
->value
;
1172 rev
->mime_boundary
= NULL
;
1174 rev
->mime_boundary
= arg
;
1176 rev
->mime_boundary
= git_version_string
;
1181 static int header_callback(const struct option
*opt
, const char *arg
, int unset
)
1184 string_list_clear(&extra_hdr
, 0);
1185 string_list_clear(&extra_to
, 0);
1186 string_list_clear(&extra_cc
, 0);
1193 static int to_callback(const struct option
*opt
, const char *arg
, int unset
)
1196 string_list_clear(&extra_to
, 0);
1198 string_list_append(&extra_to
, arg
);
1202 static int cc_callback(const struct option
*opt
, const char *arg
, int unset
)
1205 string_list_clear(&extra_cc
, 0);
1207 string_list_append(&extra_cc
, arg
);
1211 static int from_callback(const struct option
*opt
, const char *arg
, int unset
)
1213 char **from
= opt
->value
;
1220 *from
= xstrdup(arg
);
1222 *from
= xstrdup(git_committer_info(IDENT_NO_DATE
));
1226 struct base_tree_info
{
1227 struct object_id base_commit
;
1228 int nr_patch_id
, alloc_patch_id
;
1229 struct object_id
*patch_id
;
1232 static struct commit
*get_base_commit(const char *base_commit
,
1233 struct commit
**list
,
1236 struct commit
*base
= NULL
;
1237 struct commit
**rev
;
1238 int i
= 0, rev_nr
= 0;
1240 if (base_commit
&& strcmp(base_commit
, "auto")) {
1241 base
= lookup_commit_reference_by_name(base_commit
);
1243 die(_("Unknown commit %s"), base_commit
);
1244 } else if ((base_commit
&& !strcmp(base_commit
, "auto")) || base_auto
) {
1245 struct branch
*curr_branch
= branch_get(NULL
);
1246 const char *upstream
= branch_get_upstream(curr_branch
, NULL
);
1248 struct commit_list
*base_list
;
1249 struct commit
*commit
;
1250 struct object_id oid
;
1252 if (get_oid(upstream
, &oid
))
1253 die(_("Failed to resolve '%s' as a valid ref."), upstream
);
1254 commit
= lookup_commit_or_die(oid
.hash
, "upstream base");
1255 base_list
= get_merge_bases_many(commit
, total
, list
);
1256 /* There should be one and only one merge base. */
1257 if (!base_list
|| base_list
->next
)
1258 die(_("Could not find exact merge base."));
1259 base
= base_list
->item
;
1260 free_commit_list(base_list
);
1262 die(_("Failed to get upstream, if you want to record base commit automatically,\n"
1263 "please use git branch --set-upstream-to to track a remote branch.\n"
1264 "Or you could specify base commit by --base=<base-commit-id> manually."));
1268 ALLOC_ARRAY(rev
, total
);
1269 for (i
= 0; i
< total
; i
++)
1274 * Get merge base through pair-wise computations
1275 * and store it in rev[0].
1277 while (rev_nr
> 1) {
1278 for (i
= 0; i
< rev_nr
/ 2; i
++) {
1279 struct commit_list
*merge_base
;
1280 merge_base
= get_merge_bases(rev
[2 * i
], rev
[2 * i
+ 1]);
1281 if (!merge_base
|| merge_base
->next
)
1282 die(_("Failed to find exact merge base"));
1284 rev
[i
] = merge_base
->item
;
1288 rev
[i
] = rev
[2 * i
];
1289 rev_nr
= (rev_nr
+ 1) / 2;
1292 if (!in_merge_bases(base
, rev
[0]))
1293 die(_("base commit should be the ancestor of revision list"));
1295 for (i
= 0; i
< total
; i
++) {
1296 if (base
== list
[i
])
1297 die(_("base commit shouldn't be in revision list"));
1304 static void prepare_bases(struct base_tree_info
*bases
,
1305 struct commit
*base
,
1306 struct commit
**list
,
1309 struct commit
*commit
;
1310 struct rev_info revs
;
1311 struct diff_options diffopt
;
1317 diff_setup(&diffopt
);
1318 DIFF_OPT_SET(&diffopt
, RECURSIVE
);
1319 diff_setup_done(&diffopt
);
1321 oidcpy(&bases
->base_commit
, &base
->object
.oid
);
1323 init_revisions(&revs
, NULL
);
1324 revs
.max_parents
= 1;
1325 revs
.topo_order
= 1;
1326 for (i
= 0; i
< total
; i
++) {
1327 list
[i
]->object
.flags
&= ~UNINTERESTING
;
1328 add_pending_object(&revs
, &list
[i
]->object
, "rev_list");
1329 list
[i
]->util
= (void *)1;
1331 base
->object
.flags
|= UNINTERESTING
;
1332 add_pending_object(&revs
, &base
->object
, "base");
1334 if (prepare_revision_walk(&revs
))
1335 die(_("revision walk setup failed"));
1337 * Traverse the commits list, get prerequisite patch ids
1338 * and stuff them in bases structure.
1340 while ((commit
= get_revision(&revs
)) != NULL
) {
1341 struct object_id oid
;
1342 struct object_id
*patch_id
;
1345 if (commit_patch_id(commit
, &diffopt
, oid
.hash
, 0))
1346 die(_("cannot get patch id"));
1347 ALLOC_GROW(bases
->patch_id
, bases
->nr_patch_id
+ 1, bases
->alloc_patch_id
);
1348 patch_id
= bases
->patch_id
+ bases
->nr_patch_id
;
1349 oidcpy(patch_id
, &oid
);
1350 bases
->nr_patch_id
++;
1354 static void print_bases(struct base_tree_info
*bases
, FILE *file
)
1358 /* Only do this once, either for the cover or for the first one */
1359 if (is_null_oid(&bases
->base_commit
))
1362 /* Show the base commit */
1363 fprintf(file
, "\nbase-commit: %s\n", oid_to_hex(&bases
->base_commit
));
1365 /* Show the prerequisite patches */
1366 for (i
= bases
->nr_patch_id
- 1; i
>= 0; i
--)
1367 fprintf(file
, "prerequisite-patch-id: %s\n", oid_to_hex(&bases
->patch_id
[i
]));
1369 free(bases
->patch_id
);
1370 bases
->nr_patch_id
= 0;
1371 bases
->alloc_patch_id
= 0;
1372 oidclr(&bases
->base_commit
);
1375 int cmd_format_patch(int argc
, const char **argv
, const char *prefix
)
1377 struct commit
*commit
;
1378 struct commit
**list
= NULL
;
1379 struct rev_info rev
;
1380 struct setup_revision_opt s_r_opt
;
1381 int nr
= 0, total
, i
;
1383 int start_number
= -1;
1384 int just_numbers
= 0;
1385 int ignore_if_in_upstream
= 0;
1386 int cover_letter
= -1;
1387 int boundary_count
= 0;
1388 int no_binary_diff
= 0;
1389 int zero_commit
= 0;
1390 struct commit
*origin
= NULL
;
1391 const char *in_reply_to
= NULL
;
1392 struct patch_ids ids
;
1393 struct strbuf buf
= STRBUF_INIT
;
1394 int use_patch_format
= 0;
1396 int reroll_count
= -1;
1397 char *branch_name
= NULL
;
1398 char *base_commit
= NULL
;
1399 struct base_tree_info bases
;
1401 const struct option builtin_format_patch_options
[] = {
1402 { OPTION_CALLBACK
, 'n', "numbered", &numbered
, NULL
,
1403 N_("use [PATCH n/m] even with a single patch"),
1404 PARSE_OPT_NOARG
, numbered_callback
},
1405 { OPTION_CALLBACK
, 'N', "no-numbered", &numbered
, NULL
,
1406 N_("use [PATCH] even with multiple patches"),
1407 PARSE_OPT_NOARG
, no_numbered_callback
},
1408 OPT_BOOL('s', "signoff", &do_signoff
, N_("add Signed-off-by:")),
1409 OPT_BOOL(0, "stdout", &use_stdout
,
1410 N_("print patches to standard out")),
1411 OPT_BOOL(0, "cover-letter", &cover_letter
,
1412 N_("generate a cover letter")),
1413 OPT_BOOL(0, "numbered-files", &just_numbers
,
1414 N_("use simple number sequence for output file names")),
1415 OPT_STRING(0, "suffix", &fmt_patch_suffix
, N_("sfx"),
1416 N_("use <sfx> instead of '.patch'")),
1417 OPT_INTEGER(0, "start-number", &start_number
,
1418 N_("start numbering patches at <n> instead of 1")),
1419 OPT_INTEGER('v', "reroll-count", &reroll_count
,
1420 N_("mark the series as Nth re-roll")),
1421 { OPTION_CALLBACK
, 0, "subject-prefix", &rev
, N_("prefix"),
1422 N_("Use [<prefix>] instead of [PATCH]"),
1423 PARSE_OPT_NONEG
, subject_prefix_callback
},
1424 { OPTION_CALLBACK
, 'o', "output-directory", &output_directory
,
1425 N_("dir"), N_("store resulting files in <dir>"),
1426 PARSE_OPT_NONEG
, output_directory_callback
},
1427 { OPTION_CALLBACK
, 'k', "keep-subject", &rev
, NULL
,
1428 N_("don't strip/add [PATCH]"),
1429 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
, keep_callback
},
1430 OPT_BOOL(0, "no-binary", &no_binary_diff
,
1431 N_("don't output binary diffs")),
1432 OPT_BOOL(0, "zero-commit", &zero_commit
,
1433 N_("output all-zero hash in From header")),
1434 OPT_BOOL(0, "ignore-if-in-upstream", &ignore_if_in_upstream
,
1435 N_("don't include a patch matching a commit upstream")),
1436 { OPTION_SET_INT
, 'p', "no-stat", &use_patch_format
, NULL
,
1437 N_("show patch format instead of default (patch + stat)"),
1438 PARSE_OPT_NONEG
| PARSE_OPT_NOARG
, NULL
, 1},
1439 OPT_GROUP(N_("Messaging")),
1440 { OPTION_CALLBACK
, 0, "add-header", NULL
, N_("header"),
1441 N_("add email header"), 0, header_callback
},
1442 { OPTION_CALLBACK
, 0, "to", NULL
, N_("email"), N_("add To: header"),
1444 { OPTION_CALLBACK
, 0, "cc", NULL
, N_("email"), N_("add Cc: header"),
1446 { OPTION_CALLBACK
, 0, "from", &from
, N_("ident"),
1447 N_("set From address to <ident> (or committer ident if absent)"),
1448 PARSE_OPT_OPTARG
, from_callback
},
1449 OPT_STRING(0, "in-reply-to", &in_reply_to
, N_("message-id"),
1450 N_("make first mail a reply to <message-id>")),
1451 { OPTION_CALLBACK
, 0, "attach", &rev
, N_("boundary"),
1452 N_("attach the patch"), PARSE_OPT_OPTARG
,
1454 { OPTION_CALLBACK
, 0, "inline", &rev
, N_("boundary"),
1455 N_("inline the patch"),
1456 PARSE_OPT_OPTARG
| PARSE_OPT_NONEG
,
1458 { OPTION_CALLBACK
, 0, "thread", &thread
, N_("style"),
1459 N_("enable message threading, styles: shallow, deep"),
1460 PARSE_OPT_OPTARG
, thread_callback
},
1461 OPT_STRING(0, "signature", &signature
, N_("signature"),
1462 N_("add a signature")),
1463 OPT_STRING(0, "base", &base_commit
, N_("base-commit"),
1464 N_("add prerequisite tree info to the patch series")),
1465 OPT_FILENAME(0, "signature-file", &signature_file
,
1466 N_("add a signature from a file")),
1467 OPT__QUIET(&quiet
, N_("don't print the patch filenames")),
1471 extra_hdr
.strdup_strings
= 1;
1472 extra_to
.strdup_strings
= 1;
1473 extra_cc
.strdup_strings
= 1;
1474 init_log_defaults();
1475 git_config(git_format_config
, NULL
);
1476 init_revisions(&rev
, prefix
);
1477 rev
.commit_format
= CMIT_FMT_EMAIL
;
1478 rev
.expand_tabs_in_log_default
= 0;
1479 rev
.verbose_header
= 1;
1481 rev
.max_parents
= 1;
1482 DIFF_OPT_SET(&rev
.diffopt
, RECURSIVE
);
1483 rev
.subject_prefix
= fmt_patch_subject_prefix
;
1484 memset(&s_r_opt
, 0, sizeof(s_r_opt
));
1485 s_r_opt
.def
= "HEAD";
1486 s_r_opt
.revarg_opt
= REVARG_COMMITTISH
;
1488 if (default_attach
) {
1489 rev
.mime_boundary
= default_attach
;
1494 * Parse the arguments before setup_revisions(), or something
1495 * like "git format-patch -o a123 HEAD^.." may fail; a123 is
1496 * possibly a valid SHA1.
1498 argc
= parse_options(argc
, argv
, prefix
, builtin_format_patch_options
,
1499 builtin_format_patch_usage
,
1500 PARSE_OPT_KEEP_ARGV0
| PARSE_OPT_KEEP_UNKNOWN
|
1501 PARSE_OPT_KEEP_DASHDASH
);
1503 if (0 < reroll_count
) {
1504 struct strbuf sprefix
= STRBUF_INIT
;
1505 strbuf_addf(&sprefix
, "%s v%d",
1506 rev
.subject_prefix
, reroll_count
);
1507 rev
.reroll_count
= reroll_count
;
1508 rev
.subject_prefix
= strbuf_detach(&sprefix
, NULL
);
1511 for (i
= 0; i
< extra_hdr
.nr
; i
++) {
1512 strbuf_addstr(&buf
, extra_hdr
.items
[i
].string
);
1513 strbuf_addch(&buf
, '\n');
1517 strbuf_addstr(&buf
, "To: ");
1518 for (i
= 0; i
< extra_to
.nr
; i
++) {
1520 strbuf_addstr(&buf
, " ");
1521 strbuf_addstr(&buf
, extra_to
.items
[i
].string
);
1522 if (i
+ 1 < extra_to
.nr
)
1523 strbuf_addch(&buf
, ',');
1524 strbuf_addch(&buf
, '\n');
1528 strbuf_addstr(&buf
, "Cc: ");
1529 for (i
= 0; i
< extra_cc
.nr
; i
++) {
1531 strbuf_addstr(&buf
, " ");
1532 strbuf_addstr(&buf
, extra_cc
.items
[i
].string
);
1533 if (i
+ 1 < extra_cc
.nr
)
1534 strbuf_addch(&buf
, ',');
1535 strbuf_addch(&buf
, '\n');
1538 rev
.extra_headers
= strbuf_detach(&buf
, NULL
);
1541 if (split_ident_line(&rev
.from_ident
, from
, strlen(from
)))
1542 die(_("invalid ident line: %s"), from
);
1545 if (start_number
< 0)
1549 * If numbered is set solely due to format.numbered in config,
1550 * and it would conflict with --keep-subject (-k) from the
1551 * command line, reset "numbered".
1553 if (numbered
&& keep_subject
&& !numbered_cmdline_opt
)
1556 if (numbered
&& keep_subject
)
1557 die (_("-n and -k are mutually exclusive."));
1558 if (keep_subject
&& subject_prefix
)
1559 die (_("--subject-prefix and -k are mutually exclusive."));
1560 rev
.preserve_subject
= keep_subject
;
1562 argc
= setup_revisions(argc
, argv
, &rev
, &s_r_opt
);
1564 die (_("unrecognized argument: %s"), argv
[1]);
1566 if (rev
.diffopt
.output_format
& DIFF_FORMAT_NAME
)
1567 die(_("--name-only does not make sense"));
1568 if (rev
.diffopt
.output_format
& DIFF_FORMAT_NAME_STATUS
)
1569 die(_("--name-status does not make sense"));
1570 if (rev
.diffopt
.output_format
& DIFF_FORMAT_CHECKDIFF
)
1571 die(_("--check does not make sense"));
1573 if (!use_patch_format
&&
1574 (!rev
.diffopt
.output_format
||
1575 rev
.diffopt
.output_format
== DIFF_FORMAT_PATCH
))
1576 rev
.diffopt
.output_format
= DIFF_FORMAT_DIFFSTAT
| DIFF_FORMAT_SUMMARY
;
1578 /* Always generate a patch */
1579 rev
.diffopt
.output_format
|= DIFF_FORMAT_PATCH
;
1581 rev
.zero_commit
= zero_commit
;
1583 if (!DIFF_OPT_TST(&rev
.diffopt
, TEXT
) && !no_binary_diff
)
1584 DIFF_OPT_SET(&rev
.diffopt
, BINARY
);
1587 init_display_notes(&rev
.notes_opt
);
1589 if (!output_directory
&& !use_stdout
)
1590 output_directory
= config_output_directory
;
1593 output_directory
= set_outdir(prefix
, output_directory
);
1597 if (output_directory
) {
1598 if (rev
.diffopt
.use_color
!= GIT_COLOR_ALWAYS
)
1599 rev
.diffopt
.use_color
= GIT_COLOR_NEVER
;
1601 die(_("standard output, or directory, which one?"));
1602 if (mkdir(output_directory
, 0777) < 0 && errno
!= EEXIST
)
1603 die_errno(_("Could not create directory '%s'"),
1607 if (rev
.pending
.nr
== 1) {
1610 if (rev
.max_count
< 0 && !rev
.show_root_diff
) {
1612 * This is traditional behaviour of "git format-patch
1613 * origin" that prepares what the origin side still
1616 rev
.pending
.objects
[0].item
->flags
|= UNINTERESTING
;
1617 add_head_to_pending(&rev
);
1621 * Otherwise, it is "format-patch -22 HEAD", and/or
1622 * "format-patch --root HEAD". The user wants
1623 * get_revision() to do the usual traversal.
1626 if (!strcmp(rev
.pending
.objects
[0].name
, "HEAD"))
1630 struct object_id oid
;
1631 const char *ref
, *v
;
1632 ref
= resolve_ref_unsafe("HEAD", RESOLVE_REF_READING
,
1634 if (ref
&& skip_prefix(ref
, "refs/heads/", &v
))
1635 branch_name
= xstrdup(v
);
1637 branch_name
= xstrdup(""); /* no branch */
1642 * We cannot move this anywhere earlier because we do want to
1643 * know if --root was given explicitly from the command line.
1645 rev
.show_root_diff
= 1;
1647 if (ignore_if_in_upstream
) {
1648 /* Don't say anything if head and upstream are the same. */
1649 if (rev
.pending
.nr
== 2) {
1650 struct object_array_entry
*o
= rev
.pending
.objects
;
1651 if (oidcmp(&o
[0].item
->oid
, &o
[1].item
->oid
) == 0)
1654 get_patch_ids(&rev
, &ids
);
1657 if (prepare_revision_walk(&rev
))
1658 die(_("revision walk setup failed"));
1660 while ((commit
= get_revision(&rev
)) != NULL
) {
1661 if (commit
->object
.flags
& BOUNDARY
) {
1663 origin
= (boundary_count
== 1) ? commit
: NULL
;
1667 if (ignore_if_in_upstream
&& has_commit_patch_id(commit
, &ids
))
1671 REALLOC_ARRAY(list
, nr
);
1672 list
[nr
- 1] = commit
;
1678 if (cover_letter
== -1) {
1679 if (config_cover_letter
== COVER_AUTO
)
1680 cover_letter
= (total
> 1);
1682 cover_letter
= (config_cover_letter
== COVER_ON
);
1684 if (!keep_subject
&& auto_number
&& (total
> 1 || cover_letter
))
1687 rev
.total
= total
+ start_number
- 1;
1690 ; /* --no-signature inhibits all signatures */
1691 } else if (signature
&& signature
!= git_version_string
) {
1692 ; /* non-default signature already set */
1693 } else if (signature_file
) {
1694 struct strbuf buf
= STRBUF_INIT
;
1696 if (strbuf_read_file(&buf
, signature_file
, 128) < 0)
1697 die_errno(_("unable to read signature file '%s'"), signature_file
);
1698 signature
= strbuf_detach(&buf
, NULL
);
1701 memset(&bases
, 0, sizeof(bases
));
1702 if (base_commit
|| base_auto
) {
1703 struct commit
*base
= get_base_commit(base_commit
, list
, nr
);
1704 reset_revision_walk();
1705 prepare_bases(&bases
, base
, list
, nr
);
1708 if (in_reply_to
|| thread
|| cover_letter
)
1709 rev
.ref_message_ids
= xcalloc(1, sizeof(struct string_list
));
1711 const char *msgid
= clean_message_id(in_reply_to
);
1712 string_list_append(rev
.ref_message_ids
, msgid
);
1714 rev
.numbered_files
= just_numbers
;
1715 rev
.patch_suffix
= fmt_patch_suffix
;
1718 gen_message_id(&rev
, "cover");
1719 make_cover_letter(&rev
, use_stdout
,
1720 origin
, nr
, list
, branch_name
, quiet
);
1721 print_bases(&bases
, rev
.diffopt
.file
);
1722 print_signature(rev
.diffopt
.file
);
1726 rev
.add_signoff
= do_signoff
;
1730 rev
.nr
= total
- nr
+ (start_number
- 1);
1731 /* Make the second and subsequent mails replies to the first */
1733 /* Have we already had a message ID? */
1734 if (rev
.message_id
) {
1736 * For deep threading: make every mail
1737 * a reply to the previous one, no
1738 * matter what other options are set.
1740 * For shallow threading:
1742 * Without --cover-letter and
1743 * --in-reply-to, make every mail a
1744 * reply to the one before.
1746 * With --in-reply-to but no
1747 * --cover-letter, make every mail a
1748 * reply to the <reply-to>.
1750 * With --cover-letter, make every
1751 * mail but the cover letter a reply
1752 * to the cover letter. The cover
1753 * letter is a reply to the
1754 * --in-reply-to, if specified.
1756 if (thread
== THREAD_SHALLOW
1757 && rev
.ref_message_ids
->nr
> 0
1758 && (!cover_letter
|| rev
.nr
> 1))
1759 free(rev
.message_id
);
1761 string_list_append(rev
.ref_message_ids
,
1764 gen_message_id(&rev
, oid_to_hex(&commit
->object
.oid
));
1768 open_next_file(rev
.numbered_files
? NULL
: commit
, NULL
, &rev
, quiet
))
1769 die(_("Failed to create output files"));
1770 shown
= log_tree_commit(&rev
, commit
);
1771 free_commit_buffer(commit
);
1773 /* We put one extra blank line between formatted
1774 * patches and this flag is used by log-tree code
1775 * to see if it needs to emit a LF before showing
1776 * the log; when using one file per patch, we do
1777 * not want the extra blank line.
1782 print_bases(&bases
, rev
.diffopt
.file
);
1783 if (rev
.mime_boundary
)
1784 fprintf(rev
.diffopt
.file
, "\n--%s%s--\n\n\n",
1785 mime_boundary_leader
,
1788 print_signature(rev
.diffopt
.file
);
1791 fclose(rev
.diffopt
.file
);
1795 string_list_clear(&extra_to
, 0);
1796 string_list_clear(&extra_cc
, 0);
1797 string_list_clear(&extra_hdr
, 0);
1798 if (ignore_if_in_upstream
)
1799 free_patch_ids(&ids
);
1803 static int add_pending_commit(const char *arg
, struct rev_info
*revs
, int flags
)
1805 struct object_id oid
;
1806 if (get_oid(arg
, &oid
) == 0) {
1807 struct commit
*commit
= lookup_commit_reference(oid
.hash
);
1809 commit
->object
.flags
|= flags
;
1810 add_pending_object(revs
, &commit
->object
, arg
);
1817 static const char * const cherry_usage
[] = {
1818 N_("git cherry [-v] [<upstream> [<head> [<limit>]]]"),
1822 static void print_commit(char sign
, struct commit
*commit
, int verbose
,
1823 int abbrev
, FILE *file
)
1826 fprintf(file
, "%c %s\n", sign
,
1827 find_unique_abbrev(commit
->object
.oid
.hash
, abbrev
));
1829 struct strbuf buf
= STRBUF_INIT
;
1830 pp_commit_easy(CMIT_FMT_ONELINE
, commit
, &buf
);
1831 fprintf(file
, "%c %s %s\n", sign
,
1832 find_unique_abbrev(commit
->object
.oid
.hash
, abbrev
),
1834 strbuf_release(&buf
);
1838 int cmd_cherry(int argc
, const char **argv
, const char *prefix
)
1840 struct rev_info revs
;
1841 struct patch_ids ids
;
1842 struct commit
*commit
;
1843 struct commit_list
*list
= NULL
;
1844 struct branch
*current_branch
;
1845 const char *upstream
;
1846 const char *head
= "HEAD";
1847 const char *limit
= NULL
;
1848 int verbose
= 0, abbrev
= 0;
1850 struct option options
[] = {
1851 OPT__ABBREV(&abbrev
),
1852 OPT__VERBOSE(&verbose
, N_("be verbose")),
1856 argc
= parse_options(argc
, argv
, prefix
, options
, cherry_usage
, 0);
1869 current_branch
= branch_get(NULL
);
1870 upstream
= branch_get_upstream(current_branch
, NULL
);
1872 fprintf(stderr
, _("Could not find a tracked"
1873 " remote branch, please"
1874 " specify <upstream> manually.\n"));
1875 usage_with_options(cherry_usage
, options
);
1879 init_revisions(&revs
, prefix
);
1880 revs
.max_parents
= 1;
1882 if (add_pending_commit(head
, &revs
, 0))
1883 die(_("Unknown commit %s"), head
);
1884 if (add_pending_commit(upstream
, &revs
, UNINTERESTING
))
1885 die(_("Unknown commit %s"), upstream
);
1887 /* Don't say anything if head and upstream are the same. */
1888 if (revs
.pending
.nr
== 2) {
1889 struct object_array_entry
*o
= revs
.pending
.objects
;
1890 if (oidcmp(&o
[0].item
->oid
, &o
[1].item
->oid
) == 0)
1894 get_patch_ids(&revs
, &ids
);
1896 if (limit
&& add_pending_commit(limit
, &revs
, UNINTERESTING
))
1897 die(_("Unknown commit %s"), limit
);
1899 /* reverse the list of commits */
1900 if (prepare_revision_walk(&revs
))
1901 die(_("revision walk setup failed"));
1902 while ((commit
= get_revision(&revs
)) != NULL
) {
1903 commit_list_insert(commit
, &list
);
1909 commit
= list
->item
;
1910 if (has_commit_patch_id(commit
, &ids
))
1912 print_commit(sign
, commit
, verbose
, abbrev
, revs
.diffopt
.file
);
1916 free_patch_ids(&ids
);