Skip to content

Instantly share code, notes, and snippets.

@eglaysher
Created February 1, 2010 23:59
Show Gist options
  • Save eglaysher/292200 to your computer and use it in GitHub Desktop.
Save eglaysher/292200 to your computer and use it in GitHub Desktop.
grepsource: Quick grepping when we have git-grep, find|xargs when we don't.
(defconst grepsource-filetypes
'("*.h" "*.hpp" "*.cpp" "*.c" "*.cc" "*.cpp" "*.inl" "*.grd" "*.idl" "*.m"
"*.mm" "*.py" "*.sh" "*.cfg" "*SConscript" "SConscript*" "*.scons"
"*.vcproj" "*.vsprops" "*.make" "*.gyp" "*.gypi")
"A list of filetype patterns that grepsource will use.")
(defun grepsource (cmd-args)
"Grep `default-directory' using git-grep for speed if we're in
a git repository and falling back to a big \"find | xargs grep\"
command if we aren't."
(interactive (list (read-from-minibuffer "Grep project for string: ")))
;; When we're in a git repository, use git grep so we don't have to
;; find-files.
(let ((quoted (replace-regexp-in-string "\"" "\\\\\"" cmd-args))
(grep-use-null-device nil))
(if (file-exists-p (concat default-directory "/.git"))
;; We can accelerate our grep using the local git stuff.
(grep (concat "git --no-pager grep -n -e \"" quoted "\" -- "
(mapconcat 'identity grepsource-filetypes " ")))
;; Fallback to using find.
(grep (concat "find . -path '*/.svn' -prune -o -name \""
(mapconcat 'identity grepsource-filetypes
"\" -or -name \"")
"\" | xargs grep -nH -e \"" quoted "\"")))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment