blob: d4f3080e3f8c3d8026292a2eeccc17152b404254 [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-----"
8
Daniel Barkalow2636f612005-04-28 07:46:33 -07009const char *tag_type = "tag";
10
Junio C Hamano9534f402005-11-02 15:19:13 -080011struct object *deref_tag(struct object *o, const char *warn, int warnlen)
Junio C Hamano37fde872005-08-05 00:47:56 -070012{
Linus Torvalds19746322006-07-11 20:45:31 -070013 while (o && o->type == OBJ_TAG)
Martin Koegler24e8a3c2008-02-18 08:31:55 +010014 if (((struct tag *)o)->tagged)
15 o = parse_object(((struct tag *)o)->tagged->sha1);
16 else
17 o = NULL;
Junio C Hamano9534f402005-11-02 15:19:13 -080018 if (!o && warn) {
19 if (!warnlen)
20 warnlen = strlen(warn);
21 error("missing object referenced by '%.*s'", warnlen, warn);
22 }
Junio C Hamano37fde872005-08-05 00:47:56 -070023 return o;
24}
25
Jason McMullan5d6ccf52005-06-03 11:05:39 -040026struct tag *lookup_tag(const unsigned char *sha1)
Daniel Barkalow2636f612005-04-28 07:46:33 -070027{
Linus Torvalds100c5f32007-04-16 22:11:43 -070028 struct object *obj = lookup_object(sha1);
29 if (!obj)
30 return create_object(sha1, OBJ_TAG, alloc_tag_node());
Nicolas Pitred1af0022005-05-20 16:59:17 -040031 if (!obj->type)
Linus Torvalds19746322006-07-11 20:45:31 -070032 obj->type = OBJ_TAG;
Junio C Hamanod2c030d2010-09-05 22:32:05 -070033 if (obj->type != OBJ_TAG) {
34 error("Object %s is a %s, not a tag",
35 sha1_to_hex(sha1), typename(obj->type));
36 return NULL;
37 }
38 return (struct tag *) obj;
Daniel Barkalow2636f612005-04-28 07:46:33 -070039}
40
Shawn O. Pearcee451d062010-04-12 16:25:28 -070041static unsigned long parse_tag_date(const char *buf, const char *tail)
42{
43 const char *dateptr;
44
45 while (buf < tail && *buf++ != '>')
46 /* nada */;
47 if (buf >= tail)
48 return 0;
49 dateptr = buf;
50 while (buf < tail && *buf++ != '\n')
51 /* nada */;
52 if (buf >= tail)
53 return 0;
54 /* dateptr < buf && buf[-1] == '\n', so strtoul will stop at buf-1 */
55 return strtoul(dateptr, NULL, 10);
56}
57
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -040058int parse_tag_buffer(struct tag *item, void *data, unsigned long size)
Daniel Barkalow2636f612005-04-28 07:46:33 -070059{
Nicolas Pitre0ab17952007-02-26 14:56:00 -050060 unsigned char sha1[20];
Daniel Barkalow89e42022005-06-21 20:35:10 -040061 char type[20];
Shawn O. Pearce28de5b62010-04-12 16:25:27 -070062 const char *bufptr = data;
63 const char *tail = bufptr + size;
64 const char *nl;
Edgar Toernigae200ee2005-04-30 09:51:03 -070065
Shawn O. Pearce2e0052a2010-04-12 16:25:25 -070066 if (item->object.parsed)
67 return 0;
68 item->object.parsed = 1;
Daniel Barkalow2636f612005-04-28 07:46:33 -070069
Daniel Barkalow2636f612005-04-28 07:46:33 -070070 if (size < 64)
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -040071 return -1;
Shawn O. Pearce28de5b62010-04-12 16:25:27 -070072 if (memcmp("object ", bufptr, 7) || get_sha1_hex(bufptr + 7, sha1) || bufptr[47] != '\n')
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -040073 return -1;
Shawn O. Pearce28de5b62010-04-12 16:25:27 -070074 bufptr += 48; /* "object " + sha1 + "\n" */
Daniel Barkalow2636f612005-04-28 07:46:33 -070075
Shawn O. Pearce28de5b62010-04-12 16:25:27 -070076 if (prefixcmp(bufptr, "type "))
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -040077 return -1;
Shawn O. Pearce28de5b62010-04-12 16:25:27 -070078 bufptr += 5;
79 nl = memchr(bufptr, '\n', tail - bufptr);
80 if (!nl || sizeof(type) <= (nl - bufptr))
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -040081 return -1;
Shawn O. Pearce28de5b62010-04-12 16:25:27 -070082 strncpy(type, bufptr, nl - bufptr);
83 type[nl - bufptr] = '\0';
84 bufptr = nl + 1;
Daniel Barkalow2636f612005-04-28 07:46:33 -070085
Nicolas Pitre0ab17952007-02-26 14:56:00 -050086 if (!strcmp(type, blob_type)) {
87 item->tagged = &lookup_blob(sha1)->object;
88 } else if (!strcmp(type, tree_type)) {
89 item->tagged = &lookup_tree(sha1)->object;
90 } else if (!strcmp(type, commit_type)) {
91 item->tagged = &lookup_commit(sha1)->object;
92 } else if (!strcmp(type, tag_type)) {
93 item->tagged = &lookup_tag(sha1)->object;
94 } else {
95 error("Unknown type %s", type);
96 item->tagged = NULL;
97 }
98
Shawn O. Pearce28de5b62010-04-12 16:25:27 -070099 if (prefixcmp(bufptr, "tag "))
100 return -1;
101 bufptr += 4;
102 nl = memchr(bufptr, '\n', tail - bufptr);
103 if (!nl)
104 return -1;
105 item->tag = xmemdupz(bufptr, nl - bufptr);
106 bufptr = nl + 1;
107
Shawn O. Pearcee451d062010-04-12 16:25:28 -0700108 if (!prefixcmp(bufptr, "tagger "))
109 item->date = parse_tag_date(bufptr, tail);
110 else
111 item->date = 0;
112
Daniel Barkalow2636f612005-04-28 07:46:33 -0700113 return 0;
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400114}
Sergey Vlasov13019d42005-05-04 21:44:15 +0400115
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400116int parse_tag(struct tag *item)
117{
Nicolas Pitre21666f12007-02-26 14:55:59 -0500118 enum object_type type;
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400119 void *data;
120 unsigned long size;
121 int ret;
122
123 if (item->object.parsed)
124 return 0;
Nicolas Pitre21666f12007-02-26 14:55:59 -0500125 data = read_sha1_file(item->object.sha1, &type, &size);
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400126 if (!data)
127 return error("Could not read %s",
128 sha1_to_hex(item->object.sha1));
Nicolas Pitre21666f12007-02-26 14:55:59 -0500129 if (type != OBJ_TAG) {
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400130 free(data);
131 return error("Object %s not a tag",
132 sha1_to_hex(item->object.sha1));
133 }
134 ret = parse_tag_buffer(item, data, size);
Sergey Vlasov13019d42005-05-04 21:44:15 +0400135 free(data);
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400136 return ret;
Daniel Barkalow2636f612005-04-28 07:46:33 -0700137}
Michael J Gruberac58c4c2010-11-10 12:17:27 +0100138
139size_t parse_signature(const char *buf, unsigned long size)
140{
141 char *eol;
142 size_t len = 0;
143 while (len < size && prefixcmp(buf + len, PGP_SIGNATURE)) {
144 eol = memchr(buf + len, '\n', size - len);
145 len += eol ? eol - (buf + len) + 1 : size - len;
146 }
147 return len;
148}