blob: bbacd59a23f7994980f4bf017324833ca3d4adb3 [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)
Junio C Hamano37fde872005-08-05 00:47:56 -070012 o = parse_object(((struct tag *)o)->tagged->sha1);
Junio C Hamano9534f402005-11-02 15:19:13 -080013 if (!o && warn) {
14 if (!warnlen)
15 warnlen = strlen(warn);
16 error("missing object referenced by '%.*s'", warnlen, warn);
17 }
Junio C Hamano37fde872005-08-05 00:47:56 -070018 return o;
19}
20
Jason McMullan5d6ccf52005-06-03 11:05:39 -040021struct tag *lookup_tag(const unsigned char *sha1)
Daniel Barkalow2636f612005-04-28 07:46:33 -070022{
Linus Torvalds100c5f32007-04-16 22:11:43 -070023 struct object *obj = lookup_object(sha1);
24 if (!obj)
25 return create_object(sha1, OBJ_TAG, alloc_tag_node());
Nicolas Pitred1af0022005-05-20 16:59:17 -040026 if (!obj->type)
Linus Torvalds19746322006-07-11 20:45:31 -070027 obj->type = OBJ_TAG;
28 if (obj->type != OBJ_TAG) {
Johan Herlandeb096262007-05-29 01:21:25 +020029 error("Object %s is a %s, not a tag",
Linus Torvalds885a86a2006-06-14 16:45:13 -070030 sha1_to_hex(sha1), typename(obj->type));
Daniel Barkalow2636f612005-04-28 07:46:33 -070031 return NULL;
32 }
33 return (struct tag *) obj;
34}
35
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -040036int parse_tag_buffer(struct tag *item, void *data, unsigned long size)
Daniel Barkalow2636f612005-04-28 07:46:33 -070037{
Edgar Toernigae200ee2005-04-30 09:51:03 -070038 int typelen, taglen;
Nicolas Pitre0ab17952007-02-26 14:56:00 -050039 unsigned char sha1[20];
Edgar Toernigae200ee2005-04-30 09:51:03 -070040 const char *type_line, *tag_line, *sig_line;
Daniel Barkalow89e42022005-06-21 20:35:10 -040041 char type[20];
Edgar Toernigae200ee2005-04-30 09:51:03 -070042
Daniel Barkalow2636f612005-04-28 07:46:33 -070043 if (item->object.parsed)
44 return 0;
45 item->object.parsed = 1;
Daniel Barkalow2636f612005-04-28 07:46:33 -070046
Daniel Barkalow2636f612005-04-28 07:46:33 -070047 if (size < 64)
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -040048 return -1;
Nicolas Pitre0ab17952007-02-26 14:56:00 -050049 if (memcmp("object ", data, 7) || get_sha1_hex((char *) data + 7, sha1))
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -040050 return -1;
Daniel Barkalow2636f612005-04-28 07:46:33 -070051
Florian Forster1d7f1712006-06-18 17:18:09 +020052 type_line = (char *) data + 48;
Daniel Barkalow2636f612005-04-28 07:46:33 -070053 if (memcmp("\ntype ", type_line-1, 6))
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -040054 return -1;
Daniel Barkalow2636f612005-04-28 07:46:33 -070055
56 tag_line = strchr(type_line, '\n');
57 if (!tag_line || memcmp("tag ", ++tag_line, 4))
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -040058 return -1;
Daniel Barkalow2636f612005-04-28 07:46:33 -070059
60 sig_line = strchr(tag_line, '\n');
61 if (!sig_line)
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -040062 return -1;
Daniel Barkalow2636f612005-04-28 07:46:33 -070063 sig_line++;
64
65 typelen = tag_line - type_line - strlen("type \n");
66 if (typelen >= 20)
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -040067 return -1;
Daniel Barkalow89e42022005-06-21 20:35:10 -040068 memcpy(type, type_line + 5, typelen);
69 type[typelen] = '\0';
Daniel Barkalow2636f612005-04-28 07:46:33 -070070 taglen = sig_line - tag_line - strlen("tag \n");
71 item->tag = xmalloc(taglen + 1);
72 memcpy(item->tag, tag_line + 4, taglen);
73 item->tag[taglen] = '\0';
74
Nicolas Pitre0ab17952007-02-26 14:56:00 -050075 if (!strcmp(type, blob_type)) {
76 item->tagged = &lookup_blob(sha1)->object;
77 } else if (!strcmp(type, tree_type)) {
78 item->tagged = &lookup_tree(sha1)->object;
79 } else if (!strcmp(type, commit_type)) {
80 item->tagged = &lookup_commit(sha1)->object;
81 } else if (!strcmp(type, tag_type)) {
82 item->tagged = &lookup_tag(sha1)->object;
83 } else {
84 error("Unknown type %s", type);
85 item->tagged = NULL;
86 }
87
Junio C Hamano27dedf02005-11-16 21:32:44 -080088 if (item->tagged && track_object_refs) {
89 struct object_refs *refs = alloc_object_refs(1);
90 refs->ref[0] = item->tagged;
91 set_object_refs(&item->object, refs);
92 }
Daniel Barkalow89e42022005-06-21 20:35:10 -040093
Daniel Barkalow2636f612005-04-28 07:46:33 -070094 return 0;
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -040095}
Sergey Vlasov13019d42005-05-04 21:44:15 +040096
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -040097int parse_tag(struct tag *item)
98{
Nicolas Pitre21666f12007-02-26 14:55:59 -050099 enum object_type type;
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400100 void *data;
101 unsigned long size;
102 int ret;
103
104 if (item->object.parsed)
105 return 0;
Nicolas Pitre21666f12007-02-26 14:55:59 -0500106 data = read_sha1_file(item->object.sha1, &type, &size);
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400107 if (!data)
108 return error("Could not read %s",
109 sha1_to_hex(item->object.sha1));
Nicolas Pitre21666f12007-02-26 14:55:59 -0500110 if (type != OBJ_TAG) {
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400111 free(data);
112 return error("Object %s not a tag",
113 sha1_to_hex(item->object.sha1));
114 }
115 ret = parse_tag_buffer(item, data, size);
Sergey Vlasov13019d42005-05-04 21:44:15 +0400116 free(data);
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400117 return ret;
Daniel Barkalow2636f612005-04-28 07:46:33 -0700118}