Wednesday, December 10, 2008

Searching Through Code

I commonly need to search through a code base to find a definition of some constant or function that I have come across.  I don't have a Visual Studio Project setup, so I can't just right click on a symbol and have it show me the definition.

For about six months I had resorted to using 'find' and 'grep' for this task.  This approach had a some pleasure in it.  I was grinding out regular expressions maybe 12 times a day and I love regular expressions.

Just recently I found a tool called 'ack'.  This tool is great because it packages up the find ... -exec  grep command nicely and gives me nice looking, well formatted results.  I'm still missing something though.  One example is to be able to return the names of functions that use the constant or call the function that I'm interested in.

Friday, December 5, 2008

Piping find to find

So let's say you've got a bunch of files from a big source repository with changes, backed up in a single level directory. These files were from many different folders and different hierarchies in the trunk. You didn't check them in because your work wasn't finished, but now you want to get back to work so you need to know where to put them in the trunk.

You could do use find to locate each file:

find /path/to/trunk -name fileA
find /path/to/trunk -name fileB


But you have all your files in a folder and you want to find all of them at once. This sounds like a job for xargs:

xargs find /path/to/trunk -name '{}'

But we have to pipe in the names of the files from our flat directory:

find /path/to/backup -type f | xargs find /path/to/trunk -name

Last problem is that find will produce full paths, and we just want the file name. Here I'm assuming unix style path names //path/to/file for sed (also xargs needs to be told to use just 1 argument per invocation) :

find /path/to/backup -type f -print0 | sed 's/\/\/.*\///g' | xargs -0 -n 1 find /path/to/trunk -name

The type option tells find to only find files vs. directories. The print0 and 0 options are advocated by xargs. Null terminated arguments are guaranteed to work whereas newline terminated arguments are not.