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_show_root
= 1;
27 static int decoration_style
;
28 static const char *fmt_patch_subject_prefix
= "PATCH";
29 static const char *fmt_pretty
;
31 static const char * const builtin_log_usage
=
32 "git log [<options>] [<since>..<until>] [[--] <path>...]\n"
33 " or: git show [options] <object>...";
35 static int parse_decoration_style(const char *var
, const char *value
)
37 switch (git_config_maybe_bool(var
, value
)) {
39 return DECORATE_SHORT_REFS
;
45 if (!strcmp(value
, "full"))
46 return DECORATE_FULL_REFS
;
47 else if (!strcmp(value
, "short"))
48 return DECORATE_SHORT_REFS
;
52 static void cmd_log_init_defaults(struct rev_info
*rev
)
54 rev
->abbrev
= DEFAULT_ABBREV
;
55 rev
->commit_format
= CMIT_FMT_DEFAULT
;
57 get_commit_format(fmt_pretty
, rev
);
58 rev
->verbose_header
= 1;
59 DIFF_OPT_SET(&rev
->diffopt
, RECURSIVE
);
60 rev
->show_root_diff
= default_show_root
;
61 rev
->subject_prefix
= fmt_patch_subject_prefix
;
62 DIFF_OPT_SET(&rev
->diffopt
, ALLOW_TEXTCONV
);
64 if (default_date_mode
)
65 rev
->date_mode
= parse_date_format(default_date_mode
);
68 static void cmd_log_init_finish(int argc
, const char **argv
, const char *prefix
,
69 struct rev_info
*rev
, struct setup_revision_opt
*opt
)
72 int decoration_given
= 0;
73 struct userformat_want w
;
75 * Check for -h before setup_revisions(), or "git log -h" will
76 * fail when run without a git directory.
78 if (argc
== 2 && !strcmp(argv
[1], "-h"))
79 usage(builtin_log_usage
);
80 argc
= setup_revisions(argc
, argv
, rev
, opt
);
82 memset(&w
, 0, sizeof(w
));
83 userformat_find_requirements(NULL
, &w
);
85 if (!rev
->show_notes_given
&& (!rev
->pretty_given
|| w
.notes
))
88 init_display_notes(&rev
->notes_opt
);
90 if (rev
->diffopt
.pickaxe
|| rev
->diffopt
.filter
)
91 rev
->always_show_header
= 0;
92 if (DIFF_OPT_TST(&rev
->diffopt
, FOLLOW_RENAMES
)) {
93 rev
->always_show_header
= 0;
94 if (rev
->diffopt
.pathspec
.nr
!= 1)
95 usage("git logs can only follow renames on one pathname at a time");
97 for (i
= 1; i
< argc
; i
++) {
98 const char *arg
= argv
[i
];
99 if (!strcmp(arg
, "--decorate")) {
100 decoration_style
= DECORATE_SHORT_REFS
;
101 decoration_given
= 1;
102 } else if (!prefixcmp(arg
, "--decorate=")) {
103 const char *v
= skip_prefix(arg
, "--decorate=");
104 decoration_style
= parse_decoration_style(arg
, v
);
105 if (decoration_style
< 0)
106 die(_("invalid --decorate option: %s"), arg
);
107 decoration_given
= 1;
108 } else if (!strcmp(arg
, "--no-decorate")) {
109 decoration_style
= 0;
110 } else if (!strcmp(arg
, "--source")) {
111 rev
->show_source
= 1;
112 } else if (!strcmp(arg
, "-h")) {
113 usage(builtin_log_usage
);
115 die(_("unrecognized argument: %s"), arg
);
119 * defeat log.decorate configuration interacting with --pretty=raw
120 * from the command line.
122 if (!decoration_given
&& rev
->pretty_given
123 && rev
->commit_format
== CMIT_FMT_RAW
)
124 decoration_style
= 0;
126 if (decoration_style
) {
127 rev
->show_decorations
= 1;
128 load_ref_decorations(decoration_style
);
133 static void cmd_log_init(int argc
, const char **argv
, const char *prefix
,
134 struct rev_info
*rev
, struct setup_revision_opt
*opt
)
136 cmd_log_init_defaults(rev
);
137 cmd_log_init_finish(argc
, argv
, prefix
, rev
, opt
);
141 * This gives a rough estimate for how many commits we
142 * will print out in the list.
144 static int estimate_commit_count(struct rev_info
*rev
, struct commit_list
*list
)
149 struct commit
*commit
= list
->item
;
150 unsigned int flags
= commit
->object
.flags
;
152 if (!(flags
& (TREESAME
| UNINTERESTING
)))
158 static void show_early_header(struct rev_info
*rev
, const char *stage
, int nr
)
160 if (rev
->shown_one
) {
162 if (rev
->commit_format
!= CMIT_FMT_ONELINE
)
163 putchar(rev
->diffopt
.line_termination
);
165 printf(_("Final output: %d %s\n"), nr
, stage
);
168 static struct itimerval early_output_timer
;
170 static void log_show_early(struct rev_info
*revs
, struct commit_list
*list
)
172 int i
= revs
->early_output
;
175 sort_in_topological_order(&list
, revs
->lifo
);
177 struct commit
*commit
= list
->item
;
178 switch (simplify_commit(revs
, commit
)) {
181 int n
= estimate_commit_count(revs
, list
);
182 show_early_header(revs
, "incomplete", n
);
185 log_tree_commit(revs
, commit
);
196 /* Did we already get enough commits for the early output? */
201 * ..if no, then repeat it twice a second until we
204 * NOTE! We don't use "it_interval", because if the
205 * reader isn't listening, we want our output to be
206 * throttled by the writing, and not have the timer
207 * trigger every second even if we're blocked on a
210 early_output_timer
.it_value
.tv_sec
= 0;
211 early_output_timer
.it_value
.tv_usec
= 500000;
212 setitimer(ITIMER_REAL
, &early_output_timer
, NULL
);
215 static void early_output(int signal
)
217 show_early_output
= log_show_early
;
220 static void setup_early_output(struct rev_info
*rev
)
225 * Set up the signal handler, minimally intrusively:
226 * we only set a single volatile integer word (not
227 * using sigatomic_t - trying to avoid unnecessary
228 * system dependencies and headers), and using
231 memset(&sa
, 0, sizeof(sa
));
232 sa
.sa_handler
= early_output
;
233 sigemptyset(&sa
.sa_mask
);
234 sa
.sa_flags
= SA_RESTART
;
235 sigaction(SIGALRM
, &sa
, NULL
);
238 * If we can get the whole output in less than a
239 * tenth of a second, don't even bother doing the
240 * early-output thing..
242 * This is a one-time-only trigger.
244 early_output_timer
.it_value
.tv_sec
= 0;
245 early_output_timer
.it_value
.tv_usec
= 100000;
246 setitimer(ITIMER_REAL
, &early_output_timer
, NULL
);
249 static void finish_early_output(struct rev_info
*rev
)
251 int n
= estimate_commit_count(rev
, rev
->commits
);
252 signal(SIGALRM
, SIG_IGN
);
253 show_early_header(rev
, "done", n
);
256 static int cmd_log_walk(struct rev_info
*rev
)
258 struct commit
*commit
;
262 if (rev
->early_output
)
263 setup_early_output(rev
);
265 if (prepare_revision_walk(rev
))
266 die(_("revision walk setup failed"));
268 if (rev
->early_output
)
269 finish_early_output(rev
);
272 * For --check and --exit-code, the exit code is based on CHECK_FAILED
273 * and HAS_CHANGES being accumulated in rev->diffopt, so be careful to
274 * retain that state information if replacing rev->diffopt in this loop
276 while ((commit
= get_revision(rev
)) != NULL
) {
277 if (!log_tree_commit(rev
, commit
) &&
280 * We decremented max_count in get_revision,
281 * but we didn't actually show the commit.
284 if (!rev
->reflog_info
) {
285 /* we allow cycles in reflog ancestry */
286 free(commit
->buffer
);
287 commit
->buffer
= NULL
;
289 free_commit_list(commit
->parents
);
290 commit
->parents
= NULL
;
291 if (saved_nrl
< rev
->diffopt
.needed_rename_limit
)
292 saved_nrl
= rev
->diffopt
.needed_rename_limit
;
293 if (rev
->diffopt
.degraded_cc_to_c
)
296 rev
->diffopt
.degraded_cc_to_c
= saved_dcctc
;
297 rev
->diffopt
.needed_rename_limit
= saved_nrl
;
299 if (rev
->diffopt
.output_format
& DIFF_FORMAT_CHECKDIFF
&&
300 DIFF_OPT_TST(&rev
->diffopt
, CHECK_FAILED
)) {
303 return diff_result_code(&rev
->diffopt
, 0);
306 static int git_log_config(const char *var
, const char *value
, void *cb
)
308 if (!strcmp(var
, "format.pretty"))
309 return git_config_string(&fmt_pretty
, var
, value
);
310 if (!strcmp(var
, "format.subjectprefix"))
311 return git_config_string(&fmt_patch_subject_prefix
, var
, value
);
312 if (!strcmp(var
, "log.date"))
313 return git_config_string(&default_date_mode
, var
, value
);
314 if (!strcmp(var
, "log.decorate")) {
315 decoration_style
= parse_decoration_style(var
, value
);
316 if (decoration_style
< 0)
317 decoration_style
= 0; /* maybe warn? */
320 if (!strcmp(var
, "log.showroot")) {
321 default_show_root
= git_config_bool(var
, value
);
324 if (!prefixcmp(var
, "color.decorate."))
325 return parse_decorate_color_config(var
, 15, value
);
327 return git_diff_ui_config(var
, value
, cb
);
330 int cmd_whatchanged(int argc
, const char **argv
, const char *prefix
)
333 struct setup_revision_opt opt
;
335 git_config(git_log_config
, NULL
);
337 if (diff_use_color_default
== -1)
338 diff_use_color_default
= git_use_color_default
;
340 init_revisions(&rev
, prefix
);
342 rev
.simplify_history
= 0;
343 memset(&opt
, 0, sizeof(opt
));
345 cmd_log_init(argc
, argv
, prefix
, &rev
, &opt
);
346 if (!rev
.diffopt
.output_format
)
347 rev
.diffopt
.output_format
= DIFF_FORMAT_RAW
;
348 return cmd_log_walk(&rev
);
351 static void show_tagger(char *buf
, int len
, struct rev_info
*rev
)
353 struct strbuf out
= STRBUF_INIT
;
355 pp_user_info("Tagger", rev
->commit_format
, &out
, buf
, rev
->date_mode
,
356 get_log_output_encoding());
357 printf("%s", out
.buf
);
358 strbuf_release(&out
);
361 static int show_object(const unsigned char *sha1
, int show_tag_object
,
362 struct rev_info
*rev
)
365 enum object_type type
;
366 char *buf
= read_sha1_file(sha1
, &type
, &size
);
370 return error(_("Could not read object %s"), sha1_to_hex(sha1
));
373 while (offset
< size
&& buf
[offset
] != '\n') {
374 int new_offset
= offset
+ 1;
375 while (new_offset
< size
&& buf
[new_offset
++] != '\n')
377 if (!prefixcmp(buf
+ offset
, "tagger "))
378 show_tagger(buf
+ offset
+ 7,
379 new_offset
- offset
- 7, rev
);
384 fwrite(buf
+ offset
, size
- offset
, 1, stdout
);
389 static int show_tree_object(const unsigned char *sha1
,
390 const char *base
, int baselen
,
391 const char *pathname
, unsigned mode
, int stage
, void *context
)
393 printf("%s%s\n", pathname
, S_ISDIR(mode
) ? "/" : "");
397 static void show_rev_tweak_rev(struct rev_info
*rev
, struct setup_revision_opt
*opt
)
399 if (rev
->ignore_merges
) {
400 /* There was no "-m" on the command line */
401 rev
->ignore_merges
= 0;
402 if (!rev
->first_parent_only
&& !rev
->combine_merges
) {
403 /* No "--first-parent", "-c", nor "--cc" */
404 rev
->combine_merges
= 1;
405 rev
->dense_combined_merges
= 1;
408 if (!rev
->diffopt
.output_format
)
409 rev
->diffopt
.output_format
= DIFF_FORMAT_PATCH
;
412 int cmd_show(int argc
, const char **argv
, const char *prefix
)
415 struct object_array_entry
*objects
;
416 struct setup_revision_opt opt
;
417 struct pathspec match_all
;
418 int i
, count
, ret
= 0;
420 git_config(git_log_config
, NULL
);
422 if (diff_use_color_default
== -1)
423 diff_use_color_default
= git_use_color_default
;
425 init_pathspec(&match_all
, NULL
);
426 init_revisions(&rev
, prefix
);
428 rev
.always_show_header
= 1;
430 memset(&opt
, 0, sizeof(opt
));
432 opt
.tweak
= show_rev_tweak_rev
;
433 cmd_log_init(argc
, argv
, prefix
, &rev
, &opt
);
435 count
= rev
.pending
.nr
;
436 objects
= rev
.pending
.objects
;
437 for (i
= 0; i
< count
&& !ret
; i
++) {
438 struct object
*o
= objects
[i
].item
;
439 const char *name
= objects
[i
].name
;
442 ret
= show_object(o
->sha1
, 0, NULL
);
445 struct tag
*t
= (struct tag
*)o
;
449 printf("%stag %s%s\n",
450 diff_get_color_opt(&rev
.diffopt
, DIFF_COMMIT
),
452 diff_get_color_opt(&rev
.diffopt
, DIFF_RESET
));
453 ret
= show_object(o
->sha1
, 1, &rev
);
457 o
= parse_object(t
->tagged
->sha1
);
459 ret
= error(_("Could not read object %s"),
460 sha1_to_hex(t
->tagged
->sha1
));
468 printf("%stree %s%s\n\n",
469 diff_get_color_opt(&rev
.diffopt
, DIFF_COMMIT
),
471 diff_get_color_opt(&rev
.diffopt
, DIFF_RESET
));
472 read_tree_recursive((struct tree
*)o
, "", 0, 0, &match_all
,
473 show_tree_object
, NULL
);
477 rev
.pending
.nr
= rev
.pending
.alloc
= 0;
478 rev
.pending
.objects
= NULL
;
479 add_object_array(o
, name
, &rev
.pending
);
480 ret
= cmd_log_walk(&rev
);
483 ret
= error(_("Unknown type: %d"), o
->type
);
491 * This is equivalent to "git log -g --abbrev-commit --pretty=oneline"
493 int cmd_log_reflog(int argc
, const char **argv
, const char *prefix
)
496 struct setup_revision_opt opt
;
498 git_config(git_log_config
, NULL
);
500 if (diff_use_color_default
== -1)
501 diff_use_color_default
= git_use_color_default
;
503 init_revisions(&rev
, prefix
);
504 init_reflog_walk(&rev
.reflog_info
);
505 rev
.abbrev_commit
= 1;
506 rev
.verbose_header
= 1;
507 memset(&opt
, 0, sizeof(opt
));
509 cmd_log_init_defaults(&rev
);
510 rev
.commit_format
= CMIT_FMT_ONELINE
;
511 rev
.use_terminator
= 1;
512 rev
.always_show_header
= 1;
513 cmd_log_init_finish(argc
, argv
, prefix
, &rev
, &opt
);
515 return cmd_log_walk(&rev
);
518 int cmd_log(int argc
, const char **argv
, const char *prefix
)
521 struct setup_revision_opt opt
;
523 git_config(git_log_config
, NULL
);
525 if (diff_use_color_default
== -1)
526 diff_use_color_default
= git_use_color_default
;
528 init_revisions(&rev
, prefix
);
529 rev
.always_show_header
= 1;
530 memset(&opt
, 0, sizeof(opt
));
532 cmd_log_init(argc
, argv
, prefix
, &rev
, &opt
);
533 return cmd_log_walk(&rev
);
538 static const char *fmt_patch_suffix
= ".patch";
539 static int numbered
= 0;
540 static int auto_number
= 1;
542 static char *default_attach
= NULL
;
544 static struct string_list extra_hdr
;
545 static struct string_list extra_to
;
546 static struct string_list extra_cc
;
548 static void add_header(const char *value
)
550 struct string_list_item
*item
;
551 int len
= strlen(value
);
552 while (len
&& value
[len
- 1] == '\n')
555 if (!strncasecmp(value
, "to: ", 4)) {
556 item
= string_list_append(&extra_to
, value
+ 4);
558 } else if (!strncasecmp(value
, "cc: ", 4)) {
559 item
= string_list_append(&extra_cc
, value
+ 4);
562 item
= string_list_append(&extra_hdr
, value
);
565 item
->string
[len
] = '\0';
568 #define THREAD_SHALLOW 1
569 #define THREAD_DEEP 2
571 static int do_signoff
;
572 static const char *signature
= git_version_string
;
574 static int git_format_config(const char *var
, const char *value
, void *cb
)
576 if (!strcmp(var
, "format.headers")) {
578 die(_("format.headers without value"));
582 if (!strcmp(var
, "format.suffix"))
583 return git_config_string(&fmt_patch_suffix
, var
, value
);
584 if (!strcmp(var
, "format.to")) {
586 return config_error_nonbool(var
);
587 string_list_append(&extra_to
, value
);
590 if (!strcmp(var
, "format.cc")) {
592 return config_error_nonbool(var
);
593 string_list_append(&extra_cc
, value
);
596 if (!strcmp(var
, "diff.color") || !strcmp(var
, "color.diff")) {
599 if (!strcmp(var
, "format.numbered")) {
600 if (value
&& !strcasecmp(value
, "auto")) {
604 numbered
= git_config_bool(var
, value
);
605 auto_number
= auto_number
&& numbered
;
608 if (!strcmp(var
, "format.attach")) {
610 default_attach
= xstrdup(value
);
612 default_attach
= xstrdup(git_version_string
);
615 if (!strcmp(var
, "format.thread")) {
616 if (value
&& !strcasecmp(value
, "deep")) {
617 thread
= THREAD_DEEP
;
620 if (value
&& !strcasecmp(value
, "shallow")) {
621 thread
= THREAD_SHALLOW
;
624 thread
= git_config_bool(var
, value
) && THREAD_SHALLOW
;
627 if (!strcmp(var
, "format.signoff")) {
628 do_signoff
= git_config_bool(var
, value
);
631 if (!strcmp(var
, "format.signature"))
632 return git_config_string(&signature
, var
, value
);
634 return git_log_config(var
, value
, cb
);
637 static FILE *realstdout
= NULL
;
638 static const char *output_directory
= NULL
;
639 static int outdir_offset
;
641 static int reopen_stdout(struct commit
*commit
, struct rev_info
*rev
, int quiet
)
643 struct strbuf filename
= STRBUF_INIT
;
644 int suffix_len
= strlen(fmt_patch_suffix
) + 1;
646 if (output_directory
) {
647 strbuf_addstr(&filename
, output_directory
);
649 PATH_MAX
- FORMAT_PATCH_NAME_MAX
- suffix_len
)
650 return error(_("name of output directory is too long"));
651 if (filename
.buf
[filename
.len
- 1] != '/')
652 strbuf_addch(&filename
, '/');
655 get_patch_filename(commit
, rev
->nr
, fmt_patch_suffix
, &filename
);
658 fprintf(realstdout
, "%s\n", filename
.buf
+ outdir_offset
);
660 if (freopen(filename
.buf
, "w", stdout
) == NULL
)
661 return error(_("Cannot open patch file %s"), filename
.buf
);
663 strbuf_release(&filename
);
667 static void get_patch_ids(struct rev_info
*rev
, struct patch_ids
*ids
, const char *prefix
)
669 struct rev_info check_rev
;
670 struct commit
*commit
;
671 struct object
*o1
, *o2
;
672 unsigned flags1
, flags2
;
674 if (rev
->pending
.nr
!= 2)
675 die(_("Need exactly one range."));
677 o1
= rev
->pending
.objects
[0].item
;
679 o2
= rev
->pending
.objects
[1].item
;
682 if ((flags1
& UNINTERESTING
) == (flags2
& UNINTERESTING
))
683 die(_("Not a range."));
687 /* given a range a..b get all patch ids for b..a */
688 init_revisions(&check_rev
, prefix
);
689 o1
->flags
^= UNINTERESTING
;
690 o2
->flags
^= UNINTERESTING
;
691 add_pending_object(&check_rev
, o1
, "o1");
692 add_pending_object(&check_rev
, o2
, "o2");
693 if (prepare_revision_walk(&check_rev
))
694 die(_("revision walk setup failed"));
696 while ((commit
= get_revision(&check_rev
)) != NULL
) {
698 if (commit
->parents
&& commit
->parents
->next
)
701 add_commit_patch_id(commit
, ids
);
704 /* reset for next revision walk */
705 clear_commit_marks((struct commit
*)o1
,
706 SEEN
| UNINTERESTING
| SHOWN
| ADDED
);
707 clear_commit_marks((struct commit
*)o2
,
708 SEEN
| UNINTERESTING
| SHOWN
| ADDED
);
713 static void gen_message_id(struct rev_info
*info
, char *base
)
715 const char *committer
= git_committer_info(IDENT_WARN_ON_NO_NAME
);
716 const char *email_start
= strrchr(committer
, '<');
717 const char *email_end
= strrchr(committer
, '>');
718 struct strbuf buf
= STRBUF_INIT
;
719 if (!email_start
|| !email_end
|| email_start
> email_end
- 1)
720 die(_("Could not extract email from committer identity."));
721 strbuf_addf(&buf
, "%s.%lu.git.%.*s", base
,
722 (unsigned long) time(NULL
),
723 (int)(email_end
- email_start
- 1), email_start
+ 1);
724 info
->message_id
= strbuf_detach(&buf
, NULL
);
727 static void print_signature(void)
729 if (signature
&& *signature
)
730 printf("-- \n%s\n\n", signature
);
733 static void make_cover_letter(struct rev_info
*rev
, int use_stdout
,
734 int numbered
, int numbered_files
,
735 struct commit
*origin
,
736 int nr
, struct commit
**list
, struct commit
*head
,
739 const char *committer
;
740 const char *subject_start
= NULL
;
741 const char *body
= "*** SUBJECT HERE ***\n\n*** BLURB HERE ***\n";
743 const char *extra_headers
= rev
->extra_headers
;
745 struct strbuf sb
= STRBUF_INIT
;
747 const char *encoding
= "UTF-8";
748 struct diff_options opts
;
749 int need_8bit_cte
= 0;
750 struct commit
*commit
= NULL
;
752 if (rev
->commit_format
!= CMIT_FMT_EMAIL
)
753 die(_("Cover letter needs email format"));
755 committer
= git_committer_info(0);
757 if (!numbered_files
) {
759 * We fake a commit for the cover letter so we get the filename
762 commit
= xcalloc(1, sizeof(*commit
));
763 commit
->buffer
= xmalloc(400);
764 snprintf(commit
->buffer
, 400,
765 "tree 0000000000000000000000000000000000000000\n"
770 sha1_to_hex(head
->object
.sha1
), committer
, committer
);
773 if (!use_stdout
&& reopen_stdout(commit
, rev
, quiet
))
778 free(commit
->buffer
);
782 log_write_email_headers(rev
, head
, &subject_start
, &extra_headers
,
785 for (i
= 0; !need_8bit_cte
&& i
< nr
; i
++)
786 if (has_non_ascii(list
[i
]->buffer
))
790 pp_user_info(NULL
, CMIT_FMT_EMAIL
, &sb
, committer
, DATE_RFC2822
,
792 pp_title_line(CMIT_FMT_EMAIL
, &msg
, &sb
, subject_start
, extra_headers
,
793 encoding
, need_8bit_cte
);
794 pp_remainder(CMIT_FMT_EMAIL
, &msg
, &sb
, 0);
795 printf("%s\n", sb
.buf
);
804 for (i
= 0; i
< nr
; i
++)
805 shortlog_add_commit(&log
, list
[i
]);
807 shortlog_output(&log
);
810 * We can only do diffstat with a unique reference point
815 memcpy(&opts
, &rev
->diffopt
, sizeof(opts
));
816 opts
.output_format
= DIFF_FORMAT_SUMMARY
| DIFF_FORMAT_DIFFSTAT
;
818 diff_setup_done(&opts
);
820 diff_tree_sha1(origin
->tree
->object
.sha1
,
821 head
->tree
->object
.sha1
,
830 static const char *clean_message_id(const char *msg_id
)
833 const char *a
, *z
, *m
;
836 while ((ch
= *m
) && (isspace(ch
) || (ch
== '<')))
841 if (!isspace(ch
) && (ch
!= '>'))
846 die(_("insane in-reply-to: %s"), msg_id
);
849 return xmemdupz(a
, z
- a
);
852 static const char *set_outdir(const char *prefix
, const char *output_directory
)
854 if (output_directory
&& is_absolute_path(output_directory
))
855 return output_directory
;
857 if (!prefix
|| !*prefix
) {
858 if (output_directory
)
859 return output_directory
;
860 /* The user did not explicitly ask for "./" */
865 outdir_offset
= strlen(prefix
);
866 if (!output_directory
)
869 return xstrdup(prefix_filename(prefix
, outdir_offset
,
873 static const char * const builtin_format_patch_usage
[] = {
874 "git format-patch [options] [<since> | <revision range>]",
878 static int keep_subject
= 0;
880 static int keep_callback(const struct option
*opt
, const char *arg
, int unset
)
882 ((struct rev_info
*)opt
->value
)->total
= -1;
887 static int subject_prefix
= 0;
889 static int subject_prefix_callback(const struct option
*opt
, const char *arg
,
893 ((struct rev_info
*)opt
->value
)->subject_prefix
= arg
;
897 static int numbered_cmdline_opt
= 0;
899 static int numbered_callback(const struct option
*opt
, const char *arg
,
902 *(int *)opt
->value
= numbered_cmdline_opt
= unset
? 0 : 1;
908 static int no_numbered_callback(const struct option
*opt
, const char *arg
,
911 return numbered_callback(opt
, arg
, 1);
914 static int output_directory_callback(const struct option
*opt
, const char *arg
,
917 const char **dir
= (const char **)opt
->value
;
919 die(_("Two output directories?"));
924 static int thread_callback(const struct option
*opt
, const char *arg
, int unset
)
926 int *thread
= (int *)opt
->value
;
929 else if (!arg
|| !strcmp(arg
, "shallow"))
930 *thread
= THREAD_SHALLOW
;
931 else if (!strcmp(arg
, "deep"))
932 *thread
= THREAD_DEEP
;
938 static int attach_callback(const struct option
*opt
, const char *arg
, int unset
)
940 struct rev_info
*rev
= (struct rev_info
*)opt
->value
;
942 rev
->mime_boundary
= NULL
;
944 rev
->mime_boundary
= arg
;
946 rev
->mime_boundary
= git_version_string
;
947 rev
->no_inline
= unset
? 0 : 1;
951 static int inline_callback(const struct option
*opt
, const char *arg
, int unset
)
953 struct rev_info
*rev
= (struct rev_info
*)opt
->value
;
955 rev
->mime_boundary
= NULL
;
957 rev
->mime_boundary
= arg
;
959 rev
->mime_boundary
= git_version_string
;
964 static int header_callback(const struct option
*opt
, const char *arg
, int unset
)
967 string_list_clear(&extra_hdr
, 0);
968 string_list_clear(&extra_to
, 0);
969 string_list_clear(&extra_cc
, 0);
976 static int to_callback(const struct option
*opt
, const char *arg
, int unset
)
979 string_list_clear(&extra_to
, 0);
981 string_list_append(&extra_to
, arg
);
985 static int cc_callback(const struct option
*opt
, const char *arg
, int unset
)
988 string_list_clear(&extra_cc
, 0);
990 string_list_append(&extra_cc
, arg
);
994 int cmd_format_patch(int argc
, const char **argv
, const char *prefix
)
996 struct commit
*commit
;
997 struct commit
**list
= NULL
;
999 struct setup_revision_opt s_r_opt
;
1000 int nr
= 0, total
, i
;
1002 int start_number
= -1;
1003 int numbered_files
= 0; /* _just_ numbers */
1004 int ignore_if_in_upstream
= 0;
1005 int cover_letter
= 0;
1006 int boundary_count
= 0;
1007 int no_binary_diff
= 0;
1008 struct commit
*origin
= NULL
, *head
= NULL
;
1009 const char *in_reply_to
= NULL
;
1010 struct patch_ids ids
;
1011 char *add_signoff
= NULL
;
1012 struct strbuf buf
= STRBUF_INIT
;
1013 int use_patch_format
= 0;
1015 const struct option builtin_format_patch_options
[] = {
1016 { OPTION_CALLBACK
, 'n', "numbered", &numbered
, NULL
,
1017 "use [PATCH n/m] even with a single patch",
1018 PARSE_OPT_NOARG
, numbered_callback
},
1019 { OPTION_CALLBACK
, 'N', "no-numbered", &numbered
, NULL
,
1020 "use [PATCH] even with multiple patches",
1021 PARSE_OPT_NOARG
, no_numbered_callback
},
1022 OPT_BOOLEAN('s', "signoff", &do_signoff
, "add Signed-off-by:"),
1023 OPT_BOOLEAN(0, "stdout", &use_stdout
,
1024 "print patches to standard out"),
1025 OPT_BOOLEAN(0, "cover-letter", &cover_letter
,
1026 "generate a cover letter"),
1027 OPT_BOOLEAN(0, "numbered-files", &numbered_files
,
1028 "use simple number sequence for output file names"),
1029 OPT_STRING(0, "suffix", &fmt_patch_suffix
, "sfx",
1030 "use <sfx> instead of '.patch'"),
1031 OPT_INTEGER(0, "start-number", &start_number
,
1032 "start numbering patches at <n> instead of 1"),
1033 { OPTION_CALLBACK
, 0, "subject-prefix", &rev
, "prefix",
1034 "Use [<prefix>] instead of [PATCH]",
1035 PARSE_OPT_NONEG
, subject_prefix_callback
},
1036 { OPTION_CALLBACK
, 'o', "output-directory", &output_directory
,
1037 "dir", "store resulting files in <dir>",
1038 PARSE_OPT_NONEG
, output_directory_callback
},
1039 { OPTION_CALLBACK
, 'k', "keep-subject", &rev
, NULL
,
1040 "don't strip/add [PATCH]",
1041 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
, keep_callback
},
1042 OPT_BOOLEAN(0, "no-binary", &no_binary_diff
,
1043 "don't output binary diffs"),
1044 OPT_BOOLEAN(0, "ignore-if-in-upstream", &ignore_if_in_upstream
,
1045 "don't include a patch matching a commit upstream"),
1046 { OPTION_BOOLEAN
, 'p', "no-stat", &use_patch_format
, NULL
,
1047 "show patch format instead of default (patch + stat)",
1048 PARSE_OPT_NONEG
| PARSE_OPT_NOARG
},
1049 OPT_GROUP("Messaging"),
1050 { OPTION_CALLBACK
, 0, "add-header", NULL
, "header",
1051 "add email header", 0, header_callback
},
1052 { OPTION_CALLBACK
, 0, "to", NULL
, "email", "add To: header",
1054 { OPTION_CALLBACK
, 0, "cc", NULL
, "email", "add Cc: header",
1056 OPT_STRING(0, "in-reply-to", &in_reply_to
, "message-id",
1057 "make first mail a reply to <message-id>"),
1058 { OPTION_CALLBACK
, 0, "attach", &rev
, "boundary",
1059 "attach the patch", PARSE_OPT_OPTARG
,
1061 { OPTION_CALLBACK
, 0, "inline", &rev
, "boundary",
1063 PARSE_OPT_OPTARG
| PARSE_OPT_NONEG
,
1065 { OPTION_CALLBACK
, 0, "thread", &thread
, "style",
1066 "enable message threading, styles: shallow, deep",
1067 PARSE_OPT_OPTARG
, thread_callback
},
1068 OPT_STRING(0, "signature", &signature
, "signature",
1070 OPT_BOOLEAN(0, "quiet", &quiet
,
1071 "don't print the patch filenames"),
1075 extra_hdr
.strdup_strings
= 1;
1076 extra_to
.strdup_strings
= 1;
1077 extra_cc
.strdup_strings
= 1;
1078 git_config(git_format_config
, NULL
);
1079 init_revisions(&rev
, prefix
);
1080 rev
.commit_format
= CMIT_FMT_EMAIL
;
1081 rev
.verbose_header
= 1;
1083 rev
.max_parents
= 1;
1084 DIFF_OPT_SET(&rev
.diffopt
, RECURSIVE
);
1085 rev
.subject_prefix
= fmt_patch_subject_prefix
;
1086 memset(&s_r_opt
, 0, sizeof(s_r_opt
));
1087 s_r_opt
.def
= "HEAD";
1089 if (default_attach
) {
1090 rev
.mime_boundary
= default_attach
;
1095 * Parse the arguments before setup_revisions(), or something
1096 * like "git format-patch -o a123 HEAD^.." may fail; a123 is
1097 * possibly a valid SHA1.
1099 argc
= parse_options(argc
, argv
, prefix
, builtin_format_patch_options
,
1100 builtin_format_patch_usage
,
1101 PARSE_OPT_KEEP_ARGV0
| PARSE_OPT_KEEP_UNKNOWN
|
1102 PARSE_OPT_KEEP_DASHDASH
);
1105 const char *committer
;
1107 committer
= git_committer_info(IDENT_ERROR_ON_NO_NAME
);
1108 endpos
= strchr(committer
, '>');
1110 die(_("bogus committer info %s"), committer
);
1111 add_signoff
= xmemdupz(committer
, endpos
- committer
+ 1);
1114 for (i
= 0; i
< extra_hdr
.nr
; i
++) {
1115 strbuf_addstr(&buf
, extra_hdr
.items
[i
].string
);
1116 strbuf_addch(&buf
, '\n');
1120 strbuf_addstr(&buf
, "To: ");
1121 for (i
= 0; i
< extra_to
.nr
; i
++) {
1123 strbuf_addstr(&buf
, " ");
1124 strbuf_addstr(&buf
, extra_to
.items
[i
].string
);
1125 if (i
+ 1 < extra_to
.nr
)
1126 strbuf_addch(&buf
, ',');
1127 strbuf_addch(&buf
, '\n');
1131 strbuf_addstr(&buf
, "Cc: ");
1132 for (i
= 0; i
< extra_cc
.nr
; i
++) {
1134 strbuf_addstr(&buf
, " ");
1135 strbuf_addstr(&buf
, extra_cc
.items
[i
].string
);
1136 if (i
+ 1 < extra_cc
.nr
)
1137 strbuf_addch(&buf
, ',');
1138 strbuf_addch(&buf
, '\n');
1141 rev
.extra_headers
= strbuf_detach(&buf
, NULL
);
1143 if (start_number
< 0)
1147 * If numbered is set solely due to format.numbered in config,
1148 * and it would conflict with --keep-subject (-k) from the
1149 * command line, reset "numbered".
1151 if (numbered
&& keep_subject
&& !numbered_cmdline_opt
)
1154 if (numbered
&& keep_subject
)
1155 die (_("-n and -k are mutually exclusive."));
1156 if (keep_subject
&& subject_prefix
)
1157 die (_("--subject-prefix and -k are mutually exclusive."));
1159 argc
= setup_revisions(argc
, argv
, &rev
, &s_r_opt
);
1161 die (_("unrecognized argument: %s"), argv
[1]);
1163 if (rev
.diffopt
.output_format
& DIFF_FORMAT_NAME
)
1164 die(_("--name-only does not make sense"));
1165 if (rev
.diffopt
.output_format
& DIFF_FORMAT_NAME_STATUS
)
1166 die(_("--name-status does not make sense"));
1167 if (rev
.diffopt
.output_format
& DIFF_FORMAT_CHECKDIFF
)
1168 die(_("--check does not make sense"));
1170 if (!use_patch_format
&&
1171 (!rev
.diffopt
.output_format
||
1172 rev
.diffopt
.output_format
== DIFF_FORMAT_PATCH
))
1173 rev
.diffopt
.output_format
= DIFF_FORMAT_DIFFSTAT
| DIFF_FORMAT_SUMMARY
;
1175 /* Always generate a patch */
1176 rev
.diffopt
.output_format
|= DIFF_FORMAT_PATCH
;
1178 if (!DIFF_OPT_TST(&rev
.diffopt
, TEXT
) && !no_binary_diff
)
1179 DIFF_OPT_SET(&rev
.diffopt
, BINARY
);
1182 init_display_notes(&rev
.notes_opt
);
1185 output_directory
= set_outdir(prefix
, output_directory
);
1189 if (output_directory
) {
1191 die(_("standard output, or directory, which one?"));
1192 if (mkdir(output_directory
, 0777) < 0 && errno
!= EEXIST
)
1193 die_errno(_("Could not create directory '%s'"),
1197 if (rev
.pending
.nr
== 1) {
1198 if (rev
.max_count
< 0 && !rev
.show_root_diff
) {
1200 * This is traditional behaviour of "git format-patch
1201 * origin" that prepares what the origin side still
1204 rev
.pending
.objects
[0].item
->flags
|= UNINTERESTING
;
1205 add_head_to_pending(&rev
);
1208 * Otherwise, it is "format-patch -22 HEAD", and/or
1209 * "format-patch --root HEAD". The user wants
1210 * get_revision() to do the usual traversal.
1215 * We cannot move this anywhere earlier because we do want to
1216 * know if --root was given explicitly from the command line.
1218 rev
.show_root_diff
= 1;
1221 /* remember the range */
1223 for (i
= 0; i
< rev
.pending
.nr
; i
++) {
1224 struct object
*o
= rev
.pending
.objects
[i
].item
;
1225 if (!(o
->flags
& UNINTERESTING
))
1226 head
= (struct commit
*)o
;
1228 /* We can't generate a cover letter without any patches */
1233 if (ignore_if_in_upstream
) {
1234 /* Don't say anything if head and upstream are the same. */
1235 if (rev
.pending
.nr
== 2) {
1236 struct object_array_entry
*o
= rev
.pending
.objects
;
1237 if (hashcmp(o
[0].item
->sha1
, o
[1].item
->sha1
) == 0)
1240 get_patch_ids(&rev
, &ids
, prefix
);
1244 realstdout
= xfdopen(xdup(1), "w");
1246 if (prepare_revision_walk(&rev
))
1247 die(_("revision walk setup failed"));
1249 while ((commit
= get_revision(&rev
)) != NULL
) {
1250 if (commit
->object
.flags
& BOUNDARY
) {
1252 origin
= (boundary_count
== 1) ? commit
: NULL
;
1256 if (ignore_if_in_upstream
&&
1257 has_commit_patch_id(commit
, &ids
))
1261 list
= xrealloc(list
, nr
* sizeof(list
[0]));
1262 list
[nr
- 1] = commit
;
1265 if (!keep_subject
&& auto_number
&& total
> 1)
1268 rev
.total
= total
+ start_number
- 1;
1269 if (in_reply_to
|| thread
|| cover_letter
)
1270 rev
.ref_message_ids
= xcalloc(1, sizeof(struct string_list
));
1272 const char *msgid
= clean_message_id(in_reply_to
);
1273 string_list_append(rev
.ref_message_ids
, msgid
);
1275 rev
.numbered_files
= numbered_files
;
1276 rev
.patch_suffix
= fmt_patch_suffix
;
1279 gen_message_id(&rev
, "cover");
1280 make_cover_letter(&rev
, use_stdout
, numbered
, numbered_files
,
1281 origin
, nr
, list
, head
, quiet
);
1285 rev
.add_signoff
= add_signoff
;
1289 rev
.nr
= total
- nr
+ (start_number
- 1);
1290 /* Make the second and subsequent mails replies to the first */
1292 /* Have we already had a message ID? */
1293 if (rev
.message_id
) {
1295 * For deep threading: make every mail
1296 * a reply to the previous one, no
1297 * matter what other options are set.
1299 * For shallow threading:
1301 * Without --cover-letter and
1302 * --in-reply-to, make every mail a
1303 * reply to the one before.
1305 * With --in-reply-to but no
1306 * --cover-letter, make every mail a
1307 * reply to the <reply-to>.
1309 * With --cover-letter, make every
1310 * mail but the cover letter a reply
1311 * to the cover letter. The cover
1312 * letter is a reply to the
1313 * --in-reply-to, if specified.
1315 if (thread
== THREAD_SHALLOW
1316 && rev
.ref_message_ids
->nr
> 0
1317 && (!cover_letter
|| rev
.nr
> 1))
1318 free(rev
.message_id
);
1320 string_list_append(rev
.ref_message_ids
,
1323 gen_message_id(&rev
, sha1_to_hex(commit
->object
.sha1
));
1326 if (!use_stdout
&& reopen_stdout(numbered_files
? NULL
: commit
,
1328 die(_("Failed to create output files"));
1329 shown
= log_tree_commit(&rev
, commit
);
1330 free(commit
->buffer
);
1331 commit
->buffer
= NULL
;
1333 /* We put one extra blank line between formatted
1334 * patches and this flag is used by log-tree code
1335 * to see if it needs to emit a LF before showing
1336 * the log; when using one file per patch, we do
1337 * not want the extra blank line.
1342 if (rev
.mime_boundary
)
1343 printf("\n--%s%s--\n\n\n",
1344 mime_boundary_leader
,
1353 string_list_clear(&extra_to
, 0);
1354 string_list_clear(&extra_cc
, 0);
1355 string_list_clear(&extra_hdr
, 0);
1356 if (ignore_if_in_upstream
)
1357 free_patch_ids(&ids
);
1361 static int add_pending_commit(const char *arg
, struct rev_info
*revs
, int flags
)
1363 unsigned char sha1
[20];
1364 if (get_sha1(arg
, sha1
) == 0) {
1365 struct commit
*commit
= lookup_commit_reference(sha1
);
1367 commit
->object
.flags
|= flags
;
1368 add_pending_object(revs
, &commit
->object
, arg
);
1375 static const char * const cherry_usage
[] = {
1376 "git cherry [-v] [<upstream> [<head> [<limit>]]]",
1380 static void print_commit(char sign
, struct commit
*commit
, int verbose
,
1384 printf("%c %s\n", sign
,
1385 find_unique_abbrev(commit
->object
.sha1
, abbrev
));
1387 struct strbuf buf
= STRBUF_INIT
;
1388 struct pretty_print_context ctx
= {0};
1389 pretty_print_commit(CMIT_FMT_ONELINE
, commit
, &buf
, &ctx
);
1390 printf("%c %s %s\n", sign
,
1391 find_unique_abbrev(commit
->object
.sha1
, abbrev
),
1393 strbuf_release(&buf
);
1397 int cmd_cherry(int argc
, const char **argv
, const char *prefix
)
1399 struct rev_info revs
;
1400 struct patch_ids ids
;
1401 struct commit
*commit
;
1402 struct commit_list
*list
= NULL
;
1403 struct branch
*current_branch
;
1404 const char *upstream
;
1405 const char *head
= "HEAD";
1406 const char *limit
= NULL
;
1407 int verbose
= 0, abbrev
= 0;
1409 struct option options
[] = {
1410 OPT__ABBREV(&abbrev
),
1411 OPT__VERBOSE(&verbose
, "be verbose"),
1415 argc
= parse_options(argc
, argv
, prefix
, options
, cherry_usage
, 0);
1428 current_branch
= branch_get(NULL
);
1429 if (!current_branch
|| !current_branch
->merge
1430 || !current_branch
->merge
[0]
1431 || !current_branch
->merge
[0]->dst
) {
1432 fprintf(stderr
, _("Could not find a tracked"
1433 " remote branch, please"
1434 " specify <upstream> manually.\n"));
1435 usage_with_options(cherry_usage
, options
);
1438 upstream
= current_branch
->merge
[0]->dst
;
1441 init_revisions(&revs
, prefix
);
1443 revs
.combine_merges
= 0;
1444 revs
.ignore_merges
= 1;
1445 DIFF_OPT_SET(&revs
.diffopt
, RECURSIVE
);
1447 if (add_pending_commit(head
, &revs
, 0))
1448 die(_("Unknown commit %s"), head
);
1449 if (add_pending_commit(upstream
, &revs
, UNINTERESTING
))
1450 die(_("Unknown commit %s"), upstream
);
1452 /* Don't say anything if head and upstream are the same. */
1453 if (revs
.pending
.nr
== 2) {
1454 struct object_array_entry
*o
= revs
.pending
.objects
;
1455 if (hashcmp(o
[0].item
->sha1
, o
[1].item
->sha1
) == 0)
1459 get_patch_ids(&revs
, &ids
, prefix
);
1461 if (limit
&& add_pending_commit(limit
, &revs
, UNINTERESTING
))
1462 die(_("Unknown commit %s"), limit
);
1464 /* reverse the list of commits */
1465 if (prepare_revision_walk(&revs
))
1466 die(_("revision walk setup failed"));
1467 while ((commit
= get_revision(&revs
)) != NULL
) {
1469 if (commit
->parents
&& commit
->parents
->next
)
1472 commit_list_insert(commit
, &list
);
1478 commit
= list
->item
;
1479 if (has_commit_patch_id(commit
, &ids
))
1481 print_commit(sign
, commit
, verbose
, abbrev
);
1485 free_patch_ids(&ids
);