Section 5.4. Comparing File Revisions

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

 bash-2.05a$ cvs diff -c -r 1.2 -r 1.3 Makefile Index: Makefile ============================= RCS file: /var/lib/cvs/wizzard/Makefile,v retrieving revision 1.2 retrieving revision 1.3 diff -c -r1.2 -r1.3 *** Makefile     1 Apr 2006 06:57:23 -0000     1.2 --- Makefile     12 Apr 2006 12:57:40 -0000    1.3 *************** *** 2,10 ****   # Makefile for the Wizzard project   # First created by J Vesperman, 1 Apr 2006   # ! # Current revision $Revision$   # On branch $Name$ (not expanded if this is the trunk) ! # Latest change by $Author$ on $Date$   #   # Initial declarations --- 2,10 ----   # Makefile for the Wizzard project   # First created by J Vesperman, 1 Apr 2002   # ! # Current revision $Revision$   # On branch $Name$ (not expanded if this is the trunk) ! # Latest change by $Author$ on $Date$   #   # Initial declarations *************** *** 34,39 **** --- 33,41 ----   #   # Log record for Makefile changes:   # $Log$   # Revision 1.3  2006/03/07 21:52:50  madd   # madd SC edits   #   # Revision 1.2  2006/02/27 21:47:43  madd   # madd R2 conversion edits   #   # Revision 1.1  2006/02/26 22:22:10  madd   # Initial revision   # + # Revision 1.3  2005/09/12 12:57:40  jenn + # Trying again with the wrappers.. + #   # Revision 1.2  2005/09/01 06:57:23  jenn   # Initial code in the Makefile.   # 

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.

In CVS 1.11.1 and later, you can also use cvs diff to generate files for the Unix and Linux program patch. The patch program is designed to update a text file from one version to another, automatically.

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

 bash-2.05a$ cvs rdiff -r 1.1 -r HEAD wizzard/src/handheld.c  > patchfile bash-2.05a$ 

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

 bash-2.05a$ cvs diff -y server.c Index: server.c ============================= RCS file: /var/lib/cvs/wizzard/src/server.c,v retrieving revision 1.1 diff --side-by-side -r1.1 server.c /*                            /*  * server.c                    * server.c  * Apr 13 2006                 * Apr 13 2006  * Developer: Jenn Vesperman   * Developer: Jenn Vesperman  *                             *  * server code file            * server code file  */                            */                              >                              > #include <iostream.h>                              >                              > int main (  ) {                              >   cout << "Hello World\n";                              >   return (0);                              > } 

Essential CVS
Essential CVS (Essentials)
ISBN: 0596527039
EAN: 2147483647
Year: 2006
Pages: 148

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: