diff mbox series

commit: correctly escape @ of stashes

Message ID pull.815.git.git.1593768655179.gitgitgadget@gmail.com (mailing list archive)
State New, archived
Headers show
Series commit: correctly escape @ of stashes | expand

Commit Message

Linus Arver via GitGitGadget July 3, 2020, 9:30 a.m. UTC
From: Martin Bektchiev <martin.bektchiev@progress.com>

Autocomplete suggestions for stashes are broken due to `stash@`
being suggested without escaping.

Reproducible on `GNU bash, version 3.2.57(1)-release` and
`macOS Catalina 10.15.5`.

Signed-off-by: Martin Bektchiev <martin.bektchiev@progress.com>
---
    Correctly escape @ of stashes
    
    Autocomplete suggestions for stashes are broken due to stash@being
    suggested without escaping.
    
    Reproducible on GNU bash, version 3.2.57(1)-release
    (x86_64-apple-darwin19)and macOS Catalina 10.15.5.

Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-815%2Fmbektchiev%2Fpatch-1-v1
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-815/mbektchiev/patch-1-v1
Pull-Request: https://github.com/git/git/pull/815

 contrib/completion/git-completion.bash | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)


base-commit: a08a83db2bf27f015bec9a435f6d73e223c21c5e

Comments

Junio C Hamano July 6, 2020, 6:39 a.m. UTC | #1
"Martin Bektchiev via GitGitGadget" <gitgitgadget@gmail.com> writes:

> From: Martin Bektchiev <martin.bektchiev@progress.com>
>
> Autocomplete suggestions for stashes are broken due to `stash@`
> being suggested without escaping.

What is unclear to readers of this sentence is why '@' needs to be
quoted in the first place.  '@' is not like '$' that has a syntactic
significance to the shell.

In fact, I just tried

    $ git stash show stas<TAB>

in a test repository where there is only one stash entry and got it
completed to

    $ git stash show stash@{0}

and pressing <Enter> from this state did exactly what I expected to
see.

> Reproducible on `GNU bash, version 3.2.57(1)-release` and
> `macOS Catalina 10.15.5`.

What did you reproduce?  The completion gave me "stash@{0}" (without
surrounding double quotes) in my above experiment?  If so, that does
seem reproducible, but I do not see "suggestions ... are broken" here.

> @@ -2999,12 +2999,14 @@ _git_stash ()
>  				__git_complete_refs
>  			else
>  				__gitcomp_nl "$(__git stash list \
> -						| sed -n -e 's/:.*//p')"
> +						| sed -n -e 's/:.*//p' \
> +						| sed 's/@/\\@/')"

This is not a new problem introduced by this patch, but we should
get rid of these unnecessary backslashes and pipe at the beginning
of the second line, both of which make the resulting code harder
than necessary to read.  Ending the line with '|' pipe would give
enough clue to the shell that you haven't finished talking to it,
and you can continue to the next line without any backslashes just
fine.

>  			fi
>  			;;
>  		show,*|apply,*|drop,*|pop,*)
>  			__gitcomp_nl "$(__git stash list \
> -					| sed -n -e 's/:.*//p')"
> +					| sed -n -e 's/:.*//p' \
> +					| sed 's/@/\\@/')"

Ditto.

>  			;;
>  		*)
>  			;;
>
> base-commit: a08a83db2bf27f015bec9a435f6d73e223c21c5e
Johannes Schindelin July 6, 2020, 11:14 p.m. UTC | #2
Mirroring https://github.com/git/git/pull/815#issuecomment-654683996
manually, as I lack the time to figure out why Martin's mails don't make
it to the mailing list:

Hi Junio,

Thank you for reviewing this patch! I've amended and pushed the branch and
you can find my comments inline:


> "Martin Bektchiev via GitGitGadget" <gitgitgadget@gmail.com> writes:
>
> > From: Martin Bektchiev <martin.bektchiev@progress.com>
> >
> > Autocomplete suggestions for stashes are broken due to `stash@`
> > being suggested without escaping.
>
> What is unclear to readers of this sentence is why '@' needs to be
> quoted in the first place.  '@' is not like '$' that has a syntactic
> significance to the shell.
>
> In fact, I just tried
>
>     $ git stash show stas<TAB>
>
> in a test repository where there is only one stash entry and got it
> completed to
>
>     $ git stash show stash@{0}
>
> and pressing <Enter> from this state did exactly what I expected to
> see.
>
> > Reproducible on `GNU bash, version 3.2.57(1)-release` and
> > `macOS Catalina 10.15.5`.
>
> What did you reproduce?  The completion gave me "stash@{0}" (without
> surrounding double quotes) in my above experiment?  If so, that does
> seem reproducible, but I do not see "suggestions ... are broken" here.

The problem is visible when you have more than one stash. If
you press TABm autocompletion stops at the `{` and if you then
press TAB once more for a list of all stashes instead of that
the completion changes to `stashstash{` and becomes broken at
this point.

