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 const char *fmt_patch_subject_prefix
= "PATCH";
28 static const char *fmt_pretty
;
30 static const char * const builtin_log_usage
=
31 "git log [<options>] [<since>..<until>] [[--] <path>...]\n"
32 " or: git show [options] <object>...";
34 static void cmd_log_init(int argc
, const char **argv
, const char *prefix
,
38 int decoration_style
= 0;
40 rev
->abbrev
= DEFAULT_ABBREV
;
41 rev
->commit_format
= CMIT_FMT_DEFAULT
;
43 get_commit_format(fmt_pretty
, rev
);
44 rev
->verbose_header
= 1;
45 DIFF_OPT_SET(&rev
->diffopt
, RECURSIVE
);
46 rev
->show_root_diff
= default_show_root
;
47 rev
->subject_prefix
= fmt_patch_subject_prefix
;
48 DIFF_OPT_SET(&rev
->diffopt
, ALLOW_TEXTCONV
);
50 if (default_date_mode
)
51 rev
->date_mode
= parse_date_format(default_date_mode
);
54 * Check for -h before setup_revisions(), or "git log -h" will
55 * fail when run without a git directory.
57 if (argc
== 2 && !strcmp(argv
[1], "-h"))
58 usage(builtin_log_usage
);
59 argc
= setup_revisions(argc
, argv
, rev
, "HEAD");
61 if (rev
->diffopt
.pickaxe
|| rev
->diffopt
.filter
)
62 rev
->always_show_header
= 0;
63 if (DIFF_OPT_TST(&rev
->diffopt
, FOLLOW_RENAMES
)) {
64 rev
->always_show_header
= 0;
65 if (rev
->diffopt
.nr_paths
!= 1)
66 usage("git logs can only follow renames on one pathname at a time");
68 for (i
= 1; i
< argc
; i
++) {
69 const char *arg
= argv
[i
];
70 if (!strcmp(arg
, "--decorate")) {
71 decoration_style
= DECORATE_SHORT_REFS
;
72 } else if (!prefixcmp(arg
, "--decorate=")) {
73 const char *v
= skip_prefix(arg
, "--decorate=");
74 if (!strcmp(v
, "full"))
75 decoration_style
= DECORATE_FULL_REFS
;
76 else if (!strcmp(v
, "short"))
77 decoration_style
= DECORATE_SHORT_REFS
;
79 die("invalid --decorate option: %s", arg
);
80 } else if (!strcmp(arg
, "--source")) {
82 } else if (!strcmp(arg
, "-h")) {
83 usage(builtin_log_usage
);
85 die("unrecognized argument: %s", arg
);
87 if (decoration_style
) {
88 rev
->show_decorations
= 1;
89 load_ref_decorations(decoration_style
);
94 * This gives a rough estimate for how many commits we
95 * will print out in the list.
97 static int estimate_commit_count(struct rev_info
*rev
, struct commit_list
*list
)
102 struct commit
*commit
= list
->item
;
103 unsigned int flags
= commit
->object
.flags
;
105 if (!(flags
& (TREESAME
| UNINTERESTING
)))
111 static void show_early_header(struct rev_info
*rev
, const char *stage
, int nr
)
113 if (rev
->shown_one
) {
115 if (rev
->commit_format
!= CMIT_FMT_ONELINE
)
116 putchar(rev
->diffopt
.line_termination
);
118 printf("Final output: %d %s\n", nr
, stage
);
121 static struct itimerval early_output_timer
;
123 static void log_show_early(struct rev_info
*revs
, struct commit_list
*list
)
125 int i
= revs
->early_output
;
128 sort_in_topological_order(&list
, revs
->lifo
);
130 struct commit
*commit
= list
->item
;
131 switch (simplify_commit(revs
, commit
)) {
134 int n
= estimate_commit_count(revs
, list
);
135 show_early_header(revs
, "incomplete", n
);
138 log_tree_commit(revs
, commit
);
149 /* Did we already get enough commits for the early output? */
154 * ..if no, then repeat it twice a second until we
157 * NOTE! We don't use "it_interval", because if the
158 * reader isn't listening, we want our output to be
159 * throttled by the writing, and not have the timer
160 * trigger every second even if we're blocked on a
163 early_output_timer
.it_value
.tv_sec
= 0;
164 early_output_timer
.it_value
.tv_usec
= 500000;
165 setitimer(ITIMER_REAL
, &early_output_timer
, NULL
);
168 static void early_output(int signal
)
170 show_early_output
= log_show_early
;
173 static void setup_early_output(struct rev_info
*rev
)
178 * Set up the signal handler, minimally intrusively:
179 * we only set a single volatile integer word (not
180 * using sigatomic_t - trying to avoid unnecessary
181 * system dependencies and headers), and using
184 memset(&sa
, 0, sizeof(sa
));
185 sa
.sa_handler
= early_output
;
186 sigemptyset(&sa
.sa_mask
);
187 sa
.sa_flags
= SA_RESTART
;
188 sigaction(SIGALRM
, &sa
, NULL
);
191 * If we can get the whole output in less than a
192 * tenth of a second, don't even bother doing the
193 * early-output thing..
195 * This is a one-time-only trigger.
197 early_output_timer
.it_value
.tv_sec
= 0;
198 early_output_timer
.it_value
.tv_usec
= 100000;
199 setitimer(ITIMER_REAL
, &early_output_timer
, NULL
);
202 static void finish_early_output(struct rev_info
*rev
)
204 int n
= estimate_commit_count(rev
, rev
->commits
);
205 signal(SIGALRM
, SIG_IGN
);
206 show_early_header(rev
, "done", n
);
209 static int cmd_log_walk(struct rev_info
*rev
)
211 struct commit
*commit
;
213 if (rev
->early_output
)
214 setup_early_output(rev
);
216 if (prepare_revision_walk(rev
))
217 die("revision walk setup failed");
219 if (rev
->early_output
)
220 finish_early_output(rev
);
223 * For --check and --exit-code, the exit code is based on CHECK_FAILED
224 * and HAS_CHANGES being accumulated in rev->diffopt, so be careful to
225 * retain that state information if replacing rev->diffopt in this loop
227 while ((commit
= get_revision(rev
)) != NULL
) {
228 log_tree_commit(rev
, commit
);
229 if (!rev
->reflog_info
) {
230 /* we allow cycles in reflog ancestry */
231 free(commit
->buffer
);
232 commit
->buffer
= NULL
;
234 free_commit_list(commit
->parents
);
235 commit
->parents
= NULL
;
237 if (rev
->diffopt
.output_format
& DIFF_FORMAT_CHECKDIFF
&&
238 DIFF_OPT_TST(&rev
->diffopt
, CHECK_FAILED
)) {
241 return diff_result_code(&rev
->diffopt
, 0);
244 static int git_log_config(const char *var
, const char *value
, void *cb
)
246 if (!strcmp(var
, "format.pretty"))
247 return git_config_string(&fmt_pretty
, var
, value
);
248 if (!strcmp(var
, "format.subjectprefix"))
249 return git_config_string(&fmt_patch_subject_prefix
, var
, value
);
250 if (!strcmp(var
, "log.date"))
251 return git_config_string(&default_date_mode
, var
, value
);
252 if (!strcmp(var
, "log.showroot")) {
253 default_show_root
= git_config_bool(var
, value
);
256 return git_diff_ui_config(var
, value
, cb
);
259 int cmd_whatchanged(int argc
, const char **argv
, const char *prefix
)
263 git_config(git_log_config
, NULL
);
265 if (diff_use_color_default
== -1)
266 diff_use_color_default
= git_use_color_default
;
268 init_revisions(&rev
, prefix
);
270 rev
.simplify_history
= 0;
271 cmd_log_init(argc
, argv
, prefix
, &rev
);
272 if (!rev
.diffopt
.output_format
)
273 rev
.diffopt
.output_format
= DIFF_FORMAT_RAW
;
274 return cmd_log_walk(&rev
);
277 static void show_tagger(char *buf
, int len
, struct rev_info
*rev
)
279 struct strbuf out
= STRBUF_INIT
;
281 pp_user_info("Tagger", rev
->commit_format
, &out
, buf
, rev
->date_mode
,
282 git_log_output_encoding
?
283 git_log_output_encoding
: git_commit_encoding
);
284 printf("%s", out
.buf
);
285 strbuf_release(&out
);
288 static int show_object(const unsigned char *sha1
, int show_tag_object
,
289 struct rev_info
*rev
)
292 enum object_type type
;
293 char *buf
= read_sha1_file(sha1
, &type
, &size
);
297 return error("Could not read object %s", sha1_to_hex(sha1
));
300 while (offset
< size
&& buf
[offset
] != '\n') {
301 int new_offset
= offset
+ 1;
302 while (new_offset
< size
&& buf
[new_offset
++] != '\n')
304 if (!prefixcmp(buf
+ offset
, "tagger "))
305 show_tagger(buf
+ offset
+ 7,
306 new_offset
- offset
- 7, rev
);
311 fwrite(buf
+ offset
, size
- offset
, 1, stdout
);
316 static int show_tree_object(const unsigned char *sha1
,
317 const char *base
, int baselen
,
318 const char *pathname
, unsigned mode
, int stage
, void *context
)
320 printf("%s%s\n", pathname
, S_ISDIR(mode
) ? "/" : "");
324 int cmd_show(int argc
, const char **argv
, const char *prefix
)
327 struct object_array_entry
*objects
;
328 int i
, count
, ret
= 0;
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
.combine_merges
= 1;
338 rev
.dense_combined_merges
= 1;
339 rev
.always_show_header
= 1;
340 rev
.ignore_merges
= 0;
342 cmd_log_init(argc
, argv
, prefix
, &rev
);
344 count
= rev
.pending
.nr
;
345 objects
= rev
.pending
.objects
;
346 for (i
= 0; i
< count
&& !ret
; i
++) {
347 struct object
*o
= objects
[i
].item
;
348 const char *name
= objects
[i
].name
;
351 ret
= show_object(o
->sha1
, 0, NULL
);
354 struct tag
*t
= (struct tag
*)o
;
358 printf("%stag %s%s\n",
359 diff_get_color_opt(&rev
.diffopt
, DIFF_COMMIT
),
361 diff_get_color_opt(&rev
.diffopt
, DIFF_RESET
));
362 ret
= show_object(o
->sha1
, 1, &rev
);
366 o
= parse_object(t
->tagged
->sha1
);
368 ret
= error("Could not read object %s",
369 sha1_to_hex(t
->tagged
->sha1
));
377 printf("%stree %s%s\n\n",
378 diff_get_color_opt(&rev
.diffopt
, DIFF_COMMIT
),
380 diff_get_color_opt(&rev
.diffopt
, DIFF_RESET
));
381 read_tree_recursive((struct tree
*)o
, "", 0, 0, NULL
,
382 show_tree_object
, NULL
);
386 rev
.pending
.nr
= rev
.pending
.alloc
= 0;
387 rev
.pending
.objects
= NULL
;
388 add_object_array(o
, name
, &rev
.pending
);
389 ret
= cmd_log_walk(&rev
);
392 ret
= error("Unknown type: %d", o
->type
);
400 * This is equivalent to "git log -g --abbrev-commit --pretty=oneline"
402 int cmd_log_reflog(int argc
, const char **argv
, const char *prefix
)
406 git_config(git_log_config
, NULL
);
408 if (diff_use_color_default
== -1)
409 diff_use_color_default
= git_use_color_default
;
411 init_revisions(&rev
, prefix
);
412 init_reflog_walk(&rev
.reflog_info
);
413 rev
.abbrev_commit
= 1;
414 rev
.verbose_header
= 1;
415 cmd_log_init(argc
, argv
, prefix
, &rev
);
418 * This means that we override whatever commit format the user gave
419 * on the cmd line. Sad, but cmd_log_init() currently doesn't
420 * allow us to set a different default.
422 rev
.commit_format
= CMIT_FMT_ONELINE
;
423 rev
.use_terminator
= 1;
424 rev
.always_show_header
= 1;
427 * We get called through "git reflog", so unlike the other log
428 * routines, we need to set up our pager manually..
432 return cmd_log_walk(&rev
);
435 int cmd_log(int argc
, const char **argv
, const char *prefix
)
439 git_config(git_log_config
, NULL
);
441 if (diff_use_color_default
== -1)
442 diff_use_color_default
= git_use_color_default
;
444 init_revisions(&rev
, prefix
);
445 rev
.always_show_header
= 1;
446 cmd_log_init(argc
, argv
, prefix
, &rev
);
447 return cmd_log_walk(&rev
);
452 static const char *fmt_patch_suffix
= ".patch";
453 static int numbered
= 0;
454 static int auto_number
= 1;
456 static char *default_attach
= NULL
;
458 static char **extra_hdr
;
459 static int extra_hdr_nr
;
460 static int extra_hdr_alloc
;
462 static char **extra_to
;
463 static int extra_to_nr
;
464 static int extra_to_alloc
;
466 static char **extra_cc
;
467 static int extra_cc_nr
;
468 static int extra_cc_alloc
;
470 static void add_header(const char *value
)
472 int len
= strlen(value
);
473 while (len
&& value
[len
- 1] == '\n')
475 if (!strncasecmp(value
, "to: ", 4)) {
476 ALLOC_GROW(extra_to
, extra_to_nr
+ 1, extra_to_alloc
);
477 extra_to
[extra_to_nr
++] = xstrndup(value
+ 4, len
- 4);
480 if (!strncasecmp(value
, "cc: ", 4)) {
481 ALLOC_GROW(extra_cc
, extra_cc_nr
+ 1, extra_cc_alloc
);
482 extra_cc
[extra_cc_nr
++] = xstrndup(value
+ 4, len
- 4);
485 ALLOC_GROW(extra_hdr
, extra_hdr_nr
+ 1, extra_hdr_alloc
);
486 extra_hdr
[extra_hdr_nr
++] = xstrndup(value
, len
);
489 #define THREAD_SHALLOW 1
490 #define THREAD_DEEP 2
491 static int thread
= 0;
492 static int do_signoff
= 0;
494 static int git_format_config(const char *var
, const char *value
, void *cb
)
496 if (!strcmp(var
, "format.headers")) {
498 die("format.headers without value");
502 if (!strcmp(var
, "format.suffix"))
503 return git_config_string(&fmt_patch_suffix
, var
, value
);
504 if (!strcmp(var
, "format.cc")) {
506 return config_error_nonbool(var
);
507 ALLOC_GROW(extra_cc
, extra_cc_nr
+ 1, extra_cc_alloc
);
508 extra_cc
[extra_cc_nr
++] = xstrdup(value
);
511 if (!strcmp(var
, "diff.color") || !strcmp(var
, "color.diff")) {
514 if (!strcmp(var
, "format.numbered")) {
515 if (value
&& !strcasecmp(value
, "auto")) {
519 numbered
= git_config_bool(var
, value
);
520 auto_number
= auto_number
&& numbered
;
523 if (!strcmp(var
, "format.attach")) {
525 default_attach
= xstrdup(value
);
527 default_attach
= xstrdup(git_version_string
);
530 if (!strcmp(var
, "format.thread")) {
531 if (value
&& !strcasecmp(value
, "deep")) {
532 thread
= THREAD_DEEP
;
535 if (value
&& !strcasecmp(value
, "shallow")) {
536 thread
= THREAD_SHALLOW
;
539 thread
= git_config_bool(var
, value
) && THREAD_SHALLOW
;
542 if (!strcmp(var
, "format.signoff")) {
543 do_signoff
= git_config_bool(var
, value
);
547 return git_log_config(var
, value
, cb
);
550 static FILE *realstdout
= NULL
;
551 static const char *output_directory
= NULL
;
552 static int outdir_offset
;
554 static int reopen_stdout(struct commit
*commit
, struct rev_info
*rev
)
556 struct strbuf filename
= STRBUF_INIT
;
557 int suffix_len
= strlen(fmt_patch_suffix
) + 1;
559 if (output_directory
) {
560 strbuf_addstr(&filename
, output_directory
);
562 PATH_MAX
- FORMAT_PATCH_NAME_MAX
- suffix_len
)
563 return error("name of output directory is too long");
564 if (filename
.buf
[filename
.len
- 1] != '/')
565 strbuf_addch(&filename
, '/');
568 get_patch_filename(commit
, rev
->nr
, fmt_patch_suffix
, &filename
);
570 if (!DIFF_OPT_TST(&rev
->diffopt
, QUIET
))
571 fprintf(realstdout
, "%s\n", filename
.buf
+ outdir_offset
);
573 if (freopen(filename
.buf
, "w", stdout
) == NULL
)
574 return error("Cannot open patch file %s", filename
.buf
);
576 strbuf_release(&filename
);
580 static void get_patch_ids(struct rev_info
*rev
, struct patch_ids
*ids
, const char *prefix
)
582 struct rev_info check_rev
;
583 struct commit
*commit
;
584 struct object
*o1
, *o2
;
585 unsigned flags1
, flags2
;
587 if (rev
->pending
.nr
!= 2)
588 die("Need exactly one range.");
590 o1
= rev
->pending
.objects
[0].item
;
592 o2
= rev
->pending
.objects
[1].item
;
595 if ((flags1
& UNINTERESTING
) == (flags2
& UNINTERESTING
))
600 /* given a range a..b get all patch ids for b..a */
601 init_revisions(&check_rev
, prefix
);
602 o1
->flags
^= UNINTERESTING
;
603 o2
->flags
^= UNINTERESTING
;
604 add_pending_object(&check_rev
, o1
, "o1");
605 add_pending_object(&check_rev
, o2
, "o2");
606 if (prepare_revision_walk(&check_rev
))
607 die("revision walk setup failed");
609 while ((commit
= get_revision(&check_rev
)) != NULL
) {
611 if (commit
->parents
&& commit
->parents
->next
)
614 add_commit_patch_id(commit
, ids
);
617 /* reset for next revision walk */
618 clear_commit_marks((struct commit
*)o1
,
619 SEEN
| UNINTERESTING
| SHOWN
| ADDED
);
620 clear_commit_marks((struct commit
*)o2
,
621 SEEN
| UNINTERESTING
| SHOWN
| ADDED
);
626 static void gen_message_id(struct rev_info
*info
, char *base
)
628 const char *committer
= git_committer_info(IDENT_WARN_ON_NO_NAME
);
629 const char *email_start
= strrchr(committer
, '<');
630 const char *email_end
= strrchr(committer
, '>');
631 struct strbuf buf
= STRBUF_INIT
;
632 if (!email_start
|| !email_end
|| email_start
> email_end
- 1)
633 die("Could not extract email from committer identity.");
634 strbuf_addf(&buf
, "%s.%lu.git.%.*s", base
,
635 (unsigned long) time(NULL
),
636 (int)(email_end
- email_start
- 1), email_start
+ 1);
637 info
->message_id
= strbuf_detach(&buf
, NULL
);
640 static void make_cover_letter(struct rev_info
*rev
, int use_stdout
,
641 int numbered
, int numbered_files
,
642 struct commit
*origin
,
643 int nr
, struct commit
**list
, struct commit
*head
)
645 const char *committer
;
646 const char *subject_start
= NULL
;
647 const char *body
= "*** SUBJECT HERE ***\n\n*** BLURB HERE ***\n";
649 const char *extra_headers
= rev
->extra_headers
;
651 struct strbuf sb
= STRBUF_INIT
;
653 const char *encoding
= "UTF-8";
654 struct diff_options opts
;
655 int need_8bit_cte
= 0;
656 struct commit
*commit
= NULL
;
658 if (rev
->commit_format
!= CMIT_FMT_EMAIL
)
659 die("Cover letter needs email format");
661 committer
= git_committer_info(0);
663 if (!numbered_files
) {
665 * We fake a commit for the cover letter so we get the filename
668 commit
= xcalloc(1, sizeof(*commit
));
669 commit
->buffer
= xmalloc(400);
670 snprintf(commit
->buffer
, 400,
671 "tree 0000000000000000000000000000000000000000\n"
676 sha1_to_hex(head
->object
.sha1
), committer
, committer
);
679 if (!use_stdout
&& reopen_stdout(commit
, rev
))
684 free(commit
->buffer
);
688 log_write_email_headers(rev
, head
, &subject_start
, &extra_headers
,
691 for (i
= 0; !need_8bit_cte
&& i
< nr
; i
++)
692 if (has_non_ascii(list
[i
]->buffer
))
696 pp_user_info(NULL
, CMIT_FMT_EMAIL
, &sb
, committer
, DATE_RFC2822
,
698 pp_title_line(CMIT_FMT_EMAIL
, &msg
, &sb
, subject_start
, extra_headers
,
699 encoding
, need_8bit_cte
);
700 pp_remainder(CMIT_FMT_EMAIL
, &msg
, &sb
, 0);
701 printf("%s\n", sb
.buf
);
710 for (i
= 0; i
< nr
; i
++)
711 shortlog_add_commit(&log
, list
[i
]);
713 shortlog_output(&log
);
716 * We can only do diffstat with a unique reference point
721 memcpy(&opts
, &rev
->diffopt
, sizeof(opts
));
722 opts
.output_format
= DIFF_FORMAT_SUMMARY
| DIFF_FORMAT_DIFFSTAT
;
724 diff_setup_done(&opts
);
726 diff_tree_sha1(origin
->tree
->object
.sha1
,
727 head
->tree
->object
.sha1
,
735 static const char *clean_message_id(const char *msg_id
)
738 const char *a
, *z
, *m
;
741 while ((ch
= *m
) && (isspace(ch
) || (ch
== '<')))
746 if (!isspace(ch
) && (ch
!= '>'))
751 die("insane in-reply-to: %s", msg_id
);
754 return xmemdupz(a
, z
- a
);
757 static const char *set_outdir(const char *prefix
, const char *output_directory
)
759 if (output_directory
&& is_absolute_path(output_directory
))
760 return output_directory
;
762 if (!prefix
|| !*prefix
) {
763 if (output_directory
)
764 return output_directory
;
765 /* The user did not explicitly ask for "./" */
770 outdir_offset
= strlen(prefix
);
771 if (!output_directory
)
774 return xstrdup(prefix_filename(prefix
, outdir_offset
,
778 static const char * const builtin_format_patch_usage
[] = {
779 "git format-patch [options] [<since> | <revision range>]",
783 static int keep_subject
= 0;
785 static int keep_callback(const struct option
*opt
, const char *arg
, int unset
)
787 ((struct rev_info
*)opt
->value
)->total
= -1;
792 static int subject_prefix
= 0;
794 static int subject_prefix_callback(const struct option
*opt
, const char *arg
,
798 ((struct rev_info
*)opt
->value
)->subject_prefix
= arg
;
802 static int numbered_cmdline_opt
= 0;
804 static int numbered_callback(const struct option
*opt
, const char *arg
,
807 *(int *)opt
->value
= numbered_cmdline_opt
= unset
? 0 : 1;
813 static int no_numbered_callback(const struct option
*opt
, const char *arg
,
816 return numbered_callback(opt
, arg
, 1);
819 static int output_directory_callback(const struct option
*opt
, const char *arg
,
822 const char **dir
= (const char **)opt
->value
;
824 die("Two output directories?");
829 static int thread_callback(const struct option
*opt
, const char *arg
, int unset
)
831 int *thread
= (int *)opt
->value
;
834 else if (!arg
|| !strcmp(arg
, "shallow"))
835 *thread
= THREAD_SHALLOW
;
836 else if (!strcmp(arg
, "deep"))
837 *thread
= THREAD_DEEP
;
843 static int attach_callback(const struct option
*opt
, const char *arg
, int unset
)
845 struct rev_info
*rev
= (struct rev_info
*)opt
->value
;
847 rev
->mime_boundary
= NULL
;
849 rev
->mime_boundary
= arg
;
851 rev
->mime_boundary
= git_version_string
;
852 rev
->no_inline
= unset
? 0 : 1;
856 static int inline_callback(const struct option
*opt
, const char *arg
, int unset
)
858 struct rev_info
*rev
= (struct rev_info
*)opt
->value
;
860 rev
->mime_boundary
= NULL
;
862 rev
->mime_boundary
= arg
;
864 rev
->mime_boundary
= git_version_string
;
869 static int header_callback(const struct option
*opt
, const char *arg
, int unset
)
875 static int cc_callback(const struct option
*opt
, const char *arg
, int unset
)
877 ALLOC_GROW(extra_cc
, extra_cc_nr
+ 1, extra_cc_alloc
);
878 extra_cc
[extra_cc_nr
++] = xstrdup(arg
);
882 int cmd_format_patch(int argc
, const char **argv
, const char *prefix
)
884 struct commit
*commit
;
885 struct commit
**list
= NULL
;
887 int nr
= 0, total
, i
;
889 int start_number
= -1;
890 int numbered_files
= 0; /* _just_ numbers */
891 int ignore_if_in_upstream
= 0;
892 int cover_letter
= 0;
893 int boundary_count
= 0;
894 int no_binary_diff
= 0;
895 struct commit
*origin
= NULL
, *head
= NULL
;
896 const char *in_reply_to
= NULL
;
897 struct patch_ids ids
;
898 char *add_signoff
= NULL
;
899 struct strbuf buf
= STRBUF_INIT
;
900 const struct option builtin_format_patch_options
[] = {
901 { OPTION_CALLBACK
, 'n', "numbered", &numbered
, NULL
,
902 "use [PATCH n/m] even with a single patch",
903 PARSE_OPT_NOARG
, numbered_callback
},
904 { OPTION_CALLBACK
, 'N', "no-numbered", &numbered
, NULL
,
905 "use [PATCH] even with multiple patches",
906 PARSE_OPT_NOARG
, no_numbered_callback
},
907 OPT_BOOLEAN('s', "signoff", &do_signoff
, "add Signed-off-by:"),
908 OPT_BOOLEAN(0, "stdout", &use_stdout
,
909 "print patches to standard out"),
910 OPT_BOOLEAN(0, "cover-letter", &cover_letter
,
911 "generate a cover letter"),
912 OPT_BOOLEAN(0, "numbered-files", &numbered_files
,
913 "use simple number sequence for output file names"),
914 OPT_STRING(0, "suffix", &fmt_patch_suffix
, "sfx",
915 "use <sfx> instead of '.patch'"),
916 OPT_INTEGER(0, "start-number", &start_number
,
917 "start numbering patches at <n> instead of 1"),
918 { OPTION_CALLBACK
, 0, "subject-prefix", &rev
, "prefix",
919 "Use [<prefix>] instead of [PATCH]",
920 PARSE_OPT_NONEG
, subject_prefix_callback
},
921 { OPTION_CALLBACK
, 'o', "output-directory", &output_directory
,
922 "dir", "store resulting files in <dir>",
923 PARSE_OPT_NONEG
, output_directory_callback
},
924 { OPTION_CALLBACK
, 'k', "keep-subject", &rev
, NULL
,
925 "don't strip/add [PATCH]",
926 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
, keep_callback
},
927 OPT_BOOLEAN(0, "no-binary", &no_binary_diff
,
928 "don't output binary diffs"),
929 OPT_BOOLEAN(0, "ignore-if-in-upstream", &ignore_if_in_upstream
,
930 "don't include a patch matching a commit upstream"),
931 OPT_GROUP("Messaging"),
932 { OPTION_CALLBACK
, 0, "add-header", NULL
, "header",
933 "add email header", PARSE_OPT_NONEG
,
935 { OPTION_CALLBACK
, 0, "cc", NULL
, "email", "add Cc: header",
936 PARSE_OPT_NONEG
, cc_callback
},
937 OPT_STRING(0, "in-reply-to", &in_reply_to
, "message-id",
938 "make first mail a reply to <message-id>"),
939 { OPTION_CALLBACK
, 0, "attach", &rev
, "boundary",
940 "attach the patch", PARSE_OPT_OPTARG
,
942 { OPTION_CALLBACK
, 0, "inline", &rev
, "boundary",
944 PARSE_OPT_OPTARG
| PARSE_OPT_NONEG
,
946 { OPTION_CALLBACK
, 0, "thread", &thread
, "style",
947 "enable message threading, styles: shallow, deep",
948 PARSE_OPT_OPTARG
, thread_callback
},
952 git_config(git_format_config
, NULL
);
953 init_revisions(&rev
, prefix
);
954 rev
.commit_format
= CMIT_FMT_EMAIL
;
955 rev
.verbose_header
= 1;
957 rev
.combine_merges
= 0;
958 rev
.ignore_merges
= 1;
959 DIFF_OPT_SET(&rev
.diffopt
, RECURSIVE
);
961 rev
.subject_prefix
= fmt_patch_subject_prefix
;
963 if (default_attach
) {
964 rev
.mime_boundary
= default_attach
;
969 * Parse the arguments before setup_revisions(), or something
970 * like "git format-patch -o a123 HEAD^.." may fail; a123 is
971 * possibly a valid SHA1.
973 argc
= parse_options(argc
, argv
, prefix
, builtin_format_patch_options
,
974 builtin_format_patch_usage
,
975 PARSE_OPT_KEEP_ARGV0
| PARSE_OPT_KEEP_UNKNOWN
);
978 const char *committer
;
980 committer
= git_committer_info(IDENT_ERROR_ON_NO_NAME
);
981 endpos
= strchr(committer
, '>');
983 die("bogus committer info %s", committer
);
984 add_signoff
= xmemdupz(committer
, endpos
- committer
+ 1);
987 for (i
= 0; i
< extra_hdr_nr
; i
++) {
988 strbuf_addstr(&buf
, extra_hdr
[i
]);
989 strbuf_addch(&buf
, '\n');
993 strbuf_addstr(&buf
, "To: ");
994 for (i
= 0; i
< extra_to_nr
; i
++) {
996 strbuf_addstr(&buf
, " ");
997 strbuf_addstr(&buf
, extra_to
[i
]);
998 if (i
+ 1 < extra_to_nr
)
999 strbuf_addch(&buf
, ',');
1000 strbuf_addch(&buf
, '\n');
1004 strbuf_addstr(&buf
, "Cc: ");
1005 for (i
= 0; i
< extra_cc_nr
; i
++) {
1007 strbuf_addstr(&buf
, " ");
1008 strbuf_addstr(&buf
, extra_cc
[i
]);
1009 if (i
+ 1 < extra_cc_nr
)
1010 strbuf_addch(&buf
, ',');
1011 strbuf_addch(&buf
, '\n');
1014 rev
.extra_headers
= strbuf_detach(&buf
, NULL
);
1016 if (start_number
< 0)
1020 * If numbered is set solely due to format.numbered in config,
1021 * and it would conflict with --keep-subject (-k) from the
1022 * command line, reset "numbered".
1024 if (numbered
&& keep_subject
&& !numbered_cmdline_opt
)
1027 if (numbered
&& keep_subject
)
1028 die ("-n and -k are mutually exclusive.");
1029 if (keep_subject
&& subject_prefix
)
1030 die ("--subject-prefix and -k are mutually exclusive.");
1032 argc
= setup_revisions(argc
, argv
, &rev
, "HEAD");
1034 die ("unrecognized argument: %s", argv
[1]);
1036 if (!rev
.diffopt
.output_format
1037 || rev
.diffopt
.output_format
== DIFF_FORMAT_PATCH
)
1038 rev
.diffopt
.output_format
= DIFF_FORMAT_DIFFSTAT
| DIFF_FORMAT_SUMMARY
| DIFF_FORMAT_PATCH
;
1040 if (!DIFF_OPT_TST(&rev
.diffopt
, TEXT
) && !no_binary_diff
)
1041 DIFF_OPT_SET(&rev
.diffopt
, BINARY
);
1044 output_directory
= set_outdir(prefix
, output_directory
);
1046 if (output_directory
) {
1048 die("standard output, or directory, which one?");
1049 if (mkdir(output_directory
, 0777) < 0 && errno
!= EEXIST
)
1050 die_errno("Could not create directory '%s'",
1054 if (rev
.pending
.nr
== 1) {
1055 if (rev
.max_count
< 0 && !rev
.show_root_diff
) {
1057 * This is traditional behaviour of "git format-patch
1058 * origin" that prepares what the origin side still
1061 rev
.pending
.objects
[0].item
->flags
|= UNINTERESTING
;
1062 add_head_to_pending(&rev
);
1065 * Otherwise, it is "format-patch -22 HEAD", and/or
1066 * "format-patch --root HEAD". The user wants
1067 * get_revision() to do the usual traversal.
1072 * We cannot move this anywhere earlier because we do want to
1073 * know if --root was given explicitly from the comand line.
1075 rev
.show_root_diff
= 1;
1078 /* remember the range */
1080 for (i
= 0; i
< rev
.pending
.nr
; i
++) {
1081 struct object
*o
= rev
.pending
.objects
[i
].item
;
1082 if (!(o
->flags
& UNINTERESTING
))
1083 head
= (struct commit
*)o
;
1085 /* We can't generate a cover letter without any patches */
1090 if (ignore_if_in_upstream
)
1091 get_patch_ids(&rev
, &ids
, prefix
);
1094 realstdout
= xfdopen(xdup(1), "w");
1096 if (prepare_revision_walk(&rev
))
1097 die("revision walk setup failed");
1099 while ((commit
= get_revision(&rev
)) != NULL
) {
1100 if (commit
->object
.flags
& BOUNDARY
) {
1102 origin
= (boundary_count
== 1) ? commit
: NULL
;
1107 if (commit
->parents
&& commit
->parents
->next
)
1110 if (ignore_if_in_upstream
&&
1111 has_commit_patch_id(commit
, &ids
))
1115 list
= xrealloc(list
, nr
* sizeof(list
[0]));
1116 list
[nr
- 1] = commit
;
1119 if (!keep_subject
&& auto_number
&& total
> 1)
1122 rev
.total
= total
+ start_number
- 1;
1123 if (in_reply_to
|| thread
|| cover_letter
)
1124 rev
.ref_message_ids
= xcalloc(1, sizeof(struct string_list
));
1126 const char *msgid
= clean_message_id(in_reply_to
);
1127 string_list_append(msgid
, rev
.ref_message_ids
);
1129 rev
.numbered_files
= numbered_files
;
1130 rev
.patch_suffix
= fmt_patch_suffix
;
1133 gen_message_id(&rev
, "cover");
1134 make_cover_letter(&rev
, use_stdout
, numbered
, numbered_files
,
1135 origin
, nr
, list
, head
);
1139 rev
.add_signoff
= add_signoff
;
1143 rev
.nr
= total
- nr
+ (start_number
- 1);
1144 /* Make the second and subsequent mails replies to the first */
1146 /* Have we already had a message ID? */
1147 if (rev
.message_id
) {
1149 * For deep threading: make every mail
1150 * a reply to the previous one, no
1151 * matter what other options are set.
1153 * For shallow threading:
1155 * Without --cover-letter and
1156 * --in-reply-to, make every mail a
1157 * reply to the one before.
1159 * With --in-reply-to but no
1160 * --cover-letter, make every mail a
1161 * reply to the <reply-to>.
1163 * With --cover-letter, make every
1164 * mail but the cover letter a reply
1165 * to the cover letter. The cover
1166 * letter is a reply to the
1167 * --in-reply-to, if specified.
1169 if (thread
== THREAD_SHALLOW
1170 && rev
.ref_message_ids
->nr
> 0
1171 && (!cover_letter
|| rev
.nr
> 1))
1172 free(rev
.message_id
);
1174 string_list_append(rev
.message_id
,
1175 rev
.ref_message_ids
);
1177 gen_message_id(&rev
, sha1_to_hex(commit
->object
.sha1
));
1180 if (!use_stdout
&& reopen_stdout(numbered_files
? NULL
: commit
,
1182 die("Failed to create output files");
1183 shown
= log_tree_commit(&rev
, commit
);
1184 free(commit
->buffer
);
1185 commit
->buffer
= NULL
;
1187 /* We put one extra blank line between formatted
1188 * patches and this flag is used by log-tree code
1189 * to see if it needs to emit a LF before showing
1190 * the log; when using one file per patch, we do
1191 * not want the extra blank line.
1196 if (rev
.mime_boundary
)
1197 printf("\n--%s%s--\n\n\n",
1198 mime_boundary_leader
,
1201 printf("-- \n%s\n\n", git_version_string
);
1207 if (ignore_if_in_upstream
)
1208 free_patch_ids(&ids
);
1212 static int add_pending_commit(const char *arg
, struct rev_info
*revs
, int flags
)
1214 unsigned char sha1
[20];
1215 if (get_sha1(arg
, sha1
) == 0) {
1216 struct commit
*commit
= lookup_commit_reference(sha1
);
1218 commit
->object
.flags
|= flags
;
1219 add_pending_object(revs
, &commit
->object
, arg
);
1226 static const char cherry_usage
[] =
1227 "git cherry [-v] [<upstream> [<head> [<limit>]]]";
1228 int cmd_cherry(int argc
, const char **argv
, const char *prefix
)
1230 struct rev_info revs
;
1231 struct patch_ids ids
;
1232 struct commit
*commit
;
1233 struct commit_list
*list
= NULL
;
1234 struct branch
*current_branch
;
1235 const char *upstream
;
1236 const char *head
= "HEAD";
1237 const char *limit
= NULL
;
1240 if (argc
> 1 && !strcmp(argv
[1], "-v")) {
1246 if (argc
> 1 && !strcmp(argv
[1], "-h"))
1247 usage(cherry_usage
);
1260 current_branch
= branch_get(NULL
);
1261 if (!current_branch
|| !current_branch
->merge
1262 || !current_branch
->merge
[0]
1263 || !current_branch
->merge
[0]->dst
) {
1264 fprintf(stderr
, "Could not find a tracked"
1265 " remote branch, please"
1266 " specify <upstream> manually.\n");
1267 usage(cherry_usage
);
1270 upstream
= current_branch
->merge
[0]->dst
;
1273 init_revisions(&revs
, prefix
);
1275 revs
.combine_merges
= 0;
1276 revs
.ignore_merges
= 1;
1277 DIFF_OPT_SET(&revs
.diffopt
, RECURSIVE
);
1279 if (add_pending_commit(head
, &revs
, 0))
1280 die("Unknown commit %s", head
);
1281 if (add_pending_commit(upstream
, &revs
, UNINTERESTING
))
1282 die("Unknown commit %s", upstream
);
1284 /* Don't say anything if head and upstream are the same. */
1285 if (revs
.pending
.nr
== 2) {
1286 struct object_array_entry
*o
= revs
.pending
.objects
;
1287 if (hashcmp(o
[0].item
->sha1
, o
[1].item
->sha1
) == 0)
1291 get_patch_ids(&revs
, &ids
, prefix
);
1293 if (limit
&& add_pending_commit(limit
, &revs
, UNINTERESTING
))
1294 die("Unknown commit %s", limit
);
1296 /* reverse the list of commits */
1297 if (prepare_revision_walk(&revs
))
1298 die("revision walk setup failed");
1299 while ((commit
= get_revision(&revs
)) != NULL
) {
1301 if (commit
->parents
&& commit
->parents
->next
)
1304 commit_list_insert(commit
, &list
);
1310 commit
= list
->item
;
1311 if (has_commit_patch_id(commit
, &ids
))
1315 struct strbuf buf
= STRBUF_INIT
;
1316 struct pretty_print_context ctx
= {0};
1317 pretty_print_commit(CMIT_FMT_ONELINE
, commit
,
1319 printf("%c %s %s\n", sign
,
1320 sha1_to_hex(commit
->object
.sha1
), buf
.buf
);
1321 strbuf_release(&buf
);
1324 printf("%c %s\n", sign
,
1325 sha1_to_hex(commit
->object
.sha1
));
1331 free_patch_ids(&ids
);