]>
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 DB |
6 | |
7 | const char *tag_type = "tag"; | |
8 | ||
9534f40b | 9 | struct object *deref_tag(struct object *o, const char *warn, int warnlen) |
37fde874 | 10 | { |
1974632c | 11 | while (o && o->type == OBJ_TAG) |
24e8a3c9 MK |
12 | if (((struct tag *)o)->tagged) |
13 | o = parse_object(((struct tag *)o)->tagged->sha1); | |
14 | else | |
15 | o = NULL; | |
9534f40b JH |
16 | if (!o && warn) { |
17 | if (!warnlen) | |
18 | warnlen = strlen(warn); | |
19 | error("missing object referenced by '%.*s'", warnlen, warn); | |
20 | } | |
37fde874 JH |
21 | return o; |
22 | } | |
23 | ||
90108a24 JK |
24 | struct object *deref_tag_noverify(struct object *o) |
25 | { | |
26 | while (o && o->type == OBJ_TAG) { | |
27 | o = parse_object(o->sha1); | |
28 | if (o && o->type == OBJ_TAG && ((struct tag *)o)->tagged) | |
29 | o = ((struct tag *)o)->tagged; | |
30 | else | |
31 | o = NULL; | |
32 | } | |
33 | return o; | |
34 | } | |
35 | ||
5d6ccf5c | 36 | struct tag *lookup_tag(const unsigned char *sha1) |
2636f614 | 37 | { |
100c5f3b LT |
38 | struct object *obj = lookup_object(sha1); |
39 | if (!obj) | |
d36f51c1 | 40 | return create_object(sha1, alloc_tag_node()); |
8ff226a9 | 41 | return object_as_type(obj, OBJ_TAG, 0); |
2636f614 DB |
42 | } |
43 | ||
e451d06b SP |
44 | static unsigned long parse_tag_date(const char *buf, const char *tail) |
45 | { | |
46 | const char *dateptr; | |
47 | ||
48 | while (buf < tail && *buf++ != '>') | |
49 | /* nada */; | |
50 | if (buf >= tail) | |
51 | return 0; | |
52 | dateptr = buf; | |
53 | while (buf < tail && *buf++ != '\n') | |
54 | /* nada */; | |
55 | if (buf >= tail) | |
56 | return 0; | |
57 | /* dateptr < buf && buf[-1] == '\n', so strtoul will stop at buf-1 */ | |
58 | return strtoul(dateptr, NULL, 10); | |
59 | } | |
60 | ||
cf7b1cad | 61 | int parse_tag_buffer(struct tag *item, const void *data, unsigned long size) |
2636f614 | 62 | { |
0ab17950 | 63 | unsigned char sha1[20]; |
89e4202f | 64 | char type[20]; |
28de5b6b SP |
65 | const char *bufptr = data; |
66 | const char *tail = bufptr + size; | |
67 | const char *nl; | |
ae200ee5 | 68 | |
2e0052a5 SP |
69 | if (item->object.parsed) |
70 | return 0; | |
71 | item->object.parsed = 1; | |
2636f614 | 72 | |
2636f614 | 73 | if (size < 64) |
bd2c39f5 | 74 | return -1; |
28de5b6b | 75 | if (memcmp("object ", bufptr, 7) || get_sha1_hex(bufptr + 7, sha1) || bufptr[47] != '\n') |
bd2c39f5 | 76 | return -1; |
28de5b6b | 77 | bufptr += 48; /* "object " + sha1 + "\n" */ |
2636f614 | 78 | |
59556548 | 79 | if (!starts_with(bufptr, "type ")) |
bd2c39f5 | 80 | return -1; |
28de5b6b SP |
81 | bufptr += 5; |
82 | nl = memchr(bufptr, '\n', tail - bufptr); | |
83 | if (!nl || sizeof(type) <= (nl - bufptr)) | |
bd2c39f5 | 84 | return -1; |
28de5b6b SP |
85 | strncpy(type, bufptr, nl - bufptr); |
86 | type[nl - bufptr] = '\0'; | |
87 | bufptr = nl + 1; | |
2636f614 | 88 | |
0ab17950 NP |
89 | if (!strcmp(type, blob_type)) { |
90 | item->tagged = &lookup_blob(sha1)->object; | |
91 | } else if (!strcmp(type, tree_type)) { | |
92 | item->tagged = &lookup_tree(sha1)->object; | |
93 | } else if (!strcmp(type, commit_type)) { | |
94 | item->tagged = &lookup_commit(sha1)->object; | |
95 | } else if (!strcmp(type, tag_type)) { | |
96 | item->tagged = &lookup_tag(sha1)->object; | |
97 | } else { | |
98 | error("Unknown type %s", type); | |
99 | item->tagged = NULL; | |
100 | } | |
101 | ||
59556548 | 102 | if (bufptr + 4 < tail && starts_with(bufptr, "tag ")) |
85594252 NTND |
103 | ; /* good */ |
104 | else | |
28de5b6b SP |
105 | return -1; |
106 | bufptr += 4; | |
107 | nl = memchr(bufptr, '\n', tail - bufptr); | |
108 | if (!nl) | |
109 | return -1; | |
110 | item->tag = xmemdupz(bufptr, nl - bufptr); | |
111 | bufptr = nl + 1; | |
112 | ||
59556548 | 113 | if (bufptr + 7 < tail && starts_with(bufptr, "tagger ")) |
e451d06b SP |
114 | item->date = parse_tag_date(bufptr, tail); |
115 | else | |
116 | item->date = 0; | |
117 | ||
2636f614 | 118 | return 0; |
bd2c39f5 | 119 | } |
13019d41 | 120 | |
bd2c39f5 NP |
121 | int parse_tag(struct tag *item) |
122 | { | |
21666f1a | 123 | enum object_type type; |
bd2c39f5 NP |
124 | void *data; |
125 | unsigned long size; | |
126 | int ret; | |
127 | ||
128 | if (item->object.parsed) | |
129 | return 0; | |
21666f1a | 130 | data = read_sha1_file(item->object.sha1, &type, &size); |
bd2c39f5 NP |
131 | if (!data) |
132 | return error("Could not read %s", | |
133 | sha1_to_hex(item->object.sha1)); | |
21666f1a | 134 | if (type != OBJ_TAG) { |
bd2c39f5 NP |
135 | free(data); |
136 | return error("Object %s not a tag", | |
137 | sha1_to_hex(item->object.sha1)); | |
138 | } | |
139 | ret = parse_tag_buffer(item, data, size); | |
13019d41 | 140 | free(data); |
bd2c39f5 | 141 | return ret; |
2636f614 | 142 | } |