hgbook
diff en/ch02-tour-merge.xml @ 654:1c13ed2130a7
Merge with http://hg.serpentine.com/mercurial/book
author | Dongsheng Song <dongsheng.song@gmail.com> |
---|---|
date | Mon Mar 30 16:23:33 2009 +0800 (2009-03-30) |
parents | 7e7c47481e4f 0b45854f0b7b |
children | 65e9a18d2c7e |
line diff
1.1 --- a/en/ch02-tour-merge.xml Fri Mar 20 16:43:35 2009 +0800 1.2 +++ b/en/ch02-tour-merge.xml Mon Mar 30 16:23:33 2009 +0800 1.3 @@ -1,10 +1,10 @@ 1.4 <!-- vim: set filetype=docbkxml shiftwidth=2 autoindent expandtab tw=77 : --> 1.5 1.6 -<chapter id="chap.tour-merge"> 1.7 +<chapter id="chap:tour-merge"> 1.8 <?dbhtml filename="a-tour-of-mercurial-merging-work.html"?> 1.9 <title>A tour of Mercurial: merging work</title> 1.10 1.11 - <para>We've now covered cloning a repository, making changes in a 1.12 + <para id="x_338">We've now covered cloning a repository, making changes in a 1.13 repository, and pulling or pushing changes from one repository 1.14 into another. Our next step is <emphasis>merging</emphasis> 1.15 changes from separate repositories.</para> 1.16 @@ -12,86 +12,85 @@ 1.17 <sect1> 1.18 <title>Merging streams of work</title> 1.19 1.20 - <para>Merging is a fundamental part of working with a distributed 1.21 + <para id="x_339">Merging is a fundamental part of working with a distributed 1.22 revision control tool.</para> 1.23 <itemizedlist> 1.24 - <listitem><para>Alice and Bob each have a personal copy of a 1.25 + <listitem><para id="x_33a">Alice and Bob each have a personal copy of a 1.26 repository for a project they're collaborating on. Alice 1.27 fixes a bug in her repository; Bob adds a new feature in 1.28 his. They want the shared repository to contain both the 1.29 bug fix and the new feature.</para> 1.30 </listitem> 1.31 - <listitem><para>I frequently work on several different tasks for 1.32 + <listitem><para id="x_33b">I frequently work on several different tasks for 1.33 a single project at once, each safely isolated in its own 1.34 repository. Working this way means that I often need to 1.35 merge one piece of my own work with another.</para> 1.36 </listitem></itemizedlist> 1.37 1.38 - <para>Because merging is such a common thing to need to do, 1.39 + <para id="x_33c">Because merging is such a common thing to need to do, 1.40 Mercurial makes it easy. Let's walk through the process. We'll 1.41 begin by cloning yet another repository (see how often they 1.42 spring up?) and making a change in it.</para> 1.43 1.44 &interaction.tour.merge.clone; 1.45 1.46 - <para>We should now have two copies of 1.47 + <para id="x_33d">We should now have two copies of 1.48 <filename>hello.c</filename> with different contents. The 1.49 histories of the two repositories have also diverged, as 1.50 - illustrated in figure <xref endterm="fig.tour-merge.sep-repos.caption" 1.51 - linkend="fig.tour-merge.sep-repos"/>.</para> 1.52 + illustrated in <xref 1.53 + linkend="fig:tour-merge:sep-repos"/>.</para> 1.54 1.55 &interaction.tour.merge.cat; 1.56 1.57 - <informalfigure id="fig.tour-merge.sep-repos"> 1.58 + <figure id="fig:tour-merge:sep-repos"> 1.59 + <title>Divergent recent histories of the <filename 1.60 + class="directory">my-hello</filename> and <filename 1.61 + class="directory">my-new-hello</filename> 1.62 + repositories</title> 1.63 <mediaobject> 1.64 - <imageobject><imagedata fileref="images/tour-merge-sep-repos.png"/></imageobject> 1.65 + <imageobject><imagedata fileref="figs/tour-merge-sep-repos.png"/></imageobject> 1.66 <textobject><phrase>XXX add text</phrase></textobject> 1.67 - <caption><para id="fig.tour-merge.sep-repos.caption">Divergent recent 1.68 - histories of the <filename 1.69 - class="directory">my-hello</filename> and <filename 1.70 - class="directory">my-new-hello</filename> 1.71 - repositories</para></caption> 1.72 </mediaobject> 1.73 - </informalfigure> 1.74 - 1.75 - <para>We already know that pulling changes from our <filename 1.76 + </figure> 1.77 + 1.78 + <para id="x_33f">We already know that pulling changes from our <filename 1.79 class="directory">my-hello</filename> repository will have no 1.80 effect on the working directory.</para> 1.81 1.82 &interaction.tour.merge.pull; 1.83 1.84 - <para>However, the <command role="hg-cmd">hg pull</command> 1.85 + <para id="x_340">However, the <command role="hg-cmd">hg pull</command> 1.86 command says something about <quote>heads</quote>.</para> 1.87 1.88 <sect2> 1.89 <title>Head changesets</title> 1.90 1.91 - <para>A head is a change that has no descendants, or children, 1.92 + <para id="x_341">A head is a change that has no descendants, or children, 1.93 as they're also known. The tip revision is thus a head, 1.94 because the newest revision in a repository doesn't have any 1.95 children, but a repository can contain more than one 1.96 head.</para> 1.97 1.98 - <informalfigure id="fig.tour-merge.pull"> 1.99 + <figure id="fig:tour-merge:pull"> 1.100 + <title>Repository contents after pulling from <filename 1.101 + class="directory">my-hello</filename> into <filename 1.102 + class="directory">my-new-hello</filename></title> 1.103 <mediaobject> 1.104 - <imageobject><imagedata fileref="images/tour-merge-pull.png"/></imageobject> 1.105 + <imageobject> 1.106 + <imagedata fileref="figs/tour-merge-pull.png"/> 1.107 + </imageobject> 1.108 <textobject><phrase>XXX add text</phrase></textobject> 1.109 - <caption><para id="fig.tour-merge.pull.caption">Repository contents after 1.110 - pulling from <filename class="directory">my-hello</filename> into 1.111 - <filename class="directory">my-new-hello</filename></para></caption> 1.112 </mediaobject> 1.113 - </informalfigure> 1.114 - 1.115 - <para>In figure <xref endterm="fig.tour-merge.pull.caption" 1.116 - linkend="fig.tour-merge.pull"/>, you can 1.117 + </figure> 1.118 + 1.119 + <para id="x_343">In <xref linkend="fig:tour-merge:pull"/>, you can 1.120 see the effect of the pull from <filename 1.121 class="directory">my-hello</filename> into <filename 1.122 class="directory">my-new-hello</filename>. The history that 1.123 was already present in <filename 1.124 class="directory">my-new-hello</filename> is untouched, but 1.125 - a new revision has been added. By referring to figure <xref 1.126 - endterm="fig.tour-merge.sep-repos.caption" 1.127 - linkend="fig.tour-merge.sep-repos"/>, we can see that the 1.128 + a new revision has been added. By referring to <xref 1.129 + linkend="fig:tour-merge:sep-repos"/>, we can see that the 1.130 <emphasis>changeset ID</emphasis> remains the same in the new 1.131 repository, but the <emphasis>revision number</emphasis> has 1.132 changed. (This, incidentally, is a fine example of why it's 1.133 @@ -105,13 +104,13 @@ 1.134 <sect2> 1.135 <title>Performing the merge</title> 1.136 1.137 - <para>What happens if we try to use the normal <command 1.138 + <para id="x_344">What happens if we try to use the normal <command 1.139 role="hg-cmd">hg update</command> command to update to the 1.140 new tip?</para> 1.141 1.142 &interaction.tour.merge.update; 1.143 1.144 - <para>Mercurial is telling us that the <command role="hg-cmd">hg 1.145 + <para id="x_345">Mercurial is telling us that the <command role="hg-cmd">hg 1.146 update</command> command won't do a merge; it won't update 1.147 the working directory when it thinks we might be wanting to do 1.148 a merge, unless we force it to do so. Instead, we use the 1.149 @@ -120,16 +119,18 @@ 1.150 1.151 &interaction.tour.merge.merge; 1.152 1.153 - <informalfigure id="fig.tour-merge.merge"> 1.154 + <figure id="fig:tour-merge:merge"> 1.155 + <title>Working directory and repository during merge, and 1.156 + following commit</title> 1.157 <mediaobject> 1.158 - <imageobject><imagedata fileref="images/tour-merge-merge.png"/></imageobject> 1.159 + <imageobject> 1.160 + <imagedata fileref="figs/tour-merge-merge.png"/> 1.161 + </imageobject> 1.162 <textobject><phrase>XXX add text</phrase></textobject> 1.163 - <caption><para id="fig.tour-merge.merge.caption">Working directory and 1.164 - repository during merge, and following commit</para></caption> 1.165 </mediaobject> 1.166 - </informalfigure> 1.167 - 1.168 - <para>This updates the working directory so that it contains 1.169 + </figure> 1.170 + 1.171 + <para id="x_347">This updates the working directory so that it contains 1.172 changes from <emphasis>both</emphasis> heads, which is 1.173 reflected in both the output of <command role="hg-cmd">hg 1.174 parents</command> and the contents of 1.175 @@ -141,22 +142,22 @@ 1.176 <sect2> 1.177 <title>Committing the results of the merge</title> 1.178 1.179 - <para>Whenever we've done a merge, <command role="hg-cmd">hg 1.180 + <para id="x_348">Whenever we've done a merge, <command role="hg-cmd">hg 1.181 parents</command> will display two parents until we <command 1.182 role="hg-cmd">hg commit</command> the results of the 1.183 merge.</para> 1.184 1.185 &interaction.tour.merge.commit; 1.186 1.187 - <para>We now have a new tip revision; notice that it has 1.188 + <para id="x_349">We now have a new tip revision; notice that it has 1.189 <emphasis>both</emphasis> of our former heads as its parents. 1.190 These are the same revisions that were previously displayed by 1.191 <command role="hg-cmd">hg parents</command>.</para> 1.192 1.193 &interaction.tour.merge.tip; 1.194 1.195 - <para>In figure <xref endterm="fig.tour-merge.merge.caption" 1.196 - linkend="fig.tour-merge.merge"/>, you can see a 1.197 + <para id="x_34a">In <xref 1.198 + linkend="fig:tour-merge:merge"/>, you can see a 1.199 representation of what happens to the working directory during 1.200 the merge, and how this affects the repository when the commit 1.201 happens. During the merge, the working directory has two 1.202 @@ -168,32 +169,29 @@ 1.203 <sect1> 1.204 <title>Merging conflicting changes</title> 1.205 1.206 - <para>Most merges are simple affairs, but sometimes you'll find 1.207 + <para id="x_34b">Most merges are simple affairs, but sometimes you'll find 1.208 yourself merging changes where each modifies the same portions 1.209 of the same files. Unless both modifications are identical, 1.210 this results in a <emphasis>conflict</emphasis>, where you have 1.211 to decide how to reconcile the different changes into something 1.212 coherent.</para> 1.213 1.214 - <informalfigure id="fig.tour-merge.conflict"> 1.215 + <figure id="fig:tour-merge:conflict"> 1.216 + <title>Conflicting changes to a document</title> 1.217 <mediaobject> 1.218 - <imageobject><imagedata fileref="images/tour-merge-conflict.png"/> 1.219 - </imageobject> 1.220 - <textobject><phrase>XXX add text</phrase></textobject> 1.221 - <caption><para id="fig.tour-merge.conflict.caption">Conflicting 1.222 - changes to a document</para></caption> 1.223 + <imageobject><imagedata fileref="figs/tour-merge-conflict.png"/></imageobject> 1.224 + <textobject><phrase>XXX add text</phrase></textobject> 1.225 </mediaobject> 1.226 - </informalfigure> 1.227 - 1.228 - <para>Figure <xref endterm="fig.tour-merge.conflict.caption" 1.229 - linkend="fig.tour-merge.conflict"/> illustrates 1.230 + </figure> 1.231 + 1.232 + <para id="x_34d"><xref linkend="fig:tour-merge:conflict"/> illustrates 1.233 an instance of two conflicting changes to a document. We 1.234 started with a single version of the file; then we made some 1.235 changes; while someone else made different changes to the same 1.236 text. Our task in resolving the conflicting changes is to 1.237 decide what the file should look like.</para> 1.238 1.239 - <para>Mercurial doesn't have a built-in facility for handling 1.240 + <para id="x_34e">Mercurial doesn't have a built-in facility for handling 1.241 conflicts. Instead, it runs an external program called 1.242 <command>hgmerge</command>. This is a shell script that is 1.243 bundled with Mercurial; you can change it to behave however you 1.244 @@ -204,7 +202,7 @@ 1.245 human guidance) or aren't present, the script tries a few 1.246 different graphical merging tools.</para> 1.247 1.248 - <para>It's also possible to get Mercurial to run another program 1.249 + <para id="x_34f">It's also possible to get Mercurial to run another program 1.250 or script instead of <command>hgmerge</command>, by setting the 1.251 <envar>HGMERGE</envar> environment variable to the name of your 1.252 preferred program.</para> 1.253 @@ -212,59 +210,59 @@ 1.254 <sect2> 1.255 <title>Using a graphical merge tool</title> 1.256 1.257 - <para>My preferred graphical merge tool is 1.258 + <para id="x_350">My preferred graphical merge tool is 1.259 <command>kdiff3</command>, which I'll use to describe the 1.260 features that are common to graphical file merging tools. You 1.261 can see a screenshot of <command>kdiff3</command> in action in 1.262 - figure <xref endterm="fig.tour-merge.kdiff3.caption" 1.263 - linkend="fig.tour-merge.kdiff3"/>. The kind of 1.264 + <xref linkend="fig:tour-merge:kdiff3"/>. The kind of 1.265 merge it is performing is called a <emphasis>three-way 1.266 merge</emphasis>, because there are three different versions 1.267 of the file of interest to us. The tool thus splits the upper 1.268 portion of the window into three panes:</para> 1.269 <itemizedlist> 1.270 - <listitem><para>At the left is the <emphasis>base</emphasis> 1.271 + <listitem><para id="x_351">At the left is the <emphasis>base</emphasis> 1.272 version of the file, i.e. the most recent version from 1.273 which the two versions we're trying to merge are 1.274 descended.</para> 1.275 </listitem> 1.276 - <listitem><para>In the middle is <quote>our</quote> version of 1.277 + <listitem><para id="x_352">In the middle is <quote>our</quote> version of 1.278 the file, with the contents that we modified.</para> 1.279 </listitem> 1.280 - <listitem><para>On the right is <quote>their</quote> version 1.281 + <listitem><para id="x_353">On the right is <quote>their</quote> version 1.282 of the file, the one that from the changeset that we're 1.283 trying to merge with.</para> 1.284 </listitem></itemizedlist> 1.285 - <para>In the pane below these is the current 1.286 + <para id="x_354">In the pane below these is the current 1.287 <emphasis>result</emphasis> of the merge. Our task is to 1.288 replace all of the red text, which indicates unresolved 1.289 conflicts, with some sensible merger of the 1.290 <quote>ours</quote> and <quote>theirs</quote> versions of the 1.291 file.</para> 1.292 1.293 - <para>All four of these panes are <emphasis>locked 1.294 + <para id="x_355">All four of these panes are <emphasis>locked 1.295 together</emphasis>; if we scroll vertically or horizontally 1.296 in any of them, the others are updated to display the 1.297 corresponding sections of their respective files.</para> 1.298 1.299 - <informalfigure id="fig.tour-merge.kdiff3"> 1.300 - <mediaobject> 1.301 - <imageobject><imagedata width="100%" fileref="images/kdiff3.png"/> 1.302 - </imageobject> 1.303 - <textobject><phrase>XXX add text</phrase></textobject> 1.304 - <caption><para id="fig.tour-merge.kdiff3.caption">Using 1.305 - <command>kdiff3</command> to merge versions of a file</para> 1.306 - </caption> 1.307 - </mediaobject> 1.308 - </informalfigure> 1.309 - 1.310 - <para>For each conflicting portion of the file, we can choose to 1.311 + <figure id="fig:tour-merge:kdiff3"> 1.312 + <title>Using <command>kdiff3</command> to merge versions of a 1.313 + file</title> 1.314 + <mediaobject> 1.315 + <imageobject> 1.316 + <imagedata imagedata width="100%" fileref="figs/kdiff3.png"/></imageobject> 1.317 + <textobject> 1.318 + <phrase>XXX add text</phrase> 1.319 + </textobject> 1.320 + </mediaobject> 1.321 + </figure> 1.322 + 1.323 + <para id="x_357">For each conflicting portion of the file, we can choose to 1.324 resolve the conflict using some combination of text from the 1.325 base version, ours, or theirs. We can also manually edit the 1.326 merged file at any time, in case we need to make further 1.327 modifications.</para> 1.328 1.329 - <para>There are <emphasis>many</emphasis> file merging tools 1.330 + <para id="x_358">There are <emphasis>many</emphasis> file merging tools 1.331 available, too many to cover here. They vary in which 1.332 platforms they are available for, and in their particular 1.333 strengths and weaknesses. Most are tuned for merging files 1.334 @@ -275,20 +273,19 @@ 1.335 <sect2> 1.336 <title>A worked example</title> 1.337 1.338 - <para>In this example, we will reproduce the file modification 1.339 - history of figure <xref endterm="fig.tour-merge.conflict.caption" 1.340 - linkend="fig.tour-merge.conflict"/> 1.341 + <para id="x_359">In this example, we will reproduce the file modification 1.342 + history of <xref linkend="fig:tour-merge:conflict"/> 1.343 above. Let's begin by creating a repository with a base 1.344 version of our document.</para> 1.345 1.346 &interaction.tour-merge-conflict.wife; 1.347 1.348 - <para>We'll clone the repository and make a change to the 1.349 + <para id="x_35a">We'll clone the repository and make a change to the 1.350 file.</para> 1.351 1.352 &interaction.tour-merge-conflict.cousin; 1.353 1.354 - <para>And another clone, to simulate someone else making a 1.355 + <para id="x_35b">And another clone, to simulate someone else making a 1.356 change to the file. (This hints at the idea that it's not all 1.357 that unusual to merge with yourself when you isolate tasks in 1.358 separate repositories, and indeed to find and resolve 1.359 @@ -296,13 +293,13 @@ 1.360 1.361 &interaction.tour-merge-conflict.son; 1.362 1.363 - <para>Having created two 1.364 + <para id="x_35c">Having created two 1.365 different versions of the file, we'll set up an environment 1.366 suitable for running our merge.</para> 1.367 1.368 &interaction.tour-merge-conflict.pull; 1.369 1.370 - <para>In this example, I won't use Mercurial's normal 1.371 + <para id="x_35d">In this example, I won't use Mercurial's normal 1.372 <command>hgmerge</command> program to do the merge, because it 1.373 would drop my nice automated example-running tool into a 1.374 graphical user interface. Instead, I'll set 1.375 @@ -312,25 +309,25 @@ 1.376 example on your computer, don't bother setting 1.377 <envar>HGMERGE</envar>.</para> 1.378 1.379 - <para><emphasis role="bold">XXX FIX THIS 1.380 + <para id="x_35e"><emphasis role="bold">XXX FIX THIS 1.381 EXAMPLE.</emphasis></para> 1.382 1.383 &interaction.tour-merge-conflict.merge; 1.384 1.385 - <para>Because <command>merge</command> can't resolve the 1.386 + <para id="x_35f">Because <command>merge</command> can't resolve the 1.387 conflicting changes, it leaves <emphasis>merge 1.388 markers</emphasis> inside the file that has conflicts, 1.389 indicating which lines have conflicts, and whether they came 1.390 from our version of the file or theirs.</para> 1.391 1.392 - <para>Mercurial can tell from the way <command>merge</command> 1.393 + <para id="x_360">Mercurial can tell from the way <command>merge</command> 1.394 exits that it wasn't able to merge successfully, so it tells 1.395 us what commands we'll need to run if we want to redo the 1.396 merging operation. This could be useful if, for example, we 1.397 were running a graphical merge tool and quit because we were 1.398 confused or realised we had made a mistake.</para> 1.399 1.400 - <para>If automatic or manual merges fail, there's nothing to 1.401 + <para id="x_361">If automatic or manual merges fail, there's nothing to 1.402 prevent us from <quote>fixing up</quote> the affected files 1.403 ourselves, and committing the results of our merge:</para> 1.404 1.405 @@ -338,32 +335,32 @@ 1.406 1.407 </sect2> 1.408 </sect1> 1.409 - <sect1 id="sec.tour-merge.fetch"> 1.410 + <sect1 id="sec:tour-merge:fetch"> 1.411 <title>Simplifying the pull-merge-commit sequence</title> 1.412 1.413 - <para>The process of merging changes as outlined above is 1.414 + <para id="x_362">The process of merging changes as outlined above is 1.415 straightforward, but requires running three commands in 1.416 sequence.</para> 1.417 <programlisting>hg pull 1.418 hg merge 1.419 hg commit -m 'Merged remote changes'</programlisting> 1.420 - <para>In the case of the final commit, you also need to enter a 1.421 + <para id="x_363">In the case of the final commit, you also need to enter a 1.422 commit message, which is almost always going to be a piece of 1.423 uninteresting <quote>boilerplate</quote> text.</para> 1.424 1.425 - <para>It would be nice to reduce the number of steps needed, if 1.426 + <para id="x_364">It would be nice to reduce the number of steps needed, if 1.427 this were possible. Indeed, Mercurial is distributed with an 1.428 extension called <literal role="hg-ext">fetch</literal> that 1.429 does just this.</para> 1.430 1.431 - <para>Mercurial provides a flexible extension mechanism that lets 1.432 + <para id="x_365">Mercurial provides a flexible extension mechanism that lets 1.433 people extend its functionality, while keeping the core of 1.434 Mercurial small and easy to deal with. Some extensions add new 1.435 commands that you can use from the command line, while others 1.436 work <quote>behind the scenes,</quote> for example adding 1.437 capabilities to the server.</para> 1.438 1.439 - <para>The <literal role="hg-ext">fetch</literal> extension adds a 1.440 + <para id="x_366">The <literal role="hg-ext">fetch</literal> extension adds a 1.441 new command called, not surprisingly, <command role="hg-cmd">hg 1.442 fetch</command>. This extension acts as a combination of 1.443 <command role="hg-cmd">hg pull</command>, <command 1.444 @@ -376,7 +373,7 @@ 1.445 added, it updates the working directory to the new tip 1.446 changeset.</para> 1.447 1.448 - <para>Enabling the <literal role="hg-ext">fetch</literal> 1.449 + <para id="x_367">Enabling the <literal role="hg-ext">fetch</literal> 1.450 extension is easy. Edit your <filename 1.451 role="special">.hgrc</filename>, and either go to the <literal 1.452 role="rc-extensions">extensions</literal> section or create an 1.453 @@ -385,7 +382,7 @@ 1.454 </literal></quote>.</para> 1.455 <programlisting>[extensions] 1.456 fetch =</programlisting> 1.457 - <para>(Normally, on the right-hand side of the 1.458 + <para id="x_368">(Normally, on the right-hand side of the 1.459 <quote><literal>=</literal></quote> would appear the location of 1.460 the extension, but since the <literal 1.461 role="hg-ext">fetch</literal> extension is in the standard