1#!/bin/sh
2#
3# Copyright (c) 2009, Red Hat Inc, Author: Michael S. Tsirkin (mst@redhat.com)
4#
5
6test_description='test clone --reference'
7. ./test-lib.sh
8
9base_dir=$(pwd)
10
11test_alternate_is_used () {
12	alternates_file="$1" &&
13	working_dir="$2" &&
14	test_line_count = 1 "$alternates_file" &&
15	echo "0 objects, 0 kilobytes" >expect &&
16	git -C "$working_dir" count-objects >actual &&
17	test_cmp expect actual
18}
19
20test_expect_success 'preparing first repository' '
21	test_create_repo A &&
22	(
23		cd A &&
24		echo first >file1 &&
25		git add file1 &&
26		git commit -m A-initial
27	)
28'
29
30test_expect_success 'preparing second repository' '
31	git clone A B &&
32	(
33		cd B &&
34		echo second >file2 &&
35		git add file2 &&
36		git commit -m B-addition &&
37		git repack -a -d &&
38		git prune
39	)
40'
41
42test_expect_success 'preparing superproject' '
43	test_create_repo super &&
44	(
45		cd super &&
46		echo file >file &&
47		git add file &&
48		git commit -m B-super-initial
49	)
50'
51
52test_expect_success 'submodule add --reference uses alternates' '
53	(
54		cd super &&
55		git submodule add --reference ../B "file://$base_dir/A" sub &&
56		git commit -m B-super-added &&
57		git repack -ad
58	) &&
59	test_alternate_is_used super/.git/modules/sub/objects/info/alternates super/sub
60'
61
62test_expect_success 'submodule add --reference with --dissociate does not use alternates' '
63	(
64		cd super &&
65		git submodule add --reference ../B --dissociate "file://$base_dir/A" sub-dissociate &&
66		git commit -m B-super-added &&
67		git repack -ad
68	) &&
69	test_path_is_missing super/.git/modules/sub-dissociate/objects/info/alternates
70'
71
72test_expect_success 'that reference gets used with add' '
73	(
74		cd super/sub &&
75		echo "0 objects, 0 kilobytes" >expected &&
76		git count-objects >current &&
77		diff expected current
78	)
79'
80
81# The tests up to this point, and repositories created by them
82# (A, B, super and super/sub), are about setting up the stage
83# for subsequent tests and meant to be kept throughout the
84# remainder of the test.
85# Tests from here on, if they create their own test repository,
86# are expected to clean after themselves.
87
88test_expect_success 'updating superproject keeps alternates' '
89	test_when_finished "rm -rf super-clone" &&
90	git clone super super-clone &&
91	git -C super-clone submodule update --init --reference ../B &&
92	test_alternate_is_used super-clone/.git/modules/sub/objects/info/alternates super-clone/sub
93'
94
95test_expect_success 'updating superproject with --dissociate does not keep alternates' '
96	test_when_finished "rm -rf super-clone" &&
97	git clone super super-clone &&
98	git -C super-clone submodule update --init --reference ../B --dissociate &&
99	test_path_is_missing super-clone/.git/modules/sub/objects/info/alternates
100'
101
102test_expect_success 'submodules use alternates when cloning a superproject' '
103	test_when_finished "rm -rf super-clone" &&
104	git clone --reference super --recursive super super-clone &&
105	(
106		cd super-clone &&
107		# test superproject has alternates setup correctly
108		test_alternate_is_used .git/objects/info/alternates . &&
109		# test submodule has correct setup
110		test_alternate_is_used .git/modules/sub/objects/info/alternates sub
111	)
112'
113
114test_expect_success 'missing submodule alternate fails clone and submodule update' '
115	test_when_finished "rm -rf super-clone" &&
116	git clone super super2 &&
117	test_must_fail git clone --recursive --reference super2 super2 super-clone &&
118	(
119		cd super-clone &&
120		# test superproject has alternates setup correctly
121		test_alternate_is_used .git/objects/info/alternates . &&
122		# update of the submodule succeeds
123		test_must_fail git submodule update --init &&
124		# and we have no alternates:
125		test_path_is_missing .git/modules/sub/objects/info/alternates &&
126		test_path_is_missing sub/file1
127	)
128'
129
130test_expect_success 'ignoring missing submodule alternates passes clone and submodule update' '
131	test_when_finished "rm -rf super-clone" &&
132	git clone --reference-if-able super2 --recursive super2 super-clone &&
133	(
134		cd super-clone &&
135		# test superproject has alternates setup correctly
136		test_alternate_is_used .git/objects/info/alternates . &&
137		# update of the submodule succeeds
138		git submodule update --init &&
139		# and we have no alternates:
140		test_path_is_missing .git/modules/sub/objects/info/alternates &&
141		test_path_is_file sub/file1
142	)
143'
144
145test_expect_success 'preparing second superproject with a nested submodule plus partial clone' '
146	test_create_repo supersuper &&
147	(
148		cd supersuper &&
149		echo "I am super super." >file &&
150		git add file &&
151		git commit -m B-super-super-initial &&
152		git submodule add "file://$base_dir/super" subwithsub &&
153		git commit -m B-super-super-added &&
154		git submodule update --init --recursive &&
155		git repack -ad
156	) &&
157	git clone supersuper supersuper2 &&
158	(
159		cd supersuper2 &&
160		git submodule update --init
161	)
162'
163
164# At this point there are three root-level positories: A, B, super and super2
165
166test_expect_success 'nested submodule alternate in works and is actually used' '
167	test_when_finished "rm -rf supersuper-clone" &&
168	git clone --recursive --reference supersuper supersuper supersuper-clone &&
169	(
170		cd supersuper-clone &&
171		# test superproject has alternates setup correctly
172		test_alternate_is_used .git/objects/info/alternates . &&
173		# immediate submodule has alternate:
174		test_alternate_is_used .git/modules/subwithsub/objects/info/alternates subwithsub &&
175		# nested submodule also has alternate:
176		test_alternate_is_used .git/modules/subwithsub/modules/sub/objects/info/alternates subwithsub/sub
177	)
178'
179
180check_that_two_of_three_alternates_are_used() {
181	test_alternate_is_used .git/objects/info/alternates . &&
182	# immediate submodule has alternate:
183	test_alternate_is_used .git/modules/subwithsub/objects/info/alternates subwithsub &&
184	# but nested submodule has no alternate:
185	test_path_is_missing .git/modules/subwithsub/modules/sub/objects/info/alternates
186}
187
188
189test_expect_success 'missing nested submodule alternate fails clone and submodule update' '
190	test_when_finished "rm -rf supersuper-clone" &&
191	test_must_fail git clone --recursive --reference supersuper2 supersuper2 supersuper-clone &&
192	(
193		cd supersuper-clone &&
194		check_that_two_of_three_alternates_are_used &&
195		# update of the submodule fails
196		test_must_fail git submodule update --init --recursive
197	)
198'
199
200test_expect_success 'missing nested submodule alternate in --reference-if-able mode' '
201	test_when_finished "rm -rf supersuper-clone" &&
202	git clone --recursive --reference-if-able supersuper2 supersuper2 supersuper-clone &&
203	(
204		cd supersuper-clone &&
205		check_that_two_of_three_alternates_are_used &&
206		# update of the submodule succeeds
207		git submodule update --init --recursive
208	)
209'
210
211test_done
212