Skip to content

Commit 8efabc9

Browse files
FirstLoveLifegitster
authored andcommitted
interpret-trailers: factor trailer rewriting
Extract the trailer rewriting logic into a helper that appends to an output strbuf. Update interpret_trailers() to handle file I/O only: read input once, call the helper, and write the buffered result. This separation makes it easier to move the helper into trailer.c in the next commit. Signed-off-by: Phillip Wood <phillip.wood@dunelm.org.uk> Signed-off-by: Li Chen <me@linux.beauty> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 7c02d39 commit 8efabc9

1 file changed

Lines changed: 30 additions & 23 deletions

File tree

builtin/interpret-trailers.c

Lines changed: 30 additions & 23 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 *input, 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;
148145

149-
trailer_config_init();
150-
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, input->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, input->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, input->buf + trailer_block_end(trailer_block),
171+
input->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 input = STRBUF_INIT;
180+
struct strbuf out = STRBUF_INIT;
181+
FILE *outfile = stdout;
182+
183+
trailer_config_init();
184+
185+
read_input_file(&input, file);
186+
187+
if (opts->in_place)
188+
outfile = create_in_place_tempfile(file);
189+
190+
process_trailers(opts, new_trailer_head, &input, &out);
186191

192+
strbuf_write(&out, 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

191-
strbuf_release(&sb);
197+
strbuf_release(&input);
198+
strbuf_release(&out);
192199
}
193200

194201
int cmd_interpret_trailers(int argc,

0 commit comments

Comments
 (0)