[PATCH] Add function to parse an object of unspecified type (take 2)
[git/jnareb-git/bp-gitweb.git] / tag.c
blobf079d83a9de5c3d6d98b2a29339ec1ef3a25f333
1 #include "tag.h"
2 #include "cache.h"
4 const char *tag_type = "tag";
6 struct tag *lookup_tag(unsigned char *sha1)
8 struct object *obj = lookup_object(sha1);
9 if (!obj) {
10 struct tag *ret = xmalloc(sizeof(struct tag));
11 memset(ret, 0, sizeof(struct tag));
12 created_object(sha1, &ret->object);
13 ret->object.type = tag_type;
14 return ret;
16 if (obj->type != tag_type) {
17 error("Object %s is a %s, not a tree",
18 sha1_to_hex(sha1), obj->type);
19 return NULL;
21 return (struct tag *) obj;
24 int parse_tag(struct tag *item)
26 char type[20];
27 void *data, *bufptr;
28 unsigned long size;
29 if (item->object.parsed)
30 return 0;
31 item->object.parsed = 1;
32 data = bufptr = read_sha1_file(item->object.sha1, type, &size);
33 if (!data)
34 return error("Could not read %s",
35 sha1_to_hex(item->object.sha1));
36 if (strcmp(type, tag_type))
37 return error("Object %s not a tag",
38 sha1_to_hex(item->object.sha1));
40 int typelen, taglen;
41 unsigned char object[20];
42 const char *type_line, *tag_line, *sig_line;
44 if (size < 64)
45 return -1;
46 if (memcmp("object ", data, 7) || get_sha1_hex(data + 7, object))
47 return -1;
49 item->tagged = parse_object(object);
51 type_line = data + 48;
52 if (memcmp("\ntype ", type_line-1, 6))
53 return -1;
55 tag_line = strchr(type_line, '\n');
56 if (!tag_line || memcmp("tag ", ++tag_line, 4))
57 return -1;
59 sig_line = strchr(tag_line, '\n');
60 if (!sig_line)
61 return -1;
62 sig_line++;
64 typelen = tag_line - type_line - strlen("type \n");
65 if (typelen >= 20)
66 return -1;
67 taglen = sig_line - tag_line - strlen("tag \n");
68 item->tag = xmalloc(taglen + 1);
69 memcpy(item->tag, tag_line + 4, taglen);
70 item->tag[taglen] = '\0';
72 return 0;