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

[PATCH v4 6/8] pretty: move trailer formatting to trailer.c

From
Jeff King <peff@peff.net>
Date
Aug 15, 2017, 10:23 UTC
Message-ID
<20170815102355.zf4lvkntbwxacgca@sigill.intra.peff.net>
In-Reply-To
<20170815102254.knccmhgralfijwli@sigill.intra.peff.net>

The next commit will add many features to the %(trailer) placeholder in pretty.c. We'll need to access some internal functions of trailer.c for that, so our options are either:

  1. expose those functions publicly
or
  2. make an entry point into trailer.c to do the formatting

Doing (2) ends up exposing less surface area, though do note that caveats in the docstring of the new function.

Signed-off-by: Jeff King <peff@peff.net>
---
 pretty.c  | 13 ++-----------
 trailer.c | 18 ++++++++++++++++++
 trailer.h | 14 ++++++++++++++
 3 files changed, 34 insertions(+), 11 deletions(-)
diff --git a/pretty.c b/pretty.c
index 39cad5112b..99b309bb15 100644
--- a/pretty.c
+++ b/pretty.c
@@ -871,16 +871,6 @@ const char *format_subject(struct strbuf *sb, const char *msg,
 	return msg;
 }
 
-static void format_trailers(struct strbuf *sb, const char *msg)
-{
-	struct trailer_info info;
-
-	trailer_info_get(&info, msg);
-	strbuf_add(sb, info.trailer_start,
-		   info.trailer_end - info.trailer_start);
-	trailer_info_release(&info);
-}
-
 static void parse_commit_message(struct format_commit_context *c)
 {
 	const char *msg = c->message + c->message_off;
@@ -1293,7 +1283,8 @@ static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */
 	}
 
 	if (starts_with(placeholder, "(trailers)")) {
-		format_trailers(sb, msg + c->subject_off);
+		struct process_trailer_options opts = PROCESS_TRAILER_OPTIONS_INIT;
+		format_trailers_from_commit(sb, msg + c->subject_off, &opts);
 		return strlen("(trailers)");
 	}
 
diff --git a/trailer.c b/trailer.c
index ed4fedc087..2d2b997ccc 100644
--- a/trailer.c
+++ b/trailer.c
@@ -1091,3 +1091,21 @@ void trailer_info_release(struct trailer_info *info)
 		free(info->trailers[i]);
 	free(info->trailers);
 }
+
+static void format_trailer_info(struct strbuf *out,
+				const struct trailer_info *info,
+				const struct process_trailer_options *opts)
+{
+	strbuf_add(out, info->trailer_start,
+		   info->trailer_end - info->trailer_start);
+}
+
+void format_trailers_from_commit(struct strbuf *out, const char *msg,
+				 const struct process_trailer_options *opts)
+{
+	struct trailer_info info;
+
+	trailer_info_get(&info, msg);
+	format_trailer_info(out, &info, opts);
+	trailer_info_release(&info);
+}
diff --git a/trailer.h b/trailer.h
index 194f85a102..a172811022 100644
--- a/trailer.h
+++ b/trailer.h
@@ -40,4 +40,18 @@ void trailer_info_get(struct trailer_info *info, const char *str);
 
 void trailer_info_release(struct trailer_info *info);
 
+/*
+ * Format the trailers from the commit msg "msg" into the strbuf "out".
+ * Note two caveats about "opts":
+ *
+ *   - this is primarily a helper for pretty.c, and not
+ *     all of the flags are supported.
+ *
+ *   - this differs from process_trailers slightly in that we always format
+ *     only the trailer block itself, even if the "only_trailers" option is not
+ *     set.
+ */
+void format_trailers_from_commit(struct strbuf *out, const char *msg,
+				 const struct process_trailer_options *opts);
+
 #endif /* TRAILER_H */
