commit-reach(repo_get_merge_bases): pass on "missing commits" errors
[git.git] / revision.c
blob60406f365a92b56b278489f6630f3cd89020a7b0
1 #include "git-compat-util.h"
2 #include "config.h"
3 #include "environment.h"
4 #include "gettext.h"
5 #include "hex.h"
6 #include "object-name.h"
7 #include "object-file.h"
8 #include "object-store-ll.h"
9 #include "oidset.h"
10 #include "tag.h"
11 #include "blob.h"
12 #include "tree.h"
13 #include "commit.h"
14 #include "diff.h"
15 #include "diff-merges.h"
16 #include "refs.h"
17 #include "revision.h"
18 #include "repository.h"
19 #include "graph.h"
20 #include "grep.h"
21 #include "reflog-walk.h"
22 #include "patch-ids.h"
23 #include "decorate.h"
24 #include "string-list.h"
25 #include "line-log.h"
26 #include "mailmap.h"
27 #include "commit-slab.h"
28 #include "cache-tree.h"
29 #include "bisect.h"
30 #include "packfile.h"
31 #include "worktree.h"
32 #include "read-cache.h"
33 #include "setup.h"
34 #include "sparse-index.h"
35 #include "strvec.h"
36 #include "trace2.h"
37 #include "commit-reach.h"
38 #include "commit-graph.h"
39 #include "prio-queue.h"
40 #include "hashmap.h"
41 #include "utf8.h"
42 #include "bloom.h"
43 #include "json-writer.h"
44 #include "list-objects-filter-options.h"
45 #include "resolve-undo.h"
46 #include "parse-options.h"
47 #include "wildmatch.h"
49 volatile show_early_output_fn_t show_early_output;
51 static const char *term_bad;
52 static const char *term_good;
54 implement_shared_commit_slab(revision_sources, char *);
56 static inline int want_ancestry(const struct rev_info *revs);
58 void show_object_with_name(FILE *out, struct object *obj, const char *name)
60 fprintf(out, "%s ", oid_to_hex(&obj->oid));
61 for (const char *p = name; *p && *p != '\n'; p++)
62 fputc(*p, out);
63 fputc('\n', out);
66 static void mark_blob_uninteresting(struct blob *blob)
68 if (!blob)
69 return;
70 if (blob->object.flags & UNINTERESTING)
71 return;
72 blob->object.flags |= UNINTERESTING;
75 static void mark_tree_contents_uninteresting(struct repository *r,
76 struct tree *tree)
78 struct tree_desc desc;
79 struct name_entry entry;
81 if (parse_tree_gently(tree, 1) < 0)
82 return;
84 init_tree_desc(&desc, tree->buffer, tree->size);
85 while (tree_entry(&desc, &entry)) {
86 switch (object_type(entry.mode)) {
87 case OBJ_TREE:
88 mark_tree_uninteresting(r, lookup_tree(r, &entry.oid));
89 break;
90 case OBJ_BLOB:
91 mark_blob_uninteresting(lookup_blob(r, &entry.oid));
92 break;
93 default:
94 /* Subproject commit - not in this repository */
95 break;
100 * We don't care about the tree any more
101 * after it has been marked uninteresting.
103 free_tree_buffer(tree);
106 void mark_tree_uninteresting(struct repository *r, struct tree *tree)
108 struct object *obj;
110 if (!tree)
111 return;
113 obj = &tree->object;
114 if (obj->flags & UNINTERESTING)
115 return;
116 obj->flags |= UNINTERESTING;
117 mark_tree_contents_uninteresting(r, tree);
120 struct path_and_oids_entry {
121 struct hashmap_entry ent;
122 char *path;
123 struct oidset trees;
126 static int path_and_oids_cmp(const void *hashmap_cmp_fn_data UNUSED,
127 const struct hashmap_entry *eptr,
128 const struct hashmap_entry *entry_or_key,
129 const void *keydata UNUSED)
131 const struct path_and_oids_entry *e1, *e2;
133 e1 = container_of(eptr, const struct path_and_oids_entry, ent);
134 e2 = container_of(entry_or_key, const struct path_and_oids_entry, ent);
136 return strcmp(e1->path, e2->path);
139 static void paths_and_oids_clear(struct hashmap *map)
141 struct hashmap_iter iter;
142 struct path_and_oids_entry *entry;
144 hashmap_for_each_entry(map, &iter, entry, ent /* member name */) {
145 oidset_clear(&entry->trees);
146 free(entry->path);
149 hashmap_clear_and_free(map, struct path_and_oids_entry, ent);
152 static void paths_and_oids_insert(struct hashmap *map,
153 const char *path,
154 const struct object_id *oid)
156 int hash = strhash(path);
157 struct path_and_oids_entry key;
158 struct path_and_oids_entry *entry;
160 hashmap_entry_init(&key.ent, hash);
162 /* use a shallow copy for the lookup */
163 key.path = (char *)path;
164 oidset_init(&key.trees, 0);
166 entry = hashmap_get_entry(map, &key, ent, NULL);
167 if (!entry) {
168 CALLOC_ARRAY(entry, 1);
169 hashmap_entry_init(&entry->ent, hash);
170 entry->path = xstrdup(key.path);
171 oidset_init(&entry->trees, 16);
172 hashmap_put(map, &entry->ent);
175 oidset_insert(&entry->trees, oid);
178 static void add_children_by_path(struct repository *r,
179 struct tree *tree,
180 struct hashmap *map)
182 struct tree_desc desc;
183 struct name_entry entry;
185 if (!tree)
186 return;
188 if (parse_tree_gently(tree, 1) < 0)
189 return;
191 init_tree_desc(&desc, tree->buffer, tree->size);
192 while (tree_entry(&desc, &entry)) {
193 switch (object_type(entry.mode)) {
194 case OBJ_TREE:
195 paths_and_oids_insert(map, entry.path, &entry.oid);
197 if (tree->object.flags & UNINTERESTING) {
198 struct tree *child = lookup_tree(r, &entry.oid);
199 if (child)
200 child->object.flags |= UNINTERESTING;
202 break;
203 case OBJ_BLOB:
204 if (tree->object.flags & UNINTERESTING) {
205 struct blob *child = lookup_blob(r, &entry.oid);
206 if (child)
207 child->object.flags |= UNINTERESTING;
209 break;
210 default:
211 /* Subproject commit - not in this repository */
212 break;
216 free_tree_buffer(tree);
219 void mark_trees_uninteresting_sparse(struct repository *r,
220 struct oidset *trees)
222 unsigned has_interesting = 0, has_uninteresting = 0;
223 struct hashmap map = HASHMAP_INIT(path_and_oids_cmp, NULL);
224 struct hashmap_iter map_iter;
225 struct path_and_oids_entry *entry;
226 struct object_id *oid;
227 struct oidset_iter iter;
229 oidset_iter_init(trees, &iter);
230 while ((!has_interesting || !has_uninteresting) &&
231 (oid = oidset_iter_next(&iter))) {
232 struct tree *tree = lookup_tree(r, oid);
234 if (!tree)
235 continue;
237 if (tree->object.flags & UNINTERESTING)
238 has_uninteresting = 1;
239 else
240 has_interesting = 1;
243 /* Do not walk unless we have both types of trees. */
244 if (!has_uninteresting || !has_interesting)
245 return;
247 oidset_iter_init(trees, &iter);
248 while ((oid = oidset_iter_next(&iter))) {
249 struct tree *tree = lookup_tree(r, oid);
250 add_children_by_path(r, tree, &map);
253 hashmap_for_each_entry(&map, &map_iter, entry, ent /* member name */)
254 mark_trees_uninteresting_sparse(r, &entry->trees);
256 paths_and_oids_clear(&map);
259 struct commit_stack {
260 struct commit **items;
261 size_t nr, alloc;
263 #define COMMIT_STACK_INIT { 0 }
265 static void commit_stack_push(struct commit_stack *stack, struct commit *commit)
267 ALLOC_GROW(stack->items, stack->nr + 1, stack->alloc);
268 stack->items[stack->nr++] = commit;
271 static struct commit *commit_stack_pop(struct commit_stack *stack)
273 return stack->nr ? stack->items[--stack->nr] : NULL;
276 static void commit_stack_clear(struct commit_stack *stack)
278 FREE_AND_NULL(stack->items);
279 stack->nr = stack->alloc = 0;
282 static void mark_one_parent_uninteresting(struct rev_info *revs, struct commit *commit,
283 struct commit_stack *pending)
285 struct commit_list *l;
287 if (commit->object.flags & UNINTERESTING)
288 return;
289 commit->object.flags |= UNINTERESTING;
292 * Normally we haven't parsed the parent
293 * yet, so we won't have a parent of a parent
294 * here. However, it may turn out that we've
295 * reached this commit some other way (where it
296 * wasn't uninteresting), in which case we need
297 * to mark its parents recursively too..
299 for (l = commit->parents; l; l = l->next) {
300 commit_stack_push(pending, l->item);
301 if (revs && revs->exclude_first_parent_only)
302 break;
306 void mark_parents_uninteresting(struct rev_info *revs, struct commit *commit)
308 struct commit_stack pending = COMMIT_STACK_INIT;
309 struct commit_list *l;
311 for (l = commit->parents; l; l = l->next) {
312 mark_one_parent_uninteresting(revs, l->item, &pending);
313 if (revs && revs->exclude_first_parent_only)
314 break;
317 while (pending.nr > 0)
318 mark_one_parent_uninteresting(revs, commit_stack_pop(&pending),
319 &pending);
321 commit_stack_clear(&pending);
324 static void add_pending_object_with_path(struct rev_info *revs,
325 struct object *obj,
326 const char *name, unsigned mode,
327 const char *path)
329 struct interpret_branch_name_options options = { 0 };
330 if (!obj)
331 return;
332 if (revs->no_walk && (obj->flags & UNINTERESTING))
333 revs->no_walk = 0;
334 if (revs->reflog_info && obj->type == OBJ_COMMIT) {
335 struct strbuf buf = STRBUF_INIT;
336 size_t namelen = strlen(name);
337 int len = repo_interpret_branch_name(the_repository, name,
338 namelen, &buf, &options);
340 if (0 < len && len < namelen && buf.len)
341 strbuf_addstr(&buf, name + len);
342 add_reflog_for_walk(revs->reflog_info,
343 (struct commit *)obj,
344 buf.buf[0] ? buf.buf: name);
345 strbuf_release(&buf);
346 return; /* do not add the commit itself */
348 add_object_array_with_path(obj, name, &revs->pending, mode, path);
351 static void add_pending_object_with_mode(struct rev_info *revs,
352 struct object *obj,
353 const char *name, unsigned mode)
355 add_pending_object_with_path(revs, obj, name, mode, NULL);
358 void add_pending_object(struct rev_info *revs,
359 struct object *obj, const char *name)
361 add_pending_object_with_mode(revs, obj, name, S_IFINVALID);
364 void add_head_to_pending(struct rev_info *revs)
366 struct object_id oid;
367 struct object *obj;
368 if (repo_get_oid(the_repository, "HEAD", &oid))
369 return;
370 obj = parse_object(revs->repo, &oid);
371 if (!obj)
372 return;
373 add_pending_object(revs, obj, "HEAD");
376 static struct object *get_reference(struct rev_info *revs, const char *name,
377 const struct object_id *oid,
378 unsigned int flags)
380 struct object *object;
382 object = parse_object_with_flags(revs->repo, oid,
383 revs->verify_objects ? 0 :
384 PARSE_OBJECT_SKIP_HASH_CHECK);
386 if (!object) {
387 if (revs->ignore_missing)
388 return object;
389 if (revs->exclude_promisor_objects && is_promisor_object(oid))
390 return NULL;
391 die("bad object %s", name);
393 object->flags |= flags;
394 return object;
397 void add_pending_oid(struct rev_info *revs, const char *name,
398 const struct object_id *oid, unsigned int flags)
400 struct object *object = get_reference(revs, name, oid, flags);
401 add_pending_object(revs, object, name);
404 static struct commit *handle_commit(struct rev_info *revs,
405 struct object_array_entry *entry)
407 struct object *object = entry->item;
408 const char *name = entry->name;
409 const char *path = entry->path;
410 unsigned int mode = entry->mode;
411 unsigned long flags = object->flags;
414 * Tag object? Look what it points to..
416 while (object->type == OBJ_TAG) {
417 struct tag *tag = (struct tag *) object;
418 if (revs->tag_objects && !(flags & UNINTERESTING))
419 add_pending_object(revs, object, tag->tag);
420 object = parse_object(revs->repo, get_tagged_oid(tag));
421 if (!object) {
422 if (revs->ignore_missing_links || (flags & UNINTERESTING))
423 return NULL;
424 if (revs->exclude_promisor_objects &&
425 is_promisor_object(&tag->tagged->oid))
426 return NULL;
427 die("bad object %s", oid_to_hex(&tag->tagged->oid));
429 object->flags |= flags;
431 * We'll handle the tagged object by looping or dropping
432 * through to the non-tag handlers below. Do not
433 * propagate path data from the tag's pending entry.
435 path = NULL;
436 mode = 0;
440 * Commit object? Just return it, we'll do all the complex
441 * reachability crud.
443 if (object->type == OBJ_COMMIT) {
444 struct commit *commit = (struct commit *)object;
446 if (repo_parse_commit(revs->repo, commit) < 0)
447 die("unable to parse commit %s", name);
448 if (flags & UNINTERESTING) {
449 mark_parents_uninteresting(revs, commit);
451 if (!revs->topo_order || !generation_numbers_enabled(the_repository))
452 revs->limited = 1;
454 if (revs->sources) {
455 char **slot = revision_sources_at(revs->sources, commit);
457 if (!*slot)
458 *slot = xstrdup(name);
460 return commit;
464 * Tree object? Either mark it uninteresting, or add it
465 * to the list of objects to look at later..
467 if (object->type == OBJ_TREE) {
468 struct tree *tree = (struct tree *)object;
469 if (!revs->tree_objects)
470 return NULL;
471 if (flags & UNINTERESTING) {
472 mark_tree_contents_uninteresting(revs->repo, tree);
473 return NULL;
475 add_pending_object_with_path(revs, object, name, mode, path);
476 return NULL;
480 * Blob object? You know the drill by now..
482 if (object->type == OBJ_BLOB) {
483 if (!revs->blob_objects)
484 return NULL;
485 if (flags & UNINTERESTING)
486 return NULL;
487 add_pending_object_with_path(revs, object, name, mode, path);
488 return NULL;
490 die("%s is unknown object", name);
493 static int everybody_uninteresting(struct commit_list *orig,
494 struct commit **interesting_cache)
496 struct commit_list *list = orig;
498 if (*interesting_cache) {
499 struct commit *commit = *interesting_cache;
500 if (!(commit->object.flags & UNINTERESTING))
501 return 0;
504 while (list) {
505 struct commit *commit = list->item;
506 list = list->next;
507 if (commit->object.flags & UNINTERESTING)
508 continue;
510 *interesting_cache = commit;
511 return 0;
513 return 1;
517 * A definition of "relevant" commit that we can use to simplify limited graphs
518 * by eliminating side branches.
520 * A "relevant" commit is one that is !UNINTERESTING (ie we are including it
521 * in our list), or that is a specified BOTTOM commit. Then after computing
522 * a limited list, during processing we can generally ignore boundary merges
523 * coming from outside the graph, (ie from irrelevant parents), and treat
524 * those merges as if they were single-parent. TREESAME is defined to consider
525 * only relevant parents, if any. If we are TREESAME to our on-graph parents,
526 * we don't care if we were !TREESAME to non-graph parents.
528 * Treating bottom commits as relevant ensures that a limited graph's
529 * connection to the actual bottom commit is not viewed as a side branch, but
530 * treated as part of the graph. For example:
532 * ....Z...A---X---o---o---B
533 * . /
534 * W---Y
536 * When computing "A..B", the A-X connection is at least as important as
537 * Y-X, despite A being flagged UNINTERESTING.
539 * And when computing --ancestry-path "A..B", the A-X connection is more
540 * important than Y-X, despite both A and Y being flagged UNINTERESTING.
542 static inline int relevant_commit(struct commit *commit)
544 return (commit->object.flags & (UNINTERESTING | BOTTOM)) != UNINTERESTING;
548 * Return a single relevant commit from a parent list. If we are a TREESAME
549 * commit, and this selects one of our parents, then we can safely simplify to
550 * that parent.
552 static struct commit *one_relevant_parent(const struct rev_info *revs,
553 struct commit_list *orig)
555 struct commit_list *list = orig;
556 struct commit *relevant = NULL;
558 if (!orig)
559 return NULL;
562 * For 1-parent commits, or if first-parent-only, then return that
563 * first parent (even if not "relevant" by the above definition).
564 * TREESAME will have been set purely on that parent.
566 if (revs->first_parent_only || !orig->next)
567 return orig->item;
570 * For multi-parent commits, identify a sole relevant parent, if any.
571 * If we have only one relevant parent, then TREESAME will be set purely
572 * with regard to that parent, and we can simplify accordingly.
574 * If we have more than one relevant parent, or no relevant parents
575 * (and multiple irrelevant ones), then we can't select a parent here
576 * and return NULL.
578 while (list) {
579 struct commit *commit = list->item;
580 list = list->next;
581 if (relevant_commit(commit)) {
582 if (relevant)
583 return NULL;
584 relevant = commit;
587 return relevant;
591 * The goal is to get REV_TREE_NEW as the result only if the
592 * diff consists of all '+' (and no other changes), REV_TREE_OLD
593 * if the whole diff is removal of old data, and otherwise
594 * REV_TREE_DIFFERENT (of course if the trees are the same we
595 * want REV_TREE_SAME).
597 * The only time we care about the distinction is when
598 * remove_empty_trees is in effect, in which case we care only about
599 * whether the whole change is REV_TREE_NEW, or if there's another type
600 * of change. Which means we can stop the diff early in either of these
601 * cases:
603 * 1. We're not using remove_empty_trees at all.
605 * 2. We saw anything except REV_TREE_NEW.
607 #define REV_TREE_SAME 0
608 #define REV_TREE_NEW 1 /* Only new files */
609 #define REV_TREE_OLD 2 /* Only files removed */
610 #define REV_TREE_DIFFERENT 3 /* Mixed changes */
611 static int tree_difference = REV_TREE_SAME;
613 static void file_add_remove(struct diff_options *options,
614 int addremove,
615 unsigned mode UNUSED,
616 const struct object_id *oid UNUSED,
617 int oid_valid UNUSED,
618 const char *fullpath UNUSED,
619 unsigned dirty_submodule UNUSED)
621 int diff = addremove == '+' ? REV_TREE_NEW : REV_TREE_OLD;
622 struct rev_info *revs = options->change_fn_data;
624 tree_difference |= diff;
625 if (!revs->remove_empty_trees || tree_difference != REV_TREE_NEW)
626 options->flags.has_changes = 1;
629 static void file_change(struct diff_options *options,
630 unsigned old_mode UNUSED,
631 unsigned new_mode UNUSED,
632 const struct object_id *old_oid UNUSED,
633 const struct object_id *new_oid UNUSED,
634 int old_oid_valid UNUSED,
635 int new_oid_valid UNUSED,
636 const char *fullpath UNUSED,
637 unsigned old_dirty_submodule UNUSED,
638 unsigned new_dirty_submodule UNUSED)
640 tree_difference = REV_TREE_DIFFERENT;
641 options->flags.has_changes = 1;
644 static int bloom_filter_atexit_registered;
645 static unsigned int count_bloom_filter_maybe;
646 static unsigned int count_bloom_filter_definitely_not;
647 static unsigned int count_bloom_filter_false_positive;
648 static unsigned int count_bloom_filter_not_present;
650 static void trace2_bloom_filter_statistics_atexit(void)
652 struct json_writer jw = JSON_WRITER_INIT;
654 jw_object_begin(&jw, 0);
655 jw_object_intmax(&jw, "filter_not_present", count_bloom_filter_not_present);
656 jw_object_intmax(&jw, "maybe", count_bloom_filter_maybe);
657 jw_object_intmax(&jw, "definitely_not", count_bloom_filter_definitely_not);
658 jw_object_intmax(&jw, "false_positive", count_bloom_filter_false_positive);
659 jw_end(&jw);
661 trace2_data_json("bloom", the_repository, "statistics", &jw);
663 jw_release(&jw);
666 static int forbid_bloom_filters(struct pathspec *spec)
668 if (spec->has_wildcard)
669 return 1;
670 if (spec->nr > 1)
671 return 1;
672 if (spec->magic & ~PATHSPEC_LITERAL)
673 return 1;
674 if (spec->nr && (spec->items[0].magic & ~PATHSPEC_LITERAL))
675 return 1;
677 return 0;
680 static void prepare_to_use_bloom_filter(struct rev_info *revs)
682 struct pathspec_item *pi;
683 char *path_alloc = NULL;
684 const char *path, *p;
685 size_t len;
686 int path_component_nr = 1;
688 if (!revs->commits)
689 return;
691 if (forbid_bloom_filters(&revs->prune_data))
692 return;
694 repo_parse_commit(revs->repo, revs->commits->item);
696 revs->bloom_filter_settings = get_bloom_filter_settings(revs->repo);
697 if (!revs->bloom_filter_settings)
698 return;
700 if (!revs->pruning.pathspec.nr)
701 return;
703 pi = &revs->pruning.pathspec.items[0];
705 /* remove single trailing slash from path, if needed */
706 if (pi->len > 0 && pi->match[pi->len - 1] == '/') {
707 path_alloc = xmemdupz(pi->match, pi->len - 1);
708 path = path_alloc;
709 } else
710 path = pi->match;
712 len = strlen(path);
713 if (!len) {
714 revs->bloom_filter_settings = NULL;
715 free(path_alloc);
716 return;
719 p = path;
720 while (*p) {
722 * At this point, the path is normalized to use Unix-style
723 * path separators. This is required due to how the
724 * changed-path Bloom filters store the paths.
726 if (*p == '/')
727 path_component_nr++;
728 p++;
731 revs->bloom_keys_nr = path_component_nr;
732 ALLOC_ARRAY(revs->bloom_keys, revs->bloom_keys_nr);
734 fill_bloom_key(path, len, &revs->bloom_keys[0],
735 revs->bloom_filter_settings);
736 path_component_nr = 1;
738 p = path + len - 1;
739 while (p > path) {
740 if (*p == '/')
741 fill_bloom_key(path, p - path,
742 &revs->bloom_keys[path_component_nr++],
743 revs->bloom_filter_settings);
744 p--;
747 if (trace2_is_enabled() && !bloom_filter_atexit_registered) {
748 atexit(trace2_bloom_filter_statistics_atexit);
749 bloom_filter_atexit_registered = 1;
752 free(path_alloc);
755 static int check_maybe_different_in_bloom_filter(struct rev_info *revs,
756 struct commit *commit)
758 struct bloom_filter *filter;
759 int result = 1, j;
761 if (!revs->repo->objects->commit_graph)
762 return -1;
764 if (commit_graph_generation(commit) == GENERATION_NUMBER_INFINITY)
765 return -1;
767 filter = get_bloom_filter(revs->repo, commit);
769 if (!filter) {
770 count_bloom_filter_not_present++;
771 return -1;
774 for (j = 0; result && j < revs->bloom_keys_nr; j++) {
775 result = bloom_filter_contains(filter,
776 &revs->bloom_keys[j],
777 revs->bloom_filter_settings);
780 if (result)
781 count_bloom_filter_maybe++;
782 else
783 count_bloom_filter_definitely_not++;
785 return result;
788 static int rev_compare_tree(struct rev_info *revs,
789 struct commit *parent, struct commit *commit, int nth_parent)
791 struct tree *t1 = repo_get_commit_tree(the_repository, parent);
792 struct tree *t2 = repo_get_commit_tree(the_repository, commit);
793 int bloom_ret = 1;
795 if (!t1)
796 return REV_TREE_NEW;
797 if (!t2)
798 return REV_TREE_OLD;
800 if (revs->simplify_by_decoration) {
802 * If we are simplifying by decoration, then the commit
803 * is worth showing if it has a tag pointing at it.
805 if (get_name_decoration(&commit->object))
806 return REV_TREE_DIFFERENT;
808 * A commit that is not pointed by a tag is uninteresting
809 * if we are not limited by path. This means that you will
810 * see the usual "commits that touch the paths" plus any
811 * tagged commit by specifying both --simplify-by-decoration
812 * and pathspec.
814 if (!revs->prune_data.nr)
815 return REV_TREE_SAME;
818 if (revs->bloom_keys_nr && !nth_parent) {
819 bloom_ret = check_maybe_different_in_bloom_filter(revs, commit);
821 if (bloom_ret == 0)
822 return REV_TREE_SAME;
825 tree_difference = REV_TREE_SAME;
826 revs->pruning.flags.has_changes = 0;
827 diff_tree_oid(&t1->object.oid, &t2->object.oid, "", &revs->pruning);
829 if (!nth_parent)
830 if (bloom_ret == 1 && tree_difference == REV_TREE_SAME)
831 count_bloom_filter_false_positive++;
833 return tree_difference;
836 static int rev_same_tree_as_empty(struct rev_info *revs, struct commit *commit)
838 struct tree *t1 = repo_get_commit_tree(the_repository, commit);
840 if (!t1)
841 return 0;
843 tree_difference = REV_TREE_SAME;
844 revs->pruning.flags.has_changes = 0;
845 diff_tree_oid(NULL, &t1->object.oid, "", &revs->pruning);
847 return tree_difference == REV_TREE_SAME;
850 struct treesame_state {
851 unsigned int nparents;
852 unsigned char treesame[FLEX_ARRAY];
855 static struct treesame_state *initialise_treesame(struct rev_info *revs, struct commit *commit)
857 unsigned n = commit_list_count(commit->parents);
858 struct treesame_state *st = xcalloc(1, st_add(sizeof(*st), n));
859 st->nparents = n;
860 add_decoration(&revs->treesame, &commit->object, st);
861 return st;
865 * Must be called immediately after removing the nth_parent from a commit's
866 * parent list, if we are maintaining the per-parent treesame[] decoration.
867 * This does not recalculate the master TREESAME flag - update_treesame()
868 * should be called to update it after a sequence of treesame[] modifications
869 * that may have affected it.
871 static int compact_treesame(struct rev_info *revs, struct commit *commit, unsigned nth_parent)
873 struct treesame_state *st;
874 int old_same;
876 if (!commit->parents) {
878 * Have just removed the only parent from a non-merge.
879 * Different handling, as we lack decoration.
881 if (nth_parent != 0)
882 die("compact_treesame %u", nth_parent);
883 old_same = !!(commit->object.flags & TREESAME);
884 if (rev_same_tree_as_empty(revs, commit))
885 commit->object.flags |= TREESAME;
886 else
887 commit->object.flags &= ~TREESAME;
888 return old_same;
891 st = lookup_decoration(&revs->treesame, &commit->object);
892 if (!st || nth_parent >= st->nparents)
893 die("compact_treesame %u", nth_parent);
895 old_same = st->treesame[nth_parent];
896 memmove(st->treesame + nth_parent,
897 st->treesame + nth_parent + 1,
898 st->nparents - nth_parent - 1);
901 * If we've just become a non-merge commit, update TREESAME
902 * immediately, and remove the no-longer-needed decoration.
903 * If still a merge, defer update until update_treesame().
905 if (--st->nparents == 1) {
906 if (commit->parents->next)
907 die("compact_treesame parents mismatch");
908 if (st->treesame[0] && revs->dense)
909 commit->object.flags |= TREESAME;
910 else
911 commit->object.flags &= ~TREESAME;
912 free(add_decoration(&revs->treesame, &commit->object, NULL));
915 return old_same;
918 static unsigned update_treesame(struct rev_info *revs, struct commit *commit)
920 if (commit->parents && commit->parents->next) {
921 unsigned n;
922 struct treesame_state *st;
923 struct commit_list *p;
924 unsigned relevant_parents;
925 unsigned relevant_change, irrelevant_change;
927 st = lookup_decoration(&revs->treesame, &commit->object);
928 if (!st)
929 die("update_treesame %s", oid_to_hex(&commit->object.oid));
930 relevant_parents = 0;
931 relevant_change = irrelevant_change = 0;
932 for (p = commit->parents, n = 0; p; n++, p = p->next) {
933 if (relevant_commit(p->item)) {
934 relevant_change |= !st->treesame[n];
935 relevant_parents++;
936 } else
937 irrelevant_change |= !st->treesame[n];
939 if (relevant_parents ? relevant_change : irrelevant_change)
940 commit->object.flags &= ~TREESAME;
941 else
942 commit->object.flags |= TREESAME;
945 return commit->object.flags & TREESAME;
948 static inline int limiting_can_increase_treesame(const struct rev_info *revs)
951 * TREESAME is irrelevant unless prune && dense;
952 * if simplify_history is set, we can't have a mixture of TREESAME and
953 * !TREESAME INTERESTING parents (and we don't have treesame[]
954 * decoration anyway);
955 * if first_parent_only is set, then the TREESAME flag is locked
956 * against the first parent (and again we lack treesame[] decoration).
958 return revs->prune && revs->dense &&
959 !revs->simplify_history &&
960 !revs->first_parent_only;
963 static void try_to_simplify_commit(struct rev_info *revs, struct commit *commit)
965 struct commit_list **pp, *parent;
966 struct treesame_state *ts = NULL;
967 int relevant_change = 0, irrelevant_change = 0;
968 int relevant_parents, nth_parent;
971 * If we don't do pruning, everything is interesting
973 if (!revs->prune)
974 return;
976 if (!repo_get_commit_tree(the_repository, commit))
977 return;
979 if (!commit->parents) {
980 if (rev_same_tree_as_empty(revs, commit))
981 commit->object.flags |= TREESAME;
982 return;
986 * Normal non-merge commit? If we don't want to make the
987 * history dense, we consider it always to be a change..
989 if (!revs->dense && !commit->parents->next)
990 return;
992 for (pp = &commit->parents, nth_parent = 0, relevant_parents = 0;
993 (parent = *pp) != NULL;
994 pp = &parent->next, nth_parent++) {
995 struct commit *p = parent->item;
996 if (relevant_commit(p))
997 relevant_parents++;
999 if (nth_parent == 1) {
1001 * This our second loop iteration - so we now know
1002 * we're dealing with a merge.
1004 * Do not compare with later parents when we care only about
1005 * the first parent chain, in order to avoid derailing the
1006 * traversal to follow a side branch that brought everything
1007 * in the path we are limited to by the pathspec.
1009 if (revs->first_parent_only)
1010 break;
1012 * If this will remain a potentially-simplifiable
1013 * merge, remember per-parent treesame if needed.
1014 * Initialise the array with the comparison from our
1015 * first iteration.
1017 if (revs->treesame.name &&
1018 !revs->simplify_history &&
1019 !(commit->object.flags & UNINTERESTING)) {
1020 ts = initialise_treesame(revs, commit);
1021 if (!(irrelevant_change || relevant_change))
1022 ts->treesame[0] = 1;
1025 if (repo_parse_commit(revs->repo, p) < 0)
1026 die("cannot simplify commit %s (because of %s)",
1027 oid_to_hex(&commit->object.oid),
1028 oid_to_hex(&p->object.oid));
1029 switch (rev_compare_tree(revs, p, commit, nth_parent)) {
1030 case REV_TREE_SAME:
1031 if (!revs->simplify_history || !relevant_commit(p)) {
1032 /* Even if a merge with an uninteresting
1033 * side branch brought the entire change
1034 * we are interested in, we do not want
1035 * to lose the other branches of this
1036 * merge, so we just keep going.
1038 if (ts)
1039 ts->treesame[nth_parent] = 1;
1040 continue;
1042 parent->next = NULL;
1043 commit->parents = parent;
1046 * A merge commit is a "diversion" if it is not
1047 * TREESAME to its first parent but is TREESAME
1048 * to a later parent. In the simplified history,
1049 * we "divert" the history walk to the later
1050 * parent. These commits are shown when "show_pulls"
1051 * is enabled, so do not mark the object as
1052 * TREESAME here.
1054 if (!revs->show_pulls || !nth_parent)
1055 commit->object.flags |= TREESAME;
1057 return;
1059 case REV_TREE_NEW:
1060 if (revs->remove_empty_trees &&
1061 rev_same_tree_as_empty(revs, p)) {
1062 /* We are adding all the specified
1063 * paths from this parent, so the
1064 * history beyond this parent is not
1065 * interesting. Remove its parents
1066 * (they are grandparents for us).
1067 * IOW, we pretend this parent is a
1068 * "root" commit.
1070 if (repo_parse_commit(revs->repo, p) < 0)
1071 die("cannot simplify commit %s (invalid %s)",
1072 oid_to_hex(&commit->object.oid),
1073 oid_to_hex(&p->object.oid));
1074 p->parents = NULL;
1076 /* fallthrough */
1077 case REV_TREE_OLD:
1078 case REV_TREE_DIFFERENT:
1079 if (relevant_commit(p))
1080 relevant_change = 1;
1081 else
1082 irrelevant_change = 1;
1084 if (!nth_parent)
1085 commit->object.flags |= PULL_MERGE;
1087 continue;
1089 die("bad tree compare for commit %s", oid_to_hex(&commit->object.oid));
1093 * TREESAME is straightforward for single-parent commits. For merge
1094 * commits, it is most useful to define it so that "irrelevant"
1095 * parents cannot make us !TREESAME - if we have any relevant
1096 * parents, then we only consider TREESAMEness with respect to them,
1097 * allowing irrelevant merges from uninteresting branches to be
1098 * simplified away. Only if we have only irrelevant parents do we
1099 * base TREESAME on them. Note that this logic is replicated in
1100 * update_treesame, which should be kept in sync.
1102 if (relevant_parents ? !relevant_change : !irrelevant_change)
1103 commit->object.flags |= TREESAME;
1106 static int process_parents(struct rev_info *revs, struct commit *commit,
1107 struct commit_list **list, struct prio_queue *queue)
1109 struct commit_list *parent = commit->parents;
1110 unsigned pass_flags;
1112 if (commit->object.flags & ADDED)
1113 return 0;
1114 if (revs->do_not_die_on_missing_objects &&
1115 oidset_contains(&revs->missing_commits, &commit->object.oid))
1116 return 0;
1117 commit->object.flags |= ADDED;
1119 if (revs->include_check &&
1120 !revs->include_check(commit, revs->include_check_data))
1121 return 0;
1124 * If the commit is uninteresting, don't try to
1125 * prune parents - we want the maximal uninteresting
1126 * set.
1128 * Normally we haven't parsed the parent
1129 * yet, so we won't have a parent of a parent
1130 * here. However, it may turn out that we've
1131 * reached this commit some other way (where it
1132 * wasn't uninteresting), in which case we need
1133 * to mark its parents recursively too..
1135 if (commit->object.flags & UNINTERESTING) {
1136 while (parent) {
1137 struct commit *p = parent->item;
1138 parent = parent->next;
1139 if (p)
1140 p->object.flags |= UNINTERESTING;
1141 if (repo_parse_commit_gently(revs->repo, p, 1) < 0)
1142 continue;
1143 if (p->parents)
1144 mark_parents_uninteresting(revs, p);
1145 if (p->object.flags & SEEN)
1146 continue;
1147 p->object.flags |= (SEEN | NOT_USER_GIVEN);
1148 if (list)
1149 commit_list_insert_by_date(p, list);
1150 if (queue)
1151 prio_queue_put(queue, p);
1152 if (revs->exclude_first_parent_only)
1153 break;
1155 return 0;
1159 * Ok, the commit wasn't uninteresting. Try to
1160 * simplify the commit history and find the parent
1161 * that has no differences in the path set if one exists.
1163 try_to_simplify_commit(revs, commit);
1165 if (revs->no_walk)
1166 return 0;
1168 pass_flags = (commit->object.flags & (SYMMETRIC_LEFT | ANCESTRY_PATH));
1170 for (parent = commit->parents; parent; parent = parent->next) {
1171 struct commit *p = parent->item;
1172 int gently = revs->ignore_missing_links ||
1173 revs->exclude_promisor_objects ||
1174 revs->do_not_die_on_missing_objects;
1175 if (repo_parse_commit_gently(revs->repo, p, gently) < 0) {
1176 if (revs->exclude_promisor_objects &&
1177 is_promisor_object(&p->object.oid)) {
1178 if (revs->first_parent_only)
1179 break;
1180 continue;
1183 if (revs->do_not_die_on_missing_objects)
1184 oidset_insert(&revs->missing_commits, &p->object.oid);
1185 else
1186 return -1; /* corrupt repository */
1188 if (revs->sources) {
1189 char **slot = revision_sources_at(revs->sources, p);
1191 if (!*slot)
1192 *slot = *revision_sources_at(revs->sources, commit);
1194 p->object.flags |= pass_flags;
1195 if (!(p->object.flags & SEEN)) {
1196 p->object.flags |= (SEEN | NOT_USER_GIVEN);
1197 if (list)
1198 commit_list_insert_by_date(p, list);
1199 if (queue)
1200 prio_queue_put(queue, p);
1202 if (revs->first_parent_only)
1203 break;
1205 return 0;
1208 static void cherry_pick_list(struct commit_list *list, struct rev_info *revs)
1210 struct commit_list *p;
1211 int left_count = 0, right_count = 0;
1212 int left_first;
1213 struct patch_ids ids;
1214 unsigned cherry_flag;
1216 /* First count the commits on the left and on the right */
1217 for (p = list; p; p = p->next) {
1218 struct commit *commit = p->item;
1219 unsigned flags = commit->object.flags;
1220 if (flags & BOUNDARY)
1222 else if (flags & SYMMETRIC_LEFT)
1223 left_count++;
1224 else
1225 right_count++;
1228 if (!left_count || !right_count)
1229 return;
1231 left_first = left_count < right_count;
1232 init_patch_ids(revs->repo, &ids);
1233 ids.diffopts.pathspec = revs->diffopt.pathspec;
1235 /* Compute patch-ids for one side */
1236 for (p = list; p; p = p->next) {
1237 struct commit *commit = p->item;
1238 unsigned flags = commit->object.flags;
1240 if (flags & BOUNDARY)
1241 continue;
1243 * If we have fewer left, left_first is set and we omit
1244 * commits on the right branch in this loop. If we have
1245 * fewer right, we skip the left ones.
1247 if (left_first != !!(flags & SYMMETRIC_LEFT))
1248 continue;
1249 add_commit_patch_id(commit, &ids);
1252 /* either cherry_mark or cherry_pick are true */
1253 cherry_flag = revs->cherry_mark ? PATCHSAME : SHOWN;
1255 /* Check the other side */
1256 for (p = list; p; p = p->next) {
1257 struct commit *commit = p->item;
1258 struct patch_id *id;
1259 unsigned flags = commit->object.flags;
1261 if (flags & BOUNDARY)
1262 continue;
1264 * If we have fewer left, left_first is set and we omit
1265 * commits on the left branch in this loop.
1267 if (left_first == !!(flags & SYMMETRIC_LEFT))
1268 continue;
1271 * Have we seen the same patch id?
1273 id = patch_id_iter_first(commit, &ids);
1274 if (!id)
1275 continue;
1277 commit->object.flags |= cherry_flag;
1278 do {
1279 id->commit->object.flags |= cherry_flag;
1280 } while ((id = patch_id_iter_next(id, &ids)));
1283 free_patch_ids(&ids);
1286 /* How many extra uninteresting commits we want to see.. */
1287 #define SLOP 5
1289 static int still_interesting(struct commit_list *src, timestamp_t date, int slop,
1290 struct commit **interesting_cache)
1293 * No source list at all? We're definitely done..
1295 if (!src)
1296 return 0;
1299 * Does the destination list contain entries with a date
1300 * before the source list? Definitely _not_ done.
1302 if (date <= src->item->date)
1303 return SLOP;
1306 * Does the source list still have interesting commits in
1307 * it? Definitely not done..
1309 if (!everybody_uninteresting(src, interesting_cache))
1310 return SLOP;
1312 /* Ok, we're closing in.. */
1313 return slop-1;
1317 * "rev-list --ancestry-path=C_0 [--ancestry-path=C_1 ...] A..B"
1318 * computes commits that are ancestors of B but not ancestors of A but
1319 * further limits the result to those that have any of C in their
1320 * ancestry path (i.e. are either ancestors of any of C, descendants
1321 * of any of C, or are any of C). If --ancestry-path is specified with
1322 * no commit, we use all bottom commits for C.
1324 * Before this function is called, ancestors of C will have already
1325 * been marked with ANCESTRY_PATH previously.
1327 * This takes the list of bottom commits and the result of "A..B"
1328 * without --ancestry-path, and limits the latter further to the ones
1329 * that have any of C in their ancestry path. Since the ancestors of C
1330 * have already been marked (a prerequisite of this function), we just
1331 * need to mark the descendants, then exclude any commit that does not
1332 * have any of these marks.
1334 static void limit_to_ancestry(struct commit_list *bottoms, struct commit_list *list)
1336 struct commit_list *p;
1337 struct commit_list *rlist = NULL;
1338 int made_progress;
1341 * Reverse the list so that it will be likely that we would
1342 * process parents before children.
1344 for (p = list; p; p = p->next)
1345 commit_list_insert(p->item, &rlist);
1347 for (p = bottoms; p; p = p->next)
1348 p->item->object.flags |= TMP_MARK;
1351 * Mark the ones that can reach bottom commits in "list",
1352 * in a bottom-up fashion.
1354 do {
1355 made_progress = 0;
1356 for (p = rlist; p; p = p->next) {
1357 struct commit *c = p->item;
1358 struct commit_list *parents;
1359 if (c->object.flags & (TMP_MARK | UNINTERESTING))
1360 continue;
1361 for (parents = c->parents;
1362 parents;
1363 parents = parents->next) {
1364 if (!(parents->item->object.flags & TMP_MARK))
1365 continue;
1366 c->object.flags |= TMP_MARK;
1367 made_progress = 1;
1368 break;
1371 } while (made_progress);
1374 * NEEDSWORK: decide if we want to remove parents that are
1375 * not marked with TMP_MARK from commit->parents for commits
1376 * in the resulting list. We may not want to do that, though.
1380 * The ones that are not marked with either TMP_MARK or
1381 * ANCESTRY_PATH are uninteresting
1383 for (p = list; p; p = p->next) {
1384 struct commit *c = p->item;
1385 if (c->object.flags & (TMP_MARK | ANCESTRY_PATH))
1386 continue;
1387 c->object.flags |= UNINTERESTING;
1390 /* We are done with TMP_MARK and ANCESTRY_PATH */
1391 for (p = list; p; p = p->next)
1392 p->item->object.flags &= ~(TMP_MARK | ANCESTRY_PATH);
1393 for (p = bottoms; p; p = p->next)
1394 p->item->object.flags &= ~(TMP_MARK | ANCESTRY_PATH);
1395 free_commit_list(rlist);
1399 * Before walking the history, add the set of "negative" refs the
1400 * caller has asked to exclude to the bottom list.
1402 * This is used to compute "rev-list --ancestry-path A..B", as we need
1403 * to filter the result of "A..B" further to the ones that can actually
1404 * reach A.
1406 static void collect_bottom_commits(struct commit_list *list,
1407 struct commit_list **bottom)
1409 struct commit_list *elem;
1410 for (elem = list; elem; elem = elem->next)
1411 if (elem->item->object.flags & BOTTOM)
1412 commit_list_insert(elem->item, bottom);
1415 /* Assumes either left_only or right_only is set */
1416 static void limit_left_right(struct commit_list *list, struct rev_info *revs)
1418 struct commit_list *p;
1420 for (p = list; p; p = p->next) {
1421 struct commit *commit = p->item;
1423 if (revs->right_only) {
1424 if (commit->object.flags & SYMMETRIC_LEFT)
1425 commit->object.flags |= SHOWN;
1426 } else /* revs->left_only is set */
1427 if (!(commit->object.flags & SYMMETRIC_LEFT))
1428 commit->object.flags |= SHOWN;
1432 static int limit_list(struct rev_info *revs)
1434 int slop = SLOP;
1435 timestamp_t date = TIME_MAX;
1436 struct commit_list *original_list = revs->commits;
1437 struct commit_list *newlist = NULL;
1438 struct commit_list **p = &newlist;
1439 struct commit *interesting_cache = NULL;
1441 if (revs->ancestry_path_implicit_bottoms) {
1442 collect_bottom_commits(original_list,
1443 &revs->ancestry_path_bottoms);
1444 if (!revs->ancestry_path_bottoms)
1445 die("--ancestry-path given but there are no bottom commits");
1448 while (original_list) {
1449 struct commit *commit = pop_commit(&original_list);
1450 struct object *obj = &commit->object;
1451 show_early_output_fn_t show;
1453 if (commit == interesting_cache)
1454 interesting_cache = NULL;
1456 if (revs->max_age != -1 && (commit->date < revs->max_age))
1457 obj->flags |= UNINTERESTING;
1458 if (process_parents(revs, commit, &original_list, NULL) < 0)
1459 return -1;
1460 if (obj->flags & UNINTERESTING) {
1461 mark_parents_uninteresting(revs, commit);
1462 slop = still_interesting(original_list, date, slop, &interesting_cache);
1463 if (slop)
1464 continue;
1465 break;
1467 if (revs->min_age != -1 && (commit->date > revs->min_age) &&
1468 !revs->line_level_traverse)
1469 continue;
1470 if (revs->max_age_as_filter != -1 &&
1471 (commit->date < revs->max_age_as_filter) && !revs->line_level_traverse)
1472 continue;
1473 date = commit->date;
1474 p = &commit_list_insert(commit, p)->next;
1476 show = show_early_output;
1477 if (!show)
1478 continue;
1480 show(revs, newlist);
1481 show_early_output = NULL;
1483 if (revs->cherry_pick || revs->cherry_mark)
1484 cherry_pick_list(newlist, revs);
1486 if (revs->left_only || revs->right_only)
1487 limit_left_right(newlist, revs);
1489 if (revs->ancestry_path)
1490 limit_to_ancestry(revs->ancestry_path_bottoms, newlist);
1493 * Check if any commits have become TREESAME by some of their parents
1494 * becoming UNINTERESTING.
1496 if (limiting_can_increase_treesame(revs)) {
1497 struct commit_list *list = NULL;
1498 for (list = newlist; list; list = list->next) {
1499 struct commit *c = list->item;
1500 if (c->object.flags & (UNINTERESTING | TREESAME))
1501 continue;
1502 update_treesame(revs, c);
1506 free_commit_list(original_list);
1507 revs->commits = newlist;
1508 return 0;
1512 * Add an entry to refs->cmdline with the specified information.
1513 * *name is copied.
1515 static void add_rev_cmdline(struct rev_info *revs,
1516 struct object *item,
1517 const char *name,
1518 int whence,
1519 unsigned flags)
1521 struct rev_cmdline_info *info = &revs->cmdline;
1522 unsigned int nr = info->nr;
1524 ALLOC_GROW(info->rev, nr + 1, info->alloc);
1525 info->rev[nr].item = item;
1526 info->rev[nr].name = xstrdup(name);
1527 info->rev[nr].whence = whence;
1528 info->rev[nr].flags = flags;
1529 info->nr++;
1532 static void add_rev_cmdline_list(struct rev_info *revs,
1533 struct commit_list *commit_list,
1534 int whence,
1535 unsigned flags)
1537 while (commit_list) {
1538 struct object *object = &commit_list->item->object;
1539 add_rev_cmdline(revs, object, oid_to_hex(&object->oid),
1540 whence, flags);
1541 commit_list = commit_list->next;
1545 int ref_excluded(const struct ref_exclusions *exclusions, const char *path)
1547 const char *stripped_path = strip_namespace(path);
1548 struct string_list_item *item;
1550 for_each_string_list_item(item, &exclusions->excluded_refs) {
1551 if (!wildmatch(item->string, path, 0))
1552 return 1;
1555 if (ref_is_hidden(stripped_path, path, &exclusions->hidden_refs))
1556 return 1;
1558 return 0;
1561 void init_ref_exclusions(struct ref_exclusions *exclusions)
1563 struct ref_exclusions blank = REF_EXCLUSIONS_INIT;
1564 memcpy(exclusions, &blank, sizeof(*exclusions));
1567 void clear_ref_exclusions(struct ref_exclusions *exclusions)
1569 string_list_clear(&exclusions->excluded_refs, 0);
1570 strvec_clear(&exclusions->hidden_refs);
1571 exclusions->hidden_refs_configured = 0;
1574 void add_ref_exclusion(struct ref_exclusions *exclusions, const char *exclude)
1576 string_list_append(&exclusions->excluded_refs, exclude);
1579 struct exclude_hidden_refs_cb {
1580 struct ref_exclusions *exclusions;
1581 const char *section;
1584 static int hide_refs_config(const char *var, const char *value,
1585 const struct config_context *ctx UNUSED,
1586 void *cb_data)
1588 struct exclude_hidden_refs_cb *cb = cb_data;
1589 cb->exclusions->hidden_refs_configured = 1;
1590 return parse_hide_refs_config(var, value, cb->section,
1591 &cb->exclusions->hidden_refs);
1594 void exclude_hidden_refs(struct ref_exclusions *exclusions, const char *section)
1596 struct exclude_hidden_refs_cb cb;
1598 if (strcmp(section, "fetch") && strcmp(section, "receive") &&
1599 strcmp(section, "uploadpack"))
1600 die(_("unsupported section for hidden refs: %s"), section);
1602 if (exclusions->hidden_refs_configured)
1603 die(_("--exclude-hidden= passed more than once"));
1605 cb.exclusions = exclusions;
1606 cb.section = section;
1608 git_config(hide_refs_config, &cb);
1611 struct all_refs_cb {
1612 int all_flags;
1613 int warned_bad_reflog;
1614 struct rev_info *all_revs;
1615 const char *name_for_errormsg;
1616 struct worktree *wt;
1619 static int handle_one_ref(const char *path, const struct object_id *oid,
1620 int flag UNUSED,
1621 void *cb_data)
1623 struct all_refs_cb *cb = cb_data;
1624 struct object *object;
1626 if (ref_excluded(&cb->all_revs->ref_excludes, path))
1627 return 0;
1629 object = get_reference(cb->all_revs, path, oid, cb->all_flags);
1630 add_rev_cmdline(cb->all_revs, object, path, REV_CMD_REF, cb->all_flags);
1631 add_pending_object(cb->all_revs, object, path);
1632 return 0;
1635 static void init_all_refs_cb(struct all_refs_cb *cb, struct rev_info *revs,
1636 unsigned flags)
1638 cb->all_revs = revs;
1639 cb->all_flags = flags;
1640 revs->rev_input_given = 1;
1641 cb->wt = NULL;
1644 static void handle_refs(struct ref_store *refs,
1645 struct rev_info *revs, unsigned flags,
1646 int (*for_each)(struct ref_store *, each_ref_fn, void *))
1648 struct all_refs_cb cb;
1650 if (!refs) {
1651 /* this could happen with uninitialized submodules */
1652 return;
1655 init_all_refs_cb(&cb, revs, flags);
1656 for_each(refs, handle_one_ref, &cb);
1659 static void handle_one_reflog_commit(struct object_id *oid, void *cb_data)
1661 struct all_refs_cb *cb = cb_data;
1662 if (!is_null_oid(oid)) {
1663 struct object *o = parse_object(cb->all_revs->repo, oid);
1664 if (o) {
1665 o->flags |= cb->all_flags;
1666 /* ??? CMDLINEFLAGS ??? */
1667 add_pending_object(cb->all_revs, o, "");
1669 else if (!cb->warned_bad_reflog) {
1670 warning("reflog of '%s' references pruned commits",
1671 cb->name_for_errormsg);
1672 cb->warned_bad_reflog = 1;
1677 static int handle_one_reflog_ent(struct object_id *ooid, struct object_id *noid,
1678 const char *email UNUSED,
1679 timestamp_t timestamp UNUSED,
1680 int tz UNUSED,
1681 const char *message UNUSED,
1682 void *cb_data)
1684 handle_one_reflog_commit(ooid, cb_data);
1685 handle_one_reflog_commit(noid, cb_data);
1686 return 0;
1689 static int handle_one_reflog(const char *refname_in_wt,
1690 const struct object_id *oid UNUSED,
1691 int flag UNUSED, void *cb_data)
1693 struct all_refs_cb *cb = cb_data;
1694 struct strbuf refname = STRBUF_INIT;
1696 cb->warned_bad_reflog = 0;
1697 strbuf_worktree_ref(cb->wt, &refname, refname_in_wt);
1698 cb->name_for_errormsg = refname.buf;
1699 refs_for_each_reflog_ent(get_main_ref_store(the_repository),
1700 refname.buf,
1701 handle_one_reflog_ent, cb_data);
1702 strbuf_release(&refname);
1703 return 0;
1706 static void add_other_reflogs_to_pending(struct all_refs_cb *cb)
1708 struct worktree **worktrees, **p;
1710 worktrees = get_worktrees();
1711 for (p = worktrees; *p; p++) {
1712 struct worktree *wt = *p;
1714 if (wt->is_current)
1715 continue;
1717 cb->wt = wt;
1718 refs_for_each_reflog(get_worktree_ref_store(wt),
1719 handle_one_reflog,
1720 cb);
1722 free_worktrees(worktrees);
1725 void add_reflogs_to_pending(struct rev_info *revs, unsigned flags)
1727 struct all_refs_cb cb;
1729 cb.all_revs = revs;
1730 cb.all_flags = flags;
1731 cb.wt = NULL;
1732 for_each_reflog(handle_one_reflog, &cb);
1734 if (!revs->single_worktree)
1735 add_other_reflogs_to_pending(&cb);
1738 static void add_cache_tree(struct cache_tree *it, struct rev_info *revs,
1739 struct strbuf *path, unsigned int flags)
1741 size_t baselen = path->len;
1742 int i;
1744 if (it->entry_count >= 0) {
1745 struct tree *tree = lookup_tree(revs->repo, &it->oid);
1746 tree->object.flags |= flags;
1747 add_pending_object_with_path(revs, &tree->object, "",
1748 040000, path->buf);
1751 for (i = 0; i < it->subtree_nr; i++) {
1752 struct cache_tree_sub *sub = it->down[i];
1753 strbuf_addf(path, "%s%s", baselen ? "/" : "", sub->name);
1754 add_cache_tree(sub->cache_tree, revs, path, flags);
1755 strbuf_setlen(path, baselen);
1760 static void add_resolve_undo_to_pending(struct index_state *istate, struct rev_info *revs)
1762 struct string_list_item *item;
1763 struct string_list *resolve_undo = istate->resolve_undo;
1765 if (!resolve_undo)
1766 return;
1768 for_each_string_list_item(item, resolve_undo) {
1769 const char *path = item->string;
1770 struct resolve_undo_info *ru = item->util;
1771 int i;
1773 if (!ru)
1774 continue;
1775 for (i = 0; i < 3; i++) {
1776 struct blob *blob;
1778 if (!ru->mode[i] || !S_ISREG(ru->mode[i]))
1779 continue;
1781 blob = lookup_blob(revs->repo, &ru->oid[i]);
1782 if (!blob) {
1783 warning(_("resolve-undo records `%s` which is missing"),
1784 oid_to_hex(&ru->oid[i]));
1785 continue;
1787 add_pending_object_with_path(revs, &blob->object, "",
1788 ru->mode[i], path);
1793 static void do_add_index_objects_to_pending(struct rev_info *revs,
1794 struct index_state *istate,
1795 unsigned int flags)
1797 int i;
1799 /* TODO: audit for interaction with sparse-index. */
1800 ensure_full_index(istate);
1801 for (i = 0; i < istate->cache_nr; i++) {
1802 struct cache_entry *ce = istate->cache[i];
1803 struct blob *blob;
1805 if (S_ISGITLINK(ce->ce_mode))
1806 continue;
1808 blob = lookup_blob(revs->repo, &ce->oid);
1809 if (!blob)
1810 die("unable to add index blob to traversal");
1811 blob->object.flags |= flags;
1812 add_pending_object_with_path(revs, &blob->object, "",
1813 ce->ce_mode, ce->name);
1816 if (istate->cache_tree) {
1817 struct strbuf path = STRBUF_INIT;
1818 add_cache_tree(istate->cache_tree, revs, &path, flags);
1819 strbuf_release(&path);
1822 add_resolve_undo_to_pending(istate, revs);
1825 void add_index_objects_to_pending(struct rev_info *revs, unsigned int flags)
1827 struct worktree **worktrees, **p;
1829 repo_read_index(revs->repo);
1830 do_add_index_objects_to_pending(revs, revs->repo->index, flags);
1832 if (revs->single_worktree)
1833 return;
1835 worktrees = get_worktrees();
1836 for (p = worktrees; *p; p++) {
1837 struct worktree *wt = *p;
1838 struct index_state istate = INDEX_STATE_INIT(revs->repo);
1840 if (wt->is_current)
1841 continue; /* current index already taken care of */
1843 if (read_index_from(&istate,
1844 worktree_git_path(wt, "index"),
1845 get_worktree_git_dir(wt)) > 0)
1846 do_add_index_objects_to_pending(revs, &istate, flags);
1847 discard_index(&istate);
1849 free_worktrees(worktrees);
1852 struct add_alternate_refs_data {
1853 struct rev_info *revs;
1854 unsigned int flags;
1857 static void add_one_alternate_ref(const struct object_id *oid,
1858 void *vdata)
1860 const char *name = ".alternate";
1861 struct add_alternate_refs_data *data = vdata;
1862 struct object *obj;
1864 obj = get_reference(data->revs, name, oid, data->flags);
1865 add_rev_cmdline(data->revs, obj, name, REV_CMD_REV, data->flags);
1866 add_pending_object(data->revs, obj, name);
1869 static void add_alternate_refs_to_pending(struct rev_info *revs,
1870 unsigned int flags)
1872 struct add_alternate_refs_data data;
1873 data.revs = revs;
1874 data.flags = flags;
1875 for_each_alternate_ref(add_one_alternate_ref, &data);
1878 static int add_parents_only(struct rev_info *revs, const char *arg_, int flags,
1879 int exclude_parent)
1881 struct object_id oid;
1882 struct object *it;
1883 struct commit *commit;
1884 struct commit_list *parents;
1885 int parent_number;
1886 const char *arg = arg_;
1888 if (*arg == '^') {
1889 flags ^= UNINTERESTING | BOTTOM;
1890 arg++;
1892 if (repo_get_oid_committish(the_repository, arg, &oid))
1893 return 0;
1894 while (1) {
1895 it = get_reference(revs, arg, &oid, 0);
1896 if (!it && revs->ignore_missing)
1897 return 0;
1898 if (it->type != OBJ_TAG)
1899 break;
1900 if (!((struct tag*)it)->tagged)
1901 return 0;
1902 oidcpy(&oid, &((struct tag*)it)->tagged->oid);
1904 if (it->type != OBJ_COMMIT)
1905 return 0;
1906 commit = (struct commit *)it;
1907 if (exclude_parent &&
1908 exclude_parent > commit_list_count(commit->parents))
1909 return 0;
1910 for (parents = commit->parents, parent_number = 1;
1911 parents;
1912 parents = parents->next, parent_number++) {
1913 if (exclude_parent && parent_number != exclude_parent)
1914 continue;
1916 it = &parents->item->object;
1917 it->flags |= flags;
1918 add_rev_cmdline(revs, it, arg_, REV_CMD_PARENTS_ONLY, flags);
1919 add_pending_object(revs, it, arg);
1921 return 1;
1924 void repo_init_revisions(struct repository *r,
1925 struct rev_info *revs,
1926 const char *prefix)
1928 struct rev_info blank = REV_INFO_INIT;
1929 memcpy(revs, &blank, sizeof(*revs));
1931 revs->repo = r;
1932 revs->pruning.repo = r;
1933 revs->pruning.add_remove = file_add_remove;
1934 revs->pruning.change = file_change;
1935 revs->pruning.change_fn_data = revs;
1936 revs->prefix = prefix;
1938 grep_init(&revs->grep_filter, revs->repo);
1939 revs->grep_filter.status_only = 1;
1941 repo_diff_setup(revs->repo, &revs->diffopt);
1942 if (prefix && !revs->diffopt.prefix) {
1943 revs->diffopt.prefix = prefix;
1944 revs->diffopt.prefix_length = strlen(prefix);
1947 init_display_notes(&revs->notes_opt);
1948 list_objects_filter_init(&revs->filter);
1949 init_ref_exclusions(&revs->ref_excludes);
1952 static void add_pending_commit_list(struct rev_info *revs,
1953 struct commit_list *commit_list,
1954 unsigned int flags)
1956 while (commit_list) {
1957 struct object *object = &commit_list->item->object;
1958 object->flags |= flags;
1959 add_pending_object(revs, object, oid_to_hex(&object->oid));
1960 commit_list = commit_list->next;
1964 static void prepare_show_merge(struct rev_info *revs)
1966 struct commit_list *bases = NULL;
1967 struct commit *head, *other;
1968 struct object_id oid;
1969 const char **prune = NULL;
1970 int i, prune_num = 1; /* counting terminating NULL */
1971 struct index_state *istate = revs->repo->index;
1973 if (repo_get_oid(the_repository, "HEAD", &oid))
1974 die("--merge without HEAD?");
1975 head = lookup_commit_or_die(&oid, "HEAD");
1976 if (repo_get_oid(the_repository, "MERGE_HEAD", &oid))
1977 die("--merge without MERGE_HEAD?");
1978 other = lookup_commit_or_die(&oid, "MERGE_HEAD");
1979 add_pending_object(revs, &head->object, "HEAD");
1980 add_pending_object(revs, &other->object, "MERGE_HEAD");
1981 if (repo_get_merge_bases(the_repository, head, other, &bases) < 0)
1982 exit(128);
1983 add_rev_cmdline_list(revs, bases, REV_CMD_MERGE_BASE, UNINTERESTING | BOTTOM);
1984 add_pending_commit_list(revs, bases, UNINTERESTING | BOTTOM);
1985 free_commit_list(bases);
1986 head->object.flags |= SYMMETRIC_LEFT;
1988 if (!istate->cache_nr)
1989 repo_read_index(revs->repo);
1990 for (i = 0; i < istate->cache_nr; i++) {
1991 const struct cache_entry *ce = istate->cache[i];
1992 if (!ce_stage(ce))
1993 continue;
1994 if (ce_path_match(istate, ce, &revs->prune_data, NULL)) {
1995 prune_num++;
1996 REALLOC_ARRAY(prune, prune_num);
1997 prune[prune_num-2] = ce->name;
1998 prune[prune_num-1] = NULL;
2000 while ((i+1 < istate->cache_nr) &&
2001 ce_same_name(ce, istate->cache[i+1]))
2002 i++;
2004 clear_pathspec(&revs->prune_data);
2005 parse_pathspec(&revs->prune_data, PATHSPEC_ALL_MAGIC & ~PATHSPEC_LITERAL,
2006 PATHSPEC_PREFER_FULL | PATHSPEC_LITERAL_PATH, "", prune);
2007 revs->limited = 1;
2010 static int dotdot_missing(const char *arg, char *dotdot,
2011 struct rev_info *revs, int symmetric)
2013 if (revs->ignore_missing)
2014 return 0;
2015 /* de-munge so we report the full argument */
2016 *dotdot = '.';
2017 die(symmetric
2018 ? "Invalid symmetric difference expression %s"
2019 : "Invalid revision range %s", arg);
2022 static int handle_dotdot_1(const char *arg, char *dotdot,
2023 struct rev_info *revs, int flags,
2024 int cant_be_filename,
2025 struct object_context *a_oc,
2026 struct object_context *b_oc)
2028 const char *a_name, *b_name;
2029 struct object_id a_oid, b_oid;
2030 struct object *a_obj, *b_obj;
2031 unsigned int a_flags, b_flags;
2032 int symmetric = 0;
2033 unsigned int flags_exclude = flags ^ (UNINTERESTING | BOTTOM);
2034 unsigned int oc_flags = GET_OID_COMMITTISH | GET_OID_RECORD_PATH;
2036 a_name = arg;
2037 if (!*a_name)
2038 a_name = "HEAD";
2040 b_name = dotdot + 2;
2041 if (*b_name == '.') {
2042 symmetric = 1;
2043 b_name++;
2045 if (!*b_name)
2046 b_name = "HEAD";
2048 if (get_oid_with_context(revs->repo, a_name, oc_flags, &a_oid, a_oc) ||
2049 get_oid_with_context(revs->repo, b_name, oc_flags, &b_oid, b_oc))
2050 return -1;
2052 if (!cant_be_filename) {
2053 *dotdot = '.';
2054 verify_non_filename(revs->prefix, arg);
2055 *dotdot = '\0';
2058 a_obj = parse_object(revs->repo, &a_oid);
2059 b_obj = parse_object(revs->repo, &b_oid);
2060 if (!a_obj || !b_obj)
2061 return dotdot_missing(arg, dotdot, revs, symmetric);
2063 if (!symmetric) {
2064 /* just A..B */
2065 b_flags = flags;
2066 a_flags = flags_exclude;
2067 } else {
2068 /* A...B -- find merge bases between the two */
2069 struct commit *a, *b;
2070 struct commit_list *exclude = NULL;
2072 a = lookup_commit_reference(revs->repo, &a_obj->oid);
2073 b = lookup_commit_reference(revs->repo, &b_obj->oid);
2074 if (!a || !b)
2075 return dotdot_missing(arg, dotdot, revs, symmetric);
2077 if (repo_get_merge_bases(the_repository, a, b, &exclude) < 0) {
2078 free_commit_list(exclude);
2079 return -1;
2081 add_rev_cmdline_list(revs, exclude, REV_CMD_MERGE_BASE,
2082 flags_exclude);
2083 add_pending_commit_list(revs, exclude, flags_exclude);
2084 free_commit_list(exclude);
2086 b_flags = flags;
2087 a_flags = flags | SYMMETRIC_LEFT;
2090 a_obj->flags |= a_flags;
2091 b_obj->flags |= b_flags;
2092 add_rev_cmdline(revs, a_obj, a_name, REV_CMD_LEFT, a_flags);
2093 add_rev_cmdline(revs, b_obj, b_name, REV_CMD_RIGHT, b_flags);
2094 add_pending_object_with_path(revs, a_obj, a_name, a_oc->mode, a_oc->path);
2095 add_pending_object_with_path(revs, b_obj, b_name, b_oc->mode, b_oc->path);
2096 return 0;
2099 static int handle_dotdot(const char *arg,
2100 struct rev_info *revs, int flags,
2101 int cant_be_filename)
2103 struct object_context a_oc, b_oc;
2104 char *dotdot = strstr(arg, "..");
2105 int ret;
2107 if (!dotdot)
2108 return -1;
2110 memset(&a_oc, 0, sizeof(a_oc));
2111 memset(&b_oc, 0, sizeof(b_oc));
2113 *dotdot = '\0';
2114 ret = handle_dotdot_1(arg, dotdot, revs, flags, cant_be_filename,
2115 &a_oc, &b_oc);
2116 *dotdot = '.';
2118 free(a_oc.path);
2119 free(b_oc.path);
2121 return ret;
2124 static int handle_revision_arg_1(const char *arg_, struct rev_info *revs, int flags, unsigned revarg_opt)
2126 struct object_context oc;
2127 char *mark;
2128 struct object *object;
2129 struct object_id oid;
2130 int local_flags;
2131 const char *arg = arg_;
2132 int cant_be_filename = revarg_opt & REVARG_CANNOT_BE_FILENAME;
2133 unsigned get_sha1_flags = GET_OID_RECORD_PATH;
2135 flags = flags & UNINTERESTING ? flags | BOTTOM : flags & ~BOTTOM;
2137 if (!cant_be_filename && !strcmp(arg, "..")) {
2139 * Just ".."? That is not a range but the
2140 * pathspec for the parent directory.
2142 return -1;
2145 if (!handle_dotdot(arg, revs, flags, revarg_opt))
2146 return 0;
2148 mark = strstr(arg, "^@");
2149 if (mark && !mark[2]) {
2150 *mark = 0;
2151 if (add_parents_only(revs, arg, flags, 0))
2152 return 0;
2153 *mark = '^';
2155 mark = strstr(arg, "^!");
2156 if (mark && !mark[2]) {
2157 *mark = 0;
2158 if (!add_parents_only(revs, arg, flags ^ (UNINTERESTING | BOTTOM), 0))
2159 *mark = '^';
2161 mark = strstr(arg, "^-");
2162 if (mark) {
2163 int exclude_parent = 1;
2165 if (mark[2]) {
2166 if (strtol_i(mark + 2, 10, &exclude_parent) ||
2167 exclude_parent < 1)
2168 return -1;
2171 *mark = 0;
2172 if (!add_parents_only(revs, arg, flags ^ (UNINTERESTING | BOTTOM), exclude_parent))
2173 *mark = '^';
2176 local_flags = 0;
2177 if (*arg == '^') {
2178 local_flags = UNINTERESTING | BOTTOM;
2179 arg++;
2182 if (revarg_opt & REVARG_COMMITTISH)
2183 get_sha1_flags |= GET_OID_COMMITTISH;
2185 if (get_oid_with_context(revs->repo, arg, get_sha1_flags, &oid, &oc))
2186 return revs->ignore_missing ? 0 : -1;
2187 if (!cant_be_filename)
2188 verify_non_filename(revs->prefix, arg);
2189 object = get_reference(revs, arg, &oid, flags ^ local_flags);
2190 if (!object)
2191 return revs->ignore_missing ? 0 : -1;
2192 add_rev_cmdline(revs, object, arg_, REV_CMD_REV, flags ^ local_flags);
2193 add_pending_object_with_path(revs, object, arg, oc.mode, oc.path);
2194 free(oc.path);
2195 return 0;
2198 int handle_revision_arg(const char *arg, struct rev_info *revs, int flags, unsigned revarg_opt)
2200 int ret = handle_revision_arg_1(arg, revs, flags, revarg_opt);
2201 if (!ret)
2202 revs->rev_input_given = 1;
2203 return ret;
2206 static void read_pathspec_from_stdin(struct strbuf *sb,
2207 struct strvec *prune)
2209 while (strbuf_getline(sb, stdin) != EOF)
2210 strvec_push(prune, sb->buf);
2213 static void add_grep(struct rev_info *revs, const char *ptn, enum grep_pat_token what)
2215 append_grep_pattern(&revs->grep_filter, ptn, "command line", 0, what);
2218 static void add_header_grep(struct rev_info *revs, enum grep_header_field field, const char *pattern)
2220 append_header_grep_pattern(&revs->grep_filter, field, pattern);
2223 static void add_message_grep(struct rev_info *revs, const char *pattern)
2225 add_grep(revs, pattern, GREP_PATTERN_BODY);
2228 static int parse_count(const char *arg)
2230 int count;
2232 if (strtol_i(arg, 10, &count) < 0)
2233 die("'%s': not an integer", arg);
2234 return count;
2237 static timestamp_t parse_age(const char *arg)
2239 timestamp_t num;
2240 char *p;
2242 errno = 0;
2243 num = parse_timestamp(arg, &p, 10);
2244 if (errno || *p || p == arg)
2245 die("'%s': not a number of seconds since epoch", arg);
2246 return num;
2249 static int handle_revision_opt(struct rev_info *revs, int argc, const char **argv,
2250 int *unkc, const char **unkv,
2251 const struct setup_revision_opt* opt)
2253 const char *arg = argv[0];
2254 const char *optarg = NULL;
2255 int argcount;
2256 const unsigned hexsz = the_hash_algo->hexsz;
2258 /* pseudo revision arguments */
2259 if (!strcmp(arg, "--all") || !strcmp(arg, "--branches") ||
2260 !strcmp(arg, "--tags") || !strcmp(arg, "--remotes") ||
2261 !strcmp(arg, "--reflog") || !strcmp(arg, "--not") ||
2262 !strcmp(arg, "--no-walk") || !strcmp(arg, "--do-walk") ||
2263 !strcmp(arg, "--bisect") || starts_with(arg, "--glob=") ||
2264 !strcmp(arg, "--indexed-objects") ||
2265 !strcmp(arg, "--alternate-refs") ||
2266 starts_with(arg, "--exclude=") || starts_with(arg, "--exclude-hidden=") ||
2267 starts_with(arg, "--branches=") || starts_with(arg, "--tags=") ||
2268 starts_with(arg, "--remotes=") || starts_with(arg, "--no-walk="))
2270 unkv[(*unkc)++] = arg;
2271 return 1;
2274 if ((argcount = parse_long_opt("max-count", argv, &optarg))) {
2275 revs->max_count = parse_count(optarg);
2276 revs->no_walk = 0;
2277 return argcount;
2278 } else if ((argcount = parse_long_opt("skip", argv, &optarg))) {
2279 revs->skip_count = parse_count(optarg);
2280 return argcount;
2281 } else if ((*arg == '-') && isdigit(arg[1])) {
2282 /* accept -<digit>, like traditional "head" */
2283 revs->max_count = parse_count(arg + 1);
2284 revs->no_walk = 0;
2285 } else if (!strcmp(arg, "-n")) {
2286 if (argc <= 1)
2287 return error("-n requires an argument");
2288 revs->max_count = parse_count(argv[1]);
2289 revs->no_walk = 0;
2290 return 2;
2291 } else if (skip_prefix(arg, "-n", &optarg)) {
2292 revs->max_count = parse_count(optarg);
2293 revs->no_walk = 0;
2294 } else if ((argcount = parse_long_opt("max-age", argv, &optarg))) {
2295 revs->max_age = parse_age(optarg);
2296 return argcount;
2297 } else if ((argcount = parse_long_opt("since", argv, &optarg))) {
2298 revs->max_age = approxidate(optarg);
2299 return argcount;
2300 } else if ((argcount = parse_long_opt("since-as-filter", argv, &optarg))) {
2301 revs->max_age_as_filter = approxidate(optarg);
2302 return argcount;
2303 } else if ((argcount = parse_long_opt("after", argv, &optarg))) {
2304 revs->max_age = approxidate(optarg);
2305 return argcount;
2306 } else if ((argcount = parse_long_opt("min-age", argv, &optarg))) {
2307 revs->min_age = parse_age(optarg);
2308 return argcount;
2309 } else if ((argcount = parse_long_opt("before", argv, &optarg))) {
2310 revs->min_age = approxidate(optarg);
2311 return argcount;
2312 } else if ((argcount = parse_long_opt("until", argv, &optarg))) {
2313 revs->min_age = approxidate(optarg);
2314 return argcount;
2315 } else if (!strcmp(arg, "--first-parent")) {
2316 revs->first_parent_only = 1;
2317 } else if (!strcmp(arg, "--exclude-first-parent-only")) {
2318 revs->exclude_first_parent_only = 1;
2319 } else if (!strcmp(arg, "--ancestry-path")) {
2320 revs->ancestry_path = 1;
2321 revs->simplify_history = 0;
2322 revs->limited = 1;
2323 revs->ancestry_path_implicit_bottoms = 1;
2324 } else if (skip_prefix(arg, "--ancestry-path=", &optarg)) {
2325 struct commit *c;
2326 struct object_id oid;
2327 const char *msg = _("could not get commit for ancestry-path argument %s");
2329 revs->ancestry_path = 1;
2330 revs->simplify_history = 0;
2331 revs->limited = 1;
2333 if (repo_get_oid_committish(revs->repo, optarg, &oid))
2334 return error(msg, optarg);
2335 get_reference(revs, optarg, &oid, ANCESTRY_PATH);
2336 c = lookup_commit_reference(revs->repo, &oid);
2337 if (!c)
2338 return error(msg, optarg);
2339 commit_list_insert(c, &revs->ancestry_path_bottoms);
2340 } else if (!strcmp(arg, "-g") || !strcmp(arg, "--walk-reflogs")) {
2341 init_reflog_walk(&revs->reflog_info);
2342 } else if (!strcmp(arg, "--default")) {
2343 if (argc <= 1)
2344 return error("bad --default argument");
2345 revs->def = argv[1];
2346 return 2;
2347 } else if (!strcmp(arg, "--merge")) {
2348 revs->show_merge = 1;
2349 } else if (!strcmp(arg, "--topo-order")) {
2350 revs->sort_order = REV_SORT_IN_GRAPH_ORDER;
2351 revs->topo_order = 1;
2352 } else if (!strcmp(arg, "--simplify-merges")) {
2353 revs->simplify_merges = 1;
2354 revs->topo_order = 1;
2355 revs->rewrite_parents = 1;
2356 revs->simplify_history = 0;
2357 revs->limited = 1;
2358 } else if (!strcmp(arg, "--simplify-by-decoration")) {
2359 revs->simplify_merges = 1;
2360 revs->topo_order = 1;
2361 revs->rewrite_parents = 1;
2362 revs->simplify_history = 0;
2363 revs->simplify_by_decoration = 1;
2364 revs->limited = 1;
2365 revs->prune = 1;
2366 } else if (!strcmp(arg, "--date-order")) {
2367 revs->sort_order = REV_SORT_BY_COMMIT_DATE;
2368 revs->topo_order = 1;
2369 } else if (!strcmp(arg, "--author-date-order")) {
2370 revs->sort_order = REV_SORT_BY_AUTHOR_DATE;
2371 revs->topo_order = 1;
2372 } else if (!strcmp(arg, "--early-output")) {
2373 revs->early_output = 100;
2374 revs->topo_order = 1;
2375 } else if (skip_prefix(arg, "--early-output=", &optarg)) {
2376 if (strtoul_ui(optarg, 10, &revs->early_output) < 0)
2377 die("'%s': not a non-negative integer", optarg);
2378 revs->topo_order = 1;
2379 } else if (!strcmp(arg, "--parents")) {
2380 revs->rewrite_parents = 1;
2381 revs->print_parents = 1;
2382 } else if (!strcmp(arg, "--dense")) {
2383 revs->dense = 1;
2384 } else if (!strcmp(arg, "--sparse")) {
2385 revs->dense = 0;
2386 } else if (!strcmp(arg, "--in-commit-order")) {
2387 revs->tree_blobs_in_commit_order = 1;
2388 } else if (!strcmp(arg, "--remove-empty")) {
2389 revs->remove_empty_trees = 1;
2390 } else if (!strcmp(arg, "--merges")) {
2391 revs->min_parents = 2;
2392 } else if (!strcmp(arg, "--no-merges")) {
2393 revs->max_parents = 1;
2394 } else if (skip_prefix(arg, "--min-parents=", &optarg)) {
2395 revs->min_parents = parse_count(optarg);
2396 } else if (!strcmp(arg, "--no-min-parents")) {
2397 revs->min_parents = 0;
2398 } else if (skip_prefix(arg, "--max-parents=", &optarg)) {
2399 revs->max_parents = parse_count(optarg);
2400 } else if (!strcmp(arg, "--no-max-parents")) {
2401 revs->max_parents = -1;
2402 } else if (!strcmp(arg, "--boundary")) {
2403 revs->boundary = 1;
2404 } else if (!strcmp(arg, "--left-right")) {
2405 revs->left_right = 1;
2406 } else if (!strcmp(arg, "--left-only")) {
2407 if (revs->right_only)
2408 die(_("options '%s' and '%s' cannot be used together"),
2409 "--left-only", "--right-only/--cherry");
2410 revs->left_only = 1;
2411 } else if (!strcmp(arg, "--right-only")) {
2412 if (revs->left_only)
2413 die(_("options '%s' and '%s' cannot be used together"), "--right-only", "--left-only");
2414 revs->right_only = 1;
2415 } else if (!strcmp(arg, "--cherry")) {
2416 if (revs->left_only)
2417 die(_("options '%s' and '%s' cannot be used together"), "--cherry", "--left-only");
2418 revs->cherry_mark = 1;
2419 revs->right_only = 1;
2420 revs->max_parents = 1;
2421 revs->limited = 1;
2422 } else if (!strcmp(arg, "--count")) {
2423 revs->count = 1;
2424 } else if (!strcmp(arg, "--cherry-mark")) {
2425 if (revs->cherry_pick)
2426 die(_("options '%s' and '%s' cannot be used together"), "--cherry-mark", "--cherry-pick");
2427 revs->cherry_mark = 1;
2428 revs->limited = 1; /* needs limit_list() */
2429 } else if (!strcmp(arg, "--cherry-pick")) {
2430 if (revs->cherry_mark)
2431 die(_("options '%s' and '%s' cannot be used together"), "--cherry-pick", "--cherry-mark");
2432 revs->cherry_pick = 1;
2433 revs->limited = 1;
2434 } else if (!strcmp(arg, "--objects")) {
2435 revs->tag_objects = 1;
2436 revs->tree_objects = 1;
2437 revs->blob_objects = 1;
2438 } else if (!strcmp(arg, "--objects-edge")) {
2439 revs->tag_objects = 1;
2440 revs->tree_objects = 1;
2441 revs->blob_objects = 1;
2442 revs->edge_hint = 1;
2443 } else if (!strcmp(arg, "--objects-edge-aggressive")) {
2444 revs->tag_objects = 1;
2445 revs->tree_objects = 1;
2446 revs->blob_objects = 1;
2447 revs->edge_hint = 1;
2448 revs->edge_hint_aggressive = 1;
2449 } else if (!strcmp(arg, "--verify-objects")) {
2450 revs->tag_objects = 1;
2451 revs->tree_objects = 1;
2452 revs->blob_objects = 1;
2453 revs->verify_objects = 1;
2454 disable_commit_graph(revs->repo);
2455 } else if (!strcmp(arg, "--unpacked")) {
2456 revs->unpacked = 1;
2457 } else if (starts_with(arg, "--unpacked=")) {
2458 die(_("--unpacked=<packfile> no longer supported"));
2459 } else if (!strcmp(arg, "--no-kept-objects")) {
2460 revs->no_kept_objects = 1;
2461 revs->keep_pack_cache_flags |= IN_CORE_KEEP_PACKS;
2462 revs->keep_pack_cache_flags |= ON_DISK_KEEP_PACKS;
2463 } else if (skip_prefix(arg, "--no-kept-objects=", &optarg)) {
2464 revs->no_kept_objects = 1;
2465 if (!strcmp(optarg, "in-core"))
2466 revs->keep_pack_cache_flags |= IN_CORE_KEEP_PACKS;
2467 if (!strcmp(optarg, "on-disk"))
2468 revs->keep_pack_cache_flags |= ON_DISK_KEEP_PACKS;
2469 } else if (!strcmp(arg, "-r")) {
2470 revs->diff = 1;
2471 revs->diffopt.flags.recursive = 1;
2472 } else if (!strcmp(arg, "-t")) {
2473 revs->diff = 1;
2474 revs->diffopt.flags.recursive = 1;
2475 revs->diffopt.flags.tree_in_recursive = 1;
2476 } else if ((argcount = diff_merges_parse_opts(revs, argv))) {
2477 return argcount;
2478 } else if (!strcmp(arg, "-v")) {
2479 revs->verbose_header = 1;
2480 } else if (!strcmp(arg, "--pretty")) {
2481 revs->verbose_header = 1;
2482 revs->pretty_given = 1;
2483 get_commit_format(NULL, revs);
2484 } else if (skip_prefix(arg, "--pretty=", &optarg) ||
2485 skip_prefix(arg, "--format=", &optarg)) {
2487 * Detached form ("--pretty X" as opposed to "--pretty=X")
2488 * not allowed, since the argument is optional.
2490 revs->verbose_header = 1;
2491 revs->pretty_given = 1;
2492 get_commit_format(optarg, revs);
2493 } else if (!strcmp(arg, "--expand-tabs")) {
2494 revs->expand_tabs_in_log = 8;
2495 } else if (!strcmp(arg, "--no-expand-tabs")) {
2496 revs->expand_tabs_in_log = 0;
2497 } else if (skip_prefix(arg, "--expand-tabs=", &arg)) {
2498 int val;
2499 if (strtol_i(arg, 10, &val) < 0 || val < 0)
2500 die("'%s': not a non-negative integer", arg);
2501 revs->expand_tabs_in_log = val;
2502 } else if (!strcmp(arg, "--show-notes") || !strcmp(arg, "--notes")) {
2503 enable_default_display_notes(&revs->notes_opt, &revs->show_notes);
2504 revs->show_notes_given = 1;
2505 } else if (!strcmp(arg, "--show-signature")) {
2506 revs->show_signature = 1;
2507 } else if (!strcmp(arg, "--no-show-signature")) {
2508 revs->show_signature = 0;
2509 } else if (!strcmp(arg, "--show-linear-break")) {
2510 revs->break_bar = " ..........";
2511 revs->track_linear = 1;
2512 revs->track_first_time = 1;
2513 } else if (skip_prefix(arg, "--show-linear-break=", &optarg)) {
2514 revs->break_bar = xstrdup(optarg);
2515 revs->track_linear = 1;
2516 revs->track_first_time = 1;
2517 } else if (!strcmp(arg, "--show-notes-by-default")) {
2518 revs->show_notes_by_default = 1;
2519 } else if (skip_prefix(arg, "--show-notes=", &optarg) ||
2520 skip_prefix(arg, "--notes=", &optarg)) {
2521 if (starts_with(arg, "--show-notes=") &&
2522 revs->notes_opt.use_default_notes < 0)
2523 revs->notes_opt.use_default_notes = 1;
2524 enable_ref_display_notes(&revs->notes_opt, &revs->show_notes, optarg);
2525 revs->show_notes_given = 1;
2526 } else if (!strcmp(arg, "--no-notes")) {
2527 disable_display_notes(&revs->notes_opt, &revs->show_notes);
2528 revs->show_notes_given = 1;
2529 } else if (!strcmp(arg, "--standard-notes")) {
2530 revs->show_notes_given = 1;
2531 revs->notes_opt.use_default_notes = 1;
2532 } else if (!strcmp(arg, "--no-standard-notes")) {
2533 revs->notes_opt.use_default_notes = 0;
2534 } else if (!strcmp(arg, "--oneline")) {
2535 revs->verbose_header = 1;
2536 get_commit_format("oneline", revs);
2537 revs->pretty_given = 1;
2538 revs->abbrev_commit = 1;
2539 } else if (!strcmp(arg, "--graph")) {
2540 graph_clear(revs->graph);
2541 revs->graph = graph_init(revs);
2542 } else if (!strcmp(arg, "--no-graph")) {
2543 graph_clear(revs->graph);
2544 revs->graph = NULL;
2545 } else if (!strcmp(arg, "--encode-email-headers")) {
2546 revs->encode_email_headers = 1;
2547 } else if (!strcmp(arg, "--no-encode-email-headers")) {
2548 revs->encode_email_headers = 0;
2549 } else if (!strcmp(arg, "--root")) {
2550 revs->show_root_diff = 1;
2551 } else if (!strcmp(arg, "--no-commit-id")) {
2552 revs->no_commit_id = 1;
2553 } else if (!strcmp(arg, "--always")) {
2554 revs->always_show_header = 1;
2555 } else if (!strcmp(arg, "--no-abbrev")) {
2556 revs->abbrev = 0;
2557 } else if (!strcmp(arg, "--abbrev")) {
2558 revs->abbrev = DEFAULT_ABBREV;
2559 } else if (skip_prefix(arg, "--abbrev=", &optarg)) {
2560 revs->abbrev = strtoul(optarg, NULL, 10);
2561 if (revs->abbrev < MINIMUM_ABBREV)
2562 revs->abbrev = MINIMUM_ABBREV;
2563 else if (revs->abbrev > hexsz)
2564 revs->abbrev = hexsz;
2565 } else if (!strcmp(arg, "--abbrev-commit")) {
2566 revs->abbrev_commit = 1;
2567 revs->abbrev_commit_given = 1;
2568 } else if (!strcmp(arg, "--no-abbrev-commit")) {
2569 revs->abbrev_commit = 0;
2570 } else if (!strcmp(arg, "--full-diff")) {
2571 revs->diff = 1;
2572 revs->full_diff = 1;
2573 } else if (!strcmp(arg, "--show-pulls")) {
2574 revs->show_pulls = 1;
2575 } else if (!strcmp(arg, "--full-history")) {
2576 revs->simplify_history = 0;
2577 } else if (!strcmp(arg, "--relative-date")) {
2578 revs->date_mode.type = DATE_RELATIVE;
2579 revs->date_mode_explicit = 1;
2580 } else if ((argcount = parse_long_opt("date", argv, &optarg))) {
2581 parse_date_format(optarg, &revs->date_mode);
2582 revs->date_mode_explicit = 1;
2583 return argcount;
2584 } else if (!strcmp(arg, "--log-size")) {
2585 revs->show_log_size = 1;
2588 * Grepping the commit log
2590 else if ((argcount = parse_long_opt("author", argv, &optarg))) {
2591 add_header_grep(revs, GREP_HEADER_AUTHOR, optarg);
2592 return argcount;
2593 } else if ((argcount = parse_long_opt("committer", argv, &optarg))) {
2594 add_header_grep(revs, GREP_HEADER_COMMITTER, optarg);
2595 return argcount;
2596 } else if ((argcount = parse_long_opt("grep-reflog", argv, &optarg))) {
2597 add_header_grep(revs, GREP_HEADER_REFLOG, optarg);
2598 return argcount;
2599 } else if ((argcount = parse_long_opt("grep", argv, &optarg))) {
2600 add_message_grep(revs, optarg);
2601 return argcount;
2602 } else if (!strcmp(arg, "--basic-regexp")) {
2603 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_BRE;
2604 } else if (!strcmp(arg, "--extended-regexp") || !strcmp(arg, "-E")) {
2605 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_ERE;
2606 } else if (!strcmp(arg, "--regexp-ignore-case") || !strcmp(arg, "-i")) {
2607 revs->grep_filter.ignore_case = 1;
2608 revs->diffopt.pickaxe_opts |= DIFF_PICKAXE_IGNORE_CASE;
2609 } else if (!strcmp(arg, "--fixed-strings") || !strcmp(arg, "-F")) {
2610 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_FIXED;
2611 } else if (!strcmp(arg, "--perl-regexp") || !strcmp(arg, "-P")) {
2612 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_PCRE;
2613 } else if (!strcmp(arg, "--all-match")) {
2614 revs->grep_filter.all_match = 1;
2615 } else if (!strcmp(arg, "--invert-grep")) {
2616 revs->grep_filter.no_body_match = 1;
2617 } else if ((argcount = parse_long_opt("encoding", argv, &optarg))) {
2618 if (strcmp(optarg, "none"))
2619 git_log_output_encoding = xstrdup(optarg);
2620 else
2621 git_log_output_encoding = "";
2622 return argcount;
2623 } else if (!strcmp(arg, "--reverse")) {
2624 revs->reverse ^= 1;
2625 } else if (!strcmp(arg, "--children")) {
2626 revs->children.name = "children";
2627 revs->limited = 1;
2628 } else if (!strcmp(arg, "--ignore-missing")) {
2629 revs->ignore_missing = 1;
2630 } else if (opt && opt->allow_exclude_promisor_objects &&
2631 !strcmp(arg, "--exclude-promisor-objects")) {
2632 if (fetch_if_missing)
2633 BUG("exclude_promisor_objects can only be used when fetch_if_missing is 0");
2634 revs->exclude_promisor_objects = 1;
2635 } else {
2636 int opts = diff_opt_parse(&revs->diffopt, argv, argc, revs->prefix);
2637 if (!opts)
2638 unkv[(*unkc)++] = arg;
2639 return opts;
2642 return 1;
2645 void parse_revision_opt(struct rev_info *revs, struct parse_opt_ctx_t *ctx,
2646 const struct option *options,
2647 const char * const usagestr[])
2649 int n = handle_revision_opt(revs, ctx->argc, ctx->argv,
2650 &ctx->cpidx, ctx->out, NULL);
2651 if (n <= 0) {
2652 error("unknown option `%s'", ctx->argv[0]);
2653 usage_with_options(usagestr, options);
2655 ctx->argv += n;
2656 ctx->argc -= n;
2659 void revision_opts_finish(struct rev_info *revs)
2661 if (revs->graph && revs->track_linear)
2662 die(_("options '%s' and '%s' cannot be used together"), "--show-linear-break", "--graph");
2664 if (revs->graph) {
2665 revs->topo_order = 1;
2666 revs->rewrite_parents = 1;
2670 static int for_each_bisect_ref(struct ref_store *refs, each_ref_fn fn,
2671 void *cb_data, const char *term)
2673 struct strbuf bisect_refs = STRBUF_INIT;
2674 int status;
2675 strbuf_addf(&bisect_refs, "refs/bisect/%s", term);
2676 status = refs_for_each_fullref_in(refs, bisect_refs.buf, NULL, fn, cb_data);
2677 strbuf_release(&bisect_refs);
2678 return status;
2681 static int for_each_bad_bisect_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
2683 return for_each_bisect_ref(refs, fn, cb_data, term_bad);
2686 static int for_each_good_bisect_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
2688 return for_each_bisect_ref(refs, fn, cb_data, term_good);
2691 static int handle_revision_pseudo_opt(struct rev_info *revs,
2692 const char **argv, int *flags)
2694 const char *arg = argv[0];
2695 const char *optarg;
2696 struct ref_store *refs;
2697 int argcount;
2699 if (revs->repo != the_repository) {
2701 * We need some something like get_submodule_worktrees()
2702 * before we can go through all worktrees of a submodule,
2703 * .e.g with adding all HEADs from --all, which is not
2704 * supported right now, so stick to single worktree.
2706 if (!revs->single_worktree)
2707 BUG("--single-worktree cannot be used together with submodule");
2709 refs = get_main_ref_store(revs->repo);
2712 * NOTE!
2714 * Commands like "git shortlog" will not accept the options below
2715 * unless parse_revision_opt queues them (as opposed to erroring
2716 * out).
2718 * When implementing your new pseudo-option, remember to
2719 * register it in the list at the top of handle_revision_opt.
2721 if (!strcmp(arg, "--all")) {
2722 handle_refs(refs, revs, *flags, refs_for_each_ref);
2723 handle_refs(refs, revs, *flags, refs_head_ref);
2724 if (!revs->single_worktree) {
2725 struct all_refs_cb cb;
2727 init_all_refs_cb(&cb, revs, *flags);
2728 other_head_refs(handle_one_ref, &cb);
2730 clear_ref_exclusions(&revs->ref_excludes);
2731 } else if (!strcmp(arg, "--branches")) {
2732 if (revs->ref_excludes.hidden_refs_configured)
2733 return error(_("options '%s' and '%s' cannot be used together"),
2734 "--exclude-hidden", "--branches");
2735 handle_refs(refs, revs, *flags, refs_for_each_branch_ref);
2736 clear_ref_exclusions(&revs->ref_excludes);
2737 } else if (!strcmp(arg, "--bisect")) {
2738 read_bisect_terms(&term_bad, &term_good);
2739 handle_refs(refs, revs, *flags, for_each_bad_bisect_ref);
2740 handle_refs(refs, revs, *flags ^ (UNINTERESTING | BOTTOM),
2741 for_each_good_bisect_ref);
2742 revs->bisect = 1;
2743 } else if (!strcmp(arg, "--tags")) {
2744 if (revs->ref_excludes.hidden_refs_configured)
2745 return error(_("options '%s' and '%s' cannot be used together"),
2746 "--exclude-hidden", "--tags");
2747 handle_refs(refs, revs, *flags, refs_for_each_tag_ref);
2748 clear_ref_exclusions(&revs->ref_excludes);
2749 } else if (!strcmp(arg, "--remotes")) {
2750 if (revs->ref_excludes.hidden_refs_configured)
2751 return error(_("options '%s' and '%s' cannot be used together"),
2752 "--exclude-hidden", "--remotes");
2753 handle_refs(refs, revs, *flags, refs_for_each_remote_ref);
2754 clear_ref_exclusions(&revs->ref_excludes);
2755 } else if ((argcount = parse_long_opt("glob", argv, &optarg))) {
2756 struct all_refs_cb cb;
2757 init_all_refs_cb(&cb, revs, *flags);
2758 for_each_glob_ref(handle_one_ref, optarg, &cb);
2759 clear_ref_exclusions(&revs->ref_excludes);
2760 return argcount;
2761 } else if ((argcount = parse_long_opt("exclude", argv, &optarg))) {
2762 add_ref_exclusion(&revs->ref_excludes, optarg);
2763 return argcount;
2764 } else if ((argcount = parse_long_opt("exclude-hidden", argv, &optarg))) {
2765 exclude_hidden_refs(&revs->ref_excludes, optarg);
2766 return argcount;
2767 } else if (skip_prefix(arg, "--branches=", &optarg)) {
2768 struct all_refs_cb cb;
2769 if (revs->ref_excludes.hidden_refs_configured)
2770 return error(_("options '%s' and '%s' cannot be used together"),
2771 "--exclude-hidden", "--branches");
2772 init_all_refs_cb(&cb, revs, *flags);
2773 for_each_glob_ref_in(handle_one_ref, optarg, "refs/heads/", &cb);
2774 clear_ref_exclusions(&revs->ref_excludes);
2775 } else if (skip_prefix(arg, "--tags=", &optarg)) {
2776 struct all_refs_cb cb;
2777 if (revs->ref_excludes.hidden_refs_configured)
2778 return error(_("options '%s' and '%s' cannot be used together"),
2779 "--exclude-hidden", "--tags");
2780 init_all_refs_cb(&cb, revs, *flags);
2781 for_each_glob_ref_in(handle_one_ref, optarg, "refs/tags/", &cb);
2782 clear_ref_exclusions(&revs->ref_excludes);
2783 } else if (skip_prefix(arg, "--remotes=", &optarg)) {
2784 struct all_refs_cb cb;
2785 if (revs->ref_excludes.hidden_refs_configured)
2786 return error(_("options '%s' and '%s' cannot be used together"),
2787 "--exclude-hidden", "--remotes");
2788 init_all_refs_cb(&cb, revs, *flags);
2789 for_each_glob_ref_in(handle_one_ref, optarg, "refs/remotes/", &cb);
2790 clear_ref_exclusions(&revs->ref_excludes);
2791 } else if (!strcmp(arg, "--reflog")) {
2792 add_reflogs_to_pending(revs, *flags);
2793 } else if (!strcmp(arg, "--indexed-objects")) {
2794 add_index_objects_to_pending(revs, *flags);
2795 } else if (!strcmp(arg, "--alternate-refs")) {
2796 add_alternate_refs_to_pending(revs, *flags);
2797 } else if (!strcmp(arg, "--not")) {
2798 *flags ^= UNINTERESTING | BOTTOM;
2799 } else if (!strcmp(arg, "--no-walk")) {
2800 revs->no_walk = 1;
2801 } else if (skip_prefix(arg, "--no-walk=", &optarg)) {
2803 * Detached form ("--no-walk X" as opposed to "--no-walk=X")
2804 * not allowed, since the argument is optional.
2806 revs->no_walk = 1;
2807 if (!strcmp(optarg, "sorted"))
2808 revs->unsorted_input = 0;
2809 else if (!strcmp(optarg, "unsorted"))
2810 revs->unsorted_input = 1;
2811 else
2812 return error("invalid argument to --no-walk");
2813 } else if (!strcmp(arg, "--do-walk")) {
2814 revs->no_walk = 0;
2815 } else if (!strcmp(arg, "--single-worktree")) {
2816 revs->single_worktree = 1;
2817 } else if (skip_prefix(arg, ("--filter="), &arg)) {
2818 parse_list_objects_filter(&revs->filter, arg);
2819 } else if (!strcmp(arg, ("--no-filter"))) {
2820 list_objects_filter_set_no_filter(&revs->filter);
2821 } else {
2822 return 0;
2825 return 1;
2828 static void read_revisions_from_stdin(struct rev_info *revs,
2829 struct strvec *prune)
2831 struct strbuf sb;
2832 int seen_dashdash = 0;
2833 int seen_end_of_options = 0;
2834 int save_warning;
2835 int flags = 0;
2837 save_warning = warn_on_object_refname_ambiguity;
2838 warn_on_object_refname_ambiguity = 0;
2840 strbuf_init(&sb, 1000);
2841 while (strbuf_getline(&sb, stdin) != EOF) {
2842 if (!sb.len)
2843 break;
2845 if (!strcmp(sb.buf, "--")) {
2846 seen_dashdash = 1;
2847 break;
2850 if (!seen_end_of_options && sb.buf[0] == '-') {
2851 const char *argv[] = { sb.buf, NULL };
2853 if (!strcmp(sb.buf, "--end-of-options")) {
2854 seen_end_of_options = 1;
2855 continue;
2858 if (handle_revision_pseudo_opt(revs, argv, &flags) > 0)
2859 continue;
2861 die(_("invalid option '%s' in --stdin mode"), sb.buf);
2864 if (handle_revision_arg(sb.buf, revs, flags,
2865 REVARG_CANNOT_BE_FILENAME))
2866 die("bad revision '%s'", sb.buf);
2868 if (seen_dashdash)
2869 read_pathspec_from_stdin(&sb, prune);
2871 strbuf_release(&sb);
2872 warn_on_object_refname_ambiguity = save_warning;
2875 static void NORETURN diagnose_missing_default(const char *def)
2877 int flags;
2878 const char *refname;
2880 refname = resolve_ref_unsafe(def, 0, NULL, &flags);
2881 if (!refname || !(flags & REF_ISSYMREF) || (flags & REF_ISBROKEN))
2882 die(_("your current branch appears to be broken"));
2884 skip_prefix(refname, "refs/heads/", &refname);
2885 die(_("your current branch '%s' does not have any commits yet"),
2886 refname);
2890 * Parse revision information, filling in the "rev_info" structure,
2891 * and removing the used arguments from the argument list.
2893 * Returns the number of arguments left that weren't recognized
2894 * (which are also moved to the head of the argument list)
2896 int setup_revisions(int argc, const char **argv, struct rev_info *revs, struct setup_revision_opt *opt)
2898 int i, flags, left, seen_dashdash, revarg_opt;
2899 struct strvec prune_data = STRVEC_INIT;
2900 int seen_end_of_options = 0;
2902 /* First, search for "--" */
2903 if (opt && opt->assume_dashdash) {
2904 seen_dashdash = 1;
2905 } else {
2906 seen_dashdash = 0;
2907 for (i = 1; i < argc; i++) {
2908 const char *arg = argv[i];
2909 if (strcmp(arg, "--"))
2910 continue;
2911 if (opt && opt->free_removed_argv_elements)
2912 free((char *)argv[i]);
2913 argv[i] = NULL;
2914 argc = i;
2915 if (argv[i + 1])
2916 strvec_pushv(&prune_data, argv + i + 1);
2917 seen_dashdash = 1;
2918 break;
2922 /* Second, deal with arguments and options */
2923 flags = 0;
2924 revarg_opt = opt ? opt->revarg_opt : 0;
2925 if (seen_dashdash)
2926 revarg_opt |= REVARG_CANNOT_BE_FILENAME;
2927 for (left = i = 1; i < argc; i++) {
2928 const char *arg = argv[i];
2929 if (!seen_end_of_options && *arg == '-') {
2930 int opts;
2932 opts = handle_revision_pseudo_opt(
2933 revs, argv + i,
2934 &flags);
2935 if (opts > 0) {
2936 i += opts - 1;
2937 continue;
2940 if (!strcmp(arg, "--stdin")) {
2941 if (revs->disable_stdin) {
2942 argv[left++] = arg;
2943 continue;
2945 if (revs->read_from_stdin++)
2946 die("--stdin given twice?");
2947 read_revisions_from_stdin(revs, &prune_data);
2948 continue;
2951 if (!strcmp(arg, "--end-of-options")) {
2952 seen_end_of_options = 1;
2953 continue;
2956 opts = handle_revision_opt(revs, argc - i, argv + i,
2957 &left, argv, opt);
2958 if (opts > 0) {
2959 i += opts - 1;
2960 continue;
2962 if (opts < 0)
2963 exit(128);
2964 continue;
2968 if (handle_revision_arg(arg, revs, flags, revarg_opt)) {
2969 int j;
2970 if (seen_dashdash || *arg == '^')
2971 die("bad revision '%s'", arg);
2973 /* If we didn't have a "--":
2974 * (1) all filenames must exist;
2975 * (2) all rev-args must not be interpretable
2976 * as a valid filename.
2977 * but the latter we have checked in the main loop.
2979 for (j = i; j < argc; j++)
2980 verify_filename(revs->prefix, argv[j], j == i);
2982 strvec_pushv(&prune_data, argv + i);
2983 break;
2986 revision_opts_finish(revs);
2988 if (prune_data.nr) {
2990 * If we need to introduce the magic "a lone ':' means no
2991 * pathspec whatsoever", here is the place to do so.
2993 * if (prune_data.nr == 1 && !strcmp(prune_data[0], ":")) {
2994 * prune_data.nr = 0;
2995 * prune_data.alloc = 0;
2996 * free(prune_data.path);
2997 * prune_data.path = NULL;
2998 * } else {
2999 * terminate prune_data.alloc with NULL and
3000 * call init_pathspec() to set revs->prune_data here.
3003 parse_pathspec(&revs->prune_data, 0, 0,
3004 revs->prefix, prune_data.v);
3006 strvec_clear(&prune_data);
3008 if (!revs->def)
3009 revs->def = opt ? opt->def : NULL;
3010 if (opt && opt->tweak)
3011 opt->tweak(revs);
3012 if (revs->show_merge)
3013 prepare_show_merge(revs);
3014 if (revs->def && !revs->pending.nr && !revs->rev_input_given) {
3015 struct object_id oid;
3016 struct object *object;
3017 struct object_context oc;
3018 if (get_oid_with_context(revs->repo, revs->def, 0, &oid, &oc))
3019 diagnose_missing_default(revs->def);
3020 object = get_reference(revs, revs->def, &oid, 0);
3021 add_pending_object_with_mode(revs, object, revs->def, oc.mode);
3024 /* Did the user ask for any diff output? Run the diff! */
3025 if (revs->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT)
3026 revs->diff = 1;
3028 /* Pickaxe, diff-filter and rename following need diffs */
3029 if ((revs->diffopt.pickaxe_opts & DIFF_PICKAXE_KINDS_MASK) ||
3030 revs->diffopt.filter ||
3031 revs->diffopt.flags.follow_renames)
3032 revs->diff = 1;
3034 if (revs->diffopt.objfind)
3035 revs->simplify_history = 0;
3037 if (revs->line_level_traverse) {
3038 if (want_ancestry(revs))
3039 revs->limited = 1;
3040 revs->topo_order = 1;
3043 if (revs->topo_order && !generation_numbers_enabled(the_repository))
3044 revs->limited = 1;
3046 if (revs->prune_data.nr) {
3047 copy_pathspec(&revs->pruning.pathspec, &revs->prune_data);
3048 /* Can't prune commits with rename following: the paths change.. */
3049 if (!revs->diffopt.flags.follow_renames)
3050 revs->prune = 1;
3051 if (!revs->full_diff)
3052 copy_pathspec(&revs->diffopt.pathspec,
3053 &revs->prune_data);
3056 diff_merges_setup_revs(revs);
3058 revs->diffopt.abbrev = revs->abbrev;
3060 diff_setup_done(&revs->diffopt);
3062 if (!is_encoding_utf8(get_log_output_encoding()))
3063 revs->grep_filter.ignore_locale = 1;
3064 compile_grep_patterns(&revs->grep_filter);
3066 if (revs->reflog_info && revs->limited)
3067 die("cannot combine --walk-reflogs with history-limiting options");
3068 if (revs->rewrite_parents && revs->children.name)
3069 die(_("options '%s' and '%s' cannot be used together"), "--parents", "--children");
3070 if (revs->filter.choice && !revs->blob_objects)
3071 die(_("object filtering requires --objects"));
3074 * Limitations on the graph functionality
3076 die_for_incompatible_opt3(!!revs->graph, "--graph",
3077 !!revs->reverse, "--reverse",
3078 !!revs->reflog_info, "--walk-reflogs");
3080 if (revs->no_walk && revs->graph)
3081 die(_("options '%s' and '%s' cannot be used together"), "--no-walk", "--graph");
3082 if (!revs->reflog_info && revs->grep_filter.use_reflog_filter)
3083 die(_("the option '%s' requires '%s'"), "--grep-reflog", "--walk-reflogs");
3085 if (revs->line_level_traverse &&
3086 (revs->diffopt.output_format & ~(DIFF_FORMAT_PATCH | DIFF_FORMAT_NO_OUTPUT)))
3087 die(_("-L does not yet support diff formats besides -p and -s"));
3089 if (revs->expand_tabs_in_log < 0)
3090 revs->expand_tabs_in_log = revs->expand_tabs_in_log_default;
3092 if (!revs->show_notes_given && revs->show_notes_by_default) {
3093 enable_default_display_notes(&revs->notes_opt, &revs->show_notes);
3094 revs->show_notes_given = 1;
3097 return left;
3100 static void release_revisions_cmdline(struct rev_cmdline_info *cmdline)
3102 unsigned int i;
3104 for (i = 0; i < cmdline->nr; i++)
3105 free((char *)cmdline->rev[i].name);
3106 free(cmdline->rev);
3109 static void release_revisions_mailmap(struct string_list *mailmap)
3111 if (!mailmap)
3112 return;
3113 clear_mailmap(mailmap);
3114 free(mailmap);
3117 static void release_revisions_topo_walk_info(struct topo_walk_info *info);
3119 static void free_void_commit_list(void *list)
3121 free_commit_list(list);
3124 void release_revisions(struct rev_info *revs)
3126 free_commit_list(revs->commits);
3127 free_commit_list(revs->ancestry_path_bottoms);
3128 object_array_clear(&revs->pending);
3129 object_array_clear(&revs->boundary_commits);
3130 release_revisions_cmdline(&revs->cmdline);
3131 list_objects_filter_release(&revs->filter);
3132 clear_pathspec(&revs->prune_data);
3133 date_mode_release(&revs->date_mode);
3134 release_revisions_mailmap(revs->mailmap);
3135 free_grep_patterns(&revs->grep_filter);
3136 graph_clear(revs->graph);
3137 /* TODO (need to handle "no_free"): diff_free(&revs->diffopt) */
3138 diff_free(&revs->pruning);
3139 reflog_walk_info_release(revs->reflog_info);
3140 release_revisions_topo_walk_info(revs->topo_walk_info);
3141 clear_decoration(&revs->children, free_void_commit_list);
3142 clear_decoration(&revs->merge_simplification, free);
3143 clear_decoration(&revs->treesame, free);
3144 line_log_free(revs);
3145 oidset_clear(&revs->missing_commits);
3148 static void add_child(struct rev_info *revs, struct commit *parent, struct commit *child)
3150 struct commit_list *l = xcalloc(1, sizeof(*l));
3152 l->item = child;
3153 l->next = add_decoration(&revs->children, &parent->object, l);
3156 static int remove_duplicate_parents(struct rev_info *revs, struct commit *commit)
3158 struct treesame_state *ts = lookup_decoration(&revs->treesame, &commit->object);
3159 struct commit_list **pp, *p;
3160 int surviving_parents;
3162 /* Examine existing parents while marking ones we have seen... */
3163 pp = &commit->parents;
3164 surviving_parents = 0;
3165 while ((p = *pp) != NULL) {
3166 struct commit *parent = p->item;
3167 if (parent->object.flags & TMP_MARK) {
3168 *pp = p->next;
3169 if (ts)
3170 compact_treesame(revs, commit, surviving_parents);
3171 continue;
3173 parent->object.flags |= TMP_MARK;
3174 surviving_parents++;
3175 pp = &p->next;
3177 /* clear the temporary mark */
3178 for (p = commit->parents; p; p = p->next) {
3179 p->item->object.flags &= ~TMP_MARK;
3181 /* no update_treesame() - removing duplicates can't affect TREESAME */
3182 return surviving_parents;
3185 struct merge_simplify_state {
3186 struct commit *simplified;
3189 static struct merge_simplify_state *locate_simplify_state(struct rev_info *revs, struct commit *commit)
3191 struct merge_simplify_state *st;
3193 st = lookup_decoration(&revs->merge_simplification, &commit->object);
3194 if (!st) {
3195 CALLOC_ARRAY(st, 1);
3196 add_decoration(&revs->merge_simplification, &commit->object, st);
3198 return st;
3201 static int mark_redundant_parents(struct commit *commit)
3203 struct commit_list *h = reduce_heads(commit->parents);
3204 int i = 0, marked = 0;
3205 struct commit_list *po, *pn;
3207 /* Want these for sanity-checking only */
3208 int orig_cnt = commit_list_count(commit->parents);
3209 int cnt = commit_list_count(h);
3212 * Not ready to remove items yet, just mark them for now, based
3213 * on the output of reduce_heads(). reduce_heads outputs the reduced
3214 * set in its original order, so this isn't too hard.
3216 po = commit->parents;
3217 pn = h;
3218 while (po) {
3219 if (pn && po->item == pn->item) {
3220 pn = pn->next;
3221 i++;
3222 } else {
3223 po->item->object.flags |= TMP_MARK;
3224 marked++;
3226 po=po->next;
3229 if (i != cnt || cnt+marked != orig_cnt)
3230 die("mark_redundant_parents %d %d %d %d", orig_cnt, cnt, i, marked);
3232 free_commit_list(h);
3234 return marked;
3237 static int mark_treesame_root_parents(struct commit *commit)
3239 struct commit_list *p;
3240 int marked = 0;
3242 for (p = commit->parents; p; p = p->next) {
3243 struct commit *parent = p->item;
3244 if (!parent->parents && (parent->object.flags & TREESAME)) {
3245 parent->object.flags |= TMP_MARK;
3246 marked++;
3250 return marked;
3254 * Awkward naming - this means one parent we are TREESAME to.
3255 * cf mark_treesame_root_parents: root parents that are TREESAME (to an
3256 * empty tree). Better name suggestions?
3258 static int leave_one_treesame_to_parent(struct rev_info *revs, struct commit *commit)
3260 struct treesame_state *ts = lookup_decoration(&revs->treesame, &commit->object);
3261 struct commit *unmarked = NULL, *marked = NULL;
3262 struct commit_list *p;
3263 unsigned n;
3265 for (p = commit->parents, n = 0; p; p = p->next, n++) {
3266 if (ts->treesame[n]) {
3267 if (p->item->object.flags & TMP_MARK) {
3268 if (!marked)
3269 marked = p->item;
3270 } else {
3271 if (!unmarked) {
3272 unmarked = p->item;
3273 break;
3280 * If we are TREESAME to a marked-for-deletion parent, but not to any
3281 * unmarked parents, unmark the first TREESAME parent. This is the
3282 * parent that the default simplify_history==1 scan would have followed,
3283 * and it doesn't make sense to omit that path when asking for a
3284 * simplified full history. Retaining it improves the chances of
3285 * understanding odd missed merges that took an old version of a file.
3287 * Example:
3289 * I--------*X A modified the file, but mainline merge X used
3290 * \ / "-s ours", so took the version from I. X is
3291 * `-*A--' TREESAME to I and !TREESAME to A.
3293 * Default log from X would produce "I". Without this check,
3294 * --full-history --simplify-merges would produce "I-A-X", showing
3295 * the merge commit X and that it changed A, but not making clear that
3296 * it had just taken the I version. With this check, the topology above
3297 * is retained.
3299 * Note that it is possible that the simplification chooses a different
3300 * TREESAME parent from the default, in which case this test doesn't
3301 * activate, and we _do_ drop the default parent. Example:
3303 * I------X A modified the file, but it was reverted in B,
3304 * \ / meaning mainline merge X is TREESAME to both
3305 * *A-*B parents.
3307 * Default log would produce "I" by following the first parent;
3308 * --full-history --simplify-merges will produce "I-A-B". But this is a
3309 * reasonable result - it presents a logical full history leading from
3310 * I to X, and X is not an important merge.
3312 if (!unmarked && marked) {
3313 marked->object.flags &= ~TMP_MARK;
3314 return 1;
3317 return 0;
3320 static int remove_marked_parents(struct rev_info *revs, struct commit *commit)
3322 struct commit_list **pp, *p;
3323 int nth_parent, removed = 0;
3325 pp = &commit->parents;
3326 nth_parent = 0;
3327 while ((p = *pp) != NULL) {
3328 struct commit *parent = p->item;
3329 if (parent->object.flags & TMP_MARK) {
3330 parent->object.flags &= ~TMP_MARK;
3331 *pp = p->next;
3332 free(p);
3333 removed++;
3334 compact_treesame(revs, commit, nth_parent);
3335 continue;
3337 pp = &p->next;
3338 nth_parent++;
3341 /* Removing parents can only increase TREESAMEness */
3342 if (removed && !(commit->object.flags & TREESAME))
3343 update_treesame(revs, commit);
3345 return nth_parent;
3348 static struct commit_list **simplify_one(struct rev_info *revs, struct commit *commit, struct commit_list **tail)
3350 struct commit_list *p;
3351 struct commit *parent;
3352 struct merge_simplify_state *st, *pst;
3353 int cnt;
3355 st = locate_simplify_state(revs, commit);
3358 * Have we handled this one?
3360 if (st->simplified)
3361 return tail;
3364 * An UNINTERESTING commit simplifies to itself, so does a
3365 * root commit. We do not rewrite parents of such commit
3366 * anyway.
3368 if ((commit->object.flags & UNINTERESTING) || !commit->parents) {
3369 st->simplified = commit;
3370 return tail;
3374 * Do we know what commit all of our parents that matter
3375 * should be rewritten to? Otherwise we are not ready to
3376 * rewrite this one yet.
3378 for (cnt = 0, p = commit->parents; p; p = p->next) {
3379 pst = locate_simplify_state(revs, p->item);
3380 if (!pst->simplified) {
3381 tail = &commit_list_insert(p->item, tail)->next;
3382 cnt++;
3384 if (revs->first_parent_only)
3385 break;
3387 if (cnt) {
3388 tail = &commit_list_insert(commit, tail)->next;
3389 return tail;
3393 * Rewrite our list of parents. Note that this cannot
3394 * affect our TREESAME flags in any way - a commit is
3395 * always TREESAME to its simplification.
3397 for (p = commit->parents; p; p = p->next) {
3398 pst = locate_simplify_state(revs, p->item);
3399 p->item = pst->simplified;
3400 if (revs->first_parent_only)
3401 break;
3404 if (revs->first_parent_only)
3405 cnt = 1;
3406 else
3407 cnt = remove_duplicate_parents(revs, commit);
3410 * It is possible that we are a merge and one side branch
3411 * does not have any commit that touches the given paths;
3412 * in such a case, the immediate parent from that branch
3413 * will be rewritten to be the merge base.
3415 * o----X X: the commit we are looking at;
3416 * / / o: a commit that touches the paths;
3417 * ---o----'
3419 * Further, a merge of an independent branch that doesn't
3420 * touch the path will reduce to a treesame root parent:
3422 * ----o----X X: the commit we are looking at;
3423 * / o: a commit that touches the paths;
3424 * r r: a root commit not touching the paths
3426 * Detect and simplify both cases.
3428 if (1 < cnt) {
3429 int marked = mark_redundant_parents(commit);
3430 marked += mark_treesame_root_parents(commit);
3431 if (marked)
3432 marked -= leave_one_treesame_to_parent(revs, commit);
3433 if (marked)
3434 cnt = remove_marked_parents(revs, commit);
3438 * A commit simplifies to itself if it is a root, if it is
3439 * UNINTERESTING, if it touches the given paths, or if it is a
3440 * merge and its parents don't simplify to one relevant commit
3441 * (the first two cases are already handled at the beginning of
3442 * this function).
3444 * Otherwise, it simplifies to what its sole relevant parent
3445 * simplifies to.
3447 if (!cnt ||
3448 (commit->object.flags & UNINTERESTING) ||
3449 !(commit->object.flags & TREESAME) ||
3450 (parent = one_relevant_parent(revs, commit->parents)) == NULL ||
3451 (revs->show_pulls && (commit->object.flags & PULL_MERGE)))
3452 st->simplified = commit;
3453 else {
3454 pst = locate_simplify_state(revs, parent);
3455 st->simplified = pst->simplified;
3457 return tail;
3460 static void simplify_merges(struct rev_info *revs)
3462 struct commit_list *list, *next;
3463 struct commit_list *yet_to_do, **tail;
3464 struct commit *commit;
3466 if (!revs->prune)
3467 return;
3469 /* feed the list reversed */
3470 yet_to_do = NULL;
3471 for (list = revs->commits; list; list = next) {
3472 commit = list->item;
3473 next = list->next;
3475 * Do not free(list) here yet; the original list
3476 * is used later in this function.
3478 commit_list_insert(commit, &yet_to_do);
3480 while (yet_to_do) {
3481 list = yet_to_do;
3482 yet_to_do = NULL;
3483 tail = &yet_to_do;
3484 while (list) {
3485 commit = pop_commit(&list);
3486 tail = simplify_one(revs, commit, tail);
3490 /* clean up the result, removing the simplified ones */
3491 list = revs->commits;
3492 revs->commits = NULL;
3493 tail = &revs->commits;
3494 while (list) {
3495 struct merge_simplify_state *st;
3497 commit = pop_commit(&list);
3498 st = locate_simplify_state(revs, commit);
3499 if (st->simplified == commit)
3500 tail = &commit_list_insert(commit, tail)->next;
3504 static void set_children(struct rev_info *revs)
3506 struct commit_list *l;
3507 for (l = revs->commits; l; l = l->next) {
3508 struct commit *commit = l->item;
3509 struct commit_list *p;
3511 for (p = commit->parents; p; p = p->next)
3512 add_child(revs, p->item, commit);
3516 void reset_revision_walk(void)
3518 clear_object_flags(SEEN | ADDED | SHOWN | TOPO_WALK_EXPLORED | TOPO_WALK_INDEGREE);
3521 static int mark_uninteresting(const struct object_id *oid,
3522 struct packed_git *pack UNUSED,
3523 uint32_t pos UNUSED,
3524 void *cb)
3526 struct rev_info *revs = cb;
3527 struct object *o = lookup_unknown_object(revs->repo, oid);
3528 o->flags |= UNINTERESTING | SEEN;
3529 return 0;
3532 define_commit_slab(indegree_slab, int);
3533 define_commit_slab(author_date_slab, timestamp_t);
3535 struct topo_walk_info {
3536 timestamp_t min_generation;
3537 struct prio_queue explore_queue;
3538 struct prio_queue indegree_queue;
3539 struct prio_queue topo_queue;
3540 struct indegree_slab indegree;
3541 struct author_date_slab author_date;
3544 static int topo_walk_atexit_registered;
3545 static unsigned int count_explore_walked;
3546 static unsigned int count_indegree_walked;
3547 static unsigned int count_topo_walked;
3549 static void trace2_topo_walk_statistics_atexit(void)
3551 struct json_writer jw = JSON_WRITER_INIT;
3553 jw_object_begin(&jw, 0);
3554 jw_object_intmax(&jw, "count_explore_walked", count_explore_walked);
3555 jw_object_intmax(&jw, "count_indegree_walked", count_indegree_walked);
3556 jw_object_intmax(&jw, "count_topo_walked", count_topo_walked);
3557 jw_end(&jw);
3559 trace2_data_json("topo_walk", the_repository, "statistics", &jw);
3561 jw_release(&jw);
3564 static inline void test_flag_and_insert(struct prio_queue *q, struct commit *c, int flag)
3566 if (c->object.flags & flag)
3567 return;
3569 c->object.flags |= flag;
3570 prio_queue_put(q, c);
3573 static void explore_walk_step(struct rev_info *revs)
3575 struct topo_walk_info *info = revs->topo_walk_info;
3576 struct commit_list *p;
3577 struct commit *c = prio_queue_get(&info->explore_queue);
3579 if (!c)
3580 return;
3582 if (repo_parse_commit_gently(revs->repo, c, 1) < 0)
3583 return;
3585 count_explore_walked++;
3587 if (revs->sort_order == REV_SORT_BY_AUTHOR_DATE)
3588 record_author_date(&info->author_date, c);
3590 if (revs->max_age != -1 && (c->date < revs->max_age))
3591 c->object.flags |= UNINTERESTING;
3593 if (process_parents(revs, c, NULL, NULL) < 0)
3594 return;
3596 if (c->object.flags & UNINTERESTING)
3597 mark_parents_uninteresting(revs, c);
3599 for (p = c->parents; p; p = p->next)
3600 test_flag_and_insert(&info->explore_queue, p->item, TOPO_WALK_EXPLORED);
3603 static void explore_to_depth(struct rev_info *revs,
3604 timestamp_t gen_cutoff)
3606 struct topo_walk_info *info = revs->topo_walk_info;
3607 struct commit *c;
3608 while ((c = prio_queue_peek(&info->explore_queue)) &&
3609 commit_graph_generation(c) >= gen_cutoff)
3610 explore_walk_step(revs);
3613 static void indegree_walk_step(struct rev_info *revs)
3615 struct commit_list *p;
3616 struct topo_walk_info *info = revs->topo_walk_info;
3617 struct commit *c = prio_queue_get(&info->indegree_queue);
3619 if (!c)
3620 return;
3622 if (repo_parse_commit_gently(revs->repo, c, 1) < 0)
3623 return;
3625 count_indegree_walked++;
3627 explore_to_depth(revs, commit_graph_generation(c));
3629 for (p = c->parents; p; p = p->next) {
3630 struct commit *parent = p->item;
3631 int *pi = indegree_slab_at(&info->indegree, parent);
3633 if (repo_parse_commit_gently(revs->repo, parent, 1) < 0)
3634 return;
3636 if (*pi)
3637 (*pi)++;
3638 else
3639 *pi = 2;
3641 test_flag_and_insert(&info->indegree_queue, parent, TOPO_WALK_INDEGREE);
3643 if (revs->first_parent_only)
3644 return;
3648 static void compute_indegrees_to_depth(struct rev_info *revs,
3649 timestamp_t gen_cutoff)
3651 struct topo_walk_info *info = revs->topo_walk_info;
3652 struct commit *c;
3653 while ((c = prio_queue_peek(&info->indegree_queue)) &&
3654 commit_graph_generation(c) >= gen_cutoff)
3655 indegree_walk_step(revs);
3658 static void release_revisions_topo_walk_info(struct topo_walk_info *info)
3660 if (!info)
3661 return;
3662 clear_prio_queue(&info->explore_queue);
3663 clear_prio_queue(&info->indegree_queue);
3664 clear_prio_queue(&info->topo_queue);
3665 clear_indegree_slab(&info->indegree);
3666 clear_author_date_slab(&info->author_date);
3667 free(info);
3670 static void reset_topo_walk(struct rev_info *revs)
3672 release_revisions_topo_walk_info(revs->topo_walk_info);
3673 revs->topo_walk_info = NULL;
3676 static void init_topo_walk(struct rev_info *revs)
3678 struct topo_walk_info *info;
3679 struct commit_list *list;
3680 if (revs->topo_walk_info)
3681 reset_topo_walk(revs);
3683 revs->topo_walk_info = xmalloc(sizeof(struct topo_walk_info));
3684 info = revs->topo_walk_info;
3685 memset(info, 0, sizeof(struct topo_walk_info));
3687 init_indegree_slab(&info->indegree);
3688 memset(&info->explore_queue, 0, sizeof(info->explore_queue));
3689 memset(&info->indegree_queue, 0, sizeof(info->indegree_queue));
3690 memset(&info->topo_queue, 0, sizeof(info->topo_queue));
3692 switch (revs->sort_order) {
3693 default: /* REV_SORT_IN_GRAPH_ORDER */
3694 info->topo_queue.compare = NULL;
3695 break;
3696 case REV_SORT_BY_COMMIT_DATE:
3697 info->topo_queue.compare = compare_commits_by_commit_date;
3698 break;
3699 case REV_SORT_BY_AUTHOR_DATE:
3700 init_author_date_slab(&info->author_date);
3701 info->topo_queue.compare = compare_commits_by_author_date;
3702 info->topo_queue.cb_data = &info->author_date;
3703 break;
3706 info->explore_queue.compare = compare_commits_by_gen_then_commit_date;
3707 info->indegree_queue.compare = compare_commits_by_gen_then_commit_date;
3709 info->min_generation = GENERATION_NUMBER_INFINITY;
3710 for (list = revs->commits; list; list = list->next) {
3711 struct commit *c = list->item;
3712 timestamp_t generation;
3714 if (repo_parse_commit_gently(revs->repo, c, 1))
3715 continue;
3717 test_flag_and_insert(&info->explore_queue, c, TOPO_WALK_EXPLORED);
3718 test_flag_and_insert(&info->indegree_queue, c, TOPO_WALK_INDEGREE);
3720 generation = commit_graph_generation(c);
3721 if (generation < info->min_generation)
3722 info->min_generation = generation;
3724 *(indegree_slab_at(&info->indegree, c)) = 1;
3726 if (revs->sort_order == REV_SORT_BY_AUTHOR_DATE)
3727 record_author_date(&info->author_date, c);
3729 compute_indegrees_to_depth(revs, info->min_generation);
3731 for (list = revs->commits; list; list = list->next) {
3732 struct commit *c = list->item;
3734 if (*(indegree_slab_at(&info->indegree, c)) == 1)
3735 prio_queue_put(&info->topo_queue, c);
3739 * This is unfortunate; the initial tips need to be shown
3740 * in the order given from the revision traversal machinery.
3742 if (revs->sort_order == REV_SORT_IN_GRAPH_ORDER)
3743 prio_queue_reverse(&info->topo_queue);
3745 if (trace2_is_enabled() && !topo_walk_atexit_registered) {
3746 atexit(trace2_topo_walk_statistics_atexit);
3747 topo_walk_atexit_registered = 1;
3751 static struct commit *next_topo_commit(struct rev_info *revs)
3753 struct commit *c;
3754 struct topo_walk_info *info = revs->topo_walk_info;
3756 /* pop next off of topo_queue */
3757 c = prio_queue_get(&info->topo_queue);
3759 if (c)
3760 *(indegree_slab_at(&info->indegree, c)) = 0;
3762 return c;
3765 static void expand_topo_walk(struct rev_info *revs, struct commit *commit)
3767 struct commit_list *p;
3768 struct topo_walk_info *info = revs->topo_walk_info;
3769 if (process_parents(revs, commit, NULL, NULL) < 0) {
3770 if (!revs->ignore_missing_links)
3771 die("Failed to traverse parents of commit %s",
3772 oid_to_hex(&commit->object.oid));
3775 count_topo_walked++;
3777 for (p = commit->parents; p; p = p->next) {
3778 struct commit *parent = p->item;
3779 int *pi;
3780 timestamp_t generation;
3782 if (parent->object.flags & UNINTERESTING)
3783 continue;
3785 if (repo_parse_commit_gently(revs->repo, parent, 1) < 0)
3786 continue;
3788 generation = commit_graph_generation(parent);
3789 if (generation < info->min_generation) {
3790 info->min_generation = generation;
3791 compute_indegrees_to_depth(revs, info->min_generation);
3794 pi = indegree_slab_at(&info->indegree, parent);
3796 (*pi)--;
3797 if (*pi == 1)
3798 prio_queue_put(&info->topo_queue, parent);
3800 if (revs->first_parent_only)
3801 return;
3805 int prepare_revision_walk(struct rev_info *revs)
3807 int i;
3808 struct object_array old_pending;
3809 struct commit_list **next = &revs->commits;
3811 memcpy(&old_pending, &revs->pending, sizeof(old_pending));
3812 revs->pending.nr = 0;
3813 revs->pending.alloc = 0;
3814 revs->pending.objects = NULL;
3815 for (i = 0; i < old_pending.nr; i++) {
3816 struct object_array_entry *e = old_pending.objects + i;
3817 struct commit *commit = handle_commit(revs, e);
3818 if (commit) {
3819 if (!(commit->object.flags & SEEN)) {
3820 commit->object.flags |= SEEN;
3821 next = commit_list_append(commit, next);
3825 object_array_clear(&old_pending);
3827 /* Signal whether we need per-parent treesame decoration */
3828 if (revs->simplify_merges ||
3829 (revs->limited && limiting_can_increase_treesame(revs)))
3830 revs->treesame.name = "treesame";
3832 if (revs->exclude_promisor_objects) {
3833 for_each_packed_object(mark_uninteresting, revs,
3834 FOR_EACH_OBJECT_PROMISOR_ONLY);
3837 oidset_init(&revs->missing_commits, 0);
3839 if (!revs->reflog_info)
3840 prepare_to_use_bloom_filter(revs);
3841 if (!revs->unsorted_input)
3842 commit_list_sort_by_date(&revs->commits);
3843 if (revs->no_walk)
3844 return 0;
3845 if (revs->limited) {
3846 if (limit_list(revs) < 0)
3847 return -1;
3848 if (revs->topo_order)
3849 sort_in_topological_order(&revs->commits, revs->sort_order);
3850 } else if (revs->topo_order)
3851 init_topo_walk(revs);
3852 if (revs->line_level_traverse && want_ancestry(revs))
3854 * At the moment we can only do line-level log with parent
3855 * rewriting by performing this expensive pre-filtering step.
3856 * If parent rewriting is not requested, then we rather
3857 * perform the line-level log filtering during the regular
3858 * history traversal.
3860 line_log_filter(revs);
3861 if (revs->simplify_merges)
3862 simplify_merges(revs);
3863 if (revs->children.name)
3864 set_children(revs);
3866 return 0;
3869 static enum rewrite_result rewrite_one_1(struct rev_info *revs,
3870 struct commit **pp,
3871 struct prio_queue *queue)
3873 for (;;) {
3874 struct commit *p = *pp;
3875 if (!revs->limited)
3876 if (process_parents(revs, p, NULL, queue) < 0)
3877 return rewrite_one_error;
3878 if (p->object.flags & UNINTERESTING)
3879 return rewrite_one_ok;
3880 if (!(p->object.flags & TREESAME))
3881 return rewrite_one_ok;
3882 if (!p->parents)
3883 return rewrite_one_noparents;
3884 if (!(p = one_relevant_parent(revs, p->parents)))
3885 return rewrite_one_ok;
3886 *pp = p;
3890 static void merge_queue_into_list(struct prio_queue *q, struct commit_list **list)
3892 while (q->nr) {
3893 struct commit *item = prio_queue_peek(q);
3894 struct commit_list *p = *list;
3896 if (p && p->item->date >= item->date)
3897 list = &p->next;
3898 else {
3899 p = commit_list_insert(item, list);
3900 list = &p->next; /* skip newly added item */
3901 prio_queue_get(q); /* pop item */
3906 static enum rewrite_result rewrite_one(struct rev_info *revs, struct commit **pp)
3908 struct prio_queue queue = { compare_commits_by_commit_date };
3909 enum rewrite_result ret = rewrite_one_1(revs, pp, &queue);
3910 merge_queue_into_list(&queue, &revs->commits);
3911 clear_prio_queue(&queue);
3912 return ret;
3915 int rewrite_parents(struct rev_info *revs, struct commit *commit,
3916 rewrite_parent_fn_t rewrite_parent)
3918 struct commit_list **pp = &commit->parents;
3919 while (*pp) {
3920 struct commit_list *parent = *pp;
3921 switch (rewrite_parent(revs, &parent->item)) {
3922 case rewrite_one_ok:
3923 break;
3924 case rewrite_one_noparents:
3925 *pp = parent->next;
3926 continue;
3927 case rewrite_one_error:
3928 return -1;
3930 pp = &parent->next;
3932 remove_duplicate_parents(revs, commit);
3933 return 0;
3936 static int commit_match(struct commit *commit, struct rev_info *opt)
3938 int retval;
3939 const char *encoding;
3940 const char *message;
3941 struct strbuf buf = STRBUF_INIT;
3943 if (!opt->grep_filter.pattern_list && !opt->grep_filter.header_list)
3944 return 1;
3946 /* Prepend "fake" headers as needed */
3947 if (opt->grep_filter.use_reflog_filter) {
3948 strbuf_addstr(&buf, "reflog ");
3949 get_reflog_message(&buf, opt->reflog_info);
3950 strbuf_addch(&buf, '\n');
3954 * We grep in the user's output encoding, under the assumption that it
3955 * is the encoding they are most likely to write their grep pattern
3956 * for. In addition, it means we will match the "notes" encoding below,
3957 * so we will not end up with a buffer that has two different encodings
3958 * in it.
3960 encoding = get_log_output_encoding();
3961 message = repo_logmsg_reencode(the_repository, commit, NULL, encoding);
3963 /* Copy the commit to temporary if we are using "fake" headers */
3964 if (buf.len)
3965 strbuf_addstr(&buf, message);
3967 if (opt->grep_filter.header_list && opt->mailmap) {
3968 const char *commit_headers[] = { "author ", "committer ", NULL };
3970 if (!buf.len)
3971 strbuf_addstr(&buf, message);
3973 apply_mailmap_to_header(&buf, commit_headers, opt->mailmap);
3976 /* Append "fake" message parts as needed */
3977 if (opt->show_notes) {
3978 if (!buf.len)
3979 strbuf_addstr(&buf, message);
3980 format_display_notes(&commit->object.oid, &buf, encoding, 1);
3984 * Find either in the original commit message, or in the temporary.
3985 * Note that we cast away the constness of "message" here. It is
3986 * const because it may come from the cached commit buffer. That's OK,
3987 * because we know that it is modifiable heap memory, and that while
3988 * grep_buffer may modify it for speed, it will restore any
3989 * changes before returning.
3991 if (buf.len)
3992 retval = grep_buffer(&opt->grep_filter, buf.buf, buf.len);
3993 else
3994 retval = grep_buffer(&opt->grep_filter,
3995 (char *)message, strlen(message));
3996 strbuf_release(&buf);
3997 repo_unuse_commit_buffer(the_repository, commit, message);
3998 return retval;
4001 static inline int want_ancestry(const struct rev_info *revs)
4003 return (revs->rewrite_parents || revs->children.name);
4007 * Return a timestamp to be used for --since/--until comparisons for this
4008 * commit, based on the revision options.
4010 static timestamp_t comparison_date(const struct rev_info *revs,
4011 struct commit *commit)
4013 return revs->reflog_info ?
4014 get_reflog_timestamp(revs->reflog_info) :
4015 commit->date;
4018 enum commit_action get_commit_action(struct rev_info *revs, struct commit *commit)
4020 if (commit->object.flags & SHOWN)
4021 return commit_ignore;
4022 if (revs->unpacked && has_object_pack(&commit->object.oid))
4023 return commit_ignore;
4024 if (revs->no_kept_objects) {
4025 if (has_object_kept_pack(&commit->object.oid,
4026 revs->keep_pack_cache_flags))
4027 return commit_ignore;
4029 if (commit->object.flags & UNINTERESTING)
4030 return commit_ignore;
4031 if (revs->line_level_traverse && !want_ancestry(revs)) {
4033 * In case of line-level log with parent rewriting
4034 * prepare_revision_walk() already took care of all line-level
4035 * log filtering, and there is nothing left to do here.
4037 * If parent rewriting was not requested, then this is the
4038 * place to perform the line-level log filtering. Notably,
4039 * this check, though expensive, must come before the other,
4040 * cheaper filtering conditions, because the tracked line
4041 * ranges must be adjusted even when the commit will end up
4042 * being ignored based on other conditions.
4044 if (!line_log_process_ranges_arbitrary_commit(revs, commit))
4045 return commit_ignore;
4047 if (revs->min_age != -1 &&
4048 comparison_date(revs, commit) > revs->min_age)
4049 return commit_ignore;
4050 if (revs->max_age_as_filter != -1 &&
4051 comparison_date(revs, commit) < revs->max_age_as_filter)
4052 return commit_ignore;
4053 if (revs->min_parents || (revs->max_parents >= 0)) {
4054 int n = commit_list_count(commit->parents);
4055 if ((n < revs->min_parents) ||
4056 ((revs->max_parents >= 0) && (n > revs->max_parents)))
4057 return commit_ignore;
4059 if (!commit_match(commit, revs))
4060 return commit_ignore;
4061 if (revs->prune && revs->dense) {
4062 /* Commit without changes? */
4063 if (commit->object.flags & TREESAME) {
4064 int n;
4065 struct commit_list *p;
4066 /* drop merges unless we want parenthood */
4067 if (!want_ancestry(revs))
4068 return commit_ignore;
4070 if (revs->show_pulls && (commit->object.flags & PULL_MERGE))
4071 return commit_show;
4074 * If we want ancestry, then need to keep any merges
4075 * between relevant commits to tie together topology.
4076 * For consistency with TREESAME and simplification
4077 * use "relevant" here rather than just INTERESTING,
4078 * to treat bottom commit(s) as part of the topology.
4080 for (n = 0, p = commit->parents; p; p = p->next)
4081 if (relevant_commit(p->item))
4082 if (++n >= 2)
4083 return commit_show;
4084 return commit_ignore;
4087 return commit_show;
4090 define_commit_slab(saved_parents, struct commit_list *);
4092 #define EMPTY_PARENT_LIST ((struct commit_list *)-1)
4095 * You may only call save_parents() once per commit (this is checked
4096 * for non-root commits).
4098 static void save_parents(struct rev_info *revs, struct commit *commit)
4100 struct commit_list **pp;
4102 if (!revs->saved_parents_slab) {
4103 revs->saved_parents_slab = xmalloc(sizeof(struct saved_parents));
4104 init_saved_parents(revs->saved_parents_slab);
4107 pp = saved_parents_at(revs->saved_parents_slab, commit);
4110 * When walking with reflogs, we may visit the same commit
4111 * several times: once for each appearance in the reflog.
4113 * In this case, save_parents() will be called multiple times.
4114 * We want to keep only the first set of parents. We need to
4115 * store a sentinel value for an empty (i.e., NULL) parent
4116 * list to distinguish it from a not-yet-saved list, however.
4118 if (*pp)
4119 return;
4120 if (commit->parents)
4121 *pp = copy_commit_list(commit->parents);
4122 else
4123 *pp = EMPTY_PARENT_LIST;
4126 static void free_saved_parents(struct rev_info *revs)
4128 if (revs->saved_parents_slab)
4129 clear_saved_parents(revs->saved_parents_slab);
4132 struct commit_list *get_saved_parents(struct rev_info *revs, const struct commit *commit)
4134 struct commit_list *parents;
4136 if (!revs->saved_parents_slab)
4137 return commit->parents;
4139 parents = *saved_parents_at(revs->saved_parents_slab, commit);
4140 if (parents == EMPTY_PARENT_LIST)
4141 return NULL;
4142 return parents;
4145 enum commit_action simplify_commit(struct rev_info *revs, struct commit *commit)
4147 enum commit_action action = get_commit_action(revs, commit);
4149 if (action == commit_show &&
4150 revs->prune && revs->dense && want_ancestry(revs)) {
4152 * --full-diff on simplified parents is no good: it
4153 * will show spurious changes from the commits that
4154 * were elided. So we save the parents on the side
4155 * when --full-diff is in effect.
4157 if (revs->full_diff)
4158 save_parents(revs, commit);
4159 if (rewrite_parents(revs, commit, rewrite_one) < 0)
4160 return commit_error;
4162 return action;
4165 static void track_linear(struct rev_info *revs, struct commit *commit)
4167 if (revs->track_first_time) {
4168 revs->linear = 1;
4169 revs->track_first_time = 0;
4170 } else {
4171 struct commit_list *p;
4172 for (p = revs->previous_parents; p; p = p->next)
4173 if (p->item == NULL || /* first commit */
4174 oideq(&p->item->object.oid, &commit->object.oid))
4175 break;
4176 revs->linear = p != NULL;
4178 if (revs->reverse) {
4179 if (revs->linear)
4180 commit->object.flags |= TRACK_LINEAR;
4182 free_commit_list(revs->previous_parents);
4183 revs->previous_parents = copy_commit_list(commit->parents);
4186 static struct commit *get_revision_1(struct rev_info *revs)
4188 while (1) {
4189 struct commit *commit;
4191 if (revs->reflog_info)
4192 commit = next_reflog_entry(revs->reflog_info);
4193 else if (revs->topo_walk_info)
4194 commit = next_topo_commit(revs);
4195 else
4196 commit = pop_commit(&revs->commits);
4198 if (!commit)
4199 return NULL;
4201 if (revs->reflog_info)
4202 commit->object.flags &= ~(ADDED | SEEN | SHOWN);
4205 * If we haven't done the list limiting, we need to look at
4206 * the parents here. We also need to do the date-based limiting
4207 * that we'd otherwise have done in limit_list().
4209 if (!revs->limited) {
4210 if (revs->max_age != -1 &&
4211 comparison_date(revs, commit) < revs->max_age)
4212 continue;
4214 if (revs->reflog_info)
4215 try_to_simplify_commit(revs, commit);
4216 else if (revs->topo_walk_info)
4217 expand_topo_walk(revs, commit);
4218 else if (process_parents(revs, commit, &revs->commits, NULL) < 0) {
4219 if (!revs->ignore_missing_links)
4220 die("Failed to traverse parents of commit %s",
4221 oid_to_hex(&commit->object.oid));
4225 switch (simplify_commit(revs, commit)) {
4226 case commit_ignore:
4227 continue;
4228 case commit_error:
4229 die("Failed to simplify parents of commit %s",
4230 oid_to_hex(&commit->object.oid));
4231 default:
4232 if (revs->track_linear)
4233 track_linear(revs, commit);
4234 return commit;
4240 * Return true for entries that have not yet been shown. (This is an
4241 * object_array_each_func_t.)
4243 static int entry_unshown(struct object_array_entry *entry, void *cb_data UNUSED)
4245 return !(entry->item->flags & SHOWN);
4249 * If array is on the verge of a realloc, garbage-collect any entries
4250 * that have already been shown to try to free up some space.
4252 static void gc_boundary(struct object_array *array)
4254 if (array->nr == array->alloc)
4255 object_array_filter(array, entry_unshown, NULL);
4258 static void create_boundary_commit_list(struct rev_info *revs)
4260 unsigned i;
4261 struct commit *c;
4262 struct object_array *array = &revs->boundary_commits;
4263 struct object_array_entry *objects = array->objects;
4266 * If revs->commits is non-NULL at this point, an error occurred in
4267 * get_revision_1(). Ignore the error and continue printing the
4268 * boundary commits anyway. (This is what the code has always
4269 * done.)
4271 free_commit_list(revs->commits);
4272 revs->commits = NULL;
4275 * Put all of the actual boundary commits from revs->boundary_commits
4276 * into revs->commits
4278 for (i = 0; i < array->nr; i++) {
4279 c = (struct commit *)(objects[i].item);
4280 if (!c)
4281 continue;
4282 if (!(c->object.flags & CHILD_SHOWN))
4283 continue;
4284 if (c->object.flags & (SHOWN | BOUNDARY))
4285 continue;
4286 c->object.flags |= BOUNDARY;
4287 commit_list_insert(c, &revs->commits);
4291 * If revs->topo_order is set, sort the boundary commits
4292 * in topological order
4294 sort_in_topological_order(&revs->commits, revs->sort_order);
4297 static struct commit *get_revision_internal(struct rev_info *revs)
4299 struct commit *c = NULL;
4300 struct commit_list *l;
4302 if (revs->boundary == 2) {
4304 * All of the normal commits have already been returned,
4305 * and we are now returning boundary commits.
4306 * create_boundary_commit_list() has populated
4307 * revs->commits with the remaining commits to return.
4309 c = pop_commit(&revs->commits);
4310 if (c)
4311 c->object.flags |= SHOWN;
4312 return c;
4316 * If our max_count counter has reached zero, then we are done. We
4317 * don't simply return NULL because we still might need to show
4318 * boundary commits. But we want to avoid calling get_revision_1, which
4319 * might do a considerable amount of work finding the next commit only
4320 * for us to throw it away.
4322 * If it is non-zero, then either we don't have a max_count at all
4323 * (-1), or it is still counting, in which case we decrement.
4325 if (revs->max_count) {
4326 c = get_revision_1(revs);
4327 if (c) {
4328 while (revs->skip_count > 0) {
4329 revs->skip_count--;
4330 c = get_revision_1(revs);
4331 if (!c)
4332 break;
4336 if (revs->max_count > 0)
4337 revs->max_count--;
4340 if (c)
4341 c->object.flags |= SHOWN;
4343 if (!revs->boundary)
4344 return c;
4346 if (!c) {
4348 * get_revision_1() runs out the commits, and
4349 * we are done computing the boundaries.
4350 * switch to boundary commits output mode.
4352 revs->boundary = 2;
4355 * Update revs->commits to contain the list of
4356 * boundary commits.
4358 create_boundary_commit_list(revs);
4360 return get_revision_internal(revs);
4364 * boundary commits are the commits that are parents of the
4365 * ones we got from get_revision_1() but they themselves are
4366 * not returned from get_revision_1(). Before returning
4367 * 'c', we need to mark its parents that they could be boundaries.
4370 for (l = c->parents; l; l = l->next) {
4371 struct object *p;
4372 p = &(l->item->object);
4373 if (p->flags & (CHILD_SHOWN | SHOWN))
4374 continue;
4375 p->flags |= CHILD_SHOWN;
4376 gc_boundary(&revs->boundary_commits);
4377 add_object_array(p, NULL, &revs->boundary_commits);
4380 return c;
4383 struct commit *get_revision(struct rev_info *revs)
4385 struct commit *c;
4386 struct commit_list *reversed;
4388 if (revs->reverse) {
4389 reversed = NULL;
4390 while ((c = get_revision_internal(revs)))
4391 commit_list_insert(c, &reversed);
4392 revs->commits = reversed;
4393 revs->reverse = 0;
4394 revs->reverse_output_stage = 1;
4397 if (revs->reverse_output_stage) {
4398 c = pop_commit(&revs->commits);
4399 if (revs->track_linear)
4400 revs->linear = !!(c && c->object.flags & TRACK_LINEAR);
4401 return c;
4404 c = get_revision_internal(revs);
4405 if (c && revs->graph)
4406 graph_update(revs->graph, c);
4407 if (!c) {
4408 free_saved_parents(revs);
4409 free_commit_list(revs->previous_parents);
4410 revs->previous_parents = NULL;
4412 return c;
4415 const char *get_revision_mark(const struct rev_info *revs, const struct commit *commit)
4417 if (commit->object.flags & BOUNDARY)
4418 return "-";
4419 else if (commit->object.flags & UNINTERESTING)
4420 return "^";
4421 else if (commit->object.flags & PATCHSAME)
4422 return "=";
4423 else if (!revs || revs->left_right) {
4424 if (commit->object.flags & SYMMETRIC_LEFT)
4425 return "<";
4426 else
4427 return ">";
4428 } else if (revs->graph)
4429 return "*";
4430 else if (revs->cherry_mark)
4431 return "+";
4432 return "";
4435 void put_revision_mark(const struct rev_info *revs, const struct commit *commit)
4437 const char *mark = get_revision_mark(revs, commit);
4438 if (!strlen(mark))
4439 return;
4440 fputs(mark, stdout);
4441 putchar(' ');