1 | This is Info file diff.info, produced by Makeinfo-1.55 from the input |
---|
2 | file ./diff.texi. |
---|
3 | |
---|
4 | This file documents the the GNU `diff', `diff3', `sdiff', and `cmp' |
---|
5 | commands for showing the differences between text files and the `patch' |
---|
6 | command for using their output to update files. |
---|
7 | |
---|
8 | Copyright (C) 1992, 1993, 1994 Free Software Foundation, Inc. |
---|
9 | |
---|
10 | Permission is granted to make and distribute verbatim copies of this |
---|
11 | manual provided the copyright notice and this permission notice are |
---|
12 | preserved on all copies. |
---|
13 | |
---|
14 | Permission is granted to copy and distribute modified versions of |
---|
15 | this manual under the conditions for verbatim copying, provided that |
---|
16 | the entire resulting derived work is distributed under the terms of a |
---|
17 | permission notice identical to this one. |
---|
18 | |
---|
19 | Permission is granted to copy and distribute translations of this |
---|
20 | manual into another language, under the above conditions for modified |
---|
21 | versions, except that this permission notice may be stated in a |
---|
22 | translation approved by the Foundation. |
---|
23 | |
---|
24 | |
---|
25 | File: diff.info, Node: Top, Up: (dir) |
---|
26 | |
---|
27 | This file documents the the GNU `diff', `diff3', `sdiff', and `cmp' |
---|
28 | commands for showing the differences between text files and the `patch' |
---|
29 | command for using their output to update files. |
---|
30 | |
---|
31 | This is Edition 1.2, for `diff' 2.4 and `patch' 2.1. |
---|
32 | |
---|
33 | * Menu: |
---|
34 | |
---|
35 | * Overview:: Preliminary information. |
---|
36 | |
---|
37 | * Comparison:: What file comparison means. |
---|
38 | * Output Formats:: Formats for difference reports. |
---|
39 | * Comparing Directories:: Comparing files and directories. |
---|
40 | * Adjusting Output:: Making `diff' output prettier. |
---|
41 | * diff Performance:: Making `diff' smarter or faster. |
---|
42 | * Comparing Three Files:: Formats for three-way difference reports. |
---|
43 | |
---|
44 | * diff3 Merging:: Merging from a common ancestor. |
---|
45 | * Interactive Merging:: Interactive merging with `sdiff'. |
---|
46 | * Merging with patch:: Using `patch' to change old files into new ones. |
---|
47 | * Making Patches:: Tips for making patch distributions. |
---|
48 | |
---|
49 | * Invoking cmp:: How to run `cmp' and a summary of its options. |
---|
50 | * Invoking diff:: How to run `diff' and a summary of its options. |
---|
51 | * Invoking diff3:: How to run `diff3' and a summary of its options. |
---|
52 | * Invoking patch:: How to run `patch' and a summary of its options. |
---|
53 | * Invoking sdiff:: How to run `sdiff' and a summary of its options. |
---|
54 | |
---|
55 | * Incomplete Lines:: Lines that lack trailing newlines. |
---|
56 | * Projects:: If you think you've found a bug or other shortcoming. |
---|
57 | |
---|
58 | * Concept Index:: Index of concepts. |
---|
59 | |
---|
60 | |
---|
61 | File: diff.info, Node: Overview, Next: Comparison, Up: Top |
---|
62 | |
---|
63 | Overview |
---|
64 | ******** |
---|
65 | |
---|
66 | Computer users often find occasion to ask how two files differ. |
---|
67 | Perhaps one file is a newer version of the other file. Or maybe the |
---|
68 | two files started out as identical copies but were changed by different |
---|
69 | people. |
---|
70 | |
---|
71 | You can use the `diff' command to show differences between two |
---|
72 | files, or each corresponding file in two directories. `diff' outputs |
---|
73 | differences between files line by line in any of several formats, |
---|
74 | selectable by command line options. This set of differences is often |
---|
75 | called a "diff" or "patch". For files that are identical, `diff' |
---|
76 | normally produces no output; for binary (non-text) files, `diff' |
---|
77 | normally reports only that they are different. |
---|
78 | |
---|
79 | You can use the `cmp' command to show the offsets and line numbers |
---|
80 | where two files differ. `cmp' can also show all the characters that |
---|
81 | differ between the two files, side by side. Another way to compare two |
---|
82 | files character by character is the Emacs command `M-x |
---|
83 | compare-windows'. *Note Other Window: (emacs)Other Window, for more |
---|
84 | information on that command. |
---|
85 | |
---|
86 | You can use the `diff3' command to show differences among three |
---|
87 | files. When two people have made independent changes to a common |
---|
88 | original, `diff3' can report the differences between the original and |
---|
89 | the two changed versions, and can produce a merged file that contains |
---|
90 | both persons' changes together with warnings about conflicts. |
---|
91 | |
---|
92 | You can use the `sdiff' command to merge two files interactively. |
---|
93 | |
---|
94 | You can use the set of differences produced by `diff' to distribute |
---|
95 | updates to text files (such as program source code) to other people. |
---|
96 | This method is especially useful when the differences are small compared |
---|
97 | to the complete files. Given `diff' output, you can use the `patch' |
---|
98 | program to update, or "patch", a copy of the file. If you think of |
---|
99 | `diff' as subtracting one file from another to produce their |
---|
100 | difference, you can think of `patch' as adding the difference to one |
---|
101 | file to reproduce the other. |
---|
102 | |
---|
103 | This manual first concentrates on making diffs, and later shows how |
---|
104 | to use diffs to update files. |
---|
105 | |
---|
106 | GNU `diff' was written by Mike Haertel, David Hayes, Richard |
---|
107 | Stallman, Len Tower, and Paul Eggert. Wayne Davison designed and |
---|
108 | implemented the unified output format. The basic algorithm is described |
---|
109 | in "An O(ND) Difference Algorithm and its Variations", Eugene W. Myers, |
---|
110 | `Algorithmica' Vol. 1 No. 2, 1986, pp. 251-266; and in "A File |
---|
111 | Comparison Program", Webb Miller and Eugene W. Myers, |
---|
112 | `Software--Practice and Experience' Vol. 15 No. 11, 1985, pp. 1025-1040. |
---|
113 | The algorithm was independently discovered as described in "Algorithms |
---|
114 | for Approximate String Matching", E. Ukkonen, `Information and Control' |
---|
115 | Vol. 64, 1985, pp. 100-118. |
---|
116 | |
---|
117 | GNU `diff3' was written by Randy Smith. GNU `sdiff' was written by |
---|
118 | Thomas Lord. GNU `cmp' was written by Torbjorn Granlund and David |
---|
119 | MacKenzie. |
---|
120 | |
---|
121 | `patch' was written mainly by Larry Wall; the GNU enhancements were |
---|
122 | written mainly by Wayne Davison and David MacKenzie. Parts of this |
---|
123 | manual are adapted from a manual page written by Larry Wall, with his |
---|
124 | permission. |
---|
125 | |
---|
126 | |
---|
127 | File: diff.info, Node: Comparison, Next: Output Formats, Prev: Overview, Up: Top |
---|
128 | |
---|
129 | What Comparison Means |
---|
130 | ********************* |
---|
131 | |
---|
132 | There are several ways to think about the differences between two |
---|
133 | files. One way to think of the differences is as a series of lines |
---|
134 | that were deleted from, inserted in, or changed in one file to produce |
---|
135 | the other file. `diff' compares two files line by line, finds groups of |
---|
136 | lines that differ, and reports each group of differing lines. It can |
---|
137 | report the differing lines in several formats, which have different |
---|
138 | purposes. |
---|
139 | |
---|
140 | GNU `diff' can show whether files are different without detailing |
---|
141 | the differences. It also provides ways to suppress certain kinds of |
---|
142 | differences that are not important to you. Most commonly, such |
---|
143 | differences are changes in the amount of white space between words or |
---|
144 | lines. `diff' also provides ways to suppress differences in alphabetic |
---|
145 | case or in lines that match a regular expression that you provide. |
---|
146 | These options can accumulate; for example, you can ignore changes in |
---|
147 | both white space and alphabetic case. |
---|
148 | |
---|
149 | Another way to think of the differences between two files is as a |
---|
150 | sequence of pairs of characters that can be either identical or |
---|
151 | different. `cmp' reports the differences between two files character |
---|
152 | by character, instead of line by line. As a result, it is more useful |
---|
153 | than `diff' for comparing binary files. For text files, `cmp' is |
---|
154 | useful mainly when you want to know only whether two files are |
---|
155 | identical. |
---|
156 | |
---|
157 | To illustrate the effect that considering changes character by |
---|
158 | character can have compared with considering them line by line, think |
---|
159 | of what happens if a single newline character is added to the beginning |
---|
160 | of a file. If that file is then compared with an otherwise identical |
---|
161 | file that lacks the newline at the beginning, `diff' will report that a |
---|
162 | blank line has been added to the file, while `cmp' will report that |
---|
163 | almost every character of the two files differs. |
---|
164 | |
---|
165 | `diff3' normally compares three input files line by line, finds |
---|
166 | groups of lines that differ, and reports each group of differing lines. |
---|
167 | Its output is designed to make it easy to inspect two different sets of |
---|
168 | changes to the same file. |
---|
169 | |
---|
170 | * Menu: |
---|
171 | |
---|
172 | * Hunks:: Groups of differing lines. |
---|
173 | * White Space:: Suppressing differences in white space. |
---|
174 | * Blank Lines:: Suppressing differences in blank lines. |
---|
175 | * Case Folding:: Suppressing differences in alphabetic case. |
---|
176 | * Specified Folding:: Suppressing differences that match regular expressions. |
---|
177 | * Brief:: Summarizing which files are different. |
---|
178 | * Binary:: Comparing binary files or forcing text comparisons. |
---|
179 | |
---|
180 | |
---|
181 | File: diff.info, Node: Hunks, Next: White Space, Up: Comparison |
---|
182 | |
---|
183 | Hunks |
---|
184 | ===== |
---|
185 | |
---|
186 | When comparing two files, `diff' finds sequences of lines common to |
---|
187 | both files, interspersed with groups of differing lines called "hunks". |
---|
188 | Comparing two identical files yields one sequence of common lines and |
---|
189 | no hunks, because no lines differ. Comparing two entirely different |
---|
190 | files yields no common lines and one large hunk that contains all lines |
---|
191 | of both files. In general, there are many ways to match up lines |
---|
192 | between two given files. `diff' tries to minimize the total hunk size |
---|
193 | by finding large sequences of common lines interspersed with small |
---|
194 | hunks of differing lines. |
---|
195 | |
---|
196 | For example, suppose the file `F' contains the three lines `a', `b', |
---|
197 | `c', and the file `G' contains the same three lines in reverse order |
---|
198 | `c', `b', `a'. If `diff' finds the line `c' as common, then the command |
---|
199 | `diff F G' produces this output: |
---|
200 | |
---|
201 | 1,2d0 |
---|
202 | < a |
---|
203 | < b |
---|
204 | 3a2,3 |
---|
205 | > b |
---|
206 | > a |
---|
207 | |
---|
208 | But if `diff' notices the common line `b' instead, it produces this |
---|
209 | output: |
---|
210 | |
---|
211 | 1c1 |
---|
212 | < a |
---|
213 | --- |
---|
214 | > c |
---|
215 | 3c3 |
---|
216 | < c |
---|
217 | --- |
---|
218 | > a |
---|
219 | |
---|
220 | It is also possible to find `a' as the common line. `diff' does not |
---|
221 | always find an optimal matching between the files; it takes shortcuts |
---|
222 | to run faster. But its output is usually close to the shortest |
---|
223 | possible. You can adjust this tradeoff with the `--minimal' option |
---|
224 | (*note diff Performance::.). |
---|
225 | |
---|
226 | |
---|
227 | File: diff.info, Node: White Space, Next: Blank Lines, Prev: Hunks, Up: Comparison |
---|
228 | |
---|
229 | Suppressing Differences in Blank and Tab Spacing |
---|
230 | ================================================ |
---|
231 | |
---|
232 | The `-b' and `--ignore-space-change' options ignore white space at |
---|
233 | line end, and considers all other sequences of one or more white space |
---|
234 | characters to be equivalent. With these options, `diff' considers the |
---|
235 | following two lines to be equivalent, where `$' denotes the line end: |
---|
236 | |
---|
237 | Here lyeth muche rychnesse in lytell space. -- John Heywood$ |
---|
238 | Here lyeth muche rychnesse in lytell space. -- John Heywood $ |
---|
239 | |
---|
240 | The `-w' and `--ignore-all-space' options are stronger than `-b'. |
---|
241 | They ignore difference even if one file has white space where the other |
---|
242 | file has none. "White space" characters include tab, newline, vertical |
---|
243 | tab, form feed, carriage return, and space; some locales may define |
---|
244 | additional characters to be white space. With these options, `diff' |
---|
245 | considers the following two lines to be equivalent, where `$' denotes |
---|
246 | the line end and `^M' denotes a carriage return: |
---|
247 | |
---|
248 | Here lyeth muche rychnesse in lytell space.-- John Heywood$ |
---|
249 | He relyeth much erychnes seinly tells pace. --John Heywood ^M$ |
---|
250 | |
---|
251 | |
---|
252 | File: diff.info, Node: Blank Lines, Next: Case Folding, Prev: White Space, Up: Comparison |
---|
253 | |
---|
254 | Suppressing Differences in Blank Lines |
---|
255 | ====================================== |
---|
256 | |
---|
257 | The `-B' and `--ignore-blank-lines' options ignore insertions or |
---|
258 | deletions of blank lines. These options normally affect only lines |
---|
259 | that are completely empty; they do not affect lines that look empty but |
---|
260 | contain space or tab characters. With these options, for example, a |
---|
261 | file containing |
---|
262 | 1. A point is that which has no part. |
---|
263 | |
---|
264 | 2. A line is breadthless length. |
---|
265 | -- Euclid, The Elements, I |
---|
266 | |
---|
267 | is considered identical to a file containing |
---|
268 | 1. A point is that which has no part. |
---|
269 | 2. A line is breadthless length. |
---|
270 | |
---|
271 | |
---|
272 | -- Euclid, The Elements, I |
---|
273 | |
---|
274 | |
---|
275 | File: diff.info, Node: Case Folding, Next: Specified Folding, Prev: Blank Lines, Up: Comparison |
---|
276 | |
---|
277 | Suppressing Case Differences |
---|
278 | ============================ |
---|
279 | |
---|
280 | GNU `diff' can treat lowercase letters as equivalent to their |
---|
281 | uppercase counterparts, so that, for example, it considers `Funky |
---|
282 | Stuff', `funky STUFF', and `fUNKy stuFf' to all be the same. To |
---|
283 | request this, use the `-i' or `--ignore-case' option. |
---|
284 | |
---|
285 | |
---|
286 | File: diff.info, Node: Specified Folding, Next: Brief, Prev: Case Folding, Up: Comparison |
---|
287 | |
---|
288 | Suppressing Lines Matching a Regular Expression |
---|
289 | =============================================== |
---|
290 | |
---|
291 | To ignore insertions and deletions of lines that match a regular |
---|
292 | expression, use the `-I REGEXP' or `--ignore-matching-lines=REGEXP' |
---|
293 | option. You should escape regular expressions that contain shell |
---|
294 | metacharacters to prevent the shell from expanding them. For example, |
---|
295 | `diff -I '^[0-9]'' ignores all changes to lines beginning with a digit. |
---|
296 | |
---|
297 | However, `-I' only ignores the insertion or deletion of lines that |
---|
298 | contain the regular expression if every changed line in the hunk--every |
---|
299 | insertion and every deletion--matches the regular expression. In other |
---|
300 | words, for each nonignorable change, `diff' prints the complete set of |
---|
301 | changes in its vicinity, including the ignorable ones. |
---|
302 | |
---|
303 | You can specify more than one regular expression for lines to ignore |
---|
304 | by using more than one `-I' option. `diff' tries to match each line |
---|
305 | against each regular expression, starting with the last one given. |
---|
306 | |
---|
307 | |
---|
308 | File: diff.info, Node: Brief, Next: Binary, Prev: Specified Folding, Up: Comparison |
---|
309 | |
---|
310 | Summarizing Which Files Differ |
---|
311 | ============================== |
---|
312 | |
---|
313 | When you only want to find out whether files are different, and you |
---|
314 | don't care what the differences are, you can use the summary output |
---|
315 | format. In this format, instead of showing the differences between the |
---|
316 | files, `diff' simply reports whether files differ. The `-q' and |
---|
317 | `--brief' options select this output format. |
---|
318 | |
---|
319 | This format is especially useful when comparing the contents of two |
---|
320 | directories. It is also much faster than doing the normal line by line |
---|
321 | comparisons, because `diff' can stop analyzing the files as soon as it |
---|
322 | knows that there are any differences. |
---|
323 | |
---|
324 | You can also get a brief indication of whether two files differ by |
---|
325 | using `cmp'. For files that are identical, `cmp' produces no output. |
---|
326 | When the files differ, by default, `cmp' outputs the byte offset and |
---|
327 | line number where the first difference occurs. You can use the `-s' |
---|
328 | option to suppress that information, so that `cmp' produces no output |
---|
329 | and reports whether the files differ using only its exit status (*note |
---|
330 | Invoking cmp::.). |
---|
331 | |
---|
332 | Unlike `diff', `cmp' cannot compare directories; it can only compare |
---|
333 | two files. |
---|
334 | |
---|
335 | |
---|
336 | File: diff.info, Node: Binary, Prev: Brief, Up: Comparison |
---|
337 | |
---|
338 | Binary Files and Forcing Text Comparisons |
---|
339 | ========================================= |
---|
340 | |
---|
341 | If `diff' thinks that either of the two files it is comparing is |
---|
342 | binary (a non-text file), it normally treats that pair of files much as |
---|
343 | if the summary output format had been selected (*note Brief::.), and |
---|
344 | reports only that the binary files are different. This is because line |
---|
345 | by line comparisons are usually not meaningful for binary files. |
---|
346 | |
---|
347 | `diff' determines whether a file is text or binary by checking the |
---|
348 | first few bytes in the file; the exact number of bytes is system |
---|
349 | dependent, but it is typically several thousand. If every character in |
---|
350 | that part of the file is non-null, `diff' considers the file to be |
---|
351 | text; otherwise it considers the file to be binary. |
---|
352 | |
---|
353 | Sometimes you might want to force `diff' to consider files to be |
---|
354 | text. For example, you might be comparing text files that contain null |
---|
355 | characters; `diff' would erroneously decide that those are non-text |
---|
356 | files. Or you might be comparing documents that are in a format used |
---|
357 | by a word processing system that uses null characters to indicate |
---|
358 | special formatting. You can force `diff' to consider all files to be |
---|
359 | text files, and compare them line by line, by using the `-a' or |
---|
360 | `--text' option. If the files you compare using this option do not in |
---|
361 | fact contain text, they will probably contain few newline characters, |
---|
362 | and the `diff' output will consist of hunks showing differences between |
---|
363 | long lines of whatever characters the files contain. |
---|
364 | |
---|
365 | You can also force `diff' to consider all files to be binary files, |
---|
366 | and report only whether they differ (but not how). Use the `--brief' |
---|
367 | option for this. |
---|
368 | |
---|
369 | In operating systems that distinguish between text and binary files, |
---|
370 | `diff' normally reads and writes all data as text. Use the `--binary' |
---|
371 | option to force `diff' to read and write binary data instead. This |
---|
372 | option has no effect on a Posix-compliant system like GNU or |
---|
373 | traditional Unix. However, many personal computer operating systems |
---|
374 | represent the end of a line with a carriage return followed by a |
---|
375 | newline. On such systems, `diff' normally ignores these carriage |
---|
376 | returns on input and generates them at the end of each output line, but |
---|
377 | with the `--binary' option `diff' treats each carriage return as just |
---|
378 | another input character, and does not generate a carriage return at the |
---|
379 | end of each output line. This can be useful when dealing with non-text |
---|
380 | files that are meant to be interchanged with Posix-compliant systems. |
---|
381 | |
---|
382 | If you want to compare two files byte by byte, you can use the `cmp' |
---|
383 | program with the `-l' option to show the values of each differing byte |
---|
384 | in the two files. With GNU `cmp', you can also use the `-c' option to |
---|
385 | show the ASCII representation of those bytes. *Note Invoking cmp::, |
---|
386 | for more information. |
---|
387 | |
---|
388 | If `diff3' thinks that any of the files it is comparing is binary (a |
---|
389 | non-text file), it normally reports an error, because such comparisons |
---|
390 | are usually not useful. `diff3' uses the same test as `diff' to decide |
---|
391 | whether a file is binary. As with `diff', if the input files contain a |
---|
392 | few non-text characters but otherwise are like text files, you can |
---|
393 | force `diff3' to consider all files to be text files and compare them |
---|
394 | line by line by using the `-a' or `--text' options. |
---|
395 | |
---|
396 | |
---|
397 | File: diff.info, Node: Output Formats, Next: Comparing Directories, Prev: Comparison, Up: Top |
---|
398 | |
---|
399 | `diff' Output Formats |
---|
400 | ********************* |
---|
401 | |
---|
402 | `diff' has several mutually exclusive options for output format. |
---|
403 | The following sections describe each format, illustrating how `diff' |
---|
404 | reports the differences between two sample input files. |
---|
405 | |
---|
406 | * Menu: |
---|
407 | |
---|
408 | * Sample diff Input:: Sample `diff' input files for examples. |
---|
409 | * Normal:: Showing differences without surrounding text. |
---|
410 | * Context:: Showing differences with the surrounding text. |
---|
411 | * Side by Side:: Showing differences in two columns. |
---|
412 | * Scripts:: Generating scripts for other programs. |
---|
413 | * If-then-else:: Merging files with if-then-else. |
---|
414 | |
---|
415 | |
---|
416 | File: diff.info, Node: Sample diff Input, Next: Normal, Up: Output Formats |
---|
417 | |
---|
418 | Two Sample Input Files |
---|
419 | ====================== |
---|
420 | |
---|
421 | Here are two sample files that we will use in numerous examples to |
---|
422 | illustrate the output of `diff' and how various options can change it. |
---|
423 | |
---|
424 | This is the file `lao': |
---|
425 | |
---|
426 | The Way that can be told of is not the eternal Way; |
---|
427 | The name that can be named is not the eternal name. |
---|
428 | The Nameless is the origin of Heaven and Earth; |
---|
429 | The Named is the mother of all things. |
---|
430 | Therefore let there always be non-being, |
---|
431 | so we may see their subtlety, |
---|
432 | And let there always be being, |
---|
433 | so we may see their outcome. |
---|
434 | The two are the same, |
---|
435 | But after they are produced, |
---|
436 | they have different names. |
---|
437 | |
---|
438 | This is the file `tzu': |
---|
439 | |
---|
440 | The Nameless is the origin of Heaven and Earth; |
---|
441 | The named is the mother of all things. |
---|
442 | |
---|
443 | Therefore let there always be non-being, |
---|
444 | so we may see their subtlety, |
---|
445 | And let there always be being, |
---|
446 | so we may see their outcome. |
---|
447 | The two are the same, |
---|
448 | But after they are produced, |
---|
449 | they have different names. |
---|
450 | They both may be called deep and profound. |
---|
451 | Deeper and more profound, |
---|
452 | The door of all subtleties! |
---|
453 | |
---|
454 | In this example, the first hunk contains just the first two lines of |
---|
455 | `lao', the second hunk contains the fourth line of `lao' opposing the |
---|
456 | second and third lines of `tzu', and the last hunk contains just the |
---|
457 | last three lines of `tzu'. |
---|
458 | |
---|
459 | |
---|
460 | File: diff.info, Node: Normal, Next: Context, Prev: Sample diff Input, Up: Output Formats |
---|
461 | |
---|
462 | Showing Differences Without Context |
---|
463 | =================================== |
---|
464 | |
---|
465 | The "normal" `diff' output format shows each hunk of differences |
---|
466 | without any surrounding context. Sometimes such output is the clearest |
---|
467 | way to see how lines have changed, without the clutter of nearby |
---|
468 | unchanged lines (although you can get similar results with the context |
---|
469 | or unified formats by using 0 lines of context). However, this format |
---|
470 | is no longer widely used for sending out patches; for that purpose, the |
---|
471 | context format (*note Context Format::.) and the unified format (*note |
---|
472 | Unified Format::.) are superior. Normal format is the default for |
---|
473 | compatibility with older versions of `diff' and the Posix standard. |
---|
474 | |
---|
475 | * Menu: |
---|
476 | |
---|
477 | * Detailed Normal:: A detailed description of normal output format. |
---|
478 | * Example Normal:: Sample output in the normal format. |
---|
479 | |
---|
480 | |
---|
481 | File: diff.info, Node: Detailed Normal, Next: Example Normal, Up: Normal |
---|
482 | |
---|
483 | Detailed Description of Normal Format |
---|
484 | ------------------------------------- |
---|
485 | |
---|
486 | The normal output format consists of one or more hunks of |
---|
487 | differences; each hunk shows one area where the files differ. Normal |
---|
488 | format hunks look like this: |
---|
489 | |
---|
490 | CHANGE-COMMAND |
---|
491 | < FROM-FILE-LINE |
---|
492 | < FROM-FILE-LINE... |
---|
493 | --- |
---|
494 | > TO-FILE-LINE |
---|
495 | > TO-FILE-LINE... |
---|
496 | |
---|
497 | There are three types of change commands. Each consists of a line |
---|
498 | number or comma-separated range of lines in the first file, a single |
---|
499 | character indicating the kind of change to make, and a line number or |
---|
500 | comma-separated range of lines in the second file. All line numbers are |
---|
501 | the original line numbers in each file. The types of change commands |
---|
502 | are: |
---|
503 | |
---|
504 | `LaR' |
---|
505 | Add the lines in range R of the second file after line L of the |
---|
506 | first file. For example, `8a12,15' means append lines 12-15 of |
---|
507 | file 2 after line 8 of file 1; or, if changing file 2 into file 1, |
---|
508 | delete lines 12-15 of file 2. |
---|
509 | |
---|
510 | `FcT' |
---|
511 | Replace the lines in range F of the first file with lines in range |
---|
512 | T of the second file. This is like a combined add and delete, but |
---|
513 | more compact. For example, `5,7c8,10' means change lines 5-7 of |
---|
514 | file 1 to read as lines 8-10 of file 2; or, if changing file 2 into |
---|
515 | file 1, change lines 8-10 of file 2 to read as lines 5-7 of file 1. |
---|
516 | |
---|
517 | `RdL' |
---|
518 | Delete the lines in range R from the first file; line L is where |
---|
519 | they would have appeared in the second file had they not been |
---|
520 | deleted. For example, `5,7d3' means delete lines 5-7 of file 1; |
---|
521 | or, if changing file 2 into file 1, append lines 5-7 of file 1 |
---|
522 | after line 3 of file 2. |
---|
523 | |
---|
524 | |
---|
525 | File: diff.info, Node: Example Normal, Prev: Detailed Normal, Up: Normal |
---|
526 | |
---|
527 | An Example of Normal Format |
---|
528 | --------------------------- |
---|
529 | |
---|
530 | Here is the output of the command `diff lao tzu' (*note Sample diff |
---|
531 | Input::., for the complete contents of the two files). Notice that it |
---|
532 | shows only the lines that are different between the two files. |
---|
533 | |
---|
534 | 1,2d0 |
---|
535 | < The Way that can be told of is not the eternal Way; |
---|
536 | < The name that can be named is not the eternal name. |
---|
537 | 4c2,3 |
---|
538 | < The Named is the mother of all things. |
---|
539 | --- |
---|
540 | > The named is the mother of all things. |
---|
541 | > |
---|
542 | 11a11,13 |
---|
543 | > They both may be called deep and profound. |
---|
544 | > Deeper and more profound, |
---|
545 | > The door of all subtleties! |
---|
546 | |
---|
547 | |
---|
548 | File: diff.info, Node: Context, Next: Side by Side, Prev: Normal, Up: Output Formats |
---|
549 | |
---|
550 | Showing Differences in Their Context |
---|
551 | ==================================== |
---|
552 | |
---|
553 | Usually, when you are looking at the differences between files, you |
---|
554 | will also want to see the parts of the files near the lines that |
---|
555 | differ, to help you understand exactly what has changed. These nearby |
---|
556 | parts of the files are called the "context". |
---|
557 | |
---|
558 | GNU `diff' provides two output formats that show context around the |
---|
559 | differing lines: "context format" and "unified format". It can |
---|
560 | optionally show in which function or section of the file the differing |
---|
561 | lines are found. |
---|
562 | |
---|
563 | If you are distributing new versions of files to other people in the |
---|
564 | form of `diff' output, you should use one of the output formats that |
---|
565 | show context so that they can apply the diffs even if they have made |
---|
566 | small changes of their own to the files. `patch' can apply the diffs |
---|
567 | in this case by searching in the files for the lines of context around |
---|
568 | the differing lines; if those lines are actually a few lines away from |
---|
569 | where the diff says they are, `patch' can adjust the line numbers |
---|
570 | accordingly and still apply the diff correctly. *Note Imperfect::, for |
---|
571 | more information on using `patch' to apply imperfect diffs. |
---|
572 | |
---|
573 | * Menu: |
---|
574 | |
---|
575 | * Context Format:: An output format that shows surrounding lines. |
---|
576 | * Unified Format:: A more compact output format that shows context. |
---|
577 | * Sections:: Showing which sections of the files differences are in. |
---|
578 | * Alternate Names:: Showing alternate file names in context headers. |
---|
579 | |
---|
580 | |
---|
581 | File: diff.info, Node: Context Format, Next: Unified Format, Up: Context |
---|
582 | |
---|
583 | Context Format |
---|
584 | -------------- |
---|
585 | |
---|
586 | The context output format shows several lines of context around the |
---|
587 | lines that differ. It is the standard format for distributing updates |
---|
588 | to source code. |
---|
589 | |
---|
590 | To select this output format, use the `-C LINES', |
---|
591 | `--context[=LINES]', or `-c' option. The argument LINES that some of |
---|
592 | these options take is the number of lines of context to show. If you |
---|
593 | do not specify LINES, it defaults to three. For proper operation, |
---|
594 | `patch' typically needs at least two lines of context. |
---|
595 | |
---|
596 | * Menu: |
---|
597 | |
---|
598 | * Detailed Context:: A detailed description of the context output format. |
---|
599 | * Example Context:: Sample output in context format. |
---|
600 | * Less Context:: Another sample with less context. |
---|
601 | |
---|
602 | |
---|
603 | File: diff.info, Node: Detailed Context, Next: Example Context, Up: Context Format |
---|
604 | |
---|
605 | Detailed Description of Context Format |
---|
606 | ...................................... |
---|
607 | |
---|
608 | The context output format starts with a two-line header, which looks |
---|
609 | like this: |
---|
610 | |
---|
611 | *** FROM-FILE FROM-FILE-MODIFICATION-TIME |
---|
612 | --- TO-FILE TO-FILE-MODIFICATION TIME |
---|
613 | |
---|
614 | You can change the header's content with the `-L LABEL' or |
---|
615 | `--label=LABEL' option; see *Note Alternate Names::. |
---|
616 | |
---|
617 | Next come one or more hunks of differences; each hunk shows one area |
---|
618 | where the files differ. Context format hunks look like this: |
---|
619 | |
---|
620 | *************** |
---|
621 | *** FROM-FILE-LINE-RANGE **** |
---|
622 | FROM-FILE-LINE |
---|
623 | FROM-FILE-LINE... |
---|
624 | --- TO-FILE-LINE-RANGE ---- |
---|
625 | TO-FILE-LINE |
---|
626 | TO-FILE-LINE... |
---|
627 | |
---|
628 | The lines of context around the lines that differ start with two |
---|
629 | space characters. The lines that differ between the two files start |
---|
630 | with one of the following indicator characters, followed by a space |
---|
631 | character: |
---|
632 | |
---|
633 | `!' |
---|
634 | A line that is part of a group of one or more lines that changed |
---|
635 | between the two files. There is a corresponding group of lines |
---|
636 | marked with `!' in the part of this hunk for the other file. |
---|
637 | |
---|
638 | `+' |
---|
639 | An "inserted" line in the second file that corresponds to nothing |
---|
640 | in the first file. |
---|
641 | |
---|
642 | `-' |
---|
643 | A "deleted" line in the first file that corresponds to nothing in |
---|
644 | the second file. |
---|
645 | |
---|
646 | If all of the changes in a hunk are insertions, the lines of |
---|
647 | FROM-FILE are omitted. If all of the changes are deletions, the lines |
---|
648 | of TO-FILE are omitted. |
---|
649 | |
---|
650 | |
---|
651 | File: diff.info, Node: Example Context, Next: Less Context, Prev: Detailed Context, Up: Context Format |
---|
652 | |
---|
653 | An Example of Context Format |
---|
654 | ............................ |
---|
655 | |
---|
656 | Here is the output of `diff -c lao tzu' (*note Sample diff Input::., |
---|
657 | for the complete contents of the two files). Notice that up to three |
---|
658 | lines that are not different are shown around each line that is |
---|
659 | different; they are the context lines. Also notice that the first two |
---|
660 | hunks have run together, because their contents overlap. |
---|
661 | |
---|
662 | *** lao Sat Jan 26 23:30:39 1991 |
---|
663 | --- tzu Sat Jan 26 23:30:50 1991 |
---|
664 | *************** |
---|
665 | *** 1,7 **** |
---|
666 | - The Way that can be told of is not the eternal Way; |
---|
667 | - The name that can be named is not the eternal name. |
---|
668 | The Nameless is the origin of Heaven and Earth; |
---|
669 | ! The Named is the mother of all things. |
---|
670 | Therefore let there always be non-being, |
---|
671 | so we may see their subtlety, |
---|
672 | And let there always be being, |
---|
673 | --- 1,6 ---- |
---|
674 | The Nameless is the origin of Heaven and Earth; |
---|
675 | ! The named is the mother of all things. |
---|
676 | ! |
---|
677 | Therefore let there always be non-being, |
---|
678 | so we may see their subtlety, |
---|
679 | And let there always be being, |
---|
680 | *************** |
---|
681 | *** 9,11 **** |
---|
682 | --- 8,13 ---- |
---|
683 | The two are the same, |
---|
684 | But after they are produced, |
---|
685 | they have different names. |
---|
686 | + They both may be called deep and profound. |
---|
687 | + Deeper and more profound, |
---|
688 | + The door of all subtleties! |
---|
689 | |
---|
690 | |
---|
691 | File: diff.info, Node: Less Context, Prev: Example Context, Up: Context Format |
---|
692 | |
---|
693 | An Example of Context Format with Less Context |
---|
694 | .............................................. |
---|
695 | |
---|
696 | Here is the output of `diff --context=1 lao tzu' (*note Sample diff |
---|
697 | Input::., for the complete contents of the two files). Notice that at |
---|
698 | most one context line is reported here. |
---|
699 | |
---|
700 | *** lao Sat Jan 26 23:30:39 1991 |
---|
701 | --- tzu Sat Jan 26 23:30:50 1991 |
---|
702 | *************** |
---|
703 | *** 1,5 **** |
---|
704 | - The Way that can be told of is not the eternal Way; |
---|
705 | - The name that can be named is not the eternal name. |
---|
706 | The Nameless is the origin of Heaven and Earth; |
---|
707 | ! The Named is the mother of all things. |
---|
708 | Therefore let there always be non-being, |
---|
709 | --- 1,4 ---- |
---|
710 | The Nameless is the origin of Heaven and Earth; |
---|
711 | ! The named is the mother of all things. |
---|
712 | ! |
---|
713 | Therefore let there always be non-being, |
---|
714 | *************** |
---|
715 | *** 11 **** |
---|
716 | --- 10,13 ---- |
---|
717 | they have different names. |
---|
718 | + They both may be called deep and profound. |
---|
719 | + Deeper and more profound, |
---|
720 | + The door of all subtleties! |
---|
721 | |
---|
722 | |
---|
723 | File: diff.info, Node: Unified Format, Next: Sections, Prev: Context Format, Up: Context |
---|
724 | |
---|
725 | Unified Format |
---|
726 | -------------- |
---|
727 | |
---|
728 | The unified output format is a variation on the context format that |
---|
729 | is more compact because it omits redundant context lines. To select |
---|
730 | this output format, use the `-U LINES', `--unified[=LINES]', or `-u' |
---|
731 | option. The argument LINES is the number of lines of context to show. |
---|
732 | When it is not given, it defaults to three. |
---|
733 | |
---|
734 | At present, only GNU `diff' can produce this format and only GNU |
---|
735 | `patch' can automatically apply diffs in this format. For proper |
---|
736 | operation, `patch' typically needs at least two lines of context. |
---|
737 | |
---|
738 | * Menu: |
---|
739 | |
---|
740 | * Detailed Unified:: A detailed description of unified format. |
---|
741 | * Example Unified:: Sample output in unified format. |
---|
742 | |
---|
743 | |
---|
744 | File: diff.info, Node: Detailed Unified, Next: Example Unified, Up: Unified Format |
---|
745 | |
---|
746 | Detailed Description of Unified Format |
---|
747 | ...................................... |
---|
748 | |
---|
749 | The unified output format starts with a two-line header, which looks |
---|
750 | like this: |
---|
751 | |
---|
752 | --- FROM-FILE FROM-FILE-MODIFICATION-TIME |
---|
753 | +++ TO-FILE TO-FILE-MODIFICATION-TIME |
---|
754 | |
---|
755 | You can change the header's content with the `-L LABEL' or |
---|
756 | `--label=LABEL' option; see *Note Alternate Names::. |
---|
757 | |
---|
758 | Next come one or more hunks of differences; each hunk shows one area |
---|
759 | where the files differ. Unified format hunks look like this: |
---|
760 | |
---|
761 | @@ FROM-FILE-RANGE TO-FILE-RANGE @@ |
---|
762 | LINE-FROM-EITHER-FILE |
---|
763 | LINE-FROM-EITHER-FILE... |
---|
764 | |
---|
765 | The lines common to both files begin with a space character. The |
---|
766 | lines that actually differ between the two files have one of the |
---|
767 | following indicator characters in the left column: |
---|
768 | |
---|
769 | `+' |
---|
770 | A line was added here to the first file. |
---|
771 | |
---|
772 | `-' |
---|
773 | A line was removed here from the first file. |
---|
774 | |
---|
775 | |
---|
776 | File: diff.info, Node: Example Unified, Prev: Detailed Unified, Up: Unified Format |
---|
777 | |
---|
778 | An Example of Unified Format |
---|
779 | ............................ |
---|
780 | |
---|
781 | Here is the output of the command `diff -u lao tzu' (*note Sample |
---|
782 | diff Input::., for the complete contents of the two files): |
---|
783 | |
---|
784 | --- lao Sat Jan 26 23:30:39 1991 |
---|
785 | +++ tzu Sat Jan 26 23:30:50 1991 |
---|
786 | @@ -1,7 +1,6 @@ |
---|
787 | -The Way that can be told of is not the eternal Way; |
---|
788 | -The name that can be named is not the eternal name. |
---|
789 | The Nameless is the origin of Heaven and Earth; |
---|
790 | -The Named is the mother of all things. |
---|
791 | +The named is the mother of all things. |
---|
792 | + |
---|
793 | Therefore let there always be non-being, |
---|
794 | so we may see their subtlety, |
---|
795 | And let there always be being, |
---|
796 | @@ -9,3 +8,6 @@ |
---|
797 | The two are the same, |
---|
798 | But after they are produced, |
---|
799 | they have different names. |
---|
800 | +They both may be called deep and profound. |
---|
801 | +Deeper and more profound, |
---|
802 | +The door of all subtleties! |
---|
803 | |
---|
804 | |
---|
805 | File: diff.info, Node: Sections, Next: Alternate Names, Prev: Unified Format, Up: Context |
---|
806 | |
---|
807 | Showing Which Sections Differences Are in |
---|
808 | ----------------------------------------- |
---|
809 | |
---|
810 | Sometimes you might want to know which part of the files each change |
---|
811 | falls in. If the files are source code, this could mean which function |
---|
812 | was changed. If the files are documents, it could mean which chapter or |
---|
813 | appendix was changed. GNU `diff' can show this by displaying the |
---|
814 | nearest section heading line that precedes the differing lines. Which |
---|
815 | lines are "section headings" is determined by a regular expression. |
---|
816 | |
---|
817 | * Menu: |
---|
818 | |
---|
819 | * Specified Headings:: Showing headings that match regular expressions. |
---|
820 | * C Function Headings:: Showing headings of C functions. |
---|
821 | |
---|
822 | |
---|
823 | File: diff.info, Node: Specified Headings, Next: C Function Headings, Up: Sections |
---|
824 | |
---|
825 | Showing Lines That Match Regular Expressions |
---|
826 | ............................................ |
---|
827 | |
---|
828 | To show in which sections differences occur for files that are not |
---|
829 | source code for C or similar languages, use the `-F REGEXP' or |
---|
830 | `--show-function-line=REGEXP' option. `diff' considers lines that |
---|
831 | match the argument REGEXP to be the beginning of a section of the file. |
---|
832 | Here are suggested regular expressions for some common languages: |
---|
833 | |
---|
834 | `^[A-Za-z_]' |
---|
835 | C, C++, Prolog |
---|
836 | |
---|
837 | `^(' |
---|
838 | Lisp |
---|
839 | |
---|
840 | `^@\(chapter\|appendix\|unnumbered\|chapheading\)' |
---|
841 | Texinfo |
---|
842 | |
---|
843 | This option does not automatically select an output format; in order |
---|
844 | to use it, you must select the context format (*note Context Format::.) |
---|
845 | or unified format (*note Unified Format::.). In other output formats it |
---|
846 | has no effect. |
---|
847 | |
---|
848 | The `-F' and `--show-function-line' options find the nearest |
---|
849 | unchanged line that precedes each hunk of differences and matches the |
---|
850 | given regular expression. Then they add that line to the end of the |
---|
851 | line of asterisks in the context format, or to the `@@' line in unified |
---|
852 | format. If no matching line exists, they leave the output for that |
---|
853 | hunk unchanged. If that line is more than 40 characters long, they |
---|
854 | output only the first 40 characters. You can specify more than one |
---|
855 | regular expression for such lines; `diff' tries to match each line |
---|
856 | against each regular expression, starting with the last one given. This |
---|
857 | means that you can use `-p' and `-F' together, if you wish. |
---|
858 | |
---|
859 | |
---|
860 | File: diff.info, Node: C Function Headings, Prev: Specified Headings, Up: Sections |
---|
861 | |
---|
862 | Showing C Function Headings |
---|
863 | ........................... |
---|
864 | |
---|
865 | To show in which functions differences occur for C and similar |
---|
866 | languages, you can use the `-p' or `--show-c-function' option. This |
---|
867 | option automatically defaults to the context output format (*note |
---|
868 | Context Format::.), with the default number of lines of context. You |
---|
869 | can override that number with `-C LINES' elsewhere in the command line. |
---|
870 | You can override both the format and the number with `-U LINES' |
---|
871 | elsewhere in the command line. |
---|
872 | |
---|
873 | The `-p' and `--show-c-function' options are equivalent to |
---|
874 | `-F'^[_a-zA-Z$]'' if the unified format is specified, otherwise `-c |
---|
875 | -F'^[_a-zA-Z$]'' (*note Specified Headings::.). GNU `diff' provides |
---|
876 | them for the sake of convenience. |
---|
877 | |
---|
878 | |
---|
879 | File: diff.info, Node: Alternate Names, Prev: Sections, Up: Context |
---|
880 | |
---|
881 | Showing Alternate File Names |
---|
882 | ---------------------------- |
---|
883 | |
---|
884 | If you are comparing two files that have meaningless or uninformative |
---|
885 | names, you might want `diff' to show alternate names in the header of |
---|
886 | the context and unified output formats. To do this, use the `-L LABEL' |
---|
887 | or `--label=LABEL' option. The first time you give this option, its |
---|
888 | argument replaces the name and date of the first file in the header; |
---|
889 | the second time, its argument replaces the name and date of the second |
---|
890 | file. If you give this option more than twice, `diff' reports an |
---|
891 | error. The `-L' option does not affect the file names in the `pr' |
---|
892 | header when the `-l' or `--paginate' option is used (*note |
---|
893 | Pagination::.). |
---|
894 | |
---|
895 | Here are the first two lines of the output from `diff -C2 -Loriginal |
---|
896 | -Lmodified lao tzu': |
---|
897 | |
---|
898 | *** original |
---|
899 | --- modified |
---|
900 | |
---|
901 | |
---|
902 | File: diff.info, Node: Side by Side, Next: Scripts, Prev: Context, Up: Output Formats |
---|
903 | |
---|
904 | Showing Differences Side by Side |
---|
905 | ================================ |
---|
906 | |
---|
907 | `diff' can produce a side by side difference listing of two files. |
---|
908 | The files are listed in two columns with a gutter between them. The |
---|
909 | gutter contains one of the following markers: |
---|
910 | |
---|
911 | white space |
---|
912 | The corresponding lines are in common. That is, either the lines |
---|
913 | are identical, or the difference is ignored because of one of the |
---|
914 | `--ignore' options (*note White Space::.). |
---|
915 | |
---|
916 | `|' |
---|
917 | The corresponding lines differ, and they are either both complete |
---|
918 | or both incomplete. |
---|
919 | |
---|
920 | `<' |
---|
921 | The files differ and only the first file contains the line. |
---|
922 | |
---|
923 | `>' |
---|
924 | The files differ and only the second file contains the line. |
---|
925 | |
---|
926 | `(' |
---|
927 | Only the first file contains the line, but the difference is |
---|
928 | ignored. |
---|
929 | |
---|
930 | `)' |
---|
931 | Only the second file contains the line, but the difference is |
---|
932 | ignored. |
---|
933 | |
---|
934 | `\' |
---|
935 | The corresponding lines differ, and only the first line is |
---|
936 | incomplete. |
---|
937 | |
---|
938 | `/' |
---|
939 | The corresponding lines differ, and only the second line is |
---|
940 | incomplete. |
---|
941 | |
---|
942 | Normally, an output line is incomplete if and only if the lines that |
---|
943 | it contains are incomplete; *Note Incomplete Lines::. However, when an |
---|
944 | output line represents two differing lines, one might be incomplete |
---|
945 | while the other is not. In this case, the output line is complete, but |
---|
946 | its the gutter is marked `\' if the first line is incomplete, `/' if |
---|
947 | the second line is. |
---|
948 | |
---|
949 | Side by side format is sometimes easiest to read, but it has |
---|
950 | limitations. It generates much wider output than usual, and truncates |
---|
951 | lines that are too long to fit. Also, it relies on lining up output |
---|
952 | more heavily than usual, so its output looks particularly bad if you |
---|
953 | use varying width fonts, nonstandard tab stops, or nonprinting |
---|
954 | characters. |
---|
955 | |
---|
956 | You can use the `sdiff' command to interactively merge side by side |
---|
957 | differences. *Note Interactive Merging::, for more information on |
---|
958 | merging files. |
---|
959 | |
---|
960 | * Menu: |
---|
961 | |
---|
962 | * Side by Side Format:: Controlling side by side output format. |
---|
963 | * Example Side by Side:: Sample side by side output. |
---|
964 | |
---|
965 | |
---|
966 | File: diff.info, Node: Side by Side Format, Next: Example Side by Side, Up: Side by Side |
---|
967 | |
---|
968 | Controlling Side by Side Format |
---|
969 | =============================== |
---|
970 | |
---|
971 | The `-y' or `--side-by-side' option selects side by side format. |
---|
972 | Because side by side output lines contain two input lines, they are |
---|
973 | wider than usual. They are normally 130 columns, which can fit onto a |
---|
974 | traditional printer line. You can set the length of output lines with |
---|
975 | the `-W COLUMNS' or `--width=COLUMNS' option. The output line is split |
---|
976 | into two halves of equal length, separated by a small gutter to mark |
---|
977 | differences; the right half is aligned to a tab stop so that tabs line |
---|
978 | up. Input lines that are too long to fit in half of an output line are |
---|
979 | truncated for output. |
---|
980 | |
---|
981 | The `--left-column' option prints only the left column of two common |
---|
982 | lines. The `--suppress-common-lines' option suppresses common lines |
---|
983 | entirely. |
---|
984 | |
---|
985 | |
---|
986 | File: diff.info, Node: Example Side by Side, Prev: Side by Side Format, Up: Side by Side |
---|
987 | |
---|
988 | An Example of Side by Side Format |
---|
989 | --------------------------------- |
---|
990 | |
---|
991 | Here is the output of the command `diff -y -W 72 lao tzu' (*note |
---|
992 | Sample diff Input::., for the complete contents of the two files). |
---|
993 | |
---|
994 | The Way that can be told of is n < |
---|
995 | The name that can be named is no < |
---|
996 | The Nameless is the origin of He The Nameless is the origin of He |
---|
997 | The Named is the mother of all t | The named is the mother of all t |
---|
998 | > |
---|
999 | Therefore let there always be no Therefore let there always be no |
---|
1000 | so we may see their subtlety, so we may see their subtlety, |
---|
1001 | And let there always be being, And let there always be being, |
---|
1002 | so we may see their outcome. so we may see their outcome. |
---|
1003 | The two are the same, The two are the same, |
---|
1004 | But after they are produced, But after they are produced, |
---|
1005 | they have different names. they have different names. |
---|
1006 | > They both may be called deep and |
---|
1007 | > Deeper and more profound, |
---|
1008 | > The door of all subtleties! |
---|
1009 | |
---|
1010 | |
---|
1011 | File: diff.info, Node: Scripts, Next: If-then-else, Prev: Side by Side, Up: Output Formats |
---|
1012 | |
---|
1013 | Making Edit Scripts |
---|
1014 | =================== |
---|
1015 | |
---|
1016 | Several output modes produce command scripts for editing FROM-FILE |
---|
1017 | to produce TO-FILE. |
---|
1018 | |
---|
1019 | * Menu: |
---|
1020 | |
---|
1021 | * ed Scripts:: Using `diff' to produce commands for `ed'. |
---|
1022 | * Forward ed:: Making forward `ed' scripts. |
---|
1023 | * RCS:: A special `diff' output format used by RCS. |
---|
1024 | |
---|
1025 | |
---|
1026 | File: diff.info, Node: ed Scripts, Next: Forward ed, Up: Scripts |
---|
1027 | |
---|
1028 | `ed' Scripts |
---|
1029 | ------------ |
---|
1030 | |
---|
1031 | `diff' can produce commands that direct the `ed' text editor to |
---|
1032 | change the first file into the second file. Long ago, this was the |
---|
1033 | only output mode that was suitable for editing one file into another |
---|
1034 | automatically; today, with `patch', it is almost obsolete. Use the |
---|
1035 | `-e' or `--ed' option to select this output format. |
---|
1036 | |
---|
1037 | Like the normal format (*note Normal::.), this output format does not |
---|
1038 | show any context; unlike the normal format, it does not include the |
---|
1039 | information necessary to apply the diff in reverse (to produce the first |
---|
1040 | file if all you have is the second file and the diff). |
---|
1041 | |
---|
1042 | If the file `d' contains the output of `diff -e old new', then the |
---|
1043 | command `(cat d && echo w) | ed - old' edits `old' to make it a copy of |
---|
1044 | `new'. More generally, if `d1', `d2', ..., `dN' contain the outputs of |
---|
1045 | `diff -e old new1', `diff -e new1 new2', ..., `diff -e newN-1 newN', |
---|
1046 | respectively, then the command `(cat d1 d2 ... dN && echo w) | ed - |
---|
1047 | old' edits `old' to make it a copy of `newN'. |
---|
1048 | |
---|
1049 | * Menu: |
---|
1050 | |
---|
1051 | * Detailed ed:: A detailed description of `ed' format. |
---|
1052 | * Example ed:: A sample `ed' script. |
---|
1053 | |
---|
1054 | |
---|
1055 | File: diff.info, Node: Detailed ed, Next: Example ed, Up: ed Scripts |
---|
1056 | |
---|
1057 | Detailed Description of `ed' Format |
---|
1058 | ................................... |
---|
1059 | |
---|
1060 | The `ed' output format consists of one or more hunks of differences. |
---|
1061 | The changes closest to the ends of the files come first so that |
---|
1062 | commands that change the number of lines do not affect how `ed' |
---|
1063 | interprets line numbers in succeeding commands. `ed' format hunks look |
---|
1064 | like this: |
---|
1065 | |
---|
1066 | CHANGE-COMMAND |
---|
1067 | TO-FILE-LINE |
---|
1068 | TO-FILE-LINE... |
---|
1069 | . |
---|
1070 | |
---|
1071 | Because `ed' uses a single period on a line to indicate the end of |
---|
1072 | input, GNU `diff' protects lines of changes that contain a single |
---|
1073 | period on a line by writing two periods instead, then writing a |
---|
1074 | subsequent `ed' command to change the two periods into one. The `ed' |
---|
1075 | format cannot represent an incomplete line, so if the second file ends |
---|
1076 | in a changed incomplete line, `diff' reports an error and then pretends |
---|
1077 | that a newline was appended. |
---|
1078 | |
---|
1079 | There are three types of change commands. Each consists of a line |
---|
1080 | number or comma-separated range of lines in the first file and a single |
---|
1081 | character indicating the kind of change to make. All line numbers are |
---|
1082 | the original line numbers in the file. The types of change commands |
---|
1083 | are: |
---|
1084 | |
---|
1085 | `La' |
---|
1086 | Add text from the second file after line L in the first file. For |
---|
1087 | example, `8a' means to add the following lines after line 8 of file |
---|
1088 | 1. |
---|
1089 | |
---|
1090 | `Rc' |
---|
1091 | Replace the lines in range R in the first file with the following |
---|
1092 | lines. Like a combined add and delete, but more compact. For |
---|
1093 | example, `5,7c' means change lines 5-7 of file 1 to read as the |
---|
1094 | text file 2. |
---|
1095 | |
---|
1096 | `Rd' |
---|
1097 | Delete the lines in range R from the first file. For example, |
---|
1098 | `5,7d' means delete lines 5-7 of file 1. |
---|
1099 | |
---|
1100 | |
---|
1101 | File: diff.info, Node: Example ed, Prev: Detailed ed, Up: ed Scripts |
---|
1102 | |
---|
1103 | Example `ed' Script |
---|
1104 | ................... |
---|
1105 | |
---|
1106 | Here is the output of `diff -e lao tzu' (*note Sample diff Input::., |
---|
1107 | for the complete contents of the two files): |
---|
1108 | |
---|
1109 | 11a |
---|
1110 | They both may be called deep and profound. |
---|
1111 | Deeper and more profound, |
---|
1112 | The door of all subtleties! |
---|
1113 | . |
---|
1114 | 4c |
---|
1115 | The named is the mother of all things. |
---|
1116 | |
---|
1117 | . |
---|
1118 | 1,2d |
---|
1119 | |
---|
1120 | |
---|
1121 | File: diff.info, Node: Forward ed, Next: RCS, Prev: ed Scripts, Up: Scripts |
---|
1122 | |
---|
1123 | Forward `ed' Scripts |
---|
1124 | -------------------- |
---|
1125 | |
---|
1126 | `diff' can produce output that is like an `ed' script, but with |
---|
1127 | hunks in forward (front to back) order. The format of the commands is |
---|
1128 | also changed slightly: command characters precede the lines they |
---|
1129 | modify, spaces separate line numbers in ranges, and no attempt is made |
---|
1130 | to disambiguate hunk lines consisting of a single period. Like `ed' |
---|
1131 | format, forward `ed' format cannot represent incomplete lines. |
---|
1132 | |
---|
1133 | Forward `ed' format is not very useful, because neither `ed' nor |
---|
1134 | `patch' can apply diffs in this format. It exists mainly for |
---|
1135 | compatibility with older versions of `diff'. Use the `-f' or |
---|
1136 | `--forward-ed' option to select it. |
---|
1137 | |
---|
1138 | |
---|
1139 | File: diff.info, Node: RCS, Prev: Forward ed, Up: Scripts |
---|
1140 | |
---|
1141 | RCS Scripts |
---|
1142 | ----------- |
---|
1143 | |
---|
1144 | The RCS output format is designed specifically for use by the |
---|
1145 | Revision Control System, which is a set of free programs used for |
---|
1146 | organizing different versions and systems of files. Use the `-n' or |
---|
1147 | `--rcs' option to select this output format. It is like the forward |
---|
1148 | `ed' format (*note Forward ed::.), but it can represent arbitrary |
---|
1149 | changes to the contents of a file because it avoids the forward `ed' |
---|
1150 | format's problems with lines consisting of a single period and with |
---|
1151 | incomplete lines. Instead of ending text sections with a line |
---|
1152 | consisting of a single period, each command specifies the number of |
---|
1153 | lines it affects; a combination of the `a' and `d' commands are used |
---|
1154 | instead of `c'. Also, if the second file ends in a changed incomplete |
---|
1155 | line, then the output also ends in an incomplete line. |
---|
1156 | |
---|
1157 | Here is the output of `diff -n lao tzu' (*note Sample diff Input::., |
---|
1158 | for the complete contents of the two files): |
---|
1159 | |
---|
1160 | d1 2 |
---|
1161 | d4 1 |
---|
1162 | a4 2 |
---|
1163 | The named is the mother of all things. |
---|
1164 | |
---|
1165 | a11 3 |
---|
1166 | They both may be called deep and profound. |
---|
1167 | Deeper and more profound, |
---|
1168 | The door of all subtleties! |
---|
1169 | |
---|
1170 | |
---|
1171 | File: diff.info, Node: If-then-else, Prev: Scripts, Up: Output Formats |
---|
1172 | |
---|
1173 | Merging Files with If-then-else |
---|
1174 | =============================== |
---|
1175 | |
---|
1176 | You can use `diff' to merge two files of C source code. The output |
---|
1177 | of `diff' in this format contains all the lines of both files. Lines |
---|
1178 | common to both files are output just once; the differing parts are |
---|
1179 | separated by the C preprocessor directives `#ifdef NAME' or `#ifndef |
---|
1180 | NAME', `#else', and `#endif'. When compiling the output, you select |
---|
1181 | which version to use by either defining or leaving undefined the macro |
---|
1182 | NAME. |
---|
1183 | |
---|
1184 | To merge two files, use `diff' with the `-D NAME' or `--ifdef=NAME' |
---|
1185 | option. The argument NAME is the C preprocessor identifier to use in |
---|
1186 | the `#ifdef' and `#ifndef' directives. |
---|
1187 | |
---|
1188 | For example, if you change an instance of `wait (&s)' to `waitpid |
---|
1189 | (-1, &s, 0)' and then merge the old and new files with the |
---|
1190 | `--ifdef=HAVE_WAITPID' option, then the affected part of your code |
---|
1191 | might look like this: |
---|
1192 | |
---|
1193 | do { |
---|
1194 | #ifndef HAVE_WAITPID |
---|
1195 | if ((w = wait (&s)) < 0 && errno != EINTR) |
---|
1196 | #else /* HAVE_WAITPID */ |
---|
1197 | if ((w = waitpid (-1, &s, 0)) < 0 && errno != EINTR) |
---|
1198 | #endif /* HAVE_WAITPID */ |
---|
1199 | return w; |
---|
1200 | } while (w != child); |
---|
1201 | |
---|
1202 | You can specify formats for languages other than C by using line |
---|
1203 | group formats and line formats, as described in the next sections. |
---|
1204 | |
---|
1205 | * Menu: |
---|
1206 | |
---|
1207 | * Line Group Formats:: Formats for general if-then-else line groups. |
---|
1208 | * Line Formats:: Formats for each line in a line group. |
---|
1209 | * Detailed If-then-else:: A detailed description of if-then-else format. |
---|
1210 | * Example If-then-else:: Sample if-then-else format output. |
---|
1211 | |
---|