Clean up PATH sanitization in rbenv-which

This commit is contained in:
Mislav Marohnić 2014-10-13 12:24:45 +02:00
parent e4cbf04592
commit 3ee395f9b5

View file

@ -15,6 +15,16 @@ if [ "$1" = "--complete" ]; then
exec rbenv shims --short
fi
remove_from_path() {
local path_to_remove="$1"
local path_before
local result=":$PATH:"
while [ "$path_before" != "$result" ]; do
path_before="$result"
result="${result//:$path_to_remove:/:}"
done
echo "${result%:}"
}
RBENV_COMMAND="$1"
@ -26,18 +36,8 @@ fi
RBENV_VERSION="${RBENV_VERSION:-$(rbenv-version-name)}"
if [ "$RBENV_VERSION" = "system" ]; then
# Remove shims from PATH. Use a loop, because Bash won't remove all ":foo:"
# in ":foo:foo:" in one go.
path=":$PATH:"
remove="${RBENV_ROOT}/shims"
while true; do
path_before="$path"
path="${path//:$remove:/:}"
if [[ "$path_before" = "$path" ]]; then
break
fi
done
RBENV_COMMAND_PATH="$(PATH=$path command -v "$RBENV_COMMAND" || true)"
PATH="$(remove_from_path "${RBENV_ROOT}/shims")"
RBENV_COMMAND_PATH="$(command -v "$RBENV_COMMAND" || true)"
else
RBENV_COMMAND_PATH="${RBENV_ROOT}/versions/${RBENV_VERSION}/bin/${RBENV_COMMAND}"
fi