promisor-remote: skip move_to_tail when no-op
[git/git.git] / tag.c
CommitLineData
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
11const char *tag_type = "tag";
12
45a227ef
ST
13static 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);
94240b91
LP
31
32 if (!(flags & GPG_VERIFY_OMIT_STATUS))
33 print_signature_buffer(&sigc, flags);
45a227ef
ST
34
35 signature_check_clear(&sigc);
36 return ret;
37}
38
84571760 39int gpg_verify_tag(const struct object_id *oid, const char *name_to_report,
45a227ef
ST
40 unsigned flags)
41{
42 enum object_type type;
43 char *buf;
44 unsigned long size;
45 int ret;
46
0df8e965 47 type = oid_object_info(the_repository, oid, NULL);
45a227ef
ST
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 :
aab9583f 52 find_unique_abbrev(oid, DEFAULT_ABBREV),
debca9d2 53 type_name(type));
45a227ef 54
b4f5aca4 55 buf = read_object_file(oid, &type, &size);
45a227ef
ST
56 if (!buf)
57 return error("%s: unable to read file.",
58 name_to_report ?
59 name_to_report :
aab9583f 60 find_unique_abbrev(oid, DEFAULT_ABBREV));
45a227ef
ST
61
62 ret = run_gpg_verify(buf, size, flags);
63
64 free(buf);
65 return ret;
66}
67
286d258d 68struct object *deref_tag(struct repository *r, struct object *o, const char *warn, int warnlen)
37fde874 69{
8c4cc326 70 struct object_id *last_oid = NULL;
1974632c 71 while (o && o->type == OBJ_TAG)
8c4cc326
JT
72 if (((struct tag *)o)->tagged) {
73 last_oid = &((struct tag *)o)->tagged->oid;
09ca6130 74 o = parse_object(r, last_oid);
8c4cc326
JT
75 } else {
76 last_oid = NULL;
24e8a3c9 77 o = NULL;
8c4cc326 78 }
9534f40b 79 if (!o && warn) {
8c4cc326
JT
80 if (last_oid && is_promisor_object(last_oid))
81 return NULL;
9534f40b
JH
82 if (!warnlen)
83 warnlen = strlen(warn);
84 error("missing object referenced by '%.*s'", warnlen, warn);
85 }
37fde874
JH
86 return o;
87}
88
90108a24
JK
89struct object *deref_tag_noverify(struct object *o)
90{
91 while (o && o->type == OBJ_TAG) {
109cd76d 92 o = parse_object(the_repository, &o->oid);
90108a24
JK
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
8bde69b9 101struct tag *lookup_tag(struct repository *r, const struct object_id *oid)
2636f614 102{
8bde69b9 103 struct object *obj = lookup_object(r, oid->hash);
100c5f3b 104 if (!obj)
8bde69b9
SB
105 return create_object(r, oid->hash,
106 alloc_tag_node(r));
107 return object_as_type(r, obj, OBJ_TAG, 0);
2636f614
DB
108}
109
dddbad72 110static timestamp_t parse_tag_date(const char *buf, const char *tail)
e451d06b
SP
111{
112 const char *dateptr;
113
114 while (buf < tail && *buf++ != '>')
115 /* nada */;
116 if (buf >= tail)
117 return 0;
118 dateptr = buf;
119 while (buf < tail && *buf++ != '\n')
120 /* nada */;
121 if (buf >= tail)
122 return 0;
1aeb7e75
JS
123 /* dateptr < buf && buf[-1] == '\n', so parsing will stop at buf-1 */
124 return parse_timestamp(dateptr, NULL, 10);
e451d06b
SP
125}
126
14ba97f8
SB
127void release_tag_memory(struct tag *t)
128{
129 free(t->tag);
130 t->tagged = NULL;
131 t->object.parsed = 0;
132 t->date = 0;
133}
134
84f80cd2 135int parse_tag_buffer(struct repository *r, struct tag *item, const void *data, unsigned long size)
2636f614 136{
1e4085a0 137 struct object_id oid;
89e4202f 138 char type[20];
28de5b6b
SP
139 const char *bufptr = data;
140 const char *tail = bufptr + size;
141 const char *nl;
ae200ee5 142
2e0052a5
SP
143 if (item->object.parsed)
144 return 0;
145 item->object.parsed = 1;
2636f614 146
d8a3a690 147 if (size < the_hash_algo->hexsz + 24)
bd2c39f5 148 return -1;
1e4085a0 149 if (memcmp("object ", bufptr, 7) || parse_oid_hex(bufptr + 7, &oid, &bufptr) || *bufptr++ != '\n')
bd2c39f5 150 return -1;
2636f614 151
59556548 152 if (!starts_with(bufptr, "type "))
bd2c39f5 153 return -1;
28de5b6b
SP
154 bufptr += 5;
155 nl = memchr(bufptr, '\n', tail - bufptr);
156 if (!nl || sizeof(type) <= (nl - bufptr))
bd2c39f5 157 return -1;
eddda371 158 memcpy(type, bufptr, nl - bufptr);
28de5b6b
SP
159 type[nl - bufptr] = '\0';
160 bufptr = nl + 1;
2636f614 161
0ab17950 162 if (!strcmp(type, blob_type)) {
84f80cd2 163 item->tagged = (struct object *)lookup_blob(r, &oid);
0ab17950 164 } else if (!strcmp(type, tree_type)) {
84f80cd2 165 item->tagged = (struct object *)lookup_tree(r, &oid);
0ab17950 166 } else if (!strcmp(type, commit_type)) {
84f80cd2 167 item->tagged = (struct object *)lookup_commit(r, &oid);
0ab17950 168 } else if (!strcmp(type, tag_type)) {
84f80cd2 169 item->tagged = (struct object *)lookup_tag(r, &oid);
0ab17950
NP
170 } else {
171 error("Unknown type %s", type);
172 item->tagged = NULL;
173 }
174
59556548 175 if (bufptr + 4 < tail && starts_with(bufptr, "tag "))
85594252
NTND
176 ; /* good */
177 else
28de5b6b
SP
178 return -1;
179 bufptr += 4;
180 nl = memchr(bufptr, '\n', tail - bufptr);
181 if (!nl)
182 return -1;
183 item->tag = xmemdupz(bufptr, nl - bufptr);
184 bufptr = nl + 1;
185
59556548 186 if (bufptr + 7 < tail && starts_with(bufptr, "tagger "))
e451d06b
SP
187 item->date = parse_tag_date(bufptr, tail);
188 else
189 item->date = 0;
190
2636f614 191 return 0;
bd2c39f5 192}
13019d41 193
bd2c39f5
NP
194int parse_tag(struct tag *item)
195{
21666f1a 196 enum object_type type;
bd2c39f5
NP
197 void *data;
198 unsigned long size;
199 int ret;
200
201 if (item->object.parsed)
202 return 0;
b4f5aca4 203 data = read_object_file(&item->object.oid, &type, &size);
bd2c39f5
NP
204 if (!data)
205 return error("Could not read %s",
f2fd0760 206 oid_to_hex(&item->object.oid));
21666f1a 207 if (type != OBJ_TAG) {
bd2c39f5
NP
208 free(data);
209 return error("Object %s not a tag",
f2fd0760 210 oid_to_hex(&item->object.oid));
bd2c39f5 211 }
0e740fed 212 ret = parse_tag_buffer(the_repository, item, data, size);
13019d41 213 free(data);
bd2c39f5 214 return ret;
2636f614 215}