revision: keep topo-walk free of unintersting commits
[git.git] / revision.c
blob621feb9df716400f32d016e1d36fc368b6a884fb
1 #include "cache.h"
2 #include "object-store.h"
3 #include "tag.h"
4 #include "blob.h"
5 #include "tree.h"
6 #include "commit.h"
7 #include "diff.h"
8 #include "refs.h"
9 #include "revision.h"
10 #include "repository.h"
11 #include "graph.h"
12 #include "grep.h"
13 #include "reflog-walk.h"
14 #include "patch-ids.h"
15 #include "decorate.h"
16 #include "log-tree.h"
17 #include "string-list.h"
18 #include "line-log.h"
19 #include "mailmap.h"
20 #include "commit-slab.h"
21 #include "dir.h"
22 #include "cache-tree.h"
23 #include "bisect.h"
24 #include "packfile.h"
25 #include "worktree.h"
26 #include "argv-array.h"
27 #include "commit-reach.h"
28 #include "commit-graph.h"
29 #include "prio-queue.h"
30 #include "hashmap.h"
32 volatile show_early_output_fn_t show_early_output;
34 static const char *term_bad;
35 static const char *term_good;
37 implement_shared_commit_slab(revision_sources, char *);
39 void show_object_with_name(FILE *out, struct object *obj, const char *name)
41 const char *p;
43 fprintf(out, "%s ", oid_to_hex(&obj->oid));
44 for (p = name; *p && *p != '\n'; p++)
45 fputc(*p, out);
46 fputc('\n', out);
49 static void mark_blob_uninteresting(struct blob *blob)
51 if (!blob)
52 return;
53 if (blob->object.flags & UNINTERESTING)
54 return;
55 blob->object.flags |= UNINTERESTING;
58 static void mark_tree_contents_uninteresting(struct repository *r,
59 struct tree *tree)
61 struct tree_desc desc;
62 struct name_entry entry;
64 if (parse_tree_gently(tree, 1) < 0)
65 return;
67 init_tree_desc(&desc, tree->buffer, tree->size);
68 while (tree_entry(&desc, &entry)) {
69 switch (object_type(entry.mode)) {
70 case OBJ_TREE:
71 mark_tree_uninteresting(r, lookup_tree(r, &entry.oid));
72 break;
73 case OBJ_BLOB:
74 mark_blob_uninteresting(lookup_blob(r, &entry.oid));
75 break;
76 default:
77 /* Subproject commit - not in this repository */
78 break;
83 * We don't care about the tree any more
84 * after it has been marked uninteresting.
86 free_tree_buffer(tree);
89 void mark_tree_uninteresting(struct repository *r, struct tree *tree)
91 struct object *obj;
93 if (!tree)
94 return;
96 obj = &tree->object;
97 if (obj->flags & UNINTERESTING)
98 return;
99 obj->flags |= UNINTERESTING;
100 mark_tree_contents_uninteresting(r, tree);
103 struct path_and_oids_entry {
104 struct hashmap_entry ent;
105 char *path;
106 struct oidset trees;
109 static int path_and_oids_cmp(const void *hashmap_cmp_fn_data,
110 const struct path_and_oids_entry *e1,
111 const struct path_and_oids_entry *e2,
112 const void *keydata)
114 return strcmp(e1->path, e2->path);
117 static void paths_and_oids_init(struct hashmap *map)
119 hashmap_init(map, (hashmap_cmp_fn) path_and_oids_cmp, NULL, 0);
122 static void paths_and_oids_clear(struct hashmap *map)
124 struct hashmap_iter iter;
125 struct path_and_oids_entry *entry;
126 hashmap_iter_init(map, &iter);
128 while ((entry = (struct path_and_oids_entry *)hashmap_iter_next(&iter))) {
129 oidset_clear(&entry->trees);
130 free(entry->path);
133 hashmap_free(map, 1);
136 static void paths_and_oids_insert(struct hashmap *map,
137 const char *path,
138 const struct object_id *oid)
140 int hash = strhash(path);
141 struct path_and_oids_entry key;
142 struct path_and_oids_entry *entry;
144 hashmap_entry_init(&key, hash);
146 /* use a shallow copy for the lookup */
147 key.path = (char *)path;
148 oidset_init(&key.trees, 0);
150 if (!(entry = (struct path_and_oids_entry *)hashmap_get(map, &key, NULL))) {
151 entry = xcalloc(1, sizeof(struct path_and_oids_entry));
152 hashmap_entry_init(entry, hash);
153 entry->path = xstrdup(key.path);
154 oidset_init(&entry->trees, 16);
155 hashmap_put(map, entry);
158 oidset_insert(&entry->trees, oid);
161 static void add_children_by_path(struct repository *r,
162 struct tree *tree,
163 struct hashmap *map)
165 struct tree_desc desc;
166 struct name_entry entry;
168 if (!tree)
169 return;
171 if (parse_tree_gently(tree, 1) < 0)
172 return;
174 init_tree_desc(&desc, tree->buffer, tree->size);
175 while (tree_entry(&desc, &entry)) {
176 switch (object_type(entry.mode)) {
177 case OBJ_TREE:
178 paths_and_oids_insert(map, entry.path, &entry.oid);
180 if (tree->object.flags & UNINTERESTING) {
181 struct tree *child = lookup_tree(r, &entry.oid);
182 if (child)
183 child->object.flags |= UNINTERESTING;
185 break;
186 case OBJ_BLOB:
187 if (tree->object.flags & UNINTERESTING) {
188 struct blob *child = lookup_blob(r, &entry.oid);
189 if (child)
190 child->object.flags |= UNINTERESTING;
192 break;
193 default:
194 /* Subproject commit - not in this repository */
195 break;
199 free_tree_buffer(tree);
202 void mark_trees_uninteresting_sparse(struct repository *r,
203 struct oidset *trees)
205 unsigned has_interesting = 0, has_uninteresting = 0;
206 struct hashmap map;
207 struct hashmap_iter map_iter;
208 struct path_and_oids_entry *entry;
209 struct object_id *oid;
210 struct oidset_iter iter;
212 oidset_iter_init(trees, &iter);
213 while ((!has_interesting || !has_uninteresting) &&
214 (oid = oidset_iter_next(&iter))) {
215 struct tree *tree = lookup_tree(r, oid);
217 if (!tree)
218 continue;
220 if (tree->object.flags & UNINTERESTING)
221 has_uninteresting = 1;
222 else
223 has_interesting = 1;
226 /* Do not walk unless we have both types of trees. */
227 if (!has_uninteresting || !has_interesting)
228 return;
230 paths_and_oids_init(&map);
232 oidset_iter_init(trees, &iter);
233 while ((oid = oidset_iter_next(&iter))) {
234 struct tree *tree = lookup_tree(r, oid);
235 add_children_by_path(r, tree, &map);
238 hashmap_iter_init(&map, &map_iter);
239 while ((entry = hashmap_iter_next(&map_iter)))
240 mark_trees_uninteresting_sparse(r, &entry->trees);
242 paths_and_oids_clear(&map);
245 struct commit_stack {
246 struct commit **items;
247 size_t nr, alloc;
249 #define COMMIT_STACK_INIT { NULL, 0, 0 }
251 static void commit_stack_push(struct commit_stack *stack, struct commit *commit)
253 ALLOC_GROW(stack->items, stack->nr + 1, stack->alloc);
254 stack->items[stack->nr++] = commit;
257 static struct commit *commit_stack_pop(struct commit_stack *stack)
259 return stack->nr ? stack->items[--stack->nr] : NULL;
262 static void commit_stack_clear(struct commit_stack *stack)
264 FREE_AND_NULL(stack->items);
265 stack->nr = stack->alloc = 0;
268 static void mark_one_parent_uninteresting(struct commit *commit,
269 struct commit_stack *pending)
271 struct commit_list *l;
273 if (commit->object.flags & UNINTERESTING)
274 return;
275 commit->object.flags |= UNINTERESTING;
278 * Normally we haven't parsed the parent
279 * yet, so we won't have a parent of a parent
280 * here. However, it may turn out that we've
281 * reached this commit some other way (where it
282 * wasn't uninteresting), in which case we need
283 * to mark its parents recursively too..
285 for (l = commit->parents; l; l = l->next)
286 commit_stack_push(pending, l->item);
289 void mark_parents_uninteresting(struct commit *commit)
291 struct commit_stack pending = COMMIT_STACK_INIT;
292 struct commit_list *l;
294 for (l = commit->parents; l; l = l->next)
295 mark_one_parent_uninteresting(l->item, &pending);
297 while (pending.nr > 0)
298 mark_one_parent_uninteresting(commit_stack_pop(&pending),
299 &pending);
301 commit_stack_clear(&pending);
304 static void add_pending_object_with_path(struct rev_info *revs,
305 struct object *obj,
306 const char *name, unsigned mode,
307 const char *path)
309 if (!obj)
310 return;
311 if (revs->no_walk && (obj->flags & UNINTERESTING))
312 revs->no_walk = 0;
313 if (revs->reflog_info && obj->type == OBJ_COMMIT) {
314 struct strbuf buf = STRBUF_INIT;
315 int len = interpret_branch_name(name, 0, &buf, 0);
317 if (0 < len && name[len] && buf.len)
318 strbuf_addstr(&buf, name + len);
319 add_reflog_for_walk(revs->reflog_info,
320 (struct commit *)obj,
321 buf.buf[0] ? buf.buf: name);
322 strbuf_release(&buf);
323 return; /* do not add the commit itself */
325 add_object_array_with_path(obj, name, &revs->pending, mode, path);
328 static void add_pending_object_with_mode(struct rev_info *revs,
329 struct object *obj,
330 const char *name, unsigned mode)
332 add_pending_object_with_path(revs, obj, name, mode, NULL);
335 void add_pending_object(struct rev_info *revs,
336 struct object *obj, const char *name)
338 add_pending_object_with_mode(revs, obj, name, S_IFINVALID);
341 void add_head_to_pending(struct rev_info *revs)
343 struct object_id oid;
344 struct object *obj;
345 if (get_oid("HEAD", &oid))
346 return;
347 obj = parse_object(revs->repo, &oid);
348 if (!obj)
349 return;
350 add_pending_object(revs, obj, "HEAD");
353 static struct object *get_reference(struct rev_info *revs, const char *name,
354 const struct object_id *oid,
355 unsigned int flags)
357 struct object *object;
360 * If the repository has commit graphs, repo_parse_commit() avoids
361 * reading the object buffer, so use it whenever possible.
363 if (oid_object_info(revs->repo, oid, NULL) == OBJ_COMMIT) {
364 struct commit *c = lookup_commit(revs->repo, oid);
365 if (!repo_parse_commit(revs->repo, c))
366 object = (struct object *) c;
367 else
368 object = NULL;
369 } else {
370 object = parse_object(revs->repo, oid);
373 if (!object) {
374 if (revs->ignore_missing)
375 return object;
376 if (revs->exclude_promisor_objects && is_promisor_object(oid))
377 return NULL;
378 die("bad object %s", name);
380 object->flags |= flags;
381 return object;
384 void add_pending_oid(struct rev_info *revs, const char *name,
385 const struct object_id *oid, unsigned int flags)
387 struct object *object = get_reference(revs, name, oid, flags);
388 add_pending_object(revs, object, name);
391 static struct commit *handle_commit(struct rev_info *revs,
392 struct object_array_entry *entry)
394 struct object *object = entry->item;
395 const char *name = entry->name;
396 const char *path = entry->path;
397 unsigned int mode = entry->mode;
398 unsigned long flags = object->flags;
401 * Tag object? Look what it points to..
403 while (object->type == OBJ_TAG) {
404 struct tag *tag = (struct tag *) object;
405 if (revs->tag_objects && !(flags & UNINTERESTING))
406 add_pending_object(revs, object, tag->tag);
407 if (!tag->tagged)
408 die("bad tag");
409 object = parse_object(revs->repo, &tag->tagged->oid);
410 if (!object) {
411 if (revs->ignore_missing_links || (flags & UNINTERESTING))
412 return NULL;
413 if (revs->exclude_promisor_objects &&
414 is_promisor_object(&tag->tagged->oid))
415 return NULL;
416 die("bad object %s", oid_to_hex(&tag->tagged->oid));
418 object->flags |= flags;
420 * We'll handle the tagged object by looping or dropping
421 * through to the non-tag handlers below. Do not
422 * propagate path data from the tag's pending entry.
424 path = NULL;
425 mode = 0;
429 * Commit object? Just return it, we'll do all the complex
430 * reachability crud.
432 if (object->type == OBJ_COMMIT) {
433 struct commit *commit = (struct commit *)object;
435 if (parse_commit(commit) < 0)
436 die("unable to parse commit %s", name);
437 if (flags & UNINTERESTING) {
438 mark_parents_uninteresting(commit);
440 if (!revs->topo_order || !generation_numbers_enabled(the_repository))
441 revs->limited = 1;
443 if (revs->sources) {
444 char **slot = revision_sources_at(revs->sources, commit);
446 if (!*slot)
447 *slot = xstrdup(name);
449 return commit;
453 * Tree object? Either mark it uninteresting, or add it
454 * to the list of objects to look at later..
456 if (object->type == OBJ_TREE) {
457 struct tree *tree = (struct tree *)object;
458 if (!revs->tree_objects)
459 return NULL;
460 if (flags & UNINTERESTING) {
461 mark_tree_contents_uninteresting(revs->repo, tree);
462 return NULL;
464 add_pending_object_with_path(revs, object, name, mode, path);
465 return NULL;
469 * Blob object? You know the drill by now..
471 if (object->type == OBJ_BLOB) {
472 if (!revs->blob_objects)
473 return NULL;
474 if (flags & UNINTERESTING)
475 return NULL;
476 add_pending_object_with_path(revs, object, name, mode, path);
477 return NULL;
479 die("%s is unknown object", name);
482 static int everybody_uninteresting(struct commit_list *orig,
483 struct commit **interesting_cache)
485 struct commit_list *list = orig;
487 if (*interesting_cache) {
488 struct commit *commit = *interesting_cache;
489 if (!(commit->object.flags & UNINTERESTING))
490 return 0;
493 while (list) {
494 struct commit *commit = list->item;
495 list = list->next;
496 if (commit->object.flags & UNINTERESTING)
497 continue;
499 *interesting_cache = commit;
500 return 0;
502 return 1;
506 * A definition of "relevant" commit that we can use to simplify limited graphs
507 * by eliminating side branches.
509 * A "relevant" commit is one that is !UNINTERESTING (ie we are including it
510 * in our list), or that is a specified BOTTOM commit. Then after computing
511 * a limited list, during processing we can generally ignore boundary merges
512 * coming from outside the graph, (ie from irrelevant parents), and treat
513 * those merges as if they were single-parent. TREESAME is defined to consider
514 * only relevant parents, if any. If we are TREESAME to our on-graph parents,
515 * we don't care if we were !TREESAME to non-graph parents.
517 * Treating bottom commits as relevant ensures that a limited graph's
518 * connection to the actual bottom commit is not viewed as a side branch, but
519 * treated as part of the graph. For example:
521 * ....Z...A---X---o---o---B
522 * . /
523 * W---Y
525 * When computing "A..B", the A-X connection is at least as important as
526 * Y-X, despite A being flagged UNINTERESTING.
528 * And when computing --ancestry-path "A..B", the A-X connection is more
529 * important than Y-X, despite both A and Y being flagged UNINTERESTING.
531 static inline int relevant_commit(struct commit *commit)
533 return (commit->object.flags & (UNINTERESTING | BOTTOM)) != UNINTERESTING;
537 * Return a single relevant commit from a parent list. If we are a TREESAME
538 * commit, and this selects one of our parents, then we can safely simplify to
539 * that parent.
541 static struct commit *one_relevant_parent(const struct rev_info *revs,
542 struct commit_list *orig)
544 struct commit_list *list = orig;
545 struct commit *relevant = NULL;
547 if (!orig)
548 return NULL;
551 * For 1-parent commits, or if first-parent-only, then return that
552 * first parent (even if not "relevant" by the above definition).
553 * TREESAME will have been set purely on that parent.
555 if (revs->first_parent_only || !orig->next)
556 return orig->item;
559 * For multi-parent commits, identify a sole relevant parent, if any.
560 * If we have only one relevant parent, then TREESAME will be set purely
561 * with regard to that parent, and we can simplify accordingly.
563 * If we have more than one relevant parent, or no relevant parents
564 * (and multiple irrelevant ones), then we can't select a parent here
565 * and return NULL.
567 while (list) {
568 struct commit *commit = list->item;
569 list = list->next;
570 if (relevant_commit(commit)) {
571 if (relevant)
572 return NULL;
573 relevant = commit;
576 return relevant;
580 * The goal is to get REV_TREE_NEW as the result only if the
581 * diff consists of all '+' (and no other changes), REV_TREE_OLD
582 * if the whole diff is removal of old data, and otherwise
583 * REV_TREE_DIFFERENT (of course if the trees are the same we
584 * want REV_TREE_SAME).
586 * The only time we care about the distinction is when
587 * remove_empty_trees is in effect, in which case we care only about
588 * whether the whole change is REV_TREE_NEW, or if there's another type
589 * of change. Which means we can stop the diff early in either of these
590 * cases:
592 * 1. We're not using remove_empty_trees at all.
594 * 2. We saw anything except REV_TREE_NEW.
596 static int tree_difference = REV_TREE_SAME;
598 static void file_add_remove(struct diff_options *options,
599 int addremove, unsigned mode,
600 const struct object_id *oid,
601 int oid_valid,
602 const char *fullpath, unsigned dirty_submodule)
604 int diff = addremove == '+' ? REV_TREE_NEW : REV_TREE_OLD;
605 struct rev_info *revs = options->change_fn_data;
607 tree_difference |= diff;
608 if (!revs->remove_empty_trees || tree_difference != REV_TREE_NEW)
609 options->flags.has_changes = 1;
612 static void file_change(struct diff_options *options,
613 unsigned old_mode, unsigned new_mode,
614 const struct object_id *old_oid,
615 const struct object_id *new_oid,
616 int old_oid_valid, int new_oid_valid,
617 const char *fullpath,
618 unsigned old_dirty_submodule, unsigned new_dirty_submodule)
620 tree_difference = REV_TREE_DIFFERENT;
621 options->flags.has_changes = 1;
624 static int rev_compare_tree(struct rev_info *revs,
625 struct commit *parent, struct commit *commit)
627 struct tree *t1 = get_commit_tree(parent);
628 struct tree *t2 = get_commit_tree(commit);
630 if (!t1)
631 return REV_TREE_NEW;
632 if (!t2)
633 return REV_TREE_OLD;
635 if (revs->simplify_by_decoration) {
637 * If we are simplifying by decoration, then the commit
638 * is worth showing if it has a tag pointing at it.
640 if (get_name_decoration(&commit->object))
641 return REV_TREE_DIFFERENT;
643 * A commit that is not pointed by a tag is uninteresting
644 * if we are not limited by path. This means that you will
645 * see the usual "commits that touch the paths" plus any
646 * tagged commit by specifying both --simplify-by-decoration
647 * and pathspec.
649 if (!revs->prune_data.nr)
650 return REV_TREE_SAME;
653 tree_difference = REV_TREE_SAME;
654 revs->pruning.flags.has_changes = 0;
655 if (diff_tree_oid(&t1->object.oid, &t2->object.oid, "",
656 &revs->pruning) < 0)
657 return REV_TREE_DIFFERENT;
658 return tree_difference;
661 static int rev_same_tree_as_empty(struct rev_info *revs, struct commit *commit)
663 int retval;
664 struct tree *t1 = get_commit_tree(commit);
666 if (!t1)
667 return 0;
669 tree_difference = REV_TREE_SAME;
670 revs->pruning.flags.has_changes = 0;
671 retval = diff_tree_oid(NULL, &t1->object.oid, "", &revs->pruning);
673 return retval >= 0 && (tree_difference == REV_TREE_SAME);
676 struct treesame_state {
677 unsigned int nparents;
678 unsigned char treesame[FLEX_ARRAY];
681 static struct treesame_state *initialise_treesame(struct rev_info *revs, struct commit *commit)
683 unsigned n = commit_list_count(commit->parents);
684 struct treesame_state *st = xcalloc(1, st_add(sizeof(*st), n));
685 st->nparents = n;
686 add_decoration(&revs->treesame, &commit->object, st);
687 return st;
691 * Must be called immediately after removing the nth_parent from a commit's
692 * parent list, if we are maintaining the per-parent treesame[] decoration.
693 * This does not recalculate the master TREESAME flag - update_treesame()
694 * should be called to update it after a sequence of treesame[] modifications
695 * that may have affected it.
697 static int compact_treesame(struct rev_info *revs, struct commit *commit, unsigned nth_parent)
699 struct treesame_state *st;
700 int old_same;
702 if (!commit->parents) {
704 * Have just removed the only parent from a non-merge.
705 * Different handling, as we lack decoration.
707 if (nth_parent != 0)
708 die("compact_treesame %u", nth_parent);
709 old_same = !!(commit->object.flags & TREESAME);
710 if (rev_same_tree_as_empty(revs, commit))
711 commit->object.flags |= TREESAME;
712 else
713 commit->object.flags &= ~TREESAME;
714 return old_same;
717 st = lookup_decoration(&revs->treesame, &commit->object);
718 if (!st || nth_parent >= st->nparents)
719 die("compact_treesame %u", nth_parent);
721 old_same = st->treesame[nth_parent];
722 memmove(st->treesame + nth_parent,
723 st->treesame + nth_parent + 1,
724 st->nparents - nth_parent - 1);
727 * If we've just become a non-merge commit, update TREESAME
728 * immediately, and remove the no-longer-needed decoration.
729 * If still a merge, defer update until update_treesame().
731 if (--st->nparents == 1) {
732 if (commit->parents->next)
733 die("compact_treesame parents mismatch");
734 if (st->treesame[0] && revs->dense)
735 commit->object.flags |= TREESAME;
736 else
737 commit->object.flags &= ~TREESAME;
738 free(add_decoration(&revs->treesame, &commit->object, NULL));
741 return old_same;
744 static unsigned update_treesame(struct rev_info *revs, struct commit *commit)
746 if (commit->parents && commit->parents->next) {
747 unsigned n;
748 struct treesame_state *st;
749 struct commit_list *p;
750 unsigned relevant_parents;
751 unsigned relevant_change, irrelevant_change;
753 st = lookup_decoration(&revs->treesame, &commit->object);
754 if (!st)
755 die("update_treesame %s", oid_to_hex(&commit->object.oid));
756 relevant_parents = 0;
757 relevant_change = irrelevant_change = 0;
758 for (p = commit->parents, n = 0; p; n++, p = p->next) {
759 if (relevant_commit(p->item)) {
760 relevant_change |= !st->treesame[n];
761 relevant_parents++;
762 } else
763 irrelevant_change |= !st->treesame[n];
765 if (relevant_parents ? relevant_change : irrelevant_change)
766 commit->object.flags &= ~TREESAME;
767 else
768 commit->object.flags |= TREESAME;
771 return commit->object.flags & TREESAME;
774 static inline int limiting_can_increase_treesame(const struct rev_info *revs)
777 * TREESAME is irrelevant unless prune && dense;
778 * if simplify_history is set, we can't have a mixture of TREESAME and
779 * !TREESAME INTERESTING parents (and we don't have treesame[]
780 * decoration anyway);
781 * if first_parent_only is set, then the TREESAME flag is locked
782 * against the first parent (and again we lack treesame[] decoration).
784 return revs->prune && revs->dense &&
785 !revs->simplify_history &&
786 !revs->first_parent_only;
789 static void try_to_simplify_commit(struct rev_info *revs, struct commit *commit)
791 struct commit_list **pp, *parent;
792 struct treesame_state *ts = NULL;
793 int relevant_change = 0, irrelevant_change = 0;
794 int relevant_parents, nth_parent;
797 * If we don't do pruning, everything is interesting
799 if (!revs->prune)
800 return;
802 if (!get_commit_tree(commit))
803 return;
805 if (!commit->parents) {
806 if (rev_same_tree_as_empty(revs, commit))
807 commit->object.flags |= TREESAME;
808 return;
812 * Normal non-merge commit? If we don't want to make the
813 * history dense, we consider it always to be a change..
815 if (!revs->dense && !commit->parents->next)
816 return;
818 for (pp = &commit->parents, nth_parent = 0, relevant_parents = 0;
819 (parent = *pp) != NULL;
820 pp = &parent->next, nth_parent++) {
821 struct commit *p = parent->item;
822 if (relevant_commit(p))
823 relevant_parents++;
825 if (nth_parent == 1) {
827 * This our second loop iteration - so we now know
828 * we're dealing with a merge.
830 * Do not compare with later parents when we care only about
831 * the first parent chain, in order to avoid derailing the
832 * traversal to follow a side branch that brought everything
833 * in the path we are limited to by the pathspec.
835 if (revs->first_parent_only)
836 break;
838 * If this will remain a potentially-simplifiable
839 * merge, remember per-parent treesame if needed.
840 * Initialise the array with the comparison from our
841 * first iteration.
843 if (revs->treesame.name &&
844 !revs->simplify_history &&
845 !(commit->object.flags & UNINTERESTING)) {
846 ts = initialise_treesame(revs, commit);
847 if (!(irrelevant_change || relevant_change))
848 ts->treesame[0] = 1;
851 if (parse_commit(p) < 0)
852 die("cannot simplify commit %s (because of %s)",
853 oid_to_hex(&commit->object.oid),
854 oid_to_hex(&p->object.oid));
855 switch (rev_compare_tree(revs, p, commit)) {
856 case REV_TREE_SAME:
857 if (!revs->simplify_history || !relevant_commit(p)) {
858 /* Even if a merge with an uninteresting
859 * side branch brought the entire change
860 * we are interested in, we do not want
861 * to lose the other branches of this
862 * merge, so we just keep going.
864 if (ts)
865 ts->treesame[nth_parent] = 1;
866 continue;
868 parent->next = NULL;
869 commit->parents = parent;
870 commit->object.flags |= TREESAME;
871 return;
873 case REV_TREE_NEW:
874 if (revs->remove_empty_trees &&
875 rev_same_tree_as_empty(revs, p)) {
876 /* We are adding all the specified
877 * paths from this parent, so the
878 * history beyond this parent is not
879 * interesting. Remove its parents
880 * (they are grandparents for us).
881 * IOW, we pretend this parent is a
882 * "root" commit.
884 if (parse_commit(p) < 0)
885 die("cannot simplify commit %s (invalid %s)",
886 oid_to_hex(&commit->object.oid),
887 oid_to_hex(&p->object.oid));
888 p->parents = NULL;
890 /* fallthrough */
891 case REV_TREE_OLD:
892 case REV_TREE_DIFFERENT:
893 if (relevant_commit(p))
894 relevant_change = 1;
895 else
896 irrelevant_change = 1;
897 continue;
899 die("bad tree compare for commit %s", oid_to_hex(&commit->object.oid));
903 * TREESAME is straightforward for single-parent commits. For merge
904 * commits, it is most useful to define it so that "irrelevant"
905 * parents cannot make us !TREESAME - if we have any relevant
906 * parents, then we only consider TREESAMEness with respect to them,
907 * allowing irrelevant merges from uninteresting branches to be
908 * simplified away. Only if we have only irrelevant parents do we
909 * base TREESAME on them. Note that this logic is replicated in
910 * update_treesame, which should be kept in sync.
912 if (relevant_parents ? !relevant_change : !irrelevant_change)
913 commit->object.flags |= TREESAME;
916 static int process_parents(struct rev_info *revs, struct commit *commit,
917 struct commit_list **list, struct prio_queue *queue)
919 struct commit_list *parent = commit->parents;
920 unsigned left_flag;
922 if (commit->object.flags & ADDED)
923 return 0;
924 commit->object.flags |= ADDED;
926 if (revs->include_check &&
927 !revs->include_check(commit, revs->include_check_data))
928 return 0;
931 * If the commit is uninteresting, don't try to
932 * prune parents - we want the maximal uninteresting
933 * set.
935 * Normally we haven't parsed the parent
936 * yet, so we won't have a parent of a parent
937 * here. However, it may turn out that we've
938 * reached this commit some other way (where it
939 * wasn't uninteresting), in which case we need
940 * to mark its parents recursively too..
942 if (commit->object.flags & UNINTERESTING) {
943 while (parent) {
944 struct commit *p = parent->item;
945 parent = parent->next;
946 if (p)
947 p->object.flags |= UNINTERESTING;
948 if (parse_commit_gently(p, 1) < 0)
949 continue;
950 if (p->parents)
951 mark_parents_uninteresting(p);
952 if (p->object.flags & SEEN)
953 continue;
954 p->object.flags |= SEEN;
955 if (list)
956 commit_list_insert_by_date(p, list);
957 if (queue)
958 prio_queue_put(queue, p);
960 return 0;
964 * Ok, the commit wasn't uninteresting. Try to
965 * simplify the commit history and find the parent
966 * that has no differences in the path set if one exists.
968 try_to_simplify_commit(revs, commit);
970 if (revs->no_walk)
971 return 0;
973 left_flag = (commit->object.flags & SYMMETRIC_LEFT);
975 for (parent = commit->parents; parent; parent = parent->next) {
976 struct commit *p = parent->item;
977 int gently = revs->ignore_missing_links ||
978 revs->exclude_promisor_objects;
979 if (parse_commit_gently(p, gently) < 0) {
980 if (revs->exclude_promisor_objects &&
981 is_promisor_object(&p->object.oid)) {
982 if (revs->first_parent_only)
983 break;
984 continue;
986 return -1;
988 if (revs->sources) {
989 char **slot = revision_sources_at(revs->sources, p);
991 if (!*slot)
992 *slot = *revision_sources_at(revs->sources, commit);
994 p->object.flags |= left_flag;
995 if (!(p->object.flags & SEEN)) {
996 p->object.flags |= SEEN;
997 if (list)
998 commit_list_insert_by_date(p, list);
999 if (queue)
1000 prio_queue_put(queue, p);
1002 if (revs->first_parent_only)
1003 break;
1005 return 0;
1008 static void cherry_pick_list(struct commit_list *list, struct rev_info *revs)
1010 struct commit_list *p;
1011 int left_count = 0, right_count = 0;
1012 int left_first;
1013 struct patch_ids ids;
1014 unsigned cherry_flag;
1016 /* First count the commits on the left and on the right */
1017 for (p = list; p; p = p->next) {
1018 struct commit *commit = p->item;
1019 unsigned flags = commit->object.flags;
1020 if (flags & BOUNDARY)
1022 else if (flags & SYMMETRIC_LEFT)
1023 left_count++;
1024 else
1025 right_count++;
1028 if (!left_count || !right_count)
1029 return;
1031 left_first = left_count < right_count;
1032 init_patch_ids(revs->repo, &ids);
1033 ids.diffopts.pathspec = revs->diffopt.pathspec;
1035 /* Compute patch-ids for one side */
1036 for (p = list; p; p = p->next) {
1037 struct commit *commit = p->item;
1038 unsigned flags = commit->object.flags;
1040 if (flags & BOUNDARY)
1041 continue;
1043 * If we have fewer left, left_first is set and we omit
1044 * commits on the right branch in this loop. If we have
1045 * fewer right, we skip the left ones.
1047 if (left_first != !!(flags & SYMMETRIC_LEFT))
1048 continue;
1049 add_commit_patch_id(commit, &ids);
1052 /* either cherry_mark or cherry_pick are true */
1053 cherry_flag = revs->cherry_mark ? PATCHSAME : SHOWN;
1055 /* Check the other side */
1056 for (p = list; p; p = p->next) {
1057 struct commit *commit = p->item;
1058 struct patch_id *id;
1059 unsigned flags = commit->object.flags;
1061 if (flags & BOUNDARY)
1062 continue;
1064 * If we have fewer left, left_first is set and we omit
1065 * commits on the left branch in this loop.
1067 if (left_first == !!(flags & SYMMETRIC_LEFT))
1068 continue;
1071 * Have we seen the same patch id?
1073 id = has_commit_patch_id(commit, &ids);
1074 if (!id)
1075 continue;
1077 commit->object.flags |= cherry_flag;
1078 id->commit->object.flags |= cherry_flag;
1081 free_patch_ids(&ids);
1084 /* How many extra uninteresting commits we want to see.. */
1085 #define SLOP 5
1087 static int still_interesting(struct commit_list *src, timestamp_t date, int slop,
1088 struct commit **interesting_cache)
1091 * No source list at all? We're definitely done..
1093 if (!src)
1094 return 0;
1097 * Does the destination list contain entries with a date
1098 * before the source list? Definitely _not_ done.
1100 if (date <= src->item->date)
1101 return SLOP;
1104 * Does the source list still have interesting commits in
1105 * it? Definitely not done..
1107 if (!everybody_uninteresting(src, interesting_cache))
1108 return SLOP;
1110 /* Ok, we're closing in.. */
1111 return slop-1;
1115 * "rev-list --ancestry-path A..B" computes commits that are ancestors
1116 * of B but not ancestors of A but further limits the result to those
1117 * that are descendants of A. This takes the list of bottom commits and
1118 * the result of "A..B" without --ancestry-path, and limits the latter
1119 * further to the ones that can reach one of the commits in "bottom".
1121 static void limit_to_ancestry(struct commit_list *bottom, struct commit_list *list)
1123 struct commit_list *p;
1124 struct commit_list *rlist = NULL;
1125 int made_progress;
1128 * Reverse the list so that it will be likely that we would
1129 * process parents before children.
1131 for (p = list; p; p = p->next)
1132 commit_list_insert(p->item, &rlist);
1134 for (p = bottom; p; p = p->next)
1135 p->item->object.flags |= TMP_MARK;
1138 * Mark the ones that can reach bottom commits in "list",
1139 * in a bottom-up fashion.
1141 do {
1142 made_progress = 0;
1143 for (p = rlist; p; p = p->next) {
1144 struct commit *c = p->item;
1145 struct commit_list *parents;
1146 if (c->object.flags & (TMP_MARK | UNINTERESTING))
1147 continue;
1148 for (parents = c->parents;
1149 parents;
1150 parents = parents->next) {
1151 if (!(parents->item->object.flags & TMP_MARK))
1152 continue;
1153 c->object.flags |= TMP_MARK;
1154 made_progress = 1;
1155 break;
1158 } while (made_progress);
1161 * NEEDSWORK: decide if we want to remove parents that are
1162 * not marked with TMP_MARK from commit->parents for commits
1163 * in the resulting list. We may not want to do that, though.
1167 * The ones that are not marked with TMP_MARK are uninteresting
1169 for (p = list; p; p = p->next) {
1170 struct commit *c = p->item;
1171 if (c->object.flags & TMP_MARK)
1172 continue;
1173 c->object.flags |= UNINTERESTING;
1176 /* We are done with the TMP_MARK */
1177 for (p = list; p; p = p->next)
1178 p->item->object.flags &= ~TMP_MARK;
1179 for (p = bottom; p; p = p->next)
1180 p->item->object.flags &= ~TMP_MARK;
1181 free_commit_list(rlist);
1185 * Before walking the history, keep the set of "negative" refs the
1186 * caller has asked to exclude.
1188 * This is used to compute "rev-list --ancestry-path A..B", as we need
1189 * to filter the result of "A..B" further to the ones that can actually
1190 * reach A.
1192 static struct commit_list *collect_bottom_commits(struct commit_list *list)
1194 struct commit_list *elem, *bottom = NULL;
1195 for (elem = list; elem; elem = elem->next)
1196 if (elem->item->object.flags & BOTTOM)
1197 commit_list_insert(elem->item, &bottom);
1198 return bottom;
1201 /* Assumes either left_only or right_only is set */
1202 static void limit_left_right(struct commit_list *list, struct rev_info *revs)
1204 struct commit_list *p;
1206 for (p = list; p; p = p->next) {
1207 struct commit *commit = p->item;
1209 if (revs->right_only) {
1210 if (commit->object.flags & SYMMETRIC_LEFT)
1211 commit->object.flags |= SHOWN;
1212 } else /* revs->left_only is set */
1213 if (!(commit->object.flags & SYMMETRIC_LEFT))
1214 commit->object.flags |= SHOWN;
1218 static int limit_list(struct rev_info *revs)
1220 int slop = SLOP;
1221 timestamp_t date = TIME_MAX;
1222 struct commit_list *list = revs->commits;
1223 struct commit_list *newlist = NULL;
1224 struct commit_list **p = &newlist;
1225 struct commit_list *bottom = NULL;
1226 struct commit *interesting_cache = NULL;
1228 if (revs->ancestry_path) {
1229 bottom = collect_bottom_commits(list);
1230 if (!bottom)
1231 die("--ancestry-path given but there are no bottom commits");
1234 while (list) {
1235 struct commit *commit = pop_commit(&list);
1236 struct object *obj = &commit->object;
1237 show_early_output_fn_t show;
1239 if (commit == interesting_cache)
1240 interesting_cache = NULL;
1242 if (revs->max_age != -1 && (commit->date < revs->max_age))
1243 obj->flags |= UNINTERESTING;
1244 if (process_parents(revs, commit, &list, NULL) < 0)
1245 return -1;
1246 if (obj->flags & UNINTERESTING) {
1247 mark_parents_uninteresting(commit);
1248 slop = still_interesting(list, date, slop, &interesting_cache);
1249 if (slop)
1250 continue;
1251 break;
1253 if (revs->min_age != -1 && (commit->date > revs->min_age))
1254 continue;
1255 date = commit->date;
1256 p = &commit_list_insert(commit, p)->next;
1258 show = show_early_output;
1259 if (!show)
1260 continue;
1262 show(revs, newlist);
1263 show_early_output = NULL;
1265 if (revs->cherry_pick || revs->cherry_mark)
1266 cherry_pick_list(newlist, revs);
1268 if (revs->left_only || revs->right_only)
1269 limit_left_right(newlist, revs);
1271 if (bottom) {
1272 limit_to_ancestry(bottom, newlist);
1273 free_commit_list(bottom);
1277 * Check if any commits have become TREESAME by some of their parents
1278 * becoming UNINTERESTING.
1280 if (limiting_can_increase_treesame(revs))
1281 for (list = newlist; list; list = list->next) {
1282 struct commit *c = list->item;
1283 if (c->object.flags & (UNINTERESTING | TREESAME))
1284 continue;
1285 update_treesame(revs, c);
1288 revs->commits = newlist;
1289 return 0;
1293 * Add an entry to refs->cmdline with the specified information.
1294 * *name is copied.
1296 static void add_rev_cmdline(struct rev_info *revs,
1297 struct object *item,
1298 const char *name,
1299 int whence,
1300 unsigned flags)
1302 struct rev_cmdline_info *info = &revs->cmdline;
1303 unsigned int nr = info->nr;
1305 ALLOC_GROW(info->rev, nr + 1, info->alloc);
1306 info->rev[nr].item = item;
1307 info->rev[nr].name = xstrdup(name);
1308 info->rev[nr].whence = whence;
1309 info->rev[nr].flags = flags;
1310 info->nr++;
1313 static void add_rev_cmdline_list(struct rev_info *revs,
1314 struct commit_list *commit_list,
1315 int whence,
1316 unsigned flags)
1318 while (commit_list) {
1319 struct object *object = &commit_list->item->object;
1320 add_rev_cmdline(revs, object, oid_to_hex(&object->oid),
1321 whence, flags);
1322 commit_list = commit_list->next;
1326 struct all_refs_cb {
1327 int all_flags;
1328 int warned_bad_reflog;
1329 struct rev_info *all_revs;
1330 const char *name_for_errormsg;
1331 struct worktree *wt;
1334 int ref_excluded(struct string_list *ref_excludes, const char *path)
1336 struct string_list_item *item;
1338 if (!ref_excludes)
1339 return 0;
1340 for_each_string_list_item(item, ref_excludes) {
1341 if (!wildmatch(item->string, path, 0))
1342 return 1;
1344 return 0;
1347 static int handle_one_ref(const char *path, const struct object_id *oid,
1348 int flag, void *cb_data)
1350 struct all_refs_cb *cb = cb_data;
1351 struct object *object;
1353 if (ref_excluded(cb->all_revs->ref_excludes, path))
1354 return 0;
1356 object = get_reference(cb->all_revs, path, oid, cb->all_flags);
1357 add_rev_cmdline(cb->all_revs, object, path, REV_CMD_REF, cb->all_flags);
1358 add_pending_oid(cb->all_revs, path, oid, cb->all_flags);
1359 return 0;
1362 static void init_all_refs_cb(struct all_refs_cb *cb, struct rev_info *revs,
1363 unsigned flags)
1365 cb->all_revs = revs;
1366 cb->all_flags = flags;
1367 revs->rev_input_given = 1;
1368 cb->wt = NULL;
1371 void clear_ref_exclusion(struct string_list **ref_excludes_p)
1373 if (*ref_excludes_p) {
1374 string_list_clear(*ref_excludes_p, 0);
1375 free(*ref_excludes_p);
1377 *ref_excludes_p = NULL;
1380 void add_ref_exclusion(struct string_list **ref_excludes_p, const char *exclude)
1382 if (!*ref_excludes_p) {
1383 *ref_excludes_p = xcalloc(1, sizeof(**ref_excludes_p));
1384 (*ref_excludes_p)->strdup_strings = 1;
1386 string_list_append(*ref_excludes_p, exclude);
1389 static void handle_refs(struct ref_store *refs,
1390 struct rev_info *revs, unsigned flags,
1391 int (*for_each)(struct ref_store *, each_ref_fn, void *))
1393 struct all_refs_cb cb;
1395 if (!refs) {
1396 /* this could happen with uninitialized submodules */
1397 return;
1400 init_all_refs_cb(&cb, revs, flags);
1401 for_each(refs, handle_one_ref, &cb);
1404 static void handle_one_reflog_commit(struct object_id *oid, void *cb_data)
1406 struct all_refs_cb *cb = cb_data;
1407 if (!is_null_oid(oid)) {
1408 struct object *o = parse_object(cb->all_revs->repo, oid);
1409 if (o) {
1410 o->flags |= cb->all_flags;
1411 /* ??? CMDLINEFLAGS ??? */
1412 add_pending_object(cb->all_revs, o, "");
1414 else if (!cb->warned_bad_reflog) {
1415 warning("reflog of '%s' references pruned commits",
1416 cb->name_for_errormsg);
1417 cb->warned_bad_reflog = 1;
1422 static int handle_one_reflog_ent(struct object_id *ooid, struct object_id *noid,
1423 const char *email, timestamp_t timestamp, int tz,
1424 const char *message, void *cb_data)
1426 handle_one_reflog_commit(ooid, cb_data);
1427 handle_one_reflog_commit(noid, cb_data);
1428 return 0;
1431 static int handle_one_reflog(const char *refname_in_wt,
1432 const struct object_id *oid,
1433 int flag, void *cb_data)
1435 struct all_refs_cb *cb = cb_data;
1436 struct strbuf refname = STRBUF_INIT;
1438 cb->warned_bad_reflog = 0;
1439 strbuf_worktree_ref(cb->wt, &refname, refname_in_wt);
1440 cb->name_for_errormsg = refname.buf;
1441 refs_for_each_reflog_ent(get_main_ref_store(the_repository),
1442 refname.buf,
1443 handle_one_reflog_ent, cb_data);
1444 strbuf_release(&refname);
1445 return 0;
1448 static void add_other_reflogs_to_pending(struct all_refs_cb *cb)
1450 struct worktree **worktrees, **p;
1452 worktrees = get_worktrees(0);
1453 for (p = worktrees; *p; p++) {
1454 struct worktree *wt = *p;
1456 if (wt->is_current)
1457 continue;
1459 cb->wt = wt;
1460 refs_for_each_reflog(get_worktree_ref_store(wt),
1461 handle_one_reflog,
1462 cb);
1464 free_worktrees(worktrees);
1467 void add_reflogs_to_pending(struct rev_info *revs, unsigned flags)
1469 struct all_refs_cb cb;
1471 cb.all_revs = revs;
1472 cb.all_flags = flags;
1473 cb.wt = NULL;
1474 for_each_reflog(handle_one_reflog, &cb);
1476 if (!revs->single_worktree)
1477 add_other_reflogs_to_pending(&cb);
1480 static void add_cache_tree(struct cache_tree *it, struct rev_info *revs,
1481 struct strbuf *path, unsigned int flags)
1483 size_t baselen = path->len;
1484 int i;
1486 if (it->entry_count >= 0) {
1487 struct tree *tree = lookup_tree(revs->repo, &it->oid);
1488 tree->object.flags |= flags;
1489 add_pending_object_with_path(revs, &tree->object, "",
1490 040000, path->buf);
1493 for (i = 0; i < it->subtree_nr; i++) {
1494 struct cache_tree_sub *sub = it->down[i];
1495 strbuf_addf(path, "%s%s", baselen ? "/" : "", sub->name);
1496 add_cache_tree(sub->cache_tree, revs, path, flags);
1497 strbuf_setlen(path, baselen);
1502 static void do_add_index_objects_to_pending(struct rev_info *revs,
1503 struct index_state *istate,
1504 unsigned int flags)
1506 int i;
1508 for (i = 0; i < istate->cache_nr; i++) {
1509 struct cache_entry *ce = istate->cache[i];
1510 struct blob *blob;
1512 if (S_ISGITLINK(ce->ce_mode))
1513 continue;
1515 blob = lookup_blob(revs->repo, &ce->oid);
1516 if (!blob)
1517 die("unable to add index blob to traversal");
1518 blob->object.flags |= flags;
1519 add_pending_object_with_path(revs, &blob->object, "",
1520 ce->ce_mode, ce->name);
1523 if (istate->cache_tree) {
1524 struct strbuf path = STRBUF_INIT;
1525 add_cache_tree(istate->cache_tree, revs, &path, flags);
1526 strbuf_release(&path);
1530 void add_index_objects_to_pending(struct rev_info *revs, unsigned int flags)
1532 struct worktree **worktrees, **p;
1534 repo_read_index(revs->repo);
1535 do_add_index_objects_to_pending(revs, revs->repo->index, flags);
1537 if (revs->single_worktree)
1538 return;
1540 worktrees = get_worktrees(0);
1541 for (p = worktrees; *p; p++) {
1542 struct worktree *wt = *p;
1543 struct index_state istate = { NULL };
1545 if (wt->is_current)
1546 continue; /* current index already taken care of */
1548 if (read_index_from(&istate,
1549 worktree_git_path(wt, "index"),
1550 get_worktree_git_dir(wt)) > 0)
1551 do_add_index_objects_to_pending(revs, &istate, flags);
1552 discard_index(&istate);
1554 free_worktrees(worktrees);
1557 static int add_parents_only(struct rev_info *revs, const char *arg_, int flags,
1558 int exclude_parent)
1560 struct object_id oid;
1561 struct object *it;
1562 struct commit *commit;
1563 struct commit_list *parents;
1564 int parent_number;
1565 const char *arg = arg_;
1567 if (*arg == '^') {
1568 flags ^= UNINTERESTING | BOTTOM;
1569 arg++;
1571 if (get_oid_committish(arg, &oid))
1572 return 0;
1573 while (1) {
1574 it = get_reference(revs, arg, &oid, 0);
1575 if (!it && revs->ignore_missing)
1576 return 0;
1577 if (it->type != OBJ_TAG)
1578 break;
1579 if (!((struct tag*)it)->tagged)
1580 return 0;
1581 oidcpy(&oid, &((struct tag*)it)->tagged->oid);
1583 if (it->type != OBJ_COMMIT)
1584 return 0;
1585 commit = (struct commit *)it;
1586 if (exclude_parent &&
1587 exclude_parent > commit_list_count(commit->parents))
1588 return 0;
1589 for (parents = commit->parents, parent_number = 1;
1590 parents;
1591 parents = parents->next, parent_number++) {
1592 if (exclude_parent && parent_number != exclude_parent)
1593 continue;
1595 it = &parents->item->object;
1596 it->flags |= flags;
1597 add_rev_cmdline(revs, it, arg_, REV_CMD_PARENTS_ONLY, flags);
1598 add_pending_object(revs, it, arg);
1600 return 1;
1603 void repo_init_revisions(struct repository *r,
1604 struct rev_info *revs,
1605 const char *prefix)
1607 memset(revs, 0, sizeof(*revs));
1609 revs->repo = r;
1610 revs->abbrev = DEFAULT_ABBREV;
1611 revs->ignore_merges = 1;
1612 revs->simplify_history = 1;
1613 revs->pruning.repo = r;
1614 revs->pruning.flags.recursive = 1;
1615 revs->pruning.flags.quick = 1;
1616 revs->pruning.add_remove = file_add_remove;
1617 revs->pruning.change = file_change;
1618 revs->pruning.change_fn_data = revs;
1619 revs->sort_order = REV_SORT_IN_GRAPH_ORDER;
1620 revs->dense = 1;
1621 revs->prefix = prefix;
1622 revs->max_age = -1;
1623 revs->min_age = -1;
1624 revs->skip_count = -1;
1625 revs->max_count = -1;
1626 revs->max_parents = -1;
1627 revs->expand_tabs_in_log = -1;
1629 revs->commit_format = CMIT_FMT_DEFAULT;
1630 revs->expand_tabs_in_log_default = 8;
1632 init_grep_defaults(revs->repo);
1633 grep_init(&revs->grep_filter, revs->repo, prefix);
1634 revs->grep_filter.status_only = 1;
1636 repo_diff_setup(revs->repo, &revs->diffopt);
1637 if (prefix && !revs->diffopt.prefix) {
1638 revs->diffopt.prefix = prefix;
1639 revs->diffopt.prefix_length = strlen(prefix);
1642 revs->notes_opt.use_default_notes = -1;
1645 static void add_pending_commit_list(struct rev_info *revs,
1646 struct commit_list *commit_list,
1647 unsigned int flags)
1649 while (commit_list) {
1650 struct object *object = &commit_list->item->object;
1651 object->flags |= flags;
1652 add_pending_object(revs, object, oid_to_hex(&object->oid));
1653 commit_list = commit_list->next;
1657 static void prepare_show_merge(struct rev_info *revs)
1659 struct commit_list *bases;
1660 struct commit *head, *other;
1661 struct object_id oid;
1662 const char **prune = NULL;
1663 int i, prune_num = 1; /* counting terminating NULL */
1664 struct index_state *istate = revs->repo->index;
1666 if (get_oid("HEAD", &oid))
1667 die("--merge without HEAD?");
1668 head = lookup_commit_or_die(&oid, "HEAD");
1669 if (get_oid("MERGE_HEAD", &oid))
1670 die("--merge without MERGE_HEAD?");
1671 other = lookup_commit_or_die(&oid, "MERGE_HEAD");
1672 add_pending_object(revs, &head->object, "HEAD");
1673 add_pending_object(revs, &other->object, "MERGE_HEAD");
1674 bases = get_merge_bases(head, other);
1675 add_rev_cmdline_list(revs, bases, REV_CMD_MERGE_BASE, UNINTERESTING | BOTTOM);
1676 add_pending_commit_list(revs, bases, UNINTERESTING | BOTTOM);
1677 free_commit_list(bases);
1678 head->object.flags |= SYMMETRIC_LEFT;
1680 if (!istate->cache_nr)
1681 repo_read_index(revs->repo);
1682 for (i = 0; i < istate->cache_nr; i++) {
1683 const struct cache_entry *ce = istate->cache[i];
1684 if (!ce_stage(ce))
1685 continue;
1686 if (ce_path_match(istate, ce, &revs->prune_data, NULL)) {
1687 prune_num++;
1688 REALLOC_ARRAY(prune, prune_num);
1689 prune[prune_num-2] = ce->name;
1690 prune[prune_num-1] = NULL;
1692 while ((i+1 < istate->cache_nr) &&
1693 ce_same_name(ce, istate->cache[i+1]))
1694 i++;
1696 clear_pathspec(&revs->prune_data);
1697 parse_pathspec(&revs->prune_data, PATHSPEC_ALL_MAGIC & ~PATHSPEC_LITERAL,
1698 PATHSPEC_PREFER_FULL | PATHSPEC_LITERAL_PATH, "", prune);
1699 revs->limited = 1;
1702 static int dotdot_missing(const char *arg, char *dotdot,
1703 struct rev_info *revs, int symmetric)
1705 if (revs->ignore_missing)
1706 return 0;
1707 /* de-munge so we report the full argument */
1708 *dotdot = '.';
1709 die(symmetric
1710 ? "Invalid symmetric difference expression %s"
1711 : "Invalid revision range %s", arg);
1714 static int handle_dotdot_1(const char *arg, char *dotdot,
1715 struct rev_info *revs, int flags,
1716 int cant_be_filename,
1717 struct object_context *a_oc,
1718 struct object_context *b_oc)
1720 const char *a_name, *b_name;
1721 struct object_id a_oid, b_oid;
1722 struct object *a_obj, *b_obj;
1723 unsigned int a_flags, b_flags;
1724 int symmetric = 0;
1725 unsigned int flags_exclude = flags ^ (UNINTERESTING | BOTTOM);
1726 unsigned int oc_flags = GET_OID_COMMITTISH | GET_OID_RECORD_PATH;
1728 a_name = arg;
1729 if (!*a_name)
1730 a_name = "HEAD";
1732 b_name = dotdot + 2;
1733 if (*b_name == '.') {
1734 symmetric = 1;
1735 b_name++;
1737 if (!*b_name)
1738 b_name = "HEAD";
1740 if (get_oid_with_context(revs->repo, a_name, oc_flags, &a_oid, a_oc) ||
1741 get_oid_with_context(revs->repo, b_name, oc_flags, &b_oid, b_oc))
1742 return -1;
1744 if (!cant_be_filename) {
1745 *dotdot = '.';
1746 verify_non_filename(revs->prefix, arg);
1747 *dotdot = '\0';
1750 a_obj = parse_object(revs->repo, &a_oid);
1751 b_obj = parse_object(revs->repo, &b_oid);
1752 if (!a_obj || !b_obj)
1753 return dotdot_missing(arg, dotdot, revs, symmetric);
1755 if (!symmetric) {
1756 /* just A..B */
1757 b_flags = flags;
1758 a_flags = flags_exclude;
1759 } else {
1760 /* A...B -- find merge bases between the two */
1761 struct commit *a, *b;
1762 struct commit_list *exclude;
1764 a = lookup_commit_reference(revs->repo, &a_obj->oid);
1765 b = lookup_commit_reference(revs->repo, &b_obj->oid);
1766 if (!a || !b)
1767 return dotdot_missing(arg, dotdot, revs, symmetric);
1769 exclude = get_merge_bases(a, b);
1770 add_rev_cmdline_list(revs, exclude, REV_CMD_MERGE_BASE,
1771 flags_exclude);
1772 add_pending_commit_list(revs, exclude, flags_exclude);
1773 free_commit_list(exclude);
1775 b_flags = flags;
1776 a_flags = flags | SYMMETRIC_LEFT;
1779 a_obj->flags |= a_flags;
1780 b_obj->flags |= b_flags;
1781 add_rev_cmdline(revs, a_obj, a_name, REV_CMD_LEFT, a_flags);
1782 add_rev_cmdline(revs, b_obj, b_name, REV_CMD_RIGHT, b_flags);
1783 add_pending_object_with_path(revs, a_obj, a_name, a_oc->mode, a_oc->path);
1784 add_pending_object_with_path(revs, b_obj, b_name, b_oc->mode, b_oc->path);
1785 return 0;
1788 static int handle_dotdot(const char *arg,
1789 struct rev_info *revs, int flags,
1790 int cant_be_filename)
1792 struct object_context a_oc, b_oc;
1793 char *dotdot = strstr(arg, "..");
1794 int ret;
1796 if (!dotdot)
1797 return -1;
1799 memset(&a_oc, 0, sizeof(a_oc));
1800 memset(&b_oc, 0, sizeof(b_oc));
1802 *dotdot = '\0';
1803 ret = handle_dotdot_1(arg, dotdot, revs, flags, cant_be_filename,
1804 &a_oc, &b_oc);
1805 *dotdot = '.';
1807 free(a_oc.path);
1808 free(b_oc.path);
1810 return ret;
1813 int handle_revision_arg(const char *arg_, struct rev_info *revs, int flags, unsigned revarg_opt)
1815 struct object_context oc;
1816 char *mark;
1817 struct object *object;
1818 struct object_id oid;
1819 int local_flags;
1820 const char *arg = arg_;
1821 int cant_be_filename = revarg_opt & REVARG_CANNOT_BE_FILENAME;
1822 unsigned get_sha1_flags = GET_OID_RECORD_PATH;
1824 flags = flags & UNINTERESTING ? flags | BOTTOM : flags & ~BOTTOM;
1826 if (!cant_be_filename && !strcmp(arg, "..")) {
1828 * Just ".."? That is not a range but the
1829 * pathspec for the parent directory.
1831 return -1;
1834 if (!handle_dotdot(arg, revs, flags, revarg_opt))
1835 return 0;
1837 mark = strstr(arg, "^@");
1838 if (mark && !mark[2]) {
1839 *mark = 0;
1840 if (add_parents_only(revs, arg, flags, 0))
1841 return 0;
1842 *mark = '^';
1844 mark = strstr(arg, "^!");
1845 if (mark && !mark[2]) {
1846 *mark = 0;
1847 if (!add_parents_only(revs, arg, flags ^ (UNINTERESTING | BOTTOM), 0))
1848 *mark = '^';
1850 mark = strstr(arg, "^-");
1851 if (mark) {
1852 int exclude_parent = 1;
1854 if (mark[2]) {
1855 char *end;
1856 exclude_parent = strtoul(mark + 2, &end, 10);
1857 if (*end != '\0' || !exclude_parent)
1858 return -1;
1861 *mark = 0;
1862 if (!add_parents_only(revs, arg, flags ^ (UNINTERESTING | BOTTOM), exclude_parent))
1863 *mark = '^';
1866 local_flags = 0;
1867 if (*arg == '^') {
1868 local_flags = UNINTERESTING | BOTTOM;
1869 arg++;
1872 if (revarg_opt & REVARG_COMMITTISH)
1873 get_sha1_flags |= GET_OID_COMMITTISH;
1875 if (get_oid_with_context(revs->repo, arg, get_sha1_flags, &oid, &oc))
1876 return revs->ignore_missing ? 0 : -1;
1877 if (!cant_be_filename)
1878 verify_non_filename(revs->prefix, arg);
1879 object = get_reference(revs, arg, &oid, flags ^ local_flags);
1880 if (!object)
1881 return revs->ignore_missing ? 0 : -1;
1882 add_rev_cmdline(revs, object, arg_, REV_CMD_REV, flags ^ local_flags);
1883 add_pending_object_with_path(revs, object, arg, oc.mode, oc.path);
1884 free(oc.path);
1885 return 0;
1888 static void read_pathspec_from_stdin(struct strbuf *sb,
1889 struct argv_array *prune)
1891 while (strbuf_getline(sb, stdin) != EOF)
1892 argv_array_push(prune, sb->buf);
1895 static void read_revisions_from_stdin(struct rev_info *revs,
1896 struct argv_array *prune)
1898 struct strbuf sb;
1899 int seen_dashdash = 0;
1900 int save_warning;
1902 save_warning = warn_on_object_refname_ambiguity;
1903 warn_on_object_refname_ambiguity = 0;
1905 strbuf_init(&sb, 1000);
1906 while (strbuf_getline(&sb, stdin) != EOF) {
1907 int len = sb.len;
1908 if (!len)
1909 break;
1910 if (sb.buf[0] == '-') {
1911 if (len == 2 && sb.buf[1] == '-') {
1912 seen_dashdash = 1;
1913 break;
1915 die("options not supported in --stdin mode");
1917 if (handle_revision_arg(sb.buf, revs, 0,
1918 REVARG_CANNOT_BE_FILENAME))
1919 die("bad revision '%s'", sb.buf);
1921 if (seen_dashdash)
1922 read_pathspec_from_stdin(&sb, prune);
1924 strbuf_release(&sb);
1925 warn_on_object_refname_ambiguity = save_warning;
1928 static void add_grep(struct rev_info *revs, const char *ptn, enum grep_pat_token what)
1930 append_grep_pattern(&revs->grep_filter, ptn, "command line", 0, what);
1933 static void add_header_grep(struct rev_info *revs, enum grep_header_field field, const char *pattern)
1935 append_header_grep_pattern(&revs->grep_filter, field, pattern);
1938 static void add_message_grep(struct rev_info *revs, const char *pattern)
1940 add_grep(revs, pattern, GREP_PATTERN_BODY);
1943 static int handle_revision_opt(struct rev_info *revs, int argc, const char **argv,
1944 int *unkc, const char **unkv,
1945 const struct setup_revision_opt* opt)
1947 const char *arg = argv[0];
1948 const char *optarg;
1949 int argcount;
1950 const unsigned hexsz = the_hash_algo->hexsz;
1952 /* pseudo revision arguments */
1953 if (!strcmp(arg, "--all") || !strcmp(arg, "--branches") ||
1954 !strcmp(arg, "--tags") || !strcmp(arg, "--remotes") ||
1955 !strcmp(arg, "--reflog") || !strcmp(arg, "--not") ||
1956 !strcmp(arg, "--no-walk") || !strcmp(arg, "--do-walk") ||
1957 !strcmp(arg, "--bisect") || starts_with(arg, "--glob=") ||
1958 !strcmp(arg, "--indexed-objects") ||
1959 starts_with(arg, "--exclude=") ||
1960 starts_with(arg, "--branches=") || starts_with(arg, "--tags=") ||
1961 starts_with(arg, "--remotes=") || starts_with(arg, "--no-walk="))
1963 unkv[(*unkc)++] = arg;
1964 return 1;
1967 if ((argcount = parse_long_opt("max-count", argv, &optarg))) {
1968 revs->max_count = atoi(optarg);
1969 revs->no_walk = 0;
1970 return argcount;
1971 } else if ((argcount = parse_long_opt("skip", argv, &optarg))) {
1972 revs->skip_count = atoi(optarg);
1973 return argcount;
1974 } else if ((*arg == '-') && isdigit(arg[1])) {
1975 /* accept -<digit>, like traditional "head" */
1976 if (strtol_i(arg + 1, 10, &revs->max_count) < 0 ||
1977 revs->max_count < 0)
1978 die("'%s': not a non-negative integer", arg + 1);
1979 revs->no_walk = 0;
1980 } else if (!strcmp(arg, "-n")) {
1981 if (argc <= 1)
1982 return error("-n requires an argument");
1983 revs->max_count = atoi(argv[1]);
1984 revs->no_walk = 0;
1985 return 2;
1986 } else if (skip_prefix(arg, "-n", &optarg)) {
1987 revs->max_count = atoi(optarg);
1988 revs->no_walk = 0;
1989 } else if ((argcount = parse_long_opt("max-age", argv, &optarg))) {
1990 revs->max_age = atoi(optarg);
1991 return argcount;
1992 } else if ((argcount = parse_long_opt("since", argv, &optarg))) {
1993 revs->max_age = approxidate(optarg);
1994 return argcount;
1995 } else if ((argcount = parse_long_opt("after", argv, &optarg))) {
1996 revs->max_age = approxidate(optarg);
1997 return argcount;
1998 } else if ((argcount = parse_long_opt("min-age", argv, &optarg))) {
1999 revs->min_age = atoi(optarg);
2000 return argcount;
2001 } else if ((argcount = parse_long_opt("before", argv, &optarg))) {
2002 revs->min_age = approxidate(optarg);
2003 return argcount;
2004 } else if ((argcount = parse_long_opt("until", argv, &optarg))) {
2005 revs->min_age = approxidate(optarg);
2006 return argcount;
2007 } else if (!strcmp(arg, "--first-parent")) {
2008 revs->first_parent_only = 1;
2009 } else if (!strcmp(arg, "--ancestry-path")) {
2010 revs->ancestry_path = 1;
2011 revs->simplify_history = 0;
2012 revs->limited = 1;
2013 } else if (!strcmp(arg, "-g") || !strcmp(arg, "--walk-reflogs")) {
2014 init_reflog_walk(&revs->reflog_info);
2015 } else if (!strcmp(arg, "--default")) {
2016 if (argc <= 1)
2017 return error("bad --default argument");
2018 revs->def = argv[1];
2019 return 2;
2020 } else if (!strcmp(arg, "--merge")) {
2021 revs->show_merge = 1;
2022 } else if (!strcmp(arg, "--topo-order")) {
2023 revs->sort_order = REV_SORT_IN_GRAPH_ORDER;
2024 revs->topo_order = 1;
2025 } else if (!strcmp(arg, "--simplify-merges")) {
2026 revs->simplify_merges = 1;
2027 revs->topo_order = 1;
2028 revs->rewrite_parents = 1;
2029 revs->simplify_history = 0;
2030 revs->limited = 1;
2031 } else if (!strcmp(arg, "--simplify-by-decoration")) {
2032 revs->simplify_merges = 1;
2033 revs->topo_order = 1;
2034 revs->rewrite_parents = 1;
2035 revs->simplify_history = 0;
2036 revs->simplify_by_decoration = 1;
2037 revs->limited = 1;
2038 revs->prune = 1;
2039 load_ref_decorations(NULL, DECORATE_SHORT_REFS);
2040 } else if (!strcmp(arg, "--date-order")) {
2041 revs->sort_order = REV_SORT_BY_COMMIT_DATE;
2042 revs->topo_order = 1;
2043 } else if (!strcmp(arg, "--author-date-order")) {
2044 revs->sort_order = REV_SORT_BY_AUTHOR_DATE;
2045 revs->topo_order = 1;
2046 } else if (!strcmp(arg, "--early-output")) {
2047 revs->early_output = 100;
2048 revs->topo_order = 1;
2049 } else if (skip_prefix(arg, "--early-output=", &optarg)) {
2050 if (strtoul_ui(optarg, 10, &revs->early_output) < 0)
2051 die("'%s': not a non-negative integer", optarg);
2052 revs->topo_order = 1;
2053 } else if (!strcmp(arg, "--parents")) {
2054 revs->rewrite_parents = 1;
2055 revs->print_parents = 1;
2056 } else if (!strcmp(arg, "--dense")) {
2057 revs->dense = 1;
2058 } else if (!strcmp(arg, "--sparse")) {
2059 revs->dense = 0;
2060 } else if (!strcmp(arg, "--in-commit-order")) {
2061 revs->tree_blobs_in_commit_order = 1;
2062 } else if (!strcmp(arg, "--remove-empty")) {
2063 revs->remove_empty_trees = 1;
2064 } else if (!strcmp(arg, "--merges")) {
2065 revs->min_parents = 2;
2066 } else if (!strcmp(arg, "--no-merges")) {
2067 revs->max_parents = 1;
2068 } else if (skip_prefix(arg, "--min-parents=", &optarg)) {
2069 revs->min_parents = atoi(optarg);
2070 } else if (!strcmp(arg, "--no-min-parents")) {
2071 revs->min_parents = 0;
2072 } else if (skip_prefix(arg, "--max-parents=", &optarg)) {
2073 revs->max_parents = atoi(optarg);
2074 } else if (!strcmp(arg, "--no-max-parents")) {
2075 revs->max_parents = -1;
2076 } else if (!strcmp(arg, "--boundary")) {
2077 revs->boundary = 1;
2078 } else if (!strcmp(arg, "--left-right")) {
2079 revs->left_right = 1;
2080 } else if (!strcmp(arg, "--left-only")) {
2081 if (revs->right_only)
2082 die("--left-only is incompatible with --right-only"
2083 " or --cherry");
2084 revs->left_only = 1;
2085 } else if (!strcmp(arg, "--right-only")) {
2086 if (revs->left_only)
2087 die("--right-only is incompatible with --left-only");
2088 revs->right_only = 1;
2089 } else if (!strcmp(arg, "--cherry")) {
2090 if (revs->left_only)
2091 die("--cherry is incompatible with --left-only");
2092 revs->cherry_mark = 1;
2093 revs->right_only = 1;
2094 revs->max_parents = 1;
2095 revs->limited = 1;
2096 } else if (!strcmp(arg, "--count")) {
2097 revs->count = 1;
2098 } else if (!strcmp(arg, "--cherry-mark")) {
2099 if (revs->cherry_pick)
2100 die("--cherry-mark is incompatible with --cherry-pick");
2101 revs->cherry_mark = 1;
2102 revs->limited = 1; /* needs limit_list() */
2103 } else if (!strcmp(arg, "--cherry-pick")) {
2104 if (revs->cherry_mark)
2105 die("--cherry-pick is incompatible with --cherry-mark");
2106 revs->cherry_pick = 1;
2107 revs->limited = 1;
2108 } else if (!strcmp(arg, "--objects")) {
2109 revs->tag_objects = 1;
2110 revs->tree_objects = 1;
2111 revs->blob_objects = 1;
2112 } else if (!strcmp(arg, "--objects-edge")) {
2113 revs->tag_objects = 1;
2114 revs->tree_objects = 1;
2115 revs->blob_objects = 1;
2116 revs->edge_hint = 1;
2117 } else if (!strcmp(arg, "--objects-edge-aggressive")) {
2118 revs->tag_objects = 1;
2119 revs->tree_objects = 1;
2120 revs->blob_objects = 1;
2121 revs->edge_hint = 1;
2122 revs->edge_hint_aggressive = 1;
2123 } else if (!strcmp(arg, "--verify-objects")) {
2124 revs->tag_objects = 1;
2125 revs->tree_objects = 1;
2126 revs->blob_objects = 1;
2127 revs->verify_objects = 1;
2128 } else if (!strcmp(arg, "--unpacked")) {
2129 revs->unpacked = 1;
2130 } else if (starts_with(arg, "--unpacked=")) {
2131 die("--unpacked=<packfile> no longer supported.");
2132 } else if (!strcmp(arg, "-r")) {
2133 revs->diff = 1;
2134 revs->diffopt.flags.recursive = 1;
2135 } else if (!strcmp(arg, "-t")) {
2136 revs->diff = 1;
2137 revs->diffopt.flags.recursive = 1;
2138 revs->diffopt.flags.tree_in_recursive = 1;
2139 } else if (!strcmp(arg, "-m")) {
2140 revs->ignore_merges = 0;
2141 } else if (!strcmp(arg, "-c")) {
2142 revs->diff = 1;
2143 revs->dense_combined_merges = 0;
2144 revs->combine_merges = 1;
2145 } else if (!strcmp(arg, "--combined-all-paths")) {
2146 revs->diff = 1;
2147 revs->combined_all_paths = 1;
2148 } else if (!strcmp(arg, "--cc")) {
2149 revs->diff = 1;
2150 revs->dense_combined_merges = 1;
2151 revs->combine_merges = 1;
2152 } else if (!strcmp(arg, "-v")) {
2153 revs->verbose_header = 1;
2154 } else if (!strcmp(arg, "--pretty")) {
2155 revs->verbose_header = 1;
2156 revs->pretty_given = 1;
2157 get_commit_format(NULL, revs);
2158 } else if (skip_prefix(arg, "--pretty=", &optarg) ||
2159 skip_prefix(arg, "--format=", &optarg)) {
2161 * Detached form ("--pretty X" as opposed to "--pretty=X")
2162 * not allowed, since the argument is optional.
2164 revs->verbose_header = 1;
2165 revs->pretty_given = 1;
2166 get_commit_format(optarg, revs);
2167 } else if (!strcmp(arg, "--expand-tabs")) {
2168 revs->expand_tabs_in_log = 8;
2169 } else if (!strcmp(arg, "--no-expand-tabs")) {
2170 revs->expand_tabs_in_log = 0;
2171 } else if (skip_prefix(arg, "--expand-tabs=", &arg)) {
2172 int val;
2173 if (strtol_i(arg, 10, &val) < 0 || val < 0)
2174 die("'%s': not a non-negative integer", arg);
2175 revs->expand_tabs_in_log = val;
2176 } else if (!strcmp(arg, "--show-notes") || !strcmp(arg, "--notes")) {
2177 revs->show_notes = 1;
2178 revs->show_notes_given = 1;
2179 revs->notes_opt.use_default_notes = 1;
2180 } else if (!strcmp(arg, "--show-signature")) {
2181 revs->show_signature = 1;
2182 } else if (!strcmp(arg, "--no-show-signature")) {
2183 revs->show_signature = 0;
2184 } else if (!strcmp(arg, "--show-linear-break")) {
2185 revs->break_bar = " ..........";
2186 revs->track_linear = 1;
2187 revs->track_first_time = 1;
2188 } else if (skip_prefix(arg, "--show-linear-break=", &optarg)) {
2189 revs->break_bar = xstrdup(optarg);
2190 revs->track_linear = 1;
2191 revs->track_first_time = 1;
2192 } else if (skip_prefix(arg, "--show-notes=", &optarg) ||
2193 skip_prefix(arg, "--notes=", &optarg)) {
2194 struct strbuf buf = STRBUF_INIT;
2195 revs->show_notes = 1;
2196 revs->show_notes_given = 1;
2197 if (starts_with(arg, "--show-notes=") &&
2198 revs->notes_opt.use_default_notes < 0)
2199 revs->notes_opt.use_default_notes = 1;
2200 strbuf_addstr(&buf, optarg);
2201 expand_notes_ref(&buf);
2202 string_list_append(&revs->notes_opt.extra_notes_refs,
2203 strbuf_detach(&buf, NULL));
2204 } else if (!strcmp(arg, "--no-notes")) {
2205 revs->show_notes = 0;
2206 revs->show_notes_given = 1;
2207 revs->notes_opt.use_default_notes = -1;
2208 /* we have been strdup'ing ourselves, so trick
2209 * string_list into free()ing strings */
2210 revs->notes_opt.extra_notes_refs.strdup_strings = 1;
2211 string_list_clear(&revs->notes_opt.extra_notes_refs, 0);
2212 revs->notes_opt.extra_notes_refs.strdup_strings = 0;
2213 } else if (!strcmp(arg, "--standard-notes")) {
2214 revs->show_notes_given = 1;
2215 revs->notes_opt.use_default_notes = 1;
2216 } else if (!strcmp(arg, "--no-standard-notes")) {
2217 revs->notes_opt.use_default_notes = 0;
2218 } else if (!strcmp(arg, "--oneline")) {
2219 revs->verbose_header = 1;
2220 get_commit_format("oneline", revs);
2221 revs->pretty_given = 1;
2222 revs->abbrev_commit = 1;
2223 } else if (!strcmp(arg, "--graph")) {
2224 revs->topo_order = 1;
2225 revs->rewrite_parents = 1;
2226 revs->graph = graph_init(revs);
2227 } else if (!strcmp(arg, "--root")) {
2228 revs->show_root_diff = 1;
2229 } else if (!strcmp(arg, "--no-commit-id")) {
2230 revs->no_commit_id = 1;
2231 } else if (!strcmp(arg, "--always")) {
2232 revs->always_show_header = 1;
2233 } else if (!strcmp(arg, "--no-abbrev")) {
2234 revs->abbrev = 0;
2235 } else if (!strcmp(arg, "--abbrev")) {
2236 revs->abbrev = DEFAULT_ABBREV;
2237 } else if (skip_prefix(arg, "--abbrev=", &optarg)) {
2238 revs->abbrev = strtoul(optarg, NULL, 10);
2239 if (revs->abbrev < MINIMUM_ABBREV)
2240 revs->abbrev = MINIMUM_ABBREV;
2241 else if (revs->abbrev > hexsz)
2242 revs->abbrev = hexsz;
2243 } else if (!strcmp(arg, "--abbrev-commit")) {
2244 revs->abbrev_commit = 1;
2245 revs->abbrev_commit_given = 1;
2246 } else if (!strcmp(arg, "--no-abbrev-commit")) {
2247 revs->abbrev_commit = 0;
2248 } else if (!strcmp(arg, "--full-diff")) {
2249 revs->diff = 1;
2250 revs->full_diff = 1;
2251 } else if (!strcmp(arg, "--full-history")) {
2252 revs->simplify_history = 0;
2253 } else if (!strcmp(arg, "--relative-date")) {
2254 revs->date_mode.type = DATE_RELATIVE;
2255 revs->date_mode_explicit = 1;
2256 } else if ((argcount = parse_long_opt("date", argv, &optarg))) {
2257 parse_date_format(optarg, &revs->date_mode);
2258 revs->date_mode_explicit = 1;
2259 return argcount;
2260 } else if (!strcmp(arg, "--log-size")) {
2261 revs->show_log_size = 1;
2264 * Grepping the commit log
2266 else if ((argcount = parse_long_opt("author", argv, &optarg))) {
2267 add_header_grep(revs, GREP_HEADER_AUTHOR, optarg);
2268 return argcount;
2269 } else if ((argcount = parse_long_opt("committer", argv, &optarg))) {
2270 add_header_grep(revs, GREP_HEADER_COMMITTER, optarg);
2271 return argcount;
2272 } else if ((argcount = parse_long_opt("grep-reflog", argv, &optarg))) {
2273 add_header_grep(revs, GREP_HEADER_REFLOG, optarg);
2274 return argcount;
2275 } else if ((argcount = parse_long_opt("grep", argv, &optarg))) {
2276 add_message_grep(revs, optarg);
2277 return argcount;
2278 } else if (!strcmp(arg, "--grep-debug")) {
2279 revs->grep_filter.debug = 1;
2280 } else if (!strcmp(arg, "--basic-regexp")) {
2281 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_BRE;
2282 } else if (!strcmp(arg, "--extended-regexp") || !strcmp(arg, "-E")) {
2283 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_ERE;
2284 } else if (!strcmp(arg, "--regexp-ignore-case") || !strcmp(arg, "-i")) {
2285 revs->grep_filter.ignore_case = 1;
2286 revs->diffopt.pickaxe_opts |= DIFF_PICKAXE_IGNORE_CASE;
2287 } else if (!strcmp(arg, "--fixed-strings") || !strcmp(arg, "-F")) {
2288 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_FIXED;
2289 } else if (!strcmp(arg, "--perl-regexp") || !strcmp(arg, "-P")) {
2290 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_PCRE;
2291 } else if (!strcmp(arg, "--all-match")) {
2292 revs->grep_filter.all_match = 1;
2293 } else if (!strcmp(arg, "--invert-grep")) {
2294 revs->invert_grep = 1;
2295 } else if ((argcount = parse_long_opt("encoding", argv, &optarg))) {
2296 if (strcmp(optarg, "none"))
2297 git_log_output_encoding = xstrdup(optarg);
2298 else
2299 git_log_output_encoding = "";
2300 return argcount;
2301 } else if (!strcmp(arg, "--reverse")) {
2302 revs->reverse ^= 1;
2303 } else if (!strcmp(arg, "--children")) {
2304 revs->children.name = "children";
2305 revs->limited = 1;
2306 } else if (!strcmp(arg, "--ignore-missing")) {
2307 revs->ignore_missing = 1;
2308 } else if (opt && opt->allow_exclude_promisor_objects &&
2309 !strcmp(arg, "--exclude-promisor-objects")) {
2310 if (fetch_if_missing)
2311 BUG("exclude_promisor_objects can only be used when fetch_if_missing is 0");
2312 revs->exclude_promisor_objects = 1;
2313 } else {
2314 int opts = diff_opt_parse(&revs->diffopt, argv, argc, revs->prefix);
2315 if (!opts)
2316 unkv[(*unkc)++] = arg;
2317 return opts;
2319 if (revs->graph && revs->track_linear)
2320 die("--show-linear-break and --graph are incompatible");
2322 return 1;
2325 void parse_revision_opt(struct rev_info *revs, struct parse_opt_ctx_t *ctx,
2326 const struct option *options,
2327 const char * const usagestr[])
2329 int n = handle_revision_opt(revs, ctx->argc, ctx->argv,
2330 &ctx->cpidx, ctx->out, NULL);
2331 if (n <= 0) {
2332 error("unknown option `%s'", ctx->argv[0]);
2333 usage_with_options(usagestr, options);
2335 ctx->argv += n;
2336 ctx->argc -= n;
2339 static int for_each_bisect_ref(struct ref_store *refs, each_ref_fn fn,
2340 void *cb_data, const char *term)
2342 struct strbuf bisect_refs = STRBUF_INIT;
2343 int status;
2344 strbuf_addf(&bisect_refs, "refs/bisect/%s", term);
2345 status = refs_for_each_fullref_in(refs, bisect_refs.buf, fn, cb_data, 0);
2346 strbuf_release(&bisect_refs);
2347 return status;
2350 static int for_each_bad_bisect_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
2352 return for_each_bisect_ref(refs, fn, cb_data, term_bad);
2355 static int for_each_good_bisect_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
2357 return for_each_bisect_ref(refs, fn, cb_data, term_good);
2360 static int handle_revision_pseudo_opt(const char *submodule,
2361 struct rev_info *revs,
2362 int argc, const char **argv, int *flags)
2364 const char *arg = argv[0];
2365 const char *optarg;
2366 struct ref_store *refs;
2367 int argcount;
2369 if (submodule) {
2371 * We need some something like get_submodule_worktrees()
2372 * before we can go through all worktrees of a submodule,
2373 * .e.g with adding all HEADs from --all, which is not
2374 * supported right now, so stick to single worktree.
2376 if (!revs->single_worktree)
2377 BUG("--single-worktree cannot be used together with submodule");
2378 refs = get_submodule_ref_store(submodule);
2379 } else
2380 refs = get_main_ref_store(revs->repo);
2383 * NOTE!
2385 * Commands like "git shortlog" will not accept the options below
2386 * unless parse_revision_opt queues them (as opposed to erroring
2387 * out).
2389 * When implementing your new pseudo-option, remember to
2390 * register it in the list at the top of handle_revision_opt.
2392 if (!strcmp(arg, "--all")) {
2393 handle_refs(refs, revs, *flags, refs_for_each_ref);
2394 handle_refs(refs, revs, *flags, refs_head_ref);
2395 if (!revs->single_worktree) {
2396 struct all_refs_cb cb;
2398 init_all_refs_cb(&cb, revs, *flags);
2399 other_head_refs(handle_one_ref, &cb);
2401 clear_ref_exclusion(&revs->ref_excludes);
2402 } else if (!strcmp(arg, "--branches")) {
2403 handle_refs(refs, revs, *flags, refs_for_each_branch_ref);
2404 clear_ref_exclusion(&revs->ref_excludes);
2405 } else if (!strcmp(arg, "--bisect")) {
2406 read_bisect_terms(&term_bad, &term_good);
2407 handle_refs(refs, revs, *flags, for_each_bad_bisect_ref);
2408 handle_refs(refs, revs, *flags ^ (UNINTERESTING | BOTTOM),
2409 for_each_good_bisect_ref);
2410 revs->bisect = 1;
2411 } else if (!strcmp(arg, "--tags")) {
2412 handle_refs(refs, revs, *flags, refs_for_each_tag_ref);
2413 clear_ref_exclusion(&revs->ref_excludes);
2414 } else if (!strcmp(arg, "--remotes")) {
2415 handle_refs(refs, revs, *flags, refs_for_each_remote_ref);
2416 clear_ref_exclusion(&revs->ref_excludes);
2417 } else if ((argcount = parse_long_opt("glob", argv, &optarg))) {
2418 struct all_refs_cb cb;
2419 init_all_refs_cb(&cb, revs, *flags);
2420 for_each_glob_ref(handle_one_ref, optarg, &cb);
2421 clear_ref_exclusion(&revs->ref_excludes);
2422 return argcount;
2423 } else if ((argcount = parse_long_opt("exclude", argv, &optarg))) {
2424 add_ref_exclusion(&revs->ref_excludes, optarg);
2425 return argcount;
2426 } else if (skip_prefix(arg, "--branches=", &optarg)) {
2427 struct all_refs_cb cb;
2428 init_all_refs_cb(&cb, revs, *flags);
2429 for_each_glob_ref_in(handle_one_ref, optarg, "refs/heads/", &cb);
2430 clear_ref_exclusion(&revs->ref_excludes);
2431 } else if (skip_prefix(arg, "--tags=", &optarg)) {
2432 struct all_refs_cb cb;
2433 init_all_refs_cb(&cb, revs, *flags);
2434 for_each_glob_ref_in(handle_one_ref, optarg, "refs/tags/", &cb);
2435 clear_ref_exclusion(&revs->ref_excludes);
2436 } else if (skip_prefix(arg, "--remotes=", &optarg)) {
2437 struct all_refs_cb cb;
2438 init_all_refs_cb(&cb, revs, *flags);
2439 for_each_glob_ref_in(handle_one_ref, optarg, "refs/remotes/", &cb);
2440 clear_ref_exclusion(&revs->ref_excludes);
2441 } else if (!strcmp(arg, "--reflog")) {
2442 add_reflogs_to_pending(revs, *flags);
2443 } else if (!strcmp(arg, "--indexed-objects")) {
2444 add_index_objects_to_pending(revs, *flags);
2445 } else if (!strcmp(arg, "--not")) {
2446 *flags ^= UNINTERESTING | BOTTOM;
2447 } else if (!strcmp(arg, "--no-walk")) {
2448 revs->no_walk = REVISION_WALK_NO_WALK_SORTED;
2449 } else if (skip_prefix(arg, "--no-walk=", &optarg)) {
2451 * Detached form ("--no-walk X" as opposed to "--no-walk=X")
2452 * not allowed, since the argument is optional.
2454 if (!strcmp(optarg, "sorted"))
2455 revs->no_walk = REVISION_WALK_NO_WALK_SORTED;
2456 else if (!strcmp(optarg, "unsorted"))
2457 revs->no_walk = REVISION_WALK_NO_WALK_UNSORTED;
2458 else
2459 return error("invalid argument to --no-walk");
2460 } else if (!strcmp(arg, "--do-walk")) {
2461 revs->no_walk = 0;
2462 } else if (!strcmp(arg, "--single-worktree")) {
2463 revs->single_worktree = 1;
2464 } else {
2465 return 0;
2468 return 1;
2471 static void NORETURN diagnose_missing_default(const char *def)
2473 int flags;
2474 const char *refname;
2476 refname = resolve_ref_unsafe(def, 0, NULL, &flags);
2477 if (!refname || !(flags & REF_ISSYMREF) || (flags & REF_ISBROKEN))
2478 die(_("your current branch appears to be broken"));
2480 skip_prefix(refname, "refs/heads/", &refname);
2481 die(_("your current branch '%s' does not have any commits yet"),
2482 refname);
2486 * Parse revision information, filling in the "rev_info" structure,
2487 * and removing the used arguments from the argument list.
2489 * Returns the number of arguments left that weren't recognized
2490 * (which are also moved to the head of the argument list)
2492 int setup_revisions(int argc, const char **argv, struct rev_info *revs, struct setup_revision_opt *opt)
2494 int i, flags, left, seen_dashdash, got_rev_arg = 0, revarg_opt;
2495 struct argv_array prune_data = ARGV_ARRAY_INIT;
2496 const char *submodule = NULL;
2498 if (opt)
2499 submodule = opt->submodule;
2501 /* First, search for "--" */
2502 if (opt && opt->assume_dashdash) {
2503 seen_dashdash = 1;
2504 } else {
2505 seen_dashdash = 0;
2506 for (i = 1; i < argc; i++) {
2507 const char *arg = argv[i];
2508 if (strcmp(arg, "--"))
2509 continue;
2510 argv[i] = NULL;
2511 argc = i;
2512 if (argv[i + 1])
2513 argv_array_pushv(&prune_data, argv + i + 1);
2514 seen_dashdash = 1;
2515 break;
2519 /* Second, deal with arguments and options */
2520 flags = 0;
2521 revarg_opt = opt ? opt->revarg_opt : 0;
2522 if (seen_dashdash)
2523 revarg_opt |= REVARG_CANNOT_BE_FILENAME;
2524 for (left = i = 1; i < argc; i++) {
2525 const char *arg = argv[i];
2526 if (*arg == '-') {
2527 int opts;
2529 opts = handle_revision_pseudo_opt(submodule,
2530 revs, argc - i, argv + i,
2531 &flags);
2532 if (opts > 0) {
2533 i += opts - 1;
2534 continue;
2537 if (!strcmp(arg, "--stdin")) {
2538 if (revs->disable_stdin) {
2539 argv[left++] = arg;
2540 continue;
2542 if (revs->read_from_stdin++)
2543 die("--stdin given twice?");
2544 read_revisions_from_stdin(revs, &prune_data);
2545 continue;
2548 opts = handle_revision_opt(revs, argc - i, argv + i,
2549 &left, argv, opt);
2550 if (opts > 0) {
2551 i += opts - 1;
2552 continue;
2554 if (opts < 0)
2555 exit(128);
2556 continue;
2560 if (handle_revision_arg(arg, revs, flags, revarg_opt)) {
2561 int j;
2562 if (seen_dashdash || *arg == '^')
2563 die("bad revision '%s'", arg);
2565 /* If we didn't have a "--":
2566 * (1) all filenames must exist;
2567 * (2) all rev-args must not be interpretable
2568 * as a valid filename.
2569 * but the latter we have checked in the main loop.
2571 for (j = i; j < argc; j++)
2572 verify_filename(revs->prefix, argv[j], j == i);
2574 argv_array_pushv(&prune_data, argv + i);
2575 break;
2577 else
2578 got_rev_arg = 1;
2581 if (prune_data.argc) {
2583 * If we need to introduce the magic "a lone ':' means no
2584 * pathspec whatsoever", here is the place to do so.
2586 * if (prune_data.nr == 1 && !strcmp(prune_data[0], ":")) {
2587 * prune_data.nr = 0;
2588 * prune_data.alloc = 0;
2589 * free(prune_data.path);
2590 * prune_data.path = NULL;
2591 * } else {
2592 * terminate prune_data.alloc with NULL and
2593 * call init_pathspec() to set revs->prune_data here.
2596 parse_pathspec(&revs->prune_data, 0, 0,
2597 revs->prefix, prune_data.argv);
2599 argv_array_clear(&prune_data);
2601 if (revs->def == NULL)
2602 revs->def = opt ? opt->def : NULL;
2603 if (opt && opt->tweak)
2604 opt->tweak(revs, opt);
2605 if (revs->show_merge)
2606 prepare_show_merge(revs);
2607 if (revs->def && !revs->pending.nr && !revs->rev_input_given && !got_rev_arg) {
2608 struct object_id oid;
2609 struct object *object;
2610 struct object_context oc;
2611 if (get_oid_with_context(revs->repo, revs->def, 0, &oid, &oc))
2612 diagnose_missing_default(revs->def);
2613 object = get_reference(revs, revs->def, &oid, 0);
2614 add_pending_object_with_mode(revs, object, revs->def, oc.mode);
2617 /* Did the user ask for any diff output? Run the diff! */
2618 if (revs->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT)
2619 revs->diff = 1;
2621 /* Pickaxe, diff-filter and rename following need diffs */
2622 if ((revs->diffopt.pickaxe_opts & DIFF_PICKAXE_KINDS_MASK) ||
2623 revs->diffopt.filter ||
2624 revs->diffopt.flags.follow_renames)
2625 revs->diff = 1;
2627 if (revs->diffopt.objfind)
2628 revs->simplify_history = 0;
2630 if (revs->topo_order && !generation_numbers_enabled(the_repository))
2631 revs->limited = 1;
2633 if (revs->prune_data.nr) {
2634 copy_pathspec(&revs->pruning.pathspec, &revs->prune_data);
2635 /* Can't prune commits with rename following: the paths change.. */
2636 if (!revs->diffopt.flags.follow_renames)
2637 revs->prune = 1;
2638 if (!revs->full_diff)
2639 copy_pathspec(&revs->diffopt.pathspec,
2640 &revs->prune_data);
2642 if (revs->combine_merges)
2643 revs->ignore_merges = 0;
2644 if (revs->combined_all_paths && !revs->combine_merges)
2645 die("--combined-all-paths makes no sense without -c or --cc");
2647 revs->diffopt.abbrev = revs->abbrev;
2649 if (revs->line_level_traverse) {
2650 revs->limited = 1;
2651 revs->topo_order = 1;
2654 diff_setup_done(&revs->diffopt);
2656 grep_commit_pattern_type(GREP_PATTERN_TYPE_UNSPECIFIED,
2657 &revs->grep_filter);
2658 compile_grep_patterns(&revs->grep_filter);
2660 if (revs->reverse && revs->reflog_info)
2661 die("cannot combine --reverse with --walk-reflogs");
2662 if (revs->reflog_info && revs->limited)
2663 die("cannot combine --walk-reflogs with history-limiting options");
2664 if (revs->rewrite_parents && revs->children.name)
2665 die("cannot combine --parents and --children");
2668 * Limitations on the graph functionality
2670 if (revs->reverse && revs->graph)
2671 die("cannot combine --reverse with --graph");
2673 if (revs->reflog_info && revs->graph)
2674 die("cannot combine --walk-reflogs with --graph");
2675 if (revs->no_walk && revs->graph)
2676 die("cannot combine --no-walk with --graph");
2677 if (!revs->reflog_info && revs->grep_filter.use_reflog_filter)
2678 die("cannot use --grep-reflog without --walk-reflogs");
2680 if (revs->first_parent_only && revs->bisect)
2681 die(_("--first-parent is incompatible with --bisect"));
2683 if (revs->line_level_traverse &&
2684 (revs->diffopt.output_format & ~(DIFF_FORMAT_PATCH | DIFF_FORMAT_NO_OUTPUT)))
2685 die(_("-L does not yet support diff formats besides -p and -s"));
2687 if (revs->expand_tabs_in_log < 0)
2688 revs->expand_tabs_in_log = revs->expand_tabs_in_log_default;
2690 return left;
2693 static void add_child(struct rev_info *revs, struct commit *parent, struct commit *child)
2695 struct commit_list *l = xcalloc(1, sizeof(*l));
2697 l->item = child;
2698 l->next = add_decoration(&revs->children, &parent->object, l);
2701 static int remove_duplicate_parents(struct rev_info *revs, struct commit *commit)
2703 struct treesame_state *ts = lookup_decoration(&revs->treesame, &commit->object);
2704 struct commit_list **pp, *p;
2705 int surviving_parents;
2707 /* Examine existing parents while marking ones we have seen... */
2708 pp = &commit->parents;
2709 surviving_parents = 0;
2710 while ((p = *pp) != NULL) {
2711 struct commit *parent = p->item;
2712 if (parent->object.flags & TMP_MARK) {
2713 *pp = p->next;
2714 if (ts)
2715 compact_treesame(revs, commit, surviving_parents);
2716 continue;
2718 parent->object.flags |= TMP_MARK;
2719 surviving_parents++;
2720 pp = &p->next;
2722 /* clear the temporary mark */
2723 for (p = commit->parents; p; p = p->next) {
2724 p->item->object.flags &= ~TMP_MARK;
2726 /* no update_treesame() - removing duplicates can't affect TREESAME */
2727 return surviving_parents;
2730 struct merge_simplify_state {
2731 struct commit *simplified;
2734 static struct merge_simplify_state *locate_simplify_state(struct rev_info *revs, struct commit *commit)
2736 struct merge_simplify_state *st;
2738 st = lookup_decoration(&revs->merge_simplification, &commit->object);
2739 if (!st) {
2740 st = xcalloc(1, sizeof(*st));
2741 add_decoration(&revs->merge_simplification, &commit->object, st);
2743 return st;
2746 static int mark_redundant_parents(struct commit *commit)
2748 struct commit_list *h = reduce_heads(commit->parents);
2749 int i = 0, marked = 0;
2750 struct commit_list *po, *pn;
2752 /* Want these for sanity-checking only */
2753 int orig_cnt = commit_list_count(commit->parents);
2754 int cnt = commit_list_count(h);
2757 * Not ready to remove items yet, just mark them for now, based
2758 * on the output of reduce_heads(). reduce_heads outputs the reduced
2759 * set in its original order, so this isn't too hard.
2761 po = commit->parents;
2762 pn = h;
2763 while (po) {
2764 if (pn && po->item == pn->item) {
2765 pn = pn->next;
2766 i++;
2767 } else {
2768 po->item->object.flags |= TMP_MARK;
2769 marked++;
2771 po=po->next;
2774 if (i != cnt || cnt+marked != orig_cnt)
2775 die("mark_redundant_parents %d %d %d %d", orig_cnt, cnt, i, marked);
2777 free_commit_list(h);
2779 return marked;
2782 static int mark_treesame_root_parents(struct commit *commit)
2784 struct commit_list *p;
2785 int marked = 0;
2787 for (p = commit->parents; p; p = p->next) {
2788 struct commit *parent = p->item;
2789 if (!parent->parents && (parent->object.flags & TREESAME)) {
2790 parent->object.flags |= TMP_MARK;
2791 marked++;
2795 return marked;
2799 * Awkward naming - this means one parent we are TREESAME to.
2800 * cf mark_treesame_root_parents: root parents that are TREESAME (to an
2801 * empty tree). Better name suggestions?
2803 static int leave_one_treesame_to_parent(struct rev_info *revs, struct commit *commit)
2805 struct treesame_state *ts = lookup_decoration(&revs->treesame, &commit->object);
2806 struct commit *unmarked = NULL, *marked = NULL;
2807 struct commit_list *p;
2808 unsigned n;
2810 for (p = commit->parents, n = 0; p; p = p->next, n++) {
2811 if (ts->treesame[n]) {
2812 if (p->item->object.flags & TMP_MARK) {
2813 if (!marked)
2814 marked = p->item;
2815 } else {
2816 if (!unmarked) {
2817 unmarked = p->item;
2818 break;
2825 * If we are TREESAME to a marked-for-deletion parent, but not to any
2826 * unmarked parents, unmark the first TREESAME parent. This is the
2827 * parent that the default simplify_history==1 scan would have followed,
2828 * and it doesn't make sense to omit that path when asking for a
2829 * simplified full history. Retaining it improves the chances of
2830 * understanding odd missed merges that took an old version of a file.
2832 * Example:
2834 * I--------*X A modified the file, but mainline merge X used
2835 * \ / "-s ours", so took the version from I. X is
2836 * `-*A--' TREESAME to I and !TREESAME to A.
2838 * Default log from X would produce "I". Without this check,
2839 * --full-history --simplify-merges would produce "I-A-X", showing
2840 * the merge commit X and that it changed A, but not making clear that
2841 * it had just taken the I version. With this check, the topology above
2842 * is retained.
2844 * Note that it is possible that the simplification chooses a different
2845 * TREESAME parent from the default, in which case this test doesn't
2846 * activate, and we _do_ drop the default parent. Example:
2848 * I------X A modified the file, but it was reverted in B,
2849 * \ / meaning mainline merge X is TREESAME to both
2850 * *A-*B parents.
2852 * Default log would produce "I" by following the first parent;
2853 * --full-history --simplify-merges will produce "I-A-B". But this is a
2854 * reasonable result - it presents a logical full history leading from
2855 * I to X, and X is not an important merge.
2857 if (!unmarked && marked) {
2858 marked->object.flags &= ~TMP_MARK;
2859 return 1;
2862 return 0;
2865 static int remove_marked_parents(struct rev_info *revs, struct commit *commit)
2867 struct commit_list **pp, *p;
2868 int nth_parent, removed = 0;
2870 pp = &commit->parents;
2871 nth_parent = 0;
2872 while ((p = *pp) != NULL) {
2873 struct commit *parent = p->item;
2874 if (parent->object.flags & TMP_MARK) {
2875 parent->object.flags &= ~TMP_MARK;
2876 *pp = p->next;
2877 free(p);
2878 removed++;
2879 compact_treesame(revs, commit, nth_parent);
2880 continue;
2882 pp = &p->next;
2883 nth_parent++;
2886 /* Removing parents can only increase TREESAMEness */
2887 if (removed && !(commit->object.flags & TREESAME))
2888 update_treesame(revs, commit);
2890 return nth_parent;
2893 static struct commit_list **simplify_one(struct rev_info *revs, struct commit *commit, struct commit_list **tail)
2895 struct commit_list *p;
2896 struct commit *parent;
2897 struct merge_simplify_state *st, *pst;
2898 int cnt;
2900 st = locate_simplify_state(revs, commit);
2903 * Have we handled this one?
2905 if (st->simplified)
2906 return tail;
2909 * An UNINTERESTING commit simplifies to itself, so does a
2910 * root commit. We do not rewrite parents of such commit
2911 * anyway.
2913 if ((commit->object.flags & UNINTERESTING) || !commit->parents) {
2914 st->simplified = commit;
2915 return tail;
2919 * Do we know what commit all of our parents that matter
2920 * should be rewritten to? Otherwise we are not ready to
2921 * rewrite this one yet.
2923 for (cnt = 0, p = commit->parents; p; p = p->next) {
2924 pst = locate_simplify_state(revs, p->item);
2925 if (!pst->simplified) {
2926 tail = &commit_list_insert(p->item, tail)->next;
2927 cnt++;
2929 if (revs->first_parent_only)
2930 break;
2932 if (cnt) {
2933 tail = &commit_list_insert(commit, tail)->next;
2934 return tail;
2938 * Rewrite our list of parents. Note that this cannot
2939 * affect our TREESAME flags in any way - a commit is
2940 * always TREESAME to its simplification.
2942 for (p = commit->parents; p; p = p->next) {
2943 pst = locate_simplify_state(revs, p->item);
2944 p->item = pst->simplified;
2945 if (revs->first_parent_only)
2946 break;
2949 if (revs->first_parent_only)
2950 cnt = 1;
2951 else
2952 cnt = remove_duplicate_parents(revs, commit);
2955 * It is possible that we are a merge and one side branch
2956 * does not have any commit that touches the given paths;
2957 * in such a case, the immediate parent from that branch
2958 * will be rewritten to be the merge base.
2960 * o----X X: the commit we are looking at;
2961 * / / o: a commit that touches the paths;
2962 * ---o----'
2964 * Further, a merge of an independent branch that doesn't
2965 * touch the path will reduce to a treesame root parent:
2967 * ----o----X X: the commit we are looking at;
2968 * / o: a commit that touches the paths;
2969 * r r: a root commit not touching the paths
2971 * Detect and simplify both cases.
2973 if (1 < cnt) {
2974 int marked = mark_redundant_parents(commit);
2975 marked += mark_treesame_root_parents(commit);
2976 if (marked)
2977 marked -= leave_one_treesame_to_parent(revs, commit);
2978 if (marked)
2979 cnt = remove_marked_parents(revs, commit);
2983 * A commit simplifies to itself if it is a root, if it is
2984 * UNINTERESTING, if it touches the given paths, or if it is a
2985 * merge and its parents don't simplify to one relevant commit
2986 * (the first two cases are already handled at the beginning of
2987 * this function).
2989 * Otherwise, it simplifies to what its sole relevant parent
2990 * simplifies to.
2992 if (!cnt ||
2993 (commit->object.flags & UNINTERESTING) ||
2994 !(commit->object.flags & TREESAME) ||
2995 (parent = one_relevant_parent(revs, commit->parents)) == NULL)
2996 st->simplified = commit;
2997 else {
2998 pst = locate_simplify_state(revs, parent);
2999 st->simplified = pst->simplified;
3001 return tail;
3004 static void simplify_merges(struct rev_info *revs)
3006 struct commit_list *list, *next;
3007 struct commit_list *yet_to_do, **tail;
3008 struct commit *commit;
3010 if (!revs->prune)
3011 return;
3013 /* feed the list reversed */
3014 yet_to_do = NULL;
3015 for (list = revs->commits; list; list = next) {
3016 commit = list->item;
3017 next = list->next;
3019 * Do not free(list) here yet; the original list
3020 * is used later in this function.
3022 commit_list_insert(commit, &yet_to_do);
3024 while (yet_to_do) {
3025 list = yet_to_do;
3026 yet_to_do = NULL;
3027 tail = &yet_to_do;
3028 while (list) {
3029 commit = pop_commit(&list);
3030 tail = simplify_one(revs, commit, tail);
3034 /* clean up the result, removing the simplified ones */
3035 list = revs->commits;
3036 revs->commits = NULL;
3037 tail = &revs->commits;
3038 while (list) {
3039 struct merge_simplify_state *st;
3041 commit = pop_commit(&list);
3042 st = locate_simplify_state(revs, commit);
3043 if (st->simplified == commit)
3044 tail = &commit_list_insert(commit, tail)->next;
3048 static void set_children(struct rev_info *revs)
3050 struct commit_list *l;
3051 for (l = revs->commits; l; l = l->next) {
3052 struct commit *commit = l->item;
3053 struct commit_list *p;
3055 for (p = commit->parents; p; p = p->next)
3056 add_child(revs, p->item, commit);
3060 void reset_revision_walk(void)
3062 clear_object_flags(SEEN | ADDED | SHOWN);
3065 static int mark_uninteresting(const struct object_id *oid,
3066 struct packed_git *pack,
3067 uint32_t pos,
3068 void *cb)
3070 struct rev_info *revs = cb;
3071 struct object *o = parse_object(revs->repo, oid);
3072 o->flags |= UNINTERESTING | SEEN;
3073 return 0;
3076 define_commit_slab(indegree_slab, int);
3077 define_commit_slab(author_date_slab, timestamp_t);
3079 struct topo_walk_info {
3080 uint32_t min_generation;
3081 struct prio_queue explore_queue;
3082 struct prio_queue indegree_queue;
3083 struct prio_queue topo_queue;
3084 struct indegree_slab indegree;
3085 struct author_date_slab author_date;
3088 static inline void test_flag_and_insert(struct prio_queue *q, struct commit *c, int flag)
3090 if (c->object.flags & flag)
3091 return;
3093 c->object.flags |= flag;
3094 prio_queue_put(q, c);
3097 static void explore_walk_step(struct rev_info *revs)
3099 struct topo_walk_info *info = revs->topo_walk_info;
3100 struct commit_list *p;
3101 struct commit *c = prio_queue_get(&info->explore_queue);
3103 if (!c)
3104 return;
3106 if (parse_commit_gently(c, 1) < 0)
3107 return;
3109 if (revs->sort_order == REV_SORT_BY_AUTHOR_DATE)
3110 record_author_date(&info->author_date, c);
3112 if (revs->max_age != -1 && (c->date < revs->max_age))
3113 c->object.flags |= UNINTERESTING;
3115 if (process_parents(revs, c, NULL, NULL) < 0)
3116 return;
3118 if (c->object.flags & UNINTERESTING)
3119 mark_parents_uninteresting(c);
3121 for (p = c->parents; p; p = p->next)
3122 test_flag_and_insert(&info->explore_queue, p->item, TOPO_WALK_EXPLORED);
3125 static void explore_to_depth(struct rev_info *revs,
3126 uint32_t gen_cutoff)
3128 struct topo_walk_info *info = revs->topo_walk_info;
3129 struct commit *c;
3130 while ((c = prio_queue_peek(&info->explore_queue)) &&
3131 c->generation >= gen_cutoff)
3132 explore_walk_step(revs);
3135 static void indegree_walk_step(struct rev_info *revs)
3137 struct commit_list *p;
3138 struct topo_walk_info *info = revs->topo_walk_info;
3139 struct commit *c = prio_queue_get(&info->indegree_queue);
3141 if (!c)
3142 return;
3144 if (parse_commit_gently(c, 1) < 0)
3145 return;
3147 explore_to_depth(revs, c->generation);
3149 for (p = c->parents; p; p = p->next) {
3150 struct commit *parent = p->item;
3151 int *pi = indegree_slab_at(&info->indegree, parent);
3153 if (*pi)
3154 (*pi)++;
3155 else
3156 *pi = 2;
3158 test_flag_and_insert(&info->indegree_queue, parent, TOPO_WALK_INDEGREE);
3160 if (revs->first_parent_only)
3161 return;
3165 static void compute_indegrees_to_depth(struct rev_info *revs,
3166 uint32_t gen_cutoff)
3168 struct topo_walk_info *info = revs->topo_walk_info;
3169 struct commit *c;
3170 while ((c = prio_queue_peek(&info->indegree_queue)) &&
3171 c->generation >= gen_cutoff)
3172 indegree_walk_step(revs);
3175 static void init_topo_walk(struct rev_info *revs)
3177 struct topo_walk_info *info;
3178 struct commit_list *list;
3179 revs->topo_walk_info = xmalloc(sizeof(struct topo_walk_info));
3180 info = revs->topo_walk_info;
3181 memset(info, 0, sizeof(struct topo_walk_info));
3183 init_indegree_slab(&info->indegree);
3184 memset(&info->explore_queue, 0, sizeof(info->explore_queue));
3185 memset(&info->indegree_queue, 0, sizeof(info->indegree_queue));
3186 memset(&info->topo_queue, 0, sizeof(info->topo_queue));
3188 switch (revs->sort_order) {
3189 default: /* REV_SORT_IN_GRAPH_ORDER */
3190 info->topo_queue.compare = NULL;
3191 break;
3192 case REV_SORT_BY_COMMIT_DATE:
3193 info->topo_queue.compare = compare_commits_by_commit_date;
3194 break;
3195 case REV_SORT_BY_AUTHOR_DATE:
3196 init_author_date_slab(&info->author_date);
3197 info->topo_queue.compare = compare_commits_by_author_date;
3198 info->topo_queue.cb_data = &info->author_date;
3199 break;
3202 info->explore_queue.compare = compare_commits_by_gen_then_commit_date;
3203 info->indegree_queue.compare = compare_commits_by_gen_then_commit_date;
3205 info->min_generation = GENERATION_NUMBER_INFINITY;
3206 for (list = revs->commits; list; list = list->next) {
3207 struct commit *c = list->item;
3209 if (parse_commit_gently(c, 1))
3210 continue;
3212 test_flag_and_insert(&info->explore_queue, c, TOPO_WALK_EXPLORED);
3213 test_flag_and_insert(&info->indegree_queue, c, TOPO_WALK_INDEGREE);
3215 if (c->generation < info->min_generation)
3216 info->min_generation = c->generation;
3218 *(indegree_slab_at(&info->indegree, c)) = 1;
3220 if (revs->sort_order == REV_SORT_BY_AUTHOR_DATE)
3221 record_author_date(&info->author_date, c);
3223 compute_indegrees_to_depth(revs, info->min_generation);
3225 for (list = revs->commits; list; list = list->next) {
3226 struct commit *c = list->item;
3228 if (*(indegree_slab_at(&info->indegree, c)) == 1)
3229 prio_queue_put(&info->topo_queue, c);
3233 * This is unfortunate; the initial tips need to be shown
3234 * in the order given from the revision traversal machinery.
3236 if (revs->sort_order == REV_SORT_IN_GRAPH_ORDER)
3237 prio_queue_reverse(&info->topo_queue);
3240 static struct commit *next_topo_commit(struct rev_info *revs)
3242 struct commit *c;
3243 struct topo_walk_info *info = revs->topo_walk_info;
3245 /* pop next off of topo_queue */
3246 c = prio_queue_get(&info->topo_queue);
3248 if (c)
3249 *(indegree_slab_at(&info->indegree, c)) = 0;
3251 return c;
3254 static void expand_topo_walk(struct rev_info *revs, struct commit *commit)
3256 struct commit_list *p;
3257 struct topo_walk_info *info = revs->topo_walk_info;
3258 if (process_parents(revs, commit, NULL, NULL) < 0) {
3259 if (!revs->ignore_missing_links)
3260 die("Failed to traverse parents of commit %s",
3261 oid_to_hex(&commit->object.oid));
3264 for (p = commit->parents; p; p = p->next) {
3265 struct commit *parent = p->item;
3266 int *pi;
3268 if (parent->object.flags & UNINTERESTING)
3269 continue;
3271 if (parse_commit_gently(parent, 1) < 0)
3272 continue;
3274 if (parent->generation < info->min_generation) {
3275 info->min_generation = parent->generation;
3276 compute_indegrees_to_depth(revs, info->min_generation);
3279 pi = indegree_slab_at(&info->indegree, parent);
3281 (*pi)--;
3282 if (*pi == 1)
3283 prio_queue_put(&info->topo_queue, parent);
3285 if (revs->first_parent_only)
3286 return;
3290 int prepare_revision_walk(struct rev_info *revs)
3292 int i;
3293 struct object_array old_pending;
3294 struct commit_list **next = &revs->commits;
3296 memcpy(&old_pending, &revs->pending, sizeof(old_pending));
3297 revs->pending.nr = 0;
3298 revs->pending.alloc = 0;
3299 revs->pending.objects = NULL;
3300 for (i = 0; i < old_pending.nr; i++) {
3301 struct object_array_entry *e = old_pending.objects + i;
3302 struct commit *commit = handle_commit(revs, e);
3303 if (commit) {
3304 if (!(commit->object.flags & SEEN)) {
3305 commit->object.flags |= SEEN;
3306 next = commit_list_append(commit, next);
3310 object_array_clear(&old_pending);
3312 /* Signal whether we need per-parent treesame decoration */
3313 if (revs->simplify_merges ||
3314 (revs->limited && limiting_can_increase_treesame(revs)))
3315 revs->treesame.name = "treesame";
3317 if (revs->exclude_promisor_objects) {
3318 for_each_packed_object(mark_uninteresting, revs,
3319 FOR_EACH_OBJECT_PROMISOR_ONLY);
3322 if (revs->no_walk != REVISION_WALK_NO_WALK_UNSORTED)
3323 commit_list_sort_by_date(&revs->commits);
3324 if (revs->no_walk)
3325 return 0;
3326 if (revs->limited) {
3327 if (limit_list(revs) < 0)
3328 return -1;
3329 if (revs->topo_order)
3330 sort_in_topological_order(&revs->commits, revs->sort_order);
3331 } else if (revs->topo_order)
3332 init_topo_walk(revs);
3333 if (revs->line_level_traverse)
3334 line_log_filter(revs);
3335 if (revs->simplify_merges)
3336 simplify_merges(revs);
3337 if (revs->children.name)
3338 set_children(revs);
3339 return 0;
3342 static enum rewrite_result rewrite_one_1(struct rev_info *revs,
3343 struct commit **pp,
3344 struct prio_queue *queue)
3346 for (;;) {
3347 struct commit *p = *pp;
3348 if (!revs->limited)
3349 if (process_parents(revs, p, NULL, queue) < 0)
3350 return rewrite_one_error;
3351 if (p->object.flags & UNINTERESTING)
3352 return rewrite_one_ok;
3353 if (!(p->object.flags & TREESAME))
3354 return rewrite_one_ok;
3355 if (!p->parents)
3356 return rewrite_one_noparents;
3357 if ((p = one_relevant_parent(revs, p->parents)) == NULL)
3358 return rewrite_one_ok;
3359 *pp = p;
3363 static void merge_queue_into_list(struct prio_queue *q, struct commit_list **list)
3365 while (q->nr) {
3366 struct commit *item = prio_queue_peek(q);
3367 struct commit_list *p = *list;
3369 if (p && p->item->date >= item->date)
3370 list = &p->next;
3371 else {
3372 p = commit_list_insert(item, list);
3373 list = &p->next; /* skip newly added item */
3374 prio_queue_get(q); /* pop item */
3379 static enum rewrite_result rewrite_one(struct rev_info *revs, struct commit **pp)
3381 struct prio_queue queue = { compare_commits_by_commit_date };
3382 enum rewrite_result ret = rewrite_one_1(revs, pp, &queue);
3383 merge_queue_into_list(&queue, &revs->commits);
3384 clear_prio_queue(&queue);
3385 return ret;
3388 int rewrite_parents(struct rev_info *revs, struct commit *commit,
3389 rewrite_parent_fn_t rewrite_parent)
3391 struct commit_list **pp = &commit->parents;
3392 while (*pp) {
3393 struct commit_list *parent = *pp;
3394 switch (rewrite_parent(revs, &parent->item)) {
3395 case rewrite_one_ok:
3396 break;
3397 case rewrite_one_noparents:
3398 *pp = parent->next;
3399 continue;
3400 case rewrite_one_error:
3401 return -1;
3403 pp = &parent->next;
3405 remove_duplicate_parents(revs, commit);
3406 return 0;
3409 static int commit_rewrite_person(struct strbuf *buf, const char *what, struct string_list *mailmap)
3411 char *person, *endp;
3412 size_t len, namelen, maillen;
3413 const char *name;
3414 const char *mail;
3415 struct ident_split ident;
3417 person = strstr(buf->buf, what);
3418 if (!person)
3419 return 0;
3421 person += strlen(what);
3422 endp = strchr(person, '\n');
3423 if (!endp)
3424 return 0;
3426 len = endp - person;
3428 if (split_ident_line(&ident, person, len))
3429 return 0;
3431 mail = ident.mail_begin;
3432 maillen = ident.mail_end - ident.mail_begin;
3433 name = ident.name_begin;
3434 namelen = ident.name_end - ident.name_begin;
3436 if (map_user(mailmap, &mail, &maillen, &name, &namelen)) {
3437 struct strbuf namemail = STRBUF_INIT;
3439 strbuf_addf(&namemail, "%.*s <%.*s>",
3440 (int)namelen, name, (int)maillen, mail);
3442 strbuf_splice(buf, ident.name_begin - buf->buf,
3443 ident.mail_end - ident.name_begin + 1,
3444 namemail.buf, namemail.len);
3446 strbuf_release(&namemail);
3448 return 1;
3451 return 0;
3454 static int commit_match(struct commit *commit, struct rev_info *opt)
3456 int retval;
3457 const char *encoding;
3458 const char *message;
3459 struct strbuf buf = STRBUF_INIT;
3461 if (!opt->grep_filter.pattern_list && !opt->grep_filter.header_list)
3462 return 1;
3464 /* Prepend "fake" headers as needed */
3465 if (opt->grep_filter.use_reflog_filter) {
3466 strbuf_addstr(&buf, "reflog ");
3467 get_reflog_message(&buf, opt->reflog_info);
3468 strbuf_addch(&buf, '\n');
3472 * We grep in the user's output encoding, under the assumption that it
3473 * is the encoding they are most likely to write their grep pattern
3474 * for. In addition, it means we will match the "notes" encoding below,
3475 * so we will not end up with a buffer that has two different encodings
3476 * in it.
3478 encoding = get_log_output_encoding();
3479 message = logmsg_reencode(commit, NULL, encoding);
3481 /* Copy the commit to temporary if we are using "fake" headers */
3482 if (buf.len)
3483 strbuf_addstr(&buf, message);
3485 if (opt->grep_filter.header_list && opt->mailmap) {
3486 if (!buf.len)
3487 strbuf_addstr(&buf, message);
3489 commit_rewrite_person(&buf, "\nauthor ", opt->mailmap);
3490 commit_rewrite_person(&buf, "\ncommitter ", opt->mailmap);
3493 /* Append "fake" message parts as needed */
3494 if (opt->show_notes) {
3495 if (!buf.len)
3496 strbuf_addstr(&buf, message);
3497 format_display_notes(&commit->object.oid, &buf, encoding, 1);
3501 * Find either in the original commit message, or in the temporary.
3502 * Note that we cast away the constness of "message" here. It is
3503 * const because it may come from the cached commit buffer. That's OK,
3504 * because we know that it is modifiable heap memory, and that while
3505 * grep_buffer may modify it for speed, it will restore any
3506 * changes before returning.
3508 if (buf.len)
3509 retval = grep_buffer(&opt->grep_filter, buf.buf, buf.len);
3510 else
3511 retval = grep_buffer(&opt->grep_filter,
3512 (char *)message, strlen(message));
3513 strbuf_release(&buf);
3514 unuse_commit_buffer(commit, message);
3515 return opt->invert_grep ? !retval : retval;
3518 static inline int want_ancestry(const struct rev_info *revs)
3520 return (revs->rewrite_parents || revs->children.name);
3524 * Return a timestamp to be used for --since/--until comparisons for this
3525 * commit, based on the revision options.
3527 static timestamp_t comparison_date(const struct rev_info *revs,
3528 struct commit *commit)
3530 return revs->reflog_info ?
3531 get_reflog_timestamp(revs->reflog_info) :
3532 commit->date;
3535 enum commit_action get_commit_action(struct rev_info *revs, struct commit *commit)
3537 if (commit->object.flags & SHOWN)
3538 return commit_ignore;
3539 if (revs->unpacked && has_object_pack(&commit->object.oid))
3540 return commit_ignore;
3541 if (commit->object.flags & UNINTERESTING)
3542 return commit_ignore;
3543 if (revs->min_age != -1 &&
3544 comparison_date(revs, commit) > revs->min_age)
3545 return commit_ignore;
3546 if (revs->min_parents || (revs->max_parents >= 0)) {
3547 int n = commit_list_count(commit->parents);
3548 if ((n < revs->min_parents) ||
3549 ((revs->max_parents >= 0) && (n > revs->max_parents)))
3550 return commit_ignore;
3552 if (!commit_match(commit, revs))
3553 return commit_ignore;
3554 if (revs->prune && revs->dense) {
3555 /* Commit without changes? */
3556 if (commit->object.flags & TREESAME) {
3557 int n;
3558 struct commit_list *p;
3559 /* drop merges unless we want parenthood */
3560 if (!want_ancestry(revs))
3561 return commit_ignore;
3563 * If we want ancestry, then need to keep any merges
3564 * between relevant commits to tie together topology.
3565 * For consistency with TREESAME and simplification
3566 * use "relevant" here rather than just INTERESTING,
3567 * to treat bottom commit(s) as part of the topology.
3569 for (n = 0, p = commit->parents; p; p = p->next)
3570 if (relevant_commit(p->item))
3571 if (++n >= 2)
3572 return commit_show;
3573 return commit_ignore;
3576 return commit_show;
3579 define_commit_slab(saved_parents, struct commit_list *);
3581 #define EMPTY_PARENT_LIST ((struct commit_list *)-1)
3584 * You may only call save_parents() once per commit (this is checked
3585 * for non-root commits).
3587 static void save_parents(struct rev_info *revs, struct commit *commit)
3589 struct commit_list **pp;
3591 if (!revs->saved_parents_slab) {
3592 revs->saved_parents_slab = xmalloc(sizeof(struct saved_parents));
3593 init_saved_parents(revs->saved_parents_slab);
3596 pp = saved_parents_at(revs->saved_parents_slab, commit);
3599 * When walking with reflogs, we may visit the same commit
3600 * several times: once for each appearance in the reflog.
3602 * In this case, save_parents() will be called multiple times.
3603 * We want to keep only the first set of parents. We need to
3604 * store a sentinel value for an empty (i.e., NULL) parent
3605 * list to distinguish it from a not-yet-saved list, however.
3607 if (*pp)
3608 return;
3609 if (commit->parents)
3610 *pp = copy_commit_list(commit->parents);
3611 else
3612 *pp = EMPTY_PARENT_LIST;
3615 static void free_saved_parents(struct rev_info *revs)
3617 if (revs->saved_parents_slab)
3618 clear_saved_parents(revs->saved_parents_slab);
3621 struct commit_list *get_saved_parents(struct rev_info *revs, const struct commit *commit)
3623 struct commit_list *parents;
3625 if (!revs->saved_parents_slab)
3626 return commit->parents;
3628 parents = *saved_parents_at(revs->saved_parents_slab, commit);
3629 if (parents == EMPTY_PARENT_LIST)
3630 return NULL;
3631 return parents;
3634 enum commit_action simplify_commit(struct rev_info *revs, struct commit *commit)
3636 enum commit_action action = get_commit_action(revs, commit);
3638 if (action == commit_show &&
3639 revs->prune && revs->dense && want_ancestry(revs)) {
3641 * --full-diff on simplified parents is no good: it
3642 * will show spurious changes from the commits that
3643 * were elided. So we save the parents on the side
3644 * when --full-diff is in effect.
3646 if (revs->full_diff)
3647 save_parents(revs, commit);
3648 if (rewrite_parents(revs, commit, rewrite_one) < 0)
3649 return commit_error;
3651 return action;
3654 static void track_linear(struct rev_info *revs, struct commit *commit)
3656 if (revs->track_first_time) {
3657 revs->linear = 1;
3658 revs->track_first_time = 0;
3659 } else {
3660 struct commit_list *p;
3661 for (p = revs->previous_parents; p; p = p->next)
3662 if (p->item == NULL || /* first commit */
3663 oideq(&p->item->object.oid, &commit->object.oid))
3664 break;
3665 revs->linear = p != NULL;
3667 if (revs->reverse) {
3668 if (revs->linear)
3669 commit->object.flags |= TRACK_LINEAR;
3671 free_commit_list(revs->previous_parents);
3672 revs->previous_parents = copy_commit_list(commit->parents);
3675 static struct commit *get_revision_1(struct rev_info *revs)
3677 while (1) {
3678 struct commit *commit;
3680 if (revs->reflog_info)
3681 commit = next_reflog_entry(revs->reflog_info);
3682 else if (revs->topo_walk_info)
3683 commit = next_topo_commit(revs);
3684 else
3685 commit = pop_commit(&revs->commits);
3687 if (!commit)
3688 return NULL;
3690 if (revs->reflog_info)
3691 commit->object.flags &= ~(ADDED | SEEN | SHOWN);
3694 * If we haven't done the list limiting, we need to look at
3695 * the parents here. We also need to do the date-based limiting
3696 * that we'd otherwise have done in limit_list().
3698 if (!revs->limited) {
3699 if (revs->max_age != -1 &&
3700 comparison_date(revs, commit) < revs->max_age)
3701 continue;
3703 if (revs->reflog_info)
3704 try_to_simplify_commit(revs, commit);
3705 else if (revs->topo_walk_info)
3706 expand_topo_walk(revs, commit);
3707 else if (process_parents(revs, commit, &revs->commits, NULL) < 0) {
3708 if (!revs->ignore_missing_links)
3709 die("Failed to traverse parents of commit %s",
3710 oid_to_hex(&commit->object.oid));
3714 switch (simplify_commit(revs, commit)) {
3715 case commit_ignore:
3716 continue;
3717 case commit_error:
3718 die("Failed to simplify parents of commit %s",
3719 oid_to_hex(&commit->object.oid));
3720 default:
3721 if (revs->track_linear)
3722 track_linear(revs, commit);
3723 return commit;
3729 * Return true for entries that have not yet been shown. (This is an
3730 * object_array_each_func_t.)
3732 static int entry_unshown(struct object_array_entry *entry, void *cb_data_unused)
3734 return !(entry->item->flags & SHOWN);
3738 * If array is on the verge of a realloc, garbage-collect any entries
3739 * that have already been shown to try to free up some space.
3741 static void gc_boundary(struct object_array *array)
3743 if (array->nr == array->alloc)
3744 object_array_filter(array, entry_unshown, NULL);
3747 static void create_boundary_commit_list(struct rev_info *revs)
3749 unsigned i;
3750 struct commit *c;
3751 struct object_array *array = &revs->boundary_commits;
3752 struct object_array_entry *objects = array->objects;
3755 * If revs->commits is non-NULL at this point, an error occurred in
3756 * get_revision_1(). Ignore the error and continue printing the
3757 * boundary commits anyway. (This is what the code has always
3758 * done.)
3760 if (revs->commits) {
3761 free_commit_list(revs->commits);
3762 revs->commits = NULL;
3766 * Put all of the actual boundary commits from revs->boundary_commits
3767 * into revs->commits
3769 for (i = 0; i < array->nr; i++) {
3770 c = (struct commit *)(objects[i].item);
3771 if (!c)
3772 continue;
3773 if (!(c->object.flags & CHILD_SHOWN))
3774 continue;
3775 if (c->object.flags & (SHOWN | BOUNDARY))
3776 continue;
3777 c->object.flags |= BOUNDARY;
3778 commit_list_insert(c, &revs->commits);
3782 * If revs->topo_order is set, sort the boundary commits
3783 * in topological order
3785 sort_in_topological_order(&revs->commits, revs->sort_order);
3788 static struct commit *get_revision_internal(struct rev_info *revs)
3790 struct commit *c = NULL;
3791 struct commit_list *l;
3793 if (revs->boundary == 2) {
3795 * All of the normal commits have already been returned,
3796 * and we are now returning boundary commits.
3797 * create_boundary_commit_list() has populated
3798 * revs->commits with the remaining commits to return.
3800 c = pop_commit(&revs->commits);
3801 if (c)
3802 c->object.flags |= SHOWN;
3803 return c;
3807 * If our max_count counter has reached zero, then we are done. We
3808 * don't simply return NULL because we still might need to show
3809 * boundary commits. But we want to avoid calling get_revision_1, which
3810 * might do a considerable amount of work finding the next commit only
3811 * for us to throw it away.
3813 * If it is non-zero, then either we don't have a max_count at all
3814 * (-1), or it is still counting, in which case we decrement.
3816 if (revs->max_count) {
3817 c = get_revision_1(revs);
3818 if (c) {
3819 while (revs->skip_count > 0) {
3820 revs->skip_count--;
3821 c = get_revision_1(revs);
3822 if (!c)
3823 break;
3827 if (revs->max_count > 0)
3828 revs->max_count--;
3831 if (c)
3832 c->object.flags |= SHOWN;
3834 if (!revs->boundary)
3835 return c;
3837 if (!c) {
3839 * get_revision_1() runs out the commits, and
3840 * we are done computing the boundaries.
3841 * switch to boundary commits output mode.
3843 revs->boundary = 2;
3846 * Update revs->commits to contain the list of
3847 * boundary commits.
3849 create_boundary_commit_list(revs);
3851 return get_revision_internal(revs);
3855 * boundary commits are the commits that are parents of the
3856 * ones we got from get_revision_1() but they themselves are
3857 * not returned from get_revision_1(). Before returning
3858 * 'c', we need to mark its parents that they could be boundaries.
3861 for (l = c->parents; l; l = l->next) {
3862 struct object *p;
3863 p = &(l->item->object);
3864 if (p->flags & (CHILD_SHOWN | SHOWN))
3865 continue;
3866 p->flags |= CHILD_SHOWN;
3867 gc_boundary(&revs->boundary_commits);
3868 add_object_array(p, NULL, &revs->boundary_commits);
3871 return c;
3874 struct commit *get_revision(struct rev_info *revs)
3876 struct commit *c;
3877 struct commit_list *reversed;
3879 if (revs->reverse) {
3880 reversed = NULL;
3881 while ((c = get_revision_internal(revs)))
3882 commit_list_insert(c, &reversed);
3883 revs->commits = reversed;
3884 revs->reverse = 0;
3885 revs->reverse_output_stage = 1;
3888 if (revs->reverse_output_stage) {
3889 c = pop_commit(&revs->commits);
3890 if (revs->track_linear)
3891 revs->linear = !!(c && c->object.flags & TRACK_LINEAR);
3892 return c;
3895 c = get_revision_internal(revs);
3896 if (c && revs->graph)
3897 graph_update(revs->graph, c);
3898 if (!c) {
3899 free_saved_parents(revs);
3900 if (revs->previous_parents) {
3901 free_commit_list(revs->previous_parents);
3902 revs->previous_parents = NULL;
3905 return c;
3908 char *get_revision_mark(const struct rev_info *revs, const struct commit *commit)
3910 if (commit->object.flags & BOUNDARY)
3911 return "-";
3912 else if (commit->object.flags & UNINTERESTING)
3913 return "^";
3914 else if (commit->object.flags & PATCHSAME)
3915 return "=";
3916 else if (!revs || revs->left_right) {
3917 if (commit->object.flags & SYMMETRIC_LEFT)
3918 return "<";
3919 else
3920 return ">";
3921 } else if (revs->graph)
3922 return "*";
3923 else if (revs->cherry_mark)
3924 return "+";
3925 return "";
3928 void put_revision_mark(const struct rev_info *revs, const struct commit *commit)
3930 char *mark = get_revision_mark(revs, commit);
3931 if (!strlen(mark))
3932 return;
3933 fputs(mark, stdout);
3934 putchar(' ');