From cf6d89bcf906ba72560847cb804646b9cd8d99b4 Mon Sep 17 00:00:00 2001 From: danix Date: Fri, 14 Apr 2023 10:45:52 +0200 Subject: sync operations now check for last sync before running again. It's possible to set a delay between sync operations. --- SOURCE/CORE/core-list.sh | 2 ++ SOURCE/CORE/git.sh | 18 +++++++++++++++--- SOURCE/head.sh | 2 ++ 3 files changed, 19 insertions(+), 3 deletions(-) (limited to 'SOURCE') diff --git a/SOURCE/CORE/core-list.sh b/SOURCE/CORE/core-list.sh index 17349cd..6ab2896 100644 --- a/SOURCE/CORE/core-list.sh +++ b/SOURCE/CORE/core-list.sh @@ -1,4 +1,6 @@ listnotes() { + # attempt syncing before listing all notes + gitsync # [ $PLAIN == true ] && echo "output is plain text" || echo "output is colored" if [[ $(ls -A "$NOTESDIR") ]]; then if [ $PLAIN == false ]; then diff --git a/SOURCE/CORE/git.sh b/SOURCE/CORE/git.sh index d37fe72..8edfc1f 100644 --- a/SOURCE/CORE/git.sh +++ b/SOURCE/CORE/git.sh @@ -13,9 +13,21 @@ is_git_repo() { # sync local repository to remote gitsync() { - echo "Syncing notes with git on remote \"$GITREMOTE\"" - cd $BASEDIR - $GIT pull + NOWSYNC=$(date +%s) + # LASTSYNC is the last time we synced to the remote, or 0 if it's the first time. + LASTSYNC=$($JQ -r '.git["lastpull"] // 0' "$DB") + [ $PLAIN == false ] && echo "Syncing notes with git on remote \"$GITREMOTE\"" + SYNCDIFF=$(( ${NOWSYNC} - ${LASTSYNC} )) + if (( $SYNCDIFF > $GITSYNCDELAY )); then + #more than our delay time has passed. We can sync again. + $JQ --arg n "$NOWSYNC" '.git["lastpull"] = $n' "$DB" > $TMPDB + mv $TMPDB $DB + cd $BASEDIR + $GIT pull + else + # Last synced less than $GITSYNCDELAY seconds ago. We shall wait + [ $PLAIN == false ] && echo "Last synced less than $GITSYNCDELAY seconds ago. We shall wait" + fi } # check for USEGIT and subsequent variables diff --git a/SOURCE/head.sh b/SOURCE/head.sh index 7b684f1..890d711 100644 --- a/SOURCE/head.sh +++ b/SOURCE/head.sh @@ -51,6 +51,8 @@ NOTESDIR=${BASEDIR}/notes USEGIT=true # Address of your remote repository GITREMOTE=${GITREMOTE:-""} +# How long should we wait (in seconds) between sync on the git remote. Default 3600 (1 hour) +GITSYNCDELAY=${GITSYNCDELAY:-3600} } # end set_defaults, do not change this line. -- cgit v1.2.3