[PATCH] don't load and decompress objects twice with parse_object()
[git/git.git] / tag.c
CommitLineData
2636f614
DB
1#include "tag.h"
2#include "cache.h"
3
4const char *tag_type = "tag";
5
6struct tag *lookup_tag(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 != tag_type) {
17 error("Object %s is a %s, not a tree",
18 sha1_to_hex(sha1), obj->type);
19 return NULL;
20 }
21 return (struct tag *) obj;
22}
23
bd2c39f5 24int parse_tag_buffer(struct tag *item, void *data, unsigned long size)
2636f614 25{
ae200ee5
ET
26 int typelen, taglen;
27 unsigned char object[20];
28 const char *type_line, *tag_line, *sig_line;
29
2636f614
DB
30 if (item->object.parsed)
31 return 0;
32 item->object.parsed = 1;
2636f614 33
2636f614 34 if (size < 64)
bd2c39f5 35 return -1;
2636f614 36 if (memcmp("object ", data, 7) || get_sha1_hex(data + 7, object))
bd2c39f5 37 return -1;
2636f614
DB
38
39 item->tagged = parse_object(object);
770896e5
LT
40 if (item->tagged)
41 add_ref(&item->object, item->tagged);
2636f614
DB
42
43 type_line = data + 48;
44 if (memcmp("\ntype ", type_line-1, 6))
bd2c39f5 45 return -1;
2636f614
DB
46
47 tag_line = strchr(type_line, '\n');
48 if (!tag_line || memcmp("tag ", ++tag_line, 4))
bd2c39f5 49 return -1;
2636f614
DB
50
51 sig_line = strchr(tag_line, '\n');
52 if (!sig_line)
bd2c39f5 53 return -1;
2636f614
DB
54 sig_line++;
55
56 typelen = tag_line - type_line - strlen("type \n");
57 if (typelen >= 20)
bd2c39f5 58 return -1;
2636f614
DB
59 taglen = sig_line - tag_line - strlen("tag \n");
60 item->tag = xmalloc(taglen + 1);
61 memcpy(item->tag, tag_line + 4, taglen);
62 item->tag[taglen] = '\0';
63
64 return 0;
bd2c39f5 65}
13019d41 66
bd2c39f5
NP
67int parse_tag(struct tag *item)
68{
69 char type[20];
70 void *data;
71 unsigned long size;
72 int ret;
73
74 if (item->object.parsed)
75 return 0;
76 data = read_sha1_file(item->object.sha1, type, &size);
77 if (!data)
78 return error("Could not read %s",
79 sha1_to_hex(item->object.sha1));
80 if (strcmp(type, tag_type)) {
81 free(data);
82 return error("Object %s not a tag",
83 sha1_to_hex(item->object.sha1));
84 }
85 ret = parse_tag_buffer(item, data, size);
13019d41 86 free(data);
bd2c39f5 87 return ret;
2636f614 88}