]> git.ipfire.org Git - thirdparty/git.git/blame - tag.c
prune-packed: fix minor memory leak
[thirdparty/git.git] / tag.c
CommitLineData
2636f614 1#include "cache.h"
8f1d2e6f 2#include "tag.h"
0ab17950
NP
3#include "commit.h"
4#include "tree.h"
5#include "blob.h"
2636f614 6
ac58c4c7 7#define PGP_SIGNATURE "-----BEGIN PGP SIGNATURE-----"
3d5854e7 8#define PGP_MESSAGE "-----BEGIN PGP MESSAGE-----"
ac58c4c7 9
2636f614
DB
10const char *tag_type = "tag";
11
9534f40b 12struct object *deref_tag(struct object *o, const char *warn, int warnlen)
37fde874 13{
1974632c 14 while (o && o->type == OBJ_TAG)
24e8a3c9
MK
15 if (((struct tag *)o)->tagged)
16 o = parse_object(((struct tag *)o)->tagged->sha1);
17 else
18 o = NULL;
9534f40b
JH
19 if (!o && warn) {
20 if (!warnlen)
21 warnlen = strlen(warn);
22 error("missing object referenced by '%.*s'", warnlen, warn);
23 }
37fde874
JH
24 return o;
25}
26
90108a24
JK
27struct object *deref_tag_noverify(struct object *o)
28{
29 while (o && o->type == OBJ_TAG) {
30 o = parse_object(o->sha1);
31 if (o && o->type == OBJ_TAG && ((struct tag *)o)->tagged)
32 o = ((struct tag *)o)->tagged;
33 else
34 o = NULL;
35 }
36 return o;
37}
38
5d6ccf5c 39struct tag *lookup_tag(const unsigned char *sha1)
2636f614 40{
100c5f3b
LT
41 struct object *obj = lookup_object(sha1);
42 if (!obj)
d36f51c1 43 return create_object(sha1, alloc_tag_node());
8ff226a9 44 return object_as_type(obj, OBJ_TAG, 0);
2636f614
DB
45}
46
e451d06b
SP
47static unsigned long parse_tag_date(const char *buf, const char *tail)
48{
49 const char *dateptr;
50
51 while (buf < tail && *buf++ != '>')
52 /* nada */;
53 if (buf >= tail)
54 return 0;
55 dateptr = buf;
56 while (buf < tail && *buf++ != '\n')
57 /* nada */;
58 if (buf >= tail)
59 return 0;
60 /* dateptr < buf && buf[-1] == '\n', so strtoul will stop at buf-1 */
61 return strtoul(dateptr, NULL, 10);
62}
63
cf7b1cad 64int parse_tag_buffer(struct tag *item, const void *data, unsigned long size)
2636f614 65{
0ab17950 66 unsigned char sha1[20];
89e4202f 67 char type[20];
28de5b6b
SP
68 const char *bufptr = data;
69 const char *tail = bufptr + size;
70 const char *nl;
ae200ee5 71
2e0052a5
SP
72 if (item->object.parsed)
73 return 0;
74 item->object.parsed = 1;
2636f614 75
2636f614 76 if (size < 64)
bd2c39f5 77 return -1;
28de5b6b 78 if (memcmp("object ", bufptr, 7) || get_sha1_hex(bufptr + 7, sha1) || bufptr[47] != '\n')
bd2c39f5 79 return -1;
28de5b6b 80 bufptr += 48; /* "object " + sha1 + "\n" */
2636f614 81
59556548 82 if (!starts_with(bufptr, "type "))
bd2c39f5 83 return -1;
28de5b6b
SP
84 bufptr += 5;
85 nl = memchr(bufptr, '\n', tail - bufptr);
86 if (!nl || sizeof(type) <= (nl - bufptr))
bd2c39f5 87 return -1;
28de5b6b
SP
88 strncpy(type, bufptr, nl - bufptr);
89 type[nl - bufptr] = '\0';
90 bufptr = nl + 1;
2636f614 91
0ab17950
NP
92 if (!strcmp(type, blob_type)) {
93 item->tagged = &lookup_blob(sha1)->object;
94 } else if (!strcmp(type, tree_type)) {
95 item->tagged = &lookup_tree(sha1)->object;
96 } else if (!strcmp(type, commit_type)) {
97 item->tagged = &lookup_commit(sha1)->object;
98 } else if (!strcmp(type, tag_type)) {
99 item->tagged = &lookup_tag(sha1)->object;
100 } else {
101 error("Unknown type %s", type);
102 item->tagged = NULL;
103 }
104
59556548 105 if (bufptr + 4 < tail && starts_with(bufptr, "tag "))
85594252
NTND
106 ; /* good */
107 else
28de5b6b
SP
108 return -1;
109 bufptr += 4;
110 nl = memchr(bufptr, '\n', tail - bufptr);
111 if (!nl)
112 return -1;
113 item->tag = xmemdupz(bufptr, nl - bufptr);
114 bufptr = nl + 1;
115
59556548 116 if (bufptr + 7 < tail && starts_with(bufptr, "tagger "))
e451d06b
SP
117 item->date = parse_tag_date(bufptr, tail);
118 else
119 item->date = 0;
120
2636f614 121 return 0;
bd2c39f5 122}
13019d41 123
bd2c39f5
NP
124int parse_tag(struct tag *item)
125{
21666f1a 126 enum object_type type;
bd2c39f5
NP
127 void *data;
128 unsigned long size;
129 int ret;
130
131 if (item->object.parsed)
132 return 0;
21666f1a 133 data = read_sha1_file(item->object.sha1, &type, &size);
bd2c39f5
NP
134 if (!data)
135 return error("Could not read %s",
136 sha1_to_hex(item->object.sha1));
21666f1a 137 if (type != OBJ_TAG) {
bd2c39f5
NP
138 free(data);
139 return error("Object %s not a tag",
140 sha1_to_hex(item->object.sha1));
141 }
142 ret = parse_tag_buffer(item, data, size);
13019d41 143 free(data);
bd2c39f5 144 return ret;
2636f614 145}
ac58c4c7 146
2f47eae2
JH
147/*
148 * Look at a signed tag object, and return the offset where
149 * the embedded detached signature begins, or the end of the
150 * data when there is no such signature.
151 */
ac58c4c7
MG
152size_t parse_signature(const char *buf, unsigned long size)
153{
154 char *eol;
155 size_t len = 0;
59556548
CC
156 while (len < size && !starts_with(buf + len, PGP_SIGNATURE) &&
157 !starts_with(buf + len, PGP_MESSAGE)) {
ac58c4c7
MG
158 eol = memchr(buf + len, '\n', size - len);
159 len += eol ? eol - (buf + len) + 1 : size - len;
160 }
161 return len;
162}