cat-file: teach --batch to stream blob objects
[gitweb.git] / builtin / cat-file.c
index 40f87b4649aa97ca1f33a02f09f5938ce5f41f74..70dd8c8a101e04a409d25b535a4968a7935ef81d 100644 (file)
 #define BATCH 1
 #define BATCH_CHECK 2
 
-static void pprint_tag(const unsigned char *sha1, const char *buf, unsigned long size)
-{
-       /* the parser in tag.c is useless here. */
-       const char *endp = buf + size;
-       const char *cp = buf;
-
-       while (cp < endp) {
-               char c = *cp++;
-               if (c != '\n')
-                       continue;
-               if (7 <= endp - cp && !memcmp("tagger ", cp, 7)) {
-                       const char *tagger = cp;
-
-                       /* Found the tagger line.  Copy out the contents
-                        * of the buffer so far.
-                        */
-                       write_or_die(1, buf, cp - buf);
-
-                       /*
-                        * Do something intelligent, like pretty-printing
-                        * the date.
-                        */
-                       while (cp < endp) {
-                               if (*cp++ == '\n') {
-                                       /* tagger to cp is a line
-                                        * that has ident and time.
-                                        */
-                                       const char *sp = tagger;
-                                       char *ep;
-                                       unsigned long date;
-                                       long tz;
-                                       while (sp < cp && *sp != '>')
-                                               sp++;
-                                       if (sp == cp) {
-                                               /* give up */
-                                               write_or_die(1, tagger,
-                                                            cp - tagger);
-                                               break;
-                                       }
-                                       while (sp < cp &&
-                                              !('0' <= *sp && *sp <= '9'))
-                                               sp++;
-                                       write_or_die(1, tagger, sp - tagger);
-                                       date = strtoul(sp, &ep, 10);
-                                       tz = strtol(ep, NULL, 10);
-                                       sp = show_date(date, tz, 0);
-                                       write_or_die(1, sp, strlen(sp));
-                                       xwrite(1, "\n", 1);
-                                       break;
-                               }
-                       }
-                       break;
-               }
-               if (cp < endp && *cp == '\n')
-                       /* end of header */
-                       break;
-       }
-       /* At this point, we have copied out the header up to the end of
-        * the tagger line and cp points at one past \n.  It could be the
-        * next header line after the tagger line, or it could be another
-        * \n that marks the end of the headers.  We need to copy out the
-        * remainder as is.
-        */
-       if (cp < endp)
-               write_or_die(1, cp, endp - cp);
-}
-
 static int cat_one_file(int opt, const char *exp_type, const char *obj_name)
 {
        unsigned char sha1[20];
@@ -133,10 +66,6 @@ static int cat_one_file(int opt, const char *exp_type, const char *obj_name)
                buf = read_sha1_file(sha1, &type, &size);
                if (!buf)
                        die("Cannot read object %s", obj_name);
-               if (type == OBJ_TAG) {
-                       pprint_tag(sha1, buf, size);
-                       return 0;
-               }
 
                /* otherwise just spit out the data */
                break;
@@ -188,12 +117,36 @@ static int cat_one_file(int opt, const char *exp_type, const char *obj_name)
        return 0;
 }
 
+static void print_object_or_die(int fd, const unsigned char *sha1,
+                               enum object_type type, unsigned long size)
+{
+       if (type == OBJ_BLOB) {
+               if (stream_blob_to_fd(fd, sha1, NULL, 0) < 0)
+                       die("unable to stream %s to stdout", sha1_to_hex(sha1));
+       }
+       else {
+               enum object_type rtype;
+               unsigned long rsize;
+               void *contents;
+
+               contents = read_sha1_file(sha1, &rtype, &rsize);
+               if (!contents)
+                       die("object %s disappeared", sha1_to_hex(sha1));
+               if (rtype != type)
+                       die("object %s changed type!?", sha1_to_hex(sha1));
+               if (rsize != size)
+                       die("object %s change size!?", sha1_to_hex(sha1));
+
+               write_or_die(fd, contents, size);
+               free(contents);
+       }
+}
+
 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 = NULL;
 
        if (!obj_name)
           return 1;
@@ -204,16 +157,10 @@ static int batch_one_object(const char *obj_name, int print_contents)
                return 0;
        }
 
-       if (print_contents == BATCH)
-               contents = read_sha1_file(sha1, &type, &size);
-       else
-               type = sha1_object_info(sha1, &size);
-
+       type = sha1_object_info(sha1, &size);
        if (type <= 0) {
                printf("%s missing\n", obj_name);
                fflush(stdout);
-               if (print_contents == BATCH)
-                       free(contents);
                return 0;
        }
 
@@ -221,12 +168,9 @@ static int batch_one_object(const char *obj_name, int print_contents)
        fflush(stdout);
 
        if (print_contents == BATCH) {
-               write_or_die(1, contents, size);
-               printf("\n");
-               fflush(stdout);
-               free(contents);
+               print_object_or_die(1, sha1, type, size);
+               write_or_die(1, "\n", 1);
        }
-
        return 0;
 }