Merge branch 'jk/run-command-notdot'
[git.git] / revision.c
bloba1ddb9e11cbe3a52bb8d3eee3785524db4055854
1 #include "cache.h"
2 #include "object-store.h"
3 #include "tag.h"
4 #include "blob.h"
5 #include "tree.h"
6 #include "commit.h"
7 #include "diff.h"
8 #include "refs.h"
9 #include "revision.h"
10 #include "repository.h"
11 #include "graph.h"
12 #include "grep.h"
13 #include "reflog-walk.h"
14 #include "patch-ids.h"
15 #include "decorate.h"
16 #include "log-tree.h"
17 #include "string-list.h"
18 #include "line-log.h"
19 #include "mailmap.h"
20 #include "commit-slab.h"
21 #include "dir.h"
22 #include "cache-tree.h"
23 #include "bisect.h"
24 #include "packfile.h"
25 #include "worktree.h"
26 #include "argv-array.h"
27 #include "commit-reach.h"
29 volatile show_early_output_fn_t show_early_output;
31 static const char *term_bad;
32 static const char *term_good;
34 implement_shared_commit_slab(revision_sources, char *);
36 void show_object_with_name(FILE *out, struct object *obj, const char *name)
38 const char *p;
40 fprintf(out, "%s ", oid_to_hex(&obj->oid));
41 for (p = name; *p && *p != '\n'; p++)
42 fputc(*p, out);
43 fputc('\n', out);
46 static void mark_blob_uninteresting(struct blob *blob)
48 if (!blob)
49 return;
50 if (blob->object.flags & UNINTERESTING)
51 return;
52 blob->object.flags |= UNINTERESTING;
55 static void mark_tree_contents_uninteresting(struct repository *r,
56 struct tree *tree)
58 struct tree_desc desc;
59 struct name_entry entry;
61 if (parse_tree_gently(tree, 1) < 0)
62 return;
64 init_tree_desc(&desc, tree->buffer, tree->size);
65 while (tree_entry(&desc, &entry)) {
66 switch (object_type(entry.mode)) {
67 case OBJ_TREE:
68 mark_tree_uninteresting(r, lookup_tree(r, entry.oid));
69 break;
70 case OBJ_BLOB:
71 mark_blob_uninteresting(lookup_blob(r, entry.oid));
72 break;
73 default:
74 /* Subproject commit - not in this repository */
75 break;
80 * We don't care about the tree any more
81 * after it has been marked uninteresting.
83 free_tree_buffer(tree);
86 void mark_tree_uninteresting(struct repository *r, struct tree *tree)
88 struct object *obj;
90 if (!tree)
91 return;
93 obj = &tree->object;
94 if (obj->flags & UNINTERESTING)
95 return;
96 obj->flags |= UNINTERESTING;
97 mark_tree_contents_uninteresting(r, tree);
100 struct commit_stack {
101 struct commit **items;
102 size_t nr, alloc;
104 #define COMMIT_STACK_INIT { NULL, 0, 0 }
106 static void commit_stack_push(struct commit_stack *stack, struct commit *commit)
108 ALLOC_GROW(stack->items, stack->nr + 1, stack->alloc);
109 stack->items[stack->nr++] = commit;
112 static struct commit *commit_stack_pop(struct commit_stack *stack)
114 return stack->nr ? stack->items[--stack->nr] : NULL;
117 static void commit_stack_clear(struct commit_stack *stack)
119 FREE_AND_NULL(stack->items);
120 stack->nr = stack->alloc = 0;
123 static void mark_one_parent_uninteresting(struct commit *commit,
124 struct commit_stack *pending)
126 struct commit_list *l;
128 if (commit->object.flags & UNINTERESTING)
129 return;
130 commit->object.flags |= UNINTERESTING;
133 * Normally we haven't parsed the parent
134 * yet, so we won't have a parent of a parent
135 * here. However, it may turn out that we've
136 * reached this commit some other way (where it
137 * wasn't uninteresting), in which case we need
138 * to mark its parents recursively too..
140 for (l = commit->parents; l; l = l->next)
141 commit_stack_push(pending, l->item);
144 void mark_parents_uninteresting(struct commit *commit)
146 struct commit_stack pending = COMMIT_STACK_INIT;
147 struct commit_list *l;
149 for (l = commit->parents; l; l = l->next)
150 mark_one_parent_uninteresting(l->item, &pending);
152 while (pending.nr > 0)
153 mark_one_parent_uninteresting(commit_stack_pop(&pending),
154 &pending);
156 commit_stack_clear(&pending);
159 static void add_pending_object_with_path(struct rev_info *revs,
160 struct object *obj,
161 const char *name, unsigned mode,
162 const char *path)
164 if (!obj)
165 return;
166 if (revs->no_walk && (obj->flags & UNINTERESTING))
167 revs->no_walk = 0;
168 if (revs->reflog_info && obj->type == OBJ_COMMIT) {
169 struct strbuf buf = STRBUF_INIT;
170 int len = interpret_branch_name(name, 0, &buf, 0);
172 if (0 < len && name[len] && buf.len)
173 strbuf_addstr(&buf, name + len);
174 add_reflog_for_walk(revs->reflog_info,
175 (struct commit *)obj,
176 buf.buf[0] ? buf.buf: name);
177 strbuf_release(&buf);
178 return; /* do not add the commit itself */
180 add_object_array_with_path(obj, name, &revs->pending, mode, path);
183 static void add_pending_object_with_mode(struct rev_info *revs,
184 struct object *obj,
185 const char *name, unsigned mode)
187 add_pending_object_with_path(revs, obj, name, mode, NULL);
190 void add_pending_object(struct rev_info *revs,
191 struct object *obj, const char *name)
193 add_pending_object_with_mode(revs, obj, name, S_IFINVALID);
196 void add_head_to_pending(struct rev_info *revs)
198 struct object_id oid;
199 struct object *obj;
200 if (get_oid("HEAD", &oid))
201 return;
202 obj = parse_object(revs->repo, &oid);
203 if (!obj)
204 return;
205 add_pending_object(revs, obj, "HEAD");
208 static struct object *get_reference(struct rev_info *revs, const char *name,
209 const struct object_id *oid,
210 unsigned int flags)
212 struct object *object;
214 object = parse_object(revs->repo, oid);
215 if (!object) {
216 if (revs->ignore_missing)
217 return object;
218 if (revs->exclude_promisor_objects && is_promisor_object(oid))
219 return NULL;
220 die("bad object %s", name);
222 object->flags |= flags;
223 return object;
226 void add_pending_oid(struct rev_info *revs, const char *name,
227 const struct object_id *oid, unsigned int flags)
229 struct object *object = get_reference(revs, name, oid, flags);
230 add_pending_object(revs, object, name);
233 static struct commit *handle_commit(struct rev_info *revs,
234 struct object_array_entry *entry)
236 struct object *object = entry->item;
237 const char *name = entry->name;
238 const char *path = entry->path;
239 unsigned int mode = entry->mode;
240 unsigned long flags = object->flags;
243 * Tag object? Look what it points to..
245 while (object->type == OBJ_TAG) {
246 struct tag *tag = (struct tag *) object;
247 if (revs->tag_objects && !(flags & UNINTERESTING))
248 add_pending_object(revs, object, tag->tag);
249 if (!tag->tagged)
250 die("bad tag");
251 object = parse_object(revs->repo, &tag->tagged->oid);
252 if (!object) {
253 if (revs->ignore_missing_links || (flags & UNINTERESTING))
254 return NULL;
255 if (revs->exclude_promisor_objects &&
256 is_promisor_object(&tag->tagged->oid))
257 return NULL;
258 die("bad object %s", oid_to_hex(&tag->tagged->oid));
260 object->flags |= flags;
262 * We'll handle the tagged object by looping or dropping
263 * through to the non-tag handlers below. Do not
264 * propagate path data from the tag's pending entry.
266 path = NULL;
267 mode = 0;
271 * Commit object? Just return it, we'll do all the complex
272 * reachability crud.
274 if (object->type == OBJ_COMMIT) {
275 struct commit *commit = (struct commit *)object;
277 if (parse_commit(commit) < 0)
278 die("unable to parse commit %s", name);
279 if (flags & UNINTERESTING) {
280 mark_parents_uninteresting(commit);
281 revs->limited = 1;
283 if (revs->sources) {
284 char **slot = revision_sources_at(revs->sources, commit);
286 if (!*slot)
287 *slot = xstrdup(name);
289 return commit;
293 * Tree object? Either mark it uninteresting, or add it
294 * to the list of objects to look at later..
296 if (object->type == OBJ_TREE) {
297 struct tree *tree = (struct tree *)object;
298 if (!revs->tree_objects)
299 return NULL;
300 if (flags & UNINTERESTING) {
301 mark_tree_contents_uninteresting(revs->repo, tree);
302 return NULL;
304 add_pending_object_with_path(revs, object, name, mode, path);
305 return NULL;
309 * Blob object? You know the drill by now..
311 if (object->type == OBJ_BLOB) {
312 if (!revs->blob_objects)
313 return NULL;
314 if (flags & UNINTERESTING)
315 return NULL;
316 add_pending_object_with_path(revs, object, name, mode, path);
317 return NULL;
319 die("%s is unknown object", name);
322 static int everybody_uninteresting(struct commit_list *orig,
323 struct commit **interesting_cache)
325 struct commit_list *list = orig;
327 if (*interesting_cache) {
328 struct commit *commit = *interesting_cache;
329 if (!(commit->object.flags & UNINTERESTING))
330 return 0;
333 while (list) {
334 struct commit *commit = list->item;
335 list = list->next;
336 if (commit->object.flags & UNINTERESTING)
337 continue;
339 *interesting_cache = commit;
340 return 0;
342 return 1;
346 * A definition of "relevant" commit that we can use to simplify limited graphs
347 * by eliminating side branches.
349 * A "relevant" commit is one that is !UNINTERESTING (ie we are including it
350 * in our list), or that is a specified BOTTOM commit. Then after computing
351 * a limited list, during processing we can generally ignore boundary merges
352 * coming from outside the graph, (ie from irrelevant parents), and treat
353 * those merges as if they were single-parent. TREESAME is defined to consider
354 * only relevant parents, if any. If we are TREESAME to our on-graph parents,
355 * we don't care if we were !TREESAME to non-graph parents.
357 * Treating bottom commits as relevant ensures that a limited graph's
358 * connection to the actual bottom commit is not viewed as a side branch, but
359 * treated as part of the graph. For example:
361 * ....Z...A---X---o---o---B
362 * . /
363 * W---Y
365 * When computing "A..B", the A-X connection is at least as important as
366 * Y-X, despite A being flagged UNINTERESTING.
368 * And when computing --ancestry-path "A..B", the A-X connection is more
369 * important than Y-X, despite both A and Y being flagged UNINTERESTING.
371 static inline int relevant_commit(struct commit *commit)
373 return (commit->object.flags & (UNINTERESTING | BOTTOM)) != UNINTERESTING;
377 * Return a single relevant commit from a parent list. If we are a TREESAME
378 * commit, and this selects one of our parents, then we can safely simplify to
379 * that parent.
381 static struct commit *one_relevant_parent(const struct rev_info *revs,
382 struct commit_list *orig)
384 struct commit_list *list = orig;
385 struct commit *relevant = NULL;
387 if (!orig)
388 return NULL;
391 * For 1-parent commits, or if first-parent-only, then return that
392 * first parent (even if not "relevant" by the above definition).
393 * TREESAME will have been set purely on that parent.
395 if (revs->first_parent_only || !orig->next)
396 return orig->item;
399 * For multi-parent commits, identify a sole relevant parent, if any.
400 * If we have only one relevant parent, then TREESAME will be set purely
401 * with regard to that parent, and we can simplify accordingly.
403 * If we have more than one relevant parent, or no relevant parents
404 * (and multiple irrelevant ones), then we can't select a parent here
405 * and return NULL.
407 while (list) {
408 struct commit *commit = list->item;
409 list = list->next;
410 if (relevant_commit(commit)) {
411 if (relevant)
412 return NULL;
413 relevant = commit;
416 return relevant;
420 * The goal is to get REV_TREE_NEW as the result only if the
421 * diff consists of all '+' (and no other changes), REV_TREE_OLD
422 * if the whole diff is removal of old data, and otherwise
423 * REV_TREE_DIFFERENT (of course if the trees are the same we
424 * want REV_TREE_SAME).
426 * The only time we care about the distinction is when
427 * remove_empty_trees is in effect, in which case we care only about
428 * whether the whole change is REV_TREE_NEW, or if there's another type
429 * of change. Which means we can stop the diff early in either of these
430 * cases:
432 * 1. We're not using remove_empty_trees at all.
434 * 2. We saw anything except REV_TREE_NEW.
436 static int tree_difference = REV_TREE_SAME;
438 static void file_add_remove(struct diff_options *options,
439 int addremove, unsigned mode,
440 const struct object_id *oid,
441 int oid_valid,
442 const char *fullpath, unsigned dirty_submodule)
444 int diff = addremove == '+' ? REV_TREE_NEW : REV_TREE_OLD;
445 struct rev_info *revs = options->change_fn_data;
447 tree_difference |= diff;
448 if (!revs->remove_empty_trees || tree_difference != REV_TREE_NEW)
449 options->flags.has_changes = 1;
452 static void file_change(struct diff_options *options,
453 unsigned old_mode, unsigned new_mode,
454 const struct object_id *old_oid,
455 const struct object_id *new_oid,
456 int old_oid_valid, int new_oid_valid,
457 const char *fullpath,
458 unsigned old_dirty_submodule, unsigned new_dirty_submodule)
460 tree_difference = REV_TREE_DIFFERENT;
461 options->flags.has_changes = 1;
464 static int rev_compare_tree(struct rev_info *revs,
465 struct commit *parent, struct commit *commit)
467 struct tree *t1 = get_commit_tree(parent);
468 struct tree *t2 = get_commit_tree(commit);
470 if (!t1)
471 return REV_TREE_NEW;
472 if (!t2)
473 return REV_TREE_OLD;
475 if (revs->simplify_by_decoration) {
477 * If we are simplifying by decoration, then the commit
478 * is worth showing if it has a tag pointing at it.
480 if (get_name_decoration(&commit->object))
481 return REV_TREE_DIFFERENT;
483 * A commit that is not pointed by a tag is uninteresting
484 * if we are not limited by path. This means that you will
485 * see the usual "commits that touch the paths" plus any
486 * tagged commit by specifying both --simplify-by-decoration
487 * and pathspec.
489 if (!revs->prune_data.nr)
490 return REV_TREE_SAME;
493 tree_difference = REV_TREE_SAME;
494 revs->pruning.flags.has_changes = 0;
495 if (diff_tree_oid(&t1->object.oid, &t2->object.oid, "",
496 &revs->pruning) < 0)
497 return REV_TREE_DIFFERENT;
498 return tree_difference;
501 static int rev_same_tree_as_empty(struct rev_info *revs, struct commit *commit)
503 int retval;
504 struct tree *t1 = get_commit_tree(commit);
506 if (!t1)
507 return 0;
509 tree_difference = REV_TREE_SAME;
510 revs->pruning.flags.has_changes = 0;
511 retval = diff_tree_oid(NULL, &t1->object.oid, "", &revs->pruning);
513 return retval >= 0 && (tree_difference == REV_TREE_SAME);
516 struct treesame_state {
517 unsigned int nparents;
518 unsigned char treesame[FLEX_ARRAY];
521 static struct treesame_state *initialise_treesame(struct rev_info *revs, struct commit *commit)
523 unsigned n = commit_list_count(commit->parents);
524 struct treesame_state *st = xcalloc(1, st_add(sizeof(*st), n));
525 st->nparents = n;
526 add_decoration(&revs->treesame, &commit->object, st);
527 return st;
531 * Must be called immediately after removing the nth_parent from a commit's
532 * parent list, if we are maintaining the per-parent treesame[] decoration.
533 * This does not recalculate the master TREESAME flag - update_treesame()
534 * should be called to update it after a sequence of treesame[] modifications
535 * that may have affected it.
537 static int compact_treesame(struct rev_info *revs, struct commit *commit, unsigned nth_parent)
539 struct treesame_state *st;
540 int old_same;
542 if (!commit->parents) {
544 * Have just removed the only parent from a non-merge.
545 * Different handling, as we lack decoration.
547 if (nth_parent != 0)
548 die("compact_treesame %u", nth_parent);
549 old_same = !!(commit->object.flags & TREESAME);
550 if (rev_same_tree_as_empty(revs, commit))
551 commit->object.flags |= TREESAME;
552 else
553 commit->object.flags &= ~TREESAME;
554 return old_same;
557 st = lookup_decoration(&revs->treesame, &commit->object);
558 if (!st || nth_parent >= st->nparents)
559 die("compact_treesame %u", nth_parent);
561 old_same = st->treesame[nth_parent];
562 memmove(st->treesame + nth_parent,
563 st->treesame + nth_parent + 1,
564 st->nparents - nth_parent - 1);
567 * If we've just become a non-merge commit, update TREESAME
568 * immediately, and remove the no-longer-needed decoration.
569 * If still a merge, defer update until update_treesame().
571 if (--st->nparents == 1) {
572 if (commit->parents->next)
573 die("compact_treesame parents mismatch");
574 if (st->treesame[0] && revs->dense)
575 commit->object.flags |= TREESAME;
576 else
577 commit->object.flags &= ~TREESAME;
578 free(add_decoration(&revs->treesame, &commit->object, NULL));
581 return old_same;
584 static unsigned update_treesame(struct rev_info *revs, struct commit *commit)
586 if (commit->parents && commit->parents->next) {
587 unsigned n;
588 struct treesame_state *st;
589 struct commit_list *p;
590 unsigned relevant_parents;
591 unsigned relevant_change, irrelevant_change;
593 st = lookup_decoration(&revs->treesame, &commit->object);
594 if (!st)
595 die("update_treesame %s", oid_to_hex(&commit->object.oid));
596 relevant_parents = 0;
597 relevant_change = irrelevant_change = 0;
598 for (p = commit->parents, n = 0; p; n++, p = p->next) {
599 if (relevant_commit(p->item)) {
600 relevant_change |= !st->treesame[n];
601 relevant_parents++;
602 } else
603 irrelevant_change |= !st->treesame[n];
605 if (relevant_parents ? relevant_change : irrelevant_change)
606 commit->object.flags &= ~TREESAME;
607 else
608 commit->object.flags |= TREESAME;
611 return commit->object.flags & TREESAME;
614 static inline int limiting_can_increase_treesame(const struct rev_info *revs)
617 * TREESAME is irrelevant unless prune && dense;
618 * if simplify_history is set, we can't have a mixture of TREESAME and
619 * !TREESAME INTERESTING parents (and we don't have treesame[]
620 * decoration anyway);
621 * if first_parent_only is set, then the TREESAME flag is locked
622 * against the first parent (and again we lack treesame[] decoration).
624 return revs->prune && revs->dense &&
625 !revs->simplify_history &&
626 !revs->first_parent_only;
629 static void try_to_simplify_commit(struct rev_info *revs, struct commit *commit)
631 struct commit_list **pp, *parent;
632 struct treesame_state *ts = NULL;
633 int relevant_change = 0, irrelevant_change = 0;
634 int relevant_parents, nth_parent;
637 * If we don't do pruning, everything is interesting
639 if (!revs->prune)
640 return;
642 if (!get_commit_tree(commit))
643 return;
645 if (!commit->parents) {
646 if (rev_same_tree_as_empty(revs, commit))
647 commit->object.flags |= TREESAME;
648 return;
652 * Normal non-merge commit? If we don't want to make the
653 * history dense, we consider it always to be a change..
655 if (!revs->dense && !commit->parents->next)
656 return;
658 for (pp = &commit->parents, nth_parent = 0, relevant_parents = 0;
659 (parent = *pp) != NULL;
660 pp = &parent->next, nth_parent++) {
661 struct commit *p = parent->item;
662 if (relevant_commit(p))
663 relevant_parents++;
665 if (nth_parent == 1) {
667 * This our second loop iteration - so we now know
668 * we're dealing with a merge.
670 * Do not compare with later parents when we care only about
671 * the first parent chain, in order to avoid derailing the
672 * traversal to follow a side branch that brought everything
673 * in the path we are limited to by the pathspec.
675 if (revs->first_parent_only)
676 break;
678 * If this will remain a potentially-simplifiable
679 * merge, remember per-parent treesame if needed.
680 * Initialise the array with the comparison from our
681 * first iteration.
683 if (revs->treesame.name &&
684 !revs->simplify_history &&
685 !(commit->object.flags & UNINTERESTING)) {
686 ts = initialise_treesame(revs, commit);
687 if (!(irrelevant_change || relevant_change))
688 ts->treesame[0] = 1;
691 if (parse_commit(p) < 0)
692 die("cannot simplify commit %s (because of %s)",
693 oid_to_hex(&commit->object.oid),
694 oid_to_hex(&p->object.oid));
695 switch (rev_compare_tree(revs, p, commit)) {
696 case REV_TREE_SAME:
697 if (!revs->simplify_history || !relevant_commit(p)) {
698 /* Even if a merge with an uninteresting
699 * side branch brought the entire change
700 * we are interested in, we do not want
701 * to lose the other branches of this
702 * merge, so we just keep going.
704 if (ts)
705 ts->treesame[nth_parent] = 1;
706 continue;
708 parent->next = NULL;
709 commit->parents = parent;
710 commit->object.flags |= TREESAME;
711 return;
713 case REV_TREE_NEW:
714 if (revs->remove_empty_trees &&
715 rev_same_tree_as_empty(revs, p)) {
716 /* We are adding all the specified
717 * paths from this parent, so the
718 * history beyond this parent is not
719 * interesting. Remove its parents
720 * (they are grandparents for us).
721 * IOW, we pretend this parent is a
722 * "root" commit.
724 if (parse_commit(p) < 0)
725 die("cannot simplify commit %s (invalid %s)",
726 oid_to_hex(&commit->object.oid),
727 oid_to_hex(&p->object.oid));
728 p->parents = NULL;
730 /* fallthrough */
731 case REV_TREE_OLD:
732 case REV_TREE_DIFFERENT:
733 if (relevant_commit(p))
734 relevant_change = 1;
735 else
736 irrelevant_change = 1;
737 continue;
739 die("bad tree compare for commit %s", oid_to_hex(&commit->object.oid));
743 * TREESAME is straightforward for single-parent commits. For merge
744 * commits, it is most useful to define it so that "irrelevant"
745 * parents cannot make us !TREESAME - if we have any relevant
746 * parents, then we only consider TREESAMEness with respect to them,
747 * allowing irrelevant merges from uninteresting branches to be
748 * simplified away. Only if we have only irrelevant parents do we
749 * base TREESAME on them. Note that this logic is replicated in
750 * update_treesame, which should be kept in sync.
752 if (relevant_parents ? !relevant_change : !irrelevant_change)
753 commit->object.flags |= TREESAME;
756 static void commit_list_insert_by_date_cached(struct commit *p, struct commit_list **head,
757 struct commit_list *cached_base, struct commit_list **cache)
759 struct commit_list *new_entry;
761 if (cached_base && p->date < cached_base->item->date)
762 new_entry = commit_list_insert_by_date(p, &cached_base->next);
763 else
764 new_entry = commit_list_insert_by_date(p, head);
766 if (cache && (!*cache || p->date < (*cache)->item->date))
767 *cache = new_entry;
770 static int add_parents_to_list(struct rev_info *revs, struct commit *commit,
771 struct commit_list **list, struct commit_list **cache_ptr)
773 struct commit_list *parent = commit->parents;
774 unsigned left_flag;
775 struct commit_list *cached_base = cache_ptr ? *cache_ptr : NULL;
777 if (commit->object.flags & ADDED)
778 return 0;
779 commit->object.flags |= ADDED;
781 if (revs->include_check &&
782 !revs->include_check(commit, revs->include_check_data))
783 return 0;
786 * If the commit is uninteresting, don't try to
787 * prune parents - we want the maximal uninteresting
788 * set.
790 * Normally we haven't parsed the parent
791 * yet, so we won't have a parent of a parent
792 * here. However, it may turn out that we've
793 * reached this commit some other way (where it
794 * wasn't uninteresting), in which case we need
795 * to mark its parents recursively too..
797 if (commit->object.flags & UNINTERESTING) {
798 while (parent) {
799 struct commit *p = parent->item;
800 parent = parent->next;
801 if (p)
802 p->object.flags |= UNINTERESTING;
803 if (parse_commit_gently(p, 1) < 0)
804 continue;
805 if (p->parents)
806 mark_parents_uninteresting(p);
807 if (p->object.flags & SEEN)
808 continue;
809 p->object.flags |= SEEN;
810 commit_list_insert_by_date_cached(p, list, cached_base, cache_ptr);
812 return 0;
816 * Ok, the commit wasn't uninteresting. Try to
817 * simplify the commit history and find the parent
818 * that has no differences in the path set if one exists.
820 try_to_simplify_commit(revs, commit);
822 if (revs->no_walk)
823 return 0;
825 left_flag = (commit->object.flags & SYMMETRIC_LEFT);
827 for (parent = commit->parents; parent; parent = parent->next) {
828 struct commit *p = parent->item;
829 int gently = revs->ignore_missing_links ||
830 revs->exclude_promisor_objects;
831 if (parse_commit_gently(p, gently) < 0) {
832 if (revs->exclude_promisor_objects &&
833 is_promisor_object(&p->object.oid)) {
834 if (revs->first_parent_only)
835 break;
836 continue;
838 return -1;
840 if (revs->sources) {
841 char **slot = revision_sources_at(revs->sources, p);
843 if (!*slot)
844 *slot = *revision_sources_at(revs->sources, commit);
846 p->object.flags |= left_flag;
847 if (!(p->object.flags & SEEN)) {
848 p->object.flags |= SEEN;
849 commit_list_insert_by_date_cached(p, list, cached_base, cache_ptr);
851 if (revs->first_parent_only)
852 break;
854 return 0;
857 static void cherry_pick_list(struct commit_list *list, struct rev_info *revs)
859 struct commit_list *p;
860 int left_count = 0, right_count = 0;
861 int left_first;
862 struct patch_ids ids;
863 unsigned cherry_flag;
865 /* First count the commits on the left and on the right */
866 for (p = list; p; p = p->next) {
867 struct commit *commit = p->item;
868 unsigned flags = commit->object.flags;
869 if (flags & BOUNDARY)
871 else if (flags & SYMMETRIC_LEFT)
872 left_count++;
873 else
874 right_count++;
877 if (!left_count || !right_count)
878 return;
880 left_first = left_count < right_count;
881 init_patch_ids(revs->repo, &ids);
882 ids.diffopts.pathspec = revs->diffopt.pathspec;
884 /* Compute patch-ids for one side */
885 for (p = list; p; p = p->next) {
886 struct commit *commit = p->item;
887 unsigned flags = commit->object.flags;
889 if (flags & BOUNDARY)
890 continue;
892 * If we have fewer left, left_first is set and we omit
893 * commits on the right branch in this loop. If we have
894 * fewer right, we skip the left ones.
896 if (left_first != !!(flags & SYMMETRIC_LEFT))
897 continue;
898 add_commit_patch_id(commit, &ids);
901 /* either cherry_mark or cherry_pick are true */
902 cherry_flag = revs->cherry_mark ? PATCHSAME : SHOWN;
904 /* Check the other side */
905 for (p = list; p; p = p->next) {
906 struct commit *commit = p->item;
907 struct patch_id *id;
908 unsigned flags = commit->object.flags;
910 if (flags & BOUNDARY)
911 continue;
913 * If we have fewer left, left_first is set and we omit
914 * commits on the left branch in this loop.
916 if (left_first == !!(flags & SYMMETRIC_LEFT))
917 continue;
920 * Have we seen the same patch id?
922 id = has_commit_patch_id(commit, &ids);
923 if (!id)
924 continue;
926 commit->object.flags |= cherry_flag;
927 id->commit->object.flags |= cherry_flag;
930 free_patch_ids(&ids);
933 /* How many extra uninteresting commits we want to see.. */
934 #define SLOP 5
936 static int still_interesting(struct commit_list *src, timestamp_t date, int slop,
937 struct commit **interesting_cache)
940 * No source list at all? We're definitely done..
942 if (!src)
943 return 0;
946 * Does the destination list contain entries with a date
947 * before the source list? Definitely _not_ done.
949 if (date <= src->item->date)
950 return SLOP;
953 * Does the source list still have interesting commits in
954 * it? Definitely not done..
956 if (!everybody_uninteresting(src, interesting_cache))
957 return SLOP;
959 /* Ok, we're closing in.. */
960 return slop-1;
964 * "rev-list --ancestry-path A..B" computes commits that are ancestors
965 * of B but not ancestors of A but further limits the result to those
966 * that are descendants of A. This takes the list of bottom commits and
967 * the result of "A..B" without --ancestry-path, and limits the latter
968 * further to the ones that can reach one of the commits in "bottom".
970 static void limit_to_ancestry(struct commit_list *bottom, struct commit_list *list)
972 struct commit_list *p;
973 struct commit_list *rlist = NULL;
974 int made_progress;
977 * Reverse the list so that it will be likely that we would
978 * process parents before children.
980 for (p = list; p; p = p->next)
981 commit_list_insert(p->item, &rlist);
983 for (p = bottom; p; p = p->next)
984 p->item->object.flags |= TMP_MARK;
987 * Mark the ones that can reach bottom commits in "list",
988 * in a bottom-up fashion.
990 do {
991 made_progress = 0;
992 for (p = rlist; p; p = p->next) {
993 struct commit *c = p->item;
994 struct commit_list *parents;
995 if (c->object.flags & (TMP_MARK | UNINTERESTING))
996 continue;
997 for (parents = c->parents;
998 parents;
999 parents = parents->next) {
1000 if (!(parents->item->object.flags & TMP_MARK))
1001 continue;
1002 c->object.flags |= TMP_MARK;
1003 made_progress = 1;
1004 break;
1007 } while (made_progress);
1010 * NEEDSWORK: decide if we want to remove parents that are
1011 * not marked with TMP_MARK from commit->parents for commits
1012 * in the resulting list. We may not want to do that, though.
1016 * The ones that are not marked with TMP_MARK are uninteresting
1018 for (p = list; p; p = p->next) {
1019 struct commit *c = p->item;
1020 if (c->object.flags & TMP_MARK)
1021 continue;
1022 c->object.flags |= UNINTERESTING;
1025 /* We are done with the TMP_MARK */
1026 for (p = list; p; p = p->next)
1027 p->item->object.flags &= ~TMP_MARK;
1028 for (p = bottom; p; p = p->next)
1029 p->item->object.flags &= ~TMP_MARK;
1030 free_commit_list(rlist);
1034 * Before walking the history, keep the set of "negative" refs the
1035 * caller has asked to exclude.
1037 * This is used to compute "rev-list --ancestry-path A..B", as we need
1038 * to filter the result of "A..B" further to the ones that can actually
1039 * reach A.
1041 static struct commit_list *collect_bottom_commits(struct commit_list *list)
1043 struct commit_list *elem, *bottom = NULL;
1044 for (elem = list; elem; elem = elem->next)
1045 if (elem->item->object.flags & BOTTOM)
1046 commit_list_insert(elem->item, &bottom);
1047 return bottom;
1050 /* Assumes either left_only or right_only is set */
1051 static void limit_left_right(struct commit_list *list, struct rev_info *revs)
1053 struct commit_list *p;
1055 for (p = list; p; p = p->next) {
1056 struct commit *commit = p->item;
1058 if (revs->right_only) {
1059 if (commit->object.flags & SYMMETRIC_LEFT)
1060 commit->object.flags |= SHOWN;
1061 } else /* revs->left_only is set */
1062 if (!(commit->object.flags & SYMMETRIC_LEFT))
1063 commit->object.flags |= SHOWN;
1067 static int limit_list(struct rev_info *revs)
1069 int slop = SLOP;
1070 timestamp_t date = TIME_MAX;
1071 struct commit_list *list = revs->commits;
1072 struct commit_list *newlist = NULL;
1073 struct commit_list **p = &newlist;
1074 struct commit_list *bottom = NULL;
1075 struct commit *interesting_cache = NULL;
1077 if (revs->ancestry_path) {
1078 bottom = collect_bottom_commits(list);
1079 if (!bottom)
1080 die("--ancestry-path given but there are no bottom commits");
1083 while (list) {
1084 struct commit *commit = pop_commit(&list);
1085 struct object *obj = &commit->object;
1086 show_early_output_fn_t show;
1088 if (commit == interesting_cache)
1089 interesting_cache = NULL;
1091 if (revs->max_age != -1 && (commit->date < revs->max_age))
1092 obj->flags |= UNINTERESTING;
1093 if (add_parents_to_list(revs, commit, &list, NULL) < 0)
1094 return -1;
1095 if (obj->flags & UNINTERESTING) {
1096 mark_parents_uninteresting(commit);
1097 slop = still_interesting(list, date, slop, &interesting_cache);
1098 if (slop)
1099 continue;
1100 break;
1102 if (revs->min_age != -1 && (commit->date > revs->min_age))
1103 continue;
1104 date = commit->date;
1105 p = &commit_list_insert(commit, p)->next;
1107 show = show_early_output;
1108 if (!show)
1109 continue;
1111 show(revs, newlist);
1112 show_early_output = NULL;
1114 if (revs->cherry_pick || revs->cherry_mark)
1115 cherry_pick_list(newlist, revs);
1117 if (revs->left_only || revs->right_only)
1118 limit_left_right(newlist, revs);
1120 if (bottom) {
1121 limit_to_ancestry(bottom, newlist);
1122 free_commit_list(bottom);
1126 * Check if any commits have become TREESAME by some of their parents
1127 * becoming UNINTERESTING.
1129 if (limiting_can_increase_treesame(revs))
1130 for (list = newlist; list; list = list->next) {
1131 struct commit *c = list->item;
1132 if (c->object.flags & (UNINTERESTING | TREESAME))
1133 continue;
1134 update_treesame(revs, c);
1137 revs->commits = newlist;
1138 return 0;
1142 * Add an entry to refs->cmdline with the specified information.
1143 * *name is copied.
1145 static void add_rev_cmdline(struct rev_info *revs,
1146 struct object *item,
1147 const char *name,
1148 int whence,
1149 unsigned flags)
1151 struct rev_cmdline_info *info = &revs->cmdline;
1152 unsigned int nr = info->nr;
1154 ALLOC_GROW(info->rev, nr + 1, info->alloc);
1155 info->rev[nr].item = item;
1156 info->rev[nr].name = xstrdup(name);
1157 info->rev[nr].whence = whence;
1158 info->rev[nr].flags = flags;
1159 info->nr++;
1162 static void add_rev_cmdline_list(struct rev_info *revs,
1163 struct commit_list *commit_list,
1164 int whence,
1165 unsigned flags)
1167 while (commit_list) {
1168 struct object *object = &commit_list->item->object;
1169 add_rev_cmdline(revs, object, oid_to_hex(&object->oid),
1170 whence, flags);
1171 commit_list = commit_list->next;
1175 struct all_refs_cb {
1176 int all_flags;
1177 int warned_bad_reflog;
1178 struct rev_info *all_revs;
1179 const char *name_for_errormsg;
1180 struct ref_store *refs;
1183 int ref_excluded(struct string_list *ref_excludes, const char *path)
1185 struct string_list_item *item;
1187 if (!ref_excludes)
1188 return 0;
1189 for_each_string_list_item(item, ref_excludes) {
1190 if (!wildmatch(item->string, path, 0))
1191 return 1;
1193 return 0;
1196 static int handle_one_ref(const char *path, const struct object_id *oid,
1197 int flag, void *cb_data)
1199 struct all_refs_cb *cb = cb_data;
1200 struct object *object;
1202 if (ref_excluded(cb->all_revs->ref_excludes, path))
1203 return 0;
1205 object = get_reference(cb->all_revs, path, oid, cb->all_flags);
1206 add_rev_cmdline(cb->all_revs, object, path, REV_CMD_REF, cb->all_flags);
1207 add_pending_oid(cb->all_revs, path, oid, cb->all_flags);
1208 return 0;
1211 static void init_all_refs_cb(struct all_refs_cb *cb, struct rev_info *revs,
1212 unsigned flags)
1214 cb->all_revs = revs;
1215 cb->all_flags = flags;
1216 revs->rev_input_given = 1;
1217 cb->refs = NULL;
1220 void clear_ref_exclusion(struct string_list **ref_excludes_p)
1222 if (*ref_excludes_p) {
1223 string_list_clear(*ref_excludes_p, 0);
1224 free(*ref_excludes_p);
1226 *ref_excludes_p = NULL;
1229 void add_ref_exclusion(struct string_list **ref_excludes_p, const char *exclude)
1231 if (!*ref_excludes_p) {
1232 *ref_excludes_p = xcalloc(1, sizeof(**ref_excludes_p));
1233 (*ref_excludes_p)->strdup_strings = 1;
1235 string_list_append(*ref_excludes_p, exclude);
1238 static void handle_refs(struct ref_store *refs,
1239 struct rev_info *revs, unsigned flags,
1240 int (*for_each)(struct ref_store *, each_ref_fn, void *))
1242 struct all_refs_cb cb;
1244 if (!refs) {
1245 /* this could happen with uninitialized submodules */
1246 return;
1249 init_all_refs_cb(&cb, revs, flags);
1250 for_each(refs, handle_one_ref, &cb);
1253 static void handle_one_reflog_commit(struct object_id *oid, void *cb_data)
1255 struct all_refs_cb *cb = cb_data;
1256 if (!is_null_oid(oid)) {
1257 struct object *o = parse_object(cb->all_revs->repo, oid);
1258 if (o) {
1259 o->flags |= cb->all_flags;
1260 /* ??? CMDLINEFLAGS ??? */
1261 add_pending_object(cb->all_revs, o, "");
1263 else if (!cb->warned_bad_reflog) {
1264 warning("reflog of '%s' references pruned commits",
1265 cb->name_for_errormsg);
1266 cb->warned_bad_reflog = 1;
1271 static int handle_one_reflog_ent(struct object_id *ooid, struct object_id *noid,
1272 const char *email, timestamp_t timestamp, int tz,
1273 const char *message, void *cb_data)
1275 handle_one_reflog_commit(ooid, cb_data);
1276 handle_one_reflog_commit(noid, cb_data);
1277 return 0;
1280 static int handle_one_reflog(const char *path, const struct object_id *oid,
1281 int flag, void *cb_data)
1283 struct all_refs_cb *cb = cb_data;
1284 cb->warned_bad_reflog = 0;
1285 cb->name_for_errormsg = path;
1286 refs_for_each_reflog_ent(cb->refs, path,
1287 handle_one_reflog_ent, cb_data);
1288 return 0;
1291 static void add_other_reflogs_to_pending(struct all_refs_cb *cb)
1293 struct worktree **worktrees, **p;
1295 worktrees = get_worktrees(0);
1296 for (p = worktrees; *p; p++) {
1297 struct worktree *wt = *p;
1299 if (wt->is_current)
1300 continue;
1302 cb->refs = get_worktree_ref_store(wt);
1303 refs_for_each_reflog(cb->refs,
1304 handle_one_reflog,
1305 cb);
1307 free_worktrees(worktrees);
1310 void add_reflogs_to_pending(struct rev_info *revs, unsigned flags)
1312 struct all_refs_cb cb;
1314 cb.all_revs = revs;
1315 cb.all_flags = flags;
1316 cb.refs = get_main_ref_store(revs->repo);
1317 for_each_reflog(handle_one_reflog, &cb);
1319 if (!revs->single_worktree)
1320 add_other_reflogs_to_pending(&cb);
1323 static void add_cache_tree(struct cache_tree *it, struct rev_info *revs,
1324 struct strbuf *path)
1326 size_t baselen = path->len;
1327 int i;
1329 if (it->entry_count >= 0) {
1330 struct tree *tree = lookup_tree(revs->repo, &it->oid);
1331 add_pending_object_with_path(revs, &tree->object, "",
1332 040000, path->buf);
1335 for (i = 0; i < it->subtree_nr; i++) {
1336 struct cache_tree_sub *sub = it->down[i];
1337 strbuf_addf(path, "%s%s", baselen ? "/" : "", sub->name);
1338 add_cache_tree(sub->cache_tree, revs, path);
1339 strbuf_setlen(path, baselen);
1344 static void do_add_index_objects_to_pending(struct rev_info *revs,
1345 struct index_state *istate)
1347 int i;
1349 for (i = 0; i < istate->cache_nr; i++) {
1350 struct cache_entry *ce = istate->cache[i];
1351 struct blob *blob;
1353 if (S_ISGITLINK(ce->ce_mode))
1354 continue;
1356 blob = lookup_blob(revs->repo, &ce->oid);
1357 if (!blob)
1358 die("unable to add index blob to traversal");
1359 add_pending_object_with_path(revs, &blob->object, "",
1360 ce->ce_mode, ce->name);
1363 if (istate->cache_tree) {
1364 struct strbuf path = STRBUF_INIT;
1365 add_cache_tree(istate->cache_tree, revs, &path);
1366 strbuf_release(&path);
1370 void add_index_objects_to_pending(struct rev_info *revs, unsigned int flags)
1372 struct worktree **worktrees, **p;
1374 read_index(revs->repo->index);
1375 do_add_index_objects_to_pending(revs, revs->repo->index);
1377 if (revs->single_worktree)
1378 return;
1380 worktrees = get_worktrees(0);
1381 for (p = worktrees; *p; p++) {
1382 struct worktree *wt = *p;
1383 struct index_state istate = { NULL };
1385 if (wt->is_current)
1386 continue; /* current index already taken care of */
1388 if (read_index_from(&istate,
1389 worktree_git_path(wt, "index"),
1390 get_worktree_git_dir(wt)) > 0)
1391 do_add_index_objects_to_pending(revs, &istate);
1392 discard_index(&istate);
1394 free_worktrees(worktrees);
1397 static int add_parents_only(struct rev_info *revs, const char *arg_, int flags,
1398 int exclude_parent)
1400 struct object_id oid;
1401 struct object *it;
1402 struct commit *commit;
1403 struct commit_list *parents;
1404 int parent_number;
1405 const char *arg = arg_;
1407 if (*arg == '^') {
1408 flags ^= UNINTERESTING | BOTTOM;
1409 arg++;
1411 if (get_oid_committish(arg, &oid))
1412 return 0;
1413 while (1) {
1414 it = get_reference(revs, arg, &oid, 0);
1415 if (!it && revs->ignore_missing)
1416 return 0;
1417 if (it->type != OBJ_TAG)
1418 break;
1419 if (!((struct tag*)it)->tagged)
1420 return 0;
1421 oidcpy(&oid, &((struct tag*)it)->tagged->oid);
1423 if (it->type != OBJ_COMMIT)
1424 return 0;
1425 commit = (struct commit *)it;
1426 if (exclude_parent &&
1427 exclude_parent > commit_list_count(commit->parents))
1428 return 0;
1429 for (parents = commit->parents, parent_number = 1;
1430 parents;
1431 parents = parents->next, parent_number++) {
1432 if (exclude_parent && parent_number != exclude_parent)
1433 continue;
1435 it = &parents->item->object;
1436 it->flags |= flags;
1437 add_rev_cmdline(revs, it, arg_, REV_CMD_PARENTS_ONLY, flags);
1438 add_pending_object(revs, it, arg);
1440 return 1;
1443 void repo_init_revisions(struct repository *r,
1444 struct rev_info *revs,
1445 const char *prefix)
1447 memset(revs, 0, sizeof(*revs));
1449 revs->repo = r;
1450 revs->abbrev = DEFAULT_ABBREV;
1451 revs->ignore_merges = 1;
1452 revs->simplify_history = 1;
1453 revs->pruning.flags.recursive = 1;
1454 revs->pruning.flags.quick = 1;
1455 revs->pruning.add_remove = file_add_remove;
1456 revs->pruning.change = file_change;
1457 revs->pruning.change_fn_data = revs;
1458 revs->sort_order = REV_SORT_IN_GRAPH_ORDER;
1459 revs->dense = 1;
1460 revs->prefix = prefix;
1461 revs->max_age = -1;
1462 revs->min_age = -1;
1463 revs->skip_count = -1;
1464 revs->max_count = -1;
1465 revs->max_parents = -1;
1466 revs->expand_tabs_in_log = -1;
1468 revs->commit_format = CMIT_FMT_DEFAULT;
1469 revs->expand_tabs_in_log_default = 8;
1471 init_grep_defaults(revs->repo);
1472 grep_init(&revs->grep_filter, revs->repo, prefix);
1473 revs->grep_filter.status_only = 1;
1475 repo_diff_setup(revs->repo, &revs->diffopt);
1476 if (prefix && !revs->diffopt.prefix) {
1477 revs->diffopt.prefix = prefix;
1478 revs->diffopt.prefix_length = strlen(prefix);
1481 revs->notes_opt.use_default_notes = -1;
1484 static void add_pending_commit_list(struct rev_info *revs,
1485 struct commit_list *commit_list,
1486 unsigned int flags)
1488 while (commit_list) {
1489 struct object *object = &commit_list->item->object;
1490 object->flags |= flags;
1491 add_pending_object(revs, object, oid_to_hex(&object->oid));
1492 commit_list = commit_list->next;
1496 static void prepare_show_merge(struct rev_info *revs)
1498 struct commit_list *bases;
1499 struct commit *head, *other;
1500 struct object_id oid;
1501 const char **prune = NULL;
1502 int i, prune_num = 1; /* counting terminating NULL */
1503 struct index_state *istate = revs->repo->index;
1505 if (get_oid("HEAD", &oid))
1506 die("--merge without HEAD?");
1507 head = lookup_commit_or_die(&oid, "HEAD");
1508 if (get_oid("MERGE_HEAD", &oid))
1509 die("--merge without MERGE_HEAD?");
1510 other = lookup_commit_or_die(&oid, "MERGE_HEAD");
1511 add_pending_object(revs, &head->object, "HEAD");
1512 add_pending_object(revs, &other->object, "MERGE_HEAD");
1513 bases = get_merge_bases(head, other);
1514 add_rev_cmdline_list(revs, bases, REV_CMD_MERGE_BASE, UNINTERESTING | BOTTOM);
1515 add_pending_commit_list(revs, bases, UNINTERESTING | BOTTOM);
1516 free_commit_list(bases);
1517 head->object.flags |= SYMMETRIC_LEFT;
1519 if (!istate->cache_nr)
1520 read_index(istate);
1521 for (i = 0; i < istate->cache_nr; i++) {
1522 const struct cache_entry *ce = istate->cache[i];
1523 if (!ce_stage(ce))
1524 continue;
1525 if (ce_path_match(istate, ce, &revs->prune_data, NULL)) {
1526 prune_num++;
1527 REALLOC_ARRAY(prune, prune_num);
1528 prune[prune_num-2] = ce->name;
1529 prune[prune_num-1] = NULL;
1531 while ((i+1 < istate->cache_nr) &&
1532 ce_same_name(ce, istate->cache[i+1]))
1533 i++;
1535 clear_pathspec(&revs->prune_data);
1536 parse_pathspec(&revs->prune_data, PATHSPEC_ALL_MAGIC & ~PATHSPEC_LITERAL,
1537 PATHSPEC_PREFER_FULL | PATHSPEC_LITERAL_PATH, "", prune);
1538 revs->limited = 1;
1541 static int dotdot_missing(const char *arg, char *dotdot,
1542 struct rev_info *revs, int symmetric)
1544 if (revs->ignore_missing)
1545 return 0;
1546 /* de-munge so we report the full argument */
1547 *dotdot = '.';
1548 die(symmetric
1549 ? "Invalid symmetric difference expression %s"
1550 : "Invalid revision range %s", arg);
1553 static int handle_dotdot_1(const char *arg, char *dotdot,
1554 struct rev_info *revs, int flags,
1555 int cant_be_filename,
1556 struct object_context *a_oc,
1557 struct object_context *b_oc)
1559 const char *a_name, *b_name;
1560 struct object_id a_oid, b_oid;
1561 struct object *a_obj, *b_obj;
1562 unsigned int a_flags, b_flags;
1563 int symmetric = 0;
1564 unsigned int flags_exclude = flags ^ (UNINTERESTING | BOTTOM);
1565 unsigned int oc_flags = GET_OID_COMMITTISH | GET_OID_RECORD_PATH;
1567 a_name = arg;
1568 if (!*a_name)
1569 a_name = "HEAD";
1571 b_name = dotdot + 2;
1572 if (*b_name == '.') {
1573 symmetric = 1;
1574 b_name++;
1576 if (!*b_name)
1577 b_name = "HEAD";
1579 if (get_oid_with_context(a_name, oc_flags, &a_oid, a_oc) ||
1580 get_oid_with_context(b_name, oc_flags, &b_oid, b_oc))
1581 return -1;
1583 if (!cant_be_filename) {
1584 *dotdot = '.';
1585 verify_non_filename(revs->prefix, arg);
1586 *dotdot = '\0';
1589 a_obj = parse_object(revs->repo, &a_oid);
1590 b_obj = parse_object(revs->repo, &b_oid);
1591 if (!a_obj || !b_obj)
1592 return dotdot_missing(arg, dotdot, revs, symmetric);
1594 if (!symmetric) {
1595 /* just A..B */
1596 b_flags = flags;
1597 a_flags = flags_exclude;
1598 } else {
1599 /* A...B -- find merge bases between the two */
1600 struct commit *a, *b;
1601 struct commit_list *exclude;
1603 a = lookup_commit_reference(revs->repo, &a_obj->oid);
1604 b = lookup_commit_reference(revs->repo, &b_obj->oid);
1605 if (!a || !b)
1606 return dotdot_missing(arg, dotdot, revs, symmetric);
1608 exclude = get_merge_bases(a, b);
1609 add_rev_cmdline_list(revs, exclude, REV_CMD_MERGE_BASE,
1610 flags_exclude);
1611 add_pending_commit_list(revs, exclude, flags_exclude);
1612 free_commit_list(exclude);
1614 b_flags = flags;
1615 a_flags = flags | SYMMETRIC_LEFT;
1618 a_obj->flags |= a_flags;
1619 b_obj->flags |= b_flags;
1620 add_rev_cmdline(revs, a_obj, a_name, REV_CMD_LEFT, a_flags);
1621 add_rev_cmdline(revs, b_obj, b_name, REV_CMD_RIGHT, b_flags);
1622 add_pending_object_with_path(revs, a_obj, a_name, a_oc->mode, a_oc->path);
1623 add_pending_object_with_path(revs, b_obj, b_name, b_oc->mode, b_oc->path);
1624 return 0;
1627 static int handle_dotdot(const char *arg,
1628 struct rev_info *revs, int flags,
1629 int cant_be_filename)
1631 struct object_context a_oc, b_oc;
1632 char *dotdot = strstr(arg, "..");
1633 int ret;
1635 if (!dotdot)
1636 return -1;
1638 memset(&a_oc, 0, sizeof(a_oc));
1639 memset(&b_oc, 0, sizeof(b_oc));
1641 *dotdot = '\0';
1642 ret = handle_dotdot_1(arg, dotdot, revs, flags, cant_be_filename,
1643 &a_oc, &b_oc);
1644 *dotdot = '.';
1646 free(a_oc.path);
1647 free(b_oc.path);
1649 return ret;
1652 int handle_revision_arg(const char *arg_, struct rev_info *revs, int flags, unsigned revarg_opt)
1654 struct object_context oc;
1655 char *mark;
1656 struct object *object;
1657 struct object_id oid;
1658 int local_flags;
1659 const char *arg = arg_;
1660 int cant_be_filename = revarg_opt & REVARG_CANNOT_BE_FILENAME;
1661 unsigned get_sha1_flags = GET_OID_RECORD_PATH;
1663 flags = flags & UNINTERESTING ? flags | BOTTOM : flags & ~BOTTOM;
1665 if (!cant_be_filename && !strcmp(arg, "..")) {
1667 * Just ".."? That is not a range but the
1668 * pathspec for the parent directory.
1670 return -1;
1673 if (!handle_dotdot(arg, revs, flags, revarg_opt))
1674 return 0;
1676 mark = strstr(arg, "^@");
1677 if (mark && !mark[2]) {
1678 *mark = 0;
1679 if (add_parents_only(revs, arg, flags, 0))
1680 return 0;
1681 *mark = '^';
1683 mark = strstr(arg, "^!");
1684 if (mark && !mark[2]) {
1685 *mark = 0;
1686 if (!add_parents_only(revs, arg, flags ^ (UNINTERESTING | BOTTOM), 0))
1687 *mark = '^';
1689 mark = strstr(arg, "^-");
1690 if (mark) {
1691 int exclude_parent = 1;
1693 if (mark[2]) {
1694 char *end;
1695 exclude_parent = strtoul(mark + 2, &end, 10);
1696 if (*end != '\0' || !exclude_parent)
1697 return -1;
1700 *mark = 0;
1701 if (!add_parents_only(revs, arg, flags ^ (UNINTERESTING | BOTTOM), exclude_parent))
1702 *mark = '^';
1705 local_flags = 0;
1706 if (*arg == '^') {
1707 local_flags = UNINTERESTING | BOTTOM;
1708 arg++;
1711 if (revarg_opt & REVARG_COMMITTISH)
1712 get_sha1_flags |= GET_OID_COMMITTISH;
1714 if (get_oid_with_context(arg, get_sha1_flags, &oid, &oc))
1715 return revs->ignore_missing ? 0 : -1;
1716 if (!cant_be_filename)
1717 verify_non_filename(revs->prefix, arg);
1718 object = get_reference(revs, arg, &oid, flags ^ local_flags);
1719 add_rev_cmdline(revs, object, arg_, REV_CMD_REV, flags ^ local_flags);
1720 add_pending_object_with_path(revs, object, arg, oc.mode, oc.path);
1721 free(oc.path);
1722 return 0;
1725 static void read_pathspec_from_stdin(struct rev_info *revs, struct strbuf *sb,
1726 struct argv_array *prune)
1728 while (strbuf_getline(sb, stdin) != EOF)
1729 argv_array_push(prune, sb->buf);
1732 static void read_revisions_from_stdin(struct rev_info *revs,
1733 struct argv_array *prune)
1735 struct strbuf sb;
1736 int seen_dashdash = 0;
1737 int save_warning;
1739 save_warning = warn_on_object_refname_ambiguity;
1740 warn_on_object_refname_ambiguity = 0;
1742 strbuf_init(&sb, 1000);
1743 while (strbuf_getline(&sb, stdin) != EOF) {
1744 int len = sb.len;
1745 if (!len)
1746 break;
1747 if (sb.buf[0] == '-') {
1748 if (len == 2 && sb.buf[1] == '-') {
1749 seen_dashdash = 1;
1750 break;
1752 die("options not supported in --stdin mode");
1754 if (handle_revision_arg(sb.buf, revs, 0,
1755 REVARG_CANNOT_BE_FILENAME))
1756 die("bad revision '%s'", sb.buf);
1758 if (seen_dashdash)
1759 read_pathspec_from_stdin(revs, &sb, prune);
1761 strbuf_release(&sb);
1762 warn_on_object_refname_ambiguity = save_warning;
1765 static void add_grep(struct rev_info *revs, const char *ptn, enum grep_pat_token what)
1767 append_grep_pattern(&revs->grep_filter, ptn, "command line", 0, what);
1770 static void add_header_grep(struct rev_info *revs, enum grep_header_field field, const char *pattern)
1772 append_header_grep_pattern(&revs->grep_filter, field, pattern);
1775 static void add_message_grep(struct rev_info *revs, const char *pattern)
1777 add_grep(revs, pattern, GREP_PATTERN_BODY);
1780 static int handle_revision_opt(struct rev_info *revs, int argc, const char **argv,
1781 int *unkc, const char **unkv)
1783 const char *arg = argv[0];
1784 const char *optarg;
1785 int argcount;
1786 const unsigned hexsz = the_hash_algo->hexsz;
1788 /* pseudo revision arguments */
1789 if (!strcmp(arg, "--all") || !strcmp(arg, "--branches") ||
1790 !strcmp(arg, "--tags") || !strcmp(arg, "--remotes") ||
1791 !strcmp(arg, "--reflog") || !strcmp(arg, "--not") ||
1792 !strcmp(arg, "--no-walk") || !strcmp(arg, "--do-walk") ||
1793 !strcmp(arg, "--bisect") || starts_with(arg, "--glob=") ||
1794 !strcmp(arg, "--indexed-objects") ||
1795 starts_with(arg, "--exclude=") ||
1796 starts_with(arg, "--branches=") || starts_with(arg, "--tags=") ||
1797 starts_with(arg, "--remotes=") || starts_with(arg, "--no-walk="))
1799 unkv[(*unkc)++] = arg;
1800 return 1;
1803 if ((argcount = parse_long_opt("max-count", argv, &optarg))) {
1804 revs->max_count = atoi(optarg);
1805 revs->no_walk = 0;
1806 return argcount;
1807 } else if ((argcount = parse_long_opt("skip", argv, &optarg))) {
1808 revs->skip_count = atoi(optarg);
1809 return argcount;
1810 } else if ((*arg == '-') && isdigit(arg[1])) {
1811 /* accept -<digit>, like traditional "head" */
1812 if (strtol_i(arg + 1, 10, &revs->max_count) < 0 ||
1813 revs->max_count < 0)
1814 die("'%s': not a non-negative integer", arg + 1);
1815 revs->no_walk = 0;
1816 } else if (!strcmp(arg, "-n")) {
1817 if (argc <= 1)
1818 return error("-n requires an argument");
1819 revs->max_count = atoi(argv[1]);
1820 revs->no_walk = 0;
1821 return 2;
1822 } else if (skip_prefix(arg, "-n", &optarg)) {
1823 revs->max_count = atoi(optarg);
1824 revs->no_walk = 0;
1825 } else if ((argcount = parse_long_opt("max-age", argv, &optarg))) {
1826 revs->max_age = atoi(optarg);
1827 return argcount;
1828 } else if ((argcount = parse_long_opt("since", argv, &optarg))) {
1829 revs->max_age = approxidate(optarg);
1830 return argcount;
1831 } else if ((argcount = parse_long_opt("after", argv, &optarg))) {
1832 revs->max_age = approxidate(optarg);
1833 return argcount;
1834 } else if ((argcount = parse_long_opt("min-age", argv, &optarg))) {
1835 revs->min_age = atoi(optarg);
1836 return argcount;
1837 } else if ((argcount = parse_long_opt("before", argv, &optarg))) {
1838 revs->min_age = approxidate(optarg);
1839 return argcount;
1840 } else if ((argcount = parse_long_opt("until", argv, &optarg))) {
1841 revs->min_age = approxidate(optarg);
1842 return argcount;
1843 } else if (!strcmp(arg, "--first-parent")) {
1844 revs->first_parent_only = 1;
1845 } else if (!strcmp(arg, "--ancestry-path")) {
1846 revs->ancestry_path = 1;
1847 revs->simplify_history = 0;
1848 revs->limited = 1;
1849 } else if (!strcmp(arg, "-g") || !strcmp(arg, "--walk-reflogs")) {
1850 init_reflog_walk(&revs->reflog_info);
1851 } else if (!strcmp(arg, "--default")) {
1852 if (argc <= 1)
1853 return error("bad --default argument");
1854 revs->def = argv[1];
1855 return 2;
1856 } else if (!strcmp(arg, "--merge")) {
1857 revs->show_merge = 1;
1858 } else if (!strcmp(arg, "--topo-order")) {
1859 revs->sort_order = REV_SORT_IN_GRAPH_ORDER;
1860 revs->topo_order = 1;
1861 } else if (!strcmp(arg, "--simplify-merges")) {
1862 revs->simplify_merges = 1;
1863 revs->topo_order = 1;
1864 revs->rewrite_parents = 1;
1865 revs->simplify_history = 0;
1866 revs->limited = 1;
1867 } else if (!strcmp(arg, "--simplify-by-decoration")) {
1868 revs->simplify_merges = 1;
1869 revs->topo_order = 1;
1870 revs->rewrite_parents = 1;
1871 revs->simplify_history = 0;
1872 revs->simplify_by_decoration = 1;
1873 revs->limited = 1;
1874 revs->prune = 1;
1875 load_ref_decorations(NULL, DECORATE_SHORT_REFS);
1876 } else if (!strcmp(arg, "--date-order")) {
1877 revs->sort_order = REV_SORT_BY_COMMIT_DATE;
1878 revs->topo_order = 1;
1879 } else if (!strcmp(arg, "--author-date-order")) {
1880 revs->sort_order = REV_SORT_BY_AUTHOR_DATE;
1881 revs->topo_order = 1;
1882 } else if (!strcmp(arg, "--early-output")) {
1883 revs->early_output = 100;
1884 revs->topo_order = 1;
1885 } else if (skip_prefix(arg, "--early-output=", &optarg)) {
1886 if (strtoul_ui(optarg, 10, &revs->early_output) < 0)
1887 die("'%s': not a non-negative integer", optarg);
1888 revs->topo_order = 1;
1889 } else if (!strcmp(arg, "--parents")) {
1890 revs->rewrite_parents = 1;
1891 revs->print_parents = 1;
1892 } else if (!strcmp(arg, "--dense")) {
1893 revs->dense = 1;
1894 } else if (!strcmp(arg, "--sparse")) {
1895 revs->dense = 0;
1896 } else if (!strcmp(arg, "--in-commit-order")) {
1897 revs->tree_blobs_in_commit_order = 1;
1898 } else if (!strcmp(arg, "--remove-empty")) {
1899 revs->remove_empty_trees = 1;
1900 } else if (!strcmp(arg, "--merges")) {
1901 revs->min_parents = 2;
1902 } else if (!strcmp(arg, "--no-merges")) {
1903 revs->max_parents = 1;
1904 } else if (skip_prefix(arg, "--min-parents=", &optarg)) {
1905 revs->min_parents = atoi(optarg);
1906 } else if (!strcmp(arg, "--no-min-parents")) {
1907 revs->min_parents = 0;
1908 } else if (skip_prefix(arg, "--max-parents=", &optarg)) {
1909 revs->max_parents = atoi(optarg);
1910 } else if (!strcmp(arg, "--no-max-parents")) {
1911 revs->max_parents = -1;
1912 } else if (!strcmp(arg, "--boundary")) {
1913 revs->boundary = 1;
1914 } else if (!strcmp(arg, "--left-right")) {
1915 revs->left_right = 1;
1916 } else if (!strcmp(arg, "--left-only")) {
1917 if (revs->right_only)
1918 die("--left-only is incompatible with --right-only"
1919 " or --cherry");
1920 revs->left_only = 1;
1921 } else if (!strcmp(arg, "--right-only")) {
1922 if (revs->left_only)
1923 die("--right-only is incompatible with --left-only");
1924 revs->right_only = 1;
1925 } else if (!strcmp(arg, "--cherry")) {
1926 if (revs->left_only)
1927 die("--cherry is incompatible with --left-only");
1928 revs->cherry_mark = 1;
1929 revs->right_only = 1;
1930 revs->max_parents = 1;
1931 revs->limited = 1;
1932 } else if (!strcmp(arg, "--count")) {
1933 revs->count = 1;
1934 } else if (!strcmp(arg, "--cherry-mark")) {
1935 if (revs->cherry_pick)
1936 die("--cherry-mark is incompatible with --cherry-pick");
1937 revs->cherry_mark = 1;
1938 revs->limited = 1; /* needs limit_list() */
1939 } else if (!strcmp(arg, "--cherry-pick")) {
1940 if (revs->cherry_mark)
1941 die("--cherry-pick is incompatible with --cherry-mark");
1942 revs->cherry_pick = 1;
1943 revs->limited = 1;
1944 } else if (!strcmp(arg, "--objects")) {
1945 revs->tag_objects = 1;
1946 revs->tree_objects = 1;
1947 revs->blob_objects = 1;
1948 } else if (!strcmp(arg, "--objects-edge")) {
1949 revs->tag_objects = 1;
1950 revs->tree_objects = 1;
1951 revs->blob_objects = 1;
1952 revs->edge_hint = 1;
1953 } else if (!strcmp(arg, "--objects-edge-aggressive")) {
1954 revs->tag_objects = 1;
1955 revs->tree_objects = 1;
1956 revs->blob_objects = 1;
1957 revs->edge_hint = 1;
1958 revs->edge_hint_aggressive = 1;
1959 } else if (!strcmp(arg, "--verify-objects")) {
1960 revs->tag_objects = 1;
1961 revs->tree_objects = 1;
1962 revs->blob_objects = 1;
1963 revs->verify_objects = 1;
1964 } else if (!strcmp(arg, "--unpacked")) {
1965 revs->unpacked = 1;
1966 } else if (starts_with(arg, "--unpacked=")) {
1967 die("--unpacked=<packfile> no longer supported.");
1968 } else if (!strcmp(arg, "-r")) {
1969 revs->diff = 1;
1970 revs->diffopt.flags.recursive = 1;
1971 } else if (!strcmp(arg, "-t")) {
1972 revs->diff = 1;
1973 revs->diffopt.flags.recursive = 1;
1974 revs->diffopt.flags.tree_in_recursive = 1;
1975 } else if (!strcmp(arg, "-m")) {
1976 revs->ignore_merges = 0;
1977 } else if (!strcmp(arg, "-c")) {
1978 revs->diff = 1;
1979 revs->dense_combined_merges = 0;
1980 revs->combine_merges = 1;
1981 } else if (!strcmp(arg, "--cc")) {
1982 revs->diff = 1;
1983 revs->dense_combined_merges = 1;
1984 revs->combine_merges = 1;
1985 } else if (!strcmp(arg, "-v")) {
1986 revs->verbose_header = 1;
1987 } else if (!strcmp(arg, "--pretty")) {
1988 revs->verbose_header = 1;
1989 revs->pretty_given = 1;
1990 get_commit_format(NULL, revs);
1991 } else if (skip_prefix(arg, "--pretty=", &optarg) ||
1992 skip_prefix(arg, "--format=", &optarg)) {
1994 * Detached form ("--pretty X" as opposed to "--pretty=X")
1995 * not allowed, since the argument is optional.
1997 revs->verbose_header = 1;
1998 revs->pretty_given = 1;
1999 get_commit_format(optarg, revs);
2000 } else if (!strcmp(arg, "--expand-tabs")) {
2001 revs->expand_tabs_in_log = 8;
2002 } else if (!strcmp(arg, "--no-expand-tabs")) {
2003 revs->expand_tabs_in_log = 0;
2004 } else if (skip_prefix(arg, "--expand-tabs=", &arg)) {
2005 int val;
2006 if (strtol_i(arg, 10, &val) < 0 || val < 0)
2007 die("'%s': not a non-negative integer", arg);
2008 revs->expand_tabs_in_log = val;
2009 } else if (!strcmp(arg, "--show-notes") || !strcmp(arg, "--notes")) {
2010 revs->show_notes = 1;
2011 revs->show_notes_given = 1;
2012 revs->notes_opt.use_default_notes = 1;
2013 } else if (!strcmp(arg, "--show-signature")) {
2014 revs->show_signature = 1;
2015 } else if (!strcmp(arg, "--no-show-signature")) {
2016 revs->show_signature = 0;
2017 } else if (!strcmp(arg, "--show-linear-break")) {
2018 revs->break_bar = " ..........";
2019 revs->track_linear = 1;
2020 revs->track_first_time = 1;
2021 } else if (skip_prefix(arg, "--show-linear-break=", &optarg)) {
2022 revs->break_bar = xstrdup(optarg);
2023 revs->track_linear = 1;
2024 revs->track_first_time = 1;
2025 } else if (skip_prefix(arg, "--show-notes=", &optarg) ||
2026 skip_prefix(arg, "--notes=", &optarg)) {
2027 struct strbuf buf = STRBUF_INIT;
2028 revs->show_notes = 1;
2029 revs->show_notes_given = 1;
2030 if (starts_with(arg, "--show-notes=") &&
2031 revs->notes_opt.use_default_notes < 0)
2032 revs->notes_opt.use_default_notes = 1;
2033 strbuf_addstr(&buf, optarg);
2034 expand_notes_ref(&buf);
2035 string_list_append(&revs->notes_opt.extra_notes_refs,
2036 strbuf_detach(&buf, NULL));
2037 } else if (!strcmp(arg, "--no-notes")) {
2038 revs->show_notes = 0;
2039 revs->show_notes_given = 1;
2040 revs->notes_opt.use_default_notes = -1;
2041 /* we have been strdup'ing ourselves, so trick
2042 * string_list into free()ing strings */
2043 revs->notes_opt.extra_notes_refs.strdup_strings = 1;
2044 string_list_clear(&revs->notes_opt.extra_notes_refs, 0);
2045 revs->notes_opt.extra_notes_refs.strdup_strings = 0;
2046 } else if (!strcmp(arg, "--standard-notes")) {
2047 revs->show_notes_given = 1;
2048 revs->notes_opt.use_default_notes = 1;
2049 } else if (!strcmp(arg, "--no-standard-notes")) {
2050 revs->notes_opt.use_default_notes = 0;
2051 } else if (!strcmp(arg, "--oneline")) {
2052 revs->verbose_header = 1;
2053 get_commit_format("oneline", revs);
2054 revs->pretty_given = 1;
2055 revs->abbrev_commit = 1;
2056 } else if (!strcmp(arg, "--graph")) {
2057 revs->topo_order = 1;
2058 revs->rewrite_parents = 1;
2059 revs->graph = graph_init(revs);
2060 } else if (!strcmp(arg, "--root")) {
2061 revs->show_root_diff = 1;
2062 } else if (!strcmp(arg, "--no-commit-id")) {
2063 revs->no_commit_id = 1;
2064 } else if (!strcmp(arg, "--always")) {
2065 revs->always_show_header = 1;
2066 } else if (!strcmp(arg, "--no-abbrev")) {
2067 revs->abbrev = 0;
2068 } else if (!strcmp(arg, "--abbrev")) {
2069 revs->abbrev = DEFAULT_ABBREV;
2070 } else if (skip_prefix(arg, "--abbrev=", &optarg)) {
2071 revs->abbrev = strtoul(optarg, NULL, 10);
2072 if (revs->abbrev < MINIMUM_ABBREV)
2073 revs->abbrev = MINIMUM_ABBREV;
2074 else if (revs->abbrev > hexsz)
2075 revs->abbrev = hexsz;
2076 } else if (!strcmp(arg, "--abbrev-commit")) {
2077 revs->abbrev_commit = 1;
2078 revs->abbrev_commit_given = 1;
2079 } else if (!strcmp(arg, "--no-abbrev-commit")) {
2080 revs->abbrev_commit = 0;
2081 } else if (!strcmp(arg, "--full-diff")) {
2082 revs->diff = 1;
2083 revs->full_diff = 1;
2084 } else if (!strcmp(arg, "--full-history")) {
2085 revs->simplify_history = 0;
2086 } else if (!strcmp(arg, "--relative-date")) {
2087 revs->date_mode.type = DATE_RELATIVE;
2088 revs->date_mode_explicit = 1;
2089 } else if ((argcount = parse_long_opt("date", argv, &optarg))) {
2090 parse_date_format(optarg, &revs->date_mode);
2091 revs->date_mode_explicit = 1;
2092 return argcount;
2093 } else if (!strcmp(arg, "--log-size")) {
2094 revs->show_log_size = 1;
2097 * Grepping the commit log
2099 else if ((argcount = parse_long_opt("author", argv, &optarg))) {
2100 add_header_grep(revs, GREP_HEADER_AUTHOR, optarg);
2101 return argcount;
2102 } else if ((argcount = parse_long_opt("committer", argv, &optarg))) {
2103 add_header_grep(revs, GREP_HEADER_COMMITTER, optarg);
2104 return argcount;
2105 } else if ((argcount = parse_long_opt("grep-reflog", argv, &optarg))) {
2106 add_header_grep(revs, GREP_HEADER_REFLOG, optarg);
2107 return argcount;
2108 } else if ((argcount = parse_long_opt("grep", argv, &optarg))) {
2109 add_message_grep(revs, optarg);
2110 return argcount;
2111 } else if (!strcmp(arg, "--grep-debug")) {
2112 revs->grep_filter.debug = 1;
2113 } else if (!strcmp(arg, "--basic-regexp")) {
2114 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_BRE;
2115 } else if (!strcmp(arg, "--extended-regexp") || !strcmp(arg, "-E")) {
2116 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_ERE;
2117 } else if (!strcmp(arg, "--regexp-ignore-case") || !strcmp(arg, "-i")) {
2118 revs->grep_filter.ignore_case = 1;
2119 revs->diffopt.pickaxe_opts |= DIFF_PICKAXE_IGNORE_CASE;
2120 } else if (!strcmp(arg, "--fixed-strings") || !strcmp(arg, "-F")) {
2121 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_FIXED;
2122 } else if (!strcmp(arg, "--perl-regexp") || !strcmp(arg, "-P")) {
2123 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_PCRE;
2124 } else if (!strcmp(arg, "--all-match")) {
2125 revs->grep_filter.all_match = 1;
2126 } else if (!strcmp(arg, "--invert-grep")) {
2127 revs->invert_grep = 1;
2128 } else if ((argcount = parse_long_opt("encoding", argv, &optarg))) {
2129 if (strcmp(optarg, "none"))
2130 git_log_output_encoding = xstrdup(optarg);
2131 else
2132 git_log_output_encoding = "";
2133 return argcount;
2134 } else if (!strcmp(arg, "--reverse")) {
2135 revs->reverse ^= 1;
2136 } else if (!strcmp(arg, "--children")) {
2137 revs->children.name = "children";
2138 revs->limited = 1;
2139 } else if (!strcmp(arg, "--ignore-missing")) {
2140 revs->ignore_missing = 1;
2141 } else if (!strcmp(arg, "--exclude-promisor-objects")) {
2142 if (fetch_if_missing)
2143 BUG("exclude_promisor_objects can only be used when fetch_if_missing is 0");
2144 revs->exclude_promisor_objects = 1;
2145 } else {
2146 int opts = diff_opt_parse(&revs->diffopt, argv, argc, revs->prefix);
2147 if (!opts)
2148 unkv[(*unkc)++] = arg;
2149 return opts;
2151 if (revs->graph && revs->track_linear)
2152 die("--show-linear-break and --graph are incompatible");
2154 return 1;
2157 void parse_revision_opt(struct rev_info *revs, struct parse_opt_ctx_t *ctx,
2158 const struct option *options,
2159 const char * const usagestr[])
2161 int n = handle_revision_opt(revs, ctx->argc, ctx->argv,
2162 &ctx->cpidx, ctx->out);
2163 if (n <= 0) {
2164 error("unknown option `%s'", ctx->argv[0]);
2165 usage_with_options(usagestr, options);
2167 ctx->argv += n;
2168 ctx->argc -= n;
2171 static int for_each_bisect_ref(struct ref_store *refs, each_ref_fn fn,
2172 void *cb_data, const char *term)
2174 struct strbuf bisect_refs = STRBUF_INIT;
2175 int status;
2176 strbuf_addf(&bisect_refs, "refs/bisect/%s", term);
2177 status = refs_for_each_fullref_in(refs, bisect_refs.buf, fn, cb_data, 0);
2178 strbuf_release(&bisect_refs);
2179 return status;
2182 static int for_each_bad_bisect_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
2184 return for_each_bisect_ref(refs, fn, cb_data, term_bad);
2187 static int for_each_good_bisect_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
2189 return for_each_bisect_ref(refs, fn, cb_data, term_good);
2192 static int handle_revision_pseudo_opt(const char *submodule,
2193 struct rev_info *revs,
2194 int argc, const char **argv, int *flags)
2196 const char *arg = argv[0];
2197 const char *optarg;
2198 struct ref_store *refs;
2199 int argcount;
2201 if (submodule) {
2203 * We need some something like get_submodule_worktrees()
2204 * before we can go through all worktrees of a submodule,
2205 * .e.g with adding all HEADs from --all, which is not
2206 * supported right now, so stick to single worktree.
2208 if (!revs->single_worktree)
2209 BUG("--single-worktree cannot be used together with submodule");
2210 refs = get_submodule_ref_store(submodule);
2211 } else
2212 refs = get_main_ref_store(revs->repo);
2215 * NOTE!
2217 * Commands like "git shortlog" will not accept the options below
2218 * unless parse_revision_opt queues them (as opposed to erroring
2219 * out).
2221 * When implementing your new pseudo-option, remember to
2222 * register it in the list at the top of handle_revision_opt.
2224 if (!strcmp(arg, "--all")) {
2225 handle_refs(refs, revs, *flags, refs_for_each_ref);
2226 handle_refs(refs, revs, *flags, refs_head_ref);
2227 if (!revs->single_worktree) {
2228 struct all_refs_cb cb;
2230 init_all_refs_cb(&cb, revs, *flags);
2231 other_head_refs(handle_one_ref, &cb);
2233 clear_ref_exclusion(&revs->ref_excludes);
2234 } else if (!strcmp(arg, "--branches")) {
2235 handle_refs(refs, revs, *flags, refs_for_each_branch_ref);
2236 clear_ref_exclusion(&revs->ref_excludes);
2237 } else if (!strcmp(arg, "--bisect")) {
2238 read_bisect_terms(&term_bad, &term_good);
2239 handle_refs(refs, revs, *flags, for_each_bad_bisect_ref);
2240 handle_refs(refs, revs, *flags ^ (UNINTERESTING | BOTTOM),
2241 for_each_good_bisect_ref);
2242 revs->bisect = 1;
2243 } else if (!strcmp(arg, "--tags")) {
2244 handle_refs(refs, revs, *flags, refs_for_each_tag_ref);
2245 clear_ref_exclusion(&revs->ref_excludes);
2246 } else if (!strcmp(arg, "--remotes")) {
2247 handle_refs(refs, revs, *flags, refs_for_each_remote_ref);
2248 clear_ref_exclusion(&revs->ref_excludes);
2249 } else if ((argcount = parse_long_opt("glob", argv, &optarg))) {
2250 struct all_refs_cb cb;
2251 init_all_refs_cb(&cb, revs, *flags);
2252 for_each_glob_ref(handle_one_ref, optarg, &cb);
2253 clear_ref_exclusion(&revs->ref_excludes);
2254 return argcount;
2255 } else if ((argcount = parse_long_opt("exclude", argv, &optarg))) {
2256 add_ref_exclusion(&revs->ref_excludes, optarg);
2257 return argcount;
2258 } else if (skip_prefix(arg, "--branches=", &optarg)) {
2259 struct all_refs_cb cb;
2260 init_all_refs_cb(&cb, revs, *flags);
2261 for_each_glob_ref_in(handle_one_ref, optarg, "refs/heads/", &cb);
2262 clear_ref_exclusion(&revs->ref_excludes);
2263 } else if (skip_prefix(arg, "--tags=", &optarg)) {
2264 struct all_refs_cb cb;
2265 init_all_refs_cb(&cb, revs, *flags);
2266 for_each_glob_ref_in(handle_one_ref, optarg, "refs/tags/", &cb);
2267 clear_ref_exclusion(&revs->ref_excludes);
2268 } else if (skip_prefix(arg, "--remotes=", &optarg)) {
2269 struct all_refs_cb cb;
2270 init_all_refs_cb(&cb, revs, *flags);
2271 for_each_glob_ref_in(handle_one_ref, optarg, "refs/remotes/", &cb);
2272 clear_ref_exclusion(&revs->ref_excludes);
2273 } else if (!strcmp(arg, "--reflog")) {
2274 add_reflogs_to_pending(revs, *flags);
2275 } else if (!strcmp(arg, "--indexed-objects")) {
2276 add_index_objects_to_pending(revs, *flags);
2277 } else if (!strcmp(arg, "--not")) {
2278 *flags ^= UNINTERESTING | BOTTOM;
2279 } else if (!strcmp(arg, "--no-walk")) {
2280 revs->no_walk = REVISION_WALK_NO_WALK_SORTED;
2281 } else if (skip_prefix(arg, "--no-walk=", &optarg)) {
2283 * Detached form ("--no-walk X" as opposed to "--no-walk=X")
2284 * not allowed, since the argument is optional.
2286 if (!strcmp(optarg, "sorted"))
2287 revs->no_walk = REVISION_WALK_NO_WALK_SORTED;
2288 else if (!strcmp(optarg, "unsorted"))
2289 revs->no_walk = REVISION_WALK_NO_WALK_UNSORTED;
2290 else
2291 return error("invalid argument to --no-walk");
2292 } else if (!strcmp(arg, "--do-walk")) {
2293 revs->no_walk = 0;
2294 } else if (!strcmp(arg, "--single-worktree")) {
2295 revs->single_worktree = 1;
2296 } else {
2297 return 0;
2300 return 1;
2303 static void NORETURN diagnose_missing_default(const char *def)
2305 int flags;
2306 const char *refname;
2308 refname = resolve_ref_unsafe(def, 0, NULL, &flags);
2309 if (!refname || !(flags & REF_ISSYMREF) || (flags & REF_ISBROKEN))
2310 die(_("your current branch appears to be broken"));
2312 skip_prefix(refname, "refs/heads/", &refname);
2313 die(_("your current branch '%s' does not have any commits yet"),
2314 refname);
2318 * Parse revision information, filling in the "rev_info" structure,
2319 * and removing the used arguments from the argument list.
2321 * Returns the number of arguments left that weren't recognized
2322 * (which are also moved to the head of the argument list)
2324 int setup_revisions(int argc, const char **argv, struct rev_info *revs, struct setup_revision_opt *opt)
2326 int i, flags, left, seen_dashdash, got_rev_arg = 0, revarg_opt;
2327 struct argv_array prune_data = ARGV_ARRAY_INIT;
2328 const char *submodule = NULL;
2330 if (opt)
2331 submodule = opt->submodule;
2333 /* First, search for "--" */
2334 if (opt && opt->assume_dashdash) {
2335 seen_dashdash = 1;
2336 } else {
2337 seen_dashdash = 0;
2338 for (i = 1; i < argc; i++) {
2339 const char *arg = argv[i];
2340 if (strcmp(arg, "--"))
2341 continue;
2342 argv[i] = NULL;
2343 argc = i;
2344 if (argv[i + 1])
2345 argv_array_pushv(&prune_data, argv + i + 1);
2346 seen_dashdash = 1;
2347 break;
2351 /* Second, deal with arguments and options */
2352 flags = 0;
2353 revarg_opt = opt ? opt->revarg_opt : 0;
2354 if (seen_dashdash)
2355 revarg_opt |= REVARG_CANNOT_BE_FILENAME;
2356 for (left = i = 1; i < argc; i++) {
2357 const char *arg = argv[i];
2358 if (*arg == '-') {
2359 int opts;
2361 opts = handle_revision_pseudo_opt(submodule,
2362 revs, argc - i, argv + i,
2363 &flags);
2364 if (opts > 0) {
2365 i += opts - 1;
2366 continue;
2369 if (!strcmp(arg, "--stdin")) {
2370 if (revs->disable_stdin) {
2371 argv[left++] = arg;
2372 continue;
2374 if (revs->read_from_stdin++)
2375 die("--stdin given twice?");
2376 read_revisions_from_stdin(revs, &prune_data);
2377 continue;
2380 opts = handle_revision_opt(revs, argc - i, argv + i, &left, argv);
2381 if (opts > 0) {
2382 i += opts - 1;
2383 continue;
2385 if (opts < 0)
2386 exit(128);
2387 continue;
2391 if (handle_revision_arg(arg, revs, flags, revarg_opt)) {
2392 int j;
2393 if (seen_dashdash || *arg == '^')
2394 die("bad revision '%s'", arg);
2396 /* If we didn't have a "--":
2397 * (1) all filenames must exist;
2398 * (2) all rev-args must not be interpretable
2399 * as a valid filename.
2400 * but the latter we have checked in the main loop.
2402 for (j = i; j < argc; j++)
2403 verify_filename(revs->prefix, argv[j], j == i);
2405 argv_array_pushv(&prune_data, argv + i);
2406 break;
2408 else
2409 got_rev_arg = 1;
2412 if (prune_data.argc) {
2414 * If we need to introduce the magic "a lone ':' means no
2415 * pathspec whatsoever", here is the place to do so.
2417 * if (prune_data.nr == 1 && !strcmp(prune_data[0], ":")) {
2418 * prune_data.nr = 0;
2419 * prune_data.alloc = 0;
2420 * free(prune_data.path);
2421 * prune_data.path = NULL;
2422 * } else {
2423 * terminate prune_data.alloc with NULL and
2424 * call init_pathspec() to set revs->prune_data here.
2427 parse_pathspec(&revs->prune_data, 0, 0,
2428 revs->prefix, prune_data.argv);
2430 argv_array_clear(&prune_data);
2432 if (revs->def == NULL)
2433 revs->def = opt ? opt->def : NULL;
2434 if (opt && opt->tweak)
2435 opt->tweak(revs, opt);
2436 if (revs->show_merge)
2437 prepare_show_merge(revs);
2438 if (revs->def && !revs->pending.nr && !revs->rev_input_given && !got_rev_arg) {
2439 struct object_id oid;
2440 struct object *object;
2441 struct object_context oc;
2442 if (get_oid_with_context(revs->def, 0, &oid, &oc))
2443 diagnose_missing_default(revs->def);
2444 object = get_reference(revs, revs->def, &oid, 0);
2445 add_pending_object_with_mode(revs, object, revs->def, oc.mode);
2448 /* Did the user ask for any diff output? Run the diff! */
2449 if (revs->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT)
2450 revs->diff = 1;
2452 /* Pickaxe, diff-filter and rename following need diffs */
2453 if ((revs->diffopt.pickaxe_opts & DIFF_PICKAXE_KINDS_MASK) ||
2454 revs->diffopt.filter ||
2455 revs->diffopt.flags.follow_renames)
2456 revs->diff = 1;
2458 if (revs->diffopt.objfind)
2459 revs->simplify_history = 0;
2461 if (revs->topo_order)
2462 revs->limited = 1;
2464 if (revs->prune_data.nr) {
2465 copy_pathspec(&revs->pruning.pathspec, &revs->prune_data);
2466 /* Can't prune commits with rename following: the paths change.. */
2467 if (!revs->diffopt.flags.follow_renames)
2468 revs->prune = 1;
2469 if (!revs->full_diff)
2470 copy_pathspec(&revs->diffopt.pathspec,
2471 &revs->prune_data);
2473 if (revs->combine_merges)
2474 revs->ignore_merges = 0;
2475 revs->diffopt.abbrev = revs->abbrev;
2477 if (revs->line_level_traverse) {
2478 revs->limited = 1;
2479 revs->topo_order = 1;
2482 diff_setup_done(&revs->diffopt);
2484 grep_commit_pattern_type(GREP_PATTERN_TYPE_UNSPECIFIED,
2485 &revs->grep_filter);
2486 compile_grep_patterns(&revs->grep_filter);
2488 if (revs->reverse && revs->reflog_info)
2489 die("cannot combine --reverse with --walk-reflogs");
2490 if (revs->reflog_info && revs->limited)
2491 die("cannot combine --walk-reflogs with history-limiting options");
2492 if (revs->rewrite_parents && revs->children.name)
2493 die("cannot combine --parents and --children");
2496 * Limitations on the graph functionality
2498 if (revs->reverse && revs->graph)
2499 die("cannot combine --reverse with --graph");
2501 if (revs->reflog_info && revs->graph)
2502 die("cannot combine --walk-reflogs with --graph");
2503 if (revs->no_walk && revs->graph)
2504 die("cannot combine --no-walk with --graph");
2505 if (!revs->reflog_info && revs->grep_filter.use_reflog_filter)
2506 die("cannot use --grep-reflog without --walk-reflogs");
2508 if (revs->first_parent_only && revs->bisect)
2509 die(_("--first-parent is incompatible with --bisect"));
2511 if (revs->expand_tabs_in_log < 0)
2512 revs->expand_tabs_in_log = revs->expand_tabs_in_log_default;
2514 return left;
2517 static void add_child(struct rev_info *revs, struct commit *parent, struct commit *child)
2519 struct commit_list *l = xcalloc(1, sizeof(*l));
2521 l->item = child;
2522 l->next = add_decoration(&revs->children, &parent->object, l);
2525 static int remove_duplicate_parents(struct rev_info *revs, struct commit *commit)
2527 struct treesame_state *ts = lookup_decoration(&revs->treesame, &commit->object);
2528 struct commit_list **pp, *p;
2529 int surviving_parents;
2531 /* Examine existing parents while marking ones we have seen... */
2532 pp = &commit->parents;
2533 surviving_parents = 0;
2534 while ((p = *pp) != NULL) {
2535 struct commit *parent = p->item;
2536 if (parent->object.flags & TMP_MARK) {
2537 *pp = p->next;
2538 if (ts)
2539 compact_treesame(revs, commit, surviving_parents);
2540 continue;
2542 parent->object.flags |= TMP_MARK;
2543 surviving_parents++;
2544 pp = &p->next;
2546 /* clear the temporary mark */
2547 for (p = commit->parents; p; p = p->next) {
2548 p->item->object.flags &= ~TMP_MARK;
2550 /* no update_treesame() - removing duplicates can't affect TREESAME */
2551 return surviving_parents;
2554 struct merge_simplify_state {
2555 struct commit *simplified;
2558 static struct merge_simplify_state *locate_simplify_state(struct rev_info *revs, struct commit *commit)
2560 struct merge_simplify_state *st;
2562 st = lookup_decoration(&revs->merge_simplification, &commit->object);
2563 if (!st) {
2564 st = xcalloc(1, sizeof(*st));
2565 add_decoration(&revs->merge_simplification, &commit->object, st);
2567 return st;
2570 static int mark_redundant_parents(struct rev_info *revs, struct commit *commit)
2572 struct commit_list *h = reduce_heads(commit->parents);
2573 int i = 0, marked = 0;
2574 struct commit_list *po, *pn;
2576 /* Want these for sanity-checking only */
2577 int orig_cnt = commit_list_count(commit->parents);
2578 int cnt = commit_list_count(h);
2581 * Not ready to remove items yet, just mark them for now, based
2582 * on the output of reduce_heads(). reduce_heads outputs the reduced
2583 * set in its original order, so this isn't too hard.
2585 po = commit->parents;
2586 pn = h;
2587 while (po) {
2588 if (pn && po->item == pn->item) {
2589 pn = pn->next;
2590 i++;
2591 } else {
2592 po->item->object.flags |= TMP_MARK;
2593 marked++;
2595 po=po->next;
2598 if (i != cnt || cnt+marked != orig_cnt)
2599 die("mark_redundant_parents %d %d %d %d", orig_cnt, cnt, i, marked);
2601 free_commit_list(h);
2603 return marked;
2606 static int mark_treesame_root_parents(struct rev_info *revs, struct commit *commit)
2608 struct commit_list *p;
2609 int marked = 0;
2611 for (p = commit->parents; p; p = p->next) {
2612 struct commit *parent = p->item;
2613 if (!parent->parents && (parent->object.flags & TREESAME)) {
2614 parent->object.flags |= TMP_MARK;
2615 marked++;
2619 return marked;
2623 * Awkward naming - this means one parent we are TREESAME to.
2624 * cf mark_treesame_root_parents: root parents that are TREESAME (to an
2625 * empty tree). Better name suggestions?
2627 static int leave_one_treesame_to_parent(struct rev_info *revs, struct commit *commit)
2629 struct treesame_state *ts = lookup_decoration(&revs->treesame, &commit->object);
2630 struct commit *unmarked = NULL, *marked = NULL;
2631 struct commit_list *p;
2632 unsigned n;
2634 for (p = commit->parents, n = 0; p; p = p->next, n++) {
2635 if (ts->treesame[n]) {
2636 if (p->item->object.flags & TMP_MARK) {
2637 if (!marked)
2638 marked = p->item;
2639 } else {
2640 if (!unmarked) {
2641 unmarked = p->item;
2642 break;
2649 * If we are TREESAME to a marked-for-deletion parent, but not to any
2650 * unmarked parents, unmark the first TREESAME parent. This is the
2651 * parent that the default simplify_history==1 scan would have followed,
2652 * and it doesn't make sense to omit that path when asking for a
2653 * simplified full history. Retaining it improves the chances of
2654 * understanding odd missed merges that took an old version of a file.
2656 * Example:
2658 * I--------*X A modified the file, but mainline merge X used
2659 * \ / "-s ours", so took the version from I. X is
2660 * `-*A--' TREESAME to I and !TREESAME to A.
2662 * Default log from X would produce "I". Without this check,
2663 * --full-history --simplify-merges would produce "I-A-X", showing
2664 * the merge commit X and that it changed A, but not making clear that
2665 * it had just taken the I version. With this check, the topology above
2666 * is retained.
2668 * Note that it is possible that the simplification chooses a different
2669 * TREESAME parent from the default, in which case this test doesn't
2670 * activate, and we _do_ drop the default parent. Example:
2672 * I------X A modified the file, but it was reverted in B,
2673 * \ / meaning mainline merge X is TREESAME to both
2674 * *A-*B parents.
2676 * Default log would produce "I" by following the first parent;
2677 * --full-history --simplify-merges will produce "I-A-B". But this is a
2678 * reasonable result - it presents a logical full history leading from
2679 * I to X, and X is not an important merge.
2681 if (!unmarked && marked) {
2682 marked->object.flags &= ~TMP_MARK;
2683 return 1;
2686 return 0;
2689 static int remove_marked_parents(struct rev_info *revs, struct commit *commit)
2691 struct commit_list **pp, *p;
2692 int nth_parent, removed = 0;
2694 pp = &commit->parents;
2695 nth_parent = 0;
2696 while ((p = *pp) != NULL) {
2697 struct commit *parent = p->item;
2698 if (parent->object.flags & TMP_MARK) {
2699 parent->object.flags &= ~TMP_MARK;
2700 *pp = p->next;
2701 free(p);
2702 removed++;
2703 compact_treesame(revs, commit, nth_parent);
2704 continue;
2706 pp = &p->next;
2707 nth_parent++;
2710 /* Removing parents can only increase TREESAMEness */
2711 if (removed && !(commit->object.flags & TREESAME))
2712 update_treesame(revs, commit);
2714 return nth_parent;
2717 static struct commit_list **simplify_one(struct rev_info *revs, struct commit *commit, struct commit_list **tail)
2719 struct commit_list *p;
2720 struct commit *parent;
2721 struct merge_simplify_state *st, *pst;
2722 int cnt;
2724 st = locate_simplify_state(revs, commit);
2727 * Have we handled this one?
2729 if (st->simplified)
2730 return tail;
2733 * An UNINTERESTING commit simplifies to itself, so does a
2734 * root commit. We do not rewrite parents of such commit
2735 * anyway.
2737 if ((commit->object.flags & UNINTERESTING) || !commit->parents) {
2738 st->simplified = commit;
2739 return tail;
2743 * Do we know what commit all of our parents that matter
2744 * should be rewritten to? Otherwise we are not ready to
2745 * rewrite this one yet.
2747 for (cnt = 0, p = commit->parents; p; p = p->next) {
2748 pst = locate_simplify_state(revs, p->item);
2749 if (!pst->simplified) {
2750 tail = &commit_list_insert(p->item, tail)->next;
2751 cnt++;
2753 if (revs->first_parent_only)
2754 break;
2756 if (cnt) {
2757 tail = &commit_list_insert(commit, tail)->next;
2758 return tail;
2762 * Rewrite our list of parents. Note that this cannot
2763 * affect our TREESAME flags in any way - a commit is
2764 * always TREESAME to its simplification.
2766 for (p = commit->parents; p; p = p->next) {
2767 pst = locate_simplify_state(revs, p->item);
2768 p->item = pst->simplified;
2769 if (revs->first_parent_only)
2770 break;
2773 if (revs->first_parent_only)
2774 cnt = 1;
2775 else
2776 cnt = remove_duplicate_parents(revs, commit);
2779 * It is possible that we are a merge and one side branch
2780 * does not have any commit that touches the given paths;
2781 * in such a case, the immediate parent from that branch
2782 * will be rewritten to be the merge base.
2784 * o----X X: the commit we are looking at;
2785 * / / o: a commit that touches the paths;
2786 * ---o----'
2788 * Further, a merge of an independent branch that doesn't
2789 * touch the path will reduce to a treesame root parent:
2791 * ----o----X X: the commit we are looking at;
2792 * / o: a commit that touches the paths;
2793 * r r: a root commit not touching the paths
2795 * Detect and simplify both cases.
2797 if (1 < cnt) {
2798 int marked = mark_redundant_parents(revs, commit);
2799 marked += mark_treesame_root_parents(revs, commit);
2800 if (marked)
2801 marked -= leave_one_treesame_to_parent(revs, commit);
2802 if (marked)
2803 cnt = remove_marked_parents(revs, commit);
2807 * A commit simplifies to itself if it is a root, if it is
2808 * UNINTERESTING, if it touches the given paths, or if it is a
2809 * merge and its parents don't simplify to one relevant commit
2810 * (the first two cases are already handled at the beginning of
2811 * this function).
2813 * Otherwise, it simplifies to what its sole relevant parent
2814 * simplifies to.
2816 if (!cnt ||
2817 (commit->object.flags & UNINTERESTING) ||
2818 !(commit->object.flags & TREESAME) ||
2819 (parent = one_relevant_parent(revs, commit->parents)) == NULL)
2820 st->simplified = commit;
2821 else {
2822 pst = locate_simplify_state(revs, parent);
2823 st->simplified = pst->simplified;
2825 return tail;
2828 static void simplify_merges(struct rev_info *revs)
2830 struct commit_list *list, *next;
2831 struct commit_list *yet_to_do, **tail;
2832 struct commit *commit;
2834 if (!revs->prune)
2835 return;
2837 /* feed the list reversed */
2838 yet_to_do = NULL;
2839 for (list = revs->commits; list; list = next) {
2840 commit = list->item;
2841 next = list->next;
2843 * Do not free(list) here yet; the original list
2844 * is used later in this function.
2846 commit_list_insert(commit, &yet_to_do);
2848 while (yet_to_do) {
2849 list = yet_to_do;
2850 yet_to_do = NULL;
2851 tail = &yet_to_do;
2852 while (list) {
2853 commit = pop_commit(&list);
2854 tail = simplify_one(revs, commit, tail);
2858 /* clean up the result, removing the simplified ones */
2859 list = revs->commits;
2860 revs->commits = NULL;
2861 tail = &revs->commits;
2862 while (list) {
2863 struct merge_simplify_state *st;
2865 commit = pop_commit(&list);
2866 st = locate_simplify_state(revs, commit);
2867 if (st->simplified == commit)
2868 tail = &commit_list_insert(commit, tail)->next;
2872 static void set_children(struct rev_info *revs)
2874 struct commit_list *l;
2875 for (l = revs->commits; l; l = l->next) {
2876 struct commit *commit = l->item;
2877 struct commit_list *p;
2879 for (p = commit->parents; p; p = p->next)
2880 add_child(revs, p->item, commit);
2884 void reset_revision_walk(void)
2886 clear_object_flags(SEEN | ADDED | SHOWN);
2889 static int mark_uninteresting(const struct object_id *oid,
2890 struct packed_git *pack,
2891 uint32_t pos,
2892 void *cb)
2894 struct rev_info *revs = cb;
2895 struct object *o = parse_object(revs->repo, oid);
2896 o->flags |= UNINTERESTING | SEEN;
2897 return 0;
2900 int prepare_revision_walk(struct rev_info *revs)
2902 int i;
2903 struct object_array old_pending;
2904 struct commit_list **next = &revs->commits;
2906 memcpy(&old_pending, &revs->pending, sizeof(old_pending));
2907 revs->pending.nr = 0;
2908 revs->pending.alloc = 0;
2909 revs->pending.objects = NULL;
2910 for (i = 0; i < old_pending.nr; i++) {
2911 struct object_array_entry *e = old_pending.objects + i;
2912 struct commit *commit = handle_commit(revs, e);
2913 if (commit) {
2914 if (!(commit->object.flags & SEEN)) {
2915 commit->object.flags |= SEEN;
2916 next = commit_list_append(commit, next);
2920 object_array_clear(&old_pending);
2922 /* Signal whether we need per-parent treesame decoration */
2923 if (revs->simplify_merges ||
2924 (revs->limited && limiting_can_increase_treesame(revs)))
2925 revs->treesame.name = "treesame";
2927 if (revs->exclude_promisor_objects) {
2928 for_each_packed_object(mark_uninteresting, revs,
2929 FOR_EACH_OBJECT_PROMISOR_ONLY);
2932 if (revs->no_walk != REVISION_WALK_NO_WALK_UNSORTED)
2933 commit_list_sort_by_date(&revs->commits);
2934 if (revs->no_walk)
2935 return 0;
2936 if (revs->limited)
2937 if (limit_list(revs) < 0)
2938 return -1;
2939 if (revs->topo_order)
2940 sort_in_topological_order(&revs->commits, revs->sort_order);
2941 if (revs->line_level_traverse)
2942 line_log_filter(revs);
2943 if (revs->simplify_merges)
2944 simplify_merges(revs);
2945 if (revs->children.name)
2946 set_children(revs);
2947 return 0;
2950 static enum rewrite_result rewrite_one(struct rev_info *revs, struct commit **pp)
2952 struct commit_list *cache = NULL;
2954 for (;;) {
2955 struct commit *p = *pp;
2956 if (!revs->limited)
2957 if (add_parents_to_list(revs, p, &revs->commits, &cache) < 0)
2958 return rewrite_one_error;
2959 if (p->object.flags & UNINTERESTING)
2960 return rewrite_one_ok;
2961 if (!(p->object.flags & TREESAME))
2962 return rewrite_one_ok;
2963 if (!p->parents)
2964 return rewrite_one_noparents;
2965 if ((p = one_relevant_parent(revs, p->parents)) == NULL)
2966 return rewrite_one_ok;
2967 *pp = p;
2971 int rewrite_parents(struct rev_info *revs, struct commit *commit,
2972 rewrite_parent_fn_t rewrite_parent)
2974 struct commit_list **pp = &commit->parents;
2975 while (*pp) {
2976 struct commit_list *parent = *pp;
2977 switch (rewrite_parent(revs, &parent->item)) {
2978 case rewrite_one_ok:
2979 break;
2980 case rewrite_one_noparents:
2981 *pp = parent->next;
2982 continue;
2983 case rewrite_one_error:
2984 return -1;
2986 pp = &parent->next;
2988 remove_duplicate_parents(revs, commit);
2989 return 0;
2992 static int commit_rewrite_person(struct strbuf *buf, const char *what, struct string_list *mailmap)
2994 char *person, *endp;
2995 size_t len, namelen, maillen;
2996 const char *name;
2997 const char *mail;
2998 struct ident_split ident;
3000 person = strstr(buf->buf, what);
3001 if (!person)
3002 return 0;
3004 person += strlen(what);
3005 endp = strchr(person, '\n');
3006 if (!endp)
3007 return 0;
3009 len = endp - person;
3011 if (split_ident_line(&ident, person, len))
3012 return 0;
3014 mail = ident.mail_begin;
3015 maillen = ident.mail_end - ident.mail_begin;
3016 name = ident.name_begin;
3017 namelen = ident.name_end - ident.name_begin;
3019 if (map_user(mailmap, &mail, &maillen, &name, &namelen)) {
3020 struct strbuf namemail = STRBUF_INIT;
3022 strbuf_addf(&namemail, "%.*s <%.*s>",
3023 (int)namelen, name, (int)maillen, mail);
3025 strbuf_splice(buf, ident.name_begin - buf->buf,
3026 ident.mail_end - ident.name_begin + 1,
3027 namemail.buf, namemail.len);
3029 strbuf_release(&namemail);
3031 return 1;
3034 return 0;
3037 static int commit_match(struct commit *commit, struct rev_info *opt)
3039 int retval;
3040 const char *encoding;
3041 const char *message;
3042 struct strbuf buf = STRBUF_INIT;
3044 if (!opt->grep_filter.pattern_list && !opt->grep_filter.header_list)
3045 return 1;
3047 /* Prepend "fake" headers as needed */
3048 if (opt->grep_filter.use_reflog_filter) {
3049 strbuf_addstr(&buf, "reflog ");
3050 get_reflog_message(&buf, opt->reflog_info);
3051 strbuf_addch(&buf, '\n');
3055 * We grep in the user's output encoding, under the assumption that it
3056 * is the encoding they are most likely to write their grep pattern
3057 * for. In addition, it means we will match the "notes" encoding below,
3058 * so we will not end up with a buffer that has two different encodings
3059 * in it.
3061 encoding = get_log_output_encoding();
3062 message = logmsg_reencode(commit, NULL, encoding);
3064 /* Copy the commit to temporary if we are using "fake" headers */
3065 if (buf.len)
3066 strbuf_addstr(&buf, message);
3068 if (opt->grep_filter.header_list && opt->mailmap) {
3069 if (!buf.len)
3070 strbuf_addstr(&buf, message);
3072 commit_rewrite_person(&buf, "\nauthor ", opt->mailmap);
3073 commit_rewrite_person(&buf, "\ncommitter ", opt->mailmap);
3076 /* Append "fake" message parts as needed */
3077 if (opt->show_notes) {
3078 if (!buf.len)
3079 strbuf_addstr(&buf, message);
3080 format_display_notes(&commit->object.oid, &buf, encoding, 1);
3084 * Find either in the original commit message, or in the temporary.
3085 * Note that we cast away the constness of "message" here. It is
3086 * const because it may come from the cached commit buffer. That's OK,
3087 * because we know that it is modifiable heap memory, and that while
3088 * grep_buffer may modify it for speed, it will restore any
3089 * changes before returning.
3091 if (buf.len)
3092 retval = grep_buffer(&opt->grep_filter, buf.buf, buf.len);
3093 else
3094 retval = grep_buffer(&opt->grep_filter,
3095 (char *)message, strlen(message));
3096 strbuf_release(&buf);
3097 unuse_commit_buffer(commit, message);
3098 return opt->invert_grep ? !retval : retval;
3101 static inline int want_ancestry(const struct rev_info *revs)
3103 return (revs->rewrite_parents || revs->children.name);
3107 * Return a timestamp to be used for --since/--until comparisons for this
3108 * commit, based on the revision options.
3110 static timestamp_t comparison_date(const struct rev_info *revs,
3111 struct commit *commit)
3113 return revs->reflog_info ?
3114 get_reflog_timestamp(revs->reflog_info) :
3115 commit->date;
3118 enum commit_action get_commit_action(struct rev_info *revs, struct commit *commit)
3120 if (commit->object.flags & SHOWN)
3121 return commit_ignore;
3122 if (revs->unpacked && has_object_pack(&commit->object.oid))
3123 return commit_ignore;
3124 if (commit->object.flags & UNINTERESTING)
3125 return commit_ignore;
3126 if (revs->min_age != -1 &&
3127 comparison_date(revs, commit) > revs->min_age)
3128 return commit_ignore;
3129 if (revs->min_parents || (revs->max_parents >= 0)) {
3130 int n = commit_list_count(commit->parents);
3131 if ((n < revs->min_parents) ||
3132 ((revs->max_parents >= 0) && (n > revs->max_parents)))
3133 return commit_ignore;
3135 if (!commit_match(commit, revs))
3136 return commit_ignore;
3137 if (revs->prune && revs->dense) {
3138 /* Commit without changes? */
3139 if (commit->object.flags & TREESAME) {
3140 int n;
3141 struct commit_list *p;
3142 /* drop merges unless we want parenthood */
3143 if (!want_ancestry(revs))
3144 return commit_ignore;
3146 * If we want ancestry, then need to keep any merges
3147 * between relevant commits to tie together topology.
3148 * For consistency with TREESAME and simplification
3149 * use "relevant" here rather than just INTERESTING,
3150 * to treat bottom commit(s) as part of the topology.
3152 for (n = 0, p = commit->parents; p; p = p->next)
3153 if (relevant_commit(p->item))
3154 if (++n >= 2)
3155 return commit_show;
3156 return commit_ignore;
3159 return commit_show;
3162 define_commit_slab(saved_parents, struct commit_list *);
3164 #define EMPTY_PARENT_LIST ((struct commit_list *)-1)
3167 * You may only call save_parents() once per commit (this is checked
3168 * for non-root commits).
3170 static void save_parents(struct rev_info *revs, struct commit *commit)
3172 struct commit_list **pp;
3174 if (!revs->saved_parents_slab) {
3175 revs->saved_parents_slab = xmalloc(sizeof(struct saved_parents));
3176 init_saved_parents(revs->saved_parents_slab);
3179 pp = saved_parents_at(revs->saved_parents_slab, commit);
3182 * When walking with reflogs, we may visit the same commit
3183 * several times: once for each appearance in the reflog.
3185 * In this case, save_parents() will be called multiple times.
3186 * We want to keep only the first set of parents. We need to
3187 * store a sentinel value for an empty (i.e., NULL) parent
3188 * list to distinguish it from a not-yet-saved list, however.
3190 if (*pp)
3191 return;
3192 if (commit->parents)
3193 *pp = copy_commit_list(commit->parents);
3194 else
3195 *pp = EMPTY_PARENT_LIST;
3198 static void free_saved_parents(struct rev_info *revs)
3200 if (revs->saved_parents_slab)
3201 clear_saved_parents(revs->saved_parents_slab);
3204 struct commit_list *get_saved_parents(struct rev_info *revs, const struct commit *commit)
3206 struct commit_list *parents;
3208 if (!revs->saved_parents_slab)
3209 return commit->parents;
3211 parents = *saved_parents_at(revs->saved_parents_slab, commit);
3212 if (parents == EMPTY_PARENT_LIST)
3213 return NULL;
3214 return parents;
3217 enum commit_action simplify_commit(struct rev_info *revs, struct commit *commit)
3219 enum commit_action action = get_commit_action(revs, commit);
3221 if (action == commit_show &&
3222 revs->prune && revs->dense && want_ancestry(revs)) {
3224 * --full-diff on simplified parents is no good: it
3225 * will show spurious changes from the commits that
3226 * were elided. So we save the parents on the side
3227 * when --full-diff is in effect.
3229 if (revs->full_diff)
3230 save_parents(revs, commit);
3231 if (rewrite_parents(revs, commit, rewrite_one) < 0)
3232 return commit_error;
3234 return action;
3237 static void track_linear(struct rev_info *revs, struct commit *commit)
3239 if (revs->track_first_time) {
3240 revs->linear = 1;
3241 revs->track_first_time = 0;
3242 } else {
3243 struct commit_list *p;
3244 for (p = revs->previous_parents; p; p = p->next)
3245 if (p->item == NULL || /* first commit */
3246 oideq(&p->item->object.oid, &commit->object.oid))
3247 break;
3248 revs->linear = p != NULL;
3250 if (revs->reverse) {
3251 if (revs->linear)
3252 commit->object.flags |= TRACK_LINEAR;
3254 free_commit_list(revs->previous_parents);
3255 revs->previous_parents = copy_commit_list(commit->parents);
3258 static struct commit *get_revision_1(struct rev_info *revs)
3260 while (1) {
3261 struct commit *commit;
3263 if (revs->reflog_info)
3264 commit = next_reflog_entry(revs->reflog_info);
3265 else
3266 commit = pop_commit(&revs->commits);
3268 if (!commit)
3269 return NULL;
3271 if (revs->reflog_info)
3272 commit->object.flags &= ~(ADDED | SEEN | SHOWN);
3275 * If we haven't done the list limiting, we need to look at
3276 * the parents here. We also need to do the date-based limiting
3277 * that we'd otherwise have done in limit_list().
3279 if (!revs->limited) {
3280 if (revs->max_age != -1 &&
3281 comparison_date(revs, commit) < revs->max_age)
3282 continue;
3284 if (revs->reflog_info)
3285 try_to_simplify_commit(revs, commit);
3286 else if (add_parents_to_list(revs, commit, &revs->commits, NULL) < 0) {
3287 if (!revs->ignore_missing_links)
3288 die("Failed to traverse parents of commit %s",
3289 oid_to_hex(&commit->object.oid));
3293 switch (simplify_commit(revs, commit)) {
3294 case commit_ignore:
3295 continue;
3296 case commit_error:
3297 die("Failed to simplify parents of commit %s",
3298 oid_to_hex(&commit->object.oid));
3299 default:
3300 if (revs->track_linear)
3301 track_linear(revs, commit);
3302 return commit;
3308 * Return true for entries that have not yet been shown. (This is an
3309 * object_array_each_func_t.)
3311 static int entry_unshown(struct object_array_entry *entry, void *cb_data_unused)
3313 return !(entry->item->flags & SHOWN);
3317 * If array is on the verge of a realloc, garbage-collect any entries
3318 * that have already been shown to try to free up some space.
3320 static void gc_boundary(struct object_array *array)
3322 if (array->nr == array->alloc)
3323 object_array_filter(array, entry_unshown, NULL);
3326 static void create_boundary_commit_list(struct rev_info *revs)
3328 unsigned i;
3329 struct commit *c;
3330 struct object_array *array = &revs->boundary_commits;
3331 struct object_array_entry *objects = array->objects;
3334 * If revs->commits is non-NULL at this point, an error occurred in
3335 * get_revision_1(). Ignore the error and continue printing the
3336 * boundary commits anyway. (This is what the code has always
3337 * done.)
3339 if (revs->commits) {
3340 free_commit_list(revs->commits);
3341 revs->commits = NULL;
3345 * Put all of the actual boundary commits from revs->boundary_commits
3346 * into revs->commits
3348 for (i = 0; i < array->nr; i++) {
3349 c = (struct commit *)(objects[i].item);
3350 if (!c)
3351 continue;
3352 if (!(c->object.flags & CHILD_SHOWN))
3353 continue;
3354 if (c->object.flags & (SHOWN | BOUNDARY))
3355 continue;
3356 c->object.flags |= BOUNDARY;
3357 commit_list_insert(c, &revs->commits);
3361 * If revs->topo_order is set, sort the boundary commits
3362 * in topological order
3364 sort_in_topological_order(&revs->commits, revs->sort_order);
3367 static struct commit *get_revision_internal(struct rev_info *revs)
3369 struct commit *c = NULL;
3370 struct commit_list *l;
3372 if (revs->boundary == 2) {
3374 * All of the normal commits have already been returned,
3375 * and we are now returning boundary commits.
3376 * create_boundary_commit_list() has populated
3377 * revs->commits with the remaining commits to return.
3379 c = pop_commit(&revs->commits);
3380 if (c)
3381 c->object.flags |= SHOWN;
3382 return c;
3386 * If our max_count counter has reached zero, then we are done. We
3387 * don't simply return NULL because we still might need to show
3388 * boundary commits. But we want to avoid calling get_revision_1, which
3389 * might do a considerable amount of work finding the next commit only
3390 * for us to throw it away.
3392 * If it is non-zero, then either we don't have a max_count at all
3393 * (-1), or it is still counting, in which case we decrement.
3395 if (revs->max_count) {
3396 c = get_revision_1(revs);
3397 if (c) {
3398 while (revs->skip_count > 0) {
3399 revs->skip_count--;
3400 c = get_revision_1(revs);
3401 if (!c)
3402 break;
3406 if (revs->max_count > 0)
3407 revs->max_count--;
3410 if (c)
3411 c->object.flags |= SHOWN;
3413 if (!revs->boundary)
3414 return c;
3416 if (!c) {
3418 * get_revision_1() runs out the commits, and
3419 * we are done computing the boundaries.
3420 * switch to boundary commits output mode.
3422 revs->boundary = 2;
3425 * Update revs->commits to contain the list of
3426 * boundary commits.
3428 create_boundary_commit_list(revs);
3430 return get_revision_internal(revs);
3434 * boundary commits are the commits that are parents of the
3435 * ones we got from get_revision_1() but they themselves are
3436 * not returned from get_revision_1(). Before returning
3437 * 'c', we need to mark its parents that they could be boundaries.
3440 for (l = c->parents; l; l = l->next) {
3441 struct object *p;
3442 p = &(l->item->object);
3443 if (p->flags & (CHILD_SHOWN | SHOWN))
3444 continue;
3445 p->flags |= CHILD_SHOWN;
3446 gc_boundary(&revs->boundary_commits);
3447 add_object_array(p, NULL, &revs->boundary_commits);
3450 return c;
3453 struct commit *get_revision(struct rev_info *revs)
3455 struct commit *c;
3456 struct commit_list *reversed;
3458 if (revs->reverse) {
3459 reversed = NULL;
3460 while ((c = get_revision_internal(revs)))
3461 commit_list_insert(c, &reversed);
3462 revs->commits = reversed;
3463 revs->reverse = 0;
3464 revs->reverse_output_stage = 1;
3467 if (revs->reverse_output_stage) {
3468 c = pop_commit(&revs->commits);
3469 if (revs->track_linear)
3470 revs->linear = !!(c && c->object.flags & TRACK_LINEAR);
3471 return c;
3474 c = get_revision_internal(revs);
3475 if (c && revs->graph)
3476 graph_update(revs->graph, c);
3477 if (!c) {
3478 free_saved_parents(revs);
3479 if (revs->previous_parents) {
3480 free_commit_list(revs->previous_parents);
3481 revs->previous_parents = NULL;
3484 return c;
3487 char *get_revision_mark(const struct rev_info *revs, const struct commit *commit)
3489 if (commit->object.flags & BOUNDARY)
3490 return "-";
3491 else if (commit->object.flags & UNINTERESTING)
3492 return "^";
3493 else if (commit->object.flags & PATCHSAME)
3494 return "=";
3495 else if (!revs || revs->left_right) {
3496 if (commit->object.flags & SYMMETRIC_LEFT)
3497 return "<";
3498 else
3499 return ">";
3500 } else if (revs->graph)
3501 return "*";
3502 else if (revs->cherry_mark)
3503 return "+";
3504 return "";
3507 void put_revision_mark(const struct rev_info *revs, const struct commit *commit)
3509 char *mark = get_revision_mark(revs, commit);
3510 if (!strlen(mark))
3511 return;
3512 fputs(mark, stdout);
3513 putchar(' ');