From 7371f0aeaae6af46d143a82bdcbf1f845d7a51a0 Mon Sep 17 00:00:00 2001 From: Ivan Malison Date: Thu, 14 Jun 2018 16:00:30 -0700 Subject: [PATCH] [Linux] Start using git-sync to auto-synchronize directories --- .../config/systemd/user/git-sync@.service | 12 +++++++++ dotfiles/lib/bin/git_sync_directory.sh | 25 +++++++++++++------ tasks/shell/enable_user_units.sh | 17 +++++++++++++ 3 files changed, 47 insertions(+), 7 deletions(-) create mode 100644 dotfiles/config/systemd/user/git-sync@.service create mode 100755 tasks/shell/enable_user_units.sh diff --git a/dotfiles/config/systemd/user/git-sync@.service b/dotfiles/config/systemd/user/git-sync@.service new file mode 100644 index 00000000..0ce1a70d --- /dev/null +++ b/dotfiles/config/systemd/user/git-sync@.service @@ -0,0 +1,12 @@ +[Unit] +Description=git-sync %f %I %i + +[Service] +Type=simple +ExecStart=/usr/bin/env git_sync_directory.sh %f +ExecStop=/usr/bin/env pkill git_sync_directory.sh +Restart=always +RestartSec=3 + +[Install] +WantedBy=wm.target diff --git a/dotfiles/lib/bin/git_sync_directory.sh b/dotfiles/lib/bin/git_sync_directory.sh index ce756041..088a8a3d 100755 --- a/dotfiles/lib/bin/git_sync_directory.sh +++ b/dotfiles/lib/bin/git_sync_directory.sh @@ -1,11 +1,22 @@ -#!/usr/bin/env bash - -cd "$1" +#!/usr/bin/env zsh echo "Syncing $1" -git-sync -enable_git_sync.sh +cd "$1" -while changedFile=$(inotifywait ./ -r -e modify,move,create,delete --format "%w%f" --exclude '\.git' 2>/dev/null); do - git check-ignore "$changedFile" >> /dev/null || git-sync +counter=0 +while true; do + changedFile=$(inotifywait ./ -r -e modify,move,create,delete --format "%w%f" --exclude '\.git' -t 20 2>/dev/null) + if [ -z "$changedFile" ] + then + counter=$((counter+1)) + if [ $counter -gt 4 ]; then + git-sync + counter=0 + else + git-sync > /dev/null + fi + else + echo "Syncing for: $changedFile" + { git check-ignore "$changedFile" > /dev/null; } || git-sync + fi done diff --git a/tasks/shell/enable_user_units.sh b/tasks/shell/enable_user_units.sh new file mode 100755 index 00000000..9679fedd --- /dev/null +++ b/tasks/shell/enable_user_units.sh @@ -0,0 +1,17 @@ +#!/usr/bin/env sh + +enable_git_sync () { + unit_name=$(systemd-escape -p "$1" --template git-sync@.service) + echo $unit_name + systemctl --user enable "$unit_name" +} + + unit_name=$(systemd-escape -p "$HOME/.password-store" --template git-sync@.service) + + +cd "$HOME/.config/systemd/user/" +find * -type f | grep -v git-sync | grep -E "\.service$" | xargs -I unitname sh -c 'echo unitname && systemctl --user enable unitname' + +enable_git_sync "$HOME/org" +enable_git_sync "$HOME/config" +enable_git_sync "$HOME/.password-store"