Code

commit, merge: initialize static strbuf
[git.git] / builtin-cat-file.c
index df009ade7aae3ad82974a915efa16525e76ccaed..7441a56acdbefdd8044a406f4d756ce8a4f06644 100644 (file)
@@ -8,23 +8,10 @@
 #include "tag.h"
 #include "tree.h"
 #include "builtin.h"
+#include "parse-options.h"
 
-static void flush_buffer(const char *buf, unsigned long size)
-{
-       while (size > 0) {
-               long ret = xwrite(1, buf, size);
-               if (ret < 0) {
-                       /* Ignore epipe */
-                       if (errno == EPIPE)
-                               break;
-                       die("git-cat-file: %s", strerror(errno));
-               } else if (!ret) {
-                       die("git-cat-file: disk full?");
-               }
-               size -= ret;
-               buf += ret;
-       }
-}
+#define BATCH 1
+#define BATCH_CHECK 2
 
 static void pprint_tag(const unsigned char *sha1, const char *buf, unsigned long size)
 {
@@ -42,7 +29,7 @@ static void pprint_tag(const unsigned char *sha1, const char *buf, unsigned long
                        /* Found the tagger line.  Copy out the contents
                         * of the buffer so far.
                         */
-                       flush_buffer(buf, cp - buf);
+                       write_or_die(1, buf, cp - buf);
 
                        /*
                         * Do something intelligent, like pretty-printing
@@ -61,18 +48,18 @@ static void pprint_tag(const unsigned char *sha1, const char *buf, unsigned long
                                                sp++;
                                        if (sp == cp) {
                                                /* give up */
-                                               flush_buffer(tagger,
+                                               write_or_die(1, tagger,
                                                             cp - tagger);
                                                break;
                                        }
                                        while (sp < cp &&
                                               !('0' <= *sp && *sp <= '9'))
                                                sp++;
-                                       flush_buffer(tagger, sp - tagger);
+                                       write_or_die(1, tagger, sp - tagger);
                                        date = strtoul(sp, &ep, 10);
                                        tz = strtol(ep, NULL, 10);
-                                       sp = show_date(date, tz);
-                                       flush_buffer(sp, strlen(sp));
+                                       sp = show_date(date, tz, 0);
+                                       write_or_die(1, sp, strlen(sp));
                                        xwrite(1, "\n", 1);
                                        break;
                                }
@@ -90,41 +77,32 @@ static void pprint_tag(const unsigned char *sha1, const char *buf, unsigned long
         * remainder as is.
         */
        if (cp < endp)
-               flush_buffer(cp, endp - cp);
+               write_or_die(1, cp, endp - cp);
 }
 
-int cmd_cat_file(int argc, const char **argv, const char *prefix)
+static int cat_one_file(int opt, const char *exp_type, const char *obj_name)
 {
        unsigned char sha1[20];
-       char type[20];
+       enum object_type type;
        void *buf;
        unsigned long size;
-       int opt;
-
-       git_config(git_default_config);
-       if (argc != 3)
-               usage("git-cat-file [-t|-s|-e|-p|<type>] <sha1>");
-       if (get_sha1(argv[2], sha1))
-               die("Not a valid object name %s", argv[2]);
-
-       opt = 0;
-       if ( argv[1][0] == '-' ) {
-               opt = argv[1][1];
-               if ( !opt || argv[1][2] )
-                       opt = -1; /* Not a single character option */
-       }
+
+       if (get_sha1(obj_name, sha1))
+               die("Not a valid object name %s", obj_name);
 
        buf = NULL;
        switch (opt) {
        case 't':
-               if (!sha1_object_info(sha1, type, NULL)) {
-                       printf("%s\n", type);
+               type = sha1_object_info(sha1, NULL);
+               if (type > 0) {
+                       printf("%s\n", typename(type));
                        return 0;
                }
                break;
 
        case 's':
-               if (!sha1_object_info(sha1, type, &size)) {
+               type = sha1_object_info(sha1, &size);
+               if (type > 0) {
                        printf("%lu\n", size);
                        return 0;
                }
@@ -134,17 +112,20 @@ int cmd_cat_file(int argc, const char **argv, const char *prefix)
                return !has_sha1_file(sha1);
 
        case 'p':
-               if (sha1_object_info(sha1, type, NULL))
-                       die("Not a valid object name %s", argv[2]);
+               type = sha1_object_info(sha1, NULL);
+               if (type < 0)
+                       die("Not a valid object name %s", obj_name);
 
                /* custom pretty-print here */
-               if (!strcmp(type, tree_type))
-                       return cmd_ls_tree(2, argv + 1, NULL);
+               if (type == OBJ_TREE) {
+                       const char *ls_args[3] = {"ls-tree", obj_name, NULL};
+                       return cmd_ls_tree(2, ls_args, NULL);
+               }
 
-               buf = read_sha1_file(sha1, type, &size);
+               buf = read_sha1_file(sha1, &type, &size);
                if (!buf)
-                       die("Cannot read object %s", argv[2]);
-               if (!strcmp(type, tag_type)) {
+                       die("Cannot read object %s", obj_name);
+               if (type == OBJ_TAG) {
                        pprint_tag(sha1, buf, size);
                        return 0;
                }
@@ -152,16 +133,126 @@ int cmd_cat_file(int argc, const char **argv, const char *prefix)
                /* otherwise just spit out the data */
                break;
        case 0:
-               buf = read_object_with_reference(sha1, argv[1], &size, NULL);
+               buf = read_object_with_reference(sha1, exp_type, &size, NULL);
                break;
 
        default:
-               die("git-cat-file: unknown option: %s\n", argv[1]);
+               die("git-cat-file: unknown option: %s\n", exp_type);
        }
 
        if (!buf)
-               die("git-cat-file %s: bad file", argv[2]);
+               die("git-cat-file %s: bad file", obj_name);
+
+       write_or_die(1, buf, size);
+       return 0;
+}
+
+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;
+
+       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);
+               free(contents);
+       }
+
+       return 0;
+}
+
+static int batch_objects(int print_contents)
+{
+       struct strbuf buf;
+
+       strbuf_init(&buf, 0);
+       while (strbuf_getline(&buf, stdin, '\n') != EOF) {
+               int error = batch_one_object(buf.buf, print_contents);
+               if (error)
+                       return error;
+       }
 
-       flush_buffer(buf, size);
        return 0;
 }
+
+static const char * const cat_file_usage[] = {
+       "git cat-file [-t|-s|-e|-p|<type>] <sha1>",
+       "git cat-file [--batch|--batch-check] < <list_of_sha1s>",
+       NULL
+};
+
+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;
+
+       const struct option options[] = {
+               OPT_GROUP("<type> 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()
+       };
+
+       git_config(git_default_config, NULL);
+
+       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 (batch)
+               return batch_objects(batch);
+
+       return cat_one_file(opt, exp_type, obj_name);
+}