🌐 AI搜索 & 代理 主页
blob: 7d38cc0f4de1c16b5b52725ba7a6a361650a6b41 [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
Jason McMullan5d6ccf52005-06-03 15:05:3927struct tag *lookup_tag(const unsigned char *sha1)
Daniel Barkalow2636f612005-04-28 14:46:3328{
Linus Torvalds100c5f32007-04-17 05:11:4329 struct object *obj = lookup_object(sha1);
30 if (!obj)
31 return create_object(sha1, OBJ_TAG, alloc_tag_node());
Nicolas Pitred1af0022005-05-20 20:59:1732 if (!obj->type)
Linus Torvalds19746322006-07-12 03:45:3133 obj->type = OBJ_TAG;
Junio C Hamanod2c030d2010-09-06 05:32:0534 if (obj->type != OBJ_TAG) {
35 error("Object %s is a %s, not a tag",
36 sha1_to_hex(sha1), typename(obj->type));
37 return NULL;
38 }
39 return (struct tag *) obj;
Daniel Barkalow2636f612005-04-28 14:46:3340}
41
Shawn O. Pearcee451d062010-04-12 23:25:2842static unsigned long parse_tag_date(const char *buf, const char *tail)
43{
44 const char *dateptr;
45
46 while (buf < tail && *buf++ != '>')
47 /* nada */;
48 if (buf >= tail)
49 return 0;
50 dateptr = buf;
51 while (buf < tail && *buf++ != '\n')
52 /* nada */;
53 if (buf >= tail)
54 return 0;
55 /* dateptr < buf && buf[-1] == '\n', so strtoul will stop at buf-1 */
56 return strtoul(dateptr, NULL, 10);
57}
58
Nguyễn Thái Ngọc Duycf7b1ca2011-02-05 10:52:2059int parse_tag_buffer(struct tag *item, const void *data, unsigned long size)
Daniel Barkalow2636f612005-04-28 14:46:3360{
Nicolas Pitre0ab17952007-02-26 19:56:0061 unsigned char sha1[20];
Daniel Barkalow89e42022005-06-22 00:35:1062 char type[20];
Shawn O. Pearce28de5b62010-04-12 23:25:2763 const char *bufptr = data;
64 const char *tail = bufptr + size;
65 const char *nl;
Edgar Toernigae200ee2005-04-30 16:51:0366
Shawn O. Pearce2e0052a2010-04-12 23:25:2567 if (item->object.parsed)
68 return 0;
69 item->object.parsed = 1;
Daniel Barkalow2636f612005-04-28 14:46:3370
Daniel Barkalow2636f612005-04-28 14:46:3371 if (size < 64)
Nicolas Pitrebd2c39f2005-05-06 17:48:3472 return -1;
Shawn O. Pearce28de5b62010-04-12 23:25:2773 if (memcmp("object ", bufptr, 7) || get_sha1_hex(bufptr + 7, sha1) || bufptr[47] != '\n')
Nicolas Pitrebd2c39f2005-05-06 17:48:3474 return -1;
Shawn O. Pearce28de5b62010-04-12 23:25:2775 bufptr += 48; /* "object " + sha1 + "\n" */
Daniel Barkalow2636f612005-04-28 14:46:3376
Shawn O. Pearce28de5b62010-04-12 23:25:2777 if (prefixcmp(bufptr, "type "))
Nicolas Pitrebd2c39f2005-05-06 17:48:3478 return -1;
Shawn O. Pearce28de5b62010-04-12 23:25:2779 bufptr += 5;
80 nl = memchr(bufptr, '\n', tail - bufptr);
81 if (!nl || sizeof(type) <= (nl - bufptr))
Nicolas Pitrebd2c39f2005-05-06 17:48:3482 return -1;
Shawn O. Pearce28de5b62010-04-12 23:25:2783 strncpy(type, bufptr, nl - bufptr);
84 type[nl - bufptr] = '\0';
85 bufptr = nl + 1;
Daniel Barkalow2636f612005-04-28 14:46:3386
Nicolas Pitre0ab17952007-02-26 19:56:0087 if (!strcmp(type, blob_type)) {
88 item->tagged = &lookup_blob(sha1)->object;
89 } else if (!strcmp(type, tree_type)) {
90 item->tagged = &lookup_tree(sha1)->object;
91 } else if (!strcmp(type, commit_type)) {
92 item->tagged = &lookup_commit(sha1)->object;
93 } else if (!strcmp(type, tag_type)) {
94 item->tagged = &lookup_tag(sha1)->object;
95 } else {
96 error("Unknown type %s", type);
97 item->tagged = NULL;
98 }
99
Nguyễn Thái Ngọc Duy85594252011-02-14 13:02:51100 if (bufptr + 4 < tail && !prefixcmp(bufptr, "tag "))
101 ; /* good */
102 else
Shawn O. Pearce28de5b62010-04-12 23:25:27103 return -1;
104 bufptr += 4;
105 nl = memchr(bufptr, '\n', tail - bufptr);
106 if (!nl)
107 return -1;
108 item->tag = xmemdupz(bufptr, nl - bufptr);
109 bufptr = nl + 1;
110
Nguyễn Thái Ngọc Duy85594252011-02-14 13:02:51111 if (bufptr + 7 < tail && !prefixcmp(bufptr, "tagger "))
Shawn O. Pearcee451d062010-04-12 23:25:28112 item->date = parse_tag_date(bufptr, tail);
113 else
114 item->date = 0;
115
Daniel Barkalow2636f612005-04-28 14:46:33116 return 0;
Nicolas Pitrebd2c39f2005-05-06 17:48:34117}
Sergey Vlasov13019d42005-05-04 17:44:15118
Nicolas Pitrebd2c39f2005-05-06 17:48:34119int parse_tag(struct tag *item)
120{
Nicolas Pitre21666f12007-02-26 19:55:59121 enum object_type type;
Nicolas Pitrebd2c39f2005-05-06 17:48:34122 void *data;
123 unsigned long size;
124 int ret;
125
126 if (item->object.parsed)
127 return 0;
Nicolas Pitre21666f12007-02-26 19:55:59128 data = read_sha1_file(item->object.sha1, &type, &size);
Nicolas Pitrebd2c39f2005-05-06 17:48:34129 if (!data)
130 return error("Could not read %s",
131 sha1_to_hex(item->object.sha1));
Nicolas Pitre21666f12007-02-26 19:55:59132 if (type != OBJ_TAG) {
Nicolas Pitrebd2c39f2005-05-06 17:48:34133 free(data);
134 return error("Object %s not a tag",
135 sha1_to_hex(item->object.sha1));
136 }
137 ret = parse_tag_buffer(item, data, size);
Sergey Vlasov13019d42005-05-04 17:44:15138 free(data);
Nicolas Pitrebd2c39f2005-05-06 17:48:34139 return ret;
Daniel Barkalow2636f612005-04-28 14:46:33140}
Michael J Gruberac58c4c2010-11-10 11:17:27141
142size_t parse_signature(const char *buf, unsigned long size)
143{
144 char *eol;
145 size_t len = 0;
Michael J Gruber3d5854e2010-11-10 11:17:30146 while (len < size && prefixcmp(buf + len, PGP_SIGNATURE) &&
147 prefixcmp(buf + len, PGP_MESSAGE)) {
Michael J Gruberac58c4c2010-11-10 11:17:27148 eol = memchr(buf + len, '\n', size - len);
149 len += eol ? eol - (buf + len) + 1 : size - len;
150 }
151 return len;
152}