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 /* Set a default date-time format for git log ("log.date" config variable) */
24 static const char *default_date_mode
= NULL
;
26 static int default_abbrev_commit
;
27 static int default_show_root
= 1;
28 static int decoration_style
;
29 static int decoration_given
;
30 static const char *fmt_patch_subject_prefix
= "PATCH";
31 static const char *fmt_pretty
;
33 static const char * const builtin_log_usage
[] = {
34 "git log [<options>] [<since>..<until>] [[--] <path>...]\n"
35 " or: git show [options] <object>...",
39 static int parse_decoration_style(const char *var
, const char *value
)
41 switch (git_config_maybe_bool(var
, value
)) {
43 return DECORATE_SHORT_REFS
;
49 if (!strcmp(value
, "full"))
50 return DECORATE_FULL_REFS
;
51 else if (!strcmp(value
, "short"))
52 return DECORATE_SHORT_REFS
;
56 static int decorate_callback(const struct option
*opt
, const char *arg
, int unset
)
61 decoration_style
= parse_decoration_style("command line", arg
);
63 decoration_style
= DECORATE_SHORT_REFS
;
65 if (decoration_style
< 0)
66 die("invalid --decorate option: %s", arg
);
73 static void cmd_log_init_defaults(struct rev_info
*rev
)
75 rev
->abbrev
= DEFAULT_ABBREV
;
76 rev
->commit_format
= CMIT_FMT_DEFAULT
;
78 get_commit_format(fmt_pretty
, rev
);
79 rev
->verbose_header
= 1;
80 DIFF_OPT_SET(&rev
->diffopt
, RECURSIVE
);
81 rev
->abbrev_commit
= default_abbrev_commit
;
82 rev
->show_root_diff
= default_show_root
;
83 rev
->subject_prefix
= fmt_patch_subject_prefix
;
84 DIFF_OPT_SET(&rev
->diffopt
, ALLOW_TEXTCONV
);
86 if (default_date_mode
)
87 rev
->date_mode
= parse_date_format(default_date_mode
);
90 static void cmd_log_init_finish(int argc
, const char **argv
, const char *prefix
,
91 struct rev_info
*rev
, struct setup_revision_opt
*opt
)
93 struct userformat_want w
;
94 int quiet
= 0, source
= 0;
96 const struct option builtin_log_options
[] = {
97 OPT_BOOLEAN(0, "quiet", &quiet
, "suppress diff output"),
98 OPT_BOOLEAN(0, "source", &source
, "show source"),
99 { OPTION_CALLBACK
, 0, "decorate", NULL
, NULL
, "decorate options",
100 PARSE_OPT_OPTARG
, decorate_callback
},
104 argc
= parse_options(argc
, argv
, prefix
,
105 builtin_log_options
, builtin_log_usage
,
106 PARSE_OPT_KEEP_ARGV0
| PARSE_OPT_KEEP_UNKNOWN
|
107 PARSE_OPT_KEEP_DASHDASH
);
109 argc
= setup_revisions(argc
, argv
, rev
, opt
);
111 /* Any arguments at this point are not recognized */
113 die("unrecognized argument: %s", argv
[1]);
115 memset(&w
, 0, sizeof(w
));
116 userformat_find_requirements(NULL
, &w
);
118 if (!rev
->show_notes_given
&& (!rev
->pretty_given
|| w
.notes
))
121 init_display_notes(&rev
->notes_opt
);
123 if (rev
->diffopt
.pickaxe
|| rev
->diffopt
.filter
)
124 rev
->always_show_header
= 0;
125 if (DIFF_OPT_TST(&rev
->diffopt
, FOLLOW_RENAMES
)) {
126 rev
->always_show_header
= 0;
127 if (rev
->diffopt
.pathspec
.nr
!= 1)
128 usage("git logs can only follow renames on one pathname at a time");
132 rev
->show_source
= 1;
134 if (rev
->pretty_given
&& rev
->commit_format
== CMIT_FMT_RAW
) {
136 * "log --pretty=raw" is special; ignore UI oriented
137 * configuration variables such as decoration.
139 if (!decoration_given
)
140 decoration_style
= 0;
141 if (!rev
->abbrev_commit_given
)
142 rev
->abbrev_commit
= 0;
145 if (decoration_style
) {
146 rev
->show_decorations
= 1;
147 load_ref_decorations(decoration_style
);
152 static void cmd_log_init(int argc
, const char **argv
, const char *prefix
,
153 struct rev_info
*rev
, struct setup_revision_opt
*opt
)
155 cmd_log_init_defaults(rev
);
156 cmd_log_init_finish(argc
, argv
, prefix
, rev
, opt
);
160 * This gives a rough estimate for how many commits we
161 * will print out in the list.
163 static int estimate_commit_count(struct rev_info
*rev
, struct commit_list
*list
)
168 struct commit
*commit
= list
->item
;
169 unsigned int flags
= commit
->object
.flags
;
171 if (!(flags
& (TREESAME
| UNINTERESTING
)))
177 static void show_early_header(struct rev_info
*rev
, const char *stage
, int nr
)
179 if (rev
->shown_one
) {
181 if (rev
->commit_format
!= CMIT_FMT_ONELINE
)
182 putchar(rev
->diffopt
.line_termination
);
184 printf(_("Final output: %d %s\n"), nr
, stage
);
187 static struct itimerval early_output_timer
;
189 static void log_show_early(struct rev_info
*revs
, struct commit_list
*list
)
191 int i
= revs
->early_output
;
194 sort_in_topological_order(&list
, revs
->lifo
);
196 struct commit
*commit
= list
->item
;
197 switch (simplify_commit(revs
, commit
)) {
200 int n
= estimate_commit_count(revs
, list
);
201 show_early_header(revs
, "incomplete", n
);
204 log_tree_commit(revs
, commit
);
215 /* Did we already get enough commits for the early output? */
220 * ..if no, then repeat it twice a second until we
223 * NOTE! We don't use "it_interval", because if the
224 * reader isn't listening, we want our output to be
225 * throttled by the writing, and not have the timer
226 * trigger every second even if we're blocked on a
229 early_output_timer
.it_value
.tv_sec
= 0;
230 early_output_timer
.it_value
.tv_usec
= 500000;
231 setitimer(ITIMER_REAL
, &early_output_timer
, NULL
);
234 static void early_output(int signal
)
236 show_early_output
= log_show_early
;
239 static void setup_early_output(struct rev_info
*rev
)
244 * Set up the signal handler, minimally intrusively:
245 * we only set a single volatile integer word (not
246 * using sigatomic_t - trying to avoid unnecessary
247 * system dependencies and headers), and using
250 memset(&sa
, 0, sizeof(sa
));
251 sa
.sa_handler
= early_output
;
252 sigemptyset(&sa
.sa_mask
);
253 sa
.sa_flags
= SA_RESTART
;
254 sigaction(SIGALRM
, &sa
, NULL
);
257 * If we can get the whole output in less than a
258 * tenth of a second, don't even bother doing the
259 * early-output thing..
261 * This is a one-time-only trigger.
263 early_output_timer
.it_value
.tv_sec
= 0;
264 early_output_timer
.it_value
.tv_usec
= 100000;
265 setitimer(ITIMER_REAL
, &early_output_timer
, NULL
);
268 static void finish_early_output(struct rev_info
*rev
)
270 int n
= estimate_commit_count(rev
, rev
->commits
);
271 signal(SIGALRM
, SIG_IGN
);
272 show_early_header(rev
, "done", n
);
275 static int cmd_log_walk(struct rev_info
*rev
)
277 struct commit
*commit
;
281 if (rev
->early_output
)
282 setup_early_output(rev
);
284 if (prepare_revision_walk(rev
))
285 die(_("revision walk setup failed"));
287 if (rev
->early_output
)
288 finish_early_output(rev
);
291 * For --check and --exit-code, the exit code is based on CHECK_FAILED
292 * and HAS_CHANGES being accumulated in rev->diffopt, so be careful to
293 * retain that state information if replacing rev->diffopt in this loop
295 while ((commit
= get_revision(rev
)) != NULL
) {
296 if (!log_tree_commit(rev
, commit
) &&
299 * We decremented max_count in get_revision,
300 * but we didn't actually show the commit.
303 if (!rev
->reflog_info
) {
304 /* we allow cycles in reflog ancestry */
305 free(commit
->buffer
);
306 commit
->buffer
= NULL
;
308 free_commit_list(commit
->parents
);
309 commit
->parents
= NULL
;
310 if (saved_nrl
< rev
->diffopt
.needed_rename_limit
)
311 saved_nrl
= rev
->diffopt
.needed_rename_limit
;
312 if (rev
->diffopt
.degraded_cc_to_c
)
315 rev
->diffopt
.degraded_cc_to_c
= saved_dcctc
;
316 rev
->diffopt
.needed_rename_limit
= saved_nrl
;
318 if (rev
->diffopt
.output_format
& DIFF_FORMAT_CHECKDIFF
&&
319 DIFF_OPT_TST(&rev
->diffopt
, CHECK_FAILED
)) {
322 return diff_result_code(&rev
->diffopt
, 0);
325 static int git_log_config(const char *var
, const char *value
, void *cb
)
327 if (!strcmp(var
, "format.pretty"))
328 return git_config_string(&fmt_pretty
, var
, value
);
329 if (!strcmp(var
, "format.subjectprefix"))
330 return git_config_string(&fmt_patch_subject_prefix
, var
, value
);
331 if (!strcmp(var
, "log.abbrevcommit")) {
332 default_abbrev_commit
= git_config_bool(var
, value
);
335 if (!strcmp(var
, "log.date"))
336 return git_config_string(&default_date_mode
, var
, value
);
337 if (!strcmp(var
, "log.decorate")) {
338 decoration_style
= parse_decoration_style(var
, value
);
339 if (decoration_style
< 0)
340 decoration_style
= 0; /* maybe warn? */
343 if (!strcmp(var
, "log.showroot")) {
344 default_show_root
= git_config_bool(var
, value
);
347 if (!prefixcmp(var
, "color.decorate."))
348 return parse_decorate_color_config(var
, 15, value
);
350 return git_diff_ui_config(var
, value
, cb
);
353 int cmd_whatchanged(int argc
, const char **argv
, const char *prefix
)
356 struct setup_revision_opt opt
;
358 git_config(git_log_config
, NULL
);
360 if (diff_use_color_default
== -1)
361 diff_use_color_default
= git_use_color_default
;
363 init_revisions(&rev
, prefix
);
365 rev
.simplify_history
= 0;
366 memset(&opt
, 0, sizeof(opt
));
368 cmd_log_init(argc
, argv
, prefix
, &rev
, &opt
);
369 if (!rev
.diffopt
.output_format
)
370 rev
.diffopt
.output_format
= DIFF_FORMAT_RAW
;
371 return cmd_log_walk(&rev
);
374 static void show_tagger(char *buf
, int len
, struct rev_info
*rev
)
376 struct strbuf out
= STRBUF_INIT
;
378 pp_user_info("Tagger", rev
->commit_format
, &out
, buf
, rev
->date_mode
,
379 get_log_output_encoding());
380 printf("%s", out
.buf
);
381 strbuf_release(&out
);
384 static int show_object(const unsigned char *sha1
, int show_tag_object
,
385 struct rev_info
*rev
)
388 enum object_type type
;
389 char *buf
= read_sha1_file(sha1
, &type
, &size
);
393 return error(_("Could not read object %s"), sha1_to_hex(sha1
));
396 while (offset
< size
&& buf
[offset
] != '\n') {
397 int new_offset
= offset
+ 1;
398 while (new_offset
< size
&& buf
[new_offset
++] != '\n')
400 if (!prefixcmp(buf
+ offset
, "tagger "))
401 show_tagger(buf
+ offset
+ 7,
402 new_offset
- offset
- 7, rev
);
407 fwrite(buf
+ offset
, size
- offset
, 1, stdout
);
412 static int show_tree_object(const unsigned char *sha1
,
413 const char *base
, int baselen
,
414 const char *pathname
, unsigned mode
, int stage
, void *context
)
416 printf("%s%s\n", pathname
, S_ISDIR(mode
) ? "/" : "");
420 static void show_rev_tweak_rev(struct rev_info
*rev
, struct setup_revision_opt
*opt
)
422 if (rev
->ignore_merges
) {
423 /* There was no "-m" on the command line */
424 rev
->ignore_merges
= 0;
425 if (!rev
->first_parent_only
&& !rev
->combine_merges
) {
426 /* No "--first-parent", "-c", nor "--cc" */
427 rev
->combine_merges
= 1;
428 rev
->dense_combined_merges
= 1;
431 if (!rev
->diffopt
.output_format
)
432 rev
->diffopt
.output_format
= DIFF_FORMAT_PATCH
;
435 int cmd_show(int argc
, const char **argv
, const char *prefix
)
438 struct object_array_entry
*objects
;
439 struct setup_revision_opt opt
;
440 struct pathspec match_all
;
441 int i
, count
, ret
= 0;
443 git_config(git_log_config
, NULL
);
445 if (diff_use_color_default
== -1)
446 diff_use_color_default
= git_use_color_default
;
448 init_pathspec(&match_all
, NULL
);
449 init_revisions(&rev
, prefix
);
451 rev
.always_show_header
= 1;
453 memset(&opt
, 0, sizeof(opt
));
455 opt
.tweak
= show_rev_tweak_rev
;
456 cmd_log_init(argc
, argv
, prefix
, &rev
, &opt
);
458 count
= rev
.pending
.nr
;
459 objects
= rev
.pending
.objects
;
460 for (i
= 0; i
< count
&& !ret
; i
++) {
461 struct object
*o
= objects
[i
].item
;
462 const char *name
= objects
[i
].name
;
465 ret
= show_object(o
->sha1
, 0, NULL
);
468 struct tag
*t
= (struct tag
*)o
;
472 printf("%stag %s%s\n",
473 diff_get_color_opt(&rev
.diffopt
, DIFF_COMMIT
),
475 diff_get_color_opt(&rev
.diffopt
, DIFF_RESET
));
476 ret
= show_object(o
->sha1
, 1, &rev
);
480 o
= parse_object(t
->tagged
->sha1
);
482 ret
= error(_("Could not read object %s"),
483 sha1_to_hex(t
->tagged
->sha1
));
491 printf("%stree %s%s\n\n",
492 diff_get_color_opt(&rev
.diffopt
, DIFF_COMMIT
),
494 diff_get_color_opt(&rev
.diffopt
, DIFF_RESET
));
495 read_tree_recursive((struct tree
*)o
, "", 0, 0, &match_all
,
496 show_tree_object
, NULL
);
500 rev
.pending
.nr
= rev
.pending
.alloc
= 0;
501 rev
.pending
.objects
= NULL
;
502 add_object_array(o
, name
, &rev
.pending
);
503 ret
= cmd_log_walk(&rev
);
506 ret
= error(_("Unknown type: %d"), o
->type
);
514 * This is equivalent to "git log -g --abbrev-commit --pretty=oneline"
516 int cmd_log_reflog(int argc
, const char **argv
, const char *prefix
)
519 struct setup_revision_opt opt
;
521 git_config(git_log_config
, NULL
);
523 if (diff_use_color_default
== -1)
524 diff_use_color_default
= git_use_color_default
;
526 init_revisions(&rev
, prefix
);
527 init_reflog_walk(&rev
.reflog_info
);
528 rev
.verbose_header
= 1;
529 memset(&opt
, 0, sizeof(opt
));
531 cmd_log_init_defaults(&rev
);
532 rev
.abbrev_commit
= 1;
533 rev
.commit_format
= CMIT_FMT_ONELINE
;
534 rev
.use_terminator
= 1;
535 rev
.always_show_header
= 1;
536 cmd_log_init_finish(argc
, argv
, prefix
, &rev
, &opt
);
538 return cmd_log_walk(&rev
);
541 int cmd_log(int argc
, const char **argv
, const char *prefix
)
544 struct setup_revision_opt opt
;
546 git_config(git_log_config
, NULL
);
548 if (diff_use_color_default
== -1)
549 diff_use_color_default
= git_use_color_default
;
551 init_revisions(&rev
, prefix
);
552 rev
.always_show_header
= 1;
553 memset(&opt
, 0, sizeof(opt
));
555 cmd_log_init(argc
, argv
, prefix
, &rev
, &opt
);
556 return cmd_log_walk(&rev
);
561 static const char *fmt_patch_suffix
= ".patch";
562 static int numbered
= 0;
563 static int auto_number
= 1;
565 static char *default_attach
= NULL
;
567 static struct string_list extra_hdr
;
568 static struct string_list extra_to
;
569 static struct string_list extra_cc
;
571 static void add_header(const char *value
)
573 struct string_list_item
*item
;
574 int len
= strlen(value
);
575 while (len
&& value
[len
- 1] == '\n')
578 if (!strncasecmp(value
, "to: ", 4)) {
579 item
= string_list_append(&extra_to
, value
+ 4);
581 } else if (!strncasecmp(value
, "cc: ", 4)) {
582 item
= string_list_append(&extra_cc
, value
+ 4);
585 item
= string_list_append(&extra_hdr
, value
);
588 item
->string
[len
] = '\0';
591 #define THREAD_SHALLOW 1
592 #define THREAD_DEEP 2
594 static int do_signoff
;
595 static const char *signature
= git_version_string
;
597 static int git_format_config(const char *var
, const char *value
, void *cb
)
599 if (!strcmp(var
, "format.headers")) {
601 die(_("format.headers without value"));
605 if (!strcmp(var
, "format.suffix"))
606 return git_config_string(&fmt_patch_suffix
, var
, value
);
607 if (!strcmp(var
, "format.to")) {
609 return config_error_nonbool(var
);
610 string_list_append(&extra_to
, value
);
613 if (!strcmp(var
, "format.cc")) {
615 return config_error_nonbool(var
);
616 string_list_append(&extra_cc
, value
);
619 if (!strcmp(var
, "diff.color") || !strcmp(var
, "color.diff")) {
622 if (!strcmp(var
, "format.numbered")) {
623 if (value
&& !strcasecmp(value
, "auto")) {
627 numbered
= git_config_bool(var
, value
);
628 auto_number
= auto_number
&& numbered
;
631 if (!strcmp(var
, "format.attach")) {
633 default_attach
= xstrdup(value
);
635 default_attach
= xstrdup(git_version_string
);
638 if (!strcmp(var
, "format.thread")) {
639 if (value
&& !strcasecmp(value
, "deep")) {
640 thread
= THREAD_DEEP
;
643 if (value
&& !strcasecmp(value
, "shallow")) {
644 thread
= THREAD_SHALLOW
;
647 thread
= git_config_bool(var
, value
) && THREAD_SHALLOW
;
650 if (!strcmp(var
, "format.signoff")) {
651 do_signoff
= git_config_bool(var
, value
);
654 if (!strcmp(var
, "format.signature"))
655 return git_config_string(&signature
, var
, value
);
657 return git_log_config(var
, value
, cb
);
660 static FILE *realstdout
= NULL
;
661 static const char *output_directory
= NULL
;
662 static int outdir_offset
;
664 static int reopen_stdout(struct commit
*commit
, struct rev_info
*rev
, int quiet
)
666 struct strbuf filename
= STRBUF_INIT
;
667 int suffix_len
= strlen(fmt_patch_suffix
) + 1;
669 if (output_directory
) {
670 strbuf_addstr(&filename
, output_directory
);
672 PATH_MAX
- FORMAT_PATCH_NAME_MAX
- suffix_len
)
673 return error(_("name of output directory is too long"));
674 if (filename
.buf
[filename
.len
- 1] != '/')
675 strbuf_addch(&filename
, '/');
678 get_patch_filename(commit
, rev
->nr
, fmt_patch_suffix
, &filename
);
681 fprintf(realstdout
, "%s\n", filename
.buf
+ outdir_offset
);
683 if (freopen(filename
.buf
, "w", stdout
) == NULL
)
684 return error(_("Cannot open patch file %s"), filename
.buf
);
686 strbuf_release(&filename
);
690 static void get_patch_ids(struct rev_info
*rev
, struct patch_ids
*ids
, const char *prefix
)
692 struct rev_info check_rev
;
693 struct commit
*commit
;
694 struct object
*o1
, *o2
;
695 unsigned flags1
, flags2
;
697 if (rev
->pending
.nr
!= 2)
698 die(_("Need exactly one range."));
700 o1
= rev
->pending
.objects
[0].item
;
702 o2
= rev
->pending
.objects
[1].item
;
705 if ((flags1
& UNINTERESTING
) == (flags2
& UNINTERESTING
))
706 die(_("Not a range."));
710 /* given a range a..b get all patch ids for b..a */
711 init_revisions(&check_rev
, prefix
);
712 o1
->flags
^= UNINTERESTING
;
713 o2
->flags
^= UNINTERESTING
;
714 add_pending_object(&check_rev
, o1
, "o1");
715 add_pending_object(&check_rev
, o2
, "o2");
716 if (prepare_revision_walk(&check_rev
))
717 die(_("revision walk setup failed"));
719 while ((commit
= get_revision(&check_rev
)) != NULL
) {
721 if (commit
->parents
&& commit
->parents
->next
)
724 add_commit_patch_id(commit
, ids
);
727 /* reset for next revision walk */
728 clear_commit_marks((struct commit
*)o1
,
729 SEEN
| UNINTERESTING
| SHOWN
| ADDED
);
730 clear_commit_marks((struct commit
*)o2
,
731 SEEN
| UNINTERESTING
| SHOWN
| ADDED
);
736 static void gen_message_id(struct rev_info
*info
, char *base
)
738 const char *committer
= git_committer_info(IDENT_WARN_ON_NO_NAME
);
739 const char *email_start
= strrchr(committer
, '<');
740 const char *email_end
= strrchr(committer
, '>');
741 struct strbuf buf
= STRBUF_INIT
;
742 if (!email_start
|| !email_end
|| email_start
> email_end
- 1)
743 die(_("Could not extract email from committer identity."));
744 strbuf_addf(&buf
, "%s.%lu.git.%.*s", base
,
745 (unsigned long) time(NULL
),
746 (int)(email_end
- email_start
- 1), email_start
+ 1);
747 info
->message_id
= strbuf_detach(&buf
, NULL
);
750 static void print_signature(void)
752 if (signature
&& *signature
)
753 printf("-- \n%s\n\n", signature
);
756 static void make_cover_letter(struct rev_info
*rev
, int use_stdout
,
757 int numbered
, int numbered_files
,
758 struct commit
*origin
,
759 int nr
, struct commit
**list
, struct commit
*head
,
762 const char *committer
;
763 const char *subject_start
= NULL
;
764 const char *body
= "*** SUBJECT HERE ***\n\n*** BLURB HERE ***\n";
766 const char *extra_headers
= rev
->extra_headers
;
768 struct strbuf sb
= STRBUF_INIT
;
770 const char *encoding
= "UTF-8";
771 struct diff_options opts
;
772 int need_8bit_cte
= 0;
773 struct commit
*commit
= NULL
;
775 if (rev
->commit_format
!= CMIT_FMT_EMAIL
)
776 die(_("Cover letter needs email format"));
778 committer
= git_committer_info(0);
780 if (!numbered_files
) {
782 * We fake a commit for the cover letter so we get the filename
785 commit
= xcalloc(1, sizeof(*commit
));
786 commit
->buffer
= xmalloc(400);
787 snprintf(commit
->buffer
, 400,
788 "tree 0000000000000000000000000000000000000000\n"
793 sha1_to_hex(head
->object
.sha1
), committer
, committer
);
796 if (!use_stdout
&& reopen_stdout(commit
, rev
, quiet
))
801 free(commit
->buffer
);
805 log_write_email_headers(rev
, head
, &subject_start
, &extra_headers
,
808 for (i
= 0; !need_8bit_cte
&& i
< nr
; i
++)
809 if (has_non_ascii(list
[i
]->buffer
))
813 pp_user_info(NULL
, CMIT_FMT_EMAIL
, &sb
, committer
, DATE_RFC2822
,
815 pp_title_line(CMIT_FMT_EMAIL
, &msg
, &sb
, subject_start
, extra_headers
,
816 encoding
, need_8bit_cte
);
817 pp_remainder(CMIT_FMT_EMAIL
, &msg
, &sb
, 0);
818 printf("%s\n", sb
.buf
);
827 for (i
= 0; i
< nr
; i
++)
828 shortlog_add_commit(&log
, list
[i
]);
830 shortlog_output(&log
);
833 * We can only do diffstat with a unique reference point
838 memcpy(&opts
, &rev
->diffopt
, sizeof(opts
));
839 opts
.output_format
= DIFF_FORMAT_SUMMARY
| DIFF_FORMAT_DIFFSTAT
;
841 diff_setup_done(&opts
);
843 diff_tree_sha1(origin
->tree
->object
.sha1
,
844 head
->tree
->object
.sha1
,
853 static const char *clean_message_id(const char *msg_id
)
856 const char *a
, *z
, *m
;
859 while ((ch
= *m
) && (isspace(ch
) || (ch
== '<')))
864 if (!isspace(ch
) && (ch
!= '>'))
869 die(_("insane in-reply-to: %s"), msg_id
);
872 return xmemdupz(a
, z
- a
);
875 static const char *set_outdir(const char *prefix
, const char *output_directory
)
877 if (output_directory
&& is_absolute_path(output_directory
))
878 return output_directory
;
880 if (!prefix
|| !*prefix
) {
881 if (output_directory
)
882 return output_directory
;
883 /* The user did not explicitly ask for "./" */
888 outdir_offset
= strlen(prefix
);
889 if (!output_directory
)
892 return xstrdup(prefix_filename(prefix
, outdir_offset
,
896 static const char * const builtin_format_patch_usage
[] = {
897 "git format-patch [options] [<since> | <revision range>]",
901 static int keep_subject
= 0;
903 static int keep_callback(const struct option
*opt
, const char *arg
, int unset
)
905 ((struct rev_info
*)opt
->value
)->total
= -1;
910 static int subject_prefix
= 0;
912 static int subject_prefix_callback(const struct option
*opt
, const char *arg
,
916 ((struct rev_info
*)opt
->value
)->subject_prefix
= arg
;
920 static int numbered_cmdline_opt
= 0;
922 static int numbered_callback(const struct option
*opt
, const char *arg
,
925 *(int *)opt
->value
= numbered_cmdline_opt
= unset
? 0 : 1;
931 static int no_numbered_callback(const struct option
*opt
, const char *arg
,
934 return numbered_callback(opt
, arg
, 1);
937 static int output_directory_callback(const struct option
*opt
, const char *arg
,
940 const char **dir
= (const char **)opt
->value
;
942 die(_("Two output directories?"));
947 static int thread_callback(const struct option
*opt
, const char *arg
, int unset
)
949 int *thread
= (int *)opt
->value
;
952 else if (!arg
|| !strcmp(arg
, "shallow"))
953 *thread
= THREAD_SHALLOW
;
954 else if (!strcmp(arg
, "deep"))
955 *thread
= THREAD_DEEP
;
961 static int attach_callback(const struct option
*opt
, const char *arg
, int unset
)
963 struct rev_info
*rev
= (struct rev_info
*)opt
->value
;
965 rev
->mime_boundary
= NULL
;
967 rev
->mime_boundary
= arg
;
969 rev
->mime_boundary
= git_version_string
;
970 rev
->no_inline
= unset
? 0 : 1;
974 static int inline_callback(const struct option
*opt
, const char *arg
, int unset
)
976 struct rev_info
*rev
= (struct rev_info
*)opt
->value
;
978 rev
->mime_boundary
= NULL
;
980 rev
->mime_boundary
= arg
;
982 rev
->mime_boundary
= git_version_string
;
987 static int header_callback(const struct option
*opt
, const char *arg
, int unset
)
990 string_list_clear(&extra_hdr
, 0);
991 string_list_clear(&extra_to
, 0);
992 string_list_clear(&extra_cc
, 0);
999 static int to_callback(const struct option
*opt
, const char *arg
, int unset
)
1002 string_list_clear(&extra_to
, 0);
1004 string_list_append(&extra_to
, arg
);
1008 static int cc_callback(const struct option
*opt
, const char *arg
, int unset
)
1011 string_list_clear(&extra_cc
, 0);
1013 string_list_append(&extra_cc
, arg
);
1017 int cmd_format_patch(int argc
, const char **argv
, const char *prefix
)
1019 struct commit
*commit
;
1020 struct commit
**list
= NULL
;
1021 struct rev_info rev
;
1022 struct setup_revision_opt s_r_opt
;
1023 int nr
= 0, total
, i
;
1025 int start_number
= -1;
1026 int numbered_files
= 0; /* _just_ numbers */
1027 int ignore_if_in_upstream
= 0;
1028 int cover_letter
= 0;
1029 int boundary_count
= 0;
1030 int no_binary_diff
= 0;
1031 struct commit
*origin
= NULL
, *head
= NULL
;
1032 const char *in_reply_to
= NULL
;
1033 struct patch_ids ids
;
1034 char *add_signoff
= NULL
;
1035 struct strbuf buf
= STRBUF_INIT
;
1036 int use_patch_format
= 0;
1038 const struct option builtin_format_patch_options
[] = {
1039 { OPTION_CALLBACK
, 'n', "numbered", &numbered
, NULL
,
1040 "use [PATCH n/m] even with a single patch",
1041 PARSE_OPT_NOARG
, numbered_callback
},
1042 { OPTION_CALLBACK
, 'N', "no-numbered", &numbered
, NULL
,
1043 "use [PATCH] even with multiple patches",
1044 PARSE_OPT_NOARG
, no_numbered_callback
},
1045 OPT_BOOLEAN('s', "signoff", &do_signoff
, "add Signed-off-by:"),
1046 OPT_BOOLEAN(0, "stdout", &use_stdout
,
1047 "print patches to standard out"),
1048 OPT_BOOLEAN(0, "cover-letter", &cover_letter
,
1049 "generate a cover letter"),
1050 OPT_BOOLEAN(0, "numbered-files", &numbered_files
,
1051 "use simple number sequence for output file names"),
1052 OPT_STRING(0, "suffix", &fmt_patch_suffix
, "sfx",
1053 "use <sfx> instead of '.patch'"),
1054 OPT_INTEGER(0, "start-number", &start_number
,
1055 "start numbering patches at <n> instead of 1"),
1056 { OPTION_CALLBACK
, 0, "subject-prefix", &rev
, "prefix",
1057 "Use [<prefix>] instead of [PATCH]",
1058 PARSE_OPT_NONEG
, subject_prefix_callback
},
1059 { OPTION_CALLBACK
, 'o', "output-directory", &output_directory
,
1060 "dir", "store resulting files in <dir>",
1061 PARSE_OPT_NONEG
, output_directory_callback
},
1062 { OPTION_CALLBACK
, 'k', "keep-subject", &rev
, NULL
,
1063 "don't strip/add [PATCH]",
1064 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
, keep_callback
},
1065 OPT_BOOLEAN(0, "no-binary", &no_binary_diff
,
1066 "don't output binary diffs"),
1067 OPT_BOOLEAN(0, "ignore-if-in-upstream", &ignore_if_in_upstream
,
1068 "don't include a patch matching a commit upstream"),
1069 { OPTION_BOOLEAN
, 'p', "no-stat", &use_patch_format
, NULL
,
1070 "show patch format instead of default (patch + stat)",
1071 PARSE_OPT_NONEG
| PARSE_OPT_NOARG
},
1072 OPT_GROUP("Messaging"),
1073 { OPTION_CALLBACK
, 0, "add-header", NULL
, "header",
1074 "add email header", 0, header_callback
},
1075 { OPTION_CALLBACK
, 0, "to", NULL
, "email", "add To: header",
1077 { OPTION_CALLBACK
, 0, "cc", NULL
, "email", "add Cc: header",
1079 OPT_STRING(0, "in-reply-to", &in_reply_to
, "message-id",
1080 "make first mail a reply to <message-id>"),
1081 { OPTION_CALLBACK
, 0, "attach", &rev
, "boundary",
1082 "attach the patch", PARSE_OPT_OPTARG
,
1084 { OPTION_CALLBACK
, 0, "inline", &rev
, "boundary",
1086 PARSE_OPT_OPTARG
| PARSE_OPT_NONEG
,
1088 { OPTION_CALLBACK
, 0, "thread", &thread
, "style",
1089 "enable message threading, styles: shallow, deep",
1090 PARSE_OPT_OPTARG
, thread_callback
},
1091 OPT_STRING(0, "signature", &signature
, "signature",
1093 OPT_BOOLEAN(0, "quiet", &quiet
,
1094 "don't print the patch filenames"),
1098 extra_hdr
.strdup_strings
= 1;
1099 extra_to
.strdup_strings
= 1;
1100 extra_cc
.strdup_strings
= 1;
1101 git_config(git_format_config
, NULL
);
1102 init_revisions(&rev
, prefix
);
1103 rev
.commit_format
= CMIT_FMT_EMAIL
;
1104 rev
.verbose_header
= 1;
1106 rev
.max_parents
= 1;
1107 DIFF_OPT_SET(&rev
.diffopt
, RECURSIVE
);
1108 rev
.subject_prefix
= fmt_patch_subject_prefix
;
1109 memset(&s_r_opt
, 0, sizeof(s_r_opt
));
1110 s_r_opt
.def
= "HEAD";
1112 if (default_attach
) {
1113 rev
.mime_boundary
= default_attach
;
1118 * Parse the arguments before setup_revisions(), or something
1119 * like "git format-patch -o a123 HEAD^.." may fail; a123 is
1120 * possibly a valid SHA1.
1122 argc
= parse_options(argc
, argv
, prefix
, builtin_format_patch_options
,
1123 builtin_format_patch_usage
,
1124 PARSE_OPT_KEEP_ARGV0
| PARSE_OPT_KEEP_UNKNOWN
|
1125 PARSE_OPT_KEEP_DASHDASH
);
1128 const char *committer
;
1130 committer
= git_committer_info(IDENT_ERROR_ON_NO_NAME
);
1131 endpos
= strchr(committer
, '>');
1133 die(_("bogus committer info %s"), committer
);
1134 add_signoff
= xmemdupz(committer
, endpos
- committer
+ 1);
1137 for (i
= 0; i
< extra_hdr
.nr
; i
++) {
1138 strbuf_addstr(&buf
, extra_hdr
.items
[i
].string
);
1139 strbuf_addch(&buf
, '\n');
1143 strbuf_addstr(&buf
, "To: ");
1144 for (i
= 0; i
< extra_to
.nr
; i
++) {
1146 strbuf_addstr(&buf
, " ");
1147 strbuf_addstr(&buf
, extra_to
.items
[i
].string
);
1148 if (i
+ 1 < extra_to
.nr
)
1149 strbuf_addch(&buf
, ',');
1150 strbuf_addch(&buf
, '\n');
1154 strbuf_addstr(&buf
, "Cc: ");
1155 for (i
= 0; i
< extra_cc
.nr
; i
++) {
1157 strbuf_addstr(&buf
, " ");
1158 strbuf_addstr(&buf
, extra_cc
.items
[i
].string
);
1159 if (i
+ 1 < extra_cc
.nr
)
1160 strbuf_addch(&buf
, ',');
1161 strbuf_addch(&buf
, '\n');
1164 rev
.extra_headers
= strbuf_detach(&buf
, NULL
);
1166 if (start_number
< 0)
1170 * If numbered is set solely due to format.numbered in config,
1171 * and it would conflict with --keep-subject (-k) from the
1172 * command line, reset "numbered".
1174 if (numbered
&& keep_subject
&& !numbered_cmdline_opt
)
1177 if (numbered
&& keep_subject
)
1178 die (_("-n and -k are mutually exclusive."));
1179 if (keep_subject
&& subject_prefix
)
1180 die (_("--subject-prefix and -k are mutually exclusive."));
1182 argc
= setup_revisions(argc
, argv
, &rev
, &s_r_opt
);
1184 die (_("unrecognized argument: %s"), argv
[1]);
1186 if (rev
.diffopt
.output_format
& DIFF_FORMAT_NAME
)
1187 die(_("--name-only does not make sense"));
1188 if (rev
.diffopt
.output_format
& DIFF_FORMAT_NAME_STATUS
)
1189 die(_("--name-status does not make sense"));
1190 if (rev
.diffopt
.output_format
& DIFF_FORMAT_CHECKDIFF
)
1191 die(_("--check does not make sense"));
1193 if (!use_patch_format
&&
1194 (!rev
.diffopt
.output_format
||
1195 rev
.diffopt
.output_format
== DIFF_FORMAT_PATCH
))
1196 rev
.diffopt
.output_format
= DIFF_FORMAT_DIFFSTAT
| DIFF_FORMAT_SUMMARY
;
1198 /* Always generate a patch */
1199 rev
.diffopt
.output_format
|= DIFF_FORMAT_PATCH
;
1201 if (!DIFF_OPT_TST(&rev
.diffopt
, TEXT
) && !no_binary_diff
)
1202 DIFF_OPT_SET(&rev
.diffopt
, BINARY
);
1205 init_display_notes(&rev
.notes_opt
);
1208 output_directory
= set_outdir(prefix
, output_directory
);
1212 if (output_directory
) {
1214 die(_("standard output, or directory, which one?"));
1215 if (mkdir(output_directory
, 0777) < 0 && errno
!= EEXIST
)
1216 die_errno(_("Could not create directory '%s'"),
1220 if (rev
.pending
.nr
== 1) {
1221 if (rev
.max_count
< 0 && !rev
.show_root_diff
) {
1223 * This is traditional behaviour of "git format-patch
1224 * origin" that prepares what the origin side still
1227 rev
.pending
.objects
[0].item
->flags
|= UNINTERESTING
;
1228 add_head_to_pending(&rev
);
1231 * Otherwise, it is "format-patch -22 HEAD", and/or
1232 * "format-patch --root HEAD". The user wants
1233 * get_revision() to do the usual traversal.
1238 * We cannot move this anywhere earlier because we do want to
1239 * know if --root was given explicitly from the command line.
1241 rev
.show_root_diff
= 1;
1244 /* remember the range */
1246 for (i
= 0; i
< rev
.pending
.nr
; i
++) {
1247 struct object
*o
= rev
.pending
.objects
[i
].item
;
1248 if (!(o
->flags
& UNINTERESTING
))
1249 head
= (struct commit
*)o
;
1251 /* We can't generate a cover letter without any patches */
1256 if (ignore_if_in_upstream
) {
1257 /* Don't say anything if head and upstream are the same. */
1258 if (rev
.pending
.nr
== 2) {
1259 struct object_array_entry
*o
= rev
.pending
.objects
;
1260 if (hashcmp(o
[0].item
->sha1
, o
[1].item
->sha1
) == 0)
1263 get_patch_ids(&rev
, &ids
, prefix
);
1267 realstdout
= xfdopen(xdup(1), "w");
1269 if (prepare_revision_walk(&rev
))
1270 die(_("revision walk setup failed"));
1272 while ((commit
= get_revision(&rev
)) != NULL
) {
1273 if (commit
->object
.flags
& BOUNDARY
) {
1275 origin
= (boundary_count
== 1) ? commit
: NULL
;
1279 if (ignore_if_in_upstream
&&
1280 has_commit_patch_id(commit
, &ids
))
1284 list
= xrealloc(list
, nr
* sizeof(list
[0]));
1285 list
[nr
- 1] = commit
;
1288 if (!keep_subject
&& auto_number
&& total
> 1)
1291 rev
.total
= total
+ start_number
- 1;
1292 if (in_reply_to
|| thread
|| cover_letter
)
1293 rev
.ref_message_ids
= xcalloc(1, sizeof(struct string_list
));
1295 const char *msgid
= clean_message_id(in_reply_to
);
1296 string_list_append(rev
.ref_message_ids
, msgid
);
1298 rev
.numbered_files
= numbered_files
;
1299 rev
.patch_suffix
= fmt_patch_suffix
;
1302 gen_message_id(&rev
, "cover");
1303 make_cover_letter(&rev
, use_stdout
, numbered
, numbered_files
,
1304 origin
, nr
, list
, head
, quiet
);
1308 rev
.add_signoff
= add_signoff
;
1312 rev
.nr
= total
- nr
+ (start_number
- 1);
1313 /* Make the second and subsequent mails replies to the first */
1315 /* Have we already had a message ID? */
1316 if (rev
.message_id
) {
1318 * For deep threading: make every mail
1319 * a reply to the previous one, no
1320 * matter what other options are set.
1322 * For shallow threading:
1324 * Without --cover-letter and
1325 * --in-reply-to, make every mail a
1326 * reply to the one before.
1328 * With --in-reply-to but no
1329 * --cover-letter, make every mail a
1330 * reply to the <reply-to>.
1332 * With --cover-letter, make every
1333 * mail but the cover letter a reply
1334 * to the cover letter. The cover
1335 * letter is a reply to the
1336 * --in-reply-to, if specified.
1338 if (thread
== THREAD_SHALLOW
1339 && rev
.ref_message_ids
->nr
> 0
1340 && (!cover_letter
|| rev
.nr
> 1))
1341 free(rev
.message_id
);
1343 string_list_append(rev
.ref_message_ids
,
1346 gen_message_id(&rev
, sha1_to_hex(commit
->object
.sha1
));
1349 if (!use_stdout
&& reopen_stdout(numbered_files
? NULL
: commit
,
1351 die(_("Failed to create output files"));
1352 shown
= log_tree_commit(&rev
, commit
);
1353 free(commit
->buffer
);
1354 commit
->buffer
= NULL
;
1356 /* We put one extra blank line between formatted
1357 * patches and this flag is used by log-tree code
1358 * to see if it needs to emit a LF before showing
1359 * the log; when using one file per patch, we do
1360 * not want the extra blank line.
1365 if (rev
.mime_boundary
)
1366 printf("\n--%s%s--\n\n\n",
1367 mime_boundary_leader
,
1376 string_list_clear(&extra_to
, 0);
1377 string_list_clear(&extra_cc
, 0);
1378 string_list_clear(&extra_hdr
, 0);
1379 if (ignore_if_in_upstream
)
1380 free_patch_ids(&ids
);
1384 static int add_pending_commit(const char *arg
, struct rev_info
*revs
, int flags
)
1386 unsigned char sha1
[20];
1387 if (get_sha1(arg
, sha1
) == 0) {
1388 struct commit
*commit
= lookup_commit_reference(sha1
);
1390 commit
->object
.flags
|= flags
;
1391 add_pending_object(revs
, &commit
->object
, arg
);
1398 static const char * const cherry_usage
[] = {
1399 "git cherry [-v] [<upstream> [<head> [<limit>]]]",
1403 static void print_commit(char sign
, struct commit
*commit
, int verbose
,
1407 printf("%c %s\n", sign
,
1408 find_unique_abbrev(commit
->object
.sha1
, abbrev
));
1410 struct strbuf buf
= STRBUF_INIT
;
1411 struct pretty_print_context ctx
= {0};
1412 pretty_print_commit(CMIT_FMT_ONELINE
, commit
, &buf
, &ctx
);
1413 printf("%c %s %s\n", sign
,
1414 find_unique_abbrev(commit
->object
.sha1
, abbrev
),
1416 strbuf_release(&buf
);
1420 int cmd_cherry(int argc
, const char **argv
, const char *prefix
)
1422 struct rev_info revs
;
1423 struct patch_ids ids
;
1424 struct commit
*commit
;
1425 struct commit_list
*list
= NULL
;
1426 struct branch
*current_branch
;
1427 const char *upstream
;
1428 const char *head
= "HEAD";
1429 const char *limit
= NULL
;
1430 int verbose
= 0, abbrev
= 0;
1432 struct option options
[] = {
1433 OPT__ABBREV(&abbrev
),
1434 OPT__VERBOSE(&verbose
, "be verbose"),
1438 argc
= parse_options(argc
, argv
, prefix
, options
, cherry_usage
, 0);
1451 current_branch
= branch_get(NULL
);
1452 if (!current_branch
|| !current_branch
->merge
1453 || !current_branch
->merge
[0]
1454 || !current_branch
->merge
[0]->dst
) {
1455 fprintf(stderr
, _("Could not find a tracked"
1456 " remote branch, please"
1457 " specify <upstream> manually.\n"));
1458 usage_with_options(cherry_usage
, options
);
1461 upstream
= current_branch
->merge
[0]->dst
;
1464 init_revisions(&revs
, prefix
);
1466 revs
.combine_merges
= 0;
1467 revs
.ignore_merges
= 1;
1468 DIFF_OPT_SET(&revs
.diffopt
, RECURSIVE
);
1470 if (add_pending_commit(head
, &revs
, 0))
1471 die(_("Unknown commit %s"), head
);
1472 if (add_pending_commit(upstream
, &revs
, UNINTERESTING
))
1473 die(_("Unknown commit %s"), upstream
);
1475 /* Don't say anything if head and upstream are the same. */
1476 if (revs
.pending
.nr
== 2) {
1477 struct object_array_entry
*o
= revs
.pending
.objects
;
1478 if (hashcmp(o
[0].item
->sha1
, o
[1].item
->sha1
) == 0)
1482 get_patch_ids(&revs
, &ids
, prefix
);
1484 if (limit
&& add_pending_commit(limit
, &revs
, UNINTERESTING
))
1485 die(_("Unknown commit %s"), limit
);
1487 /* reverse the list of commits */
1488 if (prepare_revision_walk(&revs
))
1489 die(_("revision walk setup failed"));
1490 while ((commit
= get_revision(&revs
)) != NULL
) {
1492 if (commit
->parents
&& commit
->parents
->next
)
1495 commit_list_insert(commit
, &list
);
1501 commit
= list
->item
;
1502 if (has_commit_patch_id(commit
, &ids
))
1504 print_commit(sign
, commit
, verbose
, abbrev
);
1508 free_patch_ids(&ids
);