code

gitgrep 검색에서 특정 디렉터리/파일을 제외하는 방법

starcafe 2023. 6. 22. 22:04
반응형

gitgrep 검색에서 특정 디렉터리/파일을 제외하는 방법

다음을 사용하여 Git 저장소를 검색할 때 특정 경로/디렉토리/파일을 제외하는 방법이 있습니까?git grep그것과 비슷한 것.--exclude보통의 옵션grep명령?

사용해야 합니다.git grep왜냐하면 사용하기 때문입니다.grep직접 실행 속도가 너무 느립니다.

"마법의 단어" 1.9.0을 입력합니다.exclude에 추가되었습니다.pathspec그래서 만약 당신이 검색하고 싶다면.foobar일치하는 파일을 제외한 모든 파일에서*.java할 수 있는 일:

git grep foobar -- ':(exclude)*.java'

또는 를 사용합니다.!제외의 "짧은 형식":

git grep foobar -- ':!*.java'

제외를 사용하는 경우 v2.12까지 igit 버전을 입력합니다.pathspec하나 이상의 "스캐너"가 있어야 합니다.pathspec위의 예에서 추가할 내용./*(현재 디렉토리 아래의 모든 것을 포함) 다음 어딘가에.--뿐만 아니라.Git v2.13에서 이 제한이 해제되었고,git grep foobar -- ':!*.java'가 없는 작업./*.

모든 "마법의 단어"에 대한 좋은 참조가 있습니다.pathspecgit-scm.com 에서 (또는 그냥.git help glossary).

업데이트: git >= 1.9의 경우 제외 패턴에 대한 네이티브 지원이 있습니다. 단 한 명의 답변만 참조하십시오.

이는 거꾸로 보일 수 있지만 제외 패턴과 일치하지 않는 파일 목록을 전달할 수 있습니다.git grep다음과 같이:

git grep <pattern> -- `git ls-files | grep -v <exclude-pattern>`

grep -v일치하지 않는 모든 경로를 반환합니다.<exclude-pattern>참고:git ls-files또한 사용합니다.--exclude매개 변수입니다. 그러나 이 매개 변수는 추적되지 않은 파일에만 적용됩니다.

불가능하지만, 최근에 논의가 되었습니다.링크에서 제안된 해결 방법:

넣을 수 있습니다.*.dll.gitignore 파일로 이동합니다.git grep --exclude-standard.

EDIT는 1.9.0을 얻었기 때문에 아무도 대답하지 않습니다.

저장소에 속성 파일을 만들어 파일 또는 디렉터리를 이진으로 표시할 수 있습니다.

$ cat .git/info/attributes 
directory/to/ignore/*.* binary
directory/to/ignore/*/*.* binary
another_directory/to/also/ignore/*.* binary

이진 파일의 일치 항목은 포함 줄 없이 나열됩니다.

$ git grep "bar"
Binary file directory/to/ignore/filename matches
other_directory/other_filename:      foo << bar - bazz[:whatnot]

@kynan의 예를 바탕으로 저는 이 스크립트를 만들어 제 길에 놓았습니다.~/bin/) 로서gg를 사용합니다.git grep그러나 일부 지정된 파일 형식은 사용하지 않습니다.

저희 레포에는 이미지가 많아서 이미지 파일을 제외했고, 전체 레포를 검색하면 검색 시간이 1/3로 줄어듭니다.그러나 스크립트는 다른 파일 형식이나 젤러럴 패턴을 제외하도록 쉽게 수정할 수 있습니다.

#!/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/그리고 a를 만들었습니다.git-gg입니다./usr/lib/git-core/.

노트 2

명령어로 만들 수 .sh그러면 repo의 루트에서 호출되므로 git-discovery.그리고 그것은 내가 원하는 것이 아닙니다!

언급URL : https://stackoverflow.com/questions/10423143/how-to-exclude-certain-directories-files-from-git-grep-search

반응형