1

I was experimenting with custom git subcommand completion:

I am trying to extend the completion for git commit -m or git commit --message.

$HOME/.zsh/completions/_git-foo (this path is added to fpath fpath+=~/.zsh/completions)

#compdef git-foo

_git-foo() {
  _git-commit

  local -a commands
  commands=(
    'hello-git:our first autocompletion function.'
    'version:show the used gem version.'
  )

  if (( CURRENT == 3 )); then
    if [[ $words[2] == "--message" || $words[2] == "--message=" || $words[2] == "-m" ]]; then
      _describe -t commands 'commands' commands
    fi
  fi

  return 0
}

_git-foo

but the main objective is to add this additional completion to the already defined _git-commit completion without breaking the original completion function for git commit

I have tried to change the #compdef directive to #compdef git-commit and file name from _git-foo to _git-commit but it didn't work.

I was getting the following error:

_git-commit:1: maximum nested function level reached; increase FUNCNEST?
_git-commit:1: maximum nested function level reached; increase FUNCNEST?
_git-commit:1: maximum nested function level reached; increase FUNCNEST?

Creating your custom completion for a custom subcommand works fine, but how to extend the already defined git completions without breaking the original one?

AK-35
  • 559
  • 2
  • 9
  • 24

1 Answers1

1

Note that I'm not yet able to acheive what you're aiming to do. I am also fiddling with zsh custom autocompletion lately, hopefully what I found can help you out

In any case I'll be interested in your feedback as I'm discovering the ropes as well :D

Maximum nested function level error

Based on this answer from user 4015-alt:

You are attempting to define a function _git_commit which calls itself, thus creating a recursion. To prevent such mistakes from creating infinite loop, zsh will stop when the amount of nested function call reaches configured amount: FUNCNEST

Potential solution

Based on this answer from user Attila

You could save a reference to the original _git_commit completion function used natively by git, and redefine it as you need Then use the reference you saved to call back the original completion function:

_git_commit_orig="$functions[_git_commit]"

_git_commit() {
  [...]
  _git_commit_orig "$@"
}

As mentioned by Attila:

You wouldn't need to call compdef again, since it is already bound to that function, and you just changed the function definition.

Hope this helps

Bleacks
  • 31
  • 4
  • Hi @Bleacks. I actually had problem with recursion as well. I ended up not going this route. I'll take a look at it and give it a try. I think it is good know how to extend the functionality of existing completion function for future works. Thanks – AK-35 Feb 03 '23 at 20:29