chiark / gitweb /
git-debpush: introduce and call fail_check
[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 failed_check=false
67 fail_check () {
68     if $force; then
69         echo >&2 "$us: warning: $*"
70     else
71         echo >&2 "$us: $*"
72         failed_check=true
73     fi
74 }
75
76 # ---- Parse command line
77
78 getopt=$(getopt -s bash -o 'nfu:' \
79               -l 'no-push,force,branch:,remote:,distro:,upstream:,quilt:,gbp,dpm,\
80 baredebian,baredebian+git,baredebian+tarball' \
81               -n "$us" -- "$@")
82 eval "set - $getopt"
83 set -e$DGIT_TEST_DEBPUSH_DEBUG
84
85 git_tag_opts=()
86 pushing=true
87 force=false
88 distro=debian
89 quilt_mode=""
90 branch="HEAD"
91
92 while true; do
93     case "$1" in
94         '-n'|'--no-push') pushing=false;           shift;   continue ;;
95         '-u')             git_tag_opts+=(-u "$2"); shift 2; continue ;;
96         '-f'|'--force')   force=true;              shift;   continue ;;
97         '--gbp')          quilt_mode='gbp';        shift;   continue ;;
98         '--dpm')          quilt_mode='dpm';        shift;   continue ;;
99         '--branch')       branch=$2;               shift 2; continue ;;
100         '--remote')       remote=$2;               shift 2; continue ;;
101         '--distro')       distro=$2;               shift 2; continue ;;
102         '--quilt')        quilt_mode=$2;           shift 2; continue ;;
103         '--upstream')     upstream_tag=$2;         shift 2; continue ;;
104
105         '--baredebian'|'--baredebian+git')
106             quilt_mode=baredebian;         shift; continue ;;
107         '--baredebian+tarball')
108             fail "--baredebian+tarball quilt mode not supported"
109             ;;
110
111         '--') shift; break ;;
112         *) badusage "unknown option $1" ;;
113     esac
114 done
115
116 if [ $# != 0 ]; then
117     badusage 'no positional arguments allowed'
118 fi
119
120 case "$quilt_mode" in
121     linear|auto|smash|nofix|gbp|dpm|unapplied|baredebian|'') ;;
122     baredebian+git) quilt_mode="baredebian" ;;
123     baredebian+tarball) fail "--baredebian+tarball quilt mode not supported" ;;
124     *) badusage "invalid quilt mode: $quilt_mode" ;;
125 esac
126
127 # ---- Gather git information
128
129 remoteconfigs=()
130 push_branch=()
131
132 # Maybe $branch is a symbolic ref.  If so, resolve it
133 branchref="$(git symbolic-ref -q $branch || test $? = 1)"
134 if [ "x$branchref" != "x" ]; then
135    branch="$branchref"
136 fi
137 # If $branch is the name of a branch but it does not start with
138 # 'refs/heads/', prepend 'refs/heads/', so that we can know later
139 # whether we are tagging a branch or some other kind of committish
140 case "$branch" in
141     refs/heads/*) ;;
142     *)
143         branchref="$(git for-each-ref --format='%(objectname)' \
144                          '[r]efs/heads/$branch')"
145         if [ "x$branchref" != "x" ]; then
146             branch="refs/heads/$branch"
147         fi
148         ;;
149 esac
150
151 # If our tag will point at a branch, push that branch, and add its
152 # pushRemote and remote to the things we'll check if the user didn't
153 # supply a remote
154 case "$branch" in
155     refs/heads/*)
156         b=${branch#refs/heads/}
157         push_branch+=("$b")
158         remoteconfigs+=( branch.$b.pushRemote branch.$b.remote )
159         ;;
160 esac
161
162 # also check, if the branch does not have its own pushRemote or
163 # remote, whether there's a default push remote configured
164 remoteconfigs+=(remote.pushDefault)
165
166 if $pushing && [ "x$remote" = "x" ]; then
167     for c in "${remoteconfigs[@]}"; do
168         remote=$(git config "$c" || test $? = 1)
169         if [ "x$remote" != "x" ]; then break; fi
170     done
171     if [ "x$remote" = "x" ]; then
172         fail "pushing, but could not determine remote, so need --remote="
173     fi
174 fi
175
176 # ---- Gather source package information
177
178 temp=$(mktemp -d)
179 trap cleanup EXIT
180 mkdir "$temp/debian"
181 git cat-file blob "$branch":debian/changelog >"$temp/debian/changelog"
182 version=$(cd $temp; dpkg-parsechangelog -SVersion)
183 source=$(cd $temp; dpkg-parsechangelog -SSource)
184 target=$(cd $temp; dpkg-parsechangelog -SDistribution)
185 rm -rf "$temp"
186 trap - EXIT
187
188 # ---- Useful sanity checks
189
190 if [ "$target" = "UNRELEASED" ]; then
191     fail_check "UNRELEASED changelog"
192 fi
193
194 # TODO additional checks we might do:
195 #
196 # - are we uploading to a different suite from the last tag
197 #   (e.g. unstable after experimental)?  user should pass option to
198 #   confirm
199 #
200 # - walking backwards from $branch, if there is an archive/ strictly
201 #   before we reach most recent debian/ tag, error, this might be a
202 #   push of the dgit view to the maintainer branch
203
204 if ! $force && $failed_check; then
205     fail "some checks failed; you can override with --force"
206 fi
207
208 # ---- Create the git tag
209
210 format="$(get_file_from_ref debian/source/format)"
211 case "$format" in
212     '3.0 (quilt)')  upstream=true ;;
213     '3.0 (native)') upstream=false ;;
214     '1.0'|'')
215         if get_file_from_ref debian/source/options | grep '^-sn *$'; then
216             upstream=false
217         elif get_file_from_ref debian/source/options | grep '^-sk *$'; then
218             upstream=true
219         else
220             fail 'please see "SOURCE FORMAT 1.0" in git-debpush(1)'
221         fi
222         ;;
223     *)
224         fail "unsupported debian/source/format $format"
225         ;;
226 esac
227
228 upstream_info=""
229 if $upstream; then
230     if [ "x$upstream_tag" = x ]; then
231         upstream_tag=$(
232             set +e
233             git deborig --just-print --version="$version" \
234                            | head -n1
235             ps="${PIPESTATUS[*]}"
236             set -e
237             case "$ps" in
238                 "0 0"|"141 0") ;; # ok or SIGPIPE
239                 *" 0")
240                     echo >&2 \
241  "$us: git-deborig failed; maybe try $us --upstream=TAG"
242                     exit 0
243                     ;;
244                 *) exit 127; # presumably head will have complained
245             esac
246         )
247         if [ "x$upstream_tag" = x ]; then exit 127; fi
248     fi
249     upstream_committish=$(git rev-parse "refs/tags/${upstream_tag}"^{})
250     upstream_info=" upstream-tag=$upstream_tag upstream=$upstream_committish"
251 fi
252
253 # convert according to DEP-14 rules
254 git_version=$(echo $version | tr ':~' '%_' | sed 's/\.(?=\.|$|lock$)/.#/g')
255
256 debian_tag="$distro/$git_version"
257
258 # If the user didn't supply a quilt mode, look for it in a previous
259 # tag made by this script
260 if [ "x$quilt_mode" = "x" ] && [ "$format" = "3.0 (quilt)" ]; then
261     set +o pipefail             # perl will SIGPIPE git-log(1) here
262     tag=$(git log --pretty=format:'%D' --decorate=full "$branch" \
263         | perl -wne 'use Dpkg::Version;
264                 @pieces = split /, /, $_;
265                 @debian_tag_vs = sort {version_compare($b, $a)}
266                     map { m|tag: refs/tags/debian/(.+)| ? $1 : () } @pieces;
267                 if (@debian_tag_vs) { print "debian/$debian_tag_vs[0]\n"; exit }')
268     if [ "x$tag" != "x" ]; then
269         quilt_mode=$(git cat-file -p $(git rev-parse "$tag") \
270                          | perl -wne \
271                                 'm/^\[dgit.*--quilt=([a-z+]+).*\]$/;
272                                  if ($1) { print "$1\n"; exit }')
273     fi
274     set -o pipefail
275 fi
276
277 quilt_mode_text=""
278 if [ "$format" = "3.0 (quilt)" ]; then
279     if [ "x$quilt_mode" = "x" ]; then
280         echo >&2 "$us: could not determine the git branch layout"
281         echo >&2 "$us: please supply a --quilt= argument"
282         exit 1
283     else
284         quilt_mode_text=" --quilt=$quilt_mode"
285     fi
286 fi
287
288 git tag "${git_tag_opts[@]}" -s -F- "$debian_tag" "$branch" <<EOF
289 $source release $version for $target
290
291 [dgit distro=$distro split$quilt_mode_text]
292 [dgit please-upload$upstream_info]
293 EOF
294
295 # ---- Do a git push
296
297 if $pushing; then
298     # xxx when user can specify upstream_tag, must cope with spaces
299     git push "$remote" "${push_branch[@]}" $upstream_tag "$debian_tag"
300 fi