Skip to content

Commit 3a1ea94

Browse files
chiyutianyigitster
authored andcommitted
commit-graph.c: no lazy fetch in lookup_commit_in_graph()
The commit-graph is used to opportunistically optimize accesses to certain pieces of information on commit objects, and lookup_commit_in_graph() tries to say "no" when the requested commit does not locally exist by returning NULL, in which case the caller can ask for (which may result in on-demand fetching from a promisor remote) and parse the commit object itself. However, it uses a wrong helper, repo_has_object_file(), to do so. This helper not only checks if an object is mmediately available in the local object store, but also tries to fetch from a promisor remote. But the fetch machinery calls lookup_commit_in_graph(), thus causing an infinite loop. We should make lookup_commit_in_graph() expect that a commit given to it can be legitimately missing from the local object store, by using the has_object_file() helper instead. Signed-off-by: Han Xin <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent dc8c8de commit 3a1ea94

File tree

2 files changed

+71
-1
lines changed

2 files changed

+71
-1
lines changed

commit-graph.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -907,7 +907,7 @@ struct commit *lookup_commit_in_graph(struct repository *repo, const struct obje
907907
return NULL;
908908
if (!search_commit_pos_in_graph(id, repo->objects->commit_graph, &pos))
909909
return NULL;
910-
if (!repo_has_object_file(repo, id))
910+
if (!has_object(repo, id, 0))
911911
return NULL;
912912

913913
commit = lookup_commit(repo, id);
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#!/bin/sh
2+
3+
test_description='test for no lazy fetch with the commit-graph'
4+
5+
. ./test-lib.sh
6+
7+
run_with_limited_processses () {
8+
# bash and ksh use "ulimit -u", dash uses "ulimit -p"
9+
if test -n "$BASH_VERSION"
10+
then
11+
ulimit_max_process="-u"
12+
elif test -n "$KSH_VERSION"
13+
then
14+
ulimit_max_process="-u"
15+
fi
16+
(ulimit ${ulimit_max_process-"-p"} 512 && "$@")
17+
}
18+
19+
test_lazy_prereq ULIMIT_PROCESSES '
20+
run_with_limited_processses true
21+
'
22+
23+
if ! test_have_prereq ULIMIT_PROCESSES
24+
then
25+
skip_all='skipping tests for no lazy fetch with the commit-graph, ulimit processes not available'
26+
test_done
27+
fi
28+
29+
test_expect_success 'setup: prepare a repository with a commit' '
30+
git init with-commit &&
31+
test_commit -C with-commit the-commit &&
32+
oid=$(git -C with-commit rev-parse HEAD)
33+
'
34+
35+
test_expect_success 'setup: prepare a repository with commit-graph contains the commit' '
36+
git init with-commit-graph &&
37+
echo "$(pwd)/with-commit/.git/objects" \
38+
>with-commit-graph/.git/objects/info/alternates &&
39+
# create a ref that points to the commit in alternates
40+
git -C with-commit-graph update-ref refs/ref_to_the_commit "$oid" &&
41+
# prepare some other objects to commit-graph
42+
test_commit -C with-commit-graph something &&
43+
git -c gc.writeCommitGraph=true -C with-commit-graph gc &&
44+
test_path_is_file with-commit-graph/.git/objects/info/commit-graph
45+
'
46+
47+
test_expect_success 'setup: change the alternates to what without the commit' '
48+
git init --bare without-commit &&
49+
git -C with-commit-graph cat-file -e $oid &&
50+
echo "$(pwd)/without-commit/objects" \
51+
>with-commit-graph/.git/objects/info/alternates &&
52+
test_must_fail git -C with-commit-graph cat-file -e $oid
53+
'
54+
55+
test_expect_success 'fetch any commit from promisor with the usage of the commit graph' '
56+
# setup promisor and prepare any commit to fetch
57+
git -C with-commit-graph remote add origin "$(pwd)/with-commit" &&
58+
git -C with-commit-graph config remote.origin.promisor true &&
59+
git -C with-commit-graph config remote.origin.partialclonefilter blob:none &&
60+
test_commit -C with-commit any-commit &&
61+
anycommit=$(git -C with-commit rev-parse HEAD) &&
62+
63+
run_with_limited_processses env GIT_TRACE="$(pwd)/trace.txt" \
64+
git -C with-commit-graph fetch origin $anycommit 2>err &&
65+
! grep "fatal: promisor-remote: unable to fork off fetch subprocess" err &&
66+
grep "git fetch origin" trace.txt >actual &&
67+
test_line_count = 1 actual
68+
'
69+
70+
test_done

0 commit comments

Comments
 (0)