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