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>
This commit is contained in:
Jeff King
2013-07-10 07:38:58 -04:00
committed by Junio C Hamano
parent 98e2092b50
commit b71bd48017

View File

@ -13,9 +13,6 @@
#include "userdiff.h" #include "userdiff.h"
#include "streaming.h" #include "streaming.h"
#define BATCH 1
#define BATCH_CHECK 2
static int cat_one_file(int opt, const char *exp_type, const char *obj_name) static int cat_one_file(int opt, const char *exp_type, const char *obj_name)
{ {
unsigned char sha1[20]; unsigned char sha1[20];
@ -142,7 +139,12 @@ static void print_object_or_die(int fd, const unsigned char *sha1,
} }
} }
static int batch_one_object(const char *obj_name, int print_contents) struct batch_options {
int enabled;
int print_contents;
};
static int batch_one_object(const char *obj_name, struct batch_options *opt)
{ {
unsigned char sha1[20]; unsigned char sha1[20];
enum object_type type = 0; enum object_type type = 0;
@ -167,19 +169,19 @@ static int batch_one_object(const char *obj_name, int print_contents)
printf("%s %s %lu\n", sha1_to_hex(sha1), typename(type), size); printf("%s %s %lu\n", sha1_to_hex(sha1), typename(type), size);
fflush(stdout); fflush(stdout);
if (print_contents == BATCH) { if (opt->print_contents) {
print_object_or_die(1, sha1, type, size); print_object_or_die(1, sha1, type, size);
write_or_die(1, "\n", 1); write_or_die(1, "\n", 1);
} }
return 0; return 0;
} }
static int batch_objects(int print_contents) static int batch_objects(struct batch_options *opt)
{ {
struct strbuf buf = STRBUF_INIT; struct strbuf buf = STRBUF_INIT;
while (strbuf_getline(&buf, stdin, '\n') != EOF) { while (strbuf_getline(&buf, stdin, '\n') != EOF) {
int error = batch_one_object(buf.buf, print_contents); int error = batch_one_object(buf.buf, opt);
if (error) if (error)
return error; return error;
} }
@ -201,10 +203,28 @@ static int git_cat_file_config(const char *var, const char *value, void *cb)
return git_default_config(var, value, cb); return git_default_config(var, value, cb);
} }
static int batch_option_callback(const struct option *opt,
const char *arg,
int unset)
{
struct batch_options *bo = opt->value;
if (unset) {
memset(bo, 0, sizeof(*bo));
return 0;
}
bo->enabled = 1;
bo->print_contents = !strcmp(opt->long_name, "batch");
return 0;
}
int cmd_cat_file(int argc, const char **argv, const char *prefix) int cmd_cat_file(int argc, const char **argv, const char *prefix)
{ {
int opt = 0, batch = 0; int opt = 0;
const char *exp_type = NULL, *obj_name = NULL; const char *exp_type = NULL, *obj_name = NULL;
struct batch_options batch = {0};
const struct option options[] = { const struct option options[] = {
OPT_GROUP(N_("<type> can be one of: blob, tree, commit, tag")), 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)
OPT_SET_INT('p', NULL, &opt, N_("pretty-print object's content"), 'p'), OPT_SET_INT('p', NULL, &opt, N_("pretty-print object's content"), 'p'),
OPT_SET_INT(0, "textconv", &opt, OPT_SET_INT(0, "textconv", &opt,
N_("for blob objects, run textconv on object's content"), 'c'), N_("for blob objects, run textconv on object's content"), 'c'),
OPT_SET_INT(0, "batch", &batch, { OPTION_CALLBACK, 0, "batch", &batch, NULL,
N_("show info and content of objects fed from the standard input"), N_("show info and content of objects fed from the standard input"),
BATCH), PARSE_OPT_NOARG, batch_option_callback },
OPT_SET_INT(0, "batch-check", &batch, { OPTION_CALLBACK, 0, "batch-check", &batch, NULL,
N_("show info about objects fed from the standard input"), N_("show info about objects fed from the standard input"),
BATCH_CHECK), PARSE_OPT_NOARG, batch_option_callback },
OPT_END() OPT_END()
}; };
@ -237,19 +257,19 @@ int cmd_cat_file(int argc, const char **argv, const char *prefix)
else else
usage_with_options(cat_file_usage, options); usage_with_options(cat_file_usage, options);
} }
if (!opt && !batch) { if (!opt && !batch.enabled) {
if (argc == 2) { if (argc == 2) {
exp_type = argv[0]; exp_type = argv[0];
obj_name = argv[1]; obj_name = argv[1];
} else } else
usage_with_options(cat_file_usage, options); usage_with_options(cat_file_usage, options);
} }
if (batch && (opt || argc)) { if (batch.enabled && (opt || argc)) {
usage_with_options(cat_file_usage, options); usage_with_options(cat_file_usage, options);
} }
if (batch) if (batch.enabled)
return batch_objects(batch); return batch_objects(&batch);
return cat_one_file(opt, exp_type, obj_name); return cat_one_file(opt, exp_type, obj_name);
} }