Mdiff: Difference between revisions

From Fluids Wiki
Jump to navigation Jump to search
No edit summary
 
(9 intermediate revisions by 2 users not shown)
Line 7: Line 7:
== Installing a graphical diff program ==
== Installing a graphical diff program ==


You will need a graphical diff program (xdiff, xxdiff, fldiff, etc) and fldiff is recommended. For ubuntu linux, this may be installed by the command
You will need a graphical diff program, meld is a good one. For ubuntu linux, this may be installed by the command


<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
$ sudo apt-get install fldiff
$ sudo apt-get install meld
</syntaxhighlight>
</syntaxhighlight>
If you don't have ubuntu, check your package manager for the fldiff package. Failing that, install it from source code from http://www.easysw.com/~mike/fldiff/index.html.


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


<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
#!/bin/bash
#!/bin/bash
# A directory compare script for comparing source codes
# A directory compare script for comparing source codes
# Usage:  mdiff.sh dir1 dir2 [S]
# Usage:  mdiff.sh dir1 dir2
# where specifying S will cause the script to show the "only in" entries
#
# Michael Dunphy 23 Nov 2005
 
DIFF=diff
DIFF=diff
GUIDIFF=fldiff
GUIDIFF=meld
 
if [ $# -lt 2 ]; then
if [ $# -lt 2 ]; then
   echo "quit!"
   echo "quit!"
   exit 1;
   exit 1;
fi
fi
 
if [ -z $3 ]; then
OI="Only in"  
  OI="Only in"  
else
if [ -d $1 ] && [ -d $2 ]; then
  OI="NOT_REMOVING_ONLY_IN_ENTRIES"
  $DIFF -r -b --brief --exclude=.svn \
fi
   $1 $2 \
 
d1=$1
d2=$2
if [ -d $d1 ] && [ -d $d2 ]; then
$HOME/bin/gdiff -r -b --brief \
  $DIFF -r -b --exclude=.svn --brief \
   $d1 $d2 \
   | sed s/"Files"/$GUIDIFF/g | sed s/" and "/" "/g \
   | sed s/"Files"/$GUIDIFF/g | sed s/" and "/" "/g \
   | sed s/differ//g | grep -v "$OI"
   | sed s/differ//g | grep -v "$OI"
Line 53: Line 42:
Now that you have both fldiff and mdiff.sh you can compare directories by
Now that you have both fldiff and mdiff.sh you can compare directories by
<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
$ mdiff dir1 dir2 | sh
$ mdiff.sh dir1 dir2
</syntaxhighlight>
</syntaxhighlight>


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.
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.


A screenshot of fldiff is shown on the author's website: http://www.easysw.com/~mike/fldiff/index.html#screenshot
This script is useful for source code, LaTeX code, MATLAB code, or anything else that lives in a large number of flat text files.

Latest revision as of 15:11, 17 May 2016

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 mdiff.sh 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

Installing mdiff.sh

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

#!/bin/bash
# A directory compare script for comparing source codes
# Usage:  mdiff.sh dir1 dir2
 
DIFF=diff
GUIDIFF=meld
 
if [ $# -lt 2 ]; then
  echo "quit!"
  exit 1;
fi
 
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"
fi

Usage

Now that you have both fldiff and mdiff.sh you can compare directories by

$ mdiff.sh 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.