diff --git a/scripts/find-unused-images.sh b/scripts/find-unused-images.sh index ffaa2f64e2f..700f4fe74dc 100755 --- a/scripts/find-unused-images.sh +++ b/scripts/find-unused-images.sh @@ -14,25 +14,35 @@ git fetch --all # List all the version branches branches="master" -version_branches=$(git branch --list "version-[0-9]-[0-9]") -branches+=" $version_branches" +version_branches=$(git branch -a | grep -E 'version-[0-9]+(-[0-9]+)*$') +for version_branch in $version_branches; do + # Remove leading spaces and remote prefix (if any) + version_branch=$(echo $version_branch | sed 's/ *//;s/remotes\/origin\///') + + branches+=" $version_branch" +done + +echo "Detected branches to evaluate: $branches" for current_branch in $branches; do echo "Current branch: $current_branch" - git checkout origin/$current_branch + git checkout $current_branch find docs -type f -name "*.md" -exec grep -Hn "\.webp" {} + > docs_used_images.json find _partials -type f -name "*.mdx" -exec grep -Hn "\.webp" {} + > partials_used_images.json cat docs_used_images.json partials_used_images.json > used_images.json + echo "Created used_images.json" line_number=1 for img in $(cat all_images.json); do if grep -q $img used_images.json; then - sed -i .bak "${line_number}s|.*|${img},FOUND_USED|" all_images.json + sed -i "${line_number}s|.*|${img},FOUND_USED|" all_images.json fi ((line_number++)) done + echo "Finished processing branch $current_branch" + done # Remove all marked used files to make up the list @@ -40,7 +50,6 @@ sed '/FOUND_USED/d' all_images.json > unused_images.json # Clean up files rm all_images.json -rm all_images.json.bak rm docs_used_images.json rm partials_used_images.json rm used_images.json