🌐 AI搜索 & 代理 主页
blob: fc3834db467dc4e83ca0a24da61a3f39048f3ec2 [file] [log] [blame]
Elijah Newrend812c3b2023-04-11 07:41:561#include "git-compat-util.h"
Elijah Newren32a8f512023-03-21 06:26:032#include "environment.h"
Junio C Hamano8f1d2e62006-01-07 09:33:543#include "tag.h"
Elijah Newrendabab1d2023-04-11 07:41:494#include "object-name.h"
Elijah Newrena034e912023-05-16 06:34:065#include "object-store-ll.h"
Nicolas Pitre0ab17952007-02-26 19:56:006#include "commit.h"
7#include "tree.h"
8#include "blob.h"
Stefan Beller14ba97f2018-05-15 21:48:429#include "alloc.h"
Lukas Puehringer94240b92017-01-17 23:37:1810#include "gpg-interface.h"
Elijah Newren41771fa2023-02-24 00:09:2711#include "hex.h"
Jonathan Tan8c4cc322018-07-13 00:03:0712#include "packfile.h"
Daniel Barkalow2636f612005-04-28 14:46:3313
14const char *tag_type = "tag";
15
Santiago Torres45a227e2016-04-22 14:52:0416static int run_gpg_verify(const char *buf, unsigned long size, unsigned flags)
17{
18 struct signature_check sigc;
brian m. carlson482c1192021-02-11 02:08:0319 struct strbuf payload = STRBUF_INIT;
20 struct strbuf signature = STRBUF_INIT;
Santiago Torres45a227e2016-04-22 14:52:0421 int ret;
22
23 memset(&sigc, 0, sizeof(sigc));
24
brian m. carlson482c1192021-02-11 02:08:0325 if (!parse_signature(buf, size, &payload, &signature)) {
Santiago Torres45a227e2016-04-22 14:52:0426 if (flags & GPG_VERIFY_VERBOSE)
brian m. carlson482c1192021-02-11 02:08:0327 write_in_full(1, buf, size);
Santiago Torres45a227e2016-04-22 14:52:0428 return error("no signature found");
29 }
30
Fabian Stelzerdd3aa412021-12-09 08:52:4731 sigc.payload_type = SIGNATURE_PAYLOAD_TAG;
Fabian Stelzer02769432021-12-09 08:52:4332 sigc.payload = strbuf_detach(&payload, &sigc.payload_len);
33 ret = check_signature(&sigc, signature.buf, signature.len);
Lukas Puehringer94240b92017-01-17 23:37:1834
35 if (!(flags & GPG_VERIFY_OMIT_STATUS))
36 print_signature_buffer(&sigc, flags);
Santiago Torres45a227e2016-04-22 14:52:0437
38 signature_check_clear(&sigc);
brian m. carlson482c1192021-02-11 02:08:0339 strbuf_release(&payload);
40 strbuf_release(&signature);
Santiago Torres45a227e2016-04-22 14:52:0441 return ret;
42}
43
Stefan Beller84571762017-07-13 00:44:1544int gpg_verify_tag(const struct object_id *oid, const char *name_to_report,
Santiago Torres45a227e2016-04-22 14:52:0445 unsigned flags)
46{
47 enum object_type type;
48 char *buf;
49 unsigned long size;
50 int ret;
51
Stefan Beller0df8e962018-04-25 18:20:5952 type = oid_object_info(the_repository, oid, NULL);
Santiago Torres45a227e2016-04-22 14:52:0453 if (type != OBJ_TAG)
54 return error("%s: cannot verify a non-tag object of type %s.",
55 name_to_report ?
56 name_to_report :
Ævar Arnfjörð Bjarmasond850b7a2023-03-28 13:58:4657 repo_find_unique_abbrev(the_repository, oid, DEFAULT_ABBREV),
Brandon Williamsdebca9d2018-02-14 18:59:2458 type_name(type));
Santiago Torres45a227e2016-04-22 14:52:0459
Ævar Arnfjörð Bjarmasonbc726bd2023-03-28 13:58:5060 buf = repo_read_object_file(the_repository, oid, &type, &size);
Santiago Torres45a227e2016-04-22 14:52:0461 if (!buf)
62 return error("%s: unable to read file.",
63 name_to_report ?
64 name_to_report :
Ævar Arnfjörð Bjarmasond850b7a2023-03-28 13:58:4665 repo_find_unique_abbrev(the_repository, oid, DEFAULT_ABBREV));
Santiago Torres45a227e2016-04-22 14:52:0466
67 ret = run_gpg_verify(buf, size, flags);
68
69 free(buf);
70 return ret;
71}
72
Stefan Beller286d2582018-06-29 01:22:2073struct object *deref_tag(struct repository *r, struct object *o, const char *warn, int warnlen)
Junio C Hamano37fde872005-08-05 07:47:5674{
Jonathan Tan8c4cc322018-07-13 00:03:0775 struct object_id *last_oid = NULL;
Linus Torvalds19746322006-07-12 03:45:3176 while (o && o->type == OBJ_TAG)
Jonathan Tan8c4cc322018-07-13 00:03:0777 if (((struct tag *)o)->tagged) {
78 last_oid = &((struct tag *)o)->tagged->oid;
Junio C Hamano09ca6132018-08-02 22:30:4679 o = parse_object(r, last_oid);
Jonathan Tan8c4cc322018-07-13 00:03:0780 } else {
81 last_oid = NULL;
Martin Koegler24e8a3c2008-02-18 07:31:5582 o = NULL;
Jonathan Tan8c4cc322018-07-13 00:03:0783 }
Junio C Hamano9534f402005-11-02 23:19:1384 if (!o && warn) {
Jonathan Tan8c4cc322018-07-13 00:03:0785 if (last_oid && is_promisor_object(last_oid))
86 return NULL;
Junio C Hamano9534f402005-11-02 23:19:1387 if (!warnlen)
88 warnlen = strlen(warn);
89 error("missing object referenced by '%.*s'", warnlen, warn);
90 }
Junio C Hamano37fde872005-08-05 07:47:5691 return o;
92}
93
Jeff King90108a22012-01-06 19:18:0194struct object *deref_tag_noverify(struct object *o)
95{
96 while (o && o->type == OBJ_TAG) {
Stefan Beller109cd762018-06-29 01:21:5197 o = parse_object(the_repository, &o->oid);
Jeff King90108a22012-01-06 19:18:0198 if (o && o->type == OBJ_TAG && ((struct tag *)o)->tagged)
99 o = ((struct tag *)o)->tagged;
100 else
101 o = NULL;
102 }
103 return o;
104}
105
Stefan Beller8bde69b2018-06-29 01:22:11106struct tag *lookup_tag(struct repository *r, const struct object_id *oid)
Daniel Barkalow2636f612005-04-28 14:46:33107{
Jeff Kingd0229ab2019-06-20 07:41:14108 struct object *obj = lookup_object(r, oid);
Linus Torvalds100c5f32007-04-17 05:11:43109 if (!obj)
Jeff Kinga3785092019-06-20 07:41:21110 return create_object(r, oid, alloc_tag_node(r));
Abhishek Kumar6da43d92020-06-17 09:14:08111 return object_as_type(obj, OBJ_TAG, 0);
Daniel Barkalow2636f612005-04-28 14:46:33112}
113
Johannes Schindelindddbad72017-04-26 19:29:31114static timestamp_t parse_tag_date(const char *buf, const char *tail)
Shawn O. Pearcee451d062010-04-12 23:25:28115{
116 const char *dateptr;
117
118 while (buf < tail && *buf++ != '>')
119 /* nada */;
120 if (buf >= tail)
121 return 0;
122 dateptr = buf;
123 while (buf < tail && *buf++ != '\n')
124 /* nada */;
125 if (buf >= tail)
126 return 0;
Johannes Schindelin1aeb7e72017-04-21 10:45:44127 /* dateptr < buf && buf[-1] == '\n', so parsing will stop at buf-1 */
128 return parse_timestamp(dateptr, NULL, 10);
Shawn O. Pearcee451d062010-04-12 23:25:28129}
130
Stefan Beller14ba97f2018-05-15 21:48:42131void release_tag_memory(struct tag *t)
132{
133 free(t->tag);
134 t->tagged = NULL;
135 t->object.parsed = 0;
136 t->date = 0;
137}
138
Stefan Beller84f80cd2018-06-29 01:22:12139int parse_tag_buffer(struct repository *r, struct tag *item, const void *data, unsigned long size)
Daniel Barkalow2636f612005-04-28 14:46:33140{
brian m. carlson1e4085a2017-05-06 22:10:02141 struct object_id oid;
Daniel Barkalow89e42022005-06-22 00:35:10142 char type[20];
Shawn O. Pearce28de5b62010-04-12 23:25:27143 const char *bufptr = data;
144 const char *tail = bufptr + size;
145 const char *nl;
Edgar Toernigae200ee2005-04-30 16:51:03146
Shawn O. Pearce2e0052a2010-04-12 23:25:25147 if (item->object.parsed)
148 return 0;
Jeff King228c78f2019-10-25 21:20:20149
150 if (item->tag) {
151 /*
152 * Presumably left over from a previous failed parse;
153 * clear it out in preparation for re-parsing (we'll probably
154 * hit the same error, which lets us tell our current caller
155 * about the problem).
156 */
157 FREE_AND_NULL(item->tag);
158 }
Daniel Barkalow2636f612005-04-28 14:46:33159
brian m. carlsond8a3a692018-10-15 00:01:58160 if (size < the_hash_algo->hexsz + 24)
Nicolas Pitrebd2c39f2005-05-06 17:48:34161 return -1;
brian m. carlson1e4085a2017-05-06 22:10:02162 if (memcmp("object ", bufptr, 7) || parse_oid_hex(bufptr + 7, &oid, &bufptr) || *bufptr++ != '\n')
Nicolas Pitrebd2c39f2005-05-06 17:48:34163 return -1;
Daniel Barkalow2636f612005-04-28 14:46:33164
Christian Couder59556542013-11-30 20:55:40165 if (!starts_with(bufptr, "type "))
Nicolas Pitrebd2c39f2005-05-06 17:48:34166 return -1;
Shawn O. Pearce28de5b62010-04-12 23:25:27167 bufptr += 5;
168 nl = memchr(bufptr, '\n', tail - bufptr);
169 if (!nl || sizeof(type) <= (nl - bufptr))
Nicolas Pitrebd2c39f2005-05-06 17:48:34170 return -1;
Jeff Kingeddda372015-09-24 21:08:26171 memcpy(type, bufptr, nl - bufptr);
Shawn O. Pearce28de5b62010-04-12 23:25:27172 type[nl - bufptr] = '\0';
173 bufptr = nl + 1;
Daniel Barkalow2636f612005-04-28 14:46:33174
Nicolas Pitre0ab17952007-02-26 19:56:00175 if (!strcmp(type, blob_type)) {
Stefan Beller84f80cd2018-06-29 01:22:12176 item->tagged = (struct object *)lookup_blob(r, &oid);
Nicolas Pitre0ab17952007-02-26 19:56:00177 } else if (!strcmp(type, tree_type)) {
Stefan Beller84f80cd2018-06-29 01:22:12178 item->tagged = (struct object *)lookup_tree(r, &oid);
Nicolas Pitre0ab17952007-02-26 19:56:00179 } else if (!strcmp(type, commit_type)) {
Stefan Beller84f80cd2018-06-29 01:22:12180 item->tagged = (struct object *)lookup_commit(r, &oid);
Nicolas Pitre0ab17952007-02-26 19:56:00181 } else if (!strcmp(type, tag_type)) {
Stefan Beller84f80cd2018-06-29 01:22:12182 item->tagged = (struct object *)lookup_tag(r, &oid);
Nicolas Pitre0ab17952007-02-26 19:56:00183 } else {
Jeff King78d50142019-10-18 04:45:35184 return error("unknown tag type '%s' in %s",
185 type, oid_to_hex(&item->object.oid));
Nicolas Pitre0ab17952007-02-26 19:56:00186 }
187
Jeff King78d50142019-10-18 04:45:35188 if (!item->tagged)
189 return error("bad tag pointer to %s in %s",
190 oid_to_hex(&oid),
191 oid_to_hex(&item->object.oid));
192
Christian Couder59556542013-11-30 20:55:40193 if (bufptr + 4 < tail && starts_with(bufptr, "tag "))
Nguyễn Thái Ngọc Duy85594252011-02-14 13:02:51194 ; /* good */
195 else
Shawn O. Pearce28de5b62010-04-12 23:25:27196 return -1;
197 bufptr += 4;
198 nl = memchr(bufptr, '\n', tail - bufptr);
199 if (!nl)
200 return -1;
201 item->tag = xmemdupz(bufptr, nl - bufptr);
202 bufptr = nl + 1;
203
Christian Couder59556542013-11-30 20:55:40204 if (bufptr + 7 < tail && starts_with(bufptr, "tagger "))
Shawn O. Pearcee451d062010-04-12 23:25:28205 item->date = parse_tag_date(bufptr, tail);
206 else
207 item->date = 0;
208
Jeff King228c78f2019-10-25 21:20:20209 item->object.parsed = 1;
Daniel Barkalow2636f612005-04-28 14:46:33210 return 0;
Nicolas Pitrebd2c39f2005-05-06 17:48:34211}
Sergey Vlasov13019d42005-05-04 17:44:15212
Nicolas Pitrebd2c39f2005-05-06 17:48:34213int parse_tag(struct tag *item)
214{
Nicolas Pitre21666f12007-02-26 19:55:59215 enum object_type type;
Nicolas Pitrebd2c39f2005-05-06 17:48:34216 void *data;
217 unsigned long size;
218 int ret;
219
220 if (item->object.parsed)
221 return 0;
Ævar Arnfjörð Bjarmasonbc726bd2023-03-28 13:58:50222 data = repo_read_object_file(the_repository, &item->object.oid, &type,
223 &size);
Nicolas Pitrebd2c39f2005-05-06 17:48:34224 if (!data)
225 return error("Could not read %s",
brian m. carlsonf2fd0762015-11-10 02:22:28226 oid_to_hex(&item->object.oid));
Nicolas Pitre21666f12007-02-26 19:55:59227 if (type != OBJ_TAG) {
Nicolas Pitrebd2c39f2005-05-06 17:48:34228 free(data);
229 return error("Object %s not a tag",
brian m. carlsonf2fd0762015-11-10 02:22:28230 oid_to_hex(&item->object.oid));
Nicolas Pitrebd2c39f2005-05-06 17:48:34231 }
Stefan Beller0e740fe2018-06-29 01:22:04232 ret = parse_tag_buffer(the_repository, item, data, size);
Sergey Vlasov13019d42005-05-04 17:44:15233 free(data);
Nicolas Pitrebd2c39f2005-05-06 17:48:34234 return ret;
Daniel Barkalow2636f612005-04-28 14:46:33235}
René Scharfedad3f062019-09-05 19:55:55236
237struct object_id *get_tagged_oid(struct tag *tag)
238{
239 if (!tag->tagged)
240 die("bad tag");
241 return &tag->tagged->oid;
242}