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