3 #include "object-store.h"
8 #include "gpg-interface.h"
11 const char *tag_type = "tag";
13 static int run_gpg_verify(const char *buf, unsigned long size, unsigned flags)
15 struct signature_check sigc;
16 struct strbuf payload = STRBUF_INIT;
17 struct strbuf signature = STRBUF_INIT;
20 memset(&sigc, 0, sizeof(sigc));
22 if (!parse_signature(buf, size, &payload, &signature)) {
23 if (flags & GPG_VERIFY_VERBOSE)
24 write_in_full(1, buf, size);
25 return error("no signature found");
28 ret = check_signature(payload.buf, payload.len, signature.buf,
29 signature.len, &sigc);
31 if (!(flags & GPG_VERIFY_OMIT_STATUS))
32 print_signature_buffer(&sigc, flags);
34 signature_check_clear(&sigc);
35 strbuf_release(&payload);
36 strbuf_release(&signature);
40 int gpg_verify_tag(const struct object_id *oid, const char *name_to_report,
43 enum object_type type;
48 type = oid_object_info(the_repository, oid, NULL);
50 return error("%s: cannot verify a non-tag object of type %s.",
53 find_unique_abbrev(oid, DEFAULT_ABBREV),
56 buf = read_object_file(oid, &type, &size);
58 return error("%s: unable to read file.",
61 find_unique_abbrev(oid, DEFAULT_ABBREV));
63 ret = run_gpg_verify(buf, size, flags);
69 struct object *deref_tag(struct repository *r, struct object *o, const char *warn, int warnlen)
71 struct object_id *last_oid = NULL;
72 while (o && o->type == OBJ_TAG)
73 if (((struct tag *)o)->tagged) {
74 last_oid = &((struct tag *)o)->tagged->oid;
75 o = parse_object(r, last_oid);
81 if (last_oid && is_promisor_object(last_oid))
84 warnlen = strlen(warn);
85 error("missing object referenced by '%.*s'", warnlen, warn);
90 struct object *deref_tag_noverify(struct object *o)
92 while (o && o->type == OBJ_TAG) {
93 o = parse_object(the_repository, &o->oid);
94 if (o && o->type == OBJ_TAG && ((struct tag *)o)->tagged)
95 o = ((struct tag *)o)->tagged;
102 struct tag *lookup_tag(struct repository *r, const struct object_id *oid)
104 struct object *obj = lookup_object(r, oid);
106 return create_object(r, oid, alloc_tag_node(r));
107 return object_as_type(obj, OBJ_TAG, 0);
110 static timestamp_t parse_tag_date(const char *buf, const char *tail)
114 while (buf < tail && *buf++ != '>')
119 while (buf < tail && *buf++ != '\n')
123 /* dateptr < buf && buf[-1] == '\n', so parsing will stop at buf-1 */
124 return parse_timestamp(dateptr, NULL, 10);
127 void release_tag_memory(struct tag *t)
131 t->object.parsed = 0;
135 int parse_tag_buffer(struct repository *r, struct tag *item, const void *data, unsigned long size)
137 struct object_id oid;
139 const char *bufptr = data;
140 const char *tail = bufptr + size;
143 if (item->object.parsed)
148 * Presumably left over from a previous failed parse;
149 * clear it out in preparation for re-parsing (we'll probably
150 * hit the same error, which lets us tell our current caller
151 * about the problem).
153 FREE_AND_NULL(item->tag);
156 if (size < the_hash_algo->hexsz + 24)
158 if (memcmp("object ", bufptr, 7) || parse_oid_hex(bufptr + 7, &oid, &bufptr) || *bufptr++ != '\n')
161 if (!starts_with(bufptr, "type "))
164 nl = memchr(bufptr, '\n', tail - bufptr);
165 if (!nl || sizeof(type) <= (nl - bufptr))
167 memcpy(type, bufptr, nl - bufptr);
168 type[nl - bufptr] = '\0';
171 if (!strcmp(type, blob_type)) {
172 item->tagged = (struct object *)lookup_blob(r, &oid);
173 } else if (!strcmp(type, tree_type)) {
174 item->tagged = (struct object *)lookup_tree(r, &oid);
175 } else if (!strcmp(type, commit_type)) {
176 item->tagged = (struct object *)lookup_commit(r, &oid);
177 } else if (!strcmp(type, tag_type)) {
178 item->tagged = (struct object *)lookup_tag(r, &oid);
180 return error("unknown tag type '%s' in %s",
181 type, oid_to_hex(&item->object.oid));
185 return error("bad tag pointer to %s in %s",
187 oid_to_hex(&item->object.oid));
189 if (bufptr + 4 < tail && starts_with(bufptr, "tag "))
194 nl = memchr(bufptr, '\n', tail - bufptr);
197 item->tag = xmemdupz(bufptr, nl - bufptr);
200 if (bufptr + 7 < tail && starts_with(bufptr, "tagger "))
201 item->date = parse_tag_date(bufptr, tail);
205 item->object.parsed = 1;
209 int parse_tag(struct tag *item)
211 enum object_type type;
216 if (item->object.parsed)
218 data = read_object_file(&item->object.oid, &type, &size);
220 return error("Could not read %s",
221 oid_to_hex(&item->object.oid));
222 if (type != OBJ_TAG) {
224 return error("Object %s not a tag",
225 oid_to_hex(&item->object.oid));
227 ret = parse_tag_buffer(the_repository, item, data, size);
232 struct object_id *get_tagged_oid(struct tag *tag)
236 return &tag->tagged->oid;