blob: b1ab75ff0164b0090f43ba55d2a70239ca2fdb7a [file] [log] [blame]
Daniel Barkalow2636f612005-04-28 07:46:33 -07001#include "tag.h"
2#include "cache.h"
3
4const char *tag_type = "tag";
5
Junio C Hamano37fde872005-08-05 00:47:56 -07006struct object *deref_tag(struct object *o)
7{
8 while (o && o->type == tag_type)
9 o = parse_object(((struct tag *)o)->tagged->sha1);
10 return o;
11}
12
Jason McMullan5d6ccf52005-06-03 11:05:39 -040013struct tag *lookup_tag(const unsigned char *sha1)
Daniel Barkalow2636f612005-04-28 07:46:33 -070014{
15 struct object *obj = lookup_object(sha1);
16 if (!obj) {
17 struct tag *ret = xmalloc(sizeof(struct tag));
18 memset(ret, 0, sizeof(struct tag));
19 created_object(sha1, &ret->object);
20 ret->object.type = tag_type;
21 return ret;
22 }
Nicolas Pitred1af0022005-05-20 16:59:17 -040023 if (!obj->type)
24 obj->type = tag_type;
Daniel Barkalow2636f612005-04-28 07:46:33 -070025 if (obj->type != tag_type) {
26 error("Object %s is a %s, not a tree",
27 sha1_to_hex(sha1), obj->type);
28 return NULL;
29 }
30 return (struct tag *) obj;
31}
32
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -040033int parse_tag_buffer(struct tag *item, void *data, unsigned long size)
Daniel Barkalow2636f612005-04-28 07:46:33 -070034{
Edgar Toernigae200ee2005-04-30 09:51:03 -070035 int typelen, taglen;
36 unsigned char object[20];
37 const char *type_line, *tag_line, *sig_line;
Daniel Barkalow89e42022005-06-21 20:35:10 -040038 char type[20];
Edgar Toernigae200ee2005-04-30 09:51:03 -070039
Daniel Barkalow2636f612005-04-28 07:46:33 -070040 if (item->object.parsed)
41 return 0;
42 item->object.parsed = 1;
Daniel Barkalow2636f612005-04-28 07:46:33 -070043
Daniel Barkalow2636f612005-04-28 07:46:33 -070044 if (size < 64)
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -040045 return -1;
Daniel Barkalow2636f612005-04-28 07:46:33 -070046 if (memcmp("object ", data, 7) || get_sha1_hex(data + 7, object))
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -040047 return -1;
Daniel Barkalow2636f612005-04-28 07:46:33 -070048
Daniel Barkalow2636f612005-04-28 07:46:33 -070049 type_line = data + 48;
50 if (memcmp("\ntype ", type_line-1, 6))
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -040051 return -1;
Daniel Barkalow2636f612005-04-28 07:46:33 -070052
53 tag_line = strchr(type_line, '\n');
54 if (!tag_line || memcmp("tag ", ++tag_line, 4))
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -040055 return -1;
Daniel Barkalow2636f612005-04-28 07:46:33 -070056
57 sig_line = strchr(tag_line, '\n');
58 if (!sig_line)
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -040059 return -1;
Daniel Barkalow2636f612005-04-28 07:46:33 -070060 sig_line++;
61
62 typelen = tag_line - type_line - strlen("type \n");
63 if (typelen >= 20)
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -040064 return -1;
Daniel Barkalow89e42022005-06-21 20:35:10 -040065 memcpy(type, type_line + 5, typelen);
66 type[typelen] = '\0';
Daniel Barkalow2636f612005-04-28 07:46:33 -070067 taglen = sig_line - tag_line - strlen("tag \n");
68 item->tag = xmalloc(taglen + 1);
69 memcpy(item->tag, tag_line + 4, taglen);
70 item->tag[taglen] = '\0';
71
Daniel Barkalow89e42022005-06-21 20:35:10 -040072 item->tagged = lookup_object_type(object, type);
73 if (item->tagged)
74 add_ref(&item->object, item->tagged);
75
Daniel Barkalow2636f612005-04-28 07:46:33 -070076 return 0;
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -040077}
Sergey Vlasov13019d42005-05-04 21:44:15 +040078
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -040079int parse_tag(struct tag *item)
80{
81 char type[20];
82 void *data;
83 unsigned long size;
84 int ret;
85
86 if (item->object.parsed)
87 return 0;
88 data = read_sha1_file(item->object.sha1, type, &size);
89 if (!data)
90 return error("Could not read %s",
91 sha1_to_hex(item->object.sha1));
92 if (strcmp(type, tag_type)) {
93 free(data);
94 return error("Object %s not a tag",
95 sha1_to_hex(item->object.sha1));
96 }
97 ret = parse_tag_buffer(item, data, size);
Sergey Vlasov13019d42005-05-04 21:44:15 +040098 free(data);
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -040099 return ret;
Daniel Barkalow2636f612005-04-28 07:46:33 -0700100}