-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgit-sync-on-inotify
executable file
·39 lines (33 loc) · 1.14 KB
/
git-sync-on-inotify
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
29
30
31
32
33
34
35
36
37
38
39
#!/usr/bin/env bash
GIT_SYNC_DIRECTORY="${GIT_SYNC_DIRECTORY:-$(pwd)}"
GIT_SYNC_COMMAND="${GIT_SYNC_COMMAND:-git-sync}"
GIT_SYNC_INTERVAL="${GIT_SYNC_INTERVAL:-500}"
# Initialize the directory
if [ ! -d "$GIT_SYNC_DIRECTORY" ]; then
if [ -z "$GIT_SYNC_REPOSITORY" ]; then
echo "Please specify a value for GIT_SYNC_REPOSITORY in order to initialize the git repository"
exit 1
else
base="$(dirname $GIT_SYNC_DIRECTORY)"
mkdir -p "$base"
cd "$base"
git clone "$GIT_SYNC_REPOSITORY" "$(basename $GIT_SYNC_DIRECTORY)"
fi
fi
cd "$GIT_SYNC_DIRECTORY"
remote_name=$(git config --get branch.$(basename $(git symbolic-ref -q HEAD)).pushRemote)
echo "Syncing $(git remote get-url $remote_name) at $(pwd) with a default sync interval of $GIT_SYNC_INTERVAL"
while true; do
changedFile=$(
inotifywait "$GIT_SYNC_DIRECTORY" -r -e modify,move,create,delete \
--format "%w%f" --exclude '\.git' -t "$GIT_SYNC_INTERVAL" 2>/dev/null
)
if [ -z "$changedFile" ]
then
echo "Syncing due to timeout"
$GIT_SYNC_COMMAND -n -s
else
echo "Syncing for: $changedFile"
{ git check-ignore "$changedFile" > /dev/null; } || $GIT_SYNC_COMMAND -n -s
fi
done