Merge branch 'nd/format-patch-stat-width'
[git] / t / t7408-submodule-reference.sh
1 #!/bin/sh
2 #
3 # Copyright (c) 2009, Red Hat Inc, Author: Michael S. Tsirkin (mst@redhat.com)
4 #
5
6 test_description='test clone --reference'
7 . ./test-lib.sh
8
9 base_dir=$(pwd)
10
11 test_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
20 test_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
30 test_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
42 test_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
52 test_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
62 test_expect_success 'that reference gets used with add' '
63         (
64                 cd super/sub &&
65                 echo "0 objects, 0 kilobytes" >expected &&
66                 git count-objects >current &&
67                 diff expected current
68         )
69 '
70
71 # The tests up to this point, and repositories created by them
72 # (A, B, super and super/sub), are about setting up the stage
73 # for subsequent tests and meant to be kept throughout the
74 # remainder of the test.
75 # Tests from here on, if they create their own test repository,
76 # are expected to clean after themselves.
77
78 test_expect_success 'updating superproject keeps alternates' '
79         test_when_finished "rm -rf super-clone" &&
80         git clone super super-clone &&
81         git -C super-clone submodule update --init --reference ../B &&
82         test_alternate_is_used super-clone/.git/modules/sub/objects/info/alternates super-clone/sub
83 '
84
85 test_expect_success 'submodules use alternates when cloning a superproject' '
86         test_when_finished "rm -rf super-clone" &&
87         git clone --reference super --recursive super super-clone &&
88         (
89                 cd super-clone &&
90                 # test superproject has alternates setup correctly
91                 test_alternate_is_used .git/objects/info/alternates . &&
92                 # test submodule has correct setup
93                 test_alternate_is_used .git/modules/sub/objects/info/alternates sub
94         )
95 '
96
97 test_expect_success 'missing submodule alternate fails clone and submodule update' '
98         test_when_finished "rm -rf super-clone" &&
99         git clone super super2 &&
100         test_must_fail git clone --recursive --reference super2 super2 super-clone &&
101         (
102                 cd super-clone &&
103                 # test superproject has alternates setup correctly
104                 test_alternate_is_used .git/objects/info/alternates . &&
105                 # update of the submodule succeeds
106                 test_must_fail git submodule update --init &&
107                 # and we have no alternates:
108                 test_must_fail test_alternate_is_used .git/modules/sub/objects/info/alternates sub &&
109                 test_must_fail test_path_is_file sub/file1
110         )
111 '
112
113 test_expect_success 'ignoring missing submodule alternates passes clone and submodule update' '
114         test_when_finished "rm -rf super-clone" &&
115         git clone --reference-if-able super2 --recursive super2 super-clone &&
116         (
117                 cd super-clone &&
118                 # test superproject has alternates setup correctly
119                 test_alternate_is_used .git/objects/info/alternates . &&
120                 # update of the submodule succeeds
121                 git submodule update --init &&
122                 # and we have no alternates:
123                 test_must_fail test_alternate_is_used .git/modules/sub/objects/info/alternates sub &&
124                 test_path_is_file sub/file1
125         )
126 '
127
128 test_expect_success 'preparing second superproject with a nested submodule plus partial clone' '
129         test_create_repo supersuper &&
130         (
131                 cd supersuper &&
132                 echo "I am super super." >file &&
133                 git add file &&
134                 git commit -m B-super-super-initial
135                 git submodule add "file://$base_dir/super" subwithsub &&
136                 git commit -m B-super-super-added &&
137                 git submodule update --init --recursive &&
138                 git repack -ad
139         ) &&
140         git clone supersuper supersuper2 &&
141         (
142                 cd supersuper2 &&
143                 git submodule update --init
144         )
145 '
146
147 # At this point there are three root-level positories: A, B, super and super2
148
149 test_expect_success 'nested submodule alternate in works and is actually used' '
150         test_when_finished "rm -rf supersuper-clone" &&
151         git clone --recursive --reference supersuper supersuper supersuper-clone &&
152         (
153                 cd supersuper-clone &&
154                 # test superproject has alternates setup correctly
155                 test_alternate_is_used .git/objects/info/alternates . &&
156                 # immediate submodule has alternate:
157                 test_alternate_is_used .git/modules/subwithsub/objects/info/alternates subwithsub &&
158                 # nested submodule also has alternate:
159                 test_alternate_is_used .git/modules/subwithsub/modules/sub/objects/info/alternates subwithsub/sub
160         )
161 '
162
163 check_that_two_of_three_alternates_are_used() {
164         test_alternate_is_used .git/objects/info/alternates . &&
165         # immediate submodule has alternate:
166         test_alternate_is_used .git/modules/subwithsub/objects/info/alternates subwithsub &&
167         # but nested submodule has no alternate:
168         test_must_fail test_alternate_is_used .git/modules/subwithsub/modules/sub/objects/info/alternates subwithsub/sub
169 }
170
171
172 test_expect_success 'missing nested submodule alternate fails clone and submodule update' '
173         test_when_finished "rm -rf supersuper-clone" &&
174         test_must_fail git clone --recursive --reference supersuper2 supersuper2 supersuper-clone &&
175         (
176                 cd supersuper-clone &&
177                 check_that_two_of_three_alternates_are_used &&
178                 # update of the submodule fails
179                 test_must_fail git submodule update --init --recursive
180         )
181 '
182
183 test_expect_success 'missing nested submodule alternate in --reference-if-able mode' '
184         test_when_finished "rm -rf supersuper-clone" &&
185         git clone --recursive --reference-if-able supersuper2 supersuper2 supersuper-clone &&
186         (
187                 cd supersuper-clone &&
188                 check_that_two_of_three_alternates_are_used &&
189                 # update of the submodule succeeds
190                 git submodule update --init --recursive
191         )
192 '
193
194 test_done