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