4f681dbbe11c7f37d47d30bfa33a215c0dd9e189
[platform/upstream/git.git] / t / t5537-fetch-shallow.sh
1 #!/bin/sh
2
3 test_description='fetch/clone from a shallow clone'
4
5 . ./test-lib.sh
6
7 commit() {
8         echo "$1" >tracked &&
9         git add tracked &&
10         git commit -m "$1"
11 }
12
13 test_expect_success 'setup' '
14         commit 1 &&
15         commit 2 &&
16         commit 3 &&
17         commit 4 &&
18         git config --global transfer.fsckObjects true &&
19         test_oid_cache <<-EOF
20         perl sha1:s/0034shallow %s/0036unshallow %s/
21         perl sha256:s/004cshallow %s/004eunshallow %s/
22         EOF
23 '
24
25 test_expect_success 'setup shallow clone' '
26         git clone --no-local --depth=2 .git shallow &&
27         git --git-dir=shallow/.git log --format=%s >actual &&
28         cat <<EOF >expect &&
29 4
30 3
31 EOF
32         test_cmp expect actual
33 '
34
35 test_expect_success 'clone from shallow clone' '
36         git clone --no-local shallow shallow2 &&
37         (
38         cd shallow2 &&
39         git fsck &&
40         git log --format=%s >actual &&
41         cat <<EOF >expect &&
42 4
43 3
44 EOF
45         test_cmp expect actual
46         )
47 '
48
49 test_expect_success 'fetch from shallow clone' '
50         (
51         cd shallow &&
52         commit 5
53         ) &&
54         (
55         cd shallow2 &&
56         git fetch &&
57         git fsck &&
58         git log --format=%s origin/master >actual &&
59         cat <<EOF >expect &&
60 5
61 4
62 3
63 EOF
64         test_cmp expect actual
65         )
66 '
67
68 test_expect_success 'fetch --depth from shallow clone' '
69         (
70         cd shallow &&
71         commit 6
72         ) &&
73         (
74         cd shallow2 &&
75         git fetch --depth=2 &&
76         git fsck &&
77         git log --format=%s origin/master >actual &&
78         cat <<EOF >expect &&
79 6
80 5
81 EOF
82         test_cmp expect actual
83         )
84 '
85
86 test_expect_success 'fetch --unshallow from shallow clone' '
87         (
88         cd shallow2 &&
89         git fetch --unshallow &&
90         git fsck &&
91         git log --format=%s origin/master >actual &&
92         cat <<EOF >expect &&
93 6
94 5
95 4
96 3
97 EOF
98         test_cmp expect actual
99         )
100 '
101
102 test_expect_success 'fetch something upstream has but hidden by clients shallow boundaries' '
103         # the blob "1" is available in .git but hidden by the
104         # shallow2/.git/shallow and it should be resent
105         ! git --git-dir=shallow2/.git cat-file blob $(echo 1|git hash-object --stdin) >/dev/null &&
106         echo 1 >1.t &&
107         git add 1.t &&
108         git commit -m add-1-back &&
109         (
110         cd shallow2 &&
111         git fetch ../.git +refs/heads/master:refs/remotes/top/master &&
112         git fsck &&
113         git log --format=%s top/master >actual &&
114         cat <<EOF >expect &&
115 add-1-back
116 4
117 3
118 EOF
119         test_cmp expect actual
120         ) &&
121         git --git-dir=shallow2/.git cat-file blob $(echo 1|git hash-object --stdin) >/dev/null
122
123 '
124
125 test_expect_success 'fetch that requires changes in .git/shallow is filtered' '
126         (
127         cd shallow &&
128         git checkout --orphan no-shallow &&
129         commit no-shallow
130         ) &&
131         git init notshallow &&
132         (
133         cd notshallow &&
134         git fetch ../shallow/.git refs/heads/*:refs/remotes/shallow/*&&
135         git for-each-ref --format="%(refname)" >actual.refs &&
136         cat <<EOF >expect.refs &&
137 refs/remotes/shallow/no-shallow
138 EOF
139         test_cmp expect.refs actual.refs &&
140         git log --format=%s shallow/no-shallow >actual &&
141         cat <<EOF >expect &&
142 no-shallow
143 EOF
144         test_cmp expect actual
145         )
146 '
147
148 test_expect_success 'fetch --update-shallow' '
149         (
150         cd shallow &&
151         git checkout master &&
152         commit 7 &&
153         git tag -m foo heavy-tag HEAD^ &&
154         git tag light-tag HEAD^:tracked
155         ) &&
156         (
157         cd notshallow &&
158         git fetch --update-shallow ../shallow/.git refs/heads/*:refs/remotes/shallow/* &&
159         git fsck &&
160         git for-each-ref --sort=refname --format="%(refname)" >actual.refs &&
161         cat <<EOF >expect.refs &&
162 refs/remotes/shallow/master
163 refs/remotes/shallow/no-shallow
164 refs/tags/heavy-tag
165 refs/tags/light-tag
166 EOF
167         test_cmp expect.refs actual.refs &&
168         git log --format=%s shallow/master >actual &&
169         cat <<EOF >expect &&
170 7
171 6
172 5
173 4
174 3
175 EOF
176         test_cmp expect actual
177         )
178 '
179
180 test_expect_success POSIXPERM,SANITY 'shallow fetch from a read-only repo' '
181         cp -R .git read-only.git &&
182         test_when_finished "find read-only.git -type d -print | xargs chmod +w" &&
183         find read-only.git -print | xargs chmod -w &&
184         git clone --no-local --depth=2 read-only.git from-read-only &&
185         git --git-dir=from-read-only/.git log --format=%s >actual &&
186         cat >expect <<EOF &&
187 add-1-back
188 4
189 EOF
190         test_cmp expect actual
191 '
192
193 test_expect_success '.git/shallow is edited by repack' '
194         git init shallow-server &&
195         test_commit -C shallow-server A &&
196         test_commit -C shallow-server B &&
197         git -C shallow-server checkout -b branch &&
198         test_commit -C shallow-server C &&
199         test_commit -C shallow-server E &&
200         test_commit -C shallow-server D &&
201         d="$(git -C shallow-server rev-parse --verify D^0)" &&
202         git -C shallow-server checkout master &&
203
204         git clone --depth=1 --no-tags --no-single-branch \
205                 "file://$PWD/shallow-server" shallow-client &&
206
207         : now remove the branch and fetch with prune &&
208         git -C shallow-server branch -D branch &&
209         git -C shallow-client fetch --prune --depth=1 \
210                 origin "+refs/heads/*:refs/remotes/origin/*" &&
211         git -C shallow-client repack -adfl &&
212         test_must_fail git -C shallow-client rev-parse --verify $d^0 &&
213         ! grep $d shallow-client/.git/shallow &&
214
215         git -C shallow-server branch branch-orig $d &&
216         git -C shallow-client fetch --prune --depth=2 \
217                 origin "+refs/heads/*:refs/remotes/origin/*"
218 '
219
220 . "$TEST_DIRECTORY"/lib-httpd.sh
221 start_httpd
222
223 REPO="$HTTPD_DOCUMENT_ROOT_PATH/repo"
224
225 test_expect_success 'shallow fetches check connectivity before writing shallow file' '
226         rm -rf "$REPO" client &&
227
228         git init "$REPO" &&
229         test_commit -C "$REPO" one &&
230         test_commit -C "$REPO" two &&
231         test_commit -C "$REPO" three &&
232
233         git init client &&
234
235         # Use protocol v2 to ensure that shallow information is sent exactly
236         # once by the server, since we are planning to manipulate it.
237         git -C "$REPO" config protocol.version 2 &&
238         git -C client config protocol.version 2 &&
239
240         git -C client fetch --depth=2 "$HTTPD_URL/one_time_perl/repo" master:a_branch &&
241
242         # Craft a situation in which the server sends back an unshallow request
243         # with an empty packfile. This is done by refetching with a shorter
244         # depth (to ensure that the packfile is empty), and overwriting the
245         # shallow line in the response with the unshallow line we want.
246         printf "$(test_oid perl)" \
247                "$(git -C "$REPO" rev-parse HEAD)" \
248                "$(git -C "$REPO" rev-parse HEAD^)" \
249                >"$HTTPD_ROOT_PATH/one-time-perl" &&
250         test_must_fail env GIT_TEST_SIDEBAND_ALL=0 git -C client \
251                 fetch --depth=1 "$HTTPD_URL/one_time_perl/repo" \
252                 master:a_branch &&
253
254         # Ensure that the one-time-perl script was used.
255         ! test -e "$HTTPD_ROOT_PATH/one-time-perl" &&
256
257         # Ensure that the resulting repo is consistent, despite our failure to
258         # fetch.
259         git -C client fsck
260 '
261
262 # DO NOT add non-httpd-specific tests here, because the last part of this
263 # test script is only executed when httpd is available and enabled.
264
265 test_done