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"
15 const char *tag_type
= "tag";
17 static int run_gpg_verify(const char *buf
, unsigned long size
, unsigned flags
)
19 struct signature_check sigc
;
20 struct strbuf payload
= STRBUF_INIT
;
21 struct strbuf signature
= STRBUF_INIT
;
24 memset(&sigc
, 0, sizeof(sigc
));
26 if (!parse_signature(buf
, size
, &payload
, &signature
)) {
27 if (flags
& GPG_VERIFY_VERBOSE
)
28 write_in_full(1, buf
, size
);
29 return error("no signature found");
32 sigc
.payload_type
= SIGNATURE_PAYLOAD_TAG
;
33 sigc
.payload
= strbuf_detach(&payload
, &sigc
.payload_len
);
34 ret
= check_signature(&sigc
, signature
.buf
, signature
.len
);
36 if (!(flags
& GPG_VERIFY_OMIT_STATUS
))
37 print_signature_buffer(&sigc
, flags
);
39 signature_check_clear(&sigc
);
40 strbuf_release(&payload
);
41 strbuf_release(&signature
);
45 int gpg_verify_tag(const struct object_id
*oid
, const char *name_to_report
,
48 enum object_type type
;
53 type
= oid_object_info(the_repository
, oid
, NULL
);
55 return error("%s: cannot verify a non-tag object of type %s.",
58 repo_find_unique_abbrev(the_repository
, oid
, DEFAULT_ABBREV
),
61 buf
= repo_read_object_file(the_repository
, oid
, &type
, &size
);
63 return error("%s: unable to read file.",
66 repo_find_unique_abbrev(the_repository
, oid
, DEFAULT_ABBREV
));
68 ret
= run_gpg_verify(buf
, size
, flags
);
74 struct object
*deref_tag(struct repository
*r
, struct object
*o
, const char *warn
, int warnlen
)
76 struct object_id
*last_oid
= NULL
;
77 while (o
&& o
->type
== OBJ_TAG
)
78 if (((struct tag
*)o
)->tagged
) {
79 last_oid
= &((struct tag
*)o
)->tagged
->oid
;
80 o
= parse_object(r
, last_oid
);
86 if (last_oid
&& is_promisor_object(last_oid
))
89 warnlen
= strlen(warn
);
90 error("missing object referenced by '%.*s'", warnlen
, warn
);
95 struct object
*deref_tag_noverify(struct object
*o
)
97 while (o
&& o
->type
== OBJ_TAG
) {
98 o
= parse_object(the_repository
, &o
->oid
);
99 if (o
&& o
->type
== OBJ_TAG
&& ((struct tag
*)o
)->tagged
)
100 o
= ((struct tag
*)o
)->tagged
;
107 struct tag
*lookup_tag(struct repository
*r
, const struct object_id
*oid
)
109 struct object
*obj
= lookup_object(r
, oid
);
111 return create_object(r
, oid
, alloc_tag_node(r
));
112 return object_as_type(obj
, OBJ_TAG
, 0);
115 static timestamp_t
parse_tag_date(const char *buf
, const char *tail
)
119 while (buf
< tail
&& *buf
++ != '>')
124 while (buf
< tail
&& *buf
++ != '\n')
128 /* dateptr < buf && buf[-1] == '\n', so parsing will stop at buf-1 */
129 return parse_timestamp(dateptr
, NULL
, 10);
132 void release_tag_memory(struct tag
*t
)
136 t
->object
.parsed
= 0;
140 int parse_tag_buffer(struct repository
*r
, struct tag
*item
, const void *data
, unsigned long size
)
142 struct object_id oid
;
144 const char *bufptr
= data
;
145 const char *tail
= bufptr
+ size
;
148 if (item
->object
.parsed
)
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).
158 FREE_AND_NULL(item
->tag
);
161 if (size
< the_hash_algo
->hexsz
+ 24)
163 if (memcmp("object ", bufptr
, 7) || parse_oid_hex(bufptr
+ 7, &oid
, &bufptr
) || *bufptr
++ != '\n')
166 if (!starts_with(bufptr
, "type "))
169 nl
= memchr(bufptr
, '\n', tail
- bufptr
);
170 if (!nl
|| sizeof(type
) <= (nl
- bufptr
))
172 memcpy(type
, bufptr
, nl
- bufptr
);
173 type
[nl
- bufptr
] = '\0';
176 if (!strcmp(type
, blob_type
)) {
177 item
->tagged
= (struct object
*)lookup_blob(r
, &oid
);
178 } else if (!strcmp(type
, tree_type
)) {
179 item
->tagged
= (struct object
*)lookup_tree(r
, &oid
);
180 } else if (!strcmp(type
, commit_type
)) {
181 item
->tagged
= (struct object
*)lookup_commit(r
, &oid
);
182 } else if (!strcmp(type
, tag_type
)) {
183 item
->tagged
= (struct object
*)lookup_tag(r
, &oid
);
185 return error("unknown tag type '%s' in %s",
186 type
, oid_to_hex(&item
->object
.oid
));
190 return error("bad tag pointer to %s in %s",
192 oid_to_hex(&item
->object
.oid
));
194 if (bufptr
+ 4 < tail
&& starts_with(bufptr
, "tag "))
199 nl
= memchr(bufptr
, '\n', tail
- bufptr
);
202 item
->tag
= xmemdupz(bufptr
, nl
- bufptr
);
205 if (bufptr
+ 7 < tail
&& starts_with(bufptr
, "tagger "))
206 item
->date
= parse_tag_date(bufptr
, tail
);
210 item
->object
.parsed
= 1;
214 int parse_tag(struct tag
*item
)
216 enum object_type type
;
221 if (item
->object
.parsed
)
223 data
= repo_read_object_file(the_repository
, &item
->object
.oid
, &type
,
226 return error("Could not read %s",
227 oid_to_hex(&item
->object
.oid
));
228 if (type
!= OBJ_TAG
) {
230 return error("Object %s not a tag",
231 oid_to_hex(&item
->object
.oid
));
233 ret
= parse_tag_buffer(the_repository
, item
, data
, size
);
238 struct object_id
*get_tagged_oid(struct tag
*tag
)
242 return &tag
->tagged
->oid
;