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