]> git.ipfire.org Git - thirdparty/git.git/blame - blob.c
Add specialized object allocator
[thirdparty/git.git] / blob.c
CommitLineData
175785e5 1#include "cache.h"
8f1d2e6f 2#include "blob.h"
175785e5
DB
3#include <stdlib.h>
4
5const char *blob_type = "blob";
6
5d6ccf5c 7struct blob *lookup_blob(const unsigned char *sha1)
175785e5
DB
8{
9 struct object *obj = lookup_object(sha1);
10 if (!obj) {
855419f7 11 struct blob *ret = alloc_blob_node();
175785e5 12 created_object(sha1, &ret->object);
885a86ab 13 ret->object.type = TYPE_BLOB;
175785e5
DB
14 return ret;
15 }
d1af002d 16 if (!obj->type)
885a86ab
LT
17 obj->type = TYPE_BLOB;
18 if (obj->type != TYPE_BLOB) {
19 error("Object %s is a %s, not a blob",
20 sha1_to_hex(sha1), typename(obj->type));
175785e5
DB
21 return NULL;
22 }
23 return (struct blob *) obj;
24}
a510bfaa 25
bd2c39f5
NP
26int parse_blob_buffer(struct blob *item, void *buffer, unsigned long size)
27{
28 item->object.parsed = 1;
29 return 0;
30}
31
a510bfaa
DB
32int parse_blob(struct blob *item)
33{
34 char type[20];
35 void *buffer;
36 unsigned long size;
bd2c39f5
NP
37 int ret;
38
a510bfaa
DB
39 if (item->object.parsed)
40 return 0;
a510bfaa
DB
41 buffer = read_sha1_file(item->object.sha1, type, &size);
42 if (!buffer)
43 return error("Could not read %s",
44 sha1_to_hex(item->object.sha1));
45 if (strcmp(type, blob_type))
46 return error("Object %s not a blob",
47 sha1_to_hex(item->object.sha1));
bd2c39f5
NP
48 ret = parse_blob_buffer(item, buffer, size);
49 free(buffer);
50 return ret;
a510bfaa 51}