Blame subr.rtl/rtl_fetch_git.subr

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() {
Lucio Andrés Illanes Albornoz 438d60
	local	_cache_dname="${1}" _git_args="${2}" _git_branch="${3}" _mirrors="${4}"		\
Lucio Andrés Illanes Albornoz 438d60
		_pkg_name="${5}" _subdir="${6}" _tgtdir="${7}" _url="${8}" _cache_dname_full=""	\
Lucio Andrés Illanes Albornoz 438d60
		_clonefl=0 _git_pull_log_fname="" _oldpwd="" _url_base="";
Lucio Andrés Illanes Albornoz aeeaa0
Lucio Andrés Illanes Albornoz aeeaa0
	_cache_dname_full="${_cache_dname}/${_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 "${?}";
Lucio Andrés Illanes Albornoz aeeaa0
	trap "rm -f \"${_cache_dname_full%%[/]}.fetching\"" EXIT;
Lucio Andrés Illanes Albornoz aeeaa0
	if [ -e "${_cache_dname_full}" ]; then
Lucio Andrés Illanes Albornoz 438d60
		(rtl_fileop cd "${_cache_dname_full}" || exit 1;
Lucio Andrés Illanes Albornoz 438d60
		 _git_pull_log_fname="$(mktemp)" || exit 1;
Lucio Andrés Illanes Albornoz 438d60
		 trap 'rm -f "${_git_pull_log_fname}" 2>/dev/null' EXIT HUP INT TERM USR1 USR2;
Lucio Andrés Illanes Albornoz 438d60
		 if ! git pull ${_git_args} origin "${_git_branch:-main}" >"${_git_pull_log_fname}" 2>&1; then
Lucio Andrés Illanes Albornoz 438d60
			if grep -q '^fatal: refusing to merge unrelated histories$' "${_git_pull_log_fname}"; then
Lucio Andrés Illanes Albornoz 438d60
				cat "${_git_pull_log_fname}"; printf "Detected forced push(es).\n";
Lucio Andrés Illanes Albornoz 438d60
			elif grep -q '^Automatic merge failed; fix conflicts and then commit the result.$' "${_git_pull_log_fname}"; then
Lucio Andrés Illanes Albornoz 438d60
				cat "${_git_pull_log_fname}"; printf "Detected forced push(es).\n"; git merge --abort;
Lucio Andrés Illanes Albornoz 438d60
			else
Lucio Andrés Illanes Albornoz 438d60
				cat "${_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;
Lucio Andrés Illanes Albornoz 438d60
				elif git pull ${_git_args} origin "${_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
Lucio Andrés Illanes Albornoz 438d60
			cat "${_git_pull_log_fname}"; exit 0;
Lucio Andrés Illanes Albornoz 438d60
		 fi;) || return 1;
Lucio Andrés Illanes Albornoz aeeaa0
		(rtl_fileop cd "${_cache_dname_full}" &&\
Lucio Andrés Illanes Albornoz aeeaa0
			git submodule update) || return 1;
Lucio Andrés Illanes Albornoz aeeaa0
	else	if git clone ${_git_args} -b "${_git_branch:-main}" "${_url}" "${_cache_dname_full}"; then
Lucio Andrés Illanes Albornoz aeeaa0
			_clonefl=1;
Lucio Andrés Illanes Albornoz aeeaa0
		elif [ "${_mirrors}" = "skip" ]; then
Lucio Andrés Illanes Albornoz aeeaa0
			return 1;
Lucio Andrés Illanes Albornoz aeeaa0
		else	for _url_base in ${_mirrors}; do
Lucio Andrés Illanes Albornoz 986162
				if git clone ${_git_args} -b "${_git_branch:-main}" "${_url_base}/${_pkg_name}/${_subdir}" "${_cache_dname_full}"; then
Lucio Andrés Illanes Albornoz aeeaa0
					_clonefl=1; break;
Lucio Andrés Illanes Albornoz aeeaa0
				fi;
Lucio Andrés Illanes Albornoz aeeaa0
			done;
Lucio Andrés Illanes Albornoz aeeaa0
		fi;
Lucio Andrés Illanes Albornoz aeeaa0
		if [ "${_clonefl}" -eq 0 ]; then
Lucio Andrés Illanes Albornoz aeeaa0
			return 1;
Lucio Andrés Illanes Albornoz aeeaa0
		else	if [ -n "${_git_branch}" ]; then
Lucio Andrés Illanes Albornoz aeeaa0
				(rtl_fileop cd "${_cache_dname_full}" &&\
Lucio Andrés Illanes Albornoz aeeaa0
					git checkout "${_git_branch}") || return 1;
Lucio Andrés Illanes Albornoz aeeaa0
			fi;
Lucio Andrés Illanes Albornoz aeeaa0
			(rtl_fileop cd "${_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;
Lucio Andrés Illanes Albornoz aeeaa0
	if [ "${_cache_dname}" != "${_tgtdir}" ]; then
Lucio Andrés Illanes Albornoz aeeaa0
		_oldpwd="${PWD}"; rtl_fileop cd "${_tgtdir}" || return 1;
Lucio Andrés Illanes Albornoz aeeaa0
		rtl_fileop rm "${_tgtdir}/${_subdir}" || return 1;
Lucio Andrés Illanes Albornoz aeeaa0
		if [ ! -e "$(rtl_dirname "${_tgtdir}/${_subdir}")" ]; then
Lucio Andrés Illanes Albornoz aeeaa0
			rtl_fileop mkdir "$(rtl_dirname "${_tgtdir}/${_subdir}")";
Lucio Andrés Illanes Albornoz aeeaa0
		fi;
Lucio Andrés Illanes Albornoz aeeaa0
		rtl_fileop cp "${_cache_dname_full}" "${_tgtdir}/${_subdir}" || return 1;
Lucio Andrés Illanes Albornoz aeeaa0
		rtl_fileop cd "${_oldpwd}" || return 1;
Lucio Andrés Illanes Albornoz aeeaa0
	fi) 4<>"${_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() {
Lucio Andrés Illanes Albornoz aeeaa0
	local _git_args="${1}" _tgtdir="${2}" _rc=0 _repo_dname="" _subdir="" _url="" _url_spec=""; shift 2;
Lucio Andrés Illanes Albornoz aeeaa0
Lucio Andrés Illanes Albornoz aeeaa0
	for _url_spec in "${@}"; do
Lucio Andrés Illanes Albornoz aeeaa0
		_subdir="${_url_spec%=*}"; _subdir="${_subdir##*/}"; _url="${_url_spec#*=}"; _url="${_url%@*}";
Lucio Andrés Illanes Albornoz 986162
		_repo_dname="${_subdir}"; [ "${_repo_dname%.git}" = "${_repo_dname}" ] && _repo_dname="${_repo_dname}.git";
Lucio Andrés Illanes Albornoz aeeaa0
Lucio Andrés Illanes Albornoz aeeaa0
		if [ ! -e "$(rtl_dirname "${_tgtdir}")" ]; then
Lucio Andrés Illanes Albornoz aeeaa0
			rtl_fileop mkdir "$(rtl_dirname "${_tgtdir}")";
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 "${?}";
Lucio Andrés Illanes Albornoz aeeaa0
		trap "rm -f \"${_tgtdir}/.fetching\"" EXIT;
Lucio Andrés Illanes Albornoz aeeaa0
		if [ -e "${_tgtdir}/${_repo_dname}" ]; then
Lucio Andrés Illanes Albornoz aeeaa0
			(rtl_fileop cd "${_tgtdir}/${_repo_dname}" && git fetch ${_git_args} --all) || return 1;
Lucio Andrés Illanes Albornoz 986162
		else	(rtl_fileop cd "${_tgtdir}" && git clone ${_git_args} --mirror "${_url}" "${_repo_dname}") || return 1;
Lucio Andrés Illanes Albornoz aeeaa0
		fi) 4<>"${_tgtdir}/.fetching";
