convert object type handling from a string to a number
[git/git.git] / tag.c
CommitLineData
2636f614 1#include "cache.h"
8f1d2e6f 2#include "tag.h"
2636f614
DB
3
4const char *tag_type = "tag";
5
9534f40b 6struct object *deref_tag(struct object *o, const char *warn, int warnlen)
37fde874 7{
1974632c 8 while (o && o->type == OBJ_TAG)
37fde874 9 o = parse_object(((struct tag *)o)->tagged->sha1);
9534f40b
JH
10 if (!o && warn) {
11 if (!warnlen)
12 warnlen = strlen(warn);
13 error("missing object referenced by '%.*s'", warnlen, warn);
14 }
37fde874
JH
15 return o;
16}
17
5d6ccf5c 18struct tag *lookup_tag(const unsigned char *sha1)
2636f614
DB
19{
20 struct object *obj = lookup_object(sha1);
21 if (!obj) {
855419f7 22 struct tag *ret = alloc_tag_node();
2636f614 23 created_object(sha1, &ret->object);
1974632c 24 ret->object.type = OBJ_TAG;
2636f614
DB
25 return ret;
26 }
d1af002d 27 if (!obj->type)
1974632c
LT
28 obj->type = OBJ_TAG;
29 if (obj->type != OBJ_TAG) {
885a86ab
LT
30 error("Object %s is a %s, not a tree",
31 sha1_to_hex(sha1), typename(obj->type));
2636f614
DB
32 return NULL;
33 }
34 return (struct tag *) obj;
35}
36
bd2c39f5 37int parse_tag_buffer(struct tag *item, void *data, unsigned long size)
2636f614 38{
ae200ee5
ET
39 int typelen, taglen;
40 unsigned char object[20];
41 const char *type_line, *tag_line, *sig_line;
89e4202f 42 char type[20];
ae200ee5 43
2636f614
DB
44 if (item->object.parsed)
45 return 0;
46 item->object.parsed = 1;
2636f614 47
2636f614 48 if (size < 64)
bd2c39f5 49 return -1;
1d7f171c 50 if (memcmp("object ", data, 7) || get_sha1_hex((char *) data + 7, object))
bd2c39f5 51 return -1;
2636f614 52
1d7f171c 53 type_line = (char *) data + 48;
2636f614 54 if (memcmp("\ntype ", type_line-1, 6))
bd2c39f5 55 return -1;
2636f614
DB
56
57 tag_line = strchr(type_line, '\n');
58 if (!tag_line || memcmp("tag ", ++tag_line, 4))
bd2c39f5 59 return -1;
2636f614
DB
60
61 sig_line = strchr(tag_line, '\n');
62 if (!sig_line)
bd2c39f5 63 return -1;
2636f614
DB
64 sig_line++;
65
66 typelen = tag_line - type_line - strlen("type \n");
67 if (typelen >= 20)
bd2c39f5 68 return -1;
89e4202f
DB
69 memcpy(type, type_line + 5, typelen);
70 type[typelen] = '\0';
2636f614
DB
71 taglen = sig_line - tag_line - strlen("tag \n");
72 item->tag = xmalloc(taglen + 1);
73 memcpy(item->tag, tag_line + 4, taglen);
74 item->tag[taglen] = '\0';
75
89e4202f 76 item->tagged = lookup_object_type(object, type);
27dedf0c
JH
77 if (item->tagged && track_object_refs) {
78 struct object_refs *refs = alloc_object_refs(1);
79 refs->ref[0] = item->tagged;
80 set_object_refs(&item->object, refs);
81 }
89e4202f 82
2636f614 83 return 0;
bd2c39f5 84}
13019d41 85
bd2c39f5
NP
86int parse_tag(struct tag *item)
87{
21666f1a 88 enum object_type type;
bd2c39f5
NP
89 void *data;
90 unsigned long size;
91 int ret;
92
93 if (item->object.parsed)
94 return 0;
21666f1a 95 data = read_sha1_file(item->object.sha1, &type, &size);
bd2c39f5
NP
96 if (!data)
97 return error("Could not read %s",
98 sha1_to_hex(item->object.sha1));
21666f1a 99 if (type != OBJ_TAG) {
bd2c39f5
NP
100 free(data);
101 return error("Object %s not a tag",
102 sha1_to_hex(item->object.sha1));
103 }
104 ret = parse_tag_buffer(item, data, size);
13019d41 105 free(data);
bd2c39f5 106 return ret;
2636f614 107}