cat-file: refactor --batch option parsing
We currently use an int to tell us whether --batch parsing is on, and if so, whether we should print the full object contents. Let's instead factor this into a struct, filled in by callback, which will make further batch-related options easy to add. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Jeff King committed
Jul 10, 2013 at 07:38 UTC
b71bd48017981f7257d8c0a5096825321b4fb49b
1 file changed
+38
-18
builtin/cat-file.c
+38
-18
@@ -13,9 +13,6 @@
13
#include "userdiff.h"
14
#include "streaming.h"
15
16
-#define BATCH 1
17
-#define BATCH_CHECK 2
18
-
16
static int cat_one_file(int opt, const char *exp_type, const char *obj_name)
17
{
18
unsigned char sha1[20];
@@ -142,7 +139,12 @@ static void print_object_or_die(int fd, const unsigned char *sha1,
139
}
140
}
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)
148
{
149
unsigned char sha1[20];
150
enum object_type type = 0;
@@ -167,19 +169,19 @@ static int batch_one_object(const char *obj_name, int print_contents)
169
printf("%s %s %lu\n", sha1_to_hex(sha1), typename(type), size);
170
fflush(stdout);
171
170
- if (print_contents == BATCH) {
172
+ if (opt->print_contents) {
173
print_object_or_die(1, sha1, type, size);
174
write_or_die(1, "\n", 1);
175
}
176
return 0;
177
}
178
177
-static int batch_objects(int print_contents)
179
+static int batch_objects(struct batch_options *opt)
180
{
181
struct strbuf buf = STRBUF_INIT;
182
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);
185
if (error)
186
return error;
187
}
@@ -201,10 +203,28 @@ static int git_cat_file_config(const char *var, const char *value, void *cb)
203
return git_default_config(var, value, cb);
204
}
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
+
223
int cmd_cat_file(int argc, const char **argv, const char *prefix)
224
{
206
- int opt = 0, batch = 0;
225
+ int opt = 0;
226
const char *exp_type = NULL, *obj_name = NULL;
227
+ struct batch_options batch = {0};
228
229
const struct option options[] = {
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)
235
OPT_SET_INT('p', NULL, &opt, N_("pretty-print object's content"), 'p'),
236
OPT_SET_INT(0, "textconv", &opt,
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 },
244
OPT_END()
245
};
246
@@ -237,19 +257,19 @@ int cmd_cat_file(int argc, const char **argv, const char *prefix)
257
else
258
usage_with_options(cat_file_usage, options);
259
}
240
- if (!opt && !batch) {
260
+ if (!opt && !batch.enabled) {
261
if (argc == 2) {
262
exp_type = argv[0];
263
obj_name = argv[1];
264
} else
265
usage_with_options(cat_file_usage, options);
266
}
247
- if (batch && (opt || argc)) {
267
+ if (batch.enabled && (opt || argc)) {
268
usage_with_options(cat_file_usage, options);
269
}
270
251
- if (batch)
252
- return batch_objects(batch);
271
+ if (batch.enabled)
272
+ return batch_objects(&batch);
273
274
return cat_one_file(opt, exp_type, obj_name);
275
}