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