chiark / gitweb /
4f6f3078a9d15e099e86e47952670ac5f4f35637
[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         t-fscks
396         echo ok.
397 }
398
399 t-rm-dput-dropping () {
400         rm -f $tmp/${p}_${v}_*.upload
401 }
402
403 t-dgit () {
404         local dgit=${DGIT_TEST-dgit}
405         pwd
406         : '
407 {{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{'
408         $dgit --dgit=$dgit --dget:-u --dput:--config=$tmp/dput.cf \
409                 --config-lookup-explode=dgit-distro.debian.alias-canon \
410                 ${distro--dtest-dummy} ${distro+${distro:+-d}${distro}} \
411                 $DGIT_TEST_OPTS $DGIT_TEST_DEBUG \
412                 -k39B13D8A $t_dgit_xopts "$@"
413         : '}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
414 '
415 }
416
417 t-diff-nogit () {
418         diff --exclude=.git --exclude=.pc -ruN $*
419 }
420
421 t-files-notexist () {
422         local f
423         for f in "$@"; do
424                 if [ -e $f ]; then
425                         fail "$f exists!"
426                 fi
427         done
428 }
429
430 t-cloned-fetched-good () {
431         t-diff-nogit ../extract/$p-${v%-*} .
432         t-clean-on-branch dgit/sid
433         t-refs-same-start
434         t-refs-same \
435                 refs/heads/dgit/sid \
436                 refs/remotes/dgit/dgit/sid
437         t-refs-notexist refs/dgit/unstable refs/remotes/dgit/dgit/unstable
438 }
439
440 t-output () {
441         printf "%s${1:+\n}" "$1" >$tmp/t.want
442         shift
443         "$@" >$tmp/t.got
444         diff $tmp/t.want $tmp/t.got
445 }
446
447 t-clean-on-branch () {
448         t-output "## $1" git status -b --porcelain
449 }
450
451 t-setup-done () {
452         local savevars=$1
453         local savedirs=$2
454         local importeval=$3
455
456         local import=IMPORT.${0##*/}
457         exec 4>$tmp/$import.new
458
459         local vn
460         for vn in $savevars; do
461                 perl >&4 -I. -MDebian::Dgit -e '
462                         printf "%s=%s\n", $ARGV[0], shellquote $ARGV[1]
463                 ' $vn "$(eval "printf '%s\n' \"\$$vn\"")"
464         done
465
466         (set -e; cd $tmp; tar cf $import.tar $savedirs)
467
468         printf >&4 "\n%s\n" "$importeval"
469
470         mv -f $tmp/$import.new $tmp/$import
471 }
472
473 t-setup-import () {
474         local setupname=$1
475
476         local setupsrc
477         local lock
478         if [ "x$tmpbase" = x ]; then
479                 # ADTTMP was set on entry to tests/lib, so we
480                 # are not sharing tmp area between tests
481                 setupsrc="$tmp"
482                 lock="$tmp/.dummy.lock"
483         else
484                 setupsrc="$tmpbase/$setupname"
485                 lock="$setupsrc.lock"
486         fi
487
488         local simport="$setupsrc/IMPORT.$setupname"
489
490         if ! [ -e "$simport" ]; then
491                 with-lock-ex -w "$lock" \
492                 xargs -0 -a $tmp/.save-env \
493                 bash -xec '
494                         cd "$1"; shift
495                         setupname="$1"; shift
496                         simport="$1"; shift
497                         if [ -e "$simport" ]; then exit 0; fi
498                         env - "$@" \
499                         "tests/setup/$setupname"
500                 ' x "$root" "$setupname" "$simport"
501         fi
502
503         if [ "x$setupsrc" != "x$tmp" ]; then
504                 (set -e; cd $tmp; tar xf "$simport.tar")
505         fi
506
507         . "$simport"
508 }
509
510 t-git-get-ref-exact () {
511         local ref=$1
512         # does not dereference, unlike t-git-get-ref
513         case "$ref" in
514         refs/*) ;;
515         *) fail "t-git-get-ref-exact bad $ref" ;;
516         esac
517         git for-each-ref --format='%(objectname)' "[r]efs/${ref#refs/}"
518 }
519
520 t-git-get-ref () {
521         local ref=$1
522         case "$ref" in
523         refs/*) ;;
524         *) fail "t-git-get-ref bad $ref" ;;
525         esac
526         (git show-ref -d $1 || test $? = 1) | perl -ne '
527                 $x = $1 if m#^(\w+) \Q'$1'\E(?:\^\{\})?$#;
528                 END { print "$x\n" if length $x; }
529         '
530 }
531
532 t-ref-same-exact () {
533         local name="$1"
534         local val; val=`t-git-get-ref-exact $name`
535         t-ref-same-val "$name" $val
536 }
537
538 t-ref-same () {
539         local name="$1"
540         local val; val=`t-git-get-ref $name`
541         t-ref-same-val "$name" $val
542 }
543
544 t-ref-head () {
545         local val; val=`git rev-parse HEAD`
546         t-ref-same-val HEAD $val
547 }
548
549 t-ref-same-val () {
550         local name="$1"
551         local val=$2
552         case "${t_ref_val-unset}" in
553         unset)          ;;
554         "$val")         ;;
555         *)              fail "ref varies: ($name)\
556  ${val:-nothing} != ${t_ref_val:-nothing} (${t_ref_names[*]})" ;;
557         esac
558         t_ref_val="$val"
559         t_ref_names+=("$name")
560 }
561
562 t-refs-same-start () {
563         unset t_ref_val
564         t_ref_names=()
565 }
566
567 t-refs-same () {
568         local g
569         for g in $*; do
570                 t-ref-same $g
571         done
572 }
573
574 t-refs-notexist () {
575         local val
576         for g in $*; do
577                 val=`t-git-get-ref $g`
578                 if [ "x$val" != x ]; then
579                         fail "ref $g unexpectedly exists ($val)"
580                 fi
581         done
582 }
583
584 t-v-tag () {
585         echo refs/tags/$tagpfx/${v//\~/_}
586 }
587
588 t-format-ref () {
589         git log -n1 --pretty=format:"$1" "$2"
590 }
591
592 t-sametree-parent () {
593         local ref=$1
594         local parent
595         local ctree; ctree=$(t-format-ref '%T' "$ref")
596         while :; do
597                 local psame=''
598                 for parent in $(t-format-ref '%P' "$ref"); do
599                         local ptree; ptree=$(t-format-ref '%T' "$parent")
600                         if [ "x$ptree" = "x$ctree" ]; then
601                                 psame+=" $parent"
602                         fi
603                 done
604                 case "$psame" in ""|" * *") break ;; esac
605                 ref="${psame# }"
606         done
607         echo "$ref"
608 }
609
610 t-check-pushed-master () {
611         local master; master=`t-git-get-ref refs/heads/master`
612         if [ x$master = x$t_ref_val ]; then return; fi
613         if [ x$master = x ]; then fail "failed to push master"; fi
614         # didn't update master, it must be not FF
615         local mbase; mbase=`t-git-merge-base $master $t_ref_val`
616         if [ x$mbase = x$master ]; then fail "failed to ff master"; fi
617 }
618
619 t-pushed-good () {
620         local branch=$1
621         local suite=${2:-sid}
622         t-refs-same \
623                 refs/heads/$branch
624         t-pushed-good-core
625 }
626         
627 t-pushed-good-core () {
628         t-ref-dsc-dgit
629         t-refs-same \
630                 `t-v-tag` \
631                 refs/remotes/dgit/dgit/$suite
632         t-refs-notexist \
633                 refs/heads/dgit/unstable \
634                 refs/remotes/dgit/dgit/unstable
635         (set -e; cd $dgitrepo
636          t-refs-same \
637                 refs/dgit/$suite \
638                 `t-v-tag`
639          ${t_check_pushed_master:- : NOT-DRS-NO-CHECK-PUSHED-MASTER}
640          t-refs-notexist \
641                 refs/dgit/unstable
642         )
643         git verify-tag `t-v-tag`
644 }
645
646 t-splitbrain-pushed-good--unpack () {
647         cd $tmp
648         rm -rf t-unpack
649         mkdir t-unpack
650         cd t-unpack
651         ln -s $tmp/mirror/pool/main/*.orig*.tar* .
652         ln -s $tmp/incoming/*.orig*.tar* . ||:
653         ln -s $incoming_dsc .
654         ln -s ${incoming_dsc/.dsc/.debian.tar}* .
655         dpkg-source "$@" -x *.dsc
656         cd */.
657         git init
658         git fetch ../../$p "refs/tags/*:refs/tags/*"
659 }
660
661 t-splitbrain-pushed-good--checkprep () {
662         git add -Af .
663         git rm --cached -r --ignore-unmatch .pc
664 }
665
666 t-splitbrain-pushed-good--checkdiff () {
667         local tag=$1
668         t-splitbrain-pushed-good--checkprep
669         t-output "" git diff --stat --cached $tag
670 }
671
672 t-splitbrain-pushed-good-start () {
673         dep14tag=refs/tags/test-dummy/${v//\~/_}
674         dgittag=$(t-v-tag)
675         t-output "" git status --porcelain
676         t-ref-head
677         t-refs-same $dep14tag
678         (set -e; cd $dgitrepo; t-refs-same $dep14tag)
679         git merge-base --is-ancestor $dep14tag $dgittag
680
681         t-refs-same-start
682         t-ref-same refs/heads/split.p
683         case "$(t-git-get-ref refs/heads/split.b)" in
684         "$t_ref_val") ;;
685         "$(git rev-parse refs/heads/split.p^0)") ;;
686         "$(git rev-parse refs/heads/split.p^1)") ;;
687         *) fail 'bad b/p' ;;
688         esac
689         t-pushed-good-core
690
691         t-incoming-dsc
692
693         t-splitbrain-pushed-good--unpack
694         t-splitbrain-pushed-good--checkdiff $dgittag
695 }
696 t-splitbrain-pushed-good-end-made-dep14 () {
697         t-splitbrain-pushed-good--checkdiff $dep14tag
698         cd $tmp/$p
699 }
700
701 t-splitbrain-rm-gitignore-patch () {
702         perl -i -pe '
703                 next unless $_ eq "auto-gitignore\n";
704                 die if $counter++;
705                 chomp;
706                 rename "debian/patches/$_", "../t-auto-gitignore" or die $!;
707                 $_ = "";
708         ' debian/patches/series
709 }
710
711 t-gbp-pushed-good () {
712         local suite=${1:-sid}
713         t-splitbrain-pushed-good-start
714
715         # Right, now we want to check that the maintainer tree and
716         # the dgit tree differ in exactly the ways we expect.  We
717         # achieve this by trying to reconstruct the maintainer tree
718         # from the dgit tree.
719
720         # So, unpack it withut the patches applied
721         t-splitbrain-pushed-good--unpack --skip-patches
722
723         # dgit might have added a .gitignore patch, which we need to
724         # drop and remove
725         t-splitbrain-rm-gitignore-patch
726
727         # Now the result should differ only in non-debian/ .gitignores
728         t-splitbrain-pushed-good--checkprep
729         git diff --cached --name-only $dep14tag >../changed
730         perl -ne '
731                 next if !m#^debian/# && m#(^|/)\.gitignore#;
732                 die "$_ mismatch";
733         ' <../changed
734
735         # If we actually apply the gitignore patch by hand, it
736         # should be perfect:
737         if [ -f ../t-auto-gitignore ]; then
738                 patch --backup-if-mismatch -p1 -u <../t-auto-gitignore
739         fi
740
741         t-splitbrain-pushed-good-end-made-dep14
742 }
743
744 t-unapplied-pushed-good () {
745         t-splitbrain-pushed-good-start
746         t-splitbrain-pushed-good--unpack --skip-patches
747         t-splitbrain-pushed-good-end-made-dep14
748 }
749
750 t-dpm-pushed-good () {
751         t-splitbrain-pushed-good-start
752         t-splitbrain-pushed-good--unpack
753         t-splitbrain-rm-gitignore-patch
754         t-splitbrain-pushed-good-end-made-dep14
755 }
756
757 t-commit-build-push-expect-log () {
758         local msg=$1
759         local mpat=$2
760         t-commit "$msg"
761         t-dgit build
762         LC_MESSAGES=C \
763         t-dgit push --new 2>&1 |tee $tmp/push.log
764         t-grep-mpat "$mpat" $tmp/push.log
765 }
766
767 t-822-field () {
768         local file=$1
769         local field=$2
770         perl -e '
771                 use Dpkg::Control::Hash;
772                 my $h = new Dpkg::Control::Hash allow_pgp=>1;
773                 $h->parse(\*STDIN,"'"$file"'");
774                 my $val = $h->{"'$field'"},"\n";
775                 die "'"$file $field"'" unless defined $val;
776                 print $val,"\n";
777         ' <$file
778 }
779
780 t-stunt-envvar () {
781         local var=$1
782         local tstunt=$2
783         eval '
784                 case "$'$var'" in
785                 "$tstunt:"*)    ;;
786                 *":$tstunt:"*)  ;;
787                 "")             '$var'="$tstunt" ;;
788                 *)              '$var'="$tstunt:$'$var'" ;;
789                 esac
790                 export '$var'
791         '
792 }
793
794 t-tstunt--save-real () {
795         local f="$1"
796         case "$f" in
797         */*) return ;;
798         esac
799
800         local rc
801         local real
802         set +e
803         real=$(type -p "$f")
804         rc=$?
805         set -e
806
807         case $rc in
808         1)      return ;;
809         0)      ;;
810         *)      fail "did not find $f on PATH $PATH" ;;
811         esac
812
813         local varname=${f//[^_0-9a-zA-Z]/_}
814         varname=DGIT_TEST_REAL_${varname^^}
815
816         eval "
817                 : \${$varname:=\$real}
818                 export $varname
819         "
820 }
821
822 t-tstunt () {
823         local tstunt=$tmp/tstunt
824         t-stunt-envvar PATH $tstunt
825         t-stunt-envvar PERLLIB $tstunt
826         local f
827         for f in "$@"; do
828                 t-tstunt--save-real $f
829                 f="./$f"
830                 local d="$tstunt/${f%/*}"
831                 mkdir -p $d
832                 ln -sf "$troot/tstunt/$f" "$d"/.
833         done
834 }
835
836 t-tstunt-parsechangelog () {
837         t-tstunt dpkg-parsechangelog Dpkg/Changelog/Parse.pm
838 }
839
840 t-tstunt-lintian () {
841         t-tstunt lintian
842 }
843
844 t-tstunt-debuild () {
845         t-tstunt debuild
846 }
847
848 t-incoming-dsc () {
849         local dsc=${p}_${v}.dsc
850         incoming_dsc=$tmp/incoming/$dsc
851 }
852
853 t-ref-dsc-dgit () {
854         t-incoming-dsc
855         local val; val=`t-822-field $incoming_dsc Dgit`
856         val=$( perl -e '
857                 $_=shift @ARGV;
858                 die "Dgit $_ ?" unless m/^\w+\b/;
859                 print $&,"\n" or die $!;
860         ' "$val")
861         t-ref-same-val $incoming_dsc "$val"
862 }
863
864 t-apply-diff () {
865         local v1=$1
866         local v2=$2
867         (cd $troot/pkg-srcs;
868          debdiff ${p}_${v1}.dsc ${p}_${v2}.dsc || test $? = 1) \
869          | patch -p1 -u
870 }
871
872 t-gbp-unapplied-pq2qc () {
873         # does `gbp pq export'
874         # commits the resulting debian/patches on  qc/BRANCH
875         # leaves us on qc/BRANCH (eg "qc/quilt-tip"))
876         # qc/BRANCH is not fast-forwarding
877
878         gbp pq export
879
880         branch=`git symbolic-ref HEAD`
881         branch=${branch#refs/heads/}
882
883         case "$branch" in
884         */*) fail "unexpected branch $branch" ;;
885         esac
886
887         git branch -f qc/$branch
888         git checkout qc/$branch
889         git add debian/patches
890         git commit -m 'Commit patch queue'
891 }
892
893 t-git-pseudo-merge () {
894         # like   git merge -s ours
895         if [ ! "$git_pseuomerge_opts" ]; then
896                 if git merge --help \
897                  | grep -q allow-unrelated-histories; then
898                         git_pseuomerge_opts='--allow-unrelated-histories'
899                 fi
900                 git_pseuomerge_opts+=' -s ours'
901         fi
902         git merge $git_pseuomerge_opts "$@"
903 }
904
905 t-gbp-example-prep-no-ff () {
906         t-tstunt-parsechangelog
907         t-archive example 1.0-1
908         t-git-none
909         t-worktree 1.0
910
911         cd example
912
913         t-dgit fetch
914
915         git checkout -b patch-queue/quilt-tip-2 patch-queue/quilt-tip
916         gbp pq rebase
917
918         echo '/* some comment */' >>src.c
919         git add src.c
920         git commit -m 'Add a comment to an upstream file'
921
922         t-gbp-unapplied-pq2qc
923
924         t-commit 'some updates' 1.0-2
925 }
926
927 t-gbp-example-prep () {
928         t-gbp-example-prep-no-ff
929
930         t-git-pseudo-merge \
931                 -m 'Pseudo-merge to make descendant of archive' \
932                 remotes/dgit/dgit/sid
933 }
934
935 t-make-badcommit () {
936         badcommit=$(
937                 git cat-file commit HEAD | \
938                 perl -pe 's/^committer /commiter /' | \
939                 git hash-object -w -t commit --stdin
940         )
941         t-expect-fsck-fail $badcommit
942 }
943
944 t-commit () {
945         local msg=$1
946         v=${2:-${majorv:-1}.$revision}
947         dch --force-distribution -v$v --distribution ${3:-unstable} "$1"
948         git add debian/changelog
949         debcommit
950         revision=$(( ${revision-0} + 1 ))
951 }
952
953 t-git-config () {
954         git config --global "$@"
955 }
956
957 t-drs () {
958         export DGIT_TEST_TROOT=$troot
959  t-git-config dgit-distro.test-dummy.git-url "ext::$troot/drs-git-ext %S "
960  t-git-config dgit-distro.test-dummy.git-check true
961  t-git-config dgit-distro.test-dummy.git-create true
962  t-git-config dgit-distro.test-dummy.dgit-tag-format new,old,maint
963         cp $troot/gnupg/{dd.gpg,dm.gpg,dm.txt} $tmp/.
964         cp $troot/suites $tmp/.
965         cp $troot/suites $tmp/suites-master
966
967         export t_check_pushed_master=t-check-pushed-master
968
969         drs_dispatch=$tmp/distro=test-dummy
970         mkdir $drs_dispatch
971
972         if [ "x$DGIT_TEST_INTREE" != x ]; then
973                 ln -sf "$DGIT_TEST_INTREE" $drs_dispatch/dgit-live
974         fi
975
976         ln -sf $tmp/git $drs_dispatch/repos
977         ln -sf $tmp/suites $tmp/suites-master $tmp/dm.txt $drs_dispatch/
978         mkdir -p $drs_dispatch/keyrings
979         ln -sf $tmp/dd.gpg $drs_dispatch/keyrings/debian-keyring.gpg
980         ln -sf $tmp/dm.gpg $drs_dispatch/keyrings/debian-maintainers.gpg
981         ln -sf /bin/true $drs_dispatch/policy-hook
982 }
983
984 t-newtag () {
985  export tagpfx=archive/test-dummy
986  t-git-config dgit-distro.test-dummy.dgit-tag-format new,maint
987 }
988 t-oldtag () {
989  export tagpfx=test-dummy
990  t-git-config dgit-distro.test-dummy.dgit-tag-format old
991 }
992
993 t-dsd () {
994         t-drs
995  t-git-config dgit-distro.test-dummy.ssh "$troot/dsd-ssh"
996  t-git-config dgit-distro.test-dummy.git-check ssh-cmd
997  t-git-config dgit-distro.test-dummy.git-create true
998  t-git-config dgit-distro.test-dummy.git-url \
999                 "ext::$troot/dsd-ssh X %S /dgit/test-dummy/repos"
1000
1001  t-git-config dgit-distro.test-dummy.diverts.drs /drs
1002  t-git-config dgit-distro.test-dummy/drs.ssh "$troot/ssh"
1003  t-git-config dgit-distro.test-dummy/drs.git-url $tmp/git
1004  t-git-config dgit-distro.test-dummy/drs.git-check ssh-cmd
1005  t-git-config dgit-distro.test-dummy/drs.git-create ssh-cmd
1006
1007         echo 'no-such-package* drs' >$drs_dispatch/diverts
1008 }
1009
1010 t-policy-admin () {
1011         ${DGIT_INFRA_PFX}dgit-repos-admin-debian --repos $tmp/git "$@"
1012 }
1013
1014 t-policy-nonexist () {
1015         ln -sf no-such-file-or-directory $drs_dispatch/policy-hook
1016 }
1017
1018 t-make-hook-link () {
1019         local hook=$1 # in infra/
1020         local linkpath=$2
1021         hook=${DGIT_INFRA_PFX}$hook
1022         case $hook in
1023         */*)    ;;
1024         *)      hook=`type -P $hook` ;;
1025         esac
1026         ln -sf "$hook" $linkpath
1027 }
1028
1029 t-policy () {
1030         local policyhook=$1
1031         t-make-hook-link $policyhook $drs_dispatch/policy-hook
1032 }
1033
1034 t-debpolicy () {
1035         t-dsd
1036         t-policy dgit-repos-policy-debian
1037
1038         mkdir $tmp/git
1039         t-policy-admin create-db
1040 }
1041
1042 t-policy-periodic () {
1043         ${DGIT_REPOS_SERVER_TEST-dgit-repos-server} \
1044                 test-dummy $drs_dispatch '' --cron
1045 }
1046
1047 t-restrict () {
1048         local restriction=$1
1049         (cd $root; t-restriction-$restriction >&2)
1050 }
1051
1052 t-dependencies () {
1053         : "Hopefully installed: $*"
1054 }
1055
1056 t-chain-test () {
1057         local ct=$1
1058         local d=${0%/*}
1059         cd $root
1060         export DGIT_TEST_TESTNAME="$testname"
1061         export DGIT_TEST_TMPBASE="$tmpbase"
1062         export ADTTMP=$tmp
1063         exec "$d/$ct"
1064 }       
1065
1066 t-alt-test () {
1067         local t=${0##*/}
1068         t-${t%%-*}
1069         t-chain-test "${t#*-}"
1070 }
1071
1072 case "$0" in
1073 */gnupg) ;;
1074 *)      t-setup-import gnupg    ;;
1075 esac