Code

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