1 #define USE_THE_REPOSITORY_VARIABLE
3 #include "git-compat-util.h"
4 #include "environment.h"
6 #include "object-name.h"
7 #include "object-store-ll.h"
12 #include "gpg-interface.h"
16 const char *tag_type
= "tag";
18 static int run_gpg_verify(const char *buf
, unsigned long size
, unsigned flags
)
20 struct signature_check sigc
;
21 struct strbuf payload
= STRBUF_INIT
;
22 struct strbuf signature
= STRBUF_INIT
;
25 memset(&sigc
, 0, sizeof(sigc
));
27 if (!parse_signature(buf
, size
, &payload
, &signature
)) {
28 if (flags
& GPG_VERIFY_VERBOSE
)
29 write_in_full(1, buf
, size
);
30 return error("no signature found");
33 sigc
.payload_type
= SIGNATURE_PAYLOAD_TAG
;
34 sigc
.payload
= strbuf_detach(&payload
, &sigc
.payload_len
);
35 ret
= check_signature(&sigc
, signature
.buf
, signature
.len
);
37 if (!(flags
& GPG_VERIFY_OMIT_STATUS
))
38 print_signature_buffer(&sigc
, flags
);
40 signature_check_clear(&sigc
);
41 strbuf_release(&payload
);
42 strbuf_release(&signature
);
46 int gpg_verify_tag(const struct object_id
*oid
, const char *name_to_report
,
49 enum object_type type
;
54 type
= oid_object_info(the_repository
, oid
, NULL
);
56 return error("%s: cannot verify a non-tag object of type %s.",
59 repo_find_unique_abbrev(the_repository
, oid
, DEFAULT_ABBREV
),
62 buf
= repo_read_object_file(the_repository
, oid
, &type
, &size
);
64 return error("%s: unable to read file.",
67 repo_find_unique_abbrev(the_repository
, oid
, DEFAULT_ABBREV
));
69 ret
= run_gpg_verify(buf
, size
, flags
);
75 struct object
*deref_tag(struct repository
*r
, struct object
*o
, const char *warn
, int warnlen
)
77 struct object_id
*last_oid
= NULL
;
78 while (o
&& o
->type
== OBJ_TAG
)
79 if (((struct tag
*)o
)->tagged
) {
80 last_oid
= &((struct tag
*)o
)->tagged
->oid
;
81 o
= parse_object(r
, last_oid
);
87 if (last_oid
&& is_promisor_object(last_oid
))
90 warnlen
= strlen(warn
);
91 error("missing object referenced by '%.*s'", warnlen
, warn
);
96 struct object
*deref_tag_noverify(struct repository
*r
, struct object
*o
)
98 while (o
&& o
->type
== OBJ_TAG
) {
99 o
= parse_object(r
, &o
->oid
);
100 if (o
&& o
->type
== OBJ_TAG
&& ((struct tag
*)o
)->tagged
)
101 o
= ((struct tag
*)o
)->tagged
;
108 struct tag
*lookup_tag(struct repository
*r
, const struct object_id
*oid
)
110 struct object
*obj
= lookup_object(r
, oid
);
112 return create_object(r
, oid
, alloc_tag_node(r
));
113 return object_as_type(obj
, OBJ_TAG
, 0);
116 static timestamp_t
parse_tag_date(const char *buf
, const char *tail
)
120 while (buf
< tail
&& *buf
++ != '>')
125 while (buf
< tail
&& *buf
++ != '\n')
129 /* dateptr < buf && buf[-1] == '\n', so parsing will stop at buf-1 */
130 return parse_timestamp(dateptr
, NULL
, 10);
133 void release_tag_memory(struct tag
*t
)
137 t
->object
.parsed
= 0;
141 int parse_tag_buffer(struct repository
*r
, struct tag
*item
, const void *data
, unsigned long size
)
143 struct object_id oid
;
145 const char *bufptr
= data
;
146 const char *tail
= bufptr
+ size
;
149 if (item
->object
.parsed
)
154 * Presumably left over from a previous failed parse;
155 * clear it out in preparation for re-parsing (we'll probably
156 * hit the same error, which lets us tell our current caller
157 * about the problem).
159 FREE_AND_NULL(item
->tag
);
162 if (size
< the_hash_algo
->hexsz
+ 24)
164 if (memcmp("object ", bufptr
, 7) || parse_oid_hex(bufptr
+ 7, &oid
, &bufptr
) || *bufptr
++ != '\n')
167 if (!starts_with(bufptr
, "type "))
170 nl
= memchr(bufptr
, '\n', tail
- bufptr
);
171 if (!nl
|| sizeof(type
) <= (nl
- bufptr
))
173 memcpy(type
, bufptr
, nl
- bufptr
);
174 type
[nl
- bufptr
] = '\0';
177 if (!strcmp(type
, blob_type
)) {
178 item
->tagged
= (struct object
*)lookup_blob(r
, &oid
);
179 } else if (!strcmp(type
, tree_type
)) {
180 item
->tagged
= (struct object
*)lookup_tree(r
, &oid
);
181 } else if (!strcmp(type
, commit_type
)) {
182 item
->tagged
= (struct object
*)lookup_commit(r
, &oid
);
183 } else if (!strcmp(type
, tag_type
)) {
184 item
->tagged
= (struct object
*)lookup_tag(r
, &oid
);
186 return error("unknown tag type '%s' in %s",
187 type
, oid_to_hex(&item
->object
.oid
));
191 return error("bad tag pointer to %s in %s",
193 oid_to_hex(&item
->object
.oid
));
195 if (bufptr
+ 4 < tail
&& starts_with(bufptr
, "tag "))
200 nl
= memchr(bufptr
, '\n', tail
- bufptr
);
203 item
->tag
= xmemdupz(bufptr
, nl
- bufptr
);
206 if (bufptr
+ 7 < tail
&& starts_with(bufptr
, "tagger "))
207 item
->date
= parse_tag_date(bufptr
, tail
);
211 item
->object
.parsed
= 1;
215 int parse_tag(struct tag
*item
)
217 enum object_type type
;
222 if (item
->object
.parsed
)
224 data
= repo_read_object_file(the_repository
, &item
->object
.oid
, &type
,
227 return error("Could not read %s",
228 oid_to_hex(&item
->object
.oid
));
229 if (type
!= OBJ_TAG
) {
231 return error("Object %s not a tag",
232 oid_to_hex(&item
->object
.oid
));
234 ret
= parse_tag_buffer(the_repository
, item
, data
, size
);
239 struct object_id
*get_tagged_oid(struct tag
*tag
)
243 return &tag
->tagged
->oid
;