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