hgbook

view en/ch12-mq.xml @ 623:082bb76417f1

Add Po4a 0.37-dev(2009-03-08)
author Dongsheng Song <dongsheng.song@gmail.com>
date Thu Mar 12 15:43:56 2009 +0800 (2009-03-12)
parents 8fcd44708f41
children 8366882f67f2 cfdb601a3c8b
line source
1 <!-- vim: set filetype=docbkxml shiftwidth=2 autoindent expandtab tw=77 : -->
3 <chapter id="chap:mq">
4 <?dbhtml filename="managing-change-with-mercurial-queues.html"?>
5 <title>Managing change with Mercurial Queues</title>
7 <sect1 id="sec:mq:patch-mgmt">
8 <title>The patch management problem</title>
10 <para>Here is a common scenario: you need to install a software
11 package from source, but you find a bug that you must fix in the
12 source before you can start using the package. You make your
13 changes, forget about the package for a while, and a few months
14 later you need to upgrade to a newer version of the package. If
15 the newer version of the package still has the bug, you must
16 extract your fix from the older source tree and apply it against
17 the newer version. This is a tedious task, and it's easy to
18 make mistakes.</para>
20 <para>This is a simple case of the <quote>patch management</quote>
21 problem. You have an <quote>upstream</quote> source tree that
22 you can't change; you need to make some local changes on top of
23 the upstream tree; and you'd like to be able to keep those
24 changes separate, so that you can apply them to newer versions
25 of the upstream source.</para>
27 <para>The patch management problem arises in many situations.
28 Probably the most visible is that a user of an open source
29 software project will contribute a bug fix or new feature to the
30 project's maintainers in the form of a patch.</para>
32 <para>Distributors of operating systems that include open source
33 software often need to make changes to the packages they
34 distribute so that they will build properly in their
35 environments.</para>
37 <para>When you have few changes to maintain, it is easy to manage
38 a single patch using the standard <command>diff</command> and
39 <command>patch</command> programs (see section <xref
40 linkend="sec:mq:patch"/> for a discussion of these
41 tools). Once the number of changes grows, it starts to make
42 sense to maintain patches as discrete <quote>chunks of
43 work,</quote> so that for example a single patch will contain
44 only one bug fix (the patch might modify several files, but it's
45 doing <quote>only one thing</quote>), and you may have a number
46 of such patches for different bugs you need fixed and local
47 changes you require. In this situation, if you submit a bug fix
48 patch to the upstream maintainers of a package and they include
49 your fix in a subsequent release, you can simply drop that
50 single patch when you're updating to the newer release.</para>
52 <para>Maintaining a single patch against an upstream tree is a
53 little tedious and error-prone, but not difficult. However, the
54 complexity of the problem grows rapidly as the number of patches
55 you have to maintain increases. With more than a tiny number of
56 patches in hand, understanding which ones you have applied and
57 maintaining them moves from messy to overwhelming.</para>
59 <para>Fortunately, Mercurial includes a powerful extension,
60 Mercurial Queues (or simply <quote>MQ</quote>), that massively
61 simplifies the patch management problem.</para>
63 </sect1>
64 <sect1 id="sec:mq:history">
65 <title>The prehistory of Mercurial Queues</title>
67 <para>During the late 1990s, several Linux kernel developers
68 started to maintain <quote>patch series</quote> that modified
69 the behaviour of the Linux kernel. Some of these series were
70 focused on stability, some on feature coverage, and others were
71 more speculative.</para>
73 <para>The sizes of these patch series grew rapidly. In 2002,
74 Andrew Morton published some shell scripts he had been using to
75 automate the task of managing his patch queues. Andrew was
76 successfully using these scripts to manage hundreds (sometimes
77 thousands) of patches on top of the Linux kernel.</para>
79 <sect2 id="sec:mq:quilt">
80 <title>A patchwork quilt</title>
82 <para>In early 2003, Andreas Gruenbacher and Martin Quinson
83 borrowed the approach of Andrew's scripts and published a tool
84 called <quote>patchwork quilt</quote>
85 <citation>web:quilt</citation>, or simply <quote>quilt</quote>
86 (see <citation>gruenbacher:2005</citation> for a paper
87 describing it). Because quilt substantially automated patch
88 management, it rapidly gained a large following among open
89 source software developers.</para>
91 <para>Quilt manages a <emphasis>stack of patches</emphasis> on
92 top of a directory tree. To begin, you tell quilt to manage a
93 directory tree, and tell it which files you want to manage; it
94 stores away the names and contents of those files. To fix a
95 bug, you create a new patch (using a single command), edit the
96 files you need to fix, then <quote>refresh</quote> the
97 patch.</para>
99 <para>The refresh step causes quilt to scan the directory tree;
100 it updates the patch with all of the changes you have made.
101 You can create another patch on top of the first, which will
102 track the changes required to modify the tree from <quote>tree
103 with one patch applied</quote> to <quote>tree with two
104 patches applied</quote>.</para>
106 <para>You can <emphasis>change</emphasis> which patches are
107 applied to the tree. If you <quote>pop</quote> a patch, the
108 changes made by that patch will vanish from the directory
109 tree. Quilt remembers which patches you have popped, though,
110 so you can <quote>push</quote> a popped patch again, and the
111 directory tree will be restored to contain the modifications
112 in the patch. Most importantly, you can run the
113 <quote>refresh</quote> command at any time, and the topmost
114 applied patch will be updated. This means that you can, at
115 any time, change both which patches are applied and what
116 modifications those patches make.</para>
118 <para>Quilt knows nothing about revision control tools, so it
119 works equally well on top of an unpacked tarball or a
120 Subversion working copy.</para>
122 </sect2>
123 <sect2 id="sec:mq:quilt-mq">
124 <title>From patchwork quilt to Mercurial Queues</title>
126 <para>In mid-2005, Chris Mason took the features of quilt and
127 wrote an extension that he called Mercurial Queues, which
128 added quilt-like behaviour to Mercurial.</para>
130 <para>The key difference between quilt and MQ is that quilt
131 knows nothing about revision control systems, while MQ is
132 <emphasis>integrated</emphasis> into Mercurial. Each patch
133 that you push is represented as a Mercurial changeset. Pop a
134 patch, and the changeset goes away.</para>
136 <para>Because quilt does not care about revision control tools,
137 it is still a tremendously useful piece of software to know
138 about for situations where you cannot use Mercurial and
139 MQ.</para>
141 </sect2>
142 </sect1>
143 <sect1>
144 <title>The huge advantage of MQ</title>
146 <para>I cannot overstate the value that MQ offers through the
147 unification of patches and revision control.</para>
149 <para>A major reason that patches have persisted in the free
150 software and open source world&emdash;in spite of the
151 availability of increasingly capable revision control tools over
152 the years&emdash;is the <emphasis>agility</emphasis> they
153 offer.</para>
155 <para>Traditional revision control tools make a permanent,
156 irreversible record of everything that you do. While this has
157 great value, it's also somewhat stifling. If you want to
158 perform a wild-eyed experiment, you have to be careful in how
159 you go about it, or you risk leaving unneeded&emdash;or worse,
160 misleading or destabilising&emdash;traces of your missteps and
161 errors in the permanent revision record.</para>
163 <para>By contrast, MQ's marriage of distributed revision control
164 with patches makes it much easier to isolate your work. Your
165 patches live on top of normal revision history, and you can make
166 them disappear or reappear at will. If you don't like a patch,
167 you can drop it. If a patch isn't quite as you want it to be,
168 simply fix it&emdash;as many times as you need to, until you
169 have refined it into the form you desire.</para>
171 <para>As an example, the integration of patches with revision
172 control makes understanding patches and debugging their
173 effects&emdash;and their interplay with the code they're based
174 on&emdash;<emphasis>enormously</emphasis> easier. Since every
175 applied patch has an associated changeset, you can give <command
176 role="hg-cmd">hg log</command> a file name to see which
177 changesets and patches affected the file. You can use the
178 <command role="hg-cmd">hg bisect</command> command to
179 binary-search through all changesets and applied patches to see
180 where a bug got introduced or fixed. You can use the <command
181 role="hg-cmd">hg annotate</command> command to see which
182 changeset or patch modified a particular line of a source file.
183 And so on.</para>
185 </sect1>
186 <sect1 id="sec:mq:patch">
187 <title>Understanding patches</title>
189 <para>Because MQ doesn't hide its patch-oriented nature, it is
190 helpful to understand what patches are, and a little about the
191 tools that work with them.</para>
193 <para>The traditional Unix <command>diff</command> command
194 compares two files, and prints a list of differences between
195 them. The <command>patch</command> command understands these
196 differences as <emphasis>modifications</emphasis> to make to a
197 file. Take a look below for a simple example of these commands
198 in action.</para>
200 &interaction.mq.dodiff.diff;
202 <para>The type of file that <command>diff</command> generates (and
203 <command>patch</command> takes as input) is called a
204 <quote>patch</quote> or a <quote>diff</quote>; there is no
205 difference between a patch and a diff. (We'll use the term
206 <quote>patch</quote>, since it's more commonly used.)</para>
208 <para>A patch file can start with arbitrary text; the
209 <command>patch</command> command ignores this text, but MQ uses
210 it as the commit message when creating changesets. To find the
211 beginning of the patch content, <command>patch</command>
212 searches for the first line that starts with the string
213 <quote><literal>diff -</literal></quote>.</para>
215 <para>MQ works with <emphasis>unified</emphasis> diffs
216 (<command>patch</command> can accept several other diff formats,
217 but MQ doesn't). A unified diff contains two kinds of header.
218 The <emphasis>file header</emphasis> describes the file being
219 modified; it contains the name of the file to modify. When
220 <command>patch</command> sees a new file header, it looks for a
221 file with that name to start modifying.</para>
223 <para>After the file header comes a series of
224 <emphasis>hunks</emphasis>. Each hunk starts with a header;
225 this identifies the range of line numbers within the file that
226 the hunk should modify. Following the header, a hunk starts and
227 ends with a few (usually three) lines of text from the
228 unmodified file; these are called the
229 <emphasis>context</emphasis> for the hunk. If there's only a
230 small amount of context between successive hunks,
231 <command>diff</command> doesn't print a new hunk header; it just
232 runs the hunks together, with a few lines of context between
233 modifications.</para>
235 <para>Each line of context begins with a space character. Within
236 the hunk, a line that begins with
237 <quote><literal>-</literal></quote> means <quote>remove this
238 line,</quote> while a line that begins with
239 <quote><literal>+</literal></quote> means <quote>insert this
240 line.</quote> For example, a line that is modified is
241 represented by one deletion and one insertion.</para>
243 <para>We will return to some of the more subtle aspects of patches
244 later (in section <xref linkend="sec:mq:adv-patch"/>), but you
245 should have
246 enough information now to use MQ.</para>
248 </sect1>
249 <sect1 id="sec:mq:start">
250 <title>Getting started with Mercurial Queues</title>
252 <para>Because MQ is implemented as an extension, you must
253 explicitly enable before you can use it. (You don't need to
254 download anything; MQ ships with the standard Mercurial
255 distribution.) To enable MQ, edit your <filename
256 role="home">~/.hgrc</filename> file, and add the lines
257 below.</para>
259 <programlisting>[extensions] hgext.mq =</programlisting>
261 <para>Once the extension is enabled, it will make a number of new
262 commands available. To verify that the extension is working,
263 you can use <command role="hg-cmd">hg help</command> to see if
264 the <command role="hg-ext-mq">qinit</command> command is now
265 available.</para>
267 &interaction.mq.qinit-help.help;
269 <para>You can use MQ with <emphasis>any</emphasis> Mercurial
270 repository, and its commands only operate within that
271 repository. To get started, simply prepare the repository using
272 the <command role="hg-ext-mq">qinit</command> command.</para>
274 &interaction.mq.tutorial.qinit;
276 <para>This command creates an empty directory called <filename
277 role="special" class="directory">.hg/patches</filename>, where
278 MQ will keep its metadata. As with many Mercurial commands, the
279 <command role="hg-ext-mq">qinit</command> command prints nothing
280 if it succeeds.</para>
282 <sect2>
283 <title>Creating a new patch</title>
285 <para>To begin work on a new patch, use the <command
286 role="hg-ext-mq">qnew</command> command. This command takes
287 one argument, the name of the patch to create.</para>
289 <para>MQ will use this as the name of an actual file in the
290 <filename role="special"
291 class="directory">.hg/patches</filename> directory, as you
292 can see below.</para>
294 &interaction.mq.tutorial.qnew;
296 <para>Also newly present in the <filename role="special"
297 class="directory">.hg/patches</filename> directory are two
298 other files, <filename role="special">series</filename> and
299 <filename role="special">status</filename>. The <filename
300 role="special">series</filename> file lists all of the
301 patches that MQ knows about for this repository, with one
302 patch per line. Mercurial uses the <filename
303 role="special">status</filename> file for internal
304 book-keeping; it tracks all of the patches that MQ has
305 <emphasis>applied</emphasis> in this repository.</para>
307 <note>
308 <para> You may sometimes want to edit the <filename
309 role="special">series</filename> file by hand; for
310 example, to change the sequence in which some patches are
311 applied. However, manually editing the <filename
312 role="special">status</filename> file is almost always a
313 bad idea, as it's easy to corrupt MQ's idea of what is
314 happening.</para>
315 </note>
317 <para>Once you have created your new patch, you can edit files
318 in the working directory as you usually would. All of the
319 normal Mercurial commands, such as <command role="hg-cmd">hg
320 diff</command> and <command role="hg-cmd">hg
321 annotate</command>, work exactly as they did before.</para>
323 </sect2>
324 <sect2>
325 <title>Refreshing a patch</title>
327 <para>When you reach a point where you want to save your work,
328 use the <command role="hg-ext-mq">qrefresh</command> command
329 to update the patch you are working on.</para>
331 &interaction.mq.tutorial.qrefresh;
333 <para>This command folds the changes you have made in the
334 working directory into your patch, and updates its
335 corresponding changeset to contain those changes.</para>
337 <para>You can run <command role="hg-ext-mq">qrefresh</command>
338 as often as you like, so it's a good way to
339 <quote>checkpoint</quote> your work. Refresh your patch at an
340 opportune time; try an experiment; and if the experiment
341 doesn't work out, <command role="hg-cmd">hg revert</command>
342 your modifications back to the last time you refreshed.</para>
344 &interaction.mq.tutorial.qrefresh2;
346 </sect2>
347 <sect2>
348 <title>Stacking and tracking patches</title>
350 <para>Once you have finished working on a patch, or need to work
351 on another, you can use the <command
352 role="hg-ext-mq">qnew</command> command again to create a
353 new patch. Mercurial will apply this patch on top of your
354 existing patch.</para>
356 &interaction.mq.tutorial.qnew2;
357 <para>Notice that the patch contains the changes in our prior
358 patch as part of its context (you can see this more clearly in
359 the output of <command role="hg-cmd">hg
360 annotate</command>).</para>
362 <para>So far, with the exception of <command
363 role="hg-ext-mq">qnew</command> and <command
364 role="hg-ext-mq">qrefresh</command>, we've been careful to
365 only use regular Mercurial commands. However, MQ provides
366 many commands that are easier to use when you are thinking
367 about patches, as illustrated below.</para>
369 &interaction.mq.tutorial.qseries;
371 <itemizedlist>
372 <listitem><para>The <command
373 role="hg-ext-mq">qseries</command> command lists every
374 patch that MQ knows about in this repository, from oldest
375 to newest (most recently
376 <emphasis>created</emphasis>).</para>
377 </listitem>
378 <listitem><para>The <command
379 role="hg-ext-mq">qapplied</command> command lists every
380 patch that MQ has <emphasis>applied</emphasis> in this
381 repository, again from oldest to newest (most recently
382 applied).</para>
383 </listitem></itemizedlist>
385 </sect2>
386 <sect2>
387 <title>Manipulating the patch stack</title>
389 <para>The previous discussion implied that there must be a
390 difference between <quote>known</quote> and
391 <quote>applied</quote> patches, and there is. MQ can manage a
392 patch without it being applied in the repository.</para>
394 <para>An <emphasis>applied</emphasis> patch has a corresponding
395 changeset in the repository, and the effects of the patch and
396 changeset are visible in the working directory. You can undo
397 the application of a patch using the <command
398 role="hg-ext-mq">qpop</command> command. MQ still
399 <emphasis>knows about</emphasis>, or manages, a popped patch,
400 but the patch no longer has a corresponding changeset in the
401 repository, and the working directory does not contain the
402 changes made by the patch. Figure <xref
403 linkend="fig:mq:stack"/> illustrates
404 the difference between applied and tracked patches.</para>
406 <informalfigure id="fig:mq:stack">
407 <mediaobject><imageobject><imagedata
408 fileref="mq-stack"/></imageobject><textobject><phrase>XXX
409 add text</phrase></textobject><caption><para>Applied and
410 unapplied patches in the MQ patch
411 stack</para></caption></mediaobject>
412 </informalfigure>
414 <para>You can reapply an unapplied, or popped, patch using the
415 <command role="hg-ext-mq">qpush</command> command. This
416 creates a new changeset to correspond to the patch, and the
417 patch's changes once again become present in the working
418 directory. See below for examples of <command
419 role="hg-ext-mq">qpop</command> and <command
420 role="hg-ext-mq">qpush</command> in action.</para>
421 &interaction.mq.tutorial.qpop;
423 <para>Notice that once we have popped a patch or two patches,
424 the output of <command role="hg-ext-mq">qseries</command>
425 remains the same, while that of <command
426 role="hg-ext-mq">qapplied</command> has changed.</para>
429 </sect2>
430 <sect2>
431 <title>Pushing and popping many patches</title>
433 <para>While <command role="hg-ext-mq">qpush</command> and
434 <command role="hg-ext-mq">qpop</command> each operate on a
435 single patch at a time by default, you can push and pop many
436 patches in one go. The <option
437 role="hg-ext-mq-cmd-qpush-opt">hg -a</option> option to
438 <command role="hg-ext-mq">qpush</command> causes it to push
439 all unapplied patches, while the <option
440 role="hg-ext-mq-cmd-qpop-opt">-a</option> option to <command
441 role="hg-ext-mq">qpop</command> causes it to pop all applied
442 patches. (For some more ways to push and pop many patches,
443 see section <xref linkend="sec:mq:perf"/>
444 below.)</para>
446 &interaction.mq.tutorial.qpush-a;
448 </sect2>
449 <sect2>
450 <title>Safety checks, and overriding them</title>
452 <para>Several MQ commands check the working directory before
453 they do anything, and fail if they find any modifications.
454 They do this to ensure that you won't lose any changes that
455 you have made, but not yet incorporated into a patch. The
456 example below illustrates this; the <command
457 role="hg-ext-mq">qnew</command> command will not create a
458 new patch if there are outstanding changes, caused in this
459 case by the <command role="hg-cmd">hg add</command> of
460 <filename>file3</filename>.</para>
462 &interaction.mq.tutorial.add;
464 <para>Commands that check the working directory all take an
465 <quote>I know what I'm doing</quote> option, which is always
466 named <option>-f</option>. The exact meaning of
467 <option>-f</option> depends on the command. For example,
468 <command role="hg-cmd">hg qnew <option
469 role="hg-ext-mq-cmd-qnew-opt">hg -f</option></command>
470 will incorporate any outstanding changes into the new patch it
471 creates, but <command role="hg-cmd">hg qpop <option
472 role="hg-ext-mq-cmd-qpop-opt">hg -f</option></command>
473 will revert modifications to any files affected by the patch
474 that it is popping. Be sure to read the documentation for a
475 command's <option>-f</option> option before you use it!</para>
477 </sect2>
478 <sect2>
479 <title>Working on several patches at once</title>
481 <para>The <command role="hg-ext-mq">qrefresh</command> command
482 always refreshes the <emphasis>topmost</emphasis> applied
483 patch. This means that you can suspend work on one patch (by
484 refreshing it), pop or push to make a different patch the top,
485 and work on <emphasis>that</emphasis> patch for a
486 while.</para>
488 <para>Here's an example that illustrates how you can use this
489 ability. Let's say you're developing a new feature as two
490 patches. The first is a change to the core of your software,
491 and the second&emdash;layered on top of the
492 first&emdash;changes the user interface to use the code you
493 just added to the core. If you notice a bug in the core while
494 you're working on the UI patch, it's easy to fix the core.
495 Simply <command role="hg-ext-mq">qrefresh</command> the UI
496 patch to save your in-progress changes, and <command
497 role="hg-ext-mq">qpop</command> down to the core patch. Fix
498 the core bug, <command role="hg-ext-mq">qrefresh</command> the
499 core patch, and <command role="hg-ext-mq">qpush</command> back
500 to the UI patch to continue where you left off.</para>
502 </sect2>
503 </sect1>
504 <sect1 id="sec:mq:adv-patch">
505 <title>More about patches</title>
507 <para>MQ uses the GNU <command>patch</command> command to apply
508 patches, so it's helpful to know a few more detailed aspects of
509 how <command>patch</command> works, and about patches
510 themselves.</para>
512 <sect2>
513 <title>The strip count</title>
515 <para>If you look at the file headers in a patch, you will
516 notice that the pathnames usually have an extra component on
517 the front that isn't present in the actual path name. This is
518 a holdover from the way that people used to generate patches
519 (people still do this, but it's somewhat rare with modern
520 revision control tools).</para>
522 <para>Alice would unpack a tarball, edit her files, then decide
523 that she wanted to create a patch. So she'd rename her
524 working directory, unpack the tarball again (hence the need
525 for the rename), and use the <option
526 role="cmd-opt-diff">-r</option> and <option
527 role="cmd-opt-diff">-N</option> options to
528 <command>diff</command> to recursively generate a patch
529 between the unmodified directory and the modified one. The
530 result would be that the name of the unmodified directory
531 would be at the front of the left-hand path in every file
532 header, and the name of the modified directory would be at the
533 front of the right-hand path.</para>
535 <para>Since someone receiving a patch from the Alices of the net
536 would be unlikely to have unmodified and modified directories
537 with exactly the same names, the <command>patch</command>
538 command has a <option role="cmd-opt-patch">-p</option> option
539 that indicates the number of leading path name components to
540 strip when trying to apply a patch. This number is called the
541 <emphasis>strip count</emphasis>.</para>
543 <para>An option of <quote><literal>-p1</literal></quote> means
544 <quote>use a strip count of one</quote>. If
545 <command>patch</command> sees a file name
546 <filename>foo/bar/baz</filename> in a file header, it will
547 strip <filename>foo</filename> and try to patch a file named
548 <filename>bar/baz</filename>. (Strictly speaking, the strip
549 count refers to the number of <emphasis>path
550 separators</emphasis> (and the components that go with them
551 ) to strip. A strip count of one will turn
552 <filename>foo/bar</filename> into <filename>bar</filename>,
553 but <filename>/foo/bar</filename> (notice the extra leading
554 slash) into <filename>foo/bar</filename>.)</para>
556 <para>The <quote>standard</quote> strip count for patches is
557 one; almost all patches contain one leading path name
558 component that needs to be stripped. Mercurial's <command
559 role="hg-cmd">hg diff</command> command generates path names
560 in this form, and the <command role="hg-cmd">hg
561 import</command> command and MQ expect patches to have a
562 strip count of one.</para>
564 <para>If you receive a patch from someone that you want to add
565 to your patch queue, and the patch needs a strip count other
566 than one, you cannot just <command
567 role="hg-ext-mq">qimport</command> the patch, because
568 <command role="hg-ext-mq">qimport</command> does not yet have
569 a <literal>-p</literal> option (see <ulink role="hg-bug"
570 url="http://www.selenic.com/mercurial/bts/issue311">issue
571 311</ulink>). Your best bet is to <command
572 role="hg-ext-mq">qnew</command> a patch of your own, then
573 use <command>patch -pN</command> to apply their patch,
574 followed by <command role="hg-cmd">hg addremove</command> to
575 pick up any files added or removed by the patch, followed by
576 <command role="hg-ext-mq">hg qrefresh</command>. This
577 complexity may become unnecessary; see <ulink role="hg-bug"
578 url="http://www.selenic.com/mercurial/bts/issue311">issue
579 311</ulink> for details.
580 </para>
581 </sect2>
582 <sect2>
583 <title>Strategies for applying a patch</title>
585 <para>When <command>patch</command> applies a hunk, it tries a
586 handful of successively less accurate strategies to try to
587 make the hunk apply. This falling-back technique often makes
588 it possible to take a patch that was generated against an old
589 version of a file, and apply it against a newer version of
590 that file.</para>
592 <para>First, <command>patch</command> tries an exact match,
593 where the line numbers, the context, and the text to be
594 modified must apply exactly. If it cannot make an exact
595 match, it tries to find an exact match for the context,
596 without honouring the line numbering information. If this
597 succeeds, it prints a line of output saying that the hunk was
598 applied, but at some <emphasis>offset</emphasis> from the
599 original line number.</para>
601 <para>If a context-only match fails, <command>patch</command>
602 removes the first and last lines of the context, and tries a
603 <emphasis>reduced</emphasis> context-only match. If the hunk
604 with reduced context succeeds, it prints a message saying that
605 it applied the hunk with a <emphasis>fuzz factor</emphasis>
606 (the number after the fuzz factor indicates how many lines of
607 context <command>patch</command> had to trim before the patch
608 applied).</para>
610 <para>When neither of these techniques works,
611 <command>patch</command> prints a message saying that the hunk
612 in question was rejected. It saves rejected hunks (also
613 simply called <quote>rejects</quote>) to a file with the same
614 name, and an added <filename role="special">.rej</filename>
615 extension. It also saves an unmodified copy of the file with
616 a <filename role="special">.orig</filename> extension; the
617 copy of the file without any extensions will contain any
618 changes made by hunks that <emphasis>did</emphasis> apply
619 cleanly. If you have a patch that modifies
620 <filename>foo</filename> with six hunks, and one of them fails
621 to apply, you will have: an unmodified
622 <filename>foo.orig</filename>, a <filename>foo.rej</filename>
623 containing one hunk, and <filename>foo</filename>, containing
624 the changes made by the five successful hunks.</para>
626 </sect2>
627 <sect2>
628 <title>Some quirks of patch representation</title>
630 <para>There are a few useful things to know about how
631 <command>patch</command> works with files.</para>
632 <itemizedlist>
633 <listitem><para>This should already be obvious, but
634 <command>patch</command> cannot handle binary
635 files.</para>
636 </listitem>
637 <listitem><para>Neither does it care about the executable bit;
638 it creates new files as readable, but not
639 executable.</para>
640 </listitem>
641 <listitem><para><command>patch</command> treats the removal of
642 a file as a diff between the file to be removed and the
643 empty file. So your idea of <quote>I deleted this
644 file</quote> looks like <quote>every line of this file
645 was deleted</quote> in a patch.</para>
646 </listitem>
647 <listitem><para>It treats the addition of a file as a diff
648 between the empty file and the file to be added. So in a
649 patch, your idea of <quote>I added this file</quote> looks
650 like <quote>every line of this file was
651 added</quote>.</para>
652 </listitem>
653 <listitem><para>It treats a renamed file as the removal of the
654 old name, and the addition of the new name. This means
655 that renamed files have a big footprint in patches. (Note
656 also that Mercurial does not currently try to infer when
657 files have been renamed or copied in a patch.)</para>
658 </listitem>
659 <listitem><para><command>patch</command> cannot represent
660 empty files, so you cannot use a patch to represent the
661 notion <quote>I added this empty file to the
662 tree</quote>.</para>
663 </listitem></itemizedlist>
664 </sect2>
665 <sect2>
666 <title>Beware the fuzz</title>
668 <para>While applying a hunk at an offset, or with a fuzz factor,
669 will often be completely successful, these inexact techniques
670 naturally leave open the possibility of corrupting the patched
671 file. The most common cases typically involve applying a
672 patch twice, or at an incorrect location in the file. If
673 <command>patch</command> or <command
674 role="hg-ext-mq">qpush</command> ever mentions an offset or
675 fuzz factor, you should make sure that the modified files are
676 correct afterwards.</para>
678 <para>It's often a good idea to refresh a patch that has applied
679 with an offset or fuzz factor; refreshing the patch generates
680 new context information that will make it apply cleanly. I
681 say <quote>often,</quote> not <quote>always,</quote> because
682 sometimes refreshing a patch will make it fail to apply
683 against a different revision of the underlying files. In some
684 cases, such as when you're maintaining a patch that must sit
685 on top of multiple versions of a source tree, it's acceptable
686 to have a patch apply with some fuzz, provided you've verified
687 the results of the patching process in such cases.</para>
689 </sect2>
690 <sect2>
691 <title>Handling rejection</title>
693 <para>If <command role="hg-ext-mq">qpush</command> fails to
694 apply a patch, it will print an error message and exit. If it
695 has left <filename role="special">.rej</filename> files
696 behind, it is usually best to fix up the rejected hunks before
697 you push more patches or do any further work.</para>
699 <para>If your patch <emphasis>used to</emphasis> apply cleanly,
700 and no longer does because you've changed the underlying code
701 that your patches are based on, Mercurial Queues can help; see
702 section <xref
703 linkend="sec:mq:merge"/> for details.</para>
705 <para>Unfortunately, there aren't any great techniques for
706 dealing with rejected hunks. Most often, you'll need to view
707 the <filename role="special">.rej</filename> file and edit the
708 target file, applying the rejected hunks by hand.</para>
710 <para>If you're feeling adventurous, Neil Brown, a Linux kernel
711 hacker, wrote a tool called <command>wiggle</command>
712 <citation>web:wiggle</citation>, which is more vigorous than
713 <command>patch</command> in its attempts to make a patch
714 apply.</para>
716 <para>Another Linux kernel hacker, Chris Mason (the author of
717 Mercurial Queues), wrote a similar tool called
718 <command>mpatch</command> <citation>web:mpatch</citation>,
719 which takes a simple approach to automating the application of
720 hunks rejected by <command>patch</command>. The
721 <command>mpatch</command> command can help with four common
722 reasons that a hunk may be rejected:</para>
724 <itemizedlist>
725 <listitem><para>The context in the middle of a hunk has
726 changed.</para>
727 </listitem>
728 <listitem><para>A hunk is missing some context at the
729 beginning or end.</para>
730 </listitem>
731 <listitem><para>A large hunk might apply better&emdash;either
732 entirely or in part&emdash;if it was broken up into
733 smaller hunks.</para>
734 </listitem>
735 <listitem><para>A hunk removes lines with slightly different
736 content than those currently present in the file.</para>
737 </listitem></itemizedlist>
739 <para>If you use <command>wiggle</command> or
740 <command>mpatch</command>, you should be doubly careful to
741 check your results when you're done. In fact,
742 <command>mpatch</command> enforces this method of
743 double-checking the tool's output, by automatically dropping
744 you into a merge program when it has done its job, so that you
745 can verify its work and finish off any remaining
746 merges.</para>
748 </sect2>
749 </sect1>
750 <sect1 id="sec:mq:perf">
751 <title>Getting the best performance out of MQ</title>
753 <para>MQ is very efficient at handling a large number of patches.
754 I ran some performance experiments in mid-2006 for a talk that I
755 gave at the 2006 EuroPython conference
756 <citation>web:europython</citation>. I used as my data set the
757 Linux 2.6.17-mm1 patch series, which consists of 1,738 patches.
758 I applied these on top of a Linux kernel repository containing
759 all 27,472 revisions between Linux 2.6.12-rc2 and Linux
760 2.6.17.</para>
762 <para>On my old, slow laptop, I was able to <command
763 role="hg-cmd">hg qpush <option
764 role="hg-ext-mq-cmd-qpush-opt">hg -a</option></command> all
765 1,738 patches in 3.5 minutes, and <command role="hg-cmd">hg qpop
766 <option role="hg-ext-mq-cmd-qpop-opt">hg -a</option></command>
767 them all in 30 seconds. (On a newer laptop, the time to push
768 all patches dropped to two minutes.) I could <command
769 role="hg-ext-mq">qrefresh</command> one of the biggest patches
770 (which made 22,779 lines of changes to 287 files) in 6.6
771 seconds.</para>
773 <para>Clearly, MQ is well suited to working in large trees, but
774 there are a few tricks you can use to get the best performance
775 of it.</para>
777 <para>First of all, try to <quote>batch</quote> operations
778 together. Every time you run <command
779 role="hg-ext-mq">qpush</command> or <command
780 role="hg-ext-mq">qpop</command>, these commands scan the
781 working directory once to make sure you haven't made some
782 changes and then forgotten to run <command
783 role="hg-ext-mq">qrefresh</command>. On a small tree, the
784 time that this scan takes is unnoticeable. However, on a
785 medium-sized tree (containing tens of thousands of files), it
786 can take a second or more.</para>
788 <para>The <command role="hg-ext-mq">qpush</command> and <command
789 role="hg-ext-mq">qpop</command> commands allow you to push and
790 pop multiple patches at a time. You can identify the
791 <quote>destination patch</quote> that you want to end up at.
792 When you <command role="hg-ext-mq">qpush</command> with a
793 destination specified, it will push patches until that patch is
794 at the top of the applied stack. When you <command
795 role="hg-ext-mq">qpop</command> to a destination, MQ will pop
796 patches until the destination patch is at the top.</para>
798 <para>You can identify a destination patch using either the name
799 of the patch, or by number. If you use numeric addressing,
800 patches are counted from zero; this means that the first patch
801 is zero, the second is one, and so on.</para>
803 </sect1>
804 <sect1 id="sec:mq:merge">
805 <title>Updating your patches when the underlying code
806 changes</title>
808 <para>It's common to have a stack of patches on top of an
809 underlying repository that you don't modify directly. If you're
810 working on changes to third-party code, or on a feature that is
811 taking longer to develop than the rate of change of the code
812 beneath, you will often need to sync up with the underlying
813 code, and fix up any hunks in your patches that no longer apply.
814 This is called <emphasis>rebasing</emphasis> your patch
815 series.</para>
817 <para>The simplest way to do this is to <command role="hg-cmd">hg
818 qpop <option role="hg-ext-mq-cmd-qpop-opt">hg
819 -a</option></command> your patches, then <command
820 role="hg-cmd">hg pull</command> changes into the underlying
821 repository, and finally <command role="hg-cmd">hg qpush <option
822 role="hg-ext-mq-cmd-qpop-opt">hg -a</option></command> your
823 patches again. MQ will stop pushing any time it runs across a
824 patch that fails to apply during conflicts, allowing you to fix
825 your conflicts, <command role="hg-ext-mq">qrefresh</command> the
826 affected patch, and continue pushing until you have fixed your
827 entire stack.</para>
829 <para>This approach is easy to use and works well if you don't
830 expect changes to the underlying code to affect how well your
831 patches apply. If your patch stack touches code that is modified
832 frequently or invasively in the underlying repository, however,
833 fixing up rejected hunks by hand quickly becomes
834 tiresome.</para>
836 <para>It's possible to partially automate the rebasing process.
837 If your patches apply cleanly against some revision of the
838 underlying repo, MQ can use this information to help you to
839 resolve conflicts between your patches and a different
840 revision.</para>
842 <para>The process is a little involved.</para>
843 <orderedlist>
844 <listitem><para>To begin, <command role="hg-cmd">hg qpush
845 -a</command> all of your patches on top of the revision
846 where you know that they apply cleanly.</para>
847 </listitem>
848 <listitem><para>Save a backup copy of your patch directory using
849 <command role="hg-cmd">hg qsave <option
850 role="hg-ext-mq-cmd-qsave-opt">hg -e</option> <option
851 role="hg-ext-mq-cmd-qsave-opt">hg -c</option></command>.
852 This prints the name of the directory that it has saved the
853 patches in. It will save the patches to a directory called
854 <filename role="special"
855 class="directory">.hg/patches.N</filename>, where
856 <literal>N</literal> is a small integer. It also commits a
857 <quote>save changeset</quote> on top of your applied
858 patches; this is for internal book-keeping, and records the
859 states of the <filename role="special">series</filename> and
860 <filename role="special">status</filename> files.</para>
861 </listitem>
862 <listitem><para>Use <command role="hg-cmd">hg pull</command> to
863 bring new changes into the underlying repository. (Don't
864 run <command role="hg-cmd">hg pull -u</command>; see below
865 for why.)</para>
866 </listitem>
867 <listitem><para>Update to the new tip revision, using <command
868 role="hg-cmd">hg update <option
869 role="hg-opt-update">-C</option></command> to override
870 the patches you have pushed.</para>
871 </listitem>
872 <listitem><para>Merge all patches using
873 \hgcmdargs{qpush}{<option role="hg-ext-mq-cmd-qpush-opt">hg
874 -m</option> <option role="hg-ext-mq-cmd-qpush-opt">hg
875 -a</option>}. The <option
876 role="hg-ext-mq-cmd-qpush-opt">hg -m</option> option to
877 <command role="hg-ext-mq">qpush</command> tells MQ to
878 perform a three-way merge if the patch fails to
879 apply.</para>
880 </listitem></orderedlist>
882 <para>During the <command role="hg-cmd">hg qpush <option
883 role="hg-ext-mq-cmd-qpush-opt">hg -m</option></command>,
884 each patch in the <filename role="special">series</filename>
885 file is applied normally. If a patch applies with fuzz or
886 rejects, MQ looks at the queue you <command
887 role="hg-ext-mq">qsave</command>d, and performs a three-way
888 merge with the corresponding changeset. This merge uses
889 Mercurial's normal merge machinery, so it may pop up a GUI merge
890 tool to help you to resolve problems.</para>
892 <para>When you finish resolving the effects of a patch, MQ
893 refreshes your patch based on the result of the merge.</para>
895 <para>At the end of this process, your repository will have one
896 extra head from the old patch queue, and a copy of the old patch
897 queue will be in <filename role="special"
898 class="directory">.hg/patches.N</filename>. You can remove the
899 extra head using <command role="hg-cmd">hg qpop -a -n
900 patches.N</command> or <command role="hg-cmd">hg
901 strip</command>. You can delete <filename role="special"
902 class="directory">.hg/patches.N</filename> once you are sure
903 that you no longer need it as a backup.</para>
905 </sect1>
906 <sect1>
907 <title>Identifying patches</title>
909 <para>MQ commands that work with patches let you refer to a patch
910 either by using its name or by a number. By name is obvious
911 enough; pass the name <filename>foo.patch</filename> to <command
912 role="hg-ext-mq">qpush</command>, for example, and it will
913 push patches until <filename>foo.patch</filename> is
914 applied.</para>
916 <para>As a shortcut, you can refer to a patch using both a name
917 and a numeric offset; <literal>foo.patch-2</literal> means
918 <quote>two patches before <literal>foo.patch</literal></quote>,
919 while <literal>bar.patch+4</literal> means <quote>four patches
920 after <literal>bar.patch</literal></quote>.</para>
922 <para>Referring to a patch by index isn't much different. The
923 first patch printed in the output of <command
924 role="hg-ext-mq">qseries</command> is patch zero (yes, it's
925 one of those start-at-zero counting systems); the second is
926 patch one; and so on.</para>
928 <para>MQ also makes it easy to work with patches when you are
929 using normal Mercurial commands. Every command that accepts a
930 changeset ID will also accept the name of an applied patch. MQ
931 augments the tags normally in the repository with an eponymous
932 one for each applied patch. In addition, the special tags
933 \index{tags!special tag
934 names!<literal>qbase</literal>}<literal>qbase</literal> and
935 \index{tags!special tag
936 names!<literal>qtip</literal>}<literal>qtip</literal> identify
937 the <quote>bottom-most</quote> and topmost applied patches,
938 respectively.</para>
940 <para>These additions to Mercurial's normal tagging capabilities
941 make dealing with patches even more of a breeze.</para>
942 <itemizedlist>
943 <listitem><para>Want to patchbomb a mailing list with your
944 latest series of changes?</para>
945 <programlisting>hg email qbase:qtip
946 </programlisting>
947 <para> (Don't know what <quote>patchbombing</quote> is? See
948 section <xref linkend="sec:hgext:patchbomb"/>.)</para>
949 </listitem>
950 <listitem><para>Need to see all of the patches since
951 <literal>foo.patch</literal> that have touched files in a
952 subdirectory of your tree?</para>
953 <programlisting>
954 hg log -r foo.patch:qtip <emphasis>subdir</emphasis>
955 </programlisting>
956 </listitem>
957 </itemizedlist>
959 <para>Because MQ makes the names of patches available to the rest
960 of Mercurial through its normal internal tag machinery, you
961 don't need to type in the entire name of a patch when you want
962 to identify it by name.</para>
964 <para>Another nice consequence of representing patch names as tags
965 is that when you run the <command role="hg-cmd">hg log</command>
966 command, it will display a patch's name as a tag, simply as part
967 of its normal output. This makes it easy to visually
968 distinguish applied patches from underlying
969 <quote>normal</quote> revisions. The following example shows a
970 few normal Mercurial commands in use with applied
971 patches.</para>
973 &interaction.mq.id.output;
975 </sect1>
976 <sect1>
977 <title>Useful things to know about</title>
979 <para>There are a number of aspects of MQ usage that don't fit
980 tidily into sections of their own, but that are good to know.
981 Here they are, in one place.</para>
983 <itemizedlist>
984 <listitem><para>Normally, when you <command
985 role="hg-ext-mq">qpop</command> a patch and <command
986 role="hg-ext-mq">qpush</command> it again, the changeset
987 that represents the patch after the pop/push will have a
988 <emphasis>different identity</emphasis> than the changeset
989 that represented the hash beforehand. See section <xref
990 linkend="sec:mqref:cmd:qpush"/> for
991 information as to why this is.</para>
992 </listitem>
993 <listitem><para>It's not a good idea to <command
994 role="hg-cmd">hg merge</command> changes from another
995 branch with a patch changeset, at least if you want to
996 maintain the <quote>patchiness</quote> of that changeset and
997 changesets below it on the patch stack. If you try to do
998 this, it will appear to succeed, but MQ will become
999 confused.</para>
1000 </listitem></itemizedlist>
1002 </sect1>
1003 <sect1 id="sec:mq:repo">
1004 <title>Managing patches in a repository</title>
1006 <para>Because MQ's <filename role="special"
1007 class="directory">.hg/patches</filename> directory resides
1008 outside a Mercurial repository's working directory, the
1009 <quote>underlying</quote> Mercurial repository knows nothing
1010 about the management or presence of patches.</para>
1012 <para>This presents the interesting possibility of managing the
1013 contents of the patch directory as a Mercurial repository in its
1014 own right. This can be a useful way to work. For example, you
1015 can work on a patch for a while, <command
1016 role="hg-ext-mq">qrefresh</command> it, then <command
1017 role="hg-cmd">hg commit</command> the current state of the
1018 patch. This lets you <quote>roll back</quote> to that version
1019 of the patch later on.</para>
1021 <para>You can then share different versions of the same patch
1022 stack among multiple underlying repositories. I use this when I
1023 am developing a Linux kernel feature. I have a pristine copy of
1024 my kernel sources for each of several CPU architectures, and a
1025 cloned repository under each that contains the patches I am
1026 working on. When I want to test a change on a different
1027 architecture, I push my current patches to the patch repository
1028 associated with that kernel tree, pop and push all of my
1029 patches, and build and test that kernel.</para>
1031 <para>Managing patches in a repository makes it possible for
1032 multiple developers to work on the same patch series without
1033 colliding with each other, all on top of an underlying source
1034 base that they may or may not control.</para>
1036 <sect2>
1037 <title>MQ support for patch repositories</title>
1039 <para>MQ helps you to work with the <filename role="special"
1040 class="directory">.hg/patches</filename> directory as a
1041 repository; when you prepare a repository for working with
1042 patches using <command role="hg-ext-mq">qinit</command>, you
1043 can pass the <option role="hg-ext-mq-cmd-qinit-opt">hg
1044 -c</option> option to create the <filename role="special"
1045 class="directory">.hg/patches</filename> directory as a
1046 Mercurial repository.</para>
1048 <note>
1049 <para> If you forget to use the <option
1050 role="hg-ext-mq-cmd-qinit-opt">hg -c</option> option, you
1051 can simply go into the <filename role="special"
1052 class="directory">.hg/patches</filename> directory at any
1053 time and run <command role="hg-cmd">hg init</command>.
1054 Don't forget to add an entry for the <filename
1055 role="special">status</filename> file to the <filename
1056 role="special">.hgignore</filename> file, though</para>
1058 <para> (<command role="hg-cmd">hg qinit <option
1059 role="hg-ext-mq-cmd-qinit-opt">hg -c</option></command>
1060 does this for you automatically); you
1061 <emphasis>really</emphasis> don't want to manage the
1062 <filename role="special">status</filename> file.</para>
1063 </note>
1065 <para>As a convenience, if MQ notices that the <filename
1066 class="directory">.hg/patches</filename> directory is a
1067 repository, it will automatically <command role="hg-cmd">hg
1068 add</command> every patch that you create and import.</para>
1070 <para>MQ provides a shortcut command, <command
1071 role="hg-ext-mq">qcommit</command>, that runs <command
1072 role="hg-cmd">hg commit</command> in the <filename
1073 role="special" class="directory">.hg/patches</filename>
1074 directory. This saves some bothersome typing.</para>
1076 <para>Finally, as a convenience to manage the patch directory,
1077 you can define the alias <command>mq</command> on Unix
1078 systems. For example, on Linux systems using the
1079 <command>bash</command> shell, you can include the following
1080 snippet in your <filename
1081 role="home">~/.bashrc</filename>.</para>
1083 <programlisting>alias mq=`hg -R $(hg
1084 root)/.hg/patches'</programlisting>
1086 <para>You can then issue commands of the form <command>mq
1087 pull</command> from the main repository.</para>
1089 </sect2>
1090 <sect2>
1091 <title>A few things to watch out for</title>
1093 <para>MQ's support for working with a repository full of patches
1094 is limited in a few small respects.</para>
1096 <para>MQ cannot automatically detect changes that you make to
1097 the patch directory. If you <command role="hg-cmd">hg
1098 pull</command>, manually edit, or <command role="hg-cmd">hg
1099 update</command> changes to patches or the <filename
1100 role="special">series</filename> file, you will have to
1101 <command role="hg-cmd">hg qpop <option
1102 role="hg-ext-mq-cmd-qpop-opt">hg -a</option></command> and
1103 then <command role="hg-cmd">hg qpush <option
1104 role="hg-ext-mq-cmd-qpush-opt">hg -a</option></command> in
1105 the underlying repository to see those changes show up there.
1106 If you forget to do this, you can confuse MQ's idea of which
1107 patches are applied.</para>
1109 </sect2>
1110 </sect1>
1111 <sect1 id="sec:mq:tools">
1112 <title>Third party tools for working with patches</title>
1114 <para>Once you've been working with patches for a while, you'll
1115 find yourself hungry for tools that will help you to understand
1116 and manipulate the patches you're dealing with.</para>
1118 <para>The <command>diffstat</command> command
1119 <citation>web:diffstat</citation> generates a histogram of the
1120 modifications made to each file in a patch. It provides a good
1121 way to <quote>get a sense of</quote> a patch&emdash;which files
1122 it affects, and how much change it introduces to each file and
1123 as a whole. (I find that it's a good idea to use
1124 <command>diffstat</command>'s <option
1125 role="cmd-opt-diffstat">-p</option> option as a matter of
1126 course, as otherwise it will try to do clever things with
1127 prefixes of file names that inevitably confuse at least
1128 me.)</para>
1130 &interaction.mq.tools.tools;
1132 <para>The <literal role="package">patchutils</literal> package
1133 <citation>web:patchutils</citation> is invaluable. It provides a
1134 set of small utilities that follow the <quote>Unix
1135 philosophy;</quote> each does one useful thing with a patch.
1136 The <literal role="package">patchutils</literal> command I use
1137 most is <command>filterdiff</command>, which extracts subsets
1138 from a patch file. For example, given a patch that modifies
1139 hundreds of files across dozens of directories, a single
1140 invocation of <command>filterdiff</command> can generate a
1141 smaller patch that only touches files whose names match a
1142 particular glob pattern. See section <xref
1143 linkend="mq-collab:tips:interdiff"/> for another
1144 example.</para>
1146 </sect1>
1147 <sect1>
1148 <title>Good ways to work with patches</title>
1150 <para>Whether you are working on a patch series to submit to a
1151 free software or open source project, or a series that you
1152 intend to treat as a sequence of regular changesets when you're
1153 done, you can use some simple techniques to keep your work well
1154 organised.</para>
1156 <para>Give your patches descriptive names. A good name for a
1157 patch might be <filename>rework-device-alloc.patch</filename>,
1158 because it will immediately give you a hint what the purpose of
1159 the patch is. Long names shouldn't be a problem; you won't be
1160 typing the names often, but you <emphasis>will</emphasis> be
1161 running commands like <command
1162 role="hg-ext-mq">qapplied</command> and <command
1163 role="hg-ext-mq">qtop</command> over and over. Good naming
1164 becomes especially important when you have a number of patches
1165 to work with, or if you are juggling a number of different tasks
1166 and your patches only get a fraction of your attention.</para>
1168 <para>Be aware of what patch you're working on. Use the <command
1169 role="hg-ext-mq">qtop</command> command and skim over the text
1170 of your patches frequently&emdash;for example, using <command
1171 role="hg-cmd">hg tip <option
1172 role="hg-opt-tip">-p</option></command>)&emdash;to be sure
1173 of where you stand. I have several times worked on and <command
1174 role="hg-ext-mq">qrefresh</command>ed a patch other than the
1175 one I intended, and it's often tricky to migrate changes into
1176 the right patch after making them in the wrong one.</para>
1178 <para>For this reason, it is very much worth investing a little
1179 time to learn how to use some of the third-party tools I
1180 described in section <xref linkend="sec:mq:tools"/>,
1181 particularly
1182 <command>diffstat</command> and <command>filterdiff</command>.
1183 The former will give you a quick idea of what changes your patch
1184 is making, while the latter makes it easy to splice hunks
1185 selectively out of one patch and into another.</para>
1187 </sect1>
1188 <sect1>
1189 <title>MQ cookbook</title>
1191 <sect2>
1192 <title>Manage <quote>trivial</quote> patches</title>
1194 <para>Because the overhead of dropping files into a new
1195 Mercurial repository is so low, it makes a lot of sense to
1196 manage patches this way even if you simply want to make a few
1197 changes to a source tarball that you downloaded.</para>
1199 <para>Begin by downloading and unpacking the source tarball, and
1200 turning it into a Mercurial repository.</para>
1202 &interaction.mq.tarball.download;
1204 <para>Continue by creating a patch stack and making your
1205 changes.</para>
1207 &interaction.mq.tarball.qinit;
1209 <para>Let's say a few weeks or months pass, and your package
1210 author releases a new version. First, bring their changes
1211 into the repository.</para>
1213 &interaction.mq.tarball.newsource;
1215 <para>The pipeline starting with <command role="hg-cmd">hg
1216 locate</command> above deletes all files in the working
1217 directory, so that <command role="hg-cmd">hg
1218 commit</command>'s <option
1219 role="hg-opt-commit">--addremove</option> option can
1220 actually tell which files have really been removed in the
1221 newer version of the source.</para>
1223 <para>Finally, you can apply your patches on top of the new
1224 tree.</para>
1226 &interaction.mq.tarball.repush;
1228 </sect2>
1229 <sect2 id="sec:mq:combine">
1230 <title>Combining entire patches</title>
1232 <para>MQ provides a command, <command
1233 role="hg-ext-mq">qfold</command> that lets you combine
1234 entire patches. This <quote>folds</quote> the patches you
1235 name, in the order you name them, into the topmost applied
1236 patch, and concatenates their descriptions onto the end of its
1237 description. The patches that you fold must be unapplied
1238 before you fold them.</para>
1240 <para>The order in which you fold patches matters. If your
1241 topmost applied patch is <literal>foo</literal>, and you
1242 <command role="hg-ext-mq">qfold</command>
1243 <literal>bar</literal> and <literal>quux</literal> into it,
1244 you will end up with a patch that has the same effect as if
1245 you applied first <literal>foo</literal>, then
1246 <literal>bar</literal>, followed by
1247 <literal>quux</literal>.</para>
1249 </sect2>
1250 <sect2>
1251 <title>Merging part of one patch into another</title>
1253 <para>Merging <emphasis>part</emphasis> of one patch into
1254 another is more difficult than combining entire
1255 patches.</para>
1257 <para>If you want to move changes to entire files, you can use
1258 <command>filterdiff</command>'s <option
1259 role="cmd-opt-filterdiff">-i</option> and <option
1260 role="cmd-opt-filterdiff">-x</option> options to choose the
1261 modifications to snip out of one patch, concatenating its
1262 output onto the end of the patch you want to merge into. You
1263 usually won't need to modify the patch you've merged the
1264 changes from. Instead, MQ will report some rejected hunks
1265 when you <command role="hg-ext-mq">qpush</command> it (from
1266 the hunks you moved into the other patch), and you can simply
1267 <command role="hg-ext-mq">qrefresh</command> the patch to drop
1268 the duplicate hunks.</para>
1270 <para>If you have a patch that has multiple hunks modifying a
1271 file, and you only want to move a few of those hunks, the job
1272 becomes more messy, but you can still partly automate it. Use
1273 <command>lsdiff -nvv</command> to print some metadata about
1274 the patch.</para>
1276 &interaction.mq.tools.lsdiff;
1278 <para>This command prints three different kinds of
1279 number:</para>
1280 <itemizedlist>
1281 <listitem><para>(in the first column) a <emphasis>file
1282 number</emphasis> to identify each file modified in the
1283 patch;</para>
1284 </listitem>
1285 <listitem><para>(on the next line, indented) the line number
1286 within a modified file where a hunk starts; and</para>
1287 </listitem>
1288 <listitem><para>(on the same line) a <emphasis>hunk
1289 number</emphasis> to identify that hunk.</para>
1290 </listitem></itemizedlist>
1292 <para>You'll have to use some visual inspection, and reading of
1293 the patch, to identify the file and hunk numbers you'll want,
1294 but you can then pass them to to
1295 <command>filterdiff</command>'s <option
1296 role="cmd-opt-filterdiff">--files</option> and <option
1297 role="cmd-opt-filterdiff">--hunks</option> options, to
1298 select exactly the file and hunk you want to extract.</para>
1300 <para>Once you have this hunk, you can concatenate it onto the
1301 end of your destination patch and continue with the remainder
1302 of section <xref linkend="sec:mq:combine"/>.</para>
1304 </sect2>
1305 </sect1>
1306 <sect1>
1307 <title>Differences between quilt and MQ</title>
1309 <para>If you are already familiar with quilt, MQ provides a
1310 similar command set. There are a few differences in the way
1311 that it works.</para>
1313 <para>You will already have noticed that most quilt commands have
1314 MQ counterparts that simply begin with a
1315 <quote><literal>q</literal></quote>. The exceptions are quilt's
1316 <literal>add</literal> and <literal>remove</literal> commands,
1317 the counterparts for which are the normal Mercurial <command
1318 role="hg-cmd">hg add</command> and <command role="hg-cmd">hg
1319 remove</command> commands. There is no MQ equivalent of the
1320 quilt <literal>edit</literal> command.</para>
1322 </sect1>
1323 </chapter>
1325 <!--
1326 local variables:
1327 sgml-parent-document: ("00book.xml" "book" "chapter")
1328 end:
1329 -->