Prepare 0.99.7 release candidate branch.
[git/jnareb-git/bp-gitweb.git] / tag.c
blobb1ab75ff0164b0090f43ba55d2a70239ca2fdb7a
1 #include "tag.h"
2 #include "cache.h"
4 const char *tag_type = "tag";
6 struct object *deref_tag(struct object *o)
8 while (o && o->type == tag_type)
9 o = parse_object(((struct tag *)o)->tagged->sha1);
10 return o;
13 struct tag *lookup_tag(const unsigned char *sha1)
15 struct object *obj = lookup_object(sha1);
16 if (!obj) {
17 struct tag *ret = xmalloc(sizeof(struct tag));
18 memset(ret, 0, sizeof(struct tag));
19 created_object(sha1, &ret->object);
20 ret->object.type = tag_type;
21 return ret;
23 if (!obj->type)
24 obj->type = tag_type;
25 if (obj->type != tag_type) {
26 error("Object %s is a %s, not a tree",
27 sha1_to_hex(sha1), obj->type);
28 return NULL;
30 return (struct tag *) obj;
33 int parse_tag_buffer(struct tag *item, void *data, unsigned long size)
35 int typelen, taglen;
36 unsigned char object[20];
37 const char *type_line, *tag_line, *sig_line;
38 char type[20];
40 if (item->object.parsed)
41 return 0;
42 item->object.parsed = 1;
44 if (size < 64)
45 return -1;
46 if (memcmp("object ", data, 7) || get_sha1_hex(data + 7, object))
47 return -1;
49 type_line = data + 48;
50 if (memcmp("\ntype ", type_line-1, 6))
51 return -1;
53 tag_line = strchr(type_line, '\n');
54 if (!tag_line || memcmp("tag ", ++tag_line, 4))
55 return -1;
57 sig_line = strchr(tag_line, '\n');
58 if (!sig_line)
59 return -1;
60 sig_line++;
62 typelen = tag_line - type_line - strlen("type \n");
63 if (typelen >= 20)
64 return -1;
65 memcpy(type, type_line + 5, typelen);
66 type[typelen] = '\0';
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 item->tagged = lookup_object_type(object, type);
73 if (item->tagged)
74 add_ref(&item->object, item->tagged);
76 return 0;
79 int parse_tag(struct tag *item)
81 char type[20];
82 void *data;
83 unsigned long size;
84 int ret;
86 if (item->object.parsed)
87 return 0;
88 data = read_sha1_file(item->object.sha1, type, &size);
89 if (!data)
90 return error("Could not read %s",
91 sha1_to_hex(item->object.sha1));
92 if (strcmp(type, tag_type)) {
93 free(data);
94 return error("Object %s not a tag",
95 sha1_to_hex(item->object.sha1));
97 ret = parse_tag_buffer(item, data, size);
98 free(data);
99 return ret;