Skip to content

Commit c387517

Browse files
author
Noah Gorny
authored
Merge pull request #1626 from NoahGorny/update-to-stable-or-unstable
bash-it update to stable or dev
2 parents 27702f6 + a1adfaa commit c387517

File tree

5 files changed

+133
-37
lines changed

5 files changed

+133
-37
lines changed

README.md

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,24 @@ Have a look at our [bash-it-docker repository](https://github.com/Bash-it/bash-i
8787

8888
### Updating
8989

90-
To update Bash-it to the latest version, simply run:
90+
To update Bash-it to the latest stable version, simply run:
9191

9292
```bash
93-
bash-it update
93+
bash-it update stable
9494
```
9595

96-
that's all.
96+
If you want to update to the latest dev version (directly from master), run:
97+
98+
```bash
99+
bash-it update dev
100+
```
101+
102+
If you want to update automatically and unattended, you can add the optional
103+
`-s/--silent` flag, for example:
104+
105+
```bash
106+
bash-it update dev --silent
107+
```
97108

98109
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.
99110
For these cases, we provide a `migrate` command:

completion/available/bash-it.completion.bash

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,16 @@ _bash-it-comp()
8787
COMPREPLY=( $(compgen -W "${doctor_args}" -- ${cur}) )
8888
return 0
8989
;;
90-
migrate | reload | search | update | version)
90+
update)
91+
if [[ ${cur} == -* ]];then
92+
local update_args="-s --silent"
93+
else
94+
local update_args="stable dev"
95+
fi
96+
COMPREPLY=( $(compgen -W "${update_args}" -- ${cur}) )
97+
return 0
98+
;;
99+
migrate | reload | search | version)
91100
return 0
92101
;;
93102
enable | disable)

lib/helpers.bash

Lines changed: 98 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ bash-it ()
8080
_bash-it-search $component "$@"
8181
return;;
8282
update)
83-
func=_bash-it_update;;
83+
func=_bash-it_update-$component;;
8484
migrate)
8585
func=_bash-it-migrate;;
8686
version)
@@ -158,60 +158,127 @@ _bash-it-plugins ()
158158
_bash-it-describe "plugins" "a" "plugin" "Plugin"
159159
}
160160

