blob: 4470d2bf78e1fbb00d00e487f41daa4373cf48e1 [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"
Nicolas Pitre0ab17952007-02-26 14:56:00 -05003#include "commit.h"
4#include "tree.h"
5#include "blob.h"
Daniel Barkalow2636f612005-04-28 07:46:33 -07006
7const char *tag_type = "tag";
8
Junio C Hamano9534f402005-11-02 15:19:13 -08009struct object *deref_tag(struct object *o, const char *warn, int warnlen)
Junio C Hamano37fde872005-08-05 00:47:56 -070010{
Linus Torvalds19746322006-07-11 20:45:31 -070011 while (o && o->type == OBJ_TAG)
Martin Koegler24e8a3c2008-02-18 08:31:55 +010012 if (((struct tag *)o)->tagged)
13 o = parse_object(((struct tag *)o)->tagged->sha1);
14 else
15 o = NULL;
Junio C Hamano9534f402005-11-02 15:19:13 -080016 if (!o && warn) {
17 if (!warnlen)
18 warnlen = strlen(warn);
19 error("missing object referenced by '%.*s'", warnlen, warn);
20 }
Junio C Hamano37fde872005-08-05 00:47:56 -070021 return o;
22}
23
Jason McMullan5d6ccf52005-06-03 11:05:39 -040024struct tag *lookup_tag(const unsigned char *sha1)
Daniel Barkalow2636f612005-04-28 07:46:33 -070025{
Linus Torvalds100c5f32007-04-16 22:11:43 -070026 struct object *obj = lookup_object(sha1);
27 if (!obj)
28 return create_object(sha1, OBJ_TAG, alloc_tag_node());
Nicolas Pitred1af0022005-05-20 16:59:17 -040029 if (!obj->type)
Linus Torvalds19746322006-07-11 20:45:31 -070030 obj->type = OBJ_TAG;
31 if (obj->type != OBJ_TAG) {
Johan Herlandeb096262007-05-29 01:21:25 +020032 error("Object %s is a %s, not a tag",
Linus Torvalds885a86a2006-06-14 16:45:13 -070033 sha1_to_hex(sha1), typename(obj->type));
Daniel Barkalow2636f612005-04-28 07:46:33 -070034 return NULL;
35 }
36 return (struct tag *) obj;
37}
38
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -040039int parse_tag_buffer(struct tag *item, void *data, unsigned long size)
Daniel Barkalow2636f612005-04-28 07:46:33 -070040{
Edgar Toernigae200ee2005-04-30 09:51:03 -070041 int typelen, taglen;
Nicolas Pitre0ab17952007-02-26 14:56:00 -050042 unsigned char sha1[20];
Edgar Toernigae200ee2005-04-30 09:51:03 -070043 const char *type_line, *tag_line, *sig_line;
Daniel Barkalow89e42022005-06-21 20:35:10 -040044 char type[20];
Martin Koeglera0393ef2008-01-06 20:03:10 +010045 const char *start = data;
Edgar Toernigae200ee2005-04-30 09:51:03 -070046
Daniel Barkalow2636f612005-04-28 07:46:33 -070047 if (item->object.parsed)
48 return 0;
49 item->object.parsed = 1;
Daniel Barkalow2636f612005-04-28 07:46:33 -070050
Daniel Barkalow2636f612005-04-28 07:46:33 -070051 if (size < 64)
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -040052 return -1;
Nicolas Pitre0ab17952007-02-26 14:56:00 -050053 if (memcmp("object ", data, 7) || get_sha1_hex((char *) data + 7, sha1))
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -040054 return -1;
Daniel Barkalow2636f612005-04-28 07:46:33 -070055
Florian Forster1d7f1712006-06-18 17:18:09 +020056 type_line = (char *) data + 48;
Daniel Barkalow2636f612005-04-28 07:46:33 -070057 if (memcmp("\ntype ", type_line-1, 6))
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -040058 return -1;
Daniel Barkalow2636f612005-04-28 07:46:33 -070059
Martin Koeglera0393ef2008-01-06 20:03:10 +010060 tag_line = memchr(type_line, '\n', size - (type_line - start));
Daniel Barkalow2636f612005-04-28 07:46:33 -070061 if (!tag_line || memcmp("tag ", ++tag_line, 4))
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -040062 return -1;
Daniel Barkalow2636f612005-04-28 07:46:33 -070063
Martin Koeglera0393ef2008-01-06 20:03:10 +010064 sig_line = memchr(tag_line, '\n', size - (tag_line - start));
Daniel Barkalow2636f612005-04-28 07:46:33 -070065 if (!sig_line)
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -040066 return -1;
Daniel Barkalow2636f612005-04-28 07:46:33 -070067 sig_line++;
68
69 typelen = tag_line - type_line - strlen("type \n");
70 if (typelen >= 20)
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -040071 return -1;
Daniel Barkalow89e42022005-06-21 20:35:10 -040072 memcpy(type, type_line + 5, typelen);
73 type[typelen] = '\0';
Daniel Barkalow2636f612005-04-28 07:46:33 -070074 taglen = sig_line - tag_line - strlen("tag \n");
Pierre Habouzit182af832007-09-16 00:32:36 +020075 item->tag = xmemdupz(tag_line + 4, taglen);
Daniel Barkalow2636f612005-04-28 07:46:33 -070076
Nicolas Pitre0ab17952007-02-26 14:56:00 -050077 if (!strcmp(type, blob_type)) {
78 item->tagged = &lookup_blob(sha1)->object;
79 } else if (!strcmp(type, tree_type)) {
80 item->tagged = &lookup_tree(sha1)->object;
81 } else if (!strcmp(type, commit_type)) {
82 item->tagged = &lookup_commit(sha1)->object;
83 } else if (!strcmp(type, tag_type)) {
84 item->tagged = &lookup_tag(sha1)->object;
85 } else {
86 error("Unknown type %s", type);
87 item->tagged = NULL;
88 }
89
Daniel Barkalow2636f612005-04-28 07:46:33 -070090 return 0;
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -040091}
Sergey Vlasov13019d42005-05-04 21:44:15 +040092
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -040093int parse_tag(struct tag *item)
94{
Nicolas Pitre21666f12007-02-26 14:55:59 -050095 enum object_type type;
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -040096 void *data;
97 unsigned long size;
98 int ret;
99
100 if (item->object.parsed)
101 return 0;
Nicolas Pitre21666f12007-02-26 14:55:59 -0500102 data = read_sha1_file(item->object.sha1, &type, &size);
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400103 if (!data)
104 return error("Could not read %s",
105 sha1_to_hex(item->object.sha1));
Nicolas Pitre21666f12007-02-26 14:55:59 -0500106 if (type != OBJ_TAG) {
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400107 free(data);
108 return error("Object %s not a tag",
109 sha1_to_hex(item->object.sha1));
110 }
111 ret = parse_tag_buffer(item, data, size);
Sergey Vlasov13019d42005-05-04 21:44:15 +0400112 free(data);
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400113 return ret;
Daniel Barkalow2636f612005-04-28 07:46:33 -0700114}