Files
cis/core/addAndCheckGitRepository.sh
T
2026-10-07 23:55:29 +02:00

104 lines
3.9 KiB
Bash
Executable File

#!/bin/bash
source /cis/core/base.module.sh
#WARNING: Used for core functionality in setup.sh
# DO NOT rename the script and test changes well!
function checkPermissions() {
local -r _FOLDER="${1:?"Missing first parameter FOLDER"}"
local -r _RIGHTS="${2:?"Missing second parameter RIGHTS"}"
local -r _TESTBRANCH="write-test-branch-$(date +%s)"
if [ -d "${_FOLDER}.git" ] && git -C "${_FOLDER}" push origin HEAD:refs/heads/${_TESTBRANCH} &> /dev/null; then
# Cleanup immediately
git -C "${_FOLDER}" push origin :${_TESTBRANCH} &> /dev/null
[ "${_RIGHTS}" == "writable" ] && return 0
else
[ "${_RIGHTS}" == "readonly" ] && return 0
fi
echo "FAIL: The rights of the repository are incorrect: ("$(readlink -f ${0})")" >&2
echo " - '${_FOLDER}' is not '${_RIGHTS}'" >&2
echo " - check the settings of gitea." >&2
return 1
}
function cloneOrPull() {
local -r _FOLDER="${1:?"Missing first parameter FOLDER"}"
local -r _REPOSITORY="${2:?"Missing second parameter REPOSITORY"}"
[ -d "${_FOLDER}.git" ] \
&& git -C "${_FOLDER}" pull &> /dev/null \
&& return 0
! [ -d "${_FOLDER}.git" ] \
&& GIT_SSH_COMMAND='ssh -o StrictHostKeyChecking=accept-new' git clone "${_REPOSITORY}" "${_FOLDER}" &> /dev/null \
&& return 0
echo "FAIL: The local repository is not updatable: ("$(readlink -f ${0})")" >&2
echo " - '${_FOLDER}'" >&2
echo " - check your network and the permissions in gitea." >&2
return 1
}
function printRepository() {
local -r _FOLDER="${1:?"Missing first parameter FOLDER"}"
local -r _CONFIGURED_REPOSITORY="$(git -C "${_FOLDER:?"Missing FOLDER"}" config --get remote.origin.url 2> /dev/null)"
local -r _SUGGESTED_REPOSITORY="${2}"
! [ -z "${_CONFIGURED_REPOSITORY}" ] \
&& echo "${_CONFIGURED_REPOSITORY}" \
&& return 0
while true; do
read -e -p "Enter ssh URL to clone Repository: " -i "${_SUGGESTED_REPOSITORY}" _REPOSITORY
echo "${_REPOSITORY}" | grep -F 'git@' &> /dev/null \
&& git ls-remote "${_REPOSITORY}" &> /dev/null \
&& echo "${_REPOSITORY:?"Missing REPOSITORY: e.g. ssh://git@your.domain.com/cis.git"}" \
&& return 0
done
echo "FAIL: The remote repository is not accessible: ("$(readlink -f ${0})")" >&2
echo " - '${_REPOSITORY}'" >&2
echo " - check the settings of gitea." >&2
return 1
}
# Note that an unprivileged user can use this script successfully,
# if no user has to be added to the host because it already exists.
function addAndCheckGitRepository() {
local -r _FOLDER="${1:?"Missing first parameter FOLDER"}"
local -r _RIGHTS="${2:?"Missing second parameter RIGHTS: (readonly, writable) "}"
local -r _REPOSITORY="$(printRepository "${_FOLDER}" "${3}")"
echo \
&& cloneOrPull "${_FOLDER}" "${_REPOSITORY:?"Missing REPOSITORY: e.g. ssh://git@your.domain.com/cis.git"}" \
&& checkPermissions "${_FOLDER}" "${_RIGHTS}" \
&& echo "SUCCESS: The git repository is usable. ("$(readlink -f ${0})")" \
&& echo " - remote repository: '${_REPOSITORY}'" \
&& echo " - local repository: '${_FOLDER}' (${_RIGHTS})" \
&& return 0
echo "FAIL: The repository is not functional: ("$(readlink -f ${0})")" >&2
echo " - remote repository: '${_REPOSITORY}'" >&2
echo " - local repository: '${_FOLDER}'" >&2
echo " - due to an error or insufficient rights or" >&2
echo " - one check failed." >&2
return 1
}
# sanitizes all parameters
base.set FOLDER "${1}" '^[-a-zA-Z0-9/:@._]*/$'
base.set RIGHTS "${2}" '^(readonly|writable)$'
base.set SUGGESTED_REPOSITORY "${3}" '^[-a-zA-Z0-9/:@._]*$' optional
addAndCheckGitRepository \
"${FOLDER:?"Missing FOLDER"}" \
"${RIGHTS:?"Missing RIGHTS"}" \
"${SUGGESTED_REPOSITORY}" \
&& exit 0
exit 1