t7600-merge: modernize test format
[alt-git.git] / tag.c
blob96dbd5b2d5501e31b7a7bb75d7630347bfdc24c0
1 #include "git-compat-util.h"
2 #include "environment.h"
3 #include "tag.h"
4 #include "object-name.h"
5 #include "object-store.h"
6 #include "commit.h"
7 #include "tree.h"
8 #include "blob.h"
9 #include "alloc.h"
10 #include "gpg-interface.h"
11 #include "hex.h"
12 #include "packfile.h"
13 #include "wrapper.h"
15 const char *tag_type = "tag";
17 static int run_gpg_verify(const char *buf, unsigned long size, unsigned flags)
19 struct signature_check sigc;
20 struct strbuf payload = STRBUF_INIT;
21 struct strbuf signature = STRBUF_INIT;
22 int ret;
24 memset(&sigc, 0, sizeof(sigc));
26 if (!parse_signature(buf, size, &payload, &signature)) {
27 if (flags & GPG_VERIFY_VERBOSE)
28 write_in_full(1, buf, size);
29 return error("no signature found");
32 sigc.payload_type = SIGNATURE_PAYLOAD_TAG;
33 sigc.payload = strbuf_detach(&payload, &sigc.payload_len);
34 ret = check_signature(&sigc, signature.buf, signature.len);
36 if (!(flags & GPG_VERIFY_OMIT_STATUS))
37 print_signature_buffer(&sigc, flags);
39 signature_check_clear(&sigc);
40 strbuf_release(&payload);
41 strbuf_release(&signature);
42 return ret;
45 int gpg_verify_tag(const struct object_id *oid, const char *name_to_report,
46 unsigned flags)
48 enum object_type type;
49 char *buf;
50 unsigned long size;
51 int ret;
53 type = oid_object_info(the_repository, oid, NULL);
54 if (type != OBJ_TAG)
55 return error("%s: cannot verify a non-tag object of type %s.",
56 name_to_report ?
57 name_to_report :
58 repo_find_unique_abbrev(the_repository, oid, DEFAULT_ABBREV),
59 type_name(type));
61 buf = repo_read_object_file(the_repository, oid, &type, &size);
62 if (!buf)
63 return error("%s: unable to read file.",
64 name_to_report ?
65 name_to_report :
66 repo_find_unique_abbrev(the_repository, oid, DEFAULT_ABBREV));
68 ret = run_gpg_verify(buf, size, flags);
70 free(buf);
71 return ret;
74 struct object *deref_tag(struct repository *r, struct object *o, const char *warn, int warnlen)
76 struct object_id *last_oid = NULL;
77 while (o && o->type == OBJ_TAG)
78 if (((struct tag *)o)->tagged) {
79 last_oid = &((struct tag *)o)->tagged->oid;
80 o = parse_object(r, last_oid);
81 } else {
82 last_oid = NULL;
83 o = NULL;
85 if (!o && warn) {
86 if (last_oid && is_promisor_object(last_oid))
87 return NULL;
88 if (!warnlen)
89 warnlen = strlen(warn);
90 error("missing object referenced by '%.*s'", warnlen, warn);
92 return o;
95 struct object *deref_tag_noverify(struct object *o)
97 while (o && o->type == OBJ_TAG) {
98 o = parse_object(the_repository, &o->oid);
99 if (o && o->type == OBJ_TAG && ((struct tag *)o)->tagged)
100 o = ((struct tag *)o)->tagged;
101 else
102 o = NULL;
104 return o;
107 struct tag *lookup_tag(struct repository *r, const struct object_id *oid)
109 struct object *obj = lookup_object(r, oid);
110 if (!obj)
111 return create_object(r, oid, alloc_tag_node(r));
112 return object_as_type(obj, OBJ_TAG, 0);
115 static timestamp_t parse_tag_date(const char *buf, const char *tail)
117 const char *dateptr;
119 while (buf < tail && *buf++ != '>')
120 /* nada */;
121 if (buf >= tail)
122 return 0;
123 dateptr = buf;
124 while (buf < tail && *buf++ != '\n')
125 /* nada */;
126 if (buf >= tail)
127 return 0;
128 /* dateptr < buf && buf[-1] == '\n', so parsing will stop at buf-1 */
129 return parse_timestamp(dateptr, NULL, 10);
132 void release_tag_memory(struct tag *t)
134 free(t->tag);
135 t->tagged = NULL;
136 t->object.parsed = 0;
137 t->date = 0;
140 int parse_tag_buffer(struct repository *r, struct tag *item, const void *data, unsigned long size)
142 struct object_id oid;
143 char type[20];
144 const char *bufptr = data;
145 const char *tail = bufptr + size;
146 const char *nl;
148 if (item->object.parsed)
149 return 0;
151 if (item->tag) {
153 * Presumably left over from a previous failed parse;
154 * clear it out in preparation for re-parsing (we'll probably
155 * hit the same error, which lets us tell our current caller
156 * about the problem).
158 FREE_AND_NULL(item->tag);
161 if (size < the_hash_algo->hexsz + 24)
162 return -1;
163 if (memcmp("object ", bufptr, 7) || parse_oid_hex(bufptr + 7, &oid, &bufptr) || *bufptr++ != '\n')
164 return -1;
166 if (!starts_with(bufptr, "type "))
167 return -1;
168 bufptr += 5;
169 nl = memchr(bufptr, '\n', tail - bufptr);
170 if (!nl || sizeof(type) <= (nl - bufptr))
171 return -1;
172 memcpy(type, bufptr, nl - bufptr);
173 type[nl - bufptr] = '\0';
174 bufptr = nl + 1;
176 if (!strcmp(type, blob_type)) {
177 item->tagged = (struct object *)lookup_blob(r, &oid);
178 } else if (!strcmp(type, tree_type)) {
179 item->tagged = (struct object *)lookup_tree(r, &oid);
180 } else if (!strcmp(type, commit_type)) {
181 item->tagged = (struct object *)lookup_commit(r, &oid);
182 } else if (!strcmp(type, tag_type)) {
183 item->tagged = (struct object *)lookup_tag(r, &oid);
184 } else {
185 return error("unknown tag type '%s' in %s",
186 type, oid_to_hex(&item->object.oid));
189 if (!item->tagged)
190 return error("bad tag pointer to %s in %s",
191 oid_to_hex(&oid),
192 oid_to_hex(&item->object.oid));
194 if (bufptr + 4 < tail && starts_with(bufptr, "tag "))
195 ; /* good */
196 else
197 return -1;
198 bufptr += 4;
199 nl = memchr(bufptr, '\n', tail - bufptr);
200 if (!nl)
201 return -1;
202 item->tag = xmemdupz(bufptr, nl - bufptr);
203 bufptr = nl + 1;
205 if (bufptr + 7 < tail && starts_with(bufptr, "tagger "))
206 item->date = parse_tag_date(bufptr, tail);
207 else
208 item->date = 0;
210 item->object.parsed = 1;
211 return 0;
214 int parse_tag(struct tag *item)
216 enum object_type type;
217 void *data;
218 unsigned long size;
219 int ret;
221 if (item->object.parsed)
222 return 0;
223 data = repo_read_object_file(the_repository, &item->object.oid, &type,
224 &size);
225 if (!data)
226 return error("Could not read %s",
227 oid_to_hex(&item->object.oid));
228 if (type != OBJ_TAG) {
229 free(data);
230 return error("Object %s not a tag",
231 oid_to_hex(&item->object.oid));
233 ret = parse_tag_buffer(the_repository, item, data, size);
234 free(data);
235 return ret;
238 struct object_id *get_tagged_oid(struct tag *tag)
240 if (!tag->tagged)
241 die("bad tag");
242 return &tag->tagged->oid;