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