Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: [PATCH] Remove redundancies from `git` completion



On Wed, Sep 1, 2021 at 4:56 PM Daniel Shahaf <d.s@xxxxxxxxxxxxxxxxxx> wrote:
>
> Marlon Richert wrote on Mon, Aug 30, 2021 at 14:17:17 +0300:
> > In _git-show, remove calls to __git_commits and __git_tags.
> >   Rationale: 'commits' and 'tags' are already added by __git_trees.
> >   Adding them twice makes `zstyle ... tag-order` give unexpected
> >   results.
> >
>
> Complete example, please?

Before the patch, setting `zstyle '*' tag-order '! commit-tags
heads-remote'` failed to hide 'commit-tags' and 'heads-remote' from
`git show` completion. After the patch, this works correctly and will
show 'commit-tags' or 'heads-remote' only if other tags produce no
matches.

>
> > In __git_recent_commits, remove the line that adds 'heads'.
> >   Rationale: The completion for most subcommands already adds
> >   'heads-local' and 'heads-remote'. Adding an additional 'heads' inside
> >   __git_recent_commits results in heads being listed twice in two
> >   separate groups.
>
> Complete example, please?

Before the patch, `git log` completion listed groups 'local head',
'remote head', then other groups, and then group 'head', which
repeated items from the first two groups. After the patch, the last
group is omitted.

> Also, what about those subcommands that
> *don't* already add heads-local and heads-remote?

Good observation. Those would be the completion functions for `git
commit --fixup`, `git commit --squash`, `git revert` and `git
send-email`. My patch tried to handle that case, too, but some more
testing revealed that it didn't work correctly.

Here is a new patch that fixes this by making these four completions
use __git_commits, which prefers recent commits and tags, but can also
complete any other commits, tags or heads.
From 4442829b53506d705e5ffc5ea2306fe9116d6474 Mon Sep 17 00:00:00 2001
From: Marlon Richert <marlonrichert@xxxxxxxxxxxxxxxxxxxxxxxx>
Date: Thu, 2 Sep 2021 11:46:15 +0300
Subject: [PATCH] Eliminate duplicate completions from _git

Before this patch, setting
`zstyle '*' tag-order '! commit-tags heads-remote'`
failed to hide 'commit-tags' and 'heads-remote' from `git show`
completion. After this patch, this works correctly and will show
'commit-tags' or 'heads-remote' only if other tags produce no matches.

Before this patch, any completion using __git_commits (for example,
`git log`) listed groups 'local head', 'remote head', then other groups,
and then group 'head', which repeated items from the first two groups.
After this patch, the last group is omitted.

Additionally, after this patch, completions that previously could match
only recent commits, tags and heads, can now match _any_ commits, tags
or heads, but will prefer recent commits and tags.
---
 Completion/Unix/Command/_git | 26 ++++++++++----------------
 1 file changed, 10 insertions(+), 16 deletions(-)

diff --git a/Completion/Unix/Command/_git b/Completion/Unix/Command/_git
index a82b70e83..bbe7475bd 100644
--- a/Completion/Unix/Command/_git
+++ b/Completion/Unix/Command/_git
@@ -685,8 +685,8 @@ _git-commit () {
   # TODO: --interactive isn't explicitly listed in the documentation.
   _arguments -S -s $endopt \
     '(-a --all --interactive -o --only -i --include *)'{-a,--all}'[stage all modified and deleted paths]' \
-    '--fixup=[construct a commit message for use with rebase --autosquash]:commit to be amended:__git_recent_commits' \
-    '--squash=[construct a commit message for use with rebase --autosquash]:commit to be amended:__git_recent_commits' \
+    '--fixup=[construct a commit message for use with rebase --autosquash]:commit to be amended:__git_commits' \
+    '--squash=[construct a commit message for use with rebase --autosquash]:commit to be amended:__git_commits' \
     $reset_author_opt \
     '(        --porcelain --dry-run)--short[dry run with short output format]' \
     '--branch[show branch information]' \
@@ -1656,7 +1656,7 @@ _git-revert () {
     '*'{-X,--strategy-option=}'[pass merge-strategy-specific option to merge strategy]:option' \
     '(-S --gpg-sign --no-gpg-sign)'{-S-,--gpg-sign=-}'[GPG-sign the commit]::key id' \
     "(-S --gpg-sign --no-gpg-sign)--no-gpg-sign[don't GPG-sign the commit]" \
-    ': :__git_recent_commits'
+    ': :__git_commits'
 }
 
 (( $+functions[_git-rm] )) ||
@@ -1763,10 +1763,9 @@ _git-show () {
   case $state in
     (object)
       _alternative \
-        'commits::__git_commits' \
-        'tags::__git_tags' \
-        'trees::__git_trees' \
-        'blobs::__git_blobs' && ret=0
+            'trees::__git_trees' \
+            'blobs::__git_blobs' \
+          && ret=0
       ;;
   esac
 
@@ -4536,7 +4535,7 @@ _git-send-email () {
     '(- *)--dump-aliases[dump configured aliases and exit]' \
     '*: : _alternative -O expl
       "files:file:_files"
-      "commits:recent commit object name:__git_commit_objects_prefer_recent"'
+      "commits:recent commit object name:__git_commits"'
 }
 
 (( $+functions[_git-svn] )) ||
@@ -6920,7 +6919,7 @@ __git_commit_objects () {
 (( $+functions[__git_recent_commits] )) ||
 __git_recent_commits () {
   local gitdir expl start
-  declare -a descr tags heads commits argument_array_names commit_opts
+  declare -a descr tags commits argument_array_names commit_opts
   local h i j k ret
   integer distance_from_head
   local label
@@ -6985,11 +6984,8 @@ __git_recent_commits () {
     j=${${j# \(}%\)} # strip leading ' (' and trailing ')'
     j=${j/ ->/,}  # Convert " -> master, origin/master".
     for j in ${(s:, :)j}; do
-      if [[ $j == 'tag: '* ]] ; then
-        tags+=( ${j#tag: } )
-      else
-        heads+=( $j )
-      fi
+      [[ $j == 'tag: '* ]] &&
+          tags+=( ${j#tag: } )
     done
   done
 
@@ -6999,8 +6995,6 @@ __git_recent_commits () {
   _describe -V -t commits 'recent commit object name' descr && ret=0
   expl=()
   _wanted commit-tags expl 'commit tag' compadd "$@" -a - tags && ret=0
-  expl=()
-  _wanted heads expl 'head' compadd -M "r:|/=* r:|=*" "$@" -a - heads && ret=0
   return ret
 }
 
-- 
2.33.0



Messages sorted by: Reverse Date, Date, Thread, Author