]> git.ipfire.org Git - thirdparty/git.git/blame - blob.c
Merge branch 'ml/cvsserver'
[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) {
812666c8 11 struct blob *ret = xmalloc(sizeof(struct blob));
175785e5
DB
12 memset(ret, 0, sizeof(struct blob));
13 created_object(sha1, &ret->object);
14 ret->object.type = blob_type;
175785e5
DB
15 return ret;
16 }
d1af002d
NP
17 if (!obj->type)
18 obj->type = blob_type;
a510bfaa 19 if (obj->type != blob_type) {
175785e5
DB
20 error("Object %s is a %s, not a blob",
21 sha1_to_hex(sha1), obj->type);
22 return NULL;
23 }
24 return (struct blob *) obj;
25}
a510bfaa 26
bd2c39f5
NP
27int parse_blob_buffer(struct blob *item, void *buffer, unsigned long size)
28{
29 item->object.parsed = 1;
30 return 0;
31}
32
a510bfaa
DB
33int parse_blob(struct blob *item)
34{
35 char type[20];
36 void *buffer;
37 unsigned long size;
bd2c39f5
NP
38 int ret;
39
a510bfaa
DB
40 if (item->object.parsed)
41 return 0;
a510bfaa
DB
42 buffer = read_sha1_file(item->object.sha1, type, &size);
43 if (!buffer)
44 return error("Could not read %s",
45 sha1_to_hex(item->object.sha1));
46 if (strcmp(type, blob_type))
47 return error("Object %s not a blob",
48 sha1_to_hex(item->object.sha1));
bd2c39f5
NP
49 ret = parse_blob_buffer(item, buffer, size);
50 free(buffer);
51 return ret;
a510bfaa 52}