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"
18 #include "run-command.h"
21 /* Set a default date-time format for git log ("log.date" config variable) */
22 static const char *default_date_mode
= NULL
;
24 static int default_show_root
= 1;
25 static const char *fmt_patch_subject_prefix
= "PATCH";
26 static const char *fmt_pretty
;
28 static void add_name_decoration(const char *prefix
, const char *name
, struct object
*obj
)
30 int plen
= strlen(prefix
);
31 int nlen
= strlen(name
);
32 struct name_decoration
*res
= xmalloc(sizeof(struct name_decoration
) + plen
+ nlen
);
33 memcpy(res
->name
, prefix
, plen
);
34 memcpy(res
->name
+ plen
, name
, nlen
+ 1);
35 res
->next
= add_decoration(&name_decoration
, obj
, res
);
38 static int add_ref_decoration(const char *refname
, const unsigned char *sha1
, int flags
, void *cb_data
)
40 struct object
*obj
= parse_object(sha1
);
43 add_name_decoration("", refname
, obj
);
44 while (obj
->type
== OBJ_TAG
) {
45 obj
= ((struct tag
*)obj
)->tagged
;
48 add_name_decoration("tag: ", refname
, obj
);
53 static void cmd_log_init(int argc
, const char **argv
, const char *prefix
,
59 rev
->abbrev
= DEFAULT_ABBREV
;
60 rev
->commit_format
= CMIT_FMT_DEFAULT
;
62 get_commit_format(fmt_pretty
, rev
);
63 rev
->verbose_header
= 1;
64 DIFF_OPT_SET(&rev
->diffopt
, RECURSIVE
);
65 rev
->show_root_diff
= default_show_root
;
66 rev
->subject_prefix
= fmt_patch_subject_prefix
;
68 if (default_date_mode
)
69 rev
->date_mode
= parse_date_format(default_date_mode
);
71 argc
= setup_revisions(argc
, argv
, rev
, "HEAD");
73 if (rev
->diffopt
.pickaxe
|| rev
->diffopt
.filter
)
74 rev
->always_show_header
= 0;
75 if (DIFF_OPT_TST(&rev
->diffopt
, FOLLOW_RENAMES
)) {
76 rev
->always_show_header
= 0;
77 if (rev
->diffopt
.nr_paths
!= 1)
78 usage("git logs can only follow renames on one pathname at a time");
80 for (i
= 1; i
< argc
; i
++) {
81 const char *arg
= argv
[i
];
82 if (!strcmp(arg
, "--decorate")) {
84 for_each_ref(add_ref_decoration
, NULL
);
87 die("unrecognized argument: %s", arg
);
92 * This gives a rough estimate for how many commits we
93 * will print out in the list.
95 static int estimate_commit_count(struct rev_info
*rev
, struct commit_list
*list
)
100 struct commit
*commit
= list
->item
;
101 unsigned int flags
= commit
->object
.flags
;
103 if (!(flags
& (TREESAME
| UNINTERESTING
)))
109 static void show_early_header(struct rev_info
*rev
, const char *stage
, int nr
)
111 if (rev
->shown_one
) {
113 if (rev
->commit_format
!= CMIT_FMT_ONELINE
)
114 putchar(rev
->diffopt
.line_termination
);
116 printf("Final output: %d %s\n", nr
, stage
);
119 struct itimerval early_output_timer
;
121 static void log_show_early(struct rev_info
*revs
, struct commit_list
*list
)
123 int i
= revs
->early_output
;
126 sort_in_topological_order(&list
, revs
->lifo
);
128 struct commit
*commit
= list
->item
;
129 switch (simplify_commit(revs
, commit
)) {
132 int n
= estimate_commit_count(revs
, list
);
133 show_early_header(revs
, "incomplete", n
);
136 log_tree_commit(revs
, commit
);
147 /* Did we already get enough commits for the early output? */
152 * ..if no, then repeat it twice a second until we
155 * NOTE! We don't use "it_interval", because if the
156 * reader isn't listening, we want our output to be
157 * throttled by the writing, and not have the timer
158 * trigger every second even if we're blocked on a
161 early_output_timer
.it_value
.tv_sec
= 0;
162 early_output_timer
.it_value
.tv_usec
= 500000;
163 setitimer(ITIMER_REAL
, &early_output_timer
, NULL
);
166 static void early_output(int signal
)
168 show_early_output
= log_show_early
;
171 static void setup_early_output(struct rev_info
*rev
)
176 * Set up the signal handler, minimally intrusively:
177 * we only set a single volatile integer word (not
178 * using sigatomic_t - trying to avoid unnecessary
179 * system dependencies and headers), and using
182 memset(&sa
, 0, sizeof(sa
));
183 sa
.sa_handler
= early_output
;
184 sigemptyset(&sa
.sa_mask
);
185 sa
.sa_flags
= SA_RESTART
;
186 sigaction(SIGALRM
, &sa
, NULL
);
189 * If we can get the whole output in less than a
190 * tenth of a second, don't even bother doing the
191 * early-output thing..
193 * This is a one-time-only trigger.
195 early_output_timer
.it_value
.tv_sec
= 0;
196 early_output_timer
.it_value
.tv_usec
= 100000;
197 setitimer(ITIMER_REAL
, &early_output_timer
, NULL
);
200 static void finish_early_output(struct rev_info
*rev
)
202 int n
= estimate_commit_count(rev
, rev
->commits
);
203 signal(SIGALRM
, SIG_IGN
);
204 show_early_header(rev
, "done", n
);
207 static int cmd_log_walk(struct rev_info
*rev
)
209 struct commit
*commit
;
211 if (rev
->early_output
)
212 setup_early_output(rev
);
214 if (prepare_revision_walk(rev
))
215 die("revision walk setup failed");
217 if (rev
->early_output
)
218 finish_early_output(rev
);
220 while ((commit
= get_revision(rev
)) != NULL
) {
221 log_tree_commit(rev
, commit
);
222 if (!rev
->reflog_info
) {
223 /* we allow cycles in reflog ancestry */
224 free(commit
->buffer
);
225 commit
->buffer
= NULL
;
227 free_commit_list(commit
->parents
);
228 commit
->parents
= NULL
;
233 static int git_log_config(const char *var
, const char *value
, void *cb
)
235 if (!strcmp(var
, "format.pretty"))
236 return git_config_string(&fmt_pretty
, var
, value
);
237 if (!strcmp(var
, "format.subjectprefix"))
238 return git_config_string(&fmt_patch_subject_prefix
, var
, value
);
239 if (!strcmp(var
, "log.date"))
240 return git_config_string(&default_date_mode
, var
, value
);
241 if (!strcmp(var
, "log.showroot")) {
242 default_show_root
= git_config_bool(var
, value
);
245 return git_diff_ui_config(var
, value
, cb
);
248 int cmd_whatchanged(int argc
, const char **argv
, const char *prefix
)
252 git_config(git_log_config
, NULL
);
254 if (diff_use_color_default
== -1)
255 diff_use_color_default
= git_use_color_default
;
257 init_revisions(&rev
, prefix
);
259 rev
.simplify_history
= 0;
260 cmd_log_init(argc
, argv
, prefix
, &rev
);
261 if (!rev
.diffopt
.output_format
)
262 rev
.diffopt
.output_format
= DIFF_FORMAT_RAW
;
263 return cmd_log_walk(&rev
);
266 static void show_tagger(char *buf
, int len
, struct rev_info
*rev
)
272 email_end
= memchr(buf
, '>', len
);
278 date
= strtoul(p
, &p
, 10);
281 tz
= (int)strtol(p
, NULL
, 10);
282 printf("Tagger: %.*s\nDate: %s\n", (int)(email_end
- buf
), buf
,
283 show_date(date
, tz
, rev
->date_mode
));
286 static int show_object(const unsigned char *sha1
, int show_tag_object
,
287 struct rev_info
*rev
)
290 enum object_type type
;
291 char *buf
= read_sha1_file(sha1
, &type
, &size
);
295 return error("Could not read object %s", sha1_to_hex(sha1
));
298 while (offset
< size
&& buf
[offset
] != '\n') {
299 int new_offset
= offset
+ 1;
300 while (new_offset
< size
&& buf
[new_offset
++] != '\n')
302 if (!prefixcmp(buf
+ offset
, "tagger "))
303 show_tagger(buf
+ offset
+ 7,
304 new_offset
- offset
- 7, rev
);
309 fwrite(buf
+ offset
, size
- offset
, 1, stdout
);
314 static int show_tree_object(const unsigned char *sha1
,
315 const char *base
, int baselen
,
316 const char *pathname
, unsigned mode
, int stage
, void *context
)
318 printf("%s%s\n", pathname
, S_ISDIR(mode
) ? "/" : "");
322 int cmd_show(int argc
, const char **argv
, const char *prefix
)
325 struct object_array_entry
*objects
;
326 int i
, count
, ret
= 0;
328 git_config(git_log_config
, NULL
);
330 if (diff_use_color_default
== -1)
331 diff_use_color_default
= git_use_color_default
;
333 init_revisions(&rev
, prefix
);
335 rev
.combine_merges
= 1;
336 rev
.dense_combined_merges
= 1;
337 rev
.always_show_header
= 1;
338 rev
.ignore_merges
= 0;
340 cmd_log_init(argc
, argv
, prefix
, &rev
);
342 count
= rev
.pending
.nr
;
343 objects
= rev
.pending
.objects
;
344 for (i
= 0; i
< count
&& !ret
; i
++) {
345 struct object
*o
= objects
[i
].item
;
346 const char *name
= objects
[i
].name
;
349 ret
= show_object(o
->sha1
, 0, NULL
);
352 struct tag
*t
= (struct tag
*)o
;
354 printf("%stag %s%s\n",
355 diff_get_color_opt(&rev
.diffopt
, DIFF_COMMIT
),
357 diff_get_color_opt(&rev
.diffopt
, DIFF_RESET
));
358 ret
= show_object(o
->sha1
, 1, &rev
);
361 o
= parse_object(t
->tagged
->sha1
);
363 ret
= error("Could not read object %s",
364 sha1_to_hex(t
->tagged
->sha1
));
370 printf("%stree %s%s\n\n",
371 diff_get_color_opt(&rev
.diffopt
, DIFF_COMMIT
),
373 diff_get_color_opt(&rev
.diffopt
, DIFF_RESET
));
374 read_tree_recursive((struct tree
*)o
, "", 0, 0, NULL
,
375 show_tree_object
, NULL
);
378 rev
.pending
.nr
= rev
.pending
.alloc
= 0;
379 rev
.pending
.objects
= NULL
;
380 add_object_array(o
, name
, &rev
.pending
);
381 ret
= cmd_log_walk(&rev
);
384 ret
= error("Unknown type: %d", o
->type
);
392 * This is equivalent to "git log -g --abbrev-commit --pretty=oneline"
394 int cmd_log_reflog(int argc
, const char **argv
, const char *prefix
)
398 git_config(git_log_config
, NULL
);
400 if (diff_use_color_default
== -1)
401 diff_use_color_default
= git_use_color_default
;
403 init_revisions(&rev
, prefix
);
404 init_reflog_walk(&rev
.reflog_info
);
405 rev
.abbrev_commit
= 1;
406 rev
.verbose_header
= 1;
407 cmd_log_init(argc
, argv
, prefix
, &rev
);
410 * This means that we override whatever commit format the user gave
411 * on the cmd line. Sad, but cmd_log_init() currently doesn't
412 * allow us to set a different default.
414 rev
.commit_format
= CMIT_FMT_ONELINE
;
415 rev
.use_terminator
= 1;
416 rev
.always_show_header
= 1;
419 * We get called through "git reflog", so unlike the other log
420 * routines, we need to set up our pager manually..
424 return cmd_log_walk(&rev
);
427 int cmd_log(int argc
, const char **argv
, const char *prefix
)
431 git_config(git_log_config
, NULL
);
433 if (diff_use_color_default
== -1)
434 diff_use_color_default
= git_use_color_default
;
436 init_revisions(&rev
, prefix
);
437 rev
.always_show_header
= 1;
438 cmd_log_init(argc
, argv
, prefix
, &rev
);
439 return cmd_log_walk(&rev
);
443 #define FORMAT_PATCH_NAME_MAX 64
445 static int istitlechar(char c
)
447 return (c
>= 'a' && c
<= 'z') || (c
>= 'A' && c
<= 'Z') ||
448 (c
>= '0' && c
<= '9') || c
== '.' || c
== '_';
451 static const char *fmt_patch_suffix
= ".patch";
452 static int numbered
= 0;
453 static int auto_number
= 0;
455 static char **extra_hdr
;
456 static int extra_hdr_nr
;
457 static int extra_hdr_alloc
;
459 static char **extra_to
;
460 static int extra_to_nr
;
461 static int extra_to_alloc
;
463 static char **extra_cc
;
464 static int extra_cc_nr
;
465 static int extra_cc_alloc
;
467 static void add_header(const char *value
)
469 int len
= strlen(value
);
470 while (len
&& value
[len
- 1] == '\n')
472 if (!strncasecmp(value
, "to: ", 4)) {
473 ALLOC_GROW(extra_to
, extra_to_nr
+ 1, extra_to_alloc
);
474 extra_to
[extra_to_nr
++] = xstrndup(value
+ 4, len
- 4);
477 if (!strncasecmp(value
, "cc: ", 4)) {
478 ALLOC_GROW(extra_cc
, extra_cc_nr
+ 1, extra_cc_alloc
);
479 extra_cc
[extra_cc_nr
++] = xstrndup(value
+ 4, len
- 4);
482 ALLOC_GROW(extra_hdr
, extra_hdr_nr
+ 1, extra_hdr_alloc
);
483 extra_hdr
[extra_hdr_nr
++] = xstrndup(value
, len
);
486 static int git_format_config(const char *var
, const char *value
, void *cb
)
488 if (!strcmp(var
, "format.headers")) {
490 die("format.headers without value");
494 if (!strcmp(var
, "format.suffix"))
495 return git_config_string(&fmt_patch_suffix
, var
, value
);
496 if (!strcmp(var
, "format.cc")) {
498 return config_error_nonbool(var
);
499 ALLOC_GROW(extra_cc
, extra_cc_nr
+ 1, extra_cc_alloc
);
500 extra_cc
[extra_cc_nr
++] = xstrdup(value
);
503 if (!strcmp(var
, "diff.color") || !strcmp(var
, "color.diff")) {
506 if (!strcmp(var
, "format.numbered")) {
507 if (value
&& !strcasecmp(value
, "auto")) {
511 numbered
= git_config_bool(var
, value
);
515 return git_log_config(var
, value
, cb
);
519 static const char *get_oneline_for_filename(struct commit
*commit
,
522 static char filename
[PATH_MAX
];
525 int suffix_len
= strlen(fmt_patch_suffix
) + 1;
527 sol
= strstr(commit
->buffer
, "\n\n");
534 /* strip [PATCH] or [PATCH blabla] */
535 if (!keep_subject
&& !prefixcmp(sol
, "[PATCH")) {
536 char *eos
= strchr(sol
+ 6, ']');
538 while (isspace(*eos
))
545 j
< FORMAT_PATCH_NAME_MAX
- suffix_len
- 5 &&
546 len
< sizeof(filename
) - suffix_len
&&
547 sol
[j
] && sol
[j
] != '\n';
549 if (istitlechar(sol
[j
])) {
551 filename
[len
++] = '-';
554 filename
[len
++] = sol
[j
];
556 while (sol
[j
+ 1] == '.')
561 while (filename
[len
- 1] == '.'
562 || filename
[len
- 1] == '-')
564 filename
[len
] = '\0';
569 static FILE *realstdout
= NULL
;
570 static const char *output_directory
= NULL
;
572 static int reopen_stdout(const char *oneline
, int nr
, int total
)
574 char filename
[PATH_MAX
];
576 int suffix_len
= strlen(fmt_patch_suffix
) + 1;
578 if (output_directory
) {
579 len
= snprintf(filename
, sizeof(filename
), "%s",
582 sizeof(filename
) - FORMAT_PATCH_NAME_MAX
- suffix_len
)
583 return error("name of output directory is too long");
584 if (filename
[len
- 1] != '/')
585 filename
[len
++] = '/';
589 len
+= sprintf(filename
+ len
, "%d", nr
);
591 len
+= sprintf(filename
+ len
, "%04d-", nr
);
592 len
+= snprintf(filename
+ len
, sizeof(filename
) - len
- 1
593 - suffix_len
, "%s", oneline
);
594 strcpy(filename
+ len
, fmt_patch_suffix
);
597 fprintf(realstdout
, "%s\n", filename
);
598 if (freopen(filename
, "w", stdout
) == NULL
)
599 return error("Cannot open patch file %s",filename
);
604 static void get_patch_ids(struct rev_info
*rev
, struct patch_ids
*ids
, const char *prefix
)
606 struct rev_info check_rev
;
607 struct commit
*commit
;
608 struct object
*o1
, *o2
;
609 unsigned flags1
, flags2
;
611 if (rev
->pending
.nr
!= 2)
612 die("Need exactly one range.");
614 o1
= rev
->pending
.objects
[0].item
;
616 o2
= rev
->pending
.objects
[1].item
;
619 if ((flags1
& UNINTERESTING
) == (flags2
& UNINTERESTING
))
624 /* given a range a..b get all patch ids for b..a */
625 init_revisions(&check_rev
, prefix
);
626 o1
->flags
^= UNINTERESTING
;
627 o2
->flags
^= UNINTERESTING
;
628 add_pending_object(&check_rev
, o1
, "o1");
629 add_pending_object(&check_rev
, o2
, "o2");
630 if (prepare_revision_walk(&check_rev
))
631 die("revision walk setup failed");
633 while ((commit
= get_revision(&check_rev
)) != NULL
) {
635 if (commit
->parents
&& commit
->parents
->next
)
638 add_commit_patch_id(commit
, ids
);
641 /* reset for next revision walk */
642 clear_commit_marks((struct commit
*)o1
,
643 SEEN
| UNINTERESTING
| SHOWN
| ADDED
);
644 clear_commit_marks((struct commit
*)o2
,
645 SEEN
| UNINTERESTING
| SHOWN
| ADDED
);
650 static void gen_message_id(struct rev_info
*info
, char *base
)
652 const char *committer
= git_committer_info(IDENT_WARN_ON_NO_NAME
);
653 const char *email_start
= strrchr(committer
, '<');
654 const char *email_end
= strrchr(committer
, '>');
656 if (!email_start
|| !email_end
|| email_start
> email_end
- 1)
657 die("Could not extract email from committer identity.");
658 strbuf_init(&buf
, 0);
659 strbuf_addf(&buf
, "%s.%lu.git.%.*s", base
,
660 (unsigned long) time(NULL
),
661 (int)(email_end
- email_start
- 1), email_start
+ 1);
662 info
->message_id
= strbuf_detach(&buf
, NULL
);
665 static void make_cover_letter(struct rev_info
*rev
, int use_stdout
,
666 int numbered
, int numbered_files
,
667 struct commit
*origin
,
668 int nr
, struct commit
**list
, struct commit
*head
)
670 const char *committer
;
672 const char *subject_start
= NULL
;
673 const char *body
= "*** SUBJECT HERE ***\n\n*** BLURB HERE ***\n";
675 const char *extra_headers
= rev
->extra_headers
;
679 const char *encoding
= "utf-8";
680 struct diff_options opts
;
681 int need_8bit_cte
= 0;
683 if (rev
->commit_format
!= CMIT_FMT_EMAIL
)
684 die("Cover letter needs email format");
686 if (!use_stdout
&& reopen_stdout(numbered_files
?
687 NULL
: "cover-letter", 0, rev
->total
))
690 head_sha1
= sha1_to_hex(head
->object
.sha1
);
692 log_write_email_headers(rev
, head_sha1
, &subject_start
, &extra_headers
,
695 committer
= git_committer_info(0);
699 pp_user_info(NULL
, CMIT_FMT_EMAIL
, &sb
, committer
, DATE_RFC2822
,
701 pp_title_line(CMIT_FMT_EMAIL
, &msg
, &sb
, subject_start
, extra_headers
,
702 encoding
, need_8bit_cte
);
703 pp_remainder(CMIT_FMT_EMAIL
, &msg
, &sb
, 0);
704 printf("%s\n", sb
.buf
);
713 for (i
= 0; i
< nr
; i
++)
714 shortlog_add_commit(&log
, list
[i
]);
716 shortlog_output(&log
);
719 * We can only do diffstat with a unique reference point
724 memcpy(&opts
, &rev
->diffopt
, sizeof(opts
));
725 opts
.output_format
= DIFF_FORMAT_SUMMARY
| DIFF_FORMAT_DIFFSTAT
;
727 diff_setup_done(&opts
);
729 diff_tree_sha1(origin
->tree
->object
.sha1
,
730 head
->tree
->object
.sha1
,
738 static const char *clean_message_id(const char *msg_id
)
741 const char *a
, *z
, *m
;
744 while ((ch
= *m
) && (isspace(ch
) || (ch
== '<')))
749 if (!isspace(ch
) && (ch
!= '>'))
754 die("insane in-reply-to: %s", msg_id
);
757 return xmemdupz(a
, z
- a
);
760 int cmd_format_patch(int argc
, const char **argv
, const char *prefix
)
762 struct commit
*commit
;
763 struct commit
**list
= NULL
;
765 int nr
= 0, total
, i
, j
;
767 int start_number
= -1;
768 int keep_subject
= 0;
769 int numbered_files
= 0; /* _just_ numbers */
770 int subject_prefix
= 0;
771 int ignore_if_in_upstream
= 0;
773 int cover_letter
= 0;
774 int boundary_count
= 0;
775 int no_binary_diff
= 0;
776 struct commit
*origin
= NULL
, *head
= NULL
;
777 const char *in_reply_to
= NULL
;
778 struct patch_ids ids
;
779 char *add_signoff
= NULL
;
782 git_config(git_format_config
, NULL
);
783 init_revisions(&rev
, prefix
);
784 rev
.commit_format
= CMIT_FMT_EMAIL
;
785 rev
.verbose_header
= 1;
787 rev
.combine_merges
= 0;
788 rev
.ignore_merges
= 1;
789 DIFF_OPT_SET(&rev
.diffopt
, RECURSIVE
);
791 rev
.subject_prefix
= fmt_patch_subject_prefix
;
794 * Parse the arguments before setup_revisions(), or something
795 * like "git format-patch -o a123 HEAD^.." may fail; a123 is
796 * possibly a valid SHA1.
798 for (i
= 1, j
= 1; i
< argc
; i
++) {
799 if (!strcmp(argv
[i
], "--stdout"))
801 else if (!strcmp(argv
[i
], "-n") ||
802 !strcmp(argv
[i
], "--numbered"))
804 else if (!strcmp(argv
[i
], "-N") ||
805 !strcmp(argv
[i
], "--no-numbered")) {
809 else if (!prefixcmp(argv
[i
], "--start-number="))
810 start_number
= strtol(argv
[i
] + 15, NULL
, 10);
811 else if (!strcmp(argv
[i
], "--numbered-files"))
813 else if (!strcmp(argv
[i
], "--start-number")) {
816 die("Need a number for --start-number");
817 start_number
= strtol(argv
[i
], NULL
, 10);
819 else if (!prefixcmp(argv
[i
], "--cc=")) {
820 ALLOC_GROW(extra_cc
, extra_cc_nr
+ 1, extra_cc_alloc
);
821 extra_cc
[extra_cc_nr
++] = xstrdup(argv
[i
] + 5);
823 else if (!strcmp(argv
[i
], "-k") ||
824 !strcmp(argv
[i
], "--keep-subject")) {
828 else if (!strcmp(argv
[i
], "--output-directory") ||
829 !strcmp(argv
[i
], "-o")) {
832 die("Which directory?");
833 if (output_directory
)
834 die("Two output directories?");
835 output_directory
= argv
[i
];
837 else if (!strcmp(argv
[i
], "--signoff") ||
838 !strcmp(argv
[i
], "-s")) {
839 const char *committer
;
841 committer
= git_committer_info(IDENT_ERROR_ON_NO_NAME
);
842 endpos
= strchr(committer
, '>');
844 die("bogus committer info %s\n", committer
);
845 add_signoff
= xmemdupz(committer
, endpos
- committer
+ 1);
847 else if (!strcmp(argv
[i
], "--attach")) {
848 rev
.mime_boundary
= git_version_string
;
851 else if (!prefixcmp(argv
[i
], "--attach=")) {
852 rev
.mime_boundary
= argv
[i
] + 9;
855 else if (!strcmp(argv
[i
], "--inline")) {
856 rev
.mime_boundary
= git_version_string
;
859 else if (!prefixcmp(argv
[i
], "--inline=")) {
860 rev
.mime_boundary
= argv
[i
] + 9;
863 else if (!strcmp(argv
[i
], "--ignore-if-in-upstream"))
864 ignore_if_in_upstream
= 1;
865 else if (!strcmp(argv
[i
], "--thread"))
867 else if (!prefixcmp(argv
[i
], "--in-reply-to="))
868 in_reply_to
= argv
[i
] + 14;
869 else if (!strcmp(argv
[i
], "--in-reply-to")) {
872 die("Need a Message-Id for --in-reply-to");
873 in_reply_to
= argv
[i
];
874 } else if (!prefixcmp(argv
[i
], "--subject-prefix=")) {
876 rev
.subject_prefix
= argv
[i
] + 17;
877 } else if (!prefixcmp(argv
[i
], "--suffix="))
878 fmt_patch_suffix
= argv
[i
] + 9;
879 else if (!strcmp(argv
[i
], "--cover-letter"))
881 else if (!strcmp(argv
[i
], "--no-binary"))
888 strbuf_init(&buf
, 0);
890 for (i
= 0; i
< extra_hdr_nr
; i
++) {
891 strbuf_addstr(&buf
, extra_hdr
[i
]);
892 strbuf_addch(&buf
, '\n');
896 strbuf_addstr(&buf
, "To: ");
897 for (i
= 0; i
< extra_to_nr
; i
++) {
899 strbuf_addstr(&buf
, " ");
900 strbuf_addstr(&buf
, extra_to
[i
]);
901 if (i
+ 1 < extra_to_nr
)
902 strbuf_addch(&buf
, ',');
903 strbuf_addch(&buf
, '\n');
907 strbuf_addstr(&buf
, "Cc: ");
908 for (i
= 0; i
< extra_cc_nr
; i
++) {
910 strbuf_addstr(&buf
, " ");
911 strbuf_addstr(&buf
, extra_cc
[i
]);
912 if (i
+ 1 < extra_cc_nr
)
913 strbuf_addch(&buf
, ',');
914 strbuf_addch(&buf
, '\n');
917 rev
.extra_headers
= strbuf_detach(&buf
, 0);
919 if (start_number
< 0)
921 if (numbered
&& keep_subject
)
922 die ("-n and -k are mutually exclusive.");
923 if (keep_subject
&& subject_prefix
)
924 die ("--subject-prefix and -k are mutually exclusive.");
925 if (numbered_files
&& use_stdout
)
926 die ("--numbered-files and --stdout are mutually exclusive.");
928 argc
= setup_revisions(argc
, argv
, &rev
, "HEAD");
930 die ("unrecognized argument: %s", argv
[1]);
932 if (!rev
.diffopt
.output_format
)
933 rev
.diffopt
.output_format
= DIFF_FORMAT_DIFFSTAT
| DIFF_FORMAT_SUMMARY
| DIFF_FORMAT_PATCH
;
935 if (!DIFF_OPT_TST(&rev
.diffopt
, TEXT
) && !no_binary_diff
)
936 DIFF_OPT_SET(&rev
.diffopt
, BINARY
);
938 if (!output_directory
&& !use_stdout
)
939 output_directory
= prefix
;
941 if (output_directory
) {
943 die("standard output, or directory, which one?");
944 if (mkdir(output_directory
, 0777) < 0 && errno
!= EEXIST
)
945 die("Could not create directory %s",
949 if (rev
.pending
.nr
== 1) {
950 if (rev
.max_count
< 0 && !rev
.show_root_diff
) {
952 * This is traditional behaviour of "git format-patch
953 * origin" that prepares what the origin side still
956 rev
.pending
.objects
[0].item
->flags
|= UNINTERESTING
;
957 add_head_to_pending(&rev
);
960 * Otherwise, it is "format-patch -22 HEAD", and/or
961 * "format-patch --root HEAD". The user wants
962 * get_revision() to do the usual traversal.
966 /* remember the range */
968 for (i
= 0; i
< rev
.pending
.nr
; i
++) {
969 struct object
*o
= rev
.pending
.objects
[i
].item
;
970 if (!(o
->flags
& UNINTERESTING
))
971 head
= (struct commit
*)o
;
973 /* We can't generate a cover letter without any patches */
978 if (ignore_if_in_upstream
)
979 get_patch_ids(&rev
, &ids
, prefix
);
982 realstdout
= xfdopen(xdup(1), "w");
984 if (prepare_revision_walk(&rev
))
985 die("revision walk setup failed");
987 while ((commit
= get_revision(&rev
)) != NULL
) {
988 if (commit
->object
.flags
& BOUNDARY
) {
990 origin
= (boundary_count
== 1) ? commit
: NULL
;
995 if (commit
->parents
&& commit
->parents
->next
)
998 if (ignore_if_in_upstream
&&
999 has_commit_patch_id(commit
, &ids
))
1003 list
= xrealloc(list
, nr
* sizeof(list
[0]));
1004 list
[nr
- 1] = commit
;
1007 if (!keep_subject
&& auto_number
&& total
> 1)
1010 rev
.total
= total
+ start_number
- 1;
1012 rev
.ref_message_id
= clean_message_id(in_reply_to
);
1015 gen_message_id(&rev
, "cover");
1016 make_cover_letter(&rev
, use_stdout
, numbered
, numbered_files
,
1017 origin
, nr
, list
, head
);
1021 rev
.add_signoff
= add_signoff
;
1025 rev
.nr
= total
- nr
+ (start_number
- 1);
1026 /* Make the second and subsequent mails replies to the first */
1028 /* Have we already had a message ID? */
1029 if (rev
.message_id
) {
1031 * If we've got the ID to be a reply
1032 * to, discard the current ID;
1033 * otherwise, make everything a reply
1036 if (rev
.ref_message_id
)
1037 free(rev
.message_id
);
1039 rev
.ref_message_id
= rev
.message_id
;
1041 gen_message_id(&rev
, sha1_to_hex(commit
->object
.sha1
));
1043 if (!use_stdout
&& reopen_stdout(numbered_files
? NULL
:
1044 get_oneline_for_filename(commit
, keep_subject
),
1046 die("Failed to create output files");
1047 shown
= log_tree_commit(&rev
, commit
);
1048 free(commit
->buffer
);
1049 commit
->buffer
= NULL
;
1051 /* We put one extra blank line between formatted
1052 * patches and this flag is used by log-tree code
1053 * to see if it needs to emit a LF before showing
1054 * the log; when using one file per patch, we do
1055 * not want the extra blank line.
1060 if (rev
.mime_boundary
)
1061 printf("\n--%s%s--\n\n\n",
1062 mime_boundary_leader
,
1065 printf("-- \n%s\n\n", git_version_string
);
1071 if (ignore_if_in_upstream
)
1072 free_patch_ids(&ids
);
1076 static int add_pending_commit(const char *arg
, struct rev_info
*revs
, int flags
)
1078 unsigned char sha1
[20];
1079 if (get_sha1(arg
, sha1
) == 0) {
1080 struct commit
*commit
= lookup_commit_reference(sha1
);
1082 commit
->object
.flags
|= flags
;
1083 add_pending_object(revs
, &commit
->object
, arg
);
1090 static const char cherry_usage
[] =
1091 "git cherry [-v] <upstream> [<head>] [<limit>]";
1092 int cmd_cherry(int argc
, const char **argv
, const char *prefix
)
1094 struct rev_info revs
;
1095 struct patch_ids ids
;
1096 struct commit
*commit
;
1097 struct commit_list
*list
= NULL
;
1098 const char *upstream
;
1099 const char *head
= "HEAD";
1100 const char *limit
= NULL
;
1103 if (argc
> 1 && !strcmp(argv
[1], "-v")) {
1120 usage(cherry_usage
);
1123 init_revisions(&revs
, prefix
);
1125 revs
.combine_merges
= 0;
1126 revs
.ignore_merges
= 1;
1127 DIFF_OPT_SET(&revs
.diffopt
, RECURSIVE
);
1129 if (add_pending_commit(head
, &revs
, 0))
1130 die("Unknown commit %s", head
);
1131 if (add_pending_commit(upstream
, &revs
, UNINTERESTING
))
1132 die("Unknown commit %s", upstream
);
1134 /* Don't say anything if head and upstream are the same. */
1135 if (revs
.pending
.nr
== 2) {
1136 struct object_array_entry
*o
= revs
.pending
.objects
;
1137 if (hashcmp(o
[0].item
->sha1
, o
[1].item
->sha1
) == 0)
1141 get_patch_ids(&revs
, &ids
, prefix
);
1143 if (limit
&& add_pending_commit(limit
, &revs
, UNINTERESTING
))
1144 die("Unknown commit %s", limit
);
1146 /* reverse the list of commits */
1147 if (prepare_revision_walk(&revs
))
1148 die("revision walk setup failed");
1149 while ((commit
= get_revision(&revs
)) != NULL
) {
1151 if (commit
->parents
&& commit
->parents
->next
)
1154 commit_list_insert(commit
, &list
);
1160 commit
= list
->item
;
1161 if (has_commit_patch_id(commit
, &ids
))
1166 strbuf_init(&buf
, 0);
1167 pretty_print_commit(CMIT_FMT_ONELINE
, commit
,
1168 &buf
, 0, NULL
, NULL
, 0, 0);
1169 printf("%c %s %s\n", sign
,
1170 sha1_to_hex(commit
->object
.sha1
), buf
.buf
);
1171 strbuf_release(&buf
);
1174 printf("%c %s\n", sign
,
1175 sha1_to_hex(commit
->object
.sha1
));
1181 free_patch_ids(&ids
);