request-pull: rewrite to C
[git] / t / t5150-request-pull.sh
1 #!/bin/sh
2
3 test_description='Test workflows involving pull request.'
4
5 . ./test-lib.sh
6
7 test_expect_success 'setup' '
8         git init --bare upstream.git &&
9         git init --bare downstream.git &&
10         git clone upstream.git upstream-private &&
11         git clone downstream.git local &&
12
13         trash_url="file://$TRASH_DIRECTORY" &&
14         downstream_url="$trash_url/downstream.git/" &&
15         upstream_url="$trash_url/upstream.git/" &&
16
17         (
18                 cd upstream-private &&
19                 cat <<-\EOT >mnemonic.txt &&
20                 Thirtey days hath November,
21                 Aprile, June, and September:
22                 EOT
23                 git add mnemonic.txt &&
24                 test_tick &&
25                 git commit -m "\"Thirty days\", a reminder of month lengths" &&
26                 git tag -m "version 1" -a initial &&
27                 git push --tags origin master
28         ) &&
29         (
30                 cd local &&
31                 git remote add upstream "$trash_url/upstream.git" &&
32                 git fetch upstream &&
33                 git pull upstream master &&
34                 cat <<-\EOT >>mnemonic.txt &&
35                 Of twyecescore-eightt is but eine,
36                 And all the remnante be thrycescore-eine.
37                 O’course Leap yare comes an’pynes,
38                 Ev’rie foure yares, gote it ryghth.
39                 An’twyecescore-eight is but twyecescore-nyne.
40                 EOT
41                 git add mnemonic.txt &&
42                 test_tick &&
43                 git commit -m "More detail" &&
44                 git tag -m "version 2" -a full &&
45                 git checkout -b simplify HEAD^ &&
46                 mv mnemonic.txt mnemonic.standard &&
47                 cat <<-\EOT >mnemonic.clarified &&
48                 Thirty days has September,
49                 All the rest I can’t remember.
50                 EOT
51                 git add -N mnemonic.standard mnemonic.clarified &&
52                 git commit -a -m "Adapt to use modern, simpler English
53
54 But keep the old version, too, in case some people prefer it." &&
55                 git checkout master
56         )
57 '
58
59 test_expect_success 'setup: two scripts for reading pull requests' '
60         downstream_url_for_sed=$(
61                 printf "%s\n" "$downstream_url" |
62                 sed -e '\''s/\\/\\\\/g'\'' -e '\''s/[[/.*^$]/\\&/g'\''
63         ) &&
64
65         cat <<-\EOT >read-request.sed &&
66         #!/bin/sed -nf
67         # Note that a request could ask for "tag $tagname"
68         / in the git repository at:$/!d
69         n
70         /^$/ n
71         s/ tag \([^ ]*\)$/ tag--\1/
72         s/^[    ]*\(.*\) \([^ ]*\)/please pull\
73         \1\
74         \2/p
75         q
76         EOT
77
78         cat <<-EOT >fuzz.sed
79         #!/bin/sed -nf
80         s/$downstream_url_for_sed/URL/g
81         s/$_x40/OBJECT_NAME/g
82         s/A U Thor/AUTHOR/g
83         s/[-0-9]\{10\} [:0-9]\{8\} [-+][0-9]\{4\}/DATE/g
84         s/        [^ ].*/        SUBJECT/g
85         s/  [^ ].* (DATE)/  SUBJECT (DATE)/g
86         s/for-upstream/BRANCH/g
87         s/mnemonic.txt/FILENAME/g
88         s/^version [0-9]/VERSION/
89         /^ FILENAME | *[0-9]* [-+]*\$/ b diffstat
90         /^AUTHOR ([0-9]*):\$/ b shortlog
91         p
92         b
93         : diffstat
94         n
95         / [0-9]* files* changed/ {
96                 a\\
97         DIFFSTAT
98                 b
99         }
100         b diffstat
101         : shortlog
102         /^        [a-zA-Z]/ n
103         /^[a-zA-Z]* ([0-9]*):\$/ n
104         /^\$/ N
105         /^\n[a-zA-Z]* ([0-9]*):\$/!{
106                 a\\
107         SHORTLOG
108                 D
109         }
110         n
111         b shortlog
112         EOT
113 '
114
115 test_expect_success 'pull request when forgot to push' '
116         rm -fr downstream.git &&
117         git init --bare downstream.git &&
118         (
119                 cd local &&
120                 git checkout initial &&
121                 git merge --ff-only master &&
122                 test_must_fail git request-pull initial "$downstream_url" \
123                         2>../err
124         ) &&
125         cat err &&
126         grep "No branch of.*is at:\$" err &&
127         grep "Are you sure you pushed" err
128 '
129
130 test_expect_success 'pull request after push' '
131         rm -fr downstream.git &&
132         git init --bare downstream.git &&
133         (
134                 cd local &&
135                 git checkout initial &&
136                 git merge --ff-only master &&
137                 git push origin master:for-upstream &&
138                 git request-pull initial origin >../request
139         ) &&
140         sed -nf read-request.sed <request >digest &&
141         cat digest &&
142         {
143                 read task &&
144                 read repository &&
145                 read branch
146         } <digest &&
147         (
148                 cd upstream-private &&
149                 git checkout initial &&
150                 git pull --ff-only "$repository" "$branch"
151         ) &&
152         test "$branch" = for-upstream &&
153         test_cmp local/mnemonic.txt upstream-private/mnemonic.txt
154 '
155
156 test_expect_success 'request names an appropriate branch' '
157         rm -fr downstream.git &&
158         git init --bare downstream.git &&
159         (
160                 cd local &&
161                 git checkout initial &&
162                 git merge --ff-only master &&
163                 git push --tags origin master simplify &&
164                 git push origin master:for-upstream &&
165                 git request-pull initial "$downstream_url" >../request
166         ) &&
167         sed -nf read-request.sed <request >digest &&
168         cat digest &&
169         {
170                 read task &&
171                 read repository &&
172                 read branch
173         } <digest &&
174         test "$branch" = tags/full
175 '
176
177 test_expect_success 'pull request format' '
178         rm -fr downstream.git &&
179         git init --bare downstream.git &&
180         cat <<-\EOT >expect &&
181         The following changes since commit OBJECT_NAME:
182
183           SUBJECT (DATE)
184
185         are available in the git repository at:
186
187           URL BRANCH
188
189         for you to fetch changes up to OBJECT_NAME:
190
191           SUBJECT (DATE)
192
193         ----------------------------------------------------------------
194         VERSION
195
196         ----------------------------------------------------------------
197         SHORTLOG
198
199         DIFFSTAT
200         EOT
201         (
202                 cd local &&
203                 git checkout initial &&
204                 git merge --ff-only master &&
205                 git push origin master:for-upstream &&
206                 git request-pull initial "$downstream_url" >../request
207         ) &&
208         <request sed -nf fuzz.sed >request.fuzzy &&
209         test_i18ncmp expect request.fuzzy
210 '
211
212 test_expect_success 'request-pull ignores OPTIONS_KEEPDASHDASH poison' '
213         (
214                 cd local &&
215                 OPTIONS_KEEPDASHDASH=Yes &&
216                 export OPTIONS_KEEPDASHDASH &&
217                 git checkout initial &&
218                 git merge --ff-only master &&
219                 git push origin master:for-upstream &&
220                 git request-pull -- initial "$downstream_url" >../request
221         )
222 '
223
224 test_expect_success 'pull request when pushed tag' '
225         rm -fr downstream.git &&
226         git init --bare downstream.git &&
227         (
228                 cd local &&
229                 git checkout initial &&
230                 git merge --ff-only master &&
231                 git tag zeebra &&
232                 git push origin master:for-upstream full zeebra &&
233                 git request-pull initial origin 2>../err
234         ) &&
235         cat err &&
236         ! grep "You locally have .* but it does not (yet)" err
237 '
238
239 test_expect_success 'pull request with branch description' '
240         test_when_finished "(cd local && git checkout - && git branch -D for-upstream)" &&
241         rm -fr downstream.git &&
242         git init --bare downstream.git &&
243         (
244                 cd local &&
245                 git checkout -b for-upstream master &&
246                 git config branch.for-upstream.description "Branch for upstream$LF" &&
247                 git push origin for-upstream &&
248                 git request-pull initial origin >../request
249         ) &&
250         cat request &&
251         grep "(from the branch description for for-upstream local branch)" request &&
252         grep "Branch for upstream" request
253 '
254
255 test_expect_success 'pull request with branch description from rev' '
256         test_when_finished "(cd local && git checkout - && git branch -D for-upstream)" &&
257         rm -fr downstream.git &&
258         git init --bare downstream.git &&
259         (
260                 cd local &&
261                 git checkout -b for-upstream master &&
262                 git config branch.for-upstream.description "Branch for upstream$LF" &&
263                 git push origin for-upstream &&
264                 git request-pull initial origin for-upstream >../request
265         ) &&
266         cat request &&
267         grep "(from the branch description for for-upstream local branch)" request &&
268         grep "Branch for upstream" request
269 '
270
271 test_expect_success 'pull request with exact match' '
272         test_when_finished "(cd local && git checkout - && git branch -D for-upstream)" &&
273         rm -fr downstream.git &&
274         git init --bare downstream.git &&
275         (
276                 cd local &&
277                 git checkout -b for-upstream master &&
278                 git push origin master:for-upstream master:zeebra &&
279                 git request-pull initial origin for-upstream >../request
280         ) &&
281         cat request &&
282         sed -nf read-request.sed <request >digest &&
283         cat digest &&
284         {
285                 read task &&
286                 read repository &&
287                 read branch
288         } <digest &&
289         test "$branch" = for-upstream
290 '
291
292 test_done