list-objects-filter: correct usage of ALLOC_GROW
[git.git] / revision.c
blob6d355b43c37ac15ca113c513750fcb2d435a2bef
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"
28 volatile show_early_output_fn_t show_early_output;
30 static const char *term_bad;
31 static const char *term_good;
33 implement_shared_commit_slab(revision_sources, char *);
35 void show_object_with_name(FILE *out, struct object *obj, const char *name)
37 const char *p;
39 fprintf(out, "%s ", oid_to_hex(&obj->oid));
40 for (p = name; *p && *p != '\n'; p++)
41 fputc(*p, out);
42 fputc('\n', out);
45 static void mark_blob_uninteresting(struct blob *blob)
47 if (!blob)
48 return;
49 if (blob->object.flags & UNINTERESTING)
50 return;
51 blob->object.flags |= UNINTERESTING;
54 static void mark_tree_contents_uninteresting(struct tree *tree)
56 struct tree_desc desc;
57 struct name_entry entry;
59 if (parse_tree_gently(tree, 1) < 0)
60 return;
62 init_tree_desc(&desc, tree->buffer, tree->size);
63 while (tree_entry(&desc, &entry)) {
64 switch (object_type(entry.mode)) {
65 case OBJ_TREE:
66 mark_tree_uninteresting(lookup_tree(the_repository, entry.oid));
67 break;
68 case OBJ_BLOB:
69 mark_blob_uninteresting(lookup_blob(the_repository, entry.oid));
70 break;
71 default:
72 /* Subproject commit - not in this repository */
73 break;
78 * We don't care about the tree any more
79 * after it has been marked uninteresting.
81 free_tree_buffer(tree);
84 void mark_tree_uninteresting(struct tree *tree)
86 struct object *obj;
88 if (!tree)
89 return;
91 obj = &tree->object;
92 if (obj->flags & UNINTERESTING)
93 return;
94 obj->flags |= UNINTERESTING;
95 mark_tree_contents_uninteresting(tree);
98 struct commit_stack {
99 struct commit **items;
100 size_t nr, alloc;
102 #define COMMIT_STACK_INIT { NULL, 0, 0 }
104 static void commit_stack_push(struct commit_stack *stack, struct commit *commit)
106 ALLOC_GROW(stack->items, stack->nr + 1, stack->alloc);
107 stack->items[stack->nr++] = commit;
110 static struct commit *commit_stack_pop(struct commit_stack *stack)
112 return stack->nr ? stack->items[--stack->nr] : NULL;
115 static void commit_stack_clear(struct commit_stack *stack)
117 FREE_AND_NULL(stack->items);
118 stack->nr = stack->alloc = 0;
121 static void mark_one_parent_uninteresting(struct commit *commit,
122 struct commit_stack *pending)
124 struct commit_list *l;
126 if (commit->object.flags & UNINTERESTING)
127 return;
128 commit->object.flags |= UNINTERESTING;
131 * Normally we haven't parsed the parent
132 * yet, so we won't have a parent of a parent
133 * here. However, it may turn out that we've
134 * reached this commit some other way (where it
135 * wasn't uninteresting), in which case we need
136 * to mark its parents recursively too..
138 for (l = commit->parents; l; l = l->next)
139 commit_stack_push(pending, l->item);
142 void mark_parents_uninteresting(struct commit *commit)
144 struct commit_stack pending = COMMIT_STACK_INIT;
145 struct commit_list *l;
147 for (l = commit->parents; l; l = l->next)
148 mark_one_parent_uninteresting(l->item, &pending);
150 while (pending.nr > 0)
151 mark_one_parent_uninteresting(commit_stack_pop(&pending),
152 &pending);
154 commit_stack_clear(&pending);
157 static void add_pending_object_with_path(struct rev_info *revs,
158 struct object *obj,
159 const char *name, unsigned mode,
160 const char *path)
162 if (!obj)
163 return;
164 if (revs->no_walk && (obj->flags & UNINTERESTING))
165 revs->no_walk = 0;
166 if (revs->reflog_info && obj->type == OBJ_COMMIT) {
167 struct strbuf buf = STRBUF_INIT;
168 int len = interpret_branch_name(name, 0, &buf, 0);
170 if (0 < len && name[len] && buf.len)
171 strbuf_addstr(&buf, name + len);
172 add_reflog_for_walk(revs->reflog_info,
173 (struct commit *)obj,
174 buf.buf[0] ? buf.buf: name);
175 strbuf_release(&buf);
176 return; /* do not add the commit itself */
178 add_object_array_with_path(obj, name, &revs->pending, mode, path);
181 static void add_pending_object_with_mode(struct rev_info *revs,
182 struct object *obj,
183 const char *name, unsigned mode)
185 add_pending_object_with_path(revs, obj, name, mode, NULL);
188 void add_pending_object(struct rev_info *revs,
189 struct object *obj, const char *name)
191 add_pending_object_with_mode(revs, obj, name, S_IFINVALID);
194 void add_head_to_pending(struct rev_info *revs)
196 struct object_id oid;
197 struct object *obj;
198 if (get_oid("HEAD", &oid))
199 return;
200 obj = parse_object(the_repository, &oid);
201 if (!obj)
202 return;
203 add_pending_object(revs, obj, "HEAD");
206 static struct object *get_reference(struct rev_info *revs, const char *name,
207 const struct object_id *oid,
208 unsigned int flags)
210 struct object *object;
212 object = parse_object(the_repository, oid);
213 if (!object) {
214 if (revs->ignore_missing)
215 return object;
216 if (revs->exclude_promisor_objects && is_promisor_object(oid))
217 return NULL;
218 die("bad object %s", name);
220 object->flags |= flags;
221 return object;
224 void add_pending_oid(struct rev_info *revs, const char *name,
225 const struct object_id *oid, unsigned int flags)
227 struct object *object = get_reference(revs, name, oid, flags);
228 add_pending_object(revs, object, name);
231 static struct commit *handle_commit(struct rev_info *revs,
232 struct object_array_entry *entry)
234 struct object *object = entry->item;
235 const char *name = entry->name;
236 const char *path = entry->path;
237 unsigned int mode = entry->mode;
238 unsigned long flags = object->flags;
241 * Tag object? Look what it points to..
243 while (object->type == OBJ_TAG) {
244 struct tag *tag = (struct tag *) object;
245 if (revs->tag_objects && !(flags & UNINTERESTING))
246 add_pending_object(revs, object, tag->tag);
247 if (!tag->tagged)
248 die("bad tag");
249 object = parse_object(the_repository, &tag->tagged->oid);
250 if (!object) {
251 if (revs->ignore_missing_links || (flags & UNINTERESTING))
252 return NULL;
253 if (revs->exclude_promisor_objects &&
254 is_promisor_object(&tag->tagged->oid))
255 return NULL;
256 die("bad object %s", oid_to_hex(&tag->tagged->oid));
258 object->flags |= flags;
260 * We'll handle the tagged object by looping or dropping
261 * through to the non-tag handlers below. Do not
262 * propagate path data from the tag's pending entry.
264 path = NULL;
265 mode = 0;
269 * Commit object? Just return it, we'll do all the complex
270 * reachability crud.
272 if (object->type == OBJ_COMMIT) {
273 struct commit *commit = (struct commit *)object;
275 if (parse_commit(commit) < 0)
276 die("unable to parse commit %s", name);
277 if (flags & UNINTERESTING) {
278 mark_parents_uninteresting(commit);
279 revs->limited = 1;
281 if (revs->sources) {
282 char **slot = revision_sources_at(revs->sources, commit);
284 if (!*slot)
285 *slot = xstrdup(name);
287 return commit;
291 * Tree object? Either mark it uninteresting, or add it
292 * to the list of objects to look at later..
294 if (object->type == OBJ_TREE) {
295 struct tree *tree = (struct tree *)object;
296 if (!revs->tree_objects)
297 return NULL;
298 if (flags & UNINTERESTING) {
299 mark_tree_contents_uninteresting(tree);
300 return NULL;
302 add_pending_object_with_path(revs, object, name, mode, path);
303 return NULL;
307 * Blob object? You know the drill by now..
309 if (object->type == OBJ_BLOB) {
310 if (!revs->blob_objects)
311 return NULL;
312 if (flags & UNINTERESTING)
313 return NULL;
314 add_pending_object_with_path(revs, object, name, mode, path);
315 return NULL;
317 die("%s is unknown object", name);
320 static int everybody_uninteresting(struct commit_list *orig,
321 struct commit **interesting_cache)
323 struct commit_list *list = orig;
325 if (*interesting_cache) {
326 struct commit *commit = *interesting_cache;
327 if (!(commit->object.flags & UNINTERESTING))
328 return 0;
331 while (list) {
332 struct commit *commit = list->item;
333 list = list->next;
334 if (commit->object.flags & UNINTERESTING)
335 continue;
337 *interesting_cache = commit;
338 return 0;
340 return 1;
344 * A definition of "relevant" commit that we can use to simplify limited graphs
345 * by eliminating side branches.
347 * A "relevant" commit is one that is !UNINTERESTING (ie we are including it
348 * in our list), or that is a specified BOTTOM commit. Then after computing
349 * a limited list, during processing we can generally ignore boundary merges
350 * coming from outside the graph, (ie from irrelevant parents), and treat
351 * those merges as if they were single-parent. TREESAME is defined to consider
352 * only relevant parents, if any. If we are TREESAME to our on-graph parents,
353 * we don't care if we were !TREESAME to non-graph parents.
355 * Treating bottom commits as relevant ensures that a limited graph's
356 * connection to the actual bottom commit is not viewed as a side branch, but
357 * treated as part of the graph. For example:
359 * ....Z...A---X---o---o---B
360 * . /
361 * W---Y
363 * When computing "A..B", the A-X connection is at least as important as
364 * Y-X, despite A being flagged UNINTERESTING.
366 * And when computing --ancestry-path "A..B", the A-X connection is more
367 * important than Y-X, despite both A and Y being flagged UNINTERESTING.
369 static inline int relevant_commit(struct commit *commit)
371 return (commit->object.flags & (UNINTERESTING | BOTTOM)) != UNINTERESTING;
375 * Return a single relevant commit from a parent list. If we are a TREESAME
376 * commit, and this selects one of our parents, then we can safely simplify to
377 * that parent.
379 static struct commit *one_relevant_parent(const struct rev_info *revs,
380 struct commit_list *orig)
382 struct commit_list *list = orig;
383 struct commit *relevant = NULL;
385 if (!orig)
386 return NULL;
389 * For 1-parent commits, or if first-parent-only, then return that
390 * first parent (even if not "relevant" by the above definition).
391 * TREESAME will have been set purely on that parent.
393 if (revs->first_parent_only || !orig->next)
394 return orig->item;
397 * For multi-parent commits, identify a sole relevant parent, if any.
398 * If we have only one relevant parent, then TREESAME will be set purely
399 * with regard to that parent, and we can simplify accordingly.
401 * If we have more than one relevant parent, or no relevant parents
402 * (and multiple irrelevant ones), then we can't select a parent here
403 * and return NULL.
405 while (list) {
406 struct commit *commit = list->item;
407 list = list->next;
408 if (relevant_commit(commit)) {
409 if (relevant)
410 return NULL;
411 relevant = commit;
414 return relevant;
418 * The goal is to get REV_TREE_NEW as the result only if the
419 * diff consists of all '+' (and no other changes), REV_TREE_OLD
420 * if the whole diff is removal of old data, and otherwise
421 * REV_TREE_DIFFERENT (of course if the trees are the same we
422 * want REV_TREE_SAME).
424 * The only time we care about the distinction is when
425 * remove_empty_trees is in effect, in which case we care only about
426 * whether the whole change is REV_TREE_NEW, or if there's another type
427 * of change. Which means we can stop the diff early in either of these
428 * cases:
430 * 1. We're not using remove_empty_trees at all.
432 * 2. We saw anything except REV_TREE_NEW.
434 static int tree_difference = REV_TREE_SAME;
436 static void file_add_remove(struct diff_options *options,
437 int addremove, unsigned mode,
438 const struct object_id *oid,
439 int oid_valid,
440 const char *fullpath, unsigned dirty_submodule)
442 int diff = addremove == '+' ? REV_TREE_NEW : REV_TREE_OLD;
443 struct rev_info *revs = options->change_fn_data;
445 tree_difference |= diff;
446 if (!revs->remove_empty_trees || tree_difference != REV_TREE_NEW)
447 options->flags.has_changes = 1;
450 static void file_change(struct diff_options *options,
451 unsigned old_mode, unsigned new_mode,
452 const struct object_id *old_oid,
453 const struct object_id *new_oid,
454 int old_oid_valid, int new_oid_valid,
455 const char *fullpath,
456 unsigned old_dirty_submodule, unsigned new_dirty_submodule)
458 tree_difference = REV_TREE_DIFFERENT;
459 options->flags.has_changes = 1;
462 static int rev_compare_tree(struct rev_info *revs,
463 struct commit *parent, struct commit *commit)
465 struct tree *t1 = get_commit_tree(parent);
466 struct tree *t2 = get_commit_tree(commit);
468 if (!t1)
469 return REV_TREE_NEW;
470 if (!t2)
471 return REV_TREE_OLD;
473 if (revs->simplify_by_decoration) {
475 * If we are simplifying by decoration, then the commit
476 * is worth showing if it has a tag pointing at it.
478 if (get_name_decoration(&commit->object))
479 return REV_TREE_DIFFERENT;
481 * A commit that is not pointed by a tag is uninteresting
482 * if we are not limited by path. This means that you will
483 * see the usual "commits that touch the paths" plus any
484 * tagged commit by specifying both --simplify-by-decoration
485 * and pathspec.
487 if (!revs->prune_data.nr)
488 return REV_TREE_SAME;
491 tree_difference = REV_TREE_SAME;
492 revs->pruning.flags.has_changes = 0;
493 if (diff_tree_oid(&t1->object.oid, &t2->object.oid, "",
494 &revs->pruning) < 0)
495 return REV_TREE_DIFFERENT;
496 return tree_difference;
499 static int rev_same_tree_as_empty(struct rev_info *revs, struct commit *commit)
501 int retval;
502 struct tree *t1 = get_commit_tree(commit);
504 if (!t1)
505 return 0;
507 tree_difference = REV_TREE_SAME;
508 revs->pruning.flags.has_changes = 0;
509 retval = diff_tree_oid(NULL, &t1->object.oid, "", &revs->pruning);
511 return retval >= 0 && (tree_difference == REV_TREE_SAME);
514 struct treesame_state {
515 unsigned int nparents;
516 unsigned char treesame[FLEX_ARRAY];
519 static struct treesame_state *initialise_treesame(struct rev_info *revs, struct commit *commit)
521 unsigned n = commit_list_count(commit->parents);
522 struct treesame_state *st = xcalloc(1, st_add(sizeof(*st), n));
523 st->nparents = n;
524 add_decoration(&revs->treesame, &commit->object, st);
525 return st;
529 * Must be called immediately after removing the nth_parent from a commit's
530 * parent list, if we are maintaining the per-parent treesame[] decoration.
531 * This does not recalculate the master TREESAME flag - update_treesame()
532 * should be called to update it after a sequence of treesame[] modifications
533 * that may have affected it.
535 static int compact_treesame(struct rev_info *revs, struct commit *commit, unsigned nth_parent)
537 struct treesame_state *st;
538 int old_same;
540 if (!commit->parents) {
542 * Have just removed the only parent from a non-merge.
543 * Different handling, as we lack decoration.
545 if (nth_parent != 0)
546 die("compact_treesame %u", nth_parent);
547 old_same = !!(commit->object.flags & TREESAME);
548 if (rev_same_tree_as_empty(revs, commit))
549 commit->object.flags |= TREESAME;
550 else
551 commit->object.flags &= ~TREESAME;
552 return old_same;
555 st = lookup_decoration(&revs->treesame, &commit->object);
556 if (!st || nth_parent >= st->nparents)
557 die("compact_treesame %u", nth_parent);
559 old_same = st->treesame[nth_parent];
560 memmove(st->treesame + nth_parent,
561 st->treesame + nth_parent + 1,
562 st->nparents - nth_parent - 1);
565 * If we've just become a non-merge commit, update TREESAME
566 * immediately, and remove the no-longer-needed decoration.
567 * If still a merge, defer update until update_treesame().
569 if (--st->nparents == 1) {
570 if (commit->parents->next)
571 die("compact_treesame parents mismatch");
572 if (st->treesame[0] && revs->dense)
573 commit->object.flags |= TREESAME;
574 else
575 commit->object.flags &= ~TREESAME;
576 free(add_decoration(&revs->treesame, &commit->object, NULL));
579 return old_same;
582 static unsigned update_treesame(struct rev_info *revs, struct commit *commit)
584 if (commit->parents && commit->parents->next) {
585 unsigned n;
586 struct treesame_state *st;
587 struct commit_list *p;
588 unsigned relevant_parents;
589 unsigned relevant_change, irrelevant_change;
591 st = lookup_decoration(&revs->treesame, &commit->object);
592 if (!st)
593 die("update_treesame %s", oid_to_hex(&commit->object.oid));
594 relevant_parents = 0;
595 relevant_change = irrelevant_change = 0;
596 for (p = commit->parents, n = 0; p; n++, p = p->next) {
597 if (relevant_commit(p->item)) {
598 relevant_change |= !st->treesame[n];
599 relevant_parents++;
600 } else
601 irrelevant_change |= !st->treesame[n];
603 if (relevant_parents ? relevant_change : irrelevant_change)
604 commit->object.flags &= ~TREESAME;
605 else
606 commit->object.flags |= TREESAME;
609 return commit->object.flags & TREESAME;
612 static inline int limiting_can_increase_treesame(const struct rev_info *revs)
615 * TREESAME is irrelevant unless prune && dense;
616 * if simplify_history is set, we can't have a mixture of TREESAME and
617 * !TREESAME INTERESTING parents (and we don't have treesame[]
618 * decoration anyway);
619 * if first_parent_only is set, then the TREESAME flag is locked
620 * against the first parent (and again we lack treesame[] decoration).
622 return revs->prune && revs->dense &&
623 !revs->simplify_history &&
624 !revs->first_parent_only;
627 static void try_to_simplify_commit(struct rev_info *revs, struct commit *commit)
629 struct commit_list **pp, *parent;
630 struct treesame_state *ts = NULL;
631 int relevant_change = 0, irrelevant_change = 0;
632 int relevant_parents, nth_parent;
635 * If we don't do pruning, everything is interesting
637 if (!revs->prune)
638 return;
640 if (!get_commit_tree(commit))
641 return;
643 if (!commit->parents) {
644 if (rev_same_tree_as_empty(revs, commit))
645 commit->object.flags |= TREESAME;
646 return;
650 * Normal non-merge commit? If we don't want to make the
651 * history dense, we consider it always to be a change..
653 if (!revs->dense && !commit->parents->next)
654 return;
656 for (pp = &commit->parents, nth_parent = 0, relevant_parents = 0;
657 (parent = *pp) != NULL;
658 pp = &parent->next, nth_parent++) {
659 struct commit *p = parent->item;
660 if (relevant_commit(p))
661 relevant_parents++;
663 if (nth_parent == 1) {
665 * This our second loop iteration - so we now know
666 * we're dealing with a merge.
668 * Do not compare with later parents when we care only about
669 * the first parent chain, in order to avoid derailing the
670 * traversal to follow a side branch that brought everything
671 * in the path we are limited to by the pathspec.
673 if (revs->first_parent_only)
674 break;
676 * If this will remain a potentially-simplifiable
677 * merge, remember per-parent treesame if needed.
678 * Initialise the array with the comparison from our
679 * first iteration.
681 if (revs->treesame.name &&
682 !revs->simplify_history &&
683 !(commit->object.flags & UNINTERESTING)) {
684 ts = initialise_treesame(revs, commit);
685 if (!(irrelevant_change || relevant_change))
686 ts->treesame[0] = 1;
689 if (parse_commit(p) < 0)
690 die("cannot simplify commit %s (because of %s)",
691 oid_to_hex(&commit->object.oid),
692 oid_to_hex(&p->object.oid));
693 switch (rev_compare_tree(revs, p, commit)) {
694 case REV_TREE_SAME:
695 if (!revs->simplify_history || !relevant_commit(p)) {
696 /* Even if a merge with an uninteresting
697 * side branch brought the entire change
698 * we are interested in, we do not want
699 * to lose the other branches of this
700 * merge, so we just keep going.
702 if (ts)
703 ts->treesame[nth_parent] = 1;
704 continue;
706 parent->next = NULL;
707 commit->parents = parent;
708 commit->object.flags |= TREESAME;
709 return;
711 case REV_TREE_NEW:
712 if (revs->remove_empty_trees &&
713 rev_same_tree_as_empty(revs, p)) {
714 /* We are adding all the specified
715 * paths from this parent, so the
716 * history beyond this parent is not
717 * interesting. Remove its parents
718 * (they are grandparents for us).
719 * IOW, we pretend this parent is a
720 * "root" commit.
722 if (parse_commit(p) < 0)
723 die("cannot simplify commit %s (invalid %s)",
724 oid_to_hex(&commit->object.oid),
725 oid_to_hex(&p->object.oid));
726 p->parents = NULL;
728 /* fallthrough */
729 case REV_TREE_OLD:
730 case REV_TREE_DIFFERENT:
731 if (relevant_commit(p))
732 relevant_change = 1;
733 else
734 irrelevant_change = 1;
735 continue;
737 die("bad tree compare for commit %s", oid_to_hex(&commit->object.oid));
741 * TREESAME is straightforward for single-parent commits. For merge
742 * commits, it is most useful to define it so that "irrelevant"
743 * parents cannot make us !TREESAME - if we have any relevant
744 * parents, then we only consider TREESAMEness with respect to them,
745 * allowing irrelevant merges from uninteresting branches to be
746 * simplified away. Only if we have only irrelevant parents do we
747 * base TREESAME on them. Note that this logic is replicated in
748 * update_treesame, which should be kept in sync.
750 if (relevant_parents ? !relevant_change : !irrelevant_change)
751 commit->object.flags |= TREESAME;
754 static void commit_list_insert_by_date_cached(struct commit *p, struct commit_list **head,
755 struct commit_list *cached_base, struct commit_list **cache)
757 struct commit_list *new_entry;
759 if (cached_base && p->date < cached_base->item->date)
760 new_entry = commit_list_insert_by_date(p, &cached_base->next);
761 else
762 new_entry = commit_list_insert_by_date(p, head);
764 if (cache && (!*cache || p->date < (*cache)->item->date))
765 *cache = new_entry;
768 static int add_parents_to_list(struct rev_info *revs, struct commit *commit,
769 struct commit_list **list, struct commit_list **cache_ptr)
771 struct commit_list *parent = commit->parents;
772 unsigned left_flag;
773 struct commit_list *cached_base = cache_ptr ? *cache_ptr : NULL;
775 if (commit->object.flags & ADDED)
776 return 0;
777 commit->object.flags |= ADDED;
779 if (revs->include_check &&
780 !revs->include_check(commit, revs->include_check_data))
781 return 0;
784 * If the commit is uninteresting, don't try to
785 * prune parents - we want the maximal uninteresting
786 * set.
788 * Normally we haven't parsed the parent
789 * yet, so we won't have a parent of a parent
790 * here. However, it may turn out that we've
791 * reached this commit some other way (where it
792 * wasn't uninteresting), in which case we need
793 * to mark its parents recursively too..
795 if (commit->object.flags & UNINTERESTING) {
796 while (parent) {
797 struct commit *p = parent->item;
798 parent = parent->next;
799 if (p)
800 p->object.flags |= UNINTERESTING;
801 if (parse_commit_gently(p, 1) < 0)
802 continue;
803 if (p->parents)
804 mark_parents_uninteresting(p);
805 if (p->object.flags & SEEN)
806 continue;
807 p->object.flags |= SEEN;
808 commit_list_insert_by_date_cached(p, list, cached_base, cache_ptr);
810 return 0;
814 * Ok, the commit wasn't uninteresting. Try to
815 * simplify the commit history and find the parent
816 * that has no differences in the path set if one exists.
818 try_to_simplify_commit(revs, commit);
820 if (revs->no_walk)
821 return 0;
823 left_flag = (commit->object.flags & SYMMETRIC_LEFT);
825 for (parent = commit->parents; parent; parent = parent->next) {
826 struct commit *p = parent->item;
827 int gently = revs->ignore_missing_links ||
828 revs->exclude_promisor_objects;
829 if (parse_commit_gently(p, gently) < 0) {
830 if (revs->exclude_promisor_objects &&
831 is_promisor_object(&p->object.oid)) {
832 if (revs->first_parent_only)
833 break;
834 continue;
836 return -1;
838 if (revs->sources) {
839 char **slot = revision_sources_at(revs->sources, p);
841 if (!*slot)
842 *slot = *revision_sources_at(revs->sources, commit);
844 p->object.flags |= left_flag;
845 if (!(p->object.flags & SEEN)) {
846 p->object.flags |= SEEN;
847 commit_list_insert_by_date_cached(p, list, cached_base, cache_ptr);
849 if (revs->first_parent_only)
850 break;
852 return 0;
855 static void cherry_pick_list(struct commit_list *list, struct rev_info *revs)
857 struct commit_list *p;
858 int left_count = 0, right_count = 0;
859 int left_first;
860 struct patch_ids ids;
861 unsigned cherry_flag;
863 /* First count the commits on the left and on the right */
864 for (p = list; p; p = p->next) {
865 struct commit *commit = p->item;
866 unsigned flags = commit->object.flags;
867 if (flags & BOUNDARY)
869 else if (flags & SYMMETRIC_LEFT)
870 left_count++;
871 else
872 right_count++;
875 if (!left_count || !right_count)
876 return;
878 left_first = left_count < right_count;
879 init_patch_ids(&ids);
880 ids.diffopts.pathspec = revs->diffopt.pathspec;
882 /* Compute patch-ids for one side */
883 for (p = list; p; p = p->next) {
884 struct commit *commit = p->item;
885 unsigned flags = commit->object.flags;
887 if (flags & BOUNDARY)
888 continue;
890 * If we have fewer left, left_first is set and we omit
891 * commits on the right branch in this loop. If we have
892 * fewer right, we skip the left ones.
894 if (left_first != !!(flags & SYMMETRIC_LEFT))
895 continue;
896 add_commit_patch_id(commit, &ids);
899 /* either cherry_mark or cherry_pick are true */
900 cherry_flag = revs->cherry_mark ? PATCHSAME : SHOWN;
902 /* Check the other side */
903 for (p = list; p; p = p->next) {
904 struct commit *commit = p->item;
905 struct patch_id *id;
906 unsigned flags = commit->object.flags;
908 if (flags & BOUNDARY)
909 continue;
911 * If we have fewer left, left_first is set and we omit
912 * commits on the left branch in this loop.
914 if (left_first == !!(flags & SYMMETRIC_LEFT))
915 continue;
918 * Have we seen the same patch id?
920 id = has_commit_patch_id(commit, &ids);
921 if (!id)
922 continue;
924 commit->object.flags |= cherry_flag;
925 id->commit->object.flags |= cherry_flag;
928 free_patch_ids(&ids);
931 /* How many extra uninteresting commits we want to see.. */
932 #define SLOP 5
934 static int still_interesting(struct commit_list *src, timestamp_t date, int slop,
935 struct commit **interesting_cache)
938 * No source list at all? We're definitely done..
940 if (!src)
941 return 0;
944 * Does the destination list contain entries with a date
945 * before the source list? Definitely _not_ done.
947 if (date <= src->item->date)
948 return SLOP;
951 * Does the source list still have interesting commits in
952 * it? Definitely not done..
954 if (!everybody_uninteresting(src, interesting_cache))
955 return SLOP;
957 /* Ok, we're closing in.. */
958 return slop-1;
962 * "rev-list --ancestry-path A..B" computes commits that are ancestors
963 * of B but not ancestors of A but further limits the result to those
964 * that are descendants of A. This takes the list of bottom commits and
965 * the result of "A..B" without --ancestry-path, and limits the latter
966 * further to the ones that can reach one of the commits in "bottom".
968 static void limit_to_ancestry(struct commit_list *bottom, struct commit_list *list)
970 struct commit_list *p;
971 struct commit_list *rlist = NULL;
972 int made_progress;
975 * Reverse the list so that it will be likely that we would
976 * process parents before children.
978 for (p = list; p; p = p->next)
979 commit_list_insert(p->item, &rlist);
981 for (p = bottom; p; p = p->next)
982 p->item->object.flags |= TMP_MARK;
985 * Mark the ones that can reach bottom commits in "list",
986 * in a bottom-up fashion.
988 do {
989 made_progress = 0;
990 for (p = rlist; p; p = p->next) {
991 struct commit *c = p->item;
992 struct commit_list *parents;
993 if (c->object.flags & (TMP_MARK | UNINTERESTING))
994 continue;
995 for (parents = c->parents;
996 parents;
997 parents = parents->next) {
998 if (!(parents->item->object.flags & TMP_MARK))
999 continue;
1000 c->object.flags |= TMP_MARK;
1001 made_progress = 1;
1002 break;
1005 } while (made_progress);
1008 * NEEDSWORK: decide if we want to remove parents that are
1009 * not marked with TMP_MARK from commit->parents for commits
1010 * in the resulting list. We may not want to do that, though.
1014 * The ones that are not marked with TMP_MARK are uninteresting
1016 for (p = list; p; p = p->next) {
1017 struct commit *c = p->item;
1018 if (c->object.flags & TMP_MARK)
1019 continue;
1020 c->object.flags |= UNINTERESTING;
1023 /* We are done with the TMP_MARK */
1024 for (p = list; p; p = p->next)
1025 p->item->object.flags &= ~TMP_MARK;
1026 for (p = bottom; p; p = p->next)
1027 p->item->object.flags &= ~TMP_MARK;
1028 free_commit_list(rlist);
1032 * Before walking the history, keep the set of "negative" refs the
1033 * caller has asked to exclude.
1035 * This is used to compute "rev-list --ancestry-path A..B", as we need
1036 * to filter the result of "A..B" further to the ones that can actually
1037 * reach A.
1039 static struct commit_list *collect_bottom_commits(struct commit_list *list)
1041 struct commit_list *elem, *bottom = NULL;
1042 for (elem = list; elem; elem = elem->next)
1043 if (elem->item->object.flags & BOTTOM)
1044 commit_list_insert(elem->item, &bottom);
1045 return bottom;
1048 /* Assumes either left_only or right_only is set */
1049 static void limit_left_right(struct commit_list *list, struct rev_info *revs)
1051 struct commit_list *p;
1053 for (p = list; p; p = p->next) {
1054 struct commit *commit = p->item;
1056 if (revs->right_only) {
1057 if (commit->object.flags & SYMMETRIC_LEFT)
1058 commit->object.flags |= SHOWN;
1059 } else /* revs->left_only is set */
1060 if (!(commit->object.flags & SYMMETRIC_LEFT))
1061 commit->object.flags |= SHOWN;
1065 static int limit_list(struct rev_info *revs)
1067 int slop = SLOP;
1068 timestamp_t date = TIME_MAX;
1069 struct commit_list *list = revs->commits;
1070 struct commit_list *newlist = NULL;
1071 struct commit_list **p = &newlist;
1072 struct commit_list *bottom = NULL;
1073 struct commit *interesting_cache = NULL;
1075 if (revs->ancestry_path) {
1076 bottom = collect_bottom_commits(list);
1077 if (!bottom)
1078 die("--ancestry-path given but there are no bottom commits");
1081 while (list) {
1082 struct commit *commit = pop_commit(&list);
1083 struct object *obj = &commit->object;
1084 show_early_output_fn_t show;
1086 if (commit == interesting_cache)
1087 interesting_cache = NULL;
1089 if (revs->max_age != -1 && (commit->date < revs->max_age))
1090 obj->flags |= UNINTERESTING;
1091 if (add_parents_to_list(revs, commit, &list, NULL) < 0)
1092 return -1;
1093 if (obj->flags & UNINTERESTING) {
1094 mark_parents_uninteresting(commit);
1095 slop = still_interesting(list, date, slop, &interesting_cache);
1096 if (slop)
1097 continue;
1098 break;
1100 if (revs->min_age != -1 && (commit->date > revs->min_age))
1101 continue;
1102 date = commit->date;
1103 p = &commit_list_insert(commit, p)->next;
1105 show = show_early_output;
1106 if (!show)
1107 continue;
1109 show(revs, newlist);
1110 show_early_output = NULL;
1112 if (revs->cherry_pick || revs->cherry_mark)
1113 cherry_pick_list(newlist, revs);
1115 if (revs->left_only || revs->right_only)
1116 limit_left_right(newlist, revs);
1118 if (bottom) {
1119 limit_to_ancestry(bottom, newlist);
1120 free_commit_list(bottom);
1124 * Check if any commits have become TREESAME by some of their parents
1125 * becoming UNINTERESTING.
1127 if (limiting_can_increase_treesame(revs))
1128 for (list = newlist; list; list = list->next) {
1129 struct commit *c = list->item;
1130 if (c->object.flags & (UNINTERESTING | TREESAME))
1131 continue;
1132 update_treesame(revs, c);
1135 revs->commits = newlist;
1136 return 0;
1140 * Add an entry to refs->cmdline with the specified information.
1141 * *name is copied.
1143 static void add_rev_cmdline(struct rev_info *revs,
1144 struct object *item,
1145 const char *name,
1146 int whence,
1147 unsigned flags)
1149 struct rev_cmdline_info *info = &revs->cmdline;
1150 unsigned int nr = info->nr;
1152 ALLOC_GROW(info->rev, nr + 1, info->alloc);
1153 info->rev[nr].item = item;
1154 info->rev[nr].name = xstrdup(name);
1155 info->rev[nr].whence = whence;
1156 info->rev[nr].flags = flags;
1157 info->nr++;
1160 static void add_rev_cmdline_list(struct rev_info *revs,
1161 struct commit_list *commit_list,
1162 int whence,
1163 unsigned flags)
1165 while (commit_list) {
1166 struct object *object = &commit_list->item->object;
1167 add_rev_cmdline(revs, object, oid_to_hex(&object->oid),
1168 whence, flags);
1169 commit_list = commit_list->next;
1173 struct all_refs_cb {
1174 int all_flags;
1175 int warned_bad_reflog;
1176 struct rev_info *all_revs;
1177 const char *name_for_errormsg;
1178 struct ref_store *refs;
1181 int ref_excluded(struct string_list *ref_excludes, const char *path)
1183 struct string_list_item *item;
1185 if (!ref_excludes)
1186 return 0;
1187 for_each_string_list_item(item, ref_excludes) {
1188 if (!wildmatch(item->string, path, 0))
1189 return 1;
1191 return 0;
1194 static int handle_one_ref(const char *path, const struct object_id *oid,
1195 int flag, void *cb_data)
1197 struct all_refs_cb *cb = cb_data;
1198 struct object *object;
1200 if (ref_excluded(cb->all_revs->ref_excludes, path))
1201 return 0;
1203 object = get_reference(cb->all_revs, path, oid, cb->all_flags);
1204 add_rev_cmdline(cb->all_revs, object, path, REV_CMD_REF, cb->all_flags);
1205 add_pending_oid(cb->all_revs, path, oid, cb->all_flags);
1206 return 0;
1209 static void init_all_refs_cb(struct all_refs_cb *cb, struct rev_info *revs,
1210 unsigned flags)
1212 cb->all_revs = revs;
1213 cb->all_flags = flags;
1214 revs->rev_input_given = 1;
1215 cb->refs = NULL;
1218 void clear_ref_exclusion(struct string_list **ref_excludes_p)
1220 if (*ref_excludes_p) {
1221 string_list_clear(*ref_excludes_p, 0);
1222 free(*ref_excludes_p);
1224 *ref_excludes_p = NULL;
1227 void add_ref_exclusion(struct string_list **ref_excludes_p, const char *exclude)
1229 if (!*ref_excludes_p) {
1230 *ref_excludes_p = xcalloc(1, sizeof(**ref_excludes_p));
1231 (*ref_excludes_p)->strdup_strings = 1;
1233 string_list_append(*ref_excludes_p, exclude);
1236 static void handle_refs(struct ref_store *refs,
1237 struct rev_info *revs, unsigned flags,
1238 int (*for_each)(struct ref_store *, each_ref_fn, void *))
1240 struct all_refs_cb cb;
1242 if (!refs) {
1243 /* this could happen with uninitialized submodules */
1244 return;
1247 init_all_refs_cb(&cb, revs, flags);
1248 for_each(refs, handle_one_ref, &cb);
1251 static void handle_one_reflog_commit(struct object_id *oid, void *cb_data)
1253 struct all_refs_cb *cb = cb_data;
1254 if (!is_null_oid(oid)) {
1255 struct object *o = parse_object(the_repository, oid);
1256 if (o) {
1257 o->flags |= cb->all_flags;
1258 /* ??? CMDLINEFLAGS ??? */
1259 add_pending_object(cb->all_revs, o, "");
1261 else if (!cb->warned_bad_reflog) {
1262 warning("reflog of '%s' references pruned commits",
1263 cb->name_for_errormsg);
1264 cb->warned_bad_reflog = 1;
1269 static int handle_one_reflog_ent(struct object_id *ooid, struct object_id *noid,
1270 const char *email, timestamp_t timestamp, int tz,
1271 const char *message, void *cb_data)
1273 handle_one_reflog_commit(ooid, cb_data);
1274 handle_one_reflog_commit(noid, cb_data);
1275 return 0;
1278 static int handle_one_reflog(const char *path, const struct object_id *oid,
1279 int flag, void *cb_data)
1281 struct all_refs_cb *cb = cb_data;
1282 cb->warned_bad_reflog = 0;
1283 cb->name_for_errormsg = path;
1284 refs_for_each_reflog_ent(cb->refs, path,
1285 handle_one_reflog_ent, cb_data);
1286 return 0;
1289 static void add_other_reflogs_to_pending(struct all_refs_cb *cb)
1291 struct worktree **worktrees, **p;
1293 worktrees = get_worktrees(0);
1294 for (p = worktrees; *p; p++) {
1295 struct worktree *wt = *p;
1297 if (wt->is_current)
1298 continue;
1300 cb->refs = get_worktree_ref_store(wt);
1301 refs_for_each_reflog(cb->refs,
1302 handle_one_reflog,
1303 cb);
1305 free_worktrees(worktrees);
1308 void add_reflogs_to_pending(struct rev_info *revs, unsigned flags)
1310 struct all_refs_cb cb;
1312 cb.all_revs = revs;
1313 cb.all_flags = flags;
1314 cb.refs = get_main_ref_store(the_repository);
1315 for_each_reflog(handle_one_reflog, &cb);
1317 if (!revs->single_worktree)
1318 add_other_reflogs_to_pending(&cb);
1321 static void add_cache_tree(struct cache_tree *it, struct rev_info *revs,
1322 struct strbuf *path)
1324 size_t baselen = path->len;
1325 int i;
1327 if (it->entry_count >= 0) {
1328 struct tree *tree = lookup_tree(the_repository, &it->oid);
1329 add_pending_object_with_path(revs, &tree->object, "",
1330 040000, path->buf);
1333 for (i = 0; i < it->subtree_nr; i++) {
1334 struct cache_tree_sub *sub = it->down[i];
1335 strbuf_addf(path, "%s%s", baselen ? "/" : "", sub->name);
1336 add_cache_tree(sub->cache_tree, revs, path);
1337 strbuf_setlen(path, baselen);
1342 static void do_add_index_objects_to_pending(struct rev_info *revs,
1343 struct index_state *istate)
1345 int i;
1347 for (i = 0; i < istate->cache_nr; i++) {
1348 struct cache_entry *ce = istate->cache[i];
1349 struct blob *blob;
1351 if (S_ISGITLINK(ce->ce_mode))
1352 continue;
1354 blob = lookup_blob(the_repository, &ce->oid);
1355 if (!blob)
1356 die("unable to add index blob to traversal");
1357 add_pending_object_with_path(revs, &blob->object, "",
1358 ce->ce_mode, ce->name);
1361 if (istate->cache_tree) {
1362 struct strbuf path = STRBUF_INIT;
1363 add_cache_tree(istate->cache_tree, revs, &path);
1364 strbuf_release(&path);
1368 void add_index_objects_to_pending(struct rev_info *revs, unsigned int flags)
1370 struct worktree **worktrees, **p;
1372 read_cache();
1373 do_add_index_objects_to_pending(revs, &the_index);
1375 if (revs->single_worktree)
1376 return;
1378 worktrees = get_worktrees(0);
1379 for (p = worktrees; *p; p++) {
1380 struct worktree *wt = *p;
1381 struct index_state istate = { NULL };
1383 if (wt->is_current)
1384 continue; /* current index already taken care of */
1386 if (read_index_from(&istate,
1387 worktree_git_path(wt, "index"),
1388 get_worktree_git_dir(wt)) > 0)
1389 do_add_index_objects_to_pending(revs, &istate);
1390 discard_index(&istate);
1392 free_worktrees(worktrees);
1395 static int add_parents_only(struct rev_info *revs, const char *arg_, int flags,
1396 int exclude_parent)
1398 struct object_id oid;
1399 struct object *it;
1400 struct commit *commit;
1401 struct commit_list *parents;
1402 int parent_number;
1403 const char *arg = arg_;
1405 if (*arg == '^') {
1406 flags ^= UNINTERESTING | BOTTOM;
1407 arg++;
1409 if (get_oid_committish(arg, &oid))
1410 return 0;
1411 while (1) {
1412 it = get_reference(revs, arg, &oid, 0);
1413 if (!it && revs->ignore_missing)
1414 return 0;
1415 if (it->type != OBJ_TAG)
1416 break;
1417 if (!((struct tag*)it)->tagged)
1418 return 0;
1419 oidcpy(&oid, &((struct tag*)it)->tagged->oid);
1421 if (it->type != OBJ_COMMIT)
1422 return 0;
1423 commit = (struct commit *)it;
1424 if (exclude_parent &&
1425 exclude_parent > commit_list_count(commit->parents))
1426 return 0;
1427 for (parents = commit->parents, parent_number = 1;
1428 parents;
1429 parents = parents->next, parent_number++) {
1430 if (exclude_parent && parent_number != exclude_parent)
1431 continue;
1433 it = &parents->item->object;
1434 it->flags |= flags;
1435 add_rev_cmdline(revs, it, arg_, REV_CMD_PARENTS_ONLY, flags);
1436 add_pending_object(revs, it, arg);
1438 return 1;
1441 void init_revisions(struct rev_info *revs, const char *prefix)
1443 memset(revs, 0, sizeof(*revs));
1445 revs->abbrev = DEFAULT_ABBREV;
1446 revs->ignore_merges = 1;
1447 revs->simplify_history = 1;
1448 revs->pruning.flags.recursive = 1;
1449 revs->pruning.flags.quick = 1;
1450 revs->pruning.add_remove = file_add_remove;
1451 revs->pruning.change = file_change;
1452 revs->pruning.change_fn_data = revs;
1453 revs->sort_order = REV_SORT_IN_GRAPH_ORDER;
1454 revs->dense = 1;
1455 revs->prefix = prefix;
1456 revs->max_age = -1;
1457 revs->min_age = -1;
1458 revs->skip_count = -1;
1459 revs->max_count = -1;
1460 revs->max_parents = -1;
1461 revs->expand_tabs_in_log = -1;
1463 revs->commit_format = CMIT_FMT_DEFAULT;
1464 revs->expand_tabs_in_log_default = 8;
1466 init_grep_defaults();
1467 grep_init(&revs->grep_filter, prefix);
1468 revs->grep_filter.status_only = 1;
1470 diff_setup(&revs->diffopt);
1471 if (prefix && !revs->diffopt.prefix) {
1472 revs->diffopt.prefix = prefix;
1473 revs->diffopt.prefix_length = strlen(prefix);
1476 revs->notes_opt.use_default_notes = -1;
1479 static void add_pending_commit_list(struct rev_info *revs,
1480 struct commit_list *commit_list,
1481 unsigned int flags)
1483 while (commit_list) {
1484 struct object *object = &commit_list->item->object;
1485 object->flags |= flags;
1486 add_pending_object(revs, object, oid_to_hex(&object->oid));
1487 commit_list = commit_list->next;
1491 static void prepare_show_merge(struct rev_info *revs)
1493 struct commit_list *bases;
1494 struct commit *head, *other;
1495 struct object_id oid;
1496 const char **prune = NULL;
1497 int i, prune_num = 1; /* counting terminating NULL */
1499 if (get_oid("HEAD", &oid))
1500 die("--merge without HEAD?");
1501 head = lookup_commit_or_die(&oid, "HEAD");
1502 if (get_oid("MERGE_HEAD", &oid))
1503 die("--merge without MERGE_HEAD?");
1504 other = lookup_commit_or_die(&oid, "MERGE_HEAD");
1505 add_pending_object(revs, &head->object, "HEAD");
1506 add_pending_object(revs, &other->object, "MERGE_HEAD");
1507 bases = get_merge_bases(head, other);
1508 add_rev_cmdline_list(revs, bases, REV_CMD_MERGE_BASE, UNINTERESTING | BOTTOM);
1509 add_pending_commit_list(revs, bases, UNINTERESTING | BOTTOM);
1510 free_commit_list(bases);
1511 head->object.flags |= SYMMETRIC_LEFT;
1513 if (!active_nr)
1514 read_cache();
1515 for (i = 0; i < active_nr; i++) {
1516 const struct cache_entry *ce = active_cache[i];
1517 if (!ce_stage(ce))
1518 continue;
1519 if (ce_path_match(ce, &revs->prune_data, NULL)) {
1520 prune_num++;
1521 REALLOC_ARRAY(prune, prune_num);
1522 prune[prune_num-2] = ce->name;
1523 prune[prune_num-1] = NULL;
1525 while ((i+1 < active_nr) &&
1526 ce_same_name(ce, active_cache[i+1]))
1527 i++;
1529 clear_pathspec(&revs->prune_data);
1530 parse_pathspec(&revs->prune_data, PATHSPEC_ALL_MAGIC & ~PATHSPEC_LITERAL,
1531 PATHSPEC_PREFER_FULL | PATHSPEC_LITERAL_PATH, "", prune);
1532 revs->limited = 1;
1535 static int dotdot_missing(const char *arg, char *dotdot,
1536 struct rev_info *revs, int symmetric)
1538 if (revs->ignore_missing)
1539 return 0;
1540 /* de-munge so we report the full argument */
1541 *dotdot = '.';
1542 die(symmetric
1543 ? "Invalid symmetric difference expression %s"
1544 : "Invalid revision range %s", arg);
1547 static int handle_dotdot_1(const char *arg, char *dotdot,
1548 struct rev_info *revs, int flags,
1549 int cant_be_filename,
1550 struct object_context *a_oc,
1551 struct object_context *b_oc)
1553 const char *a_name, *b_name;
1554 struct object_id a_oid, b_oid;
1555 struct object *a_obj, *b_obj;
1556 unsigned int a_flags, b_flags;
1557 int symmetric = 0;
1558 unsigned int flags_exclude = flags ^ (UNINTERESTING | BOTTOM);
1559 unsigned int oc_flags = GET_OID_COMMITTISH | GET_OID_RECORD_PATH;
1561 a_name = arg;
1562 if (!*a_name)
1563 a_name = "HEAD";
1565 b_name = dotdot + 2;
1566 if (*b_name == '.') {
1567 symmetric = 1;
1568 b_name++;
1570 if (!*b_name)
1571 b_name = "HEAD";
1573 if (get_oid_with_context(a_name, oc_flags, &a_oid, a_oc) ||
1574 get_oid_with_context(b_name, oc_flags, &b_oid, b_oc))
1575 return -1;
1577 if (!cant_be_filename) {
1578 *dotdot = '.';
1579 verify_non_filename(revs->prefix, arg);
1580 *dotdot = '\0';
1583 a_obj = parse_object(the_repository, &a_oid);
1584 b_obj = parse_object(the_repository, &b_oid);
1585 if (!a_obj || !b_obj)
1586 return dotdot_missing(arg, dotdot, revs, symmetric);
1588 if (!symmetric) {
1589 /* just A..B */
1590 b_flags = flags;
1591 a_flags = flags_exclude;
1592 } else {
1593 /* A...B -- find merge bases between the two */
1594 struct commit *a, *b;
1595 struct commit_list *exclude;
1597 a = lookup_commit_reference(the_repository, &a_obj->oid);
1598 b = lookup_commit_reference(the_repository, &b_obj->oid);
1599 if (!a || !b)
1600 return dotdot_missing(arg, dotdot, revs, symmetric);
1602 exclude = get_merge_bases(a, b);
1603 add_rev_cmdline_list(revs, exclude, REV_CMD_MERGE_BASE,
1604 flags_exclude);
1605 add_pending_commit_list(revs, exclude, flags_exclude);
1606 free_commit_list(exclude);
1608 b_flags = flags;
1609 a_flags = flags | SYMMETRIC_LEFT;
1612 a_obj->flags |= a_flags;
1613 b_obj->flags |= b_flags;
1614 add_rev_cmdline(revs, a_obj, a_name, REV_CMD_LEFT, a_flags);
1615 add_rev_cmdline(revs, b_obj, b_name, REV_CMD_RIGHT, b_flags);
1616 add_pending_object_with_path(revs, a_obj, a_name, a_oc->mode, a_oc->path);
1617 add_pending_object_with_path(revs, b_obj, b_name, b_oc->mode, b_oc->path);
1618 return 0;
1621 static int handle_dotdot(const char *arg,
1622 struct rev_info *revs, int flags,
1623 int cant_be_filename)
1625 struct object_context a_oc, b_oc;
1626 char *dotdot = strstr(arg, "..");
1627 int ret;
1629 if (!dotdot)
1630 return -1;
1632 memset(&a_oc, 0, sizeof(a_oc));
1633 memset(&b_oc, 0, sizeof(b_oc));
1635 *dotdot = '\0';
1636 ret = handle_dotdot_1(arg, dotdot, revs, flags, cant_be_filename,
1637 &a_oc, &b_oc);
1638 *dotdot = '.';
1640 free(a_oc.path);
1641 free(b_oc.path);
1643 return ret;
1646 int handle_revision_arg(const char *arg_, struct rev_info *revs, int flags, unsigned revarg_opt)
1648 struct object_context oc;
1649 char *mark;
1650 struct object *object;
1651 struct object_id oid;
1652 int local_flags;
1653 const char *arg = arg_;
1654 int cant_be_filename = revarg_opt & REVARG_CANNOT_BE_FILENAME;
1655 unsigned get_sha1_flags = GET_OID_RECORD_PATH;
1657 flags = flags & UNINTERESTING ? flags | BOTTOM : flags & ~BOTTOM;
1659 if (!cant_be_filename && !strcmp(arg, "..")) {
1661 * Just ".."? That is not a range but the
1662 * pathspec for the parent directory.
1664 return -1;
1667 if (!handle_dotdot(arg, revs, flags, revarg_opt))
1668 return 0;
1670 mark = strstr(arg, "^@");
1671 if (mark && !mark[2]) {
1672 *mark = 0;
1673 if (add_parents_only(revs, arg, flags, 0))
1674 return 0;
1675 *mark = '^';
1677 mark = strstr(arg, "^!");
1678 if (mark && !mark[2]) {
1679 *mark = 0;
1680 if (!add_parents_only(revs, arg, flags ^ (UNINTERESTING | BOTTOM), 0))
1681 *mark = '^';
1683 mark = strstr(arg, "^-");
1684 if (mark) {
1685 int exclude_parent = 1;
1687 if (mark[2]) {
1688 char *end;
1689 exclude_parent = strtoul(mark + 2, &end, 10);
1690 if (*end != '\0' || !exclude_parent)
1691 return -1;
1694 *mark = 0;
1695 if (!add_parents_only(revs, arg, flags ^ (UNINTERESTING | BOTTOM), exclude_parent))
1696 *mark = '^';
1699 local_flags = 0;
1700 if (*arg == '^') {
1701 local_flags = UNINTERESTING | BOTTOM;
1702 arg++;
1705 if (revarg_opt & REVARG_COMMITTISH)
1706 get_sha1_flags |= GET_OID_COMMITTISH;
1708 if (get_oid_with_context(arg, get_sha1_flags, &oid, &oc))
1709 return revs->ignore_missing ? 0 : -1;
1710 if (!cant_be_filename)
1711 verify_non_filename(revs->prefix, arg);
1712 object = get_reference(revs, arg, &oid, flags ^ local_flags);
1713 add_rev_cmdline(revs, object, arg_, REV_CMD_REV, flags ^ local_flags);
1714 add_pending_object_with_path(revs, object, arg, oc.mode, oc.path);
1715 free(oc.path);
1716 return 0;
1719 static void read_pathspec_from_stdin(struct rev_info *revs, struct strbuf *sb,
1720 struct argv_array *prune)
1722 while (strbuf_getline(sb, stdin) != EOF)
1723 argv_array_push(prune, sb->buf);
1726 static void read_revisions_from_stdin(struct rev_info *revs,
1727 struct argv_array *prune)
1729 struct strbuf sb;
1730 int seen_dashdash = 0;
1731 int save_warning;
1733 save_warning = warn_on_object_refname_ambiguity;
1734 warn_on_object_refname_ambiguity = 0;
1736 strbuf_init(&sb, 1000);
1737 while (strbuf_getline(&sb, stdin) != EOF) {
1738 int len = sb.len;
1739 if (!len)
1740 break;
1741 if (sb.buf[0] == '-') {
1742 if (len == 2 && sb.buf[1] == '-') {
1743 seen_dashdash = 1;
1744 break;
1746 die("options not supported in --stdin mode");
1748 if (handle_revision_arg(sb.buf, revs, 0,
1749 REVARG_CANNOT_BE_FILENAME))
1750 die("bad revision '%s'", sb.buf);
1752 if (seen_dashdash)
1753 read_pathspec_from_stdin(revs, &sb, prune);
1755 strbuf_release(&sb);
1756 warn_on_object_refname_ambiguity = save_warning;
1759 static void add_grep(struct rev_info *revs, const char *ptn, enum grep_pat_token what)
1761 append_grep_pattern(&revs->grep_filter, ptn, "command line", 0, what);
1764 static void add_header_grep(struct rev_info *revs, enum grep_header_field field, const char *pattern)
1766 append_header_grep_pattern(&revs->grep_filter, field, pattern);
1769 static void add_message_grep(struct rev_info *revs, const char *pattern)
1771 add_grep(revs, pattern, GREP_PATTERN_BODY);
1774 static int handle_revision_opt(struct rev_info *revs, int argc, const char **argv,
1775 int *unkc, const char **unkv)
1777 const char *arg = argv[0];
1778 const char *optarg;
1779 int argcount;
1780 const unsigned hexsz = the_hash_algo->hexsz;
1782 /* pseudo revision arguments */
1783 if (!strcmp(arg, "--all") || !strcmp(arg, "--branches") ||
1784 !strcmp(arg, "--tags") || !strcmp(arg, "--remotes") ||
1785 !strcmp(arg, "--reflog") || !strcmp(arg, "--not") ||
1786 !strcmp(arg, "--no-walk") || !strcmp(arg, "--do-walk") ||
1787 !strcmp(arg, "--bisect") || starts_with(arg, "--glob=") ||
1788 !strcmp(arg, "--indexed-objects") ||
1789 starts_with(arg, "--exclude=") ||
1790 starts_with(arg, "--branches=") || starts_with(arg, "--tags=") ||
1791 starts_with(arg, "--remotes=") || starts_with(arg, "--no-walk="))
1793 unkv[(*unkc)++] = arg;
1794 return 1;
1797 if ((argcount = parse_long_opt("max-count", argv, &optarg))) {
1798 revs->max_count = atoi(optarg);
1799 revs->no_walk = 0;
1800 return argcount;
1801 } else if ((argcount = parse_long_opt("skip", argv, &optarg))) {
1802 revs->skip_count = atoi(optarg);
1803 return argcount;
1804 } else if ((*arg == '-') && isdigit(arg[1])) {
1805 /* accept -<digit>, like traditional "head" */
1806 if (strtol_i(arg + 1, 10, &revs->max_count) < 0 ||
1807 revs->max_count < 0)
1808 die("'%s': not a non-negative integer", arg + 1);
1809 revs->no_walk = 0;
1810 } else if (!strcmp(arg, "-n")) {
1811 if (argc <= 1)
1812 return error("-n requires an argument");
1813 revs->max_count = atoi(argv[1]);
1814 revs->no_walk = 0;
1815 return 2;
1816 } else if (skip_prefix(arg, "-n", &optarg)) {
1817 revs->max_count = atoi(optarg);
1818 revs->no_walk = 0;
1819 } else if ((argcount = parse_long_opt("max-age", argv, &optarg))) {
1820 revs->max_age = atoi(optarg);
1821 return argcount;
1822 } else if ((argcount = parse_long_opt("since", argv, &optarg))) {
1823 revs->max_age = approxidate(optarg);
1824 return argcount;
1825 } else if ((argcount = parse_long_opt("after", argv, &optarg))) {
1826 revs->max_age = approxidate(optarg);
1827 return argcount;
1828 } else if ((argcount = parse_long_opt("min-age", argv, &optarg))) {
1829 revs->min_age = atoi(optarg);
1830 return argcount;
1831 } else if ((argcount = parse_long_opt("before", argv, &optarg))) {
1832 revs->min_age = approxidate(optarg);
1833 return argcount;
1834 } else if ((argcount = parse_long_opt("until", argv, &optarg))) {
1835 revs->min_age = approxidate(optarg);
1836 return argcount;
1837 } else if (!strcmp(arg, "--first-parent")) {
1838 revs->first_parent_only = 1;
1839 } else if (!strcmp(arg, "--ancestry-path")) {
1840 revs->ancestry_path = 1;
1841 revs->simplify_history = 0;
1842 revs->limited = 1;
1843 } else if (!strcmp(arg, "-g") || !strcmp(arg, "--walk-reflogs")) {
1844 init_reflog_walk(&revs->reflog_info);
1845 } else if (!strcmp(arg, "--default")) {
1846 if (argc <= 1)
1847 return error("bad --default argument");
1848 revs->def = argv[1];
1849 return 2;
1850 } else if (!strcmp(arg, "--merge")) {
1851 revs->show_merge = 1;
1852 } else if (!strcmp(arg, "--topo-order")) {
1853 revs->sort_order = REV_SORT_IN_GRAPH_ORDER;
1854 revs->topo_order = 1;
1855 } else if (!strcmp(arg, "--simplify-merges")) {
1856 revs->simplify_merges = 1;
1857 revs->topo_order = 1;
1858 revs->rewrite_parents = 1;
1859 revs->simplify_history = 0;
1860 revs->limited = 1;
1861 } else if (!strcmp(arg, "--simplify-by-decoration")) {
1862 revs->simplify_merges = 1;
1863 revs->topo_order = 1;
1864 revs->rewrite_parents = 1;
1865 revs->simplify_history = 0;
1866 revs->simplify_by_decoration = 1;
1867 revs->limited = 1;
1868 revs->prune = 1;
1869 load_ref_decorations(NULL, DECORATE_SHORT_REFS);
1870 } else if (!strcmp(arg, "--date-order")) {
1871 revs->sort_order = REV_SORT_BY_COMMIT_DATE;
1872 revs->topo_order = 1;
1873 } else if (!strcmp(arg, "--author-date-order")) {
1874 revs->sort_order = REV_SORT_BY_AUTHOR_DATE;
1875 revs->topo_order = 1;
1876 } else if (!strcmp(arg, "--early-output")) {
1877 revs->early_output = 100;
1878 revs->topo_order = 1;
1879 } else if (skip_prefix(arg, "--early-output=", &optarg)) {
1880 if (strtoul_ui(optarg, 10, &revs->early_output) < 0)
1881 die("'%s': not a non-negative integer", optarg);
1882 revs->topo_order = 1;
1883 } else if (!strcmp(arg, "--parents")) {
1884 revs->rewrite_parents = 1;
1885 revs->print_parents = 1;
1886 } else if (!strcmp(arg, "--dense")) {
1887 revs->dense = 1;
1888 } else if (!strcmp(arg, "--sparse")) {
1889 revs->dense = 0;
1890 } else if (!strcmp(arg, "--in-commit-order")) {
1891 revs->tree_blobs_in_commit_order = 1;
1892 } else if (!strcmp(arg, "--remove-empty")) {
1893 revs->remove_empty_trees = 1;
1894 } else if (!strcmp(arg, "--merges")) {
1895 revs->min_parents = 2;
1896 } else if (!strcmp(arg, "--no-merges")) {
1897 revs->max_parents = 1;
1898 } else if (skip_prefix(arg, "--min-parents=", &optarg)) {
1899 revs->min_parents = atoi(optarg);
1900 } else if (!strcmp(arg, "--no-min-parents")) {
1901 revs->min_parents = 0;
1902 } else if (skip_prefix(arg, "--max-parents=", &optarg)) {
1903 revs->max_parents = atoi(optarg);
1904 } else if (!strcmp(arg, "--no-max-parents")) {
1905 revs->max_parents = -1;
1906 } else if (!strcmp(arg, "--boundary")) {
1907 revs->boundary = 1;
1908 } else if (!strcmp(arg, "--left-right")) {
1909 revs->left_right = 1;
1910 } else if (!strcmp(arg, "--left-only")) {
1911 if (revs->right_only)
1912 die("--left-only is incompatible with --right-only"
1913 " or --cherry");
1914 revs->left_only = 1;
1915 } else if (!strcmp(arg, "--right-only")) {
1916 if (revs->left_only)
1917 die("--right-only is incompatible with --left-only");
1918 revs->right_only = 1;
1919 } else if (!strcmp(arg, "--cherry")) {
1920 if (revs->left_only)
1921 die("--cherry is incompatible with --left-only");
1922 revs->cherry_mark = 1;
1923 revs->right_only = 1;
1924 revs->max_parents = 1;
1925 revs->limited = 1;
1926 } else if (!strcmp(arg, "--count")) {
1927 revs->count = 1;
1928 } else if (!strcmp(arg, "--cherry-mark")) {
1929 if (revs->cherry_pick)
1930 die("--cherry-mark is incompatible with --cherry-pick");
1931 revs->cherry_mark = 1;
1932 revs->limited = 1; /* needs limit_list() */
1933 } else if (!strcmp(arg, "--cherry-pick")) {
1934 if (revs->cherry_mark)
1935 die("--cherry-pick is incompatible with --cherry-mark");
1936 revs->cherry_pick = 1;
1937 revs->limited = 1;
1938 } else if (!strcmp(arg, "--objects")) {
1939 revs->tag_objects = 1;
1940 revs->tree_objects = 1;
1941 revs->blob_objects = 1;
1942 } else if (!strcmp(arg, "--objects-edge")) {
1943 revs->tag_objects = 1;
1944 revs->tree_objects = 1;
1945 revs->blob_objects = 1;
1946 revs->edge_hint = 1;
1947 } else if (!strcmp(arg, "--objects-edge-aggressive")) {
1948 revs->tag_objects = 1;
1949 revs->tree_objects = 1;
1950 revs->blob_objects = 1;
1951 revs->edge_hint = 1;
1952 revs->edge_hint_aggressive = 1;
1953 } else if (!strcmp(arg, "--verify-objects")) {
1954 revs->tag_objects = 1;
1955 revs->tree_objects = 1;
1956 revs->blob_objects = 1;
1957 revs->verify_objects = 1;
1958 } else if (!strcmp(arg, "--unpacked")) {
1959 revs->unpacked = 1;
1960 } else if (starts_with(arg, "--unpacked=")) {
1961 die("--unpacked=<packfile> no longer supported.");
1962 } else if (!strcmp(arg, "-r")) {
1963 revs->diff = 1;
1964 revs->diffopt.flags.recursive = 1;
1965 } else if (!strcmp(arg, "-t")) {
1966 revs->diff = 1;
1967 revs->diffopt.flags.recursive = 1;
1968 revs->diffopt.flags.tree_in_recursive = 1;
1969 } else if (!strcmp(arg, "-m")) {
1970 revs->ignore_merges = 0;
1971 } else if (!strcmp(arg, "-c")) {
1972 revs->diff = 1;
1973 revs->dense_combined_merges = 0;
1974 revs->combine_merges = 1;
1975 } else if (!strcmp(arg, "--cc")) {
1976 revs->diff = 1;
1977 revs->dense_combined_merges = 1;
1978 revs->combine_merges = 1;
1979 } else if (!strcmp(arg, "-v")) {
1980 revs->verbose_header = 1;
1981 } else if (!strcmp(arg, "--pretty")) {
1982 revs->verbose_header = 1;
1983 revs->pretty_given = 1;
1984 get_commit_format(NULL, revs);
1985 } else if (skip_prefix(arg, "--pretty=", &optarg) ||
1986 skip_prefix(arg, "--format=", &optarg)) {
1988 * Detached form ("--pretty X" as opposed to "--pretty=X")
1989 * not allowed, since the argument is optional.
1991 revs->verbose_header = 1;
1992 revs->pretty_given = 1;
1993 get_commit_format(optarg, revs);
1994 } else if (!strcmp(arg, "--expand-tabs")) {
1995 revs->expand_tabs_in_log = 8;
1996 } else if (!strcmp(arg, "--no-expand-tabs")) {
1997 revs->expand_tabs_in_log = 0;
1998 } else if (skip_prefix(arg, "--expand-tabs=", &arg)) {
1999 int val;
2000 if (strtol_i(arg, 10, &val) < 0 || val < 0)
2001 die("'%s': not a non-negative integer", arg);
2002 revs->expand_tabs_in_log = val;
2003 } else if (!strcmp(arg, "--show-notes") || !strcmp(arg, "--notes")) {
2004 revs->show_notes = 1;
2005 revs->show_notes_given = 1;
2006 revs->notes_opt.use_default_notes = 1;
2007 } else if (!strcmp(arg, "--show-signature")) {
2008 revs->show_signature = 1;
2009 } else if (!strcmp(arg, "--no-show-signature")) {
2010 revs->show_signature = 0;
2011 } else if (!strcmp(arg, "--show-linear-break")) {
2012 revs->break_bar = " ..........";
2013 revs->track_linear = 1;
2014 revs->track_first_time = 1;
2015 } else if (skip_prefix(arg, "--show-linear-break=", &optarg)) {
2016 revs->break_bar = xstrdup(optarg);
2017 revs->track_linear = 1;
2018 revs->track_first_time = 1;
2019 } else if (skip_prefix(arg, "--show-notes=", &optarg) ||
2020 skip_prefix(arg, "--notes=", &optarg)) {
2021 struct strbuf buf = STRBUF_INIT;
2022 revs->show_notes = 1;
2023 revs->show_notes_given = 1;
2024 if (starts_with(arg, "--show-notes=") &&
2025 revs->notes_opt.use_default_notes < 0)
2026 revs->notes_opt.use_default_notes = 1;
2027 strbuf_addstr(&buf, optarg);
2028 expand_notes_ref(&buf);
2029 string_list_append(&revs->notes_opt.extra_notes_refs,
2030 strbuf_detach(&buf, NULL));
2031 } else if (!strcmp(arg, "--no-notes")) {
2032 revs->show_notes = 0;
2033 revs->show_notes_given = 1;
2034 revs->notes_opt.use_default_notes = -1;
2035 /* we have been strdup'ing ourselves, so trick
2036 * string_list into free()ing strings */
2037 revs->notes_opt.extra_notes_refs.strdup_strings = 1;
2038 string_list_clear(&revs->notes_opt.extra_notes_refs, 0);
2039 revs->notes_opt.extra_notes_refs.strdup_strings = 0;
2040 } else if (!strcmp(arg, "--standard-notes")) {
2041 revs->show_notes_given = 1;
2042 revs->notes_opt.use_default_notes = 1;
2043 } else if (!strcmp(arg, "--no-standard-notes")) {
2044 revs->notes_opt.use_default_notes = 0;
2045 } else if (!strcmp(arg, "--oneline")) {
2046 revs->verbose_header = 1;
2047 get_commit_format("oneline", revs);
2048 revs->pretty_given = 1;
2049 revs->abbrev_commit = 1;
2050 } else if (!strcmp(arg, "--graph")) {
2051 revs->topo_order = 1;
2052 revs->rewrite_parents = 1;
2053 revs->graph = graph_init(revs);
2054 } else if (!strcmp(arg, "--root")) {
2055 revs->show_root_diff = 1;
2056 } else if (!strcmp(arg, "--no-commit-id")) {
2057 revs->no_commit_id = 1;
2058 } else if (!strcmp(arg, "--always")) {
2059 revs->always_show_header = 1;
2060 } else if (!strcmp(arg, "--no-abbrev")) {
2061 revs->abbrev = 0;
2062 } else if (!strcmp(arg, "--abbrev")) {
2063 revs->abbrev = DEFAULT_ABBREV;
2064 } else if (skip_prefix(arg, "--abbrev=", &optarg)) {
2065 revs->abbrev = strtoul(optarg, NULL, 10);
2066 if (revs->abbrev < MINIMUM_ABBREV)
2067 revs->abbrev = MINIMUM_ABBREV;
2068 else if (revs->abbrev > hexsz)
2069 revs->abbrev = hexsz;
2070 } else if (!strcmp(arg, "--abbrev-commit")) {
2071 revs->abbrev_commit = 1;
2072 revs->abbrev_commit_given = 1;
2073 } else if (!strcmp(arg, "--no-abbrev-commit")) {
2074 revs->abbrev_commit = 0;
2075 } else if (!strcmp(arg, "--full-diff")) {
2076 revs->diff = 1;
2077 revs->full_diff = 1;
2078 } else if (!strcmp(arg, "--full-history")) {
2079 revs->simplify_history = 0;
2080 } else if (!strcmp(arg, "--relative-date")) {
2081 revs->date_mode.type = DATE_RELATIVE;
2082 revs->date_mode_explicit = 1;
2083 } else if ((argcount = parse_long_opt("date", argv, &optarg))) {
2084 parse_date_format(optarg, &revs->date_mode);
2085 revs->date_mode_explicit = 1;
2086 return argcount;
2087 } else if (!strcmp(arg, "--log-size")) {
2088 revs->show_log_size = 1;
2091 * Grepping the commit log
2093 else if ((argcount = parse_long_opt("author", argv, &optarg))) {
2094 add_header_grep(revs, GREP_HEADER_AUTHOR, optarg);
2095 return argcount;
2096 } else if ((argcount = parse_long_opt("committer", argv, &optarg))) {
2097 add_header_grep(revs, GREP_HEADER_COMMITTER, optarg);
2098 return argcount;
2099 } else if ((argcount = parse_long_opt("grep-reflog", argv, &optarg))) {
2100 add_header_grep(revs, GREP_HEADER_REFLOG, optarg);
2101 return argcount;
2102 } else if ((argcount = parse_long_opt("grep", argv, &optarg))) {
2103 add_message_grep(revs, optarg);
2104 return argcount;
2105 } else if (!strcmp(arg, "--grep-debug")) {
2106 revs->grep_filter.debug = 1;
2107 } else if (!strcmp(arg, "--basic-regexp")) {
2108 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_BRE;
2109 } else if (!strcmp(arg, "--extended-regexp") || !strcmp(arg, "-E")) {
2110 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_ERE;
2111 } else if (!strcmp(arg, "--regexp-ignore-case") || !strcmp(arg, "-i")) {
2112 revs->grep_filter.ignore_case = 1;
2113 revs->diffopt.pickaxe_opts |= DIFF_PICKAXE_IGNORE_CASE;
2114 } else if (!strcmp(arg, "--fixed-strings") || !strcmp(arg, "-F")) {
2115 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_FIXED;
2116 } else if (!strcmp(arg, "--perl-regexp") || !strcmp(arg, "-P")) {
2117 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_PCRE;
2118 } else if (!strcmp(arg, "--all-match")) {
2119 revs->grep_filter.all_match = 1;
2120 } else if (!strcmp(arg, "--invert-grep")) {
2121 revs->invert_grep = 1;
2122 } else if ((argcount = parse_long_opt("encoding", argv, &optarg))) {
2123 if (strcmp(optarg, "none"))
2124 git_log_output_encoding = xstrdup(optarg);
2125 else
2126 git_log_output_encoding = "";
2127 return argcount;
2128 } else if (!strcmp(arg, "--reverse")) {
2129 revs->reverse ^= 1;
2130 } else if (!strcmp(arg, "--children")) {
2131 revs->children.name = "children";
2132 revs->limited = 1;
2133 } else if (!strcmp(arg, "--ignore-missing")) {
2134 revs->ignore_missing = 1;
2135 } else if (!strcmp(arg, "--exclude-promisor-objects")) {
2136 if (fetch_if_missing)
2137 BUG("exclude_promisor_objects can only be used when fetch_if_missing is 0");
2138 revs->exclude_promisor_objects = 1;
2139 } else {
2140 int opts = diff_opt_parse(&revs->diffopt, argv, argc, revs->prefix);
2141 if (!opts)
2142 unkv[(*unkc)++] = arg;
2143 return opts;
2145 if (revs->graph && revs->track_linear)
2146 die("--show-linear-break and --graph are incompatible");
2148 return 1;
2151 void parse_revision_opt(struct rev_info *revs, struct parse_opt_ctx_t *ctx,
2152 const struct option *options,
2153 const char * const usagestr[])
2155 int n = handle_revision_opt(revs, ctx->argc, ctx->argv,
2156 &ctx->cpidx, ctx->out);
2157 if (n <= 0) {
2158 error("unknown option `%s'", ctx->argv[0]);
2159 usage_with_options(usagestr, options);
2161 ctx->argv += n;
2162 ctx->argc -= n;
2165 static int for_each_bisect_ref(struct ref_store *refs, each_ref_fn fn,
2166 void *cb_data, const char *term)
2168 struct strbuf bisect_refs = STRBUF_INIT;
2169 int status;
2170 strbuf_addf(&bisect_refs, "refs/bisect/%s", term);
2171 status = refs_for_each_fullref_in(refs, bisect_refs.buf, fn, cb_data, 0);
2172 strbuf_release(&bisect_refs);
2173 return status;
2176 static int for_each_bad_bisect_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
2178 return for_each_bisect_ref(refs, fn, cb_data, term_bad);
2181 static int for_each_good_bisect_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
2183 return for_each_bisect_ref(refs, fn, cb_data, term_good);
2186 static int handle_revision_pseudo_opt(const char *submodule,
2187 struct rev_info *revs,
2188 int argc, const char **argv, int *flags)
2190 const char *arg = argv[0];
2191 const char *optarg;
2192 struct ref_store *refs;
2193 int argcount;
2195 if (submodule) {
2197 * We need some something like get_submodule_worktrees()
2198 * before we can go through all worktrees of a submodule,
2199 * .e.g with adding all HEADs from --all, which is not
2200 * supported right now, so stick to single worktree.
2202 if (!revs->single_worktree)
2203 BUG("--single-worktree cannot be used together with submodule");
2204 refs = get_submodule_ref_store(submodule);
2205 } else
2206 refs = get_main_ref_store(the_repository);
2209 * NOTE!
2211 * Commands like "git shortlog" will not accept the options below
2212 * unless parse_revision_opt queues them (as opposed to erroring
2213 * out).
2215 * When implementing your new pseudo-option, remember to
2216 * register it in the list at the top of handle_revision_opt.
2218 if (!strcmp(arg, "--all")) {
2219 handle_refs(refs, revs, *flags, refs_for_each_ref);
2220 handle_refs(refs, revs, *flags, refs_head_ref);
2221 if (!revs->single_worktree) {
2222 struct all_refs_cb cb;
2224 init_all_refs_cb(&cb, revs, *flags);
2225 other_head_refs(handle_one_ref, &cb);
2227 clear_ref_exclusion(&revs->ref_excludes);
2228 } else if (!strcmp(arg, "--branches")) {
2229 handle_refs(refs, revs, *flags, refs_for_each_branch_ref);
2230 clear_ref_exclusion(&revs->ref_excludes);
2231 } else if (!strcmp(arg, "--bisect")) {
2232 read_bisect_terms(&term_bad, &term_good);
2233 handle_refs(refs, revs, *flags, for_each_bad_bisect_ref);
2234 handle_refs(refs, revs, *flags ^ (UNINTERESTING | BOTTOM),
2235 for_each_good_bisect_ref);
2236 revs->bisect = 1;
2237 } else if (!strcmp(arg, "--tags")) {
2238 handle_refs(refs, revs, *flags, refs_for_each_tag_ref);
2239 clear_ref_exclusion(&revs->ref_excludes);
2240 } else if (!strcmp(arg, "--remotes")) {
2241 handle_refs(refs, revs, *flags, refs_for_each_remote_ref);
2242 clear_ref_exclusion(&revs->ref_excludes);
2243 } else if ((argcount = parse_long_opt("glob", argv, &optarg))) {
2244 struct all_refs_cb cb;
2245 init_all_refs_cb(&cb, revs, *flags);
2246 for_each_glob_ref(handle_one_ref, optarg, &cb);
2247 clear_ref_exclusion(&revs->ref_excludes);
2248 return argcount;
2249 } else if ((argcount = parse_long_opt("exclude", argv, &optarg))) {
2250 add_ref_exclusion(&revs->ref_excludes, optarg);
2251 return argcount;
2252 } else if (skip_prefix(arg, "--branches=", &optarg)) {
2253 struct all_refs_cb cb;
2254 init_all_refs_cb(&cb, revs, *flags);
2255 for_each_glob_ref_in(handle_one_ref, optarg, "refs/heads/", &cb);
2256 clear_ref_exclusion(&revs->ref_excludes);
2257 } else if (skip_prefix(arg, "--tags=", &optarg)) {
2258 struct all_refs_cb cb;
2259 init_all_refs_cb(&cb, revs, *flags);
2260 for_each_glob_ref_in(handle_one_ref, optarg, "refs/tags/", &cb);
2261 clear_ref_exclusion(&revs->ref_excludes);
2262 } else if (skip_prefix(arg, "--remotes=", &optarg)) {
2263 struct all_refs_cb cb;
2264 init_all_refs_cb(&cb, revs, *flags);
2265 for_each_glob_ref_in(handle_one_ref, optarg, "refs/remotes/", &cb);
2266 clear_ref_exclusion(&revs->ref_excludes);
2267 } else if (!strcmp(arg, "--reflog")) {
2268 add_reflogs_to_pending(revs, *flags);
2269 } else if (!strcmp(arg, "--indexed-objects")) {
2270 add_index_objects_to_pending(revs, *flags);
2271 } else if (!strcmp(arg, "--not")) {
2272 *flags ^= UNINTERESTING | BOTTOM;
2273 } else if (!strcmp(arg, "--no-walk")) {
2274 revs->no_walk = REVISION_WALK_NO_WALK_SORTED;
2275 } else if (skip_prefix(arg, "--no-walk=", &optarg)) {
2277 * Detached form ("--no-walk X" as opposed to "--no-walk=X")
2278 * not allowed, since the argument is optional.
2280 if (!strcmp(optarg, "sorted"))
2281 revs->no_walk = REVISION_WALK_NO_WALK_SORTED;
2282 else if (!strcmp(optarg, "unsorted"))
2283 revs->no_walk = REVISION_WALK_NO_WALK_UNSORTED;
2284 else
2285 return error("invalid argument to --no-walk");
2286 } else if (!strcmp(arg, "--do-walk")) {
2287 revs->no_walk = 0;
2288 } else if (!strcmp(arg, "--single-worktree")) {
2289 revs->single_worktree = 1;
2290 } else {
2291 return 0;
2294 return 1;
2297 static void NORETURN diagnose_missing_default(const char *def)
2299 int flags;
2300 const char *refname;
2302 refname = resolve_ref_unsafe(def, 0, NULL, &flags);
2303 if (!refname || !(flags & REF_ISSYMREF) || (flags & REF_ISBROKEN))
2304 die(_("your current branch appears to be broken"));
2306 skip_prefix(refname, "refs/heads/", &refname);
2307 die(_("your current branch '%s' does not have any commits yet"),
2308 refname);
2312 * Parse revision information, filling in the "rev_info" structure,
2313 * and removing the used arguments from the argument list.
2315 * Returns the number of arguments left that weren't recognized
2316 * (which are also moved to the head of the argument list)
2318 int setup_revisions(int argc, const char **argv, struct rev_info *revs, struct setup_revision_opt *opt)
2320 int i, flags, left, seen_dashdash, read_from_stdin, got_rev_arg = 0, revarg_opt;
2321 struct argv_array prune_data = ARGV_ARRAY_INIT;
2322 const char *submodule = NULL;
2324 if (opt)
2325 submodule = opt->submodule;
2327 /* First, search for "--" */
2328 if (opt && opt->assume_dashdash) {
2329 seen_dashdash = 1;
2330 } else {
2331 seen_dashdash = 0;
2332 for (i = 1; i < argc; i++) {
2333 const char *arg = argv[i];
2334 if (strcmp(arg, "--"))
2335 continue;
2336 argv[i] = NULL;
2337 argc = i;
2338 if (argv[i + 1])
2339 argv_array_pushv(&prune_data, argv + i + 1);
2340 seen_dashdash = 1;
2341 break;
2345 /* Second, deal with arguments and options */
2346 flags = 0;
2347 revarg_opt = opt ? opt->revarg_opt : 0;
2348 if (seen_dashdash)
2349 revarg_opt |= REVARG_CANNOT_BE_FILENAME;
2350 read_from_stdin = 0;
2351 for (left = i = 1; i < argc; i++) {
2352 const char *arg = argv[i];
2353 if (*arg == '-') {
2354 int opts;
2356 opts = handle_revision_pseudo_opt(submodule,
2357 revs, argc - i, argv + i,
2358 &flags);
2359 if (opts > 0) {
2360 i += opts - 1;
2361 continue;
2364 if (!strcmp(arg, "--stdin")) {
2365 if (revs->disable_stdin) {
2366 argv[left++] = arg;
2367 continue;
2369 if (read_from_stdin++)
2370 die("--stdin given twice?");
2371 read_revisions_from_stdin(revs, &prune_data);
2372 continue;
2375 opts = handle_revision_opt(revs, argc - i, argv + i, &left, argv);
2376 if (opts > 0) {
2377 i += opts - 1;
2378 continue;
2380 if (opts < 0)
2381 exit(128);
2382 continue;
2386 if (handle_revision_arg(arg, revs, flags, revarg_opt)) {
2387 int j;
2388 if (seen_dashdash || *arg == '^')
2389 die("bad revision '%s'", arg);
2391 /* If we didn't have a "--":
2392 * (1) all filenames must exist;
2393 * (2) all rev-args must not be interpretable
2394 * as a valid filename.
2395 * but the latter we have checked in the main loop.
2397 for (j = i; j < argc; j++)
2398 verify_filename(revs->prefix, argv[j], j == i);
2400 argv_array_pushv(&prune_data, argv + i);
2401 break;
2403 else
2404 got_rev_arg = 1;
2407 if (prune_data.argc) {
2409 * If we need to introduce the magic "a lone ':' means no
2410 * pathspec whatsoever", here is the place to do so.
2412 * if (prune_data.nr == 1 && !strcmp(prune_data[0], ":")) {
2413 * prune_data.nr = 0;
2414 * prune_data.alloc = 0;
2415 * free(prune_data.path);
2416 * prune_data.path = NULL;
2417 * } else {
2418 * terminate prune_data.alloc with NULL and
2419 * call init_pathspec() to set revs->prune_data here.
2422 parse_pathspec(&revs->prune_data, 0, 0,
2423 revs->prefix, prune_data.argv);
2425 argv_array_clear(&prune_data);
2427 if (revs->def == NULL)
2428 revs->def = opt ? opt->def : NULL;
2429 if (opt && opt->tweak)
2430 opt->tweak(revs, opt);
2431 if (revs->show_merge)
2432 prepare_show_merge(revs);
2433 if (revs->def && !revs->pending.nr && !revs->rev_input_given && !got_rev_arg) {
2434 struct object_id oid;
2435 struct object *object;
2436 struct object_context oc;
2437 if (get_oid_with_context(revs->def, 0, &oid, &oc))
2438 diagnose_missing_default(revs->def);
2439 object = get_reference(revs, revs->def, &oid, 0);
2440 add_pending_object_with_mode(revs, object, revs->def, oc.mode);
2443 /* Did the user ask for any diff output? Run the diff! */
2444 if (revs->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT)
2445 revs->diff = 1;
2447 /* Pickaxe, diff-filter and rename following need diffs */
2448 if ((revs->diffopt.pickaxe_opts & DIFF_PICKAXE_KINDS_MASK) ||
2449 revs->diffopt.filter ||
2450 revs->diffopt.flags.follow_renames)
2451 revs->diff = 1;
2453 if (revs->diffopt.objfind)
2454 revs->simplify_history = 0;
2456 if (revs->topo_order)
2457 revs->limited = 1;
2459 if (revs->prune_data.nr) {
2460 copy_pathspec(&revs->pruning.pathspec, &revs->prune_data);
2461 /* Can't prune commits with rename following: the paths change.. */
2462 if (!revs->diffopt.flags.follow_renames)
2463 revs->prune = 1;
2464 if (!revs->full_diff)
2465 copy_pathspec(&revs->diffopt.pathspec,
2466 &revs->prune_data);
2468 if (revs->combine_merges)
2469 revs->ignore_merges = 0;
2470 revs->diffopt.abbrev = revs->abbrev;
2472 if (revs->line_level_traverse) {
2473 revs->limited = 1;
2474 revs->topo_order = 1;
2477 diff_setup_done(&revs->diffopt);
2479 grep_commit_pattern_type(GREP_PATTERN_TYPE_UNSPECIFIED,
2480 &revs->grep_filter);
2481 compile_grep_patterns(&revs->grep_filter);
2483 if (revs->reverse && revs->reflog_info)
2484 die("cannot combine --reverse with --walk-reflogs");
2485 if (revs->reflog_info && revs->limited)
2486 die("cannot combine --walk-reflogs with history-limiting options");
2487 if (revs->rewrite_parents && revs->children.name)
2488 die("cannot combine --parents and --children");
2491 * Limitations on the graph functionality
2493 if (revs->reverse && revs->graph)
2494 die("cannot combine --reverse with --graph");
2496 if (revs->reflog_info && revs->graph)
2497 die("cannot combine --walk-reflogs with --graph");
2498 if (revs->no_walk && revs->graph)
2499 die("cannot combine --no-walk with --graph");
2500 if (!revs->reflog_info && revs->grep_filter.use_reflog_filter)
2501 die("cannot use --grep-reflog without --walk-reflogs");
2503 if (revs->first_parent_only && revs->bisect)
2504 die(_("--first-parent is incompatible with --bisect"));
2506 if (revs->expand_tabs_in_log < 0)
2507 revs->expand_tabs_in_log = revs->expand_tabs_in_log_default;
2509 return left;
2512 static void add_child(struct rev_info *revs, struct commit *parent, struct commit *child)
2514 struct commit_list *l = xcalloc(1, sizeof(*l));
2516 l->item = child;
2517 l->next = add_decoration(&revs->children, &parent->object, l);
2520 static int remove_duplicate_parents(struct rev_info *revs, struct commit *commit)
2522 struct treesame_state *ts = lookup_decoration(&revs->treesame, &commit->object);
2523 struct commit_list **pp, *p;
2524 int surviving_parents;
2526 /* Examine existing parents while marking ones we have seen... */
2527 pp = &commit->parents;
2528 surviving_parents = 0;
2529 while ((p = *pp) != NULL) {
2530 struct commit *parent = p->item;
2531 if (parent->object.flags & TMP_MARK) {
2532 *pp = p->next;
2533 if (ts)
2534 compact_treesame(revs, commit, surviving_parents);
2535 continue;
2537 parent->object.flags |= TMP_MARK;
2538 surviving_parents++;
2539 pp = &p->next;
2541 /* clear the temporary mark */
2542 for (p = commit->parents; p; p = p->next) {
2543 p->item->object.flags &= ~TMP_MARK;
2545 /* no update_treesame() - removing duplicates can't affect TREESAME */
2546 return surviving_parents;
2549 struct merge_simplify_state {
2550 struct commit *simplified;
2553 static struct merge_simplify_state *locate_simplify_state(struct rev_info *revs, struct commit *commit)
2555 struct merge_simplify_state *st;
2557 st = lookup_decoration(&revs->merge_simplification, &commit->object);
2558 if (!st) {
2559 st = xcalloc(1, sizeof(*st));
2560 add_decoration(&revs->merge_simplification, &commit->object, st);
2562 return st;
2565 static int mark_redundant_parents(struct rev_info *revs, struct commit *commit)
2567 struct commit_list *h = reduce_heads(commit->parents);
2568 int i = 0, marked = 0;
2569 struct commit_list *po, *pn;
2571 /* Want these for sanity-checking only */
2572 int orig_cnt = commit_list_count(commit->parents);
2573 int cnt = commit_list_count(h);
2576 * Not ready to remove items yet, just mark them for now, based
2577 * on the output of reduce_heads(). reduce_heads outputs the reduced
2578 * set in its original order, so this isn't too hard.
2580 po = commit->parents;
2581 pn = h;
2582 while (po) {
2583 if (pn && po->item == pn->item) {
2584 pn = pn->next;
2585 i++;
2586 } else {
2587 po->item->object.flags |= TMP_MARK;
2588 marked++;
2590 po=po->next;
2593 if (i != cnt || cnt+marked != orig_cnt)
2594 die("mark_redundant_parents %d %d %d %d", orig_cnt, cnt, i, marked);
2596 free_commit_list(h);
2598 return marked;
2601 static int mark_treesame_root_parents(struct rev_info *revs, struct commit *commit)
2603 struct commit_list *p;
2604 int marked = 0;
2606 for (p = commit->parents; p; p = p->next) {
2607 struct commit *parent = p->item;
2608 if (!parent->parents && (parent->object.flags & TREESAME)) {
2609 parent->object.flags |= TMP_MARK;
2610 marked++;
2614 return marked;
2618 * Awkward naming - this means one parent we are TREESAME to.
2619 * cf mark_treesame_root_parents: root parents that are TREESAME (to an
2620 * empty tree). Better name suggestions?
2622 static int leave_one_treesame_to_parent(struct rev_info *revs, struct commit *commit)
2624 struct treesame_state *ts = lookup_decoration(&revs->treesame, &commit->object);
2625 struct commit *unmarked = NULL, *marked = NULL;
2626 struct commit_list *p;
2627 unsigned n;
2629 for (p = commit->parents, n = 0; p; p = p->next, n++) {
2630 if (ts->treesame[n]) {
2631 if (p->item->object.flags & TMP_MARK) {
2632 if (!marked)
2633 marked = p->item;
2634 } else {
2635 if (!unmarked) {
2636 unmarked = p->item;
2637 break;
2644 * If we are TREESAME to a marked-for-deletion parent, but not to any
2645 * unmarked parents, unmark the first TREESAME parent. This is the
2646 * parent that the default simplify_history==1 scan would have followed,
2647 * and it doesn't make sense to omit that path when asking for a
2648 * simplified full history. Retaining it improves the chances of
2649 * understanding odd missed merges that took an old version of a file.
2651 * Example:
2653 * I--------*X A modified the file, but mainline merge X used
2654 * \ / "-s ours", so took the version from I. X is
2655 * `-*A--' TREESAME to I and !TREESAME to A.
2657 * Default log from X would produce "I". Without this check,
2658 * --full-history --simplify-merges would produce "I-A-X", showing
2659 * the merge commit X and that it changed A, but not making clear that
2660 * it had just taken the I version. With this check, the topology above
2661 * is retained.
2663 * Note that it is possible that the simplification chooses a different
2664 * TREESAME parent from the default, in which case this test doesn't
2665 * activate, and we _do_ drop the default parent. Example:
2667 * I------X A modified the file, but it was reverted in B,
2668 * \ / meaning mainline merge X is TREESAME to both
2669 * *A-*B parents.
2671 * Default log would produce "I" by following the first parent;
2672 * --full-history --simplify-merges will produce "I-A-B". But this is a
2673 * reasonable result - it presents a logical full history leading from
2674 * I to X, and X is not an important merge.
2676 if (!unmarked && marked) {
2677 marked->object.flags &= ~TMP_MARK;
2678 return 1;
2681 return 0;
2684 static int remove_marked_parents(struct rev_info *revs, struct commit *commit)
2686 struct commit_list **pp, *p;
2687 int nth_parent, removed = 0;
2689 pp = &commit->parents;
2690 nth_parent = 0;
2691 while ((p = *pp) != NULL) {
2692 struct commit *parent = p->item;
2693 if (parent->object.flags & TMP_MARK) {
2694 parent->object.flags &= ~TMP_MARK;
2695 *pp = p->next;
2696 free(p);
2697 removed++;
2698 compact_treesame(revs, commit, nth_parent);
2699 continue;
2701 pp = &p->next;
2702 nth_parent++;
2705 /* Removing parents can only increase TREESAMEness */
2706 if (removed && !(commit->object.flags & TREESAME))
2707 update_treesame(revs, commit);
2709 return nth_parent;
2712 static struct commit_list **simplify_one(struct rev_info *revs, struct commit *commit, struct commit_list **tail)
2714 struct commit_list *p;
2715 struct commit *parent;
2716 struct merge_simplify_state *st, *pst;
2717 int cnt;
2719 st = locate_simplify_state(revs, commit);
2722 * Have we handled this one?
2724 if (st->simplified)
2725 return tail;
2728 * An UNINTERESTING commit simplifies to itself, so does a
2729 * root commit. We do not rewrite parents of such commit
2730 * anyway.
2732 if ((commit->object.flags & UNINTERESTING) || !commit->parents) {
2733 st->simplified = commit;
2734 return tail;
2738 * Do we know what commit all of our parents that matter
2739 * should be rewritten to? Otherwise we are not ready to
2740 * rewrite this one yet.
2742 for (cnt = 0, p = commit->parents; p; p = p->next) {
2743 pst = locate_simplify_state(revs, p->item);
2744 if (!pst->simplified) {
2745 tail = &commit_list_insert(p->item, tail)->next;
2746 cnt++;
2748 if (revs->first_parent_only)
2749 break;
2751 if (cnt) {
2752 tail = &commit_list_insert(commit, tail)->next;
2753 return tail;
2757 * Rewrite our list of parents. Note that this cannot
2758 * affect our TREESAME flags in any way - a commit is
2759 * always TREESAME to its simplification.
2761 for (p = commit->parents; p; p = p->next) {
2762 pst = locate_simplify_state(revs, p->item);
2763 p->item = pst->simplified;
2764 if (revs->first_parent_only)
2765 break;
2768 if (revs->first_parent_only)
2769 cnt = 1;
2770 else
2771 cnt = remove_duplicate_parents(revs, commit);
2774 * It is possible that we are a merge and one side branch
2775 * does not have any commit that touches the given paths;
2776 * in such a case, the immediate parent from that branch
2777 * will be rewritten to be the merge base.
2779 * o----X X: the commit we are looking at;
2780 * / / o: a commit that touches the paths;
2781 * ---o----'
2783 * Further, a merge of an independent branch that doesn't
2784 * touch the path will reduce to a treesame root parent:
2786 * ----o----X X: the commit we are looking at;
2787 * / o: a commit that touches the paths;
2788 * r r: a root commit not touching the paths
2790 * Detect and simplify both cases.
2792 if (1 < cnt) {
2793 int marked = mark_redundant_parents(revs, commit);
2794 marked += mark_treesame_root_parents(revs, commit);
2795 if (marked)
2796 marked -= leave_one_treesame_to_parent(revs, commit);
2797 if (marked)
2798 cnt = remove_marked_parents(revs, commit);
2802 * A commit simplifies to itself if it is a root, if it is
2803 * UNINTERESTING, if it touches the given paths, or if it is a
2804 * merge and its parents don't simplify to one relevant commit
2805 * (the first two cases are already handled at the beginning of
2806 * this function).
2808 * Otherwise, it simplifies to what its sole relevant parent
2809 * simplifies to.
2811 if (!cnt ||
2812 (commit->object.flags & UNINTERESTING) ||
2813 !(commit->object.flags & TREESAME) ||
2814 (parent = one_relevant_parent(revs, commit->parents)) == NULL)
2815 st->simplified = commit;
2816 else {
2817 pst = locate_simplify_state(revs, parent);
2818 st->simplified = pst->simplified;
2820 return tail;
2823 static void simplify_merges(struct rev_info *revs)
2825 struct commit_list *list, *next;
2826 struct commit_list *yet_to_do, **tail;
2827 struct commit *commit;
2829 if (!revs->prune)
2830 return;
2832 /* feed the list reversed */
2833 yet_to_do = NULL;
2834 for (list = revs->commits; list; list = next) {
2835 commit = list->item;
2836 next = list->next;
2838 * Do not free(list) here yet; the original list
2839 * is used later in this function.
2841 commit_list_insert(commit, &yet_to_do);
2843 while (yet_to_do) {
2844 list = yet_to_do;
2845 yet_to_do = NULL;
2846 tail = &yet_to_do;
2847 while (list) {
2848 commit = pop_commit(&list);
2849 tail = simplify_one(revs, commit, tail);
2853 /* clean up the result, removing the simplified ones */
2854 list = revs->commits;
2855 revs->commits = NULL;
2856 tail = &revs->commits;
2857 while (list) {
2858 struct merge_simplify_state *st;
2860 commit = pop_commit(&list);
2861 st = locate_simplify_state(revs, commit);
2862 if (st->simplified == commit)
2863 tail = &commit_list_insert(commit, tail)->next;
2867 static void set_children(struct rev_info *revs)
2869 struct commit_list *l;
2870 for (l = revs->commits; l; l = l->next) {
2871 struct commit *commit = l->item;
2872 struct commit_list *p;
2874 for (p = commit->parents; p; p = p->next)
2875 add_child(revs, p->item, commit);
2879 void reset_revision_walk(void)
2881 clear_object_flags(SEEN | ADDED | SHOWN);
2884 static int mark_uninteresting(const struct object_id *oid,
2885 struct packed_git *pack,
2886 uint32_t pos,
2887 void *unused)
2889 struct object *o = parse_object(the_repository, oid);
2890 o->flags |= UNINTERESTING | SEEN;
2891 return 0;
2894 int prepare_revision_walk(struct rev_info *revs)
2896 int i;
2897 struct object_array old_pending;
2898 struct commit_list **next = &revs->commits;
2900 memcpy(&old_pending, &revs->pending, sizeof(old_pending));
2901 revs->pending.nr = 0;
2902 revs->pending.alloc = 0;
2903 revs->pending.objects = NULL;
2904 for (i = 0; i < old_pending.nr; i++) {
2905 struct object_array_entry *e = old_pending.objects + i;
2906 struct commit *commit = handle_commit(revs, e);
2907 if (commit) {
2908 if (!(commit->object.flags & SEEN)) {
2909 commit->object.flags |= SEEN;
2910 next = commit_list_append(commit, next);
2914 object_array_clear(&old_pending);
2916 /* Signal whether we need per-parent treesame decoration */
2917 if (revs->simplify_merges ||
2918 (revs->limited && limiting_can_increase_treesame(revs)))
2919 revs->treesame.name = "treesame";
2921 if (revs->exclude_promisor_objects) {
2922 for_each_packed_object(mark_uninteresting, NULL,
2923 FOR_EACH_OBJECT_PROMISOR_ONLY);
2926 if (revs->no_walk != REVISION_WALK_NO_WALK_UNSORTED)
2927 commit_list_sort_by_date(&revs->commits);
2928 if (revs->no_walk)
2929 return 0;
2930 if (revs->limited)
2931 if (limit_list(revs) < 0)
2932 return -1;
2933 if (revs->topo_order)
2934 sort_in_topological_order(&revs->commits, revs->sort_order);
2935 if (revs->line_level_traverse)
2936 line_log_filter(revs);
2937 if (revs->simplify_merges)
2938 simplify_merges(revs);
2939 if (revs->children.name)
2940 set_children(revs);
2941 return 0;
2944 static enum rewrite_result rewrite_one(struct rev_info *revs, struct commit **pp)
2946 struct commit_list *cache = NULL;
2948 for (;;) {
2949 struct commit *p = *pp;
2950 if (!revs->limited)
2951 if (add_parents_to_list(revs, p, &revs->commits, &cache) < 0)
2952 return rewrite_one_error;
2953 if (p->object.flags & UNINTERESTING)
2954 return rewrite_one_ok;
2955 if (!(p->object.flags & TREESAME))
2956 return rewrite_one_ok;
2957 if (!p->parents)
2958 return rewrite_one_noparents;
2959 if ((p = one_relevant_parent(revs, p->parents)) == NULL)
2960 return rewrite_one_ok;
2961 *pp = p;
2965 int rewrite_parents(struct rev_info *revs, struct commit *commit,
2966 rewrite_parent_fn_t rewrite_parent)
2968 struct commit_list **pp = &commit->parents;
2969 while (*pp) {
2970 struct commit_list *parent = *pp;
2971 switch (rewrite_parent(revs, &parent->item)) {
2972 case rewrite_one_ok:
2973 break;
2974 case rewrite_one_noparents:
2975 *pp = parent->next;
2976 continue;
2977 case rewrite_one_error:
2978 return -1;
2980 pp = &parent->next;
2982 remove_duplicate_parents(revs, commit);
2983 return 0;
2986 static int commit_rewrite_person(struct strbuf *buf, const char *what, struct string_list *mailmap)
2988 char *person, *endp;
2989 size_t len, namelen, maillen;
2990 const char *name;
2991 const char *mail;
2992 struct ident_split ident;
2994 person = strstr(buf->buf, what);
2995 if (!person)
2996 return 0;
2998 person += strlen(what);
2999 endp = strchr(person, '\n');
3000 if (!endp)
3001 return 0;
3003 len = endp - person;
3005 if (split_ident_line(&ident, person, len))
3006 return 0;
3008 mail = ident.mail_begin;
3009 maillen = ident.mail_end - ident.mail_begin;
3010 name = ident.name_begin;
3011 namelen = ident.name_end - ident.name_begin;
3013 if (map_user(mailmap, &mail, &maillen, &name, &namelen)) {
3014 struct strbuf namemail = STRBUF_INIT;
3016 strbuf_addf(&namemail, "%.*s <%.*s>",
3017 (int)namelen, name, (int)maillen, mail);
3019 strbuf_splice(buf, ident.name_begin - buf->buf,
3020 ident.mail_end - ident.name_begin + 1,
3021 namemail.buf, namemail.len);
3023 strbuf_release(&namemail);
3025 return 1;
3028 return 0;
3031 static int commit_match(struct commit *commit, struct rev_info *opt)
3033 int retval;
3034 const char *encoding;
3035 const char *message;
3036 struct strbuf buf = STRBUF_INIT;
3038 if (!opt->grep_filter.pattern_list && !opt->grep_filter.header_list)
3039 return 1;
3041 /* Prepend "fake" headers as needed */
3042 if (opt->grep_filter.use_reflog_filter) {
3043 strbuf_addstr(&buf, "reflog ");
3044 get_reflog_message(&buf, opt->reflog_info);
3045 strbuf_addch(&buf, '\n');
3049 * We grep in the user's output encoding, under the assumption that it
3050 * is the encoding they are most likely to write their grep pattern
3051 * for. In addition, it means we will match the "notes" encoding below,
3052 * so we will not end up with a buffer that has two different encodings
3053 * in it.
3055 encoding = get_log_output_encoding();
3056 message = logmsg_reencode(commit, NULL, encoding);
3058 /* Copy the commit to temporary if we are using "fake" headers */
3059 if (buf.len)
3060 strbuf_addstr(&buf, message);
3062 if (opt->grep_filter.header_list && opt->mailmap) {
3063 if (!buf.len)
3064 strbuf_addstr(&buf, message);
3066 commit_rewrite_person(&buf, "\nauthor ", opt->mailmap);
3067 commit_rewrite_person(&buf, "\ncommitter ", opt->mailmap);
3070 /* Append "fake" message parts as needed */
3071 if (opt->show_notes) {
3072 if (!buf.len)
3073 strbuf_addstr(&buf, message);
3074 format_display_notes(&commit->object.oid, &buf, encoding, 1);
3078 * Find either in the original commit message, or in the temporary.
3079 * Note that we cast away the constness of "message" here. It is
3080 * const because it may come from the cached commit buffer. That's OK,
3081 * because we know that it is modifiable heap memory, and that while
3082 * grep_buffer may modify it for speed, it will restore any
3083 * changes before returning.
3085 if (buf.len)
3086 retval = grep_buffer(&opt->grep_filter, buf.buf, buf.len);
3087 else
3088 retval = grep_buffer(&opt->grep_filter,
3089 (char *)message, strlen(message));
3090 strbuf_release(&buf);
3091 unuse_commit_buffer(commit, message);
3092 return opt->invert_grep ? !retval : retval;
3095 static inline int want_ancestry(const struct rev_info *revs)
3097 return (revs->rewrite_parents || revs->children.name);
3101 * Return a timestamp to be used for --since/--until comparisons for this
3102 * commit, based on the revision options.
3104 static timestamp_t comparison_date(const struct rev_info *revs,
3105 struct commit *commit)
3107 return revs->reflog_info ?
3108 get_reflog_timestamp(revs->reflog_info) :
3109 commit->date;
3112 enum commit_action get_commit_action(struct rev_info *revs, struct commit *commit)
3114 if (commit->object.flags & SHOWN)
3115 return commit_ignore;
3116 if (revs->unpacked && has_object_pack(&commit->object.oid))
3117 return commit_ignore;
3118 if (commit->object.flags & UNINTERESTING)
3119 return commit_ignore;
3120 if (revs->min_age != -1 &&
3121 comparison_date(revs, commit) > revs->min_age)
3122 return commit_ignore;
3123 if (revs->min_parents || (revs->max_parents >= 0)) {
3124 int n = commit_list_count(commit->parents);
3125 if ((n < revs->min_parents) ||
3126 ((revs->max_parents >= 0) && (n > revs->max_parents)))
3127 return commit_ignore;
3129 if (!commit_match(commit, revs))
3130 return commit_ignore;
3131 if (revs->prune && revs->dense) {
3132 /* Commit without changes? */
3133 if (commit->object.flags & TREESAME) {
3134 int n;
3135 struct commit_list *p;
3136 /* drop merges unless we want parenthood */
3137 if (!want_ancestry(revs))
3138 return commit_ignore;
3140 * If we want ancestry, then need to keep any merges
3141 * between relevant commits to tie together topology.
3142 * For consistency with TREESAME and simplification
3143 * use "relevant" here rather than just INTERESTING,
3144 * to treat bottom commit(s) as part of the topology.
3146 for (n = 0, p = commit->parents; p; p = p->next)
3147 if (relevant_commit(p->item))
3148 if (++n >= 2)
3149 return commit_show;
3150 return commit_ignore;
3153 return commit_show;
3156 define_commit_slab(saved_parents, struct commit_list *);
3158 #define EMPTY_PARENT_LIST ((struct commit_list *)-1)
3161 * You may only call save_parents() once per commit (this is checked
3162 * for non-root commits).
3164 static void save_parents(struct rev_info *revs, struct commit *commit)
3166 struct commit_list **pp;
3168 if (!revs->saved_parents_slab) {
3169 revs->saved_parents_slab = xmalloc(sizeof(struct saved_parents));
3170 init_saved_parents(revs->saved_parents_slab);
3173 pp = saved_parents_at(revs->saved_parents_slab, commit);
3176 * When walking with reflogs, we may visit the same commit
3177 * several times: once for each appearance in the reflog.
3179 * In this case, save_parents() will be called multiple times.
3180 * We want to keep only the first set of parents. We need to
3181 * store a sentinel value for an empty (i.e., NULL) parent
3182 * list to distinguish it from a not-yet-saved list, however.
3184 if (*pp)
3185 return;
3186 if (commit->parents)
3187 *pp = copy_commit_list(commit->parents);
3188 else
3189 *pp = EMPTY_PARENT_LIST;
3192 static void free_saved_parents(struct rev_info *revs)
3194 if (revs->saved_parents_slab)
3195 clear_saved_parents(revs->saved_parents_slab);
3198 struct commit_list *get_saved_parents(struct rev_info *revs, const struct commit *commit)
3200 struct commit_list *parents;
3202 if (!revs->saved_parents_slab)
3203 return commit->parents;
3205 parents = *saved_parents_at(revs->saved_parents_slab, commit);
3206 if (parents == EMPTY_PARENT_LIST)
3207 return NULL;
3208 return parents;
3211 enum commit_action simplify_commit(struct rev_info *revs, struct commit *commit)
3213 enum commit_action action = get_commit_action(revs, commit);
3215 if (action == commit_show &&
3216 revs->prune && revs->dense && want_ancestry(revs)) {
3218 * --full-diff on simplified parents is no good: it
3219 * will show spurious changes from the commits that
3220 * were elided. So we save the parents on the side
3221 * when --full-diff is in effect.
3223 if (revs->full_diff)
3224 save_parents(revs, commit);
3225 if (rewrite_parents(revs, commit, rewrite_one) < 0)
3226 return commit_error;
3228 return action;
3231 static void track_linear(struct rev_info *revs, struct commit *commit)
3233 if (revs->track_first_time) {
3234 revs->linear = 1;
3235 revs->track_first_time = 0;
3236 } else {
3237 struct commit_list *p;
3238 for (p = revs->previous_parents; p; p = p->next)
3239 if (p->item == NULL || /* first commit */
3240 !oidcmp(&p->item->object.oid, &commit->object.oid))
3241 break;
3242 revs->linear = p != NULL;
3244 if (revs->reverse) {
3245 if (revs->linear)
3246 commit->object.flags |= TRACK_LINEAR;
3248 free_commit_list(revs->previous_parents);
3249 revs->previous_parents = copy_commit_list(commit->parents);
3252 static struct commit *get_revision_1(struct rev_info *revs)
3254 while (1) {
3255 struct commit *commit;
3257 if (revs->reflog_info)
3258 commit = next_reflog_entry(revs->reflog_info);
3259 else
3260 commit = pop_commit(&revs->commits);
3262 if (!commit)
3263 return NULL;
3265 if (revs->reflog_info)
3266 commit->object.flags &= ~(ADDED | SEEN | SHOWN);
3269 * If we haven't done the list limiting, we need to look at
3270 * the parents here. We also need to do the date-based limiting
3271 * that we'd otherwise have done in limit_list().
3273 if (!revs->limited) {
3274 if (revs->max_age != -1 &&
3275 comparison_date(revs, commit) < revs->max_age)
3276 continue;
3278 if (revs->reflog_info)
3279 try_to_simplify_commit(revs, commit);
3280 else if (add_parents_to_list(revs, commit, &revs->commits, NULL) < 0) {
3281 if (!revs->ignore_missing_links)
3282 die("Failed to traverse parents of commit %s",
3283 oid_to_hex(&commit->object.oid));
3287 switch (simplify_commit(revs, commit)) {
3288 case commit_ignore:
3289 continue;
3290 case commit_error:
3291 die("Failed to simplify parents of commit %s",
3292 oid_to_hex(&commit->object.oid));
3293 default:
3294 if (revs->track_linear)
3295 track_linear(revs, commit);
3296 return commit;
3302 * Return true for entries that have not yet been shown. (This is an
3303 * object_array_each_func_t.)
3305 static int entry_unshown(struct object_array_entry *entry, void *cb_data_unused)
3307 return !(entry->item->flags & SHOWN);
3311 * If array is on the verge of a realloc, garbage-collect any entries
3312 * that have already been shown to try to free up some space.
3314 static void gc_boundary(struct object_array *array)
3316 if (array->nr == array->alloc)
3317 object_array_filter(array, entry_unshown, NULL);
3320 static void create_boundary_commit_list(struct rev_info *revs)
3322 unsigned i;
3323 struct commit *c;
3324 struct object_array *array = &revs->boundary_commits;
3325 struct object_array_entry *objects = array->objects;
3328 * If revs->commits is non-NULL at this point, an error occurred in
3329 * get_revision_1(). Ignore the error and continue printing the
3330 * boundary commits anyway. (This is what the code has always
3331 * done.)
3333 if (revs->commits) {
3334 free_commit_list(revs->commits);
3335 revs->commits = NULL;
3339 * Put all of the actual boundary commits from revs->boundary_commits
3340 * into revs->commits
3342 for (i = 0; i < array->nr; i++) {
3343 c = (struct commit *)(objects[i].item);
3344 if (!c)
3345 continue;
3346 if (!(c->object.flags & CHILD_SHOWN))
3347 continue;
3348 if (c->object.flags & (SHOWN | BOUNDARY))
3349 continue;
3350 c->object.flags |= BOUNDARY;
3351 commit_list_insert(c, &revs->commits);
3355 * If revs->topo_order is set, sort the boundary commits
3356 * in topological order
3358 sort_in_topological_order(&revs->commits, revs->sort_order);
3361 static struct commit *get_revision_internal(struct rev_info *revs)
3363 struct commit *c = NULL;
3364 struct commit_list *l;
3366 if (revs->boundary == 2) {
3368 * All of the normal commits have already been returned,
3369 * and we are now returning boundary commits.
3370 * create_boundary_commit_list() has populated
3371 * revs->commits with the remaining commits to return.
3373 c = pop_commit(&revs->commits);
3374 if (c)
3375 c->object.flags |= SHOWN;
3376 return c;
3380 * If our max_count counter has reached zero, then we are done. We
3381 * don't simply return NULL because we still might need to show
3382 * boundary commits. But we want to avoid calling get_revision_1, which
3383 * might do a considerable amount of work finding the next commit only
3384 * for us to throw it away.
3386 * If it is non-zero, then either we don't have a max_count at all
3387 * (-1), or it is still counting, in which case we decrement.
3389 if (revs->max_count) {
3390 c = get_revision_1(revs);
3391 if (c) {
3392 while (revs->skip_count > 0) {
3393 revs->skip_count--;
3394 c = get_revision_1(revs);
3395 if (!c)
3396 break;
3400 if (revs->max_count > 0)
3401 revs->max_count--;
3404 if (c)
3405 c->object.flags |= SHOWN;
3407 if (!revs->boundary)
3408 return c;
3410 if (!c) {
3412 * get_revision_1() runs out the commits, and
3413 * we are done computing the boundaries.
3414 * switch to boundary commits output mode.
3416 revs->boundary = 2;
3419 * Update revs->commits to contain the list of
3420 * boundary commits.
3422 create_boundary_commit_list(revs);
3424 return get_revision_internal(revs);
3428 * boundary commits are the commits that are parents of the
3429 * ones we got from get_revision_1() but they themselves are
3430 * not returned from get_revision_1(). Before returning
3431 * 'c', we need to mark its parents that they could be boundaries.
3434 for (l = c->parents; l; l = l->next) {
3435 struct object *p;
3436 p = &(l->item->object);
3437 if (p->flags & (CHILD_SHOWN | SHOWN))
3438 continue;
3439 p->flags |= CHILD_SHOWN;
3440 gc_boundary(&revs->boundary_commits);
3441 add_object_array(p, NULL, &revs->boundary_commits);
3444 return c;
3447 struct commit *get_revision(struct rev_info *revs)
3449 struct commit *c;
3450 struct commit_list *reversed;
3452 if (revs->reverse) {
3453 reversed = NULL;
3454 while ((c = get_revision_internal(revs)))
3455 commit_list_insert(c, &reversed);
3456 revs->commits = reversed;
3457 revs->reverse = 0;
3458 revs->reverse_output_stage = 1;
3461 if (revs->reverse_output_stage) {
3462 c = pop_commit(&revs->commits);
3463 if (revs->track_linear)
3464 revs->linear = !!(c && c->object.flags & TRACK_LINEAR);
3465 return c;
3468 c = get_revision_internal(revs);
3469 if (c && revs->graph)
3470 graph_update(revs->graph, c);
3471 if (!c) {
3472 free_saved_parents(revs);
3473 if (revs->previous_parents) {
3474 free_commit_list(revs->previous_parents);
3475 revs->previous_parents = NULL;
3478 return c;
3481 char *get_revision_mark(const struct rev_info *revs, const struct commit *commit)
3483 if (commit->object.flags & BOUNDARY)
3484 return "-";
3485 else if (commit->object.flags & UNINTERESTING)
3486 return "^";
3487 else if (commit->object.flags & PATCHSAME)
3488 return "=";
3489 else if (!revs || revs->left_right) {
3490 if (commit->object.flags & SYMMETRIC_LEFT)
3491 return "<";
3492 else
3493 return ">";
3494 } else if (revs->graph)
3495 return "*";
3496 else if (revs->cherry_mark)
3497 return "+";
3498 return "";
3501 void put_revision_mark(const struct rev_info *revs, const struct commit *commit)
3503 char *mark = get_revision_mark(revs, commit);
3504 if (!strlen(mark))
3505 return;
3506 fputs(mark, stdout);
3507 putchar(' ');