zsh-workers
 help / color / mirror / code / Atom feed
* [PATCH] New completion tag: __git_recent_branches
@ 2016-06-03 20:40 Daniel Shahaf
  2016-06-06 13:42 ` Matthew Martin
  0 siblings, 1 reply; 3+ messages in thread
From: Daniel Shahaf @ 2016-06-03 20:40 UTC (permalink / raw)
  To: zsh-workers; +Cc: Nils Luxton

Nils and I have been looking into a "recent branches" completion
function for git; it provides the following functionality:

[[[
% () { for i; git checkout -b br$i && git commit --allow-empty -m "Commit $i" } foo{1..3} 
Switched to a new branch 'brfoo1'
[brfoo1 2499cbf] Commit foo1
Switched to a new branch 'brfoo2'
[brfoo2 15b3dfd] Commit foo2
Switched to a new branch 'brfoo3'
[brfoo3 371fa70] Commit foo3

% compdef __git_recent_branches f
% f <TAB>
> recent branches
brfoo2  - Commit foo2
brfoo1  - Commit foo1
master  - Initial import
]]]

The code is attached (history at [1,2]).  I'd like to add these two
functions to _git.  I don't plan to have _git call these two functions
yet.

The "2" in the function name will go away.  The hardcoded "-1000" is
precedented elsewhere in _git (won't be hard to make it configureable).

Cheers,

Daniel

[1] https://github.com/ascii-soup/zsh-git-recent-branches
[2] https://github.com/danielshahaf/zsh-git-recent-branches



# 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
}

__git_recent_branches2() {
    local -a branches descriptions
    local branch description
    local -a reply

    __git_recent_branches__names \
    ; for branch in $reply
    do
        # ### We'd want to convert all $reply to $descriptions in one shot,
        # ### 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.
        # ### Let's hope the user is well-stocked on cutlery.
        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.
        if [[ -n "$description" ]]; then
          branches+=$branch
          descriptions+="${branch}:${description/:/\:}"
        fi
    done

    _describe -V -t recent-branches "recent branches" descriptions branches
}


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] New completion tag: __git_recent_branches
  2016-06-03 20:40 [PATCH] New completion tag: __git_recent_branches Daniel Shahaf
@ 2016-06-06 13:42 ` Matthew Martin
  2016-06-07 22:53   ` Daniel Shahaf
  0 siblings, 1 reply; 3+ messages in thread
From: Matthew Martin @ 2016-06-06 13:42 UTC (permalink / raw)
  To: zsh-workers

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.

- Matthew Martin


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] New completion tag: __git_recent_branches
  2016-06-06 13:42 ` Matthew Martin
@ 2016-06-07 22:53   ` Daniel Shahaf
  0 siblings, 0 replies; 3+ messages in thread
From: Daniel Shahaf @ 2016-06-07 22:53 UTC (permalink / raw)
  To: zsh-workers

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


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2016-06-07 22:53 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-06-03 20:40 [PATCH] New completion tag: __git_recent_branches Daniel Shahaf
2016-06-06 13:42 ` Matthew Martin
2016-06-07 22:53   ` Daniel Shahaf

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).