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"
24 /* Set a default date-time format for git log ("log.date" config variable) */
25 static const char *default_date_mode
= NULL
;
27 static int default_show_root
= 1;
28 static int decoration_style
;
29 static const char *fmt_patch_subject_prefix
= "PATCH";
30 static const char *fmt_pretty
;
31 static const char *dashdash
= "--";
33 static char builtin_log_usage
[] =
34 "git log [<options>] [<since>..<until>] [[--] <path>...]\n"
35 "git log [<options>] -L n,m <path>\n"
36 " or: git show [options] <object>...";
38 static const char *log_opt_usage
[] = {
43 struct line_opt_callback_data
{
44 struct diff_line_range
**range
;
45 struct parse_opt_ctx_t
*ctx
;
49 static int parse_decoration_style(const char *var
, const char *value
)
51 switch (git_config_maybe_bool(var
, value
)) {
53 return DECORATE_SHORT_REFS
;
59 if (!strcmp(value
, "full"))
60 return DECORATE_FULL_REFS
;
61 else if (!strcmp(value
, "short"))
62 return DECORATE_SHORT_REFS
;
66 static int log_line_range_callback(const struct option
*option
, const char *arg
, int unset
)
68 struct line_opt_callback_data
*data
= option
->value
;
69 struct diff_line_range
*r
= *data
->range
;
70 struct parse_opt_ctx_t
*ctx
= data
->ctx
;
75 if (r
->nr
== 0 && r
->next
== NULL
)
76 ctx
->out
[ctx
->cpidx
++] = dashdash
;
78 diff_line_range_append(r
, arg
);
79 data
->rev
->line_level_traverse
= 1;
83 static void cmd_log_init(int argc
, const char **argv
, const char *prefix
,
84 struct rev_info
*rev
, struct setup_revision_opt
*opt
)
87 int decoration_given
= 0;
88 static int full_line_diff
;
89 struct userformat_want w
;
90 const char *path
= NULL
, *fullpath
= NULL
;
91 static struct diff_line_range
*range
;
92 struct diff_line_range
*r
= NULL
;
93 static struct parse_opt_ctx_t ctx
;
94 static struct line_opt_callback_data line_cb
= {&range
, &ctx
, NULL
};
95 static const struct option options
[] = {
96 OPT_CALLBACK('L', NULL
, &line_cb
, "n,m",
97 "Process only line range n,m, counting from 1",
98 log_line_range_callback
),
99 OPT_BOOLEAN(0, "full-line-diff", &full_line_diff
,
100 "Always print the interesting range even if the \
101 current commit does not change any line of it"),
106 range
= xmalloc(sizeof(*range
));
107 DIFF_LINE_RANGE_INIT(range
);
109 rev
->abbrev
= DEFAULT_ABBREV
;
110 rev
->commit_format
= CMIT_FMT_DEFAULT
;
112 get_commit_format(fmt_pretty
, rev
);
113 rev
->verbose_header
= 1;
114 DIFF_OPT_SET(&rev
->diffopt
, RECURSIVE
);
115 rev
->show_root_diff
= default_show_root
;
116 rev
->subject_prefix
= fmt_patch_subject_prefix
;
117 DIFF_OPT_SET(&rev
->diffopt
, ALLOW_TEXTCONV
);
119 if (default_date_mode
)
120 rev
->date_mode
= parse_date_format(default_date_mode
);
123 * Check for -h before setup_revisions(), or "git log -h" will
124 * fail when run without a git directory.
126 if (argc
== 2 && !strcmp(argv
[1], "-h"))
127 usage(builtin_log_usage
);
129 parse_options_start(&ctx
, argc
, argv
, prefix
, PARSE_OPT_KEEP_DASHDASH
|
130 PARSE_OPT_KEEP_ARGV0
| PARSE_OPT_STOP_AT_NON_OPTION
);
132 switch (parse_options_step(&ctx
, options
, log_opt_usage
)) {
137 case PARSE_OPT_NON_OPTION
:
138 path
= parse_options_current(&ctx
);
139 fullpath
= prefix_path(prefix
, prefix
? strlen(prefix
) : 0, path
);
140 range
->spec
= alloc_filespec(fullpath
);
141 free((void *)fullpath
);
142 if (range
->nr
== 0) {
144 die("Path %s need a -L <range> option\n"
145 "If you want follow the history of the whole file "
146 "use 'git log -L 1,$ <path>'", range
->spec
->path
);
148 parse_options_next(&ctx
, 1);
152 r
= xmalloc(sizeof(*r
));
153 DIFF_LINE_RANGE_INIT(r
);
156 parse_options_next(&ctx
, 1);
158 case PARSE_OPT_UNKNOWN
:
159 parse_options_next(&ctx
, 1);
163 parse_revision_opt(rev
, &ctx
, options
, log_opt_usage
);
166 argc
= parse_options_end(&ctx
);
168 /* die if '-L <range>' with no pathspec follow */
169 if (range
->nr
> 0 && range
->spec
== NULL
)
170 die("Each -L should follow a path");
171 /* clear up the last range */
172 if (range
->nr
== 0) {
173 struct diff_line_range
*r
= range
->next
;
174 DIFF_LINE_RANGE_CLEAR(range
);
178 argc
= setup_revisions(argc
, argv
, rev
, opt
);
180 memset(&w
, 0, sizeof(w
));
181 userformat_find_requirements(NULL
, &w
);
183 if (!rev
->show_notes_given
&& (!rev
->pretty_given
|| w
.notes
))
186 init_display_notes(&rev
->notes_opt
);
188 if (rev
->diffopt
.pickaxe
|| rev
->diffopt
.filter
)
189 rev
->always_show_header
= 0;
190 if (DIFF_OPT_TST(&rev
->diffopt
, FOLLOW_RENAMES
)) {
191 rev
->always_show_header
= 0;
192 if (rev
->diffopt
.nr_paths
!= 1)
193 usage("git logs can only follow renames on one pathname at a time");
195 for (i
= 1; i
< argc
; i
++) {
196 const char *arg
= argv
[i
];
197 if (!strcmp(arg
, "--decorate")) {
198 decoration_style
= DECORATE_SHORT_REFS
;
199 decoration_given
= 1;
200 } else if (!prefixcmp(arg
, "--decorate=")) {
201 const char *v
= skip_prefix(arg
, "--decorate=");
202 decoration_style
= parse_decoration_style(arg
, v
);
203 if (decoration_style
< 0)
204 die("invalid --decorate option: %s", arg
);
205 decoration_given
= 1;
206 } else if (!strcmp(arg
, "--no-decorate")) {
207 decoration_style
= 0;
208 } else if (!strcmp(arg
, "--source")) {
209 rev
->show_source
= 1;
210 } else if (!strcmp(arg
, "-h")) {
211 usage(builtin_log_usage
);
213 die("unrecognized argument: %s", arg
);
217 * defeat log.decorate configuration interacting with --pretty=raw
218 * from the command line.
220 if (!decoration_given
&& rev
->pretty_given
221 && rev
->commit_format
== CMIT_FMT_RAW
)
222 decoration_style
= 0;
224 if (decoration_style
) {
225 rev
->show_decorations
= 1;
226 load_ref_decorations(decoration_style
);
229 /* Test whether line level history is asked for */
230 if (range
&& range
->nr
> 0) {
231 setup_line(rev
, range
);
232 rev
->full_line_diff
= full_line_diff
;
238 * This gives a rough estimate for how many commits we
239 * will print out in the list.
241 static int estimate_commit_count(struct rev_info
*rev
, struct commit_list
*list
)
246 struct commit
*commit
= list
->item
;
247 unsigned int flags
= commit
->object
.flags
;
249 if (!(flags
& (TREESAME
| UNINTERESTING
)))
255 static void show_early_header(struct rev_info
*rev
, const char *stage
, int nr
)
257 if (rev
->shown_one
) {
259 if (rev
->commit_format
!= CMIT_FMT_ONELINE
)
260 putchar(rev
->diffopt
.line_termination
);
262 printf("Final output: %d %s\n", nr
, stage
);
265 static struct itimerval early_output_timer
;
267 static void log_show_early(struct rev_info
*revs
, struct commit_list
*list
)
269 int i
= revs
->early_output
;
272 sort_in_topological_order(&list
, revs
->lifo
);
274 struct commit
*commit
= list
->item
;
275 switch (simplify_commit(revs
, commit
)) {
278 int n
= estimate_commit_count(revs
, list
);
279 show_early_header(revs
, "incomplete", n
);
282 log_tree_commit(revs
, commit
);
293 /* Did we already get enough commits for the early output? */
298 * ..if no, then repeat it twice a second until we
301 * NOTE! We don't use "it_interval", because if the
302 * reader isn't listening, we want our output to be
303 * throttled by the writing, and not have the timer
304 * trigger every second even if we're blocked on a
307 early_output_timer
.it_value
.tv_sec
= 0;
308 early_output_timer
.it_value
.tv_usec
= 500000;
309 setitimer(ITIMER_REAL
, &early_output_timer
, NULL
);
312 static void early_output(int signal
)
314 show_early_output
= log_show_early
;
317 static void setup_early_output(struct rev_info
*rev
)
322 * Set up the signal handler, minimally intrusively:
323 * we only set a single volatile integer word (not
324 * using sigatomic_t - trying to avoid unnecessary
325 * system dependencies and headers), and using
328 memset(&sa
, 0, sizeof(sa
));
329 sa
.sa_handler
= early_output
;
330 sigemptyset(&sa
.sa_mask
);
331 sa
.sa_flags
= SA_RESTART
;
332 sigaction(SIGALRM
, &sa
, NULL
);
335 * If we can get the whole output in less than a
336 * tenth of a second, don't even bother doing the
337 * early-output thing..
339 * This is a one-time-only trigger.
341 early_output_timer
.it_value
.tv_sec
= 0;
342 early_output_timer
.it_value
.tv_usec
= 100000;
343 setitimer(ITIMER_REAL
, &early_output_timer
, NULL
);
346 static void finish_early_output(struct rev_info
*rev
)
348 int n
= estimate_commit_count(rev
, rev
->commits
);
349 signal(SIGALRM
, SIG_IGN
);
350 show_early_header(rev
, "done", n
);
353 static int cmd_log_walk(struct rev_info
*rev
)
355 struct commit
*commit
;
357 if (rev
->early_output
)
358 setup_early_output(rev
);
360 if (prepare_revision_walk(rev
))
361 die("revision walk setup failed");
363 if (rev
->early_output
)
364 finish_early_output(rev
);
367 * For --check and --exit-code, the exit code is based on CHECK_FAILED
368 * and HAS_CHANGES being accumulated in rev->diffopt, so be careful to
369 * retain that state information if replacing rev->diffopt in this loop
371 while ((commit
= get_revision(rev
)) != NULL
) {
372 log_tree_commit(rev
, commit
);
373 if (!rev
->reflog_info
) {
374 /* we allow cycles in reflog ancestry */
375 free(commit
->buffer
);
376 commit
->buffer
= NULL
;
378 free_commit_list(commit
->parents
);
379 commit
->parents
= NULL
;
381 if (rev
->diffopt
.output_format
& DIFF_FORMAT_CHECKDIFF
&&
382 DIFF_OPT_TST(&rev
->diffopt
, CHECK_FAILED
)) {
385 return diff_result_code(&rev
->diffopt
, 0);
388 static int git_log_config(const char *var
, const char *value
, void *cb
)
390 if (!strcmp(var
, "format.pretty"))
391 return git_config_string(&fmt_pretty
, var
, value
);
392 if (!strcmp(var
, "format.subjectprefix"))
393 return git_config_string(&fmt_patch_subject_prefix
, var
, value
);
394 if (!strcmp(var
, "log.date"))
395 return git_config_string(&default_date_mode
, var
, value
);
396 if (!strcmp(var
, "log.decorate")) {
397 decoration_style
= parse_decoration_style(var
, value
);
398 if (decoration_style
< 0)
399 decoration_style
= 0; /* maybe warn? */
402 if (!strcmp(var
, "log.showroot")) {
403 default_show_root
= git_config_bool(var
, value
);
406 if (!prefixcmp(var
, "color.decorate."))
407 return parse_decorate_color_config(var
, 15, value
);
409 return git_diff_ui_config(var
, value
, cb
);
412 int cmd_whatchanged(int argc
, const char **argv
, const char *prefix
)
415 struct setup_revision_opt opt
;
417 git_config(git_log_config
, NULL
);
419 if (diff_use_color_default
== -1)
420 diff_use_color_default
= git_use_color_default
;
422 init_revisions(&rev
, prefix
);
424 rev
.simplify_history
= 0;
425 memset(&opt
, 0, sizeof(opt
));
427 cmd_log_init(argc
, argv
, prefix
, &rev
, &opt
);
428 if (!rev
.diffopt
.output_format
)
429 rev
.diffopt
.output_format
= DIFF_FORMAT_RAW
;
430 return cmd_log_walk(&rev
);
433 static void show_tagger(char *buf
, int len
, struct rev_info
*rev
)
435 struct strbuf out
= STRBUF_INIT
;
437 pp_user_info("Tagger", rev
->commit_format
, &out
, buf
, rev
->date_mode
,
438 git_log_output_encoding
?
439 git_log_output_encoding
: git_commit_encoding
);
440 printf("%s", out
.buf
);
441 strbuf_release(&out
);
444 static int show_object(const unsigned char *sha1
, int show_tag_object
,
445 struct rev_info
*rev
)
448 enum object_type type
;
449 char *buf
= read_sha1_file(sha1
, &type
, &size
);
453 return error("Could not read object %s", sha1_to_hex(sha1
));
456 while (offset
< size
&& buf
[offset
] != '\n') {
457 int new_offset
= offset
+ 1;
458 while (new_offset
< size
&& buf
[new_offset
++] != '\n')
460 if (!prefixcmp(buf
+ offset
, "tagger "))
461 show_tagger(buf
+ offset
+ 7,
462 new_offset
- offset
- 7, rev
);
467 fwrite(buf
+ offset
, size
- offset
, 1, stdout
);
472 static int show_tree_object(const unsigned char *sha1
,
473 const char *base
, int baselen
,
474 const char *pathname
, unsigned mode
, int stage
, void *context
)
476 printf("%s%s\n", pathname
, S_ISDIR(mode
) ? "/" : "");
480 static void show_rev_tweak_rev(struct rev_info
*rev
, struct setup_revision_opt
*opt
)
482 if (rev
->ignore_merges
) {
483 /* There was no "-m" on the command line */
484 rev
->ignore_merges
= 0;
485 if (!rev
->first_parent_only
&& !rev
->combine_merges
) {
486 /* No "--first-parent", "-c", nor "--cc" */
487 rev
->combine_merges
= 1;
488 rev
->dense_combined_merges
= 1;
491 if (!rev
->diffopt
.output_format
)
492 rev
->diffopt
.output_format
= DIFF_FORMAT_PATCH
;
495 int cmd_show(int argc
, const char **argv
, const char *prefix
)
498 struct object_array_entry
*objects
;
499 struct setup_revision_opt opt
;
500 int i
, count
, ret
= 0;
502 git_config(git_log_config
, NULL
);
504 if (diff_use_color_default
== -1)
505 diff_use_color_default
= git_use_color_default
;
507 init_revisions(&rev
, prefix
);
509 rev
.always_show_header
= 1;
511 memset(&opt
, 0, sizeof(opt
));
513 opt
.tweak
= show_rev_tweak_rev
;
514 cmd_log_init(argc
, argv
, prefix
, &rev
, &opt
);
516 count
= rev
.pending
.nr
;
517 objects
= rev
.pending
.objects
;
518 for (i
= 0; i
< count
&& !ret
; i
++) {
519 struct object
*o
= objects
[i
].item
;
520 const char *name
= objects
[i
].name
;
523 ret
= show_object(o
->sha1
, 0, NULL
);
526 struct tag
*t
= (struct tag
*)o
;
530 printf("%stag %s%s\n",
531 diff_get_color_opt(&rev
.diffopt
, DIFF_COMMIT
),
533 diff_get_color_opt(&rev
.diffopt
, DIFF_RESET
));
534 ret
= show_object(o
->sha1
, 1, &rev
);
538 o
= parse_object(t
->tagged
->sha1
);
540 ret
= error("Could not read object %s",
541 sha1_to_hex(t
->tagged
->sha1
));
549 printf("%stree %s%s\n\n",
550 diff_get_color_opt(&rev
.diffopt
, DIFF_COMMIT
),
552 diff_get_color_opt(&rev
.diffopt
, DIFF_RESET
));
553 read_tree_recursive((struct tree
*)o
, "", 0, 0, NULL
,
554 show_tree_object
, NULL
);
558 rev
.pending
.nr
= rev
.pending
.alloc
= 0;
559 rev
.pending
.objects
= NULL
;
560 add_object_array(o
, name
, &rev
.pending
);
561 ret
= cmd_log_walk(&rev
);
564 ret
= error("Unknown type: %d", o
->type
);
572 * This is equivalent to "git log -g --abbrev-commit --pretty=oneline"
574 int cmd_log_reflog(int argc
, const char **argv
, const char *prefix
)
577 struct setup_revision_opt opt
;
579 git_config(git_log_config
, NULL
);
581 if (diff_use_color_default
== -1)
582 diff_use_color_default
= git_use_color_default
;
584 init_revisions(&rev
, prefix
);
585 init_reflog_walk(&rev
.reflog_info
);
586 rev
.abbrev_commit
= 1;
587 rev
.verbose_header
= 1;
588 memset(&opt
, 0, sizeof(opt
));
590 cmd_log_init(argc
, argv
, prefix
, &rev
, &opt
);
593 * This means that we override whatever commit format the user gave
594 * on the cmd line. Sad, but cmd_log_init() currently doesn't
595 * allow us to set a different default.
597 rev
.commit_format
= CMIT_FMT_ONELINE
;
598 rev
.use_terminator
= 1;
599 rev
.always_show_header
= 1;
601 return cmd_log_walk(&rev
);
604 int cmd_log(int argc
, const char **argv
, const char *prefix
)
607 struct setup_revision_opt opt
;
609 git_config(git_log_config
, NULL
);
611 if (diff_use_color_default
== -1)
612 diff_use_color_default
= git_use_color_default
;
614 init_revisions(&rev
, prefix
);
615 rev
.always_show_header
= 1;
616 memset(&opt
, 0, sizeof(opt
));
618 cmd_log_init(argc
, argv
, prefix
, &rev
, &opt
);
619 if (rev
.line_level_traverse
)
620 return cmd_line_log_walk(&rev
);
622 return cmd_log_walk(&rev
);
627 static const char *fmt_patch_suffix
= ".patch";
628 static int numbered
= 0;
629 static int auto_number
= 1;
631 static char *default_attach
= NULL
;
633 static struct string_list extra_hdr
;
634 static struct string_list extra_to
;
635 static struct string_list extra_cc
;
637 static void add_header(const char *value
)
639 struct string_list_item
*item
;
640 int len
= strlen(value
);
641 while (len
&& value
[len
- 1] == '\n')
644 if (!strncasecmp(value
, "to: ", 4)) {
645 item
= string_list_append(&extra_to
, value
+ 4);
647 } else if (!strncasecmp(value
, "cc: ", 4)) {
648 item
= string_list_append(&extra_cc
, value
+ 4);
651 item
= string_list_append(&extra_hdr
, value
);
654 item
->string
[len
] = '\0';
657 #define THREAD_SHALLOW 1
658 #define THREAD_DEEP 2
660 static int do_signoff
;
661 static const char *signature
= git_version_string
;
663 static int git_format_config(const char *var
, const char *value
, void *cb
)
665 if (!strcmp(var
, "format.headers")) {
667 die("format.headers without value");
671 if (!strcmp(var
, "format.suffix"))
672 return git_config_string(&fmt_patch_suffix
, var
, value
);
673 if (!strcmp(var
, "format.to")) {
675 return config_error_nonbool(var
);
676 string_list_append(&extra_to
, value
);
679 if (!strcmp(var
, "format.cc")) {
681 return config_error_nonbool(var
);
682 string_list_append(&extra_cc
, value
);
685 if (!strcmp(var
, "diff.color") || !strcmp(var
, "color.diff")) {
688 if (!strcmp(var
, "format.numbered")) {
689 if (value
&& !strcasecmp(value
, "auto")) {
693 numbered
= git_config_bool(var
, value
);
694 auto_number
= auto_number
&& numbered
;
697 if (!strcmp(var
, "format.attach")) {
699 default_attach
= xstrdup(value
);
701 default_attach
= xstrdup(git_version_string
);
704 if (!strcmp(var
, "format.thread")) {
705 if (value
&& !strcasecmp(value
, "deep")) {
706 thread
= THREAD_DEEP
;
709 if (value
&& !strcasecmp(value
, "shallow")) {
710 thread
= THREAD_SHALLOW
;
713 thread
= git_config_bool(var
, value
) && THREAD_SHALLOW
;
716 if (!strcmp(var
, "format.signoff")) {
717 do_signoff
= git_config_bool(var
, value
);
720 if (!strcmp(var
, "format.signature"))
721 return git_config_string(&signature
, var
, value
);
723 return git_log_config(var
, value
, cb
);
726 static FILE *realstdout
= NULL
;
727 static const char *output_directory
= NULL
;
728 static int outdir_offset
;
730 static int reopen_stdout(struct commit
*commit
, struct rev_info
*rev
)
732 struct strbuf filename
= STRBUF_INIT
;
733 int suffix_len
= strlen(fmt_patch_suffix
) + 1;
735 if (output_directory
) {
736 strbuf_addstr(&filename
, output_directory
);
738 PATH_MAX
- FORMAT_PATCH_NAME_MAX
- suffix_len
)
739 return error("name of output directory is too long");
740 if (filename
.buf
[filename
.len
- 1] != '/')
741 strbuf_addch(&filename
, '/');
744 get_patch_filename(commit
, rev
->nr
, fmt_patch_suffix
, &filename
);
746 if (!DIFF_OPT_TST(&rev
->diffopt
, QUICK
))
747 fprintf(realstdout
, "%s\n", filename
.buf
+ outdir_offset
);
749 if (freopen(filename
.buf
, "w", stdout
) == NULL
)
750 return error("Cannot open patch file %s", filename
.buf
);
752 strbuf_release(&filename
);
756 static void get_patch_ids(struct rev_info
*rev
, struct patch_ids
*ids
, const char *prefix
)
758 struct rev_info check_rev
;
759 struct commit
*commit
;
760 struct object
*o1
, *o2
;
761 unsigned flags1
, flags2
;
763 if (rev
->pending
.nr
!= 2)
764 die("Need exactly one range.");
766 o1
= rev
->pending
.objects
[0].item
;
768 o2
= rev
->pending
.objects
[1].item
;
771 if ((flags1
& UNINTERESTING
) == (flags2
& UNINTERESTING
))
776 /* given a range a..b get all patch ids for b..a */
777 init_revisions(&check_rev
, prefix
);
778 o1
->flags
^= UNINTERESTING
;
779 o2
->flags
^= UNINTERESTING
;
780 add_pending_object(&check_rev
, o1
, "o1");
781 add_pending_object(&check_rev
, o2
, "o2");
782 if (prepare_revision_walk(&check_rev
))
783 die("revision walk setup failed");
785 while ((commit
= get_revision(&check_rev
)) != NULL
) {
787 if (commit
->parents
&& commit
->parents
->next
)
790 add_commit_patch_id(commit
, ids
);
793 /* reset for next revision walk */
794 clear_commit_marks((struct commit
*)o1
,
795 SEEN
| UNINTERESTING
| SHOWN
| ADDED
);
796 clear_commit_marks((struct commit
*)o2
,
797 SEEN
| UNINTERESTING
| SHOWN
| ADDED
);
802 static void gen_message_id(struct rev_info
*info
, char *base
)
804 const char *committer
= git_committer_info(IDENT_WARN_ON_NO_NAME
);
805 const char *email_start
= strrchr(committer
, '<');
806 const char *email_end
= strrchr(committer
, '>');
807 struct strbuf buf
= STRBUF_INIT
;
808 if (!email_start
|| !email_end
|| email_start
> email_end
- 1)
809 die("Could not extract email from committer identity.");
810 strbuf_addf(&buf
, "%s.%lu.git.%.*s", base
,
811 (unsigned long) time(NULL
),
812 (int)(email_end
- email_start
- 1), email_start
+ 1);
813 info
->message_id
= strbuf_detach(&buf
, NULL
);
816 static void print_signature(void)
818 if (signature
&& *signature
)
819 printf("-- \n%s\n\n", signature
);
822 static void make_cover_letter(struct rev_info
*rev
, int use_stdout
,
823 int numbered
, int numbered_files
,
824 struct commit
*origin
,
825 int nr
, struct commit
**list
, struct commit
*head
)
827 const char *committer
;
828 const char *subject_start
= NULL
;
829 const char *body
= "*** SUBJECT HERE ***\n\n*** BLURB HERE ***\n";
831 const char *extra_headers
= rev
->extra_headers
;
833 struct strbuf sb
= STRBUF_INIT
;
835 const char *encoding
= "UTF-8";
836 struct diff_options opts
;
837 int need_8bit_cte
= 0;
838 struct commit
*commit
= NULL
;
840 if (rev
->commit_format
!= CMIT_FMT_EMAIL
)
841 die("Cover letter needs email format");
843 committer
= git_committer_info(0);
845 if (!numbered_files
) {
847 * We fake a commit for the cover letter so we get the filename
850 commit
= xcalloc(1, sizeof(*commit
));
851 commit
->buffer
= xmalloc(400);
852 snprintf(commit
->buffer
, 400,
853 "tree 0000000000000000000000000000000000000000\n"
858 sha1_to_hex(head
->object
.sha1
), committer
, committer
);
861 if (!use_stdout
&& reopen_stdout(commit
, rev
))
866 free(commit
->buffer
);
870 log_write_email_headers(rev
, head
, &subject_start
, &extra_headers
,
873 for (i
= 0; !need_8bit_cte
&& i
< nr
; i
++)
874 if (has_non_ascii(list
[i
]->buffer
))
878 pp_user_info(NULL
, CMIT_FMT_EMAIL
, &sb
, committer
, DATE_RFC2822
,
880 pp_title_line(CMIT_FMT_EMAIL
, &msg
, &sb
, subject_start
, extra_headers
,
881 encoding
, need_8bit_cte
);
882 pp_remainder(CMIT_FMT_EMAIL
, &msg
, &sb
, 0);
883 printf("%s\n", sb
.buf
);
892 for (i
= 0; i
< nr
; i
++)
893 shortlog_add_commit(&log
, list
[i
]);
895 shortlog_output(&log
);
898 * We can only do diffstat with a unique reference point
903 memcpy(&opts
, &rev
->diffopt
, sizeof(opts
));
904 opts
.output_format
= DIFF_FORMAT_SUMMARY
| DIFF_FORMAT_DIFFSTAT
;
906 diff_setup_done(&opts
);
908 diff_tree_sha1(origin
->tree
->object
.sha1
,
909 head
->tree
->object
.sha1
,
918 static const char *clean_message_id(const char *msg_id
)
921 const char *a
, *z
, *m
;
924 while ((ch
= *m
) && (isspace(ch
) || (ch
== '<')))
929 if (!isspace(ch
) && (ch
!= '>'))
934 die("insane in-reply-to: %s", msg_id
);
937 return xmemdupz(a
, z
- a
);
940 static const char *set_outdir(const char *prefix
, const char *output_directory
)
942 if (output_directory
&& is_absolute_path(output_directory
))
943 return output_directory
;
945 if (!prefix
|| !*prefix
) {
946 if (output_directory
)
947 return output_directory
;
948 /* The user did not explicitly ask for "./" */
953 outdir_offset
= strlen(prefix
);
954 if (!output_directory
)
957 return xstrdup(prefix_filename(prefix
, outdir_offset
,
961 static const char * const builtin_format_patch_usage
[] = {
962 "git format-patch [options] [<since> | <revision range>]",
966 static int keep_subject
= 0;
968 static int keep_callback(const struct option
*opt
, const char *arg
, int unset
)
970 ((struct rev_info
*)opt
->value
)->total
= -1;
975 static int subject_prefix
= 0;
977 static int subject_prefix_callback(const struct option
*opt
, const char *arg
,
981 ((struct rev_info
*)opt
->value
)->subject_prefix
= arg
;
985 static int numbered_cmdline_opt
= 0;
987 static int numbered_callback(const struct option
*opt
, const char *arg
,
990 *(int *)opt
->value
= numbered_cmdline_opt
= unset
? 0 : 1;
996 static int no_numbered_callback(const struct option
*opt
, const char *arg
,
999 return numbered_callback(opt
, arg
, 1);
1002 static int output_directory_callback(const struct option
*opt
, const char *arg
,
1005 const char **dir
= (const char **)opt
->value
;
1007 die("Two output directories?");
1012 static int thread_callback(const struct option
*opt
, const char *arg
, int unset
)
1014 int *thread
= (int *)opt
->value
;
1017 else if (!arg
|| !strcmp(arg
, "shallow"))
1018 *thread
= THREAD_SHALLOW
;
1019 else if (!strcmp(arg
, "deep"))
1020 *thread
= THREAD_DEEP
;
1026 static int attach_callback(const struct option
*opt
, const char *arg
, int unset
)
1028 struct rev_info
*rev
= (struct rev_info
*)opt
->value
;
1030 rev
->mime_boundary
= NULL
;
1032 rev
->mime_boundary
= arg
;
1034 rev
->mime_boundary
= git_version_string
;
1035 rev
->no_inline
= unset
? 0 : 1;
1039 static int inline_callback(const struct option
*opt
, const char *arg
, int unset
)
1041 struct rev_info
*rev
= (struct rev_info
*)opt
->value
;
1043 rev
->mime_boundary
= NULL
;
1045 rev
->mime_boundary
= arg
;
1047 rev
->mime_boundary
= git_version_string
;
1052 static int header_callback(const struct option
*opt
, const char *arg
, int unset
)
1055 string_list_clear(&extra_hdr
, 0);
1056 string_list_clear(&extra_to
, 0);
1057 string_list_clear(&extra_cc
, 0);
1064 static int to_callback(const struct option
*opt
, const char *arg
, int unset
)
1067 string_list_clear(&extra_to
, 0);
1069 string_list_append(&extra_to
, arg
);
1073 static int cc_callback(const struct option
*opt
, const char *arg
, int unset
)
1076 string_list_clear(&extra_cc
, 0);
1078 string_list_append(&extra_cc
, arg
);
1082 int cmd_format_patch(int argc
, const char **argv
, const char *prefix
)
1084 struct commit
*commit
;
1085 struct commit
**list
= NULL
;
1086 struct rev_info rev
;
1087 struct setup_revision_opt s_r_opt
;
1088 int nr
= 0, total
, i
;
1090 int start_number
= -1;
1091 int numbered_files
= 0; /* _just_ numbers */
1092 int ignore_if_in_upstream
= 0;
1093 int cover_letter
= 0;
1094 int boundary_count
= 0;
1095 int no_binary_diff
= 0;
1096 struct commit
*origin
= NULL
, *head
= NULL
;
1097 const char *in_reply_to
= NULL
;
1098 struct patch_ids ids
;
1099 char *add_signoff
= NULL
;
1100 struct strbuf buf
= STRBUF_INIT
;
1101 int use_patch_format
= 0;
1102 const struct option builtin_format_patch_options
[] = {
1103 { OPTION_CALLBACK
, 'n', "numbered", &numbered
, NULL
,
1104 "use [PATCH n/m] even with a single patch",
1105 PARSE_OPT_NOARG
, numbered_callback
},
1106 { OPTION_CALLBACK
, 'N', "no-numbered", &numbered
, NULL
,
1107 "use [PATCH] even with multiple patches",
1108 PARSE_OPT_NOARG
, no_numbered_callback
},
1109 OPT_BOOLEAN('s', "signoff", &do_signoff
, "add Signed-off-by:"),
1110 OPT_BOOLEAN(0, "stdout", &use_stdout
,
1111 "print patches to standard out"),
1112 OPT_BOOLEAN(0, "cover-letter", &cover_letter
,
1113 "generate a cover letter"),
1114 OPT_BOOLEAN(0, "numbered-files", &numbered_files
,
1115 "use simple number sequence for output file names"),
1116 OPT_STRING(0, "suffix", &fmt_patch_suffix
, "sfx",
1117 "use <sfx> instead of '.patch'"),
1118 OPT_INTEGER(0, "start-number", &start_number
,
1119 "start numbering patches at <n> instead of 1"),
1120 { OPTION_CALLBACK
, 0, "subject-prefix", &rev
, "prefix",
1121 "Use [<prefix>] instead of [PATCH]",
1122 PARSE_OPT_NONEG
, subject_prefix_callback
},
1123 { OPTION_CALLBACK
, 'o', "output-directory", &output_directory
,
1124 "dir", "store resulting files in <dir>",
1125 PARSE_OPT_NONEG
, output_directory_callback
},
1126 { OPTION_CALLBACK
, 'k', "keep-subject", &rev
, NULL
,
1127 "don't strip/add [PATCH]",
1128 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
, keep_callback
},
1129 OPT_BOOLEAN(0, "no-binary", &no_binary_diff
,
1130 "don't output binary diffs"),
1131 OPT_BOOLEAN(0, "ignore-if-in-upstream", &ignore_if_in_upstream
,
1132 "don't include a patch matching a commit upstream"),
1133 { OPTION_BOOLEAN
, 'p', "no-stat", &use_patch_format
, NULL
,
1134 "show patch format instead of default (patch + stat)",
1135 PARSE_OPT_NONEG
| PARSE_OPT_NOARG
},
1136 OPT_GROUP("Messaging"),
1137 { OPTION_CALLBACK
, 0, "add-header", NULL
, "header",
1138 "add email header", 0, header_callback
},
1139 { OPTION_CALLBACK
, 0, "to", NULL
, "email", "add To: header",
1141 { OPTION_CALLBACK
, 0, "cc", NULL
, "email", "add Cc: header",
1143 OPT_STRING(0, "in-reply-to", &in_reply_to
, "message-id",
1144 "make first mail a reply to <message-id>"),
1145 { OPTION_CALLBACK
, 0, "attach", &rev
, "boundary",
1146 "attach the patch", PARSE_OPT_OPTARG
,
1148 { OPTION_CALLBACK
, 0, "inline", &rev
, "boundary",
1150 PARSE_OPT_OPTARG
| PARSE_OPT_NONEG
,
1152 { OPTION_CALLBACK
, 0, "thread", &thread
, "style",
1153 "enable message threading, styles: shallow, deep",
1154 PARSE_OPT_OPTARG
, thread_callback
},
1155 OPT_STRING(0, "signature", &signature
, "signature",
1160 extra_hdr
.strdup_strings
= 1;
1161 extra_to
.strdup_strings
= 1;
1162 extra_cc
.strdup_strings
= 1;
1163 git_config(git_format_config
, NULL
);
1164 init_revisions(&rev
, prefix
);
1165 rev
.commit_format
= CMIT_FMT_EMAIL
;
1166 rev
.verbose_header
= 1;
1169 DIFF_OPT_SET(&rev
.diffopt
, RECURSIVE
);
1170 rev
.subject_prefix
= fmt_patch_subject_prefix
;
1171 memset(&s_r_opt
, 0, sizeof(s_r_opt
));
1172 s_r_opt
.def
= "HEAD";
1174 if (default_attach
) {
1175 rev
.mime_boundary
= default_attach
;
1180 * Parse the arguments before setup_revisions(), or something
1181 * like "git format-patch -o a123 HEAD^.." may fail; a123 is
1182 * possibly a valid SHA1.
1184 argc
= parse_options(argc
, argv
, prefix
, builtin_format_patch_options
,
1185 builtin_format_patch_usage
,
1186 PARSE_OPT_KEEP_ARGV0
| PARSE_OPT_KEEP_UNKNOWN
|
1187 PARSE_OPT_KEEP_DASHDASH
);
1190 const char *committer
;
1192 committer
= git_committer_info(IDENT_ERROR_ON_NO_NAME
);
1193 endpos
= strchr(committer
, '>');
1195 die("bogus committer info %s", committer
);
1196 add_signoff
= xmemdupz(committer
, endpos
- committer
+ 1);
1199 for (i
= 0; i
< extra_hdr
.nr
; i
++) {
1200 strbuf_addstr(&buf
, extra_hdr
.items
[i
].string
);
1201 strbuf_addch(&buf
, '\n');
1205 strbuf_addstr(&buf
, "To: ");
1206 for (i
= 0; i
< extra_to
.nr
; i
++) {
1208 strbuf_addstr(&buf
, " ");
1209 strbuf_addstr(&buf
, extra_to
.items
[i
].string
);
1210 if (i
+ 1 < extra_to
.nr
)
1211 strbuf_addch(&buf
, ',');
1212 strbuf_addch(&buf
, '\n');
1216 strbuf_addstr(&buf
, "Cc: ");
1217 for (i
= 0; i
< extra_cc
.nr
; i
++) {
1219 strbuf_addstr(&buf
, " ");
1220 strbuf_addstr(&buf
, extra_cc
.items
[i
].string
);
1221 if (i
+ 1 < extra_cc
.nr
)
1222 strbuf_addch(&buf
, ',');
1223 strbuf_addch(&buf
, '\n');
1226 rev
.extra_headers
= strbuf_detach(&buf
, NULL
);
1228 if (start_number
< 0)
1232 * If numbered is set solely due to format.numbered in config,
1233 * and it would conflict with --keep-subject (-k) from the
1234 * command line, reset "numbered".
1236 if (numbered
&& keep_subject
&& !numbered_cmdline_opt
)
1239 if (numbered
&& keep_subject
)
1240 die ("-n and -k are mutually exclusive.");
1241 if (keep_subject
&& subject_prefix
)
1242 die ("--subject-prefix and -k are mutually exclusive.");
1244 argc
= setup_revisions(argc
, argv
, &rev
, &s_r_opt
);
1246 die ("unrecognized argument: %s", argv
[1]);
1248 if (rev
.diffopt
.output_format
& DIFF_FORMAT_NAME
)
1249 die("--name-only does not make sense");
1250 if (rev
.diffopt
.output_format
& DIFF_FORMAT_NAME_STATUS
)
1251 die("--name-status does not make sense");
1252 if (rev
.diffopt
.output_format
& DIFF_FORMAT_CHECKDIFF
)
1253 die("--check does not make sense");
1255 if (!use_patch_format
&&
1256 (!rev
.diffopt
.output_format
||
1257 rev
.diffopt
.output_format
== DIFF_FORMAT_PATCH
))
1258 rev
.diffopt
.output_format
= DIFF_FORMAT_DIFFSTAT
| DIFF_FORMAT_SUMMARY
;
1260 /* Always generate a patch */
1261 rev
.diffopt
.output_format
|= DIFF_FORMAT_PATCH
;
1263 if (!DIFF_OPT_TST(&rev
.diffopt
, TEXT
) && !no_binary_diff
)
1264 DIFF_OPT_SET(&rev
.diffopt
, BINARY
);
1267 init_display_notes(&rev
.notes_opt
);
1270 output_directory
= set_outdir(prefix
, output_directory
);
1272 if (output_directory
) {
1274 die("standard output, or directory, which one?");
1275 if (mkdir(output_directory
, 0777) < 0 && errno
!= EEXIST
)
1276 die_errno("Could not create directory '%s'",
1280 if (rev
.pending
.nr
== 1) {
1281 if (rev
.max_count
< 0 && !rev
.show_root_diff
) {
1283 * This is traditional behaviour of "git format-patch
1284 * origin" that prepares what the origin side still
1287 rev
.pending
.objects
[0].item
->flags
|= UNINTERESTING
;
1288 add_head_to_pending(&rev
);
1291 * Otherwise, it is "format-patch -22 HEAD", and/or
1292 * "format-patch --root HEAD". The user wants
1293 * get_revision() to do the usual traversal.
1298 * We cannot move this anywhere earlier because we do want to
1299 * know if --root was given explicitly from the command line.
1301 rev
.show_root_diff
= 1;
1304 /* remember the range */
1306 for (i
= 0; i
< rev
.pending
.nr
; i
++) {
1307 struct object
*o
= rev
.pending
.objects
[i
].item
;
1308 if (!(o
->flags
& UNINTERESTING
))
1309 head
= (struct commit
*)o
;
1311 /* We can't generate a cover letter without any patches */
1316 if (ignore_if_in_upstream
) {
1317 /* Don't say anything if head and upstream are the same. */
1318 if (rev
.pending
.nr
== 2) {
1319 struct object_array_entry
*o
= rev
.pending
.objects
;
1320 if (hashcmp(o
[0].item
->sha1
, o
[1].item
->sha1
) == 0)
1323 get_patch_ids(&rev
, &ids
, prefix
);
1327 realstdout
= xfdopen(xdup(1), "w");
1329 if (prepare_revision_walk(&rev
))
1330 die("revision walk setup failed");
1332 while ((commit
= get_revision(&rev
)) != NULL
) {
1333 if (commit
->object
.flags
& BOUNDARY
) {
1335 origin
= (boundary_count
== 1) ? commit
: NULL
;
1339 if (ignore_if_in_upstream
&&
1340 has_commit_patch_id(commit
, &ids
))
1344 list
= xrealloc(list
, nr
* sizeof(list
[0]));
1345 list
[nr
- 1] = commit
;
1348 if (!keep_subject
&& auto_number
&& total
> 1)
1351 rev
.total
= total
+ start_number
- 1;
1352 if (in_reply_to
|| thread
|| cover_letter
)
1353 rev
.ref_message_ids
= xcalloc(1, sizeof(struct string_list
));
1355 const char *msgid
= clean_message_id(in_reply_to
);
1356 string_list_append(rev
.ref_message_ids
, msgid
);
1358 rev
.numbered_files
= numbered_files
;
1359 rev
.patch_suffix
= fmt_patch_suffix
;
1362 gen_message_id(&rev
, "cover");
1363 make_cover_letter(&rev
, use_stdout
, numbered
, numbered_files
,
1364 origin
, nr
, list
, head
);
1368 rev
.add_signoff
= add_signoff
;
1372 rev
.nr
= total
- nr
+ (start_number
- 1);
1373 /* Make the second and subsequent mails replies to the first */
1375 /* Have we already had a message ID? */
1376 if (rev
.message_id
) {
1378 * For deep threading: make every mail
1379 * a reply to the previous one, no
1380 * matter what other options are set.
1382 * For shallow threading:
1384 * Without --cover-letter and
1385 * --in-reply-to, make every mail a
1386 * reply to the one before.
1388 * With --in-reply-to but no
1389 * --cover-letter, make every mail a
1390 * reply to the <reply-to>.
1392 * With --cover-letter, make every
1393 * mail but the cover letter a reply
1394 * to the cover letter. The cover
1395 * letter is a reply to the
1396 * --in-reply-to, if specified.
1398 if (thread
== THREAD_SHALLOW
1399 && rev
.ref_message_ids
->nr
> 0
1400 && (!cover_letter
|| rev
.nr
> 1))
1401 free(rev
.message_id
);
1403 string_list_append(rev
.ref_message_ids
,
1406 gen_message_id(&rev
, sha1_to_hex(commit
->object
.sha1
));
1409 if (!use_stdout
&& reopen_stdout(numbered_files
? NULL
: commit
,
1411 die("Failed to create output files");
1412 shown
= log_tree_commit(&rev
, commit
);
1413 free(commit
->buffer
);
1414 commit
->buffer
= NULL
;
1416 /* We put one extra blank line between formatted
1417 * patches and this flag is used by log-tree code
1418 * to see if it needs to emit a LF before showing
1419 * the log; when using one file per patch, we do
1420 * not want the extra blank line.
1425 if (rev
.mime_boundary
)
1426 printf("\n--%s%s--\n\n\n",
1427 mime_boundary_leader
,
1436 string_list_clear(&extra_to
, 0);
1437 string_list_clear(&extra_cc
, 0);
1438 string_list_clear(&extra_hdr
, 0);
1439 if (ignore_if_in_upstream
)
1440 free_patch_ids(&ids
);
1444 static int add_pending_commit(const char *arg
, struct rev_info
*revs
, int flags
)
1446 unsigned char sha1
[20];
1447 if (get_sha1(arg
, sha1
) == 0) {
1448 struct commit
*commit
= lookup_commit_reference(sha1
);
1450 commit
->object
.flags
|= flags
;
1451 add_pending_object(revs
, &commit
->object
, arg
);
1458 static const char * const cherry_usage
[] = {
1459 "git cherry [-v] [<upstream> [<head> [<limit>]]]",
1463 int cmd_cherry(int argc
, const char **argv
, const char *prefix
)
1465 struct rev_info revs
;
1466 struct patch_ids ids
;
1467 struct commit
*commit
;
1468 struct commit_list
*list
= NULL
;
1469 struct branch
*current_branch
;
1470 const char *upstream
;
1471 const char *head
= "HEAD";
1472 const char *limit
= NULL
;
1473 int verbose
= 0, abbrev
= 0;
1475 struct option options
[] = {
1476 OPT__ABBREV(&abbrev
),
1477 OPT__VERBOSE(&verbose
),
1481 argc
= parse_options(argc
, argv
, prefix
, options
, cherry_usage
, 0);
1494 current_branch
= branch_get(NULL
);
1495 if (!current_branch
|| !current_branch
->merge
1496 || !current_branch
->merge
[0]
1497 || !current_branch
->merge
[0]->dst
) {
1498 fprintf(stderr
, "Could not find a tracked"
1499 " remote branch, please"
1500 " specify <upstream> manually.\n");
1501 usage_with_options(cherry_usage
, options
);
1504 upstream
= current_branch
->merge
[0]->dst
;
1507 init_revisions(&revs
, prefix
);
1509 revs
.combine_merges
= 0;
1510 revs
.ignore_merges
= 1;
1511 DIFF_OPT_SET(&revs
.diffopt
, RECURSIVE
);
1513 if (add_pending_commit(head
, &revs
, 0))
1514 die("Unknown commit %s", head
);
1515 if (add_pending_commit(upstream
, &revs
, UNINTERESTING
))
1516 die("Unknown commit %s", upstream
);
1518 /* Don't say anything if head and upstream are the same. */
1519 if (revs
.pending
.nr
== 2) {
1520 struct object_array_entry
*o
= revs
.pending
.objects
;
1521 if (hashcmp(o
[0].item
->sha1
, o
[1].item
->sha1
) == 0)
1525 get_patch_ids(&revs
, &ids
, prefix
);
1527 if (limit
&& add_pending_commit(limit
, &revs
, UNINTERESTING
))
1528 die("Unknown commit %s", limit
);
1530 /* reverse the list of commits */
1531 if (prepare_revision_walk(&revs
))
1532 die("revision walk setup failed");
1533 while ((commit
= get_revision(&revs
)) != NULL
) {
1535 if (commit
->parents
&& commit
->parents
->next
)
1538 commit_list_insert(commit
, &list
);
1544 commit
= list
->item
;
1545 if (has_commit_patch_id(commit
, &ids
))
1549 struct strbuf buf
= STRBUF_INIT
;
1550 struct pretty_print_context ctx
= {0};
1551 pretty_print_commit(CMIT_FMT_ONELINE
, commit
,
1553 printf("%c %s %s\n", sign
,
1554 find_unique_abbrev(commit
->object
.sha1
, abbrev
),
1556 strbuf_release(&buf
);
1559 printf("%c %s\n", sign
,
1560 find_unique_abbrev(commit
->object
.sha1
, abbrev
));
1566 free_patch_ids(&ids
);