{"thread":{"id":"54895","subject":"[PATCH 0/2] completion: make __git_complete public","startedAt":"2020-12-28T19:24:02Z","lastAt":"2020-12-29T00:37:01Z","messageCount":7,"participants":["Felipe Contreras","Denton Liu"],"isPatch":true,"patchVersion":1,"patchTotal":2},"messages":[{"id":"413090","messageId":"20201228192302.80467-1-felipe.contreras@gmail.com","threadId":"54895","inReplyTo":null,"subject":"[PATCH 0/2] completion: make __git_complete public","fromName":"Felipe Contreras","fromEmail":"felipe.contreras@gmail.com","sentAt":"2020-12-28T19:23:00Z","receivedAt":"2020-12-28T19:24:02Z","isPatch":true,"sender":{"key":"felipe.contreras@gmail.com","avatar":"https://avatars.githubusercontent.com/u/8358?v=4"},"body":"Everything is explained in PATCH 2/2.\n\nFelipe Contreras (2):\n  test: completion: add tests for __git_complete\n  completion: add proper public __git_complete\n\n contrib/completion/git-completion.bash | 31 ++++++++++++++++++++------\n t/t9902-completion.sh                  | 20 +++++++++++++++++\n 2 files changed, 44 insertions(+), 7 deletions(-)\n\n-- \n2.30.0.rc2\n\n"},{"id":"413091","messageId":"20201228192302.80467-2-felipe.contreras@gmail.com","threadId":"54895","inReplyTo":"20201228192302.80467-1-felipe.contreras@gmail.com","subject":"[PATCH 1/2] test: completion: add tests for __git_complete","fromName":"Felipe Contreras","fromEmail":"felipe.contreras@gmail.com","sentAt":"2020-12-28T19:23:01Z","receivedAt":"2020-12-28T19:24:02Z","isPatch":true,"sender":{"key":"felipe.contreras@gmail.com","avatar":"https://avatars.githubusercontent.com/u/8358?v=4"},"body":"Signed-off-by: Felipe Contreras <felipe.contreras@gmail.com>\n---\n t/t9902-completion.sh | 7 +++++++\n 1 file changed, 7 insertions(+)\n\ndiff --git a/t/t9902-completion.sh b/t/t9902-completion.sh\nindex a1c4f1f6d4..2e59fe4de0 100755\n--- a/t/t9902-completion.sh\n+++ b/t/t9902-completion.sh\n@@ -2380,4 +2380,11 @@ test_expect_success 'sourcing the completion script clears cached --options' '\n \tverbose test -z \"$__gitcomp_builtin_notes_edit\"\n '\n \n+test_expect_success '__git_complete' '\n+\t__git_complete foo __git_main &&\n+\ttest \"$(type -t __git_wrap__git_main)\" == function &&\n+\t__git_complete gf _git_fetch &&\n+\ttest \"$(type -t __git_wrap_git_fetch)\" == function\n+'\n+\n test_done\n-- \n2.30.0.rc2\n\n"},{"id":"413092","messageId":"20201228192302.80467-3-felipe.contreras@gmail.com","threadId":"54895","inReplyTo":"20201228192302.80467-1-felipe.contreras@gmail.com","subject":"[PATCH 2/2] completion: add proper public __git_complete","fromName":"Felipe Contreras","fromEmail":"felipe.contreras@gmail.com","sentAt":"2020-12-28T19:23:02Z","receivedAt":"2020-12-28T19:24:02Z","isPatch":true,"sender":{"key":"felipe.contreras@gmail.com","avatar":"https://avatars.githubusercontent.com/u/8358?v=4"},"body":"Back in 2012 I argued [1] for the introduction of a helper that would\nallow users to specify aliases like:\n\n  git_complete gf git_fetch\n\nBack then there was pushback because there was no clear guideline for\npublic functions (git_complete vs _git_complete vs _GIT_complete), and\nsome aliases didn't actually work.\n\nFast-forward to 2020 and there's still no guideline for public\nfunctions, and those aliases still don't work (even though I sent the\nfixes).\n\nThis has not prevented people from using this function that is clearly\nneeded to setup custom aliases [2], and in fact it's the recommended\nway. But it is cumbersome that the user must type:\n\n  __git_complete gf _git_fetch\n\nOr worse:\n\n  __git_complete gk __gitk_main\n\n8 years is more than enough time to stop waiting for the perfect to\ncome; let's define a public function (with the same name) that is\nactually user-friendly:\n\n  __git_complete gf git_fetch\n  __git_complete gk gitk\n\nWhile also maintaining backwards compatibility.\n\nThe logic is:\n\n 1. If $2 exists, use it directly\n 2. If not, check if __$2_main exists\n 3. If not, check if _$2 exists\n 4. If not, fail\n\n[1] https://lore.kernel.org/git/1334524814-13581-1-git-send-email-felipe.contreras@gmail.com/\n[2] https://stackoverflow.com/questions/342969/how-do-i-get-bash-completion-to-work-with-aliases\n\nSigned-off-by: Felipe Contreras <felipe.contreras@gmail.com>\n---\n contrib/completion/git-completion.bash | 31 ++++++++++++++++++++------\n t/t9902-completion.sh                  | 15 ++++++++++++-\n 2 files changed, 38 insertions(+), 8 deletions(-)\n\ndiff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash\nindex 463a3124da..6b0432a72d 100644\n--- a/contrib/completion/git-completion.bash\n+++ b/contrib/completion/git-completion.bash\n@@ -3493,10 +3493,7 @@ __git_func_wrap ()\n \t$1\n }\n \n-# Setup completion for certain functions defined above by setting common\n-# variables and workarounds.\n-# This is NOT a public function; use at your own risk.\n-__git_complete ()\n+___git_complete ()\n {\n \tlocal wrapper=\"__git_wrap${2}\"\n \teval \"$wrapper () { __git_func_wrap $2 ; }\"\n@@ -3504,13 +3501,33 @@ __git_complete ()\n \t\t|| complete -o default -o nospace -F $wrapper $1\n }\n \n-__git_complete git __git_main\n-__git_complete gitk __gitk_main\n+# Setup the completion for git commands\n+# 1: command or alias\n+# 2: function to call (e.g. `git`, `gitk`, `git_fetch`)\n+__git_complete ()\n+{\n+\tlocal func\n+\n+\tif [[ \"$(type -t $2)\" == function ]]; then\n+\t\tfunc=$2\n+\telif [[ \"$(type -t __$2_main)\" == function ]]; then\n+\t\tfunc=__$2_main\n+\telif [[ \"$(type -t _$2)\" == function ]]; then\n+\t\tfunc=_$2\n+\telse\n+\t\techo \"ERROR: could not find function '$2'\" 1>&2\n+\t\treturn 1\n+\tfi\n+\t___git_complete $1 $func\n+}\n+\n+___git_complete git __git_main\n+___git_complete gitk __gitk_main\n \n # The following are necessary only for Cygwin, and only are needed\n # when the user has tab-completed the executable name and consequently\n # included the '.exe' suffix.\n #\n if [ \"$OSTYPE\" = cygwin ]; then\n-\t__git_complete git.exe __git_main\n+\t___git_complete git.exe __git_main\n fi\ndiff --git a/t/t9902-completion.sh b/t/t9902-completion.sh\nindex 2e59fe4de0..53669a36f1 100755\n--- a/t/t9902-completion.sh\n+++ b/t/t9902-completion.sh\n@@ -2381,10 +2381,23 @@ test_expect_success 'sourcing the completion script clears cached --options' '\n '\n \n test_expect_success '__git_complete' '\n+\tunset -f __git_wrap__git_main &&\n+\n \t__git_complete foo __git_main &&\n \ttest \"$(type -t __git_wrap__git_main)\" == function &&\n+\tunset -f __git_wrap__git_main &&\n+\n \t__git_complete gf _git_fetch &&\n-\ttest \"$(type -t __git_wrap_git_fetch)\" == function\n+\ttest \"$(type -t __git_wrap_git_fetch)\" == function &&\n+\n+\t__git_complete foo git &&\n+\ttest \"$(type -t __git_wrap__git_main)\" == function &&\n+\tunset -f __git_wrap__git_main &&\n+\n+\t__git_complete gd git_diff &&\n+\ttest \"$(type -t __git_wrap_git_diff)\" == function &&\n+\n+\ttest_must_fail __git_complete ga missing\n '\n \n test_done\n-- \n2.30.0.rc2\n\n"},{"id":"413093","messageId":"CAMP44s1e1P0KEG4O0wrgwK7GatxiufRM1d9b2kFms4-G_3zXOg@mail.gmail.com","threadId":"54895","inReplyTo":"20201228192302.80467-3-felipe.contreras@gmail.com","subject":"Re: [PATCH 2/2] completion: add proper public __git_complete","fromName":"Felipe Contreras","fromEmail":"felipe.contreras@gmail.com","sentAt":"2020-12-28T19:26:25Z","receivedAt":"2020-12-28T19:27:19Z","isPatch":true,"sender":{"key":"felipe.contreras@gmail.com","avatar":"https://avatars.githubusercontent.com/u/8358?v=4"},"body":"On Mon, Dec 28, 2020 at 1:23 PM Felipe Contreras\n<felipe.contreras@gmail.com> wrote:\n>\n> Back in 2012 I argued [1] for the introduction of a helper that would\n> allow users to specify aliases like:\n>\n>   git_complete gf git_fetch\n>\n> Back then there was pushback because there was no clear guideline for\n> public functions (git_complete vs _git_complete vs _GIT_complete), and\n> some aliases didn't actually work.\n>\n> Fast-forward to 2020 and there's still no guideline for public\n> functions, and those aliases still don't work (even though I sent the\n> fixes).\n>\n> This has not prevented people from using this function that is clearly\n> needed to setup custom aliases [2], and in fact it's the recommended\n> way. But it is cumbersome that the user must type:\n>\n>   __git_complete gf _git_fetch\n>\n> Or worse:\n>\n>   __git_complete gk __gitk_main\n>\n> 8 years is more than enough time to stop waiting for the perfect to\n> come; let's define a public function (with the same name) that is\n> actually user-friendly:\n>\n>   __git_complete gf git_fetch\n>   __git_complete gk gitk\n>\n> While also maintaining backwards compatibility.\n>\n> The logic is:\n>\n>  1. If $2 exists, use it directly\n>  2. If not, check if __$2_main exists\n>  3. If not, check if _$2 exists\n>  4. If not, fail\n>\n> [1] https://lore.kernel.org/git/1334524814-13581-1-git-send-email-felipe.contreras@gmail.com/\n> [2] https://stackoverflow.com/questions/342969/how-do-i-get-bash-completion-to-work-with-aliases\n\nFixed wrong address of SZEDER Gábor.\n\n-- \nFelipe Contreras\n"},{"id":"413095","messageId":"X+pi4NoR116Js/XI@generichostname","threadId":"54895","inReplyTo":"20201228192302.80467-2-felipe.contreras@gmail.com","subject":"Re: [PATCH 1/2] test: completion: add tests for __git_complete","fromName":"Denton Liu","fromEmail":"liu.denton@gmail.com","sentAt":"2020-12-28T22:57:36Z","receivedAt":"2020-12-28T22:58:31Z","isPatch":true,"sender":{"key":"liu.denton@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9620836?v=4"},"body":"Hi Felipe,\n\nOn Mon, Dec 28, 2020 at 01:23:01PM -0600, Felipe Contreras wrote:\n> Signed-off-by: Felipe Contreras <felipe.contreras@gmail.com>\n> ---\n>  t/t9902-completion.sh | 7 +++++++\n>  1 file changed, 7 insertions(+)\n> \n> diff --git a/t/t9902-completion.sh b/t/t9902-completion.sh\n> index a1c4f1f6d4..2e59fe4de0 100755\n> --- a/t/t9902-completion.sh\n> +++ b/t/t9902-completion.sh\n> @@ -2380,4 +2380,11 @@ test_expect_success 'sourcing the completion script clears cached --options' '\n>  \tverbose test -z \"$__gitcomp_builtin_notes_edit\"\n>  '\n>  \n> +test_expect_success '__git_complete' '\n> +\t__git_complete foo __git_main &&\n> +\ttest \"$(type -t __git_wrap__git_main)\" == function &&\n\ns/==/=/ for this and the other patch.\n\n-Denton\n\n> +\t__git_complete gf _git_fetch &&\n> +\ttest \"$(type -t __git_wrap_git_fetch)\" == function\n> +'\n> +\n>  test_done\n> -- \n> 2.30.0.rc2\n> \n"},{"id":"413113","messageId":"5fea746c536c0_22b892087a@natae.notmuch","threadId":"54895","inReplyTo":"X+pi4NoR116Js/XI@generichostname","subject":"Re: [PATCH 1/2] test: completion: add tests for __git_complete","fromName":"Felipe Contreras","fromEmail":"felipe.contreras@gmail.com","sentAt":"2020-12-29T00:12:28Z","receivedAt":"2020-12-29T00:13:12Z","isPatch":true,"sender":{"key":"felipe.contreras@gmail.com","avatar":"https://avatars.githubusercontent.com/u/8358?v=4"},"body":"Denton Liu wrote:\n> Hi Felipe,\n> \n> On Mon, Dec 28, 2020 at 01:23:01PM -0600, Felipe Contreras wrote:\n> > Signed-off-by: Felipe Contreras <felipe.contreras@gmail.com>\n> > ---\n> >  t/t9902-completion.sh | 7 +++++++\n> >  1 file changed, 7 insertions(+)\n> > \n> > diff --git a/t/t9902-completion.sh b/t/t9902-completion.sh\n> > index a1c4f1f6d4..2e59fe4de0 100755\n> > --- a/t/t9902-completion.sh\n> > +++ b/t/t9902-completion.sh\n> > @@ -2380,4 +2380,11 @@ test_expect_success 'sourcing the completion script clears cached --options' '\n> >  \tverbose test -z \"$__gitcomp_builtin_notes_edit\"\n> >  '\n> >  \n> > +test_expect_success '__git_complete' '\n> > +\t__git_complete foo __git_main &&\n> > +\ttest \"$(type -t __git_wrap__git_main)\" == function &&\n> \n> s/==/=/ for this and the other patch.\n\nSure, I'll include that in the next version.\n\nBut I expect people will make further comments on the whole method to\nfind out if a function exists.\n\n-- \nFelipe Contreras\n"},{"id":"413114","messageId":"5fea7a017271f_22e4c208a5@natae.notmuch","threadId":"54895","inReplyTo":"5fea746c536c0_22b892087a@natae.notmuch","subject":"Re: [PATCH 1/2] test: completion: add tests for __git_complete","fromName":"Felipe Contreras","fromEmail":"felipe.contreras@gmail.com","sentAt":"2020-12-29T00:36:17Z","receivedAt":"2020-12-29T00:37:01Z","isPatch":true,"sender":{"key":"felipe.contreras@gmail.com","avatar":"https://avatars.githubusercontent.com/u/8358?v=4"},"body":"Felipe Contreras wrote:\n> Denton Liu wrote:\n> > Hi Felipe,\n> > \n> > On Mon, Dec 28, 2020 at 01:23:01PM -0600, Felipe Contreras wrote:\n> > > Signed-off-by: Felipe Contreras <felipe.contreras@gmail.com>\n> > > ---\n> > >  t/t9902-completion.sh | 7 +++++++\n> > >  1 file changed, 7 insertions(+)\n> > > \n> > > diff --git a/t/t9902-completion.sh b/t/t9902-completion.sh\n> > > index a1c4f1f6d4..2e59fe4de0 100755\n> > > --- a/t/t9902-completion.sh\n> > > +++ b/t/t9902-completion.sh\n> > > @@ -2380,4 +2380,11 @@ test_expect_success 'sourcing the completion script clears cached --options' '\n> > >  \tverbose test -z \"$__gitcomp_builtin_notes_edit\"\n> > >  '\n> > >  \n> > > +test_expect_success '__git_complete' '\n> > > +\t__git_complete foo __git_main &&\n> > > +\ttest \"$(type -t __git_wrap__git_main)\" == function &&\n> > \n> > s/==/=/ for this and the other patch.\n> \n> Sure, I'll include that in the next version.\n> \n> But I expect people will make further comments on the whole method to\n> find out if a function exists.\n\nI just noticed there's already a check for that in git-completion.bash:\n\n\tif declare -f $completion_func >/dev/null 2>/dev/null\n\nI suppose that's prefered to: if test \"$(type -t func)\" = function\n\n-- \nFelipe Contreras\n"}]}