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