Skip to content

Commit b71bd48

Browse files
peffgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 98e2092 commit b71bd48

File tree

1 file changed

+38
-18
lines changed

1 file changed

+38
-18
lines changed

builtin/cat-file.c

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@
1313
#include "userdiff.h"
1414
#include "streaming.h"
1515

16-
#define BATCH 1
17-
#define BATCH_CHECK 2
18-
1916
static int cat_one_file(int opt, const char *exp_type, const char *obj_name)
2017
{
2118
unsigned char sha1[20];
@@ -142,7 +139,12 @@ static void print_object_or_die(int fd, const unsigned char *sha1,
142139
}
143140
}
144141

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)
146148
{
147149
unsigned char sha1[20];
148150
enum object_type type = 0;
@@ -167,19 +169,19 @@ static int batch_one_object(const char *obj_name, int print_contents)
167169
printf("%s %s %lu\n", sha1_to_hex(sha1), typename(type), size);
168170
fflush(stdout);
169171

170-
if (print_contents == BATCH) {
172+
if (opt->print_contents) {
171173
print_object_or_die(1, sha1, type, size);
172174
write_or_die(1, "\n", 1);
173175
}
174176
return 0;
175177
}
176178

177-
static int batch_objects(int print_contents)
179+
static int batch_objects(struct batch_options *opt)
178180
{
179181
struct strbuf buf = STRBUF_INIT;
180182

181183
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);
183185
if (error)
184186
return error;
185187
}
@@ -201,10 +203,28 @@ static int git_cat_file_config(const char *var, const char *value, void *cb)
201203
return git_default_config(var, value, cb);
202204
}
203205

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+
204223
int cmd_cat_file(int argc, const char **argv, const char *prefix)
205224
{
206-
int opt = 0, batch = 0;
225+
int opt = 0;
207226
const char *exp_type = NULL, *obj_name = NULL;
227+
struct batch_options batch = {0};
208228

209229
const struct option options[] = {
210230
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)
215235
OPT_SET_INT('p', NULL, &opt, N_("pretty-print object's content"), 'p'),
216236
OPT_SET_INT(0, "textconv", &opt,
217237
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 },
224244
OPT_END()
225245
};
226246

@@ -237,19 +257,19 @@ int cmd_cat_file(int argc, const char **argv, const char *prefix)
237257
else
238258
usage_with_options(cat_file_usage, options);
239259
}
240-
if (!opt && !batch) {
260+
if (!opt && !batch.enabled) {
241261
if (argc == 2) {
242262
exp_type = argv[0];
243263
obj_name = argv[1];
244264
} else
245265
usage_with_options(cat_file_usage, options);
246266
}
247-
if (batch && (opt || argc)) {
267+
if (batch.enabled && (opt || argc)) {
248268
usage_with_options(cat_file_usage, options);
249269
}
250270

251-
if (batch)
252-
return batch_objects(batch);
271+
if (batch.enabled)
272+
return batch_objects(&batch);
253273

254274
return cat_one_file(opt, exp_type, obj_name);
255275
}

0 commit comments

Comments
 (0)