git-remote-hg: use internal clone's hgrc
[git] / contrib / remote-helpers / test-hg.sh
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
9 test_description='Test remote-hg'
10
11 test -n "$TEST_DIRECTORY" || TEST_DIRECTORY=${0%/*}/../../t
12 . "$TEST_DIRECTORY"/test-lib.sh
13
14 if ! test_have_prereq PYTHON
15 then
16         skip_all='skipping remote-hg tests; python not available'
17         test_done
18 fi
19
20 if ! python -c 'import mercurial'
21 then
22         skip_all='skipping remote-hg tests; mercurial not available'
23         test_done
24 fi
25
26 check () {
27         echo $3 >expected &&
28         git --git-dir=$1/.git log --format='%s' -1 $2 >actual
29         test_cmp expected actual
30 }
31
32 check_branch () {
33         if test -n "$3"
34         then
35                 echo $3 >expected &&
36                 hg -R $1 log -r $2 --template '{desc}\n' >actual &&
37                 test_cmp expected actual
38         else
39                 hg -R $1 branches >out &&
40                 ! grep $2 out
41         fi
42 }
43
44 check_bookmark () {
45         if test -n "$3"
46         then
47                 echo $3 >expected &&
48                 hg -R $1 log -r "bookmark('$2')" --template '{desc}\n' >actual &&
49                 test_cmp expected actual
50         else
51                 hg -R $1 bookmarks >out &&
52                 ! grep $2 out
53         fi
54 }
55
56 check_push () {
57         expected_ret=$1 ret=0 ref_ret=0
58
59         shift
60         git push origin "$@" 2>error
61         ret=$?
62         cat error
63
64         while IFS=':' read branch kind
65         do
66                 case "$kind" in
67                 'new')
68                         grep "^ \* \[new branch\] *${branch} -> ${branch}$" error || ref_ret=1
69                         ;;
70                 'non-fast-forward')
71                         grep "^ ! \[rejected\] *${branch} -> ${branch} (non-fast-forward)$" error || ref_ret=1
72                         ;;
73                 'fetch-first')
74                         grep "^ ! \[rejected\] *${branch} -> ${branch} (fetch first)$" error || ref_ret=1
75                         ;;
76                 'forced-update')
77                         grep "^ + [a-f0-9]*\.\.\.[a-f0-9]* *${branch} -> ${branch} (forced update)$" error || ref_ret=1
78                         ;;
79                 '')
80                         grep "^   [a-f0-9]*\.\.[a-f0-9]* *${branch} -> ${branch}$" error || ref_ret=1
81                         ;;
82                 esac
83                 test $ref_ret -ne 0 && echo "match for '$branch' failed" && break
84         done
85
86         if test $expected_ret -ne $ret || test $ref_ret -ne 0
87         then
88                 return 1
89         fi
90
91         return 0
92 }
93
94 setup () {
95         (
96         echo "[ui]"
97         echo "username = H G Wells <wells@example.com>"
98         echo "[extensions]"
99         echo "mq ="
100         ) >>"$HOME"/.hgrc &&
101
102         GIT_AUTHOR_DATE="2007-01-01 00:00:00 +0230" &&
103         GIT_COMMITTER_DATE="$GIT_AUTHOR_DATE" &&
104         export GIT_COMMITTER_DATE GIT_AUTHOR_DATE
105 }
106
107 setup
108
109 test_expect_success 'setup' '
110         (
111         hg init hgrepo &&
112         cd hgrepo &&
113         echo zero >content &&
114         hg add content &&
115         hg commit -m zero
116         )
117 '
118
119 test_expect_success 'cloning' '
120         test_when_finished "rm -rf gitrepo*" &&
121         git clone "hg::hgrepo" gitrepo &&
122         check gitrepo HEAD zero
123 '
124
125 test_expect_success 'cloning with branches' '
126         test_when_finished "rm -rf gitrepo*" &&
127
128         (
129         cd hgrepo &&
130         hg branch next &&
131         echo next >content &&
132         hg commit -m next
133         ) &&
134
135         git clone "hg::hgrepo" gitrepo &&
136         check gitrepo origin/branches/next next
137 '
138
139 test_expect_success 'cloning with bookmarks' '
140         test_when_finished "rm -rf gitrepo*" &&
141
142         (
143         cd hgrepo &&
144         hg checkout default &&
145         hg bookmark feature-a &&
146         echo feature-a >content &&
147         hg commit -m feature-a
148         ) &&
149
150         git clone "hg::hgrepo" gitrepo &&
151         check gitrepo origin/feature-a feature-a
152 '
153
154 test_expect_success 'update bookmark' '
155         test_when_finished "rm -rf gitrepo*" &&
156
157         (
158         cd hgrepo &&
159         hg bookmark devel
160         ) &&
161
162         (
163         git clone "hg::hgrepo" gitrepo &&
164         cd gitrepo &&
165         git checkout --quiet devel &&
166         echo devel >content &&
167         git commit -a -m devel &&
168         git push --quiet
169         ) &&
170
171         check_bookmark hgrepo devel devel
172 '
173
174 test_expect_success 'new bookmark' '
175         test_when_finished "rm -rf gitrepo*" &&
176
177         (
178         git clone "hg::hgrepo" gitrepo &&
179         cd gitrepo &&
180         git checkout --quiet -b feature-b &&
181         echo feature-b >content &&
182         git commit -a -m feature-b &&
183         git push --quiet origin feature-b
184         ) &&
185
186         check_bookmark hgrepo feature-b feature-b
187 '
188
189 # cleanup previous stuff
190 rm -rf hgrepo
191
192 author_test () {
193         echo $1 >>content &&
194         hg commit -u "$2" -m "add $1" &&
195         echo "$3" >>../expected
196 }
197
198 test_expect_success 'authors' '
199         test_when_finished "rm -rf hgrepo gitrepo" &&
200
201         (
202         hg init hgrepo &&
203         cd hgrepo &&
204
205         touch content &&
206         hg add content &&
207
208         >../expected &&
209         author_test alpha "" "H G Wells <wells@example.com>" &&
210         author_test beta "beta" "beta <unknown>" &&
211         author_test gamma "gamma <test@example.com> (comment)" "gamma <test@example.com>" &&
212         author_test delta "<delta@example.com>" "Unknown <delta@example.com>" &&
213         author_test epsilon "epsilon<test@example.com>" "epsilon <test@example.com>" &&
214         author_test zeta "zeta <test@example.com" "zeta <test@example.com>" &&
215         author_test eta " eta " "eta <unknown>" &&
216         author_test theta "theta < test@example.com >" "theta <test@example.com>" &&
217         author_test iota "iota >test@example.com>" "iota <test@example.com>" &&
218         author_test kappa "kappa < test <at> example <dot> com>" "kappa <unknown>" &&
219         author_test lambda "lambda@example.com" "Unknown <lambda@example.com>" &&
220         author_test mu "mu.mu@example.com" "Unknown <mu.mu@example.com>"
221         ) &&
222
223         git clone "hg::hgrepo" gitrepo &&
224         git --git-dir=gitrepo/.git log --reverse --format="%an <%ae>" >actual &&
225
226         test_cmp expected actual
227 '
228
229 test_expect_success 'strip' '
230         test_when_finished "rm -rf hgrepo gitrepo" &&
231
232         (
233         hg init hgrepo &&
234         cd hgrepo &&
235
236         echo one >>content &&
237         hg add content &&
238         hg commit -m one &&
239
240         echo two >>content &&
241         hg commit -m two
242         ) &&
243
244         git clone "hg::hgrepo" gitrepo &&
245
246         (
247         cd hgrepo &&
248         hg strip 1 &&
249
250         echo three >>content &&
251         hg commit -m three &&
252
253         echo four >>content &&
254         hg commit -m four
255         ) &&
256
257         (
258         cd gitrepo &&
259         git fetch &&
260         git log --format="%s" origin/master >../actual
261         ) &&
262
263         hg -R hgrepo log --template "{desc}\n" >expected &&
264         test_cmp actual expected
265 '
266
267 test_expect_success 'remote push with master bookmark' '
268         test_when_finished "rm -rf hgrepo gitrepo*" &&
269
270         (
271         hg init hgrepo &&
272         cd hgrepo &&
273         echo zero >content &&
274         hg add content &&
275         hg commit -m zero &&
276         hg bookmark master &&
277         echo one >content &&
278         hg commit -m one
279         ) &&
280
281         (
282         git clone "hg::hgrepo" gitrepo &&
283         cd gitrepo &&
284         echo two >content &&
285         git commit -a -m two &&
286         git push
287         ) &&
288
289         check_branch hgrepo default two
290 '
291
292 cat >expected <<\EOF
293 changeset:   0:6e2126489d3d
294 tag:         tip
295 user:        A U Thor <author@example.com>
296 date:        Mon Jan 01 00:00:00 2007 +0230
297 summary:     one
298
299 EOF
300
301 test_expect_success 'remote push from master branch' '
302         test_when_finished "rm -rf hgrepo gitrepo*" &&
303
304         hg init hgrepo &&
305
306         (
307         git init gitrepo &&
308         cd gitrepo &&
309         git remote add origin "hg::../hgrepo" &&
310         echo one >content &&
311         git add content &&
312         git commit -a -m one &&
313         git push origin master
314         ) &&
315
316         hg -R hgrepo log >actual &&
317         cat actual &&
318         test_cmp expected actual &&
319
320         check_branch hgrepo default one
321 '
322
323 GIT_REMOTE_HG_TEST_REMOTE=1
324 export GIT_REMOTE_HG_TEST_REMOTE
325
326 test_expect_success 'remote cloning' '
327         test_when_finished "rm -rf gitrepo*" &&
328
329         (
330         hg init hgrepo &&
331         cd hgrepo &&
332         echo zero >content &&
333         hg add content &&
334         hg commit -m zero
335         ) &&
336
337         git clone "hg::hgrepo" gitrepo &&
338         check gitrepo HEAD zero
339 '
340
341 test_expect_success 'moving remote clone' '
342         test_when_finished "rm -rf gitrepo*" &&
343
344         (
345         git clone "hg::hgrepo" gitrepo &&
346         mv gitrepo gitrepo2 &&
347         cd gitrepo2 &&
348         git fetch
349         )
350 '
351
352 test_expect_success 'remote update bookmark' '
353         test_when_finished "rm -rf gitrepo*" &&
354
355         (
356         cd hgrepo &&
357         hg bookmark devel
358         ) &&
359
360         (
361         git clone "hg::hgrepo" gitrepo &&
362         cd gitrepo &&
363         git checkout --quiet devel &&
364         echo devel >content &&
365         git commit -a -m devel &&
366         git push --quiet
367         ) &&
368
369         check_bookmark hgrepo devel devel
370 '
371
372 test_expect_success 'remote new bookmark' '
373         test_when_finished "rm -rf gitrepo*" &&
374
375         (
376         git clone "hg::hgrepo" gitrepo &&
377         cd gitrepo &&
378         git checkout --quiet -b feature-b &&
379         echo feature-b >content &&
380         git commit -a -m feature-b &&
381         git push --quiet origin feature-b
382         ) &&
383
384         check_bookmark hgrepo feature-b feature-b
385 '
386
387 test_expect_success 'remote push diverged' '
388         test_when_finished "rm -rf gitrepo*" &&
389
390         git clone "hg::hgrepo" gitrepo &&
391
392         (
393         cd hgrepo &&
394         hg checkout default &&
395         echo bump >content &&
396         hg commit -m bump
397         ) &&
398
399         (
400         cd gitrepo &&
401         echo diverge >content &&
402         git commit -a -m diverged &&
403         check_push 1 <<-\EOF
404         master:non-fast-forward
405         EOF
406         ) &&
407
408         check_branch hgrepo default bump
409 '
410
411 test_expect_success 'remote update bookmark diverge' '
412         test_when_finished "rm -rf gitrepo*" &&
413
414         (
415         cd hgrepo &&
416         hg checkout tip^ &&
417         hg bookmark diverge
418         ) &&
419
420         git clone "hg::hgrepo" gitrepo &&
421
422         (
423         cd hgrepo &&
424         echo "bump bookmark" >content &&
425         hg commit -m "bump bookmark"
426         ) &&
427
428         (
429         cd gitrepo &&
430         git checkout --quiet diverge &&
431         echo diverge >content &&
432         git commit -a -m diverge &&
433         check_push 1 <<-\EOF
434         diverge:fetch-first
435         EOF
436         ) &&
437
438         check_bookmark hgrepo diverge "bump bookmark"
439 '
440
441 test_expect_success 'remote new bookmark multiple branch head' '
442         test_when_finished "rm -rf gitrepo*" &&
443
444         (
445         git clone "hg::hgrepo" gitrepo &&
446         cd gitrepo &&
447         git checkout --quiet -b feature-c HEAD^ &&
448         echo feature-c >content &&
449         git commit -a -m feature-c &&
450         git push --quiet origin feature-c
451         ) &&
452
453         check_bookmark hgrepo feature-c feature-c
454 '
455
456 # cleanup previous stuff
457 rm -rf hgrepo
458
459 test_expect_success 'fetch special filenames' '
460         test_when_finished "rm -rf hgrepo gitrepo && LC_ALL=C" &&
461
462         LC_ALL=en_US.UTF-8
463         export LC_ALL
464
465         (
466         hg init hgrepo &&
467         cd hgrepo &&
468
469         echo test >> "æ rø" &&
470         hg add "æ rø" &&
471         echo test >> "ø~?" &&
472         hg add "ø~?" &&
473         hg commit -m add-utf-8 &&
474         echo test >> "æ rø" &&
475         hg commit -m test-utf-8 &&
476         hg rm "ø~?" &&
477         hg mv "æ rø" "ø~?" &&
478         hg commit -m hg-mv-utf-8
479         ) &&
480
481         (
482         git clone "hg::hgrepo" gitrepo &&
483         cd gitrepo &&
484         git -c core.quotepath=false ls-files > ../actual
485         ) &&
486         echo "ø~?" > expected &&
487         test_cmp expected actual
488 '
489
490 test_expect_success 'push special filenames' '
491         test_when_finished "rm -rf hgrepo gitrepo && LC_ALL=C" &&
492
493         mkdir -p tmp && cd tmp &&
494
495         LC_ALL=en_US.UTF-8
496         export LC_ALL
497
498         (
499         hg init hgrepo &&
500         cd hgrepo &&
501
502         echo one >> content &&
503         hg add content &&
504         hg commit -m one
505         ) &&
506
507         (
508         git clone "hg::hgrepo" gitrepo &&
509         cd gitrepo &&
510
511         echo test >> "æ rø" &&
512         git add "æ rø" &&
513         git commit -m utf-8 &&
514
515         git push
516         ) &&
517
518         (cd hgrepo &&
519         hg update &&
520         hg manifest > ../actual
521         ) &&
522
523         printf "content\næ rø\n" > expected &&
524         test_cmp expected actual
525 '
526
527 setup_big_push () {
528         (
529         hg init hgrepo &&
530         cd hgrepo &&
531         echo zero >content &&
532         hg add content &&
533         hg commit -m zero &&
534         hg bookmark bad_bmark1 &&
535         echo one >content &&
536         hg commit -m one &&
537         hg bookmark bad_bmark2 &&
538         hg bookmark good_bmark &&
539         hg bookmark -i good_bmark &&
540         hg -q branch good_branch &&
541         echo "good branch" >content &&
542         hg commit -m "good branch" &&
543         hg -q branch bad_branch &&
544         echo "bad branch" >content &&
545         hg commit -m "bad branch"
546         ) &&
547
548         git clone "hg::hgrepo" gitrepo &&
549
550         (
551         cd gitrepo &&
552         echo two >content &&
553         git commit -q -a -m two &&
554
555         git checkout -q good_bmark &&
556         echo three >content &&
557         git commit -q -a -m three &&
558
559         git checkout -q bad_bmark1 &&
560         git reset --hard HEAD^ &&
561         echo four >content &&
562         git commit -q -a -m four &&
563
564         git checkout -q bad_bmark2 &&
565         git reset --hard HEAD^ &&
566         echo five >content &&
567         git commit -q -a -m five &&
568
569         git checkout -q -b new_bmark master &&
570         echo six >content &&
571         git commit -q -a -m six &&
572
573         git checkout -q branches/good_branch &&
574         echo seven >content &&
575         git commit -q -a -m seven &&
576         echo eight >content &&
577         git commit -q -a -m eight &&
578
579         git checkout -q branches/bad_branch &&
580         git reset --hard HEAD^ &&
581         echo nine >content &&
582         git commit -q -a -m nine &&
583
584         git checkout -q -b branches/new_branch master &&
585         echo ten >content &&
586         git commit -q -a -m ten
587         )
588 }
589
590 test_expect_success 'remote big push' '
591         test_when_finished "rm -rf hgrepo gitrepo*" &&
592
593         setup_big_push
594
595         (
596         cd gitrepo &&
597
598         check_push 1 --all <<-\EOF
599         master
600         good_bmark
601         branches/good_branch
602         new_bmark:new
603         branches/new_branch:new
604         bad_bmark1:non-fast-forward
605         bad_bmark2:non-fast-forward
606         branches/bad_branch:non-fast-forward
607         EOF
608         ) &&
609
610         check_branch hgrepo default one &&
611         check_branch hgrepo good_branch "good branch" &&
612         check_branch hgrepo bad_branch "bad branch" &&
613         check_branch hgrepo new_branch '' &&
614         check_bookmark hgrepo good_bmark one &&
615         check_bookmark hgrepo bad_bmark1 one &&
616         check_bookmark hgrepo bad_bmark2 one &&
617         check_bookmark hgrepo new_bmark ''
618 '
619
620 test_expect_success 'remote big push fetch first' '
621         test_when_finished "rm -rf hgrepo gitrepo*" &&
622
623         (
624         hg init hgrepo &&
625         cd hgrepo &&
626         echo zero >content &&
627         hg add content &&
628         hg commit -m zero &&
629         hg bookmark bad_bmark &&
630         hg bookmark good_bmark &&
631         hg bookmark -i good_bmark &&
632         hg -q branch good_branch &&
633         echo "good branch" >content &&
634         hg commit -m "good branch" &&
635         hg -q branch bad_branch &&
636         echo "bad branch" >content &&
637         hg commit -m "bad branch"
638         ) &&
639
640         git clone "hg::hgrepo" gitrepo &&
641
642         (
643         cd hgrepo &&
644         hg bookmark -f bad_bmark &&
645         echo update_bmark >content &&
646         hg commit -m "update bmark"
647         ) &&
648
649         (
650         cd gitrepo &&
651         echo two >content &&
652         git commit -q -a -m two &&
653
654         git checkout -q good_bmark &&
655         echo three >content &&
656         git commit -q -a -m three &&
657
658         git checkout -q bad_bmark &&
659         echo four >content &&
660         git commit -q -a -m four &&
661
662         git checkout -q branches/bad_branch &&
663         echo five >content &&
664         git commit -q -a -m five &&
665
666         check_push 1 --all <<-\EOF &&
667         master
668         good_bmark
669         bad_bmark:fetch-first
670         branches/bad_branch:festch-first
671         EOF
672
673         git fetch &&
674
675         check_push 1 --all <<-\EOF
676         master
677         good_bmark
678         bad_bmark:non-fast-forward
679         branches/bad_branch:non-fast-forward
680         EOF
681         )
682 '
683
684 test_expect_failure 'remote big push force' '
685         test_when_finished "rm -rf hgrepo gitrepo*" &&
686
687         setup_big_push
688
689         (
690         cd gitrepo &&
691
692         check_push 0 --force --all <<-\EOF
693         master
694         good_bmark
695         branches/good_branch
696         new_bmark:new
697         branches/new_branch:new
698         bad_bmark1:forced-update
699         bad_bmark2:forced-update
700         branches/bad_branch:forced-update
701         EOF
702         ) &&
703
704         check_branch hgrepo default six &&
705         check_branch hgrepo good_branch eight &&
706         check_branch hgrepo bad_branch nine &&
707         check_branch hgrepo new_branch ten &&
708         check_bookmark hgrepo good_bmark three &&
709         check_bookmark hgrepo bad_bmark1 four &&
710         check_bookmark hgrepo bad_bmark2 five &&
711         check_bookmark hgrepo new_bmark six
712 '
713
714 test_expect_failure 'remote big push dry-run' '
715         test_when_finished "rm -rf hgrepo gitrepo*" &&
716
717         setup_big_push
718
719         (
720         cd gitrepo &&
721
722         check_push 1 --dry-run --all <<-\EOF &&
723         master
724         good_bmark
725         branches/good_branch
726         new_bmark:new
727         branches/new_branch:new
728         bad_bmark1:non-fast-forward
729         bad_bmark2:non-fast-forward
730         branches/bad_branch:non-fast-forward
731         EOF
732
733         check_push 0 --dry-run master good_bmark new_bmark branches/good_branch branches/new_branch <<-\EOF
734         master
735         good_bmark
736         branches/good_branch
737         new_bmark:new
738         branches/new_branch:new
739         EOF
740         ) &&
741
742         check_branch hgrepo default one &&
743         check_branch hgrepo good_branch "good branch" &&
744         check_branch hgrepo bad_branch "bad branch" &&
745         check_branch hgrepo new_branch '' &&
746         check_bookmark hgrepo good_bmark one &&
747         check_bookmark hgrepo bad_bmark1 one &&
748         check_bookmark hgrepo bad_bmark2 one &&
749         check_bookmark hgrepo new_bmark ''
750 '
751
752 test_expect_success 'remote double failed push' '
753         test_when_finished "rm -rf hgrepo gitrepo*" &&
754
755         (
756         hg init hgrepo &&
757         cd hgrepo &&
758         echo zero >content &&
759         hg add content &&
760         hg commit -m zero &&
761         echo one >content &&
762         hg commit -m one
763         ) &&
764
765         (
766         git clone "hg::hgrepo" gitrepo &&
767         cd gitrepo &&
768         git reset --hard HEAD^ &&
769         echo two >content &&
770         git commit -a -m two &&
771         test_expect_code 1 git push &&
772         test_expect_code 1 git push
773         )
774 '
775
776 test_done