git-sync: Add sanity check before remote pull

Added check to fetch remote updates before actually pulling in remote
changes.
This may help somewhat with the modification of files when they are
simultaneously open in vim, though I am not entirely sure.
Needs further investigation.
This commit is contained in:
Marty Oehme 2020-11-13 08:21:16 +01:00
parent 8b223673fd
commit 0b43d717d3
Signed by: Marty
GPG key ID: B7538B8F50A1C800

View file

@ -19,13 +19,16 @@ set_target() {
DIR="${GS_TARGETDIR:-$1}"
}
commit() {
pull() {
msg "Pulling upstream changes into $DIR"
run_git pull --ff-only --ff
}
commit() {
msg "Committing changes to $DIR"
run_git add .
# shellcheck disable=2039
run_git commit --no-gpg-sign -m "Git sync: $(date +%F_%R) from ${HOSTNAME:-"$HOST"}"
run_git commit --no-gpg-sign -m "Git sync: $(date +%F_%R) from ${HOSTNAME:-"${HOST:-undefined}"}"
}
push() {
@ -38,7 +41,7 @@ should_commit() {
msg "No changes to commit in $DIR" 2
false
else
msg "Found changes to commit in $DIR"
msg "Found changes to commit in $DIR" 2
true
fi
}
@ -51,12 +54,23 @@ should_push() {
fi
}
should_pull() {
run_git fetch
# shellcheck disable=1083
if [ "$(run_git rev-parse HEAD)" = "$(run_git rev-parse @{u})" ]; then
false
else
true
fi
}
run_git() {
git -C "$DIR" "$@"
}
# echos its first argument
# verbosity level optionally set through second argument
# default verbosity 1 (info), can be set to 2 (debug), or 0 (error)
msg() {
lvl=${2:-1}
[ "$lvl" -gt "$VERBOSITY" ] && return 0
@ -67,6 +81,10 @@ msg() {
watch_changes() {
no_change_cycle=0
while true; do
if should_pull; then
pull
fi
if should_commit; then
commit
no_change_cycle=0