From 33132d593711fef037533616e7b46facef804f4e Mon Sep 17 00:00:00 2001 From: Ram Balachandran Date: Sun, 2 Jul 2023 17:17:25 +0930 Subject: [PATCH 1/7] modified to enable commits only after GIT_SYNC_INTERVAL --- contrib/git-sync-on-inotify | 51 ++++++++++++++++++++----------------- 1 file changed, 28 insertions(+), 23 deletions(-) diff --git a/contrib/git-sync-on-inotify b/contrib/git-sync-on-inotify index 0d5b031..11ffe6d 100755 --- a/contrib/git-sync-on-inotify +++ b/contrib/git-sync-on-inotify @@ -3,37 +3,42 @@ GIT_SYNC_DIRECTORY="${GIT_SYNC_DIRECTORY:-$(pwd)}" GIT_SYNC_COMMAND="${GIT_SYNC_COMMAND:-git-sync}" GIT_SYNC_INTERVAL="${GIT_SYNC_INTERVAL:-500}" +GIT_SLEEP_TIME=$((GIT_SYNC_INTERVAL - 10)) + # 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 + 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) +remote_name=$(git config --get branch.$(basename $(git symbolic-ref -q HEAD)).remote) + +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" -echo "Syncing $(git remote get-url $remote_name) at $(pwd) with a default sync interval of $GIT_SYNC_INTERVAL" +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 From 7a41ec8cc34baf75dc3e0f14dbebe6c90919d658 Mon Sep 17 00:00:00 2001 From: Ram Balachandran Date: Tue, 4 Jul 2023 08:25:19 +0930 Subject: [PATCH 2/7] improved formatting --- contrib/git-sync-on-inotify | 45 +++++++++++++++++-------------------- 1 file changed, 21 insertions(+), 24 deletions(-) diff --git a/contrib/git-sync-on-inotify b/contrib/git-sync-on-inotify index 11ffe6d..4ff9d67 100755 --- a/contrib/git-sync-on-inotify +++ b/contrib/git-sync-on-inotify @@ -5,18 +5,17 @@ GIT_SYNC_COMMAND="${GIT_SYNC_COMMAND:-git-sync}" GIT_SYNC_INTERVAL="${GIT_SYNC_INTERVAL:-500}" GIT_SLEEP_TIME=$((GIT_SYNC_INTERVAL - 10)) - # 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 +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" @@ -28,17 +27,15 @@ echo "Syncing $(git remote get-url $remote_name) at $(pwd) with a default sync i 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 - ) - 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 + 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 From 6f8748072244af2770bb19d00e2136ab398b1fde Mon Sep 17 00:00:00 2001 From: Ram Balachandran Date: Thu, 6 Jul 2023 16:26:23 +0930 Subject: [PATCH 3/7] small modifications --- contrib/git-sync-on-inotify | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/contrib/git-sync-on-inotify b/contrib/git-sync-on-inotify index db2fd47..8637e44 100755 --- a/contrib/git-sync-on-inotify +++ b/contrib/git-sync-on-inotify @@ -26,13 +26,14 @@ fi cd "$GIT_SYNC_DIRECTORY" -remote_name=$(git config --get branch.$(basename $(git symbolic-ref -q HEAD)).remote) +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 and a sleep time of $GIT_SLEEP_TIME" -last_sync=$(date +%s) $GIT_SYNC_COMMAND -n -s +last_sync=$(date +%s) + while true; do changedFile=$( inotifywait "$GIT_SYNC_DIRECTORY" -r -e modify,move,create,delete \ From e218a489c4802d615ef5b5df936cc525d4e3709a Mon Sep 17 00:00:00 2001 From: Ram Balachandran Date: Fri, 7 Jul 2023 16:15:31 +0930 Subject: [PATCH 4/7] created cronjob script --- contrib/git-sync-cron.sh | 7 +++++++ contrib/git-sync-on-inotify | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) create mode 100755 contrib/git-sync-cron.sh diff --git a/contrib/git-sync-cron.sh b/contrib/git-sync-cron.sh new file mode 100755 index 0000000..413310a --- /dev/null +++ b/contrib/git-sync-cron.sh @@ -0,0 +1,7 @@ +#!/bin/bash +# run this script in cronjob by editing crontab -e and following line +# @reboot /bin/bash +export GIT_SYNC_DIRECTORY="${HOME}/my_codes/github_repos/my_repos/codesScripts" +export GIT_SYNC_COMMAND="/usr/local/bin/git-sync" +export GIT_SYNC_INTERVAL=900 +/bin/bash /usr/local/bin/git-sync-on-inotify \ No newline at end of file diff --git a/contrib/git-sync-on-inotify b/contrib/git-sync-on-inotify index 8637e44..178d7a8 100755 --- a/contrib/git-sync-on-inotify +++ b/contrib/git-sync-on-inotify @@ -1,7 +1,7 @@ #!/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)) # Initialize the directory From 34af33ed4d3b178116b2fce98194632126f2995f Mon Sep 17 00:00:00 2001 From: Ram Balachandran Date: Fri, 7 Jul 2023 21:28:03 +0930 Subject: [PATCH 5/7] minor updates to cron script --- contrib/git-sync-cron.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contrib/git-sync-cron.sh b/contrib/git-sync-cron.sh index 413310a..fa2c951 100755 --- a/contrib/git-sync-cron.sh +++ b/contrib/git-sync-cron.sh @@ -1,7 +1,7 @@ #!/bin/bash # run this script in cronjob by editing crontab -e and following line # @reboot /bin/bash -export GIT_SYNC_DIRECTORY="${HOME}/my_codes/github_repos/my_repos/codesScripts" +export GIT_SYNC_DIRECTORY="" export GIT_SYNC_COMMAND="/usr/local/bin/git-sync" export GIT_SYNC_INTERVAL=900 /bin/bash /usr/local/bin/git-sync-on-inotify \ No newline at end of file From fcd47af97267d03b90c7e44e12af833f7e529812 Mon Sep 17 00:00:00 2001 From: Ram Balachandran Date: Wed, 18 Oct 2023 21:08:18 +1030 Subject: [PATCH 6/7] throw an error if inotify doesnt exist in the system --- contrib/git-sync-on-inotify | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/contrib/git-sync-on-inotify b/contrib/git-sync-on-inotify index 178d7a8..13b9234 100755 --- a/contrib/git-sync-on-inotify +++ b/contrib/git-sync-on-inotify @@ -4,6 +4,13 @@ GIT_SYNC_COMMAND="${GIT_SYNC_COMMAND:-git-sync}" 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 "command_name does not exist. exiting" + exit 1 +fi + + # Initialize the directory if [ ! -d "$GIT_SYNC_DIRECTORY" ]; then if [ -z "$GIT_SYNC_REPOSITORY" ]; then From c4ef6224608b8227e119a9697dbf94764bc2698a Mon Sep 17 00:00:00 2001 From: Ram Balachandran Date: Wed, 18 Oct 2023 21:13:34 +1030 Subject: [PATCH 7/7] minor bug fix --- contrib/git-sync-on-inotify | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/contrib/git-sync-on-inotify b/contrib/git-sync-on-inotify index 13b9234..56192b3 100755 --- a/contrib/git-sync-on-inotify +++ b/contrib/git-sync-on-inotify @@ -5,8 +5,8 @@ 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 "command_name does not exist. exiting" +if [ ! type inotifywait &> /dev/null ]; then + echo "inotifywait does not exist. exiting" exit 1 fi