1 #include "git-compat-util.h"
2 #include "environment.h"
4 #include "object-name.h"
5 #include "object-store-ll.h"
10 #include "gpg-interface.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
;
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
);
44 int gpg_verify_tag(const struct object_id
*oid
, const char *name_to_report
,
47 enum object_type type
;
52 type
= oid_object_info(the_repository
, oid
, NULL
);
54 return error("%s: cannot verify a non-tag object of type %s.",
57 repo_find_unique_abbrev(the_repository
, oid
, DEFAULT_ABBREV
),
60 buf
= repo_read_object_file(the_repository
, oid
, &type
, &size
);
62 return error("%s: unable to read file.",
65 repo_find_unique_abbrev(the_repository
, oid
, DEFAULT_ABBREV
));
67 ret
= run_gpg_verify(buf
, size
, flags
);
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
);
85 if (last_oid
&& is_promisor_object(last_oid
))
88 warnlen
= strlen(warn
);
89 error("missing object referenced by '%.*s'", warnlen
, warn
);
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
;
106 struct tag
*lookup_tag(struct repository
*r
, const struct object_id
*oid
)
108 struct object
*obj
= lookup_object(r
, oid
);
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
)
118 while (buf
< tail
&& *buf
++ != '>')
123 while (buf
< tail
&& *buf
++ != '\n')
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
)
135 t
->object
.parsed
= 0;
139 int parse_tag_buffer(struct repository
*r
, struct tag
*item
, const void *data
, unsigned long size
)
141 struct object_id oid
;
143 const char *bufptr
= data
;
144 const char *tail
= bufptr
+ size
;
147 if (item
->object
.parsed
)
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)
162 if (memcmp("object ", bufptr
, 7) || parse_oid_hex(bufptr
+ 7, &oid
, &bufptr
) || *bufptr
++ != '\n')
165 if (!starts_with(bufptr
, "type "))
168 nl
= memchr(bufptr
, '\n', tail
- bufptr
);
169 if (!nl
|| sizeof(type
) <= (nl
- bufptr
))
171 memcpy(type
, bufptr
, nl
- bufptr
);
172 type
[nl
- bufptr
] = '\0';
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
);
184 return error("unknown tag type '%s' in %s",
185 type
, oid_to_hex(&item
->object
.oid
));
189 return error("bad tag pointer to %s in %s",
191 oid_to_hex(&item
->object
.oid
));
193 if (bufptr
+ 4 < tail
&& starts_with(bufptr
, "tag "))
198 nl
= memchr(bufptr
, '\n', tail
- bufptr
);
201 item
->tag
= xmemdupz(bufptr
, nl
- bufptr
);
204 if (bufptr
+ 7 < tail
&& starts_with(bufptr
, "tagger "))
205 item
->date
= parse_tag_date(bufptr
, tail
);
209 item
->object
.parsed
= 1;
213 int parse_tag(struct tag
*item
)
215 enum object_type type
;
220 if (item
->object
.parsed
)
222 data
= repo_read_object_file(the_repository
, &item
->object
.oid
, &type
,
225 return error("Could not read %s",
226 oid_to_hex(&item
->object
.oid
));
227 if (type
!= OBJ_TAG
) {
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
);
237 struct object_id
*get_tagged_oid(struct tag
*tag
)
241 return &tag
->tagged
->oid
;