hgbook
diff en/tour.tex @ 91:7524d52d9577
More tour progress.
author | Bryan O'Sullivan <bos@serpentine.com> |
---|---|
date | Thu Oct 12 16:01:40 2006 -0700 (2006-10-12) |
parents | d351032c189c |
children | 72d207927dc4 |
line diff
1.1 --- a/en/tour.tex Thu Oct 12 10:33:03 2006 -0700 1.2 +++ b/en/tour.tex Thu Oct 12 16:01:40 2006 -0700 1.3 @@ -145,8 +145,8 @@ 1.4 1.5 To introduce a little terminology, the \dirname{.hg} directory is the 1.6 ``real'' repository, and all of the files and directories that coexist 1.7 -with it are said to live in the ``working directory''. An easy way to 1.8 -remember the distinction is that the \emph{repository} contains the 1.9 +with it are said to live in the \emph{working directory}. An easy way 1.10 +to remember the distinction is that the \emph{repository} contains the 1.11 \emph{history} of your project, while the \emph{working directory} 1.12 contains a \emph{snapshot} of your project at a particular point in 1.13 history. 1.14 @@ -217,10 +217,9 @@ 1.15 \subsection{Viewing specific revisions} 1.16 1.17 To narrow the output of \hgcmd{log} down to a single revision, use the 1.18 -\hgopt{log}{-r} option. You can use either a revision number or a 1.19 -long-form changeset identifier, and you can provide as many revisions 1.20 -as you want. 1.21 -\interaction{tour.log-r} 1.22 +\hgopt{log}{-r} (or \hgopt{log}{--rev}) option. You can use either a 1.23 +revision number or a long-form changeset identifier, and you can 1.24 +provide as many revisions as you want. \interaction{tour.log-r} 1.25 1.26 If you want to see the history of several revisions without having to 1.27 list each one, you can use \emph{range notation}; this lets you 1.28 @@ -231,6 +230,248 @@ 1.29 \hgcmdargs{log}{-r 2:4} prints $2,3,4$ while \hgcmdargs{log}{-r 4:2} 1.30 prints $4,3,2$. 1.31 1.32 +\subsection{More detailed information} 1.33 + 1.34 +While the summary information printed by \hgcmd{log} is useful if you 1.35 +already know what you're looking for, you may need to see a complete 1.36 +description of the change, or a list of the files changed, if you're 1.37 +trying to decide whether a changeset is the one you're looking for. 1.38 +The \hgcmd{log} command's \hggopt{-v} (or \hggopt{--verbose}) 1.39 +option gives you this extra detail. 1.40 +\interaction{tour.log-v} 1.41 + 1.42 +If you want to see both the description and content of a change, add 1.43 +the \hgopt{log}{-p} (or \hgopt{log}{--patch}) option. This displays 1.44 +the content of a change as a \emph{unified diff} (if you've never seen 1.45 +a unified diff before, see section~\ref{sec:mq:patch} for an overview). 1.46 +\interaction{tour.log-vp} 1.47 + 1.48 +\section{All about command options} 1.49 + 1.50 +Let's take a brief break from exploring Mercurial commands to discuss 1.51 +a pattern in the way that they work; you may find this useful to keep 1.52 +in mind as we continiue our tour. 1.53 + 1.54 +Mercurial has a consistent and straightforward approach to dealing 1.55 +with the options that you can pass to commands. It follows the 1.56 +conventions for options that are common to modern Linux and Unix 1.57 +systems. 1.58 +\begin{itemize} 1.59 +\item Every option has a long name. For example, as we've already 1.60 + seen, the \hgcmd{log} command accepts a \hgopt{log}{--rev} option. 1.61 +\item Most options have short names, too. Instead of 1.62 + \hgopt{log}{--rev}, we can use \hgopt{log}{-r}. (The reason that 1.63 + some options don't have short names is that the options in question 1.64 + are rarely used.) 1.65 +\item Long options start with two dashes (e.g.~\hgopt{log}{--rev}), 1.66 + while short options start with one (e.g.~\hgopt{log}{-r}). 1.67 +\item Option naming and usage is consistent across commands. For 1.68 + example, every command that lets you specify a changeset~ID or 1.69 + revision number accepts both \hgopt{log}{-r} and \hgopt{log}{--rev} 1.70 + arguments. 1.71 +\end{itemize} 1.72 +In the examples throughout this book, I use short options instead of 1.73 +long. This just reflects my own preference, so don't read anything 1.74 +significant into it. 1.75 + 1.76 +Most commands that print output of some kind will print more output 1.77 +when passed a \hggopt{-v} (or \hggopt{--verbose}) option, and less 1.78 +when passed \hggopt{-q} (or \hggopt{--quiet}). 1.79 + 1.80 +\section{Making and reviewing changes} 1.81 + 1.82 +Now that we have a grasp of viewing history in Mercurial, let's take a 1.83 +look at making some changes and examining them. 1.84 + 1.85 +The first thing we'll do is isolate our experiment in a repository of 1.86 +its own. We use the \hgcmd{clone} command, but we don't need to 1.87 +clone a copy of the remote repository. Since we already have a copy 1.88 +of it locally, we can just clone that instead. This is much faster 1.89 +than cloning over the network, and cloning a local repository uses 1.90 +less disk space in most cases, too. 1.91 +\interaction{tour.reclone} 1.92 +As an aside, it's often good practice to keep a ``pristine'' copy of a 1.93 +remote repository around, which you can then make temporary clones of 1.94 +to create sandboxes for each task you want to work on. This lets you 1.95 +work on multiple tasks in parallel, each isolated from the others 1.96 +until it's complete and you're ready to integrate it back. Because 1.97 +local clones are so cheap, there's almost no overhead to cloning and 1.98 +destroying repositories whenever you want. 1.99 + 1.100 +In our \dirname{my-hello} repository, we have a file 1.101 +\filename{hello.c} that contains the classic ``hello, world'' program. 1.102 +Let's use the ancient and venerable \command{sed} command to edit this 1.103 +file so that it prints a second line of output. (I'm only using 1.104 +\command{sed} to do this because it's easy to write a scripted example 1.105 +this way. Since you're not under the same constraint, you probably 1.106 +won't want to use \command{sed}; simply use your preferred text editor to 1.107 +do the same thing.) 1.108 +\interaction{tour.sed} 1.109 + 1.110 +Mercurial's \hgcmd{status} command will tell us what Mercurial knows 1.111 +about the files in the repository. 1.112 +\interaction{tour.status} 1.113 +The \hgcmd{status} command prints no output for some files, but a line 1.114 +starting with ``\texttt{M}'' for \filename{hello.c}. Unless you tell 1.115 +it to, \hgcmd{status} will not print any output for files that have 1.116 +not been modified. 1.117 + 1.118 +The ``\texttt{M}'' indicates that Mercurial has noticed that we 1.119 +modified \filename{hello.c}. Notice that we didn't need to 1.120 +\emph{inform} Mercurial that we were going to modify the file before 1.121 +we started, or that we had modified the file after we were done; it 1.122 +was able to figure this out itself. 1.123 + 1.124 +It's a little bit helpful to know that we've modified 1.125 +\filename{hello.c}, but we might prefer to know exactly \emph{what} 1.126 +changes we've made to it. To do this, we use the \hgcmd{diff} 1.127 +command. 1.128 +\interaction{tour.diff} 1.129 + 1.130 +\section{Recording changes in a new changeset} 1.131 + 1.132 +We can modify files, build and test our changes, and use 1.133 +\hgcmd{status} and \hgcmd{diff} to review our changes, until we're 1.134 +satisfied with what we've done and arrive at a natural stopping point 1.135 +where we want to record our work in a new changeset. 1.136 + 1.137 +The \hgcmd{commit} command lets us create a new changeset; we'll 1.138 +usually refer to this as ``making a commit'' or ``committing''. 1.139 + 1.140 +\subsection{Writing a commit message} 1.141 + 1.142 +When we commit a change, Mercurial drops us into a text editor, to 1.143 +enter a message that will describe the modifications we've made in 1.144 +this changeset. This is called the \emph{commit message}. It will be 1.145 +a record for readers of what we did and why, and it will be printed by 1.146 +\hgcmd{log} after we've finished committing. 1.147 +\interaction{tour.commit} 1.148 + 1.149 +The editor that the \hgcmd{commit} command drops us into will contain 1.150 +an empty line, followed by a number of lines starting with 1.151 +``\texttt{HG:}''. 1.152 +\begin{codesample2} 1.153 + \emph{empty line} 1.154 + HG: changed hello.c 1.155 +\end{codesample2} 1.156 +Mercurial ignores the lines that start with ``\texttt{HG:}''; it uses 1.157 +them only to tell us which files it's recording changes to. Modifying 1.158 +or deleting these lines has no effect. 1.159 + 1.160 +\subsection{Writing a good commit message} 1.161 + 1.162 +Since \hgcmd{log} only prints the first line of a commit message by 1.163 +default, it's best to write a commit message whose first line stands 1.164 +alone. Here's a real example of a commit message that \emph{doesn't} 1.165 +follow this guideline, and hence has a summary that is not readable. 1.166 +\begin{codesample2} 1.167 + changeset: 73:584af0e231be 1.168 + user: Censored Person <censored.person@example.org> 1.169 + date: Tue Sep 26 21:37:07 2006 -0700 1.170 + summary: include buildmeister/commondefs. Add an exports and install 1.171 +\end{codesample2} 1.172 + 1.173 +As far as the remainder of the contents of the commit message are 1.174 +concerned, there are no hard-and-fast rules. Mercurial itself doesn't 1.175 +interpret or care about the contents of the commit message, though 1.176 +your project may have policies that dictate a certain kind of 1.177 +formatting. 1.178 + 1.179 +My personal preference is for short, but informative, commit messages 1.180 +that tell me something that I can't figure out with a quick glance at 1.181 +the output of \hgcmdargs{log}{--patch}. 1.182 + 1.183 +\subsection{Aborting a commit} 1.184 + 1.185 +If you decide that you don't want to commit while in the middle of 1.186 +editing a commit message, simply exit from your editor without saving 1.187 +the file that it's editing. This will cause nothing to happen to 1.188 +either the repository or the working directory. 1.189 + 1.190 +If we run the \hgcmd{commit} command without any arguments, it records 1.191 +all of the changes we've made, as reported by \hgcmd{status} and 1.192 +\hgcmd{diff}. 1.193 + 1.194 +\subsection{Admiring our new handywork} 1.195 + 1.196 +Once we've finished the commit, we can use the \hgcmd{tip} command to 1.197 +display the changeset we just created. This command produces output 1.198 +that is identical to \hgcmd{log}, but it only displays the newest 1.199 +revision in the repository. 1.200 +\interaction{tour.tip} 1.201 +We refer to the newest revision in the repository as the tip revision, 1.202 +or simply the tip. 1.203 + 1.204 +\section{Sharing changes} 1.205 + 1.206 +We mentioned earlier that repositories in Mercurial are 1.207 +self-contained. This means that the changeset we just created exists 1.208 +only in our \dirname{my-hello} repository. Let's look at a few ways 1.209 +that we can propagate this change into other repositories. 1.210 + 1.211 +\subsection{Pulling changes from another repository} 1.212 +\label{sec:tour:pull} 1.213 + 1.214 +To get started, let's clone our original \dirname{hello} repository, 1.215 +which does not contain the change we just committed. We'll call our 1.216 +temporary repository \dirname{hello-pull}. 1.217 +\interaction{tour.clone-pull} 1.218 + 1.219 +We'll use the \hgcmd{pull} command to bring changes from 1.220 +\dirname{my-hello} into \dirname{hello-pull}. However, blindly 1.221 +pulling unknown changes into a repository is a somewhat scary 1.222 +prospect. Mercurial provides the \hgcmd{incoming} command to tell us 1.223 +what changes the \hgcmd{pull} command \emph{would} pull into the 1.224 +repository, without actually pulling the changes in. 1.225 +\interaction{tour.incoming} 1.226 +(Of course, someone could cause more changesets to appear in the 1.227 +repository that we ran \hgcmd{incoming} in, before we get a chance to 1.228 +\hgcmd{pull} the changes, so that we could end up pulling changes that we 1.229 +didn't expect.) 1.230 + 1.231 +Bringing changes into a repository is a simple matter of running the 1.232 +\hgcmd{pull} command, and telling it which repository to pull from. 1.233 +\interaction{tour.pull} 1.234 +As you can see from the before-and-after output of \hgcmd{tip}, we 1.235 +have successfully pulled changes into our repository. There remains 1.236 +one step before we can work with those changes. 1.237 + 1.238 +\section{Updating the working directory} 1.239 + 1.240 +We have so far glossed over the relatioship between a repository and 1.241 +its working directory. The \hgcmd{pull} command that we ran in 1.242 +section~\ref{sec:tour:pull} brought changes into the repository, but 1.243 +if we check, there's no sign of those changes in the working 1.244 +directory. This is because \hgcmd{pull} does not (by default) touch 1.245 +the working directory. Instead, we use the \hgcmd{update} command to 1.246 +do this. 1.247 +\interaction{tour.update} 1.248 + 1.249 +It might seem a bit strange that \hgcmd{pull} doesn't update the 1.250 +working directory automatically. There's actually a good reason for 1.251 +this: you can use \hgcmd{update} to update the working directory to 1.252 +the state it was in at \emph{any revision} in the history of the 1.253 +repository. If you had the working directory updated to an old 1.254 +revision---to hunt down the origin of a bug, say---and ran a 1.255 +\hgcmd{pull} which automatically updated the working directory to a 1.256 +new revision, you might not be terribly happy. 1.257 + 1.258 +However, since pull-then-update is such a common thing to do, 1.259 +Mercurial lets you combine the two by passing the \hgopt{pull}{-u} 1.260 +option to \hgcmd{pull}. 1.261 +\begin{codesample2} 1.262 + hg pull -u 1.263 +\end{codesample2} 1.264 + 1.265 +To find out what revision the working directory is at, use the 1.266 +\hgcmd{parents} command. 1.267 +\interaction{tour.parents} 1.268 +To update the working directory to a particular revision, give a 1.269 +revision number or changeset~ID to the \hgcmd{update} command. 1.270 +\interaction{tour.older} 1.271 +If you omit an explicit revision, \hgcmd{update} will update to the 1.272 +tip revision. 1.273 + 1.274 %%% Local Variables: 1.275 %%% mode: latex 1.276 %%% TeX-master: "00book"