Merge branch 'en/merge-recursive-skip-fix'
[git] / t / t0410-partial-clone.sh
1 #!/bin/sh
2
3 test_description='partial clone'
4
5 . ./test-lib.sh
6
7 delete_object () {
8         rm $1/.git/objects/$(echo $2 | sed -e 's|^..|&/|')
9 }
10
11 pack_as_from_promisor () {
12         HASH=$(git -C repo pack-objects .git/objects/pack/pack) &&
13         >repo/.git/objects/pack/pack-$HASH.promisor &&
14         echo $HASH
15 }
16
17 promise_and_delete () {
18         HASH=$(git -C repo rev-parse "$1") &&
19         git -C repo tag -a -m message my_annotated_tag "$HASH" &&
20         git -C repo rev-parse my_annotated_tag | pack_as_from_promisor &&
21         # tag -d prints a message to stdout, so redirect it
22         git -C repo tag -d my_annotated_tag >/dev/null &&
23         delete_object repo "$HASH"
24 }
25
26 test_expect_success 'extensions.partialclone without filter' '
27         test_create_repo server &&
28         git clone --filter="blob:none" "file://$(pwd)/server" client &&
29         git -C client config --unset core.partialclonefilter &&
30         git -C client fetch origin
31 '
32
33 test_expect_success 'missing reflog object, but promised by a commit, passes fsck' '
34         rm -rf repo &&
35         test_create_repo repo &&
36         test_commit -C repo my_commit &&
37
38         A=$(git -C repo commit-tree -m a HEAD^{tree}) &&
39         C=$(git -C repo commit-tree -m c -p $A HEAD^{tree}) &&
40
41         # Reference $A only from reflog, and delete it
42         git -C repo branch my_branch "$A" &&
43         git -C repo branch -f my_branch my_commit &&
44         delete_object repo "$A" &&
45
46         # State that we got $C, which refers to $A, from promisor
47         printf "$C\n" | pack_as_from_promisor &&
48
49         # Normally, it fails
50         test_must_fail git -C repo fsck &&
51
52         # But with the extension, it succeeds
53         git -C repo config core.repositoryformatversion 1 &&
54         git -C repo config extensions.partialclone "arbitrary string" &&
55         git -C repo fsck
56 '
57
58 test_expect_success 'missing reflog object, but promised by a tag, passes fsck' '
59         rm -rf repo &&
60         test_create_repo repo &&
61         test_commit -C repo my_commit &&
62
63         A=$(git -C repo commit-tree -m a HEAD^{tree}) &&
64         git -C repo tag -a -m d my_tag_name $A &&
65         T=$(git -C repo rev-parse my_tag_name) &&
66         git -C repo tag -d my_tag_name &&
67
68         # Reference $A only from reflog, and delete it
69         git -C repo branch my_branch "$A" &&
70         git -C repo branch -f my_branch my_commit &&
71         delete_object repo "$A" &&
72
73         # State that we got $T, which refers to $A, from promisor
74         printf "$T\n" | pack_as_from_promisor &&
75
76         git -C repo config core.repositoryformatversion 1 &&
77         git -C repo config extensions.partialclone "arbitrary string" &&
78         git -C repo fsck
79 '
80
81 test_expect_success 'missing reflog object alone fails fsck, even with extension set' '
82         rm -rf repo &&
83         test_create_repo repo &&
84         test_commit -C repo my_commit &&
85
86         A=$(git -C repo commit-tree -m a HEAD^{tree}) &&
87         B=$(git -C repo commit-tree -m b HEAD^{tree}) &&
88
89         # Reference $A only from reflog, and delete it
90         git -C repo branch my_branch "$A" &&
91         git -C repo branch -f my_branch my_commit &&
92         delete_object repo "$A" &&
93
94         git -C repo config core.repositoryformatversion 1 &&
95         git -C repo config extensions.partialclone "arbitrary string" &&
96         test_must_fail git -C repo fsck
97 '
98
99 test_expect_success 'missing ref object, but promised, passes fsck' '
100         rm -rf repo &&
101         test_create_repo repo &&
102         test_commit -C repo my_commit &&
103
104         A=$(git -C repo commit-tree -m a HEAD^{tree}) &&
105
106         # Reference $A only from ref
107         git -C repo branch my_branch "$A" &&
108         promise_and_delete "$A" &&
109
110         git -C repo config core.repositoryformatversion 1 &&
111         git -C repo config extensions.partialclone "arbitrary string" &&
112         git -C repo fsck
113 '
114
115 test_expect_success 'missing object, but promised, passes fsck' '
116         rm -rf repo &&
117         test_create_repo repo &&
118         test_commit -C repo 1 &&
119         test_commit -C repo 2 &&
120         test_commit -C repo 3 &&
121         git -C repo tag -a annotated_tag -m "annotated tag" &&
122
123         C=$(git -C repo rev-parse 1) &&
124         T=$(git -C repo rev-parse 2^{tree}) &&
125         B=$(git hash-object repo/3.t) &&
126         AT=$(git -C repo rev-parse annotated_tag) &&
127
128         promise_and_delete "$C" &&
129         promise_and_delete "$T" &&
130         promise_and_delete "$B" &&
131         promise_and_delete "$AT" &&
132
133         git -C repo config core.repositoryformatversion 1 &&
134         git -C repo config extensions.partialclone "arbitrary string" &&
135         git -C repo fsck
136 '
137
138 test_expect_success 'missing CLI object, but promised, passes fsck' '
139         rm -rf repo &&
140         test_create_repo repo &&
141         test_commit -C repo my_commit &&
142
143         A=$(git -C repo commit-tree -m a HEAD^{tree}) &&
144         promise_and_delete "$A" &&
145
146         git -C repo config core.repositoryformatversion 1 &&
147         git -C repo config extensions.partialclone "arbitrary string" &&
148         git -C repo fsck "$A"
149 '
150
151 test_expect_success 'fetching of missing objects' '
152         rm -rf repo &&
153         test_create_repo server &&
154         test_commit -C server foo &&
155         git -C server repack -a -d --write-bitmap-index &&
156
157         git clone "file://$(pwd)/server" repo &&
158         HASH=$(git -C repo rev-parse foo) &&
159         rm -rf repo/.git/objects/* &&
160
161         git -C repo config core.repositoryformatversion 1 &&
162         git -C repo config extensions.partialclone "origin" &&
163         git -C repo cat-file -p "$HASH" &&
164
165         # Ensure that the .promisor file is written, and check that its
166         # associated packfile contains the object
167         ls repo/.git/objects/pack/pack-*.promisor >promisorlist &&
168         test_line_count = 1 promisorlist &&
169         IDX=$(cat promisorlist | sed "s/promisor$/idx/") &&
170         git verify-pack --verbose "$IDX" | grep "$HASH"
171 '
172
173 test_expect_success 'rev-list stops traversal at missing and promised commit' '
174         rm -rf repo &&
175         test_create_repo repo &&
176         test_commit -C repo foo &&
177         test_commit -C repo bar &&
178
179         FOO=$(git -C repo rev-parse foo) &&
180         promise_and_delete "$FOO" &&
181
182         git -C repo config core.repositoryformatversion 1 &&
183         git -C repo config extensions.partialclone "arbitrary string" &&
184         git -C repo rev-list --exclude-promisor-objects --objects bar >out &&
185         grep $(git -C repo rev-parse bar) out &&
186         ! grep $FOO out
187 '
188
189 test_expect_success 'rev-list stops traversal at missing and promised tree' '
190         rm -rf repo &&
191         test_create_repo repo &&
192         test_commit -C repo foo &&
193         mkdir repo/a_dir &&
194         echo something >repo/a_dir/something &&
195         git -C repo add a_dir/something &&
196         git -C repo commit -m bar &&
197
198         # foo^{tree} (tree referenced from commit)
199         TREE=$(git -C repo rev-parse foo^{tree}) &&
200
201         # a tree referenced by HEAD^{tree} (tree referenced from tree)
202         TREE2=$(git -C repo ls-tree HEAD^{tree} | grep " tree " | head -1 | cut -b13-52) &&
203
204         promise_and_delete "$TREE" &&
205         promise_and_delete "$TREE2" &&
206
207         git -C repo config core.repositoryformatversion 1 &&
208         git -C repo config extensions.partialclone "arbitrary string" &&
209         git -C repo rev-list --exclude-promisor-objects --objects HEAD >out &&
210         grep $(git -C repo rev-parse foo) out &&
211         ! grep $TREE out &&
212         grep $(git -C repo rev-parse HEAD) out &&
213         ! grep $TREE2 out
214 '
215
216 test_expect_success 'rev-list stops traversal at missing and promised blob' '
217         rm -rf repo &&
218         test_create_repo repo &&
219         echo something >repo/something &&
220         git -C repo add something &&
221         git -C repo commit -m foo &&
222
223         BLOB=$(git -C repo hash-object -w something) &&
224         promise_and_delete "$BLOB" &&
225
226         git -C repo config core.repositoryformatversion 1 &&
227         git -C repo config extensions.partialclone "arbitrary string" &&
228         git -C repo rev-list --exclude-promisor-objects --objects HEAD >out &&
229         grep $(git -C repo rev-parse HEAD) out &&
230         ! grep $BLOB out
231 '
232
233 test_expect_success 'rev-list stops traversal at promisor commit, tree, and blob' '
234         rm -rf repo &&
235         test_create_repo repo &&
236         test_commit -C repo foo &&
237         test_commit -C repo bar &&
238         test_commit -C repo baz &&
239
240         COMMIT=$(git -C repo rev-parse foo) &&
241         TREE=$(git -C repo rev-parse bar^{tree}) &&
242         BLOB=$(git hash-object repo/baz.t) &&
243         printf "%s\n%s\n%s\n" $COMMIT $TREE $BLOB | pack_as_from_promisor &&
244
245         git -C repo config core.repositoryformatversion 1 &&
246         git -C repo config extensions.partialclone "arbitrary string" &&
247         git -C repo rev-list --exclude-promisor-objects --objects HEAD >out &&
248         ! grep $COMMIT out &&
249         ! grep $TREE out &&
250         ! grep $BLOB out &&
251         grep $(git -C repo rev-parse bar) out  # sanity check that some walking was done
252 '
253
254 test_expect_success 'rev-list accepts missing and promised objects on command line' '
255         rm -rf repo &&
256         test_create_repo repo &&
257         test_commit -C repo foo &&
258         test_commit -C repo bar &&
259         test_commit -C repo baz &&
260
261         COMMIT=$(git -C repo rev-parse foo) &&
262         TREE=$(git -C repo rev-parse bar^{tree}) &&
263         BLOB=$(git hash-object repo/baz.t) &&
264
265         promise_and_delete $COMMIT &&
266         promise_and_delete $TREE &&
267         promise_and_delete $BLOB &&
268
269         git -C repo config core.repositoryformatversion 1 &&
270         git -C repo config extensions.partialclone "arbitrary string" &&
271         git -C repo rev-list --exclude-promisor-objects --objects "$COMMIT" "$TREE" "$BLOB"
272 '
273
274 test_expect_success 'gc does not repack promisor objects' '
275         rm -rf repo &&
276         test_create_repo repo &&
277         test_commit -C repo my_commit &&
278
279         TREE_HASH=$(git -C repo rev-parse HEAD^{tree}) &&
280         HASH=$(printf "$TREE_HASH\n" | pack_as_from_promisor) &&
281
282         git -C repo config core.repositoryformatversion 1 &&
283         git -C repo config extensions.partialclone "arbitrary string" &&
284         git -C repo gc &&
285
286         # Ensure that the promisor packfile still exists, and remove it
287         test -e repo/.git/objects/pack/pack-$HASH.pack &&
288         rm repo/.git/objects/pack/pack-$HASH.* &&
289
290         # Ensure that the single other pack contains the commit, but not the tree
291         ls repo/.git/objects/pack/pack-*.pack >packlist &&
292         test_line_count = 1 packlist &&
293         git verify-pack repo/.git/objects/pack/pack-*.pack -v >out &&
294         grep "$(git -C repo rev-parse HEAD)" out &&
295         ! grep "$TREE_HASH" out
296 '
297
298 test_expect_success 'gc stops traversal when a missing but promised object is reached' '
299         rm -rf repo &&
300         test_create_repo repo &&
301         test_commit -C repo my_commit &&
302
303         TREE_HASH=$(git -C repo rev-parse HEAD^{tree}) &&
304         HASH=$(promise_and_delete $TREE_HASH) &&
305
306         git -C repo config core.repositoryformatversion 1 &&
307         git -C repo config extensions.partialclone "arbitrary string" &&
308         git -C repo gc &&
309
310         # Ensure that the promisor packfile still exists, and remove it
311         test -e repo/.git/objects/pack/pack-$HASH.pack &&
312         rm repo/.git/objects/pack/pack-$HASH.* &&
313
314         # Ensure that the single other pack contains the commit, but not the tree
315         ls repo/.git/objects/pack/pack-*.pack >packlist &&
316         test_line_count = 1 packlist &&
317         git verify-pack repo/.git/objects/pack/pack-*.pack -v >out &&
318         grep "$(git -C repo rev-parse HEAD)" out &&
319         ! grep "$TREE_HASH" out
320 '
321
322 LIB_HTTPD_PORT=12345  # default port, 410, cannot be used as non-root
323 . "$TEST_DIRECTORY"/lib-httpd.sh
324 start_httpd
325
326 test_expect_success 'fetching of missing objects from an HTTP server' '
327         rm -rf repo &&
328         SERVER="$HTTPD_DOCUMENT_ROOT_PATH/server" &&
329         test_create_repo "$SERVER" &&
330         test_commit -C "$SERVER" foo &&
331         git -C "$SERVER" repack -a -d --write-bitmap-index &&
332
333         git clone $HTTPD_URL/smart/server repo &&
334         HASH=$(git -C repo rev-parse foo) &&
335         rm -rf repo/.git/objects/* &&
336
337         git -C repo config core.repositoryformatversion 1 &&
338         git -C repo config extensions.partialclone "origin" &&
339         git -C repo cat-file -p "$HASH" &&
340
341         # Ensure that the .promisor file is written, and check that its
342         # associated packfile contains the object
343         ls repo/.git/objects/pack/pack-*.promisor >promisorlist &&
344         test_line_count = 1 promisorlist &&
345         IDX=$(cat promisorlist | sed "s/promisor$/idx/") &&
346         git verify-pack --verbose "$IDX" | grep "$HASH"
347 '
348
349 stop_httpd
350
351 test_done