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
,
56 int decoration_given
= 0;
58 rev
->abbrev
= DEFAULT_ABBREV
;
59 rev
->commit_format
= CMIT_FMT_DEFAULT
;
61 get_commit_format(fmt_pretty
, rev
);
62 rev
->verbose_header
= 1;
63 DIFF_OPT_SET(&rev
->diffopt
, RECURSIVE
);
64 rev
->show_root_diff
= default_show_root
;
65 rev
->subject_prefix
= fmt_patch_subject_prefix
;
66 DIFF_OPT_SET(&rev
->diffopt
, ALLOW_TEXTCONV
);
68 if (default_date_mode
)
69 rev
->date_mode
= parse_date_format(default_date_mode
);
72 * Check for -h before setup_revisions(), or "git log -h" will
73 * fail when run without a git directory.
75 if (argc
== 2 && !strcmp(argv
[1], "-h"))
76 usage(builtin_log_usage
);
77 argc
= setup_revisions(argc
, argv
, rev
, "HEAD");
79 if (!rev
->show_notes_given
&& !rev
->pretty_given
)
82 if (rev
->diffopt
.pickaxe
|| rev
->diffopt
.filter
)
83 rev
->always_show_header
= 0;
84 if (DIFF_OPT_TST(&rev
->diffopt
, FOLLOW_RENAMES
)) {
85 rev
->always_show_header
= 0;
86 if (rev
->diffopt
.nr_paths
!= 1)
87 usage("git logs can only follow renames on one pathname at a time");
89 for (i
= 1; i
< argc
; i
++) {
90 const char *arg
= argv
[i
];
91 if (!strcmp(arg
, "--decorate")) {
92 decoration_style
= DECORATE_SHORT_REFS
;
94 } else if (!prefixcmp(arg
, "--decorate=")) {
95 const char *v
= skip_prefix(arg
, "--decorate=");
96 decoration_style
= parse_decoration_style(arg
, v
);
97 if (decoration_style
< 0)
98 die("invalid --decorate option: %s", arg
);
100 } else if (!strcmp(arg
, "--no-decorate")) {
101 decoration_style
= 0;
102 } else if (!strcmp(arg
, "--source")) {
103 rev
->show_source
= 1;
104 } else if (!strcmp(arg
, "-h")) {
105 usage(builtin_log_usage
);
107 die("unrecognized argument: %s", arg
);
111 * defeat log.decorate configuration interacting with --pretty=raw
112 * from the command line.
114 if (!decoration_given
&& rev
->pretty_given
115 && rev
->commit_format
== CMIT_FMT_RAW
)
116 decoration_style
= 0;
118 if (decoration_style
) {
119 rev
->show_decorations
= 1;
120 load_ref_decorations(decoration_style
);
125 * This gives a rough estimate for how many commits we
126 * will print out in the list.
128 static int estimate_commit_count(struct rev_info
*rev
, struct commit_list
*list
)
133 struct commit
*commit
= list
->item
;
134 unsigned int flags
= commit
->object
.flags
;
136 if (!(flags
& (TREESAME
| UNINTERESTING
)))
142 static void show_early_header(struct rev_info
*rev
, const char *stage
, int nr
)
144 if (rev
->shown_one
) {
146 if (rev
->commit_format
!= CMIT_FMT_ONELINE
)
147 putchar(rev
->diffopt
.line_termination
);
149 printf("Final output: %d %s\n", nr
, stage
);
152 static struct itimerval early_output_timer
;
154 static void log_show_early(struct rev_info
*revs
, struct commit_list
*list
)
156 int i
= revs
->early_output
;
159 sort_in_topological_order(&list
, revs
->lifo
);
161 struct commit
*commit
= list
->item
;
162 switch (simplify_commit(revs
, commit
)) {
165 int n
= estimate_commit_count(revs
, list
);
166 show_early_header(revs
, "incomplete", n
);
169 log_tree_commit(revs
, commit
);
180 /* Did we already get enough commits for the early output? */
185 * ..if no, then repeat it twice a second until we
188 * NOTE! We don't use "it_interval", because if the
189 * reader isn't listening, we want our output to be
190 * throttled by the writing, and not have the timer
191 * trigger every second even if we're blocked on a
194 early_output_timer
.it_value
.tv_sec
= 0;
195 early_output_timer
.it_value
.tv_usec
= 500000;
196 setitimer(ITIMER_REAL
, &early_output_timer
, NULL
);
199 static void early_output(int signal
)
201 show_early_output
= log_show_early
;
204 static void setup_early_output(struct rev_info
*rev
)
209 * Set up the signal handler, minimally intrusively:
210 * we only set a single volatile integer word (not
211 * using sigatomic_t - trying to avoid unnecessary
212 * system dependencies and headers), and using
215 memset(&sa
, 0, sizeof(sa
));
216 sa
.sa_handler
= early_output
;
217 sigemptyset(&sa
.sa_mask
);
218 sa
.sa_flags
= SA_RESTART
;
219 sigaction(SIGALRM
, &sa
, NULL
);
222 * If we can get the whole output in less than a
223 * tenth of a second, don't even bother doing the
224 * early-output thing..
226 * This is a one-time-only trigger.
228 early_output_timer
.it_value
.tv_sec
= 0;
229 early_output_timer
.it_value
.tv_usec
= 100000;
230 setitimer(ITIMER_REAL
, &early_output_timer
, NULL
);
233 static void finish_early_output(struct rev_info
*rev
)
235 int n
= estimate_commit_count(rev
, rev
->commits
);
236 signal(SIGALRM
, SIG_IGN
);
237 show_early_header(rev
, "done", n
);
240 static int cmd_log_walk(struct rev_info
*rev
)
242 struct commit
*commit
;
244 if (rev
->early_output
)
245 setup_early_output(rev
);
247 if (prepare_revision_walk(rev
))
248 die("revision walk setup failed");
250 if (rev
->early_output
)
251 finish_early_output(rev
);
254 * For --check and --exit-code, the exit code is based on CHECK_FAILED
255 * and HAS_CHANGES being accumulated in rev->diffopt, so be careful to
256 * retain that state information if replacing rev->diffopt in this loop
258 while ((commit
= get_revision(rev
)) != NULL
) {
259 log_tree_commit(rev
, commit
);
260 if (!rev
->reflog_info
) {
261 /* we allow cycles in reflog ancestry */
262 free(commit
->buffer
);
263 commit
->buffer
= NULL
;
265 free_commit_list(commit
->parents
);
266 commit
->parents
= NULL
;
268 if (rev
->diffopt
.output_format
& DIFF_FORMAT_CHECKDIFF
&&
269 DIFF_OPT_TST(&rev
->diffopt
, CHECK_FAILED
)) {
272 return diff_result_code(&rev
->diffopt
, 0);
275 static int git_log_config(const char *var
, const char *value
, void *cb
)
277 if (!strcmp(var
, "format.pretty"))
278 return git_config_string(&fmt_pretty
, var
, value
);
279 if (!strcmp(var
, "format.subjectprefix"))
280 return git_config_string(&fmt_patch_subject_prefix
, var
, value
);
281 if (!strcmp(var
, "log.date"))
282 return git_config_string(&default_date_mode
, var
, value
);
283 if (!strcmp(var
, "log.decorate")) {
284 decoration_style
= parse_decoration_style(var
, value
);
285 if (decoration_style
< 0)
286 decoration_style
= 0; /* maybe warn? */
289 if (!strcmp(var
, "log.showroot")) {
290 default_show_root
= git_config_bool(var
, value
);
293 return git_diff_ui_config(var
, value
, cb
);
296 int cmd_whatchanged(int argc
, const char **argv
, const char *prefix
)
300 git_config(git_log_config
, NULL
);
302 if (diff_use_color_default
== -1)
303 diff_use_color_default
= git_use_color_default
;
305 init_revisions(&rev
, prefix
);
307 rev
.simplify_history
= 0;
308 cmd_log_init(argc
, argv
, prefix
, &rev
);
309 if (!rev
.diffopt
.output_format
)
310 rev
.diffopt
.output_format
= DIFF_FORMAT_RAW
;
311 return cmd_log_walk(&rev
);
314 static void show_tagger(char *buf
, int len
, struct rev_info
*rev
)
316 struct strbuf out
= STRBUF_INIT
;
318 pp_user_info("Tagger", rev
->commit_format
, &out
, buf
, rev
->date_mode
,
319 git_log_output_encoding
?
320 git_log_output_encoding
: git_commit_encoding
);
321 printf("%s", out
.buf
);
322 strbuf_release(&out
);
325 static int show_object(const unsigned char *sha1
, int show_tag_object
,
326 struct rev_info
*rev
)
329 enum object_type type
;
330 char *buf
= read_sha1_file(sha1
, &type
, &size
);
334 return error("Could not read object %s", sha1_to_hex(sha1
));
337 while (offset
< size
&& buf
[offset
] != '\n') {
338 int new_offset
= offset
+ 1;
339 while (new_offset
< size
&& buf
[new_offset
++] != '\n')
341 if (!prefixcmp(buf
+ offset
, "tagger "))
342 show_tagger(buf
+ offset
+ 7,
343 new_offset
- offset
- 7, rev
);
348 fwrite(buf
+ offset
, size
- offset
, 1, stdout
);
353 static int show_tree_object(const unsigned char *sha1
,
354 const char *base
, int baselen
,
355 const char *pathname
, unsigned mode
, int stage
, void *context
)
357 printf("%s%s\n", pathname
, S_ISDIR(mode
) ? "/" : "");
361 int cmd_show(int argc
, const char **argv
, const char *prefix
)
364 struct object_array_entry
*objects
;
365 int i
, count
, ret
= 0;
367 git_config(git_log_config
, NULL
);
369 if (diff_use_color_default
== -1)
370 diff_use_color_default
= git_use_color_default
;
372 init_revisions(&rev
, prefix
);
374 rev
.combine_merges
= 1;
375 rev
.dense_combined_merges
= 1;
376 rev
.always_show_header
= 1;
377 rev
.ignore_merges
= 0;
379 cmd_log_init(argc
, argv
, prefix
, &rev
);
381 count
= rev
.pending
.nr
;
382 objects
= rev
.pending
.objects
;
383 for (i
= 0; i
< count
&& !ret
; i
++) {
384 struct object
*o
= objects
[i
].item
;
385 const char *name
= objects
[i
].name
;
388 ret
= show_object(o
->sha1
, 0, NULL
);
391 struct tag
*t
= (struct tag
*)o
;
395 printf("%stag %s%s\n",
396 diff_get_color_opt(&rev
.diffopt
, DIFF_COMMIT
),
398 diff_get_color_opt(&rev
.diffopt
, DIFF_RESET
));
399 ret
= show_object(o
->sha1
, 1, &rev
);
403 o
= parse_object(t
->tagged
->sha1
);
405 ret
= error("Could not read object %s",
406 sha1_to_hex(t
->tagged
->sha1
));
414 printf("%stree %s%s\n\n",
415 diff_get_color_opt(&rev
.diffopt
, DIFF_COMMIT
),
417 diff_get_color_opt(&rev
.diffopt
, DIFF_RESET
));
418 read_tree_recursive((struct tree
*)o
, "", 0, 0, NULL
,
419 show_tree_object
, NULL
);
423 rev
.pending
.nr
= rev
.pending
.alloc
= 0;
424 rev
.pending
.objects
= NULL
;
425 add_object_array(o
, name
, &rev
.pending
);
426 ret
= cmd_log_walk(&rev
);
429 ret
= error("Unknown type: %d", o
->type
);
437 * This is equivalent to "git log -g --abbrev-commit --pretty=oneline"
439 int cmd_log_reflog(int argc
, const char **argv
, const char *prefix
)
443 git_config(git_log_config
, NULL
);
445 if (diff_use_color_default
== -1)
446 diff_use_color_default
= git_use_color_default
;
448 init_revisions(&rev
, prefix
);
449 init_reflog_walk(&rev
.reflog_info
);
450 rev
.abbrev_commit
= 1;
451 rev
.verbose_header
= 1;
452 cmd_log_init(argc
, argv
, prefix
, &rev
);
455 * This means that we override whatever commit format the user gave
456 * on the cmd line. Sad, but cmd_log_init() currently doesn't
457 * allow us to set a different default.
459 rev
.commit_format
= CMIT_FMT_ONELINE
;
460 rev
.use_terminator
= 1;
461 rev
.always_show_header
= 1;
464 * We get called through "git reflog", so unlike the other log
465 * routines, we need to set up our pager manually..
469 return cmd_log_walk(&rev
);
472 int cmd_log(int argc
, const char **argv
, const char *prefix
)
476 git_config(git_log_config
, NULL
);
478 if (diff_use_color_default
== -1)
479 diff_use_color_default
= git_use_color_default
;
481 init_revisions(&rev
, prefix
);
482 rev
.always_show_header
= 1;
483 cmd_log_init(argc
, argv
, prefix
, &rev
);
484 return cmd_log_walk(&rev
);
489 static const char *fmt_patch_suffix
= ".patch";
490 static int numbered
= 0;
491 static int auto_number
= 1;
493 static char *default_attach
= NULL
;
495 static char **extra_hdr
;
496 static int extra_hdr_nr
;
497 static int extra_hdr_alloc
;
499 static char **extra_to
;
500 static int extra_to_nr
;
501 static int extra_to_alloc
;
503 static char **extra_cc
;
504 static int extra_cc_nr
;
505 static int extra_cc_alloc
;
507 static void add_header(const char *value
)
509 int len
= strlen(value
);
510 while (len
&& value
[len
- 1] == '\n')
512 if (!strncasecmp(value
, "to: ", 4)) {
513 ALLOC_GROW(extra_to
, extra_to_nr
+ 1, extra_to_alloc
);
514 extra_to
[extra_to_nr
++] = xstrndup(value
+ 4, len
- 4);
517 if (!strncasecmp(value
, "cc: ", 4)) {
518 ALLOC_GROW(extra_cc
, extra_cc_nr
+ 1, extra_cc_alloc
);
519 extra_cc
[extra_cc_nr
++] = xstrndup(value
+ 4, len
- 4);
522 ALLOC_GROW(extra_hdr
, extra_hdr_nr
+ 1, extra_hdr_alloc
);
523 extra_hdr
[extra_hdr_nr
++] = xstrndup(value
, len
);
526 #define THREAD_SHALLOW 1
527 #define THREAD_DEEP 2
528 static int thread
= 0;
529 static int do_signoff
= 0;
531 static int git_format_config(const char *var
, const char *value
, void *cb
)
533 if (!strcmp(var
, "format.headers")) {
535 die("format.headers without value");
539 if (!strcmp(var
, "format.suffix"))
540 return git_config_string(&fmt_patch_suffix
, var
, value
);
541 if (!strcmp(var
, "format.cc")) {
543 return config_error_nonbool(var
);
544 ALLOC_GROW(extra_cc
, extra_cc_nr
+ 1, extra_cc_alloc
);
545 extra_cc
[extra_cc_nr
++] = xstrdup(value
);
548 if (!strcmp(var
, "diff.color") || !strcmp(var
, "color.diff")) {
551 if (!strcmp(var
, "format.numbered")) {
552 if (value
&& !strcasecmp(value
, "auto")) {
556 numbered
= git_config_bool(var
, value
);
557 auto_number
= auto_number
&& numbered
;
560 if (!strcmp(var
, "format.attach")) {
562 default_attach
= xstrdup(value
);
564 default_attach
= xstrdup(git_version_string
);
567 if (!strcmp(var
, "format.thread")) {
568 if (value
&& !strcasecmp(value
, "deep")) {
569 thread
= THREAD_DEEP
;
572 if (value
&& !strcasecmp(value
, "shallow")) {
573 thread
= THREAD_SHALLOW
;
576 thread
= git_config_bool(var
, value
) && THREAD_SHALLOW
;
579 if (!strcmp(var
, "format.signoff")) {
580 do_signoff
= git_config_bool(var
, value
);
584 return git_log_config(var
, value
, cb
);
587 static FILE *realstdout
= NULL
;
588 static const char *output_directory
= NULL
;
589 static int outdir_offset
;
591 static int reopen_stdout(struct commit
*commit
, struct rev_info
*rev
)
593 struct strbuf filename
= STRBUF_INIT
;
594 int suffix_len
= strlen(fmt_patch_suffix
) + 1;
596 if (output_directory
) {
597 strbuf_addstr(&filename
, output_directory
);
599 PATH_MAX
- FORMAT_PATCH_NAME_MAX
- suffix_len
)
600 return error("name of output directory is too long");
601 if (filename
.buf
[filename
.len
- 1] != '/')
602 strbuf_addch(&filename
, '/');
605 get_patch_filename(commit
, rev
->nr
, fmt_patch_suffix
, &filename
);
607 if (!DIFF_OPT_TST(&rev
->diffopt
, QUICK
))
608 fprintf(realstdout
, "%s\n", filename
.buf
+ outdir_offset
);
610 if (freopen(filename
.buf
, "w", stdout
) == NULL
)
611 return error("Cannot open patch file %s", filename
.buf
);
613 strbuf_release(&filename
);
617 static void get_patch_ids(struct rev_info
*rev
, struct patch_ids
*ids
, const char *prefix
)
619 struct rev_info check_rev
;
620 struct commit
*commit
;
621 struct object
*o1
, *o2
;
622 unsigned flags1
, flags2
;
624 if (rev
->pending
.nr
!= 2)
625 die("Need exactly one range.");
627 o1
= rev
->pending
.objects
[0].item
;
629 o2
= rev
->pending
.objects
[1].item
;
632 if ((flags1
& UNINTERESTING
) == (flags2
& UNINTERESTING
))
637 /* given a range a..b get all patch ids for b..a */
638 init_revisions(&check_rev
, prefix
);
639 o1
->flags
^= UNINTERESTING
;
640 o2
->flags
^= UNINTERESTING
;
641 add_pending_object(&check_rev
, o1
, "o1");
642 add_pending_object(&check_rev
, o2
, "o2");
643 if (prepare_revision_walk(&check_rev
))
644 die("revision walk setup failed");
646 while ((commit
= get_revision(&check_rev
)) != NULL
) {
648 if (commit
->parents
&& commit
->parents
->next
)
651 add_commit_patch_id(commit
, ids
);
654 /* reset for next revision walk */
655 clear_commit_marks((struct commit
*)o1
,
656 SEEN
| UNINTERESTING
| SHOWN
| ADDED
);
657 clear_commit_marks((struct commit
*)o2
,
658 SEEN
| UNINTERESTING
| SHOWN
| ADDED
);
663 static void gen_message_id(struct rev_info
*info
, char *base
)
665 const char *committer
= git_committer_info(IDENT_WARN_ON_NO_NAME
);
666 const char *email_start
= strrchr(committer
, '<');
667 const char *email_end
= strrchr(committer
, '>');
668 struct strbuf buf
= STRBUF_INIT
;
669 if (!email_start
|| !email_end
|| email_start
> email_end
- 1)
670 die("Could not extract email from committer identity.");
671 strbuf_addf(&buf
, "%s.%lu.git.%.*s", base
,
672 (unsigned long) time(NULL
),
673 (int)(email_end
- email_start
- 1), email_start
+ 1);
674 info
->message_id
= strbuf_detach(&buf
, NULL
);
677 static void make_cover_letter(struct rev_info
*rev
, int use_stdout
,
678 int numbered
, int numbered_files
,
679 struct commit
*origin
,
680 int nr
, struct commit
**list
, struct commit
*head
)
682 const char *committer
;
683 const char *subject_start
= NULL
;
684 const char *body
= "*** SUBJECT HERE ***\n\n*** BLURB HERE ***\n";
686 const char *extra_headers
= rev
->extra_headers
;
688 struct strbuf sb
= STRBUF_INIT
;
690 const char *encoding
= "UTF-8";
691 struct diff_options opts
;
692 int need_8bit_cte
= 0;
693 struct commit
*commit
= NULL
;
695 if (rev
->commit_format
!= CMIT_FMT_EMAIL
)
696 die("Cover letter needs email format");
698 committer
= git_committer_info(0);
700 if (!numbered_files
) {
702 * We fake a commit for the cover letter so we get the filename
705 commit
= xcalloc(1, sizeof(*commit
));
706 commit
->buffer
= xmalloc(400);
707 snprintf(commit
->buffer
, 400,
708 "tree 0000000000000000000000000000000000000000\n"
713 sha1_to_hex(head
->object
.sha1
), committer
, committer
);
716 if (!use_stdout
&& reopen_stdout(commit
, rev
))
721 free(commit
->buffer
);
725 log_write_email_headers(rev
, head
, &subject_start
, &extra_headers
,
728 for (i
= 0; !need_8bit_cte
&& i
< nr
; i
++)
729 if (has_non_ascii(list
[i
]->buffer
))
733 pp_user_info(NULL
, CMIT_FMT_EMAIL
, &sb
, committer
, DATE_RFC2822
,
735 pp_title_line(CMIT_FMT_EMAIL
, &msg
, &sb
, subject_start
, extra_headers
,
736 encoding
, need_8bit_cte
);
737 pp_remainder(CMIT_FMT_EMAIL
, &msg
, &sb
, 0);
738 printf("%s\n", sb
.buf
);
747 for (i
= 0; i
< nr
; i
++)
748 shortlog_add_commit(&log
, list
[i
]);
750 shortlog_output(&log
);
753 * We can only do diffstat with a unique reference point
758 memcpy(&opts
, &rev
->diffopt
, sizeof(opts
));
759 opts
.output_format
= DIFF_FORMAT_SUMMARY
| DIFF_FORMAT_DIFFSTAT
;
761 diff_setup_done(&opts
);
763 diff_tree_sha1(origin
->tree
->object
.sha1
,
764 head
->tree
->object
.sha1
,
772 static const char *clean_message_id(const char *msg_id
)
775 const char *a
, *z
, *m
;
778 while ((ch
= *m
) && (isspace(ch
) || (ch
== '<')))
783 if (!isspace(ch
) && (ch
!= '>'))
788 die("insane in-reply-to: %s", msg_id
);
791 return xmemdupz(a
, z
- a
);
794 static const char *set_outdir(const char *prefix
, const char *output_directory
)
796 if (output_directory
&& is_absolute_path(output_directory
))
797 return output_directory
;
799 if (!prefix
|| !*prefix
) {
800 if (output_directory
)
801 return output_directory
;
802 /* The user did not explicitly ask for "./" */
807 outdir_offset
= strlen(prefix
);
808 if (!output_directory
)
811 return xstrdup(prefix_filename(prefix
, outdir_offset
,
815 static const char * const builtin_format_patch_usage
[] = {
816 "git format-patch [options] [<since> | <revision range>]",
820 static int keep_subject
= 0;
822 static int keep_callback(const struct option
*opt
, const char *arg
, int unset
)
824 ((struct rev_info
*)opt
->value
)->total
= -1;
829 static int subject_prefix
= 0;
831 static int subject_prefix_callback(const struct option
*opt
, const char *arg
,
835 ((struct rev_info
*)opt
->value
)->subject_prefix
= arg
;
839 static int numbered_cmdline_opt
= 0;
841 static int numbered_callback(const struct option
*opt
, const char *arg
,
844 *(int *)opt
->value
= numbered_cmdline_opt
= unset
? 0 : 1;
850 static int no_numbered_callback(const struct option
*opt
, const char *arg
,
853 return numbered_callback(opt
, arg
, 1);
856 static int output_directory_callback(const struct option
*opt
, const char *arg
,
859 const char **dir
= (const char **)opt
->value
;
861 die("Two output directories?");
866 static int thread_callback(const struct option
*opt
, const char *arg
, int unset
)
868 int *thread
= (int *)opt
->value
;
871 else if (!arg
|| !strcmp(arg
, "shallow"))
872 *thread
= THREAD_SHALLOW
;
873 else if (!strcmp(arg
, "deep"))
874 *thread
= THREAD_DEEP
;
880 static int attach_callback(const struct option
*opt
, const char *arg
, int unset
)
882 struct rev_info
*rev
= (struct rev_info
*)opt
->value
;
884 rev
->mime_boundary
= NULL
;
886 rev
->mime_boundary
= arg
;
888 rev
->mime_boundary
= git_version_string
;
889 rev
->no_inline
= unset
? 0 : 1;
893 static int inline_callback(const struct option
*opt
, const char *arg
, int unset
)
895 struct rev_info
*rev
= (struct rev_info
*)opt
->value
;
897 rev
->mime_boundary
= NULL
;
899 rev
->mime_boundary
= arg
;
901 rev
->mime_boundary
= git_version_string
;
906 static int header_callback(const struct option
*opt
, const char *arg
, int unset
)
912 static int cc_callback(const struct option
*opt
, const char *arg
, int unset
)
914 ALLOC_GROW(extra_cc
, extra_cc_nr
+ 1, extra_cc_alloc
);
915 extra_cc
[extra_cc_nr
++] = xstrdup(arg
);
919 int cmd_format_patch(int argc
, const char **argv
, const char *prefix
)
921 struct commit
*commit
;
922 struct commit
**list
= NULL
;
924 int nr
= 0, total
, i
;
926 int start_number
= -1;
927 int numbered_files
= 0; /* _just_ numbers */
928 int ignore_if_in_upstream
= 0;
929 int cover_letter
= 0;
930 int boundary_count
= 0;
931 int no_binary_diff
= 0;
932 struct commit
*origin
= NULL
, *head
= NULL
;
933 const char *in_reply_to
= NULL
;
934 struct patch_ids ids
;
935 char *add_signoff
= NULL
;
936 struct strbuf buf
= STRBUF_INIT
;
937 int use_patch_format
= 0;
938 const struct option builtin_format_patch_options
[] = {
939 { OPTION_CALLBACK
, 'n', "numbered", &numbered
, NULL
,
940 "use [PATCH n/m] even with a single patch",
941 PARSE_OPT_NOARG
, numbered_callback
},
942 { OPTION_CALLBACK
, 'N', "no-numbered", &numbered
, NULL
,
943 "use [PATCH] even with multiple patches",
944 PARSE_OPT_NOARG
, no_numbered_callback
},
945 OPT_BOOLEAN('s', "signoff", &do_signoff
, "add Signed-off-by:"),
946 OPT_BOOLEAN(0, "stdout", &use_stdout
,
947 "print patches to standard out"),
948 OPT_BOOLEAN(0, "cover-letter", &cover_letter
,
949 "generate a cover letter"),
950 OPT_BOOLEAN(0, "numbered-files", &numbered_files
,
951 "use simple number sequence for output file names"),
952 OPT_STRING(0, "suffix", &fmt_patch_suffix
, "sfx",
953 "use <sfx> instead of '.patch'"),
954 OPT_INTEGER(0, "start-number", &start_number
,
955 "start numbering patches at <n> instead of 1"),
956 { OPTION_CALLBACK
, 0, "subject-prefix", &rev
, "prefix",
957 "Use [<prefix>] instead of [PATCH]",
958 PARSE_OPT_NONEG
, subject_prefix_callback
},
959 { OPTION_CALLBACK
, 'o', "output-directory", &output_directory
,
960 "dir", "store resulting files in <dir>",
961 PARSE_OPT_NONEG
, output_directory_callback
},
962 { OPTION_CALLBACK
, 'k', "keep-subject", &rev
, NULL
,
963 "don't strip/add [PATCH]",
964 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
, keep_callback
},
965 OPT_BOOLEAN(0, "no-binary", &no_binary_diff
,
966 "don't output binary diffs"),
967 OPT_BOOLEAN(0, "ignore-if-in-upstream", &ignore_if_in_upstream
,
968 "don't include a patch matching a commit upstream"),
969 { OPTION_BOOLEAN
, 'p', "no-stat", &use_patch_format
, NULL
,
970 "show patch format instead of default (patch + stat)",
971 PARSE_OPT_NONEG
| PARSE_OPT_NOARG
},
972 OPT_GROUP("Messaging"),
973 { OPTION_CALLBACK
, 0, "add-header", NULL
, "header",
974 "add email header", PARSE_OPT_NONEG
,
976 { OPTION_CALLBACK
, 0, "cc", NULL
, "email", "add Cc: header",
977 PARSE_OPT_NONEG
, cc_callback
},
978 OPT_STRING(0, "in-reply-to", &in_reply_to
, "message-id",
979 "make first mail a reply to <message-id>"),
980 { OPTION_CALLBACK
, 0, "attach", &rev
, "boundary",
981 "attach the patch", PARSE_OPT_OPTARG
,
983 { OPTION_CALLBACK
, 0, "inline", &rev
, "boundary",
985 PARSE_OPT_OPTARG
| PARSE_OPT_NONEG
,
987 { OPTION_CALLBACK
, 0, "thread", &thread
, "style",
988 "enable message threading, styles: shallow, deep",
989 PARSE_OPT_OPTARG
, thread_callback
},
993 git_config(git_format_config
, NULL
);
994 init_revisions(&rev
, prefix
);
995 rev
.commit_format
= CMIT_FMT_EMAIL
;
996 rev
.verbose_header
= 1;
998 rev
.combine_merges
= 0;
999 rev
.ignore_merges
= 1;
1000 DIFF_OPT_SET(&rev
.diffopt
, RECURSIVE
);
1002 rev
.subject_prefix
= fmt_patch_subject_prefix
;
1004 if (default_attach
) {
1005 rev
.mime_boundary
= default_attach
;
1010 * Parse the arguments before setup_revisions(), or something
1011 * like "git format-patch -o a123 HEAD^.." may fail; a123 is
1012 * possibly a valid SHA1.
1014 argc
= parse_options(argc
, argv
, prefix
, builtin_format_patch_options
,
1015 builtin_format_patch_usage
,
1016 PARSE_OPT_KEEP_ARGV0
| PARSE_OPT_KEEP_UNKNOWN
|
1017 PARSE_OPT_KEEP_DASHDASH
);
1020 const char *committer
;
1022 committer
= git_committer_info(IDENT_ERROR_ON_NO_NAME
);
1023 endpos
= strchr(committer
, '>');
1025 die("bogus committer info %s", committer
);
1026 add_signoff
= xmemdupz(committer
, endpos
- committer
+ 1);
1029 for (i
= 0; i
< extra_hdr_nr
; i
++) {
1030 strbuf_addstr(&buf
, extra_hdr
[i
]);
1031 strbuf_addch(&buf
, '\n');
1035 strbuf_addstr(&buf
, "To: ");
1036 for (i
= 0; i
< extra_to_nr
; i
++) {
1038 strbuf_addstr(&buf
, " ");
1039 strbuf_addstr(&buf
, extra_to
[i
]);
1040 if (i
+ 1 < extra_to_nr
)
1041 strbuf_addch(&buf
, ',');
1042 strbuf_addch(&buf
, '\n');
1046 strbuf_addstr(&buf
, "Cc: ");
1047 for (i
= 0; i
< extra_cc_nr
; i
++) {
1049 strbuf_addstr(&buf
, " ");
1050 strbuf_addstr(&buf
, extra_cc
[i
]);
1051 if (i
+ 1 < extra_cc_nr
)
1052 strbuf_addch(&buf
, ',');
1053 strbuf_addch(&buf
, '\n');
1056 rev
.extra_headers
= strbuf_detach(&buf
, NULL
);
1058 if (start_number
< 0)
1062 * If numbered is set solely due to format.numbered in config,
1063 * and it would conflict with --keep-subject (-k) from the
1064 * command line, reset "numbered".
1066 if (numbered
&& keep_subject
&& !numbered_cmdline_opt
)
1069 if (numbered
&& keep_subject
)
1070 die ("-n and -k are mutually exclusive.");
1071 if (keep_subject
&& subject_prefix
)
1072 die ("--subject-prefix and -k are mutually exclusive.");
1074 argc
= setup_revisions(argc
, argv
, &rev
, "HEAD");
1076 die ("unrecognized argument: %s", argv
[1]);
1078 if (rev
.diffopt
.output_format
& DIFF_FORMAT_NAME
)
1079 die("--name-only does not make sense");
1080 if (rev
.diffopt
.output_format
& DIFF_FORMAT_NAME_STATUS
)
1081 die("--name-status does not make sense");
1082 if (rev
.diffopt
.output_format
& DIFF_FORMAT_CHECKDIFF
)
1083 die("--check does not make sense");
1085 if (!use_patch_format
&&
1086 (!rev
.diffopt
.output_format
||
1087 rev
.diffopt
.output_format
== DIFF_FORMAT_PATCH
))
1088 rev
.diffopt
.output_format
= DIFF_FORMAT_DIFFSTAT
| DIFF_FORMAT_SUMMARY
;
1090 /* Always generate a patch */
1091 rev
.diffopt
.output_format
|= DIFF_FORMAT_PATCH
;
1093 if (!DIFF_OPT_TST(&rev
.diffopt
, TEXT
) && !no_binary_diff
)
1094 DIFF_OPT_SET(&rev
.diffopt
, BINARY
);
1097 output_directory
= set_outdir(prefix
, output_directory
);
1099 if (output_directory
) {
1101 die("standard output, or directory, which one?");
1102 if (mkdir(output_directory
, 0777) < 0 && errno
!= EEXIST
)
1103 die_errno("Could not create directory '%s'",
1107 if (rev
.pending
.nr
== 1) {
1108 if (rev
.max_count
< 0 && !rev
.show_root_diff
) {
1110 * This is traditional behaviour of "git format-patch
1111 * origin" that prepares what the origin side still
1114 rev
.pending
.objects
[0].item
->flags
|= UNINTERESTING
;
1115 add_head_to_pending(&rev
);
1118 * Otherwise, it is "format-patch -22 HEAD", and/or
1119 * "format-patch --root HEAD". The user wants
1120 * get_revision() to do the usual traversal.
1125 * We cannot move this anywhere earlier because we do want to
1126 * know if --root was given explicitly from the comand line.
1128 rev
.show_root_diff
= 1;
1131 /* remember the range */
1133 for (i
= 0; i
< rev
.pending
.nr
; i
++) {
1134 struct object
*o
= rev
.pending
.objects
[i
].item
;
1135 if (!(o
->flags
& UNINTERESTING
))
1136 head
= (struct commit
*)o
;
1138 /* We can't generate a cover letter without any patches */
1143 if (ignore_if_in_upstream
)
1144 get_patch_ids(&rev
, &ids
, prefix
);
1147 realstdout
= xfdopen(xdup(1), "w");
1149 if (prepare_revision_walk(&rev
))
1150 die("revision walk setup failed");
1152 while ((commit
= get_revision(&rev
)) != NULL
) {
1153 if (commit
->object
.flags
& BOUNDARY
) {
1155 origin
= (boundary_count
== 1) ? commit
: NULL
;
1160 if (commit
->parents
&& commit
->parents
->next
)
1163 if (ignore_if_in_upstream
&&
1164 has_commit_patch_id(commit
, &ids
))
1168 list
= xrealloc(list
, nr
* sizeof(list
[0]));
1169 list
[nr
- 1] = commit
;
1172 if (!keep_subject
&& auto_number
&& total
> 1)
1175 rev
.total
= total
+ start_number
- 1;
1176 if (in_reply_to
|| thread
|| cover_letter
)
1177 rev
.ref_message_ids
= xcalloc(1, sizeof(struct string_list
));
1179 const char *msgid
= clean_message_id(in_reply_to
);
1180 string_list_append(msgid
, rev
.ref_message_ids
);
1182 rev
.numbered_files
= numbered_files
;
1183 rev
.patch_suffix
= fmt_patch_suffix
;
1186 gen_message_id(&rev
, "cover");
1187 make_cover_letter(&rev
, use_stdout
, numbered
, numbered_files
,
1188 origin
, nr
, list
, head
);
1192 rev
.add_signoff
= add_signoff
;
1196 rev
.nr
= total
- nr
+ (start_number
- 1);
1197 /* Make the second and subsequent mails replies to the first */
1199 /* Have we already had a message ID? */
1200 if (rev
.message_id
) {
1202 * For deep threading: make every mail
1203 * a reply to the previous one, no
1204 * matter what other options are set.
1206 * For shallow threading:
1208 * Without --cover-letter and
1209 * --in-reply-to, make every mail a
1210 * reply to the one before.
1212 * With --in-reply-to but no
1213 * --cover-letter, make every mail a
1214 * reply to the <reply-to>.
1216 * With --cover-letter, make every
1217 * mail but the cover letter a reply
1218 * to the cover letter. The cover
1219 * letter is a reply to the
1220 * --in-reply-to, if specified.
1222 if (thread
== THREAD_SHALLOW
1223 && rev
.ref_message_ids
->nr
> 0
1224 && (!cover_letter
|| rev
.nr
> 1))
1225 free(rev
.message_id
);
1227 string_list_append(rev
.message_id
,
1228 rev
.ref_message_ids
);
1230 gen_message_id(&rev
, sha1_to_hex(commit
->object
.sha1
));
1233 if (!use_stdout
&& reopen_stdout(numbered_files
? NULL
: commit
,
1235 die("Failed to create output files");
1236 shown
= log_tree_commit(&rev
, commit
);
1237 free(commit
->buffer
);
1238 commit
->buffer
= NULL
;
1240 /* We put one extra blank line between formatted
1241 * patches and this flag is used by log-tree code
1242 * to see if it needs to emit a LF before showing
1243 * the log; when using one file per patch, we do
1244 * not want the extra blank line.
1249 if (rev
.mime_boundary
)
1250 printf("\n--%s%s--\n\n\n",
1251 mime_boundary_leader
,
1254 printf("-- \n%s\n\n", git_version_string
);
1260 if (ignore_if_in_upstream
)
1261 free_patch_ids(&ids
);
1265 static int add_pending_commit(const char *arg
, struct rev_info
*revs
, int flags
)
1267 unsigned char sha1
[20];
1268 if (get_sha1(arg
, sha1
) == 0) {
1269 struct commit
*commit
= lookup_commit_reference(sha1
);
1271 commit
->object
.flags
|= flags
;
1272 add_pending_object(revs
, &commit
->object
, arg
);
1279 static const char cherry_usage
[] =
1280 "git cherry [-v] [<upstream> [<head> [<limit>]]]";
1281 int cmd_cherry(int argc
, const char **argv
, const char *prefix
)
1283 struct rev_info revs
;
1284 struct patch_ids ids
;
1285 struct commit
*commit
;
1286 struct commit_list
*list
= NULL
;
1287 struct branch
*current_branch
;
1288 const char *upstream
;
1289 const char *head
= "HEAD";
1290 const char *limit
= NULL
;
1293 if (argc
> 1 && !strcmp(argv
[1], "-v")) {
1299 if (argc
> 1 && !strcmp(argv
[1], "-h"))
1300 usage(cherry_usage
);
1313 current_branch
= branch_get(NULL
);
1314 if (!current_branch
|| !current_branch
->merge
1315 || !current_branch
->merge
[0]
1316 || !current_branch
->merge
[0]->dst
) {
1317 fprintf(stderr
, "Could not find a tracked"
1318 " remote branch, please"
1319 " specify <upstream> manually.\n");
1320 usage(cherry_usage
);
1323 upstream
= current_branch
->merge
[0]->dst
;
1326 init_revisions(&revs
, prefix
);
1328 revs
.combine_merges
= 0;
1329 revs
.ignore_merges
= 1;
1330 DIFF_OPT_SET(&revs
.diffopt
, RECURSIVE
);
1332 if (add_pending_commit(head
, &revs
, 0))
1333 die("Unknown commit %s", head
);
1334 if (add_pending_commit(upstream
, &revs
, UNINTERESTING
))
1335 die("Unknown commit %s", upstream
);
1337 /* Don't say anything if head and upstream are the same. */
1338 if (revs
.pending
.nr
== 2) {
1339 struct object_array_entry
*o
= revs
.pending
.objects
;
1340 if (hashcmp(o
[0].item
->sha1
, o
[1].item
->sha1
) == 0)
1344 get_patch_ids(&revs
, &ids
, prefix
);
1346 if (limit
&& add_pending_commit(limit
, &revs
, UNINTERESTING
))
1347 die("Unknown commit %s", limit
);
1349 /* reverse the list of commits */
1350 if (prepare_revision_walk(&revs
))
1351 die("revision walk setup failed");
1352 while ((commit
= get_revision(&revs
)) != NULL
) {
1354 if (commit
->parents
&& commit
->parents
->next
)
1357 commit_list_insert(commit
, &list
);
1363 commit
= list
->item
;
1364 if (has_commit_patch_id(commit
, &ids
))
1368 struct strbuf buf
= STRBUF_INIT
;
1369 struct pretty_print_context ctx
= {0};
1370 pretty_print_commit(CMIT_FMT_ONELINE
, commit
,
1372 printf("%c %s %s\n", sign
,
1373 sha1_to_hex(commit
->object
.sha1
), buf
.buf
);
1374 strbuf_release(&buf
);
1377 printf("%c %s\n", sign
,
1378 sha1_to_hex(commit
->object
.sha1
));
1384 free_patch_ids(&ids
);