Как исключить определенные каталоги / файлы из поиска git grep

115
задан Sklivvz 6 October 2012 в 15:39
поделиться

1 ответ

На примере @kynan в качестве основы я создал этот скрипт и поместил его в свой путь (~/bin/) как gg. Он использует git grep, но избегает некоторых указанных типов файлов.

В нашем репо много изображений, поэтому я исключил файлы изображений, и это сокращает время поиска до 1/3, если я ищу весь репо. Но скрипт может быть легко изменен, чтобы исключить другие типы файлов или geleralpatterns.

#!/bin/bash                                                                    
#                                                                              
# Wrapper of git-grep that excludes certain filetypes.                         
# NOTE: The filetypes to exclude is hardcoded 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 fileendings 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                                                                      

Примечание 1

В соответствии с этим должна быть возможность назвать вещь git-gg и иметь возможность называть ее как обычный git команда вроде:

$ git gg searchstring

Но я не могу заставить это работать. Я создал скрипт в своем ~/bin/ и сделал символическую ссылку git-gg в /usr/lib/git-core/.

Примечание 2

Команду нельзя превратить в обычный sh псевдоним псевдонима, поскольку она будет вызываться в корне репо. И это не то, чего я хочу!

2
ответ дан Community 6 October 2012 в 15:39
поделиться
Другие вопросы по тегам:

Похожие вопросы: