[Top] [Prev] [Next] [Index] [TOC]

Chapter 16

Diff: A Tool for Displaying Program Differences

Diff is the Toolsuite component for displaying differences between files. It displays two files side by side with line by line differences highlighted in color. A green background is used for lines that are changed, a blue background for lines that are added, and a red background for lines that are deleted. Two customized bit-mapped scroll bars are used to display a thumbnail sketch of the differences between the two files. They are useful for quickly locating changes, deletions, and additions. Diff also reports the number of changes, additions, and deletions that have to be made to bring two files into agreement.


16.1 Background

One of the major problems in using the diff (UNIX) command to find the differences between two files is that the user has to expend a tremendous amount of effort understanding its output before discovering how these two files differ. Diff, on the other hand, has a graphical user interface which displays two files side by side with line by line differences highlighted in color. A green background is used for lines that are changed, a blue background for lines that are added, and a red background for lines that are deleted. Two customized bit-mapped scroll bars are used to display a thumbnail sketch of the differences between the two files. They are useful for quickly locating changes, deletions, and additions.

The scrolling can be either synchronized or independent. In the synchronized mode, when one file is scrolled up or down, the other file scrolls to ensure that changes are displayed side by side in the two text windows. On the other hand, in the non-synchronized mode, only the file in the text window under the mouse is scrolled while the other file remains unmoved.

Diff is valuable for merging versions of a program produced by two programmers into a single, reconciled version. It can also quickly identify differences between a new and an old version of a document.

16.2 A Tutorial

The use of Diff is most easily understood by an example. In this chapter we use the same wordcount program as used in the previous chapters to illustrate the basic features of Diff. To copy these files, create a new directory, cd to it, and copy the contents of the directory in which the tutorial files are installed into the new directory. For the illustrations in this chapter, we will use two files: main.c and main_err.c.

Invoke Diff by:

	prompt:> xdiff main_err.c main.c
Figure 16-1 shows the resulting display. If no file names are specified after the Diff command, the text windows will be empty. There are two other ways to specify which two files to compare. One way is to enter the first file name in the rectangle next to ``File name 1'' and the second file name in the rectangle next to ``File name 2''. The other way is to click on the ``File1'' or ``File2'' button to open a file dialog window, then select a file from the file list and click on the ``Load'' button with the left mouse button or hit the return key.
Figure 16-1 The display window of Diff

Clicking with any mouse button (left, middle or right) at any spot in the scroll bars brings the corresponding region of the file into the text window. You can use the arrows at the top or the bottom of the scroll bar to scroll up or down the source file a few lines at a time. You can also drag the mouse up or down the scroll bar with the left mouse button pressed to rapidly scroll up or down the file. In addition, Diff provides keyboard shortcuts. It may be necessary to click with the left mouse button to activate the desired text area. Once focused properly, the Up or Down arrow key will move the text up or down one line at a time. Any time the mouse is over the horizontal bar, you may scroll horizontally in that file, either with the Left and Right arrow keys or using the left mouse button. The PageUp and PageDown keys scroll up and down the source file one page at a time, respectively. The Home key scrolls to the beginning of the file, whereas the End key goes to the end of the file.

The ``Sync'' button in the top button bar works as a toggle. If it is on, clicking turns it off; if it is off, clicking turns it on. This button is used to determine whether file1 and file2 should be moved together in a synchronized way. By default, they are synchronized. As displayed in Figure 16-1, we see that a block of two lines in file1 is replaced by a block of six lines in file2. Both blocks are highlighted in green. Since the ratio of the number of lines changed in these two files is 1:3, Diff ensures that when every line in the block that is highlighted in green in file1 is scrolled up or down, three such lines in file2 are also scrolled up or down, or vice versa. We also see that a block of new code has been added to file2. This block is highlighted in blue. Diff ensures that file1 stays unmoved, that is, not scrolled up or down, while the blue highlighted block in file2 is scanned to examine the new code. The same technique also applies to the deletion code in file1 which is highlighted in red.

If you want to view these two files independently, i.e., without moving them together, you can click on the ``Sync'' button to turn it off. Once it is clicked, the button is dimmed to indicate Diff is in the non-synchronized mode. In this mode, the text window under the mouse has the focus. You can move the file in that window up or down by clicking a mouse button on the corresponding scroll bar or pressing shortcut keys: UpArrow, DownArrow, LeftArrow, RightArrow, PageUp, PageDown, Home or End.

You can switch back to the synchronized mode by clicking on the ``Sync'' button again. Depending on which text window has the last focus, the file in the other text window will be automatically scrolled to resume the synchronization. For example, if the left text window has the last focus, when the Sync mode is turned on, file2 in the right text window will then be automatically scrolled up or down to make itself in synchronization with file1. In the same way, if the right text window has the last focus, the left window will be adjusted.

The number of changes, additions, and deletions that have to be made to bring the two files into agreement appears in the middle bar in the rectangles next to the ``# of changes'', ``# of additions'', and ``# of deletions'' labels. In our case as displayed in Figure 16-1, there are 3 changes, 1 addition, and 1 deletion from main_err.c to main.c.

To see a description of the colors used in highlighting, select the ``changes'', ``additions'', or ``deletions'' button in the top button bar. A highlighted description window will pop up as in Figure 16-2.

Figure 16-2 The highlight description window of Diff
To get on-line help, click on the ``Help'' button in the top button bar.

To quit Diff, click on the ``Close'' button in the top button bar. This will close the Diff text window.



[Top] [Prev] [Next] [Index] [TOC]