On 27/11/2026 20:31, Junio C Hamano wrote:
> Phillip Wood <phil…@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