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

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

From
Junio C Hamano <gitster@pobox.com>
Date
Nov 27, 2026, 20:31 UTC
Message-ID
<xmqq7bhy819l.fsf@gitster.g>
In-Reply-To
<ee7de58093c69fbbab686a68b5b781bc60d90a2b.1795798429.git.phillip.wood@dunelm.org.uk>
Phillip Wood <phillip.wood123@gmail.com> writes:
Show 8 quoted lines
> +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.

Show 25 quoted lines
>  void sq_quote_buf_pretty(struct strbuf *dst, const char *src)
>  {
> -	static const char ok_punct[] = "+,-./:=@_^";
> -	const char *p;
> +	const char *p = src, *q;
> +	bool quote_it = false;
> +	unsigned brace = 0;
>  
>  	/* Avoid losing a zero-length string by adding '' */
> -	if (!*src) {
> +	if (!*p) {
>  		strbuf_addstr(dst, "''");
>  		return;
>  	}
>  
> -	for (p = src; *p; p++) {
> -		if (!isalnum(*p) && !strchr(ok_punct, *p)) {
> -			sq_quote_buf(dst, src);
> -			return;
> -		}
> +	/* '~' 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.
Previous: Phillip WoodNext: Phillip Wood
Message 4 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.