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