Skip to content

Commit

Permalink
A helper funcion for removing broken symlinks (#376)
Browse files Browse the repository at this point in the history
* A helper funcion for removing broken symlinks

Two types of symlinks are considered broken:
1. Broken symlinks.
2. Symlinks pointing to a path outside of the source tree.

This function was impleneted in
  knative/serving#2842
and should be useful for all knative modules.

* Readd LF at end of file after merge
  • Loading branch information
erain authored and knative-prow-robot committed Mar 20, 2019
1 parent 91c8ce0 commit 7b67483
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions scripts/library.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 7b67483

Please sign in to comment.