3 #include "object-store.h"
8 #include "gpg-interface.h"
12 const char *tag_type
= "tag";
14 static int run_gpg_verify(const char *buf
, unsigned long size
, unsigned flags
)
16 struct signature_check sigc
;
17 struct strbuf payload
= STRBUF_INIT
;
18 struct strbuf signature
= STRBUF_INIT
;
21 memset(&sigc
, 0, sizeof(sigc
));
23 if (!parse_signature(buf
, size
, &payload
, &signature
)) {
24 if (flags
& GPG_VERIFY_VERBOSE
)
25 write_in_full(1, buf
, size
);
26 return error("no signature found");
29 sigc
.payload_type
= SIGNATURE_PAYLOAD_TAG
;
30 sigc
.payload
= strbuf_detach(&payload
, &sigc
.payload_len
);
31 ret
= check_signature(&sigc
, signature
.buf
, signature
.len
);
33 if (!(flags
& GPG_VERIFY_OMIT_STATUS
))
34 print_signature_buffer(&sigc
, flags
);
36 signature_check_clear(&sigc
);
37 strbuf_release(&payload
);
38 strbuf_release(&signature
);
42 int gpg_verify_tag(const struct object_id
*oid
, const char *name_to_report
,
45 enum object_type type
;
50 type
= oid_object_info(the_repository
, oid
, NULL
);
52 return error("%s: cannot verify a non-tag object of type %s.",
55 find_unique_abbrev(oid
, DEFAULT_ABBREV
),
58 buf
= read_object_file(oid
, &type
, &size
);
60 return error("%s: unable to read file.",
63 find_unique_abbrev(oid
, DEFAULT_ABBREV
));
65 ret
= run_gpg_verify(buf
, size
, flags
);
71 struct object
*deref_tag(struct repository
*r
, struct object
*o
, const char *warn
, int warnlen
)
73 struct object_id
*last_oid
= NULL
;
74 while (o
&& o
->type
== OBJ_TAG
)
75 if (((struct tag
*)o
)->tagged
) {
76 last_oid
= &((struct tag
*)o
)->tagged
->oid
;
77 o
= parse_object(r
, last_oid
);
83 if (last_oid
&& is_promisor_object(last_oid
))
86 warnlen
= strlen(warn
);
87 error("missing object referenced by '%.*s'", warnlen
, warn
);
92 struct object
*deref_tag_noverify(struct object
*o
)
94 while (o
&& o
->type
== OBJ_TAG
) {
95 o
= parse_object(the_repository
, &o
->oid
);
96 if (o
&& o
->type
== OBJ_TAG
&& ((struct tag
*)o
)->tagged
)
97 o
= ((struct tag
*)o
)->tagged
;
104 struct tag
*lookup_tag(struct repository
*r
, const struct object_id
*oid
)
106 struct object
*obj
= lookup_object(r
, oid
);
108 return create_object(r
, oid
, alloc_tag_node(r
));
109 return object_as_type(obj
, OBJ_TAG
, 0);
112 static timestamp_t
parse_tag_date(const char *buf
, const char *tail
)
116 while (buf
< tail
&& *buf
++ != '>')
121 while (buf
< tail
&& *buf
++ != '\n')
125 /* dateptr < buf && buf[-1] == '\n', so parsing will stop at buf-1 */
126 return parse_timestamp(dateptr
, NULL
, 10);
129 void release_tag_memory(struct tag
*t
)
133 t
->object
.parsed
= 0;
137 int parse_tag_buffer(struct repository
*r
, struct tag
*item
, const void *data
, unsigned long size
)
139 struct object_id oid
;
141 const char *bufptr
= data
;
142 const char *tail
= bufptr
+ size
;
145 if (item
->object
.parsed
)
150 * Presumably left over from a previous failed parse;
151 * clear it out in preparation for re-parsing (we'll probably
152 * hit the same error, which lets us tell our current caller
153 * about the problem).
155 FREE_AND_NULL(item
->tag
);
158 if (size
< the_hash_algo
->hexsz
+ 24)
160 if (memcmp("object ", bufptr
, 7) || parse_oid_hex(bufptr
+ 7, &oid
, &bufptr
) || *bufptr
++ != '\n')
163 if (!starts_with(bufptr
, "type "))
166 nl
= memchr(bufptr
, '\n', tail
- bufptr
);
167 if (!nl
|| sizeof(type
) <= (nl
- bufptr
))
169 memcpy(type
, bufptr
, nl
- bufptr
);
170 type
[nl
- bufptr
] = '\0';
173 if (!strcmp(type
, blob_type
)) {
174 item
->tagged
= (struct object
*)lookup_blob(r
, &oid
);
175 } else if (!strcmp(type
, tree_type
)) {
176 item
->tagged
= (struct object
*)lookup_tree(r
, &oid
);
177 } else if (!strcmp(type
, commit_type
)) {
178 item
->tagged
= (struct object
*)lookup_commit(r
, &oid
);
179 } else if (!strcmp(type
, tag_type
)) {
180 item
->tagged
= (struct object
*)lookup_tag(r
, &oid
);
182 return error("unknown tag type '%s' in %s",
183 type
, oid_to_hex(&item
->object
.oid
));
187 return error("bad tag pointer to %s in %s",
189 oid_to_hex(&item
->object
.oid
));
191 if (bufptr
+ 4 < tail
&& starts_with(bufptr
, "tag "))
196 nl
= memchr(bufptr
, '\n', tail
- bufptr
);
199 item
->tag
= xmemdupz(bufptr
, nl
- bufptr
);
202 if (bufptr
+ 7 < tail
&& starts_with(bufptr
, "tagger "))
203 item
->date
= parse_tag_date(bufptr
, tail
);
207 item
->object
.parsed
= 1;
211 int parse_tag(struct tag
*item
)
213 enum object_type type
;
218 if (item
->object
.parsed
)
220 data
= read_object_file(&item
->object
.oid
, &type
, &size
);
222 return error("Could not read %s",
223 oid_to_hex(&item
->object
.oid
));
224 if (type
!= OBJ_TAG
) {
226 return error("Object %s not a tag",
227 oid_to_hex(&item
->object
.oid
));
229 ret
= parse_tag_buffer(the_repository
, item
, data
, size
);
234 struct object_id
*get_tagged_oid(struct tag
*tag
)
238 return &tag
->tagged
->oid
;