Sync with 2.39.4
[git.git] / revision.c
blob21f5f572c22ec7054da6bc20fef26ee2a59a19d5
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,
604 unsigned mode UNUSED,
605 const struct object_id *oid UNUSED,
606 int oid_valid UNUSED,
607 const char *fullpath UNUSED,
608 unsigned dirty_submodule UNUSED)
610 int diff = addremove == '+' ? REV_TREE_NEW : REV_TREE_OLD;
611 struct rev_info *revs = options->change_fn_data;
613 tree_difference |= diff;
614 if (!revs->remove_empty_trees || tree_difference != REV_TREE_NEW)
615 options->flags.has_changes = 1;
618 static void file_change(struct diff_options *options,
619 unsigned old_mode UNUSED,
620 unsigned new_mode UNUSED,
621 const struct object_id *old_oid UNUSED,
622 const struct object_id *new_oid UNUSED,
623 int old_oid_valid UNUSED,
624 int new_oid_valid UNUSED,
625 const char *fullpath UNUSED,
626 unsigned old_dirty_submodule UNUSED,
627 unsigned new_dirty_submodule UNUSED)
629 tree_difference = REV_TREE_DIFFERENT;
630 options->flags.has_changes = 1;
633 static int bloom_filter_atexit_registered;
634 static unsigned int count_bloom_filter_maybe;
635 static unsigned int count_bloom_filter_definitely_not;
636 static unsigned int count_bloom_filter_false_positive;
637 static unsigned int count_bloom_filter_not_present;
639 static void trace2_bloom_filter_statistics_atexit(void)
641 struct json_writer jw = JSON_WRITER_INIT;
643 jw_object_begin(&jw, 0);
644 jw_object_intmax(&jw, "filter_not_present", count_bloom_filter_not_present);
645 jw_object_intmax(&jw, "maybe", count_bloom_filter_maybe);
646 jw_object_intmax(&jw, "definitely_not", count_bloom_filter_definitely_not);
647 jw_object_intmax(&jw, "false_positive", count_bloom_filter_false_positive);
648 jw_end(&jw);
650 trace2_data_json("bloom", the_repository, "statistics", &jw);
652 jw_release(&jw);
655 static int forbid_bloom_filters(struct pathspec *spec)
657 if (spec->has_wildcard)
658 return 1;
659 if (spec->nr > 1)
660 return 1;
661 if (spec->magic & ~PATHSPEC_LITERAL)
662 return 1;
663 if (spec->nr && (spec->items[0].magic & ~PATHSPEC_LITERAL))
664 return 1;
666 return 0;
669 static void prepare_to_use_bloom_filter(struct rev_info *revs)
671 struct pathspec_item *pi;
672 char *path_alloc = NULL;
673 const char *path, *p;
674 size_t len;
675 int path_component_nr = 1;
677 if (!revs->commits)
678 return;
680 if (forbid_bloom_filters(&revs->prune_data))
681 return;
683 repo_parse_commit(revs->repo, revs->commits->item);
685 revs->bloom_filter_settings = get_bloom_filter_settings(revs->repo);
686 if (!revs->bloom_filter_settings)
687 return;
689 if (!revs->pruning.pathspec.nr)
690 return;
692 pi = &revs->pruning.pathspec.items[0];
694 /* remove single trailing slash from path, if needed */
695 if (pi->len > 0 && pi->match[pi->len - 1] == '/') {
696 path_alloc = xmemdupz(pi->match, pi->len - 1);
697 path = path_alloc;
698 } else
699 path = pi->match;
701 len = strlen(path);
702 if (!len) {
703 revs->bloom_filter_settings = NULL;
704 free(path_alloc);
705 return;
708 p = path;
709 while (*p) {
711 * At this point, the path is normalized to use Unix-style
712 * path separators. This is required due to how the
713 * changed-path Bloom filters store the paths.
715 if (*p == '/')
716 path_component_nr++;
717 p++;
720 revs->bloom_keys_nr = path_component_nr;
721 ALLOC_ARRAY(revs->bloom_keys, revs->bloom_keys_nr);
723 fill_bloom_key(path, len, &revs->bloom_keys[0],
724 revs->bloom_filter_settings);
725 path_component_nr = 1;
727 p = path + len - 1;
728 while (p > path) {
729 if (*p == '/')
730 fill_bloom_key(path, p - path,
731 &revs->bloom_keys[path_component_nr++],
732 revs->bloom_filter_settings);
733 p--;
736 if (trace2_is_enabled() && !bloom_filter_atexit_registered) {
737 atexit(trace2_bloom_filter_statistics_atexit);
738 bloom_filter_atexit_registered = 1;
741 free(path_alloc);
744 static int check_maybe_different_in_bloom_filter(struct rev_info *revs,
745 struct commit *commit)
747 struct bloom_filter *filter;
748 int result = 1, j;
750 if (!revs->repo->objects->commit_graph)
751 return -1;
753 if (commit_graph_generation(commit) == GENERATION_NUMBER_INFINITY)
754 return -1;
756 filter = get_bloom_filter(revs->repo, commit);
758 if (!filter) {
759 count_bloom_filter_not_present++;
760 return -1;
763 for (j = 0; result && j < revs->bloom_keys_nr; j++) {
764 result = bloom_filter_contains(filter,
765 &revs->bloom_keys[j],
766 revs->bloom_filter_settings);
769 if (result)
770 count_bloom_filter_maybe++;
771 else
772 count_bloom_filter_definitely_not++;
774 return result;
777 static int rev_compare_tree(struct rev_info *revs,
778 struct commit *parent, struct commit *commit, int nth_parent)
780 struct tree *t1 = get_commit_tree(parent);
781 struct tree *t2 = get_commit_tree(commit);
782 int bloom_ret = 1;
784 if (!t1)
785 return REV_TREE_NEW;
786 if (!t2)
787 return REV_TREE_OLD;
789 if (revs->simplify_by_decoration) {
791 * If we are simplifying by decoration, then the commit
792 * is worth showing if it has a tag pointing at it.
794 if (get_name_decoration(&commit->object))
795 return REV_TREE_DIFFERENT;
797 * A commit that is not pointed by a tag is uninteresting
798 * if we are not limited by path. This means that you will
799 * see the usual "commits that touch the paths" plus any
800 * tagged commit by specifying both --simplify-by-decoration
801 * and pathspec.
803 if (!revs->prune_data.nr)
804 return REV_TREE_SAME;
807 if (revs->bloom_keys_nr && !nth_parent) {
808 bloom_ret = check_maybe_different_in_bloom_filter(revs, commit);
810 if (bloom_ret == 0)
811 return REV_TREE_SAME;
814 tree_difference = REV_TREE_SAME;
815 revs->pruning.flags.has_changes = 0;
816 diff_tree_oid(&t1->object.oid, &t2->object.oid, "", &revs->pruning);
818 if (!nth_parent)
819 if (bloom_ret == 1 && tree_difference == REV_TREE_SAME)
820 count_bloom_filter_false_positive++;
822 return tree_difference;
825 static int rev_same_tree_as_empty(struct rev_info *revs, struct commit *commit)
827 struct tree *t1 = get_commit_tree(commit);
829 if (!t1)
830 return 0;
832 tree_difference = REV_TREE_SAME;
833 revs->pruning.flags.has_changes = 0;
834 diff_tree_oid(NULL, &t1->object.oid, "", &revs->pruning);
836 return tree_difference == REV_TREE_SAME;
839 struct treesame_state {
840 unsigned int nparents;
841 unsigned char treesame[FLEX_ARRAY];
844 static struct treesame_state *initialise_treesame(struct rev_info *revs, struct commit *commit)
846 unsigned n = commit_list_count(commit->parents);
847 struct treesame_state *st = xcalloc(1, st_add(sizeof(*st), n));
848 st->nparents = n;
849 add_decoration(&revs->treesame, &commit->object, st);
850 return st;
854 * Must be called immediately after removing the nth_parent from a commit's
855 * parent list, if we are maintaining the per-parent treesame[] decoration.
856 * This does not recalculate the master TREESAME flag - update_treesame()
857 * should be called to update it after a sequence of treesame[] modifications
858 * that may have affected it.
860 static int compact_treesame(struct rev_info *revs, struct commit *commit, unsigned nth_parent)
862 struct treesame_state *st;
863 int old_same;
865 if (!commit->parents) {
867 * Have just removed the only parent from a non-merge.
868 * Different handling, as we lack decoration.
870 if (nth_parent != 0)
871 die("compact_treesame %u", nth_parent);
872 old_same = !!(commit->object.flags & TREESAME);
873 if (rev_same_tree_as_empty(revs, commit))
874 commit->object.flags |= TREESAME;
875 else
876 commit->object.flags &= ~TREESAME;
877 return old_same;
880 st = lookup_decoration(&revs->treesame, &commit->object);
881 if (!st || nth_parent >= st->nparents)
882 die("compact_treesame %u", nth_parent);
884 old_same = st->treesame[nth_parent];
885 memmove(st->treesame + nth_parent,
886 st->treesame + nth_parent + 1,
887 st->nparents - nth_parent - 1);
890 * If we've just become a non-merge commit, update TREESAME
891 * immediately, and remove the no-longer-needed decoration.
892 * If still a merge, defer update until update_treesame().
894 if (--st->nparents == 1) {
895 if (commit->parents->next)
896 die("compact_treesame parents mismatch");
897 if (st->treesame[0] && revs->dense)
898 commit->object.flags |= TREESAME;
899 else
900 commit->object.flags &= ~TREESAME;
901 free(add_decoration(&revs->treesame, &commit->object, NULL));
904 return old_same;
907 static unsigned update_treesame(struct rev_info *revs, struct commit *commit)
909 if (commit->parents && commit->parents->next) {
910 unsigned n;
911 struct treesame_state *st;
912 struct commit_list *p;
913 unsigned relevant_parents;
914 unsigned relevant_change, irrelevant_change;
916 st = lookup_decoration(&revs->treesame, &commit->object);
917 if (!st)
918 die("update_treesame %s", oid_to_hex(&commit->object.oid));
919 relevant_parents = 0;
920 relevant_change = irrelevant_change = 0;
921 for (p = commit->parents, n = 0; p; n++, p = p->next) {
922 if (relevant_commit(p->item)) {
923 relevant_change |= !st->treesame[n];
924 relevant_parents++;
925 } else
926 irrelevant_change |= !st->treesame[n];
928 if (relevant_parents ? relevant_change : irrelevant_change)
929 commit->object.flags &= ~TREESAME;
930 else
931 commit->object.flags |= TREESAME;
934 return commit->object.flags & TREESAME;
937 static inline int limiting_can_increase_treesame(const struct rev_info *revs)
940 * TREESAME is irrelevant unless prune && dense;
941 * if simplify_history is set, we can't have a mixture of TREESAME and
942 * !TREESAME INTERESTING parents (and we don't have treesame[]
943 * decoration anyway);
944 * if first_parent_only is set, then the TREESAME flag is locked
945 * against the first parent (and again we lack treesame[] decoration).
947 return revs->prune && revs->dense &&
948 !revs->simplify_history &&
949 !revs->first_parent_only;
952 static void try_to_simplify_commit(struct rev_info *revs, struct commit *commit)
954 struct commit_list **pp, *parent;
955 struct treesame_state *ts = NULL;
956 int relevant_change = 0, irrelevant_change = 0;
957 int relevant_parents, nth_parent;
960 * If we don't do pruning, everything is interesting
962 if (!revs->prune)
963 return;
965 if (!get_commit_tree(commit))
966 return;
968 if (!commit->parents) {
969 if (rev_same_tree_as_empty(revs, commit))
970 commit->object.flags |= TREESAME;
971 return;
975 * Normal non-merge commit? If we don't want to make the
976 * history dense, we consider it always to be a change..
978 if (!revs->dense && !commit->parents->next)
979 return;
981 for (pp = &commit->parents, nth_parent = 0, relevant_parents = 0;
982 (parent = *pp) != NULL;
983 pp = &parent->next, nth_parent++) {
984 struct commit *p = parent->item;
985 if (relevant_commit(p))
986 relevant_parents++;
988 if (nth_parent == 1) {
990 * This our second loop iteration - so we now know
991 * we're dealing with a merge.
993 * Do not compare with later parents when we care only about
994 * the first parent chain, in order to avoid derailing the
995 * traversal to follow a side branch that brought everything
996 * in the path we are limited to by the pathspec.
998 if (revs->first_parent_only)
999 break;
1001 * If this will remain a potentially-simplifiable
1002 * merge, remember per-parent treesame if needed.
1003 * Initialise the array with the comparison from our
1004 * first iteration.
1006 if (revs->treesame.name &&
1007 !revs->simplify_history &&
1008 !(commit->object.flags & UNINTERESTING)) {
1009 ts = initialise_treesame(revs, commit);
1010 if (!(irrelevant_change || relevant_change))
1011 ts->treesame[0] = 1;
1014 if (repo_parse_commit(revs->repo, p) < 0)
1015 die("cannot simplify commit %s (because of %s)",
1016 oid_to_hex(&commit->object.oid),
1017 oid_to_hex(&p->object.oid));
1018 switch (rev_compare_tree(revs, p, commit, nth_parent)) {
1019 case REV_TREE_SAME:
1020 if (!revs->simplify_history || !relevant_commit(p)) {
1021 /* Even if a merge with an uninteresting
1022 * side branch brought the entire change
1023 * we are interested in, we do not want
1024 * to lose the other branches of this
1025 * merge, so we just keep going.
1027 if (ts)
1028 ts->treesame[nth_parent] = 1;
1029 continue;
1031 parent->next = NULL;
1032 commit->parents = parent;
1035 * A merge commit is a "diversion" if it is not
1036 * TREESAME to its first parent but is TREESAME
1037 * to a later parent. In the simplified history,
1038 * we "divert" the history walk to the later
1039 * parent. These commits are shown when "show_pulls"
1040 * is enabled, so do not mark the object as
1041 * TREESAME here.
1043 if (!revs->show_pulls || !nth_parent)
1044 commit->object.flags |= TREESAME;
1046 return;
1048 case REV_TREE_NEW:
1049 if (revs->remove_empty_trees &&
1050 rev_same_tree_as_empty(revs, p)) {
1051 /* We are adding all the specified
1052 * paths from this parent, so the
1053 * history beyond this parent is not
1054 * interesting. Remove its parents
1055 * (they are grandparents for us).
1056 * IOW, we pretend this parent is a
1057 * "root" commit.
1059 if (repo_parse_commit(revs->repo, p) < 0)
1060 die("cannot simplify commit %s (invalid %s)",
1061 oid_to_hex(&commit->object.oid),
1062 oid_to_hex(&p->object.oid));
1063 p->parents = NULL;
1065 /* fallthrough */
1066 case REV_TREE_OLD:
1067 case REV_TREE_DIFFERENT:
1068 if (relevant_commit(p))
1069 relevant_change = 1;
1070 else
1071 irrelevant_change = 1;
1073 if (!nth_parent)
1074 commit->object.flags |= PULL_MERGE;
1076 continue;
1078 die("bad tree compare for commit %s", oid_to_hex(&commit->object.oid));
1082 * TREESAME is straightforward for single-parent commits. For merge
1083 * commits, it is most useful to define it so that "irrelevant"
1084 * parents cannot make us !TREESAME - if we have any relevant
1085 * parents, then we only consider TREESAMEness with respect to them,
1086 * allowing irrelevant merges from uninteresting branches to be
1087 * simplified away. Only if we have only irrelevant parents do we
1088 * base TREESAME on them. Note that this logic is replicated in
1089 * update_treesame, which should be kept in sync.
1091 if (relevant_parents ? !relevant_change : !irrelevant_change)
1092 commit->object.flags |= TREESAME;
1095 static int process_parents(struct rev_info *revs, struct commit *commit,
1096 struct commit_list **list, struct prio_queue *queue)
1098 struct commit_list *parent = commit->parents;
1099 unsigned pass_flags;
1101 if (commit->object.flags & ADDED)
1102 return 0;
1103 commit->object.flags |= ADDED;
1105 if (revs->include_check &&
1106 !revs->include_check(commit, revs->include_check_data))
1107 return 0;
1110 * If the commit is uninteresting, don't try to
1111 * prune parents - we want the maximal uninteresting
1112 * set.
1114 * Normally we haven't parsed the parent
1115 * yet, so we won't have a parent of a parent
1116 * here. However, it may turn out that we've
1117 * reached this commit some other way (where it
1118 * wasn't uninteresting), in which case we need
1119 * to mark its parents recursively too..
1121 if (commit->object.flags & UNINTERESTING) {
1122 while (parent) {
1123 struct commit *p = parent->item;
1124 parent = parent->next;
1125 if (p)
1126 p->object.flags |= UNINTERESTING;
1127 if (repo_parse_commit_gently(revs->repo, p, 1) < 0)
1128 continue;
1129 if (p->parents)
1130 mark_parents_uninteresting(revs, p);
1131 if (p->object.flags & SEEN)
1132 continue;
1133 p->object.flags |= (SEEN | NOT_USER_GIVEN);
1134 if (list)
1135 commit_list_insert_by_date(p, list);
1136 if (queue)
1137 prio_queue_put(queue, p);
1138 if (revs->exclude_first_parent_only)
1139 break;
1141 return 0;
1145 * Ok, the commit wasn't uninteresting. Try to
1146 * simplify the commit history and find the parent
1147 * that has no differences in the path set if one exists.
1149 try_to_simplify_commit(revs, commit);
1151 if (revs->no_walk)
1152 return 0;
1154 pass_flags = (commit->object.flags & (SYMMETRIC_LEFT | ANCESTRY_PATH));
1156 for (parent = commit->parents; parent; parent = parent->next) {
1157 struct commit *p = parent->item;
1158 int gently = revs->ignore_missing_links ||
1159 revs->exclude_promisor_objects;
1160 if (repo_parse_commit_gently(revs->repo, p, gently) < 0) {
1161 if (revs->exclude_promisor_objects &&
1162 is_promisor_object(&p->object.oid)) {
1163 if (revs->first_parent_only)
1164 break;
1165 continue;
1167 return -1;
1169 if (revs->sources) {
1170 char **slot = revision_sources_at(revs->sources, p);
1172 if (!*slot)
1173 *slot = *revision_sources_at(revs->sources, commit);
1175 p->object.flags |= pass_flags;
1176 if (!(p->object.flags & SEEN)) {
1177 p->object.flags |= (SEEN | NOT_USER_GIVEN);
1178 if (list)
1179 commit_list_insert_by_date(p, list);
1180 if (queue)
1181 prio_queue_put(queue, p);
1183 if (revs->first_parent_only)
1184 break;
1186 return 0;
1189 static void cherry_pick_list(struct commit_list *list, struct rev_info *revs)
1191 struct commit_list *p;
1192 int left_count = 0, right_count = 0;
1193 int left_first;
1194 struct patch_ids ids;
1195 unsigned cherry_flag;
1197 /* First count the commits on the left and on the right */
1198 for (p = list; p; p = p->next) {
1199 struct commit *commit = p->item;
1200 unsigned flags = commit->object.flags;
1201 if (flags & BOUNDARY)
1203 else if (flags & SYMMETRIC_LEFT)
1204 left_count++;
1205 else
1206 right_count++;
1209 if (!left_count || !right_count)
1210 return;
1212 left_first = left_count < right_count;
1213 init_patch_ids(revs->repo, &ids);
1214 ids.diffopts.pathspec = revs->diffopt.pathspec;
1216 /* Compute patch-ids for one side */
1217 for (p = list; p; p = p->next) {
1218 struct commit *commit = p->item;
1219 unsigned flags = commit->object.flags;
1221 if (flags & BOUNDARY)
1222 continue;
1224 * If we have fewer left, left_first is set and we omit
1225 * commits on the right branch in this loop. If we have
1226 * fewer right, we skip the left ones.
1228 if (left_first != !!(flags & SYMMETRIC_LEFT))
1229 continue;
1230 add_commit_patch_id(commit, &ids);
1233 /* either cherry_mark or cherry_pick are true */
1234 cherry_flag = revs->cherry_mark ? PATCHSAME : SHOWN;
1236 /* Check the other side */
1237 for (p = list; p; p = p->next) {
1238 struct commit *commit = p->item;
1239 struct patch_id *id;
1240 unsigned flags = commit->object.flags;
1242 if (flags & BOUNDARY)
1243 continue;
1245 * If we have fewer left, left_first is set and we omit
1246 * commits on the left branch in this loop.
1248 if (left_first == !!(flags & SYMMETRIC_LEFT))
1249 continue;
1252 * Have we seen the same patch id?
1254 id = patch_id_iter_first(commit, &ids);
1255 if (!id)
1256 continue;
1258 commit->object.flags |= cherry_flag;
1259 do {
1260 id->commit->object.flags |= cherry_flag;
1261 } while ((id = patch_id_iter_next(id, &ids)));
1264 free_patch_ids(&ids);
1267 /* How many extra uninteresting commits we want to see.. */
1268 #define SLOP 5
1270 static int still_interesting(struct commit_list *src, timestamp_t date, int slop,
1271 struct commit **interesting_cache)
1274 * No source list at all? We're definitely done..
1276 if (!src)
1277 return 0;
1280 * Does the destination list contain entries with a date
1281 * before the source list? Definitely _not_ done.
1283 if (date <= src->item->date)
1284 return SLOP;
1287 * Does the source list still have interesting commits in
1288 * it? Definitely not done..
1290 if (!everybody_uninteresting(src, interesting_cache))
1291 return SLOP;
1293 /* Ok, we're closing in.. */
1294 return slop-1;
1298 * "rev-list --ancestry-path=C_0 [--ancestry-path=C_1 ...] A..B"
1299 * computes commits that are ancestors of B but not ancestors of A but
1300 * further limits the result to those that have any of C in their
1301 * ancestry path (i.e. are either ancestors of any of C, descendants
1302 * of any of C, or are any of C). If --ancestry-path is specified with
1303 * no commit, we use all bottom commits for C.
1305 * Before this function is called, ancestors of C will have already
1306 * been marked with ANCESTRY_PATH previously.
1308 * This takes the list of bottom commits and the result of "A..B"
1309 * without --ancestry-path, and limits the latter further to the ones
1310 * that have any of C in their ancestry path. Since the ancestors of C
1311 * have already been marked (a prerequisite of this function), we just
1312 * need to mark the descendants, then exclude any commit that does not
1313 * have any of these marks.
1315 static void limit_to_ancestry(struct commit_list *bottoms, struct commit_list *list)
1317 struct commit_list *p;
1318 struct commit_list *rlist = NULL;
1319 int made_progress;
1322 * Reverse the list so that it will be likely that we would
1323 * process parents before children.
1325 for (p = list; p; p = p->next)
1326 commit_list_insert(p->item, &rlist);
1328 for (p = bottoms; p; p = p->next)
1329 p->item->object.flags |= TMP_MARK;
1332 * Mark the ones that can reach bottom commits in "list",
1333 * in a bottom-up fashion.
1335 do {
1336 made_progress = 0;
1337 for (p = rlist; p; p = p->next) {
1338 struct commit *c = p->item;
1339 struct commit_list *parents;
1340 if (c->object.flags & (TMP_MARK | UNINTERESTING))
1341 continue;
1342 for (parents = c->parents;
1343 parents;
1344 parents = parents->next) {
1345 if (!(parents->item->object.flags & TMP_MARK))
1346 continue;
1347 c->object.flags |= TMP_MARK;
1348 made_progress = 1;
1349 break;
1352 } while (made_progress);
1355 * NEEDSWORK: decide if we want to remove parents that are
1356 * not marked with TMP_MARK from commit->parents for commits
1357 * in the resulting list. We may not want to do that, though.
1361 * The ones that are not marked with either TMP_MARK or
1362 * ANCESTRY_PATH are uninteresting
1364 for (p = list; p; p = p->next) {
1365 struct commit *c = p->item;
1366 if (c->object.flags & (TMP_MARK | ANCESTRY_PATH))
1367 continue;
1368 c->object.flags |= UNINTERESTING;
1371 /* We are done with TMP_MARK and ANCESTRY_PATH */
1372 for (p = list; p; p = p->next)
1373 p->item->object.flags &= ~(TMP_MARK | ANCESTRY_PATH);
1374 for (p = bottoms; p; p = p->next)
1375 p->item->object.flags &= ~(TMP_MARK | ANCESTRY_PATH);
1376 free_commit_list(rlist);
1380 * Before walking the history, add the set of "negative" refs the
1381 * caller has asked to exclude to the bottom list.
1383 * This is used to compute "rev-list --ancestry-path A..B", as we need
1384 * to filter the result of "A..B" further to the ones that can actually
1385 * reach A.
1387 static void collect_bottom_commits(struct commit_list *list,
1388 struct commit_list **bottom)
1390 struct commit_list *elem;
1391 for (elem = list; elem; elem = elem->next)
1392 if (elem->item->object.flags & BOTTOM)
1393 commit_list_insert(elem->item, bottom);
1396 /* Assumes either left_only or right_only is set */
1397 static void limit_left_right(struct commit_list *list, struct rev_info *revs)
1399 struct commit_list *p;
1401 for (p = list; p; p = p->next) {
1402 struct commit *commit = p->item;
1404 if (revs->right_only) {
1405 if (commit->object.flags & SYMMETRIC_LEFT)
1406 commit->object.flags |= SHOWN;
1407 } else /* revs->left_only is set */
1408 if (!(commit->object.flags & SYMMETRIC_LEFT))
1409 commit->object.flags |= SHOWN;
1413 static int limit_list(struct rev_info *revs)
1415 int slop = SLOP;
1416 timestamp_t date = TIME_MAX;
1417 struct commit_list *original_list = revs->commits;
1418 struct commit_list *newlist = NULL;
1419 struct commit_list **p = &newlist;
1420 struct commit *interesting_cache = NULL;
1422 if (revs->ancestry_path_implicit_bottoms) {
1423 collect_bottom_commits(original_list,
1424 &revs->ancestry_path_bottoms);
1425 if (!revs->ancestry_path_bottoms)
1426 die("--ancestry-path given but there are no bottom commits");
1429 while (original_list) {
1430 struct commit *commit = pop_commit(&original_list);
1431 struct object *obj = &commit->object;
1432 show_early_output_fn_t show;
1434 if (commit == interesting_cache)
1435 interesting_cache = NULL;
1437 if (revs->max_age != -1 && (commit->date < revs->max_age))
1438 obj->flags |= UNINTERESTING;
1439 if (process_parents(revs, commit, &original_list, NULL) < 0)
1440 return -1;
1441 if (obj->flags & UNINTERESTING) {
1442 mark_parents_uninteresting(revs, commit);
1443 slop = still_interesting(original_list, date, slop, &interesting_cache);
1444 if (slop)
1445 continue;
1446 break;
1448 if (revs->min_age != -1 && (commit->date > revs->min_age) &&
1449 !revs->line_level_traverse)
1450 continue;
1451 if (revs->max_age_as_filter != -1 &&
1452 (commit->date < revs->max_age_as_filter) && !revs->line_level_traverse)
1453 continue;
1454 date = commit->date;
1455 p = &commit_list_insert(commit, p)->next;
1457 show = show_early_output;
1458 if (!show)
1459 continue;
1461 show(revs, newlist);
1462 show_early_output = NULL;
1464 if (revs->cherry_pick || revs->cherry_mark)
1465 cherry_pick_list(newlist, revs);
1467 if (revs->left_only || revs->right_only)
1468 limit_left_right(newlist, revs);
1470 if (revs->ancestry_path)
1471 limit_to_ancestry(revs->ancestry_path_bottoms, newlist);
1474 * Check if any commits have become TREESAME by some of their parents
1475 * becoming UNINTERESTING.
1477 if (limiting_can_increase_treesame(revs)) {
1478 struct commit_list *list = NULL;
1479 for (list = newlist; list; list = list->next) {
1480 struct commit *c = list->item;
1481 if (c->object.flags & (UNINTERESTING | TREESAME))
1482 continue;
1483 update_treesame(revs, c);
1487 free_commit_list(original_list);
1488 revs->commits = newlist;
1489 return 0;
1493 * Add an entry to refs->cmdline with the specified information.
1494 * *name is copied.
1496 static void add_rev_cmdline(struct rev_info *revs,
1497 struct object *item,
1498 const char *name,
1499 int whence,
1500 unsigned flags)
1502 struct rev_cmdline_info *info = &revs->cmdline;
1503 unsigned int nr = info->nr;
1505 ALLOC_GROW(info->rev, nr + 1, info->alloc);
1506 info->rev[nr].item = item;
1507 info->rev[nr].name = xstrdup(name);
1508 info->rev[nr].whence = whence;
1509 info->rev[nr].flags = flags;
1510 info->nr++;
1513 static void add_rev_cmdline_list(struct rev_info *revs,
1514 struct commit_list *commit_list,
1515 int whence,
1516 unsigned flags)
1518 while (commit_list) {
1519 struct object *object = &commit_list->item->object;
1520 add_rev_cmdline(revs, object, oid_to_hex(&object->oid),
1521 whence, flags);
1522 commit_list = commit_list->next;
1526 int ref_excluded(const struct ref_exclusions *exclusions, const char *path)
1528 const char *stripped_path = strip_namespace(path);
1529 struct string_list_item *item;
1531 for_each_string_list_item(item, &exclusions->excluded_refs) {
1532 if (!wildmatch(item->string, path, 0))
1533 return 1;
1536 if (ref_is_hidden(stripped_path, path, &exclusions->hidden_refs))
1537 return 1;
1539 return 0;
1542 void init_ref_exclusions(struct ref_exclusions *exclusions)
1544 struct ref_exclusions blank = REF_EXCLUSIONS_INIT;
1545 memcpy(exclusions, &blank, sizeof(*exclusions));
1548 void clear_ref_exclusions(struct ref_exclusions *exclusions)
1550 string_list_clear(&exclusions->excluded_refs, 0);
1551 string_list_clear(&exclusions->hidden_refs, 0);
1552 exclusions->hidden_refs_configured = 0;
1555 void add_ref_exclusion(struct ref_exclusions *exclusions, const char *exclude)
1557 string_list_append(&exclusions->excluded_refs, exclude);
1560 struct exclude_hidden_refs_cb {
1561 struct ref_exclusions *exclusions;
1562 const char *section;
1565 static int hide_refs_config(const char *var, const char *value, void *cb_data)
1567 struct exclude_hidden_refs_cb *cb = cb_data;
1568 cb->exclusions->hidden_refs_configured = 1;
1569 return parse_hide_refs_config(var, value, cb->section,
1570 &cb->exclusions->hidden_refs);
1573 void exclude_hidden_refs(struct ref_exclusions *exclusions, const char *section)
1575 struct exclude_hidden_refs_cb cb;
1577 if (strcmp(section, "receive") && strcmp(section, "uploadpack"))
1578 die(_("unsupported section for hidden refs: %s"), section);
1580 if (exclusions->hidden_refs_configured)
1581 die(_("--exclude-hidden= passed more than once"));
1583 cb.exclusions = exclusions;
1584 cb.section = section;
1586 git_config(hide_refs_config, &cb);
1589 struct all_refs_cb {
1590 int all_flags;
1591 int warned_bad_reflog;
1592 struct rev_info *all_revs;
1593 const char *name_for_errormsg;
1594 struct worktree *wt;
1597 static int handle_one_ref(const char *path, const struct object_id *oid,
1598 int flag UNUSED,
1599 void *cb_data)
1601 struct all_refs_cb *cb = cb_data;
1602 struct object *object;
1604 if (ref_excluded(&cb->all_revs->ref_excludes, path))
1605 return 0;
1607 object = get_reference(cb->all_revs, path, oid, cb->all_flags);
1608 add_rev_cmdline(cb->all_revs, object, path, REV_CMD_REF, cb->all_flags);
1609 add_pending_object(cb->all_revs, object, path);
1610 return 0;
1613 static void init_all_refs_cb(struct all_refs_cb *cb, struct rev_info *revs,
1614 unsigned flags)
1616 cb->all_revs = revs;
1617 cb->all_flags = flags;
1618 revs->rev_input_given = 1;
1619 cb->wt = NULL;
1622 static void handle_refs(struct ref_store *refs,
1623 struct rev_info *revs, unsigned flags,
1624 int (*for_each)(struct ref_store *, each_ref_fn, void *))
1626 struct all_refs_cb cb;
1628 if (!refs) {
1629 /* this could happen with uninitialized submodules */
1630 return;
1633 init_all_refs_cb(&cb, revs, flags);
1634 for_each(refs, handle_one_ref, &cb);
1637 static void handle_one_reflog_commit(struct object_id *oid, void *cb_data)
1639 struct all_refs_cb *cb = cb_data;
1640 if (!is_null_oid(oid)) {
1641 struct object *o = parse_object(cb->all_revs->repo, oid);
1642 if (o) {
1643 o->flags |= cb->all_flags;
1644 /* ??? CMDLINEFLAGS ??? */
1645 add_pending_object(cb->all_revs, o, "");
1647 else if (!cb->warned_bad_reflog) {
1648 warning("reflog of '%s' references pruned commits",
1649 cb->name_for_errormsg);
1650 cb->warned_bad_reflog = 1;
1655 static int handle_one_reflog_ent(struct object_id *ooid, struct object_id *noid,
1656 const char *email UNUSED,
1657 timestamp_t timestamp UNUSED,
1658 int tz UNUSED,
1659 const char *message UNUSED,
1660 void *cb_data)
1662 handle_one_reflog_commit(ooid, cb_data);
1663 handle_one_reflog_commit(noid, cb_data);
1664 return 0;
1667 static int handle_one_reflog(const char *refname_in_wt,
1668 const struct object_id *oid UNUSED,
1669 int flag UNUSED, void *cb_data)
1671 struct all_refs_cb *cb = cb_data;
1672 struct strbuf refname = STRBUF_INIT;
1674 cb->warned_bad_reflog = 0;
1675 strbuf_worktree_ref(cb->wt, &refname, refname_in_wt);
1676 cb->name_for_errormsg = refname.buf;
1677 refs_for_each_reflog_ent(get_main_ref_store(the_repository),
1678 refname.buf,
1679 handle_one_reflog_ent, cb_data);
1680 strbuf_release(&refname);
1681 return 0;
1684 static void add_other_reflogs_to_pending(struct all_refs_cb *cb)
1686 struct worktree **worktrees, **p;
1688 worktrees = get_worktrees();
1689 for (p = worktrees; *p; p++) {
1690 struct worktree *wt = *p;
1692 if (wt->is_current)
1693 continue;
1695 cb->wt = wt;
1696 refs_for_each_reflog(get_worktree_ref_store(wt),
1697 handle_one_reflog,
1698 cb);
1700 free_worktrees(worktrees);
1703 void add_reflogs_to_pending(struct rev_info *revs, unsigned flags)
1705 struct all_refs_cb cb;
1707 cb.all_revs = revs;
1708 cb.all_flags = flags;
1709 cb.wt = NULL;
1710 for_each_reflog(handle_one_reflog, &cb);
1712 if (!revs->single_worktree)
1713 add_other_reflogs_to_pending(&cb);
1716 static void add_cache_tree(struct cache_tree *it, struct rev_info *revs,
1717 struct strbuf *path, unsigned int flags)
1719 size_t baselen = path->len;
1720 int i;
1722 if (it->entry_count >= 0) {
1723 struct tree *tree = lookup_tree(revs->repo, &it->oid);
1724 tree->object.flags |= flags;
1725 add_pending_object_with_path(revs, &tree->object, "",
1726 040000, path->buf);
1729 for (i = 0; i < it->subtree_nr; i++) {
1730 struct cache_tree_sub *sub = it->down[i];
1731 strbuf_addf(path, "%s%s", baselen ? "/" : "", sub->name);
1732 add_cache_tree(sub->cache_tree, revs, path, flags);
1733 strbuf_setlen(path, baselen);
1738 static void add_resolve_undo_to_pending(struct index_state *istate, struct rev_info *revs)
1740 struct string_list_item *item;
1741 struct string_list *resolve_undo = istate->resolve_undo;
1743 if (!resolve_undo)
1744 return;
1746 for_each_string_list_item(item, resolve_undo) {
1747 const char *path = item->string;
1748 struct resolve_undo_info *ru = item->util;
1749 int i;
1751 if (!ru)
1752 continue;
1753 for (i = 0; i < 3; i++) {
1754 struct blob *blob;
1756 if (!ru->mode[i] || !S_ISREG(ru->mode[i]))
1757 continue;
1759 blob = lookup_blob(revs->repo, &ru->oid[i]);
1760 if (!blob) {
1761 warning(_("resolve-undo records `%s` which is missing"),
1762 oid_to_hex(&ru->oid[i]));
1763 continue;
1765 add_pending_object_with_path(revs, &blob->object, "",
1766 ru->mode[i], path);
1771 static void do_add_index_objects_to_pending(struct rev_info *revs,
1772 struct index_state *istate,
1773 unsigned int flags)
1775 int i;
1777 /* TODO: audit for interaction with sparse-index. */
1778 ensure_full_index(istate);
1779 for (i = 0; i < istate->cache_nr; i++) {
1780 struct cache_entry *ce = istate->cache[i];
1781 struct blob *blob;
1783 if (S_ISGITLINK(ce->ce_mode))
1784 continue;
1786 blob = lookup_blob(revs->repo, &ce->oid);
1787 if (!blob)
1788 die("unable to add index blob to traversal");
1789 blob->object.flags |= flags;
1790 add_pending_object_with_path(revs, &blob->object, "",
1791 ce->ce_mode, ce->name);
1794 if (istate->cache_tree) {
1795 struct strbuf path = STRBUF_INIT;
1796 add_cache_tree(istate->cache_tree, revs, &path, flags);
1797 strbuf_release(&path);
1800 add_resolve_undo_to_pending(istate, revs);
1803 void add_index_objects_to_pending(struct rev_info *revs, unsigned int flags)
1805 struct worktree **worktrees, **p;
1807 repo_read_index(revs->repo);
1808 do_add_index_objects_to_pending(revs, revs->repo->index, flags);
1810 if (revs->single_worktree)
1811 return;
1813 worktrees = get_worktrees();
1814 for (p = worktrees; *p; p++) {
1815 struct worktree *wt = *p;
1816 struct index_state istate = INDEX_STATE_INIT(revs->repo);
1818 if (wt->is_current)
1819 continue; /* current index already taken care of */
1821 if (read_index_from(&istate,
1822 worktree_git_path(wt, "index"),
1823 get_worktree_git_dir(wt)) > 0)
1824 do_add_index_objects_to_pending(revs, &istate, flags);
1825 discard_index(&istate);
1827 free_worktrees(worktrees);
1830 struct add_alternate_refs_data {
1831 struct rev_info *revs;
1832 unsigned int flags;
1835 static void add_one_alternate_ref(const struct object_id *oid,
1836 void *vdata)
1838 const char *name = ".alternate";
1839 struct add_alternate_refs_data *data = vdata;
1840 struct object *obj;
1842 obj = get_reference(data->revs, name, oid, data->flags);
1843 add_rev_cmdline(data->revs, obj, name, REV_CMD_REV, data->flags);
1844 add_pending_object(data->revs, obj, name);
1847 static void add_alternate_refs_to_pending(struct rev_info *revs,
1848 unsigned int flags)
1850 struct add_alternate_refs_data data;
1851 data.revs = revs;
1852 data.flags = flags;
1853 for_each_alternate_ref(add_one_alternate_ref, &data);
1856 static int add_parents_only(struct rev_info *revs, const char *arg_, int flags,
1857 int exclude_parent)
1859 struct object_id oid;
1860 struct object *it;
1861 struct commit *commit;
1862 struct commit_list *parents;
1863 int parent_number;
1864 const char *arg = arg_;
1866 if (*arg == '^') {
1867 flags ^= UNINTERESTING | BOTTOM;
1868 arg++;
1870 if (get_oid_committish(arg, &oid))
1871 return 0;
1872 while (1) {
1873 it = get_reference(revs, arg, &oid, 0);
1874 if (!it && revs->ignore_missing)
1875 return 0;
1876 if (it->type != OBJ_TAG)
1877 break;
1878 if (!((struct tag*)it)->tagged)
1879 return 0;
1880 oidcpy(&oid, &((struct tag*)it)->tagged->oid);
1882 if (it->type != OBJ_COMMIT)
1883 return 0;
1884 commit = (struct commit *)it;
1885 if (exclude_parent &&
1886 exclude_parent > commit_list_count(commit->parents))
1887 return 0;
1888 for (parents = commit->parents, parent_number = 1;
1889 parents;
1890 parents = parents->next, parent_number++) {
1891 if (exclude_parent && parent_number != exclude_parent)
1892 continue;
1894 it = &parents->item->object;
1895 it->flags |= flags;
1896 add_rev_cmdline(revs, it, arg_, REV_CMD_PARENTS_ONLY, flags);
1897 add_pending_object(revs, it, arg);
1899 return 1;
1902 void repo_init_revisions(struct repository *r,
1903 struct rev_info *revs,
1904 const char *prefix)
1906 struct rev_info blank = REV_INFO_INIT;
1907 memcpy(revs, &blank, sizeof(*revs));
1909 revs->repo = r;
1910 revs->pruning.repo = r;
1911 revs->pruning.add_remove = file_add_remove;
1912 revs->pruning.change = file_change;
1913 revs->pruning.change_fn_data = revs;
1914 revs->prefix = prefix;
1916 grep_init(&revs->grep_filter, revs->repo);
1917 revs->grep_filter.status_only = 1;
1919 repo_diff_setup(revs->repo, &revs->diffopt);
1920 if (prefix && !revs->diffopt.prefix) {
1921 revs->diffopt.prefix = prefix;
1922 revs->diffopt.prefix_length = strlen(prefix);
1925 init_display_notes(&revs->notes_opt);
1926 list_objects_filter_init(&revs->filter);
1927 init_ref_exclusions(&revs->ref_excludes);
1930 static void add_pending_commit_list(struct rev_info *revs,
1931 struct commit_list *commit_list,
1932 unsigned int flags)
1934 while (commit_list) {
1935 struct object *object = &commit_list->item->object;
1936 object->flags |= flags;
1937 add_pending_object(revs, object, oid_to_hex(&object->oid));
1938 commit_list = commit_list->next;
1942 static void prepare_show_merge(struct rev_info *revs)
1944 struct commit_list *bases;
1945 struct commit *head, *other;
1946 struct object_id oid;
1947 const char **prune = NULL;
1948 int i, prune_num = 1; /* counting terminating NULL */
1949 struct index_state *istate = revs->repo->index;
1951 if (get_oid("HEAD", &oid))
1952 die("--merge without HEAD?");
1953 head = lookup_commit_or_die(&oid, "HEAD");
1954 if (get_oid("MERGE_HEAD", &oid))
1955 die("--merge without MERGE_HEAD?");
1956 other = lookup_commit_or_die(&oid, "MERGE_HEAD");
1957 add_pending_object(revs, &head->object, "HEAD");
1958 add_pending_object(revs, &other->object, "MERGE_HEAD");
1959 bases = get_merge_bases(head, other);
1960 add_rev_cmdline_list(revs, bases, REV_CMD_MERGE_BASE, UNINTERESTING | BOTTOM);
1961 add_pending_commit_list(revs, bases, UNINTERESTING | BOTTOM);
1962 free_commit_list(bases);
1963 head->object.flags |= SYMMETRIC_LEFT;
1965 if (!istate->cache_nr)
1966 repo_read_index(revs->repo);
1967 for (i = 0; i < istate->cache_nr; i++) {
1968 const struct cache_entry *ce = istate->cache[i];
1969 if (!ce_stage(ce))
1970 continue;
1971 if (ce_path_match(istate, ce, &revs->prune_data, NULL)) {
1972 prune_num++;
1973 REALLOC_ARRAY(prune, prune_num);
1974 prune[prune_num-2] = ce->name;
1975 prune[prune_num-1] = NULL;
1977 while ((i+1 < istate->cache_nr) &&
1978 ce_same_name(ce, istate->cache[i+1]))
1979 i++;
1981 clear_pathspec(&revs->prune_data);
1982 parse_pathspec(&revs->prune_data, PATHSPEC_ALL_MAGIC & ~PATHSPEC_LITERAL,
1983 PATHSPEC_PREFER_FULL | PATHSPEC_LITERAL_PATH, "", prune);
1984 revs->limited = 1;
1987 static int dotdot_missing(const char *arg, char *dotdot,
1988 struct rev_info *revs, int symmetric)
1990 if (revs->ignore_missing)
1991 return 0;
1992 /* de-munge so we report the full argument */
1993 *dotdot = '.';
1994 die(symmetric
1995 ? "Invalid symmetric difference expression %s"
1996 : "Invalid revision range %s", arg);
1999 static int handle_dotdot_1(const char *arg, char *dotdot,
2000 struct rev_info *revs, int flags,
2001 int cant_be_filename,
2002 struct object_context *a_oc,
2003 struct object_context *b_oc)
2005 const char *a_name, *b_name;
2006 struct object_id a_oid, b_oid;
2007 struct object *a_obj, *b_obj;
2008 unsigned int a_flags, b_flags;
2009 int symmetric = 0;
2010 unsigned int flags_exclude = flags ^ (UNINTERESTING | BOTTOM);
2011 unsigned int oc_flags = GET_OID_COMMITTISH | GET_OID_RECORD_PATH;
2013 a_name = arg;
2014 if (!*a_name)
2015 a_name = "HEAD";
2017 b_name = dotdot + 2;
2018 if (*b_name == '.') {
2019 symmetric = 1;
2020 b_name++;
2022 if (!*b_name)
2023 b_name = "HEAD";
2025 if (get_oid_with_context(revs->repo, a_name, oc_flags, &a_oid, a_oc) ||
2026 get_oid_with_context(revs->repo, b_name, oc_flags, &b_oid, b_oc))
2027 return -1;
2029 if (!cant_be_filename) {
2030 *dotdot = '.';
2031 verify_non_filename(revs->prefix, arg);
2032 *dotdot = '\0';
2035 a_obj = parse_object(revs->repo, &a_oid);
2036 b_obj = parse_object(revs->repo, &b_oid);
2037 if (!a_obj || !b_obj)
2038 return dotdot_missing(arg, dotdot, revs, symmetric);
2040 if (!symmetric) {
2041 /* just A..B */
2042 b_flags = flags;
2043 a_flags = flags_exclude;
2044 } else {
2045 /* A...B -- find merge bases between the two */
2046 struct commit *a, *b;
2047 struct commit_list *exclude;
2049 a = lookup_commit_reference(revs->repo, &a_obj->oid);
2050 b = lookup_commit_reference(revs->repo, &b_obj->oid);
2051 if (!a || !b)
2052 return dotdot_missing(arg, dotdot, revs, symmetric);
2054 exclude = get_merge_bases(a, b);
2055 add_rev_cmdline_list(revs, exclude, REV_CMD_MERGE_BASE,
2056 flags_exclude);
2057 add_pending_commit_list(revs, exclude, flags_exclude);
2058 free_commit_list(exclude);
2060 b_flags = flags;
2061 a_flags = flags | SYMMETRIC_LEFT;
2064 a_obj->flags |= a_flags;
2065 b_obj->flags |= b_flags;
2066 add_rev_cmdline(revs, a_obj, a_name, REV_CMD_LEFT, a_flags);
2067 add_rev_cmdline(revs, b_obj, b_name, REV_CMD_RIGHT, b_flags);
2068 add_pending_object_with_path(revs, a_obj, a_name, a_oc->mode, a_oc->path);
2069 add_pending_object_with_path(revs, b_obj, b_name, b_oc->mode, b_oc->path);
2070 return 0;
2073 static int handle_dotdot(const char *arg,
2074 struct rev_info *revs, int flags,
2075 int cant_be_filename)
2077 struct object_context a_oc, b_oc;
2078 char *dotdot = strstr(arg, "..");
2079 int ret;
2081 if (!dotdot)
2082 return -1;
2084 memset(&a_oc, 0, sizeof(a_oc));
2085 memset(&b_oc, 0, sizeof(b_oc));
2087 *dotdot = '\0';
2088 ret = handle_dotdot_1(arg, dotdot, revs, flags, cant_be_filename,
2089 &a_oc, &b_oc);
2090 *dotdot = '.';
2092 free(a_oc.path);
2093 free(b_oc.path);
2095 return ret;
2098 static int handle_revision_arg_1(const char *arg_, struct rev_info *revs, int flags, unsigned revarg_opt)
2100 struct object_context oc;
2101 char *mark;
2102 struct object *object;
2103 struct object_id oid;
2104 int local_flags;
2105 const char *arg = arg_;
2106 int cant_be_filename = revarg_opt & REVARG_CANNOT_BE_FILENAME;
2107 unsigned get_sha1_flags = GET_OID_RECORD_PATH;
2109 flags = flags & UNINTERESTING ? flags | BOTTOM : flags & ~BOTTOM;
2111 if (!cant_be_filename && !strcmp(arg, "..")) {
2113 * Just ".."? That is not a range but the
2114 * pathspec for the parent directory.
2116 return -1;
2119 if (!handle_dotdot(arg, revs, flags, revarg_opt))
2120 return 0;
2122 mark = strstr(arg, "^@");
2123 if (mark && !mark[2]) {
2124 *mark = 0;
2125 if (add_parents_only(revs, arg, flags, 0))
2126 return 0;
2127 *mark = '^';
2129 mark = strstr(arg, "^!");
2130 if (mark && !mark[2]) {
2131 *mark = 0;
2132 if (!add_parents_only(revs, arg, flags ^ (UNINTERESTING | BOTTOM), 0))
2133 *mark = '^';
2135 mark = strstr(arg, "^-");
2136 if (mark) {
2137 int exclude_parent = 1;
2139 if (mark[2]) {
2140 if (strtol_i(mark + 2, 10, &exclude_parent) ||
2141 exclude_parent < 1)
2142 return -1;
2145 *mark = 0;
2146 if (!add_parents_only(revs, arg, flags ^ (UNINTERESTING | BOTTOM), exclude_parent))
2147 *mark = '^';
2150 local_flags = 0;
2151 if (*arg == '^') {
2152 local_flags = UNINTERESTING | BOTTOM;
2153 arg++;
2156 if (revarg_opt & REVARG_COMMITTISH)
2157 get_sha1_flags |= GET_OID_COMMITTISH;
2159 if (get_oid_with_context(revs->repo, arg, get_sha1_flags, &oid, &oc))
2160 return revs->ignore_missing ? 0 : -1;
2161 if (!cant_be_filename)
2162 verify_non_filename(revs->prefix, arg);
2163 object = get_reference(revs, arg, &oid, flags ^ local_flags);
2164 if (!object)
2165 return revs->ignore_missing ? 0 : -1;
2166 add_rev_cmdline(revs, object, arg_, REV_CMD_REV, flags ^ local_flags);
2167 add_pending_object_with_path(revs, object, arg, oc.mode, oc.path);
2168 free(oc.path);
2169 return 0;
2172 int handle_revision_arg(const char *arg, struct rev_info *revs, int flags, unsigned revarg_opt)
2174 int ret = handle_revision_arg_1(arg, revs, flags, revarg_opt);
2175 if (!ret)
2176 revs->rev_input_given = 1;
2177 return ret;
2180 static void read_pathspec_from_stdin(struct strbuf *sb,
2181 struct strvec *prune)
2183 while (strbuf_getline(sb, stdin) != EOF)
2184 strvec_push(prune, sb->buf);
2187 static void read_revisions_from_stdin(struct rev_info *revs,
2188 struct strvec *prune)
2190 struct strbuf sb;
2191 int seen_dashdash = 0;
2192 int save_warning;
2194 save_warning = warn_on_object_refname_ambiguity;
2195 warn_on_object_refname_ambiguity = 0;
2197 strbuf_init(&sb, 1000);
2198 while (strbuf_getline(&sb, stdin) != EOF) {
2199 int len = sb.len;
2200 if (!len)
2201 break;
2202 if (sb.buf[0] == '-') {
2203 if (len == 2 && sb.buf[1] == '-') {
2204 seen_dashdash = 1;
2205 break;
2207 die("options not supported in --stdin mode");
2209 if (handle_revision_arg(sb.buf, revs, 0,
2210 REVARG_CANNOT_BE_FILENAME))
2211 die("bad revision '%s'", sb.buf);
2213 if (seen_dashdash)
2214 read_pathspec_from_stdin(&sb, prune);
2216 strbuf_release(&sb);
2217 warn_on_object_refname_ambiguity = save_warning;
2220 static void add_grep(struct rev_info *revs, const char *ptn, enum grep_pat_token what)
2222 append_grep_pattern(&revs->grep_filter, ptn, "command line", 0, what);
2225 static void add_header_grep(struct rev_info *revs, enum grep_header_field field, const char *pattern)
2227 append_header_grep_pattern(&revs->grep_filter, field, pattern);
2230 static void add_message_grep(struct rev_info *revs, const char *pattern)
2232 add_grep(revs, pattern, GREP_PATTERN_BODY);
2235 static int handle_revision_opt(struct rev_info *revs, int argc, const char **argv,
2236 int *unkc, const char **unkv,
2237 const struct setup_revision_opt* opt)
2239 const char *arg = argv[0];
2240 const char *optarg = NULL;
2241 int argcount;
2242 const unsigned hexsz = the_hash_algo->hexsz;
2244 /* pseudo revision arguments */
2245 if (!strcmp(arg, "--all") || !strcmp(arg, "--branches") ||
2246 !strcmp(arg, "--tags") || !strcmp(arg, "--remotes") ||
2247 !strcmp(arg, "--reflog") || !strcmp(arg, "--not") ||
2248 !strcmp(arg, "--no-walk") || !strcmp(arg, "--do-walk") ||
2249 !strcmp(arg, "--bisect") || starts_with(arg, "--glob=") ||
2250 !strcmp(arg, "--indexed-objects") ||
2251 !strcmp(arg, "--alternate-refs") ||
2252 starts_with(arg, "--exclude=") || starts_with(arg, "--exclude-hidden=") ||
2253 starts_with(arg, "--branches=") || starts_with(arg, "--tags=") ||
2254 starts_with(arg, "--remotes=") || starts_with(arg, "--no-walk="))
2256 unkv[(*unkc)++] = arg;
2257 return 1;
2260 if ((argcount = parse_long_opt("max-count", argv, &optarg))) {
2261 revs->max_count = atoi(optarg);
2262 revs->no_walk = 0;
2263 return argcount;
2264 } else if ((argcount = parse_long_opt("skip", argv, &optarg))) {
2265 revs->skip_count = atoi(optarg);
2266 return argcount;
2267 } else if ((*arg == '-') && isdigit(arg[1])) {
2268 /* accept -<digit>, like traditional "head" */
2269 if (strtol_i(arg + 1, 10, &revs->max_count) < 0 ||
2270 revs->max_count < 0)
2271 die("'%s': not a non-negative integer", arg + 1);
2272 revs->no_walk = 0;
2273 } else if (!strcmp(arg, "-n")) {
2274 if (argc <= 1)
2275 return error("-n requires an argument");
2276 revs->max_count = atoi(argv[1]);
2277 revs->no_walk = 0;
2278 return 2;
2279 } else if (skip_prefix(arg, "-n", &optarg)) {
2280 revs->max_count = atoi(optarg);
2281 revs->no_walk = 0;
2282 } else if ((argcount = parse_long_opt("max-age", argv, &optarg))) {
2283 revs->max_age = atoi(optarg);
2284 return argcount;
2285 } else if ((argcount = parse_long_opt("since", argv, &optarg))) {
2286 revs->max_age = approxidate(optarg);
2287 return argcount;
2288 } else if ((argcount = parse_long_opt("since-as-filter", argv, &optarg))) {
2289 revs->max_age_as_filter = approxidate(optarg);
2290 return argcount;
2291 } else if ((argcount = parse_long_opt("after", argv, &optarg))) {
2292 revs->max_age = approxidate(optarg);
2293 return argcount;
2294 } else if ((argcount = parse_long_opt("min-age", argv, &optarg))) {
2295 revs->min_age = atoi(optarg);
2296 return argcount;
2297 } else if ((argcount = parse_long_opt("before", argv, &optarg))) {
2298 revs->min_age = approxidate(optarg);
2299 return argcount;
2300 } else if ((argcount = parse_long_opt("until", argv, &optarg))) {
2301 revs->min_age = approxidate(optarg);
2302 return argcount;
2303 } else if (!strcmp(arg, "--first-parent")) {
2304 revs->first_parent_only = 1;
2305 } else if (!strcmp(arg, "--exclude-first-parent-only")) {
2306 revs->exclude_first_parent_only = 1;
2307 } else if (!strcmp(arg, "--ancestry-path")) {
2308 revs->ancestry_path = 1;
2309 revs->simplify_history = 0;
2310 revs->limited = 1;
2311 revs->ancestry_path_implicit_bottoms = 1;
2312 } else if (skip_prefix(arg, "--ancestry-path=", &optarg)) {
2313 struct commit *c;
2314 struct object_id oid;
2315 const char *msg = _("could not get commit for ancestry-path argument %s");
2317 revs->ancestry_path = 1;
2318 revs->simplify_history = 0;
2319 revs->limited = 1;
2321 if (repo_get_oid_committish(revs->repo, optarg, &oid))
2322 return error(msg, optarg);
2323 get_reference(revs, optarg, &oid, ANCESTRY_PATH);
2324 c = lookup_commit_reference(revs->repo, &oid);
2325 if (!c)
2326 return error(msg, optarg);
2327 commit_list_insert(c, &revs->ancestry_path_bottoms);
2328 } else if (!strcmp(arg, "-g") || !strcmp(arg, "--walk-reflogs")) {
2329 init_reflog_walk(&revs->reflog_info);
2330 } else if (!strcmp(arg, "--default")) {
2331 if (argc <= 1)
2332 return error("bad --default argument");
2333 revs->def = argv[1];
2334 return 2;
2335 } else if (!strcmp(arg, "--merge")) {
2336 revs->show_merge = 1;
2337 } else if (!strcmp(arg, "--topo-order")) {
2338 revs->sort_order = REV_SORT_IN_GRAPH_ORDER;
2339 revs->topo_order = 1;
2340 } else if (!strcmp(arg, "--simplify-merges")) {
2341 revs->simplify_merges = 1;
2342 revs->topo_order = 1;
2343 revs->rewrite_parents = 1;
2344 revs->simplify_history = 0;
2345 revs->limited = 1;
2346 } else if (!strcmp(arg, "--simplify-by-decoration")) {
2347 revs->simplify_merges = 1;
2348 revs->topo_order = 1;
2349 revs->rewrite_parents = 1;
2350 revs->simplify_history = 0;
2351 revs->simplify_by_decoration = 1;
2352 revs->limited = 1;
2353 revs->prune = 1;
2354 } else if (!strcmp(arg, "--date-order")) {
2355 revs->sort_order = REV_SORT_BY_COMMIT_DATE;
2356 revs->topo_order = 1;
2357 } else if (!strcmp(arg, "--author-date-order")) {
2358 revs->sort_order = REV_SORT_BY_AUTHOR_DATE;
2359 revs->topo_order = 1;
2360 } else if (!strcmp(arg, "--early-output")) {
2361 revs->early_output = 100;
2362 revs->topo_order = 1;
2363 } else if (skip_prefix(arg, "--early-output=", &optarg)) {
2364 if (strtoul_ui(optarg, 10, &revs->early_output) < 0)
2365 die("'%s': not a non-negative integer", optarg);
2366 revs->topo_order = 1;
2367 } else if (!strcmp(arg, "--parents")) {
2368 revs->rewrite_parents = 1;
2369 revs->print_parents = 1;
2370 } else if (!strcmp(arg, "--dense")) {
2371 revs->dense = 1;
2372 } else if (!strcmp(arg, "--sparse")) {
2373 revs->dense = 0;
2374 } else if (!strcmp(arg, "--in-commit-order")) {
2375 revs->tree_blobs_in_commit_order = 1;
2376 } else if (!strcmp(arg, "--remove-empty")) {
2377 revs->remove_empty_trees = 1;
2378 } else if (!strcmp(arg, "--merges")) {
2379 revs->min_parents = 2;
2380 } else if (!strcmp(arg, "--no-merges")) {
2381 revs->max_parents = 1;
2382 } else if (skip_prefix(arg, "--min-parents=", &optarg)) {
2383 revs->min_parents = atoi(optarg);
2384 } else if (!strcmp(arg, "--no-min-parents")) {
2385 revs->min_parents = 0;
2386 } else if (skip_prefix(arg, "--max-parents=", &optarg)) {
2387 revs->max_parents = atoi(optarg);
2388 } else if (!strcmp(arg, "--no-max-parents")) {
2389 revs->max_parents = -1;
2390 } else if (!strcmp(arg, "--boundary")) {
2391 revs->boundary = 1;
2392 } else if (!strcmp(arg, "--left-right")) {
2393 revs->left_right = 1;
2394 } else if (!strcmp(arg, "--left-only")) {
2395 if (revs->right_only)
2396 die("--left-only is incompatible with --right-only"
2397 " or --cherry");
2398 revs->left_only = 1;
2399 } else if (!strcmp(arg, "--right-only")) {
2400 if (revs->left_only)
2401 die(_("options '%s' and '%s' cannot be used together"), "--right-only", "--left-only");
2402 revs->right_only = 1;
2403 } else if (!strcmp(arg, "--cherry")) {
2404 if (revs->left_only)
2405 die(_("options '%s' and '%s' cannot be used together"), "--cherry", "--left-only");
2406 revs->cherry_mark = 1;
2407 revs->right_only = 1;
2408 revs->max_parents = 1;
2409 revs->limited = 1;
2410 } else if (!strcmp(arg, "--count")) {
2411 revs->count = 1;
2412 } else if (!strcmp(arg, "--cherry-mark")) {
2413 if (revs->cherry_pick)
2414 die(_("options '%s' and '%s' cannot be used together"), "--cherry-mark", "--cherry-pick");
2415 revs->cherry_mark = 1;
2416 revs->limited = 1; /* needs limit_list() */
2417 } else if (!strcmp(arg, "--cherry-pick")) {
2418 if (revs->cherry_mark)
2419 die(_("options '%s' and '%s' cannot be used together"), "--cherry-pick", "--cherry-mark");
2420 revs->cherry_pick = 1;
2421 revs->limited = 1;
2422 } else if (!strcmp(arg, "--objects")) {
2423 revs->tag_objects = 1;
2424 revs->tree_objects = 1;
2425 revs->blob_objects = 1;
2426 } else if (!strcmp(arg, "--objects-edge")) {
2427 revs->tag_objects = 1;
2428 revs->tree_objects = 1;
2429 revs->blob_objects = 1;
2430 revs->edge_hint = 1;
2431 } else if (!strcmp(arg, "--objects-edge-aggressive")) {
2432 revs->tag_objects = 1;
2433 revs->tree_objects = 1;
2434 revs->blob_objects = 1;
2435 revs->edge_hint = 1;
2436 revs->edge_hint_aggressive = 1;
2437 } else if (!strcmp(arg, "--verify-objects")) {
2438 revs->tag_objects = 1;
2439 revs->tree_objects = 1;
2440 revs->blob_objects = 1;
2441 revs->verify_objects = 1;
2442 disable_commit_graph(revs->repo);
2443 } else if (!strcmp(arg, "--unpacked")) {
2444 revs->unpacked = 1;
2445 } else if (starts_with(arg, "--unpacked=")) {
2446 die(_("--unpacked=<packfile> no longer supported"));
2447 } else if (!strcmp(arg, "--no-kept-objects")) {
2448 revs->no_kept_objects = 1;
2449 revs->keep_pack_cache_flags |= IN_CORE_KEEP_PACKS;
2450 revs->keep_pack_cache_flags |= ON_DISK_KEEP_PACKS;
2451 } else if (skip_prefix(arg, "--no-kept-objects=", &optarg)) {
2452 revs->no_kept_objects = 1;
2453 if (!strcmp(optarg, "in-core"))
2454 revs->keep_pack_cache_flags |= IN_CORE_KEEP_PACKS;
2455 if (!strcmp(optarg, "on-disk"))
2456 revs->keep_pack_cache_flags |= ON_DISK_KEEP_PACKS;
2457 } else if (!strcmp(arg, "-r")) {
2458 revs->diff = 1;
2459 revs->diffopt.flags.recursive = 1;
2460 } else if (!strcmp(arg, "-t")) {
2461 revs->diff = 1;
2462 revs->diffopt.flags.recursive = 1;
2463 revs->diffopt.flags.tree_in_recursive = 1;
2464 } else if ((argcount = diff_merges_parse_opts(revs, argv))) {
2465 return argcount;
2466 } else if (!strcmp(arg, "-v")) {
2467 revs->verbose_header = 1;
2468 } else if (!strcmp(arg, "--pretty")) {
2469 revs->verbose_header = 1;
2470 revs->pretty_given = 1;
2471 get_commit_format(NULL, revs);
2472 } else if (skip_prefix(arg, "--pretty=", &optarg) ||
2473 skip_prefix(arg, "--format=", &optarg)) {
2475 * Detached form ("--pretty X" as opposed to "--pretty=X")
2476 * not allowed, since the argument is optional.
2478 revs->verbose_header = 1;
2479 revs->pretty_given = 1;
2480 get_commit_format(optarg, revs);
2481 } else if (!strcmp(arg, "--expand-tabs")) {
2482 revs->expand_tabs_in_log = 8;
2483 } else if (!strcmp(arg, "--no-expand-tabs")) {
2484 revs->expand_tabs_in_log = 0;
2485 } else if (skip_prefix(arg, "--expand-tabs=", &arg)) {
2486 int val;
2487 if (strtol_i(arg, 10, &val) < 0 || val < 0)
2488 die("'%s': not a non-negative integer", arg);
2489 revs->expand_tabs_in_log = val;
2490 } else if (!strcmp(arg, "--show-notes") || !strcmp(arg, "--notes")) {
2491 enable_default_display_notes(&revs->notes_opt, &revs->show_notes);
2492 revs->show_notes_given = 1;
2493 } else if (!strcmp(arg, "--show-signature")) {
2494 revs->show_signature = 1;
2495 } else if (!strcmp(arg, "--no-show-signature")) {
2496 revs->show_signature = 0;
2497 } else if (!strcmp(arg, "--show-linear-break")) {
2498 revs->break_bar = " ..........";
2499 revs->track_linear = 1;
2500 revs->track_first_time = 1;
2501 } else if (skip_prefix(arg, "--show-linear-break=", &optarg)) {
2502 revs->break_bar = xstrdup(optarg);
2503 revs->track_linear = 1;
2504 revs->track_first_time = 1;
2505 } else if (skip_prefix(arg, "--show-notes=", &optarg) ||
2506 skip_prefix(arg, "--notes=", &optarg)) {
2507 if (starts_with(arg, "--show-notes=") &&
2508 revs->notes_opt.use_default_notes < 0)
2509 revs->notes_opt.use_default_notes = 1;
2510 enable_ref_display_notes(&revs->notes_opt, &revs->show_notes, optarg);
2511 revs->show_notes_given = 1;
2512 } else if (!strcmp(arg, "--no-notes")) {
2513 disable_display_notes(&revs->notes_opt, &revs->show_notes);
2514 revs->show_notes_given = 1;
2515 } else if (!strcmp(arg, "--standard-notes")) {
2516 revs->show_notes_given = 1;
2517 revs->notes_opt.use_default_notes = 1;
2518 } else if (!strcmp(arg, "--no-standard-notes")) {
2519 revs->notes_opt.use_default_notes = 0;
2520 } else if (!strcmp(arg, "--oneline")) {
2521 revs->verbose_header = 1;
2522 get_commit_format("oneline", revs);
2523 revs->pretty_given = 1;
2524 revs->abbrev_commit = 1;
2525 } else if (!strcmp(arg, "--graph")) {
2526 graph_clear(revs->graph);
2527 revs->graph = graph_init(revs);
2528 } else if (!strcmp(arg, "--no-graph")) {
2529 graph_clear(revs->graph);
2530 revs->graph = NULL;
2531 } else if (!strcmp(arg, "--encode-email-headers")) {
2532 revs->encode_email_headers = 1;
2533 } else if (!strcmp(arg, "--no-encode-email-headers")) {
2534 revs->encode_email_headers = 0;
2535 } else if (!strcmp(arg, "--root")) {
2536 revs->show_root_diff = 1;
2537 } else if (!strcmp(arg, "--no-commit-id")) {
2538 revs->no_commit_id = 1;
2539 } else if (!strcmp(arg, "--always")) {
2540 revs->always_show_header = 1;
2541 } else if (!strcmp(arg, "--no-abbrev")) {
2542 revs->abbrev = 0;
2543 } else if (!strcmp(arg, "--abbrev")) {
2544 revs->abbrev = DEFAULT_ABBREV;
2545 } else if (skip_prefix(arg, "--abbrev=", &optarg)) {
2546 revs->abbrev = strtoul(optarg, NULL, 10);
2547 if (revs->abbrev < MINIMUM_ABBREV)
2548 revs->abbrev = MINIMUM_ABBREV;
2549 else if (revs->abbrev > hexsz)
2550 revs->abbrev = hexsz;
2551 } else if (!strcmp(arg, "--abbrev-commit")) {
2552 revs->abbrev_commit = 1;
2553 revs->abbrev_commit_given = 1;
2554 } else if (!strcmp(arg, "--no-abbrev-commit")) {
2555 revs->abbrev_commit = 0;
2556 } else if (!strcmp(arg, "--full-diff")) {
2557 revs->diff = 1;
2558 revs->full_diff = 1;
2559 } else if (!strcmp(arg, "--show-pulls")) {
2560 revs->show_pulls = 1;
2561 } else if (!strcmp(arg, "--full-history")) {
2562 revs->simplify_history = 0;
2563 } else if (!strcmp(arg, "--relative-date")) {
2564 revs->date_mode.type = DATE_RELATIVE;
2565 revs->date_mode_explicit = 1;
2566 } else if ((argcount = parse_long_opt("date", argv, &optarg))) {
2567 parse_date_format(optarg, &revs->date_mode);
2568 revs->date_mode_explicit = 1;
2569 return argcount;
2570 } else if (!strcmp(arg, "--log-size")) {
2571 revs->show_log_size = 1;
2574 * Grepping the commit log
2576 else if ((argcount = parse_long_opt("author", argv, &optarg))) {
2577 add_header_grep(revs, GREP_HEADER_AUTHOR, optarg);
2578 return argcount;
2579 } else if ((argcount = parse_long_opt("committer", argv, &optarg))) {
2580 add_header_grep(revs, GREP_HEADER_COMMITTER, optarg);
2581 return argcount;
2582 } else if ((argcount = parse_long_opt("grep-reflog", argv, &optarg))) {
2583 add_header_grep(revs, GREP_HEADER_REFLOG, optarg);
2584 return argcount;
2585 } else if ((argcount = parse_long_opt("grep", argv, &optarg))) {
2586 add_message_grep(revs, optarg);
2587 return argcount;
2588 } else if (!strcmp(arg, "--basic-regexp")) {
2589 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_BRE;
2590 } else if (!strcmp(arg, "--extended-regexp") || !strcmp(arg, "-E")) {
2591 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_ERE;
2592 } else if (!strcmp(arg, "--regexp-ignore-case") || !strcmp(arg, "-i")) {
2593 revs->grep_filter.ignore_case = 1;
2594 revs->diffopt.pickaxe_opts |= DIFF_PICKAXE_IGNORE_CASE;
2595 } else if (!strcmp(arg, "--fixed-strings") || !strcmp(arg, "-F")) {
2596 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_FIXED;
2597 } else if (!strcmp(arg, "--perl-regexp") || !strcmp(arg, "-P")) {
2598 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_PCRE;
2599 } else if (!strcmp(arg, "--all-match")) {
2600 revs->grep_filter.all_match = 1;
2601 } else if (!strcmp(arg, "--invert-grep")) {
2602 revs->grep_filter.no_body_match = 1;
2603 } else if ((argcount = parse_long_opt("encoding", argv, &optarg))) {
2604 if (strcmp(optarg, "none"))
2605 git_log_output_encoding = xstrdup(optarg);
2606 else
2607 git_log_output_encoding = "";
2608 return argcount;
2609 } else if (!strcmp(arg, "--reverse")) {
2610 revs->reverse ^= 1;
2611 } else if (!strcmp(arg, "--children")) {
2612 revs->children.name = "children";
2613 revs->limited = 1;
2614 } else if (!strcmp(arg, "--ignore-missing")) {
2615 revs->ignore_missing = 1;
2616 } else if (opt && opt->allow_exclude_promisor_objects &&
2617 !strcmp(arg, "--exclude-promisor-objects")) {
2618 if (fetch_if_missing)
2619 BUG("exclude_promisor_objects can only be used when fetch_if_missing is 0");
2620 revs->exclude_promisor_objects = 1;
2621 } else {
2622 int opts = diff_opt_parse(&revs->diffopt, argv, argc, revs->prefix);
2623 if (!opts)
2624 unkv[(*unkc)++] = arg;
2625 return opts;
2628 return 1;
2631 void parse_revision_opt(struct rev_info *revs, struct parse_opt_ctx_t *ctx,
2632 const struct option *options,
2633 const char * const usagestr[])
2635 int n = handle_revision_opt(revs, ctx->argc, ctx->argv,
2636 &ctx->cpidx, ctx->out, NULL);
2637 if (n <= 0) {
2638 error("unknown option `%s'", ctx->argv[0]);
2639 usage_with_options(usagestr, options);
2641 ctx->argv += n;
2642 ctx->argc -= n;
2645 void revision_opts_finish(struct rev_info *revs)
2647 if (revs->graph && revs->track_linear)
2648 die(_("options '%s' and '%s' cannot be used together"), "--show-linear-break", "--graph");
2650 if (revs->graph) {
2651 revs->topo_order = 1;
2652 revs->rewrite_parents = 1;
2656 static int for_each_bisect_ref(struct ref_store *refs, each_ref_fn fn,
2657 void *cb_data, const char *term)
2659 struct strbuf bisect_refs = STRBUF_INIT;
2660 int status;
2661 strbuf_addf(&bisect_refs, "refs/bisect/%s", term);
2662 status = refs_for_each_fullref_in(refs, bisect_refs.buf, fn, cb_data);
2663 strbuf_release(&bisect_refs);
2664 return status;
2667 static int for_each_bad_bisect_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
2669 return for_each_bisect_ref(refs, fn, cb_data, term_bad);
2672 static int for_each_good_bisect_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
2674 return for_each_bisect_ref(refs, fn, cb_data, term_good);
2677 static int handle_revision_pseudo_opt(struct rev_info *revs,
2678 const char **argv, int *flags)
2680 const char *arg = argv[0];
2681 const char *optarg;
2682 struct ref_store *refs;
2683 int argcount;
2685 if (revs->repo != the_repository) {
2687 * We need some something like get_submodule_worktrees()
2688 * before we can go through all worktrees of a submodule,
2689 * .e.g with adding all HEADs from --all, which is not
2690 * supported right now, so stick to single worktree.
2692 if (!revs->single_worktree)
2693 BUG("--single-worktree cannot be used together with submodule");
2695 refs = get_main_ref_store(revs->repo);
2698 * NOTE!
2700 * Commands like "git shortlog" will not accept the options below
2701 * unless parse_revision_opt queues them (as opposed to erroring
2702 * out).
2704 * When implementing your new pseudo-option, remember to
2705 * register it in the list at the top of handle_revision_opt.
2707 if (!strcmp(arg, "--all")) {
2708 handle_refs(refs, revs, *flags, refs_for_each_ref);
2709 handle_refs(refs, revs, *flags, refs_head_ref);
2710 if (!revs->single_worktree) {
2711 struct all_refs_cb cb;
2713 init_all_refs_cb(&cb, revs, *flags);
2714 other_head_refs(handle_one_ref, &cb);
2716 clear_ref_exclusions(&revs->ref_excludes);
2717 } else if (!strcmp(arg, "--branches")) {
2718 if (revs->ref_excludes.hidden_refs_configured)
2719 return error(_("--exclude-hidden cannot be used together with --branches"));
2720 handle_refs(refs, revs, *flags, refs_for_each_branch_ref);
2721 clear_ref_exclusions(&revs->ref_excludes);
2722 } else if (!strcmp(arg, "--bisect")) {
2723 read_bisect_terms(&term_bad, &term_good);
2724 handle_refs(refs, revs, *flags, for_each_bad_bisect_ref);
2725 handle_refs(refs, revs, *flags ^ (UNINTERESTING | BOTTOM),
2726 for_each_good_bisect_ref);
2727 revs->bisect = 1;
2728 } else if (!strcmp(arg, "--tags")) {
2729 if (revs->ref_excludes.hidden_refs_configured)
2730 return error(_("--exclude-hidden cannot be used together with --tags"));
2731 handle_refs(refs, revs, *flags, refs_for_each_tag_ref);
2732 clear_ref_exclusions(&revs->ref_excludes);
2733 } else if (!strcmp(arg, "--remotes")) {
2734 if (revs->ref_excludes.hidden_refs_configured)
2735 return error(_("--exclude-hidden cannot be used together with --remotes"));
2736 handle_refs(refs, revs, *flags, refs_for_each_remote_ref);
2737 clear_ref_exclusions(&revs->ref_excludes);
2738 } else if ((argcount = parse_long_opt("glob", argv, &optarg))) {
2739 struct all_refs_cb cb;
2740 init_all_refs_cb(&cb, revs, *flags);
2741 for_each_glob_ref(handle_one_ref, optarg, &cb);
2742 clear_ref_exclusions(&revs->ref_excludes);
2743 return argcount;
2744 } else if ((argcount = parse_long_opt("exclude", argv, &optarg))) {
2745 add_ref_exclusion(&revs->ref_excludes, optarg);
2746 return argcount;
2747 } else if ((argcount = parse_long_opt("exclude-hidden", argv, &optarg))) {
2748 exclude_hidden_refs(&revs->ref_excludes, optarg);
2749 return argcount;
2750 } else if (skip_prefix(arg, "--branches=", &optarg)) {
2751 struct all_refs_cb cb;
2752 if (revs->ref_excludes.hidden_refs_configured)
2753 return error(_("--exclude-hidden cannot be used together with --branches"));
2754 init_all_refs_cb(&cb, revs, *flags);
2755 for_each_glob_ref_in(handle_one_ref, optarg, "refs/heads/", &cb);
2756 clear_ref_exclusions(&revs->ref_excludes);
2757 } else if (skip_prefix(arg, "--tags=", &optarg)) {
2758 struct all_refs_cb cb;
2759 if (revs->ref_excludes.hidden_refs_configured)
2760 return error(_("--exclude-hidden cannot be used together with --tags"));
2761 init_all_refs_cb(&cb, revs, *flags);
2762 for_each_glob_ref_in(handle_one_ref, optarg, "refs/tags/", &cb);
2763 clear_ref_exclusions(&revs->ref_excludes);
2764 } else if (skip_prefix(arg, "--remotes=", &optarg)) {
2765 struct all_refs_cb cb;
2766 if (revs->ref_excludes.hidden_refs_configured)
2767 return error(_("--exclude-hidden cannot be used together with --remotes"));
2768 init_all_refs_cb(&cb, revs, *flags);
2769 for_each_glob_ref_in(handle_one_ref, optarg, "refs/remotes/", &cb);
2770 clear_ref_exclusions(&revs->ref_excludes);
2771 } else if (!strcmp(arg, "--reflog")) {
2772 add_reflogs_to_pending(revs, *flags);
2773 } else if (!strcmp(arg, "--indexed-objects")) {
2774 add_index_objects_to_pending(revs, *flags);
2775 } else if (!strcmp(arg, "--alternate-refs")) {
2776 add_alternate_refs_to_pending(revs, *flags);
2777 } else if (!strcmp(arg, "--not")) {
2778 *flags ^= UNINTERESTING | BOTTOM;
2779 } else if (!strcmp(arg, "--no-walk")) {
2780 revs->no_walk = 1;
2781 } else if (skip_prefix(arg, "--no-walk=", &optarg)) {
2783 * Detached form ("--no-walk X" as opposed to "--no-walk=X")
2784 * not allowed, since the argument is optional.
2786 revs->no_walk = 1;
2787 if (!strcmp(optarg, "sorted"))
2788 revs->unsorted_input = 0;
2789 else if (!strcmp(optarg, "unsorted"))
2790 revs->unsorted_input = 1;
2791 else
2792 return error("invalid argument to --no-walk");
2793 } else if (!strcmp(arg, "--do-walk")) {
2794 revs->no_walk = 0;
2795 } else if (!strcmp(arg, "--single-worktree")) {
2796 revs->single_worktree = 1;
2797 } else if (skip_prefix(arg, ("--filter="), &arg)) {
2798 parse_list_objects_filter(&revs->filter, arg);
2799 } else if (!strcmp(arg, ("--no-filter"))) {
2800 list_objects_filter_set_no_filter(&revs->filter);
2801 } else {
2802 return 0;
2805 return 1;
2808 static void NORETURN diagnose_missing_default(const char *def)
2810 int flags;
2811 const char *refname;
2813 refname = resolve_ref_unsafe(def, 0, NULL, &flags);
2814 if (!refname || !(flags & REF_ISSYMREF) || (flags & REF_ISBROKEN))
2815 die(_("your current branch appears to be broken"));
2817 skip_prefix(refname, "refs/heads/", &refname);
2818 die(_("your current branch '%s' does not have any commits yet"),
2819 refname);
2823 * Parse revision information, filling in the "rev_info" structure,
2824 * and removing the used arguments from the argument list.
2826 * Returns the number of arguments left that weren't recognized
2827 * (which are also moved to the head of the argument list)
2829 int setup_revisions(int argc, const char **argv, struct rev_info *revs, struct setup_revision_opt *opt)
2831 int i, flags, left, seen_dashdash, revarg_opt;
2832 struct strvec prune_data = STRVEC_INIT;
2833 int seen_end_of_options = 0;
2835 /* First, search for "--" */
2836 if (opt && opt->assume_dashdash) {
2837 seen_dashdash = 1;
2838 } else {
2839 seen_dashdash = 0;
2840 for (i = 1; i < argc; i++) {
2841 const char *arg = argv[i];
2842 if (strcmp(arg, "--"))
2843 continue;
2844 if (opt && opt->free_removed_argv_elements)
2845 free((char *)argv[i]);
2846 argv[i] = NULL;
2847 argc = i;
2848 if (argv[i + 1])
2849 strvec_pushv(&prune_data, argv + i + 1);
2850 seen_dashdash = 1;
2851 break;
2855 /* Second, deal with arguments and options */
2856 flags = 0;
2857 revarg_opt = opt ? opt->revarg_opt : 0;
2858 if (seen_dashdash)
2859 revarg_opt |= REVARG_CANNOT_BE_FILENAME;
2860 for (left = i = 1; i < argc; i++) {
2861 const char *arg = argv[i];
2862 if (!seen_end_of_options && *arg == '-') {
2863 int opts;
2865 opts = handle_revision_pseudo_opt(
2866 revs, argv + i,
2867 &flags);
2868 if (opts > 0) {
2869 i += opts - 1;
2870 continue;
2873 if (!strcmp(arg, "--stdin")) {
2874 if (revs->disable_stdin) {
2875 argv[left++] = arg;
2876 continue;
2878 if (revs->read_from_stdin++)
2879 die("--stdin given twice?");
2880 read_revisions_from_stdin(revs, &prune_data);
2881 continue;
2884 if (!strcmp(arg, "--end-of-options")) {
2885 seen_end_of_options = 1;
2886 continue;
2889 opts = handle_revision_opt(revs, argc - i, argv + i,
2890 &left, argv, opt);
2891 if (opts > 0) {
2892 i += opts - 1;
2893 continue;
2895 if (opts < 0)
2896 exit(128);
2897 continue;
2901 if (handle_revision_arg(arg, revs, flags, revarg_opt)) {
2902 int j;
2903 if (seen_dashdash || *arg == '^')
2904 die("bad revision '%s'", arg);
2906 /* If we didn't have a "--":
2907 * (1) all filenames must exist;
2908 * (2) all rev-args must not be interpretable
2909 * as a valid filename.
2910 * but the latter we have checked in the main loop.
2912 for (j = i; j < argc; j++)
2913 verify_filename(revs->prefix, argv[j], j == i);
2915 strvec_pushv(&prune_data, argv + i);
2916 break;
2919 revision_opts_finish(revs);
2921 if (prune_data.nr) {
2923 * If we need to introduce the magic "a lone ':' means no
2924 * pathspec whatsoever", here is the place to do so.
2926 * if (prune_data.nr == 1 && !strcmp(prune_data[0], ":")) {
2927 * prune_data.nr = 0;
2928 * prune_data.alloc = 0;
2929 * free(prune_data.path);
2930 * prune_data.path = NULL;
2931 * } else {
2932 * terminate prune_data.alloc with NULL and
2933 * call init_pathspec() to set revs->prune_data here.
2936 parse_pathspec(&revs->prune_data, 0, 0,
2937 revs->prefix, prune_data.v);
2939 strvec_clear(&prune_data);
2941 if (!revs->def)
2942 revs->def = opt ? opt->def : NULL;
2943 if (opt && opt->tweak)
2944 opt->tweak(revs, opt);
2945 if (revs->show_merge)
2946 prepare_show_merge(revs);
2947 if (revs->def && !revs->pending.nr && !revs->rev_input_given) {
2948 struct object_id oid;
2949 struct object *object;
2950 struct object_context oc;
2951 if (get_oid_with_context(revs->repo, revs->def, 0, &oid, &oc))
2952 diagnose_missing_default(revs->def);
2953 object = get_reference(revs, revs->def, &oid, 0);
2954 add_pending_object_with_mode(revs, object, revs->def, oc.mode);
2957 /* Did the user ask for any diff output? Run the diff! */
2958 if (revs->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT)
2959 revs->diff = 1;
2961 /* Pickaxe, diff-filter and rename following need diffs */
2962 if ((revs->diffopt.pickaxe_opts & DIFF_PICKAXE_KINDS_MASK) ||
2963 revs->diffopt.filter ||
2964 revs->diffopt.flags.follow_renames)
2965 revs->diff = 1;
2967 if (revs->diffopt.objfind)
2968 revs->simplify_history = 0;
2970 if (revs->line_level_traverse) {
2971 if (want_ancestry(revs))
2972 revs->limited = 1;
2973 revs->topo_order = 1;
2976 if (revs->topo_order && !generation_numbers_enabled(the_repository))
2977 revs->limited = 1;
2979 if (revs->prune_data.nr) {
2980 copy_pathspec(&revs->pruning.pathspec, &revs->prune_data);
2981 /* Can't prune commits with rename following: the paths change.. */
2982 if (!revs->diffopt.flags.follow_renames)
2983 revs->prune = 1;
2984 if (!revs->full_diff)
2985 copy_pathspec(&revs->diffopt.pathspec,
2986 &revs->prune_data);
2989 diff_merges_setup_revs(revs);
2991 revs->diffopt.abbrev = revs->abbrev;
2993 diff_setup_done(&revs->diffopt);
2995 if (!is_encoding_utf8(get_log_output_encoding()))
2996 revs->grep_filter.ignore_locale = 1;
2997 compile_grep_patterns(&revs->grep_filter);
2999 if (revs->reverse && revs->reflog_info)
3000 die(_("options '%s' and '%s' cannot be used together"), "--reverse", "--walk-reflogs");
3001 if (revs->reflog_info && revs->limited)
3002 die("cannot combine --walk-reflogs with history-limiting options");
3003 if (revs->rewrite_parents && revs->children.name)
3004 die(_("options '%s' and '%s' cannot be used together"), "--parents", "--children");
3005 if (revs->filter.choice && !revs->blob_objects)
3006 die(_("object filtering requires --objects"));
3009 * Limitations on the graph functionality
3011 if (revs->reverse && revs->graph)
3012 die(_("options '%s' and '%s' cannot be used together"), "--reverse", "--graph");
3014 if (revs->reflog_info && revs->graph)
3015 die(_("options '%s' and '%s' cannot be used together"), "--walk-reflogs", "--graph");
3016 if (revs->no_walk && revs->graph)
3017 die(_("options '%s' and '%s' cannot be used together"), "--no-walk", "--graph");
3018 if (!revs->reflog_info && revs->grep_filter.use_reflog_filter)
3019 die(_("the option '%s' requires '%s'"), "--grep-reflog", "--walk-reflogs");
3021 if (revs->line_level_traverse &&
3022 (revs->diffopt.output_format & ~(DIFF_FORMAT_PATCH | DIFF_FORMAT_NO_OUTPUT)))
3023 die(_("-L does not yet support diff formats besides -p and -s"));
3025 if (revs->expand_tabs_in_log < 0)
3026 revs->expand_tabs_in_log = revs->expand_tabs_in_log_default;
3028 return left;
3031 static void release_revisions_cmdline(struct rev_cmdline_info *cmdline)
3033 unsigned int i;
3035 for (i = 0; i < cmdline->nr; i++)
3036 free((char *)cmdline->rev[i].name);
3037 free(cmdline->rev);
3040 static void release_revisions_mailmap(struct string_list *mailmap)
3042 if (!mailmap)
3043 return;
3044 clear_mailmap(mailmap);
3045 free(mailmap);
3048 static void release_revisions_topo_walk_info(struct topo_walk_info *info);
3050 void release_revisions(struct rev_info *revs)
3052 free_commit_list(revs->commits);
3053 free_commit_list(revs->ancestry_path_bottoms);
3054 object_array_clear(&revs->pending);
3055 object_array_clear(&revs->boundary_commits);
3056 release_revisions_cmdline(&revs->cmdline);
3057 list_objects_filter_release(&revs->filter);
3058 clear_pathspec(&revs->prune_data);
3059 date_mode_release(&revs->date_mode);
3060 release_revisions_mailmap(revs->mailmap);
3061 free_grep_patterns(&revs->grep_filter);
3062 graph_clear(revs->graph);
3063 /* TODO (need to handle "no_free"): diff_free(&revs->diffopt) */
3064 diff_free(&revs->pruning);
3065 reflog_walk_info_release(revs->reflog_info);
3066 release_revisions_topo_walk_info(revs->topo_walk_info);
3069 static void add_child(struct rev_info *revs, struct commit *parent, struct commit *child)
3071 struct commit_list *l = xcalloc(1, sizeof(*l));
3073 l->item = child;
3074 l->next = add_decoration(&revs->children, &parent->object, l);
3077 static int remove_duplicate_parents(struct rev_info *revs, struct commit *commit)
3079 struct treesame_state *ts = lookup_decoration(&revs->treesame, &commit->object);
3080 struct commit_list **pp, *p;
3081 int surviving_parents;
3083 /* Examine existing parents while marking ones we have seen... */
3084 pp = &commit->parents;
3085 surviving_parents = 0;
3086 while ((p = *pp) != NULL) {
3087 struct commit *parent = p->item;
3088 if (parent->object.flags & TMP_MARK) {
3089 *pp = p->next;
3090 if (ts)
3091 compact_treesame(revs, commit, surviving_parents);
3092 continue;
3094 parent->object.flags |= TMP_MARK;
3095 surviving_parents++;
3096 pp = &p->next;
3098 /* clear the temporary mark */
3099 for (p = commit->parents; p; p = p->next) {
3100 p->item->object.flags &= ~TMP_MARK;
3102 /* no update_treesame() - removing duplicates can't affect TREESAME */
3103 return surviving_parents;
3106 struct merge_simplify_state {
3107 struct commit *simplified;
3110 static struct merge_simplify_state *locate_simplify_state(struct rev_info *revs, struct commit *commit)
3112 struct merge_simplify_state *st;
3114 st = lookup_decoration(&revs->merge_simplification, &commit->object);
3115 if (!st) {
3116 CALLOC_ARRAY(st, 1);
3117 add_decoration(&revs->merge_simplification, &commit->object, st);
3119 return st;
3122 static int mark_redundant_parents(struct commit *commit)
3124 struct commit_list *h = reduce_heads(commit->parents);
3125 int i = 0, marked = 0;
3126 struct commit_list *po, *pn;
3128 /* Want these for sanity-checking only */
3129 int orig_cnt = commit_list_count(commit->parents);
3130 int cnt = commit_list_count(h);
3133 * Not ready to remove items yet, just mark them for now, based
3134 * on the output of reduce_heads(). reduce_heads outputs the reduced
3135 * set in its original order, so this isn't too hard.
3137 po = commit->parents;
3138 pn = h;
3139 while (po) {
3140 if (pn && po->item == pn->item) {
3141 pn = pn->next;
3142 i++;
3143 } else {
3144 po->item->object.flags |= TMP_MARK;
3145 marked++;
3147 po=po->next;
3150 if (i != cnt || cnt+marked != orig_cnt)
3151 die("mark_redundant_parents %d %d %d %d", orig_cnt, cnt, i, marked);
3153 free_commit_list(h);
3155 return marked;
3158 static int mark_treesame_root_parents(struct commit *commit)
3160 struct commit_list *p;
3161 int marked = 0;
3163 for (p = commit->parents; p; p = p->next) {
3164 struct commit *parent = p->item;
3165 if (!parent->parents && (parent->object.flags & TREESAME)) {
3166 parent->object.flags |= TMP_MARK;
3167 marked++;
3171 return marked;
3175 * Awkward naming - this means one parent we are TREESAME to.
3176 * cf mark_treesame_root_parents: root parents that are TREESAME (to an
3177 * empty tree). Better name suggestions?
3179 static int leave_one_treesame_to_parent(struct rev_info *revs, struct commit *commit)
3181 struct treesame_state *ts = lookup_decoration(&revs->treesame, &commit->object);
3182 struct commit *unmarked = NULL, *marked = NULL;
3183 struct commit_list *p;
3184 unsigned n;
3186 for (p = commit->parents, n = 0; p; p = p->next, n++) {
3187 if (ts->treesame[n]) {
3188 if (p->item->object.flags & TMP_MARK) {
3189 if (!marked)
3190 marked = p->item;
3191 } else {
3192 if (!unmarked) {
3193 unmarked = p->item;
3194 break;
3201 * If we are TREESAME to a marked-for-deletion parent, but not to any
3202 * unmarked parents, unmark the first TREESAME parent. This is the
3203 * parent that the default simplify_history==1 scan would have followed,
3204 * and it doesn't make sense to omit that path when asking for a
3205 * simplified full history. Retaining it improves the chances of
3206 * understanding odd missed merges that took an old version of a file.
3208 * Example:
3210 * I--------*X A modified the file, but mainline merge X used
3211 * \ / "-s ours", so took the version from I. X is
3212 * `-*A--' TREESAME to I and !TREESAME to A.
3214 * Default log from X would produce "I". Without this check,
3215 * --full-history --simplify-merges would produce "I-A-X", showing
3216 * the merge commit X and that it changed A, but not making clear that
3217 * it had just taken the I version. With this check, the topology above
3218 * is retained.
3220 * Note that it is possible that the simplification chooses a different
3221 * TREESAME parent from the default, in which case this test doesn't
3222 * activate, and we _do_ drop the default parent. Example:
3224 * I------X A modified the file, but it was reverted in B,
3225 * \ / meaning mainline merge X is TREESAME to both
3226 * *A-*B parents.
3228 * Default log would produce "I" by following the first parent;
3229 * --full-history --simplify-merges will produce "I-A-B". But this is a
3230 * reasonable result - it presents a logical full history leading from
3231 * I to X, and X is not an important merge.
3233 if (!unmarked && marked) {
3234 marked->object.flags &= ~TMP_MARK;
3235 return 1;
3238 return 0;
3241 static int remove_marked_parents(struct rev_info *revs, struct commit *commit)
3243 struct commit_list **pp, *p;
3244 int nth_parent, removed = 0;
3246 pp = &commit->parents;
3247 nth_parent = 0;
3248 while ((p = *pp) != NULL) {
3249 struct commit *parent = p->item;
3250 if (parent->object.flags & TMP_MARK) {
3251 parent->object.flags &= ~TMP_MARK;
3252 *pp = p->next;
3253 free(p);
3254 removed++;
3255 compact_treesame(revs, commit, nth_parent);
3256 continue;
3258 pp = &p->next;
3259 nth_parent++;
3262 /* Removing parents can only increase TREESAMEness */
3263 if (removed && !(commit->object.flags & TREESAME))
3264 update_treesame(revs, commit);
3266 return nth_parent;
3269 static struct commit_list **simplify_one(struct rev_info *revs, struct commit *commit, struct commit_list **tail)
3271 struct commit_list *p;
3272 struct commit *parent;
3273 struct merge_simplify_state *st, *pst;
3274 int cnt;
3276 st = locate_simplify_state(revs, commit);
3279 * Have we handled this one?
3281 if (st->simplified)
3282 return tail;
3285 * An UNINTERESTING commit simplifies to itself, so does a
3286 * root commit. We do not rewrite parents of such commit
3287 * anyway.
3289 if ((commit->object.flags & UNINTERESTING) || !commit->parents) {
3290 st->simplified = commit;
3291 return tail;
3295 * Do we know what commit all of our parents that matter
3296 * should be rewritten to? Otherwise we are not ready to
3297 * rewrite this one yet.
3299 for (cnt = 0, p = commit->parents; p; p = p->next) {
3300 pst = locate_simplify_state(revs, p->item);
3301 if (!pst->simplified) {
3302 tail = &commit_list_insert(p->item, tail)->next;
3303 cnt++;
3305 if (revs->first_parent_only)
3306 break;
3308 if (cnt) {
3309 tail = &commit_list_insert(commit, tail)->next;
3310 return tail;
3314 * Rewrite our list of parents. Note that this cannot
3315 * affect our TREESAME flags in any way - a commit is
3316 * always TREESAME to its simplification.
3318 for (p = commit->parents; p; p = p->next) {
3319 pst = locate_simplify_state(revs, p->item);
3320 p->item = pst->simplified;
3321 if (revs->first_parent_only)
3322 break;
3325 if (revs->first_parent_only)
3326 cnt = 1;
3327 else
3328 cnt = remove_duplicate_parents(revs, commit);
3331 * It is possible that we are a merge and one side branch
3332 * does not have any commit that touches the given paths;
3333 * in such a case, the immediate parent from that branch
3334 * will be rewritten to be the merge base.
3336 * o----X X: the commit we are looking at;
3337 * / / o: a commit that touches the paths;
3338 * ---o----'
3340 * Further, a merge of an independent branch that doesn't
3341 * touch the path will reduce to a treesame root parent:
3343 * ----o----X X: the commit we are looking at;
3344 * / o: a commit that touches the paths;
3345 * r r: a root commit not touching the paths
3347 * Detect and simplify both cases.
3349 if (1 < cnt) {
3350 int marked = mark_redundant_parents(commit);
3351 marked += mark_treesame_root_parents(commit);
3352 if (marked)
3353 marked -= leave_one_treesame_to_parent(revs, commit);
3354 if (marked)
3355 cnt = remove_marked_parents(revs, commit);
3359 * A commit simplifies to itself if it is a root, if it is
3360 * UNINTERESTING, if it touches the given paths, or if it is a
3361 * merge and its parents don't simplify to one relevant commit
3362 * (the first two cases are already handled at the beginning of
3363 * this function).
3365 * Otherwise, it simplifies to what its sole relevant parent
3366 * simplifies to.
3368 if (!cnt ||
3369 (commit->object.flags & UNINTERESTING) ||
3370 !(commit->object.flags & TREESAME) ||
3371 (parent = one_relevant_parent(revs, commit->parents)) == NULL ||
3372 (revs->show_pulls && (commit->object.flags & PULL_MERGE)))
3373 st->simplified = commit;
3374 else {
3375 pst = locate_simplify_state(revs, parent);
3376 st->simplified = pst->simplified;
3378 return tail;
3381 static void simplify_merges(struct rev_info *revs)
3383 struct commit_list *list, *next;
3384 struct commit_list *yet_to_do, **tail;
3385 struct commit *commit;
3387 if (!revs->prune)
3388 return;
3390 /* feed the list reversed */
3391 yet_to_do = NULL;
3392 for (list = revs->commits; list; list = next) {
3393 commit = list->item;
3394 next = list->next;
3396 * Do not free(list) here yet; the original list
3397 * is used later in this function.
3399 commit_list_insert(commit, &yet_to_do);
3401 while (yet_to_do) {
3402 list = yet_to_do;
3403 yet_to_do = NULL;
3404 tail = &yet_to_do;
3405 while (list) {
3406 commit = pop_commit(&list);
3407 tail = simplify_one(revs, commit, tail);
3411 /* clean up the result, removing the simplified ones */
3412 list = revs->commits;
3413 revs->commits = NULL;
3414 tail = &revs->commits;
3415 while (list) {
3416 struct merge_simplify_state *st;
3418 commit = pop_commit(&list);
3419 st = locate_simplify_state(revs, commit);
3420 if (st->simplified == commit)
3421 tail = &commit_list_insert(commit, tail)->next;
3425 static void set_children(struct rev_info *revs)
3427 struct commit_list *l;
3428 for (l = revs->commits; l; l = l->next) {
3429 struct commit *commit = l->item;
3430 struct commit_list *p;
3432 for (p = commit->parents; p; p = p->next)
3433 add_child(revs, p->item, commit);
3437 void reset_revision_walk(void)
3439 clear_object_flags(SEEN | ADDED | SHOWN | TOPO_WALK_EXPLORED | TOPO_WALK_INDEGREE);
3442 static int mark_uninteresting(const struct object_id *oid,
3443 struct packed_git *pack,
3444 uint32_t pos,
3445 void *cb)
3447 struct rev_info *revs = cb;
3448 struct object *o = lookup_unknown_object(revs->repo, oid);
3449 o->flags |= UNINTERESTING | SEEN;
3450 return 0;
3453 define_commit_slab(indegree_slab, int);
3454 define_commit_slab(author_date_slab, timestamp_t);
3456 struct topo_walk_info {
3457 timestamp_t min_generation;
3458 struct prio_queue explore_queue;
3459 struct prio_queue indegree_queue;
3460 struct prio_queue topo_queue;
3461 struct indegree_slab indegree;
3462 struct author_date_slab author_date;
3465 static int topo_walk_atexit_registered;
3466 static unsigned int count_explore_walked;
3467 static unsigned int count_indegree_walked;
3468 static unsigned int count_topo_walked;
3470 static void trace2_topo_walk_statistics_atexit(void)
3472 struct json_writer jw = JSON_WRITER_INIT;
3474 jw_object_begin(&jw, 0);
3475 jw_object_intmax(&jw, "count_explore_walked", count_explore_walked);
3476 jw_object_intmax(&jw, "count_indegree_walked", count_indegree_walked);
3477 jw_object_intmax(&jw, "count_topo_walked", count_topo_walked);
3478 jw_end(&jw);
3480 trace2_data_json("topo_walk", the_repository, "statistics", &jw);
3482 jw_release(&jw);
3485 static inline void test_flag_and_insert(struct prio_queue *q, struct commit *c, int flag)
3487 if (c->object.flags & flag)
3488 return;
3490 c->object.flags |= flag;
3491 prio_queue_put(q, c);
3494 static void explore_walk_step(struct rev_info *revs)
3496 struct topo_walk_info *info = revs->topo_walk_info;
3497 struct commit_list *p;
3498 struct commit *c = prio_queue_get(&info->explore_queue);
3500 if (!c)
3501 return;
3503 if (repo_parse_commit_gently(revs->repo, c, 1) < 0)
3504 return;
3506 count_explore_walked++;
3508 if (revs->sort_order == REV_SORT_BY_AUTHOR_DATE)
3509 record_author_date(&info->author_date, c);
3511 if (revs->max_age != -1 && (c->date < revs->max_age))
3512 c->object.flags |= UNINTERESTING;
3514 if (process_parents(revs, c, NULL, NULL) < 0)
3515 return;
3517 if (c->object.flags & UNINTERESTING)
3518 mark_parents_uninteresting(revs, c);
3520 for (p = c->parents; p; p = p->next)
3521 test_flag_and_insert(&info->explore_queue, p->item, TOPO_WALK_EXPLORED);
3524 static void explore_to_depth(struct rev_info *revs,
3525 timestamp_t gen_cutoff)
3527 struct topo_walk_info *info = revs->topo_walk_info;
3528 struct commit *c;
3529 while ((c = prio_queue_peek(&info->explore_queue)) &&
3530 commit_graph_generation(c) >= gen_cutoff)
3531 explore_walk_step(revs);
3534 static void indegree_walk_step(struct rev_info *revs)
3536 struct commit_list *p;
3537 struct topo_walk_info *info = revs->topo_walk_info;
3538 struct commit *c = prio_queue_get(&info->indegree_queue);
3540 if (!c)
3541 return;
3543 if (repo_parse_commit_gently(revs->repo, c, 1) < 0)
3544 return;
3546 count_indegree_walked++;
3548 explore_to_depth(revs, commit_graph_generation(c));
3550 for (p = c->parents; p; p = p->next) {
3551 struct commit *parent = p->item;
3552 int *pi = indegree_slab_at(&info->indegree, parent);
3554 if (repo_parse_commit_gently(revs->repo, parent, 1) < 0)
3555 return;
3557 if (*pi)
3558 (*pi)++;
3559 else
3560 *pi = 2;
3562 test_flag_and_insert(&info->indegree_queue, parent, TOPO_WALK_INDEGREE);
3564 if (revs->first_parent_only)
3565 return;
3569 static void compute_indegrees_to_depth(struct rev_info *revs,
3570 timestamp_t gen_cutoff)
3572 struct topo_walk_info *info = revs->topo_walk_info;
3573 struct commit *c;
3574 while ((c = prio_queue_peek(&info->indegree_queue)) &&
3575 commit_graph_generation(c) >= gen_cutoff)
3576 indegree_walk_step(revs);
3579 static void release_revisions_topo_walk_info(struct topo_walk_info *info)
3581 if (!info)
3582 return;
3583 clear_prio_queue(&info->explore_queue);
3584 clear_prio_queue(&info->indegree_queue);
3585 clear_prio_queue(&info->topo_queue);
3586 clear_indegree_slab(&info->indegree);
3587 clear_author_date_slab(&info->author_date);
3588 free(info);
3591 static void reset_topo_walk(struct rev_info *revs)
3593 release_revisions_topo_walk_info(revs->topo_walk_info);
3594 revs->topo_walk_info = NULL;
3597 static void init_topo_walk(struct rev_info *revs)
3599 struct topo_walk_info *info;
3600 struct commit_list *list;
3601 if (revs->topo_walk_info)
3602 reset_topo_walk(revs);
3604 revs->topo_walk_info = xmalloc(sizeof(struct topo_walk_info));
3605 info = revs->topo_walk_info;
3606 memset(info, 0, sizeof(struct topo_walk_info));
3608 init_indegree_slab(&info->indegree);
3609 memset(&info->explore_queue, 0, sizeof(info->explore_queue));
3610 memset(&info->indegree_queue, 0, sizeof(info->indegree_queue));
3611 memset(&info->topo_queue, 0, sizeof(info->topo_queue));
3613 switch (revs->sort_order) {
3614 default: /* REV_SORT_IN_GRAPH_ORDER */
3615 info->topo_queue.compare = NULL;
3616 break;
3617 case REV_SORT_BY_COMMIT_DATE:
3618 info->topo_queue.compare = compare_commits_by_commit_date;
3619 break;
3620 case REV_SORT_BY_AUTHOR_DATE:
3621 init_author_date_slab(&info->author_date);
3622 info->topo_queue.compare = compare_commits_by_author_date;
3623 info->topo_queue.cb_data = &info->author_date;
3624 break;
3627 info->explore_queue.compare = compare_commits_by_gen_then_commit_date;
3628 info->indegree_queue.compare = compare_commits_by_gen_then_commit_date;
3630 info->min_generation = GENERATION_NUMBER_INFINITY;
3631 for (list = revs->commits; list; list = list->next) {
3632 struct commit *c = list->item;
3633 timestamp_t generation;
3635 if (repo_parse_commit_gently(revs->repo, c, 1))
3636 continue;
3638 test_flag_and_insert(&info->explore_queue, c, TOPO_WALK_EXPLORED);
3639 test_flag_and_insert(&info->indegree_queue, c, TOPO_WALK_INDEGREE);
3641 generation = commit_graph_generation(c);
3642 if (generation < info->min_generation)
3643 info->min_generation = generation;
3645 *(indegree_slab_at(&info->indegree, c)) = 1;
3647 if (revs->sort_order == REV_SORT_BY_AUTHOR_DATE)
3648 record_author_date(&info->author_date, c);
3650 compute_indegrees_to_depth(revs, info->min_generation);
3652 for (list = revs->commits; list; list = list->next) {
3653 struct commit *c = list->item;
3655 if (*(indegree_slab_at(&info->indegree, c)) == 1)
3656 prio_queue_put(&info->topo_queue, c);
3660 * This is unfortunate; the initial tips need to be shown
3661 * in the order given from the revision traversal machinery.
3663 if (revs->sort_order == REV_SORT_IN_GRAPH_ORDER)
3664 prio_queue_reverse(&info->topo_queue);
3666 if (trace2_is_enabled() && !topo_walk_atexit_registered) {
3667 atexit(trace2_topo_walk_statistics_atexit);
3668 topo_walk_atexit_registered = 1;
3672 static struct commit *next_topo_commit(struct rev_info *revs)
3674 struct commit *c;
3675 struct topo_walk_info *info = revs->topo_walk_info;
3677 /* pop next off of topo_queue */
3678 c = prio_queue_get(&info->topo_queue);
3680 if (c)
3681 *(indegree_slab_at(&info->indegree, c)) = 0;
3683 return c;
3686 static void expand_topo_walk(struct rev_info *revs, struct commit *commit)
3688 struct commit_list *p;
3689 struct topo_walk_info *info = revs->topo_walk_info;
3690 if (process_parents(revs, commit, NULL, NULL) < 0) {
3691 if (!revs->ignore_missing_links)
3692 die("Failed to traverse parents of commit %s",
3693 oid_to_hex(&commit->object.oid));
3696 count_topo_walked++;
3698 for (p = commit->parents; p; p = p->next) {
3699 struct commit *parent = p->item;
3700 int *pi;
3701 timestamp_t generation;
3703 if (parent->object.flags & UNINTERESTING)
3704 continue;
3706 if (repo_parse_commit_gently(revs->repo, parent, 1) < 0)
3707 continue;
3709 generation = commit_graph_generation(parent);
3710 if (generation < info->min_generation) {
3711 info->min_generation = generation;
3712 compute_indegrees_to_depth(revs, info->min_generation);
3715 pi = indegree_slab_at(&info->indegree, parent);
3717 (*pi)--;
3718 if (*pi == 1)
3719 prio_queue_put(&info->topo_queue, parent);
3721 if (revs->first_parent_only)
3722 return;
3726 int prepare_revision_walk(struct rev_info *revs)
3728 int i;
3729 struct object_array old_pending;
3730 struct commit_list **next = &revs->commits;
3732 memcpy(&old_pending, &revs->pending, sizeof(old_pending));
3733 revs->pending.nr = 0;
3734 revs->pending.alloc = 0;
3735 revs->pending.objects = NULL;
3736 for (i = 0; i < old_pending.nr; i++) {
3737 struct object_array_entry *e = old_pending.objects + i;
3738 struct commit *commit = handle_commit(revs, e);
3739 if (commit) {
3740 if (!(commit->object.flags & SEEN)) {
3741 commit->object.flags |= SEEN;
3742 next = commit_list_append(commit, next);
3746 object_array_clear(&old_pending);
3748 /* Signal whether we need per-parent treesame decoration */
3749 if (revs->simplify_merges ||
3750 (revs->limited && limiting_can_increase_treesame(revs)))
3751 revs->treesame.name = "treesame";
3753 if (revs->exclude_promisor_objects) {
3754 for_each_packed_object(mark_uninteresting, revs,
3755 FOR_EACH_OBJECT_PROMISOR_ONLY);
3758 if (!revs->reflog_info)
3759 prepare_to_use_bloom_filter(revs);
3760 if (!revs->unsorted_input)
3761 commit_list_sort_by_date(&revs->commits);
3762 if (revs->no_walk)
3763 return 0;
3764 if (revs->limited) {
3765 if (limit_list(revs) < 0)
3766 return -1;
3767 if (revs->topo_order)
3768 sort_in_topological_order(&revs->commits, revs->sort_order);
3769 } else if (revs->topo_order)
3770 init_topo_walk(revs);
3771 if (revs->line_level_traverse && want_ancestry(revs))
3773 * At the moment we can only do line-level log with parent
3774 * rewriting by performing this expensive pre-filtering step.
3775 * If parent rewriting is not requested, then we rather
3776 * perform the line-level log filtering during the regular
3777 * history traversal.
3779 line_log_filter(revs);
3780 if (revs->simplify_merges)
3781 simplify_merges(revs);
3782 if (revs->children.name)
3783 set_children(revs);
3785 return 0;
3788 static enum rewrite_result rewrite_one_1(struct rev_info *revs,
3789 struct commit **pp,
3790 struct prio_queue *queue)
3792 for (;;) {
3793 struct commit *p = *pp;
3794 if (!revs->limited)
3795 if (process_parents(revs, p, NULL, queue) < 0)
3796 return rewrite_one_error;
3797 if (p->object.flags & UNINTERESTING)
3798 return rewrite_one_ok;
3799 if (!(p->object.flags & TREESAME))
3800 return rewrite_one_ok;
3801 if (!p->parents)
3802 return rewrite_one_noparents;
3803 if (!(p = one_relevant_parent(revs, p->parents)))
3804 return rewrite_one_ok;
3805 *pp = p;
3809 static void merge_queue_into_list(struct prio_queue *q, struct commit_list **list)
3811 while (q->nr) {
3812 struct commit *item = prio_queue_peek(q);
3813 struct commit_list *p = *list;
3815 if (p && p->item->date >= item->date)
3816 list = &p->next;
3817 else {
3818 p = commit_list_insert(item, list);
3819 list = &p->next; /* skip newly added item */
3820 prio_queue_get(q); /* pop item */
3825 static enum rewrite_result rewrite_one(struct rev_info *revs, struct commit **pp)
3827 struct prio_queue queue = { compare_commits_by_commit_date };
3828 enum rewrite_result ret = rewrite_one_1(revs, pp, &queue);
3829 merge_queue_into_list(&queue, &revs->commits);
3830 clear_prio_queue(&queue);
3831 return ret;
3834 int rewrite_parents(struct rev_info *revs, struct commit *commit,
3835 rewrite_parent_fn_t rewrite_parent)
3837 struct commit_list **pp = &commit->parents;
3838 while (*pp) {
3839 struct commit_list *parent = *pp;
3840 switch (rewrite_parent(revs, &parent->item)) {
3841 case rewrite_one_ok:
3842 break;
3843 case rewrite_one_noparents:
3844 *pp = parent->next;
3845 continue;
3846 case rewrite_one_error:
3847 return -1;
3849 pp = &parent->next;
3851 remove_duplicate_parents(revs, commit);
3852 return 0;
3855 static int commit_match(struct commit *commit, struct rev_info *opt)
3857 int retval;
3858 const char *encoding;
3859 const char *message;
3860 struct strbuf buf = STRBUF_INIT;
3862 if (!opt->grep_filter.pattern_list && !opt->grep_filter.header_list)
3863 return 1;
3865 /* Prepend "fake" headers as needed */
3866 if (opt->grep_filter.use_reflog_filter) {
3867 strbuf_addstr(&buf, "reflog ");
3868 get_reflog_message(&buf, opt->reflog_info);
3869 strbuf_addch(&buf, '\n');
3873 * We grep in the user's output encoding, under the assumption that it
3874 * is the encoding they are most likely to write their grep pattern
3875 * for. In addition, it means we will match the "notes" encoding below,
3876 * so we will not end up with a buffer that has two different encodings
3877 * in it.
3879 encoding = get_log_output_encoding();
3880 message = logmsg_reencode(commit, NULL, encoding);
3882 /* Copy the commit to temporary if we are using "fake" headers */
3883 if (buf.len)
3884 strbuf_addstr(&buf, message);
3886 if (opt->grep_filter.header_list && opt->mailmap) {
3887 const char *commit_headers[] = { "author ", "committer ", NULL };
3889 if (!buf.len)
3890 strbuf_addstr(&buf, message);
3892 apply_mailmap_to_header(&buf, commit_headers, opt->mailmap);
3895 /* Append "fake" message parts as needed */
3896 if (opt->show_notes) {
3897 if (!buf.len)
3898 strbuf_addstr(&buf, message);
3899 format_display_notes(&commit->object.oid, &buf, encoding, 1);
3903 * Find either in the original commit message, or in the temporary.
3904 * Note that we cast away the constness of "message" here. It is
3905 * const because it may come from the cached commit buffer. That's OK,
3906 * because we know that it is modifiable heap memory, and that while
3907 * grep_buffer may modify it for speed, it will restore any
3908 * changes before returning.
3910 if (buf.len)
3911 retval = grep_buffer(&opt->grep_filter, buf.buf, buf.len);
3912 else
3913 retval = grep_buffer(&opt->grep_filter,
3914 (char *)message, strlen(message));
3915 strbuf_release(&buf);
3916 unuse_commit_buffer(commit, message);
3917 return retval;
3920 static inline int want_ancestry(const struct rev_info *revs)
3922 return (revs->rewrite_parents || revs->children.name);
3926 * Return a timestamp to be used for --since/--until comparisons for this
3927 * commit, based on the revision options.
3929 static timestamp_t comparison_date(const struct rev_info *revs,
3930 struct commit *commit)
3932 return revs->reflog_info ?
3933 get_reflog_timestamp(revs->reflog_info) :
3934 commit->date;
3937 enum commit_action get_commit_action(struct rev_info *revs, struct commit *commit)
3939 if (commit->object.flags & SHOWN)
3940 return commit_ignore;
3941 if (revs->unpacked && has_object_pack(&commit->object.oid))
3942 return commit_ignore;
3943 if (revs->no_kept_objects) {
3944 if (has_object_kept_pack(&commit->object.oid,
3945 revs->keep_pack_cache_flags))
3946 return commit_ignore;
3948 if (commit->object.flags & UNINTERESTING)
3949 return commit_ignore;
3950 if (revs->line_level_traverse && !want_ancestry(revs)) {
3952 * In case of line-level log with parent rewriting
3953 * prepare_revision_walk() already took care of all line-level
3954 * log filtering, and there is nothing left to do here.
3956 * If parent rewriting was not requested, then this is the
3957 * place to perform the line-level log filtering. Notably,
3958 * this check, though expensive, must come before the other,
3959 * cheaper filtering conditions, because the tracked line
3960 * ranges must be adjusted even when the commit will end up
3961 * being ignored based on other conditions.
3963 if (!line_log_process_ranges_arbitrary_commit(revs, commit))
3964 return commit_ignore;
3966 if (revs->min_age != -1 &&
3967 comparison_date(revs, commit) > revs->min_age)
3968 return commit_ignore;
3969 if (revs->max_age_as_filter != -1 &&
3970 comparison_date(revs, commit) < revs->max_age_as_filter)
3971 return commit_ignore;
3972 if (revs->min_parents || (revs->max_parents >= 0)) {
3973 int n = commit_list_count(commit->parents);
3974 if ((n < revs->min_parents) ||
3975 ((revs->max_parents >= 0) && (n > revs->max_parents)))
3976 return commit_ignore;
3978 if (!commit_match(commit, revs))
3979 return commit_ignore;
3980 if (revs->prune && revs->dense) {
3981 /* Commit without changes? */
3982 if (commit->object.flags & TREESAME) {
3983 int n;
3984 struct commit_list *p;
3985 /* drop merges unless we want parenthood */
3986 if (!want_ancestry(revs))
3987 return commit_ignore;
3989 if (revs->show_pulls && (commit->object.flags & PULL_MERGE))
3990 return commit_show;
3993 * If we want ancestry, then need to keep any merges
3994 * between relevant commits to tie together topology.
3995 * For consistency with TREESAME and simplification
3996 * use "relevant" here rather than just INTERESTING,
3997 * to treat bottom commit(s) as part of the topology.
3999 for (n = 0, p = commit->parents; p; p = p->next)
4000 if (relevant_commit(p->item))
4001 if (++n >= 2)
4002 return commit_show;
4003 return commit_ignore;
4006 return commit_show;
4009 define_commit_slab(saved_parents, struct commit_list *);
4011 #define EMPTY_PARENT_LIST ((struct commit_list *)-1)
4014 * You may only call save_parents() once per commit (this is checked
4015 * for non-root commits).
4017 static void save_parents(struct rev_info *revs, struct commit *commit)
4019 struct commit_list **pp;
4021 if (!revs->saved_parents_slab) {
4022 revs->saved_parents_slab = xmalloc(sizeof(struct saved_parents));
4023 init_saved_parents(revs->saved_parents_slab);
4026 pp = saved_parents_at(revs->saved_parents_slab, commit);
4029 * When walking with reflogs, we may visit the same commit
4030 * several times: once for each appearance in the reflog.
4032 * In this case, save_parents() will be called multiple times.
4033 * We want to keep only the first set of parents. We need to
4034 * store a sentinel value for an empty (i.e., NULL) parent
4035 * list to distinguish it from a not-yet-saved list, however.
4037 if (*pp)
4038 return;
4039 if (commit->parents)
4040 *pp = copy_commit_list(commit->parents);
4041 else
4042 *pp = EMPTY_PARENT_LIST;
4045 static void free_saved_parents(struct rev_info *revs)
4047 if (revs->saved_parents_slab)
4048 clear_saved_parents(revs->saved_parents_slab);
4051 struct commit_list *get_saved_parents(struct rev_info *revs, const struct commit *commit)
4053 struct commit_list *parents;
4055 if (!revs->saved_parents_slab)
4056 return commit->parents;
4058 parents = *saved_parents_at(revs->saved_parents_slab, commit);
4059 if (parents == EMPTY_PARENT_LIST)
4060 return NULL;
4061 return parents;
4064 enum commit_action simplify_commit(struct rev_info *revs, struct commit *commit)
4066 enum commit_action action = get_commit_action(revs, commit);
4068 if (action == commit_show &&
4069 revs->prune && revs->dense && want_ancestry(revs)) {
4071 * --full-diff on simplified parents is no good: it
4072 * will show spurious changes from the commits that
4073 * were elided. So we save the parents on the side
4074 * when --full-diff is in effect.
4076 if (revs->full_diff)
4077 save_parents(revs, commit);
4078 if (rewrite_parents(revs, commit, rewrite_one) < 0)
4079 return commit_error;
4081 return action;
4084 static void track_linear(struct rev_info *revs, struct commit *commit)
4086 if (revs->track_first_time) {
4087 revs->linear = 1;
4088 revs->track_first_time = 0;
4089 } else {
4090 struct commit_list *p;
4091 for (p = revs->previous_parents; p; p = p->next)
4092 if (p->item == NULL || /* first commit */
4093 oideq(&p->item->object.oid, &commit->object.oid))
4094 break;
4095 revs->linear = p != NULL;
4097 if (revs->reverse) {
4098 if (revs->linear)
4099 commit->object.flags |= TRACK_LINEAR;
4101 free_commit_list(revs->previous_parents);
4102 revs->previous_parents = copy_commit_list(commit->parents);
4105 static struct commit *get_revision_1(struct rev_info *revs)
4107 while (1) {
4108 struct commit *commit;
4110 if (revs->reflog_info)
4111 commit = next_reflog_entry(revs->reflog_info);
4112 else if (revs->topo_walk_info)
4113 commit = next_topo_commit(revs);
4114 else
4115 commit = pop_commit(&revs->commits);
4117 if (!commit)
4118 return NULL;
4120 if (revs->reflog_info)
4121 commit->object.flags &= ~(ADDED | SEEN | SHOWN);
4124 * If we haven't done the list limiting, we need to look at
4125 * the parents here. We also need to do the date-based limiting
4126 * that we'd otherwise have done in limit_list().
4128 if (!revs->limited) {
4129 if (revs->max_age != -1 &&
4130 comparison_date(revs, commit) < revs->max_age)
4131 continue;
4133 if (revs->reflog_info)
4134 try_to_simplify_commit(revs, commit);
4135 else if (revs->topo_walk_info)
4136 expand_topo_walk(revs, commit);
4137 else if (process_parents(revs, commit, &revs->commits, NULL) < 0) {
4138 if (!revs->ignore_missing_links)
4139 die("Failed to traverse parents of commit %s",
4140 oid_to_hex(&commit->object.oid));
4144 switch (simplify_commit(revs, commit)) {
4145 case commit_ignore:
4146 continue;
4147 case commit_error:
4148 die("Failed to simplify parents of commit %s",
4149 oid_to_hex(&commit->object.oid));
4150 default:
4151 if (revs->track_linear)
4152 track_linear(revs, commit);
4153 return commit;
4159 * Return true for entries that have not yet been shown. (This is an
4160 * object_array_each_func_t.)
4162 static int entry_unshown(struct object_array_entry *entry, void *cb_data_unused)
4164 return !(entry->item->flags & SHOWN);
4168 * If array is on the verge of a realloc, garbage-collect any entries
4169 * that have already been shown to try to free up some space.
4171 static void gc_boundary(struct object_array *array)
4173 if (array->nr == array->alloc)
4174 object_array_filter(array, entry_unshown, NULL);
4177 static void create_boundary_commit_list(struct rev_info *revs)
4179 unsigned i;
4180 struct commit *c;
4181 struct object_array *array = &revs->boundary_commits;
4182 struct object_array_entry *objects = array->objects;
4185 * If revs->commits is non-NULL at this point, an error occurred in
4186 * get_revision_1(). Ignore the error and continue printing the
4187 * boundary commits anyway. (This is what the code has always
4188 * done.)
4190 free_commit_list(revs->commits);
4191 revs->commits = NULL;
4194 * Put all of the actual boundary commits from revs->boundary_commits
4195 * into revs->commits
4197 for (i = 0; i < array->nr; i++) {
4198 c = (struct commit *)(objects[i].item);
4199 if (!c)
4200 continue;
4201 if (!(c->object.flags & CHILD_SHOWN))
4202 continue;
4203 if (c->object.flags & (SHOWN | BOUNDARY))
4204 continue;
4205 c->object.flags |= BOUNDARY;
4206 commit_list_insert(c, &revs->commits);
4210 * If revs->topo_order is set, sort the boundary commits
4211 * in topological order
4213 sort_in_topological_order(&revs->commits, revs->sort_order);
4216 static struct commit *get_revision_internal(struct rev_info *revs)
4218 struct commit *c = NULL;
4219 struct commit_list *l;
4221 if (revs->boundary == 2) {
4223 * All of the normal commits have already been returned,
4224 * and we are now returning boundary commits.
4225 * create_boundary_commit_list() has populated
4226 * revs->commits with the remaining commits to return.
4228 c = pop_commit(&revs->commits);
4229 if (c)
4230 c->object.flags |= SHOWN;
4231 return c;
4235 * If our max_count counter has reached zero, then we are done. We
4236 * don't simply return NULL because we still might need to show
4237 * boundary commits. But we want to avoid calling get_revision_1, which
4238 * might do a considerable amount of work finding the next commit only
4239 * for us to throw it away.
4241 * If it is non-zero, then either we don't have a max_count at all
4242 * (-1), or it is still counting, in which case we decrement.
4244 if (revs->max_count) {
4245 c = get_revision_1(revs);
4246 if (c) {
4247 while (revs->skip_count > 0) {
4248 revs->skip_count--;
4249 c = get_revision_1(revs);
4250 if (!c)
4251 break;
4255 if (revs->max_count > 0)
4256 revs->max_count--;
4259 if (c)
4260 c->object.flags |= SHOWN;
4262 if (!revs->boundary)
4263 return c;
4265 if (!c) {
4267 * get_revision_1() runs out the commits, and
4268 * we are done computing the boundaries.
4269 * switch to boundary commits output mode.
4271 revs->boundary = 2;
4274 * Update revs->commits to contain the list of
4275 * boundary commits.
4277 create_boundary_commit_list(revs);
4279 return get_revision_internal(revs);
4283 * boundary commits are the commits that are parents of the
4284 * ones we got from get_revision_1() but they themselves are
4285 * not returned from get_revision_1(). Before returning
4286 * 'c', we need to mark its parents that they could be boundaries.
4289 for (l = c->parents; l; l = l->next) {
4290 struct object *p;
4291 p = &(l->item->object);
4292 if (p->flags & (CHILD_SHOWN | SHOWN))
4293 continue;
4294 p->flags |= CHILD_SHOWN;
4295 gc_boundary(&revs->boundary_commits);
4296 add_object_array(p, NULL, &revs->boundary_commits);
4299 return c;
4302 struct commit *get_revision(struct rev_info *revs)
4304 struct commit *c;
4305 struct commit_list *reversed;
4307 if (revs->reverse) {
4308 reversed = NULL;
4309 while ((c = get_revision_internal(revs)))
4310 commit_list_insert(c, &reversed);
4311 revs->commits = reversed;
4312 revs->reverse = 0;
4313 revs->reverse_output_stage = 1;
4316 if (revs->reverse_output_stage) {
4317 c = pop_commit(&revs->commits);
4318 if (revs->track_linear)
4319 revs->linear = !!(c && c->object.flags & TRACK_LINEAR);
4320 return c;
4323 c = get_revision_internal(revs);
4324 if (c && revs->graph)
4325 graph_update(revs->graph, c);
4326 if (!c) {
4327 free_saved_parents(revs);
4328 free_commit_list(revs->previous_parents);
4329 revs->previous_parents = NULL;
4331 return c;
4334 const char *get_revision_mark(const struct rev_info *revs, const struct commit *commit)
4336 if (commit->object.flags & BOUNDARY)
4337 return "-";
4338 else if (commit->object.flags & UNINTERESTING)
4339 return "^";
4340 else if (commit->object.flags & PATCHSAME)
4341 return "=";
4342 else if (!revs || revs->left_right) {
4343 if (commit->object.flags & SYMMETRIC_LEFT)
4344 return "<";
4345 else
4346 return ">";
4347 } else if (revs->graph)
4348 return "*";
4349 else if (revs->cherry_mark)
4350 return "+";
4351 return "";
4354 void put_revision_mark(const struct rev_info *revs, const struct commit *commit)
4356 const char *mark = get_revision_mark(revs, commit);
4357 if (!strlen(mark))
4358 return;
4359 fputs(mark, stdout);
4360 putchar(' ');