diff options
| author | mobinmob <mobinmob@disroot.org> | 2024-08-23 12:39:06 +0300 |
|---|---|---|
| committer | mobinmob <mobinmob@disroot.org> | 2024-08-23 12:39:06 +0300 |
| commit | 867485686365c745984778cc7404b8e955e2cea2 (patch) | |
| tree | d8ea6170955c8e558f1893790f949b781ee9988e | |
| parent | 51fd1a7e010a30c2b399681649a3e131ea2c24f0 (diff) | |
| download | autils-867485686365c745984778cc7404b8e955e2cea2.tar.gz autils-867485686365c745984778cc7404b8e955e2cea2.zip | |
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.
| -rwxr-xr-x | apkg-reposync | 42 |
1 files 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 <official> 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 <official> 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 |