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