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>
This commit is contained in:
Adam Roben
2008-04-23 15:17:46 -04:00
committed by Junio C Hamano
parent 4814dbe830
commit 05d5667fec
3 changed files with 163 additions and 7 deletions

View File

@ -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);