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