bba0734f |
1 | # check if GITCLIENT has been set or set it to the output of hostname |
1f4d7742 |
2 | if [ -z "$GITCLIENT" ]; then |
bba0734f |
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 |
1f4d7742 |
19 | # accepts -f parameter to skip last sync check |
4cbcb39e |
20 | gitsync() { |
1f4d7742 |
21 | FORCE=$1 |
bba0734f |
22 | if [[ $USEGIT && -n $GITREMOTE ]]; then |
bba0734f |
23 | [ $PLAIN == false ] && echo "Syncing notes with git on remote \"$GITREMOTE\"" |
1f4d7742 |
24 | NOWSYNC=$(date +%s) |
25 | if [[ $FORCE == "-f" ]]; then |
bba0734f |
26 | $JQ --arg n "$NOWSYNC" '.git["lastpull"] = $n' "$DB" > $TMPDB |
27 | mv $TMPDB $DB |
28 | cd $BASEDIR |
cf9f797b |
29 | [ $PLAIN == false ] && $GIT pull || $GIT pull -q |
bba0734f |
30 | else |
1f4d7742 |
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 |
37 | mv $TMPDB $DB |
38 | cd $BASEDIR |
cf9f797b |
39 | [ $PLAIN == false ] && $GIT pull || $GIT pull -q |
1f4d7742 |
40 | else |
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" |
43 | fi |
bba0734f |
44 | fi |
45 | else |
46 | # no git, so we just keep going |
47 | true |
48 | fi |
49 | } |
50 | |
51 | # add note to git and push it to remote |
52 | gitadd() { |
53 | if [[ $USEGIT && -n $GITREMOTE ]]; then |
54 | [ $PLAIN == false ] && echo "Adding note to remote \"$GITREMOTE\"" |
cf6d89bc |
55 | cd $BASEDIR |
bba0734f |
56 | $GIT add . |
57 | $GIT commit -m "$(basename $0) - adding note from ${GITCLIENT}" |
58 | $GIT push origin master |
cf6d89bc |
59 | else |
bba0734f |
60 | # no git, so we just keep going |
61 | true |
cf6d89bc |
62 | fi |
4cbcb39e |
63 | } |
64 | |
d1f115c1 |
65 | # edited note added to git and pushed it to remote |
66 | gitedit() { |
67 | if [[ $USEGIT && -n $GITREMOTE ]]; then |
68 | [ $PLAIN == false ] && echo "Editing note on remote \"$GITREMOTE\"" |
69 | cd $BASEDIR |
70 | $GIT add . |
71 | $GIT commit -m "$(basename $0) - ${GITCLIENT} note edited." |
72 | $GIT push origin master |
73 | else |
74 | # no git, so we just keep going |
75 | true |
76 | fi |
77 | } |
78 | |
79 | # add note to git and push it to remote |
80 | gitremove() { |
81 | NOTE=$1 |
82 | FILE=$2 |
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" |
87 | cd $BASEDIR |
88 | $GIT rm notes/* |
89 | $GIT commit -m "$(basename $0) - ${GITCLIENT} removing all notes." |
90 | $GIT push origin master |
91 | else |
92 | echo "Deleting note ID ${NOTE}" |
93 | local OK=$(check_noteID "$NOTE") |
94 | cd $BASEDIR |
95 | $GIT rm notes/${FILE} |
96 | $GIT add . |
97 | $GIT commit -m "$(basename $0) - ${GITCLIENT} removing note ID ${NOTE}." |
98 | $GIT push origin master |
99 | fi |
100 | else |
101 | # no git, so we just keep going |
102 | true |
103 | fi |
104 | } |
105 | |
4cbcb39e |
106 | # check for USEGIT and subsequent variables |
107 | if [[ $USEGIT && -n $GITREMOTE ]]; then |
108 | # GIT is a go. |
109 | if ! is_git_repo $BASEDIR; then |
110 | # initializing git repository |
111 | cd $BASEDIR |
112 | $GIT init |
113 | echo "adding all files to git" |
114 | $GIT add . |
bba0734f |
115 | $GIT commit -m "$(basename $0) - initial commit from ${GITCLIENT}" |
4cbcb39e |
116 | $GIT remote add origin $GITREMOTE |
117 | $GIT push -u origin master |
118 | fi |
119 | elif [[ $USEGIT && -z $GITREMOTE ]]; then |
120 | echo "GITREMOTE variable not set. reverting USEGIT to false" |
121 | USEGIT=false |
122 | fi |
123 | |