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

Re: [RFC PATCH] quote: make sq_quote_buf_pretty() prettier

From
PWPhillip Wood <phillip.wood123@gmail.com>
Date
Nov 30, 2026, 16:39 UTC
Message-ID
<01526f4d-b40e-4288-aa22-441c3e6e68e2@gmail.com>
In-Reply-To
<xmqq7bhy819l.fsf@gitster.g>
On 27/11/2026 20:31, Junio C Hamano wrote:
Show 14 quoted lines
> 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
Show 12 quoted lines
>> +	/* '~' 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
Previous: Junio C HamanoNext: Junio C Hamano
Message 5 of 10 in “quote: make sq_quote_buf_pretty() prettier”
  1. quote: make sq_quote_buf_pretty() prettierPhillip Wood, Nov 27, 2026
  2. Nahid hasan SagorNov 27, 2026
  3. Phillip WoodNov 30, 2026
  4. Junio C HamanoNov 27, 2026
  5. Phillip WoodNov 30, 2026
  6. Junio C HamanoNov 28, 2026
  7. Phillip WoodNov 30, 2026
  8. D. Ben KnobleNov 30, 2026
  9. Phillip WoodNov 30, 2026
  10. D. Ben KnobleNov 30, 2026

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.