git
/
tag.c
244 строки · 5.8 Кб
1#define USE_THE_REPOSITORY_VARIABLE
2
3#include "git-compat-util.h"
4#include "environment.h"
5#include "tag.h"
6#include "object-name.h"
7#include "object-store-ll.h"
8#include "commit.h"
9#include "tree.h"
10#include "blob.h"
11#include "alloc.h"
12#include "gpg-interface.h"
13#include "hex.h"
14#include "packfile.h"
15
16const char *tag_type = "tag";
17
18static int run_gpg_verify(const char *buf, unsigned long size, unsigned flags)
19{
20struct signature_check sigc;
21struct strbuf payload = STRBUF_INIT;
22struct strbuf signature = STRBUF_INIT;
23int ret;
24
25memset(&sigc, 0, sizeof(sigc));
26
27if (!parse_signature(buf, size, &payload, &signature)) {
28if (flags & GPG_VERIFY_VERBOSE)
29write_in_full(1, buf, size);
30return error("no signature found");
31}
32
33sigc.payload_type = SIGNATURE_PAYLOAD_TAG;
34sigc.payload = strbuf_detach(&payload, &sigc.payload_len);
35ret = check_signature(&sigc, signature.buf, signature.len);
36
37if (!(flags & GPG_VERIFY_OMIT_STATUS))
38print_signature_buffer(&sigc, flags);
39
40signature_check_clear(&sigc);
41strbuf_release(&payload);
42strbuf_release(&signature);
43return ret;
44}
45
46int gpg_verify_tag(const struct object_id *oid, const char *name_to_report,
47unsigned flags)
48{
49enum object_type type;
50char *buf;
51unsigned long size;
52int ret;
53
54type = oid_object_info(the_repository, oid, NULL);
55if (type != OBJ_TAG)
56return error("%s: cannot verify a non-tag object of type %s.",
57name_to_report ?
58name_to_report :
59repo_find_unique_abbrev(the_repository, oid, DEFAULT_ABBREV),
60type_name(type));
61
62buf = repo_read_object_file(the_repository, oid, &type, &size);
63if (!buf)
64return error("%s: unable to read file.",
65name_to_report ?
66name_to_report :
67repo_find_unique_abbrev(the_repository, oid, DEFAULT_ABBREV));
68
69ret = run_gpg_verify(buf, size, flags);
70
71free(buf);
72return ret;
73}
74
75struct object *deref_tag(struct repository *r, struct object *o, const char *warn, int warnlen)
76{
77struct object_id *last_oid = NULL;
78while (o && o->type == OBJ_TAG)
79if (((struct tag *)o)->tagged) {
80last_oid = &((struct tag *)o)->tagged->oid;
81o = parse_object(r, last_oid);
82} else {
83last_oid = NULL;
84o = NULL;
85}
86if (!o && warn) {
87if (last_oid && is_promisor_object(last_oid))
88return NULL;
89if (!warnlen)
90warnlen = strlen(warn);
91error("missing object referenced by '%.*s'", warnlen, warn);
92}
93return o;
94}
95
96struct object *deref_tag_noverify(struct repository *r, struct object *o)
97{
98while (o && o->type == OBJ_TAG) {
99o = parse_object(r, &o->oid);
100if (o && o->type == OBJ_TAG && ((struct tag *)o)->tagged)
101o = ((struct tag *)o)->tagged;
102else
103o = NULL;
104}
105return o;
106}
107
108struct tag *lookup_tag(struct repository *r, const struct object_id *oid)
109{
110struct object *obj = lookup_object(r, oid);
111if (!obj)
112return create_object(r, oid, alloc_tag_node(r));
113return object_as_type(obj, OBJ_TAG, 0);
114}
115
116static timestamp_t parse_tag_date(const char *buf, const char *tail)
117{
118const char *dateptr;
119
120while (buf < tail && *buf++ != '>')
121/* nada */;
122if (buf >= tail)
123return 0;
124dateptr = buf;
125while (buf < tail && *buf++ != '\n')
126/* nada */;
127if (buf >= tail)
128return 0;
129/* dateptr < buf && buf[-1] == '\n', so parsing will stop at buf-1 */
130return parse_timestamp(dateptr, NULL, 10);
131}
132
133void release_tag_memory(struct tag *t)
134{
135free(t->tag);
136t->tagged = NULL;
137t->object.parsed = 0;
138t->date = 0;
139}
140
141int parse_tag_buffer(struct repository *r, struct tag *item, const void *data, unsigned long size)
142{
143struct object_id oid;
144char type[20];
145const char *bufptr = data;
146const char *tail = bufptr + size;
147const char *nl;
148
149if (item->object.parsed)
150return 0;
151
152if (item->tag) {
153/*
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).
158*/
159FREE_AND_NULL(item->tag);
160}
161
162if (size < the_hash_algo->hexsz + 24)
163return -1;
164if (memcmp("object ", bufptr, 7) || parse_oid_hex(bufptr + 7, &oid, &bufptr) || *bufptr++ != '\n')
165return -1;
166
167if (!starts_with(bufptr, "type "))
168return -1;
169bufptr += 5;
170nl = memchr(bufptr, '\n', tail - bufptr);
171if (!nl || sizeof(type) <= (nl - bufptr))
172return -1;
173memcpy(type, bufptr, nl - bufptr);
174type[nl - bufptr] = '\0';
175bufptr = nl + 1;
176
177if (!strcmp(type, blob_type)) {
178item->tagged = (struct object *)lookup_blob(r, &oid);
179} else if (!strcmp(type, tree_type)) {
180item->tagged = (struct object *)lookup_tree(r, &oid);
181} else if (!strcmp(type, commit_type)) {
182item->tagged = (struct object *)lookup_commit(r, &oid);
183} else if (!strcmp(type, tag_type)) {
184item->tagged = (struct object *)lookup_tag(r, &oid);
185} else {
186return error("unknown tag type '%s' in %s",
187type, oid_to_hex(&item->object.oid));
188}
189
190if (!item->tagged)
191return error("bad tag pointer to %s in %s",
192oid_to_hex(&oid),
193oid_to_hex(&item->object.oid));
194
195if (bufptr + 4 < tail && starts_with(bufptr, "tag "))
196; /* good */
197else
198return -1;
199bufptr += 4;
200nl = memchr(bufptr, '\n', tail - bufptr);
201if (!nl)
202return -1;
203item->tag = xmemdupz(bufptr, nl - bufptr);
204bufptr = nl + 1;
205
206if (bufptr + 7 < tail && starts_with(bufptr, "tagger "))
207item->date = parse_tag_date(bufptr, tail);
208else
209item->date = 0;
210
211item->object.parsed = 1;
212return 0;
213}
214
215int parse_tag(struct tag *item)
216{
217enum object_type type;
218void *data;
219unsigned long size;
220int ret;
221
222if (item->object.parsed)
223return 0;
224data = repo_read_object_file(the_repository, &item->object.oid, &type,
225&size);
226if (!data)
227return error("Could not read %s",
228oid_to_hex(&item->object.oid));
229if (type != OBJ_TAG) {
230free(data);
231return error("Object %s not a tag",
232oid_to_hex(&item->object.oid));
233}
234ret = parse_tag_buffer(the_repository, item, data, size);
235free(data);
236return ret;
237}
238
239struct object_id *get_tagged_oid(struct tag *tag)
240{
241if (!tag->tagged)
242die("bad tag");
243return &tag->tagged->oid;
244}
245