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

[PATCH v2 3/5] [GSOC] ref-filter: --format=%(raw) re-support --perl

From
ZheNing Hu via GitGitGadget <gitgitgadget@gmail.com>
Date
Jul 23, 2021, 09:04 UTC
Message-ID
<00a29dd16d7c5e176e560bec1dc335b1eb6ccaf8.1627031043.git.gitgitgadget@gmail.com>
In-Reply-To
<pull.1000.v2.git.1627031043.gitgitgadget@gmail.com>
From: ZheNing Hu <adlternative@gmail.com>

Because the perl language can handle binary data correctly, add the function perl_quote_buf_with_len(), which can specify the length of the data and prevent the data from being truncated at '\0' to help `--format="%(raw)"` re-support `--perl`.

Helped-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
Signed-off-by: ZheNing Hu <adlternative@gmail.com>
---
 Documentation/git-for-each-ref.txt |  4 ++--
 quote.c                            | 17 +++++++++++++++++
 quote.h                            |  1 +
 ref-filter.c                       | 15 +++++++++++----
 t/t6300-for-each-ref.sh            | 19 +++++++++++++++++--
 5 files changed, 48 insertions(+), 8 deletions(-)
diff --git a/Documentation/git-for-each-ref.txt b/Documentation/git-for-each-ref.txt
index cbb6f87d13f..6da899c6296 100644
--- a/Documentation/git-for-each-ref.txt
+++ b/Documentation/git-for-each-ref.txt
@@ -241,8 +241,8 @@ raw:size::
 	The raw data size of the object.
 
 Note that `--format=%(raw)` can not be used with `--python`, `--shell`, `--tcl`,
-`--perl` because such language may not support arbitrary binary data in their
-string variable type.
+because such language may not support arbitrary binary data in their string
+variable type.
 
 The message in a commit or a tag object is `contents`, from which
 `contents:<part>` can be used to extract various parts out of:
diff --git a/quote.c b/quote.c
index 8a3a5e39eb1..26719d21d1e 100644
--- a/quote.c
+++ b/quote.c
@@ -471,6 +471,23 @@ void perl_quote_buf(struct strbuf *sb, const char *src)
 	strbuf_addch(sb, sq);
 }
 
