]>
Commit | Line | Data |
---|---|---|
2636f614 | 1 | #include "cache.h" |
8f1d2e6f | 2 | #include "tag.h" |
0ab17950 NP |
3 | #include "commit.h" |
4 | #include "tree.h" | |
5 | #include "blob.h" | |
2636f614 DB |
6 | |
7 | const char *tag_type = "tag"; | |
8 | ||
9534f40b | 9 | struct object *deref_tag(struct object *o, const char *warn, int warnlen) |
37fde874 | 10 | { |
1974632c | 11 | while (o && o->type == OBJ_TAG) |
24e8a3c9 MK |
12 | if (((struct tag *)o)->tagged) |
13 | o = parse_object(((struct tag *)o)->tagged->sha1); | |
14 | else | |
15 | o = NULL; | |
9534f40b JH |
16 | if (!o && warn) { |
17 | if (!warnlen) | |
18 | warnlen = strlen(warn); | |
19 | error("missing object referenced by '%.*s'", warnlen, warn); | |
20 | } | |
37fde874 JH |
21 | return o; |
22 | } | |
23 | ||
5d6ccf5c | 24 | struct tag *lookup_tag(const unsigned char *sha1) |
2636f614 | 25 | { |
100c5f3b LT |
26 | struct object *obj = lookup_object(sha1); |
27 | if (!obj) | |
28 | return create_object(sha1, OBJ_TAG, alloc_tag_node()); | |
d1af002d | 29 | if (!obj->type) |
1974632c LT |
30 | obj->type = OBJ_TAG; |
31 | if (obj->type != OBJ_TAG) { | |
eb09626b | 32 | error("Object %s is a %s, not a tag", |
885a86ab | 33 | sha1_to_hex(sha1), typename(obj->type)); |
2636f614 DB |
34 | return NULL; |
35 | } | |
36 | return (struct tag *) obj; | |
37 | } | |
38 | ||
bd2c39f5 | 39 | int parse_tag_buffer(struct tag *item, void *data, unsigned long size) |
2636f614 | 40 | { |
ae200ee5 | 41 | int typelen, taglen; |
0ab17950 | 42 | unsigned char sha1[20]; |
ae200ee5 | 43 | const char *type_line, *tag_line, *sig_line; |
89e4202f | 44 | char type[20]; |
a0393ef6 | 45 | const char *start = data; |
ae200ee5 | 46 | |
2636f614 DB |
47 | if (item->object.parsed) |
48 | return 0; | |
49 | item->object.parsed = 1; | |
2636f614 | 50 | |
2636f614 | 51 | if (size < 64) |
bd2c39f5 | 52 | return -1; |
0ab17950 | 53 | if (memcmp("object ", data, 7) || get_sha1_hex((char *) data + 7, sha1)) |
bd2c39f5 | 54 | return -1; |
2636f614 | 55 | |
1d7f171c | 56 | type_line = (char *) data + 48; |
2636f614 | 57 | if (memcmp("\ntype ", type_line-1, 6)) |
bd2c39f5 | 58 | return -1; |
2636f614 | 59 | |
a0393ef6 | 60 | tag_line = memchr(type_line, '\n', size - (type_line - start)); |
2636f614 | 61 | if (!tag_line || memcmp("tag ", ++tag_line, 4)) |
bd2c39f5 | 62 | return -1; |
2636f614 | 63 | |
a0393ef6 | 64 | sig_line = memchr(tag_line, '\n', size - (tag_line - start)); |
2636f614 | 65 | if (!sig_line) |
bd2c39f5 | 66 | return -1; |
2636f614 DB |
67 | sig_line++; |
68 | ||
69 | typelen = tag_line - type_line - strlen("type \n"); | |
70 | if (typelen >= 20) | |
bd2c39f5 | 71 | return -1; |
89e4202f DB |
72 | memcpy(type, type_line + 5, typelen); |
73 | type[typelen] = '\0'; | |
2636f614 | 74 | taglen = sig_line - tag_line - strlen("tag \n"); |
182af834 | 75 | item->tag = xmemdupz(tag_line + 4, taglen); |
2636f614 | 76 | |
0ab17950 NP |
77 | if (!strcmp(type, blob_type)) { |
78 | item->tagged = &lookup_blob(sha1)->object; | |
79 | } else if (!strcmp(type, tree_type)) { | |
80 | item->tagged = &lookup_tree(sha1)->object; | |
81 | } else if (!strcmp(type, commit_type)) { | |
82 | item->tagged = &lookup_commit(sha1)->object; | |
83 | } else if (!strcmp(type, tag_type)) { | |
84 | item->tagged = &lookup_tag(sha1)->object; | |
85 | } else { | |
86 | error("Unknown type %s", type); | |
87 | item->tagged = NULL; | |
88 | } | |
89 | ||
2636f614 | 90 | return 0; |
bd2c39f5 | 91 | } |
13019d41 | 92 | |
bd2c39f5 NP |
93 | int parse_tag(struct tag *item) |
94 | { | |
21666f1a | 95 | enum object_type type; |
bd2c39f5 NP |
96 | void *data; |
97 | unsigned long size; | |
98 | int ret; | |
99 | ||
100 | if (item->object.parsed) | |
101 | return 0; | |
21666f1a | 102 | data = read_sha1_file(item->object.sha1, &type, &size); |
bd2c39f5 NP |
103 | if (!data) |
104 | return error("Could not read %s", | |
105 | sha1_to_hex(item->object.sha1)); | |
21666f1a | 106 | if (type != OBJ_TAG) { |
bd2c39f5 NP |
107 | free(data); |
108 | return error("Object %s not a tag", | |
109 | sha1_to_hex(item->object.sha1)); | |
110 | } | |
111 | ret = parse_tag_buffer(item, data, size); | |
13019d41 | 112 | free(data); |
bd2c39f5 | 113 | return ret; |
2636f614 | 114 | } |