2 * Builtin "git log" and related commands (show, whatchanged)
4 * (C) Copyright 2006 Linus Torvalds
14 #include "reflog-walk.h"
15 #include "patch-ids.h"
17 #include "run-command.h"
19 static int default_show_root
= 1;
20 static const char *fmt_patch_subject_prefix
= "PATCH";
22 static void add_name_decoration(const char *prefix
, const char *name
, struct object
*obj
)
24 int plen
= strlen(prefix
);
25 int nlen
= strlen(name
);
26 struct name_decoration
*res
= xmalloc(sizeof(struct name_decoration
) + plen
+ nlen
);
27 memcpy(res
->name
, prefix
, plen
);
28 memcpy(res
->name
+ plen
, name
, nlen
+ 1);
29 res
->next
= add_decoration(&name_decoration
, obj
, res
);
32 static int add_ref_decoration(const char *refname
, const unsigned char *sha1
, int flags
, void *cb_data
)
34 struct object
*obj
= parse_object(sha1
);
37 add_name_decoration("", refname
, obj
);
38 while (obj
->type
== OBJ_TAG
) {
39 obj
= ((struct tag
*)obj
)->tagged
;
42 add_name_decoration("tag: ", refname
, obj
);
47 static void cmd_log_init(int argc
, const char **argv
, const char *prefix
,
53 rev
->abbrev
= DEFAULT_ABBREV
;
54 rev
->commit_format
= CMIT_FMT_DEFAULT
;
55 rev
->verbose_header
= 1;
56 DIFF_OPT_SET(&rev
->diffopt
, RECURSIVE
);
57 rev
->show_root_diff
= default_show_root
;
58 rev
->subject_prefix
= fmt_patch_subject_prefix
;
59 argc
= setup_revisions(argc
, argv
, rev
, "HEAD");
60 if (rev
->diffopt
.pickaxe
|| rev
->diffopt
.filter
)
61 rev
->always_show_header
= 0;
62 if (DIFF_OPT_TST(&rev
->diffopt
, FOLLOW_RENAMES
)) {
63 rev
->always_show_header
= 0;
64 if (rev
->diffopt
.nr_paths
!= 1)
65 usage("git logs can only follow renames on one pathname at a time");
67 for (i
= 1; i
< argc
; i
++) {
68 const char *arg
= argv
[i
];
69 if (!strcmp(arg
, "--decorate")) {
71 for_each_ref(add_ref_decoration
, NULL
);
74 die("unrecognized argument: %s", arg
);
79 * This gives a rough estimate for how many commits we
80 * will print out in the list.
82 static int estimate_commit_count(struct rev_info
*rev
, struct commit_list
*list
)
87 struct commit
*commit
= list
->item
;
88 unsigned int flags
= commit
->object
.flags
;
90 if (!(flags
& (TREESAME
| UNINTERESTING
)))
96 static void show_early_header(struct rev_info
*rev
, const char *stage
, int nr
)
100 if (rev
->commit_format
!= CMIT_FMT_ONELINE
)
101 putchar(rev
->diffopt
.line_termination
);
103 printf("Final output: %d %s\n", nr
, stage
);
106 struct itimerval early_output_timer
;
108 static void log_show_early(struct rev_info
*revs
, struct commit_list
*list
)
110 int i
= revs
->early_output
;
113 sort_in_topological_order(&list
, revs
->lifo
);
115 struct commit
*commit
= list
->item
;
116 switch (simplify_commit(revs
, commit
)) {
119 int n
= estimate_commit_count(revs
, list
);
120 show_early_header(revs
, "incomplete", n
);
123 log_tree_commit(revs
, commit
);
134 /* Did we already get enough commits for the early output? */
139 * ..if no, then repeat it twice a second until we
142 * NOTE! We don't use "it_interval", because if the
143 * reader isn't listening, we want our output to be
144 * throttled by the writing, and not have the timer
145 * trigger every second even if we're blocked on a
148 early_output_timer
.it_value
.tv_sec
= 0;
149 early_output_timer
.it_value
.tv_usec
= 500000;
150 setitimer(ITIMER_REAL
, &early_output_timer
, NULL
);
153 static void early_output(int signal
)
155 show_early_output
= log_show_early
;
158 static void setup_early_output(struct rev_info
*rev
)
163 * Set up the signal handler, minimally intrusively:
164 * we only set a single volatile integer word (not
165 * using sigatomic_t - trying to avoid unnecessary
166 * system dependencies and headers), and using
169 memset(&sa
, 0, sizeof(sa
));
170 sa
.sa_handler
= early_output
;
171 sigemptyset(&sa
.sa_mask
);
172 sa
.sa_flags
= SA_RESTART
;
173 sigaction(SIGALRM
, &sa
, NULL
);
176 * If we can get the whole output in less than a
177 * tenth of a second, don't even bother doing the
178 * early-output thing..
180 * This is a one-time-only trigger.
182 early_output_timer
.it_value
.tv_sec
= 0;
183 early_output_timer
.it_value
.tv_usec
= 100000;
184 setitimer(ITIMER_REAL
, &early_output_timer
, NULL
);
187 static void finish_early_output(struct rev_info
*rev
)
189 int n
= estimate_commit_count(rev
, rev
->commits
);
190 signal(SIGALRM
, SIG_IGN
);
191 show_early_header(rev
, "done", n
);
194 static int cmd_log_walk(struct rev_info
*rev
)
196 struct commit
*commit
;
198 if (rev
->early_output
)
199 setup_early_output(rev
);
201 prepare_revision_walk(rev
);
203 if (rev
->early_output
)
204 finish_early_output(rev
);
206 while ((commit
= get_revision(rev
)) != NULL
) {
207 log_tree_commit(rev
, commit
);
208 if (!rev
->reflog_info
) {
209 /* we allow cycles in reflog ancestry */
210 free(commit
->buffer
);
211 commit
->buffer
= NULL
;
213 free_commit_list(commit
->parents
);
214 commit
->parents
= NULL
;
219 static int git_log_config(const char *var
, const char *value
)
221 if (!strcmp(var
, "format.subjectprefix")) {
223 config_error_nonbool(var
);
224 fmt_patch_subject_prefix
= xstrdup(value
);
227 if (!strcmp(var
, "log.showroot")) {
228 default_show_root
= git_config_bool(var
, value
);
231 return git_diff_ui_config(var
, value
);
234 int cmd_whatchanged(int argc
, const char **argv
, const char *prefix
)
238 git_config(git_log_config
);
239 init_revisions(&rev
, prefix
);
241 rev
.simplify_history
= 0;
242 cmd_log_init(argc
, argv
, prefix
, &rev
);
243 if (!rev
.diffopt
.output_format
)
244 rev
.diffopt
.output_format
= DIFF_FORMAT_RAW
;
245 return cmd_log_walk(&rev
);
248 static void show_tagger(char *buf
, int len
, struct rev_info
*rev
)
254 email_end
= memchr(buf
, '>', len
);
260 date
= strtoul(p
, &p
, 10);
263 tz
= (int)strtol(p
, NULL
, 10);
264 printf("Tagger: %.*s\nDate: %s\n", (int)(email_end
- buf
), buf
,
265 show_date(date
, tz
, rev
->date_mode
));
268 static int show_object(const unsigned char *sha1
, int show_tag_object
,
269 struct rev_info
*rev
)
272 enum object_type type
;
273 char *buf
= read_sha1_file(sha1
, &type
, &size
);
277 return error("Could not read object %s", sha1_to_hex(sha1
));
280 while (offset
< size
&& buf
[offset
] != '\n') {
281 int new_offset
= offset
+ 1;
282 while (new_offset
< size
&& buf
[new_offset
++] != '\n')
284 if (!prefixcmp(buf
+ offset
, "tagger "))
285 show_tagger(buf
+ offset
+ 7,
286 new_offset
- offset
- 7, rev
);
291 fwrite(buf
+ offset
, size
- offset
, 1, stdout
);
296 static int show_tree_object(const unsigned char *sha1
,
297 const char *base
, int baselen
,
298 const char *pathname
, unsigned mode
, int stage
)
300 printf("%s%s\n", pathname
, S_ISDIR(mode
) ? "/" : "");
304 int cmd_show(int argc
, const char **argv
, const char *prefix
)
307 struct object_array_entry
*objects
;
308 int i
, count
, ret
= 0;
310 git_config(git_log_config
);
311 init_revisions(&rev
, prefix
);
313 rev
.combine_merges
= 1;
314 rev
.dense_combined_merges
= 1;
315 rev
.always_show_header
= 1;
316 rev
.ignore_merges
= 0;
318 cmd_log_init(argc
, argv
, prefix
, &rev
);
320 count
= rev
.pending
.nr
;
321 objects
= rev
.pending
.objects
;
322 for (i
= 0; i
< count
&& !ret
; i
++) {
323 struct object
*o
= objects
[i
].item
;
324 const char *name
= objects
[i
].name
;
327 ret
= show_object(o
->sha1
, 0, NULL
);
330 struct tag
*t
= (struct tag
*)o
;
332 printf("%stag %s%s\n",
333 diff_get_color_opt(&rev
.diffopt
, DIFF_COMMIT
),
335 diff_get_color_opt(&rev
.diffopt
, DIFF_RESET
));
336 ret
= show_object(o
->sha1
, 1, &rev
);
337 objects
[i
].item
= (struct object
*)t
->tagged
;
342 printf("%stree %s%s\n\n",
343 diff_get_color_opt(&rev
.diffopt
, DIFF_COMMIT
),
345 diff_get_color_opt(&rev
.diffopt
, DIFF_RESET
));
346 read_tree_recursive((struct tree
*)o
, "", 0, 0, NULL
,
350 rev
.pending
.nr
= rev
.pending
.alloc
= 0;
351 rev
.pending
.objects
= NULL
;
352 add_object_array(o
, name
, &rev
.pending
);
353 ret
= cmd_log_walk(&rev
);
356 ret
= error("Unknown type: %d", o
->type
);
364 * This is equivalent to "git log -g --abbrev-commit --pretty=oneline"
366 int cmd_log_reflog(int argc
, const char **argv
, const char *prefix
)
370 git_config(git_log_config
);
371 init_revisions(&rev
, prefix
);
372 init_reflog_walk(&rev
.reflog_info
);
373 rev
.abbrev_commit
= 1;
374 rev
.verbose_header
= 1;
375 cmd_log_init(argc
, argv
, prefix
, &rev
);
378 * This means that we override whatever commit format the user gave
379 * on the cmd line. Sad, but cmd_log_init() currently doesn't
380 * allow us to set a different default.
382 rev
.commit_format
= CMIT_FMT_ONELINE
;
383 rev
.always_show_header
= 1;
386 * We get called through "git reflog", so unlike the other log
387 * routines, we need to set up our pager manually..
391 return cmd_log_walk(&rev
);
394 int cmd_log(int argc
, const char **argv
, const char *prefix
)
398 git_config(git_log_config
);
399 init_revisions(&rev
, prefix
);
400 rev
.always_show_header
= 1;
401 cmd_log_init(argc
, argv
, prefix
, &rev
);
402 return cmd_log_walk(&rev
);
406 #define FORMAT_PATCH_NAME_MAX 64
408 static int istitlechar(char c
)
410 return (c
>= 'a' && c
<= 'z') || (c
>= 'A' && c
<= 'Z') ||
411 (c
>= '0' && c
<= '9') || c
== '.' || c
== '_';
414 static const char *fmt_patch_suffix
= ".patch";
415 static int numbered
= 0;
416 static int auto_number
= 0;
418 static char **extra_hdr
;
419 static int extra_hdr_nr
;
420 static int extra_hdr_alloc
;
422 static char **extra_to
;
423 static int extra_to_nr
;
424 static int extra_to_alloc
;
426 static char **extra_cc
;
427 static int extra_cc_nr
;
428 static int extra_cc_alloc
;
430 static void add_header(const char *value
)
432 int len
= strlen(value
);
433 while (value
[len
- 1] == '\n')
435 if (!strncasecmp(value
, "to: ", 4)) {
436 ALLOC_GROW(extra_to
, extra_to_nr
+ 1, extra_to_alloc
);
437 extra_to
[extra_to_nr
++] = xstrndup(value
+ 4, len
- 4);
440 if (!strncasecmp(value
, "cc: ", 4)) {
441 ALLOC_GROW(extra_cc
, extra_cc_nr
+ 1, extra_cc_alloc
);
442 extra_cc
[extra_cc_nr
++] = xstrndup(value
+ 4, len
- 4);
445 ALLOC_GROW(extra_hdr
, extra_hdr_nr
+ 1, extra_hdr_alloc
);
446 extra_hdr
[extra_hdr_nr
++] = xstrndup(value
, len
);
449 static int git_format_config(const char *var
, const char *value
)
451 if (!strcmp(var
, "format.headers")) {
453 die("format.headers without value");
457 if (!strcmp(var
, "format.suffix")) {
459 return config_error_nonbool(var
);
460 fmt_patch_suffix
= xstrdup(value
);
463 if (!strcmp(var
, "diff.color") || !strcmp(var
, "color.diff")) {
466 if (!strcmp(var
, "format.numbered")) {
467 if (value
&& !strcasecmp(value
, "auto")) {
471 numbered
= git_config_bool(var
, value
);
475 return git_log_config(var
, value
);
479 static const char *get_oneline_for_filename(struct commit
*commit
,
482 static char filename
[PATH_MAX
];
485 int suffix_len
= strlen(fmt_patch_suffix
) + 1;
487 sol
= strstr(commit
->buffer
, "\n\n");
494 /* strip [PATCH] or [PATCH blabla] */
495 if (!keep_subject
&& !prefixcmp(sol
, "[PATCH")) {
496 char *eos
= strchr(sol
+ 6, ']');
498 while (isspace(*eos
))
505 j
< FORMAT_PATCH_NAME_MAX
- suffix_len
- 5 &&
506 len
< sizeof(filename
) - suffix_len
&&
507 sol
[j
] && sol
[j
] != '\n';
509 if (istitlechar(sol
[j
])) {
511 filename
[len
++] = '-';
514 filename
[len
++] = sol
[j
];
516 while (sol
[j
+ 1] == '.')
521 while (filename
[len
- 1] == '.'
522 || filename
[len
- 1] == '-')
524 filename
[len
] = '\0';
529 static FILE *realstdout
= NULL
;
530 static const char *output_directory
= NULL
;
532 static int reopen_stdout(const char *oneline
, int nr
, int total
)
534 char filename
[PATH_MAX
];
536 int suffix_len
= strlen(fmt_patch_suffix
) + 1;
538 if (output_directory
) {
539 len
= snprintf(filename
, sizeof(filename
), "%s",
542 sizeof(filename
) - FORMAT_PATCH_NAME_MAX
- suffix_len
)
543 return error("name of output directory is too long");
544 if (filename
[len
- 1] != '/')
545 filename
[len
++] = '/';
549 len
+= sprintf(filename
+ len
, "%d", nr
);
551 len
+= sprintf(filename
+ len
, "%04d-", nr
);
552 len
+= snprintf(filename
+ len
, sizeof(filename
) - len
- 1
553 - suffix_len
, "%s", oneline
);
554 strcpy(filename
+ len
, fmt_patch_suffix
);
557 fprintf(realstdout
, "%s\n", filename
);
558 if (freopen(filename
, "w", stdout
) == NULL
)
559 return error("Cannot open patch file %s",filename
);
564 static void get_patch_ids(struct rev_info
*rev
, struct patch_ids
*ids
, const char *prefix
)
566 struct rev_info check_rev
;
567 struct commit
*commit
;
568 struct object
*o1
, *o2
;
569 unsigned flags1
, flags2
;
571 if (rev
->pending
.nr
!= 2)
572 die("Need exactly one range.");
574 o1
= rev
->pending
.objects
[0].item
;
576 o2
= rev
->pending
.objects
[1].item
;
579 if ((flags1
& UNINTERESTING
) == (flags2
& UNINTERESTING
))
584 /* given a range a..b get all patch ids for b..a */
585 init_revisions(&check_rev
, prefix
);
586 o1
->flags
^= UNINTERESTING
;
587 o2
->flags
^= UNINTERESTING
;
588 add_pending_object(&check_rev
, o1
, "o1");
589 add_pending_object(&check_rev
, o2
, "o2");
590 prepare_revision_walk(&check_rev
);
592 while ((commit
= get_revision(&check_rev
)) != NULL
) {
594 if (commit
->parents
&& commit
->parents
->next
)
597 add_commit_patch_id(commit
, ids
);
600 /* reset for next revision walk */
601 clear_commit_marks((struct commit
*)o1
,
602 SEEN
| UNINTERESTING
| SHOWN
| ADDED
);
603 clear_commit_marks((struct commit
*)o2
,
604 SEEN
| UNINTERESTING
| SHOWN
| ADDED
);
609 static void gen_message_id(struct rev_info
*info
, char *base
)
611 const char *committer
= git_committer_info(IDENT_WARN_ON_NO_NAME
);
612 const char *email_start
= strrchr(committer
, '<');
613 const char *email_end
= strrchr(committer
, '>');
615 if (!email_start
|| !email_end
|| email_start
> email_end
- 1)
616 die("Could not extract email from committer identity.");
617 strbuf_init(&buf
, 0);
618 strbuf_addf(&buf
, "%s.%lu.git.%.*s", base
,
619 (unsigned long) time(NULL
),
620 (int)(email_end
- email_start
- 1), email_start
+ 1);
621 info
->message_id
= strbuf_detach(&buf
, NULL
);
624 static void make_cover_letter(struct rev_info
*rev
,
625 int use_stdout
, int numbered
, int numbered_files
,
626 struct commit
*origin
, struct commit
*head
)
628 const char *committer
;
629 const char *origin_sha1
, *head_sha1
;
631 const char *subject_start
= NULL
;
632 const char *body
= "*** SUBJECT HERE ***\n\n*** BLURB HERE ***\n";
634 const char *extra_headers
= rev
->extra_headers
;
636 const char *encoding
= "utf-8";
638 if (rev
->commit_format
!= CMIT_FMT_EMAIL
)
639 die("Cover letter needs email format");
641 if (!use_stdout
&& reopen_stdout(numbered_files
?
642 NULL
: "cover-letter", 0, rev
->total
))
645 origin_sha1
= sha1_to_hex(origin
? origin
->object
.sha1
: null_sha1
);
646 head_sha1
= sha1_to_hex(head
->object
.sha1
);
648 log_write_email_headers(rev
, head_sha1
, &subject_start
, &extra_headers
);
650 committer
= git_committer_info(0);
654 pp_user_info(NULL
, CMIT_FMT_EMAIL
, &sb
, committer
, DATE_RFC2822
,
656 pp_title_line(CMIT_FMT_EMAIL
, &msg
, &sb
, subject_start
, extra_headers
,
658 pp_remainder(CMIT_FMT_EMAIL
, &msg
, &sb
, 0);
659 printf("%s\n", sb
.buf
);
664 * We can only do diffstat with a unique reference point, and
665 * log is a bit tricky, so just skip it.
670 argv
[0] = "shortlog";
673 argv
[3] = origin_sha1
;
677 run_command_v_opt(argv
, RUN_GIT_CMD
);
681 argv
[2] = "--summary";
684 argv
[5] = origin_sha1
;
688 run_command_v_opt(argv
, RUN_GIT_CMD
);
694 static const char *clean_message_id(const char *msg_id
)
697 const char *a
, *z
, *m
;
700 while ((ch
= *m
) && (isspace(ch
) || (ch
== '<')))
705 if (!isspace(ch
) && (ch
!= '>'))
710 die("insane in-reply-to: %s", msg_id
);
713 return xmemdupz(a
, z
- a
);
716 int cmd_format_patch(int argc
, const char **argv
, const char *prefix
)
718 struct commit
*commit
;
719 struct commit
**list
= NULL
;
721 int nr
= 0, total
, i
, j
;
723 int start_number
= -1;
724 int keep_subject
= 0;
725 int numbered_files
= 0; /* _just_ numbers */
726 int subject_prefix
= 0;
727 int ignore_if_in_upstream
= 0;
729 int cover_letter
= 0;
730 struct commit
*origin
= NULL
, *head
= NULL
;
731 const char *in_reply_to
= NULL
;
732 struct patch_ids ids
;
733 char *add_signoff
= NULL
;
736 git_config(git_format_config
);
737 init_revisions(&rev
, prefix
);
738 rev
.commit_format
= CMIT_FMT_EMAIL
;
739 rev
.verbose_header
= 1;
741 rev
.combine_merges
= 0;
742 rev
.ignore_merges
= 1;
743 rev
.diffopt
.msg_sep
= "";
744 DIFF_OPT_SET(&rev
.diffopt
, RECURSIVE
);
746 rev
.subject_prefix
= fmt_patch_subject_prefix
;
749 * Parse the arguments before setup_revisions(), or something
750 * like "git format-patch -o a123 HEAD^.." may fail; a123 is
751 * possibly a valid SHA1.
753 for (i
= 1, j
= 1; i
< argc
; i
++) {
754 if (!strcmp(argv
[i
], "--stdout"))
756 else if (!strcmp(argv
[i
], "-n") ||
757 !strcmp(argv
[i
], "--numbered"))
759 else if (!strcmp(argv
[i
], "-N") ||
760 !strcmp(argv
[i
], "--no-numbered")) {
764 else if (!prefixcmp(argv
[i
], "--start-number="))
765 start_number
= strtol(argv
[i
] + 15, NULL
, 10);
766 else if (!strcmp(argv
[i
], "--numbered-files"))
768 else if (!strcmp(argv
[i
], "--start-number")) {
771 die("Need a number for --start-number");
772 start_number
= strtol(argv
[i
], NULL
, 10);
774 else if (!strcmp(argv
[i
], "-k") ||
775 !strcmp(argv
[i
], "--keep-subject")) {
779 else if (!strcmp(argv
[i
], "--output-directory") ||
780 !strcmp(argv
[i
], "-o")) {
783 die("Which directory?");
784 if (output_directory
)
785 die("Two output directories?");
786 output_directory
= argv
[i
];
788 else if (!strcmp(argv
[i
], "--signoff") ||
789 !strcmp(argv
[i
], "-s")) {
790 const char *committer
;
792 committer
= git_committer_info(IDENT_ERROR_ON_NO_NAME
);
793 endpos
= strchr(committer
, '>');
795 die("bogos committer info %s\n", committer
);
796 add_signoff
= xmemdupz(committer
, endpos
- committer
+ 1);
798 else if (!strcmp(argv
[i
], "--attach")) {
799 rev
.mime_boundary
= git_version_string
;
802 else if (!prefixcmp(argv
[i
], "--attach=")) {
803 rev
.mime_boundary
= argv
[i
] + 9;
806 else if (!strcmp(argv
[i
], "--inline")) {
807 rev
.mime_boundary
= git_version_string
;
810 else if (!prefixcmp(argv
[i
], "--inline=")) {
811 rev
.mime_boundary
= argv
[i
] + 9;
814 else if (!strcmp(argv
[i
], "--ignore-if-in-upstream"))
815 ignore_if_in_upstream
= 1;
816 else if (!strcmp(argv
[i
], "--thread"))
818 else if (!prefixcmp(argv
[i
], "--in-reply-to="))
819 in_reply_to
= argv
[i
] + 14;
820 else if (!strcmp(argv
[i
], "--in-reply-to")) {
823 die("Need a Message-Id for --in-reply-to");
824 in_reply_to
= argv
[i
];
825 } else if (!prefixcmp(argv
[i
], "--subject-prefix=")) {
827 rev
.subject_prefix
= argv
[i
] + 17;
828 } else if (!prefixcmp(argv
[i
], "--suffix="))
829 fmt_patch_suffix
= argv
[i
] + 9;
830 else if (!strcmp(argv
[i
], "--cover-letter"))
837 strbuf_init(&buf
, 0);
839 for (i
= 0; i
< extra_hdr_nr
; i
++) {
840 strbuf_addstr(&buf
, extra_hdr
[i
]);
841 strbuf_addch(&buf
, '\n');
845 strbuf_addstr(&buf
, "To: ");
846 for (i
= 0; i
< extra_to_nr
; i
++) {
848 strbuf_addstr(&buf
, " ");
849 strbuf_addstr(&buf
, extra_to
[i
]);
850 if (i
+ 1 < extra_to_nr
)
851 strbuf_addch(&buf
, ',');
852 strbuf_addch(&buf
, '\n');
856 strbuf_addstr(&buf
, "Cc: ");
857 for (i
= 0; i
< extra_cc_nr
; i
++) {
859 strbuf_addstr(&buf
, " ");
860 strbuf_addstr(&buf
, extra_cc
[i
]);
861 if (i
+ 1 < extra_cc_nr
)
862 strbuf_addch(&buf
, ',');
863 strbuf_addch(&buf
, '\n');
866 rev
.extra_headers
= strbuf_detach(&buf
, 0);
868 if (start_number
< 0)
870 if (numbered
&& keep_subject
)
871 die ("-n and -k are mutually exclusive.");
872 if (keep_subject
&& subject_prefix
)
873 die ("--subject-prefix and -k are mutually exclusive.");
874 if (numbered_files
&& use_stdout
)
875 die ("--numbered-files and --stdout are mutually exclusive.");
877 argc
= setup_revisions(argc
, argv
, &rev
, "HEAD");
879 die ("unrecognized argument: %s", argv
[1]);
881 if (!rev
.diffopt
.output_format
)
882 rev
.diffopt
.output_format
= DIFF_FORMAT_DIFFSTAT
| DIFF_FORMAT_SUMMARY
| DIFF_FORMAT_PATCH
;
884 if (!DIFF_OPT_TST(&rev
.diffopt
, TEXT
))
885 DIFF_OPT_SET(&rev
.diffopt
, BINARY
);
887 if (!output_directory
&& !use_stdout
)
888 output_directory
= prefix
;
890 if (output_directory
) {
892 die("standard output, or directory, which one?");
893 if (mkdir(output_directory
, 0777) < 0 && errno
!= EEXIST
)
894 die("Could not create directory %s",
898 if (rev
.pending
.nr
== 1) {
899 if (rev
.max_count
< 0 && !rev
.show_root_diff
) {
901 * This is traditional behaviour of "git format-patch
902 * origin" that prepares what the origin side still
905 rev
.pending
.objects
[0].item
->flags
|= UNINTERESTING
;
906 add_head_to_pending(&rev
);
909 * Otherwise, it is "format-patch -22 HEAD", and/or
910 * "format-patch --root HEAD". The user wants
911 * get_revision() to do the usual traversal.
915 /* remember the range */
916 int negative_count
= 0;
918 for (i
= 0; i
< rev
.pending
.nr
; i
++) {
919 struct object
*o
= rev
.pending
.objects
[i
].item
;
920 if (o
->flags
& UNINTERESTING
) {
921 origin
= (struct commit
*)o
;
924 head
= (struct commit
*)o
;
926 /* Multiple origins don't work for diffstat. */
927 if (negative_count
> 1)
929 /* We can't generate a cover letter without any patches */
934 if (ignore_if_in_upstream
)
935 get_patch_ids(&rev
, &ids
, prefix
);
938 realstdout
= xfdopen(xdup(1), "w");
940 prepare_revision_walk(&rev
);
941 while ((commit
= get_revision(&rev
)) != NULL
) {
943 if (commit
->parents
&& commit
->parents
->next
)
946 if (ignore_if_in_upstream
&&
947 has_commit_patch_id(commit
, &ids
))
951 list
= xrealloc(list
, nr
* sizeof(list
[0]));
952 list
[nr
- 1] = commit
;
955 if (!keep_subject
&& auto_number
&& total
> 1)
958 rev
.total
= total
+ start_number
- 1;
960 rev
.ref_message_id
= clean_message_id(in_reply_to
);
963 gen_message_id(&rev
, "cover");
964 make_cover_letter(&rev
, use_stdout
, numbered
, numbered_files
,
969 rev
.add_signoff
= add_signoff
;
973 rev
.nr
= total
- nr
+ (start_number
- 1);
974 /* Make the second and subsequent mails replies to the first */
976 /* Have we already had a message ID? */
977 if (rev
.message_id
) {
979 * If we've got the ID to be a reply
980 * to, discard the current ID;
981 * otherwise, make everything a reply
984 if (rev
.ref_message_id
)
985 free(rev
.message_id
);
987 rev
.ref_message_id
= rev
.message_id
;
989 gen_message_id(&rev
, sha1_to_hex(commit
->object
.sha1
));
991 if (!use_stdout
&& reopen_stdout(numbered_files
? NULL
:
992 get_oneline_for_filename(commit
, keep_subject
),
994 die("Failed to create output files");
995 shown
= log_tree_commit(&rev
, commit
);
996 free(commit
->buffer
);
997 commit
->buffer
= NULL
;
999 /* We put one extra blank line between formatted
1000 * patches and this flag is used by log-tree code
1001 * to see if it needs to emit a LF before showing
1002 * the log; when using one file per patch, we do
1003 * not want the extra blank line.
1008 if (rev
.mime_boundary
)
1009 printf("\n--%s%s--\n\n\n",
1010 mime_boundary_leader
,
1013 printf("-- \n%s\n\n", git_version_string
);
1019 if (ignore_if_in_upstream
)
1020 free_patch_ids(&ids
);
1024 static int add_pending_commit(const char *arg
, struct rev_info
*revs
, int flags
)
1026 unsigned char sha1
[20];
1027 if (get_sha1(arg
, sha1
) == 0) {
1028 struct commit
*commit
= lookup_commit_reference(sha1
);
1030 commit
->object
.flags
|= flags
;
1031 add_pending_object(revs
, &commit
->object
, arg
);
1038 static const char cherry_usage
[] =
1039 "git-cherry [-v] <upstream> [<head>] [<limit>]";
1040 int cmd_cherry(int argc
, const char **argv
, const char *prefix
)
1042 struct rev_info revs
;
1043 struct patch_ids ids
;
1044 struct commit
*commit
;
1045 struct commit_list
*list
= NULL
;
1046 const char *upstream
;
1047 const char *head
= "HEAD";
1048 const char *limit
= NULL
;
1051 if (argc
> 1 && !strcmp(argv
[1], "-v")) {
1068 usage(cherry_usage
);
1071 init_revisions(&revs
, prefix
);
1073 revs
.combine_merges
= 0;
1074 revs
.ignore_merges
= 1;
1075 DIFF_OPT_SET(&revs
.diffopt
, RECURSIVE
);
1077 if (add_pending_commit(head
, &revs
, 0))
1078 die("Unknown commit %s", head
);
1079 if (add_pending_commit(upstream
, &revs
, UNINTERESTING
))
1080 die("Unknown commit %s", upstream
);
1082 /* Don't say anything if head and upstream are the same. */
1083 if (revs
.pending
.nr
== 2) {
1084 struct object_array_entry
*o
= revs
.pending
.objects
;
1085 if (hashcmp(o
[0].item
->sha1
, o
[1].item
->sha1
) == 0)
1089 get_patch_ids(&revs
, &ids
, prefix
);
1091 if (limit
&& add_pending_commit(limit
, &revs
, UNINTERESTING
))
1092 die("Unknown commit %s", limit
);
1094 /* reverse the list of commits */
1095 prepare_revision_walk(&revs
);
1096 while ((commit
= get_revision(&revs
)) != NULL
) {
1098 if (commit
->parents
&& commit
->parents
->next
)
1101 commit_list_insert(commit
, &list
);
1107 commit
= list
->item
;
1108 if (has_commit_patch_id(commit
, &ids
))
1113 strbuf_init(&buf
, 0);
1114 pretty_print_commit(CMIT_FMT_ONELINE
, commit
,
1115 &buf
, 0, NULL
, NULL
, 0, 0);
1116 printf("%c %s %s\n", sign
,
1117 sha1_to_hex(commit
->object
.sha1
), buf
.buf
);
1118 strbuf_release(&buf
);
1121 printf("%c %s\n", sign
,
1122 sha1_to_hex(commit
->object
.sha1
));
1128 free_patch_ids(&ids
);