trailer: make parse_trailers() return trailer_info pointer

This is the second and final preparatory commit for making the trailer_info struct private to the trailer implementation. Make trailer_info_get() do the actual work of allocating a new trailer_info struct, and return a pointer to it. Because parse_trailers() wraps around trailer_info_get(), it too can return this pointer to the caller. From the trailer API user's perspective, the call to trailer_info_new() can be replaced with parse_trailers(); do so in interpret-trailers. Because trailer_info_new() is no longer called by interpret-trailers, remove this function from the trailer API. With this change, we no longer allocate trailer_info on the stack --- all uses of it are via a pointer where the actual data is always allocated at runtime through trailer_info_new(). Make trailer_info_release() free this dynamically allocated memory. Finally, due to the way the function signatures of parse_trailers() and trailer_info_get() have changed, update the callsites in format_trailers_from_commit() and trailer_iterator_init() accordingly. Helped-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Linus Arver <linus@ucla.edu> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Linus Arver committed May 2, 2024 at 04:54 UTC 24a25c630cfe72d2d77fed5d2841f7c017a269b5
3 files changed +31 -31
builtin/interpret-trailers.c
+2 -2
@@ -141,7 +141,7 @@ static void interpret_trailers(const struct process_trailer_options *opts,
141 LIST_HEAD(head);
142 struct strbuf sb = STRBUF_INIT;
143 struct strbuf trailer_block = STRBUF_INIT;
144 - struct trailer_info *info = trailer_info_new();
144 + struct trailer_info *info;
145 FILE *outfile = stdout;
146
147 trailer_config_init();
@@ -151,7 +151,7 @@ static void interpret_trailers(const struct process_trailer_options *opts,
151 if (opts->in_place)
152 outfile = create_in_place_tempfile(file);
153
154 - parse_trailers(opts, info, sb.buf, &head);
154 + info = parse_trailers(opts, sb.buf, &head);
155
156 /* Print the lines before the trailers */
157 if (!opts->only_trailers)
trailer.c
+22 -19
@@ -952,7 +952,7 @@ static void unfold_value(struct strbuf *val)
952 strbuf_release(&out);
953 }
954
955 -struct trailer_info *trailer_info_new(void)
955 +static struct trailer_info *trailer_info_new(void)
956 {
957 struct trailer_info *info = xcalloc(1, sizeof(*info));
958 return info;
@@ -962,16 +962,16 @@ struct trailer_info *trailer_info_new(void)
962 * Parse trailers in "str", populating the trailer info and "head"
963 * linked list structure.
964 */
965 -void parse_trailers(const struct process_trailer_options *opts,
966 - struct trailer_info *info,
967 - const char *str,
968 - struct list_head *head)
965 +struct trailer_info *parse_trailers(const struct process_trailer_options *opts,
966 + const char *str,
967 + struct list_head *head)
968 {
969 + struct trailer_info *info;
970 struct strbuf tok = STRBUF_INIT;
971 struct strbuf val = STRBUF_INIT;
972 size_t i;
973
974 - trailer_info_get(opts, str, info);
974 + info = trailer_info_get(opts, str);
975
976 for (i = 0; i < info->trailer_nr; i++) {
977 int separator_pos;
@@ -995,6 +995,8 @@ void parse_trailers(const struct process_trailer_options *opts,
995 strbuf_detach(&val, NULL));
996 }
997 }
998 +
999 + return info;
1000 }
1001
1002 void free_trailers(struct list_head *trailers)
@@ -1021,10 +1023,10 @@ int blank_line_before_trailer_block(struct trailer_info *info)
1023 return info->blank_line_before_trailer;
1024 }
1025
1024 -void trailer_info_get(const struct process_trailer_options *opts,
1025 - const char *str,
1026 - struct trailer_info *info)
1026 +struct trailer_info *trailer_info_get(const struct process_trailer_options *opts,
1027 + const char *str)
1028 {
1029 + struct trailer_info *info = trailer_info_new();
1030 size_t end_of_log_message = 0, trailer_block_start = 0;
1031 struct strbuf **trailer_lines, **ptr;
1032 char **trailer_strings = NULL;
@@ -1063,6 +1065,8 @@ void trailer_info_get(const struct process_trailer_options *opts,
1065 info->trailer_block_end = end_of_log_message;
1066 info->trailers = trailer_strings;
1067 info->trailer_nr = nr;
1068 +
1069 + return info;
1070 }
1071
1072 void trailer_info_release(struct trailer_info *info)
@@ -1071,6 +1075,7 @@ void trailer_info_release(struct trailer_info *info)
1075 for (i = 0; i < info->trailer_nr; i++)
1076 free(info->trailers[i]);
1077 free(info->trailers);
1078 + free(info);
1079 }
1080
1081 void format_trailers(const struct process_trailer_options *opts,
@@ -1138,21 +1143,19 @@ void format_trailers_from_commit(const struct process_trailer_options *opts,
1143 struct strbuf *out)
1144 {
1145 LIST_HEAD(trailer_objects);
1141 - struct trailer_info info;
1142 -
1143 - parse_trailers(opts, &info, msg, &trailer_objects);
1146 + struct trailer_info *info = parse_trailers(opts, msg, &trailer_objects);
1147
1148 /* If we want the whole block untouched, we can take the fast path. */
1149 if (!opts->only_trailers && !opts->unfold && !opts->filter &&
1150 !opts->separator && !opts->key_only && !opts->value_only &&
1151 !opts->key_value_separator) {
1149 - strbuf_add(out, msg + info.trailer_block_start,
1150 - info.trailer_block_end - info.trailer_block_start);
1152 + strbuf_add(out, msg + info->trailer_block_start,
1153 + info->trailer_block_end - info->trailer_block_start);
1154 } else
1155 format_trailers(opts, &trailer_objects, out);
1156
1157 free_trailers(&trailer_objects);
1155 - trailer_info_release(&info);
1158 + trailer_info_release(info);
1159 }
1160
1161 void trailer_iterator_init(struct trailer_iterator *iter, const char *msg)
@@ -1161,14 +1164,14 @@ void trailer_iterator_init(struct trailer_iterator *iter, const char *msg)
1164 strbuf_init(&iter->key, 0);
1165 strbuf_init(&iter->val, 0);
1166 opts.no_divider = 1;
1164 - trailer_info_get(&opts, msg, &iter->internal.info);
1167 + iter->internal.info = trailer_info_get(&opts, msg);
1168 iter->internal.cur = 0;
1169 }
1170
1171 int trailer_iterator_advance(struct trailer_iterator *iter)
1172 {
1170 - if (iter->internal.cur < iter->internal.info.trailer_nr) {
1171 - char *line = iter->internal.info.trailers[iter->internal.cur++];
1173 + if (iter->internal.cur < iter->internal.info->trailer_nr) {
1174 + char *line = iter->internal.info->trailers[iter->internal.cur++];
1175 int separator_pos = find_separator(line, separators);
1176
1177 iter->raw = line;
@@ -1185,7 +1188,7 @@ int trailer_iterator_advance(struct trailer_iterator *iter)
1188
1189 void trailer_iterator_release(struct trailer_iterator *iter)
1190 {
1188 - trailer_info_release(&iter->internal.info);
1191 + trailer_info_release(iter->internal.info);
1192 strbuf_release(&iter->val);
1193 strbuf_release(&iter->key);
1194 }
trailer.h
+7 -10
@@ -89,18 +89,15 @@ void parse_trailers_from_command_line_args(struct list_head *arg_head,
89 void process_trailers_lists(struct list_head *head,
90 struct list_head *arg_head);
91
92 -void parse_trailers(const struct process_trailer_options *,
93 - struct trailer_info *,
94 - const char *str,
95 - struct list_head *head);
96 -
97 -void trailer_info_get(const struct process_trailer_options *,
98 - const char *str,
99 - struct trailer_info *);
92 +struct trailer_info *parse_trailers(const struct process_trailer_options *,
93 + const char *str,
94 + struct list_head *head);
95 +struct trailer_info *trailer_info_get(const struct process_trailer_options *,
96 + const char *str);
97 +
98 size_t trailer_block_start(struct trailer_info *);
99 size_t trailer_block_end(struct trailer_info *);
100 int blank_line_before_trailer_block(struct trailer_info *);
103 -struct trailer_info *trailer_info_new(void);
101
102 void trailer_info_release(struct trailer_info *info);
103
@@ -141,7 +138,7 @@ struct trailer_iterator {
138
139 /* private */
140 struct {
144 - struct trailer_info info;
141 + struct trailer_info *info;
142 size_t cur;
143 } internal;
144 };