Skip to content

Commit bf35e0a

Browse files
Linus Arvergitster
authored andcommitted
format_trailers(): use strbuf instead of FILE
This is another preparatory refactor to unify the trailer formatters. Make format_trailers() also write to a strbuf, to align with format_trailers_from_commit() which also does the same. Doing this makes format_trailers() behave similar to format_trailer_info() (which will soon help us replace one with the other). Signed-off-by: Linus Arver <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 9aa1b2b commit bf35e0a

File tree

3 files changed

+14
-8
lines changed

3 files changed

+14
-8
lines changed

builtin/interpret-trailers.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ static void interpret_trailers(const struct process_trailer_options *opts,
140140
{
141141
LIST_HEAD(head);
142142
struct strbuf sb = STRBUF_INIT;
143+
struct strbuf trailer_block = STRBUF_INIT;
143144
struct trailer_info info;
144145
FILE *outfile = stdout;
145146

@@ -169,8 +170,11 @@ static void interpret_trailers(const struct process_trailer_options *opts,
169170
process_trailers_lists(&head, &arg_head);
170171
}
171172

172-
format_trailers(opts, &head, outfile);
173+
/* Print trailer block. */
174+
format_trailers(opts, &head, &trailer_block);
173175
free_trailers(&head);
176+
fwrite(trailer_block.buf, 1, trailer_block.len, outfile);
177+
strbuf_release(&trailer_block);
174178

175179
/* Print the lines after the trailers as is */
176180
if (!opts->only_trailers)

trailer.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -144,34 +144,35 @@ static char last_non_space_char(const char *s)
144144
return '\0';
145145
}
146146

147-
static void print_tok_val(FILE *outfile, const char *tok, const char *val)
147+
static void print_tok_val(struct strbuf *out, const char *tok, const char *val)
148148
{
149149
char c;
150150

151151
if (!tok) {
152-
fprintf(outfile, "%s\n", val);
152+
strbuf_addf(out, "%s\n", val);
153153
return;
154154
}
155155

156156
c = last_non_space_char(tok);
157157
if (!c)
158158
return;
159159
if (strchr(separators, c))
160-
fprintf(outfile, "%s%s\n", tok, val);
160+
strbuf_addf(out, "%s%s\n", tok, val);
161161
else
162-
fprintf(outfile, "%s%c %s\n", tok, separators[0], val);
162+
strbuf_addf(out, "%s%c %s\n", tok, separators[0], val);
163163
}
164164

165165
void format_trailers(const struct process_trailer_options *opts,
166-
struct list_head *trailers, FILE *outfile)
166+
struct list_head *trailers,
167+
struct strbuf *out)
167168
{
168169
struct list_head *pos;
169170
struct trailer_item *item;
170171
list_for_each(pos, trailers) {
171172
item = list_entry(pos, struct trailer_item, list);
172173
if ((!opts->trim_empty || strlen(item->value) > 0) &&
173174
(!opts->only_trailers || item->token))
174-
print_tok_val(outfile, item->token, item->value);
175+
print_tok_val(out, item->token, item->value);
175176
}
176177
}
177178

trailer.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,8 @@ void trailer_info_release(struct trailer_info *info);
102102

103103
void trailer_config_init(void);
104104
void format_trailers(const struct process_trailer_options *,
105-
struct list_head *trailers, FILE *outfile);
105+
struct list_head *trailers,
106+
struct strbuf *out);
106107
void free_trailers(struct list_head *);
107108

108109
/*

0 commit comments

Comments
 (0)