chiark / gitweb /
git-debrebase: Rename a variable $ups_tag to $ups_rev
[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         ${t_archive_ln_s-ln -s} \
366                  $troot/pkg-srcs/${p}_${2%-*}* $tmp/mirror/pool/main/
367         t-archive-query $suite
368         rm -rf $tmp/extract
369         mkdir $tmp/extract
370         (set -e; cd $tmp/extract; dpkg-source -x ../mirror/pool/main/$dscf)
371 }
372
373 t-git-dir-time-passes () {
374         touch -d 'last year' $dgitrepo
375 }
376
377 t-git-dir-check () {
378         local gitdir=$dgitrepo
379         case "$1" in
380         enoent)
381                 if test -e "$gitdir"; then fail "$gitdir exists"; fi
382                 return
383                 ;;
384         public) wantstat='7[75]5' ;;
385         secret) wantstat='7[70]0' ;;
386         *)      fail "$1 t-git-dir-check ?" ;;
387         esac
388         gotstat=`stat -c%a $gitdir`
389         case "$gotstat" in
390         *$wantstat) return ;;
391         *)      fail "$gitdir has mode $gotstat, expected $wantstat" ;;
392         esac
393 }
394
395 t-expect-fsck-fail () {
396         echo >>$tmp/fsck.expected-errors "$1"
397 }
398
399 t-git-fsck () {
400         local fsckerrs=$(git rev-parse --git-dir)/dgit-test-fsck.errs
401
402         set +e
403         LC_MESSAGES=C git fsck --no-dangling --strict 2>&1 \
404                 | tee $fsckerrs
405         ps="${PIPESTATUS[*]}"
406         set -e
407
408         local pats
409         if [ -f $tmp/fsck.expected-errors ]; then
410                 pats=(-w -f $tmp/fsck.expected-errors)
411         else
412                 test "$ps" = "0 0"
413         fi
414         pats+=(-e 'notice: HEAD points to an unborn branch')
415         pats+=(-e 'notice: No default references')
416
417         set +e
418         grep -v "${pats[@]}" $fsckerrs
419         rc=$?
420         set -e
421         case $rc in
422         1) ;; # no unexpected errors
423         0) fail "unexpected messages from git-fsck" ;;
424         *) fail "grep of git-fsck failed" ;;
425         esac
426 }
427
428 t-check-only-bpd () {
429         if [ "$bpd" = .. ]; then return; fi
430         t-files-notexist \
431                 $tmp/*.{deb,changes,dsc,buildinfo} \
432                 $tmp/*.{tar,diff}.*
433 }
434
435 t-fscks () {
436         (
437         shopt -s nullglob
438         for d in $tmp/*/.git $tmp/git/*.git; do
439                 cd "${d%/.git}"
440                 t-git-fsck
441         done
442         )
443 }
444
445 t-ok () {
446         : '========================================'
447         t-check-only-bpd
448         t-fscks
449         t-save-artifacts
450         echo ok.
451 }
452
453 t-save-artifacts () {
454         artifacts="$AUTOPKGTEST_ARTIFACTS"
455         if [ x"$artifacts" = x ]; then return; fi
456         if [ x"tmp" = x ]; then return; fi
457         GZIP=-1v tar -C "$tmp" -zc -f "$artifacts/${0##*/}.tar.gz" \
458                 --exclude=\*.tar .
459 }
460
461 t-rm-dput-dropping () {
462         rm -f $tmp/${p}_${v}_*.upload
463 }
464
465 t-dgit () {
466         local dgit=${DGIT_TEST-dgit}
467         pwd >&2
468         : '
469 {{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{'
470         $dgit --dgit=$dgit --dget:-u --dput:--config=$tmp/dput.cf \
471  ${dgit_config_debian_alias-"--config-lookup-explode=dgit-distro.debian.alias-canon"} \
472  ${DGIT_GITDEBREBASE_TEST+--git-debrebase=}${DGIT_GITDEBREBASE_TEST} \
473                 ${distro+${distro:+-d}}${distro--dtest-dummy} \
474                 $DGIT_TEST_OPTS $DGIT_TEST_DEBUG \
475                 -kBCD22CD83243B79D3DFAC33EA3DBCBC039B13D8A $t_dgit_xopts "$@"
476         : '}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
477 '
478 }
479
480 t-non-dgit-upload () {
481         t-dgit -wgf build-source
482
483         cd ..
484         c=${p}_${v}_source.changes
485         debsign -kBCD22CD83243B79D3DFAC33EA3DBCBC039B13D8A $c
486         dput -c $tmp/dput.cf test-dummy $c
487
488         t-archive-process-incoming sid
489         t-git-next-date
490         cd $p
491 }
492
493 t-dgit-manpage () {
494         local section=$1
495         local page=$2
496         (export LC_ALL=C.UTF-8
497          if [ "$DGIT_TEST_INTREE" ]; then
498                 make -C $DGIT_TEST_INTREE $page.$section.view
499          else
500                 man $section $page
501          fi)
502 }
503
504 t-diff-nogit () {
505         diff --exclude=.git --exclude=.pc -ruN $*
506 }
507
508 t-files-notexist () {
509         local f
510         for f in "$@"; do
511                 if [ -e $f ]; then
512                         fail "$f exists!"
513                 fi
514         done
515 }
516
517 t-cloned-fetched-good () {
518         t-diff-nogit ../extract/$p-${v%-*} .
519         t-clean-on-branch dgit/sid
520         t-refs-same-start
521         t-refs-same \
522                 refs/heads/dgit/sid \
523                 refs/remotes/dgit/dgit/sid
524         t-refs-notexist refs/dgit/unstable refs/remotes/dgit/dgit/unstable
525 }
526
527 t-output () {
528         printf "%s${1:+\n}" "$1" >$tmp/t.want
529         shift
530         "$@" >$tmp/t.got
531         diff $tmp/t.want $tmp/t.got
532 }
533
534 t-clean-on-branch () {
535         t-output "## $1" git status -b --porcelain
536 }
537
538 t-setup-done () {
539         local savevars=$1
540         local savedirs=$2
541         local importeval=$3
542
543         local import=IMPORT.${DGIT_TEST_TESTNAME-${0##*/}}
544         exec 4>$tmp/$import.new
545
546         local vn
547         for vn in $savevars; do
548                 perl >&4 -"I$root" -MDebian::Dgit -e '
549                         printf "%s=%s\n", $ARGV[0], shellquote $ARGV[1]
550                 ' $vn "$(eval "printf '%s\n' \"\$$vn\"")"
551         done
552
553         perl >&4 -"I$root" -MDebian::Dgit -we '
554                 foreach my $vn (grep m/^DGIT_TEST_REAL_/, keys %ENV) {
555                         print STDERR "saving-exporting $vn\n";
556                         printf "export %s=%s\n", $vn, shellquote $ENV{$vn}
557                                 or die $!;
558                 }
559         '
560
561         (set -e; cd $tmp; tar cf $import.tar $savedirs)
562
563         printf >&4 "\n%s\n" "$importeval"
564
565         mv -f $tmp/$import.new $tmp/$import
566 }
567
568 t-setup-import () {
569         local setupname=$1
570
571         local setupsrc
572         local lock
573         if [ "x$tmpbase" = x ]; then
574                 # ADTTMP was set on entry to tests/lib, so we
575                 # are not sharing tmp area between tests
576                 setupsrc="$tmp"
577                 lock="$tmp/.dummy.lock"
578         else
579                 setupsrc="$tmpbase/$setupname"
580                 lock="$setupsrc.lock"
581         fi
582
583         local simport="$setupsrc/IMPORT.$setupname"
584
585         if ! [ -e "$simport" ]; then
586                 with-lock-ex -w "$lock" \
587                 xargs -0 -a $tmp/.save-env \
588                 bash -xec '
589                         cd "$1"; shift
590                         setupname="$1"; shift
591                         simport="$1"; shift
592                         if [ -e "$simport" ]; then exit 0; fi
593                         env - "$@" \
594                         "tests/setup/$setupname"
595                 ' x "$root" "$setupname" "$simport"
596         fi
597
598         if [ "x$setupsrc" != "x$tmp" ]; then
599                 (set -e; cd $tmp; tar xf "$simport.tar")
600         fi
601
602         mkdir -p $tmp/must-clean
603
604         . "$simport"
605 }
606
607 t-git-get-ref-exact () {
608         local ref=$1
609         # does not dereference, unlike t-git-get-ref
610         case "$ref" in
611         refs/*) ;;
612         *) fail "t-git-get-ref-exact bad $ref" ;;
613         esac
614         git for-each-ref --format='%(objectname)' "[r]efs/${ref#refs/}"
615 }
616
617 t-git-get-ref () {
618         local ref=$1
619         case "$ref" in
620         refs/*) ;;
621         *) fail "t-git-get-ref bad $ref" ;;
622         esac
623         (git show-ref -d $1 || test $? = 1) | perl -ne '
624                 $x = $1 if m#^(\w+) \Q'$1'\E(?:\^\{\})?$#;
625                 END { print "$x\n" if length $x; }
626         '
627 }
628
629 t-ref-same-exact () {
630         local name="$1"
631         local val; val=`t-git-get-ref-exact $name`
632         t-ref-same-val "$name" $val
633 }
634
635 t-ref-same () {
636         local name="$1"
637         local val; val=`t-git-get-ref $name`
638         t-ref-same-val "$name" $val
639 }
640
641 t-ref-head () {
642         local val; val=`git rev-parse HEAD`
643         t-ref-same-val HEAD $val
644 }
645
646 t-ref-same-val () {
647         local name="$1"
648         local val=$2
649         case "${t_ref_val-unset}" in
650         unset)          ;;
651         "$val")         ;;
652         *)              fail "ref varies: ($name)\
653  ${val:-nothing} != ${t_ref_val:-nothing} (${t_ref_names[*]})" ;;
654         esac
655         t_ref_val="$val"
656         t_ref_names+=("$name")
657 }
658
659 t-refs-same-start () {
660         unset t_ref_val
661         t_ref_names=()
662 }
663
664 t-refs-same () {
665         local g
666         for g in $*; do
667                 t-ref-same $g
668         done
669 }
670
671 t-refs-notexist () {
672         local val
673         for g in $*; do
674                 val=`t-git-get-ref $g`
675                 if [ "x$val" != x ]; then
676                         fail "ref $g unexpectedly exists ($val)"
677                 fi
678         done
679 }
680
681 t-v-tag () {
682         echo refs/tags/$tagpfx/${v//\~/_}
683 }
684
685 t-format-ref () {
686         git log -n1 --pretty=format:"$1" "$2"
687 }
688
689 t-sametree-parent () {
690         local ref=$1
691         local parent
692         local ctree; ctree=$(t-format-ref '%T' "$ref")
693         while :; do
694                 local psame=''
695                 for parent in $(t-format-ref '%P' "$ref"); do
696                         local ptree; ptree=$(t-format-ref '%T' "$parent")
697                         if [ "x$ptree" = "x$ctree" ]; then
698                                 psame+=" $parent"
699                         fi
700                 done
701                 case "$psame" in ""|" * *") break ;; esac
702                 ref="${psame# }"
703         done
704         echo "$ref"
705 }
706
707 t-check-pushed-master () {
708         local master; master=`t-git-get-ref refs/heads/master`
709         if [ x$master = x$t_ref_val ]; then return; fi
710         if [ x$master = x ]; then fail "failed to push master"; fi
711         # didn't update master, it must be not FF
712         local mbase; mbase=`t-git-merge-base $master $t_ref_val`
713         if [ x$mbase = x$master ]; then fail "failed to ff master"; fi
714 }
715
716 t-push-was-source-only () {
717         local f
718         t-files-notexist $tmp/incoming/${p}_${v}_*.deb \
719                          $tmp/incoming/${p}_${v}_*.udeb
720         # we permit _source.buildinfo files; see test_changes_source_only()
721         for f in $tmp/incoming/${p}_${v}_*.buildinfo; do
722             if [ -e $f ]; then
723                 case "$f" in
724                     *_source.buildinfo) ;;
725                     *) fail "non-source-only file $f exists!" ;;
726                 esac
727             fi
728         done
729 }
730
731 t-push-included () {
732     for f in $@; do
733         stat $tmp/incoming/$f
734     done
735 }
736
737 t-pushed-good () {
738         local branch=$1
739         local suite=${2:-sid}
740         t-refs-same \
741                 refs/heads/$branch
742         t-pushed-good-core
743 }
744         
745 t-pushed-good-core () {
746         t-ref-dsc-dgit
747         t-refs-same \
748                 `t-v-tag` \
749                 refs/remotes/dgit/dgit/$suite
750         t-refs-notexist \
751                 refs/heads/dgit/unstable \
752                 refs/remotes/dgit/dgit/unstable
753         (set -e; cd $dgitrepo
754          t-refs-same \
755                 refs/dgit/$suite \
756                 `t-v-tag`
757          ${t_check_pushed_master:- : NOT-DRS-NO-CHECK-PUSHED-MASTER}
758          t-refs-notexist \
759                 refs/dgit/unstable
760         )
761         git verify-tag `t-v-tag`
762 }
763
764 t-pushed-good-check-changes () {
765         changes_filename="$tmp/incoming/${p}_${v}_*.changes"
766         grep -E "^Distribution: $suite" $changes_filename
767         grep -E "^Version: $v" $changes_filename
768 }
769
770 t-splitbrain-pushed-good--unpack () {
771         cd $tmp
772         rm -rf t-unpack
773         mkdir t-unpack
774         cd t-unpack
775         ln -s $tmp/mirror/pool/main/*.orig*.tar* .
776         ln -s $tmp/incoming/*.orig*.tar* . ||:
777         ln -s $incoming_dsc .
778         ln -s ${incoming_dsc/.dsc/.debian.tar}* .
779         ln -s ${incoming_dsc/.dsc/.tar}* . ||:
780         dpkg-source "$@" -x *.dsc
781         cd */.
782         git init
783         git fetch ../../$p "refs/tags/*:refs/tags/*"
784 }
785
786 t-splitbrain-pushed-good--checkprep () {
787         git add -Af .
788         git rm --cached -r --ignore-unmatch .pc
789 }
790
791 t-splitbrain-pushed-good--checkdiff () {
792         local tag=$1
793         t-splitbrain-pushed-good--checkprep
794         t-output "" git diff --stat --cached $tag
795 }
796
797 t-splitbrain-pushed-good-start () {
798         dep14tag=refs/tags/test-dummy/${v//\~/_}
799         dgittag=$(t-v-tag)
800         t-output "" git status --porcelain
801         t-ref-head
802         t-refs-same $dep14tag
803         (set -e; cd $dgitrepo; t-refs-same $dep14tag)
804         git merge-base --is-ancestor $dep14tag $dgittag
805
806         t-refs-same-start
807         t-ref-same refs/heads/split.p
808         local split_b=$(t-git-get-ref refs/heads/split.b)
809         case "$split_b" in
810         "$t_ref_val") ;;
811         "$(git rev-parse refs/heads/split.p^0)") ;;
812         "$(git rev-parse refs/heads/split.p^1)") ;;
813         *) fail "bad b/p (b=$split_b)" ;;
814         esac
815         t-pushed-good-core
816
817         t-incoming-dsc
818
819         t-splitbrain-pushed-good--unpack
820         t-splitbrain-pushed-good--checkdiff $dgittag
821 }
822 t-splitbrain-pushed-good-end-made-dep14 () {
823         t-splitbrain-pushed-good--checkdiff $dep14tag
824         cd $tmp/$p
825 }
826
827 t-splitbrain-rm-1-patch () {
828         local patchname=$1
829         perl -i -pe '
830                 next unless $_ eq "'"$patchname"'\n";
831                 die if $counter++;
832                 chomp;
833                 rename "debian/patches/$_", "../t-'"$patchname"'" or die $!;
834                 $_ = "";
835         ' debian/patches/series
836 }
837
838 t-splitbrain-rm-gitignore-patch () {
839         t-splitbrain-rm-1-patch auto-gitignore
840 }
841
842 t-gbp-pushed-good () {
843         local suite=${1:-sid}
844         t-splitbrain-pushed-good-start
845
846         # Right, now we want to check that the maintainer tree and
847         # the dgit tree differ in exactly the ways we expect.  We
848         # achieve this by trying to reconstruct the maintainer tree
849         # from the dgit tree.
850
851         # So, unpack it withut the patches applied
852         t-splitbrain-pushed-good--unpack --skip-patches
853
854         # dgit might have added a .gitignore patch, which we need to
855         # drop and remove
856         t-splitbrain-rm-gitignore-patch
857
858         # Now the result should differ only in non-debian/ .gitignores
859         t-splitbrain-pushed-good--checkprep
860         git diff --cached --name-only $dep14tag >../changed
861         perl -ne '
862                 next if !m#^debian/# && m#(^|/)\.gitignore#;
863                 die "$_ mismatch";
864         ' <../changed
865
866         # If we actually apply the gitignore patch by hand, it
867         # should be perfect:
868         if [ -f ../t-auto-gitignore ]; then
869                 patch --backup-if-mismatch -p1 -u <../t-auto-gitignore
870         fi
871
872         t-splitbrain-pushed-good-end-made-dep14
873 }
874
875 t-unapplied-pushed-good () {
876         local suite=${1:-sid}
877         t-splitbrain-pushed-good-start
878         t-splitbrain-pushed-good--unpack --skip-patches
879         t-splitbrain-pushed-good-end-made-dep14
880 }
881
882 t-dpm-pushed-good () {
883         local suite=${1:-sid}
884         t-splitbrain-pushed-good-start
885         t-splitbrain-pushed-good--unpack
886         t-splitbrain-rm-gitignore-patch
887         t-splitbrain-pushed-good-end-made-dep14
888 }
889
890 t-split-unchanged-pushed-good () {
891         local suite=${1:-sid}
892         t-splitbrain-pushed-good-start
893         t-splitbrain-pushed-good--unpack
894         t-splitbrain-pushed-good-end-made-dep14
895 }
896
897 t-commit-build-push-expect-log () {
898         local msg=$1
899         local mpat=$2
900         t-commit "$msg"
901         t-dgit build
902         LC_MESSAGES=C \
903         t-dgit push --new 2>&1 |tee $tmp/push.log
904         t-grep-mpat "$mpat" $tmp/push.log
905 }
906
907 t-822-field () {
908         local file=$1
909         local field=$2
910         perl -e '
911                 use Dpkg::Control::Hash;
912                 my $h = new Dpkg::Control::Hash allow_pgp=>1;
913                 $h->parse(\*STDIN,"'"$file"'");
914                 my $val = $h->{"'$field'"},"\n";
915                 die "'"$file $field"'" unless defined $val;
916                 print $val,"\n";
917         ' <$file
918 }
919
920 t-defdistro () {
921         export DGIT_TEST_DISTRO=''
922         distro=''
923         t-git-config dgit-suite.unstable.distro test-dummy
924 }
925
926 t-stunt-envvar () {
927         local var=$1
928         local tstunt=$2
929         eval '
930                 case "$'$var'" in
931                 "$tstunt:"*)    ;;
932                 *":$tstunt:"*)  ;;
933                 "")             '$var'="$tstunt" ;;
934                 *)              '$var'="$tstunt:$'$var'" ;;
935                 esac
936                 export '$var'
937         '
938 }
939
940 t-tstunt--save-real () {
941         local f="$1"
942         case "$f" in
943         */*) return ;;
944         esac
945
946         local rc
947         local real
948         set +e
949         real=$(
950                 p=":$PATH:"
951                 p="${p/:"$tmp/tstunt":/:}"
952                 p="${p%:}"
953                 p="${p#:}"
954                 PATH="$p"
955                 type -p "$f"
956         )
957         rc=$?
958         set -e
959
960         case $rc in
961         1)      return ;;
962         0)      ;;
963         *)      fail "did not find $f on PATH $PATH" ;;
964         esac
965
966         local varname=${f//[^_0-9a-zA-Z]/_}
967         varname=DGIT_TEST_REAL_${varname^^}
968
969         eval "
970                 : \${$varname:=\$real}
971                 export $varname
972         "
973 }
974
975 t-tstunt () {
976         local tstunt=$tmp/tstunt
977         t-stunt-envvar PATH $tstunt
978         t-stunt-envvar PERLLIB $tstunt
979         local f
980         for f in "$@"; do
981                 t-tstunt--save-real $f
982                 f="./$f"
983                 local d="$tstunt/${f%/*}"
984                 mkdir -p $d
985                 ln -sf "$troot/tstunt/$f" "$d"/.
986         done
987 }
988
989 t-tstunt-parsechangelog () {
990         t-tstunt dpkg-parsechangelog Dpkg/Changelog/Parse.pm
991 }
992
993 t-tstunt-lintian () {
994         t-tstunt lintian
995 }
996
997 t-tstunt-debuild () {
998         t-tstunt debuild
999 }
1000
1001 t-incoming-dsc () {
1002         local dsc=${p}_${v}.dsc
1003         incoming_dsc=$tmp/incoming/$dsc
1004 }
1005
1006 t-ref-dsc-dgit () {
1007         t-incoming-dsc
1008         local val; val=`t-822-field $incoming_dsc Dgit`
1009         val=$( perl -e '
1010                 $_=shift @ARGV;
1011                 die "Dgit $_ ?" unless m/^\w+\b/;
1012                 print $&,"\n" or die $!;
1013         ' "$val")
1014         t-ref-same-val $incoming_dsc "$val"
1015 }
1016
1017 t-apply-diff () {
1018         local v1=$1
1019         local v2=$2
1020         (cd $troot/pkg-srcs;
1021          debdiff ${p}_${v1}.dsc ${p}_${v2}.dsc || test $? = 1) \
1022          | patch -p1 -u
1023 }
1024
1025 t-gbp-unapplied-pq2qc () {
1026         # does `gbp pq export'
1027         # commits the resulting debian/patches on  qc/BRANCH
1028         # leaves us on qc/BRANCH (eg "qc/quilt-tip"))
1029         # qc/BRANCH is not fast-forwarding
1030
1031         gbp pq export
1032
1033         branch=`git symbolic-ref HEAD`
1034         branch=${branch#refs/heads/}
1035
1036         case "$branch" in
1037         */*) fail "unexpected branch $branch" ;;
1038         esac
1039
1040         git branch -f qc/$branch
1041         git checkout qc/$branch
1042         git add debian/patches
1043         git commit -m 'Commit patch queue'
1044 }
1045
1046 t-git-pseudo-merge () {
1047         # like   git merge -s ours
1048         if [ ! "$git_pseuomerge_opts" ]; then
1049                 if git merge --help \
1050                  | grep -q allow-unrelated-histories; then
1051                         git_pseuomerge_opts='--allow-unrelated-histories'
1052                 fi
1053                 git_pseuomerge_opts+=' -s ours'
1054         fi
1055         git merge $git_pseuomerge_opts "$@"
1056 }
1057
1058 t-gbp-example-prep-no-ff () {
1059         t-archive example 1.0-1
1060         t-git-none
1061         t-worktree 1.0
1062
1063         cd example
1064
1065         t-dgit fetch
1066
1067         git checkout -b patch-queue/quilt-tip-2 patch-queue/quilt-tip
1068         gbp pq rebase
1069
1070         echo '/* some comment */' >>src.c
1071         git add src.c
1072         git commit -m 'Add a comment to an upstream file'
1073
1074         t-gbp-unapplied-pq2qc
1075
1076         t-commit 'some updates' 1.0-2
1077 }
1078
1079 t-gbp-example-prep () {
1080         t-gbp-example-prep-no-ff
1081
1082         t-git-pseudo-merge \
1083                 -m 'Pseudo-merge to make descendant of archive' \
1084                 remotes/dgit/dgit/sid
1085 }
1086
1087 t-make-badcommit () {
1088         badcommit=$(
1089                 git cat-file commit HEAD | \
1090                 perl -pe 's/^committer /commiter /' | \
1091                 git hash-object -w -t commit --stdin
1092         )
1093         t-expect-fsck-fail $badcommit
1094 }
1095
1096 t-make-orig () {
1097         # leaves ust set to filename of orig tarball
1098         local p=$1
1099         local v=$2
1100         local tag=${3-v$2}
1101         ust=${p}_${v}.orig.tar.gz
1102         GZIP=-1 git archive -o $bpd/$ust --prefix=${p}-${v}/ $tag
1103 }
1104
1105 t-merge-conflicted-stripping-conflict-markers () {
1106         local otherbranch=$1
1107         local file=$2
1108
1109         t-expect-fail F:"Merge conflict in $file" \
1110         git merge $otherbranch
1111
1112         perl -i~ -ne 'print unless m{^(?:\<\<\<|\>\>\>|===)}' "$file"
1113         git add "$file"
1114         git commit --no-edit
1115 }
1116
1117 t-commit () {
1118         local msg=$1
1119         v=${2:-${majorv:-1}.$revision}
1120         $troot/tstunt/debchange \
1121                 --force-distribution -v$v --distribution ${3:-unstable} "$1"
1122         git add debian/changelog
1123         debcommit
1124         revision=$(( ${revision-0} + 1 ))
1125 }
1126
1127 t-dch-r-rune () {
1128         local cmd="$1"; shift
1129         local suite=${1-unstable}
1130         $cmd -r -D "$suite" ''
1131 }
1132
1133 t-dch-commit-r () {
1134         t-dch-r-rune t-dch-commit "$@"
1135 }
1136
1137 t-dch-commit () {
1138         $troot/tstunt/debchange "$@"
1139         git commit -m "dch $*" debian/changelog
1140 }
1141
1142 t-git-config () {
1143         git config --global "$@"
1144 }
1145
1146 t-drs () {
1147  t-git-config dgit-distro.test-dummy.git-url "ext::$troot/drs-git-ext %S "
1148  t-git-config dgit-distro.test-dummy.git-check true
1149  t-git-config dgit-distro.test-dummy.git-create true
1150         cp $troot/gnupg/{dd.gpg,dm.gpg,dm.txt} $tmp/.
1151         cp $troot/suites $tmp/.
1152         cp $troot/suites $tmp/suites-master
1153
1154         export t_check_pushed_master=t-check-pushed-master
1155
1156         drs_dispatch=$tmp/distro=test-dummy
1157         mkdir $drs_dispatch
1158
1159         if [ "x$DGIT_TEST_INTREE" != x ]; then
1160                 ln -sf "$DGIT_TEST_INTREE" $drs_dispatch/dgit-live
1161         fi
1162
1163         ln -sf $tmp/git $drs_dispatch/repos
1164         ln -sf $tmp/suites $tmp/suites-master $tmp/dm.txt $drs_dispatch/
1165         mkdir -p $drs_dispatch/keyrings
1166         ln -sf $tmp/dd.gpg $drs_dispatch/keyrings/debian-keyring.gpg
1167         ln -sf $tmp/dm.gpg $drs_dispatch/keyrings/debian-maintainers.gpg
1168         ln -sf /bin/true $drs_dispatch/policy-hook
1169 }
1170
1171 t-dsd () {
1172         t-drs
1173  t-git-config dgit-distro.test-dummy.ssh "$troot/dsd-ssh"
1174  t-git-config dgit-distro.test-dummy.git-check ssh-cmd
1175  t-git-config dgit-distro.test-dummy.git-create true
1176  t-git-config dgit-distro.test-dummy.git-url \
1177                 "ext::$troot/dsd-ssh X %S /dgit/test-dummy/repos"
1178
1179  t-git-config dgit-distro.test-dummy.diverts.drs /drs
1180  t-git-config dgit-distro.test-dummy/drs.ssh "$troot/ssh"
1181  t-git-config dgit-distro.test-dummy/drs.git-url $tmp/git
1182  t-git-config dgit-distro.test-dummy/drs.git-check ssh-cmd
1183  t-git-config dgit-distro.test-dummy/drs.git-create ssh-cmd
1184
1185         echo 'no-such-package* drs' >$drs_dispatch/diverts
1186 }
1187
1188 t-policy-admin () {
1189         : '(((((((((((((((((((((((((((((((((((((((('
1190         ${DGIT_INFRA_PFX}dgit-repos-admin-debian --repos $tmp/git "$@"
1191         : '))))))))))))))))))))))))))))))))))))))))'
1192 }
1193
1194 t-policy-nonexist () {
1195         ln -sf no-such-file-or-directory $drs_dispatch/policy-hook
1196 }
1197
1198 t-make-hook-link () {
1199         local hook=$1 # in infra/
1200         local linkpath=$2
1201         hook=${DGIT_INFRA_PFX}$hook
1202         case $hook in
1203         */*)    ;;
1204         *)      hook=`type -P $hook` ;;
1205         esac
1206         ln -sf "$hook" $linkpath
1207 }
1208
1209 t-policy () {
1210         local policyhook=$1
1211         t-make-hook-link $policyhook $drs_dispatch/policy-hook
1212 }
1213
1214 t-debpolicy () {
1215         t-dsd
1216         t-policy dgit-repos-policy-debian
1217
1218         mkdir -p $tmp/git
1219         t-policy-admin create-db
1220 }
1221
1222 t-policy-periodic () {
1223         : '(((((((((((((((((((((((((((((((((((((((('
1224         ${DGIT_REPOS_SERVER_TEST-dgit-repos-server} \
1225                 test-dummy $drs_dispatch '' --cron
1226         : '))))))))))))))))))))))))))))))))))))))))'
1227 }
1228
1229 t-tagupl-settings () {
1230         export DGIT_DRS_EMAIL_NOREPLY=noreply@example.org
1231         export DGIT_DRS_SENDMAIL=$troot/tstunt/sendmail
1232         export DGIT_DRS_DGIT=$troot/tstunt/dgit
1233         t-chain-test-somehow
1234
1235         mkdir ../pretend-salsa
1236         (set -e; cd ../pretend-salsa; git init --bare)
1237         git remote add salsa $tmp/pretend-salsa
1238         # git branch --set-upstream-to complains, so
1239         git config branch.master.remote salsa
1240         git config branch.master.merge refs/heads/master
1241 }
1242
1243 t-tagupl-run-drs () {
1244         local source=$1
1245         cd ..
1246         cd tagupl
1247
1248         DGIT_DRS_ANY_URL=1 \
1249         DGIT_DRS_MODE="tag2upload $source $tagname" \
1250          $troot/drs-git-ext 
1251
1252         cd $tmp/$p
1253 }
1254
1255 t-tagupl-test () {
1256         ${DGIT_DEBPUSH_TEST-git debpush} \
1257                 --distro=test-dummy -u Senatus "$@"
1258
1259         mkdir ../tagupl
1260         t-tagupl-run-drs $tmp/pretend-salsa
1261
1262         cd ../pretend-salsa
1263         t-refs-same refs/heads/master
1264         cd ../$p
1265
1266         t-dgit fetch
1267 }
1268
1269 t-buildproductsdir-config () {
1270         bpd=$tmp/bpd
1271         t-git-config dgit.default.build-products-dir $bpd
1272         mkdir -p $bpd
1273         cat <<END >>$tmp/.gbp.conf
1274 [buildpackage]
1275 export-dir = $bpd
1276 END
1277 }
1278
1279 t-restrict () {
1280         local restriction=$1
1281         (cd $root; t-restriction-$restriction >&2)
1282 }
1283
1284 t-dependencies () {
1285         : "Hopefully installed: $*"
1286 }
1287
1288 t-chain-test-somehow () {
1289         export DGIT_TEST_TESTNAME="$testname"
1290         export DGIT_TEST_TMPBASE="$tmpbase"
1291         export ADTTMP=$tmp
1292 }
1293
1294 t-chain-test () {
1295         t-chain-test-somehow
1296         local ct=$1
1297         local d=${0%/*}
1298         cd $root
1299         exec "$d/$ct"
1300 }       
1301
1302 t-alt-test () {
1303         local t=${0##*/}
1304         t-${t%%-*}
1305         t-chain-test "${t#*-}"
1306 }
1307
1308 t-git-config dgit.default.old-dsc-distro test-dummy
1309
1310 for import in ${autoimport-gnupg}; do
1311         case "$0" in
1312         */$import) ;;
1313         *)
1314                 t-setup-import $import
1315                 ;;
1316         esac
1317 done