Mercurial. Automatically Updating a Project with Hooks
After we moved from Subversion to Mercurial, all that remained was to port the SVN hooks for “deploying” the project. For production, we will update to the head of the “EXAMPLE” branch, that is, the main branch.
As a reminder, the path to the hg repository is “/var/hg”, the project name is “EXAMPLE”, and your username is “USER1”.
Preparing the Project and Settings
In the previous article, you should have configured Mercurial and initialized the project. Since our project directory is “/home/USER1/projects/EXAMPLE” and we don’t want to keep two working copies on the same server, we’ll:
- move the repository to our working projects directory
- create a symbolic link in “/var/hg/repos” pointing to the working project directory
mv /var/hg/repos/EXAMPLE /home/USER1/projects/EXAMPLE
ln -s /home/USER1/projects/EXAMPLE /var/hg/repos/EXAMPLE
The changegroup Hook
Now let’s create a directory for hooks, create the hook itself, and edit the config:
mkdir /home/USER1/projects/EXAMPLE/.hg/hooks
touch /home/USER1/projects/EXAMPLE/.hg/hooks/changegrouphook
chmod +x /home/USER1/projects/EXAMPLE/.hg/hooks/changegrouphook
emacs /home/USER1/projects/EXAMPLE/.hg/hgrc
Make the config look like this:
[hooks]
changegroup = /home/USER1/projects/EXAMPLE/.hg/hooks/changegrouphook
The “changegroup” hook is triggered when the push/pull/unbundle commands are invoked, and only once, unlike the “commit” hook, which is invoked for every “changeset”.
Make the contents of the “changegrouphook” hook file look like this:
cd /home/USER1/projects/EXAMPLE && /usr/local/bin/hg update EXAMPLE
You can read more about hooks on the official documentation page and in Bryan O’Sullivan’s book “Mercurial: The Definitive Guide”.
Comments are closed. Read the original discussion in Russian →
