Auto sync GIT repos in MAC
Use the following to auto-commit your changes in git repo. The commit will include the file changed. You can control the monitor interval by changing the variable sleeptime
in git-commit-repos.sh
. All the git operations are logged in /tmp/gitwatch-commit.log
.
Use cases:
- syncing your notes repository automatically eg. Sublime ZK
Requirements:
Steps:
- Make sure all the required softwares are installed
- Create new folder
gitwatch
in your home folder
- Copy
git-commit-repos.sh
and git-commit-unattended.sh
files in the gitwatch folder
git-commit-repos.sh
Bash |
---|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 | #!/bin/bash
# send notification on start
/usr/local/opt/terminal-notifier/bin/terminal-notifier -title "Status" -message "Gitwatch online."
repos=(
"/Users/dev/git/scripts"
"<ANOTHER REPO PATH>"
"<YET ANOTHER REPO PATH>"
);
path="$HOME/gitwatch/"
sleeptime=600
for repo in ${repos[*]}
do
/usr/local/opt/terminal-notifier/bin/terminal-notifier -title "Gitwatch" -message "working on $repo"
running=$(ps aux | grep -wv "^\s*$$" | grep -v "grep" | grep "/usr/local/opt/fswatch/bin/fswatch" | grep $repo | wc -l);
## exit if already running
if [ $running -gt 0 ];then
/usr/local/opt/terminal-notifier/bin/terminal-notifier -title "Gitwatch" -message "Exiting on $repo"
exit 0;
fi
## exclude files with -e <file>
## 4913 is file created by neovim on edit
/usr/local/opt/fswatch/bin/fswatch -r "$repo" -l $sleeptime -e '.vscode/settings.json' -e '.saved_searches.zks' -e ".search_results.zkr" -e ".lock" -e "4913" -o -e ".DS_Store" -e ".git" -e ".*\.swp" -e ".*~" --event=Created --event=Updated --event=Removed -0 | xargs -0 -n 1 -I {} basename {} | xargs -I{} "$PATH/git-commit-unattended.sh" "$repo" {} &>> /tmp/gitwatch-commit.log &
done
|
Bash |
---|
1
2
3
4
5
6
7
8
9
10
11
12
13
14 | #!/bin/bash
echo "\n`date`\n=============\n"$1"\n"
echo "starting commiting... $1"
cd "$1"
CHAGNEdFILES=`git status --porcelain | cut -c 4- | tr '\n\r' ',' | sed -e 's/\%$//' | sed -e 's/,$//'`
/usr/bin/git -C "$1" pull
/usr/bin/git -C "$1" add .
/usr/bin/git -C "$1" commit -m "Changed: $CHAGNEdFILES"
/usr/bin/git -C "$1" push
echo "finished commiting"
/usr/local/opt/terminal-notifier/bin/terminal-notifier -title "$1" -message "synced"
|
- Copy
com.gitwatch.dev.plist
in ~/Library/LaunchAgents
com.gitwatch.dev.plist
Text Only |
---|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 | <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC -//Apple Computer//DTD PLIST 1.0//EN http://www.apple.com/DTDs/PropertyList-1.0.dtd>
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.gitwatch.dev</string>
<key>ProgramArguments</key>
<array>
<string>bash</string>
<string>-c</string>
<string>~/gitwatch/git-commit-repos.sh</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>WorkingDirectory</key>
<string>~/Documents/</string>
<key>StandardOutPath</key>
<string>/tmp/gitwatch/gitwatch.out.log</string>
<key>StandardErrorPath</key>
<string>/tmp/gitwatch/gitwatch.err.log</string>
</dict>
</plist>
|
- Run following commands in terminal
launchctl load ~/Library/LaunchAgents/com.gitwatch.dev.plist
launchctl start ~/Library/LaunchAgents/com.gitwatch.dev.plist
- Change the
repo
, path
and sleeptime
as desired.
- Check if working via
tail -f /tmp/gitwatch-commit.log
bash -x git-commit-repos.sh
Sources: