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