Shapejam

Trying to follow Buckminster Fuller's example in life, one step at a time to bring you brain scrapings for a new millennium.

Setting up Hg on a Windows machine... Part 1

06 Mar 2012 - Nic Ho Chee

Like most programmers I have a workflow which allows me to type code at one end, and a seamless, bugfree application will pop out of the other ;).  Some part of the workflow invariably involves the use of Source Control Software, which at a minimum allows me to store away a versioned list of work so if something untoward should happen to my workstation my code and resources are at least retrievable in one form or another.

Over the years I have used many Source Control systems, the most recent CVS/SVN, and have issues with most of them (a subject of another blog…) but in 2010 a tool forced it’s way into my workflow… Mercurial (Hg).  This is part one of my adventure in getting this running on windows.

What is Hg 

It's a source control system, but not as we know it.  You don't have a single central repo unless you explicitly create one with a workflow.  Each user can clone a repo, do work on a repo and let other users clone their new repo and commit changes at any level, gradually merging changes back into the main "branch" as needed.  This allows users to create their own clone of the “main” repo (which can be backed up separately) without affecting other users… other users can get and work on this repo, and when ready the code will be promoted back into the main repo (try doing that with branches in CVS and I think you will prolly end up crying at some stage…)

For more information have a look at the use case workflows and the wiki that covers a basic explanation of it.

http://mercurial.selenic.com/guide/
http://mercurial.selenic.com/wiki/UnderstandingMercurial

What Do I Need To Try This Out

In this first part I’ve got Hg running on a basic webserver.  Using these apps you can set up pushes/pulls to a repo and test a basic setup of Hg that is visible to multiple users.  The next step is to get a security model and pulling/pushing from SVN/CVS which I’ll cover in another post.  To Try this out you need the following:

  1. The latest version of mercurial, I’m using 1.4.2 without Tortoise… showing the command line some love :). http://mercurial.selenic.com/wiki/Download?action=show&redirect=BinaryPackages#Windows.
  2. The version of Python that this version of Hg is built against.  In this case it's Python 2.5.4.
  3. Perl to handle cgi.  I’m loving Strawberry perl at the moment, you can get it here: http://strawberryperl.com/
  4. A web server of some sort.  I’ve gone for the free server Abyss, but it works on Apache also. http://www.aprelium.com/

How To… Install

hg init  
http://mercurial.selenic.com/wiki/PublishingRepositories
http://mercurial.selenic.com/wiki/HgWebDirStepByStep  
[ui] username = Bob A <boba@doesntexist.com>  
cd /d c:\temp mkdir MercurialTest cd MercurialTest mkdir ProjectA cd ProjectA hg init  
echo thisisastring > Test1.txt echo thisisastringalso > Test2.txt  
hg add  
hg commit  
hg clone http://selenic.com/repo/hg  
cp C:\temp\MercurialTest\Mercurial\hg\hgwebdir.cgi C:\Program Files\Mercurial  
#!c:/Python25/python.exe # # An example CGI script to export multiple hgweb repos, edit as necessary  
# adjust python path if not a system-wide install: import sys sys.path.insert(0, "c:/Program Files/Mercurial/lib")  
# [collections] CodeRepo = C:\temp\MercurialTest  

Tab containing the Abyss Web Server Console

Interpreters: CGI/ISAPI c:\strawberry\perl\bin\perl510.dll  cgi pl  
Virtual Path /cgi-bin/hgwebdir.cgi  
<html> <head> </head> <body> This is a test. </body> </html>  

Before we go any further it’s probably best to test that you can access the repos both on the same machine and off machine to ensure that everything is working as expected.

To test on the webserved machine:

mkdir C:\temp\MercurialTest\ProjectACopy  
hg clone http://127.0.0.1/cgi-bin/hgwebdir.cgi/CodeRepo/ProjectA/  

To test on another machine:

hg clone http://<insert your webserver address here>/cgi-bin/hgwebdir.cgi/CodeRepo/ProjectA/  

Backing Up A Repo

The simplest method I can see is to clone a repo/push changes to a repo, and save the repo to backup/zip up the directory structure and hive off to disk.  If something goes wrong you clone the backed-up repo and voila :)…

http://mercurial.selenic.com/wiki/Repository

Next…

A few things to look into:

  1. At the moment all the world can see and interact with your repo, which we probably don’t want… so we need to look at locking down the repos to known users, and using SSH for authentication and pushing/pulling to your repos.
  2. The server will support pull by default, but not push, we need to turn that on.
  3. Hg can push/pull to CVS and SVN.  Wouldn’t it be nice to have this working.
  4. Setting up an automated backup.