pretty: move trailer formatting to trailer.c

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> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Aug 15, 2017 at 06:23 UTC a388b10fc17c435df32c3875225a1468edad9535
3 files changed +34 -11
pretty.c
+2 -11
@@ -870,16 +870,6 @@ const char *format_subject(struct strbuf *sb, const char *msg,
870 return msg;
871 }
872
873 -static void format_trailers(struct strbuf *sb, const char *msg)
874 -{
875 - struct trailer_info info;
876 -
877 - trailer_info_get(&info, msg);
878 - strbuf_add(sb, info.trailer_start,
879 - info.trailer_end - info.trailer_start);
880 - trailer_info_release(&info);
881 -}
882 -
873 static void parse_commit_message(struct format_commit_context *c)
874 {
875 const char *msg = c->message + c->message_off;
@@ -1273,7 +1263,8 @@ static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */
1263 }
1264
1265 if (starts_with(placeholder, "(trailers)")) {
1276 - format_trailers(sb, msg + c->subject_off);
1266 + struct process_trailer_options opts = PROCESS_TRAILER_OPTIONS_INIT;
1267 + format_trailers_from_commit(sb, msg + c->subject_off, &opts);
1268 return strlen("(trailers)");
1269 }
1270
trailer.c
+18
@@ -1090,3 +1090,21 @@ void trailer_info_release(struct trailer_info *info)
1090 free(info->trailers[i]);
1091 free(info->trailers);
1092 }
1093 +
1094 +static void format_trailer_info(struct strbuf *out,
1095 + const struct trailer_info *info,
1096 + const struct process_trailer_options *opts)
1097 +{
1098 + strbuf_add(out, info->trailer_start,
1099 + info->trailer_end - info->trailer_start);
1100 +}
1101 +
1102 +void format_trailers_from_commit(struct strbuf *out, const char *msg,
1103 + const struct process_trailer_options *opts)
1104 +{
1105 + struct trailer_info info;
1106 +
1107 + trailer_info_get(&info, msg);
1108 + format_trailer_info(out, &info, opts);
1109 + trailer_info_release(&info);
1110 +}
trailer.h
+14
@@ -40,4 +40,18 @@ void trailer_info_get(struct trailer_info *info, const char *str);
40
41 void trailer_info_release(struct trailer_info *info);
42
43 +/*
44 + * Format the trailers from the commit msg "msg" into the strbuf "out".
45 + * Note two caveats about "opts":
46 + *
47 + * - this is primarily a helper for pretty.c, and not
48 + * all of the flags are supported.
49 + *
50 + * - this differs from process_trailers slightly in that we always format
51 + * only the trailer block itself, even if the "only_trailers" option is not
52 + * set.
53 + */
54 +void format_trailers_from_commit(struct strbuf *out, const char *msg,
55 + const struct process_trailer_options *opts);
56 +
57 #endif /* TRAILER_H */