Skip to contentSkip to conversation

Remove sq_quote_print() in favor of *_buf

PATCH In review Ramkumar Ramachandra 2 participants 7 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

cover Remove sq_quote_print() in favor of *_buf

Hi,

While going through the for-each-ref-pretty series that Duy and I were developing, I noticed that this cleanup was independent and good as-it-is.

So here it is.

Nguyễn Thái Ngọc Duy (1):
for-each-ref, quote: convert *_quote_print -> *_quote_buf

Ramkumar Ramachandra (2):
tar-tree: remove dependency on sq_quote_print()
quote: remove sq_quote_print()

 builtin/for-each-ref.c | 13 +++++++----
 builtin/tar-tree.c     | 11 +++++----
 quote.c                | 61 ++++++++++++++++++--------------------------------
 quote.h                |  8 +++----
 4 files changed, 39 insertions(+), 54 deletions(-)
Signature · 1 line
1.8.4.rc0.4.g4634265
#1 lore raw

PATCH 1/3 for-each-ref, quote: convert *_quote_print -> *_quote_buf

From: Nguyễn Thái Ngọc Duy <[email protected]>

for-each-ref.c:print_value() currently prints values to stdout immediately using {sq|perl|python|tcl}_quote_print, giving us no opportunity to do any further processing. In preparation for getting print_value() to accept an additional strbuf argument to write to, convert the *_quote_print functions and callers to *_quote_buf.

[rr: commit message, minor modifications]

---
 builtin/for-each-ref.c | 13 +++++++++----
 quote.c                | 44 ++++++++++++++++++++++----------------------
 quote.h                |  6 +++---
 3 files changed, 34 insertions(+), 29 deletions(-)
builtin/for-each-ref.c +9 -4
index 7f059c3..1d4083c 100644
@@ -867,24 +867,29 @@ static void sort_refs(struct ref_sort *sort, struct refinfo **refs, int num_refs
 static void print_value(struct refinfo *ref, int atom, int quote_style)
 {
 	struct atom_value *v;
+	struct strbuf sb = STRBUF_INIT;
 	get_value(ref, atom, &v);
 	switch (quote_style) {
 	case QUOTE_NONE:
 		fputs(v->s, stdout);
 		break;
 	case QUOTE_SHELL:
-		sq_quote_print(stdout, v->s);
+		sq_quote_buf(&sb, v->s);
 		break;
 	case QUOTE_PERL:
-		perl_quote_print(stdout, v->s);
+		perl_quote_buf(&sb, v->s);
 		break;
 	case QUOTE_PYTHON:
-		python_quote_print(stdout, v->s);
+		python_quote_buf(&sb, v->s);
 		break;
 	case QUOTE_TCL:
-		tcl_quote_print(stdout, v->s);
+		tcl_quote_buf(&sb, v->s);
 		break;
 	}
+	if (quote_style != QUOTE_NONE) {
+		fputs(sb.buf, stdout);
+		strbuf_release(&sb);
+	}
 }
 
 static int hex1(char ch)
quote.c +22 -22

94 lines not on this page · raw message

quote.h +3 -3
index ed110a5..6996ebd 100644
@@ -68,8 +68,8 @@ extern char *quote_path_relative(const char *in, const char *prefix,
 			  struct strbuf *out);
 
 /* quoting as a string literal for other languages */
-extern void perl_quote_print(FILE *stream, const char *src);
-extern void python_quote_print(FILE *stream, const char *src);
-extern void tcl_quote_print(FILE *stream, const char *src);
+extern void perl_quote_buf(struct strbuf *sb, const char *src);
+extern void python_quote_buf(struct strbuf *sb, const char *src);
+extern void tcl_quote_buf(struct strbuf *sb, const char *src);
 
 #endif
Signature · 1 line
1.8.4.rc0.4.g4634265
#2 lore raw

PATCH 2/3 tar-tree: remove dependency on sq_quote_print()

In reply to Ramkumar Ramachandra

Currently, there is exactly one caller of sq_quote_print(), namely cmd_tar_tree(). In the interest of removing sq_quote_print() and simplification, replace it with an equivalent call to sq_quote_argv(). No functional changes intended.

---
 builtin/tar-tree.c | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)
builtin/tar-tree.c +5 -6
index 3f1e701..ba3ffe6 100644
@@ -26,8 +26,8 @@ int cmd_tar_tree(int argc, const char **argv, const char *prefix)
 	 * $0 tree-ish basedir ==>
 	 * 	git archive --format-tar --prefix=basedir tree-ish
 	 */
-	int i;
 	const char **nargv = xcalloc(sizeof(*nargv), argc + 3);
+	struct strbuf sb = STRBUF_INIT;
 	char *basedir_arg;
 	int nargc = 0;
 
