chiark / gitweb /
test suite: tagupl-baredebian: New test
[dgit.git] / git-debpush
1 #!/bin/bash
2
3 # git-debpush -- create & push a git tag with metadata for an ftp-master upload
4 #
5 # Copyright (C) 2019 Sean Whitton
6 #
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
20 set -e$DGIT_TEST_DEBPUSH_DEBUG
21 set -o pipefail
22
23 # DESIGN PRINCIPLES
24 #
25 # - do not invoke dgit, do anything involving any tarballs, no network
26 #   access except `git push` right at the end
27 #
28 # - do not look at the working tree, like `git push` `git tag`
29 #
30 # - we are always in split brain mode, because that fits this workflow,
31 #   and avoids pushes failing just because dgit in the intermediary
32 #   service wants to append commits
33 #
34 # - if there is no previous tag created by this script, require a quilt
35 #   mode; if there is a previous tag, and no quilt mode provided, assume
36 #   same quilt mode as in previous tag created by this script
37
38 # ---- Helper functions and variables
39
40 us="$(basename $0)"
41
42 cleanup() {
43     if [ -d "$temp" ]; then
44         rm -rf "$temp"
45     fi
46 }
47
48 fail () {
49     echo >&2 "$us: $*";
50     exit 127;
51 }
52
53 badusage () {
54     fail "bad usage: $*";
55 }
56
57 get_file_from_ref () {
58     local path=$1
59
60     if git ls-tree --name-only -r "$branch" \
61             | grep -Eq "^$path$"; then
62         git cat-file blob $branch:$path
63     fi
64 }
65
66 # ---- Parse command line
67
68 getopt=$(getopt -s bash -o 'nfu:' \
69               -l 'no-push,force,branch:,remote:,distro:,upstream:,quilt:,gbp,dpm,\
70 baredebian,baredebian+git,baredebian+tarball' \
71               -n "$us" -- "$@")
72 eval "set - $getopt"
73 set -e$DGIT_TEST_DEBPUSH_DEBUG
74
75 git_tag_opts=()
76 pushing=true
77 force=false
78 distro=debian
79 quilt_mode=""
80 branch="HEAD"
81
82 while true; do
83     case "$1" in
84         '-n'|'--no-push') pushing=false;           shift;   continue ;;
85         '-u')             git_tag_opts+=(-u "$2"); shift 2; continue ;;
86         '-f'|'--force')   force=true;              shift;   continue ;;
87         '--gbp')          quilt_mode='gbp';        shift;   continue ;;
88         '--dpm')          quilt_mode='dpm';        shift;   continue ;;
89         '--branch')       branch=$2;               shift 2; continue ;;
90         '--remote')       remote=$2;               shift 2; continue ;;
91         '--distro')       distro=$2;               shift 2; continue ;;
92         '--quilt')        quilt_mode=$2;           shift 2; continue ;;
93         '--upstream')     upstream_tag=$2;         shift 2; continue ;;
94
95         '--baredebian'|'--baredebian+git')
96             quilt_mode=baredebian;         shift; continue ;;
97         '--baredebian+tarball')
98             fail "--baredebian+tarball quilt mode not supported"
99             ;;
100
101         '--') shift; break ;;
102         *) badusage "unknown option $1" ;;
103     esac
104 done
105
106 if [ $# != 0 ]; then
107     badusage 'no positional arguments allowed'
108 fi
109
110 case "$quilt_mode" in
111     linear|auto|smash|nofix|gbp|dpm|unapplied|baredebian|'') ;;
112     baredebian+git) quilt_mode="baredebian" ;;
113     baredebian+tarball) fail "--baredebian+tarball quilt mode not supported" ;;
114     *) badusage "invalid quilt mode: $quilt_mode" ;;
115 esac
116
117 # ---- Gather git information
118
119 remoteconfigs=()
120 push_branch=()
121
122 # Maybe $branch is a symbolic ref.  If so, resolve it
123 branchref="$(git symbolic-ref -q $branch || test $? = 1)"
124 if [ "x$branchref" != "x" ]; then
125    branch="$branchref"
126 fi
127 # If $branch is the name of a branch but it does not start with
128 # 'refs/heads/', prepend 'refs/heads/', so that we can know later
129 # whether we are tagging a branch or some other kind of committish
130 case "$branch" in
131     refs/heads/*) ;;
132     *)
133         branchref="$(git for-each-ref --format='%(objectname)' \
134                          '[r]efs/heads/$branch')"
135         if [ "x$branchref" != "x" ]; then
136             branch="refs/heads/$branch"
137         fi
138         ;;
139 esac
140
141 # If our tag will point at a branch, push that branch, and add its
142 # pushRemote and remote to the things we'll check if the user didn't
143 # supply a remote
144 case "$branch" in
145     refs/heads/*)
146         b=${branch#refs/heads/}
147         push_branch+=("$b")
148         remoteconfigs+=( branch.$b.pushRemote branch.$b.remote )
149         ;;
150 esac
151
152 # also check, if the branch does not have its own pushRemote or
153 # remote, whether there's a default push remote configured
154 remoteconfigs+=(remote.pushDefault)
155
156 if $pushing && [ "x$remote" = "x" ]; then
157     for c in "${remoteconfigs[@]}"; do
158         remote=$(git config "$c" || test $? = 1)
159         if [ "x$remote" != "x" ]; then break; fi
160     done
161     if [ "x$remote" = "x" ]; then
162         fail "pushing, but could not determine remote, so need --remote="
163     fi
164 fi
165
166 # ---- Gather source package information
167
168 temp=$(mktemp -d)
169 trap cleanup EXIT
170 mkdir "$temp/debian"
171 git cat-file blob "$branch":debian/changelog >"$temp/debian/changelog"
172 version=$(cd $temp; dpkg-parsechangelog -SVersion)
173 source=$(cd $temp; dpkg-parsechangelog -SSource)
174 target=$(cd $temp; dpkg-parsechangelog -SDistribution)
175 rm -rf "$temp"
176 trap - EXIT
177
178 # ---- Useful sanity checks
179
180 if ! $force; then
181
182     if [ "$target" = "UNRELEASED" ]; then
183         fail "UNRELEASED changelog"
184     fi
185
186     # TODO additional checks we might do:
187     #
188     # - are we uploading to a different suite from the last tag
189     #   (e.g. unstable after experimental)?  user should pass option to
190     #   confirm
191     #
192     # - walking backwards from $branch, if there is an archive/ strictly
193     #   before we reach most recent debian/ tag, error, this might be a
194     #   push of the dgit view to the maintainer branch
195
196 fi
197
198 # ---- Create the git tag
199
200 format="$(get_file_from_ref debian/source/format)"
201 case "$format" in
202     '3.0 (quilt)')  upstream=true ;;
203     '3.0 (native)') upstream=false ;;
204     '1.0'|'')
205         if get_file_from_ref debian/source/options | grep '^-sn *$'; then
206             upstream=false
207         elif get_file_from_ref debian/source/options | grep '^-sk *$'; then
208             upstream=true
209         else
210             fail 'please see "SOURCE FORMAT 1.0" in git-debpush(1)'
211         fi
212         ;;
213     *)
214         fail "unsupported debian/source/format $format"
215         ;;
216 esac
217
218 upstream_info=""
219 if $upstream; then
220     if [ "x$upstream_tag" = x ]; then
221         upstream_tag=$(
222             set +e
223             git deborig --just-print --version="$version" \
224                            | head -n1
225             ps="${PIPESTATUS[*]}"
226             set -e
227             case "$ps" in
228                 "0 0"|"141 0") ;; # ok or SIGPIPE
229                 *" 0")
230                     echo >&2 \
231  "$us: git-deborig failed; maybe try $us --upstream=TAG"
232                     exit 0
233                     ;;
234                 *) exit 127; # presumably head will have complained
235             esac
236         )
237         if [ "x$upstream_tag" = x ]; then exit 127; fi
238     fi
239     upstream_committish=$(git rev-parse "refs/tags/${upstream_tag}"^{})
240     upstream_info=" upstream-tag=$upstream_tag upstream=$upstream_committish"
241 fi
242
243 # convert according to DEP-14 rules
244 git_version=$(echo $version | tr ':~' '%_' | sed 's/\.(?=\.|$|lock$)/.#/g')
245
246 debian_tag="$distro/$git_version"
247
248 # If the user didn't supply a quilt mode, look for it in a previous
249 # tag made by this script
250 if [ "x$quilt_mode" = "x" ] && [ "$format" = "3.0 (quilt)" ]; then
251     set +o pipefail             # perl will SIGPIPE git-log(1) here
252     tag=$(git log --pretty=format:'%D' --decorate=full "$branch" \
253         | perl -wne 'use Dpkg::Version;
254                 @pieces = split /, /, $_;
255                 @debian_tag_vs = sort {version_compare($b, $a)}
256                     map { m|tag: refs/tags/debian/(.+)| ? $1 : () } @pieces;
257                 if (@debian_tag_vs) { print "debian/$debian_tag_vs[0]\n"; exit }')
258     if [ "x$tag" != "x" ]; then
259         quilt_mode=$(git cat-file -p $(git rev-parse "$tag") \
260                          | perl -wne \
261                                 'm/^\[dgit.*--quilt=([a-z+]+).*\]$/;
262                                  if ($1) { print "$1\n"; exit }')
263     fi
264     set -o pipefail
265 fi
266
267 quilt_mode_text=""
268 if [ "$format" = "3.0 (quilt)" ]; then
269     if [ "x$quilt_mode" = "x" ]; then
270         echo >&2 "$us: could not determine the git branch layout"
271         echo >&2 "$us: please supply a --quilt= argument"
272         exit 1
273     else
274         quilt_mode_text=" --quilt=$quilt_mode"
275     fi
276 fi
277
278 git tag "${git_tag_opts[@]}" -s -F- "$debian_tag" "$branch" <<EOF
279 $source release $version for $target
280
281 [dgit distro=$distro split$quilt_mode_text]
282 [dgit please-upload$upstream_info]
283 EOF
284
285 # ---- Do a git push
286
287 if $pushing; then
288     # xxx when user can specify upstream_tag, must cope with spaces
289     git push "$remote" "${push_branch[@]}" $upstream_tag "$debian_tag"
290 fi