From Fluids Wiki
Jump to: navigation, search

Source code comparison

The scenario of wanting to know what has changed between two versions of source code often arises. For example, you are running a specific version of a model (SPINS, MITgcm, IGW, etc.) while the model is being improved by the developers. After a few weeks or months pass, a lot of differences accumulate between the latest greatest version and the version you are actually running. It's not always easy to determine what has changed, particularly if a revision control system is not being used.

Command line diff can be helpful, but, reading the output of diff is awkward and the context is usually omitted. ("Sure, line 75 has been replaced, but what subroutine is that in?"). The script is designed to alleviate this problem.

Installing a graphical diff program

You will need a graphical diff program, meld is a good one. For ubuntu linux, this may be installed by the command

$ sudo apt-get install meld


You also need the shell script which should be placed in your $HOME/bin. You also need to make the script executable, use the command: chmod 755 You may need to edit line 6 (GUIDIFF=) to reflect which graphical diff program you have installed. The code for follows:

# A directory compare script for comparing source codes
# Usage: dir1 dir2
if [ $# -lt 2 ]; then
  echo "quit!"
  exit 1;
OI="Only in" 
if [ -d $1 ] && [ -d $2 ]; then
  $DIFF -r -b --brief --exclude=.svn \
  $1 $2 \
  | sed s/"Files"/$GUIDIFF/g | sed s/" and "/" "/g \
  | sed s/differ//g | grep -v "$OI"


Now that you have both fldiff and you can compare directories by

$ dir1 dir2

You will be presented with a graphical window that shows a file from dir1 on the left and from dir2 on the right, with differences highlighted. Press Ctrl+Q to close that window and proceed to the next file. The result is that with one command, you can rapidly scroll through all files that have changed and see the differences.

This script is useful for source code, LaTeX code, MATLAB code, or anything else that lives in a large number of flat text files.