Mark McLoughlin a3cf992
#!/bin/bash
Mark McLoughlin a3cf992
Mark McLoughlin a3cf992
# This script formats the patches from a git branch, adds them
Mark McLoughlin a3cf992
# to the current branch and updates the spec file
Mark McLoughlin a3cf992
Mark McLoughlin a3cf992
# To use it, do e.g.
Mark McLoughlin a3cf992
#
Mark McLoughlin a3cf992
#   $> git checkout master
Mark McLoughlin a3cf992
#   $> git remote add -f fedora-openstack git@github.com:fedora-openstack/nova.git
Mark McLoughlin a3cf992
#   $> git branch master-patches fedora-openstack/master
Mark McLoughlin a3cf992
#   $> ./update-patches.sh
Mark McLoughlin a3cf992
#
Mark McLoughlin a3cf992
# Now your left with a commit which updates the patches
Mark McLoughlin a3cf992
#
Mark McLoughlin a3cf992
# When you've pushed and built the package, don't forget to also
Mark McLoughlin a3cf992
# push the patches with e.g.
Mark McLoughlin a3cf992
#
Mark McLoughlin a3cf992
#   $> git push fedora-openstack +master-patches:master
Mark McLoughlin a3cf992
#
Mark McLoughlin a3cf992
57bbf4b
set -e # exit on failure
57bbf4b
045f6d7
git status -uno --porcelain | grep . && {
045f6d7
    echo "The repo is not clean. Aborting" >&2
045f6d7
    exit 1
045f6d7
}
045f6d7
837b979
filterdiff /dev/null || {
837b979
    echo "Please install patchutils" >&2
837b979
    exit 1
837b979
}
837b979
Mark McLoughlin a3cf992
spec=$(fedpkg gimmespec)
Mark McLoughlin a3cf992
branch=$(git branch | awk '/^\* / {print $2}')
Mark McLoughlin a3cf992
patches_branch="${branch}-patches"
Mark McLoughlin a3cf992
patches_base=$(awk -F '=' '/# patches_base/ { print $2 }' "${spec}")
Mark McLoughlin a3cf992
orig_patches=$(awk '/^Patch[0-9][0-9]*:/ { print $2 }' "${spec}")
Mark McLoughlin a3cf992
Mark McLoughlin a3cf992
#
Mark McLoughlin a3cf992
# Create a commit which removes all the patches
Mark McLoughlin a3cf992
#
Mark McLoughlin a3cf992
git rm ${orig_patches}
a520996
git commit --allow-empty -m "Updated patches from ${patches_branch}" ${orig_patches}
Mark McLoughlin a3cf992
Mark McLoughlin a3cf992
#
Mark McLoughlin a3cf992
# Check out the ${branch}-patches branch and format the patches
Mark McLoughlin a3cf992
#
Mark McLoughlin a3cf992
git checkout "${patches_branch}"
6ebdb36
new_patches=$(git format-patch --no-renames --no-signature -N "${patches_base}")
Mark McLoughlin a3cf992
Mark McLoughlin a3cf992
#
837b979
# Filter non dist files from the patches as otherwise
837b979
# `patch` will prompt/fail for the non existent files
837b979
#
837b979
for patch in $new_patches; do
837b979
    filterdiff -x '*/.*' $patch > $patch.$$
837b979
    mv $patch.$$ $patch
837b979
done
837b979
837b979
#
Mark McLoughlin a3cf992
# Switch back to the original branch and add the patches
Mark McLoughlin a3cf992
#
Mark McLoughlin a3cf992
git checkout "${branch}"
Mark McLoughlin a3cf992
git add ${new_patches}
Mark McLoughlin a3cf992
Mark McLoughlin a3cf992
#
Mark McLoughlin a3cf992
# Remove the Patch/%patch lines from the spec file
Mark McLoughlin a3cf992
#
Mark McLoughlin a3cf992
sed -i '/^\(Patch\|%patch\)[0-9][0-9]*/d' "${spec}"
Mark McLoughlin a3cf992
Mark McLoughlin a3cf992
#
Mark McLoughlin a3cf992
# Add a new set of Patch/%patch lines
Mark McLoughlin a3cf992
#
Mark McLoughlin a3cf992
patches_list=$(mktemp)
Mark McLoughlin a3cf992
patches_apply=$(mktemp)
Mark McLoughlin a3cf992
57bbf4b
trap "rm '${patches_list}' '${patches_apply}'" EXIT
57bbf4b
Mark McLoughlin a3cf992
i=1;
Mark McLoughlin a3cf992
for p in ${new_patches}; do
Mark McLoughlin a3cf992
    printf "Patch%.4d: %s\n" "${i}" "${p}" >> "${patches_list}"
Mark McLoughlin a3cf992
    printf "%%patch%.4d -p1\n" "${i}" >> "${patches_apply}"
Mark McLoughlin a3cf992
    i=$((i+1))
Mark McLoughlin a3cf992
done
Mark McLoughlin a3cf992
Mark McLoughlin a3cf992
sed -i -e "/# patches_base/ { N; r ${patches_list}" -e "}" "${spec}"
Mark McLoughlin a3cf992
sed -i -e "/%setup -q / { N; r ${patches_apply}" -e "}" "${spec}"
Mark McLoughlin a3cf992
Mark McLoughlin a3cf992
#
Mark McLoughlin a3cf992
# Update the original commit to include the new set of patches
Mark McLoughlin a3cf992
#
Mark McLoughlin a3cf992
git commit --amend -m "Updated patches from ${patches_branch}" "${spec}" ${new_patches}