1#!/bin/sh
2
3test_description='pushing to a repository using push options'
4
5. ./test-lib.sh
6. "$TEST_DIRECTORY"/lib-httpd.sh
7start_httpd
8
9mk_repo_pair () {
10 rm -rf workbench upstream &&
11 test_create_repo upstream &&
12 test_create_repo workbench &&
13 (
14 cd upstream &&
15 git config receive.denyCurrentBranch warn &&
16 mkdir -p .git/hooks &&
17 cat >.git/hooks/pre-receive <<-'EOF' &&
18 #!/bin/sh
19 if test -n "$GIT_PUSH_OPTION_COUNT"; then
20 i=0
21 >hooks/pre-receive.push_options
22 while test "$i" -lt "$GIT_PUSH_OPTION_COUNT"; do
23 eval "value=\$GIT_PUSH_OPTION_$i"
24 echo $value >>hooks/pre-receive.push_options
25 i=$((i + 1))
26 done
27 fi
28 EOF
29 chmod u+x .git/hooks/pre-receive
30
31 cat >.git/hooks/post-receive <<-'EOF' &&
32 #!/bin/sh
33 if test -n "$GIT_PUSH_OPTION_COUNT"; then
34 i=0
35 >hooks/post-receive.push_options
36 while test "$i" -lt "$GIT_PUSH_OPTION_COUNT"; do
37 eval "value=\$GIT_PUSH_OPTION_$i"
38 echo $value >>hooks/post-receive.push_options
39 i=$((i + 1))
40 done
41 fi
42 EOF
43 chmod u+x .git/hooks/post-receive
44 ) &&
45 (
46 cd workbench &&
47 git remote add up ../upstream
48 )
49}
50
51# Compare the ref ($1) in upstream with a ref value from workbench ($2)
52# i.e. test_refs second HEAD@{2}
53test_refs () {
54 test $# = 2 &&
55 git -C upstream rev-parse --verify "$1" >expect &&
56 git -C workbench rev-parse --verify "$2" >actual &&
57 test_cmp expect actual
58}
59
60test_expect_success 'one push option works for a single branch' '
61 mk_repo_pair &&
62 git -C upstream config receive.advertisePushOptions true &&
63 (
64 cd workbench &&
65 test_commit one &&
66 git push --mirror up &&
67 test_commit two &&
68 git push --push-option=asdf up master
69 ) &&
70 test_refs master master &&
71 echo "asdf" >expect &&
72 test_cmp expect upstream/.git/hooks/pre-receive.push_options &&
73 test_cmp expect upstream/.git/hooks/post-receive.push_options
74'
75
76test_expect_success 'push option denied by remote' '
77 mk_repo_pair &&
78 git -C upstream config receive.advertisePushOptions false &&
79 (
80 cd workbench &&
81 test_commit one &&
82 git push --mirror up &&
83 test_commit two &&
84 test_must_fail git push --push-option=asdf up master
85 ) &&
86 test_refs master HEAD@{1}
87'
88
89test_expect_success 'two push options work' '
90 mk_repo_pair &&
91 git -C upstream config receive.advertisePushOptions true &&
92 (
93 cd workbench &&
94 test_commit one &&
95 git push --mirror up &&
96 test_commit two &&
97 git push --push-option=asdf --push-option="more structured text" up master
98 ) &&
99 test_refs master master &&
100 printf "asdf\nmore structured text\n" >expect &&
101 test_cmp expect upstream/.git/hooks/pre-receive.push_options &&
102 test_cmp expect upstream/.git/hooks/post-receive.push_options
103'
104
105test_expect_success 'push option denied properly by http server' '
106 test_when_finished "rm -rf test_http_clone" &&
107 test_when_finished "rm -rf \"$HTTPD_DOCUMENT_ROOT_PATH\"/upstream.git" &&
108 mk_repo_pair &&
109 git -C upstream config receive.advertisePushOptions false &&
110 git -C upstream config http.receivepack true &&
111 cp -R upstream/.git "$HTTPD_DOCUMENT_ROOT_PATH"/upstream.git &&
112 git clone "$HTTPD_URL"/smart/upstream test_http_clone &&
113 test_commit -C test_http_clone one &&
114 test_must_fail git -C test_http_clone push --push-option=asdf origin master 2>actual &&
115 test_i18ngrep "the receiving end does not support push options" actual &&
116 git -C test_http_clone push origin master
117'
118
119test_expect_success 'push options work properly across http' '
120 test_when_finished "rm -rf test_http_clone" &&
121 test_when_finished "rm -rf \"$HTTPD_DOCUMENT_ROOT_PATH\"/upstream.git" &&
122 mk_repo_pair &&
123 git -C upstream config receive.advertisePushOptions true &&
124 git -C upstream config http.receivepack true &&
125 cp -R upstream/.git "$HTTPD_DOCUMENT_ROOT_PATH"/upstream.git &&
126 git clone "$HTTPD_URL"/smart/upstream test_http_clone &&
127
128 test_commit -C test_http_clone one &&
129 git -C test_http_clone push origin master &&
130 git -C "$HTTPD_DOCUMENT_ROOT_PATH"/upstream.git rev-parse --verify master >expect &&
131 git -C test_http_clone rev-parse --verify master >actual &&
132 test_cmp expect actual &&
133
134 test_commit -C test_http_clone two &&
135 git -C test_http_clone push --push-option=asdf --push-option="more structured text" origin master &&
136 printf "asdf\nmore structured text\n" >expect &&
137 test_cmp expect "$HTTPD_DOCUMENT_ROOT_PATH"/upstream.git/hooks/pre-receive.push_options &&
138 test_cmp expect "$HTTPD_DOCUMENT_ROOT_PATH"/upstream.git/hooks/post-receive.push_options &&
139
140 git -C "$HTTPD_DOCUMENT_ROOT_PATH"/upstream.git rev-parse --verify master >expect &&
141 git -C test_http_clone rev-parse --verify master >actual &&
142 test_cmp expect actual
143'
144
145stop_httpd
146
147test_done