blob: ac0e57398a2b236c9501ac43c517ead371b8988c [file] [log] [blame]
Daniel Barkalow2636f612005-04-28 07:46:33 -07001#include "cache.h"
Junio C Hamano8f1d2e62006-01-07 01:33:54 -08002#include "tag.h"
Daniel Barkalow2636f612005-04-28 07:46:33 -07003
4const char *tag_type = "tag";
5
Junio C Hamano9534f402005-11-02 15:19:13 -08006struct object *deref_tag(struct object *o, const char *warn, int warnlen)
Junio C Hamano37fde872005-08-05 00:47:56 -07007{
8 while (o && o->type == tag_type)
9 o = parse_object(((struct tag *)o)->tagged->sha1);
Junio C Hamano9534f402005-11-02 15:19:13 -080010 if (!o && warn) {
11 if (!warnlen)
12 warnlen = strlen(warn);
13 error("missing object referenced by '%.*s'", warnlen, warn);
14 }
Junio C Hamano37fde872005-08-05 00:47:56 -070015 return o;
16}
17
Jason McMullan5d6ccf52005-06-03 11:05:39 -040018struct tag *lookup_tag(const unsigned char *sha1)
Daniel Barkalow2636f612005-04-28 07:46:33 -070019{
20 struct object *obj = lookup_object(sha1);
21 if (!obj) {
22 struct tag *ret = xmalloc(sizeof(struct tag));
23 memset(ret, 0, sizeof(struct tag));
24 created_object(sha1, &ret->object);
25 ret->object.type = tag_type;
26 return ret;
27 }
Nicolas Pitred1af0022005-05-20 16:59:17 -040028 if (!obj->type)
29 obj->type = tag_type;
Daniel Barkalow2636f612005-04-28 07:46:33 -070030 if (obj->type != tag_type) {
31 error("Object %s is a %s, not a tree",
32 sha1_to_hex(sha1), obj->type);
33 return NULL;
34 }
35 return (struct tag *) obj;
36}
37
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -040038int parse_tag_buffer(struct tag *item, void *data, unsigned long size)
Daniel Barkalow2636f612005-04-28 07:46:33 -070039{
Edgar Toernigae200ee2005-04-30 09:51:03 -070040 int typelen, taglen;
41 unsigned char object[20];
42 const char *type_line, *tag_line, *sig_line;
Daniel Barkalow89e42022005-06-21 20:35:10 -040043 char type[20];
Edgar Toernigae200ee2005-04-30 09:51:03 -070044
Daniel Barkalow2636f612005-04-28 07:46:33 -070045 if (item->object.parsed)
46 return 0;
47 item->object.parsed = 1;
Daniel Barkalow2636f612005-04-28 07:46:33 -070048
Daniel Barkalow2636f612005-04-28 07:46:33 -070049 if (size < 64)
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -040050 return -1;
Daniel Barkalow2636f612005-04-28 07:46:33 -070051 if (memcmp("object ", data, 7) || get_sha1_hex(data + 7, object))
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -040052 return -1;
Daniel Barkalow2636f612005-04-28 07:46:33 -070053
Daniel Barkalow2636f612005-04-28 07:46:33 -070054 type_line = data + 48;
55 if (memcmp("\ntype ", type_line-1, 6))
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -040056 return -1;
Daniel Barkalow2636f612005-04-28 07:46:33 -070057
58 tag_line = strchr(type_line, '\n');
59 if (!tag_line || memcmp("tag ", ++tag_line, 4))
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -040060 return -1;
Daniel Barkalow2636f612005-04-28 07:46:33 -070061
62 sig_line = strchr(tag_line, '\n');
63 if (!sig_line)
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -040064 return -1;
Daniel Barkalow2636f612005-04-28 07:46:33 -070065 sig_line++;
66
67 typelen = tag_line - type_line - strlen("type \n");
68 if (typelen >= 20)
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -040069 return -1;
Daniel Barkalow89e42022005-06-21 20:35:10 -040070 memcpy(type, type_line + 5, typelen);
71 type[typelen] = '\0';
Daniel Barkalow2636f612005-04-28 07:46:33 -070072 taglen = sig_line - tag_line - strlen("tag \n");
73 item->tag = xmalloc(taglen + 1);
74 memcpy(item->tag, tag_line + 4, taglen);
75 item->tag[taglen] = '\0';
76
Daniel Barkalow89e42022005-06-21 20:35:10 -040077 item->tagged = lookup_object_type(object, type);
Junio C Hamano27dedf02005-11-16 21:32:44 -080078 if (item->tagged && track_object_refs) {
79 struct object_refs *refs = alloc_object_refs(1);
80 refs->ref[0] = item->tagged;
81 set_object_refs(&item->object, refs);
82 }
Daniel Barkalow89e42022005-06-21 20:35:10 -040083
Daniel Barkalow2636f612005-04-28 07:46:33 -070084 return 0;
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -040085}
Sergey Vlasov13019d42005-05-04 21:44:15 +040086
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -040087int parse_tag(struct tag *item)
88{
89 char type[20];
90 void *data;
91 unsigned long size;
92 int ret;
93
94 if (item->object.parsed)
95 return 0;
96 data = read_sha1_file(item->object.sha1, type, &size);
97 if (!data)
98 return error("Could not read %s",
99 sha1_to_hex(item->object.sha1));
100 if (strcmp(type, tag_type)) {
101 free(data);
102 return error("Object %s not a tag",
103 sha1_to_hex(item->object.sha1));
104 }
105 ret = parse_tag_buffer(item, data, size);
Sergey Vlasov13019d42005-05-04 21:44:15 +0400106 free(data);
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400107 return ret;
Daniel Barkalow2636f612005-04-28 07:46:33 -0700108}