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