sync operations now check for last sync before running again. It's possible to set...
[bash-notes.git] / SOURCE / CORE / git.sh
CommitLineData
4cbcb39e 1# returns true if the argument provided directory is a git repository
2is_git_repo() {
3 DIR=$1
4 if [[ -d $DIR ]]; then
5 cd $DIR
6 if git rev-parse 2>/dev/null; then
7 true
8 else
9 false
10 fi
11 fi
12}
13
14# sync local repository to remote
15gitsync() {
cf6d89bc 16 NOWSYNC=$(date +%s)
17 # LASTSYNC is the last time we synced to the remote, or 0 if it's the first time.
18 LASTSYNC=$($JQ -r '.git["lastpull"] // 0' "$DB")
19 [ $PLAIN == false ] && echo "Syncing notes with git on remote \"$GITREMOTE\""
20 SYNCDIFF=$(( ${NOWSYNC} - ${LASTSYNC} ))
21 if (( $SYNCDIFF > $GITSYNCDELAY )); then
22 #more than our delay time has passed. We can sync again.
23 $JQ --arg n "$NOWSYNC" '.git["lastpull"] = $n' "$DB" > $TMPDB
24 mv $TMPDB $DB
25 cd $BASEDIR
26 $GIT pull
27 else
28 # Last synced less than $GITSYNCDELAY seconds ago. We shall wait
29 [ $PLAIN == false ] && echo "Last synced less than $GITSYNCDELAY seconds ago. We shall wait"
30 fi
4cbcb39e 31}
32
33# check for USEGIT and subsequent variables
34if [[ $USEGIT && -n $GITREMOTE ]]; then
35 # GIT is a go.
36 if ! is_git_repo $BASEDIR; then
37 # initializing git repository
38 cd $BASEDIR
39 $GIT init
40 echo "adding all files to git"
41 $GIT add .
42 $GIT commit -m "$(basename $0) - initial commit"
43 $GIT remote add origin $GITREMOTE
44 $GIT push -u origin master
45 fi
46elif [[ $USEGIT && -z $GITREMOTE ]]; then
47 echo "GITREMOTE variable not set. reverting USEGIT to false"
48 USEGIT=false
49fi
50