branch 2.87 KB
#!/bin/bash
# 2003-2010, 2012, 2015, 2017 (c) Etersoft www.etersoft.ru
# Author: Vitaly Lipatov <lav@etersoft.ru>
# Public domain

# used: BINARYREPO MDISTR
# set: USEBRANCH OLDBRANCH
set_usebranch()
{
	#local OLDBRANCH
	#[ -f "$LISTNAMES" ] || fatal "Run with one spec inside gear repo"
	USEBRANCH=$BINARYREPO

	[ -n "$USEBRANCH" ] || return 1
    [ "$USEBRANCH" = "$(get_altdistr_mod "$DISTRVERSION")" ] && return 1

	# TODO: drop support of the legacy
	# support for obsoleted M?? names
	OLDBRANCH=$MDISTR

	if [ "$OLDBRANCH" != "$USEBRANCH" ] ; then
		if is_exist_branch $OLDBRANCH ; then
			echo "Exists $OLDBRANCH branch, will use it instead $USEBRANCH"
			USEBRANCH=$OLDBRANCH
		fi
	fi

	return 0
}

# used: USEBRANCH OLDBRANCH BPSPEC
# set: CURBRANCH
set_currentbranch()
{
	CURBRANCH=$(get_current_branch)

	local cb
	cb=$(cat $(get_root_git_dir)/.git/.rpmbph.current 2>/dev/null)
	if [ -n "$cb" ] ; then
		echog "Autorestore from broken build..."
		docmd git checkout $cb || fatal
		CURBRANCH=$cb
		rm -fv $(get_root_git_dir)/.git/.rpmbph.current
		#rm -fv $BPSPEC $BPSPEC.*
		[ -n "$BPSPEC" ] && rm -fv $BPSPEC.*
	fi

	if [ "$CURBRANCH" = "$USEBRANCH" ] || [ "$CURBRANCH" = "$OLDBRANCH" ] ; then
		fatal "You are already in backported branch $CURBRANCH. Run rpmbph in sisyphus or master branch only."
	fi
}

# used: USEBRANCH
checkout_bpbranch()
{
	# TODO
	[ -n "$CURBRANCH" ] || fatal "use assert in checkout_bpbranch"

	echo "$CURBRANCH" > $(get_root_git_dir)/.git/.rpmbph.current

	local PGEAR="gear"
	# Create branch if not exist yet
	if is_exist_branch $USEBRANCH ; then
		docmd git checkout $USEBRANCH || fatal "Can't checkout branch $USEBRANCH. Use $BPSPEC manually or remove it."
		if ! git branch -a | grep -q "remotes/$PGEAR/" ; then
			docmd gremote $GIRARHOST -o
			docmd git fetch $PGEAR $USEBRANCH
		fi
		# if we successful with gear
		if git branch -a | grep -q "remotes/$PGEAR/$USEBRANCH" ; then
			docmd git fetch $PGEAR $USEBRANCH
			docmd git merge --ff-only $PGEAR/$USEBRANCH || fatal
		fi
	else
		info "Creating branch $USEBRANCH..."
		if ! git branch -a | grep -q "remotes/$PGEAR/$USEBRANCH" ; then
			# if we have no fetched branch
			docmd gremote $GIRARHOST -o
			docmd git fetch $PGEAR $USEBRANCH
		fi
		# if we successful with gear
		if git branch -a | grep -q "remotes/$PGEAR/$USEBRANCH" ; then
			docmd git fetch $PGEAR $USEBRANCH
			docmd git checkout -b $USEBRANCH $PGEAR/$USEBRANCH || fatal
		else
			docmd git checkout -b $USEBRANCH || fatal
		fi
	fi
}

# used: USEBRNACH
# set: CURBRANCH
checkout_usebranch()
{
	CURBRANCH=$(get_current_branch)

	if [ "$USEBRANCH" = "$CURBRANCH" ] ; then
		# let's think we called from rpmbph
		return
	fi

	checkout_bpbranch
}

checkout_original_branch()
{
	local CURBRANCH
	CURBRANCH=$(cat $(get_root_git_dir)/.git/.rpmbph.current 2>/dev/null)
	[ -z "$CURBRANCH" ] && return

	rm -f $(get_root_git_dir)/.git/.rpmbph.current
	docmd git checkout $CURBRANCH
}