Subversion integration with Guiffy Diff and Merge Tool

Software Engineering

Subversion (svn) is a popular version control system, and has been written to provide several improvements over CVS versioning system. It also provides possibilities to integrate with third party applications for common tasks like diff and merge of working copies of files with those in version control. So for example, to do diff of a file, one would enter:

svn diff <filename>
OR
svn diff <path>

Which would provide usual GNU diff format output of the said <filename> or all the files in <path> which have been locally modified.

Also, if one does svn update to get the latest updates from repository, the command would be:
svn update <file>
OR
svn update <path>

In case a file has been locally modified on a previous checked out version, svn will do an automatic merge of the file and if the merge goes through successfully, it would report it with a 'G' flag, else in case of conflicted merge, it would report it with a 'C' flag. The user has to then edit the conflicted file and check for all places where conflicts were found.

We now explore how above tasks can be made simpler by integrating svn with Guiffy, a graphical tool for doing side by side diff, as well as three way merges.

Subversion provides command line option to plug in third party programs for doing diff or merge. So, to do see diff of modified files in Guiffy, the commmand would be:

svn diff --diff-cmd <path to Guify diff script> <path or file name>

Following script can be plugged in above which converts the parameters given by svn to those expected by Guiffy.

#!/bin/bash

# Configure path to guiffy here
GUIFFY=~/guiffy

# Subversion provides the paths we need as the sixth and seventh parameters. The left file will actually get expanded to the local pristine file stored by svn. The right file is the locally modified copy. Interchange these if you want to view it the other way in guiffy.
LEFT=${6}
RIGHT=${7}

# Call guiffy to open its diff window to view diff and review changes
$GUIFFY $LEFT $RIGHT

# Now, svn expects to be returned an errorcode of 0 if no differences were detected, 1 if some were. Any other errorcode will be treated as fatal.
# Note: whether differences were found, or not found -- guiffy always returns 0, so we will return 0 to svn and end at that. Returning anything apart from 0 or 1 tells svn something fatal happened so it will stop invoking guiffy after first file.
exit 0

When svn diff is used in above fashion for multiple files, it will open up Guiffy for each file one by one.

Above script has been based on example found at svn documentation page below:
http://svnbook.red-bean.com/en/1.2/svn.advanced.externaldifftools.html

To use Guiffy to do visual merge and 3 way merge while doing update, following command and script can be used as plugin to svn.

svn update --diff3-cmd <path to Guify merge script> <path or file name>

#!/bin/bash

# Configure path to guiffy here
GUIFFY=~/guiffy

# Subversion provides the paths we need as the ninth, tenth, and eleventh parameters.
MINE=${9}
OLDER=${10}
YOURS=${11}

sureMergeFile=${MINE}.guiffysmerge
# Call the guiffy suremerge command between locally modified, checked in version, and parent of checked in version. Save merge output into sureMergeFile. Do save merge output in guiffy before exiting; to do that:click on menu Merge->Save Merged File, Press F2, OR click on Save button on toolbar with an 'M'.
$GUIFFY -s $MINE $YOURS $OLDER $sureMergeFile

# not tested:Call the guiffy merge command between locally modified and checked in version
#$GUIFFY -m $MINE $YOURS

# After performing the merge, this script needs to print the contents of the merged file to stdout. Note: If this is not done, svn will overwrite the file as empty since nothing was sent to it. This output to stdout is picked by svn which writes it onto locally modified file.
cat $sureMergeFile
# clean up the temp sure merge file now that svn has saved our merge changes onto the locally modified copy
rm $sureMergeFile

# Now, svn expects to be returned an errorcode of 0 if no differences were detected, 1 if some were. Any other errorcode will be treated as fatal and svn will stop at that file. We simply return 0 since guiffy always returns that.
exit 0

The above script is somewhat more complex and demands that the script must output the merge file to stdout, because the same output is read by svn and written into the working copy.

In case of multiple files which are updated, the Guiffy tool will get invoked for the conflict case only, and when user reviews and merges the changes, and then saves and exits, the control shifts back to svn. Svn command will then proceed to the next files in its list.

In summary, the integration of Guiffy tool, using above scripts, makes for lot more productivity, and makes code reviews and updates from repository less error prone.