Skip to content

Commit

Permalink
rbenv install: fix substituting $HOME with "~" (#2501)
Browse files Browse the repository at this point in the history
Depending on bash version, the expression `${var/$HOME\//~/}` will not have
effect because the "~" character in the replacement expression is expanded.

The updated approach is a bit of a mouthful, but it avoids using "~" in a
substitution pattern, while also guarding against values of HOME that are
blank or when HOME is literally just "/".
  • Loading branch information
mislav authored Jan 21, 2025
1 parent 3643c8e commit fdcfac8
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
4 changes: 3 additions & 1 deletion bin/rbenv-install
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,10 @@ if [ "$STATUS" == "2" ]; then
printf ":\n\n"
echo " brew upgrade ruby-build"
elif [ -d "${here}/.git" ]; then
display_here="$here"
[[ -z "${HOME%/}" || $here != "${HOME}/"* ]] || display_here="~${here#"$HOME"}"
printf ":\n\n"
echo " git -C ${here/${HOME}\//~/} pull"
echo " git -C $display_here pull"
else
printf ".\n"
fi
Expand Down
7 changes: 6 additions & 1 deletion test/rbenv.bats
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ OUT
}

@test "nonexistent version" {
display_here="${BATS_TEST_DIRNAME}"/..
if [[ -n $HOME && $display_here == "${HOME}/"* ]]; then
display_here="~${display_here#"${HOME}"}"
fi

stub_git_dir=
if [ ! -d "${BATS_TEST_DIRNAME}"/../.git ]; then
stub_git_dir="${BATS_TEST_DIRNAME}"/../.git
Expand All @@ -113,7 +118,7 @@ See all available versions with \`rbenv install --list-all'.
If the version you need is missing, try upgrading ruby-build:
git -C ${BATS_TEST_DIRNAME/$HOME\//~/}/.. pull
git -C $display_here pull
OUT

unstub brew
Expand Down

0 comments on commit fdcfac8

Please sign in to comment.