hgbook

view en/mq.tex @ 8:a25335b56825

Progress on MQ tutorial.
author Bryan O'Sullivan <bos@serpentine.com>
date Tue Jun 27 23:21:05 2006 -0700 (2006-06-27)
parents 339e75288632
children e9d5b4c3d16b
line source
1 \chapter{Managing change with Mercurial Queues}
2 \label{chap:mq}
4 \section{The patch management problem}
5 \label{sec:mq:patch-mgmt}
7 Here is a common scenario: you need to install a software package from
8 source, but you find a bug that you must fix in the source before you
9 can start using the package. You make your changes, forget about the
10 package for a while, and a few months later you need to upgrade to a
11 newer version of the package. If the newer version of the package
12 still has the bug, you must extract your fix from the older source
13 tree and apply it against the newer version. This is a tedious task,
14 and it's easy to make mistakes.
16 This is a simple case of the ``patch management'' problem. You have
17 an ``upstream'' source tree that you can't change; you need to make
18 some local changes on top of the upstream tree; and you'd like to be
19 able to keep those changes separate, so that you can apply them to
20 newer versions of the upstream source.
22 The patch management problem arises in many situations. Probably the
23 most visible is that a user of an open source software project will
24 contribute a bug fix or new feature to the project's maintainers in the
25 form of a patch.
27 Distributors of operating systems that include open source software
28 often need to make changes to the packages they distribute so that
29 they will build properly in their environments.
31 When you have few changes to maintain, it is easy to manage a single
32 patch using the standard \texttt{diff} and \texttt{patch} programs.
33 Once the number of changes grows, it starts to makes sense to maintain
34 patches as discrete ``chunks of work,'' so that for example a single
35 patch will contain only one bug fix (the patch might modify several
36 files, but it's doing ``only one thing''), and you may have a number
37 of such patches for different bugs you need fixed and local changes
38 you require. In this situation, if you submit a bug fix patch to the
39 upstream maintainers of a package and they include your fix in a
40 subsequent release, you can simply drop that single patch when you're
41 updating to the newer release.
43 Maintaining a single patch against an upstream tree is a little
44 tedious and error-prone, but not difficult. However, the complexity
45 of the problem grows rapidly as the number of patches you have to
46 maintain increases. With more than a tiny number of patches in hand,
47 understanding which ones you have applied and maintaining them moves
48 from messy to overwhelming.
50 Fortunately, Mercurial includes a powerful extension, Mercurial Queues
51 (or simply ``MQ''), that massively simplifies the patch management
52 problem.
54 \section{The prehistory of Mercurial Queues}
55 \label{sec:mq:history}
57 During the late 1990s, several Linux kernel developers started to
58 maintain ``patch series'' that modified the behaviour of the Linux
59 kernel. Some of these series were focused on stability, some on
60 feature coverage, and others were more speculative.
62 The sizes of these patch series grew rapidly. In 2002, Andrew Morton
63 published some shell scripts he had been using to automate the task of
64 managing his patch queues. Andrew was successfully using these
65 scripts to manage hundreds (sometimes thousands) of patches on top of
66 the Linux kernel.
68 \subsection{A patchwork quilt}
69 \label{sec:mq:quilt}
72 In early 2003, Andreas Gruenbacher and Martin Quinson borrowed the
73 approach of Andrew's scripts and published a tool called ``patchwork
74 quilt''~\cite{web:quilt}, or simply ``quilt''
75 (see~\cite{gruenbacher:2005} for a paper describing it). Because
76 quilt substantially automated patch management, it rapidly gained a
77 large following among open source software developers.
79 Quilt manages a \emph{stack of patches} on top of a directory tree.
80 To begin, you tell quilt to manage a directory tree; it stores away
81 the names and contents of all files in the tree. To fix a bug, you
82 create a new patch (using a single command), edit the files you need
83 to fix, then ``refresh'' the patch.
85 The refresh step causes quilt to scan the directory tree; it updates
86 the patch with all of the changes you have made. You can create
87 another patch on top of the first, which will track the changes
88 required to modify the tree from ``tree with one patch applied'' to
89 ``tree with two patches applied''.
91 You can \emph{change} which patches are applied to the tree. If you
92 ``pop'' a patch, the changes made by that patch will vanish from the
93 directory tree. Quilt remembers which patches you have popped,
94 though, so you can ``push'' a popped patch again, and the directory
95 tree will be restored to contain the modifications in the patch. Most
96 importantly, you can run the ``refresh'' command at any time, and the
97 topmost applied patch will be updated. This means that you can, at
98 any time, change both which patches are applied and what
99 modifications those patches make.
101 Quilt knows nothing about revision control tools, so it works equally
102 well on top of an unpacked tarball or a Subversion repository.
104 \subsection{From patchwork quilt to Mercurial Queues}
105 \label{sec:mq:quilt-mq}
107 In mid-2005, Chris Mason took the features of quilt and wrote an
108 extension that he called Mercurial Queues, which added quilt-like
109 behaviour to Mercurial.
111 The key difference between quilt and MQ is that quilt knows nothing
112 about revision control systems, while MQ is \emph{integrated} into
113 Mercurial. Each patch that you push is represented as a Mercurial
114 changeset. Pop a patch, and the changeset goes away.
116 This integration makes understanding patches and debugging their
117 effects \emph{enormously} easier. Since every applied patch has an
118 associated changeset, you can use \hgcmdargs{log}{\emph{filename}} to
119 see which changesets and patches affected a file. You can use the
120 \hgext{bisect} extension to binary-search through all changesets and
121 applied patches to see where a bug got introduced or fixed. You can
122 use the \hgcmd{annotate} command to see which changeset or patch
123 modified a particular line of a source file. And so on.
125 Because quilt does not care about revision control tools, it is still
126 a tremendously useful piece of software to know about for situations
127 where you cannot use Mercurial and MQ.
128 \section{Getting started with Mercurial Queues}
129 \label{sec:mq:start}
131 Because MQ is implemented as an extension, you must explicitly enable
132 before you can use it. (You don't need to download anything; MQ ships
133 with the standard Mercurial distribution.) To enable MQ, edit your
134 \tildefile{.hgrc} file, and add the lines in figure~\ref{ex:mq:config}.
136 \begin{figure}[h]
137 \begin{codesample4}
138 [extensions]
139 hgext.mq =
140 \end{codesample4}
141 \label{ex:mq:config}
142 \caption{Contents to add to \tildefile{.hgrc} to enable the MQ extension}
143 \end{figure}
145 Once the extension is enabled, it will make a number of new commands
146 available. To verify that the extension is working, you can use
147 \hgcmd{help} to see if the \hgcmd{qinit} command is now available; see
148 the example in figure~\ref{ex:mq:enabled}.
150 \begin{figure}[h]
151 \interaction{mq.qinit-help.help}
152 \caption{How to verify that MQ is enabled}
153 \label{ex:mq:enabled}
154 \end{figure}
156 You can use MQ with \emph{any} Mercurial repository, and its commands
157 only operate within that repository. To get started, simply prepare
158 the repository using the \hgcmd{qinit} command (see
159 figure~\ref{ex:mq:qinit}). This command creates an empty directory
160 called \filename{.hg/patches}, where MQ will keep its metadata. As
161 with many Mercurial commands, the \hgcmd{qinit} command prints nothing
162 if it succeeds.
164 \begin{figure}[h]
165 \interaction{mq.tutorial.qinit}
166 \caption{Preparing a repository for use with MQ}
167 \label{ex:mq:qinit}
168 \end{figure}
170 \begin{figure}[h]
171 \interaction{mq.tutorial.qnew}
172 \caption{Creating a new patch}
173 \label{ex:mq:qnew}
174 \end{figure}
176 \subsection{Creating a new patch}
178 To begin work on a new patch, use the \hgcmd{qnew} command. This
179 command takes one argument, the name of the patch to create. MQ will
180 use this as the name of an actual file in the \filename{.hg/patches}
181 directory, as you can see in figure~\ref{ex:mq:qnew}.
183 Also newly present in the \filename{.hg/patches} directory are two
184 other files, \filename{series} and \filename{status}. The
185 \filename{series} file lists all of the patches that MQ knows about
186 for this repository, with one patch per line. Mercurial uses the
187 \filename{status} file for internal book-keeping; it tracks all of the
188 patches that MQ has \emph{applied} in this repository.
190 \begin{note}
191 You may sometimes want to edit the \filename{series} file by hand;
192 for example, to change the sequence in which some patches are
193 applied. However, manually editing the \filename{status} file is
194 almost always a bad idea, as it's easy to corrupt MQ's idea of what
195 is happening.
196 \end{note}
198 Once you have created your new patch, you can edit files in the
199 working directory as you usually would. All of the normal Mercurial
200 commands, such as \hgcmd{diff} and \hgcmd{annotate}, work exactly as
201 they did before.
202 \subsection{Refreshing a patch}
204 When you reach a point where you want to save your work, use the
205 \hgcmd{qrefresh} command (figure~\ref{ex:mq:qnew}) to update the patch
206 you are working on. This command folds the changes you have made in
207 the working directory into your patch, and updates its corresponding
208 changeset to contain those changes.
210 \begin{figure}[h]
211 \interaction{mq.tutorial.qrefresh}
212 \caption{Refreshing a patch}
213 \label{ex:mq:qrefresh}
214 \end{figure}
216 You can run \hgcmd{qrefresh} as often as you like, so it's a good way
217 to ``checkpoint'' your work. Reefresh your patch at an opportune
218 time; try an experiment; and if the experiment doesn't work out,
219 \hgcmd{revert} your modifications back to the last time you refreshed.
221 \begin{figure}[h]
222 \interaction{mq.tutorial.qrefresh2}
223 \caption{Refresh a patch many times to accumulate changes}
224 \label{ex:mq:qrefresh2}
225 \end{figure}
227 \subsection{Stacking and tracking patches}
229 Once you have finished working on a patch, or need to work on another,
230 you can use the \hgcmd{qnew} command again to create a new patch.
231 Mercurial will apply this patch on top of your existing patch. See
232 figure~\ref{ex:mq:qnew2} for an example. Notice that the patch
233 contains the changes in our prior patch as part of its context (you
234 can see this more clearly in the output of \hgcmd{annotate}).
236 \begin{figure}[h]
237 \interaction{mq.tutorial.qnew2}
238 \caption{Stacking a second patch on top of the first}
239 \label{ex:mq:qnew2}
240 \end{figure}
242 So far, with the exception of \hgcmd{qnew} and \hgcmd{qrefresh}, we've
243 been careful to only use regular Mercurial commands. However, there
244 are more ``natural'' commands you can use when thinking about patches
245 with MQ, as illustrated in figure~\ref{ex:mq:qseries}:
247 \begin{itemize}
248 \item The \hgcmd{qseries} command lists every patch that MQ knows
249 about in this repository, from oldest to newest (most recently
250 \emph{created}).
251 \item The \hgcmd{qapplied} command lists every patch that MQ has
252 \emph{applied} in this repository, again from oldest to newest (most
253 recently applied).
254 \end{itemize}
256 \begin{figure}[h]
257 \interaction{mq.tutorial.qseries}
258 \caption{Understanding the patch stack with \hgcmd{qseries} and
259 \hgcmd{qapplied}}
260 \label{ex:mq:qseries}
261 \end{figure}
263 \subsection{Manipulating the patch stack}
265 The previous discussion implied that there must be a difference
266 between ``known'' and ``applied'' patches, and there is. MQ can know
267 about a patch without it being applied in the repository.
269 An \emph{applied} patch has a corresponding changeset in the
270 repository, and the effects of the patch and changeset are visible in
271 the working directory. You can undo the application of a patch using
272 the \hgcmd{qpop} command. MQ still \emph{knows about} a popped patch,
273 but it no longer has a corresponding changeset in the repository, and
274 the working directory does not contain the changes made by the patch.
276 \begin{figure}[h]
277 \interaction{mq.tutorial.qpop}
278 \caption{Modifying the stack of applied patches}
279 \label{ex:mq:qpop}
280 \end{figure}
282 You can reapply an unapplied, or popped, patch using the \hgcmd{qpush}
283 command. This creates a new changeset to correspond to the patch, and
284 the patch's changes once again become present in the working
285 directory. See figure~\ref{ex:mq:qpop} for examples of \hgcmd{qpop}
286 and \hgcmd{qpush} in action. Notice that once we have popped a patch
287 or two patches, the output of \hgcmd{qseries} remains the same, while
288 that of \hgcmd{qapplied} has changed.
290 MQ does not limit you to pushing or popping one patch. You can have
291 no patches, all of them, or any number in between applied at some
292 point in time.
294 %%% Local Variables:
295 %%% mode: latex
296 %%% TeX-master: "00book"
297 %%% End: