2018-03-04 22:04:08 +00:00
|
|
|
#!/usr/bin/env bash
|
2018-02-11 10:29:07 +00:00
|
|
|
set -euo pipefail
|
2016-07-21 15:05:41 +00:00
|
|
|
|
2018-03-04 22:31:30 +00:00
|
|
|
[ ${BASH_VERSINFO[0]:-0} -lt 4 ] && {
|
2023-07-24 18:54:38 +00:00
|
|
|
echo "Bash too old, update to >=4.0"
|
|
|
|
exit 1
|
2018-03-04 22:28:50 +00:00
|
|
|
}
|
|
|
|
|
2018-03-04 21:49:15 +00:00
|
|
|
SYSTEM=$(uname -s | tr 'A-Z' 'a-z')
|
|
|
|
|
2018-02-11 10:29:07 +00:00
|
|
|
FORCE=0
|
2018-03-04 21:49:15 +00:00
|
|
|
typeset -A REPOS
|
|
|
|
REPOS=(
|
2023-07-24 18:54:38 +00:00
|
|
|
[public]='https://git.luzifer.io/luzifer/cfg.git#master'
|
|
|
|
[secret]='https://git.luzifer.io/luzifer/cfg-secret.git#master'
|
2018-03-04 21:49:15 +00:00
|
|
|
)
|
2016-07-21 15:05:41 +00:00
|
|
|
|
2018-02-11 10:29:07 +00:00
|
|
|
# --- OPT parsing ---
|
|
|
|
|
|
|
|
while getopts "f" opt; do
|
2023-07-24 18:54:38 +00:00
|
|
|
case "$opt" in
|
|
|
|
f)
|
|
|
|
FORCE=1
|
|
|
|
;;
|
|
|
|
esac
|
2018-02-11 10:29:07 +00:00
|
|
|
done
|
|
|
|
|
2018-03-10 11:47:55 +00:00
|
|
|
shift $((OPTIND - 1))
|
2018-02-11 10:29:07 +00:00
|
|
|
[ "${1:-}" = "--" ] && shift
|
|
|
|
|
|
|
|
# --- OPT parsing ---
|
|
|
|
|
2018-03-04 21:49:15 +00:00
|
|
|
if [ -e ${HOME}/bin/script_framework.sh ]; then
|
2023-07-24 18:54:38 +00:00
|
|
|
source ${HOME}/bin/script_framework.sh
|
2018-03-04 21:49:15 +00:00
|
|
|
else
|
2023-07-24 18:54:38 +00:00
|
|
|
function step() { echo "$@"; }
|
|
|
|
function fatal() {
|
|
|
|
echo "$@"
|
|
|
|
exit 1
|
|
|
|
}
|
2016-07-21 15:05:41 +00:00
|
|
|
fi
|
|
|
|
|
2018-03-10 11:47:55 +00:00
|
|
|
function config() {
|
2023-07-24 18:54:38 +00:00
|
|
|
git --git-dir="${HOME}/.cfg/${repo_name}" --work-tree="${HOME}" $@
|
2016-07-21 15:05:41 +00:00
|
|
|
}
|
|
|
|
|
2018-03-04 21:49:15 +00:00
|
|
|
for repo_name in "${!REPOS[@]}"; do
|
2023-07-24 18:54:38 +00:00
|
|
|
clone_url=$(echo ${REPOS[$repo_name]} | cut -d '#' -f 1)
|
|
|
|
branch=$(echo ${REPOS[$repo_name]} | cut -d '#' -f 2)
|
|
|
|
|
2023-07-24 18:58:04 +00:00
|
|
|
step "Working on '${repo_name}' (remote: '${clone_url}', branch: '${branch}')..."
|
2023-07-24 18:54:38 +00:00
|
|
|
|
|
|
|
# Clone repo if it's not already available
|
|
|
|
if ! [ -d "${HOME}/.cfg/${repo_name}" ]; then
|
|
|
|
git clone --bare "${clone_url}" --branch "${branch}" "${HOME}/.cfg/${repo_name}"
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Set basic git options for the repo
|
|
|
|
config config status.showUntrackedFiles no
|
|
|
|
|
|
|
|
# Do not overwrite local changes
|
|
|
|
if (! config diff --exit-code 2>&1 >/dev/null) && [ ${FORCE} -eq 0 ]; then
|
|
|
|
error "Repo '${repo_name}' has unsaved changes and force-flag is not set"
|
|
|
|
continue
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Refresh latest master
|
|
|
|
config fetch -q origin ${branch} || { fatal "Failed to fetch '${repo_name}'"; }
|
|
|
|
|
|
|
|
# Apply latest master
|
|
|
|
COMMITS_AHEAD=$(config rev-list --left-right --count FETCH_HEAD...HEAD | awk '{ print $2 }')
|
|
|
|
if [ ${COMMITS_AHEAD} -gt 0 ]; then
|
|
|
|
echo "Local commits found, trying to rebase..."
|
|
|
|
config rebase FETCH_HEAD
|
|
|
|
else
|
|
|
|
echo "No local commits, resetting to remote master..."
|
|
|
|
config reset --hard FETCH_HEAD
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Update submodules
|
|
|
|
config submodule update --init --recursive
|
2016-07-21 15:05:41 +00:00
|
|
|
done
|