When working with WordPress core, you often have to go back and forth through commits, scroll or search through revision logs, switch between tags and branches and so on. This can be annoying with a slow Internet connection and impossible without a connection at all, but what if you’re working on a core patch on a plane or a submarine?
Lucky for us, Subversion comes with a remote repository mirroring tool called svnsync. It’s like database replication, but for Subversion. It allows us to create a complete mirror of the WordPress Subversion repository and host it locally, for quicker access. We can then set up a cron task to keep the mirror in sync every once in a while.
Preparing the Mirror Repository
It’s fairly easy to start a mirror. You’ll need to create an empty Subversion repository with svnadmin, like so:
mkdir /home/kovshenin/svn cd /home/kovshenin/svn svnadmin create wordpress
Since svnsync will need to modify revision properties after revisions have been committed, we’ll need to allow revision property changes, which are disabled by default. To do that, we’ll just have the pre-revprop-change hook exit with code 0:
cd wordpress/hooks echo '#!/bin/sh' > pre-revprop-change echo 'exit 0' >> pre-revprop-change chmod +x pre-revprop-change
Initializing the Mirror and Syncing
The destination repository is now ready to become a mirror.
svnsync init file:///home/kovshenin/svn/wordpress https://core.svn.wordpress.org svnsync sync file:///home/kovshenin/svn/wordpress
At this point, svnsync will start reading revisions from the WordPress Subversion repository, and replaying them on your local mirror. At the time of writing, WordPress has over 24,000 revisions, so this may take a while, especially on a slower connection. Another option is to use a dump file, which we’ll talk about in a sec. If the sync process is interrupted, you can start it again with “svnsync sync” and it’ll always pick up where it left.
Using the Mirror
After the sync is complete, you can use your new repository mirror to check out working copies, browse revision logs, switch branches and so on, which will be extremely fast:
cd /home/kovshenin/www svn checkout file:///home/kovshenin/svn/wordpress . svn log -r 9 svn sw ^/branches/3.5
If you’ve already have a working copy of the WordPress source code checked out, you can easily relocate it to your new local mirror with svn relocate:
cd /home/kovshenin/existing/ svn relocate file:///home/kovshenin/svn/wordpress/trunk
Note, that for a successful relocate, the UUID of both repositories should match. You can change the UUID of your mirror repository with svnadmin setuuid.
Finally, since running svn update on any working copy, will not pull in the new changes from the original Subversion repository, but rather update from your mirror, it’s important to keep your mirror in sync:
svnsync sync file:///home/kovshenin/svn/wordpress
It’s also a good idea to create a cron job that would run svnsync every hour or so.
The most painful part is reading all the 24k revisions from the WordPress Subversion repository, which can take a while, depending on your Internet connection. Luckily, there’s a trick you can use to speed up the process.
Starting from a Subversion Dump File
Before initializing the mirror, you can use a Subversion dump file (or backup file) to pre-populate your empty repository, which will only take a few minutes, and then have svnsync simply fill in the remaining revisions from source.
svnadmin load /home/kovshenin/svn/wordpress < wordpress.dump svnsync init --allow-non-empty file:///home/kovshenin/svn/wordpress https://core.svn.wordpress.org svnsync sync file:///home/kovshenin/svn/wordpress
So the question is where does one get a 24,000 revision dump file? Unfortunately, at the time of writing, there isn’t one you can get directly from WordPress.org, but you can download my dump file if you like.
That’s about it! Hopefully the above tips will help you maintain a local mirror and make your Subversion experience faster, as well as not require an Internet connection at all times. Make sure you keep your mirror in sync, and also make sure you never commit anything to your mirror manually — svnsync doesn’t like that, and may easily result in a corrupt repository. You can learn more about svnsync here.
A final word to Git and Mercurial fans: I love Git and Mercurial as much as you do, in fact, I use Git (not necessarily GitHub) on a daily basis. And if you came here to say I “should try git,” please go away :)