With the example by kynan as the base, I made this script and put it in my path (~/bin/) as gg. It does use git grep, but it avoids some specified filetypes.
In our repository, it’s a lot of images, so I have excluded the imagefiles, and this takes the search time down to 1/3 if I search the whole repository. But the script could easily be modified to exclude other filetypes or geleralpatterns.
#!/bin/bash # # Wrapper of git-grep that excludes certain filetypes. # NOTE: The filetypes to exclude is hard coded for my specific needs. # # The basic setup of this script is from here: # https://stackoverflow.com/a/14226610/42580 # But there is issues with giving extra path information to the script # therefor I crafted the while-thing that moves path-parts to the other side # of the '--'. # Declare the filetypes to ignore here EXCLUDES="png xcf jpg jpeg pdf ps" # Rebuild the list of file endings to a good regexp EXCLUDES=`echo $EXCLUDES | sed -e 's/ /\\\|/g' -e 's/.*/\\\.\\\(\0\\\)/'` # Store the stuff that is moved from the arguments. moved= # If git-grep returns this "fatal..." then move the last element of the # arg-list to the list of files to search. err="fatal: bad flag '--' used after filename" while [ "$err" = "fatal: bad flag '--' used after filename" ]; do { err=$(git grep "$@" -- `git ls-files $moved | grep -iv "$EXCLUDES"` \ 2>&1 1>&3-) } 3>&1 # The rest of the code in this loop is here to move the last argument in # the arglist to a separate list $moved. I had issues with whitespace in # the search-string, so this is loosely based on: # http://www.linuxjournal.com/content/bash-preserving-whitespace-using-set-and-eval x=1 items= for i in "$@"; do if [ $x -lt $# ]; then items="$items \"$i\"" else moved="$i $moved" fi x=$(($x+1)) done eval set -- $items done # Show the error if there was any echo $err
Note 1
According to this, it should be possible to name the thing git-gg and be able to call it as a regular Git command like:
git gg searchstring
But I can not get this working. I created the script in my ~/bin/ folder and made the git-gg symbolic link in folder /usr/lib/git-core/.
Note 2
The command can not be made into an regular sh git-alias since it will then be invoked at the root of the repository. And that is not what I want!