5.4. Comparing File Revisions
As your project develops, you sometimes need to compare a current revision with an old one, or compare two older revisions with each other. This usually happens when several developers are working on the same file and you need to know which changes were made to a file since you last worked on it.
The cvs diff command compares two revisions of a file and displays the differences. It uses a version of the GNU diff program internal to CVS; this code is also used when revisions are merged during cvs update.
The diff command and the similar rdiff command are also useful for creating patches to be installed by the GNU patch program.
cvs diff has the following syntax:
cvs [cvs-options] diff [command_options] [filenames]
You can call cvs diff with filenames, directories, or module names. If you don't give a filename, the current working directory is the default.
Usually, you call cvs diff with at least one -r tag or -D date command option. If you invoke it with a single -r or -D parameter, CVS compares the current copy in the working directory with the version in the repository. For example, if you check out revision 1.6 of Makefile, edit the file, then run cvs diff -r 1.6 Makefile, diff displays the changes you made to Makefile since you checked it out.
If you invoke cvs diff with two -r or -D options, CVS compares the two revisions against each other. The command cvs diff -r 1.5 -r 1.7 Makefile displays the changes between revisions 1.5 and 1.7.
Example 5-14 shows a diff command that compares revisions 1.2 and 1.3 of the Makefile file, using the -c option to display a few lines of context around each change. The output starts with a header listing the filename, the repository copy of the file (listed as RCS file), the revisions to be retrieved, the diff options, and the number of times the revisions were committed.
Example 5-14. Using cvs diff
diff then shows the sections that were changed. The diff in Example 5-14 shows the changes that occur when keywords are expanded, so the contents of the lines should help you understand the diff.
The first change occurs in the headers to the Makefile, when the revision number and date are changed. These changes overwrite lines in the old revision, so the changed lines are denoted with an exclamation point (!) in the left column. The revision listed first in the parameter list is shown first, followed by the second revision.
The second change to the file is the new log message. This is shown only once, with plus marks (+) in the left column to show that the line was added.
Graphic clients tend to produce side-by-side diff displays, with color coding for the differences. Figure 5-3 shows the Cervisia diff display.
Figure 5-3. Graphic diff display
Chapter 10 provides the full list of symbols and meanings for cvs diff.
The cvs rdiff command is a variant of cvs diff that can be used to create a patch file that can be installed using the standard GNU patch command. cvs rdiff is useful for publishing updates of programs. It does not require a sandbox but does require file, directory, or module name parameters. If cvs rdiff is used outside a sandbox, the repository path must be given with the -d CVS option or the CVSROOT environment variable. If cvs rdiff is used in a sandbox that connects to a repository other than the one you want to work from, the repository path must be given with the -d CVS option.
cvs rdiff has the following syntax:
cvs [cvs-options] rdiff [command_options] filenames
The filenames parameter can be one or more directories, filenames, or module names. Output from cvs rdiff is directed to stdout. If you intend to use the output as a patch file, you can redirect it to a file using the > shell operator. Example 5-15 shows how to make a patch for handheld.c that upgrades the file from revision 1.1 to the most current revision.
Example 5-15. Making a patch file
cvs diff and cvs rdiff take two types of options. The first are standard options that determine which revisions of which files are checked and which keyword mode is used. The second set of options determines the output format and affects how CVS compares the files. Chapter 10 provides the full list of options to cvs diff and cvs rdiff.
One of the most useful cvs diff (and cvs rdiff) options is -y, also available as --side-by-side. This option displays the differences between the files by showing the relevant lines beside each other. Open your terminal window as wide as possible when using this option. Example 5-16 shows a side-by-side diff between the current revision of a file in the sandbox and the most recent revision in the repository.
Example 5-16. Displaying diff side-by-side