full integration of rev-cache into git, completed test suite
[git/spearce.git] / revision.c
blobed21885fbb9fb723e423ce6fcb27ec8210dede2d
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 "rev-cache.h"
17 volatile show_early_output_fn_t show_early_output;
19 char *path_name(const struct name_path *path, const char *name)
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;
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] = '/';
40 return n;
43 void add_object(struct object *obj,
44 struct object_array *p,
45 struct name_path *path,
46 const char *name)
48 add_object_array(obj, path_name(path, name), p);
51 static void mark_blob_uninteresting(struct blob *blob)
53 if (!blob)
54 return;
55 if (blob->object.flags & UNINTERESTING)
56 return;
57 blob->object.flags |= UNINTERESTING;
60 void mark_tree_uninteresting(struct tree *tree)
62 struct tree_desc desc;
63 struct name_entry entry;
64 struct object *obj = &tree->object;
66 if (!tree)
67 return;
68 if (obj->flags & UNINTERESTING)
69 return;
70 obj->flags |= UNINTERESTING;
71 if (!has_sha1_file(obj->sha1))
72 return;
73 if (parse_tree(tree) < 0)
74 die("bad tree %s", sha1_to_hex(obj->sha1));
76 init_tree_desc(&desc, tree->buffer, tree->size);
77 while (tree_entry(&desc, &entry)) {
78 switch (object_type(entry.mode)) {
79 case OBJ_TREE:
80 mark_tree_uninteresting(lookup_tree(entry.sha1));
81 break;
82 case OBJ_BLOB:
83 mark_blob_uninteresting(lookup_blob(entry.sha1));
84 break;
85 default:
86 /* Subproject commit - not in this repository */
87 break;
92 * We don't care about the tree any more
93 * after it has been marked uninteresting.
95 free(tree->buffer);
96 tree->buffer = NULL;
99 void mark_parents_uninteresting(struct commit *commit)
101 struct commit_list *parents = commit->parents;
103 while (parents) {
104 struct commit *commit = parents->item;
105 if (!(commit->object.flags & UNINTERESTING)) {
106 commit->object.flags |= UNINTERESTING;
109 * Normally we haven't parsed the parent
110 * yet, so we won't have a parent of a parent
111 * here. However, it may turn out that we've
112 * reached this commit some other way (where it
113 * wasn't uninteresting), in which case we need
114 * to mark its parents recursively too..
116 if (commit->parents)
117 mark_parents_uninteresting(commit);
121 * A missing commit is ok iff its parent is marked
122 * uninteresting.
124 * We just mark such a thing parsed, so that when
125 * it is popped next time around, we won't be trying
126 * to parse it and get an error.
128 if (!has_sha1_file(commit->object.sha1))
129 commit->object.parsed = 1;
130 parents = parents->next;
134 static void add_pending_object_with_mode(struct rev_info *revs, struct object *obj, const char *name, unsigned mode)
136 if (revs->no_walk && (obj->flags & UNINTERESTING))
137 revs->no_walk = 0;
138 if (revs->reflog_info && obj->type == OBJ_COMMIT &&
139 add_reflog_for_walk(revs->reflog_info,
140 (struct commit *)obj, name))
141 return;
142 add_object_array_with_mode(obj, name, &revs->pending, mode);
145 void add_pending_object(struct rev_info *revs, struct object *obj, const char *name)
147 add_pending_object_with_mode(revs, obj, name, S_IFINVALID);
150 void add_head_to_pending(struct rev_info *revs)
152 unsigned char sha1[20];
153 struct object *obj;
154 if (get_sha1("HEAD", sha1))
155 return;
156 obj = parse_object(sha1);
157 if (!obj)
158 return;
159 add_pending_object(revs, obj, "HEAD");
162 static struct object *get_reference(struct rev_info *revs, const char *name, const unsigned char *sha1, unsigned int flags)
164 struct object *object;
166 object = parse_object(sha1);
167 if (!object)
168 die("bad object %s", name);
169 object->flags |= flags;
170 return object;
173 static struct commit *handle_commit(struct rev_info *revs, struct object *object, const char *name)
175 unsigned long flags = object->flags;
178 * Tag object? Look what it points to..
180 while (object->type == OBJ_TAG) {
181 struct tag *tag = (struct tag *) object;
182 if (revs->tag_objects && !(flags & UNINTERESTING))
183 add_pending_object(revs, object, tag->tag);
184 if (!tag->tagged)
185 die("bad tag");
186 object = parse_object(tag->tagged->sha1);
187 if (!object) {
188 if (flags & UNINTERESTING)
189 return NULL;
190 die("bad object %s", sha1_to_hex(tag->tagged->sha1));
195 * Commit object? Just return it, we'll do all the complex
196 * reachability crud.
198 if (object->type == OBJ_COMMIT) {
199 struct commit *commit = (struct commit *)object;
200 if (parse_commit(commit) < 0)
201 die("unable to parse commit %s", name);
202 if (flags & UNINTERESTING) {
203 commit->object.flags |= UNINTERESTING;
204 mark_parents_uninteresting(commit);
205 revs->limited = 1;
207 if (revs->show_source && !commit->util)
208 commit->util = (void *) name;
209 return commit;
213 * Tree object? Either mark it uninteresting, or add it
214 * to the list of objects to look at later..
216 if (object->type == OBJ_TREE) {
217 struct tree *tree = (struct tree *)object;
218 if (!revs->tree_objects)
219 return NULL;
220 if (flags & UNINTERESTING) {
221 mark_tree_uninteresting(tree);
222 return NULL;
224 add_pending_object(revs, object, "");
225 return NULL;
229 * Blob object? You know the drill by now..
231 if (object->type == OBJ_BLOB) {
232 struct blob *blob = (struct blob *)object;
233 if (!revs->blob_objects)
234 return NULL;
235 if (flags & UNINTERESTING) {
236 mark_blob_uninteresting(blob);
237 return NULL;
239 add_pending_object(revs, object, "");
240 return NULL;
242 die("%s is unknown object", name);
245 static int everybody_uninteresting(struct commit_list *orig)
247 struct commit_list *list = orig;
248 while (list) {
249 struct commit *commit = list->item;
250 list = list->next;
251 if (commit->object.flags & UNINTERESTING)
252 continue;
253 return 0;
255 return 1;
259 * The goal is to get REV_TREE_NEW as the result only if the
260 * diff consists of all '+' (and no other changes), REV_TREE_OLD
261 * if the whole diff is removal of old data, and otherwise
262 * REV_TREE_DIFFERENT (of course if the trees are the same we
263 * want REV_TREE_SAME).
264 * That means that once we get to REV_TREE_DIFFERENT, we do not
265 * have to look any further.
267 static int tree_difference = REV_TREE_SAME;
269 static void file_add_remove(struct diff_options *options,
270 int addremove, unsigned mode,
271 const unsigned char *sha1,
272 const char *fullpath)
274 int diff = addremove == '+' ? REV_TREE_NEW : REV_TREE_OLD;
276 tree_difference |= diff;
277 if (tree_difference == REV_TREE_DIFFERENT)
278 DIFF_OPT_SET(options, HAS_CHANGES);
281 static void file_change(struct diff_options *options,
282 unsigned old_mode, unsigned new_mode,
283 const unsigned char *old_sha1,
284 const unsigned char *new_sha1,
285 const char *fullpath)
287 tree_difference = REV_TREE_DIFFERENT;
288 DIFF_OPT_SET(options, HAS_CHANGES);
291 static int rev_compare_tree(struct rev_info *revs, struct commit *parent, struct commit *commit)
293 struct tree *t1 = parent->tree;
294 struct tree *t2 = commit->tree;
296 if (!t1)
297 return REV_TREE_NEW;
298 if (!t2)
299 return REV_TREE_OLD;
301 if (revs->simplify_by_decoration) {
303 * If we are simplifying by decoration, then the commit
304 * is worth showing if it has a tag pointing at it.
306 if (lookup_decoration(&name_decoration, &commit->object))
307 return REV_TREE_DIFFERENT;
309 * A commit that is not pointed by a tag is uninteresting
310 * if we are not limited by path. This means that you will
311 * see the usual "commits that touch the paths" plus any
312 * tagged commit by specifying both --simplify-by-decoration
313 * and pathspec.
315 if (!revs->prune_data)
316 return REV_TREE_SAME;
319 tree_difference = REV_TREE_SAME;
320 DIFF_OPT_CLR(&revs->pruning, HAS_CHANGES);
321 if (diff_tree_sha1(t1->object.sha1, t2->object.sha1, "",
322 &revs->pruning) < 0)
323 return REV_TREE_DIFFERENT;
324 return tree_difference;
327 static int rev_same_tree_as_empty(struct rev_info *revs, struct commit *commit)
329 int retval;
330 void *tree;
331 unsigned long size;
332 struct tree_desc empty, real;
333 struct tree *t1 = commit->tree;
335 if (!t1)
336 return 0;
338 tree = read_object_with_reference(t1->object.sha1, tree_type, &size, NULL);
339 if (!tree)
340 return 0;
341 init_tree_desc(&real, tree, size);
342 init_tree_desc(&empty, "", 0);
344 tree_difference = REV_TREE_SAME;
345 DIFF_OPT_CLR(&revs->pruning, HAS_CHANGES);
346 retval = diff_tree(&empty, &real, "", &revs->pruning);
347 free(tree);
349 return retval >= 0 && (tree_difference == REV_TREE_SAME);
352 static void try_to_simplify_commit(struct rev_info *revs, struct commit *commit)
354 struct commit_list **pp, *parent;
355 int tree_changed = 0, tree_same = 0;
358 * If we don't do pruning, everything is interesting
360 if (!revs->prune)
361 return;
363 if (!commit->tree)
364 return;
366 if (!commit->parents) {
367 if (rev_same_tree_as_empty(revs, commit))
368 commit->object.flags |= TREESAME;
369 return;
373 * Normal non-merge commit? If we don't want to make the
374 * history dense, we consider it always to be a change..
376 if (!revs->dense && !commit->parents->next)
377 return;
379 pp = &commit->parents;
380 while ((parent = *pp) != NULL) {
381 struct commit *p = parent->item;
383 if (parse_commit(p) < 0)
384 die("cannot simplify commit %s (because of %s)",
385 sha1_to_hex(commit->object.sha1),
386 sha1_to_hex(p->object.sha1));
387 switch (rev_compare_tree(revs, p, commit)) {
388 case REV_TREE_SAME:
389 tree_same = 1;
390 if (!revs->simplify_history || (p->object.flags & UNINTERESTING)) {
391 /* Even if a merge with an uninteresting
392 * side branch brought the entire change
393 * we are interested in, we do not want
394 * to lose the other branches of this
395 * merge, so we just keep going.
397 pp = &parent->next;
398 continue;
400 parent->next = NULL;
401 commit->parents = parent;
402 commit->object.flags |= TREESAME;
403 return;
405 case REV_TREE_NEW:
406 if (revs->remove_empty_trees &&
407 rev_same_tree_as_empty(revs, p)) {
408 /* We are adding all the specified
409 * paths from this parent, so the
410 * history beyond this parent is not
411 * interesting. Remove its parents
412 * (they are grandparents for us).
413 * IOW, we pretend this parent is a
414 * "root" commit.
416 if (parse_commit(p) < 0)
417 die("cannot simplify commit %s (invalid %s)",
418 sha1_to_hex(commit->object.sha1),
419 sha1_to_hex(p->object.sha1));
420 p->parents = NULL;
422 /* fallthrough */
423 case REV_TREE_OLD:
424 case REV_TREE_DIFFERENT:
425 tree_changed = 1;
426 pp = &parent->next;
427 continue;
429 die("bad tree compare for commit %s", sha1_to_hex(commit->object.sha1));
431 if (tree_changed && !tree_same)
432 return;
433 commit->object.flags |= TREESAME;
436 void insert_by_date_cached(struct commit *p, struct commit_list **head,
437 struct commit_list *cached_base, struct commit_list **cache)
439 struct commit_list *new_entry;
441 if (cached_base && p->date < cached_base->item->date)
442 new_entry = insert_by_date(p, &cached_base->next);
443 else
444 new_entry = insert_by_date(p, head);
446 if (cache && (!*cache || p->date < (*cache)->item->date))
447 *cache = new_entry;
450 static int add_parents_to_list(struct rev_info *revs, struct commit *commit,
451 struct commit_list **list, struct commit_list **cache_ptr)
453 struct commit_list *parent = commit->parents;
454 unsigned left_flag;
455 struct commit_list *cached_base = cache_ptr ? *cache_ptr : NULL;
457 if (commit->object.flags & ADDED)
458 return 0;
459 commit->object.flags |= ADDED;
462 * If the commit is uninteresting, don't try to
463 * prune parents - we want the maximal uninteresting
464 * set.
466 * Normally we haven't parsed the parent
467 * yet, so we won't have a parent of a parent
468 * here. However, it may turn out that we've
469 * reached this commit some other way (where it
470 * wasn't uninteresting), in which case we need
471 * to mark its parents recursively too..
473 if (commit->object.flags & UNINTERESTING) {
474 while (parent) {
475 struct commit *p = parent->item;
476 parent = parent->next;
477 if (p)
478 p->object.flags |= UNINTERESTING;
479 if (parse_commit(p) < 0)
480 continue;
481 if (p->parents)
482 mark_parents_uninteresting(p);
483 if (p->object.flags & SEEN)
484 continue;
485 p->object.flags |= SEEN;
486 insert_by_date_cached(p, list, cached_base, cache_ptr);
488 return 0;
492 * Ok, the commit wasn't uninteresting. Try to
493 * simplify the commit history and find the parent
494 * that has no differences in the path set if one exists.
496 try_to_simplify_commit(revs, commit);
498 if (revs->no_walk)
499 return 0;
501 left_flag = (commit->object.flags & SYMMETRIC_LEFT);
503 for (parent = commit->parents; parent; parent = parent->next) {
504 struct commit *p = parent->item;
506 if (parse_commit(p) < 0)
507 return -1;
508 if (revs->show_source && !p->util)
509 p->util = commit->util;
510 p->object.flags |= left_flag;
511 if (!(p->object.flags & SEEN)) {
512 p->object.flags |= SEEN;
513 insert_by_date_cached(p, list, cached_base, cache_ptr);
515 if (revs->first_parent_only)
516 break;
518 return 0;
521 static void cherry_pick_list(struct commit_list *list, struct rev_info *revs)
523 struct commit_list *p;
524 int left_count = 0, right_count = 0;
525 int left_first;
526 struct patch_ids ids;
528 /* First count the commits on the left and on the right */
529 for (p = list; p; p = p->next) {
530 struct commit *commit = p->item;
531 unsigned flags = commit->object.flags;
532 if (flags & BOUNDARY)
534 else if (flags & SYMMETRIC_LEFT)
535 left_count++;
536 else
537 right_count++;
540 left_first = left_count < right_count;
541 init_patch_ids(&ids);
542 if (revs->diffopt.nr_paths) {
543 ids.diffopts.nr_paths = revs->diffopt.nr_paths;
544 ids.diffopts.paths = revs->diffopt.paths;
545 ids.diffopts.pathlens = revs->diffopt.pathlens;
548 /* Compute patch-ids for one side */
549 for (p = list; p; p = p->next) {
550 struct commit *commit = p->item;
551 unsigned flags = commit->object.flags;
553 if (flags & BOUNDARY)
554 continue;
556 * If we have fewer left, left_first is set and we omit
557 * commits on the right branch in this loop. If we have
558 * fewer right, we skip the left ones.
560 if (left_first != !!(flags & SYMMETRIC_LEFT))
561 continue;
562 commit->util = add_commit_patch_id(commit, &ids);
565 /* Check the other side */
566 for (p = list; p; p = p->next) {
567 struct commit *commit = p->item;
568 struct patch_id *id;
569 unsigned flags = commit->object.flags;
571 if (flags & BOUNDARY)
572 continue;
574 * If we have fewer left, left_first is set and we omit
575 * commits on the left branch in this loop.
577 if (left_first == !!(flags & SYMMETRIC_LEFT))
578 continue;
581 * Have we seen the same patch id?
583 id = has_commit_patch_id(commit, &ids);
584 if (!id)
585 continue;
586 id->seen = 1;
587 commit->object.flags |= SHOWN;
590 /* Now check the original side for seen ones */
591 for (p = list; p; p = p->next) {
592 struct commit *commit = p->item;
593 struct patch_id *ent;
595 ent = commit->util;
596 if (!ent)
597 continue;
598 if (ent->seen)
599 commit->object.flags |= SHOWN;
600 commit->util = NULL;
603 free_patch_ids(&ids);
606 /* How many extra uninteresting commits we want to see.. */
607 #define SLOP 5
609 static int still_interesting(struct commit_list *src, unsigned long date, int slop)
612 * No source list at all? We're definitely done..
614 if (!src)
615 return 0;
618 * Does the destination list contain entries with a date
619 * before the source list? Definitely _not_ done.
621 if (date < src->item->date)
622 return SLOP;
625 * Does the source list still have interesting commits in
626 * it? Definitely not done..
628 if (!everybody_uninteresting(src))
629 return SLOP;
631 /* Ok, we're closing in.. */
632 return slop-1;
635 static int limit_list(struct rev_info *revs)
637 int slop = SLOP;
638 unsigned long date = ~0ul;
639 struct commit_list *list = revs->commits;
640 struct commit_list *newlist = NULL;
641 struct commit_list **p = &newlist;
642 unsigned char *cache_sha1;
643 char used_cache;
645 while (list) {
646 struct commit_list *entry = list;
647 struct commit *commit = list->item;
648 struct object *obj = &commit->object;
649 show_early_output_fn_t show;
651 list = list->next;
652 free(entry);
654 if (revs->max_age != -1 && (commit->date < revs->max_age))
655 obj->flags |= UNINTERESTING;
657 /* rev-cache to the rescue!!! */
658 used_cache = 0;
659 if (!revs->dont_cache_me && !(obj->flags & ADDED)) {
660 cache_sha1 = get_cache_slice(commit);
661 if (cache_sha1) {
662 if (traverse_cache_slice(revs, cache_sha1, commit, &date, &slop, &p, &list) < 0)
663 used_cache = 0;
664 else
665 used_cache = 1;
669 if (!used_cache) {
670 if (add_parents_to_list(revs, commit, &list, NULL) < 0)
671 return -1;
672 if (obj->flags & UNINTERESTING) {
673 mark_parents_uninteresting(commit); /* ME: why? */
674 if (revs->show_all)
675 p = &commit_list_insert(commit, p)->next;
676 slop = still_interesting(list, date, slop);
677 if (slop > 0)
678 continue;
679 /* If showing all, add the whole pending list to the end */
680 if (revs->show_all)
681 *p = list;
682 break;
684 if (revs->min_age != -1 && (commit->date > revs->min_age))
685 continue;
686 date = commit->date;
687 p = &commit_list_insert(commit, p)->next;
690 show = show_early_output;
691 if (!show)
692 continue;
694 show(revs, newlist);
695 show_early_output = NULL;
697 if (revs->cherry_pick)
698 cherry_pick_list(newlist, revs);
700 revs->commits = newlist;
701 return 0;
704 struct all_refs_cb {
705 int all_flags;
706 int warned_bad_reflog;
707 struct rev_info *all_revs;
708 const char *name_for_errormsg;
711 static int handle_one_ref(const char *path, const unsigned char *sha1, int flag, void *cb_data)
713 struct all_refs_cb *cb = cb_data;
714 struct object *object = get_reference(cb->all_revs, path, sha1,
715 cb->all_flags);
716 add_pending_object(cb->all_revs, object, path);
717 return 0;
720 static void handle_refs(struct rev_info *revs, unsigned flags,
721 int (*for_each)(each_ref_fn, void *))
723 struct all_refs_cb cb;
724 cb.all_revs = revs;
725 cb.all_flags = flags;
726 for_each(handle_one_ref, &cb);
729 static void handle_one_reflog_commit(unsigned char *sha1, void *cb_data)
731 struct all_refs_cb *cb = cb_data;
732 if (!is_null_sha1(sha1)) {
733 struct object *o = parse_object(sha1);
734 if (o) {
735 o->flags |= cb->all_flags;
736 add_pending_object(cb->all_revs, o, "");
738 else if (!cb->warned_bad_reflog) {
739 warning("reflog of '%s' references pruned commits",
740 cb->name_for_errormsg);
741 cb->warned_bad_reflog = 1;
746 static int handle_one_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
747 const char *email, unsigned long timestamp, int tz,
748 const char *message, void *cb_data)
750 handle_one_reflog_commit(osha1, cb_data);
751 handle_one_reflog_commit(nsha1, cb_data);
752 return 0;
755 static int handle_one_reflog(const char *path, const unsigned char *sha1, int flag, void *cb_data)
757 struct all_refs_cb *cb = cb_data;
758 cb->warned_bad_reflog = 0;
759 cb->name_for_errormsg = path;
760 for_each_reflog_ent(path, handle_one_reflog_ent, cb_data);
761 return 0;
764 static void handle_reflog(struct rev_info *revs, unsigned flags)
766 struct all_refs_cb cb;
767 cb.all_revs = revs;
768 cb.all_flags = flags;
769 for_each_reflog(handle_one_reflog, &cb);
772 static int add_parents_only(struct rev_info *revs, const char *arg, int flags)
774 unsigned char sha1[20];
775 struct object *it;
776 struct commit *commit;
777 struct commit_list *parents;
779 if (*arg == '^') {
780 flags ^= UNINTERESTING;
781 arg++;
783 if (get_sha1(arg, sha1))
784 return 0;
785 while (1) {
786 it = get_reference(revs, arg, sha1, 0);
787 if (it->type != OBJ_TAG)
788 break;
789 if (!((struct tag*)it)->tagged)
790 return 0;
791 hashcpy(sha1, ((struct tag*)it)->tagged->sha1);
793 if (it->type != OBJ_COMMIT)
794 return 0;
795 commit = (struct commit *)it;
796 for (parents = commit->parents; parents; parents = parents->next) {
797 it = &parents->item->object;
798 it->flags |= flags;
799 add_pending_object(revs, it, arg);
801 return 1;
804 void init_revisions(struct rev_info *revs, const char *prefix)
806 memset(revs, 0, sizeof(*revs));
808 revs->abbrev = DEFAULT_ABBREV;
809 revs->ignore_merges = 1;
810 revs->simplify_history = 1;
811 DIFF_OPT_SET(&revs->pruning, RECURSIVE);
812 DIFF_OPT_SET(&revs->pruning, QUIET);
813 revs->pruning.add_remove = file_add_remove;
814 revs->pruning.change = file_change;
815 revs->lifo = 1;
816 revs->dense = 1;
817 revs->prefix = prefix;
818 revs->max_age = -1;
819 revs->min_age = -1;
820 revs->skip_count = -1;
821 revs->max_count = -1;
823 revs->commit_format = CMIT_FMT_DEFAULT;
825 revs->grep_filter.status_only = 1;
826 revs->grep_filter.pattern_tail = &(revs->grep_filter.pattern_list);
827 revs->grep_filter.regflags = REG_NEWLINE;
829 diff_setup(&revs->diffopt);
830 if (prefix && !revs->diffopt.prefix) {
831 revs->diffopt.prefix = prefix;
832 revs->diffopt.prefix_length = strlen(prefix);
835 init_rev_cache_info(&revs->rev_cache_info);
838 static void add_pending_commit_list(struct rev_info *revs,
839 struct commit_list *commit_list,
840 unsigned int flags)
842 while (commit_list) {
843 struct object *object = &commit_list->item->object;
844 object->flags |= flags;
845 add_pending_object(revs, object, sha1_to_hex(object->sha1));
846 commit_list = commit_list->next;
850 static void prepare_show_merge(struct rev_info *revs)
852 struct commit_list *bases;
853 struct commit *head, *other;
854 unsigned char sha1[20];
855 const char **prune = NULL;
856 int i, prune_num = 1; /* counting terminating NULL */
858 if (get_sha1("HEAD", sha1) || !(head = lookup_commit(sha1)))
859 die("--merge without HEAD?");
860 if (get_sha1("MERGE_HEAD", sha1) || !(other = lookup_commit(sha1)))
861 die("--merge without MERGE_HEAD?");
862 add_pending_object(revs, &head->object, "HEAD");
863 add_pending_object(revs, &other->object, "MERGE_HEAD");
864 bases = get_merge_bases(head, other, 1);
865 add_pending_commit_list(revs, bases, UNINTERESTING);
866 free_commit_list(bases);
867 head->object.flags |= SYMMETRIC_LEFT;
869 if (!active_nr)
870 read_cache();
871 for (i = 0; i < active_nr; i++) {
872 struct cache_entry *ce = active_cache[i];
873 if (!ce_stage(ce))
874 continue;
875 if (ce_path_match(ce, revs->prune_data)) {
876 prune_num++;
877 prune = xrealloc(prune, sizeof(*prune) * prune_num);
878 prune[prune_num-2] = ce->name;
879 prune[prune_num-1] = NULL;
881 while ((i+1 < active_nr) &&
882 ce_same_name(ce, active_cache[i+1]))
883 i++;
885 revs->prune_data = prune;
886 revs->limited = 1;
889 int handle_revision_arg(const char *arg, struct rev_info *revs,
890 int flags,
891 int cant_be_filename)
893 unsigned mode;
894 char *dotdot;
895 struct object *object;
896 unsigned char sha1[20];
897 int local_flags;
899 dotdot = strstr(arg, "..");
900 if (dotdot) {
901 unsigned char from_sha1[20];
902 const char *next = dotdot + 2;
903 const char *this = arg;
904 int symmetric = *next == '.';
905 unsigned int flags_exclude = flags ^ UNINTERESTING;
907 *dotdot = 0;
908 next += symmetric;
910 if (!*next)
911 next = "HEAD";
912 if (dotdot == arg)
913 this = "HEAD";
914 if (!get_sha1(this, from_sha1) &&
915 !get_sha1(next, sha1)) {
916 struct commit *a, *b;
917 struct commit_list *exclude;
919 a = lookup_commit_reference(from_sha1);
920 b = lookup_commit_reference(sha1);
921 if (!a || !b) {
922 die(symmetric ?
923 "Invalid symmetric difference expression %s...%s" :
924 "Invalid revision range %s..%s",
925 arg, next);
928 if (!cant_be_filename) {
929 *dotdot = '.';
930 verify_non_filename(revs->prefix, arg);
933 if (symmetric) {
934 exclude = get_merge_bases(a, b, 1);
935 add_pending_commit_list(revs, exclude,
936 flags_exclude);
937 free_commit_list(exclude);
938 a->object.flags |= flags | SYMMETRIC_LEFT;
939 } else
940 a->object.flags |= flags_exclude;
941 b->object.flags |= flags;
942 add_pending_object(revs, &a->object, this);
943 add_pending_object(revs, &b->object, next);
944 return 0;
946 *dotdot = '.';
948 dotdot = strstr(arg, "^@");
949 if (dotdot && !dotdot[2]) {
950 *dotdot = 0;
951 if (add_parents_only(revs, arg, flags))
952 return 0;
953 *dotdot = '^';
955 dotdot = strstr(arg, "^!");
956 if (dotdot && !dotdot[2]) {
957 *dotdot = 0;
958 if (!add_parents_only(revs, arg, flags ^ UNINTERESTING))
959 *dotdot = '^';
962 local_flags = 0;
963 if (*arg == '^') {
964 local_flags = UNINTERESTING;
965 arg++;
967 if (get_sha1_with_mode(arg, sha1, &mode))
968 return -1;
969 if (!cant_be_filename)
970 verify_non_filename(revs->prefix, arg);
971 object = get_reference(revs, arg, sha1, flags ^ local_flags);
972 add_pending_object_with_mode(revs, object, arg, mode);
973 return 0;
976 void read_revisions_from_stdin(struct rev_info *revs)
978 char line[1000];
980 while (fgets(line, sizeof(line), stdin) != NULL) {
981 int len = strlen(line);
982 if (len && line[len - 1] == '\n')
983 line[--len] = '\0';
984 if (!len)
985 break;
986 if (line[0] == '-')
987 die("options not supported in --stdin mode");
988 if (handle_revision_arg(line, revs, 0, 1))
989 die("bad revision '%s'", line);
993 static void add_grep(struct rev_info *revs, const char *ptn, enum grep_pat_token what)
995 append_grep_pattern(&revs->grep_filter, ptn, "command line", 0, what);
998 static void add_header_grep(struct rev_info *revs, enum grep_header_field field, const char *pattern)
1000 append_header_grep_pattern(&revs->grep_filter, field, pattern);
1003 static void add_message_grep(struct rev_info *revs, const char *pattern)
1005 add_grep(revs, pattern, GREP_PATTERN_BODY);
1008 static int handle_revision_opt(struct rev_info *revs, int argc, const char **argv,
1009 int *unkc, const char **unkv)
1011 const char *arg = argv[0];
1013 /* pseudo revision arguments */
1014 if (!strcmp(arg, "--all") || !strcmp(arg, "--branches") ||
1015 !strcmp(arg, "--tags") || !strcmp(arg, "--remotes") ||
1016 !strcmp(arg, "--reflog") || !strcmp(arg, "--not") ||
1017 !strcmp(arg, "--no-walk") || !strcmp(arg, "--do-walk"))
1019 unkv[(*unkc)++] = arg;
1020 return 1;
1023 if (!prefixcmp(arg, "--max-count=")) {
1024 revs->max_count = atoi(arg + 12);
1025 } else if (!prefixcmp(arg, "--skip=")) {
1026 revs->skip_count = atoi(arg + 7);
1027 } else if ((*arg == '-') && isdigit(arg[1])) {
1028 /* accept -<digit>, like traditional "head" */
1029 revs->max_count = atoi(arg + 1);
1030 } else if (!strcmp(arg, "-n")) {
1031 if (argc <= 1)
1032 return error("-n requires an argument");
1033 revs->max_count = atoi(argv[1]);
1034 return 2;
1035 } else if (!prefixcmp(arg, "-n")) {
1036 revs->max_count = atoi(arg + 2);
1037 } else if (!prefixcmp(arg, "--max-age=")) {
1038 revs->max_age = atoi(arg + 10);
1039 } else if (!prefixcmp(arg, "--since=")) {
1040 revs->max_age = approxidate(arg + 8);
1041 } else if (!prefixcmp(arg, "--after=")) {
1042 revs->max_age = approxidate(arg + 8);
1043 } else if (!prefixcmp(arg, "--min-age=")) {
1044 revs->min_age = atoi(arg + 10);
1045 } else if (!prefixcmp(arg, "--before=")) {
1046 revs->min_age = approxidate(arg + 9);
1047 } else if (!prefixcmp(arg, "--until=")) {
1048 revs->min_age = approxidate(arg + 8);
1049 } else if (!strcmp(arg, "--first-parent")) {
1050 revs->first_parent_only = 1;
1051 } else if (!strcmp(arg, "-g") || !strcmp(arg, "--walk-reflogs")) {
1052 init_reflog_walk(&revs->reflog_info);
1053 } else if (!strcmp(arg, "--default")) {
1054 if (argc <= 1)
1055 return error("bad --default argument");
1056 revs->def = argv[1];
1057 return 2;
1058 } else if (!strcmp(arg, "--merge")) {
1059 revs->show_merge = 1;
1060 } else if (!strcmp(arg, "--topo-order")) {
1061 revs->lifo = 1;
1062 revs->topo_order = 1;
1063 } else if (!strcmp(arg, "--simplify-merges")) {
1064 revs->simplify_merges = 1;
1065 revs->rewrite_parents = 1;
1066 revs->simplify_history = 0;
1067 revs->limited = 1;
1068 } else if (!strcmp(arg, "--simplify-by-decoration")) {
1069 revs->simplify_merges = 1;
1070 revs->rewrite_parents = 1;
1071 revs->simplify_history = 0;
1072 revs->simplify_by_decoration = 1;
1073 revs->limited = 1;
1074 revs->prune = 1;
1075 load_ref_decorations(DECORATE_SHORT_REFS);
1076 } else if (!strcmp(arg, "--date-order")) {
1077 revs->lifo = 0;
1078 revs->topo_order = 1;
1079 } else if (!prefixcmp(arg, "--early-output")) {
1080 int count = 100;
1081 switch (arg[14]) {
1082 case '=':
1083 count = atoi(arg+15);
1084 /* Fallthrough */
1085 case 0:
1086 revs->topo_order = 1;
1087 revs->early_output = count;
1089 } else if (!strcmp(arg, "--parents")) {
1090 revs->rewrite_parents = 1;
1091 revs->print_parents = 1;
1092 } else if (!strcmp(arg, "--dense")) {
1093 revs->dense = 1;
1094 } else if (!strcmp(arg, "--sparse")) {
1095 revs->dense = 0;
1096 } else if (!strcmp(arg, "--show-all")) {
1097 revs->show_all = 1;
1098 } else if (!strcmp(arg, "--remove-empty")) {
1099 revs->remove_empty_trees = 1;
1100 } else if (!strcmp(arg, "--merges")) {
1101 revs->merges_only = 1;
1102 } else if (!strcmp(arg, "--no-merges")) {
1103 revs->no_merges = 1;
1104 } else if (!strcmp(arg, "--boundary")) {
1105 revs->boundary = 1;
1106 } else if (!strcmp(arg, "--left-right")) {
1107 revs->left_right = 1;
1108 } else if (!strcmp(arg, "--cherry-pick")) {
1109 revs->cherry_pick = 1;
1110 revs->limited = 1;
1111 } else if (!strcmp(arg, "--objects")) {
1112 revs->tag_objects = 1;
1113 revs->tree_objects = 1;
1114 revs->blob_objects = 1;
1115 } else if (!strcmp(arg, "--objects-edge")) {
1116 revs->tag_objects = 1;
1117 revs->tree_objects = 1;
1118 revs->blob_objects = 1;
1119 revs->edge_hint = 1;
1120 } else if (!strcmp(arg, "--unpacked")) {
1121 revs->unpacked = 1;
1122 } else if (!prefixcmp(arg, "--unpacked=")) {
1123 die("--unpacked=<packfile> no longer supported.");
1124 } else if (!strcmp(arg, "-r")) {
1125 revs->diff = 1;
1126 DIFF_OPT_SET(&revs->diffopt, RECURSIVE);
1127 } else if (!strcmp(arg, "-t")) {
1128 revs->diff = 1;
1129 DIFF_OPT_SET(&revs->diffopt, RECURSIVE);
1130 DIFF_OPT_SET(&revs->diffopt, TREE_IN_RECURSIVE);
1131 } else if (!strcmp(arg, "-m")) {
1132 revs->ignore_merges = 0;
1133 } else if (!strcmp(arg, "-c")) {
1134 revs->diff = 1;
1135 revs->dense_combined_merges = 0;
1136 revs->combine_merges = 1;
1137 } else if (!strcmp(arg, "--cc")) {
1138 revs->diff = 1;
1139 revs->dense_combined_merges = 1;
1140 revs->combine_merges = 1;
1141 } else if (!strcmp(arg, "-v")) {
1142 revs->verbose_header = 1;
1143 } else if (!strcmp(arg, "--pretty")) {
1144 revs->verbose_header = 1;
1145 get_commit_format(arg+8, revs);
1146 } else if (!prefixcmp(arg, "--pretty=") || !prefixcmp(arg, "--format=")) {
1147 revs->verbose_header = 1;
1148 get_commit_format(arg+9, revs);
1149 } else if (!strcmp(arg, "--oneline")) {
1150 revs->verbose_header = 1;
1151 get_commit_format("oneline", revs);
1152 revs->abbrev_commit = 1;
1153 } else if (!strcmp(arg, "--graph")) {
1154 revs->topo_order = 1;
1155 revs->rewrite_parents = 1;
1156 revs->graph = graph_init(revs);
1157 } else if (!strcmp(arg, "--root")) {
1158 revs->show_root_diff = 1;
1159 } else if (!strcmp(arg, "--no-commit-id")) {
1160 revs->no_commit_id = 1;
1161 } else if (!strcmp(arg, "--always")) {
1162 revs->always_show_header = 1;
1163 } else if (!strcmp(arg, "--no-abbrev")) {
1164 revs->abbrev = 0;
1165 } else if (!strcmp(arg, "--abbrev")) {
1166 revs->abbrev = DEFAULT_ABBREV;
1167 } else if (!prefixcmp(arg, "--abbrev=")) {
1168 revs->abbrev = strtoul(arg + 9, NULL, 10);
1169 if (revs->abbrev < MINIMUM_ABBREV)
1170 revs->abbrev = MINIMUM_ABBREV;
1171 else if (revs->abbrev > 40)
1172 revs->abbrev = 40;
1173 } else if (!strcmp(arg, "--abbrev-commit")) {
1174 revs->abbrev_commit = 1;
1175 } else if (!strcmp(arg, "--full-diff")) {
1176 revs->diff = 1;
1177 revs->full_diff = 1;
1178 } else if (!strcmp(arg, "--full-history")) {
1179 revs->simplify_history = 0;
1180 } else if (!strcmp(arg, "--relative-date")) {
1181 revs->date_mode = DATE_RELATIVE;
1182 } else if (!strncmp(arg, "--date=", 7)) {
1183 revs->date_mode = parse_date_format(arg + 7);
1184 } else if (!strcmp(arg, "--log-size")) {
1185 revs->show_log_size = 1;
1188 * Grepping the commit log
1190 else if (!prefixcmp(arg, "--author=")) {
1191 add_header_grep(revs, GREP_HEADER_AUTHOR, arg+9);
1192 } else if (!prefixcmp(arg, "--committer=")) {
1193 add_header_grep(revs, GREP_HEADER_COMMITTER, arg+12);
1194 } else if (!prefixcmp(arg, "--grep=")) {
1195 add_message_grep(revs, arg+7);
1196 } else if (!strcmp(arg, "--extended-regexp") || !strcmp(arg, "-E")) {
1197 revs->grep_filter.regflags |= REG_EXTENDED;
1198 } else if (!strcmp(arg, "--regexp-ignore-case") || !strcmp(arg, "-i")) {
1199 revs->grep_filter.regflags |= REG_ICASE;
1200 } else if (!strcmp(arg, "--fixed-strings") || !strcmp(arg, "-F")) {
1201 revs->grep_filter.fixed = 1;
1202 } else if (!strcmp(arg, "--all-match")) {
1203 revs->grep_filter.all_match = 1;
1204 } else if (!prefixcmp(arg, "--encoding=")) {
1205 arg += 11;
1206 if (strcmp(arg, "none"))
1207 git_log_output_encoding = xstrdup(arg);
1208 else
1209 git_log_output_encoding = "";
1210 } else if (!strcmp(arg, "--reverse")) {
1211 revs->reverse ^= 1;
1212 } else if (!strcmp(arg, "--children")) {
1213 revs->children.name = "children";
1214 revs->limited = 1;
1215 } else {
1216 int opts = diff_opt_parse(&revs->diffopt, argv, argc);
1217 if (!opts)
1218 unkv[(*unkc)++] = arg;
1219 return opts;
1222 return 1;
1225 void parse_revision_opt(struct rev_info *revs, struct parse_opt_ctx_t *ctx,
1226 const struct option *options,
1227 const char * const usagestr[])
1229 int n = handle_revision_opt(revs, ctx->argc, ctx->argv,
1230 &ctx->cpidx, ctx->out);
1231 if (n <= 0) {
1232 error("unknown option `%s'", ctx->argv[0]);
1233 usage_with_options(usagestr, options);
1235 ctx->argv += n;
1236 ctx->argc -= n;
1240 * Parse revision information, filling in the "rev_info" structure,
1241 * and removing the used arguments from the argument list.
1243 * Returns the number of arguments left that weren't recognized
1244 * (which are also moved to the head of the argument list)
1246 int setup_revisions(int argc, const char **argv, struct rev_info *revs, const char *def)
1248 int i, flags, left, seen_dashdash;
1250 /* First, search for "--" */
1251 seen_dashdash = 0;
1252 for (i = 1; i < argc; i++) {
1253 const char *arg = argv[i];
1254 if (strcmp(arg, "--"))
1255 continue;
1256 argv[i] = NULL;
1257 argc = i;
1258 if (argv[i + 1])
1259 revs->prune_data = get_pathspec(revs->prefix, argv + i + 1);
1260 seen_dashdash = 1;
1261 break;
1264 /* Second, deal with arguments and options */
1265 flags = 0;
1266 for (left = i = 1; i < argc; i++) {
1267 const char *arg = argv[i];
1268 if (*arg == '-') {
1269 int opts;
1271 if (!strcmp(arg, "--all")) {
1272 handle_refs(revs, flags, for_each_ref);
1273 handle_refs(revs, flags, head_ref);
1274 continue;
1276 if (!strcmp(arg, "--branches")) {
1277 handle_refs(revs, flags, for_each_branch_ref);
1278 continue;
1280 if (!strcmp(arg, "--tags")) {
1281 handle_refs(revs, flags, for_each_tag_ref);
1282 continue;
1284 if (!strcmp(arg, "--remotes")) {
1285 handle_refs(revs, flags, for_each_remote_ref);
1286 continue;
1288 if (!strcmp(arg, "--reflog")) {
1289 handle_reflog(revs, flags);
1290 continue;
1292 if (!strcmp(arg, "--not")) {
1293 flags ^= UNINTERESTING;
1294 continue;
1296 if (!strcmp(arg, "--no-walk")) {
1297 revs->no_walk = 1;
1298 continue;
1300 if (!strcmp(arg, "--do-walk")) {
1301 revs->no_walk = 0;
1302 continue;
1305 opts = handle_revision_opt(revs, argc - i, argv + i, &left, argv);
1306 if (opts > 0) {
1307 i += opts - 1;
1308 continue;
1310 if (opts < 0)
1311 exit(128);
1312 continue;
1315 if (handle_revision_arg(arg, revs, flags, seen_dashdash)) {
1316 int j;
1317 if (seen_dashdash || *arg == '^')
1318 die("bad revision '%s'", arg);
1320 /* If we didn't have a "--":
1321 * (1) all filenames must exist;
1322 * (2) all rev-args must not be interpretable
1323 * as a valid filename.
1324 * but the latter we have checked in the main loop.
1326 for (j = i; j < argc; j++)
1327 verify_filename(revs->prefix, argv[j]);
1329 revs->prune_data = get_pathspec(revs->prefix,
1330 argv + i);
1331 break;
1335 if (revs->def == NULL)
1336 revs->def = def;
1337 if (revs->show_merge)
1338 prepare_show_merge(revs);
1339 if (revs->def && !revs->pending.nr) {
1340 unsigned char sha1[20];
1341 struct object *object;
1342 unsigned mode;
1343 if (get_sha1_with_mode(revs->def, sha1, &mode))
1344 die("bad default revision '%s'", revs->def);
1345 object = get_reference(revs, revs->def, sha1, 0);
1346 add_pending_object_with_mode(revs, object, revs->def, mode);
1349 /* Did the user ask for any diff output? Run the diff! */
1350 if (revs->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT)
1351 revs->diff = 1;
1353 /* Pickaxe, diff-filter and rename following need diffs */
1354 if (revs->diffopt.pickaxe ||
1355 revs->diffopt.filter ||
1356 DIFF_OPT_TST(&revs->diffopt, FOLLOW_RENAMES))
1357 revs->diff = 1;
1359 if (revs->topo_order)
1360 revs->limited = 1;
1362 if (revs->prune_data) {
1363 diff_tree_setup_paths(revs->prune_data, &revs->pruning);
1364 /* Can't prune commits with rename following: the paths change.. */
1365 if (!DIFF_OPT_TST(&revs->diffopt, FOLLOW_RENAMES))
1366 revs->prune = 1;
1367 if (!revs->full_diff)
1368 diff_tree_setup_paths(revs->prune_data, &revs->diffopt);
1370 if (revs->combine_merges) {
1371 revs->ignore_merges = 0;
1372 if (revs->dense_combined_merges && !revs->diffopt.output_format)
1373 revs->diffopt.output_format = DIFF_FORMAT_PATCH;
1375 revs->diffopt.abbrev = revs->abbrev;
1376 if (diff_setup_done(&revs->diffopt) < 0)
1377 die("diff_setup_done failed");
1379 compile_grep_patterns(&revs->grep_filter);
1381 if (revs->reverse && revs->reflog_info)
1382 die("cannot combine --reverse with --walk-reflogs");
1383 if (revs->rewrite_parents && revs->children.name)
1384 die("cannot combine --parents and --children");
1387 * Limitations on the graph functionality
1389 if (revs->reverse && revs->graph)
1390 die("cannot combine --reverse with --graph");
1392 if (revs->reflog_info && revs->graph)
1393 die("cannot combine --walk-reflogs with --graph");
1395 /* limits on caching
1396 * todo: implement this functionality */
1397 if (revs->prune || revs->diff)
1398 revs->dont_cache_me = 1;
1400 return left;
1403 static void add_child(struct rev_info *revs, struct commit *parent, struct commit *child)
1405 struct commit_list *l = xcalloc(1, sizeof(*l));
1407 l->item = child;
1408 l->next = add_decoration(&revs->children, &parent->object, l);
1411 static int remove_duplicate_parents(struct commit *commit)
1413 struct commit_list **pp, *p;
1414 int surviving_parents;
1416 /* Examine existing parents while marking ones we have seen... */
1417 pp = &commit->parents;
1418 while ((p = *pp) != NULL) {
1419 struct commit *parent = p->item;
1420 if (parent->object.flags & TMP_MARK) {
1421 *pp = p->next;
1422 continue;
1424 parent->object.flags |= TMP_MARK;
1425 pp = &p->next;
1427 /* count them while clearing the temporary mark */
1428 surviving_parents = 0;
1429 for (p = commit->parents; p; p = p->next) {
1430 p->item->object.flags &= ~TMP_MARK;
1431 surviving_parents++;
1433 return surviving_parents;
1436 struct merge_simplify_state {
1437 struct commit *simplified;
1440 static struct merge_simplify_state *locate_simplify_state(struct rev_info *revs, struct commit *commit)
1442 struct merge_simplify_state *st;
1444 st = lookup_decoration(&revs->merge_simplification, &commit->object);
1445 if (!st) {
1446 st = xcalloc(1, sizeof(*st));
1447 add_decoration(&revs->merge_simplification, &commit->object, st);
1449 return st;
1452 static struct commit_list **simplify_one(struct rev_info *revs, struct commit *commit, struct commit_list **tail)
1454 struct commit_list *p;
1455 struct merge_simplify_state *st, *pst;
1456 int cnt;
1458 st = locate_simplify_state(revs, commit);
1461 * Have we handled this one?
1463 if (st->simplified)
1464 return tail;
1467 * An UNINTERESTING commit simplifies to itself, so does a
1468 * root commit. We do not rewrite parents of such commit
1469 * anyway.
1471 if ((commit->object.flags & UNINTERESTING) || !commit->parents) {
1472 st->simplified = commit;
1473 return tail;
1477 * Do we know what commit all of our parents should be rewritten to?
1478 * Otherwise we are not ready to rewrite this one yet.
1480 for (cnt = 0, p = commit->parents; p; p = p->next) {
1481 pst = locate_simplify_state(revs, p->item);
1482 if (!pst->simplified) {
1483 tail = &commit_list_insert(p->item, tail)->next;
1484 cnt++;
1487 if (cnt) {
1488 tail = &commit_list_insert(commit, tail)->next;
1489 return tail;
1493 * Rewrite our list of parents.
1495 for (p = commit->parents; p; p = p->next) {
1496 pst = locate_simplify_state(revs, p->item);
1497 p->item = pst->simplified;
1499 cnt = remove_duplicate_parents(commit);
1502 * It is possible that we are a merge and one side branch
1503 * does not have any commit that touches the given paths;
1504 * in such a case, the immediate parents will be rewritten
1505 * to different commits.
1507 * o----X X: the commit we are looking at;
1508 * / / o: a commit that touches the paths;
1509 * ---o----'
1511 * Further reduce the parents by removing redundant parents.
1513 if (1 < cnt) {
1514 struct commit_list *h = reduce_heads(commit->parents);
1515 cnt = commit_list_count(h);
1516 free_commit_list(commit->parents);
1517 commit->parents = h;
1521 * A commit simplifies to itself if it is a root, if it is
1522 * UNINTERESTING, if it touches the given paths, or if it is a
1523 * merge and its parents simplifies to more than one commits
1524 * (the first two cases are already handled at the beginning of
1525 * this function).
1527 * Otherwise, it simplifies to what its sole parent simplifies to.
1529 if (!cnt ||
1530 (commit->object.flags & UNINTERESTING) ||
1531 !(commit->object.flags & TREESAME) ||
1532 (1 < cnt))
1533 st->simplified = commit;
1534 else {
1535 pst = locate_simplify_state(revs, commit->parents->item);
1536 st->simplified = pst->simplified;
1538 return tail;
1541 static void simplify_merges(struct rev_info *revs)
1543 struct commit_list *list;
1544 struct commit_list *yet_to_do, **tail;
1546 if (!revs->topo_order)
1547 sort_in_topological_order(&revs->commits, revs->lifo);
1548 if (!revs->prune)
1549 return;
1551 /* feed the list reversed */
1552 yet_to_do = NULL;
1553 for (list = revs->commits; list; list = list->next)
1554 commit_list_insert(list->item, &yet_to_do);
1555 while (yet_to_do) {
1556 list = yet_to_do;
1557 yet_to_do = NULL;
1558 tail = &yet_to_do;
1559 while (list) {
1560 struct commit *commit = list->item;
1561 struct commit_list *next = list->next;
1562 free(list);
1563 list = next;
1564 tail = simplify_one(revs, commit, tail);
1568 /* clean up the result, removing the simplified ones */
1569 list = revs->commits;
1570 revs->commits = NULL;
1571 tail = &revs->commits;
1572 while (list) {
1573 struct commit *commit = list->item;
1574 struct commit_list *next = list->next;
1575 struct merge_simplify_state *st;
1576 free(list);
1577 list = next;
1578 st = locate_simplify_state(revs, commit);
1579 if (st->simplified == commit)
1580 tail = &commit_list_insert(commit, tail)->next;
1584 static void set_children(struct rev_info *revs)
1586 struct commit_list *l;
1587 for (l = revs->commits; l; l = l->next) {
1588 struct commit *commit = l->item;
1589 struct commit_list *p;
1591 for (p = commit->parents; p; p = p->next)
1592 add_child(revs, p->item, commit);
1596 int prepare_revision_walk(struct rev_info *revs)
1598 int nr = revs->pending.nr;
1599 struct object_array_entry *e, *list;
1601 e = list = revs->pending.objects;
1602 revs->pending.nr = 0;
1603 revs->pending.alloc = 0;
1604 revs->pending.objects = NULL;
1605 while (--nr >= 0) {
1606 struct commit *commit = handle_commit(revs, e->item, e->name);
1607 if (commit) {
1608 if (!(commit->object.flags & SEEN)) {
1609 commit->object.flags |= SEEN;
1610 insert_by_date(commit, &revs->commits);
1613 e++;
1615 free(list);
1617 if (revs->no_walk)
1618 return 0;
1619 if (revs->limited)
1620 if (limit_list(revs) < 0)
1621 return -1;
1622 if (revs->topo_order)
1623 sort_in_topological_order(&revs->commits, revs->lifo);
1624 if (revs->simplify_merges)
1625 simplify_merges(revs);
1626 if (revs->children.name)
1627 set_children(revs);
1628 return 0;
1631 enum rewrite_result {
1632 rewrite_one_ok,
1633 rewrite_one_noparents,
1634 rewrite_one_error,
1637 static enum rewrite_result rewrite_one(struct rev_info *revs, struct commit **pp)
1639 struct commit_list *cache = NULL;
1641 for (;;) {
1642 struct commit *p = *pp;
1643 if (!revs->limited)
1644 if (add_parents_to_list(revs, p, &revs->commits, &cache) < 0)
1645 return rewrite_one_error;
1646 if (p->parents && p->parents->next)
1647 return rewrite_one_ok;
1648 if (p->object.flags & UNINTERESTING)
1649 return rewrite_one_ok;
1650 if (!(p->object.flags & TREESAME))
1651 return rewrite_one_ok;
1652 if (!p->parents)
1653 return rewrite_one_noparents;
1654 *pp = p->parents->item;
1658 static int rewrite_parents(struct rev_info *revs, struct commit *commit)
1660 struct commit_list **pp = &commit->parents;
1661 while (*pp) {
1662 struct commit_list *parent = *pp;
1663 switch (rewrite_one(revs, &parent->item)) {
1664 case rewrite_one_ok:
1665 break;
1666 case rewrite_one_noparents:
1667 *pp = parent->next;
1668 continue;
1669 case rewrite_one_error:
1670 return -1;
1672 pp = &parent->next;
1674 remove_duplicate_parents(commit);
1675 return 0;
1678 static int commit_match(struct commit *commit, struct rev_info *opt)
1680 if (!opt->grep_filter.pattern_list)
1681 return 1;
1682 if (!commit->object.parsed)
1683 parse_commit(commit);
1684 return grep_buffer(&opt->grep_filter,
1685 NULL, /* we say nothing, not even filename */
1686 commit->buffer, strlen(commit->buffer));
1689 static inline int want_ancestry(struct rev_info *revs)
1691 return (revs->rewrite_parents || revs->children.name);
1694 enum commit_action get_commit_action(struct rev_info *revs, struct commit *commit)
1696 if (commit->object.flags & SHOWN)
1697 return commit_ignore;
1698 if (revs->unpacked && has_sha1_pack(commit->object.sha1))
1699 return commit_ignore;
1700 if (revs->show_all)
1701 return commit_show;
1702 if (commit->object.flags & UNINTERESTING)
1703 return commit_ignore;
1704 if (revs->min_age != -1 && (commit->date > revs->min_age))
1705 return commit_ignore;
1706 if (revs->no_merges && commit->parents && commit->parents->next)
1707 return commit_ignore;
1708 if (revs->merges_only && !(commit->parents && commit->parents->next))
1709 return commit_ignore;
1710 if (!commit_match(commit, revs))
1711 return commit_ignore;
1712 if (revs->prune && revs->dense) {
1713 /* Commit without changes? */
1714 if (commit->object.flags & TREESAME) {
1715 /* drop merges unless we want parenthood */
1716 if (!want_ancestry(revs))
1717 return commit_ignore;
1718 /* non-merge - always ignore it */
1719 if (!commit->parents || !commit->parents->next)
1720 return commit_ignore;
1723 return commit_show;
1726 enum commit_action simplify_commit(struct rev_info *revs, struct commit *commit)
1728 enum commit_action action = get_commit_action(revs, commit);
1730 if (action == commit_show &&
1731 !revs->show_all &&
1732 revs->prune && revs->dense && want_ancestry(revs)) {
1733 if (rewrite_parents(revs, commit) < 0)
1734 return commit_error;
1736 return action;
1739 static struct commit *get_revision_1(struct rev_info *revs)
1741 if (!revs->commits)
1742 return NULL;
1744 do {
1745 struct commit_list *entry = revs->commits;
1746 struct commit *commit = entry->item;
1747 struct object *obj = &commit->object;
1749 revs->commits = entry->next;
1750 free(entry);
1752 if (revs->reflog_info)
1753 fake_reflog_parent(revs->reflog_info, commit);
1756 * If we haven't done the list limiting, we need to look at
1757 * the parents here. We also need to do the date-based limiting
1758 * that we'd otherwise have done in limit_list().
1760 if (!revs->limited) {
1761 if (revs->max_age != -1 &&
1762 (commit->date < revs->max_age))
1763 continue;
1765 if (!revs->dont_cache_me) {
1766 struct commit_list *queue = 0, **queuep = &queue;
1767 unsigned char *cache_sha1;
1769 if (obj->flags & ADDED)
1770 goto skip_parenting;
1772 cache_sha1 = get_cache_slice(commit);
1773 if (cache_sha1) {
1774 if (!traverse_cache_slice(revs, cache_sha1, commit, 0, 0, &queuep, &revs->commits)) {
1775 struct commit_list *work = revs->commits;
1777 /* attach queue to end of ->commits */
1778 while (work && work->next)
1779 work = work->next;
1781 if (work)
1782 work->next = queue;
1783 else
1784 revs->commits = queue;
1786 goto skip_parenting;
1791 if (add_parents_to_list(revs, commit, &revs->commits, NULL) < 0)
1792 die("Failed to traverse parents of commit %s",
1793 sha1_to_hex(commit->object.sha1));
1796 skip_parenting:
1797 switch (simplify_commit(revs, commit)) {
1798 case commit_ignore:
1799 continue;
1800 case commit_error:
1801 die("Failed to simplify parents of commit %s",
1802 sha1_to_hex(commit->object.sha1));
1803 default:
1804 return commit;
1806 } while (revs->commits);
1807 return NULL;
1810 static void gc_boundary(struct object_array *array)
1812 unsigned nr = array->nr;
1813 unsigned alloc = array->alloc;
1814 struct object_array_entry *objects = array->objects;
1816 if (alloc <= nr) {
1817 unsigned i, j;
1818 for (i = j = 0; i < nr; i++) {
1819 if (objects[i].item->flags & SHOWN)
1820 continue;
1821 if (i != j)
1822 objects[j] = objects[i];
1823 j++;
1825 for (i = j; i < nr; i++)
1826 objects[i].item = NULL;
1827 array->nr = j;
1831 static void create_boundary_commit_list(struct rev_info *revs)
1833 unsigned i;
1834 struct commit *c;
1835 struct object_array *array = &revs->boundary_commits;
1836 struct object_array_entry *objects = array->objects;
1839 * If revs->commits is non-NULL at this point, an error occurred in
1840 * get_revision_1(). Ignore the error and continue printing the
1841 * boundary commits anyway. (This is what the code has always
1842 * done.)
1844 if (revs->commits) {
1845 free_commit_list(revs->commits);
1846 revs->commits = NULL;
1850 * Put all of the actual boundary commits from revs->boundary_commits
1851 * into revs->commits
1853 for (i = 0; i < array->nr; i++) {
1854 c = (struct commit *)(objects[i].item);
1855 if (!c)
1856 continue;
1857 if (!(c->object.flags & CHILD_SHOWN))
1858 continue;
1859 if (c->object.flags & (SHOWN | BOUNDARY))
1860 continue;
1861 c->object.flags |= BOUNDARY;
1862 commit_list_insert(c, &revs->commits);
1866 * If revs->topo_order is set, sort the boundary commits
1867 * in topological order
1869 sort_in_topological_order(&revs->commits, revs->lifo);
1872 static struct commit *get_revision_internal(struct rev_info *revs)
1874 struct commit *c = NULL;
1875 struct commit_list *l;
1877 if (revs->boundary == 2) {
1879 * All of the normal commits have already been returned,
1880 * and we are now returning boundary commits.
1881 * create_boundary_commit_list() has populated
1882 * revs->commits with the remaining commits to return.
1884 c = pop_commit(&revs->commits);
1885 if (c)
1886 c->object.flags |= SHOWN;
1887 return c;
1891 * Now pick up what they want to give us
1893 c = get_revision_1(revs);
1894 if (c) {
1895 while (0 < revs->skip_count) {
1896 revs->skip_count--;
1897 c = get_revision_1(revs);
1898 if (!c)
1899 break;
1904 * Check the max_count.
1906 switch (revs->max_count) {
1907 case -1:
1908 break;
1909 case 0:
1910 c = NULL;
1911 break;
1912 default:
1913 revs->max_count--;
1916 if (c)
1917 c->object.flags |= SHOWN;
1919 if (!revs->boundary) {
1920 return c;
1923 if (!c) {
1925 * get_revision_1() runs out the commits, and
1926 * we are done computing the boundaries.
1927 * switch to boundary commits output mode.
1929 revs->boundary = 2;
1932 * Update revs->commits to contain the list of
1933 * boundary commits.
1935 create_boundary_commit_list(revs);
1937 return get_revision_internal(revs);
1941 * boundary commits are the commits that are parents of the
1942 * ones we got from get_revision_1() but they themselves are
1943 * not returned from get_revision_1(). Before returning
1944 * 'c', we need to mark its parents that they could be boundaries.
1947 for (l = c->parents; l; l = l->next) {
1948 struct object *p;
1949 p = &(l->item->object);
1950 if (p->flags & (CHILD_SHOWN | SHOWN))
1951 continue;
1952 p->flags |= CHILD_SHOWN;
1953 gc_boundary(&revs->boundary_commits);
1954 add_object_array(p, NULL, &revs->boundary_commits);
1957 return c;
1960 struct commit *get_revision(struct rev_info *revs)
1962 struct commit *c;
1963 struct commit_list *reversed;
1965 if (revs->reverse) {
1966 reversed = NULL;
1967 while ((c = get_revision_internal(revs))) {
1968 commit_list_insert(c, &reversed);
1970 revs->commits = reversed;
1971 revs->reverse = 0;
1972 revs->reverse_output_stage = 1;
1975 if (revs->reverse_output_stage)
1976 return pop_commit(&revs->commits);
1978 c = get_revision_internal(revs);
1979 if (c && revs->graph)
1980 graph_update(revs->graph, c);
1981 return c;