From cec6d467925a8c61f04faba74cfc92d32bca000e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ville=20Skytt=C3=A4?= Date: Tue, 8 Mar 2022 20:56:22 +0200 Subject: [PATCH 1/2] Don't bother reading empty version files --- libexec/rbenv-version-file-read | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libexec/rbenv-version-file-read b/libexec/rbenv-version-file-read index c94404ca..2e3b4a2e 100755 --- a/libexec/rbenv-version-file-read +++ b/libexec/rbenv-version-file-read @@ -5,7 +5,7 @@ set -e VERSION_FILE="$1" -if [ -e "$VERSION_FILE" ]; then +if [ -s "$VERSION_FILE" ]; then # Read the first word from the specified version file. Avoid reading it whole. IFS="${IFS}"$'\r' words=( $(cut -b 1-1024 "$VERSION_FILE") ) From b39d4291bed8439330f8a399dcdf6f11cf03eabe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ville=20Skytt=C3=A4?= Date: Tue, 8 Mar 2022 20:58:58 +0200 Subject: [PATCH 2/2] Simplify version file read Avoid a subshell and external `cut` invocation, as well as a throwaway intermediate array. --- libexec/rbenv-version-file-read | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/libexec/rbenv-version-file-read b/libexec/rbenv-version-file-read index 2e3b4a2e..b467d76f 100755 --- a/libexec/rbenv-version-file-read +++ b/libexec/rbenv-version-file-read @@ -8,8 +8,7 @@ VERSION_FILE="$1" if [ -s "$VERSION_FILE" ]; then # Read the first word from the specified version file. Avoid reading it whole. IFS="${IFS}"$'\r' - words=( $(cut -b 1-1024 "$VERSION_FILE") ) - version="${words[0]}" + read -n 1024 -d "" -r version _ <"$VERSION_FILE" || : if [ "$version" = ".." ] || [[ $version == */* ]]; then echo "rbenv: invalid version in \`$VERSION_FILE'" >&2