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