blob: 74d0dabe5d8c1f06a3f67475368c34e3b4046456 [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{
Linus Torvalds885a86a2006-06-14 16:45:13 -07008 while (o && o->type == TYPE_TAG)
Junio C Hamano37fde872005-08-05 00:47:56 -07009 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) {
Linus Torvalds855419f2006-06-19 10:44:15 -070022 struct tag *ret = alloc_tag_node();
Daniel Barkalow2636f612005-04-28 07:46:33 -070023 created_object(sha1, &ret->object);
Linus Torvalds885a86a2006-06-14 16:45:13 -070024 ret->object.type = TYPE_TAG;
Daniel Barkalow2636f612005-04-28 07:46:33 -070025 return ret;
26 }
Nicolas Pitred1af0022005-05-20 16:59:17 -040027 if (!obj->type)
Linus Torvalds885a86a2006-06-14 16:45:13 -070028 obj->type = TYPE_TAG;
29 if (obj->type != TYPE_TAG) {
30 error("Object %s is a %s, not a tree",
31 sha1_to_hex(sha1), typename(obj->type));
Daniel Barkalow2636f612005-04-28 07:46:33 -070032 return NULL;
33 }
34 return (struct tag *) obj;
35}
36
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -040037int parse_tag_buffer(struct tag *item, void *data, unsigned long size)
Daniel Barkalow2636f612005-04-28 07:46:33 -070038{
Edgar Toernigae200ee2005-04-30 09:51:03 -070039 int typelen, taglen;
40 unsigned char object[20];
41 const char *type_line, *tag_line, *sig_line;
Daniel Barkalow89e42022005-06-21 20:35:10 -040042 char type[20];
Edgar Toernigae200ee2005-04-30 09:51:03 -070043
Daniel Barkalow2636f612005-04-28 07:46:33 -070044 if (item->object.parsed)
45 return 0;
46 item->object.parsed = 1;
Daniel Barkalow2636f612005-04-28 07:46:33 -070047
Daniel Barkalow2636f612005-04-28 07:46:33 -070048 if (size < 64)
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -040049 return -1;
Florian Forster1d7f1712006-06-18 17:18:09 +020050 if (memcmp("object ", data, 7) || get_sha1_hex((char *) data + 7, object))
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -040051 return -1;
Daniel Barkalow2636f612005-04-28 07:46:33 -070052
Florian Forster1d7f1712006-06-18 17:18:09 +020053 type_line = (char *) data + 48;
Daniel Barkalow2636f612005-04-28 07:46:33 -070054 if (memcmp("\ntype ", type_line-1, 6))
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -040055 return -1;
Daniel Barkalow2636f612005-04-28 07:46:33 -070056
57 tag_line = strchr(type_line, '\n');
58 if (!tag_line || memcmp("tag ", ++tag_line, 4))
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -040059 return -1;
Daniel Barkalow2636f612005-04-28 07:46:33 -070060
61 sig_line = strchr(tag_line, '\n');
62 if (!sig_line)
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -040063 return -1;
Daniel Barkalow2636f612005-04-28 07:46:33 -070064 sig_line++;
65
66 typelen = tag_line - type_line - strlen("type \n");
67 if (typelen >= 20)
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -040068 return -1;
Daniel Barkalow89e42022005-06-21 20:35:10 -040069 memcpy(type, type_line + 5, typelen);
70 type[typelen] = '\0';
Daniel Barkalow2636f612005-04-28 07:46:33 -070071 taglen = sig_line - tag_line - strlen("tag \n");
72 item->tag = xmalloc(taglen + 1);
73 memcpy(item->tag, tag_line + 4, taglen);
74 item->tag[taglen] = '\0';
75
Daniel Barkalow89e42022005-06-21 20:35:10 -040076 item->tagged = lookup_object_type(object, type);
Junio C Hamano27dedf02005-11-16 21:32:44 -080077 if (item->tagged && track_object_refs) {
78 struct object_refs *refs = alloc_object_refs(1);
79 refs->ref[0] = item->tagged;
80 set_object_refs(&item->object, refs);
81 }
Daniel Barkalow89e42022005-06-21 20:35:10 -040082
Daniel Barkalow2636f612005-04-28 07:46:33 -070083 return 0;
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -040084}
Sergey Vlasov13019d42005-05-04 21:44:15 +040085
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -040086int parse_tag(struct tag *item)
87{
88 char type[20];
89 void *data;
90 unsigned long size;
91 int ret;
92
93 if (item->object.parsed)
94 return 0;
95 data = read_sha1_file(item->object.sha1, type, &size);
96 if (!data)
97 return error("Could not read %s",
98 sha1_to_hex(item->object.sha1));
99 if (strcmp(type, tag_type)) {
100 free(data);
101 return error("Object %s not a tag",
102 sha1_to_hex(item->object.sha1));
103 }
104 ret = parse_tag_buffer(item, data, size);
Sergey Vlasov13019d42005-05-04 21:44:15 +0400105 free(data);
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400106 return ret;
Daniel Barkalow2636f612005-04-28 07:46:33 -0700107}