Skip to content

Commit 930a3aa

Browse files
committed
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 <[email protected]>
1 parent 46171c9 commit 930a3aa

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

path-walk.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -296,9 +296,11 @@ int walk_objects_by_path(struct path_walk_info *info)
296296
struct object_array_entry *pending = info->revs->pending.objects + i;
297297
struct object *obj = pending->item;
298298

299-
if (obj->type == OBJ_COMMIT)
299+
if (obj->type == OBJ_COMMIT || obj->flags & SEEN)
300300
continue;
301301

302+
obj->flags |= SEEN;
303+
302304
while (obj->type == OBJ_TAG) {
303305
struct tag *tag = lookup_tag(info->revs->repo,
304306
&obj->oid);
@@ -358,9 +360,13 @@ int walk_objects_by_path(struct path_walk_info *info)
358360
oid = get_commit_tree_oid(c);
359361
t = lookup_tree(info->revs->repo, oid);
360362

363+
if (t->object.flags & SEEN)
364+
continue;
365+
t->object.flags |= SEEN;
366+
361367
if (t) {
362-
oidset_insert(&root_tree_set, oid);
363-
oid_array_append(&root_tree_list->oids, oid);
368+
if (!oidset_insert(&root_tree_set, oid))
369+
oid_array_append(&root_tree_list->oids, oid);
364370
} else {
365371
warning("could not find tree %s", oid_to_hex(oid));
366372
}

t/t6601-path-walk.sh

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,4 +311,26 @@ test_expect_success 'topic, not base, boundary with pruning' '
311311
test_cmp expect.sorted out.sorted
312312
'
313313

314+
test_expect_success 'trees are reported exactly once' '
315+
test_when_finished "rm -rf unique-trees" &&
316+
test_create_repo unique-trees &&
317+
(
318+
cd unique-trees &&
319+
mkdir initial &&
320+
test_commit initial/file &&
321+
322+
git switch -c move-to-top &&
323+
git mv initial/file.t ./ &&
324+
test_tick &&
325+
git commit -m moved &&
326+
327+
git update-ref refs/heads/other HEAD
328+
) &&
329+
330+
test-tool -C unique-trees path-walk -- --all >out &&
331+
tree=$(git -C unique-trees rev-parse HEAD:) &&
332+
grep "$tree" out >out-filtered &&
333+
test_line_count = 1 out-filtered
334+
'
335+
314336
test_done

0 commit comments

Comments
 (0)