git-sync

Tool for periodic syncing of git repositories
git clone https://git.sinitax.com/sinitax/git-sync
Log | Files | Refs | sfeed.txt

commit 2e110a3885f1f197e3f918c41652a5df18a7976d
parent b09b0c13f3e5d7e3fcd56109e0b9400ea6aeb2b3
Author: Louis Burda <contact@sinitax.com>
Date:   Tue, 17 Jun 2025 10:50:02 +0200

Impove timeout behavior and log to different files

Diffstat:
Mgit-syncd | 59+++++++++++++++++++++++++++++++++++++++++++++--------------
1 file changed, 45 insertions(+), 14 deletions(-)

diff --git a/git-syncd b/git-syncd @@ -3,16 +3,20 @@ GIT_SYNCD_CONFIG_DIR=${GIT_SYNCD_CONFIG_DIR:-"$HOME/.config/git-sync"} GIT_SYNCD_CONFIG=${GIT_SYNCD_CONFIG:-"$GIT_SYNCD_CONFIG_DIR/syncd.rc"} +git_syncd=$0 + git_syncd_repos=() -git_syncd_ignore=() + +# auto sync exclude regex pattern +declare -g -A git_syncd_exclude=() # maximum time seconds between fetching changes and syncing git_syncd_default_sync_timeout=600 -declare -A git_syncd_sync_timeout +declare -g -A git_syncd_sync_timeout=() # number of seconds to wait after deteced change to push git_syncd_default_sync_delay=300 -declare -A git_syncd_sync_delay +declare -g -A git_syncd_sync_delay=() sync() { echo "starting sync.." @@ -21,19 +25,21 @@ sync() { watcher() { repo=$1 - excludes=() + echo $repo ${git_syncd_exclude[@]} ${git_syncd_exclude[$repo]} + exclude=${git_syncd_exclude[$repo]:-'\.git'} sync_delay=${git_syncd_sync_delay[$repo]:-$git_syncd_default_sync_delay} sync_timeout=${git_syncd_sync_timeout[$repo]:-$git_syncd_default_sync_timeout} sync "$repo" timeout=$sync_timeout while true; do - file=$(timeout "$timeout" inotifywait "$repo" -r \ - -e modify,move,create,delete --exclude '\.git' \ - ${excludes[@]} 2>/dev/null) + echo "watching (${timeout}s)" + file=$(timeout "$timeout" wd "$repo" inotifywait "." -r \ + -e modify,move,create,delete --exclude "$exclude" 2>/dev/null) + rc=$? if [ ! -z "$file" ]; then echo "inotify wake: $file" timeout=$sync_delay - else + elif [ $rc -eq 124 ]; then sync "$repo" timeout=$sync_timeout fi @@ -46,38 +52,63 @@ log_prefix() { repo_name=$(basename "$repo") repo_dir_name=$(basename $(dirname "$repo")) while read -r line; do - prefix="$(date "+%Y-%m-%d %T") $repo_dir_name/$repo_name" + prefix="$(date "+%Y-%m-%d %T")" #$repo_dir_name/$repo_name" prefix="$(echo "$prefix" | tr -d "\"" | tr -d "'")" echo "[$prefix] $line" done } +kill_all() { + pkill -A -9 git-syncd +} + git_syncd_watchers=() kill_watchers() { - for pid in "${git_syncd_watchers}"; do - kill -9 "$pid" &>/dev/null + kill_all + return + fail=0 + for pid in "${git_syncd_watchers[@]}"; do + echo "killing $pid.." >&2 + pkill -P "$pid" &>/dev/null waitpid -t 10 "$pid" &>/dev/null - kill -0 "$pid" &>/dev/null && notify-send "Failed to kill git-syncd watcher" + kill -0 -P "$pid" &>/dev/null && notify-send "Failed to kill git-syncd watcher" && fail=1 done + [ $fail -ne 0 ] && exit 1 git_syncd_watchers=() } start_watchers() { for repo in "${git_syncd_repos[@]}"; do echo "watching '$repo'.." - watcher "$repo" 2>&1 | log_prefix "$repo" & + $git_syncd watch "$repo" & git_syncd_watchers+=($!) done } load_config() { + git_syncd_repos=() + declare -g -A git_syncd_exclude=() + declare -g -A git_syncd_sync_delay=() + declare -g -A git_syncd_sync_timeout=() if [ -e "$GIT_SYNCD_CONFIG" ]; then echo "sourcing '$GIT_SYNCD_CONFIG'.." source "$GIT_SYNCD_CONFIG" fi } +if [ "$1" = "watch" ]; then + shift + repo="$1"; shift + repo_dir_name="$(basename $(dirname "$repo"))" + repo_name="$(basename "$repo")" + log_path="$HOME/.log/git-sync/$repo_dir_name" + mkdir -p "$log_path" + load_config + watcher "$repo" 2>&1 | log_prefix "$repo" > "$log_path/$repo_name" + exit +fi + load_config -trap kill_watchers EXIT +trap kill_all EXIT start_watchers while true; do file=$(inotifywait --format "%w%f" -e modify,delete "$GIT_SYNCD_CONFIG")