From 05d5667fec9650b049f47edd8cca23a43b135365 Mon Sep 17 00:00:00 2001 From: Adam Roben Date: Wed, 23 Apr 2008 15:17:46 -0400 Subject: git-cat-file: Add --batch-check option This new option allows multiple objects to be specified on stdin. For each object specified, a line of the following form is printed: SP SP LF If the object does not exist in the repository, a line of the following form is printed: SP missing LF Signed-off-by: Adam Roben Signed-off-by: Junio C Hamano --- builtin-cat-file.c | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 72 insertions(+), 2 deletions(-) (limited to 'builtin-cat-file.c') diff --git a/builtin-cat-file.c b/builtin-cat-file.c index a76bb16b4..832cfd184 100644 --- a/builtin-cat-file.c +++ b/builtin-cat-file.c @@ -143,11 +143,48 @@ static int cat_one_file(int opt, const char *exp_type, const char *obj_name) return 0; } -static const char cat_file_usage[] = "git-cat-file [-t|-s|-e|-p|] "; +static int batch_one_object(const char *obj_name) +{ + unsigned char sha1[20]; + enum object_type type; + unsigned long size; + + if (!obj_name) + return 1; + + if (get_sha1(obj_name, sha1)) { + printf("%s missing\n", obj_name); + return 0; + } + + type = sha1_object_info(sha1, &size); + if (type <= 0) + return 1; + + printf("%s %s %lu\n", sha1_to_hex(sha1), typename(type), size); + + return 0; +} + +static int batch_objects(void) +{ + struct strbuf buf; + + strbuf_init(&buf, 0); + while (strbuf_getline(&buf, stdin, '\n') != EOF) { + int error = batch_one_object(buf.buf); + if (error) + return error; + } + + return 0; +} + +static const char cat_file_usage[] = "git-cat-file [ [-t|-s|-e|-p|] | --batch-check < ]"; int cmd_cat_file(int argc, const char **argv, const char *prefix) { - int i, opt = 0; + int i, opt = 0, batch_check = 0; const char *exp_type = NULL, *obj_name = NULL; git_config(git_default_config); @@ -155,7 +192,28 @@ int cmd_cat_file(int argc, const char **argv, const char *prefix) for (i = 1; i < argc; ++i) { const char *arg = argv[i]; + if (!strcmp(arg, "--batch-check")) { + if (opt) { + error("git-cat-file: Can't use --batch-check with -%c", opt); + usage(cat_file_usage); + } else if (exp_type) { + error("git-cat-file: Can't use --batch-check when a type (\"%s\") is specified", exp_type); + usage(cat_file_usage); + } else if (obj_name) { + error("git-cat-file: Can't use --batch-check when an object (\"%s\") is specified", obj_name); + usage(cat_file_usage); + } + + batch_check = 1; + continue; + } + if (!strcmp(arg, "-t") || !strcmp(arg, "-s") || !strcmp(arg, "-e") || !strcmp(arg, "-p")) { + if (batch_check) { + error("git-cat-file: Can't use %s with --batch-check", arg); + usage(cat_file_usage); + } + exp_type = arg; opt = exp_type[1]; continue; @@ -165,6 +223,11 @@ int cmd_cat_file(int argc, const char **argv, const char *prefix) usage(cat_file_usage); if (!exp_type) { + if (batch_check) { + error("git-cat-file: Can't specify a type (\"%s\") with --batch-check", arg); + usage(cat_file_usage); + } + exp_type = arg; continue; } @@ -172,10 +235,17 @@ int cmd_cat_file(int argc, const char **argv, const char *prefix) if (obj_name) usage(cat_file_usage); + // We should have hit one of the earlier if (batch_check) cases before + // getting here. + assert(!batch_check); + obj_name = arg; break; } + if (batch_check) + return batch_objects(); + if (!exp_type || !obj_name) usage(cat_file_usage); -- cgit v1.2.1