git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
* [PATCH 0/2] completion: make __git_complete public
@ 2020-12-28 19:23 Felipe Contreras
  2020-12-28 19:23 ` [PATCH 1/2] test: completion: add tests for __git_complete Felipe Contreras
  2020-12-28 19:23 ` [PATCH 2/2] completion: add proper public __git_complete Felipe Contreras
  0 siblings, 2 replies; 7+ messages in thread
From: Felipe Contreras @ 2020-12-28 19:23 UTC (permalink / raw)
  To: git; +Cc: SZEDER Gábor, Junio C Hamano, Jonathan Nieder,
	Felipe Contreras

Everything is explained in PATCH 2/2.

Felipe Contreras (2):
  test: completion: add tests for __git_complete
  completion: add proper public __git_complete

 contrib/completion/git-completion.bash | 31 ++++++++++++++++++++------
 t/t9902-completion.sh                  | 20 +++++++++++++++++
 2 files changed, 44 insertions(+), 7 deletions(-)

-- 
2.30.0.rc2


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH 1/2] test: completion: add tests for __git_complete
  2020-12-28 19:23 [PATCH 0/2] completion: make __git_complete public Felipe Contreras
@ 2020-12-28 19:23 ` Felipe Contreras
  2020-12-28 22:57   ` Denton Liu
  2020-12-28 19:23 ` [PATCH 2/2] completion: add proper public __git_complete Felipe Contreras
  1 sibling, 1 reply; 7+ messages in thread
From: Felipe Contreras @ 2020-12-28 19:23 UTC (permalink / raw)
  To: git; +Cc: SZEDER Gábor, Junio C Hamano, Jonathan Nieder,
	Felipe Contreras

Signed-off-by: Felipe Contreras <felipe.contreras@gmail.com>
---
 t/t9902-completion.sh | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/t/t9902-completion.sh b/t/t9902-completion.sh
index a1c4f1f6d4..2e59fe4de0 100755
--- a/t/t9902-completion.sh
+++ b/t/t9902-completion.sh
@@ -2380,4 +2380,11 @@ test_expect_success 'sourcing the completion script clears cached --options' '
 	verbose test -z "$__gitcomp_builtin_notes_edit"
 '
 
+test_expect_success '__git_complete' '
+	__git_complete foo __git_main &&
+	test "$(type -t __git_wrap__git_main)" == function &&
+	__git_complete gf _git_fetch &&
+	test "$(type -t __git_wrap_git_fetch)" == function
+'
+
 test_done
-- 
2.30.0.rc2


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH 2/2] completion: add proper public __git_complete
  2020-12-28 19:23 [PATCH 0/2] completion: make __git_complete public Felipe Contreras
  2020-12-28 19:23 ` [PATCH 1/2] test: completion: add tests for __git_complete Felipe Contreras
@ 2020-12-28 19:23 ` Felipe Contreras
  2020-12-28 19:26   ` Felipe Contreras
  1 sibling, 1 reply; 7+ messages in thread
From: Felipe Contreras @ 2020-12-28 19:23 UTC (permalink / raw)
  To: git; +Cc: SZEDER Gábor, Junio C Hamano, Jonathan Nieder,
	Felipe Contreras

Back in 2012 I argued [1] for the introduction of a helper that would
allow users to specify aliases like:

  git_complete gf git_fetch

Back then there was pushback because there was no clear guideline for
public functions (git_complete vs _git_complete vs _GIT_complete), and
some aliases didn't actually work.

Fast-forward to 2020 and there's still no guideline for public
functions, and those aliases still don't work (even though I sent the
fixes).

This has not prevented people from using this function that is clearly
needed to setup custom aliases [2], and in fact it's the recommended
way. But it is cumbersome that the user must type:

  __git_complete gf _git_fetch

Or worse:

  __git_complete gk __gitk_main

