]> asedeno.scripts.mit.edu Git - git.git/blob - revision.c
Merge branch 'sh/p4'
[git.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 "grep.h"
10 #include "reflog-walk.h"
11 #include "patch-ids.h"
12
13 volatile show_early_output_fn_t show_early_output;
14
15 static char *path_name(struct name_path *path, const char *name)
16 {
17         struct name_path *p;
18         char *n, *m;
19         int nlen = strlen(name);
20         int len = nlen + 1;
21
22         for (p = path; p; p = p->up) {
23                 if (p->elem_len)
24                         len += p->elem_len + 1;
25         }
26         n = xmalloc(len);
27         m = n + len - (nlen + 1);
28         strcpy(m, name);
29         for (p = path; p; p = p->up) {
30                 if (p->elem_len) {
31                         m -= p->elem_len + 1;
32                         memcpy(m, p->elem, p->elem_len);
33                         m[p->elem_len] = '/';
34                 }
35         }
36         return n;
37 }
38
39 void add_object(struct object *obj,
40                 struct object_array *p,
41                 struct name_path *path,
42                 const char *name)
43 {
44         add_object_array(obj, path_name(path, name), p);
45 }
46
47 static void mark_blob_uninteresting(struct blob *blob)
48 {
49         if (blob->object.flags & UNINTERESTING)
50                 return;
51         blob->object.flags |= UNINTERESTING;
52 }
53
54 void mark_tree_uninteresting(struct tree *tree)
55 {
56         struct tree_desc desc;
57         struct name_entry entry;
58         struct object *obj = &tree->object;
59
60         if (obj->flags & UNINTERESTING)
61                 return;
62         obj->flags |= UNINTERESTING;
63         if (!has_sha1_file(obj->sha1))
64                 return;
65         if (parse_tree(tree) < 0)
66                 die("bad tree %s", sha1_to_hex(obj->sha1));
67
68         init_tree_desc(&desc, tree->buffer, tree->size);
69         while (tree_entry(&desc, &entry)) {
70                 if (S_ISDIR(entry.mode))
71                         mark_tree_uninteresting(lookup_tree(entry.sha1));
72                 else
73                         mark_blob_uninteresting(lookup_blob(entry.sha1));
74         }
75
76         /*
77          * We don't care about the tree any more
78          * after it has been marked uninteresting.
79          */
80         free(tree->buffer);
81         tree->buffer = NULL;
82 }
83
84 void mark_parents_uninteresting(struct commit *commit)
85 {
86         struct commit_list *parents = commit->parents;
87
88         while (parents) {
89                 struct commit *commit = parents->item;
90                 if (!(commit->object.flags & UNINTERESTING)) {
91                         commit->object.flags |= UNINTERESTING;
92
93                         /*
94                          * Normally we haven't parsed the parent
95                          * yet, so we won't have a parent of a parent
96                          * here. However, it may turn out that we've
97                          * reached this commit some other way (where it
98                          * wasn't uninteresting), in which case we need
99                          * to mark its parents recursively too..
100                          */
101                         if (commit->parents)
102                                 mark_parents_uninteresting(commit);
103                 }
104
105                 /*
106                  * A missing commit is ok iff its parent is marked
107                  * uninteresting.
108                  *
109                  * We just mark such a thing parsed, so that when
110                  * it is popped next time around, we won't be trying
111                  * to parse it and get an error.
112                  */
113                 if (!has_sha1_file(commit->object.sha1))
114                         commit->object.parsed = 1;
115                 parents = parents->next;
116         }
117 }
118
119 static void add_pending_object_with_mode(struct rev_info *revs, struct object *obj, const char *name, unsigned mode)
120 {
121         if (revs->no_walk && (obj->flags & UNINTERESTING))
122                 die("object ranges do not make sense when not walking revisions");
123         if (revs->reflog_info && obj->type == OBJ_COMMIT &&
124                         add_reflog_for_walk(revs->reflog_info,
125                                 (struct commit *)obj, name))
126                 return;
127         add_object_array_with_mode(obj, name, &revs->pending, mode);
128 }
129
130 void add_pending_object(struct rev_info *revs, struct object *obj, const char *name)
131 {
132         add_pending_object_with_mode(revs, obj, name, S_IFINVALID);
133 }
134
135 static struct object *get_reference(struct rev_info *revs, const char *name, const unsigned char *sha1, unsigned int flags)
136 {
137         struct object *object;
138
139         object = parse_object(sha1);
140         if (!object)
141                 die("bad object %s", name);
142         object->flags |= flags;
143         return object;
144 }
145
146 static struct commit *handle_commit(struct rev_info *revs, struct object *object, const char *name)
147 {
148         unsigned long flags = object->flags;
149
150         /*
151          * Tag object? Look what it points to..
152          */
153         while (object->type == OBJ_TAG) {
154                 struct tag *tag = (struct tag *) object;
155                 if (revs->tag_objects && !(flags & UNINTERESTING))
156                         add_pending_object(revs, object, tag->tag);
157                 object = parse_object(tag->tagged->sha1);
158                 if (!object)
159                         die("bad object %s", sha1_to_hex(tag->tagged->sha1));
160         }
161
162         /*
163          * Commit object? Just return it, we'll do all the complex
164          * reachability crud.
165          */
166         if (object->type == OBJ_COMMIT) {
167                 struct commit *commit = (struct commit *)object;
168                 if (parse_commit(commit) < 0)
169                         die("unable to parse commit %s", name);
170                 if (flags & UNINTERESTING) {
171                         commit->object.flags |= UNINTERESTING;
172                         mark_parents_uninteresting(commit);
173                         revs->limited = 1;
174                 }
175                 return commit;
176         }
177
178         /*
179          * Tree object? Either mark it uniniteresting, or add it
180          * to the list of objects to look at later..
181          */
182         if (object->type == OBJ_TREE) {
183                 struct tree *tree = (struct tree *)object;
184                 if (!revs->tree_objects)
185                         return NULL;
186                 if (flags & UNINTERESTING) {
187                         mark_tree_uninteresting(tree);
188                         return NULL;
189                 }
190                 add_pending_object(revs, object, "");
191                 return NULL;
192         }
193
194         /*
195          * Blob object? You know the drill by now..
196          */
197         if (object->type == OBJ_BLOB) {
198                 struct blob *blob = (struct blob *)object;
199                 if (!revs->blob_objects)
200                         return NULL;
201                 if (flags & UNINTERESTING) {
202                         mark_blob_uninteresting(blob);
203                         return NULL;
204                 }
205                 add_pending_object(revs, object, "");
206                 return NULL;
207         }
208         die("%s is unknown object", name);
209 }
210
211 static int everybody_uninteresting(struct commit_list *orig)
212 {
213         struct commit_list *list = orig;
214         while (list) {
215                 struct commit *commit = list->item;
216                 list = list->next;
217                 if (commit->object.flags & UNINTERESTING)
218                         continue;
219                 return 0;
220         }
221         return 1;
222 }
223
224 /*
225  * The goal is to get REV_TREE_NEW as the result only if the
226  * diff consists of all '+' (and no other changes), and
227  * REV_TREE_DIFFERENT otherwise (of course if the trees are
228  * the same we want REV_TREE_SAME).  That means that once we
229  * get to REV_TREE_DIFFERENT, we do not have to look any further.
230  */
231 static int tree_difference = REV_TREE_SAME;
232
233 static void file_add_remove(struct diff_options *options,
234                     int addremove, unsigned mode,
235                     const unsigned char *sha1,
236                     const char *base, const char *path)
237 {
238         int diff = REV_TREE_DIFFERENT;
239
240         /*
241          * Is it an add of a new file? It means that the old tree
242          * didn't have it at all, so we will turn "REV_TREE_SAME" ->
243          * "REV_TREE_NEW", but leave any "REV_TREE_DIFFERENT" alone
244          * (and if it already was "REV_TREE_NEW", we'll keep it
245          * "REV_TREE_NEW" of course).
246          */
247         if (addremove == '+') {
248                 diff = tree_difference;
249                 if (diff != REV_TREE_SAME)
250                         return;
251                 diff = REV_TREE_NEW;
252         }
253         tree_difference = diff;
254         if (tree_difference == REV_TREE_DIFFERENT)
255                 DIFF_OPT_SET(options, HAS_CHANGES);
256 }
257
258 static void file_change(struct diff_options *options,
259                  unsigned old_mode, unsigned new_mode,
260                  const unsigned char *old_sha1,
261                  const unsigned char *new_sha1,
262                  const char *base, const char *path)
263 {
264         tree_difference = REV_TREE_DIFFERENT;
265         DIFF_OPT_SET(options, HAS_CHANGES);
266 }
267
268 static int rev_compare_tree(struct rev_info *revs, struct tree *t1, struct tree *t2)
269 {
270         if (!t1)
271                 return REV_TREE_NEW;
272         if (!t2)
273                 return REV_TREE_DIFFERENT;
274         tree_difference = REV_TREE_SAME;
275         DIFF_OPT_CLR(&revs->pruning, HAS_CHANGES);
276         if (diff_tree_sha1(t1->object.sha1, t2->object.sha1, "",
277                            &revs->pruning) < 0)
278                 return REV_TREE_DIFFERENT;
279         return tree_difference;
280 }
281
282 static int rev_same_tree_as_empty(struct rev_info *revs, struct tree *t1)
283 {
284         int retval;
285         void *tree;
286         unsigned long size;
287         struct tree_desc empty, real;
288
289         if (!t1)
290                 return 0;
291
292         tree = read_object_with_reference(t1->object.sha1, tree_type, &size, NULL);
293         if (!tree)
294                 return 0;
295         init_tree_desc(&real, tree, size);
296         init_tree_desc(&empty, "", 0);
297
298         tree_difference = REV_TREE_SAME;
299         DIFF_OPT_CLR(&revs->pruning, HAS_CHANGES);
300         retval = diff_tree(&empty, &real, "", &revs->pruning);
301         free(tree);
302
303         return retval >= 0 && (tree_difference == REV_TREE_SAME);
304 }
305
306 static void try_to_simplify_commit(struct rev_info *revs, struct commit *commit)
307 {
308         struct commit_list **pp, *parent;
309         int tree_changed = 0, tree_same = 0;
310
311         /*
312          * If we don't do pruning, everything is interesting
313          */
314         if (!revs->prune)
315                 return;
316
317         if (!commit->tree)
318                 return;
319
320         if (!commit->parents) {
321                 if (rev_same_tree_as_empty(revs, commit->tree))
322                         commit->object.flags |= TREESAME;
323                 return;
324         }
325
326         /*
327          * Normal non-merge commit? If we don't want to make the
328          * history dense, we consider it always to be a change..
329          */
330         if (!revs->dense && !commit->parents->next)
331                 return;
332
333         pp = &commit->parents;
334         while ((parent = *pp) != NULL) {
335                 struct commit *p = parent->item;
336
337                 if (parse_commit(p) < 0)
338                         die("cannot simplify commit %s (because of %s)",
339                             sha1_to_hex(commit->object.sha1),
340                             sha1_to_hex(p->object.sha1));
341                 switch (rev_compare_tree(revs, p->tree, commit->tree)) {
342                 case REV_TREE_SAME:
343                         tree_same = 1;
344                         if (!revs->simplify_history || (p->object.flags & UNINTERESTING)) {
345                                 /* Even if a merge with an uninteresting
346                                  * side branch brought the entire change
347                                  * we are interested in, we do not want
348                                  * to lose the other branches of this
349                                  * merge, so we just keep going.
350                                  */
351                                 pp = &parent->next;
352                                 continue;
353                         }
354                         parent->next = NULL;
355                         commit->parents = parent;
356                         commit->object.flags |= TREESAME;
357                         return;
358
359                 case REV_TREE_NEW:
360                         if (revs->remove_empty_trees &&
361                             rev_same_tree_as_empty(revs, p->tree)) {
362                                 /* We are adding all the specified
363                                  * paths from this parent, so the
364                                  * history beyond this parent is not
365                                  * interesting.  Remove its parents
366                                  * (they are grandparents for us).
367                                  * IOW, we pretend this parent is a
368                                  * "root" commit.
369                                  */
370                                 if (parse_commit(p) < 0)
371                                         die("cannot simplify commit %s (invalid %s)",
372                                             sha1_to_hex(commit->object.sha1),
373                                             sha1_to_hex(p->object.sha1));
374                                 p->parents = NULL;
375                         }
376                 /* fallthrough */
377                 case REV_TREE_DIFFERENT:
378                         tree_changed = 1;
379                         pp = &parent->next;
380                         continue;
381                 }
382                 die("bad tree compare for commit %s", sha1_to_hex(commit->object.sha1));
383         }
384         if (tree_changed && !tree_same)
385                 return;
386         commit->object.flags |= TREESAME;
387 }
388
389 static int add_parents_to_list(struct rev_info *revs, struct commit *commit, struct commit_list **list)
390 {
391         struct commit_list *parent = commit->parents;
392         unsigned left_flag;
393         int add, rest;
394
395         if (commit->object.flags & ADDED)
396                 return 0;
397         commit->object.flags |= ADDED;
398
399         /*
400          * If the commit is uninteresting, don't try to
401          * prune parents - we want the maximal uninteresting
402          * set.
403          *
404          * Normally we haven't parsed the parent
405          * yet, so we won't have a parent of a parent
406          * here. However, it may turn out that we've
407          * reached this commit some other way (where it
408          * wasn't uninteresting), in which case we need
409          * to mark its parents recursively too..
410          */
411         if (commit->object.flags & UNINTERESTING) {
412                 while (parent) {
413                         struct commit *p = parent->item;
414                         parent = parent->next;
415                         if (parse_commit(p) < 0)
416                                 return -1;
417                         p->object.flags |= UNINTERESTING;
418                         if (p->parents)
419                                 mark_parents_uninteresting(p);
420                         if (p->object.flags & SEEN)
421                                 continue;
422                         p->object.flags |= SEEN;
423                         insert_by_date(p, list);
424                 }
425                 return 0;
426         }
427
428         /*
429          * Ok, the commit wasn't uninteresting. Try to
430          * simplify the commit history and find the parent
431          * that has no differences in the path set if one exists.
432          */
433         try_to_simplify_commit(revs, commit);
434
435         if (revs->no_walk)
436                 return 0;
437
438         left_flag = (commit->object.flags & SYMMETRIC_LEFT);
439
440         rest = !revs->first_parent_only;
441         for (parent = commit->parents, add = 1; parent; add = rest) {
442                 struct commit *p = parent->item;
443
444                 parent = parent->next;
445                 if (parse_commit(p) < 0)
446                         return -1;
447                 p->object.flags |= left_flag;
448                 if (p->object.flags & SEEN)
449                         continue;
450                 p->object.flags |= SEEN;
451                 if (add)
452                         insert_by_date(p, list);
453         }
454         return 0;
455 }
456
457 static void cherry_pick_list(struct commit_list *list, struct rev_info *revs)
458 {
459         struct commit_list *p;
460         int left_count = 0, right_count = 0;
461         int left_first;
462         struct patch_ids ids;
463
464         /* First count the commits on the left and on the right */
465         for (p = list; p; p = p->next) {
466                 struct commit *commit = p->item;
467                 unsigned flags = commit->object.flags;
468                 if (flags & BOUNDARY)
469                         ;
470                 else if (flags & SYMMETRIC_LEFT)
471                         left_count++;
472                 else
473                         right_count++;
474         }
475
476         left_first = left_count < right_count;
477         init_patch_ids(&ids);
478         if (revs->diffopt.nr_paths) {
479                 ids.diffopts.nr_paths = revs->diffopt.nr_paths;
480                 ids.diffopts.paths = revs->diffopt.paths;
481                 ids.diffopts.pathlens = revs->diffopt.pathlens;
482         }
483
484         /* Compute patch-ids for one side */
485         for (p = list; p; p = p->next) {
486                 struct commit *commit = p->item;
487                 unsigned flags = commit->object.flags;
488
489                 if (flags & BOUNDARY)
490                         continue;
491                 /*
492                  * If we have fewer left, left_first is set and we omit
493                  * commits on the right branch in this loop.  If we have
494                  * fewer right, we skip the left ones.
495                  */
496                 if (left_first != !!(flags & SYMMETRIC_LEFT))
497                         continue;
498                 commit->util = add_commit_patch_id(commit, &ids);
499         }
500
501         /* Check the other side */
502         for (p = list; p; p = p->next) {
503                 struct commit *commit = p->item;
504                 struct patch_id *id;
505                 unsigned flags = commit->object.flags;
506
507                 if (flags & BOUNDARY)
508                         continue;
509                 /*
510                  * If we have fewer left, left_first is set and we omit
511                  * commits on the left branch in this loop.
512                  */
513                 if (left_first == !!(flags & SYMMETRIC_LEFT))
514                         continue;
515
516                 /*
517                  * Have we seen the same patch id?
518                  */
519                 id = has_commit_patch_id(commit, &ids);
520                 if (!id)
521                         continue;
522                 id->seen = 1;
523                 commit->object.flags |= SHOWN;
524         }
525
526         /* Now check the original side for seen ones */
527         for (p = list; p; p = p->next) {
528                 struct commit *commit = p->item;
529                 struct patch_id *ent;
530
531                 ent = commit->util;
532                 if (!ent)
533                         continue;
534                 if (ent->seen)
535                         commit->object.flags |= SHOWN;
536                 commit->util = NULL;
537         }
538
539         free_patch_ids(&ids);
540 }
541
542 static int limit_list(struct rev_info *revs)
543 {
544         struct commit_list *list = revs->commits;
545         struct commit_list *newlist = NULL;
546         struct commit_list **p = &newlist;
547
548         while (list) {
549                 struct commit_list *entry = list;
550                 struct commit *commit = list->item;
551                 struct object *obj = &commit->object;
552                 show_early_output_fn_t show;
553
554                 list = list->next;
555                 free(entry);
556
557                 if (revs->max_age != -1 && (commit->date < revs->max_age))
558                         obj->flags |= UNINTERESTING;
559                 if (add_parents_to_list(revs, commit, &list) < 0)
560                         return -1;
561                 if (obj->flags & UNINTERESTING) {
562                         mark_parents_uninteresting(commit);
563                         if (everybody_uninteresting(list))
564                                 break;
565                         continue;
566                 }
567                 if (revs->min_age != -1 && (commit->date > revs->min_age))
568                         continue;
569                 p = &commit_list_insert(commit, p)->next;
570
571                 show = show_early_output;
572                 if (!show)
573                         continue;
574
575                 show(revs, newlist);
576                 show_early_output = NULL;
577         }
578         if (revs->cherry_pick)
579                 cherry_pick_list(newlist, revs);
580
581         revs->commits = newlist;
582         return 0;
583 }
584
585 struct all_refs_cb {
586         int all_flags;
587         int warned_bad_reflog;
588         struct rev_info *all_revs;
589         const char *name_for_errormsg;
590 };
591
592 static int handle_one_ref(const char *path, const unsigned char *sha1, int flag, void *cb_data)
593 {
594         struct all_refs_cb *cb = cb_data;
595         struct object *object = get_reference(cb->all_revs, path, sha1,
596                                               cb->all_flags);
597         add_pending_object(cb->all_revs, object, path);
598         return 0;
599 }
600
601 static void handle_all(struct rev_info *revs, unsigned flags)
602 {
603         struct all_refs_cb cb;
604         cb.all_revs = revs;
605         cb.all_flags = flags;
606         for_each_ref(handle_one_ref, &cb);
607 }
608
609 static void handle_one_reflog_commit(unsigned char *sha1, void *cb_data)
610 {
611         struct all_refs_cb *cb = cb_data;
612         if (!is_null_sha1(sha1)) {
613                 struct object *o = parse_object(sha1);
614                 if (o) {
615                         o->flags |= cb->all_flags;
616                         add_pending_object(cb->all_revs, o, "");
617                 }
618                 else if (!cb->warned_bad_reflog) {
619                         warning("reflog of '%s' references pruned commits",
620                                 cb->name_for_errormsg);
621                         cb->warned_bad_reflog = 1;
622                 }
623         }
624 }
625
626 static int handle_one_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
627                 const char *email, unsigned long timestamp, int tz,
628                 const char *message, void *cb_data)
629 {
630         handle_one_reflog_commit(osha1, cb_data);
631         handle_one_reflog_commit(nsha1, cb_data);
632         return 0;
633 }
634
635 static int handle_one_reflog(const char *path, const unsigned char *sha1, int flag, void *cb_data)
636 {
637         struct all_refs_cb *cb = cb_data;
638         cb->warned_bad_reflog = 0;
639         cb->name_for_errormsg = path;
640         for_each_reflog_ent(path, handle_one_reflog_ent, cb_data);
641         return 0;
642 }
643
644 static void handle_reflog(struct rev_info *revs, unsigned flags)
645 {
646         struct all_refs_cb cb;
647         cb.all_revs = revs;
648         cb.all_flags = flags;
649         for_each_reflog(handle_one_reflog, &cb);
650 }
651
652 static int add_parents_only(struct rev_info *revs, const char *arg, int flags)
653 {
654         unsigned char sha1[20];
655         struct object *it;
656         struct commit *commit;
657         struct commit_list *parents;
658
659         if (*arg == '^') {
660                 flags ^= UNINTERESTING;
661                 arg++;
662         }
663         if (get_sha1(arg, sha1))
664                 return 0;
665         while (1) {
666                 it = get_reference(revs, arg, sha1, 0);
667                 if (it->type != OBJ_TAG)
668                         break;
669                 hashcpy(sha1, ((struct tag*)it)->tagged->sha1);
670         }
671         if (it->type != OBJ_COMMIT)
672                 return 0;
673         commit = (struct commit *)it;
674         for (parents = commit->parents; parents; parents = parents->next) {
675                 it = &parents->item->object;
676                 it->flags |= flags;
677                 add_pending_object(revs, it, arg);
678         }
679         return 1;
680 }
681
682 void init_revisions(struct rev_info *revs, const char *prefix)
683 {
684         memset(revs, 0, sizeof(*revs));
685
686         revs->abbrev = DEFAULT_ABBREV;
687         revs->ignore_merges = 1;
688         revs->simplify_history = 1;
689         DIFF_OPT_SET(&revs->pruning, RECURSIVE);
690         DIFF_OPT_SET(&revs->pruning, QUIET);
691         revs->pruning.add_remove = file_add_remove;
692         revs->pruning.change = file_change;
693         revs->lifo = 1;
694         revs->dense = 1;
695         revs->prefix = prefix;
696         revs->max_age = -1;
697         revs->min_age = -1;
698         revs->skip_count = -1;
699         revs->max_count = -1;
700
701         revs->commit_format = CMIT_FMT_DEFAULT;
702
703         diff_setup(&revs->diffopt);
704 }
705
706 static void add_pending_commit_list(struct rev_info *revs,
707                                     struct commit_list *commit_list,
708                                     unsigned int flags)
709 {
710         while (commit_list) {
711                 struct object *object = &commit_list->item->object;
712                 object->flags |= flags;
713                 add_pending_object(revs, object, sha1_to_hex(object->sha1));
714                 commit_list = commit_list->next;
715         }
716 }
717
718 static void prepare_show_merge(struct rev_info *revs)
719 {
720         struct commit_list *bases;
721         struct commit *head, *other;
722         unsigned char sha1[20];
723         const char **prune = NULL;
724         int i, prune_num = 1; /* counting terminating NULL */
725
726         if (get_sha1("HEAD", sha1) || !(head = lookup_commit(sha1)))
727                 die("--merge without HEAD?");
728         if (get_sha1("MERGE_HEAD", sha1) || !(other = lookup_commit(sha1)))
729                 die("--merge without MERGE_HEAD?");
730         add_pending_object(revs, &head->object, "HEAD");
731         add_pending_object(revs, &other->object, "MERGE_HEAD");
732         bases = get_merge_bases(head, other, 1);
733         while (bases) {
734                 struct commit *it = bases->item;
735                 struct commit_list *n = bases->next;
736                 free(bases);
737                 bases = n;
738                 it->object.flags |= UNINTERESTING;
739                 add_pending_object(revs, &it->object, "(merge-base)");
740         }
741
742         if (!active_nr)
743                 read_cache();
744         for (i = 0; i < active_nr; i++) {
745                 struct cache_entry *ce = active_cache[i];
746                 if (!ce_stage(ce))
747                         continue;
748                 if (ce_path_match(ce, revs->prune_data)) {
749                         prune_num++;
750                         prune = xrealloc(prune, sizeof(*prune) * prune_num);
751                         prune[prune_num-2] = ce->name;
752                         prune[prune_num-1] = NULL;
753                 }
754                 while ((i+1 < active_nr) &&
755                        ce_same_name(ce, active_cache[i+1]))
756                         i++;
757         }
758         revs->prune_data = prune;
759 }
760
761 int handle_revision_arg(const char *arg, struct rev_info *revs,
762                         int flags,
763                         int cant_be_filename)
764 {
765         unsigned mode;
766         char *dotdot;
767         struct object *object;
768         unsigned char sha1[20];
769         int local_flags;
770
771         dotdot = strstr(arg, "..");
772         if (dotdot) {
773                 unsigned char from_sha1[20];
774                 const char *next = dotdot + 2;
775                 const char *this = arg;
776                 int symmetric = *next == '.';
777                 unsigned int flags_exclude = flags ^ UNINTERESTING;
778
779                 *dotdot = 0;
780                 next += symmetric;
781
782                 if (!*next)
783                         next = "HEAD";
784                 if (dotdot == arg)
785                         this = "HEAD";
786                 if (!get_sha1(this, from_sha1) &&
787                     !get_sha1(next, sha1)) {
788                         struct commit *a, *b;
789                         struct commit_list *exclude;
790
791                         a = lookup_commit_reference(from_sha1);
792                         b = lookup_commit_reference(sha1);
793                         if (!a || !b) {
794                                 die(symmetric ?
795                                     "Invalid symmetric difference expression %s...%s" :
796                                     "Invalid revision range %s..%s",
797                                     arg, next);
798                         }
799
800                         if (!cant_be_filename) {
801                                 *dotdot = '.';
802                                 verify_non_filename(revs->prefix, arg);
803                         }
804
805                         if (symmetric) {
806                                 exclude = get_merge_bases(a, b, 1);
807                                 add_pending_commit_list(revs, exclude,
808                                                         flags_exclude);
809                                 free_commit_list(exclude);
810                                 a->object.flags |= flags | SYMMETRIC_LEFT;
811                         } else
812                                 a->object.flags |= flags_exclude;
813                         b->object.flags |= flags;
814                         add_pending_object(revs, &a->object, this);
815                         add_pending_object(revs, &b->object, next);
816                         return 0;
817                 }
818                 *dotdot = '.';
819         }
820         dotdot = strstr(arg, "^@");
821         if (dotdot && !dotdot[2]) {
822                 *dotdot = 0;
823                 if (add_parents_only(revs, arg, flags))
824                         return 0;
825                 *dotdot = '^';
826         }
827         dotdot = strstr(arg, "^!");
828         if (dotdot && !dotdot[2]) {
829                 *dotdot = 0;
830                 if (!add_parents_only(revs, arg, flags ^ UNINTERESTING))
831                         *dotdot = '^';
832         }
833
834         local_flags = 0;
835         if (*arg == '^') {
836                 local_flags = UNINTERESTING;
837                 arg++;
838         }
839         if (get_sha1_with_mode(arg, sha1, &mode))
840                 return -1;
841         if (!cant_be_filename)
842                 verify_non_filename(revs->prefix, arg);
843         object = get_reference(revs, arg, sha1, flags ^ local_flags);
844         add_pending_object_with_mode(revs, object, arg, mode);
845         return 0;
846 }
847
848 static void add_grep(struct rev_info *revs, const char *ptn, enum grep_pat_token what)
849 {
850         if (!revs->grep_filter) {
851                 struct grep_opt *opt = xcalloc(1, sizeof(*opt));
852                 opt->status_only = 1;
853                 opt->pattern_tail = &(opt->pattern_list);
854                 opt->regflags = REG_NEWLINE;
855                 revs->grep_filter = opt;
856         }
857         append_grep_pattern(revs->grep_filter, ptn,
858                             "command line", 0, what);
859 }
860
861 static void add_header_grep(struct rev_info *revs, const char *field, const char *pattern)
862 {
863         char *pat;
864         const char *prefix;
865         int patlen, fldlen;
866
867         fldlen = strlen(field);
868         patlen = strlen(pattern);
869         pat = xmalloc(patlen + fldlen + 10);
870         prefix = ".*";
871         if (*pattern == '^') {
872                 prefix = "";
873                 pattern++;
874         }
875         sprintf(pat, "^%s %s%s", field, prefix, pattern);
876         add_grep(revs, pat, GREP_PATTERN_HEAD);
877 }
878
879 static void add_message_grep(struct rev_info *revs, const char *pattern)
880 {
881         add_grep(revs, pattern, GREP_PATTERN_BODY);
882 }
883
884 static void add_ignore_packed(struct rev_info *revs, const char *name)
885 {
886         int num = ++revs->num_ignore_packed;
887
888         revs->ignore_packed = xrealloc(revs->ignore_packed,
889                                        sizeof(const char **) * (num + 1));
890         revs->ignore_packed[num-1] = name;
891         revs->ignore_packed[num] = NULL;
892 }
893
894 /*
895  * Parse revision information, filling in the "rev_info" structure,
896  * and removing the used arguments from the argument list.
897  *
898  * Returns the number of arguments left that weren't recognized
899  * (which are also moved to the head of the argument list)
900  */
901 int setup_revisions(int argc, const char **argv, struct rev_info *revs, const char *def)
902 {
903         int i, flags, seen_dashdash, show_merge;
904         const char **unrecognized = argv + 1;
905         int left = 1;
906         int all_match = 0;
907         int regflags = 0;
908
909         /* First, search for "--" */
910         seen_dashdash = 0;
911         for (i = 1; i < argc; i++) {
912                 const char *arg = argv[i];
913                 if (strcmp(arg, "--"))
914                         continue;
915                 argv[i] = NULL;
916                 argc = i;
917                 if (argv[i + 1])
918                         revs->prune_data = get_pathspec(revs->prefix, argv + i + 1);
919                 seen_dashdash = 1;
920                 break;
921         }
922
923         flags = show_merge = 0;
924         for (i = 1; i < argc; i++) {
925                 const char *arg = argv[i];
926                 if (*arg == '-') {
927                         int opts;
928                         if (!prefixcmp(arg, "--max-count=")) {
929                                 revs->max_count = atoi(arg + 12);
930                                 continue;
931                         }
932                         if (!prefixcmp(arg, "--skip=")) {
933                                 revs->skip_count = atoi(arg + 7);
934                                 continue;
935                         }
936                         /* accept -<digit>, like traditional "head" */
937                         if ((*arg == '-') && isdigit(arg[1])) {
938                                 revs->max_count = atoi(arg + 1);
939                                 continue;
940                         }
941                         if (!strcmp(arg, "-n")) {
942                                 if (argc <= i + 1)
943                                         die("-n requires an argument");
944                                 revs->max_count = atoi(argv[++i]);
945                                 continue;
946                         }
947                         if (!prefixcmp(arg, "-n")) {
948                                 revs->max_count = atoi(arg + 2);
949                                 continue;
950                         }
951                         if (!prefixcmp(arg, "--max-age=")) {
952                                 revs->max_age = atoi(arg + 10);
953                                 continue;
954                         }
955                         if (!prefixcmp(arg, "--since=")) {
956                                 revs->max_age = approxidate(arg + 8);
957                                 continue;
958                         }
959                         if (!prefixcmp(arg, "--after=")) {
960                                 revs->max_age = approxidate(arg + 8);
961                                 continue;
962                         }
963                         if (!prefixcmp(arg, "--min-age=")) {
964                                 revs->min_age = atoi(arg + 10);
965                                 continue;
966                         }
967                         if (!prefixcmp(arg, "--before=")) {
968                                 revs->min_age = approxidate(arg + 9);
969                                 continue;
970                         }
971                         if (!prefixcmp(arg, "--until=")) {
972                                 revs->min_age = approxidate(arg + 8);
973                                 continue;
974                         }
975                         if (!strcmp(arg, "--all")) {
976                                 handle_all(revs, flags);
977                                 continue;
978                         }
979                         if (!strcmp(arg, "--first-parent")) {
980                                 revs->first_parent_only = 1;
981                                 continue;
982                         }
983                         if (!strcmp(arg, "--reflog")) {
984                                 handle_reflog(revs, flags);
985                                 continue;
986                         }
987                         if (!strcmp(arg, "-g") ||
988                                         !strcmp(arg, "--walk-reflogs")) {
989                                 init_reflog_walk(&revs->reflog_info);
990                                 continue;
991                         }
992                         if (!strcmp(arg, "--not")) {
993                                 flags ^= UNINTERESTING;
994                                 continue;
995                         }
996                         if (!strcmp(arg, "--default")) {
997                                 if (++i >= argc)
998                                         die("bad --default argument");
999                                 def = argv[i];
1000                                 continue;
1001                         }
1002                         if (!strcmp(arg, "--merge")) {
1003                                 show_merge = 1;
1004                                 continue;
1005                         }
1006                         if (!strcmp(arg, "--topo-order")) {
1007                                 revs->topo_order = 1;
1008                                 continue;
1009                         }
1010                         if (!strcmp(arg, "--date-order")) {
1011                                 revs->lifo = 0;
1012                                 revs->topo_order = 1;
1013                                 continue;
1014                         }
1015                         if (!prefixcmp(arg, "--early-output")) {
1016                                 int count = 100;
1017                                 switch (arg[14]) {
1018                                 case '=':
1019                                         count = atoi(arg+15);
1020                                         /* Fallthrough */
1021                                 case 0:
1022                                         revs->topo_order = 1;
1023                                         revs->early_output = count;
1024                                         continue;
1025                                 }
1026                         }
1027                         if (!strcmp(arg, "--parents")) {
1028                                 revs->parents = 1;
1029                                 continue;
1030                         }
1031                         if (!strcmp(arg, "--dense")) {
1032                                 revs->dense = 1;
1033                                 continue;
1034                         }
1035                         if (!strcmp(arg, "--sparse")) {
1036                                 revs->dense = 0;
1037                                 continue;
1038                         }
1039                         if (!strcmp(arg, "--remove-empty")) {
1040                                 revs->remove_empty_trees = 1;
1041                                 continue;
1042                         }
1043                         if (!strcmp(arg, "--no-merges")) {
1044                                 revs->no_merges = 1;
1045                                 continue;
1046                         }
1047                         if (!strcmp(arg, "--boundary")) {
1048                                 revs->boundary = 1;
1049                                 continue;
1050                         }
1051                         if (!strcmp(arg, "--left-right")) {
1052                                 revs->left_right = 1;
1053                                 continue;
1054                         }
1055                         if (!strcmp(arg, "--cherry-pick")) {
1056                                 revs->cherry_pick = 1;
1057                                 revs->limited = 1;
1058                                 continue;
1059                         }
1060                         if (!strcmp(arg, "--objects")) {
1061                                 revs->tag_objects = 1;
1062                                 revs->tree_objects = 1;
1063                                 revs->blob_objects = 1;
1064                                 continue;
1065                         }
1066                         if (!strcmp(arg, "--objects-edge")) {
1067                                 revs->tag_objects = 1;
1068                                 revs->tree_objects = 1;
1069                                 revs->blob_objects = 1;
1070                                 revs->edge_hint = 1;
1071                                 continue;
1072                         }
1073                         if (!strcmp(arg, "--unpacked")) {
1074                                 revs->unpacked = 1;
1075                                 free(revs->ignore_packed);
1076                                 revs->ignore_packed = NULL;
1077                                 revs->num_ignore_packed = 0;
1078                                 continue;
1079                         }
1080                         if (!prefixcmp(arg, "--unpacked=")) {
1081                                 revs->unpacked = 1;
1082                                 add_ignore_packed(revs, arg+11);
1083                                 continue;
1084                         }
1085                         if (!strcmp(arg, "-r")) {
1086                                 revs->diff = 1;
1087                                 DIFF_OPT_SET(&revs->diffopt, RECURSIVE);
1088                                 continue;
1089                         }
1090                         if (!strcmp(arg, "-t")) {
1091                                 revs->diff = 1;
1092                                 DIFF_OPT_SET(&revs->diffopt, RECURSIVE);
1093                                 DIFF_OPT_SET(&revs->diffopt, TREE_IN_RECURSIVE);
1094                                 continue;
1095                         }
1096                         if (!strcmp(arg, "-m")) {
1097                                 revs->ignore_merges = 0;
1098                                 continue;
1099                         }
1100                         if (!strcmp(arg, "-c")) {
1101                                 revs->diff = 1;
1102                                 revs->dense_combined_merges = 0;
1103                                 revs->combine_merges = 1;
1104                                 continue;
1105                         }
1106                         if (!strcmp(arg, "--cc")) {
1107                                 revs->diff = 1;
1108                                 revs->dense_combined_merges = 1;
1109                                 revs->combine_merges = 1;
1110                                 continue;
1111                         }
1112                         if (!strcmp(arg, "-v")) {
1113                                 revs->verbose_header = 1;
1114                                 continue;
1115                         }
1116                         if (!prefixcmp(arg, "--pretty")) {
1117                                 revs->verbose_header = 1;
1118                                 revs->commit_format = get_commit_format(arg+8);
1119                                 continue;
1120                         }
1121                         if (!strcmp(arg, "--root")) {
1122                                 revs->show_root_diff = 1;
1123                                 continue;
1124                         }
1125                         if (!strcmp(arg, "--no-commit-id")) {
1126                                 revs->no_commit_id = 1;
1127                                 continue;
1128                         }
1129                         if (!strcmp(arg, "--always")) {
1130                                 revs->always_show_header = 1;
1131                                 continue;
1132                         }
1133                         if (!strcmp(arg, "--no-abbrev")) {
1134                                 revs->abbrev = 0;
1135                                 continue;
1136                         }
1137                         if (!strcmp(arg, "--abbrev")) {
1138                                 revs->abbrev = DEFAULT_ABBREV;
1139                                 continue;
1140                         }
1141                         if (!prefixcmp(arg, "--abbrev=")) {
1142                                 revs->abbrev = strtoul(arg + 9, NULL, 10);
1143                                 if (revs->abbrev < MINIMUM_ABBREV)
1144                                         revs->abbrev = MINIMUM_ABBREV;
1145                                 else if (revs->abbrev > 40)
1146                                         revs->abbrev = 40;
1147                                 continue;
1148                         }
1149                         if (!strcmp(arg, "--abbrev-commit")) {
1150                                 revs->abbrev_commit = 1;
1151                                 continue;
1152                         }
1153                         if (!strcmp(arg, "--full-diff")) {
1154                                 revs->diff = 1;
1155                                 revs->full_diff = 1;
1156                                 continue;
1157                         }
1158                         if (!strcmp(arg, "--full-history")) {
1159                                 revs->simplify_history = 0;
1160                                 continue;
1161                         }
1162                         if (!strcmp(arg, "--relative-date")) {
1163                                 revs->date_mode = DATE_RELATIVE;
1164                                 continue;
1165                         }
1166                         if (!strncmp(arg, "--date=", 7)) {
1167                                 revs->date_mode = parse_date_format(arg + 7);
1168                                 continue;
1169                         }
1170                         if (!strcmp(arg, "--log-size")) {
1171                                 revs->show_log_size = 1;
1172                                 continue;
1173                         }
1174
1175                         /*
1176                          * Grepping the commit log
1177                          */
1178                         if (!prefixcmp(arg, "--author=")) {
1179                                 add_header_grep(revs, "author", arg+9);
1180                                 continue;
1181                         }
1182                         if (!prefixcmp(arg, "--committer=")) {
1183                                 add_header_grep(revs, "committer", arg+12);
1184                                 continue;
1185                         }
1186                         if (!prefixcmp(arg, "--grep=")) {
1187                                 add_message_grep(revs, arg+7);
1188                                 continue;
1189                         }
1190                         if (!strcmp(arg, "--extended-regexp") ||
1191                             !strcmp(arg, "-E")) {
1192                                 regflags |= REG_EXTENDED;
1193                                 continue;
1194                         }
1195                         if (!strcmp(arg, "--regexp-ignore-case") ||
1196                             !strcmp(arg, "-i")) {
1197                                 regflags |= REG_ICASE;
1198                                 continue;
1199                         }
1200                         if (!strcmp(arg, "--all-match")) {
1201                                 all_match = 1;
1202                                 continue;
1203                         }
1204                         if (!prefixcmp(arg, "--encoding=")) {
1205                                 arg += 11;
1206                                 if (strcmp(arg, "none"))
1207                                         git_log_output_encoding = xstrdup(arg);
1208                                 else
1209                                         git_log_output_encoding = "";
1210                                 continue;
1211                         }
1212                         if (!strcmp(arg, "--reverse")) {
1213                                 revs->reverse ^= 1;
1214                                 continue;
1215                         }
1216                         if (!strcmp(arg, "--no-walk")) {
1217                                 revs->no_walk = 1;
1218                                 continue;
1219                         }
1220                         if (!strcmp(arg, "--do-walk")) {
1221                                 revs->no_walk = 0;
1222                                 continue;
1223                         }
1224
1225                         opts = diff_opt_parse(&revs->diffopt, argv+i, argc-i);
1226                         if (opts > 0) {
1227                                 i += opts - 1;
1228                                 continue;
1229                         }
1230                         *unrecognized++ = arg;
1231                         left++;
1232                         continue;
1233                 }
1234
1235                 if (handle_revision_arg(arg, revs, flags, seen_dashdash)) {
1236                         int j;
1237                         if (seen_dashdash || *arg == '^')
1238                                 die("bad revision '%s'", arg);
1239
1240                         /* If we didn't have a "--":
1241                          * (1) all filenames must exist;
1242                          * (2) all rev-args must not be interpretable
1243                          *     as a valid filename.
1244                          * but the latter we have checked in the main loop.
1245                          */
1246                         for (j = i; j < argc; j++)
1247                                 verify_filename(revs->prefix, argv[j]);
1248
1249                         revs->prune_data = get_pathspec(revs->prefix,
1250                                                         argv + i);
1251                         break;
1252                 }
1253         }
1254
1255         if (revs->grep_filter)
1256                 revs->grep_filter->regflags |= regflags;
1257
1258         if (show_merge)
1259                 prepare_show_merge(revs);
1260         if (def && !revs->pending.nr) {
1261                 unsigned char sha1[20];
1262                 struct object *object;
1263                 unsigned mode;
1264                 if (get_sha1_with_mode(def, sha1, &mode))
1265                         die("bad default revision '%s'", def);
1266                 object = get_reference(revs, def, sha1, 0);
1267                 add_pending_object_with_mode(revs, object, def, mode);
1268         }
1269
1270         /* Did the user ask for any diff output? Run the diff! */
1271         if (revs->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT)
1272                 revs->diff = 1;
1273
1274         /* Pickaxe and rename following needs diffs */
1275         if (revs->diffopt.pickaxe || DIFF_OPT_TST(&revs->diffopt, FOLLOW_RENAMES))
1276                 revs->diff = 1;
1277
1278         if (revs->topo_order)
1279                 revs->limited = 1;
1280
1281         if (revs->prune_data) {
1282                 diff_tree_setup_paths(revs->prune_data, &revs->pruning);
1283                 /* Can't prune commits with rename following: the paths change.. */
1284                 if (!DIFF_OPT_TST(&revs->diffopt, FOLLOW_RENAMES))
1285                         revs->prune = 1;
1286                 if (!revs->full_diff)
1287                         diff_tree_setup_paths(revs->prune_data, &revs->diffopt);
1288         }
1289         if (revs->combine_merges) {
1290                 revs->ignore_merges = 0;
1291                 if (revs->dense_combined_merges && !revs->diffopt.output_format)
1292                         revs->diffopt.output_format = DIFF_FORMAT_PATCH;
1293         }
1294         revs->diffopt.abbrev = revs->abbrev;
1295         if (diff_setup_done(&revs->diffopt) < 0)
1296                 die("diff_setup_done failed");
1297
1298         if (revs->grep_filter) {
1299                 revs->grep_filter->all_match = all_match;
1300                 compile_grep_patterns(revs->grep_filter);
1301         }
1302
1303         if (revs->reverse && revs->reflog_info)
1304                 die("cannot combine --reverse with --walk-reflogs");
1305
1306         return left;
1307 }
1308
1309 int prepare_revision_walk(struct rev_info *revs)
1310 {
1311         int nr = revs->pending.nr;
1312         struct object_array_entry *e, *list;
1313
1314         e = list = revs->pending.objects;
1315         revs->pending.nr = 0;
1316         revs->pending.alloc = 0;
1317         revs->pending.objects = NULL;
1318         while (--nr >= 0) {
1319                 struct commit *commit = handle_commit(revs, e->item, e->name);
1320                 if (commit) {
1321                         if (!(commit->object.flags & SEEN)) {
1322                                 commit->object.flags |= SEEN;
1323                                 insert_by_date(commit, &revs->commits);
1324                         }
1325                 }
1326                 e++;
1327         }
1328         free(list);
1329
1330         if (revs->no_walk)
1331                 return 0;
1332         if (revs->limited)
1333                 if (limit_list(revs) < 0)
1334                         return -1;
1335         if (revs->topo_order)
1336                 sort_in_topological_order(&revs->commits, revs->lifo);
1337         return 0;
1338 }
1339
1340 enum rewrite_result {
1341         rewrite_one_ok,
1342         rewrite_one_noparents,
1343         rewrite_one_error,
1344 };
1345
1346 static enum rewrite_result rewrite_one(struct rev_info *revs, struct commit **pp)
1347 {
1348         for (;;) {
1349                 struct commit *p = *pp;
1350                 if (!revs->limited)
1351                         if (add_parents_to_list(revs, p, &revs->commits) < 0)
1352                                 return rewrite_one_error;
1353                 if (p->parents && p->parents->next)
1354                         return rewrite_one_ok;
1355                 if (p->object.flags & UNINTERESTING)
1356                         return rewrite_one_ok;
1357                 if (!(p->object.flags & TREESAME))
1358                         return rewrite_one_ok;
1359                 if (!p->parents)
1360                         return rewrite_one_noparents;
1361                 *pp = p->parents->item;
1362         }
1363 }
1364
1365 static void remove_duplicate_parents(struct commit *commit)
1366 {
1367         struct commit_list **pp, *p;
1368
1369         /* Examine existing parents while marking ones we have seen... */
1370         pp = &commit->parents;
1371         while ((p = *pp) != NULL) {
1372                 struct commit *parent = p->item;
1373                 if (parent->object.flags & TMP_MARK) {
1374                         *pp = p->next;
1375                         continue;
1376                 }
1377                 parent->object.flags |= TMP_MARK;
1378                 pp = &p->next;
1379         }
1380         /* ... and clear the temporary mark */
1381         for (p = commit->parents; p; p = p->next)
1382                 p->item->object.flags &= ~TMP_MARK;
1383 }
1384
1385 static int rewrite_parents(struct rev_info *revs, struct commit *commit)
1386 {
1387         struct commit_list **pp = &commit->parents;
1388         while (*pp) {
1389                 struct commit_list *parent = *pp;
1390                 switch (rewrite_one(revs, &parent->item)) {
1391                 case rewrite_one_ok:
1392                         break;
1393                 case rewrite_one_noparents:
1394                         *pp = parent->next;
1395                         continue;
1396                 case rewrite_one_error:
1397                         return -1;
1398                 }
1399                 pp = &parent->next;
1400         }
1401         remove_duplicate_parents(commit);
1402         return 0;
1403 }
1404
1405 static int commit_match(struct commit *commit, struct rev_info *opt)
1406 {
1407         if (!opt->grep_filter)
1408                 return 1;
1409         return grep_buffer(opt->grep_filter,
1410                            NULL, /* we say nothing, not even filename */
1411                            commit->buffer, strlen(commit->buffer));
1412 }
1413
1414 enum commit_action simplify_commit(struct rev_info *revs, struct commit *commit)
1415 {
1416         if (commit->object.flags & SHOWN)
1417                 return commit_ignore;
1418         if (revs->unpacked && has_sha1_pack(commit->object.sha1, revs->ignore_packed))
1419                 return commit_ignore;
1420         if (commit->object.flags & UNINTERESTING)
1421                 return commit_ignore;
1422         if (revs->min_age != -1 && (commit->date > revs->min_age))
1423                 return commit_ignore;
1424         if (revs->no_merges && commit->parents && commit->parents->next)
1425                 return commit_ignore;
1426         if (!commit_match(commit, revs))
1427                 return commit_ignore;
1428         if (revs->prune && revs->dense) {
1429                 /* Commit without changes? */
1430                 if (commit->object.flags & TREESAME) {
1431                         /* drop merges unless we want parenthood */
1432                         if (!revs->parents)
1433                                 return commit_ignore;
1434                         /* non-merge - always ignore it */
1435                         if (!commit->parents || !commit->parents->next)
1436                                 return commit_ignore;
1437                 }
1438                 if (revs->parents && rewrite_parents(revs, commit) < 0)
1439                         return commit_error;
1440         }
1441         return commit_show;
1442 }
1443
1444 static struct commit *get_revision_1(struct rev_info *revs)
1445 {
1446         if (!revs->commits)
1447                 return NULL;
1448
1449         do {
1450                 struct commit_list *entry = revs->commits;
1451                 struct commit *commit = entry->item;
1452
1453                 revs->commits = entry->next;
1454                 free(entry);
1455
1456                 if (revs->reflog_info)
1457                         fake_reflog_parent(revs->reflog_info, commit);
1458
1459                 /*
1460                  * If we haven't done the list limiting, we need to look at
1461                  * the parents here. We also need to do the date-based limiting
1462                  * that we'd otherwise have done in limit_list().
1463                  */
1464                 if (!revs->limited) {
1465                         if (revs->max_age != -1 &&
1466                             (commit->date < revs->max_age))
1467                                 continue;
1468                         if (add_parents_to_list(revs, commit, &revs->commits) < 0)
1469                                 return NULL;
1470                 }
1471
1472                 switch (simplify_commit(revs, commit)) {
1473                 case commit_ignore:
1474                         continue;
1475                 case commit_error:
1476                         return NULL;
1477                 default:
1478                         return commit;
1479                 }
1480         } while (revs->commits);
1481         return NULL;
1482 }
1483
1484 static void gc_boundary(struct object_array *array)
1485 {
1486         unsigned nr = array->nr;
1487         unsigned alloc = array->alloc;
1488         struct object_array_entry *objects = array->objects;
1489
1490         if (alloc <= nr) {
1491                 unsigned i, j;
1492                 for (i = j = 0; i < nr; i++) {
1493                         if (objects[i].item->flags & SHOWN)
1494                                 continue;
1495                         if (i != j)
1496                                 objects[j] = objects[i];
1497                         j++;
1498                 }
1499                 for (i = j; i < nr; i++)
1500                         objects[i].item = NULL;
1501                 array->nr = j;
1502         }
1503 }
1504
1505 struct commit *get_revision(struct rev_info *revs)
1506 {
1507         struct commit *c = NULL;
1508         struct commit_list *l;
1509
1510         if (revs->boundary == 2) {
1511                 unsigned i;
1512                 struct object_array *array = &revs->boundary_commits;
1513                 struct object_array_entry *objects = array->objects;
1514                 for (i = 0; i < array->nr; i++) {
1515                         c = (struct commit *)(objects[i].item);
1516                         if (!c)
1517                                 continue;
1518                         if (!(c->object.flags & CHILD_SHOWN))
1519                                 continue;
1520                         if (!(c->object.flags & SHOWN))
1521                                 break;
1522                 }
1523                 if (array->nr <= i)
1524                         return NULL;
1525
1526                 c->object.flags |= SHOWN | BOUNDARY;
1527                 return c;
1528         }
1529
1530         if (revs->reverse) {
1531                 int limit = -1;
1532
1533                 if (0 <= revs->max_count) {
1534                         limit = revs->max_count;
1535                         if (0 < revs->skip_count)
1536                                 limit += revs->skip_count;
1537                 }
1538                 l = NULL;
1539                 while ((c = get_revision_1(revs))) {
1540                         commit_list_insert(c, &l);
1541                         if ((0 < limit) && !--limit)
1542                                 break;
1543                 }
1544                 revs->commits = l;
1545                 revs->reverse = 0;
1546                 revs->max_count = -1;
1547                 c = NULL;
1548         }
1549
1550         /*
1551          * Now pick up what they want to give us
1552          */
1553         c = get_revision_1(revs);
1554         if (c) {
1555                 while (0 < revs->skip_count) {
1556                         revs->skip_count--;
1557                         c = get_revision_1(revs);
1558                         if (!c)
1559                                 break;
1560                 }
1561         }
1562
1563         /*
1564          * Check the max_count.
1565          */
1566         switch (revs->max_count) {
1567         case -1:
1568                 break;
1569         case 0:
1570                 c = NULL;
1571                 break;
1572         default:
1573                 revs->max_count--;
1574         }
1575
1576         if (c)
1577                 c->object.flags |= SHOWN;
1578
1579         if (!revs->boundary) {
1580                 return c;
1581         }
1582
1583         if (!c) {
1584                 /*
1585                  * get_revision_1() runs out the commits, and
1586                  * we are done computing the boundaries.
1587                  * switch to boundary commits output mode.
1588                  */
1589                 revs->boundary = 2;
1590                 return get_revision(revs);
1591         }
1592
1593         /*
1594          * boundary commits are the commits that are parents of the
1595          * ones we got from get_revision_1() but they themselves are
1596          * not returned from get_revision_1().  Before returning
1597          * 'c', we need to mark its parents that they could be boundaries.
1598          */
1599
1600         for (l = c->parents; l; l = l->next) {
1601                 struct object *p;
1602                 p = &(l->item->object);
1603                 if (p->flags & (CHILD_SHOWN | SHOWN))
1604                         continue;
1605                 p->flags |= CHILD_SHOWN;
1606                 gc_boundary(&revs->boundary_commits);
1607                 add_object_array(p, NULL, &revs->boundary_commits);
1608         }
1609
1610         return c;
1611 }