summaryrefslogtreecommitdiff
path: root/quote.c
diff options
context:
space:
mode:
authorJeff King <peff@peff.net>2018-01-15 10:59:44 (GMT)
committerJunio C Hamano <gitster@pobox.com>2018-01-16 20:16:54 (GMT)
commit1fbdab21bb452ca4732bf088539247047465b99d (patch)
tree1b9f8229b5588ed959ebf0c7738856ebe3f3238f /quote.c
parente35f11c29391e557964a39204fae6b89afab6a2a (diff)
downloadgit-1fbdab21bb452ca4732bf088539247047465b99d.zip
git-1fbdab21bb452ca4732bf088539247047465b99d.tar.gz
git-1fbdab21bb452ca4732bf088539247047465b99d.tar.bz2
trace: avoid unnecessary quoting
Trace output which contains arbitrary strings (e.g., the arguments to commands which we are running) is always passed through sq_quote_buf(). That function always adds single-quotes, even if the output consists of vanilla characters. This can make the output a bit hard to read. Let's avoid the quoting if there are no characters which a shell would interpret. 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) Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'quote.c')
-rw-r--r--quote.c26
1 files changed, 26 insertions, 0 deletions
diff --git a/quote.c b/quote.c
index b2970da..37d2686 100644
--- a/quote.c
+++ b/quote.c
@@ -43,6 +43,22 @@ void sq_quote_buf(struct strbuf *dst, const char *src)
free(to_free);
}
+void sq_quote_buf_pretty(struct strbuf *dst, const char *src)
+{
+ static const char ok_punct[] = "+,-./:=@_^";
+ const char *p;
+
+ for (p = src; *p; p++) {
+ if (!isalpha(*p) && !isdigit(*p) && !strchr(ok_punct, *p)) {
+ sq_quote_buf(dst, src);
+ return;
+ }
+ }
+
+ /* if we get here, we did not need quoting */
+ strbuf_addstr(dst, src);
+}
+
void sq_quotef(struct strbuf *dst, const char *fmt, ...)
{
struct strbuf src = STRBUF_INIT;
@@ -68,6 +84,16 @@ void sq_quote_argv(struct strbuf *dst, const char **argv)
}
}
+void sq_quote_argv_pretty(struct strbuf *dst, const char **argv)
+{
+ int i;
+
+ for (i = 0; argv[i]; i++) {
+ strbuf_addch(dst, ' ');
+ sq_quote_buf_pretty(dst, argv[i]);
+ }
+}
+
static char *sq_dequote_step(char *arg, char **next)
{
char *dst = arg;