如何在 bash/zsh 中重複使用 .gitignore 中的檔案模式?

如何在 bash/zsh 中重複使用 .gitignore 中的檔案模式?

這是該內容的內容.gitignore

cron.yaml
build
target
webview
*.pyc
*.sublime-workspace
.idea
*.rej
.coverage
app/tools/temp_*.py
app/tools/*/temp_*.py

我目前正在透過此腳本迭代本地資料夾中的文件:

find . -type f | grep -v -E "(.idea|.git)" | while read file
do
  # Do something with $file
done

我想進一步過濾這個$file變量,如果它與.gitignore.是否有任何現有實用程式或 bash 內建程式可以理解這些檔案模式?

答案1

您也許可以使用grep's -faka ( --file) 選項,透過過程替換來「正規化」某些模式。例如:

find . -type f | grep -Ev '(\.idea|\.git)' | 
    grep -v -f <(sed 's/\([.|]\)/\\\1/g; s/\?/./g ; s/\*/.*/g' .gitignore) | 
    while IFS= read -r file ; do 
      # Do something with "$file"
    done

相關內容