Ignoring whitespace changes with svn diff

When using Subversion, a recent successor to the ubiquitous CVS revision control system, one may notice certain niceties of CVS are lost.

In particular, the -w flag on cvs diff.

cvs diff will display changes in the local file. Without the -r flag to specify which version is being compared, the default is to compare the local file to the currently tagged version in the repository, or the main branch HEAD if the file is not tagged.

cvs diff -w will display all non-whitespace changes. White space changes include adding or removing tabs or spaces, or changing spaces to tabs. New lines are still included in the diff.

Subversion doesn't have this shortcut. To ignore whitespace changes, you must use an external diff program with subversion:

svn diff filename --diff-cmd `which diff` -x "-w"

You can specify a different diff, such as xdiff, xxdiff, as a value to the --diff-cmd argument, but be sure to include the full path. If you want to pass more values to your diff command, include them in the -x argument value, which is passed along directly to the diff command:

svn diff -r123:126 filename --diff-cmd /path/to/diff -x "-u -w"

If you want, you can also create an alias for this. In tcsh:

alias svndiff 'svn diff --diff-cmd `which diff` -x "-w" \!*'

Finding PHP included files from several places

Depending on how the include path is constructed for a PHP application, a file may or may not exist in the include path. A developer may, for example, wish to check several locations for a file to be included, and stop processing a PHP file if the file cannot be found.

We came across this issue when trying to include the Smarty Template base class, Smarty.class.php

The file could be in one of two locations, depending on the include path, which was not standardized across our development and test machines. The file was either directly in the path, or in the Smarty directory, which was in the path.

So, we used this line:

@(include_once('Smarty.class.php')) OR require_once('Smarty/Smarty.class.php');

The first include_once will look for the Smarty file on the current include path. include_once and include do not stop the script execution on failure, whereas require will.

The @ in front of the include_once will suppress errors, including those output to screen. So, if the first include fails, the require_once comes into effect. The script will try to locate the Smarty file in the Smarty subdirectory.

Now, if the file is not found in either location, we do want the script to stop execution, so we use require_once for the second include.

We could also chain several include statements together, if needed, suppressing errors as needed until success or complete failure.

Syndicate content