Re: [PATCH] completion: zsh: support completion after "git -C <path>"
- From
Junio C Hamano <gitster@pobox.com>
- Date
- Jun 17, 2026, 17:21 UTC
- Message-ID
- <xmqqa4stw09d.fsf@gitster.g>
- In-Reply-To
- <pull.2155.git.1781710256081.gitgitgadget@gmail.com>
"Lutz Lengemann via GitGitGadget" <gitgitgadget@gmail.com> writes:
Show 5 quoted lines
> From: Lutz Lengemann <lutz@lengemann.net> > > The zsh completion wrapper (__git_zsh_main) did not handle the global -C > option, so "git -C <path> <command> <TAB>" offered nothing and could not > complete a command's arguments.
I do not write, use, or customize zsh, so please take my comments with huge grains of salt, or just ignore them completely (your choice) ;-), but one thng I noticed was that ...
Show 12 quoted lines
> diff --git a/contrib/completion/git-completion.zsh b/contrib/completion/git-completion.zsh
> index c32186a977..323049be8b 100644
> --- a/contrib/completion/git-completion.zsh
> +++ b/contrib/completion/git-completion.zsh
> @@ -227,6 +227,7 @@ __git_zsh_main ()
> '(-p --paginate --no-pager)'{-p,--paginate}'[pipe all output into ''less'']' \
> '(-p --paginate)--no-pager[do not pipe git output into a pager]' \
> '--git-dir=-[set the path to the repository]: :_directories' \
> + '*-C[run as if git was started in <path>]: :_directories' \
> '--bare[treat the repository as a bare repository]' \
> '(- :)--version[prints the git suite version]' \
> '--exec-path=-[path to where your core git programs are installed]:: :_directories' \... this part talks about not just "-C<dir>" but knows about all the other options that the "git" potty itself takes, while ...
Show 12 quoted lines
> @@ -252,6 +253,14 @@ __git_zsh_main ()
> ;;
> (arg)
> local command="${words[1]}" __git_dir __git_cmd_idx=1
> + local -a __git_C_args
> + local -i i=2
> +
> + while [[ ${orig_words[i]} == -C ]]; do
> + __git_C_args+=(-C ${orig_words[i+1]})
> + (( __git_cmd_idx += 2 ))
> + (( i += 2 ))
> + done... this only knows about "-C<dir>" and nothing else.
Doesn't it want to do something similar to what __git_main in git-completion.bash does at the beginning, namely, this part?
__git_main ()
{
local i c=1 command __git_dir __git_repo_path
local __git_C_args C_args_count=0
local __git_cmd_idx while [ $c -lt $cword ]; do
i="${words[c]}"
case "$i" in
--git-dir=*)
__git_dir="${i#--git-dir=}"
;;
--git-dir)
((c++))
__git_dir="${words[c]}"
;;
--bare)
__git_dir="."
;;
--help)
command="help"
break
;;
-c|--work-tree|--namespace)
((c++))
;;
-C)
__git_C_args[C_args_count++]=-C
((c++))
__git_C_args[C_args_count++]="${words[c]}"
;;
-*)
;;
*)
command="$i"
__git_cmd_idx="$c"
break
;;
esac
((c++))
done