Article View: linux.kernel.git
Article #3978[RFC PATCH] quote: make sq_quote_buf_pretty() prettier
From: Phillip Wood
Date: Fri, 27 Nov 2026 16:54
Date: Fri, 27 Nov 2026 16:54
154 lines
15633 bytes
15633 bytes
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
Technical Details
Message-ID:
<ee7de58093c69fbbab686a68b5b781bc60d90a2b.1795798429.git.phillip.wood@dunelm.org.uk>Path:
mbox-mo1.pugleaf.net!srv.newsdeef.eu!news.mb-net.net!open-news-network.org!news.nntp4.net!nntp.xenet.de!news.neodome.net!usenet.network!.POSTED!vger.kernel.org!git+bounces-76525-linux-kernel-git=dewy.ruShow Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!srv.newsdeef.eu!news.mb-net.net!open-news-network.org!news.nntp4.net!nntp.xenet.de!news.neodome.net!usenet.network!.POSTED!vger.kernel.org!git+bounces-76525-linux-kernel-git=dewy.ru
From: phillip.wood123@gmail.com (Phillip Wood)
Newsgroups: linux.kernel.git
Subject: [RFC PATCH] quote: make sq_quote_buf_pretty() prettier
Date: Fri, 27 Nov 2026 16:54:27 +0000
Organization: Usenet Network
Message-ID: <ee7de58093c69fbbab686a68b5b781bc60d90a2b.1795798429.git.phillip.wood@dunelm.org.uk>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Info: usenet.network;
logging-data="776919"; mail-complaints-to="abuse@usenet.network"
Cancel-Lock: sha1:ygp80OnHoZd8P9SWQB7mZpAu2LM= sha256:9KMY83JesQysYbuXtwxdTRRz+iodOByMIc1EU6yNR7I=
sha1:6EmqF+6ydOpjG+MHK7gRDqgXlQI= sha256:/oWKJNUQmuz+sxxRzoWPcYmhB9C5a/vq2ElpC/5dWkg=
List-ID: <git.vger.kernel.org>
X-Mailpost-Empty-Hdrs: X-Spam-Level
X-Received: from localhost (localhost [127.0.0.1])
by mail.dewy.ru (Postfix) with ESMTP id A3193125F61; Fri, 27 Nov 2026 19:55:03 +0300 (MSK)
X-Virus-Scanned: Debian amavisd-new at usenet.network
X-Spam-Flag: NO
X-Spam-Score: -1.049
X-Spam-Status: No, score=-1.049 tagged_above=-9999 required=6.31
tests=[DCC_CHECK=1.1, DKIM_SIGNED=0.1, DKIM_VALID=-0.1,
DKIM_VALID_AU=-0.1, FORGED_GMAIL_RCVD=1,
FREEMAIL_FORGED_FROMDOMAIN=0.001, FREEMAIL_FROM=0.001,
HEADER_FROM_DIFFERENT_DOMAINS=0.249, MAILING_LIST_MULTI=-1,
RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001, SPF_PASS=-0.001]
autolearn=ham autolearn_force=no
X-Authentication-Results: mail.dewy.ru (amavisd-new); dkim=pass (2048-bit key)
header.d=gmail.com
X-Received: from mail.dewy.ru ([127.0.0.1])
by localhost (mail.dewy.ru [127.0.0.1]) (amavisd-new, port 10024)
with ESMTP id nAJ3x8a4Juh7;
Fri, 27 Nov 2026 19:54:59 +0300 (MSK)
X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.105.105.114; helo=tor.lore.kernel.org; envelope-from=git+bounces-76525-linux-kernel-git=dewy.ru@vger.kernel.org; receiver=<UNKNOWN>
X-Authentication-Results: OpenDMARC; dmarc=fail (p=none dis=none) header.from=gmail.com
X-Received: from tor.lore.kernel.org (tor.lore.kernel.org [172.105.105.114])
by mail.dewy.ru (Postfix) with ESMTPS id 6F41A125F5E; Fri, 27 Nov 2026 19:54:51 +0300 (MSK)
X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.77.114.92])
by tor.lore.kernel.org (Postfix) with ESMTP id 3BD8E24446; Fri, 27 Nov 2026 16:54:50 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1
X-ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass;
t=1795798487;
b=QllSGI5w78IA+JCQA6n8s4/kzrDH70rEl6mC6afyspATBP/xr4MVRWDV3sI2InKB3I+f
xjui/qKwQyY1oW0u0w1jGDQTbyVLKPl/1o/Zb2CYj+VTV4Z3+U+iBroxTFFClW5oZn6JO
NU8ad2xUHMVKQjUsSW33jTGHKRpqwGLWbjmhvrfc2/l6oBjB4caPbYULAvN5iqHG1YQKU
mdY/2sL3a2miu97a2b20P8Hf/1aEuODtEIHYewx8pAgY2h1zj0QKUcUy10khGKq36X3Am
ay50d55sOVngNAy3LlEJ7QL3/kxE6MZhPXc7Fwsv3IuBBFlRTR10UlEhVRgPQEVqExA==
X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015;
a=rsa-sha256; c=relaxed/relaxed; t=1795798487;
h=X-Original-To:Received:DMARC-Filter:Received:DKIM-Signature:
X-Google-DKIM-Signature:X-Gm-Message-State:X-Gm-Gg:X-Received:
Received:From:To:Cc:Subject:Date:Message-ID:X-Mailer:Reply-To:
X-Mailing-List:List-Id:List-Post:List-Help:List-Subscribe:
List-Unsubscribe:List-Owner:Precedence:MIME-Version:Content-Type:
Content-Transfer-Encoding;
bh=zUbMg1uBKciYtUd5AvNOS51PwQFRURpwVRO5YAVFj0I=;
b=0PYvbtGLH+j1XcrDTLhgXOBi8UoVdAO7XcTYITpyleaVVEuxF+kQ1JsfvHZipGzmVQts
Fj2WiRLMYQOkzI/YTGxDHp6nG/IjlS+CGEqUW9BWp1j/GcJFNAUGlqGKfegESPDgcdiUr
eBP+LSG6x1hPamsJAkT9PCOwv3lgc9+uMxPhPz4a/Dg25Zs9/PFcAdTVbP/3ifiAOlC0h
JwcNsFQvUq/H7Dt17kZ2+KarsUD3St4nsgtq5QX5aYJclnMziqLKGZYDO/V58EP0rfPe7
hvOSUpK+tVBb81jmeOziaIxkR1o9B4Kos9uddmQcltlALS2KdT+E5wzYKczX/IdVsSQ==
X-ARC-Authentication-Results: i=2; smtp.subspace.kernel.org;
dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=oztVsHll;
dmarc=pass header.from=gmail.com;
spf=pass smtp.mailfrom=gmail.com;
arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1
X-Received: from localhost.localdomain (localhost [127.0.0.1])
by smtp.subspace.kernel.org (Postfix) with ESMTP id CA21E606A327;
Fri, 27 Nov 2026 16:54:47 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org;
dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=oztVsHll
X-Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.221.47
X-ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none;
t=1795798487;
b=YdSx/pnNFeNdcAxcRtR208J79TpZNUD6b9aajpACQnzTpkodjEvvEI4ZVZIV/MviBYIH
+SxaLvAJspVmm1cIspa8wrWBejTnkOtlGqj3U5xaVUEPolbeR9C1VZlcUCuAiYFzkAJQ+
JCgpJM3LQvIt0io3KeaNZ+FjxXaAYuZdeIyElveiomFGOWNUKADxTU5qqrUFWlok/Sdct
arf92nZ6a+QFrYvQHP/eVzT4xOVgUqKqf64tkbbK4PIpU5Pti7/LsE5bcj4pibL5H83ev
sjNCOtMeJhJDHfRbKyVZw9/KIRfpsunN+dkxExy8vCGCqhv+TDr128OL+oNUpuWAH0Q==
X-ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015;
a=rsa-sha256; c=relaxed/relaxed; t=1795798487;
h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature:
X-Gm-Message-State:X-Gm-Gg:X-Received:Received:From:To:Cc:Subject:
Date:Message-ID:X-Mailer:Reply-To:MIME-Version:Content-Type:
Content-Transfer-Encoding;
bh=zUbMg1uBKciYtUd5AvNOS51PwQFRURpwVRO5YAVFj0I=;
b=2t8pnnpIOsMOa5vOHIb6r4O6qvToDQa5UqR5jGNddwbKjRwGuZAO5ckJF3YuseYQ5SHr
gVGECG33+TCx2Bvdo335tDzC0s7Y2zNVPZrNzC5CjEIAo6D0zXGgVHUcjY2o5oMzvXGlc
tEd6oVoRYLTL/VbonjEyzB0wzfrXhl9faF2aUZ//dqCcjw8TrHmMDQ69qqCHG80xkd3sM
9jaW954qETQZuugGgWEeni/2PtajkbkvuHagvGg19vGhE0zuDcc5vhSxq9riRzWin3JMA
W61ezoN0V6nDht+3Mx73fQe1rypbDTnF3uOzSXXMMmDzK8rOo4qp3tc+Umm3waKFlWw==
X-ARC-Authentication-Results: i=1; smtp.subspace.kernel.org;
dmarc=pass header.from=gmail.com;
spf=pass smtp.mailfrom=gmail.com;
dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=oztVsHll;
arc=none smtp.remote-ip=209.85.221.47
X-Received: from mail-wr1-f47.google.com (mail-wr1-f47.google.com [209.85.221.47])
(using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)
key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256)
(No client certificate requested)
by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8CA95606A31F
for <git@vger.kernel.org>; Fri, 27 Nov 2026 16:54:46 +0000 (UTC)
X-DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 8CA95606A31F
X-Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com
X-Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com
X-Received: by mail-wr1-f47.google.com with SMTP id ffacd0b85a97d-4a34ffd3e39so4578624f8f.0
for <git@vger.kernel.org>; Fri, 27 Nov 2026 08:54:46 -0800 (PST)
X-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=gmail.com; s=20260707; t=1795798485; x=1796403285; darn=vger.kernel.org;
h=content-transfer-encoding:content-type:mime-version:reply-to
:message-id:date:subject:cc:to:from:from:to:cc:subject:date
:message-id:reply-to:content-type;
bh=zUbMg1uBKciYtUd5AvNOS51PwQFRURpwVRO5YAVFj0I=;
b=oztVsHllYTxkOkwxMxYknrEz3nPPnRlDccG5I6+i6O8MCEnhyix0psIvJNDIpwe02q
VKqjjaZbqcws30sl1dK5D0c6sP/ZXOhSJSBR7RRdAptCHfhafB3xeqMOw/7qif8ZYTC1
VxZQ/EBcdHvPzTmijjGjjHRSOGcmfN4cWkmWJVCPZLK3GUsAmlJV8hPRTo1wHS3uUUop
6kMTx1H8MF3Nc2U4I3GO+e/Dg/6D+10NjyJsyGG1b/Ijnf0GyCNaT8nxltEV2Ny6ziZ5
2+079TUz9DumtDkjvEw0Sz3xX1ZXBHKBjQVTi8RXP7GoaKWbhvnP8pexZVeuubBMUsdB
4wSg==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=1e100.net; s=20260707; t=1795798485; x=1796403285;
h=content-transfer-encoding:content-type:mime-version:reply-to
:message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from
:to:cc:subject:date:message-id:reply-to:content-type;
bh=zUbMg1uBKciYtUd5AvNOS51PwQFRURpwVRO5YAVFj0I=;
b=S+IIKZPLYjC+dF/8hbEphJ4Fu+edZ6vKgPMbMMOOKyeCqMpH/zdBYzKlEWZNnEqzTg
XT6upLcQqdewT8IA/w9JYrEhSfgoSWf7Svfiflt/1MqTiW+xlbZBfQgoWIQYhI5jegzv
weC7uvTS+7KoIt1DVfbK+tYUWRQEV4kXuIZldMpVsUVRQrblN6bH3VSrmh15JVvs1Kt8
swg7Ua5Di3RhRxOrxSTW8twNCXMcU3SVq9JmAcVKI/mLWNqHt8F9AIfxrzsX+hZ2/Xty
U5l73/Qmkeg8crL7PPYZoLT07QPK5m4N/8U53b8dd4SNbCUPXW66a212+YKmPS6UIL3J
PKKw==
X-Gm-Message-State: APuU4Z/85OltrmLZEHKvZAigD2M2keoCGEHsCV66s0y6Ymg56NYoeRcm
yYHQYOjvzDZNdZPLY3ttqZf8FCqG5G2MUJcucAyzbXckKm1TZKMiXP6OYFtb/w==
X-Gm-Gg: AZFdTZdZDEmf0jNXNJG3t/nL9E14V2gDhSPSpV3ABXTGAGTrUT7M8OswyVhaRkBo3UU
/M64VlgBgsaFUCUjsJNrr71xiZSbnVQ+ESs+iK/5kxpsVaVWcAGGdMgoNoQ81D3iGncwTgATQxu
MnWPNofwuxQNjNGisGkeU7JKTAp2PxnHS99D+KRfT9eugEQYFVMUQ/XZ8S4tbdJIFeKASArvul2
9AqgUTV8/bMxdIkjQrh3fbIbCihl2VFQAxpl24/G4ywUiziOZfAC5abmtW8qdlTfyK65PRj7z+B
NNnpejSdZIA8RiorEcDR8xnhNkPxfFyoQl126Mki2pF2/UXj1cfR+ps0Uw0WmXrr+7anHCYIVHY
nPwPTUy4vvKWv04igod74GdJfDIXhlfCgBvC0/e5gKtKgOyAJjp9DZ57oen0ZvA19KkwCocsz5m
GbML6bvLed4wYOmABN/FP5dyGurBnZv53hGGP5qUZg7Ev3NgAsppcz13JJGs2rLPUylZN+y0KMb
sATzruvl2ChWQz75/ZJPzdGROPPIpgtR045qiAY6xWIf/xxU30LTpBhKgpFSAS2N4/3V1E6gpce
XDlfd3fCz/oFAScnTw/iTykGlyIe8yZhMYc4VF0Gta+dg0Hk4irItw==
X-Received: by 2002:adf:e010:0:10b0:4a7:518f:bea6 with SMTP id ffacd0b85a97d-4abb3a4d2b1mr35901195f8f.28.1795798484521;
Fri, 27 Nov 2026 08:54:44 -0800 (PST)
X-Received: from berwick ([2a0a:ef40:724:6601:f3ff:aebc:61f8:d91f])
by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-4ab6c4353d6sm46440606f8f.56.2026.11.27.08.54.43
(version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
Fri, 27 Nov 2026 08:54:44 -0800 (PST)
X-Cc: Ben Knoble <ben.knoble@gmail.com>,
Harald Nordgren <haraldnordgren@gmail.com>,
Junio C Hamano <gitster@pobox.com>,
MD Nahid Hasan Sagor <nahidhasan141400@gmail.com>,
Phillip Wood <phillip.wood123@gmail.com>
X-Mailer: git-send-email 2.56.1.576.g0cfbda1d5c7.dirty
X-Reply-To: Phillip Wood <phillip.wood@dunelm.org.uk>
X-Mailing-List: git@vger.kernel.org
X-List-Post: <mailto:git@vger.kernel.org>
X-List-Help: <mailto:git+help@vger.kernel.org>
X-List-Subscribe: <mailto:git+subscribe@vger.kernel.org>
X-List-Unsubscribe: <mailto:git+unsubscribe@vger.kernel.org>
X-List-Owner: <mailto:git+owner@vger.kernel.org>
X-Precedence: list