blob: 78d272b863f22285048cf54b9dcb03f80cb36f00 [file] [log] [blame]
Daniel Barkalow2636f612005-04-28 07:46:33 -07001#include "cache.h"
Junio C Hamano8f1d2e62006-01-07 01:33:54 -08002#include "tag.h"
Nicolas Pitre0ab17952007-02-26 14:56:00 -05003#include "commit.h"
4#include "tree.h"
5#include "blob.h"
Daniel Barkalow2636f612005-04-28 07:46:33 -07006
Michael J Gruberac58c4c2010-11-10 12:17:27 +01007#define PGP_SIGNATURE "-----BEGIN PGP SIGNATURE-----"
Michael J Gruber3d5854e2010-11-10 12:17:30 +01008#define PGP_MESSAGE "-----BEGIN PGP MESSAGE-----"
Michael J Gruberac58c4c2010-11-10 12:17:27 +01009
Daniel Barkalow2636f612005-04-28 07:46:33 -070010const char *tag_type = "tag";
11
Junio C Hamano9534f402005-11-02 15:19:13 -080012struct object *deref_tag(struct object *o, const char *warn, int warnlen)
Junio C Hamano37fde872005-08-05 00:47:56 -070013{
Linus Torvalds19746322006-07-11 20:45:31 -070014 while (o && o->type == OBJ_TAG)
Martin Koegler24e8a3c2008-02-18 08:31:55 +010015 if (((struct tag *)o)->tagged)
16 o = parse_object(((struct tag *)o)->tagged->sha1);
17 else
18 o = NULL;
Junio C Hamano9534f402005-11-02 15:19:13 -080019 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 00:47:56 -070024 return o;
25}
26
Jeff King90108a22012-01-06 14:18:01 -050027struct 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 11:05:39 -040039struct tag *lookup_tag(const unsigned char *sha1)
Daniel Barkalow2636f612005-04-28 07:46:33 -070040{
Linus Torvalds100c5f32007-04-16 22:11:43 -070041 struct object *obj = lookup_object(sha1);
42 if (!obj)
43 return create_object(sha1, OBJ_TAG, alloc_tag_node());
Nicolas Pitred1af0022005-05-20 16:59:17 -040044 if (!obj->type)
Linus Torvalds19746322006-07-11 20:45:31 -070045 obj->type = OBJ_TAG;
Junio C Hamanod2c030d2010-09-05 22:32:05 -070046 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 07:46:33 -070052}
53
Shawn O. Pearcee451d062010-04-12 16:25:28 -070054static 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 17:52:20 +070071int parse_tag_buffer(struct tag *item, const void *data, unsigned long size)
Daniel Barkalow2636f612005-04-28 07:46:33 -070072{
Nicolas Pitre0ab17952007-02-26 14:56:00 -050073 unsigned char sha1[20];
Daniel Barkalow89e42022005-06-21 20:35:10 -040074 char type[20];
Shawn O. Pearce28de5b62010-04-12 16:25:27 -070075 const char *bufptr = data;
76 const char *tail = bufptr + size;
77 const char *nl;
Edgar Toernigae200ee2005-04-30 09:51:03 -070078
Shawn O. Pearce2e0052a2010-04-12 16:25:25 -070079 if (item->object.parsed)
80 return 0;
81 item->object.parsed = 1;
Daniel Barkalow2636f612005-04-28 07:46:33 -070082
Daniel Barkalow2636f612005-04-28 07:46:33 -070083 if (size < 64)
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -040084 return -1;
Shawn O. Pearce28de5b62010-04-12 16:25:27 -070085 if (memcmp("object ", bufptr, 7) || get_sha1_hex(bufptr + 7, sha1) || bufptr[47] != '\n')
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -040086 return -1;
Shawn O. Pearce28de5b62010-04-12 16:25:27 -070087 bufptr += 48; /* "object " + sha1 + "\n" */
Daniel Barkalow2636f612005-04-28 07:46:33 -070088
Shawn O. Pearce28de5b62010-04-12 16:25:27 -070089 if (prefixcmp(bufptr, "type "))
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -040090 return -1;
Shawn O. Pearce28de5b62010-04-12 16:25:27 -070091 bufptr += 5;
92 nl = memchr(bufptr, '\n', tail - bufptr);
93 if (!nl || sizeof(type) <= (nl - bufptr))
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -040094 return -1;
Shawn O. Pearce28de5b62010-04-12 16:25:27 -070095 strncpy(type, bufptr, nl - bufptr);
96 type[nl - bufptr] = '\0';
97 bufptr = nl + 1;
Daniel Barkalow2636f612005-04-28 07:46:33 -070098
Nicolas Pitre0ab17952007-02-26 14:56:00 -050099 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
Nguyễn Thái Ngọc Duy85594252011-02-14 20:02:51 +0700112 if (bufptr + 4 < tail && !prefixcmp(bufptr, "tag "))
113 ; /* good */
114 else
Shawn O. Pearce28de5b62010-04-12 16:25:27 -0700115 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
Nguyễn Thái Ngọc Duy85594252011-02-14 20:02:51 +0700123 if (bufptr + 7 < tail && !prefixcmp(bufptr, "tagger "))
Shawn O. Pearcee451d062010-04-12 16:25:28 -0700124 item->date = parse_tag_date(bufptr, tail);
125 else
126 item->date = 0;
127
Daniel Barkalow2636f612005-04-28 07:46:33 -0700128 return 0;
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400129}
Sergey Vlasov13019d42005-05-04 21:44:15 +0400130
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400131int parse_tag(struct tag *item)
132{
Nicolas Pitre21666f12007-02-26 14:55:59 -0500133 enum object_type type;
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400134 void *data;
135 unsigned long size;
136 int ret;
137
138 if (item->object.parsed)
139 return 0;
Nicolas Pitre21666f12007-02-26 14:55:59 -0500140 data = read_sha1_file(item->object.sha1, &type, &size);
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400141 if (!data)
142 return error("Could not read %s",
143 sha1_to_hex(item->object.sha1));
Nicolas Pitre21666f12007-02-26 14:55:59 -0500144 if (type != OBJ_TAG) {
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400145 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 21:44:15 +0400150 free(data);
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400151 return ret;
Daniel Barkalow2636f612005-04-28 07:46:33 -0700152}
Michael J Gruberac58c4c2010-11-10 12:17:27 +0100153
Junio C Hamano2f47eae2011-09-07 21:19:47 -0700154/*
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 12:17:27 +0100159size_t parse_signature(const char *buf, unsigned long size)
160{
161 char *eol;
162 size_t len = 0;
Michael J Gruber3d5854e2010-11-10 12:17:30 +0100163 while (len < size && prefixcmp(buf + len, PGP_SIGNATURE) &&
164 prefixcmp(buf + len, PGP_MESSAGE)) {
Michael J Gruberac58c4c2010-11-10 12:17:27 +0100165 eol = memchr(buf + len, '\n', size - len);
166 len += eol ? eol - (buf + len) + 1 : size - len;
167 }
168 return len;
169}