13
13
#include "userdiff.h"
14
14
#include "streaming.h"
15
15
16
- #define BATCH 1
17
- #define BATCH_CHECK 2
18
-
19
16
static int cat_one_file (int opt , const char * exp_type , const char * obj_name )
20
17
{
21
18
unsigned char sha1 [20 ];
@@ -142,7 +139,12 @@ static void print_object_or_die(int fd, const unsigned char *sha1,
142
139
}
143
140
}
144
141
145
- static int batch_one_object (const char * obj_name , int print_contents )
142
+ struct batch_options {
143
+ int enabled ;
144
+ int print_contents ;
145
+ };
146
+
147
+ static int batch_one_object (const char * obj_name , struct batch_options * opt )
146
148
{
147
149
unsigned char sha1 [20 ];
148
150
enum object_type type = 0 ;
@@ -167,19 +169,19 @@ static int batch_one_object(const char *obj_name, int print_contents)
167
169
printf ("%s %s %lu\n" , sha1_to_hex (sha1 ), typename (type ), size );
168
170
fflush (stdout );
169
171
170
- if (print_contents == BATCH ) {
172
+ if (opt -> print_contents ) {
171
173
print_object_or_die (1 , sha1 , type , size );
172
174
write_or_die (1 , "\n" , 1 );
173
175
}
174
176
return 0 ;
175
177
}
176
178
177
- static int batch_objects (int print_contents )
179
+ static int batch_objects (struct batch_options * opt )
178
180
{
179
181
struct strbuf buf = STRBUF_INIT ;
180
182
181
183
while (strbuf_getline (& buf , stdin , '\n' ) != EOF ) {
182
- int error = batch_one_object (buf .buf , print_contents );
184
+ int error = batch_one_object (buf .buf , opt );
183
185
if (error )
184
186
return error ;
185
187
}
@@ -201,10 +203,28 @@ static int git_cat_file_config(const char *var, const char *value, void *cb)
201
203
return git_default_config (var , value , cb );
202
204
}
203
205
206
+ static int batch_option_callback (const struct option * opt ,
207
+ const char * arg ,
208
+ int unset )
209
+ {
210
+ struct batch_options * bo = opt -> value ;
211
+
212
+ if (unset ) {
213
+ memset (bo , 0 , sizeof (* bo ));
214
+ return 0 ;
215
+ }
216
+
217
+ bo -> enabled = 1 ;
218
+ bo -> print_contents = !strcmp (opt -> long_name , "batch" );
219
+
220
+ return 0 ;
221
+ }
222
+
204
223
int cmd_cat_file (int argc , const char * * argv , const char * prefix )
205
224
{
206
- int opt = 0 , batch = 0 ;
225
+ int opt = 0 ;
207
226
const char * exp_type = NULL , * obj_name = NULL ;
227
+ struct batch_options batch = {0 };
208
228
209
229
const struct option options [] = {
210
230
OPT_GROUP (N_ ("<type> can be one of: blob, tree, commit, tag" )),
@@ -215,12 +235,12 @@ int cmd_cat_file(int argc, const char **argv, const char *prefix)
215
235
OPT_SET_INT ('p' , NULL , & opt , N_ ("pretty-print object's content" ), 'p' ),
216
236
OPT_SET_INT (0 , "textconv" , & opt ,
217
237
N_ ("for blob objects, run textconv on object's content" ), 'c' ),
218
- OPT_SET_INT ( 0 , "batch" , & batch ,
219
- N_ ("show info and content of objects fed from the standard input" ),
220
- BATCH ) ,
221
- OPT_SET_INT ( 0 , "batch-check" , & batch ,
222
- N_ ("show info about objects fed from the standard input" ),
223
- BATCH_CHECK ) ,
238
+ { OPTION_CALLBACK , 0 , "batch" , & batch , NULL ,
239
+ N_ ("show info and content of objects fed from the standard input" ),
240
+ PARSE_OPT_NOARG , batch_option_callback } ,
241
+ { OPTION_CALLBACK , 0 , "batch-check" , & batch , NULL ,
242
+ N_ ("show info about objects fed from the standard input" ),
243
+ PARSE_OPT_NOARG , batch_option_callback } ,
224
244
OPT_END ()
225
245
};
226
246
@@ -237,19 +257,19 @@ int cmd_cat_file(int argc, const char **argv, const char *prefix)
237
257
else
238
258
usage_with_options (cat_file_usage , options );
239
259
}
240
- if (!opt && !batch ) {
260
+ if (!opt && !batch . enabled ) {
241
261
if (argc == 2 ) {
242
262
exp_type = argv [0 ];
243
263
obj_name = argv [1 ];
244
264
} else
245
265
usage_with_options (cat_file_usage , options );
246
266
}
247
- if (batch && (opt || argc )) {
267
+ if (batch . enabled && (opt || argc )) {
248
268
usage_with_options (cat_file_usage , options );
249
269
}
250
270
251
- if (batch )
252
- return batch_objects (batch );
271
+ if (batch . enabled )
272
+ return batch_objects (& batch );
253
273
254
274
return cat_one_file (opt , exp_type , obj_name );
255
275
}
0 commit comments