From bba0734fdb0ddeea845a8534b24daf13cbda9d1d Mon Sep 17 00:00:00 2001 From: danix Date: Fri, 14 Apr 2023 11:57:37 +0200 Subject: [PATCH] adding notes now pushes changes to remote --- SOURCE/CORE/core-add.sh | 4 +++ SOURCE/CORE/git.sh | 49 ++++++++++++++++++++++++++---------- SOURCE/head.sh | 2 ++ notes.sh | 55 +++++++++++++++++++++++++++++++---------- 4 files changed, 84 insertions(+), 26 deletions(-) diff --git a/SOURCE/CORE/core-add.sh b/SOURCE/CORE/core-add.sh index 1c71bbb..c6dee8a 100644 --- a/SOURCE/CORE/core-add.sh +++ b/SOURCE/CORE/core-add.sh @@ -1,4 +1,6 @@ addnote() { + # attempt syncing before adding a note + gitsync # remove eventually existing temp DB file if [[ -f $TMPDB ]]; then rm $TMPDB @@ -21,4 +23,6 @@ addnote() { # alacritty --class notes --title notes -e /usr/bin/vim ... # shellcheck disable=SC2086,SC2091 $(${TERMINAL} ${TERM_OPTS} ${EDITOR} ${NOTESDIR}/${NOW}) + # add note to git and push to remote + gitadd } diff --git a/SOURCE/CORE/git.sh b/SOURCE/CORE/git.sh index 8edfc1f..8841521 100644 --- a/SOURCE/CORE/git.sh +++ b/SOURCE/CORE/git.sh @@ -1,3 +1,7 @@ +# check if GITCLIENT has been set or set it to the output of hostname +if [ -z $GITCLIENT ]; then + GITCLIENT=$( hostname ) +fi # returns true if the argument provided directory is a git repository is_git_repo() { DIR=$1 @@ -13,20 +17,39 @@ is_git_repo() { # sync local repository to remote gitsync() { - 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 + if [[ $USEGIT && -n $GITREMOTE ]]; then + 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 + else + # no git, so we just keep going + true + fi +} + +# add note to git and push it to remote +gitadd() { + if [[ $USEGIT && -n $GITREMOTE ]]; then + [ $PLAIN == false ] && echo "Adding note to remote \"$GITREMOTE\"" cd $BASEDIR - $GIT pull + $GIT add . + $GIT commit -m "$(basename $0) - adding note from ${GITCLIENT}" + $GIT push origin master else - # Last synced less than $GITSYNCDELAY seconds ago. We shall wait - [ $PLAIN == false ] && echo "Last synced less than $GITSYNCDELAY seconds ago. We shall wait" + # no git, so we just keep going + true fi } @@ -39,7 +62,7 @@ if [[ $USEGIT && -n $GITREMOTE ]]; then $GIT init echo "adding all files to git" $GIT add . - $GIT commit -m "$(basename $0) - initial commit" + $GIT commit -m "$(basename $0) - initial commit from ${GITCLIENT}" $GIT remote add origin $GITREMOTE $GIT push -u origin master fi diff --git a/SOURCE/head.sh b/SOURCE/head.sh index 890d711..271367f 100644 --- a/SOURCE/head.sh +++ b/SOURCE/head.sh @@ -53,6 +53,8 @@ USEGIT=true GITREMOTE=${GITREMOTE:-""} # How long should we wait (in seconds) between sync on the git remote. Default 3600 (1 hour) GITSYNCDELAY=${GITSYNCDELAY:-3600} +# The name of this client. Defaults to the output of hostname +GITCLIENT=${GITCLIENT:-""} } # end set_defaults, do not change this line. diff --git a/notes.sh b/notes.sh index 62fbd30..8af4d97 100755 --- a/notes.sh +++ b/notes.sh @@ -53,6 +53,8 @@ USEGIT=true GITREMOTE=${GITREMOTE:-""} # How long should we wait (in seconds) between sync on the git remote. Default 3600 (1 hour) GITSYNCDELAY=${GITSYNCDELAY:-3600} +# The name of this client. Defaults to the output of hostname +GITCLIENT=${GITCLIENT:-""} } # end set_defaults, do not change this line. @@ -235,6 +237,10 @@ random_title() { echo $OUTPUT } +# check if GITCLIENT has been set or set it to the output of hostname +if [ -z $GITCLIENT ]; then + GITCLIENT=$( hostname ) +fi # returns true if the argument provided directory is a git repository is_git_repo() { DIR=$1 @@ -250,20 +256,39 @@ is_git_repo() { # sync local repository to remote gitsync() { - 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 + if [[ $USEGIT && -n $GITREMOTE ]]; then + 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 + else + # no git, so we just keep going + true + fi +} + +# add note to git and push it to remote +gitadd() { + if [[ $USEGIT && -n $GITREMOTE ]]; then + [ $PLAIN == false ] && echo "Adding note to remote \"$GITREMOTE\"" cd $BASEDIR - $GIT pull + $GIT add . + $GIT commit -m "$(basename $0) - adding note from ${GITCLIENT}" + $GIT push origin master else - # Last synced less than $GITSYNCDELAY seconds ago. We shall wait - [ $PLAIN == false ] && echo "Last synced less than $GITSYNCDELAY seconds ago. We shall wait" + # no git, so we just keep going + true fi } @@ -276,7 +301,7 @@ if [[ $USEGIT && -n $GITREMOTE ]]; then $GIT init echo "adding all files to git" $GIT add . - $GIT commit -m "$(basename $0) - initial commit" + $GIT commit -m "$(basename $0) - initial commit from ${GITCLIENT}" $GIT remote add origin $GITREMOTE $GIT push -u origin master fi @@ -286,6 +311,8 @@ elif [[ $USEGIT && -z $GITREMOTE ]]; then fi addnote() { + # attempt syncing before adding a note + gitsync # remove eventually existing temp DB file if [[ -f $TMPDB ]]; then rm $TMPDB @@ -308,6 +335,8 @@ addnote() { # alacritty --class notes --title notes -e /usr/bin/vim ... # shellcheck disable=SC2086,SC2091 $(${TERMINAL} ${TERM_OPTS} ${EDITOR} ${NOTESDIR}/${NOW}) + # add note to git and push to remote + gitadd } backup_data() { BACKUPDIR="$1" -- 2.20.1