Chapter 2
A tour of Mercurial: the basics

2.1 Installing Mercurial on your system

Prebuilt binary packages of Mercurial are available for every popular operating system. These make it easy to start using Mercurial on your computer immediately.

2.1.1 Linux

Because each Linux distribution has its own packaging tools, policies, and rate of development, it’s difficult to give a comprehensive set of instructions on how to install Mercurial binaries. The version of Mercurial that you will end up with can vary depending on how active the person is who maintains the package for your distribution.

To keep things simple, I will focus on installing Mercurial from the command line under the most popular Linux distributions. Most of these distributions provide graphical package managers that will let you install Mercurial with a single click; the package name to look for is mercurial.

2.1.2 Solaris

XXX.

2.1.3 Mac OS X

Lee Cantey publishes an installer of Mercurial for Mac OS X at http://mercurial.berkwood.com. This package works on both Intel- and Power-based Macs. Before you can use it, you must install a compatible version of Universal MacPython [BI]. This is easy to do; simply follow the instructions on Lee’s site.

2.1.4 Windows

Lee Cantey also publishes an installer of Mercurial for Windows at http://mercurial.berkwood.com. This package has no external dependencies; it “just works”.

Note: The Windows version of Mercurial does not automatically convert line endings between Windows and Unix styles. If you want to share work with Unix users, you must do a little additional configuration work. XXX Flesh this out.

2.2 Getting started

To begin, we’ll use the hg version” command to find out whether Mercurial is actually installed properly. The actual version information that it prints isn’t so important; it’s whether it prints anything at all that we care about.

1  $ hg version
2  Mercurial Distributed SCM (version fe2e81229819)
3  
4  Copyright (C) 2005-2007 Matt Mackall <mpm@selenic.com> and others
5  This is free software; see the source for copying conditions. There is NO
6  warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

2.2.1 Built-in help

Mercurial provides a built-in help system. This is invaluable for those times when you find yourself stuck trying to remember how to run a command. If you are completely stuck, simply run hg help”; it will print a brief list of commands, along with a description of what each does. If you ask for help on a specific command (as below), it prints more detailed information.

1  $ hg help init
2  hg init [-e CMD] [--remotecmd CMD] [DEST]
3  
4  create a new repository in the given directory
5  
6      Initialize a new repository in the given directory.  If the given
7      directory does not exist, it is created.
8  
9      If no directory is given, the current directory is used.
10  
11      It is possible to specify an ssh:// URL as the destination.
12      Look at the help text for the pull command for important details
13      about ssh:// URLs.
14  
15  options:
16  
17   -e --ssh        specify ssh command to use
18      --remotecmd  specify hg command to run on the remote side
19  
20  use "hg -v help init" to show global options

For a more impressive level of detail (which you won’t usually need) run hg help -v”. The -v option is short for --verbose, and tells Mercurial to print more information than it usually would.

2.3 Working with a repository

In Mercurial, everything happens inside a repository. The repository for a project contains all of the files that “belong to” that project, along with a historical record of the project’s files.

There’s nothing particularly magical about a repository; it is simply a directory tree in your filesystem that Mercurial treats as special. You can rename or delete a repository any time you like, using either the command line or your file browser.

2.3.1 Making a local copy of a repository

Copying a repository is just a little bit special. While you could use a normal file copying command to make a copy of a repository, it’s best to use a built-in command that Mercurial provides. This command is called hg clone”, because it creates an identical copy of an existing repository.

1  $ hg clone http://hg.serpentine.com/tutorial/hello
2  destination directory: hello
3  requesting all changes
4  adding changesets
5  adding manifests
6  adding file changes
7  added 5 changesets with 5 changes to 2 files
8  2 files updated, 0 files merged, 0 files removed, 0 files unresolved

If our clone succeeded, we should now have a local directory called hello. This directory will contain some files.

1  $ ls -l
2  total 4
3  drwxrwxr-x 3 bos 1000 4096 Dec 10 19:55 hello
4  $ ls hello
5  Makefile  hello.c

These files have the same contents and history in our repository as they do in the repository we cloned.

Every Mercurial repository is complete, self-contained, and independent. It contains its own private copy of a project’s files and history. A cloned repository remembers the location of the repository it was cloned from, but it does not communicate with that repository, or any other, unless you tell it to.

What this means for now is that we’re free to experiment with our repository, safe in the knowledge that it’s a private “sandbox” that won’t affect anyone else.

2.3.2 What’s in a repository?

When we take a more detailed look inside a repository, we can see that it contains a directory named .hg. This is where Mercurial keeps all of its metadata for the repository.

1  $ cd hello
2  $ ls -a
3  .  ..  .hg  Makefile  hello.c

The contents of the .hg directory and its subdirectories are private to Mercurial. Every other file and directory in the repository is yours to do with as you please.

To introduce a little terminology, the .hg directory is the “real” repository, and all of the files and directories that coexist with it are said to live in the working directory. An easy way to remember the distinction is that the repository contains the history of your project, while the working directory contains a snapshot of your project at a particular point in history.

2.4 A tour through history

One of the first things we might want to do with a new, unfamiliar repository is understand its history. The hg log” command gives us a view of history.

1  $ hg log
2  changeset:   4:b57f9a090b62
3  tag:         tip
4  user:        Bryan O'Sullivan <bos@serpentine.com>
5  date:        Tue Sep 06 15:43:07 2005 -0700
6  summary:     Trim comments.
7  
8  changeset:   3:ff5d7b70a2a9
9  user:        Bryan O'Sullivan <bos@serpentine.com>
10  date:        Tue Sep 06 13:15:58 2005 -0700
11  summary:     Get make to generate the final binary from a .o file.
12  
13  changeset:   2:057d3c2d823c
14  user:        Bryan O'Sullivan <bos@serpentine.com>
15  date:        Tue Sep 06 13:15:43 2005 -0700
16  summary:     Introduce a typo into hello.c.
17  
18  changeset:   1:82e55d328c8c
19  user:        mpm@selenic.com
20  date:        Fri Aug 26 01:21:28 2005 -0700
21  summary:     Create a makefile
22  
23  changeset:   0:0a04b987be5a
24  user:        mpm@selenic.com
25  date:        Fri Aug 26 01:20:50 2005 -0700
26  summary:     Create a standard "hello, world" program
27  

By default, this command prints a brief paragraph of output for each change to the project that was recorded. In Mercurial terminology, we call each of these recorded events a changeset, because it can contain a record of changes to several files.

The fields in a record of output from hg log” are as follows.

The default output printed by hg log” is purely a summary; it is missing a lot of detail.

Figure 2.1 provides a graphical representation of the history of the hello repository, to make it a little easier to see which direction history is “flowing” in. We’ll be returning to this figure several times in this chapter and the chapter that follows.


PIC

Figure 2.1: Graphical history of the hello repository

2.4.1 Changesets, revisions, and talking to other people

As English is a notoriously sloppy language, and computer science has a hallowed history of terminological confusion (why use one term when four will do?), revision control has a variety of words and phrases that mean the same thing. If you are talking about Mercurial history with other people, you will find that the word “changeset” is often compressed to “change” or (when written) “cset”, and sometimes a changeset is referred to as a “revision” or a “rev”.

While it doesn’t matter what word you use to refer to the concept of “a changeset”, the identifier that you use to refer to “a specific changeset” is of great importance. Recall that the changeset field in the output from hg log” identifies a changeset using both a number and a hexadecimal string.

This distinction is important. If you send someone an email talking about “revision 33”, there’s a high likelihood that their revision 33 will not be the same as yours. The reason for this is that a revision number depends on the order in which changes arrived in a repository, and there is no guarantee that the same changes will happen in the same order in different repositories. Three changes a,b,c can easily appear in one repository as 0,1,2, while in another as 1,0,2.

Mercurial uses revision numbers purely as a convenient shorthand. If you need to discuss a changeset with someone, or make a record of a changeset for some other reason (for example, in a bug report), use the hexadecimal identifier.

2.4.2 Viewing specific revisions

To narrow the output of hg log” down to a single revision, use the -r (or --rev) option. You can use either a revision number or a long-form changeset identifier, and you can provide as many revisions as you want.

1  $ hg log -r 3
2  changeset:   3:ff5d7b70a2a9
3  user:        Bryan O'Sullivan <bos@serpentine.com>
4  date:        Tue Sep 06 13:15:58 2005 -0700
5  summary:     Get make to generate the final binary from a .o file.
6  
7  $ hg log -r ff5d7b70a2a9
8  changeset:   3:ff5d7b70a2a9
9  user:        Bryan O'Sullivan <bos@serpentine.com>
10  date:        Tue Sep 06 13:15:58 2005 -0700
11  summary:     Get make to generate the final binary from a .o file.
12  
13  $ hg log -r 1 -r 4
14  changeset:   1:82e55d328c8c
15  user:        mpm@selenic.com
16  date:        Fri Aug 26 01:21:28 2005 -0700
17  summary:     Create a makefile
18  
19  changeset:   4:b57f9a090b62
20  tag:         tip
21  user:        Bryan O'Sullivan <bos@serpentine.com>
22  date:        Tue Sep 06 15:43:07 2005 -0700
23  summary:     Trim comments.
24  

