hgbook

view en/ch08-branch.tex @ 551:f72b7e6cbe90

Snapshot.
author Bryan O'Sullivan <bos@serpentine.com>
date Thu Feb 05 00:01:16 2009 -0800 (2009-02-05)
parents 5cd47f721686
children
line source
1 \chapter{Managing releases and branchy development}
2 \label{chap:branch}
4 Mercurial provides several mechanisms for you to manage a project that
5 is making progress on multiple fronts at once. To understand these
6 mechanisms, let's first take a brief look at a fairly normal software
7 project structure.
9 Many software projects issue periodic ``major'' releases that contain
10 substantial new features. In parallel, they may issue ``minor''
11 releases. These are usually identical to the major releases off which
12 they're based, but with a few bugs fixed.
14 In this chapter, we'll start by talking about how to keep records of
15 project milestones such as releases. We'll then continue on to talk
16 about the flow of work between different phases of a project, and how
17 Mercurial can help you to isolate and manage this work.
19 \section{Giving a persistent name to a revision}
21 Once you decide that you'd like to call a particular revision a
22 ``release'', it's a good idea to record the identity of that revision.
23 This will let you reproduce that release at a later date, for whatever
24 purpose you might need at the time (reproducing a bug, porting to a
25 new platform, etc).
26 \interaction{tag.init}
28 Mercurial lets you give a permanent name to any revision using the
29 \hgcmd{tag} command. Not surprisingly, these names are called
30 ``tags''.
31 \interaction{tag.tag}
33 A tag is nothing more than a ``symbolic name'' for a revision. Tags
34 exist purely for your convenience, so that you have a handy permanent
35 way to refer to a revision; Mercurial doesn't interpret the tag names
36 you use in any way. Neither does Mercurial place any restrictions on
37 the name of a tag, beyond a few that are necessary to ensure that a
38 tag can be parsed unambiguously. A tag name cannot contain any of the
39 following characters:
40 \begin{itemize}
41 \item Colon (ASCII 58, ``\texttt{:}'')
42 \item Carriage return (ASCII 13, ``\Verb+\r+'')
43 \item Newline (ASCII 10, ``\Verb+\n+'')
44 \end{itemize}
46 You can use the \hgcmd{tags} command to display the tags present in
47 your repository. In the output, each tagged revision is identified
48 first by its name, then by revision number, and finally by the unique
49 hash of the revision.
50 \interaction{tag.tags}
51 Notice that \texttt{tip} is listed in the output of \hgcmd{tags}. The
52 \texttt{tip} tag is a special ``floating'' tag, which always
53 identifies the newest revision in the repository.
55 In the output of the \hgcmd{tags} command, tags are listed in reverse
56 order, by revision number. This usually means that recent tags are
57 listed before older tags. It also means that \texttt{tip} is always
58 going to be the first tag listed in the output of \hgcmd{tags}.
60 When you run \hgcmd{log}, if it displays a revision that has tags
61 associated with it, it will print those tags.
62 \interaction{tag.log}
64 Any time you need to provide a revision~ID to a Mercurial command, the
65 command will accept a tag name in its place. Internally, Mercurial
66 will translate your tag name into the corresponding revision~ID, then
67 use that.
68 \interaction{tag.log.v1.0}
70 There's no limit on the number of tags you can have in a repository,
71 or on the number of tags that a single revision can have. As a
72 practical matter, it's not a great idea to have ``too many'' (a number
73 which will vary from project to project), simply because tags are
74 supposed to help you to find revisions. If you have lots of tags, the
75 ease of using them to identify revisions diminishes rapidly.
77 For example, if your project has milestones as frequent as every few
78 days, it's perfectly reasonable to tag each one of those. But if you
79 have a continuous build system that makes sure every revision can be
80 built cleanly, you'd be introducing a lot of noise if you were to tag
81 every clean build. Instead, you could tag failed builds (on the
82 assumption that they're rare!), or simply not use tags to track
83 buildability.
85 If you want to remove a tag that you no longer want, use
86 \hgcmdargs{tag}{--remove}.
87 \interaction{tag.remove}
88 You can also modify a tag at any time, so that it identifies a
89 different revision, by simply issuing a new \hgcmd{tag} command.
90 You'll have to use the \hgopt{tag}{-f} option to tell Mercurial that
91 you \emph{really} want to update the tag.
92 \interaction{tag.replace}
93 There will still be a permanent record of the previous identity of the
94 tag, but Mercurial will no longer use it. There's thus no penalty to
95 tagging the wrong revision; all you have to do is turn around and tag
96 the correct revision once you discover your error.
98 Mercurial stores tags in a normal revision-controlled file in your
99 repository. If you've created any tags, you'll find them in a file
100 named \sfilename{.hgtags}. When you run the \hgcmd{tag} command,
101 Mercurial modifies this file, then automatically commits the change to
102 it. This means that every time you run \hgcmd{tag}, you'll see a
103 corresponding changeset in the output of \hgcmd{log}.
104 \interaction{tag.tip}
106 \subsection{Handling tag conflicts during a merge}
108 You won't often need to care about the \sfilename{.hgtags} file, but
109 it sometimes makes its presence known during a merge. The format of
110 the file is simple: it consists of a series of lines. Each line
111 starts with a changeset hash, followed by a space, followed by the
112 name of a tag.
114 If you're resolving a conflict in the \sfilename{.hgtags} file during
115 a merge, there's one twist to modifying the \sfilename{.hgtags} file:
116 when Mercurial is parsing the tags in a repository, it \emph{never}
117 reads the working copy of the \sfilename{.hgtags} file. Instead, it
118 reads the \emph{most recently committed} revision of the file.
120 An unfortunate consequence of this design is that you can't actually
121 verify that your merged \sfilename{.hgtags} file is correct until
122 \emph{after} you've committed a change. So if you find yourself
123 resolving a conflict on \sfilename{.hgtags} during a merge, be sure to
124 run \hgcmd{tags} after you commit. If it finds an error in the
125 \sfilename{.hgtags} file, it will report the location of the error,
126 which you can then fix and commit. You should then run \hgcmd{tags}
127 again, just to be sure that your fix is correct.
129 \subsection{Tags and cloning}
131 You may have noticed that the \hgcmd{clone} command has a
132 \hgopt{clone}{-r} option that lets you clone an exact copy of the
133 repository as of a particular changeset. The new clone will not
134 contain any project history that comes after the revision you
135 specified. This has an interaction with tags that can surprise the
136 unwary.
138 Recall that a tag is stored as a revision to the \sfilename{.hgtags}
139 file, so that when you create a tag, the changeset in which it's
140 recorded necessarily refers to an older changeset. When you run
141 \hgcmdargs{clone}{-r foo} to clone a repository as of tag
142 \texttt{foo}, the new clone \emph{will not contain the history that created the tag} that you used to clone the repository. The result
143 is that you'll get exactly the right subset of the project's history
144 in the new repository, but \emph{not} the tag you might have expected.
146 \subsection{When permanent tags are too much}
148 Since Mercurial's tags are revision controlled and carried around with
149 a project's history, everyone you work with will see the tags you
150 create. But giving names to revisions has uses beyond simply noting
151 that revision \texttt{4237e45506ee} is really \texttt{v2.0.2}. If
152 you're trying to track down a subtle bug, you might want a tag to
153 remind you of something like ``Anne saw the symptoms with this
154 revision''.
156 For cases like this, what you might want to use are \emph{local} tags.
157 You can create a local tag with the \hgopt{tag}{-l} option to the
158 \hgcmd{tag} command. This will store the tag in a file called
159 \sfilename{.hg/localtags}. Unlike \sfilename{.hgtags},
160 \sfilename{.hg/localtags} is not revision controlled. Any tags you
161 create using \hgopt{tag}{-l} remain strictly local to the repository
162 you're currently working in.
164 \section{The flow of changes---big picture vs. little}
166 To return to the outline I sketched at the beginning of a chapter,
167 let's think about a project that has multiple concurrent pieces of
168 work under development at once.
170 There might be a push for a new ``main'' release; a new minor bugfix
171 release to the last main release; and an unexpected ``hot fix'' to an
172 old release that is now in maintenance mode.
174 The usual way people refer to these different concurrent directions of
175 development is as ``branches''. However, we've already seen numerous
176 times that Mercurial treats \emph{all of history} as a series of
177 branches and merges. Really, what we have here is two ideas that are
178 peripherally related, but which happen to share a name.
179 \begin{itemize}
180 \item ``Big picture'' branches represent the sweep of a project's
181 evolution; people give them names, and talk about them in
182 conversation.
183 \item ``Little picture'' branches are artefacts of the day-to-day
184 activity of developing and merging changes. They expose the
185 narrative of how the code was developed.
186 \end{itemize}
188 \section{Managing big-picture branches in repositories}
190 The easiest way to isolate a ``big picture'' branch in Mercurial is in
191 a dedicated repository. If you have an existing shared
192 repository---let's call it \texttt{myproject}---that reaches a ``1.0''
193 milestone, you can start to prepare for future maintenance releases on
194 top of version~1.0 by tagging the revision from which you prepared
195 the~1.0 release.
196 \interaction{branch-repo.tag}
197 You can then clone a new shared \texttt{myproject-1.0.1} repository as
198 of that tag.
199 \interaction{branch-repo.clone}
201 Afterwards, if someone needs to work on a bug fix that ought to go
202 into an upcoming~1.0.1 minor release, they clone the
203 \texttt{myproject-1.0.1} repository, make their changes, and push them
204 back.
205 \interaction{branch-repo.bugfix}
206 Meanwhile, development for the next major release can continue,
207 isolated and unabated, in the \texttt{myproject} repository.
208 \interaction{branch-repo.new}
210 \section{Don't repeat yourself: merging across branches}
212 In many cases, if you have a bug to fix on a maintenance branch, the
213 chances are good that the bug exists on your project's main branch
214 (and possibly other maintenance branches, too). It's a rare developer
215 who wants to fix the same bug multiple times, so let's look at a few
216 ways that Mercurial can help you to manage these bugfixes without
217 duplicating your work.
219 In the simplest instance, all you need to do is pull changes from your
220 maintenance branch into your local clone of the target branch.
221 \interaction{branch-repo.pull}
222 You'll then need to merge the heads of the two branches, and push back
223 to the main branch.
224 \interaction{branch-repo.merge}
226 \section{Naming branches within one repository}
228 In most instances, isolating branches in repositories is the right
229 approach. Its simplicity makes it easy to understand; and so it's
230 hard to make mistakes. There's a one-to-one relationship between
231 branches you're working in and directories on your system. This lets
232 you use normal (non-Mercurial-aware) tools to work on files within a
233 branch/repository.
235 If you're more in the ``power user'' category (\emph{and} your
236 collaborators are too), there is an alternative way of handling
237 branches that you can consider. I've already mentioned the
238 human-level distinction between ``small picture'' and ``big picture''
239 branches. While Mercurial works with multiple ``small picture''
240 branches in a repository all the time (for example after you pull
241 changes in, but before you merge them), it can \emph{also} work with
242 multiple ``big picture'' branches.
244 The key to working this way is that Mercurial lets you assign a
245 persistent \emph{name} to a branch. There always exists a branch
246 named \texttt{default}. Even before you start naming branches
247 yourself, you can find traces of the \texttt{default} branch if you
248 look for them.
250 As an example, when you run the \hgcmd{commit} command, and it pops up
251 your editor so that you can enter a commit message, look for a line
252 that contains the text ``\texttt{HG: branch default}'' at the bottom.
253 This is telling you that your commit will occur on the branch named
254 \texttt{default}.
256 To start working with named branches, use the \hgcmd{branches}
257 command. This command lists the named branches already present in
258 your repository, telling you which changeset is the tip of each.
259 \interaction{branch-named.branches}
260 Since you haven't created any named branches yet, the only one that
261 exists is \texttt{default}.
263 To find out what the ``current'' branch is, run the \hgcmd{branch}
264 command, giving it no arguments. This tells you what branch the
265 parent of the current changeset is on.
266 \interaction{branch-named.branch}
268 To create a new branch, run the \hgcmd{branch} command again. This
269 time, give it one argument: the name of the branch you want to create.
270 \interaction{branch-named.create}
272 After you've created a branch, you might wonder what effect the
273 \hgcmd{branch} command has had. What do the \hgcmd{status} and
274 \hgcmd{tip} commands report?
275 \interaction{branch-named.status}
276 Nothing has changed in the working directory, and there's been no new
277 history created. As this suggests, running the \hgcmd{branch} command
278 has no permanent effect; it only tells Mercurial what branch name to
279 use the \emph{next} time you commit a changeset.
281 When you commit a change, Mercurial records the name of the branch on
282 which you committed. Once you've switched from the \texttt{default}
283 branch to another and committed, you'll see the name of the new branch
284 show up in the output of \hgcmd{log}, \hgcmd{tip}, and other commands
285 that display the same kind of output.
286 \interaction{branch-named.commit}
287 The \hgcmd{log}-like commands will print the branch name of every
288 changeset that's not on the \texttt{default} branch. As a result, if
289 you never use named branches, you'll never see this information.
291 Once you've named a branch and committed a change with that name,
292 every subsequent commit that descends from that change will inherit
293 the same branch name. You can change the name of a branch at any
294 time, using the \hgcmd{branch} command.
295 \interaction{branch-named.rebranch}
296 In practice, this is something you won't do very often, as branch
297 names tend to have fairly long lifetimes. (This isn't a rule, just an
298 observation.)
300 \section{Dealing with multiple named branches in a repository}
302 If you have more than one named branch in a repository, Mercurial will
303 remember the branch that your working directory on when you start a
304 command like \hgcmd{update} or \hgcmdargs{pull}{-u}. It will update
305 the working directory to the tip of this branch, no matter what the
306 ``repo-wide'' tip is. To update to a revision that's on a different
307 named branch, you may need to use the \hgopt{update}{-C} option to
308 \hgcmd{update}.
310 This behaviour is a little subtle, so let's see it in action. First,
311 let's remind ourselves what branch we're currently on, and what
312 branches are in our repository.
313 \interaction{branch-named.parents}
314 We're on the \texttt{bar} branch, but there also exists an older
315 \hgcmd{foo} branch.
317 We can \hgcmd{update} back and forth between the tips of the
318 \texttt{foo} and \texttt{bar} branches without needing to use the
319 \hgopt{update}{-C} option, because this only involves going backwards
320 and forwards linearly through our change history.
321 \interaction{branch-named.update-switchy}
323 If we go back to the \texttt{foo} branch and then run \hgcmd{update},
324 it will keep us on \texttt{foo}, not move us to the tip of
325 \texttt{bar}.
326 \interaction{branch-named.update-nothing}
328 Committing a new change on the \texttt{foo} branch introduces a new
329 head.
330 \interaction{branch-named.foo-commit}
332 \section{Branch names and merging}
334 As you've probably noticed, merges in Mercurial are not symmetrical.
335 Let's say our repository has two heads, 17 and 23. If I
336 \hgcmd{update} to 17 and then \hgcmd{merge} with 23, Mercurial records
337 17 as the first parent of the merge, and 23 as the second. Whereas if
338 I \hgcmd{update} to 23 and then \hgcmd{merge} with 17, it records 23
339 as the first parent, and 17 as the second.
341 This affects Mercurial's choice of branch name when you merge. After
342 a merge, Mercurial will retain the branch name of the first parent
343 when you commit the result of the merge. If your first parent's
344 branch name is \texttt{foo}, and you merge with \texttt{bar}, the
345 branch name will still be \texttt{foo} after you merge.
347 It's not unusual for a repository to contain multiple heads, each with
348 the same branch name. Let's say I'm working on the \texttt{foo}
349 branch, and so are you. We commit different changes; I pull your
350 changes; I now have two heads, each claiming to be on the \texttt{foo}
351 branch. The result of a merge will be a single head on the
352 \texttt{foo} branch, as you might hope.
354 But if I'm working on the \texttt{bar} branch, and I merge work from
355 the \texttt{foo} branch, the result will remain on the \texttt{bar}
356 branch.
357 \interaction{branch-named.merge}
359 To give a more concrete example, if I'm working on the
360 \texttt{bleeding-edge} branch, and I want to bring in the latest fixes
361 from the \texttt{stable} branch, Mercurial will choose the ``right''
362 (\texttt{bleeding-edge}) branch name when I pull and merge from
363 \texttt{stable}.
365 \section{Branch naming is generally useful}
367 You shouldn't think of named branches as applicable only to situations
368 where you have multiple long-lived branches cohabiting in a single
369 repository. They're very useful even in the one-branch-per-repository
370 case.
372 In the simplest case, giving a name to each branch gives you a
373 permanent record of which branch a changeset originated on. This
374 gives you more context when you're trying to follow the history of a
375 long-lived branchy project.
377 If you're working with shared repositories, you can set up a
378 \hook{pretxnchangegroup} hook on each that will block incoming changes
379 that have the ``wrong'' branch name. This provides a simple, but
380 effective, defence against people accidentally pushing changes from a
381 ``bleeding edge'' branch to a ``stable'' branch. Such a hook might
382 look like this inside the shared repo's \hgrc.
383 \begin{codesample2}
384 [hooks]
385 pretxnchangegroup.branch = hg heads --template '{branches} ' | grep mybranch
386 \end{codesample2}
388 %%% Local Variables:
389 %%% mode: latex
390 %%% TeX-master: "00book"
391 %%% End: