Real Ultimate Programming

The Home for People Who Like to Flip Out and Write Code

How to Enable Git Grep -P on OS X Using Homebrew

If you’re using Homebrew to manage software on your OS X Lion box, and using the version of git it provides, you may have noticed that you can’t use the -P flag to git grep to enable Perl-compatible regular expressions (PCRE). It turns out the default formula doesn’t enable that option when building git. Rather than have to override this every single time you upgrade git, it’s far easier to add this line to your .bashrc (or .zshrc):

export USE_LIBPCRE=yes

After you do that, you also need to brew install pcre, because if you don’t you’ll get a build error when you next attempt to build git. It will look something like:

==> Upgrading git
==> Downloading
File already downloaded in /Users/hank/Library/Caches/Homebrew
==> make prefix=/usr/local/Cellar/git/ install
    * new build flags or prefix
    * new link flags
./ > common-cmds.h+ && mv common-cmds.h+ common-cmds.h
/usr/bin/llvm-gcc -o hex.o -c -MF ./.depend/hex.o.d -MMD -MP  -O3 -w -pipe  -march=core2 -msse4.1 -I. -DUSE_LIBPCRE -DUSE_ST_TIMESPEC  -DSHA1_HEADER='<openssl/sha.h>'  -DNO_MEMMEM  hex.c
/usr/bin/llvm-gcc -o ident.o -c -MF ./.depend/ident.o.d -MMD -MP  -O3 -w -pipe  -march=core2 -msse4.1 -I. -DUSE_LIBPCRE -DUSE_ST_TIMESPEC  -DSHA1_HEADER='<openssl/sha.h>'  -DNO_MEMMEM  ident.c
/usr/bin/llvm-gcc -o kwset.o -c -MF ./.depend/kwset.o.d -MMD -MP  -O3 -w -pipe  -march=core2 -msse4.1 -I. -DUSE_LIBPCRE -DUSE_ST_TIMESPEC  -DSHA1_HEADER='<openssl/sha.h>'  -DNO_MEMMEM  kwset.c
/usr/bin/llvm-gcc -o levenshtein.o -c -MF ./.depend/levenshtein.o.d -MMD -MP  -O3 -w -pipe  -march=core2 -msse4.1 -I. -DUSE_LIBPCRE -DUSE_ST_TIMESPEC  -DSHA1_HEADER='<openssl/sha.h>'  -DNO_MEMMEM  levenshtein.c
/usr/bin/llvm-gcc -o list-objects.o -c -MF ./.depend/list-objects.o.d -MMD -MP  -O3 -w -pipe  -march=core2 -msse4.1 -I. -DUSE_LIBPCRE -DUSE_ST_TIMESPEC  -DSHA1_HEADER='<openssl/sha.h>'  -DNO_MEMMEM  list-objects.c
/usr/bin/llvm-gcc -o ll-merge.o -c -MF ./.depend/ll-merge.o.d -MMD -MP  -O3 -w -pipe  -march=core2 -msse4.1 -I. -DUSE_LIBPCRE -DUSE_ST_TIMESPEC  -DSHA1_HEADER='<openssl/sha.h>'  -DNO_MEMMEM  ll-merge.c
In file included from revision.h:5,
                 from list-objects.c:8:
grep.h:43: error: expected specifier-qualifier-list before ‘pcre’
make: *** [list-objects.o] Error 1
make: *** Waiting for unfinished jobs....
==> Exit Status: 2
==> Environment
HEAD: 730f9b7fbc30a74348223fa78e1ede295dc73340
HOMEBREW_CELLAR: /usr/local/Cellar
Hardware: dual-core 64-bit penryn
OS X: 10.7.2
Kernel Architecture: x86_64
Ruby: 1.8.7-249
/usr/bin/ruby => /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby
Xcode: 4.2.1
GCC-4.0: N/A
GCC-4.2: N/A
LLVM: build 2336
Clang: 3.0 build 211
MacPorts or Fink? false
X11 installed? true
==> Build Flags
CC: /usr/bin/llvm-gcc => /usr/llvm-gcc-4.2/bin/llvm-gcc-4.2
CXX: /usr/bin/llvm-g++ => /usr/llvm-gcc-4.2/bin/llvm-g++-4.2
LD: /usr/bin/llvm-gcc => /usr/llvm-gcc-4.2/bin/llvm-gcc-4.2
CFLAGS: -O3 -w -pipe  -march=core2 -msse4.1
CXXFLAGS: -O3 -w -pipe  -march=core2 -msse4.1

Error: Failed executing: make prefix=/usr/local/Cellar/git/ install
These existing issues may help you:
Otherwise, please report the bug:

It’s that little line grep.h:43: error: expected specifier-qualifier-list before ‘pcre’ that clues you in that you have a PCRE problem.

Back to flipping out…