Skip to content

Commit fdcfac8

Browse files
authored
rbenv install: fix substituting $HOME with "~" (rbenv#2501)
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 "/".
1 parent 3643c8e commit fdcfac8

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

bin/rbenv-install

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,8 +250,10 @@ if [ "$STATUS" == "2" ]; then
250250
printf ":\n\n"
251251
echo " brew upgrade ruby-build"
252252
elif [ -d "${here}/.git" ]; then
253+
display_here="$here"
254+
[[ -z "${HOME%/}" || $here != "${HOME}/"* ]] || display_here="~${here#"$HOME"}"
253255
printf ":\n\n"
254-
echo " git -C ${here/${HOME}\//~/} pull"
256+
echo " git -C $display_here pull"
255257
else
256258
printf ".\n"
257259
fi

test/rbenv.bats

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,11 @@ OUT
8888
}
8989

9090
@test "nonexistent version" {
91+
display_here="${BATS_TEST_DIRNAME}"/..
92+
if [[ -n $HOME && $display_here == "${HOME}/"* ]]; then
93+
display_here="~${display_here#"${HOME}"}"
94+
fi
95+
9196
stub_git_dir=
9297
if [ ! -d "${BATS_TEST_DIRNAME}"/../.git ]; then
9398
stub_git_dir="${BATS_TEST_DIRNAME}"/../.git
@@ -113,7 +118,7 @@ See all available versions with \`rbenv install --list-all'.
113118
114119
If the version you need is missing, try upgrading ruby-build:
115120
116-
git -C ${BATS_TEST_DIRNAME/$HOME\//~/}/.. pull
121+
git -C $display_here pull
117122
OUT
118123

119124
unstub brew

0 commit comments

Comments
 (0)