Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(self-update): respect option flags #633

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ jobs:
autoconf automake autotools-dev \
build-essential byacc\
file \
gcc gettext glibc-source grep \
gcc gettext glibc-source grep git \
libc6 libc6-dev libevent-dev libncurses5-dev libncursesw5-dev libtool libuvc0 lua5.1 \
m4 \
ninja-build \
Expand All @@ -58,7 +58,7 @@ jobs:
- name: "install dependencies"
id: install-deps
run: |
brew install --force --overwrite autoconf automake binutils byacc cmake coreutils curl gettext gnu-sed libevent libtool libuv lua [email protected] make ncurses ninja parallel pkg-config texinfo unzip xz zsh
brew install --force --overwrite automake binutils byacc coreutils git gnu-sed libuv lua [email protected] make ninja texinfo unzip zsh
brew link --force --overwrite ncurses

- name: "install zunit"
Expand Down
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
5 changes: 2 additions & 3 deletions tests/_support/bootstrap
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,11 @@ command git diff > ${ZINIT[HOME_DIR]}/unstaged.diff
# info 'creating test env'
git clone \
--quiet \
--depth=1 \
--dissociate \
--no-hardlinks \
--reference "${GIT_REPO}" \
file://${GIT_REPO:A} \
"${ZINIT[BIN_DIR]}" >/dev/null
"${ZINIT[BIN_DIR]}"
if (( $? != 0 )); then
error "Unable to copy ${GIT_REPO} to ${TMP_ZUNIT}" >&2
exit 1
Expand All @@ -56,7 +55,7 @@ if [[ -s $ZINIT[HOME_DIR]/unstaged.diff ]]; then
git -C "${ZINIT[BIN_DIR]}" apply "${ZINIT[HOME_DIR]}/unstaged.diff" && \
chmod g-rwX "${ZINIT[HOME_DIR]}" && \
zcompile "${ZINIT[BIN_DIR]}/zinit.zsh" >/dev/null
) >/dev/null
)
fi
(( $? != 0 )) && { error "Unable to copy ${GIT_REPO} to ${TMP_ZUNIT}" >&2; exit 1 }

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
93 changes: 42 additions & 51 deletions zinit-autoload.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -1833,62 +1833,53 @@ 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 origin/main --not $(git branch --show-current) --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')"})
# } && command git fetch --all --quiet && lines=(${(f)"$(command git --no-pager log $(git branch --show-current) --not origin/main --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')"})
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='--set-upstream 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
Loading