Blame subr.rtl/rtl_fetch_git.subr

Lucio Andrés Illanes Albornoz aeeaa0
#
8d7a8a
# Copyright (c) 2016, 2017, 2018, 2019, 2020, 2021, 2022, 2023 LucĂ­a Andrea Illanes Albornoz <lucia@luciaillanes.de>
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
550c18
#
550c18
# rtlp_fetch_url_git() - fetch Git URL for item
550c18
# @_dlcachedir:		absolute pathname to download cache directory
550c18
# @_git_args:		optional argument string to pass to git(1)
550c18
# @_git_branch:		name of Git branch
550c18
# @_mirrors:		optional list of mirror base URLs
550c18
# @_name:		single item name
550c18
# @_target_subdir:	target subdirectory name
550c18
# @_target_dname:	target directory name
550c18
# @_url:		single Git URL
550c18
#
550c18
# Returns:		zero (0) on success, non-zero (>0) on failure
550c18
#
Lucio Andrés Illanes Albornoz aeeaa0
rtlp_fetch_url_git() {
550c18
	local	_rpfug_dlcachedir="${1}" _rpfug_git_args="${2}" _rpfug_git_branch="${3}"	\
550c18
		_rpfug_mirrors="${4}" _rpfug_name="${5}" _rpfug_target_subdir="${6}"		\
550c18
		_rpfug_target_dname="${7}" _rpfug_url="${8}"					\
550c18
		_rpfug_dlcachedir_full="" _rpfug_clonefl=0 _rpfug_dname=""			\
2c3a8f
		_rpfug_oldpwd="" _rpfug_url_base="";
Lucio Andrés Illanes Albornoz aeeaa0
550c18
	_rpfug_dlcachedir_full="${_rpfug_dlcachedir}/${_rpfug_target_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 "${?}";
550c18
	trap "rm -f \"${_rpfug_dlcachedir_full%%[/]}.fetching\"" EXIT;
2c3a8f
550c18
	if [ -e "${_rpfug_dlcachedir_full}" ]; then
550c18
		(rtl_fileop cd "${_rpfug_dlcachedir_full}" || exit 1;
2c3a8f
		 git pull --rebase ${_rpfug_git_args} origin "${_rpfug_git_branch:-main}";
2c3a8f
		 exit "${?}";) || return 1;
550c18
		(rtl_fileop cd "${_rpfug_dlcachedir_full}" &&\
8159ac
			git submodule update) || return 1;
d82a0f
	else
2c3a8f
		if git clone				\
2c3a8f
			${_rpfug_git_args}		\
2c3a8f
			-b "${_rpfug_git_branch:-main}"	\
2c3a8f
			"${_rpfug_url}"			\
2c3a8f
			"${_rpfug_dlcachedir_full}";
2c3a8f
		then
e9fa07
			_rpfug_clonefl=1;
e9fa07
		elif [ "${_rpfug_mirrors}" = "skip" ]; then
Lucio Andrés Illanes Albornoz aeeaa0
			return 1;
d82a0f
		else
2c3a8f
			for _rpfug_url_base in ${_rpfug_mirrors}; do
2c3a8f
				if git clone								\
2c3a8f
					${_rpfug_git_args}						\
2c3a8f
					-b "${_rpfug_git_branch:-main}"					\
2c3a8f
					"${_rpfug_url_base}/${_rpfug_name}/${_rpfug_target_subdir}"	\
2c3a8f
					"${_rpfug_dlcachedir_full}";
2c3a8f
				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;
2c3a8f
e9fa07
		if [ "${_rpfug_clonefl}" -eq 0 ]; then
Lucio Andrés Illanes Albornoz aeeaa0
			return 1;
e9fa07
		else	if [ "${_rpfug_git_branch:+1}" = 1 ]; then
550c18
				(rtl_fileop cd "${_rpfug_dlcachedir_full}" &&\
e9fa07
					git checkout "${_rpfug_git_branch}") || return 1;
Lucio Andrés Illanes Albornoz aeeaa0
			fi;
550c18
			(rtl_fileop cd "${_rpfug_dlcachedir_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;
2c3a8f
550c18
	if [ "${_rpfug_dlcachedir}" != "${_rpfug_target_dname}" ]; then
550c18
		_rpfug_oldpwd="${PWD}"; rtl_fileop cd "${_rpfug_target_dname}" || return 1;
550c18
		rtl_fileop rm "${_rpfug_target_dname}/${_rpfug_target_subdir}" || return 1;
550c18
		_rpfug_dname="${_rpfug_target_dname}/${_rpfug_target_subdir}"; rtl_dirname \$_rpfug_dname;
2c3a8f
e9fa07
		if ! [ -e "${_rpfug_dname}" ]; then
e9fa07
			rtl_fileop mkdir "${_rpfug_dname}";
Lucio Andrés Illanes Albornoz aeeaa0
		fi;
2c3a8f
550c18
		rtl_fileop cp "${_rpfug_dlcachedir_full}" "${_rpfug_target_dname}/${_rpfug_target_subdir}" || return 1;
e9fa07
		rtl_fileop cd "${_rpfug_oldpwd}" || return 1;
550c18
	fi) 4<>"${_rpfug_dlcachedir_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
550c18
#
550c18
# rtlp_fetch_mirror_urls_git() - setup mirror for Git URL(s)
550c18
# @_git_args:		optional argument string to pass to git(1)
550c18
# @_target_dname:	target directory name
550c18
# @...:			list of Git URLs
550c18
#
550c18
# Returns:		zero (0) on success, non-zero (>0) on failure
550c18
#
Lucio Andrés Illanes Albornoz aeeaa0
rtl_fetch_mirror_urls_git() {
550c18
	local	_rfmug_git_args="${1}" _rfmug_target_dname="${2}"				\
550c18
		_rfmug_dname="" _rfmug_rc=0 _rfmug_repo_dname="" _rfmug_target_subdir=""	\
e9fa07
		_rfmug_url="" _rfmug_url_spec=""; shift 2;
Lucio Andrés Illanes Albornoz aeeaa0
e9fa07
	for _rfmug_url_spec in "${@}"; do
2c3a8f
		_rfmug_target_subdir="${_rfmug_url_spec%=*}";
2c3a8f
		_rfmug_target_subdir="${_rfmug_target_subdir##*/}";
2c3a8f
		_rfmug_url="${_rfmug_url_spec#*=}";
2c3a8f
		_rfmug_url="${_rfmug_url%@*}";
2c3a8f
		_rfmug_repo_dname="${_rfmug_target_subdir}";
2c3a8f
2c3a8f
		if [ "${_rfmug_repo_dname%.git}" = "${_rfmug_repo_dname}" ]; then
2c3a8f
			_rfmug_repo_dname="${_rfmug_repo_dname}.git";
2c3a8f
		fi;
Lucio Andrés Illanes Albornoz aeeaa0
550c18
		_rfmug_dname="${_rfmug_target_dname}"; rtl_dirname \$_rfmug_dname;
e9fa07
		if ! [ -e "${_rfmug_dname}" ]; then
e9fa07
			rtl_fileop mkdir "${_rfmug_dname}";
Lucio Andrés Illanes Albornoz aeeaa0
		fi;
2c3a8f
Lucio Andrés Illanes Albornoz aeeaa0
		(set -o errexit -o noglob -o nounset;
Lucio Andrés Illanes Albornoz aeeaa0
		rtl_flock_acquire 4 || exit "${?}";
550c18
		trap "rm -f \"${_rfmug_target_dname}/.fetching\"" EXIT;
2c3a8f
550c18
		if [ -e "${_rfmug_target_dname}/${_rfmug_repo_dname}" ]; then
2c3a8f
			(rtl_fileop cd "${_rfmug_target_dname}/${_rfmug_repo_dname}" &&\
2c3a8f
			 git fetch ${_rfmug_git_args} --all) ||\
2c3a8f
			 return 1;
d82a0f
		else
2c3a8f
			(rtl_fileop cd "${_rfmug_target_dname}" &&\
2c3a8f
			 git clone ${_rfmug_git_args} --mirror "${_rfmug_url}" "${_rfmug_repo_dname}") ||\
2c3a8f
			 return 1;
550c18
		fi) 4<>"${_rfmug_target_dname}/.fetching";
2c3a8f
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
550c18
#
550c18
# rtl_fetch_url_git() - fetch Git URL(s) for item
550c18
# @_dlcachedir:		absolute pathname to download cache directory
550c18
# @_git_args:		optional argument string to pass to git(1)
550c18
# @_target_dname:	target directory name
550c18
# @_name:		single item name
550c18
# @_mirrors:		optional list of mirror base URLs
550c18
# @...:			list of Git URLs
550c18
#
550c18
# Returns:		zero (0) on success, non-zero (>0) on failure
550c18
#
Lucio Andrés Illanes Albornoz aeeaa0
rtl_fetch_urls_git() {
550c18
	local	_rfug_dlcachedir="${1}" _rfug_git_args="${2}" _rfug_target_dname="${3}"	\
550c18
		_rfug_name="${4}" _rfug_mirrors="${5}"					\
550c18
	       	_rfug_git_branch="" _rfug_target_subdir="" _rfug_url="" _rfug_url_spec="";
550c18
	shift 5;
Lucio Andrés Illanes Albornoz aeeaa0
e9fa07
	for _rfug_url_spec in "${@}"; do
550c18
		_rfug_target_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	\
550c18
				"${_rfug_dlcachedir}" "${_rfug_git_args}"	\
e9fa07
				"${_rfug_git_branch}" "${_rfug_mirrors}"	\
550c18
				"${_rfug_name}" "${_rfug_target_subdir}"	\
550c18
				"${_rfug_target_dname}" "${_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