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

[PATCH v5 6/7] strbuf: separate callback for strbuf_expand:ing literals

From
Anders Waldenborg <anders@0x63.nu>
Date
Jan 28, 2019, 21:33 UTC
Message-ID
<20190128213337.24752-7-anders@0x63.nu>
In-Reply-To
<20190128213337.24752-1-anders@0x63.nu>

Expanding '%n' and '%xNN' is generic functionality, so extract that from the pretty.c formatter into a callback that can be reused.

No functional change intended
Signed-off-by: Anders Waldenborg <anders@0x63.nu>
---
 pretty.c | 16 +++++-----------
 strbuf.c | 21 +++++++++++++++++++++
 strbuf.h |  8 ++++++++
 3 files changed, 34 insertions(+), 11 deletions(-)
diff --git a/pretty.c b/pretty.c
index ed25845c98..7baa4c1c26 100644
--- a/pretty.c
+++ b/pretty.c
@@ -1137,9 +1137,13 @@ static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */
 	const char *msg = c->message;
 	struct commit_list *p;
 	const char *arg;
-	int ch;
+	size_t res;
 
 	/* these are independent of the commit */
+	res = strbuf_expand_literal_cb(sb, placeholder, NULL);
+	if (res)
+		return res;
+
 	switch (placeholder[0]) {
 	case 'C':
 		if (starts_with(placeholder + 1, "(auto)")) {
@@ -1158,16 +1162,6 @@ static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */
 			 */
 			return ret;
 		}
-	case 'n':		/* newline */
-		strbuf_addch(sb, '\n');
-		return 1;
-	case 'x':
-		/* %x00 == NUL, %x0a == LF, etc. */
-		ch = hex2chr(placeholder + 1);
-		if (ch < 0)
-			return 0;
-		strbuf_addch(sb, ch);
-		return 3;
 	case 'w':
 		if (placeholder[1] == '(') {
 			unsigned long width = 0, indent1 = 0, indent2 = 0;
diff --git a/strbuf.c b/strbuf.c
index f6a6cf78b9..78eecd29f7 100644
--- a/strbuf.c
+++ b/strbuf.c
@@ -380,6 +380,27 @@ void strbuf_expand(struct strbuf *sb, const char *format, expand_fn_t fn,
 	}
 }
 
+size_t strbuf_expand_literal_cb(struct strbuf *sb,
+				const char *placeholder,
+				void *context)
+{
+	int ch;
+
+	switch (placeholder[0]) {
+	case 'n':		/* newline */
+		strbuf_addch(sb, '\n');
+		return 1;
+	case 'x':
+		/* %x00 == NUL, %x0a == LF, etc. */
+		ch = hex2chr(placeholder + 1);
+		if (ch < 0)
+			return 0;
+		strbuf_addch(sb, ch);
+		return 3;
+	}
+	return 0;
+}
+
 size_t strbuf_expand_dict_cb(struct strbuf *sb, const char *placeholder,
 		void *context)
 {
diff --git a/strbuf.h b/strbuf.h
index fc40873b65..52e44c9ab8 100644
--- a/strbuf.h
+++ b/strbuf.h
@@ -320,6 +320,14 @@ void strbuf_expand(struct strbuf *sb,
 		   expand_fn_t fn,
 		   void *context);
 
+/**
+ * Used as callback for `strbuf_expand` to only expand literals
+ * (i.e. %n and %xNN). The context argument is ignored.
+ */
+size_t strbuf_expand_literal_cb(struct strbuf *sb,
+				const char *placeholder,
+				void *context);
+
 /**
  * Used as callback for `strbuf_expand()`, expects an array of
  * struct strbuf_expand_dict_entry as context, i.e. pairs of
-- 
2.17.1
Previous: Anders WaldenborgNext: Anders Waldenborg
Message 65 of 69 in “pretty: Add %(trailer:X) to display single trailer”
  1. pretty: Add %(trailer:X) to display single trailerAnders Waldenborg, Oct 28, 2018
  2. Junio C HamanoOct 29, 2018
  3. Jeff KingOct 29, 2018
  4. Anders WaldenborgOct 29, 2018
  5. Jeff KingOct 31, 2018
  6. Anders WaldenborgOct 31, 2018
  7. Jeff KingNov 1, 2018
  8. 0/5 %(trailers) improvements in pretty formatAnders Waldenborg, Nov 4, 2018
  9. 1/5 pretty: single return path in %(trailers) handlingAnders Waldenborg, Nov 4, 2018
  10. 2/5 pretty: allow showing specific trailersAnders Waldenborg, Nov 4, 2018
  11. Eric SunshineNov 4, 2018
  12. Junio C HamanoNov 5, 2018
  13. Eric SunshineNov 5, 2018
  14. Anders WaldenborgNov 5, 2018
  15. Eric SunshineNov 5, 2018
  16. Junio C HamanoNov 5, 2018
  17. 3/5 pretty: add support for "nokey" option in %(trailers)Anders Waldenborg, Nov 4, 2018
  18. 4/5 pretty: extract fundamental placeholders to separate functionAnders Waldenborg, Nov 4, 2018
  19. Junio C HamanoNov 5, 2018
  20. Anders WaldenborgNov 5, 2018
  21. Junio C HamanoNov 6, 2018
  22. 5/5 pretty: add support for separator option in %(trailers)Anders Waldenborg, Nov 4, 2018
  23. Junio C HamanoNov 5, 2018
  24. Anders WaldenborgNov 5, 2018
  25. Junio C HamanoNov 6, 2018
  26. Junio C HamanoNov 5, 2018
  27. Eric SunshineNov 4, 2018
  28. Anders WaldenborgNov 5, 2018
  29. 0/5 %(trailers) improvements in pretty formatAnders Waldenborg, Nov 18, 2018
  30. 5/5 pretty: add support for separator option in %(trailers)Anders Waldenborg, Nov 18, 2018
  31. Eric SunshineNov 20, 2018
  32. 2/5 pretty: allow showing specific trailersAnders Waldenborg, Nov 18, 2018
  33. Junio C HamanoNov 20, 2018
  34. Junio C HamanoNov 20, 2018
  35. Anders WaldenborgNov 25, 2018
  36. Junio C HamanoNov 26, 2018
  37. Anders WaldenborgNov 26, 2018
  38. Junio C HamanoNov 26, 2018
  39. 3/5 pretty: add support for "valueonly" option in %(trailers)Anders Waldenborg, Nov 18, 2018
  40. Eric SunshineNov 20, 2018
  41. 1/5 pretty: single return path in %(trailers) handlingAnders Waldenborg, Nov 18, 2018
  42. 4/5 strbuf: separate callback for strbuf_expand:ing literalsAnders Waldenborg, Nov 18, 2018
  43. 0/7 %(trailers) improvements in pretty formatAnders Waldenborg, Dec 8, 2018
  44. 2/7 pretty: allow %(trailers) options with explicit valueAnders Waldenborg, Dec 8, 2018
  45. Junio C HamanoDec 10, 2018
  46. Anders WaldenborgDec 18, 2018
  47. Jeff KingJan 29, 2019
  48. Anders WaldenborgJan 29, 2019
  49. 1/7 doc: group pretty-format.txt placeholders descriptionsAnders Waldenborg, Dec 8, 2018
  50. 3/7 pretty: single return path in %(trailers) handlingAnders Waldenborg, Dec 8, 2018
  51. 5/7 pretty: add support for "valueonly" option in %(trailers)Anders Waldenborg, Dec 8, 2018
  52. 6/7 strbuf: separate callback for strbuf_expand:ing literalsAnders Waldenborg, Dec 8, 2018
  53. 7/7 pretty: add support for separator option in %(trailers)Anders Waldenborg, Dec 8, 2018
  54. 4/7 pretty: allow showing specific trailersAnders Waldenborg, Dec 8, 2018
  55. Junio C HamanoDec 10, 2018
  56. 0/7 %(trailers) improvements in pretty formatAnders Waldenborg, Jan 28, 2019
  57. 2/7 pretty: Allow %(trailers) options with explicit valueAnders Waldenborg, Jan 28, 2019
  58. Junio C HamanoJan 28, 2019
  59. Anders WaldenborgJan 29, 2019
  60. Jeff KingJan 29, 2019
  61. Anders WaldenborgJan 29, 2019
  62. 5/7 pretty: add support for "valueonly" option in %(trailers)Anders Waldenborg, Jan 28, 2019
  63. 7/7 pretty: add support for separator option in %(trailers)Anders Waldenborg, Jan 28, 2019
  64. 1/7 doc: group pretty-format.txt placeholders descriptionsAnders Waldenborg, Jan 28, 2019
  65. 6/7 strbuf: separate callback for strbuf_expand:ing literalsAnders Waldenborg, Jan 28, 2019
  66. 4/7 pretty: allow showing specific trailersAnders Waldenborg, Jan 28, 2019
  67. 3/7 pretty: single return path in %(trailers) handlingAnders Waldenborg, Jan 28, 2019
  68. Anders WaldenborgJan 31, 2019
  69. Оля ТележнаяFeb 2, 2019

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.