Browse Source

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>
maint
Jeff King 7 years ago committed by Junio C Hamano
parent
commit
1fbdab21bb
  1. 26
      quote.c
  2. 8
      quote.h
  3. 4
      trace.c

26
quote.c

@ -43,6 +43,22 @@ void sq_quote_buf(struct strbuf *dst, const char *src) @@ -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) @@ -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;

8
quote.h

@ -33,6 +33,14 @@ extern void sq_quote_buf(struct strbuf *, const char *src); @@ -33,6 +33,14 @@ extern void sq_quote_buf(struct strbuf *, const char *src);
extern void sq_quote_argv(struct strbuf *, const char **argv);
extern void sq_quotef(struct strbuf *, const char *fmt, ...);

/*
* These match their non-pretty variants, except that they avoid
* quoting when there are no exotic characters. These should only be used for
* human-readable output, as sq_dequote() is not smart enough to dequote it.
*/
void sq_quote_buf_pretty(struct strbuf *, const char *src);
void sq_quote_argv_pretty(struct strbuf *, const char **argv);

/* This unwraps what sq_quote() produces in place, but returns
* NULL if the input does not look like what sq_quote would have
* produced.

4
trace.c

@ -157,7 +157,7 @@ static void trace_argv_vprintf_fl(const char *file, int line, @@ -157,7 +157,7 @@ static void trace_argv_vprintf_fl(const char *file, int line,

strbuf_vaddf(&buf, format, ap);

sq_quote_argv(&buf, argv);
sq_quote_argv_pretty(&buf, argv);
print_trace_line(&trace_default_key, &buf);
}

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

strbuf_reset(&command_line);
sq_quote_argv(&command_line, argv);
sq_quote_argv_pretty(&command_line, argv);
command_start_time = getnanotime();
}

Loading…
Cancel
Save