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