]>
Commit | Line | Data |
---|---|---|
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 |
10 | const char *tag_type = "tag"; |
11 | ||
9534f40b | 12 | struct 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 | ||
5d6ccf5c | 27 | struct tag *lookup_tag(const unsigned char *sha1) |
2636f614 | 28 | { |
100c5f3b LT |
29 | struct object *obj = lookup_object(sha1); |
30 | if (!obj) | |
31 | return create_object(sha1, OBJ_TAG, alloc_tag_node()); | |
d1af002d | 32 | if (!obj->type) |
1974632c | 33 | obj->type = OBJ_TAG; |
d2c030d4 JH |
34 | if (obj->type != OBJ_TAG) { |
35 | error("Object %s is a %s, not a tag", | |
36 | sha1_to_hex(sha1), typename(obj->type)); | |
37 | return NULL; | |
38 | } | |
39 | return (struct tag *) obj; | |
2636f614 DB |
40 | } |
41 | ||
e451d06b SP |
42 | static unsigned long parse_tag_date(const char *buf, const char *tail) |
43 | { | |
44 | const char *dateptr; | |
45 | ||
46 | while (buf < tail && *buf++ != '>') | |
47 | /* nada */; | |
48 | if (buf >= tail) | |
49 | return 0; | |
50 | dateptr = buf; | |
51 | while (buf < tail && *buf++ != '\n') | |
52 | /* nada */; | |
53 | if (buf >= tail) | |
54 | return 0; | |
55 | /* dateptr < buf && buf[-1] == '\n', so strtoul will stop at buf-1 */ | |
56 | return strtoul(dateptr, NULL, 10); | |
57 | } | |
58 | ||
bd2c39f5 | 59 | int parse_tag_buffer(struct tag *item, void *data, unsigned long size) |
2636f614 | 60 | { |
0ab17950 | 61 | unsigned char sha1[20]; |
89e4202f | 62 | char type[20]; |
28de5b6b SP |
63 | const char *bufptr = data; |
64 | const char *tail = bufptr + size; | |
65 | const char *nl; | |
ae200ee5 | 66 | |
2e0052a5 SP |
67 | if (item->object.parsed) |
68 | return 0; | |
69 | item->object.parsed = 1; | |
2636f614 | 70 | |
2636f614 | 71 | if (size < 64) |
bd2c39f5 | 72 | return -1; |
28de5b6b | 73 | if (memcmp("object ", bufptr, 7) || get_sha1_hex(bufptr + 7, sha1) || bufptr[47] != '\n') |
bd2c39f5 | 74 | return -1; |
28de5b6b | 75 | bufptr += 48; /* "object " + sha1 + "\n" */ |
2636f614 | 76 | |
28de5b6b | 77 | if (prefixcmp(bufptr, "type ")) |
bd2c39f5 | 78 | return -1; |
28de5b6b SP |
79 | bufptr += 5; |
80 | nl = memchr(bufptr, '\n', tail - bufptr); | |
81 | if (!nl || sizeof(type) <= (nl - bufptr)) | |
bd2c39f5 | 82 | return -1; |
28de5b6b SP |
83 | strncpy(type, bufptr, nl - bufptr); |
84 | type[nl - bufptr] = '\0'; | |
85 | bufptr = nl + 1; | |
2636f614 | 86 | |
0ab17950 NP |
87 | if (!strcmp(type, blob_type)) { |
88 | item->tagged = &lookup_blob(sha1)->object; | |
89 | } else if (!strcmp(type, tree_type)) { | |
90 | item->tagged = &lookup_tree(sha1)->object; | |
91 | } else if (!strcmp(type, commit_type)) { | |
92 | item->tagged = &lookup_commit(sha1)->object; | |
93 | } else if (!strcmp(type, tag_type)) { | |
94 | item->tagged = &lookup_tag(sha1)->object; | |
95 | } else { | |
96 | error("Unknown type %s", type); | |
97 | item->tagged = NULL; | |
98 | } | |
99 | ||
28de5b6b SP |
100 | if (prefixcmp(bufptr, "tag ")) |
101 | return -1; | |
102 | bufptr += 4; | |
103 | nl = memchr(bufptr, '\n', tail - bufptr); | |
104 | if (!nl) | |
105 | return -1; | |
106 | item->tag = xmemdupz(bufptr, nl - bufptr); | |
107 | bufptr = nl + 1; | |
108 | ||
e451d06b SP |
109 | if (!prefixcmp(bufptr, "tagger ")) |
110 | item->date = parse_tag_date(bufptr, tail); | |
111 | else | |
112 | item->date = 0; | |
113 | ||
2636f614 | 114 | return 0; |
bd2c39f5 | 115 | } |
13019d41 | 116 | |
bd2c39f5 NP |
117 | int parse_tag(struct tag *item) |
118 | { | |
21666f1a | 119 | enum object_type type; |
bd2c39f5 NP |
120 | void *data; |
121 | unsigned long size; | |
122 | int ret; | |
123 | ||
124 | if (item->object.parsed) | |
125 | return 0; | |
21666f1a | 126 | data = read_sha1_file(item->object.sha1, &type, &size); |
bd2c39f5 NP |
127 | if (!data) |
128 | return error("Could not read %s", | |
129 | sha1_to_hex(item->object.sha1)); | |
21666f1a | 130 | if (type != OBJ_TAG) { |
bd2c39f5 NP |
131 | free(data); |
132 | return error("Object %s not a tag", | |
133 | sha1_to_hex(item->object.sha1)); | |
134 | } | |
135 | ret = parse_tag_buffer(item, data, size); | |
13019d41 | 136 | free(data); |
bd2c39f5 | 137 | return ret; |
2636f614 | 138 | } |
ac58c4c7 MG |
139 | |
140 | size_t parse_signature(const char *buf, unsigned long size) | |
141 | { | |
142 | char *eol; | |
143 | size_t len = 0; | |
3d5854e7 MG |
144 | while (len < size && prefixcmp(buf + len, PGP_SIGNATURE) && |
145 | prefixcmp(buf + len, PGP_MESSAGE)) { | |
ac58c4c7 MG |
146 | eol = memchr(buf + len, '\n', size - len); |
147 | len += eol ? eol - (buf + len) + 1 : size - len; | |
148 | } | |
149 | return len; | |
150 | } |