Skip to content

Instantly share code, notes, and snippets.

@csrui
Created October 10, 2014 16:50
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 csrui/350b20b3fb19ae76f3c5 to your computer and use it in GitHub Desktop.
Save csrui/350b20b3fb19ae76f3c5 to your computer and use it in GitHub Desktop.
function update_git_repo() {
GIT_DIR=$1
GIT_REMOTE=$2
GIT_BRANCH=${3:-master}
if [ ! -d $GIT_DIR ]; then
CURRENT_SHA=""
git clone --depth 1 $GIT_REMOTE $GIT_DIR -b $GIT_BRANCH
else
CURRENT_REMOTE=$(cd $GIT_DIR && git config --get remote.origin.url || true)
if [ "$GIT_REMOTE" == "$CURRENT_REMOTE" ]; then
CURRENT_SHA=$(cat $GIT_DIR/.git/refs/heads/$GIT_BRANCH)
else
rm -Rf $GIT_DIR
exit 0 # Process manager should restart this script
fi
fi
cd $GIT_DIR && \
git fetch && \
git reset --hard origin/$GIT_BRANCH
NEW_SHA=$(cat $GIT_DIR/.git/refs/heads/$GIT_BRANCH)
}
update_git_repo "/tmp/myrepo" "git://example.com/my/repo.git"
sleep 60 # No need for a tight loop
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment