Mercurial_ The Definitive Guide - Bryan O'Sullivan [92]
A patch file can start with arbitrary text; the patch command ignores this text, but MQ uses it as the commit message when creating changesets. To find the beginning of the patch content, patch searches for the first line that starts with the string diff -.
MQ works with unified diffs (patch can accept several other diff formats, but MQ doesn’t). A unified diff contains two kinds of header. The file header describes the file being modified; it contains the name of the file to modify. When patch sees a new file header, it looks for a file with that name to start modifying.
After the file header comes a series of hunks. Each hunk starts with a header; this identifies the range of line numbers within the file that the hunk should modify. Following the header, a hunk starts and ends with a few (usually three) lines of text from the unmodified file; these are called the context for the hunk. If there’s only a small amount of context between successive hunks, diff doesn’t print a new hunk header; it just runs the hunks together, with a few lines of context between modifications.
Each line of context begins with a space character. Within the hunk, a line that begins with - means “remove this line,” while a line that begins with + means “insert this line.” For example, a line that is modified is represented by one deletion and one insertion.
We will return to some of the more subtle aspects of patches later (in More About Patches), but you should have enough information now to use MQ.
Getting Started with Mercurial Queues
Because MQ is implemented as an extension, you must explicitly enable it before you can use it. (You don’t need to download anything; MQ ships with the standard Mercurial distribution.) To enable MQ, edit your ~/.hgrc file, and add the lines below.
[extensions]
hgext.mq =
Once the extension is enabled, it will make a number of new commands available. To verify that the extension is working, you can use hg help to see if the qinit command is now available.
$ hg help qinit
hg qinit [-c]
init a new queue repository
The queue repository is unversioned by default. If -c is
specified, qinit will create a separate nested repository
for patches (qinit -c may also be run later to convert
an unversioned patch repository into a versioned one).
You can use qcommit to commit changes to this queue repository.
options:
-c --create-repo create queue repository
use "hg -v help qinit" to show global options
You can use MQ with any Mercurial repository, and its commands only operate within that repository. To get started, simply prepare the repository using the qinit command.
$ hg init mq-sandbox
$ cd mq-sandbox
$ echo 'line 1' > file1
$ echo 'another line 1' > file2
$ hg add file1 file2
$ hg commit -m'first change'
$ hg qinit
This command creates an empty directory called .hg/patches, where MQ will keep its metadata. As with many Mercurial commands, the qinit command prints nothing if it succeeds.
Creating a New Patch
To begin work on a new patch, use the qnew command. This command takes one argument, the name of the patch to create.
MQ will use this as the name of an actual file in the .hg/patches directory, as you can see below.
$ hg tip
changeset: 0:aaa568b23a06
tag: tip
user: Bryan O'Sullivan date: Tue May 05 06:44:42 2009 +0000 summary: first change $ hg qnew first.patch $ hg tip changeset: 1:ca4853f45c6e tag: qtip tag: first.patch tag: tip tag: qbase user: Bryan O'Sullivan date: Tue May 05 06:44:42 2009 +0000 summary: [mq]: first.patch $ ls .hg/patches first.patch series status Also newly present in the .hg/patches directory are two other files, series and status. The series file lists all of the patches that MQ knows about for this repository, with one patch per line. Mercurial uses the status file for internal