git/list[1] front-page[2] threads[3] people[4] search[5] about
 

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
Previous: Ben KnobleNext: D. Ben Knoble
Message 3 of 15 in “completion: zsh: support completion after "git -C <path>"”
  1. completion: zsh: support completion after "git -C <path>"Lutz Lengemann via GitGitGadget, Jun 17, 2026
  2. Ben KnobleJun 17, 2026
  3. Junio C HamanoJun 17, 2026
  4. D. Ben KnobleJun 18, 2026
  5. D. Ben KnobleJul 14, 2026
  6. Lutz LengemannAug 17, 2026
  7. D. Ben KnobleAug 18, 2026
  8. Lutz LengemannAug 18, 2026
  9. D. Ben KnobleAug 18, 2026
  10. completion: zsh: support completion after "git -C <path>"Lutz Lengemann via GitGitGadget, Aug 19, 2026
  11. D. Ben KnobleAug 20, 2026
  12. Junio C HamanoAug 20, 2026
  13. D. Ben KnobleAug 21, 2026
  14. Junio C HamanoAug 21, 2026
  15. Junio C HamanoAug 26, 2026

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.