sourcezap/libexec/srczap/srczap-pull

61 lines
1.2 KiB
Text
Raw Normal View History

2024-05-11 21:43:59 +02:00
#!/bin/sh
set -e
##
# variables
libexec=$(dirname "$0")
localbase=${LOCALBASE:-/usr/local}
git="${localbase}"/bin/git
gitdir=$1
branch=$2
mode="u=rwX,g=rX,o="
##
# functions
gitexec()
{
doas -n -u _srczap \
/bin/sh -c "umask ${mode}; ${git} ${1}"
}
change_branch()
{
set +e
remote=$1
branch=$2
echo "[-] Attempt to change branch: ${branch}"
gitexec "fetch ${remote} > /dev/null 2>&1"
if ! gitexec "checkout ${branch} > /dev/null 2>&1" ||
gitexec "checkout -t ${remote}/${branch} > /dev/null 2>&1"; then
r="${?}"
2024-05-11 23:52:45 +02:00
echo "[x] 'git checkout' exited with an error"
exit "${r}"
else
echo "[-] Done"
fi
set -e
}
##
# main
if ! "${libexec}"/issrczap-member; then
2024-05-11 23:52:45 +02:00
echo "[x] This command must be run by a member of the '_srczap' group"
exit 1
fi
if [ ! -e "${gitdir}/.git" ]; then
set +x
2024-05-11 23:52:45 +02:00
echo "[x] ${gitdir} is not a valid git repository."
echo "[x] Try 'srczap clone'"
exit 1
fi
cd "${gitdir}"
remote=$(gitexec "remote" | head -n1)
cbranch=$(gitexec "branch --show-current")
if [ "${cbranch}" != "${branch}" ]; then
change_branch "${remote}" "${branch}"
fi
set -x
gitexec "pull --rebase ${remote} ${branch}"