Skip to content

Instantly share code, notes, and snippets.

@dosaboy
Last active January 29, 2016 18:09
Show Gist options
  • Save dosaboy/a9e5a453b8e6c91e4635 to your computer and use it in GitHub Desktop.
Save dosaboy/a9e5a453b8e6c91e4635 to your computer and use it in GitHub Desktop.
#!/bin/bash -eu
# Author:
# Edward Hope-Morley <opentastic@gmail.com>
#
# Description:
# Fetches package source and applies all debian/patches to current git
# working tree (ideally a clean branch created from associated build tag ).
#
# Each patch will be committed once applied successfully.
#
PATH_NOTE="this script must be run within the git repo you want to apply patches to."
ARG_SOURCE_NAME=
ARG_UBUNTU_VER=
usage ()
{
echo "USAGE: `basename $0` <package> -h -u -t <tag>"
}
if ! [ -d .git ]; then
echo "ERROR: $PATH_NOTE"
exit 1
fi
(($#)) || { usage; exit 1; }
while (($#)); do
case $1 in
-u)
sudo apt-get update
;;
-h)
usage
exit 0
;;
--version)
ARG_UBUNTU_VER="$2"
shift
;;
-v|--verbose)
set -x
;;
*)
ARG_SOURCE_NAME="$1" # e.g. nova
;;
esac
shift
done
if [ -z $ARG_UBUNTU_VER ]; then
echo "Fetching versions..."
ARG_UBUNTU_VER="`apt-cache policy $ARG_SOURCE_NAME| grep " Candidate: "| awk '{print $2}'`"
if [ -z "$ARG_UBUNTU_VER" ]; then
pkg="`apt-cache showsrc $ARG_SOURCE_NAME| grep -A1 Package-List:| tail -n 1| awk '{print $1}'`"
ARG_UBUNTU_VER="`apt-cache policy $pkg| grep " Candidate: "| awk '{print $2}'`"
fi
else
echo "Using supplied ubuntu package version '$ARG_UBUNTU_VER'"
fi
git_commit_header="Import from ubuntu $ARG_SOURCE_NAME package $ARG_UBUNTU_VER"
deb_ver=`echo $ARG_UBUNTU_VER| sed 's/[1:]*\(.*\)-.*/\1/'`
echo -e "Current debian version of $ARG_SOURCE_NAME is $deb_ver"
# Always use a fresh branch
branch_name="sru-sync-${deb_ver}-`date +%s`"
if `git branch -a| grep -q $branch_name`; then
echo "ERROR: a branch named '$branch_name' already exists"
fi
echo "Creating new git branch '$branch_name' from tag '$deb_ver'"
git checkout -b $branch_name $deb_ver
tmpdir=`mktemp -d`
echo -e "\nUsing tmpdir='$tmpdir' to download apt-source"
(
cd $tmpdir
apt-get source $ARG_SOURCE_NAME=$ARG_UBUNTU_VER
)
patches_path=$tmpdir/${ARG_SOURCE_NAME}-${deb_ver}/debian/patches
series_path=$patches_path/series
echo -e "\nApplying patches from $patches_path to current working tree"
while read -r patch; do
if [ '#' = "${patch:0:1}" ]; then
continue
fi
echo "Applying $patch"
git apply < $patches_path/$patch
git commit -a -m "$git_commit_header
- d/p/$patch"
done < $series_path
# Cleanup
rm -rf $tmpdir
echo -e "\nDone."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment