mirror of
git://git.code.sf.net/p/zsh/code
synced 2025-09-29 19:00:57 +02:00
39102: __git_recent_branches: Optimise.
This improves performance from 0.6s to 0.04s (+93%) on one of Daniel Hahler's repositories.
This commit is contained in:
parent
4d995160e4
commit
317c96b64f
2 changed files with 27 additions and 18 deletions
|
@ -1,3 +1,8 @@
|
||||||
|
2016-08-25 Daniel Shahaf <d.s@daniel.shahaf.name>
|
||||||
|
|
||||||
|
* 39102: Completion/Unix/Command/_git: __git_recent_branches:
|
||||||
|
Optimise.
|
||||||
|
|
||||||
2016-08-24 Daniel Shahaf <d.s@daniel.shahaf.name>
|
2016-08-24 Daniel Shahaf <d.s@daniel.shahaf.name>
|
||||||
|
|
||||||
* 39094: Completion/Unix/Command/_git: _git-config: When an
|
* 39094: Completion/Unix/Command/_git: _git-config: When an
|
||||||
|
|
|
@ -6054,35 +6054,39 @@ __git_recent_branches__names()
|
||||||
#
|
#
|
||||||
# See workers/38592 for an equivalent long-hand implementation, and the rest
|
# See workers/38592 for an equivalent long-hand implementation, and the rest
|
||||||
# of that thread for why this implementation was chosen instead.
|
# of that thread for why this implementation was chosen instead.
|
||||||
|
#
|
||||||
|
# Note: since we obtain the "from" part of the reflog, we only obtain heads, not tags.
|
||||||
reply=(${${(u)${${(0)"$(_call_program reflog git reflog -1000 -z --grep-reflog='\^checkout:\ moving\ from\ ' --pretty='%gs')"}#checkout: moving from }%% *}:#[[:xdigit:]](#c40)})
|
reply=(${${(u)${${(0)"$(_call_program reflog git reflog -1000 -z --grep-reflog='\^checkout:\ moving\ from\ ' --pretty='%gs')"}#checkout: moving from }%% *}:#[[:xdigit:]](#c40)})
|
||||||
}
|
}
|
||||||
|
|
||||||
(( $+functions[__git_recent_branches] )) ||
|
(( $+functions[__git_recent_branches] )) ||
|
||||||
__git_recent_branches() {
|
__git_recent_branches() {
|
||||||
local -a branches descriptions
|
local -a branches descriptions
|
||||||
local branch description
|
|
||||||
local -a reply
|
local -a reply
|
||||||
|
local -aU valid_ref_names_munged=( ${"${(f)"$(_call_program valid-ref-names 'git for-each-ref --format="%(refname)" refs/heads/ refs/tags/')"}"#refs/(heads|tags)/} )
|
||||||
|
|
||||||
__git_recent_branches__names \
|
# 1. Obtain names of recently-checked-out branches from the reflog.
|
||||||
; for branch in $reply
|
# 2. Remove ref names that that no longer exist from the list.
|
||||||
do
|
# (We must do this because #3 would otherwise croak on them.)
|
||||||
# ### We'd want to convert all $reply to $descriptions in one shot,
|
__git_recent_branches__names; branches=( ${(@)reply:*valid_ref_names_munged} )
|
||||||
# ### with this:
|
|
||||||
# ### array=("${(ps:\0:)"$(_call_program descriptions git --no-pager log --no-walk=unsorted -z --pretty=%s ${(q)reply} --)"}")
|
|
||||||
# ### , but git croaks if any of the positional arguments is a ref name
|
|
||||||
# ### that has been deleted. (So does 'git rev-parse'.)
|
|
||||||
# ### Hence, we resort to fetching the descriptions one-by-one.
|
|
||||||
# ### This would be costly if fork() is expensive.
|
|
||||||
description="$(_call_program description git --no-pager log --no-walk=unsorted --pretty=%s ${(q)branch} --)"
|
|
||||||
|
|
||||||
# If the ref has been deleted, $description would be empty.
|
# 3. Obtain log messages for all of them in one shot.
|
||||||
if [[ -n "$description" ]]; then
|
descriptions=( ${(f)"$(_call_program all-descriptions git --no-pager log --no-walk=unsorted --pretty=%s ${(q)branches} --)"} )
|
||||||
branches+=$branch
|
|
||||||
descriptions+="${branch//:/\:}:${description}"
|
if (( $#branches != $#descriptions )); then
|
||||||
fi
|
# ### Trouble...
|
||||||
|
zle -M "__git_recent_branches: \$#branches != \$#descriptions"
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
# 4. Synthesize the data structure _describe wants.
|
||||||
|
local -a branches_colon_descriptions
|
||||||
|
local branch description
|
||||||
|
for branch description in ${branches:^descriptions} ; do
|
||||||
|
branches_colon_descriptions+="${branch//:/\:}:${description}"
|
||||||
done
|
done
|
||||||
|
|
||||||
_describe -V -t recent-branches "recent branches" descriptions branches
|
_describe -V -t recent-branches "recent branches" branches_colon_descriptions
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue