Skip to content

Instantly share code, notes, and snippets.

@zachwalton
Last active September 25, 2017 20:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zachwalton/395c0916081aac1f00d2f3b0f8cf306d to your computer and use it in GitHub Desktop.
Save zachwalton/395c0916081aac1f00d2f3b0f8cf306d to your computer and use it in GitHub Desktop.
rsync files when an fswatch event is detected
#!/bin/bash
function tracked_by_git() {
cd $(dirname $1)
git ls-files --error-unmatch $1 &>/dev/null
ret=$?
cd - &>/dev/null
return $ret
}
function remote_sync() {
echo "Syncing $1 to $2..."
local_dir=$1
IFS=: read remote_host remote_dir <<< "$2"
fswatch $local_dir | while read event; do
if ! tracked_by_git "$event"; then
echo 1>&2 "[debug] ignoring file not tracked by git: ${event}..."
else
remote_path="$remote_dir${event#$local_dir}"
echo "[info] syncing $event to $remote_host:$remote_path..."
rsync &>/dev/null $event $remote_host:$remote_path
echo "[info] synced $event to $remote_host:$remote_path..."
fi
done
}
@zachwalton
Copy link
Author

zachwalton commented Sep 25, 2017

✘-INT ~/git/dotfiles [master|✚ 2]
13:43 $ remote_sync ~/git/puppet remote:projects/puppet
Syncing /Users/zachwalton/git/puppet to remote:projects/puppet...
[info] syncing /Users/zachwalton/git/puppet/.ackrc to remote:projects/puppet/.ackrc...
[info] synced /Users/zachwalton/git/puppet/.ackrc to remote:projects/puppet/.ackrc...
✔ ~/git/puppet [develop ↓·112|✚ 1⚑ 2]
11:09 $ echo >> .ackrc

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment