summaryrefslogtreecommitdiff
path: root/builtin-cat-file.c
diff options
context:
space:
mode:
authorAdam Roben <aroben@apple.com>2008-04-23 15:17:46 -0400
committerJunio C Hamano <gitster@pobox.com>2008-05-05 21:17:32 -0700
commit05d5667fec9650b049f47edd8cca23a43b135365 (patch)
tree1d7e42cd7e960d31bca1e73d91105b473f51c205 /builtin-cat-file.c
parent4814dbe830baf5a72d8abbbfcc60eeea478b47c2 (diff)
downloadgit-05d5667fec9650b049f47edd8cca23a43b135365.tar.gz
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: <sha1> SP <type> SP <size> LF If the object does not exist in the repository, a line of the following form is printed: <object> SP missing LF Signed-off-by: Adam Roben <aroben@apple.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'builtin-cat-file.c')
-rw-r--r--builtin-cat-file.c74
1 files changed, 72 insertions, 2 deletions
diff --git a/builtin-cat-file.c b/builtin-cat-file.c
index a76bb16b49..832cfd184a 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|<type>] <sha1>";
+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|<type>] <sha1> | --batch-check < <list_of_sha1s> ]";
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);