Skip to content

modified to enable sync only after GIT_SYNC_INTERVAL #30

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions contrib/git-sync-cron.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash
# run this script in cronjob by editing crontab -e and following line
# @reboot /bin/bash <path/to/this/script>
export GIT_SYNC_DIRECTORY="<path-to-your-git-repo>"
export GIT_SYNC_COMMAND="/usr/local/bin/git-sync"
export GIT_SYNC_INTERVAL=900
/bin/bash /usr/local/bin/git-sync-on-inotify
38 changes: 24 additions & 14 deletions contrib/git-sync-on-inotify
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
#!/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}"
GIT_SYNC_INTERVAL="${GIT_SYNC_INTERVAL:-600}"
GIT_SLEEP_TIME=$((GIT_SYNC_INTERVAL - 10))

# Check if inotif exist otherwise exit
if [ ! type inotifywait &> /dev/null ]; then
echo "inotifywait does not exist. exiting"
exit 1
fi


# Initialize the directory
if [ ! -d "$GIT_SYNC_DIRECTORY" ]; then
Expand All @@ -26,21 +34,23 @@ 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"

echo "Syncing $(git remote get-url $remote_name) at $(pwd) with a default sync interval of $GIT_SYNC_INTERVAL and a sleep time of $GIT_SLEEP_TIME"

$GIT_SYNC_COMMAND -n -s

last_sync=$(date +%s)

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
changedFile=$(
inotifywait "$GIT_SYNC_DIRECTORY" -r -e modify,move,create,delete \
--format "%w%f" --exclude '\.git' -t "$GIT_SYNC_INTERVAL" 2>/dev/null
)
current_time=$(date +%s)
time_since_last_sync=$((current_time - last_sync))
if [ ! -z "$changedFile" ] && [ $time_since_last_sync -ge $GIT_SLEEP_TIME ]; then
echo "Syncing for: $changedFile"
{ git check-ignore "$changedFile" >/dev/null; } || $GIT_SYNC_COMMAND -n -s
last_sync=$(date +%s)
fi
done