>
> > @@ -2999,12 +2999,14 @@ _git_stash ()
> >  				__git_complete_refs
> >  			else
> >  				__gitcomp_nl "$(__git stash list \
> > -						| sed -n -e 's/:.*//p')"
> > +						| sed -n -e 's/:.*//p' \
> > +						| sed 's/@/\\@/')"
>
> This is not a new problem introduced by this patch, but we should
> get rid of these unnecessary backslashes and pipe at the beginning
> of the second line, both of which make the resulting code harder
> than necessary to read.  Ending the line with '|' pipe would give
> enough clue to the shell that you haven't finished talking to it,
> and you can continue to the next line without any backslashes just
> fine.

Fixed.

>
> >  			fi
> >  			;;
> >  		show,*|apply,*|drop,*|pop,*)
> >  			__gitcomp_nl "$(__git stash list \
> > -					| sed -n -e 's/:.*//p')"
> > +					| sed -n -e 's/:.*//p' \
> > +					| sed 's/@/\\@/')"
>
> Ditto.

Fixed.
>
> >  			;;
> >  		*)
> >  			;;
> >
> > base-commit: a08a83db2bf27f015bec9a435f6d73e223c21c5e
>


P.S. I responded with `git send-email` but since I still cannot see my email received it might have not been sent successfully. Hopefully you'll see my comment here ...

--
You are receiving this because you commented.
Reply to this email directly or view it on GitHub:
https://github.com/git/git/pull/815#issuecomment-654683996
Junio C Hamano July 8, 2020, 8:53 p.m. UTC | #3
Johannes Schindelin <Johannes.Schindelin@gmx.de> writes:

>> In fact, I just tried
>>
>>     $ git stash show stas<TAB>
>>
>> in a test repository where there is only one stash entry and got it
>> completed to
>>
>>     $ git stash show stash@{0}
>>
>> and pressing <Enter> from this state did exactly what I expected to
>> see.
>>
>> > Reproducible on `GNU bash, version 3.2.57(1)-release` and
>> > `macOS Catalina 10.15.5`.
>>
>> What did you reproduce?  The completion gave me "stash@{0}" (without
>> surrounding double quotes) in my above experiment?  If so, that does
>> seem reproducible, but I do not see "suggestions ... are broken" here.
>
> The problem is visible when you have more than one stash. If
> you press TABm autocompletion stops at the `{` and if you then
> press TAB once more for a list of all stashes instead of that
> the completion changes to `stashstash{` and becomes broken at
> this point.

That does not reproduce for me.

    $ git stash show stas<TAB>

completes to

    $ git stash show stash@{

and any more <TAB> changes the command line, which is what I expect,
as it would be stupid to offer stash@{0} and stash@{1} (and others)
as possible completion candidates.

Also I just tried this (manually)

    $ git stash show stash\@{<TAB>

and did not get any more extra completion.

    $ set | grep ^BASH_VERSION=
    BASH_VERSION='5.0.16(1)-release'

As long as the change does not make things worse for users of newer
bash, it is OK to make things better for users of ancient versions
of bash.  It might already be considered a worse end-user experience
than what we have now to show partly-spelled stash reference as
"stash\@{" with backslash, though.

Another thing I noticed is that you shouldn't need two separate
processes of "sed" to do what you want to do.

>> > @@ -2999,12 +2999,14 @@ _git_stash ()
>> >  				__git_complete_refs
>> >  			else
>> >  				__gitcomp_nl "$(__git stash list \
>> > -						| sed -n -e 's/:.*//p')"
>> > +						| sed -n -e 's/:.*//p' \
>> > +						| sed 's/@/\\@/')"

The first and the original one -n -e "s/:.*//p" wants to show no
lines by default, unless it finds a colon on the line and strip it
and everything that follows.  You then further want to tweak that
and prefix a backslash before the first '@' you find.  So perhaps
something along the lines of...

	sed -n -e '/:/{
		s/:.*//
		s/@/\\@/
		p
	}'

... which is "show no lines by default, but on a line with a colon,
strip the colon and everything that follows, and then prefix the
first '@' on the line, if exists, with backslash, and show the result".

