cleanup of the output of the program. Updated README.
[bash-notes.git] / SOURCE / CORE / git.sh
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
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 # accepts -f parameter to skip last sync check
20 gitsync() {
21 FORCE=$1
22 if [[ $USEGIT && -n $GITREMOTE ]]; then
23 [ $PLAIN == false ] && echo "Syncing notes with git on remote \"$GITREMOTE\""
24 NOWSYNC=$(date +%s)
25 if [[ $FORCE == "-f" ]]; then
26 $JQ --arg n "$NOWSYNC" '.git["lastpull"] = $n' "$DB" > $TMPDB
27 mv $TMPDB $DB
28 cd $BASEDIR
29 [ $PLAIN == false ] && $GIT pull || $GIT pull -q
30 else
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
39 [ $PLAIN == false ] && $GIT pull || $GIT pull -q
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
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\""
55 cd $BASEDIR
56 $GIT add .
57 $GIT commit -m "$(basename $0) - adding note from ${GITCLIENT}"
58 $GIT push origin master
59 else
60 # no git, so we just keep going
61 true
62 fi
63 }
64
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 local OK=$(check_noteID "$NOTE")
93 if [[ "$OK" ]]; then
94 echo "Deleting note ID ${NOTE}"
95 cd $BASEDIR
96 $GIT rm notes/${FILE}
97 $GIT add .
98 $GIT commit -m "$(basename $0) - ${GITCLIENT} removing note ID ${NOTE}."
99 $GIT push origin master
100 fi
101 fi
102 else
103 # no git, so we just keep going
104 true
105 fi
106 }
107
108 # check for USEGIT and subsequent variables
109 if [[ $USEGIT && -n $GITREMOTE ]]; then
110 # GIT is a go.
111 if ! is_git_repo $BASEDIR; then
112 # initializing git repository
113 cd $BASEDIR
114 $GIT init
115 echo "adding all files to git"
116 $GIT add .
117 $GIT commit -m "$(basename $0) - initial commit from ${GITCLIENT}"
118 $GIT remote add origin $GITREMOTE
119 $GIT push -u origin master
120 fi
121 elif [[ $USEGIT && -z $GITREMOTE ]]; then
122 echo "GITREMOTE variable not set. reverting USEGIT to false"
123 USEGIT=false
124 fi
125