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