Clean up object creation to use more common code
[git/git.git] / blob.c
CommitLineData
175785e5 1#include "cache.h"
8f1d2e6f 2#include "blob.h"
175785e5
DB
3
4const char *blob_type = "blob";
5
5d6ccf5c 6struct blob *lookup_blob(const unsigned char *sha1)
175785e5
DB
7{
8 struct object *obj = lookup_object(sha1);
100c5f3b
LT
9 if (!obj)
10 return create_object(sha1, OBJ_BLOB, alloc_blob_node());
d1af002d 11 if (!obj->type)
1974632c
LT
12 obj->type = OBJ_BLOB;
13 if (obj->type != OBJ_BLOB) {
885a86ab
LT
14 error("Object %s is a %s, not a blob",
15 sha1_to_hex(sha1), typename(obj->type));
175785e5
DB
16 return NULL;
17 }
18 return (struct blob *) obj;
19}
a510bfaa 20
bd2c39f5
NP
21int parse_blob_buffer(struct blob *item, void *buffer, unsigned long size)
22{
23 item->object.parsed = 1;
24 return 0;
25}
26
a510bfaa
DB
27int parse_blob(struct blob *item)
28{
21666f1a 29 enum object_type type;
a510bfaa
DB
30 void *buffer;
31 unsigned long size;
bd2c39f5
NP
32 int ret;
33
a510bfaa
DB
34 if (item->object.parsed)
35 return 0;
21666f1a 36 buffer = read_sha1_file(item->object.sha1, &type, &size);
a510bfaa
DB
37 if (!buffer)
38 return error("Could not read %s",
39 sha1_to_hex(item->object.sha1));
21666f1a 40 if (type != OBJ_BLOB)
a510bfaa
DB
41 return error("Object %s not a blob",
42 sha1_to_hex(item->object.sha1));
bd2c39f5
NP
43 ret = parse_blob_buffer(item, buffer, size);
44 free(buffer);
45 return ret;
a510bfaa 46}