8 years is more than enough time to stop waiting for the perfect to
come; let's define a public function (with the same name) that is
actually user-friendly:

  __git_complete gf git_fetch
  __git_complete gk gitk

While also maintaining backwards compatibility.

The logic is:

 1. If $2 exists, use it directly
 2. If not, check if __$2_main exists
 3. If not, check if _$2 exists
 4. If not, fail

[1] https://lore.kernel.org/git/1334524814-13581-1-git-send-email-felipe.contreras@gmail.com/
[2] https://stackoverflow.com/questions/342969/how-do-i-get-bash-completion-to-work-with-aliases

Signed-off-by: Felipe Contreras <felipe.contreras@gmail.com>
---
 contrib/completion/git-completion.bash | 31 ++++++++++++++++++++------
 t/t9902-completion.sh                  | 15 ++++++++++++-
 2 files changed, 38 insertions(+), 8 deletions(-)

diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index 463a3124da..6b0432a72d 100644
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -3493,10 +3493,7 @@ __git_func_wrap ()
 	$1
 }
 
-# Setup completion for certain functions defined above by setting common
-# variables and workarounds.
-# This is NOT a public function; use at your own risk.
-__git_complete ()
+___git_complete ()
 {
 	local wrapper="__git_wrap${2}"
 	eval "$wrapper () { __git_func_wrap $2 ; }"
@@ -3504,13 +3501,33 @@ __git_complete ()
 		|| complete -o default -o nospace -F $wrapper $1
 }
 
-__git_complete git __git_main
-__git_complete gitk __gitk_main
+# Setup the completion for git commands
+# 1: command or alias
+# 2: function to call (e.g. `git`, `gitk`, `git_fetch`)
+__git_complete ()
+{
+	local func
+
+	if [[ "$(type -t $2)" == function ]]; then
+		func=$2
+	elif [[ "$(type -t __$2_main)" == function ]]; then
+		func=__$2_main
+	elif [[ "$(type -t _$2)" == function ]]; then
+		func=_$2
+	else
+		echo "ERROR: could not find function '$2'" 1>&2
+		return 1
+	fi
+	___git_complete $1 $func
+}
+
+___git_complete git __git_main
+___git_complete gitk __gitk_main
 
 # The following are necessary only for Cygwin, and only are needed
 # when the user has tab-completed the executable name and consequently
 # included the '.exe' suffix.
 #
 if [ "$OSTYPE" = cygwin ]; then
-	__git_complete git.exe __git_main
+	___git_complete git.exe __git_main
 fi
diff --git a/t/t9902-completion.sh b/t/t9902-completion.sh
index 2e59fe4de0..53669a36f1 100755
--- a/t/t9902-completion.sh
+++ b/t/t9902-completion.sh
@@ -2381,10 +2381,23 @@ test_expect_success 'sourcing the completion script clears cached --options' '
 '
 
 test_expect_success '__git_complete' '
+	unset -f __git_wrap__git_main &&
+
 	__git_complete foo __git_main &&
 	test "$(type -t __git_wrap__git_main)" == function &&
+	unset -f __git_wrap__git_main &&
+
 	__git_complete gf _git_fetch &&
-	test "$(type -t __git_wrap_git_fetch)" == function
+	test "$(type -t __git_wrap_git_fetch)" == function &&
+
+	__git_complete foo git &&
+	test "$(type -t __git_wrap__git_main)" == function &&
+	unset -f __git_wrap__git_main &&
+
+	__git_complete gd git_diff &&
+	test "$(type -t __git_wrap_git_diff)" == function &&
+
+	test_must_fail __git_complete ga missing
 '
 
 test_done
-- 
2.30.0.rc2


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH 2/2] completion: add proper public __git_complete
  2020-12-28 19:23 ` [PATCH 2/2] completion: add proper public __git_complete Felipe Contreras
@ 2020-12-28 19:26   ` Felipe Contreras
  0 siblings, 0 replies; 7+ messages in thread
