diff --git a/_zinit b/_zinit index 1e86f6996..eeb985975 100644 --- a/_zinit +++ b/_zinit @@ -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(){ diff --git a/tests/commands.zunit b/tests/commands.zunit index 6f533de0b..a247d6042 100644 --- a/tests/commands.zunit +++ b/tests/commands.zunit @@ -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') diff --git a/zinit-autoload.zsh b/zinit-autoload.zsh index cad85f3b0..4c819eb9f 100644 --- a/zinit-autoload.zsh +++ b/zinit-autoload.zsh @@ -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 [[[ diff --git a/zinit.zsh b/zinit.zsh index 387f35fb9..b3f6a2887 100644 --- a/zinit.zsh +++ b/zinit.zsh @@ -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" @@ -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