7 #define PGP_SIGNATURE "-----BEGIN PGP SIGNATURE-----"
9 const char *tag_type = "tag";
11 struct object *deref_tag(struct object *o, const char *warn, int warnlen)
13 while (o && o->type == OBJ_TAG)
14 if (((struct tag *)o)->tagged)
15 o = parse_object(((struct tag *)o)->tagged->sha1);
20 warnlen = strlen(warn);
21 error("missing object referenced by '%.*s'", warnlen, warn);
26 struct tag *lookup_tag(const unsigned char *sha1)
28 struct object *obj = lookup_object(sha1);
30 return create_object(sha1, OBJ_TAG, alloc_tag_node());
33 if (obj->type != OBJ_TAG) {
34 error("Object %s is a %s, not a tag",
35 sha1_to_hex(sha1), typename(obj->type));
38 return (struct tag *) obj;
41 static unsigned long parse_tag_date(const char *buf, const char *tail)
45 while (buf < tail && *buf++ != '>')
50 while (buf < tail && *buf++ != '\n')
54 /* dateptr < buf && buf[-1] == '\n', so strtoul will stop at buf-1 */
55 return strtoul(dateptr, NULL, 10);
58 int parse_tag_buffer(struct tag *item, void *data, unsigned long size)
60 unsigned char sha1[20];
62 const char *bufptr = data;
63 const char *tail = bufptr + size;
66 if (item->object.parsed)
68 item->object.parsed = 1;
72 if (memcmp("object ", bufptr, 7) || get_sha1_hex(bufptr + 7, sha1) || bufptr[47] != '\n')
74 bufptr += 48; /* "object " + sha1 + "\n" */
76 if (prefixcmp(bufptr, "type "))
79 nl = memchr(bufptr, '\n', tail - bufptr);
80 if (!nl || sizeof(type) <= (nl - bufptr))
82 strncpy(type, bufptr, nl - bufptr);
83 type[nl - bufptr] = '\0';
86 if (!strcmp(type, blob_type)) {
87 item->tagged = &lookup_blob(sha1)->object;
88 } else if (!strcmp(type, tree_type)) {
89 item->tagged = &lookup_tree(sha1)->object;
90 } else if (!strcmp(type, commit_type)) {
91 item->tagged = &lookup_commit(sha1)->object;
92 } else if (!strcmp(type, tag_type)) {
93 item->tagged = &lookup_tag(sha1)->object;
95 error("Unknown type %s", type);
99 if (prefixcmp(bufptr, "tag "))
102 nl = memchr(bufptr, '\n', tail - bufptr);
105 item->tag = xmemdupz(bufptr, nl - bufptr);
108 if (!prefixcmp(bufptr, "tagger "))
109 item->date = parse_tag_date(bufptr, tail);
116 int parse_tag(struct tag *item)
118 enum object_type type;
123 if (item->object.parsed)
125 data = read_sha1_file(item->object.sha1, &type, &size);
127 return error("Could not read %s",
128 sha1_to_hex(item->object.sha1));
129 if (type != OBJ_TAG) {
131 return error("Object %s not a tag",
132 sha1_to_hex(item->object.sha1));
134 ret = parse_tag_buffer(item, data, size);
139 size_t parse_signature(const char *buf, unsigned long size)
143 while (len < size && prefixcmp(buf + len, PGP_SIGNATURE)) {
144 eol = memchr(buf + len, '\n', size - len);
145 len += eol ? eol - (buf + len) + 1 : size - len;