52f24b
#!/bin/bash
52f24b
set -e
4aae85
set -u
52f24b
shopt -qu globstar
4aae85
shopt -qs expand_aliases
52f24b
export LC_COLLATE=C
52f24b
export LC_ALL=C
52f24b
31cddd
format_patch_ops="--no-signature --no-numbered --stat=80 --summary -O.git.diff.order --patience -l0"
4aae85
alias othergit="GIT_DIR=$PWD/.rhboot.git GIT_WORK_TREE=$PWD git"
bbce62
alias formatpatch="othergit format-patch $format_patch_ops"
4aae85
52f24b
usage()
52f24b
{
52f24b
	retcode=0
9fefee
	if [ -n "${1:-}" ]; then
52f24b
		exec 1>&2
52f24b
		retcode=$1
52f24b
	fi
31cddd
	echo usage: do-rebase [OPTIONS] [--dist RELEASEVER | RELEASEVER ] [GITREPO]
31cddd
	echo OPTIONS: --dist=RELEASEVER --repo=REPO --amend --nocommit --nobumpspec
52f24b
	exit $retcode
52f24b
}
52f24b
5c99bb
if ! git status | grep -q 'nothing to commit, working .* clean' ; then
52f24b
	echo "Working directory is not clean, cannot rebase." 1>&2
52f24b
	exit 1
52f24b
fi
52f24b
655e9e
gitrepo="git@github.com:rhboot/grub2.git"
52f24b
releasever=""
171351
amend=""
31cddd
commit=1
31cddd
bumpspec=1
52f24b
52f24b
declare -a savedargs
4aae85
savedargs=()
52f24b
while [ $# -gt 0 ]; do
52f24b
	case $1 in
52f24b
	--help|-h|--usage|-?|-u)
52f24b
		usage
52f24b
		;;
52f24b
	--dist=*)
52f24b
		releasever=${1##--dist=}
52f24b
		;;
52f24b
	--dist)
52f24b
		shift
52f24b
		releasever=$1
52f24b
		;;
655e9e
	--repo=*)
655e9e
		gitrepo=${1##--repo=}
655e9e
		;;
655e9e
	--repo)
655e9e
		shift
655e9e
		gitrepo=$1
655e9e
		;;
171351
	--amend)
171351
		amend="--amend"
171351
		;;
31cddd
	--commit)
31cddd
		commit=1
31cddd
		;;
31cddd
	--nocommit)
31cddd
		# --nocommit implies nobumpspec, but --bumpspec after it will
31cddd
		# force bumpspec to get run.
31cddd
		commit=0
31cddd
		bumpspec=0
31cddd
		;;
31cddd
	--bumpspec)
31cddd
		bumpspec=2
31cddd
		;;
31cddd
	--nobumpspec)
31cddd
		bumpspec=0
31cddd
		;;
52f24b
	*)
52f24b
		savedargs[${#savedargs[@]}]="$1"
52f24b
		;;
52f24b
	esac
52f24b
	shift
52f24b
done
52f24b
set -- "${savedargs[@]}"
52f24b
52f24b
if [ -z "${releasever}" -a $# -gt 0 ]; then
52f24b
	releasever=$1
52f24b
	shift
52f24b
else
52f24b
	dist=$(git status | grep "On branch" | cut -d\  -f3-)
52f24b
	if [ -z "$dist" ]; then
52f24b
		echo "Could not figure out distro release version" 1>&2
52f24b
		usage 1
52f24b
	fi
52f24b
	case "$(eval echo \${dist})" in
52f24b
	.fc*)
52f24b
		releasever=$(echo ${dist} | \
52f24b
			sed 's/^\.fc\([[:digit:]]\+\)$/fedora-\1/')
52f24b
		;;
52f24b
	.*)
52f24b
		releasever=$(echo $dist | \
52f24b
			sed 's/^\.\([[:alpha:]]\+\)\([[:digit:]]\+\)$/\1-\2/')
52f24b
		;;
4aae85
	rhel*)
4aae85
		releasever=${dist}
4aae85
		dist=.el${releasever##rhel-}
4aae85
		;;
52f24b
	esac
52f24b
	if [ -z "${releasever}" -o "${releasever}" = "${dist}" ]; then
52f24b
		echo "Could not figure out distro release version" 1>&2
52f24b
		usage 1
52f24b
	fi
52f24b
fi
52f24b
52f24b
if [ -z "$releasever" ]; then
52f24b
	echo "Could not figure out distro release version" 1>&2
52f24b
	usage 1
52f24b
fi
52f24b
655e9e
if [ -n "${1:-}" ]; then
655e9e
	gitrepo=$1
655e9e
	shift
655e9e
fi
655e9e
52f24b
if [ $# -ge 1 ]; then
52f24b
	echo "Unknown argument \"$1\"" 1>&2
52f24b
	usage 1
52f24b
fi
52f24b
4aae85
if [ ! -d $PWD/.rhboot.git ]; then
4aae85
	othergit init
4aae85
	if ! othergit remote add \
655e9e
		 rhboot $gitrepo \
4aae85
		  >/dev/null 2>&1 ; then
4aae85
		echo "Could not add remote: rhboot" 1>&2
4aae85
		exit 1
4aae85
	fi
4aae85
elif othergit remote show -n rhboot | grep -q "URL: github$" ; then
4aae85
	if ! othergit remote add \
655e9e
		 rhboot $gitrepo \
4aae85
		  >/dev/null 2>&1 ; then
4aae85
		echo "Could not add remote: rhboot" 1>&2
4aae85
		exit 1
4aae85
	fi
52f24b
fi
52f24b
4aae85
othergit fetch rhboot
4aae85
remote=$(othergit branch --list -r "rhboot/${releasever}" 2>/dev/null)
4aae85
if [ "${remote}" != "  rhboot/${releasever}" ]; then
52f24b
	echo branch \"${releasever}\" does not appear to exist 1>&2
52f24b
	exit 1
52f24b
fi
52f24b
52f24b
unset LC_ALL
52f24b
git rm -q 0*.patch
52f24b
4aae85
> grub.patches
bbce62
patches=$(formatpatch grub-2.02..refs/remotes/rhboot/${releasever})
52f24b
for x in $patches ; do
52f24b
	echo Patch$(echo ${x} | cut -d- -f1): ${x} >> grub.patches
52f24b
done
31cddd
if [[ -z "$amend" && ${bumpspec} -gt 0 ]] || [[ ${bumpspec} -ge 2 ]] ;  then
171351
    rpmdev-bumpspec -c "- Rebased to newer upstream for ${releasever}" grub2.spec
171351
fi
4aae85
git add 0*.patch grub2.spec grub.patches
31cddd
if [[ ${commit} -eq 1 ]] ; then
31cddd
  if [ -z "$amend" ]; then
31cddd
    fedpkg commit -s -c
31cddd
  else
31cddd
    git commit --amend
31cddd
  fi
171351
fi