July, 2011


26
Jul 11

softwhere: an alternative to grep for searching your source trees

At a previous workplace, we had a little script built around glimpse that indexed our source tree, making it quick to search for keywords. Someone cleverly called this script softwhere, and gave it a handy alias of sw.

When I started working on Mozilla, I wanted softwhere. And I thought to myself: “Hey, I run OS X now. Isn’t every file on my computer automatically indexed?” The answer, of course, is yes. And thus, softwhere 2.0 was born:

#!/bin/sh

files=`mdfind -onlyin /src/mozilla-central "$*"`

for file in $files; do
    grep -H "$*" $file
done

(You’ll need to replace /src/mozilla-central in the mdfind command above to wherever you store your primary source code repository.)

I’ve saved softwhere.sh in ~/bin, which is in my PATH. And I’ve added alias sw=softwhere.sh into my .bashrc. And thus:

joe@joe-laptop:~$ sw imgLoader::Init
/src/mozilla-central/modules/libpr0n/src/imgLoader.cpp.orig:nsresult imgLoader::InitCache()
/src/mozilla-central/modules/libpr0n/src/imgLoader.cpp.orig:nsresult imgLoader::Init()
joe@joe-laptop:~$