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