{"thread":{"id":"66945","subject":"[RFC PATCH] quote: make sq_quote_buf_pretty() prettier","startedAt":"2026-11-27T16:54:27Z","lastAt":"2026-11-30T17:02:05Z","messageCount":10,"participants":["Phillip Wood","Nahid hasan Sagor","Junio C Hamano","D. Ben Knoble"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"559537","messageId":"ee7de58093c69fbbab686a68b5b781bc60d90a2b.1795798429.git.phillip.wood@dunelm.org.uk","threadId":"66945","inReplyTo":null,"subject":"[RFC PATCH] quote: make sq_quote_buf_pretty() prettier","fromName":"Phillip Wood","fromEmail":"phillip.wood123@gmail.com","sentAt":"2026-11-27T16:54:27Z","receivedAt":"2026-11-27T16:54:27Z","isPatch":true,"sender":{"key":"phillip.wood@dunelm.org.uk","avatar":null},"body":"From: Phillip Wood <phillip.wood@dunelm.org.uk>\n\nThe output of sq_quote_buf_pretty() is intended for human consumption\nand as such it tries to avoid unnecessary quoting. However, it\nis over enthusiastic about adding quotes to multibyte strings and\nstrings containing '~', '{' or '}'. It also adds unnecessary pairs\nof empty quotes when a string starts or ends with a single quote. Fix\nthis by using a dedicated quoting routine that tries to avoid adding\nunnecessary quotes, rather than reusing sq_quote_buf(). Tilde is only\nquoted if it is the first character. Braces are only quoted if there is\na mismatch between the number of opening and closing braces, or they\ncontain a comma or a character that needs quoting. Sections between\nbackslash escapes are only quoted if they contain characters that need\nto be quoted. The table below shows the change in output for various\ninputs. The aim is to make the output more readable without changing\nthe meaning of the quoted output were it to be evaluated by the shell.\n\nWe could further improve the readability by dropping support for csh\nwhich requires that '!' is backslash escaped. It is not clear to me how\nwidely used csh is these days, none of the big three BSD distributions\nseem to have used it as the default shell for a number of years.\n\nText\t\tCurrent output\t\tNew output\ndanger!\t\t'danger'\\!''\t\tdanger\\!\n'look out!'\t''\\''look out'\\!''\\'''\t\\''look out'\\!\\'\nDvořák\t\t'Dvořák'\t\tDvořák\nHEAD~3\t\t'HEAD~3'\t\tHEAD~3\nHEAD@{1}\t'HEAD@{1}'\t\tHEAD@{1}\nHEAD^{/,}\t'HEAD^{/,}'\t\t'HEAD^{/,}'\n\nSigned-off-by: Phillip Wood <phillip.wood@dunelm.org.uk>\n---\nFollowing on from the discussion at [1] this tries to improve the\nreadability of the output without changing which shells we support\nwhich is a bigger change that requires more thought and discussion. If\nwe do decide to go the route of per-shell quoting the changes here\nwill be useful for supporting POSIX shells.\n\nThere aren't any tests yet, though I have tested it manually\nby looking at the trace output from commands like\n\n   GIT_TRACE=1 git -c alias.z='log -1 HEAD~1' z\n\nI'll add some tests if there is interest in this patch. On that subject,\ndoes anyone know how to easily write table driven unit tests in clar\nwith a separate test for each input?\n\n[1] https://lore.kernel.org/git/02A50A47-A048-417E-9D1E-0C12D8FB1476@gmail.com\n\nbase-commit: 77904e546de3bd940621092ecec47a88d0cede5d\nPublished-As: https://github.com/phillipwood/git/releases/tag/pw%2Fsq-quote-prettier%2Frfc-v1\nView-Changes-At: https://github.com/phillipwood/git/compare/77904e546...ee7de5809\nFetch-It-Via: git fetch https://github.com/phillipwood/git pw/sq-quote-prettier/rfc-v1\n\n quote.c | 73 +++++++++++++++++++++++++++++++++++++++++++++++++--------\n 1 file changed, 63 insertions(+), 10 deletions(-)\n\ndiff --git a/quote.c b/quote.c\nindex 235fac8e470..dfd379a102f 100644\n--- a/quote.c\n+++ b/quote.c\n@@ -47,26 +47,79 @@ void sq_quote_buf(struct strbuf *dst, const char *src)\n \tfree(to_free);\n }\n \n+static bool needs_sq_quote(char c, unsigned brace)\n+{\n+\tif (isalnum(c) || (unsigned char)c > 0x7f)\n+\t\treturn false;\n+\n+\tswitch (c) {\n+\tcase ',':\n+\t\treturn brace > 0; /* Prevent brace expansion */\n+\tcase '}':\n+\t\treturn !brace; /* Quote unpaired '}' */\n+\tcase '~':\n+\tcase '+':\n+\tcase '-':\n+\tcase '.':\n+\tcase '/':\n+\tcase ':':\n+\tcase '=':\n+\tcase '@':\n+\tcase '_':\n+\tcase '^':\n+\t\treturn false;\n+\tdefault:\n+\t\treturn true;\n+\t}\n+}\n+\n void sq_quote_buf_pretty(struct strbuf *dst, const char *src)\n {\n-\tstatic const char ok_punct[] = \"+,-./:=@_^\";\n-\tconst char *p;\n+\tconst char *p = src, *q;\n+\tbool quote_it = false;\n+\tunsigned brace = 0;\n \n \t/* Avoid losing a zero-length string by adding '' */\n-\tif (!*src) {\n+\tif (!*p) {\n \t\tstrbuf_addstr(dst, \"''\");\n \t\treturn;\n \t}\n \n-\tfor (p = src; *p; p++) {\n-\t\tif (!isalnum(*p) && !strchr(ok_punct, *p)) {\n-\t\t\tsq_quote_buf(dst, src);\n-\t\t\treturn;\n-\t\t}\n+\t/* '~' is only special if it is the first character */\n+\tif (*p == '~') {\n+\t\tquote_it = true;\n+\t\tp++;\n \t}\n \n-\t/* if we get here, we did not need quoting */\n-\tstrbuf_addstr(dst, src);\n+\tfor (q = p; *q; q++) {\n+\t\tif (need_bs_quote(*q)) {\n+\t\t\tif (brace)\n+\t\t\t\tquote_it = true;\n+\t\t\tif (quote_it)\n+\t\t\t\tstrbuf_addch(dst, '\\'');\n+\t\t\tstrbuf_add(dst, p, q - p);\n+\t\t\tp = q + 1;\n+\t\t\tif (quote_it)\n+\t\t\t\tstrbuf_addch(dst, '\\'');\n+\t\t\tstrbuf_addch(dst, '\\\\');\n+\t\t\tstrbuf_addch(dst, *q);\n+\t\t\tif (!brace)\n+\t\t\t\tquote_it = false;\n+\t\t} else if (*q == '{') {\n+\t\t\tbrace++;\n+\t\t} else if (*q == '}' && brace) {\n+\t\t\tbrace--;\n+\t\t} else if (!quote_it && needs_sq_quote(*q, brace)) {\n+\t\t\tquote_it = true;\n+\t\t}\n+\t}\n+\tif (brace && !quote_it)\n+\t\tquote_it = true; /* Quote unpaired '{' */\n+\tif (quote_it)\n+\t\tstrbuf_addch(dst, '\\'');\n+\tstrbuf_add(dst, p, q - p);\n+\tif (quote_it)\n+\t\tstrbuf_addch(dst, '\\'');\n }\n \n void sq_quotef(struct strbuf *dst, const char *fmt, ...)\n-- \n2.56.1.576.g0cfbda1d5c7.dirty\n\n\n"},{"id":"559544","messageId":"CABQNq+mnfJKohF5F4aQZ8APZk1bvOKo0F6e4FPrMuBvG1CpanA@mail.gmail.com","threadId":"66945","inReplyTo":"ee7de58093c69fbbab686a68b5b781bc60d90a2b.1795798429.git.phillip.wood@dunelm.org.uk","subject":"Re: [RFC PATCH] quote: make sq_quote_buf_pretty() prettier","fromName":"Nahid hasan Sagor","fromEmail":"nahidhasan141400@gmail.com","sentAt":"2026-11-27T18:25:22Z","receivedAt":"2026-11-27T18:25:22Z","isPatch":true,"sender":{"key":"nahidhasan141400@gmail.com","avatar":null},"body":"> +       /* '~' is only special if it is the first character */\n> +       if (*p == '~') {\n> +               quote_it = true;\n> +               p++;\n>         }\n\nI may be misreading, but I think a leading '~' gets lost after the\n'p++', so it never gets copied by strbuf_add(dst, p, q - p) later. That\nwould make \"~foo\" come out as \"'foo'\".\n\n"},{"id":"559552","messageId":"xmqq7bhy819l.fsf@gitster.g","threadId":"66945","inReplyTo":"ee7de58093c69fbbab686a68b5b781bc60d90a2b.1795798429.git.phillip.wood@dunelm.org.uk","subject":"Re: [RFC PATCH] quote: make sq_quote_buf_pretty() prettier","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-11-27T20:31:18Z","receivedAt":"2026-11-27T20:31:18Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Phillip Wood <phillip.wood123@gmail.com> writes:\n\n> +static bool needs_sq_quote(char c, unsigned brace)\n> +{\n> +\tif (isalnum(c) || (unsigned char)c > 0x7f)\n> +\t\treturn false;\n> +\n> +\tswitch (c) {\n> +\tcase ',':\n> +\t\treturn brace > 0; /* Prevent brace expansion */\n\nWeb search tells me that bash, zsh, and ksh93 expands sequence\nexpressions such as \"{1..5}\" or \"{a..z}\" without any comma in the pair\nof braces.  Because '.' returns false unconditionally here and no other\ncheck triggers quoting for \"..\", sq_quote_buf_pretty() leaves \"{1..5}\"\nunquoted, which expands to five arguments if pasted into those shells.\n\n>  void sq_quote_buf_pretty(struct strbuf *dst, const char *src)\n>  {\n> -\tstatic const char ok_punct[] = \"+,-./:=@_^\";\n> -\tconst char *p;\n> +\tconst char *p = src, *q;\n> +\tbool quote_it = false;\n> +\tunsigned brace = 0;\n>  \n>  \t/* Avoid losing a zero-length string by adding '' */\n> -\tif (!*src) {\n> +\tif (!*p) {\n>  \t\tstrbuf_addstr(dst, \"''\");\n>  \t\treturn;\n>  \t}\n>  \n> -\tfor (p = src; *p; p++) {\n> -\t\tif (!isalnum(*p) && !strchr(ok_punct, *p)) {\n> -\t\t\tsq_quote_buf(dst, src);\n> -\t\t\treturn;\n> -\t\t}\n> +\t/* '~' is only special if it is the first character */\n> +\tif (*p == '~') {\n> +\t\tquote_it = true;\n> +\t\tp++;\n>  \t}\n\nAs noticed by Nahid already, this loses the leading '~'.  Also, do we\nwant to do something to this corner case?\n\n    $ echo P=A:~/B:C\n    P=A:/home/gitster/B:C\n\ni.e., bash and friends seem to expand ~ that is not at the beginning of\nvalue when the whole thing looks like a variable assignment.  Without\n\"P=\" at front, this does not seem to expand.\n\nThanks.\n\n"},{"id":"559572","messageId":"xmqqmrqt3ior.fsf@gitster.g","threadId":"66945","inReplyTo":"ee7de58093c69fbbab686a68b5b781bc60d90a2b.1795798429.git.phillip.wood@dunelm.org.uk","subject":"Re: [RFC PATCH] quote: make sq_quote_buf_pretty() prettier","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-11-28T00:26:12Z","receivedAt":"2026-11-28T00:26:12Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Phillip Wood <phillip.wood123@gmail.com> writes:\n\nI forgot to mention a couple of things.\n\n> ... It also adds unnecessary pairs\n> of empty quotes when a string starts or ends with a single quote.\n\n\"a single quote or an exclamation point.\"  We can see the example in the\ntable you have below, for the entry for 'danger!'.\n\n> +\tfor (q = p; *q; q++) {\n> +\t\tif (need_bs_quote(*q)) {\n> +\t\t\tif (brace)\n> +\t\t\t\tquote_it = true;\n> +\t\t\tif (quote_it)\n> +\t\t\t\tstrbuf_addch(dst, '\\'');\n> +\t\t\tstrbuf_add(dst, p, q - p);\n> +\t\t\tp = q + 1;\n> +\t\t\tif (quote_it)\n> +\t\t\t\tstrbuf_addch(dst, '\\'');\n> +\t\t\tstrbuf_addch(dst, '\\\\');\n> +\t\t\tstrbuf_addch(dst, *q);\n> +\t\t\tif (!brace)\n> +\t\t\t\tquote_it = false;\n\nWhen brace > 0, quote_it stays true after emitting the backslash-escaped\ncharacter.\n\nIf the next character also needs backslash quoting (such as \"{a'!b}\"),\nor if the string ends right there with an unclosed brace (such as \"{!\",\np == q (i.e., an empty segment) but we emit it in a pair of single\nquotes.\n\n> +\t\t} else if (*q == '{') {\n> +\t\t\tbrace++;\n> +\t\t} else if (*q == '}' && brace) {\n> +\t\t\tbrace--;\n> +\t\t} else if (!quote_it && needs_sq_quote(*q, brace)) {\n> +\t\t\tquote_it = true;\n> +\t\t}\n> +\t}\n> +\tif (brace && !quote_it)\n> +\t\tquote_it = true; /* Quote unpaired '{' */\n> +\tif (quote_it)\n> +\t\tstrbuf_addch(dst, '\\'');\n> +\tstrbuf_add(dst, p, q - p);\n> +\tif (quote_it)\n> +\t\tstrbuf_addch(dst, '\\'');\n\nAfter the loop, here also is an instance of single quote pair around\npotentially an empty segment.  Perhaps a short helper or macro that does\nan equivalent of\n\n\tif (p < q) {\n\t\tif (quote_it)\n\t\t\tstrbuf_addch(dst, '\\'');\n\t\tstrbuf_add(dst, p, q - p);\n\t\tif (quote_it)\n\t\t\tstrbuf_addch(dst, '\\'');\n\t}\t\t\n\nand use it twice (the bs-quote code path needs to be slightly rewritten\nif we wanted to share this piece of code that way)?\n\n>  }\n>  \n>  void sq_quotef(struct strbuf *dst, const char *fmt, ...)\n\n"},{"id":"559700","messageId":"030b99fb-1176-4514-9787-fcd733649eb7@gmail.com","threadId":"66945","inReplyTo":"CABQNq+mnfJKohF5F4aQZ8APZk1bvOKo0F6e4FPrMuBvG1CpanA@mail.gmail.com","subject":"Re: [RFC PATCH] quote: make sq_quote_buf_pretty() prettier","fromName":"Phillip Wood","fromEmail":"phillip.wood123@gmail.com","sentAt":"2026-11-30T14:51:08Z","receivedAt":"2026-11-30T14:51:08Z","isPatch":true,"sender":{"key":"phillip.wood@dunelm.org.uk","avatar":null},"body":"Hi Nahid\n\nOn 27/11/2026 18:25, Nahid hasan Sagor wrote:\n>> +       /* '~' is only special if it is the first character */\n>> +       if (*p == '~') {\n>> +               quote_it = true;\n>> +               p++;\n>>          }\n> > I may be misreading, but I think a leading '~' gets lost after the\n> 'p++', so it never gets copied by strbuf_add(dst, p, q - p) later. That\n> would make \"~foo\" come out as \"'foo'\".\n\nYou're right - good catch. Junio has pointed out some other problems with the handling of '~' as well so I'll rework this.\n\nThanks\n\nPhillip\n\n"},{"id":"559703","messageId":"CALnO6CDyTh+O_S6Q6Nq46wckLGXdvTKJmU6uK_SKn5S=p4X20g@mail.gmail.com","threadId":"66945","inReplyTo":"ee7de58093c69fbbab686a68b5b781bc60d90a2b.1795798429.git.phillip.wood@dunelm.org.uk","subject":"Re: [RFC PATCH] quote: make sq_quote_buf_pretty() prettier","fromName":"D. Ben Knoble","fromEmail":"ben.knoble@gmail.com","sentAt":"2026-11-30T15:19:14Z","receivedAt":"2026-11-30T15:19:14Z","isPatch":true,"sender":{"key":"ben.knoble@gmail.com","avatar":"https://avatars.githubusercontent.com/u/22802209?v=4"},"body":"Hi Phillip, thanks for working on this!\n\nOn Fri, Nov 27, 2026 at 11:54 AM Phillip Wood <phillip.wood123@gmail.com> wrote:\n\n>  void sq_quote_buf_pretty(struct strbuf *dst, const char *src)\n>  {\n> -       static const char ok_punct[] = \"+,-./:=@_^\";\n> -       const char *p;\n> +       const char *p = src, *q;\n> +       bool quote_it = false;\n> +       unsigned brace = 0;\n>\n>         /* Avoid losing a zero-length string by adding '' */\n> -       if (!*src) {\n> +       if (!*p) {\n>                 strbuf_addstr(dst, \"''\");\n>                 return;\n>         }\n>\n> -       for (p = src; *p; p++) {\n> -               if (!isalnum(*p) && !strchr(ok_punct, *p)) {\n> -                       sq_quote_buf(dst, src);\n> -                       return;\n> -               }\n> +       /* '~' is only special if it is the first character */\n> +       if (*p == '~') {\n> +               quote_it = true;\n> +               p++;\n>         }\n>\n> -       /* if we get here, we did not need quoting */\n> -       strbuf_addstr(dst, src);\n> +       for (q = p; *q; q++) {\n> +               if (need_bs_quote(*q)) {\n> +                       if (brace)\n> +                               quote_it = true;\n> +                       if (quote_it)\n> +                               strbuf_addch(dst, '\\'');\n> +                       strbuf_add(dst, p, q - p);\n> +                       p = q + 1;\n> +                       if (quote_it)\n> +                               strbuf_addch(dst, '\\'');\n> +                       strbuf_addch(dst, '\\\\');\n> +                       strbuf_addch(dst, *q);\n> +                       if (!brace)\n> +                               quote_it = false;\n> +               } else if (*q == '{') {\n> +                       brace++;\n> +               } else if (*q == '}' && brace) {\n> +                       brace--;\n\nI think this simple counter may count wrongly for scenarios like\n\"}{a,b}\", but I haven't tried it myself yet. My shells (Zsh and Bash)\nboth do the following:\n\n    $ echo }{a,b}\n    }a }b\n\nand I *think* that brace==0 when we see the comma here, even though it\nwould need quoted to avoid expansion.\n\nI think we are unlikely to emit such strange punctuation ourselves,\nbut this would be one corner case of potentially many, I suspect.\n\n> +               } else if (!quote_it && needs_sq_quote(*q, brace)) {\n> +                       quote_it = true;\n> +               }\n> +       }\n> +       if (brace && !quote_it)\n> +               quote_it = true; /* Quote unpaired '{' */\n> +       if (quote_it)\n> +               strbuf_addch(dst, '\\'');\n> +       strbuf_add(dst, p, q - p);\n> +       if (quote_it)\n> +               strbuf_addch(dst, '\\'');\n>  }\n>\n>  void sq_quotef(struct strbuf *dst, const char *fmt, ...)\n> --\n> 2.56.1.576.g0cfbda1d5c7.dirty\n>\n\n\n-- \nD. Ben Knoble\n\n"},{"id":"559720","messageId":"27a384cb-7279-4061-9925-c55107575f6d@gmail.com","threadId":"66945","inReplyTo":"CALnO6CDyTh+O_S6Q6Nq46wckLGXdvTKJmU6uK_SKn5S=p4X20g@mail.gmail.com","subject":"Re: [RFC PATCH] quote: make sq_quote_buf_pretty() prettier","fromName":"Phillip Wood","fromEmail":"phillip.wood123@gmail.com","sentAt":"2026-11-30T16:38:24Z","receivedAt":"2026-11-30T16:38:24Z","isPatch":true,"sender":{"key":"phillip.wood@dunelm.org.uk","avatar":null},"body":"Hi Ben\n\nOn 30/11/2026 15:19, D. Ben Knoble wrote:\n> Hi Phillip, thanks for working on this!\n\nThanks\n\n> On Fri, Nov 27, 2026 at 11:54 AM Phillip Wood <phillip.wood123@gmail.com> wrote:\n> >> +       for (q = p; *q; q++) {\n>> +               if (need_bs_quote(*q)) {\n>> +                       if (brace)\n>> +                               quote_it = true;\n>> +                       if (quote_it)\n>> +                               strbuf_addch(dst, '\\'');\n>> +                       strbuf_add(dst, p, q - p);\n>> +                       p = q + 1;\n>> +                       if (quote_it)\n>> +                               strbuf_addch(dst, '\\'');\n>> +                       strbuf_addch(dst, '\\\\');\n>> +                       strbuf_addch(dst, *q);\n>> +                       if (!brace)\n>> +                               quote_it = false;\n>> +               } else if (*q == '{') {\n>> +                       brace++;\n>> +               } else if (*q == '}' && brace) {\n>> +                       brace--;\n> > I think this simple counter may count wrongly for scenarios like\n> \"}{a,b}\", but I haven't tried it myself yet. My shells (Zsh and Bash)\n> both do the following:\n> >      $ echo }{a,b}\n>      }a }b\n> > and I *think* that brace==0 when we see the comma here, even though it\n> would need quoted to avoid expansion.\n\nWe only decrement \"brace\" when we see '}' if \"brace > 0\". If we see '}' when \"brace == 0\" then we quote everything up to the next backslash escape or the end of the string. So we decide to quote this before we see the comma.\n\nThanks\n\nPhillip\n\n> > I think we are unlikely to emit such strange punctuation ourselves,\n> but this would be one corner case of potentially many, I suspect.\n\n\n> >> +               } else if (!quote_it && needs_sq_quote(*q, brace)) {\n>> +                       quote_it = true;\n>> +               }\n>> +       }\n>> +       if (brace && !quote_it)\n>> +               quote_it = true; /* Quote unpaired '{' */\n>> +       if (quote_it)\n>> +               strbuf_addch(dst, '\\'');\n>> +       strbuf_add(dst, p, q - p);\n>> +       if (quote_it)\n>> +               strbuf_addch(dst, '\\'');\n>>   }\n>>\n>>   void sq_quotef(struct strbuf *dst, const char *fmt, ...)\n>> --\n>> 2.56.1.576.g0cfbda1d5c7.dirty\n>>\n> > "},{"id":"559721","messageId":"01526f4d-b40e-4288-aa22-441c3e6e68e2@gmail.com","threadId":"66945","inReplyTo":"xmqq7bhy819l.fsf@gitster.g","subject":"Re: [RFC PATCH] quote: make sq_quote_buf_pretty() prettier","fromName":"Phillip Wood","fromEmail":"phillip.wood123@gmail.com","sentAt":"2026-11-30T16:39:54Z","receivedAt":"2026-11-30T16:39:54Z","isPatch":true,"sender":{"key":"phillip.wood@dunelm.org.uk","avatar":null},"body":"On 27/11/2026 20:31, Junio C Hamano wrote:\n> Phillip Wood <phillip.wood123@gmail.com> writes:\n> >> +static bool needs_sq_quote(char c, unsigned brace)\n>> +{\n>> +\tif (isalnum(c) || (unsigned char)c > 0x7f)\n>> +\t\treturn false;\n>> +\n>> +\tswitch (c) {\n>> +\tcase ',':\n>> +\t\treturn brace > 0; /* Prevent brace expansion */\n> > Web search tells me that bash, zsh, and ksh93 expands sequence\n> expressions such as \"{1..5}\" or \"{a..z}\" without any comma in the pair\n> of braces.  Because '.' returns false unconditionally here and no other\n> check triggers quoting for \"..\", sq_quote_buf_pretty() leaves \"{1..5}\"\n> unquoted, which expands to five arguments if pasted into those shells.\n\nOh, thanks for pointing that out. I think it should be fairly easy to detect '..' when brace != 0\n\n>> +\t/* '~' is only special if it is the first character */\n>> +\tif (*p == '~') {\n>> +\t\tquote_it = true;\n>> +\t\tp++;\n>>   \t}\n> > As noticed by Nahid already, this loses the leading '~'.  Also, do we\n> want to do something to this corner case?\n> >      $ echo P=A:~/B:C\n>      P=A:/home/gitster/B:C\n> > i.e., bash and friends seem to expand ~ that is not at the beginning of\n> value when the whole thing looks like a variable assignment.  Without\n> \"P=\" at front, this does not seem to expand.\nThanks, 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'.\n\nzsh 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.\n\nBy default, zsh also expands \"=cmd\" to the absolute path of \"cmd\" in the same places as '~' would be subject to tilde expansion.\n\nOn 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.\n\nI'll try and re-roll later this week\n\nThanks\n\nPhillip\n\n"},{"id":"559722","messageId":"311ab1f8-4d1a-43d7-843b-c19e28e51332@gmail.com","threadId":"66945","inReplyTo":"xmqqmrqt3ior.fsf@gitster.g","subject":"Re: [RFC PATCH] quote: make sq_quote_buf_pretty() prettier","fromName":"Phillip Wood","fromEmail":"phillip.wood123@gmail.com","sentAt":"2026-11-30T16:40:00Z","receivedAt":"2026-11-30T16:40:00Z","isPatch":true,"sender":{"key":"phillip.wood@dunelm.org.uk","avatar":null},"body":"On 28/11/2026 00:26, Junio C Hamano wrote:\n> Phillip Wood <phillip.wood123@gmail.com> writes:\n> > I forgot to mention a couple of things.\n> >> ... It also adds unnecessary pairs\n>> of empty quotes when a string starts or ends with a single quote.\n> > \"a single quote or an exclamation point.\"  We can see the example in the\n> table you have below, for the entry for 'danger!'.\n\nI'll add that\n\n>> +\tfor (q = p; *q; q++) {\n>> +\t\tif (need_bs_quote(*q)) {\n>> +\t\t\tif (brace)\n>> +\t\t\t\tquote_it = true;\n>> +\t\t\tif (quote_it)\n>> +\t\t\t\tstrbuf_addch(dst, '\\'');\n>> +\t\t\tstrbuf_add(dst, p, q - p);\n>> +\t\t\tp = q + 1;\n>> +\t\t\tif (quote_it)\n>> +\t\t\t\tstrbuf_addch(dst, '\\'');\n>> +\t\t\tstrbuf_addch(dst, '\\\\');\n>> +\t\t\tstrbuf_addch(dst, *q);\n>> +\t\t\tif (!brace)\n>> +\t\t\t\tquote_it = false;\n> > When brace > 0, quote_it stays true after emitting the backslash-escaped\n> character.\n> > If the next character also needs backslash quoting (such as \"{a'!b}\"),\n> or if the string ends right there with an unclosed brace (such as \"{!\",\n> p == q (i.e., an empty segment) but we emit it in a pair of single\n> quotes.\n\nGood point\n\n> After the loop, here also is an instance of single quote pair around\n> potentially an empty segment.  Perhaps a short helper or macro that does\n> an equivalent of\n> > \tif (p < q) {\n> \t\tif (quote_it)\n> \t\t\tstrbuf_addch(dst, '\\'');\n> \t\tstrbuf_add(dst, p, q - p);\n> \t\tif (quote_it)\n> \t\t\tstrbuf_addch(dst, '\\'');\n> \t}\t\t\n> > and use it twice (the bs-quote code path needs to be slightly rewritten\n> if we wanted to share this piece of code that way)?\n\nThat's a good idea\n\nThanks\n\nPhillip\n\n> >>   }\n>>   >>   void sq_quotef(struct strbuf *dst, const char *fmt, ...)\n\n\n"},{"id":"559724","messageId":"CALnO6CCKMRx8=LS34kadG75Xeh0q6DVO8sEGNO1Mv1ty1HMyXg@mail.gmail.com","threadId":"66945","inReplyTo":"27a384cb-7279-4061-9925-c55107575f6d@gmail.com","subject":"Re: [RFC PATCH] quote: make sq_quote_buf_pretty() prettier","fromName":"D. Ben Knoble","fromEmail":"ben.knoble@gmail.com","sentAt":"2026-11-30T17:02:05Z","receivedAt":"2026-11-30T17:02:05Z","isPatch":true,"sender":{"key":"ben.knoble@gmail.com","avatar":"https://avatars.githubusercontent.com/u/22802209?v=4"},"body":"On Mon, Nov 30, 2026 at 11:39 AM Phillip Wood <phillip.wood123@gmail.com> wrote:\n> On 30/11/2026 15:19, D. Ben Knoble wrote:\n> > On Fri, Nov 27, 2026 at 11:54 AM Phillip Wood <phillip.wood123@gmail.com> wrote:\n\n> >> +               } else if (*q == '}' && brace) {\n> >> +                       brace--;\n> >\n> > I think this simple counter may count wrongly for scenarios like\n> > \"}{a,b}\", but I haven't tried it myself yet. My shells (Zsh and Bash)\n> > both do the following:\n> >\n> >      $ echo }{a,b}\n> >      }a }b\n> >\n> > and I *think* that brace==0 when we see the comma here, even though it\n> > would need quoted to avoid expansion.\n>\n> We only decrement \"brace\" when we see '}' if \"brace > 0\". If we see '}'\n> when \"brace == 0\" then we quote everything up to the next backslash\n> escape or the end of the string. So we decide to quote this before we\n> see the comma.\n\nDoh, I missed the \"&& brace\"---thanks!\n\nSome other time, I (or anyone interested) should throw a fuzzer at\nthis and see what happens ;)\n\n"}]}