3 # Copyright (C) 2006 Martin Waitz <tali@admingilde.org>
6 test_description
='test clone --reference'
7 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
=main
8 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
10 TEST_PASSES_SANITIZE_LEAK
=true
15 U
=$base_dir/UPLOAD_LOG
17 # create a commit in repo $1 with name $2
27 # check that there are $2 loose objects in repo $1
30 git
-C "$1" count-objects
>actual.raw
&&
31 cut
-d' ' -f1 <actual.raw
>actual
&&
32 test_cmp expect actual
35 test_expect_success
'preparing first repository' '
40 test_expect_success
'preparing second repository' '
43 git -C B repack -ad &&
47 test_expect_success
'cloning with reference (-l -s)' '
48 git clone -l -s --reference B A C
51 test_expect_success
'existence of info/alternates' '
52 test_line_count = 2 C/.git/objects/info/alternates
55 test_expect_success
'pulling from reference' '
56 git -C C pull ../B main
59 test_expect_success
'that reference gets used' '
63 test_expect_success
'cloning with reference (no -l -s)' '
64 GIT_TRACE_PACKET=$U.D git clone --reference B "file://$(pwd)/A" D
67 test_expect_success
'fetched no objects' '
72 test_expect_success
'existence of info/alternates' '
73 test_line_count = 1 D/.git/objects/info/alternates
76 test_expect_success
'pulling from reference' '
77 git -C D pull ../B main
80 test_expect_success
'that reference gets used' '
84 test_expect_success
'updating origin' '
86 git -C A repack -ad &&
90 test_expect_success
'pulling changes from origin' '
91 git -C C pull --no-rebase origin
94 # the 2 local objects are commit and tree from the merge
95 test_expect_success
'that alternate to origin gets used' '
99 test_expect_success
'pulling changes from origin' '
100 git -C D pull --no-rebase origin
103 # the 5 local objects are expected; file3 blob, commit in A to add it
104 # and its tree, and 2 are our tree and the merge commit.
105 test_expect_success
'check objects expected to exist locally' '
109 test_expect_success
'preparing alternate repository #1' '
110 test_create_repo F &&
114 test_expect_success
'cloning alternate repo #2 and adding changes to repo #1' '
119 test_expect_success
'cloning alternate repo #1, using #2 as reference' '
120 git clone --reference G F H
123 test_expect_success
'cloning with reference being subset of source (-l -s)' '
124 git clone -l -s --reference A B E
127 test_expect_success
'cloning with multiple references drops duplicates' '
128 git clone -s --reference B --reference A --reference B A dups &&
129 test_line_count = 2 dups/.git/objects/info/alternates
132 test_expect_success
'clone with reference from a tagged repository' '
134 cd A && git tag -a -m tagged HEAD
136 git clone --reference=A A I
139 test_expect_success
'prepare branched repository' '
143 git checkout -b other main^ &&
144 echo other >otherfile &&
146 git commit -m other &&
151 test_expect_success
'fetch with incomplete alternates' '
153 echo "$base_dir/A/.git/objects" >K/.git/objects/info/alternates &&
156 git remote add J "file://$base_dir/J" &&
157 GIT_TRACE_PACKET=$U.K git fetch J
159 main_object=$(cd A && git for-each-ref --format="%(objectname)" refs/heads/main) &&
161 ! grep " want $main_object" "$U.K" &&
162 tag_object=$(cd A && git for-each-ref --format="%(objectname)" refs/tags/HEAD) &&
163 ! grep " want $tag_object" "$U.K"
166 test_expect_success
'clone using repo with gitfile as a reference' '
167 git clone --separate-git-dir=L A M &&
168 git clone --reference=M A N &&
169 echo "$base_dir/L/objects" >expected &&
170 test_cmp expected "$base_dir/N/.git/objects/info/alternates"
173 test_expect_success
'clone using repo pointed at by gitfile as reference' '
174 git clone --reference=M/.git A O &&
175 echo "$base_dir/L/objects" >expected &&
176 test_cmp expected "$base_dir/O/.git/objects/info/alternates"
179 test_expect_success
'clone and dissociate from reference' '
182 cd P && test_commit one
186 cd Q && test_commit two
188 git clone --no-local --reference=P Q R &&
189 git clone --no-local --reference=P --dissociate Q S &&
190 # removing the reference P would corrupt R but not S
192 test_must_fail git -C R fsck &&
195 test_expect_success
'clone, dissociate from partial reference and repack' '
205 git clone --bare P Q &&
208 git checkout -b second &&
212 git clone --bare --dissociate --reference=P Q R &&
213 ls R/objects/pack/*.pack >packs.txt &&
214 test_line_count = 1 packs.txt
217 test_expect_success
'clone, dissociate from alternates' '
219 test_create_repo A &&
221 git clone --reference=A A B &&
222 test_line_count = 1 B/.git/objects/info/alternates &&
223 git clone --local --dissociate B C &&
224 ! test -f C/.git/objects/info/alternates &&
228 test_expect_success
'setup repo with garbage in objects/*' '
235 >.some-hidden-file &&
237 mkdir .some-hidden-dir &&
238 >.some-hidden-dir/some-file &&
239 >.some-hidden-dir/.some-dot-file &&
241 >some-dir/some-file &&
242 >some-dir/.some-dot-file
246 test_expect_success
'clone a repo with garbage in objects/*' '
247 for option in --local --no-hardlinks --shared --dissociate
249 git clone $option S S$option || return 1 &&
250 git -C S$option fsck || return 1
252 find S-* -name "*some*" | sort >actual &&
253 cat >expected <<-EOF &&
254 S--dissociate/.git/objects/.some-hidden-dir
255 S--dissociate/.git/objects/.some-hidden-dir/.some-dot-file
256 S--dissociate/.git/objects/.some-hidden-dir/some-file
257 S--dissociate/.git/objects/.some-hidden-file
258 S--dissociate/.git/objects/some-dir
259 S--dissociate/.git/objects/some-dir/.some-dot-file
260 S--dissociate/.git/objects/some-dir/some-file
261 S--dissociate/.git/objects/some-file
262 S--local/.git/objects/.some-hidden-dir
263 S--local/.git/objects/.some-hidden-dir/.some-dot-file
264 S--local/.git/objects/.some-hidden-dir/some-file
265 S--local/.git/objects/.some-hidden-file
266 S--local/.git/objects/some-dir
267 S--local/.git/objects/some-dir/.some-dot-file
268 S--local/.git/objects/some-dir/some-file
269 S--local/.git/objects/some-file
270 S--no-hardlinks/.git/objects/.some-hidden-dir
271 S--no-hardlinks/.git/objects/.some-hidden-dir/.some-dot-file
272 S--no-hardlinks/.git/objects/.some-hidden-dir/some-file
273 S--no-hardlinks/.git/objects/.some-hidden-file
274 S--no-hardlinks/.git/objects/some-dir
275 S--no-hardlinks/.git/objects/some-dir/.some-dot-file
276 S--no-hardlinks/.git/objects/some-dir/some-file
277 S--no-hardlinks/.git/objects/some-file
279 test_cmp expected actual
282 test_expect_success SYMLINKS
'setup repo with manually symlinked or unknown files at objects/' '
286 git config gc.auto 0 &&
294 find ?? -type d >loose-dirs &&
295 last_loose=$(tail -n 1 loose-dirs) &&
296 mv $last_loose a-loose-dir &&
297 ln -s a-loose-dir $last_loose &&
298 first_loose=$(head -n 1 loose-dirs) &&
303 mv $obj ../an-object &&
304 ln -s ../an-object $obj &&
307 echo unknown_content >unknown_file
310 git -C T rev-list --all --objects >T.objects
314 test_expect_success SYMLINKS
'clone repo with symlinked or unknown files at objects/' '
315 # None of these options work when cloning locally, since T has
316 # symlinks in its `$GIT_DIR/objects` directory
317 for option in --local --no-hardlinks --dissociate
319 test_must_fail git clone $option T T$option 2>err || return 1 &&
320 test_i18ngrep "symlink.*exists" err || return 1
323 # But `--shared` clones should still work, even when specifying
324 # a local path *and* that repository has symlinks present in its
325 # `$GIT_DIR/objects` directory.
326 git clone --shared T T--shared &&
327 git -C T--shared fsck &&
328 git -C T--shared rev-list --all --objects >T--shared.objects &&
329 test_cmp T.objects T--shared.objects &&
331 cd T--shared/.git/objects &&
332 find . -type f | sort >../../../T--shared.objects-files.raw &&
333 find . -type l | sort >../../../T--shared.objects-symlinks.raw
336 for raw in $(ls T*.raw)
338 sed -e "s!/../!/Y/!; s![0-9a-f]\{38,\}!Z!" -e "/commit-graph/d" \
339 -e "/multi-pack-index/d" -e "/rev/d" <$raw >$raw.de-sha-1 &&
340 sort $raw.de-sha-1 >$raw.de-sha || return 1
343 echo ./info/alternates >expected-files &&
344 test_cmp expected-files T--shared.objects-files.raw &&
345 test_must_be_empty T--shared.objects-symlinks.raw
348 test_expect_success SYMLINKS
'clone repo with symlinked objects directory' '
349 test_when_finished "rm -fr sensitive malicious" &&
351 mkdir -p sensitive &&
352 echo "secret" >sensitive/file &&
354 git init malicious &&
355 rm -fr malicious/.git/objects &&
356 ln -s "$(pwd)/sensitive" ./malicious/.git/objects &&
358 test_must_fail git clone --local malicious clone 2>err &&
360 test_path_is_missing clone &&
361 grep "is a symlink, refusing to clone with --local" err