Skip to content

Instantly share code, notes, and snippets.

@makadev
Last active December 3, 2016 13:40
Show Gist options
  • Save makadev/3e4a00fbca703b5f7cf3779e51e36fca to your computer and use it in GitHub Desktop.
Save makadev/3e4a00fbca703b5f7cf3779e51e36fca to your computer and use it in GitHub Desktop.
#!/bin/bash
REPO_STORE=/home/git/repo-mirror
REPO_POS=${REPO_STORE}/${GL_REPO}.git
# change umask such that anyone can read the repositories (read/search for all, rw/search for owner)
umask 022;
# uncomment for local side information on push
#echo "**UPDATE MIRROR** @" ${REPO_POS}
# check if mirror repository exists, if not -> create a bare copy
if [[ ! -e "$REPO_POS" ]]; then
mkdir -p ${REPO_POS} > /dev/null 2>&1
cd ${REPO_POS} > /dev/null 2>&1
git init --bare "--shared=umask" > /dev/null 2>&1
cd - > /dev/null 2>&1
fi
# push that data...
git push --mirror ${REPO_POS} > /dev/null 2>&1
# make sure we leave with EC 0
exit 0
@makadev
Copy link
Author

makadev commented Nov 23, 2016

Gitolite repo-specific bare mirror hook

This creates a readable, bare copy of repositories for which this is enabled and keeps it up to date on every push. F.e. for redmine, gitweb, etc. that can't - and should not - access the main repository.

How to enable repo-specific and make that hook execute for a certain repository

  1. enable LOCAL_CODE => "$ENV{HOME}/local", (server only) or LOCAL_CODE => "$rc{GL_ADMIN_BASE}/local", (push-able via gitolite-admin) in .gitolite.rc
  2. enable 'repo-specific-hooks', in gitolite.rc
  3. create the directory if not existent in the gitolite home dir: mkdir -p local/hooks/repo-specific
  4. insert script and chmod +x local/hooks/repo-specific/bare-mirror-hook.sh
  5. add option hook.post-receive = bare-mirror-hook.sh for the repositories, add/commit/push

Note

  • I tried the same with VREF. While basically working the same (you can drop this into the VREF path and it works out of box) it seems VREF are triggered before the pushed data is integrated. So the script only copied the last state, not the current one. Maybe someone has insight on that and can drop a message on how to do that with VREF (since they are more powerful with argument passing and all).
  • This hook can easily be modified to do additional or other stuff like mirroring the specified repositories onto another disk or running a backup command (like on some Hosters VMs) to push the new data onto an internal FTP and so on.
  • For in depth info on repo-specific check gitolite documentation like http://gitolite.com/gitolite/cookbook.html#v3.6-variation-repo-specific-hooks.

Troubleshooting

  • Be sure to update your gitolite-admin config after modifying .gitolite.rc for gitolite to reload it (there may be another way with gitolite setup but I didn't test it). Check if the script local/hooks/multi-hook-driver was created, It's a sign that it worked.
  • Uncomment the update line, it should output something like remote: **UPDATE MIRROR** @ /home/git/repo-mirror/testing/testing.git, if not then the hook is simply not called f.e. due to a typo in the hook option.

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