list-unused-images.sh 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #!/usr/bin/env bash
  2. set -uo pipefail
  3. IFS=$'\n\t'
  4. check_git_history=false
  5. rm -f tmp-unused-images
  6. rm -f tmp-unused-images-history
  7. # List images which might be unused.
  8. # Exceptions are ignored, and for .svg files we also look for potential .png
  9. # files with the same base name, as they might be sources.
  10. files=$(find -name "_build" -prune -o \( -iname "*.png" -o -iname "*.webp" -o -iname "*.jpg" -o -iname "*.svg" -o -iname "*.gif" \) -print | sort)
  11. for path in $files; do
  12. file=$(basename "$path")
  13. if [[ "$path" == *./img/* ]]; then
  14. continue
  15. fi
  16. ext=${file##*.}
  17. base=${file%.*}
  18. found=$(rg -l ":: .*[ /]$file")
  19. if [ -z "$found" ] && [ "$ext" == "svg" ]; then
  20. # May be source file.
  21. found=$(rg -l ":: .*[ /]$base.png")
  22. fi
  23. if [ -z "$found" ]; then
  24. echo "$path" >> tmp-unused-images
  25. fi
  26. done
  27. echo "Wrote list of unused images to: tmp-unused-images"
  28. if [ "$check_git_history" = true ]; then
  29. for file in $(cat tmp-unused-images); do
  30. echo "File: $file"
  31. git log --diff-filter=A --follow "$file"
  32. echo
  33. done > tmp-unused-images-history
  34. echo "Wrote list of unused images in Git history to: tmp-unused-images-history"
  35. fi