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

Re: [PATCH] contrib/completion: escape the forward slash in __git_match_ctag

From
Junio C Hamano <gitster@pobox.com>
Date
Mar 15, 2015, 01:58 UTC
Message-ID
<xmqq7fujvx6l.fsf@gitster.dls.corp.google.com>
In-Reply-To
<1426340439-7171-1-git-send-email-john@szakmeister.net>
John Szakmeister <john@szakmeister.net> writes:
Show 41 quoted lines
> The current definition results in an incorrect expansion of the term under zsh.
> For instance "/^${1////\\/}/" under zsh with the argument "hi" results in:
>     /^/\/h/\/i/
>
> This results in an output similar to this when trying to complete `git grep
> chartab` under zsh:
>
>     :: git grep chartabawk: cmd. line:1: /^/\/c/\/h/\/a/\/r/\/t/\/a/\/b/ { print $1 }
>     awk: cmd. line:1:    ^ backslash not last character on line
>     awk: cmd. line:1: /^/\/c/\/h/\/a/\/r/\/t/\/a/\/b/ { print $1 }
>     awk: cmd. line:1:    ^ syntax error
>
> Leaving the prompt in a goofy state until the user hits a key.
>
> Escaping the literal / in the parameter expansion (using "/^${1//\//\\/}/")
> results in:
>     /^chartab/
>
> allowing the completion to work correctly.
>
> This formulation also works under bash.
>
> Signed-off-by: John Szakmeister <john@szakmeister.net>
> ---
>
> I've been bit by this bug quite a bit, but didn't have time to track it down
> until today.  I hope the proposed solution is acceptable.
>
>  contrib/completion/git-completion.bash | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
> index c21190d..a899234 100644
> --- a/contrib/completion/git-completion.bash
> +++ b/contrib/completion/git-completion.bash
> @@ -1305,7 +1305,7 @@ _git_gitk ()
>  }
>  
>  __git_match_ctag() {
> -	awk "/^${1////\\/}/ { print \$1 }" "$2"
> +	awk "/^${1//\//\\/}/ { print \$1 }" "$2"

The updated pattern look sensible to me. / to start the pattern part, extra / to say "repeatedly replace all", \/ to say "a single slash is what is to be replaced, / to say "here is where the pattern ends", and then \\/ to say "replace with backslash-slash".

In fact, I have to suspect that the original working by pure accident or a bug.

Thanks.
>  }
>  
>  _git_grep ()
Previous: John Szakmeister
Message 2 of 2 in “contrib/completion: escape the forward slash in __git_match_ctag”
  1. contrib/completion: escape the forward slash in __git_match_ctagJohn Szakmeister, Mar 14, 2015
  2. Junio C HamanoMar 15, 2015

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.