a0fe45f
#!/bin/bash
a0fe45f
# Usage:
a0fe45f
#    ./otp-get-patches.sh /path/to/otp OTP_R14B02 fedora-R14B02
a0fe45f
#
a0fe45f
# otp-get-patches.sh - update erlang.spec and otp-00*.patch files
a0fe45f
#
a0fe45f
# otp-get-patches.sh updates the erlang.spec and otp-00*.patch
a0fe45f
# files in the git index. After an otp-get-patches.sh run, you
Hans Ulrich Niedermann e7421b9
# will need to review the staged git changes, possibly adapt the
Hans Ulrich Niedermann e7421b9
# 'Release:' and '%changelog' parts of erlang.spec, and can then
a0fe45f
# "git commit" everything.
a0fe45f
#
a0fe45f
# Caution: Leave the four special comment lines untouched in the
a0fe45f
# spec file, as otp-get-patches.sh requires them and will only
Hans Ulrich Niedermann e7421b9
# touch the parts of erlang.spec between the respective start/end
Hans Ulrich Niedermann e7421b9
# comment pair:
a0fe45f
#
a0fe45f
# # start of autogenerated patch tag list
a0fe45f
# # end of autogenerated patch tag list
a0fe45f
# # start of autogenerated prep patch list
a0fe45f
# # end of autogenerated prep patch list
a0fe45f
#
a0fe45f
# The following special comment lines in the git commit messages
a0fe45f
# will be interpreted:
a0fe45f
#
a0fe45f
#    Fedora-Spec-Comment: This patch only applies to EL6 builds
a0fe45f
#    Fedora-Spec-Before: %if 0%?el6}
a0fe45f
#    Fedora-Spec-After: %endif
a0fe45f
#
a0fe45f
# If there is no "Fedora-Spec-Comment:" line, we will use
a0fe45f
# "Fedora specific patch".
a0fe45f
a0fe45f
# Command line parsing
a0fe45f
otp_dir="${1:?'Fatal: otp git repo dir required'}"
a0fe45f
otp_upstream="${2:?'Fatal: git ref to upstream release required'}"
a0fe45f
otp_fedora="${3:?'Fatal: git ref to branch with fedora patches required'}"
a0fe45f
a0fe45f
# Setup
a0fe45f
set -e
a0fe45f
# set -x
a0fe45f
tmpdir="$(mktemp -d --tmpdir="$PWD")"
a0fe45f
a0fe45f
# Generate patch files
a0fe45f
pushd "$otp_dir"
2845f91
git format-patch -N --no-signature --no-stat -o "$tmpdir" "${otp_upstream}..${otp_fedora}" > "$tmpdir/patch-list.txt"
a0fe45f
popd
a0fe45f
a0fe45f
test -s "$tmpdir/patch-list.txt"
a0fe45f
a0fe45f
# Process patch files
a0fe45f
echo "# start of autogenerated patch tag list" > "$tmpdir/patch-list-tags.txt"
a0fe45f
echo "# start of autogenerated prep patch list" > "$tmpdir/patch-list-prep.txt"
a0fe45f
n=1
a0fe45f
while read patch
a0fe45f
do
a0fe45f
	otppatch="$(dirname "$patch")/otp-$(basename "$patch")"
a0fe45f
	${SED-sed} -e '1d' -e '/^-- $/,$d' "$patch" > "$otppatch"
a0fe45f
	rm -f "$patch"
a0fe45f
	comment="$(sed -n 's/^Fedora-Spec-Comment:\s*//p' "$otppatch")"
a0fe45f
	if test "x$comment" = "x"; then comment="Fedora specific patch"; fi
a0fe45f
	echo "# ${comment}" >> "$tmpdir/patch-list-tags.txt"
a0fe45f
	echo "#   $(sed -n 's/^Subject: \[PATCH\] //p' "$otppatch")" >> "$tmpdir/patch-list-tags.txt"
a0fe45f
	echo "Patch$n: $(basename "$otppatch")" >> "$tmpdir/patch-list-tags.txt"
a0fe45f
	base="$(basename "$patch" ".patch" | sed 's/^00[0-9][0-9]-//')"
a0fe45f
	backupext=".$(echo -n "$base" | tr -c -s '[:alnum:]' '_')"
a0fe45f
	sed -n 's/^Fedora-Spec-Before:\s*//p' "$otppatch" >> "$tmpdir/patch-list-prep.txt"
a0fe45f
	echo "%patch$n -p1 -b ${backupext}" >> "$tmpdir/patch-list-prep.txt"
a0fe45f
	sed -n 's/^Fedora-Spec-After:\s*//p' "$otppatch" >> "$tmpdir/patch-list-prep.txt"
a0fe45f
	n=$(($n + 1))
a0fe45f
done < "$tmpdir/patch-list.txt"
a0fe45f
echo "# end of autogenerated patch tag list" >> "$tmpdir/patch-list-tags.txt"
a0fe45f
echo "# end of autogenerated prep patch list" >> "$tmpdir/patch-list-prep.txt"
a0fe45f
a0fe45f
# Create updated spec file
a0fe45f
specfile="erlang.spec"
a0fe45f
newspec1="${tmpdir}/${specfile}.new1"
a0fe45f
newspec2="${tmpdir}/${specfile}.new2"
a0fe45f
sed '/^# start of autogenerated patch tag list$/,$d' "$specfile" > "$newspec1"
a0fe45f
cat "$tmpdir/patch-list-tags.txt" >> "$newspec1"
a0fe45f
sed '1,/^# end of autogenerated patch tag list/d' "$specfile" >> "$newspec1"
a0fe45f
sed '/^# start of autogenerated prep patch list$/,$d' "$newspec1" > "$newspec2"
a0fe45f
cat "$tmpdir/patch-list-prep.txt" >> "$newspec2"
a0fe45f
sed '1,/^# end of autogenerated prep patch list/d' "$newspec1" >> "$newspec2"
a0fe45f
a0fe45f
# Actually put all changes into git index
a0fe45f
git rm -f otp-00*.patch
a0fe45f
mv "$tmpdir/otp-00"*.patch .
a0fe45f
git add otp-00*.patch
a0fe45f
mv -f "$newspec2" "$specfile"
a0fe45f
git add "$specfile"
a0fe45f
a0fe45f
rm -rf "$tmpdir"
a0fe45f
# End of file.