revision: parse "git log -<count>" more carefully
[git/git-svn.git] / revision.c
blobd5e2f1a257dfd508bd2a2d1f1fde6c49a03541f5
1 #include "cache.h"
2 #include "tag.h"
3 #include "blob.h"
4 #include "tree.h"
5 #include "commit.h"
6 #include "diff.h"
7 #include "refs.h"
8 #include "revision.h"
9 #include "graph.h"
10 #include "grep.h"
11 #include "reflog-walk.h"
12 #include "patch-ids.h"
13 #include "decorate.h"
14 #include "log-tree.h"
15 #include "string-list.h"
16 #include "line-log.h"
17 #include "mailmap.h"
18 #include "commit-slab.h"
20 volatile show_early_output_fn_t show_early_output;
22 char *path_name(const struct name_path *path, const char *name)
24 const struct name_path *p;
25 char *n, *m;
26 int nlen = strlen(name);
27 int len = nlen + 1;
29 for (p = path; p; p = p->up) {
30 if (p->elem_len)
31 len += p->elem_len + 1;
33 n = xmalloc(len);
34 m = n + len - (nlen + 1);
35 strcpy(m, name);
36 for (p = path; p; p = p->up) {
37 if (p->elem_len) {
38 m -= p->elem_len + 1;
39 memcpy(m, p->elem, p->elem_len);
40 m[p->elem_len] = '/';
43 return n;
46 static int show_path_component_truncated(FILE *out, const char *name, int len)
48 int cnt;
49 for (cnt = 0; cnt < len; cnt++) {
50 int ch = name[cnt];
51 if (!ch || ch == '\n')
52 return -1;
53 fputc(ch, out);
55 return len;
58 static int show_path_truncated(FILE *out, const struct name_path *path)
60 int emitted, ours;
62 if (!path)
63 return 0;
64 emitted = show_path_truncated(out, path->up);
65 if (emitted < 0)
66 return emitted;
67 if (emitted)
68 fputc('/', out);
69 ours = show_path_component_truncated(out, path->elem, path->elem_len);
70 if (ours < 0)
71 return ours;
72 return ours || emitted;
75 void show_object_with_name(FILE *out, struct object *obj,
76 const struct name_path *path, const char *component)
78 struct name_path leaf;
79 leaf.up = (struct name_path *)path;
80 leaf.elem = component;
81 leaf.elem_len = strlen(component);
83 fprintf(out, "%s ", sha1_to_hex(obj->sha1));
84 show_path_truncated(out, &leaf);
85 fputc('\n', out);
88 void add_object(struct object *obj,
89 struct object_array *p,
90 struct name_path *path,
91 const char *name)
93 char *pn = path_name(path, name);
94 add_object_array(obj, pn, p);
95 free(pn);
98 static void mark_blob_uninteresting(struct blob *blob)
100 if (!blob)
101 return;
102 if (blob->object.flags & UNINTERESTING)
103 return;
104 blob->object.flags |= UNINTERESTING;
107 static void mark_tree_contents_uninteresting(struct tree *tree)
109 struct tree_desc desc;
110 struct name_entry entry;
111 struct object *obj = &tree->object;
113 if (!has_sha1_file(obj->sha1))
114 return;
115 if (parse_tree(tree) < 0)
116 die("bad tree %s", sha1_to_hex(obj->sha1));
118 init_tree_desc(&desc, tree->buffer, tree->size);
119 while (tree_entry(&desc, &entry)) {
120 switch (object_type(entry.mode)) {
121 case OBJ_TREE:
122 mark_tree_uninteresting(lookup_tree(entry.sha1));
123 break;
124 case OBJ_BLOB:
125 mark_blob_uninteresting(lookup_blob(entry.sha1));
126 break;
127 default:
128 /* Subproject commit - not in this repository */
129 break;
134 * We don't care about the tree any more
135 * after it has been marked uninteresting.
137 free_tree_buffer(tree);
140 void mark_tree_uninteresting(struct tree *tree)
142 struct object *obj = &tree->object;
144 if (!tree)
145 return;
146 if (obj->flags & UNINTERESTING)
147 return;
148 obj->flags |= UNINTERESTING;
149 mark_tree_contents_uninteresting(tree);
152 void mark_parents_uninteresting(struct commit *commit)
154 struct commit_list *parents = NULL, *l;
156 for (l = commit->parents; l; l = l->next)
157 commit_list_insert(l->item, &parents);
159 while (parents) {
160 struct commit *commit = parents->item;
161 l = parents;
162 parents = parents->next;
163 free(l);
165 while (commit) {
167 * A missing commit is ok iff its parent is marked
168 * uninteresting.
170 * We just mark such a thing parsed, so that when
171 * it is popped next time around, we won't be trying
172 * to parse it and get an error.
174 if (!has_sha1_file(commit->object.sha1))
175 commit->object.parsed = 1;
177 if (commit->object.flags & UNINTERESTING)
178 break;
180 commit->object.flags |= UNINTERESTING;
183 * Normally we haven't parsed the parent
184 * yet, so we won't have a parent of a parent
185 * here. However, it may turn out that we've
186 * reached this commit some other way (where it
187 * wasn't uninteresting), in which case we need
188 * to mark its parents recursively too..
190 if (!commit->parents)
191 break;
193 for (l = commit->parents->next; l; l = l->next)
194 commit_list_insert(l->item, &parents);
195 commit = commit->parents->item;
200 static void add_pending_object_with_mode(struct rev_info *revs,
201 struct object *obj,
202 const char *name, unsigned mode)
204 if (!obj)
205 return;
206 if (revs->no_walk && (obj->flags & UNINTERESTING))
207 revs->no_walk = 0;
208 if (revs->reflog_info && obj->type == OBJ_COMMIT) {
209 struct strbuf buf = STRBUF_INIT;
210 int len = interpret_branch_name(name, 0, &buf);
211 int st;
213 if (0 < len && name[len] && buf.len)
214 strbuf_addstr(&buf, name + len);
215 st = add_reflog_for_walk(revs->reflog_info,
216 (struct commit *)obj,
217 buf.buf[0] ? buf.buf: name);
218 strbuf_release(&buf);
219 if (st)
220 return;
222 add_object_array_with_mode(obj, name, &revs->pending, mode);
225 void add_pending_object(struct rev_info *revs,
226 struct object *obj, const char *name)
228 add_pending_object_with_mode(revs, obj, name, S_IFINVALID);
231 void add_head_to_pending(struct rev_info *revs)
233 unsigned char sha1[20];
234 struct object *obj;
235 if (get_sha1("HEAD", sha1))
236 return;
237 obj = parse_object(sha1);
238 if (!obj)
239 return;
240 add_pending_object(revs, obj, "HEAD");
243 static struct object *get_reference(struct rev_info *revs, const char *name,
244 const unsigned char *sha1,
245 unsigned int flags)
247 struct object *object;
249 object = parse_object(sha1);
250 if (!object) {
251 if (revs->ignore_missing)
252 return object;
253 die("bad object %s", name);
255 object->flags |= flags;
256 return object;
259 void add_pending_sha1(struct rev_info *revs, const char *name,
260 const unsigned char *sha1, unsigned int flags)
262 struct object *object = get_reference(revs, name, sha1, flags);
263 add_pending_object(revs, object, name);
266 static struct commit *handle_commit(struct rev_info *revs,
267 struct object *object, const char *name)
269 unsigned long flags = object->flags;
272 * Tag object? Look what it points to..
274 while (object->type == OBJ_TAG) {
275 struct tag *tag = (struct tag *) object;
276 if (revs->tag_objects && !(flags & UNINTERESTING))
277 add_pending_object(revs, object, tag->tag);
278 if (!tag->tagged)
279 die("bad tag");
280 object = parse_object(tag->tagged->sha1);
281 if (!object) {
282 if (flags & UNINTERESTING)
283 return NULL;
284 die("bad object %s", sha1_to_hex(tag->tagged->sha1));
286 object->flags |= flags;
290 * Commit object? Just return it, we'll do all the complex
291 * reachability crud.
293 if (object->type == OBJ_COMMIT) {
294 struct commit *commit = (struct commit *)object;
295 if (parse_commit(commit) < 0)
296 die("unable to parse commit %s", name);
297 if (flags & UNINTERESTING) {
298 mark_parents_uninteresting(commit);
299 revs->limited = 1;
301 if (revs->show_source && !commit->util)
302 commit->util = (void *) name;
303 return commit;
307 * Tree object? Either mark it uninteresting, or add it
308 * to the list of objects to look at later..
310 if (object->type == OBJ_TREE) {
311 struct tree *tree = (struct tree *)object;
312 if (!revs->tree_objects)
313 return NULL;
314 if (flags & UNINTERESTING) {
315 mark_tree_contents_uninteresting(tree);
316 return NULL;
318 add_pending_object(revs, object, "");
319 return NULL;
323 * Blob object? You know the drill by now..
325 if (object->type == OBJ_BLOB) {
326 if (!revs->blob_objects)
327 return NULL;
328 if (flags & UNINTERESTING)
329 return NULL;
330 add_pending_object(revs, object, "");
331 return NULL;
333 die("%s is unknown object", name);
336 static int everybody_uninteresting(struct commit_list *orig)
338 struct commit_list *list = orig;
339 while (list) {
340 struct commit *commit = list->item;
341 list = list->next;
342 if (commit->object.flags & UNINTERESTING)
343 continue;
344 return 0;
346 return 1;
350 * A definition of "relevant" commit that we can use to simplify limited graphs
351 * by eliminating side branches.
353 * A "relevant" commit is one that is !UNINTERESTING (ie we are including it
354 * in our list), or that is a specified BOTTOM commit. Then after computing
355 * a limited list, during processing we can generally ignore boundary merges
356 * coming from outside the graph, (ie from irrelevant parents), and treat
357 * those merges as if they were single-parent. TREESAME is defined to consider
358 * only relevant parents, if any. If we are TREESAME to our on-graph parents,
359 * we don't care if we were !TREESAME to non-graph parents.
361 * Treating bottom commits as relevant ensures that a limited graph's
362 * connection to the actual bottom commit is not viewed as a side branch, but
363 * treated as part of the graph. For example:
365 * ....Z...A---X---o---o---B
366 * . /
367 * W---Y
369 * When computing "A..B", the A-X connection is at least as important as
370 * Y-X, despite A being flagged UNINTERESTING.
372 * And when computing --ancestry-path "A..B", the A-X connection is more
373 * important than Y-X, despite both A and Y being flagged UNINTERESTING.
375 static inline int relevant_commit(struct commit *commit)
377 return (commit->object.flags & (UNINTERESTING | BOTTOM)) != UNINTERESTING;
381 * Return a single relevant commit from a parent list. If we are a TREESAME
382 * commit, and this selects one of our parents, then we can safely simplify to
383 * that parent.
385 static struct commit *one_relevant_parent(const struct rev_info *revs,
386 struct commit_list *orig)
388 struct commit_list *list = orig;
389 struct commit *relevant = NULL;
391 if (!orig)
392 return NULL;
395 * For 1-parent commits, or if first-parent-only, then return that
396 * first parent (even if not "relevant" by the above definition).
397 * TREESAME will have been set purely on that parent.
399 if (revs->first_parent_only || !orig->next)
400 return orig->item;
403 * For multi-parent commits, identify a sole relevant parent, if any.
404 * If we have only one relevant parent, then TREESAME will be set purely
405 * with regard to that parent, and we can simplify accordingly.
407 * If we have more than one relevant parent, or no relevant parents
408 * (and multiple irrelevant ones), then we can't select a parent here
409 * and return NULL.
411 while (list) {
412 struct commit *commit = list->item;
413 list = list->next;
414 if (relevant_commit(commit)) {
415 if (relevant)
416 return NULL;
417 relevant = commit;
420 return relevant;
424 * The goal is to get REV_TREE_NEW as the result only if the
425 * diff consists of all '+' (and no other changes), REV_TREE_OLD
426 * if the whole diff is removal of old data, and otherwise
427 * REV_TREE_DIFFERENT (of course if the trees are the same we
428 * want REV_TREE_SAME).
429 * That means that once we get to REV_TREE_DIFFERENT, we do not
430 * have to look any further.
432 static int tree_difference = REV_TREE_SAME;
434 static void file_add_remove(struct diff_options *options,
435 int addremove, unsigned mode,
436 const unsigned char *sha1,
437 int sha1_valid,
438 const char *fullpath, unsigned dirty_submodule)
440 int diff = addremove == '+' ? REV_TREE_NEW : REV_TREE_OLD;
442 tree_difference |= diff;
443 if (tree_difference == REV_TREE_DIFFERENT)
444 DIFF_OPT_SET(options, HAS_CHANGES);
447 static void file_change(struct diff_options *options,
448 unsigned old_mode, unsigned new_mode,
449 const unsigned char *old_sha1,
450 const unsigned char *new_sha1,
451 int old_sha1_valid, int new_sha1_valid,
452 const char *fullpath,
453 unsigned old_dirty_submodule, unsigned new_dirty_submodule)
455 tree_difference = REV_TREE_DIFFERENT;
456 DIFF_OPT_SET(options, HAS_CHANGES);
459 static int rev_compare_tree(struct rev_info *revs,
460 struct commit *parent, struct commit *commit)
462 struct tree *t1 = parent->tree;
463 struct tree *t2 = commit->tree;
465 if (!t1)
466 return REV_TREE_NEW;
467 if (!t2)
468 return REV_TREE_OLD;
470 if (revs->simplify_by_decoration) {
472 * If we are simplifying by decoration, then the commit
473 * is worth showing if it has a tag pointing at it.
475 if (lookup_decoration(&name_decoration, &commit->object))
476 return REV_TREE_DIFFERENT;
478 * A commit that is not pointed by a tag is uninteresting
479 * if we are not limited by path. This means that you will
480 * see the usual "commits that touch the paths" plus any
481 * tagged commit by specifying both --simplify-by-decoration
482 * and pathspec.
484 if (!revs->prune_data.nr)
485 return REV_TREE_SAME;
488 tree_difference = REV_TREE_SAME;
489 DIFF_OPT_CLR(&revs->pruning, HAS_CHANGES);
490 if (diff_tree_sha1(t1->object.sha1, t2->object.sha1, "",
491 &revs->pruning) < 0)
492 return REV_TREE_DIFFERENT;
493 return tree_difference;
496 static int rev_same_tree_as_empty(struct rev_info *revs, struct commit *commit)
498 int retval;
499 void *tree;
500 unsigned long size;
501 struct tree_desc empty, real;
502 struct tree *t1 = commit->tree;
504 if (!t1)
505 return 0;
507 tree = read_object_with_reference(t1->object.sha1, tree_type, &size, NULL);
508 if (!tree)
509 return 0;
510 init_tree_desc(&real, tree, size);
511 init_tree_desc(&empty, "", 0);
513 tree_difference = REV_TREE_SAME;
514 DIFF_OPT_CLR(&revs->pruning, HAS_CHANGES);
515 retval = diff_tree(&empty, &real, "", &revs->pruning);
516 free(tree);
518 return retval >= 0 && (tree_difference == REV_TREE_SAME);
521 struct treesame_state {
522 unsigned int nparents;
523 unsigned char treesame[FLEX_ARRAY];
526 static struct treesame_state *initialise_treesame(struct rev_info *revs, struct commit *commit)
528 unsigned n = commit_list_count(commit->parents);
529 struct treesame_state *st = xcalloc(1, sizeof(*st) + n);
530 st->nparents = n;
531 add_decoration(&revs->treesame, &commit->object, st);
532 return st;
536 * Must be called immediately after removing the nth_parent from a commit's
537 * parent list, if we are maintaining the per-parent treesame[] decoration.
538 * This does not recalculate the master TREESAME flag - update_treesame()
539 * should be called to update it after a sequence of treesame[] modifications
540 * that may have affected it.
542 static int compact_treesame(struct rev_info *revs, struct commit *commit, unsigned nth_parent)
544 struct treesame_state *st;
545 int old_same;
547 if (!commit->parents) {
549 * Have just removed the only parent from a non-merge.
550 * Different handling, as we lack decoration.
552 if (nth_parent != 0)
553 die("compact_treesame %u", nth_parent);
554 old_same = !!(commit->object.flags & TREESAME);
555 if (rev_same_tree_as_empty(revs, commit))
556 commit->object.flags |= TREESAME;
557 else
558 commit->object.flags &= ~TREESAME;
559 return old_same;
562 st = lookup_decoration(&revs->treesame, &commit->object);
563 if (!st || nth_parent >= st->nparents)
564 die("compact_treesame %u", nth_parent);
566 old_same = st->treesame[nth_parent];
567 memmove(st->treesame + nth_parent,
568 st->treesame + nth_parent + 1,
569 st->nparents - nth_parent - 1);
572 * If we've just become a non-merge commit, update TREESAME
573 * immediately, and remove the no-longer-needed decoration.
574 * If still a merge, defer update until update_treesame().
576 if (--st->nparents == 1) {
577 if (commit->parents->next)
578 die("compact_treesame parents mismatch");
579 if (st->treesame[0] && revs->dense)
580 commit->object.flags |= TREESAME;
581 else
582 commit->object.flags &= ~TREESAME;
583 free(add_decoration(&revs->treesame, &commit->object, NULL));
586 return old_same;
589 static unsigned update_treesame(struct rev_info *revs, struct commit *commit)
591 if (commit->parents && commit->parents->next) {
592 unsigned n;
593 struct treesame_state *st;
594 struct commit_list *p;
595 unsigned relevant_parents;
596 unsigned relevant_change, irrelevant_change;
598 st = lookup_decoration(&revs->treesame, &commit->object);
599 if (!st)
600 die("update_treesame %s", sha1_to_hex(commit->object.sha1));
601 relevant_parents = 0;
602 relevant_change = irrelevant_change = 0;
603 for (p = commit->parents, n = 0; p; n++, p = p->next) {
604 if (relevant_commit(p->item)) {
605 relevant_change |= !st->treesame[n];
606 relevant_parents++;
607 } else
608 irrelevant_change |= !st->treesame[n];
610 if (relevant_parents ? relevant_change : irrelevant_change)
611 commit->object.flags &= ~TREESAME;
612 else
613 commit->object.flags |= TREESAME;
616 return commit->object.flags & TREESAME;
619 static inline int limiting_can_increase_treesame(const struct rev_info *revs)
622 * TREESAME is irrelevant unless prune && dense;
623 * if simplify_history is set, we can't have a mixture of TREESAME and
624 * !TREESAME INTERESTING parents (and we don't have treesame[]
625 * decoration anyway);
626 * if first_parent_only is set, then the TREESAME flag is locked
627 * against the first parent (and again we lack treesame[] decoration).
629 return revs->prune && revs->dense &&
630 !revs->simplify_history &&
631 !revs->first_parent_only;
634 static void try_to_simplify_commit(struct rev_info *revs, struct commit *commit)
636 struct commit_list **pp, *parent;
637 struct treesame_state *ts = NULL;
638 int relevant_change = 0, irrelevant_change = 0;
639 int relevant_parents, nth_parent;
642 * If we don't do pruning, everything is interesting
644 if (!revs->prune)
645 return;
647 if (!commit->tree)
648 return;
650 if (!commit->parents) {
651 if (rev_same_tree_as_empty(revs, commit))
652 commit->object.flags |= TREESAME;
653 return;
657 * Normal non-merge commit? If we don't want to make the
658 * history dense, we consider it always to be a change..
660 if (!revs->dense && !commit->parents->next)
661 return;
663 for (pp = &commit->parents, nth_parent = 0, relevant_parents = 0;
664 (parent = *pp) != NULL;
665 pp = &parent->next, nth_parent++) {
666 struct commit *p = parent->item;
667 if (relevant_commit(p))
668 relevant_parents++;
670 if (nth_parent == 1) {
672 * This our second loop iteration - so we now know
673 * we're dealing with a merge.
675 * Do not compare with later parents when we care only about
676 * the first parent chain, in order to avoid derailing the
677 * traversal to follow a side branch that brought everything
678 * in the path we are limited to by the pathspec.
680 if (revs->first_parent_only)
681 break;
683 * If this will remain a potentially-simplifiable
684 * merge, remember per-parent treesame if needed.
685 * Initialise the array with the comparison from our
686 * first iteration.
688 if (revs->treesame.name &&
689 !revs->simplify_history &&
690 !(commit->object.flags & UNINTERESTING)) {
691 ts = initialise_treesame(revs, commit);
692 if (!(irrelevant_change || relevant_change))
693 ts->treesame[0] = 1;
696 if (parse_commit(p) < 0)
697 die("cannot simplify commit %s (because of %s)",
698 sha1_to_hex(commit->object.sha1),
699 sha1_to_hex(p->object.sha1));
700 switch (rev_compare_tree(revs, p, commit)) {
701 case REV_TREE_SAME:
702 if (!revs->simplify_history || !relevant_commit(p)) {
703 /* Even if a merge with an uninteresting
704 * side branch brought the entire change
705 * we are interested in, we do not want
706 * to lose the other branches of this
707 * merge, so we just keep going.
709 if (ts)
710 ts->treesame[nth_parent] = 1;
711 continue;
713 parent->next = NULL;
714 commit->parents = parent;
715 commit->object.flags |= TREESAME;
716 return;
718 case REV_TREE_NEW:
719 if (revs->remove_empty_trees &&
720 rev_same_tree_as_empty(revs, p)) {
721 /* We are adding all the specified
722 * paths from this parent, so the
723 * history beyond this parent is not
724 * interesting. Remove its parents
725 * (they are grandparents for us).
726 * IOW, we pretend this parent is a
727 * "root" commit.
729 if (parse_commit(p) < 0)
730 die("cannot simplify commit %s (invalid %s)",
731 sha1_to_hex(commit->object.sha1),
732 sha1_to_hex(p->object.sha1));
733 p->parents = NULL;
735 /* fallthrough */
736 case REV_TREE_OLD:
737 case REV_TREE_DIFFERENT:
738 if (relevant_commit(p))
739 relevant_change = 1;
740 else
741 irrelevant_change = 1;
742 continue;
744 die("bad tree compare for commit %s", sha1_to_hex(commit->object.sha1));
748 * TREESAME is straightforward for single-parent commits. For merge
749 * commits, it is most useful to define it so that "irrelevant"
750 * parents cannot make us !TREESAME - if we have any relevant
751 * parents, then we only consider TREESAMEness with respect to them,
752 * allowing irrelevant merges from uninteresting branches to be
753 * simplified away. Only if we have only irrelevant parents do we
754 * base TREESAME on them. Note that this logic is replicated in
755 * update_treesame, which should be kept in sync.
757 if (relevant_parents ? !relevant_change : !irrelevant_change)
758 commit->object.flags |= TREESAME;
761 static void commit_list_insert_by_date_cached(struct commit *p, struct commit_list **head,
762 struct commit_list *cached_base, struct commit_list **cache)
764 struct commit_list *new_entry;
766 if (cached_base && p->date < cached_base->item->date)
767 new_entry = commit_list_insert_by_date(p, &cached_base->next);
768 else
769 new_entry = commit_list_insert_by_date(p, head);
771 if (cache && (!*cache || p->date < (*cache)->item->date))
772 *cache = new_entry;
775 static int add_parents_to_list(struct rev_info *revs, struct commit *commit,
776 struct commit_list **list, struct commit_list **cache_ptr)
778 struct commit_list *parent = commit->parents;
779 unsigned left_flag;
780 struct commit_list *cached_base = cache_ptr ? *cache_ptr : NULL;
782 if (commit->object.flags & ADDED)
783 return 0;
784 commit->object.flags |= ADDED;
787 * If the commit is uninteresting, don't try to
788 * prune parents - we want the maximal uninteresting
789 * set.
791 * Normally we haven't parsed the parent
792 * yet, so we won't have a parent of a parent
793 * here. However, it may turn out that we've
794 * reached this commit some other way (where it
795 * wasn't uninteresting), in which case we need
796 * to mark its parents recursively too..
798 if (commit->object.flags & UNINTERESTING) {
799 while (parent) {
800 struct commit *p = parent->item;
801 parent = parent->next;
802 if (p)
803 p->object.flags |= UNINTERESTING;
804 if (parse_commit(p) < 0)
805 continue;
806 if (p->parents)
807 mark_parents_uninteresting(p);
808 if (p->object.flags & SEEN)
809 continue;
810 p->object.flags |= SEEN;
811 commit_list_insert_by_date_cached(p, list, cached_base, cache_ptr);
813 return 0;
817 * Ok, the commit wasn't uninteresting. Try to
818 * simplify the commit history and find the parent
819 * that has no differences in the path set if one exists.
821 try_to_simplify_commit(revs, commit);
823 if (revs->no_walk)
824 return 0;
826 left_flag = (commit->object.flags & SYMMETRIC_LEFT);
828 for (parent = commit->parents; parent; parent = parent->next) {
829 struct commit *p = parent->item;
831 if (parse_commit(p) < 0)
832 return -1;
833 if (revs->show_source && !p->util)
834 p->util = commit->util;
835 p->object.flags |= left_flag;
836 if (!(p->object.flags & SEEN)) {
837 p->object.flags |= SEEN;
838 commit_list_insert_by_date_cached(p, list, cached_base, cache_ptr);
840 if (revs->first_parent_only)
841 break;
843 return 0;
846 static void cherry_pick_list(struct commit_list *list, struct rev_info *revs)
848 struct commit_list *p;
849 int left_count = 0, right_count = 0;
850 int left_first;
851 struct patch_ids ids;
852 unsigned cherry_flag;
854 /* First count the commits on the left and on the right */
855 for (p = list; p; p = p->next) {
856 struct commit *commit = p->item;
857 unsigned flags = commit->object.flags;
858 if (flags & BOUNDARY)
860 else if (flags & SYMMETRIC_LEFT)
861 left_count++;
862 else
863 right_count++;
866 if (!left_count || !right_count)
867 return;
869 left_first = left_count < right_count;
870 init_patch_ids(&ids);
871 ids.diffopts.pathspec = revs->diffopt.pathspec;
873 /* Compute patch-ids for one side */
874 for (p = list; p; p = p->next) {
875 struct commit *commit = p->item;
876 unsigned flags = commit->object.flags;
878 if (flags & BOUNDARY)
879 continue;
881 * If we have fewer left, left_first is set and we omit
882 * commits on the right branch in this loop. If we have
883 * fewer right, we skip the left ones.
885 if (left_first != !!(flags & SYMMETRIC_LEFT))
886 continue;
887 commit->util = add_commit_patch_id(commit, &ids);
890 /* either cherry_mark or cherry_pick are true */
891 cherry_flag = revs->cherry_mark ? PATCHSAME : SHOWN;
893 /* Check the other side */
894 for (p = list; p; p = p->next) {
895 struct commit *commit = p->item;
896 struct patch_id *id;
897 unsigned flags = commit->object.flags;
899 if (flags & BOUNDARY)
900 continue;
902 * If we have fewer left, left_first is set and we omit
903 * commits on the left branch in this loop.
905 if (left_first == !!(flags & SYMMETRIC_LEFT))
906 continue;
909 * Have we seen the same patch id?
911 id = has_commit_patch_id(commit, &ids);
912 if (!id)
913 continue;
914 id->seen = 1;
915 commit->object.flags |= cherry_flag;
918 /* Now check the original side for seen ones */
919 for (p = list; p; p = p->next) {
920 struct commit *commit = p->item;
921 struct patch_id *ent;
923 ent = commit->util;
924 if (!ent)
925 continue;
926 if (ent->seen)
927 commit->object.flags |= cherry_flag;
928 commit->util = NULL;
931 free_patch_ids(&ids);
934 /* How many extra uninteresting commits we want to see.. */
935 #define SLOP 5
937 static int still_interesting(struct commit_list *src, unsigned long date, int slop)
940 * No source list at all? We're definitely done..
942 if (!src)
943 return 0;
946 * Does the destination list contain entries with a date
947 * before the source list? Definitely _not_ done.
949 if (date <= src->item->date)
950 return SLOP;
953 * Does the source list still have interesting commits in
954 * it? Definitely not done..
956 if (!everybody_uninteresting(src))
957 return SLOP;
959 /* Ok, we're closing in.. */
960 return slop-1;
964 * "rev-list --ancestry-path A..B" computes commits that are ancestors
965 * of B but not ancestors of A but further limits the result to those
966 * that are descendants of A. This takes the list of bottom commits and
967 * the result of "A..B" without --ancestry-path, and limits the latter
968 * further to the ones that can reach one of the commits in "bottom".
970 static void limit_to_ancestry(struct commit_list *bottom, struct commit_list *list)
972 struct commit_list *p;
973 struct commit_list *rlist = NULL;
974 int made_progress;
977 * Reverse the list so that it will be likely that we would
978 * process parents before children.
980 for (p = list; p; p = p->next)
981 commit_list_insert(p->item, &rlist);
983 for (p = bottom; p; p = p->next)
984 p->item->object.flags |= TMP_MARK;
987 * Mark the ones that can reach bottom commits in "list",
988 * in a bottom-up fashion.
990 do {
991 made_progress = 0;
992 for (p = rlist; p; p = p->next) {
993 struct commit *c = p->item;
994 struct commit_list *parents;
995 if (c->object.flags & (TMP_MARK | UNINTERESTING))
996 continue;
997 for (parents = c->parents;
998 parents;
999 parents = parents->next) {
1000 if (!(parents->item->object.flags & TMP_MARK))
1001 continue;
1002 c->object.flags |= TMP_MARK;
1003 made_progress = 1;
1004 break;
1007 } while (made_progress);
1010 * NEEDSWORK: decide if we want to remove parents that are
1011 * not marked with TMP_MARK from commit->parents for commits
1012 * in the resulting list. We may not want to do that, though.
1016 * The ones that are not marked with TMP_MARK are uninteresting
1018 for (p = list; p; p = p->next) {
1019 struct commit *c = p->item;
1020 if (c->object.flags & TMP_MARK)
1021 continue;
1022 c->object.flags |= UNINTERESTING;
1025 /* We are done with the TMP_MARK */
1026 for (p = list; p; p = p->next)
1027 p->item->object.flags &= ~TMP_MARK;
1028 for (p = bottom; p; p = p->next)
1029 p->item->object.flags &= ~TMP_MARK;
1030 free_commit_list(rlist);
1034 * Before walking the history, keep the set of "negative" refs the
1035 * caller has asked to exclude.
1037 * This is used to compute "rev-list --ancestry-path A..B", as we need
1038 * to filter the result of "A..B" further to the ones that can actually
1039 * reach A.
1041 static struct commit_list *collect_bottom_commits(struct commit_list *list)
1043 struct commit_list *elem, *bottom = NULL;
1044 for (elem = list; elem; elem = elem->next)
1045 if (elem->item->object.flags & BOTTOM)
1046 commit_list_insert(elem->item, &bottom);
1047 return bottom;
1050 /* Assumes either left_only or right_only is set */
1051 static void limit_left_right(struct commit_list *list, struct rev_info *revs)
1053 struct commit_list *p;
1055 for (p = list; p; p = p->next) {
1056 struct commit *commit = p->item;
1058 if (revs->right_only) {
1059 if (commit->object.flags & SYMMETRIC_LEFT)
1060 commit->object.flags |= SHOWN;
1061 } else /* revs->left_only is set */
1062 if (!(commit->object.flags & SYMMETRIC_LEFT))
1063 commit->object.flags |= SHOWN;
1067 static int limit_list(struct rev_info *revs)
1069 int slop = SLOP;
1070 unsigned long date = ~0ul;
1071 struct commit_list *list = revs->commits;
1072 struct commit_list *newlist = NULL;
1073 struct commit_list **p = &newlist;
1074 struct commit_list *bottom = NULL;
1076 if (revs->ancestry_path) {
1077 bottom = collect_bottom_commits(list);
1078 if (!bottom)
1079 die("--ancestry-path given but there are no bottom commits");
1082 while (list) {
1083 struct commit_list *entry = list;
1084 struct commit *commit = list->item;
1085 struct object *obj = &commit->object;
1086 show_early_output_fn_t show;
1088 list = list->next;
1089 free(entry);
1091 if (revs->max_age != -1 && (commit->date < revs->max_age))
1092 obj->flags |= UNINTERESTING;
1093 if (add_parents_to_list(revs, commit, &list, NULL) < 0)
1094 return -1;
1095 if (obj->flags & UNINTERESTING) {
1096 mark_parents_uninteresting(commit);
1097 if (revs->show_all)
1098 p = &commit_list_insert(commit, p)->next;
1099 slop = still_interesting(list, date, slop);
1100 if (slop)
1101 continue;
1102 /* If showing all, add the whole pending list to the end */
1103 if (revs->show_all)
1104 *p = list;
1105 break;
1107 if (revs->min_age != -1 && (commit->date > revs->min_age))
1108 continue;
1109 date = commit->date;
1110 p = &commit_list_insert(commit, p)->next;
1112 show = show_early_output;
1113 if (!show)
1114 continue;
1116 show(revs, newlist);
1117 show_early_output = NULL;
1119 if (revs->cherry_pick || revs->cherry_mark)
1120 cherry_pick_list(newlist, revs);
1122 if (revs->left_only || revs->right_only)
1123 limit_left_right(newlist, revs);
1125 if (bottom) {
1126 limit_to_ancestry(bottom, newlist);
1127 free_commit_list(bottom);
1131 * Check if any commits have become TREESAME by some of their parents
1132 * becoming UNINTERESTING.
1134 if (limiting_can_increase_treesame(revs))
1135 for (list = newlist; list; list = list->next) {
1136 struct commit *c = list->item;
1137 if (c->object.flags & (UNINTERESTING | TREESAME))
1138 continue;
1139 update_treesame(revs, c);
1142 revs->commits = newlist;
1143 return 0;
1147 * Add an entry to refs->cmdline with the specified information.
1148 * *name is copied.
1150 static void add_rev_cmdline(struct rev_info *revs,
1151 struct object *item,
1152 const char *name,
1153 int whence,
1154 unsigned flags)
1156 struct rev_cmdline_info *info = &revs->cmdline;
1157 int nr = info->nr;
1159 ALLOC_GROW(info->rev, nr + 1, info->alloc);
1160 info->rev[nr].item = item;
1161 info->rev[nr].name = xstrdup(name);
1162 info->rev[nr].whence = whence;
1163 info->rev[nr].flags = flags;
1164 info->nr++;
1167 static void add_rev_cmdline_list(struct rev_info *revs,
1168 struct commit_list *commit_list,
1169 int whence,
1170 unsigned flags)
1172 while (commit_list) {
1173 struct object *object = &commit_list->item->object;
1174 add_rev_cmdline(revs, object, sha1_to_hex(object->sha1),
1175 whence, flags);
1176 commit_list = commit_list->next;
1180 struct all_refs_cb {
1181 int all_flags;
1182 int warned_bad_reflog;
1183 struct rev_info *all_revs;
1184 const char *name_for_errormsg;
1187 static int handle_one_ref(const char *path, const unsigned char *sha1, int flag, void *cb_data)
1189 struct all_refs_cb *cb = cb_data;
1190 struct object *object = get_reference(cb->all_revs, path, sha1,
1191 cb->all_flags);
1192 add_rev_cmdline(cb->all_revs, object, path, REV_CMD_REF, cb->all_flags);
1193 add_pending_sha1(cb->all_revs, path, sha1, cb->all_flags);
1194 return 0;
1197 static void init_all_refs_cb(struct all_refs_cb *cb, struct rev_info *revs,
1198 unsigned flags)
1200 cb->all_revs = revs;
1201 cb->all_flags = flags;
1204 static void handle_refs(const char *submodule, struct rev_info *revs, unsigned flags,
1205 int (*for_each)(const char *, each_ref_fn, void *))
1207 struct all_refs_cb cb;
1208 init_all_refs_cb(&cb, revs, flags);
1209 for_each(submodule, handle_one_ref, &cb);
1212 static void handle_one_reflog_commit(unsigned char *sha1, void *cb_data)
1214 struct all_refs_cb *cb = cb_data;
1215 if (!is_null_sha1(sha1)) {
1216 struct object *o = parse_object(sha1);
1217 if (o) {
1218 o->flags |= cb->all_flags;
1219 /* ??? CMDLINEFLAGS ??? */
1220 add_pending_object(cb->all_revs, o, "");
1222 else if (!cb->warned_bad_reflog) {
1223 warning("reflog of '%s' references pruned commits",
1224 cb->name_for_errormsg);
1225 cb->warned_bad_reflog = 1;
1230 static int handle_one_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
1231 const char *email, unsigned long timestamp, int tz,
1232 const char *message, void *cb_data)
1234 handle_one_reflog_commit(osha1, cb_data);
1235 handle_one_reflog_commit(nsha1, cb_data);
1236 return 0;
1239 static int handle_one_reflog(const char *path, const unsigned char *sha1, int flag, void *cb_data)
1241 struct all_refs_cb *cb = cb_data;
1242 cb->warned_bad_reflog = 0;
1243 cb->name_for_errormsg = path;
1244 for_each_reflog_ent(path, handle_one_reflog_ent, cb_data);
1245 return 0;
1248 static void handle_reflog(struct rev_info *revs, unsigned flags)
1250 struct all_refs_cb cb;
1251 cb.all_revs = revs;
1252 cb.all_flags = flags;
1253 for_each_reflog(handle_one_reflog, &cb);
1256 static int add_parents_only(struct rev_info *revs, const char *arg_, int flags)
1258 unsigned char sha1[20];
1259 struct object *it;
1260 struct commit *commit;
1261 struct commit_list *parents;
1262 const char *arg = arg_;
1264 if (*arg == '^') {
1265 flags ^= UNINTERESTING | BOTTOM;
1266 arg++;
1268 if (get_sha1_committish(arg, sha1))
1269 return 0;
1270 while (1) {
1271 it = get_reference(revs, arg, sha1, 0);
1272 if (!it && revs->ignore_missing)
1273 return 0;
1274 if (it->type != OBJ_TAG)
1275 break;
1276 if (!((struct tag*)it)->tagged)
1277 return 0;
1278 hashcpy(sha1, ((struct tag*)it)->tagged->sha1);
1280 if (it->type != OBJ_COMMIT)
1281 return 0;
1282 commit = (struct commit *)it;
1283 for (parents = commit->parents; parents; parents = parents->next) {
1284 it = &parents->item->object;
1285 it->flags |= flags;
1286 add_rev_cmdline(revs, it, arg_, REV_CMD_PARENTS_ONLY, flags);
1287 add_pending_object(revs, it, arg);
1289 return 1;
1292 void init_revisions(struct rev_info *revs, const char *prefix)
1294 memset(revs, 0, sizeof(*revs));
1296 revs->abbrev = DEFAULT_ABBREV;
1297 revs->ignore_merges = 1;
1298 revs->simplify_history = 1;
1299 DIFF_OPT_SET(&revs->pruning, RECURSIVE);
1300 DIFF_OPT_SET(&revs->pruning, QUICK);
1301 revs->pruning.add_remove = file_add_remove;
1302 revs->pruning.change = file_change;
1303 revs->sort_order = REV_SORT_IN_GRAPH_ORDER;
1304 revs->dense = 1;
1305 revs->prefix = prefix;
1306 revs->max_age = -1;
1307 revs->min_age = -1;
1308 revs->skip_count = -1;
1309 revs->max_count = -1;
1310 revs->max_parents = -1;
1312 revs->commit_format = CMIT_FMT_DEFAULT;
1314 init_grep_defaults();
1315 grep_init(&revs->grep_filter, prefix);
1316 revs->grep_filter.status_only = 1;
1317 revs->grep_filter.regflags = REG_NEWLINE;
1319 diff_setup(&revs->diffopt);
1320 if (prefix && !revs->diffopt.prefix) {
1321 revs->diffopt.prefix = prefix;
1322 revs->diffopt.prefix_length = strlen(prefix);
1325 revs->notes_opt.use_default_notes = -1;
1328 static void add_pending_commit_list(struct rev_info *revs,
1329 struct commit_list *commit_list,
1330 unsigned int flags)
1332 while (commit_list) {
1333 struct object *object = &commit_list->item->object;
1334 object->flags |= flags;
1335 add_pending_object(revs, object, sha1_to_hex(object->sha1));
1336 commit_list = commit_list->next;
1340 static void prepare_show_merge(struct rev_info *revs)
1342 struct commit_list *bases;
1343 struct commit *head, *other;
1344 unsigned char sha1[20];
1345 const char **prune = NULL;
1346 int i, prune_num = 1; /* counting terminating NULL */
1348 if (get_sha1("HEAD", sha1))
1349 die("--merge without HEAD?");
1350 head = lookup_commit_or_die(sha1, "HEAD");
1351 if (get_sha1("MERGE_HEAD", sha1))
1352 die("--merge without MERGE_HEAD?");
1353 other = lookup_commit_or_die(sha1, "MERGE_HEAD");
1354 add_pending_object(revs, &head->object, "HEAD");
1355 add_pending_object(revs, &other->object, "MERGE_HEAD");
1356 bases = get_merge_bases(head, other, 1);
1357 add_rev_cmdline_list(revs, bases, REV_CMD_MERGE_BASE, UNINTERESTING | BOTTOM);
1358 add_pending_commit_list(revs, bases, UNINTERESTING | BOTTOM);
1359 free_commit_list(bases);
1360 head->object.flags |= SYMMETRIC_LEFT;
1362 if (!active_nr)
1363 read_cache();
1364 for (i = 0; i < active_nr; i++) {
1365 const struct cache_entry *ce = active_cache[i];
1366 if (!ce_stage(ce))
1367 continue;
1368 if (ce_path_match(ce, &revs->prune_data)) {
1369 prune_num++;
1370 prune = xrealloc(prune, sizeof(*prune) * prune_num);
1371 prune[prune_num-2] = ce->name;
1372 prune[prune_num-1] = NULL;
1374 while ((i+1 < active_nr) &&
1375 ce_same_name(ce, active_cache[i+1]))
1376 i++;
1378 free_pathspec(&revs->prune_data);
1379 parse_pathspec(&revs->prune_data, PATHSPEC_ALL_MAGIC & ~PATHSPEC_LITERAL,
1380 PATHSPEC_PREFER_FULL | PATHSPEC_LITERAL_PATH, "", prune);
1381 revs->limited = 1;
1384 int handle_revision_arg(const char *arg_, struct rev_info *revs, int flags, unsigned revarg_opt)
1386 struct object_context oc;
1387 char *dotdot;
1388 struct object *object;
1389 unsigned char sha1[20];
1390 int local_flags;
1391 const char *arg = arg_;
1392 int cant_be_filename = revarg_opt & REVARG_CANNOT_BE_FILENAME;
1393 unsigned get_sha1_flags = 0;
1395 flags = flags & UNINTERESTING ? flags | BOTTOM : flags & ~BOTTOM;
1397 dotdot = strstr(arg, "..");
1398 if (dotdot) {
1399 unsigned char from_sha1[20];
1400 const char *next = dotdot + 2;
1401 const char *this = arg;
1402 int symmetric = *next == '.';
1403 unsigned int flags_exclude = flags ^ (UNINTERESTING | BOTTOM);
1404 static const char head_by_default[] = "HEAD";
1405 unsigned int a_flags;
1407 *dotdot = 0;
1408 next += symmetric;
1410 if (!*next)
1411 next = head_by_default;
1412 if (dotdot == arg)
1413 this = head_by_default;
1414 if (this == head_by_default && next == head_by_default &&
1415 !symmetric) {
1417 * Just ".."? That is not a range but the
1418 * pathspec for the parent directory.
1420 if (!cant_be_filename) {
1421 *dotdot = '.';
1422 return -1;
1425 if (!get_sha1_committish(this, from_sha1) &&
1426 !get_sha1_committish(next, sha1)) {
1427 struct object *a_obj, *b_obj;
1429 if (!cant_be_filename) {
1430 *dotdot = '.';
1431 verify_non_filename(revs->prefix, arg);
1434 a_obj = parse_object(from_sha1);
1435 b_obj = parse_object(sha1);
1436 if (!a_obj || !b_obj) {
1437 missing:
1438 if (revs->ignore_missing)
1439 return 0;
1440 die(symmetric
1441 ? "Invalid symmetric difference expression %s"
1442 : "Invalid revision range %s", arg);
1445 if (!symmetric) {
1446 /* just A..B */
1447 a_flags = flags_exclude;
1448 } else {
1449 /* A...B -- find merge bases between the two */
1450 struct commit *a, *b;
1451 struct commit_list *exclude;
1453 a = (a_obj->type == OBJ_COMMIT
1454 ? (struct commit *)a_obj
1455 : lookup_commit_reference(a_obj->sha1));
1456 b = (b_obj->type == OBJ_COMMIT
1457 ? (struct commit *)b_obj
1458 : lookup_commit_reference(b_obj->sha1));
1459 if (!a || !b)
1460 goto missing;
1461 exclude = get_merge_bases(a, b, 1);
1462 add_rev_cmdline_list(revs, exclude,
1463 REV_CMD_MERGE_BASE,
1464 flags_exclude);
1465 add_pending_commit_list(revs, exclude,
1466 flags_exclude);
1467 free_commit_list(exclude);
1469 a_flags = flags | SYMMETRIC_LEFT;
1472 a_obj->flags |= a_flags;
1473 b_obj->flags |= flags;
1474 add_rev_cmdline(revs, a_obj, this,
1475 REV_CMD_LEFT, a_flags);
1476 add_rev_cmdline(revs, b_obj, next,
1477 REV_CMD_RIGHT, flags);
1478 add_pending_object(revs, a_obj, this);
1479 add_pending_object(revs, b_obj, next);
1480 return 0;
1482 *dotdot = '.';
1484 dotdot = strstr(arg, "^@");
1485 if (dotdot && !dotdot[2]) {
1486 *dotdot = 0;
1487 if (add_parents_only(revs, arg, flags))
1488 return 0;
1489 *dotdot = '^';
1491 dotdot = strstr(arg, "^!");
1492 if (dotdot && !dotdot[2]) {
1493 *dotdot = 0;
1494 if (!add_parents_only(revs, arg, flags ^ (UNINTERESTING | BOTTOM)))
1495 *dotdot = '^';
1498 local_flags = 0;
1499 if (*arg == '^') {
1500 local_flags = UNINTERESTING | BOTTOM;
1501 arg++;
1504 if (revarg_opt & REVARG_COMMITTISH)
1505 get_sha1_flags = GET_SHA1_COMMITTISH;
1507 if (get_sha1_with_context(arg, get_sha1_flags, sha1, &oc))
1508 return revs->ignore_missing ? 0 : -1;
1509 if (!cant_be_filename)
1510 verify_non_filename(revs->prefix, arg);
1511 object = get_reference(revs, arg, sha1, flags ^ local_flags);
1512 add_rev_cmdline(revs, object, arg_, REV_CMD_REV, flags ^ local_flags);
1513 add_pending_object_with_mode(revs, object, arg, oc.mode);
1514 return 0;
1517 struct cmdline_pathspec {
1518 int alloc;
1519 int nr;
1520 const char **path;
1523 static void append_prune_data(struct cmdline_pathspec *prune, const char **av)
1525 while (*av) {
1526 ALLOC_GROW(prune->path, prune->nr + 1, prune->alloc);
1527 prune->path[prune->nr++] = *(av++);
1531 static void read_pathspec_from_stdin(struct rev_info *revs, struct strbuf *sb,
1532 struct cmdline_pathspec *prune)
1534 while (strbuf_getwholeline(sb, stdin, '\n') != EOF) {
1535 int len = sb->len;
1536 if (len && sb->buf[len - 1] == '\n')
1537 sb->buf[--len] = '\0';
1538 ALLOC_GROW(prune->path, prune->nr + 1, prune->alloc);
1539 prune->path[prune->nr++] = xstrdup(sb->buf);
1543 static void read_revisions_from_stdin(struct rev_info *revs,
1544 struct cmdline_pathspec *prune)
1546 struct strbuf sb;
1547 int seen_dashdash = 0;
1549 strbuf_init(&sb, 1000);
1550 while (strbuf_getwholeline(&sb, stdin, '\n') != EOF) {
1551 int len = sb.len;
1552 if (len && sb.buf[len - 1] == '\n')
1553 sb.buf[--len] = '\0';
1554 if (!len)
1555 break;
1556 if (sb.buf[0] == '-') {
1557 if (len == 2 && sb.buf[1] == '-') {
1558 seen_dashdash = 1;
1559 break;
1561 die("options not supported in --stdin mode");
1563 if (handle_revision_arg(sb.buf, revs, 0,
1564 REVARG_CANNOT_BE_FILENAME))
1565 die("bad revision '%s'", sb.buf);
1567 if (seen_dashdash)
1568 read_pathspec_from_stdin(revs, &sb, prune);
1569 strbuf_release(&sb);
1572 static void add_grep(struct rev_info *revs, const char *ptn, enum grep_pat_token what)
1574 append_grep_pattern(&revs->grep_filter, ptn, "command line", 0, what);
1577 static void add_header_grep(struct rev_info *revs, enum grep_header_field field, const char *pattern)
1579 append_header_grep_pattern(&revs->grep_filter, field, pattern);
1582 static void add_message_grep(struct rev_info *revs, const char *pattern)
1584 add_grep(revs, pattern, GREP_PATTERN_BODY);
1587 static int handle_revision_opt(struct rev_info *revs, int argc, const char **argv,
1588 int *unkc, const char **unkv)
1590 const char *arg = argv[0];
1591 const char *optarg;
1592 int argcount;
1594 /* pseudo revision arguments */
1595 if (!strcmp(arg, "--all") || !strcmp(arg, "--branches") ||
1596 !strcmp(arg, "--tags") || !strcmp(arg, "--remotes") ||
1597 !strcmp(arg, "--reflog") || !strcmp(arg, "--not") ||
1598 !strcmp(arg, "--no-walk") || !strcmp(arg, "--do-walk") ||
1599 !strcmp(arg, "--bisect") || !prefixcmp(arg, "--glob=") ||
1600 !prefixcmp(arg, "--branches=") || !prefixcmp(arg, "--tags=") ||
1601 !prefixcmp(arg, "--remotes=") || !prefixcmp(arg, "--no-walk="))
1603 unkv[(*unkc)++] = arg;
1604 return 1;
1607 if ((argcount = parse_long_opt("max-count", argv, &optarg))) {
1608 revs->max_count = atoi(optarg);
1609 revs->no_walk = 0;
1610 return argcount;
1611 } else if ((argcount = parse_long_opt("skip", argv, &optarg))) {
1612 revs->skip_count = atoi(optarg);
1613 return argcount;
1614 } else if ((*arg == '-') && isdigit(arg[1])) {
1615 /* accept -<digit>, like traditional "head" */
1616 if (strtol_i(arg + 1, 10, &revs->max_count) < 0 ||
1617 revs->max_count < 0)
1618 die("'%s': not a non-negative integer", arg + 1);
1619 revs->no_walk = 0;
1620 } else if (!strcmp(arg, "-n")) {
1621 if (argc <= 1)
1622 return error("-n requires an argument");
1623 revs->max_count = atoi(argv[1]);
1624 revs->no_walk = 0;
1625 return 2;
1626 } else if (!prefixcmp(arg, "-n")) {
1627 revs->max_count = atoi(arg + 2);
1628 revs->no_walk = 0;
1629 } else if ((argcount = parse_long_opt("max-age", argv, &optarg))) {
1630 revs->max_age = atoi(optarg);
1631 return argcount;
1632 } else if ((argcount = parse_long_opt("since", argv, &optarg))) {
1633 revs->max_age = approxidate(optarg);
1634 return argcount;
1635 } else if ((argcount = parse_long_opt("after", argv, &optarg))) {
1636 revs->max_age = approxidate(optarg);
1637 return argcount;
1638 } else if ((argcount = parse_long_opt("min-age", argv, &optarg))) {
1639 revs->min_age = atoi(optarg);
1640 return argcount;
1641 } else if ((argcount = parse_long_opt("before", argv, &optarg))) {
1642 revs->min_age = approxidate(optarg);
1643 return argcount;
1644 } else if ((argcount = parse_long_opt("until", argv, &optarg))) {
1645 revs->min_age = approxidate(optarg);
1646 return argcount;
1647 } else if (!strcmp(arg, "--first-parent")) {
1648 revs->first_parent_only = 1;
1649 } else if (!strcmp(arg, "--ancestry-path")) {
1650 revs->ancestry_path = 1;
1651 revs->simplify_history = 0;
1652 revs->limited = 1;
1653 } else if (!strcmp(arg, "-g") || !strcmp(arg, "--walk-reflogs")) {
1654 init_reflog_walk(&revs->reflog_info);
1655 } else if (!strcmp(arg, "--default")) {
1656 if (argc <= 1)
1657 return error("bad --default argument");
1658 revs->def = argv[1];
1659 return 2;
1660 } else if (!strcmp(arg, "--merge")) {
1661 revs->show_merge = 1;
1662 } else if (!strcmp(arg, "--topo-order")) {
1663 revs->sort_order = REV_SORT_IN_GRAPH_ORDER;
1664 revs->topo_order = 1;
1665 } else if (!strcmp(arg, "--simplify-merges")) {
1666 revs->simplify_merges = 1;
1667 revs->topo_order = 1;
1668 revs->rewrite_parents = 1;
1669 revs->simplify_history = 0;
1670 revs->limited = 1;
1671 } else if (!strcmp(arg, "--simplify-by-decoration")) {
1672 revs->simplify_merges = 1;
1673 revs->topo_order = 1;
1674 revs->rewrite_parents = 1;
1675 revs->simplify_history = 0;
1676 revs->simplify_by_decoration = 1;
1677 revs->limited = 1;
1678 revs->prune = 1;
1679 load_ref_decorations(DECORATE_SHORT_REFS);
1680 } else if (!strcmp(arg, "--date-order")) {
1681 revs->sort_order = REV_SORT_BY_COMMIT_DATE;
1682 revs->topo_order = 1;
1683 } else if (!strcmp(arg, "--author-date-order")) {
1684 revs->sort_order = REV_SORT_BY_AUTHOR_DATE;
1685 revs->topo_order = 1;
1686 } else if (!prefixcmp(arg, "--early-output")) {
1687 int count = 100;
1688 switch (arg[14]) {
1689 case '=':
1690 count = atoi(arg+15);
1691 /* Fallthrough */
1692 case 0:
1693 revs->topo_order = 1;
1694 revs->early_output = count;
1696 } else if (!strcmp(arg, "--parents")) {
1697 revs->rewrite_parents = 1;
1698 revs->print_parents = 1;
1699 } else if (!strcmp(arg, "--dense")) {
1700 revs->dense = 1;
1701 } else if (!strcmp(arg, "--sparse")) {
1702 revs->dense = 0;
1703 } else if (!strcmp(arg, "--show-all")) {
1704 revs->show_all = 1;
1705 } else if (!strcmp(arg, "--remove-empty")) {
1706 revs->remove_empty_trees = 1;
1707 } else if (!strcmp(arg, "--merges")) {
1708 revs->min_parents = 2;
1709 } else if (!strcmp(arg, "--no-merges")) {
1710 revs->max_parents = 1;
1711 } else if (!prefixcmp(arg, "--min-parents=")) {
1712 revs->min_parents = atoi(arg+14);
1713 } else if (!prefixcmp(arg, "--no-min-parents")) {
1714 revs->min_parents = 0;
1715 } else if (!prefixcmp(arg, "--max-parents=")) {
1716 revs->max_parents = atoi(arg+14);
1717 } else if (!prefixcmp(arg, "--no-max-parents")) {
1718 revs->max_parents = -1;
1719 } else if (!strcmp(arg, "--boundary")) {
1720 revs->boundary = 1;
1721 } else if (!strcmp(arg, "--left-right")) {
1722 revs->left_right = 1;
1723 } else if (!strcmp(arg, "--left-only")) {
1724 if (revs->right_only)
1725 die("--left-only is incompatible with --right-only"
1726 " or --cherry");
1727 revs->left_only = 1;
1728 } else if (!strcmp(arg, "--right-only")) {
1729 if (revs->left_only)
1730 die("--right-only is incompatible with --left-only");
1731 revs->right_only = 1;
1732 } else if (!strcmp(arg, "--cherry")) {
1733 if (revs->left_only)
1734 die("--cherry is incompatible with --left-only");
1735 revs->cherry_mark = 1;
1736 revs->right_only = 1;
1737 revs->max_parents = 1;
1738 revs->limited = 1;
1739 } else if (!strcmp(arg, "--count")) {
1740 revs->count = 1;
1741 } else if (!strcmp(arg, "--cherry-mark")) {
1742 if (revs->cherry_pick)
1743 die("--cherry-mark is incompatible with --cherry-pick");
1744 revs->cherry_mark = 1;
1745 revs->limited = 1; /* needs limit_list() */
1746 } else if (!strcmp(arg, "--cherry-pick")) {
1747 if (revs->cherry_mark)
1748 die("--cherry-pick is incompatible with --cherry-mark");
1749 revs->cherry_pick = 1;
1750 revs->limited = 1;
1751 } else if (!strcmp(arg, "--objects")) {
1752 revs->tag_objects = 1;
1753 revs->tree_objects = 1;
1754 revs->blob_objects = 1;
1755 } else if (!strcmp(arg, "--objects-edge")) {
1756 revs->tag_objects = 1;
1757 revs->tree_objects = 1;
1758 revs->blob_objects = 1;
1759 revs->edge_hint = 1;
1760 } else if (!strcmp(arg, "--verify-objects")) {
1761 revs->tag_objects = 1;
1762 revs->tree_objects = 1;
1763 revs->blob_objects = 1;
1764 revs->verify_objects = 1;
1765 } else if (!strcmp(arg, "--unpacked")) {
1766 revs->unpacked = 1;
1767 } else if (!prefixcmp(arg, "--unpacked=")) {
1768 die("--unpacked=<packfile> no longer supported.");
1769 } else if (!strcmp(arg, "-r")) {
1770 revs->diff = 1;
1771 DIFF_OPT_SET(&revs->diffopt, RECURSIVE);
1772 } else if (!strcmp(arg, "-t")) {
1773 revs->diff = 1;
1774 DIFF_OPT_SET(&revs->diffopt, RECURSIVE);
1775 DIFF_OPT_SET(&revs->diffopt, TREE_IN_RECURSIVE);
1776 } else if (!strcmp(arg, "-m")) {
1777 revs->ignore_merges = 0;
1778 } else if (!strcmp(arg, "-c")) {
1779 revs->diff = 1;
1780 revs->dense_combined_merges = 0;
1781 revs->combine_merges = 1;
1782 } else if (!strcmp(arg, "--cc")) {
1783 revs->diff = 1;
1784 revs->dense_combined_merges = 1;
1785 revs->combine_merges = 1;
1786 } else if (!strcmp(arg, "-v")) {
1787 revs->verbose_header = 1;
1788 } else if (!strcmp(arg, "--pretty")) {
1789 revs->verbose_header = 1;
1790 revs->pretty_given = 1;
1791 get_commit_format(arg+8, revs);
1792 } else if (!prefixcmp(arg, "--pretty=") || !prefixcmp(arg, "--format=")) {
1794 * Detached form ("--pretty X" as opposed to "--pretty=X")
1795 * not allowed, since the argument is optional.
1797 revs->verbose_header = 1;
1798 revs->pretty_given = 1;
1799 get_commit_format(arg+9, revs);
1800 } else if (!strcmp(arg, "--show-notes") || !strcmp(arg, "--notes")) {
1801 revs->show_notes = 1;
1802 revs->show_notes_given = 1;
1803 revs->notes_opt.use_default_notes = 1;
1804 } else if (!strcmp(arg, "--show-signature")) {
1805 revs->show_signature = 1;
1806 } else if (!prefixcmp(arg, "--show-notes=") ||
1807 !prefixcmp(arg, "--notes=")) {
1808 struct strbuf buf = STRBUF_INIT;
1809 revs->show_notes = 1;
1810 revs->show_notes_given = 1;
1811 if (!prefixcmp(arg, "--show-notes")) {
1812 if (revs->notes_opt.use_default_notes < 0)
1813 revs->notes_opt.use_default_notes = 1;
1814 strbuf_addstr(&buf, arg+13);
1816 else
1817 strbuf_addstr(&buf, arg+8);
1818 expand_notes_ref(&buf);
1819 string_list_append(&revs->notes_opt.extra_notes_refs,
1820 strbuf_detach(&buf, NULL));
1821 } else if (!strcmp(arg, "--no-notes")) {
1822 revs->show_notes = 0;
1823 revs->show_notes_given = 1;
1824 revs->notes_opt.use_default_notes = -1;
1825 /* we have been strdup'ing ourselves, so trick
1826 * string_list into free()ing strings */
1827 revs->notes_opt.extra_notes_refs.strdup_strings = 1;
1828 string_list_clear(&revs->notes_opt.extra_notes_refs, 0);
1829 revs->notes_opt.extra_notes_refs.strdup_strings = 0;
1830 } else if (!strcmp(arg, "--standard-notes")) {
1831 revs->show_notes_given = 1;
1832 revs->notes_opt.use_default_notes = 1;
1833 } else if (!strcmp(arg, "--no-standard-notes")) {
1834 revs->notes_opt.use_default_notes = 0;
1835 } else if (!strcmp(arg, "--oneline")) {
1836 revs->verbose_header = 1;
1837 get_commit_format("oneline", revs);
1838 revs->pretty_given = 1;
1839 revs->abbrev_commit = 1;
1840 } else if (!strcmp(arg, "--graph")) {
1841 revs->topo_order = 1;
1842 revs->rewrite_parents = 1;
1843 revs->graph = graph_init(revs);
1844 } else if (!strcmp(arg, "--root")) {
1845 revs->show_root_diff = 1;
1846 } else if (!strcmp(arg, "--no-commit-id")) {
1847 revs->no_commit_id = 1;
1848 } else if (!strcmp(arg, "--always")) {
1849 revs->always_show_header = 1;
1850 } else if (!strcmp(arg, "--no-abbrev")) {
1851 revs->abbrev = 0;
1852 } else if (!strcmp(arg, "--abbrev")) {
1853 revs->abbrev = DEFAULT_ABBREV;
1854 } else if (!prefixcmp(arg, "--abbrev=")) {
1855 revs->abbrev = strtoul(arg + 9, NULL, 10);
1856 if (revs->abbrev < MINIMUM_ABBREV)
1857 revs->abbrev = MINIMUM_ABBREV;
1858 else if (revs->abbrev > 40)
1859 revs->abbrev = 40;
1860 } else if (!strcmp(arg, "--abbrev-commit")) {
1861 revs->abbrev_commit = 1;
1862 revs->abbrev_commit_given = 1;
1863 } else if (!strcmp(arg, "--no-abbrev-commit")) {
1864 revs->abbrev_commit = 0;
1865 } else if (!strcmp(arg, "--full-diff")) {
1866 revs->diff = 1;
1867 revs->full_diff = 1;
1868 } else if (!strcmp(arg, "--full-history")) {
1869 revs->simplify_history = 0;
1870 } else if (!strcmp(arg, "--relative-date")) {
1871 revs->date_mode = DATE_RELATIVE;
1872 revs->date_mode_explicit = 1;
1873 } else if ((argcount = parse_long_opt("date", argv, &optarg))) {
1874 revs->date_mode = parse_date_format(optarg);
1875 revs->date_mode_explicit = 1;
1876 return argcount;
1877 } else if (!strcmp(arg, "--log-size")) {
1878 revs->show_log_size = 1;
1881 * Grepping the commit log
1883 else if ((argcount = parse_long_opt("author", argv, &optarg))) {
1884 add_header_grep(revs, GREP_HEADER_AUTHOR, optarg);
1885 return argcount;
1886 } else if ((argcount = parse_long_opt("committer", argv, &optarg))) {
1887 add_header_grep(revs, GREP_HEADER_COMMITTER, optarg);
1888 return argcount;
1889 } else if ((argcount = parse_long_opt("grep-reflog", argv, &optarg))) {
1890 add_header_grep(revs, GREP_HEADER_REFLOG, optarg);
1891 return argcount;
1892 } else if ((argcount = parse_long_opt("grep", argv, &optarg))) {
1893 add_message_grep(revs, optarg);
1894 return argcount;
1895 } else if (!strcmp(arg, "--grep-debug")) {
1896 revs->grep_filter.debug = 1;
1897 } else if (!strcmp(arg, "--basic-regexp")) {
1898 grep_set_pattern_type_option(GREP_PATTERN_TYPE_BRE, &revs->grep_filter);
1899 } else if (!strcmp(arg, "--extended-regexp") || !strcmp(arg, "-E")) {
1900 grep_set_pattern_type_option(GREP_PATTERN_TYPE_ERE, &revs->grep_filter);
1901 } else if (!strcmp(arg, "--regexp-ignore-case") || !strcmp(arg, "-i")) {
1902 revs->grep_filter.regflags |= REG_ICASE;
1903 DIFF_OPT_SET(&revs->diffopt, PICKAXE_IGNORE_CASE);
1904 } else if (!strcmp(arg, "--fixed-strings") || !strcmp(arg, "-F")) {
1905 grep_set_pattern_type_option(GREP_PATTERN_TYPE_FIXED, &revs->grep_filter);
1906 } else if (!strcmp(arg, "--perl-regexp")) {
1907 grep_set_pattern_type_option(GREP_PATTERN_TYPE_PCRE, &revs->grep_filter);
1908 } else if (!strcmp(arg, "--all-match")) {
1909 revs->grep_filter.all_match = 1;
1910 } else if ((argcount = parse_long_opt("encoding", argv, &optarg))) {
1911 if (strcmp(optarg, "none"))
1912 git_log_output_encoding = xstrdup(optarg);
1913 else
1914 git_log_output_encoding = "";
1915 return argcount;
1916 } else if (!strcmp(arg, "--reverse")) {
1917 revs->reverse ^= 1;
1918 } else if (!strcmp(arg, "--children")) {
1919 revs->children.name = "children";
1920 revs->limited = 1;
1921 } else if (!strcmp(arg, "--ignore-missing")) {
1922 revs->ignore_missing = 1;
1923 } else {
1924 int opts = diff_opt_parse(&revs->diffopt, argv, argc);
1925 if (!opts)
1926 unkv[(*unkc)++] = arg;
1927 return opts;
1930 return 1;
1933 void parse_revision_opt(struct rev_info *revs, struct parse_opt_ctx_t *ctx,
1934 const struct option *options,
1935 const char * const usagestr[])
1937 int n = handle_revision_opt(revs, ctx->argc, ctx->argv,
1938 &ctx->cpidx, ctx->out);
1939 if (n <= 0) {
1940 error("unknown option `%s'", ctx->argv[0]);
1941 usage_with_options(usagestr, options);
1943 ctx->argv += n;
1944 ctx->argc -= n;
1947 static int for_each_bad_bisect_ref(const char *submodule, each_ref_fn fn, void *cb_data)
1949 return for_each_ref_in_submodule(submodule, "refs/bisect/bad", fn, cb_data);
1952 static int for_each_good_bisect_ref(const char *submodule, each_ref_fn fn, void *cb_data)
1954 return for_each_ref_in_submodule(submodule, "refs/bisect/good", fn, cb_data);
1957 static int handle_revision_pseudo_opt(const char *submodule,
1958 struct rev_info *revs,
1959 int argc, const char **argv, int *flags)
1961 const char *arg = argv[0];
1962 const char *optarg;
1963 int argcount;
1966 * NOTE!
1968 * Commands like "git shortlog" will not accept the options below
1969 * unless parse_revision_opt queues them (as opposed to erroring
1970 * out).
1972 * When implementing your new pseudo-option, remember to
1973 * register it in the list at the top of handle_revision_opt.
1975 if (!strcmp(arg, "--all")) {
1976 handle_refs(submodule, revs, *flags, for_each_ref_submodule);
1977 handle_refs(submodule, revs, *flags, head_ref_submodule);
1978 } else if (!strcmp(arg, "--branches")) {
1979 handle_refs(submodule, revs, *flags, for_each_branch_ref_submodule);
1980 } else if (!strcmp(arg, "--bisect")) {
1981 handle_refs(submodule, revs, *flags, for_each_bad_bisect_ref);
1982 handle_refs(submodule, revs, *flags ^ (UNINTERESTING | BOTTOM), for_each_good_bisect_ref);
1983 revs->bisect = 1;
1984 } else if (!strcmp(arg, "--tags")) {
1985 handle_refs(submodule, revs, *flags, for_each_tag_ref_submodule);
1986 } else if (!strcmp(arg, "--remotes")) {
1987 handle_refs(submodule, revs, *flags, for_each_remote_ref_submodule);
1988 } else if ((argcount = parse_long_opt("glob", argv, &optarg))) {
1989 struct all_refs_cb cb;
1990 init_all_refs_cb(&cb, revs, *flags);
1991 for_each_glob_ref(handle_one_ref, optarg, &cb);
1992 return argcount;
1993 } else if (!prefixcmp(arg, "--branches=")) {
1994 struct all_refs_cb cb;
1995 init_all_refs_cb(&cb, revs, *flags);
1996 for_each_glob_ref_in(handle_one_ref, arg + 11, "refs/heads/", &cb);
1997 } else if (!prefixcmp(arg, "--tags=")) {
1998 struct all_refs_cb cb;
1999 init_all_refs_cb(&cb, revs, *flags);
2000 for_each_glob_ref_in(handle_one_ref, arg + 7, "refs/tags/", &cb);
2001 } else if (!prefixcmp(arg, "--remotes=")) {
2002 struct all_refs_cb cb;
2003 init_all_refs_cb(&cb, revs, *flags);
2004 for_each_glob_ref_in(handle_one_ref, arg + 10, "refs/remotes/", &cb);
2005 } else if (!strcmp(arg, "--reflog")) {
2006 handle_reflog(revs, *flags);
2007 } else if (!strcmp(arg, "--not")) {
2008 *flags ^= UNINTERESTING | BOTTOM;
2009 } else if (!strcmp(arg, "--no-walk")) {
2010 revs->no_walk = REVISION_WALK_NO_WALK_SORTED;
2011 } else if (!prefixcmp(arg, "--no-walk=")) {
2013 * Detached form ("--no-walk X" as opposed to "--no-walk=X")
2014 * not allowed, since the argument is optional.
2016 if (!strcmp(arg + 10, "sorted"))
2017 revs->no_walk = REVISION_WALK_NO_WALK_SORTED;
2018 else if (!strcmp(arg + 10, "unsorted"))
2019 revs->no_walk = REVISION_WALK_NO_WALK_UNSORTED;
2020 else
2021 return error("invalid argument to --no-walk");
2022 } else if (!strcmp(arg, "--do-walk")) {
2023 revs->no_walk = 0;
2024 } else {
2025 return 0;
2028 return 1;
2032 * Parse revision information, filling in the "rev_info" structure,
2033 * and removing the used arguments from the argument list.
2035 * Returns the number of arguments left that weren't recognized
2036 * (which are also moved to the head of the argument list)
2038 int setup_revisions(int argc, const char **argv, struct rev_info *revs, struct setup_revision_opt *opt)
2040 int i, flags, left, seen_dashdash, read_from_stdin, got_rev_arg = 0, revarg_opt;
2041 struct cmdline_pathspec prune_data;
2042 const char *submodule = NULL;
2044 memset(&prune_data, 0, sizeof(prune_data));
2045 if (opt)
2046 submodule = opt->submodule;
2048 /* First, search for "--" */
2049 if (opt && opt->assume_dashdash) {
2050 seen_dashdash = 1;
2051 } else {
2052 seen_dashdash = 0;
2053 for (i = 1; i < argc; i++) {
2054 const char *arg = argv[i];
2055 if (strcmp(arg, "--"))
2056 continue;
2057 argv[i] = NULL;
2058 argc = i;
2059 if (argv[i + 1])
2060 append_prune_data(&prune_data, argv + i + 1);
2061 seen_dashdash = 1;
2062 break;
2066 /* Second, deal with arguments and options */
2067 flags = 0;
2068 revarg_opt = opt ? opt->revarg_opt : 0;
2069 if (seen_dashdash)
2070 revarg_opt |= REVARG_CANNOT_BE_FILENAME;
2071 read_from_stdin = 0;
2072 for (left = i = 1; i < argc; i++) {
2073 const char *arg = argv[i];
2074 if (*arg == '-') {
2075 int opts;
2077 opts = handle_revision_pseudo_opt(submodule,
2078 revs, argc - i, argv + i,
2079 &flags);
2080 if (opts > 0) {
2081 i += opts - 1;
2082 continue;
2085 if (!strcmp(arg, "--stdin")) {
2086 if (revs->disable_stdin) {
2087 argv[left++] = arg;
2088 continue;
2090 if (read_from_stdin++)
2091 die("--stdin given twice?");
2092 read_revisions_from_stdin(revs, &prune_data);
2093 continue;
2096 opts = handle_revision_opt(revs, argc - i, argv + i, &left, argv);
2097 if (opts > 0) {
2098 i += opts - 1;
2099 continue;
2101 if (opts < 0)
2102 exit(128);
2103 continue;
2107 if (handle_revision_arg(arg, revs, flags, revarg_opt)) {
2108 int j;
2109 if (seen_dashdash || *arg == '^')
2110 die("bad revision '%s'", arg);
2112 /* If we didn't have a "--":
2113 * (1) all filenames must exist;
2114 * (2) all rev-args must not be interpretable
2115 * as a valid filename.
2116 * but the latter we have checked in the main loop.
2118 for (j = i; j < argc; j++)
2119 verify_filename(revs->prefix, argv[j], j == i);
2121 append_prune_data(&prune_data, argv + i);
2122 break;
2124 else
2125 got_rev_arg = 1;
2128 if (prune_data.nr) {
2130 * If we need to introduce the magic "a lone ':' means no
2131 * pathspec whatsoever", here is the place to do so.
2133 * if (prune_data.nr == 1 && !strcmp(prune_data[0], ":")) {
2134 * prune_data.nr = 0;
2135 * prune_data.alloc = 0;
2136 * free(prune_data.path);
2137 * prune_data.path = NULL;
2138 * } else {
2139 * terminate prune_data.alloc with NULL and
2140 * call init_pathspec() to set revs->prune_data here.
2143 ALLOC_GROW(prune_data.path, prune_data.nr + 1, prune_data.alloc);
2144 prune_data.path[prune_data.nr++] = NULL;
2145 parse_pathspec(&revs->prune_data, 0, 0,
2146 revs->prefix, prune_data.path);
2149 if (revs->def == NULL)
2150 revs->def = opt ? opt->def : NULL;
2151 if (opt && opt->tweak)
2152 opt->tweak(revs, opt);
2153 if (revs->show_merge)
2154 prepare_show_merge(revs);
2155 if (revs->def && !revs->pending.nr && !got_rev_arg) {
2156 unsigned char sha1[20];
2157 struct object *object;
2158 struct object_context oc;
2159 if (get_sha1_with_context(revs->def, 0, sha1, &oc))
2160 die("bad default revision '%s'", revs->def);
2161 object = get_reference(revs, revs->def, sha1, 0);
2162 add_pending_object_with_mode(revs, object, revs->def, oc.mode);
2165 /* Did the user ask for any diff output? Run the diff! */
2166 if (revs->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT)
2167 revs->diff = 1;
2169 /* Pickaxe, diff-filter and rename following need diffs */
2170 if (revs->diffopt.pickaxe ||
2171 revs->diffopt.filter ||
2172 DIFF_OPT_TST(&revs->diffopt, FOLLOW_RENAMES))
2173 revs->diff = 1;
2175 if (revs->topo_order)
2176 revs->limited = 1;
2178 if (revs->prune_data.nr) {
2179 copy_pathspec(&revs->pruning.pathspec, &revs->prune_data);
2180 /* Can't prune commits with rename following: the paths change.. */
2181 if (!DIFF_OPT_TST(&revs->diffopt, FOLLOW_RENAMES))
2182 revs->prune = 1;
2183 if (!revs->full_diff)
2184 copy_pathspec(&revs->diffopt.pathspec,
2185 &revs->prune_data);
2187 if (revs->combine_merges)
2188 revs->ignore_merges = 0;
2189 revs->diffopt.abbrev = revs->abbrev;
2191 if (revs->line_level_traverse) {
2192 revs->limited = 1;
2193 revs->topo_order = 1;
2196 diff_setup_done(&revs->diffopt);
2198 grep_commit_pattern_type(GREP_PATTERN_TYPE_UNSPECIFIED,
2199 &revs->grep_filter);
2200 compile_grep_patterns(&revs->grep_filter);
2202 if (revs->reverse && revs->reflog_info)
2203 die("cannot combine --reverse with --walk-reflogs");
2204 if (revs->rewrite_parents && revs->children.name)
2205 die("cannot combine --parents and --children");
2208 * Limitations on the graph functionality
2210 if (revs->reverse && revs->graph)
2211 die("cannot combine --reverse with --graph");
2213 if (revs->reflog_info && revs->graph)
2214 die("cannot combine --walk-reflogs with --graph");
2215 if (!revs->reflog_info && revs->grep_filter.use_reflog_filter)
2216 die("cannot use --grep-reflog without --walk-reflogs");
2218 return left;
2221 static void add_child(struct rev_info *revs, struct commit *parent, struct commit *child)
2223 struct commit_list *l = xcalloc(1, sizeof(*l));
2225 l->item = child;
2226 l->next = add_decoration(&revs->children, &parent->object, l);
2229 static int remove_duplicate_parents(struct rev_info *revs, struct commit *commit)
2231 struct treesame_state *ts = lookup_decoration(&revs->treesame, &commit->object);
2232 struct commit_list **pp, *p;
2233 int surviving_parents;
2235 /* Examine existing parents while marking ones we have seen... */
2236 pp = &commit->parents;
2237 surviving_parents = 0;
2238 while ((p = *pp) != NULL) {
2239 struct commit *parent = p->item;
2240 if (parent->object.flags & TMP_MARK) {
2241 *pp = p->next;
2242 if (ts)
2243 compact_treesame(revs, commit, surviving_parents);
2244 continue;
2246 parent->object.flags |= TMP_MARK;
2247 surviving_parents++;
2248 pp = &p->next;
2250 /* clear the temporary mark */
2251 for (p = commit->parents; p; p = p->next) {
2252 p->item->object.flags &= ~TMP_MARK;
2254 /* no update_treesame() - removing duplicates can't affect TREESAME */
2255 return surviving_parents;
2258 struct merge_simplify_state {
2259 struct commit *simplified;
2262 static struct merge_simplify_state *locate_simplify_state(struct rev_info *revs, struct commit *commit)
2264 struct merge_simplify_state *st;
2266 st = lookup_decoration(&revs->merge_simplification, &commit->object);
2267 if (!st) {
2268 st = xcalloc(1, sizeof(*st));
2269 add_decoration(&revs->merge_simplification, &commit->object, st);
2271 return st;
2274 static int mark_redundant_parents(struct rev_info *revs, struct commit *commit)
2276 struct commit_list *h = reduce_heads(commit->parents);
2277 int i = 0, marked = 0;
2278 struct commit_list *po, *pn;
2280 /* Want these for sanity-checking only */
2281 int orig_cnt = commit_list_count(commit->parents);
2282 int cnt = commit_list_count(h);
2285 * Not ready to remove items yet, just mark them for now, based
2286 * on the output of reduce_heads(). reduce_heads outputs the reduced
2287 * set in its original order, so this isn't too hard.
2289 po = commit->parents;
2290 pn = h;
2291 while (po) {
2292 if (pn && po->item == pn->item) {
2293 pn = pn->next;
2294 i++;
2295 } else {
2296 po->item->object.flags |= TMP_MARK;
2297 marked++;
2299 po=po->next;
2302 if (i != cnt || cnt+marked != orig_cnt)
2303 die("mark_redundant_parents %d %d %d %d", orig_cnt, cnt, i, marked);
2305 free_commit_list(h);
2307 return marked;
2310 static int mark_treesame_root_parents(struct rev_info *revs, struct commit *commit)
2312 struct commit_list *p;
2313 int marked = 0;
2315 for (p = commit->parents; p; p = p->next) {
2316 struct commit *parent = p->item;
2317 if (!parent->parents && (parent->object.flags & TREESAME)) {
2318 parent->object.flags |= TMP_MARK;
2319 marked++;
2323 return marked;
2327 * Awkward naming - this means one parent we are TREESAME to.
2328 * cf mark_treesame_root_parents: root parents that are TREESAME (to an
2329 * empty tree). Better name suggestions?
2331 static int leave_one_treesame_to_parent(struct rev_info *revs, struct commit *commit)
2333 struct treesame_state *ts = lookup_decoration(&revs->treesame, &commit->object);
2334 struct commit *unmarked = NULL, *marked = NULL;
2335 struct commit_list *p;
2336 unsigned n;
2338 for (p = commit->parents, n = 0; p; p = p->next, n++) {
2339 if (ts->treesame[n]) {
2340 if (p->item->object.flags & TMP_MARK) {
2341 if (!marked)
2342 marked = p->item;
2343 } else {
2344 if (!unmarked) {
2345 unmarked = p->item;
2346 break;
2353 * If we are TREESAME to a marked-for-deletion parent, but not to any
2354 * unmarked parents, unmark the first TREESAME parent. This is the
2355 * parent that the default simplify_history==1 scan would have followed,
2356 * and it doesn't make sense to omit that path when asking for a
2357 * simplified full history. Retaining it improves the chances of
2358 * understanding odd missed merges that took an old version of a file.
2360 * Example:
2362 * I--------*X A modified the file, but mainline merge X used
2363 * \ / "-s ours", so took the version from I. X is
2364 * `-*A--' TREESAME to I and !TREESAME to A.
2366 * Default log from X would produce "I". Without this check,
2367 * --full-history --simplify-merges would produce "I-A-X", showing
2368 * the merge commit X and that it changed A, but not making clear that
2369 * it had just taken the I version. With this check, the topology above
2370 * is retained.
2372 * Note that it is possible that the simplification chooses a different
2373 * TREESAME parent from the default, in which case this test doesn't
2374 * activate, and we _do_ drop the default parent. Example:
2376 * I------X A modified the file, but it was reverted in B,
2377 * \ / meaning mainline merge X is TREESAME to both
2378 * *A-*B parents.
2380 * Default log would produce "I" by following the first parent;
2381 * --full-history --simplify-merges will produce "I-A-B". But this is a
2382 * reasonable result - it presents a logical full history leading from
2383 * I to X, and X is not an important merge.
2385 if (!unmarked && marked) {
2386 marked->object.flags &= ~TMP_MARK;
2387 return 1;
2390 return 0;
2393 static int remove_marked_parents(struct rev_info *revs, struct commit *commit)
2395 struct commit_list **pp, *p;
2396 int nth_parent, removed = 0;
2398 pp = &commit->parents;
2399 nth_parent = 0;
2400 while ((p = *pp) != NULL) {
2401 struct commit *parent = p->item;
2402 if (parent->object.flags & TMP_MARK) {
2403 parent->object.flags &= ~TMP_MARK;
2404 *pp = p->next;
2405 free(p);
2406 removed++;
2407 compact_treesame(revs, commit, nth_parent);
2408 continue;
2410 pp = &p->next;
2411 nth_parent++;
2414 /* Removing parents can only increase TREESAMEness */
2415 if (removed && !(commit->object.flags & TREESAME))
2416 update_treesame(revs, commit);
2418 return nth_parent;
2421 static struct commit_list **simplify_one(struct rev_info *revs, struct commit *commit, struct commit_list **tail)
2423 struct commit_list *p;
2424 struct commit *parent;
2425 struct merge_simplify_state *st, *pst;
2426 int cnt;
2428 st = locate_simplify_state(revs, commit);
2431 * Have we handled this one?
2433 if (st->simplified)
2434 return tail;
2437 * An UNINTERESTING commit simplifies to itself, so does a
2438 * root commit. We do not rewrite parents of such commit
2439 * anyway.
2441 if ((commit->object.flags & UNINTERESTING) || !commit->parents) {
2442 st->simplified = commit;
2443 return tail;
2447 * Do we know what commit all of our parents that matter
2448 * should be rewritten to? Otherwise we are not ready to
2449 * rewrite this one yet.
2451 for (cnt = 0, p = commit->parents; p; p = p->next) {
2452 pst = locate_simplify_state(revs, p->item);
2453 if (!pst->simplified) {
2454 tail = &commit_list_insert(p->item, tail)->next;
2455 cnt++;
2457 if (revs->first_parent_only)
2458 break;
2460 if (cnt) {
2461 tail = &commit_list_insert(commit, tail)->next;
2462 return tail;
2466 * Rewrite our list of parents. Note that this cannot
2467 * affect our TREESAME flags in any way - a commit is
2468 * always TREESAME to its simplification.
2470 for (p = commit->parents; p; p = p->next) {
2471 pst = locate_simplify_state(revs, p->item);
2472 p->item = pst->simplified;
2473 if (revs->first_parent_only)
2474 break;
2477 if (revs->first_parent_only)
2478 cnt = 1;
2479 else
2480 cnt = remove_duplicate_parents(revs, commit);
2483 * It is possible that we are a merge and one side branch
2484 * does not have any commit that touches the given paths;
2485 * in such a case, the immediate parent from that branch
2486 * will be rewritten to be the merge base.
2488 * o----X X: the commit we are looking at;
2489 * / / o: a commit that touches the paths;
2490 * ---o----'
2492 * Further, a merge of an independent branch that doesn't
2493 * touch the path will reduce to a treesame root parent:
2495 * ----o----X X: the commit we are looking at;
2496 * / o: a commit that touches the paths;
2497 * r r: a root commit not touching the paths
2499 * Detect and simplify both cases.
2501 if (1 < cnt) {
2502 int marked = mark_redundant_parents(revs, commit);
2503 marked += mark_treesame_root_parents(revs, commit);
2504 if (marked)
2505 marked -= leave_one_treesame_to_parent(revs, commit);
2506 if (marked)
2507 cnt = remove_marked_parents(revs, commit);
2511 * A commit simplifies to itself if it is a root, if it is
2512 * UNINTERESTING, if it touches the given paths, or if it is a
2513 * merge and its parents don't simplify to one relevant commit
2514 * (the first two cases are already handled at the beginning of
2515 * this function).
2517 * Otherwise, it simplifies to what its sole relevant parent
2518 * simplifies to.
2520 if (!cnt ||
2521 (commit->object.flags & UNINTERESTING) ||
2522 !(commit->object.flags & TREESAME) ||
2523 (parent = one_relevant_parent(revs, commit->parents)) == NULL)
2524 st->simplified = commit;
2525 else {
2526 pst = locate_simplify_state(revs, parent);
2527 st->simplified = pst->simplified;
2529 return tail;
2532 static void simplify_merges(struct rev_info *revs)
2534 struct commit_list *list, *next;
2535 struct commit_list *yet_to_do, **tail;
2536 struct commit *commit;
2538 if (!revs->prune)
2539 return;
2541 /* feed the list reversed */
2542 yet_to_do = NULL;
2543 for (list = revs->commits; list; list = next) {
2544 commit = list->item;
2545 next = list->next;
2547 * Do not free(list) here yet; the original list
2548 * is used later in this function.
2550 commit_list_insert(commit, &yet_to_do);
2552 while (yet_to_do) {
2553 list = yet_to_do;
2554 yet_to_do = NULL;
2555 tail = &yet_to_do;
2556 while (list) {
2557 commit = list->item;
2558 next = list->next;
2559 free(list);
2560 list = next;
2561 tail = simplify_one(revs, commit, tail);
2565 /* clean up the result, removing the simplified ones */
2566 list = revs->commits;
2567 revs->commits = NULL;
2568 tail = &revs->commits;
2569 while (list) {
2570 struct merge_simplify_state *st;
2572 commit = list->item;
2573 next = list->next;
2574 free(list);
2575 list = next;
2576 st = locate_simplify_state(revs, commit);
2577 if (st->simplified == commit)
2578 tail = &commit_list_insert(commit, tail)->next;
2582 static void set_children(struct rev_info *revs)
2584 struct commit_list *l;
2585 for (l = revs->commits; l; l = l->next) {
2586 struct commit *commit = l->item;
2587 struct commit_list *p;
2589 for (p = commit->parents; p; p = p->next)
2590 add_child(revs, p->item, commit);
2594 void reset_revision_walk(void)
2596 clear_object_flags(SEEN | ADDED | SHOWN);
2599 int prepare_revision_walk(struct rev_info *revs)
2601 int nr = revs->pending.nr;
2602 struct object_array_entry *e, *list;
2603 struct commit_list **next = &revs->commits;
2605 e = list = revs->pending.objects;
2606 revs->pending.nr = 0;
2607 revs->pending.alloc = 0;
2608 revs->pending.objects = NULL;
2609 while (--nr >= 0) {
2610 struct commit *commit = handle_commit(revs, e->item, e->name);
2611 if (commit) {
2612 if (!(commit->object.flags & SEEN)) {
2613 commit->object.flags |= SEEN;
2614 next = commit_list_append(commit, next);
2617 e++;
2619 if (!revs->leak_pending)
2620 free(list);
2622 /* Signal whether we need per-parent treesame decoration */
2623 if (revs->simplify_merges ||
2624 (revs->limited && limiting_can_increase_treesame(revs)))
2625 revs->treesame.name = "treesame";
2627 if (revs->no_walk != REVISION_WALK_NO_WALK_UNSORTED)
2628 commit_list_sort_by_date(&revs->commits);
2629 if (revs->no_walk)
2630 return 0;
2631 if (revs->limited)
2632 if (limit_list(revs) < 0)
2633 return -1;
2634 if (revs->topo_order)
2635 sort_in_topological_order(&revs->commits, revs->sort_order);
2636 if (revs->line_level_traverse)
2637 line_log_filter(revs);
2638 if (revs->simplify_merges)
2639 simplify_merges(revs);
2640 if (revs->children.name)
2641 set_children(revs);
2642 return 0;
2645 static enum rewrite_result rewrite_one(struct rev_info *revs, struct commit **pp)
2647 struct commit_list *cache = NULL;
2649 for (;;) {
2650 struct commit *p = *pp;
2651 if (!revs->limited)
2652 if (add_parents_to_list(revs, p, &revs->commits, &cache) < 0)
2653 return rewrite_one_error;
2654 if (p->object.flags & UNINTERESTING)
2655 return rewrite_one_ok;
2656 if (!(p->object.flags & TREESAME))
2657 return rewrite_one_ok;
2658 if (!p->parents)
2659 return rewrite_one_noparents;
2660 if ((p = one_relevant_parent(revs, p->parents)) == NULL)
2661 return rewrite_one_ok;
2662 *pp = p;
2666 int rewrite_parents(struct rev_info *revs, struct commit *commit,
2667 rewrite_parent_fn_t rewrite_parent)
2669 struct commit_list **pp = &commit->parents;
2670 while (*pp) {
2671 struct commit_list *parent = *pp;
2672 switch (rewrite_parent(revs, &parent->item)) {
2673 case rewrite_one_ok:
2674 break;
2675 case rewrite_one_noparents:
2676 *pp = parent->next;
2677 continue;
2678 case rewrite_one_error:
2679 return -1;
2681 pp = &parent->next;
2683 remove_duplicate_parents(revs, commit);
2684 return 0;
2687 static int commit_rewrite_person(struct strbuf *buf, const char *what, struct string_list *mailmap)
2689 char *person, *endp;
2690 size_t len, namelen, maillen;
2691 const char *name;
2692 const char *mail;
2693 struct ident_split ident;
2695 person = strstr(buf->buf, what);
2696 if (!person)
2697 return 0;
2699 person += strlen(what);
2700 endp = strchr(person, '\n');
2701 if (!endp)
2702 return 0;
2704 len = endp - person;
2706 if (split_ident_line(&ident, person, len))
2707 return 0;
2709 mail = ident.mail_begin;
2710 maillen = ident.mail_end - ident.mail_begin;
2711 name = ident.name_begin;
2712 namelen = ident.name_end - ident.name_begin;
2714 if (map_user(mailmap, &mail, &maillen, &name, &namelen)) {
2715 struct strbuf namemail = STRBUF_INIT;
2717 strbuf_addf(&namemail, "%.*s <%.*s>",
2718 (int)namelen, name, (int)maillen, mail);
2720 strbuf_splice(buf, ident.name_begin - buf->buf,
2721 ident.mail_end - ident.name_begin + 1,
2722 namemail.buf, namemail.len);
2724 strbuf_release(&namemail);
2726 return 1;
2729 return 0;
2732 static int commit_match(struct commit *commit, struct rev_info *opt)
2734 int retval;
2735 const char *encoding;
2736 char *message;
2737 struct strbuf buf = STRBUF_INIT;
2739 if (!opt->grep_filter.pattern_list && !opt->grep_filter.header_list)
2740 return 1;
2742 /* Prepend "fake" headers as needed */
2743 if (opt->grep_filter.use_reflog_filter) {
2744 strbuf_addstr(&buf, "reflog ");
2745 get_reflog_message(&buf, opt->reflog_info);
2746 strbuf_addch(&buf, '\n');
2750 * We grep in the user's output encoding, under the assumption that it
2751 * is the encoding they are most likely to write their grep pattern
2752 * for. In addition, it means we will match the "notes" encoding below,
2753 * so we will not end up with a buffer that has two different encodings
2754 * in it.
2756 encoding = get_log_output_encoding();
2757 message = logmsg_reencode(commit, NULL, encoding);
2759 /* Copy the commit to temporary if we are using "fake" headers */
2760 if (buf.len)
2761 strbuf_addstr(&buf, message);
2763 if (opt->grep_filter.header_list && opt->mailmap) {
2764 if (!buf.len)
2765 strbuf_addstr(&buf, message);
2767 commit_rewrite_person(&buf, "\nauthor ", opt->mailmap);
2768 commit_rewrite_person(&buf, "\ncommitter ", opt->mailmap);
2771 /* Append "fake" message parts as needed */
2772 if (opt->show_notes) {
2773 if (!buf.len)
2774 strbuf_addstr(&buf, message);
2775 format_display_notes(commit->object.sha1, &buf, encoding, 1);
2778 /* Find either in the original commit message, or in the temporary */
2779 if (buf.len)
2780 retval = grep_buffer(&opt->grep_filter, buf.buf, buf.len);
2781 else
2782 retval = grep_buffer(&opt->grep_filter,
2783 message, strlen(message));
2784 strbuf_release(&buf);
2785 logmsg_free(message, commit);
2786 return retval;
2789 static inline int want_ancestry(const struct rev_info *revs)
2791 return (revs->rewrite_parents || revs->children.name);
2794 enum commit_action get_commit_action(struct rev_info *revs, struct commit *commit)
2796 if (commit->object.flags & SHOWN)
2797 return commit_ignore;
2798 if (revs->unpacked && has_sha1_pack(commit->object.sha1))
2799 return commit_ignore;
2800 if (revs->show_all)
2801 return commit_show;
2802 if (commit->object.flags & UNINTERESTING)
2803 return commit_ignore;
2804 if (revs->min_age != -1 && (commit->date > revs->min_age))
2805 return commit_ignore;
2806 if (revs->min_parents || (revs->max_parents >= 0)) {
2807 int n = commit_list_count(commit->parents);
2808 if ((n < revs->min_parents) ||
2809 ((revs->max_parents >= 0) && (n > revs->max_parents)))
2810 return commit_ignore;
2812 if (!commit_match(commit, revs))
2813 return commit_ignore;
2814 if (revs->prune && revs->dense) {
2815 /* Commit without changes? */
2816 if (commit->object.flags & TREESAME) {
2817 int n;
2818 struct commit_list *p;
2819 /* drop merges unless we want parenthood */
2820 if (!want_ancestry(revs))
2821 return commit_ignore;
2823 * If we want ancestry, then need to keep any merges
2824 * between relevant commits to tie together topology.
2825 * For consistency with TREESAME and simplification
2826 * use "relevant" here rather than just INTERESTING,
2827 * to treat bottom commit(s) as part of the topology.
2829 for (n = 0, p = commit->parents; p; p = p->next)
2830 if (relevant_commit(p->item))
2831 if (++n >= 2)
2832 return commit_show;
2833 return commit_ignore;
2836 return commit_show;
2839 enum commit_action simplify_commit(struct rev_info *revs, struct commit *commit)
2841 enum commit_action action = get_commit_action(revs, commit);
2843 if (action == commit_show &&
2844 !revs->show_all &&
2845 revs->prune && revs->dense && want_ancestry(revs)) {
2847 * --full-diff on simplified parents is no good: it
2848 * will show spurious changes from the commits that
2849 * were elided. So we save the parents on the side
2850 * when --full-diff is in effect.
2852 if (revs->full_diff)
2853 save_parents(revs, commit);
2854 if (rewrite_parents(revs, commit, rewrite_one) < 0)
2855 return commit_error;
2857 return action;
2860 static struct commit *get_revision_1(struct rev_info *revs)
2862 if (!revs->commits)
2863 return NULL;
2865 do {
2866 struct commit_list *entry = revs->commits;
2867 struct commit *commit = entry->item;
2869 revs->commits = entry->next;
2870 free(entry);
2872 if (revs->reflog_info) {
2873 save_parents(revs, commit);
2874 fake_reflog_parent(revs->reflog_info, commit);
2875 commit->object.flags &= ~(ADDED | SEEN | SHOWN);
2879 * If we haven't done the list limiting, we need to look at
2880 * the parents here. We also need to do the date-based limiting
2881 * that we'd otherwise have done in limit_list().
2883 if (!revs->limited) {
2884 if (revs->max_age != -1 &&
2885 (commit->date < revs->max_age))
2886 continue;
2887 if (add_parents_to_list(revs, commit, &revs->commits, NULL) < 0)
2888 die("Failed to traverse parents of commit %s",
2889 sha1_to_hex(commit->object.sha1));
2892 switch (simplify_commit(revs, commit)) {
2893 case commit_ignore:
2894 continue;
2895 case commit_error:
2896 die("Failed to simplify parents of commit %s",
2897 sha1_to_hex(commit->object.sha1));
2898 default:
2899 return commit;
2901 } while (revs->commits);
2902 return NULL;
2906 * Return true for entries that have not yet been shown. (This is an
2907 * object_array_each_func_t.)
2909 static int entry_unshown(struct object_array_entry *entry, void *cb_data_unused)
2911 return !(entry->item->flags & SHOWN);
2915 * If array is on the verge of a realloc, garbage-collect any entries
2916 * that have already been shown to try to free up some space.
2918 static void gc_boundary(struct object_array *array)
2920 if (array->nr == array->alloc)
2921 object_array_filter(array, entry_unshown, NULL);
2924 static void create_boundary_commit_list(struct rev_info *revs)
2926 unsigned i;
2927 struct commit *c;
2928 struct object_array *array = &revs->boundary_commits;
2929 struct object_array_entry *objects = array->objects;
2932 * If revs->commits is non-NULL at this point, an error occurred in
2933 * get_revision_1(). Ignore the error and continue printing the
2934 * boundary commits anyway. (This is what the code has always
2935 * done.)
2937 if (revs->commits) {
2938 free_commit_list(revs->commits);
2939 revs->commits = NULL;
2943 * Put all of the actual boundary commits from revs->boundary_commits
2944 * into revs->commits
2946 for (i = 0; i < array->nr; i++) {
2947 c = (struct commit *)(objects[i].item);
2948 if (!c)
2949 continue;
2950 if (!(c->object.flags & CHILD_SHOWN))
2951 continue;
2952 if (c->object.flags & (SHOWN | BOUNDARY))
2953 continue;
2954 c->object.flags |= BOUNDARY;
2955 commit_list_insert(c, &revs->commits);
2959 * If revs->topo_order is set, sort the boundary commits
2960 * in topological order
2962 sort_in_topological_order(&revs->commits, revs->sort_order);
2965 static struct commit *get_revision_internal(struct rev_info *revs)
2967 struct commit *c = NULL;
2968 struct commit_list *l;
2970 if (revs->boundary == 2) {
2972 * All of the normal commits have already been returned,
2973 * and we are now returning boundary commits.
2974 * create_boundary_commit_list() has populated
2975 * revs->commits with the remaining commits to return.
2977 c = pop_commit(&revs->commits);
2978 if (c)
2979 c->object.flags |= SHOWN;
2980 return c;
2984 * If our max_count counter has reached zero, then we are done. We
2985 * don't simply return NULL because we still might need to show
2986 * boundary commits. But we want to avoid calling get_revision_1, which
2987 * might do a considerable amount of work finding the next commit only
2988 * for us to throw it away.
2990 * If it is non-zero, then either we don't have a max_count at all
2991 * (-1), or it is still counting, in which case we decrement.
2993 if (revs->max_count) {
2994 c = get_revision_1(revs);
2995 if (c) {
2996 while (revs->skip_count > 0) {
2997 revs->skip_count--;
2998 c = get_revision_1(revs);
2999 if (!c)
3000 break;
3004 if (revs->max_count > 0)
3005 revs->max_count--;
3008 if (c)
3009 c->object.flags |= SHOWN;
3011 if (!revs->boundary)
3012 return c;
3014 if (!c) {
3016 * get_revision_1() runs out the commits, and
3017 * we are done computing the boundaries.
3018 * switch to boundary commits output mode.
3020 revs->boundary = 2;
3023 * Update revs->commits to contain the list of
3024 * boundary commits.
3026 create_boundary_commit_list(revs);
3028 return get_revision_internal(revs);
3032 * boundary commits are the commits that are parents of the
3033 * ones we got from get_revision_1() but they themselves are
3034 * not returned from get_revision_1(). Before returning
3035 * 'c', we need to mark its parents that they could be boundaries.
3038 for (l = c->parents; l; l = l->next) {
3039 struct object *p;
3040 p = &(l->item->object);
3041 if (p->flags & (CHILD_SHOWN | SHOWN))
3042 continue;
3043 p->flags |= CHILD_SHOWN;
3044 gc_boundary(&revs->boundary_commits);
3045 add_object_array(p, NULL, &revs->boundary_commits);
3048 return c;
3051 struct commit *get_revision(struct rev_info *revs)
3053 struct commit *c;
3054 struct commit_list *reversed;
3056 if (revs->reverse) {
3057 reversed = NULL;
3058 while ((c = get_revision_internal(revs)))
3059 commit_list_insert(c, &reversed);
3060 revs->commits = reversed;
3061 revs->reverse = 0;
3062 revs->reverse_output_stage = 1;
3065 if (revs->reverse_output_stage)
3066 return pop_commit(&revs->commits);
3068 c = get_revision_internal(revs);
3069 if (c && revs->graph)
3070 graph_update(revs->graph, c);
3071 if (!c)
3072 free_saved_parents(revs);
3073 return c;
3076 char *get_revision_mark(const struct rev_info *revs, const struct commit *commit)
3078 if (commit->object.flags & BOUNDARY)
3079 return "-";
3080 else if (commit->object.flags & UNINTERESTING)
3081 return "^";
3082 else if (commit->object.flags & PATCHSAME)
3083 return "=";
3084 else if (!revs || revs->left_right) {
3085 if (commit->object.flags & SYMMETRIC_LEFT)
3086 return "<";
3087 else
3088 return ">";
3089 } else if (revs->graph)
3090 return "*";
3091 else if (revs->cherry_mark)
3092 return "+";
3093 return "";
3096 void put_revision_mark(const struct rev_info *revs, const struct commit *commit)
3098 char *mark = get_revision_mark(revs, commit);
3099 if (!strlen(mark))
3100 return;
3101 fputs(mark, stdout);
3102 putchar(' ');
3105 define_commit_slab(saved_parents, struct commit_list *);
3107 #define EMPTY_PARENT_LIST ((struct commit_list *)-1)
3109 void save_parents(struct rev_info *revs, struct commit *commit)
3111 struct commit_list **pp;
3113 if (!revs->saved_parents_slab) {
3114 revs->saved_parents_slab = xmalloc(sizeof(struct saved_parents));
3115 init_saved_parents(revs->saved_parents_slab);
3118 pp = saved_parents_at(revs->saved_parents_slab, commit);
3121 * When walking with reflogs, we may visit the same commit
3122 * several times: once for each appearance in the reflog.
3124 * In this case, save_parents() will be called multiple times.
3125 * We want to keep only the first set of parents. We need to
3126 * store a sentinel value for an empty (i.e., NULL) parent
3127 * list to distinguish it from a not-yet-saved list, however.
3129 if (*pp)
3130 return;
3131 if (commit->parents)
3132 *pp = copy_commit_list(commit->parents);
3133 else
3134 *pp = EMPTY_PARENT_LIST;
3137 struct commit_list *get_saved_parents(struct rev_info *revs, const struct commit *commit)
3139 struct commit_list *parents;
3141 if (!revs->saved_parents_slab)
3142 return commit->parents;
3144 parents = *saved_parents_at(revs->saved_parents_slab, commit);
3145 if (parents == EMPTY_PARENT_LIST)
3146 return NULL;
3147 return parents;
3150 void free_saved_parents(struct rev_info *revs)
3152 if (revs->saved_parents_slab)
3153 clear_saved_parents(revs->saved_parents_slab);