hgbook
diff fr/branch.tex @ 963:1dd00abb3fa9
merge with bryan - it's been a while but everything seems ok
author | Romain PELISSE <belaran@gmail.com> |
---|---|
date | Sun Aug 16 03:41:39 2009 +0200 (2009-08-16) |
parents | c36a6f534b99 |
children |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/fr/branch.tex Sun Aug 16 03:41:39 2009 +0200 1.3 @@ -0,0 +1,392 @@ 1.4 +\chapter{Managing releases and branchy development} 1.5 +\label{chap:branch} 1.6 + 1.7 +Mercurial provides several mechanisms for you to manage a project that 1.8 +is making progress on multiple fronts at once. To understand these 1.9 +mechanisms, let's first take a brief look at a fairly normal software 1.10 +project structure. 1.11 + 1.12 +Many software projects issue periodic ``major'' releases that contain 1.13 +substantial new features. In parallel, they may issue ``minor'' 1.14 +releases. These are usually identical to the major releases off which 1.15 +they're based, but with a few bugs fixed. 1.16 + 1.17 +In this chapter, we'll start by talking about how to keep records of 1.18 +project milestones such as releases. We'll then continue on to talk 1.19 +about the flow of work between different phases of a project, and how 1.20 +Mercurial can help you to isolate and manage this work. 1.21 + 1.22 +\section{Giving a persistent name to a revision} 1.23 + 1.24 +Once you decide that you'd like to call a particular revision a 1.25 +``release'', it's a good idea to record the identity of that revision. 1.26 +This will let you reproduce that release at a later date, for whatever 1.27 +purpose you might need at the time (reproducing a bug, porting to a 1.28 +new platform, etc). 1.29 +\interaction{tag.init} 1.30 + 1.31 +Mercurial lets you give a permanent name to any revision using the 1.32 +\hgcmd{tag} command. Not surprisingly, these names are called 1.33 +``tags''. 1.34 +\interaction{tag.tag} 1.35 + 1.36 +A tag is nothing more than a ``symbolic name'' for a revision. Tags 1.37 +exist purely for your convenience, so that you have a handy permanent 1.38 +way to refer to a revision; Mercurial doesn't interpret the tag names 1.39 +you use in any way. Neither does Mercurial place any restrictions on 1.40 +the name of a tag, beyond a few that are necessary to ensure that a 1.41 +tag can be parsed unambiguously. A tag name cannot contain any of the 1.42 +following characters: 1.43 +\begin{itemize} 1.44 +\item Colon (ASCII 58, ``\texttt{:}'') 1.45 +\item Carriage return (ASCII 13, ``\Verb+\r+'') 1.46 +\item Newline (ASCII 10, ``\Verb+\n+'') 1.47 +\end{itemize} 1.48 + 1.49 +You can use the \hgcmd{tags} command to display the tags present in 1.50 +your repository. In the output, each tagged revision is identified 1.51 +first by its name, then by revision number, and finally by the unique 1.52 +hash of the revision. 1.53 +\interaction{tag.tags} 1.54 +Notice that \texttt{tip} is listed in the output of \hgcmd{tags}. The 1.55 +\texttt{tip} tag is a special ``floating'' tag, which always 1.56 +identifies the newest revision in the repository. 1.57 + 1.58 +In the output of the \hgcmd{tags} command, tags are listed in reverse 1.59 +order, by revision number. This usually means that recent tags are 1.60 +listed before older tags. It also means that \texttt{tip} is always 1.61 +going to be the first tag listed in the output of \hgcmd{tags}. 1.62 + 1.63 +When you run \hgcmd{log}, if it displays a revision that has tags 1.64 +associated with it, it will print those tags. 1.65 +\interaction{tag.log} 1.66 + 1.67 +Any time you need to provide a revision~ID to a Mercurial command, the 1.68 +command will accept a tag name in its place. Internally, Mercurial 1.69 +will translate your tag name into the corresponding revision~ID, then 1.70 +use that. 1.71 +\interaction{tag.log.v1.0} 1.72 + 1.73 +There's no limit on the number of tags you can have in a repository, 1.74 +or on the number of tags that a single revision can have. As a 1.75 +practical matter, it's not a great idea to have ``too many'' (a number 1.76 +which will vary from project to project), simply because tags are 1.77 +supposed to help you to find revisions. If you have lots of tags, the 1.78 +ease of using them to identify revisions diminishes rapidly. 1.79 + 1.80 +For example, if your project has milestones as frequent as every few 1.81 +days, it's perfectly reasonable to tag each one of those. But if you 1.82 +have a continuous build system that makes sure every revision can be 1.83 +built cleanly, you'd be introducing a lot of noise if you were to tag 1.84 +every clean build. Instead, you could tag failed builds (on the 1.85 +assumption that they're rare!), or simply not use tags to track 1.86 +buildability. 1.87 + 1.88 +If you want to remove a tag that you no longer want, use 1.89 +\hgcmdargs{tag}{--remove}. 1.90 +\interaction{tag.remove} 1.91 +You can also modify a tag at any time, so that it identifies a 1.92 +different revision, by simply issuing a new \hgcmd{tag} command. 1.93 +You'll have to use the \hgopt{tag}{-f} option to tell Mercurial that 1.94 +you \emph{really} want to update the tag. 1.95 +\interaction{tag.replace} 1.96 +There will still be a permanent record of the previous identity of the 1.97 +tag, but Mercurial will no longer use it. There's thus no penalty to 1.98 +tagging the wrong revision; all you have to do is turn around and tag 1.99 +the correct revision once you discover your error. 1.100 + 1.101 +Mercurial stores tags in a normal revision-controlled file in your 1.102 +repository. If you've created any tags, you'll find them in a file 1.103 +named \sfilename{.hgtags}. When you run the \hgcmd{tag} command, 1.104 +Mercurial modifies this file, then automatically commits the change to 1.105 +it. This means that every time you run \hgcmd{tag}, you'll see a 1.106 +corresponding changeset in the output of \hgcmd{log}. 1.107 +\interaction{tag.tip} 1.108 + 1.109 +\subsection{Handling tag conflicts during a merge} 1.110 + 1.111 +You won't often need to care about the \sfilename{.hgtags} file, but 1.112 +it sometimes makes its presence known during a merge. The format of 1.113 +the file is simple: it consists of a series of lines. Each line 1.114 +starts with a changeset hash, followed by a space, followed by the 1.115 +name of a tag. 1.116 + 1.117 +If you're resolving a conflict in the \sfilename{.hgtags} file during 1.118 +a merge, there's one twist to modifying the \sfilename{.hgtags} file: 1.119 +when Mercurial is parsing the tags in a repository, it \emph{never} 1.120 +reads the working copy of the \sfilename{.hgtags} file. Instead, it 1.121 +reads the \emph{most recently committed} revision of the file. 1.122 + 1.123 +An unfortunate consequence of this design is that you can't actually 1.124 +verify that your merged \sfilename{.hgtags} file is correct until 1.125 +\emph{after} you've committed a change. So if you find yourself 1.126 +resolving a conflict on \sfilename{.hgtags} during a merge, be sure to 1.127 +run \hgcmd{tags} after you commit. If it finds an error in the 1.128 +\sfilename{.hgtags} file, it will report the location of the error, 1.129 +which you can then fix and commit. You should then run \hgcmd{tags} 1.130 +again, just to be sure that your fix is correct. 1.131 + 1.132 +\subsection{Tags and cloning} 1.133 + 1.134 +You may have noticed that the \hgcmd{clone} command has a 1.135 +\hgopt{clone}{-r} option that lets you clone an exact copy of the 1.136 +repository as of a particular changeset. The new clone will not 1.137 +contain any project history that comes after the revision you 1.138 +specified. This has an interaction with tags that can surprise the 1.139 +unwary. 1.140 + 1.141 +Recall that a tag is stored as a revision to the \sfilename{.hgtags} 1.142 +file, so that when you create a tag, the changeset in which it's 1.143 +recorded necessarily refers to an older changeset. When you run 1.144 +\hgcmdargs{clone}{-r foo} to clone a repository as of tag 1.145 +\texttt{foo}, the new clone \emph{will not contain the history that 1.146 + created the tag} that you used to clone the repository. The result 1.147 +is that you'll get exactly the right subset of the project's history 1.148 +in the new repository, but \emph{not} the tag you might have expected. 1.149 + 1.150 +\subsection{When permanent tags are too much} 1.151 + 1.152 +Since Mercurial's tags are revision controlled and carried around with 1.153 +a project's history, everyone you work with will see the tags you 1.154 +create. But giving names to revisions has uses beyond simply noting 1.155 +that revision \texttt{4237e45506ee} is really \texttt{v2.0.2}. If 1.156 +you're trying to track down a subtle bug, you might want a tag to 1.157 +remind you of something like ``Anne saw the symptoms with this 1.158 +revision''. 1.159 + 1.160 +For cases like this, what you might want to use are \emph{local} tags. 1.161 +You can create a local tag with the \hgopt{tag}{-l} option to the 1.162 +\hgcmd{tag} command. This will store the tag in a file called 1.163 +\sfilename{.hg/localtags}. Unlike \sfilename{.hgtags}, 1.164 +\sfilename{.hg/localtags} is not revision controlled. Any tags you 1.165 +create using \hgopt{tag}{-l} remain strictly local to the repository 1.166 +you're currently working in. 1.167 + 1.168 +\section{The flow of changes---big picture vs. little} 1.169 + 1.170 +To return to the outline I sketched at the beginning of a chapter, 1.171 +let's think about a project that has multiple concurrent pieces of 1.172 +work under development at once. 1.173 + 1.174 +There might be a push for a new ``main'' release; a new minor bugfix 1.175 +release to the last main release; and an unexpected ``hot fix'' to an 1.176 +old release that is now in maintenance mode. 1.177 + 1.178 +The usual way people refer to these different concurrent directions of 1.179 +development is as ``branches''. However, we've already seen numerous 1.180 +times that Mercurial treats \emph{all of history} as a series of 1.181 +branches and merges. Really, what we have here is two ideas that are 1.182 +peripherally related, but which happen to share a name. 1.183 +\begin{itemize} 1.184 +\item ``Big picture'' branches represent the sweep of a project's 1.185 + evolution; people give them names, and talk about them in 1.186 + conversation. 1.187 +\item ``Little picture'' branches are artefacts of the day-to-day 1.188 + activity of developing and merging changes. They expose the 1.189 + narrative of how the code was developed. 1.190 +\end{itemize} 1.191 + 1.192 +\section{Managing big-picture branches in repositories} 1.193 + 1.194 +The easiest way to isolate a ``big picture'' branch in Mercurial is in 1.195 +a dedicated repository. If you have an existing shared 1.196 +repository---let's call it \texttt{myproject}---that reaches a ``1.0'' 1.197 +milestone, you can start to prepare for future maintenance releases on 1.198 +top of version~1.0 by tagging the revision from which you prepared 1.199 +the~1.0 release. 1.200 +\interaction{branch-repo.tag} 1.201 +You can then clone a new shared \texttt{myproject-1.0.1} repository as 1.202 +of that tag. 1.203 +\interaction{branch-repo.clone} 1.204 + 1.205 +Afterwards, if someone needs to work on a bug fix that ought to go 1.206 +into an upcoming~1.0.1 minor release, they clone the 1.207 +\texttt{myproject-1.0.1} repository, make their changes, and push them 1.208 +back. 1.209 +\interaction{branch-repo.bugfix} 1.210 +Meanwhile, development for the next major release can continue, 1.211 +isolated and unabated, in the \texttt{myproject} repository. 1.212 +\interaction{branch-repo.new} 1.213 + 1.214 +\section{Don't repeat yourself: merging across branches} 1.215 + 1.216 +In many cases, if you have a bug to fix on a maintenance branch, the 1.217 +chances are good that the bug exists on your project's main branch 1.218 +(and possibly other maintenance branches, too). It's a rare developer 1.219 +who wants to fix the same bug multiple times, so let's look at a few 1.220 +ways that Mercurial can help you to manage these bugfixes without 1.221 +duplicating your work. 1.222 + 1.223 +In the simplest instance, all you need to do is pull changes from your 1.224 +maintenance branch into your local clone of the target branch. 1.225 +\interaction{branch-repo.pull} 1.226 +You'll then need to merge the heads of the two branches, and push back 1.227 +to the main branch. 1.228 +\interaction{branch-repo.merge} 1.229 + 1.230 +\section{Naming branches within one repository} 1.231 + 1.232 +In most instances, isolating branches in repositories is the right 1.233 +approach. Its simplicity makes it easy to understand; and so it's 1.234 +hard to make mistakes. There's a one-to-one relationship between 1.235 +branches you're working in and directories on your system. This lets 1.236 +you use normal (non-Mercurial-aware) tools to work on files within a 1.237 +branch/repository. 1.238 + 1.239 +If you're more in the ``power user'' category (\emph{and} your 1.240 +collaborators are too), there is an alternative way of handling 1.241 +branches that you can consider. I've already mentioned the 1.242 +human-level distinction between ``small picture'' and ``big picture'' 1.243 +branches. While Mercurial works with multiple ``small picture'' 1.244 +branches in a repository all the time (for example after you pull 1.245 +changes in, but before you merge them), it can \emph{also} work with 1.246 +multiple ``big picture'' branches. 1.247 + 1.248 +The key to working this way is that Mercurial lets you assign a 1.249 +persistent \emph{name} to a branch. There always exists a branch 1.250 +named \texttt{default}. Even before you start naming branches 1.251 +yourself, you can find traces of the \texttt{default} branch if you 1.252 +look for them. 1.253 + 1.254 +As an example, when you run the \hgcmd{commit} command, and it pops up 1.255 +your editor so that you can enter a commit message, look for a line 1.256 +that contains the text ``\texttt{HG: branch default}'' at the bottom. 1.257 +This is telling you that your commit will occur on the branch named 1.258 +\texttt{default}. 1.259 + 1.260 +To start working with named branches, use the \hgcmd{branches} 1.261 +command. This command lists the named branches already present in 1.262 +your repository, telling you which changeset is the tip of each. 1.263 +\interaction{branch-named.branches} 1.264 +Since you haven't created any named branches yet, the only one that 1.265 +exists is \texttt{default}. 1.266 + 1.267 +To find out what the ``current'' branch is, run the \hgcmd{branch} 1.268 +command, giving it no arguments. This tells you what branch the 1.269 +parent of the current changeset is on. 1.270 +\interaction{branch-named.branch} 1.271 + 1.272 +To create a new branch, run the \hgcmd{branch} command again. This 1.273 +time, give it one argument: the name of the branch you want to create. 1.274 +\interaction{branch-named.create} 1.275 + 1.276 +After you've created a branch, you might wonder what effect the 1.277 +\hgcmd{branch} command has had. What do the \hgcmd{status} and 1.278 +\hgcmd{tip} commands report? 1.279 +\interaction{branch-named.status} 1.280 +Nothing has changed in the working directory, and there's been no new 1.281 +history created. As this suggests, running the \hgcmd{branch} command 1.282 +has no permanent effect; it only tells Mercurial what branch name to 1.283 +use the \emph{next} time you commit a changeset. 1.284 + 1.285 +When you commit a change, Mercurial records the name of the branch on 1.286 +which you committed. Once you've switched from the \texttt{default} 1.287 +branch to another and committed, you'll see the name of the new branch 1.288 +show up in the output of \hgcmd{log}, \hgcmd{tip}, and other commands 1.289 +that display the same kind of output. 1.290 +\interaction{branch-named.commit} 1.291 +The \hgcmd{log}-like commands will print the branch name of every 1.292 +changeset that's not on the \texttt{default} branch. As a result, if 1.293 +you never use named branches, you'll never see this information. 1.294 + 1.295 +Once you've named a branch and committed a change with that name, 1.296 +every subsequent commit that descends from that change will inherit 1.297 +the same branch name. You can change the name of a branch at any 1.298 +time, using the \hgcmd{branch} command. 1.299 +\interaction{branch-named.rebranch} 1.300 +In practice, this is something you won't do very often, as branch 1.301 +names tend to have fairly long lifetimes. (This isn't a rule, just an 1.302 +observation.) 1.303 + 1.304 +\section{Dealing with multiple named branches in a repository} 1.305 + 1.306 +If you have more than one named branch in a repository, Mercurial will 1.307 +remember the branch that your working directory on when you start a 1.308 +command like \hgcmd{update} or \hgcmdargs{pull}{-u}. It will update 1.309 +the working directory to the tip of this branch, no matter what the 1.310 +``repo-wide'' tip is. To update to a revision that's on a different 1.311 +named branch, you may need to use the \hgopt{update}{-C} option to 1.312 +\hgcmd{update}. 1.313 + 1.314 +This behaviour is a little subtle, so let's see it in action. First, 1.315 +let's remind ourselves what branch we're currently on, and what 1.316 +branches are in our repository. 1.317 +\interaction{branch-named.parents} 1.318 +We're on the \texttt{bar} branch, but there also exists an older 1.319 +\hgcmd{foo} branch. 1.320 + 1.321 +We can \hgcmd{update} back and forth between the tips of the 1.322 +\texttt{foo} and \texttt{bar} branches without needing to use the 1.323 +\hgopt{update}{-C} option, because this only involves going backwards 1.324 +and forwards linearly through our change history. 1.325 +\interaction{branch-named.update-switchy} 1.326 + 1.327 +If we go back to the \texttt{foo} branch and then run \hgcmd{update}, 1.328 +it will keep us on \texttt{foo}, not move us to the tip of 1.329 +\texttt{bar}. 1.330 +\interaction{branch-named.update-nothing} 1.331 + 1.332 +Committing a new change on the \texttt{foo} branch introduces a new 1.333 +head. 1.334 +\interaction{branch-named.foo-commit} 1.335 + 1.336 +\section{Branch names and merging} 1.337 + 1.338 +As you've probably noticed, merges in Mercurial are not symmetrical. 1.339 +Let's say our repository has two heads, 17 and 23. If I 1.340 +\hgcmd{update} to 17 and then \hgcmd{merge} with 23, Mercurial records 1.341 +17 as the first parent of the merge, and 23 as the second. Whereas if 1.342 +I \hgcmd{update} to 23 and then \hgcmd{merge} with 17, it records 23 1.343 +as the first parent, and 17 as the second. 1.344 + 1.345 +This affects Mercurial's choice of branch name when you merge. After 1.346 +a merge, Mercurial will retain the branch name of the first parent 1.347 +when you commit the result of the merge. If your first parent's 1.348 +branch name is \texttt{foo}, and you merge with \texttt{bar}, the 1.349 +branch name will still be \texttt{foo} after you merge. 1.350 + 1.351 +It's not unusual for a repository to contain multiple heads, each with 1.352 +the same branch name. Let's say I'm working on the \texttt{foo} 1.353 +branch, and so are you. We commit different changes; I pull your 1.354 +changes; I now have two heads, each claiming to be on the \texttt{foo} 1.355 +branch. The result of a merge will be a single head on the 1.356 +\texttt{foo} branch, as you might hope. 1.357 + 1.358 +But if I'm working on the \texttt{bar} branch, and I merge work from 1.359 +the \texttt{foo} branch, the result will remain on the \texttt{bar} 1.360 +branch. 1.361 +\interaction{branch-named.merge} 1.362 + 1.363 +To give a more concrete example, if I'm working on the 1.364 +\texttt{bleeding-edge} branch, and I want to bring in the latest fixes 1.365 +from the \texttt{stable} branch, Mercurial will choose the ``right'' 1.366 +(\texttt{bleeding-edge}) branch name when I pull and merge from 1.367 +\texttt{stable}. 1.368 + 1.369 +\section{Branch naming is generally useful} 1.370 + 1.371 +You shouldn't think of named branches as applicable only to situations 1.372 +where you have multiple long-lived branches cohabiting in a single 1.373 +repository. They're very useful even in the one-branch-per-repository 1.374 +case. 1.375 + 1.376 +In the simplest case, giving a name to each branch gives you a 1.377 +permanent record of which branch a changeset originated on. This 1.378 +gives you more context when you're trying to follow the history of a 1.379 +long-lived branchy project. 1.380 + 1.381 +If you're working with shared repositories, you can set up a 1.382 +\hook{pretxnchangegroup} hook on each that will block incoming changes 1.383 +that have the ``wrong'' branch name. This provides a simple, but 1.384 +effective, defence against people accidentally pushing changes from a 1.385 +``bleeding edge'' branch to a ``stable'' branch. Such a hook might 1.386 +look like this inside the shared repo's \hgrc. 1.387 +\begin{codesample2} 1.388 + [hooks] 1.389 + pretxnchangegroup.branch = hg heads --template '{branches} ' | grep mybranch 1.390 +\end{codesample2} 1.391 + 1.392 +%%% Local Variables: 1.393 +%%% mode: latex 1.394 +%%% TeX-master: "00book" 1.395 +%%% End: