Archive for March, 2012

SEISMIC – Really Simple Mercurial Sharing

March 17, 2012 Leave a comment

SEISMIC stands for:

S – Self
E – Explanatory
I – Installer
S – Sharing
M – Mercurial
I – Intercourse
C – Cupcakes

Okay, so the last two words don’t fit with the topic. I had to put them in to pad out the acronym. Here’s how you run it:

sudo bash

Ideally you’ll be typing those two lines into a fresh install of Ubuntu 11.10. Even if your install isn’t so fresh, the script tries to only set things up if they haven’t already been set up.

After it does the first-time steps, it offers you some options:

[0] Quit
[1] Add user
[2] Add repository

Like I say, it’s self-explanatory. Maybe there’s a quicker way to get started and do the obvious maintenance tasks for sharing mercurial repositories, but I don’t know about it yet.

Once you’ve run it, you can see your new shared repositories here:


You’ll need to log in using one of the user accounts you’ve created. You can also clone a repository on a client machine:

hg clone http://your-vm-hostname/hg/your-repository

And you can commit and push changes back to it – again, hg push will require your Mercurial username/password.

Running Windows? Oh dear. Why not set up a VM? (Don’t have any VM hosting software? VirtualBox is free).

The config created by this script is very simple (so it has a high probability of working). But on the downside it’s not really secure, basic basic (plain text) authentication is used. But it gives you a working starting point to investigate further, e.g. as the song goes, “If you liked it then you should have put a certificate on it”.

Tips for setting up a VM:

– Download the Ubuntu server 64-bit .iso

– Set up your VM so it has a terabyte virtual disk and grows on demand, and a bridged network connection instead of NAT.

– During install you should get to specify a suitable hostname. If not (or you change your mind about it), after your first login:

sudo nano /etc/hostname

Then reboot.

In case the script download site goes wrong, here’s what it contains:

# Install apache and mercurial
apt-get install apache2 mercurial

# Create dir /var/hg/repos where all repositories will live
if [ -d /var/hg/repos ]
  echo "Already created /var/hg/repos"
  mkdir /var/hg
  mkdir /var/hg/repos
  chown -R www-data:www-data /var/hg/repos

  # Allow pushing without SSL
  echo "[web]" >> /etc/mercurial/hgrc
  echo "allow_push = *" >> /etc/mercurial/hgrc
  echo "push_ssl = false" >> /etc/mercurial/hgrc

# Copy the hg .cgi script into place and make it runnable
if [ -a /var/hg/hgweb.cgi ]
  echo "Already created /var/hg/hgweb.cgi"
  cp /usr/share/doc/mercurial/examples/hgweb.cgi /var/hg/hgweb.cgi
  chmod a+x /var/hg/hgweb.cgi
  sed -i.bak "s|/path/to/repo/or/config|/var/hg/hgweb.config|" /var/hg/hgweb.cgi

if [ -a /var/hg/hgweb.config ]
  echo "Already created /var/hg/hgweb.config"
  echo "[paths]
/ = /var/hg/repos/*" > /var/hg/hgweb.config

# Configure Apache
if grep /var/hg/hgweb.cgi /etc/apache2/sites-available/default
  echo "Already configured Apache"
  sed -i.bak 's|</VirtualHost>|ScriptAlias /hg \"/var/hg/hgweb.cgi\"\
  <Location /hg>\
  AuthType Basic\
  AuthName \"Mercurial repositories\"\
  AuthUserFile /var/hg/hgusers\
  Require valid-user\
  </VirtualHost>|' /etc/apache2/sites-available/default
  apache2ctl restart


while [ $shouldQuit == false ]
  echo ""
  echo "[0] Quit"
  echo "[1] Add user"
  echo "[2] Add repository"

  read menuoption

  case $menuoption in
    0) shouldQuit=true;;

    1) echo -n "Creating new Mercurial user - give them a name:"
       read hgnewusername
       if [ -a /var/hg/hgusers ] 
         htpasswd -m /var/hg/hgusers $hgnewusername
         htpasswd -mc /var/hg/hgusers $hgnewusername

    2) echo ""
       echo "Existing repositories:"
       ls /var/hg/repos
       echo ""
       echo -n "Enter name for new repository:"
       read hgrepname
       echo -n "Enter contact name:"
       read hgrepcont
       echo -n "Enter description:"
       read hgrepdesc
       cd /var/hg/repos
       mkdir $hgrepname
       cd $hgrepname
       hg init
       echo "[web]
contact = $hgrepcont
description = $hgrepdesc" > .hg/hgrc
       cd ..
       chown -R www-data:www-data .

Categories: Uncategorized Tags: ,