Merge branch 'jn/gitweb-blame' into pu
[git/spearce.git] / revision.c
blob42f5093560e6fc1ba522e9ea1a9c7e4c0f21195c
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, QUICK);
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 revs->date_mode_explicit = 1;
1183 } else if (!strncmp(arg, "--date=", 7)) {
1184 revs->date_mode = parse_date_format(arg + 7);
1185 revs->date_mode_explicit = 1;
1186 } else if (!strcmp(arg, "--log-size")) {
1187 revs->show_log_size = 1;
1190 * Grepping the commit log
1192 else if (!prefixcmp(arg, "--author=")) {
1193 add_header_grep(revs, GREP_HEADER_AUTHOR, arg+9);
1194 } else if (!prefixcmp(arg, "--committer=")) {
1195 add_header_grep(revs, GREP_HEADER_COMMITTER, arg+12);
1196 } else if (!prefixcmp(arg, "--grep=")) {
1197 add_message_grep(revs, arg+7);
1198 } else if (!strcmp(arg, "--extended-regexp") || !strcmp(arg, "-E")) {
1199 revs->grep_filter.regflags |= REG_EXTENDED;
1200 } else if (!strcmp(arg, "--regexp-ignore-case") || !strcmp(arg, "-i")) {
1201 revs->grep_filter.regflags |= REG_ICASE;
1202 } else if (!strcmp(arg, "--fixed-strings") || !strcmp(arg, "-F")) {
1203 revs->grep_filter.fixed = 1;
1204 } else if (!strcmp(arg, "--all-match")) {
1205 revs->grep_filter.all_match = 1;
1206 } else if (!prefixcmp(arg, "--encoding=")) {
1207 arg += 11;
1208 if (strcmp(arg, "none"))
1209 git_log_output_encoding = xstrdup(arg);
1210 else
1211 git_log_output_encoding = "";
1212 } else if (!strcmp(arg, "--reverse")) {
1213 revs->reverse ^= 1;
1214 } else if (!strcmp(arg, "--children")) {
1215 revs->children.name = "children";
1216 revs->limited = 1;
1217 } else {
1218 int opts = diff_opt_parse(&revs->diffopt, argv, argc);
1219 if (!opts)
1220 unkv[(*unkc)++] = arg;
1221 return opts;
1224 return 1;
1227 void parse_revision_opt(struct rev_info *revs, struct parse_opt_ctx_t *ctx,
1228 const struct option *options,
1229 const char * const usagestr[])
1231 int n = handle_revision_opt(revs, ctx->argc, ctx->argv,
1232 &ctx->cpidx, ctx->out);
1233 if (n <= 0) {
1234 error("unknown option `%s'", ctx->argv[0]);
1235 usage_with_options(usagestr, options);
1237 ctx->argv += n;
1238 ctx->argc -= n;
1242 * Parse revision information, filling in the "rev_info" structure,
1243 * and removing the used arguments from the argument list.
1245 * Returns the number of arguments left that weren't recognized
1246 * (which are also moved to the head of the argument list)
1248 int setup_revisions(int argc, const char **argv, struct rev_info *revs, const char *def)
1250 int i, flags, left, seen_dashdash;
1252 /* First, search for "--" */
1253 seen_dashdash = 0;
1254 for (i = 1; i < argc; i++) {
1255 const char *arg = argv[i];
1256 if (strcmp(arg, "--"))
1257 continue;
1258 argv[i] = NULL;
1259 argc = i;
1260 if (argv[i + 1])
1261 revs->prune_data = get_pathspec(revs->prefix, argv + i + 1);
1262 seen_dashdash = 1;
1263 break;
1266 /* Second, deal with arguments and options */
1267 flags = 0;
1268 for (left = i = 1; i < argc; i++) {
1269 const char *arg = argv[i];
1270 if (*arg == '-') {
1271 int opts;
1273 if (!strcmp(arg, "--all")) {
1274 handle_refs(revs, flags, for_each_ref);
1275 handle_refs(revs, flags, head_ref);
1276 continue;
1278 if (!strcmp(arg, "--branches")) {
1279 handle_refs(revs, flags, for_each_branch_ref);
1280 continue;
1282 if (!strcmp(arg, "--tags")) {
1283 handle_refs(revs, flags, for_each_tag_ref);
1284 continue;
1286 if (!strcmp(arg, "--remotes")) {
1287 handle_refs(revs, flags, for_each_remote_ref);
1288 continue;
1290 if (!strcmp(arg, "--reflog")) {
1291 handle_reflog(revs, flags);
1292 continue;
1294 if (!strcmp(arg, "--not")) {
1295 flags ^= UNINTERESTING;
1296 continue;
1298 if (!strcmp(arg, "--no-walk")) {
1299 revs->no_walk = 1;
1300 continue;
1302 if (!strcmp(arg, "--do-walk")) {
1303 revs->no_walk = 0;
1304 continue;
1307 opts = handle_revision_opt(revs, argc - i, argv + i, &left, argv);
1308 if (opts > 0) {
1309 i += opts - 1;
1310 continue;
1312 if (opts < 0)
1313 exit(128);
1314 continue;
1317 if (handle_revision_arg(arg, revs, flags, seen_dashdash)) {
1318 int j;
1319 if (seen_dashdash || *arg == '^')
1320 die("bad revision '%s'", arg);
1322 /* If we didn't have a "--":
1323 * (1) all filenames must exist;
1324 * (2) all rev-args must not be interpretable
1325 * as a valid filename.
1326 * but the latter we have checked in the main loop.
1328 for (j = i; j < argc; j++)
1329 verify_filename(revs->prefix, argv[j]);
1331 revs->prune_data = get_pathspec(revs->prefix,
1332 argv + i);
1333 break;
1337 if (revs->def == NULL)
1338 revs->def = def;
1339 if (revs->show_merge)
1340 prepare_show_merge(revs);
1341 if (revs->def && !revs->pending.nr) {
1342 unsigned char sha1[20];
1343 struct object *object;
1344 unsigned mode;
1345 if (get_sha1_with_mode(revs->def, sha1, &mode))
1346 die("bad default revision '%s'", revs->def);
1347 object = get_reference(revs, revs->def, sha1, 0);
1348 add_pending_object_with_mode(revs, object, revs->def, mode);
1351 /* Did the user ask for any diff output? Run the diff! */
1352 if (revs->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT)
1353 revs->diff = 1;
1355 /* Pickaxe, diff-filter and rename following need diffs */
1356 if (revs->diffopt.pickaxe ||
1357 revs->diffopt.filter ||
1358 DIFF_OPT_TST(&revs->diffopt, FOLLOW_RENAMES))
1359 revs->diff = 1;
1361 if (revs->topo_order)
1362 revs->limited = 1;
1364 if (revs->prune_data) {
1365 diff_tree_setup_paths(revs->prune_data, &revs->pruning);
1366 /* Can't prune commits with rename following: the paths change.. */
1367 if (!DIFF_OPT_TST(&revs->diffopt, FOLLOW_RENAMES))
1368 revs->prune = 1;
1369 if (!revs->full_diff)
1370 diff_tree_setup_paths(revs->prune_data, &revs->diffopt);
1372 if (revs->combine_merges) {
1373 revs->ignore_merges = 0;
1374 if (revs->dense_combined_merges && !revs->diffopt.output_format)
1375 revs->diffopt.output_format = DIFF_FORMAT_PATCH;
1377 revs->diffopt.abbrev = revs->abbrev;
1378 if (diff_setup_done(&revs->diffopt) < 0)
1379 die("diff_setup_done failed");
1381 compile_grep_patterns(&revs->grep_filter);
1383 if (revs->reverse && revs->reflog_info)
1384 die("cannot combine --reverse with --walk-reflogs");
1385 if (revs->rewrite_parents && revs->children.name)
1386 die("cannot combine --parents and --children");
1389 * Limitations on the graph functionality
1391 if (revs->reverse && revs->graph)
1392 die("cannot combine --reverse with --graph");
1394 if (revs->reflog_info && revs->graph)
1395 die("cannot combine --walk-reflogs with --graph");
1397 /* limits on caching
1398 * todo: implement this functionality */
1399 if (revs->prune || revs->diff)
1400 revs->dont_cache_me = 1;
1402 return left;
1405 static void add_child(struct rev_info *revs, struct commit *parent, struct commit *child)
1407 struct commit_list *l = xcalloc(1, sizeof(*l));
1409 l->item = child;
1410 l->next = add_decoration(&revs->children, &parent->object, l);
1413 static int remove_duplicate_parents(struct commit *commit)
1415 struct commit_list **pp, *p;
1416 int surviving_parents;
1418 /* Examine existing parents while marking ones we have seen... */
1419 pp = &commit->parents;
1420 while ((p = *pp) != NULL) {
1421 struct commit *parent = p->item;
1422 if (parent->object.flags & TMP_MARK) {
1423 *pp = p->next;
1424 continue;
1426 parent->object.flags |= TMP_MARK;
1427 pp = &p->next;
1429 /* count them while clearing the temporary mark */
1430 surviving_parents = 0;
1431 for (p = commit->parents; p; p = p->next) {
1432 p->item->object.flags &= ~TMP_MARK;
1433 surviving_parents++;
1435 return surviving_parents;
1438 struct merge_simplify_state {
1439 struct commit *simplified;
1442 static struct merge_simplify_state *locate_simplify_state(struct rev_info *revs, struct commit *commit)
1444 struct merge_simplify_state *st;
1446 st = lookup_decoration(&revs->merge_simplification, &commit->object);
1447 if (!st) {
1448 st = xcalloc(1, sizeof(*st));
1449 add_decoration(&revs->merge_simplification, &commit->object, st);
1451 return st;
1454 static struct commit_list **simplify_one(struct rev_info *revs, struct commit *commit, struct commit_list **tail)
1456 struct commit_list *p;
1457 struct merge_simplify_state *st, *pst;
1458 int cnt;
1460 st = locate_simplify_state(revs, commit);
1463 * Have we handled this one?
1465 if (st->simplified)
1466 return tail;
1469 * An UNINTERESTING commit simplifies to itself, so does a
1470 * root commit. We do not rewrite parents of such commit
1471 * anyway.
1473 if ((commit->object.flags & UNINTERESTING) || !commit->parents) {
1474 st->simplified = commit;
1475 return tail;
1479 * Do we know what commit all of our parents should be rewritten to?
1480 * Otherwise we are not ready to rewrite this one yet.
1482 for (cnt = 0, p = commit->parents; p; p = p->next) {
1483 pst = locate_simplify_state(revs, p->item);
1484 if (!pst->simplified) {
1485 tail = &commit_list_insert(p->item, tail)->next;
1486 cnt++;
1489 if (cnt) {
1490 tail = &commit_list_insert(commit, tail)->next;
1491 return tail;
1495 * Rewrite our list of parents.
1497 for (p = commit->parents; p; p = p->next) {
1498 pst = locate_simplify_state(revs, p->item);
1499 p->item = pst->simplified;
1501 cnt = remove_duplicate_parents(commit);
1504 * It is possible that we are a merge and one side branch
1505 * does not have any commit that touches the given paths;
1506 * in such a case, the immediate parents will be rewritten
1507 * to different commits.
1509 * o----X X: the commit we are looking at;
1510 * / / o: a commit that touches the paths;
1511 * ---o----'
1513 * Further reduce the parents by removing redundant parents.
1515 if (1 < cnt) {
1516 struct commit_list *h = reduce_heads(commit->parents);
1517 cnt = commit_list_count(h);
1518 free_commit_list(commit->parents);
1519 commit->parents = h;
1523 * A commit simplifies to itself if it is a root, if it is
1524 * UNINTERESTING, if it touches the given paths, or if it is a
1525 * merge and its parents simplifies to more than one commits
1526 * (the first two cases are already handled at the beginning of
1527 * this function).
1529 * Otherwise, it simplifies to what its sole parent simplifies to.
1531 if (!cnt ||
1532 (commit->object.flags & UNINTERESTING) ||
1533 !(commit->object.flags & TREESAME) ||
1534 (1 < cnt))
1535 st->simplified = commit;
1536 else {
1537 pst = locate_simplify_state(revs, commit->parents->item);
1538 st->simplified = pst->simplified;
1540 return tail;
1543 static void simplify_merges(struct rev_info *revs)
1545 struct commit_list *list;
1546 struct commit_list *yet_to_do, **tail;
1548 if (!revs->topo_order)
1549 sort_in_topological_order(&revs->commits, revs->lifo);
1550 if (!revs->prune)
1551 return;
1553 /* feed the list reversed */
1554 yet_to_do = NULL;
1555 for (list = revs->commits; list; list = list->next)
1556 commit_list_insert(list->item, &yet_to_do);
1557 while (yet_to_do) {
1558 list = yet_to_do;
1559 yet_to_do = NULL;
1560 tail = &yet_to_do;
1561 while (list) {
1562 struct commit *commit = list->item;
1563 struct commit_list *next = list->next;
1564 free(list);
1565 list = next;
1566 tail = simplify_one(revs, commit, tail);
1570 /* clean up the result, removing the simplified ones */
1571 list = revs->commits;
1572 revs->commits = NULL;
1573 tail = &revs->commits;
1574 while (list) {
1575 struct commit *commit = list->item;
1576 struct commit_list *next = list->next;
1577 struct merge_simplify_state *st;
1578 free(list);
1579 list = next;
1580 st = locate_simplify_state(revs, commit);
1581 if (st->simplified == commit)
1582 tail = &commit_list_insert(commit, tail)->next;
1586 static void set_children(struct rev_info *revs)
1588 struct commit_list *l;
1589 for (l = revs->commits; l; l = l->next) {
1590 struct commit *commit = l->item;
1591 struct commit_list *p;
1593 for (p = commit->parents; p; p = p->next)
1594 add_child(revs, p->item, commit);
1598 int prepare_revision_walk(struct rev_info *revs)
1600 int nr = revs->pending.nr;
1601 struct object_array_entry *e, *list;
1603 e = list = revs->pending.objects;
1604 revs->pending.nr = 0;
1605 revs->pending.alloc = 0;
1606 revs->pending.objects = NULL;
1607 while (--nr >= 0) {
1608 struct commit *commit = handle_commit(revs, e->item, e->name);
1609 if (commit) {
1610 if (!(commit->object.flags & SEEN)) {
1611 commit->object.flags |= SEEN;
1612 insert_by_date(commit, &revs->commits);
1615 e++;
1617 free(list);
1619 if (revs->no_walk)
1620 return 0;
1621 if (revs->limited)
1622 if (limit_list(revs) < 0)
1623 return -1;
1624 if (revs->topo_order)
1625 sort_in_topological_order(&revs->commits, revs->lifo);
1626 if (revs->simplify_merges)
1627 simplify_merges(revs);
1628 if (revs->children.name)
1629 set_children(revs);
1630 return 0;
1633 enum rewrite_result {
1634 rewrite_one_ok,
1635 rewrite_one_noparents,
1636 rewrite_one_error,
1639 static enum rewrite_result rewrite_one(struct rev_info *revs, struct commit **pp)
1641 struct commit_list *cache = NULL;
1643 for (;;) {
1644 struct commit *p = *pp;
1645 if (!revs->limited)
1646 if (add_parents_to_list(revs, p, &revs->commits, &cache) < 0)
1647 return rewrite_one_error;
1648 if (p->parents && p->parents->next)
1649 return rewrite_one_ok;
1650 if (p->object.flags & UNINTERESTING)
1651 return rewrite_one_ok;
1652 if (!(p->object.flags & TREESAME))
1653 return rewrite_one_ok;
1654 if (!p->parents)
1655 return rewrite_one_noparents;
1656 *pp = p->parents->item;
1660 static int rewrite_parents(struct rev_info *revs, struct commit *commit)
1662 struct commit_list **pp = &commit->parents;
1663 while (*pp) {
1664 struct commit_list *parent = *pp;
1665 switch (rewrite_one(revs, &parent->item)) {
1666 case rewrite_one_ok:
1667 break;
1668 case rewrite_one_noparents:
1669 *pp = parent->next;
1670 continue;
1671 case rewrite_one_error:
1672 return -1;
1674 pp = &parent->next;
1676 remove_duplicate_parents(commit);
1677 return 0;
1680 static int commit_match(struct commit *commit, struct rev_info *opt)
1682 if (!opt->grep_filter.pattern_list)
1683 return 1;
1684 if (!commit->object.parsed)
1685 parse_commit(commit);
1686 return grep_buffer(&opt->grep_filter,
1687 NULL, /* we say nothing, not even filename */
1688 commit->buffer, strlen(commit->buffer));
1691 static inline int want_ancestry(struct rev_info *revs)
1693 return (revs->rewrite_parents || revs->children.name);
1696 enum commit_action get_commit_action(struct rev_info *revs, struct commit *commit)
1698 if (commit->object.flags & SHOWN)
1699 return commit_ignore;
1700 if (revs->unpacked && has_sha1_pack(commit->object.sha1))
1701 return commit_ignore;
1702 if (revs->show_all)
1703 return commit_show;
1704 if (commit->object.flags & UNINTERESTING)
1705 return commit_ignore;
1706 if (revs->min_age != -1 && (commit->date > revs->min_age))
1707 return commit_ignore;
1708 if (revs->no_merges && commit->parents && commit->parents->next)
1709 return commit_ignore;
1710 if (revs->merges_only && !(commit->parents && commit->parents->next))
1711 return commit_ignore;
1712 if (!commit_match(commit, revs))
1713 return commit_ignore;
1714 if (revs->prune && revs->dense) {
1715 /* Commit without changes? */
1716 if (commit->object.flags & TREESAME) {
1717 /* drop merges unless we want parenthood */
1718 if (!want_ancestry(revs))
1719 return commit_ignore;
1720 /* non-merge - always ignore it */
1721 if (!commit->parents || !commit->parents->next)
1722 return commit_ignore;
1725 return commit_show;
1728 enum commit_action simplify_commit(struct rev_info *revs, struct commit *commit)
1730 enum commit_action action = get_commit_action(revs, commit);
1732 if (action == commit_show &&
1733 !revs->show_all &&
1734 revs->prune && revs->dense && want_ancestry(revs)) {
1735 if (rewrite_parents(revs, commit) < 0)
1736 return commit_error;
1738 return action;
1741 static struct commit *get_revision_1(struct rev_info *revs)
1743 if (!revs->commits)
1744 return NULL;
1746 do {
1747 struct commit_list *entry = revs->commits;
1748 struct commit *commit = entry->item;
1749 struct object *obj = &commit->object;
1751 revs->commits = entry->next;
1752 free(entry);
1754 if (revs->reflog_info)
1755 fake_reflog_parent(revs->reflog_info, commit);
1758 * If we haven't done the list limiting, we need to look at
1759 * the parents here. We also need to do the date-based limiting
1760 * that we'd otherwise have done in limit_list().
1762 if (!revs->limited) {
1763 if (revs->max_age != -1 &&
1764 (commit->date < revs->max_age))
1765 continue;
1767 if (!revs->dont_cache_me) {
1768 struct commit_list *queue = 0, **queuep = &queue;
1769 unsigned char *cache_sha1;
1771 if (obj->flags & ADDED)
1772 goto skip_parenting;
1774 cache_sha1 = get_cache_slice(commit);
1775 if (cache_sha1) {
1776 if (!traverse_cache_slice(revs, cache_sha1, commit, 0, 0, &queuep, &revs->commits)) {
1777 struct commit_list *work = revs->commits;
1779 /* attach queue to end of ->commits */
1780 while (work && work->next)
1781 work = work->next;
1783 if (work)
1784 work->next = queue;
1785 else
1786 revs->commits = queue;
1788 goto skip_parenting;
1793 if (add_parents_to_list(revs, commit, &revs->commits, NULL) < 0)
1794 die("Failed to traverse parents of commit %s",
1795 sha1_to_hex(commit->object.sha1));
1798 skip_parenting:
1799 switch (simplify_commit(revs, commit)) {
1800 case commit_ignore:
1801 continue;
1802 case commit_error:
1803 die("Failed to simplify parents of commit %s",
1804 sha1_to_hex(commit->object.sha1));
1805 default:
1806 return commit;
1808 } while (revs->commits);
1809 return NULL;
1812 static void gc_boundary(struct object_array *array)
1814 unsigned nr = array->nr;
1815 unsigned alloc = array->alloc;
1816 struct object_array_entry *objects = array->objects;
1818 if (alloc <= nr) {
1819 unsigned i, j;
1820 for (i = j = 0; i < nr; i++) {
1821 if (objects[i].item->flags & SHOWN)
1822 continue;
1823 if (i != j)
1824 objects[j] = objects[i];
1825 j++;
1827 for (i = j; i < nr; i++)
1828 objects[i].item = NULL;
1829 array->nr = j;
1833 static void create_boundary_commit_list(struct rev_info *revs)
1835 unsigned i;
1836 struct commit *c;
1837 struct object_array *array = &revs->boundary_commits;
1838 struct object_array_entry *objects = array->objects;
1841 * If revs->commits is non-NULL at this point, an error occurred in
1842 * get_revision_1(). Ignore the error and continue printing the
1843 * boundary commits anyway. (This is what the code has always
1844 * done.)
1846 if (revs->commits) {
1847 free_commit_list(revs->commits);
1848 revs->commits = NULL;
1852 * Put all of the actual boundary commits from revs->boundary_commits
1853 * into revs->commits
1855 for (i = 0; i < array->nr; i++) {
1856 c = (struct commit *)(objects[i].item);
1857 if (!c)
1858 continue;
1859 if (!(c->object.flags & CHILD_SHOWN))
1860 continue;
1861 if (c->object.flags & (SHOWN | BOUNDARY))
1862 continue;
1863 c->object.flags |= BOUNDARY;
1864 commit_list_insert(c, &revs->commits);
1868 * If revs->topo_order is set, sort the boundary commits
1869 * in topological order
1871 sort_in_topological_order(&revs->commits, revs->lifo);
1874 static struct commit *get_revision_internal(struct rev_info *revs)
1876 struct commit *c = NULL;
1877 struct commit_list *l;
1879 if (revs->boundary == 2) {
1881 * All of the normal commits have already been returned,
1882 * and we are now returning boundary commits.
1883 * create_boundary_commit_list() has populated
1884 * revs->commits with the remaining commits to return.
1886 c = pop_commit(&revs->commits);
1887 if (c)
1888 c->object.flags |= SHOWN;
1889 return c;
1893 * Now pick up what they want to give us
1895 c = get_revision_1(revs);
1896 if (c) {
1897 while (0 < revs->skip_count) {
1898 revs->skip_count--;
1899 c = get_revision_1(revs);
1900 if (!c)
1901 break;
1906 * Check the max_count.
1908 switch (revs->max_count) {
1909 case -1:
1910 break;
1911 case 0:
1912 c = NULL;
1913 break;
1914 default:
1915 revs->max_count--;
1918 if (c)
1919 c->object.flags |= SHOWN;
1921 if (!revs->boundary) {
1922 return c;
1925 if (!c) {
1927 * get_revision_1() runs out the commits, and
1928 * we are done computing the boundaries.
1929 * switch to boundary commits output mode.
1931 revs->boundary = 2;
1934 * Update revs->commits to contain the list of
1935 * boundary commits.
1937 create_boundary_commit_list(revs);
1939 return get_revision_internal(revs);
1943 * boundary commits are the commits that are parents of the
1944 * ones we got from get_revision_1() but they themselves are
1945 * not returned from get_revision_1(). Before returning
1946 * 'c', we need to mark its parents that they could be boundaries.
1949 for (l = c->parents; l; l = l->next) {
1950 struct object *p;
1951 p = &(l->item->object);
1952 if (p->flags & (CHILD_SHOWN | SHOWN))
1953 continue;
1954 p->flags |= CHILD_SHOWN;
1955 gc_boundary(&revs->boundary_commits);
1956 add_object_array(p, NULL, &revs->boundary_commits);
1959 return c;
1962 struct commit *get_revision(struct rev_info *revs)
1964 struct commit *c;
1965 struct commit_list *reversed;
1967 if (revs->reverse) {
1968 reversed = NULL;
1969 while ((c = get_revision_internal(revs))) {
1970 commit_list_insert(c, &reversed);
1972 revs->commits = reversed;
1973 revs->reverse = 0;
1974 revs->reverse_output_stage = 1;
1977 if (revs->reverse_output_stage)
1978 return pop_commit(&revs->commits);
1980 c = get_revision_internal(revs);
1981 if (c && revs->graph)
1982 graph_update(revs->graph, c);
1983 return c;