If you want to see the history of several revisions without having to list each one, you can use range notation; this lets you express the idea “I want all revisions between a and b, inclusive”.

1  $ hg log -r 2:4
2  changeset:   2:057d3c2d823c
3  user:        Bryan O'Sullivan <bos@serpentine.com>
4  date:        Tue Sep 06 13:15:43 2005 -0700
5  summary:     Introduce a typo into hello.c.
6  
7  changeset:   3:ff5d7b70a2a9
8  user:        Bryan O'Sullivan <bos@serpentine.com>
9  date:        Tue Sep 06 13:15:58 2005 -0700
10  summary:     Get make to generate the final binary from a .o file.
11  
12  changeset:   4:b57f9a090b62
13  tag:         tip
14  user:        Bryan O'Sullivan <bos@serpentine.com>
15  date:        Tue Sep 06 15:43:07 2005 -0700
16  summary:     Trim comments.
17  

Mercurial also honours the order in which you specify revisions, so hg log -r 2:4” prints 2,3,4 while hg log -r 4:2” prints 4,3,2.

2.4.3 More detailed information

While the summary information printed by hg log” is useful if you already know what you’re looking for, you may need to see a complete description of the change, or a list of the files changed, if you’re trying to decide whether a changeset is the one you’re looking for. The hg log” command’s -v (or --verbose) option gives you this extra detail.

1  $ hg log -v -r 3
2  changeset:   3:ff5d7b70a2a9
3  user:        Bryan O'Sullivan <bos@serpentine.com>
4  date:        Tue Sep 06 13:15:58 2005 -0700
5  files:       Makefile
6  description:
7  Get make to generate the final binary from a .o file.
8  
9  

If you want to see both the description and content of a change, add the -p (or --patch) option. This displays the content of a change as a unified diff (if you’ve never seen a unified diff before, see section 12.4 for an overview).

1  $ hg log -v -p -r 2
2  changeset:   2:057d3c2d823c
3  user:        Bryan O'Sullivan <bos@serpentine.com>
4  date:        Tue Sep 06 13:15:43 2005 -0700
5  files:       hello.c
6  description:
7  Introduce a typo into hello.c.
8  
9  
10  diff -r 82e55d328c8c -r 057d3c2d823c hello.c
11  --- a/hello.c Fri Aug 26 01:21:28 2005 -0700
12  +++ b/hello.c Tue Sep 06 13:15:43 2005 -0700
13  @@ -11,6 +11,6 @@
14  
15   int main(int argc, char ⋆⋆argv)
16   {
17  - printf("hello, world!n");
18  + printf("hello, world!");
19    return 0;
20   }
21  

2.5 All about command options

Let’s take a brief break from exploring Mercurial commands to discuss a pattern in the way that they work; you may find this useful to keep in mind as we continue our tour.

Mercurial has a consistent and straightforward approach to dealing with the options that you can pass to commands. It follows the conventions for options that are common to modern Linux and Unix systems.

In the examples throughout this book, I use short options instead of long. This just reflects my own preference, so don’t read anything significant into it.

Most commands that print output of some kind will print more output when passed a -v (or --verbose) option, and less when passed -q (or --quiet).

2.6 Making and reviewing changes

Now that we have a grasp of viewing history in Mercurial, let’s take a look at making some changes and examining them.

The first thing we’ll do is isolate our experiment in a repository of its own. We use the hg clone” command, but we don’t need to clone a copy of the remote repository. Since we already have a copy of it locally, we can just clone that instead. This is much faster than cloning over the network, and cloning a local repository uses less disk space in most cases, too.

1  $ cd ..
2  $ hg clone hello my-hello
3  2 files updated, 0 files merged, 0 files removed, 0 files unresolved
4  $ cd my-hello

As an aside, it’s often good practice to keep a “pristine” copy of a remote repository around, which you can then make temporary clones of to create sandboxes for each task you want to work on. This lets you work on multiple tasks in parallel, each isolated from the others until it’s complete and you’re ready to integrate it back. Because local clones are so cheap, there’s almost no overhead to cloning and destroying repositories whenever you want.