+void perl_quote_buf_with_len(struct strbuf *sb, const char *src, size_t len)
+{
+	const char sq = '\'';
+	const char bq = '\\';
+	const char *c = src;
+	const char *end = src + len;
+
+	strbuf_addch(sb, sq);
+	while (c != end) {
+		if (*c == sq || *c == bq)
+			strbuf_addch(sb, bq);
+		strbuf_addch(sb, *c);
+		c++;
+	}
+	strbuf_addch(sb, sq);
+}
+
 void python_quote_buf(struct strbuf *sb, const char *src)
 {
 	const char sq = '\'';
diff --git a/quote.h b/quote.h
index 768cc6338e2..0fe69e264b0 100644
--- a/quote.h
+++ b/quote.h
@@ -94,6 +94,7 @@ char *quote_path(const char *in, const char *prefix, struct strbuf *out, unsigne
 
 /* quoting as a string literal for other languages */
 void perl_quote_buf(struct strbuf *sb, const char *src);
+void perl_quote_buf_with_len(struct strbuf *sb, const char *src, size_t len);
 void python_quote_buf(struct strbuf *sb, const char *src);
 void tcl_quote_buf(struct strbuf *sb, const char *src);
 void basic_regex_quote_buf(struct strbuf *sb, const char *src);
diff --git a/ref-filter.c b/ref-filter.c
index a713507d783..0ff8574a00c 100644
--- a/ref-filter.c
+++ b/ref-filter.c
@@ -744,7 +744,10 @@ static void quote_formatting(struct strbuf *s, const char *str, size_t len, int
 		sq_quote_buf(s, str);
 		break;
 	case QUOTE_PERL:
-		perl_quote_buf(s, str);
+		if (len != -1)
+			perl_quote_buf_with_len(s, str, len);
+		else
+			perl_quote_buf(s, str);
 		break;
 	case QUOTE_PYTHON:
 		python_quote_buf(s, str);
@@ -1008,10 +1011,14 @@ int verify_ref_format(struct ref_format *format)
 		at = parse_ref_filter_atom(format, sp + 2, ep, &err);
 		if (at < 0)
 			die("%s", err.buf);
-		if (format->quote_style && used_atom[at].atom_type == ATOM_RAW &&
-		    used_atom[at].u.raw_data.option == RAW_BARE)
+
+		if ((format->quote_style == QUOTE_PYTHON ||
+		     format->quote_style == QUOTE_SHELL ||
+		     format->quote_style == QUOTE_TCL) &&
+		     used_atom[at].atom_type == ATOM_RAW &&
+		     used_atom[at].u.raw_data.option == RAW_BARE)
 			die(_("--format=%.*s cannot be used with"
-			      "--python, --shell, --tcl, --perl"), (int)(ep - sp - 2), sp + 2);
+			      "--python, --shell, --tcl"), (int)(ep - sp - 2), sp + 2);
 		cp = ep + 1;
 
 		if (skip_prefix(used_atom[at].name, "color:", &color))
diff --git a/t/t6300-for-each-ref.sh b/t/t6300-for-each-ref.sh
index 18554f62d94..3d15d0a5360 100755
--- a/t/t6300-for-each-ref.sh
+++ b/t/t6300-for-each-ref.sh
@@ -915,8 +915,23 @@ test_expect_success '%(raw) with --tcl must fail' '
 	test_must_fail git for-each-ref --format="%(raw)" --tcl
 '
 
-test_expect_success '%(raw) with --perl must fail' '
-	test_must_fail git for-each-ref --format="%(raw)" --perl
+test_expect_success '%(raw) with --perl' '
+	git for-each-ref --format="\$name= %(raw);
+print \"\$name\"" refs/myblobs/blob1 --perl | perl >actual &&
+	cmp blob1 actual &&
+	git for-each-ref --format="\$name= %(raw);
+print \"\$name\"" refs/myblobs/blob3 --perl | perl >actual &&
+	cmp blob3 actual &&
+	git for-each-ref --format="\$name= %(raw);
+print \"\$name\"" refs/myblobs/blob8 --perl | perl >actual &&
+	cmp blob8 actual &&
+	git for-each-ref --format="\$name= %(raw);
+print \"\$name\"" refs/myblobs/first --perl | perl >actual &&
+	cmp one actual &&
+	git cat-file tree refs/mytrees/first > expected &&
+	git for-each-ref --format="\$name= %(raw);
+print \"\$name\"" refs/mytrees/first --perl | perl >actual &&
+	cmp expected actual
 '
 
 test_expect_success '%(raw) with --shell must fail' '
-- 
gitgitgadget
Previous: ZheNing Hu via GitGitGadgetNext: ZheNing Hu via GitGitGadget
Message 19 of 45 in “[GSOC] ref-filter: add %(raw) and %(rest) atoms”
  1. 0/5 [GSOC] ref-filter: add %(raw) and %(rest) atomsZheNing Hu via GitGitGadget, Jul 22, 2021
  2. 1/5 [GSOC] ref-filter: add obj-type check in grab contentsZheNing Hu via GitGitGadget, Jul 22, 2021
  3. Christian CouderJul 22, 2021
  4. ZheNing HuJul 22, 2021
  5. 2/5 [GSOC] ref-filter: add %(raw) atomZheNing Hu via GitGitGadget, Jul 22, 2021
  6. Ævar Arnfjörð BjarmasonJul 22, 2021
  7. ZheNing HuJul 22, 2021
  8. 3/5 [GSOC] ref-filter: --format=%(raw) re-support --perlZheNing Hu via GitGitGadget, Jul 22, 2021
  9. 4/5 [GSOC] ref-filter: use non-const ref_format in *_atom_parser()ZheNing Hu via GitGitGadget, Jul 22, 2021
  10. 5/5 [GSOC] ref-filter: add %(rest) atomZheNing Hu via GitGitGadget, Jul 22, 2021
  11. Ævar Arnfjörð BjarmasonJul 22, 2021
  12. ZheNing HuJul 22, 2021
  13. Jacob KellerJul 23, 2021
  14. ZheNing HuJul 23, 2021
  15. Christian CouderJul 22, 2021
  16. ZheNing HuJul 22, 2021
  17. 0/5 [GSOC] ref-filter: add %(raw) and %(rest) atomsZheNing Hu via GitGitGadget, Jul 23, 2021
  18. 1/5 [GSOC] ref-filter: add obj-type check in grab contentsZheNing Hu via GitGitGadget, Jul 23, 2021
  19. 3/5 [GSOC] ref-filter: --format=%(raw) re-support --perlZheNing Hu via GitGitGadget, Jul 23, 2021
  20. 2/5 [GSOC] ref-filter: add %(raw) atomZheNing Hu via GitGitGadget, Jul 23, 2021
  21. Junio C HamanoJul 23, 2021
  22. ZheNing HuJul 24, 2021
  23. Junio C HamanoJul 24, 2021
  24. ZheNing HuJul 25, 2021
  25. 4/5 [GSOC] ref-filter: use non-const ref_format in *_atom_parser()ZheNing Hu via GitGitGadget, Jul 23, 2021
  26. 5/5 [GSOC] ref-filter: add %(rest) atomZheNing Hu via GitGitGadget, Jul 23, 2021
  27. 0/5 [GSOC] ref-filter: add %(raw) and %(rest) atomsZheNing Hu via GitGitGadget, Jul 24, 2021
  28. 1/5 [GSOC] ref-filter: add obj-type check in grab contentsZheNing Hu via GitGitGadget, Jul 24, 2021
  29. Jacob KellerJul 25, 2021
  30. 2/5 [GSOC] ref-filter: add %(raw) atomZheNing Hu via GitGitGadget, Jul 24, 2021
  31. 3/5 [GSOC] ref-filter: --format=%(raw) re-support --perlZheNing Hu via GitGitGadget, Jul 24, 2021
  32. Jacob KellerJul 25, 2021
  33. ZheNing HuJul 25, 2021
  34. 4/5 [GSOC] ref-filter: use non-const ref_format in *_atom_parser()ZheNing Hu via GitGitGadget, Jul 24, 2021
  35. 5/5 [GSOC] ref-filter: add %(rest) atomZheNing Hu via GitGitGadget, Jul 24, 2021
  36. Jacob KellerJul 25, 2021
  37. Junio C HamanoJul 26, 2021
  38. 0/5 [GSOC] ref-filter: add %(raw) and %(rest) atomsZheNing Hu via GitGitGadget, Jul 26, 2021
  39. 1/5 [GSOC] ref-filter: add obj-type check in grab contentsZheNing Hu via GitGitGadget, Jul 26, 2021
  40. Junio C HamanoJul 26, 2021
  41. ZheNing HuJul 27, 2021
  42. 2/5 [GSOC] ref-filter: add %(raw) atomZheNing Hu via GitGitGadget, Jul 26, 2021
  43. 3/5 [GSOC] ref-filter: --format=%(raw) support --perlZheNing Hu via GitGitGadget, Jul 26, 2021
  44. 4/5 [GSOC] ref-filter: use non-const ref_format in *_atom_parser()ZheNing Hu via GitGitGadget, Jul 26, 2021
  45. 5/5 [GSOC] ref-filter: add %(rest) atomZheNing Hu via GitGitGadget, Jul 26, 2021

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.