From 0b43d717d3ca387c5b01f3c176cae01fecddb6d7 Mon Sep 17 00:00:00 2001 From: Marty Oehme Date: Fri, 13 Nov 2020 08:21:16 +0100 Subject: [PATCH] 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. --- services/.local/share/services/git-sync | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/services/.local/share/services/git-sync b/services/.local/share/services/git-sync index 66a6745..27f9bf4 100755 --- a/services/.local/share/services/git-sync +++ b/services/.local/share/services/git-sync @@ -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