From 09161761c14f23dec8e8f681ff80140d828080b0 Mon Sep 17 00:00:00 2001 From: Johannes Altmanninger Date: Sun, 24 Jan 2021 11:48:17 +0100 Subject: [PATCH] Complete custom "git-foo" commands from "git foo" Since #7075, git-foo.fish files are sourced when Git completions are loaded. However, at least Cobra (CLI framework for Go) provides completions like complete git-foo ... This means that completions are only offered when typing "git-foo " and not on "git foo ". Fix this by forwarding the completion requests. Take care to only forward if there are actually completions for "git-foo", to avoid adding filename completions. --- CHANGELOG.rst | 1 + share/completions/git.fish | 14 ++++++++++++++ 2 files changed, 15 insertions(+) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 7a21acaf3..1935678e5 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -356,6 +356,7 @@ Completions - The ``make`` completions no longer second-guess make's file detection, fixing target completion in some cases (:issue:`7535`). - The command completions now correctly print the description even if the command was fully matched (like in ``ls``). - The ``set`` completions no longer hide variables starting with ``__``, they are sorted last instead. +- Completion scripts for custom Git subcommands like ``git-xyz`` are now loaded with Git completions. The completions can now be defined directly on the subcommand (using ``complete git-xyz``), and completion for ``git xyz`` will work. (:issue:`7075`, :issue:`7652`) Changes not visible to users ---------------------------- diff --git a/share/completions/git.fish b/share/completions/git.fish index a608fb424..135c22966 100644 --- a/share/completions/git.fish +++ b/share/completions/git.fish @@ -1899,6 +1899,16 @@ complete -c git -n '__fish_git_using_command bisect; and __fish_seen_argument -- ## Custom commands (git-* commands installed in the PATH) complete -c git -n __fish_git_needs_command -a '(__fish_git_custom_commands)' -d 'Custom command' +function __fish_git_complete_custom_command -a subcommand + set -l cmd (commandline -opc) + set -e cmd[1] # Drop "git". + set -l subcommand_args + if argparse -s (__fish_git_global_optspecs) -- $cmd + set subcommand_args $argv[2..] # Drop the subcommand. + end + complete -C "git-$subcommand $subcommand_args "(commandline -ct) +end + # Get the path to the generated completions # If $XDG_DATA_HOME is set, that's it, if not, it will be removed and ~/.local/share will remain. set -l generated_path $XDG_DATA_HOME/fish/generated_completions ~/.local/share/fish/generated_completions @@ -1920,5 +1930,9 @@ for git_ext in $complete_dirs/git-*.fish contains -- $cmd $__fish_git_custom_commands_completion and continue source $git_ext + set -l subcommand (string replace -r '^git-' '' -- $cmd) + if [ (complete git-$subcommand | count) -gt 0 ] + complete git -f -n "__fish_git_using_command $subcommand" -a "(__fish_git_complete_custom_command $subcommand)" + end set -a __fish_git_custom_commands_completion $cmd end