Merge branch 'maint' to synchronize with 1.5.3.6
[git] / builtin-log.c
1 /*
2  * Builtin "git log" and related commands (show, whatchanged)
3  *
4  * (C) Copyright 2006 Linus Torvalds
5  *               2006 Junio Hamano
6  */
7 #include "cache.h"
8 #include "commit.h"
9 #include "diff.h"
10 #include "revision.h"
11 #include "log-tree.h"
12 #include "builtin.h"
13 #include "tag.h"
14 #include "reflog-walk.h"
15 #include "patch-ids.h"
16 #include "refs.h"
17
18 static int default_show_root = 1;
19 static const char *fmt_patch_subject_prefix = "PATCH";
20
21 /* this is in builtin-diff.c */
22 void add_head(struct rev_info *revs);
23
24 static void add_name_decoration(const char *prefix, const char *name, struct object *obj)
25 {
26         int plen = strlen(prefix);
27         int nlen = strlen(name);
28         struct name_decoration *res = xmalloc(sizeof(struct name_decoration) + plen + nlen);
29         memcpy(res->name, prefix, plen);
30         memcpy(res->name + plen, name, nlen + 1);
31         res->next = add_decoration(&name_decoration, obj, res);
32 }
33
34 static int add_ref_decoration(const char *refname, const unsigned char *sha1, int flags, void *cb_data)
35 {
36         struct object *obj = parse_object(sha1);
37         if (!obj)
38                 return 0;
39         add_name_decoration("", refname, obj);
40         while (obj->type == OBJ_TAG) {
41                 obj = ((struct tag *)obj)->tagged;
42                 if (!obj)
43                         break;
44                 add_name_decoration("tag: ", refname, obj);
45         }
46         return 0;
47 }
48
49 static void cmd_log_init(int argc, const char **argv, const char *prefix,
50                       struct rev_info *rev)
51 {
52         int i;
53         int decorate = 0;
54
55         rev->abbrev = DEFAULT_ABBREV;
56         rev->commit_format = CMIT_FMT_DEFAULT;
57         rev->verbose_header = 1;
58         rev->diffopt.recursive = 1;
59         rev->show_root_diff = default_show_root;
60         rev->subject_prefix = fmt_patch_subject_prefix;
61         argc = setup_revisions(argc, argv, rev, "HEAD");
62         if (rev->diffopt.pickaxe || rev->diffopt.filter)
63                 rev->always_show_header = 0;
64         if (rev->diffopt.follow_renames) {
65                 rev->always_show_header = 0;
66                 if (rev->diffopt.nr_paths != 1)
67                         usage("git logs can only follow renames on one pathname at a time");
68         }
69         for (i = 1; i < argc; i++) {
70                 const char *arg = argv[i];
71                 if (!strcmp(arg, "--decorate")) {
72                         if (!decorate)
73                                 for_each_ref(add_ref_decoration, NULL);
74                         decorate = 1;
75                 } else
76                         die("unrecognized argument: %s", arg);
77         }
78 }
79
80 static int cmd_log_walk(struct rev_info *rev)
81 {
82         struct commit *commit;
83
84         prepare_revision_walk(rev);
85         while ((commit = get_revision(rev)) != NULL) {
86                 log_tree_commit(rev, commit);
87                 if (!rev->reflog_info) {
88                         /* we allow cycles in reflog ancestry */
89                         free(commit->buffer);
90                         commit->buffer = NULL;
91                 }
92                 free_commit_list(commit->parents);
93                 commit->parents = NULL;
94         }
95         return 0;
96 }
97
98 static int git_log_config(const char *var, const char *value)
99 {
100         if (!strcmp(var, "format.subjectprefix")) {
101                 if (!value)
102                         die("format.subjectprefix without value");
103                 fmt_patch_subject_prefix = xstrdup(value);
104                 return 0;
105         }
106         if (!strcmp(var, "log.showroot")) {
107                 default_show_root = git_config_bool(var, value);
108                 return 0;
109         }
110         return git_diff_ui_config(var, value);
111 }
112
113 int cmd_whatchanged(int argc, const char **argv, const char *prefix)
114 {
115         struct rev_info rev;
116
117         git_config(git_log_config);
118         init_revisions(&rev, prefix);
119         rev.diff = 1;
120         rev.simplify_history = 0;
121         cmd_log_init(argc, argv, prefix, &rev);
122         if (!rev.diffopt.output_format)
123                 rev.diffopt.output_format = DIFF_FORMAT_RAW;
124         return cmd_log_walk(&rev);
125 }
126
127 static int show_object(const unsigned char *sha1, int suppress_header)
128 {
129         unsigned long size;
130         enum object_type type;
131         char *buf = read_sha1_file(sha1, &type, &size);
132         int offset = 0;
133
134         if (!buf)
135                 return error("Could not read object %s", sha1_to_hex(sha1));
136
137         if (suppress_header)
138                 while (offset < size && buf[offset++] != '\n') {
139                         int new_offset = offset;
140                         while (new_offset < size && buf[new_offset++] != '\n')
141                                 ; /* do nothing */
142                         offset = new_offset;
143                 }
144
145         if (offset < size)
146                 fwrite(buf + offset, size - offset, 1, stdout);
147         free(buf);
148         return 0;
149 }
150
151 static int show_tree_object(const unsigned char *sha1,
152                 const char *base, int baselen,
153                 const char *pathname, unsigned mode, int stage)
154 {
155         printf("%s%s\n", pathname, S_ISDIR(mode) ? "/" : "");
156         return 0;
157 }
158
159 int cmd_show(int argc, const char **argv, const char *prefix)
160 {
161         struct rev_info rev;
162         struct object_array_entry *objects;
163         int i, count, ret = 0;
164
165         git_config(git_log_config);
166         init_revisions(&rev, prefix);
167         rev.diff = 1;
168         rev.combine_merges = 1;
169         rev.dense_combined_merges = 1;
170         rev.always_show_header = 1;
171         rev.ignore_merges = 0;
172         rev.no_walk = 1;
173         cmd_log_init(argc, argv, prefix, &rev);
174
175         count = rev.pending.nr;
176         objects = rev.pending.objects;
177         for (i = 0; i < count && !ret; i++) {
178                 struct object *o = objects[i].item;
179                 const char *name = objects[i].name;
180                 switch (o->type) {
181                 case OBJ_BLOB:
182                         ret = show_object(o->sha1, 0);
183                         break;
184                 case OBJ_TAG: {
185                         struct tag *t = (struct tag *)o;
186
187                         printf("%stag %s%s\n\n",
188                                         diff_get_color(rev.diffopt.color_diff,
189                                                 DIFF_COMMIT),
190                                         t->tag,
191                                         diff_get_color(rev.diffopt.color_diff,
192                                                 DIFF_RESET));
193                         ret = show_object(o->sha1, 1);
194                         objects[i].item = (struct object *)t->tagged;
195                         i--;
196                         break;
197                 }
198                 case OBJ_TREE:
199                         printf("%stree %s%s\n\n",
200                                         diff_get_color(rev.diffopt.color_diff,
201                                                 DIFF_COMMIT),
202                                         name,
203                                         diff_get_color(rev.diffopt.color_diff,
204                                                 DIFF_RESET));
205                         read_tree_recursive((struct tree *)o, "", 0, 0, NULL,
206                                         show_tree_object);
207                         break;
208                 case OBJ_COMMIT:
209                         rev.pending.nr = rev.pending.alloc = 0;
210                         rev.pending.objects = NULL;
211                         add_object_array(o, name, &rev.pending);
212                         ret = cmd_log_walk(&rev);
213                         break;
214                 default:
215                         ret = error("Unknown type: %d", o->type);
216                 }
217         }
218         free(objects);
219         return ret;
220 }
221
222 /*
223  * This is equivalent to "git log -g --abbrev-commit --pretty=oneline"
224  */
225 int cmd_log_reflog(int argc, const char **argv, const char *prefix)
226 {
227         struct rev_info rev;
228
229         git_config(git_log_config);
230         init_revisions(&rev, prefix);
231         init_reflog_walk(&rev.reflog_info);
232         rev.abbrev_commit = 1;
233         rev.verbose_header = 1;
234         cmd_log_init(argc, argv, prefix, &rev);
235
236         /*
237          * This means that we override whatever commit format the user gave
238          * on the cmd line.  Sad, but cmd_log_init() currently doesn't
239          * allow us to set a different default.
240          */
241         rev.commit_format = CMIT_FMT_ONELINE;
242         rev.always_show_header = 1;
243
244         /*
245          * We get called through "git reflog", so unlike the other log
246          * routines, we need to set up our pager manually..
247          */
248         setup_pager();
249
250         return cmd_log_walk(&rev);
251 }
252
253 int cmd_log(int argc, const char **argv, const char *prefix)
254 {
255         struct rev_info rev;
256
257         git_config(git_log_config);
258         init_revisions(&rev, prefix);
259         rev.always_show_header = 1;
260         cmd_log_init(argc, argv, prefix, &rev);
261         return cmd_log_walk(&rev);
262 }
263
264 /* format-patch */
265 #define FORMAT_PATCH_NAME_MAX 64
266
267 static int istitlechar(char c)
268 {
269         return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
270                 (c >= '0' && c <= '9') || c == '.' || c == '_';
271 }
272
273 static char *extra_headers = NULL;
274 static int extra_headers_size = 0;
275 static const char *fmt_patch_suffix = ".patch";
276 static int numbered = 0;
277 static int auto_number = 0;
278
279 static int git_format_config(const char *var, const char *value)
280 {
281         if (!strcmp(var, "format.headers")) {
282                 int len;
283
284                 if (!value)
285                         die("format.headers without value");
286                 len = strlen(value);
287                 extra_headers_size += len + 1;
288                 extra_headers = xrealloc(extra_headers, extra_headers_size);
289                 extra_headers[extra_headers_size - len - 1] = 0;
290                 strcat(extra_headers, value);
291                 return 0;
292         }
293         if (!strcmp(var, "format.suffix")) {
294                 if (!value)
295                         die("format.suffix without value");
296                 fmt_patch_suffix = xstrdup(value);
297                 return 0;
298         }
299         if (!strcmp(var, "diff.color") || !strcmp(var, "color.diff")) {
300                 return 0;
301         }
302         if (!strcmp(var, "format.numbered")) {
303                 if (!strcasecmp(value, "auto")) {
304                         auto_number = 1;
305                         return 0;
306                 }
307
308                 numbered = git_config_bool(var, value);
309                 return 0;
310         }
311
312         return git_log_config(var, value);
313 }
314
315
316 static FILE *realstdout = NULL;
317 static const char *output_directory = NULL;
318
319 static int reopen_stdout(struct commit *commit, int nr, int keep_subject,
320                          int numbered_files)
321 {
322         char filename[PATH_MAX];
323         char *sol;
324         int len = 0;
325         int suffix_len = strlen(fmt_patch_suffix) + 1;
326
327         if (output_directory) {
328                 if (strlen(output_directory) >=
329                     sizeof(filename) - FORMAT_PATCH_NAME_MAX - suffix_len)
330                         return error("name of output directory is too long");
331                 strlcpy(filename, output_directory, sizeof(filename) - suffix_len);
332                 len = strlen(filename);
333                 if (filename[len - 1] != '/')
334                         filename[len++] = '/';
335         }
336
337         if (numbered_files) {
338                 sprintf(filename + len, "%d", nr);
339                 len = strlen(filename);
340
341         } else {
342                 sprintf(filename + len, "%04d", nr);
343                 len = strlen(filename);
344
345                 sol = strstr(commit->buffer, "\n\n");
346                 if (sol) {
347                         int j, space = 1;
348
349                         sol += 2;
350                         /* strip [PATCH] or [PATCH blabla] */
351                         if (!keep_subject && !prefixcmp(sol, "[PATCH")) {
352                                 char *eos = strchr(sol + 6, ']');
353                                 if (eos) {
354                                         while (isspace(*eos))
355                                                 eos++;
356                                         sol = eos;
357                                 }
358                         }
359
360                         for (j = 0;
361                              j < FORMAT_PATCH_NAME_MAX - suffix_len - 5 &&
362                                      len < sizeof(filename) - suffix_len &&
363                                      sol[j] && sol[j] != '\n';
364                              j++) {
365                                 if (istitlechar(sol[j])) {
366                                         if (space) {
367                                                 filename[len++] = '-';
368                                                 space = 0;
369                                         }
370                                         filename[len++] = sol[j];
371                                         if (sol[j] == '.')
372                                                 while (sol[j + 1] == '.')
373                                                         j++;
374                                 } else
375                                         space = 1;
376                         }
377                         while (filename[len - 1] == '.'
378                                || filename[len - 1] == '-')
379                                 len--;
380                         filename[len] = 0;
381                 }
382                 if (len + suffix_len >= sizeof(filename))
383                         return error("Patch pathname too long");
384                 strcpy(filename + len, fmt_patch_suffix);
385         }
386
387         fprintf(realstdout, "%s\n", filename);
388         if (freopen(filename, "w", stdout) == NULL)
389                 return error("Cannot open patch file %s",filename);
390
391         return 0;
392 }
393
394 static void get_patch_ids(struct rev_info *rev, struct patch_ids *ids, const char *prefix)
395 {
396         struct rev_info check_rev;
397         struct commit *commit;
398         struct object *o1, *o2;
399         unsigned flags1, flags2;
400
401         if (rev->pending.nr != 2)
402                 die("Need exactly one range.");
403
404         o1 = rev->pending.objects[0].item;
405         flags1 = o1->flags;
406         o2 = rev->pending.objects[1].item;
407         flags2 = o2->flags;
408
409         if ((flags1 & UNINTERESTING) == (flags2 & UNINTERESTING))
410                 die("Not a range.");
411
412         init_patch_ids(ids);
413
414         /* given a range a..b get all patch ids for b..a */
415         init_revisions(&check_rev, prefix);
416         o1->flags ^= UNINTERESTING;
417         o2->flags ^= UNINTERESTING;
418         add_pending_object(&check_rev, o1, "o1");
419         add_pending_object(&check_rev, o2, "o2");
420         prepare_revision_walk(&check_rev);
421
422         while ((commit = get_revision(&check_rev)) != NULL) {
423                 /* ignore merges */
424                 if (commit->parents && commit->parents->next)
425                         continue;
426
427                 add_commit_patch_id(commit, ids);
428         }
429
430         /* reset for next revision walk */
431         clear_commit_marks((struct commit *)o1,
432                         SEEN | UNINTERESTING | SHOWN | ADDED);
433         clear_commit_marks((struct commit *)o2,
434                         SEEN | UNINTERESTING | SHOWN | ADDED);
435         o1->flags = flags1;
436         o2->flags = flags2;
437 }
438
439 static void gen_message_id(char *dest, unsigned int length, char *base)
440 {
441         const char *committer = git_committer_info(-1);
442         const char *email_start = strrchr(committer, '<');
443         const char *email_end = strrchr(committer, '>');
444         if(!email_start || !email_end || email_start > email_end - 1)
445                 die("Could not extract email from committer identity.");
446         snprintf(dest, length, "%s.%lu.git.%.*s", base,
447                  (unsigned long) time(NULL),
448                  (int)(email_end - email_start - 1), email_start + 1);
449 }
450
451 static const char *clean_message_id(const char *msg_id)
452 {
453         char ch;
454         const char *a, *z, *m;
455
456         m = msg_id;
457         while ((ch = *m) && (isspace(ch) || (ch == '<')))
458                 m++;
459         a = m;
460         z = NULL;
461         while ((ch = *m)) {
462                 if (!isspace(ch) && (ch != '>'))
463                         z = m;
464                 m++;
465         }
466         if (!z)
467                 die("insane in-reply-to: %s", msg_id);
468         if (++z == m)
469                 return a;
470         return xmemdupz(a, z - a);
471 }
472
473 int cmd_format_patch(int argc, const char **argv, const char *prefix)
474 {
475         struct commit *commit;
476         struct commit **list = NULL;
477         struct rev_info rev;
478         int nr = 0, total, i, j;
479         int use_stdout = 0;
480         int start_number = -1;
481         int keep_subject = 0;
482         int numbered_files = 0;         /* _just_ numbers */
483         int subject_prefix = 0;
484         int ignore_if_in_upstream = 0;
485         int thread = 0;
486         const char *in_reply_to = NULL;
487         struct patch_ids ids;
488         char *add_signoff = NULL;
489         char message_id[1024];
490         char ref_message_id[1024];
491
492         git_config(git_format_config);
493         init_revisions(&rev, prefix);
494         rev.commit_format = CMIT_FMT_EMAIL;
495         rev.verbose_header = 1;
496         rev.diff = 1;
497         rev.combine_merges = 0;
498         rev.ignore_merges = 1;
499         rev.diffopt.msg_sep = "";
500         rev.diffopt.recursive = 1;
501
502         rev.subject_prefix = fmt_patch_subject_prefix;
503         rev.extra_headers = extra_headers;
504
505         /*
506          * Parse the arguments before setup_revisions(), or something
507          * like "git format-patch -o a123 HEAD^.." may fail; a123 is
508          * possibly a valid SHA1.
509          */
510         for (i = 1, j = 1; i < argc; i++) {
511                 if (!strcmp(argv[i], "--stdout"))
512                         use_stdout = 1;
513                 else if (!strcmp(argv[i], "-n") ||
514                                 !strcmp(argv[i], "--numbered"))
515                         numbered = 1;
516                 else if (!strcmp(argv[i], "-N") ||
517                                 !strcmp(argv[i], "--no-numbered")) {
518                         numbered = 0;
519                         auto_number = 0;
520                 }
521                 else if (!prefixcmp(argv[i], "--start-number="))
522                         start_number = strtol(argv[i] + 15, NULL, 10);
523                 else if (!strcmp(argv[i], "--numbered-files"))
524                         numbered_files = 1;
525                 else if (!strcmp(argv[i], "--start-number")) {
526                         i++;
527                         if (i == argc)
528                                 die("Need a number for --start-number");
529                         start_number = strtol(argv[i], NULL, 10);
530                 }
531                 else if (!strcmp(argv[i], "-k") ||
532                                 !strcmp(argv[i], "--keep-subject")) {
533                         keep_subject = 1;
534                         rev.total = -1;
535                 }
536                 else if (!strcmp(argv[i], "--output-directory") ||
537                          !strcmp(argv[i], "-o")) {
538                         i++;
539                         if (argc <= i)
540                                 die("Which directory?");
541                         if (output_directory)
542                                 die("Two output directories?");
543                         output_directory = argv[i];
544                 }
545                 else if (!strcmp(argv[i], "--signoff") ||
546                          !strcmp(argv[i], "-s")) {
547                         const char *committer;
548                         const char *endpos;
549                         committer = git_committer_info(1);
550                         endpos = strchr(committer, '>');
551                         if (!endpos)
552                                 die("bogos committer info %s\n", committer);
553                         add_signoff = xmemdupz(committer, endpos - committer + 1);
554                 }
555                 else if (!strcmp(argv[i], "--attach")) {
556                         rev.mime_boundary = git_version_string;
557                         rev.no_inline = 1;
558                 }
559                 else if (!prefixcmp(argv[i], "--attach=")) {
560                         rev.mime_boundary = argv[i] + 9;
561                         rev.no_inline = 1;
562                 }
563                 else if (!strcmp(argv[i], "--inline")) {
564                         rev.mime_boundary = git_version_string;
565                         rev.no_inline = 0;
566                 }
567                 else if (!prefixcmp(argv[i], "--inline=")) {
568                         rev.mime_boundary = argv[i] + 9;
569                         rev.no_inline = 0;
570                 }
571                 else if (!strcmp(argv[i], "--ignore-if-in-upstream"))
572                         ignore_if_in_upstream = 1;
573                 else if (!strcmp(argv[i], "--thread"))
574                         thread = 1;
575                 else if (!prefixcmp(argv[i], "--in-reply-to="))
576                         in_reply_to = argv[i] + 14;
577                 else if (!strcmp(argv[i], "--in-reply-to")) {
578                         i++;
579                         if (i == argc)
580                                 die("Need a Message-Id for --in-reply-to");
581                         in_reply_to = argv[i];
582                 } else if (!prefixcmp(argv[i], "--subject-prefix=")) {
583                         subject_prefix = 1;
584                         rev.subject_prefix = argv[i] + 17;
585                 } else if (!prefixcmp(argv[i], "--suffix="))
586                         fmt_patch_suffix = argv[i] + 9;
587                 else
588                         argv[j++] = argv[i];
589         }
590         argc = j;
591
592         if (start_number < 0)
593                 start_number = 1;
594         if (numbered && keep_subject)
595                 die ("-n and -k are mutually exclusive.");
596         if (keep_subject && subject_prefix)
597                 die ("--subject-prefix and -k are mutually exclusive.");
598         if (numbered_files && use_stdout)
599                 die ("--numbered-files and --stdout are mutually exclusive.");
600
601         argc = setup_revisions(argc, argv, &rev, "HEAD");
602         if (argc > 1)
603                 die ("unrecognized argument: %s", argv[1]);
604
605         if (!rev.diffopt.output_format)
606                 rev.diffopt.output_format = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_SUMMARY | DIFF_FORMAT_PATCH;
607
608         if (!rev.diffopt.text)
609                 rev.diffopt.binary = 1;
610
611         if (!output_directory && !use_stdout)
612                 output_directory = prefix;
613
614         if (output_directory) {
615                 if (use_stdout)
616                         die("standard output, or directory, which one?");
617                 if (mkdir(output_directory, 0777) < 0 && errno != EEXIST)
618                         die("Could not create directory %s",
619                             output_directory);
620         }
621
622         if (rev.pending.nr == 1) {
623                 if (rev.max_count < 0 && !rev.show_root_diff) {
624                         /*
625                          * This is traditional behaviour of "git format-patch
626                          * origin" that prepares what the origin side still
627                          * does not have.
628                          */
629                         rev.pending.objects[0].item->flags |= UNINTERESTING;
630                         add_head(&rev);
631                 }
632                 /*
633                  * Otherwise, it is "format-patch -22 HEAD", and/or
634                  * "format-patch --root HEAD".  The user wants
635                  * get_revision() to do the usual traversal.
636                  */
637         }
638
639         if (ignore_if_in_upstream)
640                 get_patch_ids(&rev, &ids, prefix);
641
642         if (!use_stdout)
643                 realstdout = xfdopen(xdup(1), "w");
644
645         prepare_revision_walk(&rev);
646         while ((commit = get_revision(&rev)) != NULL) {
647                 /* ignore merges */
648                 if (commit->parents && commit->parents->next)
649                         continue;
650
651                 if (ignore_if_in_upstream &&
652                                 has_commit_patch_id(commit, &ids))
653                         continue;
654
655                 nr++;
656                 list = xrealloc(list, nr * sizeof(list[0]));
657                 list[nr - 1] = commit;
658         }
659         total = nr;
660         if (!keep_subject && auto_number && total > 1)
661                 numbered = 1;
662         if (numbered)
663                 rev.total = total + start_number - 1;
664         rev.add_signoff = add_signoff;
665         if (in_reply_to)
666                 rev.ref_message_id = clean_message_id(in_reply_to);
667         while (0 <= --nr) {
668                 int shown;
669                 commit = list[nr];
670                 rev.nr = total - nr + (start_number - 1);
671                 /* Make the second and subsequent mails replies to the first */
672                 if (thread) {
673                         if (nr == (total - 2)) {
674                                 strncpy(ref_message_id, message_id,
675                                         sizeof(ref_message_id));
676                                 ref_message_id[sizeof(ref_message_id)-1]='\0';
677                                 rev.ref_message_id = ref_message_id;
678                         }
679                         gen_message_id(message_id, sizeof(message_id),
680                                        sha1_to_hex(commit->object.sha1));
681                         rev.message_id = message_id;
682                 }
683                 if (!use_stdout)
684                         if (reopen_stdout(commit, rev.nr, keep_subject,
685                                           numbered_files))
686                                 die("Failed to create output files");
687                 shown = log_tree_commit(&rev, commit);
688                 free(commit->buffer);
689                 commit->buffer = NULL;
690
691                 /* We put one extra blank line between formatted
692                  * patches and this flag is used by log-tree code
693                  * to see if it needs to emit a LF before showing
694                  * the log; when using one file per patch, we do
695                  * not want the extra blank line.
696                  */
697                 if (!use_stdout)
698                         rev.shown_one = 0;
699                 if (shown) {
700                         if (rev.mime_boundary)
701                                 printf("\n--%s%s--\n\n\n",
702                                        mime_boundary_leader,
703                                        rev.mime_boundary);
704                         else
705                                 printf("-- \n%s\n\n", git_version_string);
706                 }
707                 if (!use_stdout)
708                         fclose(stdout);
709         }
710         free(list);
711         if (ignore_if_in_upstream)
712                 free_patch_ids(&ids);
713         return 0;
714 }
715
716 static int add_pending_commit(const char *arg, struct rev_info *revs, int flags)
717 {
718         unsigned char sha1[20];
719         if (get_sha1(arg, sha1) == 0) {
720                 struct commit *commit = lookup_commit_reference(sha1);
721                 if (commit) {
722                         commit->object.flags |= flags;
723                         add_pending_object(revs, &commit->object, arg);
724                         return 0;
725                 }
726         }
727         return -1;
728 }
729
730 static const char cherry_usage[] =
731 "git-cherry [-v] <upstream> [<head>] [<limit>]";
732 int cmd_cherry(int argc, const char **argv, const char *prefix)
733 {
734         struct rev_info revs;
735         struct patch_ids ids;
736         struct commit *commit;
737         struct commit_list *list = NULL;
738         const char *upstream;
739         const char *head = "HEAD";
740         const char *limit = NULL;
741         int verbose = 0;
742
743         if (argc > 1 && !strcmp(argv[1], "-v")) {
744                 verbose = 1;
745                 argc--;
746                 argv++;
747         }
748
749         switch (argc) {
750         case 4:
751                 limit = argv[3];
752                 /* FALLTHROUGH */
753         case 3:
754                 head = argv[2];
755                 /* FALLTHROUGH */
756         case 2:
757                 upstream = argv[1];
758                 break;
759         default:
760                 usage(cherry_usage);
761         }
762
763         init_revisions(&revs, prefix);
764         revs.diff = 1;
765         revs.combine_merges = 0;
766         revs.ignore_merges = 1;
767         revs.diffopt.recursive = 1;
768
769         if (add_pending_commit(head, &revs, 0))
770                 die("Unknown commit %s", head);
771         if (add_pending_commit(upstream, &revs, UNINTERESTING))
772                 die("Unknown commit %s", upstream);
773
774         /* Don't say anything if head and upstream are the same. */
775         if (revs.pending.nr == 2) {
776                 struct object_array_entry *o = revs.pending.objects;
777                 if (hashcmp(o[0].item->sha1, o[1].item->sha1) == 0)
778                         return 0;
779         }
780
781         get_patch_ids(&revs, &ids, prefix);
782
783         if (limit && add_pending_commit(limit, &revs, UNINTERESTING))
784                 die("Unknown commit %s", limit);
785
786         /* reverse the list of commits */
787         prepare_revision_walk(&revs);
788         while ((commit = get_revision(&revs)) != NULL) {
789                 /* ignore merges */
790                 if (commit->parents && commit->parents->next)
791                         continue;
792
793                 commit_list_insert(commit, &list);
794         }
795
796         while (list) {
797                 char sign = '+';
798
799                 commit = list->item;
800                 if (has_commit_patch_id(commit, &ids))
801                         sign = '-';
802
803                 if (verbose) {
804                         struct strbuf buf;
805                         strbuf_init(&buf, 0);
806                         pretty_print_commit(CMIT_FMT_ONELINE, commit,
807                                             &buf, 0, NULL, NULL, 0, 0);
808                         printf("%c %s %s\n", sign,
809                                sha1_to_hex(commit->object.sha1), buf.buf);
810                         strbuf_release(&buf);
811                 }
812                 else {
813                         printf("%c %s\n", sign,
814                                sha1_to_hex(commit->object.sha1));
815                 }
816
817                 list = list->next;
818         }
819
820         free_patch_ids(&ids);
821         return 0;
822 }