@@ -65,11 +65,10 @@ int cmd_tar_tree(int argc, const char **argv, const char *prefix)
 	fprintf(stderr,
 		"*** \"git tar-tree\" is now deprecated.\n"
 		"*** Running \"git archive\" instead.\n***");
-	for (i = 0; i < nargc; i++) {
-		fputc(' ', stderr);
-		sq_quote_print(stderr, nargv[i]);
-	}
-	fputc('\n', stderr);
+	sq_quote_argv(&sb, nargv, 0);
+	strbuf_addch(&sb, '\n');
+	fputs(sb.buf, stderr);
+	strbuf_release(&sb);
 	return cmd_archive(nargc, nargv, prefix);
 }
 
Signature · 1 line
1.8.4.rc0.4.g4634265
#3 lore raw

PATCH 3/3 quote: remove sq_quote_print()

In reply to Ramkumar Ramachandra

Remove sq_quote_print() since it has no callers. A nicer alternative sq_quote_buf() exists: its callers aren't forced to print immediately.

For historical context, sq_quote_print() was first introduced in 575ba9d6 (GIT_TRACE: show which built-in/external commands are executed, 2006-06-25) for the purpose of printing argv for $GIT_TRACE. Today, we achieve this using trace_argv_printf() -> sq_quote_argv() -> sq_quote_buf(), which ultimately fills in a strbuf.

---
 quote.c | 17 -----------------
 quote.h |  2 --
 2 files changed, 19 deletions(-)
quote.c +0 -17
index 9fd66c6..45e3db1 100644
@@ -42,23 +42,6 @@ void sq_quote_buf(struct strbuf *dst, const char *src)
 	free(to_free);
 }
 
-void sq_quote_print(FILE *stream, const char *src)
-{
-	char c;
-
-	fputc('\'', stream);
-	while ((c = *src++)) {
-		if (need_bs_quote(c)) {
-			fputs("'\\", stream);
-			fputc(c, stream);
-			fputc('\'', stream);
-		} else {
-			fputc(c, stream);
-		}
-	}
-	fputc('\'', stream);
-}
-
 void sq_quote_argv(struct strbuf *dst, const char** argv, size_t maxlen)
 {
 	int i;
quote.h +0 -2
index 6996ebd..71dcc3a 100644
@@ -27,8 +27,6 @@ struct strbuf;
  * excluding the final null regardless of the buffer size.
  */
 
-extern void sq_quote_print(FILE *stream, const char *src);
-
 extern void sq_quote_buf(struct strbuf *, const char *src);
 extern void sq_quote_argv(struct strbuf *, const char **argv, size_t maxlen);
 
Signature · 1 line
1.8.4.rc0.4.g4634265
#4 lore raw
In reply to Ramkumar Ramachandra
Quoted 5 lines · Ramkumar Ramachandra

While going through the for-each-ref-pretty series that Duy and I were developing, I noticed that this cleanup was independent and good as-it-is.

So here it is.

You always can first allocate a piece of memory and write into it instead of writing things out directly. The patch shows it _can_ be done, but that is not a news.

And such a change is hardly a "clean-up". It just wastes more memory you do not have to waste, in order to do what you are doing.

When there is a reason why you need an in-memory representation, this change starts to become the first step refactoring for an enhancement.

Quoted 2 lines

Nguyễn Thái Ngọc Duy (1):
for-each-ref, quote: convert *_quote_print -> *_quote_buf

The log message for this one explains it very well. This change by itself is not useful, but it will become necessary once you start needing to access an in-memory result.

#5 lore raw
Quoted 12 lines · Junio C Hamano
Quoted 5 lines · Ramkumar Ramachandra

While going through the for-each-ref-pretty series that Duy and I were developing, I noticed that this cleanup was independent and good as-it-is.

So here it is.

You always can first allocate a piece of memory and write into it instead of writing things out directly. The patch shows it _can_ be done, but that is not a news.

Quoted 6 lines

And such a change is hardly a "clean-up". It just wastes more memory you do not have to waste, in order to do what you are doing.

When there is a reason why you need an in-memory representation, this change starts to become the first step refactoring for an enhancement.

Having said all that, the patch texts all look OK, so I'll queue them with updated log messages. It was the usual me reacting to unjustified value judgement made in log messages and cover letters.

It would save me a lot of work if people stopped doing that and instead stuck to facts. For example, between print_to_buf() and print_to_stdout(), the former is *not* necessarily "nicer". It is more flexible but that flexibility comes with a price, and the caller needs a demonstrative need for that flexibility to justify the cost.

Thanks.

#6 lore raw
Quoted 3 lines · Junio C Hamano

Having said all that, the patch texts all look OK, so I'll queue them with updated log messages. It was the usual me reacting to unjustified value judgement made in log messages and cover letters.

Thanks. I'll look at how the log messages are different in the queued version, and adapt accordingly next time.