From 4cd0f5d2eb59f89ccbcde22c49155b3c9914a17d Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Mon, 7 Oct 2024 18:33:51 +0200 Subject: [PATCH] path-walk API: avoid adding a root tree more than once When adding tree objects, we are very careful to avoid adding the same tree object more than once. There was one small gap in that logic, though: when adding a root tree object. Two refs can easily share the same root tree object, and we should still not add it more than once. Signed-off-by: Johannes Schindelin --- path-walk.c | 4 +++- t/t6601-path-walk.sh | 22 ++++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/path-walk.c b/path-walk.c index 341bdd2ba4ef46..88885508f98789 100644 --- a/path-walk.c +++ b/path-walk.c @@ -344,9 +344,11 @@ static int setup_pending_objects(struct path_walk_info *info, struct object *obj = pending->item; /* Commits will be picked up by revision walk. */ - if (obj->type == OBJ_COMMIT) + if (obj->type == OBJ_COMMIT || obj->flags & SEEN) continue; + obj->flags |= SEEN; + /* Navigate annotated tag object chains. */ while (obj->type == OBJ_TAG) { struct tag *tag = lookup_tag(info->revs->repo, &obj->oid); diff --git a/t/t6601-path-walk.sh b/t/t6601-path-walk.sh index c89b0f1e19d9fb..b970700d3fbaac 100755 --- a/t/t6601-path-walk.sh +++ b/t/t6601-path-walk.sh @@ -397,4 +397,26 @@ test_expect_success 'trees are reported exactly once' ' test_line_count = 1 out-filtered ' +test_expect_success 'trees are reported exactly once' ' + test_when_finished "rm -rf unique-trees" && + test_create_repo unique-trees && + ( + cd unique-trees && + mkdir initial && + test_commit initial/file && + + git switch -c move-to-top && + git mv initial/file.t ./ && + test_tick && + git commit -m moved && + + git update-ref refs/heads/other HEAD + ) && + + test-tool -C unique-trees path-walk -- --all >out && + tree=$(git -C unique-trees rev-parse HEAD:) && + grep "$tree" out >out-filtered && + test_line_count = 1 out-filtered +' + test_done