Last active
May 31, 2018 04:00
-
-
Save anthonyfok/8d3a2909eb8c8211456b to your computer and use it in GitHub Desktop.
Bash script to automate much of the work of upgrading a Debian Go package
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Copyright © 2015–2018 Anthony Fok <foka@debian.org> | |
# | |
# SPDX-License-Identifier: Apache-2.0 | |
# | |
# Credit: http://stackoverflow.com/questions/3231804/in-bash-how-to-add-are-you-sure-y-n-to-any-command-or-alias | |
set -e | |
git pull --ff-only | |
gbp pull | |
if [ -n "$1" ]; then | |
GIT_REVISION="-git_revision $1" | |
fi | |
TMPDIR=/tmp/tmp-dh-make-golang | |
ARCH=$(dpkg-architecture -q DEB_BUILD_ARCH) | |
SRCPKG=$(grep ^Source: debian/control | cut -d' ' -f2) | |
GO_IMPORT_PATH=$(grep ^XS-Go-Import-Path: debian/control | cut -d' ' -f2) | |
OLD_VERSION=$(head -1 debian/changelog | sed -e 's/.*(\(.*\)).*/\1/') | |
#DH_MAKE_GOLANG=~/go/bin/dh-make-golang | |
DH_MAKE_GOLANG=/usr/bin/dh-make-golang | |
case $SRCPKG in | |
golang-github-pkg-sftp) | |
DH_MAKE_GOLANG=/usr/bin/dh-make-golang | |
;; | |
esac | |
echo Using $DH_MAKE_GOLANG to fetch our source | |
if [[ -z $SRCPKG || -z $GO_IMPORT_PATH || -z $OLD_VERSION ]]; then | |
echo Error: Cannot continue. | |
echo Source: \"$SRCPKG\" | |
echo XS-Go-Import-Path: \"$GO_IMPORT_PATH\" | |
echo Packaged version: \"$OLD_VERSION\" | |
echo Are you sure you are inside a Debian Go package directory? | |
exit 1 | |
fi | |
echo XS-Go-Import-Path: $GO_IMPORT_PATH | |
echo | |
echo "==================================================================" | |
echo " Fetch the latest upstream tarball" | |
echo "==================================================================" | |
rm -rf $TMPDIR | |
mkdir $TMPDIR | |
pushd $TMPDIR | |
if [ "x$SRCPKG" = 'xhugo' ]; then | |
$DH_MAKE_GOLANG make -type=program $GIT_REVISION $GO_IMPORT_PATH | |
SRCPKG_DH=${SRCPKG} | |
else | |
echo $DH_MAKE_GOLANG make -type=library $GIT_REVISION $GO_IMPORT_PATH | |
$DH_MAKE_GOLANG make -type=library $GIT_REVISION $GO_IMPORT_PATH | |
SRCPKG_DH=golang-${GO_IMPORT_PATH} | |
SRCPKG_DH=${SRCPKG_DH//\//-} | |
SRCPKG_DH=${SRCPKG_DH//_/-} | |
SRCPKG_DH=${SRCPKG_DH/.org/} | |
SRCPKG_DH=${SRCPKG_DH/.com/} | |
SRCPKG_DH=${SRCPKG_DH/gopkg.in/gopkg} | |
SRCPKG_DH=${SRCPKG_DH/google.golang/google} | |
# Converts to lowercase | |
SRCPKG_DH=${SRCPKG_DH,,} | |
fi | |
echo Canonical source package name: ${SRCPKG_DH} | |
ORIG_TARBALL_DH=$(echo ${SRCPKG_DH}_*.orig.tar.xz) | |
NEW_VERSION=$(head -1 ${SRCPKG_DH}/debian/changelog | sed -e 's/.*(\(.*\)).*/\1/') | |
CHANGES_FILE_DH=${ORIG_TARBALL_DH%.orig.tar.xz}-1_${ARCH}.changes | |
# Special case for packages that used 0.0+git instead of 0.0~git | |
case $SRCPKG in | |
golang-check.v1 | golang-yaml.v2) | |
SC_ORIG_TARBALL_DH=${ORIG_TARBALL_DH/_0.0~git/_0.0+git} | |
mv -vi $ORIG_TARBALL_DH $SC_ORIG_TARBALL_DH | |
NEW_VERSION=${NEW_VERSION/#0.0~git/0.0+git} | |
CHANGES_FILE_DH=${CHANGES_FILE_DH/_0.0~git/_0.0+git} | |
ORIG_TARBALL_DH=$SC_ORIG_TARBALL_DH | |
;; | |
esac | |
ORIG_TARBALL=${ORIG_TARBALL_DH/$SRCPKG_DH/$SRCPKG} | |
CHANGES_FILE=${CHANGES_FILE_DH/$SRCPKG_DH/$SRCPKG} | |
popd | |
# Remove epoch from old version | |
OLD_VERSION=${OLD_VERSION/#1:/} | |
echo | |
echo " Packaged version: $OLD_VERSION" | |
echo " Attempted version: $NEW_VERSION" | |
echo | |
if [ "x$SRCPKG" = "x$SRCPKG_DH" ]; then | |
echo PkgName: $SRCPKG | |
echo tarball: $ORIG_TARBALL | |
echo changes: $CHANGES_FILE | |
else | |
echo "PkgName: $SRCPKG_DH → $SRCPKG" | |
echo "tarball: $ORIG_TARBALL_DH" | |
echo " → $ORIG_TARBALL" | |
echo "changes: $CHANGES_FILE_DH" | |
echo " → $CHANGES_FILE" | |
fi | |
echo old ver: $OLD_VERSION | |
echo new ver: $NEW_VERSION | |
echo | |
if dpkg --compare-versions "$OLD_VERSION" ge "$NEW_VERSION" | |
then | |
echo "Error: The packaged is already the latest!" | |
echo | |
exit 1 | |
fi | |
echo | |
echo "==================================================================" | |
echo " Preparing to import the upstream tarball" | |
echo "==================================================================" | |
echo | |
mv -iv $TMPDIR/$ORIG_TARBALL_DH ../$ORIG_TARBALL | |
case $SRCPKG in | |
golang-github-hashicorp-go-multierror | \ | |
golang-golang-x-tools | \ | |
go-md2man | \ | |
golang-testify | \ | |
govendor) | |
echo Repacking upstream tarball... | |
echo | |
NEW_UPSTREAM_VERSION=${NEW_VERSION%-1} | |
NEW_UPSTREAM_VERSION=${NEW_UPSTREAM_VERSION#*:} | |
REPACKED_TARBALL=${ORIG_TARBALL%.orig.tar.xz}+ds.orig.tar.xz | |
XZ_OPT=-9e mk-origtargz --version $NEW_UPSTREAM_VERSION \ | |
--repack --repack-suffix +ds \ | |
--compression xz ../$ORIG_TARBALL | |
ORIG_TARBALL=$REPACKED_TARBALL | |
CHANGES_FILE=${CHANGES_FILE/-1_/+ds-1_} | |
;; | |
golang-github-muesli-smartcrop) | |
echo Repacking upstream tarball... | |
echo | |
NEW_UPSTREAM_VERSION=${NEW_VERSION%-1} | |
NEW_UPSTREAM_VERSION=${NEW_UPSTREAM_VERSION#*:} | |
REPACKED_TARBALL=${ORIG_TARBALL%.orig.tar.xz}+dfsg1.orig.tar.xz | |
XZ_OPT=-9e mk-origtargz --version $NEW_UPSTREAM_VERSION \ | |
--repack --repack-suffix +dfsg1 \ | |
--compression xz ../$ORIG_TARBALL | |
ORIG_TARBALL=$REPACKED_TARBALL | |
CHANGES_FILE=${CHANGES_FILE/-1_/+dfsg1-1_} | |
;; | |
*) | |
echo No repacking needed, continue... | |
echo | |
;; | |
esac | |
#gbp import-orig --sign-tags ../$ORIG_TARBALL | |
read -r -p "Are you sure you want to continue? [Y/n] " response | |
response=${response,,} # tolower | |
if [[ $response =~ ^(no|n)$ ]] | |
then | |
exit 0 | |
fi | |
gbp import-orig ../$ORIG_TARBALL | |
echo | |
echo "==================================================================" | |
echo " Edit the commit message" | |
echo "==================================================================" | |
read -r -p "Are you ready to commit debian/changelog now? [y/N] " response | |
response=${response,,} # tolower | |
if [[ $response =~ ^(yes|y)$ ]] | |
then | |
gbp dch --release --commit --auto --full | |
else | |
cat <<EOF | |
You still have to run these commands: | |
gbp dch --release --commit --auto --full | |
gbp buildpackage --git-pbuilder --git-sign-tags --git-tag | |
debc | less | |
lintian ../$CHANGES_FILE | |
debsign ../$CHANGES_FILE | |
gbp push | |
dput ../$CHANGES_FILE | |
EOF | |
fi | |
echo | |
echo "==================================================================" | |
echo " Ready to build the package" | |
echo "==================================================================" | |
read -r -p "Are you sure you want to continue? [y/N] " response | |
response=${response,,} # tolower | |
if [[ $response =~ ^(yes|y)$ ]] | |
then | |
gbp buildpackage --git-pbuilder --git-sign-tags --git-tag | |
debc | less | |
lintian ../$CHANGES_FILE | |
else | |
cat <<EOF | |
You still have to run these commands: | |
gbp buildpackage --git-pbuilder --git-sign-tags --git-tag | |
debc | less | |
lintian ../$CHANGES_FILE | |
debsign ../$CHANGES_FILE | |
gbp push | |
dput ../$CHANGES_FILE | |
EOF | |
fi | |
echo | |
echo "==================================================================" | |
echo " Ready to sign and upload the package" | |
echo "==================================================================" | |
read -r -p "Are you sure you want to continue? [y/N] " response | |
response=${response,,} # tolower | |
if [[ $response =~ ^(yes|y)$ ]] | |
then | |
debsign ../$CHANGES_FILE | |
gbp push | |
#http_proxy= HTTP_PROXY= https_proxy= HTTPS_PROXY= dput ../$CHANGES_FILE | |
#dput ssh-upload ../$CHANGES_FILE | |
dput ../$CHANGES_FILE | |
else | |
cat <<EOF | |
You still have to run these commands: | |
debsign ../$CHANGES_FILE | |
gbp push | |
dput ../$CHANGES_FILE | |
EOF | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment