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