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