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