From: Daniel Shahaf <d.s@daniel.shahaf.name>
To: zsh-workers@zsh.org
Cc: Nils Luxton <nils.luxton@gmail.com>
Subject: [PATCH] New completion tag: __git_recent_branches
Date: Fri, 03 Jun 2016 20:40:49 +0000 [thread overview]
Message-ID: <20160603204049.GA22304@tarsus.local2> (raw)
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
}
next reply other threads:[~2016-06-03 20:40 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-06-03 20:40 Daniel Shahaf [this message]
2016-06-06 13:42 ` Matthew Martin
2016-06-07 22:53 ` Daniel Shahaf
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20160603204049.GA22304@tarsus.local2 \
--to=d.s@daniel.shahaf.name \
--cc=nils.luxton@gmail.com \
--cc=zsh-workers@zsh.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).