🌐 AI搜索 & 代理 主页
blob: 7b07921b30641994ab3a877b6b377c5ce1f40149 [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)
43 return create_object(sha1, OBJ_TAG, alloc_tag_node());
Nicolas Pitred1af0022005-05-20 20:59:1744 if (!obj->type)
Linus Torvalds19746322006-07-12 03:45:3145 obj->type = OBJ_TAG;
Junio C Hamanod2c030d2010-09-06 05:32:0546 if (obj->type != OBJ_TAG) {
47 error("Object %s is a %s, not a tag",
48 sha1_to_hex(sha1), typename(obj->type));
49 return NULL;
50 }
51 return (struct tag *) obj;
Daniel Barkalow2636f612005-04-28 14:46:3352}
53
Shawn O. Pearcee451d062010-04-12 23:25:2854static unsigned long parse_tag_date(const char *buf, const char *tail)
55{
56 const char *dateptr;
57
58 while (buf < tail && *buf++ != '>')
59 /* nada */;
60 if (buf >= tail)
61 return 0;
62 dateptr = buf;
63 while (buf < tail && *buf++ != '\n')
64 /* nada */;
65 if (buf >= tail)
66 return 0;
67 /* dateptr < buf && buf[-1] == '\n', so strtoul will stop at buf-1 */
68 return strtoul(dateptr, NULL, 10);
69}
70
Nguyễn Thái Ngọc Duycf7b1ca2011-02-05 10:52:2071int parse_tag_buffer(struct tag *item, const void *data, unsigned long size)
Daniel Barkalow2636f612005-04-28 14:46:3372{
Nicolas Pitre0ab17952007-02-26 19:56:0073 unsigned char sha1[20];
Daniel Barkalow89e42022005-06-22 00:35:1074 char type[20];
Shawn O. Pearce28de5b62010-04-12 23:25:2775 const char *bufptr = data;
76 const char *tail = bufptr + size;
77 const char *nl;
Edgar Toernigae200ee2005-04-30 16:51:0378
Shawn O. Pearce2e0052a2010-04-12 23:25:2579 if (item->object.parsed)
80 return 0;
81 item->object.parsed = 1;
Daniel Barkalow2636f612005-04-28 14:46:3382
Daniel Barkalow2636f612005-04-28 14:46:3383 if (size < 64)
Nicolas Pitrebd2c39f2005-05-06 17:48:3484 return -1;
Shawn O. Pearce28de5b62010-04-12 23:25:2785 if (memcmp("object ", bufptr, 7) || get_sha1_hex(bufptr + 7, sha1) || bufptr[47] != '\n')
Nicolas Pitrebd2c39f2005-05-06 17:48:3486 return -1;
Shawn O. Pearce28de5b62010-04-12 23:25:2787 bufptr += 48; /* "object " + sha1 + "\n" */
Daniel Barkalow2636f612005-04-28 14:46:3388
Christian Couder59556542013-11-30 20:55:4089 if (!starts_with(bufptr, "type "))
Nicolas Pitrebd2c39f2005-05-06 17:48:3490 return -1;
Shawn O. Pearce28de5b62010-04-12 23:25:2791 bufptr += 5;
92 nl = memchr(bufptr, '\n', tail - bufptr);
93 if (!nl || sizeof(type) <= (nl - bufptr))
Nicolas Pitrebd2c39f2005-05-06 17:48:3494 return -1;
Shawn O. Pearce28de5b62010-04-12 23:25:2795 strncpy(type, bufptr, nl - bufptr);
96 type[nl - bufptr] = '\0';
97 bufptr = nl + 1;
Daniel Barkalow2636f612005-04-28 14:46:3398
Nicolas Pitre0ab17952007-02-26 19:56:0099 if (!strcmp(type, blob_type)) {
100 item->tagged = &lookup_blob(sha1)->object;
101 } else if (!strcmp(type, tree_type)) {
102 item->tagged = &lookup_tree(sha1)->object;
103 } else if (!strcmp(type, commit_type)) {
104 item->tagged = &lookup_commit(sha1)->object;
105 } else if (!strcmp(type, tag_type)) {
106 item->tagged = &lookup_tag(sha1)->object;
107 } else {
108 error("Unknown type %s", type);
109 item->tagged = NULL;
110 }
111
Christian Couder59556542013-11-30 20:55:40112 if (bufptr + 4 < tail && starts_with(bufptr, "tag "))
Nguyễn Thái Ngọc Duy85594252011-02-14 13:02:51113 ; /* good */
114 else
Shawn O. Pearce28de5b62010-04-12 23:25:27115 return -1;
116 bufptr += 4;
117 nl = memchr(bufptr, '\n', tail - bufptr);
118 if (!nl)
119 return -1;
120 item->tag = xmemdupz(bufptr, nl - bufptr);
121 bufptr = nl + 1;
122
Christian Couder59556542013-11-30 20:55:40123 if (bufptr + 7 < tail && starts_with(bufptr, "tagger "))
Shawn O. Pearcee451d062010-04-12 23:25:28124 item->date = parse_tag_date(bufptr, tail);
125 else
126 item->date = 0;
127
Daniel Barkalow2636f612005-04-28 14:46:33128 return 0;
Nicolas Pitrebd2c39f2005-05-06 17:48:34129}
Sergey Vlasov13019d42005-05-04 17:44:15130
Nicolas Pitrebd2c39f2005-05-06 17:48:34131int parse_tag(struct tag *item)
132{
Nicolas Pitre21666f12007-02-26 19:55:59133 enum object_type type;
Nicolas Pitrebd2c39f2005-05-06 17:48:34134 void *data;
135 unsigned long size;
136 int ret;
137
138 if (item->object.parsed)
139 return 0;
Nicolas Pitre21666f12007-02-26 19:55:59140 data = read_sha1_file(item->object.sha1, &type, &size);
Nicolas Pitrebd2c39f2005-05-06 17:48:34141 if (!data)
142 return error("Could not read %s",
143 sha1_to_hex(item->object.sha1));
Nicolas Pitre21666f12007-02-26 19:55:59144 if (type != OBJ_TAG) {
Nicolas Pitrebd2c39f2005-05-06 17:48:34145 free(data);
146 return error("Object %s not a tag",
147 sha1_to_hex(item->object.sha1));
148 }
149 ret = parse_tag_buffer(item, data, size);
Sergey Vlasov13019d42005-05-04 17:44:15150 free(data);
Nicolas Pitrebd2c39f2005-05-06 17:48:34151 return ret;
Daniel Barkalow2636f612005-04-28 14:46:33152}
Michael J Gruberac58c4c2010-11-10 11:17:27153
Junio C Hamano2f47eae2011-09-08 04:19:47154/*
155 * Look at a signed tag object, and return the offset where
156 * the embedded detached signature begins, or the end of the
157 * data when there is no such signature.
158 */
Michael J Gruberac58c4c2010-11-10 11:17:27159size_t parse_signature(const char *buf, unsigned long size)
160{
161 char *eol;
162 size_t len = 0;
Christian Couder59556542013-11-30 20:55:40163 while (len < size && !starts_with(buf + len, PGP_SIGNATURE) &&
164 !starts_with(buf + len, PGP_MESSAGE)) {
Michael J Gruberac58c4c2010-11-10 11:17:27165 eol = memchr(buf + len, '\n', size - len);
166 len += eol ? eol - (buf + len) + 1 : size - len;
167 }
168 return len;
169}