From 51fd1a7e010a30c2b399681649a3e131ea2c24f0 Mon Sep 17 00:00:00 2001 From: mobinmob Date: Thu, 22 Aug 2024 17:14:51 +0300 Subject: apkg-reposync: new util --- apkg-reposync | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100755 apkg-reposync diff --git a/apkg-reposync b/apkg-reposync new file mode 100755 index 0000000..b3cec1c --- /dev/null +++ b/apkg-reposync @@ -0,0 +1,53 @@ +#!/bin/sh + +APKG_CONF="${APKG_CONF:-/etc/apkg.conf}" +temp_repos_file=$(mktemp) + +die() { + [ "$1" ] && echo "error: $1" + exit 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" >> $(realpath $temp_repos_file); done +printf "%s\n" "Configured repos:" && awk ' {print $0} ' "$temp_repos_file" + +cat $temp_repos_file | grep -q "alicelinux/repos/core" > /dev/null || die "No official repos configured,exiting" +} + +sync_official() { + # This assumes the current repo structure. + dir_for_official_repo=$(cat $temp_repos_file | grep alicelinux/repos/core) + [ -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..." + cd "$dir_for_official_repo" || die "Cannot change to the repo directory, exiting..." + git pull origin main && printf "%s\n" "[~> Successfully synced the 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 + cd "$r" || die "cannot cd to the repo" + default_remote=$(git branch --list "$(git branch --show-current)" \ + "--format=%(upstream:remotename)") + default_branch=$(git remote show "${default_remote}" | sed -n '/HEAD branch/s/.*: //p') + git pull "$default_remote" "$default_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 +[ "$?" -eq 0 ] && echo "~> Repo(s) succesfully synced!" -- cgit v1.2.3 From 867485686365c745984778cc7404b8e955e2cea2 Mon Sep 17 00:00:00 2001 From: mobinmob Date: Fri, 23 Aug 2024 12:39:06 +0300 Subject: apkg-reposync: various fixes, see bellow... - add checks to commands that can fail - remove useless `cat` - split code to find remote and branch to functions - various shellcheck fixes. --- apkg-reposync | 42 ++++++++++++++++++++++++++---------------- 1 file changed, 26 insertions(+), 16 deletions(-) diff --git a/apkg-reposync b/apkg-reposync index b3cec1c..11b542d 100755 --- a/apkg-reposync +++ b/apkg-reposync @@ -8,28 +8,40 @@ die() { 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') +} + source_and_sanity_checks() { -[ ! -e $(realpath ${APKG_CONF}) ] && die "There is no configuration file in ${APKG_CONF}! Exiting..." +[ ! -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..." +[ -z "${APKG_REPO}" ] && die "There are no repos configured in $APKG_CONF, exiting..." -for r in ${APKG_REPO}; do printf "%s\n" "$r" >> $(realpath $temp_repos_file); done +for r in ${APKG_REPO}; do printf "%s\n" "$r" >> "$temp_repos_file"; done printf "%s\n" "Configured repos:" && awk ' {print $0} ' "$temp_repos_file" -cat $temp_repos_file | grep -q "alicelinux/repos/core" > /dev/null || die "No official repos configured,exiting" +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=$(cat $temp_repos_file | grep alicelinux/repos/core) + 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..." - cd "$dir_for_official_repo" || die "Cannot change to the repo directory, exiting..." - git pull origin main && printf "%s\n" "[~> Successfully synced the repo!]" - + 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" + if git pull "$default_remote" "$current_branch"; then printf "%s\n" "[~> Successfully synced the repo!]"; fi } sync_or_skip() { @@ -37,17 +49,15 @@ sync_or_skip() { unofficial_repos=$(grep -v alicelinux/repos "$temp_repos_file" | tr "\n" " ") for r in $unofficial_repos; do if [ -d "$r"/.git ]; then - cd "$r" || die "cannot cd to the repo" - default_remote=$(git branch --list "$(git branch --show-current)" \ - "--format=%(upstream:remotename)") - default_branch=$(git remote show "${default_remote}" | sed -n '/HEAD branch/s/.*: //p') - git pull "$default_remote" "$default_branch" + 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..." + printf "%s\n" "Repo in <$r> is not a git repo, skipping..." fi done } source_and_sanity_checks sync_official sync_or_skip -[ "$?" -eq 0 ] && echo "~> Repo(s) succesfully synced!" +if sync_or_skip; then echo "[~> Repo(s) succesfully synced!]"; fi -- cgit v1.2.3 From f0443679ae1a19659cea3e6765395328cb08da0a Mon Sep 17 00:00:00 2001 From: mobinmob Date: Fri, 23 Aug 2024 13:28:27 +0300 Subject: apkg-reposync: clean the temporary file. --- apkg-reposync | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/apkg-reposync b/apkg-reposync index 11b542d..3507486 100755 --- a/apkg-reposync +++ b/apkg-reposync @@ -5,6 +5,7 @@ temp_repos_file=$(mktemp) die() { [ "$1" ] && echo "error: $1" + rm -f "$temp_repos_file" exit 1 } @@ -60,4 +61,4 @@ sync_or_skip() { source_and_sanity_checks sync_official sync_or_skip -if sync_or_skip; then echo "[~> Repo(s) succesfully synced!]"; fi +if sync_or_skip; then echo "[~> Repo(s) succesfully synced!]" && rm -f "temp_repos_file"; fi -- cgit v1.2.3 From a9107929029a5dac1a40b18cae397720fa7f3f4a Mon Sep 17 00:00:00 2001 From: mobinmob Date: Fri, 23 Aug 2024 18:12:48 +0300 Subject: apkg-reposync: fix syncing twice, also minor presentation fix. --- apkg-reposync | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/apkg-reposync b/apkg-reposync index 3507486..e1e14e3 100755 --- a/apkg-reposync +++ b/apkg-reposync @@ -42,7 +42,7 @@ sync_official() { 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" - if git pull "$default_remote" "$current_branch"; then printf "%s\n" "[~> Successfully synced the repo!]"; fi + [ "$?" = 0 ] && printf "%s\n" "[~> Successfully synced the repo!]" } sync_or_skip() { @@ -53,12 +53,12 @@ sync_or_skip() { 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..." + else + printf "%s\n" "[~>Repo in <$r> is not a git repo, skipping...]" fi done } source_and_sanity_checks sync_official sync_or_skip -if sync_or_skip; then echo "[~> Repo(s) succesfully synced!]" && rm -f "temp_repos_file"; fi +[ "$?" = 0 ] && echo "[~> Repo(s) succesfully synced!]" && rm -f "temp_repos_file" -- cgit v1.2.3 From 35795794f28be83afa4faff82c4faacdded39de9 Mon Sep 17 00:00:00 2001 From: mobinmob Date: Fri, 23 Aug 2024 19:30:24 +0300 Subject: apkg-reposync: check if the user cannot write to the repos. --- apkg-reposync | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/apkg-reposync b/apkg-reposync index e1e14e3..6b275c1 100755 --- a/apkg-reposync +++ b/apkg-reposync @@ -2,6 +2,7 @@ APKG_CONF="${APKG_CONF:-/etc/apkg.conf}" temp_repos_file=$(mktemp) +write_permission=0 die() { [ "$1" ] && echo "error: $1" @@ -20,6 +21,11 @@ find_current_branch() { 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..." @@ -27,9 +33,11 @@ source_and_sanity_checks() { [ -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"; done +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" } @@ -58,6 +66,7 @@ sync_or_skip() { fi done } + source_and_sanity_checks sync_official sync_or_skip -- cgit v1.2.3