Guide to Mercurial

Written by Suman Chakravartula, CS107 TA

You will use Mercurial to manage the projects for your CS107 labs and assignments. Mercurial is a revision control system, which is a tool used by developers to manage the source code in a project. A revision control system does two critical things:

  1. It tracks all changes made to each file (which you can review to see your progress or undo if some changes turned out to be a bad idea)
  2. It can merge different versions (so team members can work independently and easily join their work together)

CS107 is not a team project course, so for us, it's about the benefits of #1. Revision control allows you to explore changes while maintaining the option to undo them if they don't work. Saved versions (along with regular testing) makes it easy to pinpoint what code changes are responsible for a new bug so you can focus your attention on fixing the right code. And if you accidentally delete a critical file or munge an important code passage, Mercurial can save the day and retrieve the old version. Without Mercurial, you could attempt to manually track your project history by saving copies of files along the way but this is tedious and error-prone. Instead you'll use Mercurial's handy commands to save/view/retrieve versions. There are many popular revision control systems available (cvs, svn, git, etc.); we chose Mercurial because it has simple commands and is easy to configure, in addition to having a great design that scales to large projects. (read Joel on Software on the beauty of Mercurial)

This quick guide walks you through the essential commands needed for CS107. If you want to go further , there is an entertaining and picture-enhanced tutorial at http://hginit.com/01.html and the official long, super-thorough tutorial at http://www.selenic.com/mercurial/wiki/index.cgi/Tutorial.

Setting up your Mercurial environment

Mercurial is installed on the myth machines, so you need to be working on (or ssh'ed into) one of these machines for the Mercurial commands to be recognized. As a test, try the command

hg --version

Mercurial should respond with "Mercurial Distributed SCM" and a copyright notice.

Before using Mercurial, you must create your personal configuration file. Using any text editor, create a text file named .hgrc in your home directory (the leading dot in the filename is important!). Add the lines below, substituting your own real name, your sunet, and your preferred text editor (emacs, vim, nano, etc.)

[ui]
editor = emacs
username = Your Real Name <yoursunet@stanford.edu>

After you saved the file, verify the result by asking Mercurial to display your configuration with the command:

hg showconfig ui

The expected response should be:

ui.editor=emacs
ui.username=My Real Name <mysunet@stanford.edu>

Now, you're good to go!

Using Mercurial to manage a CS107 project

The project and its change history is called a repository. We will set up the initial repositories for your CS107 projects.

Remmber that submitting a finished project for grading is a separate step. Committing records changes in your local repository, but when finished, you must submit to your class repo for grading!

Making your own Mercurial repos

The CS107 projects are distributed as pre-made Mercurial repositories, but should you ever need to create a repo on your own, it's simple! Simple cd in the directory where you want to create the repo and use hg init and hg add:

hg init
hg add file1.c file2.c Makefile

Frequently asked questions about Mercurial

When I try to commit, Mercurial stalls because it is "waiting for lock". What can I do?

A repository can end up stuck in a locked state if you previously bailed out midway through a commit. Any attempt to act on a locked repo will fail with an error message like this:

waiting for lock on working directory of assign1 held by 'myth8:17680'

Make a copy of your project directory (as a precaution) and then use the command /afs/ir/class/cs107/tools/unlockrepo. This script steps you through forcibly breaking the repo lock which should enable you to commit again.

When I use Mercurial to commit, it says "transaction abort, rollback completed." What happened?

This means the commit was canceled. During a commit, Mercurial brings up an editor for you to enter a commit message. If you exit the editor without entering a message, Mercurial takes the empty message as a sign that you changed your mind and cancels the commit. Also if you prefix your message with HG: (like the lines that are present when the editor is opened), Mercurial will cancel your commit because it ignores the lines beginning with HG: and treats them as empty.