Synopsis: diff [-lR] [-k kflag] [format_options] [[-r rev1 | -D date1] [-r rev2 | -D date2]] [files…]
Requires: working directory, repository.
Changes: nothing.
The diff command is used to compare different revisions of files. The default action is to compare your working files with the revisions they were based on, and report any differences that are found.
If any file names are given, only those files are compared. If any directories are given, all files under them will be compared.
The exit status for diff is different than for other cvs commands; for details CVS的退出状态.
These standard options are supported by diff (通用命令选项, for a complete description of them):
Use the most recent revision no later than date. See -r for how this affects the comparison.
Process keywords according to kflag. See 第 12 章.
Local; run only in current working directory.
Examine directories recursively. This option is on by default.
Compare with revision tag. Zero, one or two -r options can be present. With no -r option, the working file will be compared with the revision it was based on. With one -r, that revision will be compared to your current working file. With two -r options those two revisions will be compared (and your working file will not affect the outcome in any way).
One or both -r options can be replaced by a -D date option, described above.
The following options specify the format of the output. They have the same meaning as in GNU diff. Most options have two equivalent names, one of which is a single letter preceded by -, and the other of which is a long name preceded by -.
Show lines (an integer) lines of context. This option does not specify an output format by itself; it has no effect unless it is combined with -c or -u. This option is obsolete. For proper operation, patch typically needs at least two lines of context.
Treat all files as text and compare them line-by-line, even if they do not seem to be text.
Ignore trailing white space and consider all other sequences of one or more white space characters to be equivalent.
Ignore changes that just insert or delete blank lines.
Read and write data in binary mode.
Report only whether the files differ, not the details of the differences.
Use the context output format.
Use the context output format, showing lines (an integer) lines of context, or three if lines is not given. For proper operation, patch typically needs at least two lines of context.
Use format to output a line group containing differing lines from both files in if-then-else format. Line group formats.
Change the algorithm to perhaps find a smaller set of changes. This makes diff slower (sometimes much slower).
Make output that is a valid ed script.
Expand tabs to spaces in the output, to preserve the alignment of tabs in the input files.
Make output that looks vaguely like an ed script but has changes in the order they appear in the file.
In context and unified format, for each hunk of differences, show some of the last preceding line that matches regexp.
Make output that looks vaguely like an ed script but has changes in the order they appear in the file.
Use heuristics to speed handling of large files that have numerous scattered small changes.
Do not discard the last lines lines of the common prefix and the first lines lines of the common suffix.
Ignore changes in case; consider upper- and lower-case letters equivalent.
Ignore changes that just insert or delete lines that match regexp.
Make merged if-then-else output using name.
Ignore white space when comparing lines.
Ignore changes that just insert or delete blank lines.
Ignore changes in case; consider upper- and lower-case to be the same.
Ignore changes that just insert or delete lines that match regexp.
Ignore trailing white space and consider all other sequences of one or more white space characters to be equivalent.
Output a tab rather than a space before the text of a line in normal or context format. This causes the alignment of tabs in the line to look normal.
Use label instead of the file name in the context format and unified format headers.
Use label instead of the file name in the context format and unified format headers.
Print only the left column of two common lines in side by side format.
Use format to output all input lines in if-then-else format. Line formats.
Change the algorithm to perhaps find a smaller set of changes. This makes diff slower (sometimes much slower).
Output RCS-format diffs; like -f except that each command specifies the number of lines affected.
In directory comparison, if a file is found in only one directory, treat it as present but empty in the other directory.
Use format to output a group of lines taken from just the second file in if-then-else format. Line group formats.
Use format to output a line taken from just the second file in if-then-else format. Line formats.
Use format to output a group of lines taken from just the first file in if-then-else format. Line group formats.
Use format to output a line taken from just the first file in if-then-else format. Line formats.
Show which C function each change is in.
Output RCS-format diffs; like -f except that each command specifies the number of lines affected.
Report when two files are the same.
Show which C function each change is in.
In context and unified format, for each hunk of differences, show some of the last preceding line that matches regexp.
Use the side by side output format.
Use heuristics to speed handling of large files that have numerous scattered small changes.
Do not print common lines in side by side format.
Expand tabs to spaces in the output, to preserve the alignment of tabs in the input files.
Output a tab rather than a space before the text of a line in normal or context format. This causes the alignment of tabs in the line to look normal.
Treat all files as text and compare them line-by-line, even if they do not appear to be text.
Use the unified output format.
Use format to output a group of common lines taken from both files in if-then-else format. Line group formats.
Use format to output a line common to both files in if-then-else format. Line formats.
Use the unified output format, showing lines (an integer) lines of context, or three if lines is not given. For proper operation, patch typically needs at least two lines of context.
Ignore white space when comparing lines.
Use an output width of columns in side by side format.
Use the side by side output format.
Line group formats let you specify formats suitable for many applications that allow if-then-else input, including programming languages and text formatting languages. A line group format specifies the output format for a contiguous group of similar lines.
For example, the following command compares the TeX file myfile with the original version from the repository, and outputs a merged file in which old regions are surrounded by \begin{em}-\end{em} lines, and new regions are surrounded by \begin{bf}-\end{bf} lines.
cvs diff \
--old-group-format='\begin{em}
%<\end{em}
' \
--new-group-format='\begin{bf}
%>\end{bf}
' \
myfile |
The following command is equivalent to the above example, but it is a little more verbose, because it spells out the default line group formats.
cvs diff \
--old-group-format='\begin{em}
%<\end{em}
' \
--new-group-format='\begin{bf}
%>\end{bf}
' \
--unchanged-group-format='%=' \
--changed-group-format='\begin{em}
%<\end{em}
\begin{bf}
%>\end{bf}
' \
myfile |
Here is a more advanced example, which outputs a diff listing with headers containing line numbers in a "plain English" style.
cvs diff \ --unchanged-group-format='' \ --old-group-format='-------- %dn line%(n=1?:s) deleted at %df: %<' \ --new-group-format='-------- %dN line%(N=1?:s) added after %de: %>' \ --changed-group-format='-------- %dn line%(n=1?:s) changed at %df: %<-------- to: %>' \ myfile |
To specify a line group format, use one of the options listed below. You can specify up to four line group formats, one for each kind of line group. You should quote format, because it typically contains shell metacharacters.
These line groups are hunks containing only lines from the first file. The default old group format is the same as the changed group format if it is specified; otherwise it is a format that outputs the line group as-is.
These line groups are hunks containing only lines from the second file. The default new group format is same as the the changed group format if it is specified; otherwise it is a format that outputs the line group as-is.
These line groups are hunks containing lines from both files. The default changed group format is the concatenation of the old and new group formats.
These line groups contain lines common to both files. The default unchanged group format is a format that outputs the line group as-is.
In a line group format, ordinary characters represent themselves; conversion specifications start with % and have one of the following forms.
stands for the lines from the first file, including the trailing newline. Each line is formatted according to the old line format (Line formats).
stands for the lines from the second file, including the trailing newline. Each line is formatted according to the new line format.
stands for the lines common to both files, including the trailing newline. Each line is formatted according to the unchanged line format.
stands for %.
where C is a single character, stands for C. C may not be a backslash or an apostrophe. For example, %c':' stands for a colon, even inside the then-part of an if-then-else format, which a colon would normally terminate.
where O is a string of 1, 2, or 3 octal digits, stands for the character with octal code O. For example, %c'\0' stands for a null character.
where F is a printf conversion specification and n is one of the following letters, stands for n's value formatted with F.
The line number of the line just before the group in the old file.
The line number of the first line in the group in the old file; equals e + 1.
The line number of the last line in the group in the old file.
The line number of the line just after the group in the old file; equals l + 1.
The number of lines in the group in the old file; equals l - f + 1.
Likewise, for lines in the new file.
The printf conversion specification can be %d, %o, %x, or %X, specifying decimal, octal, lower case hexadecimal, or upper case hexadecimal output respectively. After the % the following options can appear in sequence: a - specifying left-justification; an integer specifying the minimum field width; and a period followed by an optional integer specifying the minimum number of digits. For example, %5dN prints the number of new lines in the group in a field of width 5 characters, using the printf format "%5d".
If A equals B then T else E. A and B are each either a decimal constant or a single letter interpreted as above. This format spec is equivalent to T if A's value equals B's; otherwise it is equivalent to E.
For example, %(N=0?no:%dN) line%(N=1?:s) is equivalent to no lines if N (the number of lines in the group in the the new file) is 0, to 1 line if N is 1, and to %dN lines otherwise.
Line formats control how each line taken from an input file is output as part of a line group in if-then-else format.
For example, the following command outputs text with a one-column change indicator to the left of the text. The first column of output is - for deleted lines, | for added lines, and a space for unchanged lines. The formats contain newline characters where newlines are desired on output.
cvs diff \ --old-line-format='-%l ' \ --new-line-format='|%l ' \ --unchanged-line-format=' %l ' \ myfile |
To specify a line format, use one of the following options. You should quote format, since it often contains shell metacharacters.
formats lines just from the first file.
formats lines just from the second file.
formats lines common to both files.
formats all lines; in effect, it sets all three above options simultaneously.
In a line format, ordinary characters represent themselves; conversion specifications start with % and have one of the following forms.
stands for the the contents of the line, not counting its trailing newline (if any). This format ignores whether the line is incomplete.
stands for the the contents of the line, including its trailing newline (if any). If a line is incomplete, this format preserves its incompleteness.
stands for %.
where C is a single character, stands for C. C may not be a backslash or an apostrophe. For example, %c':' stands for a colon.
where O is a string of 1, 2, or 3 octal digits, stands for the character with octal code O. For example, %c'\0' stands for a null character.
where F is a printf conversion specification, stands for the line number formatted with F. For example, %.5dn prints the line number using the printf format "%.5d". Line group formats, for more about printf conversion specifications.
The default line format is %l followed by a newline character.
If the input contains tab characters and it is important that they line up on output, you should ensure that %l or %L in a line format is just after a tab stop (e.g. by preceding %l or %L with a tab character), or you should use the -t or -expand-tabs option.
Taken together, the line and line group formats let you specify many different formats. For example, the following command uses a format similar to diff's normal format. You can tailor this command to get fine control over diff's output.
cvs diff \ --old-line-format='< %l ' \ --new-line-format='> %l ' \ --old-group-format='%df%(f=l?:,%dl)d%dE %<' \ --new-group-format='%dea%dF%(F=L?:,%dL) %>' \ --changed-group-format='%df%(f=l?:,%dl)c%dF%(F=L?:,%dL) %<--- %>' \ --unchanged-group-format='' \ myfile |
The following line produces a Unidiff (-u flag) between revision 1.14 and 1.19 of backend.c. Due to the -kk flag no keywords are substituted, so differences that only depend on keyword substitution are ignored.
$ cvs diff -kk -u -r 1.14 -r 1.19 backend.c |
Suppose the experimental branch EXPR1 was based on a set of files tagged RELEASE_1_0. To see what has happened on that branch, the following can be used:
$ cvs diff -r RELEASE_1_0 -r EXPR1 |
A command like this can be used to produce a context diff between two releases:
$ cvs diff -c -r RELEASE_1_0 -r RELEASE_1_1 > diffs |
If you are maintaining ChangeLogs, a command like the following just before you commit your changes may help you write the ChangeLog entry. All local modifications that have not yet been committed will be printed.
$ cvs diff -u | less |