Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: git checkout @... completion
- X-seq: zsh-workers 34831
 
- From: Daniel Shahaf <d.s@xxxxxxxxxxxxxxxxxx>
 
- To: Robbie Gates <robbie@xxxxxxx>
 
- Subject: Re: git checkout @... completion
 
- Date: Wed, 1 Apr 2015 09:41:13 +0000
 
- Cc: zsh-workers@xxxxxxx
 
- Dkim-signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d=	daniel.shahaf.name; h=cc:content-type:date:from:in-reply-to	:message-id:mime-version:references:subject:to:x-sasl-enc	:x-sasl-enc; s=mesmtp; bh=dDArKSJDOZDIeU1nXL1Gll7f3XU=; b=Nq846r	KXIv7GneeQyT6M851EyYsAUCe1beeTz4X3RdPurfMtMVisQxYSE/GrLawvlyRrta	3pq4QSaQRjgMroZKqlY+Oz3v+FlpZ0/m3vk8FCE2CJuB+ipTlFPMrkah0epjKXGP	muygfYXKkKFuzFGbm9UezndghQRANONxgi0TA=
 
- Dkim-signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d=	messagingengine.com; h=cc:content-type:date:from:in-reply-to	:message-id:mime-version:references:subject:to:x-sasl-enc	:x-sasl-enc; s=smtpout; bh=dDArKSJDOZDIeU1nXL1Gll7f3XU=; b=dZdda	WLDwp7L0I2SQalNU4DDO+wbpALTUCKeV05QXllEKLHi9dW6L9KpcDTJVExT5TV63	U0JZDGwpxjKwAJI1g0IPWoesu8H65xlD95fhGxE3bWL9fswZ+j6ihC+1xlimrq+/	UyzApgxEdPCWG/LP7KvpnxRu+Po7Nouyf3U49w=
 
- In-reply-to: <CAH6YsRe33+_Y6O5WSq_DjP=AouXVJ8=QGDHwM+mb6=1301=aVw@mail.gmail.com>
 
- List-help: <mailto:zsh-workers-help@zsh.org>
 
- List-id: Zsh Workers List <zsh-workers.zsh.org>
 
- List-post: <mailto:zsh-workers@zsh.org>
 
- Mailing-list: contact zsh-workers-help@xxxxxxx; run by ezmlm
 
- References: <CAH6YsRe33+_Y6O5WSq_DjP=AouXVJ8=QGDHwM+mb6=1301=aVw@mail.gmail.com>
 
Robbie Gates wrote on Tue, Mar 31, 2015 at 22:10:42 +1100:
> I've attached a patch (against a local copy) - please let me
> know if it's better to submit changes as a PR and i'll do that.
Patches are preferred.
>   In particular, it could be rewritten to loop over git check-ref-format
> --branch "@{-$i}" for increasing i until a given (by completion style
> maybe, or environment variable) number of unique previous branches was
The number of external command invocations should be minimized,
therefore, this doesn't sound like a good approach.
> Any pointers to completions with a configurable depth / length that
> i could use to glean the right way to do this kind of thing welcome.
I don't have a concrete example in mind, but grepping for uses of
'zstyle -s' might turn one up.
I'm unsure this should be made configurable.  Hardcoding a small
constant might be better.  I suspect 9 is too small, but not having used
this patch "in the field" that's just a guess.
On the other hand, __git_recent_commits had exactly the same problem:
what argument to pass to 'git log --limit=N'.  So perhaps a "limit"
style, or some such, would be justified, which both of these (and other
places) could use.
> diff --git a/functions/_git b/functions/_git
Please ensure attachments have the text/plain MIME type.  Usually naming
them with a "*.txt" extension does that.  Thanks.
> index c4e386b..884756c 100644
> --- a/functions/_git
> +++ b/functions/_git
What is the patch relative to?  There is no "functions/_git" in zsh's
repository [git://git.code.sf.net/p/zsh/code].
> @@ -5769,6 +5779,14 @@ __git_notes_refs () {
>    _wanted notes-refs expl 'notes ref' compadd $* - $notes_refs
>  }
>  
> +(( $+functions[__git_previous_branches] )) ||
> +__git_previous_branches () {
> +  declare -a previous_branches
> +  # We don't need to escape : in branch names, because they can't be there (see git help check-ref-format)
> +  previous_branches=(${(f)"$(_call_program previous-branches "git log --max-count=9 --walk-reflogs --grep-reflog '^checkout: moving from .* to' --format='%gs' 2>/dev/null" | awk '{if (!seen[$4]) { seen[$4]=1 ; print "@{-" NR "}:" $4 } }')"})
I see some other completions use awk, but I think it would be better to
avoid it, if possible; and here it's easily possible.  Perhaps something
like this:
    previous_branches=( ${${(f)"$(git reflog -9 --grep-reflog checkout: )"}##* } )
and then arrange the uniquification and @{-N} prefixes in zsh code,
through a unique array ('local -Ua') or an associative array.
> +  _describe "previous branch" previous_branches
Note that if the limit would be greater than 9, _describe would by
default sort the matches alphabetically, not numerically.  See
__git_recent_commits for an example of using _describe while retaining
the array sort order (in this case, the numerical order).  The problem
discussed in workers/34768 won't be a concern here because you already
uniquify the descriptions.
When reflog contains a detached head, the patch prints the full 40-digit
hash.  Is there a cheap way to shorten it into an unambiguous prefix?
I imagine in the future this could be extended to print not just
"checkout:" reflog entries, but also other entries.  Use-case: going to
the point before a rebase (which may be a commit, rather than
a checkout).
Thanks for the patch.  Looking forward to the revised version.
Daniel
(I'm going to be on-and-off AFK for the next couple of weeks, so I might
be late to reply.)
Messages sorted by:
Reverse Date,
Date,
Thread,
Author