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;
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
, opt
);
79 if (!rev
->show_notes_given
&& !rev
->pretty_given
)
82 init_display_notes(&rev
->notes_opt
);
84 if (rev
->diffopt
.pickaxe
|| rev
->diffopt
.filter
)
85 rev
->always_show_header
= 0;
86 if (DIFF_OPT_TST(&rev
->diffopt
, FOLLOW_RENAMES
)) {
87 rev
->always_show_header
= 0;
88 if (rev
->diffopt
.nr_paths
!= 1)
89 usage("git logs can only follow renames on one pathname at a time");
91 for (i
= 1; i
< argc
; i
++) {
92 const char *arg
= argv
[i
];
93 if (!strcmp(arg
, "--decorate")) {
94 decoration_style
= DECORATE_SHORT_REFS
;
96 } else if (!prefixcmp(arg
, "--decorate=")) {
97 const char *v
= skip_prefix(arg
, "--decorate=");
98 decoration_style
= parse_decoration_style(arg
, v
);
99 if (decoration_style
< 0)
100 die("invalid --decorate option: %s", arg
);
101 decoration_given
= 1;
102 } else if (!strcmp(arg
, "--no-decorate")) {
103 decoration_style
= 0;
104 } else if (!strcmp(arg
, "--source")) {
105 rev
->show_source
= 1;
106 } else if (!strcmp(arg
, "-h")) {
107 usage(builtin_log_usage
);
109 die("unrecognized argument: %s", arg
);
113 * defeat log.decorate configuration interacting with --pretty
114 * from the command line.
116 if (!decoration_given
&& rev
->pretty_given
)
117 decoration_style
= 0;
119 if (decoration_style
) {
120 rev
->show_decorations
= 1;
121 load_ref_decorations(decoration_style
);
126 * This gives a rough estimate for how many commits we
127 * will print out in the list.
129 static int estimate_commit_count(struct rev_info
*rev
, struct commit_list
*list
)
134 struct commit
*commit
= list
->item
;
135 unsigned int flags
= commit
->object
.flags
;
137 if (!(flags
& (TREESAME
| UNINTERESTING
)))
143 static void show_early_header(struct rev_info
*rev
, const char *stage
, int nr
)
145 if (rev
->shown_one
) {
147 if (rev
->commit_format
!= CMIT_FMT_ONELINE
)
148 putchar(rev
->diffopt
.line_termination
);
150 printf("Final output: %d %s\n", nr
, stage
);
153 static struct itimerval early_output_timer
;
155 static void log_show_early(struct rev_info
*revs
, struct commit_list
*list
)
157 int i
= revs
->early_output
;
160 sort_in_topological_order(&list
, revs
->lifo
);
162 struct commit
*commit
= list
->item
;
163 switch (simplify_commit(revs
, commit
)) {
166 int n
= estimate_commit_count(revs
, list
);
167 show_early_header(revs
, "incomplete", n
);
170 log_tree_commit(revs
, commit
);
181 /* Did we already get enough commits for the early output? */
186 * ..if no, then repeat it twice a second until we
189 * NOTE! We don't use "it_interval", because if the
190 * reader isn't listening, we want our output to be
191 * throttled by the writing, and not have the timer
192 * trigger every second even if we're blocked on a
195 early_output_timer
.it_value
.tv_sec
= 0;
196 early_output_timer
.it_value
.tv_usec
= 500000;
197 setitimer(ITIMER_REAL
, &early_output_timer
, NULL
);
200 static void early_output(int signal
)
202 show_early_output
= log_show_early
;
205 static void setup_early_output(struct rev_info
*rev
)
210 * Set up the signal handler, minimally intrusively:
211 * we only set a single volatile integer word (not
212 * using sigatomic_t - trying to avoid unnecessary
213 * system dependencies and headers), and using
216 memset(&sa
, 0, sizeof(sa
));
217 sa
.sa_handler
= early_output
;
218 sigemptyset(&sa
.sa_mask
);
219 sa
.sa_flags
= SA_RESTART
;
220 sigaction(SIGALRM
, &sa
, NULL
);
223 * If we can get the whole output in less than a
224 * tenth of a second, don't even bother doing the
225 * early-output thing..
227 * This is a one-time-only trigger.
229 early_output_timer
.it_value
.tv_sec
= 0;
230 early_output_timer
.it_value
.tv_usec
= 100000;
231 setitimer(ITIMER_REAL
, &early_output_timer
, NULL
);
234 static void finish_early_output(struct rev_info
*rev
)
236 int n
= estimate_commit_count(rev
, rev
->commits
);
237 signal(SIGALRM
, SIG_IGN
);
238 show_early_header(rev
, "done", n
);
241 static int cmd_log_walk(struct rev_info
*rev
)
243 struct commit
*commit
;
245 if (rev
->early_output
)
246 setup_early_output(rev
);
248 if (prepare_revision_walk(rev
))
249 die("revision walk setup failed");
251 if (rev
->early_output
)
252 finish_early_output(rev
);
255 * For --check and --exit-code, the exit code is based on CHECK_FAILED
256 * and HAS_CHANGES being accumulated in rev->diffopt, so be careful to
257 * retain that state information if replacing rev->diffopt in this loop
259 while ((commit
= get_revision(rev
)) != NULL
) {
260 log_tree_commit(rev
, commit
);
261 if (!rev
->reflog_info
) {
262 /* we allow cycles in reflog ancestry */
263 free(commit
->buffer
);
264 commit
->buffer
= NULL
;
266 free_commit_list(commit
->parents
);
267 commit
->parents
= NULL
;
269 if (rev
->diffopt
.output_format
& DIFF_FORMAT_CHECKDIFF
&&
270 DIFF_OPT_TST(&rev
->diffopt
, CHECK_FAILED
)) {
273 return diff_result_code(&rev
->diffopt
, 0);
276 static int git_log_config(const char *var
, const char *value
, void *cb
)
278 if (!strcmp(var
, "format.pretty"))
279 return git_config_string(&fmt_pretty
, var
, value
);
280 if (!strcmp(var
, "format.subjectprefix"))
281 return git_config_string(&fmt_patch_subject_prefix
, var
, value
);
282 if (!strcmp(var
, "log.date"))
283 return git_config_string(&default_date_mode
, var
, value
);
284 if (!strcmp(var
, "log.decorate")) {
285 decoration_style
= parse_decoration_style(var
, value
);
286 if (decoration_style
< 0)
287 decoration_style
= 0; /* maybe warn? */
290 if (!strcmp(var
, "log.showroot")) {
291 default_show_root
= git_config_bool(var
, value
);
294 return git_diff_ui_config(var
, value
, cb
);
297 int cmd_whatchanged(int argc
, const char **argv
, const char *prefix
)
300 struct setup_revision_opt opt
;
302 git_config(git_log_config
, NULL
);
304 if (diff_use_color_default
== -1)
305 diff_use_color_default
= git_use_color_default
;
307 init_revisions(&rev
, prefix
);
309 rev
.simplify_history
= 0;
310 memset(&opt
, 0, sizeof(opt
));
312 cmd_log_init(argc
, argv
, prefix
, &rev
, &opt
);
313 if (!rev
.diffopt
.output_format
)
314 rev
.diffopt
.output_format
= DIFF_FORMAT_RAW
;
315 return cmd_log_walk(&rev
);
318 static void show_tagger(char *buf
, int len
, struct rev_info
*rev
)
320 struct strbuf out
= STRBUF_INIT
;
322 pp_user_info("Tagger", rev
->commit_format
, &out
, buf
, rev
->date_mode
,
323 git_log_output_encoding
?
324 git_log_output_encoding
: git_commit_encoding
);
325 printf("%s", out
.buf
);
326 strbuf_release(&out
);
329 static int show_object(const unsigned char *sha1
, int show_tag_object
,
330 struct rev_info
*rev
)
333 enum object_type type
;
334 char *buf
= read_sha1_file(sha1
, &type
, &size
);
338 return error("Could not read object %s", sha1_to_hex(sha1
));
341 while (offset
< size
&& buf
[offset
] != '\n') {
342 int new_offset
= offset
+ 1;
343 while (new_offset
< size
&& buf
[new_offset
++] != '\n')
345 if (!prefixcmp(buf
+ offset
, "tagger "))
346 show_tagger(buf
+ offset
+ 7,
347 new_offset
- offset
- 7, rev
);
352 fwrite(buf
+ offset
, size
- offset
, 1, stdout
);
357 static int show_tree_object(const unsigned char *sha1
,
358 const char *base
, int baselen
,
359 const char *pathname
, unsigned mode
, int stage
, void *context
)
361 printf("%s%s\n", pathname
, S_ISDIR(mode
) ? "/" : "");
365 static void show_rev_tweak_rev(struct rev_info
*rev
, struct setup_revision_opt
*opt
)
367 if (rev
->ignore_merges
) {
368 /* There was no "-m" on the command line */
369 rev
->ignore_merges
= 0;
370 if (!rev
->first_parent_only
&& !rev
->combine_merges
) {
371 /* No "--first-parent", "-c", nor "--cc" */
372 rev
->combine_merges
= 1;
373 rev
->dense_combined_merges
= 1;
376 if (!rev
->diffopt
.output_format
)
377 rev
->diffopt
.output_format
= DIFF_FORMAT_PATCH
;
380 int cmd_show(int argc
, const char **argv
, const char *prefix
)
383 struct object_array_entry
*objects
;
384 struct setup_revision_opt opt
;
385 int i
, count
, ret
= 0;
387 git_config(git_log_config
, NULL
);
389 if (diff_use_color_default
== -1)
390 diff_use_color_default
= git_use_color_default
;
392 init_revisions(&rev
, prefix
);
394 rev
.always_show_header
= 1;
396 memset(&opt
, 0, sizeof(opt
));
398 opt
.tweak
= show_rev_tweak_rev
;
399 cmd_log_init(argc
, argv
, prefix
, &rev
, &opt
);
401 count
= rev
.pending
.nr
;
402 objects
= rev
.pending
.objects
;
403 for (i
= 0; i
< count
&& !ret
; i
++) {
404 struct object
*o
= objects
[i
].item
;
405 const char *name
= objects
[i
].name
;
408 ret
= show_object(o
->sha1
, 0, NULL
);
411 struct tag
*t
= (struct tag
*)o
;
415 printf("%stag %s%s\n",
416 diff_get_color_opt(&rev
.diffopt
, DIFF_COMMIT
),
418 diff_get_color_opt(&rev
.diffopt
, DIFF_RESET
));
419 ret
= show_object(o
->sha1
, 1, &rev
);
423 o
= parse_object(t
->tagged
->sha1
);
425 ret
= error("Could not read object %s",
426 sha1_to_hex(t
->tagged
->sha1
));
434 printf("%stree %s%s\n\n",
435 diff_get_color_opt(&rev
.diffopt
, DIFF_COMMIT
),
437 diff_get_color_opt(&rev
.diffopt
, DIFF_RESET
));
438 read_tree_recursive((struct tree
*)o
, "", 0, 0, NULL
,
439 show_tree_object
, NULL
);
443 rev
.pending
.nr
= rev
.pending
.alloc
= 0;
444 rev
.pending
.objects
= NULL
;
445 add_object_array(o
, name
, &rev
.pending
);
446 ret
= cmd_log_walk(&rev
);
449 ret
= error("Unknown type: %d", o
->type
);
457 * This is equivalent to "git log -g --abbrev-commit --pretty=oneline"
459 int cmd_log_reflog(int argc
, const char **argv
, const char *prefix
)
462 struct setup_revision_opt opt
;
464 git_config(git_log_config
, NULL
);
466 if (diff_use_color_default
== -1)
467 diff_use_color_default
= git_use_color_default
;
469 init_revisions(&rev
, prefix
);
470 init_reflog_walk(&rev
.reflog_info
);
471 rev
.abbrev_commit
= 1;
472 rev
.verbose_header
= 1;
473 memset(&opt
, 0, sizeof(opt
));
475 cmd_log_init(argc
, argv
, prefix
, &rev
, &opt
);
478 * This means that we override whatever commit format the user gave
479 * on the cmd line. Sad, but cmd_log_init() currently doesn't
480 * allow us to set a different default.
482 rev
.commit_format
= CMIT_FMT_ONELINE
;
483 rev
.use_terminator
= 1;
484 rev
.always_show_header
= 1;
487 * We get called through "git reflog", so unlike the other log
488 * routines, we need to set up our pager manually..
492 return cmd_log_walk(&rev
);
495 int cmd_log(int argc
, const char **argv
, const char *prefix
)
498 struct setup_revision_opt opt
;
500 git_config(git_log_config
, NULL
);
502 if (diff_use_color_default
== -1)
503 diff_use_color_default
= git_use_color_default
;
505 init_revisions(&rev
, prefix
);
506 rev
.always_show_header
= 1;
507 memset(&opt
, 0, sizeof(opt
));
509 cmd_log_init(argc
, argv
, prefix
, &rev
, &opt
);
510 return cmd_log_walk(&rev
);
515 static const char *fmt_patch_suffix
= ".patch";
516 static int numbered
= 0;
517 static int auto_number
= 1;
519 static char *default_attach
= NULL
;
521 static struct string_list extra_hdr
;
522 static struct string_list extra_to
;
523 static struct string_list extra_cc
;
525 static void add_header(const char *value
)
527 struct string_list_item
*item
;
528 int len
= strlen(value
);
529 while (len
&& value
[len
- 1] == '\n')
532 if (!strncasecmp(value
, "to: ", 4)) {
533 item
= string_list_append(value
+ 4, &extra_to
);
535 } else if (!strncasecmp(value
, "cc: ", 4)) {
536 item
= string_list_append(value
+ 4, &extra_cc
);
539 item
= string_list_append(value
, &extra_hdr
);
542 item
->string
[len
] = '\0';
545 #define THREAD_SHALLOW 1
546 #define THREAD_DEEP 2
547 static int thread
= 0;
548 static int do_signoff
= 0;
550 static int git_format_config(const char *var
, const char *value
, void *cb
)
552 if (!strcmp(var
, "format.headers")) {
554 die("format.headers without value");
558 if (!strcmp(var
, "format.suffix"))
559 return git_config_string(&fmt_patch_suffix
, var
, value
);
560 if (!strcmp(var
, "format.to")) {
562 return config_error_nonbool(var
);
563 string_list_append(value
, &extra_to
);
566 if (!strcmp(var
, "format.cc")) {
568 return config_error_nonbool(var
);
569 string_list_append(value
, &extra_cc
);
572 if (!strcmp(var
, "diff.color") || !strcmp(var
, "color.diff")) {
575 if (!strcmp(var
, "format.numbered")) {
576 if (value
&& !strcasecmp(value
, "auto")) {
580 numbered
= git_config_bool(var
, value
);
581 auto_number
= auto_number
&& numbered
;
584 if (!strcmp(var
, "format.attach")) {
586 default_attach
= xstrdup(value
);
588 default_attach
= xstrdup(git_version_string
);
591 if (!strcmp(var
, "format.thread")) {
592 if (value
&& !strcasecmp(value
, "deep")) {
593 thread
= THREAD_DEEP
;
596 if (value
&& !strcasecmp(value
, "shallow")) {
597 thread
= THREAD_SHALLOW
;
600 thread
= git_config_bool(var
, value
) && THREAD_SHALLOW
;
603 if (!strcmp(var
, "format.signoff")) {
604 do_signoff
= git_config_bool(var
, value
);
608 return git_log_config(var
, value
, cb
);
611 static FILE *realstdout
= NULL
;
612 static const char *output_directory
= NULL
;
613 static int outdir_offset
;
615 static int reopen_stdout(struct commit
*commit
, struct rev_info
*rev
)
617 struct strbuf filename
= STRBUF_INIT
;
618 int suffix_len
= strlen(fmt_patch_suffix
) + 1;
620 if (output_directory
) {
621 strbuf_addstr(&filename
, output_directory
);
623 PATH_MAX
- FORMAT_PATCH_NAME_MAX
- suffix_len
)
624 return error("name of output directory is too long");
625 if (filename
.buf
[filename
.len
- 1] != '/')
626 strbuf_addch(&filename
, '/');
629 get_patch_filename(commit
, rev
->nr
, fmt_patch_suffix
, &filename
);
631 if (!DIFF_OPT_TST(&rev
->diffopt
, QUICK
))
632 fprintf(realstdout
, "%s\n", filename
.buf
+ outdir_offset
);
634 if (freopen(filename
.buf
, "w", stdout
) == NULL
)
635 return error("Cannot open patch file %s", filename
.buf
);
637 strbuf_release(&filename
);
641 static void get_patch_ids(struct rev_info
*rev
, struct patch_ids
*ids
, const char *prefix
)
643 struct rev_info check_rev
;
644 struct commit
*commit
;
645 struct object
*o1
, *o2
;
646 unsigned flags1
, flags2
;
648 if (rev
->pending
.nr
!= 2)
649 die("Need exactly one range.");
651 o1
= rev
->pending
.objects
[0].item
;
653 o2
= rev
->pending
.objects
[1].item
;
656 if ((flags1
& UNINTERESTING
) == (flags2
& UNINTERESTING
))
661 /* given a range a..b get all patch ids for b..a */
662 init_revisions(&check_rev
, prefix
);
663 o1
->flags
^= UNINTERESTING
;
664 o2
->flags
^= UNINTERESTING
;
665 add_pending_object(&check_rev
, o1
, "o1");
666 add_pending_object(&check_rev
, o2
, "o2");
667 if (prepare_revision_walk(&check_rev
))
668 die("revision walk setup failed");
670 while ((commit
= get_revision(&check_rev
)) != NULL
) {
672 if (commit
->parents
&& commit
->parents
->next
)
675 add_commit_patch_id(commit
, ids
);
678 /* reset for next revision walk */
679 clear_commit_marks((struct commit
*)o1
,
680 SEEN
| UNINTERESTING
| SHOWN
| ADDED
);
681 clear_commit_marks((struct commit
*)o2
,
682 SEEN
| UNINTERESTING
| SHOWN
| ADDED
);
687 static void gen_message_id(struct rev_info
*info
, char *base
)
689 const char *committer
= git_committer_info(IDENT_WARN_ON_NO_NAME
);
690 const char *email_start
= strrchr(committer
, '<');
691 const char *email_end
= strrchr(committer
, '>');
692 struct strbuf buf
= STRBUF_INIT
;
693 if (!email_start
|| !email_end
|| email_start
> email_end
- 1)
694 die("Could not extract email from committer identity.");
695 strbuf_addf(&buf
, "%s.%lu.git.%.*s", base
,
696 (unsigned long) time(NULL
),
697 (int)(email_end
- email_start
- 1), email_start
+ 1);
698 info
->message_id
= strbuf_detach(&buf
, NULL
);
701 static void make_cover_letter(struct rev_info
*rev
, int use_stdout
,
702 int numbered
, int numbered_files
,
703 struct commit
*origin
,
704 int nr
, struct commit
**list
, struct commit
*head
)
706 const char *committer
;
707 const char *subject_start
= NULL
;
708 const char *body
= "*** SUBJECT HERE ***\n\n*** BLURB HERE ***\n";
710 const char *extra_headers
= rev
->extra_headers
;
712 struct strbuf sb
= STRBUF_INIT
;
714 const char *encoding
= "UTF-8";
715 struct diff_options opts
;
716 int need_8bit_cte
= 0;
717 struct commit
*commit
= NULL
;
719 if (rev
->commit_format
!= CMIT_FMT_EMAIL
)
720 die("Cover letter needs email format");
722 committer
= git_committer_info(0);
724 if (!numbered_files
) {
726 * We fake a commit for the cover letter so we get the filename
729 commit
= xcalloc(1, sizeof(*commit
));
730 commit
->buffer
= xmalloc(400);
731 snprintf(commit
->buffer
, 400,
732 "tree 0000000000000000000000000000000000000000\n"
737 sha1_to_hex(head
->object
.sha1
), committer
, committer
);
740 if (!use_stdout
&& reopen_stdout(commit
, rev
))
745 free(commit
->buffer
);
749 log_write_email_headers(rev
, head
, &subject_start
, &extra_headers
,
752 for (i
= 0; !need_8bit_cte
&& i
< nr
; i
++)
753 if (has_non_ascii(list
[i
]->buffer
))
757 pp_user_info(NULL
, CMIT_FMT_EMAIL
, &sb
, committer
, DATE_RFC2822
,
759 pp_title_line(CMIT_FMT_EMAIL
, &msg
, &sb
, subject_start
, extra_headers
,
760 encoding
, need_8bit_cte
);
761 pp_remainder(CMIT_FMT_EMAIL
, &msg
, &sb
, 0);
762 printf("%s\n", sb
.buf
);
771 for (i
= 0; i
< nr
; i
++)
772 shortlog_add_commit(&log
, list
[i
]);
774 shortlog_output(&log
);
777 * We can only do diffstat with a unique reference point
782 memcpy(&opts
, &rev
->diffopt
, sizeof(opts
));
783 opts
.output_format
= DIFF_FORMAT_SUMMARY
| DIFF_FORMAT_DIFFSTAT
;
785 diff_setup_done(&opts
);
787 diff_tree_sha1(origin
->tree
->object
.sha1
,
788 head
->tree
->object
.sha1
,
796 static const char *clean_message_id(const char *msg_id
)
799 const char *a
, *z
, *m
;
802 while ((ch
= *m
) && (isspace(ch
) || (ch
== '<')))
807 if (!isspace(ch
) && (ch
!= '>'))
812 die("insane in-reply-to: %s", msg_id
);
815 return xmemdupz(a
, z
- a
);
818 static const char *set_outdir(const char *prefix
, const char *output_directory
)
820 if (output_directory
&& is_absolute_path(output_directory
))
821 return output_directory
;
823 if (!prefix
|| !*prefix
) {
824 if (output_directory
)
825 return output_directory
;
826 /* The user did not explicitly ask for "./" */
831 outdir_offset
= strlen(prefix
);
832 if (!output_directory
)
835 return xstrdup(prefix_filename(prefix
, outdir_offset
,
839 static const char * const builtin_format_patch_usage
[] = {
840 "git format-patch [options] [<since> | <revision range>]",
844 static int keep_subject
= 0;
846 static int keep_callback(const struct option
*opt
, const char *arg
, int unset
)
848 ((struct rev_info
*)opt
->value
)->total
= -1;
853 static int subject_prefix
= 0;
855 static int subject_prefix_callback(const struct option
*opt
, const char *arg
,
859 ((struct rev_info
*)opt
->value
)->subject_prefix
= arg
;
863 static int numbered_cmdline_opt
= 0;
865 static int numbered_callback(const struct option
*opt
, const char *arg
,
868 *(int *)opt
->value
= numbered_cmdline_opt
= unset
? 0 : 1;
874 static int no_numbered_callback(const struct option
*opt
, const char *arg
,
877 return numbered_callback(opt
, arg
, 1);
880 static int output_directory_callback(const struct option
*opt
, const char *arg
,
883 const char **dir
= (const char **)opt
->value
;
885 die("Two output directories?");
890 static int thread_callback(const struct option
*opt
, const char *arg
, int unset
)
892 int *thread
= (int *)opt
->value
;
895 else if (!arg
|| !strcmp(arg
, "shallow"))
896 *thread
= THREAD_SHALLOW
;
897 else if (!strcmp(arg
, "deep"))
898 *thread
= THREAD_DEEP
;
904 static int attach_callback(const struct option
*opt
, const char *arg
, int unset
)
906 struct rev_info
*rev
= (struct rev_info
*)opt
->value
;
908 rev
->mime_boundary
= NULL
;
910 rev
->mime_boundary
= arg
;
912 rev
->mime_boundary
= git_version_string
;
913 rev
->no_inline
= unset
? 0 : 1;
917 static int inline_callback(const struct option
*opt
, const char *arg
, int unset
)
919 struct rev_info
*rev
= (struct rev_info
*)opt
->value
;
921 rev
->mime_boundary
= NULL
;
923 rev
->mime_boundary
= arg
;
925 rev
->mime_boundary
= git_version_string
;
930 static int header_callback(const struct option
*opt
, const char *arg
, int unset
)
933 string_list_clear(&extra_hdr
, 0);
934 string_list_clear(&extra_to
, 0);
935 string_list_clear(&extra_cc
, 0);
942 static int to_callback(const struct option
*opt
, const char *arg
, int unset
)
945 string_list_clear(&extra_to
, 0);
947 string_list_append(arg
, &extra_to
);
951 static int cc_callback(const struct option
*opt
, const char *arg
, int unset
)
954 string_list_clear(&extra_cc
, 0);
956 string_list_append(arg
, &extra_cc
);
960 int cmd_format_patch(int argc
, const char **argv
, const char *prefix
)
962 struct commit
*commit
;
963 struct commit
**list
= NULL
;
965 struct setup_revision_opt s_r_opt
;
966 int nr
= 0, total
, i
;
968 int start_number
= -1;
969 int numbered_files
= 0; /* _just_ numbers */
970 int ignore_if_in_upstream
= 0;
971 int cover_letter
= 0;
972 int boundary_count
= 0;
973 int no_binary_diff
= 0;
974 struct commit
*origin
= NULL
, *head
= NULL
;
975 const char *in_reply_to
= NULL
;
976 struct patch_ids ids
;
977 char *add_signoff
= NULL
;
978 struct strbuf buf
= STRBUF_INIT
;
979 int use_patch_format
= 0;
980 const struct option builtin_format_patch_options
[] = {
981 { OPTION_CALLBACK
, 'n', "numbered", &numbered
, NULL
,
982 "use [PATCH n/m] even with a single patch",
983 PARSE_OPT_NOARG
, numbered_callback
},
984 { OPTION_CALLBACK
, 'N', "no-numbered", &numbered
, NULL
,
985 "use [PATCH] even with multiple patches",
986 PARSE_OPT_NOARG
, no_numbered_callback
},
987 OPT_BOOLEAN('s', "signoff", &do_signoff
, "add Signed-off-by:"),
988 OPT_BOOLEAN(0, "stdout", &use_stdout
,
989 "print patches to standard out"),
990 OPT_BOOLEAN(0, "cover-letter", &cover_letter
,
991 "generate a cover letter"),
992 OPT_BOOLEAN(0, "numbered-files", &numbered_files
,
993 "use simple number sequence for output file names"),
994 OPT_STRING(0, "suffix", &fmt_patch_suffix
, "sfx",
995 "use <sfx> instead of '.patch'"),
996 OPT_INTEGER(0, "start-number", &start_number
,
997 "start numbering patches at <n> instead of 1"),
998 { OPTION_CALLBACK
, 0, "subject-prefix", &rev
, "prefix",
999 "Use [<prefix>] instead of [PATCH]",
1000 PARSE_OPT_NONEG
, subject_prefix_callback
},
1001 { OPTION_CALLBACK
, 'o', "output-directory", &output_directory
,
1002 "dir", "store resulting files in <dir>",
1003 PARSE_OPT_NONEG
, output_directory_callback
},
1004 { OPTION_CALLBACK
, 'k', "keep-subject", &rev
, NULL
,
1005 "don't strip/add [PATCH]",
1006 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
, keep_callback
},
1007 OPT_BOOLEAN(0, "no-binary", &no_binary_diff
,
1008 "don't output binary diffs"),
1009 OPT_BOOLEAN(0, "ignore-if-in-upstream", &ignore_if_in_upstream
,
1010 "don't include a patch matching a commit upstream"),
1011 { OPTION_BOOLEAN
, 'p', "no-stat", &use_patch_format
, NULL
,
1012 "show patch format instead of default (patch + stat)",
1013 PARSE_OPT_NONEG
| PARSE_OPT_NOARG
},
1014 OPT_GROUP("Messaging"),
1015 { OPTION_CALLBACK
, 0, "add-header", NULL
, "header",
1016 "add email header", 0, header_callback
},
1017 { OPTION_CALLBACK
, 0, "to", NULL
, "email", "add To: header",
1019 { OPTION_CALLBACK
, 0, "cc", NULL
, "email", "add Cc: header",
1021 OPT_STRING(0, "in-reply-to", &in_reply_to
, "message-id",
1022 "make first mail a reply to <message-id>"),
1023 { OPTION_CALLBACK
, 0, "attach", &rev
, "boundary",
1024 "attach the patch", PARSE_OPT_OPTARG
,
1026 { OPTION_CALLBACK
, 0, "inline", &rev
, "boundary",
1028 PARSE_OPT_OPTARG
| PARSE_OPT_NONEG
,
1030 { OPTION_CALLBACK
, 0, "thread", &thread
, "style",
1031 "enable message threading, styles: shallow, deep",
1032 PARSE_OPT_OPTARG
, thread_callback
},
1036 extra_hdr
.strdup_strings
= 1;
1037 extra_to
.strdup_strings
= 1;
1038 extra_cc
.strdup_strings
= 1;
1039 git_config(git_format_config
, NULL
);
1040 init_revisions(&rev
, prefix
);
1041 rev
.commit_format
= CMIT_FMT_EMAIL
;
1042 rev
.verbose_header
= 1;
1044 rev
.combine_merges
= 0;
1045 rev
.ignore_merges
= 1;
1046 DIFF_OPT_SET(&rev
.diffopt
, RECURSIVE
);
1047 rev
.subject_prefix
= fmt_patch_subject_prefix
;
1048 memset(&s_r_opt
, 0, sizeof(s_r_opt
));
1049 s_r_opt
.def
= "HEAD";
1051 if (default_attach
) {
1052 rev
.mime_boundary
= default_attach
;
1057 * Parse the arguments before setup_revisions(), or something
1058 * like "git format-patch -o a123 HEAD^.." may fail; a123 is
1059 * possibly a valid SHA1.
1061 argc
= parse_options(argc
, argv
, prefix
, builtin_format_patch_options
,
1062 builtin_format_patch_usage
,
1063 PARSE_OPT_KEEP_ARGV0
| PARSE_OPT_KEEP_UNKNOWN
|
1064 PARSE_OPT_KEEP_DASHDASH
);
1067 const char *committer
;
1069 committer
= git_committer_info(IDENT_ERROR_ON_NO_NAME
);
1070 endpos
= strchr(committer
, '>');
1072 die("bogus committer info %s", committer
);
1073 add_signoff
= xmemdupz(committer
, endpos
- committer
+ 1);
1076 for (i
= 0; i
< extra_hdr
.nr
; i
++) {
1077 strbuf_addstr(&buf
, extra_hdr
.items
[i
].string
);
1078 strbuf_addch(&buf
, '\n');
1082 strbuf_addstr(&buf
, "To: ");
1083 for (i
= 0; i
< extra_to
.nr
; i
++) {
1085 strbuf_addstr(&buf
, " ");
1086 strbuf_addstr(&buf
, extra_to
.items
[i
].string
);
1087 if (i
+ 1 < extra_to
.nr
)
1088 strbuf_addch(&buf
, ',');
1089 strbuf_addch(&buf
, '\n');
1093 strbuf_addstr(&buf
, "Cc: ");
1094 for (i
= 0; i
< extra_cc
.nr
; i
++) {
1096 strbuf_addstr(&buf
, " ");
1097 strbuf_addstr(&buf
, extra_cc
.items
[i
].string
);
1098 if (i
+ 1 < extra_cc
.nr
)
1099 strbuf_addch(&buf
, ',');
1100 strbuf_addch(&buf
, '\n');
1103 rev
.extra_headers
= strbuf_detach(&buf
, NULL
);
1105 if (start_number
< 0)
1109 * If numbered is set solely due to format.numbered in config,
1110 * and it would conflict with --keep-subject (-k) from the
1111 * command line, reset "numbered".
1113 if (numbered
&& keep_subject
&& !numbered_cmdline_opt
)
1116 if (numbered
&& keep_subject
)
1117 die ("-n and -k are mutually exclusive.");
1118 if (keep_subject
&& subject_prefix
)
1119 die ("--subject-prefix and -k are mutually exclusive.");
1121 argc
= setup_revisions(argc
, argv
, &rev
, &s_r_opt
);
1123 die ("unrecognized argument: %s", argv
[1]);
1125 if (rev
.diffopt
.output_format
& DIFF_FORMAT_NAME
)
1126 die("--name-only does not make sense");
1127 if (rev
.diffopt
.output_format
& DIFF_FORMAT_NAME_STATUS
)
1128 die("--name-status does not make sense");
1129 if (rev
.diffopt
.output_format
& DIFF_FORMAT_CHECKDIFF
)
1130 die("--check does not make sense");
1132 if (!use_patch_format
&&
1133 (!rev
.diffopt
.output_format
||
1134 rev
.diffopt
.output_format
== DIFF_FORMAT_PATCH
))
1135 rev
.diffopt
.output_format
= DIFF_FORMAT_DIFFSTAT
| DIFF_FORMAT_SUMMARY
;
1137 /* Always generate a patch */
1138 rev
.diffopt
.output_format
|= DIFF_FORMAT_PATCH
;
1140 if (!DIFF_OPT_TST(&rev
.diffopt
, TEXT
) && !no_binary_diff
)
1141 DIFF_OPT_SET(&rev
.diffopt
, BINARY
);
1144 init_display_notes(&rev
.notes_opt
);
1147 output_directory
= set_outdir(prefix
, output_directory
);
1149 if (output_directory
) {
1151 die("standard output, or directory, which one?");
1152 if (mkdir(output_directory
, 0777) < 0 && errno
!= EEXIST
)
1153 die_errno("Could not create directory '%s'",
1157 if (rev
.pending
.nr
== 1) {
1158 if (rev
.max_count
< 0 && !rev
.show_root_diff
) {
1160 * This is traditional behaviour of "git format-patch
1161 * origin" that prepares what the origin side still
1164 rev
.pending
.objects
[0].item
->flags
|= UNINTERESTING
;
1165 add_head_to_pending(&rev
);
1168 * Otherwise, it is "format-patch -22 HEAD", and/or
1169 * "format-patch --root HEAD". The user wants
1170 * get_revision() to do the usual traversal.
1175 * We cannot move this anywhere earlier because we do want to
1176 * know if --root was given explicitly from the command line.
1178 rev
.show_root_diff
= 1;
1181 /* remember the range */
1183 for (i
= 0; i
< rev
.pending
.nr
; i
++) {
1184 struct object
*o
= rev
.pending
.objects
[i
].item
;
1185 if (!(o
->flags
& UNINTERESTING
))
1186 head
= (struct commit
*)o
;
1188 /* We can't generate a cover letter without any patches */
1193 if (ignore_if_in_upstream
) {
1194 /* Don't say anything if head and upstream are the same. */
1195 if (rev
.pending
.nr
== 2) {
1196 struct object_array_entry
*o
= rev
.pending
.objects
;
1197 if (hashcmp(o
[0].item
->sha1
, o
[1].item
->sha1
) == 0)
1200 get_patch_ids(&rev
, &ids
, prefix
);
1204 realstdout
= xfdopen(xdup(1), "w");
1206 if (prepare_revision_walk(&rev
))
1207 die("revision walk setup failed");
1209 while ((commit
= get_revision(&rev
)) != NULL
) {
1210 if (commit
->object
.flags
& BOUNDARY
) {
1212 origin
= (boundary_count
== 1) ? commit
: NULL
;
1217 if (commit
->parents
&& commit
->parents
->next
)
1220 if (ignore_if_in_upstream
&&
1221 has_commit_patch_id(commit
, &ids
))
1225 list
= xrealloc(list
, nr
* sizeof(list
[0]));
1226 list
[nr
- 1] = commit
;
1229 if (!keep_subject
&& auto_number
&& total
> 1)
1232 rev
.total
= total
+ start_number
- 1;
1233 if (in_reply_to
|| thread
|| cover_letter
)
1234 rev
.ref_message_ids
= xcalloc(1, sizeof(struct string_list
));
1236 const char *msgid
= clean_message_id(in_reply_to
);
1237 string_list_append(msgid
, rev
.ref_message_ids
);
1239 rev
.numbered_files
= numbered_files
;
1240 rev
.patch_suffix
= fmt_patch_suffix
;
1243 gen_message_id(&rev
, "cover");
1244 make_cover_letter(&rev
, use_stdout
, numbered
, numbered_files
,
1245 origin
, nr
, list
, head
);
1249 rev
.add_signoff
= add_signoff
;
1253 rev
.nr
= total
- nr
+ (start_number
- 1);
1254 /* Make the second and subsequent mails replies to the first */
1256 /* Have we already had a message ID? */
1257 if (rev
.message_id
) {
1259 * For deep threading: make every mail
1260 * a reply to the previous one, no
1261 * matter what other options are set.
1263 * For shallow threading:
1265 * Without --cover-letter and
1266 * --in-reply-to, make every mail a
1267 * reply to the one before.
1269 * With --in-reply-to but no
1270 * --cover-letter, make every mail a
1271 * reply to the <reply-to>.
1273 * With --cover-letter, make every
1274 * mail but the cover letter a reply
1275 * to the cover letter. The cover
1276 * letter is a reply to the
1277 * --in-reply-to, if specified.
1279 if (thread
== THREAD_SHALLOW
1280 && rev
.ref_message_ids
->nr
> 0
1281 && (!cover_letter
|| rev
.nr
> 1))
1282 free(rev
.message_id
);
1284 string_list_append(rev
.message_id
,
1285 rev
.ref_message_ids
);
1287 gen_message_id(&rev
, sha1_to_hex(commit
->object
.sha1
));
1290 if (!use_stdout
&& reopen_stdout(numbered_files
? NULL
: commit
,
1292 die("Failed to create output files");
1293 shown
= log_tree_commit(&rev
, commit
);
1294 free(commit
->buffer
);
1295 commit
->buffer
= NULL
;
1297 /* We put one extra blank line between formatted
1298 * patches and this flag is used by log-tree code
1299 * to see if it needs to emit a LF before showing
1300 * the log; when using one file per patch, we do
1301 * not want the extra blank line.
1306 if (rev
.mime_boundary
)
1307 printf("\n--%s%s--\n\n\n",
1308 mime_boundary_leader
,
1311 printf("-- \n%s\n\n", git_version_string
);
1317 string_list_clear(&extra_to
, 0);
1318 string_list_clear(&extra_cc
, 0);
1319 string_list_clear(&extra_hdr
, 0);
1320 if (ignore_if_in_upstream
)
1321 free_patch_ids(&ids
);
1325 static int add_pending_commit(const char *arg
, struct rev_info
*revs
, int flags
)
1327 unsigned char sha1
[20];
1328 if (get_sha1(arg
, sha1
) == 0) {
1329 struct commit
*commit
= lookup_commit_reference(sha1
);
1331 commit
->object
.flags
|= flags
;
1332 add_pending_object(revs
, &commit
->object
, arg
);
1339 static const char * const cherry_usage
[] = {
1340 "git cherry [-v] [<upstream> [<head> [<limit>]]]",
1344 int cmd_cherry(int argc
, const char **argv
, const char *prefix
)
1346 struct rev_info revs
;
1347 struct patch_ids ids
;
1348 struct commit
*commit
;
1349 struct commit_list
*list
= NULL
;
1350 struct branch
*current_branch
;
1351 const char *upstream
;
1352 const char *head
= "HEAD";
1353 const char *limit
= NULL
;
1354 int verbose
= 0, abbrev
= 0;
1356 struct option options
[] = {
1357 OPT__ABBREV(&abbrev
),
1358 OPT__VERBOSE(&verbose
),
1362 argc
= parse_options(argc
, argv
, prefix
, options
, cherry_usage
, 0);
1375 current_branch
= branch_get(NULL
);
1376 if (!current_branch
|| !current_branch
->merge
1377 || !current_branch
->merge
[0]
1378 || !current_branch
->merge
[0]->dst
) {
1379 fprintf(stderr
, "Could not find a tracked"
1380 " remote branch, please"
1381 " specify <upstream> manually.\n");
1382 usage_with_options(cherry_usage
, options
);
1385 upstream
= current_branch
->merge
[0]->dst
;
1388 init_revisions(&revs
, prefix
);
1390 revs
.combine_merges
= 0;
1391 revs
.ignore_merges
= 1;
1392 DIFF_OPT_SET(&revs
.diffopt
, RECURSIVE
);
1394 if (add_pending_commit(head
, &revs
, 0))
1395 die("Unknown commit %s", head
);
1396 if (add_pending_commit(upstream
, &revs
, UNINTERESTING
))
1397 die("Unknown commit %s", upstream
);
1399 /* Don't say anything if head and upstream are the same. */
1400 if (revs
.pending
.nr
== 2) {
1401 struct object_array_entry
*o
= revs
.pending
.objects
;
1402 if (hashcmp(o
[0].item
->sha1
, o
[1].item
->sha1
) == 0)
1406 get_patch_ids(&revs
, &ids
, prefix
);
1408 if (limit
&& add_pending_commit(limit
, &revs
, UNINTERESTING
))
1409 die("Unknown commit %s", limit
);
1411 /* reverse the list of commits */
1412 if (prepare_revision_walk(&revs
))
1413 die("revision walk setup failed");
1414 while ((commit
= get_revision(&revs
)) != NULL
) {
1416 if (commit
->parents
&& commit
->parents
->next
)
1419 commit_list_insert(commit
, &list
);
1425 commit
= list
->item
;
1426 if (has_commit_patch_id(commit
, &ids
))
1430 struct strbuf buf
= STRBUF_INIT
;
1431 struct pretty_print_context ctx
= {0};
1432 pretty_print_commit(CMIT_FMT_ONELINE
, commit
,
1434 printf("%c %s %s\n", sign
,
1435 find_unique_abbrev(commit
->object
.sha1
, abbrev
),
1437 strbuf_release(&buf
);
1440 printf("%c %s\n", sign
,
1441 find_unique_abbrev(commit
->object
.sha1
, abbrev
));
1447 free_patch_ids(&ids
);