From: Tamir Duberstein Date: Tue, 09 Jun 2026 02:36:34 GMT Subject: [PATCH v2 1/2] commit-reach: handle cycles in contains walk Message-ID: <20260608-ref-filter-memoized-contains-v2-1-e72720344a7c@gmail.com> In-Reply-To: <20260608-ref-filter-memoized-contains-v2-0-e72720344a7c@gmail.com> git tag --contains uses a memoized traversal that assumes commit ancestry is acyclic. Replacement refs can violate that assumption, causing the traversal to revisit a commit already on its stack indefinitely. Mark commits while they are active. If the traversal encounters an active commit, discard the cache because it cannot distinguish answers produced by the interrupted walk. Then fall back to the cycle-safe reachability walk for that candidate. Signed-off-by: Tamir Duberstein --- commit-reach.c | 30 ++++++++++++++++++++++++++---- commit-reach.h | 3 ++- t/t7004-tag.sh | 21 +++++++++++++++++++++ 3 files changed, 49 insertions(+), 5 deletions(-) diff --git a/commit-reach.c b/commit-reach.c index 9b3ea46d6f..65b618959b 100644 --- a/commit-reach.c +++ b/commit-reach.c @@ -708,7 +708,8 @@ static int in_commit_list(const struct commit_list *want, struct commit *c) /* * Test whether the candidate is contained in the list. - * Do not recurse to find out, though, but return -1 if inconclusive. + * Do not recurse to find out, though, but return CONTAINS_UNKNOWN if + * inconclusive. */ static enum contains_result contains_test(struct commit *candidate, const struct commit_list *want, @@ -744,7 +745,7 @@ static void push_to_contains_stack(struct commit *candidate, struct contains_sta } static enum contains_result contains_tag_algo(struct commit *candidate, - const struct commit_list *want, + struct commit_list *want, struct contains_cache *cache) { struct contains_stack contains_stack = { 0, 0, NULL }; @@ -765,6 +766,7 @@ static enum contains_result contains_tag_algo(struct commit *candidate, if (result != CONTAINS_UNKNOWN) return result; + *contains_cache_at(cache, candidate) = CONTAINS_IN_PROGRESS; push_to_contains_stack(candidate, &contains_stack); while (contains_stack.nr) { struct contains_stack_entry *entry = &contains_stack.contains_stack[contains_stack.nr - 1]; @@ -776,8 +778,8 @@ static enum contains_result contains_tag_algo(struct commit *candidate, contains_stack.nr--; } /* - * If we just popped the stack, parents->item has been marked, - * therefore contains_test will return a meaningful yes/no. + * A parent may have just been popped and marked, or may still + * be active when replacement refs create a cycle. */ else switch (contains_test(parents->item, want, cache, cutoff)) { case CONTAINS_YES: @@ -787,13 +789,33 @@ static enum contains_result contains_tag_algo(struct commit *candidate, case CONTAINS_NO: entry->parents = parents->next; break; + case CONTAINS_IN_PROGRESS: + /* + * Partial negative answers are not safe across a cycle. + * Discard them and use the cycle-safe reachability walk. + */ + goto cycle; case CONTAINS_UNKNOWN: + *contains_cache_at(cache, parents->item) = + CONTAINS_IN_PROGRESS; push_to_contains_stack(parents->item, &contains_stack); break; } } free(contains_stack.contains_stack); return contains_test(candidate, want, cache, cutoff); + +cycle: + free(contains_stack.contains_stack); + clear_contains_cache(cache); + init_contains_cache(cache); + + result = repo_is_descendant_of(the_repository, candidate, want); + if (result < 0) + exit(128); + *contains_cache_at(cache, candidate) = + result ? CONTAINS_YES : CONTAINS_NO; + return result ? CONTAINS_YES : CONTAINS_NO; } int commit_contains(struct ref_filter *filter, struct commit *commit, diff --git a/commit-reach.h b/commit-reach.h index 3f3a563d8a..f908d305b1 100644 --- a/commit-reach.h +++ b/commit-reach.h @@ -73,7 +73,8 @@ int ref_newer(const struct object_id *new_oid, const struct object_id *old_oid); enum contains_result { CONTAINS_UNKNOWN = 0, CONTAINS_NO, - CONTAINS_YES + CONTAINS_YES, + CONTAINS_IN_PROGRESS }; define_commit_slab(contains_cache, enum contains_result); diff --git a/t/t7004-tag.sh b/t/t7004-tag.sh index d918005dd9..1ed91bb66e 100755 --- a/t/t7004-tag.sh +++ b/t/t7004-tag.sh @@ -1611,6 +1611,27 @@ test_expect_success 'checking that first commit is in all tags (hash)' ' test_cmp expected actual ' +test_expect_success 'tag --contains handles cyclic replacement histories' ' + first=$(git rev-parse HEAD~2) && + second=$(git rev-parse HEAD~) && + third=$(git rev-parse HEAD) && + test_when_finished " + git replace -d $first + git replace -d $third + git tag -d cycle-a cycle-b + " && + git tag cycle-a "$first" && + git tag cycle-b "$third" && + git replace --graft "$first" "$third" "$second" && + git replace --graft "$third" "$first" && + cat >expected <<-\EOF && + cycle-a + cycle-b + EOF + git tag --contains="$second" --list "cycle-*" >actual && + test_cmp expected actual +' + # other ways of specifying the commit test_expect_success 'checking that first commit is in all tags (tag)' ' cat >expected <<-\EOF && -- 2.54.0.501.g0fb508de08