Git development
 help / color / mirror / Atom feed
From: Phillip Wood <phillip.wood123@gmail.com>
To: Junio C Hamano <gitster@pobox.com>
Cc: git@vger.kernel.org, Ben Knoble <ben.knoble@gmail.com>,
	Harald Nordgren <haraldnordgren@gmail.com>,
	MD Nahid Hasan Sagor <nahidhasan141400@gmail.com>
Subject: Re: [RFC PATCH] quote: make sq_quote_buf_pretty() prettier
Date: Mon, 30 Nov 2026 16:39:54 +0000	[thread overview]
Message-ID: <01526f4d-b40e-4288-aa22-441c3e6e68e2@gmail.com> (raw)
In-Reply-To: <xmqq7bhy819l.fsf@gitster.g>

On 27/11/2026 20:31, Junio C Hamano wrote:
> Phillip Wood <phillip.wood123@gmail.com> writes:
> 
>> +static bool needs_sq_quote(char c, unsigned brace)
>> +{
>> +	if (isalnum(c) || (unsigned char)c > 0x7f)
>> +		return false;
>> +
>> +	switch (c) {
>> +	case ',':
>> +		return brace > 0; /* Prevent brace expansion */
> 
> Web search tells me that bash, zsh, and ksh93 expands sequence
> expressions such as "{1..5}" or "{a..z}" without any comma in the pair
> of braces.  Because '.' returns false unconditionally here and no other
> check triggers quoting for "..", sq_quote_buf_pretty() leaves "{1..5}"
> unquoted, which expands to five arguments if pasted into those shells.

Oh, thanks for pointing that out. I think it should be fairly easy to 
detect '..' when brace != 0

>> +	/* '~' is only special if it is the first character */
>> +	if (*p == '~') {
>> +		quote_it = true;
>> +		p++;
>>   	}
> 
> As noticed by Nahid already, this loses the leading '~'.  Also, do we
> want to do something to this corner case?
> 
>      $ echo P=A:~/B:C
>      P=A:/home/gitster/B:C
> 
> i.e., bash and friends seem to expand ~ that is not at the beginning of
> value when the whole thing looks like a variable assignment.  Without
> "P=" at front, this does not seem to expand.
Thanks, having read the manuals bash, zsh and ksh all have slightly 
different behaviors (and the zsh behavior depends on which options are 
set). Quoting strings that start with '~', or contain '=~' or ':~', is 
sufficient to prevent tilde expansion while avoiding quoting 'HEAD~1'.

zsh has an EXTENDED_GLOB option where '<pattern1>~<pattern2>' matches 
everything that <pattern1> would match that <pattern2> does not. 
Luckily, it seems to need another special character in one of the 
patterns to trigger it so we don't need to quote "HEAD~1". The 
EXTENDED_GLOB option also treats '^' specially, so to be safe we should 
be quoting anything containing '^' which we don't at the moment. That's 
a shame it means we need to quote "HEAD^2". EXTENDED_GLOB is not enabled 
by default, but I think it is worth making our output safe when it is 
enabled.

By default, zsh also expands "=cmd" to the absolute path of "cmd" in the 
same places as '~' would be subject to tilde expansion.

On a related note our treatment of assignments is a bit strange - if the 
right hand side needs quoting then we quote the whole thing which 
disables the assignment, but we don't quote it otherwise. When 
sq_quote_buf_pretty() was introduced the aim seems to have been to be 
able to copy and paste tracing output to rerun the command being traced, 
which would imply we don't want to disable assignments when the right 
hand side needs to be quoted.

I'll try and re-roll later this week

Thanks

Phillip

  reply	other threads:[~2026-11-30 16:39 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-27 16:54 [RFC PATCH] quote: make sq_quote_buf_pretty() prettier Phillip Wood
2026-11-27 18:25 ` Nahid hasan Sagor
2026-11-30 14:51   ` Phillip Wood
2026-11-27 20:31 ` Junio C Hamano
2026-11-30 16:39   ` Phillip Wood [this message]
2026-11-28  0:26 ` Junio C Hamano
2026-11-30 16:40   ` Phillip Wood
2026-11-30 15:19 ` D. Ben Knoble
2026-11-30 16:38   ` Phillip Wood
2026-11-30 17:02     ` D. Ben Knoble

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=01526f4d-b40e-4288-aa22-441c3e6e68e2@gmail.com \
    --to=phillip.wood123@gmail.com \
    --cc=ben.knoble@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=haraldnordgren@gmail.com \
    --cc=nahidhasan141400@gmail.com \
    --cc=phillip.wood@dunelm.org.uk \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox