diff --git a/scripts/library.sh b/scripts/library.sh index 2683da0d0e..3472b47735 100755 --- a/scripts/library.sh +++ b/scripts/library.sh @@ -408,6 +408,27 @@ function is_protected_gcr() { [[ -n $1 && "$1" =~ "^gcr.io/knative-(releases|nightly)/?$" ]] } +# Remove symlinks in a path that are broken or lead outside the repo. +# Parameters: $1 - path name, e.g. vendor +function remove_broken_symlinks() { + for link in $(find $1 -type l); do + # Remove broken symlinks + if [[ ! -e ${link} ]]; then + unlink ${link} + continue + fi + # Get canonical path to target, remove if outside the repo + local target="$(ls -l ${link})" + target="${target##* -> }" + [[ ${target} == /* ]] || target="./${target}" + target="$(cd `dirname ${link}` && cd ${target%/*} && echo $PWD/${target##*/})" + if [[ ${target} != *github.com/knative/* ]]; then + unlink ${link} + continue + fi + done +} + # Returns the canonical path of a filesystem object. # Parameters: $1 - path to return in canonical form # $2 - base dir for relative links; optional, defaults to current