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 | { |
0ab17950 | 41 | unsigned char sha1[20]; |
89e4202f | 42 | char type[20]; |
28de5b6b SP |
43 | const char *bufptr = data; |
44 | const char *tail = bufptr + size; | |
45 | const char *nl; | |
ae200ee5 | 46 | |
2e0052a5 SP |
47 | if (item->object.parsed) |
48 | return 0; | |
49 | item->object.parsed = 1; | |
2636f614 | 50 | |
2636f614 | 51 | if (size < 64) |
bd2c39f5 | 52 | return -1; |
28de5b6b | 53 | if (memcmp("object ", bufptr, 7) || get_sha1_hex(bufptr + 7, sha1) || bufptr[47] != '\n') |
bd2c39f5 | 54 | return -1; |
28de5b6b | 55 | bufptr += 48; /* "object " + sha1 + "\n" */ |
2636f614 | 56 | |
28de5b6b | 57 | if (prefixcmp(bufptr, "type ")) |
bd2c39f5 | 58 | return -1; |
28de5b6b SP |
59 | bufptr += 5; |
60 | nl = memchr(bufptr, '\n', tail - bufptr); | |
61 | if (!nl || sizeof(type) <= (nl - bufptr)) | |
bd2c39f5 | 62 | return -1; |
28de5b6b SP |
63 | strncpy(type, bufptr, nl - bufptr); |
64 | type[nl - bufptr] = '\0'; | |
65 | bufptr = nl + 1; | |
2636f614 | 66 | |
0ab17950 NP |
67 | if (!strcmp(type, blob_type)) { |
68 | item->tagged = &lookup_blob(sha1)->object; | |
69 | } else if (!strcmp(type, tree_type)) { | |
70 | item->tagged = &lookup_tree(sha1)->object; | |
71 | } else if (!strcmp(type, commit_type)) { | |
72 | item->tagged = &lookup_commit(sha1)->object; | |
73 | } else if (!strcmp(type, tag_type)) { | |
74 | item->tagged = &lookup_tag(sha1)->object; | |
75 | } else { | |
76 | error("Unknown type %s", type); | |
77 | item->tagged = NULL; | |
78 | } | |
79 | ||
28de5b6b SP |
80 | if (prefixcmp(bufptr, "tag ")) |
81 | return -1; | |
82 | bufptr += 4; | |
83 | nl = memchr(bufptr, '\n', tail - bufptr); | |
84 | if (!nl) | |
85 | return -1; | |
86 | item->tag = xmemdupz(bufptr, nl - bufptr); | |
87 | bufptr = nl + 1; | |
88 | ||
2636f614 | 89 | return 0; |
bd2c39f5 | 90 | } |
13019d41 | 91 | |
bd2c39f5 NP |
92 | int parse_tag(struct tag *item) |
93 | { | |
21666f1a | 94 | enum object_type type; |
bd2c39f5 NP |
95 | void *data; |
96 | unsigned long size; | |
97 | int ret; | |
98 | ||
99 | if (item->object.parsed) | |
100 | return 0; | |
21666f1a | 101 | data = read_sha1_file(item->object.sha1, &type, &size); |
bd2c39f5 NP |
102 | if (!data) |
103 | return error("Could not read %s", | |
104 | sha1_to_hex(item->object.sha1)); | |
21666f1a | 105 | if (type != OBJ_TAG) { |
bd2c39f5 NP |
106 | free(data); |
107 | return error("Object %s not a tag", | |
108 | sha1_to_hex(item->object.sha1)); | |
109 | } | |
110 | ret = parse_tag_buffer(item, data, size); | |
13019d41 | 111 | free(data); |
bd2c39f5 | 112 | return ret; |
2636f614 | 113 | } |