git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v5 2/7] trace: avoid unnecessary quoting

From
Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Date
Jan 15, 2018, 10:59 UTC
Message-ID
<20180115105949.18328-3-pclouds@gmail.com>
In-Reply-To
<20180115105949.18328-1-pclouds@gmail.com>
From: Jeff King <peff@peff.net>

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>
---
 quote.c | 26 ++++++++++++++++++++++++++
 quote.h |  8 ++++++++
 trace.c |  4 ++--
 3 files changed, 36 insertions(+), 2 deletions(-)
diff --git a/quote.c b/quote.c
index b2970da627..37d2686865 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;
diff --git a/quote.h b/quote.h
index 48a75a416b..ea992dcc91 100644
--- a/quote.h
+++ b/quote.h
@@ -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.
diff --git a/trace.c b/trace.c
index fa9174fc4b..9784915be1 100644
--- a/trace.c
+++ b/trace.c
@@ -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)
 		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();
 }
-- 
2.15.1.600.g899a5f85c6
Previous: Nguyễn Thái Ngọc DuyNext: Nguyễn Thái Ngọc Duy
Message 53 of 74 in “run-command.c: print env vars when GIT_TRACE is set”
  1. run-command.c: print env vars when GIT_TRACE is setNguyễn Thái Ngọc Duy, Jan 10, 2018
  2. Brandon WilliamsJan 10, 2018
  3. Stefan BellerJan 10, 2018
  4. Brandon WilliamsJan 10, 2018
  5. Stefan BellerJan 10, 2018
  6. run-command.c: print env vars when GIT_TRACE is setNguyễn Thái Ngọc Duy, Jan 11, 2018
  7. Duy NguyenJan 11, 2018
  8. Brandon WilliamsJan 11, 2018
  9. Stefan BellerJan 11, 2018
  10. Junio C HamanoJan 11, 2018
  11. Duy NguyenJan 12, 2018
  12. 0/4 run-command.c: print env vars when GIT_TRACE is setNguyễn Thái Ngọc Duy, Jan 12, 2018
  13. 1/4 trace.c: introduce trace_run_command()Nguyễn Thái Ngọc Duy, Jan 12, 2018
  14. Jeff KingJan 12, 2018
  15. Jeff KingJan 12, 2018
  16. 2/4 trace.c: print program 'git' when child_process.git_cmd is setNguyễn Thái Ngọc Duy, Jan 12, 2018
  17. Jeff KingJan 12, 2018
  18. 3/4 trace.c: print env vars in trace_run_command()Nguyễn Thái Ngọc Duy, Jan 12, 2018
  19. Jeff KingJan 12, 2018
  20. 4/4 trace.c: be smart about what env to print in trace_run_command()Nguyễn Thái Ngọc Duy, Jan 12, 2018
  21. Jeff KingJan 12, 2018
  22. Junio C HamanoJan 12, 2018
  23. Jeff KingJan 12, 2018
  24. Junio C HamanoJan 12, 2018
  25. Jeff KingJan 12, 2018
  26. Brandon WilliamsJan 12, 2018
  27. Junio C HamanoJan 12, 2018
  28. Duy NguyenJan 13, 2018
  29. Duy NguyenJan 13, 2018
  30. Jeff KingJan 12, 2018
  31. 5/4 sq_quote_argv: drop maxlen parameterJeff King, Jan 12, 2018
  32. Junio C HamanoJan 12, 2018
  33. 6/4 trace: avoid unnecessary quotingJeff King, Jan 12, 2018
  34. Stefan BellerJan 12, 2018
  35. Stefan BellerJan 12, 2018
  36. Duy NguyenJan 13, 2018
  37. 0/7 Trace env variables in run_command()Nguyễn Thái Ngọc Duy, Jan 13, 2018
  38. 1/7 sq_quote_argv: drop maxlen parameterNguyễn Thái Ngọc Duy, Jan 13, 2018
  39. 2/7 trace: avoid unnecessary quotingNguyễn Thái Ngọc Duy, Jan 13, 2018
  40. 3/7 trace.c: move strbuf_release() out of print_trace_line()Nguyễn Thái Ngọc Duy, Jan 13, 2018
  41. Jeff KingJan 13, 2018
  42. 4/7 trace.c: introduce trace_run_command()Nguyễn Thái Ngọc Duy, Jan 13, 2018
  43. 5/7 trace.c: print program 'git' when child_process.git_cmd is setNguyễn Thái Ngọc Duy, Jan 13, 2018
  44. 6/7 trace.c: print env vars in trace_run_command()Nguyễn Thái Ngọc Duy, Jan 13, 2018
  45. Jeff KingJan 13, 2018
  46. Junio C HamanoJan 16, 2018
  47. Stefan BellerJan 16, 2018
  48. Junio C HamanoJan 17, 2018
  49. 7/7 trace.c: print new cwd in trace_run_command()Nguyễn Thái Ngọc Duy, Jan 13, 2018
  50. Jeff KingJan 13, 2018
  51. 0/7 Trace env variables in run_command()Nguyễn Thái Ngọc Duy, Jan 15, 2018
  52. 1/7 sq_quote_argv: drop maxlen parameterNguyễn Thái Ngọc Duy, Jan 15, 2018
  53. 2/7 trace: avoid unnecessary quotingNguyễn Thái Ngọc Duy, Jan 15, 2018
  54. 3/7 trace.c: move strbuf_release() out of print_trace_line()Nguyễn Thái Ngọc Duy, Jan 15, 2018
  55. Brandon WilliamsJan 16, 2018
  56. 4/7 trace.c: introduce trace_run_command()Nguyễn Thái Ngọc Duy, Jan 15, 2018
  57. Jeff KingJan 17, 2018
  58. Junio C HamanoJan 17, 2018
  59. 5/7 trace.c: print program 'git' when child_process.git_cmd is setNguyễn Thái Ngọc Duy, Jan 15, 2018
  60. 6/7 trace.c: print env vars in trace_run_command()Nguyễn Thái Ngọc Duy, Jan 15, 2018
  61. Brandon WilliamsJan 16, 2018
  62. 7/7 trace.c: print new cwd in trace_run_command()Nguyễn Thái Ngọc Duy, Jan 15, 2018
  63. 0/7 Trace env variables in run_command()Nguyễn Thái Ngọc Duy, Jan 18, 2018
  64. 1/7 sq_quote_argv: drop maxlen parameterNguyễn Thái Ngọc Duy, Jan 18, 2018
  65. 2/7 trace: avoid unnecessary quotingNguyễn Thái Ngọc Duy, Jan 18, 2018
  66. 3/7 trace.c: move strbuf_release() out of print_trace_line()Nguyễn Thái Ngọc Duy, Jan 18, 2018
  67. 4/7 run-command.c: introduce trace_run_command()Nguyễn Thái Ngọc Duy, Jan 18, 2018
  68. 5/7 run-command.c: print program 'git' when tracing git_cmd modeNguyễn Thái Ngọc Duy, Jan 18, 2018
  69. 6/7 run-command.c: print env vars in trace_run_command()Nguyễn Thái Ngọc Duy, Jan 18, 2018
  70. 7/7 run-command.c: print new cwd in trace_run_command()Nguyễn Thái Ngọc Duy, Jan 18, 2018
  71. Jeff KingJan 19, 2018
  72. Jeff KingJan 11, 2018
  73. Duy NguyenJan 11, 2018
  74. Johannes SixtJan 11, 2018

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.