1#!/bin/sh
2#
3# Copyright (c) 2012 Felipe Contreras
4#
5# Base commands from hg-git tests:
6# https://bitbucket.org/durin42/hg-git/src
7#
8
9test_description='Test remote-hg'
10
11. ./test-lib.sh
12
13if ! test_have_prereq PYTHON; then
14 skip_all='skipping remote-hg tests; python not available'
15 test_done
16fi
17
18if ! python -c 'import mercurial'; then
19 skip_all='skipping remote-hg tests; mercurial not available'
20 test_done
21fi
22
23check () {
24 echo $3 > expected &&
25 git --git-dir=$1/.git log --format='%s' -1 $2 > actual
26 test_cmp expected actual
27}
28
29check_branch () {
30 echo $3 > expected &&
31 hg -R $1 log -r $2 --template '{desc}\n' > actual &&
32 test_cmp expected actual
33}
34
35check_bookmark () {
36 echo $3 > expected &&
37 hg -R $1 log -r "bookmark('$2')" --template '{desc}\n' > actual &&
38 test_cmp expected actual
39}
40
41setup () {
42 (
43 echo "[ui]"
44 echo "username = H G Wells <wells@example.com>"
45 echo "[extensions]"
46 echo "mq ="
47 ) >> "$HOME"/.hgrc &&
48
49 GIT_AUTHOR_DATE="2007-01-01 00:00:00 +0230" &&
50 GIT_COMMITTER_DATE="$GIT_AUTHOR_DATE" &&
51 export GIT_COMMITTER_DATE GIT_AUTHOR_DATE
52}
53
54setup
55
56test_expect_success 'cloning' '
57 test_when_finished "rm -rf gitrepo*" &&
58
59 (
60 hg init hgrepo &&
61 cd hgrepo &&
62 echo zero > content &&
63 hg add content &&
64 hg commit -m zero
65 ) &&
66
67 git clone "hg::hgrepo" gitrepo &&
68 check gitrepo HEAD zero
69'
70
71test_expect_success 'cloning with branches' '
72 test_when_finished "rm -rf gitrepo*" &&
73
74 (
75 cd hgrepo &&
76 hg branch next &&
77 echo next > content &&
78 hg commit -m next
79 ) &&
80
81 git clone "hg::hgrepo" gitrepo &&
82 check gitrepo origin/branches/next next
83'
84
85test_expect_success 'cloning with bookmarks' '
86 test_when_finished "rm -rf gitrepo*" &&
87
88 (
89 cd hgrepo &&
90 hg checkout default &&
91 hg bookmark feature-a &&
92 echo feature-a > content &&
93 hg commit -m feature-a
94 ) &&
95
96 git clone "hg::hgrepo" gitrepo &&
97 check gitrepo origin/feature-a feature-a
98'
99
100test_expect_success 'update bookmark' '
101 test_when_finished "rm -rf gitrepo*" &&
102
103 (
104 cd hgrepo &&
105 hg bookmark devel
106 ) &&
107
108 (
109 git clone "hg::hgrepo" gitrepo &&
110 cd gitrepo &&
111 git checkout --quiet devel &&
112 echo devel > content &&
113 git commit -a -m devel &&
114 git push --quiet
115 ) &&
116
117 check_bookmark hgrepo devel devel
118'
119
120test_expect_success 'new bookmark' '
121 test_when_finished "rm -rf gitrepo*" &&
122
123 (
124 git clone "hg::hgrepo" gitrepo &&
125 cd gitrepo &&
126 git checkout --quiet -b feature-b &&
127 echo feature-b > content &&
128 git commit -a -m feature-b &&
129 git push --quiet origin feature-b
130 ) &&
131
132 check_bookmark hgrepo feature-b feature-b
133'
134
135# cleanup previous stuff
136rm -rf hgrepo
137
138author_test () {
139 echo $1 >> content &&
140 hg commit -u "$2" -m "add $1" &&
141 echo "$3" >> ../expected
142}
143
144test_expect_success 'authors' '
145 test_when_finished "rm -rf hgrepo gitrepo" &&
146
147 (
148 hg init hgrepo &&
149 cd hgrepo &&
150
151 touch content &&
152 hg add content &&
153
154 > ../expected &&
155 author_test alpha "" "H G Wells <wells@example.com>" &&
156 author_test beta "test" "test <unknown>" &&
157 author_test beta "test <test@example.com> (comment)" "test <test@example.com>" &&
158 author_test gamma "<test@example.com>" "Unknown <test@example.com>" &&
159 author_test delta "name<test@example.com>" "name <test@example.com>" &&
160 author_test epsilon "name <test@example.com" "name <test@example.com>" &&
161 author_test zeta " test " "test <unknown>" &&
162 author_test eta "test < test@example.com >" "test <test@example.com>" &&
163 author_test theta "test >test@example.com>" "test <test@example.com>" &&
164 author_test iota "test < test <at> example <dot> com>" "test <unknown>" &&
165 author_test kappa "test@example.com" "Unknown <test@example.com>"
166 ) &&
167
168 git clone "hg::hgrepo" gitrepo &&
169 git --git-dir=gitrepo/.git log --reverse --format="%an <%ae>" > actual &&
170
171 test_cmp expected actual
172'
173
174test_expect_success 'strip' '
175 test_when_finished "rm -rf hgrepo gitrepo" &&
176
177 (
178 hg init hgrepo &&
179 cd hgrepo &&
180
181 echo one >> content &&
182 hg add content &&
183 hg commit -m one &&
184
185 echo two >> content &&
186 hg commit -m two
187 ) &&
188
189 git clone "hg::hgrepo" gitrepo &&
190
191 (
192 cd hgrepo &&
193 hg strip 1 &&
194
195 echo three >> content &&
196 hg commit -m three &&
197
198 echo four >> content &&
199 hg commit -m four
200 ) &&
201
202 (
203 cd gitrepo &&
204 git fetch &&
205 git log --format="%s" origin/master > ../actual
206 ) &&
207
208 hg -R hgrepo log --template "{desc}\n" > expected &&
209 test_cmp actual expected
210'
211
212test_expect_success 'remote push with master bookmark' '
213 test_when_finished "rm -rf hgrepo gitrepo*" &&
214
215 (
216 hg init hgrepo &&
217 cd hgrepo &&
218 echo zero > content &&
219 hg add content &&
220 hg commit -m zero &&
221 hg bookmark master &&
222 echo one > content &&
223 hg commit -m one
224 ) &&
225
226 (
227 git clone "hg::hgrepo" gitrepo &&
228 cd gitrepo &&
229 echo two > content &&
230 git commit -a -m two &&
231 git push
232 ) &&
233
234 check_branch hgrepo default two
235'
236
237cat > expected <<EOF
238changeset: 0:6e2126489d3d
239tag: tip
240user: A U Thor <author@example.com>
241date: Mon Jan 01 00:00:00 2007 +0230
242summary: one
243
244EOF
245
246test_expect_success 'remote push from master branch' '
247 test_when_finished "rm -rf hgrepo gitrepo*" &&
248
249 hg init hgrepo &&
250
251 (
252 git init gitrepo &&
253 cd gitrepo &&
254 git remote add origin "hg::../hgrepo" &&
255 echo one > content &&
256 git add content &&
257 git commit -a -m one &&
258 git push origin master
259 ) &&
260
261 hg -R hgrepo log > actual &&
262 cat actual &&
263 test_cmp expected actual &&
264
265 check_branch hgrepo default one
266'
267
268GIT_REMOTE_HG_TEST_REMOTE=1
269export GIT_REMOTE_HG_TEST_REMOTE
270
271test_expect_success 'remote cloning' '
272 test_when_finished "rm -rf gitrepo*" &&
273
274 (
275 hg init hgrepo &&
276 cd hgrepo &&
277 echo zero > content &&
278 hg add content &&
279 hg commit -m zero
280 ) &&
281
282 git clone "hg::hgrepo" gitrepo &&
283 check gitrepo HEAD zero
284'
285
286test_expect_success 'remote update bookmark' '
287 test_when_finished "rm -rf gitrepo*" &&
288
289 (
290 cd hgrepo &&
291 hg bookmark devel
292 ) &&
293
294 (
295 git clone "hg::hgrepo" gitrepo &&
296 cd gitrepo &&
297 git checkout --quiet devel &&
298 echo devel > content &&
299 git commit -a -m devel &&
300 git push --quiet
301 ) &&
302
303 check_bookmark hgrepo devel devel
304'
305
306test_expect_success 'remote new bookmark' '
307 test_when_finished "rm -rf gitrepo*" &&
308
309 (
310 git clone "hg::hgrepo" gitrepo &&
311 cd gitrepo &&
312 git checkout --quiet -b feature-b &&
313 echo feature-b > content &&
314 git commit -a -m feature-b &&
315 git push --quiet origin feature-b
316 ) &&
317
318 check_bookmark hgrepo feature-b feature-b
319'
320
321test_expect_failure 'remote push diverged' '
322 test_when_finished "rm -rf gitrepo*" &&
323
324 git clone "hg::hgrepo" gitrepo &&
325
326 (
327 cd hgrepo &&
328 hg checkout default &&
329 echo bump > content &&
330 hg commit -m bump
331 ) &&
332
333 (
334 cd gitrepo &&
335 echo diverge > content &&
336 git commit -a -m diverged &&
337 test_expect_code 1 git push 2> error &&
338 grep "^ ! \[rejected\] *master -> master (non-fast-forward)$" error
339 ) &&
340
341 check_branch hgrepo default bump
342'
343
344test_expect_failure 'remote update bookmark diverge' '
345 test_when_finished "rm -rf gitrepo*" &&
346
347 (
348 cd hgrepo &&
349 hg checkout tip^ &&
350 hg bookmark diverge
351 ) &&
352
353 git clone "hg::hgrepo" gitrepo &&
354
355 (
356 cd hgrepo &&
357 echo "bump bookmark" > content &&
358 hg commit -m "bump bookmark"
359 ) &&
360
361 (
362 cd gitrepo &&
363 git checkout --quiet diverge &&
364 echo diverge > content &&
365 git commit -a -m diverge &&
366 test_expect_code 1 git push 2> error &&
367 grep "^ ! \[rejected\] *diverge -> diverge (non-fast-forward)$" error
368 ) &&
369
370 check_bookmark hgrepo diverge "bump bookmark"
371'
372
373test_done