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