6 #include "gpg-interface.h"
8 const char *tag_type
= "tag";
10 static int run_gpg_verify(const char *buf
, unsigned long size
, unsigned flags
)
12 struct signature_check sigc
;
16 memset(&sigc
, 0, sizeof(sigc
));
18 payload_size
= parse_signature(buf
, size
);
20 if (size
== payload_size
) {
21 if (flags
& GPG_VERIFY_VERBOSE
)
22 write_in_full(1, buf
, payload_size
);
23 return error("no signature found");
26 ret
= check_signature(buf
, payload_size
, buf
+ payload_size
,
27 size
- payload_size
, &sigc
);
29 if (!(flags
& GPG_VERIFY_OMIT_STATUS
))
30 print_signature_buffer(&sigc
, flags
);
32 signature_check_clear(&sigc
);
36 int gpg_verify_tag(const unsigned char *sha1
, const char *name_to_report
,
39 enum object_type type
;
44 type
= sha1_object_info(sha1
, NULL
);
46 return error("%s: cannot verify a non-tag object of type %s.",
49 find_unique_abbrev(sha1
, DEFAULT_ABBREV
),
52 buf
= read_sha1_file(sha1
, &type
, &size
);
54 return error("%s: unable to read file.",
57 find_unique_abbrev(sha1
, DEFAULT_ABBREV
));
59 ret
= run_gpg_verify(buf
, size
, flags
);
65 struct object
*deref_tag(struct object
*o
, const char *warn
, int warnlen
)
67 while (o
&& o
->type
== OBJ_TAG
)
68 if (((struct tag
*)o
)->tagged
)
69 o
= parse_object(((struct tag
*)o
)->tagged
->oid
.hash
);
74 warnlen
= strlen(warn
);
75 error("missing object referenced by '%.*s'", warnlen
, warn
);
80 struct object
*deref_tag_noverify(struct object
*o
)
82 while (o
&& o
->type
== OBJ_TAG
) {
83 o
= parse_object(o
->oid
.hash
);
84 if (o
&& o
->type
== OBJ_TAG
&& ((struct tag
*)o
)->tagged
)
85 o
= ((struct tag
*)o
)->tagged
;
92 struct tag
*lookup_tag(const unsigned char *sha1
)
94 struct object
*obj
= lookup_object(sha1
);
96 return create_object(sha1
, alloc_tag_node());
97 return object_as_type(obj
, OBJ_TAG
, 0);
100 static unsigned long parse_tag_date(const char *buf
, const char *tail
)
104 while (buf
< tail
&& *buf
++ != '>')
109 while (buf
< tail
&& *buf
++ != '\n')
113 /* dateptr < buf && buf[-1] == '\n', so strtoul will stop at buf-1 */
114 return strtoul(dateptr
, NULL
, 10);
117 int parse_tag_buffer(struct tag
*item
, const void *data
, unsigned long size
)
119 unsigned char sha1
[20];
121 const char *bufptr
= data
;
122 const char *tail
= bufptr
+ size
;
125 if (item
->object
.parsed
)
127 item
->object
.parsed
= 1;
131 if (memcmp("object ", bufptr
, 7) || get_sha1_hex(bufptr
+ 7, sha1
) || bufptr
[47] != '\n')
133 bufptr
+= 48; /* "object " + sha1 + "\n" */
135 if (!starts_with(bufptr
, "type "))
138 nl
= memchr(bufptr
, '\n', tail
- bufptr
);
139 if (!nl
|| sizeof(type
) <= (nl
- bufptr
))
141 memcpy(type
, bufptr
, nl
- bufptr
);
142 type
[nl
- bufptr
] = '\0';
145 if (!strcmp(type
, blob_type
)) {
146 item
->tagged
= &lookup_blob(sha1
)->object
;
147 } else if (!strcmp(type
, tree_type
)) {
148 item
->tagged
= &lookup_tree(sha1
)->object
;
149 } else if (!strcmp(type
, commit_type
)) {
150 item
->tagged
= &lookup_commit(sha1
)->object
;
151 } else if (!strcmp(type
, tag_type
)) {
152 item
->tagged
= &lookup_tag(sha1
)->object
;
154 error("Unknown type %s", type
);
158 if (bufptr
+ 4 < tail
&& starts_with(bufptr
, "tag "))
163 nl
= memchr(bufptr
, '\n', tail
- bufptr
);
166 item
->tag
= xmemdupz(bufptr
, nl
- bufptr
);
169 if (bufptr
+ 7 < tail
&& starts_with(bufptr
, "tagger "))
170 item
->date
= parse_tag_date(bufptr
, tail
);
177 int parse_tag(struct tag
*item
)
179 enum object_type type
;
184 if (item
->object
.parsed
)
186 data
= read_sha1_file(item
->object
.oid
.hash
, &type
, &size
);
188 return error("Could not read %s",
189 oid_to_hex(&item
->object
.oid
));
190 if (type
!= OBJ_TAG
) {
192 return error("Object %s not a tag",
193 oid_to_hex(&item
->object
.oid
));
195 ret
= parse_tag_buffer(item
, data
, size
);