🌐 AI搜索 & 代理 主页
blob: 5b0ac62ed846188ad44ceac41d278ff37ec8bd9d [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
Daniel Barkalow2636f612005-04-28 14:46:337const 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
Jeff King90108a22012-01-06 19:18:0124struct object *deref_tag_noverify(struct object *o)
25{
26 while (o && o->type == OBJ_TAG) {
27 o = parse_object(o->sha1);
28 if (o && o->type == OBJ_TAG && ((struct tag *)o)->tagged)
29 o = ((struct tag *)o)->tagged;
30 else
31 o = NULL;
32 }
33 return o;
34}
35
Jason McMullan5d6ccf52005-06-03 15:05:3936struct tag *lookup_tag(const unsigned char *sha1)
Daniel Barkalow2636f612005-04-28 14:46:3337{
Linus Torvalds100c5f32007-04-17 05:11:4338 struct object *obj = lookup_object(sha1);
39 if (!obj)
Jeff Kingd36f51c2014-07-13 06:41:5540 return create_object(sha1, alloc_tag_node());
Jeff King8ff226a2014-07-13 06:42:0341 return object_as_type(obj, OBJ_TAG, 0);
Daniel Barkalow2636f612005-04-28 14:46:3342}
43
Shawn O. Pearcee451d062010-04-12 23:25:2844static unsigned long parse_tag_date(const char *buf, const char *tail)
45{
46 const char *dateptr;
47
48 while (buf < tail && *buf++ != '>')
49 /* nada */;
50 if (buf >= tail)
51 return 0;
52 dateptr = buf;
53 while (buf < tail && *buf++ != '\n')
54 /* nada */;
55 if (buf >= tail)
56 return 0;
57 /* dateptr < buf && buf[-1] == '\n', so strtoul will stop at buf-1 */
58 return strtoul(dateptr, NULL, 10);
59}
60
Nguyễn Thái Ngọc Duycf7b1ca2011-02-05 10:52:2061int parse_tag_buffer(struct tag *item, const void *data, unsigned long size)
Daniel Barkalow2636f612005-04-28 14:46:3362{
Nicolas Pitre0ab17952007-02-26 19:56:0063 unsigned char sha1[20];
Daniel Barkalow89e42022005-06-22 00:35:1064 char type[20];
Shawn O. Pearce28de5b62010-04-12 23:25:2765 const char *bufptr = data;
66 const char *tail = bufptr + size;
67 const char *nl;
Edgar Toernigae200ee2005-04-30 16:51:0368
Shawn O. Pearce2e0052a2010-04-12 23:25:2569 if (item->object.parsed)
70 return 0;
71 item->object.parsed = 1;
Daniel Barkalow2636f612005-04-28 14:46:3372
Daniel Barkalow2636f612005-04-28 14:46:3373 if (size < 64)
Nicolas Pitrebd2c39f2005-05-06 17:48:3474 return -1;
Shawn O. Pearce28de5b62010-04-12 23:25:2775 if (memcmp("object ", bufptr, 7) || get_sha1_hex(bufptr + 7, sha1) || bufptr[47] != '\n')
Nicolas Pitrebd2c39f2005-05-06 17:48:3476 return -1;
Shawn O. Pearce28de5b62010-04-12 23:25:2777 bufptr += 48; /* "object " + sha1 + "\n" */
Daniel Barkalow2636f612005-04-28 14:46:3378
Christian Couder59556542013-11-30 20:55:4079 if (!starts_with(bufptr, "type "))
Nicolas Pitrebd2c39f2005-05-06 17:48:3480 return -1;
Shawn O. Pearce28de5b62010-04-12 23:25:2781 bufptr += 5;
82 nl = memchr(bufptr, '\n', tail - bufptr);
83 if (!nl || sizeof(type) <= (nl - bufptr))
Nicolas Pitrebd2c39f2005-05-06 17:48:3484 return -1;
Shawn O. Pearce28de5b62010-04-12 23:25:2785 strncpy(type, bufptr, nl - bufptr);
86 type[nl - bufptr] = '\0';
87 bufptr = nl + 1;
Daniel Barkalow2636f612005-04-28 14:46:3388
Nicolas Pitre0ab17952007-02-26 19:56:0089 if (!strcmp(type, blob_type)) {
90 item->tagged = &lookup_blob(sha1)->object;
91 } else if (!strcmp(type, tree_type)) {
92 item->tagged = &lookup_tree(sha1)->object;
93 } else if (!strcmp(type, commit_type)) {
94 item->tagged = &lookup_commit(sha1)->object;
95 } else if (!strcmp(type, tag_type)) {
96 item->tagged = &lookup_tag(sha1)->object;
97 } else {
98 error("Unknown type %s", type);
99 item->tagged = NULL;
100 }
101
Christian Couder59556542013-11-30 20:55:40102 if (bufptr + 4 < tail && starts_with(bufptr, "tag "))
Nguyễn Thái Ngọc Duy85594252011-02-14 13:02:51103 ; /* good */
104 else
Shawn O. Pearce28de5b62010-04-12 23:25:27105 return -1;
106 bufptr += 4;
107 nl = memchr(bufptr, '\n', tail - bufptr);
108 if (!nl)
109 return -1;
110 item->tag = xmemdupz(bufptr, nl - bufptr);
111 bufptr = nl + 1;
112
Christian Couder59556542013-11-30 20:55:40113 if (bufptr + 7 < tail && starts_with(bufptr, "tagger "))
Shawn O. Pearcee451d062010-04-12 23:25:28114 item->date = parse_tag_date(bufptr, tail);
115 else
116 item->date = 0;
117
Daniel Barkalow2636f612005-04-28 14:46:33118 return 0;
Nicolas Pitrebd2c39f2005-05-06 17:48:34119}
Sergey Vlasov13019d42005-05-04 17:44:15120
Nicolas Pitrebd2c39f2005-05-06 17:48:34121int parse_tag(struct tag *item)
122{
Nicolas Pitre21666f12007-02-26 19:55:59123 enum object_type type;
Nicolas Pitrebd2c39f2005-05-06 17:48:34124 void *data;
125 unsigned long size;
126 int ret;
127
128 if (item->object.parsed)
129 return 0;
Nicolas Pitre21666f12007-02-26 19:55:59130 data = read_sha1_file(item->object.sha1, &type, &size);
Nicolas Pitrebd2c39f2005-05-06 17:48:34131 if (!data)
132 return error("Could not read %s",
133 sha1_to_hex(item->object.sha1));
Nicolas Pitre21666f12007-02-26 19:55:59134 if (type != OBJ_TAG) {
Nicolas Pitrebd2c39f2005-05-06 17:48:34135 free(data);
136 return error("Object %s not a tag",
137 sha1_to_hex(item->object.sha1));
138 }
139 ret = parse_tag_buffer(item, data, size);
Sergey Vlasov13019d42005-05-04 17:44:15140 free(data);
Nicolas Pitrebd2c39f2005-05-06 17:48:34141 return ret;
Daniel Barkalow2636f612005-04-28 14:46:33142}