line-range: plug leaking find functions
[git/gitster.git] / revision.c
blob99c75c939de3644146fde07101a3330d759ae4da
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->object.oid, 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->object.oid, 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 |
385 PARSE_OBJECT_DISCARD_TREE);
387 if (!object) {
388 if (revs->ignore_missing)
389 return NULL;
390 if (revs->exclude_promisor_objects && is_promisor_object(oid))
391 return NULL;
392 if (revs->do_not_die_on_missing_objects) {
393 oidset_insert(&revs->missing_commits, oid);
394 return NULL;
396 die("bad object %s", name);
398 object->flags |= flags;
399 return object;
402 void add_pending_oid(struct rev_info *revs, const char *name,
403 const struct object_id *oid, unsigned int flags)
405 struct object *object = get_reference(revs, name, oid, flags);
406 add_pending_object(revs, object, name);
409 static struct commit *handle_commit(struct rev_info *revs,
410 struct object_array_entry *entry)
412 struct object *object = entry->item;
413 const char *name = entry->name;
414 const char *path = entry->path;
415 unsigned int mode = entry->mode;
416 unsigned long flags = object->flags;
419 * Tag object? Look what it points to..
421 while (object->type == OBJ_TAG) {
422 struct tag *tag = (struct tag *) object;
423 struct object_id *oid;
424 if (revs->tag_objects && !(flags & UNINTERESTING))
425 add_pending_object(revs, object, tag->tag);
426 oid = get_tagged_oid(tag);
427 object = parse_object(revs->repo, oid);
428 if (!object) {
429 if (revs->ignore_missing_links || (flags & UNINTERESTING))
430 return NULL;
431 if (revs->exclude_promisor_objects &&
432 is_promisor_object(&tag->tagged->oid))
433 return NULL;
434 if (revs->do_not_die_on_missing_objects && oid) {
435 oidset_insert(&revs->missing_commits, oid);
436 return NULL;
438 die("bad object %s", oid_to_hex(&tag->tagged->oid));
440 object->flags |= flags;
442 * We'll handle the tagged object by looping or dropping
443 * through to the non-tag handlers below. Do not
444 * propagate path data from the tag's pending entry.
446 path = NULL;
447 mode = 0;
451 * Commit object? Just return it, we'll do all the complex
452 * reachability crud.
454 if (object->type == OBJ_COMMIT) {
455 struct commit *commit = (struct commit *)object;
457 if (repo_parse_commit(revs->repo, commit) < 0)
458 die("unable to parse commit %s", name);
459 if (flags & UNINTERESTING) {
460 mark_parents_uninteresting(revs, commit);
462 if (!revs->topo_order || !generation_numbers_enabled(the_repository))
463 revs->limited = 1;
465 if (revs->sources) {
466 char **slot = revision_sources_at(revs->sources, commit);
468 if (!*slot)
469 *slot = xstrdup(name);
471 return commit;
475 * Tree object? Either mark it uninteresting, or add it
476 * to the list of objects to look at later..
478 if (object->type == OBJ_TREE) {
479 struct tree *tree = (struct tree *)object;
480 if (!revs->tree_objects)
481 return NULL;
482 if (flags & UNINTERESTING) {
483 mark_tree_contents_uninteresting(revs->repo, tree);
484 return NULL;
486 add_pending_object_with_path(revs, object, name, mode, path);
487 return NULL;
491 * Blob object? You know the drill by now..
493 if (object->type == OBJ_BLOB) {
494 if (!revs->blob_objects)
495 return NULL;
496 if (flags & UNINTERESTING)
497 return NULL;
498 add_pending_object_with_path(revs, object, name, mode, path);
499 return NULL;
501 die("%s is unknown object", name);
504 static int everybody_uninteresting(struct commit_list *orig,
505 struct commit **interesting_cache)
507 struct commit_list *list = orig;
509 if (*interesting_cache) {
510 struct commit *commit = *interesting_cache;
511 if (!(commit->object.flags & UNINTERESTING))
512 return 0;
515 while (list) {
516 struct commit *commit = list->item;
517 list = list->next;
518 if (commit->object.flags & UNINTERESTING)
519 continue;
521 *interesting_cache = commit;
522 return 0;
524 return 1;
528 * A definition of "relevant" commit that we can use to simplify limited graphs
529 * by eliminating side branches.
531 * A "relevant" commit is one that is !UNINTERESTING (ie we are including it
532 * in our list), or that is a specified BOTTOM commit. Then after computing
533 * a limited list, during processing we can generally ignore boundary merges
534 * coming from outside the graph, (ie from irrelevant parents), and treat
535 * those merges as if they were single-parent. TREESAME is defined to consider
536 * only relevant parents, if any. If we are TREESAME to our on-graph parents,
537 * we don't care if we were !TREESAME to non-graph parents.
539 * Treating bottom commits as relevant ensures that a limited graph's
540 * connection to the actual bottom commit is not viewed as a side branch, but
541 * treated as part of the graph. For example:
543 * ....Z...A---X---o---o---B
544 * . /
545 * W---Y
547 * When computing "A..B", the A-X connection is at least as important as
548 * Y-X, despite A being flagged UNINTERESTING.
550 * And when computing --ancestry-path "A..B", the A-X connection is more
551 * important than Y-X, despite both A and Y being flagged UNINTERESTING.
553 static inline int relevant_commit(struct commit *commit)
555 return (commit->object.flags & (UNINTERESTING | BOTTOM)) != UNINTERESTING;
559 * Return a single relevant commit from a parent list. If we are a TREESAME
560 * commit, and this selects one of our parents, then we can safely simplify to
561 * that parent.
563 static struct commit *one_relevant_parent(const struct rev_info *revs,
564 struct commit_list *orig)
566 struct commit_list *list = orig;
567 struct commit *relevant = NULL;
569 if (!orig)
570 return NULL;
573 * For 1-parent commits, or if first-parent-only, then return that
574 * first parent (even if not "relevant" by the above definition).
575 * TREESAME will have been set purely on that parent.
577 if (revs->first_parent_only || !orig->next)
578 return orig->item;
581 * For multi-parent commits, identify a sole relevant parent, if any.
582 * If we have only one relevant parent, then TREESAME will be set purely
583 * with regard to that parent, and we can simplify accordingly.
585 * If we have more than one relevant parent, or no relevant parents
586 * (and multiple irrelevant ones), then we can't select a parent here
587 * and return NULL.
589 while (list) {
590 struct commit *commit = list->item;
591 list = list->next;
592 if (relevant_commit(commit)) {
593 if (relevant)
594 return NULL;
595 relevant = commit;
598 return relevant;
602 * The goal is to get REV_TREE_NEW as the result only if the
603 * diff consists of all '+' (and no other changes), REV_TREE_OLD
604 * if the whole diff is removal of old data, and otherwise
605 * REV_TREE_DIFFERENT (of course if the trees are the same we
606 * want REV_TREE_SAME).
608 * The only time we care about the distinction is when
609 * remove_empty_trees is in effect, in which case we care only about
610 * whether the whole change is REV_TREE_NEW, or if there's another type
611 * of change. Which means we can stop the diff early in either of these
612 * cases:
614 * 1. We're not using remove_empty_trees at all.
616 * 2. We saw anything except REV_TREE_NEW.
618 #define REV_TREE_SAME 0
619 #define REV_TREE_NEW 1 /* Only new files */
620 #define REV_TREE_OLD 2 /* Only files removed */
621 #define REV_TREE_DIFFERENT 3 /* Mixed changes */
622 static int tree_difference = REV_TREE_SAME;
624 static void file_add_remove(struct diff_options *options,
625 int addremove,
626 unsigned mode UNUSED,
627 const struct object_id *oid UNUSED,
628 int oid_valid UNUSED,
629 const char *fullpath UNUSED,
630 unsigned dirty_submodule UNUSED)
632 int diff = addremove == '+' ? REV_TREE_NEW : REV_TREE_OLD;
633 struct rev_info *revs = options->change_fn_data;
635 tree_difference |= diff;
636 if (!revs->remove_empty_trees || tree_difference != REV_TREE_NEW)
637 options->flags.has_changes = 1;
640 static void file_change(struct diff_options *options,
641 unsigned old_mode UNUSED,
642 unsigned new_mode UNUSED,
643 const struct object_id *old_oid UNUSED,
644 const struct object_id *new_oid UNUSED,
645 int old_oid_valid UNUSED,
646 int new_oid_valid UNUSED,
647 const char *fullpath UNUSED,
648 unsigned old_dirty_submodule UNUSED,
649 unsigned new_dirty_submodule UNUSED)
651 tree_difference = REV_TREE_DIFFERENT;
652 options->flags.has_changes = 1;
655 static int bloom_filter_atexit_registered;
656 static unsigned int count_bloom_filter_maybe;
657 static unsigned int count_bloom_filter_definitely_not;
658 static unsigned int count_bloom_filter_false_positive;
659 static unsigned int count_bloom_filter_not_present;
661 static void trace2_bloom_filter_statistics_atexit(void)
663 struct json_writer jw = JSON_WRITER_INIT;
665 jw_object_begin(&jw, 0);
666 jw_object_intmax(&jw, "filter_not_present", count_bloom_filter_not_present);
667 jw_object_intmax(&jw, "maybe", count_bloom_filter_maybe);
668 jw_object_intmax(&jw, "definitely_not", count_bloom_filter_definitely_not);
669 jw_object_intmax(&jw, "false_positive", count_bloom_filter_false_positive);
670 jw_end(&jw);
672 trace2_data_json("bloom", the_repository, "statistics", &jw);
674 jw_release(&jw);
677 static int forbid_bloom_filters(struct pathspec *spec)
679 if (spec->has_wildcard)
680 return 1;
681 if (spec->nr > 1)
682 return 1;
683 if (spec->magic & ~PATHSPEC_LITERAL)
684 return 1;
685 if (spec->nr && (spec->items[0].magic & ~PATHSPEC_LITERAL))
686 return 1;
688 return 0;
691 static void prepare_to_use_bloom_filter(struct rev_info *revs)
693 struct pathspec_item *pi;
694 char *path_alloc = NULL;
695 const char *path, *p;
696 size_t len;
697 int path_component_nr = 1;
699 if (!revs->commits)
700 return;
702 if (forbid_bloom_filters(&revs->prune_data))
703 return;
705 repo_parse_commit(revs->repo, revs->commits->item);
707 revs->bloom_filter_settings = get_bloom_filter_settings(revs->repo);
708 if (!revs->bloom_filter_settings)
709 return;
711 if (!revs->pruning.pathspec.nr)
712 return;
714 pi = &revs->pruning.pathspec.items[0];
716 /* remove single trailing slash from path, if needed */
717 if (pi->len > 0 && pi->match[pi->len - 1] == '/') {
718 path_alloc = xmemdupz(pi->match, pi->len - 1);
719 path = path_alloc;
720 } else
721 path = pi->match;
723 len = strlen(path);
724 if (!len) {
725 revs->bloom_filter_settings = NULL;
726 free(path_alloc);
727 return;
730 p = path;
731 while (*p) {
733 * At this point, the path is normalized to use Unix-style
734 * path separators. This is required due to how the
735 * changed-path Bloom filters store the paths.
737 if (*p == '/')
738 path_component_nr++;
739 p++;
742 revs->bloom_keys_nr = path_component_nr;
743 ALLOC_ARRAY(revs->bloom_keys, revs->bloom_keys_nr);
745 fill_bloom_key(path, len, &revs->bloom_keys[0],
746 revs->bloom_filter_settings);
747 path_component_nr = 1;
749 p = path + len - 1;
750 while (p > path) {
751 if (*p == '/')
752 fill_bloom_key(path, p - path,
753 &revs->bloom_keys[path_component_nr++],
754 revs->bloom_filter_settings);
755 p--;
758 if (trace2_is_enabled() && !bloom_filter_atexit_registered) {
759 atexit(trace2_bloom_filter_statistics_atexit);
760 bloom_filter_atexit_registered = 1;
763 free(path_alloc);
766 static int check_maybe_different_in_bloom_filter(struct rev_info *revs,
767 struct commit *commit)
769 struct bloom_filter *filter;
770 int result = 1, j;
772 if (!revs->repo->objects->commit_graph)
773 return -1;
775 if (commit_graph_generation(commit) == GENERATION_NUMBER_INFINITY)
776 return -1;
778 filter = get_bloom_filter(revs->repo, commit);
780 if (!filter) {
781 count_bloom_filter_not_present++;
782 return -1;
785 for (j = 0; result && j < revs->bloom_keys_nr; j++) {
786 result = bloom_filter_contains(filter,
787 &revs->bloom_keys[j],
788 revs->bloom_filter_settings);
791 if (result)
792 count_bloom_filter_maybe++;
793 else
794 count_bloom_filter_definitely_not++;
796 return result;
799 static int rev_compare_tree(struct rev_info *revs,
800 struct commit *parent, struct commit *commit, int nth_parent)
802 struct tree *t1 = repo_get_commit_tree(the_repository, parent);
803 struct tree *t2 = repo_get_commit_tree(the_repository, commit);
804 int bloom_ret = 1;
806 if (!t1)
807 return REV_TREE_NEW;
808 if (!t2)
809 return REV_TREE_OLD;
811 if (revs->simplify_by_decoration) {
813 * If we are simplifying by decoration, then the commit
814 * is worth showing if it has a tag pointing at it.
816 if (get_name_decoration(&commit->object))
817 return REV_TREE_DIFFERENT;
819 * A commit that is not pointed by a tag is uninteresting
820 * if we are not limited by path. This means that you will
821 * see the usual "commits that touch the paths" plus any
822 * tagged commit by specifying both --simplify-by-decoration
823 * and pathspec.
825 if (!revs->prune_data.nr)
826 return REV_TREE_SAME;
829 if (revs->bloom_keys_nr && !nth_parent) {
830 bloom_ret = check_maybe_different_in_bloom_filter(revs, commit);
832 if (bloom_ret == 0)
833 return REV_TREE_SAME;
836 tree_difference = REV_TREE_SAME;
837 revs->pruning.flags.has_changes = 0;
838 diff_tree_oid(&t1->object.oid, &t2->object.oid, "", &revs->pruning);
840 if (!nth_parent)
841 if (bloom_ret == 1 && tree_difference == REV_TREE_SAME)
842 count_bloom_filter_false_positive++;
844 return tree_difference;
847 static int rev_same_tree_as_empty(struct rev_info *revs, struct commit *commit)
849 struct tree *t1 = repo_get_commit_tree(the_repository, commit);
851 if (!t1)
852 return 0;
854 tree_difference = REV_TREE_SAME;
855 revs->pruning.flags.has_changes = 0;
856 diff_tree_oid(NULL, &t1->object.oid, "", &revs->pruning);
858 return tree_difference == REV_TREE_SAME;
861 struct treesame_state {
862 unsigned int nparents;
863 unsigned char treesame[FLEX_ARRAY];
866 static struct treesame_state *initialise_treesame(struct rev_info *revs, struct commit *commit)
868 unsigned n = commit_list_count(commit->parents);
869 struct treesame_state *st = xcalloc(1, st_add(sizeof(*st), n));
870 st->nparents = n;
871 add_decoration(&revs->treesame, &commit->object, st);
872 return st;
876 * Must be called immediately after removing the nth_parent from a commit's
877 * parent list, if we are maintaining the per-parent treesame[] decoration.
878 * This does not recalculate the master TREESAME flag - update_treesame()
879 * should be called to update it after a sequence of treesame[] modifications
880 * that may have affected it.
882 static int compact_treesame(struct rev_info *revs, struct commit *commit, unsigned nth_parent)
884 struct treesame_state *st;
885 int old_same;
887 if (!commit->parents) {
889 * Have just removed the only parent from a non-merge.
890 * Different handling, as we lack decoration.
892 if (nth_parent != 0)
893 die("compact_treesame %u", nth_parent);
894 old_same = !!(commit->object.flags & TREESAME);
895 if (rev_same_tree_as_empty(revs, commit))
896 commit->object.flags |= TREESAME;
897 else
898 commit->object.flags &= ~TREESAME;
899 return old_same;
902 st = lookup_decoration(&revs->treesame, &commit->object);
903 if (!st || nth_parent >= st->nparents)
904 die("compact_treesame %u", nth_parent);
906 old_same = st->treesame[nth_parent];
907 memmove(st->treesame + nth_parent,
908 st->treesame + nth_parent + 1,
909 st->nparents - nth_parent - 1);
912 * If we've just become a non-merge commit, update TREESAME
913 * immediately, and remove the no-longer-needed decoration.
914 * If still a merge, defer update until update_treesame().
916 if (--st->nparents == 1) {
917 if (commit->parents->next)
918 die("compact_treesame parents mismatch");
919 if (st->treesame[0] && revs->dense)
920 commit->object.flags |= TREESAME;
921 else
922 commit->object.flags &= ~TREESAME;
923 free(add_decoration(&revs->treesame, &commit->object, NULL));
926 return old_same;
929 static unsigned update_treesame(struct rev_info *revs, struct commit *commit)
931 if (commit->parents && commit->parents->next) {
932 unsigned n;
933 struct treesame_state *st;
934 struct commit_list *p;
935 unsigned relevant_parents;
936 unsigned relevant_change, irrelevant_change;
938 st = lookup_decoration(&revs->treesame, &commit->object);
939 if (!st)
940 die("update_treesame %s", oid_to_hex(&commit->object.oid));
941 relevant_parents = 0;
942 relevant_change = irrelevant_change = 0;
943 for (p = commit->parents, n = 0; p; n++, p = p->next) {
944 if (relevant_commit(p->item)) {
945 relevant_change |= !st->treesame[n];
946 relevant_parents++;
947 } else
948 irrelevant_change |= !st->treesame[n];
950 if (relevant_parents ? relevant_change : irrelevant_change)
951 commit->object.flags &= ~TREESAME;
952 else
953 commit->object.flags |= TREESAME;
956 return commit->object.flags & TREESAME;
959 static inline int limiting_can_increase_treesame(const struct rev_info *revs)
962 * TREESAME is irrelevant unless prune && dense;
963 * if simplify_history is set, we can't have a mixture of TREESAME and
964 * !TREESAME INTERESTING parents (and we don't have treesame[]
965 * decoration anyway);
966 * if first_parent_only is set, then the TREESAME flag is locked
967 * against the first parent (and again we lack treesame[] decoration).
969 return revs->prune && revs->dense &&
970 !revs->simplify_history &&
971 !revs->first_parent_only;
974 static void try_to_simplify_commit(struct rev_info *revs, struct commit *commit)
976 struct commit_list **pp, *parent;
977 struct treesame_state *ts = NULL;
978 int relevant_change = 0, irrelevant_change = 0;
979 int relevant_parents, nth_parent;
982 * If we don't do pruning, everything is interesting
984 if (!revs->prune)
985 return;
987 if (!repo_get_commit_tree(the_repository, commit))
988 return;
990 if (!commit->parents) {
991 if (rev_same_tree_as_empty(revs, commit))
992 commit->object.flags |= TREESAME;
993 return;
997 * Normal non-merge commit? If we don't want to make the
998 * history dense, we consider it always to be a change..
1000 if (!revs->dense && !commit->parents->next)
1001 return;
1003 for (pp = &commit->parents, nth_parent = 0, relevant_parents = 0;
1004 (parent = *pp) != NULL;
1005 pp = &parent->next, nth_parent++) {
1006 struct commit *p = parent->item;
1007 if (relevant_commit(p))
1008 relevant_parents++;
1010 if (nth_parent == 1) {
1012 * This our second loop iteration - so we now know
1013 * we're dealing with a merge.
1015 * Do not compare with later parents when we care only about
1016 * the first parent chain, in order to avoid derailing the
1017 * traversal to follow a side branch that brought everything
1018 * in the path we are limited to by the pathspec.
1020 if (revs->first_parent_only)
1021 break;
1023 * If this will remain a potentially-simplifiable
1024 * merge, remember per-parent treesame if needed.
1025 * Initialise the array with the comparison from our
1026 * first iteration.
1028 if (revs->treesame.name &&
1029 !revs->simplify_history &&
1030 !(commit->object.flags & UNINTERESTING)) {
1031 ts = initialise_treesame(revs, commit);
1032 if (!(irrelevant_change || relevant_change))
1033 ts->treesame[0] = 1;
1036 if (repo_parse_commit(revs->repo, p) < 0)
1037 die("cannot simplify commit %s (because of %s)",
1038 oid_to_hex(&commit->object.oid),
1039 oid_to_hex(&p->object.oid));
1040 switch (rev_compare_tree(revs, p, commit, nth_parent)) {
1041 case REV_TREE_SAME:
1042 if (!revs->simplify_history || !relevant_commit(p)) {
1043 /* Even if a merge with an uninteresting
1044 * side branch brought the entire change
1045 * we are interested in, we do not want
1046 * to lose the other branches of this
1047 * merge, so we just keep going.
1049 if (ts)
1050 ts->treesame[nth_parent] = 1;
1051 continue;
1053 parent->next = NULL;
1054 commit->parents = parent;
1057 * A merge commit is a "diversion" if it is not
1058 * TREESAME to its first parent but is TREESAME
1059 * to a later parent. In the simplified history,
1060 * we "divert" the history walk to the later
1061 * parent. These commits are shown when "show_pulls"
1062 * is enabled, so do not mark the object as
1063 * TREESAME here.
1065 if (!revs->show_pulls || !nth_parent)
1066 commit->object.flags |= TREESAME;
1068 return;
1070 case REV_TREE_NEW:
1071 if (revs->remove_empty_trees &&
1072 rev_same_tree_as_empty(revs, p)) {
1073 /* We are adding all the specified
1074 * paths from this parent, so the
1075 * history beyond this parent is not
1076 * interesting. Remove its parents
1077 * (they are grandparents for us).
1078 * IOW, we pretend this parent is a
1079 * "root" commit.
1081 if (repo_parse_commit(revs->repo, p) < 0)
1082 die("cannot simplify commit %s (invalid %s)",
1083 oid_to_hex(&commit->object.oid),
1084 oid_to_hex(&p->object.oid));
1085 p->parents = NULL;
1087 /* fallthrough */
1088 case REV_TREE_OLD:
1089 case REV_TREE_DIFFERENT:
1090 if (relevant_commit(p))
1091 relevant_change = 1;
1092 else
1093 irrelevant_change = 1;
1095 if (!nth_parent)
1096 commit->object.flags |= PULL_MERGE;
1098 continue;
1100 die("bad tree compare for commit %s", oid_to_hex(&commit->object.oid));
1104 * TREESAME is straightforward for single-parent commits. For merge
1105 * commits, it is most useful to define it so that "irrelevant"
1106 * parents cannot make us !TREESAME - if we have any relevant
1107 * parents, then we only consider TREESAMEness with respect to them,
1108 * allowing irrelevant merges from uninteresting branches to be
1109 * simplified away. Only if we have only irrelevant parents do we
1110 * base TREESAME on them. Note that this logic is replicated in
1111 * update_treesame, which should be kept in sync.
1113 if (relevant_parents ? !relevant_change : !irrelevant_change)
1114 commit->object.flags |= TREESAME;
1117 static int process_parents(struct rev_info *revs, struct commit *commit,
1118 struct commit_list **list, struct prio_queue *queue)
1120 struct commit_list *parent = commit->parents;
1121 unsigned pass_flags;
1123 if (commit->object.flags & ADDED)
1124 return 0;
1125 if (revs->do_not_die_on_missing_objects &&
1126 oidset_contains(&revs->missing_commits, &commit->object.oid))
1127 return 0;
1128 commit->object.flags |= ADDED;
1130 if (revs->include_check &&
1131 !revs->include_check(commit, revs->include_check_data))
1132 return 0;
1135 * If the commit is uninteresting, don't try to
1136 * prune parents - we want the maximal uninteresting
1137 * set.
1139 * Normally we haven't parsed the parent
1140 * yet, so we won't have a parent of a parent
1141 * here. However, it may turn out that we've
1142 * reached this commit some other way (where it
1143 * wasn't uninteresting), in which case we need
1144 * to mark its parents recursively too..
1146 if (commit->object.flags & UNINTERESTING) {
1147 while (parent) {
1148 struct commit *p = parent->item;
1149 parent = parent->next;
1150 if (p)
1151 p->object.flags |= UNINTERESTING;
1152 if (repo_parse_commit_gently(revs->repo, p, 1) < 0)
1153 continue;
1154 if (p->parents)
1155 mark_parents_uninteresting(revs, p);
1156 if (p->object.flags & SEEN)
1157 continue;
1158 p->object.flags |= (SEEN | NOT_USER_GIVEN);
1159 if (list)
1160 commit_list_insert_by_date(p, list);
1161 if (queue)
1162 prio_queue_put(queue, p);
1163 if (revs->exclude_first_parent_only)
1164 break;
1166 return 0;
1170 * Ok, the commit wasn't uninteresting. Try to
1171 * simplify the commit history and find the parent
1172 * that has no differences in the path set if one exists.
1174 try_to_simplify_commit(revs, commit);
1176 if (revs->no_walk)
1177 return 0;
1179 pass_flags = (commit->object.flags & (SYMMETRIC_LEFT | ANCESTRY_PATH));
1181 for (parent = commit->parents; parent; parent = parent->next) {
1182 struct commit *p = parent->item;
1183 int gently = revs->ignore_missing_links ||
1184 revs->exclude_promisor_objects ||
1185 revs->do_not_die_on_missing_objects;
1186 if (repo_parse_commit_gently(revs->repo, p, gently) < 0) {
1187 if (revs->exclude_promisor_objects &&
1188 is_promisor_object(&p->object.oid)) {
1189 if (revs->first_parent_only)
1190 break;
1191 continue;
1194 if (revs->do_not_die_on_missing_objects)
1195 oidset_insert(&revs->missing_commits, &p->object.oid);
1196 else
1197 return -1; /* corrupt repository */
1199 if (revs->sources) {
1200 char **slot = revision_sources_at(revs->sources, p);
1202 if (!*slot)
1203 *slot = *revision_sources_at(revs->sources, commit);
1205 p->object.flags |= pass_flags;
1206 if (!(p->object.flags & SEEN)) {
1207 p->object.flags |= (SEEN | NOT_USER_GIVEN);
1208 if (list)
1209 commit_list_insert_by_date(p, list);
1210 if (queue)
1211 prio_queue_put(queue, p);
1213 if (revs->first_parent_only)
1214 break;
1216 return 0;
1219 static void cherry_pick_list(struct commit_list *list, struct rev_info *revs)
1221 struct commit_list *p;
1222 int left_count = 0, right_count = 0;
1223 int left_first;
1224 struct patch_ids ids;
1225 unsigned cherry_flag;
1227 /* First count the commits on the left and on the right */
1228 for (p = list; p; p = p->next) {
1229 struct commit *commit = p->item;
1230 unsigned flags = commit->object.flags;
1231 if (flags & BOUNDARY)
1233 else if (flags & SYMMETRIC_LEFT)
1234 left_count++;
1235 else
1236 right_count++;
1239 if (!left_count || !right_count)
1240 return;
1242 left_first = left_count < right_count;
1243 init_patch_ids(revs->repo, &ids);
1244 ids.diffopts.pathspec = revs->diffopt.pathspec;
1246 /* Compute patch-ids for one side */
1247 for (p = list; p; p = p->next) {
1248 struct commit *commit = p->item;
1249 unsigned flags = commit->object.flags;
1251 if (flags & BOUNDARY)
1252 continue;
1254 * If we have fewer left, left_first is set and we omit
1255 * commits on the right branch in this loop. If we have
1256 * fewer right, we skip the left ones.
1258 if (left_first != !!(flags & SYMMETRIC_LEFT))
1259 continue;
1260 add_commit_patch_id(commit, &ids);
1263 /* either cherry_mark or cherry_pick are true */
1264 cherry_flag = revs->cherry_mark ? PATCHSAME : SHOWN;
1266 /* Check the other side */
1267 for (p = list; p; p = p->next) {
1268 struct commit *commit = p->item;
1269 struct patch_id *id;
1270 unsigned flags = commit->object.flags;
1272 if (flags & BOUNDARY)
1273 continue;
1275 * If we have fewer left, left_first is set and we omit
1276 * commits on the left branch in this loop.
1278 if (left_first == !!(flags & SYMMETRIC_LEFT))
1279 continue;
1282 * Have we seen the same patch id?
1284 id = patch_id_iter_first(commit, &ids);
1285 if (!id)
1286 continue;
1288 commit->object.flags |= cherry_flag;
1289 do {
1290 id->commit->object.flags |= cherry_flag;
1291 } while ((id = patch_id_iter_next(id, &ids)));
1294 free_patch_ids(&ids);
1297 /* How many extra uninteresting commits we want to see.. */
1298 #define SLOP 5
1300 static int still_interesting(struct commit_list *src, timestamp_t date, int slop,
1301 struct commit **interesting_cache)
1304 * No source list at all? We're definitely done..
1306 if (!src)
1307 return 0;
1310 * Does the destination list contain entries with a date
1311 * before the source list? Definitely _not_ done.
1313 if (date <= src->item->date)
1314 return SLOP;
1317 * Does the source list still have interesting commits in
1318 * it? Definitely not done..
1320 if (!everybody_uninteresting(src, interesting_cache))
1321 return SLOP;
1323 /* Ok, we're closing in.. */
1324 return slop-1;
1328 * "rev-list --ancestry-path=C_0 [--ancestry-path=C_1 ...] A..B"
1329 * computes commits that are ancestors of B but not ancestors of A but
1330 * further limits the result to those that have any of C in their
1331 * ancestry path (i.e. are either ancestors of any of C, descendants
1332 * of any of C, or are any of C). If --ancestry-path is specified with
1333 * no commit, we use all bottom commits for C.
1335 * Before this function is called, ancestors of C will have already
1336 * been marked with ANCESTRY_PATH previously.
1338 * This takes the list of bottom commits and the result of "A..B"
1339 * without --ancestry-path, and limits the latter further to the ones
1340 * that have any of C in their ancestry path. Since the ancestors of C
1341 * have already been marked (a prerequisite of this function), we just
1342 * need to mark the descendants, then exclude any commit that does not
1343 * have any of these marks.
1345 static void limit_to_ancestry(struct commit_list *bottoms, struct commit_list *list)
1347 struct commit_list *p;
1348 struct commit_list *rlist = NULL;
1349 int made_progress;
1352 * Reverse the list so that it will be likely that we would
1353 * process parents before children.
1355 for (p = list; p; p = p->next)
1356 commit_list_insert(p->item, &rlist);
1358 for (p = bottoms; p; p = p->next)
1359 p->item->object.flags |= TMP_MARK;
1362 * Mark the ones that can reach bottom commits in "list",
1363 * in a bottom-up fashion.
1365 do {
1366 made_progress = 0;
1367 for (p = rlist; p; p = p->next) {
1368 struct commit *c = p->item;
1369 struct commit_list *parents;
1370 if (c->object.flags & (TMP_MARK | UNINTERESTING))
1371 continue;
1372 for (parents = c->parents;
1373 parents;
1374 parents = parents->next) {
1375 if (!(parents->item->object.flags & TMP_MARK))
1376 continue;
1377 c->object.flags |= TMP_MARK;
1378 made_progress = 1;
1379 break;
1382 } while (made_progress);
1385 * NEEDSWORK: decide if we want to remove parents that are
1386 * not marked with TMP_MARK from commit->parents for commits
1387 * in the resulting list. We may not want to do that, though.
1391 * The ones that are not marked with either TMP_MARK or
1392 * ANCESTRY_PATH are uninteresting
1394 for (p = list; p; p = p->next) {
1395 struct commit *c = p->item;
1396 if (c->object.flags & (TMP_MARK | ANCESTRY_PATH))
1397 continue;
1398 c->object.flags |= UNINTERESTING;
1401 /* We are done with TMP_MARK and ANCESTRY_PATH */
1402 for (p = list; p; p = p->next)
1403 p->item->object.flags &= ~(TMP_MARK | ANCESTRY_PATH);
1404 for (p = bottoms; p; p = p->next)
1405 p->item->object.flags &= ~(TMP_MARK | ANCESTRY_PATH);
1406 free_commit_list(rlist);
1410 * Before walking the history, add the set of "negative" refs the
1411 * caller has asked to exclude to the bottom list.
1413 * This is used to compute "rev-list --ancestry-path A..B", as we need
1414 * to filter the result of "A..B" further to the ones that can actually
1415 * reach A.
1417 static void collect_bottom_commits(struct commit_list *list,
1418 struct commit_list **bottom)
1420 struct commit_list *elem;
1421 for (elem = list; elem; elem = elem->next)
1422 if (elem->item->object.flags & BOTTOM)
1423 commit_list_insert(elem->item, bottom);
1426 /* Assumes either left_only or right_only is set */
1427 static void limit_left_right(struct commit_list *list, struct rev_info *revs)
1429 struct commit_list *p;
1431 for (p = list; p; p = p->next) {
1432 struct commit *commit = p->item;
1434 if (revs->right_only) {
1435 if (commit->object.flags & SYMMETRIC_LEFT)
1436 commit->object.flags |= SHOWN;
1437 } else /* revs->left_only is set */
1438 if (!(commit->object.flags & SYMMETRIC_LEFT))
1439 commit->object.flags |= SHOWN;
1443 static int limit_list(struct rev_info *revs)
1445 int slop = SLOP;
1446 timestamp_t date = TIME_MAX;
1447 struct commit_list *original_list = revs->commits;
1448 struct commit_list *newlist = NULL;
1449 struct commit_list **p = &newlist;
1450 struct commit *interesting_cache = NULL;
1452 if (revs->ancestry_path_implicit_bottoms) {
1453 collect_bottom_commits(original_list,
1454 &revs->ancestry_path_bottoms);
1455 if (!revs->ancestry_path_bottoms)
1456 die("--ancestry-path given but there are no bottom commits");
1459 while (original_list) {
1460 struct commit *commit = pop_commit(&original_list);
1461 struct object *obj = &commit->object;
1462 show_early_output_fn_t show;
1464 if (commit == interesting_cache)
1465 interesting_cache = NULL;
1467 if (revs->max_age != -1 && (commit->date < revs->max_age))
1468 obj->flags |= UNINTERESTING;
1469 if (process_parents(revs, commit, &original_list, NULL) < 0)
1470 return -1;
1471 if (obj->flags & UNINTERESTING) {
1472 mark_parents_uninteresting(revs, commit);
1473 slop = still_interesting(original_list, date, slop, &interesting_cache);
1474 if (slop)
1475 continue;
1476 break;
1478 if (revs->min_age != -1 && (commit->date > revs->min_age) &&
1479 !revs->line_level_traverse)
1480 continue;
1481 if (revs->max_age_as_filter != -1 &&
1482 (commit->date < revs->max_age_as_filter) && !revs->line_level_traverse)
1483 continue;
1484 date = commit->date;
1485 p = &commit_list_insert(commit, p)->next;
1487 show = show_early_output;
1488 if (!show)
1489 continue;
1491 show(revs, newlist);
1492 show_early_output = NULL;
1494 if (revs->cherry_pick || revs->cherry_mark)
1495 cherry_pick_list(newlist, revs);
1497 if (revs->left_only || revs->right_only)
1498 limit_left_right(newlist, revs);
1500 if (revs->ancestry_path)
1501 limit_to_ancestry(revs->ancestry_path_bottoms, newlist);
1504 * Check if any commits have become TREESAME by some of their parents
1505 * becoming UNINTERESTING.
1507 if (limiting_can_increase_treesame(revs)) {
1508 struct commit_list *list = NULL;
1509 for (list = newlist; list; list = list->next) {
1510 struct commit *c = list->item;
1511 if (c->object.flags & (UNINTERESTING | TREESAME))
1512 continue;
1513 update_treesame(revs, c);
1517 free_commit_list(original_list);
1518 revs->commits = newlist;
1519 return 0;
1523 * Add an entry to refs->cmdline with the specified information.
1524 * *name is copied.
1526 static void add_rev_cmdline(struct rev_info *revs,
1527 struct object *item,
1528 const char *name,
1529 int whence,
1530 unsigned flags)
1532 struct rev_cmdline_info *info = &revs->cmdline;
1533 unsigned int nr = info->nr;
1535 ALLOC_GROW(info->rev, nr + 1, info->alloc);
1536 info->rev[nr].item = item;
1537 info->rev[nr].name = xstrdup(name);
1538 info->rev[nr].whence = whence;
1539 info->rev[nr].flags = flags;
1540 info->nr++;
1543 static void add_rev_cmdline_list(struct rev_info *revs,
1544 struct commit_list *commit_list,
1545 int whence,
1546 unsigned flags)
1548 while (commit_list) {
1549 struct object *object = &commit_list->item->object;
1550 add_rev_cmdline(revs, object, oid_to_hex(&object->oid),
1551 whence, flags);
1552 commit_list = commit_list->next;
1556 int ref_excluded(const struct ref_exclusions *exclusions, const char *path)
1558 const char *stripped_path = strip_namespace(path);
1559 struct string_list_item *item;
1561 for_each_string_list_item(item, &exclusions->excluded_refs) {
1562 if (!wildmatch(item->string, path, 0))
1563 return 1;
1566 if (ref_is_hidden(stripped_path, path, &exclusions->hidden_refs))
1567 return 1;
1569 return 0;
1572 void init_ref_exclusions(struct ref_exclusions *exclusions)
1574 struct ref_exclusions blank = REF_EXCLUSIONS_INIT;
1575 memcpy(exclusions, &blank, sizeof(*exclusions));
1578 void clear_ref_exclusions(struct ref_exclusions *exclusions)
1580 string_list_clear(&exclusions->excluded_refs, 0);
1581 strvec_clear(&exclusions->hidden_refs);
1582 exclusions->hidden_refs_configured = 0;
1585 void add_ref_exclusion(struct ref_exclusions *exclusions, const char *exclude)
1587 string_list_append(&exclusions->excluded_refs, exclude);
1590 struct exclude_hidden_refs_cb {
1591 struct ref_exclusions *exclusions;
1592 const char *section;
1595 static int hide_refs_config(const char *var, const char *value,
1596 const struct config_context *ctx UNUSED,
1597 void *cb_data)
1599 struct exclude_hidden_refs_cb *cb = cb_data;
1600 cb->exclusions->hidden_refs_configured = 1;
1601 return parse_hide_refs_config(var, value, cb->section,
1602 &cb->exclusions->hidden_refs);
1605 void exclude_hidden_refs(struct ref_exclusions *exclusions, const char *section)
1607 struct exclude_hidden_refs_cb cb;
1609 if (strcmp(section, "fetch") && strcmp(section, "receive") &&
1610 strcmp(section, "uploadpack"))
1611 die(_("unsupported section for hidden refs: %s"), section);
1613 if (exclusions->hidden_refs_configured)
1614 die(_("--exclude-hidden= passed more than once"));
1616 cb.exclusions = exclusions;
1617 cb.section = section;
1619 git_config(hide_refs_config, &cb);
1622 struct all_refs_cb {
1623 int all_flags;
1624 int warned_bad_reflog;
1625 struct rev_info *all_revs;
1626 const char *name_for_errormsg;
1627 struct worktree *wt;
1630 static int handle_one_ref(const char *path, const struct object_id *oid,
1631 int flag UNUSED,
1632 void *cb_data)
1634 struct all_refs_cb *cb = cb_data;
1635 struct object *object;
1637 if (ref_excluded(&cb->all_revs->ref_excludes, path))
1638 return 0;
1640 object = get_reference(cb->all_revs, path, oid, cb->all_flags);
1641 add_rev_cmdline(cb->all_revs, object, path, REV_CMD_REF, cb->all_flags);
1642 add_pending_object(cb->all_revs, object, path);
1643 return 0;
1646 static void init_all_refs_cb(struct all_refs_cb *cb, struct rev_info *revs,
1647 unsigned flags)
1649 cb->all_revs = revs;
1650 cb->all_flags = flags;
1651 revs->rev_input_given = 1;
1652 cb->wt = NULL;
1655 static void handle_refs(struct ref_store *refs,
1656 struct rev_info *revs, unsigned flags,
1657 int (*for_each)(struct ref_store *, each_ref_fn, void *))
1659 struct all_refs_cb cb;
1661 if (!refs) {
1662 /* this could happen with uninitialized submodules */
1663 return;
1666 init_all_refs_cb(&cb, revs, flags);
1667 for_each(refs, handle_one_ref, &cb);
1670 static void handle_one_reflog_commit(struct object_id *oid, void *cb_data)
1672 struct all_refs_cb *cb = cb_data;
1673 if (!is_null_oid(oid)) {
1674 struct object *o = parse_object(cb->all_revs->repo, oid);
1675 if (o) {
1676 o->flags |= cb->all_flags;
1677 /* ??? CMDLINEFLAGS ??? */
1678 add_pending_object(cb->all_revs, o, "");
1680 else if (!cb->warned_bad_reflog) {
1681 warning("reflog of '%s' references pruned commits",
1682 cb->name_for_errormsg);
1683 cb->warned_bad_reflog = 1;
1688 static int handle_one_reflog_ent(struct object_id *ooid, struct object_id *noid,
1689 const char *email UNUSED,
1690 timestamp_t timestamp UNUSED,
1691 int tz UNUSED,
1692 const char *message UNUSED,
1693 void *cb_data)
1695 handle_one_reflog_commit(ooid, cb_data);
1696 handle_one_reflog_commit(noid, cb_data);
1697 return 0;
1700 static int handle_one_reflog(const char *refname_in_wt, void *cb_data)
1702 struct all_refs_cb *cb = cb_data;
1703 struct strbuf refname = STRBUF_INIT;
1705 cb->warned_bad_reflog = 0;
1706 strbuf_worktree_ref(cb->wt, &refname, refname_in_wt);
1707 cb->name_for_errormsg = refname.buf;
1708 refs_for_each_reflog_ent(get_main_ref_store(the_repository),
1709 refname.buf,
1710 handle_one_reflog_ent, cb_data);
1711 strbuf_release(&refname);
1712 return 0;
1715 static void add_other_reflogs_to_pending(struct all_refs_cb *cb)
1717 struct worktree **worktrees, **p;
1719 worktrees = get_worktrees();
1720 for (p = worktrees; *p; p++) {
1721 struct worktree *wt = *p;
1723 if (wt->is_current)
1724 continue;
1726 cb->wt = wt;
1727 refs_for_each_reflog(get_worktree_ref_store(wt),
1728 handle_one_reflog,
1729 cb);
1731 free_worktrees(worktrees);
1734 void add_reflogs_to_pending(struct rev_info *revs, unsigned flags)
1736 struct all_refs_cb cb;
1738 cb.all_revs = revs;
1739 cb.all_flags = flags;
1740 cb.wt = NULL;
1741 refs_for_each_reflog(get_main_ref_store(the_repository),
1742 handle_one_reflog, &cb);
1744 if (!revs->single_worktree)
1745 add_other_reflogs_to_pending(&cb);
1748 static void add_cache_tree(struct cache_tree *it, struct rev_info *revs,
1749 struct strbuf *path, unsigned int flags)
1751 size_t baselen = path->len;
1752 int i;
1754 if (it->entry_count >= 0) {
1755 struct tree *tree = lookup_tree(revs->repo, &it->oid);
1756 tree->object.flags |= flags;
1757 add_pending_object_with_path(revs, &tree->object, "",
1758 040000, path->buf);
1761 for (i = 0; i < it->subtree_nr; i++) {
1762 struct cache_tree_sub *sub = it->down[i];
1763 strbuf_addf(path, "%s%s", baselen ? "/" : "", sub->name);
1764 add_cache_tree(sub->cache_tree, revs, path, flags);
1765 strbuf_setlen(path, baselen);
1770 static void add_resolve_undo_to_pending(struct index_state *istate, struct rev_info *revs)
1772 struct string_list_item *item;
1773 struct string_list *resolve_undo = istate->resolve_undo;
1775 if (!resolve_undo)
1776 return;
1778 for_each_string_list_item(item, resolve_undo) {
1779 const char *path = item->string;
1780 struct resolve_undo_info *ru = item->util;
1781 int i;
1783 if (!ru)
1784 continue;
1785 for (i = 0; i < 3; i++) {
1786 struct blob *blob;
1788 if (!ru->mode[i] || !S_ISREG(ru->mode[i]))
1789 continue;
1791 blob = lookup_blob(revs->repo, &ru->oid[i]);
1792 if (!blob) {
1793 warning(_("resolve-undo records `%s` which is missing"),
1794 oid_to_hex(&ru->oid[i]));
1795 continue;
1797 add_pending_object_with_path(revs, &blob->object, "",
1798 ru->mode[i], path);
1803 static void do_add_index_objects_to_pending(struct rev_info *revs,
1804 struct index_state *istate,
1805 unsigned int flags)
1807 int i;
1809 /* TODO: audit for interaction with sparse-index. */
1810 ensure_full_index(istate);
1811 for (i = 0; i < istate->cache_nr; i++) {
1812 struct cache_entry *ce = istate->cache[i];
1813 struct blob *blob;
1815 if (S_ISGITLINK(ce->ce_mode))
1816 continue;
1818 blob = lookup_blob(revs->repo, &ce->oid);
1819 if (!blob)
1820 die("unable to add index blob to traversal");
1821 blob->object.flags |= flags;
1822 add_pending_object_with_path(revs, &blob->object, "",
1823 ce->ce_mode, ce->name);
1826 if (istate->cache_tree) {
1827 struct strbuf path = STRBUF_INIT;
1828 add_cache_tree(istate->cache_tree, revs, &path, flags);
1829 strbuf_release(&path);
1832 add_resolve_undo_to_pending(istate, revs);
1835 void add_index_objects_to_pending(struct rev_info *revs, unsigned int flags)
1837 struct worktree **worktrees, **p;
1839 repo_read_index(revs->repo);
1840 do_add_index_objects_to_pending(revs, revs->repo->index, flags);
1842 if (revs->single_worktree)
1843 return;
1845 worktrees = get_worktrees();
1846 for (p = worktrees; *p; p++) {
1847 struct worktree *wt = *p;
1848 struct index_state istate = INDEX_STATE_INIT(revs->repo);
1850 if (wt->is_current)
1851 continue; /* current index already taken care of */
1853 if (read_index_from(&istate,
1854 worktree_git_path(wt, "index"),
1855 get_worktree_git_dir(wt)) > 0)
1856 do_add_index_objects_to_pending(revs, &istate, flags);
1857 discard_index(&istate);
1859 free_worktrees(worktrees);
1862 struct add_alternate_refs_data {
1863 struct rev_info *revs;
1864 unsigned int flags;
1867 static void add_one_alternate_ref(const struct object_id *oid,
1868 void *vdata)
1870 const char *name = ".alternate";
1871 struct add_alternate_refs_data *data = vdata;
1872 struct object *obj;
1874 obj = get_reference(data->revs, name, oid, data->flags);
1875 add_rev_cmdline(data->revs, obj, name, REV_CMD_REV, data->flags);
1876 add_pending_object(data->revs, obj, name);
1879 static void add_alternate_refs_to_pending(struct rev_info *revs,
1880 unsigned int flags)
1882 struct add_alternate_refs_data data;
1883 data.revs = revs;
1884 data.flags = flags;
1885 for_each_alternate_ref(add_one_alternate_ref, &data);
1888 static int add_parents_only(struct rev_info *revs, const char *arg_, int flags,
1889 int exclude_parent)
1891 struct object_id oid;
1892 struct object *it;
1893 struct commit *commit;
1894 struct commit_list *parents;
1895 int parent_number;
1896 const char *arg = arg_;
1898 if (*arg == '^') {
1899 flags ^= UNINTERESTING | BOTTOM;
1900 arg++;
1902 if (repo_get_oid_committish(the_repository, arg, &oid))
1903 return 0;
1904 while (1) {
1905 it = get_reference(revs, arg, &oid, 0);
1906 if (!it && revs->ignore_missing)
1907 return 0;
1908 if (it->type != OBJ_TAG)
1909 break;
1910 if (!((struct tag*)it)->tagged)
1911 return 0;
1912 oidcpy(&oid, &((struct tag*)it)->tagged->oid);
1914 if (it->type != OBJ_COMMIT)
1915 return 0;
1916 commit = (struct commit *)it;
1917 if (exclude_parent &&
1918 exclude_parent > commit_list_count(commit->parents))
1919 return 0;
1920 for (parents = commit->parents, parent_number = 1;
1921 parents;
1922 parents = parents->next, parent_number++) {
1923 if (exclude_parent && parent_number != exclude_parent)
1924 continue;
1926 it = &parents->item->object;
1927 it->flags |= flags;
1928 add_rev_cmdline(revs, it, arg_, REV_CMD_PARENTS_ONLY, flags);
1929 add_pending_object(revs, it, arg);
1931 return 1;
1934 void repo_init_revisions(struct repository *r,
1935 struct rev_info *revs,
1936 const char *prefix)
1938 struct rev_info blank = REV_INFO_INIT;
1939 memcpy(revs, &blank, sizeof(*revs));
1941 revs->repo = r;
1942 revs->pruning.repo = r;
1943 revs->pruning.add_remove = file_add_remove;
1944 revs->pruning.change = file_change;
1945 revs->pruning.change_fn_data = revs;
1946 revs->prefix = prefix;
1948 grep_init(&revs->grep_filter, revs->repo);
1949 revs->grep_filter.status_only = 1;
1951 repo_diff_setup(revs->repo, &revs->diffopt);
1952 if (prefix && !revs->diffopt.prefix) {
1953 revs->diffopt.prefix = prefix;
1954 revs->diffopt.prefix_length = strlen(prefix);
1957 init_display_notes(&revs->notes_opt);
1958 list_objects_filter_init(&revs->filter);
1959 init_ref_exclusions(&revs->ref_excludes);
1960 oidset_init(&revs->missing_commits, 0);
1963 static void add_pending_commit_list(struct rev_info *revs,
1964 struct commit_list *commit_list,
1965 unsigned int flags)
1967 while (commit_list) {
1968 struct object *object = &commit_list->item->object;
1969 object->flags |= flags;
1970 add_pending_object(revs, object, oid_to_hex(&object->oid));
1971 commit_list = commit_list->next;
1975 static const char *lookup_other_head(struct object_id *oid)
1977 int i;
1978 static const char *const other_head[] = {
1979 "MERGE_HEAD", "CHERRY_PICK_HEAD", "REVERT_HEAD", "REBASE_HEAD"
1982 for (i = 0; i < ARRAY_SIZE(other_head); i++)
1983 if (!refs_read_ref_full(get_main_ref_store(the_repository), other_head[i],
1984 RESOLVE_REF_READING | RESOLVE_REF_NO_RECURSE,
1985 oid, NULL)) {
1986 if (is_null_oid(oid))
1987 die(_("%s exists but is a symbolic ref"), other_head[i]);
1988 return other_head[i];
1991 die(_("--merge requires one of the pseudorefs MERGE_HEAD, CHERRY_PICK_HEAD, REVERT_HEAD or REBASE_HEAD"));
1994 static void prepare_show_merge(struct rev_info *revs)
1996 struct commit_list *bases = NULL;
1997 struct commit *head, *other;
1998 struct object_id oid;
1999 const char *other_name;
2000 const char **prune = NULL;
2001 int i, prune_num = 1; /* counting terminating NULL */
2002 struct index_state *istate = revs->repo->index;
2004 if (repo_get_oid(the_repository, "HEAD", &oid))
2005 die("--merge without HEAD?");
2006 head = lookup_commit_or_die(&oid, "HEAD");
2007 other_name = lookup_other_head(&oid);
2008 other = lookup_commit_or_die(&oid, other_name);
2009 add_pending_object(revs, &head->object, "HEAD");
2010 add_pending_object(revs, &other->object, other_name);
2011 if (repo_get_merge_bases(the_repository, head, other, &bases) < 0)
2012 exit(128);
2013 add_rev_cmdline_list(revs, bases, REV_CMD_MERGE_BASE, UNINTERESTING | BOTTOM);
2014 add_pending_commit_list(revs, bases, UNINTERESTING | BOTTOM);
2015 free_commit_list(bases);
2016 head->object.flags |= SYMMETRIC_LEFT;
2018 if (!istate->cache_nr)
2019 repo_read_index(revs->repo);
2020 for (i = 0; i < istate->cache_nr; i++) {
2021 const struct cache_entry *ce = istate->cache[i];
2022 if (!ce_stage(ce))
2023 continue;
2024 if (ce_path_match(istate, ce, &revs->prune_data, NULL)) {
2025 prune_num++;
2026 REALLOC_ARRAY(prune, prune_num);
2027 prune[prune_num-2] = ce->name;
2028 prune[prune_num-1] = NULL;
2030 while ((i+1 < istate->cache_nr) &&
2031 ce_same_name(ce, istate->cache[i+1]))
2032 i++;
2034 clear_pathspec(&revs->prune_data);
2035 parse_pathspec(&revs->prune_data, PATHSPEC_ALL_MAGIC & ~PATHSPEC_LITERAL,
2036 PATHSPEC_PREFER_FULL | PATHSPEC_LITERAL_PATH, "", prune);
2037 revs->limited = 1;
2040 static int dotdot_missing(const char *arg, char *dotdot,
2041 struct rev_info *revs, int symmetric)
2043 if (revs->ignore_missing)
2044 return 0;
2045 /* de-munge so we report the full argument */
2046 *dotdot = '.';
2047 die(symmetric
2048 ? "Invalid symmetric difference expression %s"
2049 : "Invalid revision range %s", arg);
2052 static int handle_dotdot_1(const char *arg, char *dotdot,
2053 struct rev_info *revs, int flags,
2054 int cant_be_filename,
2055 struct object_context *a_oc,
2056 struct object_context *b_oc)
2058 const char *a_name, *b_name;
2059 struct object_id a_oid, b_oid;
2060 struct object *a_obj, *b_obj;
2061 unsigned int a_flags, b_flags;
2062 int symmetric = 0;
2063 unsigned int flags_exclude = flags ^ (UNINTERESTING | BOTTOM);
2064 unsigned int oc_flags = GET_OID_COMMITTISH | GET_OID_RECORD_PATH;
2066 a_name = arg;
2067 if (!*a_name)
2068 a_name = "HEAD";
2070 b_name = dotdot + 2;
2071 if (*b_name == '.') {
2072 symmetric = 1;
2073 b_name++;
2075 if (!*b_name)
2076 b_name = "HEAD";
2078 if (get_oid_with_context(revs->repo, a_name, oc_flags, &a_oid, a_oc) ||
2079 get_oid_with_context(revs->repo, b_name, oc_flags, &b_oid, b_oc))
2080 return -1;
2082 if (!cant_be_filename) {
2083 *dotdot = '.';
2084 verify_non_filename(revs->prefix, arg);
2085 *dotdot = '\0';
2088 a_obj = parse_object(revs->repo, &a_oid);
2089 b_obj = parse_object(revs->repo, &b_oid);
2090 if (!a_obj || !b_obj)
2091 return dotdot_missing(arg, dotdot, revs, symmetric);
2093 if (!symmetric) {
2094 /* just A..B */
2095 b_flags = flags;
2096 a_flags = flags_exclude;
2097 } else {
2098 /* A...B -- find merge bases between the two */
2099 struct commit *a, *b;
2100 struct commit_list *exclude = NULL;
2102 a = lookup_commit_reference(revs->repo, &a_obj->oid);
2103 b = lookup_commit_reference(revs->repo, &b_obj->oid);
2104 if (!a || !b)
2105 return dotdot_missing(arg, dotdot, revs, symmetric);
2107 if (repo_get_merge_bases(the_repository, a, b, &exclude) < 0) {
2108 free_commit_list(exclude);
2109 return -1;
2111 add_rev_cmdline_list(revs, exclude, REV_CMD_MERGE_BASE,
2112 flags_exclude);
2113 add_pending_commit_list(revs, exclude, flags_exclude);
2114 free_commit_list(exclude);
2116 b_flags = flags;
2117 a_flags = flags | SYMMETRIC_LEFT;
2120 a_obj->flags |= a_flags;
2121 b_obj->flags |= b_flags;
2122 add_rev_cmdline(revs, a_obj, a_name, REV_CMD_LEFT, a_flags);
2123 add_rev_cmdline(revs, b_obj, b_name, REV_CMD_RIGHT, b_flags);
2124 add_pending_object_with_path(revs, a_obj, a_name, a_oc->mode, a_oc->path);
2125 add_pending_object_with_path(revs, b_obj, b_name, b_oc->mode, b_oc->path);
2126 return 0;
2129 static int handle_dotdot(const char *arg,
2130 struct rev_info *revs, int flags,
2131 int cant_be_filename)
2133 struct object_context a_oc = {0}, b_oc = {0};
2134 char *dotdot = strstr(arg, "..");
2135 int ret;
2137 if (!dotdot)
2138 return -1;
2140 *dotdot = '\0';
2141 ret = handle_dotdot_1(arg, dotdot, revs, flags, cant_be_filename,
2142 &a_oc, &b_oc);
2143 *dotdot = '.';
2145 object_context_release(&a_oc);
2146 object_context_release(&b_oc);
2147 return ret;
2150 static int handle_revision_arg_1(const char *arg_, struct rev_info *revs, int flags, unsigned revarg_opt)
2152 struct object_context oc = {0};
2153 char *mark;
2154 struct object *object;
2155 struct object_id oid;
2156 int local_flags;
2157 const char *arg = arg_;
2158 int cant_be_filename = revarg_opt & REVARG_CANNOT_BE_FILENAME;
2159 unsigned get_sha1_flags = GET_OID_RECORD_PATH;
2160 int ret;
2162 flags = flags & UNINTERESTING ? flags | BOTTOM : flags & ~BOTTOM;
2164 if (!cant_be_filename && !strcmp(arg, "..")) {
2166 * Just ".."? That is not a range but the
2167 * pathspec for the parent directory.
2169 ret = -1;
2170 goto out;
2173 if (!handle_dotdot(arg, revs, flags, revarg_opt)) {
2174 ret = 0;
2175 goto out;
2178 mark = strstr(arg, "^@");
2179 if (mark && !mark[2]) {
2180 *mark = 0;
2181 if (add_parents_only(revs, arg, flags, 0)) {
2182 ret = 0;
2183 goto out;
2185 *mark = '^';
2187 mark = strstr(arg, "^!");
2188 if (mark && !mark[2]) {
2189 *mark = 0;
2190 if (!add_parents_only(revs, arg, flags ^ (UNINTERESTING | BOTTOM), 0))
2191 *mark = '^';
2193 mark = strstr(arg, "^-");
2194 if (mark) {
2195 int exclude_parent = 1;
2197 if (mark[2]) {
2198 if (strtol_i(mark + 2, 10, &exclude_parent) ||
2199 exclude_parent < 1) {
2200 ret = -1;
2201 goto out;
2205 *mark = 0;
2206 if (!add_parents_only(revs, arg, flags ^ (UNINTERESTING | BOTTOM), exclude_parent))
2207 *mark = '^';
2210 local_flags = 0;
2211 if (*arg == '^') {
2212 local_flags = UNINTERESTING | BOTTOM;
2213 arg++;
2216 if (revarg_opt & REVARG_COMMITTISH)
2217 get_sha1_flags |= GET_OID_COMMITTISH;
2220 * Even if revs->do_not_die_on_missing_objects is set, we
2221 * should error out if we can't even get an oid, as
2222 * `--missing=print` should be able to report missing oids.
2224 if (get_oid_with_context(revs->repo, arg, get_sha1_flags, &oid, &oc)) {
2225 ret = revs->ignore_missing ? 0 : -1;
2226 goto out;
2228 if (!cant_be_filename)
2229 verify_non_filename(revs->prefix, arg);
2230 object = get_reference(revs, arg, &oid, flags ^ local_flags);
2231 if (!object) {
2232 ret = (revs->ignore_missing || revs->do_not_die_on_missing_objects) ? 0 : -1;
2233 goto out;
2235 add_rev_cmdline(revs, object, arg_, REV_CMD_REV, flags ^ local_flags);
2236 add_pending_object_with_path(revs, object, arg, oc.mode, oc.path);
2238 ret = 0;
2240 out:
2241 object_context_release(&oc);
2242 return ret;
2245 int handle_revision_arg(const char *arg, struct rev_info *revs, int flags, unsigned revarg_opt)
2247 int ret = handle_revision_arg_1(arg, revs, flags, revarg_opt);
2248 if (!ret)
2249 revs->rev_input_given = 1;
2250 return ret;
2253 static void read_pathspec_from_stdin(struct strbuf *sb,
2254 struct strvec *prune)
2256 while (strbuf_getline(sb, stdin) != EOF)
2257 strvec_push(prune, sb->buf);
2260 static void add_grep(struct rev_info *revs, const char *ptn, enum grep_pat_token what)
2262 append_grep_pattern(&revs->grep_filter, ptn, "command line", 0, what);
2265 static void add_header_grep(struct rev_info *revs, enum grep_header_field field, const char *pattern)
2267 append_header_grep_pattern(&revs->grep_filter, field, pattern);
2270 static void add_message_grep(struct rev_info *revs, const char *pattern)
2272 add_grep(revs, pattern, GREP_PATTERN_BODY);
2275 static int parse_count(const char *arg)
2277 int count;
2279 if (strtol_i(arg, 10, &count) < 0)
2280 die("'%s': not an integer", arg);
2281 return count;
2284 static timestamp_t parse_age(const char *arg)
2286 timestamp_t num;
2287 char *p;
2289 errno = 0;
2290 num = parse_timestamp(arg, &p, 10);
2291 if (errno || *p || p == arg)
2292 die("'%s': not a number of seconds since epoch", arg);
2293 return num;
2296 static int handle_revision_opt(struct rev_info *revs, int argc, const char **argv,
2297 int *unkc, const char **unkv,
2298 const struct setup_revision_opt* opt)
2300 const char *arg = argv[0];
2301 const char *optarg = NULL;
2302 int argcount;
2303 const unsigned hexsz = the_hash_algo->hexsz;
2305 /* pseudo revision arguments */
2306 if (!strcmp(arg, "--all") || !strcmp(arg, "--branches") ||
2307 !strcmp(arg, "--tags") || !strcmp(arg, "--remotes") ||
2308 !strcmp(arg, "--reflog") || !strcmp(arg, "--not") ||
2309 !strcmp(arg, "--no-walk") || !strcmp(arg, "--do-walk") ||
2310 !strcmp(arg, "--bisect") || starts_with(arg, "--glob=") ||
2311 !strcmp(arg, "--indexed-objects") ||
2312 !strcmp(arg, "--alternate-refs") ||
2313 starts_with(arg, "--exclude=") || starts_with(arg, "--exclude-hidden=") ||
2314 starts_with(arg, "--branches=") || starts_with(arg, "--tags=") ||
2315 starts_with(arg, "--remotes=") || starts_with(arg, "--no-walk="))
2317 unkv[(*unkc)++] = arg;
2318 return 1;
2321 if ((argcount = parse_long_opt("max-count", argv, &optarg))) {
2322 revs->max_count = parse_count(optarg);
2323 revs->no_walk = 0;
2324 return argcount;
2325 } else if ((argcount = parse_long_opt("skip", argv, &optarg))) {
2326 revs->skip_count = parse_count(optarg);
2327 return argcount;
2328 } else if ((*arg == '-') && isdigit(arg[1])) {
2329 /* accept -<digit>, like traditional "head" */
2330 revs->max_count = parse_count(arg + 1);
2331 revs->no_walk = 0;
2332 } else if (!strcmp(arg, "-n")) {
2333 if (argc <= 1)
2334 return error("-n requires an argument");
2335 revs->max_count = parse_count(argv[1]);
2336 revs->no_walk = 0;
2337 return 2;
2338 } else if (skip_prefix(arg, "-n", &optarg)) {
2339 revs->max_count = parse_count(optarg);
2340 revs->no_walk = 0;
2341 } else if ((argcount = parse_long_opt("max-age", argv, &optarg))) {
2342 revs->max_age = parse_age(optarg);
2343 return argcount;
2344 } else if ((argcount = parse_long_opt("since", argv, &optarg))) {
2345 revs->max_age = approxidate(optarg);
2346 return argcount;
2347 } else if ((argcount = parse_long_opt("since-as-filter", argv, &optarg))) {
2348 revs->max_age_as_filter = approxidate(optarg);
2349 return argcount;
2350 } else if ((argcount = parse_long_opt("after", argv, &optarg))) {
2351 revs->max_age = approxidate(optarg);
2352 return argcount;
2353 } else if ((argcount = parse_long_opt("min-age", argv, &optarg))) {
2354 revs->min_age = parse_age(optarg);
2355 return argcount;
2356 } else if ((argcount = parse_long_opt("before", argv, &optarg))) {
2357 revs->min_age = approxidate(optarg);
2358 return argcount;
2359 } else if ((argcount = parse_long_opt("until", argv, &optarg))) {
2360 revs->min_age = approxidate(optarg);
2361 return argcount;
2362 } else if (!strcmp(arg, "--first-parent")) {
2363 revs->first_parent_only = 1;
2364 } else if (!strcmp(arg, "--exclude-first-parent-only")) {
2365 revs->exclude_first_parent_only = 1;
2366 } else if (!strcmp(arg, "--ancestry-path")) {
2367 revs->ancestry_path = 1;
2368 revs->simplify_history = 0;
2369 revs->limited = 1;
2370 revs->ancestry_path_implicit_bottoms = 1;
2371 } else if (skip_prefix(arg, "--ancestry-path=", &optarg)) {
2372 struct commit *c;
2373 struct object_id oid;
2374 const char *msg = _("could not get commit for --ancestry-path argument %s");
2376 revs->ancestry_path = 1;
2377 revs->simplify_history = 0;
2378 revs->limited = 1;
2380 if (repo_get_oid_committish(revs->repo, optarg, &oid))
2381 return error(msg, optarg);
2382 get_reference(revs, optarg, &oid, ANCESTRY_PATH);
2383 c = lookup_commit_reference(revs->repo, &oid);
2384 if (!c)
2385 return error(msg, optarg);
2386 commit_list_insert(c, &revs->ancestry_path_bottoms);
2387 } else if (!strcmp(arg, "-g") || !strcmp(arg, "--walk-reflogs")) {
2388 init_reflog_walk(&revs->reflog_info);
2389 } else if (!strcmp(arg, "--default")) {
2390 if (argc <= 1)
2391 return error("bad --default argument");
2392 revs->def = argv[1];
2393 return 2;
2394 } else if (!strcmp(arg, "--merge")) {
2395 revs->show_merge = 1;
2396 } else if (!strcmp(arg, "--topo-order")) {
2397 revs->sort_order = REV_SORT_IN_GRAPH_ORDER;
2398 revs->topo_order = 1;
2399 } else if (!strcmp(arg, "--simplify-merges")) {
2400 revs->simplify_merges = 1;
2401 revs->topo_order = 1;
2402 revs->rewrite_parents = 1;
2403 revs->simplify_history = 0;
2404 revs->limited = 1;
2405 } else if (!strcmp(arg, "--simplify-by-decoration")) {
2406 revs->simplify_merges = 1;
2407 revs->topo_order = 1;
2408 revs->rewrite_parents = 1;
2409 revs->simplify_history = 0;
2410 revs->simplify_by_decoration = 1;
2411 revs->limited = 1;
2412 revs->prune = 1;
2413 } else if (!strcmp(arg, "--date-order")) {
2414 revs->sort_order = REV_SORT_BY_COMMIT_DATE;
2415 revs->topo_order = 1;
2416 } else if (!strcmp(arg, "--author-date-order")) {
2417 revs->sort_order = REV_SORT_BY_AUTHOR_DATE;
2418 revs->topo_order = 1;
2419 } else if (!strcmp(arg, "--early-output")) {
2420 revs->early_output = 100;
2421 revs->topo_order = 1;
2422 } else if (skip_prefix(arg, "--early-output=", &optarg)) {
2423 if (strtoul_ui(optarg, 10, &revs->early_output) < 0)
2424 die("'%s': not a non-negative integer", optarg);
2425 revs->topo_order = 1;
2426 } else if (!strcmp(arg, "--parents")) {
2427 revs->rewrite_parents = 1;
2428 revs->print_parents = 1;
2429 } else if (!strcmp(arg, "--dense")) {
2430 revs->dense = 1;
2431 } else if (!strcmp(arg, "--sparse")) {
2432 revs->dense = 0;
2433 } else if (!strcmp(arg, "--in-commit-order")) {
2434 revs->tree_blobs_in_commit_order = 1;
2435 } else if (!strcmp(arg, "--remove-empty")) {
2436 revs->remove_empty_trees = 1;
2437 } else if (!strcmp(arg, "--merges")) {
2438 revs->min_parents = 2;
2439 } else if (!strcmp(arg, "--no-merges")) {
2440 revs->max_parents = 1;
2441 } else if (skip_prefix(arg, "--min-parents=", &optarg)) {
2442 revs->min_parents = parse_count(optarg);
2443 } else if (!strcmp(arg, "--no-min-parents")) {
2444 revs->min_parents = 0;
2445 } else if (skip_prefix(arg, "--max-parents=", &optarg)) {
2446 revs->max_parents = parse_count(optarg);
2447 } else if (!strcmp(arg, "--no-max-parents")) {
2448 revs->max_parents = -1;
2449 } else if (!strcmp(arg, "--boundary")) {
2450 revs->boundary = 1;
2451 } else if (!strcmp(arg, "--left-right")) {
2452 revs->left_right = 1;
2453 } else if (!strcmp(arg, "--left-only")) {
2454 if (revs->right_only)
2455 die(_("options '%s' and '%s' cannot be used together"),
2456 "--left-only", "--right-only/--cherry");
2457 revs->left_only = 1;
2458 } else if (!strcmp(arg, "--right-only")) {
2459 if (revs->left_only)
2460 die(_("options '%s' and '%s' cannot be used together"), "--right-only", "--left-only");
2461 revs->right_only = 1;
2462 } else if (!strcmp(arg, "--cherry")) {
2463 if (revs->left_only)
2464 die(_("options '%s' and '%s' cannot be used together"), "--cherry", "--left-only");
2465 revs->cherry_mark = 1;
2466 revs->right_only = 1;
2467 revs->max_parents = 1;
2468 revs->limited = 1;
2469 } else if (!strcmp(arg, "--count")) {
2470 revs->count = 1;
2471 } else if (!strcmp(arg, "--cherry-mark")) {
2472 if (revs->cherry_pick)
2473 die(_("options '%s' and '%s' cannot be used together"), "--cherry-mark", "--cherry-pick");
2474 revs->cherry_mark = 1;
2475 revs->limited = 1; /* needs limit_list() */
2476 } else if (!strcmp(arg, "--cherry-pick")) {
2477 if (revs->cherry_mark)
2478 die(_("options '%s' and '%s' cannot be used together"), "--cherry-pick", "--cherry-mark");
2479 revs->cherry_pick = 1;
2480 revs->limited = 1;
2481 } else if (!strcmp(arg, "--objects")) {
2482 revs->tag_objects = 1;
2483 revs->tree_objects = 1;
2484 revs->blob_objects = 1;
2485 } else if (!strcmp(arg, "--objects-edge")) {
2486 revs->tag_objects = 1;
2487 revs->tree_objects = 1;
2488 revs->blob_objects = 1;
2489 revs->edge_hint = 1;
2490 } else if (!strcmp(arg, "--objects-edge-aggressive")) {
2491 revs->tag_objects = 1;
2492 revs->tree_objects = 1;
2493 revs->blob_objects = 1;
2494 revs->edge_hint = 1;
2495 revs->edge_hint_aggressive = 1;
2496 } else if (!strcmp(arg, "--verify-objects")) {
2497 revs->tag_objects = 1;
2498 revs->tree_objects = 1;
2499 revs->blob_objects = 1;
2500 revs->verify_objects = 1;
2501 disable_commit_graph(revs->repo);
2502 } else if (!strcmp(arg, "--unpacked")) {
2503 revs->unpacked = 1;
2504 } else if (starts_with(arg, "--unpacked=")) {
2505 die(_("--unpacked=<packfile> no longer supported"));
2506 } else if (!strcmp(arg, "--no-kept-objects")) {
2507 revs->no_kept_objects = 1;
2508 revs->keep_pack_cache_flags |= IN_CORE_KEEP_PACKS;
2509 revs->keep_pack_cache_flags |= ON_DISK_KEEP_PACKS;
2510 } else if (skip_prefix(arg, "--no-kept-objects=", &optarg)) {
2511 revs->no_kept_objects = 1;
2512 if (!strcmp(optarg, "in-core"))
2513 revs->keep_pack_cache_flags |= IN_CORE_KEEP_PACKS;
2514 if (!strcmp(optarg, "on-disk"))
2515 revs->keep_pack_cache_flags |= ON_DISK_KEEP_PACKS;
2516 } else if (!strcmp(arg, "-r")) {
2517 revs->diff = 1;
2518 revs->diffopt.flags.recursive = 1;
2519 } else if (!strcmp(arg, "-t")) {
2520 revs->diff = 1;
2521 revs->diffopt.flags.recursive = 1;
2522 revs->diffopt.flags.tree_in_recursive = 1;
2523 } else if ((argcount = diff_merges_parse_opts(revs, argv))) {
2524 return argcount;
2525 } else if (!strcmp(arg, "-v")) {
2526 revs->verbose_header = 1;
2527 } else if (!strcmp(arg, "--pretty")) {
2528 revs->verbose_header = 1;
2529 revs->pretty_given = 1;
2530 get_commit_format(NULL, revs);
2531 } else if (skip_prefix(arg, "--pretty=", &optarg) ||
2532 skip_prefix(arg, "--format=", &optarg)) {
2534 * Detached form ("--pretty X" as opposed to "--pretty=X")
2535 * not allowed, since the argument is optional.
2537 revs->verbose_header = 1;
2538 revs->pretty_given = 1;
2539 get_commit_format(optarg, revs);
2540 } else if (!strcmp(arg, "--expand-tabs")) {
2541 revs->expand_tabs_in_log = 8;
2542 } else if (!strcmp(arg, "--no-expand-tabs")) {
2543 revs->expand_tabs_in_log = 0;
2544 } else if (skip_prefix(arg, "--expand-tabs=", &arg)) {
2545 int val;
2546 if (strtol_i(arg, 10, &val) < 0 || val < 0)
2547 die("'%s': not a non-negative integer", arg);
2548 revs->expand_tabs_in_log = val;
2549 } else if (!strcmp(arg, "--show-notes") || !strcmp(arg, "--notes")) {
2550 enable_default_display_notes(&revs->notes_opt, &revs->show_notes);
2551 revs->show_notes_given = 1;
2552 } else if (!strcmp(arg, "--show-signature")) {
2553 revs->show_signature = 1;
2554 } else if (!strcmp(arg, "--no-show-signature")) {
2555 revs->show_signature = 0;
2556 } else if (!strcmp(arg, "--show-linear-break")) {
2557 revs->break_bar = " ..........";
2558 revs->track_linear = 1;
2559 revs->track_first_time = 1;
2560 } else if (skip_prefix(arg, "--show-linear-break=", &optarg)) {
2561 revs->break_bar = xstrdup(optarg);
2562 revs->track_linear = 1;
2563 revs->track_first_time = 1;
2564 } else if (!strcmp(arg, "--show-notes-by-default")) {
2565 revs->show_notes_by_default = 1;
2566 } else if (skip_prefix(arg, "--show-notes=", &optarg) ||
2567 skip_prefix(arg, "--notes=", &optarg)) {
2568 if (starts_with(arg, "--show-notes=") &&
2569 revs->notes_opt.use_default_notes < 0)
2570 revs->notes_opt.use_default_notes = 1;
2571 enable_ref_display_notes(&revs->notes_opt, &revs->show_notes, optarg);
2572 revs->show_notes_given = 1;
2573 } else if (!strcmp(arg, "--no-notes")) {
2574 disable_display_notes(&revs->notes_opt, &revs->show_notes);
2575 revs->show_notes_given = 1;
2576 } else if (!strcmp(arg, "--standard-notes")) {
2577 revs->show_notes_given = 1;
2578 revs->notes_opt.use_default_notes = 1;
2579 } else if (!strcmp(arg, "--no-standard-notes")) {
2580 revs->notes_opt.use_default_notes = 0;
2581 } else if (!strcmp(arg, "--oneline")) {
2582 revs->verbose_header = 1;
2583 get_commit_format("oneline", revs);
2584 revs->pretty_given = 1;
2585 revs->abbrev_commit = 1;
2586 } else if (!strcmp(arg, "--graph")) {
2587 graph_clear(revs->graph);
2588 revs->graph = graph_init(revs);
2589 } else if (!strcmp(arg, "--no-graph")) {
2590 graph_clear(revs->graph);
2591 revs->graph = NULL;
2592 } else if (!strcmp(arg, "--encode-email-headers")) {
2593 revs->encode_email_headers = 1;
2594 } else if (!strcmp(arg, "--no-encode-email-headers")) {
2595 revs->encode_email_headers = 0;
2596 } else if (!strcmp(arg, "--root")) {
2597 revs->show_root_diff = 1;
2598 } else if (!strcmp(arg, "--no-commit-id")) {
2599 revs->no_commit_id = 1;
2600 } else if (!strcmp(arg, "--always")) {
2601 revs->always_show_header = 1;
2602 } else if (!strcmp(arg, "--no-abbrev")) {
2603 revs->abbrev = 0;
2604 } else if (!strcmp(arg, "--abbrev")) {
2605 revs->abbrev = DEFAULT_ABBREV;
2606 } else if (skip_prefix(arg, "--abbrev=", &optarg)) {
2607 revs->abbrev = strtoul(optarg, NULL, 10);
2608 if (revs->abbrev < MINIMUM_ABBREV)
2609 revs->abbrev = MINIMUM_ABBREV;
2610 else if (revs->abbrev > hexsz)
2611 revs->abbrev = hexsz;
2612 } else if (!strcmp(arg, "--abbrev-commit")) {
2613 revs->abbrev_commit = 1;
2614 revs->abbrev_commit_given = 1;
2615 } else if (!strcmp(arg, "--no-abbrev-commit")) {
2616 revs->abbrev_commit = 0;
2617 } else if (!strcmp(arg, "--full-diff")) {
2618 revs->diff = 1;
2619 revs->full_diff = 1;
2620 } else if (!strcmp(arg, "--show-pulls")) {
2621 revs->show_pulls = 1;
2622 } else if (!strcmp(arg, "--full-history")) {
2623 revs->simplify_history = 0;
2624 } else if (!strcmp(arg, "--relative-date")) {
2625 revs->date_mode.type = DATE_RELATIVE;
2626 revs->date_mode_explicit = 1;
2627 } else if ((argcount = parse_long_opt("date", argv, &optarg))) {
2628 parse_date_format(optarg, &revs->date_mode);
2629 revs->date_mode_explicit = 1;
2630 return argcount;
2631 } else if (!strcmp(arg, "--log-size")) {
2632 revs->show_log_size = 1;
2635 * Grepping the commit log
2637 else if ((argcount = parse_long_opt("author", argv, &optarg))) {
2638 add_header_grep(revs, GREP_HEADER_AUTHOR, optarg);
2639 return argcount;
2640 } else if ((argcount = parse_long_opt("committer", argv, &optarg))) {
2641 add_header_grep(revs, GREP_HEADER_COMMITTER, optarg);
2642 return argcount;
2643 } else if ((argcount = parse_long_opt("grep-reflog", argv, &optarg))) {
2644 add_header_grep(revs, GREP_HEADER_REFLOG, optarg);
2645 return argcount;
2646 } else if ((argcount = parse_long_opt("grep", argv, &optarg))) {
2647 add_message_grep(revs, optarg);
2648 return argcount;
2649 } else if (!strcmp(arg, "--basic-regexp")) {
2650 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_BRE;
2651 } else if (!strcmp(arg, "--extended-regexp") || !strcmp(arg, "-E")) {
2652 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_ERE;
2653 } else if (!strcmp(arg, "--regexp-ignore-case") || !strcmp(arg, "-i")) {
2654 revs->grep_filter.ignore_case = 1;
2655 revs->diffopt.pickaxe_opts |= DIFF_PICKAXE_IGNORE_CASE;
2656 } else if (!strcmp(arg, "--fixed-strings") || !strcmp(arg, "-F")) {
2657 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_FIXED;
2658 } else if (!strcmp(arg, "--perl-regexp") || !strcmp(arg, "-P")) {
2659 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_PCRE;
2660 } else if (!strcmp(arg, "--all-match")) {
2661 revs->grep_filter.all_match = 1;
2662 } else if (!strcmp(arg, "--invert-grep")) {
2663 revs->grep_filter.no_body_match = 1;
2664 } else if ((argcount = parse_long_opt("encoding", argv, &optarg))) {
2665 if (strcmp(optarg, "none"))
2666 git_log_output_encoding = xstrdup(optarg);
2667 else
2668 git_log_output_encoding = "";
2669 return argcount;
2670 } else if (!strcmp(arg, "--reverse")) {
2671 revs->reverse ^= 1;
2672 } else if (!strcmp(arg, "--children")) {
2673 revs->children.name = "children";
2674 revs->limited = 1;
2675 } else if (!strcmp(arg, "--ignore-missing")) {
2676 revs->ignore_missing = 1;
2677 } else if (opt && opt->allow_exclude_promisor_objects &&
2678 !strcmp(arg, "--exclude-promisor-objects")) {
2679 if (fetch_if_missing)
2680 BUG("exclude_promisor_objects can only be used when fetch_if_missing is 0");
2681 revs->exclude_promisor_objects = 1;
2682 } else {
2683 int opts = diff_opt_parse(&revs->diffopt, argv, argc, revs->prefix);
2684 if (!opts)
2685 unkv[(*unkc)++] = arg;
2686 return opts;
2689 return 1;
2692 void parse_revision_opt(struct rev_info *revs, struct parse_opt_ctx_t *ctx,
2693 const struct option *options,
2694 const char * const usagestr[])
2696 int n = handle_revision_opt(revs, ctx->argc, ctx->argv,
2697 &ctx->cpidx, ctx->out, NULL);
2698 if (n <= 0) {
2699 error("unknown option `%s'", ctx->argv[0]);
2700 usage_with_options(usagestr, options);
2702 ctx->argv += n;
2703 ctx->argc -= n;
2706 void revision_opts_finish(struct rev_info *revs)
2708 if (revs->graph && revs->track_linear)
2709 die(_("options '%s' and '%s' cannot be used together"), "--show-linear-break", "--graph");
2711 if (revs->graph) {
2712 revs->topo_order = 1;
2713 revs->rewrite_parents = 1;
2717 static int for_each_bisect_ref(struct ref_store *refs, each_ref_fn fn,
2718 void *cb_data, const char *term)
2720 struct strbuf bisect_refs = STRBUF_INIT;
2721 int status;
2722 strbuf_addf(&bisect_refs, "refs/bisect/%s", term);
2723 status = refs_for_each_fullref_in(refs, bisect_refs.buf, NULL, fn, cb_data);
2724 strbuf_release(&bisect_refs);
2725 return status;
2728 static int for_each_bad_bisect_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
2730 return for_each_bisect_ref(refs, fn, cb_data, term_bad);
2733 static int for_each_good_bisect_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
2735 return for_each_bisect_ref(refs, fn, cb_data, term_good);
2738 static int handle_revision_pseudo_opt(struct rev_info *revs,
2739 const char **argv, int *flags)
2741 const char *arg = argv[0];
2742 const char *optarg;
2743 struct ref_store *refs;
2744 int argcount;
2746 if (revs->repo != the_repository) {
2748 * We need some something like get_submodule_worktrees()
2749 * before we can go through all worktrees of a submodule,
2750 * .e.g with adding all HEADs from --all, which is not
2751 * supported right now, so stick to single worktree.
2753 if (!revs->single_worktree)
2754 BUG("--single-worktree cannot be used together with submodule");
2756 refs = get_main_ref_store(revs->repo);
2759 * NOTE!
2761 * Commands like "git shortlog" will not accept the options below
2762 * unless parse_revision_opt queues them (as opposed to erroring
2763 * out).
2765 * When implementing your new pseudo-option, remember to
2766 * register it in the list at the top of handle_revision_opt.
2768 if (!strcmp(arg, "--all")) {
2769 handle_refs(refs, revs, *flags, refs_for_each_ref);
2770 handle_refs(refs, revs, *flags, refs_head_ref);
2771 if (!revs->single_worktree) {
2772 struct all_refs_cb cb;
2774 init_all_refs_cb(&cb, revs, *flags);
2775 other_head_refs(handle_one_ref, &cb);
2777 clear_ref_exclusions(&revs->ref_excludes);
2778 } else if (!strcmp(arg, "--branches")) {
2779 if (revs->ref_excludes.hidden_refs_configured)
2780 return error(_("options '%s' and '%s' cannot be used together"),
2781 "--exclude-hidden", "--branches");
2782 handle_refs(refs, revs, *flags, refs_for_each_branch_ref);
2783 clear_ref_exclusions(&revs->ref_excludes);
2784 } else if (!strcmp(arg, "--bisect")) {
2785 read_bisect_terms(&term_bad, &term_good);
2786 handle_refs(refs, revs, *flags, for_each_bad_bisect_ref);
2787 handle_refs(refs, revs, *flags ^ (UNINTERESTING | BOTTOM),
2788 for_each_good_bisect_ref);
2789 revs->bisect = 1;
2790 } else if (!strcmp(arg, "--tags")) {
2791 if (revs->ref_excludes.hidden_refs_configured)
2792 return error(_("options '%s' and '%s' cannot be used together"),
2793 "--exclude-hidden", "--tags");
2794 handle_refs(refs, revs, *flags, refs_for_each_tag_ref);
2795 clear_ref_exclusions(&revs->ref_excludes);
2796 } else if (!strcmp(arg, "--remotes")) {
2797 if (revs->ref_excludes.hidden_refs_configured)
2798 return error(_("options '%s' and '%s' cannot be used together"),
2799 "--exclude-hidden", "--remotes");
2800 handle_refs(refs, revs, *flags, refs_for_each_remote_ref);
2801 clear_ref_exclusions(&revs->ref_excludes);
2802 } else if ((argcount = parse_long_opt("glob", argv, &optarg))) {
2803 struct all_refs_cb cb;
2804 init_all_refs_cb(&cb, revs, *flags);
2805 refs_for_each_glob_ref(get_main_ref_store(the_repository),
2806 handle_one_ref, optarg, &cb);
2807 clear_ref_exclusions(&revs->ref_excludes);
2808 return argcount;
2809 } else if ((argcount = parse_long_opt("exclude", argv, &optarg))) {
2810 add_ref_exclusion(&revs->ref_excludes, optarg);
2811 return argcount;
2812 } else if ((argcount = parse_long_opt("exclude-hidden", argv, &optarg))) {
2813 exclude_hidden_refs(&revs->ref_excludes, optarg);
2814 return argcount;
2815 } else if (skip_prefix(arg, "--branches=", &optarg)) {
2816 struct all_refs_cb cb;
2817 if (revs->ref_excludes.hidden_refs_configured)
2818 return error(_("options '%s' and '%s' cannot be used together"),
2819 "--exclude-hidden", "--branches");
2820 init_all_refs_cb(&cb, revs, *flags);
2821 refs_for_each_glob_ref_in(get_main_ref_store(the_repository),
2822 handle_one_ref, optarg,
2823 "refs/heads/", &cb);
2824 clear_ref_exclusions(&revs->ref_excludes);
2825 } else if (skip_prefix(arg, "--tags=", &optarg)) {
2826 struct all_refs_cb cb;
2827 if (revs->ref_excludes.hidden_refs_configured)
2828 return error(_("options '%s' and '%s' cannot be used together"),
2829 "--exclude-hidden", "--tags");
2830 init_all_refs_cb(&cb, revs, *flags);
2831 refs_for_each_glob_ref_in(get_main_ref_store(the_repository),
2832 handle_one_ref, optarg,
2833 "refs/tags/", &cb);
2834 clear_ref_exclusions(&revs->ref_excludes);
2835 } else if (skip_prefix(arg, "--remotes=", &optarg)) {
2836 struct all_refs_cb cb;
2837 if (revs->ref_excludes.hidden_refs_configured)
2838 return error(_("options '%s' and '%s' cannot be used together"),
2839 "--exclude-hidden", "--remotes");
2840 init_all_refs_cb(&cb, revs, *flags);
2841 refs_for_each_glob_ref_in(get_main_ref_store(the_repository),
2842 handle_one_ref, optarg,
2843 "refs/remotes/", &cb);
2844 clear_ref_exclusions(&revs->ref_excludes);
2845 } else if (!strcmp(arg, "--reflog")) {
2846 add_reflogs_to_pending(revs, *flags);
2847 } else if (!strcmp(arg, "--indexed-objects")) {
2848 add_index_objects_to_pending(revs, *flags);
2849 } else if (!strcmp(arg, "--alternate-refs")) {
2850 add_alternate_refs_to_pending(revs, *flags);
2851 } else if (!strcmp(arg, "--not")) {
2852 *flags ^= UNINTERESTING | BOTTOM;
2853 } else if (!strcmp(arg, "--no-walk")) {
2854 revs->no_walk = 1;
2855 } else if (skip_prefix(arg, "--no-walk=", &optarg)) {
2857 * Detached form ("--no-walk X" as opposed to "--no-walk=X")
2858 * not allowed, since the argument is optional.
2860 revs->no_walk = 1;
2861 if (!strcmp(optarg, "sorted"))
2862 revs->unsorted_input = 0;
2863 else if (!strcmp(optarg, "unsorted"))
2864 revs->unsorted_input = 1;
2865 else
2866 return error("invalid argument to --no-walk");
2867 } else if (!strcmp(arg, "--do-walk")) {
2868 revs->no_walk = 0;
2869 } else if (!strcmp(arg, "--single-worktree")) {
2870 revs->single_worktree = 1;
2871 } else if (skip_prefix(arg, ("--filter="), &arg)) {
2872 parse_list_objects_filter(&revs->filter, arg);
2873 } else if (!strcmp(arg, ("--no-filter"))) {
2874 list_objects_filter_set_no_filter(&revs->filter);
2875 } else {
2876 return 0;
2879 return 1;
2882 static void read_revisions_from_stdin(struct rev_info *revs,
2883 struct strvec *prune)
2885 struct strbuf sb;
2886 int seen_dashdash = 0;
2887 int seen_end_of_options = 0;
2888 int save_warning;
2889 int flags = 0;
2891 save_warning = warn_on_object_refname_ambiguity;
2892 warn_on_object_refname_ambiguity = 0;
2894 strbuf_init(&sb, 1000);
2895 while (strbuf_getline(&sb, stdin) != EOF) {
2896 if (!sb.len)
2897 break;
2899 if (!strcmp(sb.buf, "--")) {
2900 seen_dashdash = 1;
2901 break;
2904 if (!seen_end_of_options && sb.buf[0] == '-') {
2905 const char *argv[] = { sb.buf, NULL };
2907 if (!strcmp(sb.buf, "--end-of-options")) {
2908 seen_end_of_options = 1;
2909 continue;
2912 if (handle_revision_pseudo_opt(revs, argv, &flags) > 0)
2913 continue;
2915 die(_("invalid option '%s' in --stdin mode"), sb.buf);
2918 if (handle_revision_arg(sb.buf, revs, flags,
2919 REVARG_CANNOT_BE_FILENAME))
2920 die("bad revision '%s'", sb.buf);
2922 if (seen_dashdash)
2923 read_pathspec_from_stdin(&sb, prune);
2925 strbuf_release(&sb);
2926 warn_on_object_refname_ambiguity = save_warning;
2929 static void NORETURN diagnose_missing_default(const char *def)
2931 int flags;
2932 const char *refname;
2934 refname = refs_resolve_ref_unsafe(get_main_ref_store(the_repository),
2935 def, 0, NULL, &flags);
2936 if (!refname || !(flags & REF_ISSYMREF) || (flags & REF_ISBROKEN))
2937 die(_("your current branch appears to be broken"));
2939 skip_prefix(refname, "refs/heads/", &refname);
2940 die(_("your current branch '%s' does not have any commits yet"),
2941 refname);
2945 * Parse revision information, filling in the "rev_info" structure,
2946 * and removing the used arguments from the argument list.
2948 * Returns the number of arguments left that weren't recognized
2949 * (which are also moved to the head of the argument list)
2951 int setup_revisions(int argc, const char **argv, struct rev_info *revs, struct setup_revision_opt *opt)
2953 int i, flags, left, seen_dashdash, revarg_opt;
2954 struct strvec prune_data = STRVEC_INIT;
2955 int seen_end_of_options = 0;
2957 /* First, search for "--" */
2958 if (opt && opt->assume_dashdash) {
2959 seen_dashdash = 1;
2960 } else {
2961 seen_dashdash = 0;
2962 for (i = 1; i < argc; i++) {
2963 const char *arg = argv[i];
2964 if (strcmp(arg, "--"))
2965 continue;
2966 if (opt && opt->free_removed_argv_elements)
2967 free((char *)argv[i]);
2968 argv[i] = NULL;
2969 argc = i;
2970 if (argv[i + 1])
2971 strvec_pushv(&prune_data, argv + i + 1);
2972 seen_dashdash = 1;
2973 break;
2977 /* Second, deal with arguments and options */
2978 flags = 0;
2979 revarg_opt = opt ? opt->revarg_opt : 0;
2980 if (seen_dashdash)
2981 revarg_opt |= REVARG_CANNOT_BE_FILENAME;
2982 for (left = i = 1; i < argc; i++) {
2983 const char *arg = argv[i];
2984 if (!seen_end_of_options && *arg == '-') {
2985 int opts;
2987 opts = handle_revision_pseudo_opt(
2988 revs, argv + i,
2989 &flags);
2990 if (opts > 0) {
2991 i += opts - 1;
2992 continue;
2995 if (!strcmp(arg, "--stdin")) {
2996 if (revs->disable_stdin) {
2997 argv[left++] = arg;
2998 continue;
3000 if (revs->read_from_stdin++)
3001 die("--stdin given twice?");
3002 read_revisions_from_stdin(revs, &prune_data);
3003 continue;
3006 if (!strcmp(arg, "--end-of-options")) {
3007 seen_end_of_options = 1;
3008 continue;
3011 opts = handle_revision_opt(revs, argc - i, argv + i,
3012 &left, argv, opt);
3013 if (opts > 0) {
3014 i += opts - 1;
3015 continue;
3017 if (opts < 0)
3018 exit(128);
3019 continue;
3023 if (handle_revision_arg(arg, revs, flags, revarg_opt)) {
3024 int j;
3025 if (seen_dashdash || *arg == '^')
3026 die("bad revision '%s'", arg);
3028 /* If we didn't have a "--":
3029 * (1) all filenames must exist;
3030 * (2) all rev-args must not be interpretable
3031 * as a valid filename.
3032 * but the latter we have checked in the main loop.
3034 for (j = i; j < argc; j++)
3035 verify_filename(revs->prefix, argv[j], j == i);
3037 strvec_pushv(&prune_data, argv + i);
3038 break;
3041 revision_opts_finish(revs);
3043 if (prune_data.nr) {
3045 * If we need to introduce the magic "a lone ':' means no
3046 * pathspec whatsoever", here is the place to do so.
3048 * if (prune_data.nr == 1 && !strcmp(prune_data[0], ":")) {
3049 * prune_data.nr = 0;
3050 * prune_data.alloc = 0;
3051 * free(prune_data.path);
3052 * prune_data.path = NULL;
3053 * } else {
3054 * terminate prune_data.alloc with NULL and
3055 * call init_pathspec() to set revs->prune_data here.
3058 parse_pathspec(&revs->prune_data, 0, 0,
3059 revs->prefix, prune_data.v);
3061 strvec_clear(&prune_data);
3063 if (!revs->def)
3064 revs->def = opt ? opt->def : NULL;
3065 if (opt && opt->tweak)
3066 opt->tweak(revs);
3067 if (revs->show_merge)
3068 prepare_show_merge(revs);
3069 if (revs->def && !revs->pending.nr && !revs->rev_input_given) {
3070 struct object_id oid;
3071 struct object *object;
3072 struct object_context oc;
3073 if (get_oid_with_context(revs->repo, revs->def, 0, &oid, &oc))
3074 diagnose_missing_default(revs->def);
3075 object = get_reference(revs, revs->def, &oid, 0);
3076 add_pending_object_with_mode(revs, object, revs->def, oc.mode);
3077 object_context_release(&oc);
3080 /* Did the user ask for any diff output? Run the diff! */
3081 if (revs->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT)
3082 revs->diff = 1;
3084 /* Pickaxe, diff-filter and rename following need diffs */
3085 if ((revs->diffopt.pickaxe_opts & DIFF_PICKAXE_KINDS_MASK) ||
3086 revs->diffopt.filter ||
3087 revs->diffopt.flags.follow_renames)
3088 revs->diff = 1;
3090 if (revs->diffopt.objfind)
3091 revs->simplify_history = 0;
3093 if (revs->line_level_traverse) {
3094 if (want_ancestry(revs))
3095 revs->limited = 1;
3096 revs->topo_order = 1;
3099 if (revs->topo_order && !generation_numbers_enabled(the_repository))
3100 revs->limited = 1;
3102 if (revs->prune_data.nr) {
3103 copy_pathspec(&revs->pruning.pathspec, &revs->prune_data);
3104 /* Can't prune commits with rename following: the paths change.. */
3105 if (!revs->diffopt.flags.follow_renames)
3106 revs->prune = 1;
3107 if (!revs->full_diff)
3108 copy_pathspec(&revs->diffopt.pathspec,
3109 &revs->prune_data);
3112 diff_merges_setup_revs(revs);
3114 revs->diffopt.abbrev = revs->abbrev;
3116 diff_setup_done(&revs->diffopt);
3118 if (!is_encoding_utf8(get_log_output_encoding()))
3119 revs->grep_filter.ignore_locale = 1;
3120 compile_grep_patterns(&revs->grep_filter);
3122 if (revs->reflog_info && revs->limited)
3123 die("cannot combine --walk-reflogs with history-limiting options");
3124 if (revs->rewrite_parents && revs->children.name)
3125 die(_("options '%s' and '%s' cannot be used together"), "--parents", "--children");
3126 if (revs->filter.choice && !revs->blob_objects)
3127 die(_("object filtering requires --objects"));
3130 * Limitations on the graph functionality
3132 die_for_incompatible_opt3(!!revs->graph, "--graph",
3133 !!revs->reverse, "--reverse",
3134 !!revs->reflog_info, "--walk-reflogs");
3136 if (revs->no_walk && revs->graph)
3137 die(_("options '%s' and '%s' cannot be used together"), "--no-walk", "--graph");
3138 if (!revs->reflog_info && revs->grep_filter.use_reflog_filter)
3139 die(_("the option '%s' requires '%s'"), "--grep-reflog", "--walk-reflogs");
3141 if (revs->line_level_traverse &&
3142 (revs->diffopt.output_format & ~(DIFF_FORMAT_PATCH | DIFF_FORMAT_NO_OUTPUT)))
3143 die(_("-L does not yet support diff formats besides -p and -s"));
3145 if (revs->expand_tabs_in_log < 0)
3146 revs->expand_tabs_in_log = revs->expand_tabs_in_log_default;
3148 if (!revs->show_notes_given && revs->show_notes_by_default) {
3149 enable_default_display_notes(&revs->notes_opt, &revs->show_notes);
3150 revs->show_notes_given = 1;
3153 return left;
3156 static void release_revisions_cmdline(struct rev_cmdline_info *cmdline)
3158 unsigned int i;
3160 for (i = 0; i < cmdline->nr; i++)
3161 free((char *)cmdline->rev[i].name);
3162 free(cmdline->rev);
3165 static void release_revisions_mailmap(struct string_list *mailmap)
3167 if (!mailmap)
3168 return;
3169 clear_mailmap(mailmap);
3170 free(mailmap);
3173 static void release_revisions_topo_walk_info(struct topo_walk_info *info);
3175 static void free_void_commit_list(void *list)
3177 free_commit_list(list);
3180 void release_revisions(struct rev_info *revs)
3182 free_commit_list(revs->commits);
3183 free_commit_list(revs->ancestry_path_bottoms);
3184 release_display_notes(&revs->notes_opt);
3185 object_array_clear(&revs->pending);
3186 object_array_clear(&revs->boundary_commits);
3187 release_revisions_cmdline(&revs->cmdline);
3188 list_objects_filter_release(&revs->filter);
3189 clear_pathspec(&revs->prune_data);
3190 date_mode_release(&revs->date_mode);
3191 release_revisions_mailmap(revs->mailmap);
3192 free_grep_patterns(&revs->grep_filter);
3193 graph_clear(revs->graph);
3194 diff_free(&revs->diffopt);
3195 diff_free(&revs->pruning);
3196 reflog_walk_info_release(revs->reflog_info);
3197 release_revisions_topo_walk_info(revs->topo_walk_info);
3198 clear_decoration(&revs->children, free_void_commit_list);
3199 clear_decoration(&revs->merge_simplification, free);
3200 clear_decoration(&revs->treesame, free);
3201 line_log_free(revs);
3202 oidset_clear(&revs->missing_commits);
3205 static void add_child(struct rev_info *revs, struct commit *parent, struct commit *child)
3207 struct commit_list *l = xcalloc(1, sizeof(*l));
3209 l->item = child;
3210 l->next = add_decoration(&revs->children, &parent->object, l);
3213 static int remove_duplicate_parents(struct rev_info *revs, struct commit *commit)
3215 struct treesame_state *ts = lookup_decoration(&revs->treesame, &commit->object);
3216 struct commit_list **pp, *p;
3217 int surviving_parents;
3219 /* Examine existing parents while marking ones we have seen... */
3220 pp = &commit->parents;
3221 surviving_parents = 0;
3222 while ((p = *pp) != NULL) {
3223 struct commit *parent = p->item;
3224 if (parent->object.flags & TMP_MARK) {
3225 *pp = p->next;
3226 if (ts)
3227 compact_treesame(revs, commit, surviving_parents);
3228 continue;
3230 parent->object.flags |= TMP_MARK;
3231 surviving_parents++;
3232 pp = &p->next;
3234 /* clear the temporary mark */
3235 for (p = commit->parents; p; p = p->next) {
3236 p->item->object.flags &= ~TMP_MARK;
3238 /* no update_treesame() - removing duplicates can't affect TREESAME */
3239 return surviving_parents;
3242 struct merge_simplify_state {
3243 struct commit *simplified;
3246 static struct merge_simplify_state *locate_simplify_state(struct rev_info *revs, struct commit *commit)
3248 struct merge_simplify_state *st;
3250 st = lookup_decoration(&revs->merge_simplification, &commit->object);
3251 if (!st) {
3252 CALLOC_ARRAY(st, 1);
3253 add_decoration(&revs->merge_simplification, &commit->object, st);
3255 return st;
3258 static int mark_redundant_parents(struct commit *commit)
3260 struct commit_list *h = reduce_heads(commit->parents);
3261 int i = 0, marked = 0;
3262 struct commit_list *po, *pn;
3264 /* Want these for sanity-checking only */
3265 int orig_cnt = commit_list_count(commit->parents);
3266 int cnt = commit_list_count(h);
3269 * Not ready to remove items yet, just mark them for now, based
3270 * on the output of reduce_heads(). reduce_heads outputs the reduced
3271 * set in its original order, so this isn't too hard.
3273 po = commit->parents;
3274 pn = h;
3275 while (po) {
3276 if (pn && po->item == pn->item) {
3277 pn = pn->next;
3278 i++;
3279 } else {
3280 po->item->object.flags |= TMP_MARK;
3281 marked++;
3283 po=po->next;
3286 if (i != cnt || cnt+marked != orig_cnt)
3287 die("mark_redundant_parents %d %d %d %d", orig_cnt, cnt, i, marked);
3289 free_commit_list(h);
3291 return marked;
3294 static int mark_treesame_root_parents(struct commit *commit)
3296 struct commit_list *p;
3297 int marked = 0;
3299 for (p = commit->parents; p; p = p->next) {
3300 struct commit *parent = p->item;
3301 if (!parent->parents && (parent->object.flags & TREESAME)) {
3302 parent->object.flags |= TMP_MARK;
3303 marked++;
3307 return marked;
3311 * Awkward naming - this means one parent we are TREESAME to.
3312 * cf mark_treesame_root_parents: root parents that are TREESAME (to an
3313 * empty tree). Better name suggestions?
3315 static int leave_one_treesame_to_parent(struct rev_info *revs, struct commit *commit)
3317 struct treesame_state *ts = lookup_decoration(&revs->treesame, &commit->object);
3318 struct commit *unmarked = NULL, *marked = NULL;
3319 struct commit_list *p;
3320 unsigned n;
3322 for (p = commit->parents, n = 0; p; p = p->next, n++) {
3323 if (ts->treesame[n]) {
3324 if (p->item->object.flags & TMP_MARK) {
3325 if (!marked)
3326 marked = p->item;
3327 } else {
3328 if (!unmarked) {
3329 unmarked = p->item;
3330 break;
3337 * If we are TREESAME to a marked-for-deletion parent, but not to any
3338 * unmarked parents, unmark the first TREESAME parent. This is the
3339 * parent that the default simplify_history==1 scan would have followed,
3340 * and it doesn't make sense to omit that path when asking for a
3341 * simplified full history. Retaining it improves the chances of
3342 * understanding odd missed merges that took an old version of a file.
3344 * Example:
3346 * I--------*X A modified the file, but mainline merge X used
3347 * \ / "-s ours", so took the version from I. X is
3348 * `-*A--' TREESAME to I and !TREESAME to A.
3350 * Default log from X would produce "I". Without this check,
3351 * --full-history --simplify-merges would produce "I-A-X", showing
3352 * the merge commit X and that it changed A, but not making clear that
3353 * it had just taken the I version. With this check, the topology above
3354 * is retained.
3356 * Note that it is possible that the simplification chooses a different
3357 * TREESAME parent from the default, in which case this test doesn't
3358 * activate, and we _do_ drop the default parent. Example:
3360 * I------X A modified the file, but it was reverted in B,
3361 * \ / meaning mainline merge X is TREESAME to both
3362 * *A-*B parents.
3364 * Default log would produce "I" by following the first parent;
3365 * --full-history --simplify-merges will produce "I-A-B". But this is a
3366 * reasonable result - it presents a logical full history leading from
3367 * I to X, and X is not an important merge.
3369 if (!unmarked && marked) {
3370 marked->object.flags &= ~TMP_MARK;
3371 return 1;
3374 return 0;
3377 static int remove_marked_parents(struct rev_info *revs, struct commit *commit)
3379 struct commit_list **pp, *p;
3380 int nth_parent, removed = 0;
3382 pp = &commit->parents;
3383 nth_parent = 0;
3384 while ((p = *pp) != NULL) {
3385 struct commit *parent = p->item;
3386 if (parent->object.flags & TMP_MARK) {
3387 parent->object.flags &= ~TMP_MARK;
3388 *pp = p->next;
3389 free(p);
3390 removed++;
3391 compact_treesame(revs, commit, nth_parent);
3392 continue;
3394 pp = &p->next;
3395 nth_parent++;
3398 /* Removing parents can only increase TREESAMEness */
3399 if (removed && !(commit->object.flags & TREESAME))
3400 update_treesame(revs, commit);
3402 return nth_parent;
3405 static struct commit_list **simplify_one(struct rev_info *revs, struct commit *commit, struct commit_list **tail)
3407 struct commit_list *p;
3408 struct commit *parent;
3409 struct merge_simplify_state *st, *pst;
3410 int cnt;
3412 st = locate_simplify_state(revs, commit);
3415 * Have we handled this one?
3417 if (st->simplified)
3418 return tail;
3421 * An UNINTERESTING commit simplifies to itself, so does a
3422 * root commit. We do not rewrite parents of such commit
3423 * anyway.
3425 if ((commit->object.flags & UNINTERESTING) || !commit->parents) {
3426 st->simplified = commit;
3427 return tail;
3431 * Do we know what commit all of our parents that matter
3432 * should be rewritten to? Otherwise we are not ready to
3433 * rewrite this one yet.
3435 for (cnt = 0, p = commit->parents; p; p = p->next) {
3436 pst = locate_simplify_state(revs, p->item);
3437 if (!pst->simplified) {
3438 tail = &commit_list_insert(p->item, tail)->next;
3439 cnt++;
3441 if (revs->first_parent_only)
3442 break;
3444 if (cnt) {
3445 tail = &commit_list_insert(commit, tail)->next;
3446 return tail;
3450 * Rewrite our list of parents. Note that this cannot
3451 * affect our TREESAME flags in any way - a commit is
3452 * always TREESAME to its simplification.
3454 for (p = commit->parents; p; p = p->next) {
3455 pst = locate_simplify_state(revs, p->item);
3456 p->item = pst->simplified;
3457 if (revs->first_parent_only)
3458 break;
3461 if (revs->first_parent_only)
3462 cnt = 1;
3463 else
3464 cnt = remove_duplicate_parents(revs, commit);
3467 * It is possible that we are a merge and one side branch
3468 * does not have any commit that touches the given paths;
3469 * in such a case, the immediate parent from that branch
3470 * will be rewritten to be the merge base.
3472 * o----X X: the commit we are looking at;
3473 * / / o: a commit that touches the paths;
3474 * ---o----'
3476 * Further, a merge of an independent branch that doesn't
3477 * touch the path will reduce to a treesame root parent:
3479 * ----o----X X: the commit we are looking at;
3480 * / o: a commit that touches the paths;
3481 * r r: a root commit not touching the paths
3483 * Detect and simplify both cases.
3485 if (1 < cnt) {
3486 int marked = mark_redundant_parents(commit);
3487 marked += mark_treesame_root_parents(commit);
3488 if (marked)
3489 marked -= leave_one_treesame_to_parent(revs, commit);
3490 if (marked)
3491 cnt = remove_marked_parents(revs, commit);
3495 * A commit simplifies to itself if it is a root, if it is
3496 * UNINTERESTING, if it touches the given paths, or if it is a
3497 * merge and its parents don't simplify to one relevant commit
3498 * (the first two cases are already handled at the beginning of
3499 * this function).
3501 * Otherwise, it simplifies to what its sole relevant parent
3502 * simplifies to.
3504 if (!cnt ||
3505 (commit->object.flags & UNINTERESTING) ||
3506 !(commit->object.flags & TREESAME) ||
3507 (parent = one_relevant_parent(revs, commit->parents)) == NULL ||
3508 (revs->show_pulls && (commit->object.flags & PULL_MERGE)))
3509 st->simplified = commit;
3510 else {
3511 pst = locate_simplify_state(revs, parent);
3512 st->simplified = pst->simplified;
3514 return tail;
3517 static void simplify_merges(struct rev_info *revs)
3519 struct commit_list *list, *next;
3520 struct commit_list *yet_to_do, **tail;
3521 struct commit *commit;
3523 if (!revs->prune)
3524 return;
3526 /* feed the list reversed */
3527 yet_to_do = NULL;
3528 for (list = revs->commits; list; list = next) {
3529 commit = list->item;
3530 next = list->next;
3532 * Do not free(list) here yet; the original list
3533 * is used later in this function.
3535 commit_list_insert(commit, &yet_to_do);
3537 while (yet_to_do) {
3538 list = yet_to_do;
3539 yet_to_do = NULL;
3540 tail = &yet_to_do;
3541 while (list) {
3542 commit = pop_commit(&list);
3543 tail = simplify_one(revs, commit, tail);
3547 /* clean up the result, removing the simplified ones */
3548 list = revs->commits;
3549 revs->commits = NULL;
3550 tail = &revs->commits;
3551 while (list) {
3552 struct merge_simplify_state *st;
3554 commit = pop_commit(&list);
3555 st = locate_simplify_state(revs, commit);
3556 if (st->simplified == commit)
3557 tail = &commit_list_insert(commit, tail)->next;
3561 static void set_children(struct rev_info *revs)
3563 struct commit_list *l;
3564 for (l = revs->commits; l; l = l->next) {
3565 struct commit *commit = l->item;
3566 struct commit_list *p;
3568 for (p = commit->parents; p; p = p->next)
3569 add_child(revs, p->item, commit);
3573 void reset_revision_walk(void)
3575 clear_object_flags(SEEN | ADDED | SHOWN | TOPO_WALK_EXPLORED | TOPO_WALK_INDEGREE);
3578 static int mark_uninteresting(const struct object_id *oid,
3579 struct packed_git *pack UNUSED,
3580 uint32_t pos UNUSED,
3581 void *cb)
3583 struct rev_info *revs = cb;
3584 struct object *o = lookup_unknown_object(revs->repo, oid);
3585 o->flags |= UNINTERESTING | SEEN;
3586 return 0;
3589 define_commit_slab(indegree_slab, int);
3590 define_commit_slab(author_date_slab, timestamp_t);
3592 struct topo_walk_info {
3593 timestamp_t min_generation;
3594 struct prio_queue explore_queue;
3595 struct prio_queue indegree_queue;
3596 struct prio_queue topo_queue;
3597 struct indegree_slab indegree;
3598 struct author_date_slab author_date;
3601 static int topo_walk_atexit_registered;
3602 static unsigned int count_explore_walked;
3603 static unsigned int count_indegree_walked;
3604 static unsigned int count_topo_walked;
3606 static void trace2_topo_walk_statistics_atexit(void)
3608 struct json_writer jw = JSON_WRITER_INIT;
3610 jw_object_begin(&jw, 0);
3611 jw_object_intmax(&jw, "count_explore_walked", count_explore_walked);
3612 jw_object_intmax(&jw, "count_indegree_walked", count_indegree_walked);
3613 jw_object_intmax(&jw, "count_topo_walked", count_topo_walked);
3614 jw_end(&jw);
3616 trace2_data_json("topo_walk", the_repository, "statistics", &jw);
3618 jw_release(&jw);
3621 static inline void test_flag_and_insert(struct prio_queue *q, struct commit *c, int flag)
3623 if (c->object.flags & flag)
3624 return;
3626 c->object.flags |= flag;
3627 prio_queue_put(q, c);
3630 static void explore_walk_step(struct rev_info *revs)
3632 struct topo_walk_info *info = revs->topo_walk_info;
3633 struct commit_list *p;
3634 struct commit *c = prio_queue_get(&info->explore_queue);
3636 if (!c)
3637 return;
3639 if (repo_parse_commit_gently(revs->repo, c, 1) < 0)
3640 return;
3642 count_explore_walked++;
3644 if (revs->sort_order == REV_SORT_BY_AUTHOR_DATE)
3645 record_author_date(&info->author_date, c);
3647 if (revs->max_age != -1 && (c->date < revs->max_age))
3648 c->object.flags |= UNINTERESTING;
3650 if (process_parents(revs, c, NULL, NULL) < 0)
3651 return;
3653 if (c->object.flags & UNINTERESTING)
3654 mark_parents_uninteresting(revs, c);
3656 for (p = c->parents; p; p = p->next)
3657 test_flag_and_insert(&info->explore_queue, p->item, TOPO_WALK_EXPLORED);
3660 static void explore_to_depth(struct rev_info *revs,
3661 timestamp_t gen_cutoff)
3663 struct topo_walk_info *info = revs->topo_walk_info;
3664 struct commit *c;
3665 while ((c = prio_queue_peek(&info->explore_queue)) &&
3666 commit_graph_generation(c) >= gen_cutoff)
3667 explore_walk_step(revs);
3670 static void indegree_walk_step(struct rev_info *revs)
3672 struct commit_list *p;
3673 struct topo_walk_info *info = revs->topo_walk_info;
3674 struct commit *c = prio_queue_get(&info->indegree_queue);
3676 if (!c)
3677 return;
3679 if (repo_parse_commit_gently(revs->repo, c, 1) < 0)
3680 return;
3682 count_indegree_walked++;
3684 explore_to_depth(revs, commit_graph_generation(c));
3686 for (p = c->parents; p; p = p->next) {
3687 struct commit *parent = p->item;
3688 int *pi = indegree_slab_at(&info->indegree, parent);
3690 if (repo_parse_commit_gently(revs->repo, parent, 1) < 0)
3691 return;
3693 if (*pi)
3694 (*pi)++;
3695 else
3696 *pi = 2;
3698 test_flag_and_insert(&info->indegree_queue, parent, TOPO_WALK_INDEGREE);
3700 if (revs->first_parent_only)
3701 return;
3705 static void compute_indegrees_to_depth(struct rev_info *revs,
3706 timestamp_t gen_cutoff)
3708 struct topo_walk_info *info = revs->topo_walk_info;
3709 struct commit *c;
3710 while ((c = prio_queue_peek(&info->indegree_queue)) &&
3711 commit_graph_generation(c) >= gen_cutoff)
3712 indegree_walk_step(revs);
3715 static void release_revisions_topo_walk_info(struct topo_walk_info *info)
3717 if (!info)
3718 return;
3719 clear_prio_queue(&info->explore_queue);
3720 clear_prio_queue(&info->indegree_queue);
3721 clear_prio_queue(&info->topo_queue);
3722 clear_indegree_slab(&info->indegree);
3723 clear_author_date_slab(&info->author_date);
3724 free(info);
3727 static void reset_topo_walk(struct rev_info *revs)
3729 release_revisions_topo_walk_info(revs->topo_walk_info);
3730 revs->topo_walk_info = NULL;
3733 static void init_topo_walk(struct rev_info *revs)
3735 struct topo_walk_info *info;
3736 struct commit_list *list;
3737 if (revs->topo_walk_info)
3738 reset_topo_walk(revs);
3740 revs->topo_walk_info = xmalloc(sizeof(struct topo_walk_info));
3741 info = revs->topo_walk_info;
3742 memset(info, 0, sizeof(struct topo_walk_info));
3744 init_indegree_slab(&info->indegree);
3745 memset(&info->explore_queue, 0, sizeof(info->explore_queue));
3746 memset(&info->indegree_queue, 0, sizeof(info->indegree_queue));
3747 memset(&info->topo_queue, 0, sizeof(info->topo_queue));
3749 switch (revs->sort_order) {
3750 default: /* REV_SORT_IN_GRAPH_ORDER */
3751 info->topo_queue.compare = NULL;
3752 break;
3753 case REV_SORT_BY_COMMIT_DATE:
3754 info->topo_queue.compare = compare_commits_by_commit_date;
3755 break;
3756 case REV_SORT_BY_AUTHOR_DATE:
3757 init_author_date_slab(&info->author_date);
3758 info->topo_queue.compare = compare_commits_by_author_date;
3759 info->topo_queue.cb_data = &info->author_date;
3760 break;
3763 info->explore_queue.compare = compare_commits_by_gen_then_commit_date;
3764 info->indegree_queue.compare = compare_commits_by_gen_then_commit_date;
3766 info->min_generation = GENERATION_NUMBER_INFINITY;
3767 for (list = revs->commits; list; list = list->next) {
3768 struct commit *c = list->item;
3769 timestamp_t generation;
3771 if (repo_parse_commit_gently(revs->repo, c, 1))
3772 continue;
3774 test_flag_and_insert(&info->explore_queue, c, TOPO_WALK_EXPLORED);
3775 test_flag_and_insert(&info->indegree_queue, c, TOPO_WALK_INDEGREE);
3777 generation = commit_graph_generation(c);
3778 if (generation < info->min_generation)
3779 info->min_generation = generation;
3781 *(indegree_slab_at(&info->indegree, c)) = 1;
3783 if (revs->sort_order == REV_SORT_BY_AUTHOR_DATE)
3784 record_author_date(&info->author_date, c);
3786 compute_indegrees_to_depth(revs, info->min_generation);
3788 for (list = revs->commits; list; list = list->next) {
3789 struct commit *c = list->item;
3791 if (*(indegree_slab_at(&info->indegree, c)) == 1)
3792 prio_queue_put(&info->topo_queue, c);
3796 * This is unfortunate; the initial tips need to be shown
3797 * in the order given from the revision traversal machinery.
3799 if (revs->sort_order == REV_SORT_IN_GRAPH_ORDER)
3800 prio_queue_reverse(&info->topo_queue);
3802 if (trace2_is_enabled() && !topo_walk_atexit_registered) {
3803 atexit(trace2_topo_walk_statistics_atexit);
3804 topo_walk_atexit_registered = 1;
3808 static struct commit *next_topo_commit(struct rev_info *revs)
3810 struct commit *c;
3811 struct topo_walk_info *info = revs->topo_walk_info;
3813 /* pop next off of topo_queue */
3814 c = prio_queue_get(&info->topo_queue);
3816 if (c)
3817 *(indegree_slab_at(&info->indegree, c)) = 0;
3819 return c;
3822 static void expand_topo_walk(struct rev_info *revs, struct commit *commit)
3824 struct commit_list *p;
3825 struct topo_walk_info *info = revs->topo_walk_info;
3826 if (process_parents(revs, commit, NULL, NULL) < 0) {
3827 if (!revs->ignore_missing_links)
3828 die("Failed to traverse parents of commit %s",
3829 oid_to_hex(&commit->object.oid));
3832 count_topo_walked++;
3834 for (p = commit->parents; p; p = p->next) {
3835 struct commit *parent = p->item;
3836 int *pi;
3837 timestamp_t generation;
3839 if (parent->object.flags & UNINTERESTING)
3840 continue;
3842 if (repo_parse_commit_gently(revs->repo, parent, 1) < 0)
3843 continue;
3845 generation = commit_graph_generation(parent);
3846 if (generation < info->min_generation) {
3847 info->min_generation = generation;
3848 compute_indegrees_to_depth(revs, info->min_generation);
3851 pi = indegree_slab_at(&info->indegree, parent);
3853 (*pi)--;
3854 if (*pi == 1)
3855 prio_queue_put(&info->topo_queue, parent);
3857 if (revs->first_parent_only)
3858 return;
3862 int prepare_revision_walk(struct rev_info *revs)
3864 int i;
3865 struct object_array old_pending;
3866 struct commit_list **next = &revs->commits;
3868 memcpy(&old_pending, &revs->pending, sizeof(old_pending));
3869 revs->pending.nr = 0;
3870 revs->pending.alloc = 0;
3871 revs->pending.objects = NULL;
3872 for (i = 0; i < old_pending.nr; i++) {
3873 struct object_array_entry *e = old_pending.objects + i;
3874 struct commit *commit = handle_commit(revs, e);
3875 if (commit) {
3876 if (!(commit->object.flags & SEEN)) {
3877 commit->object.flags |= SEEN;
3878 next = commit_list_append(commit, next);
3882 object_array_clear(&old_pending);
3884 /* Signal whether we need per-parent treesame decoration */
3885 if (revs->simplify_merges ||
3886 (revs->limited && limiting_can_increase_treesame(revs)))
3887 revs->treesame.name = "treesame";
3889 if (revs->exclude_promisor_objects) {
3890 for_each_packed_object(mark_uninteresting, revs,
3891 FOR_EACH_OBJECT_PROMISOR_ONLY);
3894 if (!revs->reflog_info)
3895 prepare_to_use_bloom_filter(revs);
3896 if (!revs->unsorted_input)
3897 commit_list_sort_by_date(&revs->commits);
3898 if (revs->no_walk)
3899 return 0;
3900 if (revs->limited) {
3901 if (limit_list(revs) < 0)
3902 return -1;
3903 if (revs->topo_order)
3904 sort_in_topological_order(&revs->commits, revs->sort_order);
3905 } else if (revs->topo_order)
3906 init_topo_walk(revs);
3907 if (revs->line_level_traverse && want_ancestry(revs))
3909 * At the moment we can only do line-level log with parent
3910 * rewriting by performing this expensive pre-filtering step.
3911 * If parent rewriting is not requested, then we rather
3912 * perform the line-level log filtering during the regular
3913 * history traversal.
3915 line_log_filter(revs);
3916 if (revs->simplify_merges)
3917 simplify_merges(revs);
3918 if (revs->children.name)
3919 set_children(revs);
3921 return 0;
3924 static enum rewrite_result rewrite_one_1(struct rev_info *revs,
3925 struct commit **pp,
3926 struct prio_queue *queue)
3928 for (;;) {
3929 struct commit *p = *pp;
3930 if (!revs->limited)
3931 if (process_parents(revs, p, NULL, queue) < 0)
3932 return rewrite_one_error;
3933 if (p->object.flags & UNINTERESTING)
3934 return rewrite_one_ok;
3935 if (!(p->object.flags & TREESAME))
3936 return rewrite_one_ok;
3937 if (!p->parents)
3938 return rewrite_one_noparents;
3939 if (!(p = one_relevant_parent(revs, p->parents)))
3940 return rewrite_one_ok;
3941 *pp = p;
3945 static void merge_queue_into_list(struct prio_queue *q, struct commit_list **list)
3947 while (q->nr) {
3948 struct commit *item = prio_queue_peek(q);
3949 struct commit_list *p = *list;
3951 if (p && p->item->date >= item->date)
3952 list = &p->next;
3953 else {
3954 p = commit_list_insert(item, list);
3955 list = &p->next; /* skip newly added item */
3956 prio_queue_get(q); /* pop item */
3961 static enum rewrite_result rewrite_one(struct rev_info *revs, struct commit **pp)
3963 struct prio_queue queue = { compare_commits_by_commit_date };
3964 enum rewrite_result ret = rewrite_one_1(revs, pp, &queue);
3965 merge_queue_into_list(&queue, &revs->commits);
3966 clear_prio_queue(&queue);
3967 return ret;
3970 int rewrite_parents(struct rev_info *revs, struct commit *commit,
3971 rewrite_parent_fn_t rewrite_parent)
3973 struct commit_list **pp = &commit->parents;
3974 while (*pp) {
3975 struct commit_list *parent = *pp;
3976 switch (rewrite_parent(revs, &parent->item)) {
3977 case rewrite_one_ok:
3978 break;
3979 case rewrite_one_noparents:
3980 *pp = parent->next;
3981 continue;
3982 case rewrite_one_error:
3983 return -1;
3985 pp = &parent->next;
3987 remove_duplicate_parents(revs, commit);
3988 return 0;
3991 static int commit_match(struct commit *commit, struct rev_info *opt)
3993 int retval;
3994 const char *encoding;
3995 const char *message;
3996 struct strbuf buf = STRBUF_INIT;
3998 if (!opt->grep_filter.pattern_list && !opt->grep_filter.header_list)
3999 return 1;
4001 /* Prepend "fake" headers as needed */
4002 if (opt->grep_filter.use_reflog_filter) {
4003 strbuf_addstr(&buf, "reflog ");
4004 get_reflog_message(&buf, opt->reflog_info);
4005 strbuf_addch(&buf, '\n');
4009 * We grep in the user's output encoding, under the assumption that it
4010 * is the encoding they are most likely to write their grep pattern
4011 * for. In addition, it means we will match the "notes" encoding below,
4012 * so we will not end up with a buffer that has two different encodings
4013 * in it.
4015 encoding = get_log_output_encoding();
4016 message = repo_logmsg_reencode(the_repository, commit, NULL, encoding);
4018 /* Copy the commit to temporary if we are using "fake" headers */
4019 if (buf.len)
4020 strbuf_addstr(&buf, message);
4022 if (opt->grep_filter.header_list && opt->mailmap) {
4023 const char *commit_headers[] = { "author ", "committer ", NULL };
4025 if (!buf.len)
4026 strbuf_addstr(&buf, message);
4028 apply_mailmap_to_header(&buf, commit_headers, opt->mailmap);
4031 /* Append "fake" message parts as needed */
4032 if (opt->show_notes) {
4033 if (!buf.len)
4034 strbuf_addstr(&buf, message);
4035 format_display_notes(&commit->object.oid, &buf, encoding, 1);
4039 * Find either in the original commit message, or in the temporary.
4040 * Note that we cast away the constness of "message" here. It is
4041 * const because it may come from the cached commit buffer. That's OK,
4042 * because we know that it is modifiable heap memory, and that while
4043 * grep_buffer may modify it for speed, it will restore any
4044 * changes before returning.
4046 if (buf.len)
4047 retval = grep_buffer(&opt->grep_filter, buf.buf, buf.len);
4048 else
4049 retval = grep_buffer(&opt->grep_filter,
4050 (char *)message, strlen(message));
4051 strbuf_release(&buf);
4052 repo_unuse_commit_buffer(the_repository, commit, message);
4053 return retval;
4056 static inline int want_ancestry(const struct rev_info *revs)
4058 return (revs->rewrite_parents || revs->children.name);
4062 * Return a timestamp to be used for --since/--until comparisons for this
4063 * commit, based on the revision options.
4065 static timestamp_t comparison_date(const struct rev_info *revs,
4066 struct commit *commit)
4068 return revs->reflog_info ?
4069 get_reflog_timestamp(revs->reflog_info) :
4070 commit->date;
4073 enum commit_action get_commit_action(struct rev_info *revs, struct commit *commit)
4075 if (commit->object.flags & SHOWN)
4076 return commit_ignore;
4077 if (revs->unpacked && has_object_pack(&commit->object.oid))
4078 return commit_ignore;
4079 if (revs->no_kept_objects) {
4080 if (has_object_kept_pack(&commit->object.oid,
4081 revs->keep_pack_cache_flags))
4082 return commit_ignore;
4084 if (commit->object.flags & UNINTERESTING)
4085 return commit_ignore;
4086 if (revs->line_level_traverse && !want_ancestry(revs)) {
4088 * In case of line-level log with parent rewriting
4089 * prepare_revision_walk() already took care of all line-level
4090 * log filtering, and there is nothing left to do here.
4092 * If parent rewriting was not requested, then this is the
4093 * place to perform the line-level log filtering. Notably,
4094 * this check, though expensive, must come before the other,
4095 * cheaper filtering conditions, because the tracked line
4096 * ranges must be adjusted even when the commit will end up
4097 * being ignored based on other conditions.
4099 if (!line_log_process_ranges_arbitrary_commit(revs, commit))
4100 return commit_ignore;
4102 if (revs->min_age != -1 &&
4103 comparison_date(revs, commit) > revs->min_age)
4104 return commit_ignore;
4105 if (revs->max_age_as_filter != -1 &&
4106 comparison_date(revs, commit) < revs->max_age_as_filter)
4107 return commit_ignore;
4108 if (revs->min_parents || (revs->max_parents >= 0)) {
4109 int n = commit_list_count(commit->parents);
4110 if ((n < revs->min_parents) ||
4111 ((revs->max_parents >= 0) && (n > revs->max_parents)))
4112 return commit_ignore;
4114 if (!commit_match(commit, revs))
4115 return commit_ignore;
4116 if (revs->prune && revs->dense) {
4117 /* Commit without changes? */
4118 if (commit->object.flags & TREESAME) {
4119 int n;
4120 struct commit_list *p;
4121 /* drop merges unless we want parenthood */
4122 if (!want_ancestry(revs))
4123 return commit_ignore;
4125 if (revs->show_pulls && (commit->object.flags & PULL_MERGE))
4126 return commit_show;
4129 * If we want ancestry, then need to keep any merges
4130 * between relevant commits to tie together topology.
4131 * For consistency with TREESAME and simplification
4132 * use "relevant" here rather than just INTERESTING,
4133 * to treat bottom commit(s) as part of the topology.
4135 for (n = 0, p = commit->parents; p; p = p->next)
4136 if (relevant_commit(p->item))
4137 if (++n >= 2)
4138 return commit_show;
4139 return commit_ignore;
4142 return commit_show;
4145 define_commit_slab(saved_parents, struct commit_list *);
4147 #define EMPTY_PARENT_LIST ((struct commit_list *)-1)
4150 * You may only call save_parents() once per commit (this is checked
4151 * for non-root commits).
4153 static void save_parents(struct rev_info *revs, struct commit *commit)
4155 struct commit_list **pp;
4157 if (!revs->saved_parents_slab) {
4158 revs->saved_parents_slab = xmalloc(sizeof(struct saved_parents));
4159 init_saved_parents(revs->saved_parents_slab);
4162 pp = saved_parents_at(revs->saved_parents_slab, commit);
4165 * When walking with reflogs, we may visit the same commit
4166 * several times: once for each appearance in the reflog.
4168 * In this case, save_parents() will be called multiple times.
4169 * We want to keep only the first set of parents. We need to
4170 * store a sentinel value for an empty (i.e., NULL) parent
4171 * list to distinguish it from a not-yet-saved list, however.
4173 if (*pp)
4174 return;
4175 if (commit->parents)
4176 *pp = copy_commit_list(commit->parents);
4177 else
4178 *pp = EMPTY_PARENT_LIST;
4181 static void free_saved_parents(struct rev_info *revs)
4183 if (revs->saved_parents_slab)
4184 clear_saved_parents(revs->saved_parents_slab);
4187 struct commit_list *get_saved_parents(struct rev_info *revs, const struct commit *commit)
4189 struct commit_list *parents;
4191 if (!revs->saved_parents_slab)
4192 return commit->parents;
4194 parents = *saved_parents_at(revs->saved_parents_slab, commit);
4195 if (parents == EMPTY_PARENT_LIST)
4196 return NULL;
4197 return parents;
4200 enum commit_action simplify_commit(struct rev_info *revs, struct commit *commit)
4202 enum commit_action action = get_commit_action(revs, commit);
4204 if (action == commit_show &&
4205 revs->prune && revs->dense && want_ancestry(revs)) {
4207 * --full-diff on simplified parents is no good: it
4208 * will show spurious changes from the commits that
4209 * were elided. So we save the parents on the side
4210 * when --full-diff is in effect.
4212 if (revs->full_diff)
4213 save_parents(revs, commit);
4214 if (rewrite_parents(revs, commit, rewrite_one) < 0)
4215 return commit_error;
4217 return action;
4220 static void track_linear(struct rev_info *revs, struct commit *commit)
4222 if (revs->track_first_time) {
4223 revs->linear = 1;
4224 revs->track_first_time = 0;
4225 } else {
4226 struct commit_list *p;
4227 for (p = revs->previous_parents; p; p = p->next)
4228 if (p->item == NULL || /* first commit */
4229 oideq(&p->item->object.oid, &commit->object.oid))
4230 break;
4231 revs->linear = p != NULL;
4233 if (revs->reverse) {
4234 if (revs->linear)
4235 commit->object.flags |= TRACK_LINEAR;
4237 free_commit_list(revs->previous_parents);
4238 revs->previous_parents = copy_commit_list(commit->parents);
4241 static struct commit *get_revision_1(struct rev_info *revs)
4243 while (1) {
4244 struct commit *commit;
4246 if (revs->reflog_info)
4247 commit = next_reflog_entry(revs->reflog_info);
4248 else if (revs->topo_walk_info)
4249 commit = next_topo_commit(revs);
4250 else
4251 commit = pop_commit(&revs->commits);
4253 if (!commit)
4254 return NULL;
4256 if (revs->reflog_info)
4257 commit->object.flags &= ~(ADDED | SEEN | SHOWN);
4260 * If we haven't done the list limiting, we need to look at
4261 * the parents here. We also need to do the date-based limiting
4262 * that we'd otherwise have done in limit_list().
4264 if (!revs->limited) {
4265 if (revs->max_age != -1 &&
4266 comparison_date(revs, commit) < revs->max_age)
4267 continue;
4269 if (revs->reflog_info)
4270 try_to_simplify_commit(revs, commit);
4271 else if (revs->topo_walk_info)
4272 expand_topo_walk(revs, commit);
4273 else if (process_parents(revs, commit, &revs->commits, NULL) < 0) {
4274 if (!revs->ignore_missing_links)
4275 die("Failed to traverse parents of commit %s",
4276 oid_to_hex(&commit->object.oid));
4280 switch (simplify_commit(revs, commit)) {
4281 case commit_ignore:
4282 continue;
4283 case commit_error:
4284 die("Failed to simplify parents of commit %s",
4285 oid_to_hex(&commit->object.oid));
4286 default:
4287 if (revs->track_linear)
4288 track_linear(revs, commit);
4289 return commit;
4295 * Return true for entries that have not yet been shown. (This is an
4296 * object_array_each_func_t.)
4298 static int entry_unshown(struct object_array_entry *entry, void *cb_data UNUSED)
4300 return !(entry->item->flags & SHOWN);
4304 * If array is on the verge of a realloc, garbage-collect any entries
4305 * that have already been shown to try to free up some space.
4307 static void gc_boundary(struct object_array *array)
4309 if (array->nr == array->alloc)
4310 object_array_filter(array, entry_unshown, NULL);
4313 static void create_boundary_commit_list(struct rev_info *revs)
4315 unsigned i;
4316 struct commit *c;
4317 struct object_array *array = &revs->boundary_commits;
4318 struct object_array_entry *objects = array->objects;
4321 * If revs->commits is non-NULL at this point, an error occurred in
4322 * get_revision_1(). Ignore the error and continue printing the
4323 * boundary commits anyway. (This is what the code has always
4324 * done.)
4326 free_commit_list(revs->commits);
4327 revs->commits = NULL;
4330 * Put all of the actual boundary commits from revs->boundary_commits
4331 * into revs->commits
4333 for (i = 0; i < array->nr; i++) {
4334 c = (struct commit *)(objects[i].item);
4335 if (!c)
4336 continue;
4337 if (!(c->object.flags & CHILD_SHOWN))
4338 continue;
4339 if (c->object.flags & (SHOWN | BOUNDARY))
4340 continue;
4341 c->object.flags |= BOUNDARY;
4342 commit_list_insert(c, &revs->commits);
4346 * If revs->topo_order is set, sort the boundary commits
4347 * in topological order
4349 sort_in_topological_order(&revs->commits, revs->sort_order);
4352 static struct commit *get_revision_internal(struct rev_info *revs)
4354 struct commit *c = NULL;
4355 struct commit_list *l;
4357 if (revs->boundary == 2) {
4359 * All of the normal commits have already been returned,
4360 * and we are now returning boundary commits.
4361 * create_boundary_commit_list() has populated
4362 * revs->commits with the remaining commits to return.
4364 c = pop_commit(&revs->commits);
4365 if (c)
4366 c->object.flags |= SHOWN;
4367 return c;
4371 * If our max_count counter has reached zero, then we are done. We
4372 * don't simply return NULL because we still might need to show
4373 * boundary commits. But we want to avoid calling get_revision_1, which
4374 * might do a considerable amount of work finding the next commit only
4375 * for us to throw it away.
4377 * If it is non-zero, then either we don't have a max_count at all
4378 * (-1), or it is still counting, in which case we decrement.
4380 if (revs->max_count) {
4381 c = get_revision_1(revs);
4382 if (c) {
4383 while (revs->skip_count > 0) {
4384 revs->skip_count--;
4385 c = get_revision_1(revs);
4386 if (!c)
4387 break;
4391 if (revs->max_count > 0)
4392 revs->max_count--;
4395 if (c)
4396 c->object.flags |= SHOWN;
4398 if (!revs->boundary)
4399 return c;
4401 if (!c) {
4403 * get_revision_1() runs out the commits, and
4404 * we are done computing the boundaries.
4405 * switch to boundary commits output mode.
4407 revs->boundary = 2;
4410 * Update revs->commits to contain the list of
4411 * boundary commits.
4413 create_boundary_commit_list(revs);
4415 return get_revision_internal(revs);
4419 * boundary commits are the commits that are parents of the
4420 * ones we got from get_revision_1() but they themselves are
4421 * not returned from get_revision_1(). Before returning
4422 * 'c', we need to mark its parents that they could be boundaries.
4425 for (l = c->parents; l; l = l->next) {
4426 struct object *p;
4427 p = &(l->item->object);
4428 if (p->flags & (CHILD_SHOWN | SHOWN))
4429 continue;
4430 p->flags |= CHILD_SHOWN;
4431 gc_boundary(&revs->boundary_commits);
4432 add_object_array(p, NULL, &revs->boundary_commits);
4435 return c;
4438 struct commit *get_revision(struct rev_info *revs)
4440 struct commit *c;
4441 struct commit_list *reversed;
4443 if (revs->reverse) {
4444 reversed = NULL;
4445 while ((c = get_revision_internal(revs)))
4446 commit_list_insert(c, &reversed);
4447 free_commit_list(revs->commits);
4448 revs->commits = reversed;
4449 revs->reverse = 0;
4450 revs->reverse_output_stage = 1;
4453 if (revs->reverse_output_stage) {
4454 c = pop_commit(&revs->commits);
4455 if (revs->track_linear)
4456 revs->linear = !!(c && c->object.flags & TRACK_LINEAR);
4457 return c;
4460 c = get_revision_internal(revs);
4461 if (c && revs->graph)
4462 graph_update(revs->graph, c);
4463 if (!c) {
4464 free_saved_parents(revs);
4465 free_commit_list(revs->previous_parents);
4466 revs->previous_parents = NULL;
4468 return c;
4471 const char *get_revision_mark(const struct rev_info *revs, const struct commit *commit)
4473 if (commit->object.flags & BOUNDARY)
4474 return "-";
4475 else if (commit->object.flags & UNINTERESTING)
4476 return "^";
4477 else if (commit->object.flags & PATCHSAME)
4478 return "=";
4479 else if (!revs || revs->left_right) {
4480 if (commit->object.flags & SYMMETRIC_LEFT)
4481 return "<";
4482 else
4483 return ">";
4484 } else if (revs->graph)
4485 return "*";
4486 else if (revs->cherry_mark)
4487 return "+";
4488 return "";
4491 void put_revision_mark(const struct rev_info *revs, const struct commit *commit)
4493 const char *mark = get_revision_mark(revs, commit);
4494 if (!strlen(mark))
4495 return;
4496 fputs(mark, stdout);
4497 putchar(' ');