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"
22 /* Set a default date-time format for git log ("log.date" config variable) */
23 static const char *default_date_mode
= NULL
;
25 static int default_show_root
= 1;
26 static const char *fmt_patch_subject_prefix
= "PATCH";
27 static const char *fmt_pretty
;
29 static void cmd_log_init(int argc
, const char **argv
, const char *prefix
,
34 rev
->abbrev
= DEFAULT_ABBREV
;
35 rev
->commit_format
= CMIT_FMT_DEFAULT
;
37 get_commit_format(fmt_pretty
, rev
);
38 rev
->verbose_header
= 1;
39 DIFF_OPT_SET(&rev
->diffopt
, RECURSIVE
);
40 rev
->show_root_diff
= default_show_root
;
41 rev
->subject_prefix
= fmt_patch_subject_prefix
;
42 DIFF_OPT_SET(&rev
->diffopt
, ALLOW_TEXTCONV
);
44 if (default_date_mode
)
45 rev
->date_mode
= parse_date_format(default_date_mode
);
47 argc
= setup_revisions(argc
, argv
, rev
, "HEAD");
49 if (rev
->diffopt
.pickaxe
|| rev
->diffopt
.filter
)
50 rev
->always_show_header
= 0;
51 if (DIFF_OPT_TST(&rev
->diffopt
, FOLLOW_RENAMES
)) {
52 rev
->always_show_header
= 0;
53 if (rev
->diffopt
.nr_paths
!= 1)
54 usage("git logs can only follow renames on one pathname at a time");
56 for (i
= 1; i
< argc
; i
++) {
57 const char *arg
= argv
[i
];
58 if (!strcmp(arg
, "--decorate")) {
59 load_ref_decorations();
60 rev
->show_decorations
= 1;
61 } else if (!strcmp(arg
, "--source")) {
64 die("unrecognized argument: %s", arg
);
69 * This gives a rough estimate for how many commits we
70 * will print out in the list.
72 static int estimate_commit_count(struct rev_info
*rev
, struct commit_list
*list
)
77 struct commit
*commit
= list
->item
;
78 unsigned int flags
= commit
->object
.flags
;
80 if (!(flags
& (TREESAME
| UNINTERESTING
)))
86 static void show_early_header(struct rev_info
*rev
, const char *stage
, int nr
)
90 if (rev
->commit_format
!= CMIT_FMT_ONELINE
)
91 putchar(rev
->diffopt
.line_termination
);
93 printf("Final output: %d %s\n", nr
, stage
);
96 struct itimerval early_output_timer
;
98 static void log_show_early(struct rev_info
*revs
, struct commit_list
*list
)
100 int i
= revs
->early_output
;
103 sort_in_topological_order(&list
, revs
->lifo
);
105 struct commit
*commit
= list
->item
;
106 switch (simplify_commit(revs
, commit
)) {
109 int n
= estimate_commit_count(revs
, list
);
110 show_early_header(revs
, "incomplete", n
);
113 log_tree_commit(revs
, commit
);
124 /* Did we already get enough commits for the early output? */
129 * ..if no, then repeat it twice a second until we
132 * NOTE! We don't use "it_interval", because if the
133 * reader isn't listening, we want our output to be
134 * throttled by the writing, and not have the timer
135 * trigger every second even if we're blocked on a
138 early_output_timer
.it_value
.tv_sec
= 0;
139 early_output_timer
.it_value
.tv_usec
= 500000;
140 setitimer(ITIMER_REAL
, &early_output_timer
, NULL
);
143 static void early_output(int signal
)
145 show_early_output
= log_show_early
;
148 static void setup_early_output(struct rev_info
*rev
)
153 * Set up the signal handler, minimally intrusively:
154 * we only set a single volatile integer word (not
155 * using sigatomic_t - trying to avoid unnecessary
156 * system dependencies and headers), and using
159 memset(&sa
, 0, sizeof(sa
));
160 sa
.sa_handler
= early_output
;
161 sigemptyset(&sa
.sa_mask
);
162 sa
.sa_flags
= SA_RESTART
;
163 sigaction(SIGALRM
, &sa
, NULL
);
166 * If we can get the whole output in less than a
167 * tenth of a second, don't even bother doing the
168 * early-output thing..
170 * This is a one-time-only trigger.
172 early_output_timer
.it_value
.tv_sec
= 0;
173 early_output_timer
.it_value
.tv_usec
= 100000;
174 setitimer(ITIMER_REAL
, &early_output_timer
, NULL
);
177 static void finish_early_output(struct rev_info
*rev
)
179 int n
= estimate_commit_count(rev
, rev
->commits
);
180 signal(SIGALRM
, SIG_IGN
);
181 show_early_header(rev
, "done", n
);
184 static int cmd_log_walk(struct rev_info
*rev
)
186 struct commit
*commit
;
188 if (rev
->early_output
)
189 setup_early_output(rev
);
191 if (prepare_revision_walk(rev
))
192 die("revision walk setup failed");
194 if (rev
->early_output
)
195 finish_early_output(rev
);
198 * For --check and --exit-code, the exit code is based on CHECK_FAILED
199 * and HAS_CHANGES being accumulated in rev->diffopt, so be careful to
200 * retain that state information if replacing rev->diffopt in this loop
202 while ((commit
= get_revision(rev
)) != NULL
) {
203 log_tree_commit(rev
, commit
);
204 if (!rev
->reflog_info
) {
205 /* we allow cycles in reflog ancestry */
206 free(commit
->buffer
);
207 commit
->buffer
= NULL
;
209 free_commit_list(commit
->parents
);
210 commit
->parents
= NULL
;
212 if (rev
->diffopt
.output_format
& DIFF_FORMAT_CHECKDIFF
&&
213 DIFF_OPT_TST(&rev
->diffopt
, CHECK_FAILED
)) {
216 return diff_result_code(&rev
->diffopt
, 0);
219 static int git_log_config(const char *var
, const char *value
, void *cb
)
221 if (!strcmp(var
, "format.pretty"))
222 return git_config_string(&fmt_pretty
, var
, value
);
223 if (!strcmp(var
, "format.subjectprefix"))
224 return git_config_string(&fmt_patch_subject_prefix
, var
, value
);
225 if (!strcmp(var
, "log.date"))
226 return git_config_string(&default_date_mode
, var
, value
);
227 if (!strcmp(var
, "log.showroot")) {
228 default_show_root
= git_config_bool(var
, value
);
231 return git_diff_ui_config(var
, value
, cb
);
234 int cmd_whatchanged(int argc
, const char **argv
, const char *prefix
)
238 git_config(git_log_config
, NULL
);
240 if (diff_use_color_default
== -1)
241 diff_use_color_default
= git_use_color_default
;
243 init_revisions(&rev
, prefix
);
245 rev
.simplify_history
= 0;
246 cmd_log_init(argc
, argv
, prefix
, &rev
);
247 if (!rev
.diffopt
.output_format
)
248 rev
.diffopt
.output_format
= DIFF_FORMAT_RAW
;
249 return cmd_log_walk(&rev
);
252 static void show_tagger(char *buf
, int len
, struct rev_info
*rev
)
254 struct strbuf out
= STRBUF_INIT
;
256 pp_user_info("Tagger", rev
->commit_format
, &out
, buf
, rev
->date_mode
,
257 git_log_output_encoding
?
258 git_log_output_encoding
: git_commit_encoding
);
259 printf("%s\n", out
.buf
);
260 strbuf_release(&out
);
263 static int show_object(const unsigned char *sha1
, int show_tag_object
,
264 struct rev_info
*rev
)
267 enum object_type type
;
268 char *buf
= read_sha1_file(sha1
, &type
, &size
);
272 return error("Could not read object %s", sha1_to_hex(sha1
));
275 while (offset
< size
&& buf
[offset
] != '\n') {
276 int new_offset
= offset
+ 1;
277 while (new_offset
< size
&& buf
[new_offset
++] != '\n')
279 if (!prefixcmp(buf
+ offset
, "tagger "))
280 show_tagger(buf
+ offset
+ 7,
281 new_offset
- offset
- 7, rev
);
286 fwrite(buf
+ offset
, size
- offset
, 1, stdout
);
291 static int show_tree_object(const unsigned char *sha1
,
292 const char *base
, int baselen
,
293 const char *pathname
, unsigned mode
, int stage
, void *context
)
295 printf("%s%s\n", pathname
, S_ISDIR(mode
) ? "/" : "");
299 int cmd_show(int argc
, const char **argv
, const char *prefix
)
302 struct object_array_entry
*objects
;
303 int i
, count
, ret
= 0;
305 git_config(git_log_config
, NULL
);
307 if (diff_use_color_default
== -1)
308 diff_use_color_default
= git_use_color_default
;
310 init_revisions(&rev
, prefix
);
312 rev
.combine_merges
= 1;
313 rev
.dense_combined_merges
= 1;
314 rev
.always_show_header
= 1;
315 rev
.ignore_merges
= 0;
317 cmd_log_init(argc
, argv
, prefix
, &rev
);
319 count
= rev
.pending
.nr
;
320 objects
= rev
.pending
.objects
;
321 for (i
= 0; i
< count
&& !ret
; i
++) {
322 struct object
*o
= objects
[i
].item
;
323 const char *name
= objects
[i
].name
;
326 ret
= show_object(o
->sha1
, 0, NULL
);
329 struct tag
*t
= (struct tag
*)o
;
331 printf("%stag %s%s\n",
332 diff_get_color_opt(&rev
.diffopt
, DIFF_COMMIT
),
334 diff_get_color_opt(&rev
.diffopt
, DIFF_RESET
));
335 ret
= show_object(o
->sha1
, 1, &rev
);
338 o
= parse_object(t
->tagged
->sha1
);
340 ret
= error("Could not read object %s",
341 sha1_to_hex(t
->tagged
->sha1
));
347 printf("%stree %s%s\n\n",
348 diff_get_color_opt(&rev
.diffopt
, DIFF_COMMIT
),
350 diff_get_color_opt(&rev
.diffopt
, DIFF_RESET
));
351 read_tree_recursive((struct tree
*)o
, "", 0, 0, NULL
,
352 show_tree_object
, NULL
);
355 rev
.pending
.nr
= rev
.pending
.alloc
= 0;
356 rev
.pending
.objects
= NULL
;
357 add_object_array(o
, name
, &rev
.pending
);
358 ret
= cmd_log_walk(&rev
);
361 ret
= error("Unknown type: %d", o
->type
);
369 * This is equivalent to "git log -g --abbrev-commit --pretty=oneline"
371 int cmd_log_reflog(int argc
, const char **argv
, const char *prefix
)
375 git_config(git_log_config
, NULL
);
377 if (diff_use_color_default
== -1)
378 diff_use_color_default
= git_use_color_default
;
380 init_revisions(&rev
, prefix
);
381 init_reflog_walk(&rev
.reflog_info
);
382 rev
.abbrev_commit
= 1;
383 rev
.verbose_header
= 1;
384 cmd_log_init(argc
, argv
, prefix
, &rev
);
387 * This means that we override whatever commit format the user gave
388 * on the cmd line. Sad, but cmd_log_init() currently doesn't
389 * allow us to set a different default.
391 rev
.commit_format
= CMIT_FMT_ONELINE
;
392 rev
.use_terminator
= 1;
393 rev
.always_show_header
= 1;
396 * We get called through "git reflog", so unlike the other log
397 * routines, we need to set up our pager manually..
401 return cmd_log_walk(&rev
);
404 int cmd_log(int argc
, const char **argv
, const char *prefix
)
408 git_config(git_log_config
, NULL
);
410 if (diff_use_color_default
== -1)
411 diff_use_color_default
= git_use_color_default
;
413 init_revisions(&rev
, prefix
);
414 rev
.always_show_header
= 1;
415 cmd_log_init(argc
, argv
, prefix
, &rev
);
416 return cmd_log_walk(&rev
);
420 #define FORMAT_PATCH_NAME_MAX 64
422 static int istitlechar(char c
)
424 return (c
>= 'a' && c
<= 'z') || (c
>= 'A' && c
<= 'Z') ||
425 (c
>= '0' && c
<= '9') || c
== '.' || c
== '_';
428 static const char *fmt_patch_suffix
= ".patch";
429 static int numbered
= 0;
430 static int auto_number
= 1;
432 static char *default_attach
= NULL
;
434 static char **extra_hdr
;
435 static int extra_hdr_nr
;
436 static int extra_hdr_alloc
;
438 static char **extra_to
;
439 static int extra_to_nr
;
440 static int extra_to_alloc
;
442 static char **extra_cc
;
443 static int extra_cc_nr
;
444 static int extra_cc_alloc
;
446 static void add_header(const char *value
)
448 int len
= strlen(value
);
449 while (len
&& value
[len
- 1] == '\n')
451 if (!strncasecmp(value
, "to: ", 4)) {
452 ALLOC_GROW(extra_to
, extra_to_nr
+ 1, extra_to_alloc
);
453 extra_to
[extra_to_nr
++] = xstrndup(value
+ 4, len
- 4);
456 if (!strncasecmp(value
, "cc: ", 4)) {
457 ALLOC_GROW(extra_cc
, extra_cc_nr
+ 1, extra_cc_alloc
);
458 extra_cc
[extra_cc_nr
++] = xstrndup(value
+ 4, len
- 4);
461 ALLOC_GROW(extra_hdr
, extra_hdr_nr
+ 1, extra_hdr_alloc
);
462 extra_hdr
[extra_hdr_nr
++] = xstrndup(value
, len
);
465 #define THREAD_SHALLOW 1
466 #define THREAD_DEEP 2
467 static int thread
= 0;
469 static int git_format_config(const char *var
, const char *value
, void *cb
)
471 if (!strcmp(var
, "format.headers")) {
473 die("format.headers without value");
477 if (!strcmp(var
, "format.suffix"))
478 return git_config_string(&fmt_patch_suffix
, var
, value
);
479 if (!strcmp(var
, "format.cc")) {
481 return config_error_nonbool(var
);
482 ALLOC_GROW(extra_cc
, extra_cc_nr
+ 1, extra_cc_alloc
);
483 extra_cc
[extra_cc_nr
++] = xstrdup(value
);
486 if (!strcmp(var
, "diff.color") || !strcmp(var
, "color.diff")) {
489 if (!strcmp(var
, "format.numbered")) {
490 if (value
&& !strcasecmp(value
, "auto")) {
494 numbered
= git_config_bool(var
, value
);
495 auto_number
= auto_number
&& numbered
;
498 if (!strcmp(var
, "format.attach")) {
500 default_attach
= xstrdup(value
);
502 default_attach
= xstrdup(git_version_string
);
505 if (!strcmp(var
, "format.thread")) {
506 if (value
&& !strcasecmp(value
, "deep")) {
507 thread
= THREAD_DEEP
;
510 if (value
&& !strcasecmp(value
, "shallow")) {
511 thread
= THREAD_SHALLOW
;
514 thread
= git_config_bool(var
, value
) && THREAD_SHALLOW
;
518 return git_log_config(var
, value
, cb
);
522 static const char *get_oneline_for_filename(struct commit
*commit
,
525 static char filename
[PATH_MAX
];
528 int suffix_len
= strlen(fmt_patch_suffix
) + 1;
530 sol
= strstr(commit
->buffer
, "\n\n");
537 /* strip [PATCH] or [PATCH blabla] */
538 if (!keep_subject
&& !prefixcmp(sol
, "[PATCH")) {
539 char *eos
= strchr(sol
+ 6, ']');
541 while (isspace(*eos
))
548 j
< FORMAT_PATCH_NAME_MAX
- suffix_len
- 5 &&
549 len
< sizeof(filename
) - suffix_len
&&
550 sol
[j
] && sol
[j
] != '\n';
552 if (istitlechar(sol
[j
])) {
554 filename
[len
++] = '-';
557 filename
[len
++] = sol
[j
];
559 while (sol
[j
+ 1] == '.')
564 while (filename
[len
- 1] == '.'
565 || filename
[len
- 1] == '-')
567 filename
[len
] = '\0';
572 static FILE *realstdout
= NULL
;
573 static const char *output_directory
= NULL
;
574 static int outdir_offset
;
576 static int reopen_stdout(const char *oneline
, int nr
, struct rev_info
*rev
)
578 char filename
[PATH_MAX
];
580 int suffix_len
= strlen(fmt_patch_suffix
) + 1;
582 if (output_directory
) {
583 len
= snprintf(filename
, sizeof(filename
), "%s",
586 sizeof(filename
) - FORMAT_PATCH_NAME_MAX
- suffix_len
)
587 return error("name of output directory is too long");
588 if (filename
[len
- 1] != '/')
589 filename
[len
++] = '/';
593 len
+= sprintf(filename
+ len
, "%d", nr
);
595 len
+= sprintf(filename
+ len
, "%04d-", nr
);
596 len
+= snprintf(filename
+ len
, sizeof(filename
) - len
- 1
597 - suffix_len
, "%s", oneline
);
598 strcpy(filename
+ len
, fmt_patch_suffix
);
601 if (!DIFF_OPT_TST(&rev
->diffopt
, QUIET
))
602 fprintf(realstdout
, "%s\n", filename
+ outdir_offset
);
604 if (freopen(filename
, "w", stdout
) == NULL
)
605 return error("Cannot open patch file %s",filename
);
610 static void get_patch_ids(struct rev_info
*rev
, struct patch_ids
*ids
, const char *prefix
)
612 struct rev_info check_rev
;
613 struct commit
*commit
;
614 struct object
*o1
, *o2
;
615 unsigned flags1
, flags2
;
617 if (rev
->pending
.nr
!= 2)
618 die("Need exactly one range.");
620 o1
= rev
->pending
.objects
[0].item
;
622 o2
= rev
->pending
.objects
[1].item
;
625 if ((flags1
& UNINTERESTING
) == (flags2
& UNINTERESTING
))
630 /* given a range a..b get all patch ids for b..a */
631 init_revisions(&check_rev
, prefix
);
632 o1
->flags
^= UNINTERESTING
;
633 o2
->flags
^= UNINTERESTING
;
634 add_pending_object(&check_rev
, o1
, "o1");
635 add_pending_object(&check_rev
, o2
, "o2");
636 if (prepare_revision_walk(&check_rev
))
637 die("revision walk setup failed");
639 while ((commit
= get_revision(&check_rev
)) != NULL
) {
641 if (commit
->parents
&& commit
->parents
->next
)
644 add_commit_patch_id(commit
, ids
);
647 /* reset for next revision walk */
648 clear_commit_marks((struct commit
*)o1
,
649 SEEN
| UNINTERESTING
| SHOWN
| ADDED
);
650 clear_commit_marks((struct commit
*)o2
,
651 SEEN
| UNINTERESTING
| SHOWN
| ADDED
);
656 static void gen_message_id(struct rev_info
*info
, char *base
)
658 const char *committer
= git_committer_info(IDENT_WARN_ON_NO_NAME
);
659 const char *email_start
= strrchr(committer
, '<');
660 const char *email_end
= strrchr(committer
, '>');
661 struct strbuf buf
= STRBUF_INIT
;
662 if (!email_start
|| !email_end
|| email_start
> email_end
- 1)
663 die("Could not extract email from committer identity.");
664 strbuf_addf(&buf
, "%s.%lu.git.%.*s", base
,
665 (unsigned long) time(NULL
),
666 (int)(email_end
- email_start
- 1), email_start
+ 1);
667 info
->message_id
= strbuf_detach(&buf
, NULL
);
670 static void make_cover_letter(struct rev_info
*rev
, int use_stdout
,
671 int numbered
, int numbered_files
,
672 struct commit
*origin
,
673 int nr
, struct commit
**list
, struct commit
*head
)
675 const char *committer
;
677 const char *subject_start
= NULL
;
678 const char *body
= "*** SUBJECT HERE ***\n\n*** BLURB HERE ***\n";
680 const char *extra_headers
= rev
->extra_headers
;
682 struct strbuf sb
= STRBUF_INIT
;
684 const char *encoding
= "utf-8";
685 struct diff_options opts
;
686 int need_8bit_cte
= 0;
688 if (rev
->commit_format
!= CMIT_FMT_EMAIL
)
689 die("Cover letter needs email format");
691 if (!use_stdout
&& reopen_stdout(numbered_files
?
692 NULL
: "cover-letter", 0, rev
))
695 head_sha1
= sha1_to_hex(head
->object
.sha1
);
697 log_write_email_headers(rev
, head_sha1
, &subject_start
, &extra_headers
,
700 committer
= git_committer_info(0);
703 pp_user_info(NULL
, CMIT_FMT_EMAIL
, &sb
, committer
, DATE_RFC2822
,
705 pp_title_line(CMIT_FMT_EMAIL
, &msg
, &sb
, subject_start
, extra_headers
,
706 encoding
, need_8bit_cte
);
707 pp_remainder(CMIT_FMT_EMAIL
, &msg
, &sb
, 0);
708 printf("%s\n", sb
.buf
);
717 for (i
= 0; i
< nr
; i
++)
718 shortlog_add_commit(&log
, list
[i
]);
720 shortlog_output(&log
);
723 * We can only do diffstat with a unique reference point
728 memcpy(&opts
, &rev
->diffopt
, sizeof(opts
));
729 opts
.output_format
= DIFF_FORMAT_SUMMARY
| DIFF_FORMAT_DIFFSTAT
;
731 diff_setup_done(&opts
);
733 diff_tree_sha1(origin
->tree
->object
.sha1
,
734 head
->tree
->object
.sha1
,
742 static const char *clean_message_id(const char *msg_id
)
745 const char *a
, *z
, *m
;
748 while ((ch
= *m
) && (isspace(ch
) || (ch
== '<')))
753 if (!isspace(ch
) && (ch
!= '>'))
758 die("insane in-reply-to: %s", msg_id
);
761 return xmemdupz(a
, z
- a
);
764 static const char *set_outdir(const char *prefix
, const char *output_directory
)
766 if (output_directory
&& is_absolute_path(output_directory
))
767 return output_directory
;
769 if (!prefix
|| !*prefix
) {
770 if (output_directory
)
771 return output_directory
;
772 /* The user did not explicitly ask for "./" */
777 outdir_offset
= strlen(prefix
);
778 if (!output_directory
)
781 return xstrdup(prefix_filename(prefix
, outdir_offset
,
785 int cmd_format_patch(int argc
, const char **argv
, const char *prefix
)
787 struct commit
*commit
;
788 struct commit
**list
= NULL
;
790 int nr
= 0, total
, i
, j
;
792 int start_number
= -1;
793 int keep_subject
= 0;
794 int numbered_files
= 0; /* _just_ numbers */
795 int subject_prefix
= 0;
796 int ignore_if_in_upstream
= 0;
797 int cover_letter
= 0;
798 int boundary_count
= 0;
799 int no_binary_diff
= 0;
800 struct commit
*origin
= NULL
, *head
= NULL
;
801 const char *in_reply_to
= NULL
;
802 struct patch_ids ids
;
803 char *add_signoff
= NULL
;
804 struct strbuf buf
= STRBUF_INIT
;
806 git_config(git_format_config
, NULL
);
807 init_revisions(&rev
, prefix
);
808 rev
.commit_format
= CMIT_FMT_EMAIL
;
809 rev
.verbose_header
= 1;
811 rev
.combine_merges
= 0;
812 rev
.ignore_merges
= 1;
813 DIFF_OPT_SET(&rev
.diffopt
, RECURSIVE
);
815 rev
.subject_prefix
= fmt_patch_subject_prefix
;
817 if (default_attach
) {
818 rev
.mime_boundary
= default_attach
;
823 * Parse the arguments before setup_revisions(), or something
824 * like "git format-patch -o a123 HEAD^.." may fail; a123 is
825 * possibly a valid SHA1.
827 for (i
= 1, j
= 1; i
< argc
; i
++) {
828 if (!strcmp(argv
[i
], "--stdout"))
830 else if (!strcmp(argv
[i
], "-n") ||
831 !strcmp(argv
[i
], "--numbered"))
833 else if (!strcmp(argv
[i
], "-N") ||
834 !strcmp(argv
[i
], "--no-numbered")) {
838 else if (!prefixcmp(argv
[i
], "--start-number="))
839 start_number
= strtol(argv
[i
] + 15, NULL
, 10);
840 else if (!strcmp(argv
[i
], "--numbered-files"))
842 else if (!strcmp(argv
[i
], "--start-number")) {
845 die("Need a number for --start-number");
846 start_number
= strtol(argv
[i
], NULL
, 10);
848 else if (!prefixcmp(argv
[i
], "--cc=")) {
849 ALLOC_GROW(extra_cc
, extra_cc_nr
+ 1, extra_cc_alloc
);
850 extra_cc
[extra_cc_nr
++] = xstrdup(argv
[i
] + 5);
852 else if (!strcmp(argv
[i
], "-k") ||
853 !strcmp(argv
[i
], "--keep-subject")) {
857 else if (!strcmp(argv
[i
], "--output-directory") ||
858 !strcmp(argv
[i
], "-o")) {
861 die("Which directory?");
862 if (output_directory
)
863 die("Two output directories?");
864 output_directory
= argv
[i
];
866 else if (!strcmp(argv
[i
], "--signoff") ||
867 !strcmp(argv
[i
], "-s")) {
868 const char *committer
;
870 committer
= git_committer_info(IDENT_ERROR_ON_NO_NAME
);
871 endpos
= strchr(committer
, '>');
873 die("bogus committer info %s", committer
);
874 add_signoff
= xmemdupz(committer
, endpos
- committer
+ 1);
876 else if (!strcmp(argv
[i
], "--attach")) {
877 rev
.mime_boundary
= git_version_string
;
880 else if (!prefixcmp(argv
[i
], "--attach=")) {
881 rev
.mime_boundary
= argv
[i
] + 9;
884 else if (!strcmp(argv
[i
], "--no-attach")) {
885 rev
.mime_boundary
= NULL
;
888 else if (!strcmp(argv
[i
], "--inline")) {
889 rev
.mime_boundary
= git_version_string
;
892 else if (!prefixcmp(argv
[i
], "--inline=")) {
893 rev
.mime_boundary
= argv
[i
] + 9;
896 else if (!strcmp(argv
[i
], "--ignore-if-in-upstream"))
897 ignore_if_in_upstream
= 1;
898 else if (!strcmp(argv
[i
], "--thread")
899 || !strcmp(argv
[i
], "--thread=shallow"))
900 thread
= THREAD_SHALLOW
;
901 else if (!strcmp(argv
[i
], "--thread=deep"))
902 thread
= THREAD_DEEP
;
903 else if (!strcmp(argv
[i
], "--no-thread"))
905 else if (!prefixcmp(argv
[i
], "--in-reply-to="))
906 in_reply_to
= argv
[i
] + 14;
907 else if (!strcmp(argv
[i
], "--in-reply-to")) {
910 die("Need a Message-Id for --in-reply-to");
911 in_reply_to
= argv
[i
];
912 } else if (!prefixcmp(argv
[i
], "--subject-prefix=")) {
914 rev
.subject_prefix
= argv
[i
] + 17;
915 } else if (!prefixcmp(argv
[i
], "--suffix="))
916 fmt_patch_suffix
= argv
[i
] + 9;
917 else if (!strcmp(argv
[i
], "--cover-letter"))
919 else if (!strcmp(argv
[i
], "--no-binary"))
926 for (i
= 0; i
< extra_hdr_nr
; i
++) {
927 strbuf_addstr(&buf
, extra_hdr
[i
]);
928 strbuf_addch(&buf
, '\n');
932 strbuf_addstr(&buf
, "To: ");
933 for (i
= 0; i
< extra_to_nr
; i
++) {
935 strbuf_addstr(&buf
, " ");
936 strbuf_addstr(&buf
, extra_to
[i
]);
937 if (i
+ 1 < extra_to_nr
)
938 strbuf_addch(&buf
, ',');
939 strbuf_addch(&buf
, '\n');
943 strbuf_addstr(&buf
, "Cc: ");
944 for (i
= 0; i
< extra_cc_nr
; i
++) {
946 strbuf_addstr(&buf
, " ");
947 strbuf_addstr(&buf
, extra_cc
[i
]);
948 if (i
+ 1 < extra_cc_nr
)
949 strbuf_addch(&buf
, ',');
950 strbuf_addch(&buf
, '\n');
953 rev
.extra_headers
= strbuf_detach(&buf
, 0);
955 if (start_number
< 0)
957 if (numbered
&& keep_subject
)
958 die ("-n and -k are mutually exclusive.");
959 if (keep_subject
&& subject_prefix
)
960 die ("--subject-prefix and -k are mutually exclusive.");
961 if (numbered_files
&& use_stdout
)
962 die ("--numbered-files and --stdout are mutually exclusive.");
964 argc
= setup_revisions(argc
, argv
, &rev
, "HEAD");
966 die ("unrecognized argument: %s", argv
[1]);
968 if (!rev
.diffopt
.output_format
969 || rev
.diffopt
.output_format
== DIFF_FORMAT_PATCH
)
970 rev
.diffopt
.output_format
= DIFF_FORMAT_DIFFSTAT
| DIFF_FORMAT_SUMMARY
| DIFF_FORMAT_PATCH
;
972 if (!DIFF_OPT_TST(&rev
.diffopt
, TEXT
) && !no_binary_diff
)
973 DIFF_OPT_SET(&rev
.diffopt
, BINARY
);
976 output_directory
= set_outdir(prefix
, output_directory
);
978 if (output_directory
) {
980 die("standard output, or directory, which one?");
981 if (mkdir(output_directory
, 0777) < 0 && errno
!= EEXIST
)
982 die("Could not create directory %s",
986 if (rev
.pending
.nr
== 1) {
987 if (rev
.max_count
< 0 && !rev
.show_root_diff
) {
989 * This is traditional behaviour of "git format-patch
990 * origin" that prepares what the origin side still
993 rev
.pending
.objects
[0].item
->flags
|= UNINTERESTING
;
994 add_head_to_pending(&rev
);
997 * Otherwise, it is "format-patch -22 HEAD", and/or
998 * "format-patch --root HEAD". The user wants
999 * get_revision() to do the usual traversal.
1004 * We cannot move this anywhere earlier because we do want to
1005 * know if --root was given explicitly from the comand line.
1007 rev
.show_root_diff
= 1;
1010 /* remember the range */
1012 for (i
= 0; i
< rev
.pending
.nr
; i
++) {
1013 struct object
*o
= rev
.pending
.objects
[i
].item
;
1014 if (!(o
->flags
& UNINTERESTING
))
1015 head
= (struct commit
*)o
;
1017 /* We can't generate a cover letter without any patches */
1022 if (ignore_if_in_upstream
)
1023 get_patch_ids(&rev
, &ids
, prefix
);
1026 realstdout
= xfdopen(xdup(1), "w");
1028 if (prepare_revision_walk(&rev
))
1029 die("revision walk setup failed");
1031 while ((commit
= get_revision(&rev
)) != NULL
) {
1032 if (commit
->object
.flags
& BOUNDARY
) {
1034 origin
= (boundary_count
== 1) ? commit
: NULL
;
1039 if (commit
->parents
&& commit
->parents
->next
)
1042 if (ignore_if_in_upstream
&&
1043 has_commit_patch_id(commit
, &ids
))
1047 list
= xrealloc(list
, nr
* sizeof(list
[0]));
1048 list
[nr
- 1] = commit
;
1051 if (!keep_subject
&& auto_number
&& total
> 1)
1054 rev
.total
= total
+ start_number
- 1;
1055 if (in_reply_to
|| thread
|| cover_letter
)
1056 rev
.ref_message_ids
= xcalloc(1, sizeof(struct string_list
));
1058 const char *msgid
= clean_message_id(in_reply_to
);
1059 string_list_append(msgid
, rev
.ref_message_ids
);
1063 gen_message_id(&rev
, "cover");
1064 make_cover_letter(&rev
, use_stdout
, numbered
, numbered_files
,
1065 origin
, nr
, list
, head
);
1069 rev
.add_signoff
= add_signoff
;
1073 rev
.nr
= total
- nr
+ (start_number
- 1);
1074 /* Make the second and subsequent mails replies to the first */
1076 /* Have we already had a message ID? */
1077 if (rev
.message_id
) {
1079 * For deep threading: make every mail
1080 * a reply to the previous one, no
1081 * matter what other options are set.
1083 * For shallow threading:
1085 * Without --cover-letter and
1086 * --in-reply-to, make every mail a
1087 * reply to the one before.
1089 * With --in-reply-to but no
1090 * --cover-letter, make every mail a
1091 * reply to the <reply-to>.
1093 * With --cover-letter, make every
1094 * mail but the cover letter a reply
1095 * to the cover letter. The cover
1096 * letter is a reply to the
1097 * --in-reply-to, if specified.
1099 if (thread
== THREAD_SHALLOW
1100 && rev
.ref_message_ids
->nr
> 0
1101 && (!cover_letter
|| rev
.nr
> 1))
1102 free(rev
.message_id
);
1104 string_list_append(rev
.message_id
,
1105 rev
.ref_message_ids
);
1107 gen_message_id(&rev
, sha1_to_hex(commit
->object
.sha1
));
1109 if (!use_stdout
&& reopen_stdout(numbered_files
? NULL
:
1110 get_oneline_for_filename(commit
, keep_subject
),
1112 die("Failed to create output files");
1113 shown
= log_tree_commit(&rev
, commit
);
1114 free(commit
->buffer
);
1115 commit
->buffer
= NULL
;
1117 /* We put one extra blank line between formatted
1118 * patches and this flag is used by log-tree code
1119 * to see if it needs to emit a LF before showing
1120 * the log; when using one file per patch, we do
1121 * not want the extra blank line.
1126 if (rev
.mime_boundary
)
1127 printf("\n--%s%s--\n\n\n",
1128 mime_boundary_leader
,
1131 printf("-- \n%s\n\n", git_version_string
);
1137 if (ignore_if_in_upstream
)
1138 free_patch_ids(&ids
);
1142 static int add_pending_commit(const char *arg
, struct rev_info
*revs
, int flags
)
1144 unsigned char sha1
[20];
1145 if (get_sha1(arg
, sha1
) == 0) {
1146 struct commit
*commit
= lookup_commit_reference(sha1
);
1148 commit
->object
.flags
|= flags
;
1149 add_pending_object(revs
, &commit
->object
, arg
);
1156 static const char cherry_usage
[] =
1157 "git cherry [-v] [<upstream> [<head> [<limit>]]]";
1158 int cmd_cherry(int argc
, const char **argv
, const char *prefix
)
1160 struct rev_info revs
;
1161 struct patch_ids ids
;
1162 struct commit
*commit
;
1163 struct commit_list
*list
= NULL
;
1164 struct branch
*current_branch
;
1165 const char *upstream
;
1166 const char *head
= "HEAD";
1167 const char *limit
= NULL
;
1170 if (argc
> 1 && !strcmp(argv
[1], "-v")) {
1187 current_branch
= branch_get(NULL
);
1188 if (!current_branch
|| !current_branch
->merge
1189 || !current_branch
->merge
[0]
1190 || !current_branch
->merge
[0]->dst
) {
1191 fprintf(stderr
, "Could not find a tracked"
1192 " remote branch, please"
1193 " specify <upstream> manually.\n");
1194 usage(cherry_usage
);
1197 upstream
= current_branch
->merge
[0]->dst
;
1200 init_revisions(&revs
, prefix
);
1202 revs
.combine_merges
= 0;
1203 revs
.ignore_merges
= 1;
1204 DIFF_OPT_SET(&revs
.diffopt
, RECURSIVE
);
1206 if (add_pending_commit(head
, &revs
, 0))
1207 die("Unknown commit %s", head
);
1208 if (add_pending_commit(upstream
, &revs
, UNINTERESTING
))
1209 die("Unknown commit %s", upstream
);
1211 /* Don't say anything if head and upstream are the same. */
1212 if (revs
.pending
.nr
== 2) {
1213 struct object_array_entry
*o
= revs
.pending
.objects
;
1214 if (hashcmp(o
[0].item
->sha1
, o
[1].item
->sha1
) == 0)
1218 get_patch_ids(&revs
, &ids
, prefix
);
1220 if (limit
&& add_pending_commit(limit
, &revs
, UNINTERESTING
))
1221 die("Unknown commit %s", limit
);
1223 /* reverse the list of commits */
1224 if (prepare_revision_walk(&revs
))
1225 die("revision walk setup failed");
1226 while ((commit
= get_revision(&revs
)) != NULL
) {
1228 if (commit
->parents
&& commit
->parents
->next
)
1231 commit_list_insert(commit
, &list
);
1237 commit
= list
->item
;
1238 if (has_commit_patch_id(commit
, &ids
))
1242 struct strbuf buf
= STRBUF_INIT
;
1243 pretty_print_commit(CMIT_FMT_ONELINE
, commit
,
1244 &buf
, 0, NULL
, NULL
, 0, 0);
1245 printf("%c %s %s\n", sign
,
1246 sha1_to_hex(commit
->object
.sha1
), buf
.buf
);
1247 strbuf_release(&buf
);
1250 printf("%c %s\n", sign
,
1251 sha1_to_hex(commit
->object
.sha1
));
1257 free_patch_ids(&ids
);