From: Felipe Contreras @ 2020-12-28 19:26 UTC (permalink / raw)
  To: Git; +Cc: Junio C Hamano, Jonathan Nieder, SZEDER Gábor

On Mon, Dec 28, 2020 at 1:23 PM Felipe Contreras
<felipe.contreras@gmail.com> wrote:
>
> Back in 2012 I argued [1] for the introduction of a helper that would
> allow users to specify aliases like:
>
>   git_complete gf git_fetch
>
> Back then there was pushback because there was no clear guideline for
> public functions (git_complete vs _git_complete vs _GIT_complete), and
> some aliases didn't actually work.
>
> Fast-forward to 2020 and there's still no guideline for public
> functions, and those aliases still don't work (even though I sent the
> fixes).
>
> This has not prevented people from using this function that is clearly
> needed to setup custom aliases [2], and in fact it's the recommended
> way. But it is cumbersome that the user must type:
>
>   __git_complete gf _git_fetch
>
> Or worse:
>
>   __git_complete gk __gitk_main
>
> 8 years is more than enough time to stop waiting for the perfect to
> come; let's define a public function (with the same name) that is
> actually user-friendly:
>
>   __git_complete gf git_fetch
>   __git_complete gk gitk
>
> While also maintaining backwards compatibility.
>
> The logic is:
>
>  1. If $2 exists, use it directly
>  2. If not, check if __$2_main exists
>  3. If not, check if _$2 exists
>  4. If not, fail
>
> [1] https://lore.kernel.org/git/1334524814-13581-1-git-send-email-felipe.contreras@gmail.com/
> [2] https://stackoverflow.com/questions/342969/how-do-i-get-bash-completion-to-work-with-aliases

Fixed wrong address of SZEDER Gábor.

-- 
Felipe Contreras

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH 1/2] test: completion: add tests for __git_complete
  2020-12-28 19:23 ` [PATCH 1/2] test: completion: add tests for __git_complete Felipe Contreras
@ 2020-12-28 22:57   ` Denton Liu
  2020-12-29  0:12     ` Felipe Contreras
  0 siblings, 1 reply; 7+ messages in thread
From: Denton Liu @ 2020-12-28 22:57 UTC (permalink / raw)
  To: Felipe Contreras; +Cc: git, SZEDER Gábor, Junio C Hamano, Jonathan Nieder

Hi Felipe,

On Mon, Dec 28, 2020 at 01:23:01PM -0600, Felipe Contreras wrote:
> Signed-off-by: Felipe Contreras <felipe.contreras@gmail.com>
> ---
>  t/t9902-completion.sh | 7 +++++++
>  1 file changed, 7 insertions(+)
> 
> diff --git a/t/t9902-completion.sh b/t/t9902-completion.sh
> index a1c4f1f6d4..2e59fe4de0 100755
> --- a/t/t9902-completion.sh
> +++ b/t/t9902-completion.sh
> @@ -2380,4 +2380,11 @@ test_expect_success 'sourcing the completion script clears cached --options' '
>  	verbose test -z "$__gitcomp_builtin_notes_edit"
>  '
>  
> +test_expect_success '__git_complete' '
> +	__git_complete foo __git_main &&
> +	test "$(type -t __git_wrap__git_main)" == function &&

s/==/=/ for this and the other patch.

-Denton

> +	__git_complete gf _git_fetch &&
> +	test "$(type -t __git_wrap_git_fetch)" == function
> +'
> +
>  test_done
> -- 
> 2.30.0.rc2
> 

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH 1/2] test: completion: add tests for __git_complete
  2020-12-28 22:57   ` Denton Liu
@ 2020-12-29  0:12     ` Felipe Contreras
  2020-12-29  0:36       ` Felipe Contreras
  0 siblings, 1 reply; 7+ messages in thread
