🌐 AI搜索 & 代理 主页
blob: 4470d2bf78e1fbb00d00e487f41daa4373cf48e1 [file] [log] [blame]
Daniel Barkalow2636f612005-04-28 14:46:331#include "cache.h"
Junio C Hamano8f1d2e62006-01-07 09:33:542#include "tag.h"
Nicolas Pitre0ab17952007-02-26 19:56:003#include "commit.h"
4#include "tree.h"
5#include "blob.h"
Daniel Barkalow2636f612005-04-28 14:46:336
7const char *tag_type = "tag";
8
Junio C Hamano9534f402005-11-02 23:19:139struct object *deref_tag(struct object *o, const char *warn, int warnlen)
Junio C Hamano37fde872005-08-05 07:47:5610{
Linus Torvalds19746322006-07-12 03:45:3111 while (o && o->type == OBJ_TAG)
Martin Koegler24e8a3c2008-02-18 07:31:5512 if (((struct tag *)o)->tagged)
13 o = parse_object(((struct tag *)o)->tagged->sha1);
14 else
15 o = NULL;
Junio C Hamano9534f402005-11-02 23:19:1316 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 07:47:5621 return o;
22}
23
Jason McMullan5d6ccf52005-06-03 15:05:3924struct tag *lookup_tag(const unsigned char *sha1)
Daniel Barkalow2636f612005-04-28 14:46:3325{
Linus Torvalds100c5f32007-04-17 05:11:4326 struct object *obj = lookup_object(sha1);
27 if (!obj)
28 return create_object(sha1, OBJ_TAG, alloc_tag_node());
Nicolas Pitred1af0022005-05-20 20:59:1729 if (!obj->type)
Linus Torvalds19746322006-07-12 03:45:3130 obj->type = OBJ_TAG;
31 if (obj->type != OBJ_TAG) {
Johan Herlandeb096262007-05-28 23:21:2532 error("Object %s is a %s, not a tag",
Linus Torvalds885a86a2006-06-14 23:45:1333 sha1_to_hex(sha1), typename(obj->type));
Daniel Barkalow2636f612005-04-28 14:46:3334 return NULL;
35 }
36 return (struct tag *) obj;
37}
38
Nicolas Pitrebd2c39f2005-05-06 17:48:3439int parse_tag_buffer(struct tag *item, void *data, unsigned long size)
Daniel Barkalow2636f612005-04-28 14:46:3340{
Edgar Toernigae200ee2005-04-30 16:51:0341 int typelen, taglen;
Nicolas Pitre0ab17952007-02-26 19:56:0042 unsigned char sha1[20];
Edgar Toernigae200ee2005-04-30 16:51:0343 const char *type_line, *tag_line, *sig_line;
Daniel Barkalow89e42022005-06-22 00:35:1044 char type[20];
Martin Koeglera0393ef2008-01-06 19:03:1045 const char *start = data;
Edgar Toernigae200ee2005-04-30 16:51:0346
Daniel Barkalow2636f612005-04-28 14:46:3347 if (item->object.parsed)
48 return 0;
49 item->object.parsed = 1;
Daniel Barkalow2636f612005-04-28 14:46:3350
Daniel Barkalow2636f612005-04-28 14:46:3351 if (size < 64)
Nicolas Pitrebd2c39f2005-05-06 17:48:3452 return -1;
Nicolas Pitre0ab17952007-02-26 19:56:0053 if (memcmp("object ", data, 7) || get_sha1_hex((char *) data + 7, sha1))
Nicolas Pitrebd2c39f2005-05-06 17:48:3454 return -1;
Daniel Barkalow2636f612005-04-28 14:46:3355
Florian Forster1d7f1712006-06-18 15:18:0956 type_line = (char *) data + 48;
Daniel Barkalow2636f612005-04-28 14:46:3357 if (memcmp("\ntype ", type_line-1, 6))
Nicolas Pitrebd2c39f2005-05-06 17:48:3458 return -1;
Daniel Barkalow2636f612005-04-28 14:46:3359
Martin Koeglera0393ef2008-01-06 19:03:1060 tag_line = memchr(type_line, '\n', size - (type_line - start));
Daniel Barkalow2636f612005-04-28 14:46:3361 if (!tag_line || memcmp("tag ", ++tag_line, 4))
Nicolas Pitrebd2c39f2005-05-06 17:48:3462 return -1;
Daniel Barkalow2636f612005-04-28 14:46:3363
Martin Koeglera0393ef2008-01-06 19:03:1064 sig_line = memchr(tag_line, '\n', size - (tag_line - start));
Daniel Barkalow2636f612005-04-28 14:46:3365 if (!sig_line)
Nicolas Pitrebd2c39f2005-05-06 17:48:3466 return -1;
Daniel Barkalow2636f612005-04-28 14:46:3367 sig_line++;
68
69 typelen = tag_line - type_line - strlen("type \n");
70 if (typelen >= 20)
Nicolas Pitrebd2c39f2005-05-06 17:48:3471 return -1;
Daniel Barkalow89e42022005-06-22 00:35:1072 memcpy(type, type_line + 5, typelen);
73 type[typelen] = '\0';
Daniel Barkalow2636f612005-04-28 14:46:3374 taglen = sig_line - tag_line - strlen("tag \n");
Pierre Habouzit182af832007-09-15 22:32:3675 item->tag = xmemdupz(tag_line + 4, taglen);
Daniel Barkalow2636f612005-04-28 14:46:3376
Nicolas Pitre0ab17952007-02-26 19:56:0077 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 14:46:3390 return 0;
Nicolas Pitrebd2c39f2005-05-06 17:48:3491}
Sergey Vlasov13019d42005-05-04 17:44:1592
Nicolas Pitrebd2c39f2005-05-06 17:48:3493int parse_tag(struct tag *item)
94{
Nicolas Pitre21666f12007-02-26 19:55:5995 enum object_type type;
Nicolas Pitrebd2c39f2005-05-06 17:48:3496 void *data;
97 unsigned long size;
98 int ret;
99
100 if (item->object.parsed)
101 return 0;
Nicolas Pitre21666f12007-02-26 19:55:59102 data = read_sha1_file(item->object.sha1, &type, &size);
Nicolas Pitrebd2c39f2005-05-06 17:48:34103 if (!data)
104 return error("Could not read %s",
105 sha1_to_hex(item->object.sha1));
Nicolas Pitre21666f12007-02-26 19:55:59106 if (type != OBJ_TAG) {
Nicolas Pitrebd2c39f2005-05-06 17:48:34107 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 17:44:15112 free(data);
Nicolas Pitrebd2c39f2005-05-06 17:48:34113 return ret;
Daniel Barkalow2636f612005-04-28 14:46:33114}