3 test_description
='fetch/clone from a shallow clone'
5 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
=main
6 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
16 test_expect_success
'setup' '
21 git config --global transfer.fsckObjects true &&
22 test_oid_cache <<-\EOF
23 perl sha1:s/0034shallow %s/0036unshallow %s/
24 perl sha256:s/004cshallow %s/004eunshallow %s/
28 test_expect_success
'setup shallow clone' '
29 git clone --no-local --depth=2 .git shallow &&
30 git --git-dir=shallow/.git log --format=%s >actual &&
31 test_write_lines 4 3 >expect &&
32 test_cmp expect actual
35 test_expect_success
'clone from shallow clone' '
36 git clone --no-local shallow shallow2 &&
40 git log --format=%s >actual &&
41 test_write_lines 4 3 >expect &&
42 test_cmp expect actual
46 test_expect_success
'fetch from shallow clone' '
55 git log --format=%s origin/main >actual &&
56 test_write_lines 5 4 3 >expect &&
57 test_cmp expect actual
61 test_expect_success
'fetch --depth from shallow clone' '
68 git fetch --depth=2 &&
70 git log --format=%s origin/main >actual &&
71 test_write_lines 6 5 >expect &&
72 test_cmp expect actual
76 test_expect_success
'fetch --unshallow from shallow clone' '
79 git fetch --unshallow &&
81 git log --format=%s origin/main >actual &&
82 test_write_lines 6 5 4 3 >expect &&
83 test_cmp expect actual
87 test_expect_success
'fetch --unshallow from a full clone' '
88 git clone --no-local --depth=2 .git shallow3 &&
91 git log --format=%s >actual &&
92 test_write_lines 4 3 >expect &&
93 test_cmp expect actual &&
94 git -c fetch.writeCommitGraph fetch --unshallow &&
95 git log origin/main --format=%s >actual &&
96 test_write_lines 4 3 2 1 >expect &&
97 test_cmp expect actual
101 test_expect_success
'fetch something upstream has but hidden by clients shallow boundaries' '
102 # the blob "1" is available in .git but hidden by the
103 # shallow2/.git/shallow and it should be resent
104 ! git --git-dir=shallow2/.git cat-file blob $(echo 1|git hash-object --stdin) >/dev/null &&
107 git commit -m add-1-back &&
110 git fetch ../.git +refs/heads/main:refs/remotes/top/main &&
112 git log --format=%s top/main >actual &&
113 test_write_lines add-1-back 4 3 >expect &&
114 test_cmp expect actual
116 git --git-dir=shallow2/.git cat-file blob $(echo 1|git hash-object --stdin) >/dev/null
119 test_expect_success
'fetch that requires changes in .git/shallow is filtered' '
122 git checkout --orphan no-shallow &&
125 git init notshallow &&
128 git fetch ../shallow/.git refs/heads/*:refs/remotes/shallow/* &&
129 git for-each-ref --format="%(refname)" >actual.refs &&
130 echo refs/remotes/shallow/no-shallow >expect.refs &&
131 test_cmp expect.refs actual.refs &&
132 git log --format=%s shallow/no-shallow >actual &&
133 echo no-shallow >expect &&
134 test_cmp expect actual
138 test_expect_success
'fetch --update-shallow' '
143 git tag -m foo heavy-tag HEAD^ &&
144 git tag light-tag HEAD^:tracked
148 git fetch --update-shallow ../shallow/.git refs/heads/*:refs/remotes/shallow/* &&
150 git for-each-ref --sort=refname --format="%(refname)" >actual.refs &&
151 cat <<-\EOF >expect.refs &&
152 refs/remotes/shallow/main
153 refs/remotes/shallow/no-shallow
157 test_cmp expect.refs actual.refs &&
158 git log --format=%s shallow/main >actual &&
159 test_write_lines 7 6 5 4 3 >expect &&
160 test_cmp expect actual
164 test_expect_success
'fetch --update-shallow into a repo with submodules' '
165 git init a-submodule &&
166 test_commit -C a-submodule foo &&
168 test_when_finished "rm -rf repo-with-sub" &&
169 git init repo-with-sub &&
170 git -C repo-with-sub submodule add ../a-submodule a-submodule &&
171 git -C repo-with-sub commit -m "added submodule" &&
172 git -C repo-with-sub fetch --update-shallow ../shallow/.git refs/heads/*:refs/remotes/shallow/*
175 test_expect_success
'fetch --update-shallow a commit that is also a shallow point into a repo with submodules' '
176 test_when_finished "rm -rf repo-with-sub" &&
177 git init repo-with-sub &&
178 git -C repo-with-sub submodule add ../a-submodule a-submodule &&
179 git -C repo-with-sub commit -m "added submodule" &&
181 SHALLOW=$(cat shallow/.git/shallow) &&
182 git -C repo-with-sub fetch --update-shallow ../shallow/.git "$SHALLOW":refs/heads/a-shallow
185 test_expect_success
'fetch --update-shallow (with fetch.writeCommitGraph)' '
190 git tag -m foo heavy-tag-for-graph HEAD^ &&
191 git tag light-tag-for-graph HEAD^:tracked
193 test_config -C notshallow fetch.writeCommitGraph true &&
196 git fetch --update-shallow ../shallow/.git refs/heads/*:refs/remotes/shallow/* &&
198 git for-each-ref --sort=refname --format="%(refname)" >actual.refs &&
199 cat <<-EOF >expect.refs &&
200 refs/remotes/shallow/main
201 refs/remotes/shallow/no-shallow
203 refs/tags/heavy-tag-for-graph
205 refs/tags/light-tag-for-graph
207 test_cmp expect.refs actual.refs &&
208 git log --format=%s shallow/main >actual &&
209 test_write_lines 8 7 6 5 4 3 >expect &&
210 test_cmp expect actual
214 test_expect_success POSIXPERM
,SANITY
'shallow fetch from a read-only repo' '
215 cp -R .git read-only.git &&
216 test_when_finished "find read-only.git -type d -print | xargs chmod +w" &&
217 find read-only.git -print | xargs chmod -w &&
218 git clone --no-local --depth=2 read-only.git from-read-only &&
219 git --git-dir=from-read-only/.git log --format=%s >actual &&
220 test_write_lines add-1-back 4 >expect &&
221 test_cmp expect actual
224 test_expect_success
'.git/shallow is edited by repack' '
225 git init shallow-server &&
226 test_commit -C shallow-server A &&
227 test_commit -C shallow-server B &&
228 git -C shallow-server checkout -b branch &&
229 test_commit -C shallow-server C &&
230 test_commit -C shallow-server E &&
231 test_commit -C shallow-server D &&
232 d="$(git -C shallow-server rev-parse --verify D^0)" &&
233 git -C shallow-server checkout main &&
235 git clone --depth=1 --no-tags --no-single-branch \
236 "file://$PWD/shallow-server" shallow-client &&
238 : now remove the branch and fetch with prune &&
239 git -C shallow-server branch -D branch &&
240 git -C shallow-client fetch --prune --depth=1 \
241 origin "+refs/heads/*:refs/remotes/origin/*" &&
242 git -C shallow-client repack -adfl &&
243 test_must_fail git -C shallow-client rev-parse --verify $d^0 &&
244 ! grep $d shallow-client/.git/shallow &&
246 git -C shallow-server branch branch-orig $d &&
247 git -C shallow-client fetch --prune --depth=2 \
248 origin "+refs/heads/*:refs/remotes/origin/*"
251 .
"$TEST_DIRECTORY"/lib-httpd.sh
254 REPO
="$HTTPD_DOCUMENT_ROOT_PATH/repo"
256 test_expect_success
'shallow fetches check connectivity before writing shallow file' '
257 rm -rf "$REPO" client &&
260 test_commit -C "$REPO" one &&
261 test_commit -C "$REPO" two &&
262 test_commit -C "$REPO" three &&
266 # Use protocol v2 to ensure that shallow information is sent exactly
267 # once by the server, since we are planning to manipulate it.
268 git -C "$REPO" config protocol.version 2 &&
269 git -C client config protocol.version 2 &&
271 git -C client fetch --depth=2 "$HTTPD_URL/one_time_perl/repo" main:a_branch &&
273 # Craft a situation in which the server sends back an unshallow request
274 # with an empty packfile. This is done by refetching with a shorter
275 # depth (to ensure that the packfile is empty), and overwriting the
276 # shallow line in the response with the unshallow line we want.
277 printf "$(test_oid perl)" \
278 "$(git -C "$REPO" rev-parse HEAD)" \
279 "$(git -C "$REPO" rev-parse HEAD^)" \
280 >"$HTTPD_ROOT_PATH/one-time-perl" &&
281 test_must_fail env GIT_TEST_SIDEBAND_ALL=0 git -C client \
282 fetch --depth=1 "$HTTPD_URL/one_time_perl/repo" \
285 # Ensure that the one-time-perl script was used.
286 ! test -e "$HTTPD_ROOT_PATH/one-time-perl" &&
288 # Ensure that the resulting repo is consistent, despite our failure to
293 # DO NOT add non-httpd-specific tests here, because the last part of this
294 # test script is only executed when httpd is available and enabled.