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