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
pull/1921/head
Noah Gorny 2021-08-17 23:49:04 +03:00
parent caec973c18
commit 9566a3ec18
1 changed files with 12 additions and 8 deletions

View File

@ -194,7 +194,10 @@ _bash-it-update-stable() {
_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 git checkout "$1" &> /dev/null
if [[ $? -eq 0 ]]; then if [[ $? -eq 0 ]]; then
echo "Bash-it successfully updated." echo "Bash-it successfully updated."
@ -203,7 +206,9 @@ _bash-it_pull_and_update_inner() {
_bash-it-migrate _bash-it-migrate
echo "" echo ""
echo "All done, enjoy!" echo "All done, enjoy!"
bash-it restart # Don't forget to restore the original pwd!
popd &> /dev/null
_bash-it-restart
else else
echo "Error updating Bash-it, please, check if your Bash-it installation folder (${BASH_IT}) is clean." echo "Error updating Bash-it, please, check if your Bash-it installation folder (${BASH_IT}) is clean."
fi fi
@ -220,9 +225,8 @@ _bash-it-update-() {
silent=true silent=true
fi fi
done done
local old_pwd="${PWD}"
cd "${BASH_IT}" || return pushd "${BASH_IT}" &> /dev/null || return
DIFF=$(git diff --name-status) 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 [ -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 if [[ -z "$TARGET" ]]; then
echo "Can not find tags, so can not update to latest stable version..." echo "Can not find tags, so can not update to latest stable version..."
cd "${old_pwd}" &> /dev/null popd &> /dev/null
return return
fi fi
else else
@ -284,12 +288,12 @@ _bash-it-update-() {
if [[ $silent ]]; then if [[ $silent ]]; then
echo "Updating to ${TARGET}($(git log -1 --format=%h "${TARGET}"))..." 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 else
read -e -n 1 -p "Would you like to update to ${TARGET}($(git log -1 --format=%h "${TARGET}"))? [Y/n] " RESP read -e -n 1 -p "Would you like to update to ${TARGET}($(git log -1 --format=%h "${TARGET}"))? [Y/n] " RESP
case $RESP in case $RESP in
[yY]|"") [yY]|"")
_bash-it_pull_and_update_inner $TARGET $version _bash-it_update_migrate_and_restart $TARGET $version
;; ;;
[nN]) [nN])
echo "Not updating…" echo "Not updating…"
@ -306,7 +310,7 @@ _bash-it-update-() {
echo "Bash-it is up to date, nothing to do!" echo "Bash-it is up to date, nothing to do!"
fi fi
fi fi
cd "${old_pwd}" &> /dev/null || return popd &> /dev/null
} }
_bash-it-migrate() { _bash-it-migrate() {