bba0734f |
1 | # check if GITCLIENT has been set or set it to the output of hostname |
2 | if [ -z $GITCLIENT ]; then |
3 | GITCLIENT=$( hostname ) |
4 | fi |
4cbcb39e |
5 | # returns true if the argument provided directory is a git repository |
6 | is_git_repo() { |
7 | DIR=$1 |
8 | if [[ -d $DIR ]]; then |
9 | cd $DIR |
10 | if git rev-parse 2>/dev/null; then |
11 | true |
12 | else |
13 | false |
14 | fi |
15 | fi |
16 | } |
17 | |
18 | # sync local repository to remote |
19 | gitsync() { |
bba0734f |
20 | if [[ $USEGIT && -n $GITREMOTE ]]; then |
21 | NOWSYNC=$(date +%s) |
22 | # LASTSYNC is the last time we synced to the remote, or 0 if it's the first time. |
23 | LASTSYNC=$($JQ -r '.git["lastpull"] // 0' "$DB") |
24 | [ $PLAIN == false ] && echo "Syncing notes with git on remote \"$GITREMOTE\"" |
25 | SYNCDIFF=$(( ${NOWSYNC} - ${LASTSYNC} )) |
26 | if (( $SYNCDIFF > $GITSYNCDELAY )); then |
27 | #more than our delay time has passed. We can sync again. |
28 | $JQ --arg n "$NOWSYNC" '.git["lastpull"] = $n' "$DB" > $TMPDB |
29 | mv $TMPDB $DB |
30 | cd $BASEDIR |
31 | $GIT pull |
32 | else |
33 | # Last synced less than $GITSYNCDELAY seconds ago. We shall wait |
34 | [ $PLAIN == false ] && echo "Last synced less than $GITSYNCDELAY seconds ago. We shall wait" |
35 | fi |
36 | else |
37 | # no git, so we just keep going |
38 | true |
39 | fi |
40 | } |
41 | |
42 | # add note to git and push it to remote |
43 | gitadd() { |
44 | if [[ $USEGIT && -n $GITREMOTE ]]; then |
45 | [ $PLAIN == false ] && echo "Adding note to remote \"$GITREMOTE\"" |
cf6d89bc |
46 | cd $BASEDIR |
bba0734f |
47 | $GIT add . |
48 | $GIT commit -m "$(basename $0) - adding note from ${GITCLIENT}" |
49 | $GIT push origin master |
cf6d89bc |
50 | else |
bba0734f |
51 | # no git, so we just keep going |
52 | true |
cf6d89bc |
53 | fi |
4cbcb39e |
54 | } |
55 | |
56 | # check for USEGIT and subsequent variables |
57 | if [[ $USEGIT && -n $GITREMOTE ]]; then |
58 | # GIT is a go. |
59 | if ! is_git_repo $BASEDIR; then |
60 | # initializing git repository |
61 | cd $BASEDIR |
62 | $GIT init |
63 | echo "adding all files to git" |
64 | $GIT add . |
bba0734f |
65 | $GIT commit -m "$(basename $0) - initial commit from ${GITCLIENT}" |
4cbcb39e |
66 | $GIT remote add origin $GITREMOTE |
67 | $GIT push -u origin master |
68 | fi |
69 | elif [[ $USEGIT && -z $GITREMOTE ]]; then |
70 | echo "GITREMOTE variable not set. reverting USEGIT to false" |
71 | USEGIT=false |
72 | fi |
73 | |