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