[PATCH] Enhance sha1_file_size() into sha1_object_info()
authorJunio C Hamano <junkio@cox.net>
Mon, 27 Jun 2005 10:34:06 +0000 (03:34 -0700)
committerLinus Torvalds <torvalds@ppc970.osdl.org>
Mon, 27 Jun 2005 22:27:51 +0000 (15:27 -0700)
This lets us eliminate one use of map_sha1_file() outside
sha1_file.c, to bring us one step closer to the packed GIT.

Signed-off-by: Junio C Hamano <junkio@cox.net>
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
cache.h
diff.c
pack-objects.c
sha1_file.c

diff --git a/cache.h b/cache.h
index 765438d..383fc86 100644 (file)
--- a/cache.h
+++ b/cache.h
@@ -162,7 +162,7 @@ extern char *sha1_file_name(const unsigned char *sha1);
 extern void * map_sha1_file(const unsigned char *sha1, unsigned long *size);
 extern int unpack_sha1_header(z_stream *stream, void *map, unsigned long mapsize, void *buffer, unsigned long size);
 extern int parse_sha1_header(char *hdr, char *type, unsigned long *sizep);
-extern int sha1_file_size(const unsigned char *, unsigned long *);
+extern int sha1_object_info(const unsigned char *, char *, unsigned long *);
 extern void * unpack_sha1_file(void *map, unsigned long mapsize, char *type, unsigned long *size);
 extern void * read_sha1_file(const unsigned char *sha1, char *type, unsigned long *size);
 extern int write_sha1_file(void *buf, unsigned long len, const char *type, unsigned char *return_sha1);
diff --git a/diff.c b/diff.c
index 5cb340c..a720290 100644 (file)
--- a/diff.c
+++ b/diff.c
@@ -425,7 +425,7 @@ int diff_populate_filespec(struct diff_filespec *s, int size_only)
                                s->size = e->size;
                                return 0;
                        }
-                       if (!sha1_file_size(s->sha1, &s->size))
+                       if (!sha1_object_info(s->sha1, type, &s->size))
                                locate_size_cache(s->sha1, 0, s->size);
                }
                else {
index 102af30..62ed265 100644 (file)
@@ -160,28 +160,22 @@ static void add_object_entry(unsigned char *sha1, unsigned int hash)
 
 static void check_object(struct object_entry *entry)
 {
-       char buffer[128];
-       char type[10];
-       unsigned long mapsize;
-       z_stream stream;
-       void *map;
-
-       map = map_sha1_file(entry->sha1, &mapsize);
-       if (!map)
-               die("unable to map %s", sha1_to_hex(entry->sha1));
-       if (unpack_sha1_header(&stream, map, mapsize, buffer, sizeof(buffer)) < 0)
-               die("unable to unpack %s header", sha1_to_hex(entry->sha1));
-       munmap(map, mapsize);
-       if (parse_sha1_header(buffer, type, &entry->size) < 0)
-               die("unable to parse %s header", sha1_to_hex(entry->sha1));
-       if (!strcmp(type, "commit")) {
-               entry->type = OBJ_COMMIT;
-       } else if (!strcmp(type, "tree")) {
-               entry->type = OBJ_TREE;
-       } else if (!strcmp(type, "blob")) {
-               entry->type = OBJ_BLOB;
-       } else
-               die("unable to pack object %s of type %s", sha1_to_hex(entry->sha1), type);
+       char type[20];
+
+       if (!sha1_object_info(entry->sha1, type, &entry->size)) {
+               if (!strcmp(type, "commit")) {
+                       entry->type = OBJ_COMMIT;
+               } else if (!strcmp(type, "tree")) {
+                       entry->type = OBJ_TREE;
+               } else if (!strcmp(type, "blob")) {
+                       entry->type = OBJ_BLOB;
+               } else
+                       die("unable to pack object %s of type %s",
+                           sha1_to_hex(entry->sha1), type);
+       }
+       else
+               die("unable to get type of object %s",
+                   sha1_to_hex(entry->sha1));
 }
 
 static void get_object_details(void)
index a12da4e..4225c67 100644 (file)
@@ -409,20 +409,22 @@ void * unpack_sha1_file(void *map, unsigned long mapsize, char *type, unsigned l
        return unpack_sha1_rest(&stream, hdr, *size);
 }
 
-int sha1_file_size(const unsigned char *sha1, unsigned long *sizep)
+int sha1_object_info(const unsigned char *sha1, char *type, unsigned long *sizep)
 {
-       int ret, status;
+       int status;
        unsigned long mapsize, size;
        void *map;
        z_stream stream;
-       char hdr[64], type[20];
+       char hdr[128];
 
        map = map_sha1_file(sha1, &mapsize);
        if (!map)
-               return -1;
-       ret = unpack_sha1_header(&stream, map, mapsize, hdr, sizeof(hdr));
-       if (ret < Z_OK || parse_sha1_header(hdr, type, &size) < 0)
-               status = -1;
+               return error("unable to map %s", sha1_to_hex(sha1));
+       if (unpack_sha1_header(&stream, map, mapsize, hdr, sizeof(hdr)) < 0)
+               status = error("unable to unpack %s header",
+                              sha1_to_hex(sha1));
+       if (parse_sha1_header(hdr, type, &size) < 0)
+               status = error("unable to parse %s header", sha1_to_hex(sha1));
        else {
                status = 0;
                *sizep = size;