]> git.ipfire.org Git - thirdparty/git.git/blame - tag.c
path.h: move function declarations for path.c functions from cache.h
[thirdparty/git.git] / tag.c
CommitLineData
2636f614 1#include "cache.h"
8f1d2e6f 2#include "tag.h"
cbd53a21 3#include "object-store.h"
0ab17950
NP
4#include "commit.h"
5#include "tree.h"
6#include "blob.h"
14ba97f8 7#include "alloc.h"
94240b91 8#include "gpg-interface.h"
41771fa4 9#include "hex.h"
8c4cc326 10#include "packfile.h"
2636f614
DB
11
12const char *tag_type = "tag";
13
45a227ef
ST
14static int run_gpg_verify(const char *buf, unsigned long size, unsigned flags)
15{
16 struct signature_check sigc;
482c1191 17 struct strbuf payload = STRBUF_INIT;
18 struct strbuf signature = STRBUF_INIT;
45a227ef
ST
19 int ret;
20
21 memset(&sigc, 0, sizeof(sigc));
22
482c1191 23 if (!parse_signature(buf, size, &payload, &signature)) {
45a227ef 24 if (flags & GPG_VERIFY_VERBOSE)
482c1191 25 write_in_full(1, buf, size);
45a227ef
ST
26 return error("no signature found");
27 }
28
dd3aa418 29 sigc.payload_type = SIGNATURE_PAYLOAD_TAG;
02769437
FS
30 sigc.payload = strbuf_detach(&payload, &sigc.payload_len);
31 ret = check_signature(&sigc, signature.buf, signature.len);
94240b91
LP
32
33 if (!(flags & GPG_VERIFY_OMIT_STATUS))
34 print_signature_buffer(&sigc, flags);
45a227ef
ST
35
36 signature_check_clear(&sigc);
482c1191 37 strbuf_release(&payload);
38 strbuf_release(&signature);
45a227ef
ST
39 return ret;
40}
41
84571760 42int gpg_verify_tag(const struct object_id *oid, const char *name_to_report,
45a227ef
ST
43 unsigned flags)
44{
45 enum object_type type;
46 char *buf;
47 unsigned long size;
48 int ret;
49
0df8e965 50 type = oid_object_info(the_repository, oid, NULL);
45a227ef
ST
51 if (type != OBJ_TAG)
52 return error("%s: cannot verify a non-tag object of type %s.",
53 name_to_report ?
54 name_to_report :
aab9583f 55 find_unique_abbrev(oid, DEFAULT_ABBREV),
debca9d2 56 type_name(type));
45a227ef 57
b4f5aca4 58 buf = read_object_file(oid, &type, &size);
45a227ef
ST
59 if (!buf)
60 return error("%s: unable to read file.",
61 name_to_report ?
62 name_to_report :
aab9583f 63 find_unique_abbrev(oid, DEFAULT_ABBREV));
45a227ef
ST
64
65 ret = run_gpg_verify(buf, size, flags);
66
67 free(buf);
68 return ret;
69}
70
286d258d 71struct object *deref_tag(struct repository *r, struct object *o, const char *warn, int warnlen)
37fde874 72{
8c4cc326 73 struct object_id *last_oid = NULL;
1974632c 74 while (o && o->type == OBJ_TAG)
8c4cc326
JT
75 if (((struct tag *)o)->tagged) {
76 last_oid = &((struct tag *)o)->tagged->oid;
09ca6130 77 o = parse_object(r, last_oid);
8c4cc326
JT
78 } else {
79 last_oid = NULL;
24e8a3c9 80 o = NULL;
8c4cc326 81 }
9534f40b 82 if (!o && warn) {
8c4cc326
JT
83 if (last_oid && is_promisor_object(last_oid))
84 return NULL;
9534f40b
JH
85 if (!warnlen)
86 warnlen = strlen(warn);
87 error("missing object referenced by '%.*s'", warnlen, warn);
88 }
37fde874
JH
89 return o;
90}
91
90108a24
JK
92struct object *deref_tag_noverify(struct object *o)
93{
94 while (o && o->type == OBJ_TAG) {
109cd76d 95 o = parse_object(the_repository, &o->oid);
90108a24
JK
96 if (o && o->type == OBJ_TAG && ((struct tag *)o)->tagged)
97 o = ((struct tag *)o)->tagged;
98 else
99 o = NULL;
100 }
101 return o;
102}
103
8bde69b9 104struct tag *lookup_tag(struct repository *r, const struct object_id *oid)
2636f614 105{
d0229abd 106 struct object *obj = lookup_object(r, oid);
100c5f3b 107 if (!obj)
a378509e 108 return create_object(r, oid, alloc_tag_node(r));
6da43d93 109 return object_as_type(obj, OBJ_TAG, 0);
2636f614
DB
110}
111
dddbad72 112static timestamp_t parse_tag_date(const char *buf, const char *tail)
e451d06b
SP
113{
114 const char *dateptr;
115
116 while (buf < tail && *buf++ != '>')
117 /* nada */;
118 if (buf >= tail)
119 return 0;
120 dateptr = buf;
121 while (buf < tail && *buf++ != '\n')
122 /* nada */;
123 if (buf >= tail)
124 return 0;
1aeb7e75
JS
125 /* dateptr < buf && buf[-1] == '\n', so parsing will stop at buf-1 */
126 return parse_timestamp(dateptr, NULL, 10);
e451d06b
SP
127}
128
14ba97f8
SB
129void release_tag_memory(struct tag *t)
130{
131 free(t->tag);
132 t->tagged = NULL;
133 t->object.parsed = 0;
134 t->date = 0;
135}
136
84f80cd2 137int parse_tag_buffer(struct repository *r, struct tag *item, const void *data, unsigned long size)
2636f614 138{
1e4085a0 139 struct object_id oid;
89e4202f 140 char type[20];
28de5b6b
SP
141 const char *bufptr = data;
142 const char *tail = bufptr + size;
143 const char *nl;
ae200ee5 144
2e0052a5
SP
145 if (item->object.parsed)
146 return 0;
228c78fb
JK
147
148 if (item->tag) {
149 /*
150 * Presumably left over from a previous failed parse;
151 * clear it out in preparation for re-parsing (we'll probably
152 * hit the same error, which lets us tell our current caller
153 * about the problem).
154 */
155 FREE_AND_NULL(item->tag);
156 }
2636f614 157
d8a3a690 158 if (size < the_hash_algo->hexsz + 24)
bd2c39f5 159 return -1;
1e4085a0 160 if (memcmp("object ", bufptr, 7) || parse_oid_hex(bufptr + 7, &oid, &bufptr) || *bufptr++ != '\n')
bd2c39f5 161 return -1;
2636f614 162
59556548 163 if (!starts_with(bufptr, "type "))
bd2c39f5 164 return -1;
28de5b6b
SP
165 bufptr += 5;
166 nl = memchr(bufptr, '\n', tail - bufptr);
167 if (!nl || sizeof(type) <= (nl - bufptr))
bd2c39f5 168 return -1;
eddda371 169 memcpy(type, bufptr, nl - bufptr);
28de5b6b
SP
170 type[nl - bufptr] = '\0';
171 bufptr = nl + 1;
2636f614 172
0ab17950 173 if (!strcmp(type, blob_type)) {
84f80cd2 174 item->tagged = (struct object *)lookup_blob(r, &oid);
0ab17950 175 } else if (!strcmp(type, tree_type)) {
84f80cd2 176 item->tagged = (struct object *)lookup_tree(r, &oid);
0ab17950 177 } else if (!strcmp(type, commit_type)) {
84f80cd2 178 item->tagged = (struct object *)lookup_commit(r, &oid);
0ab17950 179 } else if (!strcmp(type, tag_type)) {
84f80cd2 180 item->tagged = (struct object *)lookup_tag(r, &oid);
0ab17950 181 } else {
78d50148
JK
182 return error("unknown tag type '%s' in %s",
183 type, oid_to_hex(&item->object.oid));
0ab17950
NP
184 }
185
78d50148
JK
186 if (!item->tagged)
187 return error("bad tag pointer to %s in %s",
188 oid_to_hex(&oid),
189 oid_to_hex(&item->object.oid));
190
59556548 191 if (bufptr + 4 < tail && starts_with(bufptr, "tag "))
85594252
NTND
192 ; /* good */
193 else
28de5b6b
SP
194 return -1;
195 bufptr += 4;
196 nl = memchr(bufptr, '\n', tail - bufptr);
197 if (!nl)
198 return -1;
199 item->tag = xmemdupz(bufptr, nl - bufptr);
200 bufptr = nl + 1;
201
59556548 202 if (bufptr + 7 < tail && starts_with(bufptr, "tagger "))
e451d06b
SP
203 item->date = parse_tag_date(bufptr, tail);
204 else
205 item->date = 0;
206
228c78fb 207 item->object.parsed = 1;
2636f614 208 return 0;
bd2c39f5 209}
13019d41 210
bd2c39f5
NP
211int parse_tag(struct tag *item)
212{
21666f1a 213 enum object_type type;
bd2c39f5
NP
214 void *data;
215 unsigned long size;
216 int ret;
217
218 if (item->object.parsed)
219 return 0;
b4f5aca4 220 data = read_object_file(&item->object.oid, &type, &size);
bd2c39f5
NP
221 if (!data)
222 return error("Could not read %s",
f2fd0760 223 oid_to_hex(&item->object.oid));
21666f1a 224 if (type != OBJ_TAG) {
bd2c39f5
NP
225 free(data);
226 return error("Object %s not a tag",
f2fd0760 227 oid_to_hex(&item->object.oid));
bd2c39f5 228 }
0e740fed 229 ret = parse_tag_buffer(the_repository, item, data, size);
13019d41 230 free(data);
bd2c39f5 231 return ret;
2636f614 232}
dad3f060
RS
233
234struct object_id *get_tagged_oid(struct tag *tag)
235{
236 if (!tag->tagged)
237 die("bad tag");
238 return &tag->tagged->oid;
239}