Skip to content

Instantly share code, notes, and snippets.

@egregius313
Created December 19, 2018 01:45
Show Gist options
  • Save egregius313/da76279203c31ffe20291ef6b572f206 to your computer and use it in GitHub Desktop.
Save egregius313/da76279203c31ffe20291ef6b572f206 to your computer and use it in GitHub Desktop.
git-sync Synchronize files across machines via Git
#!/bin/bash
GITSYNC_REPOS=~/.gitsyncrepos
project_root () {
old_dir=$(pwd)
while ! ls .git 1>/dev/null 2>/dev/null
do
cd ..
done
pwd
cd $old_dir
}
git_sync_init () {
root=$(project_root)
if [ -z "$root" ]
then
echo No git repository 1>&2
return 1
fi
grep "$root" $GITSYNC_REPOS 1>/dev/null || echo "$root" >> $GITSYNC_REPOS
}
git_sync_updated_files () {
minutes=5
[ $# -ge 1 ] && minutes=$1
find $(git status -s | awk '{ print $2 }') -mmin $minutes
}
git_sync_synchronize () {
git remote get-url origin 2>/dev/null && git "$1"
}
git_sync_update_repos () {
while read repo
do
cd "$repo"
git_sync_synchronize pull
git add $(git_sync_update_files)
git commit -m "update at $(date)"
git_sync_synchronize push
cd ~
done < $GITSYNC_REPOS
}
git_sync_daemon () {
interval=300
[ $# -ge 1 ] && interval=$2
watch --interval $interval git_sync_update_repos &
}
git_sync_ls_repos () {
sort $GITSYNC_REPOS
}
case "$1" in
daemon)
git_sync_daemon
;;
init)
git_sync_init
;;
ls-repos)
git_sync_ls_repos
;;
update)
git_sync_update_files
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment