git-sync

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

git-syncd (2301B)


      1#!/bin/bash
      2
      3GIT_SYNCD_CONFIG_DIR=${GIT_SYNCD_CONFIG_DIR:-"$HOME/.config/git-sync"}
      4GIT_SYNCD_CONFIG=${GIT_SYNCD_CONFIG:-"$GIT_SYNCD_CONFIG_DIR/syncd.rc"}
      5
      6git_syncd_repos=()
      7git_syncd_ignore=()
      8
      9# maximum time seconds between fetching changes and syncing
     10git_syncd_default_sync_timeout=600
     11declare -A git_syncd_sync_timeout
     12
     13# number of seconds to wait after deteced change to push
     14git_syncd_default_sync_delay=300
     15declare -A git_syncd_sync_delay
     16
     17sync() {
     18	echo "starting sync.."
     19	cd "$repo" && git-sync -n -s
     20}
     21
     22watcher() {
     23	repo=$1
     24	excludes=()
     25	sync_delay=${git_syncd_sync_delay[$repo]:-$git_syncd_default_sync_delay}
     26	sync_timeout=${git_syncd_sync_timeout[$repo]:-$git_syncd_default_sync_timeout}
     27	sync "$repo"
     28	timeout=$sync_timeout
     29	while true; do
     30		file=$(timeout "$timeout" inotifywait "$repo" -r \
     31			-e modify,move,create,delete --exclude '\.git' \
     32			${excludes[@]} 2>/dev/null)
     33		if [ ! -z "$file" ]; then
     34			echo "inotify wake: $file"
     35			timeout=$sync_delay
     36		else
     37			sync "$repo"
     38			timeout=$sync_timeout
     39		fi
     40		sleep 1
     41	done
     42}
     43
     44log_prefix() {
     45	repo="$1"
     46	repo_name=$(basename "$repo")
     47	repo_dir_name=$(basename $(dirname "$repo"))
     48	while read -r line; do
     49		prefix="$(date "+%Y-%m-%d %T") $repo_dir_name/$repo_name"
     50		prefix="$(echo "$prefix" | tr -d "\"" | tr -d "'")"
     51		echo "[$prefix] $line"
     52	done
     53}
     54
     55git_syncd_watchers=()
     56kill_watchers() {
     57	for pid in "${git_syncd_watchers}"; do
     58		kill -9 "$pid" &>/dev/null
     59		waitpid -t 10 "$pid" &>/dev/null
     60		kill -0 "$pid" &>/dev/null && notify-send "Failed to kill git-syncd watcher"
     61	done
     62	git_syncd_watchers=()
     63}
     64start_watchers() {
     65	for repo in "${git_syncd_repos[@]}"; do
     66		echo "watching '$repo'.."
     67		watcher "$repo" 2>&1 | log_prefix "$repo" &
     68		git_syncd_watchers+=($!)
     69	done
     70}
     71
     72load_config() {
     73	if [ -e "$GIT_SYNCD_CONFIG" ]; then
     74		echo "sourcing '$GIT_SYNCD_CONFIG'.."
     75		source "$GIT_SYNCD_CONFIG"
     76	fi
     77}
     78
     79load_config
     80trap kill_watchers EXIT
     81start_watchers
     82while true; do
     83	file=$(inotifywait --format "%w%f" -e modify,delete "$GIT_SYNCD_CONFIG")
     84	if [ ! -z "$file" ]; then
     85		mod_path="$(realpath "$file" 2>/dev/null)"
     86		config_path="$(realpath "$GIT_SYNCD_CONFIG" 2>/dev/null)"
     87		echo "config $mod_path $config_path"
     88		if [ "$mod_path" = "$config_path" ]; then
     89			kill_watchers
     90			load_config
     91			start_watchers
     92		fi
     93	fi
     94	sleep 2
     95done