hgbook

view es/undo.tex @ 386:d2467817c934

some paragraphs translated to spanish
author Igor TAmara <igor@tamarapatino.org>
date Sun Nov 02 14:23:01 2008 -0500 (2008-11-02)
parents 6d2bbeb50af6
children 7864f2264e28
line source
1 \chapter{Encontrar y arreglar sus equivocaciones}
2 \label{chap:undo}
4 Errar es humano, pero tratar adecuadamente las consecuencias requiere
5 un sistema de control de revisiones de primera categoría. En este
6 capítulo, discutiremos algunas técnicas que puede usar cuando
7 encuentra que hay un problema enraizado en su proyecto. Mercurial
8 tiene unas características poderosas que le ayudarán a isolar las
9 fuentes de los problemas, y a dar cuenta de ellas apropiadamente.
11 \section{Borrar la historia local}
13 \subsection{La consignación accidental}
15 Tengo el problema ocasional, pero persistente de teclear más rápido de
16 lo que pienso, que aveces resulta en consignar un conjunto de cambios
17 incompleto o simplemente malo. En mi caso, el conjunto de cambios
18 incompleto consiste en que creé un nuevo fichero fuente, pero olvidé
19 hacerle \hgcmd{add}. Un conjunto de cambios``simplemente malo'' no es
20 tan común, pero sí resulta muy molesto.
22 \subsection{Hacer rollback una transacción}
23 \label{sec:undo:rollback}
25 En la sección~\ref{sec:concepts:txn}, mencioné que Mercurial trata
26 modificación a un repositorio como una \emph{transacción}. Cada vez
27 que consigna un conjunto de cambios o lo jala de otro repositorio,
28 Mercurial recuerda lo que hizo. Puede deshacer, o hacer \emph{roll back}\ndt{El significado igual que en los
29 ambientes de sistemas manejadores de bases de datos se refiere a
30 la atomicidad e integridad al devolver un conjunto de acciones que
31 permitan dejar el repositorio en un estado consistente previo},
32 exactamente una de tales acciones usando la orden \hgcmd{rollback}.
33 (Ver en la sección~\ref{sec:undo:rollback-after-push} una anotación
34 importante acerca del uso de esta orden.)
36 A continuación una equivocación que me sucede frecuentemente:
37 consignar un cambio en el cual he creado un nuevo fichero, pero he
38 olvidado hacerle \hgcmd{add}.
39 \interaction{rollback.commit}
40 La salida de \hgcmd{status} después de la consignación confirma
41 inmediatamente este error.
42 \interaction{rollback.status}
43 La consignación capturó los cambios en el fichero \filename{a}, pero
44 no el nuevo fichero \filename{b}. Si yo publicara este conjunto de
45 cambios a un repositorio compartido con un colega, es bastante
46 probable que algo en \filename{a} se refiriera a \filename{b}, el cual
47 podría no estar presente cuando jalen mis cambios del repositorio. Me
48 convertiría el sujeto de cierta indignación.
50 Como sea, la suerte me acompaña---Encontré mi error antes de publicar
51 el conjunto de cambios. Uso la orden \hgcmd{rollback}, y Mercurial
52 hace desaparecer el último conjunto de cambios.
53 \interaction{rollback.rollback}
54 El conjunto de cambios ya no está en la historia del repositorio, y el
55 directorio de trabajo cree que el fichero \filename{a} ha sido
56 modificado. La consignación y el roll back dejaron el directorio de
57 trabajo exactamente como estaba antes de la consignación; el conjunto
58 de cambios ha sido eliminado totlamente. Ahora puedo hacer \hgcmd{add}
59 al fichero \filename{b}, y hacer de nuevo la consignación.
60 \interaction{rollback.add}
62 \subsection{Erroneamente jalado}
64 Mantener ramas de desarrollo separadas de un proyecto en distintos
65 repositorios es una práctica común con Mercurial. Su equipo de
66 desarrollo puede tener un repositorio compartido para la versión ``0.9''
67 y otra con cambios distintos para la versión ``1.0''.
69 Con este escenario, puede imaginar las consecuencias si tuviera un
70 repositorio local ``0.9'', y jalara accidentalmente los cambios del
71 repositorio compartido de la versión ``1.0'' en este. En el peor de
72 los casos, por falta de atención, es posible que publique tales
73 cambios en el árbol compartido ``0.9'', confundiendo a todo su equipo
74 de trabajo(pero no se preocupe, volveremos a este terrorífico
75 escenario posteriormente). En todo caso, es muy probable que usted se
76 de cuenta inmediatamente, dado que Mercurial mostrará el URL de donde
77 está jalando, o que vea jalando una sospechosa gran cantidad de
78 cambios en el repositorio.
80 La orden \hgcmd{rollback} excluirá eficientemente los conjuntos de
81 cambios que haya acabado de jalar. Mercurial agrupa todos los cambios
82 de un \hgcmd{pull} a una única transacción y bastará con un
83 \hgcmd{rollback} para deshacer esta equivocación.
85 \subsection{Después de publicar, un roll back es futil}
86 \label{sec:undo:rollback-after-push}
88 El valor de \hgcmd{rollback} se anula cuando ha publicado sus cambios
89 a otro repositorio. Un cambio desaparece totalmente al hacer roll back,
90 pero \emph{solamente} en el repositorio en el cual aplica
91 \hgcmd{rollback}. Debido a que un roll back elimina la historia,
92 no hay forma de que la desaparición de un cambio se propague entre
93 repositorios.
95 Si ha publicado un cambio en otro repositorio---particularmente si es
96 un repositorio público---esencialmente está ``en terreno agreste,''
97 y tendrá que reparar la equivocación de un modo distinto. Lo que
98 pasará si publica un conjunto de cambios en algún sitio, hacer
99 rollback y después volver a jalar del repositorio del cual había
100 publicado, es que el conjunto de cambios reaparecerá en su repositorio.
102 (Si está absolutamente segruro de que el conjunto de cambios al que
103 desea hacer rollback es el cambio más reciente del repositorio en el
104 cual publicó, \emph{y} sabe que nadie más pudo haber jalado de tal
105 repositorio, puede hacer rollback del conjunto de cambios allí, pero
106 es mejor no confiar en una solución de este estilo. Si lo hace, tarde
107 o temprano un conjunto de cambios logrará colarse en un repositorio
108 que usted no controle directamente(o del cual se ha olvidado), y
109 volverá a hostigarle.)
111 \subsection{Solamente hay un roll back}
113 Mercurial almacena exactamente una transacción en su bitácora de
114 transacciones; tal transacción es la más reciente de las que haya
115 ocurrido en el repositorio. Esto significa que solamente puede hacer
116 roll back a una transacción. Si espera poder hacer roll back a una
117 transacción después al antecesor, observará que no es el
118 comportamiento que obtendrá.
119 \interaction{rollback.twice}
120 Una vez que haya aplicado un rollback en una transacción a un
121 repositorio, no podrá volver a hacer rollback hasta que haga una
122 consignación o haya jalado.
124 \section{Revertir un cambio equivocado}
126 Si modifica un fichero y se da cuenta que no quería realmente cambiar
127 tal fichero, y todavía no ha consignado los cambios, la orden
128 necesaria es \hgcmd{revert}. Observa el conjunto de cambios padre del
129 directorio y restaura los contenidos del fichero al estado de tal
130 conjunto de cambios. (Es una forma larga de decirlo, usualmente
131 deshace sus modificaciones.)
133 Ilustremos como actúa la orden \hgcmd{revert} con un ejemplo
134 pequeño. Comenzaremos modificando un fichero al cual Mercurial ya está
135 siguiendo.
136 \interaction{daily.revert.modify}
137 Si no queremos ese cambio, podemos aplicar \hgcmd{revert} al fichero.
138 \interaction{daily.revert.unmodify}
139 La orden \hgcmd{revert} nos brinda un grado adicional de seguridad
140 guardando nuestro fichero modificado con la extensión \filename{.orig}.
141 \interaction{daily.revert.status}
143 Este es un resumen de casos en los cuales la orden \hgcmd{revert} es
144 de utilidad. Describiremos cada uno de ellos con más detalle en la
145 sección siguiente.
146 \begin{itemize}
147 \item Si usted modifica un fichero, lo restaurará a su estado sin
148 modificación previo.
149 \item Si usted hace \hgcmd{add} a un fichero, revertirá el estado de
150 ``adicionado'' del fichero, pero no lo tocará
151 \item Si borra un fichero sin decirle a Mercurial, restaurará el
152 fichero con sus contenidos sin modificación.
153 \item Si usa la orden \hgcmd{remove} para eliminar un fichero, deshará
154 el estado ``removido'' del fichero, y lo restaurará con sus
155 contenidos sin modificación.
156 \end{itemize}
158 \subsection{Errores al administrar ficheros}
159 \label{sec:undo:mgmt}
161 La orden \hgcmd{revert} es útil para más que ficheros modificados. Le
162 permite reversar los resultados de todas las órdenes de administración
163 de ficheros que provee Mercurial---\hgcmd{add}, \hgcmd{remove}, y las
164 demás.
166 Si usted hace \hgcmd{add} a un fichero, y no deseaba que Mercurial le
167 diera seguimiento, use \hgcmd{revert} para deshacer la adición. No se
168 preocupe; Mercurial no modificará de forma alguna el fichero.
169 Solamente lo ``desmarcará''.
170 \interaction{daily.revert.add}
172 De forma similar, Si le solicita a Mercurial hacer \hgcmd{remove} a un
173 fichero, puede usar \hgcmd{revert} para restarurarlo a los contenidos
174 que tenía la revisión padre del directorio de trabajo.
175 \interaction{daily.revert.remove}
176 Funciona de la misma manera para un fichero que usted haya eliminado
177 manualmente, sin decirle a Mercurial (recuerde que en la terminología
178 de Mercurial esta clase de fichero se llama ``faltante'').
179 \interaction{daily.revert.missing}
181 Si usted revierte un \hgcmd{copy}, el fichero a donde se copió
182 permanece en su directorio de trabajo, pero sin seguimiento. Dado que
183 una copia no afecta el fichero fuente de copiado de ninguna maner,
184 Mercurial no hace nada con este.
185 \interaction{daily.revert.copy}
187 \subsubsection{Un caso ligeramente especial:revertir un renombramiento}
189 Si hace \hgcmd{rename} a un fichero, hay un detalle que debe tener en
190 cuenta. Cuando aplica \hgcmd{revert} a un cambio de nombre, no es
191 suficiente proveer el nombre del fichero destino, como puede verlo en
192 el siguiente ejemplo.
193 \interaction{daily.revert.rename}
194 Como puede ver en la salida de \hgcmd{status}, el fichero con el nuevo
195 nombre no se identifica más como agregado, pero el fichero con el
196 nombre-\emph{inicial} se elimna! Esto es contra-intuitivo (por lo
197 menos para mí), pero por lo menos es fácil arreglarlo.
198 \interaction{daily.revert.rename-orig}
199 Por lo tanto, recuerde, para revertir un \hgcmd{rename}, debe proveer
200 \emph{ambos} nombres, la fuente y el destino.
202 % TODO: the output doesn't look like it will be removed!
204 (A propósito, si elimina un fichero, y modifica el fichero con el
205 nuevo nombre, al revertir ambos componentes del renombramiento, cuando
206 Mercurial restaure el fichero que fue eliminado como parte del
207 renombramiento, no será modificado.
208 Si necesita que las modificaciones en el archivo destino del
209 renombramiento se muestren, no olvide copiarlas encima.)
211 Estos aspectos engorrosos de revertir el renombramiento se constituyen
212 discutiblemente en un fallo de Mercurial.
214 \section{Dealing with committed changes}
216 Consider a case where you have committed a change $a$, and another
217 change $b$ on top of it; you then realise that change $a$ was
218 incorrect. Mercurial lets you ``back out'' an entire changeset
219 automatically, and building blocks that let you reverse part of a
220 changeset by hand.
222 Before you read this section, here's something to keep in mind: the
223 \hgcmd{backout} command undoes changes by \emph{adding} history, not
224 by modifying or erasing it. It's the right tool to use if you're
225 fixing bugs, but not if you're trying to undo some change that has
226 catastrophic consequences. To deal with those, see
227 section~\ref{sec:undo:aaaiiieee}.
229 \subsection{Backing out a changeset}
231 The \hgcmd{backout} command lets you ``undo'' the effects of an entire
232 changeset in an automated fashion. Because Mercurial's history is
233 immutable, this command \emph{does not} get rid of the changeset you
234 want to undo. Instead, it creates a new changeset that
235 \emph{reverses} the effect of the to-be-undone changeset.
237 The operation of the \hgcmd{backout} command is a little intricate, so
238 let's illustrate it with some examples. First, we'll create a
239 repository with some simple changes.
240 \interaction{backout.init}
242 The \hgcmd{backout} command takes a single changeset ID as its
243 argument; this is the changeset to back out. Normally,
244 \hgcmd{backout} will drop you into a text editor to write a commit
245 message, so you can record why you're backing the change out. In this
246 example, we provide a commit message on the command line using the
247 \hgopt{backout}{-m} option.
249 \subsection{Backing out the tip changeset}
251 We're going to start by backing out the last changeset we committed.
252 \interaction{backout.simple}
253 You can see that the second line from \filename{myfile} is no longer
254 present. Taking a look at the output of \hgcmd{log} gives us an idea
255 of what the \hgcmd{backout} command has done.
256 \interaction{backout.simple.log}
257 Notice that the new changeset that \hgcmd{backout} has created is a
258 child of the changeset we backed out. It's easier to see this in
259 figure~\ref{fig:undo:backout}, which presents a graphical view of the
260 change history. As you can see, the history is nice and linear.
262 \begin{figure}[htb]
263 \centering
264 \grafix{undo-simple}
265 \caption{Backing out a change using the \hgcmd{backout} command}
266 \label{fig:undo:backout}
267 \end{figure}
269 \subsection{Backing out a non-tip change}
271 If you want to back out a change other than the last one you
272 committed, pass the \hgopt{backout}{--merge} option to the
273 \hgcmd{backout} command.
274 \interaction{backout.non-tip.clone}
275 This makes backing out any changeset a ``one-shot'' operation that's
276 usually simple and fast.
277 \interaction{backout.non-tip.backout}
279 If you take a look at the contents of \filename{myfile} after the
280 backout finishes, you'll see that the first and third changes are
281 present, but not the second.
282 \interaction{backout.non-tip.cat}
284 As the graphical history in figure~\ref{fig:undo:backout-non-tip}
285 illustrates, Mercurial actually commits \emph{two} changes in this
286 kind of situation (the box-shaped nodes are the ones that Mercurial
287 commits automatically). Before Mercurial begins the backout process,
288 it first remembers what the current parent of the working directory
289 is. It then backs out the target changeset, and commits that as a
290 changeset. Finally, it merges back to the previous parent of the
291 working directory, and commits the result of the merge.
293 % TODO: to me it looks like mercurial doesn't commit the second merge automatically!
295 \begin{figure}[htb]
296 \centering
297 \grafix{undo-non-tip}
298 \caption{Automated backout of a non-tip change using the \hgcmd{backout} command}
299 \label{fig:undo:backout-non-tip}
300 \end{figure}
302 The result is that you end up ``back where you were'', only with some
303 extra history that undoes the effect of the changeset you wanted to
304 back out.
306 \subsubsection{Always use the \hgopt{backout}{--merge} option}
308 In fact, since the \hgopt{backout}{--merge} option will do the ``right
309 thing'' whether or not the changeset you're backing out is the tip
310 (i.e.~it won't try to merge if it's backing out the tip, since there's
311 no need), you should \emph{always} use this option when you run the
312 \hgcmd{backout} command.
314 \subsection{Gaining more control of the backout process}
316 While I've recommended that you always use the
317 \hgopt{backout}{--merge} option when backing out a change, the
318 \hgcmd{backout} command lets you decide how to merge a backout
319 changeset. Taking control of the backout process by hand is something
320 you will rarely need to do, but it can be useful to understand what
321 the \hgcmd{backout} command is doing for you automatically. To
322 illustrate this, let's clone our first repository, but omit the
323 backout change that it contains.
325 \interaction{backout.manual.clone}
326 As with our earlier example, We'll commit a third changeset, then back
327 out its parent, and see what happens.
328 \interaction{backout.manual.backout}
329 Our new changeset is again a descendant of the changeset we backout
330 out; it's thus a new head, \emph{not} a descendant of the changeset
331 that was the tip. The \hgcmd{backout} command was quite explicit in
332 telling us this.
333 \interaction{backout.manual.log}
335 Again, it's easier to see what has happened by looking at a graph of
336 the revision history, in figure~\ref{fig:undo:backout-manual}. This
337 makes it clear that when we use \hgcmd{backout} to back out a change
338 other than the tip, Mercurial adds a new head to the repository (the
339 change it committed is box-shaped).
341 \begin{figure}[htb]
342 \centering
343 \grafix{undo-manual}
344 \caption{Backing out a change using the \hgcmd{backout} command}
345 \label{fig:undo:backout-manual}
346 \end{figure}
348 After the \hgcmd{backout} command has completed, it leaves the new
349 ``backout'' changeset as the parent of the working directory.
350 \interaction{backout.manual.parents}
351 Now we have two isolated sets of changes.
352 \interaction{backout.manual.heads}
354 Let's think about what we expect to see as the contents of
355 \filename{myfile} now. The first change should be present, because
356 we've never backed it out. The second change should be missing, as
357 that's the change we backed out. Since the history graph shows the
358 third change as a separate head, we \emph{don't} expect to see the
359 third change present in \filename{myfile}.
360 \interaction{backout.manual.cat}
361 To get the third change back into the file, we just do a normal merge
362 of our two heads.
363 \interaction{backout.manual.merge}
364 Afterwards, the graphical history of our repository looks like
365 figure~\ref{fig:undo:backout-manual-merge}.
367 \begin{figure}[htb]
368 \centering
369 \grafix{undo-manual-merge}
370 \caption{Manually merging a backout change}
371 \label{fig:undo:backout-manual-merge}
372 \end{figure}
374 \subsection{Why \hgcmd{backout} works as it does}
376 Here's a brief description of how the \hgcmd{backout} command works.
377 \begin{enumerate}
378 \item It ensures that the working directory is ``clean'', i.e.~that
379 the output of \hgcmd{status} would be empty.
380 \item It remembers the current parent of the working directory. Let's
381 call this changeset \texttt{orig}
382 \item It does the equivalent of a \hgcmd{update} to sync the working
383 directory to the changeset you want to back out. Let's call this
384 changeset \texttt{backout}
385 \item It finds the parent of that changeset. Let's call that
386 changeset \texttt{parent}.
387 \item For each file that the \texttt{backout} changeset affected, it
388 does the equivalent of a \hgcmdargs{revert}{-r parent} on that file,
389 to restore it to the contents it had before that changeset was
390 committed.
391 \item It commits the result as a new changeset. This changeset has
392 \texttt{backout} as its parent.
393 \item If you specify \hgopt{backout}{--merge} on the command line, it
394 merges with \texttt{orig}, and commits the result of the merge.
395 \end{enumerate}
397 An alternative way to implement the \hgcmd{backout} command would be
398 to \hgcmd{export} the to-be-backed-out changeset as a diff, then use
399 the \cmdopt{patch}{--reverse} option to the \command{patch} command to
400 reverse the effect of the change without fiddling with the working
401 directory. This sounds much simpler, but it would not work nearly as
402 well.
404 The reason that \hgcmd{backout} does an update, a commit, a merge, and
405 another commit is to give the merge machinery the best chance to do a
406 good job when dealing with all the changes \emph{between} the change
407 you're backing out and the current tip.
409 If you're backing out a changeset that's~100 revisions back in your
410 project's history, the chances that the \command{patch} command will
411 be able to apply a reverse diff cleanly are not good, because
412 intervening changes are likely to have ``broken the context'' that
413 \command{patch} uses to determine whether it can apply a patch (if
414 this sounds like gibberish, see \ref{sec:mq:patch} for a
415 discussion of the \command{patch} command). Also, Mercurial's merge
416 machinery will handle files and directories being renamed, permission
417 changes, and modifications to binary files, none of which
418 \command{patch} can deal with.
420 \section{Changes that should never have been}
421 \label{sec:undo:aaaiiieee}
423 Most of the time, the \hgcmd{backout} command is exactly what you need
424 if you want to undo the effects of a change. It leaves a permanent
425 record of exactly what you did, both when committing the original
426 changeset and when you cleaned up after it.
428 On rare occasions, though, you may find that you've committed a change
429 that really should not be present in the repository at all. For
430 example, it would be very unusual, and usually considered a mistake,
431 to commit a software project's object files as well as its source
432 files. Object files have almost no intrinsic value, and they're
433 \emph{big}, so they increase the size of the repository and the amount
434 of time it takes to clone or pull changes.
436 Before I discuss the options that you have if you commit a ``brown
437 paper bag'' change (the kind that's so bad that you want to pull a
438 brown paper bag over your head), let me first discuss some approaches
439 that probably won't work.
441 Since Mercurial treats history as accumulative---every change builds
442 on top of all changes that preceded it---you generally can't just make
443 disastrous changes disappear. The one exception is when you've just
444 committed a change, and it hasn't been pushed or pulled into another
445 repository. That's when you can safely use the \hgcmd{rollback}
446 command, as I detailed in section~\ref{sec:undo:rollback}.
448 After you've pushed a bad change to another repository, you
449 \emph{could} still use \hgcmd{rollback} to make your local copy of the
450 change disappear, but it won't have the consequences you want. The
451 change will still be present in the remote repository, so it will
452 reappear in your local repository the next time you pull.
454 If a situation like this arises, and you know which repositories your
455 bad change has propagated into, you can \emph{try} to get rid of the
456 changeefrom \emph{every} one of those repositories. This is, of
457 course, not a satisfactory solution: if you miss even a single
458 repository while you're expunging, the change is still ``in the
459 wild'', and could propagate further.
461 If you've committed one or more changes \emph{after} the change that
462 you'd like to see disappear, your options are further reduced.
463 Mercurial doesn't provide a way to ``punch a hole'' in history,
464 leaving changesets intact.
466 XXX This needs filling out. The \texttt{hg-replay} script in the
467 \texttt{examples} directory works, but doesn't handle merge
468 changesets. Kind of an important omission.
470 \subsection{Protect yourself from ``escaped'' changes}
472 If you've committed some changes to your local repository and they've
473 been pushed or pulled somewhere else, this isn't necessarily a
474 disaster. You can protect yourself ahead of time against some classes
475 of bad changeset. This is particularly easy if your team usually
476 pulls changes from a central repository.
478 By configuring some hooks on that repository to validate incoming
479 changesets (see chapter~\ref{chap:hook}), you can automatically
480 prevent some kinds of bad changeset from being pushed to the central
481 repository at all. With such a configuration in place, some kinds of
482 bad changeset will naturally tend to ``die out'' because they can't
483 propagate into the central repository. Better yet, this happens
484 without any need for explicit intervention.
486 For instance, an incoming change hook that verifies that a changeset
487 will actually compile can prevent people from inadvertantly ``breaking
488 the build''.
490 \section{Finding the source of a bug}
491 \label{sec:undo:bisect}
493 While it's all very well to be able to back out a changeset that
494 introduced a bug, this requires that you know which changeset to back
495 out. Mercurial provides an invaluable command, called
496 \hgcmd{bisect}, that helps you to automate this process and accomplish
497 it very efficiently.
499 The idea behind the \hgcmd{bisect} command is that a changeset has
500 introduced some change of behaviour that you can identify with a
501 simple binary test. You don't know which piece of code introduced the
502 change, but you know how to test for the presence of the bug. The
503 \hgcmd{bisect} command uses your test to direct its search for the
504 changeset that introduced the code that caused the bug.
506 Here are a few scenarios to help you understand how you might apply
507 this command.
508 \begin{itemize}
509 \item The most recent version of your software has a bug that you
510 remember wasn't present a few weeks ago, but you don't know when it
511 was introduced. Here, your binary test checks for the presence of
512 that bug.
513 \item You fixed a bug in a rush, and now it's time to close the entry
514 in your team's bug database. The bug database requires a changeset
515 ID when you close an entry, but you don't remember which changeset
516 you fixed the bug in. Once again, your binary test checks for the
517 presence of the bug.
518 \item Your software works correctly, but runs~15\% slower than the
519 last time you measured it. You want to know which changeset
520 introduced the performance regression. In this case, your binary
521 test measures the performance of your software, to see whether it's
522 ``fast'' or ``slow''.
523 \item The sizes of the components of your project that you ship
524 exploded recently, and you suspect that something changed in the way
525 you build your project.
526 \end{itemize}
528 From these examples, it should be clear that the \hgcmd{bisect}
529 command is not useful only for finding the sources of bugs. You can
530 use it to find any ``emergent property'' of a repository (anything
531 that you can't find from a simple text search of the files in the
532 tree) for which you can write a binary test.
534 We'll introduce a little bit of terminology here, just to make it
535 clear which parts of the search process are your responsibility, and
536 which are Mercurial's. A \emph{test} is something that \emph{you} run
537 when \hgcmd{bisect} chooses a changeset. A \emph{probe} is what
538 \hgcmd{bisect} runs to tell whether a revision is good. Finally,
539 we'll use the word ``bisect'', as both a noun and a verb, to stand in
540 for the phrase ``search using the \hgcmd{bisect} command.
542 One simple way to automate the searching process would be simply to
543 probe every changeset. However, this scales poorly. If it took ten
544 minutes to test a single changeset, and you had 10,000 changesets in
545 your repository, the exhaustive approach would take on average~35
546 \emph{days} to find the changeset that introduced a bug. Even if you
547 knew that the bug was introduced by one of the last 500 changesets,
548 and limited your search to those, you'd still be looking at over 40
549 hours to find the changeset that introduced your bug.
551 What the \hgcmd{bisect} command does is use its knowledge of the
552 ``shape'' of your project's revision history to perform a search in
553 time proportional to the \emph{logarithm} of the number of changesets
554 to check (the kind of search it performs is called a dichotomic
555 search). With this approach, searching through 10,000 changesets will
556 take less than three hours, even at ten minutes per test (the search
557 will require about 14 tests). Limit your search to the last hundred
558 changesets, and it will take only about an hour (roughly seven tests).
560 The \hgcmd{bisect} command is aware of the ``branchy'' nature of a
561 Mercurial project's revision history, so it has no problems dealing
562 with branches, merges, or multiple heads in a repoository. It can
563 prune entire branches of history with a single probe, which is how it
564 operates so efficiently.
566 \subsection{Using the \hgcmd{bisect} command}
568 Here's an example of \hgcmd{bisect} in action.
570 \begin{note}
571 In versions 0.9.5 and earlier of Mercurial, \hgcmd{bisect} was not a
572 core command: it was distributed with Mercurial as an extension.
573 This section describes the built-in command, not the old extension.
574 \end{note}
576 Now let's create a repository, so that we can try out the
577 \hgcmd{bisect} command in isolation.
578 \interaction{bisect.init}
579 We'll simulate a project that has a bug in it in a simple-minded way:
580 create trivial changes in a loop, and nominate one specific change
581 that will have the ``bug''. This loop creates 35 changesets, each
582 adding a single file to the repository. We'll represent our ``bug''
583 with a file that contains the text ``i have a gub''.
584 \interaction{bisect.commits}
586 The next thing that we'd like to do is figure out how to use the
587 \hgcmd{bisect} command. We can use Mercurial's normal built-in help
588 mechanism for this.
589 \interaction{bisect.help}
591 The \hgcmd{bisect} command works in steps. Each step proceeds as follows.
592 \begin{enumerate}
593 \item You run your binary test.
594 \begin{itemize}
595 \item If the test succeeded, you tell \hgcmd{bisect} by running the
596 \hgcmdargs{bisect}{good} command.
597 \item If it failed, run the \hgcmdargs{bisect}{--bad} command.
598 \end{itemize}
599 \item The command uses your information to decide which changeset to
600 test next.
601 \item It updates the working directory to that changeset, and the
602 process begins again.
603 \end{enumerate}
604 The process ends when \hgcmd{bisect} identifies a unique changeset
605 that marks the point where your test transitioned from ``succeeding''
606 to ``failing''.
608 To start the search, we must run the \hgcmdargs{bisect}{--reset} command.
609 \interaction{bisect.search.init}
611 In our case, the binary test we use is simple: we check to see if any
612 file in the repository contains the string ``i have a gub''. If it
613 does, this changeset contains the change that ``caused the bug''. By
614 convention, a changeset that has the property we're searching for is
615 ``bad'', while one that doesn't is ``good''.
617 Most of the time, the revision to which the working directory is
618 synced (usually the tip) already exhibits the problem introduced by
619 the buggy change, so we'll mark it as ``bad''.
620 \interaction{bisect.search.bad-init}
622 Our next task is to nominate a changeset that we know \emph{doesn't}
623 have the bug; the \hgcmd{bisect} command will ``bracket'' its search
624 between the first pair of good and bad changesets. In our case, we
625 know that revision~10 didn't have the bug. (I'll have more words
626 about choosing the first ``good'' changeset later.)
627 \interaction{bisect.search.good-init}
629 Notice that this command printed some output.
630 \begin{itemize}
631 \item It told us how many changesets it must consider before it can
632 identify the one that introduced the bug, and how many tests that
633 will require.
634 \item It updated the working directory to the next changeset to test,
635 and told us which changeset it's testing.
636 \end{itemize}
638 We now run our test in the working directory. We use the
639 \command{grep} command to see if our ``bad'' file is present in the
640 working directory. If it is, this revision is bad; if not, this
641 revision is good.
642 \interaction{bisect.search.step1}
644 This test looks like a perfect candidate for automation, so let's turn
645 it into a shell function.
646 \interaction{bisect.search.mytest}
647 We can now run an entire test step with a single command,
648 \texttt{mytest}.
649 \interaction{bisect.search.step2}
650 A few more invocations of our canned test step command, and we're
651 done.
652 \interaction{bisect.search.rest}
654 Even though we had~40 changesets to search through, the \hgcmd{bisect}
655 command let us find the changeset that introduced our ``bug'' with
656 only five tests. Because the number of tests that the \hgcmd{bisect}
657 command performs grows logarithmically with the number of changesets to
658 search, the advantage that it has over the ``brute force'' search
659 approach increases with every changeset you add.
661 \subsection{Cleaning up after your search}
663 When you're finished using the \hgcmd{bisect} command in a
664 repository, you can use the \hgcmdargs{bisect}{reset} command to drop
665 the information it was using to drive your search. The command
666 doesn't use much space, so it doesn't matter if you forget to run this
667 command. However, \hgcmd{bisect} won't let you start a new search in
668 that repository until you do a \hgcmdargs{bisect}{reset}.
669 \interaction{bisect.search.reset}
671 \section{Tips for finding bugs effectively}
673 \subsection{Give consistent input}
675 The \hgcmd{bisect} command requires that you correctly report the
676 result of every test you perform. If you tell it that a test failed
677 when it really succeeded, it \emph{might} be able to detect the
678 inconsistency. If it can identify an inconsistency in your reports,
679 it will tell you that a particular changeset is both good and bad.
680 However, it can't do this perfectly; it's about as likely to report
681 the wrong changeset as the source of the bug.
683 \subsection{Automate as much as possible}
685 When I started using the \hgcmd{bisect} command, I tried a few times
686 to run my tests by hand, on the command line. This is an approach
687 that I, at least, am not suited to. After a few tries, I found that I
688 was making enough mistakes that I was having to restart my searches
689 several times before finally getting correct results.
691 My initial problems with driving the \hgcmd{bisect} command by hand
692 occurred even with simple searches on small repositories; if the
693 problem you're looking for is more subtle, or the number of tests that
694 \hgcmd{bisect} must perform increases, the likelihood of operator
695 error ruining the search is much higher. Once I started automating my
696 tests, I had much better results.
698 The key to automated testing is twofold:
699 \begin{itemize}
700 \item always test for the same symptom, and
701 \item always feed consistent input to the \hgcmd{bisect} command.
702 \end{itemize}
703 In my tutorial example above, the \command{grep} command tests for the
704 symptom, and the \texttt{if} statement takes the result of this check
705 and ensures that we always feed the same input to the \hgcmd{bisect}
706 command. The \texttt{mytest} function marries these together in a
707 reproducible way, so that every test is uniform and consistent.
709 \subsection{Check your results}
711 Because the output of a \hgcmd{bisect} search is only as good as the
712 input you give it, don't take the changeset it reports as the
713 absolute truth. A simple way to cross-check its report is to manually
714 run your test at each of the following changesets:
715 \begin{itemize}
716 \item The changeset that it reports as the first bad revision. Your
717 test should still report this as bad.
718 \item The parent of that changeset (either parent, if it's a merge).
719 Your test should report this changeset as good.
720 \item A child of that changeset. Your test should report this
721 changeset as bad.
722 \end{itemize}
724 \subsection{Beware interference between bugs}
726 It's possible that your search for one bug could be disrupted by the
727 presence of another. For example, let's say your software crashes at
728 revision 100, and worked correctly at revision 50. Unknown to you,
729 someone else introduced a different crashing bug at revision 60, and
730 fixed it at revision 80. This could distort your results in one of
731 several ways.
733 It is possible that this other bug completely ``masks'' yours, which
734 is to say that it occurs before your bug has a chance to manifest
735 itself. If you can't avoid that other bug (for example, it prevents
736 your project from building), and so can't tell whether your bug is
737 present in a particular changeset, the \hgcmd{bisect} command cannot
738 help you directly. Instead, you can mark a changeset as untested by
739 running \hgcmdargs{bisect}{--skip}.
741 A different problem could arise if your test for a bug's presence is
742 not specific enough. If you check for ``my program crashes'', then
743 both your crashing bug and an unrelated crashing bug that masks it
744 will look like the same thing, and mislead \hgcmd{bisect}.
746 Another useful situation in which to use \hgcmdargs{bisect}{--skip} is
747 if you can't test a revision because your project was in a broken and
748 hence untestable state at that revision, perhaps because someone
749 checked in a change that prevented the project from building.
751 \subsection{Bracket your search lazily}
753 Choosing the first ``good'' and ``bad'' changesets that will mark the
754 end points of your search is often easy, but it bears a little
755 discussion nevertheless. From the perspective of \hgcmd{bisect}, the
756 ``newest'' changeset is conventionally ``bad'', and the older
757 changeset is ``good''.
759 If you're having trouble remembering when a suitable ``good'' change
760 was, so that you can tell \hgcmd{bisect}, you could do worse than
761 testing changesets at random. Just remember to eliminate contenders
762 that can't possibly exhibit the bug (perhaps because the feature with
763 the bug isn't present yet) and those where another problem masks the
764 bug (as I discussed above).
766 Even if you end up ``early'' by thousands of changesets or months of
767 history, you will only add a handful of tests to the total number that
768 \hgcmd{bisect} must perform, thanks to its logarithmic behaviour.
770 %%% Local Variables:
771 %%% mode: latex
772 %%% TeX-master: "00book"
773 %%% End: