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"
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 if (prepare_revision_walk(rev
))
202 die("revision walk setup failed");
204 if (rev
->early_output
)
205 finish_early_output(rev
);
207 while ((commit
= get_revision(rev
)) != NULL
) {
208 log_tree_commit(rev
, commit
);
209 if (!rev
->reflog_info
) {
210 /* we allow cycles in reflog ancestry */
211 free(commit
->buffer
);
212 commit
->buffer
= NULL
;
214 free_commit_list(commit
->parents
);
215 commit
->parents
= NULL
;
220 static int git_log_config(const char *var
, const char *value
)
222 if (!strcmp(var
, "format.subjectprefix")) {
224 config_error_nonbool(var
);
225 fmt_patch_subject_prefix
= xstrdup(value
);
228 if (!strcmp(var
, "log.showroot")) {
229 default_show_root
= git_config_bool(var
, value
);
232 return git_diff_ui_config(var
, value
);
235 int cmd_whatchanged(int argc
, const char **argv
, const char *prefix
)
239 git_config(git_log_config
);
241 if (diff_use_color_default
== -1)
242 diff_use_color_default
= git_use_color_default
;
244 init_revisions(&rev
, prefix
);
246 rev
.simplify_history
= 0;
247 cmd_log_init(argc
, argv
, prefix
, &rev
);
248 if (!rev
.diffopt
.output_format
)
249 rev
.diffopt
.output_format
= DIFF_FORMAT_RAW
;
250 return cmd_log_walk(&rev
);
253 static void show_tagger(char *buf
, int len
, struct rev_info
*rev
)
259 email_end
= memchr(buf
, '>', len
);
265 date
= strtoul(p
, &p
, 10);
268 tz
= (int)strtol(p
, NULL
, 10);
269 printf("Tagger: %.*s\nDate: %s\n", (int)(email_end
- buf
), buf
,
270 show_date(date
, tz
, rev
->date_mode
));
273 static int show_object(const unsigned char *sha1
, int show_tag_object
,
274 struct rev_info
*rev
)
277 enum object_type type
;
278 char *buf
= read_sha1_file(sha1
, &type
, &size
);
282 return error("Could not read object %s", sha1_to_hex(sha1
));
285 while (offset
< size
&& buf
[offset
] != '\n') {
286 int new_offset
= offset
+ 1;
287 while (new_offset
< size
&& buf
[new_offset
++] != '\n')
289 if (!prefixcmp(buf
+ offset
, "tagger "))
290 show_tagger(buf
+ offset
+ 7,
291 new_offset
- offset
- 7, rev
);
296 fwrite(buf
+ offset
, size
- offset
, 1, stdout
);
301 static int show_tree_object(const unsigned char *sha1
,
302 const char *base
, int baselen
,
303 const char *pathname
, unsigned mode
, int stage
)
305 printf("%s%s\n", pathname
, S_ISDIR(mode
) ? "/" : "");
309 int cmd_show(int argc
, const char **argv
, const char *prefix
)
312 struct object_array_entry
*objects
;
313 int i
, count
, ret
= 0;
315 git_config(git_log_config
);
317 if (diff_use_color_default
== -1)
318 diff_use_color_default
= git_use_color_default
;
320 init_revisions(&rev
, prefix
);
322 rev
.combine_merges
= 1;
323 rev
.dense_combined_merges
= 1;
324 rev
.always_show_header
= 1;
325 rev
.ignore_merges
= 0;
327 cmd_log_init(argc
, argv
, prefix
, &rev
);
329 count
= rev
.pending
.nr
;
330 objects
= rev
.pending
.objects
;
331 for (i
= 0; i
< count
&& !ret
; i
++) {
332 struct object
*o
= objects
[i
].item
;
333 const char *name
= objects
[i
].name
;
336 ret
= show_object(o
->sha1
, 0, NULL
);
339 struct tag
*t
= (struct tag
*)o
;
341 printf("%stag %s%s\n",
342 diff_get_color_opt(&rev
.diffopt
, DIFF_COMMIT
),
344 diff_get_color_opt(&rev
.diffopt
, DIFF_RESET
));
345 ret
= show_object(o
->sha1
, 1, &rev
);
346 objects
[i
].item
= (struct object
*)t
->tagged
;
351 printf("%stree %s%s\n\n",
352 diff_get_color_opt(&rev
.diffopt
, DIFF_COMMIT
),
354 diff_get_color_opt(&rev
.diffopt
, DIFF_RESET
));
355 read_tree_recursive((struct tree
*)o
, "", 0, 0, NULL
,
359 rev
.pending
.nr
= rev
.pending
.alloc
= 0;
360 rev
.pending
.objects
= NULL
;
361 add_object_array(o
, name
, &rev
.pending
);
362 ret
= cmd_log_walk(&rev
);
365 ret
= error("Unknown type: %d", o
->type
);
373 * This is equivalent to "git log -g --abbrev-commit --pretty=oneline"
375 int cmd_log_reflog(int argc
, const char **argv
, const char *prefix
)
379 git_config(git_log_config
);
381 if (diff_use_color_default
== -1)
382 diff_use_color_default
= git_use_color_default
;
384 init_revisions(&rev
, prefix
);
385 init_reflog_walk(&rev
.reflog_info
);
386 rev
.abbrev_commit
= 1;
387 rev
.verbose_header
= 1;
388 cmd_log_init(argc
, argv
, prefix
, &rev
);
391 * This means that we override whatever commit format the user gave
392 * on the cmd line. Sad, but cmd_log_init() currently doesn't
393 * allow us to set a different default.
395 rev
.commit_format
= CMIT_FMT_ONELINE
;
396 rev
.always_show_header
= 1;
399 * We get called through "git reflog", so unlike the other log
400 * routines, we need to set up our pager manually..
404 return cmd_log_walk(&rev
);
407 int cmd_log(int argc
, const char **argv
, const char *prefix
)
411 git_config(git_log_config
);
413 if (diff_use_color_default
== -1)
414 diff_use_color_default
= git_use_color_default
;
416 init_revisions(&rev
, prefix
);
417 rev
.always_show_header
= 1;
418 cmd_log_init(argc
, argv
, prefix
, &rev
);
419 return cmd_log_walk(&rev
);
423 #define FORMAT_PATCH_NAME_MAX 64
425 static int istitlechar(char c
)
427 return (c
>= 'a' && c
<= 'z') || (c
>= 'A' && c
<= 'Z') ||
428 (c
>= '0' && c
<= '9') || c
== '.' || c
== '_';
431 static char *extra_headers
= NULL
;
432 static int extra_headers_size
= 0;
433 static const char *fmt_patch_suffix
= ".patch";
434 static int numbered
= 0;
435 static int auto_number
= 0;
437 static int git_format_config(const char *var
, const char *value
)
439 if (!strcmp(var
, "format.headers")) {
443 die("format.headers without value");
445 extra_headers_size
+= len
+ 1;
446 extra_headers
= xrealloc(extra_headers
, extra_headers_size
);
447 extra_headers
[extra_headers_size
- len
- 1] = 0;
448 strcat(extra_headers
, value
);
451 if (!strcmp(var
, "format.suffix")) {
453 return config_error_nonbool(var
);
454 fmt_patch_suffix
= xstrdup(value
);
457 if (!strcmp(var
, "diff.color") || !strcmp(var
, "color.diff")) {
460 if (!strcmp(var
, "format.numbered")) {
461 if (value
&& !strcasecmp(value
, "auto")) {
465 numbered
= git_config_bool(var
, value
);
469 return git_log_config(var
, value
);
473 static FILE *realstdout
= NULL
;
474 static const char *output_directory
= NULL
;
476 static int reopen_stdout(struct commit
*commit
, int nr
, int keep_subject
,
479 char filename
[PATH_MAX
];
482 int suffix_len
= strlen(fmt_patch_suffix
) + 1;
484 if (output_directory
) {
485 if (strlen(output_directory
) >=
486 sizeof(filename
) - FORMAT_PATCH_NAME_MAX
- suffix_len
)
487 return error("name of output directory is too long");
488 strlcpy(filename
, output_directory
, sizeof(filename
) - suffix_len
);
489 len
= strlen(filename
);
490 if (filename
[len
- 1] != '/')
491 filename
[len
++] = '/';
494 if (numbered_files
) {
495 sprintf(filename
+ len
, "%d", nr
);
496 len
= strlen(filename
);
499 sprintf(filename
+ len
, "%04d", nr
);
500 len
= strlen(filename
);
502 sol
= strstr(commit
->buffer
, "\n\n");
507 /* strip [PATCH] or [PATCH blabla] */
508 if (!keep_subject
&& !prefixcmp(sol
, "[PATCH")) {
509 char *eos
= strchr(sol
+ 6, ']');
511 while (isspace(*eos
))
518 j
< FORMAT_PATCH_NAME_MAX
- suffix_len
- 5 &&
519 len
< sizeof(filename
) - suffix_len
&&
520 sol
[j
] && sol
[j
] != '\n';
522 if (istitlechar(sol
[j
])) {
524 filename
[len
++] = '-';
527 filename
[len
++] = sol
[j
];
529 while (sol
[j
+ 1] == '.')
534 while (filename
[len
- 1] == '.'
535 || filename
[len
- 1] == '-')
539 if (len
+ suffix_len
>= sizeof(filename
))
540 return error("Patch pathname too long");
541 strcpy(filename
+ len
, fmt_patch_suffix
);
544 fprintf(realstdout
, "%s\n", filename
);
545 if (freopen(filename
, "w", stdout
) == NULL
)
546 return error("Cannot open patch file %s",filename
);
551 static void get_patch_ids(struct rev_info
*rev
, struct patch_ids
*ids
, const char *prefix
)
553 struct rev_info check_rev
;
554 struct commit
*commit
;
555 struct object
*o1
, *o2
;
556 unsigned flags1
, flags2
;
558 if (rev
->pending
.nr
!= 2)
559 die("Need exactly one range.");
561 o1
= rev
->pending
.objects
[0].item
;
563 o2
= rev
->pending
.objects
[1].item
;
566 if ((flags1
& UNINTERESTING
) == (flags2
& UNINTERESTING
))
571 /* given a range a..b get all patch ids for b..a */
572 init_revisions(&check_rev
, prefix
);
573 o1
->flags
^= UNINTERESTING
;
574 o2
->flags
^= UNINTERESTING
;
575 add_pending_object(&check_rev
, o1
, "o1");
576 add_pending_object(&check_rev
, o2
, "o2");
577 if (prepare_revision_walk(&check_rev
))
578 die("revision walk setup failed");
580 while ((commit
= get_revision(&check_rev
)) != NULL
) {
582 if (commit
->parents
&& commit
->parents
->next
)
585 add_commit_patch_id(commit
, ids
);
588 /* reset for next revision walk */
589 clear_commit_marks((struct commit
*)o1
,
590 SEEN
| UNINTERESTING
| SHOWN
| ADDED
);
591 clear_commit_marks((struct commit
*)o2
,
592 SEEN
| UNINTERESTING
| SHOWN
| ADDED
);
597 static void gen_message_id(char *dest
, unsigned int length
, char *base
)
599 const char *committer
= git_committer_info(IDENT_WARN_ON_NO_NAME
);
600 const char *email_start
= strrchr(committer
, '<');
601 const char *email_end
= strrchr(committer
, '>');
602 if(!email_start
|| !email_end
|| email_start
> email_end
- 1)
603 die("Could not extract email from committer identity.");
604 snprintf(dest
, length
, "%s.%lu.git.%.*s", base
,
605 (unsigned long) time(NULL
),
606 (int)(email_end
- email_start
- 1), email_start
+ 1);
609 static const char *clean_message_id(const char *msg_id
)
612 const char *a
, *z
, *m
;
615 while ((ch
= *m
) && (isspace(ch
) || (ch
== '<')))
620 if (!isspace(ch
) && (ch
!= '>'))
625 die("insane in-reply-to: %s", msg_id
);
628 return xmemdupz(a
, z
- a
);
631 int cmd_format_patch(int argc
, const char **argv
, const char *prefix
)
633 struct commit
*commit
;
634 struct commit
**list
= NULL
;
636 int nr
= 0, total
, i
, j
;
638 int start_number
= -1;
639 int keep_subject
= 0;
640 int numbered_files
= 0; /* _just_ numbers */
641 int subject_prefix
= 0;
642 int ignore_if_in_upstream
= 0;
644 const char *in_reply_to
= NULL
;
645 struct patch_ids ids
;
646 char *add_signoff
= NULL
;
647 char message_id
[1024];
648 char ref_message_id
[1024];
650 git_config(git_format_config
);
651 init_revisions(&rev
, prefix
);
652 rev
.commit_format
= CMIT_FMT_EMAIL
;
653 rev
.verbose_header
= 1;
655 rev
.combine_merges
= 0;
656 rev
.ignore_merges
= 1;
657 rev
.diffopt
.msg_sep
= "";
658 DIFF_OPT_SET(&rev
.diffopt
, RECURSIVE
);
660 rev
.subject_prefix
= fmt_patch_subject_prefix
;
661 rev
.extra_headers
= extra_headers
;
664 * Parse the arguments before setup_revisions(), or something
665 * like "git format-patch -o a123 HEAD^.." may fail; a123 is
666 * possibly a valid SHA1.
668 for (i
= 1, j
= 1; i
< argc
; i
++) {
669 if (!strcmp(argv
[i
], "--stdout"))
671 else if (!strcmp(argv
[i
], "-n") ||
672 !strcmp(argv
[i
], "--numbered"))
674 else if (!strcmp(argv
[i
], "-N") ||
675 !strcmp(argv
[i
], "--no-numbered")) {
679 else if (!prefixcmp(argv
[i
], "--start-number="))
680 start_number
= strtol(argv
[i
] + 15, NULL
, 10);
681 else if (!strcmp(argv
[i
], "--numbered-files"))
683 else if (!strcmp(argv
[i
], "--start-number")) {
686 die("Need a number for --start-number");
687 start_number
= strtol(argv
[i
], NULL
, 10);
689 else if (!strcmp(argv
[i
], "-k") ||
690 !strcmp(argv
[i
], "--keep-subject")) {
694 else if (!strcmp(argv
[i
], "--output-directory") ||
695 !strcmp(argv
[i
], "-o")) {
698 die("Which directory?");
699 if (output_directory
)
700 die("Two output directories?");
701 output_directory
= argv
[i
];
703 else if (!strcmp(argv
[i
], "--signoff") ||
704 !strcmp(argv
[i
], "-s")) {
705 const char *committer
;
707 committer
= git_committer_info(IDENT_ERROR_ON_NO_NAME
);
708 endpos
= strchr(committer
, '>');
710 die("bogos committer info %s\n", committer
);
711 add_signoff
= xmemdupz(committer
, endpos
- committer
+ 1);
713 else if (!strcmp(argv
[i
], "--attach")) {
714 rev
.mime_boundary
= git_version_string
;
717 else if (!prefixcmp(argv
[i
], "--attach=")) {
718 rev
.mime_boundary
= argv
[i
] + 9;
721 else if (!strcmp(argv
[i
], "--inline")) {
722 rev
.mime_boundary
= git_version_string
;
725 else if (!prefixcmp(argv
[i
], "--inline=")) {
726 rev
.mime_boundary
= argv
[i
] + 9;
729 else if (!strcmp(argv
[i
], "--ignore-if-in-upstream"))
730 ignore_if_in_upstream
= 1;
731 else if (!strcmp(argv
[i
], "--thread"))
733 else if (!prefixcmp(argv
[i
], "--in-reply-to="))
734 in_reply_to
= argv
[i
] + 14;
735 else if (!strcmp(argv
[i
], "--in-reply-to")) {
738 die("Need a Message-Id for --in-reply-to");
739 in_reply_to
= argv
[i
];
740 } else if (!prefixcmp(argv
[i
], "--subject-prefix=")) {
742 rev
.subject_prefix
= argv
[i
] + 17;
743 } else if (!prefixcmp(argv
[i
], "--suffix="))
744 fmt_patch_suffix
= argv
[i
] + 9;
750 if (start_number
< 0)
752 if (numbered
&& keep_subject
)
753 die ("-n and -k are mutually exclusive.");
754 if (keep_subject
&& subject_prefix
)
755 die ("--subject-prefix and -k are mutually exclusive.");
756 if (numbered_files
&& use_stdout
)
757 die ("--numbered-files and --stdout are mutually exclusive.");
759 argc
= setup_revisions(argc
, argv
, &rev
, "HEAD");
761 die ("unrecognized argument: %s", argv
[1]);
763 if (!rev
.diffopt
.output_format
)
764 rev
.diffopt
.output_format
= DIFF_FORMAT_DIFFSTAT
| DIFF_FORMAT_SUMMARY
| DIFF_FORMAT_PATCH
;
766 if (!DIFF_OPT_TST(&rev
.diffopt
, TEXT
))
767 DIFF_OPT_SET(&rev
.diffopt
, BINARY
);
769 if (!output_directory
&& !use_stdout
)
770 output_directory
= prefix
;
772 if (output_directory
) {
774 die("standard output, or directory, which one?");
775 if (mkdir(output_directory
, 0777) < 0 && errno
!= EEXIST
)
776 die("Could not create directory %s",
780 if (rev
.pending
.nr
== 1) {
781 if (rev
.max_count
< 0 && !rev
.show_root_diff
) {
783 * This is traditional behaviour of "git format-patch
784 * origin" that prepares what the origin side still
787 rev
.pending
.objects
[0].item
->flags
|= UNINTERESTING
;
788 add_head_to_pending(&rev
);
791 * Otherwise, it is "format-patch -22 HEAD", and/or
792 * "format-patch --root HEAD". The user wants
793 * get_revision() to do the usual traversal.
797 if (ignore_if_in_upstream
)
798 get_patch_ids(&rev
, &ids
, prefix
);
801 realstdout
= xfdopen(xdup(1), "w");
803 if (prepare_revision_walk(&rev
))
804 die("revision walk setup failed");
805 while ((commit
= get_revision(&rev
)) != NULL
) {
807 if (commit
->parents
&& commit
->parents
->next
)
810 if (ignore_if_in_upstream
&&
811 has_commit_patch_id(commit
, &ids
))
815 list
= xrealloc(list
, nr
* sizeof(list
[0]));
816 list
[nr
- 1] = commit
;
819 if (!keep_subject
&& auto_number
&& total
> 1)
822 rev
.total
= total
+ start_number
- 1;
823 rev
.add_signoff
= add_signoff
;
825 rev
.ref_message_id
= clean_message_id(in_reply_to
);
829 rev
.nr
= total
- nr
+ (start_number
- 1);
830 /* Make the second and subsequent mails replies to the first */
832 if (nr
== (total
- 2)) {
833 strncpy(ref_message_id
, message_id
,
834 sizeof(ref_message_id
));
835 ref_message_id
[sizeof(ref_message_id
)-1]='\0';
836 rev
.ref_message_id
= ref_message_id
;
838 gen_message_id(message_id
, sizeof(message_id
),
839 sha1_to_hex(commit
->object
.sha1
));
840 rev
.message_id
= message_id
;
843 if (reopen_stdout(commit
, rev
.nr
, keep_subject
,
845 die("Failed to create output files");
846 shown
= log_tree_commit(&rev
, commit
);
847 free(commit
->buffer
);
848 commit
->buffer
= NULL
;
850 /* We put one extra blank line between formatted
851 * patches and this flag is used by log-tree code
852 * to see if it needs to emit a LF before showing
853 * the log; when using one file per patch, we do
854 * not want the extra blank line.
859 if (rev
.mime_boundary
)
860 printf("\n--%s%s--\n\n\n",
861 mime_boundary_leader
,
864 printf("-- \n%s\n\n", git_version_string
);
870 if (ignore_if_in_upstream
)
871 free_patch_ids(&ids
);
875 static int add_pending_commit(const char *arg
, struct rev_info
*revs
, int flags
)
877 unsigned char sha1
[20];
878 if (get_sha1(arg
, sha1
) == 0) {
879 struct commit
*commit
= lookup_commit_reference(sha1
);
881 commit
->object
.flags
|= flags
;
882 add_pending_object(revs
, &commit
->object
, arg
);
889 static const char cherry_usage
[] =
890 "git-cherry [-v] <upstream> [<head>] [<limit>]";
891 int cmd_cherry(int argc
, const char **argv
, const char *prefix
)
893 struct rev_info revs
;
894 struct patch_ids ids
;
895 struct commit
*commit
;
896 struct commit_list
*list
= NULL
;
897 const char *upstream
;
898 const char *head
= "HEAD";
899 const char *limit
= NULL
;
902 if (argc
> 1 && !strcmp(argv
[1], "-v")) {
922 init_revisions(&revs
, prefix
);
924 revs
.combine_merges
= 0;
925 revs
.ignore_merges
= 1;
926 DIFF_OPT_SET(&revs
.diffopt
, RECURSIVE
);
928 if (add_pending_commit(head
, &revs
, 0))
929 die("Unknown commit %s", head
);
930 if (add_pending_commit(upstream
, &revs
, UNINTERESTING
))
931 die("Unknown commit %s", upstream
);
933 /* Don't say anything if head and upstream are the same. */
934 if (revs
.pending
.nr
== 2) {
935 struct object_array_entry
*o
= revs
.pending
.objects
;
936 if (hashcmp(o
[0].item
->sha1
, o
[1].item
->sha1
) == 0)
940 get_patch_ids(&revs
, &ids
, prefix
);
942 if (limit
&& add_pending_commit(limit
, &revs
, UNINTERESTING
))
943 die("Unknown commit %s", limit
);
945 /* reverse the list of commits */
946 if (prepare_revision_walk(&revs
))
947 die("revision walk setup failed");
948 while ((commit
= get_revision(&revs
)) != NULL
) {
950 if (commit
->parents
&& commit
->parents
->next
)
953 commit_list_insert(commit
, &list
);
960 if (has_commit_patch_id(commit
, &ids
))
965 strbuf_init(&buf
, 0);
966 pretty_print_commit(CMIT_FMT_ONELINE
, commit
,
967 &buf
, 0, NULL
, NULL
, 0, 0);
968 printf("%c %s %s\n", sign
,
969 sha1_to_hex(commit
->object
.sha1
), buf
.buf
);
970 strbuf_release(&buf
);
973 printf("%c %s\n", sign
,
974 sha1_to_hex(commit
->object
.sha1
));
980 free_patch_ids(&ids
);