interpret-trailers: access trailer_info with new helpers

Instead of directly accessing trailer_info members, access them indirectly through new helper functions exposed by the trailer API. This is the first of two preparatory commits which will allow us to use the so-called "pimpl" (pointer to implementation) idiom for the trailer API, by making the trailer_info struct private to the trailer implementation (and thus hidden from the API). 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 655eb65d48bec60d24baf66bf19de394eb2e6aea
3 files changed +31 -6
builtin/interpret-trailers.c
+6 -6
@@ -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;
144 + struct trailer_info *info = trailer_info_new();
145 FILE *outfile = stdout;
146
147 trailer_config_init();
@@ -151,13 +151,13 @@ 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 + parse_trailers(opts, info, sb.buf, &head);
155
156 /* Print the lines before the trailers */
157 if (!opts->only_trailers)
158 - fwrite(sb.buf, 1, info.trailer_block_start, outfile);
158 + fwrite(sb.buf, 1, trailer_block_start(info), outfile);
159
160 - if (!opts->only_trailers && !info.blank_line_before_trailer)
160 + if (!opts->only_trailers && !blank_line_before_trailer_block(info))
161 fprintf(outfile, "\n");
162
163
@@ -178,8 +178,8 @@ static void interpret_trailers(const struct process_trailer_options *opts,
178
179 /* Print the lines after the trailers as is */
180 if (!opts->only_trailers)
181 - fwrite(sb.buf + info.trailer_block_end, 1, sb.len - info.trailer_block_end, outfile);
182 - trailer_info_release(&info);
181 + fwrite(sb.buf + trailer_block_end(info), 1, sb.len - trailer_block_end(info), outfile);
182 + trailer_info_release(info);
183
184 if (opts->in_place)
185 if (rename_tempfile(&trailers_tempfile, file))
trailer.c
+21
@@ -952,6 +952,12 @@ static void unfold_value(struct strbuf *val)
952 strbuf_release(&out);
953 }
954
955 +struct trailer_info *trailer_info_new(void)
956 +{
957 + struct trailer_info *info = xcalloc(1, sizeof(*info));
958 + return info;
959 +}
960 +
961 /*
962 * Parse trailers in "str", populating the trailer info and "head"
963 * linked list structure.
@@ -1000,6 +1006,21 @@ void free_trailers(struct list_head *trailers)
1006 }
1007 }
1008
1009 +size_t trailer_block_start(struct trailer_info *info)
1010 +{
1011 + return info->trailer_block_start;
1012 +}
1013 +
1014 +size_t trailer_block_end(struct trailer_info *info)
1015 +{
1016 + return info->trailer_block_end;
1017 +}
1018 +
1019 +int blank_line_before_trailer_block(struct trailer_info *info)
1020 +{
1021 + return info->blank_line_before_trailer;
1022 +}
1023 +
1024 void trailer_info_get(const struct process_trailer_options *opts,
1025 const char *str,
1026 struct trailer_info *info)
trailer.h
+4
@@ -97,6 +97,10 @@ void parse_trailers(const struct process_trailer_options *,
97 void trailer_info_get(const struct process_trailer_options *,
98 const char *str,
99 struct trailer_info *);
100 +size_t trailer_block_start(struct trailer_info *);
101 +size_t trailer_block_end(struct trailer_info *);
102 +int blank_line_before_trailer_block(struct trailer_info *);
103 +struct trailer_info *trailer_info_new(void);
104
105 void trailer_info_release(struct trailer_info *info);
106