2018-02-10 23:54:09 +00:00
|
|
|
#!/bin/bash
|
|
|
|
set -euo pipefail
|
|
|
|
|
|
|
|
DEBUG=${DEBUG:-false}
|
|
|
|
FETCH_INTERVAL=${FETCH_INTERVAL:-3600}
|
|
|
|
REPOS=( public secret )
|
|
|
|
|
|
|
|
# Print debug messages if enabled by ${DEBUG}
|
|
|
|
function debug {
|
|
|
|
[[ "${DEBUG}" = "false" ]] && return
|
|
|
|
echo "$@" >&2
|
|
|
|
}
|
|
|
|
|
2018-02-11 00:28:39 +00:00
|
|
|
function join_by { local d=$1; shift; echo -n "$1"; shift; printf "%s" "${@/#/$d}"; }
|
|
|
|
|
2018-02-10 23:54:09 +00:00
|
|
|
# Wrap git to work with git-dir and work-tree being in other locations
|
|
|
|
function gwrap {
|
|
|
|
hub --git-dir=${HOME}/.cfg/${REPO} --work-tree=${HOME} $@
|
|
|
|
}
|
|
|
|
|
|
|
|
# Ensure we're connected to network before acting
|
|
|
|
if ! ( ping -q -c 1 8.8.8.8 >/dev/null ); then
|
|
|
|
debug "No network connection, not checking"
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
2018-02-11 00:13:23 +00:00
|
|
|
STAT_PARM="-c %Y"
|
|
|
|
[[ "$(uname -s)" = "Darwin" ]] && STAT_PARM="-f %m"
|
|
|
|
|
2018-02-11 00:28:39 +00:00
|
|
|
NEED_UPDATE=()
|
2018-02-10 23:54:09 +00:00
|
|
|
# Check repos for updates
|
|
|
|
for REPO in ${REPOS[@]}; do
|
2018-02-11 00:13:23 +00:00
|
|
|
if [ $(( $(date +%s) - $(stat ${STAT_PARM} .cfg/${REPO}/FETCH_HEAD) )) -lt ${FETCH_INTERVAL} ]; then
|
2018-02-10 23:54:09 +00:00
|
|
|
debug "Last repo fetch for '${REPO}' too new, not checking"
|
|
|
|
continue
|
|
|
|
fi
|
|
|
|
|
|
|
|
gwrap fetch -q
|
|
|
|
|
|
|
|
LOCAL=$(gwrap rev-parse HEAD)
|
|
|
|
REMOTE=$(gwrap rev-parse FETCH_HEAD)
|
|
|
|
|
|
|
|
if ! [[ "${LOCAL}" = "${REMOTE}" ]]; then
|
2018-02-11 00:28:39 +00:00
|
|
|
NEED_UPDATE+=("${REPO}")
|
2018-02-10 23:54:09 +00:00
|
|
|
fi
|
|
|
|
done
|
2018-02-11 00:28:39 +00:00
|
|
|
|
|
|
|
if [ ${#NEED_UPDATE[@]} -gt 0 ]; then
|
|
|
|
echo "Config repo(s) $(join_by ", " "${NEED_UPDATE[@]}") differs from upstream"
|
|
|
|
fi
|