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