Having said that, I am very skeptical to any "solution" that has to
show "stash\@{" to end-users.  And with the patch, newer versions of
bash does seem to show the stupid "all stash entries, numbered",
i.e.

    $ git stash show stas<TAB>
    -->
    $ git stash show stash\@{
    
    $ git stash show stash\@{<TAB>
    stash\@{0}   stash\@{1}

I wonder what unpleasant end-user experience I may have to endure if
I had dozens of stash entries.  The list shows only the numbers, so
it does not help me decide which number to type at all.

A solution to allow older versions of bash to complete

    $ git stash show stas<TAB>
    -->
    $ git stash show stash@{

    $ git stash show stash@{<TAB>
    -->
    $ git stash show stash@{

would be very much welcomed, though.

Thanks.
Johannes Schindelin July 9, 2020, 9:13 a.m. UTC | #4
Hi Martin,

On Thu, 9 Jul 2020, martinb wrote:

> Ok, then I think it's best if I just close this PR. I didn't know that it
> worked alright in newer versions of BASH. And I haven't found a way to make
> it work without the escaping of @. It seems that the patch doesn't bring
> any value to the community after all...
>
> Cheers and thanks once more for the review and insights!
>
> Best wishes,
> Martin

I think I figured out why it does not arrive at the Git mailing list. The
mailing list administrators installed a rule where HTML mails are
rejected, and your mail came with a plain text _and_ an HTML part. So it
was rejected.

Ciao,
Johannes

>
> On Wed, Jul 8, 2020, 23:54 Junio C Hamano <gitster@pobox.com> wrote:
>
> > Johannes Schindelin <Johannes.Schindelin@gmx.de> writes:
> >
> > >> In fact, I just tried
> > >>
> > >>     $ git stash show stas<TAB>
> > >>
> > >> in a test repository where there is only one stash entry and got it
> > >> completed to
> > >>
> > >>     $ git stash show stash@{0}
> > >>
> > >> and pressing <Enter> from this state did exactly what I expected to
> > >> see.
> > >>
> > >> > Reproducible on `GNU bash, version 3.2.57(1)-release` and
> > >> > `macOS Catalina 10.15.5`.
> > >>
> > >> What did you reproduce?  The completion gave me "stash@{0}" (without
> > >> surrounding double quotes) in my above experiment?  If so, that does
> > >> seem reproducible, but I do not see "suggestions ... are broken" here.
> > >
> > > The problem is visible when you have more than one stash. If
> > > you press TABm autocompletion stops at the `{` and if you then
> > > press TAB once more for a list of all stashes instead of that
> > > the completion changes to `stashstash{` and becomes broken at
> > > this point.
> >
> > That does not reproduce for me.
> >
> >     $ git stash show stas<TAB>
> >
> > completes to
> >
> >     $ git stash show stash@{
> >
> > and any more <TAB> changes the command line, which is what I expect,
> > as it would be stupid to offer stash@{0} and stash@{1} (and others)
> > as possible completion candidates.
> >
> > Also I just tried this (manually)
> >
> >     $ git stash show stash\@{<TAB>
> >
> > and did not get any more extra completion.
> >
> >     $ set | grep ^BASH_VERSION=
> >     BASH_VERSION='5.0.16(1)-release'
> >
> > As long as the change does not make things worse for users of newer
> > bash, it is OK to make things better for users of ancient versions
> > of bash.  It might already be considered a worse end-user experience
> > than what we have now to show partly-spelled stash reference as
> > "stash\@{" with backslash, though.
> >
> > Another thing I noticed is that you shouldn't need two separate
> > processes of "sed" to do what you want to do.
> >
> > >> > @@ -2999,12 +2999,14 @@ _git_stash ()
> > >> >                            __git_complete_refs
> > >> >                    else
> > >> >                            __gitcomp_nl "$(__git stash list \
> > >> > -                                          | sed -n -e 's/:.*//p')"
> > >> > +                                          | sed -n -e 's/:.*//p' \
> > >> > +                                          | sed 's/@/\\@/')"
> >
> > The first and the original one -n -e "s/:.*//p" wants to show no
> > lines by default, unless it finds a colon on the line and strip it
> > and everything that follows.  You then further want to tweak that
> > and prefix a backslash before the first '@' you find.  So perhaps
> > something along the lines of...
> >
> >         sed -n -e '/:/{
> >                 s/:.*//
> >                 s/@/\\@/
> >                 p
> >         }'
> >
> > ... which is "show no lines by default, but on a line with a colon,
> > strip the colon and everything that follows, and then prefix the
> > first '@' on the line, if exists, with backslash, and show the result".
> >
> > Having said that, I am very skeptical to any "solution" that has to
> > show "stash\@{" to end-users.  And with the patch, newer versions of
> > bash does seem to show the stupid "all stash entries, numbered",
> > i.e.
> >
> >     $ git stash show stas<TAB>
> >     -->
> >     $ git stash show stash\@{
> >
> >     $ git stash show stash\@{<TAB>
> >     stash\@{0}   stash\@{1}
> >
> > I wonder what unpleasant end-user experience I may have to endure if
> > I had dozens of stash entries.  The list shows only the numbers, so
> > it does not help me decide which number to type at all.
> >
> > A solution to allow older versions of bash to complete
> >
> >     $ git stash show stas<TAB>
> >     -->
> >     $ git stash show stash@{
> >
> >     $ git stash show stash@{<TAB>
> >     -->
> >     $ git stash show stash@{
> >
> > would be very much welcomed, though.
> >
> > Thanks.
> >
> >
> >
>
diff mbox series

Patch

diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index de5d0fbbd1..986a7352ef 100644
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -2999,12 +2999,14 @@  _git_stash ()
 				__git_complete_refs
 			else
 				__gitcomp_nl "$(__git stash list \
-						| sed -n -e 's/:.*//p')"
+						| sed -n -e 's/:.*//p' \
+						| sed 's/@/\\@/')"
 			fi
 			;;
 		show,*|apply,*|drop,*|pop,*)
 			__gitcomp_nl "$(__git stash list \
-					| sed -n -e 's/:.*//p')"
+					| sed -n -e 's/:.*//p' \
+					| sed 's/@/\\@/')"
 			;;
 		*)
 			;;