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