From dbd97d956289adaf9a38ec52ba1099caf3543080 Mon Sep 17 00:00:00 2001
From: 0x1eef <0x1eef@protonmail.com>
Date: Tue, 21 May 2024 22:24:36 -0300
Subject: [PATCH] Add 'portzap checkout'
A copy of the same sourcezap command ('sourcezap checkout')
---
README.md | 25 ++++++++++----------
bin/portzap | 19 +++++++++------
libexec/portzap/portzap-checkout | 40 ++++++++++++++++++++++++++++++++
libexec/portzap/portzap-pull | 35 +++++-----------------------
man/man8/portzap.8 | 16 +++++++------
5 files changed, 79 insertions(+), 56 deletions(-)
create mode 100644 libexec/portzap/portzap-checkout
diff --git a/README.md b/README.md
index c11a3c1..e6b2e4b 100644
--- a/README.md
+++ b/README.md
@@ -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__
- The URL to a git repository.
- Default: https://git.HardenedBSD.org/HardenedBSD/ports.git.
-
-* __$PORTZAP\_BRANCH__
- The git branch to clone and pull updates from.
- Default: hardenedbsd/main.
+ The URL to a git repository
+ Default: https://git.HardenedBSD.org/HardenedBSD/ports.git
* __$PORTZAP\_INSTALLDIR__
- The directory where the ports collection will be installed.
- Default: /usr/ports/.
+ The directory where the ports collection will be installed
+ Default: /usr/ports/
## Install
@@ -82,5 +81,5 @@ via git:
## License
-[BSD Zero Clause](https://choosealicense.com/licenses/0bsd/).
-See [LICENSE](./LICENSE).
+[BSD Zero Clause](https://choosealicense.com/licenses/0bsd/)
+See [LICENSE](./LICENSE)
diff --git a/bin/portzap b/bin/portzap
index c0c51c2..232cfa1 100755
--- a/bin/portzap
+++ b/bin/portzap
@@ -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}"
@@ -57,9 +61,10 @@ case $1 in
printf "Usage: portzap COMMAND [OPTIONS]\n"
printf "\n"
printf "Commands:\n"
- printf " clone Clone the hardenedbsd ports tree\n"
- printf " pull Pull updates from the hardenedbsd ports tree\n"
- printf " rm Remove /usr/ports/ and /home/_portzap/ports/\n"
- printf " install Install the ports tree into /usr/ports/\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"
;;
esac
diff --git a/libexec/portzap/portzap-checkout b/libexec/portzap/portzap-checkout
new file mode 100644
index 0000000..b91ba85
--- /dev/null
+++ b/libexec/portzap/portzap-checkout
@@ -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}"
diff --git a/libexec/portzap/portzap-pull b/libexec/portzap/portzap-pull
index 10a70db..bb4946a 100755
--- a/libexec/portzap/portzap-pull
+++ b/libexec/portzap/portzap-pull
@@ -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}"
diff --git a/man/man8/portzap.8 b/man/man8/portzap.8
index 65400f0..a12c0fd 100644
--- a/man/man8/portzap.8
+++ b/man/man8/portzap.8
@@ -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