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>
maint
Linus Arver 2024-05-02 04:54:23 +00:00 committed by Junio C Hamano
parent 655eb65d48
commit 24a25c630c
3 changed files with 30 additions and 30 deletions

View File

@ -141,7 +141,7 @@ static void interpret_trailers(const struct process_trailer_options *opts,
LIST_HEAD(head); LIST_HEAD(head);
struct strbuf sb = STRBUF_INIT; struct strbuf sb = STRBUF_INIT;
struct strbuf trailer_block = STRBUF_INIT; struct strbuf trailer_block = STRBUF_INIT;
struct trailer_info *info = trailer_info_new(); struct trailer_info *info;
FILE *outfile = stdout; FILE *outfile = stdout;


trailer_config_init(); trailer_config_init();
@ -151,7 +151,7 @@ static void interpret_trailers(const struct process_trailer_options *opts,
if (opts->in_place) if (opts->in_place)
outfile = create_in_place_tempfile(file); outfile = create_in_place_tempfile(file);


parse_trailers(opts, info, sb.buf, &head); info = parse_trailers(opts, sb.buf, &head);


/* Print the lines before the trailers */ /* Print the lines before the trailers */
if (!opts->only_trailers) if (!opts->only_trailers)

View File

@ -952,7 +952,7 @@ static void unfold_value(struct strbuf *val)
strbuf_release(&out); strbuf_release(&out);
} }


struct trailer_info *trailer_info_new(void) static struct trailer_info *trailer_info_new(void)
{ {
struct trailer_info *info = xcalloc(1, sizeof(*info)); struct trailer_info *info = xcalloc(1, sizeof(*info));
return info; return info;
@ -962,16 +962,16 @@ struct trailer_info *trailer_info_new(void)
* Parse trailers in "str", populating the trailer info and "head" * Parse trailers in "str", populating the trailer info and "head"
* linked list structure. * linked list structure.
*/ */
void parse_trailers(const struct process_trailer_options *opts, struct trailer_info *parse_trailers(const struct process_trailer_options *opts,
struct trailer_info *info,
const char *str, const char *str,
struct list_head *head) struct list_head *head)
{ {
struct trailer_info *info;
struct strbuf tok = STRBUF_INIT; struct strbuf tok = STRBUF_INIT;
struct strbuf val = STRBUF_INIT; struct strbuf val = STRBUF_INIT;
size_t i; size_t i;


trailer_info_get(opts, str, info); info = trailer_info_get(opts, str);


for (i = 0; i < info->trailer_nr; i++) { for (i = 0; i < info->trailer_nr; i++) {
int separator_pos; int separator_pos;
@ -995,6 +995,8 @@ void parse_trailers(const struct process_trailer_options *opts,
strbuf_detach(&val, NULL)); strbuf_detach(&val, NULL));
} }
} }

return info;
} }


void free_trailers(struct list_head *trailers) void free_trailers(struct list_head *trailers)
@ -1021,10 +1023,10 @@ int blank_line_before_trailer_block(struct trailer_info *info)
return info->blank_line_before_trailer; return info->blank_line_before_trailer;
} }


void trailer_info_get(const struct process_trailer_options *opts, struct trailer_info *trailer_info_get(const struct process_trailer_options *opts,
const char *str, const char *str)
struct trailer_info *info)
{ {
struct trailer_info *info = trailer_info_new();
size_t end_of_log_message = 0, trailer_block_start = 0; size_t end_of_log_message = 0, trailer_block_start = 0;
struct strbuf **trailer_lines, **ptr; struct strbuf **trailer_lines, **ptr;
char **trailer_strings = NULL; char **trailer_strings = NULL;
@ -1063,6 +1065,8 @@ void trailer_info_get(const struct process_trailer_options *opts,
info->trailer_block_end = end_of_log_message; info->trailer_block_end = end_of_log_message;
info->trailers = trailer_strings; info->trailers = trailer_strings;
info->trailer_nr = nr; info->trailer_nr = nr;

return info;
} }


void trailer_info_release(struct trailer_info *info) void trailer_info_release(struct trailer_info *info)
@ -1071,6 +1075,7 @@ void trailer_info_release(struct trailer_info *info)
for (i = 0; i < info->trailer_nr; i++) for (i = 0; i < info->trailer_nr; i++)
free(info->trailers[i]); free(info->trailers[i]);
free(info->trailers); free(info->trailers);
free(info);
} }


