bundle: detect if bundle file cannot be created
[git] / revision.c
1 #include "cache.h"
2 #include "tag.h"
3 #include "blob.h"
4 #include "tree.h"
5 #include "commit.h"
6 #include "diff.h"
7 #include "refs.h"
8 #include "revision.h"
9 #include "graph.h"
10 #include "grep.h"
11 #include "reflog-walk.h"
12 #include "patch-ids.h"
13 #include "decorate.h"
14 #include "log-tree.h"
15 #include "string-list.h"
16
17 volatile show_early_output_fn_t show_early_output;
18
19 char *path_name(const struct name_path *path, const char *name)
20 {
21         const struct name_path *p;
22         char *n, *m;
23         int nlen = strlen(name);
24         int len = nlen + 1;
25
26         for (p = path; p; p = p->up) {
27                 if (p->elem_len)
28                         len += p->elem_len + 1;
29         }
30         n = xmalloc(len);
31         m = n + len - (nlen + 1);
32         strcpy(m, name);
33         for (p = path; p; p = p->up) {
34                 if (p->elem_len) {
35                         m -= p->elem_len + 1;
36                         memcpy(m, p->elem, p->elem_len);
37                         m[p->elem_len] = '/';
38                 }
39         }
40         return n;
41 }
42
43 void add_object(struct object *obj,
44                 struct object_array *p,
45                 struct name_path *path,
46                 const char *name)
47 {
48         add_object_array(obj, path_name(path, name), p);
49 }
50
51 static void mark_blob_uninteresting(struct blob *blob)
52 {
53         if (!blob)
54                 return;
55         if (blob->object.flags & UNINTERESTING)
56                 return;
57         blob->object.flags |= UNINTERESTING;
58 }
59
60 void mark_tree_uninteresting(struct tree *tree)
61 {
62         struct tree_desc desc;
63         struct name_entry entry;
64         struct object *obj = &tree->object;
65
66         if (!tree)
67                 return;
68         if (obj->flags & UNINTERESTING)
69                 return;
70         obj->flags |= UNINTERESTING;
71         if (!has_sha1_file(obj->sha1))
72                 return;
73         if (parse_tree(tree) < 0)
74                 die("bad tree %s", sha1_to_hex(obj->sha1));
75
76         init_tree_desc(&desc, tree->buffer, tree->size);
77         while (tree_entry(&desc, &entry)) {
78                 switch (object_type(entry.mode)) {
79                 case OBJ_TREE:
80                         mark_tree_uninteresting(lookup_tree(entry.sha1));
81                         break;
82                 case OBJ_BLOB:
83                         mark_blob_uninteresting(lookup_blob(entry.sha1));
84                         break;
85                 default:
86                         /* Subproject commit - not in this repository */
87                         break;
88                 }
89         }
90
91         /*
92          * We don't care about the tree any more
93          * after it has been marked uninteresting.
94          */
95         free(tree->buffer);
96         tree->buffer = NULL;
97 }
98
99 void mark_parents_uninteresting(struct commit *commit)
100 {
101         struct commit_list *parents = commit->parents;
102
103         while (parents) {
104                 struct commit *commit = parents->item;
105                 if (!(commit->object.flags & UNINTERESTING)) {
106                         commit->object.flags |= UNINTERESTING;
107
108                         /*
109                          * Normally we haven't parsed the parent
110                          * yet, so we won't have a parent of a parent
111                          * here. However, it may turn out that we've
112                          * reached this commit some other way (where it
113                          * wasn't uninteresting), in which case we need
114                          * to mark its parents recursively too..
115                          */
116                         if (commit->parents)
117                                 mark_parents_uninteresting(commit);
118                 }
119
120                 /*
121                  * A missing commit is ok iff its parent is marked
122                  * uninteresting.
123                  *
124                  * We just mark such a thing parsed, so that when
125                  * it is popped next time around, we won't be trying
126                  * to parse it and get an error.
127                  */
128                 if (!has_sha1_file(commit->object.sha1))
129                         commit->object.parsed = 1;
130                 parents = parents->next;
131         }
132 }
133
134 static void add_pending_object_with_mode(struct rev_info *revs, struct object *obj, const char *name, unsigned mode)
135 {
136         if (revs->no_walk && (obj->flags & UNINTERESTING))
137                 revs->no_walk = 0;
138         if (revs->reflog_info && obj->type == OBJ_COMMIT) {
139                 struct strbuf buf = STRBUF_INIT;
140                 int len = interpret_branch_name(name, &buf);
141                 int st;
142
143                 if (0 < len && name[len] && buf.len)
144                         strbuf_addstr(&buf, name + len);
145                 st = add_reflog_for_walk(revs->reflog_info,
146                                          (struct commit *)obj,
147                                          buf.buf[0] ? buf.buf: name);
148                 strbuf_release(&buf);
149                 if (st)
150                         return;
151         }
152         add_object_array_with_mode(obj, name, &revs->pending, mode);
153 }
154
155 void add_pending_object(struct rev_info *revs, struct object *obj, const char *name)
156 {
157         add_pending_object_with_mode(revs, obj, name, S_IFINVALID);
158 }
159
160 void add_head_to_pending(struct rev_info *revs)
161 {
162         unsigned char sha1[20];
163         struct object *obj;
164         if (get_sha1("HEAD", sha1))
165                 return;
166         obj = parse_object(sha1);
167         if (!obj)
168                 return;
169         add_pending_object(revs, obj, "HEAD");
170 }
171
172 static struct object *get_reference(struct rev_info *revs, const char *name, const unsigned char *sha1, unsigned int flags)
173 {
174         struct object *object;
175
176         object = parse_object(sha1);
177         if (!object)
178                 die("bad object %s", name);
179         object->flags |= flags;
180         return object;
181 }
182
183 static struct commit *handle_commit(struct rev_info *revs, struct object *object, const char *name)
184 {
185         unsigned long flags = object->flags;
186
187         /*
188          * Tag object? Look what it points to..
189          */
190         while (object->type == OBJ_TAG) {
191                 struct tag *tag = (struct tag *) object;
192                 if (revs->tag_objects && !(flags & UNINTERESTING))
193                         add_pending_object(revs, object, tag->tag);
194                 if (!tag->tagged)
195                         die("bad tag");
196                 object = parse_object(tag->tagged->sha1);
197                 if (!object) {
198                         if (flags & UNINTERESTING)
199                                 return NULL;
200                         die("bad object %s", sha1_to_hex(tag->tagged->sha1));
201                 }
202         }
203
204         /*
205          * Commit object? Just return it, we'll do all the complex
206          * reachability crud.
207          */
208         if (object->type == OBJ_COMMIT) {
209                 struct commit *commit = (struct commit *)object;
210                 if (parse_commit(commit) < 0)
211                         die("unable to parse commit %s", name);
212                 if (flags & UNINTERESTING) {
213                         commit->object.flags |= UNINTERESTING;
214                         mark_parents_uninteresting(commit);
215                         revs->limited = 1;
216                 }
217                 if (revs->show_source && !commit->util)
218                         commit->util = (void *) name;
219                 return commit;
220         }
221
222         /*
223          * Tree object? Either mark it uninteresting, or add it
224          * to the list of objects to look at later..
225          */
226         if (object->type == OBJ_TREE) {
227                 struct tree *tree = (struct tree *)object;
228                 if (!revs->tree_objects)
229                         return NULL;
230                 if (flags & UNINTERESTING) {
231                         mark_tree_uninteresting(tree);
232                         return NULL;
233                 }
234                 add_pending_object(revs, object, "");
235                 return NULL;
236         }
237
238         /*
239          * Blob object? You know the drill by now..
240          */
241         if (object->type == OBJ_BLOB) {
242                 struct blob *blob = (struct blob *)object;
243                 if (!revs->blob_objects)
244                         return NULL;
245                 if (flags & UNINTERESTING) {
246                         mark_blob_uninteresting(blob);
247                         return NULL;
248                 }
249                 add_pending_object(revs, object, "");
250                 return NULL;
251         }
252         die("%s is unknown object", name);
253 }
254
255 static int everybody_uninteresting(struct commit_list *orig)
256 {
257         struct commit_list *list = orig;
258         while (list) {
259                 struct commit *commit = list->item;
260                 list = list->next;
261                 if (commit->object.flags & UNINTERESTING)
262                         continue;
263                 return 0;
264         }
265         return 1;
266 }
267
268 /*
269  * The goal is to get REV_TREE_NEW as the result only if the
270  * diff consists of all '+' (and no other changes), REV_TREE_OLD
271  * if the whole diff is removal of old data, and otherwise
272  * REV_TREE_DIFFERENT (of course if the trees are the same we
273  * want REV_TREE_SAME).
274  * That means that once we get to REV_TREE_DIFFERENT, we do not
275  * have to look any further.
276  */
277 static int tree_difference = REV_TREE_SAME;
278
279 static void file_add_remove(struct diff_options *options,
280                     int addremove, unsigned mode,
281                     const unsigned char *sha1,
282                     const char *fullpath, unsigned dirty_submodule)
283 {
284         int diff = addremove == '+' ? REV_TREE_NEW : REV_TREE_OLD;
285
286         tree_difference |= diff;
287         if (tree_difference == REV_TREE_DIFFERENT)
288                 DIFF_OPT_SET(options, HAS_CHANGES);
289 }
290
291 static void file_change(struct diff_options *options,
292                  unsigned old_mode, unsigned new_mode,
293                  const unsigned char *old_sha1,
294                  const unsigned char *new_sha1,
295                  const char *fullpath,
296                  unsigned old_dirty_submodule, unsigned new_dirty_submodule)
297 {
298         tree_difference = REV_TREE_DIFFERENT;
299         DIFF_OPT_SET(options, HAS_CHANGES);
300 }
301
302 static int rev_compare_tree(struct rev_info *revs, struct commit *parent, struct commit *commit)
303 {
304         struct tree *t1 = parent->tree;
305         struct tree *t2 = commit->tree;
306
307         if (!t1)
308                 return REV_TREE_NEW;
309         if (!t2)
310                 return REV_TREE_OLD;
311
312         if (revs->simplify_by_decoration) {
313                 /*
314                  * If we are simplifying by decoration, then the commit
315                  * is worth showing if it has a tag pointing at it.
316                  */
317                 if (lookup_decoration(&name_decoration, &commit->object))
318                         return REV_TREE_DIFFERENT;
319                 /*
320                  * A commit that is not pointed by a tag is uninteresting
321                  * if we are not limited by path.  This means that you will
322                  * see the usual "commits that touch the paths" plus any
323                  * tagged commit by specifying both --simplify-by-decoration
324                  * and pathspec.
325                  */
326                 if (!revs->prune_data)
327                         return REV_TREE_SAME;
328         }
329
330         tree_difference = REV_TREE_SAME;
331         DIFF_OPT_CLR(&revs->pruning, HAS_CHANGES);
332         if (diff_tree_sha1(t1->object.sha1, t2->object.sha1, "",
333                            &revs->pruning) < 0)
334                 return REV_TREE_DIFFERENT;
335         return tree_difference;
336 }
337
338 static int rev_same_tree_as_empty(struct rev_info *revs, struct commit *commit)
339 {
340         int retval;
341         void *tree;
342         unsigned long size;
343         struct tree_desc empty, real;
344         struct tree *t1 = commit->tree;
345
346         if (!t1)
347                 return 0;
348
349         tree = read_object_with_reference(t1->object.sha1, tree_type, &size, NULL);
350         if (!tree)
351                 return 0;
352         init_tree_desc(&real, tree, size);
353         init_tree_desc(&empty, "", 0);
354
355         tree_difference = REV_TREE_SAME;
356         DIFF_OPT_CLR(&revs->pruning, HAS_CHANGES);
357         retval = diff_tree(&empty, &real, "", &revs->pruning);
358         free(tree);
359
360         return retval >= 0 && (tree_difference == REV_TREE_SAME);
361 }
362
363 static void try_to_simplify_commit(struct rev_info *revs, struct commit *commit)
364 {
365         struct commit_list **pp, *parent;
366         int tree_changed = 0, tree_same = 0;
367
368         /*
369          * If we don't do pruning, everything is interesting
370          */
371         if (!revs->prune)
372                 return;
373
374         if (!commit->tree)
375                 return;
376
377         if (!commit->parents) {
378                 if (rev_same_tree_as_empty(revs, commit))
379                         commit->object.flags |= TREESAME;
380                 return;
381         }
382
383         /*
384          * Normal non-merge commit? If we don't want to make the
385          * history dense, we consider it always to be a change..
386          */
387         if (!revs->dense && !commit->parents->next)
388                 return;
389
390         pp = &commit->parents;
391         while ((parent = *pp) != NULL) {
392                 struct commit *p = parent->item;
393
394                 if (parse_commit(p) < 0)
395                         die("cannot simplify commit %s (because of %s)",
396                             sha1_to_hex(commit->object.sha1),
397                             sha1_to_hex(p->object.sha1));
398                 switch (rev_compare_tree(revs, p, commit)) {
399                 case REV_TREE_SAME:
400                         tree_same = 1;
401                         if (!revs->simplify_history || (p->object.flags & UNINTERESTING)) {
402                                 /* Even if a merge with an uninteresting
403                                  * side branch brought the entire change
404                                  * we are interested in, we do not want
405                                  * to lose the other branches of this
406                                  * merge, so we just keep going.
407                                  */
408                                 pp = &parent->next;
409                                 continue;
410                         }
411                         parent->next = NULL;
412                         commit->parents = parent;
413                         commit->object.flags |= TREESAME;
414                         return;
415
416                 case REV_TREE_NEW:
417                         if (revs->remove_empty_trees &&
418                             rev_same_tree_as_empty(revs, p)) {
419                                 /* We are adding all the specified
420                                  * paths from this parent, so the
421                                  * history beyond this parent is not
422                                  * interesting.  Remove its parents
423                                  * (they are grandparents for us).
424                                  * IOW, we pretend this parent is a
425                                  * "root" commit.
426                                  */
427                                 if (parse_commit(p) < 0)
428                                         die("cannot simplify commit %s (invalid %s)",
429                                             sha1_to_hex(commit->object.sha1),
430                                             sha1_to_hex(p->object.sha1));
431                                 p->parents = NULL;
432                         }
433                 /* fallthrough */
434                 case REV_TREE_OLD:
435                 case REV_TREE_DIFFERENT:
436                         tree_changed = 1;
437                         pp = &parent->next;
438                         continue;
439                 }
440                 die("bad tree compare for commit %s", sha1_to_hex(commit->object.sha1));
441         }
442         if (tree_changed && !tree_same)
443                 return;
444         commit->object.flags |= TREESAME;
445 }
446
447 static void insert_by_date_cached(struct commit *p, struct commit_list **head,
448                     struct commit_list *cached_base, struct commit_list **cache)
449 {
450         struct commit_list *new_entry;
451
452         if (cached_base && p->date < cached_base->item->date)
453                 new_entry = insert_by_date(p, &cached_base->next);
454         else
455                 new_entry = insert_by_date(p, head);
456
457         if (cache && (!*cache || p->date < (*cache)->item->date))
458                 *cache = new_entry;
459 }
460
461 static int add_parents_to_list(struct rev_info *revs, struct commit *commit,
462                     struct commit_list **list, struct commit_list **cache_ptr)
463 {
464         struct commit_list *parent = commit->parents;
465         unsigned left_flag;
466         struct commit_list *cached_base = cache_ptr ? *cache_ptr : NULL;
467
468         if (commit->object.flags & ADDED)
469                 return 0;
470         commit->object.flags |= ADDED;
471
472         /*
473          * If the commit is uninteresting, don't try to
474          * prune parents - we want the maximal uninteresting
475          * set.
476          *
477          * Normally we haven't parsed the parent
478          * yet, so we won't have a parent of a parent
479          * here. However, it may turn out that we've
480          * reached this commit some other way (where it
481          * wasn't uninteresting), in which case we need
482          * to mark its parents recursively too..
483          */
484         if (commit->object.flags & UNINTERESTING) {
485                 while (parent) {
486                         struct commit *p = parent->item;
487                         parent = parent->next;
488                         if (p)
489                                 p->object.flags |= UNINTERESTING;
490                         if (parse_commit(p) < 0)
491                                 continue;
492                         if (p->parents)
493                                 mark_parents_uninteresting(p);
494                         if (p->object.flags & SEEN)
495                                 continue;
496                         p->object.flags |= SEEN;
497                         insert_by_date_cached(p, list, cached_base, cache_ptr);
498                 }
499                 return 0;
500         }
501
502         /*
503          * Ok, the commit wasn't uninteresting. Try to
504          * simplify the commit history and find the parent
505          * that has no differences in the path set if one exists.
506          */
507         try_to_simplify_commit(revs, commit);
508
509         if (revs->no_walk)
510                 return 0;
511
512         left_flag = (commit->object.flags & SYMMETRIC_LEFT);
513
514         for (parent = commit->parents; parent; parent = parent->next) {
515                 struct commit *p = parent->item;
516
517                 if (parse_commit(p) < 0)
518                         return -1;
519                 if (revs->show_source && !p->util)
520                         p->util = commit->util;
521                 p->object.flags |= left_flag;
522                 if (!(p->object.flags & SEEN)) {
523                         p->object.flags |= SEEN;
524                         insert_by_date_cached(p, list, cached_base, cache_ptr);
525                 }
526                 if (revs->first_parent_only)
527                         break;
528         }
529         return 0;
530 }
531
532 static void cherry_pick_list(struct commit_list *list, struct rev_info *revs)
533 {
534         struct commit_list *p;
535         int left_count = 0, right_count = 0;
536         int left_first;
537         struct patch_ids ids;
538
539         /* First count the commits on the left and on the right */
540         for (p = list; p; p = p->next) {
541                 struct commit *commit = p->item;
542                 unsigned flags = commit->object.flags;
543                 if (flags & BOUNDARY)
544                         ;
545                 else if (flags & SYMMETRIC_LEFT)
546                         left_count++;
547                 else
548                         right_count++;
549         }
550
551         if (!left_count || !right_count)
552                 return;
553
554         left_first = left_count < right_count;
555         init_patch_ids(&ids);
556         if (revs->diffopt.nr_paths) {
557                 ids.diffopts.nr_paths = revs->diffopt.nr_paths;
558                 ids.diffopts.paths = revs->diffopt.paths;
559                 ids.diffopts.pathlens = revs->diffopt.pathlens;
560         }
561
562         /* Compute patch-ids for one side */
563         for (p = list; p; p = p->next) {
564                 struct commit *commit = p->item;
565                 unsigned flags = commit->object.flags;
566
567                 if (flags & BOUNDARY)
568                         continue;
569                 /*
570                  * If we have fewer left, left_first is set and we omit
571                  * commits on the right branch in this loop.  If we have
572                  * fewer right, we skip the left ones.
573                  */
574                 if (left_first != !!(flags & SYMMETRIC_LEFT))
575                         continue;
576                 commit->util = add_commit_patch_id(commit, &ids);
577         }
578
579         /* Check the other side */
580         for (p = list; p; p = p->next) {
581                 struct commit *commit = p->item;
582                 struct patch_id *id;
583                 unsigned flags = commit->object.flags;
584
585                 if (flags & BOUNDARY)
586                         continue;
587                 /*
588                  * If we have fewer left, left_first is set and we omit
589                  * commits on the left branch in this loop.
590                  */
591                 if (left_first == !!(flags & SYMMETRIC_LEFT))
592                         continue;
593
594                 /*
595                  * Have we seen the same patch id?
596                  */
597                 id = has_commit_patch_id(commit, &ids);
598                 if (!id)
599                         continue;
600                 id->seen = 1;
601                 commit->object.flags |= SHOWN;
602         }
603
604         /* Now check the original side for seen ones */
605         for (p = list; p; p = p->next) {
606                 struct commit *commit = p->item;
607                 struct patch_id *ent;
608
609                 ent = commit->util;
610                 if (!ent)
611                         continue;
612                 if (ent->seen)
613                         commit->object.flags |= SHOWN;
614                 commit->util = NULL;
615         }
616
617         free_patch_ids(&ids);
618 }
619
620 /* How many extra uninteresting commits we want to see.. */
621 #define SLOP 5
622
623 static int still_interesting(struct commit_list *src, unsigned long date, int slop)
624 {
625         /*
626          * No source list at all? We're definitely done..
627          */
628         if (!src)
629                 return 0;
630
631         /*
632          * Does the destination list contain entries with a date
633          * before the source list? Definitely _not_ done.
634          */
635         if (date < src->item->date)
636                 return SLOP;
637
638         /*
639          * Does the source list still have interesting commits in
640          * it? Definitely not done..
641          */
642         if (!everybody_uninteresting(src))
643                 return SLOP;
644
645         /* Ok, we're closing in.. */
646         return slop-1;
647 }
648
649 /*
650  * "rev-list --ancestry-path A..B" computes commits that are ancestors
651  * of B but not ancestors of A but further limits the result to those
652  * that are descendants of A.  This takes the list of bottom commits and
653  * the result of "A..B" without --ancestry-path, and limits the latter
654  * further to the ones that can reach one of the commits in "bottom".
655  */
656 static void limit_to_ancestry(struct commit_list *bottom, struct commit_list *list)
657 {
658         struct commit_list *p;
659         struct commit_list *rlist = NULL;
660         int made_progress;
661
662         /*
663          * Reverse the list so that it will be likely that we would
664          * process parents before children.
665          */
666         for (p = list; p; p = p->next)
667                 commit_list_insert(p->item, &rlist);
668
669         for (p = bottom; p; p = p->next)
670                 p->item->object.flags |= TMP_MARK;
671
672         /*
673          * Mark the ones that can reach bottom commits in "list",
674          * in a bottom-up fashion.
675          */
676         do {
677                 made_progress = 0;
678                 for (p = rlist; p; p = p->next) {
679                         struct commit *c = p->item;
680                         struct commit_list *parents;
681                         if (c->object.flags & (TMP_MARK | UNINTERESTING))
682                                 continue;
683                         for (parents = c->parents;
684                              parents;
685                              parents = parents->next) {
686                                 if (!(parents->item->object.flags & TMP_MARK))
687                                         continue;
688                                 c->object.flags |= TMP_MARK;
689                                 made_progress = 1;
690                                 break;
691                         }
692                 }
693         } while (made_progress);
694
695         /*
696          * NEEDSWORK: decide if we want to remove parents that are
697          * not marked with TMP_MARK from commit->parents for commits
698          * in the resulting list.  We may not want to do that, though.
699          */
700
701         /*
702          * The ones that are not marked with TMP_MARK are uninteresting
703          */
704         for (p = list; p; p = p->next) {
705                 struct commit *c = p->item;
706                 if (c->object.flags & TMP_MARK)
707                         continue;
708                 c->object.flags |= UNINTERESTING;
709         }
710
711         /* We are done with the TMP_MARK */
712         for (p = list; p; p = p->next)
713                 p->item->object.flags &= ~TMP_MARK;
714         for (p = bottom; p; p = p->next)
715                 p->item->object.flags &= ~TMP_MARK;
716         free_commit_list(rlist);
717 }
718
719 /*
720  * Before walking the history, keep the set of "negative" refs the
721  * caller has asked to exclude.
722  *
723  * This is used to compute "rev-list --ancestry-path A..B", as we need
724  * to filter the result of "A..B" further to the ones that can actually
725  * reach A.
726  */
727 static struct commit_list *collect_bottom_commits(struct commit_list *list)
728 {
729         struct commit_list *elem, *bottom = NULL;
730         for (elem = list; elem; elem = elem->next)
731                 if (elem->item->object.flags & UNINTERESTING)
732                         commit_list_insert(elem->item, &bottom);
733         return bottom;
734 }
735
736 static int limit_list(struct rev_info *revs)
737 {
738         int slop = SLOP;
739         unsigned long date = ~0ul;
740         struct commit_list *list = revs->commits;
741         struct commit_list *newlist = NULL;
742         struct commit_list **p = &newlist;
743         struct commit_list *bottom = NULL;
744
745         if (revs->ancestry_path) {
746                 bottom = collect_bottom_commits(list);
747                 if (!bottom)
748                         die("--ancestry-path given but there are no bottom commits");
749         }
750
751         while (list) {
752                 struct commit_list *entry = list;
753                 struct commit *commit = list->item;
754                 struct object *obj = &commit->object;
755                 show_early_output_fn_t show;
756
757                 list = list->next;
758                 free(entry);
759
760                 if (revs->max_age != -1 && (commit->date < revs->max_age))
761                         obj->flags |= UNINTERESTING;
762                 if (add_parents_to_list(revs, commit, &list, NULL) < 0)
763                         return -1;
764                 if (obj->flags & UNINTERESTING) {
765                         mark_parents_uninteresting(commit);
766                         if (revs->show_all)
767                                 p = &commit_list_insert(commit, p)->next;
768                         slop = still_interesting(list, date, slop);
769                         if (slop)
770                                 continue;
771                         /* If showing all, add the whole pending list to the end */
772                         if (revs->show_all)
773                                 *p = list;
774                         break;
775                 }
776                 if (revs->min_age != -1 && (commit->date > revs->min_age))
777                         continue;
778                 date = commit->date;
779                 p = &commit_list_insert(commit, p)->next;
780
781                 show = show_early_output;
782                 if (!show)
783                         continue;
784
785                 show(revs, newlist);
786                 show_early_output = NULL;
787         }
788         if (revs->cherry_pick)
789                 cherry_pick_list(newlist, revs);
790
791         if (bottom) {
792                 limit_to_ancestry(bottom, newlist);
793                 free_commit_list(bottom);
794         }
795
796         revs->commits = newlist;
797         return 0;
798 }
799
800 struct all_refs_cb {
801         int all_flags;
802         int warned_bad_reflog;
803         struct rev_info *all_revs;
804         const char *name_for_errormsg;
805 };
806
807 static int handle_one_ref(const char *path, const unsigned char *sha1, int flag, void *cb_data)
808 {
809         struct all_refs_cb *cb = cb_data;
810         struct object *object = get_reference(cb->all_revs, path, sha1,
811                                               cb->all_flags);
812         add_pending_object(cb->all_revs, object, path);
813         return 0;
814 }
815
816 static void init_all_refs_cb(struct all_refs_cb *cb, struct rev_info *revs,
817         unsigned flags)
818 {
819         cb->all_revs = revs;
820         cb->all_flags = flags;
821 }
822
823 static void handle_refs(struct rev_info *revs, unsigned flags,
824                 int (*for_each)(each_ref_fn, void *))
825 {
826         struct all_refs_cb cb;
827         init_all_refs_cb(&cb, revs, flags);
828         for_each(handle_one_ref, &cb);
829 }
830
831 static void handle_one_reflog_commit(unsigned char *sha1, void *cb_data)
832 {
833         struct all_refs_cb *cb = cb_data;
834         if (!is_null_sha1(sha1)) {
835                 struct object *o = parse_object(sha1);
836                 if (o) {
837                         o->flags |= cb->all_flags;
838                         add_pending_object(cb->all_revs, o, "");
839                 }
840                 else if (!cb->warned_bad_reflog) {
841                         warning("reflog of '%s' references pruned commits",
842                                 cb->name_for_errormsg);
843                         cb->warned_bad_reflog = 1;
844                 }
845         }
846 }
847
848 static int handle_one_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
849                 const char *email, unsigned long timestamp, int tz,
850                 const char *message, void *cb_data)
851 {
852         handle_one_reflog_commit(osha1, cb_data);
853         handle_one_reflog_commit(nsha1, cb_data);
854         return 0;
855 }
856
857 static int handle_one_reflog(const char *path, const unsigned char *sha1, int flag, void *cb_data)
858 {
859         struct all_refs_cb *cb = cb_data;
860         cb->warned_bad_reflog = 0;
861         cb->name_for_errormsg = path;
862         for_each_reflog_ent(path, handle_one_reflog_ent, cb_data);
863         return 0;
864 }
865
866 static void handle_reflog(struct rev_info *revs, unsigned flags)
867 {
868         struct all_refs_cb cb;
869         cb.all_revs = revs;
870         cb.all_flags = flags;
871         for_each_reflog(handle_one_reflog, &cb);
872 }
873
874 static int add_parents_only(struct rev_info *revs, const char *arg, int flags)
875 {
876         unsigned char sha1[20];
877         struct object *it;
878         struct commit *commit;
879         struct commit_list *parents;
880
881         if (*arg == '^') {
882                 flags ^= UNINTERESTING;
883                 arg++;
884         }
885         if (get_sha1(arg, sha1))
886                 return 0;
887         while (1) {
888                 it = get_reference(revs, arg, sha1, 0);
889                 if (it->type != OBJ_TAG)
890                         break;
891                 if (!((struct tag*)it)->tagged)
892                         return 0;
893                 hashcpy(sha1, ((struct tag*)it)->tagged->sha1);
894         }
895         if (it->type != OBJ_COMMIT)
896                 return 0;
897         commit = (struct commit *)it;
898         for (parents = commit->parents; parents; parents = parents->next) {
899                 it = &parents->item->object;
900                 it->flags |= flags;
901                 add_pending_object(revs, it, arg);
902         }
903         return 1;
904 }
905
906 void init_revisions(struct rev_info *revs, const char *prefix)
907 {
908         memset(revs, 0, sizeof(*revs));
909
910         revs->abbrev = DEFAULT_ABBREV;
911         revs->ignore_merges = 1;
912         revs->simplify_history = 1;
913         DIFF_OPT_SET(&revs->pruning, RECURSIVE);
914         DIFF_OPT_SET(&revs->pruning, QUICK);
915         revs->pruning.add_remove = file_add_remove;
916         revs->pruning.change = file_change;
917         revs->lifo = 1;
918         revs->dense = 1;
919         revs->prefix = prefix;
920         revs->max_age = -1;
921         revs->min_age = -1;
922         revs->skip_count = -1;
923         revs->max_count = -1;
924
925         revs->commit_format = CMIT_FMT_DEFAULT;
926
927         revs->grep_filter.status_only = 1;
928         revs->grep_filter.pattern_tail = &(revs->grep_filter.pattern_list);
929         revs->grep_filter.header_tail = &(revs->grep_filter.header_list);
930         revs->grep_filter.regflags = REG_NEWLINE;
931
932         diff_setup(&revs->diffopt);
933         if (prefix && !revs->diffopt.prefix) {
934                 revs->diffopt.prefix = prefix;
935                 revs->diffopt.prefix_length = strlen(prefix);
936         }
937 }
938
939 static void add_pending_commit_list(struct rev_info *revs,
940                                     struct commit_list *commit_list,
941                                     unsigned int flags)
942 {
943         while (commit_list) {
944                 struct object *object = &commit_list->item->object;
945                 object->flags |= flags;
946                 add_pending_object(revs, object, sha1_to_hex(object->sha1));
947                 commit_list = commit_list->next;
948         }
949 }
950
951 static void prepare_show_merge(struct rev_info *revs)
952 {
953         struct commit_list *bases;
954         struct commit *head, *other;
955         unsigned char sha1[20];
956         const char **prune = NULL;
957         int i, prune_num = 1; /* counting terminating NULL */
958
959         if (get_sha1("HEAD", sha1) || !(head = lookup_commit(sha1)))
960                 die("--merge without HEAD?");
961         if (get_sha1("MERGE_HEAD", sha1) || !(other = lookup_commit(sha1)))
962                 die("--merge without MERGE_HEAD?");
963         add_pending_object(revs, &head->object, "HEAD");
964         add_pending_object(revs, &other->object, "MERGE_HEAD");
965         bases = get_merge_bases(head, other, 1);
966         add_pending_commit_list(revs, bases, UNINTERESTING);
967         free_commit_list(bases);
968         head->object.flags |= SYMMETRIC_LEFT;
969
970         if (!active_nr)
971                 read_cache();
972         for (i = 0; i < active_nr; i++) {
973                 struct cache_entry *ce = active_cache[i];
974                 if (!ce_stage(ce))
975                         continue;
976                 if (ce_path_match(ce, revs->prune_data)) {
977                         prune_num++;
978                         prune = xrealloc(prune, sizeof(*prune) * prune_num);
979                         prune[prune_num-2] = ce->name;
980                         prune[prune_num-1] = NULL;
981                 }
982                 while ((i+1 < active_nr) &&
983                        ce_same_name(ce, active_cache[i+1]))
984                         i++;
985         }
986         revs->prune_data = prune;
987         revs->limited = 1;
988 }
989
990 int handle_revision_arg(const char *arg, struct rev_info *revs,
991                         int flags,
992                         int cant_be_filename)
993 {
994         unsigned mode;
995         char *dotdot;
996         struct object *object;
997         unsigned char sha1[20];
998         int local_flags;
999
1000         dotdot = strstr(arg, "..");
1001         if (dotdot) {
1002                 unsigned char from_sha1[20];
1003                 const char *next = dotdot + 2;
1004                 const char *this = arg;
1005                 int symmetric = *next == '.';
1006                 unsigned int flags_exclude = flags ^ UNINTERESTING;
1007
1008                 *dotdot = 0;
1009                 next += symmetric;
1010
1011                 if (!*next)
1012                         next = "HEAD";
1013                 if (dotdot == arg)
1014                         this = "HEAD";
1015                 if (!get_sha1(this, from_sha1) &&
1016                     !get_sha1(next, sha1)) {
1017                         struct commit *a, *b;
1018                         struct commit_list *exclude;
1019
1020                         a = lookup_commit_reference(from_sha1);
1021                         b = lookup_commit_reference(sha1);
1022                         if (!a || !b) {
1023                                 die(symmetric ?
1024                                     "Invalid symmetric difference expression %s...%s" :
1025                                     "Invalid revision range %s..%s",
1026                                     arg, next);
1027                         }
1028
1029                         if (!cant_be_filename) {
1030                                 *dotdot = '.';
1031                                 verify_non_filename(revs->prefix, arg);
1032                         }
1033
1034                         if (symmetric) {
1035                                 exclude = get_merge_bases(a, b, 1);
1036                                 add_pending_commit_list(revs, exclude,
1037                                                         flags_exclude);
1038                                 free_commit_list(exclude);
1039                                 a->object.flags |= flags | SYMMETRIC_LEFT;
1040                         } else
1041                                 a->object.flags |= flags_exclude;
1042                         b->object.flags |= flags;
1043                         add_pending_object(revs, &a->object, this);
1044                         add_pending_object(revs, &b->object, next);
1045                         return 0;
1046                 }
1047                 *dotdot = '.';
1048         }
1049         dotdot = strstr(arg, "^@");
1050         if (dotdot && !dotdot[2]) {
1051                 *dotdot = 0;
1052                 if (add_parents_only(revs, arg, flags))
1053                         return 0;
1054                 *dotdot = '^';
1055         }
1056         dotdot = strstr(arg, "^!");
1057         if (dotdot && !dotdot[2]) {
1058                 *dotdot = 0;
1059                 if (!add_parents_only(revs, arg, flags ^ UNINTERESTING))
1060                         *dotdot = '^';
1061         }
1062
1063         local_flags = 0;
1064         if (*arg == '^') {
1065                 local_flags = UNINTERESTING;
1066                 arg++;
1067         }
1068         if (get_sha1_with_mode(arg, sha1, &mode))
1069                 return -1;
1070         if (!cant_be_filename)
1071                 verify_non_filename(revs->prefix, arg);
1072         object = get_reference(revs, arg, sha1, flags ^ local_flags);
1073         add_pending_object_with_mode(revs, object, arg, mode);
1074         return 0;
1075 }
1076
1077 static void read_pathspec_from_stdin(struct rev_info *revs, struct strbuf *sb, const char ***prune_data)
1078 {
1079         const char **prune = *prune_data;
1080         int prune_nr;
1081         int prune_alloc;
1082
1083         /* count existing ones */
1084         if (!prune)
1085                 prune_nr = 0;
1086         else
1087                 for (prune_nr = 0; prune[prune_nr]; prune_nr++)
1088                         ;
1089         prune_alloc = prune_nr; /* not really, but we do not know */
1090
1091         while (strbuf_getwholeline(sb, stdin, '\n') != EOF) {
1092                 int len = sb->len;
1093                 if (len && sb->buf[len - 1] == '\n')
1094                         sb->buf[--len] = '\0';
1095                 ALLOC_GROW(prune, prune_nr+1, prune_alloc);
1096                 prune[prune_nr++] = xstrdup(sb->buf);
1097         }
1098         if (prune) {
1099                 ALLOC_GROW(prune, prune_nr+1, prune_alloc);
1100                 prune[prune_nr] = NULL;
1101         }
1102         *prune_data = prune;
1103 }
1104
1105 static void read_revisions_from_stdin(struct rev_info *revs, const char ***prune)
1106 {
1107         struct strbuf sb;
1108         int seen_dashdash = 0;
1109
1110         strbuf_init(&sb, 1000);
1111         while (strbuf_getwholeline(&sb, stdin, '\n') != EOF) {
1112                 int len = sb.len;
1113                 if (len && sb.buf[len - 1] == '\n')
1114                         sb.buf[--len] = '\0';
1115                 if (!len)
1116                         break;
1117                 if (sb.buf[0] == '-') {
1118                         if (len == 2 && sb.buf[1] == '-') {
1119                                 seen_dashdash = 1;
1120                                 break;
1121                         }
1122                         die("options not supported in --stdin mode");
1123                 }
1124                 if (handle_revision_arg(sb.buf, revs, 0, 1))
1125                         die("bad revision '%s'", sb.buf);
1126         }
1127         if (seen_dashdash)
1128                 read_pathspec_from_stdin(revs, &sb, prune);
1129         strbuf_release(&sb);
1130 }
1131
1132 static void add_grep(struct rev_info *revs, const char *ptn, enum grep_pat_token what)
1133 {
1134         append_grep_pattern(&revs->grep_filter, ptn, "command line", 0, what);
1135 }
1136
1137 static void add_header_grep(struct rev_info *revs, enum grep_header_field field, const char *pattern)
1138 {
1139         append_header_grep_pattern(&revs->grep_filter, field, pattern);
1140 }
1141
1142 static void add_message_grep(struct rev_info *revs, const char *pattern)
1143 {
1144         add_grep(revs, pattern, GREP_PATTERN_BODY);
1145 }
1146
1147 static int handle_revision_opt(struct rev_info *revs, int argc, const char **argv,
1148                                int *unkc, const char **unkv)
1149 {
1150         const char *arg = argv[0];
1151
1152         /* pseudo revision arguments */
1153         if (!strcmp(arg, "--all") || !strcmp(arg, "--branches") ||
1154             !strcmp(arg, "--tags") || !strcmp(arg, "--remotes") ||
1155             !strcmp(arg, "--reflog") || !strcmp(arg, "--not") ||
1156             !strcmp(arg, "--no-walk") || !strcmp(arg, "--do-walk") ||
1157             !strcmp(arg, "--bisect"))
1158         {
1159                 unkv[(*unkc)++] = arg;
1160                 return 1;
1161         }
1162
1163         if (!prefixcmp(arg, "--max-count=")) {
1164                 revs->max_count = atoi(arg + 12);
1165                 revs->no_walk = 0;
1166         } else if (!prefixcmp(arg, "--skip=")) {
1167                 revs->skip_count = atoi(arg + 7);
1168         } else if ((*arg == '-') && isdigit(arg[1])) {
1169         /* accept -<digit>, like traditional "head" */
1170                 revs->max_count = atoi(arg + 1);
1171                 revs->no_walk = 0;
1172         } else if (!strcmp(arg, "-n")) {
1173                 if (argc <= 1)
1174                         return error("-n requires an argument");
1175                 revs->max_count = atoi(argv[1]);
1176                 revs->no_walk = 0;
1177                 return 2;
1178         } else if (!prefixcmp(arg, "-n")) {
1179                 revs->max_count = atoi(arg + 2);
1180                 revs->no_walk = 0;
1181         } else if (!prefixcmp(arg, "--max-age=")) {
1182                 revs->max_age = atoi(arg + 10);
1183         } else if (!prefixcmp(arg, "--since=")) {
1184                 revs->max_age = approxidate(arg + 8);
1185         } else if (!prefixcmp(arg, "--after=")) {
1186                 revs->max_age = approxidate(arg + 8);
1187         } else if (!prefixcmp(arg, "--min-age=")) {
1188                 revs->min_age = atoi(arg + 10);
1189         } else if (!prefixcmp(arg, "--before=")) {
1190                 revs->min_age = approxidate(arg + 9);
1191         } else if (!prefixcmp(arg, "--until=")) {
1192                 revs->min_age = approxidate(arg + 8);
1193         } else if (!strcmp(arg, "--first-parent")) {
1194                 revs->first_parent_only = 1;
1195         } else if (!strcmp(arg, "--ancestry-path")) {
1196                 revs->ancestry_path = 1;
1197                 revs->simplify_history = 0;
1198                 revs->limited = 1;
1199         } else if (!strcmp(arg, "-g") || !strcmp(arg, "--walk-reflogs")) {
1200                 init_reflog_walk(&revs->reflog_info);
1201         } else if (!strcmp(arg, "--default")) {
1202                 if (argc <= 1)
1203                         return error("bad --default argument");
1204                 revs->def = argv[1];
1205                 return 2;
1206         } else if (!strcmp(arg, "--merge")) {
1207                 revs->show_merge = 1;
1208         } else if (!strcmp(arg, "--topo-order")) {
1209                 revs->lifo = 1;
1210                 revs->topo_order = 1;
1211         } else if (!strcmp(arg, "--simplify-merges")) {
1212                 revs->simplify_merges = 1;
1213                 revs->rewrite_parents = 1;
1214                 revs->simplify_history = 0;
1215                 revs->limited = 1;
1216         } else if (!strcmp(arg, "--simplify-by-decoration")) {
1217                 revs->simplify_merges = 1;
1218                 revs->rewrite_parents = 1;
1219                 revs->simplify_history = 0;
1220                 revs->simplify_by_decoration = 1;
1221                 revs->limited = 1;
1222                 revs->prune = 1;
1223                 load_ref_decorations(DECORATE_SHORT_REFS);
1224         } else if (!strcmp(arg, "--date-order")) {
1225                 revs->lifo = 0;
1226                 revs->topo_order = 1;
1227         } else if (!prefixcmp(arg, "--early-output")) {
1228                 int count = 100;
1229                 switch (arg[14]) {
1230                 case '=':
1231                         count = atoi(arg+15);
1232                         /* Fallthrough */
1233                 case 0:
1234                         revs->topo_order = 1;
1235                        revs->early_output = count;
1236                 }
1237         } else if (!strcmp(arg, "--parents")) {
1238                 revs->rewrite_parents = 1;
1239                 revs->print_parents = 1;
1240         } else if (!strcmp(arg, "--dense")) {
1241                 revs->dense = 1;
1242         } else if (!strcmp(arg, "--sparse")) {
1243                 revs->dense = 0;
1244         } else if (!strcmp(arg, "--show-all")) {
1245                 revs->show_all = 1;
1246         } else if (!strcmp(arg, "--remove-empty")) {
1247                 revs->remove_empty_trees = 1;
1248         } else if (!strcmp(arg, "--merges")) {
1249                 revs->merges_only = 1;
1250         } else if (!strcmp(arg, "--no-merges")) {
1251                 revs->no_merges = 1;
1252         } else if (!strcmp(arg, "--boundary")) {
1253                 revs->boundary = 1;
1254         } else if (!strcmp(arg, "--left-right")) {
1255                 revs->left_right = 1;
1256         } else if (!strcmp(arg, "--count")) {
1257                 revs->count = 1;
1258         } else if (!strcmp(arg, "--cherry-pick")) {
1259                 revs->cherry_pick = 1;
1260                 revs->limited = 1;
1261         } else if (!strcmp(arg, "--objects")) {
1262                 revs->tag_objects = 1;
1263                 revs->tree_objects = 1;
1264                 revs->blob_objects = 1;
1265         } else if (!strcmp(arg, "--objects-edge")) {
1266                 revs->tag_objects = 1;
1267                 revs->tree_objects = 1;
1268                 revs->blob_objects = 1;
1269                 revs->edge_hint = 1;
1270         } else if (!strcmp(arg, "--unpacked")) {
1271                 revs->unpacked = 1;
1272         } else if (!prefixcmp(arg, "--unpacked=")) {
1273                 die("--unpacked=<packfile> no longer supported.");
1274         } else if (!strcmp(arg, "-r")) {
1275                 revs->diff = 1;
1276                 DIFF_OPT_SET(&revs->diffopt, RECURSIVE);
1277         } else if (!strcmp(arg, "-t")) {
1278                 revs->diff = 1;
1279                 DIFF_OPT_SET(&revs->diffopt, RECURSIVE);
1280                 DIFF_OPT_SET(&revs->diffopt, TREE_IN_RECURSIVE);
1281         } else if (!strcmp(arg, "-m")) {
1282                 revs->ignore_merges = 0;
1283         } else if (!strcmp(arg, "-c")) {
1284                 revs->diff = 1;
1285                 revs->dense_combined_merges = 0;
1286                 revs->combine_merges = 1;
1287         } else if (!strcmp(arg, "--cc")) {
1288                 revs->diff = 1;
1289                 revs->dense_combined_merges = 1;
1290                 revs->combine_merges = 1;
1291         } else if (!strcmp(arg, "-v")) {
1292                 revs->verbose_header = 1;
1293         } else if (!strcmp(arg, "--pretty")) {
1294                 revs->verbose_header = 1;
1295                 revs->pretty_given = 1;
1296                 get_commit_format(arg+8, revs);
1297         } else if (!prefixcmp(arg, "--pretty=") || !prefixcmp(arg, "--format=")) {
1298                 revs->verbose_header = 1;
1299                 revs->pretty_given = 1;
1300                 get_commit_format(arg+9, revs);
1301         } else if (!strcmp(arg, "--show-notes")) {
1302                 revs->show_notes = 1;
1303                 revs->show_notes_given = 1;
1304         } else if (!prefixcmp(arg, "--show-notes=")) {
1305                 struct strbuf buf = STRBUF_INIT;
1306                 revs->show_notes = 1;
1307                 revs->show_notes_given = 1;
1308                 if (!revs->notes_opt.extra_notes_refs)
1309                         revs->notes_opt.extra_notes_refs = xcalloc(1, sizeof(struct string_list));
1310                 if (!prefixcmp(arg+13, "refs/"))
1311                         /* happy */;
1312                 else if (!prefixcmp(arg+13, "notes/"))
1313                         strbuf_addstr(&buf, "refs/");
1314                 else
1315                         strbuf_addstr(&buf, "refs/notes/");
1316                 strbuf_addstr(&buf, arg+13);
1317                 string_list_append(revs->notes_opt.extra_notes_refs,
1318                                    strbuf_detach(&buf, NULL));
1319         } else if (!strcmp(arg, "--no-notes")) {
1320                 revs->show_notes = 0;
1321                 revs->show_notes_given = 1;
1322         } else if (!strcmp(arg, "--standard-notes")) {
1323                 revs->show_notes_given = 1;
1324                 revs->notes_opt.suppress_default_notes = 0;
1325         } else if (!strcmp(arg, "--no-standard-notes")) {
1326                 revs->notes_opt.suppress_default_notes = 1;
1327         } else if (!strcmp(arg, "--oneline")) {
1328                 revs->verbose_header = 1;
1329                 get_commit_format("oneline", revs);
1330                 revs->pretty_given = 1;
1331                 revs->abbrev_commit = 1;
1332         } else if (!strcmp(arg, "--graph")) {
1333                 revs->topo_order = 1;
1334                 revs->rewrite_parents = 1;
1335                 revs->graph = graph_init(revs);
1336         } else if (!strcmp(arg, "--root")) {
1337                 revs->show_root_diff = 1;
1338         } else if (!strcmp(arg, "--no-commit-id")) {
1339                 revs->no_commit_id = 1;
1340         } else if (!strcmp(arg, "--always")) {
1341                 revs->always_show_header = 1;
1342         } else if (!strcmp(arg, "--no-abbrev")) {
1343                 revs->abbrev = 0;
1344         } else if (!strcmp(arg, "--abbrev")) {
1345                 revs->abbrev = DEFAULT_ABBREV;
1346         } else if (!prefixcmp(arg, "--abbrev=")) {
1347                 revs->abbrev = strtoul(arg + 9, NULL, 10);
1348                 if (revs->abbrev < MINIMUM_ABBREV)
1349                         revs->abbrev = MINIMUM_ABBREV;
1350                 else if (revs->abbrev > 40)
1351                         revs->abbrev = 40;
1352         } else if (!strcmp(arg, "--abbrev-commit")) {
1353                 revs->abbrev_commit = 1;
1354         } else if (!strcmp(arg, "--full-diff")) {
1355                 revs->diff = 1;
1356                 revs->full_diff = 1;
1357         } else if (!strcmp(arg, "--full-history")) {
1358                 revs->simplify_history = 0;
1359         } else if (!strcmp(arg, "--relative-date")) {
1360                 revs->date_mode = DATE_RELATIVE;
1361                 revs->date_mode_explicit = 1;
1362         } else if (!strncmp(arg, "--date=", 7)) {
1363                 revs->date_mode = parse_date_format(arg + 7);
1364                 revs->date_mode_explicit = 1;
1365         } else if (!strcmp(arg, "--log-size")) {
1366                 revs->show_log_size = 1;
1367         }
1368         /*
1369          * Grepping the commit log
1370          */
1371         else if (!prefixcmp(arg, "--author=")) {
1372                 add_header_grep(revs, GREP_HEADER_AUTHOR, arg+9);
1373         } else if (!prefixcmp(arg, "--committer=")) {
1374                 add_header_grep(revs, GREP_HEADER_COMMITTER, arg+12);
1375         } else if (!prefixcmp(arg, "--grep=")) {
1376                 add_message_grep(revs, arg+7);
1377         } else if (!strcmp(arg, "--extended-regexp") || !strcmp(arg, "-E")) {
1378                 revs->grep_filter.regflags |= REG_EXTENDED;
1379         } else if (!strcmp(arg, "--regexp-ignore-case") || !strcmp(arg, "-i")) {
1380                 revs->grep_filter.regflags |= REG_ICASE;
1381         } else if (!strcmp(arg, "--fixed-strings") || !strcmp(arg, "-F")) {
1382                 revs->grep_filter.fixed = 1;
1383         } else if (!strcmp(arg, "--all-match")) {
1384                 revs->grep_filter.all_match = 1;
1385         } else if (!prefixcmp(arg, "--encoding=")) {
1386                 arg += 11;
1387                 if (strcmp(arg, "none"))
1388                         git_log_output_encoding = xstrdup(arg);
1389                 else
1390                         git_log_output_encoding = "";
1391         } else if (!strcmp(arg, "--reverse")) {
1392                 revs->reverse ^= 1;
1393         } else if (!strcmp(arg, "--children")) {
1394                 revs->children.name = "children";
1395                 revs->limited = 1;
1396         } else {
1397                 int opts = diff_opt_parse(&revs->diffopt, argv, argc);
1398                 if (!opts)
1399                         unkv[(*unkc)++] = arg;
1400                 return opts;
1401         }
1402
1403         return 1;
1404 }
1405
1406 void parse_revision_opt(struct rev_info *revs, struct parse_opt_ctx_t *ctx,
1407                         const struct option *options,
1408                         const char * const usagestr[])
1409 {
1410         int n = handle_revision_opt(revs, ctx->argc, ctx->argv,
1411                                     &ctx->cpidx, ctx->out);
1412         if (n <= 0) {
1413                 error("unknown option `%s'", ctx->argv[0]);
1414                 usage_with_options(usagestr, options);
1415         }
1416         ctx->argv += n;
1417         ctx->argc -= n;
1418 }
1419
1420 static int for_each_bad_bisect_ref(each_ref_fn fn, void *cb_data)
1421 {
1422         return for_each_ref_in("refs/bisect/bad", fn, cb_data);
1423 }
1424
1425 static int for_each_good_bisect_ref(each_ref_fn fn, void *cb_data)
1426 {
1427         return for_each_ref_in("refs/bisect/good", fn, cb_data);
1428 }
1429
1430 static void append_prune_data(const char ***prune_data, const char **av)
1431 {
1432         const char **prune = *prune_data;
1433         int prune_nr;
1434         int prune_alloc;
1435
1436         if (!prune) {
1437                 *prune_data = av;
1438                 return;
1439         }
1440
1441         /* count existing ones */
1442         for (prune_nr = 0; prune[prune_nr]; prune_nr++)
1443                 ;
1444         prune_alloc = prune_nr; /* not really, but we do not know */
1445
1446         while (*av) {
1447                 ALLOC_GROW(prune, prune_nr+1, prune_alloc);
1448                 prune[prune_nr++] = *av;
1449                 av++;
1450         }
1451         if (prune) {
1452                 ALLOC_GROW(prune, prune_nr+1, prune_alloc);
1453                 prune[prune_nr] = NULL;
1454         }
1455         *prune_data = prune;
1456 }
1457
1458 /*
1459  * Parse revision information, filling in the "rev_info" structure,
1460  * and removing the used arguments from the argument list.
1461  *
1462  * Returns the number of arguments left that weren't recognized
1463  * (which are also moved to the head of the argument list)
1464  */
1465 int setup_revisions(int argc, const char **argv, struct rev_info *revs, struct setup_revision_opt *opt)
1466 {
1467         int i, flags, left, seen_dashdash, read_from_stdin, got_rev_arg = 0;
1468         const char **prune_data = NULL;
1469
1470         /* First, search for "--" */
1471         seen_dashdash = 0;
1472         for (i = 1; i < argc; i++) {
1473                 const char *arg = argv[i];
1474                 if (strcmp(arg, "--"))
1475                         continue;
1476                 argv[i] = NULL;
1477                 argc = i;
1478                 if (argv[i + 1])
1479                         prune_data = argv + i + 1;
1480                 seen_dashdash = 1;
1481                 break;
1482         }
1483
1484         /* Second, deal with arguments and options */
1485         flags = 0;
1486         read_from_stdin = 0;
1487         for (left = i = 1; i < argc; i++) {
1488                 const char *arg = argv[i];
1489                 if (*arg == '-') {
1490                         int opts;
1491
1492                         if (!strcmp(arg, "--all")) {
1493                                 handle_refs(revs, flags, for_each_ref);
1494                                 handle_refs(revs, flags, head_ref);
1495                                 continue;
1496                         }
1497                         if (!strcmp(arg, "--branches")) {
1498                                 handle_refs(revs, flags, for_each_branch_ref);
1499                                 continue;
1500                         }
1501                         if (!strcmp(arg, "--bisect")) {
1502                                 handle_refs(revs, flags, for_each_bad_bisect_ref);
1503                                 handle_refs(revs, flags ^ UNINTERESTING, for_each_good_bisect_ref);
1504                                 revs->bisect = 1;
1505                                 continue;
1506                         }
1507                         if (!strcmp(arg, "--tags")) {
1508                                 handle_refs(revs, flags, for_each_tag_ref);
1509                                 continue;
1510                         }
1511                         if (!strcmp(arg, "--remotes")) {
1512                                 handle_refs(revs, flags, for_each_remote_ref);
1513                                 continue;
1514                         }
1515                         if (!prefixcmp(arg, "--glob=")) {
1516                                 struct all_refs_cb cb;
1517                                 init_all_refs_cb(&cb, revs, flags);
1518                                 for_each_glob_ref(handle_one_ref, arg + 7, &cb);
1519                                 continue;
1520                         }
1521                         if (!prefixcmp(arg, "--branches=")) {
1522                                 struct all_refs_cb cb;
1523                                 init_all_refs_cb(&cb, revs, flags);
1524                                 for_each_glob_ref_in(handle_one_ref, arg + 11, "refs/heads/", &cb);
1525                                 continue;
1526                         }
1527                         if (!prefixcmp(arg, "--tags=")) {
1528                                 struct all_refs_cb cb;
1529                                 init_all_refs_cb(&cb, revs, flags);
1530                                 for_each_glob_ref_in(handle_one_ref, arg + 7, "refs/tags/", &cb);
1531                                 continue;
1532                         }
1533                         if (!prefixcmp(arg, "--remotes=")) {
1534                                 struct all_refs_cb cb;
1535                                 init_all_refs_cb(&cb, revs, flags);
1536                                 for_each_glob_ref_in(handle_one_ref, arg + 10, "refs/remotes/", &cb);
1537                                 continue;
1538                         }
1539                         if (!strcmp(arg, "--reflog")) {
1540                                 handle_reflog(revs, flags);
1541                                 continue;
1542                         }
1543                         if (!strcmp(arg, "--not")) {
1544                                 flags ^= UNINTERESTING;
1545                                 continue;
1546                         }
1547                         if (!strcmp(arg, "--no-walk")) {
1548                                 revs->no_walk = 1;
1549                                 continue;
1550                         }
1551                         if (!strcmp(arg, "--do-walk")) {
1552                                 revs->no_walk = 0;
1553                                 continue;
1554                         }
1555                         if (!strcmp(arg, "--stdin")) {
1556                                 if (revs->disable_stdin) {
1557                                         argv[left++] = arg;
1558                                         continue;
1559                                 }
1560                                 if (read_from_stdin++)
1561                                         die("--stdin given twice?");
1562                                 read_revisions_from_stdin(revs, &prune_data);
1563                                 continue;
1564                         }
1565
1566                         opts = handle_revision_opt(revs, argc - i, argv + i, &left, argv);
1567                         if (opts > 0) {
1568                                 i += opts - 1;
1569                                 continue;
1570                         }
1571                         if (opts < 0)
1572                                 exit(128);
1573                         continue;
1574                 }
1575
1576                 if (handle_revision_arg(arg, revs, flags, seen_dashdash)) {
1577                         int j;
1578                         if (seen_dashdash || *arg == '^')
1579                                 die("bad revision '%s'", arg);
1580
1581                         /* If we didn't have a "--":
1582                          * (1) all filenames must exist;
1583                          * (2) all rev-args must not be interpretable
1584                          *     as a valid filename.
1585                          * but the latter we have checked in the main loop.
1586                          */
1587                         for (j = i; j < argc; j++)
1588                                 verify_filename(revs->prefix, argv[j]);
1589
1590                         append_prune_data(&prune_data, argv + i);
1591                         break;
1592                 }
1593                 else
1594                         got_rev_arg = 1;
1595         }
1596
1597         if (prune_data)
1598                 revs->prune_data = get_pathspec(revs->prefix, prune_data);
1599
1600         if (revs->def == NULL)
1601                 revs->def = opt ? opt->def : NULL;
1602         if (opt && opt->tweak)
1603                 opt->tweak(revs, opt);
1604         if (revs->show_merge)
1605                 prepare_show_merge(revs);
1606         if (revs->def && !revs->pending.nr && !got_rev_arg) {
1607                 unsigned char sha1[20];
1608                 struct object *object;
1609                 unsigned mode;
1610                 if (get_sha1_with_mode(revs->def, sha1, &mode))
1611                         die("bad default revision '%s'", revs->def);
1612                 object = get_reference(revs, revs->def, sha1, 0);
1613                 add_pending_object_with_mode(revs, object, revs->def, mode);
1614         }
1615
1616         /* Did the user ask for any diff output? Run the diff! */
1617         if (revs->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT)
1618                 revs->diff = 1;
1619
1620         /* Pickaxe, diff-filter and rename following need diffs */
1621         if (revs->diffopt.pickaxe ||
1622             revs->diffopt.filter ||
1623             DIFF_OPT_TST(&revs->diffopt, FOLLOW_RENAMES))
1624                 revs->diff = 1;
1625
1626         if (revs->topo_order)
1627                 revs->limited = 1;
1628
1629         if (revs->prune_data) {
1630                 diff_tree_setup_paths(revs->prune_data, &revs->pruning);
1631                 /* Can't prune commits with rename following: the paths change.. */
1632                 if (!DIFF_OPT_TST(&revs->diffopt, FOLLOW_RENAMES))
1633                         revs->prune = 1;
1634                 if (!revs->full_diff)
1635                         diff_tree_setup_paths(revs->prune_data, &revs->diffopt);
1636         }
1637         if (revs->combine_merges)
1638                 revs->ignore_merges = 0;
1639         revs->diffopt.abbrev = revs->abbrev;
1640         if (diff_setup_done(&revs->diffopt) < 0)
1641                 die("diff_setup_done failed");
1642
1643         compile_grep_patterns(&revs->grep_filter);
1644
1645         if (revs->reverse && revs->reflog_info)
1646                 die("cannot combine --reverse with --walk-reflogs");
1647         if (revs->rewrite_parents && revs->children.name)
1648                 die("cannot combine --parents and --children");
1649
1650         /*
1651          * Limitations on the graph functionality
1652          */
1653         if (revs->reverse && revs->graph)
1654                 die("cannot combine --reverse with --graph");
1655
1656         if (revs->reflog_info && revs->graph)
1657                 die("cannot combine --walk-reflogs with --graph");
1658
1659         return left;
1660 }
1661
1662 static void add_child(struct rev_info *revs, struct commit *parent, struct commit *child)
1663 {
1664         struct commit_list *l = xcalloc(1, sizeof(*l));
1665
1666         l->item = child;
1667         l->next = add_decoration(&revs->children, &parent->object, l);
1668 }
1669
1670 static int remove_duplicate_parents(struct commit *commit)
1671 {
1672         struct commit_list **pp, *p;
1673         int surviving_parents;
1674
1675         /* Examine existing parents while marking ones we have seen... */
1676         pp = &commit->parents;
1677         while ((p = *pp) != NULL) {
1678                 struct commit *parent = p->item;
1679                 if (parent->object.flags & TMP_MARK) {
1680                         *pp = p->next;
1681                         continue;
1682                 }
1683                 parent->object.flags |= TMP_MARK;
1684                 pp = &p->next;
1685         }
1686         /* count them while clearing the temporary mark */
1687         surviving_parents = 0;
1688         for (p = commit->parents; p; p = p->next) {
1689                 p->item->object.flags &= ~TMP_MARK;
1690                 surviving_parents++;
1691         }
1692         return surviving_parents;
1693 }
1694
1695 struct merge_simplify_state {
1696         struct commit *simplified;
1697 };
1698
1699 static struct merge_simplify_state *locate_simplify_state(struct rev_info *revs, struct commit *commit)
1700 {
1701         struct merge_simplify_state *st;
1702
1703         st = lookup_decoration(&revs->merge_simplification, &commit->object);
1704         if (!st) {
1705                 st = xcalloc(1, sizeof(*st));
1706                 add_decoration(&revs->merge_simplification, &commit->object, st);
1707         }
1708         return st;
1709 }
1710
1711 static struct commit_list **simplify_one(struct rev_info *revs, struct commit *commit, struct commit_list **tail)
1712 {
1713         struct commit_list *p;
1714         struct merge_simplify_state *st, *pst;
1715         int cnt;
1716
1717         st = locate_simplify_state(revs, commit);
1718
1719         /*
1720          * Have we handled this one?
1721          */
1722         if (st->simplified)
1723                 return tail;
1724
1725         /*
1726          * An UNINTERESTING commit simplifies to itself, so does a
1727          * root commit.  We do not rewrite parents of such commit
1728          * anyway.
1729          */
1730         if ((commit->object.flags & UNINTERESTING) || !commit->parents) {
1731                 st->simplified = commit;
1732                 return tail;
1733         }
1734
1735         /*
1736          * Do we know what commit all of our parents should be rewritten to?
1737          * Otherwise we are not ready to rewrite this one yet.
1738          */
1739         for (cnt = 0, p = commit->parents; p; p = p->next) {
1740                 pst = locate_simplify_state(revs, p->item);
1741                 if (!pst->simplified) {
1742                         tail = &commit_list_insert(p->item, tail)->next;
1743                         cnt++;
1744                 }
1745         }
1746         if (cnt) {
1747                 tail = &commit_list_insert(commit, tail)->next;
1748                 return tail;
1749         }
1750
1751         /*
1752          * Rewrite our list of parents.
1753          */
1754         for (p = commit->parents; p; p = p->next) {
1755                 pst = locate_simplify_state(revs, p->item);
1756                 p->item = pst->simplified;
1757         }
1758         cnt = remove_duplicate_parents(commit);
1759
1760         /*
1761          * It is possible that we are a merge and one side branch
1762          * does not have any commit that touches the given paths;
1763          * in such a case, the immediate parents will be rewritten
1764          * to different commits.
1765          *
1766          *      o----X          X: the commit we are looking at;
1767          *     /    /           o: a commit that touches the paths;
1768          * ---o----'
1769          *
1770          * Further reduce the parents by removing redundant parents.
1771          */
1772         if (1 < cnt) {
1773                 struct commit_list *h = reduce_heads(commit->parents);
1774                 cnt = commit_list_count(h);
1775                 free_commit_list(commit->parents);
1776                 commit->parents = h;
1777         }
1778
1779         /*
1780          * A commit simplifies to itself if it is a root, if it is
1781          * UNINTERESTING, if it touches the given paths, or if it is a
1782          * merge and its parents simplifies to more than one commits
1783          * (the first two cases are already handled at the beginning of
1784          * this function).
1785          *
1786          * Otherwise, it simplifies to what its sole parent simplifies to.
1787          */
1788         if (!cnt ||
1789             (commit->object.flags & UNINTERESTING) ||
1790             !(commit->object.flags & TREESAME) ||
1791             (1 < cnt))
1792                 st->simplified = commit;
1793         else {
1794                 pst = locate_simplify_state(revs, commit->parents->item);
1795                 st->simplified = pst->simplified;
1796         }
1797         return tail;
1798 }
1799
1800 static void simplify_merges(struct rev_info *revs)
1801 {
1802         struct commit_list *list;
1803         struct commit_list *yet_to_do, **tail;
1804
1805         if (!revs->topo_order)
1806                 sort_in_topological_order(&revs->commits, revs->lifo);
1807         if (!revs->prune)
1808                 return;
1809
1810         /* feed the list reversed */
1811         yet_to_do = NULL;
1812         for (list = revs->commits; list; list = list->next)
1813                 commit_list_insert(list->item, &yet_to_do);
1814         while (yet_to_do) {
1815                 list = yet_to_do;
1816                 yet_to_do = NULL;
1817                 tail = &yet_to_do;
1818                 while (list) {
1819                         struct commit *commit = list->item;
1820                         struct commit_list *next = list->next;
1821                         free(list);
1822                         list = next;
1823                         tail = simplify_one(revs, commit, tail);
1824                 }
1825         }
1826
1827         /* clean up the result, removing the simplified ones */
1828         list = revs->commits;
1829         revs->commits = NULL;
1830         tail = &revs->commits;
1831         while (list) {
1832                 struct commit *commit = list->item;
1833                 struct commit_list *next = list->next;
1834                 struct merge_simplify_state *st;
1835                 free(list);
1836                 list = next;
1837                 st = locate_simplify_state(revs, commit);
1838                 if (st->simplified == commit)
1839                         tail = &commit_list_insert(commit, tail)->next;
1840         }
1841 }
1842
1843 static void set_children(struct rev_info *revs)
1844 {
1845         struct commit_list *l;
1846         for (l = revs->commits; l; l = l->next) {
1847                 struct commit *commit = l->item;
1848                 struct commit_list *p;
1849
1850                 for (p = commit->parents; p; p = p->next)
1851                         add_child(revs, p->item, commit);
1852         }
1853 }
1854
1855 int prepare_revision_walk(struct rev_info *revs)
1856 {
1857         int nr = revs->pending.nr;
1858         struct object_array_entry *e, *list;
1859
1860         e = list = revs->pending.objects;
1861         revs->pending.nr = 0;
1862         revs->pending.alloc = 0;
1863         revs->pending.objects = NULL;
1864         while (--nr >= 0) {
1865                 struct commit *commit = handle_commit(revs, e->item, e->name);
1866                 if (commit) {
1867                         if (!(commit->object.flags & SEEN)) {
1868                                 commit->object.flags |= SEEN;
1869                                 insert_by_date(commit, &revs->commits);
1870                         }
1871                 }
1872                 e++;
1873         }
1874         free(list);
1875
1876         if (revs->no_walk)
1877                 return 0;
1878         if (revs->limited)
1879                 if (limit_list(revs) < 0)
1880                         return -1;
1881         if (revs->topo_order)
1882                 sort_in_topological_order(&revs->commits, revs->lifo);
1883         if (revs->simplify_merges)
1884                 simplify_merges(revs);
1885         if (revs->children.name)
1886                 set_children(revs);
1887         return 0;
1888 }
1889
1890 enum rewrite_result {
1891         rewrite_one_ok,
1892         rewrite_one_noparents,
1893         rewrite_one_error
1894 };
1895
1896 static enum rewrite_result rewrite_one(struct rev_info *revs, struct commit **pp)
1897 {
1898         struct commit_list *cache = NULL;
1899
1900         for (;;) {
1901                 struct commit *p = *pp;
1902                 if (!revs->limited)
1903                         if (add_parents_to_list(revs, p, &revs->commits, &cache) < 0)
1904                                 return rewrite_one_error;
1905                 if (p->parents && p->parents->next)
1906                         return rewrite_one_ok;
1907                 if (p->object.flags & UNINTERESTING)
1908                         return rewrite_one_ok;
1909                 if (!(p->object.flags & TREESAME))
1910                         return rewrite_one_ok;
1911                 if (!p->parents)
1912                         return rewrite_one_noparents;
1913                 *pp = p->parents->item;
1914         }
1915 }
1916
1917 static int rewrite_parents(struct rev_info *revs, struct commit *commit)
1918 {
1919         struct commit_list **pp = &commit->parents;
1920         while (*pp) {
1921                 struct commit_list *parent = *pp;
1922                 switch (rewrite_one(revs, &parent->item)) {
1923                 case rewrite_one_ok:
1924                         break;
1925                 case rewrite_one_noparents:
1926                         *pp = parent->next;
1927                         continue;
1928                 case rewrite_one_error:
1929                         return -1;
1930                 }
1931                 pp = &parent->next;
1932         }
1933         remove_duplicate_parents(commit);
1934         return 0;
1935 }
1936
1937 static int commit_match(struct commit *commit, struct rev_info *opt)
1938 {
1939         if (!opt->grep_filter.pattern_list && !opt->grep_filter.header_list)
1940                 return 1;
1941         return grep_buffer(&opt->grep_filter,
1942                            NULL, /* we say nothing, not even filename */
1943                            commit->buffer, strlen(commit->buffer));
1944 }
1945
1946 static inline int want_ancestry(struct rev_info *revs)
1947 {
1948         return (revs->rewrite_parents || revs->children.name);
1949 }
1950
1951 enum commit_action get_commit_action(struct rev_info *revs, struct commit *commit)
1952 {
1953         if (commit->object.flags & SHOWN)
1954                 return commit_ignore;
1955         if (revs->unpacked && has_sha1_pack(commit->object.sha1))
1956                 return commit_ignore;
1957         if (revs->show_all)
1958                 return commit_show;
1959         if (commit->object.flags & UNINTERESTING)
1960                 return commit_ignore;
1961         if (revs->min_age != -1 && (commit->date > revs->min_age))
1962                 return commit_ignore;
1963         if (revs->no_merges && commit->parents && commit->parents->next)
1964                 return commit_ignore;
1965         if (revs->merges_only && !(commit->parents && commit->parents->next))
1966                 return commit_ignore;
1967         if (!commit_match(commit, revs))
1968                 return commit_ignore;
1969         if (revs->prune && revs->dense) {
1970                 /* Commit without changes? */
1971                 if (commit->object.flags & TREESAME) {
1972                         /* drop merges unless we want parenthood */
1973                         if (!want_ancestry(revs))
1974                                 return commit_ignore;
1975                         /* non-merge - always ignore it */
1976                         if (!commit->parents || !commit->parents->next)
1977                                 return commit_ignore;
1978                 }
1979         }
1980         return commit_show;
1981 }
1982
1983 enum commit_action simplify_commit(struct rev_info *revs, struct commit *commit)
1984 {
1985         enum commit_action action = get_commit_action(revs, commit);
1986
1987         if (action == commit_show &&
1988             !revs->show_all &&
1989             revs->prune && revs->dense && want_ancestry(revs)) {
1990                 if (rewrite_parents(revs, commit) < 0)
1991                         return commit_error;
1992         }
1993         return action;
1994 }
1995
1996 static struct commit *get_revision_1(struct rev_info *revs)
1997 {
1998         if (!revs->commits)
1999                 return NULL;
2000
2001         do {
2002                 struct commit_list *entry = revs->commits;
2003                 struct commit *commit = entry->item;
2004
2005                 revs->commits = entry->next;
2006                 free(entry);
2007
2008                 if (revs->reflog_info)
2009                         fake_reflog_parent(revs->reflog_info, commit);
2010
2011                 /*
2012                  * If we haven't done the list limiting, we need to look at
2013                  * the parents here. We also need to do the date-based limiting
2014                  * that we'd otherwise have done in limit_list().
2015                  */
2016                 if (!revs->limited) {
2017                         if (revs->max_age != -1 &&
2018                             (commit->date < revs->max_age))
2019                                 continue;
2020                         if (add_parents_to_list(revs, commit, &revs->commits, NULL) < 0)
2021                                 die("Failed to traverse parents of commit %s",
2022                                     sha1_to_hex(commit->object.sha1));
2023                 }
2024
2025                 switch (simplify_commit(revs, commit)) {
2026                 case commit_ignore:
2027                         continue;
2028                 case commit_error:
2029                         die("Failed to simplify parents of commit %s",
2030                             sha1_to_hex(commit->object.sha1));
2031                 default:
2032                         return commit;
2033                 }
2034         } while (revs->commits);
2035         return NULL;
2036 }
2037
2038 static void gc_boundary(struct object_array *array)
2039 {
2040         unsigned nr = array->nr;
2041         unsigned alloc = array->alloc;
2042         struct object_array_entry *objects = array->objects;
2043
2044         if (alloc <= nr) {
2045                 unsigned i, j;
2046                 for (i = j = 0; i < nr; i++) {
2047                         if (objects[i].item->flags & SHOWN)
2048                                 continue;
2049                         if (i != j)
2050                                 objects[j] = objects[i];
2051                         j++;
2052                 }
2053                 for (i = j; i < nr; i++)
2054                         objects[i].item = NULL;
2055                 array->nr = j;
2056         }
2057 }
2058
2059 static void create_boundary_commit_list(struct rev_info *revs)
2060 {
2061         unsigned i;
2062         struct commit *c;
2063         struct object_array *array = &revs->boundary_commits;
2064         struct object_array_entry *objects = array->objects;
2065
2066         /*
2067          * If revs->commits is non-NULL at this point, an error occurred in
2068          * get_revision_1().  Ignore the error and continue printing the
2069          * boundary commits anyway.  (This is what the code has always
2070          * done.)
2071          */
2072         if (revs->commits) {
2073                 free_commit_list(revs->commits);
2074                 revs->commits = NULL;
2075         }
2076
2077         /*
2078          * Put all of the actual boundary commits from revs->boundary_commits
2079          * into revs->commits
2080          */
2081         for (i = 0; i < array->nr; i++) {
2082                 c = (struct commit *)(objects[i].item);
2083                 if (!c)
2084                         continue;
2085                 if (!(c->object.flags & CHILD_SHOWN))
2086                         continue;
2087                 if (c->object.flags & (SHOWN | BOUNDARY))
2088                         continue;
2089                 c->object.flags |= BOUNDARY;
2090                 commit_list_insert(c, &revs->commits);
2091         }
2092
2093         /*
2094          * If revs->topo_order is set, sort the boundary commits
2095          * in topological order
2096          */
2097         sort_in_topological_order(&revs->commits, revs->lifo);
2098 }
2099
2100 static struct commit *get_revision_internal(struct rev_info *revs)
2101 {
2102         struct commit *c = NULL;
2103         struct commit_list *l;
2104
2105         if (revs->boundary == 2) {
2106                 /*
2107                  * All of the normal commits have already been returned,
2108                  * and we are now returning boundary commits.
2109                  * create_boundary_commit_list() has populated
2110                  * revs->commits with the remaining commits to return.
2111                  */
2112                 c = pop_commit(&revs->commits);
2113                 if (c)
2114                         c->object.flags |= SHOWN;
2115                 return c;
2116         }
2117
2118         /*
2119          * Now pick up what they want to give us
2120          */
2121         c = get_revision_1(revs);
2122         if (c) {
2123                 while (0 < revs->skip_count) {
2124                         revs->skip_count--;
2125                         c = get_revision_1(revs);
2126                         if (!c)
2127                                 break;
2128                 }
2129         }
2130
2131         /*
2132          * Check the max_count.
2133          */
2134         switch (revs->max_count) {
2135         case -1:
2136                 break;
2137         case 0:
2138                 c = NULL;
2139                 break;
2140         default:
2141                 revs->max_count--;
2142         }
2143
2144         if (c)
2145                 c->object.flags |= SHOWN;
2146
2147         if (!revs->boundary) {
2148                 return c;
2149         }
2150
2151         if (!c) {
2152                 /*
2153                  * get_revision_1() runs out the commits, and
2154                  * we are done computing the boundaries.
2155                  * switch to boundary commits output mode.
2156                  */
2157                 revs->boundary = 2;
2158
2159                 /*
2160                  * Update revs->commits to contain the list of
2161                  * boundary commits.
2162                  */
2163                 create_boundary_commit_list(revs);
2164
2165                 return get_revision_internal(revs);
2166         }
2167
2168         /*
2169          * boundary commits are the commits that are parents of the
2170          * ones we got from get_revision_1() but they themselves are
2171          * not returned from get_revision_1().  Before returning
2172          * 'c', we need to mark its parents that they could be boundaries.
2173          */
2174
2175         for (l = c->parents; l; l = l->next) {
2176                 struct object *p;
2177                 p = &(l->item->object);
2178                 if (p->flags & (CHILD_SHOWN | SHOWN))
2179                         continue;
2180                 p->flags |= CHILD_SHOWN;
2181                 gc_boundary(&revs->boundary_commits);
2182                 add_object_array(p, NULL, &revs->boundary_commits);
2183         }
2184
2185         return c;
2186 }
2187
2188 struct commit *get_revision(struct rev_info *revs)
2189 {
2190         struct commit *c;
2191         struct commit_list *reversed;
2192
2193         if (revs->reverse) {
2194                 reversed = NULL;
2195                 while ((c = get_revision_internal(revs))) {
2196                         commit_list_insert(c, &reversed);
2197                 }
2198                 revs->commits = reversed;
2199                 revs->reverse = 0;
2200                 revs->reverse_output_stage = 1;
2201         }
2202
2203         if (revs->reverse_output_stage)
2204                 return pop_commit(&revs->commits);
2205
2206         c = get_revision_internal(revs);
2207         if (c && revs->graph)
2208                 graph_update(revs->graph, c);
2209         return c;
2210 }