Merge branch 'ab/fewer-the-index-macros'
[alt-git.git] / revision.c
blob439e34a7c51803335c4f5677f67058b633c46339
1 #include "cache.h"
2 #include "config.h"
3 #include "object-store.h"
4 #include "tag.h"
5 #include "blob.h"
6 #include "tree.h"
7 #include "commit.h"
8 #include "diff.h"
9 #include "diff-merges.h"
10 #include "refs.h"
11 #include "revision.h"
12 #include "repository.h"
13 #include "graph.h"
14 #include "grep.h"
15 #include "reflog-walk.h"
16 #include "patch-ids.h"
17 #include "decorate.h"
18 #include "log-tree.h"
19 #include "string-list.h"
20 #include "line-log.h"
21 #include "mailmap.h"
22 #include "commit-slab.h"
23 #include "dir.h"
24 #include "cache-tree.h"
25 #include "bisect.h"
26 #include "packfile.h"
27 #include "worktree.h"
28 #include "strvec.h"
29 #include "commit-reach.h"
30 #include "commit-graph.h"
31 #include "prio-queue.h"
32 #include "hashmap.h"
33 #include "utf8.h"
34 #include "bloom.h"
35 #include "json-writer.h"
36 #include "list-objects-filter-options.h"
37 #include "resolve-undo.h"
39 volatile show_early_output_fn_t show_early_output;
41 static const char *term_bad;
42 static const char *term_good;
44 implement_shared_commit_slab(revision_sources, char *);
46 static inline int want_ancestry(const struct rev_info *revs);
48 void show_object_with_name(FILE *out, struct object *obj, const char *name)
50 fprintf(out, "%s ", oid_to_hex(&obj->oid));
51 for (const char *p = name; *p && *p != '\n'; p++)
52 fputc(*p, out);
53 fputc('\n', out);
56 static void mark_blob_uninteresting(struct blob *blob)
58 if (!blob)
59 return;
60 if (blob->object.flags & UNINTERESTING)
61 return;
62 blob->object.flags |= UNINTERESTING;
65 static void mark_tree_contents_uninteresting(struct repository *r,
66 struct tree *tree)
68 struct tree_desc desc;
69 struct name_entry entry;
71 if (parse_tree_gently(tree, 1) < 0)
72 return;
74 init_tree_desc(&desc, tree->buffer, tree->size);
75 while (tree_entry(&desc, &entry)) {
76 switch (object_type(entry.mode)) {
77 case OBJ_TREE:
78 mark_tree_uninteresting(r, lookup_tree(r, &entry.oid));
79 break;
80 case OBJ_BLOB:
81 mark_blob_uninteresting(lookup_blob(r, &entry.oid));
82 break;
83 default:
84 /* Subproject commit - not in this repository */
85 break;
90 * We don't care about the tree any more
91 * after it has been marked uninteresting.
93 free_tree_buffer(tree);
96 void mark_tree_uninteresting(struct repository *r, struct tree *tree)
98 struct object *obj;
100 if (!tree)
101 return;
103 obj = &tree->object;
104 if (obj->flags & UNINTERESTING)
105 return;
106 obj->flags |= UNINTERESTING;
107 mark_tree_contents_uninteresting(r, tree);
110 struct path_and_oids_entry {
111 struct hashmap_entry ent;
112 char *path;
113 struct oidset trees;
116 static int path_and_oids_cmp(const void *hashmap_cmp_fn_data UNUSED,
117 const struct hashmap_entry *eptr,
118 const struct hashmap_entry *entry_or_key,
119 const void *keydata UNUSED)
121 const struct path_and_oids_entry *e1, *e2;
123 e1 = container_of(eptr, const struct path_and_oids_entry, ent);
124 e2 = container_of(entry_or_key, const struct path_and_oids_entry, ent);
126 return strcmp(e1->path, e2->path);
129 static void paths_and_oids_clear(struct hashmap *map)
131 struct hashmap_iter iter;
132 struct path_and_oids_entry *entry;
134 hashmap_for_each_entry(map, &iter, entry, ent /* member name */) {
135 oidset_clear(&entry->trees);
136 free(entry->path);
139 hashmap_clear_and_free(map, struct path_and_oids_entry, ent);
142 static void paths_and_oids_insert(struct hashmap *map,
143 const char *path,
144 const struct object_id *oid)
146 int hash = strhash(path);
147 struct path_and_oids_entry key;
148 struct path_and_oids_entry *entry;
150 hashmap_entry_init(&key.ent, hash);
152 /* use a shallow copy for the lookup */
153 key.path = (char *)path;
154 oidset_init(&key.trees, 0);
156 entry = hashmap_get_entry(map, &key, ent, NULL);
157 if (!entry) {
158 CALLOC_ARRAY(entry, 1);
159 hashmap_entry_init(&entry->ent, hash);
160 entry->path = xstrdup(key.path);
161 oidset_init(&entry->trees, 16);
162 hashmap_put(map, &entry->ent);
165 oidset_insert(&entry->trees, oid);
168 static void add_children_by_path(struct repository *r,
169 struct tree *tree,
170 struct hashmap *map)
172 struct tree_desc desc;
173 struct name_entry entry;
175 if (!tree)
176 return;
178 if (parse_tree_gently(tree, 1) < 0)
179 return;
181 init_tree_desc(&desc, tree->buffer, tree->size);
182 while (tree_entry(&desc, &entry)) {
183 switch (object_type(entry.mode)) {
184 case OBJ_TREE:
185 paths_and_oids_insert(map, entry.path, &entry.oid);
187 if (tree->object.flags & UNINTERESTING) {
188 struct tree *child = lookup_tree(r, &entry.oid);
189 if (child)
190 child->object.flags |= UNINTERESTING;
192 break;
193 case OBJ_BLOB:
194 if (tree->object.flags & UNINTERESTING) {
195 struct blob *child = lookup_blob(r, &entry.oid);
196 if (child)
197 child->object.flags |= UNINTERESTING;
199 break;
200 default:
201 /* Subproject commit - not in this repository */
202 break;
206 free_tree_buffer(tree);
209 void mark_trees_uninteresting_sparse(struct repository *r,
210 struct oidset *trees)
212 unsigned has_interesting = 0, has_uninteresting = 0;
213 struct hashmap map = HASHMAP_INIT(path_and_oids_cmp, NULL);
214 struct hashmap_iter map_iter;
215 struct path_and_oids_entry *entry;
216 struct object_id *oid;
217 struct oidset_iter iter;
219 oidset_iter_init(trees, &iter);
220 while ((!has_interesting || !has_uninteresting) &&
221 (oid = oidset_iter_next(&iter))) {
222 struct tree *tree = lookup_tree(r, oid);
224 if (!tree)
225 continue;
227 if (tree->object.flags & UNINTERESTING)
228 has_uninteresting = 1;
229 else
230 has_interesting = 1;
233 /* Do not walk unless we have both types of trees. */
234 if (!has_uninteresting || !has_interesting)
235 return;
237 oidset_iter_init(trees, &iter);
238 while ((oid = oidset_iter_next(&iter))) {
239 struct tree *tree = lookup_tree(r, oid);
240 add_children_by_path(r, tree, &map);
243 hashmap_for_each_entry(&map, &map_iter, entry, ent /* member name */)
244 mark_trees_uninteresting_sparse(r, &entry->trees);
246 paths_and_oids_clear(&map);
249 struct commit_stack {
250 struct commit **items;
251 size_t nr, alloc;
253 #define COMMIT_STACK_INIT { 0 }
255 static void commit_stack_push(struct commit_stack *stack, struct commit *commit)
257 ALLOC_GROW(stack->items, stack->nr + 1, stack->alloc);
258 stack->items[stack->nr++] = commit;
261 static struct commit *commit_stack_pop(struct commit_stack *stack)
263 return stack->nr ? stack->items[--stack->nr] : NULL;
266 static void commit_stack_clear(struct commit_stack *stack)
268 FREE_AND_NULL(stack->items);
269 stack->nr = stack->alloc = 0;
272 static void mark_one_parent_uninteresting(struct rev_info *revs, struct commit *commit,
273 struct commit_stack *pending)
275 struct commit_list *l;
277 if (commit->object.flags & UNINTERESTING)
278 return;
279 commit->object.flags |= UNINTERESTING;
282 * Normally we haven't parsed the parent
283 * yet, so we won't have a parent of a parent
284 * here. However, it may turn out that we've
285 * reached this commit some other way (where it
286 * wasn't uninteresting), in which case we need
287 * to mark its parents recursively too..
289 for (l = commit->parents; l; l = l->next) {
290 commit_stack_push(pending, l->item);
291 if (revs && revs->exclude_first_parent_only)
292 break;
296 void mark_parents_uninteresting(struct rev_info *revs, struct commit *commit)
298 struct commit_stack pending = COMMIT_STACK_INIT;
299 struct commit_list *l;
301 for (l = commit->parents; l; l = l->next) {
302 mark_one_parent_uninteresting(revs, l->item, &pending);
303 if (revs && revs->exclude_first_parent_only)
304 break;
307 while (pending.nr > 0)
308 mark_one_parent_uninteresting(revs, commit_stack_pop(&pending),
309 &pending);
311 commit_stack_clear(&pending);
314 static void add_pending_object_with_path(struct rev_info *revs,
315 struct object *obj,
316 const char *name, unsigned mode,
317 const char *path)
319 struct interpret_branch_name_options options = { 0 };
320 if (!obj)
321 return;
322 if (revs->no_walk && (obj->flags & UNINTERESTING))
323 revs->no_walk = 0;
324 if (revs->reflog_info && obj->type == OBJ_COMMIT) {
325 struct strbuf buf = STRBUF_INIT;
326 size_t namelen = strlen(name);
327 int len = interpret_branch_name(name, namelen, &buf, &options);
329 if (0 < len && len < namelen && buf.len)
330 strbuf_addstr(&buf, name + len);
331 add_reflog_for_walk(revs->reflog_info,
332 (struct commit *)obj,
333 buf.buf[0] ? buf.buf: name);
334 strbuf_release(&buf);
335 return; /* do not add the commit itself */
337 add_object_array_with_path(obj, name, &revs->pending, mode, path);
340 static void add_pending_object_with_mode(struct rev_info *revs,
341 struct object *obj,
342 const char *name, unsigned mode)
344 add_pending_object_with_path(revs, obj, name, mode, NULL);
347 void add_pending_object(struct rev_info *revs,
348 struct object *obj, const char *name)
350 add_pending_object_with_mode(revs, obj, name, S_IFINVALID);
353 void add_head_to_pending(struct rev_info *revs)
355 struct object_id oid;
356 struct object *obj;
357 if (get_oid("HEAD", &oid))
358 return;
359 obj = parse_object(revs->repo, &oid);
360 if (!obj)
361 return;
362 add_pending_object(revs, obj, "HEAD");
365 static struct object *get_reference(struct rev_info *revs, const char *name,
366 const struct object_id *oid,
367 unsigned int flags)
369 struct object *object;
371 object = parse_object_with_flags(revs->repo, oid,
372 revs->verify_objects ? 0 :
373 PARSE_OBJECT_SKIP_HASH_CHECK);
375 if (!object) {
376 if (revs->ignore_missing)
377 return object;
378 if (revs->exclude_promisor_objects && is_promisor_object(oid))
379 return NULL;
380 die("bad object %s", name);
382 object->flags |= flags;
383 return object;
386 void add_pending_oid(struct rev_info *revs, const char *name,
387 const struct object_id *oid, unsigned int flags)
389 struct object *object = get_reference(revs, name, oid, flags);
390 add_pending_object(revs, object, name);
393 static struct commit *handle_commit(struct rev_info *revs,
394 struct object_array_entry *entry)
396 struct object *object = entry->item;
397 const char *name = entry->name;
398 const char *path = entry->path;
399 unsigned int mode = entry->mode;
400 unsigned long flags = object->flags;
403 * Tag object? Look what it points to..
405 while (object->type == OBJ_TAG) {
406 struct tag *tag = (struct tag *) object;
407 if (revs->tag_objects && !(flags & UNINTERESTING))
408 add_pending_object(revs, object, tag->tag);
409 object = parse_object(revs->repo, get_tagged_oid(tag));
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 (repo_parse_commit(revs->repo, commit) < 0)
436 die("unable to parse commit %s", name);
437 if (flags & UNINTERESTING) {
438 mark_parents_uninteresting(revs, 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 #define REV_TREE_SAME 0
597 #define REV_TREE_NEW 1 /* Only new files */
598 #define REV_TREE_OLD 2 /* Only files removed */
599 #define REV_TREE_DIFFERENT 3 /* Mixed changes */
600 static int tree_difference = REV_TREE_SAME;
602 static void file_add_remove(struct diff_options *options,
603 int addremove, unsigned mode,
604 const struct object_id *oid,
605 int oid_valid,
606 const char *fullpath, unsigned dirty_submodule)
608 int diff = addremove == '+' ? REV_TREE_NEW : REV_TREE_OLD;
609 struct rev_info *revs = options->change_fn_data;
611 tree_difference |= diff;
612 if (!revs->remove_empty_trees || tree_difference != REV_TREE_NEW)
613 options->flags.has_changes = 1;
616 static void file_change(struct diff_options *options,
617 unsigned old_mode, unsigned new_mode,
618 const struct object_id *old_oid,
619 const struct object_id *new_oid,
620 int old_oid_valid, int new_oid_valid,
621 const char *fullpath,
622 unsigned old_dirty_submodule, unsigned new_dirty_submodule)
624 tree_difference = REV_TREE_DIFFERENT;
625 options->flags.has_changes = 1;
628 static int bloom_filter_atexit_registered;
629 static unsigned int count_bloom_filter_maybe;
630 static unsigned int count_bloom_filter_definitely_not;
631 static unsigned int count_bloom_filter_false_positive;
632 static unsigned int count_bloom_filter_not_present;
634 static void trace2_bloom_filter_statistics_atexit(void)
636 struct json_writer jw = JSON_WRITER_INIT;
638 jw_object_begin(&jw, 0);
639 jw_object_intmax(&jw, "filter_not_present", count_bloom_filter_not_present);
640 jw_object_intmax(&jw, "maybe", count_bloom_filter_maybe);
641 jw_object_intmax(&jw, "definitely_not", count_bloom_filter_definitely_not);
642 jw_object_intmax(&jw, "false_positive", count_bloom_filter_false_positive);
643 jw_end(&jw);
645 trace2_data_json("bloom", the_repository, "statistics", &jw);
647 jw_release(&jw);
650 static int forbid_bloom_filters(struct pathspec *spec)
652 if (spec->has_wildcard)
653 return 1;
654 if (spec->nr > 1)
655 return 1;
656 if (spec->magic & ~PATHSPEC_LITERAL)
657 return 1;
658 if (spec->nr && (spec->items[0].magic & ~PATHSPEC_LITERAL))
659 return 1;
661 return 0;
664 static void prepare_to_use_bloom_filter(struct rev_info *revs)
666 struct pathspec_item *pi;
667 char *path_alloc = NULL;
668 const char *path, *p;
669 size_t len;
670 int path_component_nr = 1;
672 if (!revs->commits)
673 return;
675 if (forbid_bloom_filters(&revs->prune_data))
676 return;
678 repo_parse_commit(revs->repo, revs->commits->item);
680 revs->bloom_filter_settings = get_bloom_filter_settings(revs->repo);
681 if (!revs->bloom_filter_settings)
682 return;
684 if (!revs->pruning.pathspec.nr)
685 return;
687 pi = &revs->pruning.pathspec.items[0];
689 /* remove single trailing slash from path, if needed */
690 if (pi->len > 0 && pi->match[pi->len - 1] == '/') {
691 path_alloc = xmemdupz(pi->match, pi->len - 1);
692 path = path_alloc;
693 } else
694 path = pi->match;
696 len = strlen(path);
697 if (!len) {
698 revs->bloom_filter_settings = NULL;
699 free(path_alloc);
700 return;
703 p = path;
704 while (*p) {
706 * At this point, the path is normalized to use Unix-style
707 * path separators. This is required due to how the
708 * changed-path Bloom filters store the paths.
710 if (*p == '/')
711 path_component_nr++;
712 p++;
715 revs->bloom_keys_nr = path_component_nr;
716 ALLOC_ARRAY(revs->bloom_keys, revs->bloom_keys_nr);
718 fill_bloom_key(path, len, &revs->bloom_keys[0],
719 revs->bloom_filter_settings);
720 path_component_nr = 1;
722 p = path + len - 1;
723 while (p > path) {
724 if (*p == '/')
725 fill_bloom_key(path, p - path,
726 &revs->bloom_keys[path_component_nr++],
727 revs->bloom_filter_settings);
728 p--;
731 if (trace2_is_enabled() && !bloom_filter_atexit_registered) {
732 atexit(trace2_bloom_filter_statistics_atexit);
733 bloom_filter_atexit_registered = 1;
736 free(path_alloc);
739 static int check_maybe_different_in_bloom_filter(struct rev_info *revs,
740 struct commit *commit)
742 struct bloom_filter *filter;
743 int result = 1, j;
745 if (!revs->repo->objects->commit_graph)
746 return -1;
748 if (commit_graph_generation(commit) == GENERATION_NUMBER_INFINITY)
749 return -1;
751 filter = get_bloom_filter(revs->repo, commit);
753 if (!filter) {
754 count_bloom_filter_not_present++;
755 return -1;
758 for (j = 0; result && j < revs->bloom_keys_nr; j++) {
759 result = bloom_filter_contains(filter,
760 &revs->bloom_keys[j],
761 revs->bloom_filter_settings);
764 if (result)
765 count_bloom_filter_maybe++;
766 else
767 count_bloom_filter_definitely_not++;
769 return result;
772 static int rev_compare_tree(struct rev_info *revs,
773 struct commit *parent, struct commit *commit, int nth_parent)
775 struct tree *t1 = get_commit_tree(parent);
776 struct tree *t2 = get_commit_tree(commit);
777 int bloom_ret = 1;
779 if (!t1)
780 return REV_TREE_NEW;
781 if (!t2)
782 return REV_TREE_OLD;
784 if (revs->simplify_by_decoration) {
786 * If we are simplifying by decoration, then the commit
787 * is worth showing if it has a tag pointing at it.
789 if (get_name_decoration(&commit->object))
790 return REV_TREE_DIFFERENT;
792 * A commit that is not pointed by a tag is uninteresting
793 * if we are not limited by path. This means that you will
794 * see the usual "commits that touch the paths" plus any
795 * tagged commit by specifying both --simplify-by-decoration
796 * and pathspec.
798 if (!revs->prune_data.nr)
799 return REV_TREE_SAME;
802 if (revs->bloom_keys_nr && !nth_parent) {
803 bloom_ret = check_maybe_different_in_bloom_filter(revs, commit);
805 if (bloom_ret == 0)
806 return REV_TREE_SAME;
809 tree_difference = REV_TREE_SAME;
810 revs->pruning.flags.has_changes = 0;
811 diff_tree_oid(&t1->object.oid, &t2->object.oid, "", &revs->pruning);
813 if (!nth_parent)
814 if (bloom_ret == 1 && tree_difference == REV_TREE_SAME)
815 count_bloom_filter_false_positive++;
817 return tree_difference;
820 static int rev_same_tree_as_empty(struct rev_info *revs, struct commit *commit)
822 struct tree *t1 = get_commit_tree(commit);
824 if (!t1)
825 return 0;
827 tree_difference = REV_TREE_SAME;
828 revs->pruning.flags.has_changes = 0;
829 diff_tree_oid(NULL, &t1->object.oid, "", &revs->pruning);
831 return tree_difference == REV_TREE_SAME;
834 struct treesame_state {
835 unsigned int nparents;
836 unsigned char treesame[FLEX_ARRAY];
839 static struct treesame_state *initialise_treesame(struct rev_info *revs, struct commit *commit)
841 unsigned n = commit_list_count(commit->parents);
842 struct treesame_state *st = xcalloc(1, st_add(sizeof(*st), n));
843 st->nparents = n;
844 add_decoration(&revs->treesame, &commit->object, st);
845 return st;
849 * Must be called immediately after removing the nth_parent from a commit's
850 * parent list, if we are maintaining the per-parent treesame[] decoration.
851 * This does not recalculate the master TREESAME flag - update_treesame()
852 * should be called to update it after a sequence of treesame[] modifications
853 * that may have affected it.
855 static int compact_treesame(struct rev_info *revs, struct commit *commit, unsigned nth_parent)
857 struct treesame_state *st;
858 int old_same;
860 if (!commit->parents) {
862 * Have just removed the only parent from a non-merge.
863 * Different handling, as we lack decoration.
865 if (nth_parent != 0)
866 die("compact_treesame %u", nth_parent);
867 old_same = !!(commit->object.flags & TREESAME);
868 if (rev_same_tree_as_empty(revs, commit))
869 commit->object.flags |= TREESAME;
870 else
871 commit->object.flags &= ~TREESAME;
872 return old_same;
875 st = lookup_decoration(&revs->treesame, &commit->object);
876 if (!st || nth_parent >= st->nparents)
877 die("compact_treesame %u", nth_parent);
879 old_same = st->treesame[nth_parent];
880 memmove(st->treesame + nth_parent,
881 st->treesame + nth_parent + 1,
882 st->nparents - nth_parent - 1);
885 * If we've just become a non-merge commit, update TREESAME
886 * immediately, and remove the no-longer-needed decoration.
887 * If still a merge, defer update until update_treesame().
889 if (--st->nparents == 1) {
890 if (commit->parents->next)
891 die("compact_treesame parents mismatch");
892 if (st->treesame[0] && revs->dense)
893 commit->object.flags |= TREESAME;
894 else
895 commit->object.flags &= ~TREESAME;
896 free(add_decoration(&revs->treesame, &commit->object, NULL));
899 return old_same;
902 static unsigned update_treesame(struct rev_info *revs, struct commit *commit)
904 if (commit->parents && commit->parents->next) {
905 unsigned n;
906 struct treesame_state *st;
907 struct commit_list *p;
908 unsigned relevant_parents;
909 unsigned relevant_change, irrelevant_change;
911 st = lookup_decoration(&revs->treesame, &commit->object);
912 if (!st)
913 die("update_treesame %s", oid_to_hex(&commit->object.oid));
914 relevant_parents = 0;
915 relevant_change = irrelevant_change = 0;
916 for (p = commit->parents, n = 0; p; n++, p = p->next) {
917 if (relevant_commit(p->item)) {
918 relevant_change |= !st->treesame[n];
919 relevant_parents++;
920 } else
921 irrelevant_change |= !st->treesame[n];
923 if (relevant_parents ? relevant_change : irrelevant_change)
924 commit->object.flags &= ~TREESAME;
925 else
926 commit->object.flags |= TREESAME;
929 return commit->object.flags & TREESAME;
932 static inline int limiting_can_increase_treesame(const struct rev_info *revs)
935 * TREESAME is irrelevant unless prune && dense;
936 * if simplify_history is set, we can't have a mixture of TREESAME and
937 * !TREESAME INTERESTING parents (and we don't have treesame[]
938 * decoration anyway);
939 * if first_parent_only is set, then the TREESAME flag is locked
940 * against the first parent (and again we lack treesame[] decoration).
942 return revs->prune && revs->dense &&
943 !revs->simplify_history &&
944 !revs->first_parent_only;
947 static void try_to_simplify_commit(struct rev_info *revs, struct commit *commit)
949 struct commit_list **pp, *parent;
950 struct treesame_state *ts = NULL;
951 int relevant_change = 0, irrelevant_change = 0;
952 int relevant_parents, nth_parent;
955 * If we don't do pruning, everything is interesting
957 if (!revs->prune)
958 return;
960 if (!get_commit_tree(commit))
961 return;
963 if (!commit->parents) {
964 if (rev_same_tree_as_empty(revs, commit))
965 commit->object.flags |= TREESAME;
966 return;
970 * Normal non-merge commit? If we don't want to make the
971 * history dense, we consider it always to be a change..
973 if (!revs->dense && !commit->parents->next)
974 return;
976 for (pp = &commit->parents, nth_parent = 0, relevant_parents = 0;
977 (parent = *pp) != NULL;
978 pp = &parent->next, nth_parent++) {
979 struct commit *p = parent->item;
980 if (relevant_commit(p))
981 relevant_parents++;
983 if (nth_parent == 1) {
985 * This our second loop iteration - so we now know
986 * we're dealing with a merge.
988 * Do not compare with later parents when we care only about
989 * the first parent chain, in order to avoid derailing the
990 * traversal to follow a side branch that brought everything
991 * in the path we are limited to by the pathspec.
993 if (revs->first_parent_only)
994 break;
996 * If this will remain a potentially-simplifiable
997 * merge, remember per-parent treesame if needed.
998 * Initialise the array with the comparison from our
999 * first iteration.
1001 if (revs->treesame.name &&
1002 !revs->simplify_history &&
1003 !(commit->object.flags & UNINTERESTING)) {
1004 ts = initialise_treesame(revs, commit);
1005 if (!(irrelevant_change || relevant_change))
1006 ts->treesame[0] = 1;
1009 if (repo_parse_commit(revs->repo, p) < 0)
1010 die("cannot simplify commit %s (because of %s)",
1011 oid_to_hex(&commit->object.oid),
1012 oid_to_hex(&p->object.oid));
1013 switch (rev_compare_tree(revs, p, commit, nth_parent)) {
1014 case REV_TREE_SAME:
1015 if (!revs->simplify_history || !relevant_commit(p)) {
1016 /* Even if a merge with an uninteresting
1017 * side branch brought the entire change
1018 * we are interested in, we do not want
1019 * to lose the other branches of this
1020 * merge, so we just keep going.
1022 if (ts)
1023 ts->treesame[nth_parent] = 1;
1024 continue;
1026 parent->next = NULL;
1027 commit->parents = parent;
1030 * A merge commit is a "diversion" if it is not
1031 * TREESAME to its first parent but is TREESAME
1032 * to a later parent. In the simplified history,
1033 * we "divert" the history walk to the later
1034 * parent. These commits are shown when "show_pulls"
1035 * is enabled, so do not mark the object as
1036 * TREESAME here.
1038 if (!revs->show_pulls || !nth_parent)
1039 commit->object.flags |= TREESAME;
1041 return;
1043 case REV_TREE_NEW:
1044 if (revs->remove_empty_trees &&
1045 rev_same_tree_as_empty(revs, p)) {
1046 /* We are adding all the specified
1047 * paths from this parent, so the
1048 * history beyond this parent is not
1049 * interesting. Remove its parents
1050 * (they are grandparents for us).
1051 * IOW, we pretend this parent is a
1052 * "root" commit.
1054 if (repo_parse_commit(revs->repo, p) < 0)
1055 die("cannot simplify commit %s (invalid %s)",
1056 oid_to_hex(&commit->object.oid),
1057 oid_to_hex(&p->object.oid));
1058 p->parents = NULL;
1060 /* fallthrough */
1061 case REV_TREE_OLD:
1062 case REV_TREE_DIFFERENT:
1063 if (relevant_commit(p))
1064 relevant_change = 1;
1065 else
1066 irrelevant_change = 1;
1068 if (!nth_parent)
1069 commit->object.flags |= PULL_MERGE;
1071 continue;
1073 die("bad tree compare for commit %s", oid_to_hex(&commit->object.oid));
1077 * TREESAME is straightforward for single-parent commits. For merge
1078 * commits, it is most useful to define it so that "irrelevant"
1079 * parents cannot make us !TREESAME - if we have any relevant
1080 * parents, then we only consider TREESAMEness with respect to them,
1081 * allowing irrelevant merges from uninteresting branches to be
1082 * simplified away. Only if we have only irrelevant parents do we
1083 * base TREESAME on them. Note that this logic is replicated in
1084 * update_treesame, which should be kept in sync.
1086 if (relevant_parents ? !relevant_change : !irrelevant_change)
1087 commit->object.flags |= TREESAME;
1090 static int process_parents(struct rev_info *revs, struct commit *commit,
1091 struct commit_list **list, struct prio_queue *queue)
1093 struct commit_list *parent = commit->parents;
1094 unsigned pass_flags;
1096 if (commit->object.flags & ADDED)
1097 return 0;
1098 commit->object.flags |= ADDED;
1100 if (revs->include_check &&
1101 !revs->include_check(commit, revs->include_check_data))
1102 return 0;
1105 * If the commit is uninteresting, don't try to
1106 * prune parents - we want the maximal uninteresting
1107 * set.
1109 * Normally we haven't parsed the parent
1110 * yet, so we won't have a parent of a parent
1111 * here. However, it may turn out that we've
1112 * reached this commit some other way (where it
1113 * wasn't uninteresting), in which case we need
1114 * to mark its parents recursively too..
1116 if (commit->object.flags & UNINTERESTING) {
1117 while (parent) {
1118 struct commit *p = parent->item;
1119 parent = parent->next;
1120 if (p)
1121 p->object.flags |= UNINTERESTING;
1122 if (repo_parse_commit_gently(revs->repo, p, 1) < 0)
1123 continue;
1124 if (p->parents)
1125 mark_parents_uninteresting(revs, p);
1126 if (p->object.flags & SEEN)
1127 continue;
1128 p->object.flags |= (SEEN | NOT_USER_GIVEN);
1129 if (list)
1130 commit_list_insert_by_date(p, list);
1131 if (queue)
1132 prio_queue_put(queue, p);
1133 if (revs->exclude_first_parent_only)
1134 break;
1136 return 0;
1140 * Ok, the commit wasn't uninteresting. Try to
1141 * simplify the commit history and find the parent
1142 * that has no differences in the path set if one exists.
1144 try_to_simplify_commit(revs, commit);
1146 if (revs->no_walk)
1147 return 0;
1149 pass_flags = (commit->object.flags & (SYMMETRIC_LEFT | ANCESTRY_PATH));
1151 for (parent = commit->parents; parent; parent = parent->next) {
1152 struct commit *p = parent->item;
1153 int gently = revs->ignore_missing_links ||
1154 revs->exclude_promisor_objects;
1155 if (repo_parse_commit_gently(revs->repo, p, gently) < 0) {
1156 if (revs->exclude_promisor_objects &&
1157 is_promisor_object(&p->object.oid)) {
1158 if (revs->first_parent_only)
1159 break;
1160 continue;
1162 return -1;
1164 if (revs->sources) {
1165 char **slot = revision_sources_at(revs->sources, p);
1167 if (!*slot)
1168 *slot = *revision_sources_at(revs->sources, commit);
1170 p->object.flags |= pass_flags;
1171 if (!(p->object.flags & SEEN)) {
1172 p->object.flags |= (SEEN | NOT_USER_GIVEN);
1173 if (list)
1174 commit_list_insert_by_date(p, list);
1175 if (queue)
1176 prio_queue_put(queue, p);
1178 if (revs->first_parent_only)
1179 break;
1181 return 0;
1184 static void cherry_pick_list(struct commit_list *list, struct rev_info *revs)
1186 struct commit_list *p;
1187 int left_count = 0, right_count = 0;
1188 int left_first;
1189 struct patch_ids ids;
1190 unsigned cherry_flag;
1192 /* First count the commits on the left and on the right */
1193 for (p = list; p; p = p->next) {
1194 struct commit *commit = p->item;
1195 unsigned flags = commit->object.flags;
1196 if (flags & BOUNDARY)
1198 else if (flags & SYMMETRIC_LEFT)
1199 left_count++;
1200 else
1201 right_count++;
1204 if (!left_count || !right_count)
1205 return;
1207 left_first = left_count < right_count;
1208 init_patch_ids(revs->repo, &ids);
1209 ids.diffopts.pathspec = revs->diffopt.pathspec;
1211 /* Compute patch-ids for one side */
1212 for (p = list; p; p = p->next) {
1213 struct commit *commit = p->item;
1214 unsigned flags = commit->object.flags;
1216 if (flags & BOUNDARY)
1217 continue;
1219 * If we have fewer left, left_first is set and we omit
1220 * commits on the right branch in this loop. If we have
1221 * fewer right, we skip the left ones.
1223 if (left_first != !!(flags & SYMMETRIC_LEFT))
1224 continue;
1225 add_commit_patch_id(commit, &ids);
1228 /* either cherry_mark or cherry_pick are true */
1229 cherry_flag = revs->cherry_mark ? PATCHSAME : SHOWN;
1231 /* Check the other side */
1232 for (p = list; p; p = p->next) {
1233 struct commit *commit = p->item;
1234 struct patch_id *id;
1235 unsigned flags = commit->object.flags;
1237 if (flags & BOUNDARY)
1238 continue;
1240 * If we have fewer left, left_first is set and we omit
1241 * commits on the left branch in this loop.
1243 if (left_first == !!(flags & SYMMETRIC_LEFT))
1244 continue;
1247 * Have we seen the same patch id?
1249 id = patch_id_iter_first(commit, &ids);
1250 if (!id)
1251 continue;
1253 commit->object.flags |= cherry_flag;
1254 do {
1255 id->commit->object.flags |= cherry_flag;
1256 } while ((id = patch_id_iter_next(id, &ids)));
1259 free_patch_ids(&ids);
1262 /* How many extra uninteresting commits we want to see.. */
1263 #define SLOP 5
1265 static int still_interesting(struct commit_list *src, timestamp_t date, int slop,
1266 struct commit **interesting_cache)
1269 * No source list at all? We're definitely done..
1271 if (!src)
1272 return 0;
1275 * Does the destination list contain entries with a date
1276 * before the source list? Definitely _not_ done.
1278 if (date <= src->item->date)
1279 return SLOP;
1282 * Does the source list still have interesting commits in
1283 * it? Definitely not done..
1285 if (!everybody_uninteresting(src, interesting_cache))
1286 return SLOP;
1288 /* Ok, we're closing in.. */
1289 return slop-1;
1293 * "rev-list --ancestry-path=C_0 [--ancestry-path=C_1 ...] A..B"
1294 * computes commits that are ancestors of B but not ancestors of A but
1295 * further limits the result to those that have any of C in their
1296 * ancestry path (i.e. are either ancestors of any of C, descendants
1297 * of any of C, or are any of C). If --ancestry-path is specified with
1298 * no commit, we use all bottom commits for C.
1300 * Before this function is called, ancestors of C will have already
1301 * been marked with ANCESTRY_PATH previously.
1303 * This takes the list of bottom commits and the result of "A..B"
1304 * without --ancestry-path, and limits the latter further to the ones
1305 * that have any of C in their ancestry path. Since the ancestors of C
1306 * have already been marked (a prerequisite of this function), we just
1307 * need to mark the descendants, then exclude any commit that does not
1308 * have any of these marks.
1310 static void limit_to_ancestry(struct commit_list *bottoms, struct commit_list *list)
1312 struct commit_list *p;
1313 struct commit_list *rlist = NULL;
1314 int made_progress;
1317 * Reverse the list so that it will be likely that we would
1318 * process parents before children.
1320 for (p = list; p; p = p->next)
1321 commit_list_insert(p->item, &rlist);
1323 for (p = bottoms; p; p = p->next)
1324 p->item->object.flags |= TMP_MARK;
1327 * Mark the ones that can reach bottom commits in "list",
1328 * in a bottom-up fashion.
1330 do {
1331 made_progress = 0;
1332 for (p = rlist; p; p = p->next) {
1333 struct commit *c = p->item;
1334 struct commit_list *parents;
1335 if (c->object.flags & (TMP_MARK | UNINTERESTING))
1336 continue;
1337 for (parents = c->parents;
1338 parents;
1339 parents = parents->next) {
1340 if (!(parents->item->object.flags & TMP_MARK))
1341 continue;
1342 c->object.flags |= TMP_MARK;
1343 made_progress = 1;
1344 break;
1347 } while (made_progress);
1350 * NEEDSWORK: decide if we want to remove parents that are
1351 * not marked with TMP_MARK from commit->parents for commits
1352 * in the resulting list. We may not want to do that, though.
1356 * The ones that are not marked with either TMP_MARK or
1357 * ANCESTRY_PATH are uninteresting
1359 for (p = list; p; p = p->next) {
1360 struct commit *c = p->item;
1361 if (c->object.flags & (TMP_MARK | ANCESTRY_PATH))
1362 continue;
1363 c->object.flags |= UNINTERESTING;
1366 /* We are done with TMP_MARK and ANCESTRY_PATH */
1367 for (p = list; p; p = p->next)
1368 p->item->object.flags &= ~(TMP_MARK | ANCESTRY_PATH);
1369 for (p = bottoms; p; p = p->next)
1370 p->item->object.flags &= ~(TMP_MARK | ANCESTRY_PATH);
1371 free_commit_list(rlist);
1375 * Before walking the history, add the set of "negative" refs the
1376 * caller has asked to exclude to the bottom list.
1378 * This is used to compute "rev-list --ancestry-path A..B", as we need
1379 * to filter the result of "A..B" further to the ones that can actually
1380 * reach A.
1382 static void collect_bottom_commits(struct commit_list *list,
1383 struct commit_list **bottom)
1385 struct commit_list *elem;
1386 for (elem = list; elem; elem = elem->next)
1387 if (elem->item->object.flags & BOTTOM)
1388 commit_list_insert(elem->item, bottom);
1391 /* Assumes either left_only or right_only is set */
1392 static void limit_left_right(struct commit_list *list, struct rev_info *revs)
1394 struct commit_list *p;
1396 for (p = list; p; p = p->next) {
1397 struct commit *commit = p->item;
1399 if (revs->right_only) {
1400 if (commit->object.flags & SYMMETRIC_LEFT)
1401 commit->object.flags |= SHOWN;
1402 } else /* revs->left_only is set */
1403 if (!(commit->object.flags & SYMMETRIC_LEFT))
1404 commit->object.flags |= SHOWN;
1408 static int limit_list(struct rev_info *revs)
1410 int slop = SLOP;
1411 timestamp_t date = TIME_MAX;
1412 struct commit_list *original_list = revs->commits;
1413 struct commit_list *newlist = NULL;
1414 struct commit_list **p = &newlist;
1415 struct commit *interesting_cache = NULL;
1417 if (revs->ancestry_path_implicit_bottoms) {
1418 collect_bottom_commits(original_list,
1419 &revs->ancestry_path_bottoms);
1420 if (!revs->ancestry_path_bottoms)
1421 die("--ancestry-path given but there are no bottom commits");
1424 while (original_list) {
1425 struct commit *commit = pop_commit(&original_list);
1426 struct object *obj = &commit->object;
1427 show_early_output_fn_t show;
1429 if (commit == interesting_cache)
1430 interesting_cache = NULL;
1432 if (revs->max_age != -1 && (commit->date < revs->max_age))
1433 obj->flags |= UNINTERESTING;
1434 if (process_parents(revs, commit, &original_list, NULL) < 0)
1435 return -1;
1436 if (obj->flags & UNINTERESTING) {
1437 mark_parents_uninteresting(revs, commit);
1438 slop = still_interesting(original_list, date, slop, &interesting_cache);
1439 if (slop)
1440 continue;
1441 break;
1443 if (revs->min_age != -1 && (commit->date > revs->min_age) &&
1444 !revs->line_level_traverse)
1445 continue;
1446 if (revs->max_age_as_filter != -1 &&
1447 (commit->date < revs->max_age_as_filter) && !revs->line_level_traverse)
1448 continue;
1449 date = commit->date;
1450 p = &commit_list_insert(commit, p)->next;
1452 show = show_early_output;
1453 if (!show)
1454 continue;
1456 show(revs, newlist);
1457 show_early_output = NULL;
1459 if (revs->cherry_pick || revs->cherry_mark)
1460 cherry_pick_list(newlist, revs);
1462 if (revs->left_only || revs->right_only)
1463 limit_left_right(newlist, revs);
1465 if (revs->ancestry_path)
1466 limit_to_ancestry(revs->ancestry_path_bottoms, newlist);
1469 * Check if any commits have become TREESAME by some of their parents
1470 * becoming UNINTERESTING.
1472 if (limiting_can_increase_treesame(revs)) {
1473 struct commit_list *list = NULL;
1474 for (list = newlist; list; list = list->next) {
1475 struct commit *c = list->item;
1476 if (c->object.flags & (UNINTERESTING | TREESAME))
1477 continue;
1478 update_treesame(revs, c);
1482 free_commit_list(original_list);
1483 revs->commits = newlist;
1484 return 0;
1488 * Add an entry to refs->cmdline with the specified information.
1489 * *name is copied.
1491 static void add_rev_cmdline(struct rev_info *revs,
1492 struct object *item,
1493 const char *name,
1494 int whence,
1495 unsigned flags)
1497 struct rev_cmdline_info *info = &revs->cmdline;
1498 unsigned int nr = info->nr;
1500 ALLOC_GROW(info->rev, nr + 1, info->alloc);
1501 info->rev[nr].item = item;
1502 info->rev[nr].name = xstrdup(name);
1503 info->rev[nr].whence = whence;
1504 info->rev[nr].flags = flags;
1505 info->nr++;
1508 static void add_rev_cmdline_list(struct rev_info *revs,
1509 struct commit_list *commit_list,
1510 int whence,
1511 unsigned flags)
1513 while (commit_list) {
1514 struct object *object = &commit_list->item->object;
1515 add_rev_cmdline(revs, object, oid_to_hex(&object->oid),
1516 whence, flags);
1517 commit_list = commit_list->next;
1521 int ref_excluded(const struct ref_exclusions *exclusions, const char *path)
1523 const char *stripped_path = strip_namespace(path);
1524 struct string_list_item *item;
1526 for_each_string_list_item(item, &exclusions->excluded_refs) {
1527 if (!wildmatch(item->string, path, 0))
1528 return 1;
1531 if (ref_is_hidden(stripped_path, path, &exclusions->hidden_refs))
1532 return 1;
1534 return 0;
1537 void init_ref_exclusions(struct ref_exclusions *exclusions)
1539 struct ref_exclusions blank = REF_EXCLUSIONS_INIT;
1540 memcpy(exclusions, &blank, sizeof(*exclusions));
1543 void clear_ref_exclusions(struct ref_exclusions *exclusions)
1545 string_list_clear(&exclusions->excluded_refs, 0);
1546 string_list_clear(&exclusions->hidden_refs, 0);
1547 exclusions->hidden_refs_configured = 0;
1550 void add_ref_exclusion(struct ref_exclusions *exclusions, const char *exclude)
1552 string_list_append(&exclusions->excluded_refs, exclude);
1555 struct exclude_hidden_refs_cb {
1556 struct ref_exclusions *exclusions;
1557 const char *section;
1560 static int hide_refs_config(const char *var, const char *value, void *cb_data)
1562 struct exclude_hidden_refs_cb *cb = cb_data;
1563 cb->exclusions->hidden_refs_configured = 1;
1564 return parse_hide_refs_config(var, value, cb->section,
1565 &cb->exclusions->hidden_refs);
1568 void exclude_hidden_refs(struct ref_exclusions *exclusions, const char *section)
1570 struct exclude_hidden_refs_cb cb;
1572 if (strcmp(section, "receive") && strcmp(section, "uploadpack"))
1573 die(_("unsupported section for hidden refs: %s"), section);
1575 if (exclusions->hidden_refs_configured)
1576 die(_("--exclude-hidden= passed more than once"));
1578 cb.exclusions = exclusions;
1579 cb.section = section;
1581 git_config(hide_refs_config, &cb);
1584 struct all_refs_cb {
1585 int all_flags;
1586 int warned_bad_reflog;
1587 struct rev_info *all_revs;
1588 const char *name_for_errormsg;
1589 struct worktree *wt;
1592 static int handle_one_ref(const char *path, const struct object_id *oid,
1593 int flag UNUSED,
1594 void *cb_data)
1596 struct all_refs_cb *cb = cb_data;
1597 struct object *object;
1599 if (ref_excluded(&cb->all_revs->ref_excludes, path))
1600 return 0;
1602 object = get_reference(cb->all_revs, path, oid, cb->all_flags);
1603 add_rev_cmdline(cb->all_revs, object, path, REV_CMD_REF, cb->all_flags);
1604 add_pending_object(cb->all_revs, object, path);
1605 return 0;
1608 static void init_all_refs_cb(struct all_refs_cb *cb, struct rev_info *revs,
1609 unsigned flags)
1611 cb->all_revs = revs;
1612 cb->all_flags = flags;
1613 revs->rev_input_given = 1;
1614 cb->wt = NULL;
1617 static void handle_refs(struct ref_store *refs,
1618 struct rev_info *revs, unsigned flags,
1619 int (*for_each)(struct ref_store *, each_ref_fn, void *))
1621 struct all_refs_cb cb;
1623 if (!refs) {
1624 /* this could happen with uninitialized submodules */
1625 return;
1628 init_all_refs_cb(&cb, revs, flags);
1629 for_each(refs, handle_one_ref, &cb);
1632 static void handle_one_reflog_commit(struct object_id *oid, void *cb_data)
1634 struct all_refs_cb *cb = cb_data;
1635 if (!is_null_oid(oid)) {
1636 struct object *o = parse_object(cb->all_revs->repo, oid);
1637 if (o) {
1638 o->flags |= cb->all_flags;
1639 /* ??? CMDLINEFLAGS ??? */
1640 add_pending_object(cb->all_revs, o, "");
1642 else if (!cb->warned_bad_reflog) {
1643 warning("reflog of '%s' references pruned commits",
1644 cb->name_for_errormsg);
1645 cb->warned_bad_reflog = 1;
1650 static int handle_one_reflog_ent(struct object_id *ooid, struct object_id *noid,
1651 const char *email UNUSED,
1652 timestamp_t timestamp UNUSED,
1653 int tz UNUSED,
1654 const char *message UNUSED,
1655 void *cb_data)
1657 handle_one_reflog_commit(ooid, cb_data);
1658 handle_one_reflog_commit(noid, cb_data);
1659 return 0;
1662 static int handle_one_reflog(const char *refname_in_wt,
1663 const struct object_id *oid UNUSED,
1664 int flag UNUSED, void *cb_data)
1666 struct all_refs_cb *cb = cb_data;
1667 struct strbuf refname = STRBUF_INIT;
1669 cb->warned_bad_reflog = 0;
1670 strbuf_worktree_ref(cb->wt, &refname, refname_in_wt);
1671 cb->name_for_errormsg = refname.buf;
1672 refs_for_each_reflog_ent(get_main_ref_store(the_repository),
1673 refname.buf,
1674 handle_one_reflog_ent, cb_data);
1675 strbuf_release(&refname);
1676 return 0;
1679 static void add_other_reflogs_to_pending(struct all_refs_cb *cb)
1681 struct worktree **worktrees, **p;
1683 worktrees = get_worktrees();
1684 for (p = worktrees; *p; p++) {
1685 struct worktree *wt = *p;
1687 if (wt->is_current)
1688 continue;
1690 cb->wt = wt;
1691 refs_for_each_reflog(get_worktree_ref_store(wt),
1692 handle_one_reflog,
1693 cb);
1695 free_worktrees(worktrees);
1698 void add_reflogs_to_pending(struct rev_info *revs, unsigned flags)
1700 struct all_refs_cb cb;
1702 cb.all_revs = revs;
1703 cb.all_flags = flags;
1704 cb.wt = NULL;
1705 for_each_reflog(handle_one_reflog, &cb);
1707 if (!revs->single_worktree)
1708 add_other_reflogs_to_pending(&cb);
1711 static void add_cache_tree(struct cache_tree *it, struct rev_info *revs,
1712 struct strbuf *path, unsigned int flags)
1714 size_t baselen = path->len;
1715 int i;
1717 if (it->entry_count >= 0) {
1718 struct tree *tree = lookup_tree(revs->repo, &it->oid);
1719 tree->object.flags |= flags;
1720 add_pending_object_with_path(revs, &tree->object, "",
1721 040000, path->buf);
1724 for (i = 0; i < it->subtree_nr; i++) {
1725 struct cache_tree_sub *sub = it->down[i];
1726 strbuf_addf(path, "%s%s", baselen ? "/" : "", sub->name);
1727 add_cache_tree(sub->cache_tree, revs, path, flags);
1728 strbuf_setlen(path, baselen);
1733 static void add_resolve_undo_to_pending(struct index_state *istate, struct rev_info *revs)
1735 struct string_list_item *item;
1736 struct string_list *resolve_undo = istate->resolve_undo;
1738 if (!resolve_undo)
1739 return;
1741 for_each_string_list_item(item, resolve_undo) {
1742 const char *path = item->string;
1743 struct resolve_undo_info *ru = item->util;
1744 int i;
1746 if (!ru)
1747 continue;
1748 for (i = 0; i < 3; i++) {
1749 struct blob *blob;
1751 if (!ru->mode[i] || !S_ISREG(ru->mode[i]))
1752 continue;
1754 blob = lookup_blob(revs->repo, &ru->oid[i]);
1755 if (!blob) {
1756 warning(_("resolve-undo records `%s` which is missing"),
1757 oid_to_hex(&ru->oid[i]));
1758 continue;
1760 add_pending_object_with_path(revs, &blob->object, "",
1761 ru->mode[i], path);
1766 static void do_add_index_objects_to_pending(struct rev_info *revs,
1767 struct index_state *istate,
1768 unsigned int flags)
1770 int i;
1772 /* TODO: audit for interaction with sparse-index. */
1773 ensure_full_index(istate);
1774 for (i = 0; i < istate->cache_nr; i++) {
1775 struct cache_entry *ce = istate->cache[i];
1776 struct blob *blob;
1778 if (S_ISGITLINK(ce->ce_mode))
1779 continue;
1781 blob = lookup_blob(revs->repo, &ce->oid);
1782 if (!blob)
1783 die("unable to add index blob to traversal");
1784 blob->object.flags |= flags;
1785 add_pending_object_with_path(revs, &blob->object, "",
1786 ce->ce_mode, ce->name);
1789 if (istate->cache_tree) {
1790 struct strbuf path = STRBUF_INIT;
1791 add_cache_tree(istate->cache_tree, revs, &path, flags);
1792 strbuf_release(&path);
1795 add_resolve_undo_to_pending(istate, revs);
1798 void add_index_objects_to_pending(struct rev_info *revs, unsigned int flags)
1800 struct worktree **worktrees, **p;
1802 repo_read_index(revs->repo);
1803 do_add_index_objects_to_pending(revs, revs->repo->index, flags);
1805 if (revs->single_worktree)
1806 return;
1808 worktrees = get_worktrees();
1809 for (p = worktrees; *p; p++) {
1810 struct worktree *wt = *p;
1811 struct index_state istate = { NULL };
1813 if (wt->is_current)
1814 continue; /* current index already taken care of */
1816 if (read_index_from(&istate,
1817 worktree_git_path(wt, "index"),
1818 get_worktree_git_dir(wt)) > 0)
1819 do_add_index_objects_to_pending(revs, &istate, flags);
1820 discard_index(&istate);
1822 free_worktrees(worktrees);
1825 struct add_alternate_refs_data {
1826 struct rev_info *revs;
1827 unsigned int flags;
1830 static void add_one_alternate_ref(const struct object_id *oid,
1831 void *vdata)
1833 const char *name = ".alternate";
1834 struct add_alternate_refs_data *data = vdata;
1835 struct object *obj;
1837 obj = get_reference(data->revs, name, oid, data->flags);
1838 add_rev_cmdline(data->revs, obj, name, REV_CMD_REV, data->flags);
1839 add_pending_object(data->revs, obj, name);
1842 static void add_alternate_refs_to_pending(struct rev_info *revs,
1843 unsigned int flags)
1845 struct add_alternate_refs_data data;
1846 data.revs = revs;
1847 data.flags = flags;
1848 for_each_alternate_ref(add_one_alternate_ref, &data);
1851 static int add_parents_only(struct rev_info *revs, const char *arg_, int flags,
1852 int exclude_parent)
1854 struct object_id oid;
1855 struct object *it;
1856 struct commit *commit;
1857 struct commit_list *parents;
1858 int parent_number;
1859 const char *arg = arg_;
1861 if (*arg == '^') {
1862 flags ^= UNINTERESTING | BOTTOM;
1863 arg++;
1865 if (get_oid_committish(arg, &oid))
1866 return 0;
1867 while (1) {
1868 it = get_reference(revs, arg, &oid, 0);
1869 if (!it && revs->ignore_missing)
1870 return 0;
1871 if (it->type != OBJ_TAG)
1872 break;
1873 if (!((struct tag*)it)->tagged)
1874 return 0;
1875 oidcpy(&oid, &((struct tag*)it)->tagged->oid);
1877 if (it->type != OBJ_COMMIT)
1878 return 0;
1879 commit = (struct commit *)it;
1880 if (exclude_parent &&
1881 exclude_parent > commit_list_count(commit->parents))
1882 return 0;
1883 for (parents = commit->parents, parent_number = 1;
1884 parents;
1885 parents = parents->next, parent_number++) {
1886 if (exclude_parent && parent_number != exclude_parent)
1887 continue;
1889 it = &parents->item->object;
1890 it->flags |= flags;
1891 add_rev_cmdline(revs, it, arg_, REV_CMD_PARENTS_ONLY, flags);
1892 add_pending_object(revs, it, arg);
1894 return 1;
1897 void repo_init_revisions(struct repository *r,
1898 struct rev_info *revs,
1899 const char *prefix)
1901 struct rev_info blank = REV_INFO_INIT;
1902 memcpy(revs, &blank, sizeof(*revs));
1904 revs->repo = r;
1905 revs->pruning.repo = r;
1906 revs->pruning.add_remove = file_add_remove;
1907 revs->pruning.change = file_change;
1908 revs->pruning.change_fn_data = revs;
1909 revs->prefix = prefix;
1911 grep_init(&revs->grep_filter, revs->repo);
1912 revs->grep_filter.status_only = 1;
1914 repo_diff_setup(revs->repo, &revs->diffopt);
1915 if (prefix && !revs->diffopt.prefix) {
1916 revs->diffopt.prefix = prefix;
1917 revs->diffopt.prefix_length = strlen(prefix);
1920 init_display_notes(&revs->notes_opt);
1921 list_objects_filter_init(&revs->filter);
1922 init_ref_exclusions(&revs->ref_excludes);
1925 static void add_pending_commit_list(struct rev_info *revs,
1926 struct commit_list *commit_list,
1927 unsigned int flags)
1929 while (commit_list) {
1930 struct object *object = &commit_list->item->object;
1931 object->flags |= flags;
1932 add_pending_object(revs, object, oid_to_hex(&object->oid));
1933 commit_list = commit_list->next;
1937 static void prepare_show_merge(struct rev_info *revs)
1939 struct commit_list *bases;
1940 struct commit *head, *other;
1941 struct object_id oid;
1942 const char **prune = NULL;
1943 int i, prune_num = 1; /* counting terminating NULL */
1944 struct index_state *istate = revs->repo->index;
1946 if (get_oid("HEAD", &oid))
1947 die("--merge without HEAD?");
1948 head = lookup_commit_or_die(&oid, "HEAD");
1949 if (get_oid("MERGE_HEAD", &oid))
1950 die("--merge without MERGE_HEAD?");
1951 other = lookup_commit_or_die(&oid, "MERGE_HEAD");
1952 add_pending_object(revs, &head->object, "HEAD");
1953 add_pending_object(revs, &other->object, "MERGE_HEAD");
1954 bases = get_merge_bases(head, other);
1955 add_rev_cmdline_list(revs, bases, REV_CMD_MERGE_BASE, UNINTERESTING | BOTTOM);
1956 add_pending_commit_list(revs, bases, UNINTERESTING | BOTTOM);
1957 free_commit_list(bases);
1958 head->object.flags |= SYMMETRIC_LEFT;
1960 if (!istate->cache_nr)
1961 repo_read_index(revs->repo);
1962 for (i = 0; i < istate->cache_nr; i++) {
1963 const struct cache_entry *ce = istate->cache[i];
1964 if (!ce_stage(ce))
1965 continue;
1966 if (ce_path_match(istate, ce, &revs->prune_data, NULL)) {
1967 prune_num++;
1968 REALLOC_ARRAY(prune, prune_num);
1969 prune[prune_num-2] = ce->name;
1970 prune[prune_num-1] = NULL;
1972 while ((i+1 < istate->cache_nr) &&
1973 ce_same_name(ce, istate->cache[i+1]))
1974 i++;
1976 clear_pathspec(&revs->prune_data);
1977 parse_pathspec(&revs->prune_data, PATHSPEC_ALL_MAGIC & ~PATHSPEC_LITERAL,
1978 PATHSPEC_PREFER_FULL | PATHSPEC_LITERAL_PATH, "", prune);
1979 revs->limited = 1;
1982 static int dotdot_missing(const char *arg, char *dotdot,
1983 struct rev_info *revs, int symmetric)
1985 if (revs->ignore_missing)
1986 return 0;
1987 /* de-munge so we report the full argument */
1988 *dotdot = '.';
1989 die(symmetric
1990 ? "Invalid symmetric difference expression %s"
1991 : "Invalid revision range %s", arg);
1994 static int handle_dotdot_1(const char *arg, char *dotdot,
1995 struct rev_info *revs, int flags,
1996 int cant_be_filename,
1997 struct object_context *a_oc,
1998 struct object_context *b_oc)
2000 const char *a_name, *b_name;
2001 struct object_id a_oid, b_oid;
2002 struct object *a_obj, *b_obj;
2003 unsigned int a_flags, b_flags;
2004 int symmetric = 0;
2005 unsigned int flags_exclude = flags ^ (UNINTERESTING | BOTTOM);
2006 unsigned int oc_flags = GET_OID_COMMITTISH | GET_OID_RECORD_PATH;
2008 a_name = arg;
2009 if (!*a_name)
2010 a_name = "HEAD";
2012 b_name = dotdot + 2;
2013 if (*b_name == '.') {
2014 symmetric = 1;
2015 b_name++;
2017 if (!*b_name)
2018 b_name = "HEAD";
2020 if (get_oid_with_context(revs->repo, a_name, oc_flags, &a_oid, a_oc) ||
2021 get_oid_with_context(revs->repo, b_name, oc_flags, &b_oid, b_oc))
2022 return -1;
2024 if (!cant_be_filename) {
2025 *dotdot = '.';
2026 verify_non_filename(revs->prefix, arg);
2027 *dotdot = '\0';
2030 a_obj = parse_object(revs->repo, &a_oid);
2031 b_obj = parse_object(revs->repo, &b_oid);
2032 if (!a_obj || !b_obj)
2033 return dotdot_missing(arg, dotdot, revs, symmetric);
2035 if (!symmetric) {
2036 /* just A..B */
2037 b_flags = flags;
2038 a_flags = flags_exclude;
2039 } else {
2040 /* A...B -- find merge bases between the two */
2041 struct commit *a, *b;
2042 struct commit_list *exclude;
2044 a = lookup_commit_reference(revs->repo, &a_obj->oid);
2045 b = lookup_commit_reference(revs->repo, &b_obj->oid);
2046 if (!a || !b)
2047 return dotdot_missing(arg, dotdot, revs, symmetric);
2049 exclude = get_merge_bases(a, b);
2050 add_rev_cmdline_list(revs, exclude, REV_CMD_MERGE_BASE,
2051 flags_exclude);
2052 add_pending_commit_list(revs, exclude, flags_exclude);
2053 free_commit_list(exclude);
2055 b_flags = flags;
2056 a_flags = flags | SYMMETRIC_LEFT;
2059 a_obj->flags |= a_flags;
2060 b_obj->flags |= b_flags;
2061 add_rev_cmdline(revs, a_obj, a_name, REV_CMD_LEFT, a_flags);
2062 add_rev_cmdline(revs, b_obj, b_name, REV_CMD_RIGHT, b_flags);
2063 add_pending_object_with_path(revs, a_obj, a_name, a_oc->mode, a_oc->path);
2064 add_pending_object_with_path(revs, b_obj, b_name, b_oc->mode, b_oc->path);
2065 return 0;
2068 static int handle_dotdot(const char *arg,
2069 struct rev_info *revs, int flags,
2070 int cant_be_filename)
2072 struct object_context a_oc, b_oc;
2073 char *dotdot = strstr(arg, "..");
2074 int ret;
2076 if (!dotdot)
2077 return -1;
2079 memset(&a_oc, 0, sizeof(a_oc));
2080 memset(&b_oc, 0, sizeof(b_oc));
2082 *dotdot = '\0';
2083 ret = handle_dotdot_1(arg, dotdot, revs, flags, cant_be_filename,
2084 &a_oc, &b_oc);
2085 *dotdot = '.';
2087 free(a_oc.path);
2088 free(b_oc.path);
2090 return ret;
2093 static int handle_revision_arg_1(const char *arg_, struct rev_info *revs, int flags, unsigned revarg_opt)
2095 struct object_context oc;
2096 char *mark;
2097 struct object *object;
2098 struct object_id oid;
2099 int local_flags;
2100 const char *arg = arg_;
2101 int cant_be_filename = revarg_opt & REVARG_CANNOT_BE_FILENAME;
2102 unsigned get_sha1_flags = GET_OID_RECORD_PATH;
2104 flags = flags & UNINTERESTING ? flags | BOTTOM : flags & ~BOTTOM;
2106 if (!cant_be_filename && !strcmp(arg, "..")) {
2108 * Just ".."? That is not a range but the
2109 * pathspec for the parent directory.
2111 return -1;
2114 if (!handle_dotdot(arg, revs, flags, revarg_opt))
2115 return 0;
2117 mark = strstr(arg, "^@");
2118 if (mark && !mark[2]) {
2119 *mark = 0;
2120 if (add_parents_only(revs, arg, flags, 0))
2121 return 0;
2122 *mark = '^';
2124 mark = strstr(arg, "^!");
2125 if (mark && !mark[2]) {
2126 *mark = 0;
2127 if (!add_parents_only(revs, arg, flags ^ (UNINTERESTING | BOTTOM), 0))
2128 *mark = '^';
2130 mark = strstr(arg, "^-");
2131 if (mark) {
2132 int exclude_parent = 1;
2134 if (mark[2]) {
2135 if (strtol_i(mark + 2, 10, &exclude_parent) ||
2136 exclude_parent < 1)
2137 return -1;
2140 *mark = 0;
2141 if (!add_parents_only(revs, arg, flags ^ (UNINTERESTING | BOTTOM), exclude_parent))
2142 *mark = '^';
2145 local_flags = 0;
2146 if (*arg == '^') {
2147 local_flags = UNINTERESTING | BOTTOM;
2148 arg++;
2151 if (revarg_opt & REVARG_COMMITTISH)
2152 get_sha1_flags |= GET_OID_COMMITTISH;
2154 if (get_oid_with_context(revs->repo, arg, get_sha1_flags, &oid, &oc))
2155 return revs->ignore_missing ? 0 : -1;
2156 if (!cant_be_filename)
2157 verify_non_filename(revs->prefix, arg);
2158 object = get_reference(revs, arg, &oid, flags ^ local_flags);
2159 if (!object)
2160 return revs->ignore_missing ? 0 : -1;
2161 add_rev_cmdline(revs, object, arg_, REV_CMD_REV, flags ^ local_flags);
2162 add_pending_object_with_path(revs, object, arg, oc.mode, oc.path);
2163 free(oc.path);
2164 return 0;
2167 int handle_revision_arg(const char *arg, struct rev_info *revs, int flags, unsigned revarg_opt)
2169 int ret = handle_revision_arg_1(arg, revs, flags, revarg_opt);
2170 if (!ret)
2171 revs->rev_input_given = 1;
2172 return ret;
2175 static void read_pathspec_from_stdin(struct strbuf *sb,
2176 struct strvec *prune)
2178 while (strbuf_getline(sb, stdin) != EOF)
2179 strvec_push(prune, sb->buf);
2182 static void read_revisions_from_stdin(struct rev_info *revs,
2183 struct strvec *prune)
2185 struct strbuf sb;
2186 int seen_dashdash = 0;
2187 int save_warning;
2189 save_warning = warn_on_object_refname_ambiguity;
2190 warn_on_object_refname_ambiguity = 0;
2192 strbuf_init(&sb, 1000);
2193 while (strbuf_getline(&sb, stdin) != EOF) {
2194 int len = sb.len;
2195 if (!len)
2196 break;
2197 if (sb.buf[0] == '-') {
2198 if (len == 2 && sb.buf[1] == '-') {
2199 seen_dashdash = 1;
2200 break;
2202 die("options not supported in --stdin mode");
2204 if (handle_revision_arg(sb.buf, revs, 0,
2205 REVARG_CANNOT_BE_FILENAME))
2206 die("bad revision '%s'", sb.buf);
2208 if (seen_dashdash)
2209 read_pathspec_from_stdin(&sb, prune);
2211 strbuf_release(&sb);
2212 warn_on_object_refname_ambiguity = save_warning;
2215 static void add_grep(struct rev_info *revs, const char *ptn, enum grep_pat_token what)
2217 append_grep_pattern(&revs->grep_filter, ptn, "command line", 0, what);
2220 static void add_header_grep(struct rev_info *revs, enum grep_header_field field, const char *pattern)
2222 append_header_grep_pattern(&revs->grep_filter, field, pattern);
2225 static void add_message_grep(struct rev_info *revs, const char *pattern)
2227 add_grep(revs, pattern, GREP_PATTERN_BODY);
2230 static int handle_revision_opt(struct rev_info *revs, int argc, const char **argv,
2231 int *unkc, const char **unkv,
2232 const struct setup_revision_opt* opt)
2234 const char *arg = argv[0];
2235 const char *optarg = NULL;
2236 int argcount;
2237 const unsigned hexsz = the_hash_algo->hexsz;
2239 /* pseudo revision arguments */
2240 if (!strcmp(arg, "--all") || !strcmp(arg, "--branches") ||
2241 !strcmp(arg, "--tags") || !strcmp(arg, "--remotes") ||
2242 !strcmp(arg, "--reflog") || !strcmp(arg, "--not") ||
2243 !strcmp(arg, "--no-walk") || !strcmp(arg, "--do-walk") ||
2244 !strcmp(arg, "--bisect") || starts_with(arg, "--glob=") ||
2245 !strcmp(arg, "--indexed-objects") ||
2246 !strcmp(arg, "--alternate-refs") ||
2247 starts_with(arg, "--exclude=") || starts_with(arg, "--exclude-hidden=") ||
2248 starts_with(arg, "--branches=") || starts_with(arg, "--tags=") ||
2249 starts_with(arg, "--remotes=") || starts_with(arg, "--no-walk="))
2251 unkv[(*unkc)++] = arg;
2252 return 1;
2255 if ((argcount = parse_long_opt("max-count", argv, &optarg))) {
2256 revs->max_count = atoi(optarg);
2257 revs->no_walk = 0;
2258 return argcount;
2259 } else if ((argcount = parse_long_opt("skip", argv, &optarg))) {
2260 revs->skip_count = atoi(optarg);
2261 return argcount;
2262 } else if ((*arg == '-') && isdigit(arg[1])) {
2263 /* accept -<digit>, like traditional "head" */
2264 if (strtol_i(arg + 1, 10, &revs->max_count) < 0 ||
2265 revs->max_count < 0)
2266 die("'%s': not a non-negative integer", arg + 1);
2267 revs->no_walk = 0;
2268 } else if (!strcmp(arg, "-n")) {
2269 if (argc <= 1)
2270 return error("-n requires an argument");
2271 revs->max_count = atoi(argv[1]);
2272 revs->no_walk = 0;
2273 return 2;
2274 } else if (skip_prefix(arg, "-n", &optarg)) {
2275 revs->max_count = atoi(optarg);
2276 revs->no_walk = 0;
2277 } else if ((argcount = parse_long_opt("max-age", argv, &optarg))) {
2278 revs->max_age = atoi(optarg);
2279 return argcount;
2280 } else if ((argcount = parse_long_opt("since", argv, &optarg))) {
2281 revs->max_age = approxidate(optarg);
2282 return argcount;
2283 } else if ((argcount = parse_long_opt("since-as-filter", argv, &optarg))) {
2284 revs->max_age_as_filter = approxidate(optarg);
2285 return argcount;
2286 } else if ((argcount = parse_long_opt("after", argv, &optarg))) {
2287 revs->max_age = approxidate(optarg);
2288 return argcount;
2289 } else if ((argcount = parse_long_opt("min-age", argv, &optarg))) {
2290 revs->min_age = atoi(optarg);
2291 return argcount;
2292 } else if ((argcount = parse_long_opt("before", argv, &optarg))) {
2293 revs->min_age = approxidate(optarg);
2294 return argcount;
2295 } else if ((argcount = parse_long_opt("until", argv, &optarg))) {
2296 revs->min_age = approxidate(optarg);
2297 return argcount;
2298 } else if (!strcmp(arg, "--first-parent")) {
2299 revs->first_parent_only = 1;
2300 } else if (!strcmp(arg, "--exclude-first-parent-only")) {
2301 revs->exclude_first_parent_only = 1;
2302 } else if (!strcmp(arg, "--ancestry-path")) {
2303 revs->ancestry_path = 1;
2304 revs->simplify_history = 0;
2305 revs->limited = 1;
2306 revs->ancestry_path_implicit_bottoms = 1;
2307 } else if (skip_prefix(arg, "--ancestry-path=", &optarg)) {
2308 struct commit *c;
2309 struct object_id oid;
2310 const char *msg = _("could not get commit for ancestry-path argument %s");
2312 revs->ancestry_path = 1;
2313 revs->simplify_history = 0;
2314 revs->limited = 1;
2316 if (repo_get_oid_committish(revs->repo, optarg, &oid))
2317 return error(msg, optarg);
2318 get_reference(revs, optarg, &oid, ANCESTRY_PATH);
2319 c = lookup_commit_reference(revs->repo, &oid);
2320 if (!c)
2321 return error(msg, optarg);
2322 commit_list_insert(c, &revs->ancestry_path_bottoms);
2323 } else if (!strcmp(arg, "-g") || !strcmp(arg, "--walk-reflogs")) {
2324 init_reflog_walk(&revs->reflog_info);
2325 } else if (!strcmp(arg, "--default")) {
2326 if (argc <= 1)
2327 return error("bad --default argument");
2328 revs->def = argv[1];
2329 return 2;
2330 } else if (!strcmp(arg, "--merge")) {
2331 revs->show_merge = 1;
2332 } else if (!strcmp(arg, "--topo-order")) {
2333 revs->sort_order = REV_SORT_IN_GRAPH_ORDER;
2334 revs->topo_order = 1;
2335 } else if (!strcmp(arg, "--simplify-merges")) {
2336 revs->simplify_merges = 1;
2337 revs->topo_order = 1;
2338 revs->rewrite_parents = 1;
2339 revs->simplify_history = 0;
2340 revs->limited = 1;
2341 } else if (!strcmp(arg, "--simplify-by-decoration")) {
2342 revs->simplify_merges = 1;
2343 revs->topo_order = 1;
2344 revs->rewrite_parents = 1;
2345 revs->simplify_history = 0;
2346 revs->simplify_by_decoration = 1;
2347 revs->limited = 1;
2348 revs->prune = 1;
2349 } else if (!strcmp(arg, "--date-order")) {
2350 revs->sort_order = REV_SORT_BY_COMMIT_DATE;
2351 revs->topo_order = 1;
2352 } else if (!strcmp(arg, "--author-date-order")) {
2353 revs->sort_order = REV_SORT_BY_AUTHOR_DATE;
2354 revs->topo_order = 1;
2355 } else if (!strcmp(arg, "--early-output")) {
2356 revs->early_output = 100;
2357 revs->topo_order = 1;
2358 } else if (skip_prefix(arg, "--early-output=", &optarg)) {
2359 if (strtoul_ui(optarg, 10, &revs->early_output) < 0)
2360 die("'%s': not a non-negative integer", optarg);
2361 revs->topo_order = 1;
2362 } else if (!strcmp(arg, "--parents")) {
2363 revs->rewrite_parents = 1;
2364 revs->print_parents = 1;
2365 } else if (!strcmp(arg, "--dense")) {
2366 revs->dense = 1;
2367 } else if (!strcmp(arg, "--sparse")) {
2368 revs->dense = 0;
2369 } else if (!strcmp(arg, "--in-commit-order")) {
2370 revs->tree_blobs_in_commit_order = 1;
2371 } else if (!strcmp(arg, "--remove-empty")) {
2372 revs->remove_empty_trees = 1;
2373 } else if (!strcmp(arg, "--merges")) {
2374 revs->min_parents = 2;
2375 } else if (!strcmp(arg, "--no-merges")) {
2376 revs->max_parents = 1;
2377 } else if (skip_prefix(arg, "--min-parents=", &optarg)) {
2378 revs->min_parents = atoi(optarg);
2379 } else if (!strcmp(arg, "--no-min-parents")) {
2380 revs->min_parents = 0;
2381 } else if (skip_prefix(arg, "--max-parents=", &optarg)) {
2382 revs->max_parents = atoi(optarg);
2383 } else if (!strcmp(arg, "--no-max-parents")) {
2384 revs->max_parents = -1;
2385 } else if (!strcmp(arg, "--boundary")) {
2386 revs->boundary = 1;
2387 } else if (!strcmp(arg, "--left-right")) {
2388 revs->left_right = 1;
2389 } else if (!strcmp(arg, "--left-only")) {
2390 if (revs->right_only)
2391 die("--left-only is incompatible with --right-only"
2392 " or --cherry");
2393 revs->left_only = 1;
2394 } else if (!strcmp(arg, "--right-only")) {
2395 if (revs->left_only)
2396 die(_("options '%s' and '%s' cannot be used together"), "--right-only", "--left-only");
2397 revs->right_only = 1;
2398 } else if (!strcmp(arg, "--cherry")) {
2399 if (revs->left_only)
2400 die(_("options '%s' and '%s' cannot be used together"), "--cherry", "--left-only");
2401 revs->cherry_mark = 1;
2402 revs->right_only = 1;
2403 revs->max_parents = 1;
2404 revs->limited = 1;
2405 } else if (!strcmp(arg, "--count")) {
2406 revs->count = 1;
2407 } else if (!strcmp(arg, "--cherry-mark")) {
2408 if (revs->cherry_pick)
2409 die(_("options '%s' and '%s' cannot be used together"), "--cherry-mark", "--cherry-pick");
2410 revs->cherry_mark = 1;
2411 revs->limited = 1; /* needs limit_list() */
2412 } else if (!strcmp(arg, "--cherry-pick")) {
2413 if (revs->cherry_mark)
2414 die(_("options '%s' and '%s' cannot be used together"), "--cherry-pick", "--cherry-mark");
2415 revs->cherry_pick = 1;
2416 revs->limited = 1;
2417 } else if (!strcmp(arg, "--objects")) {
2418 revs->tag_objects = 1;
2419 revs->tree_objects = 1;
2420 revs->blob_objects = 1;
2421 } else if (!strcmp(arg, "--objects-edge")) {
2422 revs->tag_objects = 1;
2423 revs->tree_objects = 1;
2424 revs->blob_objects = 1;
2425 revs->edge_hint = 1;
2426 } else if (!strcmp(arg, "--objects-edge-aggressive")) {
2427 revs->tag_objects = 1;
2428 revs->tree_objects = 1;
2429 revs->blob_objects = 1;
2430 revs->edge_hint = 1;
2431 revs->edge_hint_aggressive = 1;
2432 } else if (!strcmp(arg, "--verify-objects")) {
2433 revs->tag_objects = 1;
2434 revs->tree_objects = 1;
2435 revs->blob_objects = 1;
2436 revs->verify_objects = 1;
2437 disable_commit_graph(revs->repo);
2438 } else if (!strcmp(arg, "--unpacked")) {
2439 revs->unpacked = 1;
2440 } else if (starts_with(arg, "--unpacked=")) {
2441 die(_("--unpacked=<packfile> no longer supported"));
2442 } else if (!strcmp(arg, "--no-kept-objects")) {
2443 revs->no_kept_objects = 1;
2444 revs->keep_pack_cache_flags |= IN_CORE_KEEP_PACKS;
2445 revs->keep_pack_cache_flags |= ON_DISK_KEEP_PACKS;
2446 } else if (skip_prefix(arg, "--no-kept-objects=", &optarg)) {
2447 revs->no_kept_objects = 1;
2448 if (!strcmp(optarg, "in-core"))
2449 revs->keep_pack_cache_flags |= IN_CORE_KEEP_PACKS;
2450 if (!strcmp(optarg, "on-disk"))
2451 revs->keep_pack_cache_flags |= ON_DISK_KEEP_PACKS;
2452 } else if (!strcmp(arg, "-r")) {
2453 revs->diff = 1;
2454 revs->diffopt.flags.recursive = 1;
2455 } else if (!strcmp(arg, "-t")) {
2456 revs->diff = 1;
2457 revs->diffopt.flags.recursive = 1;
2458 revs->diffopt.flags.tree_in_recursive = 1;
2459 } else if ((argcount = diff_merges_parse_opts(revs, argv))) {
2460 return argcount;
2461 } else if (!strcmp(arg, "-v")) {
2462 revs->verbose_header = 1;
2463 } else if (!strcmp(arg, "--pretty")) {
2464 revs->verbose_header = 1;
2465 revs->pretty_given = 1;
2466 get_commit_format(NULL, revs);
2467 } else if (skip_prefix(arg, "--pretty=", &optarg) ||
2468 skip_prefix(arg, "--format=", &optarg)) {
2470 * Detached form ("--pretty X" as opposed to "--pretty=X")
2471 * not allowed, since the argument is optional.
2473 revs->verbose_header = 1;
2474 revs->pretty_given = 1;
2475 get_commit_format(optarg, revs);
2476 } else if (!strcmp(arg, "--expand-tabs")) {
2477 revs->expand_tabs_in_log = 8;
2478 } else if (!strcmp(arg, "--no-expand-tabs")) {
2479 revs->expand_tabs_in_log = 0;
2480 } else if (skip_prefix(arg, "--expand-tabs=", &arg)) {
2481 int val;
2482 if (strtol_i(arg, 10, &val) < 0 || val < 0)
2483 die("'%s': not a non-negative integer", arg);
2484 revs->expand_tabs_in_log = val;
2485 } else if (!strcmp(arg, "--show-notes") || !strcmp(arg, "--notes")) {
2486 enable_default_display_notes(&revs->notes_opt, &revs->show_notes);
2487 revs->show_notes_given = 1;
2488 } else if (!strcmp(arg, "--show-signature")) {
2489 revs->show_signature = 1;
2490 } else if (!strcmp(arg, "--no-show-signature")) {
2491 revs->show_signature = 0;
2492 } else if (!strcmp(arg, "--show-linear-break")) {
2493 revs->break_bar = " ..........";
2494 revs->track_linear = 1;
2495 revs->track_first_time = 1;
2496 } else if (skip_prefix(arg, "--show-linear-break=", &optarg)) {
2497 revs->break_bar = xstrdup(optarg);
2498 revs->track_linear = 1;
2499 revs->track_first_time = 1;
2500 } else if (skip_prefix(arg, "--show-notes=", &optarg) ||
2501 skip_prefix(arg, "--notes=", &optarg)) {
2502 if (starts_with(arg, "--show-notes=") &&
2503 revs->notes_opt.use_default_notes < 0)
2504 revs->notes_opt.use_default_notes = 1;
2505 enable_ref_display_notes(&revs->notes_opt, &revs->show_notes, optarg);
2506 revs->show_notes_given = 1;
2507 } else if (!strcmp(arg, "--no-notes")) {
2508 disable_display_notes(&revs->notes_opt, &revs->show_notes);
2509 revs->show_notes_given = 1;
2510 } else if (!strcmp(arg, "--standard-notes")) {
2511 revs->show_notes_given = 1;
2512 revs->notes_opt.use_default_notes = 1;
2513 } else if (!strcmp(arg, "--no-standard-notes")) {
2514 revs->notes_opt.use_default_notes = 0;
2515 } else if (!strcmp(arg, "--oneline")) {
2516 revs->verbose_header = 1;
2517 get_commit_format("oneline", revs);
2518 revs->pretty_given = 1;
2519 revs->abbrev_commit = 1;
2520 } else if (!strcmp(arg, "--graph")) {
2521 graph_clear(revs->graph);
2522 revs->graph = graph_init(revs);
2523 } else if (!strcmp(arg, "--no-graph")) {
2524 graph_clear(revs->graph);
2525 revs->graph = NULL;
2526 } else if (!strcmp(arg, "--encode-email-headers")) {
2527 revs->encode_email_headers = 1;
2528 } else if (!strcmp(arg, "--no-encode-email-headers")) {
2529 revs->encode_email_headers = 0;
2530 } else if (!strcmp(arg, "--root")) {
2531 revs->show_root_diff = 1;
2532 } else if (!strcmp(arg, "--no-commit-id")) {
2533 revs->no_commit_id = 1;
2534 } else if (!strcmp(arg, "--always")) {
2535 revs->always_show_header = 1;
2536 } else if (!strcmp(arg, "--no-abbrev")) {
2537 revs->abbrev = 0;
2538 } else if (!strcmp(arg, "--abbrev")) {
2539 revs->abbrev = DEFAULT_ABBREV;
2540 } else if (skip_prefix(arg, "--abbrev=", &optarg)) {
2541 revs->abbrev = strtoul(optarg, NULL, 10);
2542 if (revs->abbrev < MINIMUM_ABBREV)
2543 revs->abbrev = MINIMUM_ABBREV;
2544 else if (revs->abbrev > hexsz)
2545 revs->abbrev = hexsz;
2546 } else if (!strcmp(arg, "--abbrev-commit")) {
2547 revs->abbrev_commit = 1;
2548 revs->abbrev_commit_given = 1;
2549 } else if (!strcmp(arg, "--no-abbrev-commit")) {
2550 revs->abbrev_commit = 0;
2551 } else if (!strcmp(arg, "--full-diff")) {
2552 revs->diff = 1;
2553 revs->full_diff = 1;
2554 } else if (!strcmp(arg, "--show-pulls")) {
2555 revs->show_pulls = 1;
2556 } else if (!strcmp(arg, "--full-history")) {
2557 revs->simplify_history = 0;
2558 } else if (!strcmp(arg, "--relative-date")) {
2559 revs->date_mode.type = DATE_RELATIVE;
2560 revs->date_mode_explicit = 1;
2561 } else if ((argcount = parse_long_opt("date", argv, &optarg))) {
2562 parse_date_format(optarg, &revs->date_mode);
2563 revs->date_mode_explicit = 1;
2564 return argcount;
2565 } else if (!strcmp(arg, "--log-size")) {
2566 revs->show_log_size = 1;
2569 * Grepping the commit log
2571 else if ((argcount = parse_long_opt("author", argv, &optarg))) {
2572 add_header_grep(revs, GREP_HEADER_AUTHOR, optarg);
2573 return argcount;
2574 } else if ((argcount = parse_long_opt("committer", argv, &optarg))) {
2575 add_header_grep(revs, GREP_HEADER_COMMITTER, optarg);
2576 return argcount;
2577 } else if ((argcount = parse_long_opt("grep-reflog", argv, &optarg))) {
2578 add_header_grep(revs, GREP_HEADER_REFLOG, optarg);
2579 return argcount;
2580 } else if ((argcount = parse_long_opt("grep", argv, &optarg))) {
2581 add_message_grep(revs, optarg);
2582 return argcount;
2583 } else if (!strcmp(arg, "--basic-regexp")) {
2584 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_BRE;
2585 } else if (!strcmp(arg, "--extended-regexp") || !strcmp(arg, "-E")) {
2586 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_ERE;
2587 } else if (!strcmp(arg, "--regexp-ignore-case") || !strcmp(arg, "-i")) {
2588 revs->grep_filter.ignore_case = 1;
2589 revs->diffopt.pickaxe_opts |= DIFF_PICKAXE_IGNORE_CASE;
2590 } else if (!strcmp(arg, "--fixed-strings") || !strcmp(arg, "-F")) {
2591 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_FIXED;
2592 } else if (!strcmp(arg, "--perl-regexp") || !strcmp(arg, "-P")) {
2593 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_PCRE;
2594 } else if (!strcmp(arg, "--all-match")) {
2595 revs->grep_filter.all_match = 1;
2596 } else if (!strcmp(arg, "--invert-grep")) {
2597 revs->grep_filter.no_body_match = 1;
2598 } else if ((argcount = parse_long_opt("encoding", argv, &optarg))) {
2599 if (strcmp(optarg, "none"))
2600 git_log_output_encoding = xstrdup(optarg);
2601 else
2602 git_log_output_encoding = "";
2603 return argcount;
2604 } else if (!strcmp(arg, "--reverse")) {
2605 revs->reverse ^= 1;
2606 } else if (!strcmp(arg, "--children")) {
2607 revs->children.name = "children";
2608 revs->limited = 1;
2609 } else if (!strcmp(arg, "--ignore-missing")) {
2610 revs->ignore_missing = 1;
2611 } else if (opt && opt->allow_exclude_promisor_objects &&
2612 !strcmp(arg, "--exclude-promisor-objects")) {
2613 if (fetch_if_missing)
2614 BUG("exclude_promisor_objects can only be used when fetch_if_missing is 0");
2615 revs->exclude_promisor_objects = 1;
2616 } else {
2617 int opts = diff_opt_parse(&revs->diffopt, argv, argc, revs->prefix);
2618 if (!opts)
2619 unkv[(*unkc)++] = arg;
2620 return opts;
2623 return 1;
2626 void parse_revision_opt(struct rev_info *revs, struct parse_opt_ctx_t *ctx,
2627 const struct option *options,
2628 const char * const usagestr[])
2630 int n = handle_revision_opt(revs, ctx->argc, ctx->argv,
2631 &ctx->cpidx, ctx->out, NULL);
2632 if (n <= 0) {
2633 error("unknown option `%s'", ctx->argv[0]);
2634 usage_with_options(usagestr, options);
2636 ctx->argv += n;
2637 ctx->argc -= n;
2640 void revision_opts_finish(struct rev_info *revs)
2642 if (revs->graph && revs->track_linear)
2643 die(_("options '%s' and '%s' cannot be used together"), "--show-linear-break", "--graph");
2645 if (revs->graph) {
2646 revs->topo_order = 1;
2647 revs->rewrite_parents = 1;
2651 static int for_each_bisect_ref(struct ref_store *refs, each_ref_fn fn,
2652 void *cb_data, const char *term)
2654 struct strbuf bisect_refs = STRBUF_INIT;
2655 int status;
2656 strbuf_addf(&bisect_refs, "refs/bisect/%s", term);
2657 status = refs_for_each_fullref_in(refs, bisect_refs.buf, fn, cb_data);
2658 strbuf_release(&bisect_refs);
2659 return status;
2662 static int for_each_bad_bisect_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
2664 return for_each_bisect_ref(refs, fn, cb_data, term_bad);
2667 static int for_each_good_bisect_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
2669 return for_each_bisect_ref(refs, fn, cb_data, term_good);
2672 static int handle_revision_pseudo_opt(struct rev_info *revs,
2673 const char **argv, int *flags)
2675 const char *arg = argv[0];
2676 const char *optarg;
2677 struct ref_store *refs;
2678 int argcount;
2680 if (revs->repo != the_repository) {
2682 * We need some something like get_submodule_worktrees()
2683 * before we can go through all worktrees of a submodule,
2684 * .e.g with adding all HEADs from --all, which is not
2685 * supported right now, so stick to single worktree.
2687 if (!revs->single_worktree)
2688 BUG("--single-worktree cannot be used together with submodule");
2690 refs = get_main_ref_store(revs->repo);
2693 * NOTE!
2695 * Commands like "git shortlog" will not accept the options below
2696 * unless parse_revision_opt queues them (as opposed to erroring
2697 * out).
2699 * When implementing your new pseudo-option, remember to
2700 * register it in the list at the top of handle_revision_opt.
2702 if (!strcmp(arg, "--all")) {
2703 handle_refs(refs, revs, *flags, refs_for_each_ref);
2704 handle_refs(refs, revs, *flags, refs_head_ref);
2705 if (!revs->single_worktree) {
2706 struct all_refs_cb cb;
2708 init_all_refs_cb(&cb, revs, *flags);
2709 other_head_refs(handle_one_ref, &cb);
2711 clear_ref_exclusions(&revs->ref_excludes);
2712 } else if (!strcmp(arg, "--branches")) {
2713 if (revs->ref_excludes.hidden_refs_configured)
2714 return error(_("--exclude-hidden cannot be used together with --branches"));
2715 handle_refs(refs, revs, *flags, refs_for_each_branch_ref);
2716 clear_ref_exclusions(&revs->ref_excludes);
2717 } else if (!strcmp(arg, "--bisect")) {
2718 read_bisect_terms(&term_bad, &term_good);
2719 handle_refs(refs, revs, *flags, for_each_bad_bisect_ref);
2720 handle_refs(refs, revs, *flags ^ (UNINTERESTING | BOTTOM),
2721 for_each_good_bisect_ref);
2722 revs->bisect = 1;
2723 } else if (!strcmp(arg, "--tags")) {
2724 if (revs->ref_excludes.hidden_refs_configured)
2725 return error(_("--exclude-hidden cannot be used together with --tags"));
2726 handle_refs(refs, revs, *flags, refs_for_each_tag_ref);
2727 clear_ref_exclusions(&revs->ref_excludes);
2728 } else if (!strcmp(arg, "--remotes")) {
2729 if (revs->ref_excludes.hidden_refs_configured)
2730 return error(_("--exclude-hidden cannot be used together with --remotes"));
2731 handle_refs(refs, revs, *flags, refs_for_each_remote_ref);
2732 clear_ref_exclusions(&revs->ref_excludes);
2733 } else if ((argcount = parse_long_opt("glob", argv, &optarg))) {
2734 struct all_refs_cb cb;
2735 init_all_refs_cb(&cb, revs, *flags);
2736 for_each_glob_ref(handle_one_ref, optarg, &cb);
2737 clear_ref_exclusions(&revs->ref_excludes);
2738 return argcount;
2739 } else if ((argcount = parse_long_opt("exclude", argv, &optarg))) {
2740 add_ref_exclusion(&revs->ref_excludes, optarg);
2741 return argcount;
2742 } else if ((argcount = parse_long_opt("exclude-hidden", argv, &optarg))) {
2743 exclude_hidden_refs(&revs->ref_excludes, optarg);
2744 return argcount;
2745 } else if (skip_prefix(arg, "--branches=", &optarg)) {
2746 struct all_refs_cb cb;
2747 if (revs->ref_excludes.hidden_refs_configured)
2748 return error(_("--exclude-hidden cannot be used together with --branches"));
2749 init_all_refs_cb(&cb, revs, *flags);
2750 for_each_glob_ref_in(handle_one_ref, optarg, "refs/heads/", &cb);
2751 clear_ref_exclusions(&revs->ref_excludes);
2752 } else if (skip_prefix(arg, "--tags=", &optarg)) {
2753 struct all_refs_cb cb;
2754 if (revs->ref_excludes.hidden_refs_configured)
2755 return error(_("--exclude-hidden cannot be used together with --tags"));
2756 init_all_refs_cb(&cb, revs, *flags);
2757 for_each_glob_ref_in(handle_one_ref, optarg, "refs/tags/", &cb);
2758 clear_ref_exclusions(&revs->ref_excludes);
2759 } else if (skip_prefix(arg, "--remotes=", &optarg)) {
2760 struct all_refs_cb cb;
2761 if (revs->ref_excludes.hidden_refs_configured)
2762 return error(_("--exclude-hidden cannot be used together with --remotes"));
2763 init_all_refs_cb(&cb, revs, *flags);
2764 for_each_glob_ref_in(handle_one_ref, optarg, "refs/remotes/", &cb);
2765 clear_ref_exclusions(&revs->ref_excludes);
2766 } else if (!strcmp(arg, "--reflog")) {
2767 add_reflogs_to_pending(revs, *flags);
2768 } else if (!strcmp(arg, "--indexed-objects")) {
2769 add_index_objects_to_pending(revs, *flags);
2770 } else if (!strcmp(arg, "--alternate-refs")) {
2771 add_alternate_refs_to_pending(revs, *flags);
2772 } else if (!strcmp(arg, "--not")) {
2773 *flags ^= UNINTERESTING | BOTTOM;
2774 } else if (!strcmp(arg, "--no-walk")) {
2775 revs->no_walk = 1;
2776 } else if (skip_prefix(arg, "--no-walk=", &optarg)) {
2778 * Detached form ("--no-walk X" as opposed to "--no-walk=X")
2779 * not allowed, since the argument is optional.
2781 revs->no_walk = 1;
2782 if (!strcmp(optarg, "sorted"))
2783 revs->unsorted_input = 0;
2784 else if (!strcmp(optarg, "unsorted"))
2785 revs->unsorted_input = 1;
2786 else
2787 return error("invalid argument to --no-walk");
2788 } else if (!strcmp(arg, "--do-walk")) {
2789 revs->no_walk = 0;
2790 } else if (!strcmp(arg, "--single-worktree")) {
2791 revs->single_worktree = 1;
2792 } else if (skip_prefix(arg, ("--filter="), &arg)) {
2793 parse_list_objects_filter(&revs->filter, arg);
2794 } else if (!strcmp(arg, ("--no-filter"))) {
2795 list_objects_filter_set_no_filter(&revs->filter);
2796 } else {
2797 return 0;
2800 return 1;
2803 static void NORETURN diagnose_missing_default(const char *def)
2805 int flags;
2806 const char *refname;
2808 refname = resolve_ref_unsafe(def, 0, NULL, &flags);
2809 if (!refname || !(flags & REF_ISSYMREF) || (flags & REF_ISBROKEN))
2810 die(_("your current branch appears to be broken"));
2812 skip_prefix(refname, "refs/heads/", &refname);
2813 die(_("your current branch '%s' does not have any commits yet"),
2814 refname);
2818 * Parse revision information, filling in the "rev_info" structure,
2819 * and removing the used arguments from the argument list.
2821 * Returns the number of arguments left that weren't recognized
2822 * (which are also moved to the head of the argument list)
2824 int setup_revisions(int argc, const char **argv, struct rev_info *revs, struct setup_revision_opt *opt)
2826 int i, flags, left, seen_dashdash, revarg_opt;
2827 struct strvec prune_data = STRVEC_INIT;
2828 int seen_end_of_options = 0;
2830 /* First, search for "--" */
2831 if (opt && opt->assume_dashdash) {
2832 seen_dashdash = 1;
2833 } else {
2834 seen_dashdash = 0;
2835 for (i = 1; i < argc; i++) {
2836 const char *arg = argv[i];
2837 if (strcmp(arg, "--"))
2838 continue;
2839 if (opt && opt->free_removed_argv_elements)
2840 free((char *)argv[i]);
2841 argv[i] = NULL;
2842 argc = i;
2843 if (argv[i + 1])
2844 strvec_pushv(&prune_data, argv + i + 1);
2845 seen_dashdash = 1;
2846 break;
2850 /* Second, deal with arguments and options */
2851 flags = 0;
2852 revarg_opt = opt ? opt->revarg_opt : 0;
2853 if (seen_dashdash)
2854 revarg_opt |= REVARG_CANNOT_BE_FILENAME;
2855 for (left = i = 1; i < argc; i++) {
2856 const char *arg = argv[i];
2857 if (!seen_end_of_options && *arg == '-') {
2858 int opts;
2860 opts = handle_revision_pseudo_opt(
2861 revs, argv + i,
2862 &flags);
2863 if (opts > 0) {
2864 i += opts - 1;
2865 continue;
2868 if (!strcmp(arg, "--stdin")) {
2869 if (revs->disable_stdin) {
2870 argv[left++] = arg;
2871 continue;
2873 if (revs->read_from_stdin++)
2874 die("--stdin given twice?");
2875 read_revisions_from_stdin(revs, &prune_data);
2876 continue;
2879 if (!strcmp(arg, "--end-of-options")) {
2880 seen_end_of_options = 1;
2881 continue;
2884 opts = handle_revision_opt(revs, argc - i, argv + i,
2885 &left, argv, opt);
2886 if (opts > 0) {
2887 i += opts - 1;
2888 continue;
2890 if (opts < 0)
2891 exit(128);
2892 continue;
2896 if (handle_revision_arg(arg, revs, flags, revarg_opt)) {
2897 int j;
2898 if (seen_dashdash || *arg == '^')
2899 die("bad revision '%s'", arg);
2901 /* If we didn't have a "--":
2902 * (1) all filenames must exist;
2903 * (2) all rev-args must not be interpretable
2904 * as a valid filename.
2905 * but the latter we have checked in the main loop.
2907 for (j = i; j < argc; j++)
2908 verify_filename(revs->prefix, argv[j], j == i);
2910 strvec_pushv(&prune_data, argv + i);
2911 break;
2914 revision_opts_finish(revs);
2916 if (prune_data.nr) {
2918 * If we need to introduce the magic "a lone ':' means no
2919 * pathspec whatsoever", here is the place to do so.
2921 * if (prune_data.nr == 1 && !strcmp(prune_data[0], ":")) {
2922 * prune_data.nr = 0;
2923 * prune_data.alloc = 0;
2924 * free(prune_data.path);
2925 * prune_data.path = NULL;
2926 * } else {
2927 * terminate prune_data.alloc with NULL and
2928 * call init_pathspec() to set revs->prune_data here.
2931 parse_pathspec(&revs->prune_data, 0, 0,
2932 revs->prefix, prune_data.v);
2934 strvec_clear(&prune_data);
2936 if (!revs->def)
2937 revs->def = opt ? opt->def : NULL;
2938 if (opt && opt->tweak)
2939 opt->tweak(revs, opt);
2940 if (revs->show_merge)
2941 prepare_show_merge(revs);
2942 if (revs->def && !revs->pending.nr && !revs->rev_input_given) {
2943 struct object_id oid;
2944 struct object *object;
2945 struct object_context oc;
2946 if (get_oid_with_context(revs->repo, revs->def, 0, &oid, &oc))
2947 diagnose_missing_default(revs->def);
2948 object = get_reference(revs, revs->def, &oid, 0);
2949 add_pending_object_with_mode(revs, object, revs->def, oc.mode);
2952 /* Did the user ask for any diff output? Run the diff! */
2953 if (revs->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT)
2954 revs->diff = 1;
2956 /* Pickaxe, diff-filter and rename following need diffs */
2957 if ((revs->diffopt.pickaxe_opts & DIFF_PICKAXE_KINDS_MASK) ||
2958 revs->diffopt.filter ||
2959 revs->diffopt.flags.follow_renames)
2960 revs->diff = 1;
2962 if (revs->diffopt.objfind)
2963 revs->simplify_history = 0;
2965 if (revs->line_level_traverse) {
2966 if (want_ancestry(revs))
2967 revs->limited = 1;
2968 revs->topo_order = 1;
2971 if (revs->topo_order && !generation_numbers_enabled(the_repository))
2972 revs->limited = 1;
2974 if (revs->prune_data.nr) {
2975 copy_pathspec(&revs->pruning.pathspec, &revs->prune_data);
2976 /* Can't prune commits with rename following: the paths change.. */
2977 if (!revs->diffopt.flags.follow_renames)
2978 revs->prune = 1;
2979 if (!revs->full_diff)
2980 copy_pathspec(&revs->diffopt.pathspec,
2981 &revs->prune_data);
2984 diff_merges_setup_revs(revs);
2986 revs->diffopt.abbrev = revs->abbrev;
2988 diff_setup_done(&revs->diffopt);
2990 if (!is_encoding_utf8(get_log_output_encoding()))
2991 revs->grep_filter.ignore_locale = 1;
2992 compile_grep_patterns(&revs->grep_filter);
2994 if (revs->reverse && revs->reflog_info)
2995 die(_("options '%s' and '%s' cannot be used together"), "--reverse", "--walk-reflogs");
2996 if (revs->reflog_info && revs->limited)
2997 die("cannot combine --walk-reflogs with history-limiting options");
2998 if (revs->rewrite_parents && revs->children.name)
2999 die(_("options '%s' and '%s' cannot be used together"), "--parents", "--children");
3000 if (revs->filter.choice && !revs->blob_objects)
3001 die(_("object filtering requires --objects"));
3004 * Limitations on the graph functionality
3006 if (revs->reverse && revs->graph)
3007 die(_("options '%s' and '%s' cannot be used together"), "--reverse", "--graph");
3009 if (revs->reflog_info && revs->graph)
3010 die(_("options '%s' and '%s' cannot be used together"), "--walk-reflogs", "--graph");
3011 if (revs->no_walk && revs->graph)
3012 die(_("options '%s' and '%s' cannot be used together"), "--no-walk", "--graph");
3013 if (!revs->reflog_info && revs->grep_filter.use_reflog_filter)
3014 die(_("the option '%s' requires '%s'"), "--grep-reflog", "--walk-reflogs");
3016 if (revs->line_level_traverse &&
3017 (revs->diffopt.output_format & ~(DIFF_FORMAT_PATCH | DIFF_FORMAT_NO_OUTPUT)))
3018 die(_("-L does not yet support diff formats besides -p and -s"));
3020 if (revs->expand_tabs_in_log < 0)
3021 revs->expand_tabs_in_log = revs->expand_tabs_in_log_default;
3023 return left;
3026 static void release_revisions_cmdline(struct rev_cmdline_info *cmdline)
3028 unsigned int i;
3030 for (i = 0; i < cmdline->nr; i++)
3031 free((char *)cmdline->rev[i].name);
3032 free(cmdline->rev);
3035 static void release_revisions_mailmap(struct string_list *mailmap)
3037 if (!mailmap)
3038 return;
3039 clear_mailmap(mailmap);
3040 free(mailmap);
3043 static void release_revisions_topo_walk_info(struct topo_walk_info *info);
3045 void release_revisions(struct rev_info *revs)
3047 free_commit_list(revs->commits);
3048 free_commit_list(revs->ancestry_path_bottoms);
3049 object_array_clear(&revs->pending);
3050 object_array_clear(&revs->boundary_commits);
3051 release_revisions_cmdline(&revs->cmdline);
3052 list_objects_filter_release(&revs->filter);
3053 clear_pathspec(&revs->prune_data);
3054 date_mode_release(&revs->date_mode);
3055 release_revisions_mailmap(revs->mailmap);
3056 free_grep_patterns(&revs->grep_filter);
3057 /* TODO (need to handle "no_free"): diff_free(&revs->diffopt) */
3058 diff_free(&revs->pruning);
3059 reflog_walk_info_release(revs->reflog_info);
3060 release_revisions_topo_walk_info(revs->topo_walk_info);
3063 static void add_child(struct rev_info *revs, struct commit *parent, struct commit *child)
3065 struct commit_list *l = xcalloc(1, sizeof(*l));
3067 l->item = child;
3068 l->next = add_decoration(&revs->children, &parent->object, l);
3071 static int remove_duplicate_parents(struct rev_info *revs, struct commit *commit)
3073 struct treesame_state *ts = lookup_decoration(&revs->treesame, &commit->object);
3074 struct commit_list **pp, *p;
3075 int surviving_parents;
3077 /* Examine existing parents while marking ones we have seen... */
3078 pp = &commit->parents;
3079 surviving_parents = 0;
3080 while ((p = *pp) != NULL) {
3081 struct commit *parent = p->item;
3082 if (parent->object.flags & TMP_MARK) {
3083 *pp = p->next;
3084 if (ts)
3085 compact_treesame(revs, commit, surviving_parents);
3086 continue;
3088 parent->object.flags |= TMP_MARK;
3089 surviving_parents++;
3090 pp = &p->next;
3092 /* clear the temporary mark */
3093 for (p = commit->parents; p; p = p->next) {
3094 p->item->object.flags &= ~TMP_MARK;
3096 /* no update_treesame() - removing duplicates can't affect TREESAME */
3097 return surviving_parents;
3100 struct merge_simplify_state {
3101 struct commit *simplified;
3104 static struct merge_simplify_state *locate_simplify_state(struct rev_info *revs, struct commit *commit)
3106 struct merge_simplify_state *st;
3108 st = lookup_decoration(&revs->merge_simplification, &commit->object);
3109 if (!st) {
3110 CALLOC_ARRAY(st, 1);
3111 add_decoration(&revs->merge_simplification, &commit->object, st);
3113 return st;
3116 static int mark_redundant_parents(struct commit *commit)
3118 struct commit_list *h = reduce_heads(commit->parents);
3119 int i = 0, marked = 0;
3120 struct commit_list *po, *pn;
3122 /* Want these for sanity-checking only */
3123 int orig_cnt = commit_list_count(commit->parents);
3124 int cnt = commit_list_count(h);
3127 * Not ready to remove items yet, just mark them for now, based
3128 * on the output of reduce_heads(). reduce_heads outputs the reduced
3129 * set in its original order, so this isn't too hard.
3131 po = commit->parents;
3132 pn = h;
3133 while (po) {
3134 if (pn && po->item == pn->item) {
3135 pn = pn->next;
3136 i++;
3137 } else {
3138 po->item->object.flags |= TMP_MARK;
3139 marked++;
3141 po=po->next;
3144 if (i != cnt || cnt+marked != orig_cnt)
3145 die("mark_redundant_parents %d %d %d %d", orig_cnt, cnt, i, marked);
3147 free_commit_list(h);
3149 return marked;
3152 static int mark_treesame_root_parents(struct commit *commit)
3154 struct commit_list *p;
3155 int marked = 0;
3157 for (p = commit->parents; p; p = p->next) {
3158 struct commit *parent = p->item;
3159 if (!parent->parents && (parent->object.flags & TREESAME)) {
3160 parent->object.flags |= TMP_MARK;
3161 marked++;
3165 return marked;
3169 * Awkward naming - this means one parent we are TREESAME to.
3170 * cf mark_treesame_root_parents: root parents that are TREESAME (to an
3171 * empty tree). Better name suggestions?
3173 static int leave_one_treesame_to_parent(struct rev_info *revs, struct commit *commit)
3175 struct treesame_state *ts = lookup_decoration(&revs->treesame, &commit->object);
3176 struct commit *unmarked = NULL, *marked = NULL;
3177 struct commit_list *p;
3178 unsigned n;
3180 for (p = commit->parents, n = 0; p; p = p->next, n++) {
3181 if (ts->treesame[n]) {
3182 if (p->item->object.flags & TMP_MARK) {
3183 if (!marked)
3184 marked = p->item;
3185 } else {
3186 if (!unmarked) {
3187 unmarked = p->item;
3188 break;
3195 * If we are TREESAME to a marked-for-deletion parent, but not to any
3196 * unmarked parents, unmark the first TREESAME parent. This is the
3197 * parent that the default simplify_history==1 scan would have followed,
3198 * and it doesn't make sense to omit that path when asking for a
3199 * simplified full history. Retaining it improves the chances of
3200 * understanding odd missed merges that took an old version of a file.
3202 * Example:
3204 * I--------*X A modified the file, but mainline merge X used
3205 * \ / "-s ours", so took the version from I. X is
3206 * `-*A--' TREESAME to I and !TREESAME to A.
3208 * Default log from X would produce "I". Without this check,
3209 * --full-history --simplify-merges would produce "I-A-X", showing
3210 * the merge commit X and that it changed A, but not making clear that
3211 * it had just taken the I version. With this check, the topology above
3212 * is retained.
3214 * Note that it is possible that the simplification chooses a different
3215 * TREESAME parent from the default, in which case this test doesn't
3216 * activate, and we _do_ drop the default parent. Example:
3218 * I------X A modified the file, but it was reverted in B,
3219 * \ / meaning mainline merge X is TREESAME to both
3220 * *A-*B parents.
3222 * Default log would produce "I" by following the first parent;
3223 * --full-history --simplify-merges will produce "I-A-B". But this is a
3224 * reasonable result - it presents a logical full history leading from
3225 * I to X, and X is not an important merge.
3227 if (!unmarked && marked) {
3228 marked->object.flags &= ~TMP_MARK;
3229 return 1;
3232 return 0;
3235 static int remove_marked_parents(struct rev_info *revs, struct commit *commit)
3237 struct commit_list **pp, *p;
3238 int nth_parent, removed = 0;
3240 pp = &commit->parents;
3241 nth_parent = 0;
3242 while ((p = *pp) != NULL) {
3243 struct commit *parent = p->item;
3244 if (parent->object.flags & TMP_MARK) {
3245 parent->object.flags &= ~TMP_MARK;
3246 *pp = p->next;
3247 free(p);
3248 removed++;
3249 compact_treesame(revs, commit, nth_parent);
3250 continue;
3252 pp = &p->next;
3253 nth_parent++;
3256 /* Removing parents can only increase TREESAMEness */
3257 if (removed && !(commit->object.flags & TREESAME))
3258 update_treesame(revs, commit);
3260 return nth_parent;
3263 static struct commit_list **simplify_one(struct rev_info *revs, struct commit *commit, struct commit_list **tail)
3265 struct commit_list *p;
3266 struct commit *parent;
3267 struct merge_simplify_state *st, *pst;
3268 int cnt;
3270 st = locate_simplify_state(revs, commit);
3273 * Have we handled this one?
3275 if (st->simplified)
3276 return tail;
3279 * An UNINTERESTING commit simplifies to itself, so does a
3280 * root commit. We do not rewrite parents of such commit
3281 * anyway.
3283 if ((commit->object.flags & UNINTERESTING) || !commit->parents) {
3284 st->simplified = commit;
3285 return tail;
3289 * Do we know what commit all of our parents that matter
3290 * should be rewritten to? Otherwise we are not ready to
3291 * rewrite this one yet.
3293 for (cnt = 0, p = commit->parents; p; p = p->next) {
3294 pst = locate_simplify_state(revs, p->item);
3295 if (!pst->simplified) {
3296 tail = &commit_list_insert(p->item, tail)->next;
3297 cnt++;
3299 if (revs->first_parent_only)
3300 break;
3302 if (cnt) {
3303 tail = &commit_list_insert(commit, tail)->next;
3304 return tail;
3308 * Rewrite our list of parents. Note that this cannot
3309 * affect our TREESAME flags in any way - a commit is
3310 * always TREESAME to its simplification.
3312 for (p = commit->parents; p; p = p->next) {
3313 pst = locate_simplify_state(revs, p->item);
3314 p->item = pst->simplified;
3315 if (revs->first_parent_only)
3316 break;
3319 if (revs->first_parent_only)
3320 cnt = 1;
3321 else
3322 cnt = remove_duplicate_parents(revs, commit);
3325 * It is possible that we are a merge and one side branch
3326 * does not have any commit that touches the given paths;
3327 * in such a case, the immediate parent from that branch
3328 * will be rewritten to be the merge base.
3330 * o----X X: the commit we are looking at;
3331 * / / o: a commit that touches the paths;
3332 * ---o----'
3334 * Further, a merge of an independent branch that doesn't
3335 * touch the path will reduce to a treesame root parent:
3337 * ----o----X X: the commit we are looking at;
3338 * / o: a commit that touches the paths;
3339 * r r: a root commit not touching the paths
3341 * Detect and simplify both cases.
3343 if (1 < cnt) {
3344 int marked = mark_redundant_parents(commit);
3345 marked += mark_treesame_root_parents(commit);
3346 if (marked)
3347 marked -= leave_one_treesame_to_parent(revs, commit);
3348 if (marked)
3349 cnt = remove_marked_parents(revs, commit);
3353 * A commit simplifies to itself if it is a root, if it is
3354 * UNINTERESTING, if it touches the given paths, or if it is a
3355 * merge and its parents don't simplify to one relevant commit
3356 * (the first two cases are already handled at the beginning of
3357 * this function).
3359 * Otherwise, it simplifies to what its sole relevant parent
3360 * simplifies to.
3362 if (!cnt ||
3363 (commit->object.flags & UNINTERESTING) ||
3364 !(commit->object.flags & TREESAME) ||
3365 (parent = one_relevant_parent(revs, commit->parents)) == NULL ||
3366 (revs->show_pulls && (commit->object.flags & PULL_MERGE)))
3367 st->simplified = commit;
3368 else {
3369 pst = locate_simplify_state(revs, parent);
3370 st->simplified = pst->simplified;
3372 return tail;
3375 static void simplify_merges(struct rev_info *revs)
3377 struct commit_list *list, *next;
3378 struct commit_list *yet_to_do, **tail;
3379 struct commit *commit;
3381 if (!revs->prune)
3382 return;
3384 /* feed the list reversed */
3385 yet_to_do = NULL;
3386 for (list = revs->commits; list; list = next) {
3387 commit = list->item;
3388 next = list->next;
3390 * Do not free(list) here yet; the original list
3391 * is used later in this function.
3393 commit_list_insert(commit, &yet_to_do);
3395 while (yet_to_do) {
3396 list = yet_to_do;
3397 yet_to_do = NULL;
3398 tail = &yet_to_do;
3399 while (list) {
3400 commit = pop_commit(&list);
3401 tail = simplify_one(revs, commit, tail);
3405 /* clean up the result, removing the simplified ones */
3406 list = revs->commits;
3407 revs->commits = NULL;
3408 tail = &revs->commits;
3409 while (list) {
3410 struct merge_simplify_state *st;
3412 commit = pop_commit(&list);
3413 st = locate_simplify_state(revs, commit);
3414 if (st->simplified == commit)
3415 tail = &commit_list_insert(commit, tail)->next;
3419 static void set_children(struct rev_info *revs)
3421 struct commit_list *l;
3422 for (l = revs->commits; l; l = l->next) {
3423 struct commit *commit = l->item;
3424 struct commit_list *p;
3426 for (p = commit->parents; p; p = p->next)
3427 add_child(revs, p->item, commit);
3431 void reset_revision_walk(void)
3433 clear_object_flags(SEEN | ADDED | SHOWN | TOPO_WALK_EXPLORED | TOPO_WALK_INDEGREE);
3436 static int mark_uninteresting(const struct object_id *oid,
3437 struct packed_git *pack,
3438 uint32_t pos,
3439 void *cb)
3441 struct rev_info *revs = cb;
3442 struct object *o = lookup_unknown_object(revs->repo, oid);
3443 o->flags |= UNINTERESTING | SEEN;
3444 return 0;
3447 define_commit_slab(indegree_slab, int);
3448 define_commit_slab(author_date_slab, timestamp_t);
3450 struct topo_walk_info {
3451 timestamp_t min_generation;
3452 struct prio_queue explore_queue;
3453 struct prio_queue indegree_queue;
3454 struct prio_queue topo_queue;
3455 struct indegree_slab indegree;
3456 struct author_date_slab author_date;
3459 static int topo_walk_atexit_registered;
3460 static unsigned int count_explore_walked;
3461 static unsigned int count_indegree_walked;
3462 static unsigned int count_topo_walked;
3464 static void trace2_topo_walk_statistics_atexit(void)
3466 struct json_writer jw = JSON_WRITER_INIT;
3468 jw_object_begin(&jw, 0);
3469 jw_object_intmax(&jw, "count_explore_walked", count_explore_walked);
3470 jw_object_intmax(&jw, "count_indegree_walked", count_indegree_walked);
3471 jw_object_intmax(&jw, "count_topo_walked", count_topo_walked);
3472 jw_end(&jw);
3474 trace2_data_json("topo_walk", the_repository, "statistics", &jw);
3476 jw_release(&jw);
3479 static inline void test_flag_and_insert(struct prio_queue *q, struct commit *c, int flag)
3481 if (c->object.flags & flag)
3482 return;
3484 c->object.flags |= flag;
3485 prio_queue_put(q, c);
3488 static void explore_walk_step(struct rev_info *revs)
3490 struct topo_walk_info *info = revs->topo_walk_info;
3491 struct commit_list *p;
3492 struct commit *c = prio_queue_get(&info->explore_queue);
3494 if (!c)
3495 return;
3497 if (repo_parse_commit_gently(revs->repo, c, 1) < 0)
3498 return;
3500 count_explore_walked++;
3502 if (revs->sort_order == REV_SORT_BY_AUTHOR_DATE)
3503 record_author_date(&info->author_date, c);
3505 if (revs->max_age != -1 && (c->date < revs->max_age))
3506 c->object.flags |= UNINTERESTING;
3508 if (process_parents(revs, c, NULL, NULL) < 0)
3509 return;
3511 if (c->object.flags & UNINTERESTING)
3512 mark_parents_uninteresting(revs, c);
3514 for (p = c->parents; p; p = p->next)
3515 test_flag_and_insert(&info->explore_queue, p->item, TOPO_WALK_EXPLORED);
3518 static void explore_to_depth(struct rev_info *revs,
3519 timestamp_t gen_cutoff)
3521 struct topo_walk_info *info = revs->topo_walk_info;
3522 struct commit *c;
3523 while ((c = prio_queue_peek(&info->explore_queue)) &&
3524 commit_graph_generation(c) >= gen_cutoff)
3525 explore_walk_step(revs);
3528 static void indegree_walk_step(struct rev_info *revs)
3530 struct commit_list *p;
3531 struct topo_walk_info *info = revs->topo_walk_info;
3532 struct commit *c = prio_queue_get(&info->indegree_queue);
3534 if (!c)
3535 return;
3537 if (repo_parse_commit_gently(revs->repo, c, 1) < 0)
3538 return;
3540 count_indegree_walked++;
3542 explore_to_depth(revs, commit_graph_generation(c));
3544 for (p = c->parents; p; p = p->next) {
3545 struct commit *parent = p->item;
3546 int *pi = indegree_slab_at(&info->indegree, parent);
3548 if (repo_parse_commit_gently(revs->repo, parent, 1) < 0)
3549 return;
3551 if (*pi)
3552 (*pi)++;
3553 else
3554 *pi = 2;
3556 test_flag_and_insert(&info->indegree_queue, parent, TOPO_WALK_INDEGREE);
3558 if (revs->first_parent_only)
3559 return;
3563 static void compute_indegrees_to_depth(struct rev_info *revs,
3564 timestamp_t gen_cutoff)
3566 struct topo_walk_info *info = revs->topo_walk_info;
3567 struct commit *c;
3568 while ((c = prio_queue_peek(&info->indegree_queue)) &&
3569 commit_graph_generation(c) >= gen_cutoff)
3570 indegree_walk_step(revs);
3573 static void release_revisions_topo_walk_info(struct topo_walk_info *info)
3575 if (!info)
3576 return;
3577 clear_prio_queue(&info->explore_queue);
3578 clear_prio_queue(&info->indegree_queue);
3579 clear_prio_queue(&info->topo_queue);
3580 clear_indegree_slab(&info->indegree);
3581 clear_author_date_slab(&info->author_date);
3582 free(info);
3585 static void reset_topo_walk(struct rev_info *revs)
3587 release_revisions_topo_walk_info(revs->topo_walk_info);
3588 revs->topo_walk_info = NULL;
3591 static void init_topo_walk(struct rev_info *revs)
3593 struct topo_walk_info *info;
3594 struct commit_list *list;
3595 if (revs->topo_walk_info)
3596 reset_topo_walk(revs);
3598 revs->topo_walk_info = xmalloc(sizeof(struct topo_walk_info));
3599 info = revs->topo_walk_info;
3600 memset(info, 0, sizeof(struct topo_walk_info));
3602 init_indegree_slab(&info->indegree);
3603 memset(&info->explore_queue, 0, sizeof(info->explore_queue));
3604 memset(&info->indegree_queue, 0, sizeof(info->indegree_queue));
3605 memset(&info->topo_queue, 0, sizeof(info->topo_queue));
3607 switch (revs->sort_order) {
3608 default: /* REV_SORT_IN_GRAPH_ORDER */
3609 info->topo_queue.compare = NULL;
3610 break;
3611 case REV_SORT_BY_COMMIT_DATE:
3612 info->topo_queue.compare = compare_commits_by_commit_date;
3613 break;
3614 case REV_SORT_BY_AUTHOR_DATE:
3615 init_author_date_slab(&info->author_date);
3616 info->topo_queue.compare = compare_commits_by_author_date;
3617 info->topo_queue.cb_data = &info->author_date;
3618 break;
3621 info->explore_queue.compare = compare_commits_by_gen_then_commit_date;
3622 info->indegree_queue.compare = compare_commits_by_gen_then_commit_date;
3624 info->min_generation = GENERATION_NUMBER_INFINITY;
3625 for (list = revs->commits; list; list = list->next) {
3626 struct commit *c = list->item;
3627 timestamp_t generation;
3629 if (repo_parse_commit_gently(revs->repo, c, 1))
3630 continue;
3632 test_flag_and_insert(&info->explore_queue, c, TOPO_WALK_EXPLORED);
3633 test_flag_and_insert(&info->indegree_queue, c, TOPO_WALK_INDEGREE);
3635 generation = commit_graph_generation(c);
3636 if (generation < info->min_generation)
3637 info->min_generation = generation;
3639 *(indegree_slab_at(&info->indegree, c)) = 1;
3641 if (revs->sort_order == REV_SORT_BY_AUTHOR_DATE)
3642 record_author_date(&info->author_date, c);
3644 compute_indegrees_to_depth(revs, info->min_generation);
3646 for (list = revs->commits; list; list = list->next) {
3647 struct commit *c = list->item;
3649 if (*(indegree_slab_at(&info->indegree, c)) == 1)
3650 prio_queue_put(&info->topo_queue, c);
3654 * This is unfortunate; the initial tips need to be shown
3655 * in the order given from the revision traversal machinery.
3657 if (revs->sort_order == REV_SORT_IN_GRAPH_ORDER)
3658 prio_queue_reverse(&info->topo_queue);
3660 if (trace2_is_enabled() && !topo_walk_atexit_registered) {
3661 atexit(trace2_topo_walk_statistics_atexit);
3662 topo_walk_atexit_registered = 1;
3666 static struct commit *next_topo_commit(struct rev_info *revs)
3668 struct commit *c;
3669 struct topo_walk_info *info = revs->topo_walk_info;
3671 /* pop next off of topo_queue */
3672 c = prio_queue_get(&info->topo_queue);
3674 if (c)
3675 *(indegree_slab_at(&info->indegree, c)) = 0;
3677 return c;
3680 static void expand_topo_walk(struct rev_info *revs, struct commit *commit)
3682 struct commit_list *p;
3683 struct topo_walk_info *info = revs->topo_walk_info;
3684 if (process_parents(revs, commit, NULL, NULL) < 0) {
3685 if (!revs->ignore_missing_links)
3686 die("Failed to traverse parents of commit %s",
3687 oid_to_hex(&commit->object.oid));
3690 count_topo_walked++;
3692 for (p = commit->parents; p; p = p->next) {
3693 struct commit *parent = p->item;
3694 int *pi;
3695 timestamp_t generation;
3697 if (parent->object.flags & UNINTERESTING)
3698 continue;
3700 if (repo_parse_commit_gently(revs->repo, parent, 1) < 0)
3701 continue;
3703 generation = commit_graph_generation(parent);
3704 if (generation < info->min_generation) {
3705 info->min_generation = generation;
3706 compute_indegrees_to_depth(revs, info->min_generation);
3709 pi = indegree_slab_at(&info->indegree, parent);
3711 (*pi)--;
3712 if (*pi == 1)
3713 prio_queue_put(&info->topo_queue, parent);
3715 if (revs->first_parent_only)
3716 return;
3720 int prepare_revision_walk(struct rev_info *revs)
3722 int i;
3723 struct object_array old_pending;
3724 struct commit_list **next = &revs->commits;
3726 memcpy(&old_pending, &revs->pending, sizeof(old_pending));
3727 revs->pending.nr = 0;
3728 revs->pending.alloc = 0;
3729 revs->pending.objects = NULL;
3730 for (i = 0; i < old_pending.nr; i++) {
3731 struct object_array_entry *e = old_pending.objects + i;
3732 struct commit *commit = handle_commit(revs, e);
3733 if (commit) {
3734 if (!(commit->object.flags & SEEN)) {
3735 commit->object.flags |= SEEN;
3736 next = commit_list_append(commit, next);
3740 object_array_clear(&old_pending);
3742 /* Signal whether we need per-parent treesame decoration */
3743 if (revs->simplify_merges ||
3744 (revs->limited && limiting_can_increase_treesame(revs)))
3745 revs->treesame.name = "treesame";
3747 if (revs->exclude_promisor_objects) {
3748 for_each_packed_object(mark_uninteresting, revs,
3749 FOR_EACH_OBJECT_PROMISOR_ONLY);
3752 if (!revs->reflog_info)
3753 prepare_to_use_bloom_filter(revs);
3754 if (!revs->unsorted_input)
3755 commit_list_sort_by_date(&revs->commits);
3756 if (revs->no_walk)
3757 return 0;
3758 if (revs->limited) {
3759 if (limit_list(revs) < 0)
3760 return -1;
3761 if (revs->topo_order)
3762 sort_in_topological_order(&revs->commits, revs->sort_order);
3763 } else if (revs->topo_order)
3764 init_topo_walk(revs);
3765 if (revs->line_level_traverse && want_ancestry(revs))
3767 * At the moment we can only do line-level log with parent
3768 * rewriting by performing this expensive pre-filtering step.
3769 * If parent rewriting is not requested, then we rather
3770 * perform the line-level log filtering during the regular
3771 * history traversal.
3773 line_log_filter(revs);
3774 if (revs->simplify_merges)
3775 simplify_merges(revs);
3776 if (revs->children.name)
3777 set_children(revs);
3779 return 0;
3782 static enum rewrite_result rewrite_one_1(struct rev_info *revs,
3783 struct commit **pp,
3784 struct prio_queue *queue)
3786 for (;;) {
3787 struct commit *p = *pp;
3788 if (!revs->limited)
3789 if (process_parents(revs, p, NULL, queue) < 0)
3790 return rewrite_one_error;
3791 if (p->object.flags & UNINTERESTING)
3792 return rewrite_one_ok;
3793 if (!(p->object.flags & TREESAME))
3794 return rewrite_one_ok;
3795 if (!p->parents)
3796 return rewrite_one_noparents;
3797 if (!(p = one_relevant_parent(revs, p->parents)))
3798 return rewrite_one_ok;
3799 *pp = p;
3803 static void merge_queue_into_list(struct prio_queue *q, struct commit_list **list)
3805 while (q->nr) {
3806 struct commit *item = prio_queue_peek(q);
3807 struct commit_list *p = *list;
3809 if (p && p->item->date >= item->date)
3810 list = &p->next;
3811 else {
3812 p = commit_list_insert(item, list);
3813 list = &p->next; /* skip newly added item */
3814 prio_queue_get(q); /* pop item */
3819 static enum rewrite_result rewrite_one(struct rev_info *revs, struct commit **pp)
3821 struct prio_queue queue = { compare_commits_by_commit_date };
3822 enum rewrite_result ret = rewrite_one_1(revs, pp, &queue);
3823 merge_queue_into_list(&queue, &revs->commits);
3824 clear_prio_queue(&queue);
3825 return ret;
3828 int rewrite_parents(struct rev_info *revs, struct commit *commit,
3829 rewrite_parent_fn_t rewrite_parent)
3831 struct commit_list **pp = &commit->parents;
3832 while (*pp) {
3833 struct commit_list *parent = *pp;
3834 switch (rewrite_parent(revs, &parent->item)) {
3835 case rewrite_one_ok:
3836 break;
3837 case rewrite_one_noparents:
3838 *pp = parent->next;
3839 continue;
3840 case rewrite_one_error:
3841 return -1;
3843 pp = &parent->next;
3845 remove_duplicate_parents(revs, commit);
3846 return 0;
3849 static int commit_match(struct commit *commit, struct rev_info *opt)
3851 int retval;
3852 const char *encoding;
3853 const char *message;
3854 struct strbuf buf = STRBUF_INIT;
3856 if (!opt->grep_filter.pattern_list && !opt->grep_filter.header_list)
3857 return 1;
3859 /* Prepend "fake" headers as needed */
3860 if (opt->grep_filter.use_reflog_filter) {
3861 strbuf_addstr(&buf, "reflog ");
3862 get_reflog_message(&buf, opt->reflog_info);
3863 strbuf_addch(&buf, '\n');
3867 * We grep in the user's output encoding, under the assumption that it
3868 * is the encoding they are most likely to write their grep pattern
3869 * for. In addition, it means we will match the "notes" encoding below,
3870 * so we will not end up with a buffer that has two different encodings
3871 * in it.
3873 encoding = get_log_output_encoding();
3874 message = logmsg_reencode(commit, NULL, encoding);
3876 /* Copy the commit to temporary if we are using "fake" headers */
3877 if (buf.len)
3878 strbuf_addstr(&buf, message);
3880 if (opt->grep_filter.header_list && opt->mailmap) {
3881 const char *commit_headers[] = { "author ", "committer ", NULL };
3883 if (!buf.len)
3884 strbuf_addstr(&buf, message);
3886 apply_mailmap_to_header(&buf, commit_headers, opt->mailmap);
3889 /* Append "fake" message parts as needed */
3890 if (opt->show_notes) {
3891 if (!buf.len)
3892 strbuf_addstr(&buf, message);
3893 format_display_notes(&commit->object.oid, &buf, encoding, 1);
3897 * Find either in the original commit message, or in the temporary.
3898 * Note that we cast away the constness of "message" here. It is
3899 * const because it may come from the cached commit buffer. That's OK,
3900 * because we know that it is modifiable heap memory, and that while
3901 * grep_buffer may modify it for speed, it will restore any
3902 * changes before returning.
3904 if (buf.len)
3905 retval = grep_buffer(&opt->grep_filter, buf.buf, buf.len);
3906 else
3907 retval = grep_buffer(&opt->grep_filter,
3908 (char *)message, strlen(message));
3909 strbuf_release(&buf);
3910 unuse_commit_buffer(commit, message);
3911 return retval;
3914 static inline int want_ancestry(const struct rev_info *revs)
3916 return (revs->rewrite_parents || revs->children.name);
3920 * Return a timestamp to be used for --since/--until comparisons for this
3921 * commit, based on the revision options.
3923 static timestamp_t comparison_date(const struct rev_info *revs,
3924 struct commit *commit)
3926 return revs->reflog_info ?
3927 get_reflog_timestamp(revs->reflog_info) :
3928 commit->date;
3931 enum commit_action get_commit_action(struct rev_info *revs, struct commit *commit)
3933 if (commit->object.flags & SHOWN)
3934 return commit_ignore;
3935 if (revs->unpacked && has_object_pack(&commit->object.oid))
3936 return commit_ignore;
3937 if (revs->no_kept_objects) {
3938 if (has_object_kept_pack(&commit->object.oid,
3939 revs->keep_pack_cache_flags))
3940 return commit_ignore;
3942 if (commit->object.flags & UNINTERESTING)
3943 return commit_ignore;
3944 if (revs->line_level_traverse && !want_ancestry(revs)) {
3946 * In case of line-level log with parent rewriting
3947 * prepare_revision_walk() already took care of all line-level
3948 * log filtering, and there is nothing left to do here.
3950 * If parent rewriting was not requested, then this is the
3951 * place to perform the line-level log filtering. Notably,
3952 * this check, though expensive, must come before the other,
3953 * cheaper filtering conditions, because the tracked line
3954 * ranges must be adjusted even when the commit will end up
3955 * being ignored based on other conditions.
3957 if (!line_log_process_ranges_arbitrary_commit(revs, commit))
3958 return commit_ignore;
3960 if (revs->min_age != -1 &&
3961 comparison_date(revs, commit) > revs->min_age)
3962 return commit_ignore;
3963 if (revs->max_age_as_filter != -1 &&
3964 comparison_date(revs, commit) < revs->max_age_as_filter)
3965 return commit_ignore;
3966 if (revs->min_parents || (revs->max_parents >= 0)) {
3967 int n = commit_list_count(commit->parents);
3968 if ((n < revs->min_parents) ||
3969 ((revs->max_parents >= 0) && (n > revs->max_parents)))
3970 return commit_ignore;
3972 if (!commit_match(commit, revs))
3973 return commit_ignore;
3974 if (revs->prune && revs->dense) {
3975 /* Commit without changes? */
3976 if (commit->object.flags & TREESAME) {
3977 int n;
3978 struct commit_list *p;
3979 /* drop merges unless we want parenthood */
3980 if (!want_ancestry(revs))
3981 return commit_ignore;
3983 if (revs->show_pulls && (commit->object.flags & PULL_MERGE))
3984 return commit_show;
3987 * If we want ancestry, then need to keep any merges
3988 * between relevant commits to tie together topology.
3989 * For consistency with TREESAME and simplification
3990 * use "relevant" here rather than just INTERESTING,
3991 * to treat bottom commit(s) as part of the topology.
3993 for (n = 0, p = commit->parents; p; p = p->next)
3994 if (relevant_commit(p->item))
3995 if (++n >= 2)
3996 return commit_show;
3997 return commit_ignore;
4000 return commit_show;
4003 define_commit_slab(saved_parents, struct commit_list *);
4005 #define EMPTY_PARENT_LIST ((struct commit_list *)-1)
4008 * You may only call save_parents() once per commit (this is checked
4009 * for non-root commits).
4011 static void save_parents(struct rev_info *revs, struct commit *commit)
4013 struct commit_list **pp;
4015 if (!revs->saved_parents_slab) {
4016 revs->saved_parents_slab = xmalloc(sizeof(struct saved_parents));
4017 init_saved_parents(revs->saved_parents_slab);
4020 pp = saved_parents_at(revs->saved_parents_slab, commit);
4023 * When walking with reflogs, we may visit the same commit
4024 * several times: once for each appearance in the reflog.
4026 * In this case, save_parents() will be called multiple times.
4027 * We want to keep only the first set of parents. We need to
4028 * store a sentinel value for an empty (i.e., NULL) parent
4029 * list to distinguish it from a not-yet-saved list, however.
4031 if (*pp)
4032 return;
4033 if (commit->parents)
4034 *pp = copy_commit_list(commit->parents);
4035 else
4036 *pp = EMPTY_PARENT_LIST;
4039 static void free_saved_parents(struct rev_info *revs)
4041 if (revs->saved_parents_slab)
4042 clear_saved_parents(revs->saved_parents_slab);
4045 struct commit_list *get_saved_parents(struct rev_info *revs, const struct commit *commit)
4047 struct commit_list *parents;
4049 if (!revs->saved_parents_slab)
4050 return commit->parents;
4052 parents = *saved_parents_at(revs->saved_parents_slab, commit);
4053 if (parents == EMPTY_PARENT_LIST)
4054 return NULL;
4055 return parents;
4058 enum commit_action simplify_commit(struct rev_info *revs, struct commit *commit)
4060 enum commit_action action = get_commit_action(revs, commit);
4062 if (action == commit_show &&
4063 revs->prune && revs->dense && want_ancestry(revs)) {
4065 * --full-diff on simplified parents is no good: it
4066 * will show spurious changes from the commits that
4067 * were elided. So we save the parents on the side
4068 * when --full-diff is in effect.
4070 if (revs->full_diff)
4071 save_parents(revs, commit);
4072 if (rewrite_parents(revs, commit, rewrite_one) < 0)
4073 return commit_error;
4075 return action;
4078 static void track_linear(struct rev_info *revs, struct commit *commit)
4080 if (revs->track_first_time) {
4081 revs->linear = 1;
4082 revs->track_first_time = 0;
4083 } else {
4084 struct commit_list *p;
4085 for (p = revs->previous_parents; p; p = p->next)
4086 if (p->item == NULL || /* first commit */
4087 oideq(&p->item->object.oid, &commit->object.oid))
4088 break;
4089 revs->linear = p != NULL;
4091 if (revs->reverse) {
4092 if (revs->linear)
4093 commit->object.flags |= TRACK_LINEAR;
4095 free_commit_list(revs->previous_parents);
4096 revs->previous_parents = copy_commit_list(commit->parents);
4099 static struct commit *get_revision_1(struct rev_info *revs)
4101 while (1) {
4102 struct commit *commit;
4104 if (revs->reflog_info)
4105 commit = next_reflog_entry(revs->reflog_info);
4106 else if (revs->topo_walk_info)
4107 commit = next_topo_commit(revs);
4108 else
4109 commit = pop_commit(&revs->commits);
4111 if (!commit)
4112 return NULL;
4114 if (revs->reflog_info)
4115 commit->object.flags &= ~(ADDED | SEEN | SHOWN);
4118 * If we haven't done the list limiting, we need to look at
4119 * the parents here. We also need to do the date-based limiting
4120 * that we'd otherwise have done in limit_list().
4122 if (!revs->limited) {
4123 if (revs->max_age != -1 &&
4124 comparison_date(revs, commit) < revs->max_age)
4125 continue;
4127 if (revs->reflog_info)
4128 try_to_simplify_commit(revs, commit);
4129 else if (revs->topo_walk_info)
4130 expand_topo_walk(revs, commit);
4131 else if (process_parents(revs, commit, &revs->commits, NULL) < 0) {
4132 if (!revs->ignore_missing_links)
4133 die("Failed to traverse parents of commit %s",
4134 oid_to_hex(&commit->object.oid));
4138 switch (simplify_commit(revs, commit)) {
4139 case commit_ignore:
4140 continue;
4141 case commit_error:
4142 die("Failed to simplify parents of commit %s",
4143 oid_to_hex(&commit->object.oid));
4144 default:
4145 if (revs->track_linear)
4146 track_linear(revs, commit);
4147 return commit;
4153 * Return true for entries that have not yet been shown. (This is an
4154 * object_array_each_func_t.)
4156 static int entry_unshown(struct object_array_entry *entry, void *cb_data_unused)
4158 return !(entry->item->flags & SHOWN);
4162 * If array is on the verge of a realloc, garbage-collect any entries
4163 * that have already been shown to try to free up some space.
4165 static void gc_boundary(struct object_array *array)
4167 if (array->nr == array->alloc)
4168 object_array_filter(array, entry_unshown, NULL);
4171 static void create_boundary_commit_list(struct rev_info *revs)
4173 unsigned i;
4174 struct commit *c;
4175 struct object_array *array = &revs->boundary_commits;
4176 struct object_array_entry *objects = array->objects;
4179 * If revs->commits is non-NULL at this point, an error occurred in
4180 * get_revision_1(). Ignore the error and continue printing the
4181 * boundary commits anyway. (This is what the code has always
4182 * done.)
4184 free_commit_list(revs->commits);
4185 revs->commits = NULL;
4188 * Put all of the actual boundary commits from revs->boundary_commits
4189 * into revs->commits
4191 for (i = 0; i < array->nr; i++) {
4192 c = (struct commit *)(objects[i].item);
4193 if (!c)
4194 continue;
4195 if (!(c->object.flags & CHILD_SHOWN))
4196 continue;
4197 if (c->object.flags & (SHOWN | BOUNDARY))
4198 continue;
4199 c->object.flags |= BOUNDARY;
4200 commit_list_insert(c, &revs->commits);
4204 * If revs->topo_order is set, sort the boundary commits
4205 * in topological order
4207 sort_in_topological_order(&revs->commits, revs->sort_order);
4210 static struct commit *get_revision_internal(struct rev_info *revs)
4212 struct commit *c = NULL;
4213 struct commit_list *l;
4215 if (revs->boundary == 2) {
4217 * All of the normal commits have already been returned,
4218 * and we are now returning boundary commits.
4219 * create_boundary_commit_list() has populated
4220 * revs->commits with the remaining commits to return.
4222 c = pop_commit(&revs->commits);
4223 if (c)
4224 c->object.flags |= SHOWN;
4225 return c;
4229 * If our max_count counter has reached zero, then we are done. We
4230 * don't simply return NULL because we still might need to show
4231 * boundary commits. But we want to avoid calling get_revision_1, which
4232 * might do a considerable amount of work finding the next commit only
4233 * for us to throw it away.
4235 * If it is non-zero, then either we don't have a max_count at all
4236 * (-1), or it is still counting, in which case we decrement.
4238 if (revs->max_count) {
4239 c = get_revision_1(revs);
4240 if (c) {
4241 while (revs->skip_count > 0) {
4242 revs->skip_count--;
4243 c = get_revision_1(revs);
4244 if (!c)
4245 break;
4249 if (revs->max_count > 0)
4250 revs->max_count--;
4253 if (c)
4254 c->object.flags |= SHOWN;
4256 if (!revs->boundary)
4257 return c;
4259 if (!c) {
4261 * get_revision_1() runs out the commits, and
4262 * we are done computing the boundaries.
4263 * switch to boundary commits output mode.
4265 revs->boundary = 2;
4268 * Update revs->commits to contain the list of
4269 * boundary commits.
4271 create_boundary_commit_list(revs);
4273 return get_revision_internal(revs);
4277 * boundary commits are the commits that are parents of the
4278 * ones we got from get_revision_1() but they themselves are
4279 * not returned from get_revision_1(). Before returning
4280 * 'c', we need to mark its parents that they could be boundaries.
4283 for (l = c->parents; l; l = l->next) {
4284 struct object *p;
4285 p = &(l->item->object);
4286 if (p->flags & (CHILD_SHOWN | SHOWN))
4287 continue;
4288 p->flags |= CHILD_SHOWN;
4289 gc_boundary(&revs->boundary_commits);
4290 add_object_array(p, NULL, &revs->boundary_commits);
4293 return c;
4296 struct commit *get_revision(struct rev_info *revs)
4298 struct commit *c;
4299 struct commit_list *reversed;
4301 if (revs->reverse) {
4302 reversed = NULL;
4303 while ((c = get_revision_internal(revs)))
4304 commit_list_insert(c, &reversed);
4305 revs->commits = reversed;
4306 revs->reverse = 0;
4307 revs->reverse_output_stage = 1;
4310 if (revs->reverse_output_stage) {
4311 c = pop_commit(&revs->commits);
4312 if (revs->track_linear)
4313 revs->linear = !!(c && c->object.flags & TRACK_LINEAR);
4314 return c;
4317 c = get_revision_internal(revs);
4318 if (c && revs->graph)
4319 graph_update(revs->graph, c);
4320 if (!c) {
4321 free_saved_parents(revs);
4322 free_commit_list(revs->previous_parents);
4323 revs->previous_parents = NULL;
4325 return c;
4328 const char *get_revision_mark(const struct rev_info *revs, const struct commit *commit)
4330 if (commit->object.flags & BOUNDARY)
4331 return "-";
4332 else if (commit->object.flags & UNINTERESTING)
4333 return "^";
4334 else if (commit->object.flags & PATCHSAME)
4335 return "=";
4336 else if (!revs || revs->left_right) {
4337 if (commit->object.flags & SYMMETRIC_LEFT)
4338 return "<";
4339 else
4340 return ">";
4341 } else if (revs->graph)
4342 return "*";
4343 else if (revs->cherry_mark)
4344 return "+";
4345 return "";
4348 void put_revision_mark(const struct rev_info *revs, const struct commit *commit)
4350 const char *mark = get_revision_mark(revs, commit);
4351 if (!strlen(mark))
4352 return;
4353 fputs(mark, stdout);
4354 putchar(' ');