void format_trailers(const struct process_trailer_options *opts, void format_trailers(const struct process_trailer_options *opts,
@ -1138,21 +1143,19 @@ void format_trailers_from_commit(const struct process_trailer_options *opts,
struct strbuf *out) struct strbuf *out)
{ {
LIST_HEAD(trailer_objects); LIST_HEAD(trailer_objects);
struct trailer_info info; struct trailer_info *info = parse_trailers(opts, msg, &trailer_objects);

parse_trailers(opts, &info, msg, &trailer_objects);


/* If we want the whole block untouched, we can take the fast path. */ /* If we want the whole block untouched, we can take the fast path. */
if (!opts->only_trailers && !opts->unfold && !opts->filter && if (!opts->only_trailers && !opts->unfold && !opts->filter &&
!opts->separator && !opts->key_only && !opts->value_only && !opts->separator && !opts->key_only && !opts->value_only &&
!opts->key_value_separator) { !opts->key_value_separator) {
strbuf_add(out, msg + info.trailer_block_start, strbuf_add(out, msg + info->trailer_block_start,
info.trailer_block_end - info.trailer_block_start); info->trailer_block_end - info->trailer_block_start);
} else } else
format_trailers(opts, &trailer_objects, out); format_trailers(opts, &trailer_objects, out);


free_trailers(&trailer_objects); free_trailers(&trailer_objects);
trailer_info_release(&info); trailer_info_release(info);
} }


void trailer_iterator_init(struct trailer_iterator *iter, const char *msg) 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)
strbuf_init(&iter->key, 0); strbuf_init(&iter->key, 0);
strbuf_init(&iter->val, 0); strbuf_init(&iter->val, 0);
opts.no_divider = 1; opts.no_divider = 1;
trailer_info_get(&opts, msg, &iter->internal.info); iter->internal.info = trailer_info_get(&opts, msg);
iter->internal.cur = 0; iter->internal.cur = 0;
} }


int trailer_iterator_advance(struct trailer_iterator *iter) int trailer_iterator_advance(struct trailer_iterator *iter)
{ {
if (iter->internal.cur < iter->internal.info.trailer_nr) { if (iter->internal.cur < iter->internal.info->trailer_nr) {
char *line = iter->internal.info.trailers[iter->internal.cur++]; char *line = iter->internal.info->trailers[iter->internal.cur++];
int separator_pos = find_separator(line, separators); int separator_pos = find_separator(line, separators);


iter->raw = line; iter->raw = line;
@ -1185,7 +1188,7 @@ int trailer_iterator_advance(struct trailer_iterator *iter)


void trailer_iterator_release(struct trailer_iterator *iter) void trailer_iterator_release(struct trailer_iterator *iter)
{ {
trailer_info_release(&iter->internal.info); trailer_info_release(iter->internal.info);
strbuf_release(&iter->val); strbuf_release(&iter->val);
strbuf_release(&iter->key); strbuf_release(&iter->key);
} }

View File

@ -89,18 +89,15 @@ void parse_trailers_from_command_line_args(struct list_head *arg_head,
void process_trailers_lists(struct list_head *head, void process_trailers_lists(struct list_head *head,
struct list_head *arg_head); struct list_head *arg_head);


void parse_trailers(const struct process_trailer_options *, struct trailer_info *parse_trailers(const struct process_trailer_options *,
struct trailer_info *,
const char *str, const char *str,
struct list_head *head); struct list_head *head);
struct trailer_info *trailer_info_get(const struct process_trailer_options *,
const char *str);


void trailer_info_get(const struct process_trailer_options *,
const char *str,
struct trailer_info *);
size_t trailer_block_start(struct trailer_info *); size_t trailer_block_start(struct trailer_info *);
size_t trailer_block_end(struct trailer_info *); size_t trailer_block_end(struct trailer_info *);
int blank_line_before_trailer_block(struct trailer_info *); int blank_line_before_trailer_block(struct trailer_info *);
struct trailer_info *trailer_info_new(void);


void trailer_info_release(struct trailer_info *info); void trailer_info_release(struct trailer_info *info);


@ -141,7 +138,7 @@ struct trailer_iterator {


/* private */ /* private */
struct { struct {
struct trailer_info info; struct trailer_info *info;
size_t cur; size_t cur;
} internal; } internal;
}; };