apkg-reposync (2561B)
1 #!/bin/sh 2 3 APKG_CONF="${APKG_CONF:-/etc/apkg.conf}" 4 temp_repos_file=$(mktemp) 5 write_permission=0 6 7 die() { 8 [ "$1" ] && echo "error: $1" 9 rm -f "$temp_repos_file" 10 exit 1 11 } 12 13 find_default_remote() { 14 cd "${1}" || die "cannot cd to <${1}> the repo" 15 default_remote=$(git branch --list "$(git branch --show-current)" \ 16 "--format=%(upstream:remotename)") 17 } 18 19 find_current_branch() { 20 cd "${1}" || die "cannot cd to the <${1}> repo" 21 current_branch=$(git remote show "${default_remote}" | sed -n '/HEAD branch/s/.*: //p') 22 } 23 24 check_write_permissions() { 25 random_file=$(find "${1}" -type f -print0 | shuf -z -n 1) 26 test -w "$random_file" || write_permission=$((write_permission+1)) 27 } 28 29 source_and_sanity_checks() { 30 [ ! -e "$(realpath "${APKG_CONF}")" ] && die "There is no configuration file in ${APKG_CONF}! Exiting..." 31 32 . "${APKG_CONF}" 33 34 [ -z "${APKG_REPO}" ] && die "There are no repos configured in $APKG_CONF, exiting..." 35 36 for r in ${APKG_REPO}; do 37 printf "%s\n" "$r" >> "$temp_repos_file" 38 check_write_permissions "$r"; done 39 [ "$write_permission" = 0 ] || die "You need to execute the script as root!" 40 printf "%s\n" "Configured repos:" && awk ' {print $0} ' "$temp_repos_file" 41 grep -q "alicelinux/repos/core" "$temp_repos_file" || die "No official repos configured in $APKG_CONF" 42 } 43 44 sync_official() { 45 # This assumes the current repo structure. 46 dir_for_official_repo=$(grep alicelinux/repos/core "$temp_repos_file") 47 [ -d "$dir_for_official_repo" ] || die "No $dir_for_official_repo exists, exiting..." 48 dir_for_official_repo=$(echo "$dir_for_official_repo" | sed "s/repos\/core//") 49 printf "%s\n" "[~> Syncing official repos in <$dir_for_official_repo> ...]" 50 find_default_remote "$dir_for_official_repo" 51 find_current_branch "$dir_for_official_repo" 52 git pull "$default_remote" "$current_branch" || die "Cannot pull from $default_remote/$current_branch for the ofixial repos" 53 [ "$?" = 0 ] && printf "%s\n" "[~> Successfully synced the <official> repo!]" 54 } 55 56 sync_or_skip() { 57 # Remove all official repo lines from the temp_repos_file 58 unofficial_repos=$(grep -v alicelinux/repos "$temp_repos_file" | tr "\n" " ") 59 for r in $unofficial_repos; do 60 if [ -d "$r"/.git ]; then 61 find_default_remote "$r" 62 find_current_branch "$r" 63 git pull "$default_remote" "$current_branch" || die "Cannot pull from $default_remote/$current_branch" 64 else 65 printf "%s\n" "[~>Repo in <$r> is not a git repo, skipping...]" 66 fi 67 done 68 } 69 70 source_and_sanity_checks 71 sync_official 72 sync_or_skip 73 [ "$?" = 0 ] && echo "[~> Repo(s) succesfully synced!]" && rm -f "temp_repos_file"