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 int decoration_given
;
29 static const char *fmt_patch_subject_prefix
= "PATCH";
30 static const char *fmt_pretty
;
32 static const char * const builtin_log_usage
[] = {
33 "git log [<options>] [<since>..<until>] [[--] <path>...]\n"
34 " or: git show [options] <object>...",
38 static int parse_decoration_style(const char *var
, const char *value
)
40 switch (git_config_maybe_bool(var
, value
)) {
42 return DECORATE_SHORT_REFS
;
48 if (!strcmp(value
, "full"))
49 return DECORATE_FULL_REFS
;
50 else if (!strcmp(value
, "short"))
51 return DECORATE_SHORT_REFS
;
55 static int decorate_callback(const struct option
*opt
, const char *arg
, int unset
)
60 decoration_style
= parse_decoration_style("command line", arg
);
62 decoration_style
= DECORATE_SHORT_REFS
;
64 if (decoration_style
< 0)
65 die("invalid --decorate option: %s", arg
);
72 static void cmd_log_init(int argc
, const char **argv
, const char *prefix
,
73 struct rev_info
*rev
, struct setup_revision_opt
*opt
)
75 struct userformat_want w
;
76 int quiet
= 0, source
= 0;
78 const struct option builtin_log_options
[] = {
79 OPT_BOOLEAN(0, "quiet", &quiet
, "supress diff output"),
80 OPT_BOOLEAN(0, "source", &source
, "show source"),
81 { OPTION_CALLBACK
, 0, "decorate", NULL
, NULL
, "decorate options",
82 PARSE_OPT_OPTARG
, decorate_callback
},
86 argc
= parse_options(argc
, argv
, prefix
,
87 builtin_log_options
, builtin_log_usage
,
88 PARSE_OPT_KEEP_ARGV0
| PARSE_OPT_KEEP_UNKNOWN
|
89 PARSE_OPT_KEEP_DASHDASH
);
91 rev
->abbrev
= DEFAULT_ABBREV
;
92 rev
->commit_format
= CMIT_FMT_DEFAULT
;
94 get_commit_format(fmt_pretty
, rev
);
95 rev
->verbose_header
= 1;
96 DIFF_OPT_SET(&rev
->diffopt
, RECURSIVE
);
97 rev
->show_root_diff
= default_show_root
;
98 rev
->subject_prefix
= fmt_patch_subject_prefix
;
99 DIFF_OPT_SET(&rev
->diffopt
, ALLOW_TEXTCONV
);
101 if (default_date_mode
)
102 rev
->date_mode
= parse_date_format(default_date_mode
);
104 argc
= setup_revisions(argc
, argv
, rev
, opt
);
106 /* Any arguments at this point are not recognized */
108 die("unrecognized argument: %s", argv
[1]);
110 memset(&w
, 0, sizeof(w
));
111 userformat_find_requirements(NULL
, &w
);
113 if (!rev
->show_notes_given
&& (!rev
->pretty_given
|| w
.notes
))
116 init_display_notes(&rev
->notes_opt
);
118 if (rev
->diffopt
.pickaxe
|| rev
->diffopt
.filter
)
119 rev
->always_show_header
= 0;
120 if (DIFF_OPT_TST(&rev
->diffopt
, FOLLOW_RENAMES
)) {
121 rev
->always_show_header
= 0;
122 if (rev
->diffopt
.nr_paths
!= 1)
123 usage("git logs can only follow renames on one pathname at a time");
127 rev
->show_source
= 1;
130 * defeat log.decorate configuration interacting with --pretty=raw
131 * from the command line.
133 if (!decoration_given
&& rev
->pretty_given
134 && rev
->commit_format
== CMIT_FMT_RAW
)
135 decoration_style
= 0;
137 if (decoration_style
) {
138 rev
->show_decorations
= 1;
139 load_ref_decorations(decoration_style
);
145 * This gives a rough estimate for how many commits we
146 * will print out in the list.
148 static int estimate_commit_count(struct rev_info
*rev
, struct commit_list
*list
)
153 struct commit
*commit
= list
->item
;
154 unsigned int flags
= commit
->object
.flags
;
156 if (!(flags
& (TREESAME
| UNINTERESTING
)))
162 static void show_early_header(struct rev_info
*rev
, const char *stage
, int nr
)
164 if (rev
->shown_one
) {
166 if (rev
->commit_format
!= CMIT_FMT_ONELINE
)
167 putchar(rev
->diffopt
.line_termination
);
169 printf("Final output: %d %s\n", nr
, stage
);
172 static struct itimerval early_output_timer
;
174 static void log_show_early(struct rev_info
*revs
, struct commit_list
*list
)
176 int i
= revs
->early_output
;
179 sort_in_topological_order(&list
, revs
->lifo
);
181 struct commit
*commit
= list
->item
;
182 switch (simplify_commit(revs
, commit
)) {
185 int n
= estimate_commit_count(revs
, list
);
186 show_early_header(revs
, "incomplete", n
);
189 log_tree_commit(revs
, commit
);
200 /* Did we already get enough commits for the early output? */
205 * ..if no, then repeat it twice a second until we
208 * NOTE! We don't use "it_interval", because if the
209 * reader isn't listening, we want our output to be
210 * throttled by the writing, and not have the timer
211 * trigger every second even if we're blocked on a
214 early_output_timer
.it_value
.tv_sec
= 0;
215 early_output_timer
.it_value
.tv_usec
= 500000;
216 setitimer(ITIMER_REAL
, &early_output_timer
, NULL
);
219 static void early_output(int signal
)
221 show_early_output
= log_show_early
;
224 static void setup_early_output(struct rev_info
*rev
)
229 * Set up the signal handler, minimally intrusively:
230 * we only set a single volatile integer word (not
231 * using sigatomic_t - trying to avoid unnecessary
232 * system dependencies and headers), and using
235 memset(&sa
, 0, sizeof(sa
));
236 sa
.sa_handler
= early_output
;
237 sigemptyset(&sa
.sa_mask
);
238 sa
.sa_flags
= SA_RESTART
;
239 sigaction(SIGALRM
, &sa
, NULL
);
242 * If we can get the whole output in less than a
243 * tenth of a second, don't even bother doing the
244 * early-output thing..
246 * This is a one-time-only trigger.
248 early_output_timer
.it_value
.tv_sec
= 0;
249 early_output_timer
.it_value
.tv_usec
= 100000;
250 setitimer(ITIMER_REAL
, &early_output_timer
, NULL
);
253 static void finish_early_output(struct rev_info
*rev
)
255 int n
= estimate_commit_count(rev
, rev
->commits
);
256 signal(SIGALRM
, SIG_IGN
);
257 show_early_header(rev
, "done", n
);
260 static int cmd_log_walk(struct rev_info
*rev
)
262 struct commit
*commit
;
264 if (rev
->early_output
)
265 setup_early_output(rev
);
267 if (prepare_revision_walk(rev
))
268 die("revision walk setup failed");
270 if (rev
->early_output
)
271 finish_early_output(rev
);
274 * For --check and --exit-code, the exit code is based on CHECK_FAILED
275 * and HAS_CHANGES being accumulated in rev->diffopt, so be careful to
276 * retain that state information if replacing rev->diffopt in this loop
278 while ((commit
= get_revision(rev
)) != NULL
) {
279 if (!log_tree_commit(rev
, commit
) &&
282 * We decremented max_count in get_revision,
283 * but we didn't actually show the commit.
286 if (!rev
->reflog_info
) {
287 /* we allow cycles in reflog ancestry */
288 free(commit
->buffer
);
289 commit
->buffer
= NULL
;
291 free_commit_list(commit
->parents
);
292 commit
->parents
= NULL
;
294 if (rev
->diffopt
.output_format
& DIFF_FORMAT_CHECKDIFF
&&
295 DIFF_OPT_TST(&rev
->diffopt
, CHECK_FAILED
)) {
298 return diff_result_code(&rev
->diffopt
, 0);
301 static int git_log_config(const char *var
, const char *value
, void *cb
)
303 if (!strcmp(var
, "format.pretty"))
304 return git_config_string(&fmt_pretty
, var
, value
);
305 if (!strcmp(var
, "format.subjectprefix"))
306 return git_config_string(&fmt_patch_subject_prefix
, var
, value
);
307 if (!strcmp(var
, "log.date"))
308 return git_config_string(&default_date_mode
, var
, value
);
309 if (!strcmp(var
, "log.decorate")) {
310 decoration_style
= parse_decoration_style(var
, value
);
311 if (decoration_style
< 0)
312 decoration_style
= 0; /* maybe warn? */
315 if (!strcmp(var
, "log.showroot")) {
316 default_show_root
= git_config_bool(var
, value
);
319 if (!prefixcmp(var
, "color.decorate."))
320 return parse_decorate_color_config(var
, 15, value
);
322 return git_diff_ui_config(var
, value
, cb
);
325 int cmd_whatchanged(int argc
, const char **argv
, const char *prefix
)
328 struct setup_revision_opt opt
;
330 git_config(git_log_config
, NULL
);
332 if (diff_use_color_default
== -1)
333 diff_use_color_default
= git_use_color_default
;
335 init_revisions(&rev
, prefix
);
337 rev
.simplify_history
= 0;
338 memset(&opt
, 0, sizeof(opt
));
340 cmd_log_init(argc
, argv
, prefix
, &rev
, &opt
);
341 if (!rev
.diffopt
.output_format
)
342 rev
.diffopt
.output_format
= DIFF_FORMAT_RAW
;
343 return cmd_log_walk(&rev
);
346 static void show_tagger(char *buf
, int len
, struct rev_info
*rev
)
348 struct strbuf out
= STRBUF_INIT
;
350 pp_user_info("Tagger", rev
->commit_format
, &out
, buf
, rev
->date_mode
,
351 get_log_output_encoding());
352 printf("%s", out
.buf
);
353 strbuf_release(&out
);
356 static int show_object(const unsigned char *sha1
, int show_tag_object
,
357 struct rev_info
*rev
)
360 enum object_type type
;
361 char *buf
= read_sha1_file(sha1
, &type
, &size
);
365 return error("Could not read object %s", sha1_to_hex(sha1
));
368 while (offset
< size
&& buf
[offset
] != '\n') {
369 int new_offset
= offset
+ 1;
370 while (new_offset
< size
&& buf
[new_offset
++] != '\n')
372 if (!prefixcmp(buf
+ offset
, "tagger "))
373 show_tagger(buf
+ offset
+ 7,
374 new_offset
- offset
- 7, rev
);
379 fwrite(buf
+ offset
, size
- offset
, 1, stdout
);
384 static int show_tree_object(const unsigned char *sha1
,
385 const char *base
, int baselen
,
386 const char *pathname
, unsigned mode
, int stage
, void *context
)
388 printf("%s%s\n", pathname
, S_ISDIR(mode
) ? "/" : "");
392 static void show_rev_tweak_rev(struct rev_info
*rev
, struct setup_revision_opt
*opt
)
394 if (rev
->ignore_merges
) {
395 /* There was no "-m" on the command line */
396 rev
->ignore_merges
= 0;
397 if (!rev
->first_parent_only
&& !rev
->combine_merges
) {
398 /* No "--first-parent", "-c", nor "--cc" */
399 rev
->combine_merges
= 1;
400 rev
->dense_combined_merges
= 1;
403 if (!rev
->diffopt
.output_format
)
404 rev
->diffopt
.output_format
= DIFF_FORMAT_PATCH
;
407 int cmd_show(int argc
, const char **argv
, const char *prefix
)
410 struct object_array_entry
*objects
;
411 struct setup_revision_opt opt
;
412 int i
, count
, ret
= 0;
414 git_config(git_log_config
, NULL
);
416 if (diff_use_color_default
== -1)
417 diff_use_color_default
= git_use_color_default
;
419 init_revisions(&rev
, prefix
);
421 rev
.always_show_header
= 1;
423 memset(&opt
, 0, sizeof(opt
));
425 opt
.tweak
= show_rev_tweak_rev
;
426 cmd_log_init(argc
, argv
, prefix
, &rev
, &opt
);
428 count
= rev
.pending
.nr
;
429 objects
= rev
.pending
.objects
;
430 for (i
= 0; i
< count
&& !ret
; i
++) {
431 struct object
*o
= objects
[i
].item
;
432 const char *name
= objects
[i
].name
;
435 ret
= show_object(o
->sha1
, 0, NULL
);
438 struct tag
*t
= (struct tag
*)o
;
442 printf("%stag %s%s\n",
443 diff_get_color_opt(&rev
.diffopt
, DIFF_COMMIT
),
445 diff_get_color_opt(&rev
.diffopt
, DIFF_RESET
));
446 ret
= show_object(o
->sha1
, 1, &rev
);
450 o
= parse_object(t
->tagged
->sha1
);
452 ret
= error("Could not read object %s",
453 sha1_to_hex(t
->tagged
->sha1
));
461 printf("%stree %s%s\n\n",
462 diff_get_color_opt(&rev
.diffopt
, DIFF_COMMIT
),
464 diff_get_color_opt(&rev
.diffopt
, DIFF_RESET
));
465 read_tree_recursive((struct tree
*)o
, "", 0, 0, NULL
,
466 show_tree_object
, NULL
);
470 rev
.pending
.nr
= rev
.pending
.alloc
= 0;
471 rev
.pending
.objects
= NULL
;
472 add_object_array(o
, name
, &rev
.pending
);
473 ret
= cmd_log_walk(&rev
);
476 ret
= error("Unknown type: %d", o
->type
);
484 * This is equivalent to "git log -g --abbrev-commit --pretty=oneline"
486 int cmd_log_reflog(int argc
, const char **argv
, const char *prefix
)
489 struct setup_revision_opt opt
;
491 git_config(git_log_config
, NULL
);
493 if (diff_use_color_default
== -1)
494 diff_use_color_default
= git_use_color_default
;
496 init_revisions(&rev
, prefix
);
497 init_reflog_walk(&rev
.reflog_info
);
498 rev
.abbrev_commit
= 1;
499 rev
.verbose_header
= 1;
500 memset(&opt
, 0, sizeof(opt
));
502 cmd_log_init(argc
, argv
, prefix
, &rev
, &opt
);
505 * This means that we override whatever commit format the user gave
506 * on the cmd line. Sad, but cmd_log_init() currently doesn't
507 * allow us to set a different default.
509 rev
.commit_format
= CMIT_FMT_ONELINE
;
510 rev
.use_terminator
= 1;
511 rev
.always_show_header
= 1;
513 return cmd_log_walk(&rev
);
516 int cmd_log(int argc
, const char **argv
, const char *prefix
)
519 struct setup_revision_opt opt
;
521 git_config(git_log_config
, NULL
);
523 if (diff_use_color_default
== -1)
524 diff_use_color_default
= git_use_color_default
;
526 init_revisions(&rev
, prefix
);
527 rev
.always_show_header
= 1;
528 memset(&opt
, 0, sizeof(opt
));
530 cmd_log_init(argc
, argv
, prefix
, &rev
, &opt
);
531 return cmd_log_walk(&rev
);
536 static const char *fmt_patch_suffix
= ".patch";
537 static int numbered
= 0;
538 static int auto_number
= 1;
540 static char *default_attach
= NULL
;
542 static struct string_list extra_hdr
;
543 static struct string_list extra_to
;
544 static struct string_list extra_cc
;
546 static void add_header(const char *value
)
548 struct string_list_item
*item
;
549 int len
= strlen(value
);
550 while (len
&& value
[len
- 1] == '\n')
553 if (!strncasecmp(value
, "to: ", 4)) {
554 item
= string_list_append(&extra_to
, value
+ 4);
556 } else if (!strncasecmp(value
, "cc: ", 4)) {
557 item
= string_list_append(&extra_cc
, value
+ 4);
560 item
= string_list_append(&extra_hdr
, value
);
563 item
->string
[len
] = '\0';
566 #define THREAD_SHALLOW 1
567 #define THREAD_DEEP 2
569 static int do_signoff
;
570 static const char *signature
= git_version_string
;
572 static int git_format_config(const char *var
, const char *value
, void *cb
)
574 if (!strcmp(var
, "format.headers")) {
576 die("format.headers without value");
580 if (!strcmp(var
, "format.suffix"))
581 return git_config_string(&fmt_patch_suffix
, var
, value
);
582 if (!strcmp(var
, "format.to")) {
584 return config_error_nonbool(var
);
585 string_list_append(&extra_to
, value
);
588 if (!strcmp(var
, "format.cc")) {
590 return config_error_nonbool(var
);
591 string_list_append(&extra_cc
, value
);
594 if (!strcmp(var
, "diff.color") || !strcmp(var
, "color.diff")) {
597 if (!strcmp(var
, "format.numbered")) {
598 if (value
&& !strcasecmp(value
, "auto")) {
602 numbered
= git_config_bool(var
, value
);
603 auto_number
= auto_number
&& numbered
;
606 if (!strcmp(var
, "format.attach")) {
608 default_attach
= xstrdup(value
);
610 default_attach
= xstrdup(git_version_string
);
613 if (!strcmp(var
, "format.thread")) {
614 if (value
&& !strcasecmp(value
, "deep")) {
615 thread
= THREAD_DEEP
;
618 if (value
&& !strcasecmp(value
, "shallow")) {
619 thread
= THREAD_SHALLOW
;
622 thread
= git_config_bool(var
, value
) && THREAD_SHALLOW
;
625 if (!strcmp(var
, "format.signoff")) {
626 do_signoff
= git_config_bool(var
, value
);
629 if (!strcmp(var
, "format.signature"))
630 return git_config_string(&signature
, var
, value
);
632 return git_log_config(var
, value
, cb
);
635 static FILE *realstdout
= NULL
;
636 static const char *output_directory
= NULL
;
637 static int outdir_offset
;
639 static int reopen_stdout(struct commit
*commit
, struct rev_info
*rev
)
641 struct strbuf filename
= STRBUF_INIT
;
642 int suffix_len
= strlen(fmt_patch_suffix
) + 1;
644 if (output_directory
) {
645 strbuf_addstr(&filename
, output_directory
);
647 PATH_MAX
- FORMAT_PATCH_NAME_MAX
- suffix_len
)
648 return error("name of output directory is too long");
649 if (filename
.buf
[filename
.len
- 1] != '/')
650 strbuf_addch(&filename
, '/');
653 get_patch_filename(commit
, rev
->nr
, fmt_patch_suffix
, &filename
);
655 if (!DIFF_OPT_TST(&rev
->diffopt
, QUICK
))
656 fprintf(realstdout
, "%s\n", filename
.buf
+ outdir_offset
);
658 if (freopen(filename
.buf
, "w", stdout
) == NULL
)
659 return error("Cannot open patch file %s", filename
.buf
);
661 strbuf_release(&filename
);
665 static void get_patch_ids(struct rev_info
*rev
, struct patch_ids
*ids
, const char *prefix
)
667 struct rev_info check_rev
;
668 struct commit
*commit
;
669 struct object
*o1
, *o2
;
670 unsigned flags1
, flags2
;
672 if (rev
->pending
.nr
!= 2)
673 die("Need exactly one range.");
675 o1
= rev
->pending
.objects
[0].item
;
677 o2
= rev
->pending
.objects
[1].item
;
680 if ((flags1
& UNINTERESTING
) == (flags2
& UNINTERESTING
))
685 /* given a range a..b get all patch ids for b..a */
686 init_revisions(&check_rev
, prefix
);
687 o1
->flags
^= UNINTERESTING
;
688 o2
->flags
^= UNINTERESTING
;
689 add_pending_object(&check_rev
, o1
, "o1");
690 add_pending_object(&check_rev
, o2
, "o2");
691 if (prepare_revision_walk(&check_rev
))
692 die("revision walk setup failed");
694 while ((commit
= get_revision(&check_rev
)) != NULL
) {
696 if (commit
->parents
&& commit
->parents
->next
)
699 add_commit_patch_id(commit
, ids
);
702 /* reset for next revision walk */
703 clear_commit_marks((struct commit
*)o1
,
704 SEEN
| UNINTERESTING
| SHOWN
| ADDED
);
705 clear_commit_marks((struct commit
*)o2
,
706 SEEN
| UNINTERESTING
| SHOWN
| ADDED
);
711 static void gen_message_id(struct rev_info
*info
, char *base
)
713 const char *committer
= git_committer_info(IDENT_WARN_ON_NO_NAME
);
714 const char *email_start
= strrchr(committer
, '<');
715 const char *email_end
= strrchr(committer
, '>');
716 struct strbuf buf
= STRBUF_INIT
;
717 if (!email_start
|| !email_end
|| email_start
> email_end
- 1)
718 die("Could not extract email from committer identity.");
719 strbuf_addf(&buf
, "%s.%lu.git.%.*s", base
,
720 (unsigned long) time(NULL
),
721 (int)(email_end
- email_start
- 1), email_start
+ 1);
722 info
->message_id
= strbuf_detach(&buf
, NULL
);
725 static void print_signature(void)
727 if (signature
&& *signature
)
728 printf("-- \n%s\n\n", signature
);
731 static void make_cover_letter(struct rev_info
*rev
, int use_stdout
,
732 int numbered
, int numbered_files
,
733 struct commit
*origin
,
734 int nr
, struct commit
**list
, struct commit
*head
)
736 const char *committer
;
737 const char *subject_start
= NULL
;
738 const char *body
= "*** SUBJECT HERE ***\n\n*** BLURB HERE ***\n";
740 const char *extra_headers
= rev
->extra_headers
;
742 struct strbuf sb
= STRBUF_INIT
;
744 const char *encoding
= "UTF-8";
745 struct diff_options opts
;
746 int need_8bit_cte
= 0;
747 struct commit
*commit
= NULL
;
749 if (rev
->commit_format
!= CMIT_FMT_EMAIL
)
750 die("Cover letter needs email format");
752 committer
= git_committer_info(0);
754 if (!numbered_files
) {
756 * We fake a commit for the cover letter so we get the filename
759 commit
= xcalloc(1, sizeof(*commit
));
760 commit
->buffer
= xmalloc(400);
761 snprintf(commit
->buffer
, 400,
762 "tree 0000000000000000000000000000000000000000\n"
767 sha1_to_hex(head
->object
.sha1
), committer
, committer
);
770 if (!use_stdout
&& reopen_stdout(commit
, rev
))
775 free(commit
->buffer
);
779 log_write_email_headers(rev
, head
, &subject_start
, &extra_headers
,
782 for (i
= 0; !need_8bit_cte
&& i
< nr
; i
++)
783 if (has_non_ascii(list
[i
]->buffer
))
787 pp_user_info(NULL
, CMIT_FMT_EMAIL
, &sb
, committer
, DATE_RFC2822
,
789 pp_title_line(CMIT_FMT_EMAIL
, &msg
, &sb
, subject_start
, extra_headers
,
790 encoding
, need_8bit_cte
);
791 pp_remainder(CMIT_FMT_EMAIL
, &msg
, &sb
, 0);
792 printf("%s\n", sb
.buf
);
801 for (i
= 0; i
< nr
; i
++)
802 shortlog_add_commit(&log
, list
[i
]);
804 shortlog_output(&log
);
807 * We can only do diffstat with a unique reference point
812 memcpy(&opts
, &rev
->diffopt
, sizeof(opts
));
813 opts
.output_format
= DIFF_FORMAT_SUMMARY
| DIFF_FORMAT_DIFFSTAT
;
815 diff_setup_done(&opts
);
817 diff_tree_sha1(origin
->tree
->object
.sha1
,
818 head
->tree
->object
.sha1
,
827 static const char *clean_message_id(const char *msg_id
)
830 const char *a
, *z
, *m
;
833 while ((ch
= *m
) && (isspace(ch
) || (ch
== '<')))
838 if (!isspace(ch
) && (ch
!= '>'))
843 die("insane in-reply-to: %s", msg_id
);
846 return xmemdupz(a
, z
- a
);
849 static const char *set_outdir(const char *prefix
, const char *output_directory
)
851 if (output_directory
&& is_absolute_path(output_directory
))
852 return output_directory
;
854 if (!prefix
|| !*prefix
) {
855 if (output_directory
)
856 return output_directory
;
857 /* The user did not explicitly ask for "./" */
862 outdir_offset
= strlen(prefix
);
863 if (!output_directory
)
866 return xstrdup(prefix_filename(prefix
, outdir_offset
,
870 static const char * const builtin_format_patch_usage
[] = {
871 "git format-patch [options] [<since> | <revision range>]",
875 static int keep_subject
= 0;
877 static int keep_callback(const struct option
*opt
, const char *arg
, int unset
)
879 ((struct rev_info
*)opt
->value
)->total
= -1;
884 static int subject_prefix
= 0;
886 static int subject_prefix_callback(const struct option
*opt
, const char *arg
,
890 ((struct rev_info
*)opt
->value
)->subject_prefix
= arg
;
894 static int numbered_cmdline_opt
= 0;
896 static int numbered_callback(const struct option
*opt
, const char *arg
,
899 *(int *)opt
->value
= numbered_cmdline_opt
= unset
? 0 : 1;
905 static int no_numbered_callback(const struct option
*opt
, const char *arg
,
908 return numbered_callback(opt
, arg
, 1);
911 static int output_directory_callback(const struct option
*opt
, const char *arg
,
914 const char **dir
= (const char **)opt
->value
;
916 die("Two output directories?");
921 static int thread_callback(const struct option
*opt
, const char *arg
, int unset
)
923 int *thread
= (int *)opt
->value
;
926 else if (!arg
|| !strcmp(arg
, "shallow"))
927 *thread
= THREAD_SHALLOW
;
928 else if (!strcmp(arg
, "deep"))
929 *thread
= THREAD_DEEP
;
935 static int attach_callback(const struct option
*opt
, const char *arg
, int unset
)
937 struct rev_info
*rev
= (struct rev_info
*)opt
->value
;
939 rev
->mime_boundary
= NULL
;
941 rev
->mime_boundary
= arg
;
943 rev
->mime_boundary
= git_version_string
;
944 rev
->no_inline
= unset
? 0 : 1;
948 static int inline_callback(const struct option
*opt
, const char *arg
, int unset
)
950 struct rev_info
*rev
= (struct rev_info
*)opt
->value
;
952 rev
->mime_boundary
= NULL
;
954 rev
->mime_boundary
= arg
;
956 rev
->mime_boundary
= git_version_string
;
961 static int header_callback(const struct option
*opt
, const char *arg
, int unset
)
964 string_list_clear(&extra_hdr
, 0);
965 string_list_clear(&extra_to
, 0);
966 string_list_clear(&extra_cc
, 0);
973 static int to_callback(const struct option
*opt
, const char *arg
, int unset
)
976 string_list_clear(&extra_to
, 0);
978 string_list_append(&extra_to
, arg
);
982 static int cc_callback(const struct option
*opt
, const char *arg
, int unset
)
985 string_list_clear(&extra_cc
, 0);
987 string_list_append(&extra_cc
, arg
);
991 int cmd_format_patch(int argc
, const char **argv
, const char *prefix
)
993 struct commit
*commit
;
994 struct commit
**list
= NULL
;
996 struct setup_revision_opt s_r_opt
;
997 int nr
= 0, total
, i
;
999 int start_number
= -1;
1000 int numbered_files
= 0; /* _just_ numbers */
1001 int ignore_if_in_upstream
= 0;
1002 int cover_letter
= 0;
1003 int boundary_count
= 0;
1004 int no_binary_diff
= 0;
1005 struct commit
*origin
= NULL
, *head
= NULL
;
1006 const char *in_reply_to
= NULL
;
1007 struct patch_ids ids
;
1008 char *add_signoff
= NULL
;
1009 struct strbuf buf
= STRBUF_INIT
;
1010 int use_patch_format
= 0;
1011 const struct option builtin_format_patch_options
[] = {
1012 { OPTION_CALLBACK
, 'n', "numbered", &numbered
, NULL
,
1013 "use [PATCH n/m] even with a single patch",
1014 PARSE_OPT_NOARG
, numbered_callback
},
1015 { OPTION_CALLBACK
, 'N', "no-numbered", &numbered
, NULL
,
1016 "use [PATCH] even with multiple patches",
1017 PARSE_OPT_NOARG
, no_numbered_callback
},
1018 OPT_BOOLEAN('s', "signoff", &do_signoff
, "add Signed-off-by:"),
1019 OPT_BOOLEAN(0, "stdout", &use_stdout
,
1020 "print patches to standard out"),
1021 OPT_BOOLEAN(0, "cover-letter", &cover_letter
,
1022 "generate a cover letter"),
1023 OPT_BOOLEAN(0, "numbered-files", &numbered_files
,
1024 "use simple number sequence for output file names"),
1025 OPT_STRING(0, "suffix", &fmt_patch_suffix
, "sfx",
1026 "use <sfx> instead of '.patch'"),
1027 OPT_INTEGER(0, "start-number", &start_number
,
1028 "start numbering patches at <n> instead of 1"),
1029 { OPTION_CALLBACK
, 0, "subject-prefix", &rev
, "prefix",
1030 "Use [<prefix>] instead of [PATCH]",
1031 PARSE_OPT_NONEG
, subject_prefix_callback
},
1032 { OPTION_CALLBACK
, 'o', "output-directory", &output_directory
,
1033 "dir", "store resulting files in <dir>",
1034 PARSE_OPT_NONEG
, output_directory_callback
},
1035 { OPTION_CALLBACK
, 'k', "keep-subject", &rev
, NULL
,
1036 "don't strip/add [PATCH]",
1037 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
, keep_callback
},
1038 OPT_BOOLEAN(0, "no-binary", &no_binary_diff
,
1039 "don't output binary diffs"),
1040 OPT_BOOLEAN(0, "ignore-if-in-upstream", &ignore_if_in_upstream
,
1041 "don't include a patch matching a commit upstream"),
1042 { OPTION_BOOLEAN
, 'p', "no-stat", &use_patch_format
, NULL
,
1043 "show patch format instead of default (patch + stat)",
1044 PARSE_OPT_NONEG
| PARSE_OPT_NOARG
},
1045 OPT_GROUP("Messaging"),
1046 { OPTION_CALLBACK
, 0, "add-header", NULL
, "header",
1047 "add email header", 0, header_callback
},
1048 { OPTION_CALLBACK
, 0, "to", NULL
, "email", "add To: header",
1050 { OPTION_CALLBACK
, 0, "cc", NULL
, "email", "add Cc: header",
1052 OPT_STRING(0, "in-reply-to", &in_reply_to
, "message-id",
1053 "make first mail a reply to <message-id>"),
1054 { OPTION_CALLBACK
, 0, "attach", &rev
, "boundary",
1055 "attach the patch", PARSE_OPT_OPTARG
,
1057 { OPTION_CALLBACK
, 0, "inline", &rev
, "boundary",
1059 PARSE_OPT_OPTARG
| PARSE_OPT_NONEG
,
1061 { OPTION_CALLBACK
, 0, "thread", &thread
, "style",
1062 "enable message threading, styles: shallow, deep",
1063 PARSE_OPT_OPTARG
, thread_callback
},
1064 OPT_STRING(0, "signature", &signature
, "signature",
1069 extra_hdr
.strdup_strings
= 1;
1070 extra_to
.strdup_strings
= 1;
1071 extra_cc
.strdup_strings
= 1;
1072 git_config(git_format_config
, NULL
);
1073 init_revisions(&rev
, prefix
);
1074 rev
.commit_format
= CMIT_FMT_EMAIL
;
1075 rev
.verbose_header
= 1;
1078 DIFF_OPT_SET(&rev
.diffopt
, RECURSIVE
);
1079 rev
.subject_prefix
= fmt_patch_subject_prefix
;
1080 memset(&s_r_opt
, 0, sizeof(s_r_opt
));
1081 s_r_opt
.def
= "HEAD";
1083 if (default_attach
) {
1084 rev
.mime_boundary
= default_attach
;
1089 * Parse the arguments before setup_revisions(), or something
1090 * like "git format-patch -o a123 HEAD^.." may fail; a123 is
1091 * possibly a valid SHA1.
1093 argc
= parse_options(argc
, argv
, prefix
, builtin_format_patch_options
,
1094 builtin_format_patch_usage
,
1095 PARSE_OPT_KEEP_ARGV0
| PARSE_OPT_KEEP_UNKNOWN
|
1096 PARSE_OPT_KEEP_DASHDASH
);
1099 const char *committer
;
1101 committer
= git_committer_info(IDENT_ERROR_ON_NO_NAME
);
1102 endpos
= strchr(committer
, '>');
1104 die("bogus committer info %s", committer
);
1105 add_signoff
= xmemdupz(committer
, endpos
- committer
+ 1);
1108 for (i
= 0; i
< extra_hdr
.nr
; i
++) {
1109 strbuf_addstr(&buf
, extra_hdr
.items
[i
].string
);
1110 strbuf_addch(&buf
, '\n');
1114 strbuf_addstr(&buf
, "To: ");
1115 for (i
= 0; i
< extra_to
.nr
; i
++) {
1117 strbuf_addstr(&buf
, " ");
1118 strbuf_addstr(&buf
, extra_to
.items
[i
].string
);
1119 if (i
+ 1 < extra_to
.nr
)
1120 strbuf_addch(&buf
, ',');
1121 strbuf_addch(&buf
, '\n');
1125 strbuf_addstr(&buf
, "Cc: ");
1126 for (i
= 0; i
< extra_cc
.nr
; i
++) {
1128 strbuf_addstr(&buf
, " ");
1129 strbuf_addstr(&buf
, extra_cc
.items
[i
].string
);
1130 if (i
+ 1 < extra_cc
.nr
)
1131 strbuf_addch(&buf
, ',');
1132 strbuf_addch(&buf
, '\n');
1135 rev
.extra_headers
= strbuf_detach(&buf
, NULL
);
1137 if (start_number
< 0)
1141 * If numbered is set solely due to format.numbered in config,
1142 * and it would conflict with --keep-subject (-k) from the
1143 * command line, reset "numbered".
1145 if (numbered
&& keep_subject
&& !numbered_cmdline_opt
)
1148 if (numbered
&& keep_subject
)
1149 die ("-n and -k are mutually exclusive.");
1150 if (keep_subject
&& subject_prefix
)
1151 die ("--subject-prefix and -k are mutually exclusive.");
1153 argc
= setup_revisions(argc
, argv
, &rev
, &s_r_opt
);
1155 die ("unrecognized argument: %s", argv
[1]);
1157 if (rev
.diffopt
.output_format
& DIFF_FORMAT_NAME
)
1158 die("--name-only does not make sense");
1159 if (rev
.diffopt
.output_format
& DIFF_FORMAT_NAME_STATUS
)
1160 die("--name-status does not make sense");
1161 if (rev
.diffopt
.output_format
& DIFF_FORMAT_CHECKDIFF
)
1162 die("--check does not make sense");
1164 if (!use_patch_format
&&
1165 (!rev
.diffopt
.output_format
||
1166 rev
.diffopt
.output_format
== DIFF_FORMAT_PATCH
))
1167 rev
.diffopt
.output_format
= DIFF_FORMAT_DIFFSTAT
| DIFF_FORMAT_SUMMARY
;
1169 /* Always generate a patch */
1170 rev
.diffopt
.output_format
|= DIFF_FORMAT_PATCH
;
1172 if (!DIFF_OPT_TST(&rev
.diffopt
, TEXT
) && !no_binary_diff
)
1173 DIFF_OPT_SET(&rev
.diffopt
, BINARY
);
1176 init_display_notes(&rev
.notes_opt
);
1179 output_directory
= set_outdir(prefix
, output_directory
);
1183 if (output_directory
) {
1185 die("standard output, or directory, which one?");
1186 if (mkdir(output_directory
, 0777) < 0 && errno
!= EEXIST
)
1187 die_errno("Could not create directory '%s'",
1191 if (rev
.pending
.nr
== 1) {
1192 if (rev
.max_count
< 0 && !rev
.show_root_diff
) {
1194 * This is traditional behaviour of "git format-patch
1195 * origin" that prepares what the origin side still
1198 rev
.pending
.objects
[0].item
->flags
|= UNINTERESTING
;
1199 add_head_to_pending(&rev
);
1202 * Otherwise, it is "format-patch -22 HEAD", and/or
1203 * "format-patch --root HEAD". The user wants
1204 * get_revision() to do the usual traversal.
1209 * We cannot move this anywhere earlier because we do want to
1210 * know if --root was given explicitly from the command line.
1212 rev
.show_root_diff
= 1;
1215 /* remember the range */
1217 for (i
= 0; i
< rev
.pending
.nr
; i
++) {
1218 struct object
*o
= rev
.pending
.objects
[i
].item
;
1219 if (!(o
->flags
& UNINTERESTING
))
1220 head
= (struct commit
*)o
;
1222 /* We can't generate a cover letter without any patches */
1227 if (ignore_if_in_upstream
) {
1228 /* Don't say anything if head and upstream are the same. */
1229 if (rev
.pending
.nr
== 2) {
1230 struct object_array_entry
*o
= rev
.pending
.objects
;
1231 if (hashcmp(o
[0].item
->sha1
, o
[1].item
->sha1
) == 0)
1234 get_patch_ids(&rev
, &ids
, prefix
);
1238 realstdout
= xfdopen(xdup(1), "w");
1240 if (prepare_revision_walk(&rev
))
1241 die("revision walk setup failed");
1243 while ((commit
= get_revision(&rev
)) != NULL
) {
1244 if (commit
->object
.flags
& BOUNDARY
) {
1246 origin
= (boundary_count
== 1) ? commit
: NULL
;
1250 if (ignore_if_in_upstream
&&
1251 has_commit_patch_id(commit
, &ids
))
1255 list
= xrealloc(list
, nr
* sizeof(list
[0]));
1256 list
[nr
- 1] = commit
;
1259 if (!keep_subject
&& auto_number
&& total
> 1)
1262 rev
.total
= total
+ start_number
- 1;
1263 if (in_reply_to
|| thread
|| cover_letter
)
1264 rev
.ref_message_ids
= xcalloc(1, sizeof(struct string_list
));
1266 const char *msgid
= clean_message_id(in_reply_to
);
1267 string_list_append(rev
.ref_message_ids
, msgid
);
1269 rev
.numbered_files
= numbered_files
;
1270 rev
.patch_suffix
= fmt_patch_suffix
;
1273 gen_message_id(&rev
, "cover");
1274 make_cover_letter(&rev
, use_stdout
, numbered
, numbered_files
,
1275 origin
, nr
, list
, head
);
1279 rev
.add_signoff
= add_signoff
;
1283 rev
.nr
= total
- nr
+ (start_number
- 1);
1284 /* Make the second and subsequent mails replies to the first */
1286 /* Have we already had a message ID? */
1287 if (rev
.message_id
) {
1289 * For deep threading: make every mail
1290 * a reply to the previous one, no
1291 * matter what other options are set.
1293 * For shallow threading:
1295 * Without --cover-letter and
1296 * --in-reply-to, make every mail a
1297 * reply to the one before.
1299 * With --in-reply-to but no
1300 * --cover-letter, make every mail a
1301 * reply to the <reply-to>.
1303 * With --cover-letter, make every
1304 * mail but the cover letter a reply
1305 * to the cover letter. The cover
1306 * letter is a reply to the
1307 * --in-reply-to, if specified.
1309 if (thread
== THREAD_SHALLOW
1310 && rev
.ref_message_ids
->nr
> 0
1311 && (!cover_letter
|| rev
.nr
> 1))
1312 free(rev
.message_id
);
1314 string_list_append(rev
.ref_message_ids
,
1317 gen_message_id(&rev
, sha1_to_hex(commit
->object
.sha1
));
1320 if (!use_stdout
&& reopen_stdout(numbered_files
? NULL
: commit
,
1322 die("Failed to create output files");
1323 shown
= log_tree_commit(&rev
, commit
);
1324 free(commit
->buffer
);
1325 commit
->buffer
= NULL
;
1327 /* We put one extra blank line between formatted
1328 * patches and this flag is used by log-tree code
1329 * to see if it needs to emit a LF before showing
1330 * the log; when using one file per patch, we do
1331 * not want the extra blank line.
1336 if (rev
.mime_boundary
)
1337 printf("\n--%s%s--\n\n\n",
1338 mime_boundary_leader
,
1347 string_list_clear(&extra_to
, 0);
1348 string_list_clear(&extra_cc
, 0);
1349 string_list_clear(&extra_hdr
, 0);
1350 if (ignore_if_in_upstream
)
1351 free_patch_ids(&ids
);
1355 static int add_pending_commit(const char *arg
, struct rev_info
*revs
, int flags
)
1357 unsigned char sha1
[20];
1358 if (get_sha1(arg
, sha1
) == 0) {
1359 struct commit
*commit
= lookup_commit_reference(sha1
);
1361 commit
->object
.flags
|= flags
;
1362 add_pending_object(revs
, &commit
->object
, arg
);
1369 static const char * const cherry_usage
[] = {
1370 "git cherry [-v] [<upstream> [<head> [<limit>]]]",
1374 static void print_commit(char sign
, struct commit
*commit
, int verbose
,
1378 printf("%c %s\n", sign
,
1379 find_unique_abbrev(commit
->object
.sha1
, abbrev
));
1381 struct strbuf buf
= STRBUF_INIT
;
1382 struct pretty_print_context ctx
= {0};
1383 pretty_print_commit(CMIT_FMT_ONELINE
, commit
, &buf
, &ctx
);
1384 printf("%c %s %s\n", sign
,
1385 find_unique_abbrev(commit
->object
.sha1
, abbrev
),
1387 strbuf_release(&buf
);
1391 int cmd_cherry(int argc
, const char **argv
, const char *prefix
)
1393 struct rev_info revs
;
1394 struct patch_ids ids
;
1395 struct commit
*commit
;
1396 struct commit_list
*list
= NULL
;
1397 struct branch
*current_branch
;
1398 const char *upstream
;
1399 const char *head
= "HEAD";
1400 const char *limit
= NULL
;
1401 int verbose
= 0, abbrev
= 0;
1403 struct option options
[] = {
1404 OPT__ABBREV(&abbrev
),
1405 OPT__VERBOSE(&verbose
, "be verbose"),
1409 argc
= parse_options(argc
, argv
, prefix
, options
, cherry_usage
, 0);
1422 current_branch
= branch_get(NULL
);
1423 if (!current_branch
|| !current_branch
->merge
1424 || !current_branch
->merge
[0]
1425 || !current_branch
->merge
[0]->dst
) {
1426 fprintf(stderr
, "Could not find a tracked"
1427 " remote branch, please"
1428 " specify <upstream> manually.\n");
1429 usage_with_options(cherry_usage
, options
);
1432 upstream
= current_branch
->merge
[0]->dst
;
1435 init_revisions(&revs
, prefix
);
1437 revs
.combine_merges
= 0;
1438 revs
.ignore_merges
= 1;
1439 DIFF_OPT_SET(&revs
.diffopt
, RECURSIVE
);
1441 if (add_pending_commit(head
, &revs
, 0))
1442 die("Unknown commit %s", head
);
1443 if (add_pending_commit(upstream
, &revs
, UNINTERESTING
))
1444 die("Unknown commit %s", upstream
);
1446 /* Don't say anything if head and upstream are the same. */
1447 if (revs
.pending
.nr
== 2) {
1448 struct object_array_entry
*o
= revs
.pending
.objects
;
1449 if (hashcmp(o
[0].item
->sha1
, o
[1].item
->sha1
) == 0)
1453 get_patch_ids(&revs
, &ids
, prefix
);
1455 if (limit
&& add_pending_commit(limit
, &revs
, UNINTERESTING
))
1456 die("Unknown commit %s", limit
);
1458 /* reverse the list of commits */
1459 if (prepare_revision_walk(&revs
))
1460 die("revision walk setup failed");
1461 while ((commit
= get_revision(&revs
)) != NULL
) {
1463 if (commit
->parents
&& commit
->parents
->next
)
1466 commit_list_insert(commit
, &list
);
1472 commit
= list
->item
;
1473 if (has_commit_patch_id(commit
, &ids
))
1475 print_commit(sign
, commit
, verbose
, abbrev
);
1479 free_patch_ids(&ids
);