blob: f62bcdd994509323080683ce19c1a4d8241f9dec [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");
Pierre Habouzit182af832007-09-16 00:32:36 +020071 item->tag = xmemdupz(tag_line + 4, taglen);
Daniel Barkalow2636f612005-04-28 07:46:33 -070072
Nicolas Pitre0ab17952007-02-26 14:56:00 -050073 if (!strcmp(type, blob_type)) {
74 item->tagged = &lookup_blob(sha1)->object;
75 } else if (!strcmp(type, tree_type)) {
76 item->tagged = &lookup_tree(sha1)->object;
77 } else if (!strcmp(type, commit_type)) {
78 item->tagged = &lookup_commit(sha1)->object;
79 } else if (!strcmp(type, tag_type)) {
80 item->tagged = &lookup_tag(sha1)->object;
81 } else {
82 error("Unknown type %s", type);
83 item->tagged = NULL;
84 }
85
Junio C Hamano27dedf02005-11-16 21:32:44 -080086 if (item->tagged && track_object_refs) {
87 struct object_refs *refs = alloc_object_refs(1);
88 refs->ref[0] = item->tagged;
89 set_object_refs(&item->object, refs);
90 }
Daniel Barkalow89e42022005-06-21 20:35:10 -040091
Daniel Barkalow2636f612005-04-28 07:46:33 -070092 return 0;
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -040093}
Sergey Vlasov13019d42005-05-04 21:44:15 +040094
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -040095int parse_tag(struct tag *item)
96{
Nicolas Pitre21666f12007-02-26 14:55:59 -050097 enum object_type type;
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -040098 void *data;
99 unsigned long size;
100 int ret;
101
102 if (item->object.parsed)
103 return 0;
Nicolas Pitre21666f12007-02-26 14:55:59 -0500104 data = read_sha1_file(item->object.sha1, &type, &size);
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400105 if (!data)
106 return error("Could not read %s",
107 sha1_to_hex(item->object.sha1));
Nicolas Pitre21666f12007-02-26 14:55:59 -0500108 if (type != OBJ_TAG) {
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400109 free(data);
110 return error("Object %s not a tag",
111 sha1_to_hex(item->object.sha1));
112 }
113 ret = parse_tag_buffer(item, data, size);
Sergey Vlasov13019d42005-05-04 21:44:15 +0400114 free(data);
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400115 return ret;
Daniel Barkalow2636f612005-04-28 07:46:33 -0700116}