Skip to content

Commit 0fe02ec

Browse files
dschoGit for Windows Build Agent
authored and
Git for Windows Build Agent
committed
path-walk API: avoid adding a root tree more than once (#5195)
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.
2 parents 311d552 + 908ca3c commit 0fe02ec

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

Diff for: path-walk.c

+9-3
Original file line numberDiff line numberDiff line change
@@ -278,9 +278,11 @@ int walk_objects_by_path(struct path_walk_info *info)
278278
struct object_array_entry *pending = info->revs->pending.objects + i;
279279
struct object *obj = pending->item;
280280

281-
if (obj->type == OBJ_COMMIT)
281+
if (obj->type == OBJ_COMMIT || obj->flags & SEEN)
282282
continue;
283283

284+
obj->flags |= SEEN;
285+
284286
while (obj->type == OBJ_TAG) {
285287
struct tag *tag = lookup_tag(info->revs->repo,
286288
&obj->oid);
@@ -341,8 +343,12 @@ int walk_objects_by_path(struct path_walk_info *info)
341343
t = lookup_tree(info->revs->repo, oid);
342344

343345
if (t) {
344-
oidset_insert(&root_tree_set, oid);
345-
oid_array_append(&root_tree_list->oids, oid);
346+
if (t->object.flags & SEEN)
347+
continue;
348+
t->object.flags |= SEEN;
349+
350+
if (!oidset_insert(&root_tree_set, oid))
351+
oid_array_append(&root_tree_list->oids, oid);
346352
} else {
347353
warning("could not find tree %s", oid_to_hex(oid));
348354
}

Diff for: t/t6601-path-walk.sh

+22
Original file line numberDiff line numberDiff line change
@@ -276,4 +276,26 @@ test_expect_success 'topic, not base, boundary with pruning' '
276276
test_cmp expect.sorted out.sorted
277277
'
278278

279+
test_expect_success 'trees are reported exactly once' '
280+
test_when_finished "rm -rf unique-trees" &&
281+
test_create_repo unique-trees &&
282+
(
283+
cd unique-trees &&
284+
mkdir initial &&
285+
test_commit initial/file &&
286+
287+
git switch -c move-to-top &&
288+
git mv initial/file.t ./ &&
289+
test_tick &&
290+
git commit -m moved &&
291+
292+
git update-ref refs/heads/other HEAD
293+
) &&
294+
295+
test-tool -C unique-trees path-walk -- --all >out &&
296+
tree=$(git -C unique-trees rev-parse HEAD:) &&
297+
grep "$tree" out >out-filtered &&
298+
test_line_count = 1 out-filtered
299+
'
300+
279301
test_done

0 commit comments

Comments
 (0)