remote-hg: add check_push() helper
[alt-git.git] / contrib / remote-helpers / test-hg.sh
blob53d2504e12b68eeec7756ead0699dd5a5fd4b8fd
1 #!/bin/sh
3 # Copyright (c) 2012 Felipe Contreras
5 # Base commands from hg-git tests:
6 # https://bitbucket.org/durin42/hg-git/src
9 test_description='Test remote-hg'
11 . ./test-lib.sh
13 if ! test_have_prereq PYTHON; then
14 skip_all='skipping remote-hg tests; python not available'
15 test_done
18 if ! python -c 'import mercurial'; then
19 skip_all='skipping remote-hg tests; mercurial not available'
20 test_done
23 check () {
24 echo $3 > expected &&
25 git --git-dir=$1/.git log --format='%s' -1 $2 > actual
26 test_cmp expected actual
29 check_branch () {
30 if [ -n "$3" ]; then
31 echo $3 > expected &&
32 hg -R $1 log -r $2 --template '{desc}\n' > actual &&
33 test_cmp expected actual
34 else
35 hg -R $1 branches > out &&
36 ! grep $2 out
40 check_bookmark () {
41 if [ -n "$3" ]; then
42 echo $3 > expected &&
43 hg -R $1 log -r "bookmark('$2')" --template '{desc}\n' > actual &&
44 test_cmp expected actual
45 else
46 hg -R $1 bookmarks > out &&
47 ! grep $2 out
51 check_push () {
52 local expected_ret=$1 ret=0 ref_ret=0 IFS=':'
54 shift
55 git push origin "$@" 2> error
56 ret=$?
57 cat error
59 while read branch kind
61 case "$kind" in
62 'new')
63 grep "^ \* \[new branch\] *${branch} -> ${branch}$" error || ref_ret=1
65 'non-fast-forward')
66 grep "^ ! \[rejected\] *${branch} -> ${branch} (non-fast-forward)$" error || ref_ret=1
68 '')
69 grep "^ [a-f0-9]*\.\.[a-f0-9]* *${branch} -> ${branch}$" error || ref_ret=1
71 esac
72 let 'ref_ret' && echo "match for '$branch' failed" && break
73 done
75 if let 'expected_ret != ret || ref_ret'
76 then
77 return 1
80 return 0
83 setup () {
85 echo "[ui]"
86 echo "username = H G Wells <wells@example.com>"
87 echo "[extensions]"
88 echo "mq ="
89 ) >> "$HOME"/.hgrc &&
91 GIT_AUTHOR_DATE="2007-01-01 00:00:00 +0230" &&
92 GIT_COMMITTER_DATE="$GIT_AUTHOR_DATE" &&
93 export GIT_COMMITTER_DATE GIT_AUTHOR_DATE
96 setup
98 test_expect_success 'cloning' '
99 test_when_finished "rm -rf gitrepo*" &&
102 hg init hgrepo &&
103 cd hgrepo &&
104 echo zero > content &&
105 hg add content &&
106 hg commit -m zero
107 ) &&
109 git clone "hg::hgrepo" gitrepo &&
110 check gitrepo HEAD zero
113 test_expect_success 'cloning with branches' '
114 test_when_finished "rm -rf gitrepo*" &&
117 cd hgrepo &&
118 hg branch next &&
119 echo next > content &&
120 hg commit -m next
121 ) &&
123 git clone "hg::hgrepo" gitrepo &&
124 check gitrepo origin/branches/next next
127 test_expect_success 'cloning with bookmarks' '
128 test_when_finished "rm -rf gitrepo*" &&
131 cd hgrepo &&
132 hg checkout default &&
133 hg bookmark feature-a &&
134 echo feature-a > content &&
135 hg commit -m feature-a
136 ) &&
138 git clone "hg::hgrepo" gitrepo &&
139 check gitrepo origin/feature-a feature-a
142 test_expect_success 'update bookmark' '
143 test_when_finished "rm -rf gitrepo*" &&
146 cd hgrepo &&
147 hg bookmark devel
148 ) &&
151 git clone "hg::hgrepo" gitrepo &&
152 cd gitrepo &&
153 git checkout --quiet devel &&
154 echo devel > content &&
155 git commit -a -m devel &&
156 git push --quiet
157 ) &&
159 check_bookmark hgrepo devel devel
162 test_expect_success 'new bookmark' '
163 test_when_finished "rm -rf gitrepo*" &&
166 git clone "hg::hgrepo" gitrepo &&
167 cd gitrepo &&
168 git checkout --quiet -b feature-b &&
169 echo feature-b > content &&
170 git commit -a -m feature-b &&
171 git push --quiet origin feature-b
172 ) &&
174 check_bookmark hgrepo feature-b feature-b
177 # cleanup previous stuff
178 rm -rf hgrepo
180 author_test () {
181 echo $1 >> content &&
182 hg commit -u "$2" -m "add $1" &&
183 echo "$3" >> ../expected
186 test_expect_success 'authors' '
187 test_when_finished "rm -rf hgrepo gitrepo" &&
190 hg init hgrepo &&
191 cd hgrepo &&
193 touch content &&
194 hg add content &&
196 > ../expected &&
197 author_test alpha "" "H G Wells <wells@example.com>" &&
198 author_test beta "test" "test <unknown>" &&
199 author_test beta "test <test@example.com> (comment)" "test <test@example.com>" &&
200 author_test gamma "<test@example.com>" "Unknown <test@example.com>" &&
201 author_test delta "name<test@example.com>" "name <test@example.com>" &&
202 author_test epsilon "name <test@example.com" "name <test@example.com>" &&
203 author_test zeta " test " "test <unknown>" &&
204 author_test eta "test < test@example.com >" "test <test@example.com>" &&
205 author_test theta "test >test@example.com>" "test <test@example.com>" &&
206 author_test iota "test < test <at> example <dot> com>" "test <unknown>" &&
207 author_test kappa "test@example.com" "Unknown <test@example.com>"
208 ) &&
210 git clone "hg::hgrepo" gitrepo &&
211 git --git-dir=gitrepo/.git log --reverse --format="%an <%ae>" > actual &&
213 test_cmp expected actual
216 test_expect_success 'strip' '
217 test_when_finished "rm -rf hgrepo gitrepo" &&
220 hg init hgrepo &&
221 cd hgrepo &&
223 echo one >> content &&
224 hg add content &&
225 hg commit -m one &&
227 echo two >> content &&
228 hg commit -m two
229 ) &&
231 git clone "hg::hgrepo" gitrepo &&
234 cd hgrepo &&
235 hg strip 1 &&
237 echo three >> content &&
238 hg commit -m three &&
240 echo four >> content &&
241 hg commit -m four
242 ) &&
245 cd gitrepo &&
246 git fetch &&
247 git log --format="%s" origin/master > ../actual
248 ) &&
250 hg -R hgrepo log --template "{desc}\n" > expected &&
251 test_cmp actual expected
254 test_expect_success 'remote push with master bookmark' '
255 test_when_finished "rm -rf hgrepo gitrepo*" &&
258 hg init hgrepo &&
259 cd hgrepo &&
260 echo zero > content &&
261 hg add content &&
262 hg commit -m zero &&
263 hg bookmark master &&
264 echo one > content &&
265 hg commit -m one
266 ) &&
269 git clone "hg::hgrepo" gitrepo &&
270 cd gitrepo &&
271 echo two > content &&
272 git commit -a -m two &&
273 git push
274 ) &&
276 check_branch hgrepo default two
279 cat > expected <<EOF
280 changeset: 0:6e2126489d3d
281 tag: tip
282 user: A U Thor <author@example.com>
283 date: Mon Jan 01 00:00:00 2007 +0230
284 summary: one
288 test_expect_success 'remote push from master branch' '
289 test_when_finished "rm -rf hgrepo gitrepo*" &&
291 hg init hgrepo &&
294 git init gitrepo &&
295 cd gitrepo &&
296 git remote add origin "hg::../hgrepo" &&
297 echo one > content &&
298 git add content &&
299 git commit -a -m one &&
300 git push origin master
301 ) &&
303 hg -R hgrepo log > actual &&
304 cat actual &&
305 test_cmp expected actual &&
307 check_branch hgrepo default one
310 GIT_REMOTE_HG_TEST_REMOTE=1
311 export GIT_REMOTE_HG_TEST_REMOTE
313 test_expect_success 'remote cloning' '
314 test_when_finished "rm -rf gitrepo*" &&
317 hg init hgrepo &&
318 cd hgrepo &&
319 echo zero > content &&
320 hg add content &&
321 hg commit -m zero
322 ) &&
324 git clone "hg::hgrepo" gitrepo &&
325 check gitrepo HEAD zero
328 test_expect_success 'remote update bookmark' '
329 test_when_finished "rm -rf gitrepo*" &&
332 cd hgrepo &&
333 hg bookmark devel
334 ) &&
337 git clone "hg::hgrepo" gitrepo &&
338 cd gitrepo &&
339 git checkout --quiet devel &&
340 echo devel > content &&
341 git commit -a -m devel &&
342 git push --quiet
343 ) &&
345 check_bookmark hgrepo devel devel
348 test_expect_success 'remote new bookmark' '
349 test_when_finished "rm -rf gitrepo*" &&
352 git clone "hg::hgrepo" gitrepo &&
353 cd gitrepo &&
354 git checkout --quiet -b feature-b &&
355 echo feature-b > content &&
356 git commit -a -m feature-b &&
357 git push --quiet origin feature-b
358 ) &&
360 check_bookmark hgrepo feature-b feature-b
363 test_expect_success 'remote push diverged' '
364 test_when_finished "rm -rf gitrepo*" &&
366 git clone "hg::hgrepo" gitrepo &&
369 cd hgrepo &&
370 hg checkout default &&
371 echo bump > content &&
372 hg commit -m bump
373 ) &&
376 cd gitrepo &&
377 echo diverge > content &&
378 git commit -a -m diverged &&
379 check_push 1 <<-EOF
380 master:non-fast-forward
382 ) &&
384 check_branch hgrepo default bump
387 test_expect_success 'remote update bookmark diverge' '
388 test_when_finished "rm -rf gitrepo*" &&
391 cd hgrepo &&
392 hg checkout tip^ &&
393 hg bookmark diverge
394 ) &&
396 git clone "hg::hgrepo" gitrepo &&
399 cd hgrepo &&
400 echo "bump bookmark" > content &&
401 hg commit -m "bump bookmark"
402 ) &&
405 cd gitrepo &&
406 git checkout --quiet diverge &&
407 echo diverge > content &&
408 git commit -a -m diverge &&
409 check_push 1 <<-EOF
410 diverge:non-fast-forward
412 ) &&
414 check_bookmark hgrepo diverge "bump bookmark"
417 test_expect_success 'remote new bookmark multiple branch head' '
418 test_when_finished "rm -rf gitrepo*" &&
421 git clone "hg::hgrepo" gitrepo &&
422 cd gitrepo &&
423 git checkout --quiet -b feature-c HEAD^ &&
424 echo feature-c > content &&
425 git commit -a -m feature-c &&
426 git push --quiet origin feature-c
427 ) &&
429 check_bookmark hgrepo feature-c feature-c
432 # cleanup previous stuff
433 rm -rf hgrepo
435 setup_big_push () {
437 hg init hgrepo &&
438 cd hgrepo &&
439 echo zero > content &&
440 hg add content &&
441 hg commit -m zero &&
442 hg bookmark bad_bmark1 &&
443 echo one > content &&
444 hg commit -m one &&
445 hg bookmark bad_bmark2 &&
446 hg bookmark good_bmark &&
447 hg bookmark -i good_bmark &&
448 hg -q branch good_branch &&
449 echo "good branch" > content &&
450 hg commit -m "good branch" &&
451 hg -q branch bad_branch &&
452 echo "bad branch" > content &&
453 hg commit -m "bad branch"
454 ) &&
456 git clone "hg::hgrepo" gitrepo &&
459 cd gitrepo &&
460 echo two > content &&
461 git commit -q -a -m two &&
463 git checkout -q good_bmark &&
464 echo three > content &&
465 git commit -q -a -m three &&
467 git checkout -q bad_bmark1 &&
468 git reset --hard HEAD^ &&
469 echo four > content &&
470 git commit -q -a -m four &&
472 git checkout -q bad_bmark2 &&
473 git reset --hard HEAD^ &&
474 echo five > content &&
475 git commit -q -a -m five &&
477 git checkout -q -b new_bmark master &&
478 echo six > content &&
479 git commit -q -a -m six &&
481 git checkout -q branches/good_branch &&
482 echo seven > content &&
483 git commit -q -a -m seven &&
484 echo eight > content &&
485 git commit -q -a -m eight &&
487 git checkout -q branches/bad_branch &&
488 git reset --hard HEAD^ &&
489 echo nine > content &&
490 git commit -q -a -m nine &&
492 git checkout -q -b branches/new_branch master &&
493 echo ten > content &&
494 git commit -q -a -m ten
498 test_expect_success 'remote big push' '
499 test_when_finished "rm -rf hgrepo gitrepo*" &&
501 setup_big_push
504 cd gitrepo &&
506 check_push 1 --all <<-EOF
507 master
508 good_bmark
509 branches/good_branch
510 new_bmark:new
511 branches/new_branch:new
512 bad_bmark1:non-fast-forward
513 bad_bmark2:non-fast-forward
514 branches/bad_branch:non-fast-forward
516 ) &&
518 check_branch hgrepo default one &&
519 check_branch hgrepo good_branch "good branch" &&
520 check_branch hgrepo bad_branch "bad branch" &&
521 check_branch hgrepo new_branch '' &&
522 check_bookmark hgrepo good_bmark one &&
523 check_bookmark hgrepo bad_bmark1 one &&
524 check_bookmark hgrepo bad_bmark2 one &&
525 check_bookmark hgrepo new_bmark ''
528 test_expect_success 'remote double failed push' '
529 test_when_finished "rm -rf hgrepo gitrepo*" &&
532 hg init hgrepo &&
533 cd hgrepo &&
534 echo zero > content &&
535 hg add content &&
536 hg commit -m zero &&
537 echo one > content &&
538 hg commit -m one
539 ) &&
542 git clone "hg::hgrepo" gitrepo &&
543 cd gitrepo &&
544 git reset --hard HEAD^ &&
545 echo two > content &&
546 git commit -a -m two &&
547 test_expect_code 1 git push &&
548 test_expect_code 1 git push
552 test_done