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