X-Git-Url: https://asedeno.scripts.mit.edu/gitweb/?a=blobdiff_plain;f=builtin-cat-file.c;h=bd343efae7d6cc6fddef4df5c3433b97bd640d3c;hb=3c076dbe3cffee3b5cbf34759d627c47a417c145;hp=a76bb16b49d320cfe09b19e612382fb9b146553c;hpb=4814dbe830baf5a72d8abbbfcc60eeea478b47c2;p=git.git diff --git a/builtin-cat-file.c b/builtin-cat-file.c index a76bb16b4..bd343efae 100644 --- a/builtin-cat-file.c +++ b/builtin-cat-file.c @@ -8,6 +8,10 @@ #include "tag.h" #include "tree.h" #include "builtin.h" +#include "parse-options.h" + +#define BATCH 1 +#define BATCH_CHECK 2 static void pprint_tag(const unsigned char *sha1, const char *buf, unsigned long size) { @@ -143,41 +147,111 @@ 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, int print_contents) +{ + unsigned char sha1[20]; + enum object_type type = 0; + unsigned long size; + void *contents = contents; -int cmd_cat_file(int argc, const char **argv, const char *prefix) + if (!obj_name) + return 1; + + if (get_sha1(obj_name, sha1)) { + printf("%s missing\n", obj_name); + fflush(stdout); + return 0; + } + + if (print_contents == BATCH) + contents = read_sha1_file(sha1, &type, &size); + else + type = sha1_object_info(sha1, &size); + + if (type <= 0) { + printf("%s missing\n", obj_name); + fflush(stdout); + return 0; + } + + printf("%s %s %lu\n", sha1_to_hex(sha1), typename(type), size); + fflush(stdout); + + if (print_contents == BATCH) { + write_or_die(1, contents, size); + printf("\n"); + fflush(stdout); + } + + return 0; +} + +static int batch_objects(int print_contents) { - int i, opt = 0; - const char *exp_type = NULL, *obj_name = NULL; + struct strbuf buf; - git_config(git_default_config); + strbuf_init(&buf, 0); + while (strbuf_getline(&buf, stdin, '\n') != EOF) { + int error = batch_one_object(buf.buf, print_contents); + if (error) + return error; + } - for (i = 1; i < argc; ++i) { - const char *arg = argv[i]; + return 0; +} - if (!strcmp(arg, "-t") || !strcmp(arg, "-s") || !strcmp(arg, "-e") || !strcmp(arg, "-p")) { - exp_type = arg; - opt = exp_type[1]; - continue; - } +static const char * const cat_file_usage[] = { + "git-cat-file [-t|-s|-e|-p|] ", + "git-cat-file [--batch|--batch-check] < ", + NULL +}; - if (arg[0] == '-') - usage(cat_file_usage); +int cmd_cat_file(int argc, const char **argv, const char *prefix) +{ + int opt = 0, batch = 0; + const char *exp_type = NULL, *obj_name = NULL; - if (!exp_type) { - exp_type = arg; - continue; - } + const struct option options[] = { + OPT_GROUP(" can be one of: blob, tree, commit, tag"), + OPT_SET_INT('t', NULL, &opt, "show object type", 't'), + OPT_SET_INT('s', NULL, &opt, "show object size", 's'), + OPT_SET_INT('e', NULL, &opt, + "exit with zero when there's no error", 'e'), + OPT_SET_INT('p', NULL, &opt, "pretty-print object's content", 'p'), + OPT_SET_INT(0, "batch", &batch, + "show info and content of objects feeded on stdin", BATCH), + OPT_SET_INT(0, "batch-check", &batch, + "show info about objects feeded on stdin", + BATCH_CHECK), + OPT_END() + }; - if (obj_name) - usage(cat_file_usage); + git_config(git_default_config, NULL); - obj_name = arg; - break; + if (argc != 3 && argc != 2) + usage_with_options(cat_file_usage, options); + + argc = parse_options(argc, argv, options, cat_file_usage, 0); + + if (opt) { + if (argc == 1) + obj_name = argv[0]; + else + usage_with_options(cat_file_usage, options); + } + if (!opt && !batch) { + if (argc == 2) { + exp_type = argv[0]; + obj_name = argv[1]; + } else + usage_with_options(cat_file_usage, options); + } + if (batch && (opt || argc)) { + usage_with_options(cat_file_usage, options); } - if (!exp_type || !obj_name) - usage(cat_file_usage); + if (batch) + return batch_objects(batch); return cat_one_file(opt, exp_type, obj_name); }