{"thread":{"id":"61365","subject":"[PATCH v2 1/2] completion: add 'symbolic-ref'","startedAt":"2024-04-24T21:50:27Z","lastAt":"2024-04-25T10:21:19Z","messageCount":4,"participants":["Roland Hieber","Patrick Steinhardt"],"isPatch":true,"patchVersion":2,"patchTotal":2},"messages":[{"id":"493444","messageId":"20240424215019.268208-1-rhi@pengutronix.de","threadId":"61365","inReplyTo":null,"subject":"[PATCH v2 1/2] completion: add 'symbolic-ref'","fromName":"Roland Hieber","fromEmail":"rhi@pengutronix.de","sentAt":"2024-04-24T21:50:17Z","receivedAt":"2024-04-24T21:50:27Z","isPatch":true,"sender":{"key":"rhi@pengutronix.de","avatar":"https://avatars.githubusercontent.com/u/192014?v=4"},"body":"Even 'symbolic-ref' is only completed when\nGIT_COMPLETION_SHOW_ALL_COMMANDS=1 is set, it currently defaults to\ncompleting file names, which is not very helpful. Add a simple\ncompletion function which completes options and refs.\n\nSigned-off-by: Roland Hieber <rhi@pengutronix.de>\n---\nPATCH v2:\n - no changes\n\nPATCH v1: https://lore.kernel.org/git/20240424210549.256256-1-rhi@pengutronix.de/\n---\n contrib/completion/git-completion.bash | 11 +++++++++++\n t/t9902-completion.sh                  | 16 ++++++++++++++++\n 2 files changed, 27 insertions(+)\n\ndiff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash\nindex 75193ded4bde..ffcc55484bcd 100644\n--- a/contrib/completion/git-completion.bash\n+++ b/contrib/completion/git-completion.bash\n@@ -3581,6 +3581,17 @@ _git_svn ()\n \tfi\n }\n \n+_git_symbolic_ref () {\n+\tcase \"$cur\" in\n+\t--*)\n+\t\t__gitcomp \"--delete --quiet --short --recurse --no-recurse\"\n+\t\treturn\n+\t\t;;\n+\tesac\n+\t\n+\t__git_complete_refs\n+}\n+\n _git_tag ()\n {\n \tlocal i c=\"$__git_cmd_idx\" f=0\ndiff --git a/t/t9902-completion.sh b/t/t9902-completion.sh\nindex 569cf2310434..a34953da6c96 100755\n--- a/t/t9902-completion.sh\n+++ b/t/t9902-completion.sh\n@@ -2518,6 +2518,22 @@ test_expect_success 'complete tree filename with metacharacters' '\n \tEOF\n '\n \n+test_expect_success 'symbolic-ref completes short ref names ' '\n+\ttest_completion \"git symbolic-ref foo m\" <<-\\EOF\n+\tmain Z\n+\tmybranch Z\n+\tmytag Z\n+\tEOF\n+'\n+test_expect_success 'symbolic-ref completes full ref names' '\n+\ttest_completion \"git symbolic-ref foo refs/\" <<-\\EOF\n+\trefs/heads/main Z\n+\trefs/heads/mybranch Z\n+\trefs/tags/mytag Z\n+\trefs/tags/A Z\n+\tEOF\n+'\n+\n test_expect_success PERL 'send-email' '\n \ttest_completion \"git send-email --cov\" <<-\\EOF &&\n \t--cover-from-description=Z\n-- \n2.39.2\n\n"},{"id":"493445","messageId":"20240424215019.268208-2-rhi@pengutronix.de","threadId":"61365","inReplyTo":"20240424215019.268208-1-rhi@pengutronix.de","subject":"[PATCH v2 2/2] completion: add docs on how to add subcommand completions","fromName":"Roland Hieber","fromEmail":"rhi@pengutronix.de","sentAt":"2024-04-24T21:50:18Z","receivedAt":"2024-04-24T21:50:27Z","isPatch":true,"sender":{"key":"rhi@pengutronix.de","avatar":"https://avatars.githubusercontent.com/u/192014?v=4"},"body":"It took me more than a few tries and a good lecture of __git_main to\nunderstand that the two paragraphs above really only refer to adding\ncompletion functions for executables that are not called through git's\nsubcommand magic. Add a few sentences for that case.\n\nSigned-off-by: Roland Hieber <rhi@pengutronix.de>\n---\nPATCH v2:\n - fix typo and superfluous \"-my\"\n\nPATCH v1: https://lore.kernel.org/git/20240424210549.256256-2-rhi@pengutronix.de/\n---\n contrib/completion/git-completion.bash | 7 +++++++\n 1 file changed, 7 insertions(+)\n\ndiff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash\nindex ffcc55484bcd..603181d53a90 100644\n--- a/contrib/completion/git-completion.bash\n+++ b/contrib/completion/git-completion.bash\n@@ -40,6 +40,13 @@\n #\n #   __git_complete gk gitk\n #\n+# To add completion for git subcommands that live in external scripts,\n+# define a function of the form '_git_${subcommand}' while replacing all dashes\n+# with underscores, and the main git completion will make use of it.\n+# For example, to add completion after 'git do-stuff' (which could e.g. live\n+# in /usr/bin/git-do-stuff), name the completion function '_git_do_stuff'.\n+# See _git_show, _git_bisect etc. below for more examples.\n+#\n # Compatible with bash 3.2.57.\n #\n # You can set the following environment variables to influence the behavior of\n-- \n2.39.2\n\n"},{"id":"493459","messageId":"Zin1nOSfckSt-H0J@tanuki","threadId":"61365","inReplyTo":"20240424215019.268208-1-rhi@pengutronix.de","subject":"Re: [PATCH v2 1/2] completion: add 'symbolic-ref'","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2024-04-25T06:18:04Z","receivedAt":"2024-04-25T06:18:12Z","isPatch":true,"sender":{"key":"ps@pks.im","avatar":"https://avatars.githubusercontent.com/u/4056630?v=4"},"body":"On Wed, Apr 24, 2024 at 11:50:17PM +0200, Roland Hieber wrote:\n> Even 'symbolic-ref' is only completed when\n> GIT_COMPLETION_SHOW_ALL_COMMANDS=1 is set, it currently defaults to\n> completing file names, which is not very helpful. Add a simple\n> completion function which completes options and refs.\n> \n> Signed-off-by: Roland Hieber <rhi@pengutronix.de>\n> ---\n> PATCH v2:\n>  - no changes\n> \n> PATCH v1: https://lore.kernel.org/git/20240424210549.256256-1-rhi@pengutronix.de/\n> ---\n>  contrib/completion/git-completion.bash | 11 +++++++++++\n>  t/t9902-completion.sh                  | 16 ++++++++++++++++\n>  2 files changed, 27 insertions(+)\n> \n> diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash\n> index 75193ded4bde..ffcc55484bcd 100644\n> --- a/contrib/completion/git-completion.bash\n> +++ b/contrib/completion/git-completion.bash\n> @@ -3581,6 +3581,17 @@ _git_svn ()\n>  \tfi\n>  }\n>  \n> +_git_symbolic_ref () {\n> +\tcase \"$cur\" in\n> +\t--*)\n> +\t\t__gitcomp \"--delete --quiet --short --recurse --no-recurse\"\n> +\t\treturn\n> +\t\t;;\n\nInstead of hardcoding these we may use the `--git-completion-helper`\noption, which outputs the options for us. We have `__gitcomp_builtin ()`\nto do this.\n\n> +\tesac\n> +\t\n> +\t__git_complete_refs\n> +}\n> +\n>  _git_tag ()\n>  {\n>  \tlocal i c=\"$__git_cmd_idx\" f=0\n> diff --git a/t/t9902-completion.sh b/t/t9902-completion.sh\n> index 569cf2310434..a34953da6c96 100755\n> --- a/t/t9902-completion.sh\n> +++ b/t/t9902-completion.sh\n> @@ -2518,6 +2518,22 @@ test_expect_success 'complete tree filename with metacharacters' '\n>  \tEOF\n>  '\n>  \n> +test_expect_success 'symbolic-ref completes short ref names ' '\n> +\ttest_completion \"git symbolic-ref foo m\" <<-\\EOF\n> +\tmain Z\n> +\tmybranch Z\n> +\tmytag Z\n> +\tEOF\n> +'\n\nNit: let's add a newline between those two test cases.\n\nPatrick\n\n> +test_expect_success 'symbolic-ref completes full ref names' '\n> +\ttest_completion \"git symbolic-ref foo refs/\" <<-\\EOF\n> +\trefs/heads/main Z\n> +\trefs/heads/mybranch Z\n> +\trefs/tags/mytag Z\n> +\trefs/tags/A Z\n> +\tEOF\n> +'\n> +\n>  test_expect_success PERL 'send-email' '\n>  \ttest_completion \"git send-email --cov\" <<-\\EOF &&\n>  \t--cover-from-description=Z\n> -- \n> 2.39.2\n> \n> \n"},{"id":"493468","messageId":"20240425102116.fgis3qqhi7y5uh3q@pengutronix.de","threadId":"61365","inReplyTo":"Zin1nOSfckSt-H0J@tanuki","subject":"Re: [PATCH v2 1/2] completion: add 'symbolic-ref'","fromName":"Roland Hieber","fromEmail":"rhi@pengutronix.de","sentAt":"2024-04-25T10:21:16Z","receivedAt":"2024-04-25T10:21:19Z","isPatch":true,"sender":{"key":"rhi@pengutronix.de","avatar":"https://avatars.githubusercontent.com/u/192014?v=4"},"body":"On Thu, Apr 25, 2024 at 08:18:04AM +0200, Patrick Steinhardt wrote:\n> On Wed, Apr 24, 2024 at 11:50:17PM +0200, Roland Hieber wrote:\n> > Even 'symbolic-ref' is only completed when\n> > GIT_COMPLETION_SHOW_ALL_COMMANDS=1 is set, it currently defaults to\n> > completing file names, which is not very helpful. Add a simple\n> > completion function which completes options and refs.\n> > \n> > Signed-off-by: Roland Hieber <rhi@pengutronix.de>\n> > ---\n> > PATCH v2:\n> >  - no changes\n> > \n> > PATCH v1: https://lore.kernel.org/git/20240424210549.256256-1-rhi@pengutronix.de/\n> > ---\n> >  contrib/completion/git-completion.bash | 11 +++++++++++\n> >  t/t9902-completion.sh                  | 16 ++++++++++++++++\n> >  2 files changed, 27 insertions(+)\n> > \n> > diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash\n> > index 75193ded4bde..ffcc55484bcd 100644\n> > --- a/contrib/completion/git-completion.bash\n> > +++ b/contrib/completion/git-completion.bash\n> > @@ -3581,6 +3581,17 @@ _git_svn ()\n> >  \tfi\n> >  }\n> >  \n> > +_git_symbolic_ref () {\n> > +\tcase \"$cur\" in\n> > +\t--*)\n> > +\t\t__gitcomp \"--delete --quiet --short --recurse --no-recurse\"\n> > +\t\treturn\n> > +\t\t;;\n> \n> Instead of hardcoding these we may use the `--git-completion-helper`\n> option, which outputs the options for us. We have `__gitcomp_builtin ()`\n> to do this.\n\nAhh thanks, I didn't know there was automagic for it! Fixed in v3:\n<https://lore.kernel.org/git/20240425101845.708554-1-rhi@pengutronix.de/>\n\n - Roland\n\n> > +\tesac\n> > +\t\n> > +\t__git_complete_refs\n> > +}\n> > +\n> >  _git_tag ()\n> >  {\n> >  \tlocal i c=\"$__git_cmd_idx\" f=0\n> > diff --git a/t/t9902-completion.sh b/t/t9902-completion.sh\n> > index 569cf2310434..a34953da6c96 100755\n> > --- a/t/t9902-completion.sh\n> > +++ b/t/t9902-completion.sh\n> > @@ -2518,6 +2518,22 @@ test_expect_success 'complete tree filename with metacharacters' '\n> >  \tEOF\n> >  '\n> >  \n> > +test_expect_success 'symbolic-ref completes short ref names ' '\n> > +\ttest_completion \"git symbolic-ref foo m\" <<-\\EOF\n> > +\tmain Z\n> > +\tmybranch Z\n> > +\tmytag Z\n> > +\tEOF\n> > +'\n> \n> Nit: let's add a newline between those two test cases.\n> \n> Patrick\n> \n> > +test_expect_success 'symbolic-ref completes full ref names' '\n> > +\ttest_completion \"git symbolic-ref foo refs/\" <<-\\EOF\n> > +\trefs/heads/main Z\n> > +\trefs/heads/mybranch Z\n> > +\trefs/tags/mytag Z\n> > +\trefs/tags/A Z\n> > +\tEOF\n> > +'\n> > +\n> >  test_expect_success PERL 'send-email' '\n> >  \ttest_completion \"git send-email --cov\" <<-\\EOF &&\n> >  \t--cover-from-description=Z\n> > -- \n> > 2.39.2\n> > \n> > \n\n\n\n-- \nRoland Hieber, Pengutronix e.K.          | r.hieber@pengutronix.de     |\nSteuerwalder Str. 21                     | https://www.pengutronix.de/ |\n31137 Hildesheim, Germany                | Phone: +49-5121-206917-0    |\nAmtsgericht Hildesheim, HRA 2686         | Fax:   +49-5121-206917-5555 |\n"}]}