🌐 AI搜索 & 代理 主页
blob: 82d841bf2df0990f124ca9020673352124f20561 [file] [log] [blame]
Daniel Barkalow2636f612005-04-28 14:46:331#include "cache.h"
Junio C Hamano8f1d2e62006-01-07 09:33:542#include "tag.h"
Nicolas Pitre0ab17952007-02-26 19:56:003#include "commit.h"
4#include "tree.h"
5#include "blob.h"
Daniel Barkalow2636f612005-04-28 14:46:336
Michael J Gruberac58c4c2010-11-10 11:17:277#define PGP_SIGNATURE "-----BEGIN PGP SIGNATURE-----"
Michael J Gruber3d5854e2010-11-10 11:17:308#define PGP_MESSAGE "-----BEGIN PGP MESSAGE-----"
Michael J Gruberac58c4c2010-11-10 11:17:279
Daniel Barkalow2636f612005-04-28 14:46:3310const char *tag_type = "tag";
11
Junio C Hamano9534f402005-11-02 23:19:1312struct object *deref_tag(struct object *o, const char *warn, int warnlen)
Junio C Hamano37fde872005-08-05 07:47:5613{
Linus Torvalds19746322006-07-12 03:45:3114 while (o && o->type == OBJ_TAG)
Martin Koegler24e8a3c2008-02-18 07:31:5515 if (((struct tag *)o)->tagged)
16 o = parse_object(((struct tag *)o)->tagged->sha1);
17 else
18 o = NULL;
Junio C Hamano9534f402005-11-02 23:19:1319 if (!o && warn) {
20 if (!warnlen)
21 warnlen = strlen(warn);
22 error("missing object referenced by '%.*s'", warnlen, warn);
23 }
Junio C Hamano37fde872005-08-05 07:47:5624 return o;
25}
26
Jeff King90108a22012-01-06 19:18:0127struct object *deref_tag_noverify(struct object *o)
28{
29 while (o && o->type == OBJ_TAG) {
30 o = parse_object(o->sha1);
31 if (o && o->type == OBJ_TAG && ((struct tag *)o)->tagged)
32 o = ((struct tag *)o)->tagged;
33 else
34 o = NULL;
35 }
36 return o;
37}
38
Jason McMullan5d6ccf52005-06-03 15:05:3939struct tag *lookup_tag(const unsigned char *sha1)
Daniel Barkalow2636f612005-04-28 14:46:3340{
Linus Torvalds100c5f32007-04-17 05:11:4341 struct object *obj = lookup_object(sha1);
42 if (!obj)
Jeff Kingd36f51c2014-07-13 06:41:5543 return create_object(sha1, alloc_tag_node());
Jeff King8ff226a2014-07-13 06:42:0344 return object_as_type(obj, OBJ_TAG, 0);
Daniel Barkalow2636f612005-04-28 14:46:3345}
46
Shawn O. Pearcee451d062010-04-12 23:25:2847static unsigned long parse_tag_date(const char *buf, const char *tail)
48{
49 const char *dateptr;
50
51 while (buf < tail && *buf++ != '>')
52 /* nada */;
53 if (buf >= tail)
54 return 0;
55 dateptr = buf;
56 while (buf < tail && *buf++ != '\n')
57 /* nada */;
58 if (buf >= tail)
59 return 0;
60 /* dateptr < buf && buf[-1] == '\n', so strtoul will stop at buf-1 */
61 return strtoul(dateptr, NULL, 10);
62}
63
Nguyễn Thái Ngọc Duycf7b1ca2011-02-05 10:52:2064int parse_tag_buffer(struct tag *item, const void *data, unsigned long size)
Daniel Barkalow2636f612005-04-28 14:46:3365{
Nicolas Pitre0ab17952007-02-26 19:56:0066 unsigned char sha1[20];
Daniel Barkalow89e42022005-06-22 00:35:1067 char type[20];
Shawn O. Pearce28de5b62010-04-12 23:25:2768 const char *bufptr = data;
69 const char *tail = bufptr + size;
70 const char *nl;
Edgar Toernigae200ee2005-04-30 16:51:0371
Shawn O. Pearce2e0052a2010-04-12 23:25:2572 if (item->object.parsed)
73 return 0;
74 item->object.parsed = 1;
Daniel Barkalow2636f612005-04-28 14:46:3375
Daniel Barkalow2636f612005-04-28 14:46:3376 if (size < 64)
Nicolas Pitrebd2c39f2005-05-06 17:48:3477 return -1;
Shawn O. Pearce28de5b62010-04-12 23:25:2778 if (memcmp("object ", bufptr, 7) || get_sha1_hex(bufptr + 7, sha1) || bufptr[47] != '\n')
Nicolas Pitrebd2c39f2005-05-06 17:48:3479 return -1;
Shawn O. Pearce28de5b62010-04-12 23:25:2780 bufptr += 48; /* "object " + sha1 + "\n" */
Daniel Barkalow2636f612005-04-28 14:46:3381
Christian Couder59556542013-11-30 20:55:4082 if (!starts_with(bufptr, "type "))
Nicolas Pitrebd2c39f2005-05-06 17:48:3483 return -1;
Shawn O. Pearce28de5b62010-04-12 23:25:2784 bufptr += 5;
85 nl = memchr(bufptr, '\n', tail - bufptr);
86 if (!nl || sizeof(type) <= (nl - bufptr))
Nicolas Pitrebd2c39f2005-05-06 17:48:3487 return -1;
Shawn O. Pearce28de5b62010-04-12 23:25:2788 strncpy(type, bufptr, nl - bufptr);
89 type[nl - bufptr] = '\0';
90 bufptr = nl + 1;
Daniel Barkalow2636f612005-04-28 14:46:3391
Nicolas Pitre0ab17952007-02-26 19:56:0092 if (!strcmp(type, blob_type)) {
93 item->tagged = &lookup_blob(sha1)->object;
94 } else if (!strcmp(type, tree_type)) {
95 item->tagged = &lookup_tree(sha1)->object;
96 } else if (!strcmp(type, commit_type)) {
97 item->tagged = &lookup_commit(sha1)->object;
98 } else if (!strcmp(type, tag_type)) {
99 item->tagged = &lookup_tag(sha1)->object;
100 } else {
101 error("Unknown type %s", type);
102 item->tagged = NULL;
103 }
104
Christian Couder59556542013-11-30 20:55:40105 if (bufptr + 4 < tail && starts_with(bufptr, "tag "))
Nguyễn Thái Ngọc Duy85594252011-02-14 13:02:51106 ; /* good */
107 else
Shawn O. Pearce28de5b62010-04-12 23:25:27108 return -1;
109 bufptr += 4;
110 nl = memchr(bufptr, '\n', tail - bufptr);
111 if (!nl)
112 return -1;
113 item->tag = xmemdupz(bufptr, nl - bufptr);
114 bufptr = nl + 1;
115
Christian Couder59556542013-11-30 20:55:40116 if (bufptr + 7 < tail && starts_with(bufptr, "tagger "))
Shawn O. Pearcee451d062010-04-12 23:25:28117 item->date = parse_tag_date(bufptr, tail);
118 else
119 item->date = 0;
120
Daniel Barkalow2636f612005-04-28 14:46:33121 return 0;
Nicolas Pitrebd2c39f2005-05-06 17:48:34122}
Sergey Vlasov13019d42005-05-04 17:44:15123
Nicolas Pitrebd2c39f2005-05-06 17:48:34124int parse_tag(struct tag *item)
125{
Nicolas Pitre21666f12007-02-26 19:55:59126 enum object_type type;
Nicolas Pitrebd2c39f2005-05-06 17:48:34127 void *data;
128 unsigned long size;
129 int ret;
130
131 if (item->object.parsed)
132 return 0;
Nicolas Pitre21666f12007-02-26 19:55:59133 data = read_sha1_file(item->object.sha1, &type, &size);
Nicolas Pitrebd2c39f2005-05-06 17:48:34134 if (!data)
135 return error("Could not read %s",
136 sha1_to_hex(item->object.sha1));
Nicolas Pitre21666f12007-02-26 19:55:59137 if (type != OBJ_TAG) {
Nicolas Pitrebd2c39f2005-05-06 17:48:34138 free(data);
139 return error("Object %s not a tag",
140 sha1_to_hex(item->object.sha1));
141 }
142 ret = parse_tag_buffer(item, data, size);
Sergey Vlasov13019d42005-05-04 17:44:15143 free(data);
Nicolas Pitrebd2c39f2005-05-06 17:48:34144 return ret;
Daniel Barkalow2636f612005-04-28 14:46:33145}
Michael J Gruberac58c4c2010-11-10 11:17:27146
Junio C Hamano2f47eae2011-09-08 04:19:47147/*
148 * Look at a signed tag object, and return the offset where
149 * the embedded detached signature begins, or the end of the
150 * data when there is no such signature.
151 */
Michael J Gruberac58c4c2010-11-10 11:17:27152size_t parse_signature(const char *buf, unsigned long size)
153{
154 char *eol;
155 size_t len = 0;
Christian Couder59556542013-11-30 20:55:40156 while (len < size && !starts_with(buf + len, PGP_SIGNATURE) &&
157 !starts_with(buf + len, PGP_MESSAGE)) {
Michael J Gruberac58c4c2010-11-10 11:17:27158 eol = memchr(buf + len, '\n', size - len);
159 len += eol ? eol - (buf + len) + 1 : size - len;
160 }
161 return len;
162}