161-
_bash-it_update() {
161+
_bash-it_update-dev() {
162+
_about 'updates Bash-it to the latest master'
163+
_group 'lib'
164+
165+
_bash-it_update- dev "$@"
166+
}
167+
168+
_bash-it_update-stable() {
169+
_about 'updates Bash-it to the latest tag'
170+
_group 'lib'
171+
172+
_bash-it_update- stable "$@"
173+
}
174+
175+
_bash-it_pull_and_update_inner() {
176+
git checkout "$1" &> /dev/null
177+
if [[ $? -eq 0 ]]; then
178+
echo "Bash-it successfully updated."
179+
echo ""
180+
echo "Migrating your installation to the latest $2 version now..."
181+
_bash-it-migrate
182+
echo ""
183+
echo "All done, enjoy!"
184+
bash-it reload
185+
else
186+
echo "Error updating Bash-it, please, check if your Bash-it installation folder (${BASH_IT}) is clean."
187+
fi
188+
}
189+
190+
_bash-it_update-() {
162191
_about 'updates Bash-it'
192+
_param '1: What kind of update to do (stable|dev)'
163193
_group 'lib'
164194

195+
declare silent
196+
for word in $@; do
197+
if [[ ${word} == "--silent" || ${word} == "-s" ]]; then
198+
silent=true
199+
fi
200+
done
165201
local old_pwd="${PWD}"
166202

167203
cd "${BASH_IT}" || return
168204

169-
if [ -z $BASH_IT_REMOTE ]; then
205+
if [ -z "$BASH_IT_REMOTE" ]; then
170206
BASH_IT_REMOTE="origin"
171207
fi
172208

173-
git fetch &> /dev/null
209+
git fetch $BASH_IT_REMOTE --tags &> /dev/null
174210

211+
if [ -z "$BASH_IT_DEVELOPMENT_BRANCH" ]; then
212+
BASH_IT_DEVELOPMENT_BRANCH="master"
213+
fi
214+
# Defaults to stable update
215+
if [ -z "$1" ] || [ "$1" == "stable" ]; then
216+
version="stable"
217+
TARGET=$(git describe --tags "$(git rev-list --tags --max-count=1)" 2> /dev/null)
218+
219+
if [[ -z "$TARGET" ]]; then
220+
echo "Can not find tags, so can not update to latest stable version..."
221+
return
222+
fi
223+
else
224+
version="dev"
225+
TARGET=${BASH_IT_REMOTE}/${BASH_IT_DEVELOPMENT_BRANCH}
226+
fi
227+
228+
declare revision
229+
revision="HEAD..${TARGET}"
175230
declare status
176-
status="$(git rev-list master..${BASH_IT_REMOTE}/master 2> /dev/null)"
231+
status="$(git rev-list ${revision} 2> /dev/null)"
232+
declare revert
233+
234+
if [[ -z "${status}" && ${version} == "stable" ]]; then
235+
revision="${TARGET}..HEAD"
236+
status="$(git rev-list ${revision} 2> /dev/null)"
237+
revert=true
238+
fi
177239

178240
if [[ -n "${status}" ]]; then
241+
if [[ $revert ]]; then
242+
echo "Your version is a more recent development version ($(git log -1 --format=%h HEAD))"
243+
echo "You can continue in order to revert and update to the latest stable version"
244+
echo ""
245+
log_color="%Cred"
246+
fi
179247

180-
for i in $(git rev-list --merges --first-parent master..${BASH_IT_REMOTE}); do
248+
for i in $(git rev-list --merges --first-parent ${revision}); do
181249
num_of_lines=$(git log -1 --format=%B $i | awk 'NF' | wc -l)
182250
if [ $num_of_lines -eq 1 ]; then
183251
description="%s"
184252
else
185253
description="%b"
186254
fi
187-
git log --format="%h: $description (%an)" -1 $i
255+
git log --format="${log_color}%h: $description (%an)" -1 $i
188256
done
189257
echo ""
190-
read -e -n 1 -p "Would you like to update to $(git log -1 --format=%h origin/master)? [Y/n] " RESP
191-
case $RESP in
192-
[yY]|"")
193-
git pull --rebase &> /dev/null
194-
if [[ $? -eq 0 ]]; then
195-
echo "Bash-it successfully updated."
196-
echo ""
197-
echo "Migrating your installation to the latest version now..."
198-
_bash-it-migrate
199-
echo ""
200-
echo "All done, enjoy!"
201-
bash-it reload
202-
else
203-
echo "Error updating Bash-it, please, check if your Bash-it installation folder (${BASH_IT}) is clean."
204-
fi
205-
;;
206-
[nN])
207-
echo "Not upgrading…"
208-
;;
209-
*)
210-
echo -e "\033[91mPlease choose y or n.\033[m"
211-
;;
212-
esac
258+
259+
if [[ $silent ]]; then
260+
echo "Updating to ${TARGET}($(git log -1 --format=%h "${TARGET}"))..."
261+
_bash-it_pull_and_update_inner $TARGET $version
262+
else
263+
read -e -n 1 -p "Would you like to update to ${TARGET}($(git log -1 --format=%h "${TARGET}"))? [Y/n] " RESP
264+
case $RESP in
265+
[yY]|"")
266+
_bash-it_pull_and_update_inner $TARGET $version
267+
;;
268+
[nN])
269+
echo "Not updating…"
270+
;;
271+
*)
272+
echo -e "\033[91mPlease choose y or n.\033[m"
273+
;;
274+
esac
275+
fi
213276
else
214-
echo "Bash-it is up to date, nothing to do!"
277+
if [[ ${version} == "stable" ]]; then
278+
echo "You're on the latest stable version. If you want to check out the latest 'dev' version, please run \"bash-it update dev\""
279+
else
280+
echo "Bash-it is up to date, nothing to do!"
281+
fi
215282
fi
216283
cd "${old_pwd}" &> /dev/null || return
217284
}

template/bash_profile.template.bash

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ export BASH_IT_THEME='bobby'
1818
# cloned bash-it with a remote other than origin such as `bash-it`.
1919
# export BASH_IT_REMOTE='bash-it'
2020

21+
# (Advanced): Change this to the name of the main development branch if
22+
# you renamed it or if it was changed for some reason
23+
# export BASH_IT_DEVELOPMENT_BRANCH='master'
24+
2125
# Your place for hosting Git repos. I use this for private repos.
2226
export GIT_HOSTING='[email protected]'
2327

test/completion/bash-it.completion.bats

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,14 @@ function __check_completion () {
5858
assert_line -n 0 "vagrant vault vim"
5959
}
6060

61-
@test "completion bash-it: update - show no options" {
61+
@test "completion bash-it: update - show options" {
6262
run __check_completion 'bash-it update '
63-
assert_line -n 0 ""
63+
assert_line -n 0 "stable dev"
64+
}
65+
66+
@test "completion bash-it: update - show optional flags" {
67+
run __check_completion 'bash-it update -'
68+
assert_line -n 0 "-s --silent"
6469
}
6570

6671
@test "completion bash-it: search - show no options" {

0 commit comments

Comments
 (0)