Skip to content

Instantly share code, notes, and snippets.

@ianb
Created January 31, 2012 06:34
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ianb/1709249 to your computer and use it in GitHub Desktop.
Save ianb/1709249 to your computer and use it in GitHub Desktop.
An attempt at a simple sync command for git
#!/usr/bin/env bash
set -e
if [ "$1" = "-h" ] || [ "$1" = "--help" ] ; then
echo "usage: git sync [remote-name]"
echo
echo "Sync's the current repository to another remote host, using git,"
echo "but not using this repository."
echo
echo "You must set sync.default.repo (the path to store the repository)"
echo "and git.default.remote (the remote repository to push to). Example:"
echo "$ git config --add sync.default.repo .remote-repo"
echo "$ git config --add sync.default.remote git@hostname:/path.git"
exit
fi
if [ -z "$1" ] ; then
dest=default
else
dest="$1"
fi
repo_location="$(git config --get --path sync.${dest}.repo || true)"
if [ -z "$repo_location" ] ; then
repo_location="$(git config --get --path sync.repo || true)"
if [ -z "$repo_location" ] ; then
repo_location=/tmp/repos
fi
repo_location="${repo_location}/$(basename $(pwd))"
fi
if [ ! -e $repo_location ] ; then
remote="$(git config --get sync.${dest}.remote || true)"
if [ -z "$remote" ] ; then
echo "You must set sync.${dest}.remote"
echo "Like:"
echo " git config --add sync.${dest}.remote git@host.com:/path.git"
exit 2
fi
echo "making git repo in $repo_location"
mkdir -p "$(basename $repo_location)"
git clone $remote $repo_location
fi
if [ -e .syncignore ] ; then
rsync_option="--exclude-from=.syncignore"
else
rsync_option=""
fi
## FIXME: should I exclude untracked files? Seems like it
rsync $rsync_option --recursive --delete --exclude .git . $repo_location
if [ -e .syncignore ] ; then
cat .syncignore >> $repo_location/.gitignore
fi
build_command="$(git config --get sync.${dest}.build || true)"
if [ -n "$build_command" ] ; then
echo "Running $build_command"
(
cd $repo_location
$build_command
)
fi
version="$(git describe --always --dirty)"
(
cd $repo_location
adds="$(git status -s | awk '/^\?\?/ {print $2}')"
if [ -n "$adds" ] ; then
git add $adds
fi
git commit -a -m "deployment $version"
git push
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment