1 | \input texinfo @c -*-texinfo-*- |
---|
2 | @c %**start of header |
---|
3 | @setfilename diff.info |
---|
4 | @settitle Comparing and Merging Files |
---|
5 | @setchapternewpage odd |
---|
6 | @c %**end of header |
---|
7 | |
---|
8 | @ifinfo |
---|
9 | This file documents the the GNU @code{diff}, @code{diff3}, @code{sdiff}, |
---|
10 | and @code{cmp} commands for showing the differences between text files |
---|
11 | and the @code{patch} command for using their output to update files. |
---|
12 | |
---|
13 | Copyright (C) 1992, 1993, 1994 Free Software Foundation, Inc. |
---|
14 | |
---|
15 | Permission is granted to make and distribute verbatim copies of |
---|
16 | this manual provided the copyright notice and this permission notice |
---|
17 | are preserved on all copies. |
---|
18 | |
---|
19 | @ignore |
---|
20 | Permission is granted to process this file through TeX and print the |
---|
21 | results, provided the printed document carries copying permission |
---|
22 | notice identical to this one except for the removal of this paragraph |
---|
23 | (this paragraph not being relevant to the printed manual). |
---|
24 | |
---|
25 | @end ignore |
---|
26 | Permission is granted to copy and distribute modified versions of this |
---|
27 | manual under the conditions for verbatim copying, provided that the entire |
---|
28 | resulting derived work is distributed under the terms of a permission |
---|
29 | notice identical to this one. |
---|
30 | |
---|
31 | Permission is granted to copy and distribute translations of this manual |
---|
32 | into another language, under the above conditions for modified versions, |
---|
33 | except that this permission notice may be stated in a translation approved |
---|
34 | by the Foundation. |
---|
35 | @end ifinfo |
---|
36 | |
---|
37 | @titlepage |
---|
38 | @title Comparing and Merging Files |
---|
39 | @subtitle @code{diff}, @code{diff3}, @code{sdiff}, @code{cmp}, and @code{patch} |
---|
40 | @subtitle Edition 1.3, for @code{diff} 2.5 and @code{patch} 2.1 |
---|
41 | @subtitle September 1993 |
---|
42 | @author by David MacKenzie, Paul Eggert, and Richard Stallman |
---|
43 | |
---|
44 | @page |
---|
45 | @vskip 0pt plus 1filll |
---|
46 | Copyright @copyright{} 1992, 1993, 1994 Free Software Foundation, Inc. |
---|
47 | |
---|
48 | Permission is granted to make and distribute verbatim copies of |
---|
49 | this manual provided the copyright notice and this permission notice |
---|
50 | are preserved on all copies. |
---|
51 | |
---|
52 | Permission is granted to copy and distribute modified versions of this |
---|
53 | manual under the conditions for verbatim copying, provided that the entire |
---|
54 | resulting derived work is distributed under the terms of a permission |
---|
55 | notice identical to this one. |
---|
56 | |
---|
57 | Permission is granted to copy and distribute translations of this manual |
---|
58 | into another language, under the above conditions for modified versions, |
---|
59 | except that this permission notice may be stated in a translation approved |
---|
60 | by the Foundation. |
---|
61 | @end titlepage |
---|
62 | |
---|
63 | @node Top, , , (dir) |
---|
64 | |
---|
65 | @ifinfo |
---|
66 | This file documents the the GNU @code{diff}, @code{diff3}, @code{sdiff}, |
---|
67 | and @code{cmp} commands for showing the differences between text files |
---|
68 | and the @code{patch} command for using their output to update files. |
---|
69 | |
---|
70 | This is Edition 1.2, for @code{diff} 2.4 and @code{patch} 2.1. |
---|
71 | @end ifinfo |
---|
72 | |
---|
73 | @menu |
---|
74 | * Overview:: Preliminary information. |
---|
75 | |
---|
76 | * Comparison:: What file comparison means. |
---|
77 | * Output Formats:: Formats for difference reports. |
---|
78 | * Comparing Directories:: Comparing files and directories. |
---|
79 | * Adjusting Output:: Making @code{diff} output prettier. |
---|
80 | * diff Performance:: Making @code{diff} smarter or faster. |
---|
81 | * Comparing Three Files:: Formats for three-way difference reports. |
---|
82 | |
---|
83 | * diff3 Merging:: Merging from a common ancestor. |
---|
84 | * Interactive Merging:: Interactive merging with @code{sdiff}. |
---|
85 | * Merging with patch:: Using @code{patch} to change old files into new ones. |
---|
86 | * Making Patches:: Tips for making patch distributions. |
---|
87 | |
---|
88 | * Invoking cmp:: How to run @code{cmp} and a summary of its options. |
---|
89 | * Invoking diff:: How to run @code{diff} and a summary of its options. |
---|
90 | * Invoking diff3:: How to run @code{diff3} and a summary of its options. |
---|
91 | * Invoking patch:: How to run @code{patch} and a summary of its options. |
---|
92 | * Invoking sdiff:: How to run @code{sdiff} and a summary of its options. |
---|
93 | |
---|
94 | * Incomplete Lines:: Lines that lack trailing newlines. |
---|
95 | * Projects:: If you think you've found a bug or other shortcoming. |
---|
96 | |
---|
97 | * Concept Index:: Index of concepts. |
---|
98 | @end menu |
---|
99 | |
---|
100 | @node Overview, Comparison, , Top |
---|
101 | @unnumbered Overview |
---|
102 | @cindex overview of @code{diff} and @code{patch} |
---|
103 | |
---|
104 | Computer users often find occasion to ask how two files differ. Perhaps |
---|
105 | one file is a newer version of the other file. Or maybe the two files |
---|
106 | started out as identical copies but were changed by different people. |
---|
107 | |
---|
108 | You can use the @code{diff} command to show differences between two |
---|
109 | files, or each corresponding file in two directories. @code{diff} |
---|
110 | outputs differences between files line by line in any of several |
---|
111 | formats, selectable by command line options. This set of differences is |
---|
112 | often called a @dfn{diff} or @dfn{patch}. For files that are identical, |
---|
113 | @code{diff} normally produces no output; for binary (non-text) files, |
---|
114 | @code{diff} normally reports only that they are different. |
---|
115 | |
---|
116 | You can use the @code{cmp} command to show the offsets and line numbers |
---|
117 | where two files differ. @code{cmp} can also show all the characters |
---|
118 | that differ between the two files, side by side. Another way to compare |
---|
119 | two files character by character is the Emacs command @kbd{M-x |
---|
120 | compare-windows}. @xref{Other Window, , Other Window, emacs, The GNU |
---|
121 | Emacs Manual}, for more information on that command. |
---|
122 | |
---|
123 | You can use the @code{diff3} command to show differences among three |
---|
124 | files. When two people have made independent changes to a common |
---|
125 | original, @code{diff3} can report the differences between the original |
---|
126 | and the two changed versions, and can produce a merged file that |
---|
127 | contains both persons' changes together with warnings about conflicts. |
---|
128 | |
---|
129 | You can use the @code{sdiff} command to merge two files interactively. |
---|
130 | |
---|
131 | You can use the set of differences produced by @code{diff} to distribute |
---|
132 | updates to text files (such as program source code) to other people. |
---|
133 | This method is especially useful when the differences are small compared |
---|
134 | to the complete files. Given @code{diff} output, you can use the |
---|
135 | @code{patch} program to update, or @dfn{patch}, a copy of the file. If you |
---|
136 | think of @code{diff} as subtracting one file from another to produce |
---|
137 | their difference, you can think of @code{patch} as adding the difference |
---|
138 | to one file to reproduce the other. |
---|
139 | |
---|
140 | This manual first concentrates on making diffs, and later shows how to |
---|
141 | use diffs to update files. |
---|
142 | |
---|
143 | GNU @code{diff} was written by Mike Haertel, David Hayes, Richard |
---|
144 | Stallman, Len Tower, and Paul Eggert. Wayne Davison designed and |
---|
145 | implemented the unified output format. The basic algorithm is described |
---|
146 | in ``An O(ND) Difference Algorithm and its Variations'', Eugene W. Myers, |
---|
147 | @cite{Algorithmica} Vol.@: 1 No.@: 2, 1986, pp.@: 251--266; and in ``A File |
---|
148 | Comparison Program'', Webb Miller and Eugene W. Myers, |
---|
149 | @cite{Software---Practice and Experience} Vol.@: 15 No.@: 11, 1985, |
---|
150 | pp.@: 1025--1040. |
---|
151 | @c From: "Gene Myers" <gene@cs.arizona.edu> |
---|
152 | @c They are about the same basic algorithm; the Algorithmica |
---|
153 | @c paper gives a rigorous treatment and the sub-algorithm for |
---|
154 | @c delivering scripts and should be the primary reference, but |
---|
155 | @c both should be mentioned. |
---|
156 | The algorithm was independently discovered as described in |
---|
157 | ``Algorithms for Approximate String Matching'', |
---|
158 | E. Ukkonen, @cite{Information and Control} Vol.@: 64, 1985, pp.@: 100--118. |
---|
159 | @c From: "Gene Myers" <gene@cs.arizona.edu> |
---|
160 | @c Date: Wed, 29 Sep 1993 08:27:55 MST |
---|
161 | @c Ukkonen should be given credit for also discovering the algorithm used |
---|
162 | @c in GNU diff. |
---|
163 | |
---|
164 | GNU @code{diff3} was written by Randy Smith. GNU @code{sdiff} was |
---|
165 | written by Thomas Lord. GNU @code{cmp} was written by Torbjorn Granlund |
---|
166 | and David MacKenzie. |
---|
167 | |
---|
168 | @code{patch} was written mainly by Larry Wall; the GNU enhancements were |
---|
169 | written mainly by Wayne Davison and David MacKenzie. Parts of this |
---|
170 | manual are adapted from a manual page written by Larry Wall, with his |
---|
171 | permission. |
---|
172 | |
---|
173 | @node Comparison, Output Formats, Overview, Top |
---|
174 | @chapter What Comparison Means |
---|
175 | @cindex introduction |
---|
176 | |
---|
177 | There are several ways to think about the differences between two files. |
---|
178 | One way to think of the differences is as a series of lines that were |
---|
179 | deleted from, inserted in, or changed in one file to produce the other |
---|
180 | file. @code{diff} compares two files line by line, finds groups of |
---|
181 | lines that differ, and reports each group of differing lines. It can |
---|
182 | report the differing lines in several formats, which have different |
---|
183 | purposes. |
---|
184 | |
---|
185 | GNU @code{diff} can show whether files are different without detailing |
---|
186 | the differences. It also provides ways to suppress certain kinds of |
---|
187 | differences that are not important to you. Most commonly, such |
---|
188 | differences are changes in the amount of white space between words or |
---|
189 | lines. @code{diff} also provides ways to suppress differences in |
---|
190 | alphabetic case or in lines that match a regular expression that you |
---|
191 | provide. These options can accumulate; for example, you can ignore |
---|
192 | changes in both white space and alphabetic case. |
---|
193 | |
---|
194 | Another way to think of the differences between two files is as a |
---|
195 | sequence of pairs of characters that can be either identical or |
---|
196 | different. @code{cmp} reports the differences between two files |
---|
197 | character by character, instead of line by line. As a result, it is |
---|
198 | more useful than @code{diff} for comparing binary files. For text |
---|
199 | files, @code{cmp} is useful mainly when you want to know only whether |
---|
200 | two files are identical. |
---|
201 | |
---|
202 | To illustrate the effect that considering changes character by character |
---|
203 | can have compared with considering them line by line, think of what |
---|
204 | happens if a single newline character is added to the beginning of a |
---|
205 | file. If that file is then compared with an otherwise identical file |
---|
206 | that lacks the newline at the beginning, @code{diff} will report that a |
---|
207 | blank line has been added to the file, while @code{cmp} will report that |
---|
208 | almost every character of the two files differs. |
---|
209 | |
---|
210 | @code{diff3} normally compares three input files line by line, finds |
---|
211 | groups of lines that differ, and reports each group of differing lines. |
---|
212 | Its output is designed to make it easy to inspect two different sets of |
---|
213 | changes to the same file. |
---|
214 | |
---|
215 | @menu |
---|
216 | * Hunks:: Groups of differing lines. |
---|
217 | * White Space:: Suppressing differences in white space. |
---|
218 | * Blank Lines:: Suppressing differences in blank lines. |
---|
219 | * Case Folding:: Suppressing differences in alphabetic case. |
---|
220 | * Specified Folding:: Suppressing differences that match regular expressions. |
---|
221 | * Brief:: Summarizing which files are different. |
---|
222 | * Binary:: Comparing binary files or forcing text comparisons. |
---|
223 | @end menu |
---|
224 | |
---|
225 | @node Hunks, White Space, , Comparison |
---|
226 | @section Hunks |
---|
227 | @cindex hunks |
---|
228 | |
---|
229 | When comparing two files, @code{diff} finds sequences of lines common to |
---|
230 | both files, interspersed with groups of differing lines called |
---|
231 | @dfn{hunks}. Comparing two identical files yields one sequence of |
---|
232 | common lines and no hunks, because no lines differ. Comparing two |
---|
233 | entirely different files yields no common lines and one large hunk that |
---|
234 | contains all lines of both files. In general, there are many ways to |
---|
235 | match up lines between two given files. @code{diff} tries to minimize |
---|
236 | the total hunk size by finding large sequences of common lines |
---|
237 | interspersed with small hunks of differing lines. |
---|
238 | |
---|
239 | For example, suppose the file @file{F} contains the three lines |
---|
240 | @samp{a}, @samp{b}, @samp{c}, and the file @file{G} contains the same |
---|
241 | three lines in reverse order @samp{c}, @samp{b}, @samp{a}. If |
---|
242 | @code{diff} finds the line @samp{c} as common, then the command |
---|
243 | @samp{diff F G} produces this output: |
---|
244 | |
---|
245 | @example |
---|
246 | 1,2d0 |
---|
247 | < a |
---|
248 | < b |
---|
249 | 3a2,3 |
---|
250 | > b |
---|
251 | > a |
---|
252 | @end example |
---|
253 | |
---|
254 | @noindent |
---|
255 | But if @code{diff} notices the common line @samp{b} instead, it produces |
---|
256 | this output: |
---|
257 | |
---|
258 | @example |
---|
259 | 1c1 |
---|
260 | < a |
---|
261 | --- |
---|
262 | > c |
---|
263 | 3c3 |
---|
264 | < c |
---|
265 | --- |
---|
266 | > a |
---|
267 | @end example |
---|
268 | |
---|
269 | @noindent |
---|
270 | It is also possible to find @samp{a} as the common line. @code{diff} |
---|
271 | does not always find an optimal matching between the files; it takes |
---|
272 | shortcuts to run faster. But its output is usually close to the |
---|
273 | shortest possible. You can adjust this tradeoff with the |
---|
274 | @samp{--minimal} option (@pxref{diff Performance}). |
---|
275 | |
---|
276 | @node White Space, Blank Lines, Hunks, Comparison |
---|
277 | @section Suppressing Differences in Blank and Tab Spacing |
---|
278 | @cindex blank and tab difference suppression |
---|
279 | @cindex tab and blank difference suppression |
---|
280 | |
---|
281 | The @samp{-b} and @samp{--ignore-space-change} options ignore white space |
---|
282 | at line end, and considers all other sequences of one or more |
---|
283 | white space characters to be equivalent. With these options, |
---|
284 | @code{diff} considers the following two lines to be equivalent, where |
---|
285 | @samp{$} denotes the line end: |
---|
286 | |
---|
287 | @example |
---|
288 | Here lyeth muche rychnesse in lytell space. -- John Heywood$ |
---|
289 | Here lyeth muche rychnesse in lytell space. -- John Heywood $ |
---|
290 | @end example |
---|
291 | |
---|
292 | The @samp{-w} and @samp{--ignore-all-space} options are stronger than |
---|
293 | @samp{-b}. They ignore difference even if one file has white space where |
---|
294 | the other file has none. @dfn{White space} characters include |
---|
295 | tab, newline, vertical tab, form feed, carriage return, and space; |
---|
296 | some locales may define additional characters to be white space. |
---|
297 | With these options, @code{diff} considers the |
---|
298 | following two lines to be equivalent, where @samp{$} denotes the line |
---|
299 | end and @samp{^M} denotes a carriage return: |
---|
300 | |
---|
301 | @example |
---|
302 | Here lyeth muche rychnesse in lytell space.-- John Heywood$ |
---|
303 | He relyeth much erychnes seinly tells pace. --John Heywood ^M$ |
---|
304 | @end example |
---|
305 | |
---|
306 | @node Blank Lines, Case Folding, White Space, Comparison |
---|
307 | @section Suppressing Differences in Blank Lines |
---|
308 | @cindex blank line difference suppression |
---|
309 | |
---|
310 | The @samp{-B} and @samp{--ignore-blank-lines} options ignore insertions |
---|
311 | or deletions of blank lines. These options normally affect only lines |
---|
312 | that are completely empty; they do not affect lines that look empty but |
---|
313 | contain space or tab characters. With these options, for example, a |
---|
314 | file containing |
---|
315 | @example |
---|
316 | 1. A point is that which has no part. |
---|
317 | |
---|
318 | 2. A line is breadthless length. |
---|
319 | -- Euclid, The Elements, I |
---|
320 | @end example |
---|
321 | @noindent |
---|
322 | is considered identical to a file containing |
---|
323 | @example |
---|
324 | 1. A point is that which has no part. |
---|
325 | 2. A line is breadthless length. |
---|
326 | |
---|
327 | |
---|
328 | -- Euclid, The Elements, I |
---|
329 | @end example |
---|
330 | |
---|
331 | @node Case Folding, Specified Folding, Blank Lines, Comparison |
---|
332 | @section Suppressing Case Differences |
---|
333 | @cindex case difference suppression |
---|
334 | |
---|
335 | GNU @code{diff} can treat lowercase letters as equivalent to their |
---|
336 | uppercase counterparts, so that, for example, it considers @samp{Funky |
---|
337 | Stuff}, @samp{funky STUFF}, and @samp{fUNKy stuFf} to all be the same. |
---|
338 | To request this, use the @samp{-i} or @samp{--ignore-case} option. |
---|
339 | |
---|
340 | @node Specified Folding, Brief, Case Folding, Comparison |
---|
341 | @section Suppressing Lines Matching a Regular Expression |
---|
342 | @cindex regular expression suppression |
---|
343 | |
---|
344 | To ignore insertions and deletions of lines that match a regular |
---|
345 | expression, use the @samp{-I @var{regexp}} or |
---|
346 | @samp{--ignore-matching-lines=@var{regexp}} option. You should escape |
---|
347 | regular expressions that contain shell metacharacters to prevent the |
---|
348 | shell from expanding them. For example, @samp{diff -I '^[0-9]'} ignores |
---|
349 | all changes to lines beginning with a digit. |
---|
350 | |
---|
351 | However, @samp{-I} only ignores the insertion or deletion of lines that |
---|
352 | contain the regular expression if every changed line in the hunk---every |
---|
353 | insertion and every deletion---matches the regular expression. In other |
---|
354 | words, for each nonignorable change, @code{diff} prints the complete set |
---|
355 | of changes in its vicinity, including the ignorable ones. |
---|
356 | |
---|
357 | You can specify more than one regular expression for lines to ignore by |
---|
358 | using more than one @samp{-I} option. @code{diff} tries to match each |
---|
359 | line against each regular expression, starting with the last one given. |
---|
360 | |
---|
361 | @node Brief, Binary, Specified Folding, Comparison |
---|
362 | @section Summarizing Which Files Differ |
---|
363 | @cindex summarizing which files differ |
---|
364 | @cindex brief difference reports |
---|
365 | |
---|
366 | When you only want to find out whether files are different, and you |
---|
367 | don't care what the differences are, you can use the summary output |
---|
368 | format. In this format, instead of showing the differences between the |
---|
369 | files, @code{diff} simply reports whether files differ. The @samp{-q} |
---|
370 | and @samp{--brief} options select this output format. |
---|
371 | |
---|
372 | This format is especially useful when comparing the contents of two |
---|
373 | directories. It is also much faster than doing the normal line by line |
---|
374 | comparisons, because @code{diff} can stop analyzing the files as soon as |
---|
375 | it knows that there are any differences. |
---|
376 | |
---|
377 | You can also get a brief indication of whether two files differ by using |
---|
378 | @code{cmp}. For files that are identical, @code{cmp} produces no |
---|
379 | output. When the files differ, by default, @code{cmp} outputs the byte |
---|
380 | offset and line number where the first difference occurs. You can use |
---|
381 | the @samp{-s} option to suppress that information, so that @code{cmp} |
---|
382 | produces no output and reports whether the files differ using only its |
---|
383 | exit status (@pxref{Invoking cmp}). |
---|
384 | |
---|
385 | @c Fix this. |
---|
386 | Unlike @code{diff}, @code{cmp} cannot compare directories; it can only |
---|
387 | compare two files. |
---|
388 | |
---|
389 | @node Binary, , Brief, Comparison |
---|
390 | @section Binary Files and Forcing Text Comparisons |
---|
391 | @cindex binary file diff |
---|
392 | @cindex text versus binary diff |
---|
393 | |
---|
394 | If @code{diff} thinks that either of the two files it is comparing is |
---|
395 | binary (a non-text file), it normally treats that pair of files much as |
---|
396 | if the summary output format had been selected (@pxref{Brief}), and |
---|
397 | reports only that the binary files are different. This is because line |
---|
398 | by line comparisons are usually not meaningful for binary files. |
---|
399 | |
---|
400 | @code{diff} determines whether a file is text or binary by checking the |
---|
401 | first few bytes in the file; the exact number of bytes is system |
---|
402 | dependent, but it is typically several thousand. If every character in |
---|
403 | that part of the file is non-null, @code{diff} considers the file to be |
---|
404 | text; otherwise it considers the file to be binary. |
---|
405 | |
---|
406 | Sometimes you might want to force @code{diff} to consider files to be |
---|
407 | text. For example, you might be comparing text files that contain |
---|
408 | null characters; @code{diff} would erroneously decide that those are |
---|
409 | non-text files. Or you might be comparing documents that are in a |
---|
410 | format used by a word processing system that uses null characters to |
---|
411 | indicate special formatting. You can force @code{diff} to consider all |
---|
412 | files to be text files, and compare them line by line, by using the |
---|
413 | @samp{-a} or @samp{--text} option. If the files you compare using this |
---|
414 | option do not in fact contain text, they will probably contain few |
---|
415 | newline characters, and the @code{diff} output will consist of hunks |
---|
416 | showing differences between long lines of whatever characters the files |
---|
417 | contain. |
---|
418 | |
---|
419 | You can also force @code{diff} to consider all files to be binary files, |
---|
420 | and report only whether they differ (but not how). Use the |
---|
421 | @samp{--brief} option for this. |
---|
422 | |
---|
423 | In operating systems that distinguish between text and binary files, |
---|
424 | @code{diff} normally reads and writes all data as text. Use the |
---|
425 | @samp{--binary} option to force @code{diff} to read and write binary |
---|
426 | data instead. This option has no effect on a Posix-compliant system |
---|
427 | like GNU or traditional Unix. However, many personal computer |
---|
428 | operating systems represent the end of a line with a carriage return |
---|
429 | followed by a newline. On such systems, @code{diff} normally ignores |
---|
430 | these carriage returns on input and generates them at the end of each |
---|
431 | output line, but with the @samp{--binary} option @code{diff} treats |
---|
432 | each carriage return as just another input character, and does not |
---|
433 | generate a carriage return at the end of each output line. This can be |
---|
434 | useful when dealing with non-text files that are meant to be |
---|
435 | interchanged with Posix-compliant systems. |
---|
436 | |
---|
437 | If you want to compare two files byte by byte, you can use the |
---|
438 | @code{cmp} program with the @samp{-l} option to show the values of each |
---|
439 | differing byte in the two files. With GNU @code{cmp}, you can also use |
---|
440 | the @samp{-c} option to show the ASCII representation of those bytes. |
---|
441 | @xref{Invoking cmp}, for more information. |
---|
442 | |
---|
443 | If @code{diff3} thinks that any of the files it is comparing is binary |
---|
444 | (a non-text file), it normally reports an error, because such |
---|
445 | comparisons are usually not useful. @code{diff3} uses the same test as |
---|
446 | @code{diff} to decide whether a file is binary. As with @code{diff}, if |
---|
447 | the input files contain a few non-text characters but otherwise are like |
---|
448 | text files, you can force @code{diff3} to consider all files to be text |
---|
449 | files and compare them line by line by using the @samp{-a} or |
---|
450 | @samp{--text} options. |
---|
451 | |
---|
452 | @node Output Formats, Comparing Directories, Comparison, Top |
---|
453 | @chapter @code{diff} Output Formats |
---|
454 | @cindex output formats |
---|
455 | @cindex format of @code{diff} output |
---|
456 | |
---|
457 | @code{diff} has several mutually exclusive options for output format. |
---|
458 | The following sections describe each format, illustrating how |
---|
459 | @code{diff} reports the differences between two sample input files. |
---|
460 | |
---|
461 | @menu |
---|
462 | * Sample diff Input:: Sample @code{diff} input files for examples. |
---|
463 | * Normal:: Showing differences without surrounding text. |
---|
464 | * Context:: Showing differences with the surrounding text. |
---|
465 | * Side by Side:: Showing differences in two columns. |
---|
466 | * Scripts:: Generating scripts for other programs. |
---|
467 | * If-then-else:: Merging files with if-then-else. |
---|
468 | @end menu |
---|
469 | |
---|
470 | @node Sample diff Input, Normal, , Output Formats |
---|
471 | @section Two Sample Input Files |
---|
472 | @cindex @code{diff} sample input |
---|
473 | @cindex sample input for @code{diff} |
---|
474 | |
---|
475 | Here are two sample files that we will use in numerous examples to |
---|
476 | illustrate the output of @code{diff} and how various options can change |
---|
477 | it. |
---|
478 | |
---|
479 | This is the file @file{lao}: |
---|
480 | |
---|
481 | @example |
---|
482 | The Way that can be told of is not the eternal Way; |
---|
483 | The name that can be named is not the eternal name. |
---|
484 | The Nameless is the origin of Heaven and Earth; |
---|
485 | The Named is the mother of all things. |
---|
486 | Therefore let there always be non-being, |
---|
487 | so we may see their subtlety, |
---|
488 | And let there always be being, |
---|
489 | so we may see their outcome. |
---|
490 | The two are the same, |
---|
491 | But after they are produced, |
---|
492 | they have different names. |
---|
493 | @end example |
---|
494 | |
---|
495 | This is the file @file{tzu}: |
---|
496 | |
---|
497 | @example |
---|
498 | The Nameless is the origin of Heaven and Earth; |
---|
499 | The named is the mother of all things. |
---|
500 | |
---|
501 | Therefore let there always be non-being, |
---|
502 | so we may see their subtlety, |
---|
503 | And let there always be being, |
---|
504 | so we may see their outcome. |
---|
505 | The two are the same, |
---|
506 | But after they are produced, |
---|
507 | they have different names. |
---|
508 | They both may be called deep and profound. |
---|
509 | Deeper and more profound, |
---|
510 | The door of all subtleties! |
---|
511 | @end example |
---|
512 | |
---|
513 | In this example, the first hunk contains just the first two lines of |
---|
514 | @file{lao}, the second hunk contains the fourth line of @file{lao} |
---|
515 | opposing the second and third lines of @file{tzu}, and the last hunk |
---|
516 | contains just the last three lines of @file{tzu}. |
---|
517 | |
---|
518 | @node Normal, Context, Sample diff Input, Output Formats |
---|
519 | @section Showing Differences Without Context |
---|
520 | @cindex normal output format |
---|
521 | @cindex @samp{<} output format |
---|
522 | |
---|
523 | The ``normal'' @code{diff} output format shows each hunk of differences |
---|
524 | without any surrounding context. Sometimes such output is the clearest |
---|
525 | way to see how lines have changed, without the clutter of nearby |
---|
526 | unchanged lines (although you can get similar results with the context |
---|
527 | or unified formats by using 0 lines of context). However, this format |
---|
528 | is no longer widely used for sending out patches; for that purpose, the |
---|
529 | context format (@pxref{Context Format}) and the unified format |
---|
530 | (@pxref{Unified Format}) are superior. Normal format is the default for |
---|
531 | compatibility with older versions of @code{diff} and the Posix standard. |
---|
532 | |
---|
533 | @menu |
---|
534 | * Detailed Normal:: A detailed description of normal output format. |
---|
535 | * Example Normal:: Sample output in the normal format. |
---|
536 | @end menu |
---|
537 | |
---|
538 | @node Detailed Normal, Example Normal, , Normal |
---|
539 | @subsection Detailed Description of Normal Format |
---|
540 | |
---|
541 | The normal output format consists of one or more hunks of differences; |
---|
542 | each hunk shows one area where the files differ. Normal format hunks |
---|
543 | look like this: |
---|
544 | |
---|
545 | @example |
---|
546 | @var{change-command} |
---|
547 | < @var{from-file-line} |
---|
548 | < @var{from-file-line}@dots{} |
---|
549 | --- |
---|
550 | > @var{to-file-line} |
---|
551 | > @var{to-file-line}@dots{} |
---|
552 | @end example |
---|
553 | |
---|
554 | There are three types of change commands. Each consists of a line |
---|
555 | number or comma-separated range of lines in the first file, a single |
---|
556 | character indicating the kind of change to make, and a line number or |
---|
557 | comma-separated range of lines in the second file. All line numbers are |
---|
558 | the original line numbers in each file. The types of change commands |
---|
559 | are: |
---|
560 | |
---|
561 | @table @samp |
---|
562 | @item @var{l}a@var{r} |
---|
563 | Add the lines in range @var{r} of the second file after line @var{l} of |
---|
564 | the first file. For example, @samp{8a12,15} means append lines 12--15 |
---|
565 | of file 2 after line 8 of file 1; or, if changing file 2 into file 1, |
---|
566 | delete lines 12--15 of file 2. |
---|
567 | |
---|
568 | @item @var{f}c@var{t} |
---|
569 | Replace the lines in range @var{f} of the first file with lines in range |
---|
570 | @var{t} of the second file. This is like a combined add and delete, but |
---|
571 | more compact. For example, @samp{5,7c8,10} means change lines 5--7 of |
---|
572 | file 1 to read as lines 8--10 of file 2; or, if changing file 2 into |
---|
573 | file 1, change lines 8--10 of file 2 to read as lines 5--7 of file 1. |
---|
574 | |
---|
575 | @item @var{r}d@var{l} |
---|
576 | Delete the lines in range @var{r} from the first file; line @var{l} is where |
---|
577 | they would have appeared in the second file had they not been deleted. |
---|
578 | For example, @samp{5,7d3} means delete lines 5--7 of file 1; or, if |
---|
579 | changing file 2 into file 1, append lines 5--7 of file 1 after line 3 of |
---|
580 | file 2. |
---|
581 | @end table |
---|
582 | |
---|
583 | @node Example Normal, , Detailed Normal, Normal |
---|
584 | @subsection An Example of Normal Format |
---|
585 | |
---|
586 | Here is the output of the command @samp{diff lao tzu} |
---|
587 | (@pxref{Sample diff Input}, for the complete contents of the two files). |
---|
588 | Notice that it shows only the lines that are different between the two |
---|
589 | files. |
---|
590 | |
---|
591 | @example |
---|
592 | 1,2d0 |
---|
593 | < The Way that can be told of is not the eternal Way; |
---|
594 | < The name that can be named is not the eternal name. |
---|
595 | 4c2,3 |
---|
596 | < The Named is the mother of all things. |
---|
597 | --- |
---|
598 | > The named is the mother of all things. |
---|
599 | > |
---|
600 | 11a11,13 |
---|
601 | > They both may be called deep and profound. |
---|
602 | > Deeper and more profound, |
---|
603 | > The door of all subtleties! |
---|
604 | @end example |
---|
605 | |
---|
606 | @node Context, Side by Side, Normal, Output Formats |
---|
607 | @section Showing Differences in Their Context |
---|
608 | @cindex context output format |
---|
609 | @cindex @samp{!} output format |
---|
610 | |
---|
611 | Usually, when you are looking at the differences between files, you will |
---|
612 | also want to see the parts of the files near the lines that differ, to |
---|
613 | help you understand exactly what has changed. These nearby parts of the |
---|
614 | files are called the @dfn{context}. |
---|
615 | |
---|
616 | GNU @code{diff} provides two output formats that show context around the |
---|
617 | differing lines: @dfn{context format} and @dfn{unified format}. It can |
---|
618 | optionally show in which function or section of the file the differing |
---|
619 | lines are found. |
---|
620 | |
---|
621 | If you are distributing new versions of files to other people in the |
---|
622 | form of @code{diff} output, you should use one of the output formats |
---|
623 | that show context so that they can apply the diffs even if they have |
---|
624 | made small changes of their own to the files. @code{patch} can apply |
---|
625 | the diffs in this case by searching in the files for the lines of |
---|
626 | context around the differing lines; if those lines are actually a few |
---|
627 | lines away from where the diff says they are, @code{patch} can adjust |
---|
628 | the line numbers accordingly and still apply the diff correctly. |
---|
629 | @xref{Imperfect}, for more information on using @code{patch} to apply |
---|
630 | imperfect diffs. |
---|
631 | |
---|
632 | @menu |
---|
633 | * Context Format:: An output format that shows surrounding lines. |
---|
634 | * Unified Format:: A more compact output format that shows context. |
---|
635 | * Sections:: Showing which sections of the files differences are in. |
---|
636 | * Alternate Names:: Showing alternate file names in context headers. |
---|
637 | @end menu |
---|
638 | |
---|
639 | @node Context Format, Unified Format, , Context |
---|
640 | @subsection Context Format |
---|
641 | |
---|
642 | The context output format shows several lines of context around the |
---|
643 | lines that differ. It is the standard format for distributing updates |
---|
644 | to source code. |
---|
645 | |
---|
646 | To select this output format, use the @samp{-C @var{lines}}, |
---|
647 | @samp{--context@r{[}=@var{lines}@r{]}}, or @samp{-c} option. The |
---|
648 | argument @var{lines} that some of these options take is the number of |
---|
649 | lines of context to show. If you do not specify @var{lines}, it |
---|
650 | defaults to three. For proper operation, @code{patch} typically needs |
---|
651 | at least two lines of context. |
---|
652 | |
---|
653 | @menu |
---|
654 | * Detailed Context:: A detailed description of the context output format. |
---|
655 | * Example Context:: Sample output in context format. |
---|
656 | * Less Context:: Another sample with less context. |
---|
657 | @end menu |
---|
658 | |
---|
659 | @node Detailed Context, Example Context, , Context Format |
---|
660 | @subsubsection Detailed Description of Context Format |
---|
661 | |
---|
662 | The context output format starts with a two-line header, which looks |
---|
663 | like this: |
---|
664 | |
---|
665 | @example |
---|
666 | *** @var{from-file} @var{from-file-modification-time} |
---|
667 | --- @var{to-file} @var{to-file-modification time} |
---|
668 | @end example |
---|
669 | |
---|
670 | @noindent |
---|
671 | You can change the header's content with the @samp{-L @var{label}} or |
---|
672 | @samp{--label=@var{label}} option; see @ref{Alternate Names}. |
---|
673 | |
---|
674 | Next come one or more hunks of differences; each hunk shows one area |
---|
675 | where the files differ. Context format hunks look like this: |
---|
676 | |
---|
677 | @example |
---|
678 | *************** |
---|
679 | *** @var{from-file-line-range} **** |
---|
680 | @var{from-file-line} |
---|
681 | @var{from-file-line}@dots{} |
---|
682 | --- @var{to-file-line-range} ---- |
---|
683 | @var{to-file-line} |
---|
684 | @var{to-file-line}@dots{} |
---|
685 | @end example |
---|
686 | |
---|
687 | The lines of context around the lines that differ start with two space |
---|
688 | characters. The lines that differ between the two files start with one |
---|
689 | of the following indicator characters, followed by a space character: |
---|
690 | |
---|
691 | @table @samp |
---|
692 | @item ! |
---|
693 | A line that is part of a group of one or more lines that changed between |
---|
694 | the two files. There is a corresponding group of lines marked with |
---|
695 | @samp{!} in the part of this hunk for the other file. |
---|
696 | |
---|
697 | @item + |
---|
698 | An ``inserted'' line in the second file that corresponds to nothing in |
---|
699 | the first file. |
---|
700 | |
---|
701 | @item - |
---|
702 | A ``deleted'' line in the first file that corresponds to nothing in the |
---|
703 | second file. |
---|
704 | @end table |
---|
705 | |
---|
706 | If all of the changes in a hunk are insertions, the lines of |
---|
707 | @var{from-file} are omitted. If all of the changes are deletions, the |
---|
708 | lines of @var{to-file} are omitted. |
---|
709 | |
---|
710 | @node Example Context, Less Context, Detailed Context, Context Format |
---|
711 | @subsubsection An Example of Context Format |
---|
712 | |
---|
713 | Here is the output of @samp{diff -c lao tzu} (@pxref{Sample diff Input}, |
---|
714 | for the complete contents of the two files). Notice that up to three |
---|
715 | lines that are not different are shown around each line that is |
---|
716 | different; they are the context lines. Also notice that the first two |
---|
717 | hunks have run together, because their contents overlap. |
---|
718 | |
---|
719 | @example |
---|
720 | *** lao Sat Jan 26 23:30:39 1991 |
---|
721 | --- tzu Sat Jan 26 23:30:50 1991 |
---|
722 | *************** |
---|
723 | *** 1,7 **** |
---|
724 | - The Way that can be told of is not the eternal Way; |
---|
725 | - The name that can be named is not the eternal name. |
---|
726 | The Nameless is the origin of Heaven and Earth; |
---|
727 | ! The Named is the mother of all things. |
---|
728 | Therefore let there always be non-being, |
---|
729 | so we may see their subtlety, |
---|
730 | And let there always be being, |
---|
731 | --- 1,6 ---- |
---|
732 | The Nameless is the origin of Heaven and Earth; |
---|
733 | ! The named is the mother of all things. |
---|
734 | ! |
---|
735 | Therefore let there always be non-being, |
---|
736 | so we may see their subtlety, |
---|
737 | And let there always be being, |
---|
738 | *************** |
---|
739 | *** 9,11 **** |
---|
740 | --- 8,13 ---- |
---|
741 | The two are the same, |
---|
742 | But after they are produced, |
---|
743 | they have different names. |
---|
744 | + They both may be called deep and profound. |
---|
745 | + Deeper and more profound, |
---|
746 | + The door of all subtleties! |
---|
747 | @end example |
---|
748 | |
---|
749 | @node Less Context, , Example Context, Context Format |
---|
750 | @subsubsection An Example of Context Format with Less Context |
---|
751 | |
---|
752 | Here is the output of @samp{diff --context=1 lao tzu} (@pxref{Sample |
---|
753 | diff Input}, for the complete contents of the two files). Notice that |
---|
754 | at most one context line is reported here. |
---|
755 | |
---|
756 | @example |
---|
757 | *** lao Sat Jan 26 23:30:39 1991 |
---|
758 | --- tzu Sat Jan 26 23:30:50 1991 |
---|
759 | *************** |
---|
760 | *** 1,5 **** |
---|
761 | - The Way that can be told of is not the eternal Way; |
---|
762 | - The name that can be named is not the eternal name. |
---|
763 | The Nameless is the origin of Heaven and Earth; |
---|
764 | ! The Named is the mother of all things. |
---|
765 | Therefore let there always be non-being, |
---|
766 | --- 1,4 ---- |
---|
767 | The Nameless is the origin of Heaven and Earth; |
---|
768 | ! The named is the mother of all things. |
---|
769 | ! |
---|
770 | Therefore let there always be non-being, |
---|
771 | *************** |
---|
772 | *** 11 **** |
---|
773 | --- 10,13 ---- |
---|
774 | they have different names. |
---|
775 | + They both may be called deep and profound. |
---|
776 | + Deeper and more profound, |
---|
777 | + The door of all subtleties! |
---|
778 | @end example |
---|
779 | |
---|
780 | @node Unified Format, Sections, Context Format, Context |
---|
781 | @subsection Unified Format |
---|
782 | @cindex unified output format |
---|
783 | @cindex @samp{+-} output format |
---|
784 | |
---|
785 | The unified output format is a variation on the context format that is |
---|
786 | more compact because it omits redundant context lines. To select this |
---|
787 | output format, use the @samp{-U @var{lines}}, |
---|
788 | @samp{--unified@r{[}=@var{lines}@r{]}}, or @samp{-u} |
---|
789 | option. The argument @var{lines} is the number of lines of context to |
---|
790 | show. When it is not given, it defaults to three. |
---|
791 | |
---|
792 | At present, only GNU @code{diff} can produce this format and only GNU |
---|
793 | @code{patch} can automatically apply diffs in this format. For proper |
---|
794 | operation, @code{patch} typically needs at least two lines of context. |
---|
795 | |
---|
796 | @menu |
---|
797 | * Detailed Unified:: A detailed description of unified format. |
---|
798 | * Example Unified:: Sample output in unified format. |
---|
799 | @end menu |
---|
800 | |
---|
801 | @node Detailed Unified, Example Unified, , Unified Format |
---|
802 | @subsubsection Detailed Description of Unified Format |
---|
803 | |
---|
804 | The unified output format starts with a two-line header, which looks |
---|
805 | like this: |
---|
806 | |
---|
807 | @example |
---|
808 | --- @var{from-file} @var{from-file-modification-time} |
---|
809 | +++ @var{to-file} @var{to-file-modification-time} |
---|
810 | @end example |
---|
811 | |
---|
812 | @noindent |
---|
813 | You can change the header's content with the @samp{-L @var{label}} or |
---|
814 | @samp{--label=@var{label}} option; see @xref{Alternate Names}. |
---|
815 | |
---|
816 | Next come one or more hunks of differences; each hunk shows one area |
---|
817 | where the files differ. Unified format hunks look like this: |
---|
818 | |
---|
819 | @example |
---|
820 | @@@@ @var{from-file-range} @var{to-file-range} @@@@ |
---|
821 | @var{line-from-either-file} |
---|
822 | @var{line-from-either-file}@dots{} |
---|
823 | @end example |
---|
824 | |
---|
825 | The lines common to both files begin with a space character. The lines |
---|
826 | that actually differ between the two files have one of the following |
---|
827 | indicator characters in the left column: |
---|
828 | |
---|
829 | @table @samp |
---|
830 | @item + |
---|
831 | A line was added here to the first file. |
---|
832 | |
---|
833 | @item - |
---|
834 | A line was removed here from the first file. |
---|
835 | @end table |
---|
836 | |
---|
837 | @node Example Unified, , Detailed Unified, Unified Format |
---|
838 | @subsubsection An Example of Unified Format |
---|
839 | |
---|
840 | Here is the output of the command @samp{diff -u lao tzu} |
---|
841 | (@pxref{Sample diff Input}, for the complete contents of the two files): |
---|
842 | |
---|
843 | @example |
---|
844 | --- lao Sat Jan 26 23:30:39 1991 |
---|
845 | +++ tzu Sat Jan 26 23:30:50 1991 |
---|
846 | @@@@ -1,7 +1,6 @@@@ |
---|
847 | -The Way that can be told of is not the eternal Way; |
---|
848 | -The name that can be named is not the eternal name. |
---|
849 | The Nameless is the origin of Heaven and Earth; |
---|
850 | -The Named is the mother of all things. |
---|
851 | +The named is the mother of all things. |
---|
852 | + |
---|
853 | Therefore let there always be non-being, |
---|
854 | so we may see their subtlety, |
---|
855 | And let there always be being, |
---|
856 | @@@@ -9,3 +8,6 @@@@ |
---|
857 | The two are the same, |
---|
858 | But after they are produced, |
---|
859 | they have different names. |
---|
860 | +They both may be called deep and profound. |
---|
861 | +Deeper and more profound, |
---|
862 | +The door of all subtleties! |
---|
863 | @end example |
---|
864 | |
---|
865 | @node Sections, Alternate Names, Unified Format, Context |
---|
866 | @subsection Showing Which Sections Differences Are in |
---|
867 | @cindex headings |
---|
868 | @cindex section headings |
---|
869 | |
---|
870 | Sometimes you might want to know which part of the files each change |
---|
871 | falls in. If the files are source code, this could mean which function |
---|
872 | was changed. If the files are documents, it could mean which chapter or |
---|
873 | appendix was changed. GNU @code{diff} can show this by displaying the |
---|
874 | nearest section heading line that precedes the differing lines. Which |
---|
875 | lines are ``section headings'' is determined by a regular expression. |
---|
876 | |
---|
877 | @menu |
---|
878 | * Specified Headings:: Showing headings that match regular expressions. |
---|
879 | * C Function Headings:: Showing headings of C functions. |
---|
880 | @end menu |
---|
881 | |
---|
882 | @node Specified Headings, C Function Headings, , Sections |
---|
883 | @subsubsection Showing Lines That Match Regular Expressions |
---|
884 | @cindex specified headings |
---|
885 | @cindex regular expression matching headings |
---|
886 | |
---|
887 | To show in which sections differences occur for files that are not |
---|
888 | source code for C or similar languages, use the @samp{-F @var{regexp}} |
---|
889 | or @samp{--show-function-line=@var{regexp}} option. @code{diff} |
---|
890 | considers lines that match the argument @var{regexp} to be the beginning |
---|
891 | of a section of the file. Here are suggested regular expressions for |
---|
892 | some common languages: |
---|
893 | |
---|
894 | @c Please add to this list, e.g. Fortran, Pascal. |
---|
895 | @table @samp |
---|
896 | @item ^[A-Za-z_] |
---|
897 | C, C++, Prolog |
---|
898 | @item ^( |
---|
899 | Lisp |
---|
900 | @item ^@@\(chapter\|appendix\|unnumbered\|chapheading\) |
---|
901 | Texinfo |
---|
902 | @end table |
---|
903 | |
---|
904 | This option does not automatically select an output format; in order to |
---|
905 | use it, you must select the context format (@pxref{Context Format}) or |
---|
906 | unified format (@pxref{Unified Format}). In other output formats it |
---|
907 | has no effect. |
---|
908 | |
---|
909 | The @samp{-F} and @samp{--show-function-line} options find the nearest |
---|
910 | unchanged line that precedes each hunk of differences and matches the |
---|
911 | given regular expression. Then they add that line to the end of the |
---|
912 | line of asterisks in the context format, or to the @samp{@@@@} line in |
---|
913 | unified format. If no matching line exists, they leave the output for |
---|
914 | that hunk unchanged. If that line is more than 40 characters long, they |
---|
915 | output only the first 40 characters. You can specify more than one |
---|
916 | regular expression for such lines; @code{diff} tries to match each line |
---|
917 | against each regular expression, starting with the last one given. This |
---|
918 | means that you can use @samp{-p} and @samp{-F} together, if you wish. |
---|
919 | |
---|
920 | @node C Function Headings, , Specified Headings, Sections |
---|
921 | @subsubsection Showing C Function Headings |
---|
922 | @cindex C function headings |
---|
923 | @cindex function headings, C |
---|
924 | |
---|
925 | To show in which functions differences occur for C and similar |
---|
926 | languages, you can use the @samp{-p} or @samp{--show-c-function} option. |
---|
927 | This option automatically defaults to the context output format |
---|
928 | (@pxref{Context Format}), with the default number of lines of context. |
---|
929 | You can override that number with @samp{-C @var{lines}} elsewhere in the |
---|
930 | command line. You can override both the format and the number with |
---|
931 | @samp{-U @var{lines}} elsewhere in the command line. |
---|
932 | |
---|
933 | The @samp{-p} and @samp{--show-c-function} options are equivalent to |
---|
934 | @samp{-F'^[_a-zA-Z$]'} if the unified format is specified, otherwise |
---|
935 | @samp{-c -F'^[_a-zA-Z$]'} (@pxref{Specified Headings}). GNU @code{diff} |
---|
936 | provides them for the sake of convenience. |
---|
937 | |
---|
938 | @node Alternate Names, , Sections, Context |
---|
939 | @subsection Showing Alternate File Names |
---|
940 | @cindex alternate file names |
---|
941 | @cindex file name alternates |
---|
942 | |
---|
943 | If you are comparing two files that have meaningless or uninformative |
---|
944 | names, you might want @code{diff} to show alternate names in the header |
---|
945 | of the context and unified output formats. To do this, use the @samp{-L |
---|
946 | @var{label}} or @samp{--label=@var{label}} option. The first time |
---|
947 | you give this option, its argument replaces the name and date of the |
---|
948 | first file in the header; the second time, its argument replaces the |
---|
949 | name and date of the second file. If you give this option more than |
---|
950 | twice, @code{diff} reports an error. The @samp{-L} option does not |
---|
951 | affect the file names in the @code{pr} header when the @samp{-l} or |
---|
952 | @samp{--paginate} option is used (@pxref{Pagination}). |
---|
953 | |
---|
954 | Here are the first two lines of the output from @samp{diff -C2 |
---|
955 | -Loriginal -Lmodified lao tzu}: |
---|
956 | |
---|
957 | @example |
---|
958 | *** original |
---|
959 | --- modified |
---|
960 | @end example |
---|
961 | |
---|
962 | @node Side by Side, Scripts, Context, Output Formats |
---|
963 | @section Showing Differences Side by Side |
---|
964 | @cindex side by side |
---|
965 | @cindex two-column output |
---|
966 | @cindex columnar output |
---|
967 | |
---|
968 | @code{diff} can produce a side by side difference listing of two files. |
---|
969 | The files are listed in two columns with a gutter between them. The |
---|
970 | gutter contains one of the following markers: |
---|
971 | |
---|
972 | @table @asis |
---|
973 | @item white space |
---|
974 | The corresponding lines are in common. That is, either the lines are |
---|
975 | identical, or the difference is ignored because of one of the |
---|
976 | @samp{--ignore} options (@pxref{White Space}). |
---|
977 | |
---|
978 | @item @samp{|} |
---|
979 | The corresponding lines differ, and they are either both complete |
---|
980 | or both incomplete. |
---|
981 | |
---|
982 | @item @samp{<} |
---|
983 | The files differ and only the first file contains the line. |
---|
984 | |
---|
985 | @item @samp{>} |
---|
986 | The files differ and only the second file contains the line. |
---|
987 | |
---|
988 | @item @samp{(} |
---|
989 | Only the first file contains the line, but the difference is ignored. |
---|
990 | |
---|
991 | @item @samp{)} |
---|
992 | Only the second file contains the line, but the difference is ignored. |
---|
993 | |
---|
994 | @item @samp{\} |
---|
995 | The corresponding lines differ, and only the first line is incomplete. |
---|
996 | |
---|
997 | @item @samp{/} |
---|
998 | The corresponding lines differ, and only the second line is incomplete. |
---|
999 | @end table |
---|
1000 | |
---|
1001 | Normally, an output line is incomplete if and only if the lines that it |
---|
1002 | contains are incomplete; @xref{Incomplete Lines}. However, when an |
---|
1003 | output line represents two differing lines, one might be incomplete |
---|
1004 | while the other is not. In this case, the output line is complete, |
---|
1005 | but its the gutter is marked @samp{\} if the first line is incomplete, |
---|
1006 | @samp{/} if the second line is. |
---|
1007 | |
---|
1008 | Side by side format is sometimes easiest to read, but it has limitations. |
---|
1009 | It generates much wider output than usual, and truncates lines that are |
---|
1010 | too long to fit. Also, it relies on lining up output more heavily than |
---|
1011 | usual, so its output looks particularly bad if you use varying |
---|
1012 | width fonts, nonstandard tab stops, or nonprinting characters. |
---|
1013 | |
---|
1014 | You can use the @code{sdiff} command to interactively merge side by side |
---|
1015 | differences. @xref{Interactive Merging}, for more information on merging files. |
---|
1016 | |
---|
1017 | @menu |
---|
1018 | * Side by Side Format:: Controlling side by side output format. |
---|
1019 | * Example Side by Side:: Sample side by side output. |
---|
1020 | @end menu |
---|
1021 | |
---|
1022 | @node Side by Side Format, Example Side by Side, , Side by Side |
---|
1023 | @section Controlling Side by Side Format |
---|
1024 | @cindex side by side format |
---|
1025 | |
---|
1026 | The @samp{-y} or @samp{--side-by-side} option selects side by side |
---|
1027 | format. Because side by side output lines contain two input lines, they |
---|
1028 | are wider than usual. They are normally 130 columns, which can fit onto |
---|
1029 | a traditional printer line. You can set the length of output lines with |
---|
1030 | the @samp{-W @var{columns}} or @samp{--width=@var{columns}} option. The |
---|
1031 | output line is split into two halves of equal length, separated by a |
---|
1032 | small gutter to mark differences; the right half is aligned to a tab |
---|
1033 | stop so that tabs line up. Input lines that are too long to fit in half |
---|
1034 | of an output line are truncated for output. |
---|
1035 | |
---|
1036 | The @samp{--left-column} option prints only the left column of two |
---|
1037 | common lines. The @samp{--suppress-common-lines} option suppresses |
---|
1038 | common lines entirely. |
---|
1039 | |
---|
1040 | @node Example Side by Side, , Side by Side Format, Side by Side |
---|
1041 | @subsection An Example of Side by Side Format |
---|
1042 | |
---|
1043 | Here is the output of the command @samp{diff -y -W 72 lao tzu} |
---|
1044 | (@pxref{Sample diff Input}, for the complete contents of the two files). |
---|
1045 | |
---|
1046 | @example |
---|
1047 | The Way that can be told of is n < |
---|
1048 | The name that can be named is no < |
---|
1049 | The Nameless is the origin of He The Nameless is the origin of He |
---|
1050 | The Named is the mother of all t | The named is the mother of all t |
---|
1051 | > |
---|
1052 | Therefore let there always be no Therefore let there always be no |
---|
1053 | so we may see their subtlety, so we may see their subtlety, |
---|
1054 | And let there always be being, And let there always be being, |
---|
1055 | so we may see their outcome. so we may see their outcome. |
---|
1056 | The two are the same, The two are the same, |
---|
1057 | But after they are produced, But after they are produced, |
---|
1058 | they have different names. they have different names. |
---|
1059 | > They both may be called deep and |
---|
1060 | > Deeper and more profound, |
---|
1061 | > The door of all subtleties! |
---|
1062 | @end example |
---|
1063 | |
---|
1064 | @node Scripts, If-then-else, Side by Side, Output Formats |
---|
1065 | @section Making Edit Scripts |
---|
1066 | @cindex script output formats |
---|
1067 | |
---|
1068 | Several output modes produce command scripts for editing @var{from-file} |
---|
1069 | to produce @var{to-file}. |
---|
1070 | |
---|
1071 | @menu |
---|
1072 | * ed Scripts:: Using @code{diff} to produce commands for @code{ed}. |
---|
1073 | * Forward ed:: Making forward @code{ed} scripts. |
---|
1074 | * RCS:: A special @code{diff} output format used by RCS. |
---|
1075 | @end menu |
---|
1076 | |
---|
1077 | @node ed Scripts, Forward ed, , Scripts |
---|
1078 | @subsection @code{ed} Scripts |
---|
1079 | @cindex @code{ed} script output format |
---|
1080 | |
---|
1081 | @code{diff} can produce commands that direct the @code{ed} text editor |
---|
1082 | to change the first file into the second file. Long ago, this was the |
---|
1083 | only output mode that was suitable for editing one file into another |
---|
1084 | automatically; today, with @code{patch}, it is almost obsolete. Use the |
---|
1085 | @samp{-e} or @samp{--ed} option to select this output format. |
---|
1086 | |
---|
1087 | Like the normal format (@pxref{Normal}), this output format does not |
---|
1088 | show any context; unlike the normal format, it does not include the |
---|
1089 | information necessary to apply the diff in reverse (to produce the first |
---|
1090 | file if all you have is the second file and the diff). |
---|
1091 | |
---|
1092 | If the file @file{d} contains the output of @samp{diff -e old new}, then |
---|
1093 | the command @samp{(cat d && echo w) | ed - old} edits @file{old} to make |
---|
1094 | it a copy of @file{new}. More generally, if @file{d1}, @file{d2}, |
---|
1095 | @dots{}, @file{dN} contain the outputs of @samp{diff -e old new1}, |
---|
1096 | @samp{diff -e new1 new2}, @dots{}, @samp{diff -e newN-1 newN}, |
---|
1097 | respectively, then the command @samp{(cat d1 d2 @dots{} dN && echo w) | |
---|
1098 | ed - old} edits @file{old} to make it a copy of @file{newN}. |
---|
1099 | |
---|
1100 | @menu |
---|
1101 | * Detailed ed:: A detailed description of @code{ed} format. |
---|
1102 | * Example ed:: A sample @code{ed} script. |
---|
1103 | @end menu |
---|
1104 | |
---|
1105 | @node Detailed ed, Example ed, , ed Scripts |
---|
1106 | @subsubsection Detailed Description of @code{ed} Format |
---|
1107 | |
---|
1108 | The @code{ed} output format consists of one or more hunks of |
---|
1109 | differences. The changes closest to the ends of the files come first so |
---|
1110 | that commands that change the number of lines do not affect how |
---|
1111 | @code{ed} interprets line numbers in succeeding commands. @code{ed} |
---|
1112 | format hunks look like this: |
---|
1113 | |
---|
1114 | @example |
---|
1115 | @var{change-command} |
---|
1116 | @var{to-file-line} |
---|
1117 | @var{to-file-line}@dots{} |
---|
1118 | . |
---|
1119 | @end example |
---|
1120 | |
---|
1121 | Because @code{ed} uses a single period on a line to indicate the end of |
---|
1122 | input, GNU @code{diff} protects lines of changes that contain a single |
---|
1123 | period on a line by writing two periods instead, then writing a |
---|
1124 | subsequent @code{ed} command to change the two periods into one. The |
---|
1125 | @code{ed} format cannot represent an incomplete line, so if the second |
---|
1126 | file ends in a changed incomplete line, @code{diff} reports an error and |
---|
1127 | then pretends that a newline was appended. |
---|
1128 | |
---|
1129 | There are three types of change commands. Each consists of a line |
---|
1130 | number or comma-separated range of lines in the first file and a single |
---|
1131 | character indicating the kind of change to make. All line numbers are |
---|
1132 | the original line numbers in the file. The types of change commands |
---|
1133 | are: |
---|
1134 | |
---|
1135 | @table @samp |
---|
1136 | @item @var{l}a |
---|
1137 | Add text from the second file after line @var{l} in the first file. For |
---|
1138 | example, @samp{8a} means to add the following lines after line 8 of file |
---|
1139 | 1. |
---|
1140 | |
---|
1141 | @item @var{r}c |
---|
1142 | Replace the lines in range @var{r} in the first file with the following |
---|
1143 | lines. Like a combined add and delete, but more compact. For example, |
---|
1144 | @samp{5,7c} means change lines 5--7 of file 1 to read as the text file |
---|
1145 | 2. |
---|
1146 | |
---|
1147 | @item @var{r}d |
---|
1148 | Delete the lines in range @var{r} from the first file. For example, |
---|
1149 | @samp{5,7d} means delete lines 5--7 of file 1. |
---|
1150 | @end table |
---|
1151 | |
---|
1152 | @node Example ed, , Detailed ed, ed Scripts |
---|
1153 | @subsubsection Example @code{ed} Script |
---|
1154 | |
---|
1155 | Here is the output of @samp{diff -e lao tzu} (@pxref{Sample |
---|
1156 | diff Input}, for the complete contents of the two files): |
---|
1157 | |
---|
1158 | @example |
---|
1159 | 11a |
---|
1160 | They both may be called deep and profound. |
---|
1161 | Deeper and more profound, |
---|
1162 | The door of all subtleties! |
---|
1163 | . |
---|
1164 | 4c |
---|
1165 | The named is the mother of all things. |
---|
1166 | |
---|
1167 | . |
---|
1168 | 1,2d |
---|
1169 | @end example |
---|
1170 | |
---|
1171 | @node Forward ed, RCS, ed Scripts, Scripts |
---|
1172 | @subsection Forward @code{ed} Scripts |
---|
1173 | @cindex forward @code{ed} script output format |
---|
1174 | |
---|
1175 | @code{diff} can produce output that is like an @code{ed} script, but |
---|
1176 | with hunks in forward (front to back) order. The format of the commands |
---|
1177 | is also changed slightly: command characters precede the lines they |
---|
1178 | modify, spaces separate line numbers in ranges, and no attempt is made |
---|
1179 | to disambiguate hunk lines consisting of a single period. Like |
---|
1180 | @code{ed} format, forward @code{ed} format cannot represent incomplete |
---|
1181 | lines. |
---|
1182 | |
---|
1183 | Forward @code{ed} format is not very useful, because neither @code{ed} |
---|
1184 | nor @code{patch} can apply diffs in this format. It exists mainly for |
---|
1185 | compatibility with older versions of @code{diff}. Use the @samp{-f} or |
---|
1186 | @samp{--forward-ed} option to select it. |
---|
1187 | |
---|
1188 | @node RCS, , Forward ed, Scripts |
---|
1189 | @subsection RCS Scripts |
---|
1190 | @cindex RCS script output format |
---|
1191 | |
---|
1192 | The RCS output format is designed specifically for use by the Revision |
---|
1193 | Control System, which is a set of free programs used for organizing |
---|
1194 | different versions and systems of files. Use the @samp{-n} or |
---|
1195 | @samp{--rcs} option to select this output format. It is like the |
---|
1196 | forward @code{ed} format (@pxref{Forward ed}), but it can represent |
---|
1197 | arbitrary changes to the contents of a file because it avoids the |
---|
1198 | forward @code{ed} format's problems with lines consisting of a single |
---|
1199 | period and with incomplete lines. Instead of ending text sections with |
---|
1200 | a line consisting of a single period, each command specifies the number |
---|
1201 | of lines it affects; a combination of the @samp{a} and @samp{d} |
---|
1202 | commands are used instead of @samp{c}. Also, if the second file ends |
---|
1203 | in a changed incomplete line, then the output also ends in an |
---|
1204 | incomplete line. |
---|
1205 | |
---|
1206 | Here is the output of @samp{diff -n lao tzu} (@pxref{Sample |
---|
1207 | diff Input}, for the complete contents of the two files): |
---|
1208 | |
---|
1209 | @example |
---|
1210 | d1 2 |
---|
1211 | d4 1 |
---|
1212 | a4 2 |
---|
1213 | The named is the mother of all things. |
---|
1214 | |
---|
1215 | a11 3 |
---|
1216 | They both may be called deep and profound. |
---|
1217 | Deeper and more profound, |
---|
1218 | The door of all subtleties! |
---|
1219 | @end example |
---|
1220 | |
---|
1221 | @node If-then-else, , Scripts, Output Formats |
---|
1222 | @section Merging Files with If-then-else |
---|
1223 | @cindex merged output format |
---|
1224 | @cindex if-then-else output format |
---|
1225 | @cindex C if-then-else output format |
---|
1226 | @cindex @code{ifdef} output format |
---|
1227 | |
---|
1228 | You can use @code{diff} to merge two files of C source code. The output |
---|
1229 | of @code{diff} in this format contains all the lines of both files. |
---|
1230 | Lines common to both files are output just once; the differing parts are |
---|
1231 | separated by the C preprocessor directives @code{#ifdef @var{name}} or |
---|
1232 | @code{#ifndef @var{name}}, @code{#else}, and @code{#endif}. When |
---|
1233 | compiling the output, you select which version to use by either defining |
---|
1234 | or leaving undefined the macro @var{name}. |
---|
1235 | |
---|
1236 | To merge two files, use @code{diff} with the @samp{-D @var{name}} or |
---|
1237 | @samp{--ifdef=@var{name}} option. The argument @var{name} is the C |
---|
1238 | preprocessor identifier to use in the @code{#ifdef} and @code{#ifndef} |
---|
1239 | directives. |
---|
1240 | |
---|
1241 | For example, if you change an instance of @code{wait (&s)} to |
---|
1242 | @code{waitpid (-1, &s, 0)} and then merge the old and new files with |
---|
1243 | the @samp{--ifdef=HAVE_WAITPID} option, then the affected part of your code |
---|
1244 | might look like this: |
---|
1245 | |
---|
1246 | @example |
---|
1247 | do @{ |
---|
1248 | #ifndef HAVE_WAITPID |
---|
1249 | if ((w = wait (&s)) < 0 && errno != EINTR) |
---|
1250 | #else /* HAVE_WAITPID */ |
---|
1251 | if ((w = waitpid (-1, &s, 0)) < 0 && errno != EINTR) |
---|
1252 | #endif /* HAVE_WAITPID */ |
---|
1253 | return w; |
---|
1254 | @} while (w != child); |
---|
1255 | @end example |
---|
1256 | |
---|
1257 | You can specify formats for languages other than C by using line group |
---|
1258 | formats and line formats, as described in the next sections. |
---|
1259 | |
---|
1260 | @menu |
---|
1261 | * Line Group Formats:: Formats for general if-then-else line groups. |
---|
1262 | * Line Formats:: Formats for each line in a line group. |
---|
1263 | * Detailed If-then-else:: A detailed description of if-then-else format. |
---|
1264 | * Example If-then-else:: Sample if-then-else format output. |
---|
1265 | @end menu |
---|
1266 | |
---|
1267 | @node Line Group Formats, Line Formats, , If-then-else |
---|
1268 | @subsection Line Group Formats |
---|
1269 | @cindex line group formats |
---|
1270 | @cindex formats for if-then-else line groups |
---|
1271 | |
---|
1272 | Line group formats let you specify formats suitable for many |
---|
1273 | applications that allow if-then-else input, including programming |
---|
1274 | languages and text formatting languages. A line group format specifies |
---|
1275 | the output format for a contiguous group of similar lines. |
---|
1276 | |
---|
1277 | For example, the following command compares the TeX files @file{old} |
---|
1278 | and @file{new}, and outputs a merged file in which old regions are |
---|
1279 | surrounded by @samp{\begin@{em@}}-@samp{\end@{em@}} lines, and new |
---|
1280 | regions are surrounded by @samp{\begin@{bf@}}-@samp{\end@{bf@}} lines. |
---|
1281 | |
---|
1282 | @example |
---|
1283 | diff \ |
---|
1284 | --old-group-format='\begin@{em@} |
---|
1285 | %<\end@{em@} |
---|
1286 | ' \ |
---|
1287 | --new-group-format='\begin@{bf@} |
---|
1288 | %>\end@{bf@} |
---|
1289 | ' \ |
---|
1290 | old new |
---|
1291 | @end example |
---|
1292 | |
---|
1293 | The following command is equivalent to the above example, but it is a |
---|
1294 | little more verbose, because it spells out the default line group formats. |
---|
1295 | |
---|
1296 | @example |
---|
1297 | diff \ |
---|
1298 | --old-group-format='\begin@{em@} |
---|
1299 | %<\end@{em@} |
---|
1300 | ' \ |
---|
1301 | --new-group-format='\begin@{bf@} |
---|
1302 | %>\end@{bf@} |
---|
1303 | ' \ |
---|
1304 | --unchanged-group-format='%=' \ |
---|
1305 | --changed-group-format='\begin@{em@} |
---|
1306 | %<\end@{em@} |
---|
1307 | \begin@{bf@} |
---|
1308 | %>\end@{bf@} |
---|
1309 | ' \ |
---|
1310 | old new |
---|
1311 | @end example |
---|
1312 | |
---|
1313 | Here is a more advanced example, which outputs a diff listing with |
---|
1314 | headers containing line numbers in a ``plain English'' style. |
---|
1315 | |
---|
1316 | @example |
---|
1317 | diff \ |
---|
1318 | --unchanged-group-format='' \ |
---|
1319 | --old-group-format='-------- %dn line%(n=1?:s) deleted at %df: |
---|
1320 | %<' \ |
---|
1321 | --new-group-format='-------- %dN line%(N=1?:s) added after %de: |
---|
1322 | %>' \ |
---|
1323 | --changed-group-format='-------- %dn line%(n=1?:s) changed at %df: |
---|
1324 | %<-------- to: |
---|
1325 | %>' \ |
---|
1326 | old new |
---|
1327 | @end example |
---|
1328 | |
---|
1329 | To specify a line group format, use @code{diff} with one of the options |
---|
1330 | listed below. You can specify up to four line group formats, one for |
---|
1331 | each kind of line group. You should quote @var{format}, because it |
---|
1332 | typically contains shell metacharacters. |
---|
1333 | |
---|
1334 | @table @samp |
---|
1335 | @item --old-group-format=@var{format} |
---|
1336 | These line groups are hunks containing only lines from the first file. |
---|
1337 | The default old group format is the same as the changed group format if |
---|
1338 | it is specified; otherwise it is a format that outputs the line group as-is. |
---|
1339 | |
---|
1340 | @item --new-group-format=@var{format} |
---|
1341 | These line groups are hunks containing only lines from the second |
---|
1342 | file. The default new group format is same as the the changed group |
---|
1343 | format if it is specified; otherwise it is a format that outputs the |
---|
1344 | line group as-is. |
---|
1345 | |
---|
1346 | @item --changed-group-format=@var{format} |
---|
1347 | These line groups are hunks containing lines from both files. The |
---|
1348 | default changed group format is the concatenation of the old and new |
---|
1349 | group formats. |
---|
1350 | |
---|
1351 | @item --unchanged-group-format=@var{format} |
---|
1352 | These line groups contain lines common to both files. The default |
---|
1353 | unchanged group format is a format that outputs the line group as-is. |
---|
1354 | @end table |
---|
1355 | |
---|
1356 | In a line group format, ordinary characters represent themselves; |
---|
1357 | conversion specifications start with @samp{%} and have one of the |
---|
1358 | following forms. |
---|
1359 | |
---|
1360 | @table @samp |
---|
1361 | @item %< |
---|
1362 | stands for the lines from the first file, including the trailing newline. |
---|
1363 | Each line is formatted according to the old line format (@pxref{Line Formats}). |
---|
1364 | |
---|
1365 | @item %> |
---|
1366 | stands for the lines from the second file, including the trailing newline. |
---|
1367 | Each line is formatted according to the new line format. |
---|
1368 | |
---|
1369 | @item %= |
---|
1370 | stands for the lines common to both files, including the trailing newline. |
---|
1371 | Each line is formatted according to the unchanged line format. |
---|
1372 | |
---|
1373 | @item %% |
---|
1374 | stands for @samp{%}. |
---|
1375 | |
---|
1376 | @item %c'@var{C}' |
---|
1377 | where @var{C} is a single character, stands for @var{C}. |
---|
1378 | @var{C} may not be a backslash or an apostrophe. |
---|
1379 | For example, @samp{%c':'} stands for a colon, even inside |
---|
1380 | the then-part of an if-then-else format, which a colon would |
---|
1381 | normally terminate. |
---|
1382 | |
---|
1383 | @item %c'\@var{O}' |
---|
1384 | where @var{O} is a string of 1, 2, or 3 octal digits, |
---|
1385 | stands for the character with octal code @var{O}. |
---|
1386 | For example, @samp{%c'\0'} stands for a null character. |
---|
1387 | |
---|
1388 | @item @var{F}@var{n} |
---|
1389 | where @var{F} is a @code{printf} conversion specification and @var{n} is one |
---|
1390 | of the following letters, stands for @var{n}'s value formatted with @var{F}. |
---|
1391 | |
---|
1392 | @table @samp |
---|
1393 | @item e |
---|
1394 | The line number of the line just before the group in the old file. |
---|
1395 | |
---|
1396 | @item f |
---|
1397 | The line number of the first line in the group in the old file; |
---|
1398 | equals @var{e} + 1. |
---|
1399 | |
---|
1400 | @item l |
---|
1401 | The line number of the last line in the group in the old file. |
---|
1402 | |
---|
1403 | @item m |
---|
1404 | The line number of the line just after the group in the old file; |
---|
1405 | equals @var{l} + 1. |
---|
1406 | |
---|
1407 | @item n |
---|
1408 | The number of lines in the group in the old file; equals @var{l} - @var{f} + 1. |
---|
1409 | |
---|
1410 | @item E, F, L, M, N |
---|
1411 | Likewise, for lines in the new file. |
---|
1412 | |
---|
1413 | @end table |
---|
1414 | |
---|
1415 | The @code{printf} conversion specification can be @samp{%d}, |
---|
1416 | @samp{%o}, @samp{%x}, or @samp{%X}, specifying decimal, octal, |
---|
1417 | lower case hexadecimal, or upper case hexadecimal output |
---|
1418 | respectively. After the @samp{%} the following options can appear in |
---|
1419 | sequence: a @samp{-} specifying left-justification; an integer |
---|
1420 | specifying the minimum field width; and a period followed by an |
---|
1421 | optional integer specifying the minimum number of digits. |
---|
1422 | For example, @samp{%5dN} prints the number of new lines in the group |
---|
1423 | in a field of width 5 characters, using the @code{printf} format @code{"%5d"}. |
---|
1424 | |
---|
1425 | @item (@var{A}=@var{B}?@var{T}:@var{E}) |
---|
1426 | If @var{A} equals @var{B} then @var{T} else @var{E}. |
---|
1427 | @var{A} and @var{B} are each either a decimal constant |
---|
1428 | or a single letter interpreted as above. |
---|
1429 | This format spec is equivalent to @var{T} if |
---|
1430 | @var{A}'s value equals @var{B}'s; otherwise it is equivalent to @var{E}. |
---|
1431 | |
---|
1432 | For example, @samp{%(N=0?no:%dN) line%(N=1?:s)} is equivalent to |
---|
1433 | @samp{no lines} if @var{N} (the number of lines in the group in the the |
---|
1434 | new file) is 0, to @samp{1 line} if @var{N} is 1, and to @samp{%dN lines} |
---|
1435 | otherwise. |
---|
1436 | @end table |
---|
1437 | |
---|
1438 | @node Line Formats, Detailed If-then-else, Line Group Formats, If-then-else |
---|
1439 | @subsection Line Formats |
---|
1440 | @cindex line formats |
---|
1441 | |
---|
1442 | Line formats control how each line taken from an input file is |
---|
1443 | output as part of a line group in if-then-else format. |
---|
1444 | |
---|
1445 | For example, the following command outputs text with a one-column |
---|
1446 | change indicator to the left of the text. The first column of output |
---|
1447 | is @samp{-} for deleted lines, @samp{|} for added lines, and a space |
---|
1448 | for unchanged lines. The formats contain newline characters where |
---|
1449 | newlines are desired on output. |
---|
1450 | |
---|
1451 | @example |
---|
1452 | diff \ |
---|
1453 | --old-line-format='-%l |
---|
1454 | ' \ |
---|
1455 | --new-line-format='|%l |
---|
1456 | ' \ |
---|
1457 | --unchanged-line-format=' %l |
---|
1458 | ' \ |
---|
1459 | old new |
---|
1460 | @end example |
---|
1461 | |
---|
1462 | To specify a line format, use one of the following options. You should |
---|
1463 | quote @var{format}, since it often contains shell metacharacters. |
---|
1464 | |
---|
1465 | @table @samp |
---|
1466 | @item --old-line-format=@var{format} |
---|
1467 | formats lines just from the first file. |
---|
1468 | |
---|
1469 | @item --new-line-format=@var{format} |
---|
1470 | formats lines just from the second file. |
---|
1471 | |
---|
1472 | @item --unchanged-line-format=@var{format} |
---|
1473 | formats lines common to both files. |
---|
1474 | |
---|
1475 | @item --line-format=@var{format} |
---|
1476 | formats all lines; in effect, it sets all three above options simultaneously. |
---|
1477 | @end table |
---|
1478 | |
---|
1479 | In a line format, ordinary characters represent themselves; |
---|
1480 | conversion specifications start with @samp{%} and have one of the |
---|
1481 | following forms. |
---|
1482 | |
---|
1483 | @table @samp |
---|
1484 | @item %l |
---|
1485 | stands for the the contents of the line, not counting its trailing |
---|
1486 | newline (if any). This format ignores whether the line is incomplete; |
---|
1487 | @xref{Incomplete Lines}. |
---|
1488 | |
---|
1489 | @item %L |
---|
1490 | stands for the the contents of the line, including its trailing newline |
---|
1491 | (if any). If a line is incomplete, this format preserves its |
---|
1492 | incompleteness. |
---|
1493 | |
---|
1494 | @item %% |
---|
1495 | stands for @samp{%}. |
---|
1496 | |
---|
1497 | @item %c'@var{C}' |
---|
1498 | where @var{C} is a single character, stands for @var{C}. |
---|
1499 | @var{C} may not be a backslash or an apostrophe. |
---|
1500 | For example, @samp{%c':'} stands for a colon. |
---|
1501 | |
---|
1502 | @item %c'\@var{O}' |
---|
1503 | where @var{O} is a string of 1, 2, or 3 octal digits, |
---|
1504 | stands for the character with octal code @var{O}. |
---|
1505 | For example, @samp{%c'\0'} stands for a null character. |
---|
1506 | |
---|
1507 | @item @var{F}n |
---|
1508 | where @var{F} is a @code{printf} conversion specification, |
---|
1509 | stands for the line number formatted with @var{F}. |
---|
1510 | For example, @samp{%.5dn} prints the line number using the |
---|
1511 | @code{printf} format @code{"%.5d"}. @xref{Line Group Formats}, for |
---|
1512 | more about printf conversion specifications. |
---|
1513 | |
---|
1514 | @end table |
---|
1515 | |
---|
1516 | The default line format is @samp{%l} followed by a newline character. |
---|
1517 | |
---|
1518 | If the input contains tab characters and it is important that they line |
---|
1519 | up on output, you should ensure that @samp{%l} or @samp{%L} in a line |
---|
1520 | format is just after a tab stop (e.g.@: by preceding @samp{%l} or |
---|
1521 | @samp{%L} with a tab character), or you should use the @samp{-t} or |
---|
1522 | @samp{--expand-tabs} option. |
---|
1523 | |
---|
1524 | Taken together, the line and line group formats let you specify many |
---|
1525 | different formats. For example, the following command uses a format |
---|
1526 | similar to @code{diff}'s normal format. You can tailor this command |
---|
1527 | to get fine control over @code{diff}'s output. |
---|
1528 | |
---|
1529 | @example |
---|
1530 | diff \ |
---|
1531 | --old-line-format='< %l |
---|
1532 | ' \ |
---|
1533 | --new-line-format='> %l |
---|
1534 | ' \ |
---|
1535 | --old-group-format='%df%(f=l?:,%dl)d%dE |
---|
1536 | %<' \ |
---|
1537 | --new-group-format='%dea%dF%(F=L?:,%dL) |
---|
1538 | %>' \ |
---|
1539 | --changed-group-format='%df%(f=l?:,%dl)c%dF%(F=L?:,%dL) |
---|
1540 | %<--- |
---|
1541 | %>' \ |
---|
1542 | --unchanged-group-format='' \ |
---|
1543 | old new |
---|
1544 | @end example |
---|
1545 | |
---|
1546 | @node Detailed If-then-else, Example If-then-else, Line Formats, If-then-else |
---|
1547 | @subsection Detailed Description of If-then-else Format |
---|
1548 | |
---|
1549 | For lines common to both files, @code{diff} uses the unchanged line |
---|
1550 | group format. For each hunk of differences in the merged output |
---|
1551 | format, if the hunk contains only lines from the first file, |
---|
1552 | @code{diff} uses the old line group format; if the hunk contains only |
---|
1553 | lines from the second file, @code{diff} uses the new group format; |
---|
1554 | otherwise, @code{diff} uses the changed group format. |
---|
1555 | |
---|
1556 | The old, new, and unchanged line formats specify the output format of |
---|
1557 | lines from the first file, lines from the second file, and lines common |
---|
1558 | to both files, respectively. |
---|
1559 | |
---|
1560 | The option @samp{--ifdef=@var{name}} is equivalent to |
---|
1561 | the following sequence of options using shell syntax: |
---|
1562 | |
---|
1563 | @example |
---|
1564 | --old-group-format='#ifndef @var{name} |
---|
1565 | %<#endif /* not @var{name} */ |
---|
1566 | ' \ |
---|
1567 | --new-group-format='#ifdef @var{name} |
---|
1568 | %>#endif /* @var{name} */ |
---|
1569 | ' \ |
---|
1570 | --unchanged-group-format='%=' \ |
---|
1571 | --changed-group-format='#ifndef @var{name} |
---|
1572 | %<#else /* @var{name} */ |
---|
1573 | %>#endif /* @var{name} */ |
---|
1574 | ' |
---|
1575 | @end example |
---|
1576 | |
---|
1577 | You should carefully check the @code{diff} output for proper nesting. |
---|
1578 | For example, when using the the @samp{-D @var{name}} or |
---|
1579 | @samp{--ifdef=@var{name}} option, you should check that if the |
---|
1580 | differing lines contain any of the C preprocessor directives |
---|
1581 | @samp{#ifdef}, @samp{#ifndef}, @samp{#else}, @samp{#elif}, or |
---|
1582 | @samp{#endif}, they are nested properly and match. If they don't, you |
---|
1583 | must make corrections manually. It is a good idea to carefully check |
---|
1584 | the resulting code anyway to make sure that it really does what you |
---|
1585 | want it to; depending on how the input files were produced, the output |
---|
1586 | might contain duplicate or otherwise incorrect code. |
---|
1587 | |
---|
1588 | The @code{patch} @samp{-D @var{name}} option behaves just like |
---|
1589 | the @code{diff} @samp{-D @var{name}} option, except it operates on |
---|
1590 | a file and a diff to produce a merged file; @xref{patch Options}. |
---|
1591 | |
---|
1592 | @node Example If-then-else, , Detailed If-then-else, If-then-else |
---|
1593 | @subsection An Example of If-then-else Format |
---|
1594 | |
---|
1595 | Here is the output of @samp{diff -DTWO lao tzu} (@pxref{Sample |
---|
1596 | diff Input}, for the complete contents of the two files): |
---|
1597 | |
---|
1598 | @example |
---|
1599 | #ifndef TWO |
---|
1600 | The Way that can be told of is not the eternal Way; |
---|
1601 | The name that can be named is not the eternal name. |
---|
1602 | #endif /* not TWO */ |
---|
1603 | The Nameless is the origin of Heaven and Earth; |
---|
1604 | #ifndef TWO |
---|
1605 | The Named is the mother of all things. |
---|
1606 | #else /* TWO */ |
---|
1607 | The named is the mother of all things. |
---|
1608 | |
---|
1609 | #endif /* TWO */ |
---|
1610 | Therefore let there always be non-being, |
---|
1611 | so we may see their subtlety, |
---|
1612 | And let there always be being, |
---|
1613 | so we may see their outcome. |
---|
1614 | The two are the same, |
---|
1615 | But after they are produced, |
---|
1616 | they have different names. |
---|
1617 | #ifdef TWO |
---|
1618 | They both may be called deep and profound. |
---|
1619 | Deeper and more profound, |
---|
1620 | The door of all subtleties! |
---|
1621 | #endif /* TWO */ |
---|
1622 | @end example |
---|
1623 | |
---|
1624 | @node Comparing Directories, Adjusting Output, Output Formats, Top |
---|
1625 | @chapter Comparing Directories |
---|
1626 | |
---|
1627 | You can use @code{diff} to compare some or all of the files in two |
---|
1628 | directory trees. When both file name arguments to @code{diff} are |
---|
1629 | directories, it compares each file that is contained in both |
---|
1630 | directories, examining file names in alphabetical order. Normally |
---|
1631 | @code{diff} is silent about pairs of files that contain no differences, |
---|
1632 | but if you use the @samp{-s} or @samp{--report-identical-files} option, |
---|
1633 | it reports pairs of identical files. Normally @code{diff} reports |
---|
1634 | subdirectories common to both directories without comparing |
---|
1635 | subdirectories' files, but if you use the @samp{-r} or |
---|
1636 | @samp{--recursive} option, it compares every corresponding pair of files |
---|
1637 | in the directory trees, as many levels deep as they go. |
---|
1638 | |
---|
1639 | For file names that are in only one of the directories, @code{diff} |
---|
1640 | normally does not show the contents of the file that exists; it reports |
---|
1641 | only that the file exists in that directory and not in the other. You |
---|
1642 | can make @code{diff} act as though the file existed but was empty in the |
---|
1643 | other directory, so that it outputs the entire contents of the file that |
---|
1644 | actually exists. (It is output as either an insertion or a |
---|
1645 | deletion, depending on whether it is in the first or the second |
---|
1646 | directory given.) To do this, use the @samp{-N} or @samp{--new-file} |
---|
1647 | option. |
---|
1648 | |
---|
1649 | If the older directory contains one or more large files that are not in |
---|
1650 | the newer directory, you can make the patch smaller by using the |
---|
1651 | @samp{-P} or @samp{--unidirectional-new-file} option instead of @samp{-N}. |
---|
1652 | This option is like @samp{-N} except that it only inserts the contents |
---|
1653 | of files that appear in the second directory but not the first (that is, |
---|
1654 | files that were added). At the top of the patch, write instructions for |
---|
1655 | the user applying the patch to remove the files that were deleted before |
---|
1656 | applying the patch. @xref{Making Patches}, for more discussion of |
---|
1657 | making patches for distribution. |
---|
1658 | |
---|
1659 | To ignore some files while comparing directories, use the @samp{-x |
---|
1660 | @var{pattern}} or @samp{--exclude=@var{pattern}} option. This option |
---|
1661 | ignores any files or subdirectories whose base names match the shell |
---|
1662 | pattern @var{pattern}. Unlike in the shell, a period at the start of |
---|
1663 | the base of a file name matches a wildcard at the start of a pattern. |
---|
1664 | You should enclose @var{pattern} in quotes so that the shell does not |
---|
1665 | expand it. For example, the option @samp{-x '*.[ao]'} ignores any file |
---|
1666 | whose name ends with @samp{.a} or @samp{.o}. |
---|
1667 | |
---|
1668 | This option accumulates if you specify it more than once. For example, |
---|
1669 | using the options @samp{-x 'RCS' -x '*,v'} ignores any file or |
---|
1670 | subdirectory whose base name is @samp{RCS} or ends with @samp{,v}. |
---|
1671 | |
---|
1672 | If you need to give this option many times, you can instead put the |
---|
1673 | patterns in a file, one pattern per line, and use the @samp{-X |
---|
1674 | @var{file}} or @samp{--exclude-from=@var{file}} option. |
---|
1675 | |
---|
1676 | If you have been comparing two directories and stopped partway through, |
---|
1677 | later you might want to continue where you left off. You can do this by |
---|
1678 | using the @samp{-S @var{file}} or @samp{--starting-file=@var{file}} |
---|
1679 | option. This compares only the file @var{file} and all alphabetically |
---|
1680 | later files in the topmost directory level. |
---|
1681 | |
---|
1682 | @node Adjusting Output, diff Performance, Comparing Directories, Top |
---|
1683 | @chapter Making @code{diff} Output Prettier |
---|
1684 | |
---|
1685 | @code{diff} provides several ways to adjust the appearance of its output. |
---|
1686 | These adjustments can be applied to any output format. |
---|
1687 | |
---|
1688 | @menu |
---|
1689 | * Tabs:: Preserving the alignment of tabstops. |
---|
1690 | * Pagination:: Page numbering and timestamping @code{diff} output. |
---|
1691 | @end menu |
---|
1692 | |
---|
1693 | @node Tabs, Pagination, , Adjusting Output |
---|
1694 | @section Preserving Tabstop Alignment |
---|
1695 | @cindex tabstop alignment |
---|
1696 | @cindex aligning tabstops |
---|
1697 | |
---|
1698 | The lines of text in some of the @code{diff} output formats are preceded |
---|
1699 | by one or two characters that indicate whether the text is inserted, |
---|
1700 | deleted, or changed. The addition of those characters can cause tabs to |
---|
1701 | move to the next tabstop, throwing off the alignment of columns in the |
---|
1702 | line. GNU @code{diff} provides two ways to make tab-aligned columns |
---|
1703 | line up correctly. |
---|
1704 | |
---|
1705 | The first way is to have @code{diff} convert all tabs into the correct |
---|
1706 | number of spaces before outputting them; select this method with the |
---|
1707 | @samp{-t} or @samp{--expand-tabs} option. @code{diff} assumes that |
---|
1708 | tabstops are set every 8 columns. To use this form of output with |
---|
1709 | @code{patch}, you must give @code{patch} the @samp{-l} or |
---|
1710 | @samp{--ignore-white-space} option (@pxref{Changed White Space}, for more |
---|
1711 | information). |
---|
1712 | |
---|
1713 | The other method for making tabs line up correctly is to add a tab |
---|
1714 | character instead of a space after the indicator character at the |
---|
1715 | beginning of the line. This ensures that all following tab characters |
---|
1716 | are in the same position relative to tabstops that they were in the |
---|
1717 | original files, so that the output is aligned correctly. Its |
---|
1718 | disadvantage is that it can make long lines too long to fit on one line |
---|
1719 | of the screen or the paper. It also does not work with the unified |
---|
1720 | output format, which does not have a space character after the change |
---|
1721 | type indicator character. Select this method with the @samp{-T} or |
---|
1722 | @samp{--initial-tab} option. |
---|
1723 | |
---|
1724 | @node Pagination, , Tabs, Adjusting Output |
---|
1725 | @section Paginating @code{diff} Output |
---|
1726 | @cindex paginating @code{diff} output |
---|
1727 | |
---|
1728 | It can be convenient to have long output page-numbered and time-stamped. |
---|
1729 | The @samp{-l} and @samp{--paginate} options do this by sending the |
---|
1730 | @code{diff} output through the @code{pr} program. Here is what the page |
---|
1731 | header might look like for @samp{diff -lc lao tzu}: |
---|
1732 | |
---|
1733 | @example |
---|
1734 | Mar 11 13:37 1991 diff -lc lao tzu Page 1 |
---|
1735 | @end example |
---|
1736 | |
---|
1737 | @node diff Performance, Comparing Three Files, Adjusting Output, Top |
---|
1738 | @chapter @code{diff} Performance Tradeoffs |
---|
1739 | @cindex performance of @code{diff} |
---|
1740 | |
---|
1741 | GNU @code{diff} runs quite efficiently; however, in some circumstances |
---|
1742 | you can cause it to run faster or produce a more compact set of changes. |
---|
1743 | There are two ways that you can affect the performance of GNU |
---|
1744 | @code{diff} by changing the way it compares files. |
---|
1745 | |
---|
1746 | Performance has more than one dimension. These options improve one |
---|
1747 | aspect of performance at the cost of another, or they improve |
---|
1748 | performance in some cases while hurting it in others. |
---|
1749 | |
---|
1750 | The way that GNU @code{diff} determines which lines have changed always |
---|
1751 | comes up with a near-minimal set of differences. Usually it is good |
---|
1752 | enough for practical purposes. If the @code{diff} output is large, you |
---|
1753 | might want @code{diff} to use a modified algorithm that sometimes |
---|
1754 | produces a smaller set of differences. The @samp{-d} or |
---|
1755 | @samp{--minimal} option does this; however, it can also cause |
---|
1756 | @code{diff} to run more slowly than usual, so it is not the default |
---|
1757 | behavior. |
---|
1758 | |
---|
1759 | When the files you are comparing are large and have small groups of |
---|
1760 | changes scattered throughout them, you can use the @samp{-H} or |
---|
1761 | @samp{--speed-large-files} option to make a different modification to |
---|
1762 | the algorithm that @code{diff} uses. If the input files have a constant |
---|
1763 | small density of changes, this option speeds up the comparisons without |
---|
1764 | changing the output. If not, @code{diff} might produce a larger set of |
---|
1765 | differences; however, the output will still be correct. |
---|
1766 | |
---|
1767 | Normally @code{diff} discards the prefix and suffix that is common to |
---|
1768 | both files before it attempts to find a minimal set of differences. |
---|
1769 | This makes @code{diff} run faster, but occasionally it may produce |
---|
1770 | non-minimal output. The @samp{--horizon-lines=@var{lines}} option |
---|
1771 | prevents @code{diff} from discarding the last @var{lines} lines of the |
---|
1772 | prefix and the first @var{lines} lines of the suffix. This gives |
---|
1773 | @code{diff} further opportunities to find a minimal output. |
---|
1774 | |
---|
1775 | @node Comparing Three Files, diff3 Merging, diff Performance, Top |
---|
1776 | @chapter Comparing Three Files |
---|
1777 | @cindex comparing three files |
---|
1778 | @cindex format of @code{diff3} output |
---|
1779 | |
---|
1780 | Use the program @code{diff3} to compare three files and show any |
---|
1781 | differences among them. (@code{diff3} can also merge files; see |
---|
1782 | @ref{diff3 Merging}). |
---|
1783 | |
---|
1784 | The ``normal'' @code{diff3} output format shows each hunk of |
---|
1785 | differences without surrounding context. Hunks are labeled depending |
---|
1786 | on whether they are two-way or three-way, and lines are annotated by |
---|
1787 | their location in the input files. |
---|
1788 | |
---|
1789 | @xref{Invoking diff3}, for more information on how to run @code{diff3}. |
---|
1790 | |
---|
1791 | @menu |
---|
1792 | * Sample diff3 Input:: Sample @code{diff3} input for examples. |
---|
1793 | * Detailed diff3 Normal:: A detailed description of normal output format. |
---|
1794 | * diff3 Hunks:: The format of normal output format. |
---|
1795 | * Example diff3 Normal:: Sample output in the normal format. |
---|
1796 | @end menu |
---|
1797 | |
---|
1798 | @node Sample diff3 Input, Detailed diff3 Normal, , Comparing Three Files |
---|
1799 | @section A Third Sample Input File |
---|
1800 | @cindex @code{diff3} sample input |
---|
1801 | @cindex sample input for @code{diff3} |
---|
1802 | |
---|
1803 | Here is a third sample file that will be used in examples to illustrate |
---|
1804 | the output of @code{diff3} and how various options can change it. The |
---|
1805 | first two files are the same that we used for @code{diff} (@pxref{Sample |
---|
1806 | diff Input}). This is the third sample file, called @file{tao}: |
---|
1807 | |
---|
1808 | @example |
---|
1809 | The Way that can be told of is not the eternal Way; |
---|
1810 | The name that can be named is not the eternal name. |
---|
1811 | The Nameless is the origin of Heaven and Earth; |
---|
1812 | The named is the mother of all things. |
---|
1813 | |
---|
1814 | Therefore let there always be non-being, |
---|
1815 | so we may see their subtlety, |
---|
1816 | And let there always be being, |
---|
1817 | so we may see their result. |
---|
1818 | The two are the same, |
---|
1819 | But after they are produced, |
---|
1820 | they have different names. |
---|
1821 | |
---|
1822 | -- The Way of Lao-Tzu, tr. Wing-tsit Chan |
---|
1823 | @end example |
---|
1824 | |
---|
1825 | @node Detailed diff3 Normal, diff3 Hunks, Sample diff3 Input, Comparing Three Files |
---|
1826 | @section Detailed Description of @code{diff3} Normal Format |
---|
1827 | |
---|
1828 | Each hunk begins with a line marked @samp{====}. Three-way hunks have |
---|
1829 | plain @samp{====} lines, and two-way hunks have @samp{1}, @samp{2}, or |
---|
1830 | @samp{3} appended to specify which of the three input files differ in |
---|
1831 | that hunk. The hunks contain copies of two or three sets of input |
---|
1832 | lines each preceded by one or two commands identifying where the lines |
---|
1833 | came from. |
---|
1834 | |
---|
1835 | Normally, two spaces precede each copy of an input line to distinguish |
---|
1836 | it from the commands. But with the @samp{-T} or @samp{--initial-tab} |
---|
1837 | option, @code{diff3} uses a tab instead of two spaces; this lines up |
---|
1838 | tabs correctly. @xref{Tabs}, for more information. |
---|
1839 | |
---|
1840 | Commands take the following forms: |
---|
1841 | |
---|
1842 | @table @samp |
---|
1843 | @item @var{file}:@var{l}a |
---|
1844 | This hunk appears after line @var{l} of file @var{file}, and |
---|
1845 | contains no lines in that file. To edit this file to yield the other |
---|
1846 | files, one must append hunk lines taken from the other files. For |
---|
1847 | example, @samp{1:11a} means that the hunk follows line 11 in the first |
---|
1848 | file and contains no lines from that file. |
---|
1849 | |
---|
1850 | @item @var{file}:@var{r}c |
---|
1851 | This hunk contains the lines in the range @var{r} of file @var{file}. |
---|
1852 | The range @var{r} is a comma-separated pair of line numbers, or just one |
---|
1853 | number if the range is a singleton. To edit this file to yield the |
---|
1854 | other files, one must change the specified lines to be the lines taken |
---|
1855 | from the other files. For example, @samp{2:11,13c} means that the hunk |
---|
1856 | contains lines 11 through 13 from the second file. |
---|
1857 | @end table |
---|
1858 | |
---|
1859 | If the last line in a set of input lines is incomplete |
---|
1860 | (@pxref{Incomplete Lines}), it is distinguished on output from a full |
---|
1861 | line by a following line that starts with @samp{\}. |
---|
1862 | |
---|
1863 | @node diff3 Hunks, Example diff3 Normal, Detailed diff3 Normal, Comparing Three Files |
---|
1864 | @section @code{diff3} Hunks |
---|
1865 | @cindex hunks for @code{diff3} |
---|
1866 | @cindex @code{diff3} hunks |
---|
1867 | |
---|
1868 | Groups of lines that differ in two or three of the input files are |
---|
1869 | called @dfn{diff3 hunks}, by analogy with @code{diff} hunks |
---|
1870 | (@pxref{Hunks}). If all three input files differ in a @code{diff3} |
---|
1871 | hunk, the hunk is called a @dfn{three-way hunk}; if just two input files |
---|
1872 | differ, it is a @dfn{two-way hunk}. |
---|
1873 | |
---|
1874 | As with @code{diff}, several solutions are possible. When comparing the |
---|
1875 | files @samp{A}, @samp{B}, and @samp{C}, @code{diff3} normally finds |
---|
1876 | @code{diff3} hunks by merging the two-way hunks output by the two |
---|
1877 | commands @samp{diff A B} and @samp{diff A C}. This does not necessarily |
---|
1878 | minimize the size of the output, but exceptions should be rare. |
---|
1879 | |
---|
1880 | For example, suppose @file{F} contains the three lines @samp{a}, |
---|
1881 | @samp{b}, @samp{f}, @file{G} contains the lines @samp{g}, @samp{b}, |
---|
1882 | @samp{g}, and @file{H} contains the lines @samp{a}, @samp{b}, |
---|
1883 | @samp{h}. @samp{diff3 F G H} might output the following: |
---|
1884 | |
---|
1885 | @example |
---|
1886 | ====2 |
---|
1887 | 1:1c |
---|
1888 | 3:1c |
---|
1889 | a |
---|
1890 | 2:1c |
---|
1891 | g |
---|
1892 | ==== |
---|
1893 | 1:3c |
---|
1894 | f |
---|
1895 | 2:3c |
---|
1896 | g |
---|
1897 | 3:3c |
---|
1898 | h |
---|
1899 | @end example |
---|
1900 | |
---|
1901 | @noindent |
---|
1902 | because it found a two-way hunk containing @samp{a} in the first and |
---|
1903 | third files and @samp{g} in the second file, then the single line |
---|
1904 | @samp{b} common to all three files, then a three-way hunk containing |
---|
1905 | the last line of each file. |
---|
1906 | |
---|
1907 | @node Example diff3 Normal, , diff3 Hunks, Comparing Three Files |
---|
1908 | @section An Example of @code{diff3} Normal Format |
---|
1909 | |
---|
1910 | Here is the output of the command @samp{diff3 lao tzu tao} |
---|
1911 | (@pxref{Sample diff3 Input}, for the complete contents of the files). |
---|
1912 | Notice that it shows only the lines that are different among the three |
---|
1913 | files. |
---|
1914 | |
---|
1915 | @example |
---|
1916 | ====2 |
---|
1917 | 1:1,2c |
---|
1918 | 3:1,2c |
---|
1919 | The Way that can be told of is not the eternal Way; |
---|
1920 | The name that can be named is not the eternal name. |
---|
1921 | 2:0a |
---|
1922 | ====1 |
---|
1923 | 1:4c |
---|
1924 | The Named is the mother of all things. |
---|
1925 | 2:2,3c |
---|
1926 | 3:4,5c |
---|
1927 | The named is the mother of all things. |
---|
1928 | |
---|
1929 | ====3 |
---|
1930 | 1:8c |
---|
1931 | 2:7c |
---|
1932 | so we may see their outcome. |
---|
1933 | 3:9c |
---|
1934 | so we may see their result. |
---|
1935 | ==== |
---|
1936 | 1:11a |
---|
1937 | 2:11,13c |
---|
1938 | They both may be called deep and profound. |
---|
1939 | Deeper and more profound, |
---|
1940 | The door of all subtleties! |
---|
1941 | 3:13,14c |
---|
1942 | |
---|
1943 | -- The Way of Lao-Tzu, tr. Wing-tsit Chan |
---|
1944 | @end example |
---|
1945 | |
---|
1946 | @node diff3 Merging, Interactive Merging, Comparing Three Files, Top |
---|
1947 | @chapter Merging From a Common Ancestor |
---|
1948 | @cindex merging from a common ancestor |
---|
1949 | |
---|
1950 | When two people have made changes to copies of the same file, |
---|
1951 | @code{diff3} can produce a merged output that contains both sets of |
---|
1952 | changes together with warnings about conflicts. |
---|
1953 | |
---|
1954 | One might imagine programs with names like @code{diff4} and @code{diff5} |
---|
1955 | to compare more than three files simultaneously, but in practice the |
---|
1956 | need rarely arises. You can use @code{diff3} to merge three or more |
---|
1957 | sets of changes to a file by merging two change sets at a time. |
---|
1958 | |
---|
1959 | @code{diff3} can incorporate changes from two modified versions into a |
---|
1960 | common preceding version. This lets you merge the sets of changes |
---|
1961 | represented by the two newer files. Specify the common ancestor version |
---|
1962 | as the second argument and the two newer versions as the first and third |
---|
1963 | arguments, like this: |
---|
1964 | |
---|
1965 | @example |
---|
1966 | diff3 @var{mine} @var{older} @var{yours} |
---|
1967 | @end example |
---|
1968 | |
---|
1969 | @noindent |
---|
1970 | You can remember the order of the arguments by noting that they are in |
---|
1971 | alphabetical order. |
---|
1972 | |
---|
1973 | @cindex conflict |
---|
1974 | @cindex overlap |
---|
1975 | You can think of this as subtracting @var{older} from @var{yours} and |
---|
1976 | adding the result to @var{mine}, or as merging into @var{mine} the |
---|
1977 | changes that would turn @var{older} into @var{yours}. This merging is |
---|
1978 | well-defined as long as @var{mine} and @var{older} match in the |
---|
1979 | neighborhood of each such change. This fails to be true when all three |
---|
1980 | input files differ or when only @var{older} differs; we call this |
---|
1981 | a @dfn{conflict}. When all three input files differ, we call the |
---|
1982 | conflict an @dfn{overlap}. |
---|
1983 | |
---|
1984 | @code{diff3} gives you several ways to handle overlaps and conflicts. |
---|
1985 | You can omit overlaps or conflicts, or select only overlaps, |
---|
1986 | or mark conflicts with special @samp{<<<<<<<} and @samp{>>>>>>>} lines. |
---|
1987 | |
---|
1988 | @code{diff3} can output the merge results as an @code{ed} script that |
---|
1989 | that can be applied to the first file to yield the merged output. |
---|
1990 | However, it is usually better to have @code{diff3} generate the merged |
---|
1991 | output directly; this bypasses some problems with @code{ed}. |
---|
1992 | |
---|
1993 | @menu |
---|
1994 | * Which Changes:: Selecting changes to incorporate. |
---|
1995 | * Marking Conflicts:: Marking conflicts. |
---|
1996 | * Bypassing ed:: Generating merged output directly. |
---|
1997 | * Merging Incomplete Lines:: How @code{diff3} merges incomplete lines. |
---|
1998 | * Saving the Changed File:: Emulating System V behavior. |
---|
1999 | @end menu |
---|
2000 | |
---|
2001 | @node Which Changes, Marking Conflicts, , diff3 Merging |
---|
2002 | @section Selecting Which Changes to Incorporate |
---|
2003 | @cindex overlapping change, selection of |
---|
2004 | @cindex unmerged change |
---|
2005 | |
---|
2006 | You can select all unmerged changes from @var{older} to @var{yours} for merging |
---|
2007 | into @var{mine} with the @samp{-e} or @samp{--ed} option. You can |
---|
2008 | select only the nonoverlapping unmerged changes with @samp{-3} or |
---|
2009 | @samp{--easy-only}, and you can select only the overlapping changes with |
---|
2010 | @samp{-x} or @samp{--overlap-only}. |
---|
2011 | |
---|
2012 | The @samp{-e}, @samp{-3} and @samp{-x} options select only |
---|
2013 | @dfn{unmerged changes}, i.e.@: changes where @var{mine} and @var{yours} |
---|
2014 | differ; they ignore changes from @var{older} to @var{yours} where |
---|
2015 | @var{mine} and @var{yours} are identical, because they assume that such |
---|
2016 | changes have already been merged. If this assumption is not a safe |
---|
2017 | one, you can use the @samp{-A} or @samp{--show-all} option |
---|
2018 | (@pxref{Marking Conflicts}). |
---|
2019 | |
---|
2020 | Here is the output of the command @code{diff3} with each of these three |
---|
2021 | options (@pxref{Sample diff3 Input}, for the complete contents of the files). |
---|
2022 | Notice that @samp{-e} outputs the union of the disjoint sets of changes |
---|
2023 | output by @samp{-3} and @samp{-x}. |
---|
2024 | |
---|
2025 | Output of @samp{diff3 -e lao tzu tao}: |
---|
2026 | @example |
---|
2027 | 11a |
---|
2028 | |
---|
2029 | -- The Way of Lao-Tzu, tr. Wing-tsit Chan |
---|
2030 | . |
---|
2031 | 8c |
---|
2032 | so we may see their result. |
---|
2033 | . |
---|
2034 | @end example |
---|
2035 | |
---|
2036 | Output of @samp{diff3 -3 lao tzu tao}: |
---|
2037 | @example |
---|
2038 | 8c |
---|
2039 | so we may see their result. |
---|
2040 | . |
---|
2041 | @end example |
---|
2042 | |
---|
2043 | Output of @samp{diff3 -x lao tzu tao}: |
---|
2044 | @example |
---|
2045 | 11a |
---|
2046 | |
---|
2047 | -- The Way of Lao-Tzu, tr. Wing-tsit Chan |
---|
2048 | . |
---|
2049 | @end example |
---|
2050 | |
---|
2051 | @node Marking Conflicts, Bypassing ed, Which Changes, diff3 Merging |
---|
2052 | @section Marking Conflicts |
---|
2053 | @cindex conflict marking |
---|
2054 | @cindex @samp{<<<<<<<} for marking conflicts |
---|
2055 | |
---|
2056 | @code{diff3} can mark conflicts in the merged output by |
---|
2057 | bracketing them with special marker lines. A conflict |
---|
2058 | that comes from two files @var{A} and @var{B} is marked as follows: |
---|
2059 | |
---|
2060 | @example |
---|
2061 | <<<<<<< @var{A} |
---|
2062 | @r{lines from @var{A}} |
---|
2063 | ======= |
---|
2064 | @r{lines from @var{B}} |
---|
2065 | >>>>>>> @var{B} |
---|
2066 | @end example |
---|
2067 | |
---|
2068 | A conflict that comes from three files @var{A}, @var{B} and @var{C} is |
---|
2069 | marked as follows: |
---|
2070 | |
---|
2071 | @example |
---|
2072 | <<<<<<< @var{A} |
---|
2073 | @r{lines from @var{A}} |
---|
2074 | ||||||| @var{B} |
---|
2075 | @r{lines from @var{B}} |
---|
2076 | ======= |
---|
2077 | @r{lines from @var{C}} |
---|
2078 | >>>>>>> @var{C} |
---|
2079 | @end example |
---|
2080 | |
---|
2081 | The @samp{-A} or @samp{--show-all} option acts like the @samp{-e} |
---|
2082 | option, except that it brackets conflicts, and it outputs all changes |
---|
2083 | from @var{older} to @var{yours}, not just the unmerged changes. Thus, |
---|
2084 | given the sample input files (@pxref{Sample diff3 Input}), @samp{diff3 |
---|
2085 | -A lao tzu tao} puts brackets around the conflict where only @file{tzu} |
---|
2086 | differs: |
---|
2087 | |
---|
2088 | @example |
---|
2089 | <<<<<<< tzu |
---|
2090 | ======= |
---|
2091 | The Way that can be told of is not the eternal Way; |
---|
2092 | The name that can be named is not the eternal name. |
---|
2093 | >>>>>>> tao |
---|
2094 | @end example |
---|
2095 | |
---|
2096 | And it outputs the three-way conflict as follows: |
---|
2097 | |
---|
2098 | @example |
---|
2099 | <<<<<<< lao |
---|
2100 | ||||||| tzu |
---|
2101 | They both may be called deep and profound. |
---|
2102 | Deeper and more profound, |
---|
2103 | The door of all subtleties! |
---|
2104 | ======= |
---|
2105 | |
---|
2106 | -- The Way of Lao-Tzu, tr. Wing-tsit Chan |
---|
2107 | >>>>>>> tao |
---|
2108 | @end example |
---|
2109 | |
---|
2110 | The @samp{-E} or @samp{--show-overlap} option outputs less information |
---|
2111 | than the @samp{-A} or @samp{--show-all} option, because it outputs only |
---|
2112 | unmerged changes, and it never outputs the contents of the second |
---|
2113 | file. Thus the @samp{-E} option acts like the @samp{-e} option, |
---|
2114 | except that it brackets the first and third files from three-way |
---|
2115 | overlapping changes. Similarly, @samp{-X} acts like @samp{-x}, except |
---|
2116 | it brackets all its (necessarily overlapping) changes. For example, |
---|
2117 | for the three-way overlapping change above, the @samp{-E} and @samp{-X} |
---|
2118 | options output the following: |
---|
2119 | |
---|
2120 | @example |
---|
2121 | <<<<<<< lao |
---|
2122 | ======= |
---|
2123 | |
---|
2124 | -- The Way of Lao-Tzu, tr. Wing-tsit Chan |
---|
2125 | >>>>>>> tao |
---|
2126 | @end example |
---|
2127 | |
---|
2128 | If you are comparing files that have meaningless or uninformative names, |
---|
2129 | you can use the @samp{-L @var{label}} or @samp{--label=@var{label}} |
---|
2130 | option to show alternate names in the @samp{<<<<<<<}, @samp{|||||||} |
---|
2131 | and @samp{>>>>>>>} brackets. This option can be given up to three |
---|
2132 | times, once for each input file. Thus @samp{diff3 -A -L X -L Y -L Z A |
---|
2133 | B C} acts like @samp{diff3 -A A B C}, except that the output looks like |
---|
2134 | it came from files named @samp{X}, @samp{Y} and @samp{Z} rather than |
---|
2135 | from files named @samp{A}, @samp{B} and @samp{C}. |
---|
2136 | |
---|
2137 | @node Bypassing ed, Merging Incomplete Lines, Marking Conflicts, diff3 Merging |
---|
2138 | @section Generating the Merged Output Directly |
---|
2139 | @cindex merged @code{diff3} format |
---|
2140 | |
---|
2141 | With the @samp{-m} or @samp{--merge} option, @code{diff3} outputs the |
---|
2142 | merged file directly. This is more efficient than using @code{ed} to |
---|
2143 | generate it, and works even with non-text files that @code{ed} would |
---|
2144 | reject. If you specify @samp{-m} without an @code{ed} script option, |
---|
2145 | @samp{-A} (@samp{--show-all}) is assumed. |
---|
2146 | |
---|
2147 | For example, the command @samp{diff3 -m lao tzu tao} |
---|
2148 | (@pxref{Sample diff3 Input} for a copy of the input files) would output |
---|
2149 | the following: |
---|
2150 | |
---|
2151 | @example |
---|
2152 | <<<<<<< tzu |
---|
2153 | ======= |
---|
2154 | The Way that can be told of is not the eternal Way; |
---|
2155 | The name that can be named is not the eternal name. |
---|
2156 | >>>>>>> tao |
---|
2157 | The Nameless is the origin of Heaven and Earth; |
---|
2158 | The Named is the mother of all things. |
---|
2159 | Therefore let there always be non-being, |
---|
2160 | so we may see their subtlety, |
---|
2161 | And let there always be being, |
---|
2162 | so we may see their result. |
---|
2163 | The two are the same, |
---|
2164 | But after they are produced, |
---|
2165 | they have different names. |
---|
2166 | <<<<<<< lao |
---|
2167 | ||||||| tzu |
---|
2168 | They both may be called deep and profound. |
---|
2169 | Deeper and more profound, |
---|
2170 | The door of all subtleties! |
---|
2171 | ======= |
---|
2172 | |
---|
2173 | -- The Way of Lao-Tzu, tr. Wing-tsit Chan |
---|
2174 | >>>>>>> tao |
---|
2175 | @end example |
---|
2176 | |
---|
2177 | @node Merging Incomplete Lines, Saving the Changed File, Bypassing ed, diff3 Merging |
---|
2178 | @section How @code{diff3} Merges Incomplete Lines |
---|
2179 | @cindex incomplete line merging |
---|
2180 | |
---|
2181 | With @samp{-m}, incomplete lines (@pxref{Incomplete Lines}) are simply |
---|
2182 | copied to the output as they are found; if the merged output ends in an |
---|
2183 | conflict and one of the input files ends in an incomplete |
---|
2184 | line, succeeding @samp{|||||||}, @samp{=======} or @samp{>>>>>>>} |
---|
2185 | brackets appear somewhere other than the start of a line because |
---|
2186 | they are appended to the incomplete line. |
---|
2187 | |
---|
2188 | Without @samp{-m}, if an @code{ed} script option is specified and an |
---|
2189 | incomplete line is found, @code{diff3} generates a warning and acts as |
---|
2190 | if a newline had been present. |
---|
2191 | |
---|
2192 | @node Saving the Changed File, , Merging Incomplete Lines, diff3 Merging |
---|
2193 | @section Saving the Changed File |
---|
2194 | @cindex System V @code{diff3} compatibility |
---|
2195 | |
---|
2196 | Traditional Unix @code{diff3} generates an @code{ed} script without the |
---|
2197 | trailing @samp{w} and and @samp{q} commands that save the changes. |
---|
2198 | System V @code{diff3} generates these extra commands. GNU @code{diff3} |
---|
2199 | normally behaves like traditional Unix @code{diff3}, but with the |
---|
2200 | @samp{-i} option it behaves like System V @code{diff3} and appends the |
---|
2201 | @samp{w} and @samp{q} commands. |
---|
2202 | |
---|
2203 | The @samp{-i} option requires one of the @code{ed} script options |
---|
2204 | @samp{-AeExX3}, and is incompatible with the merged output option |
---|
2205 | @samp{-m}. |
---|
2206 | |
---|
2207 | @node Interactive Merging, Merging with patch, diff3 Merging, Top |
---|
2208 | @chapter Interactive Merging with @code{sdiff} |
---|
2209 | @cindex diff merging |
---|
2210 | @cindex interactive merging |
---|
2211 | |
---|
2212 | With @code{sdiff}, you can merge two files interactively based on a |
---|
2213 | side-by-side @samp{-y} format comparison (@pxref{Side by Side}). Use |
---|
2214 | @samp{-o @var{file}} or @samp{--output=@var{file}} to specify where to |
---|
2215 | put the merged text. @xref{Invoking sdiff}, for more details on the |
---|
2216 | options to @code{sdiff}. |
---|
2217 | |
---|
2218 | Another way to merge files interactively is to use the Emacs Lisp |
---|
2219 | package @code{emerge}. @xref{emerge, , emerge, emacs, The GNU Emacs |
---|
2220 | Manual}, for more information. |
---|
2221 | |
---|
2222 | @menu |
---|
2223 | * sdiff Option Summary::Summary of @code{sdiff} options. |
---|
2224 | * Merge Commands:: Merging two files interactively. |
---|
2225 | @end menu |
---|
2226 | |
---|
2227 | @node sdiff Option Summary, Merge Commands, , Interactive Merging |
---|
2228 | @section Specifying @code{diff} Options to @code{sdiff} |
---|
2229 | @cindex @code{sdiff} output format |
---|
2230 | |
---|
2231 | The following @code{sdiff} options have the same meaning as for |
---|
2232 | @code{diff}. @xref{diff Options}, for the use of these options. |
---|
2233 | |
---|
2234 | @example |
---|
2235 | -a -b -d -i -t -v |
---|
2236 | -B -H -I @var{regexp} |
---|
2237 | |
---|
2238 | --ignore-blank-lines --ignore-case |
---|
2239 | --ignore-matching-lines=@var{regexp} --ignore-space-change |
---|
2240 | --left-column --minimal --speed-large-files |
---|
2241 | --suppress-common-lines --expand-tabs |
---|
2242 | --text --version --width=@var{columns} |
---|
2243 | @end example |
---|
2244 | |
---|
2245 | For historical reasons, @code{sdiff} has alternate names for some |
---|
2246 | options. The @samp{-l} option is equivalent to the @samp{--left-column} |
---|
2247 | option, and similarly @samp{-s} is equivalent to |
---|
2248 | @samp{--suppress-common-lines}. The meaning of the @code{sdiff} |
---|
2249 | @samp{-w} and @samp{-W} options is interchanged from that of |
---|
2250 | @code{diff}: with @code{sdiff}, @samp{-w @var{columns}} is equivalent to |
---|
2251 | @samp{--width=@var{columns}}, and @samp{-W} is equivalent to |
---|
2252 | @samp{--ignore-all-space}. @code{sdiff} without the @samp{-o} option is |
---|
2253 | equivalent to @code{diff} with the @samp{-y} or @samp{--side-by-side} |
---|
2254 | option (@pxref{Side by Side}). |
---|
2255 | |
---|
2256 | @node Merge Commands, , sdiff Option Summary, Interactive Merging |
---|
2257 | @section Merge Commands |
---|
2258 | @cindex merge commands |
---|
2259 | @cindex merging interactively |
---|
2260 | |
---|
2261 | Groups of common lines, with a blank gutter, are copied from the first |
---|
2262 | file to the output. After each group of differing lines, @code{sdiff} |
---|
2263 | prompts with @samp{%} and pauses, waiting for one of the following |
---|
2264 | commands. Follow each command with @key{RET}. |
---|
2265 | |
---|
2266 | @table @samp |
---|
2267 | @item e |
---|
2268 | Discard both versions. |
---|
2269 | Invoke a text editor on an empty temporary file, |
---|
2270 | then copy the resulting file to the output. |
---|
2271 | |
---|
2272 | @item eb |
---|
2273 | Concatenate the two versions, edit the result in a temporary file, |
---|
2274 | then copy the edited result to the output. |
---|
2275 | |
---|
2276 | @item el |
---|
2277 | Edit a copy of the left version, then copy the result to the output. |
---|
2278 | |
---|
2279 | @item er |
---|
2280 | Edit a copy of the right version, then copy the result to the output. |
---|
2281 | |
---|
2282 | @item l |
---|
2283 | Copy the left version to the output. |
---|
2284 | |
---|
2285 | @item q |
---|
2286 | Quit. |
---|
2287 | |
---|
2288 | @item r |
---|
2289 | Copy the right version to the output. |
---|
2290 | |
---|
2291 | @item s |
---|
2292 | Silently copy common lines. |
---|
2293 | |
---|
2294 | @item v |
---|
2295 | Verbosely copy common lines. This is the default. |
---|
2296 | @end table |
---|
2297 | |
---|
2298 | The text editor invoked is specified by the @code{EDITOR} environment |
---|
2299 | variable if it is set. The default is system-dependent. |
---|
2300 | |
---|
2301 | @node Merging with patch, Making Patches, Interactive Merging, Top |
---|
2302 | @chapter Merging with @code{patch} |
---|
2303 | |
---|
2304 | @code{patch} takes comparison output produced by @code{diff} and applies |
---|
2305 | the differences to a copy of the original file, producing a patched |
---|
2306 | version. With @code{patch}, you can distribute just the changes to a |
---|
2307 | set of files instead of distributing the entire file set; your |
---|
2308 | correspondents can apply @code{patch} to update their copy of the files |
---|
2309 | with your changes. @code{patch} automatically determines the diff |
---|
2310 | format, skips any leading or trailing headers, and uses the headers to |
---|
2311 | determine which file to patch. This lets your correspondents feed an |
---|
2312 | article or message containing a difference listing directly to |
---|
2313 | @code{patch}. |
---|
2314 | |
---|
2315 | @code{patch} detects and warns about common problems like forward |
---|
2316 | patches. It saves the original version of the files it patches, and |
---|
2317 | saves any patches that it could not apply. It can also maintain a |
---|
2318 | @code{patchlevel.h} file to ensures that your correspondents apply |
---|
2319 | diffs in the proper order. |
---|
2320 | |
---|
2321 | @code{patch} accepts a series of diffs in its standard input, usually |
---|
2322 | separated by headers that specify which file to patch. It applies |
---|
2323 | @code{diff} hunks (@pxref{Hunks}) one by one. If a hunk does not |
---|
2324 | exactly match the original file, @code{patch} uses heuristics to try to |
---|
2325 | patch the file as well as it can. If no approximate match can be found, |
---|
2326 | @code{patch} rejects the hunk and skips to the next hunk. @code{patch} |
---|
2327 | normally replaces each file @var{f} with its new version, saving the |
---|
2328 | original file in @samp{@var{f}.orig}, and putting reject hunks (if any) |
---|
2329 | into @samp{@var{f}.rej}. |
---|
2330 | |
---|
2331 | @xref{Invoking patch}, for detailed information on the options to |
---|
2332 | @code{patch}. @xref{Backups}, for more information on how |
---|
2333 | @code{patch} names backup files. @xref{Rejects}, for more information |
---|
2334 | on where @code{patch} puts reject hunks. |
---|
2335 | |
---|
2336 | @menu |
---|
2337 | * patch Input:: Selecting the type of @code{patch} input. |
---|
2338 | * Imperfect:: Dealing with imperfect patches. |
---|
2339 | * Empty Files:: Removing empty files after patching. |
---|
2340 | * Multiple Patches:: Handling multiple patches in a file specially. |
---|
2341 | * patch Messages:: Messages and questions @code{patch} can produce. |
---|
2342 | @end menu |
---|
2343 | |
---|
2344 | @node patch Input, Imperfect, , Merging with patch |
---|
2345 | @section Selecting the @code{patch} Input Format |
---|
2346 | @cindex @code{patch} input format |
---|
2347 | |
---|
2348 | @code{patch} normally determines which @code{diff} format the patch |
---|
2349 | file uses by examining its contents. For patch files that contain |
---|
2350 | particularly confusing leading text, you might need to use one of the |
---|
2351 | following options to force @code{patch} to interpret the patch file as a |
---|
2352 | certain format of diff. The output formats listed here are the only |
---|
2353 | ones that @code{patch} can understand. |
---|
2354 | |
---|
2355 | @table @samp |
---|
2356 | @item -c |
---|
2357 | @itemx --context |
---|
2358 | context diff. |
---|
2359 | |
---|
2360 | @item -e |
---|
2361 | @itemx --ed |
---|
2362 | @code{ed} script. |
---|
2363 | |
---|
2364 | @item -n |
---|
2365 | @itemx --normal |
---|
2366 | normal diff. |
---|
2367 | |
---|
2368 | @item -u |
---|
2369 | @itemx --unified |
---|
2370 | unified diff. |
---|
2371 | @end table |
---|
2372 | |
---|
2373 | @node Imperfect, Empty Files, patch Input, Merging with patch |
---|
2374 | @section Applying Imperfect Patches |
---|
2375 | @cindex imperfect patch application |
---|
2376 | |
---|
2377 | @code{patch} tries to skip any leading text in the patch file, apply the |
---|
2378 | diff, and then skip any trailing text. Thus you can feed a news article |
---|
2379 | or mail message directly to @code{patch}, and it should work. If the |
---|
2380 | entire diff is indented by a constant amount of white space, @code{patch} |
---|
2381 | automatically ignores the indentation. |
---|
2382 | |
---|
2383 | However, certain other types of imperfect input require user |
---|
2384 | intervention. |
---|
2385 | |
---|
2386 | @menu |
---|
2387 | * Changed White Space:: When tabs and spaces don't match exactly. |
---|
2388 | * Reversed Patches:: Applying reversed patches correctly. |
---|
2389 | * Inexact:: Helping @code{patch} find close matches. |
---|
2390 | @end menu |
---|
2391 | |
---|
2392 | @node Changed White Space, Reversed Patches, , Imperfect |
---|
2393 | @subsection Applying Patches with Changed White Space |
---|
2394 | @cindex white space in patches |
---|
2395 | |
---|
2396 | Sometimes mailers, editors, or other programs change spaces into tabs, |
---|
2397 | or vice versa. If this happens to a patch file or an input file, the |
---|
2398 | files might look the same, but @code{patch} will not be able to match |
---|
2399 | them properly. If this problem occurs, use the @samp{-l} or |
---|
2400 | @samp{--ignore-white-space} option, which makes @code{patch} compare |
---|
2401 | white space loosely so that any sequence of white space in the patch file |
---|
2402 | matches any sequence of white space in the input files. Non-white-space |
---|
2403 | characters must still match exactly. Each line of the context must |
---|
2404 | still match a line in the input file. |
---|
2405 | |
---|
2406 | @node Reversed Patches, Inexact, Changed White Space, Imperfect |
---|
2407 | @subsection Applying Reversed Patches |
---|
2408 | @cindex reversed patches |
---|
2409 | |
---|
2410 | Sometimes people run @code{diff} with the new file first instead of |
---|
2411 | second. This creates a diff that is ``reversed''. To apply such |
---|
2412 | patches, give @code{patch} the @samp{-R} or @samp{--reverse} option. |
---|
2413 | @code{patch} then attempts to swap each hunk around before applying it. |
---|
2414 | Rejects come out in the swapped format. The @samp{-R} option does not |
---|
2415 | work with @code{ed} scripts because there is too little information in |
---|
2416 | them to reconstruct the reverse operation. |
---|
2417 | |
---|
2418 | Often @code{patch} can guess that the patch is reversed. If the first |
---|
2419 | hunk of a patch fails, @code{patch} reverses the hunk to see if it can |
---|
2420 | apply it that way. If it can, @code{patch} asks you if you want to have |
---|
2421 | the @samp{-R} option set; if it can't, @code{patch} continues to apply |
---|
2422 | the patch normally. This method cannot detect a reversed patch if it is |
---|
2423 | a normal diff and the first command is an append (which should have been |
---|
2424 | a delete) since appends always succeed, because a null context matches |
---|
2425 | anywhere. But most patches add or change lines rather than delete them, |
---|
2426 | so most reversed normal diffs begin with a delete, which fails, and |
---|
2427 | @code{patch} notices. |
---|
2428 | |
---|
2429 | If you apply a patch that you have already applied, @code{patch} thinks |
---|
2430 | it is a reversed patch and offers to un-apply the patch. This could be |
---|
2431 | construed as a feature. If you did this inadvertently and you don't |
---|
2432 | want to un-apply the patch, just answer @samp{n} to this offer and to |
---|
2433 | the subsequent ``apply anyway'' question---or type @kbd{C-c} to kill the |
---|
2434 | @code{patch} process. |
---|
2435 | |
---|
2436 | @node Inexact, , Reversed Patches, Imperfect |
---|
2437 | @subsection Helping @code{patch} Find Inexact Matches |
---|
2438 | @cindex inexact patches |
---|
2439 | @cindex fuzz factor when patching |
---|
2440 | |
---|
2441 | For context diffs, and to a lesser extent normal diffs, @code{patch} can |
---|
2442 | detect when the line numbers mentioned in the patch are incorrect, and |
---|
2443 | it attempts to find the correct place to apply each hunk of the patch. |
---|
2444 | As a first guess, it takes the line number mentioned in the hunk, plus |
---|
2445 | or minus any offset used in applying the previous hunk. If that is not |
---|
2446 | the correct place, @code{patch} scans both forward and backward for a |
---|
2447 | set of lines matching the context given in the hunk. |
---|
2448 | |
---|
2449 | First @code{patch} looks for a place where all lines of the context |
---|
2450 | match. If it cannot find such a place, and it is reading a context or |
---|
2451 | unified diff, and the maximum fuzz factor is set to 1 or more, then |
---|
2452 | @code{patch} makes another scan, ignoring the first and last line of |
---|
2453 | context. If that fails, and the maximum fuzz factor is set to 2 or |
---|
2454 | more, it makes another scan, ignoring the first two and last two lines |
---|
2455 | of context are ignored. It continues similarly if the maximum fuzz |
---|
2456 | factor is larger. |
---|
2457 | |
---|
2458 | The @samp{-F @var{lines}} or @samp{--fuzz=@var{lines}} option sets the |
---|
2459 | maximum fuzz factor to @var{lines}. This option only applies to context |
---|
2460 | and unified diffs; it ignores up to @var{lines} lines while looking for |
---|
2461 | the place to install a hunk. Note that a larger fuzz factor increases |
---|
2462 | the odds of making a faulty patch. The default fuzz factor is 2; it may |
---|
2463 | not be set to more than the number of lines of context in the diff, |
---|
2464 | ordinarily 3. |
---|
2465 | |
---|
2466 | If @code{patch} cannot find a place to install a hunk of the patch, it |
---|
2467 | writes the hunk out to a reject file (@pxref{Rejects}, for information |
---|
2468 | on how reject files are named). It writes out rejected hunks in context |
---|
2469 | format no matter what form the input patch is in. If the input is a |
---|
2470 | normal or @code{ed} diff, many of the contexts are simply null. The |
---|
2471 | line numbers on the hunks in the reject file may be different from those |
---|
2472 | in the patch file: they show the approximate location where @code{patch} |
---|
2473 | thinks the failed hunks belong in the new file rather than in the old |
---|
2474 | one. |
---|
2475 | |
---|
2476 | As it completes each hunk, @code{patch} tells you whether the hunk |
---|
2477 | succeeded or failed, and if it failed, on which line (in the new file) |
---|
2478 | @code{patch} thinks the hunk should go. If this is different from the |
---|
2479 | line number specified in the diff, it tells you the offset. A single |
---|
2480 | large offset @emph{may} indicate that @code{patch} installed a hunk in |
---|
2481 | the wrong place. @code{patch} also tells you if it used a fuzz factor |
---|
2482 | to make the match, in which case you should also be slightly suspicious. |
---|
2483 | |
---|
2484 | @code{patch} cannot tell if the line numbers are off in an @code{ed} |
---|
2485 | script, and can only detect wrong line numbers in a normal diff when it |
---|
2486 | finds a change or delete command. It may have the same problem with a |
---|
2487 | context diff using a fuzz factor equal to or greater than the number of |
---|
2488 | lines of context shown in the diff (typically 3). In these cases, you |
---|
2489 | should probably look at a context diff between your original and patched |
---|
2490 | input files to see if the changes make sense. Compiling without errors |
---|
2491 | is a pretty good indication that the patch worked, but not a guarantee. |
---|
2492 | |
---|
2493 | @code{patch} usually produces the correct results, even when it must |
---|
2494 | make many guesses. However, the results are guaranteed only when |
---|
2495 | the patch is applied to an exact copy of the file that the patch was |
---|
2496 | generated from. |
---|
2497 | |
---|
2498 | @node Empty Files, Multiple Patches, Imperfect, Merging with patch |
---|
2499 | @section Removing Empty Files |
---|
2500 | @cindex empty files, removing |
---|
2501 | @cindex removing empty files |
---|
2502 | |
---|
2503 | Sometimes when comparing two directories, the first directory contains a |
---|
2504 | file that the second directory does not. If you give @code{diff} the |
---|
2505 | @samp{-N} or @samp{--new-file} option, it outputs a diff that deletes |
---|
2506 | the contents of this file. By default, @code{patch} leaves an empty |
---|
2507 | file after applying such a diff. The @samp{-E} or |
---|
2508 | @samp{--remove-empty-files} option to @code{patch} deletes output files |
---|
2509 | that are empty after applying the diff. |
---|
2510 | |
---|
2511 | @node Multiple Patches, patch Messages, Empty Files, Merging with patch |
---|
2512 | @section Multiple Patches in a File |
---|
2513 | @cindex multiple patches |
---|
2514 | |
---|
2515 | If the patch file contains more than one patch, @code{patch} tries to |
---|
2516 | apply each of them as if they came from separate patch files. This |
---|
2517 | means that it determines the name of the file to patch for each patch, |
---|
2518 | and that it examines the leading text before each patch for file names |
---|
2519 | and prerequisite revision level (@pxref{Making Patches}, for more on |
---|
2520 | that topic). |
---|
2521 | |
---|
2522 | For the second and subsequent patches in the patch file, you can give |
---|
2523 | options and another original file name by separating their argument |
---|
2524 | lists with a @samp{+}. However, the argument list for a second or |
---|
2525 | subsequent patch may not specify a new patch file, since that does not |
---|
2526 | make sense. |
---|
2527 | |
---|
2528 | For example, to tell @code{patch} to strip the first three slashes from |
---|
2529 | the name of the first patch in the patch file and none from subsequent |
---|
2530 | patches, and to use @file{code.c} as the first input file, you can use: |
---|
2531 | |
---|
2532 | @example |
---|
2533 | patch -p3 code.c + -p0 < patchfile |
---|
2534 | @end example |
---|
2535 | |
---|
2536 | The @samp{-S} or @samp{--skip} option ignores the current patch from the |
---|
2537 | patch file, but continue looking for the next patch in the file. Thus, |
---|
2538 | to ignore the first and third patches in the patch file, you can use: |
---|
2539 | |
---|
2540 | @example |
---|
2541 | patch -S + + -S + < patch file |
---|
2542 | @end example |
---|
2543 | |
---|
2544 | @node patch Messages, , Multiple Patches, Merging with patch |
---|
2545 | @section Messages and Questions from @code{patch} |
---|
2546 | @cindex @code{patch} messages and questions |
---|
2547 | @cindex diagnostics from @code{patch} |
---|
2548 | @cindex messages from @code{patch} |
---|
2549 | |
---|
2550 | @code{patch} can produce a variety of messages, especially if it has |
---|
2551 | trouble decoding its input. In a few situations where it's not sure how |
---|
2552 | to proceed, @code{patch} normally prompts you for more information from |
---|
2553 | the keyboard. There are options to suppress printing non-fatal messages |
---|
2554 | and stopping for keyboard input. |
---|
2555 | |
---|
2556 | The message @samp{Hmm...} indicates that @code{patch} is reading text in |
---|
2557 | the patch file, attempting to determine whether there is a patch in that |
---|
2558 | text, and if so, what kind of patch it is. |
---|
2559 | |
---|
2560 | You can inhibit all terminal output from @code{patch}, unless an error |
---|
2561 | occurs, by using the @samp{-s}, @samp{--quiet}, or @samp{--silent} |
---|
2562 | option. |
---|
2563 | |
---|
2564 | There are two ways you can prevent @code{patch} from asking you any |
---|
2565 | questions. The @samp{-f} or @samp{--force} option assumes that you know |
---|
2566 | what you are doing. It assumes the following: |
---|
2567 | |
---|
2568 | @itemize @bullet |
---|
2569 | @item |
---|
2570 | skip patches that do not contain file names in their headers; |
---|
2571 | |
---|
2572 | @item |
---|
2573 | patch files even though they have the wrong version for the |
---|
2574 | @samp{Prereq:} line in the patch; |
---|
2575 | |
---|
2576 | @item |
---|
2577 | assume that patches are not reversed even if they look like they are. |
---|
2578 | @end itemize |
---|
2579 | |
---|
2580 | The @samp{-t} or @samp{--batch} option is similar to @samp{-f}, in that |
---|
2581 | it suppresses questions, but it makes somewhat different assumptions: |
---|
2582 | |
---|
2583 | @itemize @bullet |
---|
2584 | @item |
---|
2585 | skip patches that do not contain file names in their headers |
---|
2586 | (the same as @samp{-f}); |
---|
2587 | |
---|
2588 | @item |
---|
2589 | skip patches for which the file has the wrong version for the |
---|
2590 | @samp{Prereq:} line in the patch; |
---|
2591 | |
---|
2592 | @item |
---|
2593 | assume that patches are reversed if they look like they are. |
---|
2594 | @end itemize |
---|
2595 | |
---|
2596 | @code{patch} exits with a non-zero status if it creates any reject |
---|
2597 | files. When applying a set of patches in a loop, you should check the |
---|
2598 | exit status, so you don't apply a later patch to a partially patched |
---|
2599 | file. |
---|
2600 | |
---|
2601 | @node Making Patches, Invoking cmp, Merging with patch, Top |
---|
2602 | @chapter Tips for Making Patch Distributions |
---|
2603 | @cindex patch making tips |
---|
2604 | @cindex tips for patch making |
---|
2605 | |
---|
2606 | Here are some things you should keep in mind if you are going to |
---|
2607 | distribute patches for updating a software package. |
---|
2608 | |
---|
2609 | Make sure you have specified the file names correctly, either in a |
---|
2610 | context diff header or with an @samp{Index:} line. If you are patching |
---|
2611 | files in a subdirectory, be sure to tell the patch user to specify a |
---|
2612 | @samp{-p} or @samp{--strip} option as needed. Take care to not send out |
---|
2613 | reversed patches, since these make people wonder whether they have |
---|
2614 | already applied the patch. |
---|
2615 | |
---|
2616 | To save people from partially applying a patch before other patches that |
---|
2617 | should have gone before it, you can make the first patch in the patch |
---|
2618 | file update a file with a name like @file{patchlevel.h} or |
---|
2619 | @file{version.c}, which contains a patch level or version number. If |
---|
2620 | the input file contains the wrong version number, @code{patch} will |
---|
2621 | complain immediately. |
---|
2622 | |
---|
2623 | An even clearer way to prevent this problem is to put a @samp{Prereq:} |
---|
2624 | line before the patch. If the leading text in the patch file contains a |
---|
2625 | line that starts with @samp{Prereq:}, @code{patch} takes the next word |
---|
2626 | from that line (normally a version number) and checks whether the next |
---|
2627 | input file contains that word, preceded and followed by either |
---|
2628 | white space or a newline. If not, @code{patch} prompts you for |
---|
2629 | confirmation before proceeding. This makes it difficult to accidentally |
---|
2630 | apply patches in the wrong order. |
---|
2631 | |
---|
2632 | Since @code{patch} does not handle incomplete lines properly, make sure |
---|
2633 | that all the source files in your program end with a newline whenever |
---|
2634 | you release a version. |
---|
2635 | |
---|
2636 | To create a patch that changes an older version of a package into a |
---|
2637 | newer version, first make a copy of the older version in a scratch |
---|
2638 | directory. Typically you do that by unpacking a @code{tar} or |
---|
2639 | @code{shar} archive of the older version. |
---|
2640 | |
---|
2641 | You might be able to reduce the size of the patch by renaming or |
---|
2642 | removing some files before making the patch. If the older version of |
---|
2643 | the package contains any files that the newer version does not, or if |
---|
2644 | any files have been renamed between the two versions, make a list of |
---|
2645 | @code{rm} and @code{mv} commands for the user to execute in the old |
---|
2646 | version directory before applying the patch. Then run those commands |
---|
2647 | yourself in the scratch directory. |
---|
2648 | |
---|
2649 | If there are any files that you don't need to include in the patch |
---|
2650 | because they can easily be rebuilt from other files (for example, |
---|
2651 | @file{TAGS} and output from @code{yacc} and @code{makeinfo}), replace |
---|
2652 | the versions in the scratch directory with the newer versions, using |
---|
2653 | @code{rm} and @code{ln} or @code{cp}. |
---|
2654 | |
---|
2655 | Now you can create the patch. The de-facto standard @code{diff} format |
---|
2656 | for patch distributions is context format with two lines of context, |
---|
2657 | produced by giving @code{diff} the @samp{-C 2} option. Do not use less |
---|
2658 | than two lines of context, because @code{patch} typically needs at |
---|
2659 | least two lines for proper operation. Give @code{diff} the @samp{-P} |
---|
2660 | option in case the newer version of the package contains any files that |
---|
2661 | the older one does not. Make sure to specify the scratch directory |
---|
2662 | first and the newer directory second. |
---|
2663 | |
---|
2664 | Add to the top of the patch a note telling the user any @code{rm} and |
---|
2665 | @code{mv} commands to run before applying the patch. Then you can |
---|
2666 | remove the scratch directory. |
---|
2667 | |
---|
2668 | @node Invoking cmp, Invoking diff, Making Patches, Top |
---|
2669 | @chapter Invoking @code{cmp} |
---|
2670 | @cindex invoking @code{cmp} |
---|
2671 | @cindex @code{cmp} invocation |
---|
2672 | |
---|
2673 | The @code{cmp} command compares two files, and if they differ, tells the |
---|
2674 | first byte and line number where they differ. Its arguments are as |
---|
2675 | follows: |
---|
2676 | |
---|
2677 | @example |
---|
2678 | cmp @var{options}@dots{} @var{from-file} @r{[}@var{to-file}@var{]} |
---|
2679 | @end example |
---|
2680 | |
---|
2681 | The file name @samp{-} is always the standard input. @code{cmp} also |
---|
2682 | uses the standard input if one file name is omitted. |
---|
2683 | |
---|
2684 | An exit status of 0 means no differences were found, 1 means some |
---|
2685 | differences were found, and 2 means trouble. |
---|
2686 | |
---|
2687 | @menu |
---|
2688 | * cmp Options:: Summary of options to @code{cmp}. |
---|
2689 | @end menu |
---|
2690 | |
---|
2691 | @node cmp Options, , , Invoking cmp |
---|
2692 | @section Options to @code{cmp} |
---|
2693 | @cindex @code{cmp} options |
---|
2694 | @cindex options for @code{cmp} |
---|
2695 | |
---|
2696 | Below is a summary of all of the options that GNU @code{cmp} accepts. |
---|
2697 | Most options have two equivalent names, one of which is a single letter |
---|
2698 | preceded by @samp{-}, and the other of which is a long name preceded by |
---|
2699 | @samp{--}. Multiple single letter options (unless they take an |
---|
2700 | argument) can be combined into a single command line word: @samp{-cl} is |
---|
2701 | equivalent to @samp{-c -l}. |
---|
2702 | |
---|
2703 | @table @samp |
---|
2704 | @item -c |
---|
2705 | Print the differing characters. Display control characters as a |
---|
2706 | @samp{^} followed by a letter of the alphabet and precede characters |
---|
2707 | that have the high bit set with @samp{M-} (which stands for ``meta''). |
---|
2708 | |
---|
2709 | @item --ignore-initial=@var{bytes} |
---|
2710 | Ignore any differences in the the first @var{bytes} bytes of the input files. |
---|
2711 | Treat files with fewer than @var{bytes} bytes as if they are empty. |
---|
2712 | |
---|
2713 | @item -l |
---|
2714 | Print the (decimal) offsets and (octal) values of all differing bytes. |
---|
2715 | |
---|
2716 | @item --print-chars |
---|
2717 | Print the differing characters. Display control characters as a |
---|
2718 | @samp{^} followed by a letter of the alphabet and precede characters |
---|
2719 | that have the high bit set with @samp{M-} (which stands for ``meta''). |
---|
2720 | |
---|
2721 | @item --quiet |
---|
2722 | @itemx -s |
---|
2723 | @itemx --silent |
---|
2724 | Do not print anything; only return an exit status indicating whether |
---|
2725 | the files differ. |
---|
2726 | |
---|
2727 | @item --verbose |
---|
2728 | Print the (decimal) offsets and (octal) values of all differing bytes. |
---|
2729 | |
---|
2730 | @item -v |
---|
2731 | @item --version |
---|
2732 | Output the version number of @code{cmp}. |
---|
2733 | @end table |
---|
2734 | |
---|
2735 | @node Invoking diff, Invoking diff3, Invoking cmp, Top |
---|
2736 | @chapter Invoking @code{diff} |
---|
2737 | @cindex invoking @code{diff} |
---|
2738 | @cindex @code{diff} invocation |
---|
2739 | |
---|
2740 | The format for running the @code{diff} command is: |
---|
2741 | |
---|
2742 | @example |
---|
2743 | diff @var{options}@dots{} @var{from-file} @var{to-file} |
---|
2744 | @end example |
---|
2745 | |
---|
2746 | In the simplest case, @code{diff} compares the contents of the two files |
---|
2747 | @var{from-file} and @var{to-file}. A file name of @samp{-} stands for |
---|
2748 | text read from the standard input. As a special case, @samp{diff - -} |
---|
2749 | compares a copy of standard input to itself. |
---|
2750 | |
---|
2751 | If @var{from-file} is a directory and @var{to-file} is not, @code{diff} |
---|
2752 | compares the file in @var{from-file} whose file name is that of @var{to-file}, |
---|
2753 | and vice versa. The non-directory file must not be @samp{-}. |
---|
2754 | |
---|
2755 | If both @var{from-file} and @var{to-file} are directories, |
---|
2756 | @code{diff} compares corresponding files in both directories, in |
---|
2757 | alphabetical order; this comparison is not recursive unless the |
---|
2758 | @samp{-r} or @samp{--recursive} option is given. @code{diff} never |
---|
2759 | compares the actual contents of a directory as if it were a file. The |
---|
2760 | file that is fully specified may not be standard input, because standard |
---|
2761 | input is nameless and the notion of ``file with the same name'' does not |
---|
2762 | apply. |
---|
2763 | |
---|
2764 | @code{diff} options begin with @samp{-}, so normally @var{from-file} and |
---|
2765 | @var{to-file} may not begin with @samp{-}. However, @samp{--} as an |
---|
2766 | argument by itself treats the remaining arguments as file names even if |
---|
2767 | they begin with @samp{-}. |
---|
2768 | |
---|
2769 | An exit status of 0 means no differences were found, 1 means some |
---|
2770 | differences were found, and 2 means trouble. |
---|
2771 | |
---|
2772 | @menu |
---|
2773 | * diff Options:: Summary of options to @code{diff}. |
---|
2774 | @end menu |
---|
2775 | |
---|
2776 | @node diff Options, , , Invoking diff |
---|
2777 | @section Options to @code{diff} |
---|
2778 | @cindex @code{diff} options |
---|
2779 | @cindex options for @code{diff} |
---|
2780 | |
---|
2781 | Below is a summary of all of the options that GNU @code{diff} accepts. |
---|
2782 | Most options have two equivalent names, one of which is a single letter |
---|
2783 | preceded by @samp{-}, and the other of which is a long name preceded by |
---|
2784 | @samp{--}. Multiple single letter options (unless they take an |
---|
2785 | argument) can be combined into a single command line word: @samp{-ac} is |
---|
2786 | equivalent to @samp{-a -c}. Long named options can be abbreviated to |
---|
2787 | any unique prefix of their name. Brackets ([ and ]) indicate that an |
---|
2788 | option takes an optional argument. |
---|
2789 | |
---|
2790 | @table @samp |
---|
2791 | @item -@var{lines} |
---|
2792 | Show @var{lines} (an integer) lines of context. This option does not |
---|
2793 | specify an output format by itself; it has no effect unless it is |
---|
2794 | combined with @samp{-c} (@pxref{Context Format}) or @samp{-u} |
---|
2795 | (@pxref{Unified Format}). This option is obsolete. For proper |
---|
2796 | operation, @code{patch} typically needs at least two lines of context. |
---|
2797 | |
---|
2798 | @item -a |
---|
2799 | Treat all files as text and compare them line-by-line, even if they |
---|
2800 | do not seem to be text. @xref{Binary}. |
---|
2801 | |
---|
2802 | @item -b |
---|
2803 | Ignore changes in amount of white space. @xref{White Space}. |
---|
2804 | |
---|
2805 | @item -B |
---|
2806 | Ignore changes that just insert or delete blank lines. @xref{Blank |
---|
2807 | Lines}. |
---|
2808 | |
---|
2809 | @item --binary |
---|
2810 | Read and write data in binary mode. @xref{Binary}. |
---|
2811 | |
---|
2812 | @item --brief |
---|
2813 | Report only whether the files differ, not the details of the |
---|
2814 | differences. @xref{Brief}. |
---|
2815 | |
---|
2816 | @item -c |
---|
2817 | Use the context output format. @xref{Context Format}. |
---|
2818 | |
---|
2819 | @item -C @var{lines} |
---|
2820 | @itemx --context@r{[}=@var{lines}@r{]} |
---|
2821 | Use the context output format, showing @var{lines} (an integer) lines of |
---|
2822 | context, or three if @var{lines} is not given. @xref{Context Format}. |
---|
2823 | For proper operation, @code{patch} typically needs at least two lines of |
---|
2824 | context. |
---|
2825 | |
---|
2826 | @item --changed-group-format=@var{format} |
---|
2827 | Use @var{format} to output a line group containing differing lines from |
---|
2828 | both files in if-then-else format. @xref{Line Group Formats}. |
---|
2829 | |
---|
2830 | @item -d |
---|
2831 | Change the algorithm perhaps find a smaller set of changes. This makes |
---|
2832 | @code{diff} slower (sometimes much slower). @xref{diff Performance}. |
---|
2833 | |
---|
2834 | @item -D @var{name} |
---|
2835 | Make merged @samp{#ifdef} format output, conditional on the preprocessor |
---|
2836 | macro @var{name}. @xref{If-then-else}. |
---|
2837 | |
---|
2838 | @item -e |
---|
2839 | @itemx --ed |
---|
2840 | Make output that is a valid @code{ed} script. @xref{ed Scripts}. |
---|
2841 | |
---|
2842 | @item --exclude=@var{pattern} |
---|
2843 | When comparing directories, ignore files and subdirectories whose basenames |
---|
2844 | match @var{pattern}. @xref{Comparing Directories}. |
---|
2845 | |
---|
2846 | @item --exclude-from=@var{file} |
---|
2847 | When comparing directories, ignore files and subdirectories whose basenames |
---|
2848 | match any pattern contained in @var{file}. @xref{Comparing Directories}. |
---|
2849 | |
---|
2850 | @item --expand-tabs |
---|
2851 | Expand tabs to spaces in the output, to preserve the alignment of tabs |
---|
2852 | in the input files. @xref{Tabs}. |
---|
2853 | |
---|
2854 | @item -f |
---|
2855 | Make output that looks vaguely like an @code{ed} script but has changes |
---|
2856 | in the order they appear in the file. @xref{Forward ed}. |
---|
2857 | |
---|
2858 | @item -F @var{regexp} |
---|
2859 | In context and unified format, for each hunk of differences, show some |
---|
2860 | of the last preceding line that matches @var{regexp}. @xref{Specified |
---|
2861 | Headings}. |
---|
2862 | |
---|
2863 | @item --forward-ed |
---|
2864 | Make output that looks vaguely like an @code{ed} script but has changes |
---|
2865 | in the order they appear in the file. @xref{Forward ed}. |
---|
2866 | |
---|
2867 | @item -h |
---|
2868 | This option currently has no effect; it is present for Unix |
---|
2869 | compatibility. |
---|
2870 | |
---|
2871 | @item -H |
---|
2872 | Use heuristics to speed handling of large files that have numerous |
---|
2873 | scattered small changes. @xref{diff Performance}. |
---|
2874 | |
---|
2875 | @item --horizon-lines=@var{lines} |
---|
2876 | Do not discard the last @var{lines} lines of the common prefix |
---|
2877 | and the first @var{lines} lines of the common suffix. |
---|
2878 | @xref{diff Performance}. |
---|
2879 | |
---|
2880 | @item -i |
---|
2881 | Ignore changes in case; consider upper- and lower-case letters |
---|
2882 | equivalent. @xref{Case Folding}. |
---|
2883 | |
---|
2884 | @item -I @var{regexp} |
---|
2885 | Ignore changes that just insert or delete lines that match @var{regexp}. |
---|
2886 | @xref{Specified Folding}. |
---|
2887 | |
---|
2888 | @item --ifdef=@var{name} |
---|
2889 | Make merged if-then-else output using @var{name}. @xref{If-then-else}. |
---|
2890 | |
---|
2891 | @item --ignore-all-space |
---|
2892 | Ignore white space when comparing lines. @xref{White Space}. |
---|
2893 | |
---|
2894 | @item --ignore-blank-lines |
---|
2895 | Ignore changes that just insert or delete blank lines. @xref{Blank |
---|
2896 | Lines}. |
---|
2897 | |
---|
2898 | @item --ignore-case |
---|
2899 | Ignore changes in case; consider upper- and lower-case to be the same. |
---|
2900 | @xref{Case Folding}. |
---|
2901 | |
---|
2902 | @item --ignore-matching-lines=@var{regexp} |
---|
2903 | Ignore changes that just insert or delete lines that match @var{regexp}. |
---|
2904 | @xref{Specified Folding}. |
---|
2905 | |
---|
2906 | @item --ignore-space-change |
---|
2907 | Ignore changes in amount of white space. |
---|
2908 | @xref{White Space}. |
---|
2909 | |
---|
2910 | @item --initial-tab |
---|
2911 | Output a tab rather than a space before the text of a line in normal or |
---|
2912 | context format. This causes the alignment of tabs in the line to look |
---|
2913 | normal. @xref{Tabs}. |
---|
2914 | |
---|
2915 | @item -l |
---|
2916 | Pass the output through @code{pr} to paginate it. @xref{Pagination}. |
---|
2917 | |
---|
2918 | @item -L @var{label} |
---|
2919 | Use @var{label} instead of the file name in the context format |
---|
2920 | (@pxref{Context Format}) and unified format (@pxref{Unified Format}) |
---|
2921 | headers. @xref{RCS}. |
---|
2922 | |
---|
2923 | @item --label=@var{label} |
---|
2924 | Use @var{label} instead of the file name in the context format |
---|
2925 | (@pxref{Context Format}) and unified format (@pxref{Unified Format}) |
---|
2926 | headers. |
---|
2927 | |
---|
2928 | @item --left-column |
---|
2929 | Print only the left column of two common lines in side by side format. |
---|
2930 | @xref{Side by Side Format}. |
---|
2931 | |
---|
2932 | @item --line-format=@var{format} |
---|
2933 | Use @var{format} to output all input lines in if-then-else format. |
---|
2934 | @xref{Line Formats}. |
---|
2935 | |
---|
2936 | @item --minimal |
---|
2937 | Change the algorithm to perhaps find a smaller set of changes. This |
---|
2938 | makes @code{diff} slower (sometimes much slower). @xref{diff |
---|
2939 | Performance}. |
---|
2940 | |
---|
2941 | @item -n |
---|
2942 | Output RCS-format diffs; like @samp{-f} except that each command |
---|
2943 | specifies the number of lines affected. @xref{RCS}. |
---|
2944 | |
---|
2945 | @item -N |
---|
2946 | @itemx --new-file |
---|
2947 | In directory comparison, if a file is found in only one directory, |
---|
2948 | treat it as present but empty in the other directory. @xref{Comparing |
---|
2949 | Directories}. |
---|
2950 | |
---|
2951 | @item --new-group-format=@var{format} |
---|
2952 | Use @var{format} to output a group of lines taken from just the second |
---|
2953 | file in if-then-else format. @xref{Line Group Formats}. |
---|
2954 | |
---|
2955 | @item --new-line-format=@var{format} |
---|
2956 | Use @var{format} to output a line taken from just the second file in |
---|
2957 | if-then-else format. @xref{Line Formats}. |
---|
2958 | |
---|
2959 | @item --old-group-format=@var{format} |
---|
2960 | Use @var{format} to output a group of lines taken from just the first |
---|
2961 | file in if-then-else format. @xref{Line Group Formats}. |
---|
2962 | |
---|
2963 | @item --old-line-format=@var{format} |
---|
2964 | Use @var{format} to output a line taken from just the first file in |
---|
2965 | if-then-else format. @xref{Line Formats}. |
---|
2966 | |
---|
2967 | @item -p |
---|
2968 | Show which C function each change is in. @xref{C Function Headings}. |
---|
2969 | |
---|
2970 | @item -P |
---|
2971 | When comparing directories, if a file appears only in the second |
---|
2972 | directory of the two, treat it as present but empty in the other. |
---|
2973 | @xref{Comparing Directories}. |
---|
2974 | |
---|
2975 | @item --paginate |
---|
2976 | Pass the output through @code{pr} to paginate it. @xref{Pagination}. |
---|
2977 | |
---|
2978 | @item -q |
---|
2979 | Report only whether the files differ, not the details of the |
---|
2980 | differences. @xref{Brief}. |
---|
2981 | |
---|
2982 | @item -r |
---|
2983 | When comparing directories, recursively compare any subdirectories |
---|
2984 | found. @xref{Comparing Directories}. |
---|
2985 | |
---|
2986 | @item --rcs |
---|
2987 | Output RCS-format diffs; like @samp{-f} except that each command |
---|
2988 | specifies the number of lines affected. @xref{RCS}. |
---|
2989 | |
---|
2990 | @item --recursive |
---|
2991 | When comparing directories, recursively compare any subdirectories |
---|
2992 | found. @xref{Comparing Directories}. |
---|
2993 | |
---|
2994 | @item --report-identical-files |
---|
2995 | Report when two files are the same. @xref{Comparing Directories}. |
---|
2996 | |
---|
2997 | @item -s |
---|
2998 | Report when two files are the same. @xref{Comparing Directories}. |
---|
2999 | |
---|
3000 | @item -S @var{file} |
---|
3001 | When comparing directories, start with the file @var{file}. This is |
---|
3002 | used for resuming an aborted comparison. @xref{Comparing Directories}. |
---|
3003 | |
---|
3004 | @item --sdiff-merge-assist |
---|
3005 | Print extra information to help @code{sdiff}. @code{sdiff} uses this |
---|
3006 | option when it runs @code{diff}. This option is not intended for users |
---|
3007 | to use directly. |
---|
3008 | |
---|
3009 | @item --show-c-function |
---|
3010 | Show which C function each change is in. @xref{C Function Headings}. |
---|
3011 | |
---|
3012 | @item --show-function-line=@var{regexp} |
---|
3013 | In context and unified format, for each hunk of differences, show some |
---|
3014 | of the last preceding line that matches @var{regexp}. @xref{Specified |
---|
3015 | Headings}. |
---|
3016 | |
---|
3017 | @item --side-by-side |
---|
3018 | Use the side by side output format. @xref{Side by Side Format}. |
---|
3019 | |
---|
3020 | @item --speed-large-files |
---|
3021 | Use heuristics to speed handling of large files that have numerous |
---|
3022 | scattered small changes. @xref{diff Performance}. |
---|
3023 | |
---|
3024 | @item --starting-file=@var{file} |
---|
3025 | When comparing directories, start with the file @var{file}. This is |
---|
3026 | used for resuming an aborted comparison. @xref{Comparing Directories}. |
---|
3027 | |
---|
3028 | @item --suppress-common-lines |
---|
3029 | Do not print common lines in side by side format. |
---|
3030 | @xref{Side by Side Format}. |
---|
3031 | |
---|
3032 | @item -t |
---|
3033 | Expand tabs to spaces in the output, to preserve the alignment of tabs |
---|
3034 | in the input files. @xref{Tabs}. |
---|
3035 | |
---|
3036 | @item -T |
---|
3037 | Output a tab rather than a space before the text of a line in normal or |
---|
3038 | context format. This causes the alignment of tabs in the line to look |
---|
3039 | normal. @xref{Tabs}. |
---|
3040 | |
---|
3041 | @item --text |
---|
3042 | Treat all files as text and compare them line-by-line, even if they |
---|
3043 | do not appear to be text. @xref{Binary}. |
---|
3044 | |
---|
3045 | @item -u |
---|
3046 | Use the unified output format. @xref{Unified Format}. |
---|
3047 | |
---|
3048 | @item --unchanged-group-format=@var{format} |
---|
3049 | Use @var{format} to output a group of common lines taken from both files |
---|
3050 | in if-then-else format. @xref{Line Group Formats}. |
---|
3051 | |
---|
3052 | @item --unchanged-line-format=@var{format} |
---|
3053 | Use @var{format} to output a line common to both files in if-then-else |
---|
3054 | format. @xref{Line Formats}. |
---|
3055 | |
---|
3056 | @item --unidirectional-new-file |
---|
3057 | When comparing directories, if a file appears only in the second |
---|
3058 | directory of the two, treat it as present but empty in the other. |
---|
3059 | @xref{Comparing Directories}. |
---|
3060 | |
---|
3061 | @item -U @var{lines} |
---|
3062 | @itemx --unified@r{[}=@var{lines}@r{]} |
---|
3063 | Use the unified output format, showing @var{lines} (an integer) lines of |
---|
3064 | context, or three if @var{lines} is not given. @xref{Unified Format}. |
---|
3065 | For proper operation, @code{patch} typically needs at least two lines of |
---|
3066 | context. |
---|
3067 | |
---|
3068 | @item -v |
---|
3069 | @itemx --version |
---|
3070 | Output the version number of @code{diff}. |
---|
3071 | |
---|
3072 | @item -w |
---|
3073 | Ignore white space when comparing lines. @xref{White Space}. |
---|
3074 | |
---|
3075 | @item -W @var{columns} |
---|
3076 | @itemx --width=@var{columns} |
---|
3077 | Use an output width of @var{columns} in side by side format. |
---|
3078 | @xref{Side by Side Format}. |
---|
3079 | |
---|
3080 | @item -x @var{pattern} |
---|
3081 | When comparing directories, ignore files and subdirectories whose basenames |
---|
3082 | match @var{pattern}. @xref{Comparing Directories}. |
---|
3083 | |
---|
3084 | @item -X @var{file} |
---|
3085 | When comparing directories, ignore files and subdirectories whose basenames |
---|
3086 | match any pattern contained in @var{file}. @xref{Comparing Directories}. |
---|
3087 | |
---|
3088 | @item -y |
---|
3089 | Use the side by side output format. @xref{Side by Side Format}. |
---|
3090 | @end table |
---|
3091 | |
---|
3092 | @node Invoking diff3, Invoking patch, Invoking diff, Top |
---|
3093 | @chapter Invoking @code{diff3} |
---|
3094 | @cindex invoking @code{diff3} |
---|
3095 | @cindex @code{diff3} invocation |
---|
3096 | |
---|
3097 | The @code{diff3} command compares three files and outputs descriptions |
---|
3098 | of their differences. Its arguments are as follows: |
---|
3099 | |
---|
3100 | @example |
---|
3101 | diff3 @var{options}@dots{} @var{mine} @var{older} @var{yours} |
---|
3102 | @end example |
---|
3103 | |
---|
3104 | The files to compare are @var{mine}, @var{older}, and @var{yours}. |
---|
3105 | At most one of these three file names may be @samp{-}, |
---|
3106 | which tells @code{diff3} to read the standard input for that file. |
---|
3107 | |
---|
3108 | An exit status of 0 means @code{diff3} was successful, 1 means some |
---|
3109 | conflicts were found, and 2 means trouble. |
---|
3110 | |
---|
3111 | @menu |
---|
3112 | * diff3 Options:: Summary of options to @code{diff3}. |
---|
3113 | @end menu |
---|
3114 | |
---|
3115 | @node diff3 Options, , , Invoking diff3 |
---|
3116 | @section Options to @code{diff3} |
---|
3117 | @cindex @code{diff3} options |
---|
3118 | @cindex options for @code{diff3} |
---|
3119 | |
---|
3120 | Below is a summary of all of the options that GNU @code{diff3} |
---|
3121 | accepts. Multiple single letter options (unless they take an argument) |
---|
3122 | can be combined into a single command line argument. |
---|
3123 | |
---|
3124 | @table @samp |
---|
3125 | @item -a |
---|
3126 | Treat all files as text and compare them line-by-line, even if they |
---|
3127 | do not appear to be text. @xref{Binary}. |
---|
3128 | |
---|
3129 | @item -A |
---|
3130 | Incorporate all changes from @var{older} to @var{yours} into @var{mine}, |
---|
3131 | surrounding all conflicts with bracket lines. |
---|
3132 | @xref{Marking Conflicts}. |
---|
3133 | |
---|
3134 | @item -e |
---|
3135 | Generate an @code{ed} script that incorporates all the changes from |
---|
3136 | @var{older} to @var{yours} into @var{mine}. @xref{Which Changes}. |
---|
3137 | |
---|
3138 | @item -E |
---|
3139 | Like @samp{-e}, except bracket lines from overlapping changes' first |
---|
3140 | and third files. |
---|
3141 | @xref{Marking Conflicts}. |
---|
3142 | With @samp{-e}, an overlapping change looks like this: |
---|
3143 | |
---|
3144 | @example |
---|
3145 | <<<<<<< @var{mine} |
---|
3146 | @r{lines from @var{mine}} |
---|
3147 | ======= |
---|
3148 | @r{lines from @var{yours}} |
---|
3149 | >>>>>>> @var{yours} |
---|
3150 | @end example |
---|
3151 | |
---|
3152 | @item --ed |
---|
3153 | Generate an @code{ed} script that incorporates all the changes from |
---|
3154 | @var{older} to @var{yours} into @var{mine}. @xref{Which Changes}. |
---|
3155 | |
---|
3156 | @item --easy-only |
---|
3157 | Like @samp{-e}, except output only the nonoverlapping changes. |
---|
3158 | @xref{Which Changes}. |
---|
3159 | |
---|
3160 | @item -i |
---|
3161 | Generate @samp{w} and @samp{q} commands at the end of the @code{ed} |
---|
3162 | script for System V compatibility. This option must be combined with |
---|
3163 | one of the @samp{-AeExX3} options, and may not be combined with @samp{-m}. |
---|
3164 | @xref{Saving the Changed File}. |
---|
3165 | |
---|
3166 | @item --initial-tab |
---|
3167 | Output a tab rather than two spaces before the text of a line in normal format. |
---|
3168 | This causes the alignment of tabs in the line to look normal. @xref{Tabs}. |
---|
3169 | |
---|
3170 | @item -L @var{label} |
---|
3171 | @itemx --label=@var{label} |
---|
3172 | Use the label @var{label} for the brackets output by the @samp{-A}, |
---|
3173 | @samp{-E} and @samp{-X} options. This option may be given up to three |
---|
3174 | times, one for each input file. The default labels are the names of |
---|
3175 | the input files. Thus @samp{diff3 -L X -L Y -L Z -m A B C} acts like |
---|
3176 | @samp{diff3 -m A B C}, except that the output looks like it came from |
---|
3177 | files named @samp{X}, @samp{Y} and @samp{Z} rather than from files |
---|
3178 | named @samp{A}, @samp{B} and @samp{C}. @xref{Marking Conflicts}. |
---|
3179 | |
---|
3180 | @item -m |
---|
3181 | @itemx --merge |
---|
3182 | Apply the edit script to the first file and send the result to standard |
---|
3183 | output. Unlike piping the output from @code{diff3} to @code{ed}, this |
---|
3184 | works even for binary files and incomplete lines. @samp{-A} is assumed |
---|
3185 | if no edit script option is specified. @xref{Bypassing ed}. |
---|
3186 | |
---|
3187 | @item --overlap-only |
---|
3188 | Like @samp{-e}, except output only the overlapping changes. |
---|
3189 | @xref{Which Changes}. |
---|
3190 | |
---|
3191 | @item --show-all |
---|
3192 | Incorporate all unmerged changes from @var{older} to @var{yours} into |
---|
3193 | @var{mine}, surrounding all overlapping changes with bracket lines. |
---|
3194 | @xref{Marking Conflicts}. |
---|
3195 | |
---|
3196 | @item --show-overlap |
---|
3197 | Like @samp{-e}, except bracket lines from overlapping changes' first |
---|
3198 | and third files. |
---|
3199 | @xref{Marking Conflicts}. |
---|
3200 | |
---|
3201 | @item -T |
---|
3202 | Output a tab rather than two spaces before the text of a line in normal format. |
---|
3203 | This causes the alignment of tabs in the line to look normal. @xref{Tabs}. |
---|
3204 | |
---|
3205 | @item --text |
---|
3206 | Treat all files as text and compare them line-by-line, even if they |
---|
3207 | do not appear to be text. @xref{Binary}. |
---|
3208 | |
---|
3209 | @item -v |
---|
3210 | @itemx --version |
---|
3211 | Output the version number of @code{diff3}. |
---|
3212 | |
---|
3213 | @item -x |
---|
3214 | Like @samp{-e}, except output only the overlapping changes. |
---|
3215 | @xref{Which Changes}. |
---|
3216 | |
---|
3217 | @item -X |
---|
3218 | Like @samp{-E}, except output only the overlapping changes. |
---|
3219 | In other words, like @samp{-x}, except bracket changes as in @samp{-E}. |
---|
3220 | @xref{Marking Conflicts}. |
---|
3221 | |
---|
3222 | @item -3 |
---|
3223 | Like @samp{-e}, except output only the nonoverlapping changes. |
---|
3224 | @xref{Which Changes}. |
---|
3225 | @end table |
---|
3226 | |
---|
3227 | @node Invoking patch, Invoking sdiff, Invoking diff3, Top |
---|
3228 | @chapter Invoking @code{patch} |
---|
3229 | @cindex invoking @code{patch} |
---|
3230 | @cindex @code{patch} invocation |
---|
3231 | |
---|
3232 | Normally @code{patch} is invoked like this: |
---|
3233 | |
---|
3234 | @example |
---|
3235 | patch <@var{patchfile} |
---|
3236 | @end example |
---|
3237 | |
---|
3238 | The full format for invoking @code{patch} is: |
---|
3239 | |
---|
3240 | @example |
---|
3241 | patch @var{options}@dots{} @r{[}@var{origfile} @r{[}@var{patchfile}@r{]}@r{]} @r{[}+ @var{options}@dots{} @r{[}@var{origfile}@r{]}@r{]}@dots{} |
---|
3242 | @end example |
---|
3243 | |
---|
3244 | If you do not specify @var{patchfile}, or if @var{patchfile} is |
---|
3245 | @samp{-}, @code{patch} reads the patch (that is, the @code{diff} output) |
---|
3246 | from the standard input. |
---|
3247 | |
---|
3248 | You can specify one or more of the original files as @var{orig} arguments; |
---|
3249 | each one and options for interpreting it is separated from the others with a |
---|
3250 | @samp{+}. @xref{Multiple Patches}, for more information. |
---|
3251 | |
---|
3252 | If you do not specify an input file on the command line, @code{patch} |
---|
3253 | tries to figure out from the @dfn{leading text} (any text in the patch |
---|
3254 | that comes before the @code{diff} output) which file to edit. In the |
---|
3255 | header of a context or unified diff, @code{patch} looks in lines |
---|
3256 | beginning with @samp{***}, @samp{---}, or @samp{+++}; among those, it |
---|
3257 | chooses the shortest name of an existing file. Otherwise, if there is |
---|
3258 | an @samp{Index:} line in the leading text, @code{patch} tries to use the |
---|
3259 | file name from that line. If @code{patch} cannot figure out the name of |
---|
3260 | an existing file from the leading text, it prompts you for the name of |
---|
3261 | the file to patch. |
---|
3262 | |
---|
3263 | If the input file does not exist or is read-only, and a suitable RCS or |
---|
3264 | SCCS file exists, @code{patch} attempts to check out or get the file |
---|
3265 | before proceeding. |
---|
3266 | |
---|
3267 | By default, @code{patch} replaces the original input file with the |
---|
3268 | patched version, after renaming the original file into a backup file |
---|
3269 | (@pxref{Backups}, for a description of how @code{patch} names backup |
---|
3270 | files). You can also specify where to put the output with the @samp{-o |
---|
3271 | @var{output-file}} or @samp{--output=@var{output-file}} option. |
---|
3272 | |
---|
3273 | @menu |
---|
3274 | * patch Directories:: Changing directory and stripping directories. |
---|
3275 | * Backups:: Backup file names. |
---|
3276 | * Rejects:: Reject file names. |
---|
3277 | * patch Options:: Summary table of options to @code{patch}. |
---|
3278 | @end menu |
---|
3279 | |
---|
3280 | @node patch Directories, Backups, , Invoking patch |
---|
3281 | @section Applying Patches in Other Directories |
---|
3282 | @cindex directories and patch |
---|
3283 | @cindex patching directories |
---|
3284 | |
---|
3285 | The @samp{-d @var{directory}} or @samp{--directory=@var{directory}} |
---|
3286 | option to @code{patch} makes directory @var{directory} the current |
---|
3287 | directory for interpreting both file names in the patch file, and file |
---|
3288 | names given as arguments to other options (such as @samp{-B} and |
---|
3289 | @samp{-o}). For example, while in a news reading program, you can patch |
---|
3290 | a file in the @file{/usr/src/emacs} directory directly from the article |
---|
3291 | containing the patch like this: |
---|
3292 | |
---|
3293 | @example |
---|
3294 | | patch -d /usr/src/emacs |
---|
3295 | @end example |
---|
3296 | |
---|
3297 | Sometimes the file names given in a patch contain leading directories, |
---|
3298 | but you keep your files in a directory different from the one given in |
---|
3299 | the patch. In those cases, you can use the |
---|
3300 | @samp{-p@r{[}@var{number}@r{]}} or @samp{--strip@r{[}=@var{number}@r{]}} |
---|
3301 | option to set the file name strip count to @var{number}. The strip |
---|
3302 | count tells @code{patch} how many slashes, along with the directory |
---|
3303 | names between them, to strip from the front of file names. @samp{-p} |
---|
3304 | with no @var{number} given is equivalent to @samp{-p0}. By default, |
---|
3305 | @code{patch} strips off all leading directories, leaving just the base file |
---|
3306 | names, except that when a file name given in the patch is a relative |
---|
3307 | file name and all of its leading directories already exist, @code{patch} does |
---|
3308 | not strip off the leading directory. (A @dfn{relative} file name is one |
---|
3309 | that does not start with a slash.) |
---|
3310 | |
---|
3311 | @code{patch} looks for each file (after any slashes have been stripped) |
---|
3312 | in the current directory, or if you used the @samp{-d @var{directory}} |
---|
3313 | option, in that directory. |
---|
3314 | |
---|
3315 | For example, suppose the file name in the patch file is |
---|
3316 | @file{/gnu/src/emacs/etc/NEWS}. Using @samp{-p} or @samp{-p0} gives the |
---|
3317 | entire file name unmodified, @samp{-p1} gives |
---|
3318 | @file{gnu/src/emacs/etc/NEWS} (no leading slash), @samp{-p4} gives |
---|
3319 | @file{etc/NEWS}, and not specifying @samp{-p} at all gives @file{NEWS}. |
---|
3320 | |
---|
3321 | @node Backups, Rejects, patch Directories, Invoking patch |
---|
3322 | @section Backup File Names |
---|
3323 | @cindex backup file names |
---|
3324 | |
---|
3325 | Normally, @code{patch} renames an original input file into a backup file |
---|
3326 | by appending to its name the extension @samp{.orig}, or @samp{~} on |
---|
3327 | systems that do not support long file names. The @samp{-b |
---|
3328 | @var{backup-suffix}} or @samp{--suffix=@var{backup-suffix}} option uses |
---|
3329 | @var{backup-suffix} as the backup extension instead. |
---|
3330 | |
---|
3331 | Alternately, you can specify the extension for backup files with the |
---|
3332 | @code{SIMPLE_BACKUP_SUFFIX} environment variable, which the options |
---|
3333 | override. |
---|
3334 | |
---|
3335 | @code{patch} can also create numbered backup files the way GNU Emacs |
---|
3336 | does. With this method, instead of having a single backup of each file, |
---|
3337 | @code{patch} makes a new backup file name each time it patches a file. |
---|
3338 | For example, the backups of a file named @file{sink} would be called, |
---|
3339 | successively, @file{sink.~1~}, @file{sink.~2~}, @file{sink.~3~}, etc. |
---|
3340 | |
---|
3341 | The @samp{-V @var{backup-style}} or |
---|
3342 | @samp{--version-control=@var{backup-style}} option takes as an argument |
---|
3343 | a method for creating backup file names. You can alternately control |
---|
3344 | the type of backups that @code{patch} makes with the |
---|
3345 | @code{VERSION_CONTROL} environment variable, which the @samp{-V} option |
---|
3346 | overrides. The value of the @code{VERSION_CONTROL} environment variable |
---|
3347 | and the argument to the @samp{-V} option are like the GNU Emacs |
---|
3348 | @code{version-control} variable (@pxref{Backups, |
---|
3349 | emacs, The GNU Emacs Manual}, for more information on backup versions in |
---|
3350 | Emacs). They also recognize synonyms that are more descriptive. The |
---|
3351 | valid values are listed below; unique abbreviations are acceptable. |
---|
3352 | |
---|
3353 | @table @samp |
---|
3354 | @item t |
---|
3355 | @itemx numbered |
---|
3356 | Always make numbered backups. |
---|
3357 | |
---|
3358 | @item nil |
---|
3359 | @itemx existing |
---|
3360 | Make numbered backups of files that already have them, simple backups of |
---|
3361 | the others. This is the default. |
---|
3362 | |
---|
3363 | @item never |
---|
3364 | @itemx simple |
---|
3365 | Always make simple backups. |
---|
3366 | @end table |
---|
3367 | |
---|
3368 | Alternately, you can tell @code{patch} to prepend a prefix, such as a |
---|
3369 | directory name, to produce backup file names. The @samp{-B |
---|
3370 | @var{backup-prefix}} or @samp{--prefix=@var{backup-prefix}} option makes |
---|
3371 | backup files by prepending @var{backup-prefix} to them. If you use this |
---|
3372 | option, @code{patch} ignores any @samp{-b} option that you give. |
---|
3373 | |
---|
3374 | If the backup file already exists, @code{patch} creates a new backup |
---|
3375 | file name by changing the first lowercase letter in the last component |
---|
3376 | of the file name into uppercase. If there are no more lowercase letters |
---|
3377 | in the name, it removes the first character from the name. It repeats |
---|
3378 | this process until it comes up with a backup file name that does not |
---|
3379 | already exist. |
---|
3380 | |
---|
3381 | If you specify the output file with the @samp{-o} option, that file is |
---|
3382 | the one that is backed up, not the input file. |
---|
3383 | |
---|
3384 | @node Rejects, patch Options, Backups, Invoking patch |
---|
3385 | @section Reject File Names |
---|
3386 | @cindex reject file names |
---|
3387 | |
---|
3388 | The names for reject files (files containing patches that @code{patch} |
---|
3389 | could not find a place to apply) are normally the name of the output |
---|
3390 | file with @samp{.rej} appended (or @samp{#} on systems that do not |
---|
3391 | support long file names). |
---|
3392 | |
---|
3393 | Alternatively, you can tell @code{patch} to place all of the rejected |
---|
3394 | patches in a single file. The @samp{-r @var{reject-file}} or |
---|
3395 | @samp{--reject-file=@var{reject-file}} option uses @var{reject-file} as |
---|
3396 | the reject file name. |
---|
3397 | |
---|
3398 | @node patch Options, , Rejects, Invoking patch |
---|
3399 | @section Options to @code{patch} |
---|
3400 | @cindex @code{patch} options |
---|
3401 | @cindex options for @code{patch} |
---|
3402 | |
---|
3403 | Here is a summary of all of the options that @code{patch} accepts. |
---|
3404 | Older versions of @code{patch} do not accept long-named options or the |
---|
3405 | @samp{-t}, @samp{-E}, or @samp{-V} options. |
---|
3406 | |
---|
3407 | Multiple single-letter options that do not take an argument can be |
---|
3408 | combined into a single command line argument (with only one dash). |
---|
3409 | Brackets ([ and ]) indicate that an option takes an optional argument. |
---|
3410 | |
---|
3411 | @table @samp |
---|
3412 | @item -b @var{backup-suffix} |
---|
3413 | Use @var{backup-suffix} as the backup extension instead of |
---|
3414 | @samp{.orig} or @samp{~}. @xref{Backups}. |
---|
3415 | |
---|
3416 | @item -B @var{backup-prefix} |
---|
3417 | Use @var{backup-prefix} as a prefix to the backup file name. If this |
---|
3418 | option is specified, any @samp{-b} option is ignored. @xref{Backups}. |
---|
3419 | |
---|
3420 | @item --batch |
---|
3421 | Do not ask any questions. @xref{patch Messages}. |
---|
3422 | |
---|
3423 | @item -c |
---|
3424 | @itemx --context |
---|
3425 | Interpret the patch file as a context diff. @xref{patch Input}. |
---|
3426 | |
---|
3427 | @item -d @var{directory} |
---|
3428 | @itemx --directory=@var{directory} |
---|
3429 | Makes directory @var{directory} the current directory for interpreting |
---|
3430 | both file names in the patch file, and file names given as arguments to |
---|
3431 | other options. @xref{patch Directories}. |
---|
3432 | |
---|
3433 | @item -D @var{name} |
---|
3434 | Make merged if-then-else output using @var{format}. @xref{If-then-else}. |
---|
3435 | |
---|
3436 | @item --debug=@var{number} |
---|
3437 | Set internal debugging flags. Of interest only to @code{patch} |
---|
3438 | patchers. |
---|
3439 | |
---|
3440 | @item -e |
---|
3441 | @itemx --ed |
---|
3442 | Interpret the patch file as an @code{ed} script. @xref{patch Input}. |
---|
3443 | |
---|
3444 | @item -E |
---|
3445 | Remove output files that are empty after the patches have been applied. |
---|
3446 | @xref{Empty Files}. |
---|
3447 | |
---|
3448 | @item -f |
---|
3449 | Assume that the user knows exactly what he or she is doing, and do not |
---|
3450 | ask any questions. @xref{patch Messages}. |
---|
3451 | |
---|
3452 | @item -F @var{lines} |
---|
3453 | Set the maximum fuzz factor to @var{lines}. @xref{Inexact}. |
---|
3454 | |
---|
3455 | @item --force |
---|
3456 | Assume that the user knows exactly what he or she is doing, and do not |
---|
3457 | ask any questions. @xref{patch Messages}. |
---|
3458 | |
---|
3459 | @item --forward |
---|
3460 | Ignore patches that @code{patch} thinks are reversed or already applied. |
---|
3461 | See also @samp{-R}. @xref{Reversed Patches}. |
---|
3462 | |
---|
3463 | @item --fuzz=@var{lines} |
---|
3464 | Set the maximum fuzz factor to @var{lines}. @xref{Inexact}. |
---|
3465 | |
---|
3466 | @item --help |
---|
3467 | Print a summary of the options that @code{patch} recognizes, then exit. |
---|
3468 | |
---|
3469 | @item --ifdef=@var{name} |
---|
3470 | Make merged if-then-else output using @var{format}. @xref{If-then-else}. |
---|
3471 | |
---|
3472 | @item --ignore-white-space |
---|
3473 | @itemx -l |
---|
3474 | Let any sequence of white space in the patch file match any sequence of |
---|
3475 | white space in the input file. @xref{Changed White Space}. |
---|
3476 | |
---|
3477 | @item -n |
---|
3478 | @itemx --normal |
---|
3479 | Interpret the patch file as a normal diff. @xref{patch Input}. |
---|
3480 | |
---|
3481 | @item -N |
---|
3482 | Ignore patches that @code{patch} thinks are reversed or already applied. |
---|
3483 | See also @samp{-R}. @xref{Reversed Patches}. |
---|
3484 | |
---|
3485 | @item -o @var{output-file} |
---|
3486 | @itemx --output=@var{output-file} |
---|
3487 | Use @var{output-file} as the output file name. @xref{patch Options}. |
---|
3488 | |
---|
3489 | @item -p@r{[}@var{number}@r{]} |
---|
3490 | Set the file name strip count to @var{number}. @xref{patch Directories}. |
---|
3491 | |
---|
3492 | @item --prefix=@var{backup-prefix} |
---|
3493 | Use @var{backup-prefix} as a prefix to the backup file name. If this |
---|
3494 | option is specified, any @samp{-b} option is ignored. @xref{Backups}. |
---|
3495 | |
---|
3496 | @item --quiet |
---|
3497 | Work silently unless an error occurs. @xref{patch Messages}. |
---|
3498 | |
---|
3499 | @item -r @var{reject-file} |
---|
3500 | Use @var{reject-file} as the reject file name. @xref{Rejects}. |
---|
3501 | |
---|
3502 | @item -R |
---|
3503 | Assume that this patch was created with the old and new files swapped. |
---|
3504 | @xref{Reversed Patches}. |
---|
3505 | |
---|
3506 | @item --reject-file=@var{reject-file} |
---|
3507 | Use @var{reject-file} as the reject file name. @xref{Rejects}. |
---|
3508 | |
---|
3509 | @item --remove-empty-files |
---|
3510 | Remove output files that are empty after the patches have been applied. |
---|
3511 | @xref{Empty Files}. |
---|
3512 | |
---|
3513 | @item --reverse |
---|
3514 | Assume that this patch was created with the old and new files swapped. |
---|
3515 | @xref{Reversed Patches}. |
---|
3516 | |
---|
3517 | @item -s |
---|
3518 | Work silently unless an error occurs. @xref{patch Messages}. |
---|
3519 | |
---|
3520 | @item -S |
---|
3521 | Ignore this patch from the patch file, but continue looking for the next |
---|
3522 | patch in the file. @xref{Multiple Patches}. |
---|
3523 | |
---|
3524 | @item --silent |
---|
3525 | Work silently unless an error occurs. @xref{patch Messages}. |
---|
3526 | |
---|
3527 | @item --skip |
---|
3528 | Ignore this patch from the patch file, but continue looking for the next |
---|
3529 | patch in the file. @xref{Multiple Patches}. |
---|
3530 | |
---|
3531 | @item --strip@r{[}=@var{number}@r{]} |
---|
3532 | Set the file name strip count to @var{number}. @xref{patch Directories}. |
---|
3533 | |
---|
3534 | @item --suffix=@var{backup-suffix} |
---|
3535 | Use @var{backup-suffix} as the backup extension instead of |
---|
3536 | @samp{.orig} or @samp{~}. @xref{Backups}. |
---|
3537 | |
---|
3538 | @item -t |
---|
3539 | Do not ask any questions. @xref{patch Messages}. |
---|
3540 | |
---|
3541 | @item -u |
---|
3542 | @itemx --unified |
---|
3543 | Interpret the patch file as a unified diff. @xref{patch Input}. |
---|
3544 | |
---|
3545 | @item -v |
---|
3546 | Output the revision header and patch level of @code{patch}. |
---|
3547 | |
---|
3548 | @item -V @var{backup-style} |
---|
3549 | Select the kind of backups to make. @xref{Backups}. |
---|
3550 | |
---|
3551 | @item --version |
---|
3552 | Output the revision header and patch level of @code{patch}, then exit. |
---|
3553 | |
---|
3554 | @item --version=control=@var{backup-style} |
---|
3555 | Select the kind of backups to make. @xref{Backups}. |
---|
3556 | |
---|
3557 | @item -x @var{number} |
---|
3558 | Set internal debugging flags. Of interest only to @code{patch} |
---|
3559 | patchers. |
---|
3560 | @end table |
---|
3561 | |
---|
3562 | @node Invoking sdiff, Incomplete Lines, Invoking patch, Top |
---|
3563 | @chapter Invoking @code{sdiff} |
---|
3564 | @cindex invoking @code{sdiff} |
---|
3565 | @cindex @code{sdiff} invocation |
---|
3566 | |
---|
3567 | The @code{sdiff} command merges two files and interactively outputs the |
---|
3568 | results. Its arguments are as follows: |
---|
3569 | |
---|
3570 | @example |
---|
3571 | sdiff -o @var{outfile} @var{options}@dots{} @var{from-file} @var{to-file} |
---|
3572 | @end example |
---|
3573 | |
---|
3574 | This merges @var{from-file} with @var{to-file}, with output to @var{outfile}. |
---|
3575 | If @var{from-file} is a directory and @var{to-file} is not, @code{sdiff} |
---|
3576 | compares the file in @var{from-file} whose file name is that of @var{to-file}, |
---|
3577 | and vice versa. @var{from-file} and @var{to-file} may not both be |
---|
3578 | directories. |
---|
3579 | |
---|
3580 | @code{sdiff} options begin with @samp{-}, so normally @var{from-file} |
---|
3581 | and @var{to-file} may not begin with @samp{-}. However, @samp{--} as an |
---|
3582 | argument by itself treats the remaining arguments as file names even if |
---|
3583 | they begin with @samp{-}. You may not use @samp{-} as an input file. |
---|
3584 | |
---|
3585 | An exit status of 0 means no differences were found, 1 means some |
---|
3586 | differences were found, and 2 means trouble. |
---|
3587 | |
---|
3588 | @code{sdiff} without @samp{-o} (or @samp{--output}) produces a |
---|
3589 | side-by-side difference. This usage is obsolete; use @samp{diff |
---|
3590 | --side-by-side} instead. |
---|
3591 | |
---|
3592 | @menu |
---|
3593 | * sdiff Options:: Summary of options to @code{diff}. |
---|
3594 | @end menu |
---|
3595 | |
---|
3596 | @node sdiff Options, , , Invoking sdiff |
---|
3597 | @section Options to @code{sdiff} |
---|
3598 | @cindex @code{sdiff} options |
---|
3599 | @cindex options for @code{sdiff} |
---|
3600 | |
---|
3601 | Below is a summary of all of the options that GNU @code{sdiff} accepts. |
---|
3602 | Each option has two equivalent names, one of which is a single |
---|
3603 | letter preceded by @samp{-}, and the other of which is a long name |
---|
3604 | preceded by @samp{--}. Multiple single letter options (unless they take |
---|
3605 | an argument) can be combined into a single command line argument. Long |
---|
3606 | named options can be abbreviated to any unique prefix of their name. |
---|
3607 | |
---|
3608 | @table @samp |
---|
3609 | @item -a |
---|
3610 | Treat all files as text and compare them line-by-line, even if they |
---|
3611 | do not appear to be text. @xref{Binary}. |
---|
3612 | |
---|
3613 | @item -b |
---|
3614 | Ignore changes in amount of white space. @xref{White Space}. |
---|
3615 | |
---|
3616 | @item -B |
---|
3617 | Ignore changes that just insert or delete blank lines. @xref{Blank |
---|
3618 | Lines}. |
---|
3619 | |
---|
3620 | @item -d |
---|
3621 | Change the algorithm to perhaps find a smaller set of changes. This |
---|
3622 | makes @code{sdiff} slower (sometimes much slower). @xref{diff |
---|
3623 | Performance}. |
---|
3624 | |
---|
3625 | @item -H |
---|
3626 | Use heuristics to speed handling of large files that have numerous |
---|
3627 | scattered small changes. @xref{diff Performance}. |
---|
3628 | |
---|
3629 | @item --expand-tabs |
---|
3630 | Expand tabs to spaces in the output, to preserve the alignment of tabs |
---|
3631 | in the input files. @xref{Tabs}. |
---|
3632 | |
---|
3633 | @item -i |
---|
3634 | Ignore changes in case; consider upper- and lower-case to be the same. |
---|
3635 | @xref{Case Folding}. |
---|
3636 | |
---|
3637 | @item -I @var{regexp} |
---|
3638 | Ignore changes that just insert or delete lines that match @var{regexp}. |
---|
3639 | @xref{Specified Folding}. |
---|
3640 | |
---|
3641 | @item --ignore-all-space |
---|
3642 | Ignore white space when comparing lines. @xref{White Space}. |
---|
3643 | |
---|
3644 | @item --ignore-blank-lines |
---|
3645 | Ignore changes that just insert or delete blank lines. @xref{Blank |
---|
3646 | Lines}. |
---|
3647 | |
---|
3648 | @item --ignore-case |
---|
3649 | Ignore changes in case; consider upper- and lower-case to be the same. |
---|
3650 | @xref{Case Folding}. |
---|
3651 | |
---|
3652 | @item --ignore-matching-lines=@var{regexp} |
---|
3653 | Ignore changes that just insert or delete lines that match @var{regexp}. |
---|
3654 | @xref{Specified Folding}. |
---|
3655 | |
---|
3656 | @item --ignore-space-change |
---|
3657 | Ignore changes in amount of white space. |
---|
3658 | @xref{White Space}. |
---|
3659 | |
---|
3660 | @item -l |
---|
3661 | @itemx --left-column |
---|
3662 | Print only the left column of two common lines. |
---|
3663 | @xref{Side by Side Format}. |
---|
3664 | |
---|
3665 | @item --minimal |
---|
3666 | Change the algorithm to perhaps find a smaller set of changes. This |
---|
3667 | makes @code{sdiff} slower (sometimes much slower). @xref{diff |
---|
3668 | Performance}. |
---|
3669 | |
---|
3670 | @item -o @var{file} |
---|
3671 | @itemx --output=@var{file} |
---|
3672 | Put merged output into @var{file}. This option is required for merging. |
---|
3673 | |
---|
3674 | @item -s |
---|
3675 | @itemx --suppress-common-lines |
---|
3676 | Do not print common lines. @xref{Side by Side Format}. |
---|
3677 | |
---|
3678 | @item --speed-large-files |
---|
3679 | Use heuristics to speed handling of large files that have numerous |
---|
3680 | scattered small changes. @xref{diff Performance}. |
---|
3681 | |
---|
3682 | @item -t |
---|
3683 | Expand tabs to spaces in the output, to preserve the alignment of tabs |
---|
3684 | in the input files. @xref{Tabs}. |
---|
3685 | |
---|
3686 | @item --text |
---|
3687 | Treat all files as text and compare them line-by-line, even if they |
---|
3688 | do not appear to be text. @xref{Binary}. |
---|
3689 | |
---|
3690 | @item -v |
---|
3691 | @itemx --version |
---|
3692 | Output the version number of @code{sdiff}. |
---|
3693 | |
---|
3694 | @item -w @var{columns} |
---|
3695 | @itemx --width=@var{columns} |
---|
3696 | Use an output width of @var{columns}. @xref{Side by Side Format}. |
---|
3697 | Note that for historical reasons, this option is @samp{-W} in @code{diff}, |
---|
3698 | @samp{-w} in @code{sdiff}. |
---|
3699 | |
---|
3700 | @item -W |
---|
3701 | Ignore horizontal white space when comparing lines. @xref{White Space}. |
---|
3702 | Note that for historical reasons, this option is @samp{-w} in @code{diff}, |
---|
3703 | @samp{-W} in @code{sdiff}. |
---|
3704 | @end table |
---|
3705 | |
---|
3706 | @node Incomplete Lines, Projects, Invoking sdiff, Top |
---|
3707 | @chapter Incomplete Lines |
---|
3708 | @cindex incomplete lines |
---|
3709 | @cindex full lines |
---|
3710 | @cindex newline treatment by @code{diff} |
---|
3711 | |
---|
3712 | When an input file ends in a non-newline character, its last line is |
---|
3713 | called an @dfn{incomplete line} because its last character is not a |
---|
3714 | newline. All other lines are called @dfn{full lines} and end in a |
---|
3715 | newline character. Incomplete lines do not match full lines unless |
---|
3716 | differences in white space are ignored (@pxref{White Space}). |
---|
3717 | |
---|
3718 | An incomplete line is normally distinguished on output from a full line |
---|
3719 | by a following line that starts with @samp{\}. However, the RCS format |
---|
3720 | (@pxref{RCS}) outputs the incomplete line as-is, without any trailing |
---|
3721 | newline or following line. The side by side format normally represents |
---|
3722 | incomplete lines as-is, but in some cases uses a @samp{\} or @samp{/} |
---|
3723 | gutter marker; @xref{Side by Side}. The if-then-else line format |
---|
3724 | preserves a line's incompleteness with @samp{%L}, and discards the |
---|
3725 | newline with @samp{%l}; @xref{Line Formats}. Finally, with the |
---|
3726 | @code{ed} and forward @code{ed} output formats (@pxref{Output Formats}) |
---|
3727 | @code{diff} cannot represent an incomplete line, so it pretends there |
---|
3728 | was a newline and reports an error. |
---|
3729 | |
---|
3730 | For example, suppose @file{F} and @file{G} are one-byte files that |
---|
3731 | contain just @samp{f} and @samp{g}, respectively. Then @samp{diff F G} |
---|
3732 | outputs |
---|
3733 | |
---|
3734 | @example |
---|
3735 | 1c1 |
---|
3736 | < f |
---|
3737 | \ No newline at end of file |
---|
3738 | --- |
---|
3739 | > g |
---|
3740 | \ No newline at end of file |
---|
3741 | @end example |
---|
3742 | |
---|
3743 | @noindent |
---|
3744 | (The exact message may differ in non-English locales.) |
---|
3745 | @samp{diff -n F G} outputs the following without a trailing newline: |
---|
3746 | |
---|
3747 | @example |
---|
3748 | d1 1 |
---|
3749 | a1 1 |
---|
3750 | g |
---|
3751 | @end example |
---|
3752 | |
---|
3753 | @samp{diff -e F G} reports two errors and outputs the following: |
---|
3754 | |
---|
3755 | @example |
---|
3756 | 1c |
---|
3757 | g |
---|
3758 | . |
---|
3759 | @end example |
---|
3760 | |
---|
3761 | @node Projects, Concept Index, Incomplete Lines, Top |
---|
3762 | @chapter Future Projects |
---|
3763 | |
---|
3764 | Here are some ideas for improving GNU @code{diff} and @code{patch}. The |
---|
3765 | GNU project has identified some improvements as potential programming |
---|
3766 | projects for volunteers. You can also help by reporting any bugs that |
---|
3767 | you find. |
---|
3768 | |
---|
3769 | If you are a programmer and would like to contribute something to the |
---|
3770 | GNU project, please consider volunteering for one of these projects. If |
---|
3771 | you are seriously contemplating work, please write to |
---|
3772 | @samp{gnu@@prep.ai.mit.edu} to coordinate with other volunteers. |
---|
3773 | |
---|
3774 | @menu |
---|
3775 | * Shortcomings:: Suggested projects for improvements. |
---|
3776 | * Bugs:: Reporting bugs. |
---|
3777 | @end menu |
---|
3778 | |
---|
3779 | @node Shortcomings, Bugs, , Projects |
---|
3780 | @section Suggested Projects for Improving GNU @code{diff} and @code{patch} |
---|
3781 | @cindex projects for directories |
---|
3782 | |
---|
3783 | One should be able to use GNU @code{diff} to generate a patch from any |
---|
3784 | pair of directory trees, and given the patch and a copy of one such |
---|
3785 | tree, use @code{patch} to generate a faithful copy of the other. |
---|
3786 | Unfortunately, some changes to directory trees cannot be expressed using |
---|
3787 | current patch formats; also, @code{patch} does not handle some of the |
---|
3788 | existing formats. These shortcomings motivate the following suggested |
---|
3789 | projects. |
---|
3790 | |
---|
3791 | @menu |
---|
3792 | * Changing Structure:: Handling changes to the directory structure. |
---|
3793 | * Special Files:: Handling symbolic links, device special files, etc. |
---|
3794 | * Unusual File Names:: Handling file names that contain unusual characters. |
---|
3795 | * Arbitrary Limits:: Patching non-text files. |
---|
3796 | * Large Files:: Handling files that do not fit in memory. |
---|
3797 | * Ignoring Changes:: Ignoring certain changes while showing others. |
---|
3798 | @end menu |
---|
3799 | |
---|
3800 | @node Changing Structure, Special Files, , Shortcomings |
---|
3801 | @subsection Handling Changes to the Directory Structure |
---|
3802 | @cindex directory structure changes |
---|
3803 | |
---|
3804 | @code{diff} and @code{patch} do not handle some changes to directory |
---|
3805 | structure. For example, suppose one directory tree contains a directory |
---|
3806 | named @samp{D} with some subsidiary files, and another contains a file |
---|
3807 | with the same name @samp{D}. @samp{diff -r} does not output enough |
---|
3808 | information for @code{patch} to transform the the directory subtree into |
---|
3809 | the file. |
---|
3810 | |
---|
3811 | There should be a way to specify that a file has been deleted without |
---|
3812 | having to include its entire contents in the patch file. There should |
---|
3813 | also be a way to tell @code{patch} that a file was renamed, even if |
---|
3814 | there is no way for @code{diff} to generate such information. |
---|
3815 | |
---|
3816 | These problems can be fixed by extending the @code{diff} output format |
---|
3817 | to represent changes in directory structure, and extending @code{patch} |
---|
3818 | to understand these extensions. |
---|
3819 | |
---|
3820 | @node Special Files, Unusual File Names, Changing Structure, Shortcomings |
---|
3821 | @subsection Files that are Neither Directories Nor Regular Files |
---|
3822 | @cindex special files |
---|
3823 | |
---|
3824 | Some files are neither directories nor regular files: they are unusual |
---|
3825 | files like symbolic links, device special files, named pipes, and |
---|
3826 | sockets. Currently, @code{diff} treats symbolic links like regular files; |
---|
3827 | it treats other special files like regular files if they are specified |
---|
3828 | at the top level, but simply reports their presence when comparing |
---|
3829 | directories. This means that @code{patch} cannot represent changes |
---|
3830 | to such files. For example, if you change which file a symbolic link |
---|
3831 | points to, @code{diff} outputs the difference between the two files, |
---|
3832 | instead of the change to the symbolic link. |
---|
3833 | |
---|
3834 | @c This might not be a good idea; is it wise for root to install devices |
---|
3835 | @c this way? |
---|
3836 | @code{diff} should optionally report changes to special files specially, |
---|
3837 | and @code{patch} should be extended to understand these extensions. |
---|
3838 | |
---|
3839 | @node Unusual File Names, Arbitrary Limits, Special Files, Shortcomings |
---|
3840 | @subsection File Names that Contain Unusual Characters |
---|
3841 | @cindex file names with unusual characters |
---|
3842 | |
---|
3843 | When a file name contains an unusual character like a newline or |
---|
3844 | white space, @samp{diff -r} generates a patch that @code{patch} cannot |
---|
3845 | parse. The problem is with format of @code{diff} output, not just with |
---|
3846 | @code{patch}, because with odd enough file names one can cause |
---|
3847 | @code{diff} to generate a patch that is syntactically correct but |
---|
3848 | patches the wrong files. The format of @code{diff} output should be |
---|
3849 | extended to handle all possible file names. |
---|
3850 | |
---|
3851 | @node Arbitrary Limits, Large Files, Unusual File Names, Shortcomings |
---|
3852 | @subsection Arbitrary Limits |
---|
3853 | @cindex binary file patching |
---|
3854 | |
---|
3855 | GNU @code{diff} can analyze files with arbitrarily long lines and files |
---|
3856 | that end in incomplete lines. However, @code{patch} cannot patch such |
---|
3857 | files. The @code{patch} internal limits on line lengths should be |
---|
3858 | removed, and @code{patch} should be extended to parse @code{diff} |
---|
3859 | reports of incomplete lines. |
---|
3860 | |
---|
3861 | @node Large Files, Ignoring Changes, Arbitrary Limits, Shortcomings |
---|
3862 | @subsection Handling Files that Do Not Fit in Memory |
---|
3863 | @cindex large files |
---|
3864 | |
---|
3865 | @code{diff} operates by reading both files into memory. This method |
---|
3866 | fails if the files are too large, and @code{diff} should have a fallback. |
---|
3867 | |
---|
3868 | One way to do this is to scan the files sequentially to compute hash |
---|
3869 | codes of the lines and put the lines in equivalence classes based only |
---|
3870 | on hash code. Then compare the files normally. This does produce some |
---|
3871 | false matches. |
---|
3872 | |
---|
3873 | Then scan the two files sequentially again, checking each match to see |
---|
3874 | whether it is real. When a match is not real, mark both the |
---|
3875 | ``matching'' lines as changed. Then build an edit script as usual. |
---|
3876 | |
---|
3877 | The output routines would have to be changed to scan the files |
---|
3878 | sequentially looking for the text to print. |
---|
3879 | |
---|
3880 | @node Ignoring Changes,, Large Files, Shortcomings |
---|
3881 | @subsection Ignoring Certain Changes |
---|
3882 | |
---|
3883 | It would be nice to have a feature for specifying two strings, one in |
---|
3884 | @var{from-file} and one in @var{to-file}, which should be considered to |
---|
3885 | match. Thus, if the two strings are @samp{foo} and @samp{bar}, then if |
---|
3886 | two lines differ only in that @samp{foo} in file 1 corresponds to |
---|
3887 | @samp{bar} in file 2, the lines are treated as identical. |
---|
3888 | |
---|
3889 | It is not clear how general this feature can or should be, or |
---|
3890 | what syntax should be used for it. |
---|
3891 | |
---|
3892 | @node Bugs, , Shortcomings, Projects |
---|
3893 | @section Reporting Bugs |
---|
3894 | @cindex bug reports |
---|
3895 | @cindex reporting bugs |
---|
3896 | |
---|
3897 | If you think you have found a bug in GNU @code{cmp}, @code{diff}, |
---|
3898 | @code{diff3}, @code{sdiff}, or @code{patch}, please report it by |
---|
3899 | electronic mail to @samp{bug-gnu-utils@@prep.ai.mit.edu}. Send as |
---|
3900 | precise a description of the problem as you can, including sample input |
---|
3901 | files that produce the bug, if applicable. |
---|
3902 | |
---|
3903 | Because Larry Wall has not released a new version of @code{patch} since |
---|
3904 | mid 1988 and the GNU version of @code{patch} has been changed since |
---|
3905 | then, please send bug reports for @code{patch} by electronic mail to |
---|
3906 | both @samp{bug-gnu-utils@@prep.ai.mit.edu} and |
---|
3907 | @samp{lwall@@netlabs.com}. |
---|
3908 | |
---|
3909 | @node Concept Index, , Projects, Top |
---|
3910 | @unnumbered Concept Index |
---|
3911 | |
---|
3912 | @printindex cp |
---|
3913 | |
---|
3914 | @shortcontents |
---|
3915 | @contents |
---|
3916 | @bye |
---|