Merge branch 'db/date-underflow-fix'
[git.git] / revision.c
blob3959ccc3d90db12cb4bbc27208503f43d239fff6
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)
852 struct tree *t1 = repo_get_commit_tree(the_repository, commit);
854 if (!t1)
855 return 0;
857 tree_difference = REV_TREE_SAME;
858 revs->pruning.flags.has_changes = 0;
859 diff_tree_oid(NULL, &t1->object.oid, "", &revs->pruning);
861 return tree_difference == REV_TREE_SAME;
864 struct treesame_state {
865 unsigned int nparents;
866 unsigned char treesame[FLEX_ARRAY];
869 static struct treesame_state *initialise_treesame(struct rev_info *revs, struct commit *commit)
871 unsigned n = commit_list_count(commit->parents);
872 struct treesame_state *st = xcalloc(1, st_add(sizeof(*st), n));
873 st->nparents = n;
874 add_decoration(&revs->treesame, &commit->object, st);
875 return st;
879 * Must be called immediately after removing the nth_parent from a commit's
880 * parent list, if we are maintaining the per-parent treesame[] decoration.
881 * This does not recalculate the master TREESAME flag - update_treesame()
882 * should be called to update it after a sequence of treesame[] modifications
883 * that may have affected it.
885 static int compact_treesame(struct rev_info *revs, struct commit *commit, unsigned nth_parent)
887 struct treesame_state *st;
888 int old_same;
890 if (!commit->parents) {
892 * Have just removed the only parent from a non-merge.
893 * Different handling, as we lack decoration.
895 if (nth_parent != 0)
896 die("compact_treesame %u", nth_parent);
897 old_same = !!(commit->object.flags & TREESAME);
898 if (rev_same_tree_as_empty(revs, commit))
899 commit->object.flags |= TREESAME;
900 else
901 commit->object.flags &= ~TREESAME;
902 return old_same;
905 st = lookup_decoration(&revs->treesame, &commit->object);
906 if (!st || nth_parent >= st->nparents)
907 die("compact_treesame %u", nth_parent);
909 old_same = st->treesame[nth_parent];
910 memmove(st->treesame + nth_parent,
911 st->treesame + nth_parent + 1,
912 st->nparents - nth_parent - 1);
915 * If we've just become a non-merge commit, update TREESAME
916 * immediately, and remove the no-longer-needed decoration.
917 * If still a merge, defer update until update_treesame().
919 if (--st->nparents == 1) {
920 if (commit->parents->next)
921 die("compact_treesame parents mismatch");
922 if (st->treesame[0] && revs->dense)
923 commit->object.flags |= TREESAME;
924 else
925 commit->object.flags &= ~TREESAME;
926 free(add_decoration(&revs->treesame, &commit->object, NULL));
929 return old_same;
932 static unsigned update_treesame(struct rev_info *revs, struct commit *commit)
934 if (commit->parents && commit->parents->next) {
935 unsigned n;
936 struct treesame_state *st;
937 struct commit_list *p;
938 unsigned relevant_parents;
939 unsigned relevant_change, irrelevant_change;
941 st = lookup_decoration(&revs->treesame, &commit->object);
942 if (!st)
943 die("update_treesame %s", oid_to_hex(&commit->object.oid));
944 relevant_parents = 0;
945 relevant_change = irrelevant_change = 0;
946 for (p = commit->parents, n = 0; p; n++, p = p->next) {
947 if (relevant_commit(p->item)) {
948 relevant_change |= !st->treesame[n];
949 relevant_parents++;
950 } else
951 irrelevant_change |= !st->treesame[n];
953 if (relevant_parents ? relevant_change : irrelevant_change)
954 commit->object.flags &= ~TREESAME;
955 else
956 commit->object.flags |= TREESAME;
959 return commit->object.flags & TREESAME;
962 static inline int limiting_can_increase_treesame(const struct rev_info *revs)
965 * TREESAME is irrelevant unless prune && dense;
966 * if simplify_history is set, we can't have a mixture of TREESAME and
967 * !TREESAME INTERESTING parents (and we don't have treesame[]
968 * decoration anyway);
969 * if first_parent_only is set, then the TREESAME flag is locked
970 * against the first parent (and again we lack treesame[] decoration).
972 return revs->prune && revs->dense &&
973 !revs->simplify_history &&
974 !revs->first_parent_only;
977 static void try_to_simplify_commit(struct rev_info *revs, struct commit *commit)
979 struct commit_list **pp, *parent;
980 struct treesame_state *ts = NULL;
981 int relevant_change = 0, irrelevant_change = 0;
982 int relevant_parents, nth_parent;
985 * If we don't do pruning, everything is interesting
987 if (!revs->prune)
988 return;
990 if (!repo_get_commit_tree(the_repository, commit))
991 return;
993 if (!commit->parents) {
994 if (rev_same_tree_as_empty(revs, commit))
995 commit->object.flags |= TREESAME;
996 return;
1000 * Normal non-merge commit? If we don't want to make the
1001 * history dense, we consider it always to be a change..
1003 if (!revs->dense && !commit->parents->next)
1004 return;
1006 for (pp = &commit->parents, nth_parent = 0, relevant_parents = 0;
1007 (parent = *pp) != NULL;
1008 pp = &parent->next, nth_parent++) {
1009 struct commit *p = parent->item;
1010 if (relevant_commit(p))
1011 relevant_parents++;
1013 if (nth_parent == 1) {
1015 * This our second loop iteration - so we now know
1016 * we're dealing with a merge.
1018 * Do not compare with later parents when we care only about
1019 * the first parent chain, in order to avoid derailing the
1020 * traversal to follow a side branch that brought everything
1021 * in the path we are limited to by the pathspec.
1023 if (revs->first_parent_only)
1024 break;
1026 * If this will remain a potentially-simplifiable
1027 * merge, remember per-parent treesame if needed.
1028 * Initialise the array with the comparison from our
1029 * first iteration.
1031 if (revs->treesame.name &&
1032 !revs->simplify_history &&
1033 !(commit->object.flags & UNINTERESTING)) {
1034 ts = initialise_treesame(revs, commit);
1035 if (!(irrelevant_change || relevant_change))
1036 ts->treesame[0] = 1;
1039 if (repo_parse_commit(revs->repo, p) < 0)
1040 die("cannot simplify commit %s (because of %s)",
1041 oid_to_hex(&commit->object.oid),
1042 oid_to_hex(&p->object.oid));
1043 switch (rev_compare_tree(revs, p, commit, nth_parent)) {
1044 case REV_TREE_SAME:
1045 if (!revs->simplify_history || !relevant_commit(p)) {
1046 /* Even if a merge with an uninteresting
1047 * side branch brought the entire change
1048 * we are interested in, we do not want
1049 * to lose the other branches of this
1050 * merge, so we just keep going.
1052 if (ts)
1053 ts->treesame[nth_parent] = 1;
1054 continue;
1056 parent->next = NULL;
1057 commit->parents = parent;
1060 * A merge commit is a "diversion" if it is not
1061 * TREESAME to its first parent but is TREESAME
1062 * to a later parent. In the simplified history,
1063 * we "divert" the history walk to the later
1064 * parent. These commits are shown when "show_pulls"
1065 * is enabled, so do not mark the object as
1066 * TREESAME here.
1068 if (!revs->show_pulls || !nth_parent)
1069 commit->object.flags |= TREESAME;
1071 return;
1073 case REV_TREE_NEW:
1074 if (revs->remove_empty_trees &&
1075 rev_same_tree_as_empty(revs, p)) {
1076 /* We are adding all the specified
1077 * paths from this parent, so the
1078 * history beyond this parent is not
1079 * interesting. Remove its parents
1080 * (they are grandparents for us).
1081 * IOW, we pretend this parent is a
1082 * "root" commit.
1084 if (repo_parse_commit(revs->repo, p) < 0)
1085 die("cannot simplify commit %s (invalid %s)",
1086 oid_to_hex(&commit->object.oid),
1087 oid_to_hex(&p->object.oid));
1088 p->parents = NULL;
1090 /* fallthrough */
1091 case REV_TREE_OLD:
1092 case REV_TREE_DIFFERENT:
1093 if (relevant_commit(p))
1094 relevant_change = 1;
1095 else
1096 irrelevant_change = 1;
1098 if (!nth_parent)
1099 commit->object.flags |= PULL_MERGE;
1101 continue;
1103 die("bad tree compare for commit %s", oid_to_hex(&commit->object.oid));
1107 * TREESAME is straightforward for single-parent commits. For merge
1108 * commits, it is most useful to define it so that "irrelevant"
1109 * parents cannot make us !TREESAME - if we have any relevant
1110 * parents, then we only consider TREESAMEness with respect to them,
1111 * allowing irrelevant merges from uninteresting branches to be
1112 * simplified away. Only if we have only irrelevant parents do we
1113 * base TREESAME on them. Note that this logic is replicated in
1114 * update_treesame, which should be kept in sync.
1116 if (relevant_parents ? !relevant_change : !irrelevant_change)
1117 commit->object.flags |= TREESAME;
1120 static int process_parents(struct rev_info *revs, struct commit *commit,
1121 struct commit_list **list, struct prio_queue *queue)
1123 struct commit_list *parent = commit->parents;
1124 unsigned pass_flags;
1126 if (commit->object.flags & ADDED)
1127 return 0;
1128 if (revs->do_not_die_on_missing_objects &&
1129 oidset_contains(&revs->missing_commits, &commit->object.oid))
1130 return 0;
1131 commit->object.flags |= ADDED;
1133 if (revs->include_check &&
1134 !revs->include_check(commit, revs->include_check_data))
1135 return 0;
1138 * If the commit is uninteresting, don't try to
1139 * prune parents - we want the maximal uninteresting
1140 * set.
1142 * Normally we haven't parsed the parent
1143 * yet, so we won't have a parent of a parent
1144 * here. However, it may turn out that we've
1145 * reached this commit some other way (where it
1146 * wasn't uninteresting), in which case we need
1147 * to mark its parents recursively too..
1149 if (commit->object.flags & UNINTERESTING) {
1150 while (parent) {
1151 struct commit *p = parent->item;
1152 parent = parent->next;
1153 if (p)
1154 p->object.flags |= UNINTERESTING;
1155 if (repo_parse_commit_gently(revs->repo, p, 1) < 0)
1156 continue;
1157 if (p->parents)
1158 mark_parents_uninteresting(revs, p);
1159 if (p->object.flags & SEEN)
1160 continue;
1161 p->object.flags |= (SEEN | NOT_USER_GIVEN);
1162 if (list)
1163 commit_list_insert_by_date(p, list);
1164 if (queue)
1165 prio_queue_put(queue, p);
1166 if (revs->exclude_first_parent_only)
1167 break;
1169 return 0;
1173 * Ok, the commit wasn't uninteresting. Try to
1174 * simplify the commit history and find the parent
1175 * that has no differences in the path set if one exists.
1177 try_to_simplify_commit(revs, commit);
1179 if (revs->no_walk)
1180 return 0;
1182 pass_flags = (commit->object.flags & (SYMMETRIC_LEFT | ANCESTRY_PATH));
1184 for (parent = commit->parents; parent; parent = parent->next) {
1185 struct commit *p = parent->item;
1186 int gently = revs->ignore_missing_links ||
1187 revs->exclude_promisor_objects ||
1188 revs->do_not_die_on_missing_objects;
1189 if (repo_parse_commit_gently(revs->repo, p, gently) < 0) {
1190 if (revs->exclude_promisor_objects &&
1191 is_promisor_object(&p->object.oid)) {
1192 if (revs->first_parent_only)
1193 break;
1194 continue;
1197 if (revs->do_not_die_on_missing_objects)
1198 oidset_insert(&revs->missing_commits, &p->object.oid);
1199 else
1200 return -1; /* corrupt repository */
1202 if (revs->sources) {
1203 char **slot = revision_sources_at(revs->sources, p);
1205 if (!*slot)
1206 *slot = *revision_sources_at(revs->sources, commit);
1208 p->object.flags |= pass_flags;
1209 if (!(p->object.flags & SEEN)) {
1210 p->object.flags |= (SEEN | NOT_USER_GIVEN);
1211 if (list)
1212 commit_list_insert_by_date(p, list);
1213 if (queue)
1214 prio_queue_put(queue, p);
1216 if (revs->first_parent_only)
1217 break;
1219 return 0;
1222 static void cherry_pick_list(struct commit_list *list, struct rev_info *revs)
1224 struct commit_list *p;
1225 int left_count = 0, right_count = 0;
1226 int left_first;
1227 struct patch_ids ids;
1228 unsigned cherry_flag;
1230 /* First count the commits on the left and on the right */
1231 for (p = list; p; p = p->next) {
1232 struct commit *commit = p->item;
1233 unsigned flags = commit->object.flags;
1234 if (flags & BOUNDARY)
1236 else if (flags & SYMMETRIC_LEFT)
1237 left_count++;
1238 else
1239 right_count++;
1242 if (!left_count || !right_count)
1243 return;
1245 left_first = left_count < right_count;
1246 init_patch_ids(revs->repo, &ids);
1247 ids.diffopts.pathspec = revs->diffopt.pathspec;
1249 /* Compute patch-ids for one side */
1250 for (p = list; p; p = p->next) {
1251 struct commit *commit = p->item;
1252 unsigned flags = commit->object.flags;
1254 if (flags & BOUNDARY)
1255 continue;
1257 * If we have fewer left, left_first is set and we omit
1258 * commits on the right branch in this loop. If we have
1259 * fewer right, we skip the left ones.
1261 if (left_first != !!(flags & SYMMETRIC_LEFT))
1262 continue;
1263 add_commit_patch_id(commit, &ids);
1266 /* either cherry_mark or cherry_pick are true */
1267 cherry_flag = revs->cherry_mark ? PATCHSAME : SHOWN;
1269 /* Check the other side */
1270 for (p = list; p; p = p->next) {
1271 struct commit *commit = p->item;
1272 struct patch_id *id;
1273 unsigned flags = commit->object.flags;
1275 if (flags & BOUNDARY)
1276 continue;
1278 * If we have fewer left, left_first is set and we omit
1279 * commits on the left branch in this loop.
1281 if (left_first == !!(flags & SYMMETRIC_LEFT))
1282 continue;
1285 * Have we seen the same patch id?
1287 id = patch_id_iter_first(commit, &ids);
1288 if (!id)
1289 continue;
1291 commit->object.flags |= cherry_flag;
1292 do {
1293 id->commit->object.flags |= cherry_flag;
1294 } while ((id = patch_id_iter_next(id, &ids)));
1297 free_patch_ids(&ids);
1300 /* How many extra uninteresting commits we want to see.. */
1301 #define SLOP 5
1303 static int still_interesting(struct commit_list *src, timestamp_t date, int slop,
1304 struct commit **interesting_cache)
1307 * No source list at all? We're definitely done..
1309 if (!src)
1310 return 0;
1313 * Does the destination list contain entries with a date
1314 * before the source list? Definitely _not_ done.
1316 if (date <= src->item->date)
1317 return SLOP;
1320 * Does the source list still have interesting commits in
1321 * it? Definitely not done..
1323 if (!everybody_uninteresting(src, interesting_cache))
1324 return SLOP;
1326 /* Ok, we're closing in.. */
1327 return slop-1;
1331 * "rev-list --ancestry-path=C_0 [--ancestry-path=C_1 ...] A..B"
1332 * computes commits that are ancestors of B but not ancestors of A but
1333 * further limits the result to those that have any of C in their
1334 * ancestry path (i.e. are either ancestors of any of C, descendants
1335 * of any of C, or are any of C). If --ancestry-path is specified with
1336 * no commit, we use all bottom commits for C.
1338 * Before this function is called, ancestors of C will have already
1339 * been marked with ANCESTRY_PATH previously.
1341 * This takes the list of bottom commits and the result of "A..B"
1342 * without --ancestry-path, and limits the latter further to the ones
1343 * that have any of C in their ancestry path. Since the ancestors of C
1344 * have already been marked (a prerequisite of this function), we just
1345 * need to mark the descendants, then exclude any commit that does not
1346 * have any of these marks.
1348 static void limit_to_ancestry(struct commit_list *bottoms, struct commit_list *list)
1350 struct commit_list *p;
1351 struct commit_list *rlist = NULL;
1352 int made_progress;
1355 * Reverse the list so that it will be likely that we would
1356 * process parents before children.
1358 for (p = list; p; p = p->next)
1359 commit_list_insert(p->item, &rlist);
1361 for (p = bottoms; p; p = p->next)
1362 p->item->object.flags |= TMP_MARK;
1365 * Mark the ones that can reach bottom commits in "list",
1366 * in a bottom-up fashion.
1368 do {
1369 made_progress = 0;
1370 for (p = rlist; p; p = p->next) {
1371 struct commit *c = p->item;
1372 struct commit_list *parents;
1373 if (c->object.flags & (TMP_MARK | UNINTERESTING))
1374 continue;
1375 for (parents = c->parents;
1376 parents;
1377 parents = parents->next) {
1378 if (!(parents->item->object.flags & TMP_MARK))
1379 continue;
1380 c->object.flags |= TMP_MARK;
1381 made_progress = 1;
1382 break;
1385 } while (made_progress);
1388 * NEEDSWORK: decide if we want to remove parents that are
1389 * not marked with TMP_MARK from commit->parents for commits
1390 * in the resulting list. We may not want to do that, though.
1394 * The ones that are not marked with either TMP_MARK or
1395 * ANCESTRY_PATH are uninteresting
1397 for (p = list; p; p = p->next) {
1398 struct commit *c = p->item;
1399 if (c->object.flags & (TMP_MARK | ANCESTRY_PATH))
1400 continue;
1401 c->object.flags |= UNINTERESTING;
1404 /* We are done with TMP_MARK and ANCESTRY_PATH */
1405 for (p = list; p; p = p->next)
1406 p->item->object.flags &= ~(TMP_MARK | ANCESTRY_PATH);
1407 for (p = bottoms; p; p = p->next)
1408 p->item->object.flags &= ~(TMP_MARK | ANCESTRY_PATH);
1409 free_commit_list(rlist);
1413 * Before walking the history, add the set of "negative" refs the
1414 * caller has asked to exclude to the bottom list.
1416 * This is used to compute "rev-list --ancestry-path A..B", as we need
1417 * to filter the result of "A..B" further to the ones that can actually
1418 * reach A.
1420 static void collect_bottom_commits(struct commit_list *list,
1421 struct commit_list **bottom)
1423 struct commit_list *elem;
1424 for (elem = list; elem; elem = elem->next)
1425 if (elem->item->object.flags & BOTTOM)
1426 commit_list_insert(elem->item, bottom);
1429 /* Assumes either left_only or right_only is set */
1430 static void limit_left_right(struct commit_list *list, struct rev_info *revs)
1432 struct commit_list *p;
1434 for (p = list; p; p = p->next) {
1435 struct commit *commit = p->item;
1437 if (revs->right_only) {
1438 if (commit->object.flags & SYMMETRIC_LEFT)
1439 commit->object.flags |= SHOWN;
1440 } else /* revs->left_only is set */
1441 if (!(commit->object.flags & SYMMETRIC_LEFT))
1442 commit->object.flags |= SHOWN;
1446 static int limit_list(struct rev_info *revs)
1448 int slop = SLOP;
1449 timestamp_t date = TIME_MAX;
1450 struct commit_list *original_list = revs->commits;
1451 struct commit_list *newlist = NULL;
1452 struct commit_list **p = &newlist;
1453 struct commit *interesting_cache = NULL;
1455 if (revs->ancestry_path_implicit_bottoms) {
1456 collect_bottom_commits(original_list,
1457 &revs->ancestry_path_bottoms);
1458 if (!revs->ancestry_path_bottoms)
1459 die("--ancestry-path given but there are no bottom commits");
1462 while (original_list) {
1463 struct commit *commit = pop_commit(&original_list);
1464 struct object *obj = &commit->object;
1465 show_early_output_fn_t show;
1467 if (commit == interesting_cache)
1468 interesting_cache = NULL;
1470 if (revs->max_age != -1 && (commit->date < revs->max_age))
1471 obj->flags |= UNINTERESTING;
1472 if (process_parents(revs, commit, &original_list, NULL) < 0)
1473 return -1;
1474 if (obj->flags & UNINTERESTING) {
1475 mark_parents_uninteresting(revs, commit);
1476 slop = still_interesting(original_list, date, slop, &interesting_cache);
1477 if (slop)
1478 continue;
1479 break;
1481 if (revs->min_age != -1 && (commit->date > revs->min_age) &&
1482 !revs->line_level_traverse)
1483 continue;
1484 if (revs->max_age_as_filter != -1 &&
1485 (commit->date < revs->max_age_as_filter) && !revs->line_level_traverse)
1486 continue;
1487 date = commit->date;
1488 p = &commit_list_insert(commit, p)->next;
1490 show = show_early_output;
1491 if (!show)
1492 continue;
1494 show(revs, newlist);
1495 show_early_output = NULL;
1497 if (revs->cherry_pick || revs->cherry_mark)
1498 cherry_pick_list(newlist, revs);
1500 if (revs->left_only || revs->right_only)
1501 limit_left_right(newlist, revs);
1503 if (revs->ancestry_path)
1504 limit_to_ancestry(revs->ancestry_path_bottoms, newlist);
1507 * Check if any commits have become TREESAME by some of their parents
1508 * becoming UNINTERESTING.
1510 if (limiting_can_increase_treesame(revs)) {
1511 struct commit_list *list = NULL;
1512 for (list = newlist; list; list = list->next) {
1513 struct commit *c = list->item;
1514 if (c->object.flags & (UNINTERESTING | TREESAME))
1515 continue;
1516 update_treesame(revs, c);
1520 free_commit_list(original_list);
1521 revs->commits = newlist;
1522 return 0;
1526 * Add an entry to refs->cmdline with the specified information.
1527 * *name is copied.
1529 static void add_rev_cmdline(struct rev_info *revs,
1530 struct object *item,
1531 const char *name,
1532 int whence,
1533 unsigned flags)
1535 struct rev_cmdline_info *info = &revs->cmdline;
1536 unsigned int nr = info->nr;
1538 ALLOC_GROW(info->rev, nr + 1, info->alloc);
1539 info->rev[nr].item = item;
1540 info->rev[nr].name = xstrdup(name);
1541 info->rev[nr].whence = whence;
1542 info->rev[nr].flags = flags;
1543 info->nr++;
1546 static void add_rev_cmdline_list(struct rev_info *revs,
1547 struct commit_list *commit_list,
1548 int whence,
1549 unsigned flags)
1551 while (commit_list) {
1552 struct object *object = &commit_list->item->object;
1553 add_rev_cmdline(revs, object, oid_to_hex(&object->oid),
1554 whence, flags);
1555 commit_list = commit_list->next;
1559 int ref_excluded(const struct ref_exclusions *exclusions, const char *path)
1561 const char *stripped_path = strip_namespace(path);
1562 struct string_list_item *item;
1564 for_each_string_list_item(item, &exclusions->excluded_refs) {
1565 if (!wildmatch(item->string, path, 0))
1566 return 1;
1569 if (ref_is_hidden(stripped_path, path, &exclusions->hidden_refs))
1570 return 1;
1572 return 0;
1575 void init_ref_exclusions(struct ref_exclusions *exclusions)
1577 struct ref_exclusions blank = REF_EXCLUSIONS_INIT;
1578 memcpy(exclusions, &blank, sizeof(*exclusions));
1581 void clear_ref_exclusions(struct ref_exclusions *exclusions)
1583 string_list_clear(&exclusions->excluded_refs, 0);
1584 strvec_clear(&exclusions->hidden_refs);
1585 exclusions->hidden_refs_configured = 0;
1588 void add_ref_exclusion(struct ref_exclusions *exclusions, const char *exclude)
1590 string_list_append(&exclusions->excluded_refs, exclude);
1593 struct exclude_hidden_refs_cb {
1594 struct ref_exclusions *exclusions;
1595 const char *section;
1598 static int hide_refs_config(const char *var, const char *value,
1599 const struct config_context *ctx UNUSED,
1600 void *cb_data)
1602 struct exclude_hidden_refs_cb *cb = cb_data;
1603 cb->exclusions->hidden_refs_configured = 1;
1604 return parse_hide_refs_config(var, value, cb->section,
1605 &cb->exclusions->hidden_refs);
1608 void exclude_hidden_refs(struct ref_exclusions *exclusions, const char *section)
1610 struct exclude_hidden_refs_cb cb;
1612 if (strcmp(section, "fetch") && strcmp(section, "receive") &&
1613 strcmp(section, "uploadpack"))
1614 die(_("unsupported section for hidden refs: %s"), section);
1616 if (exclusions->hidden_refs_configured)
1617 die(_("--exclude-hidden= passed more than once"));
1619 cb.exclusions = exclusions;
1620 cb.section = section;
1622 git_config(hide_refs_config, &cb);
1625 struct all_refs_cb {
1626 int all_flags;
1627 int warned_bad_reflog;
1628 struct rev_info *all_revs;
1629 const char *name_for_errormsg;
1630 struct worktree *wt;
1633 static int handle_one_ref(const char *path, const struct object_id *oid,
1634 int flag UNUSED,
1635 void *cb_data)
1637 struct all_refs_cb *cb = cb_data;
1638 struct object *object;
1640 if (ref_excluded(&cb->all_revs->ref_excludes, path))
1641 return 0;
1643 object = get_reference(cb->all_revs, path, oid, cb->all_flags);
1644 add_rev_cmdline(cb->all_revs, object, path, REV_CMD_REF, cb->all_flags);
1645 add_pending_object(cb->all_revs, object, path);
1646 return 0;
1649 static void init_all_refs_cb(struct all_refs_cb *cb, struct rev_info *revs,
1650 unsigned flags)
1652 cb->all_revs = revs;
1653 cb->all_flags = flags;
1654 revs->rev_input_given = 1;
1655 cb->wt = NULL;
1658 static void handle_refs(struct ref_store *refs,
1659 struct rev_info *revs, unsigned flags,
1660 int (*for_each)(struct ref_store *, each_ref_fn, void *))
1662 struct all_refs_cb cb;
1664 if (!refs) {
1665 /* this could happen with uninitialized submodules */
1666 return;
1669 init_all_refs_cb(&cb, revs, flags);
1670 for_each(refs, handle_one_ref, &cb);
1673 static void handle_one_reflog_commit(struct object_id *oid, void *cb_data)
1675 struct all_refs_cb *cb = cb_data;
1676 if (!is_null_oid(oid)) {
1677 struct object *o = parse_object(cb->all_revs->repo, oid);
1678 if (o) {
1679 o->flags |= cb->all_flags;
1680 /* ??? CMDLINEFLAGS ??? */
1681 add_pending_object(cb->all_revs, o, "");
1683 else if (!cb->warned_bad_reflog) {
1684 warning("reflog of '%s' references pruned commits",
1685 cb->name_for_errormsg);
1686 cb->warned_bad_reflog = 1;
1691 static int handle_one_reflog_ent(struct object_id *ooid, struct object_id *noid,
1692 const char *email UNUSED,
1693 timestamp_t timestamp UNUSED,
1694 int tz UNUSED,
1695 const char *message UNUSED,
1696 void *cb_data)
1698 handle_one_reflog_commit(ooid, cb_data);
1699 handle_one_reflog_commit(noid, cb_data);
1700 return 0;
1703 static int handle_one_reflog(const char *refname_in_wt, void *cb_data)
1705 struct all_refs_cb *cb = cb_data;
1706 struct strbuf refname = STRBUF_INIT;
1708 cb->warned_bad_reflog = 0;
1709 strbuf_worktree_ref(cb->wt, &refname, refname_in_wt);
1710 cb->name_for_errormsg = refname.buf;
1711 refs_for_each_reflog_ent(get_main_ref_store(the_repository),
1712 refname.buf,
1713 handle_one_reflog_ent, cb_data);
1714 strbuf_release(&refname);
1715 return 0;
1718 static void add_other_reflogs_to_pending(struct all_refs_cb *cb)
1720 struct worktree **worktrees, **p;
1722 worktrees = get_worktrees();
1723 for (p = worktrees; *p; p++) {
1724 struct worktree *wt = *p;
1726 if (wt->is_current)
1727 continue;
1729 cb->wt = wt;
1730 refs_for_each_reflog(get_worktree_ref_store(wt),
1731 handle_one_reflog,
1732 cb);
1734 free_worktrees(worktrees);
1737 void add_reflogs_to_pending(struct rev_info *revs, unsigned flags)
1739 struct all_refs_cb cb;
1741 cb.all_revs = revs;
1742 cb.all_flags = flags;
1743 cb.wt = NULL;
1744 refs_for_each_reflog(get_main_ref_store(the_repository),
1745 handle_one_reflog, &cb);
1747 if (!revs->single_worktree)
1748 add_other_reflogs_to_pending(&cb);
1751 static void add_cache_tree(struct cache_tree *it, struct rev_info *revs,
1752 struct strbuf *path, unsigned int flags)
1754 size_t baselen = path->len;
1755 int i;
1757 if (it->entry_count >= 0) {
1758 struct tree *tree = lookup_tree(revs->repo, &it->oid);
1759 tree->object.flags |= flags;
1760 add_pending_object_with_path(revs, &tree->object, "",
1761 040000, path->buf);
1764 for (i = 0; i < it->subtree_nr; i++) {
1765 struct cache_tree_sub *sub = it->down[i];
1766 strbuf_addf(path, "%s%s", baselen ? "/" : "", sub->name);
1767 add_cache_tree(sub->cache_tree, revs, path, flags);
1768 strbuf_setlen(path, baselen);
1773 static void add_resolve_undo_to_pending(struct index_state *istate, struct rev_info *revs)
1775 struct string_list_item *item;
1776 struct string_list *resolve_undo = istate->resolve_undo;
1778 if (!resolve_undo)
1779 return;
1781 for_each_string_list_item(item, resolve_undo) {
1782 const char *path = item->string;
1783 struct resolve_undo_info *ru = item->util;
1784 int i;
1786 if (!ru)
1787 continue;
1788 for (i = 0; i < 3; i++) {
1789 struct blob *blob;
1791 if (!ru->mode[i] || !S_ISREG(ru->mode[i]))
1792 continue;
1794 blob = lookup_blob(revs->repo, &ru->oid[i]);
1795 if (!blob) {
1796 warning(_("resolve-undo records `%s` which is missing"),
1797 oid_to_hex(&ru->oid[i]));
1798 continue;
1800 add_pending_object_with_path(revs, &blob->object, "",
1801 ru->mode[i], path);
1806 static void do_add_index_objects_to_pending(struct rev_info *revs,
1807 struct index_state *istate,
1808 unsigned int flags)
1810 int i;
1812 /* TODO: audit for interaction with sparse-index. */
1813 ensure_full_index(istate);
1814 for (i = 0; i < istate->cache_nr; i++) {
1815 struct cache_entry *ce = istate->cache[i];
1816 struct blob *blob;
1818 if (S_ISGITLINK(ce->ce_mode))
1819 continue;
1821 blob = lookup_blob(revs->repo, &ce->oid);
1822 if (!blob)
1823 die("unable to add index blob to traversal");
1824 blob->object.flags |= flags;
1825 add_pending_object_with_path(revs, &blob->object, "",
1826 ce->ce_mode, ce->name);
1829 if (istate->cache_tree) {
1830 struct strbuf path = STRBUF_INIT;
1831 add_cache_tree(istate->cache_tree, revs, &path, flags);
1832 strbuf_release(&path);
1835 add_resolve_undo_to_pending(istate, revs);
1838 void add_index_objects_to_pending(struct rev_info *revs, unsigned int flags)
1840 struct worktree **worktrees, **p;
1842 repo_read_index(revs->repo);
1843 do_add_index_objects_to_pending(revs, revs->repo->index, flags);
1845 if (revs->single_worktree)
1846 return;
1848 worktrees = get_worktrees();
1849 for (p = worktrees; *p; p++) {
1850 struct worktree *wt = *p;
1851 struct index_state istate = INDEX_STATE_INIT(revs->repo);
1853 if (wt->is_current)
1854 continue; /* current index already taken care of */
1856 if (read_index_from(&istate,
1857 worktree_git_path(wt, "index"),
1858 get_worktree_git_dir(wt)) > 0)
1859 do_add_index_objects_to_pending(revs, &istate, flags);
1860 discard_index(&istate);
1862 free_worktrees(worktrees);
1865 struct add_alternate_refs_data {
1866 struct rev_info *revs;
1867 unsigned int flags;
1870 static void add_one_alternate_ref(const struct object_id *oid,
1871 void *vdata)
1873 const char *name = ".alternate";
1874 struct add_alternate_refs_data *data = vdata;
1875 struct object *obj;
1877 obj = get_reference(data->revs, name, oid, data->flags);
1878 add_rev_cmdline(data->revs, obj, name, REV_CMD_REV, data->flags);
1879 add_pending_object(data->revs, obj, name);
1882 static void add_alternate_refs_to_pending(struct rev_info *revs,
1883 unsigned int flags)
1885 struct add_alternate_refs_data data;
1886 data.revs = revs;
1887 data.flags = flags;
1888 for_each_alternate_ref(add_one_alternate_ref, &data);
1891 static int add_parents_only(struct rev_info *revs, const char *arg_, int flags,
1892 int exclude_parent)
1894 struct object_id oid;
1895 struct object *it;
1896 struct commit *commit;
1897 struct commit_list *parents;
1898 int parent_number;
1899 const char *arg = arg_;
1901 if (*arg == '^') {
1902 flags ^= UNINTERESTING | BOTTOM;
1903 arg++;
1905 if (repo_get_oid_committish(the_repository, arg, &oid))
1906 return 0;
1907 while (1) {
1908 it = get_reference(revs, arg, &oid, 0);
1909 if (!it && revs->ignore_missing)
1910 return 0;
1911 if (it->type != OBJ_TAG)
1912 break;
1913 if (!((struct tag*)it)->tagged)
1914 return 0;
1915 oidcpy(&oid, &((struct tag*)it)->tagged->oid);
1917 if (it->type != OBJ_COMMIT)
1918 return 0;
1919 commit = (struct commit *)it;
1920 if (exclude_parent &&
1921 exclude_parent > commit_list_count(commit->parents))
1922 return 0;
1923 for (parents = commit->parents, parent_number = 1;
1924 parents;
1925 parents = parents->next, parent_number++) {
1926 if (exclude_parent && parent_number != exclude_parent)
1927 continue;
1929 it = &parents->item->object;
1930 it->flags |= flags;
1931 add_rev_cmdline(revs, it, arg_, REV_CMD_PARENTS_ONLY, flags);
1932 add_pending_object(revs, it, arg);
1934 return 1;
1937 void repo_init_revisions(struct repository *r,
1938 struct rev_info *revs,
1939 const char *prefix)
1941 struct rev_info blank = REV_INFO_INIT;
1942 memcpy(revs, &blank, sizeof(*revs));
1944 revs->repo = r;
1945 revs->pruning.repo = r;
1946 revs->pruning.add_remove = file_add_remove;
1947 revs->pruning.change = file_change;
1948 revs->pruning.change_fn_data = revs;
1949 revs->prefix = prefix;
1951 grep_init(&revs->grep_filter, revs->repo);
1952 revs->grep_filter.status_only = 1;
1954 repo_diff_setup(revs->repo, &revs->diffopt);
1955 if (prefix && !revs->diffopt.prefix) {
1956 revs->diffopt.prefix = prefix;
1957 revs->diffopt.prefix_length = strlen(prefix);
1960 init_display_notes(&revs->notes_opt);
1961 list_objects_filter_init(&revs->filter);
1962 init_ref_exclusions(&revs->ref_excludes);
1963 oidset_init(&revs->missing_commits, 0);
1966 static void add_pending_commit_list(struct rev_info *revs,
1967 struct commit_list *commit_list,
1968 unsigned int flags)
1970 while (commit_list) {
1971 struct object *object = &commit_list->item->object;
1972 object->flags |= flags;
1973 add_pending_object(revs, object, oid_to_hex(&object->oid));
1974 commit_list = commit_list->next;
1978 static const char *lookup_other_head(struct object_id *oid)
1980 int i;
1981 static const char *const other_head[] = {
1982 "MERGE_HEAD", "CHERRY_PICK_HEAD", "REVERT_HEAD", "REBASE_HEAD"
1985 for (i = 0; i < ARRAY_SIZE(other_head); i++)
1986 if (!refs_read_ref_full(get_main_ref_store(the_repository), other_head[i],
1987 RESOLVE_REF_READING | RESOLVE_REF_NO_RECURSE,
1988 oid, NULL)) {
1989 if (is_null_oid(oid))
1990 die(_("%s exists but is a symbolic ref"), other_head[i]);
1991 return other_head[i];
1994 die(_("--merge requires one of the pseudorefs MERGE_HEAD, CHERRY_PICK_HEAD, REVERT_HEAD or REBASE_HEAD"));
1997 static void prepare_show_merge(struct rev_info *revs)
1999 struct commit_list *bases = NULL;
2000 struct commit *head, *other;
2001 struct object_id oid;
2002 const char *other_name;
2003 const char **prune = NULL;
2004 int i, prune_num = 1; /* counting terminating NULL */
2005 struct index_state *istate = revs->repo->index;
2007 if (repo_get_oid(the_repository, "HEAD", &oid))
2008 die("--merge without HEAD?");
2009 head = lookup_commit_or_die(&oid, "HEAD");
2010 other_name = lookup_other_head(&oid);
2011 other = lookup_commit_or_die(&oid, other_name);
2012 add_pending_object(revs, &head->object, "HEAD");
2013 add_pending_object(revs, &other->object, other_name);
2014 if (repo_get_merge_bases(the_repository, head, other, &bases) < 0)
2015 exit(128);
2016 add_rev_cmdline_list(revs, bases, REV_CMD_MERGE_BASE, UNINTERESTING | BOTTOM);
2017 add_pending_commit_list(revs, bases, UNINTERESTING | BOTTOM);
2018 free_commit_list(bases);
2019 head->object.flags |= SYMMETRIC_LEFT;
2021 if (!istate->cache_nr)
2022 repo_read_index(revs->repo);
2023 for (i = 0; i < istate->cache_nr; i++) {
2024 const struct cache_entry *ce = istate->cache[i];
2025 if (!ce_stage(ce))
2026 continue;
2027 if (ce_path_match(istate, ce, &revs->prune_data, NULL)) {
2028 prune_num++;
2029 REALLOC_ARRAY(prune, prune_num);
2030 prune[prune_num-2] = ce->name;
2031 prune[prune_num-1] = NULL;
2033 while ((i+1 < istate->cache_nr) &&
2034 ce_same_name(ce, istate->cache[i+1]))
2035 i++;
2037 clear_pathspec(&revs->prune_data);
2038 parse_pathspec(&revs->prune_data, PATHSPEC_ALL_MAGIC & ~PATHSPEC_LITERAL,
2039 PATHSPEC_PREFER_FULL | PATHSPEC_LITERAL_PATH, "", prune);
2040 revs->limited = 1;
2043 static int dotdot_missing(const char *arg, char *dotdot,
2044 struct rev_info *revs, int symmetric)
2046 if (revs->ignore_missing)
2047 return 0;
2048 /* de-munge so we report the full argument */
2049 *dotdot = '.';
2050 die(symmetric
2051 ? "Invalid symmetric difference expression %s"
2052 : "Invalid revision range %s", arg);
2055 static int handle_dotdot_1(const char *arg, char *dotdot,
2056 struct rev_info *revs, int flags,
2057 int cant_be_filename,
2058 struct object_context *a_oc,
2059 struct object_context *b_oc)
2061 const char *a_name, *b_name;
2062 struct object_id a_oid, b_oid;
2063 struct object *a_obj, *b_obj;
2064 unsigned int a_flags, b_flags;
2065 int symmetric = 0;
2066 unsigned int flags_exclude = flags ^ (UNINTERESTING | BOTTOM);
2067 unsigned int oc_flags = GET_OID_COMMITTISH | GET_OID_RECORD_PATH;
2069 a_name = arg;
2070 if (!*a_name)
2071 a_name = "HEAD";
2073 b_name = dotdot + 2;
2074 if (*b_name == '.') {
2075 symmetric = 1;
2076 b_name++;
2078 if (!*b_name)
2079 b_name = "HEAD";
2081 if (get_oid_with_context(revs->repo, a_name, oc_flags, &a_oid, a_oc) ||
2082 get_oid_with_context(revs->repo, b_name, oc_flags, &b_oid, b_oc))
2083 return -1;
2085 if (!cant_be_filename) {
2086 *dotdot = '.';
2087 verify_non_filename(revs->prefix, arg);
2088 *dotdot = '\0';
2091 a_obj = parse_object(revs->repo, &a_oid);
2092 b_obj = parse_object(revs->repo, &b_oid);
2093 if (!a_obj || !b_obj)
2094 return dotdot_missing(arg, dotdot, revs, symmetric);
2096 if (!symmetric) {
2097 /* just A..B */
2098 b_flags = flags;
2099 a_flags = flags_exclude;
2100 } else {
2101 /* A...B -- find merge bases between the two */
2102 struct commit *a, *b;
2103 struct commit_list *exclude = NULL;
2105 a = lookup_commit_reference(revs->repo, &a_obj->oid);
2106 b = lookup_commit_reference(revs->repo, &b_obj->oid);
2107 if (!a || !b)
2108 return dotdot_missing(arg, dotdot, revs, symmetric);
2110 if (repo_get_merge_bases(the_repository, a, b, &exclude) < 0) {
2111 free_commit_list(exclude);
2112 return -1;
2114 add_rev_cmdline_list(revs, exclude, REV_CMD_MERGE_BASE,
2115 flags_exclude);
2116 add_pending_commit_list(revs, exclude, flags_exclude);
2117 free_commit_list(exclude);
2119 b_flags = flags;
2120 a_flags = flags | SYMMETRIC_LEFT;
2123 a_obj->flags |= a_flags;
2124 b_obj->flags |= b_flags;
2125 add_rev_cmdline(revs, a_obj, a_name, REV_CMD_LEFT, a_flags);
2126 add_rev_cmdline(revs, b_obj, b_name, REV_CMD_RIGHT, b_flags);
2127 add_pending_object_with_path(revs, a_obj, a_name, a_oc->mode, a_oc->path);
2128 add_pending_object_with_path(revs, b_obj, b_name, b_oc->mode, b_oc->path);
2129 return 0;
2132 static int handle_dotdot(const char *arg,
2133 struct rev_info *revs, int flags,
2134 int cant_be_filename)
2136 struct object_context a_oc, b_oc;
2137 char *dotdot = strstr(arg, "..");
2138 int ret;
2140 if (!dotdot)
2141 return -1;
2143 memset(&a_oc, 0, sizeof(a_oc));
2144 memset(&b_oc, 0, sizeof(b_oc));
2146 *dotdot = '\0';
2147 ret = handle_dotdot_1(arg, dotdot, revs, flags, cant_be_filename,
2148 &a_oc, &b_oc);
2149 *dotdot = '.';
2151 free(a_oc.path);
2152 free(b_oc.path);
2154 return ret;
2157 static int handle_revision_arg_1(const char *arg_, struct rev_info *revs, int flags, unsigned revarg_opt)
2159 struct object_context oc;
2160 char *mark;
2161 struct object *object;
2162 struct object_id oid;
2163 int local_flags;
2164 const char *arg = arg_;
2165 int cant_be_filename = revarg_opt & REVARG_CANNOT_BE_FILENAME;
2166 unsigned get_sha1_flags = GET_OID_RECORD_PATH;
2168 flags = flags & UNINTERESTING ? flags | BOTTOM : flags & ~BOTTOM;
2170 if (!cant_be_filename && !strcmp(arg, "..")) {
2172 * Just ".."? That is not a range but the
2173 * pathspec for the parent directory.
2175 return -1;
2178 if (!handle_dotdot(arg, revs, flags, revarg_opt))
2179 return 0;
2181 mark = strstr(arg, "^@");
2182 if (mark && !mark[2]) {
2183 *mark = 0;
2184 if (add_parents_only(revs, arg, flags, 0))
2185 return 0;
2186 *mark = '^';
2188 mark = strstr(arg, "^!");
2189 if (mark && !mark[2]) {
2190 *mark = 0;
2191 if (!add_parents_only(revs, arg, flags ^ (UNINTERESTING | BOTTOM), 0))
2192 *mark = '^';
2194 mark = strstr(arg, "^-");
2195 if (mark) {
2196 int exclude_parent = 1;
2198 if (mark[2]) {
2199 if (strtol_i(mark + 2, 10, &exclude_parent) ||
2200 exclude_parent < 1)
2201 return -1;
2204 *mark = 0;
2205 if (!add_parents_only(revs, arg, flags ^ (UNINTERESTING | BOTTOM), exclude_parent))
2206 *mark = '^';
2209 local_flags = 0;
2210 if (*arg == '^') {
2211 local_flags = UNINTERESTING | BOTTOM;
2212 arg++;
2215 if (revarg_opt & REVARG_COMMITTISH)
2216 get_sha1_flags |= GET_OID_COMMITTISH;
2219 * Even if revs->do_not_die_on_missing_objects is set, we
2220 * should error out if we can't even get an oid, as
2221 * `--missing=print` should be able to report missing oids.
2223 if (get_oid_with_context(revs->repo, arg, get_sha1_flags, &oid, &oc))
2224 return revs->ignore_missing ? 0 : -1;
2225 if (!cant_be_filename)
2226 verify_non_filename(revs->prefix, arg);
2227 object = get_reference(revs, arg, &oid, flags ^ local_flags);
2228 if (!object)
2229 return (revs->ignore_missing || revs->do_not_die_on_missing_objects) ? 0 : -1;
2230 add_rev_cmdline(revs, object, arg_, REV_CMD_REV, flags ^ local_flags);
2231 add_pending_object_with_path(revs, object, arg, oc.mode, oc.path);
2232 free(oc.path);
2233 return 0;
2236 int handle_revision_arg(const char *arg, struct rev_info *revs, int flags, unsigned revarg_opt)
2238 int ret = handle_revision_arg_1(arg, revs, flags, revarg_opt);
2239 if (!ret)
2240 revs->rev_input_given = 1;
2241 return ret;
2244 static void read_pathspec_from_stdin(struct strbuf *sb,
2245 struct strvec *prune)
2247 while (strbuf_getline(sb, stdin) != EOF)
2248 strvec_push(prune, sb->buf);
2251 static void add_grep(struct rev_info *revs, const char *ptn, enum grep_pat_token what)
2253 append_grep_pattern(&revs->grep_filter, ptn, "command line", 0, what);
2256 static void add_header_grep(struct rev_info *revs, enum grep_header_field field, const char *pattern)
2258 append_header_grep_pattern(&revs->grep_filter, field, pattern);
2261 static void add_message_grep(struct rev_info *revs, const char *pattern)
2263 add_grep(revs, pattern, GREP_PATTERN_BODY);
2266 static int parse_count(const char *arg)
2268 int count;
2270 if (strtol_i(arg, 10, &count) < 0)
2271 die("'%s': not an integer", arg);
2272 return count;
2275 static timestamp_t parse_age(const char *arg)
2277 timestamp_t num;
2278 char *p;
2280 errno = 0;
2281 num = parse_timestamp(arg, &p, 10);
2282 if (errno || *p || p == arg)
2283 die("'%s': not a number of seconds since epoch", arg);
2284 return num;
2287 static int handle_revision_opt(struct rev_info *revs, int argc, const char **argv,
2288 int *unkc, const char **unkv,
2289 const struct setup_revision_opt* opt)
2291 const char *arg = argv[0];
2292 const char *optarg = NULL;
2293 int argcount;
2294 const unsigned hexsz = the_hash_algo->hexsz;
2296 /* pseudo revision arguments */
2297 if (!strcmp(arg, "--all") || !strcmp(arg, "--branches") ||
2298 !strcmp(arg, "--tags") || !strcmp(arg, "--remotes") ||
2299 !strcmp(arg, "--reflog") || !strcmp(arg, "--not") ||
2300 !strcmp(arg, "--no-walk") || !strcmp(arg, "--do-walk") ||
2301 !strcmp(arg, "--bisect") || starts_with(arg, "--glob=") ||
2302 !strcmp(arg, "--indexed-objects") ||
2303 !strcmp(arg, "--alternate-refs") ||
2304 starts_with(arg, "--exclude=") || starts_with(arg, "--exclude-hidden=") ||
2305 starts_with(arg, "--branches=") || starts_with(arg, "--tags=") ||
2306 starts_with(arg, "--remotes=") || starts_with(arg, "--no-walk="))
2308 unkv[(*unkc)++] = arg;
2309 return 1;
2312 if ((argcount = parse_long_opt("max-count", argv, &optarg))) {
2313 revs->max_count = parse_count(optarg);
2314 revs->no_walk = 0;
2315 return argcount;
2316 } else if ((argcount = parse_long_opt("skip", argv, &optarg))) {
2317 revs->skip_count = parse_count(optarg);
2318 return argcount;
2319 } else if ((*arg == '-') && isdigit(arg[1])) {
2320 /* accept -<digit>, like traditional "head" */
2321 revs->max_count = parse_count(arg + 1);
2322 revs->no_walk = 0;
2323 } else if (!strcmp(arg, "-n")) {
2324 if (argc <= 1)
2325 return error("-n requires an argument");
2326 revs->max_count = parse_count(argv[1]);
2327 revs->no_walk = 0;
2328 return 2;
2329 } else if (skip_prefix(arg, "-n", &optarg)) {
2330 revs->max_count = parse_count(optarg);
2331 revs->no_walk = 0;
2332 } else if ((argcount = parse_long_opt("max-age", argv, &optarg))) {
2333 revs->max_age = parse_age(optarg);
2334 return argcount;
2335 } else if ((argcount = parse_long_opt("since", argv, &optarg))) {
2336 revs->max_age = approxidate(optarg);
2337 return argcount;
2338 } else if ((argcount = parse_long_opt("since-as-filter", argv, &optarg))) {
2339 revs->max_age_as_filter = approxidate(optarg);
2340 return argcount;
2341 } else if ((argcount = parse_long_opt("after", argv, &optarg))) {
2342 revs->max_age = approxidate(optarg);
2343 return argcount;
2344 } else if ((argcount = parse_long_opt("min-age", argv, &optarg))) {
2345 revs->min_age = parse_age(optarg);
2346 return argcount;
2347 } else if ((argcount = parse_long_opt("before", argv, &optarg))) {
2348 revs->min_age = approxidate(optarg);
2349 return argcount;
2350 } else if ((argcount = parse_long_opt("until", argv, &optarg))) {
2351 revs->min_age = approxidate(optarg);
2352 return argcount;
2353 } else if (!strcmp(arg, "--first-parent")) {
2354 revs->first_parent_only = 1;
2355 } else if (!strcmp(arg, "--exclude-first-parent-only")) {
2356 revs->exclude_first_parent_only = 1;
2357 } else if (!strcmp(arg, "--ancestry-path")) {
2358 revs->ancestry_path = 1;
2359 revs->simplify_history = 0;
2360 revs->limited = 1;
2361 revs->ancestry_path_implicit_bottoms = 1;
2362 } else if (skip_prefix(arg, "--ancestry-path=", &optarg)) {
2363 struct commit *c;
2364 struct object_id oid;
2365 const char *msg = _("could not get commit for --ancestry-path argument %s");
2367 revs->ancestry_path = 1;
2368 revs->simplify_history = 0;
2369 revs->limited = 1;
2371 if (repo_get_oid_committish(revs->repo, optarg, &oid))
2372 return error(msg, optarg);
2373 get_reference(revs, optarg, &oid, ANCESTRY_PATH);
2374 c = lookup_commit_reference(revs->repo, &oid);
2375 if (!c)
2376 return error(msg, optarg);
2377 commit_list_insert(c, &revs->ancestry_path_bottoms);
2378 } else if (!strcmp(arg, "-g") || !strcmp(arg, "--walk-reflogs")) {
2379 init_reflog_walk(&revs->reflog_info);
2380 } else if (!strcmp(arg, "--default")) {
2381 if (argc <= 1)
2382 return error("bad --default argument");
2383 revs->def = argv[1];
2384 return 2;
2385 } else if (!strcmp(arg, "--merge")) {
2386 revs->show_merge = 1;
2387 } else if (!strcmp(arg, "--topo-order")) {
2388 revs->sort_order = REV_SORT_IN_GRAPH_ORDER;
2389 revs->topo_order = 1;
2390 } else if (!strcmp(arg, "--simplify-merges")) {
2391 revs->simplify_merges = 1;
2392 revs->topo_order = 1;
2393 revs->rewrite_parents = 1;
2394 revs->simplify_history = 0;
2395 revs->limited = 1;
2396 } else if (!strcmp(arg, "--simplify-by-decoration")) {
2397 revs->simplify_merges = 1;
2398 revs->topo_order = 1;
2399 revs->rewrite_parents = 1;
2400 revs->simplify_history = 0;
2401 revs->simplify_by_decoration = 1;
2402 revs->limited = 1;
2403 revs->prune = 1;
2404 } else if (!strcmp(arg, "--date-order")) {
2405 revs->sort_order = REV_SORT_BY_COMMIT_DATE;
2406 revs->topo_order = 1;
2407 } else if (!strcmp(arg, "--author-date-order")) {
2408 revs->sort_order = REV_SORT_BY_AUTHOR_DATE;
2409 revs->topo_order = 1;
2410 } else if (!strcmp(arg, "--early-output")) {
2411 revs->early_output = 100;
2412 revs->topo_order = 1;
2413 } else if (skip_prefix(arg, "--early-output=", &optarg)) {
2414 if (strtoul_ui(optarg, 10, &revs->early_output) < 0)
2415 die("'%s': not a non-negative integer", optarg);
2416 revs->topo_order = 1;
2417 } else if (!strcmp(arg, "--parents")) {
2418 revs->rewrite_parents = 1;
2419 revs->print_parents = 1;
2420 } else if (!strcmp(arg, "--dense")) {
2421 revs->dense = 1;
2422 } else if (!strcmp(arg, "--sparse")) {
2423 revs->dense = 0;
2424 } else if (!strcmp(arg, "--in-commit-order")) {
2425 revs->tree_blobs_in_commit_order = 1;
2426 } else if (!strcmp(arg, "--remove-empty")) {
2427 revs->remove_empty_trees = 1;
2428 } else if (!strcmp(arg, "--merges")) {
2429 revs->min_parents = 2;
2430 } else if (!strcmp(arg, "--no-merges")) {
2431 revs->max_parents = 1;
2432 } else if (skip_prefix(arg, "--min-parents=", &optarg)) {
2433 revs->min_parents = parse_count(optarg);
2434 } else if (!strcmp(arg, "--no-min-parents")) {
2435 revs->min_parents = 0;
2436 } else if (skip_prefix(arg, "--max-parents=", &optarg)) {
2437 revs->max_parents = parse_count(optarg);
2438 } else if (!strcmp(arg, "--no-max-parents")) {
2439 revs->max_parents = -1;
2440 } else if (!strcmp(arg, "--boundary")) {
2441 revs->boundary = 1;
2442 } else if (!strcmp(arg, "--left-right")) {
2443 revs->left_right = 1;
2444 } else if (!strcmp(arg, "--left-only")) {
2445 if (revs->right_only)
2446 die(_("options '%s' and '%s' cannot be used together"),
2447 "--left-only", "--right-only/--cherry");
2448 revs->left_only = 1;
2449 } else if (!strcmp(arg, "--right-only")) {
2450 if (revs->left_only)
2451 die(_("options '%s' and '%s' cannot be used together"), "--right-only", "--left-only");
2452 revs->right_only = 1;
2453 } else if (!strcmp(arg, "--cherry")) {
2454 if (revs->left_only)
2455 die(_("options '%s' and '%s' cannot be used together"), "--cherry", "--left-only");
2456 revs->cherry_mark = 1;
2457 revs->right_only = 1;
2458 revs->max_parents = 1;
2459 revs->limited = 1;
2460 } else if (!strcmp(arg, "--count")) {
2461 revs->count = 1;
2462 } else if (!strcmp(arg, "--cherry-mark")) {
2463 if (revs->cherry_pick)
2464 die(_("options '%s' and '%s' cannot be used together"), "--cherry-mark", "--cherry-pick");
2465 revs->cherry_mark = 1;
2466 revs->limited = 1; /* needs limit_list() */
2467 } else if (!strcmp(arg, "--cherry-pick")) {
2468 if (revs->cherry_mark)
2469 die(_("options '%s' and '%s' cannot be used together"), "--cherry-pick", "--cherry-mark");
2470 revs->cherry_pick = 1;
2471 revs->limited = 1;
2472 } else if (!strcmp(arg, "--objects")) {
2473 revs->tag_objects = 1;
2474 revs->tree_objects = 1;
2475 revs->blob_objects = 1;
2476 } else if (!strcmp(arg, "--objects-edge")) {
2477 revs->tag_objects = 1;
2478 revs->tree_objects = 1;
2479 revs->blob_objects = 1;
2480 revs->edge_hint = 1;
2481 } else if (!strcmp(arg, "--objects-edge-aggressive")) {
2482 revs->tag_objects = 1;
2483 revs->tree_objects = 1;
2484 revs->blob_objects = 1;
2485 revs->edge_hint = 1;
2486 revs->edge_hint_aggressive = 1;
2487 } else if (!strcmp(arg, "--verify-objects")) {
2488 revs->tag_objects = 1;
2489 revs->tree_objects = 1;
2490 revs->blob_objects = 1;
2491 revs->verify_objects = 1;
2492 disable_commit_graph(revs->repo);
2493 } else if (!strcmp(arg, "--unpacked")) {
2494 revs->unpacked = 1;
2495 } else if (starts_with(arg, "--unpacked=")) {
2496 die(_("--unpacked=<packfile> no longer supported"));
2497 } else if (!strcmp(arg, "--no-kept-objects")) {
2498 revs->no_kept_objects = 1;
2499 revs->keep_pack_cache_flags |= IN_CORE_KEEP_PACKS;
2500 revs->keep_pack_cache_flags |= ON_DISK_KEEP_PACKS;
2501 } else if (skip_prefix(arg, "--no-kept-objects=", &optarg)) {
2502 revs->no_kept_objects = 1;
2503 if (!strcmp(optarg, "in-core"))
2504 revs->keep_pack_cache_flags |= IN_CORE_KEEP_PACKS;
2505 if (!strcmp(optarg, "on-disk"))
2506 revs->keep_pack_cache_flags |= ON_DISK_KEEP_PACKS;
2507 } else if (!strcmp(arg, "-r")) {
2508 revs->diff = 1;
2509 revs->diffopt.flags.recursive = 1;
2510 } else if (!strcmp(arg, "-t")) {
2511 revs->diff = 1;
2512 revs->diffopt.flags.recursive = 1;
2513 revs->diffopt.flags.tree_in_recursive = 1;
2514 } else if ((argcount = diff_merges_parse_opts(revs, argv))) {
2515 return argcount;
2516 } else if (!strcmp(arg, "-v")) {
2517 revs->verbose_header = 1;
2518 } else if (!strcmp(arg, "--pretty")) {
2519 revs->verbose_header = 1;
2520 revs->pretty_given = 1;
2521 get_commit_format(NULL, revs);
2522 } else if (skip_prefix(arg, "--pretty=", &optarg) ||
2523 skip_prefix(arg, "--format=", &optarg)) {
2525 * Detached form ("--pretty X" as opposed to "--pretty=X")
2526 * not allowed, since the argument is optional.
2528 revs->verbose_header = 1;
2529 revs->pretty_given = 1;
2530 get_commit_format(optarg, revs);
2531 } else if (!strcmp(arg, "--expand-tabs")) {
2532 revs->expand_tabs_in_log = 8;
2533 } else if (!strcmp(arg, "--no-expand-tabs")) {
2534 revs->expand_tabs_in_log = 0;
2535 } else if (skip_prefix(arg, "--expand-tabs=", &arg)) {
2536 int val;
2537 if (strtol_i(arg, 10, &val) < 0 || val < 0)
2538 die("'%s': not a non-negative integer", arg);
2539 revs->expand_tabs_in_log = val;
2540 } else if (!strcmp(arg, "--show-notes") || !strcmp(arg, "--notes")) {
2541 enable_default_display_notes(&revs->notes_opt, &revs->show_notes);
2542 revs->show_notes_given = 1;
2543 } else if (!strcmp(arg, "--show-signature")) {
2544 revs->show_signature = 1;
2545 } else if (!strcmp(arg, "--no-show-signature")) {
2546 revs->show_signature = 0;
2547 } else if (!strcmp(arg, "--show-linear-break")) {
2548 revs->break_bar = " ..........";
2549 revs->track_linear = 1;
2550 revs->track_first_time = 1;
2551 } else if (skip_prefix(arg, "--show-linear-break=", &optarg)) {
2552 revs->break_bar = xstrdup(optarg);
2553 revs->track_linear = 1;
2554 revs->track_first_time = 1;
2555 } else if (!strcmp(arg, "--show-notes-by-default")) {
2556 revs->show_notes_by_default = 1;
2557 } else if (skip_prefix(arg, "--show-notes=", &optarg) ||
2558 skip_prefix(arg, "--notes=", &optarg)) {
2559 if (starts_with(arg, "--show-notes=") &&
2560 revs->notes_opt.use_default_notes < 0)
2561 revs->notes_opt.use_default_notes = 1;
2562 enable_ref_display_notes(&revs->notes_opt, &revs->show_notes, optarg);
2563 revs->show_notes_given = 1;
2564 } else if (!strcmp(arg, "--no-notes")) {
2565 disable_display_notes(&revs->notes_opt, &revs->show_notes);
2566 revs->show_notes_given = 1;
2567 } else if (!strcmp(arg, "--standard-notes")) {
2568 revs->show_notes_given = 1;
2569 revs->notes_opt.use_default_notes = 1;
2570 } else if (!strcmp(arg, "--no-standard-notes")) {
2571 revs->notes_opt.use_default_notes = 0;
2572 } else if (!strcmp(arg, "--oneline")) {
2573 revs->verbose_header = 1;
2574 get_commit_format("oneline", revs);
2575 revs->pretty_given = 1;
2576 revs->abbrev_commit = 1;
2577 } else if (!strcmp(arg, "--graph")) {
2578 graph_clear(revs->graph);
2579 revs->graph = graph_init(revs);
2580 } else if (!strcmp(arg, "--no-graph")) {
2581 graph_clear(revs->graph);
2582 revs->graph = NULL;
2583 } else if (!strcmp(arg, "--encode-email-headers")) {
2584 revs->encode_email_headers = 1;
2585 } else if (!strcmp(arg, "--no-encode-email-headers")) {
2586 revs->encode_email_headers = 0;
2587 } else if (!strcmp(arg, "--root")) {
2588 revs->show_root_diff = 1;
2589 } else if (!strcmp(arg, "--no-commit-id")) {
2590 revs->no_commit_id = 1;
2591 } else if (!strcmp(arg, "--always")) {
2592 revs->always_show_header = 1;
2593 } else if (!strcmp(arg, "--no-abbrev")) {
2594 revs->abbrev = 0;
2595 } else if (!strcmp(arg, "--abbrev")) {
2596 revs->abbrev = DEFAULT_ABBREV;
2597 } else if (skip_prefix(arg, "--abbrev=", &optarg)) {
2598 revs->abbrev = strtoul(optarg, NULL, 10);
2599 if (revs->abbrev < MINIMUM_ABBREV)
2600 revs->abbrev = MINIMUM_ABBREV;
2601 else if (revs->abbrev > hexsz)
2602 revs->abbrev = hexsz;
2603 } else if (!strcmp(arg, "--abbrev-commit")) {
2604 revs->abbrev_commit = 1;
2605 revs->abbrev_commit_given = 1;
2606 } else if (!strcmp(arg, "--no-abbrev-commit")) {
2607 revs->abbrev_commit = 0;
2608 } else if (!strcmp(arg, "--full-diff")) {
2609 revs->diff = 1;
2610 revs->full_diff = 1;
2611 } else if (!strcmp(arg, "--show-pulls")) {
2612 revs->show_pulls = 1;
2613 } else if (!strcmp(arg, "--full-history")) {
2614 revs->simplify_history = 0;
2615 } else if (!strcmp(arg, "--relative-date")) {
2616 revs->date_mode.type = DATE_RELATIVE;
2617 revs->date_mode_explicit = 1;
2618 } else if ((argcount = parse_long_opt("date", argv, &optarg))) {
2619 parse_date_format(optarg, &revs->date_mode);
2620 revs->date_mode_explicit = 1;
2621 return argcount;
2622 } else if (!strcmp(arg, "--log-size")) {
2623 revs->show_log_size = 1;
2626 * Grepping the commit log
2628 else if ((argcount = parse_long_opt("author", argv, &optarg))) {
2629 add_header_grep(revs, GREP_HEADER_AUTHOR, optarg);
2630 return argcount;
2631 } else if ((argcount = parse_long_opt("committer", argv, &optarg))) {
2632 add_header_grep(revs, GREP_HEADER_COMMITTER, optarg);
2633 return argcount;
2634 } else if ((argcount = parse_long_opt("grep-reflog", argv, &optarg))) {
2635 add_header_grep(revs, GREP_HEADER_REFLOG, optarg);
2636 return argcount;
2637 } else if ((argcount = parse_long_opt("grep", argv, &optarg))) {
2638 add_message_grep(revs, optarg);
2639 return argcount;
2640 } else if (!strcmp(arg, "--basic-regexp")) {
2641 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_BRE;
2642 } else if (!strcmp(arg, "--extended-regexp") || !strcmp(arg, "-E")) {
2643 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_ERE;
2644 } else if (!strcmp(arg, "--regexp-ignore-case") || !strcmp(arg, "-i")) {
2645 revs->grep_filter.ignore_case = 1;
2646 revs->diffopt.pickaxe_opts |= DIFF_PICKAXE_IGNORE_CASE;
2647 } else if (!strcmp(arg, "--fixed-strings") || !strcmp(arg, "-F")) {
2648 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_FIXED;
2649 } else if (!strcmp(arg, "--perl-regexp") || !strcmp(arg, "-P")) {
2650 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_PCRE;
2651 } else if (!strcmp(arg, "--all-match")) {
2652 revs->grep_filter.all_match = 1;
2653 } else if (!strcmp(arg, "--invert-grep")) {
2654 revs->grep_filter.no_body_match = 1;
2655 } else if ((argcount = parse_long_opt("encoding", argv, &optarg))) {
2656 free(git_log_output_encoding);
2657 if (strcmp(optarg, "none"))
2658 git_log_output_encoding = xstrdup(optarg);
2659 else
2660 git_log_output_encoding = xstrdup("");
2661 return argcount;
2662 } else if (!strcmp(arg, "--reverse")) {
2663 revs->reverse ^= 1;
2664 } else if (!strcmp(arg, "--children")) {
2665 revs->children.name = "children";
2666 revs->limited = 1;
2667 } else if (!strcmp(arg, "--ignore-missing")) {
2668 revs->ignore_missing = 1;
2669 } else if (opt && opt->allow_exclude_promisor_objects &&
2670 !strcmp(arg, "--exclude-promisor-objects")) {
2671 if (fetch_if_missing)
2672 BUG("exclude_promisor_objects can only be used when fetch_if_missing is 0");
2673 revs->exclude_promisor_objects = 1;
2674 } else {
2675 int opts = diff_opt_parse(&revs->diffopt, argv, argc, revs->prefix);
2676 if (!opts)
2677 unkv[(*unkc)++] = arg;
2678 return opts;
2681 return 1;
2684 void parse_revision_opt(struct rev_info *revs, struct parse_opt_ctx_t *ctx,
2685 const struct option *options,
2686 const char * const usagestr[])
2688 int n = handle_revision_opt(revs, ctx->argc, ctx->argv,
2689 &ctx->cpidx, ctx->out, NULL);
2690 if (n <= 0) {
2691 error("unknown option `%s'", ctx->argv[0]);
2692 usage_with_options(usagestr, options);
2694 ctx->argv += n;
2695 ctx->argc -= n;
2698 void revision_opts_finish(struct rev_info *revs)
2700 if (revs->graph && revs->track_linear)
2701 die(_("options '%s' and '%s' cannot be used together"), "--show-linear-break", "--graph");
2703 if (revs->graph) {
2704 revs->topo_order = 1;
2705 revs->rewrite_parents = 1;
2709 static int for_each_bisect_ref(struct ref_store *refs, each_ref_fn fn,
2710 void *cb_data, const char *term)
2712 struct strbuf bisect_refs = STRBUF_INIT;
2713 int status;
2714 strbuf_addf(&bisect_refs, "refs/bisect/%s", term);
2715 status = refs_for_each_fullref_in(refs, bisect_refs.buf, NULL, fn, cb_data);
2716 strbuf_release(&bisect_refs);
2717 return status;
2720 static int for_each_bad_bisect_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
2722 return for_each_bisect_ref(refs, fn, cb_data, term_bad);
2725 static int for_each_good_bisect_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
2727 return for_each_bisect_ref(refs, fn, cb_data, term_good);
2730 static int handle_revision_pseudo_opt(struct rev_info *revs,
2731 const char **argv, int *flags)
2733 const char *arg = argv[0];
2734 const char *optarg;
2735 struct ref_store *refs;
2736 int argcount;
2738 if (revs->repo != the_repository) {
2740 * We need some something like get_submodule_worktrees()
2741 * before we can go through all worktrees of a submodule,
2742 * .e.g with adding all HEADs from --all, which is not
2743 * supported right now, so stick to single worktree.
2745 if (!revs->single_worktree)
2746 BUG("--single-worktree cannot be used together with submodule");
2748 refs = get_main_ref_store(revs->repo);
2751 * NOTE!
2753 * Commands like "git shortlog" will not accept the options below
2754 * unless parse_revision_opt queues them (as opposed to erroring
2755 * out).
2757 * When implementing your new pseudo-option, remember to
2758 * register it in the list at the top of handle_revision_opt.
2760 if (!strcmp(arg, "--all")) {
2761 handle_refs(refs, revs, *flags, refs_for_each_ref);
2762 handle_refs(refs, revs, *flags, refs_head_ref);
2763 if (!revs->single_worktree) {
2764 struct all_refs_cb cb;
2766 init_all_refs_cb(&cb, revs, *flags);
2767 other_head_refs(handle_one_ref, &cb);
2769 clear_ref_exclusions(&revs->ref_excludes);
2770 } else if (!strcmp(arg, "--branches")) {
2771 if (revs->ref_excludes.hidden_refs_configured)
2772 return error(_("options '%s' and '%s' cannot be used together"),
2773 "--exclude-hidden", "--branches");
2774 handle_refs(refs, revs, *flags, refs_for_each_branch_ref);
2775 clear_ref_exclusions(&revs->ref_excludes);
2776 } else if (!strcmp(arg, "--bisect")) {
2777 read_bisect_terms(&term_bad, &term_good);
2778 handle_refs(refs, revs, *flags, for_each_bad_bisect_ref);
2779 handle_refs(refs, revs, *flags ^ (UNINTERESTING | BOTTOM),
2780 for_each_good_bisect_ref);
2781 revs->bisect = 1;
2782 } else if (!strcmp(arg, "--tags")) {
2783 if (revs->ref_excludes.hidden_refs_configured)
2784 return error(_("options '%s' and '%s' cannot be used together"),
2785 "--exclude-hidden", "--tags");
2786 handle_refs(refs, revs, *flags, refs_for_each_tag_ref);
2787 clear_ref_exclusions(&revs->ref_excludes);
2788 } else if (!strcmp(arg, "--remotes")) {
2789 if (revs->ref_excludes.hidden_refs_configured)
2790 return error(_("options '%s' and '%s' cannot be used together"),
2791 "--exclude-hidden", "--remotes");
2792 handle_refs(refs, revs, *flags, refs_for_each_remote_ref);
2793 clear_ref_exclusions(&revs->ref_excludes);
2794 } else if ((argcount = parse_long_opt("glob", argv, &optarg))) {
2795 struct all_refs_cb cb;
2796 init_all_refs_cb(&cb, revs, *flags);
2797 refs_for_each_glob_ref(get_main_ref_store(the_repository),
2798 handle_one_ref, optarg, &cb);
2799 clear_ref_exclusions(&revs->ref_excludes);
2800 return argcount;
2801 } else if ((argcount = parse_long_opt("exclude", argv, &optarg))) {
2802 add_ref_exclusion(&revs->ref_excludes, optarg);
2803 return argcount;
2804 } else if ((argcount = parse_long_opt("exclude-hidden", argv, &optarg))) {
2805 exclude_hidden_refs(&revs->ref_excludes, optarg);
2806 return argcount;
2807 } else if (skip_prefix(arg, "--branches=", &optarg)) {
2808 struct all_refs_cb cb;
2809 if (revs->ref_excludes.hidden_refs_configured)
2810 return error(_("options '%s' and '%s' cannot be used together"),
2811 "--exclude-hidden", "--branches");
2812 init_all_refs_cb(&cb, revs, *flags);
2813 refs_for_each_glob_ref_in(get_main_ref_store(the_repository),
2814 handle_one_ref, optarg,
2815 "refs/heads/", &cb);
2816 clear_ref_exclusions(&revs->ref_excludes);
2817 } else if (skip_prefix(arg, "--tags=", &optarg)) {
2818 struct all_refs_cb cb;
2819 if (revs->ref_excludes.hidden_refs_configured)
2820 return error(_("options '%s' and '%s' cannot be used together"),
2821 "--exclude-hidden", "--tags");
2822 init_all_refs_cb(&cb, revs, *flags);
2823 refs_for_each_glob_ref_in(get_main_ref_store(the_repository),
2824 handle_one_ref, optarg,
2825 "refs/tags/", &cb);
2826 clear_ref_exclusions(&revs->ref_excludes);
2827 } else if (skip_prefix(arg, "--remotes=", &optarg)) {
2828 struct all_refs_cb cb;
2829 if (revs->ref_excludes.hidden_refs_configured)
2830 return error(_("options '%s' and '%s' cannot be used together"),
2831 "--exclude-hidden", "--remotes");
2832 init_all_refs_cb(&cb, revs, *flags);
2833 refs_for_each_glob_ref_in(get_main_ref_store(the_repository),
2834 handle_one_ref, optarg,
2835 "refs/remotes/", &cb);
2836 clear_ref_exclusions(&revs->ref_excludes);
2837 } else if (!strcmp(arg, "--reflog")) {
2838 add_reflogs_to_pending(revs, *flags);
2839 } else if (!strcmp(arg, "--indexed-objects")) {
2840 add_index_objects_to_pending(revs, *flags);
2841 } else if (!strcmp(arg, "--alternate-refs")) {
2842 add_alternate_refs_to_pending(revs, *flags);
2843 } else if (!strcmp(arg, "--not")) {
2844 *flags ^= UNINTERESTING | BOTTOM;
2845 } else if (!strcmp(arg, "--no-walk")) {
2846 revs->no_walk = 1;
2847 } else if (skip_prefix(arg, "--no-walk=", &optarg)) {
2849 * Detached form ("--no-walk X" as opposed to "--no-walk=X")
2850 * not allowed, since the argument is optional.
2852 revs->no_walk = 1;
2853 if (!strcmp(optarg, "sorted"))
2854 revs->unsorted_input = 0;
2855 else if (!strcmp(optarg, "unsorted"))
2856 revs->unsorted_input = 1;
2857 else
2858 return error("invalid argument to --no-walk");
2859 } else if (!strcmp(arg, "--do-walk")) {
2860 revs->no_walk = 0;
2861 } else if (!strcmp(arg, "--single-worktree")) {
2862 revs->single_worktree = 1;
2863 } else if (skip_prefix(arg, ("--filter="), &arg)) {
2864 parse_list_objects_filter(&revs->filter, arg);
2865 } else if (!strcmp(arg, ("--no-filter"))) {
2866 list_objects_filter_set_no_filter(&revs->filter);
2867 } else {
2868 return 0;
2871 return 1;
2874 static void read_revisions_from_stdin(struct rev_info *revs,
2875 struct strvec *prune)
2877 struct strbuf sb;
2878 int seen_dashdash = 0;
2879 int seen_end_of_options = 0;
2880 int save_warning;
2881 int flags = 0;
2883 save_warning = warn_on_object_refname_ambiguity;
2884 warn_on_object_refname_ambiguity = 0;
2886 strbuf_init(&sb, 1000);
2887 while (strbuf_getline(&sb, stdin) != EOF) {
2888 if (!sb.len)
2889 break;
2891 if (!strcmp(sb.buf, "--")) {
2892 seen_dashdash = 1;
2893 break;
2896 if (!seen_end_of_options && sb.buf[0] == '-') {
2897 const char *argv[] = { sb.buf, NULL };
2899 if (!strcmp(sb.buf, "--end-of-options")) {
2900 seen_end_of_options = 1;
2901 continue;
2904 if (handle_revision_pseudo_opt(revs, argv, &flags) > 0)
2905 continue;
2907 die(_("invalid option '%s' in --stdin mode"), sb.buf);
2910 if (handle_revision_arg(sb.buf, revs, flags,
2911 REVARG_CANNOT_BE_FILENAME))
2912 die("bad revision '%s'", sb.buf);
2914 if (seen_dashdash)
2915 read_pathspec_from_stdin(&sb, prune);
2917 strbuf_release(&sb);
2918 warn_on_object_refname_ambiguity = save_warning;
2921 static void NORETURN diagnose_missing_default(const char *def)
2923 int flags;
2924 const char *refname;
2926 refname = refs_resolve_ref_unsafe(get_main_ref_store(the_repository),
2927 def, 0, NULL, &flags);
2928 if (!refname || !(flags & REF_ISSYMREF) || (flags & REF_ISBROKEN))
2929 die(_("your current branch appears to be broken"));
2931 skip_prefix(refname, "refs/heads/", &refname);
2932 die(_("your current branch '%s' does not have any commits yet"),
2933 refname);
2937 * Parse revision information, filling in the "rev_info" structure,
2938 * and removing the used arguments from the argument list.
2940 * Returns the number of arguments left that weren't recognized
2941 * (which are also moved to the head of the argument list)
2943 int setup_revisions(int argc, const char **argv, struct rev_info *revs, struct setup_revision_opt *opt)
2945 int i, flags, left, seen_dashdash, revarg_opt;
2946 struct strvec prune_data = STRVEC_INIT;
2947 int seen_end_of_options = 0;
2949 /* First, search for "--" */
2950 if (opt && opt->assume_dashdash) {
2951 seen_dashdash = 1;
2952 } else {
2953 seen_dashdash = 0;
2954 for (i = 1; i < argc; i++) {
2955 const char *arg = argv[i];
2956 if (strcmp(arg, "--"))
2957 continue;
2958 if (opt && opt->free_removed_argv_elements)
2959 free((char *)argv[i]);
2960 argv[i] = NULL;
2961 argc = i;
2962 if (argv[i + 1])
2963 strvec_pushv(&prune_data, argv + i + 1);
2964 seen_dashdash = 1;
2965 break;
2969 /* Second, deal with arguments and options */
2970 flags = 0;
2971 revarg_opt = opt ? opt->revarg_opt : 0;
2972 if (seen_dashdash)
2973 revarg_opt |= REVARG_CANNOT_BE_FILENAME;
2974 for (left = i = 1; i < argc; i++) {
2975 const char *arg = argv[i];
2976 if (!seen_end_of_options && *arg == '-') {
2977 int opts;
2979 opts = handle_revision_pseudo_opt(
2980 revs, argv + i,
2981 &flags);
2982 if (opts > 0) {
2983 i += opts - 1;
2984 continue;
2987 if (!strcmp(arg, "--stdin")) {
2988 if (revs->disable_stdin) {
2989 argv[left++] = arg;
2990 continue;
2992 if (revs->read_from_stdin++)
2993 die("--stdin given twice?");
2994 read_revisions_from_stdin(revs, &prune_data);
2995 continue;
2998 if (!strcmp(arg, "--end-of-options")) {
2999 seen_end_of_options = 1;
3000 continue;
3003 opts = handle_revision_opt(revs, argc - i, argv + i,
3004 &left, argv, opt);
3005 if (opts > 0) {
3006 i += opts - 1;
3007 continue;
3009 if (opts < 0)
3010 exit(128);
3011 continue;
3015 if (handle_revision_arg(arg, revs, flags, revarg_opt)) {
3016 int j;
3017 if (seen_dashdash || *arg == '^')
3018 die("bad revision '%s'", arg);
3020 /* If we didn't have a "--":
3021 * (1) all filenames must exist;
3022 * (2) all rev-args must not be interpretable
3023 * as a valid filename.
3024 * but the latter we have checked in the main loop.
3026 for (j = i; j < argc; j++)
3027 verify_filename(revs->prefix, argv[j], j == i);
3029 strvec_pushv(&prune_data, argv + i);
3030 break;
3033 revision_opts_finish(revs);
3035 if (prune_data.nr) {
3037 * If we need to introduce the magic "a lone ':' means no
3038 * pathspec whatsoever", here is the place to do so.
3040 * if (prune_data.nr == 1 && !strcmp(prune_data[0], ":")) {
3041 * prune_data.nr = 0;
3042 * prune_data.alloc = 0;
3043 * free(prune_data.path);
3044 * prune_data.path = NULL;
3045 * } else {
3046 * terminate prune_data.alloc with NULL and
3047 * call init_pathspec() to set revs->prune_data here.
3050 parse_pathspec(&revs->prune_data, 0, 0,
3051 revs->prefix, prune_data.v);
3053 strvec_clear(&prune_data);
3055 if (!revs->def)
3056 revs->def = opt ? opt->def : NULL;
3057 if (opt && opt->tweak)
3058 opt->tweak(revs);
3059 if (revs->show_merge)
3060 prepare_show_merge(revs);
3061 if (revs->def && !revs->pending.nr && !revs->rev_input_given) {
3062 struct object_id oid;
3063 struct object *object;
3064 struct object_context oc;
3065 if (get_oid_with_context(revs->repo, revs->def, 0, &oid, &oc))
3066 diagnose_missing_default(revs->def);
3067 object = get_reference(revs, revs->def, &oid, 0);
3068 add_pending_object_with_mode(revs, object, revs->def, oc.mode);
3071 /* Did the user ask for any diff output? Run the diff! */
3072 if (revs->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT)
3073 revs->diff = 1;
3075 /* Pickaxe, diff-filter and rename following need diffs */
3076 if ((revs->diffopt.pickaxe_opts & DIFF_PICKAXE_KINDS_MASK) ||
3077 revs->diffopt.filter ||
3078 revs->diffopt.flags.follow_renames)
3079 revs->diff = 1;
3081 if (revs->diffopt.objfind)
3082 revs->simplify_history = 0;
3084 if (revs->line_level_traverse) {
3085 if (want_ancestry(revs))
3086 revs->limited = 1;
3087 revs->topo_order = 1;
3090 if (revs->topo_order && !generation_numbers_enabled(the_repository))
3091 revs->limited = 1;
3093 if (revs->prune_data.nr) {
3094 copy_pathspec(&revs->pruning.pathspec, &revs->prune_data);
3095 /* Can't prune commits with rename following: the paths change.. */
3096 if (!revs->diffopt.flags.follow_renames)
3097 revs->prune = 1;
3098 if (!revs->full_diff)
3099 copy_pathspec(&revs->diffopt.pathspec,
3100 &revs->prune_data);
3103 diff_merges_setup_revs(revs);
3105 revs->diffopt.abbrev = revs->abbrev;
3107 diff_setup_done(&revs->diffopt);
3109 if (!is_encoding_utf8(get_log_output_encoding()))
3110 revs->grep_filter.ignore_locale = 1;
3111 compile_grep_patterns(&revs->grep_filter);
3113 if (revs->reflog_info && revs->limited)
3114 die("cannot combine --walk-reflogs with history-limiting options");
3115 if (revs->rewrite_parents && revs->children.name)
3116 die(_("options '%s' and '%s' cannot be used together"), "--parents", "--children");
3117 if (revs->filter.choice && !revs->blob_objects)
3118 die(_("object filtering requires --objects"));
3121 * Limitations on the graph functionality
3123 die_for_incompatible_opt3(!!revs->graph, "--graph",
3124 !!revs->reverse, "--reverse",
3125 !!revs->reflog_info, "--walk-reflogs");
3127 if (revs->no_walk && revs->graph)
3128 die(_("options '%s' and '%s' cannot be used together"), "--no-walk", "--graph");
3129 if (!revs->reflog_info && revs->grep_filter.use_reflog_filter)
3130 die(_("the option '%s' requires '%s'"), "--grep-reflog", "--walk-reflogs");
3132 if (revs->line_level_traverse &&
3133 (revs->diffopt.output_format & ~(DIFF_FORMAT_PATCH | DIFF_FORMAT_NO_OUTPUT)))
3134 die(_("-L does not yet support diff formats besides -p and -s"));
3136 if (revs->expand_tabs_in_log < 0)
3137 revs->expand_tabs_in_log = revs->expand_tabs_in_log_default;
3139 if (!revs->show_notes_given && revs->show_notes_by_default) {
3140 enable_default_display_notes(&revs->notes_opt, &revs->show_notes);
3141 revs->show_notes_given = 1;
3144 return left;
3147 static void release_revisions_cmdline(struct rev_cmdline_info *cmdline)
3149 unsigned int i;
3151 for (i = 0; i < cmdline->nr; i++)
3152 free((char *)cmdline->rev[i].name);
3153 free(cmdline->rev);
3156 static void release_revisions_mailmap(struct string_list *mailmap)
3158 if (!mailmap)
3159 return;
3160 clear_mailmap(mailmap);
3161 free(mailmap);
3164 static void release_revisions_topo_walk_info(struct topo_walk_info *info);
3166 static void free_void_commit_list(void *list)
3168 free_commit_list(list);
3171 void release_revisions(struct rev_info *revs)
3173 free_commit_list(revs->commits);
3174 free_commit_list(revs->ancestry_path_bottoms);
3175 object_array_clear(&revs->pending);
3176 object_array_clear(&revs->boundary_commits);
3177 release_revisions_cmdline(&revs->cmdline);
3178 list_objects_filter_release(&revs->filter);
3179 clear_pathspec(&revs->prune_data);
3180 date_mode_release(&revs->date_mode);
3181 release_revisions_mailmap(revs->mailmap);
3182 free_grep_patterns(&revs->grep_filter);
3183 graph_clear(revs->graph);
3184 /* TODO (need to handle "no_free"): diff_free(&revs->diffopt) */
3185 diff_free(&revs->pruning);
3186 reflog_walk_info_release(revs->reflog_info);
3187 release_revisions_topo_walk_info(revs->topo_walk_info);
3188 clear_decoration(&revs->children, free_void_commit_list);
3189 clear_decoration(&revs->merge_simplification, free);
3190 clear_decoration(&revs->treesame, free);
3191 line_log_free(revs);
3192 oidset_clear(&revs->missing_commits);
3195 static void add_child(struct rev_info *revs, struct commit *parent, struct commit *child)
3197 struct commit_list *l = xcalloc(1, sizeof(*l));
3199 l->item = child;
3200 l->next = add_decoration(&revs->children, &parent->object, l);
3203 static int remove_duplicate_parents(struct rev_info *revs, struct commit *commit)
3205 struct treesame_state *ts = lookup_decoration(&revs->treesame, &commit->object);
3206 struct commit_list **pp, *p;
3207 int surviving_parents;
3209 /* Examine existing parents while marking ones we have seen... */
3210 pp = &commit->parents;
3211 surviving_parents = 0;
3212 while ((p = *pp) != NULL) {
3213 struct commit *parent = p->item;
3214 if (parent->object.flags & TMP_MARK) {
3215 *pp = p->next;
3216 if (ts)
3217 compact_treesame(revs, commit, surviving_parents);
3218 continue;
3220 parent->object.flags |= TMP_MARK;
3221 surviving_parents++;
3222 pp = &p->next;
3224 /* clear the temporary mark */
3225 for (p = commit->parents; p; p = p->next) {
3226 p->item->object.flags &= ~TMP_MARK;
3228 /* no update_treesame() - removing duplicates can't affect TREESAME */
3229 return surviving_parents;
3232 struct merge_simplify_state {
3233 struct commit *simplified;
3236 static struct merge_simplify_state *locate_simplify_state(struct rev_info *revs, struct commit *commit)
3238 struct merge_simplify_state *st;
3240 st = lookup_decoration(&revs->merge_simplification, &commit->object);
3241 if (!st) {
3242 CALLOC_ARRAY(st, 1);
3243 add_decoration(&revs->merge_simplification, &commit->object, st);
3245 return st;
3248 static int mark_redundant_parents(struct commit *commit)
3250 struct commit_list *h = reduce_heads(commit->parents);
3251 int i = 0, marked = 0;
3252 struct commit_list *po, *pn;
3254 /* Want these for sanity-checking only */
3255 int orig_cnt = commit_list_count(commit->parents);
3256 int cnt = commit_list_count(h);
3259 * Not ready to remove items yet, just mark them for now, based
3260 * on the output of reduce_heads(). reduce_heads outputs the reduced
3261 * set in its original order, so this isn't too hard.
3263 po = commit->parents;
3264 pn = h;
3265 while (po) {
3266 if (pn && po->item == pn->item) {
3267 pn = pn->next;
3268 i++;
3269 } else {
3270 po->item->object.flags |= TMP_MARK;
3271 marked++;
3273 po=po->next;
3276 if (i != cnt || cnt+marked != orig_cnt)
3277 die("mark_redundant_parents %d %d %d %d", orig_cnt, cnt, i, marked);
3279 free_commit_list(h);
3281 return marked;
3284 static int mark_treesame_root_parents(struct commit *commit)
3286 struct commit_list *p;
3287 int marked = 0;
3289 for (p = commit->parents; p; p = p->next) {
3290 struct commit *parent = p->item;
3291 if (!parent->parents && (parent->object.flags & TREESAME)) {
3292 parent->object.flags |= TMP_MARK;
3293 marked++;
3297 return marked;
3301 * Awkward naming - this means one parent we are TREESAME to.
3302 * cf mark_treesame_root_parents: root parents that are TREESAME (to an
3303 * empty tree). Better name suggestions?
3305 static int leave_one_treesame_to_parent(struct rev_info *revs, struct commit *commit)
3307 struct treesame_state *ts = lookup_decoration(&revs->treesame, &commit->object);
3308 struct commit *unmarked = NULL, *marked = NULL;
3309 struct commit_list *p;
3310 unsigned n;
3312 for (p = commit->parents, n = 0; p; p = p->next, n++) {
3313 if (ts->treesame[n]) {
3314 if (p->item->object.flags & TMP_MARK) {
3315 if (!marked)
3316 marked = p->item;
3317 } else {
3318 if (!unmarked) {
3319 unmarked = p->item;
3320 break;
3327 * If we are TREESAME to a marked-for-deletion parent, but not to any
3328 * unmarked parents, unmark the first TREESAME parent. This is the
3329 * parent that the default simplify_history==1 scan would have followed,
3330 * and it doesn't make sense to omit that path when asking for a
3331 * simplified full history. Retaining it improves the chances of
3332 * understanding odd missed merges that took an old version of a file.
3334 * Example:
3336 * I--------*X A modified the file, but mainline merge X used
3337 * \ / "-s ours", so took the version from I. X is
3338 * `-*A--' TREESAME to I and !TREESAME to A.
3340 * Default log from X would produce "I". Without this check,
3341 * --full-history --simplify-merges would produce "I-A-X", showing
3342 * the merge commit X and that it changed A, but not making clear that
3343 * it had just taken the I version. With this check, the topology above
3344 * is retained.
3346 * Note that it is possible that the simplification chooses a different
3347 * TREESAME parent from the default, in which case this test doesn't
3348 * activate, and we _do_ drop the default parent. Example:
3350 * I------X A modified the file, but it was reverted in B,
3351 * \ / meaning mainline merge X is TREESAME to both
3352 * *A-*B parents.
3354 * Default log would produce "I" by following the first parent;
3355 * --full-history --simplify-merges will produce "I-A-B". But this is a
3356 * reasonable result - it presents a logical full history leading from
3357 * I to X, and X is not an important merge.
3359 if (!unmarked && marked) {
3360 marked->object.flags &= ~TMP_MARK;
3361 return 1;
3364 return 0;
3367 static int remove_marked_parents(struct rev_info *revs, struct commit *commit)
3369 struct commit_list **pp, *p;
3370 int nth_parent, removed = 0;
3372 pp = &commit->parents;
3373 nth_parent = 0;
3374 while ((p = *pp) != NULL) {
3375 struct commit *parent = p->item;
3376 if (parent->object.flags & TMP_MARK) {
3377 parent->object.flags &= ~TMP_MARK;
3378 *pp = p->next;
3379 free(p);
3380 removed++;
3381 compact_treesame(revs, commit, nth_parent);
3382 continue;
3384 pp = &p->next;
3385 nth_parent++;
3388 /* Removing parents can only increase TREESAMEness */
3389 if (removed && !(commit->object.flags & TREESAME))
3390 update_treesame(revs, commit);
3392 return nth_parent;
3395 static struct commit_list **simplify_one(struct rev_info *revs, struct commit *commit, struct commit_list **tail)
3397 struct commit_list *p;
3398 struct commit *parent;
3399 struct merge_simplify_state *st, *pst;
3400 int cnt;
3402 st = locate_simplify_state(revs, commit);
3405 * Have we handled this one?
3407 if (st->simplified)
3408 return tail;
3411 * An UNINTERESTING commit simplifies to itself, so does a
3412 * root commit. We do not rewrite parents of such commit
3413 * anyway.
3415 if ((commit->object.flags & UNINTERESTING) || !commit->parents) {
3416 st->simplified = commit;
3417 return tail;
3421 * Do we know what commit all of our parents that matter
3422 * should be rewritten to? Otherwise we are not ready to
3423 * rewrite this one yet.
3425 for (cnt = 0, p = commit->parents; p; p = p->next) {
3426 pst = locate_simplify_state(revs, p->item);
3427 if (!pst->simplified) {
3428 tail = &commit_list_insert(p->item, tail)->next;
3429 cnt++;
3431 if (revs->first_parent_only)
3432 break;
3434 if (cnt) {
3435 tail = &commit_list_insert(commit, tail)->next;
3436 return tail;
3440 * Rewrite our list of parents. Note that this cannot
3441 * affect our TREESAME flags in any way - a commit is
3442 * always TREESAME to its simplification.
3444 for (p = commit->parents; p; p = p->next) {
3445 pst = locate_simplify_state(revs, p->item);
3446 p->item = pst->simplified;
3447 if (revs->first_parent_only)
3448 break;
3451 if (revs->first_parent_only)
3452 cnt = 1;
3453 else
3454 cnt = remove_duplicate_parents(revs, commit);
3457 * It is possible that we are a merge and one side branch
3458 * does not have any commit that touches the given paths;
3459 * in such a case, the immediate parent from that branch
3460 * will be rewritten to be the merge base.
3462 * o----X X: the commit we are looking at;
3463 * / / o: a commit that touches the paths;
3464 * ---o----'
3466 * Further, a merge of an independent branch that doesn't
3467 * touch the path will reduce to a treesame root parent:
3469 * ----o----X X: the commit we are looking at;
3470 * / o: a commit that touches the paths;
3471 * r r: a root commit not touching the paths
3473 * Detect and simplify both cases.
3475 if (1 < cnt) {
3476 int marked = mark_redundant_parents(commit);
3477 marked += mark_treesame_root_parents(commit);
3478 if (marked)
3479 marked -= leave_one_treesame_to_parent(revs, commit);
3480 if (marked)
3481 cnt = remove_marked_parents(revs, commit);
3485 * A commit simplifies to itself if it is a root, if it is
3486 * UNINTERESTING, if it touches the given paths, or if it is a
3487 * merge and its parents don't simplify to one relevant commit
3488 * (the first two cases are already handled at the beginning of
3489 * this function).
3491 * Otherwise, it simplifies to what its sole relevant parent
3492 * simplifies to.
3494 if (!cnt ||
3495 (commit->object.flags & UNINTERESTING) ||
3496 !(commit->object.flags & TREESAME) ||
3497 (parent = one_relevant_parent(revs, commit->parents)) == NULL ||
3498 (revs->show_pulls && (commit->object.flags & PULL_MERGE)))
3499 st->simplified = commit;
3500 else {
3501 pst = locate_simplify_state(revs, parent);
3502 st->simplified = pst->simplified;
3504 return tail;
3507 static void simplify_merges(struct rev_info *revs)
3509 struct commit_list *list, *next;
3510 struct commit_list *yet_to_do, **tail;
3511 struct commit *commit;
3513 if (!revs->prune)
3514 return;
3516 /* feed the list reversed */
3517 yet_to_do = NULL;
3518 for (list = revs->commits; list; list = next) {
3519 commit = list->item;
3520 next = list->next;
3522 * Do not free(list) here yet; the original list
3523 * is used later in this function.
3525 commit_list_insert(commit, &yet_to_do);
3527 while (yet_to_do) {
3528 list = yet_to_do;
3529 yet_to_do = NULL;
3530 tail = &yet_to_do;
3531 while (list) {
3532 commit = pop_commit(&list);
3533 tail = simplify_one(revs, commit, tail);
3537 /* clean up the result, removing the simplified ones */
3538 list = revs->commits;
3539 revs->commits = NULL;
3540 tail = &revs->commits;
3541 while (list) {
3542 struct merge_simplify_state *st;
3544 commit = pop_commit(&list);
3545 st = locate_simplify_state(revs, commit);
3546 if (st->simplified == commit)
3547 tail = &commit_list_insert(commit, tail)->next;
3551 static void set_children(struct rev_info *revs)
3553 struct commit_list *l;
3554 for (l = revs->commits; l; l = l->next) {
3555 struct commit *commit = l->item;
3556 struct commit_list *p;
3558 for (p = commit->parents; p; p = p->next)
3559 add_child(revs, p->item, commit);
3563 void reset_revision_walk(void)
3565 clear_object_flags(SEEN | ADDED | SHOWN | TOPO_WALK_EXPLORED | TOPO_WALK_INDEGREE);
3568 static int mark_uninteresting(const struct object_id *oid,
3569 struct packed_git *pack UNUSED,
3570 uint32_t pos UNUSED,
3571 void *cb)
3573 struct rev_info *revs = cb;
3574 struct object *o = lookup_unknown_object(revs->repo, oid);
3575 o->flags |= UNINTERESTING | SEEN;
3576 return 0;
3579 define_commit_slab(indegree_slab, int);
3580 define_commit_slab(author_date_slab, timestamp_t);
3582 struct topo_walk_info {
3583 timestamp_t min_generation;
3584 struct prio_queue explore_queue;
3585 struct prio_queue indegree_queue;
3586 struct prio_queue topo_queue;
3587 struct indegree_slab indegree;
3588 struct author_date_slab author_date;
3591 static int topo_walk_atexit_registered;
3592 static unsigned int count_explore_walked;
3593 static unsigned int count_indegree_walked;
3594 static unsigned int count_topo_walked;
3596 static void trace2_topo_walk_statistics_atexit(void)
3598 struct json_writer jw = JSON_WRITER_INIT;
3600 jw_object_begin(&jw, 0);
3601 jw_object_intmax(&jw, "count_explore_walked", count_explore_walked);
3602 jw_object_intmax(&jw, "count_indegree_walked", count_indegree_walked);
3603 jw_object_intmax(&jw, "count_topo_walked", count_topo_walked);
3604 jw_end(&jw);
3606 trace2_data_json("topo_walk", the_repository, "statistics", &jw);
3608 jw_release(&jw);
3611 static inline void test_flag_and_insert(struct prio_queue *q, struct commit *c, int flag)
3613 if (c->object.flags & flag)
3614 return;
3616 c->object.flags |= flag;
3617 prio_queue_put(q, c);
3620 static void explore_walk_step(struct rev_info *revs)
3622 struct topo_walk_info *info = revs->topo_walk_info;
3623 struct commit_list *p;
3624 struct commit *c = prio_queue_get(&info->explore_queue);
3626 if (!c)
3627 return;
3629 if (repo_parse_commit_gently(revs->repo, c, 1) < 0)
3630 return;
3632 count_explore_walked++;
3634 if (revs->sort_order == REV_SORT_BY_AUTHOR_DATE)
3635 record_author_date(&info->author_date, c);
3637 if (revs->max_age != -1 && (c->date < revs->max_age))
3638 c->object.flags |= UNINTERESTING;
3640 if (process_parents(revs, c, NULL, NULL) < 0)
3641 return;
3643 if (c->object.flags & UNINTERESTING)
3644 mark_parents_uninteresting(revs, c);
3646 for (p = c->parents; p; p = p->next)
3647 test_flag_and_insert(&info->explore_queue, p->item, TOPO_WALK_EXPLORED);
3650 static void explore_to_depth(struct rev_info *revs,
3651 timestamp_t gen_cutoff)
3653 struct topo_walk_info *info = revs->topo_walk_info;
3654 struct commit *c;
3655 while ((c = prio_queue_peek(&info->explore_queue)) &&
3656 commit_graph_generation(c) >= gen_cutoff)
3657 explore_walk_step(revs);
3660 static void indegree_walk_step(struct rev_info *revs)
3662 struct commit_list *p;
3663 struct topo_walk_info *info = revs->topo_walk_info;
3664 struct commit *c = prio_queue_get(&info->indegree_queue);
3666 if (!c)
3667 return;
3669 if (repo_parse_commit_gently(revs->repo, c, 1) < 0)
3670 return;
3672 count_indegree_walked++;
3674 explore_to_depth(revs, commit_graph_generation(c));
3676 for (p = c->parents; p; p = p->next) {
3677 struct commit *parent = p->item;
3678 int *pi = indegree_slab_at(&info->indegree, parent);
3680 if (repo_parse_commit_gently(revs->repo, parent, 1) < 0)
3681 return;
3683 if (*pi)
3684 (*pi)++;
3685 else
3686 *pi = 2;
3688 test_flag_and_insert(&info->indegree_queue, parent, TOPO_WALK_INDEGREE);
3690 if (revs->first_parent_only)
3691 return;
3695 static void compute_indegrees_to_depth(struct rev_info *revs,
3696 timestamp_t gen_cutoff)
3698 struct topo_walk_info *info = revs->topo_walk_info;
3699 struct commit *c;
3700 while ((c = prio_queue_peek(&info->indegree_queue)) &&
3701 commit_graph_generation(c) >= gen_cutoff)
3702 indegree_walk_step(revs);
3705 static void release_revisions_topo_walk_info(struct topo_walk_info *info)
3707 if (!info)
3708 return;
3709 clear_prio_queue(&info->explore_queue);
3710 clear_prio_queue(&info->indegree_queue);
3711 clear_prio_queue(&info->topo_queue);
3712 clear_indegree_slab(&info->indegree);
3713 clear_author_date_slab(&info->author_date);
3714 free(info);
3717 static void reset_topo_walk(struct rev_info *revs)
3719 release_revisions_topo_walk_info(revs->topo_walk_info);
3720 revs->topo_walk_info = NULL;
3723 static void init_topo_walk(struct rev_info *revs)
3725 struct topo_walk_info *info;
3726 struct commit_list *list;
3727 if (revs->topo_walk_info)
3728 reset_topo_walk(revs);
3730 revs->topo_walk_info = xmalloc(sizeof(struct topo_walk_info));
3731 info = revs->topo_walk_info;
3732 memset(info, 0, sizeof(struct topo_walk_info));
3734 init_indegree_slab(&info->indegree);
3735 memset(&info->explore_queue, 0, sizeof(info->explore_queue));
3736 memset(&info->indegree_queue, 0, sizeof(info->indegree_queue));
3737 memset(&info->topo_queue, 0, sizeof(info->topo_queue));
3739 switch (revs->sort_order) {
3740 default: /* REV_SORT_IN_GRAPH_ORDER */
3741 info->topo_queue.compare = NULL;
3742 break;
3743 case REV_SORT_BY_COMMIT_DATE:
3744 info->topo_queue.compare = compare_commits_by_commit_date;
3745 break;
3746 case REV_SORT_BY_AUTHOR_DATE:
3747 init_author_date_slab(&info->author_date);
3748 info->topo_queue.compare = compare_commits_by_author_date;
3749 info->topo_queue.cb_data = &info->author_date;
3750 break;
3753 info->explore_queue.compare = compare_commits_by_gen_then_commit_date;
3754 info->indegree_queue.compare = compare_commits_by_gen_then_commit_date;
3756 info->min_generation = GENERATION_NUMBER_INFINITY;
3757 for (list = revs->commits; list; list = list->next) {
3758 struct commit *c = list->item;
3759 timestamp_t generation;
3761 if (repo_parse_commit_gently(revs->repo, c, 1))
3762 continue;
3764 test_flag_and_insert(&info->explore_queue, c, TOPO_WALK_EXPLORED);
3765 test_flag_and_insert(&info->indegree_queue, c, TOPO_WALK_INDEGREE);
3767 generation = commit_graph_generation(c);
3768 if (generation < info->min_generation)
3769 info->min_generation = generation;
3771 *(indegree_slab_at(&info->indegree, c)) = 1;
3773 if (revs->sort_order == REV_SORT_BY_AUTHOR_DATE)
3774 record_author_date(&info->author_date, c);
3776 compute_indegrees_to_depth(revs, info->min_generation);
3778 for (list = revs->commits; list; list = list->next) {
3779 struct commit *c = list->item;
3781 if (*(indegree_slab_at(&info->indegree, c)) == 1)
3782 prio_queue_put(&info->topo_queue, c);
3786 * This is unfortunate; the initial tips need to be shown
3787 * in the order given from the revision traversal machinery.
3789 if (revs->sort_order == REV_SORT_IN_GRAPH_ORDER)
3790 prio_queue_reverse(&info->topo_queue);
3792 if (trace2_is_enabled() && !topo_walk_atexit_registered) {
3793 atexit(trace2_topo_walk_statistics_atexit);
3794 topo_walk_atexit_registered = 1;
3798 static struct commit *next_topo_commit(struct rev_info *revs)
3800 struct commit *c;
3801 struct topo_walk_info *info = revs->topo_walk_info;
3803 /* pop next off of topo_queue */
3804 c = prio_queue_get(&info->topo_queue);
3806 if (c)
3807 *(indegree_slab_at(&info->indegree, c)) = 0;
3809 return c;
3812 static void expand_topo_walk(struct rev_info *revs, struct commit *commit)
3814 struct commit_list *p;
3815 struct topo_walk_info *info = revs->topo_walk_info;
3816 if (process_parents(revs, commit, NULL, NULL) < 0) {
3817 if (!revs->ignore_missing_links)
3818 die("Failed to traverse parents of commit %s",
3819 oid_to_hex(&commit->object.oid));
3822 count_topo_walked++;
3824 for (p = commit->parents; p; p = p->next) {
3825 struct commit *parent = p->item;
3826 int *pi;
3827 timestamp_t generation;
3829 if (parent->object.flags & UNINTERESTING)
3830 continue;
3832 if (repo_parse_commit_gently(revs->repo, parent, 1) < 0)
3833 continue;
3835 generation = commit_graph_generation(parent);
3836 if (generation < info->min_generation) {
3837 info->min_generation = generation;
3838 compute_indegrees_to_depth(revs, info->min_generation);
3841 pi = indegree_slab_at(&info->indegree, parent);
3843 (*pi)--;
3844 if (*pi == 1)
3845 prio_queue_put(&info->topo_queue, parent);
3847 if (revs->first_parent_only)
3848 return;
3852 int prepare_revision_walk(struct rev_info *revs)
3854 int i;
3855 struct object_array old_pending;
3856 struct commit_list **next = &revs->commits;
3858 memcpy(&old_pending, &revs->pending, sizeof(old_pending));
3859 revs->pending.nr = 0;
3860 revs->pending.alloc = 0;
3861 revs->pending.objects = NULL;
3862 for (i = 0; i < old_pending.nr; i++) {
3863 struct object_array_entry *e = old_pending.objects + i;
3864 struct commit *commit = handle_commit(revs, e);
3865 if (commit) {
3866 if (!(commit->object.flags & SEEN)) {
3867 commit->object.flags |= SEEN;
3868 next = commit_list_append(commit, next);
3872 object_array_clear(&old_pending);
3874 /* Signal whether we need per-parent treesame decoration */
3875 if (revs->simplify_merges ||
3876 (revs->limited && limiting_can_increase_treesame(revs)))
3877 revs->treesame.name = "treesame";
3879 if (revs->exclude_promisor_objects) {
3880 for_each_packed_object(mark_uninteresting, revs,
3881 FOR_EACH_OBJECT_PROMISOR_ONLY);
3884 if (!revs->reflog_info)
3885 prepare_to_use_bloom_filter(revs);
3886 if (!revs->unsorted_input)
3887 commit_list_sort_by_date(&revs->commits);
3888 if (revs->no_walk)
3889 return 0;
3890 if (revs->limited) {
3891 if (limit_list(revs) < 0)
3892 return -1;
3893 if (revs->topo_order)
3894 sort_in_topological_order(&revs->commits, revs->sort_order);
3895 } else if (revs->topo_order)
3896 init_topo_walk(revs);
3897 if (revs->line_level_traverse && want_ancestry(revs))
3899 * At the moment we can only do line-level log with parent
3900 * rewriting by performing this expensive pre-filtering step.
3901 * If parent rewriting is not requested, then we rather
3902 * perform the line-level log filtering during the regular
3903 * history traversal.
3905 line_log_filter(revs);
3906 if (revs->simplify_merges)
3907 simplify_merges(revs);
3908 if (revs->children.name)
3909 set_children(revs);
3911 return 0;
3914 static enum rewrite_result rewrite_one_1(struct rev_info *revs,
3915 struct commit **pp,
3916 struct prio_queue *queue)
3918 for (;;) {
3919 struct commit *p = *pp;
3920 if (!revs->limited)
3921 if (process_parents(revs, p, NULL, queue) < 0)
3922 return rewrite_one_error;
3923 if (p->object.flags & UNINTERESTING)
3924 return rewrite_one_ok;
3925 if (!(p->object.flags & TREESAME))
3926 return rewrite_one_ok;
3927 if (!p->parents)
3928 return rewrite_one_noparents;
3929 if (!(p = one_relevant_parent(revs, p->parents)))
3930 return rewrite_one_ok;
3931 *pp = p;
3935 static void merge_queue_into_list(struct prio_queue *q, struct commit_list **list)
3937 while (q->nr) {
3938 struct commit *item = prio_queue_peek(q);
3939 struct commit_list *p = *list;
3941 if (p && p->item->date >= item->date)
3942 list = &p->next;
3943 else {
3944 p = commit_list_insert(item, list);
3945 list = &p->next; /* skip newly added item */
3946 prio_queue_get(q); /* pop item */
3951 static enum rewrite_result rewrite_one(struct rev_info *revs, struct commit **pp)
3953 struct prio_queue queue = { compare_commits_by_commit_date };
3954 enum rewrite_result ret = rewrite_one_1(revs, pp, &queue);
3955 merge_queue_into_list(&queue, &revs->commits);
3956 clear_prio_queue(&queue);
3957 return ret;
3960 int rewrite_parents(struct rev_info *revs, struct commit *commit,
3961 rewrite_parent_fn_t rewrite_parent)
3963 struct commit_list **pp = &commit->parents;
3964 while (*pp) {
3965 struct commit_list *parent = *pp;
3966 switch (rewrite_parent(revs, &parent->item)) {
3967 case rewrite_one_ok:
3968 break;
3969 case rewrite_one_noparents:
3970 *pp = parent->next;
3971 continue;
3972 case rewrite_one_error:
3973 return -1;
3975 pp = &parent->next;
3977 remove_duplicate_parents(revs, commit);
3978 return 0;
3981 static int commit_match(struct commit *commit, struct rev_info *opt)
3983 int retval;
3984 const char *encoding;
3985 const char *message;
3986 struct strbuf buf = STRBUF_INIT;
3988 if (!opt->grep_filter.pattern_list && !opt->grep_filter.header_list)
3989 return 1;
3991 /* Prepend "fake" headers as needed */
3992 if (opt->grep_filter.use_reflog_filter) {
3993 strbuf_addstr(&buf, "reflog ");
3994 get_reflog_message(&buf, opt->reflog_info);
3995 strbuf_addch(&buf, '\n');
3999 * We grep in the user's output encoding, under the assumption that it
4000 * is the encoding they are most likely to write their grep pattern
4001 * for. In addition, it means we will match the "notes" encoding below,
4002 * so we will not end up with a buffer that has two different encodings
4003 * in it.
4005 encoding = get_log_output_encoding();
4006 message = repo_logmsg_reencode(the_repository, commit, NULL, encoding);
4008 /* Copy the commit to temporary if we are using "fake" headers */
4009 if (buf.len)
4010 strbuf_addstr(&buf, message);
4012 if (opt->grep_filter.header_list && opt->mailmap) {
4013 const char *commit_headers[] = { "author ", "committer ", NULL };
4015 if (!buf.len)
4016 strbuf_addstr(&buf, message);
4018 apply_mailmap_to_header(&buf, commit_headers, opt->mailmap);
4021 /* Append "fake" message parts as needed */
4022 if (opt->show_notes) {
4023 if (!buf.len)
4024 strbuf_addstr(&buf, message);
4025 format_display_notes(&commit->object.oid, &buf, encoding, 1);
4029 * Find either in the original commit message, or in the temporary.
4030 * Note that we cast away the constness of "message" here. It is
4031 * const because it may come from the cached commit buffer. That's OK,
4032 * because we know that it is modifiable heap memory, and that while
4033 * grep_buffer may modify it for speed, it will restore any
4034 * changes before returning.
4036 if (buf.len)
4037 retval = grep_buffer(&opt->grep_filter, buf.buf, buf.len);
4038 else
4039 retval = grep_buffer(&opt->grep_filter,
4040 (char *)message, strlen(message));
4041 strbuf_release(&buf);
4042 repo_unuse_commit_buffer(the_repository, commit, message);
4043 return retval;
4046 static inline int want_ancestry(const struct rev_info *revs)
4048 return (revs->rewrite_parents || revs->children.name);
4052 * Return a timestamp to be used for --since/--until comparisons for this
4053 * commit, based on the revision options.
4055 static timestamp_t comparison_date(const struct rev_info *revs,
4056 struct commit *commit)
4058 return revs->reflog_info ?
4059 get_reflog_timestamp(revs->reflog_info) :
4060 commit->date;
4063 enum commit_action get_commit_action(struct rev_info *revs, struct commit *commit)
4065 if (commit->object.flags & SHOWN)
4066 return commit_ignore;
4067 if (revs->unpacked && has_object_pack(&commit->object.oid))
4068 return commit_ignore;
4069 if (revs->no_kept_objects) {
4070 if (has_object_kept_pack(&commit->object.oid,
4071 revs->keep_pack_cache_flags))
4072 return commit_ignore;
4074 if (commit->object.flags & UNINTERESTING)
4075 return commit_ignore;
4076 if (revs->line_level_traverse && !want_ancestry(revs)) {
4078 * In case of line-level log with parent rewriting
4079 * prepare_revision_walk() already took care of all line-level
4080 * log filtering, and there is nothing left to do here.
4082 * If parent rewriting was not requested, then this is the
4083 * place to perform the line-level log filtering. Notably,
4084 * this check, though expensive, must come before the other,
4085 * cheaper filtering conditions, because the tracked line
4086 * ranges must be adjusted even when the commit will end up
4087 * being ignored based on other conditions.
4089 if (!line_log_process_ranges_arbitrary_commit(revs, commit))
4090 return commit_ignore;
4092 if (revs->min_age != -1 &&
4093 comparison_date(revs, commit) > revs->min_age)
4094 return commit_ignore;
4095 if (revs->max_age_as_filter != -1 &&
4096 comparison_date(revs, commit) < revs->max_age_as_filter)
4097 return commit_ignore;
4098 if (revs->min_parents || (revs->max_parents >= 0)) {
4099 int n = commit_list_count(commit->parents);
4100 if ((n < revs->min_parents) ||
4101 ((revs->max_parents >= 0) && (n > revs->max_parents)))
4102 return commit_ignore;
4104 if (!commit_match(commit, revs))
4105 return commit_ignore;
4106 if (revs->prune && revs->dense) {
4107 /* Commit without changes? */
4108 if (commit->object.flags & TREESAME) {
4109 int n;
4110 struct commit_list *p;
4111 /* drop merges unless we want parenthood */
4112 if (!want_ancestry(revs))
4113 return commit_ignore;
4115 if (revs->show_pulls && (commit->object.flags & PULL_MERGE))
4116 return commit_show;
4119 * If we want ancestry, then need to keep any merges
4120 * between relevant commits to tie together topology.
4121 * For consistency with TREESAME and simplification
4122 * use "relevant" here rather than just INTERESTING,
4123 * to treat bottom commit(s) as part of the topology.
4125 for (n = 0, p = commit->parents; p; p = p->next)
4126 if (relevant_commit(p->item))
4127 if (++n >= 2)
4128 return commit_show;
4129 return commit_ignore;
4132 return commit_show;
4135 define_commit_slab(saved_parents, struct commit_list *);
4137 #define EMPTY_PARENT_LIST ((struct commit_list *)-1)
4140 * You may only call save_parents() once per commit (this is checked
4141 * for non-root commits).
4143 static void save_parents(struct rev_info *revs, struct commit *commit)
4145 struct commit_list **pp;
4147 if (!revs->saved_parents_slab) {
4148 revs->saved_parents_slab = xmalloc(sizeof(struct saved_parents));
4149 init_saved_parents(revs->saved_parents_slab);
4152 pp = saved_parents_at(revs->saved_parents_slab, commit);
4155 * When walking with reflogs, we may visit the same commit
4156 * several times: once for each appearance in the reflog.
4158 * In this case, save_parents() will be called multiple times.
4159 * We want to keep only the first set of parents. We need to
4160 * store a sentinel value for an empty (i.e., NULL) parent
4161 * list to distinguish it from a not-yet-saved list, however.
4163 if (*pp)
4164 return;
4165 if (commit->parents)
4166 *pp = copy_commit_list(commit->parents);
4167 else
4168 *pp = EMPTY_PARENT_LIST;
4171 static void free_saved_parents(struct rev_info *revs)
4173 if (revs->saved_parents_slab)
4174 clear_saved_parents(revs->saved_parents_slab);
4177 struct commit_list *get_saved_parents(struct rev_info *revs, const struct commit *commit)
4179 struct commit_list *parents;
4181 if (!revs->saved_parents_slab)
4182 return commit->parents;
4184 parents = *saved_parents_at(revs->saved_parents_slab, commit);
4185 if (parents == EMPTY_PARENT_LIST)
4186 return NULL;
4187 return parents;
4190 enum commit_action simplify_commit(struct rev_info *revs, struct commit *commit)
4192 enum commit_action action = get_commit_action(revs, commit);
4194 if (action == commit_show &&
4195 revs->prune && revs->dense && want_ancestry(revs)) {
4197 * --full-diff on simplified parents is no good: it
4198 * will show spurious changes from the commits that
4199 * were elided. So we save the parents on the side
4200 * when --full-diff is in effect.
4202 if (revs->full_diff)
4203 save_parents(revs, commit);
4204 if (rewrite_parents(revs, commit, rewrite_one) < 0)
4205 return commit_error;
4207 return action;
4210 static void track_linear(struct rev_info *revs, struct commit *commit)
4212 if (revs->track_first_time) {
4213 revs->linear = 1;
4214 revs->track_first_time = 0;
4215 } else {
4216 struct commit_list *p;
4217 for (p = revs->previous_parents; p; p = p->next)
4218 if (p->item == NULL || /* first commit */
4219 oideq(&p->item->object.oid, &commit->object.oid))
4220 break;
4221 revs->linear = p != NULL;
4223 if (revs->reverse) {
4224 if (revs->linear)
4225 commit->object.flags |= TRACK_LINEAR;
4227 free_commit_list(revs->previous_parents);
4228 revs->previous_parents = copy_commit_list(commit->parents);
4231 static struct commit *get_revision_1(struct rev_info *revs)
4233 while (1) {
4234 struct commit *commit;
4236 if (revs->reflog_info)
4237 commit = next_reflog_entry(revs->reflog_info);
4238 else if (revs->topo_walk_info)
4239 commit = next_topo_commit(revs);
4240 else
4241 commit = pop_commit(&revs->commits);
4243 if (!commit)
4244 return NULL;
4246 if (revs->reflog_info)
4247 commit->object.flags &= ~(ADDED | SEEN | SHOWN);
4250 * If we haven't done the list limiting, we need to look at
4251 * the parents here. We also need to do the date-based limiting
4252 * that we'd otherwise have done in limit_list().
4254 if (!revs->limited) {
4255 if (revs->max_age != -1 &&
4256 comparison_date(revs, commit) < revs->max_age)
4257 continue;
4259 if (revs->reflog_info)
4260 try_to_simplify_commit(revs, commit);
4261 else if (revs->topo_walk_info)
4262 expand_topo_walk(revs, commit);
4263 else if (process_parents(revs, commit, &revs->commits, NULL) < 0) {
4264 if (!revs->ignore_missing_links)
4265 die("Failed to traverse parents of commit %s",
4266 oid_to_hex(&commit->object.oid));
4270 switch (simplify_commit(revs, commit)) {
4271 case commit_ignore:
4272 continue;
4273 case commit_error:
4274 die("Failed to simplify parents of commit %s",
4275 oid_to_hex(&commit->object.oid));
4276 default:
4277 if (revs->track_linear)
4278 track_linear(revs, commit);
4279 return commit;
4285 * Return true for entries that have not yet been shown. (This is an
4286 * object_array_each_func_t.)
4288 static int entry_unshown(struct object_array_entry *entry, void *cb_data UNUSED)
4290 return !(entry->item->flags & SHOWN);
4294 * If array is on the verge of a realloc, garbage-collect any entries
4295 * that have already been shown to try to free up some space.
4297 static void gc_boundary(struct object_array *array)
4299 if (array->nr == array->alloc)
4300 object_array_filter(array, entry_unshown, NULL);
4303 static void create_boundary_commit_list(struct rev_info *revs)
4305 unsigned i;
4306 struct commit *c;
4307 struct object_array *array = &revs->boundary_commits;
4308 struct object_array_entry *objects = array->objects;
4311 * If revs->commits is non-NULL at this point, an error occurred in
4312 * get_revision_1(). Ignore the error and continue printing the
4313 * boundary commits anyway. (This is what the code has always
4314 * done.)
4316 free_commit_list(revs->commits);
4317 revs->commits = NULL;
4320 * Put all of the actual boundary commits from revs->boundary_commits
4321 * into revs->commits
4323 for (i = 0; i < array->nr; i++) {
4324 c = (struct commit *)(objects[i].item);
4325 if (!c)
4326 continue;
4327 if (!(c->object.flags & CHILD_SHOWN))
4328 continue;
4329 if (c->object.flags & (SHOWN | BOUNDARY))
4330 continue;
4331 c->object.flags |= BOUNDARY;
4332 commit_list_insert(c, &revs->commits);
4336 * If revs->topo_order is set, sort the boundary commits
4337 * in topological order
4339 sort_in_topological_order(&revs->commits, revs->sort_order);
4342 static struct commit *get_revision_internal(struct rev_info *revs)
4344 struct commit *c = NULL;
4345 struct commit_list *l;
4347 if (revs->boundary == 2) {
4349 * All of the normal commits have already been returned,
4350 * and we are now returning boundary commits.
4351 * create_boundary_commit_list() has populated
4352 * revs->commits with the remaining commits to return.
4354 c = pop_commit(&revs->commits);
4355 if (c)
4356 c->object.flags |= SHOWN;
4357 return c;
4361 * If our max_count counter has reached zero, then we are done. We
4362 * don't simply return NULL because we still might need to show
4363 * boundary commits. But we want to avoid calling get_revision_1, which
4364 * might do a considerable amount of work finding the next commit only
4365 * for us to throw it away.
4367 * If it is non-zero, then either we don't have a max_count at all
4368 * (-1), or it is still counting, in which case we decrement.
4370 if (revs->max_count) {
4371 c = get_revision_1(revs);
4372 if (c) {
4373 while (revs->skip_count > 0) {
4374 revs->skip_count--;
4375 c = get_revision_1(revs);
4376 if (!c)
4377 break;
4381 if (revs->max_count > 0)
4382 revs->max_count--;
4385 if (c)
4386 c->object.flags |= SHOWN;
4388 if (!revs->boundary)
4389 return c;
4391 if (!c) {
4393 * get_revision_1() runs out the commits, and
4394 * we are done computing the boundaries.
4395 * switch to boundary commits output mode.
4397 revs->boundary = 2;
4400 * Update revs->commits to contain the list of
4401 * boundary commits.
4403 create_boundary_commit_list(revs);
4405 return get_revision_internal(revs);
4409 * boundary commits are the commits that are parents of the
4410 * ones we got from get_revision_1() but they themselves are
4411 * not returned from get_revision_1(). Before returning
4412 * 'c', we need to mark its parents that they could be boundaries.
4415 for (l = c->parents; l; l = l->next) {
4416 struct object *p;
4417 p = &(l->item->object);
4418 if (p->flags & (CHILD_SHOWN | SHOWN))
4419 continue;
4420 p->flags |= CHILD_SHOWN;
4421 gc_boundary(&revs->boundary_commits);
4422 add_object_array(p, NULL, &revs->boundary_commits);
4425 return c;
4428 struct commit *get_revision(struct rev_info *revs)
4430 struct commit *c;
4431 struct commit_list *reversed;
4433 if (revs->reverse) {
4434 reversed = NULL;
4435 while ((c = get_revision_internal(revs)))
4436 commit_list_insert(c, &reversed);
4437 revs->commits = reversed;
4438 revs->reverse = 0;
4439 revs->reverse_output_stage = 1;
4442 if (revs->reverse_output_stage) {
4443 c = pop_commit(&revs->commits);
4444 if (revs->track_linear)
4445 revs->linear = !!(c && c->object.flags & TRACK_LINEAR);
4446 return c;
4449 c = get_revision_internal(revs);
4450 if (c && revs->graph)
4451 graph_update(revs->graph, c);
4452 if (!c) {
4453 free_saved_parents(revs);
4454 free_commit_list(revs->previous_parents);
4455 revs->previous_parents = NULL;
4457 return c;
4460 const char *get_revision_mark(const struct rev_info *revs, const struct commit *commit)
4462 if (commit->object.flags & BOUNDARY)
4463 return "-";
4464 else if (commit->object.flags & UNINTERESTING)
4465 return "^";
4466 else if (commit->object.flags & PATCHSAME)
4467 return "=";
4468 else if (!revs || revs->left_right) {
4469 if (commit->object.flags & SYMMETRIC_LEFT)
4470 return "<";
4471 else
4472 return ">";
4473 } else if (revs->graph)
4474 return "*";
4475 else if (revs->cherry_mark)
4476 return "+";
4477 return "";
4480 void put_revision_mark(const struct rev_info *revs, const struct commit *commit)
4482 const char *mark = get_revision_mark(revs, commit);
4483 if (!strlen(mark))
4484 return;
4485 fputs(mark, stdout);
4486 putchar(' ');