doc: clarify <src> in refspec syntax
[git/gitster.git] / revision.c
blobe79f39e555560f11207aff62916bd297130c4ad7
1 #define USE_THE_REPOSITORY_VARIABLE
3 #include "git-compat-util.h"
4 #include "config.h"
5 #include "environment.h"
6 #include "gettext.h"
7 #include "hex.h"
8 #include "object-name.h"
9 #include "object-file.h"
10 #include "object-store-ll.h"
11 #include "oidset.h"
12 #include "tag.h"
13 #include "blob.h"
14 #include "tree.h"
15 #include "commit.h"
16 #include "diff.h"
17 #include "diff-merges.h"
18 #include "refs.h"
19 #include "revision.h"
20 #include "repository.h"
21 #include "graph.h"
22 #include "grep.h"
23 #include "reflog-walk.h"
24 #include "patch-ids.h"
25 #include "decorate.h"
26 #include "string-list.h"
27 #include "line-log.h"
28 #include "mailmap.h"
29 #include "commit-slab.h"
30 #include "cache-tree.h"
31 #include "bisect.h"
32 #include "packfile.h"
33 #include "worktree.h"
34 #include "path.h"
35 #include "read-cache.h"
36 #include "setup.h"
37 #include "sparse-index.h"
38 #include "strvec.h"
39 #include "trace2.h"
40 #include "commit-reach.h"
41 #include "commit-graph.h"
42 #include "prio-queue.h"
43 #include "hashmap.h"
44 #include "utf8.h"
45 #include "bloom.h"
46 #include "json-writer.h"
47 #include "list-objects-filter-options.h"
48 #include "resolve-undo.h"
49 #include "parse-options.h"
50 #include "wildmatch.h"
52 volatile show_early_output_fn_t show_early_output;
54 static const char *term_bad;
55 static const char *term_good;
57 implement_shared_commit_slab(revision_sources, char *);
59 static inline int want_ancestry(const struct rev_info *revs);
61 void show_object_with_name(FILE *out, struct object *obj, const char *name)
63 fprintf(out, "%s ", oid_to_hex(&obj->oid));
64 for (const char *p = name; *p && *p != '\n'; p++)
65 fputc(*p, out);
66 fputc('\n', out);
69 static void mark_blob_uninteresting(struct blob *blob)
71 if (!blob)
72 return;
73 if (blob->object.flags & UNINTERESTING)
74 return;
75 blob->object.flags |= UNINTERESTING;
78 static void mark_tree_contents_uninteresting(struct repository *r,
79 struct tree *tree)
81 struct tree_desc desc;
82 struct name_entry entry;
84 if (parse_tree_gently(tree, 1) < 0)
85 return;
87 init_tree_desc(&desc, &tree->object.oid, tree->buffer, tree->size);
88 while (tree_entry(&desc, &entry)) {
89 switch (object_type(entry.mode)) {
90 case OBJ_TREE:
91 mark_tree_uninteresting(r, lookup_tree(r, &entry.oid));
92 break;
93 case OBJ_BLOB:
94 mark_blob_uninteresting(lookup_blob(r, &entry.oid));
95 break;
96 default:
97 /* Subproject commit - not in this repository */
98 break;
103 * We don't care about the tree any more
104 * after it has been marked uninteresting.
106 free_tree_buffer(tree);
109 void mark_tree_uninteresting(struct repository *r, struct tree *tree)
111 struct object *obj;
113 if (!tree)
114 return;
116 obj = &tree->object;
117 if (obj->flags & UNINTERESTING)
118 return;
119 obj->flags |= UNINTERESTING;
120 mark_tree_contents_uninteresting(r, tree);
123 struct path_and_oids_entry {
124 struct hashmap_entry ent;
125 char *path;
126 struct oidset trees;
129 static int path_and_oids_cmp(const void *hashmap_cmp_fn_data UNUSED,
130 const struct hashmap_entry *eptr,
131 const struct hashmap_entry *entry_or_key,
132 const void *keydata UNUSED)
134 const struct path_and_oids_entry *e1, *e2;
136 e1 = container_of(eptr, const struct path_and_oids_entry, ent);
137 e2 = container_of(entry_or_key, const struct path_and_oids_entry, ent);
139 return strcmp(e1->path, e2->path);
142 static void paths_and_oids_clear(struct hashmap *map)
144 struct hashmap_iter iter;
145 struct path_and_oids_entry *entry;
147 hashmap_for_each_entry(map, &iter, entry, ent /* member name */) {
148 oidset_clear(&entry->trees);
149 free(entry->path);
152 hashmap_clear_and_free(map, struct path_and_oids_entry, ent);
155 static void paths_and_oids_insert(struct hashmap *map,
156 const char *path,
157 const struct object_id *oid)
159 int hash = strhash(path);
160 struct path_and_oids_entry key;
161 struct path_and_oids_entry *entry;
163 hashmap_entry_init(&key.ent, hash);
165 /* use a shallow copy for the lookup */
166 key.path = (char *)path;
167 oidset_init(&key.trees, 0);
169 entry = hashmap_get_entry(map, &key, ent, NULL);
170 if (!entry) {
171 CALLOC_ARRAY(entry, 1);
172 hashmap_entry_init(&entry->ent, hash);
173 entry->path = xstrdup(key.path);
174 oidset_init(&entry->trees, 16);
175 hashmap_put(map, &entry->ent);
178 oidset_insert(&entry->trees, oid);
181 static void add_children_by_path(struct repository *r,
182 struct tree *tree,
183 struct hashmap *map)
185 struct tree_desc desc;
186 struct name_entry entry;
188 if (!tree)
189 return;
191 if (parse_tree_gently(tree, 1) < 0)
192 return;
194 init_tree_desc(&desc, &tree->object.oid, tree->buffer, tree->size);
195 while (tree_entry(&desc, &entry)) {
196 switch (object_type(entry.mode)) {
197 case OBJ_TREE:
198 paths_and_oids_insert(map, entry.path, &entry.oid);
200 if (tree->object.flags & UNINTERESTING) {
201 struct tree *child = lookup_tree(r, &entry.oid);
202 if (child)
203 child->object.flags |= UNINTERESTING;
205 break;
206 case OBJ_BLOB:
207 if (tree->object.flags & UNINTERESTING) {
208 struct blob *child = lookup_blob(r, &entry.oid);
209 if (child)
210 child->object.flags |= UNINTERESTING;
212 break;
213 default:
214 /* Subproject commit - not in this repository */
215 break;
219 free_tree_buffer(tree);
222 void mark_trees_uninteresting_sparse(struct repository *r,
223 struct oidset *trees)
225 unsigned has_interesting = 0, has_uninteresting = 0;
226 struct hashmap map = HASHMAP_INIT(path_and_oids_cmp, NULL);
227 struct hashmap_iter map_iter;
228 struct path_and_oids_entry *entry;
229 struct object_id *oid;
230 struct oidset_iter iter;
232 oidset_iter_init(trees, &iter);
233 while ((!has_interesting || !has_uninteresting) &&
234 (oid = oidset_iter_next(&iter))) {
235 struct tree *tree = lookup_tree(r, oid);
237 if (!tree)
238 continue;
240 if (tree->object.flags & UNINTERESTING)
241 has_uninteresting = 1;
242 else
243 has_interesting = 1;
246 /* Do not walk unless we have both types of trees. */
247 if (!has_uninteresting || !has_interesting)
248 return;
250 oidset_iter_init(trees, &iter);
251 while ((oid = oidset_iter_next(&iter))) {
252 struct tree *tree = lookup_tree(r, oid);
253 add_children_by_path(r, tree, &map);
256 hashmap_for_each_entry(&map, &map_iter, entry, ent /* member name */)
257 mark_trees_uninteresting_sparse(r, &entry->trees);
259 paths_and_oids_clear(&map);
262 struct commit_stack {
263 struct commit **items;
264 size_t nr, alloc;
266 #define COMMIT_STACK_INIT { 0 }
268 static void commit_stack_push(struct commit_stack *stack, struct commit *commit)
270 ALLOC_GROW(stack->items, stack->nr + 1, stack->alloc);
271 stack->items[stack->nr++] = commit;
274 static struct commit *commit_stack_pop(struct commit_stack *stack)
276 return stack->nr ? stack->items[--stack->nr] : NULL;
279 static void commit_stack_clear(struct commit_stack *stack)
281 FREE_AND_NULL(stack->items);
282 stack->nr = stack->alloc = 0;
285 static void mark_one_parent_uninteresting(struct rev_info *revs, struct commit *commit,
286 struct commit_stack *pending)
288 struct commit_list *l;
290 if (commit->object.flags & UNINTERESTING)
291 return;
292 commit->object.flags |= UNINTERESTING;
295 * Normally we haven't parsed the parent
296 * yet, so we won't have a parent of a parent
297 * here. However, it may turn out that we've
298 * reached this commit some other way (where it
299 * wasn't uninteresting), in which case we need
300 * to mark its parents recursively too..
302 for (l = commit->parents; l; l = l->next) {
303 commit_stack_push(pending, l->item);
304 if (revs && revs->exclude_first_parent_only)
305 break;
309 void mark_parents_uninteresting(struct rev_info *revs, struct commit *commit)
311 struct commit_stack pending = COMMIT_STACK_INIT;
312 struct commit_list *l;
314 for (l = commit->parents; l; l = l->next) {
315 mark_one_parent_uninteresting(revs, l->item, &pending);
316 if (revs && revs->exclude_first_parent_only)
317 break;
320 while (pending.nr > 0)
321 mark_one_parent_uninteresting(revs, commit_stack_pop(&pending),
322 &pending);
324 commit_stack_clear(&pending);
327 static void add_pending_object_with_path(struct rev_info *revs,
328 struct object *obj,
329 const char *name, unsigned mode,
330 const char *path)
332 struct interpret_branch_name_options options = { 0 };
333 if (!obj)
334 return;
335 if (revs->no_walk && (obj->flags & UNINTERESTING))
336 revs->no_walk = 0;
337 if (revs->reflog_info && obj->type == OBJ_COMMIT) {
338 struct strbuf buf = STRBUF_INIT;
339 size_t namelen = strlen(name);
340 int len = repo_interpret_branch_name(the_repository, name,
341 namelen, &buf, &options);
343 if (0 < len && len < namelen && buf.len)
344 strbuf_addstr(&buf, name + len);
345 add_reflog_for_walk(revs->reflog_info,
346 (struct commit *)obj,
347 buf.buf[0] ? buf.buf: name);
348 strbuf_release(&buf);
349 return; /* do not add the commit itself */
351 add_object_array_with_path(obj, name, &revs->pending, mode, path);
354 static void add_pending_object_with_mode(struct rev_info *revs,
355 struct object *obj,
356 const char *name, unsigned mode)
358 add_pending_object_with_path(revs, obj, name, mode, NULL);
361 void add_pending_object(struct rev_info *revs,
362 struct object *obj, const char *name)
364 add_pending_object_with_mode(revs, obj, name, S_IFINVALID);
367 void add_head_to_pending(struct rev_info *revs)
369 struct object_id oid;
370 struct object *obj;
371 if (repo_get_oid(the_repository, "HEAD", &oid))
372 return;
373 obj = parse_object(revs->repo, &oid);
374 if (!obj)
375 return;
376 add_pending_object(revs, obj, "HEAD");
379 static struct object *get_reference(struct rev_info *revs, const char *name,
380 const struct object_id *oid,
381 unsigned int flags)
383 struct object *object;
385 object = parse_object_with_flags(revs->repo, oid,
386 revs->verify_objects ? 0 :
387 PARSE_OBJECT_SKIP_HASH_CHECK |
388 PARSE_OBJECT_DISCARD_TREE);
390 if (!object) {
391 if (revs->ignore_missing)
392 return NULL;
393 if (revs->exclude_promisor_objects && is_promisor_object(oid))
394 return NULL;
395 if (revs->do_not_die_on_missing_objects) {
396 oidset_insert(&revs->missing_commits, oid);
397 return NULL;
399 die("bad object %s", name);
401 object->flags |= flags;
402 return object;
405 void add_pending_oid(struct rev_info *revs, const char *name,
406 const struct object_id *oid, unsigned int flags)
408 struct object *object = get_reference(revs, name, oid, flags);
409 add_pending_object(revs, object, name);
412 static struct commit *handle_commit(struct rev_info *revs,
413 struct object_array_entry *entry)
415 struct object *object = entry->item;
416 const char *name = entry->name;
417 const char *path = entry->path;
418 unsigned int mode = entry->mode;
419 unsigned long flags = object->flags;
422 * Tag object? Look what it points to..
424 while (object->type == OBJ_TAG) {
425 struct tag *tag = (struct tag *) object;
426 struct object_id *oid;
427 if (revs->tag_objects && !(flags & UNINTERESTING))
428 add_pending_object(revs, object, tag->tag);
429 oid = get_tagged_oid(tag);
430 object = parse_object(revs->repo, oid);
431 if (!object) {
432 if (revs->ignore_missing_links || (flags & UNINTERESTING))
433 return NULL;
434 if (revs->exclude_promisor_objects &&
435 is_promisor_object(&tag->tagged->oid))
436 return NULL;
437 if (revs->do_not_die_on_missing_objects && oid) {
438 oidset_insert(&revs->missing_commits, oid);
439 return NULL;
441 die("bad object %s", oid_to_hex(&tag->tagged->oid));
443 object->flags |= flags;
445 * We'll handle the tagged object by looping or dropping
446 * through to the non-tag handlers below. Do not
447 * propagate path data from the tag's pending entry.
449 path = NULL;
450 mode = 0;
454 * Commit object? Just return it, we'll do all the complex
455 * reachability crud.
457 if (object->type == OBJ_COMMIT) {
458 struct commit *commit = (struct commit *)object;
460 if (repo_parse_commit(revs->repo, commit) < 0)
461 die("unable to parse commit %s", name);
462 if (flags & UNINTERESTING) {
463 mark_parents_uninteresting(revs, commit);
465 if (!revs->topo_order || !generation_numbers_enabled(the_repository))
466 revs->limited = 1;
468 if (revs->sources) {
469 char **slot = revision_sources_at(revs->sources, commit);
471 if (!*slot)
472 *slot = xstrdup(name);
474 return commit;
478 * Tree object? Either mark it uninteresting, or add it
479 * to the list of objects to look at later..
481 if (object->type == OBJ_TREE) {
482 struct tree *tree = (struct tree *)object;
483 if (!revs->tree_objects)
484 return NULL;
485 if (flags & UNINTERESTING) {
486 mark_tree_contents_uninteresting(revs->repo, tree);
487 return NULL;
489 add_pending_object_with_path(revs, object, name, mode, path);
490 return NULL;
494 * Blob object? You know the drill by now..
496 if (object->type == OBJ_BLOB) {
497 if (!revs->blob_objects)
498 return NULL;
499 if (flags & UNINTERESTING)
500 return NULL;
501 add_pending_object_with_path(revs, object, name, mode, path);
502 return NULL;
504 die("%s is unknown object", name);
507 static int everybody_uninteresting(struct commit_list *orig,
508 struct commit **interesting_cache)
510 struct commit_list *list = orig;
512 if (*interesting_cache) {
513 struct commit *commit = *interesting_cache;
514 if (!(commit->object.flags & UNINTERESTING))
515 return 0;
518 while (list) {
519 struct commit *commit = list->item;
520 list = list->next;
521 if (commit->object.flags & UNINTERESTING)
522 continue;
524 *interesting_cache = commit;
525 return 0;
527 return 1;
531 * A definition of "relevant" commit that we can use to simplify limited graphs
532 * by eliminating side branches.
534 * A "relevant" commit is one that is !UNINTERESTING (ie we are including it
535 * in our list), or that is a specified BOTTOM commit. Then after computing
536 * a limited list, during processing we can generally ignore boundary merges
537 * coming from outside the graph, (ie from irrelevant parents), and treat
538 * those merges as if they were single-parent. TREESAME is defined to consider
539 * only relevant parents, if any. If we are TREESAME to our on-graph parents,
540 * we don't care if we were !TREESAME to non-graph parents.
542 * Treating bottom commits as relevant ensures that a limited graph's
543 * connection to the actual bottom commit is not viewed as a side branch, but
544 * treated as part of the graph. For example:
546 * ....Z...A---X---o---o---B
547 * . /
548 * W---Y
550 * When computing "A..B", the A-X connection is at least as important as
551 * Y-X, despite A being flagged UNINTERESTING.
553 * And when computing --ancestry-path "A..B", the A-X connection is more
554 * important than Y-X, despite both A and Y being flagged UNINTERESTING.
556 static inline int relevant_commit(struct commit *commit)
558 return (commit->object.flags & (UNINTERESTING | BOTTOM)) != UNINTERESTING;
562 * Return a single relevant commit from a parent list. If we are a TREESAME
563 * commit, and this selects one of our parents, then we can safely simplify to
564 * that parent.
566 static struct commit *one_relevant_parent(const struct rev_info *revs,
567 struct commit_list *orig)
569 struct commit_list *list = orig;
570 struct commit *relevant = NULL;
572 if (!orig)
573 return NULL;
576 * For 1-parent commits, or if first-parent-only, then return that
577 * first parent (even if not "relevant" by the above definition).
578 * TREESAME will have been set purely on that parent.
580 if (revs->first_parent_only || !orig->next)
581 return orig->item;
584 * For multi-parent commits, identify a sole relevant parent, if any.
585 * If we have only one relevant parent, then TREESAME will be set purely
586 * with regard to that parent, and we can simplify accordingly.
588 * If we have more than one relevant parent, or no relevant parents
589 * (and multiple irrelevant ones), then we can't select a parent here
590 * and return NULL.
592 while (list) {
593 struct commit *commit = list->item;
594 list = list->next;
595 if (relevant_commit(commit)) {
596 if (relevant)
597 return NULL;
598 relevant = commit;
601 return relevant;
605 * The goal is to get REV_TREE_NEW as the result only if the
606 * diff consists of all '+' (and no other changes), REV_TREE_OLD
607 * if the whole diff is removal of old data, and otherwise
608 * REV_TREE_DIFFERENT (of course if the trees are the same we
609 * want REV_TREE_SAME).
611 * The only time we care about the distinction is when
612 * remove_empty_trees is in effect, in which case we care only about
613 * whether the whole change is REV_TREE_NEW, or if there's another type
614 * of change. Which means we can stop the diff early in either of these
615 * cases:
617 * 1. We're not using remove_empty_trees at all.
619 * 2. We saw anything except REV_TREE_NEW.
621 #define REV_TREE_SAME 0
622 #define REV_TREE_NEW 1 /* Only new files */
623 #define REV_TREE_OLD 2 /* Only files removed */
624 #define REV_TREE_DIFFERENT 3 /* Mixed changes */
625 static int tree_difference = REV_TREE_SAME;
627 static void file_add_remove(struct diff_options *options,
628 int addremove,
629 unsigned mode UNUSED,
630 const struct object_id *oid UNUSED,
631 int oid_valid UNUSED,
632 const char *fullpath UNUSED,
633 unsigned dirty_submodule UNUSED)
635 int diff = addremove == '+' ? REV_TREE_NEW : REV_TREE_OLD;
636 struct rev_info *revs = options->change_fn_data;
638 tree_difference |= diff;
639 if (!revs->remove_empty_trees || tree_difference != REV_TREE_NEW)
640 options->flags.has_changes = 1;
643 static void file_change(struct diff_options *options,
644 unsigned old_mode UNUSED,
645 unsigned new_mode UNUSED,
646 const struct object_id *old_oid UNUSED,
647 const struct object_id *new_oid UNUSED,
648 int old_oid_valid UNUSED,
649 int new_oid_valid UNUSED,
650 const char *fullpath UNUSED,
651 unsigned old_dirty_submodule UNUSED,
652 unsigned new_dirty_submodule UNUSED)
654 tree_difference = REV_TREE_DIFFERENT;
655 options->flags.has_changes = 1;
658 static int bloom_filter_atexit_registered;
659 static unsigned int count_bloom_filter_maybe;
660 static unsigned int count_bloom_filter_definitely_not;
661 static unsigned int count_bloom_filter_false_positive;
662 static unsigned int count_bloom_filter_not_present;
664 static void trace2_bloom_filter_statistics_atexit(void)
666 struct json_writer jw = JSON_WRITER_INIT;
668 jw_object_begin(&jw, 0);
669 jw_object_intmax(&jw, "filter_not_present", count_bloom_filter_not_present);
670 jw_object_intmax(&jw, "maybe", count_bloom_filter_maybe);
671 jw_object_intmax(&jw, "definitely_not", count_bloom_filter_definitely_not);
672 jw_object_intmax(&jw, "false_positive", count_bloom_filter_false_positive);
673 jw_end(&jw);
675 trace2_data_json("bloom", the_repository, "statistics", &jw);
677 jw_release(&jw);
680 static int forbid_bloom_filters(struct pathspec *spec)
682 if (spec->has_wildcard)
683 return 1;
684 if (spec->nr > 1)
685 return 1;
686 if (spec->magic & ~PATHSPEC_LITERAL)
687 return 1;
688 if (spec->nr && (spec->items[0].magic & ~PATHSPEC_LITERAL))
689 return 1;
691 return 0;
694 static void prepare_to_use_bloom_filter(struct rev_info *revs)
696 struct pathspec_item *pi;
697 char *path_alloc = NULL;
698 const char *path, *p;
699 size_t len;
700 int path_component_nr = 1;
702 if (!revs->commits)
703 return;
705 if (forbid_bloom_filters(&revs->prune_data))
706 return;
708 repo_parse_commit(revs->repo, revs->commits->item);
710 revs->bloom_filter_settings = get_bloom_filter_settings(revs->repo);
711 if (!revs->bloom_filter_settings)
712 return;
714 if (!revs->pruning.pathspec.nr)
715 return;
717 pi = &revs->pruning.pathspec.items[0];
719 /* remove single trailing slash from path, if needed */
720 if (pi->len > 0 && pi->match[pi->len - 1] == '/') {
721 path_alloc = xmemdupz(pi->match, pi->len - 1);
722 path = path_alloc;
723 } else
724 path = pi->match;
726 len = strlen(path);
727 if (!len) {
728 revs->bloom_filter_settings = NULL;
729 free(path_alloc);
730 return;
733 p = path;
734 while (*p) {
736 * At this point, the path is normalized to use Unix-style
737 * path separators. This is required due to how the
738 * changed-path Bloom filters store the paths.
740 if (*p == '/')
741 path_component_nr++;
742 p++;
745 revs->bloom_keys_nr = path_component_nr;
746 ALLOC_ARRAY(revs->bloom_keys, revs->bloom_keys_nr);
748 fill_bloom_key(path, len, &revs->bloom_keys[0],
749 revs->bloom_filter_settings);
750 path_component_nr = 1;
752 p = path + len - 1;
753 while (p > path) {
754 if (*p == '/')
755 fill_bloom_key(path, p - path,
756 &revs->bloom_keys[path_component_nr++],
757 revs->bloom_filter_settings);
758 p--;
761 if (trace2_is_enabled() && !bloom_filter_atexit_registered) {
762 atexit(trace2_bloom_filter_statistics_atexit);
763 bloom_filter_atexit_registered = 1;
766 free(path_alloc);
769 static int check_maybe_different_in_bloom_filter(struct rev_info *revs,
770 struct commit *commit)
772 struct bloom_filter *filter;
773 int result = 1, j;
775 if (!revs->repo->objects->commit_graph)
776 return -1;
778 if (commit_graph_generation(commit) == GENERATION_NUMBER_INFINITY)
779 return -1;
781 filter = get_bloom_filter(revs->repo, commit);
783 if (!filter) {
784 count_bloom_filter_not_present++;
785 return -1;
788 for (j = 0; result && j < revs->bloom_keys_nr; j++) {
789 result = bloom_filter_contains(filter,
790 &revs->bloom_keys[j],
791 revs->bloom_filter_settings);
794 if (result)
795 count_bloom_filter_maybe++;
796 else
797 count_bloom_filter_definitely_not++;
799 return result;
802 static int rev_compare_tree(struct rev_info *revs,
803 struct commit *parent, struct commit *commit, int nth_parent)
805 struct tree *t1 = repo_get_commit_tree(the_repository, parent);
806 struct tree *t2 = repo_get_commit_tree(the_repository, commit);
807 int bloom_ret = 1;
809 if (!t1)
810 return REV_TREE_NEW;
811 if (!t2)
812 return REV_TREE_OLD;
814 if (revs->simplify_by_decoration) {
816 * If we are simplifying by decoration, then the commit
817 * is worth showing if it has a tag pointing at it.
819 if (get_name_decoration(&commit->object))
820 return REV_TREE_DIFFERENT;
822 * A commit that is not pointed by a tag is uninteresting
823 * if we are not limited by path. This means that you will
824 * see the usual "commits that touch the paths" plus any
825 * tagged commit by specifying both --simplify-by-decoration
826 * and pathspec.
828 if (!revs->prune_data.nr)
829 return REV_TREE_SAME;
832 if (revs->bloom_keys_nr && !nth_parent) {
833 bloom_ret = check_maybe_different_in_bloom_filter(revs, commit);
835 if (bloom_ret == 0)
836 return REV_TREE_SAME;
839 tree_difference = REV_TREE_SAME;
840 revs->pruning.flags.has_changes = 0;
841 diff_tree_oid(&t1->object.oid, &t2->object.oid, "", &revs->pruning);
843 if (!nth_parent)
844 if (bloom_ret == 1 && tree_difference == REV_TREE_SAME)
845 count_bloom_filter_false_positive++;
847 return tree_difference;
850 static int rev_same_tree_as_empty(struct rev_info *revs, struct commit *commit,
851 int nth_parent)
853 struct tree *t1 = repo_get_commit_tree(the_repository, commit);
854 int bloom_ret = -1;
856 if (!t1)
857 return 0;
859 if (!nth_parent && revs->bloom_keys_nr) {
860 bloom_ret = check_maybe_different_in_bloom_filter(revs, commit);
861 if (!bloom_ret)
862 return 1;
865 tree_difference = REV_TREE_SAME;
866 revs->pruning.flags.has_changes = 0;
867 diff_tree_oid(NULL, &t1->object.oid, "", &revs->pruning);
869 if (bloom_ret == 1 && tree_difference == REV_TREE_SAME)
870 count_bloom_filter_false_positive++;
872 return tree_difference == REV_TREE_SAME;
875 struct treesame_state {
876 unsigned int nparents;
877 unsigned char treesame[FLEX_ARRAY];
880 static struct treesame_state *initialise_treesame(struct rev_info *revs, struct commit *commit)
882 unsigned n = commit_list_count(commit->parents);
883 struct treesame_state *st = xcalloc(1, st_add(sizeof(*st), n));
884 st->nparents = n;
885 add_decoration(&revs->treesame, &commit->object, st);
886 return st;
890 * Must be called immediately after removing the nth_parent from a commit's
891 * parent list, if we are maintaining the per-parent treesame[] decoration.
892 * This does not recalculate the master TREESAME flag - update_treesame()
893 * should be called to update it after a sequence of treesame[] modifications
894 * that may have affected it.
896 static int compact_treesame(struct rev_info *revs, struct commit *commit, unsigned nth_parent)
898 struct treesame_state *st;
899 int old_same;
901 if (!commit->parents) {
903 * Have just removed the only parent from a non-merge.
904 * Different handling, as we lack decoration.
906 if (nth_parent != 0)
907 die("compact_treesame %u", nth_parent);
908 old_same = !!(commit->object.flags & TREESAME);
909 if (rev_same_tree_as_empty(revs, commit, nth_parent))
910 commit->object.flags |= TREESAME;
911 else
912 commit->object.flags &= ~TREESAME;
913 return old_same;
916 st = lookup_decoration(&revs->treesame, &commit->object);
917 if (!st || nth_parent >= st->nparents)
918 die("compact_treesame %u", nth_parent);
920 old_same = st->treesame[nth_parent];
921 memmove(st->treesame + nth_parent,
922 st->treesame + nth_parent + 1,
923 st->nparents - nth_parent - 1);
926 * If we've just become a non-merge commit, update TREESAME
927 * immediately, and remove the no-longer-needed decoration.
928 * If still a merge, defer update until update_treesame().
930 if (--st->nparents == 1) {
931 if (commit->parents->next)
932 die("compact_treesame parents mismatch");
933 if (st->treesame[0] && revs->dense)
934 commit->object.flags |= TREESAME;
935 else
936 commit->object.flags &= ~TREESAME;
937 free(add_decoration(&revs->treesame, &commit->object, NULL));
940 return old_same;
943 static unsigned update_treesame(struct rev_info *revs, struct commit *commit)
945 if (commit->parents && commit->parents->next) {
946 unsigned n;
947 struct treesame_state *st;
948 struct commit_list *p;
949 unsigned relevant_parents;
950 unsigned relevant_change, irrelevant_change;
952 st = lookup_decoration(&revs->treesame, &commit->object);
953 if (!st)
954 die("update_treesame %s", oid_to_hex(&commit->object.oid));
955 relevant_parents = 0;
956 relevant_change = irrelevant_change = 0;
957 for (p = commit->parents, n = 0; p; n++, p = p->next) {
958 if (relevant_commit(p->item)) {
959 relevant_change |= !st->treesame[n];
960 relevant_parents++;
961 } else
962 irrelevant_change |= !st->treesame[n];
964 if (relevant_parents ? relevant_change : irrelevant_change)
965 commit->object.flags &= ~TREESAME;
966 else
967 commit->object.flags |= TREESAME;
970 return commit->object.flags & TREESAME;
973 static inline int limiting_can_increase_treesame(const struct rev_info *revs)
976 * TREESAME is irrelevant unless prune && dense;
977 * if simplify_history is set, we can't have a mixture of TREESAME and
978 * !TREESAME INTERESTING parents (and we don't have treesame[]
979 * decoration anyway);
980 * if first_parent_only is set, then the TREESAME flag is locked
981 * against the first parent (and again we lack treesame[] decoration).
983 return revs->prune && revs->dense &&
984 !revs->simplify_history &&
985 !revs->first_parent_only;
988 static void try_to_simplify_commit(struct rev_info *revs, struct commit *commit)
990 struct commit_list **pp, *parent;
991 struct treesame_state *ts = NULL;
992 int relevant_change = 0, irrelevant_change = 0;
993 int relevant_parents, nth_parent;
996 * If we don't do pruning, everything is interesting
998 if (!revs->prune)
999 return;
1001 if (!repo_get_commit_tree(the_repository, commit))
1002 return;
1004 if (!commit->parents) {
1006 * Pretend as if we are comparing ourselves to the
1007 * (non-existent) first parent of this commit object. Even
1008 * though no such parent exists, its changed-path Bloom filter
1009 * (if one exists) is relative to the empty tree, using Bloom
1010 * filters is allowed here.
1012 if (rev_same_tree_as_empty(revs, commit, 0))
1013 commit->object.flags |= TREESAME;
1014 return;
1018 * Normal non-merge commit? If we don't want to make the
1019 * history dense, we consider it always to be a change..
1021 if (!revs->dense && !commit->parents->next)
1022 return;
1024 for (pp = &commit->parents, nth_parent = 0, relevant_parents = 0;
1025 (parent = *pp) != NULL;
1026 pp = &parent->next, nth_parent++) {
1027 struct commit *p = parent->item;
1028 if (relevant_commit(p))
1029 relevant_parents++;
1031 if (nth_parent == 1) {
1033 * This our second loop iteration - so we now know
1034 * we're dealing with a merge.
1036 * Do not compare with later parents when we care only about
1037 * the first parent chain, in order to avoid derailing the
1038 * traversal to follow a side branch that brought everything
1039 * in the path we are limited to by the pathspec.
1041 if (revs->first_parent_only)
1042 break;
1044 * If this will remain a potentially-simplifiable
1045 * merge, remember per-parent treesame if needed.
1046 * Initialise the array with the comparison from our
1047 * first iteration.
1049 if (revs->treesame.name &&
1050 !revs->simplify_history &&
1051 !(commit->object.flags & UNINTERESTING)) {
1052 ts = initialise_treesame(revs, commit);
1053 if (!(irrelevant_change || relevant_change))
1054 ts->treesame[0] = 1;
1057 if (repo_parse_commit(revs->repo, p) < 0)
1058 die("cannot simplify commit %s (because of %s)",
1059 oid_to_hex(&commit->object.oid),
1060 oid_to_hex(&p->object.oid));
1061 switch (rev_compare_tree(revs, p, commit, nth_parent)) {
1062 case REV_TREE_SAME:
1063 if (!revs->simplify_history || !relevant_commit(p)) {
1064 /* Even if a merge with an uninteresting
1065 * side branch brought the entire change
1066 * we are interested in, we do not want
1067 * to lose the other branches of this
1068 * merge, so we just keep going.
1070 if (ts)
1071 ts->treesame[nth_parent] = 1;
1072 continue;
1075 free_commit_list(parent->next);
1076 parent->next = NULL;
1077 while (commit->parents != parent)
1078 pop_commit(&commit->parents);
1079 commit->parents = parent;
1082 * A merge commit is a "diversion" if it is not
1083 * TREESAME to its first parent but is TREESAME
1084 * to a later parent. In the simplified history,
1085 * we "divert" the history walk to the later
1086 * parent. These commits are shown when "show_pulls"
1087 * is enabled, so do not mark the object as
1088 * TREESAME here.
1090 if (!revs->show_pulls || !nth_parent)
1091 commit->object.flags |= TREESAME;
1093 return;
1095 case REV_TREE_NEW:
1096 if (revs->remove_empty_trees &&
1097 rev_same_tree_as_empty(revs, p, nth_parent)) {
1098 /* We are adding all the specified
1099 * paths from this parent, so the
1100 * history beyond this parent is not
1101 * interesting. Remove its parents
1102 * (they are grandparents for us).
1103 * IOW, we pretend this parent is a
1104 * "root" commit.
1106 if (repo_parse_commit(revs->repo, p) < 0)
1107 die("cannot simplify commit %s (invalid %s)",
1108 oid_to_hex(&commit->object.oid),
1109 oid_to_hex(&p->object.oid));
1110 free_commit_list(p->parents);
1111 p->parents = NULL;
1113 /* fallthrough */
1114 case REV_TREE_OLD:
1115 case REV_TREE_DIFFERENT:
1116 if (relevant_commit(p))
1117 relevant_change = 1;
1118 else
1119 irrelevant_change = 1;
1121 if (!nth_parent)
1122 commit->object.flags |= PULL_MERGE;
1124 continue;
1126 die("bad tree compare for commit %s", oid_to_hex(&commit->object.oid));
1130 * TREESAME is straightforward for single-parent commits. For merge
1131 * commits, it is most useful to define it so that "irrelevant"
1132 * parents cannot make us !TREESAME - if we have any relevant
1133 * parents, then we only consider TREESAMEness with respect to them,
1134 * allowing irrelevant merges from uninteresting branches to be
1135 * simplified away. Only if we have only irrelevant parents do we
1136 * base TREESAME on them. Note that this logic is replicated in
1137 * update_treesame, which should be kept in sync.
1139 if (relevant_parents ? !relevant_change : !irrelevant_change)
1140 commit->object.flags |= TREESAME;
1143 static int process_parents(struct rev_info *revs, struct commit *commit,
1144 struct commit_list **list, struct prio_queue *queue)
1146 struct commit_list *parent = commit->parents;
1147 unsigned pass_flags;
1149 if (commit->object.flags & ADDED)
1150 return 0;
1151 if (revs->do_not_die_on_missing_objects &&
1152 oidset_contains(&revs->missing_commits, &commit->object.oid))
1153 return 0;
1154 commit->object.flags |= ADDED;
1156 if (revs->include_check &&
1157 !revs->include_check(commit, revs->include_check_data))
1158 return 0;
1161 * If the commit is uninteresting, don't try to
1162 * prune parents - we want the maximal uninteresting
1163 * set.
1165 * Normally we haven't parsed the parent
1166 * yet, so we won't have a parent of a parent
1167 * here. However, it may turn out that we've
1168 * reached this commit some other way (where it
1169 * wasn't uninteresting), in which case we need
1170 * to mark its parents recursively too..
1172 if (commit->object.flags & UNINTERESTING) {
1173 while (parent) {
1174 struct commit *p = parent->item;
1175 parent = parent->next;
1176 if (p)
1177 p->object.flags |= UNINTERESTING;
1178 if (repo_parse_commit_gently(revs->repo, p, 1) < 0)
1179 continue;
1180 if (p->parents)
1181 mark_parents_uninteresting(revs, p);
1182 if (p->object.flags & SEEN)
1183 continue;
1184 p->object.flags |= (SEEN | NOT_USER_GIVEN);
1185 if (list)
1186 commit_list_insert_by_date(p, list);
1187 if (queue)
1188 prio_queue_put(queue, p);
1189 if (revs->exclude_first_parent_only)
1190 break;
1192 return 0;
1196 * Ok, the commit wasn't uninteresting. Try to
1197 * simplify the commit history and find the parent
1198 * that has no differences in the path set if one exists.
1200 try_to_simplify_commit(revs, commit);
1202 if (revs->no_walk)
1203 return 0;
1205 pass_flags = (commit->object.flags & (SYMMETRIC_LEFT | ANCESTRY_PATH));
1207 for (parent = commit->parents; parent; parent = parent->next) {
1208 struct commit *p = parent->item;
1209 int gently = revs->ignore_missing_links ||
1210 revs->exclude_promisor_objects ||
1211 revs->do_not_die_on_missing_objects;
1212 if (repo_parse_commit_gently(revs->repo, p, gently) < 0) {
1213 if (revs->exclude_promisor_objects &&
1214 is_promisor_object(&p->object.oid)) {
1215 if (revs->first_parent_only)
1216 break;
1217 continue;
1220 if (revs->do_not_die_on_missing_objects)
1221 oidset_insert(&revs->missing_commits, &p->object.oid);
1222 else
1223 return -1; /* corrupt repository */
1225 if (revs->sources) {
1226 char **slot = revision_sources_at(revs->sources, p);
1228 if (!*slot)
1229 *slot = *revision_sources_at(revs->sources, commit);
1231 p->object.flags |= pass_flags;
1232 if (!(p->object.flags & SEEN)) {
1233 p->object.flags |= (SEEN | NOT_USER_GIVEN);
1234 if (list)
1235 commit_list_insert_by_date(p, list);
1236 if (queue)
1237 prio_queue_put(queue, p);
1239 if (revs->first_parent_only)
1240 break;
1242 return 0;
1245 static void cherry_pick_list(struct commit_list *list, struct rev_info *revs)
1247 struct commit_list *p;
1248 int left_count = 0, right_count = 0;
1249 int left_first;
1250 struct patch_ids ids;
1251 unsigned cherry_flag;
1253 /* First count the commits on the left and on the right */
1254 for (p = list; p; p = p->next) {
1255 struct commit *commit = p->item;
1256 unsigned flags = commit->object.flags;
1257 if (flags & BOUNDARY)
1259 else if (flags & SYMMETRIC_LEFT)
1260 left_count++;
1261 else
1262 right_count++;
1265 if (!left_count || !right_count)
1266 return;
1268 left_first = left_count < right_count;
1269 init_patch_ids(revs->repo, &ids);
1270 ids.diffopts.pathspec = revs->diffopt.pathspec;
1272 /* Compute patch-ids for one side */
1273 for (p = list; p; p = p->next) {
1274 struct commit *commit = p->item;
1275 unsigned flags = commit->object.flags;
1277 if (flags & BOUNDARY)
1278 continue;
1280 * If we have fewer left, left_first is set and we omit
1281 * commits on the right branch in this loop. If we have
1282 * fewer right, we skip the left ones.
1284 if (left_first != !!(flags & SYMMETRIC_LEFT))
1285 continue;
1286 add_commit_patch_id(commit, &ids);
1289 /* either cherry_mark or cherry_pick are true */
1290 cherry_flag = revs->cherry_mark ? PATCHSAME : SHOWN;
1292 /* Check the other side */
1293 for (p = list; p; p = p->next) {
1294 struct commit *commit = p->item;
1295 struct patch_id *id;
1296 unsigned flags = commit->object.flags;
1298 if (flags & BOUNDARY)
1299 continue;
1301 * If we have fewer left, left_first is set and we omit
1302 * commits on the left branch in this loop.
1304 if (left_first == !!(flags & SYMMETRIC_LEFT))
1305 continue;
1308 * Have we seen the same patch id?
1310 id = patch_id_iter_first(commit, &ids);
1311 if (!id)
1312 continue;
1314 commit->object.flags |= cherry_flag;
1315 do {
1316 id->commit->object.flags |= cherry_flag;
1317 } while ((id = patch_id_iter_next(id, &ids)));
1320 free_patch_ids(&ids);
1323 /* How many extra uninteresting commits we want to see.. */
1324 #define SLOP 5
1326 static int still_interesting(struct commit_list *src, timestamp_t date, int slop,
1327 struct commit **interesting_cache)
1330 * No source list at all? We're definitely done..
1332 if (!src)
1333 return 0;
1336 * Does the destination list contain entries with a date
1337 * before the source list? Definitely _not_ done.
1339 if (date <= src->item->date)
1340 return SLOP;
1343 * Does the source list still have interesting commits in
1344 * it? Definitely not done..
1346 if (!everybody_uninteresting(src, interesting_cache))
1347 return SLOP;
1349 /* Ok, we're closing in.. */
1350 return slop-1;
1354 * "rev-list --ancestry-path=C_0 [--ancestry-path=C_1 ...] A..B"
1355 * computes commits that are ancestors of B but not ancestors of A but
1356 * further limits the result to those that have any of C in their
1357 * ancestry path (i.e. are either ancestors of any of C, descendants
1358 * of any of C, or are any of C). If --ancestry-path is specified with
1359 * no commit, we use all bottom commits for C.
1361 * Before this function is called, ancestors of C will have already
1362 * been marked with ANCESTRY_PATH previously.
1364 * This takes the list of bottom commits and the result of "A..B"
1365 * without --ancestry-path, and limits the latter further to the ones
1366 * that have any of C in their ancestry path. Since the ancestors of C
1367 * have already been marked (a prerequisite of this function), we just
1368 * need to mark the descendants, then exclude any commit that does not
1369 * have any of these marks.
1371 static void limit_to_ancestry(struct commit_list *bottoms, struct commit_list *list)
1373 struct commit_list *p;
1374 struct commit_list *rlist = NULL;
1375 int made_progress;
1378 * Reverse the list so that it will be likely that we would
1379 * process parents before children.
1381 for (p = list; p; p = p->next)
1382 commit_list_insert(p->item, &rlist);
1384 for (p = bottoms; p; p = p->next)
1385 p->item->object.flags |= TMP_MARK;
1388 * Mark the ones that can reach bottom commits in "list",
1389 * in a bottom-up fashion.
1391 do {
1392 made_progress = 0;
1393 for (p = rlist; p; p = p->next) {
1394 struct commit *c = p->item;
1395 struct commit_list *parents;
1396 if (c->object.flags & (TMP_MARK | UNINTERESTING))
1397 continue;
1398 for (parents = c->parents;
1399 parents;
1400 parents = parents->next) {
1401 if (!(parents->item->object.flags & TMP_MARK))
1402 continue;
1403 c->object.flags |= TMP_MARK;
1404 made_progress = 1;
1405 break;
1408 } while (made_progress);
1411 * NEEDSWORK: decide if we want to remove parents that are
1412 * not marked with TMP_MARK from commit->parents for commits
1413 * in the resulting list. We may not want to do that, though.
1417 * The ones that are not marked with either TMP_MARK or
1418 * ANCESTRY_PATH are uninteresting
1420 for (p = list; p; p = p->next) {
1421 struct commit *c = p->item;
1422 if (c->object.flags & (TMP_MARK | ANCESTRY_PATH))
1423 continue;
1424 c->object.flags |= UNINTERESTING;
1427 /* We are done with TMP_MARK and ANCESTRY_PATH */
1428 for (p = list; p; p = p->next)
1429 p->item->object.flags &= ~(TMP_MARK | ANCESTRY_PATH);
1430 for (p = bottoms; p; p = p->next)
1431 p->item->object.flags &= ~(TMP_MARK | ANCESTRY_PATH);
1432 free_commit_list(rlist);
1436 * Before walking the history, add the set of "negative" refs the
1437 * caller has asked to exclude to the bottom list.
1439 * This is used to compute "rev-list --ancestry-path A..B", as we need
1440 * to filter the result of "A..B" further to the ones that can actually
1441 * reach A.
1443 static void collect_bottom_commits(struct commit_list *list,
1444 struct commit_list **bottom)
1446 struct commit_list *elem;
1447 for (elem = list; elem; elem = elem->next)
1448 if (elem->item->object.flags & BOTTOM)
1449 commit_list_insert(elem->item, bottom);
1452 /* Assumes either left_only or right_only is set */
1453 static void limit_left_right(struct commit_list *list, struct rev_info *revs)
1455 struct commit_list *p;
1457 for (p = list; p; p = p->next) {
1458 struct commit *commit = p->item;
1460 if (revs->right_only) {
1461 if (commit->object.flags & SYMMETRIC_LEFT)
1462 commit->object.flags |= SHOWN;
1463 } else /* revs->left_only is set */
1464 if (!(commit->object.flags & SYMMETRIC_LEFT))
1465 commit->object.flags |= SHOWN;
1469 static int limit_list(struct rev_info *revs)
1471 int slop = SLOP;
1472 timestamp_t date = TIME_MAX;
1473 struct commit_list *original_list = revs->commits;
1474 struct commit_list *newlist = NULL;
1475 struct commit_list **p = &newlist;
1476 struct commit *interesting_cache = NULL;
1478 if (revs->ancestry_path_implicit_bottoms) {
1479 collect_bottom_commits(original_list,
1480 &revs->ancestry_path_bottoms);
1481 if (!revs->ancestry_path_bottoms)
1482 die("--ancestry-path given but there are no bottom commits");
1485 while (original_list) {
1486 struct commit *commit = pop_commit(&original_list);
1487 struct object *obj = &commit->object;
1488 show_early_output_fn_t show;
1490 if (commit == interesting_cache)
1491 interesting_cache = NULL;
1493 if (revs->max_age != -1 && (commit->date < revs->max_age))
1494 obj->flags |= UNINTERESTING;
1495 if (process_parents(revs, commit, &original_list, NULL) < 0)
1496 return -1;
1497 if (obj->flags & UNINTERESTING) {
1498 mark_parents_uninteresting(revs, commit);
1499 slop = still_interesting(original_list, date, slop, &interesting_cache);
1500 if (slop)
1501 continue;
1502 break;
1504 if (revs->min_age != -1 && (commit->date > revs->min_age) &&
1505 !revs->line_level_traverse)
1506 continue;
1507 if (revs->max_age_as_filter != -1 &&
1508 (commit->date < revs->max_age_as_filter) && !revs->line_level_traverse)
1509 continue;
1510 date = commit->date;
1511 p = &commit_list_insert(commit, p)->next;
1513 show = show_early_output;
1514 if (!show)
1515 continue;
1517 show(revs, newlist);
1518 show_early_output = NULL;
1520 if (revs->cherry_pick || revs->cherry_mark)
1521 cherry_pick_list(newlist, revs);
1523 if (revs->left_only || revs->right_only)
1524 limit_left_right(newlist, revs);
1526 if (revs->ancestry_path)
1527 limit_to_ancestry(revs->ancestry_path_bottoms, newlist);
1530 * Check if any commits have become TREESAME by some of their parents
1531 * becoming UNINTERESTING.
1533 if (limiting_can_increase_treesame(revs)) {
1534 struct commit_list *list = NULL;
1535 for (list = newlist; list; list = list->next) {
1536 struct commit *c = list->item;
1537 if (c->object.flags & (UNINTERESTING | TREESAME))
1538 continue;
1539 update_treesame(revs, c);
1543 free_commit_list(original_list);
1544 revs->commits = newlist;
1545 return 0;
1549 * Add an entry to refs->cmdline with the specified information.
1550 * *name is copied.
1552 static void add_rev_cmdline(struct rev_info *revs,
1553 struct object *item,
1554 const char *name,
1555 int whence,
1556 unsigned flags)
1558 struct rev_cmdline_info *info = &revs->cmdline;
1559 unsigned int nr = info->nr;
1561 ALLOC_GROW(info->rev, nr + 1, info->alloc);
1562 info->rev[nr].item = item;
1563 info->rev[nr].name = xstrdup(name);
1564 info->rev[nr].whence = whence;
1565 info->rev[nr].flags = flags;
1566 info->nr++;
1569 static void add_rev_cmdline_list(struct rev_info *revs,
1570 struct commit_list *commit_list,
1571 int whence,
1572 unsigned flags)
1574 while (commit_list) {
1575 struct object *object = &commit_list->item->object;
1576 add_rev_cmdline(revs, object, oid_to_hex(&object->oid),
1577 whence, flags);
1578 commit_list = commit_list->next;
1582 int ref_excluded(const struct ref_exclusions *exclusions, const char *path)
1584 const char *stripped_path = strip_namespace(path);
1585 struct string_list_item *item;
1587 for_each_string_list_item(item, &exclusions->excluded_refs) {
1588 if (!wildmatch(item->string, path, 0))
1589 return 1;
1592 if (ref_is_hidden(stripped_path, path, &exclusions->hidden_refs))
1593 return 1;
1595 return 0;
1598 void init_ref_exclusions(struct ref_exclusions *exclusions)
1600 struct ref_exclusions blank = REF_EXCLUSIONS_INIT;
1601 memcpy(exclusions, &blank, sizeof(*exclusions));
1604 void clear_ref_exclusions(struct ref_exclusions *exclusions)
1606 string_list_clear(&exclusions->excluded_refs, 0);
1607 strvec_clear(&exclusions->hidden_refs);
1608 exclusions->hidden_refs_configured = 0;
1611 void add_ref_exclusion(struct ref_exclusions *exclusions, const char *exclude)
1613 string_list_append(&exclusions->excluded_refs, exclude);
1616 struct exclude_hidden_refs_cb {
1617 struct ref_exclusions *exclusions;
1618 const char *section;
1621 static int hide_refs_config(const char *var, const char *value,
1622 const struct config_context *ctx UNUSED,
1623 void *cb_data)
1625 struct exclude_hidden_refs_cb *cb = cb_data;
1626 cb->exclusions->hidden_refs_configured = 1;
1627 return parse_hide_refs_config(var, value, cb->section,
1628 &cb->exclusions->hidden_refs);
1631 void exclude_hidden_refs(struct ref_exclusions *exclusions, const char *section)
1633 struct exclude_hidden_refs_cb cb;
1635 if (strcmp(section, "fetch") && strcmp(section, "receive") &&
1636 strcmp(section, "uploadpack"))
1637 die(_("unsupported section for hidden refs: %s"), section);
1639 if (exclusions->hidden_refs_configured)
1640 die(_("--exclude-hidden= passed more than once"));
1642 cb.exclusions = exclusions;
1643 cb.section = section;
1645 git_config(hide_refs_config, &cb);
1648 struct all_refs_cb {
1649 int all_flags;
1650 int warned_bad_reflog;
1651 struct rev_info *all_revs;
1652 const char *name_for_errormsg;
1653 struct worktree *wt;
1656 static int handle_one_ref(const char *path, const char *referent UNUSED, const struct object_id *oid,
1657 int flag UNUSED,
1658 void *cb_data)
1660 struct all_refs_cb *cb = cb_data;
1661 struct object *object;
1663 if (ref_excluded(&cb->all_revs->ref_excludes, path))
1664 return 0;
1666 object = get_reference(cb->all_revs, path, oid, cb->all_flags);
1667 add_rev_cmdline(cb->all_revs, object, path, REV_CMD_REF, cb->all_flags);
1668 add_pending_object(cb->all_revs, object, path);
1669 return 0;
1672 static void init_all_refs_cb(struct all_refs_cb *cb, struct rev_info *revs,
1673 unsigned flags)
1675 cb->all_revs = revs;
1676 cb->all_flags = flags;
1677 revs->rev_input_given = 1;
1678 cb->wt = NULL;
1681 static void handle_refs(struct ref_store *refs,
1682 struct rev_info *revs, unsigned flags,
1683 int (*for_each)(struct ref_store *, each_ref_fn, void *))
1685 struct all_refs_cb cb;
1687 if (!refs) {
1688 /* this could happen with uninitialized submodules */
1689 return;
1692 init_all_refs_cb(&cb, revs, flags);
1693 for_each(refs, handle_one_ref, &cb);
1696 static void handle_one_reflog_commit(struct object_id *oid, void *cb_data)
1698 struct all_refs_cb *cb = cb_data;
1699 if (!is_null_oid(oid)) {
1700 struct object *o = parse_object(cb->all_revs->repo, oid);
1701 if (o) {
1702 o->flags |= cb->all_flags;
1703 /* ??? CMDLINEFLAGS ??? */
1704 add_pending_object(cb->all_revs, o, "");
1706 else if (!cb->warned_bad_reflog) {
1707 warning("reflog of '%s' references pruned commits",
1708 cb->name_for_errormsg);
1709 cb->warned_bad_reflog = 1;
1714 static int handle_one_reflog_ent(struct object_id *ooid, struct object_id *noid,
1715 const char *email UNUSED,
1716 timestamp_t timestamp UNUSED,
1717 int tz UNUSED,
1718 const char *message UNUSED,
1719 void *cb_data)
1721 handle_one_reflog_commit(ooid, cb_data);
1722 handle_one_reflog_commit(noid, cb_data);
1723 return 0;
1726 static int handle_one_reflog(const char *refname_in_wt, void *cb_data)
1728 struct all_refs_cb *cb = cb_data;
1729 struct strbuf refname = STRBUF_INIT;
1731 cb->warned_bad_reflog = 0;
1732 strbuf_worktree_ref(cb->wt, &refname, refname_in_wt);
1733 cb->name_for_errormsg = refname.buf;
1734 refs_for_each_reflog_ent(get_main_ref_store(the_repository),
1735 refname.buf,
1736 handle_one_reflog_ent, cb_data);
1737 strbuf_release(&refname);
1738 return 0;
1741 static void add_other_reflogs_to_pending(struct all_refs_cb *cb)
1743 struct worktree **worktrees, **p;
1745 worktrees = get_worktrees();
1746 for (p = worktrees; *p; p++) {
1747 struct worktree *wt = *p;
1749 if (wt->is_current)
1750 continue;
1752 cb->wt = wt;
1753 refs_for_each_reflog(get_worktree_ref_store(wt),
1754 handle_one_reflog,
1755 cb);
1757 free_worktrees(worktrees);
1760 void add_reflogs_to_pending(struct rev_info *revs, unsigned flags)
1762 struct all_refs_cb cb;
1764 cb.all_revs = revs;
1765 cb.all_flags = flags;
1766 cb.wt = NULL;
1767 refs_for_each_reflog(get_main_ref_store(the_repository),
1768 handle_one_reflog, &cb);
1770 if (!revs->single_worktree)
1771 add_other_reflogs_to_pending(&cb);
1774 static void add_cache_tree(struct cache_tree *it, struct rev_info *revs,
1775 struct strbuf *path, unsigned int flags)
1777 size_t baselen = path->len;
1778 int i;
1780 if (it->entry_count >= 0) {
1781 struct tree *tree = lookup_tree(revs->repo, &it->oid);
1782 tree->object.flags |= flags;
1783 add_pending_object_with_path(revs, &tree->object, "",
1784 040000, path->buf);
1787 for (i = 0; i < it->subtree_nr; i++) {
1788 struct cache_tree_sub *sub = it->down[i];
1789 strbuf_addf(path, "%s%s", baselen ? "/" : "", sub->name);
1790 add_cache_tree(sub->cache_tree, revs, path, flags);
1791 strbuf_setlen(path, baselen);
1796 static void add_resolve_undo_to_pending(struct index_state *istate, struct rev_info *revs)
1798 struct string_list_item *item;
1799 struct string_list *resolve_undo = istate->resolve_undo;
1801 if (!resolve_undo)
1802 return;
1804 for_each_string_list_item(item, resolve_undo) {
1805 const char *path = item->string;
1806 struct resolve_undo_info *ru = item->util;
1807 int i;
1809 if (!ru)
1810 continue;
1811 for (i = 0; i < 3; i++) {
1812 struct blob *blob;
1814 if (!ru->mode[i] || !S_ISREG(ru->mode[i]))
1815 continue;
1817 blob = lookup_blob(revs->repo, &ru->oid[i]);
1818 if (!blob) {
1819 warning(_("resolve-undo records `%s` which is missing"),
1820 oid_to_hex(&ru->oid[i]));
1821 continue;
1823 add_pending_object_with_path(revs, &blob->object, "",
1824 ru->mode[i], path);
1829 static void do_add_index_objects_to_pending(struct rev_info *revs,
1830 struct index_state *istate,
1831 unsigned int flags)
1833 int i;
1835 /* TODO: audit for interaction with sparse-index. */
1836 ensure_full_index(istate);
1837 for (i = 0; i < istate->cache_nr; i++) {
1838 struct cache_entry *ce = istate->cache[i];
1839 struct blob *blob;
1841 if (S_ISGITLINK(ce->ce_mode))
1842 continue;
1844 blob = lookup_blob(revs->repo, &ce->oid);
1845 if (!blob)
1846 die("unable to add index blob to traversal");
1847 blob->object.flags |= flags;
1848 add_pending_object_with_path(revs, &blob->object, "",
1849 ce->ce_mode, ce->name);
1852 if (istate->cache_tree) {
1853 struct strbuf path = STRBUF_INIT;
1854 add_cache_tree(istate->cache_tree, revs, &path, flags);
1855 strbuf_release(&path);
1858 add_resolve_undo_to_pending(istate, revs);
1861 void add_index_objects_to_pending(struct rev_info *revs, unsigned int flags)
1863 struct worktree **worktrees, **p;
1865 repo_read_index(revs->repo);
1866 do_add_index_objects_to_pending(revs, revs->repo->index, flags);
1868 if (revs->single_worktree)
1869 return;
1871 worktrees = get_worktrees();
1872 for (p = worktrees; *p; p++) {
1873 struct worktree *wt = *p;
1874 struct index_state istate = INDEX_STATE_INIT(revs->repo);
1876 if (wt->is_current)
1877 continue; /* current index already taken care of */
1879 if (read_index_from(&istate,
1880 worktree_git_path(the_repository, wt, "index"),
1881 get_worktree_git_dir(wt)) > 0)
1882 do_add_index_objects_to_pending(revs, &istate, flags);
1883 discard_index(&istate);
1885 free_worktrees(worktrees);
1888 struct add_alternate_refs_data {
1889 struct rev_info *revs;
1890 unsigned int flags;
1893 static void add_one_alternate_ref(const struct object_id *oid,
1894 void *vdata)
1896 const char *name = ".alternate";
1897 struct add_alternate_refs_data *data = vdata;
1898 struct object *obj;
1900 obj = get_reference(data->revs, name, oid, data->flags);
1901 add_rev_cmdline(data->revs, obj, name, REV_CMD_REV, data->flags);
1902 add_pending_object(data->revs, obj, name);
1905 static void add_alternate_refs_to_pending(struct rev_info *revs,
1906 unsigned int flags)
1908 struct add_alternate_refs_data data;
1909 data.revs = revs;
1910 data.flags = flags;
1911 for_each_alternate_ref(add_one_alternate_ref, &data);
1914 static int add_parents_only(struct rev_info *revs, const char *arg_, int flags,
1915 int exclude_parent)
1917 struct object_id oid;
1918 struct object *it;
1919 struct commit *commit;
1920 struct commit_list *parents;
1921 int parent_number;
1922 const char *arg = arg_;
1924 if (*arg == '^') {
1925 flags ^= UNINTERESTING | BOTTOM;
1926 arg++;
1928 if (repo_get_oid_committish(the_repository, arg, &oid))
1929 return 0;
1930 while (1) {
1931 it = get_reference(revs, arg, &oid, 0);
1932 if (!it && revs->ignore_missing)
1933 return 0;
1934 if (it->type != OBJ_TAG)
1935 break;
1936 if (!((struct tag*)it)->tagged)
1937 return 0;
1938 oidcpy(&oid, &((struct tag*)it)->tagged->oid);
1940 if (it->type != OBJ_COMMIT)
1941 return 0;
1942 commit = (struct commit *)it;
1943 if (exclude_parent &&
1944 exclude_parent > commit_list_count(commit->parents))
1945 return 0;
1946 for (parents = commit->parents, parent_number = 1;
1947 parents;
1948 parents = parents->next, parent_number++) {
1949 if (exclude_parent && parent_number != exclude_parent)
1950 continue;
1952 it = &parents->item->object;
1953 it->flags |= flags;
1954 add_rev_cmdline(revs, it, arg_, REV_CMD_PARENTS_ONLY, flags);
1955 add_pending_object(revs, it, arg);
1957 return 1;
1960 void repo_init_revisions(struct repository *r,
1961 struct rev_info *revs,
1962 const char *prefix)
1964 struct rev_info blank = REV_INFO_INIT;
1965 memcpy(revs, &blank, sizeof(*revs));
1967 revs->repo = r;
1968 revs->pruning.repo = r;
1969 revs->pruning.add_remove = file_add_remove;
1970 revs->pruning.change = file_change;
1971 revs->pruning.change_fn_data = revs;
1972 revs->prefix = prefix;
1974 grep_init(&revs->grep_filter, revs->repo);
1975 revs->grep_filter.status_only = 1;
1977 repo_diff_setup(revs->repo, &revs->diffopt);
1978 if (prefix && !revs->diffopt.prefix) {
1979 revs->diffopt.prefix = prefix;
1980 revs->diffopt.prefix_length = strlen(prefix);
1983 init_display_notes(&revs->notes_opt);
1984 list_objects_filter_init(&revs->filter);
1985 init_ref_exclusions(&revs->ref_excludes);
1986 oidset_init(&revs->missing_commits, 0);
1989 static void add_pending_commit_list(struct rev_info *revs,
1990 struct commit_list *commit_list,
1991 unsigned int flags)
1993 while (commit_list) {
1994 struct object *object = &commit_list->item->object;
1995 object->flags |= flags;
1996 add_pending_object(revs, object, oid_to_hex(&object->oid));
1997 commit_list = commit_list->next;
2001 static const char *lookup_other_head(struct object_id *oid)
2003 int i;
2004 static const char *const other_head[] = {
2005 "MERGE_HEAD", "CHERRY_PICK_HEAD", "REVERT_HEAD", "REBASE_HEAD"
2008 for (i = 0; i < ARRAY_SIZE(other_head); i++)
2009 if (!refs_read_ref_full(get_main_ref_store(the_repository), other_head[i],
2010 RESOLVE_REF_READING | RESOLVE_REF_NO_RECURSE,
2011 oid, NULL)) {
2012 if (is_null_oid(oid))
2013 die(_("%s exists but is a symbolic ref"), other_head[i]);
2014 return other_head[i];
2017 die(_("--merge requires one of the pseudorefs MERGE_HEAD, CHERRY_PICK_HEAD, REVERT_HEAD or REBASE_HEAD"));
2020 static void prepare_show_merge(struct rev_info *revs)
2022 struct commit_list *bases = NULL;
2023 struct commit *head, *other;
2024 struct object_id oid;
2025 const char *other_name;
2026 const char **prune = NULL;
2027 int i, prune_num = 1; /* counting terminating NULL */
2028 struct index_state *istate = revs->repo->index;
2030 if (repo_get_oid(the_repository, "HEAD", &oid))
2031 die("--merge without HEAD?");
2032 head = lookup_commit_or_die(&oid, "HEAD");
2033 other_name = lookup_other_head(&oid);
2034 other = lookup_commit_or_die(&oid, other_name);
2035 add_pending_object(revs, &head->object, "HEAD");
2036 add_pending_object(revs, &other->object, other_name);
2037 if (repo_get_merge_bases(the_repository, head, other, &bases) < 0)
2038 exit(128);
2039 add_rev_cmdline_list(revs, bases, REV_CMD_MERGE_BASE, UNINTERESTING | BOTTOM);
2040 add_pending_commit_list(revs, bases, UNINTERESTING | BOTTOM);
2041 free_commit_list(bases);
2042 head->object.flags |= SYMMETRIC_LEFT;
2044 if (!istate->cache_nr)
2045 repo_read_index(revs->repo);
2046 for (i = 0; i < istate->cache_nr; i++) {
2047 const struct cache_entry *ce = istate->cache[i];
2048 if (!ce_stage(ce))
2049 continue;
2050 if (ce_path_match(istate, ce, &revs->prune_data, NULL)) {
2051 prune_num++;
2052 REALLOC_ARRAY(prune, prune_num);
2053 prune[prune_num-2] = ce->name;
2054 prune[prune_num-1] = NULL;
2056 while ((i+1 < istate->cache_nr) &&
2057 ce_same_name(ce, istate->cache[i+1]))
2058 i++;
2060 clear_pathspec(&revs->prune_data);
2061 parse_pathspec(&revs->prune_data, PATHSPEC_ALL_MAGIC & ~PATHSPEC_LITERAL,
2062 PATHSPEC_PREFER_FULL | PATHSPEC_LITERAL_PATH, "", prune);
2063 revs->limited = 1;
2066 static int dotdot_missing(const char *arg, char *dotdot,
2067 struct rev_info *revs, int symmetric)
2069 if (revs->ignore_missing)
2070 return 0;
2071 /* de-munge so we report the full argument */
2072 *dotdot = '.';
2073 die(symmetric
2074 ? "Invalid symmetric difference expression %s"
2075 : "Invalid revision range %s", arg);
2078 static int handle_dotdot_1(const char *arg, char *dotdot,
2079 struct rev_info *revs, int flags,
2080 int cant_be_filename,
2081 struct object_context *a_oc,
2082 struct object_context *b_oc)
2084 const char *a_name, *b_name;
2085 struct object_id a_oid, b_oid;
2086 struct object *a_obj, *b_obj;
2087 unsigned int a_flags, b_flags;
2088 int symmetric = 0;
2089 unsigned int flags_exclude = flags ^ (UNINTERESTING | BOTTOM);
2090 unsigned int oc_flags = GET_OID_COMMITTISH | GET_OID_RECORD_PATH;
2092 a_name = arg;
2093 if (!*a_name)
2094 a_name = "HEAD";
2096 b_name = dotdot + 2;
2097 if (*b_name == '.') {
2098 symmetric = 1;
2099 b_name++;
2101 if (!*b_name)
2102 b_name = "HEAD";
2104 if (get_oid_with_context(revs->repo, a_name, oc_flags, &a_oid, a_oc) ||
2105 get_oid_with_context(revs->repo, b_name, oc_flags, &b_oid, b_oc))
2106 return -1;
2108 if (!cant_be_filename) {
2109 *dotdot = '.';
2110 verify_non_filename(revs->prefix, arg);
2111 *dotdot = '\0';
2114 a_obj = parse_object(revs->repo, &a_oid);
2115 b_obj = parse_object(revs->repo, &b_oid);
2116 if (!a_obj || !b_obj)
2117 return dotdot_missing(arg, dotdot, revs, symmetric);
2119 if (!symmetric) {
2120 /* just A..B */
2121 b_flags = flags;
2122 a_flags = flags_exclude;
2123 } else {
2124 /* A...B -- find merge bases between the two */
2125 struct commit *a, *b;
2126 struct commit_list *exclude = NULL;
2128 a = lookup_commit_reference(revs->repo, &a_obj->oid);
2129 b = lookup_commit_reference(revs->repo, &b_obj->oid);
2130 if (!a || !b)
2131 return dotdot_missing(arg, dotdot, revs, symmetric);
2133 if (repo_get_merge_bases(the_repository, a, b, &exclude) < 0) {
2134 free_commit_list(exclude);
2135 return -1;
2137 add_rev_cmdline_list(revs, exclude, REV_CMD_MERGE_BASE,
2138 flags_exclude);
2139 add_pending_commit_list(revs, exclude, flags_exclude);
2140 free_commit_list(exclude);
2142 b_flags = flags;
2143 a_flags = flags | SYMMETRIC_LEFT;
2146 a_obj->flags |= a_flags;
2147 b_obj->flags |= b_flags;
2148 add_rev_cmdline(revs, a_obj, a_name, REV_CMD_LEFT, a_flags);
2149 add_rev_cmdline(revs, b_obj, b_name, REV_CMD_RIGHT, b_flags);
2150 add_pending_object_with_path(revs, a_obj, a_name, a_oc->mode, a_oc->path);
2151 add_pending_object_with_path(revs, b_obj, b_name, b_oc->mode, b_oc->path);
2152 return 0;
2155 static int handle_dotdot(const char *arg,
2156 struct rev_info *revs, int flags,
2157 int cant_be_filename)
2159 struct object_context a_oc = {0}, b_oc = {0};
2160 char *dotdot = strstr(arg, "..");
2161 int ret;
2163 if (!dotdot)
2164 return -1;
2166 *dotdot = '\0';
2167 ret = handle_dotdot_1(arg, dotdot, revs, flags, cant_be_filename,
2168 &a_oc, &b_oc);
2169 *dotdot = '.';
2171 object_context_release(&a_oc);
2172 object_context_release(&b_oc);
2173 return ret;
2176 static int handle_revision_arg_1(const char *arg_, struct rev_info *revs, int flags, unsigned revarg_opt)
2178 struct object_context oc = {0};
2179 char *mark;
2180 struct object *object;
2181 struct object_id oid;
2182 int local_flags;
2183 const char *arg = arg_;
2184 int cant_be_filename = revarg_opt & REVARG_CANNOT_BE_FILENAME;
2185 unsigned get_sha1_flags = GET_OID_RECORD_PATH;
2186 int ret;
2188 flags = flags & UNINTERESTING ? flags | BOTTOM : flags & ~BOTTOM;
2190 if (!cant_be_filename && !strcmp(arg, "..")) {
2192 * Just ".."? That is not a range but the
2193 * pathspec for the parent directory.
2195 ret = -1;
2196 goto out;
2199 if (!handle_dotdot(arg, revs, flags, revarg_opt)) {
2200 ret = 0;
2201 goto out;
2204 mark = strstr(arg, "^@");
2205 if (mark && !mark[2]) {
2206 *mark = 0;
2207 if (add_parents_only(revs, arg, flags, 0)) {
2208 ret = 0;
2209 goto out;
2211 *mark = '^';
2213 mark = strstr(arg, "^!");
2214 if (mark && !mark[2]) {
2215 *mark = 0;
2216 if (!add_parents_only(revs, arg, flags ^ (UNINTERESTING | BOTTOM), 0))
2217 *mark = '^';
2219 mark = strstr(arg, "^-");
2220 if (mark) {
2221 int exclude_parent = 1;
2223 if (mark[2]) {
2224 if (strtol_i(mark + 2, 10, &exclude_parent) ||
2225 exclude_parent < 1) {
2226 ret = -1;
2227 goto out;
2231 *mark = 0;
2232 if (!add_parents_only(revs, arg, flags ^ (UNINTERESTING | BOTTOM), exclude_parent))
2233 *mark = '^';
2236 local_flags = 0;
2237 if (*arg == '^') {
2238 local_flags = UNINTERESTING | BOTTOM;
2239 arg++;
2242 if (revarg_opt & REVARG_COMMITTISH)
2243 get_sha1_flags |= GET_OID_COMMITTISH;
2246 * Even if revs->do_not_die_on_missing_objects is set, we
2247 * should error out if we can't even get an oid, as
2248 * `--missing=print` should be able to report missing oids.
2250 if (get_oid_with_context(revs->repo, arg, get_sha1_flags, &oid, &oc)) {
2251 ret = revs->ignore_missing ? 0 : -1;
2252 goto out;
2254 if (!cant_be_filename)
2255 verify_non_filename(revs->prefix, arg);
2256 object = get_reference(revs, arg, &oid, flags ^ local_flags);
2257 if (!object) {
2258 ret = (revs->ignore_missing || revs->do_not_die_on_missing_objects) ? 0 : -1;
2259 goto out;
2261 add_rev_cmdline(revs, object, arg_, REV_CMD_REV, flags ^ local_flags);
2262 add_pending_object_with_path(revs, object, arg, oc.mode, oc.path);
2264 ret = 0;
2266 out:
2267 object_context_release(&oc);
2268 return ret;
2271 int handle_revision_arg(const char *arg, struct rev_info *revs, int flags, unsigned revarg_opt)
2273 int ret = handle_revision_arg_1(arg, revs, flags, revarg_opt);
2274 if (!ret)
2275 revs->rev_input_given = 1;
2276 return ret;
2279 static void read_pathspec_from_stdin(struct strbuf *sb,
2280 struct strvec *prune)
2282 while (strbuf_getline(sb, stdin) != EOF)
2283 strvec_push(prune, sb->buf);
2286 static void add_grep(struct rev_info *revs, const char *ptn, enum grep_pat_token what)
2288 append_grep_pattern(&revs->grep_filter, ptn, "command line", 0, what);
2291 static void add_header_grep(struct rev_info *revs, enum grep_header_field field, const char *pattern)
2293 append_header_grep_pattern(&revs->grep_filter, field, pattern);
2296 static void add_message_grep(struct rev_info *revs, const char *pattern)
2298 add_grep(revs, pattern, GREP_PATTERN_BODY);
2301 static int parse_count(const char *arg)
2303 int count;
2305 if (strtol_i(arg, 10, &count) < 0)
2306 die("'%s': not an integer", arg);
2307 return count;
2310 static timestamp_t parse_age(const char *arg)
2312 timestamp_t num;
2313 char *p;
2315 errno = 0;
2316 num = parse_timestamp(arg, &p, 10);
2317 if (errno || *p || p == arg)
2318 die("'%s': not a number of seconds since epoch", arg);
2319 return num;
2322 static int handle_revision_opt(struct rev_info *revs, int argc, const char **argv,
2323 int *unkc, const char **unkv,
2324 const struct setup_revision_opt* opt)
2326 const char *arg = argv[0];
2327 const char *optarg = NULL;
2328 int argcount;
2329 const unsigned hexsz = the_hash_algo->hexsz;
2331 /* pseudo revision arguments */
2332 if (!strcmp(arg, "--all") || !strcmp(arg, "--branches") ||
2333 !strcmp(arg, "--tags") || !strcmp(arg, "--remotes") ||
2334 !strcmp(arg, "--reflog") || !strcmp(arg, "--not") ||
2335 !strcmp(arg, "--no-walk") || !strcmp(arg, "--do-walk") ||
2336 !strcmp(arg, "--bisect") || starts_with(arg, "--glob=") ||
2337 !strcmp(arg, "--indexed-objects") ||
2338 !strcmp(arg, "--alternate-refs") ||
2339 starts_with(arg, "--exclude=") || starts_with(arg, "--exclude-hidden=") ||
2340 starts_with(arg, "--branches=") || starts_with(arg, "--tags=") ||
2341 starts_with(arg, "--remotes=") || starts_with(arg, "--no-walk="))
2343 unkv[(*unkc)++] = arg;
2344 return 1;
2347 if ((argcount = parse_long_opt("max-count", argv, &optarg))) {
2348 revs->max_count = parse_count(optarg);
2349 revs->no_walk = 0;
2350 return argcount;
2351 } else if ((argcount = parse_long_opt("skip", argv, &optarg))) {
2352 revs->skip_count = parse_count(optarg);
2353 return argcount;
2354 } else if ((*arg == '-') && isdigit(arg[1])) {
2355 /* accept -<digit>, like traditional "head" */
2356 revs->max_count = parse_count(arg + 1);
2357 revs->no_walk = 0;
2358 } else if (!strcmp(arg, "-n")) {
2359 if (argc <= 1)
2360 return error("-n requires an argument");
2361 revs->max_count = parse_count(argv[1]);
2362 revs->no_walk = 0;
2363 return 2;
2364 } else if (skip_prefix(arg, "-n", &optarg)) {
2365 revs->max_count = parse_count(optarg);
2366 revs->no_walk = 0;
2367 } else if ((argcount = parse_long_opt("max-age", argv, &optarg))) {
2368 revs->max_age = parse_age(optarg);
2369 return argcount;
2370 } else if ((argcount = parse_long_opt("since", argv, &optarg))) {
2371 revs->max_age = approxidate(optarg);
2372 return argcount;
2373 } else if ((argcount = parse_long_opt("since-as-filter", argv, &optarg))) {
2374 revs->max_age_as_filter = approxidate(optarg);
2375 return argcount;
2376 } else if ((argcount = parse_long_opt("after", argv, &optarg))) {
2377 revs->max_age = approxidate(optarg);
2378 return argcount;
2379 } else if ((argcount = parse_long_opt("min-age", argv, &optarg))) {
2380 revs->min_age = parse_age(optarg);
2381 return argcount;
2382 } else if ((argcount = parse_long_opt("before", argv, &optarg))) {
2383 revs->min_age = approxidate(optarg);
2384 return argcount;
2385 } else if ((argcount = parse_long_opt("until", argv, &optarg))) {
2386 revs->min_age = approxidate(optarg);
2387 return argcount;
2388 } else if (!strcmp(arg, "--first-parent")) {
2389 revs->first_parent_only = 1;
2390 } else if (!strcmp(arg, "--exclude-first-parent-only")) {
2391 revs->exclude_first_parent_only = 1;
2392 } else if (!strcmp(arg, "--ancestry-path")) {
2393 revs->ancestry_path = 1;
2394 revs->simplify_history = 0;
2395 revs->limited = 1;
2396 revs->ancestry_path_implicit_bottoms = 1;
2397 } else if (skip_prefix(arg, "--ancestry-path=", &optarg)) {
2398 struct commit *c;
2399 struct object_id oid;
2400 const char *msg = _("could not get commit for --ancestry-path argument %s");
2402 revs->ancestry_path = 1;
2403 revs->simplify_history = 0;
2404 revs->limited = 1;
2406 if (repo_get_oid_committish(revs->repo, optarg, &oid))
2407 return error(msg, optarg);
2408 get_reference(revs, optarg, &oid, ANCESTRY_PATH);
2409 c = lookup_commit_reference(revs->repo, &oid);
2410 if (!c)
2411 return error(msg, optarg);
2412 commit_list_insert(c, &revs->ancestry_path_bottoms);
2413 } else if (!strcmp(arg, "-g") || !strcmp(arg, "--walk-reflogs")) {
2414 init_reflog_walk(&revs->reflog_info);
2415 } else if (!strcmp(arg, "--default")) {
2416 if (argc <= 1)
2417 return error("bad --default argument");
2418 revs->def = argv[1];
2419 return 2;
2420 } else if (!strcmp(arg, "--merge")) {
2421 revs->show_merge = 1;
2422 } else if (!strcmp(arg, "--topo-order")) {
2423 revs->sort_order = REV_SORT_IN_GRAPH_ORDER;
2424 revs->topo_order = 1;
2425 } else if (!strcmp(arg, "--simplify-merges")) {
2426 revs->simplify_merges = 1;
2427 revs->topo_order = 1;
2428 revs->rewrite_parents = 1;
2429 revs->simplify_history = 0;
2430 revs->limited = 1;
2431 } else if (!strcmp(arg, "--simplify-by-decoration")) {
2432 revs->simplify_merges = 1;
2433 revs->topo_order = 1;
2434 revs->rewrite_parents = 1;
2435 revs->simplify_history = 0;
2436 revs->simplify_by_decoration = 1;
2437 revs->limited = 1;
2438 revs->prune = 1;
2439 } else if (!strcmp(arg, "--date-order")) {
2440 revs->sort_order = REV_SORT_BY_COMMIT_DATE;
2441 revs->topo_order = 1;
2442 } else if (!strcmp(arg, "--author-date-order")) {
2443 revs->sort_order = REV_SORT_BY_AUTHOR_DATE;
2444 revs->topo_order = 1;
2445 } else if (!strcmp(arg, "--early-output")) {
2446 revs->early_output = 100;
2447 revs->topo_order = 1;
2448 } else if (skip_prefix(arg, "--early-output=", &optarg)) {
2449 if (strtoul_ui(optarg, 10, &revs->early_output) < 0)
2450 die("'%s': not a non-negative integer", optarg);
2451 revs->topo_order = 1;
2452 } else if (!strcmp(arg, "--parents")) {
2453 revs->rewrite_parents = 1;
2454 revs->print_parents = 1;
2455 } else if (!strcmp(arg, "--dense")) {
2456 revs->dense = 1;
2457 } else if (!strcmp(arg, "--sparse")) {
2458 revs->dense = 0;
2459 } else if (!strcmp(arg, "--in-commit-order")) {
2460 revs->tree_blobs_in_commit_order = 1;
2461 } else if (!strcmp(arg, "--remove-empty")) {
2462 revs->remove_empty_trees = 1;
2463 } else if (!strcmp(arg, "--merges")) {
2464 revs->min_parents = 2;
2465 } else if (!strcmp(arg, "--no-merges")) {
2466 revs->max_parents = 1;
2467 } else if (skip_prefix(arg, "--min-parents=", &optarg)) {
2468 revs->min_parents = parse_count(optarg);
2469 } else if (!strcmp(arg, "--no-min-parents")) {
2470 revs->min_parents = 0;
2471 } else if (skip_prefix(arg, "--max-parents=", &optarg)) {
2472 revs->max_parents = parse_count(optarg);
2473 } else if (!strcmp(arg, "--no-max-parents")) {
2474 revs->max_parents = -1;
2475 } else if (!strcmp(arg, "--boundary")) {
2476 revs->boundary = 1;
2477 } else if (!strcmp(arg, "--left-right")) {
2478 revs->left_right = 1;
2479 } else if (!strcmp(arg, "--left-only")) {
2480 if (revs->right_only)
2481 die(_("options '%s' and '%s' cannot be used together"),
2482 "--left-only", "--right-only/--cherry");
2483 revs->left_only = 1;
2484 } else if (!strcmp(arg, "--right-only")) {
2485 if (revs->left_only)
2486 die(_("options '%s' and '%s' cannot be used together"), "--right-only", "--left-only");
2487 revs->right_only = 1;
2488 } else if (!strcmp(arg, "--cherry")) {
2489 if (revs->left_only)
2490 die(_("options '%s' and '%s' cannot be used together"), "--cherry", "--left-only");
2491 revs->cherry_mark = 1;
2492 revs->right_only = 1;
2493 revs->max_parents = 1;
2494 revs->limited = 1;
2495 } else if (!strcmp(arg, "--count")) {
2496 revs->count = 1;
2497 } else if (!strcmp(arg, "--cherry-mark")) {
2498 if (revs->cherry_pick)
2499 die(_("options '%s' and '%s' cannot be used together"), "--cherry-mark", "--cherry-pick");
2500 revs->cherry_mark = 1;
2501 revs->limited = 1; /* needs limit_list() */
2502 } else if (!strcmp(arg, "--cherry-pick")) {
2503 if (revs->cherry_mark)
2504 die(_("options '%s' and '%s' cannot be used together"), "--cherry-pick", "--cherry-mark");
2505 revs->cherry_pick = 1;
2506 revs->limited = 1;
2507 } else if (!strcmp(arg, "--objects")) {
2508 revs->tag_objects = 1;
2509 revs->tree_objects = 1;
2510 revs->blob_objects = 1;
2511 } else if (!strcmp(arg, "--objects-edge")) {
2512 revs->tag_objects = 1;
2513 revs->tree_objects = 1;
2514 revs->blob_objects = 1;
2515 revs->edge_hint = 1;
2516 } else if (!strcmp(arg, "--objects-edge-aggressive")) {
2517 revs->tag_objects = 1;
2518 revs->tree_objects = 1;
2519 revs->blob_objects = 1;
2520 revs->edge_hint = 1;
2521 revs->edge_hint_aggressive = 1;
2522 } else if (!strcmp(arg, "--verify-objects")) {
2523 revs->tag_objects = 1;
2524 revs->tree_objects = 1;
2525 revs->blob_objects = 1;
2526 revs->verify_objects = 1;
2527 disable_commit_graph(revs->repo);
2528 } else if (!strcmp(arg, "--unpacked")) {
2529 revs->unpacked = 1;
2530 } else if (starts_with(arg, "--unpacked=")) {
2531 die(_("--unpacked=<packfile> no longer supported"));
2532 } else if (!strcmp(arg, "--no-kept-objects")) {
2533 revs->no_kept_objects = 1;
2534 revs->keep_pack_cache_flags |= IN_CORE_KEEP_PACKS;
2535 revs->keep_pack_cache_flags |= ON_DISK_KEEP_PACKS;
2536 } else if (skip_prefix(arg, "--no-kept-objects=", &optarg)) {
2537 revs->no_kept_objects = 1;
2538 if (!strcmp(optarg, "in-core"))
2539 revs->keep_pack_cache_flags |= IN_CORE_KEEP_PACKS;
2540 if (!strcmp(optarg, "on-disk"))
2541 revs->keep_pack_cache_flags |= ON_DISK_KEEP_PACKS;
2542 } else if (!strcmp(arg, "-r")) {
2543 revs->diff = 1;
2544 revs->diffopt.flags.recursive = 1;
2545 } else if (!strcmp(arg, "-t")) {
2546 revs->diff = 1;
2547 revs->diffopt.flags.recursive = 1;
2548 revs->diffopt.flags.tree_in_recursive = 1;
2549 } else if ((argcount = diff_merges_parse_opts(revs, argv))) {
2550 return argcount;
2551 } else if (!strcmp(arg, "-v")) {
2552 revs->verbose_header = 1;
2553 } else if (!strcmp(arg, "--pretty")) {
2554 revs->verbose_header = 1;
2555 revs->pretty_given = 1;
2556 get_commit_format(NULL, revs);
2557 } else if (skip_prefix(arg, "--pretty=", &optarg) ||
2558 skip_prefix(arg, "--format=", &optarg)) {
2560 * Detached form ("--pretty X" as opposed to "--pretty=X")
2561 * not allowed, since the argument is optional.
2563 revs->verbose_header = 1;
2564 revs->pretty_given = 1;
2565 get_commit_format(optarg, revs);
2566 } else if (!strcmp(arg, "--expand-tabs")) {
2567 revs->expand_tabs_in_log = 8;
2568 } else if (!strcmp(arg, "--no-expand-tabs")) {
2569 revs->expand_tabs_in_log = 0;
2570 } else if (skip_prefix(arg, "--expand-tabs=", &arg)) {
2571 int val;
2572 if (strtol_i(arg, 10, &val) < 0 || val < 0)
2573 die("'%s': not a non-negative integer", arg);
2574 revs->expand_tabs_in_log = val;
2575 } else if (!strcmp(arg, "--show-notes") || !strcmp(arg, "--notes")) {
2576 enable_default_display_notes(&revs->notes_opt, &revs->show_notes);
2577 revs->show_notes_given = 1;
2578 } else if (!strcmp(arg, "--show-signature")) {
2579 revs->show_signature = 1;
2580 } else if (!strcmp(arg, "--no-show-signature")) {
2581 revs->show_signature = 0;
2582 } else if (!strcmp(arg, "--show-linear-break")) {
2583 revs->break_bar = " ..........";
2584 revs->track_linear = 1;
2585 revs->track_first_time = 1;
2586 } else if (skip_prefix(arg, "--show-linear-break=", &optarg)) {
2587 revs->break_bar = xstrdup(optarg);
2588 revs->track_linear = 1;
2589 revs->track_first_time = 1;
2590 } else if (!strcmp(arg, "--show-notes-by-default")) {
2591 revs->show_notes_by_default = 1;
2592 } else if (skip_prefix(arg, "--show-notes=", &optarg) ||
2593 skip_prefix(arg, "--notes=", &optarg)) {
2594 if (starts_with(arg, "--show-notes=") &&
2595 revs->notes_opt.use_default_notes < 0)
2596 revs->notes_opt.use_default_notes = 1;
2597 enable_ref_display_notes(&revs->notes_opt, &revs->show_notes, optarg);
2598 revs->show_notes_given = 1;
2599 } else if (!strcmp(arg, "--no-notes")) {
2600 disable_display_notes(&revs->notes_opt, &revs->show_notes);
2601 revs->show_notes_given = 1;
2602 } else if (!strcmp(arg, "--standard-notes")) {
2603 revs->show_notes_given = 1;
2604 revs->notes_opt.use_default_notes = 1;
2605 } else if (!strcmp(arg, "--no-standard-notes")) {
2606 revs->notes_opt.use_default_notes = 0;
2607 } else if (!strcmp(arg, "--oneline")) {
2608 revs->verbose_header = 1;
2609 get_commit_format("oneline", revs);
2610 revs->pretty_given = 1;
2611 revs->abbrev_commit = 1;
2612 } else if (!strcmp(arg, "--graph")) {
2613 graph_clear(revs->graph);
2614 revs->graph = graph_init(revs);
2615 } else if (!strcmp(arg, "--no-graph")) {
2616 graph_clear(revs->graph);
2617 revs->graph = NULL;
2618 } else if (!strcmp(arg, "--encode-email-headers")) {
2619 revs->encode_email_headers = 1;
2620 } else if (!strcmp(arg, "--no-encode-email-headers")) {
2621 revs->encode_email_headers = 0;
2622 } else if (!strcmp(arg, "--root")) {
2623 revs->show_root_diff = 1;
2624 } else if (!strcmp(arg, "--no-commit-id")) {
2625 revs->no_commit_id = 1;
2626 } else if (!strcmp(arg, "--always")) {
2627 revs->always_show_header = 1;
2628 } else if (!strcmp(arg, "--no-abbrev")) {
2629 revs->abbrev = 0;
2630 } else if (!strcmp(arg, "--abbrev")) {
2631 revs->abbrev = DEFAULT_ABBREV;
2632 } else if (skip_prefix(arg, "--abbrev=", &optarg)) {
2633 revs->abbrev = strtoul(optarg, NULL, 10);
2634 if (revs->abbrev < MINIMUM_ABBREV)
2635 revs->abbrev = MINIMUM_ABBREV;
2636 else if (revs->abbrev > hexsz)
2637 revs->abbrev = hexsz;
2638 } else if (!strcmp(arg, "--abbrev-commit")) {
2639 revs->abbrev_commit = 1;
2640 revs->abbrev_commit_given = 1;
2641 } else if (!strcmp(arg, "--no-abbrev-commit")) {
2642 revs->abbrev_commit = 0;
2643 } else if (!strcmp(arg, "--full-diff")) {
2644 revs->diff = 1;
2645 revs->full_diff = 1;
2646 } else if (!strcmp(arg, "--show-pulls")) {
2647 revs->show_pulls = 1;
2648 } else if (!strcmp(arg, "--full-history")) {
2649 revs->simplify_history = 0;
2650 } else if (!strcmp(arg, "--relative-date")) {
2651 revs->date_mode.type = DATE_RELATIVE;
2652 revs->date_mode_explicit = 1;
2653 } else if ((argcount = parse_long_opt("date", argv, &optarg))) {
2654 parse_date_format(optarg, &revs->date_mode);
2655 revs->date_mode_explicit = 1;
2656 return argcount;
2657 } else if (!strcmp(arg, "--log-size")) {
2658 revs->show_log_size = 1;
2661 * Grepping the commit log
2663 else if ((argcount = parse_long_opt("author", argv, &optarg))) {
2664 add_header_grep(revs, GREP_HEADER_AUTHOR, optarg);
2665 return argcount;
2666 } else if ((argcount = parse_long_opt("committer", argv, &optarg))) {
2667 add_header_grep(revs, GREP_HEADER_COMMITTER, optarg);
2668 return argcount;
2669 } else if ((argcount = parse_long_opt("grep-reflog", argv, &optarg))) {
2670 add_header_grep(revs, GREP_HEADER_REFLOG, optarg);
2671 return argcount;
2672 } else if ((argcount = parse_long_opt("grep", argv, &optarg))) {
2673 add_message_grep(revs, optarg);
2674 return argcount;
2675 } else if (!strcmp(arg, "--basic-regexp")) {
2676 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_BRE;
2677 } else if (!strcmp(arg, "--extended-regexp") || !strcmp(arg, "-E")) {
2678 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_ERE;
2679 } else if (!strcmp(arg, "--regexp-ignore-case") || !strcmp(arg, "-i")) {
2680 revs->grep_filter.ignore_case = 1;
2681 revs->diffopt.pickaxe_opts |= DIFF_PICKAXE_IGNORE_CASE;
2682 } else if (!strcmp(arg, "--fixed-strings") || !strcmp(arg, "-F")) {
2683 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_FIXED;
2684 } else if (!strcmp(arg, "--perl-regexp") || !strcmp(arg, "-P")) {
2685 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_PCRE;
2686 } else if (!strcmp(arg, "--all-match")) {
2687 revs->grep_filter.all_match = 1;
2688 } else if (!strcmp(arg, "--invert-grep")) {
2689 revs->grep_filter.no_body_match = 1;
2690 } else if ((argcount = parse_long_opt("encoding", argv, &optarg))) {
2691 free(git_log_output_encoding);
2692 if (strcmp(optarg, "none"))
2693 git_log_output_encoding = xstrdup(optarg);
2694 else
2695 git_log_output_encoding = xstrdup("");
2696 return argcount;
2697 } else if (!strcmp(arg, "--reverse")) {
2698 revs->reverse ^= 1;
2699 } else if (!strcmp(arg, "--children")) {
2700 revs->children.name = "children";
2701 revs->limited = 1;
2702 } else if (!strcmp(arg, "--ignore-missing")) {
2703 revs->ignore_missing = 1;
2704 } else if (opt && opt->allow_exclude_promisor_objects &&
2705 !strcmp(arg, "--exclude-promisor-objects")) {
2706 if (fetch_if_missing)
2707 BUG("exclude_promisor_objects can only be used when fetch_if_missing is 0");
2708 revs->exclude_promisor_objects = 1;
2709 } else {
2710 int opts = diff_opt_parse(&revs->diffopt, argv, argc, revs->prefix);
2711 if (!opts)
2712 unkv[(*unkc)++] = arg;
2713 return opts;
2716 return 1;
2719 void parse_revision_opt(struct rev_info *revs, struct parse_opt_ctx_t *ctx,
2720 const struct option *options,
2721 const char * const usagestr[])
2723 int n = handle_revision_opt(revs, ctx->argc, ctx->argv,
2724 &ctx->cpidx, ctx->out, NULL);
2725 if (n <= 0) {
2726 error("unknown option `%s'", ctx->argv[0]);
2727 usage_with_options(usagestr, options);
2729 ctx->argv += n;
2730 ctx->argc -= n;
2733 void revision_opts_finish(struct rev_info *revs)
2735 if (revs->graph && revs->track_linear)
2736 die(_("options '%s' and '%s' cannot be used together"), "--show-linear-break", "--graph");
2738 if (revs->graph) {
2739 revs->topo_order = 1;
2740 revs->rewrite_parents = 1;
2744 static int for_each_bisect_ref(struct ref_store *refs, each_ref_fn fn,
2745 void *cb_data, const char *term)
2747 struct strbuf bisect_refs = STRBUF_INIT;
2748 int status;
2749 strbuf_addf(&bisect_refs, "refs/bisect/%s", term);
2750 status = refs_for_each_fullref_in(refs, bisect_refs.buf, NULL, fn, cb_data);
2751 strbuf_release(&bisect_refs);
2752 return status;
2755 static int for_each_bad_bisect_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
2757 return for_each_bisect_ref(refs, fn, cb_data, term_bad);
2760 static int for_each_good_bisect_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
2762 return for_each_bisect_ref(refs, fn, cb_data, term_good);
2765 static int handle_revision_pseudo_opt(struct rev_info *revs,
2766 const char **argv, int *flags)
2768 const char *arg = argv[0];
2769 const char *optarg;
2770 struct ref_store *refs;
2771 int argcount;
2773 if (revs->repo != the_repository) {
2775 * We need some something like get_submodule_worktrees()
2776 * before we can go through all worktrees of a submodule,
2777 * .e.g with adding all HEADs from --all, which is not
2778 * supported right now, so stick to single worktree.
2780 if (!revs->single_worktree)
2781 BUG("--single-worktree cannot be used together with submodule");
2783 refs = get_main_ref_store(revs->repo);
2786 * NOTE!
2788 * Commands like "git shortlog" will not accept the options below
2789 * unless parse_revision_opt queues them (as opposed to erroring
2790 * out).
2792 * When implementing your new pseudo-option, remember to
2793 * register it in the list at the top of handle_revision_opt.
2795 if (!strcmp(arg, "--all")) {
2796 handle_refs(refs, revs, *flags, refs_for_each_ref);
2797 handle_refs(refs, revs, *flags, refs_head_ref);
2798 if (!revs->single_worktree) {
2799 struct all_refs_cb cb;
2801 init_all_refs_cb(&cb, revs, *flags);
2802 other_head_refs(handle_one_ref, &cb);
2804 clear_ref_exclusions(&revs->ref_excludes);
2805 } else if (!strcmp(arg, "--branches")) {
2806 if (revs->ref_excludes.hidden_refs_configured)
2807 return error(_("options '%s' and '%s' cannot be used together"),
2808 "--exclude-hidden", "--branches");
2809 handle_refs(refs, revs, *flags, refs_for_each_branch_ref);
2810 clear_ref_exclusions(&revs->ref_excludes);
2811 } else if (!strcmp(arg, "--bisect")) {
2812 read_bisect_terms(&term_bad, &term_good);
2813 handle_refs(refs, revs, *flags, for_each_bad_bisect_ref);
2814 handle_refs(refs, revs, *flags ^ (UNINTERESTING | BOTTOM),
2815 for_each_good_bisect_ref);
2816 revs->bisect = 1;
2817 } else if (!strcmp(arg, "--tags")) {
2818 if (revs->ref_excludes.hidden_refs_configured)
2819 return error(_("options '%s' and '%s' cannot be used together"),
2820 "--exclude-hidden", "--tags");
2821 handle_refs(refs, revs, *flags, refs_for_each_tag_ref);
2822 clear_ref_exclusions(&revs->ref_excludes);
2823 } else if (!strcmp(arg, "--remotes")) {
2824 if (revs->ref_excludes.hidden_refs_configured)
2825 return error(_("options '%s' and '%s' cannot be used together"),
2826 "--exclude-hidden", "--remotes");
2827 handle_refs(refs, revs, *flags, refs_for_each_remote_ref);
2828 clear_ref_exclusions(&revs->ref_excludes);
2829 } else if ((argcount = parse_long_opt("glob", argv, &optarg))) {
2830 struct all_refs_cb cb;
2831 init_all_refs_cb(&cb, revs, *flags);
2832 refs_for_each_glob_ref(get_main_ref_store(the_repository),
2833 handle_one_ref, optarg, &cb);
2834 clear_ref_exclusions(&revs->ref_excludes);
2835 return argcount;
2836 } else if ((argcount = parse_long_opt("exclude", argv, &optarg))) {
2837 add_ref_exclusion(&revs->ref_excludes, optarg);
2838 return argcount;
2839 } else if ((argcount = parse_long_opt("exclude-hidden", argv, &optarg))) {
2840 exclude_hidden_refs(&revs->ref_excludes, optarg);
2841 return argcount;
2842 } else if (skip_prefix(arg, "--branches=", &optarg)) {
2843 struct all_refs_cb cb;
2844 if (revs->ref_excludes.hidden_refs_configured)
2845 return error(_("options '%s' and '%s' cannot be used together"),
2846 "--exclude-hidden", "--branches");
2847 init_all_refs_cb(&cb, revs, *flags);
2848 refs_for_each_glob_ref_in(get_main_ref_store(the_repository),
2849 handle_one_ref, optarg,
2850 "refs/heads/", &cb);
2851 clear_ref_exclusions(&revs->ref_excludes);
2852 } else if (skip_prefix(arg, "--tags=", &optarg)) {
2853 struct all_refs_cb cb;
2854 if (revs->ref_excludes.hidden_refs_configured)
2855 return error(_("options '%s' and '%s' cannot be used together"),
2856 "--exclude-hidden", "--tags");
2857 init_all_refs_cb(&cb, revs, *flags);
2858 refs_for_each_glob_ref_in(get_main_ref_store(the_repository),
2859 handle_one_ref, optarg,
2860 "refs/tags/", &cb);
2861 clear_ref_exclusions(&revs->ref_excludes);
2862 } else if (skip_prefix(arg, "--remotes=", &optarg)) {
2863 struct all_refs_cb cb;
2864 if (revs->ref_excludes.hidden_refs_configured)
2865 return error(_("options '%s' and '%s' cannot be used together"),
2866 "--exclude-hidden", "--remotes");
2867 init_all_refs_cb(&cb, revs, *flags);
2868 refs_for_each_glob_ref_in(get_main_ref_store(the_repository),
2869 handle_one_ref, optarg,
2870 "refs/remotes/", &cb);
2871 clear_ref_exclusions(&revs->ref_excludes);
2872 } else if (!strcmp(arg, "--reflog")) {
2873 add_reflogs_to_pending(revs, *flags);
2874 } else if (!strcmp(arg, "--indexed-objects")) {
2875 add_index_objects_to_pending(revs, *flags);
2876 } else if (!strcmp(arg, "--alternate-refs")) {
2877 add_alternate_refs_to_pending(revs, *flags);
2878 } else if (!strcmp(arg, "--not")) {
2879 *flags ^= UNINTERESTING | BOTTOM;
2880 } else if (!strcmp(arg, "--no-walk")) {
2881 revs->no_walk = 1;
2882 } else if (skip_prefix(arg, "--no-walk=", &optarg)) {
2884 * Detached form ("--no-walk X" as opposed to "--no-walk=X")
2885 * not allowed, since the argument is optional.
2887 revs->no_walk = 1;
2888 if (!strcmp(optarg, "sorted"))
2889 revs->unsorted_input = 0;
2890 else if (!strcmp(optarg, "unsorted"))
2891 revs->unsorted_input = 1;
2892 else
2893 return error("invalid argument to --no-walk");
2894 } else if (!strcmp(arg, "--do-walk")) {
2895 revs->no_walk = 0;
2896 } else if (!strcmp(arg, "--single-worktree")) {
2897 revs->single_worktree = 1;
2898 } else if (skip_prefix(arg, ("--filter="), &arg)) {
2899 parse_list_objects_filter(&revs->filter, arg);
2900 } else if (!strcmp(arg, ("--no-filter"))) {
2901 list_objects_filter_set_no_filter(&revs->filter);
2902 } else {
2903 return 0;
2906 return 1;
2909 static void read_revisions_from_stdin(struct rev_info *revs,
2910 struct strvec *prune)
2912 struct strbuf sb;
2913 int seen_dashdash = 0;
2914 int seen_end_of_options = 0;
2915 int save_warning;
2916 int flags = 0;
2918 save_warning = warn_on_object_refname_ambiguity;
2919 warn_on_object_refname_ambiguity = 0;
2921 strbuf_init(&sb, 1000);
2922 while (strbuf_getline(&sb, stdin) != EOF) {
2923 if (!sb.len)
2924 break;
2926 if (!strcmp(sb.buf, "--")) {
2927 seen_dashdash = 1;
2928 break;
2931 if (!seen_end_of_options && sb.buf[0] == '-') {
2932 const char *argv[] = { sb.buf, NULL };
2934 if (!strcmp(sb.buf, "--end-of-options")) {
2935 seen_end_of_options = 1;
2936 continue;
2939 if (handle_revision_pseudo_opt(revs, argv, &flags) > 0)
2940 continue;
2942 die(_("invalid option '%s' in --stdin mode"), sb.buf);
2945 if (handle_revision_arg(sb.buf, revs, flags,
2946 REVARG_CANNOT_BE_FILENAME))
2947 die("bad revision '%s'", sb.buf);
2949 if (seen_dashdash)
2950 read_pathspec_from_stdin(&sb, prune);
2952 strbuf_release(&sb);
2953 warn_on_object_refname_ambiguity = save_warning;
2956 static void NORETURN diagnose_missing_default(const char *def)
2958 int flags;
2959 const char *refname;
2961 refname = refs_resolve_ref_unsafe(get_main_ref_store(the_repository),
2962 def, 0, NULL, &flags);
2963 if (!refname || !(flags & REF_ISSYMREF) || (flags & REF_ISBROKEN))
2964 die(_("your current branch appears to be broken"));
2966 skip_prefix(refname, "refs/heads/", &refname);
2967 die(_("your current branch '%s' does not have any commits yet"),
2968 refname);
2972 * Parse revision information, filling in the "rev_info" structure,
2973 * and removing the used arguments from the argument list.
2975 * Returns the number of arguments left that weren't recognized
2976 * (which are also moved to the head of the argument list)
2978 int setup_revisions(int argc, const char **argv, struct rev_info *revs, struct setup_revision_opt *opt)
2980 int i, flags, left, seen_dashdash, revarg_opt;
2981 struct strvec prune_data = STRVEC_INIT;
2982 int seen_end_of_options = 0;
2984 /* First, search for "--" */
2985 if (opt && opt->assume_dashdash) {
2986 seen_dashdash = 1;
2987 } else {
2988 seen_dashdash = 0;
2989 for (i = 1; i < argc; i++) {
2990 const char *arg = argv[i];
2991 if (strcmp(arg, "--"))
2992 continue;
2993 if (opt && opt->free_removed_argv_elements)
2994 free((char *)argv[i]);
2995 argv[i] = NULL;
2996 argc = i;
2997 if (argv[i + 1])
2998 strvec_pushv(&prune_data, argv + i + 1);
2999 seen_dashdash = 1;
3000 break;
3004 /* Second, deal with arguments and options */
3005 flags = 0;
3006 revarg_opt = opt ? opt->revarg_opt : 0;
3007 if (seen_dashdash)
3008 revarg_opt |= REVARG_CANNOT_BE_FILENAME;
3009 for (left = i = 1; i < argc; i++) {
3010 const char *arg = argv[i];
3011 if (!seen_end_of_options && *arg == '-') {
3012 int opts;
3014 opts = handle_revision_pseudo_opt(
3015 revs, argv + i,
3016 &flags);
3017 if (opts > 0) {
3018 i += opts - 1;
3019 continue;
3022 if (!strcmp(arg, "--stdin")) {
3023 if (revs->disable_stdin) {
3024 argv[left++] = arg;
3025 continue;
3027 if (revs->read_from_stdin++)
3028 die("--stdin given twice?");
3029 read_revisions_from_stdin(revs, &prune_data);
3030 continue;
3033 if (!strcmp(arg, "--end-of-options")) {
3034 seen_end_of_options = 1;
3035 continue;
3038 opts = handle_revision_opt(revs, argc - i, argv + i,
3039 &left, argv, opt);
3040 if (opts > 0) {
3041 i += opts - 1;
3042 continue;
3044 if (opts < 0)
3045 exit(128);
3046 continue;
3050 if (handle_revision_arg(arg, revs, flags, revarg_opt)) {
3051 int j;
3052 if (seen_dashdash || *arg == '^')
3053 die("bad revision '%s'", arg);
3055 /* If we didn't have a "--":
3056 * (1) all filenames must exist;
3057 * (2) all rev-args must not be interpretable
3058 * as a valid filename.
3059 * but the latter we have checked in the main loop.
3061 for (j = i; j < argc; j++)
3062 verify_filename(revs->prefix, argv[j], j == i);
3064 strvec_pushv(&prune_data, argv + i);
3065 break;
3068 revision_opts_finish(revs);
3070 if (prune_data.nr) {
3072 * If we need to introduce the magic "a lone ':' means no
3073 * pathspec whatsoever", here is the place to do so.
3075 * if (prune_data.nr == 1 && !strcmp(prune_data[0], ":")) {
3076 * prune_data.nr = 0;
3077 * prune_data.alloc = 0;
3078 * free(prune_data.path);
3079 * prune_data.path = NULL;
3080 * } else {
3081 * terminate prune_data.alloc with NULL and
3082 * call init_pathspec() to set revs->prune_data here.
3085 parse_pathspec(&revs->prune_data, 0, 0,
3086 revs->prefix, prune_data.v);
3088 strvec_clear(&prune_data);
3090 if (!revs->def)
3091 revs->def = opt ? opt->def : NULL;
3092 if (opt && opt->tweak)
3093 opt->tweak(revs);
3094 if (revs->show_merge)
3095 prepare_show_merge(revs);
3096 if (revs->def && !revs->pending.nr && !revs->rev_input_given) {
3097 struct object_id oid;
3098 struct object *object;
3099 struct object_context oc;
3100 if (get_oid_with_context(revs->repo, revs->def, 0, &oid, &oc))
3101 diagnose_missing_default(revs->def);
3102 object = get_reference(revs, revs->def, &oid, 0);
3103 add_pending_object_with_mode(revs, object, revs->def, oc.mode);
3104 object_context_release(&oc);
3107 /* Did the user ask for any diff output? Run the diff! */
3108 if (revs->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT)
3109 revs->diff = 1;
3111 /* Pickaxe, diff-filter and rename following need diffs */
3112 if ((revs->diffopt.pickaxe_opts & DIFF_PICKAXE_KINDS_MASK) ||
3113 revs->diffopt.filter ||
3114 revs->diffopt.flags.follow_renames)
3115 revs->diff = 1;
3117 if (revs->diffopt.objfind)
3118 revs->simplify_history = 0;
3120 if (revs->line_level_traverse) {
3121 if (want_ancestry(revs))
3122 revs->limited = 1;
3123 revs->topo_order = 1;
3126 if (revs->topo_order && !generation_numbers_enabled(the_repository))
3127 revs->limited = 1;
3129 if (revs->prune_data.nr) {
3130 copy_pathspec(&revs->pruning.pathspec, &revs->prune_data);
3131 /* Can't prune commits with rename following: the paths change.. */
3132 if (!revs->diffopt.flags.follow_renames)
3133 revs->prune = 1;
3134 if (!revs->full_diff)
3135 copy_pathspec(&revs->diffopt.pathspec,
3136 &revs->prune_data);
3139 diff_merges_setup_revs(revs);
3141 revs->diffopt.abbrev = revs->abbrev;
3143 diff_setup_done(&revs->diffopt);
3145 if (!is_encoding_utf8(get_log_output_encoding()))
3146 revs->grep_filter.ignore_locale = 1;
3147 compile_grep_patterns(&revs->grep_filter);
3149 if (revs->reflog_info && revs->limited)
3150 die("cannot combine --walk-reflogs with history-limiting options");
3151 if (revs->rewrite_parents && revs->children.name)
3152 die(_("options '%s' and '%s' cannot be used together"), "--parents", "--children");
3153 if (revs->filter.choice && !revs->blob_objects)
3154 die(_("object filtering requires --objects"));
3157 * Limitations on the graph functionality
3159 die_for_incompatible_opt3(!!revs->graph, "--graph",
3160 !!revs->reverse, "--reverse",
3161 !!revs->reflog_info, "--walk-reflogs");
3163 if (revs->no_walk && revs->graph)
3164 die(_("options '%s' and '%s' cannot be used together"), "--no-walk", "--graph");
3165 if (!revs->reflog_info && revs->grep_filter.use_reflog_filter)
3166 die(_("the option '%s' requires '%s'"), "--grep-reflog", "--walk-reflogs");
3168 if (revs->line_level_traverse &&
3169 (revs->diffopt.output_format & ~(DIFF_FORMAT_PATCH | DIFF_FORMAT_NO_OUTPUT)))
3170 die(_("-L does not yet support diff formats besides -p and -s"));
3172 if (revs->expand_tabs_in_log < 0)
3173 revs->expand_tabs_in_log = revs->expand_tabs_in_log_default;
3175 if (!revs->show_notes_given && revs->show_notes_by_default) {
3176 enable_default_display_notes(&revs->notes_opt, &revs->show_notes);
3177 revs->show_notes_given = 1;
3180 return left;
3183 static void release_revisions_cmdline(struct rev_cmdline_info *cmdline)
3185 unsigned int i;
3187 for (i = 0; i < cmdline->nr; i++)
3188 free((char *)cmdline->rev[i].name);
3189 free(cmdline->rev);
3192 static void release_revisions_mailmap(struct string_list *mailmap)
3194 if (!mailmap)
3195 return;
3196 clear_mailmap(mailmap);
3197 free(mailmap);
3200 static void release_revisions_topo_walk_info(struct topo_walk_info *info);
3202 static void free_void_commit_list(void *list)
3204 free_commit_list(list);
3207 void release_revisions(struct rev_info *revs)
3209 free_commit_list(revs->commits);
3210 free_commit_list(revs->ancestry_path_bottoms);
3211 release_display_notes(&revs->notes_opt);
3212 object_array_clear(&revs->pending);
3213 object_array_clear(&revs->boundary_commits);
3214 release_revisions_cmdline(&revs->cmdline);
3215 list_objects_filter_release(&revs->filter);
3216 clear_pathspec(&revs->prune_data);
3217 date_mode_release(&revs->date_mode);
3218 release_revisions_mailmap(revs->mailmap);
3219 free_grep_patterns(&revs->grep_filter);
3220 graph_clear(revs->graph);
3221 diff_free(&revs->diffopt);
3222 diff_free(&revs->pruning);
3223 reflog_walk_info_release(revs->reflog_info);
3224 release_revisions_topo_walk_info(revs->topo_walk_info);
3225 clear_decoration(&revs->children, free_void_commit_list);
3226 clear_decoration(&revs->merge_simplification, free);
3227 clear_decoration(&revs->treesame, free);
3228 line_log_free(revs);
3229 oidset_clear(&revs->missing_commits);
3232 static void add_child(struct rev_info *revs, struct commit *parent, struct commit *child)
3234 struct commit_list *l = xcalloc(1, sizeof(*l));
3236 l->item = child;
3237 l->next = add_decoration(&revs->children, &parent->object, l);
3240 static int remove_duplicate_parents(struct rev_info *revs, struct commit *commit)
3242 struct treesame_state *ts = lookup_decoration(&revs->treesame, &commit->object);
3243 struct commit_list **pp, *p;
3244 int surviving_parents;
3246 /* Examine existing parents while marking ones we have seen... */
3247 pp = &commit->parents;
3248 surviving_parents = 0;
3249 while ((p = *pp) != NULL) {
3250 struct commit *parent = p->item;
3251 if (parent->object.flags & TMP_MARK) {
3252 *pp = p->next;
3253 if (ts)
3254 compact_treesame(revs, commit, surviving_parents);
3255 continue;
3257 parent->object.flags |= TMP_MARK;
3258 surviving_parents++;
3259 pp = &p->next;
3261 /* clear the temporary mark */
3262 for (p = commit->parents; p; p = p->next) {
3263 p->item->object.flags &= ~TMP_MARK;
3265 /* no update_treesame() - removing duplicates can't affect TREESAME */
3266 return surviving_parents;
3269 struct merge_simplify_state {
3270 struct commit *simplified;
3273 static struct merge_simplify_state *locate_simplify_state(struct rev_info *revs, struct commit *commit)
3275 struct merge_simplify_state *st;
3277 st = lookup_decoration(&revs->merge_simplification, &commit->object);
3278 if (!st) {
3279 CALLOC_ARRAY(st, 1);
3280 add_decoration(&revs->merge_simplification, &commit->object, st);
3282 return st;
3285 static int mark_redundant_parents(struct commit *commit)
3287 struct commit_list *h = reduce_heads(commit->parents);
3288 int i = 0, marked = 0;
3289 struct commit_list *po, *pn;
3291 /* Want these for sanity-checking only */
3292 int orig_cnt = commit_list_count(commit->parents);
3293 int cnt = commit_list_count(h);
3296 * Not ready to remove items yet, just mark them for now, based
3297 * on the output of reduce_heads(). reduce_heads outputs the reduced
3298 * set in its original order, so this isn't too hard.
3300 po = commit->parents;
3301 pn = h;
3302 while (po) {
3303 if (pn && po->item == pn->item) {
3304 pn = pn->next;
3305 i++;
3306 } else {
3307 po->item->object.flags |= TMP_MARK;
3308 marked++;
3310 po=po->next;
3313 if (i != cnt || cnt+marked != orig_cnt)
3314 die("mark_redundant_parents %d %d %d %d", orig_cnt, cnt, i, marked);
3316 free_commit_list(h);
3318 return marked;
3321 static int mark_treesame_root_parents(struct commit *commit)
3323 struct commit_list *p;
3324 int marked = 0;
3326 for (p = commit->parents; p; p = p->next) {
3327 struct commit *parent = p->item;
3328 if (!parent->parents && (parent->object.flags & TREESAME)) {
3329 parent->object.flags |= TMP_MARK;
3330 marked++;
3334 return marked;
3338 * Awkward naming - this means one parent we are TREESAME to.
3339 * cf mark_treesame_root_parents: root parents that are TREESAME (to an
3340 * empty tree). Better name suggestions?
3342 static int leave_one_treesame_to_parent(struct rev_info *revs, struct commit *commit)
3344 struct treesame_state *ts = lookup_decoration(&revs->treesame, &commit->object);
3345 struct commit *unmarked = NULL, *marked = NULL;
3346 struct commit_list *p;
3347 unsigned n;
3349 for (p = commit->parents, n = 0; p; p = p->next, n++) {
3350 if (ts->treesame[n]) {
3351 if (p->item->object.flags & TMP_MARK) {
3352 if (!marked)
3353 marked = p->item;
3354 } else {
3355 if (!unmarked) {
3356 unmarked = p->item;
3357 break;
3364 * If we are TREESAME to a marked-for-deletion parent, but not to any
3365 * unmarked parents, unmark the first TREESAME parent. This is the
3366 * parent that the default simplify_history==1 scan would have followed,
3367 * and it doesn't make sense to omit that path when asking for a
3368 * simplified full history. Retaining it improves the chances of
3369 * understanding odd missed merges that took an old version of a file.
3371 * Example:
3373 * I--------*X A modified the file, but mainline merge X used
3374 * \ / "-s ours", so took the version from I. X is
3375 * `-*A--' TREESAME to I and !TREESAME to A.
3377 * Default log from X would produce "I". Without this check,
3378 * --full-history --simplify-merges would produce "I-A-X", showing
3379 * the merge commit X and that it changed A, but not making clear that
3380 * it had just taken the I version. With this check, the topology above
3381 * is retained.
3383 * Note that it is possible that the simplification chooses a different
3384 * TREESAME parent from the default, in which case this test doesn't
3385 * activate, and we _do_ drop the default parent. Example:
3387 * I------X A modified the file, but it was reverted in B,
3388 * \ / meaning mainline merge X is TREESAME to both
3389 * *A-*B parents.
3391 * Default log would produce "I" by following the first parent;
3392 * --full-history --simplify-merges will produce "I-A-B". But this is a
3393 * reasonable result - it presents a logical full history leading from
3394 * I to X, and X is not an important merge.
3396 if (!unmarked && marked) {
3397 marked->object.flags &= ~TMP_MARK;
3398 return 1;
3401 return 0;
3404 static int remove_marked_parents(struct rev_info *revs, struct commit *commit)
3406 struct commit_list **pp, *p;
3407 int nth_parent, removed = 0;
3409 pp = &commit->parents;
3410 nth_parent = 0;
3411 while ((p = *pp) != NULL) {
3412 struct commit *parent = p->item;
3413 if (parent->object.flags & TMP_MARK) {
3414 parent->object.flags &= ~TMP_MARK;
3415 *pp = p->next;
3416 free(p);
3417 removed++;
3418 compact_treesame(revs, commit, nth_parent);
3419 continue;
3421 pp = &p->next;
3422 nth_parent++;
3425 /* Removing parents can only increase TREESAMEness */
3426 if (removed && !(commit->object.flags & TREESAME))
3427 update_treesame(revs, commit);
3429 return nth_parent;
3432 static struct commit_list **simplify_one(struct rev_info *revs, struct commit *commit, struct commit_list **tail)
3434 struct commit_list *p;
3435 struct commit *parent;
3436 struct merge_simplify_state *st, *pst;
3437 int cnt;
3439 st = locate_simplify_state(revs, commit);
3442 * Have we handled this one?
3444 if (st->simplified)
3445 return tail;
3448 * An UNINTERESTING commit simplifies to itself, so does a
3449 * root commit. We do not rewrite parents of such commit
3450 * anyway.
3452 if ((commit->object.flags & UNINTERESTING) || !commit->parents) {
3453 st->simplified = commit;
3454 return tail;
3458 * Do we know what commit all of our parents that matter
3459 * should be rewritten to? Otherwise we are not ready to
3460 * rewrite this one yet.
3462 for (cnt = 0, p = commit->parents; p; p = p->next) {
3463 pst = locate_simplify_state(revs, p->item);
3464 if (!pst->simplified) {
3465 tail = &commit_list_insert(p->item, tail)->next;
3466 cnt++;
3468 if (revs->first_parent_only)
3469 break;
3471 if (cnt) {
3472 tail = &commit_list_insert(commit, tail)->next;
3473 return tail;
3477 * Rewrite our list of parents. Note that this cannot
3478 * affect our TREESAME flags in any way - a commit is
3479 * always TREESAME to its simplification.
3481 for (p = commit->parents; p; p = p->next) {
3482 pst = locate_simplify_state(revs, p->item);
3483 p->item = pst->simplified;
3484 if (revs->first_parent_only)
3485 break;
3488 if (revs->first_parent_only)
3489 cnt = 1;
3490 else
3491 cnt = remove_duplicate_parents(revs, commit);
3494 * It is possible that we are a merge and one side branch
3495 * does not have any commit that touches the given paths;
3496 * in such a case, the immediate parent from that branch
3497 * will be rewritten to be the merge base.
3499 * o----X X: the commit we are looking at;
3500 * / / o: a commit that touches the paths;
3501 * ---o----'
3503 * Further, a merge of an independent branch that doesn't
3504 * touch the path will reduce to a treesame root parent:
3506 * ----o----X X: the commit we are looking at;
3507 * / o: a commit that touches the paths;
3508 * r r: a root commit not touching the paths
3510 * Detect and simplify both cases.
3512 if (1 < cnt) {
3513 int marked = mark_redundant_parents(commit);
3514 marked += mark_treesame_root_parents(commit);
3515 if (marked)
3516 marked -= leave_one_treesame_to_parent(revs, commit);
3517 if (marked)
3518 cnt = remove_marked_parents(revs, commit);
3522 * A commit simplifies to itself if it is a root, if it is
3523 * UNINTERESTING, if it touches the given paths, or if it is a
3524 * merge and its parents don't simplify to one relevant commit
3525 * (the first two cases are already handled at the beginning of
3526 * this function).
3528 * Otherwise, it simplifies to what its sole relevant parent
3529 * simplifies to.
3531 if (!cnt ||
3532 (commit->object.flags & UNINTERESTING) ||
3533 !(commit->object.flags & TREESAME) ||
3534 (parent = one_relevant_parent(revs, commit->parents)) == NULL ||
3535 (revs->show_pulls && (commit->object.flags & PULL_MERGE)))
3536 st->simplified = commit;
3537 else {
3538 pst = locate_simplify_state(revs, parent);
3539 st->simplified = pst->simplified;
3541 return tail;
3544 static void simplify_merges(struct rev_info *revs)
3546 struct commit_list *list, *next;
3547 struct commit_list *yet_to_do, **tail;
3548 struct commit *commit;
3550 if (!revs->prune)
3551 return;
3553 /* feed the list reversed */
3554 yet_to_do = NULL;
3555 for (list = revs->commits; list; list = next) {
3556 commit = list->item;
3557 next = list->next;
3559 * Do not free(list) here yet; the original list
3560 * is used later in this function.
3562 commit_list_insert(commit, &yet_to_do);
3564 while (yet_to_do) {
3565 list = yet_to_do;
3566 yet_to_do = NULL;
3567 tail = &yet_to_do;
3568 while (list) {
3569 commit = pop_commit(&list);
3570 tail = simplify_one(revs, commit, tail);
3574 /* clean up the result, removing the simplified ones */
3575 list = revs->commits;
3576 revs->commits = NULL;
3577 tail = &revs->commits;
3578 while (list) {
3579 struct merge_simplify_state *st;
3581 commit = pop_commit(&list);
3582 st = locate_simplify_state(revs, commit);
3583 if (st->simplified == commit)
3584 tail = &commit_list_insert(commit, tail)->next;
3588 static void set_children(struct rev_info *revs)
3590 struct commit_list *l;
3591 for (l = revs->commits; l; l = l->next) {
3592 struct commit *commit = l->item;
3593 struct commit_list *p;
3595 for (p = commit->parents; p; p = p->next)
3596 add_child(revs, p->item, commit);
3600 void reset_revision_walk(void)
3602 clear_object_flags(SEEN | ADDED | SHOWN | TOPO_WALK_EXPLORED | TOPO_WALK_INDEGREE);
3605 static int mark_uninteresting(const struct object_id *oid,
3606 struct packed_git *pack UNUSED,
3607 uint32_t pos UNUSED,
3608 void *cb)
3610 struct rev_info *revs = cb;
3611 struct object *o = lookup_unknown_object(revs->repo, oid);
3612 o->flags |= UNINTERESTING | SEEN;
3613 return 0;
3616 define_commit_slab(indegree_slab, int);
3617 define_commit_slab(author_date_slab, timestamp_t);
3619 struct topo_walk_info {
3620 timestamp_t min_generation;
3621 struct prio_queue explore_queue;
3622 struct prio_queue indegree_queue;
3623 struct prio_queue topo_queue;
3624 struct indegree_slab indegree;
3625 struct author_date_slab author_date;
3628 static int topo_walk_atexit_registered;
3629 static unsigned int count_explore_walked;
3630 static unsigned int count_indegree_walked;
3631 static unsigned int count_topo_walked;
3633 static void trace2_topo_walk_statistics_atexit(void)
3635 struct json_writer jw = JSON_WRITER_INIT;
3637 jw_object_begin(&jw, 0);
3638 jw_object_intmax(&jw, "count_explore_walked", count_explore_walked);
3639 jw_object_intmax(&jw, "count_indegree_walked", count_indegree_walked);
3640 jw_object_intmax(&jw, "count_topo_walked", count_topo_walked);
3641 jw_end(&jw);
3643 trace2_data_json("topo_walk", the_repository, "statistics", &jw);
3645 jw_release(&jw);
3648 static inline void test_flag_and_insert(struct prio_queue *q, struct commit *c, int flag)
3650 if (c->object.flags & flag)
3651 return;
3653 c->object.flags |= flag;
3654 prio_queue_put(q, c);
3657 static void explore_walk_step(struct rev_info *revs)
3659 struct topo_walk_info *info = revs->topo_walk_info;
3660 struct commit_list *p;
3661 struct commit *c = prio_queue_get(&info->explore_queue);
3663 if (!c)
3664 return;
3666 if (repo_parse_commit_gently(revs->repo, c, 1) < 0)
3667 return;
3669 count_explore_walked++;
3671 if (revs->sort_order == REV_SORT_BY_AUTHOR_DATE)
3672 record_author_date(&info->author_date, c);
3674 if (revs->max_age != -1 && (c->date < revs->max_age))
3675 c->object.flags |= UNINTERESTING;
3677 if (process_parents(revs, c, NULL, NULL) < 0)
3678 return;
3680 if (c->object.flags & UNINTERESTING)
3681 mark_parents_uninteresting(revs, c);
3683 for (p = c->parents; p; p = p->next)
3684 test_flag_and_insert(&info->explore_queue, p->item, TOPO_WALK_EXPLORED);
3687 static void explore_to_depth(struct rev_info *revs,
3688 timestamp_t gen_cutoff)
3690 struct topo_walk_info *info = revs->topo_walk_info;
3691 struct commit *c;
3692 while ((c = prio_queue_peek(&info->explore_queue)) &&
3693 commit_graph_generation(c) >= gen_cutoff)
3694 explore_walk_step(revs);
3697 static void indegree_walk_step(struct rev_info *revs)
3699 struct commit_list *p;
3700 struct topo_walk_info *info = revs->topo_walk_info;
3701 struct commit *c = prio_queue_get(&info->indegree_queue);
3703 if (!c)
3704 return;
3706 if (repo_parse_commit_gently(revs->repo, c, 1) < 0)
3707 return;
3709 count_indegree_walked++;
3711 explore_to_depth(revs, commit_graph_generation(c));
3713 for (p = c->parents; p; p = p->next) {
3714 struct commit *parent = p->item;
3715 int *pi = indegree_slab_at(&info->indegree, parent);
3717 if (repo_parse_commit_gently(revs->repo, parent, 1) < 0)
3718 return;
3720 if (*pi)
3721 (*pi)++;
3722 else
3723 *pi = 2;
3725 test_flag_and_insert(&info->indegree_queue, parent, TOPO_WALK_INDEGREE);
3727 if (revs->first_parent_only)
3728 return;
3732 static void compute_indegrees_to_depth(struct rev_info *revs,
3733 timestamp_t gen_cutoff)
3735 struct topo_walk_info *info = revs->topo_walk_info;
3736 struct commit *c;
3737 while ((c = prio_queue_peek(&info->indegree_queue)) &&
3738 commit_graph_generation(c) >= gen_cutoff)
3739 indegree_walk_step(revs);
3742 static void release_revisions_topo_walk_info(struct topo_walk_info *info)
3744 if (!info)
3745 return;
3746 clear_prio_queue(&info->explore_queue);
3747 clear_prio_queue(&info->indegree_queue);
3748 clear_prio_queue(&info->topo_queue);
3749 clear_indegree_slab(&info->indegree);
3750 clear_author_date_slab(&info->author_date);
3751 free(info);
3754 static void reset_topo_walk(struct rev_info *revs)
3756 release_revisions_topo_walk_info(revs->topo_walk_info);
3757 revs->topo_walk_info = NULL;
3760 static void init_topo_walk(struct rev_info *revs)
3762 struct topo_walk_info *info;
3763 struct commit_list *list;
3764 if (revs->topo_walk_info)
3765 reset_topo_walk(revs);
3767 revs->topo_walk_info = xmalloc(sizeof(struct topo_walk_info));
3768 info = revs->topo_walk_info;
3769 memset(info, 0, sizeof(struct topo_walk_info));
3771 init_indegree_slab(&info->indegree);
3772 memset(&info->explore_queue, 0, sizeof(info->explore_queue));
3773 memset(&info->indegree_queue, 0, sizeof(info->indegree_queue));
3774 memset(&info->topo_queue, 0, sizeof(info->topo_queue));
3776 switch (revs->sort_order) {
3777 default: /* REV_SORT_IN_GRAPH_ORDER */
3778 info->topo_queue.compare = NULL;
3779 break;
3780 case REV_SORT_BY_COMMIT_DATE:
3781 info->topo_queue.compare = compare_commits_by_commit_date;
3782 break;
3783 case REV_SORT_BY_AUTHOR_DATE:
3784 init_author_date_slab(&info->author_date);
3785 info->topo_queue.compare = compare_commits_by_author_date;
3786 info->topo_queue.cb_data = &info->author_date;
3787 break;
3790 info->explore_queue.compare = compare_commits_by_gen_then_commit_date;
3791 info->indegree_queue.compare = compare_commits_by_gen_then_commit_date;
3793 info->min_generation = GENERATION_NUMBER_INFINITY;
3794 for (list = revs->commits; list; list = list->next) {
3795 struct commit *c = list->item;
3796 timestamp_t generation;
3798 if (repo_parse_commit_gently(revs->repo, c, 1))
3799 continue;
3801 test_flag_and_insert(&info->explore_queue, c, TOPO_WALK_EXPLORED);
3802 test_flag_and_insert(&info->indegree_queue, c, TOPO_WALK_INDEGREE);
3804 generation = commit_graph_generation(c);
3805 if (generation < info->min_generation)
3806 info->min_generation = generation;
3808 *(indegree_slab_at(&info->indegree, c)) = 1;
3810 if (revs->sort_order == REV_SORT_BY_AUTHOR_DATE)
3811 record_author_date(&info->author_date, c);
3813 compute_indegrees_to_depth(revs, info->min_generation);
3815 for (list = revs->commits; list; list = list->next) {
3816 struct commit *c = list->item;
3818 if (*(indegree_slab_at(&info->indegree, c)) == 1)
3819 prio_queue_put(&info->topo_queue, c);
3823 * This is unfortunate; the initial tips need to be shown
3824 * in the order given from the revision traversal machinery.
3826 if (revs->sort_order == REV_SORT_IN_GRAPH_ORDER)
3827 prio_queue_reverse(&info->topo_queue);
3829 if (trace2_is_enabled() && !topo_walk_atexit_registered) {
3830 atexit(trace2_topo_walk_statistics_atexit);
3831 topo_walk_atexit_registered = 1;
3835 static struct commit *next_topo_commit(struct rev_info *revs)
3837 struct commit *c;
3838 struct topo_walk_info *info = revs->topo_walk_info;
3840 /* pop next off of topo_queue */
3841 c = prio_queue_get(&info->topo_queue);
3843 if (c)
3844 *(indegree_slab_at(&info->indegree, c)) = 0;
3846 return c;
3849 static void expand_topo_walk(struct rev_info *revs, struct commit *commit)
3851 struct commit_list *p;
3852 struct topo_walk_info *info = revs->topo_walk_info;
3853 if (process_parents(revs, commit, NULL, NULL) < 0) {
3854 if (!revs->ignore_missing_links)
3855 die("Failed to traverse parents of commit %s",
3856 oid_to_hex(&commit->object.oid));
3859 count_topo_walked++;
3861 for (p = commit->parents; p; p = p->next) {
3862 struct commit *parent = p->item;
3863 int *pi;
3864 timestamp_t generation;
3866 if (parent->object.flags & UNINTERESTING)
3867 continue;
3869 if (repo_parse_commit_gently(revs->repo, parent, 1) < 0)
3870 continue;
3872 generation = commit_graph_generation(parent);
3873 if (generation < info->min_generation) {
3874 info->min_generation = generation;
3875 compute_indegrees_to_depth(revs, info->min_generation);
3878 pi = indegree_slab_at(&info->indegree, parent);
3880 (*pi)--;
3881 if (*pi == 1)
3882 prio_queue_put(&info->topo_queue, parent);
3884 if (revs->first_parent_only)
3885 return;
3889 int prepare_revision_walk(struct rev_info *revs)
3891 int i;
3892 struct object_array old_pending;
3893 struct commit_list **next = &revs->commits;
3895 memcpy(&old_pending, &revs->pending, sizeof(old_pending));
3896 revs->pending.nr = 0;
3897 revs->pending.alloc = 0;
3898 revs->pending.objects = NULL;
3899 for (i = 0; i < old_pending.nr; i++) {
3900 struct object_array_entry *e = old_pending.objects + i;
3901 struct commit *commit = handle_commit(revs, e);
3902 if (commit) {
3903 if (!(commit->object.flags & SEEN)) {
3904 commit->object.flags |= SEEN;
3905 next = commit_list_append(commit, next);
3909 object_array_clear(&old_pending);
3911 /* Signal whether we need per-parent treesame decoration */
3912 if (revs->simplify_merges ||
3913 (revs->limited && limiting_can_increase_treesame(revs)))
3914 revs->treesame.name = "treesame";
3916 if (revs->exclude_promisor_objects) {
3917 for_each_packed_object(mark_uninteresting, revs,
3918 FOR_EACH_OBJECT_PROMISOR_ONLY);
3921 if (!revs->reflog_info)
3922 prepare_to_use_bloom_filter(revs);
3923 if (!revs->unsorted_input)
3924 commit_list_sort_by_date(&revs->commits);
3925 if (revs->no_walk)
3926 return 0;
3927 if (revs->limited) {
3928 if (limit_list(revs) < 0)
3929 return -1;
3930 if (revs->topo_order)
3931 sort_in_topological_order(&revs->commits, revs->sort_order);
3932 } else if (revs->topo_order)
3933 init_topo_walk(revs);
3934 if (revs->line_level_traverse && want_ancestry(revs))
3936 * At the moment we can only do line-level log with parent
3937 * rewriting by performing this expensive pre-filtering step.
3938 * If parent rewriting is not requested, then we rather
3939 * perform the line-level log filtering during the regular
3940 * history traversal.
3942 line_log_filter(revs);
3943 if (revs->simplify_merges)
3944 simplify_merges(revs);
3945 if (revs->children.name)
3946 set_children(revs);
3948 return 0;
3951 static enum rewrite_result rewrite_one_1(struct rev_info *revs,
3952 struct commit **pp,
3953 struct prio_queue *queue)
3955 for (;;) {
3956 struct commit *p = *pp;
3957 if (!revs->limited)
3958 if (process_parents(revs, p, NULL, queue) < 0)
3959 return rewrite_one_error;
3960 if (p->object.flags & UNINTERESTING)
3961 return rewrite_one_ok;
3962 if (!(p->object.flags & TREESAME))
3963 return rewrite_one_ok;
3964 if (!p->parents)
3965 return rewrite_one_noparents;
3966 if (!(p = one_relevant_parent(revs, p->parents)))
3967 return rewrite_one_ok;
3968 *pp = p;
3972 static void merge_queue_into_list(struct prio_queue *q, struct commit_list **list)
3974 while (q->nr) {
3975 struct commit *item = prio_queue_peek(q);
3976 struct commit_list *p = *list;
3978 if (p && p->item->date >= item->date)
3979 list = &p->next;
3980 else {
3981 p = commit_list_insert(item, list);
3982 list = &p->next; /* skip newly added item */
3983 prio_queue_get(q); /* pop item */
3988 static enum rewrite_result rewrite_one(struct rev_info *revs, struct commit **pp)
3990 struct prio_queue queue = { compare_commits_by_commit_date };
3991 enum rewrite_result ret = rewrite_one_1(revs, pp, &queue);
3992 merge_queue_into_list(&queue, &revs->commits);
3993 clear_prio_queue(&queue);
3994 return ret;
3997 int rewrite_parents(struct rev_info *revs, struct commit *commit,
3998 rewrite_parent_fn_t rewrite_parent)
4000 struct commit_list **pp = &commit->parents;
4001 while (*pp) {
4002 struct commit_list *parent = *pp;
4003 switch (rewrite_parent(revs, &parent->item)) {
4004 case rewrite_one_ok:
4005 break;
4006 case rewrite_one_noparents:
4007 *pp = parent->next;
4008 continue;
4009 case rewrite_one_error:
4010 return -1;
4012 pp = &parent->next;
4014 remove_duplicate_parents(revs, commit);
4015 return 0;
4018 static int commit_match(struct commit *commit, struct rev_info *opt)
4020 int retval;
4021 const char *encoding;
4022 const char *message;
4023 struct strbuf buf = STRBUF_INIT;
4025 if (!opt->grep_filter.pattern_list && !opt->grep_filter.header_list)
4026 return 1;
4028 /* Prepend "fake" headers as needed */
4029 if (opt->grep_filter.use_reflog_filter) {
4030 strbuf_addstr(&buf, "reflog ");
4031 get_reflog_message(&buf, opt->reflog_info);
4032 strbuf_addch(&buf, '\n');
4036 * We grep in the user's output encoding, under the assumption that it
4037 * is the encoding they are most likely to write their grep pattern
4038 * for. In addition, it means we will match the "notes" encoding below,
4039 * so we will not end up with a buffer that has two different encodings
4040 * in it.
4042 encoding = get_log_output_encoding();
4043 message = repo_logmsg_reencode(the_repository, commit, NULL, encoding);
4045 /* Copy the commit to temporary if we are using "fake" headers */
4046 if (buf.len)
4047 strbuf_addstr(&buf, message);
4049 if (opt->grep_filter.header_list && opt->mailmap) {
4050 const char *commit_headers[] = { "author ", "committer ", NULL };
4052 if (!buf.len)
4053 strbuf_addstr(&buf, message);
4055 apply_mailmap_to_header(&buf, commit_headers, opt->mailmap);
4058 /* Append "fake" message parts as needed */
4059 if (opt->show_notes) {
4060 if (!buf.len)
4061 strbuf_addstr(&buf, message);
4062 format_display_notes(&commit->object.oid, &buf, encoding, 1);
4066 * Find either in the original commit message, or in the temporary.
4067 * Note that we cast away the constness of "message" here. It is
4068 * const because it may come from the cached commit buffer. That's OK,
4069 * because we know that it is modifiable heap memory, and that while
4070 * grep_buffer may modify it for speed, it will restore any
4071 * changes before returning.
4073 if (buf.len)
4074 retval = grep_buffer(&opt->grep_filter, buf.buf, buf.len);
4075 else
4076 retval = grep_buffer(&opt->grep_filter,
4077 (char *)message, strlen(message));
4078 strbuf_release(&buf);
4079 repo_unuse_commit_buffer(the_repository, commit, message);
4080 return retval;
4083 static inline int want_ancestry(const struct rev_info *revs)
4085 return (revs->rewrite_parents || revs->children.name);
4089 * Return a timestamp to be used for --since/--until comparisons for this
4090 * commit, based on the revision options.
4092 static timestamp_t comparison_date(const struct rev_info *revs,
4093 struct commit *commit)
4095 return revs->reflog_info ?
4096 get_reflog_timestamp(revs->reflog_info) :
4097 commit->date;
4100 enum commit_action get_commit_action(struct rev_info *revs, struct commit *commit)
4102 if (commit->object.flags & SHOWN)
4103 return commit_ignore;
4104 if (revs->unpacked && has_object_pack(&commit->object.oid))
4105 return commit_ignore;
4106 if (revs->no_kept_objects) {
4107 if (has_object_kept_pack(&commit->object.oid,
4108 revs->keep_pack_cache_flags))
4109 return commit_ignore;
4111 if (commit->object.flags & UNINTERESTING)
4112 return commit_ignore;
4113 if (revs->line_level_traverse && !want_ancestry(revs)) {
4115 * In case of line-level log with parent rewriting
4116 * prepare_revision_walk() already took care of all line-level
4117 * log filtering, and there is nothing left to do here.
4119 * If parent rewriting was not requested, then this is the
4120 * place to perform the line-level log filtering. Notably,
4121 * this check, though expensive, must come before the other,
4122 * cheaper filtering conditions, because the tracked line
4123 * ranges must be adjusted even when the commit will end up
4124 * being ignored based on other conditions.
4126 if (!line_log_process_ranges_arbitrary_commit(revs, commit))
4127 return commit_ignore;
4129 if (revs->min_age != -1 &&
4130 comparison_date(revs, commit) > revs->min_age)
4131 return commit_ignore;
4132 if (revs->max_age_as_filter != -1 &&
4133 comparison_date(revs, commit) < revs->max_age_as_filter)
4134 return commit_ignore;
4135 if (revs->min_parents || (revs->max_parents >= 0)) {
4136 int n = commit_list_count(commit->parents);
4137 if ((n < revs->min_parents) ||
4138 ((revs->max_parents >= 0) && (n > revs->max_parents)))
4139 return commit_ignore;
4141 if (!commit_match(commit, revs))
4142 return commit_ignore;
4143 if (revs->prune && revs->dense) {
4144 /* Commit without changes? */
4145 if (commit->object.flags & TREESAME) {
4146 int n;
4147 struct commit_list *p;
4148 /* drop merges unless we want parenthood */
4149 if (!want_ancestry(revs))
4150 return commit_ignore;
4152 if (revs->show_pulls && (commit->object.flags & PULL_MERGE))
4153 return commit_show;
4156 * If we want ancestry, then need to keep any merges
4157 * between relevant commits to tie together topology.
4158 * For consistency with TREESAME and simplification
4159 * use "relevant" here rather than just INTERESTING,
4160 * to treat bottom commit(s) as part of the topology.
4162 for (n = 0, p = commit->parents; p; p = p->next)
4163 if (relevant_commit(p->item))
4164 if (++n >= 2)
4165 return commit_show;
4166 return commit_ignore;
4169 return commit_show;
4172 define_commit_slab(saved_parents, struct commit_list *);
4174 #define EMPTY_PARENT_LIST ((struct commit_list *)-1)
4177 * You may only call save_parents() once per commit (this is checked
4178 * for non-root commits).
4180 static void save_parents(struct rev_info *revs, struct commit *commit)
4182 struct commit_list **pp;
4184 if (!revs->saved_parents_slab) {
4185 revs->saved_parents_slab = xmalloc(sizeof(struct saved_parents));
4186 init_saved_parents(revs->saved_parents_slab);
4189 pp = saved_parents_at(revs->saved_parents_slab, commit);
4192 * When walking with reflogs, we may visit the same commit
4193 * several times: once for each appearance in the reflog.
4195 * In this case, save_parents() will be called multiple times.
4196 * We want to keep only the first set of parents. We need to
4197 * store a sentinel value for an empty (i.e., NULL) parent
4198 * list to distinguish it from a not-yet-saved list, however.
4200 if (*pp)
4201 return;
4202 if (commit->parents)
4203 *pp = copy_commit_list(commit->parents);
4204 else
4205 *pp = EMPTY_PARENT_LIST;
4208 static void free_saved_parents(struct rev_info *revs)
4210 if (revs->saved_parents_slab)
4211 clear_saved_parents(revs->saved_parents_slab);
4214 struct commit_list *get_saved_parents(struct rev_info *revs, const struct commit *commit)
4216 struct commit_list *parents;
4218 if (!revs->saved_parents_slab)
4219 return commit->parents;
4221 parents = *saved_parents_at(revs->saved_parents_slab, commit);
4222 if (parents == EMPTY_PARENT_LIST)
4223 return NULL;
4224 return parents;
4227 enum commit_action simplify_commit(struct rev_info *revs, struct commit *commit)
4229 enum commit_action action = get_commit_action(revs, commit);
4231 if (action == commit_show &&
4232 revs->prune && revs->dense && want_ancestry(revs)) {
4234 * --full-diff on simplified parents is no good: it
4235 * will show spurious changes from the commits that
4236 * were elided. So we save the parents on the side
4237 * when --full-diff is in effect.
4239 if (revs->full_diff)
4240 save_parents(revs, commit);
4241 if (rewrite_parents(revs, commit, rewrite_one) < 0)
4242 return commit_error;
4244 return action;
4247 static void track_linear(struct rev_info *revs, struct commit *commit)
4249 if (revs->track_first_time) {
4250 revs->linear = 1;
4251 revs->track_first_time = 0;
4252 } else {
4253 struct commit_list *p;
4254 for (p = revs->previous_parents; p; p = p->next)
4255 if (p->item == NULL || /* first commit */
4256 oideq(&p->item->object.oid, &commit->object.oid))
4257 break;
4258 revs->linear = p != NULL;
4260 if (revs->reverse) {
4261 if (revs->linear)
4262 commit->object.flags |= TRACK_LINEAR;
4264 free_commit_list(revs->previous_parents);
4265 revs->previous_parents = copy_commit_list(commit->parents);
4268 static struct commit *get_revision_1(struct rev_info *revs)
4270 while (1) {
4271 struct commit *commit;
4273 if (revs->reflog_info)
4274 commit = next_reflog_entry(revs->reflog_info);
4275 else if (revs->topo_walk_info)
4276 commit = next_topo_commit(revs);
4277 else
4278 commit = pop_commit(&revs->commits);
4280 if (!commit)
4281 return NULL;
4283 if (revs->reflog_info)
4284 commit->object.flags &= ~(ADDED | SEEN | SHOWN);
4287 * If we haven't done the list limiting, we need to look at
4288 * the parents here. We also need to do the date-based limiting
4289 * that we'd otherwise have done in limit_list().
4291 if (!revs->limited) {
4292 if (revs->max_age != -1 &&
4293 comparison_date(revs, commit) < revs->max_age)
4294 continue;
4296 if (revs->reflog_info)
4297 try_to_simplify_commit(revs, commit);
4298 else if (revs->topo_walk_info)
4299 expand_topo_walk(revs, commit);
4300 else if (process_parents(revs, commit, &revs->commits, NULL) < 0) {
4301 if (!revs->ignore_missing_links)
4302 die("Failed to traverse parents of commit %s",
4303 oid_to_hex(&commit->object.oid));
4307 switch (simplify_commit(revs, commit)) {
4308 case commit_ignore:
4309 continue;
4310 case commit_error:
4311 die("Failed to simplify parents of commit %s",
4312 oid_to_hex(&commit->object.oid));
4313 default:
4314 if (revs->track_linear)
4315 track_linear(revs, commit);
4316 return commit;
4322 * Return true for entries that have not yet been shown. (This is an
4323 * object_array_each_func_t.)
4325 static int entry_unshown(struct object_array_entry *entry, void *cb_data UNUSED)
4327 return !(entry->item->flags & SHOWN);
4331 * If array is on the verge of a realloc, garbage-collect any entries
4332 * that have already been shown to try to free up some space.
4334 static void gc_boundary(struct object_array *array)
4336 if (array->nr == array->alloc)
4337 object_array_filter(array, entry_unshown, NULL);
4340 static void create_boundary_commit_list(struct rev_info *revs)
4342 unsigned i;
4343 struct commit *c;
4344 struct object_array *array = &revs->boundary_commits;
4345 struct object_array_entry *objects = array->objects;
4348 * If revs->commits is non-NULL at this point, an error occurred in
4349 * get_revision_1(). Ignore the error and continue printing the
4350 * boundary commits anyway. (This is what the code has always
4351 * done.)
4353 free_commit_list(revs->commits);
4354 revs->commits = NULL;
4357 * Put all of the actual boundary commits from revs->boundary_commits
4358 * into revs->commits
4360 for (i = 0; i < array->nr; i++) {
4361 c = (struct commit *)(objects[i].item);
4362 if (!c)
4363 continue;
4364 if (!(c->object.flags & CHILD_SHOWN))
4365 continue;
4366 if (c->object.flags & (SHOWN | BOUNDARY))
4367 continue;
4368 c->object.flags |= BOUNDARY;
4369 commit_list_insert(c, &revs->commits);
4373 * If revs->topo_order is set, sort the boundary commits
4374 * in topological order
4376 sort_in_topological_order(&revs->commits, revs->sort_order);
4379 static struct commit *get_revision_internal(struct rev_info *revs)
4381 struct commit *c = NULL;
4382 struct commit_list *l;
4384 if (revs->boundary == 2) {
4386 * All of the normal commits have already been returned,
4387 * and we are now returning boundary commits.
4388 * create_boundary_commit_list() has populated
4389 * revs->commits with the remaining commits to return.
4391 c = pop_commit(&revs->commits);
4392 if (c)
4393 c->object.flags |= SHOWN;
4394 return c;
4398 * If our max_count counter has reached zero, then we are done. We
4399 * don't simply return NULL because we still might need to show
4400 * boundary commits. But we want to avoid calling get_revision_1, which
4401 * might do a considerable amount of work finding the next commit only
4402 * for us to throw it away.
4404 * If it is non-zero, then either we don't have a max_count at all
4405 * (-1), or it is still counting, in which case we decrement.
4407 if (revs->max_count) {
4408 c = get_revision_1(revs);
4409 if (c) {
4410 while (revs->skip_count > 0) {
4411 revs->skip_count--;
4412 c = get_revision_1(revs);
4413 if (!c)
4414 break;
4415 free_commit_buffer(revs->repo->parsed_objects, c);
4419 if (revs->max_count > 0)
4420 revs->max_count--;
4423 if (c)
4424 c->object.flags |= SHOWN;
4426 if (!revs->boundary)
4427 return c;
4429 if (!c) {
4431 * get_revision_1() runs out the commits, and
4432 * we are done computing the boundaries.
4433 * switch to boundary commits output mode.
4435 revs->boundary = 2;
4438 * Update revs->commits to contain the list of
4439 * boundary commits.
4441 create_boundary_commit_list(revs);
4443 return get_revision_internal(revs);
4447 * boundary commits are the commits that are parents of the
4448 * ones we got from get_revision_1() but they themselves are
4449 * not returned from get_revision_1(). Before returning
4450 * 'c', we need to mark its parents that they could be boundaries.
4453 for (l = c->parents; l; l = l->next) {
4454 struct object *p;
4455 p = &(l->item->object);
4456 if (p->flags & (CHILD_SHOWN | SHOWN))
4457 continue;
4458 p->flags |= CHILD_SHOWN;
4459 gc_boundary(&revs->boundary_commits);
4460 add_object_array(p, NULL, &revs->boundary_commits);
4463 return c;
4466 struct commit *get_revision(struct rev_info *revs)
4468 struct commit *c;
4469 struct commit_list *reversed;
4471 if (revs->reverse) {
4472 reversed = NULL;
4473 while ((c = get_revision_internal(revs)))
4474 commit_list_insert(c, &reversed);
4475 free_commit_list(revs->commits);
4476 revs->commits = reversed;
4477 revs->reverse = 0;
4478 revs->reverse_output_stage = 1;
4481 if (revs->reverse_output_stage) {
4482 c = pop_commit(&revs->commits);
4483 if (revs->track_linear)
4484 revs->linear = !!(c && c->object.flags & TRACK_LINEAR);
4485 return c;
4488 c = get_revision_internal(revs);
4489 if (c && revs->graph)
4490 graph_update(revs->graph, c);
4491 if (!c) {
4492 free_saved_parents(revs);
4493 free_commit_list(revs->previous_parents);
4494 revs->previous_parents = NULL;
4496 return c;
4499 const char *get_revision_mark(const struct rev_info *revs, const struct commit *commit)
4501 if (commit->object.flags & BOUNDARY)
4502 return "-";
4503 else if (commit->object.flags & UNINTERESTING)
4504 return "^";
4505 else if (commit->object.flags & PATCHSAME)
4506 return "=";
4507 else if (!revs || revs->left_right) {
4508 if (commit->object.flags & SYMMETRIC_LEFT)
4509 return "<";
4510 else
4511 return ">";
4512 } else if (revs->graph)
4513 return "*";
4514 else if (revs->cherry_mark)
4515 return "+";
4516 return "";
4519 void put_revision_mark(const struct rev_info *revs, const struct commit *commit)
4521 const char *mark = get_revision_mark(revs, commit);
4522 if (!strlen(mark))
4523 return;
4524 fputs(mark, stdout);
4525 putchar(' ');