1 #define USE_THE_REPOSITORY_VARIABLE
2 #define DISABLE_SIGN_COMPARE_WARNINGS
4 #include "git-compat-util.h"
5 #include "environment.h"
7 #include "object-name.h"
8 #include "object-store-ll.h"
13 #include "gpg-interface.h"
17 const char *tag_type
= "tag";
19 static int run_gpg_verify(const char *buf
, unsigned long size
, unsigned flags
)
21 struct signature_check sigc
;
22 struct strbuf payload
= STRBUF_INIT
;
23 struct strbuf signature
= STRBUF_INIT
;
26 memset(&sigc
, 0, sizeof(sigc
));
28 if (!parse_signature(buf
, size
, &payload
, &signature
)) {
29 if (flags
& GPG_VERIFY_VERBOSE
)
30 write_in_full(1, buf
, size
);
31 return error("no signature found");
34 sigc
.payload_type
= SIGNATURE_PAYLOAD_TAG
;
35 sigc
.payload
= strbuf_detach(&payload
, &sigc
.payload_len
);
36 ret
= check_signature(&sigc
, signature
.buf
, signature
.len
);
38 if (!(flags
& GPG_VERIFY_OMIT_STATUS
))
39 print_signature_buffer(&sigc
, flags
);
41 signature_check_clear(&sigc
);
42 strbuf_release(&payload
);
43 strbuf_release(&signature
);
47 int gpg_verify_tag(const struct object_id
*oid
, const char *name_to_report
,
50 enum object_type type
;
55 type
= oid_object_info(the_repository
, oid
, NULL
);
57 return error("%s: cannot verify a non-tag object of type %s.",
60 repo_find_unique_abbrev(the_repository
, oid
, DEFAULT_ABBREV
),
63 buf
= repo_read_object_file(the_repository
, oid
, &type
, &size
);
65 return error("%s: unable to read file.",
68 repo_find_unique_abbrev(the_repository
, oid
, DEFAULT_ABBREV
));
70 ret
= run_gpg_verify(buf
, size
, flags
);
76 struct object
*deref_tag(struct repository
*r
, struct object
*o
, const char *warn
, int warnlen
)
78 struct object_id
*last_oid
= NULL
;
79 while (o
&& o
->type
== OBJ_TAG
)
80 if (((struct tag
*)o
)->tagged
) {
81 last_oid
= &((struct tag
*)o
)->tagged
->oid
;
82 o
= parse_object(r
, last_oid
);
88 if (last_oid
&& is_promisor_object(r
, last_oid
))
91 warnlen
= strlen(warn
);
92 error("missing object referenced by '%.*s'", warnlen
, warn
);
97 struct object
*deref_tag_noverify(struct repository
*r
, struct object
*o
)
99 while (o
&& o
->type
== OBJ_TAG
) {
100 o
= parse_object(r
, &o
->oid
);
101 if (o
&& o
->type
== OBJ_TAG
&& ((struct tag
*)o
)->tagged
)
102 o
= ((struct tag
*)o
)->tagged
;
109 struct tag
*lookup_tag(struct repository
*r
, const struct object_id
*oid
)
111 struct object
*obj
= lookup_object(r
, oid
);
113 return create_object(r
, oid
, alloc_tag_node(r
));
114 return object_as_type(obj
, OBJ_TAG
, 0);
117 static timestamp_t
parse_tag_date(const char *buf
, const char *tail
)
121 while (buf
< tail
&& *buf
++ != '>')
126 while (buf
< tail
&& *buf
++ != '\n')
130 /* dateptr < buf && buf[-1] == '\n', so parsing will stop at buf-1 */
131 return parse_timestamp(dateptr
, NULL
, 10);
134 void release_tag_memory(struct tag
*t
)
138 t
->object
.parsed
= 0;
142 int parse_tag_buffer(struct repository
*r
, struct tag
*item
, const void *data
, unsigned long size
)
144 struct object_id oid
;
146 const char *bufptr
= data
;
147 const char *tail
= bufptr
+ size
;
150 if (item
->object
.parsed
)
155 * Presumably left over from a previous failed parse;
156 * clear it out in preparation for re-parsing (we'll probably
157 * hit the same error, which lets us tell our current caller
158 * about the problem).
160 FREE_AND_NULL(item
->tag
);
163 if (size
< the_hash_algo
->hexsz
+ 24)
165 if (memcmp("object ", bufptr
, 7) || parse_oid_hex(bufptr
+ 7, &oid
, &bufptr
) || *bufptr
++ != '\n')
168 if (!starts_with(bufptr
, "type "))
171 nl
= memchr(bufptr
, '\n', tail
- bufptr
);
172 if (!nl
|| sizeof(type
) <= (nl
- bufptr
))
174 memcpy(type
, bufptr
, nl
- bufptr
);
175 type
[nl
- bufptr
] = '\0';
178 if (!strcmp(type
, blob_type
)) {
179 item
->tagged
= (struct object
*)lookup_blob(r
, &oid
);
180 } else if (!strcmp(type
, tree_type
)) {
181 item
->tagged
= (struct object
*)lookup_tree(r
, &oid
);
182 } else if (!strcmp(type
, commit_type
)) {
183 item
->tagged
= (struct object
*)lookup_commit(r
, &oid
);
184 } else if (!strcmp(type
, tag_type
)) {
185 item
->tagged
= (struct object
*)lookup_tag(r
, &oid
);
187 return error("unknown tag type '%s' in %s",
188 type
, oid_to_hex(&item
->object
.oid
));
192 return error("bad tag pointer to %s in %s",
194 oid_to_hex(&item
->object
.oid
));
196 if (bufptr
+ 4 < tail
&& starts_with(bufptr
, "tag "))
201 nl
= memchr(bufptr
, '\n', tail
- bufptr
);
204 item
->tag
= xmemdupz(bufptr
, nl
- bufptr
);
207 if (bufptr
+ 7 < tail
&& starts_with(bufptr
, "tagger "))
208 item
->date
= parse_tag_date(bufptr
, tail
);
212 item
->object
.parsed
= 1;
216 int parse_tag(struct tag
*item
)
218 enum object_type type
;
223 if (item
->object
.parsed
)
225 data
= repo_read_object_file(the_repository
, &item
->object
.oid
, &type
,
228 return error("Could not read %s",
229 oid_to_hex(&item
->object
.oid
));
230 if (type
!= OBJ_TAG
) {
232 return error("Object %s not a tag",
233 oid_to_hex(&item
->object
.oid
));
235 ret
= parse_tag_buffer(the_repository
, item
, data
, size
);
240 struct object_id
*get_tagged_oid(struct tag
*tag
)
244 return &tag
->tagged
->oid
;