Merge branch 'dh/encoding-trace-optim'
[git/gitster.git] / tag.c
blobd24170e34062f05f06f3f791f3e2a3a296dc5022
1 #define USE_THE_REPOSITORY_VARIABLE
3 #include "git-compat-util.h"
4 #include "environment.h"
5 #include "tag.h"
6 #include "object-name.h"
7 #include "object-store-ll.h"
8 #include "commit.h"
9 #include "tree.h"
10 #include "blob.h"
11 #include "alloc.h"
12 #include "gpg-interface.h"
13 #include "hex.h"
14 #include "packfile.h"
16 const char *tag_type = "tag";
18 static int run_gpg_verify(const char *buf, unsigned long size, unsigned flags)
20 struct signature_check sigc;
21 struct strbuf payload = STRBUF_INIT;
22 struct strbuf signature = STRBUF_INIT;
23 int ret;
25 memset(&sigc, 0, sizeof(sigc));
27 if (!parse_signature(buf, size, &payload, &signature)) {
28 if (flags & GPG_VERIFY_VERBOSE)
29 write_in_full(1, buf, size);
30 return error("no signature found");
33 sigc.payload_type = SIGNATURE_PAYLOAD_TAG;
34 sigc.payload = strbuf_detach(&payload, &sigc.payload_len);
35 ret = check_signature(&sigc, signature.buf, signature.len);
37 if (!(flags & GPG_VERIFY_OMIT_STATUS))
38 print_signature_buffer(&sigc, flags);
40 signature_check_clear(&sigc);
41 strbuf_release(&payload);
42 strbuf_release(&signature);
43 return ret;
46 int gpg_verify_tag(const struct object_id *oid, const char *name_to_report,
47 unsigned flags)
49 enum object_type type;
50 char *buf;
51 unsigned long size;
52 int ret;
54 type = oid_object_info(the_repository, oid, NULL);
55 if (type != OBJ_TAG)
56 return error("%s: cannot verify a non-tag object of type %s.",
57 name_to_report ?
58 name_to_report :
59 repo_find_unique_abbrev(the_repository, oid, DEFAULT_ABBREV),
60 type_name(type));
62 buf = repo_read_object_file(the_repository, oid, &type, &size);
63 if (!buf)
64 return error("%s: unable to read file.",
65 name_to_report ?
66 name_to_report :
67 repo_find_unique_abbrev(the_repository, oid, DEFAULT_ABBREV));
69 ret = run_gpg_verify(buf, size, flags);
71 free(buf);
72 return ret;
75 struct object *deref_tag(struct repository *r, struct object *o, const char *warn, int warnlen)
77 struct object_id *last_oid = NULL;
78 while (o && o->type == OBJ_TAG)
79 if (((struct tag *)o)->tagged) {
80 last_oid = &((struct tag *)o)->tagged->oid;
81 o = parse_object(r, last_oid);
82 } else {
83 last_oid = NULL;
84 o = NULL;
86 if (!o && warn) {
87 if (last_oid && is_promisor_object(last_oid))
88 return NULL;
89 if (!warnlen)
90 warnlen = strlen(warn);
91 error("missing object referenced by '%.*s'", warnlen, warn);
93 return o;
96 struct object *deref_tag_noverify(struct repository *r, struct object *o)
98 while (o && o->type == OBJ_TAG) {
99 o = parse_object(r, &o->oid);
100 if (o && o->type == OBJ_TAG && ((struct tag *)o)->tagged)
101 o = ((struct tag *)o)->tagged;
102 else
103 o = NULL;
105 return o;
108 struct tag *lookup_tag(struct repository *r, const struct object_id *oid)
110 struct object *obj = lookup_object(r, oid);
111 if (!obj)
112 return create_object(r, oid, alloc_tag_node(r));
113 return object_as_type(obj, OBJ_TAG, 0);
116 static timestamp_t parse_tag_date(const char *buf, const char *tail)
118 const char *dateptr;
120 while (buf < tail && *buf++ != '>')
121 /* nada */;
122 if (buf >= tail)
123 return 0;
124 dateptr = buf;
125 while (buf < tail && *buf++ != '\n')
126 /* nada */;
127 if (buf >= tail)
128 return 0;
129 /* dateptr < buf && buf[-1] == '\n', so parsing will stop at buf-1 */
130 return parse_timestamp(dateptr, NULL, 10);
133 void release_tag_memory(struct tag *t)
135 free(t->tag);
136 t->tagged = NULL;
137 t->object.parsed = 0;
138 t->date = 0;
141 int parse_tag_buffer(struct repository *r, struct tag *item, const void *data, unsigned long size)
143 struct object_id oid;
144 char type[20];
145 const char *bufptr = data;
146 const char *tail = bufptr + size;
147 const char *nl;
149 if (item->object.parsed)
150 return 0;
152 if (item->tag) {
154 * Presumably left over from a previous failed parse;
155 * clear it out in preparation for re-parsing (we'll probably
156 * hit the same error, which lets us tell our current caller
157 * about the problem).
159 FREE_AND_NULL(item->tag);
162 if (size < the_hash_algo->hexsz + 24)
163 return -1;
164 if (memcmp("object ", bufptr, 7) || parse_oid_hex(bufptr + 7, &oid, &bufptr) || *bufptr++ != '\n')
165 return -1;
167 if (!starts_with(bufptr, "type "))
168 return -1;
169 bufptr += 5;
170 nl = memchr(bufptr, '\n', tail - bufptr);
171 if (!nl || sizeof(type) <= (nl - bufptr))
172 return -1;
173 memcpy(type, bufptr, nl - bufptr);
174 type[nl - bufptr] = '\0';
175 bufptr = nl + 1;
177 if (!strcmp(type, blob_type)) {
178 item->tagged = (struct object *)lookup_blob(r, &oid);
179 } else if (!strcmp(type, tree_type)) {
180 item->tagged = (struct object *)lookup_tree(r, &oid);
181 } else if (!strcmp(type, commit_type)) {
182 item->tagged = (struct object *)lookup_commit(r, &oid);
183 } else if (!strcmp(type, tag_type)) {
184 item->tagged = (struct object *)lookup_tag(r, &oid);
185 } else {
186 return error("unknown tag type '%s' in %s",
187 type, oid_to_hex(&item->object.oid));
190 if (!item->tagged)
191 return error("bad tag pointer to %s in %s",
192 oid_to_hex(&oid),
193 oid_to_hex(&item->object.oid));
195 if (bufptr + 4 < tail && starts_with(bufptr, "tag "))
196 ; /* good */
197 else
198 return -1;
199 bufptr += 4;
200 nl = memchr(bufptr, '\n', tail - bufptr);
201 if (!nl)
202 return -1;
203 item->tag = xmemdupz(bufptr, nl - bufptr);
204 bufptr = nl + 1;
206 if (bufptr + 7 < tail && starts_with(bufptr, "tagger "))
207 item->date = parse_tag_date(bufptr, tail);
208 else
209 item->date = 0;
211 item->object.parsed = 1;
212 return 0;
215 int parse_tag(struct tag *item)
217 enum object_type type;
218 void *data;
219 unsigned long size;
220 int ret;
222 if (item->object.parsed)
223 return 0;
224 data = repo_read_object_file(the_repository, &item->object.oid, &type,
225 &size);
226 if (!data)
227 return error("Could not read %s",
228 oid_to_hex(&item->object.oid));
229 if (type != OBJ_TAG) {
230 free(data);
231 return error("Object %s not a tag",
232 oid_to_hex(&item->object.oid));
234 ret = parse_tag_buffer(the_repository, item, data, size);
235 free(data);
236 return ret;
239 struct object_id *get_tagged_oid(struct tag *tag)
241 if (!tag->tagged)
242 die("bad tag");
243 return &tag->tagged->oid;