ewah: support platforms that require aligned reads
[git/gitweb.git] / revision.c
blobcddd6051e307cf5455e41cdd404008d5a1967f84
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 void mark_tree_uninteresting(struct tree *tree)
109 struct tree_desc desc;
110 struct name_entry entry;
111 struct object *obj = &tree->object;
113 if (!tree)
114 return;
115 if (obj->flags & UNINTERESTING)
116 return;
117 obj->flags |= UNINTERESTING;
118 if (!has_sha1_file(obj->sha1))
119 return;
120 if (parse_tree(tree) < 0)
121 die("bad tree %s", sha1_to_hex(obj->sha1));
123 init_tree_desc(&desc, tree->buffer, tree->size);
124 while (tree_entry(&desc, &entry)) {
125 switch (object_type(entry.mode)) {
126 case OBJ_TREE:
127 mark_tree_uninteresting(lookup_tree(entry.sha1));
128 break;
129 case OBJ_BLOB:
130 mark_blob_uninteresting(lookup_blob(entry.sha1));
131 break;
132 default:
133 /* Subproject commit - not in this repository */
134 break;
139 * We don't care about the tree any more
140 * after it has been marked uninteresting.
142 free_tree_buffer(tree);
145 void mark_parents_uninteresting(struct commit *commit)
147 struct commit_list *parents = NULL, *l;
149 for (l = commit->parents; l; l = l->next)
150 commit_list_insert(l->item, &parents);
152 while (parents) {
153 struct commit *commit = parents->item;
154 l = parents;
155 parents = parents->next;
156 free(l);
158 while (commit) {
160 * A missing commit is ok iff its parent is marked
161 * uninteresting.
163 * We just mark such a thing parsed, so that when
164 * it is popped next time around, we won't be trying
165 * to parse it and get an error.
167 if (!has_sha1_file(commit->object.sha1))
168 commit->object.parsed = 1;
170 if (commit->object.flags & UNINTERESTING)
171 break;
173 commit->object.flags |= UNINTERESTING;
176 * Normally we haven't parsed the parent
177 * yet, so we won't have a parent of a parent
178 * here. However, it may turn out that we've
179 * reached this commit some other way (where it
180 * wasn't uninteresting), in which case we need
181 * to mark its parents recursively too..
183 if (!commit->parents)
184 break;
186 for (l = commit->parents->next; l; l = l->next)
187 commit_list_insert(l->item, &parents);
188 commit = commit->parents->item;
193 static void add_pending_object_with_mode(struct rev_info *revs,
194 struct object *obj,
195 const char *name, unsigned mode)
197 if (!obj)
198 return;
199 if (revs->no_walk && (obj->flags & UNINTERESTING))
200 revs->no_walk = 0;
201 if (revs->reflog_info && obj->type == OBJ_COMMIT) {
202 struct strbuf buf = STRBUF_INIT;
203 int len = interpret_branch_name(name, 0, &buf);
204 int st;
206 if (0 < len && name[len] && buf.len)
207 strbuf_addstr(&buf, name + len);
208 st = add_reflog_for_walk(revs->reflog_info,
209 (struct commit *)obj,
210 buf.buf[0] ? buf.buf: name);
211 strbuf_release(&buf);
212 if (st)
213 return;
215 add_object_array_with_mode(obj, name, &revs->pending, mode);
218 void add_pending_object(struct rev_info *revs,
219 struct object *obj, const char *name)
221 add_pending_object_with_mode(revs, obj, name, S_IFINVALID);
224 void add_head_to_pending(struct rev_info *revs)
226 unsigned char sha1[20];
227 struct object *obj;
228 if (get_sha1("HEAD", sha1))
229 return;
230 obj = parse_object(sha1);
231 if (!obj)
232 return;
233 add_pending_object(revs, obj, "HEAD");
236 static struct object *get_reference(struct rev_info *revs, const char *name,
237 const unsigned char *sha1,
238 unsigned int flags)
240 struct object *object;
242 object = parse_object(sha1);
243 if (!object) {
244 if (revs->ignore_missing)
245 return object;
246 die("bad object %s", name);
248 object->flags |= flags;
249 return object;
252 void add_pending_sha1(struct rev_info *revs, const char *name,
253 const unsigned char *sha1, unsigned int flags)
255 struct object *object = get_reference(revs, name, sha1, flags);
256 add_pending_object(revs, object, name);
259 static struct commit *handle_commit(struct rev_info *revs,
260 struct object *object, const char *name)
262 unsigned long flags = object->flags;
265 * Tag object? Look what it points to..
267 while (object->type == OBJ_TAG) {
268 struct tag *tag = (struct tag *) object;
269 if (revs->tag_objects && !(flags & UNINTERESTING))
270 add_pending_object(revs, object, tag->tag);
271 if (!tag->tagged)
272 die("bad tag");
273 object = parse_object(tag->tagged->sha1);
274 if (!object) {
275 if (flags & UNINTERESTING)
276 return NULL;
277 die("bad object %s", sha1_to_hex(tag->tagged->sha1));
282 * Commit object? Just return it, we'll do all the complex
283 * reachability crud.
285 if (object->type == OBJ_COMMIT) {
286 struct commit *commit = (struct commit *)object;
287 if (parse_commit(commit) < 0)
288 die("unable to parse commit %s", name);
289 if (flags & UNINTERESTING) {
290 commit->object.flags |= UNINTERESTING;
291 mark_parents_uninteresting(commit);
292 revs->limited = 1;
294 if (revs->show_source && !commit->util)
295 commit->util = (void *) name;
296 return commit;
300 * Tree object? Either mark it uninteresting, or add it
301 * to the list of objects to look at later..
303 if (object->type == OBJ_TREE) {
304 struct tree *tree = (struct tree *)object;
305 if (!revs->tree_objects)
306 return NULL;
307 if (flags & UNINTERESTING) {
308 mark_tree_uninteresting(tree);
309 return NULL;
311 add_pending_object(revs, object, "");
312 return NULL;
316 * Blob object? You know the drill by now..
318 if (object->type == OBJ_BLOB) {
319 struct blob *blob = (struct blob *)object;
320 if (!revs->blob_objects)
321 return NULL;
322 if (flags & UNINTERESTING) {
323 mark_blob_uninteresting(blob);
324 return NULL;
326 add_pending_object(revs, object, "");
327 return NULL;
329 die("%s is unknown object", name);
332 static int everybody_uninteresting(struct commit_list *orig)
334 struct commit_list *list = orig;
335 while (list) {
336 struct commit *commit = list->item;
337 list = list->next;
338 if (commit->object.flags & UNINTERESTING)
339 continue;
340 return 0;
342 return 1;
346 * A definition of "relevant" commit that we can use to simplify limited graphs
347 * by eliminating side branches.
349 * A "relevant" commit is one that is !UNINTERESTING (ie we are including it
350 * in our list), or that is a specified BOTTOM commit. Then after computing
351 * a limited list, during processing we can generally ignore boundary merges
352 * coming from outside the graph, (ie from irrelevant parents), and treat
353 * those merges as if they were single-parent. TREESAME is defined to consider
354 * only relevant parents, if any. If we are TREESAME to our on-graph parents,
355 * we don't care if we were !TREESAME to non-graph parents.
357 * Treating bottom commits as relevant ensures that a limited graph's
358 * connection to the actual bottom commit is not viewed as a side branch, but
359 * treated as part of the graph. For example:
361 * ....Z...A---X---o---o---B
362 * . /
363 * W---Y
365 * When computing "A..B", the A-X connection is at least as important as
366 * Y-X, despite A being flagged UNINTERESTING.
368 * And when computing --ancestry-path "A..B", the A-X connection is more
369 * important than Y-X, despite both A and Y being flagged UNINTERESTING.
371 static inline int relevant_commit(struct commit *commit)
373 return (commit->object.flags & (UNINTERESTING | BOTTOM)) != UNINTERESTING;
377 * Return a single relevant commit from a parent list. If we are a TREESAME
378 * commit, and this selects one of our parents, then we can safely simplify to
379 * that parent.
381 static struct commit *one_relevant_parent(const struct rev_info *revs,
382 struct commit_list *orig)
384 struct commit_list *list = orig;
385 struct commit *relevant = NULL;
387 if (!orig)
388 return NULL;
391 * For 1-parent commits, or if first-parent-only, then return that
392 * first parent (even if not "relevant" by the above definition).
393 * TREESAME will have been set purely on that parent.
395 if (revs->first_parent_only || !orig->next)
396 return orig->item;
399 * For multi-parent commits, identify a sole relevant parent, if any.
400 * If we have only one relevant parent, then TREESAME will be set purely
401 * with regard to that parent, and we can simplify accordingly.
403 * If we have more than one relevant parent, or no relevant parents
404 * (and multiple irrelevant ones), then we can't select a parent here
405 * and return NULL.
407 while (list) {
408 struct commit *commit = list->item;
409 list = list->next;
410 if (relevant_commit(commit)) {
411 if (relevant)
412 return NULL;
413 relevant = commit;
416 return relevant;
420 * The goal is to get REV_TREE_NEW as the result only if the
421 * diff consists of all '+' (and no other changes), REV_TREE_OLD
422 * if the whole diff is removal of old data, and otherwise
423 * REV_TREE_DIFFERENT (of course if the trees are the same we
424 * want REV_TREE_SAME).
425 * That means that once we get to REV_TREE_DIFFERENT, we do not
426 * have to look any further.
428 static int tree_difference = REV_TREE_SAME;
430 static void file_add_remove(struct diff_options *options,
431 int addremove, unsigned mode,
432 const unsigned char *sha1,
433 int sha1_valid,
434 const char *fullpath, unsigned dirty_submodule)
436 int diff = addremove == '+' ? REV_TREE_NEW : REV_TREE_OLD;
438 tree_difference |= diff;
439 if (tree_difference == REV_TREE_DIFFERENT)
440 DIFF_OPT_SET(options, HAS_CHANGES);
443 static void file_change(struct diff_options *options,
444 unsigned old_mode, unsigned new_mode,
445 const unsigned char *old_sha1,
446 const unsigned char *new_sha1,
447 int old_sha1_valid, int new_sha1_valid,
448 const char *fullpath,
449 unsigned old_dirty_submodule, unsigned new_dirty_submodule)
451 tree_difference = REV_TREE_DIFFERENT;
452 DIFF_OPT_SET(options, HAS_CHANGES);
455 static int rev_compare_tree(struct rev_info *revs,
456 struct commit *parent, struct commit *commit)
458 struct tree *t1 = parent->tree;
459 struct tree *t2 = commit->tree;
461 if (!t1)
462 return REV_TREE_NEW;
463 if (!t2)
464 return REV_TREE_OLD;
466 if (revs->simplify_by_decoration) {
468 * If we are simplifying by decoration, then the commit
469 * is worth showing if it has a tag pointing at it.
471 if (lookup_decoration(&name_decoration, &commit->object))
472 return REV_TREE_DIFFERENT;
474 * A commit that is not pointed by a tag is uninteresting
475 * if we are not limited by path. This means that you will
476 * see the usual "commits that touch the paths" plus any
477 * tagged commit by specifying both --simplify-by-decoration
478 * and pathspec.
480 if (!revs->prune_data.nr)
481 return REV_TREE_SAME;
484 tree_difference = REV_TREE_SAME;
485 DIFF_OPT_CLR(&revs->pruning, HAS_CHANGES);
486 if (diff_tree_sha1(t1->object.sha1, t2->object.sha1, "",
487 &revs->pruning) < 0)
488 return REV_TREE_DIFFERENT;
489 return tree_difference;
492 static int rev_same_tree_as_empty(struct rev_info *revs, struct commit *commit)
494 int retval;
495 void *tree;
496 unsigned long size;
497 struct tree_desc empty, real;
498 struct tree *t1 = commit->tree;
500 if (!t1)
501 return 0;
503 tree = read_object_with_reference(t1->object.sha1, tree_type, &size, NULL);
504 if (!tree)
505 return 0;
506 init_tree_desc(&real, tree, size);
507 init_tree_desc(&empty, "", 0);
509 tree_difference = REV_TREE_SAME;
510 DIFF_OPT_CLR(&revs->pruning, HAS_CHANGES);
511 retval = diff_tree(&empty, &real, "", &revs->pruning);
512 free(tree);
514 return retval >= 0 && (tree_difference == REV_TREE_SAME);
517 struct treesame_state {
518 unsigned int nparents;
519 unsigned char treesame[FLEX_ARRAY];
522 static struct treesame_state *initialise_treesame(struct rev_info *revs, struct commit *commit)
524 unsigned n = commit_list_count(commit->parents);
525 struct treesame_state *st = xcalloc(1, sizeof(*st) + n);
526 st->nparents = n;
527 add_decoration(&revs->treesame, &commit->object, st);
528 return st;
532 * Must be called immediately after removing the nth_parent from a commit's
533 * parent list, if we are maintaining the per-parent treesame[] decoration.
534 * This does not recalculate the master TREESAME flag - update_treesame()
535 * should be called to update it after a sequence of treesame[] modifications
536 * that may have affected it.
538 static int compact_treesame(struct rev_info *revs, struct commit *commit, unsigned nth_parent)
540 struct treesame_state *st;
541 int old_same;
543 if (!commit->parents) {
545 * Have just removed the only parent from a non-merge.
546 * Different handling, as we lack decoration.
548 if (nth_parent != 0)
549 die("compact_treesame %u", nth_parent);
550 old_same = !!(commit->object.flags & TREESAME);
551 if (rev_same_tree_as_empty(revs, commit))
552 commit->object.flags |= TREESAME;
553 else
554 commit->object.flags &= ~TREESAME;
555 return old_same;
558 st = lookup_decoration(&revs->treesame, &commit->object);
559 if (!st || nth_parent >= st->nparents)
560 die("compact_treesame %u", nth_parent);
562 old_same = st->treesame[nth_parent];
563 memmove(st->treesame + nth_parent,
564 st->treesame + nth_parent + 1,
565 st->nparents - nth_parent - 1);
568 * If we've just become a non-merge commit, update TREESAME
569 * immediately, and remove the no-longer-needed decoration.
570 * If still a merge, defer update until update_treesame().
572 if (--st->nparents == 1) {
573 if (commit->parents->next)
574 die("compact_treesame parents mismatch");
575 if (st->treesame[0] && revs->dense)
576 commit->object.flags |= TREESAME;
577 else
578 commit->object.flags &= ~TREESAME;
579 free(add_decoration(&revs->treesame, &commit->object, NULL));
582 return old_same;
585 static unsigned update_treesame(struct rev_info *revs, struct commit *commit)
587 if (commit->parents && commit->parents->next) {
588 unsigned n;
589 struct treesame_state *st;
590 struct commit_list *p;
591 unsigned relevant_parents;
592 unsigned relevant_change, irrelevant_change;
594 st = lookup_decoration(&revs->treesame, &commit->object);
595 if (!st)
596 die("update_treesame %s", sha1_to_hex(commit->object.sha1));
597 relevant_parents = 0;
598 relevant_change = irrelevant_change = 0;
599 for (p = commit->parents, n = 0; p; n++, p = p->next) {
600 if (relevant_commit(p->item)) {
601 relevant_change |= !st->treesame[n];
602 relevant_parents++;
603 } else
604 irrelevant_change |= !st->treesame[n];
606 if (relevant_parents ? relevant_change : irrelevant_change)
607 commit->object.flags &= ~TREESAME;
608 else
609 commit->object.flags |= TREESAME;
612 return commit->object.flags & TREESAME;
615 static inline int limiting_can_increase_treesame(const struct rev_info *revs)
618 * TREESAME is irrelevant unless prune && dense;
619 * if simplify_history is set, we can't have a mixture of TREESAME and
620 * !TREESAME INTERESTING parents (and we don't have treesame[]
621 * decoration anyway);
622 * if first_parent_only is set, then the TREESAME flag is locked
623 * against the first parent (and again we lack treesame[] decoration).
625 return revs->prune && revs->dense &&
626 !revs->simplify_history &&
627 !revs->first_parent_only;
630 static void try_to_simplify_commit(struct rev_info *revs, struct commit *commit)
632 struct commit_list **pp, *parent;
633 struct treesame_state *ts = NULL;
634 int relevant_change = 0, irrelevant_change = 0;
635 int relevant_parents, nth_parent;
638 * If we don't do pruning, everything is interesting
640 if (!revs->prune)
641 return;
643 if (!commit->tree)
644 return;
646 if (!commit->parents) {
647 if (rev_same_tree_as_empty(revs, commit))
648 commit->object.flags |= TREESAME;
649 return;
653 * Normal non-merge commit? If we don't want to make the
654 * history dense, we consider it always to be a change..
656 if (!revs->dense && !commit->parents->next)
657 return;
659 for (pp = &commit->parents, nth_parent = 0, relevant_parents = 0;
660 (parent = *pp) != NULL;
661 pp = &parent->next, nth_parent++) {
662 struct commit *p = parent->item;
663 if (relevant_commit(p))
664 relevant_parents++;
666 if (nth_parent == 1) {
668 * This our second loop iteration - so we now know
669 * we're dealing with a merge.
671 * Do not compare with later parents when we care only about
672 * the first parent chain, in order to avoid derailing the
673 * traversal to follow a side branch that brought everything
674 * in the path we are limited to by the pathspec.
676 if (revs->first_parent_only)
677 break;
679 * If this will remain a potentially-simplifiable
680 * merge, remember per-parent treesame if needed.
681 * Initialise the array with the comparison from our
682 * first iteration.
684 if (revs->treesame.name &&
685 !revs->simplify_history &&
686 !(commit->object.flags & UNINTERESTING)) {
687 ts = initialise_treesame(revs, commit);
688 if (!(irrelevant_change || relevant_change))
689 ts->treesame[0] = 1;
692 if (parse_commit(p) < 0)
693 die("cannot simplify commit %s (because of %s)",
694 sha1_to_hex(commit->object.sha1),
695 sha1_to_hex(p->object.sha1));
696 switch (rev_compare_tree(revs, p, commit)) {
697 case REV_TREE_SAME:
698 if (!revs->simplify_history || !relevant_commit(p)) {
699 /* Even if a merge with an uninteresting
700 * side branch brought the entire change
701 * we are interested in, we do not want
702 * to lose the other branches of this
703 * merge, so we just keep going.
705 if (ts)
706 ts->treesame[nth_parent] = 1;
707 continue;
709 parent->next = NULL;
710 commit->parents = parent;
711 commit->object.flags |= TREESAME;
712 return;
714 case REV_TREE_NEW:
715 if (revs->remove_empty_trees &&
716 rev_same_tree_as_empty(revs, p)) {
717 /* We are adding all the specified
718 * paths from this parent, so the
719 * history beyond this parent is not
720 * interesting. Remove its parents
721 * (they are grandparents for us).
722 * IOW, we pretend this parent is a
723 * "root" commit.
725 if (parse_commit(p) < 0)
726 die("cannot simplify commit %s (invalid %s)",
727 sha1_to_hex(commit->object.sha1),
728 sha1_to_hex(p->object.sha1));
729 p->parents = NULL;
731 /* fallthrough */
732 case REV_TREE_OLD:
733 case REV_TREE_DIFFERENT:
734 if (relevant_commit(p))
735 relevant_change = 1;
736 else
737 irrelevant_change = 1;
738 continue;
740 die("bad tree compare for commit %s", sha1_to_hex(commit->object.sha1));
744 * TREESAME is straightforward for single-parent commits. For merge
745 * commits, it is most useful to define it so that "irrelevant"
746 * parents cannot make us !TREESAME - if we have any relevant
747 * parents, then we only consider TREESAMEness with respect to them,
748 * allowing irrelevant merges from uninteresting branches to be
749 * simplified away. Only if we have only irrelevant parents do we
750 * base TREESAME on them. Note that this logic is replicated in
751 * update_treesame, which should be kept in sync.
753 if (relevant_parents ? !relevant_change : !irrelevant_change)
754 commit->object.flags |= TREESAME;
757 static void commit_list_insert_by_date_cached(struct commit *p, struct commit_list **head,
758 struct commit_list *cached_base, struct commit_list **cache)
760 struct commit_list *new_entry;
762 if (cached_base && p->date < cached_base->item->date)
763 new_entry = commit_list_insert_by_date(p, &cached_base->next);
764 else
765 new_entry = commit_list_insert_by_date(p, head);
767 if (cache && (!*cache || p->date < (*cache)->item->date))
768 *cache = new_entry;
771 static int add_parents_to_list(struct rev_info *revs, struct commit *commit,
772 struct commit_list **list, struct commit_list **cache_ptr)
774 struct commit_list *parent = commit->parents;
775 unsigned left_flag;
776 struct commit_list *cached_base = cache_ptr ? *cache_ptr : NULL;
778 if (commit->object.flags & ADDED)
779 return 0;
780 commit->object.flags |= ADDED;
782 if (revs->include_check &&
783 !revs->include_check(commit, revs->include_check_data))
784 return 0;
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, 0, "", prune);
1380 revs->limited = 1;
1383 int handle_revision_arg(const char *arg_, struct rev_info *revs, int flags, unsigned revarg_opt)
1385 struct object_context oc;
1386 char *dotdot;
1387 struct object *object;
1388 unsigned char sha1[20];
1389 int local_flags;
1390 const char *arg = arg_;
1391 int cant_be_filename = revarg_opt & REVARG_CANNOT_BE_FILENAME;
1392 unsigned get_sha1_flags = 0;
1394 flags = flags & UNINTERESTING ? flags | BOTTOM : flags & ~BOTTOM;
1396 dotdot = strstr(arg, "..");
1397 if (dotdot) {
1398 unsigned char from_sha1[20];
1399 const char *next = dotdot + 2;
1400 const char *this = arg;
1401 int symmetric = *next == '.';
1402 unsigned int flags_exclude = flags ^ (UNINTERESTING | BOTTOM);
1403 static const char head_by_default[] = "HEAD";
1404 unsigned int a_flags;
1406 *dotdot = 0;
1407 next += symmetric;
1409 if (!*next)
1410 next = head_by_default;
1411 if (dotdot == arg)
1412 this = head_by_default;
1413 if (this == head_by_default && next == head_by_default &&
1414 !symmetric) {
1416 * Just ".."? That is not a range but the
1417 * pathspec for the parent directory.
1419 if (!cant_be_filename) {
1420 *dotdot = '.';
1421 return -1;
1424 if (!get_sha1_committish(this, from_sha1) &&
1425 !get_sha1_committish(next, sha1)) {
1426 struct commit *a, *b;
1427 struct commit_list *exclude;
1429 a = lookup_commit_reference(from_sha1);
1430 b = lookup_commit_reference(sha1);
1431 if (!a || !b) {
1432 if (revs->ignore_missing)
1433 return 0;
1434 die(symmetric ?
1435 "Invalid symmetric difference expression %s...%s" :
1436 "Invalid revision range %s..%s",
1437 arg, next);
1440 if (!cant_be_filename) {
1441 *dotdot = '.';
1442 verify_non_filename(revs->prefix, arg);
1445 if (symmetric) {
1446 exclude = get_merge_bases(a, b, 1);
1447 add_rev_cmdline_list(revs, exclude,
1448 REV_CMD_MERGE_BASE,
1449 flags_exclude);
1450 add_pending_commit_list(revs, exclude,
1451 flags_exclude);
1452 free_commit_list(exclude);
1453 a_flags = flags | SYMMETRIC_LEFT;
1454 } else
1455 a_flags = flags_exclude;
1456 a->object.flags |= a_flags;
1457 b->object.flags |= flags;
1458 add_rev_cmdline(revs, &a->object, this,
1459 REV_CMD_LEFT, a_flags);
1460 add_rev_cmdline(revs, &b->object, next,
1461 REV_CMD_RIGHT, flags);
1462 add_pending_object(revs, &a->object, this);
1463 add_pending_object(revs, &b->object, next);
1464 return 0;
1466 *dotdot = '.';
1468 dotdot = strstr(arg, "^@");
1469 if (dotdot && !dotdot[2]) {
1470 *dotdot = 0;
1471 if (add_parents_only(revs, arg, flags))
1472 return 0;
1473 *dotdot = '^';
1475 dotdot = strstr(arg, "^!");
1476 if (dotdot && !dotdot[2]) {
1477 *dotdot = 0;
1478 if (!add_parents_only(revs, arg, flags ^ (UNINTERESTING | BOTTOM)))
1479 *dotdot = '^';
1482 local_flags = 0;
1483 if (*arg == '^') {
1484 local_flags = UNINTERESTING | BOTTOM;
1485 arg++;
1488 if (revarg_opt & REVARG_COMMITTISH)
1489 get_sha1_flags = GET_SHA1_COMMITTISH;
1491 if (get_sha1_with_context(arg, get_sha1_flags, sha1, &oc))
1492 return revs->ignore_missing ? 0 : -1;
1493 if (!cant_be_filename)
1494 verify_non_filename(revs->prefix, arg);
1495 object = get_reference(revs, arg, sha1, flags ^ local_flags);
1496 add_rev_cmdline(revs, object, arg_, REV_CMD_REV, flags ^ local_flags);
1497 add_pending_object_with_mode(revs, object, arg, oc.mode);
1498 return 0;
1501 struct cmdline_pathspec {
1502 int alloc;
1503 int nr;
1504 const char **path;
1507 static void append_prune_data(struct cmdline_pathspec *prune, const char **av)
1509 while (*av) {
1510 ALLOC_GROW(prune->path, prune->nr+1, prune->alloc);
1511 prune->path[prune->nr++] = *(av++);
1515 static void read_pathspec_from_stdin(struct rev_info *revs, struct strbuf *sb,
1516 struct cmdline_pathspec *prune)
1518 while (strbuf_getwholeline(sb, stdin, '\n') != EOF) {
1519 int len = sb->len;
1520 if (len && sb->buf[len - 1] == '\n')
1521 sb->buf[--len] = '\0';
1522 ALLOC_GROW(prune->path, prune->nr+1, prune->alloc);
1523 prune->path[prune->nr++] = xstrdup(sb->buf);
1527 static void read_revisions_from_stdin(struct rev_info *revs,
1528 struct cmdline_pathspec *prune)
1530 struct strbuf sb;
1531 int seen_dashdash = 0;
1533 strbuf_init(&sb, 1000);
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 if (!len)
1539 break;
1540 if (sb.buf[0] == '-') {
1541 if (len == 2 && sb.buf[1] == '-') {
1542 seen_dashdash = 1;
1543 break;
1545 die("options not supported in --stdin mode");
1547 if (handle_revision_arg(sb.buf, revs, 0,
1548 REVARG_CANNOT_BE_FILENAME))
1549 die("bad revision '%s'", sb.buf);
1551 if (seen_dashdash)
1552 read_pathspec_from_stdin(revs, &sb, prune);
1553 strbuf_release(&sb);
1556 static void add_grep(struct rev_info *revs, const char *ptn, enum grep_pat_token what)
1558 append_grep_pattern(&revs->grep_filter, ptn, "command line", 0, what);
1561 static void add_header_grep(struct rev_info *revs, enum grep_header_field field, const char *pattern)
1563 append_header_grep_pattern(&revs->grep_filter, field, pattern);
1566 static void add_message_grep(struct rev_info *revs, const char *pattern)
1568 add_grep(revs, pattern, GREP_PATTERN_BODY);
1571 static int handle_revision_opt(struct rev_info *revs, int argc, const char **argv,
1572 int *unkc, const char **unkv)
1574 const char *arg = argv[0];
1575 const char *optarg;
1576 int argcount;
1578 /* pseudo revision arguments */
1579 if (!strcmp(arg, "--all") || !strcmp(arg, "--branches") ||
1580 !strcmp(arg, "--tags") || !strcmp(arg, "--remotes") ||
1581 !strcmp(arg, "--reflog") || !strcmp(arg, "--not") ||
1582 !strcmp(arg, "--no-walk") || !strcmp(arg, "--do-walk") ||
1583 !strcmp(arg, "--bisect") || !prefixcmp(arg, "--glob=") ||
1584 !prefixcmp(arg, "--branches=") || !prefixcmp(arg, "--tags=") ||
1585 !prefixcmp(arg, "--remotes=") || !prefixcmp(arg, "--no-walk="))
1587 unkv[(*unkc)++] = arg;
1588 return 1;
1591 if ((argcount = parse_long_opt("max-count", argv, &optarg))) {
1592 revs->max_count = atoi(optarg);
1593 revs->no_walk = 0;
1594 return argcount;
1595 } else if ((argcount = parse_long_opt("skip", argv, &optarg))) {
1596 revs->skip_count = atoi(optarg);
1597 return argcount;
1598 } else if ((*arg == '-') && isdigit(arg[1])) {
1599 /* accept -<digit>, like traditional "head" */
1600 revs->max_count = atoi(arg + 1);
1601 revs->no_walk = 0;
1602 } else if (!strcmp(arg, "-n")) {
1603 if (argc <= 1)
1604 return error("-n requires an argument");
1605 revs->max_count = atoi(argv[1]);
1606 revs->no_walk = 0;
1607 return 2;
1608 } else if (!prefixcmp(arg, "-n")) {
1609 revs->max_count = atoi(arg + 2);
1610 revs->no_walk = 0;
1611 } else if ((argcount = parse_long_opt("max-age", argv, &optarg))) {
1612 revs->max_age = atoi(optarg);
1613 return argcount;
1614 } else if ((argcount = parse_long_opt("since", argv, &optarg))) {
1615 revs->max_age = approxidate(optarg);
1616 return argcount;
1617 } else if ((argcount = parse_long_opt("after", argv, &optarg))) {
1618 revs->max_age = approxidate(optarg);
1619 return argcount;
1620 } else if ((argcount = parse_long_opt("min-age", argv, &optarg))) {
1621 revs->min_age = atoi(optarg);
1622 return argcount;
1623 } else if ((argcount = parse_long_opt("before", argv, &optarg))) {
1624 revs->min_age = approxidate(optarg);
1625 return argcount;
1626 } else if ((argcount = parse_long_opt("until", argv, &optarg))) {
1627 revs->min_age = approxidate(optarg);
1628 return argcount;
1629 } else if (!strcmp(arg, "--first-parent")) {
1630 revs->first_parent_only = 1;
1631 } else if (!strcmp(arg, "--ancestry-path")) {
1632 revs->ancestry_path = 1;
1633 revs->simplify_history = 0;
1634 revs->limited = 1;
1635 } else if (!strcmp(arg, "-g") || !strcmp(arg, "--walk-reflogs")) {
1636 init_reflog_walk(&revs->reflog_info);
1637 } else if (!strcmp(arg, "--default")) {
1638 if (argc <= 1)
1639 return error("bad --default argument");
1640 revs->def = argv[1];
1641 return 2;
1642 } else if (!strcmp(arg, "--merge")) {
1643 revs->show_merge = 1;
1644 } else if (!strcmp(arg, "--topo-order")) {
1645 revs->sort_order = REV_SORT_IN_GRAPH_ORDER;
1646 revs->topo_order = 1;
1647 } else if (!strcmp(arg, "--simplify-merges")) {
1648 revs->simplify_merges = 1;
1649 revs->topo_order = 1;
1650 revs->rewrite_parents = 1;
1651 revs->simplify_history = 0;
1652 revs->limited = 1;
1653 } else if (!strcmp(arg, "--simplify-by-decoration")) {
1654 revs->simplify_merges = 1;
1655 revs->topo_order = 1;
1656 revs->rewrite_parents = 1;
1657 revs->simplify_history = 0;
1658 revs->simplify_by_decoration = 1;
1659 revs->limited = 1;
1660 revs->prune = 1;
1661 load_ref_decorations(DECORATE_SHORT_REFS);
1662 } else if (!strcmp(arg, "--date-order")) {
1663 revs->sort_order = REV_SORT_BY_COMMIT_DATE;
1664 revs->topo_order = 1;
1665 } else if (!strcmp(arg, "--author-date-order")) {
1666 revs->sort_order = REV_SORT_BY_AUTHOR_DATE;
1667 revs->topo_order = 1;
1668 } else if (!prefixcmp(arg, "--early-output")) {
1669 int count = 100;
1670 switch (arg[14]) {
1671 case '=':
1672 count = atoi(arg+15);
1673 /* Fallthrough */
1674 case 0:
1675 revs->topo_order = 1;
1676 revs->early_output = count;
1678 } else if (!strcmp(arg, "--parents")) {
1679 revs->rewrite_parents = 1;
1680 revs->print_parents = 1;
1681 } else if (!strcmp(arg, "--dense")) {
1682 revs->dense = 1;
1683 } else if (!strcmp(arg, "--sparse")) {
1684 revs->dense = 0;
1685 } else if (!strcmp(arg, "--show-all")) {
1686 revs->show_all = 1;
1687 } else if (!strcmp(arg, "--remove-empty")) {
1688 revs->remove_empty_trees = 1;
1689 } else if (!strcmp(arg, "--merges")) {
1690 revs->min_parents = 2;
1691 } else if (!strcmp(arg, "--no-merges")) {
1692 revs->max_parents = 1;
1693 } else if (!prefixcmp(arg, "--min-parents=")) {
1694 revs->min_parents = atoi(arg+14);
1695 } else if (!prefixcmp(arg, "--no-min-parents")) {
1696 revs->min_parents = 0;
1697 } else if (!prefixcmp(arg, "--max-parents=")) {
1698 revs->max_parents = atoi(arg+14);
1699 } else if (!prefixcmp(arg, "--no-max-parents")) {
1700 revs->max_parents = -1;
1701 } else if (!strcmp(arg, "--boundary")) {
1702 revs->boundary = 1;
1703 } else if (!strcmp(arg, "--left-right")) {
1704 revs->left_right = 1;
1705 } else if (!strcmp(arg, "--left-only")) {
1706 if (revs->right_only)
1707 die("--left-only is incompatible with --right-only"
1708 " or --cherry");
1709 revs->left_only = 1;
1710 } else if (!strcmp(arg, "--right-only")) {
1711 if (revs->left_only)
1712 die("--right-only is incompatible with --left-only");
1713 revs->right_only = 1;
1714 } else if (!strcmp(arg, "--cherry")) {
1715 if (revs->left_only)
1716 die("--cherry is incompatible with --left-only");
1717 revs->cherry_mark = 1;
1718 revs->right_only = 1;
1719 revs->max_parents = 1;
1720 revs->limited = 1;
1721 } else if (!strcmp(arg, "--count")) {
1722 revs->count = 1;
1723 } else if (!strcmp(arg, "--cherry-mark")) {
1724 if (revs->cherry_pick)
1725 die("--cherry-mark is incompatible with --cherry-pick");
1726 revs->cherry_mark = 1;
1727 revs->limited = 1; /* needs limit_list() */
1728 } else if (!strcmp(arg, "--cherry-pick")) {
1729 if (revs->cherry_mark)
1730 die("--cherry-pick is incompatible with --cherry-mark");
1731 revs->cherry_pick = 1;
1732 revs->limited = 1;
1733 } else if (!strcmp(arg, "--objects")) {
1734 revs->tag_objects = 1;
1735 revs->tree_objects = 1;
1736 revs->blob_objects = 1;
1737 } else if (!strcmp(arg, "--objects-edge")) {
1738 revs->tag_objects = 1;
1739 revs->tree_objects = 1;
1740 revs->blob_objects = 1;
1741 revs->edge_hint = 1;
1742 } else if (!strcmp(arg, "--verify-objects")) {
1743 revs->tag_objects = 1;
1744 revs->tree_objects = 1;
1745 revs->blob_objects = 1;
1746 revs->verify_objects = 1;
1747 } else if (!strcmp(arg, "--unpacked")) {
1748 revs->unpacked = 1;
1749 } else if (!prefixcmp(arg, "--unpacked=")) {
1750 die("--unpacked=<packfile> no longer supported.");
1751 } else if (!strcmp(arg, "-r")) {
1752 revs->diff = 1;
1753 DIFF_OPT_SET(&revs->diffopt, RECURSIVE);
1754 } else if (!strcmp(arg, "-t")) {
1755 revs->diff = 1;
1756 DIFF_OPT_SET(&revs->diffopt, RECURSIVE);
1757 DIFF_OPT_SET(&revs->diffopt, TREE_IN_RECURSIVE);
1758 } else if (!strcmp(arg, "-m")) {
1759 revs->ignore_merges = 0;
1760 } else if (!strcmp(arg, "-c")) {
1761 revs->diff = 1;
1762 revs->dense_combined_merges = 0;
1763 revs->combine_merges = 1;
1764 } else if (!strcmp(arg, "--cc")) {
1765 revs->diff = 1;
1766 revs->dense_combined_merges = 1;
1767 revs->combine_merges = 1;
1768 } else if (!strcmp(arg, "-v")) {
1769 revs->verbose_header = 1;
1770 } else if (!strcmp(arg, "--pretty")) {
1771 revs->verbose_header = 1;
1772 revs->pretty_given = 1;
1773 get_commit_format(arg+8, revs);
1774 } else if (!prefixcmp(arg, "--pretty=") || !prefixcmp(arg, "--format=")) {
1776 * Detached form ("--pretty X" as opposed to "--pretty=X")
1777 * not allowed, since the argument is optional.
1779 revs->verbose_header = 1;
1780 revs->pretty_given = 1;
1781 get_commit_format(arg+9, revs);
1782 } else if (!strcmp(arg, "--show-notes") || !strcmp(arg, "--notes")) {
1783 revs->show_notes = 1;
1784 revs->show_notes_given = 1;
1785 revs->notes_opt.use_default_notes = 1;
1786 } else if (!strcmp(arg, "--show-signature")) {
1787 revs->show_signature = 1;
1788 } else if (!prefixcmp(arg, "--show-notes=") ||
1789 !prefixcmp(arg, "--notes=")) {
1790 struct strbuf buf = STRBUF_INIT;
1791 revs->show_notes = 1;
1792 revs->show_notes_given = 1;
1793 if (!prefixcmp(arg, "--show-notes")) {
1794 if (revs->notes_opt.use_default_notes < 0)
1795 revs->notes_opt.use_default_notes = 1;
1796 strbuf_addstr(&buf, arg+13);
1798 else
1799 strbuf_addstr(&buf, arg+8);
1800 expand_notes_ref(&buf);
1801 string_list_append(&revs->notes_opt.extra_notes_refs,
1802 strbuf_detach(&buf, NULL));
1803 } else if (!strcmp(arg, "--no-notes")) {
1804 revs->show_notes = 0;
1805 revs->show_notes_given = 1;
1806 revs->notes_opt.use_default_notes = -1;
1807 /* we have been strdup'ing ourselves, so trick
1808 * string_list into free()ing strings */
1809 revs->notes_opt.extra_notes_refs.strdup_strings = 1;
1810 string_list_clear(&revs->notes_opt.extra_notes_refs, 0);
1811 revs->notes_opt.extra_notes_refs.strdup_strings = 0;
1812 } else if (!strcmp(arg, "--standard-notes")) {
1813 revs->show_notes_given = 1;
1814 revs->notes_opt.use_default_notes = 1;
1815 } else if (!strcmp(arg, "--no-standard-notes")) {
1816 revs->notes_opt.use_default_notes = 0;
1817 } else if (!strcmp(arg, "--oneline")) {
1818 revs->verbose_header = 1;
1819 get_commit_format("oneline", revs);
1820 revs->pretty_given = 1;
1821 revs->abbrev_commit = 1;
1822 } else if (!strcmp(arg, "--graph")) {
1823 revs->topo_order = 1;
1824 revs->rewrite_parents = 1;
1825 revs->graph = graph_init(revs);
1826 } else if (!strcmp(arg, "--root")) {
1827 revs->show_root_diff = 1;
1828 } else if (!strcmp(arg, "--no-commit-id")) {
1829 revs->no_commit_id = 1;
1830 } else if (!strcmp(arg, "--always")) {
1831 revs->always_show_header = 1;
1832 } else if (!strcmp(arg, "--no-abbrev")) {
1833 revs->abbrev = 0;
1834 } else if (!strcmp(arg, "--abbrev")) {
1835 revs->abbrev = DEFAULT_ABBREV;
1836 } else if (!prefixcmp(arg, "--abbrev=")) {
1837 revs->abbrev = strtoul(arg + 9, NULL, 10);
1838 if (revs->abbrev < MINIMUM_ABBREV)
1839 revs->abbrev = MINIMUM_ABBREV;
1840 else if (revs->abbrev > 40)
1841 revs->abbrev = 40;
1842 } else if (!strcmp(arg, "--abbrev-commit")) {
1843 revs->abbrev_commit = 1;
1844 revs->abbrev_commit_given = 1;
1845 } else if (!strcmp(arg, "--no-abbrev-commit")) {
1846 revs->abbrev_commit = 0;
1847 } else if (!strcmp(arg, "--full-diff")) {
1848 revs->diff = 1;
1849 revs->full_diff = 1;
1850 } else if (!strcmp(arg, "--full-history")) {
1851 revs->simplify_history = 0;
1852 } else if (!strcmp(arg, "--relative-date")) {
1853 revs->date_mode = DATE_RELATIVE;
1854 revs->date_mode_explicit = 1;
1855 } else if ((argcount = parse_long_opt("date", argv, &optarg))) {
1856 revs->date_mode = parse_date_format(optarg);
1857 revs->date_mode_explicit = 1;
1858 return argcount;
1859 } else if (!strcmp(arg, "--log-size")) {
1860 revs->show_log_size = 1;
1863 * Grepping the commit log
1865 else if ((argcount = parse_long_opt("author", argv, &optarg))) {
1866 add_header_grep(revs, GREP_HEADER_AUTHOR, optarg);
1867 return argcount;
1868 } else if ((argcount = parse_long_opt("committer", argv, &optarg))) {
1869 add_header_grep(revs, GREP_HEADER_COMMITTER, optarg);
1870 return argcount;
1871 } else if ((argcount = parse_long_opt("grep-reflog", argv, &optarg))) {
1872 add_header_grep(revs, GREP_HEADER_REFLOG, optarg);
1873 return argcount;
1874 } else if ((argcount = parse_long_opt("grep", argv, &optarg))) {
1875 add_message_grep(revs, optarg);
1876 return argcount;
1877 } else if (!strcmp(arg, "--grep-debug")) {
1878 revs->grep_filter.debug = 1;
1879 } else if (!strcmp(arg, "--basic-regexp")) {
1880 grep_set_pattern_type_option(GREP_PATTERN_TYPE_BRE, &revs->grep_filter);
1881 } else if (!strcmp(arg, "--extended-regexp") || !strcmp(arg, "-E")) {
1882 grep_set_pattern_type_option(GREP_PATTERN_TYPE_ERE, &revs->grep_filter);
1883 } else if (!strcmp(arg, "--regexp-ignore-case") || !strcmp(arg, "-i")) {
1884 revs->grep_filter.regflags |= REG_ICASE;
1885 DIFF_OPT_SET(&revs->diffopt, PICKAXE_IGNORE_CASE);
1886 } else if (!strcmp(arg, "--fixed-strings") || !strcmp(arg, "-F")) {
1887 grep_set_pattern_type_option(GREP_PATTERN_TYPE_FIXED, &revs->grep_filter);
1888 } else if (!strcmp(arg, "--perl-regexp")) {
1889 grep_set_pattern_type_option(GREP_PATTERN_TYPE_PCRE, &revs->grep_filter);
1890 } else if (!strcmp(arg, "--all-match")) {
1891 revs->grep_filter.all_match = 1;
1892 } else if ((argcount = parse_long_opt("encoding", argv, &optarg))) {
1893 if (strcmp(optarg, "none"))
1894 git_log_output_encoding = xstrdup(optarg);
1895 else
1896 git_log_output_encoding = "";
1897 return argcount;
1898 } else if (!strcmp(arg, "--reverse")) {
1899 revs->reverse ^= 1;
1900 } else if (!strcmp(arg, "--children")) {
1901 revs->children.name = "children";
1902 revs->limited = 1;
1903 } else if (!strcmp(arg, "--ignore-missing")) {
1904 revs->ignore_missing = 1;
1905 } else {
1906 int opts = diff_opt_parse(&revs->diffopt, argv, argc);
1907 if (!opts)
1908 unkv[(*unkc)++] = arg;
1909 return opts;
1912 return 1;
1915 void parse_revision_opt(struct rev_info *revs, struct parse_opt_ctx_t *ctx,
1916 const struct option *options,
1917 const char * const usagestr[])
1919 int n = handle_revision_opt(revs, ctx->argc, ctx->argv,
1920 &ctx->cpidx, ctx->out);
1921 if (n <= 0) {
1922 error("unknown option `%s'", ctx->argv[0]);
1923 usage_with_options(usagestr, options);
1925 ctx->argv += n;
1926 ctx->argc -= n;
1929 static int for_each_bad_bisect_ref(const char *submodule, each_ref_fn fn, void *cb_data)
1931 return for_each_ref_in_submodule(submodule, "refs/bisect/bad", fn, cb_data);
1934 static int for_each_good_bisect_ref(const char *submodule, each_ref_fn fn, void *cb_data)
1936 return for_each_ref_in_submodule(submodule, "refs/bisect/good", fn, cb_data);
1939 static int handle_revision_pseudo_opt(const char *submodule,
1940 struct rev_info *revs,
1941 int argc, const char **argv, int *flags)
1943 const char *arg = argv[0];
1944 const char *optarg;
1945 int argcount;
1948 * NOTE!
1950 * Commands like "git shortlog" will not accept the options below
1951 * unless parse_revision_opt queues them (as opposed to erroring
1952 * out).
1954 * When implementing your new pseudo-option, remember to
1955 * register it in the list at the top of handle_revision_opt.
1957 if (!strcmp(arg, "--all")) {
1958 handle_refs(submodule, revs, *flags, for_each_ref_submodule);
1959 handle_refs(submodule, revs, *flags, head_ref_submodule);
1960 } else if (!strcmp(arg, "--branches")) {
1961 handle_refs(submodule, revs, *flags, for_each_branch_ref_submodule);
1962 } else if (!strcmp(arg, "--bisect")) {
1963 handle_refs(submodule, revs, *flags, for_each_bad_bisect_ref);
1964 handle_refs(submodule, revs, *flags ^ (UNINTERESTING | BOTTOM), for_each_good_bisect_ref);
1965 revs->bisect = 1;
1966 } else if (!strcmp(arg, "--tags")) {
1967 handle_refs(submodule, revs, *flags, for_each_tag_ref_submodule);
1968 } else if (!strcmp(arg, "--remotes")) {
1969 handle_refs(submodule, revs, *flags, for_each_remote_ref_submodule);
1970 } else if ((argcount = parse_long_opt("glob", argv, &optarg))) {
1971 struct all_refs_cb cb;
1972 init_all_refs_cb(&cb, revs, *flags);
1973 for_each_glob_ref(handle_one_ref, optarg, &cb);
1974 return argcount;
1975 } else if (!prefixcmp(arg, "--branches=")) {
1976 struct all_refs_cb cb;
1977 init_all_refs_cb(&cb, revs, *flags);
1978 for_each_glob_ref_in(handle_one_ref, arg + 11, "refs/heads/", &cb);
1979 } else if (!prefixcmp(arg, "--tags=")) {
1980 struct all_refs_cb cb;
1981 init_all_refs_cb(&cb, revs, *flags);
1982 for_each_glob_ref_in(handle_one_ref, arg + 7, "refs/tags/", &cb);
1983 } else if (!prefixcmp(arg, "--remotes=")) {
1984 struct all_refs_cb cb;
1985 init_all_refs_cb(&cb, revs, *flags);
1986 for_each_glob_ref_in(handle_one_ref, arg + 10, "refs/remotes/", &cb);
1987 } else if (!strcmp(arg, "--reflog")) {
1988 handle_reflog(revs, *flags);
1989 } else if (!strcmp(arg, "--not")) {
1990 *flags ^= UNINTERESTING | BOTTOM;
1991 } else if (!strcmp(arg, "--no-walk")) {
1992 revs->no_walk = REVISION_WALK_NO_WALK_SORTED;
1993 } else if (!prefixcmp(arg, "--no-walk=")) {
1995 * Detached form ("--no-walk X" as opposed to "--no-walk=X")
1996 * not allowed, since the argument is optional.
1998 if (!strcmp(arg + 10, "sorted"))
1999 revs->no_walk = REVISION_WALK_NO_WALK_SORTED;
2000 else if (!strcmp(arg + 10, "unsorted"))
2001 revs->no_walk = REVISION_WALK_NO_WALK_UNSORTED;
2002 else
2003 return error("invalid argument to --no-walk");
2004 } else if (!strcmp(arg, "--do-walk")) {
2005 revs->no_walk = 0;
2006 } else {
2007 return 0;
2010 return 1;
2014 * Parse revision information, filling in the "rev_info" structure,
2015 * and removing the used arguments from the argument list.
2017 * Returns the number of arguments left that weren't recognized
2018 * (which are also moved to the head of the argument list)
2020 int setup_revisions(int argc, const char **argv, struct rev_info *revs, struct setup_revision_opt *opt)
2022 int i, flags, left, seen_dashdash, read_from_stdin, got_rev_arg = 0, revarg_opt;
2023 struct cmdline_pathspec prune_data;
2024 const char *submodule = NULL;
2026 memset(&prune_data, 0, sizeof(prune_data));
2027 if (opt)
2028 submodule = opt->submodule;
2030 /* First, search for "--" */
2031 if (opt && opt->assume_dashdash) {
2032 seen_dashdash = 1;
2033 } else {
2034 seen_dashdash = 0;
2035 for (i = 1; i < argc; i++) {
2036 const char *arg = argv[i];
2037 if (strcmp(arg, "--"))
2038 continue;
2039 argv[i] = NULL;
2040 argc = i;
2041 if (argv[i + 1])
2042 append_prune_data(&prune_data, argv + i + 1);
2043 seen_dashdash = 1;
2044 break;
2048 /* Second, deal with arguments and options */
2049 flags = 0;
2050 revarg_opt = opt ? opt->revarg_opt : 0;
2051 if (seen_dashdash)
2052 revarg_opt |= REVARG_CANNOT_BE_FILENAME;
2053 read_from_stdin = 0;
2054 for (left = i = 1; i < argc; i++) {
2055 const char *arg = argv[i];
2056 if (*arg == '-') {
2057 int opts;
2059 opts = handle_revision_pseudo_opt(submodule,
2060 revs, argc - i, argv + i,
2061 &flags);
2062 if (opts > 0) {
2063 i += opts - 1;
2064 continue;
2067 if (!strcmp(arg, "--stdin")) {
2068 if (revs->disable_stdin) {
2069 argv[left++] = arg;
2070 continue;
2072 if (read_from_stdin++)
2073 die("--stdin given twice?");
2074 read_revisions_from_stdin(revs, &prune_data);
2075 continue;
2078 opts = handle_revision_opt(revs, argc - i, argv + i, &left, argv);
2079 if (opts > 0) {
2080 i += opts - 1;
2081 continue;
2083 if (opts < 0)
2084 exit(128);
2085 continue;
2089 if (handle_revision_arg(arg, revs, flags, revarg_opt)) {
2090 int j;
2091 if (seen_dashdash || *arg == '^')
2092 die("bad revision '%s'", arg);
2094 /* If we didn't have a "--":
2095 * (1) all filenames must exist;
2096 * (2) all rev-args must not be interpretable
2097 * as a valid filename.
2098 * but the latter we have checked in the main loop.
2100 for (j = i; j < argc; j++)
2101 verify_filename(revs->prefix, argv[j], j == i);
2103 append_prune_data(&prune_data, argv + i);
2104 break;
2106 else
2107 got_rev_arg = 1;
2110 if (prune_data.nr) {
2112 * If we need to introduce the magic "a lone ':' means no
2113 * pathspec whatsoever", here is the place to do so.
2115 * if (prune_data.nr == 1 && !strcmp(prune_data[0], ":")) {
2116 * prune_data.nr = 0;
2117 * prune_data.alloc = 0;
2118 * free(prune_data.path);
2119 * prune_data.path = NULL;
2120 * } else {
2121 * terminate prune_data.alloc with NULL and
2122 * call init_pathspec() to set revs->prune_data here.
2125 ALLOC_GROW(prune_data.path, prune_data.nr+1, prune_data.alloc);
2126 prune_data.path[prune_data.nr++] = NULL;
2127 parse_pathspec(&revs->prune_data, 0, 0,
2128 revs->prefix, prune_data.path);
2131 if (revs->def == NULL)
2132 revs->def = opt ? opt->def : NULL;
2133 if (opt && opt->tweak)
2134 opt->tweak(revs, opt);
2135 if (revs->show_merge)
2136 prepare_show_merge(revs);
2137 if (revs->def && !revs->pending.nr && !got_rev_arg) {
2138 unsigned char sha1[20];
2139 struct object *object;
2140 struct object_context oc;
2141 if (get_sha1_with_context(revs->def, 0, sha1, &oc))
2142 die("bad default revision '%s'", revs->def);
2143 object = get_reference(revs, revs->def, sha1, 0);
2144 add_pending_object_with_mode(revs, object, revs->def, oc.mode);
2147 /* Did the user ask for any diff output? Run the diff! */
2148 if (revs->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT)
2149 revs->diff = 1;
2151 /* Pickaxe, diff-filter and rename following need diffs */
2152 if (revs->diffopt.pickaxe ||
2153 revs->diffopt.filter ||
2154 DIFF_OPT_TST(&revs->diffopt, FOLLOW_RENAMES))
2155 revs->diff = 1;
2157 if (revs->topo_order)
2158 revs->limited = 1;
2160 if (revs->prune_data.nr) {
2161 copy_pathspec(&revs->pruning.pathspec, &revs->prune_data);
2162 /* Can't prune commits with rename following: the paths change.. */
2163 if (!DIFF_OPT_TST(&revs->diffopt, FOLLOW_RENAMES))
2164 revs->prune = 1;
2165 if (!revs->full_diff)
2166 copy_pathspec(&revs->diffopt.pathspec,
2167 &revs->prune_data);
2169 if (revs->combine_merges)
2170 revs->ignore_merges = 0;
2171 revs->diffopt.abbrev = revs->abbrev;
2173 if (revs->line_level_traverse) {
2174 revs->limited = 1;
2175 revs->topo_order = 1;
2178 diff_setup_done(&revs->diffopt);
2180 grep_commit_pattern_type(GREP_PATTERN_TYPE_UNSPECIFIED,
2181 &revs->grep_filter);
2182 compile_grep_patterns(&revs->grep_filter);
2184 if (revs->reverse && revs->reflog_info)
2185 die("cannot combine --reverse with --walk-reflogs");
2186 if (revs->rewrite_parents && revs->children.name)
2187 die("cannot combine --parents and --children");
2190 * Limitations on the graph functionality
2192 if (revs->reverse && revs->graph)
2193 die("cannot combine --reverse with --graph");
2195 if (revs->reflog_info && revs->graph)
2196 die("cannot combine --walk-reflogs with --graph");
2197 if (!revs->reflog_info && revs->grep_filter.use_reflog_filter)
2198 die("cannot use --grep-reflog without --walk-reflogs");
2200 return left;
2203 static void add_child(struct rev_info *revs, struct commit *parent, struct commit *child)
2205 struct commit_list *l = xcalloc(1, sizeof(*l));
2207 l->item = child;
2208 l->next = add_decoration(&revs->children, &parent->object, l);
2211 static int remove_duplicate_parents(struct rev_info *revs, struct commit *commit)
2213 struct treesame_state *ts = lookup_decoration(&revs->treesame, &commit->object);
2214 struct commit_list **pp, *p;
2215 int surviving_parents;
2217 /* Examine existing parents while marking ones we have seen... */
2218 pp = &commit->parents;
2219 surviving_parents = 0;
2220 while ((p = *pp) != NULL) {
2221 struct commit *parent = p->item;
2222 if (parent->object.flags & TMP_MARK) {
2223 *pp = p->next;
2224 if (ts)
2225 compact_treesame(revs, commit, surviving_parents);
2226 continue;
2228 parent->object.flags |= TMP_MARK;
2229 surviving_parents++;
2230 pp = &p->next;
2232 /* clear the temporary mark */
2233 for (p = commit->parents; p; p = p->next) {
2234 p->item->object.flags &= ~TMP_MARK;
2236 /* no update_treesame() - removing duplicates can't affect TREESAME */
2237 return surviving_parents;
2240 struct merge_simplify_state {
2241 struct commit *simplified;
2244 static struct merge_simplify_state *locate_simplify_state(struct rev_info *revs, struct commit *commit)
2246 struct merge_simplify_state *st;
2248 st = lookup_decoration(&revs->merge_simplification, &commit->object);
2249 if (!st) {
2250 st = xcalloc(1, sizeof(*st));
2251 add_decoration(&revs->merge_simplification, &commit->object, st);
2253 return st;
2256 static int mark_redundant_parents(struct rev_info *revs, struct commit *commit)
2258 struct commit_list *h = reduce_heads(commit->parents);
2259 int i = 0, marked = 0;
2260 struct commit_list *po, *pn;
2262 /* Want these for sanity-checking only */
2263 int orig_cnt = commit_list_count(commit->parents);
2264 int cnt = commit_list_count(h);
2267 * Not ready to remove items yet, just mark them for now, based
2268 * on the output of reduce_heads(). reduce_heads outputs the reduced
2269 * set in its original order, so this isn't too hard.
2271 po = commit->parents;
2272 pn = h;
2273 while (po) {
2274 if (pn && po->item == pn->item) {
2275 pn = pn->next;
2276 i++;
2277 } else {
2278 po->item->object.flags |= TMP_MARK;
2279 marked++;
2281 po=po->next;
2284 if (i != cnt || cnt+marked != orig_cnt)
2285 die("mark_redundant_parents %d %d %d %d", orig_cnt, cnt, i, marked);
2287 free_commit_list(h);
2289 return marked;
2292 static int mark_treesame_root_parents(struct rev_info *revs, struct commit *commit)
2294 struct commit_list *p;
2295 int marked = 0;
2297 for (p = commit->parents; p; p = p->next) {
2298 struct commit *parent = p->item;
2299 if (!parent->parents && (parent->object.flags & TREESAME)) {
2300 parent->object.flags |= TMP_MARK;
2301 marked++;
2305 return marked;
2309 * Awkward naming - this means one parent we are TREESAME to.
2310 * cf mark_treesame_root_parents: root parents that are TREESAME (to an
2311 * empty tree). Better name suggestions?
2313 static int leave_one_treesame_to_parent(struct rev_info *revs, struct commit *commit)
2315 struct treesame_state *ts = lookup_decoration(&revs->treesame, &commit->object);
2316 struct commit *unmarked = NULL, *marked = NULL;
2317 struct commit_list *p;
2318 unsigned n;
2320 for (p = commit->parents, n = 0; p; p = p->next, n++) {
2321 if (ts->treesame[n]) {
2322 if (p->item->object.flags & TMP_MARK) {
2323 if (!marked)
2324 marked = p->item;
2325 } else {
2326 if (!unmarked) {
2327 unmarked = p->item;
2328 break;
2335 * If we are TREESAME to a marked-for-deletion parent, but not to any
2336 * unmarked parents, unmark the first TREESAME parent. This is the
2337 * parent that the default simplify_history==1 scan would have followed,
2338 * and it doesn't make sense to omit that path when asking for a
2339 * simplified full history. Retaining it improves the chances of
2340 * understanding odd missed merges that took an old version of a file.
2342 * Example:
2344 * I--------*X A modified the file, but mainline merge X used
2345 * \ / "-s ours", so took the version from I. X is
2346 * `-*A--' TREESAME to I and !TREESAME to A.
2348 * Default log from X would produce "I". Without this check,
2349 * --full-history --simplify-merges would produce "I-A-X", showing
2350 * the merge commit X and that it changed A, but not making clear that
2351 * it had just taken the I version. With this check, the topology above
2352 * is retained.
2354 * Note that it is possible that the simplification chooses a different
2355 * TREESAME parent from the default, in which case this test doesn't
2356 * activate, and we _do_ drop the default parent. Example:
2358 * I------X A modified the file, but it was reverted in B,
2359 * \ / meaning mainline merge X is TREESAME to both
2360 * *A-*B parents.
2362 * Default log would produce "I" by following the first parent;
2363 * --full-history --simplify-merges will produce "I-A-B". But this is a
2364 * reasonable result - it presents a logical full history leading from
2365 * I to X, and X is not an important merge.
2367 if (!unmarked && marked) {
2368 marked->object.flags &= ~TMP_MARK;
2369 return 1;
2372 return 0;
2375 static int remove_marked_parents(struct rev_info *revs, struct commit *commit)
2377 struct commit_list **pp, *p;
2378 int nth_parent, removed = 0;
2380 pp = &commit->parents;
2381 nth_parent = 0;
2382 while ((p = *pp) != NULL) {
2383 struct commit *parent = p->item;
2384 if (parent->object.flags & TMP_MARK) {
2385 parent->object.flags &= ~TMP_MARK;
2386 *pp = p->next;
2387 free(p);
2388 removed++;
2389 compact_treesame(revs, commit, nth_parent);
2390 continue;
2392 pp = &p->next;
2393 nth_parent++;
2396 /* Removing parents can only increase TREESAMEness */
2397 if (removed && !(commit->object.flags & TREESAME))
2398 update_treesame(revs, commit);
2400 return nth_parent;
2403 static struct commit_list **simplify_one(struct rev_info *revs, struct commit *commit, struct commit_list **tail)
2405 struct commit_list *p;
2406 struct commit *parent;
2407 struct merge_simplify_state *st, *pst;
2408 int cnt;
2410 st = locate_simplify_state(revs, commit);
2413 * Have we handled this one?
2415 if (st->simplified)
2416 return tail;
2419 * An UNINTERESTING commit simplifies to itself, so does a
2420 * root commit. We do not rewrite parents of such commit
2421 * anyway.
2423 if ((commit->object.flags & UNINTERESTING) || !commit->parents) {
2424 st->simplified = commit;
2425 return tail;
2429 * Do we know what commit all of our parents that matter
2430 * should be rewritten to? Otherwise we are not ready to
2431 * rewrite this one yet.
2433 for (cnt = 0, p = commit->parents; p; p = p->next) {
2434 pst = locate_simplify_state(revs, p->item);
2435 if (!pst->simplified) {
2436 tail = &commit_list_insert(p->item, tail)->next;
2437 cnt++;
2439 if (revs->first_parent_only)
2440 break;
2442 if (cnt) {
2443 tail = &commit_list_insert(commit, tail)->next;
2444 return tail;
2448 * Rewrite our list of parents. Note that this cannot
2449 * affect our TREESAME flags in any way - a commit is
2450 * always TREESAME to its simplification.
2452 for (p = commit->parents; p; p = p->next) {
2453 pst = locate_simplify_state(revs, p->item);
2454 p->item = pst->simplified;
2455 if (revs->first_parent_only)
2456 break;
2459 if (revs->first_parent_only)
2460 cnt = 1;
2461 else
2462 cnt = remove_duplicate_parents(revs, commit);
2465 * It is possible that we are a merge and one side branch
2466 * does not have any commit that touches the given paths;
2467 * in such a case, the immediate parent from that branch
2468 * will be rewritten to be the merge base.
2470 * o----X X: the commit we are looking at;
2471 * / / o: a commit that touches the paths;
2472 * ---o----'
2474 * Further, a merge of an independent branch that doesn't
2475 * touch the path will reduce to a treesame root parent:
2477 * ----o----X X: the commit we are looking at;
2478 * / o: a commit that touches the paths;
2479 * r r: a root commit not touching the paths
2481 * Detect and simplify both cases.
2483 if (1 < cnt) {
2484 int marked = mark_redundant_parents(revs, commit);
2485 marked += mark_treesame_root_parents(revs, commit);
2486 if (marked)
2487 marked -= leave_one_treesame_to_parent(revs, commit);
2488 if (marked)
2489 cnt = remove_marked_parents(revs, commit);
2493 * A commit simplifies to itself if it is a root, if it is
2494 * UNINTERESTING, if it touches the given paths, or if it is a
2495 * merge and its parents don't simplify to one relevant commit
2496 * (the first two cases are already handled at the beginning of
2497 * this function).
2499 * Otherwise, it simplifies to what its sole relevant parent
2500 * simplifies to.
2502 if (!cnt ||
2503 (commit->object.flags & UNINTERESTING) ||
2504 !(commit->object.flags & TREESAME) ||
2505 (parent = one_relevant_parent(revs, commit->parents)) == NULL)
2506 st->simplified = commit;
2507 else {
2508 pst = locate_simplify_state(revs, parent);
2509 st->simplified = pst->simplified;
2511 return tail;
2514 static void simplify_merges(struct rev_info *revs)
2516 struct commit_list *list, *next;
2517 struct commit_list *yet_to_do, **tail;
2518 struct commit *commit;
2520 if (!revs->prune)
2521 return;
2523 /* feed the list reversed */
2524 yet_to_do = NULL;
2525 for (list = revs->commits; list; list = next) {
2526 commit = list->item;
2527 next = list->next;
2529 * Do not free(list) here yet; the original list
2530 * is used later in this function.
2532 commit_list_insert(commit, &yet_to_do);
2534 while (yet_to_do) {
2535 list = yet_to_do;
2536 yet_to_do = NULL;
2537 tail = &yet_to_do;
2538 while (list) {
2539 commit = list->item;
2540 next = list->next;
2541 free(list);
2542 list = next;
2543 tail = simplify_one(revs, commit, tail);
2547 /* clean up the result, removing the simplified ones */
2548 list = revs->commits;
2549 revs->commits = NULL;
2550 tail = &revs->commits;
2551 while (list) {
2552 struct merge_simplify_state *st;
2554 commit = list->item;
2555 next = list->next;
2556 free(list);
2557 list = next;
2558 st = locate_simplify_state(revs, commit);
2559 if (st->simplified == commit)
2560 tail = &commit_list_insert(commit, tail)->next;
2564 static void set_children(struct rev_info *revs)
2566 struct commit_list *l;
2567 for (l = revs->commits; l; l = l->next) {
2568 struct commit *commit = l->item;
2569 struct commit_list *p;
2571 for (p = commit->parents; p; p = p->next)
2572 add_child(revs, p->item, commit);
2576 void reset_revision_walk(void)
2578 clear_object_flags(SEEN | ADDED | SHOWN);
2581 int prepare_revision_walk(struct rev_info *revs)
2583 int nr = revs->pending.nr;
2584 struct object_array_entry *e, *list;
2585 struct commit_list **next = &revs->commits;
2587 e = list = revs->pending.objects;
2588 revs->pending.nr = 0;
2589 revs->pending.alloc = 0;
2590 revs->pending.objects = NULL;
2591 while (--nr >= 0) {
2592 struct commit *commit = handle_commit(revs, e->item, e->name);
2593 if (commit) {
2594 if (!(commit->object.flags & SEEN)) {
2595 commit->object.flags |= SEEN;
2596 next = commit_list_append(commit, next);
2599 e++;
2601 if (!revs->leak_pending)
2602 free(list);
2604 /* Signal whether we need per-parent treesame decoration */
2605 if (revs->simplify_merges ||
2606 (revs->limited && limiting_can_increase_treesame(revs)))
2607 revs->treesame.name = "treesame";
2609 if (revs->no_walk != REVISION_WALK_NO_WALK_UNSORTED)
2610 commit_list_sort_by_date(&revs->commits);
2611 if (revs->no_walk)
2612 return 0;
2613 if (revs->limited)
2614 if (limit_list(revs) < 0)
2615 return -1;
2616 if (revs->topo_order)
2617 sort_in_topological_order(&revs->commits, revs->sort_order);
2618 if (revs->line_level_traverse)
2619 line_log_filter(revs);
2620 if (revs->simplify_merges)
2621 simplify_merges(revs);
2622 if (revs->children.name)
2623 set_children(revs);
2624 return 0;
2627 static enum rewrite_result rewrite_one(struct rev_info *revs, struct commit **pp)
2629 struct commit_list *cache = NULL;
2631 for (;;) {
2632 struct commit *p = *pp;
2633 if (!revs->limited)
2634 if (add_parents_to_list(revs, p, &revs->commits, &cache) < 0)
2635 return rewrite_one_error;
2636 if (p->object.flags & UNINTERESTING)
2637 return rewrite_one_ok;
2638 if (!(p->object.flags & TREESAME))
2639 return rewrite_one_ok;
2640 if (!p->parents)
2641 return rewrite_one_noparents;
2642 if ((p = one_relevant_parent(revs, p->parents)) == NULL)
2643 return rewrite_one_ok;
2644 *pp = p;
2648 int rewrite_parents(struct rev_info *revs, struct commit *commit,
2649 rewrite_parent_fn_t rewrite_parent)
2651 struct commit_list **pp = &commit->parents;
2652 while (*pp) {
2653 struct commit_list *parent = *pp;
2654 switch (rewrite_parent(revs, &parent->item)) {
2655 case rewrite_one_ok:
2656 break;
2657 case rewrite_one_noparents:
2658 *pp = parent->next;
2659 continue;
2660 case rewrite_one_error:
2661 return -1;
2663 pp = &parent->next;
2665 remove_duplicate_parents(revs, commit);
2666 return 0;
2669 static int commit_rewrite_person(struct strbuf *buf, const char *what, struct string_list *mailmap)
2671 char *person, *endp;
2672 size_t len, namelen, maillen;
2673 const char *name;
2674 const char *mail;
2675 struct ident_split ident;
2677 person = strstr(buf->buf, what);
2678 if (!person)
2679 return 0;
2681 person += strlen(what);
2682 endp = strchr(person, '\n');
2683 if (!endp)
2684 return 0;
2686 len = endp - person;
2688 if (split_ident_line(&ident, person, len))
2689 return 0;
2691 mail = ident.mail_begin;
2692 maillen = ident.mail_end - ident.mail_begin;
2693 name = ident.name_begin;
2694 namelen = ident.name_end - ident.name_begin;
2696 if (map_user(mailmap, &mail, &maillen, &name, &namelen)) {
2697 struct strbuf namemail = STRBUF_INIT;
2699 strbuf_addf(&namemail, "%.*s <%.*s>",
2700 (int)namelen, name, (int)maillen, mail);
2702 strbuf_splice(buf, ident.name_begin - buf->buf,
2703 ident.mail_end - ident.name_begin + 1,
2704 namemail.buf, namemail.len);
2706 strbuf_release(&namemail);
2708 return 1;
2711 return 0;
2714 static int commit_match(struct commit *commit, struct rev_info *opt)
2716 int retval;
2717 const char *encoding;
2718 char *message;
2719 struct strbuf buf = STRBUF_INIT;
2721 if (!opt->grep_filter.pattern_list && !opt->grep_filter.header_list)
2722 return 1;
2724 /* Prepend "fake" headers as needed */
2725 if (opt->grep_filter.use_reflog_filter) {
2726 strbuf_addstr(&buf, "reflog ");
2727 get_reflog_message(&buf, opt->reflog_info);
2728 strbuf_addch(&buf, '\n');
2732 * We grep in the user's output encoding, under the assumption that it
2733 * is the encoding they are most likely to write their grep pattern
2734 * for. In addition, it means we will match the "notes" encoding below,
2735 * so we will not end up with a buffer that has two different encodings
2736 * in it.
2738 encoding = get_log_output_encoding();
2739 message = logmsg_reencode(commit, NULL, encoding);
2741 /* Copy the commit to temporary if we are using "fake" headers */
2742 if (buf.len)
2743 strbuf_addstr(&buf, message);
2745 if (opt->grep_filter.header_list && opt->mailmap) {
2746 if (!buf.len)
2747 strbuf_addstr(&buf, message);
2749 commit_rewrite_person(&buf, "\nauthor ", opt->mailmap);
2750 commit_rewrite_person(&buf, "\ncommitter ", opt->mailmap);
2753 /* Append "fake" message parts as needed */
2754 if (opt->show_notes) {
2755 if (!buf.len)
2756 strbuf_addstr(&buf, message);
2757 format_display_notes(commit->object.sha1, &buf, encoding, 1);
2760 /* Find either in the original commit message, or in the temporary */
2761 if (buf.len)
2762 retval = grep_buffer(&opt->grep_filter, buf.buf, buf.len);
2763 else
2764 retval = grep_buffer(&opt->grep_filter,
2765 message, strlen(message));
2766 strbuf_release(&buf);
2767 logmsg_free(message, commit);
2768 return retval;
2771 static inline int want_ancestry(const struct rev_info *revs)
2773 return (revs->rewrite_parents || revs->children.name);
2776 enum commit_action get_commit_action(struct rev_info *revs, struct commit *commit)
2778 if (commit->object.flags & SHOWN)
2779 return commit_ignore;
2780 if (revs->unpacked && has_sha1_pack(commit->object.sha1))
2781 return commit_ignore;
2782 if (revs->show_all)
2783 return commit_show;
2784 if (commit->object.flags & UNINTERESTING)
2785 return commit_ignore;
2786 if (revs->min_age != -1 && (commit->date > revs->min_age))
2787 return commit_ignore;
2788 if (revs->min_parents || (revs->max_parents >= 0)) {
2789 int n = commit_list_count(commit->parents);
2790 if ((n < revs->min_parents) ||
2791 ((revs->max_parents >= 0) && (n > revs->max_parents)))
2792 return commit_ignore;
2794 if (!commit_match(commit, revs))
2795 return commit_ignore;
2796 if (revs->prune && revs->dense) {
2797 /* Commit without changes? */
2798 if (commit->object.flags & TREESAME) {
2799 int n;
2800 struct commit_list *p;
2801 /* drop merges unless we want parenthood */
2802 if (!want_ancestry(revs))
2803 return commit_ignore;
2805 * If we want ancestry, then need to keep any merges
2806 * between relevant commits to tie together topology.
2807 * For consistency with TREESAME and simplification
2808 * use "relevant" here rather than just INTERESTING,
2809 * to treat bottom commit(s) as part of the topology.
2811 for (n = 0, p = commit->parents; p; p = p->next)
2812 if (relevant_commit(p->item))
2813 if (++n >= 2)
2814 return commit_show;
2815 return commit_ignore;
2818 return commit_show;
2821 enum commit_action simplify_commit(struct rev_info *revs, struct commit *commit)
2823 enum commit_action action = get_commit_action(revs, commit);
2825 if (action == commit_show &&
2826 !revs->show_all &&
2827 revs->prune && revs->dense && want_ancestry(revs)) {
2829 * --full-diff on simplified parents is no good: it
2830 * will show spurious changes from the commits that
2831 * were elided. So we save the parents on the side
2832 * when --full-diff is in effect.
2834 if (revs->full_diff)
2835 save_parents(revs, commit);
2836 if (rewrite_parents(revs, commit, rewrite_one) < 0)
2837 return commit_error;
2839 return action;
2842 static struct commit *get_revision_1(struct rev_info *revs)
2844 if (!revs->commits)
2845 return NULL;
2847 do {
2848 struct commit_list *entry = revs->commits;
2849 struct commit *commit = entry->item;
2851 revs->commits = entry->next;
2852 free(entry);
2854 if (revs->reflog_info) {
2855 save_parents(revs, commit);
2856 fake_reflog_parent(revs->reflog_info, commit);
2857 commit->object.flags &= ~(ADDED | SEEN | SHOWN);
2861 * If we haven't done the list limiting, we need to look at
2862 * the parents here. We also need to do the date-based limiting
2863 * that we'd otherwise have done in limit_list().
2865 if (!revs->limited) {
2866 if (revs->max_age != -1 &&
2867 (commit->date < revs->max_age))
2868 continue;
2869 if (add_parents_to_list(revs, commit, &revs->commits, NULL) < 0)
2870 die("Failed to traverse parents of commit %s",
2871 sha1_to_hex(commit->object.sha1));
2874 switch (simplify_commit(revs, commit)) {
2875 case commit_ignore:
2876 continue;
2877 case commit_error:
2878 die("Failed to simplify parents of commit %s",
2879 sha1_to_hex(commit->object.sha1));
2880 default:
2881 return commit;
2883 } while (revs->commits);
2884 return NULL;
2888 * Return true for entries that have not yet been shown. (This is an
2889 * object_array_each_func_t.)
2891 static int entry_unshown(struct object_array_entry *entry, void *cb_data_unused)
2893 return !(entry->item->flags & SHOWN);
2897 * If array is on the verge of a realloc, garbage-collect any entries
2898 * that have already been shown to try to free up some space.
2900 static void gc_boundary(struct object_array *array)
2902 if (array->nr == array->alloc)
2903 object_array_filter(array, entry_unshown, NULL);
2906 static void create_boundary_commit_list(struct rev_info *revs)
2908 unsigned i;
2909 struct commit *c;
2910 struct object_array *array = &revs->boundary_commits;
2911 struct object_array_entry *objects = array->objects;
2914 * If revs->commits is non-NULL at this point, an error occurred in
2915 * get_revision_1(). Ignore the error and continue printing the
2916 * boundary commits anyway. (This is what the code has always
2917 * done.)
2919 if (revs->commits) {
2920 free_commit_list(revs->commits);
2921 revs->commits = NULL;
2925 * Put all of the actual boundary commits from revs->boundary_commits
2926 * into revs->commits
2928 for (i = 0; i < array->nr; i++) {
2929 c = (struct commit *)(objects[i].item);
2930 if (!c)
2931 continue;
2932 if (!(c->object.flags & CHILD_SHOWN))
2933 continue;
2934 if (c->object.flags & (SHOWN | BOUNDARY))
2935 continue;
2936 c->object.flags |= BOUNDARY;
2937 commit_list_insert(c, &revs->commits);
2941 * If revs->topo_order is set, sort the boundary commits
2942 * in topological order
2944 sort_in_topological_order(&revs->commits, revs->sort_order);
2947 static struct commit *get_revision_internal(struct rev_info *revs)
2949 struct commit *c = NULL;
2950 struct commit_list *l;
2952 if (revs->boundary == 2) {
2954 * All of the normal commits have already been returned,
2955 * and we are now returning boundary commits.
2956 * create_boundary_commit_list() has populated
2957 * revs->commits with the remaining commits to return.
2959 c = pop_commit(&revs->commits);
2960 if (c)
2961 c->object.flags |= SHOWN;
2962 return c;
2966 * If our max_count counter has reached zero, then we are done. We
2967 * don't simply return NULL because we still might need to show
2968 * boundary commits. But we want to avoid calling get_revision_1, which
2969 * might do a considerable amount of work finding the next commit only
2970 * for us to throw it away.
2972 * If it is non-zero, then either we don't have a max_count at all
2973 * (-1), or it is still counting, in which case we decrement.
2975 if (revs->max_count) {
2976 c = get_revision_1(revs);
2977 if (c) {
2978 while (0 < revs->skip_count) {
2979 revs->skip_count--;
2980 c = get_revision_1(revs);
2981 if (!c)
2982 break;
2986 if (revs->max_count > 0)
2987 revs->max_count--;
2990 if (c)
2991 c->object.flags |= SHOWN;
2993 if (!revs->boundary) {
2994 return c;
2997 if (!c) {
2999 * get_revision_1() runs out the commits, and
3000 * we are done computing the boundaries.
3001 * switch to boundary commits output mode.
3003 revs->boundary = 2;
3006 * Update revs->commits to contain the list of
3007 * boundary commits.
3009 create_boundary_commit_list(revs);
3011 return get_revision_internal(revs);
3015 * boundary commits are the commits that are parents of the
3016 * ones we got from get_revision_1() but they themselves are
3017 * not returned from get_revision_1(). Before returning
3018 * 'c', we need to mark its parents that they could be boundaries.
3021 for (l = c->parents; l; l = l->next) {
3022 struct object *p;
3023 p = &(l->item->object);
3024 if (p->flags & (CHILD_SHOWN | SHOWN))
3025 continue;
3026 p->flags |= CHILD_SHOWN;
3027 gc_boundary(&revs->boundary_commits);
3028 add_object_array(p, NULL, &revs->boundary_commits);
3031 return c;
3034 struct commit *get_revision(struct rev_info *revs)
3036 struct commit *c;
3037 struct commit_list *reversed;
3039 if (revs->reverse) {
3040 reversed = NULL;
3041 while ((c = get_revision_internal(revs))) {
3042 commit_list_insert(c, &reversed);
3044 revs->commits = reversed;
3045 revs->reverse = 0;
3046 revs->reverse_output_stage = 1;
3049 if (revs->reverse_output_stage)
3050 return pop_commit(&revs->commits);
3052 c = get_revision_internal(revs);
3053 if (c && revs->graph)
3054 graph_update(revs->graph, c);
3055 if (!c)
3056 free_saved_parents(revs);
3057 return c;
3060 char *get_revision_mark(const struct rev_info *revs, const struct commit *commit)
3062 if (commit->object.flags & BOUNDARY)
3063 return "-";
3064 else if (commit->object.flags & UNINTERESTING)
3065 return "^";
3066 else if (commit->object.flags & PATCHSAME)
3067 return "=";
3068 else if (!revs || revs->left_right) {
3069 if (commit->object.flags & SYMMETRIC_LEFT)
3070 return "<";
3071 else
3072 return ">";
3073 } else if (revs->graph)
3074 return "*";
3075 else if (revs->cherry_mark)
3076 return "+";
3077 return "";
3080 void put_revision_mark(const struct rev_info *revs, const struct commit *commit)
3082 char *mark = get_revision_mark(revs, commit);
3083 if (!strlen(mark))
3084 return;
3085 fputs(mark, stdout);
3086 putchar(' ');
3089 define_commit_slab(saved_parents, struct commit_list *);
3091 #define EMPTY_PARENT_LIST ((struct commit_list *)-1)
3093 void save_parents(struct rev_info *revs, struct commit *commit)
3095 struct commit_list **pp;
3097 if (!revs->saved_parents_slab) {
3098 revs->saved_parents_slab = xmalloc(sizeof(struct saved_parents));
3099 init_saved_parents(revs->saved_parents_slab);
3102 pp = saved_parents_at(revs->saved_parents_slab, commit);
3105 * When walking with reflogs, we may visit the same commit
3106 * several times: once for each appearance in the reflog.
3108 * In this case, save_parents() will be called multiple times.
3109 * We want to keep only the first set of parents. We need to
3110 * store a sentinel value for an empty (i.e., NULL) parent
3111 * list to distinguish it from a not-yet-saved list, however.
3113 if (*pp)
3114 return;
3115 if (commit->parents)
3116 *pp = copy_commit_list(commit->parents);
3117 else
3118 *pp = EMPTY_PARENT_LIST;
3121 struct commit_list *get_saved_parents(struct rev_info *revs, const struct commit *commit)
3123 struct commit_list *parents;
3125 if (!revs->saved_parents_slab)
3126 return commit->parents;
3128 parents = *saved_parents_at(revs->saved_parents_slab, commit);
3129 if (parents == EMPTY_PARENT_LIST)
3130 return NULL;
3131 return parents;
3134 void free_saved_parents(struct rev_info *revs)
3136 if (revs->saved_parents_slab)
3137 clear_saved_parents(revs->saved_parents_slab);