]>
Commit | Line | Data |
---|---|---|
2636f614 DB |
1 | #include "tag.h" |
2 | #include "cache.h" | |
3 | ||
4 | const char *tag_type = "tag"; | |
5 | ||
37fde874 JH |
6 | struct object *deref_tag(struct object *o) |
7 | { | |
8 | while (o && o->type == tag_type) | |
9 | o = parse_object(((struct tag *)o)->tagged->sha1); | |
10 | return o; | |
11 | } | |
12 | ||
5d6ccf5c | 13 | struct tag *lookup_tag(const unsigned char *sha1) |
2636f614 DB |
14 | { |
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; | |
22 | } | |
d1af002d NP |
23 | if (!obj->type) |
24 | obj->type = tag_type; | |
2636f614 DB |
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; | |
29 | } | |
30 | return (struct tag *) obj; | |
31 | } | |
32 | ||
bd2c39f5 | 33 | int parse_tag_buffer(struct tag *item, void *data, unsigned long size) |
2636f614 | 34 | { |
ae200ee5 ET |
35 | int typelen, taglen; |
36 | unsigned char object[20]; | |
37 | const char *type_line, *tag_line, *sig_line; | |
89e4202f | 38 | char type[20]; |
ae200ee5 | 39 | |
2636f614 DB |
40 | if (item->object.parsed) |
41 | return 0; | |
42 | item->object.parsed = 1; | |
2636f614 | 43 | |
2636f614 | 44 | if (size < 64) |
bd2c39f5 | 45 | return -1; |
2636f614 | 46 | if (memcmp("object ", data, 7) || get_sha1_hex(data + 7, object)) |
bd2c39f5 | 47 | return -1; |
2636f614 | 48 | |
2636f614 DB |
49 | type_line = data + 48; |
50 | if (memcmp("\ntype ", type_line-1, 6)) | |
bd2c39f5 | 51 | return -1; |
2636f614 DB |
52 | |
53 | tag_line = strchr(type_line, '\n'); | |
54 | if (!tag_line || memcmp("tag ", ++tag_line, 4)) | |
bd2c39f5 | 55 | return -1; |
2636f614 DB |
56 | |
57 | sig_line = strchr(tag_line, '\n'); | |
58 | if (!sig_line) | |
bd2c39f5 | 59 | return -1; |
2636f614 DB |
60 | sig_line++; |
61 | ||
62 | typelen = tag_line - type_line - strlen("type \n"); | |
63 | if (typelen >= 20) | |
bd2c39f5 | 64 | return -1; |
89e4202f DB |
65 | memcpy(type, type_line + 5, typelen); |
66 | type[typelen] = '\0'; | |
2636f614 DB |
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'; | |
71 | ||
89e4202f DB |
72 | item->tagged = lookup_object_type(object, type); |
73 | if (item->tagged) | |
74 | add_ref(&item->object, item->tagged); | |
75 | ||
2636f614 | 76 | return 0; |
bd2c39f5 | 77 | } |
13019d41 | 78 | |
bd2c39f5 NP |
79 | int parse_tag(struct tag *item) |
80 | { | |
81 | char type[20]; | |
82 | void *data; | |
83 | unsigned long size; | |
84 | int ret; | |
85 | ||
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)); | |
96 | } | |
97 | ret = parse_tag_buffer(item, data, size); | |
13019d41 | 98 | free(data); |
bd2c39f5 | 99 | return ret; |
2636f614 | 100 | } |