[PATCH] don't load and decompress objects twice with parse_object()
[git.git] / blob.c
1 #include "blob.h"
2 #include "cache.h"
3 #include <stdlib.h>
4
5 const char *blob_type = "blob";
6
7 struct blob *lookup_blob(unsigned char *sha1)
8 {
9         struct object *obj = lookup_object(sha1);
10         if (!obj) {
11                 struct blob *ret = xmalloc(sizeof(struct blob));
12                 memset(ret, 0, sizeof(struct blob));
13                 created_object(sha1, &ret->object);
14                 ret->object.type = blob_type;
15                 return ret;
16         }
17         if (obj->type != blob_type) {
18                 error("Object %s is a %s, not a blob", 
19                       sha1_to_hex(sha1), obj->type);
20                 return NULL;
21         }
22         return (struct blob *) obj;
23 }
24
25 int parse_blob_buffer(struct blob *item, void *buffer, unsigned long size)
26 {
27         item->object.parsed = 1;
28         return 0;
29 }
30
31 int parse_blob(struct blob *item)
32 {
33         char type[20];
34         void *buffer;
35         unsigned long size;
36         int ret;
37
38         if (item->object.parsed)
39                 return 0;
40         buffer = read_sha1_file(item->object.sha1, type, &size);
41         if (!buffer)
42                 return error("Could not read %s",
43                              sha1_to_hex(item->object.sha1));
44         if (strcmp(type, blob_type))
45                 return error("Object %s not a blob",
46                              sha1_to_hex(item->object.sha1));
47         ret = parse_blob_buffer(item, buffer, size);
48         free(buffer);
49         return ret;
50 }