chiark / gitweb /
Merge branch 'stable' into HEAD
[dgit.git] / tests / lib
1 #
2
3 exec 2>&1
4 set -x
5 set -o pipefail
6
7 . tests/lib-core
8 . tests/lib-restricts
9
10 t-report-failure () {
11         set +x
12         rc=$1
13         cat <<END >&2
14 TEST FAILED
15 cwd: $PWD
16 funcs: ${FUNCNAME[*]}
17 lines: ${BASH_LINENO[*]}
18 files: ${BASH_SOURCE[*]}
19 END
20         exit 16
21 }
22
23 trap 'test $? = 0 || t-report-failure' EXIT
24
25 t-filter-out-git-hyphen-dir
26
27 t-set-intree
28
29 : ${DGIT_TEST_DEBUG=-D}
30 export DGIT_TEST_DEBUG
31
32 : ${DGIT_TEST_DISTRO+ ${distro=${DGIT_TEST_DISTRO}}}
33
34 export GIT_COMMITTER_DATE='1440253867 +0100'
35 export GIT_AUTHOR_DATE='1440253867 +0100'
36
37 root=`pwd`
38 troot=$root/tests
39 testname="${DGIT_TEST_TESTNAME-${0##*/}}"
40
41 tmp=$ADTTMP
42 if [ x"$tmp" = x ]; then
43         mkdir -p tests/tmp
44         tmpbase=$troot/tmp
45         tmp=tests/tmp/$testname
46         rm -rf $tmp
47         mkdir $tmp
48 elif [ "x$DGIT_TEST_TMPBASE" != x ]; then
49         tmpbase="$DGIT_TEST_TMPBASE"
50 fi
51 cd $tmp
52
53 tmp=`pwd`
54
55 t-set-using-tmp
56
57 test -f $tmp/.save-env || \
58 env -0 >$tmp/.save-env
59
60 ln -f $troot/ssh ssh
61
62 export DEBCHANGE_VENDOR=dpkg
63
64 mkdir -p $tmp/incoming
65 cat <<END >$tmp/dput.cf
66 [test-dummy]
67 method                  = local
68 incoming                = $tmp/incoming
69 run_dinstall            = 0
70 END
71
72 : ${t_archive_method:=aq}
73 : ${tagpfx:=archive/test-dummy}
74 : ${suitespecs:=sid:unstable}
75
76 t-git-next-date () {
77         GIT_COMMITTER_DATE="$(( ${GIT_COMMITTER_DATE%% *} + 1 )) ${GIT_COMMITTER_DATE#* }"
78         GIT_AUTHOR_DATE="$GIT_COMMITTER_DATE"
79 }
80
81 t-expect-fail () {
82         local mpat="$1"; shift
83
84         set +o pipefail
85         LC_MESSAGES=C "$@" 2>&1 | tee $tmp/t.output
86         local ps="${PIPESTATUS[*]}"
87         set -o pipefail
88
89         case $ps in
90         "0 0")  fail "command unexpectedly succeeded (instead of: $mpat)" ;;
91         *" 0")  ;;
92         *)      fail "tee failed"  ;;
93         esac
94
95         t-grep-mpat "$mpat" $tmp/t.output
96 }
97
98 t-grep-mpat () {
99         local mpat="$1"
100         local file="$2"
101
102         local grepper=fgrep
103         case "$mpat" in
104         [A-Z]:*)
105                 case "$mpat" in
106                 E:*)    grepper=egrep   ;;
107                 F:*)    grepper=fgrep   ;;
108                 *)      fail "bad mpat prefix in $mpat";;
109                 esac
110                 mpat=${mpat#[A-Z]:}
111                 ;;
112         esac
113
114         $grepper -e "$mpat" "$file" ||
115                 fail "message not found"
116 }
117
118 t-expect-push-fail () {
119         local mpat="$1"; shift
120
121         local triedpush; triedpush=`git rev-parse HEAD`
122
123         t-reporefs pre-push
124         t-expect-fail "$mpat"  "$@"
125         t-reporefs post-push
126         diff $tmp/show-refs.{pre,post}-push
127
128         t-git-objects-not-present '' $triedpush
129
130         eval "$t_expect_push_fail_hook"
131 }
132
133 t-git-objects-not-present () {
134         # t-git-objects-not-present GITDIR|'' OBJID [...]
135         # specifying '' means the repo for package $p
136         local gitdir="${1-$dgitrepo}"
137         local obj
138         if ! [ -e "$gitdir" ]; then return; fi
139         for obj in "$@"; do
140                 GIT_DIR=$gitdir \
141                 t-expect-fail 'unable to find' \
142                 git cat-file -t $obj
143         done
144 }
145
146 t-reporefs () {
147         local whichoutput=$1; shift
148         local whichrepo=${1-$dgitrepo}
149         local outputfile="$tmp/show-refs.$whichoutput"
150         (set -e
151          exec >"$outputfile"
152          if test -d $whichrepo; then
153                 cd $whichrepo
154                 git show-ref |sort
155         fi)
156 }
157
158 t-untar () {
159         local tarfile=$1.tar
160         local edittree=$1.edit
161         if test -d "$edittree"; then
162                 cp -a "$edittree"/* .
163         else
164                 tar xf "$tarfile"
165         fi
166 }
167
168 t-worktree () {
169         rm -rf $p
170         t-untar $troot/worktrees/${p}_$1
171 }
172
173 t-select-package () {
174         p=$1
175         dgitrepo=$tmp/git/$p.git
176 }
177
178 t-git () {
179         t-select-package $1
180         v=$2
181         mkdir -p $tmp/git
182         local gs=$troot/git-srcs/${p}_$v.git
183         (set -e; cd $tmp/git; t-untar $gs)
184 }
185
186 t-git-none () {
187         mkdir -p $tmp/git
188         (set -e; cd $tmp/git; tar xf $troot/git-template.tar)
189 }
190
191 t-git-merge-base () {
192         git merge-base $1 $2 || test $? = 1
193 }
194
195 t-has-ancestor () {
196         # t-has-ancestor ANCESTOR
197         # (CHILD is implicit, HEAD)
198         local now;      now=`git rev-parse HEAD`
199         local ancestor; ancestor=`git rev-parse $1^{}`
200         local mbase;    mbase=`t-git-merge-base $ancestor $now`
201         if [ x$mbase != x$ancestor ]; then
202                 fail "not ff $ancestor..$now, $mbase != $ancestor"
203         fi
204 }
205
206 t-has-parent-or-is () {
207         # t-has-parent-or-is CHILD PARENT
208         local child=$1
209         local parent=$2
210         local parents
211         parents=$(git show --pretty=format:' %P %H ' "$child")
212         parent=$(git rev-parse "$parent~0")
213         case "$parents" in
214         *" $parent "*)  ;;
215         *)      fail "child $child lacks parent $parent" ;;
216         esac
217 }
218
219 t-prep-newpackage () {
220         t-select-package $1
221         v=$2
222         t-archive-none $p
223         t-git-none
224         t-worktree $v
225         cd $p
226         if ! git show-ref --verify --quiet refs/heads/master; then
227                 git branch -m dgit/sid master
228                 git remote rm dgit
229         fi
230         cd ..
231 }
232
233 t-archive-none () {
234         t-select-package $1
235         t-archive-none-$t_archive_method
236 }
237 t-archive-none-aq () {
238         mkdir -p $tmp/aq/dsc_in_suite $tmp/mirror/pool/main
239
240         : >$tmp/aq/suites
241         local jsondelim="["
242
243         local suitespec
244         for suitespec in $suitespecs; do
245                 local suite=${suitespec%%:*}
246                 local sname=${suitespec#*:}
247
248                 >$tmp/aq/package.$suite.$p
249                 t-aq-archive-updated $suite $p
250
251                 >$tmp/aq/package.new.$p
252                 t-aq-archive-updated new $p
253
254                 ln -sf $suite $tmp/aq/dsc_in_suite/$sname
255
256                 cat <<END >>$tmp/aq/suites
257 $jsondelim
258    {
259       "archive" : "ftp-master",
260       "codename" : "$suite",
261       "components" : [
262          "main",
263          "contrib",
264          "non-free"
265       ],
266       "name" : "$sname",
267       "dakname" : "$sname"
268 END
269
270                 jsondelim="   },"
271
272         done
273         cat <<END >>$tmp/aq/suites
274     }
275 ]
276 END
277 }
278
279 t-aq-archive-updated () {
280         local suite=$1
281         local p=$2
282         local suitedir=$tmp/aq/dsc_in_suite/$suite
283         mkdir -p $suitedir
284         perl <$tmp/aq/package.$suite.$p >$suitedir/$p -wne '
285                 use JSON;
286                 use strict;
287                 our @v;
288                 m{^(\S+) (\w+) ([^ \t/]+)/(\S+)} or die;
289                 push @v, {
290                         "version" => "$1",
291                         "sha256sum" => "$2",
292                         "component" => "$3",
293                         "filename" => "$4",
294                 };
295                 END {
296                         my $json = JSON->new->canonical();
297                         print $json->encode(\@v) or die $!;
298                 }
299         '
300 }
301
302 t-archive-process-incoming () {
303         local suite=$1
304         mv $tmp/incoming/${p}_* $tmp/mirror/pool/main/
305         t-archive-query "$suite"
306 }
307
308 t-archive-query () {
309         local suite=${1-sid}
310         local dscf=main/${p}_${v}.dsc
311         t-archive-query-$t_archive_method "$suite" "$p" "$v" "$dscf"
312 }
313 t-archive-query-aq () {
314         local suite=$1
315         local p=$2
316         local v=$3
317         local dscf=$4
318         local sha; sha=`sha256sum <$tmp/mirror/pool/$dscf`
319         echo "${v} ${sha%  -} $dscf" >>$tmp/aq/package.$suite.${p}
320         t-aq-archive-updated $suite $p
321 }
322
323 t-archive () {
324         t-archive-none $1
325         v=$2
326         local dscf=${p}_$2.dsc
327         rm -f $tmp/mirror/pool/main/${p}_*
328         ln $troot/pkg-srcs/${p}_${2%-*}* $tmp/mirror/pool/main/
329         t-archive-query $suite
330         rm -rf $tmp/extract
331         mkdir $tmp/extract
332         (set -e; cd $tmp/extract; dpkg-source -x ../mirror/pool/main/$dscf)
333 }
334
335 t-git-dir-time-passes () {
336         touch -d 'last year' $dgitrepo
337 }
338
339 t-git-dir-check () {
340         local gitdir=$dgitrepo
341         case "$1" in
342         enoent)
343                 if test -e "$gitdir"; then fail "$gitdir exists"; fi
344                 return
345                 ;;
346         public) wantstat='7[75]5' ;;
347         secret) wantstat='7[70]0' ;;
348         *)      fail "$1 t-git-dir-check ?" ;;
349         esac
350         gotstat=`stat -c%a $gitdir`
351         case "$gotstat" in
352         *$wantstat) return ;;
353         *)      fail "$gitdir has mode $gotstat, expected $wantstat" ;;
354         esac
355 }
356
357 t-expect-fsck-fail () {
358         echo >>$tmp/fsck.expected-errors "$1"
359 }
360
361 t-git-fsck () {
362         set +e
363         LC_MESSAGES=C git fsck --no-dangling --strict 2>&1 \
364                 | tee dgit-test-fsck.errs
365         ps="${PIPESTATUS[*]}"
366         set -e
367
368         local pats
369         if [ -f $tmp/fsck.expected-errors ]; then
370                 pats=(-w -f $tmp/fsck.expected-errors)
371         else
372                 test "$ps" = "0 0"
373         fi
374         pats+=(-e 'notice: HEAD points to an unborn branch')
375         pats+=(-e 'notice: No default references')
376
377         set +e
378         grep -v "${pats[@]}" dgit-test-fsck.errs
379         rc=$?
380         set -e
381         case $rc in
382         1) ;; # no unexpected errors
383         0) fail "unexpected messages from git-fsck" ;;
384         *) fail "grep of git-fsck failed" ;;
385         esac
386 }
387
388 t-fscks () {
389         (
390         shopt -s nullglob
391         for d in $tmp/*/.git $tmp/git/*.git; do
392                 cd "$d"
393                 t-git-fsck
394         done
395         )
396 }
397
398 t-ok () {
399         : '========================================'
400         t-fscks
401         echo ok.
402 }
403
404 t-rm-dput-dropping () {
405         rm -f $tmp/${p}_${v}_*.upload
406 }
407
408 t-dgit () {
409         local dgit=${DGIT_TEST-dgit}
410         pwd
411         : '
412 {{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{'
413         $dgit --dgit=$dgit --dget:-u --dput:--config=$tmp/dput.cf \
414  ${dgit_config_debian_alias-"--config-lookup-explode=dgit-distro.debian.alias-canon"} \
415                 ${distro+${distro:+-d}}${distro--dtest-dummy} \
416                 $DGIT_TEST_OPTS $DGIT_TEST_DEBUG \
417                 -k39B13D8A $t_dgit_xopts "$@"
418         : '}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
419 '
420 }
421
422 t-diff-nogit () {
423         diff --exclude=.git --exclude=.pc -ruN $*
424 }
425
426 t-files-notexist () {
427         local f
428         for f in "$@"; do
429                 if [ -e $f ]; then
430                         fail "$f exists!"
431                 fi
432         done
433 }
434
435 t-cloned-fetched-good () {
436         t-diff-nogit ../extract/$p-${v%-*} .
437         t-clean-on-branch dgit/sid
438         t-refs-same-start
439         t-refs-same \
440                 refs/heads/dgit/sid \
441                 refs/remotes/dgit/dgit/sid
442         t-refs-notexist refs/dgit/unstable refs/remotes/dgit/dgit/unstable
443 }
444
445 t-output () {
446         printf "%s${1:+\n}" "$1" >$tmp/t.want
447         shift
448         "$@" >$tmp/t.got
449         diff $tmp/t.want $tmp/t.got
450 }
451
452 t-clean-on-branch () {
453         t-output "## $1" git status -b --porcelain
454 }
455
456 t-setup-done () {
457         local savevars=$1
458         local savedirs=$2
459         local importeval=$3
460
461         local import=IMPORT.${0##*/}
462         exec 4>$tmp/$import.new
463
464         local vn
465         for vn in $savevars; do
466                 perl >&4 -I. -MDebian::Dgit -e '
467                         printf "%s=%s\n", $ARGV[0], shellquote $ARGV[1]
468                 ' $vn "$(eval "printf '%s\n' \"\$$vn\"")"
469         done
470
471         (set -e; cd $tmp; tar cf $import.tar $savedirs)
472
473         printf >&4 "\n%s\n" "$importeval"
474
475         mv -f $tmp/$import.new $tmp/$import
476 }
477
478 t-setup-import () {
479         local setupname=$1
480
481         local setupsrc
482         local lock
483         if [ "x$tmpbase" = x ]; then
484                 # ADTTMP was set on entry to tests/lib, so we
485                 # are not sharing tmp area between tests
486                 setupsrc="$tmp"
487                 lock="$tmp/.dummy.lock"
488         else
489                 setupsrc="$tmpbase/$setupname"
490                 lock="$setupsrc.lock"
491         fi
492
493         local simport="$setupsrc/IMPORT.$setupname"
494
495         if ! [ -e "$simport" ]; then
496                 with-lock-ex -w "$lock" \
497                 xargs -0 -a $tmp/.save-env \
498                 bash -xec '
499                         cd "$1"; shift
500                         setupname="$1"; shift
501                         simport="$1"; shift
502                         if [ -e "$simport" ]; then exit 0; fi
503                         env - "$@" \
504                         "tests/setup/$setupname"
505                 ' x "$root" "$setupname" "$simport"
506         fi
507
508         if [ "x$setupsrc" != "x$tmp" ]; then
509                 (set -e; cd $tmp; tar xf "$simport.tar")
510         fi
511
512         . "$simport"
513 }
514
515 t-git-get-ref-exact () {
516         local ref=$1
517         # does not dereference, unlike t-git-get-ref
518         case "$ref" in
519         refs/*) ;;
520         *) fail "t-git-get-ref-exact bad $ref" ;;
521         esac
522         git for-each-ref --format='%(objectname)' "[r]efs/${ref#refs/}"
523 }
524
525 t-git-get-ref () {
526         local ref=$1
527         case "$ref" in
528         refs/*) ;;
529         *) fail "t-git-get-ref bad $ref" ;;
530         esac
531         (git show-ref -d $1 || test $? = 1) | perl -ne '
532                 $x = $1 if m#^(\w+) \Q'$1'\E(?:\^\{\})?$#;
533                 END { print "$x\n" if length $x; }
534         '
535 }
536
537 t-ref-same-exact () {
538         local name="$1"
539         local val; val=`t-git-get-ref-exact $name`
540         t-ref-same-val "$name" $val
541 }
542
543 t-ref-same () {
544         local name="$1"
545         local val; val=`t-git-get-ref $name`
546         t-ref-same-val "$name" $val
547 }
548
549 t-ref-head () {
550         local val; val=`git rev-parse HEAD`
551         t-ref-same-val HEAD $val
552 }
553
554 t-ref-same-val () {
555         local name="$1"
556         local val=$2
557         case "${t_ref_val-unset}" in
558         unset)          ;;
559         "$val")         ;;
560         *)              fail "ref varies: ($name)\
561  ${val:-nothing} != ${t_ref_val:-nothing} (${t_ref_names[*]})" ;;
562         esac
563         t_ref_val="$val"
564         t_ref_names+=("$name")
565 }
566
567 t-refs-same-start () {
568         unset t_ref_val
569         t_ref_names=()
570 }
571
572 t-refs-same () {
573         local g
574         for g in $*; do
575                 t-ref-same $g
576         done
577 }
578
579 t-refs-notexist () {
580         local val
581         for g in $*; do
582                 val=`t-git-get-ref $g`
583                 if [ "x$val" != x ]; then
584                         fail "ref $g unexpectedly exists ($val)"
585                 fi
586         done
587 }
588
589 t-v-tag () {
590         echo refs/tags/$tagpfx/${v//\~/_}
591 }
592
593 t-format-ref () {
594         git log -n1 --pretty=format:"$1" "$2"
595 }
596
597 t-sametree-parent () {
598         local ref=$1
599         local parent
600         local ctree; ctree=$(t-format-ref '%T' "$ref")
601         while :; do
602                 local psame=''
603                 for parent in $(t-format-ref '%P' "$ref"); do
604                         local ptree; ptree=$(t-format-ref '%T' "$parent")
605                         if [ "x$ptree" = "x$ctree" ]; then
606                                 psame+=" $parent"
607                         fi
608                 done
609                 case "$psame" in ""|" * *") break ;; esac
610                 ref="${psame# }"
611         done
612         echo "$ref"
613 }
614
615 t-check-pushed-master () {
616         local master; master=`t-git-get-ref refs/heads/master`
617         if [ x$master = x$t_ref_val ]; then return; fi
618         if [ x$master = x ]; then fail "failed to push master"; fi
619         # didn't update master, it must be not FF
620         local mbase; mbase=`t-git-merge-base $master $t_ref_val`
621         if [ x$mbase = x$master ]; then fail "failed to ff master"; fi
622 }
623
624 t-pushed-good () {
625         local branch=$1
626         local suite=${2:-sid}
627         t-refs-same \
628                 refs/heads/$branch
629         t-pushed-good-core
630 }
631         
632 t-pushed-good-core () {
633         t-ref-dsc-dgit
634         t-refs-same \
635                 `t-v-tag` \
636                 refs/remotes/dgit/dgit/$suite
637         t-refs-notexist \
638                 refs/heads/dgit/unstable \
639                 refs/remotes/dgit/dgit/unstable
640         (set -e; cd $dgitrepo
641          t-refs-same \
642                 refs/dgit/$suite \
643                 `t-v-tag`
644          ${t_check_pushed_master:- : NOT-DRS-NO-CHECK-PUSHED-MASTER}
645          t-refs-notexist \
646                 refs/dgit/unstable
647         )
648         git verify-tag `t-v-tag`
649 }
650
651 t-splitbrain-pushed-good--unpack () {
652         cd $tmp
653         rm -rf t-unpack
654         mkdir t-unpack
655         cd t-unpack
656         ln -s $tmp/mirror/pool/main/*.orig*.tar* .
657         ln -s $tmp/incoming/*.orig*.tar* . ||:
658         ln -s $incoming_dsc .
659         ln -s ${incoming_dsc/.dsc/.debian.tar}* .
660         dpkg-source "$@" -x *.dsc
661         cd */.
662         git init
663         git fetch ../../$p "refs/tags/*:refs/tags/*"
664 }
665
666 t-splitbrain-pushed-good--checkprep () {
667         git add -Af .
668         git rm --cached -r --ignore-unmatch .pc
669 }
670
671 t-splitbrain-pushed-good--checkdiff () {
672         local tag=$1
673         t-splitbrain-pushed-good--checkprep
674         t-output "" git diff --stat --cached $tag
675 }
676
677 t-splitbrain-pushed-good-start () {
678         dep14tag=refs/tags/test-dummy/${v//\~/_}
679         dgittag=$(t-v-tag)
680         t-output "" git status --porcelain
681         t-ref-head
682         t-refs-same $dep14tag
683         (set -e; cd $dgitrepo; t-refs-same $dep14tag)
684         git merge-base --is-ancestor $dep14tag $dgittag
685
686         t-refs-same-start
687         t-ref-same refs/heads/split.p
688         case "$(t-git-get-ref refs/heads/split.b)" in
689         "$t_ref_val") ;;
690         "$(git rev-parse refs/heads/split.p^0)") ;;
691         "$(git rev-parse refs/heads/split.p^1)") ;;
692         *) fail 'bad b/p' ;;
693         esac
694         t-pushed-good-core
695
696         t-incoming-dsc
697
698         t-splitbrain-pushed-good--unpack
699         t-splitbrain-pushed-good--checkdiff $dgittag
700 }
701 t-splitbrain-pushed-good-end-made-dep14 () {
702         t-splitbrain-pushed-good--checkdiff $dep14tag
703         cd $tmp/$p
704 }
705
706 t-splitbrain-rm-gitignore-patch () {
707         perl -i -pe '
708                 next unless $_ eq "auto-gitignore\n";
709                 die if $counter++;
710                 chomp;
711                 rename "debian/patches/$_", "../t-auto-gitignore" or die $!;
712                 $_ = "";
713         ' debian/patches/series
714 }
715
716 t-gbp-pushed-good () {
717         local suite=${1:-sid}
718         t-splitbrain-pushed-good-start
719
720         # Right, now we want to check that the maintainer tree and
721         # the dgit tree differ in exactly the ways we expect.  We
722         # achieve this by trying to reconstruct the maintainer tree
723         # from the dgit tree.
724
725         # So, unpack it withut the patches applied
726         t-splitbrain-pushed-good--unpack --skip-patches
727
728         # dgit might have added a .gitignore patch, which we need to
729         # drop and remove
730         t-splitbrain-rm-gitignore-patch
731
732         # Now the result should differ only in non-debian/ .gitignores
733         t-splitbrain-pushed-good--checkprep
734         git diff --cached --name-only $dep14tag >../changed
735         perl -ne '
736                 next if !m#^debian/# && m#(^|/)\.gitignore#;
737                 die "$_ mismatch";
738         ' <../changed
739
740         # If we actually apply the gitignore patch by hand, it
741         # should be perfect:
742         if [ -f ../t-auto-gitignore ]; then
743                 patch --backup-if-mismatch -p1 -u <../t-auto-gitignore
744         fi
745
746         t-splitbrain-pushed-good-end-made-dep14
747 }
748
749 t-unapplied-pushed-good () {
750         t-splitbrain-pushed-good-start
751         t-splitbrain-pushed-good--unpack --skip-patches
752         t-splitbrain-pushed-good-end-made-dep14
753 }
754
755 t-dpm-pushed-good () {
756         t-splitbrain-pushed-good-start
757         t-splitbrain-pushed-good--unpack
758         t-splitbrain-rm-gitignore-patch
759         t-splitbrain-pushed-good-end-made-dep14
760 }
761
762 t-commit-build-push-expect-log () {
763         local msg=$1
764         local mpat=$2
765         t-commit "$msg"
766         t-dgit build
767         LC_MESSAGES=C \
768         t-dgit push --new 2>&1 |tee $tmp/push.log
769         t-grep-mpat "$mpat" $tmp/push.log
770 }
771
772 t-822-field () {
773         local file=$1
774         local field=$2
775         perl -e '
776                 use Dpkg::Control::Hash;
777                 my $h = new Dpkg::Control::Hash allow_pgp=>1;
778                 $h->parse(\*STDIN,"'"$file"'");
779                 my $val = $h->{"'$field'"},"\n";
780                 die "'"$file $field"'" unless defined $val;
781                 print $val,"\n";
782         ' <$file
783 }
784
785 t-defdistro () {
786         export DGIT_TEST_DISTRO=''
787         distro=''
788         t-git-config dgit-suite.unstable.distro test-dummy
789 }
790
791 t-stunt-envvar () {
792         local var=$1
793         local tstunt=$2
794         eval '
795                 case "$'$var'" in
796                 "$tstunt:"*)    ;;
797                 *":$tstunt:"*)  ;;
798                 "")             '$var'="$tstunt" ;;
799                 *)              '$var'="$tstunt:$'$var'" ;;
800                 esac
801                 export '$var'
802         '
803 }
804
805 t-tstunt--save-real () {
806         local f="$1"
807         case "$f" in
808         */*) return ;;
809         esac
810
811         local rc
812         local real
813         set +e
814         real=$(
815                 p=":$PATH:"
816                 p="${p/:"$tmp/tstunt":/:}"
817                 p="${p%:}"
818                 p="${p#:}"
819                 PATH="$p"
820                 type -p "$f"
821         )
822         rc=$?
823         set -e
824
825         case $rc in
826         1)      return ;;
827         0)      ;;
828         *)      fail "did not find $f on PATH $PATH" ;;
829         esac
830
831         local varname=${f//[^_0-9a-zA-Z]/_}
832         varname=DGIT_TEST_REAL_${varname^^}
833
834         eval "
835                 : \${$varname:=\$real}
836                 export $varname
837         "
838 }
839
840 t-tstunt () {
841         local tstunt=$tmp/tstunt
842         t-stunt-envvar PATH $tstunt
843         t-stunt-envvar PERLLIB $tstunt
844         local f
845         for f in "$@"; do
846                 t-tstunt--save-real $f
847                 f="./$f"
848                 local d="$tstunt/${f%/*}"
849                 mkdir -p $d
850                 ln -sf "$troot/tstunt/$f" "$d"/.
851         done
852 }
853
854 t-tstunt-parsechangelog () {
855         t-tstunt dpkg-parsechangelog Dpkg/Changelog/Parse.pm
856 }
857
858 t-tstunt-lintian () {
859         t-tstunt lintian
860 }
861
862 t-tstunt-debuild () {
863         t-tstunt debuild
864 }
865
866 t-incoming-dsc () {
867         local dsc=${p}_${v}.dsc
868         incoming_dsc=$tmp/incoming/$dsc
869 }
870
871 t-ref-dsc-dgit () {
872         t-incoming-dsc
873         local val; val=`t-822-field $incoming_dsc Dgit`
874         val=$( perl -e '
875                 $_=shift @ARGV;
876                 die "Dgit $_ ?" unless m/^\w+\b/;
877                 print $&,"\n" or die $!;
878         ' "$val")
879         t-ref-same-val $incoming_dsc "$val"
880 }
881
882 t-apply-diff () {
883         local v1=$1
884         local v2=$2
885         (cd $troot/pkg-srcs;
886          debdiff ${p}_${v1}.dsc ${p}_${v2}.dsc || test $? = 1) \
887          | patch -p1 -u
888 }
889
890 t-gbp-unapplied-pq2qc () {
891         # does `gbp pq export'
892         # commits the resulting debian/patches on  qc/BRANCH
893         # leaves us on qc/BRANCH (eg "qc/quilt-tip"))
894         # qc/BRANCH is not fast-forwarding
895
896         gbp pq export
897
898         branch=`git symbolic-ref HEAD`
899         branch=${branch#refs/heads/}
900
901         case "$branch" in
902         */*) fail "unexpected branch $branch" ;;
903         esac
904
905         git branch -f qc/$branch
906         git checkout qc/$branch
907         git add debian/patches
908         git commit -m 'Commit patch queue'
909 }
910
911 t-git-pseudo-merge () {
912         # like   git merge -s ours
913         if [ ! "$git_pseuomerge_opts" ]; then
914                 if git merge --help \
915                  | grep -q allow-unrelated-histories; then
916                         git_pseuomerge_opts='--allow-unrelated-histories'
917                 fi
918                 git_pseuomerge_opts+=' -s ours'
919         fi
920         git merge $git_pseuomerge_opts "$@"
921 }
922
923 t-gbp-example-prep-no-ff () {
924         t-tstunt-parsechangelog
925         t-archive example 1.0-1
926         t-git-none
927         t-worktree 1.0
928
929         cd example
930
931         t-dgit fetch
932
933         git checkout -b patch-queue/quilt-tip-2 patch-queue/quilt-tip
934         gbp pq rebase
935
936         echo '/* some comment */' >>src.c
937         git add src.c
938         git commit -m 'Add a comment to an upstream file'
939
940         t-gbp-unapplied-pq2qc
941
942         t-commit 'some updates' 1.0-2
943 }
944
945 t-gbp-example-prep () {
946         t-gbp-example-prep-no-ff
947
948         t-git-pseudo-merge \
949                 -m 'Pseudo-merge to make descendant of archive' \
950                 remotes/dgit/dgit/sid
951 }
952
953 t-make-badcommit () {
954         badcommit=$(
955                 git cat-file commit HEAD | \
956                 perl -pe 's/^committer /commiter /' | \
957                 git hash-object -w -t commit --stdin
958         )
959         t-expect-fsck-fail $badcommit
960 }
961
962 t-commit () {
963         local msg=$1
964         v=${2:-${majorv:-1}.$revision}
965         dch --force-distribution -v$v --distribution ${3:-unstable} "$1"
966         git add debian/changelog
967         debcommit
968         revision=$(( ${revision-0} + 1 ))
969 }
970
971 t-git-config () {
972         git config --global "$@"
973 }
974
975 t-drs () {
976         export DGIT_TEST_TROOT=$troot
977  t-git-config dgit-distro.test-dummy.git-url "ext::$troot/drs-git-ext %S "
978  t-git-config dgit-distro.test-dummy.git-check true
979  t-git-config dgit-distro.test-dummy.git-create true
980  t-git-config dgit-distro.test-dummy.dgit-tag-format new,old,maint
981         cp $troot/gnupg/{dd.gpg,dm.gpg,dm.txt} $tmp/.
982         cp $troot/suites $tmp/.
983         cp $troot/suites $tmp/suites-master
984
985         export t_check_pushed_master=t-check-pushed-master
986
987         drs_dispatch=$tmp/distro=test-dummy
988         mkdir $drs_dispatch
989
990         if [ "x$DGIT_TEST_INTREE" != x ]; then
991                 ln -sf "$DGIT_TEST_INTREE" $drs_dispatch/dgit-live
992         fi
993
994         ln -sf $tmp/git $drs_dispatch/repos
995         ln -sf $tmp/suites $tmp/suites-master $tmp/dm.txt $drs_dispatch/
996         mkdir -p $drs_dispatch/keyrings
997         ln -sf $tmp/dd.gpg $drs_dispatch/keyrings/debian-keyring.gpg
998         ln -sf $tmp/dm.gpg $drs_dispatch/keyrings/debian-maintainers.gpg
999         ln -sf /bin/true $drs_dispatch/policy-hook
1000 }
1001
1002 t-newtag () {
1003  export tagpfx=archive/test-dummy
1004  t-git-config dgit-distro.test-dummy.dgit-tag-format new,maint
1005 }
1006 t-oldtag () {
1007  export tagpfx=test-dummy
1008  t-git-config dgit-distro.test-dummy.dgit-tag-format old
1009 }
1010
1011 t-dsd () {
1012         t-drs
1013  t-git-config dgit-distro.test-dummy.ssh "$troot/dsd-ssh"
1014  t-git-config dgit-distro.test-dummy.git-check ssh-cmd
1015  t-git-config dgit-distro.test-dummy.git-create true
1016  t-git-config dgit-distro.test-dummy.git-url \
1017                 "ext::$troot/dsd-ssh X %S /dgit/test-dummy/repos"
1018
1019  t-git-config dgit-distro.test-dummy.diverts.drs /drs
1020  t-git-config dgit-distro.test-dummy/drs.ssh "$troot/ssh"
1021  t-git-config dgit-distro.test-dummy/drs.git-url $tmp/git
1022  t-git-config dgit-distro.test-dummy/drs.git-check ssh-cmd
1023  t-git-config dgit-distro.test-dummy/drs.git-create ssh-cmd
1024
1025         echo 'no-such-package* drs' >$drs_dispatch/diverts
1026 }
1027
1028 t-policy-admin () {
1029         : '(((((((((((((((((((((((((((((((((((((((('
1030         ${DGIT_INFRA_PFX}dgit-repos-admin-debian --repos $tmp/git "$@"
1031         : '))))))))))))))))))))))))))))))))))))))))'
1032 }
1033
1034 t-policy-nonexist () {
1035         ln -sf no-such-file-or-directory $drs_dispatch/policy-hook
1036 }
1037
1038 t-make-hook-link () {
1039         local hook=$1 # in infra/
1040         local linkpath=$2
1041         hook=${DGIT_INFRA_PFX}$hook
1042         case $hook in
1043         */*)    ;;
1044         *)      hook=`type -P $hook` ;;
1045         esac
1046         ln -sf "$hook" $linkpath
1047 }
1048
1049 t-policy () {
1050         local policyhook=$1
1051         t-make-hook-link $policyhook $drs_dispatch/policy-hook
1052 }
1053
1054 t-debpolicy () {
1055         t-dsd
1056         t-policy dgit-repos-policy-debian
1057
1058         mkdir $tmp/git
1059         t-policy-admin create-db
1060 }
1061
1062 t-policy-periodic () {
1063         : '(((((((((((((((((((((((((((((((((((((((('
1064         ${DGIT_REPOS_SERVER_TEST-dgit-repos-server} \
1065                 test-dummy $drs_dispatch '' --cron
1066         : '))))))))))))))))))))))))))))))))))))))))'
1067 }
1068
1069 t-restrict () {
1070         local restriction=$1
1071         (cd $root; t-restriction-$restriction >&2)
1072 }
1073
1074 t-dependencies () {
1075         : "Hopefully installed: $*"
1076 }
1077
1078 t-chain-test () {
1079         local ct=$1
1080         local d=${0%/*}
1081         cd $root
1082         export DGIT_TEST_TESTNAME="$testname"
1083         export DGIT_TEST_TMPBASE="$tmpbase"
1084         export ADTTMP=$tmp
1085         exec "$d/$ct"
1086 }       
1087
1088 t-alt-test () {
1089         local t=${0##*/}
1090         t-${t%%-*}
1091         t-chain-test "${t#*-}"
1092 }
1093
1094 t-git-config dgit.default.old-dsc-distro test-dummy
1095
1096 case "$0" in
1097 */gnupg) ;;
1098 *)      t-setup-import gnupg    ;;
1099 esac