Add 'portzap checkout'

A copy of the same sourcezap command ('sourcezap checkout')
This commit is contained in:
0x1eef 2024-05-21 22:24:36 -03:00
parent 8625d945b9
commit dbd97d9562
5 changed files with 79 additions and 56 deletions

View file

@ -13,8 +13,8 @@ can be installed into `/usr/ports/` by root.
This command should be run after installing portzap for
the first time:
# Add the portzap user, group and home directory.
# This command requires root privileges.
# Add the portzap user, group and home directory
# This command requires root privileges
# setup-portzap
### CLI: portzap
@ -27,6 +27,10 @@ the first time:
# This command is delegated to the '_portzap' user
$ portzap pull
# Checkout a branch other than the default: hardenedbsd/main
# This command is delegated to the '_portzap' user
$ portzap checkout
# Install /home/_portzap/ports/ into /usr/ports/
# This command requires root privileges
# portzap install
@ -35,20 +39,15 @@ the first time:
# This command requires root privileges
$ portzap rm
### ENVIRONMENT
* __$PORTZAP\_GITURL__ <br>
The URL to a git repository. <br>
Default: https://git.HardenedBSD.org/HardenedBSD/ports.git.
* __$PORTZAP\_BRANCH__ <br>
The git branch to clone and pull updates from. <br>
Default: hardenedbsd/main.
The URL to a git repository <br>
Default: https://git.HardenedBSD.org/HardenedBSD/ports.git
* __$PORTZAP\_INSTALLDIR__ <br>
The directory where the ports collection will be installed. <br>
Default: /usr/ports/.
The directory where the ports collection will be installed <br>
Default: /usr/ports/
## Install
@ -82,5 +81,5 @@ via git:
## License
[BSD Zero Clause](https://choosealicense.com/licenses/0bsd/). <br>
See [LICENSE](./LICENSE).
[BSD Zero Clause](https://choosealicense.com/licenses/0bsd/) <br>
See [LICENSE](./LICENSE)

View file

@ -4,9 +4,9 @@ set -e
##
# variables
localbase="${LOCALBASE:-/usr/local}"
defaultbranch="hardenedbsd/main"
gitdir="/home/_portzap/ports"
giturl="${PORTZAP_GITURL:-https://git.hardenedbsd.org/hardenedbsd/ports.git}"
branch="${PORTZAP_BRANCH:-hardenedbsd/main}"
installdir="${PORTZAP_INSTALLDIR:-/usr/ports}"
revision="${installdir}"/.portzap
libexec="${localbase}"/libexec/portzap
@ -40,11 +40,15 @@ done
case $1 in
"clone")
require_dependency "git doas"
"${libexec}"/portzap-clone "${giturl}" "${gitdir}" "${branch}"
"${libexec}"/portzap-clone "${giturl}" "${gitdir}" "${defaultbranch}"
;;
"pull")
require_dependency "git doas"
"${libexec}"/portzap-pull "${gitdir}" "${branch}"
"${libexec}"/portzap-pull "${gitdir}"
;;
"checkout")
require_dependency "git doas"
"${libexec}"/portzap-checkout "${gitdir}" "${2}"
;;
"rm")
"${libexec}"/portzap-rm "${gitdir}" "${installdir}"
@ -59,6 +63,7 @@ case $1 in
printf "Commands:\n"
printf " clone Clone the hardenedbsd ports tree\n"
printf " pull Pull updates from the hardenedbsd ports tree\n"
printf " checkout Checkout a branch other than the default\n"
printf " rm Remove /usr/ports/ and /home/_portzap/ports/\n"
printf " install Install the ports tree into /usr/ports/\n"
;;

View file

@ -0,0 +1,40 @@
#!/bin/sh
set -e
##
# variables
libexec=$(dirname "$0")
localbase=${LOCALBASE:-/usr/local}
git="${localbase}"/bin/git
mode="u=rwX,g=rX,o="
gitdir="$1"
branch="$2"
##
# functions
gitexec()
{
doas -n -u _portzap \
/bin/sh -c "umask ${mode}; ${git} ${1}"
}
##
# main
if ! "${libexec}"/isportzap-member; then
echo "[x] This command must be run by a member of the '_portzap' group"
exit 1
fi
if [ ! -e "${gitdir}/.git" ]; then
echo "[x] ${gitdir} is not a valid git repository."
echo "[x] Try: portzap clone"
exit 1
fi
set -x
cd "${gitdir}"
gitexec "fetch origin"
gitexec "checkout ${branch}" ||
gitexec "checkout -t origin/${branch}"
set +x
printf "current branch: %s\n" "${branch}"

View file

@ -7,7 +7,6 @@ libexec=$(dirname "$0")
localbase=${LOCALBASE:-/usr/local}
git="${localbase}"/bin/git
gitdir=$1
branch=$2
mode="u=rwX,g=rX,o="
##
@ -18,43 +17,21 @@ gitexec()
/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="${?}"
echo "[-] 'git checkout' exited with an error"
exit "${r}"
else
echo "[-] Done"
fi
set -e
}
##
# main
if ! "${libexec}"/isportzap-member; then
echo "[-] This command must be run by a member of the '_portzap' group"
echo "[x] This command must be run by a member of the '_portzap' group"
exit 1
fi
if [ ! -e "${gitdir}/.git" ]; then
set +x
echo "[-] ${gitdir} is not a valid git repository."
echo "[-] Try 'portzap clone'"
echo "[x] ${gitdir} is not a valid git repository."
echo "[x] Try 'portzap 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}"
cd "${gitdir}"
branch=$(gitexec "branch --show-current")
gitexec "pull --rebase origin ${branch}"

View file

@ -10,6 +10,8 @@ portzap clone
.br
portzap pull
.br
portzap checkout
.br
portzap install
.br
portzap rm
@ -33,6 +35,13 @@ This command is delegated to the '_portzap' user
Pull updates into /home/_portzap/ports/
.br
This command is delegated to the '_portzap' user
.br
.Pp
.Nm portzap checkout
.br
Checkout a branch other than the default: hardenedbsd/main
.br
This command is delegated to the '_portzap' user
.Pp
.Nm portzap install
.br
@ -55,13 +64,6 @@ The URL to a git repository
.br
Default: https://git.HardenedBSD.org/HardenedBSD/ports.git
.sp
.Nm PORTZAP_BRANCH
.br
The git branch to clone and pull updates from
.br
Default: hardenedbsd/main
.br
.sp
.Nm PORTZAP_INSTALLDIR
.br
The directory where the ports collection will be installed