Skip to content

Commit 24a25c6

Browse files
listxgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Linus Arver <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 655eb65 commit 24a25c6

File tree

3 files changed

+31
-31
lines changed

3 files changed

+31
-31
lines changed

builtin/interpret-trailers.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ static void interpret_trailers(const struct process_trailer_options *opts,
141141
LIST_HEAD(head);
142142
struct strbuf sb = STRBUF_INIT;
143143
struct strbuf trailer_block = STRBUF_INIT;
144-
struct trailer_info *info = trailer_info_new();
144+
struct trailer_info *info;
145145
FILE *outfile = stdout;
146146

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

154-
parse_trailers(opts, info, sb.buf, &head);
154+
info = parse_trailers(opts, sb.buf, &head);
155155

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

trailer.c

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -952,7 +952,7 @@ static void unfold_value(struct strbuf *val)
952952
strbuf_release(&out);
953953
}
954954

955-
struct trailer_info *trailer_info_new(void)
955+
static struct trailer_info *trailer_info_new(void)
956956
{
957957
struct trailer_info *info = xcalloc(1, sizeof(*info));
958958
return info;
@@ -962,16 +962,16 @@ struct trailer_info *trailer_info_new(void)
962962
* Parse trailers in "str", populating the trailer info and "head"
963963
* linked list structure.
964964
*/
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)
969968
{
969+
struct trailer_info *info;
970970
struct strbuf tok = STRBUF_INIT;
971971
struct strbuf val = STRBUF_INIT;
972972
size_t i;
973973

974-
trailer_info_get(opts, str, info);
974+
info = trailer_info_get(opts, str);
975975

976976
for (i = 0; i < info->trailer_nr; i++) {
977977
int separator_pos;
@@ -995,6 +995,8 @@ void parse_trailers(const struct process_trailer_options *opts,
995995
strbuf_detach(&val, NULL));
996996
}
997997
}
998+
999+
return info;
9981000
}
9991001

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

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)
10271028
{
1029+
struct trailer_info *info = trailer_info_new();
10281030
size_t end_of_log_message = 0, trailer_block_start = 0;
10291031
struct strbuf **trailer_lines, **ptr;
10301032
char **trailer_strings = NULL;
@@ -1063,6 +1065,8 @@ void trailer_info_get(const struct process_trailer_options *opts,
10631065
info->trailer_block_end = end_of_log_message;
10641066
info->trailers = trailer_strings;
10651067
info->trailer_nr = nr;
1068+
1069+
return info;
10661070
}
10671071

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

10761081
void format_trailers(const struct process_trailer_options *opts,
@@ -1138,21 +1143,19 @@ void format_trailers_from_commit(const struct process_trailer_options *opts,
11381143
struct strbuf *out)
11391144
{
11401145
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);
11441147

11451148
/* If we want the whole block untouched, we can take the fast path. */
11461149
if (!opts->only_trailers && !opts->unfold && !opts->filter &&
11471150
!opts->separator && !opts->key_only && !opts->value_only &&
11481151
!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);
11511154
} else
11521155
format_trailers(opts, &trailer_objects, out);
11531156

11541157
free_trailers(&trailer_objects);
1155-
trailer_info_release(&info);
1158+
trailer_info_release(info);
11561159
}
11571160

11581161
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)
11611164
strbuf_init(&iter->key, 0);
11621165
strbuf_init(&iter->val, 0);
11631166
opts.no_divider = 1;
1164-
trailer_info_get(&opts, msg, &iter->internal.info);
1167+
iter->internal.info = trailer_info_get(&opts, msg);
11651168
iter->internal.cur = 0;
11661169
}
11671170

11681171
int trailer_iterator_advance(struct trailer_iterator *iter)
11691172
{
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++];
11721175
int separator_pos = find_separator(line, separators);
11731176

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

11861189
void trailer_iterator_release(struct trailer_iterator *iter)
11871190
{
1188-
trailer_info_release(&iter->internal.info);
1191+
trailer_info_release(iter->internal.info);
11891192
strbuf_release(&iter->val);
11901193
strbuf_release(&iter->key);
11911194
}

trailer.h

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -89,18 +89,15 @@ void parse_trailers_from_command_line_args(struct list_head *arg_head,
8989
void process_trailers_lists(struct list_head *head,
9090
struct list_head *arg_head);
9191

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+
10098
size_t trailer_block_start(struct trailer_info *);
10199
size_t trailer_block_end(struct trailer_info *);
102100
int blank_line_before_trailer_block(struct trailer_info *);
103-
struct trailer_info *trailer_info_new(void);
104101

105102
void trailer_info_release(struct trailer_info *info);
106103

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

142139
/* private */
143140
struct {
144-
struct trailer_info info;
141+
struct trailer_info *info;
145142
size_t cur;
146143
} internal;
147144
};

0 commit comments

Comments
 (0)