7 const char *tag_type = "tag";
9 struct object *deref_tag(struct object *o, const char *warn, int warnlen)
11 while (o && o->type == OBJ_TAG)
12 if (((struct tag *)o)->tagged)
13 o = parse_object(((struct tag *)o)->tagged->sha1);
18 warnlen = strlen(warn);
19 error("missing object referenced by '%.*s'", warnlen, warn);
24 struct tag *lookup_tag(const unsigned char *sha1)
26 struct object *obj = lookup_object(sha1);
28 return create_object(sha1, OBJ_TAG, alloc_tag_node());
31 if (obj->type != OBJ_TAG) {
32 error("Object %s is a %s, not a tag",
33 sha1_to_hex(sha1), typename(obj->type));
36 return (struct tag *) obj;
39 static unsigned long parse_tag_date(const char *buf, const char *tail)
43 while (buf < tail && *buf++ != '>')
48 while (buf < tail && *buf++ != '\n')
52 /* dateptr < buf && buf[-1] == '\n', so strtoul will stop at buf-1 */
53 return strtoul(dateptr, NULL, 10);
56 int parse_tag_buffer(struct tag *item, void *data, unsigned long size)
58 unsigned char sha1[20];
60 const char *bufptr = data;
61 const char *tail = bufptr + size;
64 if (item->object.parsed)
66 item->object.parsed = 1;
70 if (memcmp("object ", bufptr, 7) || get_sha1_hex(bufptr + 7, sha1) || bufptr[47] != '\n')
72 bufptr += 48; /* "object " + sha1 + "\n" */
74 if (prefixcmp(bufptr, "type "))
77 nl = memchr(bufptr, '\n', tail - bufptr);
78 if (!nl || sizeof(type) <= (nl - bufptr))
80 strncpy(type, bufptr, nl - bufptr);
81 type[nl - bufptr] = '\0';
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;
93 error("Unknown type %s", type);
97 if (prefixcmp(bufptr, "tag "))
100 nl = memchr(bufptr, '\n', tail - bufptr);
103 item->tag = xmemdupz(bufptr, nl - bufptr);
106 if (!prefixcmp(bufptr, "tagger "))
107 item->date = parse_tag_date(bufptr, tail);
114 int parse_tag(struct tag *item)
116 enum object_type type;
121 if (item->object.parsed)
123 data = read_sha1_file(item->object.sha1, &type, &size);
125 return error("Could not read %s",
126 sha1_to_hex(item->object.sha1));
127 if (type != OBJ_TAG) {
129 return error("Object %s not a tag",
130 sha1_to_hex(item->object.sha1));
132 ret = parse_tag_buffer(item, data, size);