fsm-listen-win32: handle shortnames
[alt-git.git] / tag.c
blobdfbcd7fcc244ac2500d41655afac3136758bb331
1 #include "cache.h"
2 #include "tag.h"
3 #include "object-store.h"
4 #include "commit.h"
5 #include "tree.h"
6 #include "blob.h"
7 #include "alloc.h"
8 #include "gpg-interface.h"
9 #include "packfile.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;
18 int ret;
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 sigc.payload_type = SIGNATURE_PAYLOAD_TAG;
29 sigc.payload = strbuf_detach(&payload, &sigc.payload_len);
30 ret = check_signature(&sigc, signature.buf, signature.len);
32 if (!(flags & GPG_VERIFY_OMIT_STATUS))
33 print_signature_buffer(&sigc, flags);
35 signature_check_clear(&sigc);
36 strbuf_release(&payload);
37 strbuf_release(&signature);
38 return ret;
41 int gpg_verify_tag(const struct object_id *oid, const char *name_to_report,
42 unsigned flags)
44 enum object_type type;
45 char *buf;
46 unsigned long size;
47 int ret;
49 type = oid_object_info(the_repository, oid, NULL);
50 if (type != OBJ_TAG)
51 return error("%s: cannot verify a non-tag object of type %s.",
52 name_to_report ?
53 name_to_report :
54 find_unique_abbrev(oid, DEFAULT_ABBREV),
55 type_name(type));
57 buf = read_object_file(oid, &type, &size);
58 if (!buf)
59 return error("%s: unable to read file.",
60 name_to_report ?
61 name_to_report :
62 find_unique_abbrev(oid, DEFAULT_ABBREV));
64 ret = run_gpg_verify(buf, size, flags);
66 free(buf);
67 return ret;
70 struct object *deref_tag(struct repository *r, struct object *o, const char *warn, int warnlen)
72 struct object_id *last_oid = NULL;
73 while (o && o->type == OBJ_TAG)
74 if (((struct tag *)o)->tagged) {
75 last_oid = &((struct tag *)o)->tagged->oid;
76 o = parse_object(r, last_oid);
77 } else {
78 last_oid = NULL;
79 o = NULL;
81 if (!o && warn) {
82 if (last_oid && is_promisor_object(last_oid))
83 return NULL;
84 if (!warnlen)
85 warnlen = strlen(warn);
86 error("missing object referenced by '%.*s'", warnlen, warn);
88 return o;
91 struct object *deref_tag_noverify(struct object *o)
93 while (o && o->type == OBJ_TAG) {
94 o = parse_object(the_repository, &o->oid);
95 if (o && o->type == OBJ_TAG && ((struct tag *)o)->tagged)
96 o = ((struct tag *)o)->tagged;
97 else
98 o = NULL;
100 return o;
103 struct tag *lookup_tag(struct repository *r, const struct object_id *oid)
105 struct object *obj = lookup_object(r, oid);
106 if (!obj)
107 return create_object(r, oid, alloc_tag_node(r));
108 return object_as_type(obj, OBJ_TAG, 0);
111 static timestamp_t parse_tag_date(const char *buf, const char *tail)
113 const char *dateptr;
115 while (buf < tail && *buf++ != '>')
116 /* nada */;
117 if (buf >= tail)
118 return 0;
119 dateptr = buf;
120 while (buf < tail && *buf++ != '\n')
121 /* nada */;
122 if (buf >= tail)
123 return 0;
124 /* dateptr < buf && buf[-1] == '\n', so parsing will stop at buf-1 */
125 return parse_timestamp(dateptr, NULL, 10);
128 void release_tag_memory(struct tag *t)
130 free(t->tag);
131 t->tagged = NULL;
132 t->object.parsed = 0;
133 t->date = 0;
136 int parse_tag_buffer(struct repository *r, struct tag *item, const void *data, unsigned long size)
138 struct object_id oid;
139 char type[20];
140 const char *bufptr = data;
141 const char *tail = bufptr + size;
142 const char *nl;
144 if (item->object.parsed)
145 return 0;
147 if (item->tag) {
149 * Presumably left over from a previous failed parse;
150 * clear it out in preparation for re-parsing (we'll probably
151 * hit the same error, which lets us tell our current caller
152 * about the problem).
154 FREE_AND_NULL(item->tag);
157 if (size < the_hash_algo->hexsz + 24)
158 return -1;
159 if (memcmp("object ", bufptr, 7) || parse_oid_hex(bufptr + 7, &oid, &bufptr) || *bufptr++ != '\n')
160 return -1;
162 if (!starts_with(bufptr, "type "))
163 return -1;
164 bufptr += 5;
165 nl = memchr(bufptr, '\n', tail - bufptr);
166 if (!nl || sizeof(type) <= (nl - bufptr))
167 return -1;
168 memcpy(type, bufptr, nl - bufptr);
169 type[nl - bufptr] = '\0';
170 bufptr = nl + 1;
172 if (!strcmp(type, blob_type)) {
173 item->tagged = (struct object *)lookup_blob(r, &oid);
174 } else if (!strcmp(type, tree_type)) {
175 item->tagged = (struct object *)lookup_tree(r, &oid);
176 } else if (!strcmp(type, commit_type)) {
177 item->tagged = (struct object *)lookup_commit(r, &oid);
178 } else if (!strcmp(type, tag_type)) {
179 item->tagged = (struct object *)lookup_tag(r, &oid);
180 } else {
181 return error("unknown tag type '%s' in %s",
182 type, oid_to_hex(&item->object.oid));
185 if (!item->tagged)
186 return error("bad tag pointer to %s in %s",
187 oid_to_hex(&oid),
188 oid_to_hex(&item->object.oid));
190 if (bufptr + 4 < tail && starts_with(bufptr, "tag "))
191 ; /* good */
192 else
193 return -1;
194 bufptr += 4;
195 nl = memchr(bufptr, '\n', tail - bufptr);
196 if (!nl)
197 return -1;
198 item->tag = xmemdupz(bufptr, nl - bufptr);
199 bufptr = nl + 1;
201 if (bufptr + 7 < tail && starts_with(bufptr, "tagger "))
202 item->date = parse_tag_date(bufptr, tail);
203 else
204 item->date = 0;
206 item->object.parsed = 1;
207 return 0;
210 int parse_tag(struct tag *item)
212 enum object_type type;
213 void *data;
214 unsigned long size;
215 int ret;
217 if (item->object.parsed)
218 return 0;
219 data = read_object_file(&item->object.oid, &type, &size);
220 if (!data)
221 return error("Could not read %s",
222 oid_to_hex(&item->object.oid));
223 if (type != OBJ_TAG) {
224 free(data);
225 return error("Object %s not a tag",
226 oid_to_hex(&item->object.oid));
228 ret = parse_tag_buffer(the_repository, item, data, size);
229 free(data);
230 return ret;
233 struct object_id *get_tagged_oid(struct tag *tag)
235 if (!tag->tagged)
236 die("bad tag");
237 return &tag->tagged->oid;