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"
18 static int default_show_root
= 1;
19 static const char *fmt_patch_subject_prefix
= "PATCH";
21 static void add_name_decoration(const char *prefix
, const char *name
, struct object
*obj
)
23 int plen
= strlen(prefix
);
24 int nlen
= strlen(name
);
25 struct name_decoration
*res
= xmalloc(sizeof(struct name_decoration
) + plen
+ nlen
);
26 memcpy(res
->name
, prefix
, plen
);
27 memcpy(res
->name
+ plen
, name
, nlen
+ 1);
28 res
->next
= add_decoration(&name_decoration
, obj
, res
);
31 static int add_ref_decoration(const char *refname
, const unsigned char *sha1
, int flags
, void *cb_data
)
33 struct object
*obj
= parse_object(sha1
);
36 add_name_decoration("", refname
, obj
);
37 while (obj
->type
== OBJ_TAG
) {
38 obj
= ((struct tag
*)obj
)->tagged
;
41 add_name_decoration("tag: ", refname
, obj
);
46 static void cmd_log_init(int argc
, const char **argv
, const char *prefix
,
52 rev
->abbrev
= DEFAULT_ABBREV
;
53 rev
->commit_format
= CMIT_FMT_DEFAULT
;
54 rev
->verbose_header
= 1;
55 DIFF_OPT_SET(&rev
->diffopt
, RECURSIVE
);
56 rev
->show_root_diff
= default_show_root
;
57 rev
->subject_prefix
= fmt_patch_subject_prefix
;
58 argc
= setup_revisions(argc
, argv
, rev
, "HEAD");
59 if (rev
->diffopt
.pickaxe
|| rev
->diffopt
.filter
)
60 rev
->always_show_header
= 0;
61 if (DIFF_OPT_TST(&rev
->diffopt
, FOLLOW_RENAMES
)) {
62 rev
->always_show_header
= 0;
63 if (rev
->diffopt
.nr_paths
!= 1)
64 usage("git logs can only follow renames on one pathname at a time");
66 for (i
= 1; i
< argc
; i
++) {
67 const char *arg
= argv
[i
];
68 if (!strcmp(arg
, "--decorate")) {
70 for_each_ref(add_ref_decoration
, NULL
);
73 die("unrecognized argument: %s", arg
);
78 * This gives a rough estimate for how many commits we
79 * will print out in the list.
81 static int estimate_commit_count(struct rev_info
*rev
, struct commit_list
*list
)
86 struct commit
*commit
= list
->item
;
87 unsigned int flags
= commit
->object
.flags
;
89 if (!(flags
& (TREESAME
| UNINTERESTING
)))
95 static void show_early_header(struct rev_info
*rev
, const char *stage
, int nr
)
99 if (rev
->commit_format
!= CMIT_FMT_ONELINE
)
100 putchar(rev
->diffopt
.line_termination
);
102 printf("Final output: %d %s\n", nr
, stage
);
105 struct itimerval early_output_timer
;
107 static void log_show_early(struct rev_info
*revs
, struct commit_list
*list
)
109 int i
= revs
->early_output
;
112 sort_in_topological_order(&list
, revs
->lifo
);
114 struct commit
*commit
= list
->item
;
115 switch (simplify_commit(revs
, commit
)) {
118 int n
= estimate_commit_count(revs
, list
);
119 show_early_header(revs
, "incomplete", n
);
122 log_tree_commit(revs
, commit
);
133 /* Did we already get enough commits for the early output? */
138 * ..if no, then repeat it twice a second until we
141 * NOTE! We don't use "it_interval", because if the
142 * reader isn't listening, we want our output to be
143 * throttled by the writing, and not have the timer
144 * trigger every second even if we're blocked on a
147 early_output_timer
.it_value
.tv_sec
= 0;
148 early_output_timer
.it_value
.tv_usec
= 500000;
149 setitimer(ITIMER_REAL
, &early_output_timer
, NULL
);
152 static void early_output(int signal
)
154 show_early_output
= log_show_early
;
157 static void setup_early_output(struct rev_info
*rev
)
162 * Set up the signal handler, minimally intrusively:
163 * we only set a single volatile integer word (not
164 * using sigatomic_t - trying to avoid unnecessary
165 * system dependencies and headers), and using
168 memset(&sa
, 0, sizeof(sa
));
169 sa
.sa_handler
= early_output
;
170 sigemptyset(&sa
.sa_mask
);
171 sa
.sa_flags
= SA_RESTART
;
172 sigaction(SIGALRM
, &sa
, NULL
);
175 * If we can get the whole output in less than a
176 * tenth of a second, don't even bother doing the
177 * early-output thing..
179 * This is a one-time-only trigger.
181 early_output_timer
.it_value
.tv_sec
= 0;
182 early_output_timer
.it_value
.tv_usec
= 100000;
183 setitimer(ITIMER_REAL
, &early_output_timer
, NULL
);
186 static void finish_early_output(struct rev_info
*rev
)
188 int n
= estimate_commit_count(rev
, rev
->commits
);
189 signal(SIGALRM
, SIG_IGN
);
190 show_early_header(rev
, "done", n
);
193 static int cmd_log_walk(struct rev_info
*rev
)
195 struct commit
*commit
;
197 if (rev
->early_output
)
198 setup_early_output(rev
);
200 prepare_revision_walk(rev
);
202 if (rev
->early_output
)
203 finish_early_output(rev
);
205 while ((commit
= get_revision(rev
)) != NULL
) {
206 log_tree_commit(rev
, commit
);
207 if (!rev
->reflog_info
) {
208 /* we allow cycles in reflog ancestry */
209 free(commit
->buffer
);
210 commit
->buffer
= NULL
;
212 free_commit_list(commit
->parents
);
213 commit
->parents
= NULL
;
218 static int git_log_config(const char *var
, const char *value
)
220 if (!strcmp(var
, "format.subjectprefix")) {
222 config_error_nonbool(var
);
223 fmt_patch_subject_prefix
= xstrdup(value
);
226 if (!strcmp(var
, "log.showroot")) {
227 default_show_root
= git_config_bool(var
, value
);
230 return git_diff_ui_config(var
, value
);
233 int cmd_whatchanged(int argc
, const char **argv
, const char *prefix
)
237 git_config(git_log_config
);
238 init_revisions(&rev
, prefix
);
240 rev
.simplify_history
= 0;
241 cmd_log_init(argc
, argv
, prefix
, &rev
);
242 if (!rev
.diffopt
.output_format
)
243 rev
.diffopt
.output_format
= DIFF_FORMAT_RAW
;
244 return cmd_log_walk(&rev
);
247 static void show_tagger(char *buf
, int len
, struct rev_info
*rev
)
253 email_end
= memchr(buf
, '>', len
);
259 date
= strtoul(p
, &p
, 10);
262 tz
= (int)strtol(p
, NULL
, 10);
263 printf("Tagger: %.*s\nDate: %s\n", (int)(email_end
- buf
), buf
,
264 show_date(date
, tz
, rev
->date_mode
));
267 static int show_object(const unsigned char *sha1
, int show_tag_object
,
268 struct rev_info
*rev
)
271 enum object_type type
;
272 char *buf
= read_sha1_file(sha1
, &type
, &size
);
276 return error("Could not read object %s", sha1_to_hex(sha1
));
279 while (offset
< size
&& buf
[offset
] != '\n') {
280 int new_offset
= offset
+ 1;
281 while (new_offset
< size
&& buf
[new_offset
++] != '\n')
283 if (!prefixcmp(buf
+ offset
, "tagger "))
284 show_tagger(buf
+ offset
+ 7,
285 new_offset
- offset
- 7, rev
);
290 fwrite(buf
+ offset
, size
- offset
, 1, stdout
);
295 static int show_tree_object(const unsigned char *sha1
,
296 const char *base
, int baselen
,
297 const char *pathname
, unsigned mode
, int stage
)
299 printf("%s%s\n", pathname
, S_ISDIR(mode
) ? "/" : "");
303 int cmd_show(int argc
, const char **argv
, const char *prefix
)
306 struct object_array_entry
*objects
;
307 int i
, count
, ret
= 0;
309 git_config(git_log_config
);
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
);
336 objects
[i
].item
= (struct object
*)t
->tagged
;
341 printf("%stree %s%s\n\n",
342 diff_get_color_opt(&rev
.diffopt
, DIFF_COMMIT
),
344 diff_get_color_opt(&rev
.diffopt
, DIFF_RESET
));
345 read_tree_recursive((struct tree
*)o
, "", 0, 0, NULL
,
349 rev
.pending
.nr
= rev
.pending
.alloc
= 0;
350 rev
.pending
.objects
= NULL
;
351 add_object_array(o
, name
, &rev
.pending
);
352 ret
= cmd_log_walk(&rev
);
355 ret
= error("Unknown type: %d", o
->type
);
363 * This is equivalent to "git log -g --abbrev-commit --pretty=oneline"
365 int cmd_log_reflog(int argc
, const char **argv
, const char *prefix
)
369 git_config(git_log_config
);
370 init_revisions(&rev
, prefix
);
371 init_reflog_walk(&rev
.reflog_info
);
372 rev
.abbrev_commit
= 1;
373 rev
.verbose_header
= 1;
374 cmd_log_init(argc
, argv
, prefix
, &rev
);
377 * This means that we override whatever commit format the user gave
378 * on the cmd line. Sad, but cmd_log_init() currently doesn't
379 * allow us to set a different default.
381 rev
.commit_format
= CMIT_FMT_ONELINE
;
382 rev
.always_show_header
= 1;
385 * We get called through "git reflog", so unlike the other log
386 * routines, we need to set up our pager manually..
390 return cmd_log_walk(&rev
);
393 int cmd_log(int argc
, const char **argv
, const char *prefix
)
397 git_config(git_log_config
);
398 init_revisions(&rev
, prefix
);
399 rev
.always_show_header
= 1;
400 cmd_log_init(argc
, argv
, prefix
, &rev
);
401 return cmd_log_walk(&rev
);
405 #define FORMAT_PATCH_NAME_MAX 64
407 static int istitlechar(char c
)
409 return (c
>= 'a' && c
<= 'z') || (c
>= 'A' && c
<= 'Z') ||
410 (c
>= '0' && c
<= '9') || c
== '.' || c
== '_';
413 static char *extra_headers
= NULL
;
414 static int extra_headers_size
= 0;
415 static const char *fmt_patch_suffix
= ".patch";
416 static int numbered
= 0;
417 static int auto_number
= 0;
419 static int git_format_config(const char *var
, const char *value
)
421 if (!strcmp(var
, "format.headers")) {
425 die("format.headers without value");
427 extra_headers_size
+= len
+ 1;
428 extra_headers
= xrealloc(extra_headers
, extra_headers_size
);
429 extra_headers
[extra_headers_size
- len
- 1] = 0;
430 strcat(extra_headers
, value
);
433 if (!strcmp(var
, "format.suffix")) {
435 return config_error_nonbool(var
);
436 fmt_patch_suffix
= xstrdup(value
);
439 if (!strcmp(var
, "diff.color") || !strcmp(var
, "color.diff")) {
442 if (!strcmp(var
, "format.numbered")) {
443 if (value
&& !strcasecmp(value
, "auto")) {
447 numbered
= git_config_bool(var
, value
);
451 return git_log_config(var
, value
);
455 static FILE *realstdout
= NULL
;
456 static const char *output_directory
= NULL
;
458 static int reopen_stdout(struct commit
*commit
, int nr
, int keep_subject
,
461 char filename
[PATH_MAX
];
464 int suffix_len
= strlen(fmt_patch_suffix
) + 1;
466 if (output_directory
) {
467 if (strlen(output_directory
) >=
468 sizeof(filename
) - FORMAT_PATCH_NAME_MAX
- suffix_len
)
469 return error("name of output directory is too long");
470 strlcpy(filename
, output_directory
, sizeof(filename
) - suffix_len
);
471 len
= strlen(filename
);
472 if (filename
[len
- 1] != '/')
473 filename
[len
++] = '/';
476 if (numbered_files
) {
477 sprintf(filename
+ len
, "%d", nr
);
478 len
= strlen(filename
);
481 sprintf(filename
+ len
, "%04d", nr
);
482 len
= strlen(filename
);
484 sol
= strstr(commit
->buffer
, "\n\n");
489 /* strip [PATCH] or [PATCH blabla] */
490 if (!keep_subject
&& !prefixcmp(sol
, "[PATCH")) {
491 char *eos
= strchr(sol
+ 6, ']');
493 while (isspace(*eos
))
500 j
< FORMAT_PATCH_NAME_MAX
- suffix_len
- 5 &&
501 len
< sizeof(filename
) - suffix_len
&&
502 sol
[j
] && sol
[j
] != '\n';
504 if (istitlechar(sol
[j
])) {
506 filename
[len
++] = '-';
509 filename
[len
++] = sol
[j
];
511 while (sol
[j
+ 1] == '.')
516 while (filename
[len
- 1] == '.'
517 || filename
[len
- 1] == '-')
521 if (len
+ suffix_len
>= sizeof(filename
))
522 return error("Patch pathname too long");
523 strcpy(filename
+ len
, fmt_patch_suffix
);
526 fprintf(realstdout
, "%s\n", filename
);
527 if (freopen(filename
, "w", stdout
) == NULL
)
528 return error("Cannot open patch file %s",filename
);
533 static void get_patch_ids(struct rev_info
*rev
, struct patch_ids
*ids
, const char *prefix
)
535 struct rev_info check_rev
;
536 struct commit
*commit
;
537 struct object
*o1
, *o2
;
538 unsigned flags1
, flags2
;
540 if (rev
->pending
.nr
!= 2)
541 die("Need exactly one range.");
543 o1
= rev
->pending
.objects
[0].item
;
545 o2
= rev
->pending
.objects
[1].item
;
548 if ((flags1
& UNINTERESTING
) == (flags2
& UNINTERESTING
))
553 /* given a range a..b get all patch ids for b..a */
554 init_revisions(&check_rev
, prefix
);
555 o1
->flags
^= UNINTERESTING
;
556 o2
->flags
^= UNINTERESTING
;
557 add_pending_object(&check_rev
, o1
, "o1");
558 add_pending_object(&check_rev
, o2
, "o2");
559 prepare_revision_walk(&check_rev
);
561 while ((commit
= get_revision(&check_rev
)) != NULL
) {
563 if (commit
->parents
&& commit
->parents
->next
)
566 add_commit_patch_id(commit
, ids
);
569 /* reset for next revision walk */
570 clear_commit_marks((struct commit
*)o1
,
571 SEEN
| UNINTERESTING
| SHOWN
| ADDED
);
572 clear_commit_marks((struct commit
*)o2
,
573 SEEN
| UNINTERESTING
| SHOWN
| ADDED
);
578 static void gen_message_id(char *dest
, unsigned int length
, char *base
)
580 const char *committer
= git_committer_info(IDENT_WARN_ON_NO_NAME
);
581 const char *email_start
= strrchr(committer
, '<');
582 const char *email_end
= strrchr(committer
, '>');
583 if(!email_start
|| !email_end
|| email_start
> email_end
- 1)
584 die("Could not extract email from committer identity.");
585 snprintf(dest
, length
, "%s.%lu.git.%.*s", base
,
586 (unsigned long) time(NULL
),
587 (int)(email_end
- email_start
- 1), email_start
+ 1);
590 static const char *clean_message_id(const char *msg_id
)
593 const char *a
, *z
, *m
;
596 while ((ch
= *m
) && (isspace(ch
) || (ch
== '<')))
601 if (!isspace(ch
) && (ch
!= '>'))
606 die("insane in-reply-to: %s", msg_id
);
609 return xmemdupz(a
, z
- a
);
612 int cmd_format_patch(int argc
, const char **argv
, const char *prefix
)
614 struct commit
*commit
;
615 struct commit
**list
= NULL
;
617 int nr
= 0, total
, i
, j
;
619 int start_number
= -1;
620 int keep_subject
= 0;
621 int numbered_files
= 0; /* _just_ numbers */
622 int subject_prefix
= 0;
623 int ignore_if_in_upstream
= 0;
625 const char *in_reply_to
= NULL
;
626 struct patch_ids ids
;
627 char *add_signoff
= NULL
;
628 char message_id
[1024];
629 char ref_message_id
[1024];
631 git_config(git_format_config
);
632 init_revisions(&rev
, prefix
);
633 rev
.commit_format
= CMIT_FMT_EMAIL
;
634 rev
.verbose_header
= 1;
636 rev
.combine_merges
= 0;
637 rev
.ignore_merges
= 1;
638 rev
.diffopt
.msg_sep
= "";
639 DIFF_OPT_SET(&rev
.diffopt
, RECURSIVE
);
641 rev
.subject_prefix
= fmt_patch_subject_prefix
;
642 rev
.extra_headers
= extra_headers
;
645 * Parse the arguments before setup_revisions(), or something
646 * like "git format-patch -o a123 HEAD^.." may fail; a123 is
647 * possibly a valid SHA1.
649 for (i
= 1, j
= 1; i
< argc
; i
++) {
650 if (!strcmp(argv
[i
], "--stdout"))
652 else if (!strcmp(argv
[i
], "-n") ||
653 !strcmp(argv
[i
], "--numbered"))
655 else if (!strcmp(argv
[i
], "-N") ||
656 !strcmp(argv
[i
], "--no-numbered")) {
660 else if (!prefixcmp(argv
[i
], "--start-number="))
661 start_number
= strtol(argv
[i
] + 15, NULL
, 10);
662 else if (!strcmp(argv
[i
], "--numbered-files"))
664 else if (!strcmp(argv
[i
], "--start-number")) {
667 die("Need a number for --start-number");
668 start_number
= strtol(argv
[i
], NULL
, 10);
670 else if (!strcmp(argv
[i
], "-k") ||
671 !strcmp(argv
[i
], "--keep-subject")) {
675 else if (!strcmp(argv
[i
], "--output-directory") ||
676 !strcmp(argv
[i
], "-o")) {
679 die("Which directory?");
680 if (output_directory
)
681 die("Two output directories?");
682 output_directory
= argv
[i
];
684 else if (!strcmp(argv
[i
], "--signoff") ||
685 !strcmp(argv
[i
], "-s")) {
686 const char *committer
;
688 committer
= git_committer_info(IDENT_ERROR_ON_NO_NAME
);
689 endpos
= strchr(committer
, '>');
691 die("bogos committer info %s\n", committer
);
692 add_signoff
= xmemdupz(committer
, endpos
- committer
+ 1);
694 else if (!strcmp(argv
[i
], "--attach")) {
695 rev
.mime_boundary
= git_version_string
;
698 else if (!prefixcmp(argv
[i
], "--attach=")) {
699 rev
.mime_boundary
= argv
[i
] + 9;
702 else if (!strcmp(argv
[i
], "--inline")) {
703 rev
.mime_boundary
= git_version_string
;
706 else if (!prefixcmp(argv
[i
], "--inline=")) {
707 rev
.mime_boundary
= argv
[i
] + 9;
710 else if (!strcmp(argv
[i
], "--ignore-if-in-upstream"))
711 ignore_if_in_upstream
= 1;
712 else if (!strcmp(argv
[i
], "--thread"))
714 else if (!prefixcmp(argv
[i
], "--in-reply-to="))
715 in_reply_to
= argv
[i
] + 14;
716 else if (!strcmp(argv
[i
], "--in-reply-to")) {
719 die("Need a Message-Id for --in-reply-to");
720 in_reply_to
= argv
[i
];
721 } else if (!prefixcmp(argv
[i
], "--subject-prefix=")) {
723 rev
.subject_prefix
= argv
[i
] + 17;
724 } else if (!prefixcmp(argv
[i
], "--suffix="))
725 fmt_patch_suffix
= argv
[i
] + 9;
731 if (start_number
< 0)
733 if (numbered
&& keep_subject
)
734 die ("-n and -k are mutually exclusive.");
735 if (keep_subject
&& subject_prefix
)
736 die ("--subject-prefix and -k are mutually exclusive.");
737 if (numbered_files
&& use_stdout
)
738 die ("--numbered-files and --stdout are mutually exclusive.");
740 argc
= setup_revisions(argc
, argv
, &rev
, "HEAD");
742 die ("unrecognized argument: %s", argv
[1]);
744 if (!rev
.diffopt
.output_format
)
745 rev
.diffopt
.output_format
= DIFF_FORMAT_DIFFSTAT
| DIFF_FORMAT_SUMMARY
| DIFF_FORMAT_PATCH
;
747 if (!DIFF_OPT_TST(&rev
.diffopt
, TEXT
))
748 DIFF_OPT_SET(&rev
.diffopt
, BINARY
);
750 if (!output_directory
&& !use_stdout
)
751 output_directory
= prefix
;
753 if (output_directory
) {
755 die("standard output, or directory, which one?");
756 if (mkdir(output_directory
, 0777) < 0 && errno
!= EEXIST
)
757 die("Could not create directory %s",
761 if (rev
.pending
.nr
== 1) {
762 if (rev
.max_count
< 0 && !rev
.show_root_diff
) {
764 * This is traditional behaviour of "git format-patch
765 * origin" that prepares what the origin side still
768 rev
.pending
.objects
[0].item
->flags
|= UNINTERESTING
;
769 add_head_to_pending(&rev
);
772 * Otherwise, it is "format-patch -22 HEAD", and/or
773 * "format-patch --root HEAD". The user wants
774 * get_revision() to do the usual traversal.
778 if (ignore_if_in_upstream
)
779 get_patch_ids(&rev
, &ids
, prefix
);
782 realstdout
= xfdopen(xdup(1), "w");
784 prepare_revision_walk(&rev
);
785 while ((commit
= get_revision(&rev
)) != NULL
) {
787 if (commit
->parents
&& commit
->parents
->next
)
790 if (ignore_if_in_upstream
&&
791 has_commit_patch_id(commit
, &ids
))
795 list
= xrealloc(list
, nr
* sizeof(list
[0]));
796 list
[nr
- 1] = commit
;
799 if (!keep_subject
&& auto_number
&& total
> 1)
802 rev
.total
= total
+ start_number
- 1;
803 rev
.add_signoff
= add_signoff
;
805 rev
.ref_message_id
= clean_message_id(in_reply_to
);
809 rev
.nr
= total
- nr
+ (start_number
- 1);
810 /* Make the second and subsequent mails replies to the first */
812 if (nr
== (total
- 2)) {
813 strncpy(ref_message_id
, message_id
,
814 sizeof(ref_message_id
));
815 ref_message_id
[sizeof(ref_message_id
)-1]='\0';
816 rev
.ref_message_id
= ref_message_id
;
818 gen_message_id(message_id
, sizeof(message_id
),
819 sha1_to_hex(commit
->object
.sha1
));
820 rev
.message_id
= message_id
;
823 if (reopen_stdout(commit
, rev
.nr
, keep_subject
,
825 die("Failed to create output files");
826 shown
= log_tree_commit(&rev
, commit
);
827 free(commit
->buffer
);
828 commit
->buffer
= NULL
;
830 /* We put one extra blank line between formatted
831 * patches and this flag is used by log-tree code
832 * to see if it needs to emit a LF before showing
833 * the log; when using one file per patch, we do
834 * not want the extra blank line.
839 if (rev
.mime_boundary
)
840 printf("\n--%s%s--\n\n\n",
841 mime_boundary_leader
,
844 printf("-- \n%s\n\n", git_version_string
);
850 if (ignore_if_in_upstream
)
851 free_patch_ids(&ids
);
855 static int add_pending_commit(const char *arg
, struct rev_info
*revs
, int flags
)
857 unsigned char sha1
[20];
858 if (get_sha1(arg
, sha1
) == 0) {
859 struct commit
*commit
= lookup_commit_reference(sha1
);
861 commit
->object
.flags
|= flags
;
862 add_pending_object(revs
, &commit
->object
, arg
);
869 static const char cherry_usage
[] =
870 "git-cherry [-v] <upstream> [<head>] [<limit>]";
871 int cmd_cherry(int argc
, const char **argv
, const char *prefix
)
873 struct rev_info revs
;
874 struct patch_ids ids
;
875 struct commit
*commit
;
876 struct commit_list
*list
= NULL
;
877 const char *upstream
;
878 const char *head
= "HEAD";
879 const char *limit
= NULL
;
882 if (argc
> 1 && !strcmp(argv
[1], "-v")) {
902 init_revisions(&revs
, prefix
);
904 revs
.combine_merges
= 0;
905 revs
.ignore_merges
= 1;
906 DIFF_OPT_SET(&revs
.diffopt
, RECURSIVE
);
908 if (add_pending_commit(head
, &revs
, 0))
909 die("Unknown commit %s", head
);
910 if (add_pending_commit(upstream
, &revs
, UNINTERESTING
))
911 die("Unknown commit %s", upstream
);
913 /* Don't say anything if head and upstream are the same. */
914 if (revs
.pending
.nr
== 2) {
915 struct object_array_entry
*o
= revs
.pending
.objects
;
916 if (hashcmp(o
[0].item
->sha1
, o
[1].item
->sha1
) == 0)
920 get_patch_ids(&revs
, &ids
, prefix
);
922 if (limit
&& add_pending_commit(limit
, &revs
, UNINTERESTING
))
923 die("Unknown commit %s", limit
);
925 /* reverse the list of commits */
926 prepare_revision_walk(&revs
);
927 while ((commit
= get_revision(&revs
)) != NULL
) {
929 if (commit
->parents
&& commit
->parents
->next
)
932 commit_list_insert(commit
, &list
);
939 if (has_commit_patch_id(commit
, &ids
))
944 strbuf_init(&buf
, 0);
945 pretty_print_commit(CMIT_FMT_ONELINE
, commit
,
946 &buf
, 0, NULL
, NULL
, 0, 0);
947 printf("%c %s %s\n", sign
,
948 sha1_to_hex(commit
->object
.sha1
), buf
.buf
);
949 strbuf_release(&buf
);
952 printf("%c %s\n", sign
,
953 sha1_to_hex(commit
->object
.sha1
));
959 free_patch_ids(&ids
);