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