threads / patch / 61364

patch, 2 partscompletion: add docs on how to add subcommand completions

Subject: [PATCH 2/2] completion: add docs on how to add subcommand completions

## tl;dr

6 messages between Apr 24, 2024 and Apr 24, 2024. Diffs are folded; open one to read it.

replies: 5people: 2as markdown or json

Roland Hieber· Apr 24, 2024, 21:05 UTC · lore

[PATCH 1/2] completion: add 'symbolic-ref'

Even 'symbolic-ref' is only completed when GIT_COMPLETION_SHOW_ALL_COMMANDS=1 is set, it currently defaults to completing file names, which is not very helpful. Add a simple completion function which completes options and refs.

Signed-off-by: Roland Hieber <rhi@pengutronix.de>
---
 contrib/completion/git-completion.bash | 11 +++++++++++
 t/t9902-completion.sh                  | 16 ++++++++++++++++
 2 files changed, 27 insertions(+)
Show changes to 2 files +27 −0

contrib/completion/git-completion.bash, t/t9902-completion.sh

diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index 75193ded4bde..ffcc55484bcd 100644
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -3581,6 +3581,17 @@ _git_svn ()
 	fi
 }
 
+_git_symbolic_ref () {
+	case "$cur" in
+	--*)
+		__gitcomp "--delete --quiet --short --recurse --no-recurse"
+		return
+		;;
+	esac
+	
+	__git_complete_refs
+}
+
 _git_tag ()
 {
 	local i c="$__git_cmd_idx" f=0
diff --git a/t/t9902-completion.sh b/t/t9902-completion.sh
index 569cf2310434..a34953da6c96 100755
--- a/t/t9902-completion.sh
+++ b/t/t9902-completion.sh
@@ -2518,6 +2518,22 @@ test_expect_success 'complete tree filename with metacharacters' '
 	EOF
 '
 
+test_expect_success 'symbolic-ref completes short ref names ' '
+	test_completion "git symbolic-ref foo m" <<-\EOF
+	main Z
+	mybranch Z
+	mytag Z
+	EOF
+'
+test_expect_success 'symbolic-ref completes full ref names' '
+	test_completion "git symbolic-ref foo refs/" <<-\EOF
+	refs/heads/main Z
+	refs/heads/mybranch Z
+	refs/tags/mytag Z
+	refs/tags/A Z
+	EOF
+'
+
 test_expect_success PERL 'send-email' '
 	test_completion "git send-email --cov" <<-\EOF &&
 	--cover-from-description=Z
-- 
2.39.2
Roland Hieber· Apr 24, 2024, 21:05 UTC · re: Roland Hieber · lore

It took me more than a few tries and a good lecture of __git_main to understand that the two paragraphs above really only refer to adding completion functions for executables that are not called through git's subcommand magic. Add a few sentences for that case.

Signed-off-by: Roland Hieber <rhi@pengutronix.de>
---
 contrib/completion/git-completion.bash | 7 +++++++
 1 file changed, 7 insertions(+)
Show changes to contrib/completion/git-completion.bash +7 −0
diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index ffcc55484bcd..f0c8353c1cdb 100644
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -40,6 +40,13 @@
 #
 #   __git_complete gk gitk
 #
+# To add completion for git subcommands that live in external scripts,
+# define a function of the form '_git_${subcommand}' while replacing all dashes
+# with underscores, and the main git completion will make use of it.
+# For example, to add completeion after 'git do-my-stuff' (which could e.g. live
+# in /usr/bin/git-do-stuff), name the completion function '_git_do_stuff'.
+# See _git_show, _git_bisect etc. below for more examples.
+#
 # Compatible with bash 3.2.57.
 #
 # You can set the following environment variables to influence the behavior of
-- 
2.39.2
Roland Hieber· Apr 24, 2024, 21:17 UTC · re: Roland Hieber · lore

Re: [PATCH 2/2] completion: add docs on how to add subcommand completions

On Wed, Apr 24, 2024 at 11:05:48PM +0200, Roland Hieber wrote:
Show 22 quoted lines
> It took me more than a few tries and a good lecture of __git_main to
> understand that the two paragraphs above really only refer to adding
> completion functions for executables that are not called through git's
> subcommand magic. Add a few sentences for that case.
> 
> Signed-off-by: Roland Hieber <rhi@pengutronix.de>
> ---
>  contrib/completion/git-completion.bash | 7 +++++++
>  1 file changed, 7 insertions(+)
> 
> diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
> index ffcc55484bcd..f0c8353c1cdb 100644
> --- a/contrib/completion/git-completion.bash
> +++ b/contrib/completion/git-completion.bash
> @@ -40,6 +40,13 @@
>  #
>  #   __git_complete gk gitk
>  #
> +# To add completion for git subcommands that live in external scripts,
> +# define a function of the form '_git_${subcommand}' while replacing all dashes
> +# with underscores, and the main git completion will make use of it.
> +# For example, to add completeion after 'git do-my-stuff' (which could e.g. live
Ah, of course its s/completeion/completion/ …
 - Roland
Show 10 quoted lines
> +# in /usr/bin/git-do-stuff), name the completion function '_git_do_stuff'.
> +# See _git_show, _git_bisect etc. below for more examples.
> +#
>  # Compatible with bash 3.2.57.
>  #
>  # You can set the following environment variables to influence the behavior of
> -- 
> 2.39.2
> 
> 
-- 
Roland Hieber, Pengutronix e.K.          | r.hieber@pengutronix.de     |
Steuerwalder Str. 21                     | https://www.pengutronix.de/ |
31137 Hildesheim, Germany                | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686         | Fax:   +49-5121-206917-5555 |
Junio C Hamano· Apr 24, 2024, 21:36 UTC · re: Roland Hieber · lore

Re: [PATCH 2/2] completion: add docs on how to add subcommand completions

Roland Hieber <rhi@pengutronix.de> writes:
Show 5 quoted lines
> +# To add completion for git subcommands that live in external scripts,
> +# define a function of the form '_git_${subcommand}' while replacing all dashes
> +# with underscores, and the main git completion will make use of it.
> +# For example, to add completeion after 'git do-my-stuff' (which could e.g. live
> +# in /usr/bin/git-do-stuff), name the completion function '_git_do_stuff'.
You have either an extra "-my", or two "-my"s missing, in the above.
Show 5 quoted lines
> +# See _git_show, _git_bisect etc. below for more examples.
> +#
>  # Compatible with bash 3.2.57.
>  #
>  # You can set the following environment variables to influence the behavior of
Roland Hieber· Apr 24, 2024, 21:41 UTC · re: Junio C Hamano · lore

Re: [PATCH 2/2] completion: add docs on how to add subcommand completions

On Wed, Apr 24, 2024 at 02:36:27PM -0700, Junio C Hamano wrote:
Show 9 quoted lines
> Roland Hieber <rhi@pengutronix.de> writes:
> 
> > +# To add completion for git subcommands that live in external scripts,
> > +# define a function of the form '_git_${subcommand}' while replacing all dashes
> > +# with underscores, and the main git completion will make use of it.
> > +# For example, to add completeion after 'git do-my-stuff' (which could e.g. live
> > +# in /usr/bin/git-do-stuff), name the completion function '_git_do_stuff'.
> 
> You have either an extra "-my", or two "-my"s missing, in the above.

Ah, and I deleted one '-my' in the line before sending it… Will send a v2.

 - Roland
Show 7 quoted lines
> 
> > +# See _git_show, _git_bisect etc. below for more examples.
> > +#
> >  # Compatible with bash 3.2.57.
> >  #
> >  # You can set the following environment variables to influence the behavior of
> 
-- 
Roland Hieber, Pengutronix e.K.          | r.hieber@pengutronix.de     |
Steuerwalder Str. 21                     | https://www.pengutronix.de/ |
31137 Hildesheim, Germany                | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686         | Fax:   +49-5121-206917-5555 |
Junio C Hamano· Apr 24, 2024, 22:22 UTC · re: Roland Hieber · lore

Re: [PATCH 2/2] completion: add docs on how to add subcommand completions

Roland Hieber <rhi@pengutronix.de> writes:
> It took me more than a few tries and a good lecture of __git_main to
> understand that the two paragraphs above really only refer to adding
> completion functions for executables that are not called through git's
> subcommand magic. Add a few sentences for that case.

OK. Perhaps the two examples also need some more explanation to save the next person from wasting their time figuring out what they are trying to say? That would be a separate topic, but it would be nice to fix it while our minds are fresh on that issue.

As I already said, modulo confusion around "-my", the patch looks good to me.

Thanks.

← back to recent threads