In our my-hello repository, we have a file hello.c that contains the classic “hello, world” program. Let’s use the ancient and venerable sed command to edit this file so that it prints a second line of output. (I’m only using sed to do this because it’s easy to write a scripted example this way. Since you’re not under the same constraint, you probably won’t want to use sed; simply use your preferred text editor to do the same thing.)

1  $ sed -i '/printf/a∖∖tprintf("hello again!∖∖n");' hello.c

Mercurial’s hg status” command will tell us what Mercurial knows about the files in the repository.

1  $ ls
2  Makefile  hello.c
3  $ hg status
4  M hello.c

The hg status” command prints no output for some files, but a line starting with “M” for hello.c. Unless you tell it to, hg status” will not print any output for files that have not been modified.

The “M” indicates that Mercurial has noticed that we modified hello.c. We didn’t need to inform Mercurial that we were going to modify the file before we started, or that we had modified the file after we were done; it was able to figure this out itself.

It’s a little bit helpful to know that we’ve modified hello.c, but we might prefer to know exactly what changes we’ve made to it. To do this, we use the hg diff” command.

1  $ hg diff
2  diff -r b57f9a090b62 hello.c
3  --- a/hello.c Tue Sep 06 15:43:07 2005 -0700
4  +++ b/hello.c Mon Dec 10 19:55:40 2007 +0000
5  @@ -8,5 +8,6 @@ int main(int argc, char ⋆⋆argv)
6   int main(int argc, char ⋆⋆argv)
7   {
8    printf("hello, world!");
9  + printf("hello again!n");
10    return 0;
11   }

2.7 Recording changes in a new changeset

We can modify files, build and test our changes, and use hg status” and hg diff” to review our changes, until we’re satisfied with what we’ve done and arrive at a natural stopping point where we want to record our work in a new changeset.

The hg commit” command lets us create a new changeset; we’ll usually refer to this as “making a commit” or “committing”.

2.7.1 Setting up a username

When you try to run hg commit” for the first time, it is not guaranteed to succeed. Mercurial records your name and address with each change that you commit, so that you and others will later be able to tell who made each change. Mercurial tries to automatically figure out a sensible username to commit the change with. It will attempt each of the following methods, in order:

  1. If you specify a -u option to the hg commit” command on the command line, followed by a username, this is always given the highest precedence.
  2. If you have set the HGUSER environment variable, this is checked next.
  3. If you create a file in your home directory called .hgrc, with a username entry, that will be used next. To see what the contents of this file should look like, refer to section 2.7.1 below.
  4. If you have set the EMAIL environment variable, this will be used next.
  5. Mercurial will query your system to find out your local user name and host name, and construct a username from these components. Since this often results in a username that is not very useful, it will print a warning if it has to do this.

If all of these mechanisms fail, Mercurial will fail, printing an error message. In this case, it will not let you commit until you set up a username.

You should think of the HGUSER environment variable and the -u option to the hg commit” command as ways to override Mercurial’s default selection of username. For normal use, the simplest and most robust way to set a username for yourself is by creating a .hgrc file; see below for details.

Creating a Mercurial configuration file

To set a user name, use your favourite editor to create a file called .hgrc in your home directory. Mercurial will use this file to look up your personalised configuration settings. The initial contents of your .hgrc should look like this.

1  # This is a Mercurial configuration file.
2  [ui]
3  username = Firstname Lastname <email.address@domain.net>

The “[ui]” line begins a section of the config file, so you can read the “username = ...” line as meaning “set the value of the username item in the ui section”. A section continues until a new section begins, or the end of the file. Mercurial ignores empty lines and treats any text from “#” to the end of a line as a comment.

Choosing a user name

You can use any text you like as the value of the username config item, since this information is for reading by other people, but for interpreting by Mercurial. The convention that most people follow is to use their name and email address, as in the example above.

Note: Mercurial’s built-in web server obfuscates email addresses, to make it more difficult for the email harvesting tools that spammers use. This reduces the likelihood that you’ll start receiving more junk email if you publish a Mercurial repository on the web.

2.7.2 Writing a commit message

When we commit a change, Mercurial drops us into a text editor, to enter a message that will describe the modifications we’ve made in this changeset. This is called the commit message. It will be a record for readers of what we did and why, and it will be printed by hg log” after we’ve finished committing.

1  $ hg commit

The editor that the hg commit” command drops us into will contain an empty line, followed by a number of lines starting with “HG:”.

1  empty line
2  HG: changed hello.c

Mercurial ignores the lines that start with “HG:”; it uses them only to tell us which files it’s recording changes to. Modifying or deleting these lines has no effect.

2.7.3 Writing a good commit message

Since hg log” only prints the first line of a commit message by default, it’s best to write a commit message whose first line stands alone. Here’s a real example of a commit message that doesn’t follow this guideline, and hence has a summary that is not readable.

1  changeset:   73:584af0e231be
2  user:        Censored Person <censored.person@example.org>
3  date:        Tue Sep 26 21:37:07 2006 -0700
4  summary:     include buildmeister/commondefs.   Add an exports and install

As far as the remainder of the contents of the commit message are concerned, there are no hard-and-fast rules. Mercurial itself doesn’t interpret or care about the contents of the commit message, though your project may have policies that dictate a certain kind of formatting.

My personal preference is for short, but informative, commit messages that tell me something that I can’t figure out with a quick glance at the output of hg log --patch”.

2.7.4 Aborting a commit

If you decide that you don’t want to commit while in the middle of editing a commit message, simply exit from your editor without saving the file that it’s editing. This will cause nothing to happen to either the repository or the working directory.

If we run the hg commit” command without any arguments, it records all of the changes we’ve made, as reported by hg status” and hg diff”.

2.7.5 Admiring our new handiwork

Once we’ve finished the commit, we can use the hg tip” command to display the changeset we just created. This command produces output that is identical to hg log”, but it only displays the newest revision in the repository.

1  $ hg tip -vp
2  changeset:   5:fc920a075b40
3  tag:         tip
4  user:        Bryan O'Sullivan <bos@serpentine.com>
5  date:        Mon Dec 10 19:55:40 2007 +0000
6  files:       hello.c
7  description:
8  Added an extra line of output
9  
10  
11  diff -r b57f9a090b62 -r fc920a075b40 hello.c
12  --- a/hello.c Tue Sep 06 15:43:07 2005 -0700
13  +++ b/hello.c Mon Dec 10 19:55:40 2007 +0000
14  @@ -8,5 +8,6 @@ int main(int argc, char ⋆⋆argv)
15   int main(int argc, char ⋆⋆argv)
16   {
17    printf("hello, world!");
18  + printf("hello again!n");
19    return 0;
20   }
21  

We refer to the newest revision in the repository as the tip revision, or simply the tip.

2.8 Sharing changes

We mentioned earlier that repositories in Mercurial are self-contained. This means that the changeset we just created exists only in our my-hello repository. Let’s look at a few ways that we can propagate this change into other repositories.

2.8.1 Pulling changes from another repository

To get started, let’s clone our original hello repository, which does not contain the change we just committed. We’ll call our temporary repository hello-pull.

1  $ cd ..
2  $ hg clone hello hello-pull
3  2 files updated, 0 files merged, 0 files removed, 0 files unresolved

We’ll use the hg pull” command to bring changes from my-hello into hello-pull. However, blindly pulling unknown changes into a repository is a somewhat scary prospect. Mercurial provides the hg incoming” command to tell us what changes the hg pull” command would pull into the repository, without actually pulling the changes in.

1  $ cd hello-pull
2  $ hg incoming ../my-hello
3  comparing with ../my-hello
4  searching for changes
5  changeset:   5:fc920a075b40
6  tag:         tip
7  user:        Bryan O'Sullivan <bos@serpentine.com>
8  date:        Mon Dec 10 19:55:40 2007 +0000
9  summary:     Added an extra line of output
10  

(Of course, someone could cause more changesets to appear in the repository that we ran hg incoming” in, before we get a chance to hg pull” the changes, so that we could end up pulling changes that we didn’t expect.)

Bringing changes into a repository is a simple matter of running the hg pull” command, and telling it which repository to pull from.

1  $ hg tip
2  changeset:   4:b57f9a090b62
3  tag:         tip
4  user:        Bryan O'Sullivan <bos@serpentine.com>
5  date:        Tue Sep 06 15:43:07 2005 -0700
6  summary:     Trim comments.
7  
8  $ hg pull ../my-hello
9  pulling from ../my-hello
10  searching for changes
11  adding changesets
12  adding manifests
13  adding file changes
14  added 1 changesets with 1 changes to 1 files
15  (run 'hg update' to get a working copy)
16  $