comparison en/mq.tex @ 15:b8ac9f312a47

Document wiggle and rej
author Bryan O'Sullivan <bos@serpentine.com>
date Mon, 03 Jul 2006 17:58:29 -0700
parents e2aa527bafa0
children 81454425eee9
comparison
equal deleted inserted replaced
14:e2aa527bafa0 15:b8ac9f312a47
27 Distributors of operating systems that include open source software 27 Distributors of operating systems that include open source software
28 often need to make changes to the packages they distribute so that 28 often need to make changes to the packages they distribute so that
29 they will build properly in their environments. 29 they will build properly in their environments.
30 30
31 When you have few changes to maintain, it is easy to manage a single 31 When you have few changes to maintain, it is easy to manage a single
32 patch using the standard \texttt{diff} and \texttt{patch} programs. 32 patch using the standard \texttt{diff} and \texttt{patch} programs
33 (see section~\ref{sec:mq:patch} for a discussion of these tools).
33 Once the number of changes grows, it starts to makes sense to maintain 34 Once the number of changes grows, it starts to makes sense to maintain
34 patches as discrete ``chunks of work,'' so that for example a single 35 patches as discrete ``chunks of work,'' so that for example a single
35 patch will contain only one bug fix (the patch might modify several 36 patch will contain only one bug fix (the patch might modify several
36 files, but it's doing ``only one thing''), and you may have a number 37 files, but it's doing ``only one thing''), and you may have a number
37 of such patches for different bugs you need fixed and local changes 38 of such patches for different bugs you need fixed and local changes
317 \hgcmd{qpop} down to the core patch. Fix the core bug, 318 \hgcmd{qpop} down to the core patch. Fix the core bug,
318 \hgcmd{qrefresh} the core patch, and \hgcmd{qpush} back to the UI 319 \hgcmd{qrefresh} the core patch, and \hgcmd{qpush} back to the UI
319 patch to continue where you left off. 320 patch to continue where you left off.
320 321
321 \section{Mercurial Queues and GNU patch} 322 \section{Mercurial Queues and GNU patch}
323 \label{sec:mq:patch}
322 324
323 MQ uses the GNU \command{patch} command to apply patches. It will 325 MQ uses the GNU \command{patch} command to apply patches. It will
324 help you to understand the data that MQ and \command{patch} work with, 326 help you to understand the data that MQ and \command{patch} work with,
325 and a few aspects of how \command{patch} operates. 327 and a few aspects of how \command{patch} operates.
328
329 The \command{diff} command generates a list of modifications by
330 comparing two files. The \command{patch} command applies a list of
331 modifications to a file. The kinds of files that \command{diff} and
332 \command{patch} work with are referred to as both ``diffs'' and
333 ``patches;'' there is no difference between a diff and a patch.
326 334
327 A patch file can start with arbitrary text; MQ uses this text as the 335 A patch file can start with arbitrary text; MQ uses this text as the
328 commit message when creating changesets. It treats the first line 336 commit message when creating changesets. It treats the first line
329 that starts with the string ``\texttt{diff~-}'' as the separator 337 that starts with the string ``\texttt{diff~-}'' as the separator
330 between header and content. 338 between header and content.
331 339
332 MQ works with \emph{unified diffs} (\command{patch} can accept several 340 MQ works with \emph{unified} diffs (\command{patch} can accept several
333 other kinds of diff, but MQ doesn't). A unified diff contains two 341 other diff formats, but MQ doesn't). A unified diff contains two
334 kinds of header. The \emph{file header} describes the file being 342 kinds of header. The \emph{file header} describes the file being
335 modified; it contains the name of the file to modify. When 343 modified; it contains the name of the file to modify. When
336 \command{patch} sees a new file header, it looks for a file of that 344 \command{patch} sees a new file header, it looks for a file with that
337 name to start modifying. 345 name to start modifying.
338 346
339 After the file header come a series of \emph{hunks}. Each hunk starts 347 After the file header comes a series of \emph{hunks}. Each hunk
340 with a header; this identifies the range of line numbers within the 348 starts with a header; this identifies the range of line numbers within
341 file that the hunk should modify. Following the header, a hunk starts 349 the file that the hunk should modify. Following the header, a hunk
342 and ends with a few lines of text from the unmodified file; these are 350 starts and ends with a few (usually three) lines of text from the
343 called the \emph{context} for the hunk. Each unmodified line begins 351 unmodified file; these are called the \emph{context} for the hunk.
344 with a space characters. Within the hunk, a line that begins with 352 Each unmodified line begins with a space characters. Within the hunk,
345 ``\texttt{-}'' means ``remove this line,'' while a line that begins 353 a line that begins with ``\texttt{-}'' means ``remove this line,''
346 with ``\texttt{+}'' means ``insert this line.'' For example, a line 354 while a line that begins with ``\texttt{+}'' means ``insert this
347 that is modified is represented by one deletion and one insertion. 355 line.'' For example, a line that is modified is represented by one
356 deletion and one insertion.
357
358 The \command{diff} command runs hunks together when there's not enough
359 context between modifications to justify
348 360
349 When \command{patch} applies a hunk, it tries a handful of 361 When \command{patch} applies a hunk, it tries a handful of
350 successively less accurate strategies to try to make the hunk apply. 362 successively less accurate strategies to try to make the hunk apply.
351 This falling-back technique often makes it possible to take a patch 363 This falling-back technique often makes it possible to take a patch
352 that was generated against an old version of a file, and apply it 364 that was generated against an old version of a file, and apply it
367 \command{patch} had to trim before the patch applied). 379 \command{patch} had to trim before the patch applied).
368 380
369 When neither of these techniques works, \command{patch} prints a 381 When neither of these techniques works, \command{patch} prints a
370 message saying that the hunk in question was rejected. It saves 382 message saying that the hunk in question was rejected. It saves
371 rejected hunks to a file with the same name, and an added 383 rejected hunks to a file with the same name, and an added
372 \filename{.rej} extension. If \hgcmd{qpush} fails to apply a patch, 384 \filename{.rej} extension. It also saves an unmodified copy of the
373 it will print an error message and exit. 385 file with a \filename{.orig} extension; the copy of the file without
386 any extensions will contain any changes made by hunks that \emph{did}
387 apply cleanly. If you have a patch that modifies \filename{foo} with
388 six hunks, and one of them fails to apply, you will have: an
389 unmodified \filename{foo.orig}, a \filename{foo.rej} containing one
390 hunk, and \filename{foo}, containing the changes made by the five
391 successful five hunks.
374 392
375 \subsection{Beware the fuzz} 393 \subsection{Beware the fuzz}
376 394
377 While applying a hunk at an offset, or with a fuzz factor, will often 395 While applying a hunk at an offset, or with a fuzz factor, will often
378 be completely successful, these inexact techniques naturally leave 396 be completely successful, these inexact techniques naturally leave
390 cases, such as when you're maintaining a patch that must sit on top of 408 cases, such as when you're maintaining a patch that must sit on top of
391 multiple versions of a source tree, it's acceptable to have a patch 409 multiple versions of a source tree, it's acceptable to have a patch
392 apply with some fuzz, provided you've verified the results of the 410 apply with some fuzz, provided you've verified the results of the
393 patching process in such cases. 411 patching process in such cases.
394 412
413 \subsection{Handling rejection}
414
415 If \hgcmd{qpush} fails to apply a patch, it will print an error
416 message and exit. If it has left \filename{.rej} files behind, it is
417 usually best to fix up the rejected hunks before you push more patches
418 or do any further work.
419
420 If your patch \emph{used to} apply cleanly, and no longer does because
421 you've changed the underlying code that your patches are based on,
422 Mercurial Queues can help; see section~\ref{seq:mq:merge} for details.
423
424 Unfortunately, there aren't any great techniques for dealing with
425 rejected hunks. Most often, you'll need to view the \filename{.rej}
426 file and edit the target file, applying the rejected hunks by hand.
427
428 If you're feeling adventurous, Neil Brown, an Australian Linux kernel
429 hacker, has written a tool called \command{wiggle}~\cite{web:wiggle},
430 which is more vigorous than \command{patch} in its attempts to make a
431 patch apply.
432
433 Another Linux kernel hacker, Chris Mason (the author of Mercurial
434 Queues), wrote a similar tool called \command{rej}~\cite{web:rej},
435 which takes a simple approach to automating the application of hunks
436 rejected by \command{patch}. \command{rej} can help with four common
437 reasons that a hunk may be rejected:
438
439 \begin{itemize}
440 \item The context in the middle of a hunk has changed.
441 \item A hunk is missing some context at the beginning or end.
442 \item A large hunk might apply better--either entirely or in part--if
443 it was broken up into smaller hunks.
444 \item A hunk removes lines with slightly different content than those
445 currently present in the file.
446 \end{itemize}
447
448 If you use \command{wiggle} or \command{rej}, you should be doubly
449 careful to check your results when you're done.
450
451 \section{Updating your patches when the underlying code changes}
452 \label{sec:mq:merge}
453
454 XXX.
395 455
396 %%% Local Variables: 456 %%% Local Variables:
397 %%% mode: latex 457 %%% mode: latex
398 %%% TeX-master: "00book" 458 %%% TeX-master: "00book"
399 %%% End: 459 %%% End: