diff --git a/README.md b/README.md index 26ac5cb7..1da1f7eb 100644 --- a/README.md +++ b/README.md @@ -87,13 +87,24 @@ Have a look at our [bash-it-docker repository](https://github.com/Bash-it/bash-i ### Updating -To update Bash-it to the latest version, simply run: +To update Bash-it to the latest stable version, simply run: ```bash -bash-it update +bash-it update stable ``` -that's all. +If you want to update to the latest dev version (directly from master), run: + +```bash +bash-it update dev +``` + +If you want to update automatically and unattended, you can add the optional +`-s/--silent` flag, for example: + +```bash +bash-it update dev --silent +``` If you are using an older version of Bash-it, it's possible that some functionality has changed, or that the internal structure of how Bash-it organizes its functionality has been updated. For these cases, we provide a `migrate` command: diff --git a/completion/available/bash-it.completion.bash b/completion/available/bash-it.completion.bash index 29f81076..4fdd72d6 100644 --- a/completion/available/bash-it.completion.bash +++ b/completion/available/bash-it.completion.bash @@ -87,7 +87,16 @@ _bash-it-comp() COMPREPLY=( $(compgen -W "${doctor_args}" -- ${cur}) ) return 0 ;; - migrate | reload | search | update | version) + update) + if [[ ${cur} == -* ]];then + local update_args="-s --silent" + else + local update_args="stable dev" + fi + COMPREPLY=( $(compgen -W "${update_args}" -- ${cur}) ) + return 0 + ;; + migrate | reload | search | version) return 0 ;; enable | disable) diff --git a/lib/helpers.bash b/lib/helpers.bash index 73ae1672..8917ed50 100644 --- a/lib/helpers.bash +++ b/lib/helpers.bash @@ -80,7 +80,7 @@ bash-it () _bash-it-search $component "$@" return;; update) - func=_bash-it_update;; + func=_bash-it_update-$component;; migrate) func=_bash-it-migrate;; version) @@ -158,60 +158,127 @@ _bash-it-plugins () _bash-it-describe "plugins" "a" "plugin" "Plugin" } -_bash-it_update() { - _about 'updates Bash-it' +_bash-it_update-dev() { + _about 'updates Bash-it to the latest master' _group 'lib' + _bash-it_update- dev "$@" +} + +_bash-it_update-stable() { + _about 'updates Bash-it to the latest tag' + _group 'lib' + + _bash-it_update- stable "$@" +} + +_bash-it_pull_and_update_inner() { + git checkout "$1" &> /dev/null + if [[ $? -eq 0 ]]; then + echo "Bash-it successfully updated." + echo "" + echo "Migrating your installation to the latest $2 version now..." + _bash-it-migrate + echo "" + echo "All done, enjoy!" + bash-it reload + else + echo "Error updating Bash-it, please, check if your Bash-it installation folder (${BASH_IT}) is clean." + fi +} + +_bash-it_update-() { + _about 'updates Bash-it' + _param '1: What kind of update to do (stable|dev)' + _group 'lib' + + declare silent + for word in $@; do + if [[ ${word} == "--silent" || ${word} == "-s" ]]; then + silent=true + fi + done local old_pwd="${PWD}" cd "${BASH_IT}" || return - if [ -z $BASH_IT_REMOTE ]; then + if [ -z "$BASH_IT_REMOTE" ]; then BASH_IT_REMOTE="origin" fi - git fetch &> /dev/null + git fetch $BASH_IT_REMOTE --tags &> /dev/null + if [ -z "$BASH_IT_DEVELOPMENT_BRANCH" ]; then + BASH_IT_DEVELOPMENT_BRANCH="master" + fi + # Defaults to stable update + if [ -z "$1" ] || [ "$1" == "stable" ]; then + version="stable" + TARGET=$(git describe --tags "$(git rev-list --tags --max-count=1)" 2> /dev/null) + + if [[ -z "$TARGET" ]]; then + echo "Can not find tags, so can not update to latest stable version..." + return + fi + else + version="dev" + TARGET=${BASH_IT_REMOTE}/${BASH_IT_DEVELOPMENT_BRANCH} + fi + + declare revision + revision="HEAD..${TARGET}" declare status - status="$(git rev-list master..${BASH_IT_REMOTE}/master 2> /dev/null)" + status="$(git rev-list ${revision} 2> /dev/null)" + declare revert + + if [[ -z "${status}" && ${version} == "stable" ]]; then + revision="${TARGET}..HEAD" + status="$(git rev-list ${revision} 2> /dev/null)" + revert=true + fi if [[ -n "${status}" ]]; then + if [[ $revert ]]; then + echo "Your version is a more recent development version ($(git log -1 --format=%h HEAD))" + echo "You can continue in order to revert and update to the latest stable version" + echo "" + log_color="%Cred" + fi - for i in $(git rev-list --merges --first-parent master..${BASH_IT_REMOTE}); do + for i in $(git rev-list --merges --first-parent ${revision}); do num_of_lines=$(git log -1 --format=%B $i | awk 'NF' | wc -l) if [ $num_of_lines -eq 1 ]; then description="%s" else description="%b" fi - git log --format="%h: $description (%an)" -1 $i + git log --format="${log_color}%h: $description (%an)" -1 $i done echo "" - read -e -n 1 -p "Would you like to update to $(git log -1 --format=%h origin/master)? [Y/n] " RESP - case $RESP in - [yY]|"") - git pull --rebase &> /dev/null - if [[ $? -eq 0 ]]; then - echo "Bash-it successfully updated." - echo "" - echo "Migrating your installation to the latest version now..." - _bash-it-migrate - echo "" - echo "All done, enjoy!" - bash-it reload - else - echo "Error updating Bash-it, please, check if your Bash-it installation folder (${BASH_IT}) is clean." - fi - ;; - [nN]) - echo "Not upgrading…" - ;; - *) - echo -e "\033[91mPlease choose y or n.\033[m" - ;; - esac + + if [[ $silent ]]; then + echo "Updating to ${TARGET}($(git log -1 --format=%h "${TARGET}"))..." + _bash-it_pull_and_update_inner $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 + ;; + [nN]) + echo "Not updating…" + ;; + *) + echo -e "\033[91mPlease choose y or n.\033[m" + ;; + esac + fi else - echo "Bash-it is up to date, nothing to do!" + if [[ ${version} == "stable" ]]; then + echo "You're on the latest stable version. If you want to check out the latest 'dev' version, please run \"bash-it update dev\"" + else + echo "Bash-it is up to date, nothing to do!" + fi fi cd "${old_pwd}" &> /dev/null || return } diff --git a/template/bash_profile.template.bash b/template/bash_profile.template.bash index f8f4705f..4a840df3 100755 --- a/template/bash_profile.template.bash +++ b/template/bash_profile.template.bash @@ -18,6 +18,10 @@ export BASH_IT_THEME='bobby' # cloned bash-it with a remote other than origin such as `bash-it`. # export BASH_IT_REMOTE='bash-it' +# (Advanced): Change this to the name of the main development branch if +# you renamed it or if it was changed for some reason +# export BASH_IT_DEVELOPMENT_BRANCH='master' + # Your place for hosting Git repos. I use this for private repos. export GIT_HOSTING='git@git.domain.com' diff --git a/test/completion/bash-it.completion.bats b/test/completion/bash-it.completion.bats index 4945d939..88d567a6 100644 --- a/test/completion/bash-it.completion.bats +++ b/test/completion/bash-it.completion.bats @@ -58,9 +58,14 @@ function __check_completion () { assert_line -n 0 "vagrant vault vim" } -@test "completion bash-it: update - show no options" { +@test "completion bash-it: update - show options" { run __check_completion 'bash-it update ' - assert_line -n 0 "" + assert_line -n 0 "stable dev" +} + +@test "completion bash-it: update - show optional flags" { + run __check_completion 'bash-it update -' + assert_line -n 0 "-s --silent" } @test "completion bash-it: search - show no options" {