comparison ja/daily.tex @ 290:b0db5adf11c1 ja_root

fork Japanese translation.
author Yoshiki Yazawa <yaz@cc.rim.or.jp>
date Wed, 06 Feb 2008 17:43:11 +0900
parents en/daily.tex@00f69e8825c5
children 3b1291f24c0d
comparison
equal deleted inserted replaced
289:7be02466421b 290:b0db5adf11c1
1 \chapter{Mercurial in daily use}
2 \label{chap:daily}
3
4 \section{Telling Mercurial which files to track}
5
6 Mercurial does not work with files in your repository unless you tell
7 it to manage them. The \hgcmd{status} command will tell you which
8 files Mercurial doesn't know about; it uses a ``\texttt{?}'' to
9 display such files.
10
11 To tell Mercurial to track a file, use the \hgcmd{add} command. Once
12 you have added a file, the entry in the output of \hgcmd{status} for
13 that file changes from ``\texttt{?}'' to ``\texttt{A}''.
14 \interaction{daily.files.add}
15
16 After you run a \hgcmd{commit}, the files that you added before the
17 commit will no longer be listed in the output of \hgcmd{status}. The
18 reason for this is that \hgcmd{status} only tells you about
19 ``interesting'' files---those that you have modified or told Mercurial
20 to do something with---by default. If you have a repository that
21 contains thousands of files, you will rarely want to know about files
22 that Mercurial is tracking, but that have not changed. (You can still
23 get this information; we'll return to this later.)
24
25 Once you add a file, Mercurial doesn't do anything with it
26 immediately. Instead, it will take a snapshot of the file's state the
27 next time you perform a commit. It will then continue to track the
28 changes you make to the file every time you commit, until you remove
29 the file.
30
31 \subsection{Explicit versus implicit file naming}
32
33 A useful behaviour that Mercurial has is that if you pass the name of
34 a directory to a command, every Mercurial command will treat this as
35 ``I want to operate on every file in this directory and its
36 subdirectories''.
37 \interaction{daily.files.add-dir}
38 Notice in this example that Mercurial printed the names of the files
39 it added, whereas it didn't do so when we added the file named
40 \filename{a} in the earlier example.
41
42 What's going on is that in the former case, we explicitly named the
43 file to add on the command line, so the assumption that Mercurial
44 makes in such cases is that you know what you were doing, and it
45 doesn't print any output.
46
47 However, when we \emph{imply} the names of files by giving the name of
48 a directory, Mercurial takes the extra step of printing the name of
49 each file that it does something with. This makes it more clear what
50 is happening, and reduces the likelihood of a silent and nasty
51 surprise. This behaviour is common to most Mercurial commands.
52
53 \subsection{Aside: Mercurial tracks files, not directories}
54
55 Mercurial does not track directory information. Instead, it tracks
56 the path to a file. Before creating a file, it first creates any
57 missing directory components of the path. After it deletes a file, it
58 then deletes any empty directories that were in the deleted file's
59 path. This sounds like a trivial distinction, but it has one minor
60 practical consequence: it is not possible to represent a completely
61 empty directory in Mercurial.
62
63 Empty directories are rarely useful, and there are unintrusive
64 workarounds that you can use to achieve an appropriate effect. The
65 developers of Mercurial thus felt that the complexity that would be
66 required to manage empty directories was not worth the limited benefit
67 this feature would bring.
68
69 If you need an empty directory in your repository, there are a few
70 ways to achieve this. One is to create a directory, then \hgcmd{add} a
71 ``hidden'' file to that directory. On Unix-like systems, any file
72 name that begins with a period (``\texttt{.}'') is treated as hidden
73 by most commands and GUI tools. This approach is illustrated in
74 figure~\ref{ex:daily:hidden}.
75
76 \begin{figure}[ht]
77 \interaction{daily.files.hidden}
78 \caption{Simulating an empty directory using a hidden file}
79 \label{ex:daily:hidden}
80 \end{figure}
81
82 Another way to tackle a need for an empty directory is to simply
83 create one in your automated build scripts before they will need it.
84
85 \section{How to stop tracking a file}
86
87 Once you decide that a file no longer belongs in your repository, use
88 the \hgcmd{remove} command; this deletes the file, and tells Mercurial
89 to stop tracking it. A removed file is represented in the output of
90 \hgcmd{status} with a ``\texttt{R}''.
91 \interaction{daily.files.remove}
92
93 After you \hgcmd{remove} a file, Mercurial will no longer track
94 changes to that file, even if you recreate a file with the same name
95 in your working directory. If you do recreate a file with the same
96 name and want Mercurial to track the new file, simply \hgcmd{add} it.
97 Mercurial will know that the newly added file is not related to the
98 old file of the same name.
99
100 \subsection{Removing a file does not affect its history}
101
102 It is important to understand that removing a file has only two
103 effects.
104 \begin{itemize}
105 \item It removes the current version of the file from the working
106 directory.
107 \item It stops Mercurial from tracking changes to the file, from the
108 time of the next commit.
109 \end{itemize}
110 Removing a file \emph{does not} in any way alter the \emph{history} of
111 the file.
112
113 If you update the working directory to a changeset in which a file
114 that you have removed was still tracked, it will reappear in the
115 working directory, with the contents it had when you committed that
116 changeset. If you then update the working directory to a later
117 changeset, in which the file had been removed, Mercurial will once
118 again remove the file from the working directory.
119
120 \subsection{Missing files}
121
122 Mercurial considers a file that you have deleted, but not used
123 \hgcmd{remove} to delete, to be \emph{missing}. A missing file is
124 represented with ``\texttt{!}'' in the output of \hgcmd{status}.
125 Mercurial commands will not generally do anything with missing files.
126 \interaction{daily.files.missing}
127
128 If your repository contains a file that \hgcmd{status} reports as
129 missing, and you want the file to stay gone, you can run
130 \hgcmdargs{remove}{\hgopt{remove}{--after}} at any time later on, to
131 tell Mercurial that you really did mean to remove the file.
132 \interaction{daily.files.remove-after}
133
134 On the other hand, if you deleted the missing file by accident, use
135 \hgcmdargs{revert}{\emph{filename}} to recover the file. It will
136 reappear, in unmodified form.
137 \interaction{daily.files.recover-missing}
138
139 \subsection{Aside: why tell Mercurial explicitly to
140 remove a file?}
141
142 You might wonder why Mercurial requires you to explicitly tell it that
143 you are deleting a file. Early during the development of Mercurial,
144 it let you delete a file however you pleased; Mercurial would notice
145 the absence of the file automatically when you next ran a
146 \hgcmd{commit}, and stop tracking the file. In practice, this made it
147 too easy to accidentally remove a file without noticing.
148
149 \subsection{Useful shorthand---adding and removing files
150 in one step}
151
152 Mercurial offers a combination command, \hgcmd{addremove}, that adds
153 untracked files and marks missing files as removed.
154 \interaction{daily.files.addremove}
155 The \hgcmd{commit} command also provides a \hgopt{commit}{-A} option
156 that performs this same add-and-remove, immediately followed by a
157 commit.
158 \interaction{daily.files.commit-addremove}
159
160 \section{Copying files}
161
162 Mercurial provides a \hgcmd{copy} command that lets you make a new
163 copy of a file. When you copy a file using this command, Mercurial
164 makes a record of the fact that the new file is a copy of the original
165 file. It treats these copied files specially when you merge your work
166 with someone else's.
167
168 \subsection{The results of copying during a merge}
169
170 What happens during a merge is that changes ``follow'' a copy. To
171 best illustrate what this means, let's create an example. We'll start
172 with the usual tiny repository that contains a single file.
173 \interaction{daily.copy.init}
174 We need to do some work in parallel, so that we'll have something to
175 merge. So let's clone our repository.
176 \interaction{daily.copy.clone}
177 Back in our initial repository, let's use the \hgcmd{copy} command to
178 make a copy of the first file we created.
179 \interaction{daily.copy.copy}
180
181 If we look at the output of the \hgcmd{status} command afterwards, the
182 copied file looks just like a normal added file.
183 \interaction{daily.copy.status}
184 But if we pass the \hgopt{status}{-C} option to \hgcmd{status}, it
185 prints another line of output: this is the file that our newly-added
186 file was copied \emph{from}.
187 \interaction{daily.copy.status-copy}
188
189 Now, back in the repository we cloned, let's make a change in
190 parallel. We'll add a line of content to the original file that we
191 created.
192 \interaction{daily.copy.other}
193 Now we have a modified \filename{file} in this repository. When we
194 pull the changes from the first repository, and merge the two heads,
195 Mercurial will propagate the changes that we made locally to
196 \filename{file} into its copy, \filename{new-file}.
197 \interaction{daily.copy.merge}
198
199 \subsection{Why should changes follow copies?}
200 \label{sec:daily:why-copy}
201
202 This behaviour, of changes to a file propagating out to copies of the
203 file, might seem esoteric, but in most cases it's highly desirable.
204
205 First of all, remember that this propagation \emph{only} happens when
206 you merge. So if you \hgcmd{copy} a file, and subsequently modify the
207 original file during the normal course of your work, nothing will
208 happen.
209
210 The second thing to know is that modifications will only propagate
211 across a copy as long as the repository that you're pulling changes
212 from \emph{doesn't know} about the copy.
213
214 The reason that Mercurial does this is as follows. Let's say I make
215 an important bug fix in a source file, and commit my changes.
216 Meanwhile, you've decided to \hgcmd{copy} the file in your repository,
217 without knowing about the bug or having seen the fix, and you have
218 started hacking on your copy of the file.
219
220 If you pulled and merged my changes, and Mercurial \emph{didn't}
221 propagate changes across copies, your source file would now contain
222 the bug, and unless you remembered to propagate the bug fix by hand,
223 the bug would \emph{remain} in your copy of the file.
224
225 By automatically propagating the change that fixed the bug from the
226 original file to the copy, Mercurial prevents this class of problem.
227 To my knowledge, Mercurial is the \emph{only} revision control system
228 that propagates changes across copies like this.
229
230 Once your change history has a record that the copy and subsequent
231 merge occurred, there's usually no further need to propagate changes
232 from the original file to the copied file, and that's why Mercurial
233 only propagates changes across copies until this point, and no
234 further.
235
236 \subsection{How to make changes \emph{not} follow a copy}
237
238 If, for some reason, you decide that this business of automatically
239 propagating changes across copies is not for you, simply use your
240 system's normal file copy command (on Unix-like systems, that's
241 \command{cp}) to make a copy of a file, then \hgcmd{add} the new copy
242 by hand. Before you do so, though, please do reread
243 section~\ref{sec:daily:why-copy}, and make an informed decision that
244 this behaviour is not appropriate to your specific case.
245
246 \subsection{Behaviour of the \hgcmd{copy} command}
247
248 When you use the \hgcmd{copy} command, Mercurial makes a copy of each
249 source file as it currently stands in the working directory. This
250 means that if you make some modifications to a file, then \hgcmd{copy}
251 it without first having committed those changes, the new copy will
252 also contain the modifications you have made up until that point. (I
253 find this behaviour a little counterintuitive, which is why I mention
254 it here.)
255
256 The \hgcmd{copy} command acts similarly to the Unix \command{cp}
257 command (you can use the \hgcmd{cp} alias if you prefer). The last
258 argument is the \emph{destination}, and all prior arguments are
259 \emph{sources}. If you pass it a single file as the source, and the
260 destination does not exist, it creates a new file with that name.
261 \interaction{daily.copy.simple}
262 If the destination is a directory, Mercurial copies its sources into
263 that directory.
264 \interaction{daily.copy.dir-dest}
265 Copying a directory is recursive, and preserves the directory
266 structure of the source.
267 \interaction{daily.copy.dir-src}
268 If the source and destination are both directories, the source tree is
269 recreated in the destination directory.
270 \interaction{daily.copy.dir-src-dest}
271
272 As with the \hgcmd{rename} command, if you copy a file manually and
273 then want Mercurial to know that you've copied the file, simply use
274 the \hgopt{copy}{--after} option to \hgcmd{copy}.
275 \interaction{daily.copy.after}
276
277 \section{Renaming files}
278
279 It's rather more common to need to rename a file than to make a copy
280 of it. The reason I discussed the \hgcmd{copy} command before talking
281 about renaming files is that Mercurial treats a rename in essentially
282 the same way as a copy. Therefore, knowing what Mercurial does when
283 you copy a file tells you what to expect when you rename a file.
284
285 When you use the \hgcmd{rename} command, Mercurial makes a copy of
286 each source file, then deletes it and marks the file as removed.
287 \interaction{daily.rename.rename}
288 The \hgcmd{status} command shows the newly copied file as added, and
289 the copied-from file as removed.
290 \interaction{daily.rename.status}
291 As with the results of a \hgcmd{copy}, we must use the
292 \hgopt{status}{-C} option to \hgcmd{status} to see that the added file
293 is really being tracked by Mercurial as a copy of the original, now
294 removed, file.
295 \interaction{daily.rename.status-copy}
296
297 As with \hgcmd{remove} and \hgcmd{copy}, you can tell Mercurial about
298 a rename after the fact using the \hgopt{rename}{--after} option. In
299 most other respects, the behaviour of the \hgcmd{rename} command, and
300 the options it accepts, are similar to the \hgcmd{copy} command.
301
302 \subsection{Renaming files and merging changes}
303
304 Since Mercurial's rename is implemented as copy-and-remove, the same
305 propagation of changes happens when you merge after a rename as after
306 a copy.
307
308 If I modify a file, and you rename it to a new name, and then we merge
309 our respective changes, my modifications to the file under its
310 original name will be propagated into the file under its new name.
311 (This is something you might expect to ``simply work,'' but not all
312 revision control systems actually do this.)
313
314 Whereas having changes follow a copy is a feature where you can
315 perhaps nod and say ``yes, that might be useful,'' it should be clear
316 that having them follow a rename is definitely important. Without
317 this facility, it would simply be too easy for changes to become
318 orphaned when files are renamed.
319
320 \subsection{Divergent renames and merging}
321
322 The case of diverging names occurs when two developers start with a
323 file---let's call it \filename{foo}---in their respective
324 repositories.
325
326 \interaction{rename.divergent.clone}
327 Anne renames the file to \filename{bar}.
328 \interaction{rename.divergent.rename.anne}
329 Meanwhile, Bob renames it to \filename{quux}.
330 \interaction{rename.divergent.rename.bob}
331
332 I like to think of this as a conflict because each developer has
333 expressed different intentions about what the file ought to be named.
334
335 What do you think should happen when they merge their work?
336 Mercurial's actual behaviour is that it always preserves \emph{both}
337 names when it merges changesets that contain divergent renames.
338 \interaction{rename.divergent.merge}
339
340 Notice that Mercurial does warn about the divergent renames, but it
341 leaves it up to you to do something about the divergence after the merge.
342
343 \subsection{Convergent renames and merging}
344
345 Another kind of rename conflict occurs when two people choose to
346 rename different \emph{source} files to the same \emph{destination}.
347 In this case, Mercurial runs its normal merge machinery, and lets you
348 guide it to a suitable resolution.
349
350 \subsection{Other name-related corner cases}
351
352 Mercurial has a longstanding bug in which it fails to handle a merge
353 where one side has a file with a given name, while another has a
354 directory with the same name. This is documented as~\bug{29}.
355 \interaction{issue29.go}
356
357 \section{Recovering from mistakes}
358
359 Mercurial has some useful commands that will help you to recover from
360 some common mistakes.
361
362 The \hgcmd{revert} command lets you undo changes that you have made to
363 your working directory. For example, if you \hgcmd{add} a file by
364 accident, just run \hgcmd{revert} with the name of the file you added,
365 and while the file won't be touched in any way, it won't be tracked
366 for adding by Mercurial any longer, either. You can also use
367 \hgcmd{revert} to get rid of erroneous changes to a file.
368
369 It's useful to remember that the \hgcmd{revert} command is useful for
370 changes that you have not yet committed. Once you've committed a
371 change, if you decide it was a mistake, you can still do something
372 about it, though your options may be more limited.
373
374 For more information about the \hgcmd{revert} command, and details
375 about how to deal with changes you have already committed, see
376 chapter~\ref{chap:undo}.
377
378 %%% Local Variables:
379 %%% mode: latex
380 %%% TeX-master: "00book"
381 %%% End: