Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: [PATCH] New completion tag: __git_recent_branches



Matthew Martin wrote on Mon, Jun 06, 2016 at 08:42:21 -0500:
> On Fri, Jun 03, 2016 at 08:40:49PM +0000, Daniel Shahaf wrote:
> > # This function returns in $reply recently-checked-out refs' names, in order
> > # from most to least recent.
> > __git_recent_branches__names()
> > {
> >     local -a reflog
> >     local reflog_subject
> >     local new_head
> >     local -A seen
> >     reply=()
> > 
> >     reflog=(${(ps:\0:)"$(_call_program reflog git reflog -1000 -z --grep-reflog='\^checkout:\ moving\ from\ ' --pretty='%gs' 2>/dev/null)"})
> >     for reflog_subject in $reflog; do
> >       new_head=${${=reflog_subject}[4]}
> > 
> >       # Skip values added in previous iterations.
> >       if (( ${+seen[$new_head]} )); then
> >         continue
> >       fi
> >       seen[$new_head]="" # value is ignored
> > 
> >       # Filter out hashes, to leave only ref names.
> >       if [[ $new_head =~ '^[0-9a-f]{40}$' ]]; then
> >         continue
> >       fi
> > 
> >       # All checks passed.  Add it.
> >       reply+=( $new_head )
> >     done
> > }
> 
> reply=(${${(u)${${(0)"$(_call_program reflog git reflog -1000 -z --grep-reflog='\^checkout:\ moving\ from\ ' --pretty='%gs')"}#checkout: moving from }%% *}:#[[:xdigit]](#c40)})
> 
> time (repeat 100; __git_recent_branches__names) uses about 4.32 seconds;
> time (repeat 100; __git_recent_branches__names2) uses about 3.64
> seconds on one of my more used repos.
> 
> Slight speed improvement for reduced readability. Although it does seem
> to match _git's style better.

I think the cost center will be either the 'reflog' invocation itself,
or the 'repeat 1000 `git`' loop in the parent function, but all the
same, a 16% improvement on a large/active clone is significant.  I'll
make the change.

I added the missing colon in [[:xdigit:]].  (Although [0-9a-f] seems,
paradoxically, to be faster...)

Thanks,

Daniel



Messages sorted by: Reverse Date, Date, Thread, Author