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

completions/zsh: Improve caching behaviour #5842

Merged
merged 1 commit into from
Mar 6, 2019
Merged
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
41 changes: 28 additions & 13 deletions completions/zsh/_brew
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,19 @@ __brew_formulae_or_ruby_files() {

# completions remain in cache until any tap has new commits
__brew_completion_caching_policy() {
# rebuild cache if no cache file exists (anyway we cannot proceed further down)
! [[ -f "$1" ]] && return 0
# cache file modification date (seconds since epoch)
local -i cache_mtime=$(date -r "$1" +%s)
# latest modified homebrew tap index file
local latest_modified_git_index=(${HOMEBREW_REPOSITORY:-/usr/local/Homebrew}/Library/Taps/*/*/.git/index(om[1]))
local -i commit_mtime=$(date -r "$latest_modified_git_index" +%s)
(( $cache_mtime < $commit_mtime ))
local -a tmp

# invalidate if cache file is missing or >=2 weeks old
tmp=( $1(mw-2N) )
(( $#tmp )) || return 0

# otherwise, invalidate if latest tap index file is missing or newer than
# cache file
tmp=( ${HOMEBREW_REPOSITORY:-/usr/local/Homebrew}/Library/Taps/*/*/.git/index(om[1]N) )
[[ -z $tmp || $tmp -nt $1 ]]
}

__brew_formulae() {
zstyle ":completion:${curcontext}:" cache-policy __brew_completion_caching_policy
local -a formulae
local comp_cachename=brew_formulae
if _cache_invalid $comp_cachename || ! _retrieve_cache $comp_cachename; then
Expand Down Expand Up @@ -147,7 +148,6 @@ __brew_common_commands() {
}

__brew_all_commands() {
zstyle ":completion:${curcontext}:" cache-policy __brew_completion_caching_policy
local -a commands
local comp_cachename=brew_all_commands
if _cache_invalid $comp_cachename || ! _retrieve_cache $comp_cachename; then
Expand Down Expand Up @@ -808,15 +808,23 @@ _brew_vendor_install() {
# the main completion function
_brew() {
local curcontext="$curcontext" state state_descr line expl
local ret=1
local tmp ret=1

_arguments -C : \
'(-v)-v[verbose]' \
'1:command:->command' \
'*::options:->options' && return 0

case "$state" in
command) __brew_commands && return 0 ;;
command)
# set default cache policy
zstyle -s ":completion:${curcontext%:*}:*" cache-policy tmp
[[ -n $tmp ]] ||
zstyle ":completion:${curcontext%:*}:*" cache-policy \
__brew_completion_caching_policy

__brew_commands && return 0
;;
options)
local command_or_alias command
local -A aliases
Expand All @@ -827,7 +835,14 @@ _brew() {
command="${aliases[$command_or_alias]:-$command_or_alias}"

# change context to e.g. brew-list
curcontext="${curcontext%:*:*}:brew-${command}"
curcontext="${curcontext%:*}-${command}:${curcontext##*:}"

# set default cache policy (we repeat this dance because the context
# service differs from above)
zstyle -s ":completion:${curcontext%:*}:*" cache-policy tmp
[[ -n $tmp ]] ||
zstyle ":completion:${curcontext%:*}:*" cache-policy \
__brew_completion_caching_policy

# call completion for named command e.g. _brew_list
local completion_func="_brew_${command//-/_}"
Expand Down