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