2016-07-21 15:05:41 +00:00
|
|
|
#!/bin/bash
|
2018-02-11 10:29:07 +00:00
|
|
|
set -euo pipefail
|
2016-07-21 15:05:41 +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=(
|
|
|
|
[public]='git@github.com:Luzifer/cfg.git#master'
|
|
|
|
[secret]='git@github.com:Luzifer/cfg-secret.git#master'
|
|
|
|
[system]="git@github.com:Luzifer/cfg-system.git#${SYSTEM}"
|
|
|
|
)
|
2016-07-21 15:05:41 +00:00
|
|
|
|
2018-02-11 10:29:07 +00:00
|
|
|
# --- OPT parsing ---
|
|
|
|
|
|
|
|
while getopts "f" opt; do
|
|
|
|
case "$opt" in
|
|
|
|
f)
|
|
|
|
FORCE=1
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
|
|
|
shift $((OPTIND-1))
|
|
|
|
[ "${1:-}" = "--" ] && shift
|
|
|
|
|
|
|
|
# --- OPT parsing ---
|
|
|
|
|
2018-03-04 21:49:15 +00:00
|
|
|
if [ -e ${HOME}/bin/script_framework.sh ]; then
|
|
|
|
source ${HOME}/bin/script_framework.sh
|
|
|
|
else
|
|
|
|
function step { echo "$@"; }
|
|
|
|
function fatal { echo "$@"; exit 1; }
|
2016-07-21 15:05:41 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
function config {
|
2018-03-04 21:49:15 +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
|
|
|
|
clone_url=$(echo ${REPOS[$repo_name]} | cut -d '#' -f 1)
|
|
|
|
branch=$(echo ${REPOS[$repo_name]} | cut -d '#' -f 2)
|
|
|
|
|
|
|
|
step "Working on '${repo_name}' (remote: '${clone_url}', branch: '${branch}'..."
|
|
|
|
|
|
|
|
# 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
|
|
|
|
|
2018-02-11 10:29:07 +00:00
|
|
|
# Set basic git options for the repo
|
2016-07-21 15:05:41 +00:00
|
|
|
config config status.showUntrackedFiles no
|
2018-02-11 10:29:07 +00:00
|
|
|
|
|
|
|
# Do not overwrite local changes
|
|
|
|
if ( ! config diff --exit-code 2>&1 >/dev/null ) && [ ${FORCE} -eq 0 ]; then
|
2018-03-04 21:49:15 +00:00
|
|
|
fatal "Repo '${REPO}' has unsaved changes and force-flag is not set"
|
2018-02-11 10:29:07 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
# Refresh latest master
|
2018-03-04 21:49:15 +00:00
|
|
|
config fetch -q origin ${branch} || { fatal "Failed to fetch '${repo_name}'"; }
|
2018-02-11 10:29:07 +00:00
|
|
|
|
|
|
|
# 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
|
2016-08-11 17:12:25 +00:00
|
|
|
config submodule update --init --recursive
|
2016-07-21 15:05:41 +00:00
|
|
|
done
|