9
9
#include "gettext.h"
10
10
#include "parse-options.h"
11
11
#include "string-list.h"
12
- #include "tempfile.h"
13
12
#include "trailer.h"
14
13
#include "config.h"
15
14
@@ -84,6 +83,7 @@ static int parse_opt_parse(const struct option *opt, const char *arg,
84
83
int unset )
85
84
{
86
85
struct process_trailer_options * v = opt -> value ;
86
+
87
87
v -> only_trailers = 1 ;
88
88
v -> only_input = 1 ;
89
89
v -> unfold = 1 ;
@@ -92,37 +92,6 @@ static int parse_opt_parse(const struct option *opt, const char *arg,
92
92
return 0 ;
93
93
}
94
94
95
- static struct tempfile * trailers_tempfile ;
96
-
97
- static FILE * create_in_place_tempfile (const char * file )
98
- {
99
- struct stat st ;
100
- struct strbuf filename_template = STRBUF_INIT ;
101
- const char * tail ;
102
- FILE * outfile ;
103
-
104
- if (stat (file , & st ))
105
- die_errno (_ ("could not stat %s" ), file );
106
- if (!S_ISREG (st .st_mode ))
107
- die (_ ("file %s is not a regular file" ), file );
108
- if (!(st .st_mode & S_IWUSR ))
109
- die (_ ("file %s is not writable by user" ), file );
110
-
111
- /* Create temporary file in the same directory as the original */
112
- tail = strrchr (file , '/' );
113
- if (tail )
114
- strbuf_add (& filename_template , file , tail - file + 1 );
115
- strbuf_addstr (& filename_template , "git-interpret-trailers-XXXXXX" );
116
-
117
- trailers_tempfile = xmks_tempfile_m (filename_template .buf , st .st_mode );
118
- strbuf_release (& filename_template );
119
- outfile = fdopen_tempfile (trailers_tempfile , "w" );
120
- if (!outfile )
121
- die_errno (_ ("could not open temporary file" ));
122
-
123
- return outfile ;
124
- }
125
-
126
95
static void read_input_file (struct strbuf * sb , const char * file )
127
96
{
128
97
if (file ) {
@@ -135,61 +104,6 @@ static void read_input_file(struct strbuf *sb, const char *file)
135
104
strbuf_complete_line (sb );
136
105
}
137
106
138
- static void interpret_trailers (const struct process_trailer_options * opts ,
139
- struct list_head * new_trailer_head ,
140
- const char * file )
141
- {
142
- LIST_HEAD (head );
143
- struct strbuf sb = STRBUF_INIT ;
144
- struct strbuf trailer_block_sb = STRBUF_INIT ;
145
- struct trailer_block * trailer_block ;
146
- FILE * outfile = stdout ;
147
-
148
- trailer_config_init ();
149
-
150
- read_input_file (& sb , file );
151
-
152
- if (opts -> in_place )
153
- outfile = create_in_place_tempfile (file );
154
-
155
- trailer_block = parse_trailers (opts , sb .buf , & head );
156
-
157
- /* Print the lines before the trailer block */
158
- if (!opts -> only_trailers )
159
- fwrite (sb .buf , 1 , trailer_block_start (trailer_block ), outfile );
160
-
161
- if (!opts -> only_trailers && !blank_line_before_trailer_block (trailer_block ))
162
- fprintf (outfile , "\n" );
163
-
164
-
165
- if (!opts -> only_input ) {
166
- LIST_HEAD (config_head );
167
- LIST_HEAD (arg_head );
168
- parse_trailers_from_config (& config_head );
169
- parse_trailers_from_command_line_args (& arg_head , new_trailer_head );
170
- list_splice (& config_head , & arg_head );
171
- process_trailers_lists (& head , & arg_head );
172
- }
173
-
174
- /* Print trailer block. */
175
- format_trailers (opts , & head , & trailer_block_sb );
176
- free_trailers (& head );
177
- fwrite (trailer_block_sb .buf , 1 , trailer_block_sb .len , outfile );
178
- strbuf_release (& trailer_block_sb );
179
-
180
- /* Print the lines after the trailer block as is. */
181
- if (!opts -> only_trailers )
182
- fwrite (sb .buf + trailer_block_end (trailer_block ), 1 ,
183
- sb .len - trailer_block_end (trailer_block ), outfile );
184
- trailer_block_release (trailer_block );
185
-
186
- if (opts -> in_place )
187
- if (rename_tempfile (& trailers_tempfile , file ))
188
- die_errno (_ ("could not rename temporary file to %s" ), file );
189
-
190
- strbuf_release (& sb );
191
- }
192
-
193
107
int cmd_interpret_trailers (int argc ,
194
108
const char * * argv ,
195
109
const char * prefix ,
@@ -231,14 +145,37 @@ int cmd_interpret_trailers(int argc,
231
145
git_interpret_trailers_usage ,
232
146
options );
233
147
148
+ trailer_config_init ();
149
+
234
150
if (argc ) {
235
151
int i ;
236
- for (i = 0 ; i < argc ; i ++ )
237
- interpret_trailers (& opts , & trailers , argv [i ]);
152
+ for (i = 0 ; i < argc ; i ++ ) {
153
+ struct strbuf in_buf = STRBUF_INIT ;
154
+ struct strbuf out_buf = STRBUF_INIT ;
155
+
156
+ read_input_file (& in_buf , argv [i ]);
157
+ if (trailer_process (& opts , in_buf .buf , & trailers , & out_buf ) < 0 )
158
+ die (_ ("failed to process trailers for %s" ), argv [i ]);
159
+ if (opts .in_place )
160
+ write_file_buf (argv [i ], out_buf .buf , out_buf .len );
161
+ else
162
+ fwrite (out_buf .buf , 1 , out_buf .len , stdout );
163
+ strbuf_release (& in_buf );
164
+ strbuf_release (& out_buf );
165
+ }
238
166
} else {
167
+ struct strbuf in_buf = STRBUF_INIT ;
168
+ struct strbuf out_buf = STRBUF_INIT ;
169
+
239
170
if (opts .in_place )
240
171
die (_ ("no input file given for in-place editing" ));
241
- interpret_trailers (& opts , & trailers , NULL );
172
+
173
+ read_input_file (& in_buf , NULL );
174
+ if (trailer_process (& opts , in_buf .buf , & trailers , & out_buf ) < 0 )
175
+ die (_ ("failed to process trailers" ));
176
+ fwrite (out_buf .buf , 1 , out_buf .len , stdout );
177
+ strbuf_release (& in_buf );
178
+ strbuf_release (& out_buf );
242
179
}
243
180
244
181
new_trailers_clear (& trailers );
0 commit comments