Bash script to keep a git clone synced with a remote
Use the following under a process manager (such as runit) to keep a local git clone in sync with a remote, when a push based solution isn’t an option. Most other versions either neglect to verify remote is correct, or use git pull which can fail if someone has been monkeying with the local version.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
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
|