chainlint.pl: add POSIX shell parser
[git.git] / t / t5545-push-options.sh
blob214228349ad3995e49815850412e9c24bfc58065
1 #!/bin/sh
3 test_description='pushing to a repository using push options'
5 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
6 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
8 GIT_TEST_FATAL_REGISTER_SUBMODULE_ODB=1
9 export GIT_TEST_FATAL_REGISTER_SUBMODULE_ODB
11 . ./test-lib.sh
13 mk_repo_pair () {
14 rm -rf workbench upstream &&
15 test_create_repo upstream &&
16 test_create_repo workbench &&
18 cd upstream &&
19 git config receive.denyCurrentBranch warn &&
20 mkdir -p .git/hooks &&
21 cat >.git/hooks/pre-receive <<-'EOF' &&
22 #!/bin/sh
23 if test -n "$GIT_PUSH_OPTION_COUNT"; then
24 i=0
25 >hooks/pre-receive.push_options
26 while test "$i" -lt "$GIT_PUSH_OPTION_COUNT"; do
27 eval "value=\$GIT_PUSH_OPTION_$i"
28 echo $value >>hooks/pre-receive.push_options
29 i=$((i + 1))
30 done
32 EOF
33 chmod u+x .git/hooks/pre-receive
35 cat >.git/hooks/post-receive <<-'EOF' &&
36 #!/bin/sh
37 if test -n "$GIT_PUSH_OPTION_COUNT"; then
38 i=0
39 >hooks/post-receive.push_options
40 while test "$i" -lt "$GIT_PUSH_OPTION_COUNT"; do
41 eval "value=\$GIT_PUSH_OPTION_$i"
42 echo $value >>hooks/post-receive.push_options
43 i=$((i + 1))
44 done
46 EOF
47 chmod u+x .git/hooks/post-receive
48 ) &&
50 cd workbench &&
51 git remote add up ../upstream
55 # Compare the ref ($1) in upstream with a ref value from workbench ($2)
56 # i.e. test_refs second HEAD@{2}
57 test_refs () {
58 test $# = 2 &&
59 git -C upstream rev-parse --verify "$1" >expect &&
60 git -C workbench rev-parse --verify "$2" >actual &&
61 test_cmp expect actual
64 test_expect_success 'one push option works for a single branch' '
65 mk_repo_pair &&
66 git -C upstream config receive.advertisePushOptions true &&
68 cd workbench &&
69 test_commit one &&
70 git push --mirror up &&
71 test_commit two &&
72 git push --push-option=asdf up main
73 ) &&
74 test_refs main main &&
75 echo "asdf" >expect &&
76 test_cmp expect upstream/.git/hooks/pre-receive.push_options &&
77 test_cmp expect upstream/.git/hooks/post-receive.push_options
80 test_expect_success 'push option denied by remote' '
81 mk_repo_pair &&
82 git -C upstream config receive.advertisePushOptions false &&
84 cd workbench &&
85 test_commit one &&
86 git push --mirror up &&
87 test_commit two &&
88 test_must_fail git push --push-option=asdf up main
89 ) &&
90 test_refs main HEAD@{1}
93 test_expect_success 'two push options work' '
94 mk_repo_pair &&
95 git -C upstream config receive.advertisePushOptions true &&
97 cd workbench &&
98 test_commit one &&
99 git push --mirror up &&
100 test_commit two &&
101 git push --push-option=asdf --push-option="more structured text" up main
102 ) &&
103 test_refs main main &&
104 printf "asdf\nmore structured text\n" >expect &&
105 test_cmp expect upstream/.git/hooks/pre-receive.push_options &&
106 test_cmp expect upstream/.git/hooks/post-receive.push_options
109 test_expect_success 'push options and submodules' '
110 test_when_finished "rm -rf parent" &&
111 test_when_finished "rm -rf parent_upstream" &&
112 mk_repo_pair &&
113 git -C upstream config receive.advertisePushOptions true &&
114 cp -r upstream parent_upstream &&
115 test_commit -C upstream one &&
117 test_create_repo parent &&
118 git -C parent remote add up ../parent_upstream &&
119 test_commit -C parent one &&
120 git -C parent push --mirror up &&
122 git -C parent submodule add ../upstream workbench &&
123 git -C parent/workbench remote add up ../../upstream &&
124 git -C parent commit -m "add submodule" &&
126 test_commit -C parent/workbench two &&
127 git -C parent add workbench &&
128 git -C parent commit -m "update workbench" &&
130 git -C parent push \
131 --push-option=asdf --push-option="more structured text" \
132 --recurse-submodules=on-demand up main &&
134 git -C upstream rev-parse --verify main >expect &&
135 git -C parent/workbench rev-parse --verify main >actual &&
136 test_cmp expect actual &&
138 git -C parent_upstream rev-parse --verify main >expect &&
139 git -C parent rev-parse --verify main >actual &&
140 test_cmp expect actual &&
142 printf "asdf\nmore structured text\n" >expect &&
143 test_cmp expect upstream/.git/hooks/pre-receive.push_options &&
144 test_cmp expect upstream/.git/hooks/post-receive.push_options &&
145 test_cmp expect parent_upstream/.git/hooks/pre-receive.push_options &&
146 test_cmp expect parent_upstream/.git/hooks/post-receive.push_options
149 test_expect_success 'default push option' '
150 mk_repo_pair &&
151 git -C upstream config receive.advertisePushOptions true &&
153 cd workbench &&
154 test_commit one &&
155 git push --mirror up &&
156 test_commit two &&
157 git -c push.pushOption=default push up main
158 ) &&
159 test_refs main main &&
160 echo "default" >expect &&
161 test_cmp expect upstream/.git/hooks/pre-receive.push_options &&
162 test_cmp expect upstream/.git/hooks/post-receive.push_options
165 test_expect_success 'two default push options' '
166 mk_repo_pair &&
167 git -C upstream config receive.advertisePushOptions true &&
169 cd workbench &&
170 test_commit one &&
171 git push --mirror up &&
172 test_commit two &&
173 git -c push.pushOption=default1 -c push.pushOption=default2 push up main
174 ) &&
175 test_refs main main &&
176 printf "default1\ndefault2\n" >expect &&
177 test_cmp expect upstream/.git/hooks/pre-receive.push_options &&
178 test_cmp expect upstream/.git/hooks/post-receive.push_options
181 test_expect_success 'push option from command line overrides from-config push option' '
182 mk_repo_pair &&
183 git -C upstream config receive.advertisePushOptions true &&
185 cd workbench &&
186 test_commit one &&
187 git push --mirror up &&
188 test_commit two &&
189 git -c push.pushOption=default push --push-option=manual up main
190 ) &&
191 test_refs main main &&
192 echo "manual" >expect &&
193 test_cmp expect upstream/.git/hooks/pre-receive.push_options &&
194 test_cmp expect upstream/.git/hooks/post-receive.push_options
197 test_expect_success 'empty value of push.pushOption in config clears the list' '
198 mk_repo_pair &&
199 git -C upstream config receive.advertisePushOptions true &&
201 cd workbench &&
202 test_commit one &&
203 git push --mirror up &&
204 test_commit two &&
205 git -c push.pushOption=default1 -c push.pushOption= -c push.pushOption=default2 push up main
206 ) &&
207 test_refs main main &&
208 echo "default2" >expect &&
209 test_cmp expect upstream/.git/hooks/pre-receive.push_options &&
210 test_cmp expect upstream/.git/hooks/post-receive.push_options
213 test_expect_success 'invalid push option in config' '
214 mk_repo_pair &&
215 git -C upstream config receive.advertisePushOptions true &&
217 cd workbench &&
218 test_commit one &&
219 git push --mirror up &&
220 test_commit two &&
221 test_must_fail git -c push.pushOption push up main
222 ) &&
223 test_refs main HEAD@{1}
226 test_expect_success 'push options keep quoted characters intact (direct)' '
227 mk_repo_pair &&
228 git -C upstream config receive.advertisePushOptions true &&
229 test_commit -C workbench one &&
230 git -C workbench push --push-option="\"embedded quotes\"" up main &&
231 echo "\"embedded quotes\"" >expect &&
232 test_cmp expect upstream/.git/hooks/pre-receive.push_options
235 . "$TEST_DIRECTORY"/lib-httpd.sh
236 start_httpd
238 # set up http repository for fetching/pushing, with push options config
239 # bool set to $1
240 mk_http_pair () {
241 test_when_finished "rm -rf test_http_clone" &&
242 test_when_finished 'rm -rf "$HTTPD_DOCUMENT_ROOT_PATH"/upstream.git' &&
243 mk_repo_pair &&
244 git -C upstream config receive.advertisePushOptions "$1" &&
245 git -C upstream config http.receivepack true &&
246 cp -R upstream/.git "$HTTPD_DOCUMENT_ROOT_PATH"/upstream.git &&
247 git clone "$HTTPD_URL"/smart/upstream test_http_clone
250 test_expect_success 'push option denied properly by http server' '
251 mk_http_pair false &&
252 test_commit -C test_http_clone one &&
253 test_must_fail git -C test_http_clone push --push-option=asdf origin main 2>actual &&
254 test_i18ngrep "the receiving end does not support push options" actual &&
255 git -C test_http_clone push origin main
258 test_expect_success 'push options work properly across http' '
259 mk_http_pair true &&
261 test_commit -C test_http_clone one &&
262 git -C test_http_clone push origin main &&
263 git -C "$HTTPD_DOCUMENT_ROOT_PATH"/upstream.git rev-parse --verify main >expect &&
264 git -C test_http_clone rev-parse --verify main >actual &&
265 test_cmp expect actual &&
267 test_commit -C test_http_clone two &&
268 git -C test_http_clone push --push-option=asdf --push-option="more structured text" origin main &&
269 printf "asdf\nmore structured text\n" >expect &&
270 test_cmp expect "$HTTPD_DOCUMENT_ROOT_PATH"/upstream.git/hooks/pre-receive.push_options &&
271 test_cmp expect "$HTTPD_DOCUMENT_ROOT_PATH"/upstream.git/hooks/post-receive.push_options &&
273 git -C "$HTTPD_DOCUMENT_ROOT_PATH"/upstream.git rev-parse --verify main >expect &&
274 git -C test_http_clone rev-parse --verify main >actual &&
275 test_cmp expect actual
278 test_expect_success 'push options keep quoted characters intact (http)' '
279 mk_http_pair true &&
281 test_commit -C test_http_clone one &&
282 git -C test_http_clone push --push-option="\"embedded quotes\"" origin main &&
283 echo "\"embedded quotes\"" >expect &&
284 test_cmp expect "$HTTPD_DOCUMENT_ROOT_PATH"/upstream.git/hooks/pre-receive.push_options
287 # DO NOT add non-httpd-specific tests here, because the last part of this
288 # test script is only executed when httpd is available and enabled.
290 test_done