Skip to content

Commit

Permalink
fix(self-update): respect option flags
Browse files Browse the repository at this point in the history
Signed-off-by: Vladislav Doster <mvdoster@gmail.com>
  • Loading branch information
vladdoster committed Jan 26, 2024
1 parent d65206f commit 3463426
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 56 deletions.
5 changes: 5 additions & 0 deletions _zinit
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,11 @@ _zinit_run(){
} # ]]]
# FUNCTION: _zinit_self_update [[[
_zinit_self_update(){
_arguments \
'(-h --help)'{-h,--help}'[Show this help message]' \
'(-n --no-pager)'{-n,--no-pager}'[Do not pipe Git output into a pager]' \
'(-q --quiet)'{-q,--quiet}'[Quiet, suppress feedback messages]' \
&& ret=0
} # ]]]
# FUNCTION: _zinit_snippet [[[
_zinit_snippet(){
Expand Down
8 changes: 5 additions & 3 deletions tests/commands.zunit
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,11 @@
assert $state equals 1
}
@test 'self-update' {
run zinit self-update
assert $output contains 'Already up-to-date.'
assert $state equals 0
run zinit self-update --quiet; assert $state equals 0
assert $output contains 'Updated Zinit'

run zinit self-update; assert $state equals 0
assert $output contains 'Updating Zinit';assert $output contains 'Updated Zinit'
}
@test 'set-debug' {
ZINIT+=(DEBUG 'true')
Expand Down
92 changes: 41 additions & 51 deletions zinit-autoload.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -1833,62 +1833,52 @@ print -- "\nAvailable ice-modifiers:\n\n${ice_order[*]}"
} # ]]]
# FUNCTION: .zinit-self-update [[[
# Updates Zinit code (does a git pull)
.zinit-self-update() {
.zinit-self-update () {
builtin emulate -LR zsh ${=${options[xtrace]:#off}:+-o xtrace}
setopt extendedglob typesetsilent warncreateglobal

if .zi-check-for-git-changes "$ZINIT[BIN_DIR]"; then
[[ $1 = -q ]] && +zi-log "{pre}[self-update]{info} updating zinit repository{msg2}" \

local nl=$'\n' escape=$'\x1b['
local current_branch=$(git -C $ZINIT[BIN_DIR] rev-parse --abbrev-ref HEAD)
# local current_branch='main'
local -a lines
(
builtin cd -q "$ZINIT[BIN_DIR]" \
&& +zi-log -n "{pre}[self-update]{info} fetching latest changes from {obj}$current_branch{info} branch$nl{rst}" \
&& command git fetch --quiet \
&& lines=( ${(f)"$(command git log --color --date=short --pretty=format:'%Cgreen%cd %h %Creset%s %Cred%d%Creset || %b' ..origin/HEAD)"} )
if (( ${#lines} > 0 )); then
# Remove the (origin/main ...) segments, to expect only tags to appear
lines=( "${(S)lines[@]//\(([,[:blank:]]#(origin|HEAD|master|main)[^a-zA-Z]##(HEAD|origin|master|main)[,[:blank:]]#)#\)/}" )
# Remove " ||" if it ends the line (i.e. no additional text from the body)
lines=( "${lines[@]/ \|\|[[:blank:]]#(#e)/}" )
# If there's no ref-name, 2 consecutive spaces occur - fix this
lines=( "${lines[@]/(#b)[[:space:]]#\|\|[[:space:]]#(*)(#e)/|| ${match[1]}}" )
lines=( "${lines[@]/(#b)$escape([0-9]##)m[[:space:]]##${escape}m/$escape${match[1]}m${escape}m}" )
# Replace what follows "|| ..." with the same thing but with no
# newlines, and also only first 10 words (the (w)-flag enables
# word-indexing)
lines=( "${lines[@]/(#b)[[:blank:]]#\|\|(*)(#e)/| ${${match[1]//$nl/ }[(w)1,(w)10]}}" )
(( !OPTS[opt_-q,--quiet] )) && +zi-log "{m} {b}Updating Zinit...{rst}"
local nl=$'\n' escape=$'\x1b['
local -a lines
(
builtin cd -q "$ZINIT[BIN_DIR]" && {
(( !OPTS[opt_-q,--quiet] )) && +zi-log -n "{m} {b}self-update{rst}: fetching latest changes from {obj}main{rst} branch{nl}{rst}"
} && command git fetch --all --quiet && lines=(${(f)"$(command git --no-pager log --color --date=short --pretty='format:%C(yellow)%h%C(auto,magenta)% %C(auto,blue)%>(12,trunc)%ad %C(auto,reset)%s%C(auto,red)% gD% D %C(auto,green)%aN' -- ..origin/main)"})
if (( ${#lines} > 0 )); then
lines=("${(S)lines[@]//\(([,[:blank:]]#(origin|HEAD|master|main)[^a-zA-Z]##(HEAD|origin|master|main)[,[:blank:]]#)#\)/}")
lines=("${lines[@]/ \|\|[[:blank:]]#(#e)/}")
lines=("${lines[@]/(#b)[[:space:]]#\|\|[[:space:]]#(*)(#e)/|| ${match[1]}}")
lines=("${lines[@]/(#b)$escape([0-9]##)m[[:space:]]##${escape}m/$escape${match[1]}m${escape}m}")
lines=("${lines[@]/(#b)[[:blank:]]#\|\|(*)(#e)/| ${${match[1]//$nl/ }[(w)1,(w)10]}}")
(( !OPTS[opt_-q,--quiet] )) && {
builtin print -rl -- "${lines[@]}" | .zinit-pager
builtin print
fi
if [[ $1 != -q ]] {
command git pull --no-stat --ff-only origin main
} else {
command git pull --no-stat --quiet --ff-only origin main
}
)
if [[ $1 != -q ]] {
+zi-log "{pre}[self-update]{info} compiling zinit via {obj}zcompile{rst}"
}
command rm -f $ZINIT[BIN_DIR]/*.zwc(DN)
zcompile -U $ZINIT[BIN_DIR]/zinit.zsh
zcompile -U $ZINIT[BIN_DIR]/zinit-{'side','install','autoload','additional'}.zsh
zcompile -U $ZINIT[BIN_DIR]/share/git-process-output.zsh
# Load for the current session
[[ $1 != -q ]] && +zi-log "{pre}[self-update]{info} reloading zinit for the current session{rst}"

# +zi-log "{pre}[self-update]{info} resetting zinit repository via{rst}: {cmd}${ICE[reset]:-git reset --hard HEAD}{rst}"
source $ZINIT[BIN_DIR]/zinit.zsh
zcompile -U $ZINIT[BIN_DIR]/zinit-{'side','install','autoload'}.zsh
# Read and remember the new modification timestamps
local file
for file ( "" -side -install -autoload ) {
.zinit-get-mtime-into "${ZINIT[BIN_DIR]}/zinit$file.zsh" "ZINIT[mtime$file]"
}
fi
fi
local branch='origin main'
if (( !OPTS[opt_-q,--quiet] )); then
command git pull --autostash --ff-only --no-stat --squash ${=branch}
else
command git pull --autostash --ff-only --no-stat --quiet --squash ${=branch}
fi
# (( $? )) && {
# +zi-log "{w} Unable to update Zinit"
# return 0
# }
)
(( $? )) && {
+zi-log "{w} Unable to update Zinit"
return 0
}
command rm -f ${ZINIT[BIN_DIR]}/*.zwc(DN)
(( !OPTS[opt_-q,--quiet] )) && +zi-log "{m} {b}self-update{rst}: compiling zinit via {obj}zcompile{rst}"
local file
for file in ${ZINIT[BIN_DIR]}/zinit(*).zsh(.N); do
builtin zcompile -Uz ${file:A}
.zinit-get-mtime-into "${f:A}" "ZINIT[mtime${file:t}]"
done
(( !OPTS[opt_-q,--quiet] )) && +zi-log "{m} {b}self-update{rst}: reloading zinit for the current session{rst}"
builtin source ${ZINIT[BIN_DIR]}/zinit.zsh
+zi-log "{i} {b}Updated Zinit{rst}"
} # ]]]

# FUNCTION: .zinit-show-all-reports [[[
Expand Down
4 changes: 2 additions & 2 deletions zinit.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -2605,6 +2605,7 @@ zinit() {
delete "--all|--clean|--help|--quiet|--yes|-a|-c|-h|-q|-y"
env-whitelist "--help|--verbose|-h|-v"
light "--help|-b|-h"
self-update "--help|--no-pager|--quiet|-h|-n|-q"
snippet "--command|--force|--help|-f|-h|-x"
times "--help|-h|-m|-s"
unload "--help|--quiet|-h|-q"
Expand All @@ -2613,8 +2614,7 @@ zinit() {
)

cmd="$1"
if [[ $cmd == (times|unload|env-whitelist|update|snippet|load|light|cdreplay|\
cdclear) ]]; then
if [[ $cmd == (cdclear|cdreplay|env-whitelist|light|load|self-update|snippet|times|unload|update) ]]; then
if (( $@[(I)-*] || OPTS[opt_-h,--help] )); then
.zinit-parse-opts "$cmd" "$@"
if (( OPTS[opt_-h,--help] )); then
Expand Down

0 comments on commit 3463426

Please sign in to comment.