Skip to content

Commit 6145e03

Browse files
FirstLoveLifegitster
authored andcommitted
interpret-trailers: factor out buffer-based processing to process_trailers()
Extracted trailer processing into a helper that accumulates output in a strbuf before writing. Updated interpret_trailers() to reuse the helper, buffer output, and clean up both input and output buffers after writing. Signed-off-by: Li Chen <chenl311@chinatelecom.cn> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 77b7284 commit 6145e03

File tree

1 file changed

+29
-22
lines changed

1 file changed

+29
-22
lines changed

builtin/interpret-trailers.c

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -136,32 +136,21 @@ static void read_input_file(struct strbuf *sb, const char *file)
136136
strbuf_complete_line(sb);
137137
}
138138

139-
static void interpret_trailers(const struct process_trailer_options *opts,
140-
struct list_head *new_trailer_head,
141-
const char *file)
139+
static void process_trailers(const struct process_trailer_options *opts,
140+
struct list_head *new_trailer_head,
141+
struct strbuf *sb, struct strbuf *out)
142142
{
143143
LIST_HEAD(head);
144-
struct strbuf sb = STRBUF_INIT;
145-
struct strbuf trailer_block_sb = STRBUF_INIT;
146144
struct trailer_block *trailer_block;
147-
FILE *outfile = stdout;
148-
149-
trailer_config_init();
150145

151-
read_input_file(&sb, file);
152-
153-
if (opts->in_place)
154-
outfile = create_in_place_tempfile(file);
155-
156-
trailer_block = parse_trailers(opts, sb.buf, &head);
146+
trailer_block = parse_trailers(opts, sb->buf, &head);
157147

158148
/* Print the lines before the trailer block */
159149
if (!opts->only_trailers)
160-
fwrite(sb.buf, 1, trailer_block_start(trailer_block), outfile);
150+
strbuf_add(out, sb->buf, trailer_block_start(trailer_block));
161151

162152
if (!opts->only_trailers && !blank_line_before_trailer_block(trailer_block))
163-
fprintf(outfile, "\n");
164-
153+
strbuf_addch(out, '\n');
165154

166155
if (!opts->only_input) {
167156
LIST_HEAD(config_head);
@@ -173,22 +162,40 @@ static void interpret_trailers(const struct process_trailer_options *opts,
173162
}
174163

175164
/* Print trailer block. */
176-
format_trailers(opts, &head, &trailer_block_sb);
165+
format_trailers(opts, &head, out);
177166
free_trailers(&head);
178-
fwrite(trailer_block_sb.buf, 1, trailer_block_sb.len, outfile);
179-
strbuf_release(&trailer_block_sb);
180167

181168
/* Print the lines after the trailer block as is. */
182169
if (!opts->only_trailers)
183-
fwrite(sb.buf + trailer_block_end(trailer_block), 1,
184-
sb.len - trailer_block_end(trailer_block), outfile);
170+
strbuf_add(out, sb->buf + trailer_block_end(trailer_block),
171+
sb->len - trailer_block_end(trailer_block));
185172
trailer_block_release(trailer_block);
173+
}
174+
175+
static void interpret_trailers(const struct process_trailer_options *opts,
176+
struct list_head *new_trailer_head,
177+
const char *file)
178+
{
179+
struct strbuf sb = STRBUF_INIT;
180+
struct strbuf out = STRBUF_INIT;
181+
FILE *outfile = stdout;
182+
183+
trailer_config_init();
184+
185+
read_input_file(&sb, file);
186+
187+
if (opts->in_place)
188+
outfile = create_in_place_tempfile(file);
189+
190+
process_trailers(opts, new_trailer_head, &sb, &out);
186191

192+
fwrite(out.buf, out.len, 1, outfile);
187193
if (opts->in_place)
188194
if (rename_tempfile(&trailers_tempfile, file))
189195
die_errno(_("could not rename temporary file to %s"), file);
190196

191197
strbuf_release(&sb);
198+
strbuf_release(&out);
192199
}
193200

194201
int cmd_interpret_trailers(int argc,

0 commit comments

Comments
 (0)