Merge branch 'ow/no-dryrun-in-add-i'
[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
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 'wrong object-format' '
76         test-tool pkt-line pack >in <<-EOF &&
77         command=fetch
78         agent=git/test
79         object-format=$(test_oid wrong_algo)
80         0000
81         EOF
82         test_must_fail test-tool serve-v2 --stateless-rpc 2>err <in &&
83         test_i18ngrep "mismatched object format" err
84 '
85
86 # Test the basics of ls-refs
87 #
88 test_expect_success 'setup some refs and tags' '
89         test_commit one &&
90         git branch dev main &&
91         test_commit two &&
92         git symbolic-ref refs/heads/release refs/heads/main &&
93         git tag -a -m "annotated tag" annotated-tag
94 '
95
96 test_expect_success 'basics of ls-refs' '
97         test-tool pkt-line pack >in <<-EOF &&
98         command=ls-refs
99         object-format=$(test_oid algo)
100         0000
101         EOF
102
103         cat >expect <<-EOF &&
104         $(git rev-parse HEAD) HEAD
105         $(git rev-parse refs/heads/dev) refs/heads/dev
106         $(git rev-parse refs/heads/main) refs/heads/main
107         $(git rev-parse refs/heads/release) refs/heads/release
108         $(git rev-parse refs/tags/annotated-tag) refs/tags/annotated-tag
109         $(git rev-parse refs/tags/one) refs/tags/one
110         $(git rev-parse refs/tags/two) refs/tags/two
111         0000
112         EOF
113
114         test-tool serve-v2 --stateless-rpc <in >out &&
115         test-tool pkt-line unpack <out >actual &&
116         test_cmp expect actual
117 '
118
119 test_expect_success 'basic ref-prefixes' '
120         test-tool pkt-line pack >in <<-EOF &&
121         command=ls-refs
122         object-format=$(test_oid algo)
123         0001
124         ref-prefix refs/heads/main
125         ref-prefix refs/tags/one
126         0000
127         EOF
128
129         cat >expect <<-EOF &&
130         $(git rev-parse refs/heads/main) refs/heads/main
131         $(git rev-parse refs/tags/one) refs/tags/one
132         0000
133         EOF
134
135         test-tool serve-v2 --stateless-rpc <in >out &&
136         test-tool pkt-line unpack <out >actual &&
137         test_cmp expect actual
138 '
139
140 test_expect_success 'refs/heads prefix' '
141         test-tool pkt-line pack >in <<-EOF &&
142         command=ls-refs
143         object-format=$(test_oid algo)
144         0001
145         ref-prefix refs/heads/
146         0000
147         EOF
148
149         cat >expect <<-EOF &&
150         $(git rev-parse refs/heads/dev) refs/heads/dev
151         $(git rev-parse refs/heads/main) refs/heads/main
152         $(git rev-parse refs/heads/release) refs/heads/release
153         0000
154         EOF
155
156         test-tool serve-v2 --stateless-rpc <in >out &&
157         test-tool pkt-line unpack <out >actual &&
158         test_cmp expect actual
159 '
160
161 test_expect_success 'peel parameter' '
162         test-tool pkt-line pack >in <<-EOF &&
163         command=ls-refs
164         object-format=$(test_oid algo)
165         0001
166         peel
167         ref-prefix refs/tags/
168         0000
169         EOF
170
171         cat >expect <<-EOF &&
172         $(git rev-parse refs/tags/annotated-tag) refs/tags/annotated-tag peeled:$(git rev-parse refs/tags/annotated-tag^{})
173         $(git rev-parse refs/tags/one) refs/tags/one
174         $(git rev-parse refs/tags/two) refs/tags/two
175         0000
176         EOF
177
178         test-tool serve-v2 --stateless-rpc <in >out &&
179         test-tool pkt-line unpack <out >actual &&
180         test_cmp expect actual
181 '
182
183 test_expect_success 'symrefs parameter' '
184         test-tool pkt-line pack >in <<-EOF &&
185         command=ls-refs
186         object-format=$(test_oid algo)
187         0001
188         symrefs
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 symref-target:refs/heads/main
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 'sending server-options' '
206         test-tool pkt-line pack >in <<-EOF &&
207         command=ls-refs
208         object-format=$(test_oid algo)
209         server-option=hello
210         server-option=world
211         0001
212         ref-prefix HEAD
213         0000
214         EOF
215
216         cat >expect <<-EOF &&
217         $(git rev-parse HEAD) HEAD
218         0000
219         EOF
220
221         test-tool serve-v2 --stateless-rpc <in >out &&
222         test-tool pkt-line unpack <out >actual &&
223         test_cmp expect actual
224 '
225
226 test_expect_success 'unexpected lines are not allowed in fetch request' '
227         git init server &&
228
229         test-tool pkt-line pack >in <<-EOF &&
230         command=fetch
231         object-format=$(test_oid algo)
232         0001
233         this-is-not-a-command
234         0000
235         EOF
236
237         (
238                 cd server &&
239                 test_must_fail test-tool serve-v2 --stateless-rpc
240         ) <in >/dev/null 2>err &&
241         grep "unexpected line: .this-is-not-a-command." err
242 '
243
244 # Test the basics of object-info
245 #
246 test_expect_success 'basics of object-info' '
247         test-tool pkt-line pack >in <<-EOF &&
248         command=object-info
249         object-format=$(test_oid algo)
250         0001
251         size
252         oid $(git rev-parse two:two.t)
253         oid $(git rev-parse two:two.t)
254         0000
255         EOF
256
257         cat >expect <<-EOF &&
258         size
259         $(git rev-parse two:two.t) $(wc -c <two.t | xargs)
260         $(git rev-parse two:two.t) $(wc -c <two.t | xargs)
261         0000
262         EOF
263
264         test-tool serve-v2 --stateless-rpc <in >out &&
265         test-tool pkt-line unpack <out >actual &&
266         test_cmp expect actual
267 '
268
269 test_done