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