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 <hanxin.hx@bytedance.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Han Xin committed Jul 1, 2022 at 09:34 UTC 3a1ea94a491e3e0bcbc2d55b4cfb0b9eacace608
2 files changed +71 -1
commit-graph.c
+1 -1
@@ -907,7 +907,7 @@ struct commit *lookup_commit_in_graph(struct repository *repo, const struct obje
907 return NULL;
908 if (!search_commit_pos_in_graph(id, repo->objects->commit_graph, &pos))
909 return NULL;
910 - if (!repo_has_object_file(repo, id))
910 + if (!has_object(repo, id, 0))
911 return NULL;
912
913 commit = lookup_commit(repo, id);
t/t5330-no-lazy-fetch-with-commit-graph.sh new
+70
@@ -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