Lucio Andrés Illanes Albornoz aeeaa0
#
Lucio Andrés Illanes Albornoz aeeaa0
# set +o errexit -o noglob -o nounset is assumed.
Lucio Andrés Illanes Albornoz aeeaa0
#
Lucio Andrés Illanes Albornoz aeeaa0
Lucio Andrés Illanes Albornoz aeeaa0
rtlp_fetch_url_git() {
e9fa07
	local	_rpfug_cache_dname="${1}" _rpfug_git_args="${2}" _rpfug_git_branch="${3}"	\
e9fa07
		_rpfug_mirrors="${4}" _rpfug_pkg_name="${5}" _rpfug_subdir="${6}"		\
e9fa07
		_rpfug_tgtdir="${7}" _rpfug_url="${8}"						\
e9fa07
		_rpfug_cache_dname_full="" _rpfug_clonefl=0 _rpfug_dname=""			\
e9fa07
		_rpfug_git_pull_log_fname="" _rpfug_oldpwd="" _rpfug_url_base="";
Lucio Andrés Illanes Albornoz aeeaa0
e9fa07
	_rpfug_cache_dname_full="${_rpfug_cache_dname}/${_rpfug_subdir##*/}";
Lucio Andrés Illanes Albornoz aeeaa0
	(set -o errexit -o noglob -o nounset;
Lucio Andrés Illanes Albornoz aeeaa0
	rtl_flock_acquire 4 || exit "${?}";
e9fa07
	trap "rm -f \"${_rpfug_cache_dname_full%%[/]}.fetching\"" EXIT;
e9fa07
	if [ -e "${_rpfug_cache_dname_full}" ]; then
e9fa07
		(rtl_fileop cd "${_rpfug_cache_dname_full}" || exit 1;
e9fa07
		 _rpfug_git_pull_log_fname="$(mktemp)" || exit 1;
e9fa07
		 trap 'rm -f "${_rpfug_git_pull_log_fname}" 2>/dev/null' EXIT HUP INT TERM USR1 USR2;
e9fa07
		 if ! git pull ${_rpfug_git_args} origin "${_rpfug_git_branch:-main}" >"${_rpfug_git_pull_log_fname}" 2>&1; then
e9fa07
			if grep -q '^fatal: refusing to merge unrelated histories$' "${_rpfug_git_pull_log_fname}"; then
e9fa07
				cat "${_rpfug_git_pull_log_fname}"; printf "Detected forced push(es).\n";
e9fa07
			elif grep -q '^Automatic merge failed; fix conflicts and then commit the result.$' "${_rpfug_git_pull_log_fname}"; then
e9fa07
				cat "${_rpfug_git_pull_log_fname}"; printf "Detected forced push(es).\n"; git merge --abort;
Lucio Andrés Illanes Albornoz 438d60
			else
e9fa07
				cat "${_rpfug_git_pull_log_fname}"; exit 1;
Lucio Andrés Illanes Albornoz 438d60
			fi;
Lucio Andrés Illanes Albornoz 438d60
			while true; do
Lucio Andrés Illanes Albornoz 438d60
				printf "Attempting git-reset(1) --hard HEAD^ and git-pull(1)...\n";
Lucio Andrés Illanes Albornoz 438d60
				if ! git reset --hard "HEAD^"; then
Lucio Andrés Illanes Albornoz 438d60
					exit 1;
e9fa07
				elif git pull ${_rpfug_git_args} origin "${_rpfug_git_branch:-main}"; then
Lucio Andrés Illanes Albornoz 438d60
					exit 0;
Lucio Andrés Illanes Albornoz 438d60
				fi;
Lucio Andrés Illanes Albornoz 438d60
			done;
Lucio Andrés Illanes Albornoz 438d60
		 else
e9fa07
			cat "${_rpfug_git_pull_log_fname}"; exit 0;
Lucio Andrés Illanes Albornoz 438d60
		 fi;) || return 1;
e9fa07
		(rtl_fileop cd "${_rpfug_cache_dname_full}" &&\
Lucio Andrés Illanes Albornoz aeeaa0
			git submodule update) || return 1;
e9fa07
	else	if git clone ${_rpfug_git_args} -b "${_rpfug_git_branch:-main}" "${_rpfug_url}" "${_rpfug_cache_dname_full}"; then
e9fa07
			_rpfug_clonefl=1;
e9fa07
		elif [ "${_rpfug_mirrors}" = "skip" ]; then
Lucio Andrés Illanes Albornoz aeeaa0
			return 1;
e9fa07
		else	for _rpfug_url_base in ${_rpfug_mirrors}; do
e9fa07
				if git clone ${_rpfug_git_args} -b "${_rpfug_git_branch:-main}" "${_rpfug_url_base}/${_rpfug_pkg_name}/${_rpfug_subdir}" "${_rpfug_cache_dname_full}"; then
e9fa07
					_rpfug_clonefl=1; break;
Lucio Andrés Illanes Albornoz aeeaa0
				fi;
Lucio Andrés Illanes Albornoz aeeaa0
			done;
Lucio Andrés Illanes Albornoz aeeaa0
		fi;
e9fa07
		if [ "${_rpfug_clonefl}" -eq 0 ]; then
Lucio Andrés Illanes Albornoz aeeaa0
			return 1;
e9fa07
		else	if [ "${_rpfug_git_branch:+1}" = 1 ]; then
e9fa07
				(rtl_fileop cd "${_rpfug_cache_dname_full}" &&\
e9fa07
					git checkout "${_rpfug_git_branch}") || return 1;
Lucio Andrés Illanes Albornoz aeeaa0
			fi;
e9fa07
			(rtl_fileop cd "${_rpfug_cache_dname_full}" &&\
Lucio Andrés Illanes Albornoz aeeaa0
				git submodule update --init) || return 1;
Lucio Andrés Illanes Albornoz aeeaa0
		fi;
Lucio Andrés Illanes Albornoz aeeaa0
	fi;
e9fa07
	if [ "${_rpfug_cache_dname}" != "${_rpfug_tgtdir}" ]; then
e9fa07
		_rpfug_oldpwd="${PWD}"; rtl_fileop cd "${_rpfug_tgtdir}" || return 1;
e9fa07
		rtl_fileop rm "${_rpfug_tgtdir}/${_rpfug_subdir}" || return 1;
e9fa07
		_rpfug_dname="${_rpfug_tgtdir}/${_rpfug_subdir}"; rtl_dirname \$_rpfug_dname;
e9fa07
		if ! [ -e "${_rpfug_dname}" ]; then
e9fa07
			rtl_fileop mkdir "${_rpfug_dname}";
Lucio Andrés Illanes Albornoz aeeaa0
		fi;
e9fa07
		rtl_fileop cp "${_rpfug_cache_dname_full}" "${_rpfug_tgtdir}/${_rpfug_subdir}" || return 1;
e9fa07
		rtl_fileop cd "${_rpfug_oldpwd}" || return 1;
e9fa07
	fi) 4<>"${_rpfug_cache_dname_full%%[/]}.fetching";
Lucio Andrés Illanes Albornoz aeeaa0
Lucio Andrés Illanes Albornoz aeeaa0
	if [ "${?}" -eq 0 ]; then
Lucio Andrés Illanes Albornoz aeeaa0
		cd "$(pwd)";
Lucio Andrés Illanes Albornoz aeeaa0
	else
Lucio Andrés Illanes Albornoz aeeaa0
		return 1;
Lucio Andrés Illanes Albornoz aeeaa0
	fi;
Lucio Andrés Illanes Albornoz aeeaa0
};
Lucio Andrés Illanes Albornoz aeeaa0
Lucio Andrés Illanes Albornoz aeeaa0
rtl_fetch_mirror_urls_git() {
e9fa07
	local	_rfmug_git_args="${1}" _rfmug_tgtdir="${2}"				\
e9fa07
		_rfmug_dname="" _rfmug_rc=0 _rfmug_repo_dname="" _rfmug_subdir=""	\
e9fa07
		_rfmug_url="" _rfmug_url_spec=""; shift 2;
Lucio Andrés Illanes Albornoz aeeaa0
e9fa07
	for _rfmug_url_spec in "${@}"; do
e9fa07
		_rfmug_subdir="${_rfmug_url_spec%=*}"; _rfmug_subdir="${_rfmug_subdir##*/}"; _rfmug_url="${_rfmug_url_spec#*=}"; _rfmug_url="${_rfmug_url%@*}";
e9fa07
		_rfmug_repo_dname="${_rfmug_subdir}"; [ "${_rfmug_repo_dname%.git}" = "${_rfmug_repo_dname}" ] && _rfmug_repo_dname="${_rfmug_repo_dname}.git";
Lucio Andrés Illanes Albornoz aeeaa0
abd054
		_rfmug_dname="${_rfmug_tgtdir}"; rtl_dirname \$_rfmug_dname;
e9fa07
		if ! [ -e "${_rfmug_dname}" ]; then
e9fa07
			rtl_fileop mkdir "${_rfmug_dname}";
Lucio Andrés Illanes Albornoz aeeaa0
		fi;
Lucio Andrés Illanes Albornoz aeeaa0
		(set -o errexit -o noglob -o nounset;
Lucio Andrés Illanes Albornoz aeeaa0
		rtl_flock_acquire 4 || exit "${?}";
e9fa07
		trap "rm -f \"${_rfmug_tgtdir}/.fetching\"" EXIT;
e9fa07
		if [ -e "${_rfmug_tgtdir}/${_rfmug_repo_dname}" ]; then
e9fa07
			(rtl_fileop cd "${_rfmug_tgtdir}/${_rfmug_repo_dname}" && git fetch ${_rfmug_git_args} --all) || return 1;
e9fa07
		else	(rtl_fileop cd "${_rfmug_tgtdir}" && git clone ${_rfmug_git_args} --mirror "${_rfmug_url}" "${_rfmug_repo_dname}") || return 1;
e9fa07
		fi) 4<>"${_rfmug_tgtdir}/.fetching";
Lucio Andrés Illanes Albornoz aeeaa0
		if [ "${?}" -ne 0 ]; then
e9fa07
			_rfmug_rc=1;
Lucio Andrés Illanes Albornoz aeeaa0
		fi;
e9fa07
	done;
e9fa07
e9fa07
	return "${_rfmug_rc}";
Lucio Andrés Illanes Albornoz aeeaa0
};
Lucio Andrés Illanes Albornoz aeeaa0
Lucio Andrés Illanes Albornoz aeeaa0
rtl_fetch_urls_git() {
e9fa07
	local	_rfug_cache_dname="${1}" _rfug_git_args="${2}" _rfug_tgtdir="${3}"	\
e9fa07
		_rfug_pkg_name="${4}" _rfug_mirrors="${5}"				\
e9fa07
	       	_rfug_git_branch="" _rfug_subdir="" _rfug_url="" _rfug_url_spec=""; shift 5;
Lucio Andrés Illanes Albornoz aeeaa0
e9fa07
	for _rfug_url_spec in "${@}"; do
e9fa07
		_rfug_subdir="${_rfug_url_spec%=*}";
e9fa07
		_rfug_url="${_rfug_url_spec#*=}";
e9fa07
		_rfug_url="${_rfug_url%@*}";
e9fa07
		if [ "${_rfug_url_spec#*@}" != "${_rfug_url_spec}" ]; then
e9fa07
			_rfug_git_branch=${_rfug_url_spec#*@};
Lucio Andrés Illanes Albornoz aeeaa0
		fi;
Lucio Andrés Illanes Albornoz aeeaa0
		if ! rtlp_fetch_url_git	\
e9fa07
				"${_rfug_cache_dname}" "${_rfug_git_args}"	\
e9fa07
				"${_rfug_git_branch}" "${_rfug_mirrors}"	\
e9fa07
				"${_rfug_pkg_name}" "${_rfug_subdir}"		\
e9fa07
				"${_rfug_tgtdir}" "${_rfug_url}";
e9fa07
		then
Lucio Andrés Illanes Albornoz aeeaa0
			return 1;
Lucio Andrés Illanes Albornoz aeeaa0
		fi;
Lucio Andrés Illanes Albornoz aeeaa0
	done;
e9fa07
e9fa07
	return 0;
Lucio Andrés Illanes Albornoz aeeaa0
};
Lucio Andrés Illanes Albornoz aeeaa0
64844b
# vim:filetype=sh textwidth=0