git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH] completion: do not cache if --git-completion-helper fails

From
SZEDER Gábor <szeder.dev@gmail.com>
Date
Jun 7, 2019, 10:02 UTC
Message-ID
<20190607100252.GA24208@szeder.dev>
In-Reply-To
<20190607093034.816-1-pclouds@gmail.com>
On Fri, Jun 07, 2019 at 04:30:34PM +0700, Nguyễn Thái Ngọc Duy wrote:
Show 11 quoted lines
> "git <cmd> --git-completion-helper" could fail if the command checks for
> a repo before parse_options(). If the result is cached, later on when
> the user moves to a worktree with repo, tab completion will still fail.
> 
> Avoid this by detecting errors and not cache the completion output. We
> can try again and hopefully succeed next time (e.g. when a repo is
> found).
> 
> Of course if --git-completion-helper fails permanently because of other
> reasons (*), this will slow down completion. But I don't see any better
> option to handle that case.

I think a permanently failing 'git cmd --git-completion-helper' shouldn't really happen, unless there is a bug in the completion script or the git installation or similar exceptional situation. And then that issue should be fixed, but I don't think we should worry about an extra subshell and git process in those situations.

Show 28 quoted lines
> (*) one of those cases is if __gitcomp_builtin is called on a command
>   that does not support --git-completion-helper. And we do have a
>   generic call
> 
>     __git_complete_common "$command"
> 
>   but this case is protected with __git_support_parseopt_helper so we're
>   good.
> 
> Reported-by: Felipe Contreras <felipe.contreras@gmail.com>
> Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
> ---
>  contrib/completion/git-completion.bash | 6 ++++--
>  1 file changed, 4 insertions(+), 2 deletions(-)
> 
> diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
> index 9f71bcde96..a515de8501 100644
> --- a/contrib/completion/git-completion.bash
> +++ b/contrib/completion/git-completion.bash
> @@ -398,13 +398,15 @@ __gitcomp_builtin ()
>  	eval "options=\$$var"
>  
>  	if [ -z "$options" ]; then
> +		local nocache=
>  		# leading and trailing spaces are significant to make
>  		# option removal work correctly.
> -		options=" $incl $(__git ${cmd/_/ } --git-completion-helper) "
> +		options=" $incl $(__git ${cmd/_/ } --git-completion-helper) " || nocache=t
Why not just 'return' here?

If we return, then it won't list any options, and the shell should fall back to regular file completion. With this patch it will still list extra options, if there are any, and I don't really see the point in doing that.

Show 12 quoted lines
> +
>  		for i in $excl; do
>  			options="${options/ $i / }"
>  		done
> -		eval "$var=\"$options\""
> +		test -n "$nocache" || eval "$var=\"$options\""
>  	fi
>  
>  	__gitcomp "$options"
> -- 
> 2.22.0.rc0.322.g2b0371e29a
> 
Previous: Nguyễn Thái Ngọc DuyNext: Felipe Contreras
Message 3 of 12 in “test: completion: tests for __gitcomp regression”
  1. test: completion: tests for __gitcomp regressionFelipe Contreras, Jun 7, 2019
  2. completion: do not cache if --git-completion-helper failsNguyễn Thái Ngọc Duy, Jun 7, 2019
  3. SZEDER GáborJun 7, 2019
  4. Felipe ContrerasJun 7, 2019
  5. Duy NguyenJun 12, 2019
  6. Felipe ContrerasJun 14, 2019
  7. Duy NguyenJun 14, 2019
  8. Felipe ContrerasJun 14, 2019
  9. Duy NguyenJun 16, 2019
  10. Junio C HamanoJun 7, 2019
  11. completion: do not cache if --git-completion-helper failsNguyễn Thái Ngọc Duy, Jun 12, 2019
  12. Junio C HamanoJun 12, 2019

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.