]> git.ipfire.org Git - thirdparty/git.git/blob - t/t5701-git-serve.sh
branch.c: simplify advice-and-die sequence
[thirdparty/git.git] / t / t5701-git-serve.sh
1 #!/bin/sh
2
3 test_description='test protocol v2 server commands'
4
5 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
6 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
7
8 . ./test-lib.sh
9
10 test_expect_success 'test capability advertisement' '
11 test_oid_cache <<-EOF &&
12 wrong_algo sha1:sha256
13 wrong_algo sha256:sha1
14 EOF
15 cat >expect <<-EOF &&
16 version 2
17 agent=git/$(git version | cut -d" " -f3)
18 ls-refs=unborn
19 fetch=shallow wait-for-done
20 server-option
21 object-format=$(test_oid algo)
22 object-info
23 0000
24 EOF
25
26 GIT_TEST_SIDEBAND_ALL=0 test-tool serve-v2 \
27 --advertise-capabilities >out &&
28 test-tool pkt-line unpack <out >actual &&
29 test_cmp expect actual
30 '
31
32 test_expect_success 'stateless-rpc flag does not list capabilities' '
33 # Empty request
34 test-tool pkt-line pack >in <<-EOF &&
35 0000
36 EOF
37 test-tool serve-v2 --stateless-rpc >out <in &&
38 test_must_be_empty out &&
39
40 # EOF
41 test-tool serve-v2 --stateless-rpc >out &&
42 test_must_be_empty out
43 '
44
45 test_expect_success 'request invalid capability' '
46 test-tool pkt-line pack >in <<-EOF &&
47 foobar
48 0000
49 EOF
50 test_must_fail test-tool serve-v2 --stateless-rpc 2>err <in &&
51 test_i18ngrep "unknown capability" err
52 '
53
54 test_expect_success 'request with no command' '
55 test-tool pkt-line pack >in <<-EOF &&
56 agent=git/test
57 object-format=$(test_oid algo)
58 0000
59 EOF
60 test_must_fail test-tool serve-v2 --stateless-rpc 2>err <in &&
61 test_i18ngrep "no command requested" err
62 '
63
64 test_expect_success 'request invalid command' '
65 test-tool pkt-line pack >in <<-EOF &&
66 command=foo
67 object-format=$(test_oid algo)
68 agent=git/test
69 0000
70 EOF
71 test_must_fail test-tool serve-v2 --stateless-rpc 2>err <in &&
72 test_i18ngrep "invalid command" err
73 '
74
75 test_expect_success 'request capability as command' '
76 test-tool pkt-line pack >in <<-EOF &&
77 command=agent
78 object-format=$(test_oid algo)
79 0000
80 EOF
81 test_must_fail test-tool serve-v2 --stateless-rpc 2>err <in &&
82 grep invalid.command.*agent err
83 '
84
85 test_expect_success 'request command as capability' '
86 test-tool pkt-line pack >in <<-EOF &&
87 command=ls-refs
88 object-format=$(test_oid algo)
89 fetch
90 0000
91 EOF
92 test_must_fail test-tool serve-v2 --stateless-rpc 2>err <in &&
93 grep unknown.capability err
94 '
95
96 test_expect_success 'requested command is command=value' '
97 test-tool pkt-line pack >in <<-EOF &&
98 command=ls-refs=whatever
99 object-format=$(test_oid algo)
100 0000
101 EOF
102 test_must_fail test-tool serve-v2 --stateless-rpc 2>err <in &&
103 grep invalid.command.*ls-refs=whatever err
104 '
105
106 test_expect_success 'wrong object-format' '
107 test-tool pkt-line pack >in <<-EOF &&
108 command=fetch
109 agent=git/test
110 object-format=$(test_oid wrong_algo)
111 0000
112 EOF
113 test_must_fail test-tool serve-v2 --stateless-rpc 2>err <in &&
114 test_i18ngrep "mismatched object format" err
115 '
116
117 # Test the basics of ls-refs
118 #
119 test_expect_success 'setup some refs and tags' '
120 test_commit one &&
121 git branch dev main &&
122 test_commit two &&
123 git symbolic-ref refs/heads/release refs/heads/main &&
124 git tag -a -m "annotated tag" annotated-tag
125 '
126
127 test_expect_success 'basics of ls-refs' '
128 test-tool pkt-line pack >in <<-EOF &&
129 command=ls-refs
130 object-format=$(test_oid algo)
131 0000
132 EOF
133
134 cat >expect <<-EOF &&
135 $(git rev-parse HEAD) HEAD
136 $(git rev-parse refs/heads/dev) refs/heads/dev
137 $(git rev-parse refs/heads/main) refs/heads/main
138 $(git rev-parse refs/heads/release) refs/heads/release
139 $(git rev-parse refs/tags/annotated-tag) refs/tags/annotated-tag
140 $(git rev-parse refs/tags/one) refs/tags/one
141 $(git rev-parse refs/tags/two) refs/tags/two
142 0000
143 EOF
144
145 test-tool serve-v2 --stateless-rpc <in >out &&
146 test-tool pkt-line unpack <out >actual &&
147 test_cmp expect actual
148 '
149
150 test_expect_success 'ls-refs complains about unknown options' '
151 test-tool pkt-line pack >in <<-EOF &&
152 command=ls-refs
153 object-format=$(test_oid algo)
154 0001
155 no-such-arg
156 0000
157 EOF
158
159 test_must_fail test-tool serve-v2 --stateless-rpc 2>err <in &&
160 grep unexpected.line.*no-such-arg err
161 '
162
163 test_expect_success 'basic ref-prefixes' '
164 test-tool pkt-line pack >in <<-EOF &&
165 command=ls-refs
166 object-format=$(test_oid algo)
167 0001
168 ref-prefix refs/heads/main
169 ref-prefix refs/tags/one
170 0000
171 EOF
172
173 cat >expect <<-EOF &&
174 $(git rev-parse refs/heads/main) refs/heads/main
175 $(git rev-parse refs/tags/one) refs/tags/one
176 0000
177 EOF
178
179 test-tool serve-v2 --stateless-rpc <in >out &&
180 test-tool pkt-line unpack <out >actual &&
181 test_cmp expect actual
182 '
183
184 test_expect_success 'refs/heads prefix' '
185 test-tool pkt-line pack >in <<-EOF &&
186 command=ls-refs
187 object-format=$(test_oid algo)
188 0001
189 ref-prefix refs/heads/
190 0000
191 EOF
192
193 cat >expect <<-EOF &&
194 $(git rev-parse refs/heads/dev) refs/heads/dev
195 $(git rev-parse refs/heads/main) refs/heads/main
196 $(git rev-parse refs/heads/release) refs/heads/release
197 0000
198 EOF
199
200 test-tool serve-v2 --stateless-rpc <in >out &&
201 test-tool pkt-line unpack <out >actual &&
202 test_cmp expect actual
203 '
204
205 test_expect_success 'ignore very large set of prefixes' '
206 # generate a large number of ref-prefixes that we expect
207 # to match nothing; the value here exceeds TOO_MANY_PREFIXES
208 # from ls-refs.c.
209 {
210 echo command=ls-refs &&
211 echo object-format=$(test_oid algo) &&
212 echo 0001 &&
213 perl -le "print \"ref-prefix refs/heads/\$_\" for (1..65536)" &&
214 echo 0000
215 } |
216 test-tool pkt-line pack >in &&
217
218 # and then confirm that we see unmatched prefixes anyway (i.e.,
219 # that the prefix was not applied).
220 cat >expect <<-EOF &&
221 $(git rev-parse HEAD) HEAD
222 $(git rev-parse refs/heads/dev) refs/heads/dev
223 $(git rev-parse refs/heads/main) refs/heads/main
224 $(git rev-parse refs/heads/release) refs/heads/release
225 $(git rev-parse refs/tags/annotated-tag) refs/tags/annotated-tag
226 $(git rev-parse refs/tags/one) refs/tags/one
227 $(git rev-parse refs/tags/two) refs/tags/two
228 0000
229 EOF
230
231 test-tool serve-v2 --stateless-rpc <in >out &&
232 test-tool pkt-line unpack <out >actual &&
233 test_cmp expect actual
234 '
235
236 test_expect_success 'peel parameter' '
237 test-tool pkt-line pack >in <<-EOF &&
238 command=ls-refs
239 object-format=$(test_oid algo)
240 0001
241 peel
242 ref-prefix refs/tags/
243 0000
244 EOF
245
246 cat >expect <<-EOF &&
247 $(git rev-parse refs/tags/annotated-tag) refs/tags/annotated-tag peeled:$(git rev-parse refs/tags/annotated-tag^{})
248 $(git rev-parse refs/tags/one) refs/tags/one
249 $(git rev-parse refs/tags/two) refs/tags/two
250 0000
251 EOF
252
253 test-tool serve-v2 --stateless-rpc <in >out &&
254 test-tool pkt-line unpack <out >actual &&
255 test_cmp expect actual
256 '
257
258 test_expect_success 'symrefs parameter' '
259 test-tool pkt-line pack >in <<-EOF &&
260 command=ls-refs
261 object-format=$(test_oid algo)
262 0001
263 symrefs
264 ref-prefix refs/heads/
265 0000
266 EOF
267
268 cat >expect <<-EOF &&
269 $(git rev-parse refs/heads/dev) refs/heads/dev
270 $(git rev-parse refs/heads/main) refs/heads/main
271 $(git rev-parse refs/heads/release) refs/heads/release symref-target:refs/heads/main
272 0000
273 EOF
274
275 test-tool serve-v2 --stateless-rpc <in >out &&
276 test-tool pkt-line unpack <out >actual &&
277 test_cmp expect actual
278 '
279
280 test_expect_success 'sending server-options' '
281 test-tool pkt-line pack >in <<-EOF &&
282 command=ls-refs
283 object-format=$(test_oid algo)
284 server-option=hello
285 server-option=world
286 0001
287 ref-prefix HEAD
288 0000
289 EOF
290
291 cat >expect <<-EOF &&
292 $(git rev-parse HEAD) HEAD
293 0000
294 EOF
295
296 test-tool serve-v2 --stateless-rpc <in >out &&
297 test-tool pkt-line unpack <out >actual &&
298 test_cmp expect actual
299 '
300
301 test_expect_success 'unexpected lines are not allowed in fetch request' '
302 git init server &&
303
304 test-tool pkt-line pack >in <<-EOF &&
305 command=fetch
306 object-format=$(test_oid algo)
307 0001
308 this-is-not-a-command
309 0000
310 EOF
311
312 (
313 cd server &&
314 test_must_fail test-tool serve-v2 --stateless-rpc
315 ) <in >/dev/null 2>err &&
316 grep "unexpected line: .this-is-not-a-command." err
317 '
318
319 # Test the basics of object-info
320 #
321 test_expect_success 'basics of object-info' '
322 test-tool pkt-line pack >in <<-EOF &&
323 command=object-info
324 object-format=$(test_oid algo)
325 0001
326 size
327 oid $(git rev-parse two:two.t)
328 oid $(git rev-parse two:two.t)
329 0000
330 EOF
331
332 cat >expect <<-EOF &&
333 size
334 $(git rev-parse two:two.t) $(wc -c <two.t | xargs)
335 $(git rev-parse two:two.t) $(wc -c <two.t | xargs)
336 0000
337 EOF
338
339 test-tool serve-v2 --stateless-rpc <in >out &&
340 test-tool pkt-line unpack <out >actual &&
341 test_cmp expect actual
342 '
343
344 test_done