From fdcfac8d4208bc0c5469885930c79ada77c3a298 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mislav=20Marohni=C4=87?= Date: Tue, 21 Jan 2025 22:11:40 +0100 Subject: [PATCH] rbenv install: fix substituting $HOME with "~" (#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 "/". --- bin/rbenv-install | 4 +++- test/rbenv.bats | 7 ++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/bin/rbenv-install b/bin/rbenv-install index f6df0f53bd..b5f47800c2 100755 --- a/bin/rbenv-install +++ b/bin/rbenv-install @@ -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 diff --git a/test/rbenv.bats b/test/rbenv.bats index a93f0e60be..2f52961a31 100644 --- a/test/rbenv.bats +++ b/test/rbenv.bats @@ -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 @@ -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