hgbook

view es/undo.tex @ 387:7864f2264e28

translated some paragraphs of undo.tex
author Igor TAmara <igor@tamarapatino.org>
date Sun Nov 02 19:24:30 2008 -0500 (2008-11-02)
parents d2467817c934
children 9fdb45b994d4
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 al revertir un renombramiento se constituyen
212 discutiblemente en un fallo de Mercurial.
214 \section{Tratar cambios consignados}
216 Considere un caso en el que ha consignado el cambio $a$, y otro cambio
217 $b$ sobre este; se ha dado cuenta que el cambio $a$ era
218 incorrecto. Mercurial le permite ``retroceder'' un conjunto de cambios
219 completo automáticamente, y construir bloques que le permitan revertir
220 parte de un conjunto de cambios a mano.
222 Antes de leer esta sección, hay algo para tener en cuenta: la orden
223 \hgcmd{backout} deshace cambios \emph{adicionando} a la historia, sin
224 modificar o borrar. Es la herramienta correcta si está arreglando
225 fallos, pero no si está tratando de deshacer algún cambio que tiene
226 consecuencias catastróficas. Para tratar con esos, vea la sección~\ref{sec:undo:aaaiiieee}.
228 \subsection{Retroceder un conjunto de cambios}
230 La orden \hgcmd{backout} le permite ``deshacer'' los efectos de todo
231 un conjunto de cambios de forma automatizada. Dado que la historia de
232 Mercurial es inmutable, esta orden \emph{no} se deshace del conjunto
233 de cambios que usted desea deshacer. En cambio, crea un nuevo
234 conjunto de cambios que \emph{reversa} el conjunto de cambios que
235 usted indique.
237 La operación de la orden \hgcmd{backout} es un poco intrincada, y lo
238 ilustraremos con algunos ejemplos. Primero crearemos un repositorio
239 con algunos cambios sencillos.
240 \interaction{backout.init}
242 La orden \hgcmd{backout} toma un ID de conjunto de cambios como su
243 argumento; el conjunto de cambios a retroceder. Normalmente
244 \hgcmd{backout} le ofrecerá un editor de texto para escribir el
245 mensaje de la consignación, para dejar un registro de por qué está
246 retrocediendo. En este ejemplo, colocamos un mensaje en la
247 consignación usando la opción \hgopt{backout}{-m} .
249 \subsection{Retroceder el conjunto de cambios tip}
251 Comenzamos retrocediendo el último conjunto de cambios que consignamos.
252 \interaction{backout.simple}
253 Puede ver que la segunda línea de \filename{myfile} ya no está
254 presente. La salida de \hgcmd{log} nos da una idea de lo que la orden
255 \hgcmd{backout} ha hecho.
256 \interaction{backout.simple.log}
257 Vea que el nuevo conjunto de cambios que \hgcmd{backout} ha creado es
258 un hijo del conjunto de cambios que retrocedimos. Es más sencillo de
259 ver en la figura~\ref{fig:undo:backout}, que presenta una vista
260 gráfica de la historia de cambios. Como puede ver, la historia es
261 bonita y lineal.
263 \begin{figure}[htb]
264 \centering
265 \grafix{undo-simple}
266 \caption{Retroceso de un cambio con la orden \hgcmd{backout}}
267 \label{fig:undo:backout}
268 \end{figure}
270 \subsection{Retroceso de un cambio que no es tip}
272 Si desea retrocede un cambio distinto al último que ha consignado, use
273 la opción \hgopt{backout}{--merge} a la orden \hgcmd{backout}.
274 \interaction{backout.non-tip.clone}
275 Que resulta en un retroceso de un conjunto de cambios ``en un sólo
276 tiro'', una operación que resulta normalmente rápida y sencilla.
277 \interaction{backout.non-tip.backout}
279 Si ve los contenidos del fichero \filename{myfile} después de
280 finalizar el retroceso, verá que el primer y el tercer cambio están
281 presentes, pero no el segundo.
282 \interaction{backout.non-tip.cat}
284 Como lo muestra la historia gráfica en la
285 figura~\ref{fig:undo:backout-non-tip}, Mercurial realmente consigna
286 \emph{dos} cambios en estas situaciones (los nodos encerrados en una
287 caja son aquellos que Mercurial consigna automaticamente). Antes de
288 que Mercurial comience el proceso de retroceso, primero recuerda cuál
289 es el padre del directorio de trabajo. Posteriormente hace un
290 retroceso al conjunto de cambios objetivo y lo consigna como un
291 conjunto de cambios. Finalmente, fusiona con el padre anterior del
292 directorio de trabajo, y consigna el resultado de la fusión.
294 % TODO: to me it looks like mercurial doesn't commit the second merge automatically!
296 \begin{figure}[htb]
297 \centering
298 \grafix{undo-non-tip}
299 \caption{Retroceso automatizado de un cambio a algo que no es tip con la orden \hgcmd{backout}}
300 \label{fig:undo:backout-non-tip}
301 \end{figure}
303 El resultado es que usted termina ``donde estaba'', solamente con un
304 poco de historia adicional que deshace el efecto de un conjunto de
305 cambios que usted quería evitar.
307 \subsubsection{Always use the \hgopt{backout}{--merge} option}
309 In fact, since the \hgopt{backout}{--merge} option will do the ``right
310 thing'' whether or not the changeset you're backing out is the tip
311 (i.e.~it won't try to merge if it's backing out the tip, since there's
312 no need), you should \emph{always} use this option when you run the
313 \hgcmd{backout} command.
315 \subsection{Gaining more control of the backout process}
317 While I've recommended that you always use the
318 \hgopt{backout}{--merge} option when backing out a change, the
319 \hgcmd{backout} command lets you decide how to merge a backout
320 changeset. Taking control of the backout process by hand is something
321 you will rarely need to do, but it can be useful to understand what
322 the \hgcmd{backout} command is doing for you automatically. To
323 illustrate this, let's clone our first repository, but omit the
324 backout change that it contains.
326 \interaction{backout.manual.clone}
327 As with our earlier example, We'll commit a third changeset, then back
328 out its parent, and see what happens.
329 \interaction{backout.manual.backout}
330 Our new changeset is again a descendant of the changeset we backout
331 out; it's thus a new head, \emph{not} a descendant of the changeset
332 that was the tip. The \hgcmd{backout} command was quite explicit in
333 telling us this.
334 \interaction{backout.manual.log}
336 Again, it's easier to see what has happened by looking at a graph of
337 the revision history, in figure~\ref{fig:undo:backout-manual}. This
338 makes it clear that when we use \hgcmd{backout} to back out a change
339 other than the tip, Mercurial adds a new head to the repository (the
340 change it committed is box-shaped).
342 \begin{figure}[htb]
343 \centering
344 \grafix{undo-manual}
345 \caption{Backing out a change using the \hgcmd{backout} command}
346 \label{fig:undo:backout-manual}
347 \end{figure}
349 After the \hgcmd{backout} command has completed, it leaves the new
350 ``backout'' changeset as the parent of the working directory.
351 \interaction{backout.manual.parents}
352 Now we have two isolated sets of changes.
353 \interaction{backout.manual.heads}
355 Let's think about what we expect to see as the contents of
356 \filename{myfile} now. The first change should be present, because
357 we've never backed it out. The second change should be missing, as
358 that's the change we backed out. Since the history graph shows the
359 third change as a separate head, we \emph{don't} expect to see the
360 third change present in \filename{myfile}.
361 \interaction{backout.manual.cat}
362 To get the third change back into the file, we just do a normal merge
363 of our two heads.
364 \interaction{backout.manual.merge}
365 Afterwards, the graphical history of our repository looks like
366 figure~\ref{fig:undo:backout-manual-merge}.
368 \begin{figure}[htb]
369 \centering
370 \grafix{undo-manual-merge}
371 \caption{Manually merging a backout change}
372 \label{fig:undo:backout-manual-merge}
373 \end{figure}
375 \subsection{Why \hgcmd{backout} works as it does}
377 Here's a brief description of how the \hgcmd{backout} command works.
378 \begin{enumerate}
379 \item It ensures that the working directory is ``clean'', i.e.~that
380 the output of \hgcmd{status} would be empty.
381 \item It remembers the current parent of the working directory. Let's
382 call this changeset \texttt{orig}
383 \item It does the equivalent of a \hgcmd{update} to sync the working
384 directory to the changeset you want to back out. Let's call this
385 changeset \texttt{backout}
386 \item It finds the parent of that changeset. Let's call that
387 changeset \texttt{parent}.
388 \item For each file that the \texttt{backout} changeset affected, it
389 does the equivalent of a \hgcmdargs{revert}{-r parent} on that file,
390 to restore it to the contents it had before that changeset was
391 committed.
392 \item It commits the result as a new changeset. This changeset has
393 \texttt{backout} as its parent.
394 \item If you specify \hgopt{backout}{--merge} on the command line, it
395 merges with \texttt{orig}, and commits the result of the merge.
396 \end{enumerate}
398 An alternative way to implement the \hgcmd{backout} command would be
399 to \hgcmd{export} the to-be-backed-out changeset as a diff, then use
400 the \cmdopt{patch}{--reverse} option to the \command{patch} command to
401 reverse the effect of the change without fiddling with the working
402 directory. This sounds much simpler, but it would not work nearly as
403 well.
405 The reason that \hgcmd{backout} does an update, a commit, a merge, and
406 another commit is to give the merge machinery the best chance to do a
407 good job when dealing with all the changes \emph{between} the change
408 you're backing out and the current tip.
410 If you're backing out a changeset that's~100 revisions back in your
411 project's history, the chances that the \command{patch} command will
412 be able to apply a reverse diff cleanly are not good, because
413 intervening changes are likely to have ``broken the context'' that
414 \command{patch} uses to determine whether it can apply a patch (if
415 this sounds like gibberish, see \ref{sec:mq:patch} for a
416 discussion of the \command{patch} command). Also, Mercurial's merge
417 machinery will handle files and directories being renamed, permission
418 changes, and modifications to binary files, none of which
419 \command{patch} can deal with.
421 \section{Changes that should never have been}
422 \label{sec:undo:aaaiiieee}
424 Most of the time, the \hgcmd{backout} command is exactly what you need
425 if you want to undo the effects of a change. It leaves a permanent
426 record of exactly what you did, both when committing the original
427 changeset and when you cleaned up after it.
429 On rare occasions, though, you may find that you've committed a change
430 that really should not be present in the repository at all. For
431 example, it would be very unusual, and usually considered a mistake,
432 to commit a software project's object files as well as its source
433 files. Object files have almost no intrinsic value, and they're
434 \emph{big}, so they increase the size of the repository and the amount
435 of time it takes to clone or pull changes.
437 Before I discuss the options that you have if you commit a ``brown
438 paper bag'' change (the kind that's so bad that you want to pull a
439 brown paper bag over your head), let me first discuss some approaches
440 that probably won't work.
442 Since Mercurial treats history as accumulative---every change builds
443 on top of all changes that preceded it---you generally can't just make
444 disastrous changes disappear. The one exception is when you've just
445 committed a change, and it hasn't been pushed or pulled into another
446 repository. That's when you can safely use the \hgcmd{rollback}
447 command, as I detailed in section~\ref{sec:undo:rollback}.
449 After you've pushed a bad change to another repository, you
450 \emph{could} still use \hgcmd{rollback} to make your local copy of the
451 change disappear, but it won't have the consequences you want. The
452 change will still be present in the remote repository, so it will
453 reappear in your local repository the next time you pull.
455 If a situation like this arises, and you know which repositories your
456 bad change has propagated into, you can \emph{try} to get rid of the
457 changeefrom \emph{every} one of those repositories. This is, of
458 course, not a satisfactory solution: if you miss even a single
459 repository while you're expunging, the change is still ``in the
460 wild'', and could propagate further.
462 If you've committed one or more changes \emph{after} the change that
463 you'd like to see disappear, your options are further reduced.
464 Mercurial doesn't provide a way to ``punch a hole'' in history,
465 leaving changesets intact.
467 XXX This needs filling out. The \texttt{hg-replay} script in the
468 \texttt{examples} directory works, but doesn't handle merge
469 changesets. Kind of an important omission.
471 \subsection{Protect yourself from ``escaped'' changes}
473 If you've committed some changes to your local repository and they've
474 been pushed or pulled somewhere else, this isn't necessarily a
475 disaster. You can protect yourself ahead of time against some classes
476 of bad changeset. This is particularly easy if your team usually
477 pulls changes from a central repository.
479 By configuring some hooks on that repository to validate incoming
480 changesets (see chapter~\ref{chap:hook}), you can automatically
481 prevent some kinds of bad changeset from being pushed to the central
482 repository at all. With such a configuration in place, some kinds of
483 bad changeset will naturally tend to ``die out'' because they can't
484 propagate into the central repository. Better yet, this happens
485 without any need for explicit intervention.
487 For instance, an incoming change hook that verifies that a changeset
488 will actually compile can prevent people from inadvertantly ``breaking
489 the build''.
491 \section{Finding the source of a bug}
492 \label{sec:undo:bisect}
494 While it's all very well to be able to back out a changeset that
495 introduced a bug, this requires that you know which changeset to back
496 out. Mercurial provides an invaluable command, called
497 \hgcmd{bisect}, that helps you to automate this process and accomplish
498 it very efficiently.
500 The idea behind the \hgcmd{bisect} command is that a changeset has
501 introduced some change of behaviour that you can identify with a
502 simple binary test. You don't know which piece of code introduced the
503 change, but you know how to test for the presence of the bug. The
504 \hgcmd{bisect} command uses your test to direct its search for the
505 changeset that introduced the code that caused the bug.
507 Here are a few scenarios to help you understand how you might apply
508 this command.
509 \begin{itemize}
510 \item The most recent version of your software has a bug that you
511 remember wasn't present a few weeks ago, but you don't know when it
512 was introduced. Here, your binary test checks for the presence of
513 that bug.
514 \item You fixed a bug in a rush, and now it's time to close the entry
515 in your team's bug database. The bug database requires a changeset
516 ID when you close an entry, but you don't remember which changeset
517 you fixed the bug in. Once again, your binary test checks for the
518 presence of the bug.
519 \item Your software works correctly, but runs~15\% slower than the
520 last time you measured it. You want to know which changeset
521 introduced the performance regression. In this case, your binary
522 test measures the performance of your software, to see whether it's
523 ``fast'' or ``slow''.
524 \item The sizes of the components of your project that you ship
525 exploded recently, and you suspect that something changed in the way
526 you build your project.
527 \end{itemize}
529 From these examples, it should be clear that the \hgcmd{bisect}
530 command is not useful only for finding the sources of bugs. You can
531 use it to find any ``emergent property'' of a repository (anything
532 that you can't find from a simple text search of the files in the
533 tree) for which you can write a binary test.
535 We'll introduce a little bit of terminology here, just to make it
536 clear which parts of the search process are your responsibility, and
537 which are Mercurial's. A \emph{test} is something that \emph{you} run
538 when \hgcmd{bisect} chooses a changeset. A \emph{probe} is what
539 \hgcmd{bisect} runs to tell whether a revision is good. Finally,
540 we'll use the word ``bisect'', as both a noun and a verb, to stand in
541 for the phrase ``search using the \hgcmd{bisect} command.
543 One simple way to automate the searching process would be simply to
544 probe every changeset. However, this scales poorly. If it took ten
545 minutes to test a single changeset, and you had 10,000 changesets in
546 your repository, the exhaustive approach would take on average~35
547 \emph{days} to find the changeset that introduced a bug. Even if you
548 knew that the bug was introduced by one of the last 500 changesets,
549 and limited your search to those, you'd still be looking at over 40
550 hours to find the changeset that introduced your bug.
552 What the \hgcmd{bisect} command does is use its knowledge of the
553 ``shape'' of your project's revision history to perform a search in
554 time proportional to the \emph{logarithm} of the number of changesets
555 to check (the kind of search it performs is called a dichotomic
556 search). With this approach, searching through 10,000 changesets will
557 take less than three hours, even at ten minutes per test (the search
558 will require about 14 tests). Limit your search to the last hundred
559 changesets, and it will take only about an hour (roughly seven tests).
561 The \hgcmd{bisect} command is aware of the ``branchy'' nature of a
562 Mercurial project's revision history, so it has no problems dealing
563 with branches, merges, or multiple heads in a repoository. It can
564 prune entire branches of history with a single probe, which is how it
565 operates so efficiently.
567 \subsection{Using the \hgcmd{bisect} command}
569 Here's an example of \hgcmd{bisect} in action.
571 \begin{note}
572 In versions 0.9.5 and earlier of Mercurial, \hgcmd{bisect} was not a
573 core command: it was distributed with Mercurial as an extension.
574 This section describes the built-in command, not the old extension.
575 \end{note}
577 Now let's create a repository, so that we can try out the
578 \hgcmd{bisect} command in isolation.
579 \interaction{bisect.init}
580 We'll simulate a project that has a bug in it in a simple-minded way:
581 create trivial changes in a loop, and nominate one specific change
582 that will have the ``bug''. This loop creates 35 changesets, each
583 adding a single file to the repository. We'll represent our ``bug''
584 with a file that contains the text ``i have a gub''.
585 \interaction{bisect.commits}
587 The next thing that we'd like to do is figure out how to use the
588 \hgcmd{bisect} command. We can use Mercurial's normal built-in help
589 mechanism for this.
590 \interaction{bisect.help}
592 The \hgcmd{bisect} command works in steps. Each step proceeds as follows.
593 \begin{enumerate}
594 \item You run your binary test.
595 \begin{itemize}
596 \item If the test succeeded, you tell \hgcmd{bisect} by running the
597 \hgcmdargs{bisect}{good} command.
598 \item If it failed, run the \hgcmdargs{bisect}{--bad} command.
599 \end{itemize}
600 \item The command uses your information to decide which changeset to
601 test next.
602 \item It updates the working directory to that changeset, and the
603 process begins again.
604 \end{enumerate}
605 The process ends when \hgcmd{bisect} identifies a unique changeset
606 that marks the point where your test transitioned from ``succeeding''
607 to ``failing''.
609 To start the search, we must run the \hgcmdargs{bisect}{--reset} command.
610 \interaction{bisect.search.init}
612 In our case, the binary test we use is simple: we check to see if any
613 file in the repository contains the string ``i have a gub''. If it
614 does, this changeset contains the change that ``caused the bug''. By
615 convention, a changeset that has the property we're searching for is
616 ``bad'', while one that doesn't is ``good''.
618 Most of the time, the revision to which the working directory is
619 synced (usually the tip) already exhibits the problem introduced by
620 the buggy change, so we'll mark it as ``bad''.
621 \interaction{bisect.search.bad-init}
623 Our next task is to nominate a changeset that we know \emph{doesn't}
624 have the bug; the \hgcmd{bisect} command will ``bracket'' its search
625 between the first pair of good and bad changesets. In our case, we
626 know that revision~10 didn't have the bug. (I'll have more words
627 about choosing the first ``good'' changeset later.)
628 \interaction{bisect.search.good-init}
630 Notice that this command printed some output.
631 \begin{itemize}
632 \item It told us how many changesets it must consider before it can
633 identify the one that introduced the bug, and how many tests that
634 will require.
635 \item It updated the working directory to the next changeset to test,
636 and told us which changeset it's testing.
637 \end{itemize}
639 We now run our test in the working directory. We use the
640 \command{grep} command to see if our ``bad'' file is present in the
641 working directory. If it is, this revision is bad; if not, this
642 revision is good.
643 \interaction{bisect.search.step1}
645 This test looks like a perfect candidate for automation, so let's turn
646 it into a shell function.
647 \interaction{bisect.search.mytest}
648 We can now run an entire test step with a single command,
649 \texttt{mytest}.
650 \interaction{bisect.search.step2}
651 A few more invocations of our canned test step command, and we're
652 done.
653 \interaction{bisect.search.rest}
655 Even though we had~40 changesets to search through, the \hgcmd{bisect}
656 command let us find the changeset that introduced our ``bug'' with
657 only five tests. Because the number of tests that the \hgcmd{bisect}
658 command performs grows logarithmically with the number of changesets to
659 search, the advantage that it has over the ``brute force'' search
660 approach increases with every changeset you add.
662 \subsection{Cleaning up after your search}
664 When you're finished using the \hgcmd{bisect} command in a
665 repository, you can use the \hgcmdargs{bisect}{reset} command to drop
666 the information it was using to drive your search. The command
667 doesn't use much space, so it doesn't matter if you forget to run this
668 command. However, \hgcmd{bisect} won't let you start a new search in
669 that repository until you do a \hgcmdargs{bisect}{reset}.
670 \interaction{bisect.search.reset}
672 \section{Tips for finding bugs effectively}
674 \subsection{Give consistent input}
676 The \hgcmd{bisect} command requires that you correctly report the
677 result of every test you perform. If you tell it that a test failed
678 when it really succeeded, it \emph{might} be able to detect the
679 inconsistency. If it can identify an inconsistency in your reports,
680 it will tell you that a particular changeset is both good and bad.
681 However, it can't do this perfectly; it's about as likely to report
682 the wrong changeset as the source of the bug.
684 \subsection{Automate as much as possible}
686 When I started using the \hgcmd{bisect} command, I tried a few times
687 to run my tests by hand, on the command line. This is an approach
688 that I, at least, am not suited to. After a few tries, I found that I
689 was making enough mistakes that I was having to restart my searches
690 several times before finally getting correct results.
692 My initial problems with driving the \hgcmd{bisect} command by hand
693 occurred even with simple searches on small repositories; if the
694 problem you're looking for is more subtle, or the number of tests that
695 \hgcmd{bisect} must perform increases, the likelihood of operator
696 error ruining the search is much higher. Once I started automating my
697 tests, I had much better results.
699 The key to automated testing is twofold:
700 \begin{itemize}
701 \item always test for the same symptom, and
702 \item always feed consistent input to the \hgcmd{bisect} command.
703 \end{itemize}
704 In my tutorial example above, the \command{grep} command tests for the
705 symptom, and the \texttt{if} statement takes the result of this check
706 and ensures that we always feed the same input to the \hgcmd{bisect}
707 command. The \texttt{mytest} function marries these together in a
708 reproducible way, so that every test is uniform and consistent.
710 \subsection{Check your results}
712 Because the output of a \hgcmd{bisect} search is only as good as the
713 input you give it, don't take the changeset it reports as the
714 absolute truth. A simple way to cross-check its report is to manually
715 run your test at each of the following changesets:
716 \begin{itemize}
717 \item The changeset that it reports as the first bad revision. Your
718 test should still report this as bad.
719 \item The parent of that changeset (either parent, if it's a merge).
720 Your test should report this changeset as good.
721 \item A child of that changeset. Your test should report this
722 changeset as bad.
723 \end{itemize}
725 \subsection{Beware interference between bugs}
727 It's possible that your search for one bug could be disrupted by the
728 presence of another. For example, let's say your software crashes at
729 revision 100, and worked correctly at revision 50. Unknown to you,
730 someone else introduced a different crashing bug at revision 60, and
731 fixed it at revision 80. This could distort your results in one of
732 several ways.
734 It is possible that this other bug completely ``masks'' yours, which
735 is to say that it occurs before your bug has a chance to manifest
736 itself. If you can't avoid that other bug (for example, it prevents
737 your project from building), and so can't tell whether your bug is
738 present in a particular changeset, the \hgcmd{bisect} command cannot
739 help you directly. Instead, you can mark a changeset as untested by
740 running \hgcmdargs{bisect}{--skip}.
742 A different problem could arise if your test for a bug's presence is
743 not specific enough. If you check for ``my program crashes'', then
744 both your crashing bug and an unrelated crashing bug that masks it
745 will look like the same thing, and mislead \hgcmd{bisect}.
747 Another useful situation in which to use \hgcmdargs{bisect}{--skip} is
748 if you can't test a revision because your project was in a broken and
749 hence untestable state at that revision, perhaps because someone
750 checked in a change that prevented the project from building.
752 \subsection{Bracket your search lazily}
754 Choosing the first ``good'' and ``bad'' changesets that will mark the
755 end points of your search is often easy, but it bears a little
756 discussion nevertheless. From the perspective of \hgcmd{bisect}, the
757 ``newest'' changeset is conventionally ``bad'', and the older
758 changeset is ``good''.
760 If you're having trouble remembering when a suitable ``good'' change
761 was, so that you can tell \hgcmd{bisect}, you could do worse than
762 testing changesets at random. Just remember to eliminate contenders
763 that can't possibly exhibit the bug (perhaps because the feature with
764 the bug isn't present yet) and those where another problem masks the
765 bug (as I discussed above).
767 Even if you end up ``early'' by thousands of changesets or months of
768 history, you will only add a handful of tests to the total number that
769 \hgcmd{bisect} must perform, thanks to its logarithmic behaviour.
771 %%% Local Variables:
772 %%% mode: latex
773 %%% TeX-master: "00book"
774 %%% End: