Re: [PATCH] config: make the suggested "set" command safe to paste
On 26/11/2026 18:26, Junio C Hamano wrote:
Show 15 quoted lines
> Phillip Wood <phillip.wood123@gmail.com> writes:
> >> sq_quote_buf_pretty() is intended for that use case, so we already have
>> a central location for that logic.
> > FWIW, as the inventor of the sq_quote() family of helper functions, I
> did NOT know that we were allowed to futz with what the
> sq_quote_buf_pretty() class of helpers produce without having to worry
> about machine readability and output stability.
> > That is even though we prominently document them like this:
> > These should only be used for human-readable output, as sq_dequote()
> is not smart enough to dequote it.
> > Two things that come to mind are (1) we may need to find a way to make
> it even more prominent, and (2) "smart enough" will become irrelevant
> once we start aiming to cater to the user's shell, as predictability is
> what sq_dequote() is about, and compatibility with different "faviourite
> of the year" shells directly contradicts that goal.
I hadn't realized sq_quote_buf_pretty() was used in the tracing output. While we might get away with tweaking the quoting in the tracing output slightly, I don't think we want it to depend on the user's choice of shell. So if we decide we want to support more shells, we should probably introduce a new function for that.
Show 9 quoted lines
>> The output could be prettier though, in particular in would be nice to
>> avoid the redundant pairs of single quotes at the beginning and end of
>> ''\''hello there'\''' so that it produced \''hello there'\'
>> instead. "'hello there'" would be even prettier but would take more
>> work.
> > Yes, but I am not sure if going _specific_ to a single chosen shell,
> perhaps via user.shellsyntax = (sh|fish|csh|psh|ksh|...), is a good
> direction in general. There are folks who script in one and interact in
> another shell.
The paragraph quoted above was taking about improving the readability of the quoted output by removing redundant quotes, not tailoring the output to a specific shell.
I'm one of the people who uses different interactive and scripting shells. Personally I'd be content with git always quoting in the style of my interactive shell. If the git makes a suggestion based on a bad input that I passed to my script then I'd want to quoted for my interactive shell, not the shell that the script is written in. If I'm debugging a script, I can always tweak the config setting if I need to.
Show 11 quoted lines
> And no, going _generic_ to be usable by _any_ shells we care about is a
> direction I do not think we want to go. If backslash becomes active
> even inside a pair of single quotes in some but not all shells, then we
> must avoid inter-shell ambiguity by not using backslash while inside a
> single-quoted environment, by first closing the currently active single
> quote, giving a double-backslash (assuming that it is universally
> accepted as a single backslash character by everybody), and then going
> back to another single-quoted environment, e.g., "'abc'\\'xyz'", which
> is the familiar ugliness of our sq_quote() output. That kind of ugly
> output will be needed to be usable by everybody, but then even though it
> may be cut-and-paste-able, will it still be understandable?
I agree it is ugly and we're already doing it for '!' to accommodate csh. To support fish we would not need to unconditionally backslash escape backslashes -- our current quoting is almost always correct for fish -- we'd only need to do it when a backslash is followed by another backslash or a single quote. For the latter we're already creating ugly "'\''" output so I don't think that accommodating fish would change our current output most of the time. Having said that it is not possible to support shells whose quoting rules wildly differ from POSIX simply by tweaking the current output. If we want to support something like powershell we'll need a way of configuring the output.
Show 5 quoted lines
> My pessimistic but practical side is tempted to say that it is a lost
> cause to aim for cut-and-paste-ready output. Rather, we may be better
> off if we give output that shows clear boundaries between each
> "parameter" to "commands", and have the user express that in their
> chosen shell.
That is certainly simpler and if we abandon any pretense at shell compatibility we can avoid the ugly "'\''" by enclosing each argument in "'" and backslash escaping "'" and "\" within each quoted argument. I do think it is convenient to be able to cut-and-paste the output though. Thanks for the footnote below, that was interesting reading.
Phillip
Show 18 quoted lines
> [Footnote]
> > * 6fb737be5e ([PATCH] Make sq_expand() available as sq_quote().,
> 2005-07-07) introduced the sq- (stands for single-quote) quote
> mechanism as an externally available API function. 1fbdab21bb
> (trace: avoid unnecessary quoting, 2018-01-15) added _pretty() as a
> "human readable" variant, the first target being the output from
> tracing.
> > The principles laid out there for "pretty" are worth noting:
> > Trace output doesn't necessarily need to be shell-compatible,
> but:
> > - the shell language is a good ballpark for what humans
> consider readable (well, humans versed in command line
> tools)
> > - the run_command bits can be cut-and-pasted to a shell,
> and we'll keep that property
> > - it covers any cases which would make the output
> visually ambiguous (e.g., embedded whitespace or quotes)
> > My reference to "clear boundaries" is about the third point.