From caec973c18844b6edaf6ba62450867c7165e801f Mon Sep 17 00:00:00 2001 From: Noah Gorny Date: Tue, 17 Aug 2021 23:40:19 +0300 Subject: [PATCH 1/2] helpers: Pop pwd correctly when not needing to update --- lib/helpers.bash | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/helpers.bash b/lib/helpers.bash index 34149040..31e3fec8 100755 --- a/lib/helpers.bash +++ b/lib/helpers.bash @@ -243,6 +243,7 @@ _bash-it-update-() { if [[ -z "$TARGET" ]]; then echo "Can not find tags, so can not update to latest stable version..." + cd "${old_pwd}" &> /dev/null return fi else From 9566a3ec18e6e39b9457f4d79b85192a0aa0459d Mon Sep 17 00:00:00 2001 From: Noah Gorny Date: Tue, 17 Aug 2021 23:49:04 +0300 Subject: [PATCH 2/2] helpers: Improve pull_and_update_inner by restoring old pwd Renamed the function to _bash-it_update_migrate_and_restart Use pushd/popd instead of passing another parameter Document the function so it will be clear that it does not return --- lib/helpers.bash | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/lib/helpers.bash b/lib/helpers.bash index 31e3fec8..4d058fd1 100755 --- a/lib/helpers.bash +++ b/lib/helpers.bash @@ -194,7 +194,10 @@ _bash-it-update-stable() { _bash-it-update- stable "$@" } -_bash-it_pull_and_update_inner() { +_bash-it_update_migrate_and_restart() { + _about 'Checks out the wanted version, pops directory and restart. Does not return (because of the restart!)' + _param '1: Which branch to checkout to' + _param '2: Which type of version we are using' git checkout "$1" &> /dev/null if [[ $? -eq 0 ]]; then echo "Bash-it successfully updated." @@ -203,7 +206,9 @@ _bash-it_pull_and_update_inner() { _bash-it-migrate echo "" echo "All done, enjoy!" - bash-it restart + # Don't forget to restore the original pwd! + popd &> /dev/null + _bash-it-restart else echo "Error updating Bash-it, please, check if your Bash-it installation folder (${BASH_IT}) is clean." fi @@ -220,9 +225,8 @@ _bash-it-update-() { silent=true fi done - local old_pwd="${PWD}" - cd "${BASH_IT}" || return + pushd "${BASH_IT}" &> /dev/null || return DIFF=$(git diff --name-status) [ -n "$DIFF" ] && echo -e "Local changes detected in bash-it directory. Clean '$BASH_IT' directory to proceed.\n$DIFF" && return 1 @@ -243,7 +247,7 @@ _bash-it-update-() { if [[ -z "$TARGET" ]]; then echo "Can not find tags, so can not update to latest stable version..." - cd "${old_pwd}" &> /dev/null + popd &> /dev/null return fi else @@ -284,12 +288,12 @@ _bash-it-update-() { if [[ $silent ]]; then echo "Updating to ${TARGET}($(git log -1 --format=%h "${TARGET}"))..." - _bash-it_pull_and_update_inner $TARGET $version + _bash-it_update_migrate_and_restart $TARGET $version else read -e -n 1 -p "Would you like to update to ${TARGET}($(git log -1 --format=%h "${TARGET}"))? [Y/n] " RESP case $RESP in [yY]|"") - _bash-it_pull_and_update_inner $TARGET $version + _bash-it_update_migrate_and_restart $TARGET $version ;; [nN]) echo "Not updating…" @@ -306,7 +310,7 @@ _bash-it-update-() { echo "Bash-it is up to date, nothing to do!" fi fi - cd "${old_pwd}" &> /dev/null || return + popd &> /dev/null } _bash-it-migrate() {