checkout: update synopsys and documentation on detaching HEAD
[git/raj.git] / contrib / remote-helpers / test-hg.sh
blobf7ce8aa853d5ad9689f49a9cadf0d43ce16ed34f
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 'forced-update')
72 grep "^ + [a-f0-9]*\.\.\.[a-f0-9]* *${branch} -> ${branch} (forced update)$" error || ref_ret=1
74 '')
75 grep "^ [a-f0-9]*\.\.[a-f0-9]* *${branch} -> ${branch}$" error || ref_ret=1
77 esac
78 let 'ref_ret' && echo "match for '$branch' failed" && break
79 done
81 if let 'expected_ret != ret || ref_ret'
82 then
83 return 1
86 return 0
89 setup () {
91 echo "[ui]"
92 echo "username = H G Wells <wells@example.com>"
93 echo "[extensions]"
94 echo "mq ="
95 ) >> "$HOME"/.hgrc &&
97 GIT_AUTHOR_DATE="2007-01-01 00:00:00 +0230" &&
98 GIT_COMMITTER_DATE="$GIT_AUTHOR_DATE" &&
99 export GIT_COMMITTER_DATE GIT_AUTHOR_DATE
102 setup
104 test_expect_success 'cloning' '
105 test_when_finished "rm -rf gitrepo*" &&
108 hg init hgrepo &&
109 cd hgrepo &&
110 echo zero > content &&
111 hg add content &&
112 hg commit -m zero
113 ) &&
115 git clone "hg::hgrepo" gitrepo &&
116 check gitrepo HEAD zero
119 test_expect_success 'cloning with branches' '
120 test_when_finished "rm -rf gitrepo*" &&
123 cd hgrepo &&
124 hg branch next &&
125 echo next > content &&
126 hg commit -m next
127 ) &&
129 git clone "hg::hgrepo" gitrepo &&
130 check gitrepo origin/branches/next next
133 test_expect_success 'cloning with bookmarks' '
134 test_when_finished "rm -rf gitrepo*" &&
137 cd hgrepo &&
138 hg checkout default &&
139 hg bookmark feature-a &&
140 echo feature-a > content &&
141 hg commit -m feature-a
142 ) &&
144 git clone "hg::hgrepo" gitrepo &&
145 check gitrepo origin/feature-a feature-a
148 test_expect_success 'update bookmark' '
149 test_when_finished "rm -rf gitrepo*" &&
152 cd hgrepo &&
153 hg bookmark devel
154 ) &&
157 git clone "hg::hgrepo" gitrepo &&
158 cd gitrepo &&
159 git checkout --quiet devel &&
160 echo devel > content &&
161 git commit -a -m devel &&
162 git push --quiet
163 ) &&
165 check_bookmark hgrepo devel devel
168 test_expect_success 'new bookmark' '
169 test_when_finished "rm -rf gitrepo*" &&
172 git clone "hg::hgrepo" gitrepo &&
173 cd gitrepo &&
174 git checkout --quiet -b feature-b &&
175 echo feature-b > content &&
176 git commit -a -m feature-b &&
177 git push --quiet origin feature-b
178 ) &&
180 check_bookmark hgrepo feature-b feature-b
183 # cleanup previous stuff
184 rm -rf hgrepo
186 author_test () {
187 echo $1 >> content &&
188 hg commit -u "$2" -m "add $1" &&
189 echo "$3" >> ../expected
192 test_expect_success 'authors' '
193 test_when_finished "rm -rf hgrepo gitrepo" &&
196 hg init hgrepo &&
197 cd hgrepo &&
199 touch content &&
200 hg add content &&
202 > ../expected &&
203 author_test alpha "" "H G Wells <wells@example.com>" &&
204 author_test beta "test" "test <unknown>" &&
205 author_test beta "test <test@example.com> (comment)" "test <test@example.com>" &&
206 author_test gamma "<test@example.com>" "Unknown <test@example.com>" &&
207 author_test delta "name<test@example.com>" "name <test@example.com>" &&
208 author_test epsilon "name <test@example.com" "name <test@example.com>" &&
209 author_test zeta " test " "test <unknown>" &&
210 author_test eta "test < test@example.com >" "test <test@example.com>" &&
211 author_test theta "test >test@example.com>" "test <test@example.com>" &&
212 author_test iota "test < test <at> example <dot> com>" "test <unknown>" &&
213 author_test kappa "test@example.com" "Unknown <test@example.com>"
214 ) &&
216 git clone "hg::hgrepo" gitrepo &&
217 git --git-dir=gitrepo/.git log --reverse --format="%an <%ae>" > actual &&
219 test_cmp expected actual
222 test_expect_success 'strip' '
223 test_when_finished "rm -rf hgrepo gitrepo" &&
226 hg init hgrepo &&
227 cd hgrepo &&
229 echo one >> content &&
230 hg add content &&
231 hg commit -m one &&
233 echo two >> content &&
234 hg commit -m two
235 ) &&
237 git clone "hg::hgrepo" gitrepo &&
240 cd hgrepo &&
241 hg strip 1 &&
243 echo three >> content &&
244 hg commit -m three &&
246 echo four >> content &&
247 hg commit -m four
248 ) &&
251 cd gitrepo &&
252 git fetch &&
253 git log --format="%s" origin/master > ../actual
254 ) &&
256 hg -R hgrepo log --template "{desc}\n" > expected &&
257 test_cmp actual expected
260 test_expect_success 'remote push with master bookmark' '
261 test_when_finished "rm -rf hgrepo gitrepo*" &&
264 hg init hgrepo &&
265 cd hgrepo &&
266 echo zero > content &&
267 hg add content &&
268 hg commit -m zero &&
269 hg bookmark master &&
270 echo one > content &&
271 hg commit -m one
272 ) &&
275 git clone "hg::hgrepo" gitrepo &&
276 cd gitrepo &&
277 echo two > content &&
278 git commit -a -m two &&
279 git push
280 ) &&
282 check_branch hgrepo default two
285 cat > expected <<EOF
286 changeset: 0:6e2126489d3d
287 tag: tip
288 user: A U Thor <author@example.com>
289 date: Mon Jan 01 00:00:00 2007 +0230
290 summary: one
294 test_expect_success 'remote push from master branch' '
295 test_when_finished "rm -rf hgrepo gitrepo*" &&
297 hg init hgrepo &&
300 git init gitrepo &&
301 cd gitrepo &&
302 git remote add origin "hg::../hgrepo" &&
303 echo one > content &&
304 git add content &&
305 git commit -a -m one &&
306 git push origin master
307 ) &&
309 hg -R hgrepo log > actual &&
310 cat actual &&
311 test_cmp expected actual &&
313 check_branch hgrepo default one
316 GIT_REMOTE_HG_TEST_REMOTE=1
317 export GIT_REMOTE_HG_TEST_REMOTE
319 test_expect_success 'remote cloning' '
320 test_when_finished "rm -rf gitrepo*" &&
323 hg init hgrepo &&
324 cd hgrepo &&
325 echo zero > content &&
326 hg add content &&
327 hg commit -m zero
328 ) &&
330 git clone "hg::hgrepo" gitrepo &&
331 check gitrepo HEAD zero
334 test_expect_success 'remote update bookmark' '
335 test_when_finished "rm -rf gitrepo*" &&
338 cd hgrepo &&
339 hg bookmark devel
340 ) &&
343 git clone "hg::hgrepo" gitrepo &&
344 cd gitrepo &&
345 git checkout --quiet devel &&
346 echo devel > content &&
347 git commit -a -m devel &&
348 git push --quiet
349 ) &&
351 check_bookmark hgrepo devel devel
354 test_expect_success 'remote new bookmark' '
355 test_when_finished "rm -rf gitrepo*" &&
358 git clone "hg::hgrepo" gitrepo &&
359 cd gitrepo &&
360 git checkout --quiet -b feature-b &&
361 echo feature-b > content &&
362 git commit -a -m feature-b &&
363 git push --quiet origin feature-b
364 ) &&
366 check_bookmark hgrepo feature-b feature-b
369 test_expect_success 'remote push diverged' '
370 test_when_finished "rm -rf gitrepo*" &&
372 git clone "hg::hgrepo" gitrepo &&
375 cd hgrepo &&
376 hg checkout default &&
377 echo bump > content &&
378 hg commit -m bump
379 ) &&
382 cd gitrepo &&
383 echo diverge > content &&
384 git commit -a -m diverged &&
385 check_push 1 <<-EOF
386 master:non-fast-forward
388 ) &&
390 check_branch hgrepo default bump
393 test_expect_success 'remote update bookmark diverge' '
394 test_when_finished "rm -rf gitrepo*" &&
397 cd hgrepo &&
398 hg checkout tip^ &&
399 hg bookmark diverge
400 ) &&
402 git clone "hg::hgrepo" gitrepo &&
405 cd hgrepo &&
406 echo "bump bookmark" > content &&
407 hg commit -m "bump bookmark"
408 ) &&
411 cd gitrepo &&
412 git checkout --quiet diverge &&
413 echo diverge > content &&
414 git commit -a -m diverge &&
415 check_push 1 <<-EOF
416 diverge:fetch-first
418 ) &&
420 check_bookmark hgrepo diverge "bump bookmark"
423 test_expect_success 'remote new bookmark multiple branch head' '
424 test_when_finished "rm -rf gitrepo*" &&
427 git clone "hg::hgrepo" gitrepo &&
428 cd gitrepo &&
429 git checkout --quiet -b feature-c HEAD^ &&
430 echo feature-c > content &&
431 git commit -a -m feature-c &&
432 git push --quiet origin feature-c
433 ) &&
435 check_bookmark hgrepo feature-c feature-c
438 # cleanup previous stuff
439 rm -rf hgrepo
441 setup_big_push () {
443 hg init hgrepo &&
444 cd hgrepo &&
445 echo zero > content &&
446 hg add content &&
447 hg commit -m zero &&
448 hg bookmark bad_bmark1 &&
449 echo one > content &&
450 hg commit -m one &&
451 hg bookmark bad_bmark2 &&
452 hg bookmark good_bmark &&
453 hg bookmark -i good_bmark &&
454 hg -q branch good_branch &&
455 echo "good branch" > content &&
456 hg commit -m "good branch" &&
457 hg -q branch bad_branch &&
458 echo "bad branch" > content &&
459 hg commit -m "bad branch"
460 ) &&
462 git clone "hg::hgrepo" gitrepo &&
465 cd gitrepo &&
466 echo two > content &&
467 git commit -q -a -m two &&
469 git checkout -q good_bmark &&
470 echo three > content &&
471 git commit -q -a -m three &&
473 git checkout -q bad_bmark1 &&
474 git reset --hard HEAD^ &&
475 echo four > content &&
476 git commit -q -a -m four &&
478 git checkout -q bad_bmark2 &&
479 git reset --hard HEAD^ &&
480 echo five > content &&
481 git commit -q -a -m five &&
483 git checkout -q -b new_bmark master &&
484 echo six > content &&
485 git commit -q -a -m six &&
487 git checkout -q branches/good_branch &&
488 echo seven > content &&
489 git commit -q -a -m seven &&
490 echo eight > content &&
491 git commit -q -a -m eight &&
493 git checkout -q branches/bad_branch &&
494 git reset --hard HEAD^ &&
495 echo nine > content &&
496 git commit -q -a -m nine &&
498 git checkout -q -b branches/new_branch master &&
499 echo ten > content &&
500 git commit -q -a -m ten
504 test_expect_success 'remote big push' '
505 test_when_finished "rm -rf hgrepo gitrepo*" &&
507 setup_big_push
510 cd gitrepo &&
512 check_push 1 --all <<-EOF
513 master
514 good_bmark
515 branches/good_branch
516 new_bmark:new
517 branches/new_branch:new
518 bad_bmark1:non-fast-forward
519 bad_bmark2:non-fast-forward
520 branches/bad_branch:non-fast-forward
522 ) &&
524 check_branch hgrepo default one &&
525 check_branch hgrepo good_branch "good branch" &&
526 check_branch hgrepo bad_branch "bad branch" &&
527 check_branch hgrepo new_branch '' &&
528 check_bookmark hgrepo good_bmark one &&
529 check_bookmark hgrepo bad_bmark1 one &&
530 check_bookmark hgrepo bad_bmark2 one &&
531 check_bookmark hgrepo new_bmark ''
534 test_expect_success 'remote big push fetch first' '
535 test_when_finished "rm -rf hgrepo gitrepo*" &&
538 hg init hgrepo &&
539 cd hgrepo &&
540 echo zero > content &&
541 hg add content &&
542 hg commit -m zero &&
543 hg bookmark bad_bmark &&
544 hg bookmark good_bmark &&
545 hg bookmark -i good_bmark &&
546 hg -q branch good_branch &&
547 echo "good branch" > content &&
548 hg commit -m "good branch" &&
549 hg -q branch bad_branch &&
550 echo "bad branch" > content &&
551 hg commit -m "bad branch"
552 ) &&
554 git clone "hg::hgrepo" gitrepo &&
557 cd hgrepo &&
558 hg bookmark -f bad_bmark &&
559 echo update_bmark > content &&
560 hg commit -m "update bmark"
561 ) &&
564 cd gitrepo &&
565 echo two > content &&
566 git commit -q -a -m two &&
568 git checkout -q good_bmark &&
569 echo three > content &&
570 git commit -q -a -m three &&
572 git checkout -q bad_bmark &&
573 echo four > content &&
574 git commit -q -a -m four &&
576 git checkout -q branches/bad_branch &&
577 echo five > content &&
578 git commit -q -a -m five &&
580 check_push 1 --all <<-EOF
581 master
582 good_bmark
583 new_bmark:new
584 new_branch:new
585 bad_bmark:fetch-first
586 branches/bad_branch:festch-first
589 git fetch &&
591 check_push 1 --all <<-EOF
592 master
593 good_bmark
594 bad_bmark:non-fast-forward
595 branches/bad_branch:non-fast-forward
600 test_expect_failure 'remote big push force' '
601 test_when_finished "rm -rf hgrepo gitrepo*" &&
603 setup_big_push
606 cd gitrepo &&
608 check_push 0 --force --all <<-EOF
609 master
610 good_bmark
611 branches/good_branch
612 new_bmark:new
613 branches/new_branch:new
614 bad_bmark1:forced-update
615 bad_bmark2:forced-update
616 branches/bad_branch:forced-update
618 ) &&
620 check_branch hgrepo default six &&
621 check_branch hgrepo good_branch eight &&
622 check_branch hgrepo bad_branch nine &&
623 check_branch hgrepo new_branch ten &&
624 check_bookmark hgrepo good_bmark three &&
625 check_bookmark hgrepo bad_bmark1 four &&
626 check_bookmark hgrepo bad_bmark2 five &&
627 check_bookmark hgrepo new_bmark six
630 test_expect_failure 'remote big push dry-run' '
631 test_when_finished "rm -rf hgrepo gitrepo*" &&
633 setup_big_push
636 cd gitrepo &&
638 check_push 0 --dry-run --all <<-EOF
639 master
640 good_bmark
641 branches/good_branch
642 new_bmark:new
643 branches/new_branch:new
644 bad_bmark1:non-fast-forward
645 bad_bmark2:non-fast-forward
646 branches/bad_branch:non-fast-forward
649 check_push 0 --dry-run master good_bmark new_bmark branches/good_branch branches/new_branch <<-EOF
650 master
651 good_bmark
652 branches/good_branch
653 new_bmark:new
654 branches/new_branch:new
656 ) &&
658 check_branch hgrepo default one &&
659 check_branch hgrepo good_branch "good branch" &&
660 check_branch hgrepo bad_branch "bad branch" &&
661 check_branch hgrepo new_branch '' &&
662 check_bookmark hgrepo good_bmark one &&
663 check_bookmark hgrepo bad_bmark1 one &&
664 check_bookmark hgrepo bad_bmark2 one &&
665 check_bookmark hgrepo new_bmark ''
668 test_expect_success 'remote double failed push' '
669 test_when_finished "rm -rf hgrepo gitrepo*" &&
672 hg init hgrepo &&
673 cd hgrepo &&
674 echo zero > content &&
675 hg add content &&
676 hg commit -m zero &&
677 echo one > content &&
678 hg commit -m one
679 ) &&
682 git clone "hg::hgrepo" gitrepo &&
683 cd gitrepo &&
684 git reset --hard HEAD^ &&
685 echo two > content &&
686 git commit -a -m two &&
687 test_expect_code 1 git push &&
688 test_expect_code 1 git push
692 test_done