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 const char *fmt_patch_subject_prefix
= "PATCH";
35 static const char *fmt_pretty
;
37 static const char * const builtin_log_usage
[] = {
38 N_("git log [<options>] [<since>..<until>] [[--] <path>...]\n")
39 N_(" or: git show [options] <object>..."),
43 static int parse_decoration_style(const char *var
, const char *value
)
45 switch (git_config_maybe_bool(var
, value
)) {
47 return DECORATE_SHORT_REFS
;
53 if (!strcmp(value
, "full"))
54 return DECORATE_FULL_REFS
;
55 else if (!strcmp(value
, "short"))
56 return DECORATE_SHORT_REFS
;
60 static int decorate_callback(const struct option
*opt
, const char *arg
, int unset
)
65 decoration_style
= parse_decoration_style("command line", arg
);
67 decoration_style
= DECORATE_SHORT_REFS
;
69 if (decoration_style
< 0)
70 die("invalid --decorate option: %s", arg
);
77 static void cmd_log_init_defaults(struct rev_info
*rev
)
80 get_commit_format(fmt_pretty
, rev
);
81 rev
->verbose_header
= 1;
82 DIFF_OPT_SET(&rev
->diffopt
, RECURSIVE
);
83 rev
->diffopt
.stat_width
= -1; /* use full terminal width */
84 rev
->diffopt
.stat_graph_width
= -1; /* respect statGraphWidth config */
85 rev
->abbrev_commit
= default_abbrev_commit
;
86 rev
->show_root_diff
= default_show_root
;
87 rev
->subject_prefix
= fmt_patch_subject_prefix
;
88 DIFF_OPT_SET(&rev
->diffopt
, ALLOW_TEXTCONV
);
90 if (default_date_mode
)
91 rev
->date_mode
= parse_date_format(default_date_mode
);
94 static void cmd_log_init_finish(int argc
, const char **argv
, const char *prefix
,
95 struct rev_info
*rev
, struct setup_revision_opt
*opt
)
97 struct userformat_want w
;
98 int quiet
= 0, source
= 0, mailmap
= 0;
100 const struct option builtin_log_options
[] = {
101 OPT_BOOLEAN(0, "quiet", &quiet
, N_("suppress diff output")),
102 OPT_BOOLEAN(0, "source", &source
, N_("show source")),
103 OPT_BOOLEAN(0, "use-mailmap", &mailmap
, N_("Use mail map file")),
104 { OPTION_CALLBACK
, 0, "decorate", NULL
, NULL
, N_("decorate options"),
105 PARSE_OPT_OPTARG
, decorate_callback
},
109 argc
= parse_options(argc
, argv
, prefix
,
110 builtin_log_options
, builtin_log_usage
,
111 PARSE_OPT_KEEP_ARGV0
| PARSE_OPT_KEEP_UNKNOWN
|
112 PARSE_OPT_KEEP_DASHDASH
);
115 rev
->diffopt
.output_format
|= DIFF_FORMAT_NO_OUTPUT
;
116 argc
= setup_revisions(argc
, argv
, rev
, opt
);
118 /* Any arguments at this point are not recognized */
120 die("unrecognized argument: %s", argv
[1]);
122 memset(&w
, 0, sizeof(w
));
123 userformat_find_requirements(NULL
, &w
);
125 if (!rev
->show_notes_given
&& (!rev
->pretty_given
|| w
.notes
))
128 init_display_notes(&rev
->notes_opt
);
130 if (rev
->diffopt
.pickaxe
|| rev
->diffopt
.filter
)
131 rev
->always_show_header
= 0;
132 if (DIFF_OPT_TST(&rev
->diffopt
, FOLLOW_RENAMES
)) {
133 rev
->always_show_header
= 0;
134 if (rev
->diffopt
.pathspec
.nr
!= 1)
135 usage("git logs can only follow renames on one pathname at a time");
139 rev
->show_source
= 1;
142 rev
->mailmap
= xcalloc(1, sizeof(struct string_list
));
143 read_mailmap(rev
->mailmap
, NULL
);
146 if (rev
->pretty_given
&& rev
->commit_format
== CMIT_FMT_RAW
) {
148 * "log --pretty=raw" is special; ignore UI oriented
149 * configuration variables such as decoration.
151 if (!decoration_given
)
152 decoration_style
= 0;
153 if (!rev
->abbrev_commit_given
)
154 rev
->abbrev_commit
= 0;
157 if (decoration_style
) {
158 rev
->show_decorations
= 1;
159 load_ref_decorations(decoration_style
);
164 static void cmd_log_init(int argc
, const char **argv
, const char *prefix
,
165 struct rev_info
*rev
, struct setup_revision_opt
*opt
)
167 cmd_log_init_defaults(rev
);
168 cmd_log_init_finish(argc
, argv
, prefix
, rev
, opt
);
172 * This gives a rough estimate for how many commits we
173 * will print out in the list.
175 static int estimate_commit_count(struct rev_info
*rev
, struct commit_list
*list
)
180 struct commit
*commit
= list
->item
;
181 unsigned int flags
= commit
->object
.flags
;
183 if (!(flags
& (TREESAME
| UNINTERESTING
)))
189 static void show_early_header(struct rev_info
*rev
, const char *stage
, int nr
)
191 if (rev
->shown_one
) {
193 if (rev
->commit_format
!= CMIT_FMT_ONELINE
)
194 putchar(rev
->diffopt
.line_termination
);
196 printf(_("Final output: %d %s\n"), nr
, stage
);
199 static struct itimerval early_output_timer
;
201 static void log_show_early(struct rev_info
*revs
, struct commit_list
*list
)
203 int i
= revs
->early_output
;
206 sort_in_topological_order(&list
, revs
->lifo
);
208 struct commit
*commit
= list
->item
;
209 switch (simplify_commit(revs
, commit
)) {
212 int n
= estimate_commit_count(revs
, list
);
213 show_early_header(revs
, "incomplete", n
);
216 log_tree_commit(revs
, commit
);
227 /* Did we already get enough commits for the early output? */
232 * ..if no, then repeat it twice a second until we
235 * NOTE! We don't use "it_interval", because if the
236 * reader isn't listening, we want our output to be
237 * throttled by the writing, and not have the timer
238 * trigger every second even if we're blocked on a
241 early_output_timer
.it_value
.tv_sec
= 0;
242 early_output_timer
.it_value
.tv_usec
= 500000;
243 setitimer(ITIMER_REAL
, &early_output_timer
, NULL
);
246 static void early_output(int signal
)
248 show_early_output
= log_show_early
;
251 static void setup_early_output(struct rev_info
*rev
)
256 * Set up the signal handler, minimally intrusively:
257 * we only set a single volatile integer word (not
258 * using sigatomic_t - trying to avoid unnecessary
259 * system dependencies and headers), and using
262 memset(&sa
, 0, sizeof(sa
));
263 sa
.sa_handler
= early_output
;
264 sigemptyset(&sa
.sa_mask
);
265 sa
.sa_flags
= SA_RESTART
;
266 sigaction(SIGALRM
, &sa
, NULL
);
269 * If we can get the whole output in less than a
270 * tenth of a second, don't even bother doing the
271 * early-output thing..
273 * This is a one-time-only trigger.
275 early_output_timer
.it_value
.tv_sec
= 0;
276 early_output_timer
.it_value
.tv_usec
= 100000;
277 setitimer(ITIMER_REAL
, &early_output_timer
, NULL
);
280 static void finish_early_output(struct rev_info
*rev
)
282 int n
= estimate_commit_count(rev
, rev
->commits
);
283 signal(SIGALRM
, SIG_IGN
);
284 show_early_header(rev
, "done", n
);
287 static int cmd_log_walk(struct rev_info
*rev
)
289 struct commit
*commit
;
293 if (rev
->early_output
)
294 setup_early_output(rev
);
296 if (prepare_revision_walk(rev
))
297 die(_("revision walk setup failed"));
299 if (rev
->early_output
)
300 finish_early_output(rev
);
303 * For --check and --exit-code, the exit code is based on CHECK_FAILED
304 * and HAS_CHANGES being accumulated in rev->diffopt, so be careful to
305 * retain that state information if replacing rev->diffopt in this loop
307 while ((commit
= get_revision(rev
)) != NULL
) {
308 if (!log_tree_commit(rev
, commit
) &&
311 * We decremented max_count in get_revision,
312 * but we didn't actually show the commit.
315 if (!rev
->reflog_info
) {
316 /* we allow cycles in reflog ancestry */
317 free(commit
->buffer
);
318 commit
->buffer
= NULL
;
320 free_commit_list(commit
->parents
);
321 commit
->parents
= NULL
;
322 if (saved_nrl
< rev
->diffopt
.needed_rename_limit
)
323 saved_nrl
= rev
->diffopt
.needed_rename_limit
;
324 if (rev
->diffopt
.degraded_cc_to_c
)
327 rev
->diffopt
.degraded_cc_to_c
= saved_dcctc
;
328 rev
->diffopt
.needed_rename_limit
= saved_nrl
;
330 if (rev
->diffopt
.output_format
& DIFF_FORMAT_CHECKDIFF
&&
331 DIFF_OPT_TST(&rev
->diffopt
, CHECK_FAILED
)) {
334 return diff_result_code(&rev
->diffopt
, 0);
337 static int git_log_config(const char *var
, const char *value
, void *cb
)
339 if (!strcmp(var
, "format.pretty"))
340 return git_config_string(&fmt_pretty
, var
, value
);
341 if (!strcmp(var
, "format.subjectprefix"))
342 return git_config_string(&fmt_patch_subject_prefix
, var
, value
);
343 if (!strcmp(var
, "log.abbrevcommit")) {
344 default_abbrev_commit
= git_config_bool(var
, value
);
347 if (!strcmp(var
, "log.date"))
348 return git_config_string(&default_date_mode
, var
, value
);
349 if (!strcmp(var
, "log.decorate")) {
350 decoration_style
= parse_decoration_style(var
, value
);
351 if (decoration_style
< 0)
352 decoration_style
= 0; /* maybe warn? */
355 if (!strcmp(var
, "log.showroot")) {
356 default_show_root
= git_config_bool(var
, value
);
359 if (!prefixcmp(var
, "color.decorate."))
360 return parse_decorate_color_config(var
, 15, value
);
361 if (grep_config(var
, value
, cb
) < 0)
363 return git_diff_ui_config(var
, value
, cb
);
366 int cmd_whatchanged(int argc
, const char **argv
, const char *prefix
)
369 struct setup_revision_opt opt
;
371 init_grep_defaults();
372 git_config(git_log_config
, NULL
);
374 init_revisions(&rev
, prefix
);
376 rev
.simplify_history
= 0;
377 memset(&opt
, 0, sizeof(opt
));
379 opt
.revarg_opt
= REVARG_COMMITTISH
;
380 cmd_log_init(argc
, argv
, prefix
, &rev
, &opt
);
381 if (!rev
.diffopt
.output_format
)
382 rev
.diffopt
.output_format
= DIFF_FORMAT_RAW
;
383 return cmd_log_walk(&rev
);
386 static void show_tagger(char *buf
, int len
, struct rev_info
*rev
)
388 struct strbuf out
= STRBUF_INIT
;
389 struct pretty_print_context pp
= {0};
391 pp
.fmt
= rev
->commit_format
;
392 pp
.date_mode
= rev
->date_mode
;
393 pp_user_info(&pp
, "Tagger", &out
, buf
, get_log_output_encoding());
394 printf("%s", out
.buf
);
395 strbuf_release(&out
);
398 static int show_blob_object(const unsigned char *sha1
, struct rev_info
*rev
)
401 return stream_blob_to_fd(1, sha1
, NULL
, 0);
404 static int show_tag_object(const unsigned char *sha1
, struct rev_info
*rev
)
407 enum object_type type
;
408 char *buf
= read_sha1_file(sha1
, &type
, &size
);
412 return error(_("Could not read object %s"), sha1_to_hex(sha1
));
414 assert(type
== OBJ_TAG
);
415 while (offset
< size
&& buf
[offset
] != '\n') {
416 int new_offset
= offset
+ 1;
417 while (new_offset
< size
&& buf
[new_offset
++] != '\n')
419 if (!prefixcmp(buf
+ offset
, "tagger "))
420 show_tagger(buf
+ offset
+ 7,
421 new_offset
- offset
- 7, rev
);
426 fwrite(buf
+ offset
, size
- offset
, 1, stdout
);
431 static int show_tree_object(const unsigned char *sha1
,
432 const char *base
, int baselen
,
433 const char *pathname
, unsigned mode
, int stage
, void *context
)
435 printf("%s%s\n", pathname
, S_ISDIR(mode
) ? "/" : "");
439 static void show_rev_tweak_rev(struct rev_info
*rev
, struct setup_revision_opt
*opt
)
441 if (rev
->ignore_merges
) {
442 /* There was no "-m" on the command line */
443 rev
->ignore_merges
= 0;
444 if (!rev
->first_parent_only
&& !rev
->combine_merges
) {
445 /* No "--first-parent", "-c", nor "--cc" */
446 rev
->combine_merges
= 1;
447 rev
->dense_combined_merges
= 1;
450 if (!rev
->diffopt
.output_format
)
451 rev
->diffopt
.output_format
= DIFF_FORMAT_PATCH
;
454 int cmd_show(int argc
, const char **argv
, const char *prefix
)
457 struct object_array_entry
*objects
;
458 struct setup_revision_opt opt
;
459 struct pathspec match_all
;
460 int i
, count
, ret
= 0;
462 init_grep_defaults();
463 git_config(git_log_config
, NULL
);
465 init_pathspec(&match_all
, NULL
);
466 init_revisions(&rev
, prefix
);
468 rev
.always_show_header
= 1;
469 rev
.no_walk
= REVISION_WALK_NO_WALK_SORTED
;
470 rev
.diffopt
.stat_width
= -1; /* Scale to real terminal size */
472 memset(&opt
, 0, sizeof(opt
));
474 opt
.tweak
= show_rev_tweak_rev
;
475 cmd_log_init(argc
, argv
, prefix
, &rev
, &opt
);
478 return cmd_log_walk(&rev
);
480 count
= rev
.pending
.nr
;
481 objects
= rev
.pending
.objects
;
482 for (i
= 0; i
< count
&& !ret
; i
++) {
483 struct object
*o
= objects
[i
].item
;
484 const char *name
= objects
[i
].name
;
487 ret
= show_blob_object(o
->sha1
, NULL
);
490 struct tag
*t
= (struct tag
*)o
;
494 printf("%stag %s%s\n",
495 diff_get_color_opt(&rev
.diffopt
, DIFF_COMMIT
),
497 diff_get_color_opt(&rev
.diffopt
, DIFF_RESET
));
498 ret
= show_tag_object(o
->sha1
, &rev
);
502 o
= parse_object(t
->tagged
->sha1
);
504 ret
= error(_("Could not read object %s"),
505 sha1_to_hex(t
->tagged
->sha1
));
513 printf("%stree %s%s\n\n",
514 diff_get_color_opt(&rev
.diffopt
, DIFF_COMMIT
),
516 diff_get_color_opt(&rev
.diffopt
, DIFF_RESET
));
517 read_tree_recursive((struct tree
*)o
, "", 0, 0, &match_all
,
518 show_tree_object
, NULL
);
522 rev
.pending
.nr
= rev
.pending
.alloc
= 0;
523 rev
.pending
.objects
= NULL
;
524 add_object_array(o
, name
, &rev
.pending
);
525 ret
= cmd_log_walk(&rev
);
528 ret
= error(_("Unknown type: %d"), o
->type
);
536 * This is equivalent to "git log -g --abbrev-commit --pretty=oneline"
538 int cmd_log_reflog(int argc
, const char **argv
, const char *prefix
)
541 struct setup_revision_opt opt
;
543 init_grep_defaults();
544 git_config(git_log_config
, NULL
);
546 init_revisions(&rev
, prefix
);
547 init_reflog_walk(&rev
.reflog_info
);
548 rev
.verbose_header
= 1;
549 memset(&opt
, 0, sizeof(opt
));
551 cmd_log_init_defaults(&rev
);
552 rev
.abbrev_commit
= 1;
553 rev
.commit_format
= CMIT_FMT_ONELINE
;
554 rev
.use_terminator
= 1;
555 rev
.always_show_header
= 1;
556 cmd_log_init_finish(argc
, argv
, prefix
, &rev
, &opt
);
558 return cmd_log_walk(&rev
);
561 int cmd_log(int argc
, const char **argv
, const char *prefix
)
564 struct setup_revision_opt opt
;
566 init_grep_defaults();
567 git_config(git_log_config
, NULL
);
569 init_revisions(&rev
, prefix
);
570 rev
.always_show_header
= 1;
571 memset(&opt
, 0, sizeof(opt
));
573 opt
.revarg_opt
= REVARG_COMMITTISH
;
574 cmd_log_init(argc
, argv
, prefix
, &rev
, &opt
);
575 return cmd_log_walk(&rev
);
580 static const char *fmt_patch_suffix
= ".patch";
581 static int numbered
= 0;
582 static int auto_number
= 1;
584 static char *default_attach
= NULL
;
586 static struct string_list extra_hdr
;
587 static struct string_list extra_to
;
588 static struct string_list extra_cc
;
590 static void add_header(const char *value
)
592 struct string_list_item
*item
;
593 int len
= strlen(value
);
594 while (len
&& value
[len
- 1] == '\n')
597 if (!strncasecmp(value
, "to: ", 4)) {
598 item
= string_list_append(&extra_to
, value
+ 4);
600 } else if (!strncasecmp(value
, "cc: ", 4)) {
601 item
= string_list_append(&extra_cc
, value
+ 4);
604 item
= string_list_append(&extra_hdr
, value
);
607 item
->string
[len
] = '\0';
610 #define THREAD_SHALLOW 1
611 #define THREAD_DEEP 2
613 static int do_signoff
;
614 static const char *signature
= git_version_string
;
616 static int git_format_config(const char *var
, const char *value
, void *cb
)
618 if (!strcmp(var
, "format.headers")) {
620 die(_("format.headers without value"));
624 if (!strcmp(var
, "format.suffix"))
625 return git_config_string(&fmt_patch_suffix
, var
, value
);
626 if (!strcmp(var
, "format.to")) {
628 return config_error_nonbool(var
);
629 string_list_append(&extra_to
, value
);
632 if (!strcmp(var
, "format.cc")) {
634 return config_error_nonbool(var
);
635 string_list_append(&extra_cc
, value
);
638 if (!strcmp(var
, "diff.color") || !strcmp(var
, "color.diff") ||
639 !strcmp(var
, "color.ui")) {
642 if (!strcmp(var
, "format.numbered")) {
643 if (value
&& !strcasecmp(value
, "auto")) {
647 numbered
= git_config_bool(var
, value
);
648 auto_number
= auto_number
&& numbered
;
651 if (!strcmp(var
, "format.attach")) {
653 default_attach
= xstrdup(value
);
655 default_attach
= xstrdup(git_version_string
);
658 if (!strcmp(var
, "format.thread")) {
659 if (value
&& !strcasecmp(value
, "deep")) {
660 thread
= THREAD_DEEP
;
663 if (value
&& !strcasecmp(value
, "shallow")) {
664 thread
= THREAD_SHALLOW
;
667 thread
= git_config_bool(var
, value
) && THREAD_SHALLOW
;
670 if (!strcmp(var
, "format.signoff")) {
671 do_signoff
= git_config_bool(var
, value
);
674 if (!strcmp(var
, "format.signature"))
675 return git_config_string(&signature
, var
, value
);
677 return git_log_config(var
, value
, cb
);
680 static FILE *realstdout
= NULL
;
681 static const char *output_directory
= NULL
;
682 static int outdir_offset
;
684 static int reopen_stdout(struct commit
*commit
, const char *subject
,
685 struct rev_info
*rev
, int quiet
)
687 struct strbuf filename
= STRBUF_INIT
;
688 int suffix_len
= strlen(fmt_patch_suffix
) + 1;
690 if (output_directory
) {
691 strbuf_addstr(&filename
, output_directory
);
693 PATH_MAX
- FORMAT_PATCH_NAME_MAX
- suffix_len
)
694 return error(_("name of output directory is too long"));
695 if (filename
.buf
[filename
.len
- 1] != '/')
696 strbuf_addch(&filename
, '/');
699 get_patch_filename(commit
, subject
, rev
->nr
, fmt_patch_suffix
, &filename
);
702 fprintf(realstdout
, "%s\n", filename
.buf
+ outdir_offset
);
704 if (freopen(filename
.buf
, "w", stdout
) == NULL
)
705 return error(_("Cannot open patch file %s"), filename
.buf
);
707 strbuf_release(&filename
);
711 static void get_patch_ids(struct rev_info
*rev
, struct patch_ids
*ids
)
713 struct rev_info check_rev
;
714 struct commit
*commit
;
715 struct object
*o1
, *o2
;
716 unsigned flags1
, flags2
;
718 if (rev
->pending
.nr
!= 2)
719 die(_("Need exactly one range."));
721 o1
= rev
->pending
.objects
[0].item
;
723 o2
= rev
->pending
.objects
[1].item
;
726 if ((flags1
& UNINTERESTING
) == (flags2
& UNINTERESTING
))
727 die(_("Not a range."));
731 /* given a range a..b get all patch ids for b..a */
732 init_revisions(&check_rev
, rev
->prefix
);
733 check_rev
.max_parents
= 1;
734 o1
->flags
^= UNINTERESTING
;
735 o2
->flags
^= UNINTERESTING
;
736 add_pending_object(&check_rev
, o1
, "o1");
737 add_pending_object(&check_rev
, o2
, "o2");
738 if (prepare_revision_walk(&check_rev
))
739 die(_("revision walk setup failed"));
741 while ((commit
= get_revision(&check_rev
)) != NULL
) {
742 add_commit_patch_id(commit
, ids
);
745 /* reset for next revision walk */
746 clear_commit_marks((struct commit
*)o1
,
747 SEEN
| UNINTERESTING
| SHOWN
| ADDED
);
748 clear_commit_marks((struct commit
*)o2
,
749 SEEN
| UNINTERESTING
| SHOWN
| ADDED
);
754 static void gen_message_id(struct rev_info
*info
, char *base
)
756 struct strbuf buf
= STRBUF_INIT
;
757 strbuf_addf(&buf
, "%s.%lu.git.%s", base
,
758 (unsigned long) time(NULL
),
759 git_committer_info(IDENT_NO_NAME
|IDENT_NO_DATE
|IDENT_STRICT
));
760 info
->message_id
= strbuf_detach(&buf
, NULL
);
763 static void print_signature(void)
765 if (signature
&& *signature
)
766 printf("-- \n%s\n\n", signature
);
769 static void add_branch_description(struct strbuf
*buf
, const char *branch_name
)
771 struct strbuf desc
= STRBUF_INIT
;
772 if (!branch_name
|| !*branch_name
)
774 read_branch_desc(&desc
, branch_name
);
776 strbuf_addch(buf
, '\n');
777 strbuf_add(buf
, desc
.buf
, desc
.len
);
778 strbuf_addch(buf
, '\n');
782 static void make_cover_letter(struct rev_info
*rev
, int use_stdout
,
783 int numbered
, int numbered_files
,
784 struct commit
*origin
,
785 int nr
, struct commit
**list
, struct commit
*head
,
786 const char *branch_name
,
789 const char *committer
;
790 const char *body
= "*** SUBJECT HERE ***\n\n*** BLURB HERE ***\n";
793 struct strbuf sb
= STRBUF_INIT
;
795 const char *encoding
= "UTF-8";
796 struct diff_options opts
;
797 int need_8bit_cte
= 0;
798 struct pretty_print_context pp
= {0};
800 if (rev
->commit_format
!= CMIT_FMT_EMAIL
)
801 die(_("Cover letter needs email format"));
803 committer
= git_committer_info(0);
806 reopen_stdout(NULL
, numbered_files
? NULL
: "cover-letter", rev
, quiet
))
809 log_write_email_headers(rev
, head
, &pp
.subject
, &pp
.after_subject
,
812 for (i
= 0; !need_8bit_cte
&& i
< nr
; i
++)
813 if (has_non_ascii(list
[i
]->buffer
))
817 pp
.fmt
= CMIT_FMT_EMAIL
;
818 pp
.date_mode
= DATE_RFC2822
;
819 pp_user_info(&pp
, NULL
, &sb
, committer
, encoding
);
820 pp_title_line(&pp
, &msg
, &sb
, encoding
, need_8bit_cte
);
821 pp_remainder(&pp
, &msg
, &sb
, 0);
822 add_branch_description(&sb
, branch_name
);
823 printf("%s\n", sb
.buf
);
832 for (i
= 0; i
< nr
; i
++)
833 shortlog_add_commit(&log
, list
[i
]);
835 shortlog_output(&log
);
838 * We can only do diffstat with a unique reference point
843 memcpy(&opts
, &rev
->diffopt
, sizeof(opts
));
844 opts
.output_format
= DIFF_FORMAT_SUMMARY
| DIFF_FORMAT_DIFFSTAT
;
846 diff_setup_done(&opts
);
848 diff_tree_sha1(origin
->tree
->object
.sha1
,
849 head
->tree
->object
.sha1
,
858 static const char *clean_message_id(const char *msg_id
)
861 const char *a
, *z
, *m
;
864 while ((ch
= *m
) && (isspace(ch
) || (ch
== '<')))
869 if (!isspace(ch
) && (ch
!= '>'))
874 die(_("insane in-reply-to: %s"), msg_id
);
877 return xmemdupz(a
, z
- a
);
880 static const char *set_outdir(const char *prefix
, const char *output_directory
)
882 if (output_directory
&& is_absolute_path(output_directory
))
883 return output_directory
;
885 if (!prefix
|| !*prefix
) {
886 if (output_directory
)
887 return output_directory
;
888 /* The user did not explicitly ask for "./" */
893 outdir_offset
= strlen(prefix
);
894 if (!output_directory
)
897 return xstrdup(prefix_filename(prefix
, outdir_offset
,
901 static const char * const builtin_format_patch_usage
[] = {
902 N_("git format-patch [options] [<since> | <revision range>]"),
906 static int keep_subject
= 0;
908 static int keep_callback(const struct option
*opt
, const char *arg
, int unset
)
910 ((struct rev_info
*)opt
->value
)->total
= -1;
915 static int subject_prefix
= 0;
917 static int subject_prefix_callback(const struct option
*opt
, const char *arg
,
921 ((struct rev_info
*)opt
->value
)->subject_prefix
= arg
;
925 static int numbered_cmdline_opt
= 0;
927 static int numbered_callback(const struct option
*opt
, const char *arg
,
930 *(int *)opt
->value
= numbered_cmdline_opt
= unset
? 0 : 1;
936 static int no_numbered_callback(const struct option
*opt
, const char *arg
,
939 return numbered_callback(opt
, arg
, 1);
942 static int output_directory_callback(const struct option
*opt
, const char *arg
,
945 const char **dir
= (const char **)opt
->value
;
947 die(_("Two output directories?"));
952 static int thread_callback(const struct option
*opt
, const char *arg
, int unset
)
954 int *thread
= (int *)opt
->value
;
957 else if (!arg
|| !strcmp(arg
, "shallow"))
958 *thread
= THREAD_SHALLOW
;
959 else if (!strcmp(arg
, "deep"))
960 *thread
= THREAD_DEEP
;
966 static int attach_callback(const struct option
*opt
, const char *arg
, int unset
)
968 struct rev_info
*rev
= (struct rev_info
*)opt
->value
;
970 rev
->mime_boundary
= NULL
;
972 rev
->mime_boundary
= arg
;
974 rev
->mime_boundary
= git_version_string
;
975 rev
->no_inline
= unset
? 0 : 1;
979 static int inline_callback(const struct option
*opt
, const char *arg
, int unset
)
981 struct rev_info
*rev
= (struct rev_info
*)opt
->value
;
983 rev
->mime_boundary
= NULL
;
985 rev
->mime_boundary
= arg
;
987 rev
->mime_boundary
= git_version_string
;
992 static int header_callback(const struct option
*opt
, const char *arg
, int unset
)
995 string_list_clear(&extra_hdr
, 0);
996 string_list_clear(&extra_to
, 0);
997 string_list_clear(&extra_cc
, 0);
1004 static int to_callback(const struct option
*opt
, const char *arg
, int unset
)
1007 string_list_clear(&extra_to
, 0);
1009 string_list_append(&extra_to
, arg
);
1013 static int cc_callback(const struct option
*opt
, const char *arg
, int unset
)
1016 string_list_clear(&extra_cc
, 0);
1018 string_list_append(&extra_cc
, arg
);
1022 static char *find_branch_name(struct rev_info
*rev
)
1024 int i
, positive
= -1;
1025 unsigned char branch_sha1
[20];
1026 struct strbuf buf
= STRBUF_INIT
;
1029 for (i
= 0; i
< rev
->cmdline
.nr
; i
++) {
1030 if (rev
->cmdline
.rev
[i
].flags
& UNINTERESTING
)
1039 strbuf_addf(&buf
, "refs/heads/%s", rev
->cmdline
.rev
[positive
].name
);
1040 branch
= resolve_ref_unsafe(buf
.buf
, branch_sha1
, 1, NULL
);
1042 prefixcmp(branch
, "refs/heads/") ||
1043 hashcmp(rev
->cmdline
.rev
[positive
].item
->sha1
, branch_sha1
))
1045 strbuf_release(&buf
);
1047 return xstrdup(rev
->cmdline
.rev
[positive
].name
);
1051 int cmd_format_patch(int argc
, const char **argv
, const char *prefix
)
1053 struct commit
*commit
;
1054 struct commit
**list
= NULL
;
1055 struct rev_info rev
;
1056 struct setup_revision_opt s_r_opt
;
1057 int nr
= 0, total
, i
;
1059 int start_number
= -1;
1060 int numbered_files
= 0; /* _just_ numbers */
1061 int ignore_if_in_upstream
= 0;
1062 int cover_letter
= 0;
1063 int boundary_count
= 0;
1064 int no_binary_diff
= 0;
1065 struct commit
*origin
= NULL
, *head
= NULL
;
1066 const char *in_reply_to
= NULL
;
1067 struct patch_ids ids
;
1068 char *add_signoff
= NULL
;
1069 struct strbuf buf
= STRBUF_INIT
;
1070 int use_patch_format
= 0;
1072 char *branch_name
= NULL
;
1073 const struct option builtin_format_patch_options
[] = {
1074 { OPTION_CALLBACK
, 'n', "numbered", &numbered
, NULL
,
1075 N_("use [PATCH n/m] even with a single patch"),
1076 PARSE_OPT_NOARG
, numbered_callback
},
1077 { OPTION_CALLBACK
, 'N', "no-numbered", &numbered
, NULL
,
1078 N_("use [PATCH] even with multiple patches"),
1079 PARSE_OPT_NOARG
, no_numbered_callback
},
1080 OPT_BOOLEAN('s', "signoff", &do_signoff
, N_("add Signed-off-by:")),
1081 OPT_BOOLEAN(0, "stdout", &use_stdout
,
1082 N_("print patches to standard out")),
1083 OPT_BOOLEAN(0, "cover-letter", &cover_letter
,
1084 N_("generate a cover letter")),
1085 OPT_BOOLEAN(0, "numbered-files", &numbered_files
,
1086 N_("use simple number sequence for output file names")),
1087 OPT_STRING(0, "suffix", &fmt_patch_suffix
, N_("sfx"),
1088 N_("use <sfx> instead of '.patch'")),
1089 OPT_INTEGER(0, "start-number", &start_number
,
1090 N_("start numbering patches at <n> instead of 1")),
1091 { OPTION_CALLBACK
, 0, "subject-prefix", &rev
, N_("prefix"),
1092 N_("Use [<prefix>] instead of [PATCH]"),
1093 PARSE_OPT_NONEG
, subject_prefix_callback
},
1094 { OPTION_CALLBACK
, 'o', "output-directory", &output_directory
,
1095 N_("dir"), N_("store resulting files in <dir>"),
1096 PARSE_OPT_NONEG
, output_directory_callback
},
1097 { OPTION_CALLBACK
, 'k', "keep-subject", &rev
, NULL
,
1098 N_("don't strip/add [PATCH]"),
1099 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
, keep_callback
},
1100 OPT_BOOLEAN(0, "no-binary", &no_binary_diff
,
1101 N_("don't output binary diffs")),
1102 OPT_BOOLEAN(0, "ignore-if-in-upstream", &ignore_if_in_upstream
,
1103 N_("don't include a patch matching a commit upstream")),
1104 { OPTION_BOOLEAN
, 'p', "no-stat", &use_patch_format
, NULL
,
1105 N_("show patch format instead of default (patch + stat)"),
1106 PARSE_OPT_NONEG
| PARSE_OPT_NOARG
},
1107 OPT_GROUP(N_("Messaging")),
1108 { OPTION_CALLBACK
, 0, "add-header", NULL
, N_("header"),
1109 N_("add email header"), 0, header_callback
},
1110 { OPTION_CALLBACK
, 0, "to", NULL
, N_("email"), N_("add To: header"),
1112 { OPTION_CALLBACK
, 0, "cc", NULL
, N_("email"), N_("add Cc: header"),
1114 OPT_STRING(0, "in-reply-to", &in_reply_to
, N_("message-id"),
1115 N_("make first mail a reply to <message-id>")),
1116 { OPTION_CALLBACK
, 0, "attach", &rev
, N_("boundary"),
1117 N_("attach the patch"), PARSE_OPT_OPTARG
,
1119 { OPTION_CALLBACK
, 0, "inline", &rev
, N_("boundary"),
1120 N_("inline the patch"),
1121 PARSE_OPT_OPTARG
| PARSE_OPT_NONEG
,
1123 { OPTION_CALLBACK
, 0, "thread", &thread
, N_("style"),
1124 N_("enable message threading, styles: shallow, deep"),
1125 PARSE_OPT_OPTARG
, thread_callback
},
1126 OPT_STRING(0, "signature", &signature
, N_("signature"),
1127 N_("add a signature")),
1128 OPT_BOOLEAN(0, "quiet", &quiet
,
1129 N_("don't print the patch filenames")),
1133 extra_hdr
.strdup_strings
= 1;
1134 extra_to
.strdup_strings
= 1;
1135 extra_cc
.strdup_strings
= 1;
1136 init_grep_defaults();
1137 git_config(git_format_config
, NULL
);
1138 init_revisions(&rev
, prefix
);
1139 rev
.commit_format
= CMIT_FMT_EMAIL
;
1140 rev
.verbose_header
= 1;
1142 rev
.max_parents
= 1;
1143 DIFF_OPT_SET(&rev
.diffopt
, RECURSIVE
);
1144 rev
.subject_prefix
= fmt_patch_subject_prefix
;
1145 memset(&s_r_opt
, 0, sizeof(s_r_opt
));
1146 s_r_opt
.def
= "HEAD";
1147 s_r_opt
.revarg_opt
= REVARG_COMMITTISH
;
1149 if (default_attach
) {
1150 rev
.mime_boundary
= default_attach
;
1155 * Parse the arguments before setup_revisions(), or something
1156 * like "git format-patch -o a123 HEAD^.." may fail; a123 is
1157 * possibly a valid SHA1.
1159 argc
= parse_options(argc
, argv
, prefix
, builtin_format_patch_options
,
1160 builtin_format_patch_usage
,
1161 PARSE_OPT_KEEP_ARGV0
| PARSE_OPT_KEEP_UNKNOWN
|
1162 PARSE_OPT_KEEP_DASHDASH
);
1165 const char *committer
;
1167 committer
= git_committer_info(IDENT_STRICT
);
1168 endpos
= strchr(committer
, '>');
1170 die(_("bogus committer info %s"), committer
);
1171 add_signoff
= xmemdupz(committer
, endpos
- committer
+ 1);
1174 for (i
= 0; i
< extra_hdr
.nr
; i
++) {
1175 strbuf_addstr(&buf
, extra_hdr
.items
[i
].string
);
1176 strbuf_addch(&buf
, '\n');
1180 strbuf_addstr(&buf
, "To: ");
1181 for (i
= 0; i
< extra_to
.nr
; i
++) {
1183 strbuf_addstr(&buf
, " ");
1184 strbuf_addstr(&buf
, extra_to
.items
[i
].string
);
1185 if (i
+ 1 < extra_to
.nr
)
1186 strbuf_addch(&buf
, ',');
1187 strbuf_addch(&buf
, '\n');
1191 strbuf_addstr(&buf
, "Cc: ");
1192 for (i
= 0; i
< extra_cc
.nr
; i
++) {
1194 strbuf_addstr(&buf
, " ");
1195 strbuf_addstr(&buf
, extra_cc
.items
[i
].string
);
1196 if (i
+ 1 < extra_cc
.nr
)
1197 strbuf_addch(&buf
, ',');
1198 strbuf_addch(&buf
, '\n');
1201 rev
.extra_headers
= strbuf_detach(&buf
, NULL
);
1203 if (start_number
< 0)
1207 * If numbered is set solely due to format.numbered in config,
1208 * and it would conflict with --keep-subject (-k) from the
1209 * command line, reset "numbered".
1211 if (numbered
&& keep_subject
&& !numbered_cmdline_opt
)
1214 if (numbered
&& keep_subject
)
1215 die (_("-n and -k are mutually exclusive."));
1216 if (keep_subject
&& subject_prefix
)
1217 die (_("--subject-prefix and -k are mutually exclusive."));
1218 rev
.preserve_subject
= keep_subject
;
1220 argc
= setup_revisions(argc
, argv
, &rev
, &s_r_opt
);
1222 die (_("unrecognized argument: %s"), argv
[1]);
1224 if (rev
.diffopt
.output_format
& DIFF_FORMAT_NAME
)
1225 die(_("--name-only does not make sense"));
1226 if (rev
.diffopt
.output_format
& DIFF_FORMAT_NAME_STATUS
)
1227 die(_("--name-status does not make sense"));
1228 if (rev
.diffopt
.output_format
& DIFF_FORMAT_CHECKDIFF
)
1229 die(_("--check does not make sense"));
1231 if (!use_patch_format
&&
1232 (!rev
.diffopt
.output_format
||
1233 rev
.diffopt
.output_format
== DIFF_FORMAT_PATCH
))
1234 rev
.diffopt
.output_format
= DIFF_FORMAT_DIFFSTAT
| DIFF_FORMAT_SUMMARY
;
1236 /* Always generate a patch */
1237 rev
.diffopt
.output_format
|= DIFF_FORMAT_PATCH
;
1239 if (!DIFF_OPT_TST(&rev
.diffopt
, TEXT
) && !no_binary_diff
)
1240 DIFF_OPT_SET(&rev
.diffopt
, BINARY
);
1243 init_display_notes(&rev
.notes_opt
);
1246 output_directory
= set_outdir(prefix
, output_directory
);
1250 if (output_directory
) {
1252 die(_("standard output, or directory, which one?"));
1253 if (mkdir(output_directory
, 0777) < 0 && errno
!= EEXIST
)
1254 die_errno(_("Could not create directory '%s'"),
1258 if (rev
.pending
.nr
== 1) {
1259 if (rev
.max_count
< 0 && !rev
.show_root_diff
) {
1261 * This is traditional behaviour of "git format-patch
1262 * origin" that prepares what the origin side still
1265 unsigned char sha1
[20];
1268 rev
.pending
.objects
[0].item
->flags
|= UNINTERESTING
;
1269 add_head_to_pending(&rev
);
1270 ref
= resolve_ref_unsafe("HEAD", sha1
, 1, NULL
);
1271 if (ref
&& !prefixcmp(ref
, "refs/heads/"))
1272 branch_name
= xstrdup(ref
+ strlen("refs/heads/"));
1274 branch_name
= xstrdup(""); /* no branch */
1277 * Otherwise, it is "format-patch -22 HEAD", and/or
1278 * "format-patch --root HEAD". The user wants
1279 * get_revision() to do the usual traversal.
1284 * We cannot move this anywhere earlier because we do want to
1285 * know if --root was given explicitly from the command line.
1287 rev
.show_root_diff
= 1;
1291 * NEEDSWORK:randomly pick one positive commit to show
1292 * diffstat; this is often the tip and the command
1293 * happens to do the right thing in most cases, but a
1294 * complex command like "--cover-letter a b c ^bottom"
1295 * picks "c" and shows diffstat between bottom..c
1296 * which may not match what the series represents at
1297 * all and totally broken.
1300 for (i
= 0; i
< rev
.pending
.nr
; i
++) {
1301 struct object
*o
= rev
.pending
.objects
[i
].item
;
1302 if (!(o
->flags
& UNINTERESTING
))
1303 head
= (struct commit
*)o
;
1305 /* There is nothing to show; it is not an error, though. */
1309 branch_name
= find_branch_name(&rev
);
1312 if (ignore_if_in_upstream
) {
1313 /* Don't say anything if head and upstream are the same. */
1314 if (rev
.pending
.nr
== 2) {
1315 struct object_array_entry
*o
= rev
.pending
.objects
;
1316 if (hashcmp(o
[0].item
->sha1
, o
[1].item
->sha1
) == 0)
1319 get_patch_ids(&rev
, &ids
);
1323 realstdout
= xfdopen(xdup(1), "w");
1325 if (prepare_revision_walk(&rev
))
1326 die(_("revision walk setup failed"));
1328 while ((commit
= get_revision(&rev
)) != NULL
) {
1329 if (commit
->object
.flags
& BOUNDARY
) {
1331 origin
= (boundary_count
== 1) ? commit
: NULL
;
1335 if (ignore_if_in_upstream
&&
1336 has_commit_patch_id(commit
, &ids
))
1340 list
= xrealloc(list
, nr
* sizeof(list
[0]));
1341 list
[nr
- 1] = commit
;
1344 if (!keep_subject
&& auto_number
&& total
> 1)
1347 rev
.total
= total
+ start_number
- 1;
1348 if (in_reply_to
|| thread
|| cover_letter
)
1349 rev
.ref_message_ids
= xcalloc(1, sizeof(struct string_list
));
1351 const char *msgid
= clean_message_id(in_reply_to
);
1352 string_list_append(rev
.ref_message_ids
, msgid
);
1354 rev
.numbered_files
= numbered_files
;
1355 rev
.patch_suffix
= fmt_patch_suffix
;
1358 gen_message_id(&rev
, "cover");
1359 make_cover_letter(&rev
, use_stdout
, numbered
, numbered_files
,
1360 origin
, nr
, list
, head
, branch_name
, quiet
);
1364 rev
.add_signoff
= add_signoff
;
1368 rev
.nr
= total
- nr
+ (start_number
- 1);
1369 /* Make the second and subsequent mails replies to the first */
1371 /* Have we already had a message ID? */
1372 if (rev
.message_id
) {
1374 * For deep threading: make every mail
1375 * a reply to the previous one, no
1376 * matter what other options are set.
1378 * For shallow threading:
1380 * Without --cover-letter and
1381 * --in-reply-to, make every mail a
1382 * reply to the one before.
1384 * With --in-reply-to but no
1385 * --cover-letter, make every mail a
1386 * reply to the <reply-to>.
1388 * With --cover-letter, make every
1389 * mail but the cover letter a reply
1390 * to the cover letter. The cover
1391 * letter is a reply to the
1392 * --in-reply-to, if specified.
1394 if (thread
== THREAD_SHALLOW
1395 && rev
.ref_message_ids
->nr
> 0
1396 && (!cover_letter
|| rev
.nr
> 1))
1397 free(rev
.message_id
);
1399 string_list_append(rev
.ref_message_ids
,
1402 gen_message_id(&rev
, sha1_to_hex(commit
->object
.sha1
));
1406 reopen_stdout(numbered_files
? NULL
: commit
, NULL
, &rev
, quiet
))
1407 die(_("Failed to create output files"));
1408 shown
= log_tree_commit(&rev
, commit
);
1409 free(commit
->buffer
);
1410 commit
->buffer
= NULL
;
1412 /* We put one extra blank line between formatted
1413 * patches and this flag is used by log-tree code
1414 * to see if it needs to emit a LF before showing
1415 * the log; when using one file per patch, we do
1416 * not want the extra blank line.
1421 if (rev
.mime_boundary
)
1422 printf("\n--%s%s--\n\n\n",
1423 mime_boundary_leader
,
1433 string_list_clear(&extra_to
, 0);
1434 string_list_clear(&extra_cc
, 0);
1435 string_list_clear(&extra_hdr
, 0);
1436 if (ignore_if_in_upstream
)
1437 free_patch_ids(&ids
);
1441 static int add_pending_commit(const char *arg
, struct rev_info
*revs
, int flags
)
1443 unsigned char sha1
[20];
1444 if (get_sha1(arg
, sha1
) == 0) {
1445 struct commit
*commit
= lookup_commit_reference(sha1
);
1447 commit
->object
.flags
|= flags
;
1448 add_pending_object(revs
, &commit
->object
, arg
);
1455 static const char * const cherry_usage
[] = {
1456 N_("git cherry [-v] [<upstream> [<head> [<limit>]]]"),
1460 static void print_commit(char sign
, struct commit
*commit
, int verbose
,
1464 printf("%c %s\n", sign
,
1465 find_unique_abbrev(commit
->object
.sha1
, abbrev
));
1467 struct strbuf buf
= STRBUF_INIT
;
1468 pp_commit_easy(CMIT_FMT_ONELINE
, commit
, &buf
);
1469 printf("%c %s %s\n", sign
,
1470 find_unique_abbrev(commit
->object
.sha1
, abbrev
),
1472 strbuf_release(&buf
);
1476 int cmd_cherry(int argc
, const char **argv
, const char *prefix
)
1478 struct rev_info revs
;
1479 struct patch_ids ids
;
1480 struct commit
*commit
;
1481 struct commit_list
*list
= NULL
;
1482 struct branch
*current_branch
;
1483 const char *upstream
;
1484 const char *head
= "HEAD";
1485 const char *limit
= NULL
;
1486 int verbose
= 0, abbrev
= 0;
1488 struct option options
[] = {
1489 OPT__ABBREV(&abbrev
),
1490 OPT__VERBOSE(&verbose
, N_("be verbose")),
1494 argc
= parse_options(argc
, argv
, prefix
, options
, cherry_usage
, 0);
1507 current_branch
= branch_get(NULL
);
1508 if (!current_branch
|| !current_branch
->merge
1509 || !current_branch
->merge
[0]
1510 || !current_branch
->merge
[0]->dst
) {
1511 fprintf(stderr
, _("Could not find a tracked"
1512 " remote branch, please"
1513 " specify <upstream> manually.\n"));
1514 usage_with_options(cherry_usage
, options
);
1517 upstream
= current_branch
->merge
[0]->dst
;
1520 init_revisions(&revs
, prefix
);
1521 revs
.max_parents
= 1;
1523 if (add_pending_commit(head
, &revs
, 0))
1524 die(_("Unknown commit %s"), head
);
1525 if (add_pending_commit(upstream
, &revs
, UNINTERESTING
))
1526 die(_("Unknown commit %s"), upstream
);
1528 /* Don't say anything if head and upstream are the same. */
1529 if (revs
.pending
.nr
== 2) {
1530 struct object_array_entry
*o
= revs
.pending
.objects
;
1531 if (hashcmp(o
[0].item
->sha1
, o
[1].item
->sha1
) == 0)
1535 get_patch_ids(&revs
, &ids
);
1537 if (limit
&& add_pending_commit(limit
, &revs
, UNINTERESTING
))
1538 die(_("Unknown commit %s"), limit
);
1540 /* reverse the list of commits */
1541 if (prepare_revision_walk(&revs
))
1542 die(_("revision walk setup failed"));
1543 while ((commit
= get_revision(&revs
)) != NULL
) {
1544 commit_list_insert(commit
, &list
);
1550 commit
= list
->item
;
1551 if (has_commit_patch_id(commit
, &ids
))
1553 print_commit(sign
, commit
, verbose
, abbrev
);
1557 free_patch_ids(&ids
);