1 # check if GITCLIENT has been set or set it to the output of hostname
2 if [ -z "$GITCLIENT" ]; then
3 GITCLIENT
=$
( hostname
)
5 # returns true if the argument provided directory is a git repository
10 if git rev-parse
2>/dev
/null
; then
18 # sync local repository to remote
19 # accepts -f parameter to skip last sync check
22 if [[ $USEGIT && -n $GITREMOTE ]]; then
23 [ $PLAIN == false
] && echo "Syncing notes with git on remote \"$GITREMOTE\""
25 if [[ $FORCE == "-f" ]]; then
26 $JQ --arg n
"$NOWSYNC" '.git["lastpull"] = $n' "$DB" > $TMPDB
29 [ $PLAIN == false
] && $GIT pull ||
$GIT pull
-q
31 # LASTSYNC is the last time we synced to the remote, or 0 if it's the first time.
32 LASTSYNC
=$
($JQ -r '.git["lastpull"] // 0' "$DB")
33 SYNCDIFF
=$
(( ${NOWSYNC} - ${LASTSYNC} ))
34 if (( $SYNCDIFF > $GITSYNCDELAY )); then
35 #more than our delay time has passed. We can sync again.
36 $JQ --arg n
"$NOWSYNC" '.git["lastpull"] = $n' "$DB" > $TMPDB
39 [ $PLAIN == false
] && $GIT pull ||
$GIT pull
-q
41 # Last synced less than $GITSYNCDELAY seconds ago. We shall wait
42 [ $PLAIN == false
] && echo "Last synced less than $GITSYNCDELAY seconds ago. We shall wait"
46 # no git, so we just keep going
51 # add note to git and push it to remote
53 if [[ $USEGIT && -n $GITREMOTE ]]; then
54 [ $PLAIN == false
] && echo "Adding note to remote \"$GITREMOTE\""
57 $GIT commit
-m "$(basename $0) - adding note from ${GITCLIENT}"
58 $GIT push origin master
60 # no git, so we just keep going
65 # edited note added to git and pushed it to remote
67 if [[ $USEGIT && -n $GITREMOTE ]]; then
68 [ $PLAIN == false
] && echo "Editing note on remote \"$GITREMOTE\""
71 $GIT commit
-m "$(basename $0) - ${GITCLIENT} note edited."
72 $GIT push origin master
74 # no git, so we just keep going
79 # add note to git and push it to remote
83 if [[ $USEGIT && -n $GITREMOTE ]]; then
84 [ $PLAIN == false
] && echo "Deleting notes from remote \"$GITREMOTE\""
85 if [ "all" == $NOTE ];then
86 echo "Deleting all notes"
89 $GIT commit
-m "$(basename $0) - ${GITCLIENT} removing all notes."
90 $GIT push origin master
92 local OK
=$
(check_noteID
"$NOTE")
94 echo "Deleting note ID ${NOTE}"
98 $GIT commit
-m "$(basename $0) - ${GITCLIENT} removing note ID ${NOTE}."
99 $GIT push origin master
103 # no git, so we just keep going
108 # check for USEGIT and subsequent variables
109 if [[ $USEGIT && -n $GITREMOTE ]]; then
111 if ! is_git_repo
$BASEDIR; then
112 # initializing git repository
115 echo "adding all files to git"
117 $GIT commit
-m "$(basename $0) - initial commit from ${GITCLIENT}"
118 $GIT remote add origin
$GITREMOTE
119 $GIT push
-u origin master
121 elif [[ $USEGIT && -z $GITREMOTE ]]; then
122 echo "GITREMOTE variable not set. reverting USEGIT to false"