Skip to content

Commit 1fbdab2

Browse files
peffgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Nguyễn Thái Ngọc Duy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent e35f11c commit 1fbdab2

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed

quote.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,22 @@ void sq_quote_buf(struct strbuf *dst, const char *src)
4343
free(to_free);
4444
}
4545

46+
void sq_quote_buf_pretty(struct strbuf *dst, const char *src)
47+
{
48+
static const char ok_punct[] = "+,-./:=@_^";
49+
const char *p;
50+
51+
for (p = src; *p; p++) {
52+
if (!isalpha(*p) && !isdigit(*p) && !strchr(ok_punct, *p)) {
53+
sq_quote_buf(dst, src);
54+
return;
55+
}
56+
}
57+
58+
/* if we get here, we did not need quoting */
59+
strbuf_addstr(dst, src);
60+
}
61+
4662
void sq_quotef(struct strbuf *dst, const char *fmt, ...)
4763
{
4864
struct strbuf src = STRBUF_INIT;
@@ -68,6 +84,16 @@ void sq_quote_argv(struct strbuf *dst, const char **argv)
6884
}
6985
}
7086

87+
void sq_quote_argv_pretty(struct strbuf *dst, const char **argv)
88+
{
89+
int i;
90+
91+
for (i = 0; argv[i]; i++) {
92+
strbuf_addch(dst, ' ');
93+
sq_quote_buf_pretty(dst, argv[i]);
94+
}
95+
}
96+
7197
static char *sq_dequote_step(char *arg, char **next)
7298
{
7399
char *dst = arg;

quote.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@ extern void sq_quote_buf(struct strbuf *, const char *src);
3333
extern void sq_quote_argv(struct strbuf *, const char **argv);
3434
extern void sq_quotef(struct strbuf *, const char *fmt, ...);
3535

36+
/*
37+
* These match their non-pretty variants, except that they avoid
38+
* quoting when there are no exotic characters. These should only be used for
39+
* human-readable output, as sq_dequote() is not smart enough to dequote it.
40+
*/
41+
void sq_quote_buf_pretty(struct strbuf *, const char *src);
42+
void sq_quote_argv_pretty(struct strbuf *, const char **argv);
43+
3644
/* This unwraps what sq_quote() produces in place, but returns
3745
* NULL if the input does not look like what sq_quote would have
3846
* produced.

trace.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ static void trace_argv_vprintf_fl(const char *file, int line,
157157

158158
strbuf_vaddf(&buf, format, ap);
159159

160-
sq_quote_argv(&buf, argv);
160+
sq_quote_argv_pretty(&buf, argv);
161161
print_trace_line(&trace_default_key, &buf);
162162
}
163163

@@ -426,6 +426,6 @@ void trace_command_performance(const char **argv)
426426
atexit(print_command_performance_atexit);
427427

428428
strbuf_reset(&command_line);
429-
sq_quote_argv(&command_line, argv);
429+
sq_quote_argv_pretty(&command_line, argv);
430430
command_start_time = getnanotime();
431431
}

0 commit comments

Comments
 (0)