2 * Builtin "git log" and related commands (show, whatchanged)
4 * (C) Copyright 2006 Linus Torvalds
15 #include "reflog-walk.h"
16 #include "patch-ids.h"
17 #include "run-command.h"
20 #include "string-list.h"
21 #include "parse-options.h"
23 #include "streaming.h"
27 /* Set a default date-time format for git log ("log.date" config variable) */
28 static const char *default_date_mode
= NULL
;
30 static int default_abbrev_commit
;
31 static int default_show_root
= 1;
32 static int decoration_style
;
33 static int decoration_given
;
34 static int use_mailmap_config
;
35 static const char *fmt_patch_subject_prefix
= "PATCH";
36 static const char *fmt_pretty
;
38 static const char * const builtin_log_usage
[] = {
39 N_("git log [<options>] [<since>..<until>] [[--] <path>...]\n")
40 N_(" or: git show [options] <object>..."),
44 static int parse_decoration_style(const char *var
, const char *value
)
46 switch (git_config_maybe_bool(var
, value
)) {
48 return DECORATE_SHORT_REFS
;
54 if (!strcmp(value
, "full"))
55 return DECORATE_FULL_REFS
;
56 else if (!strcmp(value
, "short"))
57 return DECORATE_SHORT_REFS
;
61 static int decorate_callback(const struct option
*opt
, const char *arg
, int unset
)
66 decoration_style
= parse_decoration_style("command line", arg
);
68 decoration_style
= DECORATE_SHORT_REFS
;
70 if (decoration_style
< 0)
71 die("invalid --decorate option: %s", arg
);
78 static void cmd_log_init_defaults(struct rev_info
*rev
)
81 get_commit_format(fmt_pretty
, rev
);
82 rev
->verbose_header
= 1;
83 DIFF_OPT_SET(&rev
->diffopt
, RECURSIVE
);
84 rev
->diffopt
.stat_width
= -1; /* use full terminal width */
85 rev
->diffopt
.stat_graph_width
= -1; /* respect statGraphWidth config */
86 rev
->abbrev_commit
= default_abbrev_commit
;
87 rev
->show_root_diff
= default_show_root
;
88 rev
->subject_prefix
= fmt_patch_subject_prefix
;
89 DIFF_OPT_SET(&rev
->diffopt
, ALLOW_TEXTCONV
);
91 if (default_date_mode
)
92 rev
->date_mode
= parse_date_format(default_date_mode
);
95 static void cmd_log_init_finish(int argc
, const char **argv
, const char *prefix
,
96 struct rev_info
*rev
, struct setup_revision_opt
*opt
)
98 struct userformat_want w
;
99 int quiet
= 0, source
= 0, mailmap
= 0;
101 const struct option builtin_log_options
[] = {
102 OPT_BOOLEAN(0, "quiet", &quiet
, N_("suppress diff output")),
103 OPT_BOOLEAN(0, "source", &source
, N_("show source")),
104 OPT_BOOLEAN(0, "use-mailmap", &mailmap
, N_("Use mail map file")),
105 { OPTION_CALLBACK
, 0, "decorate", NULL
, NULL
, N_("decorate options"),
106 PARSE_OPT_OPTARG
, decorate_callback
},
110 mailmap
= use_mailmap_config
;
111 argc
= parse_options(argc
, argv
, prefix
,
112 builtin_log_options
, builtin_log_usage
,
113 PARSE_OPT_KEEP_ARGV0
| PARSE_OPT_KEEP_UNKNOWN
|
114 PARSE_OPT_KEEP_DASHDASH
);
117 rev
->diffopt
.output_format
|= DIFF_FORMAT_NO_OUTPUT
;
118 argc
= setup_revisions(argc
, argv
, rev
, opt
);
120 /* Any arguments at this point are not recognized */
122 die("unrecognized argument: %s", argv
[1]);
124 memset(&w
, 0, sizeof(w
));
125 userformat_find_requirements(NULL
, &w
);
127 if (!rev
->show_notes_given
&& (!rev
->pretty_given
|| w
.notes
))
130 init_display_notes(&rev
->notes_opt
);
132 if (rev
->diffopt
.pickaxe
|| rev
->diffopt
.filter
)
133 rev
->always_show_header
= 0;
134 if (DIFF_OPT_TST(&rev
->diffopt
, FOLLOW_RENAMES
)) {
135 rev
->always_show_header
= 0;
136 if (rev
->diffopt
.pathspec
.nr
!= 1)
137 usage("git logs can only follow renames on one pathname at a time");
141 rev
->show_source
= 1;
144 rev
->mailmap
= xcalloc(1, sizeof(struct string_list
));
145 read_mailmap(rev
->mailmap
, NULL
);
148 if (rev
->pretty_given
&& rev
->commit_format
== CMIT_FMT_RAW
) {
150 * "log --pretty=raw" is special; ignore UI oriented
151 * configuration variables such as decoration.
153 if (!decoration_given
)
154 decoration_style
= 0;
155 if (!rev
->abbrev_commit_given
)
156 rev
->abbrev_commit
= 0;
159 if (decoration_style
) {
160 rev
->show_decorations
= 1;
161 load_ref_decorations(decoration_style
);
166 static void cmd_log_init(int argc
, const char **argv
, const char *prefix
,
167 struct rev_info
*rev
, struct setup_revision_opt
*opt
)
169 cmd_log_init_defaults(rev
);
170 cmd_log_init_finish(argc
, argv
, prefix
, rev
, opt
);
174 * This gives a rough estimate for how many commits we
175 * will print out in the list.
177 static int estimate_commit_count(struct rev_info
*rev
, struct commit_list
*list
)
182 struct commit
*commit
= list
->item
;
183 unsigned int flags
= commit
->object
.flags
;
185 if (!(flags
& (TREESAME
| UNINTERESTING
)))
191 static void show_early_header(struct rev_info
*rev
, const char *stage
, int nr
)
193 if (rev
->shown_one
) {
195 if (rev
->commit_format
!= CMIT_FMT_ONELINE
)
196 putchar(rev
->diffopt
.line_termination
);
198 printf(_("Final output: %d %s\n"), nr
, stage
);
201 static struct itimerval early_output_timer
;
203 static void log_show_early(struct rev_info
*revs
, struct commit_list
*list
)
205 int i
= revs
->early_output
;
208 sort_in_topological_order(&list
, revs
->lifo
);
210 struct commit
*commit
= list
->item
;
211 switch (simplify_commit(revs
, commit
)) {
214 int n
= estimate_commit_count(revs
, list
);
215 show_early_header(revs
, "incomplete", n
);
218 log_tree_commit(revs
, commit
);
229 /* Did we already get enough commits for the early output? */
234 * ..if no, then repeat it twice a second until we
237 * NOTE! We don't use "it_interval", because if the
238 * reader isn't listening, we want our output to be
239 * throttled by the writing, and not have the timer
240 * trigger every second even if we're blocked on a
243 early_output_timer
.it_value
.tv_sec
= 0;
244 early_output_timer
.it_value
.tv_usec
= 500000;
245 setitimer(ITIMER_REAL
, &early_output_timer
, NULL
);
248 static void early_output(int signal
)
250 show_early_output
= log_show_early
;
253 static void setup_early_output(struct rev_info
*rev
)
258 * Set up the signal handler, minimally intrusively:
259 * we only set a single volatile integer word (not
260 * using sigatomic_t - trying to avoid unnecessary
261 * system dependencies and headers), and using
264 memset(&sa
, 0, sizeof(sa
));
265 sa
.sa_handler
= early_output
;
266 sigemptyset(&sa
.sa_mask
);
267 sa
.sa_flags
= SA_RESTART
;
268 sigaction(SIGALRM
, &sa
, NULL
);
271 * If we can get the whole output in less than a
272 * tenth of a second, don't even bother doing the
273 * early-output thing..
275 * This is a one-time-only trigger.
277 early_output_timer
.it_value
.tv_sec
= 0;
278 early_output_timer
.it_value
.tv_usec
= 100000;
279 setitimer(ITIMER_REAL
, &early_output_timer
, NULL
);
282 static void finish_early_output(struct rev_info
*rev
)
284 int n
= estimate_commit_count(rev
, rev
->commits
);
285 signal(SIGALRM
, SIG_IGN
);
286 show_early_header(rev
, "done", n
);
289 static int cmd_log_walk(struct rev_info
*rev
)
291 struct commit
*commit
;
295 if (rev
->early_output
)
296 setup_early_output(rev
);
298 if (prepare_revision_walk(rev
))
299 die(_("revision walk setup failed"));
301 if (rev
->early_output
)
302 finish_early_output(rev
);
305 * For --check and --exit-code, the exit code is based on CHECK_FAILED
306 * and HAS_CHANGES being accumulated in rev->diffopt, so be careful to
307 * retain that state information if replacing rev->diffopt in this loop
309 while ((commit
= get_revision(rev
)) != NULL
) {
310 if (!log_tree_commit(rev
, commit
) &&
313 * We decremented max_count in get_revision,
314 * but we didn't actually show the commit.
317 if (!rev
->reflog_info
) {
318 /* we allow cycles in reflog ancestry */
319 free(commit
->buffer
);
320 commit
->buffer
= NULL
;
322 free_commit_list(commit
->parents
);
323 commit
->parents
= NULL
;
324 if (saved_nrl
< rev
->diffopt
.needed_rename_limit
)
325 saved_nrl
= rev
->diffopt
.needed_rename_limit
;
326 if (rev
->diffopt
.degraded_cc_to_c
)
329 rev
->diffopt
.degraded_cc_to_c
= saved_dcctc
;
330 rev
->diffopt
.needed_rename_limit
= saved_nrl
;
332 if (rev
->diffopt
.output_format
& DIFF_FORMAT_CHECKDIFF
&&
333 DIFF_OPT_TST(&rev
->diffopt
, CHECK_FAILED
)) {
336 return diff_result_code(&rev
->diffopt
, 0);
339 static int git_log_config(const char *var
, const char *value
, void *cb
)
341 if (!strcmp(var
, "format.pretty"))
342 return git_config_string(&fmt_pretty
, var
, value
);
343 if (!strcmp(var
, "format.subjectprefix"))
344 return git_config_string(&fmt_patch_subject_prefix
, var
, value
);
345 if (!strcmp(var
, "log.abbrevcommit")) {
346 default_abbrev_commit
= git_config_bool(var
, value
);
349 if (!strcmp(var
, "log.date"))
350 return git_config_string(&default_date_mode
, var
, value
);
351 if (!strcmp(var
, "log.decorate")) {
352 decoration_style
= parse_decoration_style(var
, value
);
353 if (decoration_style
< 0)
354 decoration_style
= 0; /* maybe warn? */
357 if (!strcmp(var
, "log.showroot")) {
358 default_show_root
= git_config_bool(var
, value
);
361 if (!prefixcmp(var
, "color.decorate."))
362 return parse_decorate_color_config(var
, 15, value
);
363 if (!strcmp(var
, "log.mailmap")) {
364 use_mailmap_config
= git_config_bool(var
, value
);
368 if (grep_config(var
, value
, cb
) < 0)
370 return git_diff_ui_config(var
, value
, cb
);
373 int cmd_whatchanged(int argc
, const char **argv
, const char *prefix
)
376 struct setup_revision_opt opt
;
378 init_grep_defaults();
379 git_config(git_log_config
, NULL
);
381 init_revisions(&rev
, prefix
);
383 rev
.simplify_history
= 0;
384 memset(&opt
, 0, sizeof(opt
));
386 opt
.revarg_opt
= REVARG_COMMITTISH
;
387 cmd_log_init(argc
, argv
, prefix
, &rev
, &opt
);
388 if (!rev
.diffopt
.output_format
)
389 rev
.diffopt
.output_format
= DIFF_FORMAT_RAW
;
390 return cmd_log_walk(&rev
);
393 static void show_tagger(char *buf
, int len
, struct rev_info
*rev
)
395 struct strbuf out
= STRBUF_INIT
;
396 struct pretty_print_context pp
= {0};
398 pp
.fmt
= rev
->commit_format
;
399 pp
.date_mode
= rev
->date_mode
;
400 pp_user_info(&pp
, "Tagger", &out
, buf
, get_log_output_encoding());
401 printf("%s", out
.buf
);
402 strbuf_release(&out
);
405 static int show_blob_object(const unsigned char *sha1
, struct rev_info
*rev
)
408 return stream_blob_to_fd(1, sha1
, NULL
, 0);
411 static int show_tag_object(const unsigned char *sha1
, struct rev_info
*rev
)
414 enum object_type type
;
415 char *buf
= read_sha1_file(sha1
, &type
, &size
);
419 return error(_("Could not read object %s"), sha1_to_hex(sha1
));
421 assert(type
== OBJ_TAG
);
422 while (offset
< size
&& buf
[offset
] != '\n') {
423 int new_offset
= offset
+ 1;
424 while (new_offset
< size
&& buf
[new_offset
++] != '\n')
426 if (!prefixcmp(buf
+ offset
, "tagger "))
427 show_tagger(buf
+ offset
+ 7,
428 new_offset
- offset
- 7, rev
);
433 fwrite(buf
+ offset
, size
- offset
, 1, stdout
);
438 static int show_tree_object(const unsigned char *sha1
,
439 const char *base
, int baselen
,
440 const char *pathname
, unsigned mode
, int stage
, void *context
)
442 printf("%s%s\n", pathname
, S_ISDIR(mode
) ? "/" : "");
446 static void show_rev_tweak_rev(struct rev_info
*rev
, struct setup_revision_opt
*opt
)
448 if (rev
->ignore_merges
) {
449 /* There was no "-m" on the command line */
450 rev
->ignore_merges
= 0;
451 if (!rev
->first_parent_only
&& !rev
->combine_merges
) {
452 /* No "--first-parent", "-c", nor "--cc" */
453 rev
->combine_merges
= 1;
454 rev
->dense_combined_merges
= 1;
457 if (!rev
->diffopt
.output_format
)
458 rev
->diffopt
.output_format
= DIFF_FORMAT_PATCH
;
461 int cmd_show(int argc
, const char **argv
, const char *prefix
)
464 struct object_array_entry
*objects
;
465 struct setup_revision_opt opt
;
466 struct pathspec match_all
;
467 int i
, count
, ret
= 0;
469 init_grep_defaults();
470 git_config(git_log_config
, NULL
);
472 init_pathspec(&match_all
, NULL
);
473 init_revisions(&rev
, prefix
);
475 rev
.always_show_header
= 1;
476 rev
.no_walk
= REVISION_WALK_NO_WALK_SORTED
;
477 rev
.diffopt
.stat_width
= -1; /* Scale to real terminal size */
479 memset(&opt
, 0, sizeof(opt
));
481 opt
.tweak
= show_rev_tweak_rev
;
482 cmd_log_init(argc
, argv
, prefix
, &rev
, &opt
);
485 return cmd_log_walk(&rev
);
487 count
= rev
.pending
.nr
;
488 objects
= rev
.pending
.objects
;
489 for (i
= 0; i
< count
&& !ret
; i
++) {
490 struct object
*o
= objects
[i
].item
;
491 const char *name
= objects
[i
].name
;
494 ret
= show_blob_object(o
->sha1
, NULL
);
497 struct tag
*t
= (struct tag
*)o
;
501 printf("%stag %s%s\n",
502 diff_get_color_opt(&rev
.diffopt
, DIFF_COMMIT
),
504 diff_get_color_opt(&rev
.diffopt
, DIFF_RESET
));
505 ret
= show_tag_object(o
->sha1
, &rev
);
509 o
= parse_object(t
->tagged
->sha1
);
511 ret
= error(_("Could not read object %s"),
512 sha1_to_hex(t
->tagged
->sha1
));
520 printf("%stree %s%s\n\n",
521 diff_get_color_opt(&rev
.diffopt
, DIFF_COMMIT
),
523 diff_get_color_opt(&rev
.diffopt
, DIFF_RESET
));
524 read_tree_recursive((struct tree
*)o
, "", 0, 0, &match_all
,
525 show_tree_object
, NULL
);
529 rev
.pending
.nr
= rev
.pending
.alloc
= 0;
530 rev
.pending
.objects
= NULL
;
531 add_object_array(o
, name
, &rev
.pending
);
532 ret
= cmd_log_walk(&rev
);
535 ret
= error(_("Unknown type: %d"), o
->type
);
543 * This is equivalent to "git log -g --abbrev-commit --pretty=oneline"
545 int cmd_log_reflog(int argc
, const char **argv
, const char *prefix
)
548 struct setup_revision_opt opt
;
550 init_grep_defaults();
551 git_config(git_log_config
, NULL
);
553 init_revisions(&rev
, prefix
);
554 init_reflog_walk(&rev
.reflog_info
);
555 rev
.verbose_header
= 1;
556 memset(&opt
, 0, sizeof(opt
));
558 cmd_log_init_defaults(&rev
);
559 rev
.abbrev_commit
= 1;
560 rev
.commit_format
= CMIT_FMT_ONELINE
;
561 rev
.use_terminator
= 1;
562 rev
.always_show_header
= 1;
563 cmd_log_init_finish(argc
, argv
, prefix
, &rev
, &opt
);
565 return cmd_log_walk(&rev
);
568 int cmd_log(int argc
, const char **argv
, const char *prefix
)
571 struct setup_revision_opt opt
;
573 init_grep_defaults();
574 git_config(git_log_config
, NULL
);
576 init_revisions(&rev
, prefix
);
577 rev
.always_show_header
= 1;
578 memset(&opt
, 0, sizeof(opt
));
580 opt
.revarg_opt
= REVARG_COMMITTISH
;
581 cmd_log_init(argc
, argv
, prefix
, &rev
, &opt
);
582 return cmd_log_walk(&rev
);
587 static const char *fmt_patch_suffix
= ".patch";
588 static int numbered
= 0;
589 static int auto_number
= 1;
591 static char *default_attach
= NULL
;
593 static struct string_list extra_hdr
;
594 static struct string_list extra_to
;
595 static struct string_list extra_cc
;
597 static void add_header(const char *value
)
599 struct string_list_item
*item
;
600 int len
= strlen(value
);
601 while (len
&& value
[len
- 1] == '\n')
604 if (!strncasecmp(value
, "to: ", 4)) {
605 item
= string_list_append(&extra_to
, value
+ 4);
607 } else if (!strncasecmp(value
, "cc: ", 4)) {
608 item
= string_list_append(&extra_cc
, value
+ 4);
611 item
= string_list_append(&extra_hdr
, value
);
614 item
->string
[len
] = '\0';
617 #define THREAD_SHALLOW 1
618 #define THREAD_DEEP 2
620 static int do_signoff
;
621 static const char *signature
= git_version_string
;
623 static int git_format_config(const char *var
, const char *value
, void *cb
)
625 if (!strcmp(var
, "format.headers")) {
627 die(_("format.headers without value"));
631 if (!strcmp(var
, "format.suffix"))
632 return git_config_string(&fmt_patch_suffix
, var
, value
);
633 if (!strcmp(var
, "format.to")) {
635 return config_error_nonbool(var
);
636 string_list_append(&extra_to
, value
);
639 if (!strcmp(var
, "format.cc")) {
641 return config_error_nonbool(var
);
642 string_list_append(&extra_cc
, value
);
645 if (!strcmp(var
, "diff.color") || !strcmp(var
, "color.diff") ||
646 !strcmp(var
, "color.ui")) {
649 if (!strcmp(var
, "format.numbered")) {
650 if (value
&& !strcasecmp(value
, "auto")) {
654 numbered
= git_config_bool(var
, value
);
655 auto_number
= auto_number
&& numbered
;
658 if (!strcmp(var
, "format.attach")) {
660 default_attach
= xstrdup(value
);
662 default_attach
= xstrdup(git_version_string
);
665 if (!strcmp(var
, "format.thread")) {
666 if (value
&& !strcasecmp(value
, "deep")) {
667 thread
= THREAD_DEEP
;
670 if (value
&& !strcasecmp(value
, "shallow")) {
671 thread
= THREAD_SHALLOW
;
674 thread
= git_config_bool(var
, value
) && THREAD_SHALLOW
;
677 if (!strcmp(var
, "format.signoff")) {
678 do_signoff
= git_config_bool(var
, value
);
681 if (!strcmp(var
, "format.signature"))
682 return git_config_string(&signature
, var
, value
);
684 return git_log_config(var
, value
, cb
);
687 static FILE *realstdout
= NULL
;
688 static const char *output_directory
= NULL
;
689 static int outdir_offset
;
691 static int reopen_stdout(struct commit
*commit
, const char *subject
,
692 struct rev_info
*rev
, int quiet
)
694 struct strbuf filename
= STRBUF_INIT
;
695 int suffix_len
= strlen(fmt_patch_suffix
) + 1;
697 if (output_directory
) {
698 strbuf_addstr(&filename
, output_directory
);
700 PATH_MAX
- FORMAT_PATCH_NAME_MAX
- suffix_len
)
701 return error(_("name of output directory is too long"));
702 if (filename
.buf
[filename
.len
- 1] != '/')
703 strbuf_addch(&filename
, '/');
706 get_patch_filename(commit
, subject
, rev
->nr
, fmt_patch_suffix
, &filename
);
709 fprintf(realstdout
, "%s\n", filename
.buf
+ outdir_offset
);
711 if (freopen(filename
.buf
, "w", stdout
) == NULL
)
712 return error(_("Cannot open patch file %s"), filename
.buf
);
714 strbuf_release(&filename
);
718 static void get_patch_ids(struct rev_info
*rev
, struct patch_ids
*ids
)
720 struct rev_info check_rev
;
721 struct commit
*commit
;
722 struct object
*o1
, *o2
;
723 unsigned flags1
, flags2
;
725 if (rev
->pending
.nr
!= 2)
726 die(_("Need exactly one range."));
728 o1
= rev
->pending
.objects
[0].item
;
730 o2
= rev
->pending
.objects
[1].item
;
733 if ((flags1
& UNINTERESTING
) == (flags2
& UNINTERESTING
))
734 die(_("Not a range."));
738 /* given a range a..b get all patch ids for b..a */
739 init_revisions(&check_rev
, rev
->prefix
);
740 check_rev
.max_parents
= 1;
741 o1
->flags
^= UNINTERESTING
;
742 o2
->flags
^= UNINTERESTING
;
743 add_pending_object(&check_rev
, o1
, "o1");
744 add_pending_object(&check_rev
, o2
, "o2");
745 if (prepare_revision_walk(&check_rev
))
746 die(_("revision walk setup failed"));
748 while ((commit
= get_revision(&check_rev
)) != NULL
) {
749 add_commit_patch_id(commit
, ids
);
752 /* reset for next revision walk */
753 clear_commit_marks((struct commit
*)o1
,
754 SEEN
| UNINTERESTING
| SHOWN
| ADDED
);
755 clear_commit_marks((struct commit
*)o2
,
756 SEEN
| UNINTERESTING
| SHOWN
| ADDED
);
761 static void gen_message_id(struct rev_info
*info
, char *base
)
763 struct strbuf buf
= STRBUF_INIT
;
764 strbuf_addf(&buf
, "%s.%lu.git.%s", base
,
765 (unsigned long) time(NULL
),
766 git_committer_info(IDENT_NO_NAME
|IDENT_NO_DATE
|IDENT_STRICT
));
767 info
->message_id
= strbuf_detach(&buf
, NULL
);
770 static void print_signature(void)
772 if (signature
&& *signature
)
773 printf("-- \n%s\n\n", signature
);
776 static void add_branch_description(struct strbuf
*buf
, const char *branch_name
)
778 struct strbuf desc
= STRBUF_INIT
;
779 if (!branch_name
|| !*branch_name
)
781 read_branch_desc(&desc
, branch_name
);
783 strbuf_addch(buf
, '\n');
784 strbuf_add(buf
, desc
.buf
, desc
.len
);
785 strbuf_addch(buf
, '\n');
789 static void make_cover_letter(struct rev_info
*rev
, int use_stdout
,
790 int numbered
, int numbered_files
,
791 struct commit
*origin
,
792 int nr
, struct commit
**list
, struct commit
*head
,
793 const char *branch_name
,
796 const char *committer
;
797 const char *body
= "*** SUBJECT HERE ***\n\n*** BLURB HERE ***\n";
800 struct strbuf sb
= STRBUF_INIT
;
802 const char *encoding
= "UTF-8";
803 struct diff_options opts
;
804 int need_8bit_cte
= 0;
805 struct pretty_print_context pp
= {0};
807 if (rev
->commit_format
!= CMIT_FMT_EMAIL
)
808 die(_("Cover letter needs email format"));
810 committer
= git_committer_info(0);
813 reopen_stdout(NULL
, numbered_files
? NULL
: "cover-letter", rev
, quiet
))
816 log_write_email_headers(rev
, head
, &pp
.subject
, &pp
.after_subject
,
819 for (i
= 0; !need_8bit_cte
&& i
< nr
; i
++)
820 if (has_non_ascii(list
[i
]->buffer
))
824 pp
.fmt
= CMIT_FMT_EMAIL
;
825 pp
.date_mode
= DATE_RFC2822
;
826 pp_user_info(&pp
, NULL
, &sb
, committer
, encoding
);
827 pp_title_line(&pp
, &msg
, &sb
, encoding
, need_8bit_cte
);
828 pp_remainder(&pp
, &msg
, &sb
, 0);
829 add_branch_description(&sb
, branch_name
);
830 printf("%s\n", sb
.buf
);
839 for (i
= 0; i
< nr
; i
++)
840 shortlog_add_commit(&log
, list
[i
]);
842 shortlog_output(&log
);
845 * We can only do diffstat with a unique reference point
850 memcpy(&opts
, &rev
->diffopt
, sizeof(opts
));
851 opts
.output_format
= DIFF_FORMAT_SUMMARY
| DIFF_FORMAT_DIFFSTAT
;
853 diff_setup_done(&opts
);
855 diff_tree_sha1(origin
->tree
->object
.sha1
,
856 head
->tree
->object
.sha1
,
865 static const char *clean_message_id(const char *msg_id
)
868 const char *a
, *z
, *m
;
871 while ((ch
= *m
) && (isspace(ch
) || (ch
== '<')))
876 if (!isspace(ch
) && (ch
!= '>'))
881 die(_("insane in-reply-to: %s"), msg_id
);
884 return xmemdupz(a
, z
- a
);
887 static const char *set_outdir(const char *prefix
, const char *output_directory
)
889 if (output_directory
&& is_absolute_path(output_directory
))
890 return output_directory
;
892 if (!prefix
|| !*prefix
) {
893 if (output_directory
)
894 return output_directory
;
895 /* The user did not explicitly ask for "./" */
900 outdir_offset
= strlen(prefix
);
901 if (!output_directory
)
904 return xstrdup(prefix_filename(prefix
, outdir_offset
,
908 static const char * const builtin_format_patch_usage
[] = {
909 N_("git format-patch [options] [<since> | <revision range>]"),
913 static int keep_subject
= 0;
915 static int keep_callback(const struct option
*opt
, const char *arg
, int unset
)
917 ((struct rev_info
*)opt
->value
)->total
= -1;
922 static int subject_prefix
= 0;
924 static int subject_prefix_callback(const struct option
*opt
, const char *arg
,
928 ((struct rev_info
*)opt
->value
)->subject_prefix
= arg
;
932 static int numbered_cmdline_opt
= 0;
934 static int numbered_callback(const struct option
*opt
, const char *arg
,
937 *(int *)opt
->value
= numbered_cmdline_opt
= unset
? 0 : 1;
943 static int no_numbered_callback(const struct option
*opt
, const char *arg
,
946 return numbered_callback(opt
, arg
, 1);
949 static int output_directory_callback(const struct option
*opt
, const char *arg
,
952 const char **dir
= (const char **)opt
->value
;
954 die(_("Two output directories?"));
959 static int thread_callback(const struct option
*opt
, const char *arg
, int unset
)
961 int *thread
= (int *)opt
->value
;
964 else if (!arg
|| !strcmp(arg
, "shallow"))
965 *thread
= THREAD_SHALLOW
;
966 else if (!strcmp(arg
, "deep"))
967 *thread
= THREAD_DEEP
;
973 static int attach_callback(const struct option
*opt
, const char *arg
, int unset
)
975 struct rev_info
*rev
= (struct rev_info
*)opt
->value
;
977 rev
->mime_boundary
= NULL
;
979 rev
->mime_boundary
= arg
;
981 rev
->mime_boundary
= git_version_string
;
982 rev
->no_inline
= unset
? 0 : 1;
986 static int inline_callback(const struct option
*opt
, const char *arg
, int unset
)
988 struct rev_info
*rev
= (struct rev_info
*)opt
->value
;
990 rev
->mime_boundary
= NULL
;
992 rev
->mime_boundary
= arg
;
994 rev
->mime_boundary
= git_version_string
;
999 static int header_callback(const struct option
*opt
, const char *arg
, int unset
)
1002 string_list_clear(&extra_hdr
, 0);
1003 string_list_clear(&extra_to
, 0);
1004 string_list_clear(&extra_cc
, 0);
1011 static int to_callback(const struct option
*opt
, const char *arg
, int unset
)
1014 string_list_clear(&extra_to
, 0);
1016 string_list_append(&extra_to
, arg
);
1020 static int cc_callback(const struct option
*opt
, const char *arg
, int unset
)
1023 string_list_clear(&extra_cc
, 0);
1025 string_list_append(&extra_cc
, arg
);
1029 static char *find_branch_name(struct rev_info
*rev
)
1031 int i
, positive
= -1;
1032 unsigned char branch_sha1
[20];
1033 struct strbuf buf
= STRBUF_INIT
;
1036 for (i
= 0; i
< rev
->cmdline
.nr
; i
++) {
1037 if (rev
->cmdline
.rev
[i
].flags
& UNINTERESTING
)
1046 strbuf_addf(&buf
, "refs/heads/%s", rev
->cmdline
.rev
[positive
].name
);
1047 branch
= resolve_ref_unsafe(buf
.buf
, branch_sha1
, 1, NULL
);
1049 prefixcmp(branch
, "refs/heads/") ||
1050 hashcmp(rev
->cmdline
.rev
[positive
].item
->sha1
, branch_sha1
))
1052 strbuf_release(&buf
);
1054 return xstrdup(rev
->cmdline
.rev
[positive
].name
);
1058 int cmd_format_patch(int argc
, const char **argv
, const char *prefix
)
1060 struct commit
*commit
;
1061 struct commit
**list
= NULL
;
1062 struct rev_info rev
;
1063 struct setup_revision_opt s_r_opt
;
1064 int nr
= 0, total
, i
;
1066 int start_number
= -1;
1067 int numbered_files
= 0; /* _just_ numbers */
1068 int ignore_if_in_upstream
= 0;
1069 int cover_letter
= 0;
1070 int boundary_count
= 0;
1071 int no_binary_diff
= 0;
1072 struct commit
*origin
= NULL
, *head
= NULL
;
1073 const char *in_reply_to
= NULL
;
1074 struct patch_ids ids
;
1075 char *add_signoff
= NULL
;
1076 struct strbuf buf
= STRBUF_INIT
;
1077 int use_patch_format
= 0;
1079 char *branch_name
= NULL
;
1080 const struct option builtin_format_patch_options
[] = {
1081 { OPTION_CALLBACK
, 'n', "numbered", &numbered
, NULL
,
1082 N_("use [PATCH n/m] even with a single patch"),
1083 PARSE_OPT_NOARG
, numbered_callback
},
1084 { OPTION_CALLBACK
, 'N', "no-numbered", &numbered
, NULL
,
1085 N_("use [PATCH] even with multiple patches"),
1086 PARSE_OPT_NOARG
, no_numbered_callback
},
1087 OPT_BOOLEAN('s', "signoff", &do_signoff
, N_("add Signed-off-by:")),
1088 OPT_BOOLEAN(0, "stdout", &use_stdout
,
1089 N_("print patches to standard out")),
1090 OPT_BOOLEAN(0, "cover-letter", &cover_letter
,
1091 N_("generate a cover letter")),
1092 OPT_BOOLEAN(0, "numbered-files", &numbered_files
,
1093 N_("use simple number sequence for output file names")),
1094 OPT_STRING(0, "suffix", &fmt_patch_suffix
, N_("sfx"),
1095 N_("use <sfx> instead of '.patch'")),
1096 OPT_INTEGER(0, "start-number", &start_number
,
1097 N_("start numbering patches at <n> instead of 1")),
1098 { OPTION_CALLBACK
, 0, "subject-prefix", &rev
, N_("prefix"),
1099 N_("Use [<prefix>] instead of [PATCH]"),
1100 PARSE_OPT_NONEG
, subject_prefix_callback
},
1101 { OPTION_CALLBACK
, 'o', "output-directory", &output_directory
,
1102 N_("dir"), N_("store resulting files in <dir>"),
1103 PARSE_OPT_NONEG
, output_directory_callback
},
1104 { OPTION_CALLBACK
, 'k', "keep-subject", &rev
, NULL
,
1105 N_("don't strip/add [PATCH]"),
1106 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
, keep_callback
},
1107 OPT_BOOLEAN(0, "no-binary", &no_binary_diff
,
1108 N_("don't output binary diffs")),
1109 OPT_BOOLEAN(0, "ignore-if-in-upstream", &ignore_if_in_upstream
,
1110 N_("don't include a patch matching a commit upstream")),
1111 { OPTION_BOOLEAN
, 'p', "no-stat", &use_patch_format
, NULL
,
1112 N_("show patch format instead of default (patch + stat)"),
1113 PARSE_OPT_NONEG
| PARSE_OPT_NOARG
},
1114 OPT_GROUP(N_("Messaging")),
1115 { OPTION_CALLBACK
, 0, "add-header", NULL
, N_("header"),
1116 N_("add email header"), 0, header_callback
},
1117 { OPTION_CALLBACK
, 0, "to", NULL
, N_("email"), N_("add To: header"),
1119 { OPTION_CALLBACK
, 0, "cc", NULL
, N_("email"), N_("add Cc: header"),
1121 OPT_STRING(0, "in-reply-to", &in_reply_to
, N_("message-id"),
1122 N_("make first mail a reply to <message-id>")),
1123 { OPTION_CALLBACK
, 0, "attach", &rev
, N_("boundary"),
1124 N_("attach the patch"), PARSE_OPT_OPTARG
,
1126 { OPTION_CALLBACK
, 0, "inline", &rev
, N_("boundary"),
1127 N_("inline the patch"),
1128 PARSE_OPT_OPTARG
| PARSE_OPT_NONEG
,
1130 { OPTION_CALLBACK
, 0, "thread", &thread
, N_("style"),
1131 N_("enable message threading, styles: shallow, deep"),
1132 PARSE_OPT_OPTARG
, thread_callback
},
1133 OPT_STRING(0, "signature", &signature
, N_("signature"),
1134 N_("add a signature")),
1135 OPT_BOOLEAN(0, "quiet", &quiet
,
1136 N_("don't print the patch filenames")),
1140 extra_hdr
.strdup_strings
= 1;
1141 extra_to
.strdup_strings
= 1;
1142 extra_cc
.strdup_strings
= 1;
1143 init_grep_defaults();
1144 git_config(git_format_config
, NULL
);
1145 init_revisions(&rev
, prefix
);
1146 rev
.commit_format
= CMIT_FMT_EMAIL
;
1147 rev
.verbose_header
= 1;
1149 rev
.max_parents
= 1;
1150 DIFF_OPT_SET(&rev
.diffopt
, RECURSIVE
);
1151 rev
.subject_prefix
= fmt_patch_subject_prefix
;
1152 memset(&s_r_opt
, 0, sizeof(s_r_opt
));
1153 s_r_opt
.def
= "HEAD";
1154 s_r_opt
.revarg_opt
= REVARG_COMMITTISH
;
1156 if (default_attach
) {
1157 rev
.mime_boundary
= default_attach
;
1162 * Parse the arguments before setup_revisions(), or something
1163 * like "git format-patch -o a123 HEAD^.." may fail; a123 is
1164 * possibly a valid SHA1.
1166 argc
= parse_options(argc
, argv
, prefix
, builtin_format_patch_options
,
1167 builtin_format_patch_usage
,
1168 PARSE_OPT_KEEP_ARGV0
| PARSE_OPT_KEEP_UNKNOWN
|
1169 PARSE_OPT_KEEP_DASHDASH
);
1172 const char *committer
;
1174 committer
= git_committer_info(IDENT_STRICT
);
1175 endpos
= strchr(committer
, '>');
1177 die(_("bogus committer info %s"), committer
);
1178 add_signoff
= xmemdupz(committer
, endpos
- committer
+ 1);
1181 for (i
= 0; i
< extra_hdr
.nr
; i
++) {
1182 strbuf_addstr(&buf
, extra_hdr
.items
[i
].string
);
1183 strbuf_addch(&buf
, '\n');
1187 strbuf_addstr(&buf
, "To: ");
1188 for (i
= 0; i
< extra_to
.nr
; i
++) {
1190 strbuf_addstr(&buf
, " ");
1191 strbuf_addstr(&buf
, extra_to
.items
[i
].string
);
1192 if (i
+ 1 < extra_to
.nr
)
1193 strbuf_addch(&buf
, ',');
1194 strbuf_addch(&buf
, '\n');
1198 strbuf_addstr(&buf
, "Cc: ");
1199 for (i
= 0; i
< extra_cc
.nr
; i
++) {
1201 strbuf_addstr(&buf
, " ");
1202 strbuf_addstr(&buf
, extra_cc
.items
[i
].string
);
1203 if (i
+ 1 < extra_cc
.nr
)
1204 strbuf_addch(&buf
, ',');
1205 strbuf_addch(&buf
, '\n');
1208 rev
.extra_headers
= strbuf_detach(&buf
, NULL
);
1210 if (start_number
< 0)
1214 * If numbered is set solely due to format.numbered in config,
1215 * and it would conflict with --keep-subject (-k) from the
1216 * command line, reset "numbered".
1218 if (numbered
&& keep_subject
&& !numbered_cmdline_opt
)
1221 if (numbered
&& keep_subject
)
1222 die (_("-n and -k are mutually exclusive."));
1223 if (keep_subject
&& subject_prefix
)
1224 die (_("--subject-prefix and -k are mutually exclusive."));
1225 rev
.preserve_subject
= keep_subject
;
1227 argc
= setup_revisions(argc
, argv
, &rev
, &s_r_opt
);
1229 die (_("unrecognized argument: %s"), argv
[1]);
1231 if (rev
.diffopt
.output_format
& DIFF_FORMAT_NAME
)
1232 die(_("--name-only does not make sense"));
1233 if (rev
.diffopt
.output_format
& DIFF_FORMAT_NAME_STATUS
)
1234 die(_("--name-status does not make sense"));
1235 if (rev
.diffopt
.output_format
& DIFF_FORMAT_CHECKDIFF
)
1236 die(_("--check does not make sense"));
1238 if (!use_patch_format
&&
1239 (!rev
.diffopt
.output_format
||
1240 rev
.diffopt
.output_format
== DIFF_FORMAT_PATCH
))
1241 rev
.diffopt
.output_format
= DIFF_FORMAT_DIFFSTAT
| DIFF_FORMAT_SUMMARY
;
1243 /* Always generate a patch */
1244 rev
.diffopt
.output_format
|= DIFF_FORMAT_PATCH
;
1246 if (!DIFF_OPT_TST(&rev
.diffopt
, TEXT
) && !no_binary_diff
)
1247 DIFF_OPT_SET(&rev
.diffopt
, BINARY
);
1250 init_display_notes(&rev
.notes_opt
);
1253 output_directory
= set_outdir(prefix
, output_directory
);
1257 if (output_directory
) {
1259 die(_("standard output, or directory, which one?"));
1260 if (mkdir(output_directory
, 0777) < 0 && errno
!= EEXIST
)
1261 die_errno(_("Could not create directory '%s'"),
1265 if (rev
.pending
.nr
== 1) {
1266 if (rev
.max_count
< 0 && !rev
.show_root_diff
) {
1268 * This is traditional behaviour of "git format-patch
1269 * origin" that prepares what the origin side still
1272 unsigned char sha1
[20];
1275 rev
.pending
.objects
[0].item
->flags
|= UNINTERESTING
;
1276 add_head_to_pending(&rev
);
1277 ref
= resolve_ref_unsafe("HEAD", sha1
, 1, NULL
);
1278 if (ref
&& !prefixcmp(ref
, "refs/heads/"))
1279 branch_name
= xstrdup(ref
+ strlen("refs/heads/"));
1281 branch_name
= xstrdup(""); /* no branch */
1284 * Otherwise, it is "format-patch -22 HEAD", and/or
1285 * "format-patch --root HEAD". The user wants
1286 * get_revision() to do the usual traversal.
1291 * We cannot move this anywhere earlier because we do want to
1292 * know if --root was given explicitly from the command line.
1294 rev
.show_root_diff
= 1;
1298 * NEEDSWORK:randomly pick one positive commit to show
1299 * diffstat; this is often the tip and the command
1300 * happens to do the right thing in most cases, but a
1301 * complex command like "--cover-letter a b c ^bottom"
1302 * picks "c" and shows diffstat between bottom..c
1303 * which may not match what the series represents at
1304 * all and totally broken.
1307 for (i
= 0; i
< rev
.pending
.nr
; i
++) {
1308 struct object
*o
= rev
.pending
.objects
[i
].item
;
1309 if (!(o
->flags
& UNINTERESTING
))
1310 head
= (struct commit
*)o
;
1312 /* There is nothing to show; it is not an error, though. */
1316 branch_name
= find_branch_name(&rev
);
1319 if (ignore_if_in_upstream
) {
1320 /* Don't say anything if head and upstream are the same. */
1321 if (rev
.pending
.nr
== 2) {
1322 struct object_array_entry
*o
= rev
.pending
.objects
;
1323 if (hashcmp(o
[0].item
->sha1
, o
[1].item
->sha1
) == 0)
1326 get_patch_ids(&rev
, &ids
);
1330 realstdout
= xfdopen(xdup(1), "w");
1332 if (prepare_revision_walk(&rev
))
1333 die(_("revision walk setup failed"));
1335 while ((commit
= get_revision(&rev
)) != NULL
) {
1336 if (commit
->object
.flags
& BOUNDARY
) {
1338 origin
= (boundary_count
== 1) ? commit
: NULL
;
1342 if (ignore_if_in_upstream
&&
1343 has_commit_patch_id(commit
, &ids
))
1347 list
= xrealloc(list
, nr
* sizeof(list
[0]));
1348 list
[nr
- 1] = commit
;
1351 if (!keep_subject
&& auto_number
&& total
> 1)
1354 rev
.total
= total
+ start_number
- 1;
1355 if (in_reply_to
|| thread
|| cover_letter
)
1356 rev
.ref_message_ids
= xcalloc(1, sizeof(struct string_list
));
1358 const char *msgid
= clean_message_id(in_reply_to
);
1359 string_list_append(rev
.ref_message_ids
, msgid
);
1361 rev
.numbered_files
= numbered_files
;
1362 rev
.patch_suffix
= fmt_patch_suffix
;
1365 gen_message_id(&rev
, "cover");
1366 make_cover_letter(&rev
, use_stdout
, numbered
, numbered_files
,
1367 origin
, nr
, list
, head
, branch_name
, quiet
);
1371 rev
.add_signoff
= add_signoff
;
1375 rev
.nr
= total
- nr
+ (start_number
- 1);
1376 /* Make the second and subsequent mails replies to the first */
1378 /* Have we already had a message ID? */
1379 if (rev
.message_id
) {
1381 * For deep threading: make every mail
1382 * a reply to the previous one, no
1383 * matter what other options are set.
1385 * For shallow threading:
1387 * Without --cover-letter and
1388 * --in-reply-to, make every mail a
1389 * reply to the one before.
1391 * With --in-reply-to but no
1392 * --cover-letter, make every mail a
1393 * reply to the <reply-to>.
1395 * With --cover-letter, make every
1396 * mail but the cover letter a reply
1397 * to the cover letter. The cover
1398 * letter is a reply to the
1399 * --in-reply-to, if specified.
1401 if (thread
== THREAD_SHALLOW
1402 && rev
.ref_message_ids
->nr
> 0
1403 && (!cover_letter
|| rev
.nr
> 1))
1404 free(rev
.message_id
);
1406 string_list_append(rev
.ref_message_ids
,
1409 gen_message_id(&rev
, sha1_to_hex(commit
->object
.sha1
));
1413 reopen_stdout(numbered_files
? NULL
: commit
, NULL
, &rev
, quiet
))
1414 die(_("Failed to create output files"));
1415 shown
= log_tree_commit(&rev
, commit
);
1416 free(commit
->buffer
);
1417 commit
->buffer
= NULL
;
1419 /* We put one extra blank line between formatted
1420 * patches and this flag is used by log-tree code
1421 * to see if it needs to emit a LF before showing
1422 * the log; when using one file per patch, we do
1423 * not want the extra blank line.
1428 if (rev
.mime_boundary
)
1429 printf("\n--%s%s--\n\n\n",
1430 mime_boundary_leader
,
1440 string_list_clear(&extra_to
, 0);
1441 string_list_clear(&extra_cc
, 0);
1442 string_list_clear(&extra_hdr
, 0);
1443 if (ignore_if_in_upstream
)
1444 free_patch_ids(&ids
);
1448 static int add_pending_commit(const char *arg
, struct rev_info
*revs
, int flags
)
1450 unsigned char sha1
[20];
1451 if (get_sha1(arg
, sha1
) == 0) {
1452 struct commit
*commit
= lookup_commit_reference(sha1
);
1454 commit
->object
.flags
|= flags
;
1455 add_pending_object(revs
, &commit
->object
, arg
);
1462 static const char * const cherry_usage
[] = {
1463 N_("git cherry [-v] [<upstream> [<head> [<limit>]]]"),
1467 static void print_commit(char sign
, struct commit
*commit
, int verbose
,
1471 printf("%c %s\n", sign
,
1472 find_unique_abbrev(commit
->object
.sha1
, abbrev
));
1474 struct strbuf buf
= STRBUF_INIT
;
1475 pp_commit_easy(CMIT_FMT_ONELINE
, commit
, &buf
);
1476 printf("%c %s %s\n", sign
,
1477 find_unique_abbrev(commit
->object
.sha1
, abbrev
),
1479 strbuf_release(&buf
);
1483 int cmd_cherry(int argc
, const char **argv
, const char *prefix
)
1485 struct rev_info revs
;
1486 struct patch_ids ids
;
1487 struct commit
*commit
;
1488 struct commit_list
*list
= NULL
;
1489 struct branch
*current_branch
;
1490 const char *upstream
;
1491 const char *head
= "HEAD";
1492 const char *limit
= NULL
;
1493 int verbose
= 0, abbrev
= 0;
1495 struct option options
[] = {
1496 OPT__ABBREV(&abbrev
),
1497 OPT__VERBOSE(&verbose
, N_("be verbose")),
1501 argc
= parse_options(argc
, argv
, prefix
, options
, cherry_usage
, 0);
1514 current_branch
= branch_get(NULL
);
1515 if (!current_branch
|| !current_branch
->merge
1516 || !current_branch
->merge
[0]
1517 || !current_branch
->merge
[0]->dst
) {
1518 fprintf(stderr
, _("Could not find a tracked"
1519 " remote branch, please"
1520 " specify <upstream> manually.\n"));
1521 usage_with_options(cherry_usage
, options
);
1524 upstream
= current_branch
->merge
[0]->dst
;
1527 init_revisions(&revs
, prefix
);
1528 revs
.max_parents
= 1;
1530 if (add_pending_commit(head
, &revs
, 0))
1531 die(_("Unknown commit %s"), head
);
1532 if (add_pending_commit(upstream
, &revs
, UNINTERESTING
))
1533 die(_("Unknown commit %s"), upstream
);
1535 /* Don't say anything if head and upstream are the same. */
1536 if (revs
.pending
.nr
== 2) {
1537 struct object_array_entry
*o
= revs
.pending
.objects
;
1538 if (hashcmp(o
[0].item
->sha1
, o
[1].item
->sha1
) == 0)
1542 get_patch_ids(&revs
, &ids
);
1544 if (limit
&& add_pending_commit(limit
, &revs
, UNINTERESTING
))
1545 die(_("Unknown commit %s"), limit
);
1547 /* reverse the list of commits */
1548 if (prepare_revision_walk(&revs
))
1549 die(_("revision walk setup failed"));
1550 while ((commit
= get_revision(&revs
)) != NULL
) {
1551 commit_list_insert(commit
, &list
);
1557 commit
= list
->item
;
1558 if (has_commit_patch_id(commit
, &ids
))
1560 print_commit(sign
, commit
, verbose
, abbrev
);
1564 free_patch_ids(&ids
);