.gitignore에 없는 파일 찾기

.gitignore에 없는 파일 찾기

프로젝트의 파일을 표시하는 find 명령이 있습니다.

find . -type f -not -path './node_modules*' -a -not -path '*.git*' \
       -a -not -path './coverage*' -a -not -path './bower_components*' \
       -a -not -name '*~'

.gitignore의 파일이 표시되지 않도록 파일을 필터링하는 방법은 무엇입니까?

나는 다음을 사용하고 있다고 생각했습니다.

while read file; do
    grep $file .gitignore > /dev/null && echo $file;
done

하지만 .gitignore 파일은 glob 패턴을 가질 수 있습니다(파일이 .gitignore에 있는 경우 경로와도 작동하지 않음). glob이 있을 수 있는 패턴을 기반으로 파일을 필터링하려면 어떻게 해야 합니까?

답변1

git공급git-check-ignore파일이 제외되었는지 확인하세요 .gitignore.

따라서 다음을 사용할 수 있습니다.

find . -type f -not -path './node_modules*' \
       -a -not -path '*.git*'               \
       -a -not -path './coverage*'          \
       -a -not -path './bower_components*'  \
       -a -not -name '*~'                   \
       -exec sh -c '
         for f do
           git check-ignore -q "$f" ||
           printf '%s\n' "$f"
         done
       ' find-sh {} +

검사는 파일별로 수행되므로 막대한 비용을 지불하게 됩니다.

답변2

Git에서 추적하는 체크아웃 파일을 표시하려면 다음을 사용하세요.

$ git ls-files

이 명령에는 캐시된 파일, 추적되지 않은 파일, 수정된 파일, 무시된 파일 등과 같은 항목을 표시하는 다양한 옵션이 있습니다. 보다 git ls-files --help.

답변3

이를 수행하는 git 명령이 있습니다.

my_git_repo % git grep --line-number TODO                                                                                         
desktop/includes/controllers/user_applications.sh:126:  # TODO try running this without sudo
desktop/includes/controllers/web_tools.sh:52:   TODO: detail the actual steps here:
desktop/includes/controllers/web_tools.sh:57:   TODO: check if, at this point, the menurc file exists. i.e. it  was created

말씀하신 대로 대부분의 일반 grep 옵션을 사용하여 기본 grep을 수행하지만 파일 .git내의 파일이나 폴더는 검색하지 않습니다 .gitignore.
자세한 내용은 다음을 참조하세요.man git-grep

하위 모듈:

이 git 저장소에 다른 git 저장소가 있는 경우(하위 모듈에 있어야 함) 이 플래그를 사용하여 --recurse-submodules하위 모듈에서 검색 할 수도 있습니다.

답변4

bash glob이 실행될 배열을 사용할 수 있습니다.

다음과 같은 파일이 있습니다.

touch file1 file2 file3 some more file here

그리고 ignore이런 파일이 있어요

cat <<EOF >ignore
file*
here
EOF

사용

arr=($(cat ignore));declare -p arr

결과는 다음과 같습니다.

declare -a arr='([0]="file" [1]="file1" [2]="file2" [3]="file3" [4]="here")'

그런 다음 모든 기술을 사용하여 이 데이터를 처리할 수 있습니다.

나는 개인적으로 다음과 같은 것을 선호합니다.

awk 'NR==FNR{a[$1];next}(!($1 in a))'  <(printf '%s\n' "${arr[@]}") <(find . -type f -printf %f\\n)
#Output
some
more
ignore

관련 정보