Git development
 help / color / mirror / Atom feed
* [RFC PATCH] quote: make sq_quote_buf_pretty() prettier
@ 2026-11-27 16:54 Phillip Wood
  2026-11-27 18:25 ` Nahid hasan Sagor
                   ` (3 more replies)
  0 siblings, 4 replies; 10+ messages in thread
From: Phillip Wood @ 2026-11-27 16:54 UTC (permalink / raw)
  To: git
  Cc: Ben Knoble, Harald Nordgren, Junio C Hamano, MD Nahid Hasan Sagor,
	Phillip Wood

From: Phillip Wood <phillip.wood@dunelm.org.uk>

The output of sq_quote_buf_pretty() is intended for human consumption
and as such it tries to avoid unnecessary quoting. However, it
is over enthusiastic about adding quotes to multibyte strings and
strings containing '~', '{' or '}'. It also adds unnecessary pairs
of empty quotes when a string starts or ends with a single quote. Fix
this by using a dedicated quoting routine that tries to avoid adding
unnecessary quotes, rather than reusing sq_quote_buf(). Tilde is only
quoted if it is the first character. Braces are only quoted if there is
a mismatch between the number of opening and closing braces, or they
contain a comma or a character that needs quoting. Sections between
backslash escapes are only quoted if they contain characters that need
to be quoted. The table below shows the change in output for various
inputs. The aim is to make the output more readable without changing
the meaning of the quoted output were it to be evaluated by the shell.

We could further improve the readability by dropping support for csh
which requires that '!' is backslash escaped. It is not clear to me how
widely used csh is these days, none of the big three BSD distributions
seem to have used it as the default shell for a number of years.

Text		Current output		New output
danger!		'danger'\!''		danger\!
'look out!'	''\''look out'\!''\'''	\''look out'\!\'
Dvořák		'Dvořák'		Dvořák
HEAD~3		'HEAD~3'		HEAD~3
HEAD@{1}	'HEAD@{1}'		HEAD@{1}
HEAD^{/,}	'HEAD^{/,}'		'HEAD^{/,}'

Signed-off-by: Phillip Wood <phillip.wood@dunelm.org.uk>
---
Following on from the discussion at [1] this tries to improve the
readability of the output without changing which shells we support
which is a bigger change that requires more thought and discussion. If
we do decide to go the route of per-shell quoting the changes here
will be useful for supporting POSIX shells.

There aren't any tests yet, though I have tested it manually
by looking at the trace output from commands like

   GIT_TRACE=1 git -c alias.z='log -1 HEAD~1' z

I'll add some tests if there is interest in this patch. On that subject,
does anyone know how to easily write table driven unit tests in clar
with a separate test for each input?

[1] https://lore.kernel.org/git/02A50A47-A048-417E-9D1E-0C12D8FB1476@gmail.com

base-commit: 77904e546de3bd940621092ecec47a88d0cede5d
Published-As: https://github.com/phillipwood/git/releases/tag/pw%2Fsq-quote-prettier%2Frfc-v1
View-Changes-At: https://github.com/phillipwood/git/compare/77904e546...ee7de5809
Fetch-It-Via: git fetch https://github.com/phillipwood/git pw/sq-quote-prettier/rfc-v1

 quote.c | 73 +++++++++++++++++++++++++++++++++++++++++++++++++--------
 1 file changed, 63 insertions(+), 10 deletions(-)

diff --git a/quote.c b/quote.c
index 235fac8e470..dfd379a102f 100644
--- a/quote.c
+++ b/quote.c
@@ -47,26 +47,79 @@ void sq_quote_buf(struct strbuf *dst, const char *src)
 	free(to_free);
 }
 
+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 */
+	case '}':
+		return !brace; /* Quote unpaired '}' */
+	case '~':
+	case '+':
+	case '-':
+	case '.':
+	case '/':
+	case ':':
+	case '=':
+	case '@':
+	case '_':
+	case '^':
+		return false;
+	default:
+		return true;
+	}
+}
+
 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++;
 	}
 
-	/* if we get here, we did not need quoting */
-	strbuf_addstr(dst, src);
+	for (q = p; *q; q++) {
+		if (need_bs_quote(*q)) {
+			if (brace)
+				quote_it = true;
+			if (quote_it)
+				strbuf_addch(dst, '\'');
+			strbuf_add(dst, p, q - p);
+			p = q + 1;
+			if (quote_it)
+				strbuf_addch(dst, '\'');
+			strbuf_addch(dst, '\\');
+			strbuf_addch(dst, *q);
+			if (!brace)
+				quote_it = false;
+		} else if (*q == '{') {
+			brace++;
+		} else if (*q == '}' && brace) {
+			brace--;
+		} else if (!quote_it && needs_sq_quote(*q, brace)) {
+			quote_it = true;
+		}
+	}
+	if (brace && !quote_it)
+		quote_it = true; /* Quote unpaired '{' */
+	if (quote_it)
+		strbuf_addch(dst, '\'');
+	strbuf_add(dst, p, q - p);
+	if (quote_it)
+		strbuf_addch(dst, '\'');
 }
 
 void sq_quotef(struct strbuf *dst, const char *fmt, ...)
-- 
2.56.1.576.g0cfbda1d5c7.dirty


^ permalink raw reply related	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2026-11-30 17:02 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-27 16:54 [RFC PATCH] quote: make sq_quote_buf_pretty() prettier Phillip Wood
2026-11-27 18:25 ` Nahid hasan Sagor
2026-11-30 14:51   ` Phillip Wood
2026-11-27 20:31 ` Junio C Hamano
2026-11-30 16:39   ` Phillip Wood
2026-11-28  0:26 ` Junio C Hamano
2026-11-30 16:40   ` Phillip Wood
2026-11-30 15:19 ` D. Ben Knoble
2026-11-30 16:38   ` Phillip Wood
2026-11-30 17:02     ` D. Ben Knoble

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox