Skip to content

Instantly share code, notes, and snippets.

@mcbrwr
Created March 2, 2014 08:44
Show Gist options
  • Save mcbrwr/9303757 to your computer and use it in GitHub Desktop.
Save mcbrwr/9303757 to your computer and use it in GitHub Desktop.
AutoGit. Monitor a working copy and add/commit/pull/push on file changes. Nice for staging servers, not for production ofcourse..
#!/bin/bash
sha=0
previous_sha=0
commitmsg="online changes (autogit)"
if [ $1 ]; then
repo=$1
else
repo='.'
fi
echo "--> sanity checking repo: $repo"
# check if repo exists
# todo: do something like "git status | grep fatal"
if [ ! -d "$repo/.git" ]; then
echo "FATAL. Directory $1 does not exist or is not a git repo."
exit 0
fi
echo "--> Repo sane. Monitoring."
cd $repo
update_sha() {
sha=`ls -laR $repo | sha1sum`
}
go () {
git add .
git commit -am "$commitmsg"
git pull
git push
echo
echo "--> Monitor: Monitoring working copy... (Press enter to force commit)"
}
changed () {
echo "--> Files changed, committing..."
go
previous_sha=$sha
}
compare () {
update_sha
if [[ $sha != $previous_sha ]] ; then changed; fi
}
run () {
echo "--> Monitor: Monitoring filesystem... (Press enter to force commit)"
while true; do
compare
read -s -t 1 && (
echo "--> Monitor: Forced Update..."
go
)
done
}
echo "--> Autogit: Init..."
run
@mcbrwr
Copy link
Author

mcbrwr commented Mar 2, 2014

todo: error checking in git actions (merge conflict MIGHT be nice to handle..)

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