Merge branch 'jk/unused-post-2.39-part2'
[git/gitster.git] / tag.c
blob18b718cca666e1ecdd8520e6fc3e3e5ff9031df2
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 "hex.h"
10 #include "packfile.h"
12 const char *tag_type = "tag";
14 static int run_gpg_verify(const char *buf, unsigned long size, unsigned flags)
16 struct signature_check sigc;
17 struct strbuf payload = STRBUF_INIT;
18 struct strbuf signature = STRBUF_INIT;
19 int ret;
21 memset(&sigc, 0, sizeof(sigc));
23 if (!parse_signature(buf, size, &payload, &signature)) {
24 if (flags & GPG_VERIFY_VERBOSE)
25 write_in_full(1, buf, size);
26 return error("no signature found");
29 sigc.payload_type = SIGNATURE_PAYLOAD_TAG;
30 sigc.payload = strbuf_detach(&payload, &sigc.payload_len);
31 ret = check_signature(&sigc, signature.buf, signature.len);
33 if (!(flags & GPG_VERIFY_OMIT_STATUS))
34 print_signature_buffer(&sigc, flags);
36 signature_check_clear(&sigc);
37 strbuf_release(&payload);
38 strbuf_release(&signature);
39 return ret;
42 int gpg_verify_tag(const struct object_id *oid, const char *name_to_report,
43 unsigned flags)
45 enum object_type type;
46 char *buf;
47 unsigned long size;
48 int ret;
50 type = oid_object_info(the_repository, oid, NULL);
51 if (type != OBJ_TAG)
52 return error("%s: cannot verify a non-tag object of type %s.",
53 name_to_report ?
54 name_to_report :
55 find_unique_abbrev(oid, DEFAULT_ABBREV),
56 type_name(type));
58 buf = read_object_file(oid, &type, &size);
59 if (!buf)
60 return error("%s: unable to read file.",
61 name_to_report ?
62 name_to_report :
63 find_unique_abbrev(oid, DEFAULT_ABBREV));
65 ret = run_gpg_verify(buf, size, flags);
67 free(buf);
68 return ret;
71 struct object *deref_tag(struct repository *r, struct object *o, const char *warn, int warnlen)
73 struct object_id *last_oid = NULL;
74 while (o && o->type == OBJ_TAG)
75 if (((struct tag *)o)->tagged) {
76 last_oid = &((struct tag *)o)->tagged->oid;
77 o = parse_object(r, last_oid);
78 } else {
79 last_oid = NULL;
80 o = NULL;
82 if (!o && warn) {
83 if (last_oid && is_promisor_object(last_oid))
84 return NULL;
85 if (!warnlen)
86 warnlen = strlen(warn);
87 error("missing object referenced by '%.*s'", warnlen, warn);
89 return o;
92 struct object *deref_tag_noverify(struct object *o)
94 while (o && o->type == OBJ_TAG) {
95 o = parse_object(the_repository, &o->oid);
96 if (o && o->type == OBJ_TAG && ((struct tag *)o)->tagged)
97 o = ((struct tag *)o)->tagged;
98 else
99 o = NULL;
101 return o;
104 struct tag *lookup_tag(struct repository *r, const struct object_id *oid)
106 struct object *obj = lookup_object(r, oid);
107 if (!obj)
108 return create_object(r, oid, alloc_tag_node(r));
109 return object_as_type(obj, OBJ_TAG, 0);
112 static timestamp_t parse_tag_date(const char *buf, const char *tail)
114 const char *dateptr;
116 while (buf < tail && *buf++ != '>')
117 /* nada */;
118 if (buf >= tail)
119 return 0;
120 dateptr = buf;
121 while (buf < tail && *buf++ != '\n')
122 /* nada */;
123 if (buf >= tail)
124 return 0;
125 /* dateptr < buf && buf[-1] == '\n', so parsing will stop at buf-1 */
126 return parse_timestamp(dateptr, NULL, 10);
129 void release_tag_memory(struct tag *t)
131 free(t->tag);
132 t->tagged = NULL;
133 t->object.parsed = 0;
134 t->date = 0;
137 int parse_tag_buffer(struct repository *r, struct tag *item, const void *data, unsigned long size)
139 struct object_id oid;
140 char type[20];
141 const char *bufptr = data;
142 const char *tail = bufptr + size;
143 const char *nl;
145 if (item->object.parsed)
146 return 0;
148 if (item->tag) {
150 * Presumably left over from a previous failed parse;
151 * clear it out in preparation for re-parsing (we'll probably
152 * hit the same error, which lets us tell our current caller
153 * about the problem).
155 FREE_AND_NULL(item->tag);
158 if (size < the_hash_algo->hexsz + 24)
159 return -1;
160 if (memcmp("object ", bufptr, 7) || parse_oid_hex(bufptr + 7, &oid, &bufptr) || *bufptr++ != '\n')
161 return -1;
163 if (!starts_with(bufptr, "type "))
164 return -1;
165 bufptr += 5;
166 nl = memchr(bufptr, '\n', tail - bufptr);
167 if (!nl || sizeof(type) <= (nl - bufptr))
168 return -1;
169 memcpy(type, bufptr, nl - bufptr);
170 type[nl - bufptr] = '\0';
171 bufptr = nl + 1;
173 if (!strcmp(type, blob_type)) {
174 item->tagged = (struct object *)lookup_blob(r, &oid);
175 } else if (!strcmp(type, tree_type)) {
176 item->tagged = (struct object *)lookup_tree(r, &oid);
177 } else if (!strcmp(type, commit_type)) {
178 item->tagged = (struct object *)lookup_commit(r, &oid);
179 } else if (!strcmp(type, tag_type)) {
180 item->tagged = (struct object *)lookup_tag(r, &oid);
181 } else {
182 return error("unknown tag type '%s' in %s",
183 type, oid_to_hex(&item->object.oid));
186 if (!item->tagged)
187 return error("bad tag pointer to %s in %s",
188 oid_to_hex(&oid),
189 oid_to_hex(&item->object.oid));
191 if (bufptr + 4 < tail && starts_with(bufptr, "tag "))
192 ; /* good */
193 else
194 return -1;
195 bufptr += 4;
196 nl = memchr(bufptr, '\n', tail - bufptr);
197 if (!nl)
198 return -1;
199 item->tag = xmemdupz(bufptr, nl - bufptr);
200 bufptr = nl + 1;
202 if (bufptr + 7 < tail && starts_with(bufptr, "tagger "))
203 item->date = parse_tag_date(bufptr, tail);
204 else
205 item->date = 0;
207 item->object.parsed = 1;
208 return 0;
211 int parse_tag(struct tag *item)
213 enum object_type type;
214 void *data;
215 unsigned long size;
216 int ret;
218 if (item->object.parsed)
219 return 0;
220 data = read_object_file(&item->object.oid, &type, &size);
221 if (!data)
222 return error("Could not read %s",
223 oid_to_hex(&item->object.oid));
224 if (type != OBJ_TAG) {
225 free(data);
226 return error("Object %s not a tag",
227 oid_to_hex(&item->object.oid));
229 ret = parse_tag_buffer(the_repository, item, data, size);
230 free(data);
231 return ret;
234 struct object_id *get_tagged_oid(struct tag *tag)
236 if (!tag->tagged)
237 die("bad tag");
238 return &tag->tagged->oid;