list-objects-filter: implement filter tree:0
[git/git.git] / t / t5616-partial-clone.sh
CommitLineData
35a7ae95
JH
1#!/bin/sh
2
3test_description='git partial clone'
4
5. ./test-lib.sh
6
7# create a normal "src" repo where we can later create new commits.
8# expect_1.oids will contain a list of the OIDs of all blobs.
9test_expect_success 'setup normal src repo' '
10 echo "{print \$1}" >print_1.awk &&
11 echo "{print \$2}" >print_2.awk &&
12
13 git init src &&
14 for n in 1 2 3 4
15 do
16 echo "This is file: $n" > src/file.$n.txt
17 git -C src add file.$n.txt
18 git -C src commit -m "file $n"
19 git -C src ls-files -s file.$n.txt >>temp
20 done &&
21 awk -f print_2.awk <temp | sort >expect_1.oids &&
22 test_line_count = 4 expect_1.oids
23'
24
25# bare clone "src" giving "srv.bare" for use as our server.
26test_expect_success 'setup bare clone for server' '
27 git clone --bare "file://$(pwd)/src" srv.bare &&
28 git -C srv.bare config --local uploadpack.allowfilter 1 &&
29 git -C srv.bare config --local uploadpack.allowanysha1inwant 1
30'
31
32# do basic partial clone from "srv.bare"
33# confirm we are missing all of the known blobs.
34# confirm partial clone was registered in the local config.
35test_expect_success 'do partial clone 1' '
36 git clone --no-checkout --filter=blob:none "file://$(pwd)/srv.bare" pc1 &&
37 git -C pc1 rev-list HEAD --quiet --objects --missing=print \
38 | awk -f print_1.awk \
39 | sed "s/?//" \
40 | sort >observed.oids &&
41 test_cmp expect_1.oids observed.oids &&
42 test "$(git -C pc1 config --local core.repositoryformatversion)" = "1" &&
43 test "$(git -C pc1 config --local extensions.partialclone)" = "origin" &&
44 test "$(git -C pc1 config --local core.partialclonefilter)" = "blob:none"
45'
46
47# checkout master to force dynamic object fetch of blobs at HEAD.
48test_expect_success 'verify checkout with dynamic object fetch' '
49 git -C pc1 rev-list HEAD --quiet --objects --missing=print >observed &&
50 test_line_count = 4 observed &&
51 git -C pc1 checkout master &&
52 git -C pc1 rev-list HEAD --quiet --objects --missing=print >observed &&
53 test_line_count = 0 observed
54'
55
56# create new commits in "src" repo to establish a blame history on file.1.txt
57# and push to "srv.bare".
58test_expect_success 'push new commits to server' '
59 git -C src remote add srv "file://$(pwd)/srv.bare" &&
60 for x in a b c d e
61 do
aa57b871 62 echo "Mod file.1.txt $x" >>src/file.1.txt
35a7ae95
JH
63 git -C src add file.1.txt
64 git -C src commit -m "mod $x"
65 done &&
66 git -C src blame master -- file.1.txt >expect.blame &&
67 git -C src push -u srv master
68'
69
70# (partial) fetch in the partial clone repo from the promisor remote.
71# verify that fetch inherited the filter-spec from the config and DOES NOT
72# have the new blobs.
73test_expect_success 'partial fetch inherits filter settings' '
74 git -C pc1 fetch origin &&
75 git -C pc1 rev-list master..origin/master --quiet --objects --missing=print >observed &&
76 test_line_count = 5 observed
77'
78
79# force dynamic object fetch using diff.
80# we should only get 1 new blob (for the file in origin/master).
81test_expect_success 'verify diff causes dynamic object fetch' '
82 git -C pc1 diff master..origin/master -- file.1.txt &&
83 git -C pc1 rev-list master..origin/master --quiet --objects --missing=print >observed &&
84 test_line_count = 4 observed
85'
86
87# force full dynamic object fetch of the file's history using blame.
88# we should get the intermediate blobs for the file.
89test_expect_success 'verify blame causes dynamic object fetch' '
90 git -C pc1 blame origin/master -- file.1.txt >observed.blame &&
91 test_cmp expect.blame observed.blame &&
92 git -C pc1 rev-list master..origin/master --quiet --objects --missing=print >observed &&
93 test_line_count = 0 observed
94'
95
aa57b871
JH
96# create new commits in "src" repo to establish a history on file.2.txt
97# and push to "srv.bare".
98test_expect_success 'push new commits to server for file.2.txt' '
99 for x in a b c d e f
100 do
101 echo "Mod file.2.txt $x" >>src/file.2.txt
102 git -C src add file.2.txt
103 git -C src commit -m "mod $x"
104 done &&
105 git -C src push -u srv master
106'
107
3aa6694f 108# Do FULL fetch by disabling inherited filter-spec using --no-filter.
aa57b871
JH
109# Verify we have all the new blobs.
110test_expect_success 'override inherited filter-spec using --no-filter' '
111 git -C pc1 fetch --no-filter origin &&
112 git -C pc1 rev-list master..origin/master --quiet --objects --missing=print >observed &&
113 test_line_count = 0 observed
114'
115
3aa6694f
JH
116# create new commits in "src" repo to establish a history on file.3.txt
117# and push to "srv.bare".
118test_expect_success 'push new commits to server for file.3.txt' '
119 for x in a b c d e f
120 do
121 echo "Mod file.3.txt $x" >>src/file.3.txt
122 git -C src add file.3.txt
123 git -C src commit -m "mod $x"
124 done &&
125 git -C src push -u srv master
126'
127
128# Do a partial fetch and then try to manually fetch the missing objects.
129# This can be used as the basis of a pre-command hook to bulk fetch objects
130# perhaps combined with a command in dry-run mode.
131test_expect_success 'manual prefetch of missing objects' '
132 git -C pc1 fetch --filter=blob:none origin &&
133 git -C pc1 rev-list master..origin/master --quiet --objects --missing=print \
134 | awk -f print_1.awk \
135 | sed "s/?//" \
136 | sort >observed.oids &&
137 test_line_count = 6 observed.oids &&
138 git -C pc1 fetch-pack --stdin "file://$(pwd)/srv.bare" <observed.oids &&
139 git -C pc1 rev-list master..origin/master --quiet --objects --missing=print \
140 | awk -f print_1.awk \
141 | sed "s/?//" \
142 | sort >observed.oids &&
143 test_line_count = 0 observed.oids
144'
145
98a2ea46
JT
146test_expect_success 'partial clone with transfer.fsckobjects=1 uses index-pack --fsck-objects' '
147 git init src &&
148 test_commit -C src x &&
149 test_config -C src uploadpack.allowfilter 1 &&
150 test_config -C src uploadpack.allowanysha1inwant 1 &&
151
152 GIT_TRACE="$(pwd)/trace" git -c transfer.fsckobjects=1 \
153 clone --filter="blob:none" "file://$(pwd)/src" dst &&
154 grep "git index-pack.*--fsck-objects" trace
155'
156
bc5975d2
MD
157test_expect_success 'use fsck before and after manually fetching a missing subtree' '
158 # push new commit so server has a subtree
159 mkdir src/dir &&
160 echo "in dir" >src/dir/file.txt &&
161 git -C src add dir/file.txt &&
162 git -C src commit -m "file in dir" &&
163 git -C src push -u srv master &&
164 SUBTREE=$(git -C src rev-parse HEAD:dir) &&
165
166 rm -rf dst &&
167 git clone --no-checkout --filter=tree:0 "file://$(pwd)/srv.bare" dst &&
168 git -C dst fsck &&
169
170 # Make sure we only have commits, and all trees and blobs are missing.
171 git -C dst rev-list --missing=allow-any --objects master \
172 >fetched_objects &&
173 awk -f print_1.awk fetched_objects |
174 xargs -n1 git -C dst cat-file -t >fetched_types &&
175
176 sort -u fetched_types >unique_types.observed &&
177 echo commit >unique_types.expected &&
178 test_cmp unique_types.expected unique_types.observed &&
179
180 # Auto-fetch a tree with cat-file.
181 git -C dst cat-file -p $SUBTREE >tree_contents &&
182 grep file.txt tree_contents &&
183
184 # fsck still works after an auto-fetch of a tree.
185 git -C dst fsck &&
186
187 # Auto-fetch all remaining trees and blobs with --missing=error
188 git -C dst rev-list --missing=error --objects master >fetched_objects &&
189 test_line_count = 70 fetched_objects &&
190
191 awk -f print_1.awk fetched_objects |
192 xargs -n1 git -C dst cat-file -t >fetched_types &&
193
194 sort -u fetched_types >unique_types.observed &&
195 printf "blob\ncommit\ntree\n" >unique_types.expected &&
196 test_cmp unique_types.expected unique_types.observed
197'
198
a0c9016a
JT
199test_expect_success 'partial clone fetches blobs pointed to by refs even if normally filtered out' '
200 rm -rf src dst &&
201 git init src &&
202 test_commit -C src x &&
203 test_config -C src uploadpack.allowfilter 1 &&
204 test_config -C src uploadpack.allowanysha1inwant 1 &&
205
206 # Create a tag pointing to a blob.
207 BLOB=$(echo blob-contents | git -C src hash-object --stdin -w) &&
208 git -C src tag myblob "$BLOB" &&
209
210 git clone --filter="blob:none" "file://$(pwd)/src" dst 2>err &&
211 ! grep "does not point to a valid object" err &&
212 git -C dst fsck
213'
214
a7e67c11
JT
215. "$TEST_DIRECTORY"/lib-httpd.sh
216start_httpd
217
218# Converts bytes into a form suitable for inclusion in a sed command. For
219# example, "printf 'ab\r\n' | hex_unpack" results in '\x61\x62\x0d\x0a'.
220sed_escape () {
221 perl -e '$/ = undef; $input = <>; print unpack("H2" x length($input), $input)' |
222 sed 's/\(..\)/\\x\1/g'
223}
224
225test_expect_success 'upon cloning, check that all refs point to objects' '
226 SERVER="$HTTPD_DOCUMENT_ROOT_PATH/server" &&
227 rm -rf "$SERVER" repo &&
228 test_create_repo "$SERVER" &&
229 test_commit -C "$SERVER" foo &&
230 test_config -C "$SERVER" uploadpack.allowfilter 1 &&
231 test_config -C "$SERVER" uploadpack.allowanysha1inwant 1 &&
232
233 # Create a tag pointing to a blob.
234 BLOB=$(echo blob-contents | git -C "$SERVER" hash-object --stdin -w) &&
235 git -C "$SERVER" tag myblob "$BLOB" &&
236
237 # Craft a packfile not including that blob.
238 git -C "$SERVER" rev-parse HEAD |
239 git -C "$SERVER" pack-objects --stdout >incomplete.pack &&
240
241 # Replace the existing packfile with the crafted one. The protocol
242 # requires that the packfile be sent in sideband 1, hence the extra
243 # \x01 byte at the beginning.
244 printf "1,/packfile/!c %04x\\\\x01%s0000" \
245 "$(($(wc -c <incomplete.pack) + 5))" \
246 "$(sed_escape <incomplete.pack)" \
247 >"$HTTPD_ROOT_PATH/one-time-sed" &&
248
249 # Use protocol v2 because the sed command looks for the "packfile"
250 # section header.
251 test_config -C "$SERVER" protocol.version 2 &&
252 test_must_fail git -c protocol.version=2 clone \
253 --filter=blob:none $HTTPD_URL/one_time_sed/server repo 2>err &&
254
255 grep "did not send all necessary objects" err &&
256
257 # Ensure that the one-time-sed script was used.
258 ! test -e "$HTTPD_ROOT_PATH/one-time-sed"
259'
260
dc0a13f6
JT
261test_expect_success 'when partial cloning, tolerate server not sending target of tag' '
262 SERVER="$HTTPD_DOCUMENT_ROOT_PATH/server" &&
263 rm -rf "$SERVER" repo &&
264 test_create_repo "$SERVER" &&
265 test_commit -C "$SERVER" foo &&
266 test_config -C "$SERVER" uploadpack.allowfilter 1 &&
267 test_config -C "$SERVER" uploadpack.allowanysha1inwant 1 &&
268
269 # Create an annotated tag pointing to a blob.
270 BLOB=$(echo blob-contents | git -C "$SERVER" hash-object --stdin -w) &&
271 git -C "$SERVER" tag -m message -a myblob "$BLOB" &&
272
273 # Craft a packfile including the tag, but not the blob it points to.
8c4cc326
JT
274 # Also, omit objects referenced from HEAD in order to force a second
275 # fetch (to fetch missing objects) upon the automatic checkout that
276 # happens after a clone.
277 printf "%s\n%s\n--not\n%s\n%s\n" \
dc0a13f6
JT
278 $(git -C "$SERVER" rev-parse HEAD) \
279 $(git -C "$SERVER" rev-parse myblob) \
8c4cc326 280 $(git -C "$SERVER" rev-parse HEAD^{tree}) \
dc0a13f6
JT
281 $(git -C "$SERVER" rev-parse myblob^{blob}) |
282 git -C "$SERVER" pack-objects --thin --stdout >incomplete.pack &&
283
284 # Replace the existing packfile with the crafted one. The protocol
285 # requires that the packfile be sent in sideband 1, hence the extra
286 # \x01 byte at the beginning.
287 printf "1,/packfile/!c %04x\\\\x01%s0000" \
288 "$(($(wc -c <incomplete.pack) + 5))" \
289 "$(sed_escape <incomplete.pack)" \
290 >"$HTTPD_ROOT_PATH/one-time-sed" &&
291
292 # Use protocol v2 because the sed command looks for the "packfile"
293 # section header.
294 test_config -C "$SERVER" protocol.version 2 &&
295
296 # Exercise to make sure it works.
297 git -c protocol.version=2 clone \
8c4cc326
JT
298 --filter=blob:none $HTTPD_URL/one_time_sed/server repo 2> err &&
299 ! grep "missing object referenced by" err &&
dc0a13f6
JT
300
301 # Ensure that the one-time-sed script was used.
302 ! test -e "$HTTPD_ROOT_PATH/one-time-sed"
303'
304
a7e67c11
JT
305stop_httpd
306
35a7ae95 307test_done