Lucio Andrés Illanes Albornoz aeeaa0
		if [ "${?}" -ne 0 ]; then
Lucio Andrés Illanes Albornoz aeeaa0
			_rc=1;
Lucio Andrés Illanes Albornoz aeeaa0
		fi;
Lucio Andrés Illanes Albornoz aeeaa0
	done; return "${_rc}";
Lucio Andrés Illanes Albornoz aeeaa0
};
Lucio Andrés Illanes Albornoz aeeaa0
Lucio Andrés Illanes Albornoz aeeaa0
rtl_fetch_urls_git() {
Lucio Andrés Illanes Albornoz aeeaa0
	local	_cache_dname="${1}" _git_args="${2}" _tgtdir="${3}" _pkg_name="${4}" _mirrors="${5}"\
Lucio Andrés Illanes Albornoz aeeaa0
		_git_branch="" _subdir="" _url="" _url_spec=""; shift 5;
Lucio Andrés Illanes Albornoz aeeaa0
Lucio Andrés Illanes Albornoz aeeaa0
	for _url_spec in "${@}"; do
Lucio Andrés Illanes Albornoz aeeaa0
		_subdir="${_url_spec%=*}"; _url="${_url_spec#*=}"; _url="${_url%@*}";
Lucio Andrés Illanes Albornoz aeeaa0
		if [ "${_url_spec#*@}" != "${_url_spec}" ]; then
Lucio Andrés Illanes Albornoz aeeaa0
			_git_branch=${_url_spec#*@};
Lucio Andrés Illanes Albornoz aeeaa0
		fi;
Lucio Andrés Illanes Albornoz aeeaa0
		if ! rtlp_fetch_url_git	\
Lucio Andrés Illanes Albornoz aeeaa0
				"${_cache_dname}" "${_git_args}" "${_git_branch}" "${_mirrors}"\
Lucio Andrés Illanes Albornoz aeeaa0
				"${_pkg_name}" "${_subdir}" "${_tgtdir}" "${_url}"; then
Lucio Andrés Illanes Albornoz aeeaa0
			return 1;
Lucio Andrés Illanes Albornoz aeeaa0
		fi;
Lucio Andrés Illanes Albornoz aeeaa0
	done;
Lucio Andrés Illanes Albornoz aeeaa0
};
Lucio Andrés Illanes Albornoz aeeaa0
Lucio Andrés Illanes Albornoz aeeaa0
# vim:filetype=sh