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
9test_description='Test remote-hg'
10
11. ./test-lib.sh
12
13if ! test_have_prereq PYTHON; then
14 skip_all='skipping remote-hg tests; python not available'
15 test_done
16fi
17
18if ! python -c 'import mercurial'; then
19 skip_all='skipping remote-hg tests; mercurial not available'
20 test_done
21fi
22
23check () {
24 echo $3 > expected &&
25 git --git-dir=$1/.git log --format='%s' -1 $2 > actual
26 test_cmp expected actual
27}
28
29check_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
37 fi
38}
39
40check_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
48 fi
49}
50
51check_push () {
52 local expected_ret=$1 ret=0 ref_ret=0 IFS=':'
53
54 shift
55 git push origin "$@" 2> error
56 ret=$?
57 cat error
58
59 while read branch kind
60 do
61 case "$kind" in
62 'new')
63 grep "^ \* \[new branch\] *${branch} -> ${branch}$" error || ref_ret=1
64 ;;
65 'non-fast-forward')
66 grep "^ ! \[rejected\] *${branch} -> ${branch} (non-fast-forward)$" error || ref_ret=1
67 ;;
68 'fetch-first')
69 grep "^ ! \[rejected\] *${branch} -> ${branch} (fetch first)$" error || ref_ret=1
70 ;;
71 '')
72 grep "^ [a-f0-9]*\.\.[a-f0-9]* *${branch} -> ${branch}$" error || ref_ret=1
73 ;;
74 esac
75 let 'ref_ret' && echo "match for '$branch' failed" && break
76 done
77
78 if let 'expected_ret != ret || ref_ret'
79 then
80 return 1
81 fi
82
83 return 0
84}
85
86setup () {
87 (
88 echo "[ui]"
89 echo "username = H G Wells <wells@example.com>"
90 echo "[extensions]"
91 echo "mq ="
92 ) >> "$HOME"/.hgrc &&
93
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
97}
98
99setup
100
101test_expect_success 'cloning' '
102 test_when_finished "rm -rf gitrepo*" &&
103
104 (
105 hg init hgrepo &&
106 cd hgrepo &&
107 echo zero > content &&
108 hg add content &&
109 hg commit -m zero
110 ) &&
111
112 git clone "hg::hgrepo" gitrepo &&
113 check gitrepo HEAD zero
114'
115
116test_expect_success 'cloning with branches' '
117 test_when_finished "rm -rf gitrepo*" &&
118
119 (
120 cd hgrepo &&
121 hg branch next &&
122 echo next > content &&
123 hg commit -m next
124 ) &&
125
126 git clone "hg::hgrepo" gitrepo &&
127 check gitrepo origin/branches/next next
128'
129
130test_expect_success 'cloning with bookmarks' '
131 test_when_finished "rm -rf gitrepo*" &&
132
133 (
134 cd hgrepo &&
135 hg checkout default &&
136 hg bookmark feature-a &&
137 echo feature-a > content &&
138 hg commit -m feature-a
139 ) &&
140
141 git clone "hg::hgrepo" gitrepo &&
142 check gitrepo origin/feature-a feature-a
143'
144
145test_expect_success 'update bookmark' '
146 test_when_finished "rm -rf gitrepo*" &&
147
148 (
149 cd hgrepo &&
150 hg bookmark devel
151 ) &&
152
153 (
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 ) &&
161
162 check_bookmark hgrepo devel devel
163'
164
165test_expect_success 'new bookmark' '
166 test_when_finished "rm -rf gitrepo*" &&
167
168 (
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 ) &&
176
177 check_bookmark hgrepo feature-b feature-b
178'
179
180# cleanup previous stuff
181rm -rf hgrepo
182
183author_test () {
184 echo $1 >> content &&
185 hg commit -u "$2" -m "add $1" &&
186 echo "$3" >> ../expected
187}
188
189test_expect_success 'authors' '
190 test_when_finished "rm -rf hgrepo gitrepo" &&
191
192 (
193 hg init hgrepo &&
194 cd hgrepo &&
195
196 touch content &&
197 hg add content &&
198
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 ) &&
212
213 git clone "hg::hgrepo" gitrepo &&
214 git --git-dir=gitrepo/.git log --reverse --format="%an <%ae>" > actual &&
215
216 test_cmp expected actual
217'
218
219test_expect_success 'strip' '
220 test_when_finished "rm -rf hgrepo gitrepo" &&
221
222 (
223 hg init hgrepo &&
224 cd hgrepo &&
225
226 echo one >> content &&
227 hg add content &&
228 hg commit -m one &&
229
230 echo two >> content &&
231 hg commit -m two
232 ) &&
233
234 git clone "hg::hgrepo" gitrepo &&
235
236 (
237 cd hgrepo &&
238 hg strip 1 &&
239
240 echo three >> content &&
241 hg commit -m three &&
242
243 echo four >> content &&
244 hg commit -m four
245 ) &&
246
247 (
248 cd gitrepo &&
249 git fetch &&
250 git log --format="%s" origin/master > ../actual
251 ) &&
252
253 hg -R hgrepo log --template "{desc}\n" > expected &&
254 test_cmp actual expected
255'
256
257test_expect_success 'remote push with master bookmark' '
258 test_when_finished "rm -rf hgrepo gitrepo*" &&
259
260 (
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 ) &&
270
271 (
272 git clone "hg::hgrepo" gitrepo &&
273 cd gitrepo &&
274 echo two > content &&
275 git commit -a -m two &&
276 git push
277 ) &&
278
279 check_branch hgrepo default two
280'
281
282cat > expected <<EOF
283changeset: 0:6e2126489d3d
284tag: tip
285user: A U Thor <author@example.com>
286date: Mon Jan 01 00:00:00 2007 +0230
287summary: one
288
289EOF
290
291test_expect_success 'remote push from master branch' '
292 test_when_finished "rm -rf hgrepo gitrepo*" &&
293
294 hg init hgrepo &&
295
296 (
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 ) &&
305
306 hg -R hgrepo log > actual &&
307 cat actual &&
308 test_cmp expected actual &&
309
310 check_branch hgrepo default one
311'
312
313GIT_REMOTE_HG_TEST_REMOTE=1
314export GIT_REMOTE_HG_TEST_REMOTE
315
316test_expect_success 'remote cloning' '
317 test_when_finished "rm -rf gitrepo*" &&
318
319 (
320 hg init hgrepo &&
321 cd hgrepo &&
322 echo zero > content &&
323 hg add content &&
324 hg commit -m zero
325 ) &&
326
327 git clone "hg::hgrepo" gitrepo &&
328 check gitrepo HEAD zero
329'
330
331test_expect_success 'remote update bookmark' '
332 test_when_finished "rm -rf gitrepo*" &&
333
334 (
335 cd hgrepo &&
336 hg bookmark devel
337 ) &&
338
339 (
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 ) &&
347
348 check_bookmark hgrepo devel devel
349'
350
351test_expect_success 'remote new bookmark' '
352 test_when_finished "rm -rf gitrepo*" &&
353
354 (
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 ) &&
362
363 check_bookmark hgrepo feature-b feature-b
364'
365
366test_expect_success 'remote push diverged' '
367 test_when_finished "rm -rf gitrepo*" &&
368
369 git clone "hg::hgrepo" gitrepo &&
370
371 (
372 cd hgrepo &&
373 hg checkout default &&
374 echo bump > content &&
375 hg commit -m bump
376 ) &&
377
378 (
379 cd gitrepo &&
380 echo diverge > content &&
381 git commit -a -m diverged &&
382 check_push 1 <<-EOF
383 master:non-fast-forward
384 EOF
385 ) &&
386
387 check_branch hgrepo default bump
388'
389
390test_expect_success 'remote update bookmark diverge' '
391 test_when_finished "rm -rf gitrepo*" &&
392
393 (
394 cd hgrepo &&
395 hg checkout tip^ &&
396 hg bookmark diverge
397 ) &&
398
399 git clone "hg::hgrepo" gitrepo &&
400
401 (
402 cd hgrepo &&
403 echo "bump bookmark" > content &&
404 hg commit -m "bump bookmark"
405 ) &&
406
407 (
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
414 EOF
415 ) &&
416
417 check_bookmark hgrepo diverge "bump bookmark"
418'
419
420test_expect_success 'remote new bookmark multiple branch head' '
421 test_when_finished "rm -rf gitrepo*" &&
422
423 (
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 ) &&
431
432 check_bookmark hgrepo feature-c feature-c
433'
434
435# cleanup previous stuff
436rm -rf hgrepo
437
438setup_big_push () {
439 (
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 ) &&
458
459 git clone "hg::hgrepo" gitrepo &&
460
461 (
462 cd gitrepo &&
463 echo two > content &&
464 git commit -q -a -m two &&
465
466 git checkout -q good_bmark &&
467 echo three > content &&
468 git commit -q -a -m three &&
469
470 git checkout -q bad_bmark1 &&
471 git reset --hard HEAD^ &&
472 echo four > content &&
473 git commit -q -a -m four &&
474
475 git checkout -q bad_bmark2 &&
476 git reset --hard HEAD^ &&
477 echo five > content &&
478 git commit -q -a -m five &&
479
480 git checkout -q -b new_bmark master &&
481 echo six > content &&
482 git commit -q -a -m six &&
483
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 &&
489
490 git checkout -q branches/bad_branch &&
491 git reset --hard HEAD^ &&
492 echo nine > content &&
493 git commit -q -a -m nine &&
494
495 git checkout -q -b branches/new_branch master &&
496 echo ten > content &&
497 git commit -q -a -m ten
498 )
499}
500
501test_expect_success 'remote big push' '
502 test_when_finished "rm -rf hgrepo gitrepo*" &&
503
504 setup_big_push
505
506 (
507 cd gitrepo &&
508
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
518 EOF
519 ) &&
520
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 ''
529'
530
531test_expect_success 'remote big push fetch first' '
532 test_when_finished "rm -rf hgrepo gitrepo*" &&
533
534 (
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 ) &&
550
551 git clone "hg::hgrepo" gitrepo &&
552
553 (
554 cd hgrepo &&
555 hg bookmark -f bad_bmark &&
556 echo update_bmark > content &&
557 hg commit -m "update bmark"
558 ) &&
559
560 (
561 cd gitrepo &&
562 echo two > content &&
563 git commit -q -a -m two &&
564
565 git checkout -q good_bmark &&
566 echo three > content &&
567 git commit -q -a -m three &&
568
569 git checkout -q bad_bmark &&
570 echo four > content &&
571 git commit -q -a -m four &&
572
573 git checkout -q branches/bad_branch &&
574 echo five > content &&
575 git commit -q -a -m five &&
576
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
584 EOF
585
586 git fetch &&
587
588 check_push 1 --all <<-EOF
589 master
590 good_bmark
591 bad_bmark:non-fast-forward
592 branches/bad_branch:non-fast-forward
593 EOF
594 )
595'
596
597test_expect_failure 'remote big push dry-run' '
598 test_when_finished "rm -rf hgrepo gitrepo*" &&
599
600 setup_big_push
601
602 (
603 cd gitrepo &&
604
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
614 EOF
615
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
622 EOF
623 ) &&
624
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 ''
633'
634
635test_expect_success 'remote double failed push' '
636 test_when_finished "rm -rf hgrepo gitrepo*" &&
637
638 (
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 ) &&
647
648 (
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
656 )
657'
658
659test_done