[Fix] install.sh: update from nvm's remote; nvm_ls: list versions without globs; nvm_ensure_version_installed: do not leak VERSION
`install.sh`: an existing install now fetches from the remote whose URL is nvm's source, adding one when none matches, instead of always fetching `origin`; fresh installs still name it `origin`. The remote's URL is read with `git config`, since `git remote get-url` needs git 2.7. `nvm_ls`: `find` lists each version directory's non-hidden subdirectories itself, so it works under `set -f`, which disables the globs it was given; the directories only need to exist. `nvm_ensure_version_installed`: the resolved alias was assigned to `VERSION` without declaring it local. Co-authored-by: dajiaohuang <mikewushuwen@outlook.com> Co-authored-by: Jordan Harband <ljharb@gmail.com>
This commit is contained in:
1 parent
a4fadf1eae
commit
dbda4b9387
6 files changed
+109
-64
No files matched your search
+24
-2
@@ -172,11 +172,33 @@ install_nvm_from_git() {
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
local fetch_error
|
local fetch_error
|
||||||
|
local NVM_REMOTE_NAME
|
||||||
|
NVM_REMOTE_NAME='origin'
|
||||||
if [ -d "$INSTALL_DIR/.git" ]; then
|
if [ -d "$INSTALL_DIR/.git" ]; then
|
||||||
# Updating repo
|
# Updating repo
|
||||||
nvm_echo "=> nvm is already installed in $INSTALL_DIR, trying to update using git"
|
nvm_echo "=> nvm is already installed in $INSTALL_DIR, trying to update using git"
|
||||||
command printf '\r=> '
|
command printf '\r=> '
|
||||||
fetch_error="Failed to update nvm with $NVM_VERSION, run 'git fetch' in $INSTALL_DIR yourself."
|
fetch_error="Failed to update nvm with $NVM_VERSION, run 'git fetch' in $INSTALL_DIR yourself."
|
||||||
|
local NVM_REMOTE
|
||||||
|
local NVM_GIT_SOURCE
|
||||||
|
NVM_GIT_SOURCE="$(nvm_source git)"
|
||||||
|
NVM_REMOTE_NAME=''
|
||||||
|
for NVM_REMOTE in $(command git --git-dir="${INSTALL_DIR}/.git" remote); do
|
||||||
|
if [ "$(command git --git-dir="${INSTALL_DIR}/.git" config --get "remote.${NVM_REMOTE}.url")" = "${NVM_GIT_SOURCE}" ]; then
|
||||||
|
NVM_REMOTE_NAME="${NVM_REMOTE}"
|
||||||
|
break
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
if [ -z "${NVM_REMOTE_NAME}" ]; then
|
||||||
|
NVM_REMOTE_NAME='nvm-origin'
|
||||||
|
local NVM_REMOTE_SUFFIX
|
||||||
|
NVM_REMOTE_SUFFIX=0
|
||||||
|
while command git --git-dir="${INSTALL_DIR}/.git" config --get "remote.${NVM_REMOTE_NAME}.url" >/dev/null; do
|
||||||
|
NVM_REMOTE_SUFFIX=$((NVM_REMOTE_SUFFIX + 1))
|
||||||
|
NVM_REMOTE_NAME="nvm-origin-${NVM_REMOTE_SUFFIX}"
|
||||||
|
done
|
||||||
|
command git --git-dir="${INSTALL_DIR}/.git" remote add "${NVM_REMOTE_NAME}" "${NVM_GIT_SOURCE}" || return 2
|
||||||
|
fi
|
||||||
else
|
else
|
||||||
fetch_error="Failed to fetch origin with $NVM_VERSION. Please report this!"
|
fetch_error="Failed to fetch origin with $NVM_VERSION. Please report this!"
|
||||||
nvm_echo "=> Downloading nvm from git to '$INSTALL_DIR'"
|
nvm_echo "=> Downloading nvm from git to '$INSTALL_DIR'"
|
||||||
@@ -205,10 +227,10 @@ install_nvm_from_git() {
|
|||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
# Try to fetch tag
|
# Try to fetch tag
|
||||||
if command git --git-dir="$INSTALL_DIR"/.git --work-tree="$INSTALL_DIR" fetch origin tag "$NVM_VERSION" --depth=1 2>/dev/null; then
|
if command git --git-dir="$INSTALL_DIR"/.git --work-tree="$INSTALL_DIR" fetch "${NVM_REMOTE_NAME}" tag "$NVM_VERSION" --depth=1 2>/dev/null; then
|
||||||
:
|
:
|
||||||
# Fetch given version
|
# Fetch given version
|
||||||
elif ! command git --git-dir="$INSTALL_DIR"/.git --work-tree="$INSTALL_DIR" fetch origin "$NVM_VERSION" --depth=1; then
|
elif ! command git --git-dir="$INSTALL_DIR"/.git --work-tree="$INSTALL_DIR" fetch "${NVM_REMOTE_NAME}" "$NVM_VERSION" --depth=1; then
|
||||||
nvm_echo >&2 "$fetch_error"
|
nvm_echo >&2 "$fetch_error"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|||||||
@@ -1345,8 +1345,9 @@ nvm_ensure_version_installed() {
|
|||||||
if nvm_explain_excluded_version "${PROVIDED_VERSION}"; then
|
if nvm_explain_excluded_version "${PROVIDED_VERSION}"; then
|
||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
if VERSION="$(nvm_resolve_alias "${PROVIDED_VERSION}")"; then
|
local NVM_RESOLVED_VERSION
|
||||||
nvm_err "N/A: version \"${PROVIDED_VERSION} -> ${VERSION}\" is not yet installed."
|
if NVM_RESOLVED_VERSION="$(nvm_resolve_alias "${PROVIDED_VERSION}")"; then
|
||||||
|
nvm_err "N/A: version \"${PROVIDED_VERSION} -> ${NVM_RESOLVED_VERSION}\" is not yet installed."
|
||||||
else
|
else
|
||||||
local PREFIXED_VERSION
|
local PREFIXED_VERSION
|
||||||
PREFIXED_VERSION="$(nvm_ensure_version_prefix "${PROVIDED_VERSION}")"
|
PREFIXED_VERSION="$(nvm_ensure_version_prefix "${PROVIDED_VERSION}")"
|
||||||
@@ -2667,9 +2668,6 @@ nvm_ls() {
|
|||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
|
|
||||||
nvm_is_zsh && setopt local_options shwordsplit
|
|
||||||
nvm_is_zsh && setopt local_options nomarkdirs
|
|
||||||
|
|
||||||
local NVM_DIRS_TO_SEARCH1
|
local NVM_DIRS_TO_SEARCH1
|
||||||
NVM_DIRS_TO_SEARCH1=''
|
NVM_DIRS_TO_SEARCH1=''
|
||||||
local NVM_DIRS_TO_SEARCH2
|
local NVM_DIRS_TO_SEARCH2
|
||||||
@@ -2694,14 +2692,14 @@ nvm_ls() {
|
|||||||
NVM_SYSTEM_CHECK='any'
|
NVM_SYSTEM_CHECK='any'
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if ! nvm_dir_has_entries "${NVM_DIRS_TO_SEARCH1}"; then
|
if [ ! -d "${NVM_DIRS_TO_SEARCH1}" ]; then
|
||||||
NVM_DIRS_TO_SEARCH1=''
|
NVM_DIRS_TO_SEARCH1=''
|
||||||
fi
|
fi
|
||||||
if ! nvm_dir_has_entries "${NVM_DIRS_TO_SEARCH2}"; then
|
if [ ! -d "${NVM_DIRS_TO_SEARCH2}" ]; then
|
||||||
NVM_DIRS_TO_SEARCH2="${NVM_DIRS_TO_SEARCH1}"
|
NVM_DIRS_TO_SEARCH2=''
|
||||||
fi
|
fi
|
||||||
if ! nvm_dir_has_entries "${NVM_DIRS_TO_SEARCH3}"; then
|
if [ ! -d "${NVM_DIRS_TO_SEARCH3}" ]; then
|
||||||
NVM_DIRS_TO_SEARCH3="${NVM_DIRS_TO_SEARCH2}"
|
NVM_DIRS_TO_SEARCH3=''
|
||||||
fi
|
fi
|
||||||
|
|
||||||
local SEARCH_PATTERN
|
local SEARCH_PATTERN
|
||||||
@@ -2723,8 +2721,17 @@ nvm_ls() {
|
|||||||
NVM_DIR_SED="$(nvm_echo "${NVM_DIR}" | command sed 's/[][\.*^$#]/\\&/g')"
|
NVM_DIR_SED="$(nvm_echo "${NVM_DIR}" | command sed 's/[][\.*^$#]/\\&/g')"
|
||||||
local NVM_VERSION_DIR_IOJS_SED
|
local NVM_VERSION_DIR_IOJS_SED
|
||||||
NVM_VERSION_DIR_IOJS_SED="$(nvm_echo "${NVM_VERSION_DIR_IOJS}" | command sed 's/[][\.*^$#]/\\&/g')"
|
NVM_VERSION_DIR_IOJS_SED="$(nvm_echo "${NVM_VERSION_DIR_IOJS}" | command sed 's/[][\.*^$#]/\\&/g')"
|
||||||
VERSIONS="$(command find -L "${NVM_DIRS_TO_SEARCH1}"/* "${NVM_DIRS_TO_SEARCH2}"/* "${NVM_DIRS_TO_SEARCH3}"/* -name . -o -type d -prune -o -path "${PATTERN}*" \
|
local NVM_FIND_DIR
|
||||||
|
set --
|
||||||
|
for NVM_FIND_DIR in "${NVM_DIRS_TO_SEARCH1}" "${NVM_DIRS_TO_SEARCH2}" "${NVM_DIRS_TO_SEARCH3}"; do
|
||||||
|
if [ -n "${NVM_FIND_DIR}" ]; then
|
||||||
|
set -- "$@" "${NVM_FIND_DIR}/."
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
# each directory's non-hidden subdirectories, without globs, which `set -f` disables
|
||||||
|
VERSIONS="$(command find -L "$@" ! -name . -prune ! -name '.*' -type d \
|
||||||
| command sed -e "
|
| command sed -e "
|
||||||
|
s#/\\./\\([^/]*\\)\$#/\\1#;
|
||||||
s#${NVM_VERSION_DIR_IOJS_SED}/#versions/${NVM_IOJS_PREFIX}/#;
|
s#${NVM_VERSION_DIR_IOJS_SED}/#versions/${NVM_IOJS_PREFIX}/#;
|
||||||
s#^${NVM_DIR_SED}/##;
|
s#^${NVM_DIR_SED}/##;
|
||||||
\\#^[^v]# d;
|
\\#^[^v]# d;
|
||||||
@@ -4801,18 +4808,6 @@ nvm_write_nvmrc() {
|
|||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
# Whether a directory has an entry that `ls` would list, without forking
|
|
||||||
nvm_dir_has_entries() {
|
|
||||||
nvm_is_zsh && setopt local_options nonomatch
|
|
||||||
local NVM_DIR_ENTRY
|
|
||||||
for NVM_DIR_ENTRY in "${1-}"/*; do
|
|
||||||
if [ -e "${NVM_DIR_ENTRY}" ] || [ -L "${NVM_DIR_ENTRY}" ]; then
|
|
||||||
return 0
|
|
||||||
fi
|
|
||||||
done
|
|
||||||
return 1
|
|
||||||
}
|
|
||||||
|
|
||||||
# Check version dir permissions
|
# Check version dir permissions
|
||||||
nvm_check_file_permissions() {
|
nvm_check_file_permissions() {
|
||||||
nvm_is_zsh && setopt local_options nonomatch
|
nvm_is_zsh && setopt local_options nonomatch
|
||||||
@@ -7099,7 +7094,7 @@ nvm() {
|
|||||||
nvm_needs_lts_status nvm_filter_installed_versions nvm_is_version_selected \
|
nvm_needs_lts_status nvm_filter_installed_versions nvm_is_version_selected \
|
||||||
nvm_install_binary nvm_install_source nvm_clang_version \
|
nvm_install_binary nvm_install_source nvm_clang_version \
|
||||||
nvm_get_mirror nvm_get_download_slug nvm_download_artifact nvm_is_cached_artifact \
|
nvm_get_mirror nvm_get_download_slug nvm_download_artifact nvm_is_cached_artifact \
|
||||||
nvm_install_npm_if_needed nvm_use_if_needed nvm_dir_has_entries nvm_check_file_permissions \
|
nvm_install_npm_if_needed nvm_use_if_needed nvm_check_file_permissions \
|
||||||
nvm_print_versions nvm_validate_min_version nvm_is_below_min_version nvm_filter_min_versions nvm_compute_checksum \
|
nvm_print_versions nvm_validate_min_version nvm_is_below_min_version nvm_filter_min_versions nvm_compute_checksum \
|
||||||
nvm_get_checksum_binary \
|
nvm_get_checksum_binary \
|
||||||
nvm_get_checksum_alg nvm_get_checksum nvm_compare_checksum \
|
nvm_get_checksum_alg nvm_get_checksum nvm_compare_checksum \
|
||||||
|
|||||||
@@ -1,38 +0,0 @@
|
|||||||
#!/bin/sh
|
|
||||||
|
|
||||||
cleanup() {
|
|
||||||
rm -rf "${TEST_DIR}"
|
|
||||||
}
|
|
||||||
|
|
||||||
die() {
|
|
||||||
echo "$@"
|
|
||||||
cleanup
|
|
||||||
exit 1
|
|
||||||
}
|
|
||||||
|
|
||||||
: nvm.sh
|
|
||||||
\. ../../../nvm.sh
|
|
||||||
|
|
||||||
TEST_DIR="$(mktemp -d)"
|
|
||||||
|
|
||||||
# glob characters and a space in the path are matched literally
|
|
||||||
DIR="${TEST_DIR}/a [b] *"
|
|
||||||
|
|
||||||
! nvm_dir_has_entries "${DIR}" || die 'a missing directory has no entries'
|
|
||||||
|
|
||||||
mkdir "${DIR}"
|
|
||||||
! nvm_dir_has_entries "${DIR}" || die 'an empty directory has no entries'
|
|
||||||
|
|
||||||
touch "${DIR}/.hidden"
|
|
||||||
! nvm_dir_has_entries "${DIR}" || die 'a directory with only dotfiles has no entries, as with `ls`'
|
|
||||||
|
|
||||||
ln -s "${TEST_DIR}/nonexistent" "${DIR}/broken"
|
|
||||||
nvm_dir_has_entries "${DIR}" || die 'a broken symlink is an entry'
|
|
||||||
rm "${DIR}/broken"
|
|
||||||
|
|
||||||
mkdir "${DIR}/v1.0.0"
|
|
||||||
nvm_dir_has_entries "${DIR}" || die 'a directory with a subdirectory has entries'
|
|
||||||
|
|
||||||
! nvm_dir_has_entries "${TEST_DIR}/a [b] " || die 'the directory name is not a pattern'
|
|
||||||
|
|
||||||
cleanup
|
|
||||||
@@ -70,4 +70,12 @@ EXPECTED_OUTPUT=''
|
|||||||
[ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] || die "expected 'nvm_ensure_version_installed system' with iojs installed to give $EXPECTED_OUTPUT, got $OUTPUT"
|
[ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] || die "expected 'nvm_ensure_version_installed system' with iojs installed to give $EXPECTED_OUTPUT, got $OUTPUT"
|
||||||
[ "_$EXIT_CODE" = "_0" ] || die "expected 'nvm_ensure_version_installed system' with iojs installed to exit with 0, got $EXIT_CODE"
|
[ "_$EXIT_CODE" = "_0" ] || die "expected 'nvm_ensure_version_installed system' with iojs installed to exit with 0, got $EXIT_CODE"
|
||||||
|
|
||||||
|
# An unresolved installed-version check must not overwrite a caller's VERSION.
|
||||||
|
VERSION='caller-value'
|
||||||
|
nvm_resolve_alias() { [ "_${1-}" = '_nonexistent' ] || return 2; nvm_echo 'v123.4.5'; }
|
||||||
|
nvm_ensure_version_installed nonexistent >/dev/null 2>&1 && die 'expected missing version failure'
|
||||||
|
[ "${VERSION}" = 'caller-value' ] || die "VERSION leaked: ${VERSION}"
|
||||||
|
unset -f nvm_resolve_alias
|
||||||
|
unset VERSION
|
||||||
|
|
||||||
cleanup
|
cleanup
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
|
||||||
|
die() { echo "$*" >&2; cleanup; exit 1; }
|
||||||
|
cleanup() { rm -rf "${TEST_DIR}"; }
|
||||||
|
|
||||||
|
\. ../../../nvm.sh --no-use
|
||||||
|
\. ../../common.sh
|
||||||
|
|
||||||
|
TEST_DIR="${PWD}/nvm_ls_hidden_noglob_tmp"
|
||||||
|
NVM_DIR="${TEST_DIR}"
|
||||||
|
export NVM_DIR
|
||||||
|
mkdir -p "${NVM_DIR}/versions/node/.hidden" "${NVM_DIR}/versions/node/v24.13.0/bin" || die 'could not create fixture'
|
||||||
|
make_echo "${NVM_DIR}/versions/node/v24.13.0/bin/node" 'v24.13.0' || die 'could not create fake node'
|
||||||
|
|
||||||
|
set -f
|
||||||
|
try nvm_ls 24
|
||||||
|
[ "_${CAPTURED_STDOUT}" = '_v24.13.0' ] || die "unexpected output with noglob: ${CAPTURED_STDOUT}"
|
||||||
|
case "$-" in *f*) ;; *) die 'nvm_ls changed noglob';; esac
|
||||||
|
set +f
|
||||||
|
try nvm_ls
|
||||||
|
case "${CAPTURED_STDOUT}" in *.hidden*) die "expected hidden entries to be skipped; got ${CAPTURED_STDOUT}" ;; esac
|
||||||
|
rm -rf "${NVM_DIR}/versions/node/v24.13.0"
|
||||||
|
try nvm_ls 24
|
||||||
|
[ "_${CAPTURED_STDERR}" = '_' ] || die "unexpected error for hidden-only directory: ${CAPTURED_STDERR}"
|
||||||
|
cleanup
|
||||||
+33
@@ -0,0 +1,33 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
|
||||||
|
NVM_ENV=testing \. ../../install.sh
|
||||||
|
|
||||||
|
die() { echo "$*" >&2; exit 1; }
|
||||||
|
TEST_ROOT="$(mktemp -d)" || die 'could not create test directory'
|
||||||
|
trap 'rm -rf "${TEST_ROOT}"' EXIT HUP INT TERM
|
||||||
|
SOURCE="${TEST_ROOT}/source"
|
||||||
|
git init -q "${SOURCE}" || die 'could not initialize source'
|
||||||
|
git -C "${SOURCE}" -c user.name=test -c user.email=test@example.invalid commit --allow-empty -qm fixture || die 'could not commit fixture'
|
||||||
|
git -C "${SOURCE}" tag v0.0.1 || die 'could not tag fixture'
|
||||||
|
nvm_source() { printf '%s\n' "${SOURCE}"; }
|
||||||
|
NVM_INSTALL_VERSION=v0.0.1
|
||||||
|
export NVM_INSTALL_VERSION
|
||||||
|
|
||||||
|
NVM_DIR="${TEST_ROOT}/fresh"
|
||||||
|
export NVM_DIR
|
||||||
|
install_nvm_from_git || die 'fresh install failed'
|
||||||
|
[ "$(git -C "${NVM_DIR}" remote get-url origin)" = "${SOURCE}" ] || die 'fresh install did not retain origin'
|
||||||
|
|
||||||
|
NVM_DIR="${TEST_ROOT}/existing"
|
||||||
|
export NVM_DIR
|
||||||
|
git init -q "${NVM_DIR}" || die 'could not initialize existing install'
|
||||||
|
git -C "${NVM_DIR}" remote add fork "${TEST_ROOT}/wrong" || die 'could not add fork'
|
||||||
|
git -C "${NVM_DIR}" remote add origin "${SOURCE}" || die 'could not add origin'
|
||||||
|
install_nvm_from_git || die 'existing install did not select matching origin'
|
||||||
|
[ "$(git -C "${NVM_DIR}" remote get-url fork)" = "${TEST_ROOT}/wrong" ] || die 'fork remote changed'
|
||||||
|
|
||||||
|
git -C "${NVM_DIR}" remote set-url origin "${TEST_ROOT}/wrong-origin" || die 'could not change origin'
|
||||||
|
git -C "${NVM_DIR}" remote add nvm-origin "${TEST_ROOT}/occupied" || die 'could not occupy alternate name'
|
||||||
|
install_nvm_from_git || die 'existing install did not add a matching remote'
|
||||||
|
[ "$(git -C "${NVM_DIR}" remote get-url nvm-origin-1)" = "${SOURCE}" ] || die 'new remote missing'
|
||||||
|
[ "$(git -C "${NVM_DIR}" remote get-url origin)" = "${TEST_ROOT}/wrong-origin" ] || die 'origin remote changed'
|
||||||
Reference in new issue
Block a user