7 #define PGP_SIGNATURE "-----BEGIN PGP SIGNATURE-----"
8 #define PGP_MESSAGE "-----BEGIN PGP MESSAGE-----"
10 const char *tag_type = "tag";
12 struct object *deref_tag(struct object *o, const char *warn, int warnlen)
14 while (o && o->type == OBJ_TAG)
15 if (((struct tag *)o)->tagged)
16 o = parse_object(((struct tag *)o)->tagged->sha1);
21 warnlen = strlen(warn);
22 error("missing object referenced by '%.*s'", warnlen, warn);
27 struct object *deref_tag_noverify(struct object *o)
29 while (o && o->type == OBJ_TAG) {
30 o = parse_object(o->sha1);
31 if (o && o->type == OBJ_TAG && ((struct tag *)o)->tagged)
32 o = ((struct tag *)o)->tagged;
39 struct tag *lookup_tag(const unsigned char *sha1)
41 struct object *obj = lookup_object(sha1);
43 return create_object(sha1, alloc_tag_node());
44 return object_as_type(obj, OBJ_TAG, 0);
47 static unsigned long parse_tag_date(const char *buf, const char *tail)
51 while (buf < tail && *buf++ != '>')
56 while (buf < tail && *buf++ != '\n')
60 /* dateptr < buf && buf[-1] == '\n', so strtoul will stop at buf-1 */
61 return strtoul(dateptr, NULL, 10);
64 int parse_tag_buffer(struct tag *item, const void *data, unsigned long size)
66 unsigned char sha1[20];
68 const char *bufptr = data;
69 const char *tail = bufptr + size;
72 if (item->object.parsed)
74 item->object.parsed = 1;
78 if (memcmp("object ", bufptr, 7) || get_sha1_hex(bufptr + 7, sha1) || bufptr[47] != '\n')
80 bufptr += 48; /* "object " + sha1 + "\n" */
82 if (!starts_with(bufptr, "type "))
85 nl = memchr(bufptr, '\n', tail - bufptr);
86 if (!nl || sizeof(type) <= (nl - bufptr))
88 strncpy(type, bufptr, nl - bufptr);
89 type[nl - bufptr] = '\0';
92 if (!strcmp(type, blob_type)) {
93 item->tagged = &lookup_blob(sha1)->object;
94 } else if (!strcmp(type, tree_type)) {
95 item->tagged = &lookup_tree(sha1)->object;
96 } else if (!strcmp(type, commit_type)) {
97 item->tagged = &lookup_commit(sha1)->object;
98 } else if (!strcmp(type, tag_type)) {
99 item->tagged = &lookup_tag(sha1)->object;
101 error("Unknown type %s", type);
105 if (bufptr + 4 < tail && starts_with(bufptr, "tag "))
110 nl = memchr(bufptr, '\n', tail - bufptr);
113 item->tag = xmemdupz(bufptr, nl - bufptr);
116 if (bufptr + 7 < tail && starts_with(bufptr, "tagger "))
117 item->date = parse_tag_date(bufptr, tail);
124 int parse_tag(struct tag *item)
126 enum object_type type;
131 if (item->object.parsed)
133 data = read_sha1_file(item->object.sha1, &type, &size);
135 return error("Could not read %s",
136 sha1_to_hex(item->object.sha1));
137 if (type != OBJ_TAG) {
139 return error("Object %s not a tag",
140 sha1_to_hex(item->object.sha1));
142 ret = parse_tag_buffer(item, data, size);
148 * Look at a signed tag object, and return the offset where
149 * the embedded detached signature begins, or the end of the
150 * data when there is no such signature.
152 size_t parse_signature(const char *buf, unsigned long size)
156 while (len < size && !starts_with(buf + len, PGP_SIGNATURE) &&
157 !starts_with(buf + len, PGP_MESSAGE)) {
158 eol = memchr(buf + len, '\n', size - len);
159 len += eol ? eol - (buf + len) + 1 : size - len;