Merge branch 'jk/unavailable-can-be-missing'
[git.git] / revision.c
blob5112c9af9b0784a812ba1c3312acfc254602335e
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 "repository.h"
10 #include "graph.h"
11 #include "grep.h"
12 #include "reflog-walk.h"
13 #include "patch-ids.h"
14 #include "decorate.h"
15 #include "log-tree.h"
16 #include "string-list.h"
17 #include "line-log.h"
18 #include "mailmap.h"
19 #include "commit-slab.h"
20 #include "dir.h"
21 #include "cache-tree.h"
22 #include "bisect.h"
23 #include "packfile.h"
24 #include "worktree.h"
25 #include "argv-array.h"
27 volatile show_early_output_fn_t show_early_output;
29 static const char *term_bad;
30 static const char *term_good;
32 void show_object_with_name(FILE *out, struct object *obj, const char *name)
34 const char *p;
36 fprintf(out, "%s ", oid_to_hex(&obj->oid));
37 for (p = name; *p && *p != '\n'; p++)
38 fputc(*p, out);
39 fputc('\n', out);
42 static void mark_blob_uninteresting(struct blob *blob)
44 if (!blob)
45 return;
46 if (blob->object.flags & UNINTERESTING)
47 return;
48 blob->object.flags |= UNINTERESTING;
51 static void mark_tree_contents_uninteresting(struct tree *tree)
53 struct tree_desc desc;
54 struct name_entry entry;
56 if (parse_tree_gently(tree, 1) < 0)
57 return;
59 init_tree_desc(&desc, tree->buffer, tree->size);
60 while (tree_entry(&desc, &entry)) {
61 switch (object_type(entry.mode)) {
62 case OBJ_TREE:
63 mark_tree_uninteresting(lookup_tree(entry.oid));
64 break;
65 case OBJ_BLOB:
66 mark_blob_uninteresting(lookup_blob(entry.oid));
67 break;
68 default:
69 /* Subproject commit - not in this repository */
70 break;
75 * We don't care about the tree any more
76 * after it has been marked uninteresting.
78 free_tree_buffer(tree);
81 void mark_tree_uninteresting(struct tree *tree)
83 struct object *obj;
85 if (!tree)
86 return;
88 obj = &tree->object;
89 if (obj->flags & UNINTERESTING)
90 return;
91 obj->flags |= UNINTERESTING;
92 mark_tree_contents_uninteresting(tree);
95 struct commit_stack {
96 struct commit **items;
97 size_t nr, alloc;
99 #define COMMIT_STACK_INIT { NULL, 0, 0 }
101 static void commit_stack_push(struct commit_stack *stack, struct commit *commit)
103 ALLOC_GROW(stack->items, stack->nr + 1, stack->alloc);
104 stack->items[stack->nr++] = commit;
107 static struct commit *commit_stack_pop(struct commit_stack *stack)
109 return stack->nr ? stack->items[--stack->nr] : NULL;
112 static void commit_stack_clear(struct commit_stack *stack)
114 FREE_AND_NULL(stack->items);
115 stack->nr = stack->alloc = 0;
118 static void mark_one_parent_uninteresting(struct commit *commit,
119 struct commit_stack *pending)
121 struct commit_list *l;
123 if (commit->object.flags & UNINTERESTING)
124 return;
125 commit->object.flags |= UNINTERESTING;
128 * Normally we haven't parsed the parent
129 * yet, so we won't have a parent of a parent
130 * here. However, it may turn out that we've
131 * reached this commit some other way (where it
132 * wasn't uninteresting), in which case we need
133 * to mark its parents recursively too..
135 for (l = commit->parents; l; l = l->next)
136 commit_stack_push(pending, l->item);
139 void mark_parents_uninteresting(struct commit *commit)
141 struct commit_stack pending = COMMIT_STACK_INIT;
142 struct commit_list *l;
144 for (l = commit->parents; l; l = l->next)
145 mark_one_parent_uninteresting(l->item, &pending);
147 while (pending.nr > 0)
148 mark_one_parent_uninteresting(commit_stack_pop(&pending),
149 &pending);
151 commit_stack_clear(&pending);
154 static void add_pending_object_with_path(struct rev_info *revs,
155 struct object *obj,
156 const char *name, unsigned mode,
157 const char *path)
159 if (!obj)
160 return;
161 if (revs->no_walk && (obj->flags & UNINTERESTING))
162 revs->no_walk = 0;
163 if (revs->reflog_info && obj->type == OBJ_COMMIT) {
164 struct strbuf buf = STRBUF_INIT;
165 int len = interpret_branch_name(name, 0, &buf, 0);
167 if (0 < len && name[len] && buf.len)
168 strbuf_addstr(&buf, name + len);
169 add_reflog_for_walk(revs->reflog_info,
170 (struct commit *)obj,
171 buf.buf[0] ? buf.buf: name);
172 strbuf_release(&buf);
173 return; /* do not add the commit itself */
175 add_object_array_with_path(obj, name, &revs->pending, mode, path);
178 static void add_pending_object_with_mode(struct rev_info *revs,
179 struct object *obj,
180 const char *name, unsigned mode)
182 add_pending_object_with_path(revs, obj, name, mode, NULL);
185 void add_pending_object(struct rev_info *revs,
186 struct object *obj, const char *name)
188 add_pending_object_with_mode(revs, obj, name, S_IFINVALID);
191 void add_head_to_pending(struct rev_info *revs)
193 struct object_id oid;
194 struct object *obj;
195 if (get_oid("HEAD", &oid))
196 return;
197 obj = parse_object(&oid);
198 if (!obj)
199 return;
200 add_pending_object(revs, obj, "HEAD");
203 static struct object *get_reference(struct rev_info *revs, const char *name,
204 const struct object_id *oid,
205 unsigned int flags)
207 struct object *object;
209 object = parse_object(oid);
210 if (!object) {
211 if (revs->ignore_missing)
212 return object;
213 if (revs->exclude_promisor_objects && is_promisor_object(oid))
214 return NULL;
215 die("bad object %s", name);
217 object->flags |= flags;
218 return object;
221 void add_pending_oid(struct rev_info *revs, const char *name,
222 const struct object_id *oid, unsigned int flags)
224 struct object *object = get_reference(revs, name, oid, flags);
225 add_pending_object(revs, object, name);
228 static struct commit *handle_commit(struct rev_info *revs,
229 struct object_array_entry *entry)
231 struct object *object = entry->item;
232 const char *name = entry->name;
233 const char *path = entry->path;
234 unsigned int mode = entry->mode;
235 unsigned long flags = object->flags;
238 * Tag object? Look what it points to..
240 while (object->type == OBJ_TAG) {
241 struct tag *tag = (struct tag *) object;
242 if (revs->tag_objects && !(flags & UNINTERESTING))
243 add_pending_object(revs, object, tag->tag);
244 if (!tag->tagged)
245 die("bad tag");
246 object = parse_object(&tag->tagged->oid);
247 if (!object) {
248 if (revs->ignore_missing_links || (flags & UNINTERESTING))
249 return NULL;
250 die("bad object %s", oid_to_hex(&tag->tagged->oid));
252 object->flags |= flags;
254 * We'll handle the tagged object by looping or dropping
255 * through to the non-tag handlers below. Do not
256 * propagate path data from the tag's pending entry.
258 path = NULL;
259 mode = 0;
263 * Commit object? Just return it, we'll do all the complex
264 * reachability crud.
266 if (object->type == OBJ_COMMIT) {
267 struct commit *commit = (struct commit *)object;
268 if (parse_commit(commit) < 0)
269 die("unable to parse commit %s", name);
270 if (flags & UNINTERESTING) {
271 mark_parents_uninteresting(commit);
272 revs->limited = 1;
274 if (revs->show_source && !commit->util)
275 commit->util = xstrdup(name);
276 return commit;
280 * Tree object? Either mark it uninteresting, or add it
281 * to the list of objects to look at later..
283 if (object->type == OBJ_TREE) {
284 struct tree *tree = (struct tree *)object;
285 if (!revs->tree_objects)
286 return NULL;
287 if (flags & UNINTERESTING) {
288 mark_tree_contents_uninteresting(tree);
289 return NULL;
291 add_pending_object_with_path(revs, object, name, mode, path);
292 return NULL;
296 * Blob object? You know the drill by now..
298 if (object->type == OBJ_BLOB) {
299 if (!revs->blob_objects)
300 return NULL;
301 if (flags & UNINTERESTING)
302 return NULL;
303 add_pending_object_with_path(revs, object, name, mode, path);
304 return NULL;
306 die("%s is unknown object", name);
309 static int everybody_uninteresting(struct commit_list *orig,
310 struct commit **interesting_cache)
312 struct commit_list *list = orig;
314 if (*interesting_cache) {
315 struct commit *commit = *interesting_cache;
316 if (!(commit->object.flags & UNINTERESTING))
317 return 0;
320 while (list) {
321 struct commit *commit = list->item;
322 list = list->next;
323 if (commit->object.flags & UNINTERESTING)
324 continue;
326 *interesting_cache = commit;
327 return 0;
329 return 1;
333 * A definition of "relevant" commit that we can use to simplify limited graphs
334 * by eliminating side branches.
336 * A "relevant" commit is one that is !UNINTERESTING (ie we are including it
337 * in our list), or that is a specified BOTTOM commit. Then after computing
338 * a limited list, during processing we can generally ignore boundary merges
339 * coming from outside the graph, (ie from irrelevant parents), and treat
340 * those merges as if they were single-parent. TREESAME is defined to consider
341 * only relevant parents, if any. If we are TREESAME to our on-graph parents,
342 * we don't care if we were !TREESAME to non-graph parents.
344 * Treating bottom commits as relevant ensures that a limited graph's
345 * connection to the actual bottom commit is not viewed as a side branch, but
346 * treated as part of the graph. For example:
348 * ....Z...A---X---o---o---B
349 * . /
350 * W---Y
352 * When computing "A..B", the A-X connection is at least as important as
353 * Y-X, despite A being flagged UNINTERESTING.
355 * And when computing --ancestry-path "A..B", the A-X connection is more
356 * important than Y-X, despite both A and Y being flagged UNINTERESTING.
358 static inline int relevant_commit(struct commit *commit)
360 return (commit->object.flags & (UNINTERESTING | BOTTOM)) != UNINTERESTING;
364 * Return a single relevant commit from a parent list. If we are a TREESAME
365 * commit, and this selects one of our parents, then we can safely simplify to
366 * that parent.
368 static struct commit *one_relevant_parent(const struct rev_info *revs,
369 struct commit_list *orig)
371 struct commit_list *list = orig;
372 struct commit *relevant = NULL;
374 if (!orig)
375 return NULL;
378 * For 1-parent commits, or if first-parent-only, then return that
379 * first parent (even if not "relevant" by the above definition).
380 * TREESAME will have been set purely on that parent.
382 if (revs->first_parent_only || !orig->next)
383 return orig->item;
386 * For multi-parent commits, identify a sole relevant parent, if any.
387 * If we have only one relevant parent, then TREESAME will be set purely
388 * with regard to that parent, and we can simplify accordingly.
390 * If we have more than one relevant parent, or no relevant parents
391 * (and multiple irrelevant ones), then we can't select a parent here
392 * and return NULL.
394 while (list) {
395 struct commit *commit = list->item;
396 list = list->next;
397 if (relevant_commit(commit)) {
398 if (relevant)
399 return NULL;
400 relevant = commit;
403 return relevant;
407 * The goal is to get REV_TREE_NEW as the result only if the
408 * diff consists of all '+' (and no other changes), REV_TREE_OLD
409 * if the whole diff is removal of old data, and otherwise
410 * REV_TREE_DIFFERENT (of course if the trees are the same we
411 * want REV_TREE_SAME).
413 * The only time we care about the distinction is when
414 * remove_empty_trees is in effect, in which case we care only about
415 * whether the whole change is REV_TREE_NEW, or if there's another type
416 * of change. Which means we can stop the diff early in either of these
417 * cases:
419 * 1. We're not using remove_empty_trees at all.
421 * 2. We saw anything except REV_TREE_NEW.
423 static int tree_difference = REV_TREE_SAME;
425 static void file_add_remove(struct diff_options *options,
426 int addremove, unsigned mode,
427 const struct object_id *oid,
428 int oid_valid,
429 const char *fullpath, unsigned dirty_submodule)
431 int diff = addremove == '+' ? REV_TREE_NEW : REV_TREE_OLD;
432 struct rev_info *revs = options->change_fn_data;
434 tree_difference |= diff;
435 if (!revs->remove_empty_trees || tree_difference != REV_TREE_NEW)
436 options->flags.has_changes = 1;
439 static void file_change(struct diff_options *options,
440 unsigned old_mode, unsigned new_mode,
441 const struct object_id *old_oid,
442 const struct object_id *new_oid,
443 int old_oid_valid, int new_oid_valid,
444 const char *fullpath,
445 unsigned old_dirty_submodule, unsigned new_dirty_submodule)
447 tree_difference = REV_TREE_DIFFERENT;
448 options->flags.has_changes = 1;
451 static int rev_compare_tree(struct rev_info *revs,
452 struct commit *parent, struct commit *commit)
454 struct tree *t1 = get_commit_tree(parent);
455 struct tree *t2 = get_commit_tree(commit);
457 if (!t1)
458 return REV_TREE_NEW;
459 if (!t2)
460 return REV_TREE_OLD;
462 if (revs->simplify_by_decoration) {
464 * If we are simplifying by decoration, then the commit
465 * is worth showing if it has a tag pointing at it.
467 if (get_name_decoration(&commit->object))
468 return REV_TREE_DIFFERENT;
470 * A commit that is not pointed by a tag is uninteresting
471 * if we are not limited by path. This means that you will
472 * see the usual "commits that touch the paths" plus any
473 * tagged commit by specifying both --simplify-by-decoration
474 * and pathspec.
476 if (!revs->prune_data.nr)
477 return REV_TREE_SAME;
480 tree_difference = REV_TREE_SAME;
481 revs->pruning.flags.has_changes = 0;
482 if (diff_tree_oid(&t1->object.oid, &t2->object.oid, "",
483 &revs->pruning) < 0)
484 return REV_TREE_DIFFERENT;
485 return tree_difference;
488 static int rev_same_tree_as_empty(struct rev_info *revs, struct commit *commit)
490 int retval;
491 struct tree *t1 = get_commit_tree(commit);
493 if (!t1)
494 return 0;
496 tree_difference = REV_TREE_SAME;
497 revs->pruning.flags.has_changes = 0;
498 retval = diff_tree_oid(NULL, &t1->object.oid, "", &revs->pruning);
500 return retval >= 0 && (tree_difference == REV_TREE_SAME);
503 struct treesame_state {
504 unsigned int nparents;
505 unsigned char treesame[FLEX_ARRAY];
508 static struct treesame_state *initialise_treesame(struct rev_info *revs, struct commit *commit)
510 unsigned n = commit_list_count(commit->parents);
511 struct treesame_state *st = xcalloc(1, st_add(sizeof(*st), n));
512 st->nparents = n;
513 add_decoration(&revs->treesame, &commit->object, st);
514 return st;
518 * Must be called immediately after removing the nth_parent from a commit's
519 * parent list, if we are maintaining the per-parent treesame[] decoration.
520 * This does not recalculate the master TREESAME flag - update_treesame()
521 * should be called to update it after a sequence of treesame[] modifications
522 * that may have affected it.
524 static int compact_treesame(struct rev_info *revs, struct commit *commit, unsigned nth_parent)
526 struct treesame_state *st;
527 int old_same;
529 if (!commit->parents) {
531 * Have just removed the only parent from a non-merge.
532 * Different handling, as we lack decoration.
534 if (nth_parent != 0)
535 die("compact_treesame %u", nth_parent);
536 old_same = !!(commit->object.flags & TREESAME);
537 if (rev_same_tree_as_empty(revs, commit))
538 commit->object.flags |= TREESAME;
539 else
540 commit->object.flags &= ~TREESAME;
541 return old_same;
544 st = lookup_decoration(&revs->treesame, &commit->object);
545 if (!st || nth_parent >= st->nparents)
546 die("compact_treesame %u", nth_parent);
548 old_same = st->treesame[nth_parent];
549 memmove(st->treesame + nth_parent,
550 st->treesame + nth_parent + 1,
551 st->nparents - nth_parent - 1);
554 * If we've just become a non-merge commit, update TREESAME
555 * immediately, and remove the no-longer-needed decoration.
556 * If still a merge, defer update until update_treesame().
558 if (--st->nparents == 1) {
559 if (commit->parents->next)
560 die("compact_treesame parents mismatch");
561 if (st->treesame[0] && revs->dense)
562 commit->object.flags |= TREESAME;
563 else
564 commit->object.flags &= ~TREESAME;
565 free(add_decoration(&revs->treesame, &commit->object, NULL));
568 return old_same;
571 static unsigned update_treesame(struct rev_info *revs, struct commit *commit)
573 if (commit->parents && commit->parents->next) {
574 unsigned n;
575 struct treesame_state *st;
576 struct commit_list *p;
577 unsigned relevant_parents;
578 unsigned relevant_change, irrelevant_change;
580 st = lookup_decoration(&revs->treesame, &commit->object);
581 if (!st)
582 die("update_treesame %s", oid_to_hex(&commit->object.oid));
583 relevant_parents = 0;
584 relevant_change = irrelevant_change = 0;
585 for (p = commit->parents, n = 0; p; n++, p = p->next) {
586 if (relevant_commit(p->item)) {
587 relevant_change |= !st->treesame[n];
588 relevant_parents++;
589 } else
590 irrelevant_change |= !st->treesame[n];
592 if (relevant_parents ? relevant_change : irrelevant_change)
593 commit->object.flags &= ~TREESAME;
594 else
595 commit->object.flags |= TREESAME;
598 return commit->object.flags & TREESAME;
601 static inline int limiting_can_increase_treesame(const struct rev_info *revs)
604 * TREESAME is irrelevant unless prune && dense;
605 * if simplify_history is set, we can't have a mixture of TREESAME and
606 * !TREESAME INTERESTING parents (and we don't have treesame[]
607 * decoration anyway);
608 * if first_parent_only is set, then the TREESAME flag is locked
609 * against the first parent (and again we lack treesame[] decoration).
611 return revs->prune && revs->dense &&
612 !revs->simplify_history &&
613 !revs->first_parent_only;
616 static void try_to_simplify_commit(struct rev_info *revs, struct commit *commit)
618 struct commit_list **pp, *parent;
619 struct treesame_state *ts = NULL;
620 int relevant_change = 0, irrelevant_change = 0;
621 int relevant_parents, nth_parent;
624 * If we don't do pruning, everything is interesting
626 if (!revs->prune)
627 return;
629 if (!get_commit_tree(commit))
630 return;
632 if (!commit->parents) {
633 if (rev_same_tree_as_empty(revs, commit))
634 commit->object.flags |= TREESAME;
635 return;
639 * Normal non-merge commit? If we don't want to make the
640 * history dense, we consider it always to be a change..
642 if (!revs->dense && !commit->parents->next)
643 return;
645 for (pp = &commit->parents, nth_parent = 0, relevant_parents = 0;
646 (parent = *pp) != NULL;
647 pp = &parent->next, nth_parent++) {
648 struct commit *p = parent->item;
649 if (relevant_commit(p))
650 relevant_parents++;
652 if (nth_parent == 1) {
654 * This our second loop iteration - so we now know
655 * we're dealing with a merge.
657 * Do not compare with later parents when we care only about
658 * the first parent chain, in order to avoid derailing the
659 * traversal to follow a side branch that brought everything
660 * in the path we are limited to by the pathspec.
662 if (revs->first_parent_only)
663 break;
665 * If this will remain a potentially-simplifiable
666 * merge, remember per-parent treesame if needed.
667 * Initialise the array with the comparison from our
668 * first iteration.
670 if (revs->treesame.name &&
671 !revs->simplify_history &&
672 !(commit->object.flags & UNINTERESTING)) {
673 ts = initialise_treesame(revs, commit);
674 if (!(irrelevant_change || relevant_change))
675 ts->treesame[0] = 1;
678 if (parse_commit(p) < 0)
679 die("cannot simplify commit %s (because of %s)",
680 oid_to_hex(&commit->object.oid),
681 oid_to_hex(&p->object.oid));
682 switch (rev_compare_tree(revs, p, commit)) {
683 case REV_TREE_SAME:
684 if (!revs->simplify_history || !relevant_commit(p)) {
685 /* Even if a merge with an uninteresting
686 * side branch brought the entire change
687 * we are interested in, we do not want
688 * to lose the other branches of this
689 * merge, so we just keep going.
691 if (ts)
692 ts->treesame[nth_parent] = 1;
693 continue;
695 parent->next = NULL;
696 commit->parents = parent;
697 commit->object.flags |= TREESAME;
698 return;
700 case REV_TREE_NEW:
701 if (revs->remove_empty_trees &&
702 rev_same_tree_as_empty(revs, p)) {
703 /* We are adding all the specified
704 * paths from this parent, so the
705 * history beyond this parent is not
706 * interesting. Remove its parents
707 * (they are grandparents for us).
708 * IOW, we pretend this parent is a
709 * "root" commit.
711 if (parse_commit(p) < 0)
712 die("cannot simplify commit %s (invalid %s)",
713 oid_to_hex(&commit->object.oid),
714 oid_to_hex(&p->object.oid));
715 p->parents = NULL;
717 /* fallthrough */
718 case REV_TREE_OLD:
719 case REV_TREE_DIFFERENT:
720 if (relevant_commit(p))
721 relevant_change = 1;
722 else
723 irrelevant_change = 1;
724 continue;
726 die("bad tree compare for commit %s", oid_to_hex(&commit->object.oid));
730 * TREESAME is straightforward for single-parent commits. For merge
731 * commits, it is most useful to define it so that "irrelevant"
732 * parents cannot make us !TREESAME - if we have any relevant
733 * parents, then we only consider TREESAMEness with respect to them,
734 * allowing irrelevant merges from uninteresting branches to be
735 * simplified away. Only if we have only irrelevant parents do we
736 * base TREESAME on them. Note that this logic is replicated in
737 * update_treesame, which should be kept in sync.
739 if (relevant_parents ? !relevant_change : !irrelevant_change)
740 commit->object.flags |= TREESAME;
743 static void commit_list_insert_by_date_cached(struct commit *p, struct commit_list **head,
744 struct commit_list *cached_base, struct commit_list **cache)
746 struct commit_list *new_entry;
748 if (cached_base && p->date < cached_base->item->date)
749 new_entry = commit_list_insert_by_date(p, &cached_base->next);
750 else
751 new_entry = commit_list_insert_by_date(p, head);
753 if (cache && (!*cache || p->date < (*cache)->item->date))
754 *cache = new_entry;
757 static int add_parents_to_list(struct rev_info *revs, struct commit *commit,
758 struct commit_list **list, struct commit_list **cache_ptr)
760 struct commit_list *parent = commit->parents;
761 unsigned left_flag;
762 struct commit_list *cached_base = cache_ptr ? *cache_ptr : NULL;
764 if (commit->object.flags & ADDED)
765 return 0;
766 commit->object.flags |= ADDED;
768 if (revs->include_check &&
769 !revs->include_check(commit, revs->include_check_data))
770 return 0;
773 * If the commit is uninteresting, don't try to
774 * prune parents - we want the maximal uninteresting
775 * set.
777 * Normally we haven't parsed the parent
778 * yet, so we won't have a parent of a parent
779 * here. However, it may turn out that we've
780 * reached this commit some other way (where it
781 * wasn't uninteresting), in which case we need
782 * to mark its parents recursively too..
784 if (commit->object.flags & UNINTERESTING) {
785 while (parent) {
786 struct commit *p = parent->item;
787 parent = parent->next;
788 if (p)
789 p->object.flags |= UNINTERESTING;
790 if (parse_commit_gently(p, 1) < 0)
791 continue;
792 if (p->parents)
793 mark_parents_uninteresting(p);
794 if (p->object.flags & SEEN)
795 continue;
796 p->object.flags |= SEEN;
797 commit_list_insert_by_date_cached(p, list, cached_base, cache_ptr);
799 return 0;
803 * Ok, the commit wasn't uninteresting. Try to
804 * simplify the commit history and find the parent
805 * that has no differences in the path set if one exists.
807 try_to_simplify_commit(revs, commit);
809 if (revs->no_walk)
810 return 0;
812 left_flag = (commit->object.flags & SYMMETRIC_LEFT);
814 for (parent = commit->parents; parent; parent = parent->next) {
815 struct commit *p = parent->item;
816 int gently = revs->ignore_missing_links ||
817 revs->exclude_promisor_objects;
818 if (parse_commit_gently(p, gently) < 0) {
819 if (revs->exclude_promisor_objects &&
820 is_promisor_object(&p->object.oid)) {
821 if (revs->first_parent_only)
822 break;
823 continue;
825 return -1;
827 if (revs->show_source && !p->util)
828 p->util = commit->util;
829 p->object.flags |= left_flag;
830 if (!(p->object.flags & SEEN)) {
831 p->object.flags |= SEEN;
832 commit_list_insert_by_date_cached(p, list, cached_base, cache_ptr);
834 if (revs->first_parent_only)
835 break;
837 return 0;
840 static void cherry_pick_list(struct commit_list *list, struct rev_info *revs)
842 struct commit_list *p;
843 int left_count = 0, right_count = 0;
844 int left_first;
845 struct patch_ids ids;
846 unsigned cherry_flag;
848 /* First count the commits on the left and on the right */
849 for (p = list; p; p = p->next) {
850 struct commit *commit = p->item;
851 unsigned flags = commit->object.flags;
852 if (flags & BOUNDARY)
854 else if (flags & SYMMETRIC_LEFT)
855 left_count++;
856 else
857 right_count++;
860 if (!left_count || !right_count)
861 return;
863 left_first = left_count < right_count;
864 init_patch_ids(&ids);
865 ids.diffopts.pathspec = revs->diffopt.pathspec;
867 /* Compute patch-ids for one side */
868 for (p = list; p; p = p->next) {
869 struct commit *commit = p->item;
870 unsigned flags = commit->object.flags;
872 if (flags & BOUNDARY)
873 continue;
875 * If we have fewer left, left_first is set and we omit
876 * commits on the right branch in this loop. If we have
877 * fewer right, we skip the left ones.
879 if (left_first != !!(flags & SYMMETRIC_LEFT))
880 continue;
881 add_commit_patch_id(commit, &ids);
884 /* either cherry_mark or cherry_pick are true */
885 cherry_flag = revs->cherry_mark ? PATCHSAME : SHOWN;
887 /* Check the other side */
888 for (p = list; p; p = p->next) {
889 struct commit *commit = p->item;
890 struct patch_id *id;
891 unsigned flags = commit->object.flags;
893 if (flags & BOUNDARY)
894 continue;
896 * If we have fewer left, left_first is set and we omit
897 * commits on the left branch in this loop.
899 if (left_first == !!(flags & SYMMETRIC_LEFT))
900 continue;
903 * Have we seen the same patch id?
905 id = has_commit_patch_id(commit, &ids);
906 if (!id)
907 continue;
909 commit->object.flags |= cherry_flag;
910 id->commit->object.flags |= cherry_flag;
913 free_patch_ids(&ids);
916 /* How many extra uninteresting commits we want to see.. */
917 #define SLOP 5
919 static int still_interesting(struct commit_list *src, timestamp_t date, int slop,
920 struct commit **interesting_cache)
923 * No source list at all? We're definitely done..
925 if (!src)
926 return 0;
929 * Does the destination list contain entries with a date
930 * before the source list? Definitely _not_ done.
932 if (date <= src->item->date)
933 return SLOP;
936 * Does the source list still have interesting commits in
937 * it? Definitely not done..
939 if (!everybody_uninteresting(src, interesting_cache))
940 return SLOP;
942 /* Ok, we're closing in.. */
943 return slop-1;
947 * "rev-list --ancestry-path A..B" computes commits that are ancestors
948 * of B but not ancestors of A but further limits the result to those
949 * that are descendants of A. This takes the list of bottom commits and
950 * the result of "A..B" without --ancestry-path, and limits the latter
951 * further to the ones that can reach one of the commits in "bottom".
953 static void limit_to_ancestry(struct commit_list *bottom, struct commit_list *list)
955 struct commit_list *p;
956 struct commit_list *rlist = NULL;
957 int made_progress;
960 * Reverse the list so that it will be likely that we would
961 * process parents before children.
963 for (p = list; p; p = p->next)
964 commit_list_insert(p->item, &rlist);
966 for (p = bottom; p; p = p->next)
967 p->item->object.flags |= TMP_MARK;
970 * Mark the ones that can reach bottom commits in "list",
971 * in a bottom-up fashion.
973 do {
974 made_progress = 0;
975 for (p = rlist; p; p = p->next) {
976 struct commit *c = p->item;
977 struct commit_list *parents;
978 if (c->object.flags & (TMP_MARK | UNINTERESTING))
979 continue;
980 for (parents = c->parents;
981 parents;
982 parents = parents->next) {
983 if (!(parents->item->object.flags & TMP_MARK))
984 continue;
985 c->object.flags |= TMP_MARK;
986 made_progress = 1;
987 break;
990 } while (made_progress);
993 * NEEDSWORK: decide if we want to remove parents that are
994 * not marked with TMP_MARK from commit->parents for commits
995 * in the resulting list. We may not want to do that, though.
999 * The ones that are not marked with TMP_MARK are uninteresting
1001 for (p = list; p; p = p->next) {
1002 struct commit *c = p->item;
1003 if (c->object.flags & TMP_MARK)
1004 continue;
1005 c->object.flags |= UNINTERESTING;
1008 /* We are done with the TMP_MARK */
1009 for (p = list; p; p = p->next)
1010 p->item->object.flags &= ~TMP_MARK;
1011 for (p = bottom; p; p = p->next)
1012 p->item->object.flags &= ~TMP_MARK;
1013 free_commit_list(rlist);
1017 * Before walking the history, keep the set of "negative" refs the
1018 * caller has asked to exclude.
1020 * This is used to compute "rev-list --ancestry-path A..B", as we need
1021 * to filter the result of "A..B" further to the ones that can actually
1022 * reach A.
1024 static struct commit_list *collect_bottom_commits(struct commit_list *list)
1026 struct commit_list *elem, *bottom = NULL;
1027 for (elem = list; elem; elem = elem->next)
1028 if (elem->item->object.flags & BOTTOM)
1029 commit_list_insert(elem->item, &bottom);
1030 return bottom;
1033 /* Assumes either left_only or right_only is set */
1034 static void limit_left_right(struct commit_list *list, struct rev_info *revs)
1036 struct commit_list *p;
1038 for (p = list; p; p = p->next) {
1039 struct commit *commit = p->item;
1041 if (revs->right_only) {
1042 if (commit->object.flags & SYMMETRIC_LEFT)
1043 commit->object.flags |= SHOWN;
1044 } else /* revs->left_only is set */
1045 if (!(commit->object.flags & SYMMETRIC_LEFT))
1046 commit->object.flags |= SHOWN;
1050 static int limit_list(struct rev_info *revs)
1052 int slop = SLOP;
1053 timestamp_t date = TIME_MAX;
1054 struct commit_list *list = revs->commits;
1055 struct commit_list *newlist = NULL;
1056 struct commit_list **p = &newlist;
1057 struct commit_list *bottom = NULL;
1058 struct commit *interesting_cache = NULL;
1060 if (revs->ancestry_path) {
1061 bottom = collect_bottom_commits(list);
1062 if (!bottom)
1063 die("--ancestry-path given but there are no bottom commits");
1066 while (list) {
1067 struct commit *commit = pop_commit(&list);
1068 struct object *obj = &commit->object;
1069 show_early_output_fn_t show;
1071 if (commit == interesting_cache)
1072 interesting_cache = NULL;
1074 if (revs->max_age != -1 && (commit->date < revs->max_age))
1075 obj->flags |= UNINTERESTING;
1076 if (add_parents_to_list(revs, commit, &list, NULL) < 0)
1077 return -1;
1078 if (obj->flags & UNINTERESTING) {
1079 mark_parents_uninteresting(commit);
1080 slop = still_interesting(list, date, slop, &interesting_cache);
1081 if (slop)
1082 continue;
1083 break;
1085 if (revs->min_age != -1 && (commit->date > revs->min_age))
1086 continue;
1087 date = commit->date;
1088 p = &commit_list_insert(commit, p)->next;
1090 show = show_early_output;
1091 if (!show)
1092 continue;
1094 show(revs, newlist);
1095 show_early_output = NULL;
1097 if (revs->cherry_pick || revs->cherry_mark)
1098 cherry_pick_list(newlist, revs);
1100 if (revs->left_only || revs->right_only)
1101 limit_left_right(newlist, revs);
1103 if (bottom) {
1104 limit_to_ancestry(bottom, newlist);
1105 free_commit_list(bottom);
1109 * Check if any commits have become TREESAME by some of their parents
1110 * becoming UNINTERESTING.
1112 if (limiting_can_increase_treesame(revs))
1113 for (list = newlist; list; list = list->next) {
1114 struct commit *c = list->item;
1115 if (c->object.flags & (UNINTERESTING | TREESAME))
1116 continue;
1117 update_treesame(revs, c);
1120 revs->commits = newlist;
1121 return 0;
1125 * Add an entry to refs->cmdline with the specified information.
1126 * *name is copied.
1128 static void add_rev_cmdline(struct rev_info *revs,
1129 struct object *item,
1130 const char *name,
1131 int whence,
1132 unsigned flags)
1134 struct rev_cmdline_info *info = &revs->cmdline;
1135 unsigned int nr = info->nr;
1137 ALLOC_GROW(info->rev, nr + 1, info->alloc);
1138 info->rev[nr].item = item;
1139 info->rev[nr].name = xstrdup(name);
1140 info->rev[nr].whence = whence;
1141 info->rev[nr].flags = flags;
1142 info->nr++;
1145 static void add_rev_cmdline_list(struct rev_info *revs,
1146 struct commit_list *commit_list,
1147 int whence,
1148 unsigned flags)
1150 while (commit_list) {
1151 struct object *object = &commit_list->item->object;
1152 add_rev_cmdline(revs, object, oid_to_hex(&object->oid),
1153 whence, flags);
1154 commit_list = commit_list->next;
1158 struct all_refs_cb {
1159 int all_flags;
1160 int warned_bad_reflog;
1161 struct rev_info *all_revs;
1162 const char *name_for_errormsg;
1163 struct ref_store *refs;
1166 int ref_excluded(struct string_list *ref_excludes, const char *path)
1168 struct string_list_item *item;
1170 if (!ref_excludes)
1171 return 0;
1172 for_each_string_list_item(item, ref_excludes) {
1173 if (!wildmatch(item->string, path, 0))
1174 return 1;
1176 return 0;
1179 static int handle_one_ref(const char *path, const struct object_id *oid,
1180 int flag, void *cb_data)
1182 struct all_refs_cb *cb = cb_data;
1183 struct object *object;
1185 if (ref_excluded(cb->all_revs->ref_excludes, path))
1186 return 0;
1188 object = get_reference(cb->all_revs, path, oid, cb->all_flags);
1189 add_rev_cmdline(cb->all_revs, object, path, REV_CMD_REF, cb->all_flags);
1190 add_pending_oid(cb->all_revs, path, oid, cb->all_flags);
1191 return 0;
1194 static void init_all_refs_cb(struct all_refs_cb *cb, struct rev_info *revs,
1195 unsigned flags)
1197 cb->all_revs = revs;
1198 cb->all_flags = flags;
1199 revs->rev_input_given = 1;
1200 cb->refs = NULL;
1203 void clear_ref_exclusion(struct string_list **ref_excludes_p)
1205 if (*ref_excludes_p) {
1206 string_list_clear(*ref_excludes_p, 0);
1207 free(*ref_excludes_p);
1209 *ref_excludes_p = NULL;
1212 void add_ref_exclusion(struct string_list **ref_excludes_p, const char *exclude)
1214 if (!*ref_excludes_p) {
1215 *ref_excludes_p = xcalloc(1, sizeof(**ref_excludes_p));
1216 (*ref_excludes_p)->strdup_strings = 1;
1218 string_list_append(*ref_excludes_p, exclude);
1221 static void handle_refs(struct ref_store *refs,
1222 struct rev_info *revs, unsigned flags,
1223 int (*for_each)(struct ref_store *, each_ref_fn, void *))
1225 struct all_refs_cb cb;
1227 if (!refs) {
1228 /* this could happen with uninitialized submodules */
1229 return;
1232 init_all_refs_cb(&cb, revs, flags);
1233 for_each(refs, handle_one_ref, &cb);
1236 static void handle_one_reflog_commit(struct object_id *oid, void *cb_data)
1238 struct all_refs_cb *cb = cb_data;
1239 if (!is_null_oid(oid)) {
1240 struct object *o = parse_object(oid);
1241 if (o) {
1242 o->flags |= cb->all_flags;
1243 /* ??? CMDLINEFLAGS ??? */
1244 add_pending_object(cb->all_revs, o, "");
1246 else if (!cb->warned_bad_reflog) {
1247 warning("reflog of '%s' references pruned commits",
1248 cb->name_for_errormsg);
1249 cb->warned_bad_reflog = 1;
1254 static int handle_one_reflog_ent(struct object_id *ooid, struct object_id *noid,
1255 const char *email, timestamp_t timestamp, int tz,
1256 const char *message, void *cb_data)
1258 handle_one_reflog_commit(ooid, cb_data);
1259 handle_one_reflog_commit(noid, cb_data);
1260 return 0;
1263 static int handle_one_reflog(const char *path, const struct object_id *oid,
1264 int flag, void *cb_data)
1266 struct all_refs_cb *cb = cb_data;
1267 cb->warned_bad_reflog = 0;
1268 cb->name_for_errormsg = path;
1269 refs_for_each_reflog_ent(cb->refs, path,
1270 handle_one_reflog_ent, cb_data);
1271 return 0;
1274 static void add_other_reflogs_to_pending(struct all_refs_cb *cb)
1276 struct worktree **worktrees, **p;
1278 worktrees = get_worktrees(0);
1279 for (p = worktrees; *p; p++) {
1280 struct worktree *wt = *p;
1282 if (wt->is_current)
1283 continue;
1285 cb->refs = get_worktree_ref_store(wt);
1286 refs_for_each_reflog(cb->refs,
1287 handle_one_reflog,
1288 cb);
1290 free_worktrees(worktrees);
1293 void add_reflogs_to_pending(struct rev_info *revs, unsigned flags)
1295 struct all_refs_cb cb;
1297 cb.all_revs = revs;
1298 cb.all_flags = flags;
1299 cb.refs = get_main_ref_store(the_repository);
1300 for_each_reflog(handle_one_reflog, &cb);
1302 if (!revs->single_worktree)
1303 add_other_reflogs_to_pending(&cb);
1306 static void add_cache_tree(struct cache_tree *it, struct rev_info *revs,
1307 struct strbuf *path)
1309 size_t baselen = path->len;
1310 int i;
1312 if (it->entry_count >= 0) {
1313 struct tree *tree = lookup_tree(&it->oid);
1314 add_pending_object_with_path(revs, &tree->object, "",
1315 040000, path->buf);
1318 for (i = 0; i < it->subtree_nr; i++) {
1319 struct cache_tree_sub *sub = it->down[i];
1320 strbuf_addf(path, "%s%s", baselen ? "/" : "", sub->name);
1321 add_cache_tree(sub->cache_tree, revs, path);
1322 strbuf_setlen(path, baselen);
1327 static void do_add_index_objects_to_pending(struct rev_info *revs,
1328 struct index_state *istate)
1330 int i;
1332 for (i = 0; i < istate->cache_nr; i++) {
1333 struct cache_entry *ce = istate->cache[i];
1334 struct blob *blob;
1336 if (S_ISGITLINK(ce->ce_mode))
1337 continue;
1339 blob = lookup_blob(&ce->oid);
1340 if (!blob)
1341 die("unable to add index blob to traversal");
1342 add_pending_object_with_path(revs, &blob->object, "",
1343 ce->ce_mode, ce->name);
1346 if (istate->cache_tree) {
1347 struct strbuf path = STRBUF_INIT;
1348 add_cache_tree(istate->cache_tree, revs, &path);
1349 strbuf_release(&path);
1353 void add_index_objects_to_pending(struct rev_info *revs, unsigned int flags)
1355 struct worktree **worktrees, **p;
1357 read_cache();
1358 do_add_index_objects_to_pending(revs, &the_index);
1360 if (revs->single_worktree)
1361 return;
1363 worktrees = get_worktrees(0);
1364 for (p = worktrees; *p; p++) {
1365 struct worktree *wt = *p;
1366 struct index_state istate = { NULL };
1368 if (wt->is_current)
1369 continue; /* current index already taken care of */
1371 if (read_index_from(&istate,
1372 worktree_git_path(wt, "index"),
1373 get_worktree_git_dir(wt)) > 0)
1374 do_add_index_objects_to_pending(revs, &istate);
1375 discard_index(&istate);
1377 free_worktrees(worktrees);
1380 static int add_parents_only(struct rev_info *revs, const char *arg_, int flags,
1381 int exclude_parent)
1383 struct object_id oid;
1384 struct object *it;
1385 struct commit *commit;
1386 struct commit_list *parents;
1387 int parent_number;
1388 const char *arg = arg_;
1390 if (*arg == '^') {
1391 flags ^= UNINTERESTING | BOTTOM;
1392 arg++;
1394 if (get_oid_committish(arg, &oid))
1395 return 0;
1396 while (1) {
1397 it = get_reference(revs, arg, &oid, 0);
1398 if (!it && revs->ignore_missing)
1399 return 0;
1400 if (it->type != OBJ_TAG)
1401 break;
1402 if (!((struct tag*)it)->tagged)
1403 return 0;
1404 oidcpy(&oid, &((struct tag*)it)->tagged->oid);
1406 if (it->type != OBJ_COMMIT)
1407 return 0;
1408 commit = (struct commit *)it;
1409 if (exclude_parent &&
1410 exclude_parent > commit_list_count(commit->parents))
1411 return 0;
1412 for (parents = commit->parents, parent_number = 1;
1413 parents;
1414 parents = parents->next, parent_number++) {
1415 if (exclude_parent && parent_number != exclude_parent)
1416 continue;
1418 it = &parents->item->object;
1419 it->flags |= flags;
1420 add_rev_cmdline(revs, it, arg_, REV_CMD_PARENTS_ONLY, flags);
1421 add_pending_object(revs, it, arg);
1423 return 1;
1426 void init_revisions(struct rev_info *revs, const char *prefix)
1428 memset(revs, 0, sizeof(*revs));
1430 revs->abbrev = DEFAULT_ABBREV;
1431 revs->ignore_merges = 1;
1432 revs->simplify_history = 1;
1433 revs->pruning.flags.recursive = 1;
1434 revs->pruning.flags.quick = 1;
1435 revs->pruning.add_remove = file_add_remove;
1436 revs->pruning.change = file_change;
1437 revs->pruning.change_fn_data = revs;
1438 revs->sort_order = REV_SORT_IN_GRAPH_ORDER;
1439 revs->dense = 1;
1440 revs->prefix = prefix;
1441 revs->max_age = -1;
1442 revs->min_age = -1;
1443 revs->skip_count = -1;
1444 revs->max_count = -1;
1445 revs->max_parents = -1;
1446 revs->expand_tabs_in_log = -1;
1448 revs->commit_format = CMIT_FMT_DEFAULT;
1449 revs->expand_tabs_in_log_default = 8;
1451 init_grep_defaults();
1452 grep_init(&revs->grep_filter, prefix);
1453 revs->grep_filter.status_only = 1;
1455 diff_setup(&revs->diffopt);
1456 if (prefix && !revs->diffopt.prefix) {
1457 revs->diffopt.prefix = prefix;
1458 revs->diffopt.prefix_length = strlen(prefix);
1461 revs->notes_opt.use_default_notes = -1;
1464 static void add_pending_commit_list(struct rev_info *revs,
1465 struct commit_list *commit_list,
1466 unsigned int flags)
1468 while (commit_list) {
1469 struct object *object = &commit_list->item->object;
1470 object->flags |= flags;
1471 add_pending_object(revs, object, oid_to_hex(&object->oid));
1472 commit_list = commit_list->next;
1476 static void prepare_show_merge(struct rev_info *revs)
1478 struct commit_list *bases;
1479 struct commit *head, *other;
1480 struct object_id oid;
1481 const char **prune = NULL;
1482 int i, prune_num = 1; /* counting terminating NULL */
1484 if (get_oid("HEAD", &oid))
1485 die("--merge without HEAD?");
1486 head = lookup_commit_or_die(&oid, "HEAD");
1487 if (get_oid("MERGE_HEAD", &oid))
1488 die("--merge without MERGE_HEAD?");
1489 other = lookup_commit_or_die(&oid, "MERGE_HEAD");
1490 add_pending_object(revs, &head->object, "HEAD");
1491 add_pending_object(revs, &other->object, "MERGE_HEAD");
1492 bases = get_merge_bases(head, other);
1493 add_rev_cmdline_list(revs, bases, REV_CMD_MERGE_BASE, UNINTERESTING | BOTTOM);
1494 add_pending_commit_list(revs, bases, UNINTERESTING | BOTTOM);
1495 free_commit_list(bases);
1496 head->object.flags |= SYMMETRIC_LEFT;
1498 if (!active_nr)
1499 read_cache();
1500 for (i = 0; i < active_nr; i++) {
1501 const struct cache_entry *ce = active_cache[i];
1502 if (!ce_stage(ce))
1503 continue;
1504 if (ce_path_match(ce, &revs->prune_data, NULL)) {
1505 prune_num++;
1506 REALLOC_ARRAY(prune, prune_num);
1507 prune[prune_num-2] = ce->name;
1508 prune[prune_num-1] = NULL;
1510 while ((i+1 < active_nr) &&
1511 ce_same_name(ce, active_cache[i+1]))
1512 i++;
1514 clear_pathspec(&revs->prune_data);
1515 parse_pathspec(&revs->prune_data, PATHSPEC_ALL_MAGIC & ~PATHSPEC_LITERAL,
1516 PATHSPEC_PREFER_FULL | PATHSPEC_LITERAL_PATH, "", prune);
1517 revs->limited = 1;
1520 static int dotdot_missing(const char *arg, char *dotdot,
1521 struct rev_info *revs, int symmetric)
1523 if (revs->ignore_missing)
1524 return 0;
1525 /* de-munge so we report the full argument */
1526 *dotdot = '.';
1527 die(symmetric
1528 ? "Invalid symmetric difference expression %s"
1529 : "Invalid revision range %s", arg);
1532 static int handle_dotdot_1(const char *arg, char *dotdot,
1533 struct rev_info *revs, int flags,
1534 int cant_be_filename,
1535 struct object_context *a_oc,
1536 struct object_context *b_oc)
1538 const char *a_name, *b_name;
1539 struct object_id a_oid, b_oid;
1540 struct object *a_obj, *b_obj;
1541 unsigned int a_flags, b_flags;
1542 int symmetric = 0;
1543 unsigned int flags_exclude = flags ^ (UNINTERESTING | BOTTOM);
1544 unsigned int oc_flags = GET_OID_COMMITTISH | GET_OID_RECORD_PATH;
1546 a_name = arg;
1547 if (!*a_name)
1548 a_name = "HEAD";
1550 b_name = dotdot + 2;
1551 if (*b_name == '.') {
1552 symmetric = 1;
1553 b_name++;
1555 if (!*b_name)
1556 b_name = "HEAD";
1558 if (get_oid_with_context(a_name, oc_flags, &a_oid, a_oc) ||
1559 get_oid_with_context(b_name, oc_flags, &b_oid, b_oc))
1560 return -1;
1562 if (!cant_be_filename) {
1563 *dotdot = '.';
1564 verify_non_filename(revs->prefix, arg);
1565 *dotdot = '\0';
1568 a_obj = parse_object(&a_oid);
1569 b_obj = parse_object(&b_oid);
1570 if (!a_obj || !b_obj)
1571 return dotdot_missing(arg, dotdot, revs, symmetric);
1573 if (!symmetric) {
1574 /* just A..B */
1575 b_flags = flags;
1576 a_flags = flags_exclude;
1577 } else {
1578 /* A...B -- find merge bases between the two */
1579 struct commit *a, *b;
1580 struct commit_list *exclude;
1582 a = lookup_commit_reference(&a_obj->oid);
1583 b = lookup_commit_reference(&b_obj->oid);
1584 if (!a || !b)
1585 return dotdot_missing(arg, dotdot, revs, symmetric);
1587 exclude = get_merge_bases(a, b);
1588 add_rev_cmdline_list(revs, exclude, REV_CMD_MERGE_BASE,
1589 flags_exclude);
1590 add_pending_commit_list(revs, exclude, flags_exclude);
1591 free_commit_list(exclude);
1593 b_flags = flags;
1594 a_flags = flags | SYMMETRIC_LEFT;
1597 a_obj->flags |= a_flags;
1598 b_obj->flags |= b_flags;
1599 add_rev_cmdline(revs, a_obj, a_name, REV_CMD_LEFT, a_flags);
1600 add_rev_cmdline(revs, b_obj, b_name, REV_CMD_RIGHT, b_flags);
1601 add_pending_object_with_path(revs, a_obj, a_name, a_oc->mode, a_oc->path);
1602 add_pending_object_with_path(revs, b_obj, b_name, b_oc->mode, b_oc->path);
1603 return 0;
1606 static int handle_dotdot(const char *arg,
1607 struct rev_info *revs, int flags,
1608 int cant_be_filename)
1610 struct object_context a_oc, b_oc;
1611 char *dotdot = strstr(arg, "..");
1612 int ret;
1614 if (!dotdot)
1615 return -1;
1617 memset(&a_oc, 0, sizeof(a_oc));
1618 memset(&b_oc, 0, sizeof(b_oc));
1620 *dotdot = '\0';
1621 ret = handle_dotdot_1(arg, dotdot, revs, flags, cant_be_filename,
1622 &a_oc, &b_oc);
1623 *dotdot = '.';
1625 free(a_oc.path);
1626 free(b_oc.path);
1628 return ret;
1631 int handle_revision_arg(const char *arg_, struct rev_info *revs, int flags, unsigned revarg_opt)
1633 struct object_context oc;
1634 char *mark;
1635 struct object *object;
1636 struct object_id oid;
1637 int local_flags;
1638 const char *arg = arg_;
1639 int cant_be_filename = revarg_opt & REVARG_CANNOT_BE_FILENAME;
1640 unsigned get_sha1_flags = GET_OID_RECORD_PATH;
1642 flags = flags & UNINTERESTING ? flags | BOTTOM : flags & ~BOTTOM;
1644 if (!cant_be_filename && !strcmp(arg, "..")) {
1646 * Just ".."? That is not a range but the
1647 * pathspec for the parent directory.
1649 return -1;
1652 if (!handle_dotdot(arg, revs, flags, revarg_opt))
1653 return 0;
1655 mark = strstr(arg, "^@");
1656 if (mark && !mark[2]) {
1657 *mark = 0;
1658 if (add_parents_only(revs, arg, flags, 0))
1659 return 0;
1660 *mark = '^';
1662 mark = strstr(arg, "^!");
1663 if (mark && !mark[2]) {
1664 *mark = 0;
1665 if (!add_parents_only(revs, arg, flags ^ (UNINTERESTING | BOTTOM), 0))
1666 *mark = '^';
1668 mark = strstr(arg, "^-");
1669 if (mark) {
1670 int exclude_parent = 1;
1672 if (mark[2]) {
1673 char *end;
1674 exclude_parent = strtoul(mark + 2, &end, 10);
1675 if (*end != '\0' || !exclude_parent)
1676 return -1;
1679 *mark = 0;
1680 if (!add_parents_only(revs, arg, flags ^ (UNINTERESTING | BOTTOM), exclude_parent))
1681 *mark = '^';
1684 local_flags = 0;
1685 if (*arg == '^') {
1686 local_flags = UNINTERESTING | BOTTOM;
1687 arg++;
1690 if (revarg_opt & REVARG_COMMITTISH)
1691 get_sha1_flags |= GET_OID_COMMITTISH;
1693 if (get_oid_with_context(arg, get_sha1_flags, &oid, &oc))
1694 return revs->ignore_missing ? 0 : -1;
1695 if (!cant_be_filename)
1696 verify_non_filename(revs->prefix, arg);
1697 object = get_reference(revs, arg, &oid, flags ^ local_flags);
1698 add_rev_cmdline(revs, object, arg_, REV_CMD_REV, flags ^ local_flags);
1699 add_pending_object_with_path(revs, object, arg, oc.mode, oc.path);
1700 free(oc.path);
1701 return 0;
1704 static void read_pathspec_from_stdin(struct rev_info *revs, struct strbuf *sb,
1705 struct argv_array *prune)
1707 while (strbuf_getline(sb, stdin) != EOF)
1708 argv_array_push(prune, sb->buf);
1711 static void read_revisions_from_stdin(struct rev_info *revs,
1712 struct argv_array *prune)
1714 struct strbuf sb;
1715 int seen_dashdash = 0;
1716 int save_warning;
1718 save_warning = warn_on_object_refname_ambiguity;
1719 warn_on_object_refname_ambiguity = 0;
1721 strbuf_init(&sb, 1000);
1722 while (strbuf_getline(&sb, stdin) != EOF) {
1723 int len = sb.len;
1724 if (!len)
1725 break;
1726 if (sb.buf[0] == '-') {
1727 if (len == 2 && sb.buf[1] == '-') {
1728 seen_dashdash = 1;
1729 break;
1731 die("options not supported in --stdin mode");
1733 if (handle_revision_arg(sb.buf, revs, 0,
1734 REVARG_CANNOT_BE_FILENAME))
1735 die("bad revision '%s'", sb.buf);
1737 if (seen_dashdash)
1738 read_pathspec_from_stdin(revs, &sb, prune);
1740 strbuf_release(&sb);
1741 warn_on_object_refname_ambiguity = save_warning;
1744 static void add_grep(struct rev_info *revs, const char *ptn, enum grep_pat_token what)
1746 append_grep_pattern(&revs->grep_filter, ptn, "command line", 0, what);
1749 static void add_header_grep(struct rev_info *revs, enum grep_header_field field, const char *pattern)
1751 append_header_grep_pattern(&revs->grep_filter, field, pattern);
1754 static void add_message_grep(struct rev_info *revs, const char *pattern)
1756 add_grep(revs, pattern, GREP_PATTERN_BODY);
1759 static int handle_revision_opt(struct rev_info *revs, int argc, const char **argv,
1760 int *unkc, const char **unkv)
1762 const char *arg = argv[0];
1763 const char *optarg;
1764 int argcount;
1766 /* pseudo revision arguments */
1767 if (!strcmp(arg, "--all") || !strcmp(arg, "--branches") ||
1768 !strcmp(arg, "--tags") || !strcmp(arg, "--remotes") ||
1769 !strcmp(arg, "--reflog") || !strcmp(arg, "--not") ||
1770 !strcmp(arg, "--no-walk") || !strcmp(arg, "--do-walk") ||
1771 !strcmp(arg, "--bisect") || starts_with(arg, "--glob=") ||
1772 !strcmp(arg, "--indexed-objects") ||
1773 starts_with(arg, "--exclude=") ||
1774 starts_with(arg, "--branches=") || starts_with(arg, "--tags=") ||
1775 starts_with(arg, "--remotes=") || starts_with(arg, "--no-walk="))
1777 unkv[(*unkc)++] = arg;
1778 return 1;
1781 if ((argcount = parse_long_opt("max-count", argv, &optarg))) {
1782 revs->max_count = atoi(optarg);
1783 revs->no_walk = 0;
1784 return argcount;
1785 } else if ((argcount = parse_long_opt("skip", argv, &optarg))) {
1786 revs->skip_count = atoi(optarg);
1787 return argcount;
1788 } else if ((*arg == '-') && isdigit(arg[1])) {
1789 /* accept -<digit>, like traditional "head" */
1790 if (strtol_i(arg + 1, 10, &revs->max_count) < 0 ||
1791 revs->max_count < 0)
1792 die("'%s': not a non-negative integer", arg + 1);
1793 revs->no_walk = 0;
1794 } else if (!strcmp(arg, "-n")) {
1795 if (argc <= 1)
1796 return error("-n requires an argument");
1797 revs->max_count = atoi(argv[1]);
1798 revs->no_walk = 0;
1799 return 2;
1800 } else if (skip_prefix(arg, "-n", &optarg)) {
1801 revs->max_count = atoi(optarg);
1802 revs->no_walk = 0;
1803 } else if ((argcount = parse_long_opt("max-age", argv, &optarg))) {
1804 revs->max_age = atoi(optarg);
1805 return argcount;
1806 } else if ((argcount = parse_long_opt("since", argv, &optarg))) {
1807 revs->max_age = approxidate(optarg);
1808 return argcount;
1809 } else if ((argcount = parse_long_opt("after", argv, &optarg))) {
1810 revs->max_age = approxidate(optarg);
1811 return argcount;
1812 } else if ((argcount = parse_long_opt("min-age", argv, &optarg))) {
1813 revs->min_age = atoi(optarg);
1814 return argcount;
1815 } else if ((argcount = parse_long_opt("before", argv, &optarg))) {
1816 revs->min_age = approxidate(optarg);
1817 return argcount;
1818 } else if ((argcount = parse_long_opt("until", argv, &optarg))) {
1819 revs->min_age = approxidate(optarg);
1820 return argcount;
1821 } else if (!strcmp(arg, "--first-parent")) {
1822 revs->first_parent_only = 1;
1823 } else if (!strcmp(arg, "--ancestry-path")) {
1824 revs->ancestry_path = 1;
1825 revs->simplify_history = 0;
1826 revs->limited = 1;
1827 } else if (!strcmp(arg, "-g") || !strcmp(arg, "--walk-reflogs")) {
1828 init_reflog_walk(&revs->reflog_info);
1829 } else if (!strcmp(arg, "--default")) {
1830 if (argc <= 1)
1831 return error("bad --default argument");
1832 revs->def = argv[1];
1833 return 2;
1834 } else if (!strcmp(arg, "--merge")) {
1835 revs->show_merge = 1;
1836 } else if (!strcmp(arg, "--topo-order")) {
1837 revs->sort_order = REV_SORT_IN_GRAPH_ORDER;
1838 revs->topo_order = 1;
1839 } else if (!strcmp(arg, "--simplify-merges")) {
1840 revs->simplify_merges = 1;
1841 revs->topo_order = 1;
1842 revs->rewrite_parents = 1;
1843 revs->simplify_history = 0;
1844 revs->limited = 1;
1845 } else if (!strcmp(arg, "--simplify-by-decoration")) {
1846 revs->simplify_merges = 1;
1847 revs->topo_order = 1;
1848 revs->rewrite_parents = 1;
1849 revs->simplify_history = 0;
1850 revs->simplify_by_decoration = 1;
1851 revs->limited = 1;
1852 revs->prune = 1;
1853 load_ref_decorations(NULL, DECORATE_SHORT_REFS);
1854 } else if (!strcmp(arg, "--date-order")) {
1855 revs->sort_order = REV_SORT_BY_COMMIT_DATE;
1856 revs->topo_order = 1;
1857 } else if (!strcmp(arg, "--author-date-order")) {
1858 revs->sort_order = REV_SORT_BY_AUTHOR_DATE;
1859 revs->topo_order = 1;
1860 } else if (!strcmp(arg, "--early-output")) {
1861 revs->early_output = 100;
1862 revs->topo_order = 1;
1863 } else if (skip_prefix(arg, "--early-output=", &optarg)) {
1864 if (strtoul_ui(optarg, 10, &revs->early_output) < 0)
1865 die("'%s': not a non-negative integer", optarg);
1866 revs->topo_order = 1;
1867 } else if (!strcmp(arg, "--parents")) {
1868 revs->rewrite_parents = 1;
1869 revs->print_parents = 1;
1870 } else if (!strcmp(arg, "--dense")) {
1871 revs->dense = 1;
1872 } else if (!strcmp(arg, "--sparse")) {
1873 revs->dense = 0;
1874 } else if (!strcmp(arg, "--in-commit-order")) {
1875 revs->tree_blobs_in_commit_order = 1;
1876 } else if (!strcmp(arg, "--remove-empty")) {
1877 revs->remove_empty_trees = 1;
1878 } else if (!strcmp(arg, "--merges")) {
1879 revs->min_parents = 2;
1880 } else if (!strcmp(arg, "--no-merges")) {
1881 revs->max_parents = 1;
1882 } else if (skip_prefix(arg, "--min-parents=", &optarg)) {
1883 revs->min_parents = atoi(optarg);
1884 } else if (!strcmp(arg, "--no-min-parents")) {
1885 revs->min_parents = 0;
1886 } else if (skip_prefix(arg, "--max-parents=", &optarg)) {
1887 revs->max_parents = atoi(optarg);
1888 } else if (!strcmp(arg, "--no-max-parents")) {
1889 revs->max_parents = -1;
1890 } else if (!strcmp(arg, "--boundary")) {
1891 revs->boundary = 1;
1892 } else if (!strcmp(arg, "--left-right")) {
1893 revs->left_right = 1;
1894 } else if (!strcmp(arg, "--left-only")) {
1895 if (revs->right_only)
1896 die("--left-only is incompatible with --right-only"
1897 " or --cherry");
1898 revs->left_only = 1;
1899 } else if (!strcmp(arg, "--right-only")) {
1900 if (revs->left_only)
1901 die("--right-only is incompatible with --left-only");
1902 revs->right_only = 1;
1903 } else if (!strcmp(arg, "--cherry")) {
1904 if (revs->left_only)
1905 die("--cherry is incompatible with --left-only");
1906 revs->cherry_mark = 1;
1907 revs->right_only = 1;
1908 revs->max_parents = 1;
1909 revs->limited = 1;
1910 } else if (!strcmp(arg, "--count")) {
1911 revs->count = 1;
1912 } else if (!strcmp(arg, "--cherry-mark")) {
1913 if (revs->cherry_pick)
1914 die("--cherry-mark is incompatible with --cherry-pick");
1915 revs->cherry_mark = 1;
1916 revs->limited = 1; /* needs limit_list() */
1917 } else if (!strcmp(arg, "--cherry-pick")) {
1918 if (revs->cherry_mark)
1919 die("--cherry-pick is incompatible with --cherry-mark");
1920 revs->cherry_pick = 1;
1921 revs->limited = 1;
1922 } else if (!strcmp(arg, "--objects")) {
1923 revs->tag_objects = 1;
1924 revs->tree_objects = 1;
1925 revs->blob_objects = 1;
1926 } else if (!strcmp(arg, "--objects-edge")) {
1927 revs->tag_objects = 1;
1928 revs->tree_objects = 1;
1929 revs->blob_objects = 1;
1930 revs->edge_hint = 1;
1931 } else if (!strcmp(arg, "--objects-edge-aggressive")) {
1932 revs->tag_objects = 1;
1933 revs->tree_objects = 1;
1934 revs->blob_objects = 1;
1935 revs->edge_hint = 1;
1936 revs->edge_hint_aggressive = 1;
1937 } else if (!strcmp(arg, "--verify-objects")) {
1938 revs->tag_objects = 1;
1939 revs->tree_objects = 1;
1940 revs->blob_objects = 1;
1941 revs->verify_objects = 1;
1942 } else if (!strcmp(arg, "--unpacked")) {
1943 revs->unpacked = 1;
1944 } else if (starts_with(arg, "--unpacked=")) {
1945 die("--unpacked=<packfile> no longer supported.");
1946 } else if (!strcmp(arg, "-r")) {
1947 revs->diff = 1;
1948 revs->diffopt.flags.recursive = 1;
1949 } else if (!strcmp(arg, "-t")) {
1950 revs->diff = 1;
1951 revs->diffopt.flags.recursive = 1;
1952 revs->diffopt.flags.tree_in_recursive = 1;
1953 } else if (!strcmp(arg, "-m")) {
1954 revs->ignore_merges = 0;
1955 } else if (!strcmp(arg, "-c")) {
1956 revs->diff = 1;
1957 revs->dense_combined_merges = 0;
1958 revs->combine_merges = 1;
1959 } else if (!strcmp(arg, "--cc")) {
1960 revs->diff = 1;
1961 revs->dense_combined_merges = 1;
1962 revs->combine_merges = 1;
1963 } else if (!strcmp(arg, "-v")) {
1964 revs->verbose_header = 1;
1965 } else if (!strcmp(arg, "--pretty")) {
1966 revs->verbose_header = 1;
1967 revs->pretty_given = 1;
1968 get_commit_format(NULL, revs);
1969 } else if (skip_prefix(arg, "--pretty=", &optarg) ||
1970 skip_prefix(arg, "--format=", &optarg)) {
1972 * Detached form ("--pretty X" as opposed to "--pretty=X")
1973 * not allowed, since the argument is optional.
1975 revs->verbose_header = 1;
1976 revs->pretty_given = 1;
1977 get_commit_format(optarg, revs);
1978 } else if (!strcmp(arg, "--expand-tabs")) {
1979 revs->expand_tabs_in_log = 8;
1980 } else if (!strcmp(arg, "--no-expand-tabs")) {
1981 revs->expand_tabs_in_log = 0;
1982 } else if (skip_prefix(arg, "--expand-tabs=", &arg)) {
1983 int val;
1984 if (strtol_i(arg, 10, &val) < 0 || val < 0)
1985 die("'%s': not a non-negative integer", arg);
1986 revs->expand_tabs_in_log = val;
1987 } else if (!strcmp(arg, "--show-notes") || !strcmp(arg, "--notes")) {
1988 revs->show_notes = 1;
1989 revs->show_notes_given = 1;
1990 revs->notes_opt.use_default_notes = 1;
1991 } else if (!strcmp(arg, "--show-signature")) {
1992 revs->show_signature = 1;
1993 } else if (!strcmp(arg, "--no-show-signature")) {
1994 revs->show_signature = 0;
1995 } else if (!strcmp(arg, "--show-linear-break")) {
1996 revs->break_bar = " ..........";
1997 revs->track_linear = 1;
1998 revs->track_first_time = 1;
1999 } else if (skip_prefix(arg, "--show-linear-break=", &optarg)) {
2000 revs->break_bar = xstrdup(optarg);
2001 revs->track_linear = 1;
2002 revs->track_first_time = 1;
2003 } else if (skip_prefix(arg, "--show-notes=", &optarg) ||
2004 skip_prefix(arg, "--notes=", &optarg)) {
2005 struct strbuf buf = STRBUF_INIT;
2006 revs->show_notes = 1;
2007 revs->show_notes_given = 1;
2008 if (starts_with(arg, "--show-notes=") &&
2009 revs->notes_opt.use_default_notes < 0)
2010 revs->notes_opt.use_default_notes = 1;
2011 strbuf_addstr(&buf, optarg);
2012 expand_notes_ref(&buf);
2013 string_list_append(&revs->notes_opt.extra_notes_refs,
2014 strbuf_detach(&buf, NULL));
2015 } else if (!strcmp(arg, "--no-notes")) {
2016 revs->show_notes = 0;
2017 revs->show_notes_given = 1;
2018 revs->notes_opt.use_default_notes = -1;
2019 /* we have been strdup'ing ourselves, so trick
2020 * string_list into free()ing strings */
2021 revs->notes_opt.extra_notes_refs.strdup_strings = 1;
2022 string_list_clear(&revs->notes_opt.extra_notes_refs, 0);
2023 revs->notes_opt.extra_notes_refs.strdup_strings = 0;
2024 } else if (!strcmp(arg, "--standard-notes")) {
2025 revs->show_notes_given = 1;
2026 revs->notes_opt.use_default_notes = 1;
2027 } else if (!strcmp(arg, "--no-standard-notes")) {
2028 revs->notes_opt.use_default_notes = 0;
2029 } else if (!strcmp(arg, "--oneline")) {
2030 revs->verbose_header = 1;
2031 get_commit_format("oneline", revs);
2032 revs->pretty_given = 1;
2033 revs->abbrev_commit = 1;
2034 } else if (!strcmp(arg, "--graph")) {
2035 revs->topo_order = 1;
2036 revs->rewrite_parents = 1;
2037 revs->graph = graph_init(revs);
2038 } else if (!strcmp(arg, "--root")) {
2039 revs->show_root_diff = 1;
2040 } else if (!strcmp(arg, "--no-commit-id")) {
2041 revs->no_commit_id = 1;
2042 } else if (!strcmp(arg, "--always")) {
2043 revs->always_show_header = 1;
2044 } else if (!strcmp(arg, "--no-abbrev")) {
2045 revs->abbrev = 0;
2046 } else if (!strcmp(arg, "--abbrev")) {
2047 revs->abbrev = DEFAULT_ABBREV;
2048 } else if (skip_prefix(arg, "--abbrev=", &optarg)) {
2049 revs->abbrev = strtoul(optarg, NULL, 10);
2050 if (revs->abbrev < MINIMUM_ABBREV)
2051 revs->abbrev = MINIMUM_ABBREV;
2052 else if (revs->abbrev > 40)
2053 revs->abbrev = 40;
2054 } else if (!strcmp(arg, "--abbrev-commit")) {
2055 revs->abbrev_commit = 1;
2056 revs->abbrev_commit_given = 1;
2057 } else if (!strcmp(arg, "--no-abbrev-commit")) {
2058 revs->abbrev_commit = 0;
2059 } else if (!strcmp(arg, "--full-diff")) {
2060 revs->diff = 1;
2061 revs->full_diff = 1;
2062 } else if (!strcmp(arg, "--full-history")) {
2063 revs->simplify_history = 0;
2064 } else if (!strcmp(arg, "--relative-date")) {
2065 revs->date_mode.type = DATE_RELATIVE;
2066 revs->date_mode_explicit = 1;
2067 } else if ((argcount = parse_long_opt("date", argv, &optarg))) {
2068 parse_date_format(optarg, &revs->date_mode);
2069 revs->date_mode_explicit = 1;
2070 return argcount;
2071 } else if (!strcmp(arg, "--log-size")) {
2072 revs->show_log_size = 1;
2075 * Grepping the commit log
2077 else if ((argcount = parse_long_opt("author", argv, &optarg))) {
2078 add_header_grep(revs, GREP_HEADER_AUTHOR, optarg);
2079 return argcount;
2080 } else if ((argcount = parse_long_opt("committer", argv, &optarg))) {
2081 add_header_grep(revs, GREP_HEADER_COMMITTER, optarg);
2082 return argcount;
2083 } else if ((argcount = parse_long_opt("grep-reflog", argv, &optarg))) {
2084 add_header_grep(revs, GREP_HEADER_REFLOG, optarg);
2085 return argcount;
2086 } else if ((argcount = parse_long_opt("grep", argv, &optarg))) {
2087 add_message_grep(revs, optarg);
2088 return argcount;
2089 } else if (!strcmp(arg, "--grep-debug")) {
2090 revs->grep_filter.debug = 1;
2091 } else if (!strcmp(arg, "--basic-regexp")) {
2092 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_BRE;
2093 } else if (!strcmp(arg, "--extended-regexp") || !strcmp(arg, "-E")) {
2094 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_ERE;
2095 } else if (!strcmp(arg, "--regexp-ignore-case") || !strcmp(arg, "-i")) {
2096 revs->grep_filter.ignore_case = 1;
2097 revs->diffopt.pickaxe_opts |= DIFF_PICKAXE_IGNORE_CASE;
2098 } else if (!strcmp(arg, "--fixed-strings") || !strcmp(arg, "-F")) {
2099 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_FIXED;
2100 } else if (!strcmp(arg, "--perl-regexp") || !strcmp(arg, "-P")) {
2101 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_PCRE;
2102 } else if (!strcmp(arg, "--all-match")) {
2103 revs->grep_filter.all_match = 1;
2104 } else if (!strcmp(arg, "--invert-grep")) {
2105 revs->invert_grep = 1;
2106 } else if ((argcount = parse_long_opt("encoding", argv, &optarg))) {
2107 if (strcmp(optarg, "none"))
2108 git_log_output_encoding = xstrdup(optarg);
2109 else
2110 git_log_output_encoding = "";
2111 return argcount;
2112 } else if (!strcmp(arg, "--reverse")) {
2113 revs->reverse ^= 1;
2114 } else if (!strcmp(arg, "--children")) {
2115 revs->children.name = "children";
2116 revs->limited = 1;
2117 } else if (!strcmp(arg, "--ignore-missing")) {
2118 revs->ignore_missing = 1;
2119 } else if (!strcmp(arg, "--exclude-promisor-objects")) {
2120 if (fetch_if_missing)
2121 BUG("exclude_promisor_objects can only be used when fetch_if_missing is 0");
2122 revs->exclude_promisor_objects = 1;
2123 } else {
2124 int opts = diff_opt_parse(&revs->diffopt, argv, argc, revs->prefix);
2125 if (!opts)
2126 unkv[(*unkc)++] = arg;
2127 return opts;
2129 if (revs->graph && revs->track_linear)
2130 die("--show-linear-break and --graph are incompatible");
2132 return 1;
2135 void parse_revision_opt(struct rev_info *revs, struct parse_opt_ctx_t *ctx,
2136 const struct option *options,
2137 const char * const usagestr[])
2139 int n = handle_revision_opt(revs, ctx->argc, ctx->argv,
2140 &ctx->cpidx, ctx->out);
2141 if (n <= 0) {
2142 error("unknown option `%s'", ctx->argv[0]);
2143 usage_with_options(usagestr, options);
2145 ctx->argv += n;
2146 ctx->argc -= n;
2149 static int for_each_bisect_ref(struct ref_store *refs, each_ref_fn fn,
2150 void *cb_data, const char *term)
2152 struct strbuf bisect_refs = STRBUF_INIT;
2153 int status;
2154 strbuf_addf(&bisect_refs, "refs/bisect/%s", term);
2155 status = refs_for_each_fullref_in(refs, bisect_refs.buf, fn, cb_data, 0);
2156 strbuf_release(&bisect_refs);
2157 return status;
2160 static int for_each_bad_bisect_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
2162 return for_each_bisect_ref(refs, fn, cb_data, term_bad);
2165 static int for_each_good_bisect_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
2167 return for_each_bisect_ref(refs, fn, cb_data, term_good);
2170 static int handle_revision_pseudo_opt(const char *submodule,
2171 struct rev_info *revs,
2172 int argc, const char **argv, int *flags)
2174 const char *arg = argv[0];
2175 const char *optarg;
2176 struct ref_store *refs;
2177 int argcount;
2179 if (submodule) {
2181 * We need some something like get_submodule_worktrees()
2182 * before we can go through all worktrees of a submodule,
2183 * .e.g with adding all HEADs from --all, which is not
2184 * supported right now, so stick to single worktree.
2186 if (!revs->single_worktree)
2187 BUG("--single-worktree cannot be used together with submodule");
2188 refs = get_submodule_ref_store(submodule);
2189 } else
2190 refs = get_main_ref_store(the_repository);
2193 * NOTE!
2195 * Commands like "git shortlog" will not accept the options below
2196 * unless parse_revision_opt queues them (as opposed to erroring
2197 * out).
2199 * When implementing your new pseudo-option, remember to
2200 * register it in the list at the top of handle_revision_opt.
2202 if (!strcmp(arg, "--all")) {
2203 handle_refs(refs, revs, *flags, refs_for_each_ref);
2204 handle_refs(refs, revs, *flags, refs_head_ref);
2205 if (!revs->single_worktree) {
2206 struct all_refs_cb cb;
2208 init_all_refs_cb(&cb, revs, *flags);
2209 other_head_refs(handle_one_ref, &cb);
2211 clear_ref_exclusion(&revs->ref_excludes);
2212 } else if (!strcmp(arg, "--branches")) {
2213 handle_refs(refs, revs, *flags, refs_for_each_branch_ref);
2214 clear_ref_exclusion(&revs->ref_excludes);
2215 } else if (!strcmp(arg, "--bisect")) {
2216 read_bisect_terms(&term_bad, &term_good);
2217 handle_refs(refs, revs, *flags, for_each_bad_bisect_ref);
2218 handle_refs(refs, revs, *flags ^ (UNINTERESTING | BOTTOM),
2219 for_each_good_bisect_ref);
2220 revs->bisect = 1;
2221 } else if (!strcmp(arg, "--tags")) {
2222 handle_refs(refs, revs, *flags, refs_for_each_tag_ref);
2223 clear_ref_exclusion(&revs->ref_excludes);
2224 } else if (!strcmp(arg, "--remotes")) {
2225 handle_refs(refs, revs, *flags, refs_for_each_remote_ref);
2226 clear_ref_exclusion(&revs->ref_excludes);
2227 } else if ((argcount = parse_long_opt("glob", argv, &optarg))) {
2228 struct all_refs_cb cb;
2229 init_all_refs_cb(&cb, revs, *flags);
2230 for_each_glob_ref(handle_one_ref, optarg, &cb);
2231 clear_ref_exclusion(&revs->ref_excludes);
2232 return argcount;
2233 } else if ((argcount = parse_long_opt("exclude", argv, &optarg))) {
2234 add_ref_exclusion(&revs->ref_excludes, optarg);
2235 return argcount;
2236 } else if (skip_prefix(arg, "--branches=", &optarg)) {
2237 struct all_refs_cb cb;
2238 init_all_refs_cb(&cb, revs, *flags);
2239 for_each_glob_ref_in(handle_one_ref, optarg, "refs/heads/", &cb);
2240 clear_ref_exclusion(&revs->ref_excludes);
2241 } else if (skip_prefix(arg, "--tags=", &optarg)) {
2242 struct all_refs_cb cb;
2243 init_all_refs_cb(&cb, revs, *flags);
2244 for_each_glob_ref_in(handle_one_ref, optarg, "refs/tags/", &cb);
2245 clear_ref_exclusion(&revs->ref_excludes);
2246 } else if (skip_prefix(arg, "--remotes=", &optarg)) {
2247 struct all_refs_cb cb;
2248 init_all_refs_cb(&cb, revs, *flags);
2249 for_each_glob_ref_in(handle_one_ref, optarg, "refs/remotes/", &cb);
2250 clear_ref_exclusion(&revs->ref_excludes);
2251 } else if (!strcmp(arg, "--reflog")) {
2252 add_reflogs_to_pending(revs, *flags);
2253 } else if (!strcmp(arg, "--indexed-objects")) {
2254 add_index_objects_to_pending(revs, *flags);
2255 } else if (!strcmp(arg, "--not")) {
2256 *flags ^= UNINTERESTING | BOTTOM;
2257 } else if (!strcmp(arg, "--no-walk")) {
2258 revs->no_walk = REVISION_WALK_NO_WALK_SORTED;
2259 } else if (skip_prefix(arg, "--no-walk=", &optarg)) {
2261 * Detached form ("--no-walk X" as opposed to "--no-walk=X")
2262 * not allowed, since the argument is optional.
2264 if (!strcmp(optarg, "sorted"))
2265 revs->no_walk = REVISION_WALK_NO_WALK_SORTED;
2266 else if (!strcmp(optarg, "unsorted"))
2267 revs->no_walk = REVISION_WALK_NO_WALK_UNSORTED;
2268 else
2269 return error("invalid argument to --no-walk");
2270 } else if (!strcmp(arg, "--do-walk")) {
2271 revs->no_walk = 0;
2272 } else if (!strcmp(arg, "--single-worktree")) {
2273 revs->single_worktree = 1;
2274 } else {
2275 return 0;
2278 return 1;
2281 static void NORETURN diagnose_missing_default(const char *def)
2283 int flags;
2284 const char *refname;
2286 refname = resolve_ref_unsafe(def, 0, NULL, &flags);
2287 if (!refname || !(flags & REF_ISSYMREF) || (flags & REF_ISBROKEN))
2288 die(_("your current branch appears to be broken"));
2290 skip_prefix(refname, "refs/heads/", &refname);
2291 die(_("your current branch '%s' does not have any commits yet"),
2292 refname);
2296 * Parse revision information, filling in the "rev_info" structure,
2297 * and removing the used arguments from the argument list.
2299 * Returns the number of arguments left that weren't recognized
2300 * (which are also moved to the head of the argument list)
2302 int setup_revisions(int argc, const char **argv, struct rev_info *revs, struct setup_revision_opt *opt)
2304 int i, flags, left, seen_dashdash, read_from_stdin, got_rev_arg = 0, revarg_opt;
2305 struct argv_array prune_data = ARGV_ARRAY_INIT;
2306 const char *submodule = NULL;
2308 if (opt)
2309 submodule = opt->submodule;
2311 /* First, search for "--" */
2312 if (opt && opt->assume_dashdash) {
2313 seen_dashdash = 1;
2314 } else {
2315 seen_dashdash = 0;
2316 for (i = 1; i < argc; i++) {
2317 const char *arg = argv[i];
2318 if (strcmp(arg, "--"))
2319 continue;
2320 argv[i] = NULL;
2321 argc = i;
2322 if (argv[i + 1])
2323 argv_array_pushv(&prune_data, argv + i + 1);
2324 seen_dashdash = 1;
2325 break;
2329 /* Second, deal with arguments and options */
2330 flags = 0;
2331 revarg_opt = opt ? opt->revarg_opt : 0;
2332 if (seen_dashdash)
2333 revarg_opt |= REVARG_CANNOT_BE_FILENAME;
2334 read_from_stdin = 0;
2335 for (left = i = 1; i < argc; i++) {
2336 const char *arg = argv[i];
2337 if (*arg == '-') {
2338 int opts;
2340 opts = handle_revision_pseudo_opt(submodule,
2341 revs, argc - i, argv + i,
2342 &flags);
2343 if (opts > 0) {
2344 i += opts - 1;
2345 continue;
2348 if (!strcmp(arg, "--stdin")) {
2349 if (revs->disable_stdin) {
2350 argv[left++] = arg;
2351 continue;
2353 if (read_from_stdin++)
2354 die("--stdin given twice?");
2355 read_revisions_from_stdin(revs, &prune_data);
2356 continue;
2359 opts = handle_revision_opt(revs, argc - i, argv + i, &left, argv);
2360 if (opts > 0) {
2361 i += opts - 1;
2362 continue;
2364 if (opts < 0)
2365 exit(128);
2366 continue;
2370 if (handle_revision_arg(arg, revs, flags, revarg_opt)) {
2371 int j;
2372 if (seen_dashdash || *arg == '^')
2373 die("bad revision '%s'", arg);
2375 /* If we didn't have a "--":
2376 * (1) all filenames must exist;
2377 * (2) all rev-args must not be interpretable
2378 * as a valid filename.
2379 * but the latter we have checked in the main loop.
2381 for (j = i; j < argc; j++)
2382 verify_filename(revs->prefix, argv[j], j == i);
2384 argv_array_pushv(&prune_data, argv + i);
2385 break;
2387 else
2388 got_rev_arg = 1;
2391 if (prune_data.argc) {
2393 * If we need to introduce the magic "a lone ':' means no
2394 * pathspec whatsoever", here is the place to do so.
2396 * if (prune_data.nr == 1 && !strcmp(prune_data[0], ":")) {
2397 * prune_data.nr = 0;
2398 * prune_data.alloc = 0;
2399 * free(prune_data.path);
2400 * prune_data.path = NULL;
2401 * } else {
2402 * terminate prune_data.alloc with NULL and
2403 * call init_pathspec() to set revs->prune_data here.
2406 parse_pathspec(&revs->prune_data, 0, 0,
2407 revs->prefix, prune_data.argv);
2409 argv_array_clear(&prune_data);
2411 if (revs->def == NULL)
2412 revs->def = opt ? opt->def : NULL;
2413 if (opt && opt->tweak)
2414 opt->tweak(revs, opt);
2415 if (revs->show_merge)
2416 prepare_show_merge(revs);
2417 if (revs->def && !revs->pending.nr && !revs->rev_input_given && !got_rev_arg) {
2418 struct object_id oid;
2419 struct object *object;
2420 struct object_context oc;
2421 if (get_oid_with_context(revs->def, 0, &oid, &oc))
2422 diagnose_missing_default(revs->def);
2423 object = get_reference(revs, revs->def, &oid, 0);
2424 add_pending_object_with_mode(revs, object, revs->def, oc.mode);
2427 /* Did the user ask for any diff output? Run the diff! */
2428 if (revs->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT)
2429 revs->diff = 1;
2431 /* Pickaxe, diff-filter and rename following need diffs */
2432 if ((revs->diffopt.pickaxe_opts & DIFF_PICKAXE_KINDS_MASK) ||
2433 revs->diffopt.filter ||
2434 revs->diffopt.flags.follow_renames)
2435 revs->diff = 1;
2437 if (revs->diffopt.objfind)
2438 revs->simplify_history = 0;
2440 if (revs->topo_order)
2441 revs->limited = 1;
2443 if (revs->prune_data.nr) {
2444 copy_pathspec(&revs->pruning.pathspec, &revs->prune_data);
2445 /* Can't prune commits with rename following: the paths change.. */
2446 if (!revs->diffopt.flags.follow_renames)
2447 revs->prune = 1;
2448 if (!revs->full_diff)
2449 copy_pathspec(&revs->diffopt.pathspec,
2450 &revs->prune_data);
2452 if (revs->combine_merges)
2453 revs->ignore_merges = 0;
2454 revs->diffopt.abbrev = revs->abbrev;
2456 if (revs->line_level_traverse) {
2457 revs->limited = 1;
2458 revs->topo_order = 1;
2461 diff_setup_done(&revs->diffopt);
2463 grep_commit_pattern_type(GREP_PATTERN_TYPE_UNSPECIFIED,
2464 &revs->grep_filter);
2465 compile_grep_patterns(&revs->grep_filter);
2467 if (revs->reverse && revs->reflog_info)
2468 die("cannot combine --reverse with --walk-reflogs");
2469 if (revs->reflog_info && revs->limited)
2470 die("cannot combine --walk-reflogs with history-limiting options");
2471 if (revs->rewrite_parents && revs->children.name)
2472 die("cannot combine --parents and --children");
2475 * Limitations on the graph functionality
2477 if (revs->reverse && revs->graph)
2478 die("cannot combine --reverse with --graph");
2480 if (revs->reflog_info && revs->graph)
2481 die("cannot combine --walk-reflogs with --graph");
2482 if (revs->no_walk && revs->graph)
2483 die("cannot combine --no-walk with --graph");
2484 if (!revs->reflog_info && revs->grep_filter.use_reflog_filter)
2485 die("cannot use --grep-reflog without --walk-reflogs");
2487 if (revs->first_parent_only && revs->bisect)
2488 die(_("--first-parent is incompatible with --bisect"));
2490 if (revs->expand_tabs_in_log < 0)
2491 revs->expand_tabs_in_log = revs->expand_tabs_in_log_default;
2493 return left;
2496 static void add_child(struct rev_info *revs, struct commit *parent, struct commit *child)
2498 struct commit_list *l = xcalloc(1, sizeof(*l));
2500 l->item = child;
2501 l->next = add_decoration(&revs->children, &parent->object, l);
2504 static int remove_duplicate_parents(struct rev_info *revs, struct commit *commit)
2506 struct treesame_state *ts = lookup_decoration(&revs->treesame, &commit->object);
2507 struct commit_list **pp, *p;
2508 int surviving_parents;
2510 /* Examine existing parents while marking ones we have seen... */
2511 pp = &commit->parents;
2512 surviving_parents = 0;
2513 while ((p = *pp) != NULL) {
2514 struct commit *parent = p->item;
2515 if (parent->object.flags & TMP_MARK) {
2516 *pp = p->next;
2517 if (ts)
2518 compact_treesame(revs, commit, surviving_parents);
2519 continue;
2521 parent->object.flags |= TMP_MARK;
2522 surviving_parents++;
2523 pp = &p->next;
2525 /* clear the temporary mark */
2526 for (p = commit->parents; p; p = p->next) {
2527 p->item->object.flags &= ~TMP_MARK;
2529 /* no update_treesame() - removing duplicates can't affect TREESAME */
2530 return surviving_parents;
2533 struct merge_simplify_state {
2534 struct commit *simplified;
2537 static struct merge_simplify_state *locate_simplify_state(struct rev_info *revs, struct commit *commit)
2539 struct merge_simplify_state *st;
2541 st = lookup_decoration(&revs->merge_simplification, &commit->object);
2542 if (!st) {
2543 st = xcalloc(1, sizeof(*st));
2544 add_decoration(&revs->merge_simplification, &commit->object, st);
2546 return st;
2549 static int mark_redundant_parents(struct rev_info *revs, struct commit *commit)
2551 struct commit_list *h = reduce_heads(commit->parents);
2552 int i = 0, marked = 0;
2553 struct commit_list *po, *pn;
2555 /* Want these for sanity-checking only */
2556 int orig_cnt = commit_list_count(commit->parents);
2557 int cnt = commit_list_count(h);
2560 * Not ready to remove items yet, just mark them for now, based
2561 * on the output of reduce_heads(). reduce_heads outputs the reduced
2562 * set in its original order, so this isn't too hard.
2564 po = commit->parents;
2565 pn = h;
2566 while (po) {
2567 if (pn && po->item == pn->item) {
2568 pn = pn->next;
2569 i++;
2570 } else {
2571 po->item->object.flags |= TMP_MARK;
2572 marked++;
2574 po=po->next;
2577 if (i != cnt || cnt+marked != orig_cnt)
2578 die("mark_redundant_parents %d %d %d %d", orig_cnt, cnt, i, marked);
2580 free_commit_list(h);
2582 return marked;
2585 static int mark_treesame_root_parents(struct rev_info *revs, struct commit *commit)
2587 struct commit_list *p;
2588 int marked = 0;
2590 for (p = commit->parents; p; p = p->next) {
2591 struct commit *parent = p->item;
2592 if (!parent->parents && (parent->object.flags & TREESAME)) {
2593 parent->object.flags |= TMP_MARK;
2594 marked++;
2598 return marked;
2602 * Awkward naming - this means one parent we are TREESAME to.
2603 * cf mark_treesame_root_parents: root parents that are TREESAME (to an
2604 * empty tree). Better name suggestions?
2606 static int leave_one_treesame_to_parent(struct rev_info *revs, struct commit *commit)
2608 struct treesame_state *ts = lookup_decoration(&revs->treesame, &commit->object);
2609 struct commit *unmarked = NULL, *marked = NULL;
2610 struct commit_list *p;
2611 unsigned n;
2613 for (p = commit->parents, n = 0; p; p = p->next, n++) {
2614 if (ts->treesame[n]) {
2615 if (p->item->object.flags & TMP_MARK) {
2616 if (!marked)
2617 marked = p->item;
2618 } else {
2619 if (!unmarked) {
2620 unmarked = p->item;
2621 break;
2628 * If we are TREESAME to a marked-for-deletion parent, but not to any
2629 * unmarked parents, unmark the first TREESAME parent. This is the
2630 * parent that the default simplify_history==1 scan would have followed,
2631 * and it doesn't make sense to omit that path when asking for a
2632 * simplified full history. Retaining it improves the chances of
2633 * understanding odd missed merges that took an old version of a file.
2635 * Example:
2637 * I--------*X A modified the file, but mainline merge X used
2638 * \ / "-s ours", so took the version from I. X is
2639 * `-*A--' TREESAME to I and !TREESAME to A.
2641 * Default log from X would produce "I". Without this check,
2642 * --full-history --simplify-merges would produce "I-A-X", showing
2643 * the merge commit X and that it changed A, but not making clear that
2644 * it had just taken the I version. With this check, the topology above
2645 * is retained.
2647 * Note that it is possible that the simplification chooses a different
2648 * TREESAME parent from the default, in which case this test doesn't
2649 * activate, and we _do_ drop the default parent. Example:
2651 * I------X A modified the file, but it was reverted in B,
2652 * \ / meaning mainline merge X is TREESAME to both
2653 * *A-*B parents.
2655 * Default log would produce "I" by following the first parent;
2656 * --full-history --simplify-merges will produce "I-A-B". But this is a
2657 * reasonable result - it presents a logical full history leading from
2658 * I to X, and X is not an important merge.
2660 if (!unmarked && marked) {
2661 marked->object.flags &= ~TMP_MARK;
2662 return 1;
2665 return 0;
2668 static int remove_marked_parents(struct rev_info *revs, struct commit *commit)
2670 struct commit_list **pp, *p;
2671 int nth_parent, removed = 0;
2673 pp = &commit->parents;
2674 nth_parent = 0;
2675 while ((p = *pp) != NULL) {
2676 struct commit *parent = p->item;
2677 if (parent->object.flags & TMP_MARK) {
2678 parent->object.flags &= ~TMP_MARK;
2679 *pp = p->next;
2680 free(p);
2681 removed++;
2682 compact_treesame(revs, commit, nth_parent);
2683 continue;
2685 pp = &p->next;
2686 nth_parent++;
2689 /* Removing parents can only increase TREESAMEness */
2690 if (removed && !(commit->object.flags & TREESAME))
2691 update_treesame(revs, commit);
2693 return nth_parent;
2696 static struct commit_list **simplify_one(struct rev_info *revs, struct commit *commit, struct commit_list **tail)
2698 struct commit_list *p;
2699 struct commit *parent;
2700 struct merge_simplify_state *st, *pst;
2701 int cnt;
2703 st = locate_simplify_state(revs, commit);
2706 * Have we handled this one?
2708 if (st->simplified)
2709 return tail;
2712 * An UNINTERESTING commit simplifies to itself, so does a
2713 * root commit. We do not rewrite parents of such commit
2714 * anyway.
2716 if ((commit->object.flags & UNINTERESTING) || !commit->parents) {
2717 st->simplified = commit;
2718 return tail;
2722 * Do we know what commit all of our parents that matter
2723 * should be rewritten to? Otherwise we are not ready to
2724 * rewrite this one yet.
2726 for (cnt = 0, p = commit->parents; p; p = p->next) {
2727 pst = locate_simplify_state(revs, p->item);
2728 if (!pst->simplified) {
2729 tail = &commit_list_insert(p->item, tail)->next;
2730 cnt++;
2732 if (revs->first_parent_only)
2733 break;
2735 if (cnt) {
2736 tail = &commit_list_insert(commit, tail)->next;
2737 return tail;
2741 * Rewrite our list of parents. Note that this cannot
2742 * affect our TREESAME flags in any way - a commit is
2743 * always TREESAME to its simplification.
2745 for (p = commit->parents; p; p = p->next) {
2746 pst = locate_simplify_state(revs, p->item);
2747 p->item = pst->simplified;
2748 if (revs->first_parent_only)
2749 break;
2752 if (revs->first_parent_only)
2753 cnt = 1;
2754 else
2755 cnt = remove_duplicate_parents(revs, commit);
2758 * It is possible that we are a merge and one side branch
2759 * does not have any commit that touches the given paths;
2760 * in such a case, the immediate parent from that branch
2761 * will be rewritten to be the merge base.
2763 * o----X X: the commit we are looking at;
2764 * / / o: a commit that touches the paths;
2765 * ---o----'
2767 * Further, a merge of an independent branch that doesn't
2768 * touch the path will reduce to a treesame root parent:
2770 * ----o----X X: the commit we are looking at;
2771 * / o: a commit that touches the paths;
2772 * r r: a root commit not touching the paths
2774 * Detect and simplify both cases.
2776 if (1 < cnt) {
2777 int marked = mark_redundant_parents(revs, commit);
2778 marked += mark_treesame_root_parents(revs, commit);
2779 if (marked)
2780 marked -= leave_one_treesame_to_parent(revs, commit);
2781 if (marked)
2782 cnt = remove_marked_parents(revs, commit);
2786 * A commit simplifies to itself if it is a root, if it is
2787 * UNINTERESTING, if it touches the given paths, or if it is a
2788 * merge and its parents don't simplify to one relevant commit
2789 * (the first two cases are already handled at the beginning of
2790 * this function).
2792 * Otherwise, it simplifies to what its sole relevant parent
2793 * simplifies to.
2795 if (!cnt ||
2796 (commit->object.flags & UNINTERESTING) ||
2797 !(commit->object.flags & TREESAME) ||
2798 (parent = one_relevant_parent(revs, commit->parents)) == NULL)
2799 st->simplified = commit;
2800 else {
2801 pst = locate_simplify_state(revs, parent);
2802 st->simplified = pst->simplified;
2804 return tail;
2807 static void simplify_merges(struct rev_info *revs)
2809 struct commit_list *list, *next;
2810 struct commit_list *yet_to_do, **tail;
2811 struct commit *commit;
2813 if (!revs->prune)
2814 return;
2816 /* feed the list reversed */
2817 yet_to_do = NULL;
2818 for (list = revs->commits; list; list = next) {
2819 commit = list->item;
2820 next = list->next;
2822 * Do not free(list) here yet; the original list
2823 * is used later in this function.
2825 commit_list_insert(commit, &yet_to_do);
2827 while (yet_to_do) {
2828 list = yet_to_do;
2829 yet_to_do = NULL;
2830 tail = &yet_to_do;
2831 while (list) {
2832 commit = pop_commit(&list);
2833 tail = simplify_one(revs, commit, tail);
2837 /* clean up the result, removing the simplified ones */
2838 list = revs->commits;
2839 revs->commits = NULL;
2840 tail = &revs->commits;
2841 while (list) {
2842 struct merge_simplify_state *st;
2844 commit = pop_commit(&list);
2845 st = locate_simplify_state(revs, commit);
2846 if (st->simplified == commit)
2847 tail = &commit_list_insert(commit, tail)->next;
2851 static void set_children(struct rev_info *revs)
2853 struct commit_list *l;
2854 for (l = revs->commits; l; l = l->next) {
2855 struct commit *commit = l->item;
2856 struct commit_list *p;
2858 for (p = commit->parents; p; p = p->next)
2859 add_child(revs, p->item, commit);
2863 void reset_revision_walk(void)
2865 clear_object_flags(SEEN | ADDED | SHOWN);
2868 static int mark_uninteresting(const struct object_id *oid,
2869 struct packed_git *pack,
2870 uint32_t pos,
2871 void *unused)
2873 struct object *o = parse_object(oid);
2874 o->flags |= UNINTERESTING | SEEN;
2875 return 0;
2878 int prepare_revision_walk(struct rev_info *revs)
2880 int i;
2881 struct object_array old_pending;
2882 struct commit_list **next = &revs->commits;
2884 memcpy(&old_pending, &revs->pending, sizeof(old_pending));
2885 revs->pending.nr = 0;
2886 revs->pending.alloc = 0;
2887 revs->pending.objects = NULL;
2888 for (i = 0; i < old_pending.nr; i++) {
2889 struct object_array_entry *e = old_pending.objects + i;
2890 struct commit *commit = handle_commit(revs, e);
2891 if (commit) {
2892 if (!(commit->object.flags & SEEN)) {
2893 commit->object.flags |= SEEN;
2894 next = commit_list_append(commit, next);
2898 object_array_clear(&old_pending);
2900 /* Signal whether we need per-parent treesame decoration */
2901 if (revs->simplify_merges ||
2902 (revs->limited && limiting_can_increase_treesame(revs)))
2903 revs->treesame.name = "treesame";
2905 if (revs->exclude_promisor_objects) {
2906 for_each_packed_object(mark_uninteresting, NULL,
2907 FOR_EACH_OBJECT_PROMISOR_ONLY);
2910 if (revs->no_walk != REVISION_WALK_NO_WALK_UNSORTED)
2911 commit_list_sort_by_date(&revs->commits);
2912 if (revs->no_walk)
2913 return 0;
2914 if (revs->limited)
2915 if (limit_list(revs) < 0)
2916 return -1;
2917 if (revs->topo_order)
2918 sort_in_topological_order(&revs->commits, revs->sort_order);
2919 if (revs->line_level_traverse)
2920 line_log_filter(revs);
2921 if (revs->simplify_merges)
2922 simplify_merges(revs);
2923 if (revs->children.name)
2924 set_children(revs);
2925 return 0;
2928 static enum rewrite_result rewrite_one(struct rev_info *revs, struct commit **pp)
2930 struct commit_list *cache = NULL;
2932 for (;;) {
2933 struct commit *p = *pp;
2934 if (!revs->limited)
2935 if (add_parents_to_list(revs, p, &revs->commits, &cache) < 0)
2936 return rewrite_one_error;
2937 if (p->object.flags & UNINTERESTING)
2938 return rewrite_one_ok;
2939 if (!(p->object.flags & TREESAME))
2940 return rewrite_one_ok;
2941 if (!p->parents)
2942 return rewrite_one_noparents;
2943 if ((p = one_relevant_parent(revs, p->parents)) == NULL)
2944 return rewrite_one_ok;
2945 *pp = p;
2949 int rewrite_parents(struct rev_info *revs, struct commit *commit,
2950 rewrite_parent_fn_t rewrite_parent)
2952 struct commit_list **pp = &commit->parents;
2953 while (*pp) {
2954 struct commit_list *parent = *pp;
2955 switch (rewrite_parent(revs, &parent->item)) {
2956 case rewrite_one_ok:
2957 break;
2958 case rewrite_one_noparents:
2959 *pp = parent->next;
2960 continue;
2961 case rewrite_one_error:
2962 return -1;
2964 pp = &parent->next;
2966 remove_duplicate_parents(revs, commit);
2967 return 0;
2970 static int commit_rewrite_person(struct strbuf *buf, const char *what, struct string_list *mailmap)
2972 char *person, *endp;
2973 size_t len, namelen, maillen;
2974 const char *name;
2975 const char *mail;
2976 struct ident_split ident;
2978 person = strstr(buf->buf, what);
2979 if (!person)
2980 return 0;
2982 person += strlen(what);
2983 endp = strchr(person, '\n');
2984 if (!endp)
2985 return 0;
2987 len = endp - person;
2989 if (split_ident_line(&ident, person, len))
2990 return 0;
2992 mail = ident.mail_begin;
2993 maillen = ident.mail_end - ident.mail_begin;
2994 name = ident.name_begin;
2995 namelen = ident.name_end - ident.name_begin;
2997 if (map_user(mailmap, &mail, &maillen, &name, &namelen)) {
2998 struct strbuf namemail = STRBUF_INIT;
3000 strbuf_addf(&namemail, "%.*s <%.*s>",
3001 (int)namelen, name, (int)maillen, mail);
3003 strbuf_splice(buf, ident.name_begin - buf->buf,
3004 ident.mail_end - ident.name_begin + 1,
3005 namemail.buf, namemail.len);
3007 strbuf_release(&namemail);
3009 return 1;
3012 return 0;
3015 static int commit_match(struct commit *commit, struct rev_info *opt)
3017 int retval;
3018 const char *encoding;
3019 const char *message;
3020 struct strbuf buf = STRBUF_INIT;
3022 if (!opt->grep_filter.pattern_list && !opt->grep_filter.header_list)
3023 return 1;
3025 /* Prepend "fake" headers as needed */
3026 if (opt->grep_filter.use_reflog_filter) {
3027 strbuf_addstr(&buf, "reflog ");
3028 get_reflog_message(&buf, opt->reflog_info);
3029 strbuf_addch(&buf, '\n');
3033 * We grep in the user's output encoding, under the assumption that it
3034 * is the encoding they are most likely to write their grep pattern
3035 * for. In addition, it means we will match the "notes" encoding below,
3036 * so we will not end up with a buffer that has two different encodings
3037 * in it.
3039 encoding = get_log_output_encoding();
3040 message = logmsg_reencode(commit, NULL, encoding);
3042 /* Copy the commit to temporary if we are using "fake" headers */
3043 if (buf.len)
3044 strbuf_addstr(&buf, message);
3046 if (opt->grep_filter.header_list && opt->mailmap) {
3047 if (!buf.len)
3048 strbuf_addstr(&buf, message);
3050 commit_rewrite_person(&buf, "\nauthor ", opt->mailmap);
3051 commit_rewrite_person(&buf, "\ncommitter ", opt->mailmap);
3054 /* Append "fake" message parts as needed */
3055 if (opt->show_notes) {
3056 if (!buf.len)
3057 strbuf_addstr(&buf, message);
3058 format_display_notes(&commit->object.oid, &buf, encoding, 1);
3062 * Find either in the original commit message, or in the temporary.
3063 * Note that we cast away the constness of "message" here. It is
3064 * const because it may come from the cached commit buffer. That's OK,
3065 * because we know that it is modifiable heap memory, and that while
3066 * grep_buffer may modify it for speed, it will restore any
3067 * changes before returning.
3069 if (buf.len)
3070 retval = grep_buffer(&opt->grep_filter, buf.buf, buf.len);
3071 else
3072 retval = grep_buffer(&opt->grep_filter,
3073 (char *)message, strlen(message));
3074 strbuf_release(&buf);
3075 unuse_commit_buffer(commit, message);
3076 return opt->invert_grep ? !retval : retval;
3079 static inline int want_ancestry(const struct rev_info *revs)
3081 return (revs->rewrite_parents || revs->children.name);
3085 * Return a timestamp to be used for --since/--until comparisons for this
3086 * commit, based on the revision options.
3088 static timestamp_t comparison_date(const struct rev_info *revs,
3089 struct commit *commit)
3091 return revs->reflog_info ?
3092 get_reflog_timestamp(revs->reflog_info) :
3093 commit->date;
3096 enum commit_action get_commit_action(struct rev_info *revs, struct commit *commit)
3098 if (commit->object.flags & SHOWN)
3099 return commit_ignore;
3100 if (revs->unpacked && has_sha1_pack(commit->object.oid.hash))
3101 return commit_ignore;
3102 if (commit->object.flags & UNINTERESTING)
3103 return commit_ignore;
3104 if (revs->min_age != -1 &&
3105 comparison_date(revs, commit) > revs->min_age)
3106 return commit_ignore;
3107 if (revs->min_parents || (revs->max_parents >= 0)) {
3108 int n = commit_list_count(commit->parents);
3109 if ((n < revs->min_parents) ||
3110 ((revs->max_parents >= 0) && (n > revs->max_parents)))
3111 return commit_ignore;
3113 if (!commit_match(commit, revs))
3114 return commit_ignore;
3115 if (revs->prune && revs->dense) {
3116 /* Commit without changes? */
3117 if (commit->object.flags & TREESAME) {
3118 int n;
3119 struct commit_list *p;
3120 /* drop merges unless we want parenthood */
3121 if (!want_ancestry(revs))
3122 return commit_ignore;
3124 * If we want ancestry, then need to keep any merges
3125 * between relevant commits to tie together topology.
3126 * For consistency with TREESAME and simplification
3127 * use "relevant" here rather than just INTERESTING,
3128 * to treat bottom commit(s) as part of the topology.
3130 for (n = 0, p = commit->parents; p; p = p->next)
3131 if (relevant_commit(p->item))
3132 if (++n >= 2)
3133 return commit_show;
3134 return commit_ignore;
3137 return commit_show;
3140 define_commit_slab(saved_parents, struct commit_list *);
3142 #define EMPTY_PARENT_LIST ((struct commit_list *)-1)
3145 * You may only call save_parents() once per commit (this is checked
3146 * for non-root commits).
3148 static void save_parents(struct rev_info *revs, struct commit *commit)
3150 struct commit_list **pp;
3152 if (!revs->saved_parents_slab) {
3153 revs->saved_parents_slab = xmalloc(sizeof(struct saved_parents));
3154 init_saved_parents(revs->saved_parents_slab);
3157 pp = saved_parents_at(revs->saved_parents_slab, commit);
3160 * When walking with reflogs, we may visit the same commit
3161 * several times: once for each appearance in the reflog.
3163 * In this case, save_parents() will be called multiple times.
3164 * We want to keep only the first set of parents. We need to
3165 * store a sentinel value for an empty (i.e., NULL) parent
3166 * list to distinguish it from a not-yet-saved list, however.
3168 if (*pp)
3169 return;
3170 if (commit->parents)
3171 *pp = copy_commit_list(commit->parents);
3172 else
3173 *pp = EMPTY_PARENT_LIST;
3176 static void free_saved_parents(struct rev_info *revs)
3178 if (revs->saved_parents_slab)
3179 clear_saved_parents(revs->saved_parents_slab);
3182 struct commit_list *get_saved_parents(struct rev_info *revs, const struct commit *commit)
3184 struct commit_list *parents;
3186 if (!revs->saved_parents_slab)
3187 return commit->parents;
3189 parents = *saved_parents_at(revs->saved_parents_slab, commit);
3190 if (parents == EMPTY_PARENT_LIST)
3191 return NULL;
3192 return parents;
3195 enum commit_action simplify_commit(struct rev_info *revs, struct commit *commit)
3197 enum commit_action action = get_commit_action(revs, commit);
3199 if (action == commit_show &&
3200 revs->prune && revs->dense && want_ancestry(revs)) {
3202 * --full-diff on simplified parents is no good: it
3203 * will show spurious changes from the commits that
3204 * were elided. So we save the parents on the side
3205 * when --full-diff is in effect.
3207 if (revs->full_diff)
3208 save_parents(revs, commit);
3209 if (rewrite_parents(revs, commit, rewrite_one) < 0)
3210 return commit_error;
3212 return action;
3215 static void track_linear(struct rev_info *revs, struct commit *commit)
3217 if (revs->track_first_time) {
3218 revs->linear = 1;
3219 revs->track_first_time = 0;
3220 } else {
3221 struct commit_list *p;
3222 for (p = revs->previous_parents; p; p = p->next)
3223 if (p->item == NULL || /* first commit */
3224 !oidcmp(&p->item->object.oid, &commit->object.oid))
3225 break;
3226 revs->linear = p != NULL;
3228 if (revs->reverse) {
3229 if (revs->linear)
3230 commit->object.flags |= TRACK_LINEAR;
3232 free_commit_list(revs->previous_parents);
3233 revs->previous_parents = copy_commit_list(commit->parents);
3236 static struct commit *get_revision_1(struct rev_info *revs)
3238 while (1) {
3239 struct commit *commit;
3241 if (revs->reflog_info)
3242 commit = next_reflog_entry(revs->reflog_info);
3243 else
3244 commit = pop_commit(&revs->commits);
3246 if (!commit)
3247 return NULL;
3249 if (revs->reflog_info)
3250 commit->object.flags &= ~(ADDED | SEEN | SHOWN);
3253 * If we haven't done the list limiting, we need to look at
3254 * the parents here. We also need to do the date-based limiting
3255 * that we'd otherwise have done in limit_list().
3257 if (!revs->limited) {
3258 if (revs->max_age != -1 &&
3259 comparison_date(revs, commit) < revs->max_age)
3260 continue;
3262 if (revs->reflog_info)
3263 try_to_simplify_commit(revs, commit);
3264 else if (add_parents_to_list(revs, commit, &revs->commits, NULL) < 0) {
3265 if (!revs->ignore_missing_links)
3266 die("Failed to traverse parents of commit %s",
3267 oid_to_hex(&commit->object.oid));
3271 switch (simplify_commit(revs, commit)) {
3272 case commit_ignore:
3273 continue;
3274 case commit_error:
3275 die("Failed to simplify parents of commit %s",
3276 oid_to_hex(&commit->object.oid));
3277 default:
3278 if (revs->track_linear)
3279 track_linear(revs, commit);
3280 return commit;
3286 * Return true for entries that have not yet been shown. (This is an
3287 * object_array_each_func_t.)
3289 static int entry_unshown(struct object_array_entry *entry, void *cb_data_unused)
3291 return !(entry->item->flags & SHOWN);
3295 * If array is on the verge of a realloc, garbage-collect any entries
3296 * that have already been shown to try to free up some space.
3298 static void gc_boundary(struct object_array *array)
3300 if (array->nr == array->alloc)
3301 object_array_filter(array, entry_unshown, NULL);
3304 static void create_boundary_commit_list(struct rev_info *revs)
3306 unsigned i;
3307 struct commit *c;
3308 struct object_array *array = &revs->boundary_commits;
3309 struct object_array_entry *objects = array->objects;
3312 * If revs->commits is non-NULL at this point, an error occurred in
3313 * get_revision_1(). Ignore the error and continue printing the
3314 * boundary commits anyway. (This is what the code has always
3315 * done.)
3317 if (revs->commits) {
3318 free_commit_list(revs->commits);
3319 revs->commits = NULL;
3323 * Put all of the actual boundary commits from revs->boundary_commits
3324 * into revs->commits
3326 for (i = 0; i < array->nr; i++) {
3327 c = (struct commit *)(objects[i].item);
3328 if (!c)
3329 continue;
3330 if (!(c->object.flags & CHILD_SHOWN))
3331 continue;
3332 if (c->object.flags & (SHOWN | BOUNDARY))
3333 continue;
3334 c->object.flags |= BOUNDARY;
3335 commit_list_insert(c, &revs->commits);
3339 * If revs->topo_order is set, sort the boundary commits
3340 * in topological order
3342 sort_in_topological_order(&revs->commits, revs->sort_order);
3345 static struct commit *get_revision_internal(struct rev_info *revs)
3347 struct commit *c = NULL;
3348 struct commit_list *l;
3350 if (revs->boundary == 2) {
3352 * All of the normal commits have already been returned,
3353 * and we are now returning boundary commits.
3354 * create_boundary_commit_list() has populated
3355 * revs->commits with the remaining commits to return.
3357 c = pop_commit(&revs->commits);
3358 if (c)
3359 c->object.flags |= SHOWN;
3360 return c;
3364 * If our max_count counter has reached zero, then we are done. We
3365 * don't simply return NULL because we still might need to show
3366 * boundary commits. But we want to avoid calling get_revision_1, which
3367 * might do a considerable amount of work finding the next commit only
3368 * for us to throw it away.
3370 * If it is non-zero, then either we don't have a max_count at all
3371 * (-1), or it is still counting, in which case we decrement.
3373 if (revs->max_count) {
3374 c = get_revision_1(revs);
3375 if (c) {
3376 while (revs->skip_count > 0) {
3377 revs->skip_count--;
3378 c = get_revision_1(revs);
3379 if (!c)
3380 break;
3384 if (revs->max_count > 0)
3385 revs->max_count--;
3388 if (c)
3389 c->object.flags |= SHOWN;
3391 if (!revs->boundary)
3392 return c;
3394 if (!c) {
3396 * get_revision_1() runs out the commits, and
3397 * we are done computing the boundaries.
3398 * switch to boundary commits output mode.
3400 revs->boundary = 2;
3403 * Update revs->commits to contain the list of
3404 * boundary commits.
3406 create_boundary_commit_list(revs);
3408 return get_revision_internal(revs);
3412 * boundary commits are the commits that are parents of the
3413 * ones we got from get_revision_1() but they themselves are
3414 * not returned from get_revision_1(). Before returning
3415 * 'c', we need to mark its parents that they could be boundaries.
3418 for (l = c->parents; l; l = l->next) {
3419 struct object *p;
3420 p = &(l->item->object);
3421 if (p->flags & (CHILD_SHOWN | SHOWN))
3422 continue;
3423 p->flags |= CHILD_SHOWN;
3424 gc_boundary(&revs->boundary_commits);
3425 add_object_array(p, NULL, &revs->boundary_commits);
3428 return c;
3431 struct commit *get_revision(struct rev_info *revs)
3433 struct commit *c;
3434 struct commit_list *reversed;
3436 if (revs->reverse) {
3437 reversed = NULL;
3438 while ((c = get_revision_internal(revs)))
3439 commit_list_insert(c, &reversed);
3440 revs->commits = reversed;
3441 revs->reverse = 0;
3442 revs->reverse_output_stage = 1;
3445 if (revs->reverse_output_stage) {
3446 c = pop_commit(&revs->commits);
3447 if (revs->track_linear)
3448 revs->linear = !!(c && c->object.flags & TRACK_LINEAR);
3449 return c;
3452 c = get_revision_internal(revs);
3453 if (c && revs->graph)
3454 graph_update(revs->graph, c);
3455 if (!c) {
3456 free_saved_parents(revs);
3457 if (revs->previous_parents) {
3458 free_commit_list(revs->previous_parents);
3459 revs->previous_parents = NULL;
3462 return c;
3465 char *get_revision_mark(const struct rev_info *revs, const struct commit *commit)
3467 if (commit->object.flags & BOUNDARY)
3468 return "-";
3469 else if (commit->object.flags & UNINTERESTING)
3470 return "^";
3471 else if (commit->object.flags & PATCHSAME)
3472 return "=";
3473 else if (!revs || revs->left_right) {
3474 if (commit->object.flags & SYMMETRIC_LEFT)
3475 return "<";
3476 else
3477 return ">";
3478 } else if (revs->graph)
3479 return "*";
3480 else if (revs->cherry_mark)
3481 return "+";
3482 return "";
3485 void put_revision_mark(const struct rev_info *revs, const struct commit *commit)
3487 char *mark = get_revision_mark(revs, commit);
3488 if (!strlen(mark))
3489 return;
3490 fputs(mark, stdout);
3491 putchar(' ');