From: Felipe Contreras @ 2020-12-29  0:12 UTC (permalink / raw)
  To: Denton Liu, Felipe Contreras
  Cc: git, SZEDER Gábor, Junio C Hamano, Jonathan Nieder

Denton Liu wrote:
> Hi Felipe,
> 
> On Mon, Dec 28, 2020 at 01:23:01PM -0600, Felipe Contreras wrote:
> > Signed-off-by: Felipe Contreras <felipe.contreras@gmail.com>
> > ---
> >  t/t9902-completion.sh | 7 +++++++
> >  1 file changed, 7 insertions(+)
> > 
> > diff --git a/t/t9902-completion.sh b/t/t9902-completion.sh
> > index a1c4f1f6d4..2e59fe4de0 100755
> > --- a/t/t9902-completion.sh
> > +++ b/t/t9902-completion.sh
> > @@ -2380,4 +2380,11 @@ test_expect_success 'sourcing the completion script clears cached --options' '
> >  	verbose test -z "$__gitcomp_builtin_notes_edit"
> >  '
> >  
> > +test_expect_success '__git_complete' '
> > +	__git_complete foo __git_main &&
> > +	test "$(type -t __git_wrap__git_main)" == function &&
> 
> s/==/=/ for this and the other patch.

Sure, I'll include that in the next version.

But I expect people will make further comments on the whole method to
find out if a function exists.

-- 
Felipe Contreras

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH 1/2] test: completion: add tests for __git_complete
  2020-12-29  0:12     ` Felipe Contreras
@ 2020-12-29  0:36       ` Felipe Contreras
  0 siblings, 0 replies; 7+ messages in thread
From: Felipe Contreras @ 2020-12-29  0:36 UTC (permalink / raw)
  To: Felipe Contreras, Denton Liu, Felipe Contreras
  Cc: git, SZEDER Gábor, Junio C Hamano, Jonathan Nieder

Felipe Contreras wrote:
> Denton Liu wrote:
> > Hi Felipe,
> > 
> > On Mon, Dec 28, 2020 at 01:23:01PM -0600, Felipe Contreras wrote:
> > > Signed-off-by: Felipe Contreras <felipe.contreras@gmail.com>
> > > ---
> > >  t/t9902-completion.sh | 7 +++++++
> > >  1 file changed, 7 insertions(+)
> > > 
> > > diff --git a/t/t9902-completion.sh b/t/t9902-completion.sh
> > > index a1c4f1f6d4..2e59fe4de0 100755
> > > --- a/t/t9902-completion.sh
> > > +++ b/t/t9902-completion.sh
> > > @@ -2380,4 +2380,11 @@ test_expect_success 'sourcing the completion script clears cached --options' '
> > >  	verbose test -z "$__gitcomp_builtin_notes_edit"
> > >  '
> > >  
> > > +test_expect_success '__git_complete' '
> > > +	__git_complete foo __git_main &&
> > > +	test "$(type -t __git_wrap__git_main)" == function &&
> > 
> > s/==/=/ for this and the other patch.
> 
> Sure, I'll include that in the next version.
> 
> But I expect people will make further comments on the whole method to
> find out if a function exists.

I just noticed there's already a check for that in git-completion.bash:

	if declare -f $completion_func >/dev/null 2>/dev/null

I suppose that's prefered to: if test "$(type -t func)" = function

-- 
Felipe Contreras

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2020-12-29  0:37 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-12-28 19:23 [PATCH 0/2] completion: make __git_complete public Felipe Contreras
2020-12-28 19:23 ` [PATCH 1/2] test: completion: add tests for __git_complete Felipe Contreras
2020-12-28 22:57   ` Denton Liu
2020-12-29  0:12     ` Felipe Contreras
2020-12-29  0:36       ` Felipe Contreras
2020-12-28 19:23 ` [PATCH 2/2] completion: add proper public __git_complete Felipe Contreras
2020-12-28 19:26   ` Felipe Contreras

Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).