Skip to content

Commit 9739cde

Browse files
committed
completion: zsh: support completion after "git -C <path>"
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. Three things are needed to make it work, all scoped to -C: - Add -C to the _arguments specification, so completion no longer stops at it. - Advance __git_cmd_idx past any leading "-C <path>" options. The index is hard-coded to 1, i.e. the command is assumed to be the first argument; with -C present the command sits two words later for each -C, so the bash helpers otherwise look at the wrong word and produce nothing. - Collect the -C paths into __git_C_args, as __git_main does. The bash helpers run git to resolve aliases and list refs; without the -C paths they run in the current directory, so completion fails whenever the cwd is not the target repository or the command is an alias. With these, "git -C <path> <command> <TAB>" completes the command, its options and its arguments, including outside the repository, through aliases, and with repeated -C options. Signed-off-by: Lutz Lengemann <lutz@lengemann.net>
1 parent 0fae78c commit 9739cde

1 file changed

Lines changed: 9 additions & 0 deletions

File tree

contrib/completion/git-completion.zsh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ __git_zsh_main ()
227227
'(-p --paginate --no-pager)'{-p,--paginate}'[pipe all output into ''less'']' \
228228
'(-p --paginate)--no-pager[do not pipe git output into a pager]' \
229229
'--git-dir=-[set the path to the repository]: :_directories' \
230+
'*-C[run as if git was started in <path>]: :_directories' \
230231
'--bare[treat the repository as a bare repository]' \
231232
'(- :)--version[prints the git suite version]' \
232233
'--exec-path=-[path to where your core git programs are installed]:: :_directories' \
@@ -252,6 +253,14 @@ __git_zsh_main ()
252253
;;
253254
(arg)
254255
local command="${words[1]}" __git_dir __git_cmd_idx=1
256+
local -a __git_C_args
257+
local -i i=2
258+
259+
while [[ ${orig_words[i]} == -C ]]; do
260+
__git_C_args+=(-C ${orig_words[i+1]})
261+
(( __git_cmd_idx += 2 ))
262+
(( i += 2 ))
263+
done
255264

256265
if (( $+opt_args[--bare] )); then
257266
__git_dir='.'

0 commit comments

Comments
 (0)