config: unify code paths to get global config paths
[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         0000
23         EOF
24
25         GIT_TEST_SIDEBAND_ALL=0 test-tool serve-v2 \
26                 --advertise-capabilities >out &&
27         test-tool pkt-line unpack <out >actual &&
28         test_cmp expect actual
29 '
30
31 test_expect_success 'stateless-rpc flag does not list capabilities' '
32         # Empty request
33         test-tool pkt-line pack >in <<-EOF &&
34         0000
35         EOF
36         test-tool serve-v2 --stateless-rpc >out <in &&
37         test_must_be_empty out &&
38
39         # EOF
40         test-tool serve-v2 --stateless-rpc >out &&
41         test_must_be_empty out
42 '
43
44 test_expect_success 'request invalid capability' '
45         test-tool pkt-line pack >in <<-EOF &&
46         foobar
47         0000
48         EOF
49         test_must_fail test-tool serve-v2 --stateless-rpc 2>err <in &&
50         test_i18ngrep "unknown capability" err
51 '
52
53 test_expect_success 'request with no command' '
54         test-tool pkt-line pack >in <<-EOF &&
55         agent=git/test
56         object-format=$(test_oid algo)
57         0000
58         EOF
59         test_must_fail test-tool serve-v2 --stateless-rpc 2>err <in &&
60         test_i18ngrep "no command requested" err
61 '
62
63 test_expect_success 'request invalid command' '
64         test-tool pkt-line pack >in <<-EOF &&
65         command=foo
66         object-format=$(test_oid algo)
67         agent=git/test
68         0000
69         EOF
70         test_must_fail test-tool serve-v2 --stateless-rpc 2>err <in &&
71         test_i18ngrep "invalid command" err
72 '
73
74 test_expect_success 'wrong object-format' '
75         test-tool pkt-line pack >in <<-EOF &&
76         command=fetch
77         agent=git/test
78         object-format=$(test_oid wrong_algo)
79         0000
80         EOF
81         test_must_fail test-tool serve-v2 --stateless-rpc 2>err <in &&
82         test_i18ngrep "mismatched object format" err
83 '
84
85 # Test the basics of ls-refs
86 #
87 test_expect_success 'setup some refs and tags' '
88         test_commit one &&
89         git branch dev main &&
90         test_commit two &&
91         git symbolic-ref refs/heads/release refs/heads/main &&
92         git tag -a -m "annotated tag" annotated-tag
93 '
94
95 test_expect_success 'basics of ls-refs' '
96         test-tool pkt-line pack >in <<-EOF &&
97         command=ls-refs
98         object-format=$(test_oid algo)
99         0000
100         EOF
101
102         cat >expect <<-EOF &&
103         $(git rev-parse HEAD) HEAD
104         $(git rev-parse refs/heads/dev) refs/heads/dev
105         $(git rev-parse refs/heads/main) refs/heads/main
106         $(git rev-parse refs/heads/release) refs/heads/release
107         $(git rev-parse refs/tags/annotated-tag) refs/tags/annotated-tag
108         $(git rev-parse refs/tags/one) refs/tags/one
109         $(git rev-parse refs/tags/two) refs/tags/two
110         0000
111         EOF
112
113         test-tool serve-v2 --stateless-rpc <in >out &&
114         test-tool pkt-line unpack <out >actual &&
115         test_cmp expect actual
116 '
117
118 test_expect_success 'basic ref-prefixes' '
119         test-tool pkt-line pack >in <<-EOF &&
120         command=ls-refs
121         object-format=$(test_oid algo)
122         0001
123         ref-prefix refs/heads/main
124         ref-prefix refs/tags/one
125         0000
126         EOF
127
128         cat >expect <<-EOF &&
129         $(git rev-parse refs/heads/main) refs/heads/main
130         $(git rev-parse refs/tags/one) refs/tags/one
131         0000
132         EOF
133
134         test-tool serve-v2 --stateless-rpc <in >out &&
135         test-tool pkt-line unpack <out >actual &&
136         test_cmp expect actual
137 '
138
139 test_expect_success 'refs/heads prefix' '
140         test-tool pkt-line pack >in <<-EOF &&
141         command=ls-refs
142         object-format=$(test_oid algo)
143         0001
144         ref-prefix refs/heads/
145         0000
146         EOF
147
148         cat >expect <<-EOF &&
149         $(git rev-parse refs/heads/dev) refs/heads/dev
150         $(git rev-parse refs/heads/main) refs/heads/main
151         $(git rev-parse refs/heads/release) refs/heads/release
152         0000
153         EOF
154
155         test-tool serve-v2 --stateless-rpc <in >out &&
156         test-tool pkt-line unpack <out >actual &&
157         test_cmp expect actual
158 '
159
160 test_expect_success 'peel parameter' '
161         test-tool pkt-line pack >in <<-EOF &&
162         command=ls-refs
163         object-format=$(test_oid algo)
164         0001
165         peel
166         ref-prefix refs/tags/
167         0000
168         EOF
169
170         cat >expect <<-EOF &&
171         $(git rev-parse refs/tags/annotated-tag) refs/tags/annotated-tag peeled:$(git rev-parse refs/tags/annotated-tag^{})
172         $(git rev-parse refs/tags/one) refs/tags/one
173         $(git rev-parse refs/tags/two) refs/tags/two
174         0000
175         EOF
176
177         test-tool serve-v2 --stateless-rpc <in >out &&
178         test-tool pkt-line unpack <out >actual &&
179         test_cmp expect actual
180 '
181
182 test_expect_success 'symrefs parameter' '
183         test-tool pkt-line pack >in <<-EOF &&
184         command=ls-refs
185         object-format=$(test_oid algo)
186         0001
187         symrefs
188         ref-prefix refs/heads/
189         0000
190         EOF
191
192         cat >expect <<-EOF &&
193         $(git rev-parse refs/heads/dev) refs/heads/dev
194         $(git rev-parse refs/heads/main) refs/heads/main
195         $(git rev-parse refs/heads/release) refs/heads/release symref-target:refs/heads/main
196         0000
197         EOF
198
199         test-tool serve-v2 --stateless-rpc <in >out &&
200         test-tool pkt-line unpack <out >actual &&
201         test_cmp expect actual
202 '
203
204 test_expect_success 'sending server-options' '
205         test-tool pkt-line pack >in <<-EOF &&
206         command=ls-refs
207         object-format=$(test_oid algo)
208         server-option=hello
209         server-option=world
210         0001
211         ref-prefix HEAD
212         0000
213         EOF
214
215         cat >expect <<-EOF &&
216         $(git rev-parse HEAD) HEAD
217         0000
218         EOF
219
220         test-tool serve-v2 --stateless-rpc <in >out &&
221         test-tool pkt-line unpack <out >actual &&
222         test_cmp expect actual
223 '
224
225 test_expect_success 'unexpected lines are not allowed in fetch request' '
226         git init server &&
227
228         test-tool pkt-line pack >in <<-EOF &&
229         command=fetch
230         object-format=$(test_oid algo)
231         0001
232         this-is-not-a-command
233         0000
234         EOF
235
236         (
237                 cd server &&
238                 test_must_fail test-tool serve-v2 --stateless-rpc
239         ) <in >/dev/null 2>err &&
240         grep "unexpected line: .this-is-not-a-command." err
241 '
242
243 test_done