Blog

Searching through Subversion history

Occasionally I need to search back through old versions of projects to find a piece of code I want to resurrect or just use as reference — I haven’t found any easy built-in way to do this, so I adapted this useful script to allow me to grep through all history from within a Subversion working directory, like:

$ svngrep SecItemCopyMatching *
LSAddAccountController.m @r7: SecItemCopyMatching((CFDictionaryRef)[NSDictionary dictionaryWithObjectsAndKeys:
LSAddAccountController.m @r4: SecItemCopyMatching((CFDictionaryRef)[NSDictionary dictionaryWithObjectsAndKeys:
LSAddAccountController.m @r2: SecItemCopyMatching((CFDictionaryRef)[NSDictionary dictionaryWithObjectsAndKeys:
LSAddAccountController.m @r1: SecItemCopyMatching((CFDictionaryRef)[NSDictionary dictionaryWithObjectsAndKeys:

Here’s the script:

#!/bin/sh
pattern=$1
shift
 
for file in $@; 
do
  svn log -q "$file" 2>/dev/null | perl -ne 'print "$1\n" if /^r(\d+)/' | 
  while read r 
  do
    match=`svn cat -r $r "$file" | grep "$pattern"`
    result=$?
    if [ $result -eq 0 ]
    then
      /bin/echo -n "$file @r$r: "
      /bin/echo $match;
    elif [ $result -ne 1 ]
    then
      exit 2
    fi
  done
done;
, . Bookmark the permalink. Both comments and trackbacks are currently closed.