chiark / gitweb /
dgit-maint-merge(7): Do not cross-reference nonexistent dgit-maint-rebase(7)
[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-or-is () {
202         # t-has-parent-or-is CHILD PARENT
203         local child=$1
204         local parent=$2
205         local parents=$(git show --pretty=format:' %P %H ' "$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 $tmp/incoming/*.orig*.tar* . ||:
593         ln -s $incoming_dsc .
594         ln -s ${incoming_dsc/.dsc/.debian.tar}* .
595         dpkg-source "$@" -x *.dsc
596         cd */.
597         git init
598         git fetch ../../$p "refs/tags/*:refs/tags/*"
599 }
600
601 t-splitbrain-pushed-good--checkprep () {
602         git add -Af .
603         git rm --cached -r --ignore-unmatch .pc
604 }
605
606 t-splitbrain-pushed-good--checkdiff () {
607         local tag=$1
608         t-splitbrain-pushed-good--checkprep
609         t-output "" git diff --stat --cached $tag
610 }
611
612 t-splitbrain-pushed-good-start () {
613         dep14tag=refs/tags/test-dummy/${v//\~/_}
614         dgittag=$(t-v-tag)
615         t-output "" git status --porcelain
616         t-ref-head
617         t-refs-same $dep14tag
618         (set -e; cd $dgitrepo; t-refs-same $dep14tag)
619         git merge-base --is-ancestor $dep14tag $dgittag
620
621         t-refs-same-start
622         t-ref-same refs/heads/split.p
623         case "$(t-git-get-ref refs/heads/split.b)" in
624         "$t_ref_val") ;;
625         "$(git rev-parse refs/heads/split.p^0)") ;;
626         "$(git rev-parse refs/heads/split.p^1)") ;;
627         *) fail 'bad b/p' ;;
628         esac
629         t-pushed-good-core
630
631         t-incoming-dsc
632
633         t-splitbrain-pushed-good--unpack
634         t-splitbrain-pushed-good--checkdiff $dgittag
635 }
636 t-splitbrain-pushed-good-end-made-dep14 () {
637         t-splitbrain-pushed-good--checkdiff $dep14tag
638         cd $tmp/$p
639 }
640
641 t-splitbrain-rm-gitignore-patch () {
642         perl -i -pe '
643                 next unless $_ eq "auto-gitignore\n";
644                 die if $counter++;
645                 chomp;
646                 rename "debian/patches/$_", "../t-auto-gitignore" or die $!;
647                 $_ = "";
648         ' debian/patches/series
649 }
650
651 t-gbp-pushed-good () {
652         local suite=${1:-sid}
653         t-splitbrain-pushed-good-start
654
655         # Right, now we want to check that the maintainer tree and
656         # the dgit tree differ in exactly the ways we expect.  We
657         # achieve this by trying to reconstruct the maintainer tree
658         # from the dgit tree.
659
660         # So, unpack it withut the patches applied
661         t-splitbrain-pushed-good--unpack --skip-patches
662
663         # dgit might have added a .gitignore patch, which we need to
664         # drop and remove
665         t-splitbrain-rm-gitignore-patch
666
667         # Now the result should differ only in non-debian/ .gitignores
668         t-splitbrain-pushed-good--checkprep
669         git diff --cached --name-only $dep14tag >../changed
670         perl -ne '
671                 next if !m#^debian/# && m#(^|/)\.gitignore#;
672                 die "$_ mismatch";
673         ' <../changed
674
675         # If we actually apply the gitignore patch by hand, it
676         # should be perfect:
677         if [ -f ../t-auto-gitignore ]; then
678                 patch --backup-if-mismatch -p1 -u <../t-auto-gitignore
679         fi
680
681         t-splitbrain-pushed-good-end-made-dep14
682 }
683
684 t-unapplied-pushed-good () {
685         t-splitbrain-pushed-good-start
686         t-splitbrain-pushed-good--unpack --skip-patches
687         t-splitbrain-pushed-good-end-made-dep14
688 }
689
690 t-dpm-pushed-good () {
691         t-splitbrain-pushed-good-start
692         t-splitbrain-pushed-good--unpack
693         t-splitbrain-rm-gitignore-patch
694         t-splitbrain-pushed-good-end-made-dep14
695 }
696
697 t-commit-build-push-expect-log () {
698         local msg=$1
699         local mpat=$2
700         t-commit "$msg"
701         t-dgit build
702         LC_MESSAGES=C \
703         t-dgit push --new 2>&1 |tee $tmp/push.log
704         t-grep-mpat "$mpat" $tmp/push.log
705 }
706
707 t-822-field () {
708         local file=$1
709         local field=$2
710         perl -e '
711                 use Dpkg::Control::Hash;
712                 my $h = new Dpkg::Control::Hash allow_pgp=>1;
713                 $h->parse(\*STDIN,"'"$file"'");
714                 my $val = $h->{"'$field'"},"\n";
715                 die "'"$file $field"'" unless defined $val;
716                 print $val,"\n";
717         ' <$file
718 }
719
720 t-stunt-envvar () {
721         local var=$1
722         local tstunt=$2
723         eval '
724                 case "$'$var'" in
725                 "$tstunt:"*)    ;;
726                 *":$tstunt:"*)  ;;
727                 "")             '$var'="$tstunt" ;;
728                 *)              '$var'="$tstunt:$'$var'" ;;
729                 esac
730                 export '$var'
731         '
732 }
733
734 t-tstunt () {
735         local tstunt=$tmp/tstunt
736         t-stunt-envvar PATH $tstunt
737         t-stunt-envvar PERLLIB $tstunt
738         local f
739         for f in "$@"; do
740                 f="./$f"
741                 local d="$tstunt/${f%/*}"
742                 mkdir -p $d
743                 ln -sf "$troot/tstunt/$f" "$d"/.
744         done
745 }
746
747 t-tstunt-parsechangelog () {
748         t-tstunt dpkg-parsechangelog Dpkg/Changelog/Parse.pm
749 }
750
751 t-incoming-dsc () {
752         local dsc=${p}_${v}.dsc
753         incoming_dsc=$tmp/incoming/$dsc
754 }
755
756 t-ref-dsc-dgit () {
757         t-incoming-dsc
758         local val=`t-822-field $incoming_dsc Dgit`
759         perl -e '$_=shift @ARGV; die "Dgit $_ ?" unless m/^\w+\b/;' "$val"
760         t-ref-same-val $incoming_dsc "$val"
761 }
762
763 t-apply-diff () {
764         local v1=$1
765         local v2=$2
766         (cd $troot/pkg-srcs;
767          debdiff ${p}_${v1}.dsc ${p}_${v2}.dsc || test $? = 1) \
768          | patch -p1 -u
769 }
770
771 t-gbp-unapplied-pq2qc () {
772         # does `gbp pq export'
773         # commits the resulting debian/patches on  qc/BRANCH
774         # leaves us on qc/BRANCH (eg "qc/quilt-tip"))
775         # qc/BRANCH is not fast-forwarding
776
777         gbp pq export
778
779         branch=`git symbolic-ref HEAD`
780         branch=${branch#refs/heads/}
781
782         case "$branch" in
783         */*) fail "unexpected branch $branch" ;;
784         esac
785
786         git branch -f qc/$branch
787         git checkout qc/$branch
788         git add debian/patches
789         git commit -m 'Commit patch queue'
790 }
791
792 t-git-pseudo-merge () {
793         # like   git merge -s ours
794         if [ ! "$git_pseuomerge_opts" ]; then
795                 if git merge --help \
796                  | grep -q allow-unrelated-histories; then
797                         git_pseuomerge_opts='--allow-unrelated-histories'
798                 fi
799                 git_pseuomerge_opts+=' -s ours'
800         fi
801         git merge $git_pseuomerge_opts "$@"
802 }
803
804 t-gbp-example-prep-no-ff () {
805         t-tstunt-parsechangelog
806         t-archive example 1.0-1
807         t-git-none
808         t-worktree 1.0
809
810         cd example
811
812         t-dgit fetch
813
814         git checkout -b patch-queue/quilt-tip-2 patch-queue/quilt-tip
815         gbp pq rebase
816
817         echo '/* some comment */' >>src.c
818         git add src.c
819         git commit -m 'Add a comment to an upstream file'
820
821         t-gbp-unapplied-pq2qc
822
823         t-commit 'some updates' 1.0-2
824 }
825
826 t-gbp-example-prep () {
827         t-gbp-example-prep-no-ff
828
829         t-git-pseudo-merge \
830                 -m 'Pseudo-merge to make descendant of archive' \
831                 remotes/dgit/dgit/sid
832 }
833
834 t-commit () {
835         local msg=$1
836         v=${2:-${majorv:-1}.$revision}
837         dch -v$v --distribution ${3:-unstable} "$1"
838         git add debian/changelog
839         debcommit
840         revision=$(( ${revision-0} + 1 ))
841 }
842
843 t-git-config () {
844         git config --global "$@"
845 }
846
847 t-drs () {
848         export DGIT_TEST_TROOT=$troot
849  t-git-config dgit-distro.test-dummy.git-url "ext::$troot/drs-git-ext %S "
850  t-git-config dgit-distro.test-dummy.git-check true
851  t-git-config dgit-distro.test-dummy.git-create true
852  t-git-config dgit-distro.test-dummy.dgit-tag-format new,old,maint
853         cp $troot/gnupg/{dd.gpg,dm.gpg,dm.txt} $tmp/.
854         cp $troot/suites $tmp/.
855         cp $troot/suites $tmp/suites-master
856
857         export t_check_pushed_master=t-check-pushed-master
858
859         drs_dispatch=$tmp/distro=test-dummy
860         mkdir $drs_dispatch
861
862         if [ "x$DGIT_TEST_INTREE" != x ]; then
863                 ln -sf "$DGIT_TEST_INTREE" $drs_dispatch/dgit-live
864         fi
865
866         ln -sf $tmp/git $drs_dispatch/repos
867         ln -sf $tmp/suites $tmp/suites-master $tmp/dm.txt $drs_dispatch/
868         mkdir -p $drs_dispatch/keyrings
869         ln -sf $tmp/dd.gpg $drs_dispatch/keyrings/debian-keyring.gpg
870         ln -sf $tmp/dm.gpg $drs_dispatch/keyrings/debian-maintainers.gpg
871         ln -sf /bin/true $drs_dispatch/policy-hook
872 }
873
874 t-newtag () {
875  export tagpfx=archive/test-dummy
876  t-git-config dgit-distro.test-dummy.dgit-tag-format new,maint
877 }
878 t-oldtag () {
879  export tagpfx=test-dummy
880  t-git-config dgit-distro.test-dummy.dgit-tag-format old
881 }
882
883 t-dsd () {
884         t-drs
885  t-git-config dgit-distro.test-dummy.ssh "$troot/dsd-ssh"
886  t-git-config dgit-distro.test-dummy.git-check ssh-cmd
887  t-git-config dgit-distro.test-dummy.git-create true
888  t-git-config dgit-distro.test-dummy.git-url \
889                 "ext::$troot/dsd-ssh X %S /dgit/test-dummy/repos"
890
891  t-git-config dgit-distro.test-dummy.diverts.drs /drs
892  t-git-config dgit-distro.test-dummy/drs.ssh "$troot/ssh"
893  t-git-config dgit-distro.test-dummy/drs.git-url $tmp/git
894  t-git-config dgit-distro.test-dummy/drs.git-check ssh-cmd
895  t-git-config dgit-distro.test-dummy/drs.git-create ssh-cmd
896
897         echo 'no-such-package* drs' >$drs_dispatch/diverts
898 }
899
900 t-policy-admin () {
901         ${DGIT_INFRA_PFX}dgit-repos-admin-debian --repos $tmp/git "$@"
902 }
903
904 t-policy-nonexist () {
905         ln -sf no-such-file-or-directory $drs_dispatch/policy-hook
906 }
907
908 t-make-hook-link () {
909         local hook=$1 # in infra/
910         local linkpath=$2
911         hook=${DGIT_INFRA_PFX}$hook
912         case $hook in
913         */*)    ;;
914         *)      hook=`type -P $hook` ;;
915         esac
916         ln -sf "$hook" $linkpath
917 }
918
919 t-policy () {
920         local policyhook=$1
921         t-make-hook-link $policyhook $drs_dispatch/policy-hook
922 }
923
924 t-debpolicy () {
925         t-dsd
926         t-policy dgit-repos-policy-debian
927
928         mkdir $tmp/git
929         t-policy-admin create-db
930 }
931
932 t-policy-periodic () {
933         ${DGIT_REPOS_SERVER_TEST-dgit-repos-server} \
934                 test-dummy $drs_dispatch '' --cron
935 }
936
937 t-restrict () {
938         local restriction=$1
939         (cd $root; t-restriction-$restriction >&2)
940 }
941
942 t-dependencies () {
943         : "Hopefully installed: $*"
944 }
945
946 t-chain-test () {
947         local ct=$1
948         local d=${0%/*}
949         cd $root
950         export DGIT_TEST_TESTNAME="$testname"
951         export DGIT_TEST_TMPBASE="$tmpbase"
952         export ADTTMP=$tmp
953         exec "$d/$ct"
954 }       
955
956 t-alt-test () {
957         local t=${0##*/}
958         t-${t%%-*}
959         t-chain-test "${t#*-}"
960 }
961
962 case "$0" in
963 */gnupg) ;;
964 *)      t-setup-import gnupg    ;;
965 esac