@@ -93,37 +93,6 @@ static int parse_opt_parse(const struct option *opt, const char *arg,
9393 return 0 ;
9494}
9595
96- static struct tempfile * trailers_tempfile ;
97-
98- static FILE * create_in_place_tempfile (const char * file )
99- {
100- struct stat st ;
101- struct strbuf filename_template = STRBUF_INIT ;
102- const char * tail ;
103- FILE * outfile ;
104-
105- if (stat (file , & st ))
106- die_errno (_ ("could not stat %s" ), file );
107- if (!S_ISREG (st .st_mode ))
108- die (_ ("file %s is not a regular file" ), file );
109- if (!(st .st_mode & S_IWUSR ))
110- die (_ ("file %s is not writable by user" ), file );
111-
112- /* Create temporary file in the same directory as the original */
113- tail = strrchr (file , '/' );
114- if (tail )
115- strbuf_add (& filename_template , file , tail - file + 1 );
116- strbuf_addstr (& filename_template , "git-interpret-trailers-XXXXXX" );
117-
118- trailers_tempfile = xmks_tempfile_m (filename_template .buf , st .st_mode );
119- strbuf_release (& filename_template );
120- outfile = fdopen_tempfile (trailers_tempfile , "w" );
121- if (!outfile )
122- die_errno (_ ("could not open temporary file" ));
123-
124- return outfile ;
125- }
126-
12796static void read_input_file (struct strbuf * sb , const char * file )
12897{
12998 if (file ) {
@@ -140,55 +109,31 @@ static void interpret_trailers(const struct process_trailer_options *opts,
140109 struct list_head * new_trailer_head ,
141110 const char * file )
142111{
143- LIST_HEAD (head );
144- struct strbuf sb = STRBUF_INIT ;
145- struct strbuf trailer_block_sb = STRBUF_INIT ;
146- struct trailer_block * trailer_block ;
147- FILE * outfile = stdout ;
112+ struct strbuf input = STRBUF_INIT ;
113+ struct strbuf out = STRBUF_INIT ;
114+ struct tempfile * tempfile = NULL ;
115+ int fd = 1 ;
148116
149117 trailer_config_init ();
150118
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 );
157-
158- /* Print the lines before the trailer block */
159- if (!opts -> only_trailers )
160- fwrite (sb .buf , 1 , trailer_block_start (trailer_block ), outfile );
161-
162- if (!opts -> only_trailers && !blank_line_before_trailer_block (trailer_block ))
163- fprintf (outfile , "\n" );
119+ read_input_file (& input , file );
164120
165-
166- if (!opts -> only_input ) {
167- LIST_HEAD (config_head );
168- LIST_HEAD (arg_head );
169- parse_trailers_from_config (& config_head );
170- parse_trailers_from_command_line_args (& arg_head , new_trailer_head );
171- list_splice (& config_head , & arg_head );
172- process_trailers_lists (& head , & arg_head );
121+ if (opts -> in_place ) {
122+ tempfile = trailer_create_in_place_tempfile (file );
123+ if (!tempfile )
124+ die (NULL );
125+ fd = tempfile -> fd ;
173126 }
127+ process_trailers (opts , new_trailer_head , & input , & out );
174128
175- /* Print trailer block. */
176- format_trailers (opts , & head , & trailer_block_sb );
177- free_trailers (& head );
178- fwrite (trailer_block_sb .buf , 1 , trailer_block_sb .len , outfile );
179- strbuf_release (& trailer_block_sb );
180-
181- /* Print the lines after the trailer block as is. */
182- if (!opts -> only_trailers )
183- fwrite (sb .buf + trailer_block_end (trailer_block ), 1 ,
184- sb .len - trailer_block_end (trailer_block ), outfile );
185- trailer_block_release (trailer_block );
186-
129+ if (write_in_full (fd , out .buf , out .len ) < 0 )
130+ die_errno (_ ("could not write to temporary file '%s'" ), file );
187131 if (opts -> in_place )
188- if (rename_tempfile (& trailers_tempfile , file ))
132+ if (rename_tempfile (& tempfile , file ))
189133 die_errno (_ ("could not rename temporary file to %s" ), file );
190134
191- strbuf_release (& sb );
135+ strbuf_release (& input );
136+ strbuf_release (& out );
192137}
193138
194139int cmd_interpret_trailers (int argc ,
0 commit comments