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