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(int argc
, const char **argv
, const char *prefix
,
53 struct rev_info
*rev
, struct setup_revision_opt
*opt
)
56 int decoration_given
= 0;
57 struct userformat_want w
;
59 rev
->abbrev
= DEFAULT_ABBREV
;
60 rev
->commit_format
= CMIT_FMT_DEFAULT
;
62 get_commit_format(fmt_pretty
, rev
);
63 rev
->verbose_header
= 1;
64 DIFF_OPT_SET(&rev
->diffopt
, RECURSIVE
);
65 rev
->show_root_diff
= default_show_root
;
66 rev
->subject_prefix
= fmt_patch_subject_prefix
;
67 DIFF_OPT_SET(&rev
->diffopt
, ALLOW_TEXTCONV
);
69 if (default_date_mode
)
70 rev
->date_mode
= parse_date_format(default_date_mode
);
73 * Check for -h before setup_revisions(), or "git log -h" will
74 * fail when run without a git directory.
76 if (argc
== 2 && !strcmp(argv
[1], "-h"))
77 usage(builtin_log_usage
);
78 argc
= setup_revisions(argc
, argv
, rev
, opt
);
80 memset(&w
, 0, sizeof(w
));
81 userformat_find_requirements(NULL
, &w
);
83 if (!rev
->show_notes_given
&& (!rev
->pretty_given
|| w
.notes
))
86 init_display_notes(&rev
->notes_opt
);
88 if (rev
->diffopt
.pickaxe
|| rev
->diffopt
.filter
)
89 rev
->always_show_header
= 0;
90 if (DIFF_OPT_TST(&rev
->diffopt
, FOLLOW_RENAMES
)) {
91 rev
->always_show_header
= 0;
92 if (rev
->diffopt
.nr_paths
!= 1)
93 usage("git logs can only follow renames on one pathname at a time");
95 for (i
= 1; i
< argc
; i
++) {
96 const char *arg
= argv
[i
];
97 if (!strcmp(arg
, "--decorate")) {
98 decoration_style
= DECORATE_SHORT_REFS
;
100 } else if (!prefixcmp(arg
, "--decorate=")) {
101 const char *v
= skip_prefix(arg
, "--decorate=");
102 decoration_style
= parse_decoration_style(arg
, v
);
103 if (decoration_style
< 0)
104 die("invalid --decorate option: %s", arg
);
105 decoration_given
= 1;
106 } else if (!strcmp(arg
, "--no-decorate")) {
107 decoration_style
= 0;
108 } else if (!strcmp(arg
, "--source")) {
109 rev
->show_source
= 1;
110 } else if (!strcmp(arg
, "-h")) {
111 usage(builtin_log_usage
);
113 die("unrecognized argument: %s", arg
);
117 * defeat log.decorate configuration interacting with --pretty=raw
118 * from the command line.
120 if (!decoration_given
&& rev
->pretty_given
121 && rev
->commit_format
== CMIT_FMT_RAW
)
122 decoration_style
= 0;
124 if (decoration_style
) {
125 rev
->show_decorations
= 1;
126 load_ref_decorations(decoration_style
);
132 * This gives a rough estimate for how many commits we
133 * will print out in the list.
135 static int estimate_commit_count(struct rev_info
*rev
, struct commit_list
*list
)
140 struct commit
*commit
= list
->item
;
141 unsigned int flags
= commit
->object
.flags
;
143 if (!(flags
& (TREESAME
| UNINTERESTING
)))
149 static void show_early_header(struct rev_info
*rev
, const char *stage
, int nr
)
151 if (rev
->shown_one
) {
153 if (rev
->commit_format
!= CMIT_FMT_ONELINE
)
154 putchar(rev
->diffopt
.line_termination
);
156 printf("Final output: %d %s\n", nr
, stage
);
159 static struct itimerval early_output_timer
;
161 static void log_show_early(struct rev_info
*revs
, struct commit_list
*list
)
163 int i
= revs
->early_output
;
166 sort_in_topological_order(&list
, revs
->lifo
);
168 struct commit
*commit
= list
->item
;
169 switch (simplify_commit(revs
, commit
)) {
172 int n
= estimate_commit_count(revs
, list
);
173 show_early_header(revs
, "incomplete", n
);
176 log_tree_commit(revs
, commit
);
187 /* Did we already get enough commits for the early output? */
192 * ..if no, then repeat it twice a second until we
195 * NOTE! We don't use "it_interval", because if the
196 * reader isn't listening, we want our output to be
197 * throttled by the writing, and not have the timer
198 * trigger every second even if we're blocked on a
201 early_output_timer
.it_value
.tv_sec
= 0;
202 early_output_timer
.it_value
.tv_usec
= 500000;
203 setitimer(ITIMER_REAL
, &early_output_timer
, NULL
);
206 static void early_output(int signal
)
208 show_early_output
= log_show_early
;
211 static void setup_early_output(struct rev_info
*rev
)
216 * Set up the signal handler, minimally intrusively:
217 * we only set a single volatile integer word (not
218 * using sigatomic_t - trying to avoid unnecessary
219 * system dependencies and headers), and using
222 memset(&sa
, 0, sizeof(sa
));
223 sa
.sa_handler
= early_output
;
224 sigemptyset(&sa
.sa_mask
);
225 sa
.sa_flags
= SA_RESTART
;
226 sigaction(SIGALRM
, &sa
, NULL
);
229 * If we can get the whole output in less than a
230 * tenth of a second, don't even bother doing the
231 * early-output thing..
233 * This is a one-time-only trigger.
235 early_output_timer
.it_value
.tv_sec
= 0;
236 early_output_timer
.it_value
.tv_usec
= 100000;
237 setitimer(ITIMER_REAL
, &early_output_timer
, NULL
);
240 static void finish_early_output(struct rev_info
*rev
)
242 int n
= estimate_commit_count(rev
, rev
->commits
);
243 signal(SIGALRM
, SIG_IGN
);
244 show_early_header(rev
, "done", n
);
247 static int cmd_log_walk(struct rev_info
*rev
)
249 struct commit
*commit
;
251 if (rev
->early_output
)
252 setup_early_output(rev
);
254 if (prepare_revision_walk(rev
))
255 die("revision walk setup failed");
257 if (rev
->early_output
)
258 finish_early_output(rev
);
261 * For --check and --exit-code, the exit code is based on CHECK_FAILED
262 * and HAS_CHANGES being accumulated in rev->diffopt, so be careful to
263 * retain that state information if replacing rev->diffopt in this loop
265 while ((commit
= get_revision(rev
)) != NULL
) {
266 log_tree_commit(rev
, commit
);
267 if (!rev
->reflog_info
) {
268 /* we allow cycles in reflog ancestry */
269 free(commit
->buffer
);
270 commit
->buffer
= NULL
;
272 free_commit_list(commit
->parents
);
273 commit
->parents
= NULL
;
275 if (rev
->diffopt
.output_format
& DIFF_FORMAT_CHECKDIFF
&&
276 DIFF_OPT_TST(&rev
->diffopt
, CHECK_FAILED
)) {
279 return diff_result_code(&rev
->diffopt
, 0);
282 static int git_log_config(const char *var
, const char *value
, void *cb
)
284 if (!strcmp(var
, "format.pretty"))
285 return git_config_string(&fmt_pretty
, var
, value
);
286 if (!strcmp(var
, "format.subjectprefix"))
287 return git_config_string(&fmt_patch_subject_prefix
, var
, value
);
288 if (!strcmp(var
, "log.date"))
289 return git_config_string(&default_date_mode
, var
, value
);
290 if (!strcmp(var
, "log.decorate")) {
291 decoration_style
= parse_decoration_style(var
, value
);
292 if (decoration_style
< 0)
293 decoration_style
= 0; /* maybe warn? */
296 if (!strcmp(var
, "log.showroot")) {
297 default_show_root
= git_config_bool(var
, value
);
300 if (!prefixcmp(var
, "color.decorate."))
301 return parse_decorate_color_config(var
, 15, value
);
303 return git_diff_ui_config(var
, value
, cb
);
306 int cmd_whatchanged(int argc
, const char **argv
, const char *prefix
)
309 struct setup_revision_opt opt
;
311 git_config(git_log_config
, NULL
);
313 if (diff_use_color_default
== -1)
314 diff_use_color_default
= git_use_color_default
;
316 init_revisions(&rev
, prefix
);
318 rev
.simplify_history
= 0;
319 memset(&opt
, 0, sizeof(opt
));
321 cmd_log_init(argc
, argv
, prefix
, &rev
, &opt
);
322 if (!rev
.diffopt
.output_format
)
323 rev
.diffopt
.output_format
= DIFF_FORMAT_RAW
;
324 return cmd_log_walk(&rev
);
327 static void show_tagger(char *buf
, int len
, struct rev_info
*rev
)
329 struct strbuf out
= STRBUF_INIT
;
331 pp_user_info("Tagger", rev
->commit_format
, &out
, buf
, rev
->date_mode
,
332 get_log_output_encoding());
333 printf("%s", out
.buf
);
334 strbuf_release(&out
);
337 static int show_object(const unsigned char *sha1
, int show_tag_object
,
338 struct rev_info
*rev
)
341 enum object_type type
;
342 char *buf
= read_sha1_file(sha1
, &type
, &size
);
346 return error("Could not read object %s", sha1_to_hex(sha1
));
349 while (offset
< size
&& buf
[offset
] != '\n') {
350 int new_offset
= offset
+ 1;
351 while (new_offset
< size
&& buf
[new_offset
++] != '\n')
353 if (!prefixcmp(buf
+ offset
, "tagger "))
354 show_tagger(buf
+ offset
+ 7,
355 new_offset
- offset
- 7, rev
);
360 fwrite(buf
+ offset
, size
- offset
, 1, stdout
);
365 static int show_tree_object(const unsigned char *sha1
,
366 const char *base
, int baselen
,
367 const char *pathname
, unsigned mode
, int stage
, void *context
)
369 printf("%s%s\n", pathname
, S_ISDIR(mode
) ? "/" : "");
373 static void show_rev_tweak_rev(struct rev_info
*rev
, struct setup_revision_opt
*opt
)
375 if (rev
->ignore_merges
) {
376 /* There was no "-m" on the command line */
377 rev
->ignore_merges
= 0;
378 if (!rev
->first_parent_only
&& !rev
->combine_merges
) {
379 /* No "--first-parent", "-c", nor "--cc" */
380 rev
->combine_merges
= 1;
381 rev
->dense_combined_merges
= 1;
384 if (!rev
->diffopt
.output_format
)
385 rev
->diffopt
.output_format
= DIFF_FORMAT_PATCH
;
388 int cmd_show(int argc
, const char **argv
, const char *prefix
)
391 struct object_array_entry
*objects
;
392 struct setup_revision_opt opt
;
393 int i
, count
, ret
= 0;
395 git_config(git_log_config
, NULL
);
397 if (diff_use_color_default
== -1)
398 diff_use_color_default
= git_use_color_default
;
400 init_revisions(&rev
, prefix
);
402 rev
.always_show_header
= 1;
404 memset(&opt
, 0, sizeof(opt
));
406 opt
.tweak
= show_rev_tweak_rev
;
407 cmd_log_init(argc
, argv
, prefix
, &rev
, &opt
);
409 count
= rev
.pending
.nr
;
410 objects
= rev
.pending
.objects
;
411 for (i
= 0; i
< count
&& !ret
; i
++) {
412 struct object
*o
= objects
[i
].item
;
413 const char *name
= objects
[i
].name
;
416 ret
= show_object(o
->sha1
, 0, NULL
);
419 struct tag
*t
= (struct tag
*)o
;
423 printf("%stag %s%s\n",
424 diff_get_color_opt(&rev
.diffopt
, DIFF_COMMIT
),
426 diff_get_color_opt(&rev
.diffopt
, DIFF_RESET
));
427 ret
= show_object(o
->sha1
, 1, &rev
);
431 o
= parse_object(t
->tagged
->sha1
);
433 ret
= error("Could not read object %s",
434 sha1_to_hex(t
->tagged
->sha1
));
442 printf("%stree %s%s\n\n",
443 diff_get_color_opt(&rev
.diffopt
, DIFF_COMMIT
),
445 diff_get_color_opt(&rev
.diffopt
, DIFF_RESET
));
446 read_tree_recursive((struct tree
*)o
, "", 0, 0, NULL
,
447 show_tree_object
, NULL
);
451 rev
.pending
.nr
= rev
.pending
.alloc
= 0;
452 rev
.pending
.objects
= NULL
;
453 add_object_array(o
, name
, &rev
.pending
);
454 ret
= cmd_log_walk(&rev
);
457 ret
= error("Unknown type: %d", o
->type
);
465 * This is equivalent to "git log -g --abbrev-commit --pretty=oneline"
467 int cmd_log_reflog(int argc
, const char **argv
, const char *prefix
)
470 struct setup_revision_opt opt
;
472 git_config(git_log_config
, NULL
);
474 if (diff_use_color_default
== -1)
475 diff_use_color_default
= git_use_color_default
;
477 init_revisions(&rev
, prefix
);
478 init_reflog_walk(&rev
.reflog_info
);
479 rev
.abbrev_commit
= 1;
480 rev
.verbose_header
= 1;
481 memset(&opt
, 0, sizeof(opt
));
483 cmd_log_init(argc
, argv
, prefix
, &rev
, &opt
);
486 * This means that we override whatever commit format the user gave
487 * on the cmd line. Sad, but cmd_log_init() currently doesn't
488 * allow us to set a different default.
490 rev
.commit_format
= CMIT_FMT_ONELINE
;
491 rev
.use_terminator
= 1;
492 rev
.always_show_header
= 1;
494 return cmd_log_walk(&rev
);
497 int cmd_log(int argc
, const char **argv
, const char *prefix
)
500 struct setup_revision_opt opt
;
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
);
508 rev
.always_show_header
= 1;
509 memset(&opt
, 0, sizeof(opt
));
511 cmd_log_init(argc
, argv
, prefix
, &rev
, &opt
);
512 return cmd_log_walk(&rev
);
517 static const char *fmt_patch_suffix
= ".patch";
518 static int numbered
= 0;
519 static int auto_number
= 1;
521 static char *default_attach
= NULL
;
523 static struct string_list extra_hdr
;
524 static struct string_list extra_to
;
525 static struct string_list extra_cc
;
527 static void add_header(const char *value
)
529 struct string_list_item
*item
;
530 int len
= strlen(value
);
531 while (len
&& value
[len
- 1] == '\n')
534 if (!strncasecmp(value
, "to: ", 4)) {
535 item
= string_list_append(&extra_to
, value
+ 4);
537 } else if (!strncasecmp(value
, "cc: ", 4)) {
538 item
= string_list_append(&extra_cc
, value
+ 4);
541 item
= string_list_append(&extra_hdr
, value
);
544 item
->string
[len
] = '\0';
547 #define THREAD_SHALLOW 1
548 #define THREAD_DEEP 2
550 static int do_signoff
;
551 static const char *signature
= git_version_string
;
553 static int git_format_config(const char *var
, const char *value
, void *cb
)
555 if (!strcmp(var
, "format.headers")) {
557 die("format.headers without value");
561 if (!strcmp(var
, "format.suffix"))
562 return git_config_string(&fmt_patch_suffix
, var
, value
);
563 if (!strcmp(var
, "format.to")) {
565 return config_error_nonbool(var
);
566 string_list_append(&extra_to
, value
);
569 if (!strcmp(var
, "format.cc")) {
571 return config_error_nonbool(var
);
572 string_list_append(&extra_cc
, value
);
575 if (!strcmp(var
, "diff.color") || !strcmp(var
, "color.diff")) {
578 if (!strcmp(var
, "format.numbered")) {
579 if (value
&& !strcasecmp(value
, "auto")) {
583 numbered
= git_config_bool(var
, value
);
584 auto_number
= auto_number
&& numbered
;
587 if (!strcmp(var
, "format.attach")) {
589 default_attach
= xstrdup(value
);
591 default_attach
= xstrdup(git_version_string
);
594 if (!strcmp(var
, "format.thread")) {
595 if (value
&& !strcasecmp(value
, "deep")) {
596 thread
= THREAD_DEEP
;
599 if (value
&& !strcasecmp(value
, "shallow")) {
600 thread
= THREAD_SHALLOW
;
603 thread
= git_config_bool(var
, value
) && THREAD_SHALLOW
;
606 if (!strcmp(var
, "format.signoff")) {
607 do_signoff
= git_config_bool(var
, value
);
610 if (!strcmp(var
, "format.signature"))
611 return git_config_string(&signature
, var
, value
);
613 return git_log_config(var
, value
, cb
);
616 static FILE *realstdout
= NULL
;
617 static const char *output_directory
= NULL
;
618 static int outdir_offset
;
620 static int reopen_stdout(struct commit
*commit
, struct rev_info
*rev
)
622 struct strbuf filename
= STRBUF_INIT
;
623 int suffix_len
= strlen(fmt_patch_suffix
) + 1;
625 if (output_directory
) {
626 strbuf_addstr(&filename
, output_directory
);
628 PATH_MAX
- FORMAT_PATCH_NAME_MAX
- suffix_len
)
629 return error("name of output directory is too long");
630 if (filename
.buf
[filename
.len
- 1] != '/')
631 strbuf_addch(&filename
, '/');
634 get_patch_filename(commit
, rev
->nr
, fmt_patch_suffix
, &filename
);
636 if (!DIFF_OPT_TST(&rev
->diffopt
, QUICK
))
637 fprintf(realstdout
, "%s\n", filename
.buf
+ outdir_offset
);
639 if (freopen(filename
.buf
, "w", stdout
) == NULL
)
640 return error("Cannot open patch file %s", filename
.buf
);
642 strbuf_release(&filename
);
646 static void get_patch_ids(struct rev_info
*rev
, struct patch_ids
*ids
, const char *prefix
)
648 struct rev_info check_rev
;
649 struct commit
*commit
;
650 struct object
*o1
, *o2
;
651 unsigned flags1
, flags2
;
653 if (rev
->pending
.nr
!= 2)
654 die("Need exactly one range.");
656 o1
= rev
->pending
.objects
[0].item
;
658 o2
= rev
->pending
.objects
[1].item
;
661 if ((flags1
& UNINTERESTING
) == (flags2
& UNINTERESTING
))
666 /* given a range a..b get all patch ids for b..a */
667 init_revisions(&check_rev
, prefix
);
668 o1
->flags
^= UNINTERESTING
;
669 o2
->flags
^= UNINTERESTING
;
670 add_pending_object(&check_rev
, o1
, "o1");
671 add_pending_object(&check_rev
, o2
, "o2");
672 if (prepare_revision_walk(&check_rev
))
673 die("revision walk setup failed");
675 while ((commit
= get_revision(&check_rev
)) != NULL
) {
677 if (commit
->parents
&& commit
->parents
->next
)
680 add_commit_patch_id(commit
, ids
);
683 /* reset for next revision walk */
684 clear_commit_marks((struct commit
*)o1
,
685 SEEN
| UNINTERESTING
| SHOWN
| ADDED
);
686 clear_commit_marks((struct commit
*)o2
,
687 SEEN
| UNINTERESTING
| SHOWN
| ADDED
);
692 static void gen_message_id(struct rev_info
*info
, char *base
)
694 const char *committer
= git_committer_info(IDENT_WARN_ON_NO_NAME
);
695 const char *email_start
= strrchr(committer
, '<');
696 const char *email_end
= strrchr(committer
, '>');
697 struct strbuf buf
= STRBUF_INIT
;
698 if (!email_start
|| !email_end
|| email_start
> email_end
- 1)
699 die("Could not extract email from committer identity.");
700 strbuf_addf(&buf
, "%s.%lu.git.%.*s", base
,
701 (unsigned long) time(NULL
),
702 (int)(email_end
- email_start
- 1), email_start
+ 1);
703 info
->message_id
= strbuf_detach(&buf
, NULL
);
706 static void print_signature(void)
708 if (signature
&& *signature
)
709 printf("-- \n%s\n\n", signature
);
712 static void make_cover_letter(struct rev_info
*rev
, int use_stdout
,
713 int numbered
, int numbered_files
,
714 struct commit
*origin
,
715 int nr
, struct commit
**list
, struct commit
*head
)
717 const char *committer
;
718 const char *subject_start
= NULL
;
719 const char *body
= "*** SUBJECT HERE ***\n\n*** BLURB HERE ***\n";
721 const char *extra_headers
= rev
->extra_headers
;
723 struct strbuf sb
= STRBUF_INIT
;
725 const char *encoding
= "UTF-8";
726 struct diff_options opts
;
727 int need_8bit_cte
= 0;
728 struct commit
*commit
= NULL
;
730 if (rev
->commit_format
!= CMIT_FMT_EMAIL
)
731 die("Cover letter needs email format");
733 committer
= git_committer_info(0);
735 if (!numbered_files
) {
737 * We fake a commit for the cover letter so we get the filename
740 commit
= xcalloc(1, sizeof(*commit
));
741 commit
->buffer
= xmalloc(400);
742 snprintf(commit
->buffer
, 400,
743 "tree 0000000000000000000000000000000000000000\n"
748 sha1_to_hex(head
->object
.sha1
), committer
, committer
);
751 if (!use_stdout
&& reopen_stdout(commit
, rev
))
756 free(commit
->buffer
);
760 log_write_email_headers(rev
, head
, &subject_start
, &extra_headers
,
763 for (i
= 0; !need_8bit_cte
&& i
< nr
; i
++)
764 if (has_non_ascii(list
[i
]->buffer
))
768 pp_user_info(NULL
, CMIT_FMT_EMAIL
, &sb
, committer
, DATE_RFC2822
,
770 pp_title_line(CMIT_FMT_EMAIL
, &msg
, &sb
, subject_start
, extra_headers
,
771 encoding
, need_8bit_cte
);
772 pp_remainder(CMIT_FMT_EMAIL
, &msg
, &sb
, 0);
773 printf("%s\n", sb
.buf
);
782 for (i
= 0; i
< nr
; i
++)
783 shortlog_add_commit(&log
, list
[i
]);
785 shortlog_output(&log
);
788 * We can only do diffstat with a unique reference point
793 memcpy(&opts
, &rev
->diffopt
, sizeof(opts
));
794 opts
.output_format
= DIFF_FORMAT_SUMMARY
| DIFF_FORMAT_DIFFSTAT
;
796 diff_setup_done(&opts
);
798 diff_tree_sha1(origin
->tree
->object
.sha1
,
799 head
->tree
->object
.sha1
,
808 static const char *clean_message_id(const char *msg_id
)
811 const char *a
, *z
, *m
;
814 while ((ch
= *m
) && (isspace(ch
) || (ch
== '<')))
819 if (!isspace(ch
) && (ch
!= '>'))
824 die("insane in-reply-to: %s", msg_id
);
827 return xmemdupz(a
, z
- a
);
830 static const char *set_outdir(const char *prefix
, const char *output_directory
)
832 if (output_directory
&& is_absolute_path(output_directory
))
833 return output_directory
;
835 if (!prefix
|| !*prefix
) {
836 if (output_directory
)
837 return output_directory
;
838 /* The user did not explicitly ask for "./" */
843 outdir_offset
= strlen(prefix
);
844 if (!output_directory
)
847 return xstrdup(prefix_filename(prefix
, outdir_offset
,
851 static const char * const builtin_format_patch_usage
[] = {
852 "git format-patch [options] [<since> | <revision range>]",
856 static int keep_subject
= 0;
858 static int keep_callback(const struct option
*opt
, const char *arg
, int unset
)
860 ((struct rev_info
*)opt
->value
)->total
= -1;
865 static int subject_prefix
= 0;
867 static int subject_prefix_callback(const struct option
*opt
, const char *arg
,
871 ((struct rev_info
*)opt
->value
)->subject_prefix
= arg
;
875 static int numbered_cmdline_opt
= 0;
877 static int numbered_callback(const struct option
*opt
, const char *arg
,
880 *(int *)opt
->value
= numbered_cmdline_opt
= unset
? 0 : 1;
886 static int no_numbered_callback(const struct option
*opt
, const char *arg
,
889 return numbered_callback(opt
, arg
, 1);
892 static int output_directory_callback(const struct option
*opt
, const char *arg
,
895 const char **dir
= (const char **)opt
->value
;
897 die("Two output directories?");
902 static int thread_callback(const struct option
*opt
, const char *arg
, int unset
)
904 int *thread
= (int *)opt
->value
;
907 else if (!arg
|| !strcmp(arg
, "shallow"))
908 *thread
= THREAD_SHALLOW
;
909 else if (!strcmp(arg
, "deep"))
910 *thread
= THREAD_DEEP
;
916 static int attach_callback(const struct option
*opt
, const char *arg
, int unset
)
918 struct rev_info
*rev
= (struct rev_info
*)opt
->value
;
920 rev
->mime_boundary
= NULL
;
922 rev
->mime_boundary
= arg
;
924 rev
->mime_boundary
= git_version_string
;
925 rev
->no_inline
= unset
? 0 : 1;
929 static int inline_callback(const struct option
*opt
, const char *arg
, int unset
)
931 struct rev_info
*rev
= (struct rev_info
*)opt
->value
;
933 rev
->mime_boundary
= NULL
;
935 rev
->mime_boundary
= arg
;
937 rev
->mime_boundary
= git_version_string
;
942 static int header_callback(const struct option
*opt
, const char *arg
, int unset
)
945 string_list_clear(&extra_hdr
, 0);
946 string_list_clear(&extra_to
, 0);
947 string_list_clear(&extra_cc
, 0);
954 static int to_callback(const struct option
*opt
, const char *arg
, int unset
)
957 string_list_clear(&extra_to
, 0);
959 string_list_append(&extra_to
, arg
);
963 static int cc_callback(const struct option
*opt
, const char *arg
, int unset
)
966 string_list_clear(&extra_cc
, 0);
968 string_list_append(&extra_cc
, arg
);
972 int cmd_format_patch(int argc
, const char **argv
, const char *prefix
)
974 struct commit
*commit
;
975 struct commit
**list
= NULL
;
977 struct setup_revision_opt s_r_opt
;
978 int nr
= 0, total
, i
;
980 int start_number
= -1;
981 int numbered_files
= 0; /* _just_ numbers */
982 int ignore_if_in_upstream
= 0;
983 int cover_letter
= 0;
984 int boundary_count
= 0;
985 int no_binary_diff
= 0;
986 struct commit
*origin
= NULL
, *head
= NULL
;
987 const char *in_reply_to
= NULL
;
988 struct patch_ids ids
;
989 char *add_signoff
= NULL
;
990 struct strbuf buf
= STRBUF_INIT
;
991 int use_patch_format
= 0;
992 const struct option builtin_format_patch_options
[] = {
993 { OPTION_CALLBACK
, 'n', "numbered", &numbered
, NULL
,
994 "use [PATCH n/m] even with a single patch",
995 PARSE_OPT_NOARG
, numbered_callback
},
996 { OPTION_CALLBACK
, 'N', "no-numbered", &numbered
, NULL
,
997 "use [PATCH] even with multiple patches",
998 PARSE_OPT_NOARG
, no_numbered_callback
},
999 OPT_BOOLEAN('s', "signoff", &do_signoff
, "add Signed-off-by:"),
1000 OPT_BOOLEAN(0, "stdout", &use_stdout
,
1001 "print patches to standard out"),
1002 OPT_BOOLEAN(0, "cover-letter", &cover_letter
,
1003 "generate a cover letter"),
1004 OPT_BOOLEAN(0, "numbered-files", &numbered_files
,
1005 "use simple number sequence for output file names"),
1006 OPT_STRING(0, "suffix", &fmt_patch_suffix
, "sfx",
1007 "use <sfx> instead of '.patch'"),
1008 OPT_INTEGER(0, "start-number", &start_number
,
1009 "start numbering patches at <n> instead of 1"),
1010 { OPTION_CALLBACK
, 0, "subject-prefix", &rev
, "prefix",
1011 "Use [<prefix>] instead of [PATCH]",
1012 PARSE_OPT_NONEG
, subject_prefix_callback
},
1013 { OPTION_CALLBACK
, 'o', "output-directory", &output_directory
,
1014 "dir", "store resulting files in <dir>",
1015 PARSE_OPT_NONEG
, output_directory_callback
},
1016 { OPTION_CALLBACK
, 'k', "keep-subject", &rev
, NULL
,
1017 "don't strip/add [PATCH]",
1018 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
, keep_callback
},
1019 OPT_BOOLEAN(0, "no-binary", &no_binary_diff
,
1020 "don't output binary diffs"),
1021 OPT_BOOLEAN(0, "ignore-if-in-upstream", &ignore_if_in_upstream
,
1022 "don't include a patch matching a commit upstream"),
1023 { OPTION_BOOLEAN
, 'p', "no-stat", &use_patch_format
, NULL
,
1024 "show patch format instead of default (patch + stat)",
1025 PARSE_OPT_NONEG
| PARSE_OPT_NOARG
},
1026 OPT_GROUP("Messaging"),
1027 { OPTION_CALLBACK
, 0, "add-header", NULL
, "header",
1028 "add email header", 0, header_callback
},
1029 { OPTION_CALLBACK
, 0, "to", NULL
, "email", "add To: header",
1031 { OPTION_CALLBACK
, 0, "cc", NULL
, "email", "add Cc: header",
1033 OPT_STRING(0, "in-reply-to", &in_reply_to
, "message-id",
1034 "make first mail a reply to <message-id>"),
1035 { OPTION_CALLBACK
, 0, "attach", &rev
, "boundary",
1036 "attach the patch", PARSE_OPT_OPTARG
,
1038 { OPTION_CALLBACK
, 0, "inline", &rev
, "boundary",
1040 PARSE_OPT_OPTARG
| PARSE_OPT_NONEG
,
1042 { OPTION_CALLBACK
, 0, "thread", &thread
, "style",
1043 "enable message threading, styles: shallow, deep",
1044 PARSE_OPT_OPTARG
, thread_callback
},
1045 OPT_STRING(0, "signature", &signature
, "signature",
1050 extra_hdr
.strdup_strings
= 1;
1051 extra_to
.strdup_strings
= 1;
1052 extra_cc
.strdup_strings
= 1;
1053 git_config(git_format_config
, NULL
);
1054 init_revisions(&rev
, prefix
);
1055 rev
.commit_format
= CMIT_FMT_EMAIL
;
1056 rev
.verbose_header
= 1;
1059 DIFF_OPT_SET(&rev
.diffopt
, RECURSIVE
);
1060 rev
.subject_prefix
= fmt_patch_subject_prefix
;
1061 memset(&s_r_opt
, 0, sizeof(s_r_opt
));
1062 s_r_opt
.def
= "HEAD";
1064 if (default_attach
) {
1065 rev
.mime_boundary
= default_attach
;
1070 * Parse the arguments before setup_revisions(), or something
1071 * like "git format-patch -o a123 HEAD^.." may fail; a123 is
1072 * possibly a valid SHA1.
1074 argc
= parse_options(argc
, argv
, prefix
, builtin_format_patch_options
,
1075 builtin_format_patch_usage
,
1076 PARSE_OPT_KEEP_ARGV0
| PARSE_OPT_KEEP_UNKNOWN
|
1077 PARSE_OPT_KEEP_DASHDASH
);
1080 const char *committer
;
1082 committer
= git_committer_info(IDENT_ERROR_ON_NO_NAME
);
1083 endpos
= strchr(committer
, '>');
1085 die("bogus committer info %s", committer
);
1086 add_signoff
= xmemdupz(committer
, endpos
- committer
+ 1);
1089 for (i
= 0; i
< extra_hdr
.nr
; i
++) {
1090 strbuf_addstr(&buf
, extra_hdr
.items
[i
].string
);
1091 strbuf_addch(&buf
, '\n');
1095 strbuf_addstr(&buf
, "To: ");
1096 for (i
= 0; i
< extra_to
.nr
; i
++) {
1098 strbuf_addstr(&buf
, " ");
1099 strbuf_addstr(&buf
, extra_to
.items
[i
].string
);
1100 if (i
+ 1 < extra_to
.nr
)
1101 strbuf_addch(&buf
, ',');
1102 strbuf_addch(&buf
, '\n');
1106 strbuf_addstr(&buf
, "Cc: ");
1107 for (i
= 0; i
< extra_cc
.nr
; i
++) {
1109 strbuf_addstr(&buf
, " ");
1110 strbuf_addstr(&buf
, extra_cc
.items
[i
].string
);
1111 if (i
+ 1 < extra_cc
.nr
)
1112 strbuf_addch(&buf
, ',');
1113 strbuf_addch(&buf
, '\n');
1116 rev
.extra_headers
= strbuf_detach(&buf
, NULL
);
1118 if (start_number
< 0)
1122 * If numbered is set solely due to format.numbered in config,
1123 * and it would conflict with --keep-subject (-k) from the
1124 * command line, reset "numbered".
1126 if (numbered
&& keep_subject
&& !numbered_cmdline_opt
)
1129 if (numbered
&& keep_subject
)
1130 die ("-n and -k are mutually exclusive.");
1131 if (keep_subject
&& subject_prefix
)
1132 die ("--subject-prefix and -k are mutually exclusive.");
1134 argc
= setup_revisions(argc
, argv
, &rev
, &s_r_opt
);
1136 die ("unrecognized argument: %s", argv
[1]);
1138 if (rev
.diffopt
.output_format
& DIFF_FORMAT_NAME
)
1139 die("--name-only does not make sense");
1140 if (rev
.diffopt
.output_format
& DIFF_FORMAT_NAME_STATUS
)
1141 die("--name-status does not make sense");
1142 if (rev
.diffopt
.output_format
& DIFF_FORMAT_CHECKDIFF
)
1143 die("--check does not make sense");
1145 if (!use_patch_format
&&
1146 (!rev
.diffopt
.output_format
||
1147 rev
.diffopt
.output_format
== DIFF_FORMAT_PATCH
))
1148 rev
.diffopt
.output_format
= DIFF_FORMAT_DIFFSTAT
| DIFF_FORMAT_SUMMARY
;
1150 /* Always generate a patch */
1151 rev
.diffopt
.output_format
|= DIFF_FORMAT_PATCH
;
1153 if (!DIFF_OPT_TST(&rev
.diffopt
, TEXT
) && !no_binary_diff
)
1154 DIFF_OPT_SET(&rev
.diffopt
, BINARY
);
1157 init_display_notes(&rev
.notes_opt
);
1160 output_directory
= set_outdir(prefix
, output_directory
);
1164 if (output_directory
) {
1166 die("standard output, or directory, which one?");
1167 if (mkdir(output_directory
, 0777) < 0 && errno
!= EEXIST
)
1168 die_errno("Could not create directory '%s'",
1172 if (rev
.pending
.nr
== 1) {
1173 if (rev
.max_count
< 0 && !rev
.show_root_diff
) {
1175 * This is traditional behaviour of "git format-patch
1176 * origin" that prepares what the origin side still
1179 rev
.pending
.objects
[0].item
->flags
|= UNINTERESTING
;
1180 add_head_to_pending(&rev
);
1183 * Otherwise, it is "format-patch -22 HEAD", and/or
1184 * "format-patch --root HEAD". The user wants
1185 * get_revision() to do the usual traversal.
1190 * We cannot move this anywhere earlier because we do want to
1191 * know if --root was given explicitly from the command line.
1193 rev
.show_root_diff
= 1;
1196 /* remember the range */
1198 for (i
= 0; i
< rev
.pending
.nr
; i
++) {
1199 struct object
*o
= rev
.pending
.objects
[i
].item
;
1200 if (!(o
->flags
& UNINTERESTING
))
1201 head
= (struct commit
*)o
;
1203 /* We can't generate a cover letter without any patches */
1208 if (ignore_if_in_upstream
) {
1209 /* Don't say anything if head and upstream are the same. */
1210 if (rev
.pending
.nr
== 2) {
1211 struct object_array_entry
*o
= rev
.pending
.objects
;
1212 if (hashcmp(o
[0].item
->sha1
, o
[1].item
->sha1
) == 0)
1215 get_patch_ids(&rev
, &ids
, prefix
);
1219 realstdout
= xfdopen(xdup(1), "w");
1221 if (prepare_revision_walk(&rev
))
1222 die("revision walk setup failed");
1224 while ((commit
= get_revision(&rev
)) != NULL
) {
1225 if (commit
->object
.flags
& BOUNDARY
) {
1227 origin
= (boundary_count
== 1) ? commit
: NULL
;
1231 if (ignore_if_in_upstream
&&
1232 has_commit_patch_id(commit
, &ids
))
1236 list
= xrealloc(list
, nr
* sizeof(list
[0]));
1237 list
[nr
- 1] = commit
;
1240 if (!keep_subject
&& auto_number
&& total
> 1)
1243 rev
.total
= total
+ start_number
- 1;
1244 if (in_reply_to
|| thread
|| cover_letter
)
1245 rev
.ref_message_ids
= xcalloc(1, sizeof(struct string_list
));
1247 const char *msgid
= clean_message_id(in_reply_to
);
1248 string_list_append(rev
.ref_message_ids
, msgid
);
1250 rev
.numbered_files
= numbered_files
;
1251 rev
.patch_suffix
= fmt_patch_suffix
;
1254 gen_message_id(&rev
, "cover");
1255 make_cover_letter(&rev
, use_stdout
, numbered
, numbered_files
,
1256 origin
, nr
, list
, head
);
1260 rev
.add_signoff
= add_signoff
;
1264 rev
.nr
= total
- nr
+ (start_number
- 1);
1265 /* Make the second and subsequent mails replies to the first */
1267 /* Have we already had a message ID? */
1268 if (rev
.message_id
) {
1270 * For deep threading: make every mail
1271 * a reply to the previous one, no
1272 * matter what other options are set.
1274 * For shallow threading:
1276 * Without --cover-letter and
1277 * --in-reply-to, make every mail a
1278 * reply to the one before.
1280 * With --in-reply-to but no
1281 * --cover-letter, make every mail a
1282 * reply to the <reply-to>.
1284 * With --cover-letter, make every
1285 * mail but the cover letter a reply
1286 * to the cover letter. The cover
1287 * letter is a reply to the
1288 * --in-reply-to, if specified.
1290 if (thread
== THREAD_SHALLOW
1291 && rev
.ref_message_ids
->nr
> 0
1292 && (!cover_letter
|| rev
.nr
> 1))
1293 free(rev
.message_id
);
1295 string_list_append(rev
.ref_message_ids
,
1298 gen_message_id(&rev
, sha1_to_hex(commit
->object
.sha1
));
1301 if (!use_stdout
&& reopen_stdout(numbered_files
? NULL
: commit
,
1303 die("Failed to create output files");
1304 shown
= log_tree_commit(&rev
, commit
);
1305 free(commit
->buffer
);
1306 commit
->buffer
= NULL
;
1308 /* We put one extra blank line between formatted
1309 * patches and this flag is used by log-tree code
1310 * to see if it needs to emit a LF before showing
1311 * the log; when using one file per patch, we do
1312 * not want the extra blank line.
1317 if (rev
.mime_boundary
)
1318 printf("\n--%s%s--\n\n\n",
1319 mime_boundary_leader
,
1328 string_list_clear(&extra_to
, 0);
1329 string_list_clear(&extra_cc
, 0);
1330 string_list_clear(&extra_hdr
, 0);
1331 if (ignore_if_in_upstream
)
1332 free_patch_ids(&ids
);
1336 static int add_pending_commit(const char *arg
, struct rev_info
*revs
, int flags
)
1338 unsigned char sha1
[20];
1339 if (get_sha1(arg
, sha1
) == 0) {
1340 struct commit
*commit
= lookup_commit_reference(sha1
);
1342 commit
->object
.flags
|= flags
;
1343 add_pending_object(revs
, &commit
->object
, arg
);
1350 static const char * const cherry_usage
[] = {
1351 "git cherry [-v] [<upstream> [<head> [<limit>]]]",
1355 int cmd_cherry(int argc
, const char **argv
, const char *prefix
)
1357 struct rev_info revs
;
1358 struct patch_ids ids
;
1359 struct commit
*commit
;
1360 struct commit_list
*list
= NULL
;
1361 struct branch
*current_branch
;
1362 const char *upstream
;
1363 const char *head
= "HEAD";
1364 const char *limit
= NULL
;
1365 int verbose
= 0, abbrev
= 0;
1367 struct option options
[] = {
1368 OPT__ABBREV(&abbrev
),
1369 OPT__VERBOSE(&verbose
, "be verbose"),
1373 argc
= parse_options(argc
, argv
, prefix
, options
, cherry_usage
, 0);
1386 current_branch
= branch_get(NULL
);
1387 if (!current_branch
|| !current_branch
->merge
1388 || !current_branch
->merge
[0]
1389 || !current_branch
->merge
[0]->dst
) {
1390 fprintf(stderr
, "Could not find a tracked"
1391 " remote branch, please"
1392 " specify <upstream> manually.\n");
1393 usage_with_options(cherry_usage
, options
);
1396 upstream
= current_branch
->merge
[0]->dst
;
1399 init_revisions(&revs
, prefix
);
1401 revs
.combine_merges
= 0;
1402 revs
.ignore_merges
= 1;
1403 DIFF_OPT_SET(&revs
.diffopt
, RECURSIVE
);
1405 if (add_pending_commit(head
, &revs
, 0))
1406 die("Unknown commit %s", head
);
1407 if (add_pending_commit(upstream
, &revs
, UNINTERESTING
))
1408 die("Unknown commit %s", upstream
);
1410 /* Don't say anything if head and upstream are the same. */
1411 if (revs
.pending
.nr
== 2) {
1412 struct object_array_entry
*o
= revs
.pending
.objects
;
1413 if (hashcmp(o
[0].item
->sha1
, o
[1].item
->sha1
) == 0)
1417 get_patch_ids(&revs
, &ids
, prefix
);
1419 if (limit
&& add_pending_commit(limit
, &revs
, UNINTERESTING
))
1420 die("Unknown commit %s", limit
);
1422 /* reverse the list of commits */
1423 if (prepare_revision_walk(&revs
))
1424 die("revision walk setup failed");
1425 while ((commit
= get_revision(&revs
)) != NULL
) {
1427 if (commit
->parents
&& commit
->parents
->next
)
1430 commit_list_insert(commit
, &list
);
1436 commit
= list
->item
;
1437 if (has_commit_patch_id(commit
, &ids
))
1441 struct strbuf buf
= STRBUF_INIT
;
1442 struct pretty_print_context ctx
= {0};
1443 pretty_print_commit(CMIT_FMT_ONELINE
, commit
,
1445 printf("%c %s %s\n", sign
,
1446 find_unique_abbrev(commit
->object
.sha1
, abbrev
),
1448 strbuf_release(&buf
);
1451 printf("%c %s\n", sign
,
1452 find_unique_abbrev(commit
->object
.sha1
, abbrev
));
1458 free_patch_ids(&ids
);