zsh-workers
 help / color / mirror / code / Atom feed
* [PATCH] __git_recent_branches: Optimise.
@ 2016-08-25  0:59 Daniel Shahaf
  2016-08-30  3:36 ` [PATCH] __git_recent_branches: Silence warning on an edge case Daniel Shahaf
  0 siblings, 1 reply; 2+ messages in thread
From: Daniel Shahaf @ 2016-08-25  0:59 UTC (permalink / raw)
  To: zsh-workers; +Cc: Daniel Hahler

This improves performance from 0.6s to 0.04s (+93%) on one of Daniel Hahler's repositories.
---
 Completion/Unix/Command/_git | 42 +++++++++++++++++++++++-------------------
 1 file changed, 23 insertions(+), 19 deletions(-)

diff --git a/Completion/Unix/Command/_git b/Completion/Unix/Command/_git
index 40b2c11..66fc557 100644
--- a/Completion/Unix/Command/_git
+++ b/Completion/Unix/Command/_git
@@ -6051,35 +6051,39 @@ __git_recent_branches__names()
   #
   # See workers/38592 for an equivalent long-hand implementation, and the rest
   # 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)})
 }
 
 (( $+functions[__git_recent_branches] )) ||
 __git_recent_branches() {
   local -a branches descriptions
-  local branch description
   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 \
-  ; 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.
-      # ### 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.
-      if [[ -n "$description" ]]; then
-        branches+=$branch
-        descriptions+="${branch//:/\:}:${description}"
-      fi
+  # 1. Obtain names of recently-checked-out branches from the reflog.
+  # 2. Remove ref names that that no longer exist from the list.
+  #    (We must do this because #3 would otherwise croak on them.)
+  __git_recent_branches__names; branches=( ${(@)reply:*valid_ref_names_munged} )
+
+  # 3. Obtain log messages for all of them in one shot.
+  descriptions=( ${(f)"$(_call_program all-descriptions git --no-pager log --no-walk=unsorted --pretty=%s ${(q)branches} --)"} )
+
+  if (( $#branches != $#descriptions )); then
+    # ### 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
 
-  _describe -V -t recent-branches "recent branches" descriptions branches
+  _describe -V -t recent-branches "recent branches" branches_colon_descriptions
 }
 
 (( $+functions[__git_commits_prefer_recent] )) ||


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

* [PATCH] __git_recent_branches: Silence warning on an edge case.
  2016-08-25  0:59 [PATCH] __git_recent_branches: Optimise Daniel Shahaf
@ 2016-08-30  3:36 ` Daniel Shahaf
  0 siblings, 0 replies; 2+ messages in thread
From: Daniel Shahaf @ 2016-08-30  3:36 UTC (permalink / raw)
  To: zsh-workers; +Cc: Daniel Hahler

(The warning was correct; there is no functional change, though.)
---
 Completion/Unix/Command/_git | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)

diff --git a/Completion/Unix/Command/_git b/Completion/Unix/Command/_git
index afa3bcb..8d3bd63 100644
--- a/Completion/Unix/Command/_git
+++ b/Completion/Unix/Command/_git
@@ -6070,7 +6070,14 @@ __git_recent_branches() {
   #    (We must do this because #3 would otherwise croak on them.)
   __git_recent_branches__names; branches=( ${(@)reply:*valid_ref_names_munged} )
 
-  # 3. Obtain log messages for all of them in one shot.
+  # 3. Early out if no matches.
+  if ! (( $+branches[1] )); then
+    # This can happen in a fresh repository (immediately after 'clone' or 'init') before
+    # any 'checkout' commands were run in it.
+    return 1
+  fi
+
+  # 4. Obtain log messages for all of them in one shot.
   descriptions=( ${(f)"$(_call_program all-descriptions git --no-pager log --no-walk=unsorted --pretty=%s ${(q)branches} --)"} )
 
   if (( $#branches != $#descriptions )); then
@@ -6079,7 +6086,7 @@ __git_recent_branches() {
     return 1
   fi
 
-  # 4. Synthesize the data structure _describe wants.
+  # 5. Synthesize the data structure _describe wants.
   local -a branches_colon_descriptions
   local branch description
   for branch description in ${branches:^descriptions} ; do


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

end of thread, other threads:[~2016-08-30  3:37 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-08-25  0:59 [PATCH] __git_recent_branches: Optimise Daniel Shahaf
2016-08-30  3:36 ` [PATCH] __git_recent_branches: Silence warning on an edge case 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).