pretty: drop print_email_subject flag
[alt-git.git] / tag.c
blobfc3834db467dc4e83ca0a24da61a3f39048f3ec2
1 #include "git-compat-util.h"
2 #include "environment.h"
3 #include "tag.h"
4 #include "object-name.h"
5 #include "object-store-ll.h"
6 #include "commit.h"
7 #include "tree.h"
8 #include "blob.h"
9 #include "alloc.h"
10 #include "gpg-interface.h"
11 #include "hex.h"
12 #include "packfile.h"
14 const char *tag_type = "tag";
16 static int run_gpg_verify(const char *buf, unsigned long size, unsigned flags)
18 struct signature_check sigc;
19 struct strbuf payload = STRBUF_INIT;
20 struct strbuf signature = STRBUF_INIT;
21 int ret;
23 memset(&sigc, 0, sizeof(sigc));
25 if (!parse_signature(buf, size, &payload, &signature)) {
26 if (flags & GPG_VERIFY_VERBOSE)
27 write_in_full(1, buf, size);
28 return error("no signature found");
31 sigc.payload_type = SIGNATURE_PAYLOAD_TAG;
32 sigc.payload = strbuf_detach(&payload, &sigc.payload_len);
33 ret = check_signature(&sigc, signature.buf, signature.len);
35 if (!(flags & GPG_VERIFY_OMIT_STATUS))
36 print_signature_buffer(&sigc, flags);
38 signature_check_clear(&sigc);
39 strbuf_release(&payload);
40 strbuf_release(&signature);
41 return ret;
44 int gpg_verify_tag(const struct object_id *oid, const char *name_to_report,
45 unsigned flags)
47 enum object_type type;
48 char *buf;
49 unsigned long size;
50 int ret;
52 type = oid_object_info(the_repository, oid, NULL);
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 :
57 repo_find_unique_abbrev(the_repository, oid, DEFAULT_ABBREV),
58 type_name(type));
60 buf = repo_read_object_file(the_repository, oid, &type, &size);
61 if (!buf)
62 return error("%s: unable to read file.",
63 name_to_report ?
64 name_to_report :
65 repo_find_unique_abbrev(the_repository, oid, DEFAULT_ABBREV));
67 ret = run_gpg_verify(buf, size, flags);
69 free(buf);
70 return ret;
73 struct object *deref_tag(struct repository *r, struct object *o, const char *warn, int warnlen)
75 struct object_id *last_oid = NULL;
76 while (o && o->type == OBJ_TAG)
77 if (((struct tag *)o)->tagged) {
78 last_oid = &((struct tag *)o)->tagged->oid;
79 o = parse_object(r, last_oid);
80 } else {
81 last_oid = NULL;
82 o = NULL;
84 if (!o && warn) {
85 if (last_oid && is_promisor_object(last_oid))
86 return NULL;
87 if (!warnlen)
88 warnlen = strlen(warn);
89 error("missing object referenced by '%.*s'", warnlen, warn);
91 return o;
94 struct object *deref_tag_noverify(struct object *o)
96 while (o && o->type == OBJ_TAG) {
97 o = parse_object(the_repository, &o->oid);
98 if (o && o->type == OBJ_TAG && ((struct tag *)o)->tagged)
99 o = ((struct tag *)o)->tagged;
100 else
101 o = NULL;
103 return o;
106 struct tag *lookup_tag(struct repository *r, const struct object_id *oid)
108 struct object *obj = lookup_object(r, oid);
109 if (!obj)
110 return create_object(r, oid, alloc_tag_node(r));
111 return object_as_type(obj, OBJ_TAG, 0);
114 static timestamp_t parse_tag_date(const char *buf, const char *tail)
116 const char *dateptr;
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;
127 /* dateptr < buf && buf[-1] == '\n', so parsing will stop at buf-1 */
128 return parse_timestamp(dateptr, NULL, 10);
131 void release_tag_memory(struct tag *t)
133 free(t->tag);
134 t->tagged = NULL;
135 t->object.parsed = 0;
136 t->date = 0;
139 int parse_tag_buffer(struct repository *r, struct tag *item, const void *data, unsigned long size)
141 struct object_id oid;
142 char type[20];
143 const char *bufptr = data;
144 const char *tail = bufptr + size;
145 const char *nl;
147 if (item->object.parsed)
148 return 0;
150 if (item->tag) {
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).
157 FREE_AND_NULL(item->tag);
160 if (size < the_hash_algo->hexsz + 24)
161 return -1;
162 if (memcmp("object ", bufptr, 7) || parse_oid_hex(bufptr + 7, &oid, &bufptr) || *bufptr++ != '\n')
163 return -1;
165 if (!starts_with(bufptr, "type "))
166 return -1;
167 bufptr += 5;
168 nl = memchr(bufptr, '\n', tail - bufptr);
169 if (!nl || sizeof(type) <= (nl - bufptr))
170 return -1;
171 memcpy(type, bufptr, nl - bufptr);
172 type[nl - bufptr] = '\0';
173 bufptr = nl + 1;
175 if (!strcmp(type, blob_type)) {
176 item->tagged = (struct object *)lookup_blob(r, &oid);
177 } else if (!strcmp(type, tree_type)) {
178 item->tagged = (struct object *)lookup_tree(r, &oid);
179 } else if (!strcmp(type, commit_type)) {
180 item->tagged = (struct object *)lookup_commit(r, &oid);
181 } else if (!strcmp(type, tag_type)) {
182 item->tagged = (struct object *)lookup_tag(r, &oid);
183 } else {
184 return error("unknown tag type '%s' in %s",
185 type, oid_to_hex(&item->object.oid));
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));
193 if (bufptr + 4 < tail && starts_with(bufptr, "tag "))
194 ; /* good */
195 else
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;
204 if (bufptr + 7 < tail && starts_with(bufptr, "tagger "))
205 item->date = parse_tag_date(bufptr, tail);
206 else
207 item->date = 0;
209 item->object.parsed = 1;
210 return 0;
213 int parse_tag(struct tag *item)
215 enum object_type type;
216 void *data;
217 unsigned long size;
218 int ret;
220 if (item->object.parsed)
221 return 0;
222 data = repo_read_object_file(the_repository, &item->object.oid, &type,
223 &size);
224 if (!data)
225 return error("Could not read %s",
226 oid_to_hex(&item->object.oid));
227 if (type != OBJ_TAG) {
228 free(data);
229 return error("Object %s not a tag",
230 oid_to_hex(&item->object.oid));
232 ret = parse_tag_buffer(the_repository, item, data, size);
233 free(data);
234 return ret;
237 struct object_id *get_tagged_oid(struct tag *tag)
239 if (!tag->tagged)
240 die("bad tag");
241 return &tag->tagged->oid;