aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoremmett1 <emmett1.2miligrams@protonmail.com>2024-08-23 17:01:13 +0000
committeremmett1 <emmett1.2miligrams@protonmail.com>2024-08-23 17:01:13 +0000
commitb78ca5ec23fe5c4ac8ff4c0db82fa2ce40c73266 (patch)
tree10a0f62f2facac345dd258a32e7caaa1881cc2b4
parente350d82da6b4d74d03e8c2f06ba2486e38ce3d6f (diff)
parent35795794f28be83afa4faff82c4faacdded39de9 (diff)
downloadautils-b78ca5ec23fe5c4ac8ff4c0db82fa2ce40c73266.tar.gz
autils-b78ca5ec23fe5c4ac8ff4c0db82fa2ce40c73266.zip
Merge pull request 'apkg-reposync' (#4) from mobinmob/autils:apkg-reposync into main
Reviewed-on: https://codeberg.org/emmett1/autils/pulls/4
-rwxr-xr-xapkg-reposync73
1 files changed, 73 insertions, 0 deletions
diff --git a/apkg-reposync b/apkg-reposync
new file mode 100755
index 0000000..6b275c1
--- /dev/null
+++ b/apkg-reposync
@@ -0,0 +1,73 @@
+#!/bin/sh
+
+APKG_CONF="${APKG_CONF:-/etc/apkg.conf}"
+temp_repos_file=$(mktemp)
+write_permission=0
+
+die() {
+ [ "$1" ] && echo "error: $1"
+ rm -f "$temp_repos_file"
+ exit 1
+}
+
+find_default_remote() {
+ cd "${1}" || die "cannot cd to <${1}> the repo"
+ default_remote=$(git branch --list "$(git branch --show-current)" \
+ "--format=%(upstream:remotename)")
+}
+
+find_current_branch() {
+ cd "${1}" || die "cannot cd to the <${1}> repo"
+ current_branch=$(git remote show "${default_remote}" | sed -n '/HEAD branch/s/.*: //p')
+}
+
+check_write_permissions() {
+ random_file=$(find "${1}" -type f -print0 | shuf -z -n 1)
+ test -w "$random_file" || write_permission=$((write_permission+1))
+}
+
+source_and_sanity_checks() {
+[ ! -e "$(realpath "${APKG_CONF}")" ] && die "There is no configuration file in ${APKG_CONF}! Exiting..."
+
+. "${APKG_CONF}"
+
+[ -z "${APKG_REPO}" ] && die "There are no repos configured in $APKG_CONF, exiting..."
+
+for r in ${APKG_REPO}; do
+printf "%s\n" "$r" >> "$temp_repos_file"
+check_write_permissions "$r"; done
+[ "$write_permission" = 0 ] || die "You need to execute the script as root!"
+printf "%s\n" "Configured repos:" && awk ' {print $0} ' "$temp_repos_file"
+grep -q "alicelinux/repos/core" "$temp_repos_file" || die "No official repos configured in $APKG_CONF"
+}
+
+sync_official() {
+ # This assumes the current repo structure.
+ dir_for_official_repo=$(grep alicelinux/repos/core "$temp_repos_file")
+ [ -d "$dir_for_official_repo" ] || die "No $dir_for_official_repo exists, exiting..."
+ dir_for_official_repo=$(echo "$dir_for_official_repo" | sed "s/repos\/core//")
+ printf "%s\n" "[~> Syncing official repos in <$dir_for_official_repo> ...]"
+ find_default_remote "$dir_for_official_repo"
+ find_current_branch "$dir_for_official_repo"
+ git pull "$default_remote" "$current_branch" || die "Cannot pull from $default_remote/$current_branch for the ofixial repos"
+ [ "$?" = 0 ] && printf "%s\n" "[~> Successfully synced the <official> repo!]"
+}
+
+sync_or_skip() {
+ # Remove all official repo lines from the temp_repos_file
+ unofficial_repos=$(grep -v alicelinux/repos "$temp_repos_file" | tr "\n" " ")
+ for r in $unofficial_repos; do
+ if [ -d "$r"/.git ]; then
+ find_default_remote "$r"
+ find_current_branch "$r"
+ git pull "$default_remote" "$current_branch" || die "Cannot pull from $default_remote/$current_branch"
+ else
+ printf "%s\n" "[~>Repo in <$r> is not a git repo, skipping...]"
+ fi
+ done
+}
+
+source_and_sanity_checks
+sync_official
+sync_or_skip
+[ "$?" = 0 ] && echo "[~> Repo(s) succesfully synced!]" && rm -f "temp_repos_file"