zsh-workers
 help / color / mirror / code / Atom feed
From: "Miroslav Koškár" <mk@mkoskar.com>
To: zsh-workers@zsh.org
Cc: d.s@daniel.shahaf.name
Subject: [PATCH 3/4] _git: Fix handling of aliases
Date: Sat, 27 Jun 2020 08:12:17 +0200	[thread overview]
Message-ID: <a70b746452dfebcfb7436d9c4a96755c65712dae.1593236365.git.mk@mkoskar.com> (raw)
In-Reply-To: <cover.1593236365.git.mk@mkoskar.com>

* nested aliases
* aliases preceded with options e.g., -c <name>=<value>
---
 Completion/Unix/Command/_git | 73 ++++++++++++++++--------------------
 1 file changed, 33 insertions(+), 40 deletions(-)

diff --git a/Completion/Unix/Command/_git b/Completion/Unix/Command/_git
index 602d0d0..e2d3d6e 100644
--- a/Completion/Unix/Command/_git
+++ b/Completion/Unix/Command/_git
@@ -8082,35 +8082,8 @@ __git_color_attributes () {
 
 # Now, for the main drive...
 _git() {
-  if (( CURRENT > 2 )); then
-    local -a aliases
-    local -A git_aliases
-    local a k v
-    local endopt='!(-)--end-of-options'
-    aliases=(${(0)"$(_call_program aliases git config -z --get-regexp '\^alias\\.')"})
-    for a in ${aliases}; do
-        k="${${a/$'\n'*}/alias.}"
-        v="${a#*$'\n'}"
-        git_aliases[$k]="$v"
-    done
-
-    if (( $+git_aliases[$words[2]] && !$+commands[git-$words[2]] && !$+functions[_git-$words[2]] )); then
-      local -a tmpwords expalias
-      expalias=(${(z)git_aliases[$words[2]]})
-      tmpwords=(${words[1]} ${expalias})
-      if [[ -n "${words[3,-1]}" ]] ; then
-          tmpwords+=(${words[3,-1]})
-      fi
-      [[ -n ${words[$CURRENT]} ]] || tmpwords+=('')
-      (( CURRENT += ${#expalias} - 1 ))
-      words=("${tmpwords[@]}")
-      unset tmpwords expalias
-    fi
-
-    unset git_aliases aliases
-  fi
-
   integer ret=1
+  local -a allwords=("${words[@]}")
 
   if [[ $service == git ]]; then
     local curcontext=$curcontext state line
@@ -8143,18 +8116,38 @@ _git() {
         _git_commands && ret=0
         ;;
       (option-or-argument)
-        curcontext=${curcontext%:*:*}:git-$words[1]:
-        (( $+opt_args[--git-dir] )) && local -x GIT_DIR=${(Q)${~opt_args[--git-dir]}}
-        (( $+opt_args[--work-tree] )) && local -x GIT_WORK_TREE=${(Q)${~opt_args[--work-tree]}}
-	if ! _call_function ret _git-$words[1]; then
-	  if [[ $words[1] = \!* ]]; then
-	    words[1]=${words[1]##\!}
-	    _normal && ret=0
-	  elif zstyle -T :completion:$curcontext: use-fallback; then
-	    _default && ret=0
-	  else
-	    _message "unknown sub-command: $words[1]"
-	  fi
+        local -a aliases
+        local -A git_aliases
+        local a k v
+        local endopt='!(-)--end-of-options'
+        aliases=(${(0)"$(_call_program aliases git config -z --get-regexp '\^alias\\.')"})
+        for a in ${aliases}; do
+            k="${${a/$'\n'*}/alias.}"
+            v="${a#*$'\n'}"
+            git_aliases[$k]="$v"
+        done
+        local git_alias=git_aliases[\$words[1]]
+        if (( ${(P)+git_alias} && !$+commands[git-$words[1]] && !$+functions[_git-$words[1]] )); then
+          git_alias=${(P)git_alias}
+          local len=$#words
+          if [[ $git_alias = \!* ]]; then
+            words=("${(z)git_alias##\!}" "${words[@]:1}")
+          else
+            words=("${allwords[@]:0:-$#words}" "${(z)git_alias}" "${words[@]:1}")
+          fi
+          (( CURRENT += $#words - len ))
+          _normal && ret=0
+        else
+          curcontext=${curcontext%:*:*}:git-$words[1]:
+          (( $+opt_args[--git-dir] )) && local -x GIT_DIR=${(Q)${~opt_args[--git-dir]}}
+          (( $+opt_args[--work-tree] )) && local -x GIT_WORK_TREE=${(Q)${~opt_args[--work-tree]}}
+          if ! _call_function ret _git-$words[1]; then
+            if zstyle -T :completion:$curcontext: use-fallback; then
+              _default && ret=0
+            else
+              _message "unknown sub-command: $words[1]"
+            fi
+          fi
         fi
         ;;
       (configuration)
-- 
2.27.0



  parent reply	other threads:[~2020-06-27  6:15 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-06-24 11:12 [PATCH] _git: Improve " Miroslav Koškár
2020-06-24 11:56 ` Daniel Shahaf
2020-06-26 10:17   ` Miroslav Koškár
2020-06-26 17:00     ` Daniel Shahaf
2020-06-27  6:12       ` [PATCH 0/4] " Miroslav Koškár
2020-06-27  6:12         ` [PATCH 1/4] _git: Remove hanging whitespaces Miroslav Koškár
2020-06-27  6:12         ` [PATCH 2/4] _git: Fix insufficiently quoted pattern Miroslav Koškár
2020-06-27  6:12         ` Miroslav Koškár [this message]
2020-06-27  6:12         ` [PATCH 4/4] _git: Don't shadow global aliases Miroslav Koškár
2020-06-27  9:04       ` _git: Improve handling of aliases Miroslav Koškár
2020-06-27 20:39         ` 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=a70b746452dfebcfb7436d9c4a96755c65712dae.1593236365.git.mk@mkoskar.com \
    --to=mk@mkoskar.com \
    --cc=d.s@daniel.shahaf.name \
    --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).