remote-hg: add support for --dry-run
[git/git-svn.git] / contrib / remote-helpers / test-hg.sh
blob51f3c03875c5872dddacc86f39a7441f741209e4
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 'fetch-first')
69 grep "^ ! \[rejected\] *${branch} -> ${branch} (fetch first)$" error || ref_ret=1
71 '')
72 grep "^ [a-f0-9]*\.\.[a-f0-9]* *${branch} -> ${branch}$" error || ref_ret=1
74 esac
75 let 'ref_ret' && echo "match for '$branch' failed" && break
76 done
78 if let 'expected_ret != ret || ref_ret'
79 then
80 return 1
83 return 0
86 setup () {
88 echo "[ui]"
89 echo "username = H G Wells <wells@example.com>"
90 echo "[extensions]"
91 echo "mq ="
92 ) >> "$HOME"/.hgrc &&
94 GIT_AUTHOR_DATE="2007-01-01 00:00:00 +0230" &&
95 GIT_COMMITTER_DATE="$GIT_AUTHOR_DATE" &&
96 export GIT_COMMITTER_DATE GIT_AUTHOR_DATE
99 setup
101 test_expect_success 'cloning' '
102 test_when_finished "rm -rf gitrepo*" &&
105 hg init hgrepo &&
106 cd hgrepo &&
107 echo zero > content &&
108 hg add content &&
109 hg commit -m zero
110 ) &&
112 git clone "hg::hgrepo" gitrepo &&
113 check gitrepo HEAD zero
116 test_expect_success 'cloning with branches' '
117 test_when_finished "rm -rf gitrepo*" &&
120 cd hgrepo &&
121 hg branch next &&
122 echo next > content &&
123 hg commit -m next
124 ) &&
126 git clone "hg::hgrepo" gitrepo &&
127 check gitrepo origin/branches/next next
130 test_expect_success 'cloning with bookmarks' '
131 test_when_finished "rm -rf gitrepo*" &&
134 cd hgrepo &&
135 hg checkout default &&
136 hg bookmark feature-a &&
137 echo feature-a > content &&
138 hg commit -m feature-a
139 ) &&
141 git clone "hg::hgrepo" gitrepo &&
142 check gitrepo origin/feature-a feature-a
145 test_expect_success 'update bookmark' '
146 test_when_finished "rm -rf gitrepo*" &&
149 cd hgrepo &&
150 hg bookmark devel
151 ) &&
154 git clone "hg::hgrepo" gitrepo &&
155 cd gitrepo &&
156 git checkout --quiet devel &&
157 echo devel > content &&
158 git commit -a -m devel &&
159 git push --quiet
160 ) &&
162 check_bookmark hgrepo devel devel
165 test_expect_success 'new bookmark' '
166 test_when_finished "rm -rf gitrepo*" &&
169 git clone "hg::hgrepo" gitrepo &&
170 cd gitrepo &&
171 git checkout --quiet -b feature-b &&
172 echo feature-b > content &&
173 git commit -a -m feature-b &&
174 git push --quiet origin feature-b
175 ) &&
177 check_bookmark hgrepo feature-b feature-b
180 # cleanup previous stuff
181 rm -rf hgrepo
183 author_test () {
184 echo $1 >> content &&
185 hg commit -u "$2" -m "add $1" &&
186 echo "$3" >> ../expected
189 test_expect_success 'authors' '
190 test_when_finished "rm -rf hgrepo gitrepo" &&
193 hg init hgrepo &&
194 cd hgrepo &&
196 touch content &&
197 hg add content &&
199 > ../expected &&
200 author_test alpha "" "H G Wells <wells@example.com>" &&
201 author_test beta "test" "test <unknown>" &&
202 author_test beta "test <test@example.com> (comment)" "test <test@example.com>" &&
203 author_test gamma "<test@example.com>" "Unknown <test@example.com>" &&
204 author_test delta "name<test@example.com>" "name <test@example.com>" &&
205 author_test epsilon "name <test@example.com" "name <test@example.com>" &&
206 author_test zeta " test " "test <unknown>" &&
207 author_test eta "test < test@example.com >" "test <test@example.com>" &&
208 author_test theta "test >test@example.com>" "test <test@example.com>" &&
209 author_test iota "test < test <at> example <dot> com>" "test <unknown>" &&
210 author_test kappa "test@example.com" "Unknown <test@example.com>"
211 ) &&
213 git clone "hg::hgrepo" gitrepo &&
214 git --git-dir=gitrepo/.git log --reverse --format="%an <%ae>" > actual &&
216 test_cmp expected actual
219 test_expect_success 'strip' '
220 test_when_finished "rm -rf hgrepo gitrepo" &&
223 hg init hgrepo &&
224 cd hgrepo &&
226 echo one >> content &&
227 hg add content &&
228 hg commit -m one &&
230 echo two >> content &&
231 hg commit -m two
232 ) &&
234 git clone "hg::hgrepo" gitrepo &&
237 cd hgrepo &&
238 hg strip 1 &&
240 echo three >> content &&
241 hg commit -m three &&
243 echo four >> content &&
244 hg commit -m four
245 ) &&
248 cd gitrepo &&
249 git fetch &&
250 git log --format="%s" origin/master > ../actual
251 ) &&
253 hg -R hgrepo log --template "{desc}\n" > expected &&
254 test_cmp actual expected
257 test_expect_success 'remote push with master bookmark' '
258 test_when_finished "rm -rf hgrepo gitrepo*" &&
261 hg init hgrepo &&
262 cd hgrepo &&
263 echo zero > content &&
264 hg add content &&
265 hg commit -m zero &&
266 hg bookmark master &&
267 echo one > content &&
268 hg commit -m one
269 ) &&
272 git clone "hg::hgrepo" gitrepo &&
273 cd gitrepo &&
274 echo two > content &&
275 git commit -a -m two &&
276 git push
277 ) &&
279 check_branch hgrepo default two
282 cat > expected <<EOF
283 changeset: 0:6e2126489d3d
284 tag: tip
285 user: A U Thor <author@example.com>
286 date: Mon Jan 01 00:00:00 2007 +0230
287 summary: one
291 test_expect_success 'remote push from master branch' '
292 test_when_finished "rm -rf hgrepo gitrepo*" &&
294 hg init hgrepo &&
297 git init gitrepo &&
298 cd gitrepo &&
299 git remote add origin "hg::../hgrepo" &&
300 echo one > content &&
301 git add content &&
302 git commit -a -m one &&
303 git push origin master
304 ) &&
306 hg -R hgrepo log > actual &&
307 cat actual &&
308 test_cmp expected actual &&
310 check_branch hgrepo default one
313 GIT_REMOTE_HG_TEST_REMOTE=1
314 export GIT_REMOTE_HG_TEST_REMOTE
316 test_expect_success 'remote cloning' '
317 test_when_finished "rm -rf gitrepo*" &&
320 hg init hgrepo &&
321 cd hgrepo &&
322 echo zero > content &&
323 hg add content &&
324 hg commit -m zero
325 ) &&
327 git clone "hg::hgrepo" gitrepo &&
328 check gitrepo HEAD zero
331 test_expect_success 'remote update bookmark' '
332 test_when_finished "rm -rf gitrepo*" &&
335 cd hgrepo &&
336 hg bookmark devel
337 ) &&
340 git clone "hg::hgrepo" gitrepo &&
341 cd gitrepo &&
342 git checkout --quiet devel &&
343 echo devel > content &&
344 git commit -a -m devel &&
345 git push --quiet
346 ) &&
348 check_bookmark hgrepo devel devel
351 test_expect_success 'remote new bookmark' '
352 test_when_finished "rm -rf gitrepo*" &&
355 git clone "hg::hgrepo" gitrepo &&
356 cd gitrepo &&
357 git checkout --quiet -b feature-b &&
358 echo feature-b > content &&
359 git commit -a -m feature-b &&
360 git push --quiet origin feature-b
361 ) &&
363 check_bookmark hgrepo feature-b feature-b
366 test_expect_success 'remote push diverged' '
367 test_when_finished "rm -rf gitrepo*" &&
369 git clone "hg::hgrepo" gitrepo &&
372 cd hgrepo &&
373 hg checkout default &&
374 echo bump > content &&
375 hg commit -m bump
376 ) &&
379 cd gitrepo &&
380 echo diverge > content &&
381 git commit -a -m diverged &&
382 check_push 1 <<-EOF
383 master:non-fast-forward
385 ) &&
387 check_branch hgrepo default bump
390 test_expect_success 'remote update bookmark diverge' '
391 test_when_finished "rm -rf gitrepo*" &&
394 cd hgrepo &&
395 hg checkout tip^ &&
396 hg bookmark diverge
397 ) &&
399 git clone "hg::hgrepo" gitrepo &&
402 cd hgrepo &&
403 echo "bump bookmark" > content &&
404 hg commit -m "bump bookmark"
405 ) &&
408 cd gitrepo &&
409 git checkout --quiet diverge &&
410 echo diverge > content &&
411 git commit -a -m diverge &&
412 check_push 1 <<-EOF
413 diverge:fetch-first
415 ) &&
417 check_bookmark hgrepo diverge "bump bookmark"
420 test_expect_success 'remote new bookmark multiple branch head' '
421 test_when_finished "rm -rf gitrepo*" &&
424 git clone "hg::hgrepo" gitrepo &&
425 cd gitrepo &&
426 git checkout --quiet -b feature-c HEAD^ &&
427 echo feature-c > content &&
428 git commit -a -m feature-c &&
429 git push --quiet origin feature-c
430 ) &&
432 check_bookmark hgrepo feature-c feature-c
435 # cleanup previous stuff
436 rm -rf hgrepo
438 setup_big_push () {
440 hg init hgrepo &&
441 cd hgrepo &&
442 echo zero > content &&
443 hg add content &&
444 hg commit -m zero &&
445 hg bookmark bad_bmark1 &&
446 echo one > content &&
447 hg commit -m one &&
448 hg bookmark bad_bmark2 &&
449 hg bookmark good_bmark &&
450 hg bookmark -i good_bmark &&
451 hg -q branch good_branch &&
452 echo "good branch" > content &&
453 hg commit -m "good branch" &&
454 hg -q branch bad_branch &&
455 echo "bad branch" > content &&
456 hg commit -m "bad branch"
457 ) &&
459 git clone "hg::hgrepo" gitrepo &&
462 cd gitrepo &&
463 echo two > content &&
464 git commit -q -a -m two &&
466 git checkout -q good_bmark &&
467 echo three > content &&
468 git commit -q -a -m three &&
470 git checkout -q bad_bmark1 &&
471 git reset --hard HEAD^ &&
472 echo four > content &&
473 git commit -q -a -m four &&
475 git checkout -q bad_bmark2 &&
476 git reset --hard HEAD^ &&
477 echo five > content &&
478 git commit -q -a -m five &&
480 git checkout -q -b new_bmark master &&
481 echo six > content &&
482 git commit -q -a -m six &&
484 git checkout -q branches/good_branch &&
485 echo seven > content &&
486 git commit -q -a -m seven &&
487 echo eight > content &&
488 git commit -q -a -m eight &&
490 git checkout -q branches/bad_branch &&
491 git reset --hard HEAD^ &&
492 echo nine > content &&
493 git commit -q -a -m nine &&
495 git checkout -q -b branches/new_branch master &&
496 echo ten > content &&
497 git commit -q -a -m ten
501 test_expect_success 'remote big push' '
502 test_when_finished "rm -rf hgrepo gitrepo*" &&
504 setup_big_push
507 cd gitrepo &&
509 check_push 1 --all <<-EOF
510 master
511 good_bmark
512 branches/good_branch
513 new_bmark:new
514 branches/new_branch:new
515 bad_bmark1:non-fast-forward
516 bad_bmark2:non-fast-forward
517 branches/bad_branch:non-fast-forward
519 ) &&
521 check_branch hgrepo default one &&
522 check_branch hgrepo good_branch "good branch" &&
523 check_branch hgrepo bad_branch "bad branch" &&
524 check_branch hgrepo new_branch '' &&
525 check_bookmark hgrepo good_bmark one &&
526 check_bookmark hgrepo bad_bmark1 one &&
527 check_bookmark hgrepo bad_bmark2 one &&
528 check_bookmark hgrepo new_bmark ''
531 test_expect_success 'remote big push fetch first' '
532 test_when_finished "rm -rf hgrepo gitrepo*" &&
535 hg init hgrepo &&
536 cd hgrepo &&
537 echo zero > content &&
538 hg add content &&
539 hg commit -m zero &&
540 hg bookmark bad_bmark &&
541 hg bookmark good_bmark &&
542 hg bookmark -i good_bmark &&
543 hg -q branch good_branch &&
544 echo "good branch" > content &&
545 hg commit -m "good branch" &&
546 hg -q branch bad_branch &&
547 echo "bad branch" > content &&
548 hg commit -m "bad branch"
549 ) &&
551 git clone "hg::hgrepo" gitrepo &&
554 cd hgrepo &&
555 hg bookmark -f bad_bmark &&
556 echo update_bmark > content &&
557 hg commit -m "update bmark"
558 ) &&
561 cd gitrepo &&
562 echo two > content &&
563 git commit -q -a -m two &&
565 git checkout -q good_bmark &&
566 echo three > content &&
567 git commit -q -a -m three &&
569 git checkout -q bad_bmark &&
570 echo four > content &&
571 git commit -q -a -m four &&
573 git checkout -q branches/bad_branch &&
574 echo five > content &&
575 git commit -q -a -m five &&
577 check_push 1 --all <<-EOF
578 master
579 good_bmark
580 new_bmark:new
581 new_branch:new
582 bad_bmark:fetch-first
583 branches/bad_branch:festch-first
586 git fetch &&
588 check_push 1 --all <<-EOF
589 master
590 good_bmark
591 bad_bmark:non-fast-forward
592 branches/bad_branch:non-fast-forward
597 test_expect_failure 'remote big push dry-run' '
598 test_when_finished "rm -rf hgrepo gitrepo*" &&
600 setup_big_push
603 cd gitrepo &&
605 check_push 0 --dry-run --all <<-EOF
606 master
607 good_bmark
608 branches/good_branch
609 new_bmark:new
610 branches/new_branch:new
611 bad_bmark1:non-fast-forward
612 bad_bmark2:non-fast-forward
613 branches/bad_branch:non-fast-forward
616 check_push 0 --dry-run master good_bmark new_bmark branches/good_branch branches/new_branch <<-EOF
617 master
618 good_bmark
619 branches/good_branch
620 new_bmark:new
621 branches/new_branch:new
623 ) &&
625 check_branch hgrepo default one &&
626 check_branch hgrepo good_branch "good branch" &&
627 check_branch hgrepo bad_branch "bad branch" &&
628 check_branch hgrepo new_branch '' &&
629 check_bookmark hgrepo good_bmark one &&
630 check_bookmark hgrepo bad_bmark1 one &&
631 check_bookmark hgrepo bad_bmark2 one &&
632 check_bookmark hgrepo new_bmark ''
635 test_expect_success 'remote double failed push' '
636 test_when_finished "rm -rf hgrepo gitrepo*" &&
639 hg init hgrepo &&
640 cd hgrepo &&
641 echo zero > content &&
642 hg add content &&
643 hg commit -m zero &&
644 echo one > content &&
645 hg commit -m one
646 ) &&
649 git clone "hg::hgrepo" gitrepo &&
650 cd gitrepo &&
651 git reset --hard HEAD^ &&
652 echo two > content &&
653 git commit -a -m two &&
654 test_expect_code 1 git push &&
655 test_expect_code 1 git push
659 test_done