]> git.ipfire.org Git - thirdparty/git.git/blob - tag.c
Merge master.kernel.org:/pub/scm/gitk/gitk
[thirdparty/git.git] / tag.c
1 #include "tag.h"
2 #include "cache.h"
3
4 const char *tag_type = "tag";
5
6 struct tag *lookup_tag(const unsigned char *sha1)
7 {
8 struct object *obj = lookup_object(sha1);
9 if (!obj) {
10 struct tag *ret = xmalloc(sizeof(struct tag));
11 memset(ret, 0, sizeof(struct tag));
12 created_object(sha1, &ret->object);
13 ret->object.type = tag_type;
14 return ret;
15 }
16 if (!obj->type)
17 obj->type = tag_type;
18 if (obj->type != tag_type) {
19 error("Object %s is a %s, not a tree",
20 sha1_to_hex(sha1), obj->type);
21 return NULL;
22 }
23 return (struct tag *) obj;
24 }
25
26 int parse_tag_buffer(struct tag *item, void *data, unsigned long size)
27 {
28 int typelen, taglen;
29 unsigned char object[20];
30 const char *type_line, *tag_line, *sig_line;
31 char type[20];
32
33 if (item->object.parsed)
34 return 0;
35 item->object.parsed = 1;
36
37 if (size < 64)
38 return -1;
39 if (memcmp("object ", data, 7) || get_sha1_hex(data + 7, object))
40 return -1;
41
42 type_line = data + 48;
43 if (memcmp("\ntype ", type_line-1, 6))
44 return -1;
45
46 tag_line = strchr(type_line, '\n');
47 if (!tag_line || memcmp("tag ", ++tag_line, 4))
48 return -1;
49
50 sig_line = strchr(tag_line, '\n');
51 if (!sig_line)
52 return -1;
53 sig_line++;
54
55 typelen = tag_line - type_line - strlen("type \n");
56 if (typelen >= 20)
57 return -1;
58 memcpy(type, type_line + 5, typelen);
59 type[typelen] = '\0';
60 taglen = sig_line - tag_line - strlen("tag \n");
61 item->tag = xmalloc(taglen + 1);
62 memcpy(item->tag, tag_line + 4, taglen);
63 item->tag[taglen] = '\0';
64
65 item->tagged = lookup_object_type(object, type);
66 if (item->tagged)
67 add_ref(&item->object, item->tagged);
68
69 return 0;
70 }
71
72 int parse_tag(struct tag *item)
73 {
74 char type[20];
75 void *data;
76 unsigned long size;
77 int ret;
78
79 if (item->object.parsed)
80 return 0;
81 data = read_sha1_file(item->object.sha1, type, &size);
82 if (!data)
83 return error("Could not read %s",
84 sha1_to_hex(item->object.sha1));
85 if (strcmp(type, tag_type)) {
86 free(data);
87 return error("Object %s not a tag",
88 sha1_to_hex(item->object.sha1));
89 }
90 ret = parse_tag_buffer(item, data, size);
91 free(data);
92 return ret;
93 }