]>
Commit | Line | Data |
---|---|---|
2636f614 | 1 | #include "cache.h" |
8f1d2e6f | 2 | #include "tag.h" |
cbd53a21 | 3 | #include "object-store.h" |
0ab17950 NP |
4 | #include "commit.h" |
5 | #include "tree.h" | |
6 | #include "blob.h" | |
14ba97f8 | 7 | #include "alloc.h" |
94240b91 | 8 | #include "gpg-interface.h" |
8c4cc326 | 9 | #include "packfile.h" |
2636f614 DB |
10 | |
11 | const char *tag_type = "tag"; | |
12 | ||
45a227ef ST |
13 | static int run_gpg_verify(const char *buf, unsigned long size, unsigned flags) |
14 | { | |
15 | struct signature_check sigc; | |
482c1191 | 16 | struct strbuf payload = STRBUF_INIT; |
17 | struct strbuf signature = STRBUF_INIT; | |
45a227ef ST |
18 | int ret; |
19 | ||
20 | memset(&sigc, 0, sizeof(sigc)); | |
21 | ||
482c1191 | 22 | if (!parse_signature(buf, size, &payload, &signature)) { |
45a227ef | 23 | if (flags & GPG_VERIFY_VERBOSE) |
482c1191 | 24 | write_in_full(1, buf, size); |
45a227ef ST |
25 | return error("no signature found"); |
26 | } | |
27 | ||
dd3aa418 | 28 | sigc.payload_type = SIGNATURE_PAYLOAD_TAG; |
02769437 FS |
29 | sigc.payload = strbuf_detach(&payload, &sigc.payload_len); |
30 | ret = check_signature(&sigc, signature.buf, signature.len); | |
94240b91 LP |
31 | |
32 | if (!(flags & GPG_VERIFY_OMIT_STATUS)) | |
33 | print_signature_buffer(&sigc, flags); | |
45a227ef ST |
34 | |
35 | signature_check_clear(&sigc); | |
482c1191 | 36 | strbuf_release(&payload); |
37 | strbuf_release(&signature); | |
45a227ef ST |
38 | return ret; |
39 | } | |
40 | ||
84571760 | 41 | int gpg_verify_tag(const struct object_id *oid, const char *name_to_report, |
45a227ef ST |
42 | unsigned flags) |
43 | { | |
44 | enum object_type type; | |
45 | char *buf; | |
46 | unsigned long size; | |
47 | int ret; | |
48 | ||
0df8e965 | 49 | type = oid_object_info(the_repository, oid, NULL); |
45a227ef ST |
50 | if (type != OBJ_TAG) |
51 | return error("%s: cannot verify a non-tag object of type %s.", | |
52 | name_to_report ? | |
53 | name_to_report : | |
aab9583f | 54 | find_unique_abbrev(oid, DEFAULT_ABBREV), |
debca9d2 | 55 | type_name(type)); |
45a227ef | 56 | |
b4f5aca4 | 57 | buf = read_object_file(oid, &type, &size); |
45a227ef ST |
58 | if (!buf) |
59 | return error("%s: unable to read file.", | |
60 | name_to_report ? | |
61 | name_to_report : | |
aab9583f | 62 | find_unique_abbrev(oid, DEFAULT_ABBREV)); |
45a227ef ST |
63 | |
64 | ret = run_gpg_verify(buf, size, flags); | |
65 | ||
66 | free(buf); | |
67 | return ret; | |
68 | } | |
69 | ||
286d258d | 70 | struct object *deref_tag(struct repository *r, struct object *o, const char *warn, int warnlen) |
37fde874 | 71 | { |
8c4cc326 | 72 | struct object_id *last_oid = NULL; |
1974632c | 73 | while (o && o->type == OBJ_TAG) |
8c4cc326 JT |
74 | if (((struct tag *)o)->tagged) { |
75 | last_oid = &((struct tag *)o)->tagged->oid; | |
09ca6130 | 76 | o = parse_object(r, last_oid); |
8c4cc326 JT |
77 | } else { |
78 | last_oid = NULL; | |
24e8a3c9 | 79 | o = NULL; |
8c4cc326 | 80 | } |
9534f40b | 81 | if (!o && warn) { |
8c4cc326 JT |
82 | if (last_oid && is_promisor_object(last_oid)) |
83 | return NULL; | |
9534f40b JH |
84 | if (!warnlen) |
85 | warnlen = strlen(warn); | |
86 | error("missing object referenced by '%.*s'", warnlen, warn); | |
87 | } | |
37fde874 JH |
88 | return o; |
89 | } | |
90 | ||
90108a24 JK |
91 | struct object *deref_tag_noverify(struct object *o) |
92 | { | |
93 | while (o && o->type == OBJ_TAG) { | |
109cd76d | 94 | o = parse_object(the_repository, &o->oid); |
90108a24 JK |
95 | if (o && o->type == OBJ_TAG && ((struct tag *)o)->tagged) |
96 | o = ((struct tag *)o)->tagged; | |
97 | else | |
98 | o = NULL; | |
99 | } | |
100 | return o; | |
101 | } | |
102 | ||
8bde69b9 | 103 | struct tag *lookup_tag(struct repository *r, const struct object_id *oid) |
2636f614 | 104 | { |
d0229abd | 105 | struct object *obj = lookup_object(r, oid); |
100c5f3b | 106 | if (!obj) |
a378509e | 107 | return create_object(r, oid, alloc_tag_node(r)); |
6da43d93 | 108 | return object_as_type(obj, OBJ_TAG, 0); |
2636f614 DB |
109 | } |
110 | ||
dddbad72 | 111 | static timestamp_t parse_tag_date(const char *buf, const char *tail) |
e451d06b SP |
112 | { |
113 | const char *dateptr; | |
114 | ||
115 | while (buf < tail && *buf++ != '>') | |
116 | /* nada */; | |
117 | if (buf >= tail) | |
118 | return 0; | |
119 | dateptr = buf; | |
120 | while (buf < tail && *buf++ != '\n') | |
121 | /* nada */; | |
122 | if (buf >= tail) | |
123 | return 0; | |
1aeb7e75 JS |
124 | /* dateptr < buf && buf[-1] == '\n', so parsing will stop at buf-1 */ |
125 | return parse_timestamp(dateptr, NULL, 10); | |
e451d06b SP |
126 | } |
127 | ||
14ba97f8 SB |
128 | void release_tag_memory(struct tag *t) |
129 | { | |
130 | free(t->tag); | |
131 | t->tagged = NULL; | |
132 | t->object.parsed = 0; | |
133 | t->date = 0; | |
134 | } | |
135 | ||
84f80cd2 | 136 | int parse_tag_buffer(struct repository *r, struct tag *item, const void *data, unsigned long size) |
2636f614 | 137 | { |
1e4085a0 | 138 | struct object_id oid; |
89e4202f | 139 | char type[20]; |
28de5b6b SP |
140 | const char *bufptr = data; |
141 | const char *tail = bufptr + size; | |
142 | const char *nl; | |
ae200ee5 | 143 | |
2e0052a5 SP |
144 | if (item->object.parsed) |
145 | return 0; | |
228c78fb JK |
146 | |
147 | if (item->tag) { | |
148 | /* | |
149 | * Presumably left over from a previous failed parse; | |
150 | * clear it out in preparation for re-parsing (we'll probably | |
151 | * hit the same error, which lets us tell our current caller | |
152 | * about the problem). | |
153 | */ | |
154 | FREE_AND_NULL(item->tag); | |
155 | } | |
2636f614 | 156 | |
d8a3a690 | 157 | if (size < the_hash_algo->hexsz + 24) |
bd2c39f5 | 158 | return -1; |
1e4085a0 | 159 | if (memcmp("object ", bufptr, 7) || parse_oid_hex(bufptr + 7, &oid, &bufptr) || *bufptr++ != '\n') |
bd2c39f5 | 160 | return -1; |
2636f614 | 161 | |
59556548 | 162 | if (!starts_with(bufptr, "type ")) |
bd2c39f5 | 163 | return -1; |
28de5b6b SP |
164 | bufptr += 5; |
165 | nl = memchr(bufptr, '\n', tail - bufptr); | |
166 | if (!nl || sizeof(type) <= (nl - bufptr)) | |
bd2c39f5 | 167 | return -1; |
eddda371 | 168 | memcpy(type, bufptr, nl - bufptr); |
28de5b6b SP |
169 | type[nl - bufptr] = '\0'; |
170 | bufptr = nl + 1; | |
2636f614 | 171 | |
0ab17950 | 172 | if (!strcmp(type, blob_type)) { |
84f80cd2 | 173 | item->tagged = (struct object *)lookup_blob(r, &oid); |
0ab17950 | 174 | } else if (!strcmp(type, tree_type)) { |
84f80cd2 | 175 | item->tagged = (struct object *)lookup_tree(r, &oid); |
0ab17950 | 176 | } else if (!strcmp(type, commit_type)) { |
84f80cd2 | 177 | item->tagged = (struct object *)lookup_commit(r, &oid); |
0ab17950 | 178 | } else if (!strcmp(type, tag_type)) { |
84f80cd2 | 179 | item->tagged = (struct object *)lookup_tag(r, &oid); |
0ab17950 | 180 | } else { |
78d50148 JK |
181 | return error("unknown tag type '%s' in %s", |
182 | type, oid_to_hex(&item->object.oid)); | |
0ab17950 NP |
183 | } |
184 | ||
78d50148 JK |
185 | if (!item->tagged) |
186 | return error("bad tag pointer to %s in %s", | |
187 | oid_to_hex(&oid), | |
188 | oid_to_hex(&item->object.oid)); | |
189 | ||
59556548 | 190 | if (bufptr + 4 < tail && starts_with(bufptr, "tag ")) |
85594252 NTND |
191 | ; /* good */ |
192 | else | |
28de5b6b SP |
193 | return -1; |
194 | bufptr += 4; | |
195 | nl = memchr(bufptr, '\n', tail - bufptr); | |
196 | if (!nl) | |
197 | return -1; | |
198 | item->tag = xmemdupz(bufptr, nl - bufptr); | |
199 | bufptr = nl + 1; | |
200 | ||
59556548 | 201 | if (bufptr + 7 < tail && starts_with(bufptr, "tagger ")) |
e451d06b SP |
202 | item->date = parse_tag_date(bufptr, tail); |
203 | else | |
204 | item->date = 0; | |
205 | ||
228c78fb | 206 | item->object.parsed = 1; |
2636f614 | 207 | return 0; |
bd2c39f5 | 208 | } |
13019d41 | 209 | |
bd2c39f5 NP |
210 | int parse_tag(struct tag *item) |
211 | { | |
21666f1a | 212 | enum object_type type; |
bd2c39f5 NP |
213 | void *data; |
214 | unsigned long size; | |
215 | int ret; | |
216 | ||
217 | if (item->object.parsed) | |
218 | return 0; | |
b4f5aca4 | 219 | data = read_object_file(&item->object.oid, &type, &size); |
bd2c39f5 NP |
220 | if (!data) |
221 | return error("Could not read %s", | |
f2fd0760 | 222 | oid_to_hex(&item->object.oid)); |
21666f1a | 223 | if (type != OBJ_TAG) { |
bd2c39f5 NP |
224 | free(data); |
225 | return error("Object %s not a tag", | |
f2fd0760 | 226 | oid_to_hex(&item->object.oid)); |
bd2c39f5 | 227 | } |
0e740fed | 228 | ret = parse_tag_buffer(the_repository, item, data, size); |
13019d41 | 229 | free(data); |
bd2c39f5 | 230 | return ret; |
2636f614 | 231 | } |
dad3f060 RS |
232 | |
233 | struct object_id *get_tagged_oid(struct tag *tag) | |
234 | { | |
235 | if (!tag->tagged) | |
236 | die("bad tag"); | |
237 | return &tag->tagged->oid; | |
238 | } |