alloc: add repository argument to alloc_commit_node
[git/git.git] / tag.c
CommitLineData
2636f614 1#include "cache.h"
8f1d2e6f 2#include "tag.h"
0ab17950
NP
3#include "commit.h"
4#include "tree.h"
5#include "blob.h"
94240b91 6#include "gpg-interface.h"
2636f614
DB
7
8const char *tag_type = "tag";
9
45a227ef
ST
10static int run_gpg_verify(const char *buf, unsigned long size, unsigned flags)
11{
12 struct signature_check sigc;
13 size_t payload_size;
14 int ret;
15
16 memset(&sigc, 0, sizeof(sigc));
17
18 payload_size = parse_signature(buf, size);
19
20 if (size == payload_size) {
21 if (flags & GPG_VERIFY_VERBOSE)
22 write_in_full(1, buf, payload_size);
23 return error("no signature found");
24 }
25
26 ret = check_signature(buf, payload_size, buf + payload_size,
27 size - payload_size, &sigc);
94240b91
LP
28
29 if (!(flags & GPG_VERIFY_OMIT_STATUS))
30 print_signature_buffer(&sigc, flags);
45a227ef
ST
31
32 signature_check_clear(&sigc);
33 return ret;
34}
35
84571760 36int gpg_verify_tag(const struct object_id *oid, const char *name_to_report,
45a227ef
ST
37 unsigned flags)
38{
39 enum object_type type;
40 char *buf;
41 unsigned long size;
42 int ret;
43
0df8e965 44 type = oid_object_info(the_repository, oid, NULL);
45a227ef
ST
45 if (type != OBJ_TAG)
46 return error("%s: cannot verify a non-tag object of type %s.",
47 name_to_report ?
48 name_to_report :
aab9583f 49 find_unique_abbrev(oid, DEFAULT_ABBREV),
debca9d2 50 type_name(type));
45a227ef 51
b4f5aca4 52 buf = read_object_file(oid, &type, &size);
45a227ef
ST
53 if (!buf)
54 return error("%s: unable to read file.",
55 name_to_report ?
56 name_to_report :
aab9583f 57 find_unique_abbrev(oid, DEFAULT_ABBREV));
45a227ef
ST
58
59 ret = run_gpg_verify(buf, size, flags);
60
61 free(buf);
62 return ret;
63}
64
9534f40b 65struct object *deref_tag(struct object *o, const char *warn, int warnlen)
37fde874 66{
1974632c 67 while (o && o->type == OBJ_TAG)
24e8a3c9 68 if (((struct tag *)o)->tagged)
c251c83d 69 o = parse_object(&((struct tag *)o)->tagged->oid);
24e8a3c9
MK
70 else
71 o = NULL;
9534f40b
JH
72 if (!o && warn) {
73 if (!warnlen)
74 warnlen = strlen(warn);
75 error("missing object referenced by '%.*s'", warnlen, warn);
76 }
37fde874
JH
77 return o;
78}
79
90108a24
JK
80struct object *deref_tag_noverify(struct object *o)
81{
82 while (o && o->type == OBJ_TAG) {
c251c83d 83 o = parse_object(&o->oid);
90108a24
JK
84 if (o && o->type == OBJ_TAG && ((struct tag *)o)->tagged)
85 o = ((struct tag *)o)->tagged;
86 else
87 o = NULL;
88 }
89 return o;
90}
91
d3101b53 92struct tag *lookup_tag(const struct object_id *oid)
2636f614 93{
d3101b53 94 struct object *obj = lookup_object(oid->hash);
100c5f3b 95 if (!obj)
68f95d38
SB
96 return create_object(the_repository, oid->hash,
97 alloc_tag_node());
8ff226a9 98 return object_as_type(obj, OBJ_TAG, 0);
2636f614
DB
99}
100
dddbad72 101static timestamp_t parse_tag_date(const char *buf, const char *tail)
e451d06b
SP
102{
103 const char *dateptr;
104
105 while (buf < tail && *buf++ != '>')
106 /* nada */;
107 if (buf >= tail)
108 return 0;
109 dateptr = buf;
110 while (buf < tail && *buf++ != '\n')
111 /* nada */;
112 if (buf >= tail)
113 return 0;
1aeb7e75
JS
114 /* dateptr < buf && buf[-1] == '\n', so parsing will stop at buf-1 */
115 return parse_timestamp(dateptr, NULL, 10);
e451d06b
SP
116}
117
cf7b1cad 118int parse_tag_buffer(struct tag *item, const void *data, unsigned long size)
2636f614 119{
1e4085a0 120 struct object_id oid;
89e4202f 121 char type[20];
28de5b6b
SP
122 const char *bufptr = data;
123 const char *tail = bufptr + size;
124 const char *nl;
ae200ee5 125
2e0052a5
SP
126 if (item->object.parsed)
127 return 0;
128 item->object.parsed = 1;
2636f614 129
1e4085a0 130 if (size < GIT_SHA1_HEXSZ + 24)
bd2c39f5 131 return -1;
1e4085a0 132 if (memcmp("object ", bufptr, 7) || parse_oid_hex(bufptr + 7, &oid, &bufptr) || *bufptr++ != '\n')
bd2c39f5 133 return -1;
2636f614 134
59556548 135 if (!starts_with(bufptr, "type "))
bd2c39f5 136 return -1;
28de5b6b
SP
137 bufptr += 5;
138 nl = memchr(bufptr, '\n', tail - bufptr);
139 if (!nl || sizeof(type) <= (nl - bufptr))
bd2c39f5 140 return -1;
eddda371 141 memcpy(type, bufptr, nl - bufptr);
28de5b6b
SP
142 type[nl - bufptr] = '\0';
143 bufptr = nl + 1;
2636f614 144
0ab17950 145 if (!strcmp(type, blob_type)) {
7099153e 146 item->tagged = (struct object *)lookup_blob(&oid);
0ab17950 147 } else if (!strcmp(type, tree_type)) {
7099153e 148 item->tagged = (struct object *)lookup_tree(&oid);
0ab17950 149 } else if (!strcmp(type, commit_type)) {
7099153e 150 item->tagged = (struct object *)lookup_commit(&oid);
0ab17950 151 } else if (!strcmp(type, tag_type)) {
7099153e 152 item->tagged = (struct object *)lookup_tag(&oid);
0ab17950
NP
153 } else {
154 error("Unknown type %s", type);
155 item->tagged = NULL;
156 }
157
59556548 158 if (bufptr + 4 < tail && starts_with(bufptr, "tag "))
85594252
NTND
159 ; /* good */
160 else
28de5b6b
SP
161 return -1;
162 bufptr += 4;
163 nl = memchr(bufptr, '\n', tail - bufptr);
164 if (!nl)
165 return -1;
166 item->tag = xmemdupz(bufptr, nl - bufptr);
167 bufptr = nl + 1;
168
59556548 169 if (bufptr + 7 < tail && starts_with(bufptr, "tagger "))
e451d06b
SP
170 item->date = parse_tag_date(bufptr, tail);
171 else
172 item->date = 0;
173
2636f614 174 return 0;
bd2c39f5 175}
13019d41 176
bd2c39f5
NP
177int parse_tag(struct tag *item)
178{
21666f1a 179 enum object_type type;
bd2c39f5
NP
180 void *data;
181 unsigned long size;
182 int ret;
183
184 if (item->object.parsed)
185 return 0;
b4f5aca4 186 data = read_object_file(&item->object.oid, &type, &size);
bd2c39f5
NP
187 if (!data)
188 return error("Could not read %s",
f2fd0760 189 oid_to_hex(&item->object.oid));
21666f1a 190 if (type != OBJ_TAG) {
bd2c39f5
NP
191 free(data);
192 return error("Object %s not a tag",
f2fd0760 193 oid_to_hex(&item->object.oid));
bd2c39f5
NP
194 }
195 ret = parse_tag_buffer(item, data, size);
13019d41 196 free(data);
bd2c39f5 197 return ret;
2636f614 198}