-- 
2.14.1.352.ge5efb0d3f3
Previous: Martin ÅgrenNext: Jeff King
Message 66 of 68 in “make interpret-trailers useful for parsing”
  1. 0/5 make interpret-trailers useful for parsingJeff King, Aug 9, 2017
  2. 1/5 trailer: put process_trailers() options into a structJeff King, Aug 9, 2017
  3. 2/5 interpret-trailers: add an option to show only the trailersJeff King, Aug 9, 2017
  4. Stefan BellerAug 9, 2017
  5. Stefan BellerAug 9, 2017
  6. Jonathan TanAug 9, 2017
  7. Jeff KingAug 10, 2017
  8. 3/5 interpret-trailers: add an option to show only existing trailersJeff King, Aug 9, 2017
  9. Stefan BellerAug 9, 2017
  10. Jeff KingAug 10, 2017
  11. Stefan BellerAug 10, 2017
  12. Jeff KingAug 10, 2017
  13. Stefan BellerAug 10, 2017
  14. Junio C HamanoAug 10, 2017
  15. Jonathan TanAug 9, 2017
  16. Jeff KingAug 10, 2017
  17. 4/5 interpret-trailers: add an option to normalize outputJeff King, Aug 9, 2017
  18. 5/5 interpret-trailers: add --parse convenience optionJeff King, Aug 9, 2017
  19. Stefan BellerAug 9, 2017
  20. Jeff KingAug 10, 2017
  21. Junio C HamanoAug 9, 2017
  22. Jacob KellerAug 10, 2017
  23. Jeff KingAug 10, 2017
  24. Junio C HamanoAug 10, 2017
  25. Jacob KellerAug 13, 2017
  26. Jeff KingAug 10, 2017
  27. 1/5 trailer: put process_trailers() options into a structJeff King, Aug 10, 2017
  28. 2/5 interpret-trailers: add an option to show only the trailersJeff King, Aug 10, 2017
  29. 3/5 interpret-trailers: add an option to show only existing trailersJeff King, Aug 10, 2017
  30. 4/5 interpret-trailers: add an option to normalize outputJeff King, Aug 10, 2017
  31. Stefan BellerAug 10, 2017
  32. Jeff KingAug 10, 2017
  33. Christian CouderAug 10, 2017
  34. Jeff KingAug 10, 2017
  35. Christian CouderAug 10, 2017
  36. Stefan BellerAug 10, 2017
  37. Christian CouderAug 10, 2017
  38. Jeff KingAug 10, 2017
  39. Ramsay JonesAug 10, 2017
  40. Jeff KingAug 10, 2017
  41. Ramsay JonesAug 10, 2017
  42. Christian CouderAug 11, 2017
  43. Jeff KingAug 11, 2017
  44. Christian CouderAug 11, 2017
  45. 5/5 interpret-trailers: add --parse convenience optionJeff King, Aug 10, 2017
  46. Jeff KingAug 10, 2017
  47. 1/5 trailer: put process_trailers() options into a structJeff King, Aug 10, 2017
  48. 2/5 interpret-trailers: add an option to show only the trailersJeff King, Aug 10, 2017
  49. Stefan BellerAug 10, 2017
  50. Jeff KingAug 10, 2017
  51. Junio C HamanoAug 10, 2017
  52. 3/5 interpret-trailers: add an option to show only existing trailersJeff King, Aug 10, 2017
  53. 4/5 interpret-trailers: add an option to normalize outputJeff King, Aug 10, 2017
  54. 5/5 interpret-trailers: add --parse convenience optionJeff King, Aug 10, 2017
  55. Stefan BellerAug 10, 2017
  56. Junio C HamanoAug 10, 2017
  57. 0/8 trailer parsing via interpret-trailers and %(trailers)Jeff King, Aug 15, 2017
  58. 1/8 trailer: put process_trailers() options into a structJeff King, Aug 15, 2017
  59. 2/8 interpret-trailers: add an option to show only the trailersJeff King, Aug 15, 2017
  60. 3/8 interpret-trailers: add an option to show only existing trailersJeff King, Aug 15, 2017
  61. 4/8 interpret-trailers: add an option to unfold valuesJeff King, Aug 15, 2017
  62. 5/8 interpret-trailers: add --parse convenience optionJeff King, Aug 15, 2017
  63. Martin ÅgrenAug 15, 2017
  64. Jeff KingAug 16, 2017
  65. Martin ÅgrenAug 17, 2017
  66. 6/8 pretty: move trailer formatting to trailer.cJeff King, Aug 15, 2017
  67. 7/8 t4205: refactor %(trailers) testsJeff King, Aug 15, 2017
  68. 8/8 pretty: support normalization options for %(trailers)Jeff King, Aug 15, 2017

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.