blob: d1dcd18cd7b53e21fa15bab9baad05cf16a3b9de [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
7const char *tag_type = "tag";
8
Santiago Torres45a227e2016-04-22 10:52:04 -04009static int run_gpg_verify(const char *buf, unsigned long size, unsigned flags)
10{
11 struct signature_check sigc;
12 size_t payload_size;
13 int ret;
14
15 memset(&sigc, 0, sizeof(sigc));
16
17 payload_size = parse_signature(buf, size);
18
19 if (size == payload_size) {
20 if (flags & GPG_VERIFY_VERBOSE)
21 write_in_full(1, buf, payload_size);
22 return error("no signature found");
23 }
24
25 ret = check_signature(buf, payload_size, buf + payload_size,
26 size - payload_size, &sigc);
27 print_signature_buffer(&sigc, flags);
28
29 signature_check_clear(&sigc);
30 return ret;
31}
32
33int gpg_verify_tag(const unsigned char *sha1, const char *name_to_report,
34 unsigned flags)
35{
36 enum object_type type;
37 char *buf;
38 unsigned long size;
39 int ret;
40
41 type = sha1_object_info(sha1, NULL);
42 if (type != OBJ_TAG)
43 return error("%s: cannot verify a non-tag object of type %s.",
44 name_to_report ?
45 name_to_report :
46 find_unique_abbrev(sha1, DEFAULT_ABBREV),
47 typename(type));
48
49 buf = read_sha1_file(sha1, &type, &size);
50 if (!buf)
51 return error("%s: unable to read file.",
52 name_to_report ?
53 name_to_report :
54 find_unique_abbrev(sha1, DEFAULT_ABBREV));
55
56 ret = run_gpg_verify(buf, size, flags);
57
58 free(buf);
59 return ret;
60}
61
Junio C Hamano9534f402005-11-02 15:19:13 -080062struct object *deref_tag(struct object *o, const char *warn, int warnlen)
Junio C Hamano37fde872005-08-05 00:47:56 -070063{
Linus Torvalds19746322006-07-11 20:45:31 -070064 while (o && o->type == OBJ_TAG)
Martin Koegler24e8a3c2008-02-18 08:31:55 +010065 if (((struct tag *)o)->tagged)
brian m. carlsoned1c9972015-11-10 02:22:29 +000066 o = parse_object(((struct tag *)o)->tagged->oid.hash);
Martin Koegler24e8a3c2008-02-18 08:31:55 +010067 else
68 o = NULL;
Junio C Hamano9534f402005-11-02 15:19:13 -080069 if (!o && warn) {
70 if (!warnlen)
71 warnlen = strlen(warn);
72 error("missing object referenced by '%.*s'", warnlen, warn);
73 }
Junio C Hamano37fde872005-08-05 00:47:56 -070074 return o;
75}
76
Jeff King90108a22012-01-06 14:18:01 -050077struct object *deref_tag_noverify(struct object *o)
78{
79 while (o && o->type == OBJ_TAG) {
brian m. carlsoned1c9972015-11-10 02:22:29 +000080 o = parse_object(o->oid.hash);
Jeff King90108a22012-01-06 14:18:01 -050081 if (o && o->type == OBJ_TAG && ((struct tag *)o)->tagged)
82 o = ((struct tag *)o)->tagged;
83 else
84 o = NULL;
85 }
86 return o;
87}
88
Jason McMullan5d6ccf52005-06-03 11:05:39 -040089struct tag *lookup_tag(const unsigned char *sha1)
Daniel Barkalow2636f612005-04-28 07:46:33 -070090{
Linus Torvalds100c5f32007-04-16 22:11:43 -070091 struct object *obj = lookup_object(sha1);
92 if (!obj)
Jeff Kingd36f51c2014-07-13 02:41:55 -040093 return create_object(sha1, alloc_tag_node());
Jeff King8ff226a2014-07-13 02:42:03 -040094 return object_as_type(obj, OBJ_TAG, 0);
Daniel Barkalow2636f612005-04-28 07:46:33 -070095}
96
Shawn O. Pearcee451d062010-04-12 16:25:28 -070097static unsigned long parse_tag_date(const char *buf, const char *tail)
98{
99 const char *dateptr;
100
101 while (buf < tail && *buf++ != '>')
102 /* nada */;
103 if (buf >= tail)
104 return 0;
105 dateptr = buf;
106 while (buf < tail && *buf++ != '\n')
107 /* nada */;
108 if (buf >= tail)
109 return 0;
110 /* dateptr < buf && buf[-1] == '\n', so strtoul will stop at buf-1 */
111 return strtoul(dateptr, NULL, 10);
112}
113
Nguyễn Thái Ngọc Duycf7b1ca2011-02-05 17:52:20 +0700114int parse_tag_buffer(struct tag *item, const void *data, unsigned long size)
Daniel Barkalow2636f612005-04-28 07:46:33 -0700115{
Nicolas Pitre0ab17952007-02-26 14:56:00 -0500116 unsigned char sha1[20];
Daniel Barkalow89e42022005-06-21 20:35:10 -0400117 char type[20];
Shawn O. Pearce28de5b62010-04-12 16:25:27 -0700118 const char *bufptr = data;
119 const char *tail = bufptr + size;
120 const char *nl;
Edgar Toernigae200ee2005-04-30 09:51:03 -0700121
Shawn O. Pearce2e0052a2010-04-12 16:25:25 -0700122 if (item->object.parsed)
123 return 0;
124 item->object.parsed = 1;
Daniel Barkalow2636f612005-04-28 07:46:33 -0700125
Daniel Barkalow2636f612005-04-28 07:46:33 -0700126 if (size < 64)
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400127 return -1;
Shawn O. Pearce28de5b62010-04-12 16:25:27 -0700128 if (memcmp("object ", bufptr, 7) || get_sha1_hex(bufptr + 7, sha1) || bufptr[47] != '\n')
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400129 return -1;
Shawn O. Pearce28de5b62010-04-12 16:25:27 -0700130 bufptr += 48; /* "object " + sha1 + "\n" */
Daniel Barkalow2636f612005-04-28 07:46:33 -0700131
Christian Couder59556542013-11-30 21:55:40 +0100132 if (!starts_with(bufptr, "type "))
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400133 return -1;
Shawn O. Pearce28de5b62010-04-12 16:25:27 -0700134 bufptr += 5;
135 nl = memchr(bufptr, '\n', tail - bufptr);
136 if (!nl || sizeof(type) <= (nl - bufptr))
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400137 return -1;
Jeff Kingeddda372015-09-24 17:08:26 -0400138 memcpy(type, bufptr, nl - bufptr);
Shawn O. Pearce28de5b62010-04-12 16:25:27 -0700139 type[nl - bufptr] = '\0';
140 bufptr = nl + 1;
Daniel Barkalow2636f612005-04-28 07:46:33 -0700141
Nicolas Pitre0ab17952007-02-26 14:56:00 -0500142 if (!strcmp(type, blob_type)) {
143 item->tagged = &lookup_blob(sha1)->object;
144 } else if (!strcmp(type, tree_type)) {
145 item->tagged = &lookup_tree(sha1)->object;
146 } else if (!strcmp(type, commit_type)) {
147 item->tagged = &lookup_commit(sha1)->object;
148 } else if (!strcmp(type, tag_type)) {
149 item->tagged = &lookup_tag(sha1)->object;
150 } else {
151 error("Unknown type %s", type);
152 item->tagged = NULL;
153 }
154
Christian Couder59556542013-11-30 21:55:40 +0100155 if (bufptr + 4 < tail && starts_with(bufptr, "tag "))
Nguyễn Thái Ngọc Duy85594252011-02-14 20:02:51 +0700156 ; /* good */
157 else
Shawn O. Pearce28de5b62010-04-12 16:25:27 -0700158 return -1;
159 bufptr += 4;
160 nl = memchr(bufptr, '\n', tail - bufptr);
161 if (!nl)
162 return -1;
163 item->tag = xmemdupz(bufptr, nl - bufptr);
164 bufptr = nl + 1;
165
Christian Couder59556542013-11-30 21:55:40 +0100166 if (bufptr + 7 < tail && starts_with(bufptr, "tagger "))
Shawn O. Pearcee451d062010-04-12 16:25:28 -0700167 item->date = parse_tag_date(bufptr, tail);
168 else
169 item->date = 0;
170
Daniel Barkalow2636f612005-04-28 07:46:33 -0700171 return 0;
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400172}
Sergey Vlasov13019d42005-05-04 21:44:15 +0400173
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400174int parse_tag(struct tag *item)
175{
Nicolas Pitre21666f12007-02-26 14:55:59 -0500176 enum object_type type;
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400177 void *data;
178 unsigned long size;
179 int ret;
180
181 if (item->object.parsed)
182 return 0;
brian m. carlsoned1c9972015-11-10 02:22:29 +0000183 data = read_sha1_file(item->object.oid.hash, &type, &size);
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400184 if (!data)
185 return error("Could not read %s",
brian m. carlsonf2fd0762015-11-10 02:22:28 +0000186 oid_to_hex(&item->object.oid));
Nicolas Pitre21666f12007-02-26 14:55:59 -0500187 if (type != OBJ_TAG) {
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400188 free(data);
189 return error("Object %s not a tag",
brian m. carlsonf2fd0762015-11-10 02:22:28 +0000190 oid_to_hex(&item->object.oid));
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400191 }
192 ret = parse_tag_buffer(item, data, size);
Sergey Vlasov13019d42005-05-04 21:44:15 +0400193 free(data);
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400194 return ret;
Daniel Barkalow2636f612005-04-28 07:46:33 -0700195}