Code

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