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