Using Grep Efficiently Inside SVN Repositories

I’ve had issues grepping for text inside SVN repositories. The main issue is that grepping inside SVN codebases will return binary and .svn results. I’ve run into this probably most frequently when using Mac OS X. For some reason the –exclude argument doesn’t work for me and –exclude-dir is not supported in a default Mac OS X installation ( at least on Lion ).

Here’s an example of output from grep that’s irrelevant and should be removed:

Binary file bin/classes/com/android/Class.class matches

So instead of using the –exclude argument, I’ve chosen the -v option or –invert-match

if test -z "$1"
          echo "1st argument must be set to your search term";
        grep -R $1 * | grep -v 'svn' | grep -v 'Binary*'

You can copy and paste this code and have it be globally accessible in the Mac OS X terminal by doing the following. If /usr/local/bin has not been created yet then issue this command:

NOTE: I'm using sudo to execute these commands as the relevant directories are owned by root.

# sudo mkdir -p /usr/local/bin

Otherwise create the shell script

# sudo vim /usr/local/bin/svngrep

Then cd to the directory and make sure the file is executable.

# cd /usr/local/bin/
# chmod 755 svngrep

Now open a new terminal window and cd to the area where you want to search in your SVN repository. From there call the script like so and pass your search argument as the first and only argument.

svngrep MyMethod

Additional search terms can be excluded by adding more | grep -v 'search-term' clauses.

Leave a Reply