diff options
author | Jeff King <peff@peff.net> | 2013-07-10 15:38:58 +0400 |
---|---|---|
committer | Junio C Hamano <gitster@pobox.com> | 2013-07-11 21:39:13 +0400 |
commit | b71bd48017981f7257d8c0a5096825321b4fb49b (patch) | |
tree | 72a826b14104a16a346e7e9b9e59c161ddc3d040 | |
parent | 98e2092b5027fde9dbe68cb49a196ad2184c02c3 (diff) |
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>
-rw-r--r-- | builtin/cat-file.c | 56 |
1 files changed, 38 insertions, 18 deletions
diff --git a/builtin/cat-file.c b/builtin/cat-file.c index 70dd8c8a10..5254fe8956 100644 --- a/builtin/cat-file.c +++ b/builtin/cat-file.c @@ -13,9 +13,6 @@ #include "userdiff.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) { 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]; 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); fflush(stdout); - if (print_contents == BATCH) { + if (opt->print_contents) { print_object_or_die(1, sha1, type, size); write_or_die(1, "\n", 1); } return 0; } -static int batch_objects(int print_contents) +static int batch_objects(struct batch_options *opt) { struct strbuf buf = STRBUF_INIT; 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) 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); } +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 opt = 0, batch = 0; + int opt = 0; const char *exp_type = NULL, *obj_name = NULL; + struct batch_options batch = {0}; const struct option options[] = { 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(0, "textconv", &opt, N_("for blob objects, run textconv on object's content"), 'c'), - OPT_SET_INT(0, "batch", &batch, - N_("show info and content of objects fed from the standard input"), - BATCH), - OPT_SET_INT(0, "batch-check", &batch, - N_("show info about objects fed from the standard input"), - BATCH_CHECK), + { OPTION_CALLBACK, 0, "batch", &batch, NULL, + N_("show info and content of objects fed from the standard input"), + PARSE_OPT_NOARG, batch_option_callback }, + { OPTION_CALLBACK, 0, "batch-check", &batch, NULL, + N_("show info about objects fed from the standard input"), + PARSE_OPT_NOARG, batch_option_callback }, OPT_END() }; @@ -237,19 +257,19 @@ int cmd_cat_file(int argc, const char **argv, const char *prefix) else usage_with_options(cat_file_usage, options); } - if (!opt && !batch) { + if (!opt && !batch.enabled) { if (argc == 2) { exp_type = argv[0]; obj_name = argv[1]; } else usage_with_options(cat_file_usage, options); } - if (batch && (opt || argc)) { + if (batch.enabled && (opt || argc)) { usage_with_options(cat_file_usage, options); } - if (batch) - return batch_objects(batch); + if (batch.enabled) + return batch_objects(&batch); return cat_one_file(opt, exp_type, obj_name); } |